2015-06-25 Andrew MacLeod <amacleod@redhat.com>
[official-gcc.git] / gcc / c / c-parser.c
blobb863dbc64f0360451fc3f4b7b885920d5af70fbe
1 /* Parser for C and Objective-C.
2 Copyright (C) 1987-2015 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 "symtab.h"
43 #include "alias.h"
44 #include "flags.h"
45 #include "tree.h"
46 #include "fold-const.h"
47 #include "stringpool.h"
48 #include "attribs.h"
49 #include "stor-layout.h"
50 #include "varasm.h"
51 #include "trans-mem.h"
52 #include "langhooks.h"
53 #include "cpplib.h"
54 #include "timevar.h"
55 #include "c-family/c-pragma.h"
56 #include "c-tree.h"
57 #include "c-lang.h"
58 #include "flags.h"
59 #include "c-family/c-common.h"
60 #include "c-family/c-objc.h"
61 #include "target.h"
62 #include "hard-reg-set.h"
63 #include "function.h"
64 #include "cgraph.h"
65 #include "plugin.h"
66 #include "omp-low.h"
67 #include "builtins.h"
68 #include "gomp-constants.h"
71 /* Initialization routine for this file. */
73 void
74 c_parse_init (void)
76 /* The only initialization required is of the reserved word
77 identifiers. */
78 unsigned int i;
79 tree id;
80 int mask = 0;
82 /* Make sure RID_MAX hasn't grown past the 8 bits used to hold the keyword in
83 the c_token structure. */
84 gcc_assert (RID_MAX <= 255);
86 mask |= D_CXXONLY;
87 if (!flag_isoc99)
88 mask |= D_C99;
89 if (flag_no_asm)
91 mask |= D_ASM | D_EXT;
92 if (!flag_isoc99)
93 mask |= D_EXT89;
95 if (!c_dialect_objc ())
96 mask |= D_OBJC | D_CXX_OBJC;
98 ridpointers = ggc_cleared_vec_alloc<tree> ((int) RID_MAX);
99 for (i = 0; i < num_c_common_reswords; i++)
101 /* If a keyword is disabled, do not enter it into the table
102 and so create a canonical spelling that isn't a keyword. */
103 if (c_common_reswords[i].disable & mask)
105 if (warn_cxx_compat
106 && (c_common_reswords[i].disable & D_CXXWARN))
108 id = get_identifier (c_common_reswords[i].word);
109 C_SET_RID_CODE (id, RID_CXX_COMPAT_WARN);
110 C_IS_RESERVED_WORD (id) = 1;
112 continue;
115 id = get_identifier (c_common_reswords[i].word);
116 C_SET_RID_CODE (id, c_common_reswords[i].rid);
117 C_IS_RESERVED_WORD (id) = 1;
118 ridpointers [(int) c_common_reswords[i].rid] = id;
121 for (i = 0; i < NUM_INT_N_ENTS; i++)
123 /* We always create the symbols but they aren't always supported. */
124 char name[50];
125 sprintf (name, "__int%d", int_n_data[i].bitsize);
126 id = get_identifier (name);
127 C_SET_RID_CODE (id, RID_FIRST_INT_N + i);
128 C_IS_RESERVED_WORD (id) = 1;
132 /* The C lexer intermediates between the lexer in cpplib and c-lex.c
133 and the C parser. Unlike the C++ lexer, the parser structure
134 stores the lexer information instead of using a separate structure.
135 Identifiers are separated into ordinary identifiers, type names,
136 keywords and some other Objective-C types of identifiers, and some
137 look-ahead is maintained.
139 ??? It might be a good idea to lex the whole file up front (as for
140 C++). It would then be possible to share more of the C and C++
141 lexer code, if desired. */
143 /* More information about the type of a CPP_NAME token. */
144 typedef enum c_id_kind {
145 /* An ordinary identifier. */
146 C_ID_ID,
147 /* An identifier declared as a typedef name. */
148 C_ID_TYPENAME,
149 /* An identifier declared as an Objective-C class name. */
150 C_ID_CLASSNAME,
151 /* An address space identifier. */
152 C_ID_ADDRSPACE,
153 /* Not an identifier. */
154 C_ID_NONE
155 } c_id_kind;
157 /* A single C token after string literal concatenation and conversion
158 of preprocessing tokens to tokens. */
159 typedef struct GTY (()) c_token {
160 /* The kind of token. */
161 ENUM_BITFIELD (cpp_ttype) type : 8;
162 /* If this token is a CPP_NAME, this value indicates whether also
163 declared as some kind of type. Otherwise, it is C_ID_NONE. */
164 ENUM_BITFIELD (c_id_kind) id_kind : 8;
165 /* If this token is a keyword, this value indicates which keyword.
166 Otherwise, this value is RID_MAX. */
167 ENUM_BITFIELD (rid) keyword : 8;
168 /* If this token is a CPP_PRAGMA, this indicates the pragma that
169 was seen. Otherwise it is PRAGMA_NONE. */
170 ENUM_BITFIELD (pragma_kind) pragma_kind : 8;
171 /* The location at which this token was found. */
172 location_t location;
173 /* The value associated with this token, if any. */
174 tree value;
175 } c_token;
177 /* A parser structure recording information about the state and
178 context of parsing. Includes lexer information with up to two
179 tokens of look-ahead; more are not needed for C. */
180 typedef struct GTY(()) c_parser {
181 /* The look-ahead tokens. */
182 c_token * GTY((skip)) tokens;
183 /* Buffer for look-ahead tokens. */
184 c_token tokens_buf[2];
185 /* How many look-ahead tokens are available (0, 1 or 2, or
186 more if parsing from pre-lexed tokens). */
187 unsigned int tokens_avail;
188 /* True if a syntax error is being recovered from; false otherwise.
189 c_parser_error sets this flag. It should clear this flag when
190 enough tokens have been consumed to recover from the error. */
191 BOOL_BITFIELD error : 1;
192 /* True if we're processing a pragma, and shouldn't automatically
193 consume CPP_PRAGMA_EOL. */
194 BOOL_BITFIELD in_pragma : 1;
195 /* True if we're parsing the outermost block of an if statement. */
196 BOOL_BITFIELD in_if_block : 1;
197 /* True if we want to lex an untranslated string. */
198 BOOL_BITFIELD lex_untranslated_string : 1;
200 /* Objective-C specific parser/lexer information. */
202 /* True if we are in a context where the Objective-C "PQ" keywords
203 are considered keywords. */
204 BOOL_BITFIELD objc_pq_context : 1;
205 /* True if we are parsing a (potential) Objective-C foreach
206 statement. This is set to true after we parsed 'for (' and while
207 we wait for 'in' or ';' to decide if it's a standard C for loop or an
208 Objective-C foreach loop. */
209 BOOL_BITFIELD objc_could_be_foreach_context : 1;
210 /* The following flag is needed to contextualize Objective-C lexical
211 analysis. In some cases (e.g., 'int NSObject;'), it is
212 undesirable to bind an identifier to an Objective-C class, even
213 if a class with that name exists. */
214 BOOL_BITFIELD objc_need_raw_identifier : 1;
215 /* Nonzero if we're processing a __transaction statement. The value
216 is 1 | TM_STMT_ATTR_*. */
217 unsigned int in_transaction : 4;
218 /* True if we are in a context where the Objective-C "Property attribute"
219 keywords are valid. */
220 BOOL_BITFIELD objc_property_attr_context : 1;
222 /* Cilk Plus specific parser/lexer information. */
224 /* Buffer to hold all the tokens from parsing the vector attribute for the
225 SIMD-enabled functions (formerly known as elemental functions). */
226 vec <c_token, va_gc> *cilk_simd_fn_tokens;
227 } c_parser;
230 /* The actual parser and external interface. ??? Does this need to be
231 garbage-collected? */
233 static GTY (()) c_parser *the_parser;
235 /* Read in and lex a single token, storing it in *TOKEN. */
237 static void
238 c_lex_one_token (c_parser *parser, c_token *token)
240 timevar_push (TV_LEX);
242 token->type = c_lex_with_flags (&token->value, &token->location, NULL,
243 (parser->lex_untranslated_string
244 ? C_LEX_STRING_NO_TRANSLATE : 0));
245 token->id_kind = C_ID_NONE;
246 token->keyword = RID_MAX;
247 token->pragma_kind = PRAGMA_NONE;
249 switch (token->type)
251 case CPP_NAME:
253 tree decl;
255 bool objc_force_identifier = parser->objc_need_raw_identifier;
256 if (c_dialect_objc ())
257 parser->objc_need_raw_identifier = false;
259 if (C_IS_RESERVED_WORD (token->value))
261 enum rid rid_code = C_RID_CODE (token->value);
263 if (rid_code == RID_CXX_COMPAT_WARN)
265 warning_at (token->location,
266 OPT_Wc___compat,
267 "identifier %qE conflicts with C++ keyword",
268 token->value);
270 else if (rid_code >= RID_FIRST_ADDR_SPACE
271 && rid_code <= RID_LAST_ADDR_SPACE)
273 token->id_kind = C_ID_ADDRSPACE;
274 token->keyword = rid_code;
275 break;
277 else if (c_dialect_objc () && OBJC_IS_PQ_KEYWORD (rid_code))
279 /* We found an Objective-C "pq" keyword (in, out,
280 inout, bycopy, byref, oneway). They need special
281 care because the interpretation depends on the
282 context. */
283 if (parser->objc_pq_context)
285 token->type = CPP_KEYWORD;
286 token->keyword = rid_code;
287 break;
289 else if (parser->objc_could_be_foreach_context
290 && rid_code == RID_IN)
292 /* We are in Objective-C, inside a (potential)
293 foreach context (which means after having
294 parsed 'for (', but before having parsed ';'),
295 and we found 'in'. We consider it the keyword
296 which terminates the declaration at the
297 beginning of a foreach-statement. Note that
298 this means you can't use 'in' for anything else
299 in that context; in particular, in Objective-C
300 you can't use 'in' as the name of the running
301 variable in a C for loop. We could potentially
302 try to add code here to disambiguate, but it
303 seems a reasonable limitation. */
304 token->type = CPP_KEYWORD;
305 token->keyword = rid_code;
306 break;
308 /* Else, "pq" keywords outside of the "pq" context are
309 not keywords, and we fall through to the code for
310 normal tokens. */
312 else if (c_dialect_objc () && OBJC_IS_PATTR_KEYWORD (rid_code))
314 /* We found an Objective-C "property attribute"
315 keyword (getter, setter, readonly, etc). These are
316 only valid in the property context. */
317 if (parser->objc_property_attr_context)
319 token->type = CPP_KEYWORD;
320 token->keyword = rid_code;
321 break;
323 /* Else they are not special keywords.
326 else if (c_dialect_objc ()
327 && (OBJC_IS_AT_KEYWORD (rid_code)
328 || OBJC_IS_CXX_KEYWORD (rid_code)))
330 /* We found one of the Objective-C "@" keywords (defs,
331 selector, synchronized, etc) or one of the
332 Objective-C "cxx" keywords (class, private,
333 protected, public, try, catch, throw) without a
334 preceding '@' sign. Do nothing and fall through to
335 the code for normal tokens (in C++ we would still
336 consider the CXX ones keywords, but not in C). */
339 else
341 token->type = CPP_KEYWORD;
342 token->keyword = rid_code;
343 break;
347 decl = lookup_name (token->value);
348 if (decl)
350 if (TREE_CODE (decl) == TYPE_DECL)
352 token->id_kind = C_ID_TYPENAME;
353 break;
356 else if (c_dialect_objc ())
358 tree objc_interface_decl = objc_is_class_name (token->value);
359 /* Objective-C class names are in the same namespace as
360 variables and typedefs, and hence are shadowed by local
361 declarations. */
362 if (objc_interface_decl
363 && (!objc_force_identifier || global_bindings_p ()))
365 token->value = objc_interface_decl;
366 token->id_kind = C_ID_CLASSNAME;
367 break;
370 token->id_kind = C_ID_ID;
372 break;
373 case CPP_AT_NAME:
374 /* This only happens in Objective-C; it must be a keyword. */
375 token->type = CPP_KEYWORD;
376 switch (C_RID_CODE (token->value))
378 /* Replace 'class' with '@class', 'private' with '@private',
379 etc. This prevents confusion with the C++ keyword
380 'class', and makes the tokens consistent with other
381 Objective-C 'AT' keywords. For example '@class' is
382 reported as RID_AT_CLASS which is consistent with
383 '@synchronized', which is reported as
384 RID_AT_SYNCHRONIZED.
386 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
387 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
388 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
389 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
390 case RID_THROW: token->keyword = RID_AT_THROW; break;
391 case RID_TRY: token->keyword = RID_AT_TRY; break;
392 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
393 default: token->keyword = C_RID_CODE (token->value);
395 break;
396 case CPP_COLON:
397 case CPP_COMMA:
398 case CPP_CLOSE_PAREN:
399 case CPP_SEMICOLON:
400 /* These tokens may affect the interpretation of any identifiers
401 following, if doing Objective-C. */
402 if (c_dialect_objc ())
403 parser->objc_need_raw_identifier = false;
404 break;
405 case CPP_PRAGMA:
406 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
407 token->pragma_kind = (enum pragma_kind) TREE_INT_CST_LOW (token->value);
408 token->value = NULL;
409 break;
410 default:
411 break;
413 timevar_pop (TV_LEX);
416 /* Return a pointer to the next token from PARSER, reading it in if
417 necessary. */
419 static inline c_token *
420 c_parser_peek_token (c_parser *parser)
422 if (parser->tokens_avail == 0)
424 c_lex_one_token (parser, &parser->tokens[0]);
425 parser->tokens_avail = 1;
427 return &parser->tokens[0];
430 /* Return true if the next token from PARSER has the indicated
431 TYPE. */
433 static inline bool
434 c_parser_next_token_is (c_parser *parser, enum cpp_ttype type)
436 return c_parser_peek_token (parser)->type == type;
439 /* Return true if the next token from PARSER does not have the
440 indicated TYPE. */
442 static inline bool
443 c_parser_next_token_is_not (c_parser *parser, enum cpp_ttype type)
445 return !c_parser_next_token_is (parser, type);
448 /* Return true if the next token from PARSER is the indicated
449 KEYWORD. */
451 static inline bool
452 c_parser_next_token_is_keyword (c_parser *parser, enum rid keyword)
454 return c_parser_peek_token (parser)->keyword == keyword;
457 /* Return a pointer to the next-but-one token from PARSER, reading it
458 in if necessary. The next token is already read in. */
460 static c_token *
461 c_parser_peek_2nd_token (c_parser *parser)
463 if (parser->tokens_avail >= 2)
464 return &parser->tokens[1];
465 gcc_assert (parser->tokens_avail == 1);
466 gcc_assert (parser->tokens[0].type != CPP_EOF);
467 gcc_assert (parser->tokens[0].type != CPP_PRAGMA_EOL);
468 c_lex_one_token (parser, &parser->tokens[1]);
469 parser->tokens_avail = 2;
470 return &parser->tokens[1];
473 /* Return true if TOKEN can start a type name,
474 false otherwise. */
475 static bool
476 c_token_starts_typename (c_token *token)
478 switch (token->type)
480 case CPP_NAME:
481 switch (token->id_kind)
483 case C_ID_ID:
484 return false;
485 case C_ID_ADDRSPACE:
486 return true;
487 case C_ID_TYPENAME:
488 return true;
489 case C_ID_CLASSNAME:
490 gcc_assert (c_dialect_objc ());
491 return true;
492 default:
493 gcc_unreachable ();
495 case CPP_KEYWORD:
496 switch (token->keyword)
498 case RID_UNSIGNED:
499 case RID_LONG:
500 case RID_SHORT:
501 case RID_SIGNED:
502 case RID_COMPLEX:
503 case RID_INT:
504 case RID_CHAR:
505 case RID_FLOAT:
506 case RID_DOUBLE:
507 case RID_VOID:
508 case RID_DFLOAT32:
509 case RID_DFLOAT64:
510 case RID_DFLOAT128:
511 case RID_BOOL:
512 case RID_ENUM:
513 case RID_STRUCT:
514 case RID_UNION:
515 case RID_TYPEOF:
516 case RID_CONST:
517 case RID_ATOMIC:
518 case RID_VOLATILE:
519 case RID_RESTRICT:
520 case RID_ATTRIBUTE:
521 case RID_FRACT:
522 case RID_ACCUM:
523 case RID_SAT:
524 case RID_AUTO_TYPE:
525 return true;
526 default:
527 if (token->keyword >= RID_FIRST_INT_N
528 && token->keyword < RID_FIRST_INT_N + NUM_INT_N_ENTS
529 && int_n_enabled_p[token->keyword - RID_FIRST_INT_N])
530 return true;
531 return false;
533 case CPP_LESS:
534 if (c_dialect_objc ())
535 return true;
536 return false;
537 default:
538 return false;
542 enum c_lookahead_kind {
543 /* Always treat unknown identifiers as typenames. */
544 cla_prefer_type,
546 /* Could be parsing a nonabstract declarator. Only treat an identifier
547 as a typename if followed by another identifier or a star. */
548 cla_nonabstract_decl,
550 /* Never treat identifiers as typenames. */
551 cla_prefer_id
554 /* Return true if the next token from PARSER can start a type name,
555 false otherwise. LA specifies how to do lookahead in order to
556 detect unknown type names. If unsure, pick CLA_PREFER_ID. */
558 static inline bool
559 c_parser_next_tokens_start_typename (c_parser *parser, enum c_lookahead_kind la)
561 c_token *token = c_parser_peek_token (parser);
562 if (c_token_starts_typename (token))
563 return true;
565 /* Try a bit harder to detect an unknown typename. */
566 if (la != cla_prefer_id
567 && token->type == CPP_NAME
568 && token->id_kind == C_ID_ID
570 /* Do not try too hard when we could have "object in array". */
571 && !parser->objc_could_be_foreach_context
573 && (la == cla_prefer_type
574 || c_parser_peek_2nd_token (parser)->type == CPP_NAME
575 || c_parser_peek_2nd_token (parser)->type == CPP_MULT)
577 /* Only unknown identifiers. */
578 && !lookup_name (token->value))
579 return true;
581 return false;
584 /* Return true if TOKEN is a type qualifier, false otherwise. */
585 static bool
586 c_token_is_qualifier (c_token *token)
588 switch (token->type)
590 case CPP_NAME:
591 switch (token->id_kind)
593 case C_ID_ADDRSPACE:
594 return true;
595 default:
596 return false;
598 case CPP_KEYWORD:
599 switch (token->keyword)
601 case RID_CONST:
602 case RID_VOLATILE:
603 case RID_RESTRICT:
604 case RID_ATTRIBUTE:
605 case RID_ATOMIC:
606 return true;
607 default:
608 return false;
610 case CPP_LESS:
611 return false;
612 default:
613 gcc_unreachable ();
617 /* Return true if the next token from PARSER is a type qualifier,
618 false otherwise. */
619 static inline bool
620 c_parser_next_token_is_qualifier (c_parser *parser)
622 c_token *token = c_parser_peek_token (parser);
623 return c_token_is_qualifier (token);
626 /* Return true if TOKEN can start declaration specifiers, false
627 otherwise. */
628 static bool
629 c_token_starts_declspecs (c_token *token)
631 switch (token->type)
633 case CPP_NAME:
634 switch (token->id_kind)
636 case C_ID_ID:
637 return false;
638 case C_ID_ADDRSPACE:
639 return true;
640 case C_ID_TYPENAME:
641 return true;
642 case C_ID_CLASSNAME:
643 gcc_assert (c_dialect_objc ());
644 return true;
645 default:
646 gcc_unreachable ();
648 case CPP_KEYWORD:
649 switch (token->keyword)
651 case RID_STATIC:
652 case RID_EXTERN:
653 case RID_REGISTER:
654 case RID_TYPEDEF:
655 case RID_INLINE:
656 case RID_NORETURN:
657 case RID_AUTO:
658 case RID_THREAD:
659 case RID_UNSIGNED:
660 case RID_LONG:
661 case RID_SHORT:
662 case RID_SIGNED:
663 case RID_COMPLEX:
664 case RID_INT:
665 case RID_CHAR:
666 case RID_FLOAT:
667 case RID_DOUBLE:
668 case RID_VOID:
669 case RID_DFLOAT32:
670 case RID_DFLOAT64:
671 case RID_DFLOAT128:
672 case RID_BOOL:
673 case RID_ENUM:
674 case RID_STRUCT:
675 case RID_UNION:
676 case RID_TYPEOF:
677 case RID_CONST:
678 case RID_VOLATILE:
679 case RID_RESTRICT:
680 case RID_ATTRIBUTE:
681 case RID_FRACT:
682 case RID_ACCUM:
683 case RID_SAT:
684 case RID_ALIGNAS:
685 case RID_ATOMIC:
686 case RID_AUTO_TYPE:
687 return true;
688 default:
689 if (token->keyword >= RID_FIRST_INT_N
690 && token->keyword < RID_FIRST_INT_N + NUM_INT_N_ENTS
691 && int_n_enabled_p[token->keyword - RID_FIRST_INT_N])
692 return true;
693 return false;
695 case CPP_LESS:
696 if (c_dialect_objc ())
697 return true;
698 return false;
699 default:
700 return false;
705 /* Return true if TOKEN can start declaration specifiers or a static
706 assertion, false otherwise. */
707 static bool
708 c_token_starts_declaration (c_token *token)
710 if (c_token_starts_declspecs (token)
711 || token->keyword == RID_STATIC_ASSERT)
712 return true;
713 else
714 return false;
717 /* Return true if the next token from PARSER can start declaration
718 specifiers, false otherwise. */
719 static inline bool
720 c_parser_next_token_starts_declspecs (c_parser *parser)
722 c_token *token = c_parser_peek_token (parser);
724 /* In Objective-C, a classname normally starts a declspecs unless it
725 is immediately followed by a dot. In that case, it is the
726 Objective-C 2.0 "dot-syntax" for class objects, ie, calls the
727 setter/getter on the class. c_token_starts_declspecs() can't
728 differentiate between the two cases because it only checks the
729 current token, so we have a special check here. */
730 if (c_dialect_objc ()
731 && token->type == CPP_NAME
732 && token->id_kind == C_ID_CLASSNAME
733 && c_parser_peek_2nd_token (parser)->type == CPP_DOT)
734 return false;
736 return c_token_starts_declspecs (token);
739 /* Return true if the next tokens from PARSER can start declaration
740 specifiers or a static assertion, false otherwise. */
741 static inline bool
742 c_parser_next_tokens_start_declaration (c_parser *parser)
744 c_token *token = c_parser_peek_token (parser);
746 /* Same as above. */
747 if (c_dialect_objc ()
748 && token->type == CPP_NAME
749 && token->id_kind == C_ID_CLASSNAME
750 && c_parser_peek_2nd_token (parser)->type == CPP_DOT)
751 return false;
753 /* Labels do not start declarations. */
754 if (token->type == CPP_NAME
755 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
756 return false;
758 if (c_token_starts_declaration (token))
759 return true;
761 if (c_parser_next_tokens_start_typename (parser, cla_nonabstract_decl))
762 return true;
764 return false;
767 /* Consume the next token from PARSER. */
769 static void
770 c_parser_consume_token (c_parser *parser)
772 gcc_assert (parser->tokens_avail >= 1);
773 gcc_assert (parser->tokens[0].type != CPP_EOF);
774 gcc_assert (!parser->in_pragma || parser->tokens[0].type != CPP_PRAGMA_EOL);
775 gcc_assert (parser->error || parser->tokens[0].type != CPP_PRAGMA);
776 if (parser->tokens != &parser->tokens_buf[0])
777 parser->tokens++;
778 else if (parser->tokens_avail == 2)
779 parser->tokens[0] = parser->tokens[1];
780 parser->tokens_avail--;
783 /* Expect the current token to be a #pragma. Consume it and remember
784 that we've begun parsing a pragma. */
786 static void
787 c_parser_consume_pragma (c_parser *parser)
789 gcc_assert (!parser->in_pragma);
790 gcc_assert (parser->tokens_avail >= 1);
791 gcc_assert (parser->tokens[0].type == CPP_PRAGMA);
792 if (parser->tokens != &parser->tokens_buf[0])
793 parser->tokens++;
794 else if (parser->tokens_avail == 2)
795 parser->tokens[0] = parser->tokens[1];
796 parser->tokens_avail--;
797 parser->in_pragma = true;
800 /* Update the global input_location from TOKEN. */
801 static inline void
802 c_parser_set_source_position_from_token (c_token *token)
804 if (token->type != CPP_EOF)
806 input_location = token->location;
810 /* Issue a diagnostic of the form
811 FILE:LINE: MESSAGE before TOKEN
812 where TOKEN is the next token in the input stream of PARSER.
813 MESSAGE (specified by the caller) is usually of the form "expected
814 OTHER-TOKEN".
816 Do not issue a diagnostic if still recovering from an error.
818 ??? This is taken from the C++ parser, but building up messages in
819 this way is not i18n-friendly and some other approach should be
820 used. */
822 static void
823 c_parser_error (c_parser *parser, const char *gmsgid)
825 c_token *token = c_parser_peek_token (parser);
826 if (parser->error)
827 return;
828 parser->error = true;
829 if (!gmsgid)
830 return;
831 /* This diagnostic makes more sense if it is tagged to the line of
832 the token we just peeked at. */
833 c_parser_set_source_position_from_token (token);
834 c_parse_error (gmsgid,
835 /* Because c_parse_error does not understand
836 CPP_KEYWORD, keywords are treated like
837 identifiers. */
838 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
839 /* ??? The C parser does not save the cpp flags of a
840 token, we need to pass 0 here and we will not get
841 the source spelling of some tokens but rather the
842 canonical spelling. */
843 token->value, /*flags=*/0);
846 /* If the next token is of the indicated TYPE, consume it. Otherwise,
847 issue the error MSGID. If MSGID is NULL then a message has already
848 been produced and no message will be produced this time. Returns
849 true if found, false otherwise. */
851 static bool
852 c_parser_require (c_parser *parser,
853 enum cpp_ttype type,
854 const char *msgid)
856 if (c_parser_next_token_is (parser, type))
858 c_parser_consume_token (parser);
859 return true;
861 else
863 c_parser_error (parser, msgid);
864 return false;
868 /* If the next token is the indicated keyword, consume it. Otherwise,
869 issue the error MSGID. Returns true if found, false otherwise. */
871 static bool
872 c_parser_require_keyword (c_parser *parser,
873 enum rid keyword,
874 const char *msgid)
876 if (c_parser_next_token_is_keyword (parser, keyword))
878 c_parser_consume_token (parser);
879 return true;
881 else
883 c_parser_error (parser, msgid);
884 return false;
888 /* Like c_parser_require, except that tokens will be skipped until the
889 desired token is found. An error message is still produced if the
890 next token is not as expected. If MSGID is NULL then a message has
891 already been produced and no message will be produced this
892 time. */
894 static void
895 c_parser_skip_until_found (c_parser *parser,
896 enum cpp_ttype type,
897 const char *msgid)
899 unsigned nesting_depth = 0;
901 if (c_parser_require (parser, type, msgid))
902 return;
904 /* Skip tokens until the desired token is found. */
905 while (true)
907 /* Peek at the next token. */
908 c_token *token = c_parser_peek_token (parser);
909 /* If we've reached the token we want, consume it and stop. */
910 if (token->type == type && !nesting_depth)
912 c_parser_consume_token (parser);
913 break;
916 /* If we've run out of tokens, stop. */
917 if (token->type == CPP_EOF)
918 return;
919 if (token->type == CPP_PRAGMA_EOL && parser->in_pragma)
920 return;
921 if (token->type == CPP_OPEN_BRACE
922 || token->type == CPP_OPEN_PAREN
923 || token->type == CPP_OPEN_SQUARE)
924 ++nesting_depth;
925 else if (token->type == CPP_CLOSE_BRACE
926 || token->type == CPP_CLOSE_PAREN
927 || token->type == CPP_CLOSE_SQUARE)
929 if (nesting_depth-- == 0)
930 break;
932 /* Consume this token. */
933 c_parser_consume_token (parser);
935 parser->error = false;
938 /* Skip tokens until the end of a parameter is found, but do not
939 consume the comma, semicolon or closing delimiter. */
941 static void
942 c_parser_skip_to_end_of_parameter (c_parser *parser)
944 unsigned nesting_depth = 0;
946 while (true)
948 c_token *token = c_parser_peek_token (parser);
949 if ((token->type == CPP_COMMA || token->type == CPP_SEMICOLON)
950 && !nesting_depth)
951 break;
952 /* If we've run out of tokens, stop. */
953 if (token->type == CPP_EOF)
954 return;
955 if (token->type == CPP_PRAGMA_EOL && parser->in_pragma)
956 return;
957 if (token->type == CPP_OPEN_BRACE
958 || token->type == CPP_OPEN_PAREN
959 || token->type == CPP_OPEN_SQUARE)
960 ++nesting_depth;
961 else if (token->type == CPP_CLOSE_BRACE
962 || token->type == CPP_CLOSE_PAREN
963 || token->type == CPP_CLOSE_SQUARE)
965 if (nesting_depth-- == 0)
966 break;
968 /* Consume this token. */
969 c_parser_consume_token (parser);
971 parser->error = false;
974 /* Expect to be at the end of the pragma directive and consume an
975 end of line marker. */
977 static void
978 c_parser_skip_to_pragma_eol (c_parser *parser, bool error_if_not_eol = true)
980 gcc_assert (parser->in_pragma);
981 parser->in_pragma = false;
983 if (error_if_not_eol && c_parser_peek_token (parser)->type != CPP_PRAGMA_EOL)
984 c_parser_error (parser, "expected end of line");
986 cpp_ttype token_type;
989 c_token *token = c_parser_peek_token (parser);
990 token_type = token->type;
991 if (token_type == CPP_EOF)
992 break;
993 c_parser_consume_token (parser);
995 while (token_type != CPP_PRAGMA_EOL);
997 parser->error = false;
1000 /* Skip tokens until we have consumed an entire block, or until we
1001 have consumed a non-nested ';'. */
1003 static void
1004 c_parser_skip_to_end_of_block_or_statement (c_parser *parser)
1006 unsigned nesting_depth = 0;
1007 bool save_error = parser->error;
1009 while (true)
1011 c_token *token;
1013 /* Peek at the next token. */
1014 token = c_parser_peek_token (parser);
1016 switch (token->type)
1018 case CPP_EOF:
1019 return;
1021 case CPP_PRAGMA_EOL:
1022 if (parser->in_pragma)
1023 return;
1024 break;
1026 case CPP_SEMICOLON:
1027 /* If the next token is a ';', we have reached the
1028 end of the statement. */
1029 if (!nesting_depth)
1031 /* Consume the ';'. */
1032 c_parser_consume_token (parser);
1033 goto finished;
1035 break;
1037 case CPP_CLOSE_BRACE:
1038 /* If the next token is a non-nested '}', then we have
1039 reached the end of the current block. */
1040 if (nesting_depth == 0 || --nesting_depth == 0)
1042 c_parser_consume_token (parser);
1043 goto finished;
1045 break;
1047 case CPP_OPEN_BRACE:
1048 /* If it the next token is a '{', then we are entering a new
1049 block. Consume the entire block. */
1050 ++nesting_depth;
1051 break;
1053 case CPP_PRAGMA:
1054 /* If we see a pragma, consume the whole thing at once. We
1055 have some safeguards against consuming pragmas willy-nilly.
1056 Normally, we'd expect to be here with parser->error set,
1057 which disables these safeguards. But it's possible to get
1058 here for secondary error recovery, after parser->error has
1059 been cleared. */
1060 c_parser_consume_pragma (parser);
1061 c_parser_skip_to_pragma_eol (parser);
1062 parser->error = save_error;
1063 continue;
1065 default:
1066 break;
1069 c_parser_consume_token (parser);
1072 finished:
1073 parser->error = false;
1076 /* CPP's options (initialized by c-opts.c). */
1077 extern cpp_options *cpp_opts;
1079 /* Save the warning flags which are controlled by __extension__. */
1081 static inline int
1082 disable_extension_diagnostics (void)
1084 int ret = (pedantic
1085 | (warn_pointer_arith << 1)
1086 | (warn_traditional << 2)
1087 | (flag_iso << 3)
1088 | (warn_long_long << 4)
1089 | (warn_cxx_compat << 5)
1090 | (warn_overlength_strings << 6)
1091 /* warn_c90_c99_compat has three states: -1/0/1, so we must
1092 play tricks to properly restore it. */
1093 | ((warn_c90_c99_compat == 1) << 7)
1094 | ((warn_c90_c99_compat == -1) << 8)
1095 /* Similarly for warn_c99_c11_compat. */
1096 | ((warn_c99_c11_compat == 1) << 9)
1097 | ((warn_c99_c11_compat == -1) << 10)
1099 cpp_opts->cpp_pedantic = pedantic = 0;
1100 warn_pointer_arith = 0;
1101 cpp_opts->cpp_warn_traditional = warn_traditional = 0;
1102 flag_iso = 0;
1103 cpp_opts->cpp_warn_long_long = warn_long_long = 0;
1104 warn_cxx_compat = 0;
1105 warn_overlength_strings = 0;
1106 warn_c90_c99_compat = 0;
1107 warn_c99_c11_compat = 0;
1108 return ret;
1111 /* Restore the warning flags which are controlled by __extension__.
1112 FLAGS is the return value from disable_extension_diagnostics. */
1114 static inline void
1115 restore_extension_diagnostics (int flags)
1117 cpp_opts->cpp_pedantic = pedantic = flags & 1;
1118 warn_pointer_arith = (flags >> 1) & 1;
1119 cpp_opts->cpp_warn_traditional = warn_traditional = (flags >> 2) & 1;
1120 flag_iso = (flags >> 3) & 1;
1121 cpp_opts->cpp_warn_long_long = warn_long_long = (flags >> 4) & 1;
1122 warn_cxx_compat = (flags >> 5) & 1;
1123 warn_overlength_strings = (flags >> 6) & 1;
1124 /* See above for why is this needed. */
1125 warn_c90_c99_compat = (flags >> 7) & 1 ? 1 : ((flags >> 8) & 1 ? -1 : 0);
1126 warn_c99_c11_compat = (flags >> 9) & 1 ? 1 : ((flags >> 10) & 1 ? -1 : 0);
1129 /* Possibly kinds of declarator to parse. */
1130 typedef enum c_dtr_syn {
1131 /* A normal declarator with an identifier. */
1132 C_DTR_NORMAL,
1133 /* An abstract declarator (maybe empty). */
1134 C_DTR_ABSTRACT,
1135 /* A parameter declarator: may be either, but after a type name does
1136 not redeclare a typedef name as an identifier if it can
1137 alternatively be interpreted as a typedef name; see DR#009,
1138 applied in C90 TC1, omitted from C99 and reapplied in C99 TC2
1139 following DR#249. For example, given a typedef T, "int T" and
1140 "int *T" are valid parameter declarations redeclaring T, while
1141 "int (T)" and "int * (T)" and "int (T[])" and "int (T (int))" are
1142 abstract declarators rather than involving redundant parentheses;
1143 the same applies with attributes inside the parentheses before
1144 "T". */
1145 C_DTR_PARM
1146 } c_dtr_syn;
1148 /* The binary operation precedence levels, where 0 is a dummy lowest level
1149 used for the bottom of the stack. */
1150 enum c_parser_prec {
1151 PREC_NONE,
1152 PREC_LOGOR,
1153 PREC_LOGAND,
1154 PREC_BITOR,
1155 PREC_BITXOR,
1156 PREC_BITAND,
1157 PREC_EQ,
1158 PREC_REL,
1159 PREC_SHIFT,
1160 PREC_ADD,
1161 PREC_MULT,
1162 NUM_PRECS
1165 static void c_parser_external_declaration (c_parser *);
1166 static void c_parser_asm_definition (c_parser *);
1167 static void c_parser_declaration_or_fndef (c_parser *, bool, bool, bool,
1168 bool, bool, tree *, vec<c_token>);
1169 static void c_parser_static_assert_declaration_no_semi (c_parser *);
1170 static void c_parser_static_assert_declaration (c_parser *);
1171 static void c_parser_declspecs (c_parser *, struct c_declspecs *, bool, bool,
1172 bool, bool, bool, enum c_lookahead_kind);
1173 static struct c_typespec c_parser_enum_specifier (c_parser *);
1174 static struct c_typespec c_parser_struct_or_union_specifier (c_parser *);
1175 static tree c_parser_struct_declaration (c_parser *);
1176 static struct c_typespec c_parser_typeof_specifier (c_parser *);
1177 static tree c_parser_alignas_specifier (c_parser *);
1178 static struct c_declarator *c_parser_declarator (c_parser *, bool, c_dtr_syn,
1179 bool *);
1180 static struct c_declarator *c_parser_direct_declarator (c_parser *, bool,
1181 c_dtr_syn, bool *);
1182 static struct c_declarator *c_parser_direct_declarator_inner (c_parser *,
1183 bool,
1184 struct c_declarator *);
1185 static struct c_arg_info *c_parser_parms_declarator (c_parser *, bool, tree);
1186 static struct c_arg_info *c_parser_parms_list_declarator (c_parser *, tree,
1187 tree);
1188 static struct c_parm *c_parser_parameter_declaration (c_parser *, tree);
1189 static tree c_parser_simple_asm_expr (c_parser *);
1190 static tree c_parser_attributes (c_parser *);
1191 static struct c_type_name *c_parser_type_name (c_parser *);
1192 static struct c_expr c_parser_initializer (c_parser *);
1193 static struct c_expr c_parser_braced_init (c_parser *, tree, bool);
1194 static void c_parser_initelt (c_parser *, struct obstack *);
1195 static void c_parser_initval (c_parser *, struct c_expr *,
1196 struct obstack *);
1197 static tree c_parser_compound_statement (c_parser *);
1198 static void c_parser_compound_statement_nostart (c_parser *);
1199 static void c_parser_label (c_parser *);
1200 static void c_parser_statement (c_parser *);
1201 static void c_parser_statement_after_labels (c_parser *);
1202 static void c_parser_if_statement (c_parser *);
1203 static void c_parser_switch_statement (c_parser *);
1204 static void c_parser_while_statement (c_parser *, bool);
1205 static void c_parser_do_statement (c_parser *, bool);
1206 static void c_parser_for_statement (c_parser *, bool);
1207 static tree c_parser_asm_statement (c_parser *);
1208 static tree c_parser_asm_operands (c_parser *);
1209 static tree c_parser_asm_goto_operands (c_parser *);
1210 static tree c_parser_asm_clobbers (c_parser *);
1211 static struct c_expr c_parser_expr_no_commas (c_parser *, struct c_expr *,
1212 tree = NULL_TREE);
1213 static struct c_expr c_parser_conditional_expression (c_parser *,
1214 struct c_expr *, tree);
1215 static struct c_expr c_parser_binary_expression (c_parser *, struct c_expr *,
1216 tree);
1217 static struct c_expr c_parser_cast_expression (c_parser *, struct c_expr *);
1218 static struct c_expr c_parser_unary_expression (c_parser *);
1219 static struct c_expr c_parser_sizeof_expression (c_parser *);
1220 static struct c_expr c_parser_alignof_expression (c_parser *);
1221 static struct c_expr c_parser_postfix_expression (c_parser *);
1222 static struct c_expr c_parser_postfix_expression_after_paren_type (c_parser *,
1223 struct c_type_name *,
1224 location_t);
1225 static struct c_expr c_parser_postfix_expression_after_primary (c_parser *,
1226 location_t loc,
1227 struct c_expr);
1228 static tree c_parser_transaction (c_parser *, enum rid);
1229 static struct c_expr c_parser_transaction_expression (c_parser *, enum rid);
1230 static tree c_parser_transaction_cancel (c_parser *);
1231 static struct c_expr c_parser_expression (c_parser *);
1232 static struct c_expr c_parser_expression_conv (c_parser *);
1233 static vec<tree, va_gc> *c_parser_expr_list (c_parser *, bool, bool,
1234 vec<tree, va_gc> **, location_t *,
1235 tree *, vec<location_t> *,
1236 unsigned int * = NULL);
1237 static void c_parser_oacc_enter_exit_data (c_parser *, bool);
1238 static void c_parser_oacc_update (c_parser *);
1239 static tree c_parser_oacc_loop (location_t, c_parser *, char *);
1240 static void c_parser_omp_construct (c_parser *);
1241 static void c_parser_omp_threadprivate (c_parser *);
1242 static void c_parser_omp_barrier (c_parser *);
1243 static void c_parser_omp_flush (c_parser *);
1244 static tree c_parser_omp_for_loop (location_t, c_parser *, enum tree_code,
1245 tree, tree *);
1246 static void c_parser_omp_taskwait (c_parser *);
1247 static void c_parser_omp_taskyield (c_parser *);
1248 static void c_parser_omp_cancel (c_parser *);
1249 static void c_parser_omp_cancellation_point (c_parser *);
1251 enum pragma_context { pragma_external, pragma_struct, pragma_param,
1252 pragma_stmt, pragma_compound };
1253 static bool c_parser_pragma (c_parser *, enum pragma_context);
1254 static bool c_parser_omp_target (c_parser *, enum pragma_context);
1255 static void c_parser_omp_end_declare_target (c_parser *);
1256 static void c_parser_omp_declare (c_parser *, enum pragma_context);
1258 /* These Objective-C parser functions are only ever called when
1259 compiling Objective-C. */
1260 static void c_parser_objc_class_definition (c_parser *, tree);
1261 static void c_parser_objc_class_instance_variables (c_parser *);
1262 static void c_parser_objc_class_declaration (c_parser *);
1263 static void c_parser_objc_alias_declaration (c_parser *);
1264 static void c_parser_objc_protocol_definition (c_parser *, tree);
1265 static bool c_parser_objc_method_type (c_parser *);
1266 static void c_parser_objc_method_definition (c_parser *);
1267 static void c_parser_objc_methodprotolist (c_parser *);
1268 static void c_parser_objc_methodproto (c_parser *);
1269 static tree c_parser_objc_method_decl (c_parser *, bool, tree *, tree *);
1270 static tree c_parser_objc_type_name (c_parser *);
1271 static tree c_parser_objc_protocol_refs (c_parser *);
1272 static void c_parser_objc_try_catch_finally_statement (c_parser *);
1273 static void c_parser_objc_synchronized_statement (c_parser *);
1274 static tree c_parser_objc_selector (c_parser *);
1275 static tree c_parser_objc_selector_arg (c_parser *);
1276 static tree c_parser_objc_receiver (c_parser *);
1277 static tree c_parser_objc_message_args (c_parser *);
1278 static tree c_parser_objc_keywordexpr (c_parser *);
1279 static void c_parser_objc_at_property_declaration (c_parser *);
1280 static void c_parser_objc_at_synthesize_declaration (c_parser *);
1281 static void c_parser_objc_at_dynamic_declaration (c_parser *);
1282 static bool c_parser_objc_diagnose_bad_element_prefix
1283 (c_parser *, struct c_declspecs *);
1285 /* Cilk Plus supporting routines. */
1286 static void c_parser_cilk_simd (c_parser *);
1287 static void c_parser_cilk_for (c_parser *, tree);
1288 static bool c_parser_cilk_verify_simd (c_parser *, enum pragma_context);
1289 static tree c_parser_array_notation (location_t, c_parser *, tree, tree);
1290 static tree c_parser_cilk_clause_vectorlength (c_parser *, tree, bool);
1291 static void c_parser_cilk_grainsize (c_parser *);
1293 /* Parse a translation unit (C90 6.7, C99 6.9).
1295 translation-unit:
1296 external-declarations
1298 external-declarations:
1299 external-declaration
1300 external-declarations external-declaration
1302 GNU extensions:
1304 translation-unit:
1305 empty
1308 static void
1309 c_parser_translation_unit (c_parser *parser)
1311 if (c_parser_next_token_is (parser, CPP_EOF))
1313 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
1314 "ISO C forbids an empty translation unit");
1316 else
1318 void *obstack_position = obstack_alloc (&parser_obstack, 0);
1319 mark_valid_location_for_stdc_pragma (false);
1322 ggc_collect ();
1323 c_parser_external_declaration (parser);
1324 obstack_free (&parser_obstack, obstack_position);
1326 while (c_parser_next_token_is_not (parser, CPP_EOF));
1330 /* Parse an external declaration (C90 6.7, C99 6.9).
1332 external-declaration:
1333 function-definition
1334 declaration
1336 GNU extensions:
1338 external-declaration:
1339 asm-definition
1341 __extension__ external-declaration
1343 Objective-C:
1345 external-declaration:
1346 objc-class-definition
1347 objc-class-declaration
1348 objc-alias-declaration
1349 objc-protocol-definition
1350 objc-method-definition
1351 @end
1354 static void
1355 c_parser_external_declaration (c_parser *parser)
1357 int ext;
1358 switch (c_parser_peek_token (parser)->type)
1360 case CPP_KEYWORD:
1361 switch (c_parser_peek_token (parser)->keyword)
1363 case RID_EXTENSION:
1364 ext = disable_extension_diagnostics ();
1365 c_parser_consume_token (parser);
1366 c_parser_external_declaration (parser);
1367 restore_extension_diagnostics (ext);
1368 break;
1369 case RID_ASM:
1370 c_parser_asm_definition (parser);
1371 break;
1372 case RID_AT_INTERFACE:
1373 case RID_AT_IMPLEMENTATION:
1374 gcc_assert (c_dialect_objc ());
1375 c_parser_objc_class_definition (parser, NULL_TREE);
1376 break;
1377 case RID_AT_CLASS:
1378 gcc_assert (c_dialect_objc ());
1379 c_parser_objc_class_declaration (parser);
1380 break;
1381 case RID_AT_ALIAS:
1382 gcc_assert (c_dialect_objc ());
1383 c_parser_objc_alias_declaration (parser);
1384 break;
1385 case RID_AT_PROTOCOL:
1386 gcc_assert (c_dialect_objc ());
1387 c_parser_objc_protocol_definition (parser, NULL_TREE);
1388 break;
1389 case RID_AT_PROPERTY:
1390 gcc_assert (c_dialect_objc ());
1391 c_parser_objc_at_property_declaration (parser);
1392 break;
1393 case RID_AT_SYNTHESIZE:
1394 gcc_assert (c_dialect_objc ());
1395 c_parser_objc_at_synthesize_declaration (parser);
1396 break;
1397 case RID_AT_DYNAMIC:
1398 gcc_assert (c_dialect_objc ());
1399 c_parser_objc_at_dynamic_declaration (parser);
1400 break;
1401 case RID_AT_END:
1402 gcc_assert (c_dialect_objc ());
1403 c_parser_consume_token (parser);
1404 objc_finish_implementation ();
1405 break;
1406 default:
1407 goto decl_or_fndef;
1409 break;
1410 case CPP_SEMICOLON:
1411 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
1412 "ISO C does not allow extra %<;%> outside of a function");
1413 c_parser_consume_token (parser);
1414 break;
1415 case CPP_PRAGMA:
1416 mark_valid_location_for_stdc_pragma (true);
1417 c_parser_pragma (parser, pragma_external);
1418 mark_valid_location_for_stdc_pragma (false);
1419 break;
1420 case CPP_PLUS:
1421 case CPP_MINUS:
1422 if (c_dialect_objc ())
1424 c_parser_objc_method_definition (parser);
1425 break;
1427 /* Else fall through, and yield a syntax error trying to parse
1428 as a declaration or function definition. */
1429 default:
1430 decl_or_fndef:
1431 /* A declaration or a function definition (or, in Objective-C,
1432 an @interface or @protocol with prefix attributes). We can
1433 only tell which after parsing the declaration specifiers, if
1434 any, and the first declarator. */
1435 c_parser_declaration_or_fndef (parser, true, true, true, false, true,
1436 NULL, vNULL);
1437 break;
1441 static void c_finish_omp_declare_simd (c_parser *, tree, tree, vec<c_token>);
1443 /* Parse a declaration or function definition (C90 6.5, 6.7.1, C99
1444 6.7, 6.9.1). If FNDEF_OK is true, a function definition is
1445 accepted; otherwise (old-style parameter declarations) only other
1446 declarations are accepted. If STATIC_ASSERT_OK is true, a static
1447 assertion is accepted; otherwise (old-style parameter declarations)
1448 it is not. If NESTED is true, we are inside a function or parsing
1449 old-style parameter declarations; any functions encountered are
1450 nested functions and declaration specifiers are required; otherwise
1451 we are at top level and functions are normal functions and
1452 declaration specifiers may be optional. If EMPTY_OK is true, empty
1453 declarations are OK (subject to all other constraints); otherwise
1454 (old-style parameter declarations) they are diagnosed. If
1455 START_ATTR_OK is true, the declaration specifiers may start with
1456 attributes; otherwise they may not.
1457 OBJC_FOREACH_OBJECT_DECLARATION can be used to get back the parsed
1458 declaration when parsing an Objective-C foreach statement.
1460 declaration:
1461 declaration-specifiers init-declarator-list[opt] ;
1462 static_assert-declaration
1464 function-definition:
1465 declaration-specifiers[opt] declarator declaration-list[opt]
1466 compound-statement
1468 declaration-list:
1469 declaration
1470 declaration-list declaration
1472 init-declarator-list:
1473 init-declarator
1474 init-declarator-list , init-declarator
1476 init-declarator:
1477 declarator simple-asm-expr[opt] attributes[opt]
1478 declarator simple-asm-expr[opt] attributes[opt] = initializer
1480 GNU extensions:
1482 nested-function-definition:
1483 declaration-specifiers declarator declaration-list[opt]
1484 compound-statement
1486 Objective-C:
1487 attributes objc-class-definition
1488 attributes objc-category-definition
1489 attributes objc-protocol-definition
1491 The simple-asm-expr and attributes are GNU extensions.
1493 This function does not handle __extension__; that is handled in its
1494 callers. ??? Following the old parser, __extension__ may start
1495 external declarations, declarations in functions and declarations
1496 at the start of "for" loops, but not old-style parameter
1497 declarations.
1499 C99 requires declaration specifiers in a function definition; the
1500 absence is diagnosed through the diagnosis of implicit int. In GNU
1501 C we also allow but diagnose declarations without declaration
1502 specifiers, but only at top level (elsewhere they conflict with
1503 other syntax).
1505 In Objective-C, declarations of the looping variable in a foreach
1506 statement are exceptionally terminated by 'in' (for example, 'for
1507 (NSObject *object in array) { ... }').
1509 OpenMP:
1511 declaration:
1512 threadprivate-directive */
1514 static void
1515 c_parser_declaration_or_fndef (c_parser *parser, bool fndef_ok,
1516 bool static_assert_ok, bool empty_ok,
1517 bool nested, bool start_attr_ok,
1518 tree *objc_foreach_object_declaration,
1519 vec<c_token> omp_declare_simd_clauses)
1521 struct c_declspecs *specs;
1522 tree prefix_attrs;
1523 tree all_prefix_attrs;
1524 bool diagnosed_no_specs = false;
1525 location_t here = c_parser_peek_token (parser)->location;
1527 if (static_assert_ok
1528 && c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT))
1530 c_parser_static_assert_declaration (parser);
1531 return;
1533 specs = build_null_declspecs ();
1535 /* Try to detect an unknown type name when we have "A B" or "A *B". */
1536 if (c_parser_peek_token (parser)->type == CPP_NAME
1537 && c_parser_peek_token (parser)->id_kind == C_ID_ID
1538 && (c_parser_peek_2nd_token (parser)->type == CPP_NAME
1539 || c_parser_peek_2nd_token (parser)->type == CPP_MULT)
1540 && (!nested || !lookup_name (c_parser_peek_token (parser)->value)))
1542 error_at (here, "unknown type name %qE",
1543 c_parser_peek_token (parser)->value);
1545 /* Parse declspecs normally to get a correct pointer type, but avoid
1546 a further "fails to be a type name" error. Refuse nested functions
1547 since it is not how the user likely wants us to recover. */
1548 c_parser_peek_token (parser)->type = CPP_KEYWORD;
1549 c_parser_peek_token (parser)->keyword = RID_VOID;
1550 c_parser_peek_token (parser)->value = error_mark_node;
1551 fndef_ok = !nested;
1554 c_parser_declspecs (parser, specs, true, true, start_attr_ok,
1555 true, true, cla_nonabstract_decl);
1556 if (parser->error)
1558 c_parser_skip_to_end_of_block_or_statement (parser);
1559 return;
1561 if (nested && !specs->declspecs_seen_p)
1563 c_parser_error (parser, "expected declaration specifiers");
1564 c_parser_skip_to_end_of_block_or_statement (parser);
1565 return;
1567 finish_declspecs (specs);
1568 bool auto_type_p = specs->typespec_word == cts_auto_type;
1569 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1571 if (auto_type_p)
1572 error_at (here, "%<__auto_type%> in empty declaration");
1573 else if (empty_ok)
1574 shadow_tag (specs);
1575 else
1577 shadow_tag_warned (specs, 1);
1578 pedwarn (here, 0, "empty declaration");
1580 c_parser_consume_token (parser);
1581 return;
1584 /* Provide better error recovery. Note that a type name here is usually
1585 better diagnosed as a redeclaration. */
1586 if (empty_ok
1587 && specs->typespec_kind == ctsk_tagdef
1588 && c_parser_next_token_starts_declspecs (parser)
1589 && !c_parser_next_token_is (parser, CPP_NAME))
1591 c_parser_error (parser, "expected %<;%>, identifier or %<(%>");
1592 parser->error = false;
1593 shadow_tag_warned (specs, 1);
1594 return;
1596 else if (c_dialect_objc () && !auto_type_p)
1598 /* Prefix attributes are an error on method decls. */
1599 switch (c_parser_peek_token (parser)->type)
1601 case CPP_PLUS:
1602 case CPP_MINUS:
1603 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1604 return;
1605 if (specs->attrs)
1607 warning_at (c_parser_peek_token (parser)->location,
1608 OPT_Wattributes,
1609 "prefix attributes are ignored for methods");
1610 specs->attrs = NULL_TREE;
1612 if (fndef_ok)
1613 c_parser_objc_method_definition (parser);
1614 else
1615 c_parser_objc_methodproto (parser);
1616 return;
1617 break;
1618 default:
1619 break;
1621 /* This is where we parse 'attributes @interface ...',
1622 'attributes @implementation ...', 'attributes @protocol ...'
1623 (where attributes could be, for example, __attribute__
1624 ((deprecated)).
1626 switch (c_parser_peek_token (parser)->keyword)
1628 case RID_AT_INTERFACE:
1630 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1631 return;
1632 c_parser_objc_class_definition (parser, specs->attrs);
1633 return;
1635 break;
1636 case RID_AT_IMPLEMENTATION:
1638 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1639 return;
1640 if (specs->attrs)
1642 warning_at (c_parser_peek_token (parser)->location,
1643 OPT_Wattributes,
1644 "prefix attributes are ignored for implementations");
1645 specs->attrs = NULL_TREE;
1647 c_parser_objc_class_definition (parser, NULL_TREE);
1648 return;
1650 break;
1651 case RID_AT_PROTOCOL:
1653 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1654 return;
1655 c_parser_objc_protocol_definition (parser, specs->attrs);
1656 return;
1658 break;
1659 case RID_AT_ALIAS:
1660 case RID_AT_CLASS:
1661 case RID_AT_END:
1662 case RID_AT_PROPERTY:
1663 if (specs->attrs)
1665 c_parser_error (parser, "unexpected attribute");
1666 specs->attrs = NULL;
1668 break;
1669 default:
1670 break;
1674 pending_xref_error ();
1675 prefix_attrs = specs->attrs;
1676 all_prefix_attrs = prefix_attrs;
1677 specs->attrs = NULL_TREE;
1678 while (true)
1680 struct c_declarator *declarator;
1681 bool dummy = false;
1682 timevar_id_t tv;
1683 tree fnbody;
1684 /* Declaring either one or more declarators (in which case we
1685 should diagnose if there were no declaration specifiers) or a
1686 function definition (in which case the diagnostic for
1687 implicit int suffices). */
1688 declarator = c_parser_declarator (parser,
1689 specs->typespec_kind != ctsk_none,
1690 C_DTR_NORMAL, &dummy);
1691 if (declarator == NULL)
1693 if (omp_declare_simd_clauses.exists ()
1694 || !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
1695 c_finish_omp_declare_simd (parser, NULL_TREE, NULL_TREE,
1696 omp_declare_simd_clauses);
1697 c_parser_skip_to_end_of_block_or_statement (parser);
1698 return;
1700 if (auto_type_p && declarator->kind != cdk_id)
1702 error_at (here,
1703 "%<__auto_type%> requires a plain identifier"
1704 " as declarator");
1705 c_parser_skip_to_end_of_block_or_statement (parser);
1706 return;
1708 if (c_parser_next_token_is (parser, CPP_EQ)
1709 || c_parser_next_token_is (parser, CPP_COMMA)
1710 || c_parser_next_token_is (parser, CPP_SEMICOLON)
1711 || c_parser_next_token_is_keyword (parser, RID_ASM)
1712 || c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE)
1713 || c_parser_next_token_is_keyword (parser, RID_IN))
1715 tree asm_name = NULL_TREE;
1716 tree postfix_attrs = NULL_TREE;
1717 if (!diagnosed_no_specs && !specs->declspecs_seen_p)
1719 diagnosed_no_specs = true;
1720 pedwarn (here, 0, "data definition has no type or storage class");
1722 /* Having seen a data definition, there cannot now be a
1723 function definition. */
1724 fndef_ok = false;
1725 if (c_parser_next_token_is_keyword (parser, RID_ASM))
1726 asm_name = c_parser_simple_asm_expr (parser);
1727 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
1729 postfix_attrs = c_parser_attributes (parser);
1730 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
1732 /* This means there is an attribute specifier after
1733 the declarator in a function definition. Provide
1734 some more information for the user. */
1735 error_at (here, "attributes should be specified before the "
1736 "declarator in a function definition");
1737 c_parser_skip_to_end_of_block_or_statement (parser);
1738 return;
1741 if (c_parser_next_token_is (parser, CPP_EQ))
1743 tree d;
1744 struct c_expr init;
1745 location_t init_loc;
1746 c_parser_consume_token (parser);
1747 if (auto_type_p)
1749 start_init (NULL_TREE, asm_name, global_bindings_p ());
1750 init_loc = c_parser_peek_token (parser)->location;
1751 init = c_parser_expr_no_commas (parser, NULL);
1752 if (TREE_CODE (init.value) == COMPONENT_REF
1753 && DECL_C_BIT_FIELD (TREE_OPERAND (init.value, 1)))
1754 error_at (here,
1755 "%<__auto_type%> used with a bit-field"
1756 " initializer");
1757 init = convert_lvalue_to_rvalue (init_loc, init, true, true);
1758 tree init_type = TREE_TYPE (init.value);
1759 /* As with typeof, remove all qualifiers from atomic types. */
1760 if (init_type != error_mark_node && TYPE_ATOMIC (init_type))
1761 init_type
1762 = c_build_qualified_type (init_type, TYPE_UNQUALIFIED);
1763 bool vm_type = variably_modified_type_p (init_type,
1764 NULL_TREE);
1765 if (vm_type)
1766 init.value = c_save_expr (init.value);
1767 finish_init ();
1768 specs->typespec_kind = ctsk_typeof;
1769 specs->locations[cdw_typedef] = init_loc;
1770 specs->typedef_p = true;
1771 specs->type = init_type;
1772 if (vm_type)
1774 bool maybe_const = true;
1775 tree type_expr = c_fully_fold (init.value, false,
1776 &maybe_const);
1777 specs->expr_const_operands &= maybe_const;
1778 if (specs->expr)
1779 specs->expr = build2 (COMPOUND_EXPR,
1780 TREE_TYPE (type_expr),
1781 specs->expr, type_expr);
1782 else
1783 specs->expr = type_expr;
1785 d = start_decl (declarator, specs, true,
1786 chainon (postfix_attrs, all_prefix_attrs));
1787 if (!d)
1788 d = error_mark_node;
1789 if (omp_declare_simd_clauses.exists ()
1790 || !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
1791 c_finish_omp_declare_simd (parser, d, NULL_TREE,
1792 omp_declare_simd_clauses);
1794 else
1796 /* The declaration of the variable is in effect while
1797 its initializer is parsed. */
1798 d = start_decl (declarator, specs, true,
1799 chainon (postfix_attrs, all_prefix_attrs));
1800 if (!d)
1801 d = error_mark_node;
1802 if (omp_declare_simd_clauses.exists ()
1803 || !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
1804 c_finish_omp_declare_simd (parser, d, NULL_TREE,
1805 omp_declare_simd_clauses);
1806 start_init (d, asm_name, global_bindings_p ());
1807 init_loc = c_parser_peek_token (parser)->location;
1808 init = c_parser_initializer (parser);
1809 finish_init ();
1811 if (d != error_mark_node)
1813 maybe_warn_string_init (init_loc, TREE_TYPE (d), init);
1814 finish_decl (d, init_loc, init.value,
1815 init.original_type, asm_name);
1818 else
1820 if (auto_type_p)
1822 error_at (here,
1823 "%<__auto_type%> requires an initialized "
1824 "data declaration");
1825 c_parser_skip_to_end_of_block_or_statement (parser);
1826 return;
1828 tree d = start_decl (declarator, specs, false,
1829 chainon (postfix_attrs,
1830 all_prefix_attrs));
1831 if (omp_declare_simd_clauses.exists ()
1832 || !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
1834 tree parms = NULL_TREE;
1835 if (d && TREE_CODE (d) == FUNCTION_DECL)
1837 struct c_declarator *ce = declarator;
1838 while (ce != NULL)
1839 if (ce->kind == cdk_function)
1841 parms = ce->u.arg_info->parms;
1842 break;
1844 else
1845 ce = ce->declarator;
1847 if (parms)
1848 temp_store_parm_decls (d, parms);
1849 c_finish_omp_declare_simd (parser, d, parms,
1850 omp_declare_simd_clauses);
1851 if (parms)
1852 temp_pop_parm_decls ();
1854 if (d)
1855 finish_decl (d, UNKNOWN_LOCATION, NULL_TREE,
1856 NULL_TREE, asm_name);
1858 if (c_parser_next_token_is_keyword (parser, RID_IN))
1860 if (d)
1861 *objc_foreach_object_declaration = d;
1862 else
1863 *objc_foreach_object_declaration = error_mark_node;
1866 if (c_parser_next_token_is (parser, CPP_COMMA))
1868 if (auto_type_p)
1870 error_at (here,
1871 "%<__auto_type%> may only be used with"
1872 " a single declarator");
1873 c_parser_skip_to_end_of_block_or_statement (parser);
1874 return;
1876 c_parser_consume_token (parser);
1877 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
1878 all_prefix_attrs = chainon (c_parser_attributes (parser),
1879 prefix_attrs);
1880 else
1881 all_prefix_attrs = prefix_attrs;
1882 continue;
1884 else if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1886 c_parser_consume_token (parser);
1887 return;
1889 else if (c_parser_next_token_is_keyword (parser, RID_IN))
1891 /* This can only happen in Objective-C: we found the
1892 'in' that terminates the declaration inside an
1893 Objective-C foreach statement. Do not consume the
1894 token, so that the caller can use it to determine
1895 that this indeed is a foreach context. */
1896 return;
1898 else
1900 c_parser_error (parser, "expected %<,%> or %<;%>");
1901 c_parser_skip_to_end_of_block_or_statement (parser);
1902 return;
1905 else if (auto_type_p)
1907 error_at (here,
1908 "%<__auto_type%> requires an initialized data declaration");
1909 c_parser_skip_to_end_of_block_or_statement (parser);
1910 return;
1912 else if (!fndef_ok)
1914 c_parser_error (parser, "expected %<=%>, %<,%>, %<;%>, "
1915 "%<asm%> or %<__attribute__%>");
1916 c_parser_skip_to_end_of_block_or_statement (parser);
1917 return;
1919 /* Function definition (nested or otherwise). */
1920 if (nested)
1922 pedwarn (here, OPT_Wpedantic, "ISO C forbids nested functions");
1923 c_push_function_context ();
1925 if (!start_function (specs, declarator, all_prefix_attrs))
1927 /* This can appear in many cases looking nothing like a
1928 function definition, so we don't give a more specific
1929 error suggesting there was one. */
1930 c_parser_error (parser, "expected %<=%>, %<,%>, %<;%>, %<asm%> "
1931 "or %<__attribute__%>");
1932 if (nested)
1933 c_pop_function_context ();
1934 break;
1937 if (DECL_DECLARED_INLINE_P (current_function_decl))
1938 tv = TV_PARSE_INLINE;
1939 else
1940 tv = TV_PARSE_FUNC;
1941 timevar_push (tv);
1943 /* Parse old-style parameter declarations. ??? Attributes are
1944 not allowed to start declaration specifiers here because of a
1945 syntax conflict between a function declaration with attribute
1946 suffix and a function definition with an attribute prefix on
1947 first old-style parameter declaration. Following the old
1948 parser, they are not accepted on subsequent old-style
1949 parameter declarations either. However, there is no
1950 ambiguity after the first declaration, nor indeed on the
1951 first as long as we don't allow postfix attributes after a
1952 declarator with a nonempty identifier list in a definition;
1953 and postfix attributes have never been accepted here in
1954 function definitions either. */
1955 while (c_parser_next_token_is_not (parser, CPP_EOF)
1956 && c_parser_next_token_is_not (parser, CPP_OPEN_BRACE))
1957 c_parser_declaration_or_fndef (parser, false, false, false,
1958 true, false, NULL, vNULL);
1959 store_parm_decls ();
1960 if (omp_declare_simd_clauses.exists ()
1961 || !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
1962 c_finish_omp_declare_simd (parser, current_function_decl, NULL_TREE,
1963 omp_declare_simd_clauses);
1964 DECL_STRUCT_FUNCTION (current_function_decl)->function_start_locus
1965 = c_parser_peek_token (parser)->location;
1966 fnbody = c_parser_compound_statement (parser);
1967 if (flag_cilkplus && contains_array_notation_expr (fnbody))
1968 fnbody = expand_array_notation_exprs (fnbody);
1969 if (nested)
1971 tree decl = current_function_decl;
1972 /* Mark nested functions as needing static-chain initially.
1973 lower_nested_functions will recompute it but the
1974 DECL_STATIC_CHAIN flag is also used before that happens,
1975 by initializer_constant_valid_p. See gcc.dg/nested-fn-2.c. */
1976 DECL_STATIC_CHAIN (decl) = 1;
1977 add_stmt (fnbody);
1978 finish_function ();
1979 c_pop_function_context ();
1980 add_stmt (build_stmt (DECL_SOURCE_LOCATION (decl), DECL_EXPR, decl));
1982 else
1984 add_stmt (fnbody);
1985 finish_function ();
1988 timevar_pop (tv);
1989 break;
1993 /* Parse an asm-definition (asm() outside a function body). This is a
1994 GNU extension.
1996 asm-definition:
1997 simple-asm-expr ;
2000 static void
2001 c_parser_asm_definition (c_parser *parser)
2003 tree asm_str = c_parser_simple_asm_expr (parser);
2004 if (asm_str)
2005 symtab->finalize_toplevel_asm (asm_str);
2006 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
2009 /* Parse a static assertion (C11 6.7.10).
2011 static_assert-declaration:
2012 static_assert-declaration-no-semi ;
2015 static void
2016 c_parser_static_assert_declaration (c_parser *parser)
2018 c_parser_static_assert_declaration_no_semi (parser);
2019 if (parser->error
2020 || !c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
2021 c_parser_skip_to_end_of_block_or_statement (parser);
2024 /* Parse a static assertion (C11 6.7.10), without the trailing
2025 semicolon.
2027 static_assert-declaration-no-semi:
2028 _Static_assert ( constant-expression , string-literal )
2031 static void
2032 c_parser_static_assert_declaration_no_semi (c_parser *parser)
2034 location_t assert_loc, value_loc;
2035 tree value;
2036 tree string;
2038 gcc_assert (c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT));
2039 assert_loc = c_parser_peek_token (parser)->location;
2040 if (flag_isoc99)
2041 pedwarn_c99 (assert_loc, OPT_Wpedantic,
2042 "ISO C99 does not support %<_Static_assert%>");
2043 else
2044 pedwarn_c99 (assert_loc, OPT_Wpedantic,
2045 "ISO C90 does not support %<_Static_assert%>");
2046 c_parser_consume_token (parser);
2047 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2048 return;
2049 value_loc = c_parser_peek_token (parser)->location;
2050 value = c_parser_expr_no_commas (parser, NULL).value;
2051 parser->lex_untranslated_string = true;
2052 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
2054 parser->lex_untranslated_string = false;
2055 return;
2057 switch (c_parser_peek_token (parser)->type)
2059 case CPP_STRING:
2060 case CPP_STRING16:
2061 case CPP_STRING32:
2062 case CPP_WSTRING:
2063 case CPP_UTF8STRING:
2064 string = c_parser_peek_token (parser)->value;
2065 c_parser_consume_token (parser);
2066 parser->lex_untranslated_string = false;
2067 break;
2068 default:
2069 c_parser_error (parser, "expected string literal");
2070 parser->lex_untranslated_string = false;
2071 return;
2073 c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
2075 if (!INTEGRAL_TYPE_P (TREE_TYPE (value)))
2077 error_at (value_loc, "expression in static assertion is not an integer");
2078 return;
2080 if (TREE_CODE (value) != INTEGER_CST)
2082 value = c_fully_fold (value, false, NULL);
2083 /* Strip no-op conversions. */
2084 STRIP_TYPE_NOPS (value);
2085 if (TREE_CODE (value) == INTEGER_CST)
2086 pedwarn (value_loc, OPT_Wpedantic, "expression in static assertion "
2087 "is not an integer constant expression");
2089 if (TREE_CODE (value) != INTEGER_CST)
2091 error_at (value_loc, "expression in static assertion is not constant");
2092 return;
2094 constant_expression_warning (value);
2095 if (integer_zerop (value))
2096 error_at (assert_loc, "static assertion failed: %E", string);
2099 /* Parse some declaration specifiers (possibly none) (C90 6.5, C99
2100 6.7), adding them to SPECS (which may already include some).
2101 Storage class specifiers are accepted iff SCSPEC_OK; type
2102 specifiers are accepted iff TYPESPEC_OK; alignment specifiers are
2103 accepted iff ALIGNSPEC_OK; attributes are accepted at the start
2104 iff START_ATTR_OK; __auto_type is accepted iff AUTO_TYPE_OK.
2106 declaration-specifiers:
2107 storage-class-specifier declaration-specifiers[opt]
2108 type-specifier declaration-specifiers[opt]
2109 type-qualifier declaration-specifiers[opt]
2110 function-specifier declaration-specifiers[opt]
2111 alignment-specifier declaration-specifiers[opt]
2113 Function specifiers (inline) are from C99, and are currently
2114 handled as storage class specifiers, as is __thread. Alignment
2115 specifiers are from C11.
2117 C90 6.5.1, C99 6.7.1:
2118 storage-class-specifier:
2119 typedef
2120 extern
2121 static
2122 auto
2123 register
2124 _Thread_local
2126 (_Thread_local is new in C11.)
2128 C99 6.7.4:
2129 function-specifier:
2130 inline
2131 _Noreturn
2133 (_Noreturn is new in C11.)
2135 C90 6.5.2, C99 6.7.2:
2136 type-specifier:
2137 void
2138 char
2139 short
2141 long
2142 float
2143 double
2144 signed
2145 unsigned
2146 _Bool
2147 _Complex
2148 [_Imaginary removed in C99 TC2]
2149 struct-or-union-specifier
2150 enum-specifier
2151 typedef-name
2152 atomic-type-specifier
2154 (_Bool and _Complex are new in C99.)
2155 (atomic-type-specifier is new in C11.)
2157 C90 6.5.3, C99 6.7.3:
2159 type-qualifier:
2160 const
2161 restrict
2162 volatile
2163 address-space-qualifier
2164 _Atomic
2166 (restrict is new in C99.)
2167 (_Atomic is new in C11.)
2169 GNU extensions:
2171 declaration-specifiers:
2172 attributes declaration-specifiers[opt]
2174 type-qualifier:
2175 address-space
2177 address-space:
2178 identifier recognized by the target
2180 storage-class-specifier:
2181 __thread
2183 type-specifier:
2184 typeof-specifier
2185 __auto_type
2186 __intN
2187 _Decimal32
2188 _Decimal64
2189 _Decimal128
2190 _Fract
2191 _Accum
2192 _Sat
2194 (_Fract, _Accum, and _Sat are new from ISO/IEC DTR 18037:
2195 http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1169.pdf)
2197 atomic-type-specifier
2198 _Atomic ( type-name )
2200 Objective-C:
2202 type-specifier:
2203 class-name objc-protocol-refs[opt]
2204 typedef-name objc-protocol-refs
2205 objc-protocol-refs
2208 static void
2209 c_parser_declspecs (c_parser *parser, struct c_declspecs *specs,
2210 bool scspec_ok, bool typespec_ok, bool start_attr_ok,
2211 bool alignspec_ok, bool auto_type_ok,
2212 enum c_lookahead_kind la)
2214 bool attrs_ok = start_attr_ok;
2215 bool seen_type = specs->typespec_kind != ctsk_none;
2217 if (!typespec_ok)
2218 gcc_assert (la == cla_prefer_id);
2220 while (c_parser_next_token_is (parser, CPP_NAME)
2221 || c_parser_next_token_is (parser, CPP_KEYWORD)
2222 || (c_dialect_objc () && c_parser_next_token_is (parser, CPP_LESS)))
2224 struct c_typespec t;
2225 tree attrs;
2226 tree align;
2227 location_t loc = c_parser_peek_token (parser)->location;
2229 /* If we cannot accept a type, exit if the next token must start
2230 one. Also, if we already have seen a tagged definition,
2231 a typename would be an error anyway and likely the user
2232 has simply forgotten a semicolon, so we exit. */
2233 if ((!typespec_ok || specs->typespec_kind == ctsk_tagdef)
2234 && c_parser_next_tokens_start_typename (parser, la)
2235 && !c_parser_next_token_is_qualifier (parser))
2236 break;
2238 if (c_parser_next_token_is (parser, CPP_NAME))
2240 c_token *name_token = c_parser_peek_token (parser);
2241 tree value = name_token->value;
2242 c_id_kind kind = name_token->id_kind;
2244 if (kind == C_ID_ADDRSPACE)
2246 addr_space_t as
2247 = name_token->keyword - RID_FIRST_ADDR_SPACE;
2248 declspecs_add_addrspace (name_token->location, specs, as);
2249 c_parser_consume_token (parser);
2250 attrs_ok = true;
2251 continue;
2254 gcc_assert (!c_parser_next_token_is_qualifier (parser));
2256 /* If we cannot accept a type, and the next token must start one,
2257 exit. Do the same if we already have seen a tagged definition,
2258 since it would be an error anyway and likely the user has simply
2259 forgotten a semicolon. */
2260 if (seen_type || !c_parser_next_tokens_start_typename (parser, la))
2261 break;
2263 /* Now at an unknown typename (C_ID_ID), a C_ID_TYPENAME or
2264 a C_ID_CLASSNAME. */
2265 c_parser_consume_token (parser);
2266 seen_type = true;
2267 attrs_ok = true;
2268 if (kind == C_ID_ID)
2270 error_at (loc, "unknown type name %qE", value);
2271 t.kind = ctsk_typedef;
2272 t.spec = error_mark_node;
2274 else if (kind == C_ID_TYPENAME
2275 && (!c_dialect_objc ()
2276 || c_parser_next_token_is_not (parser, CPP_LESS)))
2278 t.kind = ctsk_typedef;
2279 /* For a typedef name, record the meaning, not the name.
2280 In case of 'foo foo, bar;'. */
2281 t.spec = lookup_name (value);
2283 else
2285 tree proto = NULL_TREE;
2286 gcc_assert (c_dialect_objc ());
2287 t.kind = ctsk_objc;
2288 if (c_parser_next_token_is (parser, CPP_LESS))
2289 proto = c_parser_objc_protocol_refs (parser);
2290 t.spec = objc_get_protocol_qualified_type (value, proto);
2292 t.expr = NULL_TREE;
2293 t.expr_const_operands = true;
2294 declspecs_add_type (name_token->location, specs, t);
2295 continue;
2297 if (c_parser_next_token_is (parser, CPP_LESS))
2299 /* Make "<SomeProtocol>" equivalent to "id <SomeProtocol>" -
2300 nisse@lysator.liu.se. */
2301 tree proto;
2302 gcc_assert (c_dialect_objc ());
2303 if (!typespec_ok || seen_type)
2304 break;
2305 proto = c_parser_objc_protocol_refs (parser);
2306 t.kind = ctsk_objc;
2307 t.spec = objc_get_protocol_qualified_type (NULL_TREE, proto);
2308 t.expr = NULL_TREE;
2309 t.expr_const_operands = true;
2310 declspecs_add_type (loc, specs, t);
2311 continue;
2313 gcc_assert (c_parser_next_token_is (parser, CPP_KEYWORD));
2314 switch (c_parser_peek_token (parser)->keyword)
2316 case RID_STATIC:
2317 case RID_EXTERN:
2318 case RID_REGISTER:
2319 case RID_TYPEDEF:
2320 case RID_INLINE:
2321 case RID_NORETURN:
2322 case RID_AUTO:
2323 case RID_THREAD:
2324 if (!scspec_ok)
2325 goto out;
2326 attrs_ok = true;
2327 /* TODO: Distinguish between function specifiers (inline, noreturn)
2328 and storage class specifiers, either here or in
2329 declspecs_add_scspec. */
2330 declspecs_add_scspec (loc, specs,
2331 c_parser_peek_token (parser)->value);
2332 c_parser_consume_token (parser);
2333 break;
2334 case RID_AUTO_TYPE:
2335 if (!auto_type_ok)
2336 goto out;
2337 /* Fall through. */
2338 case RID_UNSIGNED:
2339 case RID_LONG:
2340 case RID_SHORT:
2341 case RID_SIGNED:
2342 case RID_COMPLEX:
2343 case RID_INT:
2344 case RID_CHAR:
2345 case RID_FLOAT:
2346 case RID_DOUBLE:
2347 case RID_VOID:
2348 case RID_DFLOAT32:
2349 case RID_DFLOAT64:
2350 case RID_DFLOAT128:
2351 case RID_BOOL:
2352 case RID_FRACT:
2353 case RID_ACCUM:
2354 case RID_SAT:
2355 case RID_INT_N_0:
2356 case RID_INT_N_1:
2357 case RID_INT_N_2:
2358 case RID_INT_N_3:
2359 if (!typespec_ok)
2360 goto out;
2361 attrs_ok = true;
2362 seen_type = true;
2363 if (c_dialect_objc ())
2364 parser->objc_need_raw_identifier = true;
2365 t.kind = ctsk_resword;
2366 t.spec = c_parser_peek_token (parser)->value;
2367 t.expr = NULL_TREE;
2368 t.expr_const_operands = true;
2369 declspecs_add_type (loc, specs, t);
2370 c_parser_consume_token (parser);
2371 break;
2372 case RID_ENUM:
2373 if (!typespec_ok)
2374 goto out;
2375 attrs_ok = true;
2376 seen_type = true;
2377 t = c_parser_enum_specifier (parser);
2378 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, t.spec);
2379 declspecs_add_type (loc, specs, t);
2380 break;
2381 case RID_STRUCT:
2382 case RID_UNION:
2383 if (!typespec_ok)
2384 goto out;
2385 attrs_ok = true;
2386 seen_type = true;
2387 t = c_parser_struct_or_union_specifier (parser);
2388 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, t.spec);
2389 declspecs_add_type (loc, specs, t);
2390 break;
2391 case RID_TYPEOF:
2392 /* ??? The old parser rejected typeof after other type
2393 specifiers, but is a syntax error the best way of
2394 handling this? */
2395 if (!typespec_ok || seen_type)
2396 goto out;
2397 attrs_ok = true;
2398 seen_type = true;
2399 t = c_parser_typeof_specifier (parser);
2400 declspecs_add_type (loc, specs, t);
2401 break;
2402 case RID_ATOMIC:
2403 /* C parser handling of Objective-C constructs needs
2404 checking for correct lvalue-to-rvalue conversions, and
2405 the code in build_modify_expr handling various
2406 Objective-C cases, and that in build_unary_op handling
2407 Objective-C cases for increment / decrement, also needs
2408 updating; uses of TYPE_MAIN_VARIANT in objc_compare_types
2409 and objc_types_are_equivalent may also need updates. */
2410 if (c_dialect_objc ())
2411 sorry ("%<_Atomic%> in Objective-C");
2412 /* C parser handling of OpenMP constructs needs checking for
2413 correct lvalue-to-rvalue conversions. */
2414 if (flag_openmp)
2415 sorry ("%<_Atomic%> with OpenMP");
2416 if (flag_isoc99)
2417 pedwarn_c99 (loc, OPT_Wpedantic,
2418 "ISO C99 does not support the %<_Atomic%> qualifier");
2419 else
2420 pedwarn_c99 (loc, OPT_Wpedantic,
2421 "ISO C90 does not support the %<_Atomic%> qualifier");
2422 attrs_ok = true;
2423 tree value;
2424 value = c_parser_peek_token (parser)->value;
2425 c_parser_consume_token (parser);
2426 if (typespec_ok && c_parser_next_token_is (parser, CPP_OPEN_PAREN))
2428 /* _Atomic ( type-name ). */
2429 seen_type = true;
2430 c_parser_consume_token (parser);
2431 struct c_type_name *type = c_parser_type_name (parser);
2432 t.kind = ctsk_typeof;
2433 t.spec = error_mark_node;
2434 t.expr = NULL_TREE;
2435 t.expr_const_operands = true;
2436 if (type != NULL)
2437 t.spec = groktypename (type, &t.expr,
2438 &t.expr_const_operands);
2439 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2440 "expected %<)%>");
2441 if (t.spec != error_mark_node)
2443 if (TREE_CODE (t.spec) == ARRAY_TYPE)
2444 error_at (loc, "%<_Atomic%>-qualified array type");
2445 else if (TREE_CODE (t.spec) == FUNCTION_TYPE)
2446 error_at (loc, "%<_Atomic%>-qualified function type");
2447 else if (TYPE_QUALS (t.spec) != TYPE_UNQUALIFIED)
2448 error_at (loc, "%<_Atomic%> applied to a qualified type");
2449 else
2450 t.spec = c_build_qualified_type (t.spec, TYPE_QUAL_ATOMIC);
2452 declspecs_add_type (loc, specs, t);
2454 else
2455 declspecs_add_qual (loc, specs, value);
2456 break;
2457 case RID_CONST:
2458 case RID_VOLATILE:
2459 case RID_RESTRICT:
2460 attrs_ok = true;
2461 declspecs_add_qual (loc, specs, c_parser_peek_token (parser)->value);
2462 c_parser_consume_token (parser);
2463 break;
2464 case RID_ATTRIBUTE:
2465 if (!attrs_ok)
2466 goto out;
2467 attrs = c_parser_attributes (parser);
2468 declspecs_add_attrs (loc, specs, attrs);
2469 break;
2470 case RID_ALIGNAS:
2471 if (!alignspec_ok)
2472 goto out;
2473 align = c_parser_alignas_specifier (parser);
2474 declspecs_add_alignas (loc, specs, align);
2475 break;
2476 default:
2477 goto out;
2480 out: ;
2483 /* Parse an enum specifier (C90 6.5.2.2, C99 6.7.2.2).
2485 enum-specifier:
2486 enum attributes[opt] identifier[opt] { enumerator-list } attributes[opt]
2487 enum attributes[opt] identifier[opt] { enumerator-list , } attributes[opt]
2488 enum attributes[opt] identifier
2490 The form with trailing comma is new in C99. The forms with
2491 attributes are GNU extensions. In GNU C, we accept any expression
2492 without commas in the syntax (assignment expressions, not just
2493 conditional expressions); assignment expressions will be diagnosed
2494 as non-constant.
2496 enumerator-list:
2497 enumerator
2498 enumerator-list , enumerator
2500 enumerator:
2501 enumeration-constant
2502 enumeration-constant = constant-expression
2504 GNU Extensions:
2506 enumerator:
2507 enumeration-constant attributes[opt]
2508 enumeration-constant attributes[opt] = constant-expression
2512 static struct c_typespec
2513 c_parser_enum_specifier (c_parser *parser)
2515 struct c_typespec ret;
2516 tree attrs;
2517 tree ident = NULL_TREE;
2518 location_t enum_loc;
2519 location_t ident_loc = UNKNOWN_LOCATION; /* Quiet warning. */
2520 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ENUM));
2521 enum_loc = c_parser_peek_token (parser)->location;
2522 c_parser_consume_token (parser);
2523 attrs = c_parser_attributes (parser);
2524 enum_loc = c_parser_peek_token (parser)->location;
2525 /* Set the location in case we create a decl now. */
2526 c_parser_set_source_position_from_token (c_parser_peek_token (parser));
2527 if (c_parser_next_token_is (parser, CPP_NAME))
2529 ident = c_parser_peek_token (parser)->value;
2530 ident_loc = c_parser_peek_token (parser)->location;
2531 enum_loc = ident_loc;
2532 c_parser_consume_token (parser);
2534 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
2536 /* Parse an enum definition. */
2537 struct c_enum_contents the_enum;
2538 tree type;
2539 tree postfix_attrs;
2540 /* We chain the enumerators in reverse order, then put them in
2541 forward order at the end. */
2542 tree values;
2543 timevar_push (TV_PARSE_ENUM);
2544 type = start_enum (enum_loc, &the_enum, ident);
2545 values = NULL_TREE;
2546 c_parser_consume_token (parser);
2547 while (true)
2549 tree enum_id;
2550 tree enum_value;
2551 tree enum_decl;
2552 bool seen_comma;
2553 c_token *token;
2554 location_t comma_loc = UNKNOWN_LOCATION; /* Quiet warning. */
2555 location_t decl_loc, value_loc;
2556 if (c_parser_next_token_is_not (parser, CPP_NAME))
2558 c_parser_error (parser, "expected identifier");
2559 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
2560 values = error_mark_node;
2561 break;
2563 token = c_parser_peek_token (parser);
2564 enum_id = token->value;
2565 /* Set the location in case we create a decl now. */
2566 c_parser_set_source_position_from_token (token);
2567 decl_loc = value_loc = token->location;
2568 c_parser_consume_token (parser);
2569 /* Parse any specified attributes. */
2570 tree enum_attrs = c_parser_attributes (parser);
2571 if (c_parser_next_token_is (parser, CPP_EQ))
2573 c_parser_consume_token (parser);
2574 value_loc = c_parser_peek_token (parser)->location;
2575 enum_value = c_parser_expr_no_commas (parser, NULL).value;
2577 else
2578 enum_value = NULL_TREE;
2579 enum_decl = build_enumerator (decl_loc, value_loc,
2580 &the_enum, enum_id, enum_value);
2581 if (enum_attrs)
2582 decl_attributes (&TREE_PURPOSE (enum_decl), enum_attrs, 0);
2583 TREE_CHAIN (enum_decl) = values;
2584 values = enum_decl;
2585 seen_comma = false;
2586 if (c_parser_next_token_is (parser, CPP_COMMA))
2588 comma_loc = c_parser_peek_token (parser)->location;
2589 seen_comma = true;
2590 c_parser_consume_token (parser);
2592 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2594 if (seen_comma)
2595 pedwarn_c90 (comma_loc, OPT_Wpedantic,
2596 "comma at end of enumerator list");
2597 c_parser_consume_token (parser);
2598 break;
2600 if (!seen_comma)
2602 c_parser_error (parser, "expected %<,%> or %<}%>");
2603 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
2604 values = error_mark_node;
2605 break;
2608 postfix_attrs = c_parser_attributes (parser);
2609 ret.spec = finish_enum (type, nreverse (values),
2610 chainon (attrs, postfix_attrs));
2611 ret.kind = ctsk_tagdef;
2612 ret.expr = NULL_TREE;
2613 ret.expr_const_operands = true;
2614 timevar_pop (TV_PARSE_ENUM);
2615 return ret;
2617 else if (!ident)
2619 c_parser_error (parser, "expected %<{%>");
2620 ret.spec = error_mark_node;
2621 ret.kind = ctsk_tagref;
2622 ret.expr = NULL_TREE;
2623 ret.expr_const_operands = true;
2624 return ret;
2626 ret = parser_xref_tag (ident_loc, ENUMERAL_TYPE, ident);
2627 /* In ISO C, enumerated types can be referred to only if already
2628 defined. */
2629 if (pedantic && !COMPLETE_TYPE_P (ret.spec))
2631 gcc_assert (ident);
2632 pedwarn (enum_loc, OPT_Wpedantic,
2633 "ISO C forbids forward references to %<enum%> types");
2635 return ret;
2638 /* Parse a struct or union specifier (C90 6.5.2.1, C99 6.7.2.1).
2640 struct-or-union-specifier:
2641 struct-or-union attributes[opt] identifier[opt]
2642 { struct-contents } attributes[opt]
2643 struct-or-union attributes[opt] identifier
2645 struct-contents:
2646 struct-declaration-list
2648 struct-declaration-list:
2649 struct-declaration ;
2650 struct-declaration-list struct-declaration ;
2652 GNU extensions:
2654 struct-contents:
2655 empty
2656 struct-declaration
2657 struct-declaration-list struct-declaration
2659 struct-declaration-list:
2660 struct-declaration-list ;
2663 (Note that in the syntax here, unlike that in ISO C, the semicolons
2664 are included here rather than in struct-declaration, in order to
2665 describe the syntax with extra semicolons and missing semicolon at
2666 end.)
2668 Objective-C:
2670 struct-declaration-list:
2671 @defs ( class-name )
2673 (Note this does not include a trailing semicolon, but can be
2674 followed by further declarations, and gets a pedwarn-if-pedantic
2675 when followed by a semicolon.) */
2677 static struct c_typespec
2678 c_parser_struct_or_union_specifier (c_parser *parser)
2680 struct c_typespec ret;
2681 tree attrs;
2682 tree ident = NULL_TREE;
2683 location_t struct_loc;
2684 location_t ident_loc = UNKNOWN_LOCATION;
2685 enum tree_code code;
2686 switch (c_parser_peek_token (parser)->keyword)
2688 case RID_STRUCT:
2689 code = RECORD_TYPE;
2690 break;
2691 case RID_UNION:
2692 code = UNION_TYPE;
2693 break;
2694 default:
2695 gcc_unreachable ();
2697 struct_loc = c_parser_peek_token (parser)->location;
2698 c_parser_consume_token (parser);
2699 attrs = c_parser_attributes (parser);
2701 /* Set the location in case we create a decl now. */
2702 c_parser_set_source_position_from_token (c_parser_peek_token (parser));
2704 if (c_parser_next_token_is (parser, CPP_NAME))
2706 ident = c_parser_peek_token (parser)->value;
2707 ident_loc = c_parser_peek_token (parser)->location;
2708 struct_loc = ident_loc;
2709 c_parser_consume_token (parser);
2711 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
2713 /* Parse a struct or union definition. Start the scope of the
2714 tag before parsing components. */
2715 struct c_struct_parse_info *struct_info;
2716 tree type = start_struct (struct_loc, code, ident, &struct_info);
2717 tree postfix_attrs;
2718 /* We chain the components in reverse order, then put them in
2719 forward order at the end. Each struct-declaration may
2720 declare multiple components (comma-separated), so we must use
2721 chainon to join them, although when parsing each
2722 struct-declaration we can use TREE_CHAIN directly.
2724 The theory behind all this is that there will be more
2725 semicolon separated fields than comma separated fields, and
2726 so we'll be minimizing the number of node traversals required
2727 by chainon. */
2728 tree contents;
2729 timevar_push (TV_PARSE_STRUCT);
2730 contents = NULL_TREE;
2731 c_parser_consume_token (parser);
2732 /* Handle the Objective-C @defs construct,
2733 e.g. foo(sizeof(struct{ @defs(ClassName) }));. */
2734 if (c_parser_next_token_is_keyword (parser, RID_AT_DEFS))
2736 tree name;
2737 gcc_assert (c_dialect_objc ());
2738 c_parser_consume_token (parser);
2739 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2740 goto end_at_defs;
2741 if (c_parser_next_token_is (parser, CPP_NAME)
2742 && c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME)
2744 name = c_parser_peek_token (parser)->value;
2745 c_parser_consume_token (parser);
2747 else
2749 c_parser_error (parser, "expected class name");
2750 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
2751 goto end_at_defs;
2753 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2754 "expected %<)%>");
2755 contents = nreverse (objc_get_class_ivars (name));
2757 end_at_defs:
2758 /* Parse the struct-declarations and semicolons. Problems with
2759 semicolons are diagnosed here; empty structures are diagnosed
2760 elsewhere. */
2761 while (true)
2763 tree decls;
2764 /* Parse any stray semicolon. */
2765 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
2767 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
2768 "extra semicolon in struct or union specified");
2769 c_parser_consume_token (parser);
2770 continue;
2772 /* Stop if at the end of the struct or union contents. */
2773 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2775 c_parser_consume_token (parser);
2776 break;
2778 /* Accept #pragmas at struct scope. */
2779 if (c_parser_next_token_is (parser, CPP_PRAGMA))
2781 c_parser_pragma (parser, pragma_struct);
2782 continue;
2784 /* Parse some comma-separated declarations, but not the
2785 trailing semicolon if any. */
2786 decls = c_parser_struct_declaration (parser);
2787 contents = chainon (decls, contents);
2788 /* If no semicolon follows, either we have a parse error or
2789 are at the end of the struct or union and should
2790 pedwarn. */
2791 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
2792 c_parser_consume_token (parser);
2793 else
2795 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2796 pedwarn (c_parser_peek_token (parser)->location, 0,
2797 "no semicolon at end of struct or union");
2798 else if (parser->error
2799 || !c_parser_next_token_starts_declspecs (parser))
2801 c_parser_error (parser, "expected %<;%>");
2802 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
2803 break;
2806 /* If we come here, we have already emitted an error
2807 for an expected `;', identifier or `(', and we also
2808 recovered already. Go on with the next field. */
2811 postfix_attrs = c_parser_attributes (parser);
2812 ret.spec = finish_struct (struct_loc, type, nreverse (contents),
2813 chainon (attrs, postfix_attrs), struct_info);
2814 ret.kind = ctsk_tagdef;
2815 ret.expr = NULL_TREE;
2816 ret.expr_const_operands = true;
2817 timevar_pop (TV_PARSE_STRUCT);
2818 return ret;
2820 else if (!ident)
2822 c_parser_error (parser, "expected %<{%>");
2823 ret.spec = error_mark_node;
2824 ret.kind = ctsk_tagref;
2825 ret.expr = NULL_TREE;
2826 ret.expr_const_operands = true;
2827 return ret;
2829 ret = parser_xref_tag (ident_loc, code, ident);
2830 return ret;
2833 /* Parse a struct-declaration (C90 6.5.2.1, C99 6.7.2.1), *without*
2834 the trailing semicolon.
2836 struct-declaration:
2837 specifier-qualifier-list struct-declarator-list
2838 static_assert-declaration-no-semi
2840 specifier-qualifier-list:
2841 type-specifier specifier-qualifier-list[opt]
2842 type-qualifier specifier-qualifier-list[opt]
2843 attributes specifier-qualifier-list[opt]
2845 struct-declarator-list:
2846 struct-declarator
2847 struct-declarator-list , attributes[opt] struct-declarator
2849 struct-declarator:
2850 declarator attributes[opt]
2851 declarator[opt] : constant-expression attributes[opt]
2853 GNU extensions:
2855 struct-declaration:
2856 __extension__ struct-declaration
2857 specifier-qualifier-list
2859 Unlike the ISO C syntax, semicolons are handled elsewhere. The use
2860 of attributes where shown is a GNU extension. In GNU C, we accept
2861 any expression without commas in the syntax (assignment
2862 expressions, not just conditional expressions); assignment
2863 expressions will be diagnosed as non-constant. */
2865 static tree
2866 c_parser_struct_declaration (c_parser *parser)
2868 struct c_declspecs *specs;
2869 tree prefix_attrs;
2870 tree all_prefix_attrs;
2871 tree decls;
2872 location_t decl_loc;
2873 if (c_parser_next_token_is_keyword (parser, RID_EXTENSION))
2875 int ext;
2876 tree decl;
2877 ext = disable_extension_diagnostics ();
2878 c_parser_consume_token (parser);
2879 decl = c_parser_struct_declaration (parser);
2880 restore_extension_diagnostics (ext);
2881 return decl;
2883 if (c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT))
2885 c_parser_static_assert_declaration_no_semi (parser);
2886 return NULL_TREE;
2888 specs = build_null_declspecs ();
2889 decl_loc = c_parser_peek_token (parser)->location;
2890 /* Strictly by the standard, we shouldn't allow _Alignas here,
2891 but it appears to have been intended to allow it there, so
2892 we're keeping it as it is until WG14 reaches a conclusion
2893 of N1731.
2894 <http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1731.pdf> */
2895 c_parser_declspecs (parser, specs, false, true, true,
2896 true, false, cla_nonabstract_decl);
2897 if (parser->error)
2898 return NULL_TREE;
2899 if (!specs->declspecs_seen_p)
2901 c_parser_error (parser, "expected specifier-qualifier-list");
2902 return NULL_TREE;
2904 finish_declspecs (specs);
2905 if (c_parser_next_token_is (parser, CPP_SEMICOLON)
2906 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2908 tree ret;
2909 if (specs->typespec_kind == ctsk_none)
2911 pedwarn (decl_loc, OPT_Wpedantic,
2912 "ISO C forbids member declarations with no members");
2913 shadow_tag_warned (specs, pedantic);
2914 ret = NULL_TREE;
2916 else
2918 /* Support for unnamed structs or unions as members of
2919 structs or unions (which is [a] useful and [b] supports
2920 MS P-SDK). */
2921 tree attrs = NULL;
2923 ret = grokfield (c_parser_peek_token (parser)->location,
2924 build_id_declarator (NULL_TREE), specs,
2925 NULL_TREE, &attrs);
2926 if (ret)
2927 decl_attributes (&ret, attrs, 0);
2929 return ret;
2932 /* Provide better error recovery. Note that a type name here is valid,
2933 and will be treated as a field name. */
2934 if (specs->typespec_kind == ctsk_tagdef
2935 && TREE_CODE (specs->type) != ENUMERAL_TYPE
2936 && c_parser_next_token_starts_declspecs (parser)
2937 && !c_parser_next_token_is (parser, CPP_NAME))
2939 c_parser_error (parser, "expected %<;%>, identifier or %<(%>");
2940 parser->error = false;
2941 return NULL_TREE;
2944 pending_xref_error ();
2945 prefix_attrs = specs->attrs;
2946 all_prefix_attrs = prefix_attrs;
2947 specs->attrs = NULL_TREE;
2948 decls = NULL_TREE;
2949 while (true)
2951 /* Declaring one or more declarators or un-named bit-fields. */
2952 struct c_declarator *declarator;
2953 bool dummy = false;
2954 if (c_parser_next_token_is (parser, CPP_COLON))
2955 declarator = build_id_declarator (NULL_TREE);
2956 else
2957 declarator = c_parser_declarator (parser,
2958 specs->typespec_kind != ctsk_none,
2959 C_DTR_NORMAL, &dummy);
2960 if (declarator == NULL)
2962 c_parser_skip_to_end_of_block_or_statement (parser);
2963 break;
2965 if (c_parser_next_token_is (parser, CPP_COLON)
2966 || c_parser_next_token_is (parser, CPP_COMMA)
2967 || c_parser_next_token_is (parser, CPP_SEMICOLON)
2968 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE)
2969 || c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2971 tree postfix_attrs = NULL_TREE;
2972 tree width = NULL_TREE;
2973 tree d;
2974 if (c_parser_next_token_is (parser, CPP_COLON))
2976 c_parser_consume_token (parser);
2977 width = c_parser_expr_no_commas (parser, NULL).value;
2979 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2980 postfix_attrs = c_parser_attributes (parser);
2981 d = grokfield (c_parser_peek_token (parser)->location,
2982 declarator, specs, width, &all_prefix_attrs);
2983 decl_attributes (&d, chainon (postfix_attrs,
2984 all_prefix_attrs), 0);
2985 DECL_CHAIN (d) = decls;
2986 decls = d;
2987 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2988 all_prefix_attrs = chainon (c_parser_attributes (parser),
2989 prefix_attrs);
2990 else
2991 all_prefix_attrs = prefix_attrs;
2992 if (c_parser_next_token_is (parser, CPP_COMMA))
2993 c_parser_consume_token (parser);
2994 else if (c_parser_next_token_is (parser, CPP_SEMICOLON)
2995 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2997 /* Semicolon consumed in caller. */
2998 break;
3000 else
3002 c_parser_error (parser, "expected %<,%>, %<;%> or %<}%>");
3003 break;
3006 else
3008 c_parser_error (parser,
3009 "expected %<:%>, %<,%>, %<;%>, %<}%> or "
3010 "%<__attribute__%>");
3011 break;
3014 return decls;
3017 /* Parse a typeof specifier (a GNU extension).
3019 typeof-specifier:
3020 typeof ( expression )
3021 typeof ( type-name )
3024 static struct c_typespec
3025 c_parser_typeof_specifier (c_parser *parser)
3027 struct c_typespec ret;
3028 ret.kind = ctsk_typeof;
3029 ret.spec = error_mark_node;
3030 ret.expr = NULL_TREE;
3031 ret.expr_const_operands = true;
3032 gcc_assert (c_parser_next_token_is_keyword (parser, RID_TYPEOF));
3033 c_parser_consume_token (parser);
3034 c_inhibit_evaluation_warnings++;
3035 in_typeof++;
3036 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3038 c_inhibit_evaluation_warnings--;
3039 in_typeof--;
3040 return ret;
3042 if (c_parser_next_tokens_start_typename (parser, cla_prefer_id))
3044 struct c_type_name *type = c_parser_type_name (parser);
3045 c_inhibit_evaluation_warnings--;
3046 in_typeof--;
3047 if (type != NULL)
3049 ret.spec = groktypename (type, &ret.expr, &ret.expr_const_operands);
3050 pop_maybe_used (variably_modified_type_p (ret.spec, NULL_TREE));
3053 else
3055 bool was_vm;
3056 location_t here = c_parser_peek_token (parser)->location;
3057 struct c_expr expr = c_parser_expression (parser);
3058 c_inhibit_evaluation_warnings--;
3059 in_typeof--;
3060 if (TREE_CODE (expr.value) == COMPONENT_REF
3061 && DECL_C_BIT_FIELD (TREE_OPERAND (expr.value, 1)))
3062 error_at (here, "%<typeof%> applied to a bit-field");
3063 mark_exp_read (expr.value);
3064 ret.spec = TREE_TYPE (expr.value);
3065 was_vm = variably_modified_type_p (ret.spec, NULL_TREE);
3066 /* This is returned with the type so that when the type is
3067 evaluated, this can be evaluated. */
3068 if (was_vm)
3069 ret.expr = c_fully_fold (expr.value, false, &ret.expr_const_operands);
3070 pop_maybe_used (was_vm);
3071 /* For use in macros such as those in <stdatomic.h>, remove all
3072 qualifiers from atomic types. (const can be an issue for more macros
3073 using typeof than just the <stdatomic.h> ones.) */
3074 if (ret.spec != error_mark_node && TYPE_ATOMIC (ret.spec))
3075 ret.spec = c_build_qualified_type (ret.spec, TYPE_UNQUALIFIED);
3077 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
3078 return ret;
3081 /* Parse an alignment-specifier.
3083 C11 6.7.5:
3085 alignment-specifier:
3086 _Alignas ( type-name )
3087 _Alignas ( constant-expression )
3090 static tree
3091 c_parser_alignas_specifier (c_parser * parser)
3093 tree ret = error_mark_node;
3094 location_t loc = c_parser_peek_token (parser)->location;
3095 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ALIGNAS));
3096 c_parser_consume_token (parser);
3097 if (flag_isoc99)
3098 pedwarn_c99 (loc, OPT_Wpedantic,
3099 "ISO C99 does not support %<_Alignas%>");
3100 else
3101 pedwarn_c99 (loc, OPT_Wpedantic,
3102 "ISO C90 does not support %<_Alignas%>");
3103 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3104 return ret;
3105 if (c_parser_next_tokens_start_typename (parser, cla_prefer_id))
3107 struct c_type_name *type = c_parser_type_name (parser);
3108 if (type != NULL)
3109 ret = c_sizeof_or_alignof_type (loc, groktypename (type, NULL, NULL),
3110 false, true, 1);
3112 else
3113 ret = c_parser_expr_no_commas (parser, NULL).value;
3114 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
3115 return ret;
3118 /* Parse a declarator, possibly an abstract declarator (C90 6.5.4,
3119 6.5.5, C99 6.7.5, 6.7.6). If TYPE_SEEN_P then a typedef name may
3120 be redeclared; otherwise it may not. KIND indicates which kind of
3121 declarator is wanted. Returns a valid declarator except in the
3122 case of a syntax error in which case NULL is returned. *SEEN_ID is
3123 set to true if an identifier being declared is seen; this is used
3124 to diagnose bad forms of abstract array declarators and to
3125 determine whether an identifier list is syntactically permitted.
3127 declarator:
3128 pointer[opt] direct-declarator
3130 direct-declarator:
3131 identifier
3132 ( attributes[opt] declarator )
3133 direct-declarator array-declarator
3134 direct-declarator ( parameter-type-list )
3135 direct-declarator ( identifier-list[opt] )
3137 pointer:
3138 * type-qualifier-list[opt]
3139 * type-qualifier-list[opt] pointer
3141 type-qualifier-list:
3142 type-qualifier
3143 attributes
3144 type-qualifier-list type-qualifier
3145 type-qualifier-list attributes
3147 array-declarator:
3148 [ type-qualifier-list[opt] assignment-expression[opt] ]
3149 [ static type-qualifier-list[opt] assignment-expression ]
3150 [ type-qualifier-list static assignment-expression ]
3151 [ type-qualifier-list[opt] * ]
3153 parameter-type-list:
3154 parameter-list
3155 parameter-list , ...
3157 parameter-list:
3158 parameter-declaration
3159 parameter-list , parameter-declaration
3161 parameter-declaration:
3162 declaration-specifiers declarator attributes[opt]
3163 declaration-specifiers abstract-declarator[opt] attributes[opt]
3165 identifier-list:
3166 identifier
3167 identifier-list , identifier
3169 abstract-declarator:
3170 pointer
3171 pointer[opt] direct-abstract-declarator
3173 direct-abstract-declarator:
3174 ( attributes[opt] abstract-declarator )
3175 direct-abstract-declarator[opt] array-declarator
3176 direct-abstract-declarator[opt] ( parameter-type-list[opt] )
3178 GNU extensions:
3180 direct-declarator:
3181 direct-declarator ( parameter-forward-declarations
3182 parameter-type-list[opt] )
3184 direct-abstract-declarator:
3185 direct-abstract-declarator[opt] ( parameter-forward-declarations
3186 parameter-type-list[opt] )
3188 parameter-forward-declarations:
3189 parameter-list ;
3190 parameter-forward-declarations parameter-list ;
3192 The uses of attributes shown above are GNU extensions.
3194 Some forms of array declarator are not included in C99 in the
3195 syntax for abstract declarators; these are disallowed elsewhere.
3196 This may be a defect (DR#289).
3198 This function also accepts an omitted abstract declarator as being
3199 an abstract declarator, although not part of the formal syntax. */
3201 static struct c_declarator *
3202 c_parser_declarator (c_parser *parser, bool type_seen_p, c_dtr_syn kind,
3203 bool *seen_id)
3205 /* Parse any initial pointer part. */
3206 if (c_parser_next_token_is (parser, CPP_MULT))
3208 struct c_declspecs *quals_attrs = build_null_declspecs ();
3209 struct c_declarator *inner;
3210 c_parser_consume_token (parser);
3211 c_parser_declspecs (parser, quals_attrs, false, false, true,
3212 false, false, cla_prefer_id);
3213 inner = c_parser_declarator (parser, type_seen_p, kind, seen_id);
3214 if (inner == NULL)
3215 return NULL;
3216 else
3217 return make_pointer_declarator (quals_attrs, inner);
3219 /* Now we have a direct declarator, direct abstract declarator or
3220 nothing (which counts as a direct abstract declarator here). */
3221 return c_parser_direct_declarator (parser, type_seen_p, kind, seen_id);
3224 /* Parse a direct declarator or direct abstract declarator; arguments
3225 as c_parser_declarator. */
3227 static struct c_declarator *
3228 c_parser_direct_declarator (c_parser *parser, bool type_seen_p, c_dtr_syn kind,
3229 bool *seen_id)
3231 /* The direct declarator must start with an identifier (possibly
3232 omitted) or a parenthesized declarator (possibly abstract). In
3233 an ordinary declarator, initial parentheses must start a
3234 parenthesized declarator. In an abstract declarator or parameter
3235 declarator, they could start a parenthesized declarator or a
3236 parameter list. To tell which, the open parenthesis and any
3237 following attributes must be read. If a declaration specifier
3238 follows, then it is a parameter list; if the specifier is a
3239 typedef name, there might be an ambiguity about redeclaring it,
3240 which is resolved in the direction of treating it as a typedef
3241 name. If a close parenthesis follows, it is also an empty
3242 parameter list, as the syntax does not permit empty abstract
3243 declarators. Otherwise, it is a parenthesized declarator (in
3244 which case the analysis may be repeated inside it, recursively).
3246 ??? There is an ambiguity in a parameter declaration "int
3247 (__attribute__((foo)) x)", where x is not a typedef name: it
3248 could be an abstract declarator for a function, or declare x with
3249 parentheses. The proper resolution of this ambiguity needs
3250 documenting. At present we follow an accident of the old
3251 parser's implementation, whereby the first parameter must have
3252 some declaration specifiers other than just attributes. Thus as
3253 a parameter declaration it is treated as a parenthesized
3254 parameter named x, and as an abstract declarator it is
3255 rejected.
3257 ??? Also following the old parser, attributes inside an empty
3258 parameter list are ignored, making it a list not yielding a
3259 prototype, rather than giving an error or making it have one
3260 parameter with implicit type int.
3262 ??? Also following the old parser, typedef names may be
3263 redeclared in declarators, but not Objective-C class names. */
3265 if (kind != C_DTR_ABSTRACT
3266 && c_parser_next_token_is (parser, CPP_NAME)
3267 && ((type_seen_p
3268 && (c_parser_peek_token (parser)->id_kind == C_ID_TYPENAME
3269 || c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME))
3270 || c_parser_peek_token (parser)->id_kind == C_ID_ID))
3272 struct c_declarator *inner
3273 = build_id_declarator (c_parser_peek_token (parser)->value);
3274 *seen_id = true;
3275 inner->id_loc = c_parser_peek_token (parser)->location;
3276 c_parser_consume_token (parser);
3277 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
3280 if (kind != C_DTR_NORMAL
3281 && c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
3283 struct c_declarator *inner = build_id_declarator (NULL_TREE);
3284 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
3287 /* Either we are at the end of an abstract declarator, or we have
3288 parentheses. */
3290 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
3292 tree attrs;
3293 struct c_declarator *inner;
3294 c_parser_consume_token (parser);
3295 attrs = c_parser_attributes (parser);
3296 if (kind != C_DTR_NORMAL
3297 && (c_parser_next_token_starts_declspecs (parser)
3298 || c_parser_next_token_is (parser, CPP_CLOSE_PAREN)))
3300 struct c_arg_info *args
3301 = c_parser_parms_declarator (parser, kind == C_DTR_NORMAL,
3302 attrs);
3303 if (args == NULL)
3304 return NULL;
3305 else
3307 inner
3308 = build_function_declarator (args,
3309 build_id_declarator (NULL_TREE));
3310 return c_parser_direct_declarator_inner (parser, *seen_id,
3311 inner);
3314 /* A parenthesized declarator. */
3315 inner = c_parser_declarator (parser, type_seen_p, kind, seen_id);
3316 if (inner != NULL && attrs != NULL)
3317 inner = build_attrs_declarator (attrs, inner);
3318 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3320 c_parser_consume_token (parser);
3321 if (inner == NULL)
3322 return NULL;
3323 else
3324 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
3326 else
3328 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3329 "expected %<)%>");
3330 return NULL;
3333 else
3335 if (kind == C_DTR_NORMAL)
3337 c_parser_error (parser, "expected identifier or %<(%>");
3338 return NULL;
3340 else
3341 return build_id_declarator (NULL_TREE);
3345 /* Parse part of a direct declarator or direct abstract declarator,
3346 given that some (in INNER) has already been parsed; ID_PRESENT is
3347 true if an identifier is present, false for an abstract
3348 declarator. */
3350 static struct c_declarator *
3351 c_parser_direct_declarator_inner (c_parser *parser, bool id_present,
3352 struct c_declarator *inner)
3354 /* Parse a sequence of array declarators and parameter lists. */
3355 if (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
3357 location_t brace_loc = c_parser_peek_token (parser)->location;
3358 struct c_declarator *declarator;
3359 struct c_declspecs *quals_attrs = build_null_declspecs ();
3360 bool static_seen;
3361 bool star_seen;
3362 struct c_expr dimen;
3363 dimen.value = NULL_TREE;
3364 dimen.original_code = ERROR_MARK;
3365 dimen.original_type = NULL_TREE;
3366 c_parser_consume_token (parser);
3367 c_parser_declspecs (parser, quals_attrs, false, false, true,
3368 false, false, cla_prefer_id);
3369 static_seen = c_parser_next_token_is_keyword (parser, RID_STATIC);
3370 if (static_seen)
3371 c_parser_consume_token (parser);
3372 if (static_seen && !quals_attrs->declspecs_seen_p)
3373 c_parser_declspecs (parser, quals_attrs, false, false, true,
3374 false, false, cla_prefer_id);
3375 if (!quals_attrs->declspecs_seen_p)
3376 quals_attrs = NULL;
3377 /* If "static" is present, there must be an array dimension.
3378 Otherwise, there may be a dimension, "*", or no
3379 dimension. */
3380 if (static_seen)
3382 star_seen = false;
3383 dimen = c_parser_expr_no_commas (parser, NULL);
3385 else
3387 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
3389 dimen.value = NULL_TREE;
3390 star_seen = false;
3392 else if (flag_cilkplus
3393 && c_parser_next_token_is (parser, CPP_COLON))
3395 dimen.value = error_mark_node;
3396 star_seen = false;
3397 error_at (c_parser_peek_token (parser)->location,
3398 "array notations cannot be used in declaration");
3399 c_parser_consume_token (parser);
3401 else if (c_parser_next_token_is (parser, CPP_MULT))
3403 if (c_parser_peek_2nd_token (parser)->type == CPP_CLOSE_SQUARE)
3405 dimen.value = NULL_TREE;
3406 star_seen = true;
3407 c_parser_consume_token (parser);
3409 else
3411 star_seen = false;
3412 dimen = c_parser_expr_no_commas (parser, NULL);
3415 else
3417 star_seen = false;
3418 dimen = c_parser_expr_no_commas (parser, NULL);
3421 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
3422 c_parser_consume_token (parser);
3423 else if (flag_cilkplus
3424 && c_parser_next_token_is (parser, CPP_COLON))
3426 error_at (c_parser_peek_token (parser)->location,
3427 "array notations cannot be used in declaration");
3428 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
3429 return NULL;
3431 else
3433 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
3434 "expected %<]%>");
3435 return NULL;
3437 if (dimen.value)
3438 dimen = convert_lvalue_to_rvalue (brace_loc, dimen, true, true);
3439 declarator = build_array_declarator (brace_loc, dimen.value, quals_attrs,
3440 static_seen, star_seen);
3441 if (declarator == NULL)
3442 return NULL;
3443 inner = set_array_declarator_inner (declarator, inner);
3444 return c_parser_direct_declarator_inner (parser, id_present, inner);
3446 else if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
3448 tree attrs;
3449 struct c_arg_info *args;
3450 c_parser_consume_token (parser);
3451 attrs = c_parser_attributes (parser);
3452 args = c_parser_parms_declarator (parser, id_present, attrs);
3453 if (args == NULL)
3454 return NULL;
3455 else
3457 inner = build_function_declarator (args, inner);
3458 return c_parser_direct_declarator_inner (parser, id_present, inner);
3461 return inner;
3464 /* Parse a parameter list or identifier list, including the closing
3465 parenthesis but not the opening one. ATTRS are the attributes at
3466 the start of the list. ID_LIST_OK is true if an identifier list is
3467 acceptable; such a list must not have attributes at the start. */
3469 static struct c_arg_info *
3470 c_parser_parms_declarator (c_parser *parser, bool id_list_ok, tree attrs)
3472 push_scope ();
3473 declare_parm_level ();
3474 /* If the list starts with an identifier, it is an identifier list.
3475 Otherwise, it is either a prototype list or an empty list. */
3476 if (id_list_ok
3477 && !attrs
3478 && c_parser_next_token_is (parser, CPP_NAME)
3479 && c_parser_peek_token (parser)->id_kind == C_ID_ID
3481 /* Look ahead to detect typos in type names. */
3482 && c_parser_peek_2nd_token (parser)->type != CPP_NAME
3483 && c_parser_peek_2nd_token (parser)->type != CPP_MULT
3484 && c_parser_peek_2nd_token (parser)->type != CPP_OPEN_PAREN
3485 && c_parser_peek_2nd_token (parser)->type != CPP_OPEN_SQUARE)
3487 tree list = NULL_TREE, *nextp = &list;
3488 while (c_parser_next_token_is (parser, CPP_NAME)
3489 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
3491 *nextp = build_tree_list (NULL_TREE,
3492 c_parser_peek_token (parser)->value);
3493 nextp = & TREE_CHAIN (*nextp);
3494 c_parser_consume_token (parser);
3495 if (c_parser_next_token_is_not (parser, CPP_COMMA))
3496 break;
3497 c_parser_consume_token (parser);
3498 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3500 c_parser_error (parser, "expected identifier");
3501 break;
3504 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3506 struct c_arg_info *ret = build_arg_info ();
3507 ret->types = list;
3508 c_parser_consume_token (parser);
3509 pop_scope ();
3510 return ret;
3512 else
3514 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3515 "expected %<)%>");
3516 pop_scope ();
3517 return NULL;
3520 else
3522 struct c_arg_info *ret = c_parser_parms_list_declarator (parser, attrs,
3523 NULL);
3524 pop_scope ();
3525 return ret;
3529 /* Parse a parameter list (possibly empty), including the closing
3530 parenthesis but not the opening one. ATTRS are the attributes at
3531 the start of the list. EXPR is NULL or an expression that needs to
3532 be evaluated for the side effects of array size expressions in the
3533 parameters. */
3535 static struct c_arg_info *
3536 c_parser_parms_list_declarator (c_parser *parser, tree attrs, tree expr)
3538 bool bad_parm = false;
3540 /* ??? Following the old parser, forward parameter declarations may
3541 use abstract declarators, and if no real parameter declarations
3542 follow the forward declarations then this is not diagnosed. Also
3543 note as above that attributes are ignored as the only contents of
3544 the parentheses, or as the only contents after forward
3545 declarations. */
3546 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3548 struct c_arg_info *ret = build_arg_info ();
3549 c_parser_consume_token (parser);
3550 return ret;
3552 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
3554 struct c_arg_info *ret = build_arg_info ();
3556 if (flag_allow_parameterless_variadic_functions)
3558 /* F (...) is allowed. */
3559 ret->types = NULL_TREE;
3561 else
3563 /* Suppress -Wold-style-definition for this case. */
3564 ret->types = error_mark_node;
3565 error_at (c_parser_peek_token (parser)->location,
3566 "ISO C requires a named argument before %<...%>");
3568 c_parser_consume_token (parser);
3569 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3571 c_parser_consume_token (parser);
3572 return ret;
3574 else
3576 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3577 "expected %<)%>");
3578 return NULL;
3581 /* Nonempty list of parameters, either terminated with semicolon
3582 (forward declarations; recurse) or with close parenthesis (normal
3583 function) or with ", ... )" (variadic function). */
3584 while (true)
3586 /* Parse a parameter. */
3587 struct c_parm *parm = c_parser_parameter_declaration (parser, attrs);
3588 attrs = NULL_TREE;
3589 if (parm == NULL)
3590 bad_parm = true;
3591 else
3592 push_parm_decl (parm, &expr);
3593 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
3595 tree new_attrs;
3596 c_parser_consume_token (parser);
3597 mark_forward_parm_decls ();
3598 new_attrs = c_parser_attributes (parser);
3599 return c_parser_parms_list_declarator (parser, new_attrs, expr);
3601 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3603 c_parser_consume_token (parser);
3604 if (bad_parm)
3605 return NULL;
3606 else
3607 return get_parm_info (false, expr);
3609 if (!c_parser_require (parser, CPP_COMMA,
3610 "expected %<;%>, %<,%> or %<)%>"))
3612 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
3613 return NULL;
3615 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
3617 c_parser_consume_token (parser);
3618 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3620 c_parser_consume_token (parser);
3621 if (bad_parm)
3622 return NULL;
3623 else
3624 return get_parm_info (true, expr);
3626 else
3628 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3629 "expected %<)%>");
3630 return NULL;
3636 /* Parse a parameter declaration. ATTRS are the attributes at the
3637 start of the declaration if it is the first parameter. */
3639 static struct c_parm *
3640 c_parser_parameter_declaration (c_parser *parser, tree attrs)
3642 struct c_declspecs *specs;
3643 struct c_declarator *declarator;
3644 tree prefix_attrs;
3645 tree postfix_attrs = NULL_TREE;
3646 bool dummy = false;
3648 /* Accept #pragmas between parameter declarations. */
3649 while (c_parser_next_token_is (parser, CPP_PRAGMA))
3650 c_parser_pragma (parser, pragma_param);
3652 if (!c_parser_next_token_starts_declspecs (parser))
3654 c_token *token = c_parser_peek_token (parser);
3655 if (parser->error)
3656 return NULL;
3657 c_parser_set_source_position_from_token (token);
3658 if (c_parser_next_tokens_start_typename (parser, cla_prefer_type))
3660 error_at (token->location, "unknown type name %qE", token->value);
3661 parser->error = true;
3663 /* ??? In some Objective-C cases '...' isn't applicable so there
3664 should be a different message. */
3665 else
3666 c_parser_error (parser,
3667 "expected declaration specifiers or %<...%>");
3668 c_parser_skip_to_end_of_parameter (parser);
3669 return NULL;
3671 specs = build_null_declspecs ();
3672 if (attrs)
3674 declspecs_add_attrs (input_location, specs, attrs);
3675 attrs = NULL_TREE;
3677 c_parser_declspecs (parser, specs, true, true, true, true, false,
3678 cla_nonabstract_decl);
3679 finish_declspecs (specs);
3680 pending_xref_error ();
3681 prefix_attrs = specs->attrs;
3682 specs->attrs = NULL_TREE;
3683 declarator = c_parser_declarator (parser,
3684 specs->typespec_kind != ctsk_none,
3685 C_DTR_PARM, &dummy);
3686 if (declarator == NULL)
3688 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
3689 return NULL;
3691 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
3692 postfix_attrs = c_parser_attributes (parser);
3693 return build_c_parm (specs, chainon (postfix_attrs, prefix_attrs),
3694 declarator);
3697 /* Parse a string literal in an asm expression. It should not be
3698 translated, and wide string literals are an error although
3699 permitted by the syntax. This is a GNU extension.
3701 asm-string-literal:
3702 string-literal
3704 ??? At present, following the old parser, the caller needs to have
3705 set lex_untranslated_string to 1. It would be better to follow the
3706 C++ parser rather than using this kludge. */
3708 static tree
3709 c_parser_asm_string_literal (c_parser *parser)
3711 tree str;
3712 int save_flag = warn_overlength_strings;
3713 warn_overlength_strings = 0;
3714 if (c_parser_next_token_is (parser, CPP_STRING))
3716 str = c_parser_peek_token (parser)->value;
3717 c_parser_consume_token (parser);
3719 else if (c_parser_next_token_is (parser, CPP_WSTRING))
3721 error_at (c_parser_peek_token (parser)->location,
3722 "wide string literal in %<asm%>");
3723 str = build_string (1, "");
3724 c_parser_consume_token (parser);
3726 else
3728 c_parser_error (parser, "expected string literal");
3729 str = NULL_TREE;
3731 warn_overlength_strings = save_flag;
3732 return str;
3735 /* Parse a simple asm expression. This is used in restricted
3736 contexts, where a full expression with inputs and outputs does not
3737 make sense. This is a GNU extension.
3739 simple-asm-expr:
3740 asm ( asm-string-literal )
3743 static tree
3744 c_parser_simple_asm_expr (c_parser *parser)
3746 tree str;
3747 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ASM));
3748 /* ??? Follow the C++ parser rather than using the
3749 lex_untranslated_string kludge. */
3750 parser->lex_untranslated_string = true;
3751 c_parser_consume_token (parser);
3752 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3754 parser->lex_untranslated_string = false;
3755 return NULL_TREE;
3757 str = c_parser_asm_string_literal (parser);
3758 parser->lex_untranslated_string = false;
3759 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
3761 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
3762 return NULL_TREE;
3764 return str;
3767 static tree
3768 c_parser_attribute_any_word (c_parser *parser)
3770 tree attr_name = NULL_TREE;
3772 if (c_parser_next_token_is (parser, CPP_KEYWORD))
3774 /* ??? See comment above about what keywords are accepted here. */
3775 bool ok;
3776 switch (c_parser_peek_token (parser)->keyword)
3778 case RID_STATIC:
3779 case RID_UNSIGNED:
3780 case RID_LONG:
3781 case RID_CONST:
3782 case RID_EXTERN:
3783 case RID_REGISTER:
3784 case RID_TYPEDEF:
3785 case RID_SHORT:
3786 case RID_INLINE:
3787 case RID_NORETURN:
3788 case RID_VOLATILE:
3789 case RID_SIGNED:
3790 case RID_AUTO:
3791 case RID_RESTRICT:
3792 case RID_COMPLEX:
3793 case RID_THREAD:
3794 case RID_INT:
3795 case RID_CHAR:
3796 case RID_FLOAT:
3797 case RID_DOUBLE:
3798 case RID_VOID:
3799 case RID_DFLOAT32:
3800 case RID_DFLOAT64:
3801 case RID_DFLOAT128:
3802 case RID_BOOL:
3803 case RID_FRACT:
3804 case RID_ACCUM:
3805 case RID_SAT:
3806 case RID_TRANSACTION_ATOMIC:
3807 case RID_TRANSACTION_CANCEL:
3808 case RID_ATOMIC:
3809 case RID_AUTO_TYPE:
3810 case RID_INT_N_0:
3811 case RID_INT_N_1:
3812 case RID_INT_N_2:
3813 case RID_INT_N_3:
3814 ok = true;
3815 break;
3816 default:
3817 ok = false;
3818 break;
3820 if (!ok)
3821 return NULL_TREE;
3823 /* Accept __attribute__((__const)) as __attribute__((const)) etc. */
3824 attr_name = ridpointers[(int) c_parser_peek_token (parser)->keyword];
3826 else if (c_parser_next_token_is (parser, CPP_NAME))
3827 attr_name = c_parser_peek_token (parser)->value;
3829 return attr_name;
3832 /* Returns true of NAME is an IDENTIFIER_NODE with identiifer "vector,"
3833 "__vector" or "__vector__." */
3835 static inline bool
3836 is_cilkplus_vector_p (tree name)
3838 if (flag_cilkplus && is_attribute_p ("vector", name))
3839 return true;
3840 return false;
3843 #define CILK_SIMD_FN_CLAUSE_MASK \
3844 ((OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_VECTORLENGTH) \
3845 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_LINEAR) \
3846 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_UNIFORM) \
3847 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_MASK) \
3848 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_NOMASK))
3850 /* Parses the vector attribute of SIMD enabled functions in Cilk Plus.
3851 VEC_TOKEN is the "vector" token that is replaced with "simd" and
3852 pushed into the token list.
3853 Syntax:
3854 vector
3855 vector (<vector attributes>). */
3857 static void
3858 c_parser_cilk_simd_fn_vector_attrs (c_parser *parser, c_token vec_token)
3860 gcc_assert (is_cilkplus_vector_p (vec_token.value));
3862 int paren_scope = 0;
3863 vec_safe_push (parser->cilk_simd_fn_tokens, vec_token);
3864 /* Consume the "vector" token. */
3865 c_parser_consume_token (parser);
3867 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
3869 c_parser_consume_token (parser);
3870 paren_scope++;
3872 while (paren_scope > 0)
3874 c_token *token = c_parser_peek_token (parser);
3875 if (token->type == CPP_OPEN_PAREN)
3876 paren_scope++;
3877 else if (token->type == CPP_CLOSE_PAREN)
3878 paren_scope--;
3879 /* Do not push the last ')' since we are not pushing the '('. */
3880 if (!(token->type == CPP_CLOSE_PAREN && paren_scope == 0))
3881 vec_safe_push (parser->cilk_simd_fn_tokens, *token);
3882 c_parser_consume_token (parser);
3885 /* Since we are converting an attribute to a pragma, we need to end the
3886 attribute with PRAGMA_EOL. */
3887 c_token eol_token;
3888 memset (&eol_token, 0, sizeof (eol_token));
3889 eol_token.type = CPP_PRAGMA_EOL;
3890 vec_safe_push (parser->cilk_simd_fn_tokens, eol_token);
3893 /* Add 2 CPP_EOF at the end of PARSER->ELEM_FN_TOKENS vector. */
3895 static void
3896 c_finish_cilk_simd_fn_tokens (c_parser *parser)
3898 c_token last_token = parser->cilk_simd_fn_tokens->last ();
3900 /* c_parser_attributes is called in several places, so if these EOF
3901 tokens are already inserted, then don't do them again. */
3902 if (last_token.type == CPP_EOF)
3903 return;
3905 /* Two CPP_EOF token are added as a safety net since the normal C
3906 front-end has two token look-ahead. */
3907 c_token eof_token;
3908 eof_token.type = CPP_EOF;
3909 vec_safe_push (parser->cilk_simd_fn_tokens, eof_token);
3910 vec_safe_push (parser->cilk_simd_fn_tokens, eof_token);
3913 /* Parse (possibly empty) attributes. This is a GNU extension.
3915 attributes:
3916 empty
3917 attributes attribute
3919 attribute:
3920 __attribute__ ( ( attribute-list ) )
3922 attribute-list:
3923 attrib
3924 attribute_list , attrib
3926 attrib:
3927 empty
3928 any-word
3929 any-word ( identifier )
3930 any-word ( identifier , nonempty-expr-list )
3931 any-word ( expr-list )
3933 where the "identifier" must not be declared as a type, and
3934 "any-word" may be any identifier (including one declared as a
3935 type), a reserved word storage class specifier, type specifier or
3936 type qualifier. ??? This still leaves out most reserved keywords
3937 (following the old parser), shouldn't we include them, and why not
3938 allow identifiers declared as types to start the arguments? */
3940 static tree
3941 c_parser_attributes (c_parser *parser)
3943 tree attrs = NULL_TREE;
3944 while (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
3946 /* ??? Follow the C++ parser rather than using the
3947 lex_untranslated_string kludge. */
3948 parser->lex_untranslated_string = true;
3949 c_parser_consume_token (parser);
3950 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3952 parser->lex_untranslated_string = false;
3953 return attrs;
3955 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3957 parser->lex_untranslated_string = false;
3958 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
3959 return attrs;
3961 /* Parse the attribute list. */
3962 while (c_parser_next_token_is (parser, CPP_COMMA)
3963 || c_parser_next_token_is (parser, CPP_NAME)
3964 || c_parser_next_token_is (parser, CPP_KEYWORD))
3966 tree attr, attr_name, attr_args;
3967 vec<tree, va_gc> *expr_list;
3968 if (c_parser_next_token_is (parser, CPP_COMMA))
3970 c_parser_consume_token (parser);
3971 continue;
3974 attr_name = c_parser_attribute_any_word (parser);
3975 if (attr_name == NULL)
3976 break;
3977 if (is_cilkplus_vector_p (attr_name))
3979 c_token *v_token = c_parser_peek_token (parser);
3980 c_parser_cilk_simd_fn_vector_attrs (parser, *v_token);
3981 continue;
3983 c_parser_consume_token (parser);
3984 if (c_parser_next_token_is_not (parser, CPP_OPEN_PAREN))
3986 attr = build_tree_list (attr_name, NULL_TREE);
3987 attrs = chainon (attrs, attr);
3988 continue;
3990 c_parser_consume_token (parser);
3991 /* Parse the attribute contents. If they start with an
3992 identifier which is followed by a comma or close
3993 parenthesis, then the arguments start with that
3994 identifier; otherwise they are an expression list.
3995 In objective-c the identifier may be a classname. */
3996 if (c_parser_next_token_is (parser, CPP_NAME)
3997 && (c_parser_peek_token (parser)->id_kind == C_ID_ID
3998 || (c_dialect_objc ()
3999 && c_parser_peek_token (parser)->id_kind
4000 == C_ID_CLASSNAME))
4001 && ((c_parser_peek_2nd_token (parser)->type == CPP_COMMA)
4002 || (c_parser_peek_2nd_token (parser)->type
4003 == CPP_CLOSE_PAREN))
4004 && (attribute_takes_identifier_p (attr_name)
4005 || (c_dialect_objc ()
4006 && c_parser_peek_token (parser)->id_kind
4007 == C_ID_CLASSNAME)))
4009 tree arg1 = c_parser_peek_token (parser)->value;
4010 c_parser_consume_token (parser);
4011 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4012 attr_args = build_tree_list (NULL_TREE, arg1);
4013 else
4015 tree tree_list;
4016 c_parser_consume_token (parser);
4017 expr_list = c_parser_expr_list (parser, false, true,
4018 NULL, NULL, NULL, NULL);
4019 tree_list = build_tree_list_vec (expr_list);
4020 attr_args = tree_cons (NULL_TREE, arg1, tree_list);
4021 release_tree_vector (expr_list);
4024 else
4026 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4027 attr_args = NULL_TREE;
4028 else
4030 expr_list = c_parser_expr_list (parser, false, true,
4031 NULL, NULL, NULL, NULL);
4032 attr_args = build_tree_list_vec (expr_list);
4033 release_tree_vector (expr_list);
4036 attr = build_tree_list (attr_name, attr_args);
4037 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4038 c_parser_consume_token (parser);
4039 else
4041 parser->lex_untranslated_string = false;
4042 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
4043 "expected %<)%>");
4044 return attrs;
4046 attrs = chainon (attrs, attr);
4048 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4049 c_parser_consume_token (parser);
4050 else
4052 parser->lex_untranslated_string = false;
4053 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
4054 "expected %<)%>");
4055 return attrs;
4057 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4058 c_parser_consume_token (parser);
4059 else
4061 parser->lex_untranslated_string = false;
4062 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
4063 "expected %<)%>");
4064 return attrs;
4066 parser->lex_untranslated_string = false;
4069 if (flag_cilkplus && !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
4070 c_finish_cilk_simd_fn_tokens (parser);
4071 return attrs;
4074 /* Parse a type name (C90 6.5.5, C99 6.7.6).
4076 type-name:
4077 specifier-qualifier-list abstract-declarator[opt]
4080 static struct c_type_name *
4081 c_parser_type_name (c_parser *parser)
4083 struct c_declspecs *specs = build_null_declspecs ();
4084 struct c_declarator *declarator;
4085 struct c_type_name *ret;
4086 bool dummy = false;
4087 c_parser_declspecs (parser, specs, false, true, true, false, false,
4088 cla_prefer_type);
4089 if (!specs->declspecs_seen_p)
4091 c_parser_error (parser, "expected specifier-qualifier-list");
4092 return NULL;
4094 if (specs->type != error_mark_node)
4096 pending_xref_error ();
4097 finish_declspecs (specs);
4099 declarator = c_parser_declarator (parser,
4100 specs->typespec_kind != ctsk_none,
4101 C_DTR_ABSTRACT, &dummy);
4102 if (declarator == NULL)
4103 return NULL;
4104 ret = XOBNEW (&parser_obstack, struct c_type_name);
4105 ret->specs = specs;
4106 ret->declarator = declarator;
4107 return ret;
4110 /* Parse an initializer (C90 6.5.7, C99 6.7.8).
4112 initializer:
4113 assignment-expression
4114 { initializer-list }
4115 { initializer-list , }
4117 initializer-list:
4118 designation[opt] initializer
4119 initializer-list , designation[opt] initializer
4121 designation:
4122 designator-list =
4124 designator-list:
4125 designator
4126 designator-list designator
4128 designator:
4129 array-designator
4130 . identifier
4132 array-designator:
4133 [ constant-expression ]
4135 GNU extensions:
4137 initializer:
4140 designation:
4141 array-designator
4142 identifier :
4144 array-designator:
4145 [ constant-expression ... constant-expression ]
4147 Any expression without commas is accepted in the syntax for the
4148 constant-expressions, with non-constant expressions rejected later.
4150 This function is only used for top-level initializers; for nested
4151 ones, see c_parser_initval. */
4153 static struct c_expr
4154 c_parser_initializer (c_parser *parser)
4156 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
4157 return c_parser_braced_init (parser, NULL_TREE, false);
4158 else
4160 struct c_expr ret;
4161 location_t loc = c_parser_peek_token (parser)->location;
4162 ret = c_parser_expr_no_commas (parser, NULL);
4163 if (TREE_CODE (ret.value) != STRING_CST
4164 && TREE_CODE (ret.value) != COMPOUND_LITERAL_EXPR)
4165 ret = convert_lvalue_to_rvalue (loc, ret, true, true);
4166 return ret;
4170 /* Parse a braced initializer list. TYPE is the type specified for a
4171 compound literal, and NULL_TREE for other initializers and for
4172 nested braced lists. NESTED_P is true for nested braced lists,
4173 false for the list of a compound literal or the list that is the
4174 top-level initializer in a declaration. */
4176 static struct c_expr
4177 c_parser_braced_init (c_parser *parser, tree type, bool nested_p)
4179 struct c_expr ret;
4180 struct obstack braced_init_obstack;
4181 location_t brace_loc = c_parser_peek_token (parser)->location;
4182 gcc_obstack_init (&braced_init_obstack);
4183 gcc_assert (c_parser_next_token_is (parser, CPP_OPEN_BRACE));
4184 c_parser_consume_token (parser);
4185 if (nested_p)
4186 push_init_level (brace_loc, 0, &braced_init_obstack);
4187 else
4188 really_start_incremental_init (type);
4189 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
4191 pedwarn (brace_loc, OPT_Wpedantic, "ISO C forbids empty initializer braces");
4193 else
4195 /* Parse a non-empty initializer list, possibly with a trailing
4196 comma. */
4197 while (true)
4199 c_parser_initelt (parser, &braced_init_obstack);
4200 if (parser->error)
4201 break;
4202 if (c_parser_next_token_is (parser, CPP_COMMA))
4203 c_parser_consume_token (parser);
4204 else
4205 break;
4206 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
4207 break;
4210 if (c_parser_next_token_is_not (parser, CPP_CLOSE_BRACE))
4212 ret.value = error_mark_node;
4213 ret.original_code = ERROR_MARK;
4214 ret.original_type = NULL;
4215 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, "expected %<}%>");
4216 pop_init_level (brace_loc, 0, &braced_init_obstack);
4217 obstack_free (&braced_init_obstack, NULL);
4218 return ret;
4220 c_parser_consume_token (parser);
4221 ret = pop_init_level (brace_loc, 0, &braced_init_obstack);
4222 obstack_free (&braced_init_obstack, NULL);
4223 return ret;
4226 /* Parse a nested initializer, including designators. */
4228 static void
4229 c_parser_initelt (c_parser *parser, struct obstack * braced_init_obstack)
4231 /* Parse any designator or designator list. A single array
4232 designator may have the subsequent "=" omitted in GNU C, but a
4233 longer list or a structure member designator may not. */
4234 if (c_parser_next_token_is (parser, CPP_NAME)
4235 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
4237 /* Old-style structure member designator. */
4238 set_init_label (c_parser_peek_token (parser)->location,
4239 c_parser_peek_token (parser)->value,
4240 braced_init_obstack);
4241 /* Use the colon as the error location. */
4242 pedwarn (c_parser_peek_2nd_token (parser)->location, OPT_Wpedantic,
4243 "obsolete use of designated initializer with %<:%>");
4244 c_parser_consume_token (parser);
4245 c_parser_consume_token (parser);
4247 else
4249 /* des_seen is 0 if there have been no designators, 1 if there
4250 has been a single array designator and 2 otherwise. */
4251 int des_seen = 0;
4252 /* Location of a designator. */
4253 location_t des_loc = UNKNOWN_LOCATION; /* Quiet warning. */
4254 while (c_parser_next_token_is (parser, CPP_OPEN_SQUARE)
4255 || c_parser_next_token_is (parser, CPP_DOT))
4257 int des_prev = des_seen;
4258 if (!des_seen)
4259 des_loc = c_parser_peek_token (parser)->location;
4260 if (des_seen < 2)
4261 des_seen++;
4262 if (c_parser_next_token_is (parser, CPP_DOT))
4264 des_seen = 2;
4265 c_parser_consume_token (parser);
4266 if (c_parser_next_token_is (parser, CPP_NAME))
4268 set_init_label (des_loc, c_parser_peek_token (parser)->value,
4269 braced_init_obstack);
4270 c_parser_consume_token (parser);
4272 else
4274 struct c_expr init;
4275 init.value = error_mark_node;
4276 init.original_code = ERROR_MARK;
4277 init.original_type = NULL;
4278 c_parser_error (parser, "expected identifier");
4279 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
4280 process_init_element (input_location, init, false,
4281 braced_init_obstack);
4282 return;
4285 else
4287 tree first, second;
4288 location_t ellipsis_loc = UNKNOWN_LOCATION; /* Quiet warning. */
4289 location_t array_index_loc = UNKNOWN_LOCATION;
4290 /* ??? Following the old parser, [ objc-receiver
4291 objc-message-args ] is accepted as an initializer,
4292 being distinguished from a designator by what follows
4293 the first assignment expression inside the square
4294 brackets, but after a first array designator a
4295 subsequent square bracket is for Objective-C taken to
4296 start an expression, using the obsolete form of
4297 designated initializer without '=', rather than
4298 possibly being a second level of designation: in LALR
4299 terms, the '[' is shifted rather than reducing
4300 designator to designator-list. */
4301 if (des_prev == 1 && c_dialect_objc ())
4303 des_seen = des_prev;
4304 break;
4306 if (des_prev == 0 && c_dialect_objc ())
4308 /* This might be an array designator or an
4309 Objective-C message expression. If the former,
4310 continue parsing here; if the latter, parse the
4311 remainder of the initializer given the starting
4312 primary-expression. ??? It might make sense to
4313 distinguish when des_prev == 1 as well; see
4314 previous comment. */
4315 tree rec, args;
4316 struct c_expr mexpr;
4317 c_parser_consume_token (parser);
4318 if (c_parser_peek_token (parser)->type == CPP_NAME
4319 && ((c_parser_peek_token (parser)->id_kind
4320 == C_ID_TYPENAME)
4321 || (c_parser_peek_token (parser)->id_kind
4322 == C_ID_CLASSNAME)))
4324 /* Type name receiver. */
4325 tree id = c_parser_peek_token (parser)->value;
4326 c_parser_consume_token (parser);
4327 rec = objc_get_class_reference (id);
4328 goto parse_message_args;
4330 first = c_parser_expr_no_commas (parser, NULL).value;
4331 mark_exp_read (first);
4332 if (c_parser_next_token_is (parser, CPP_ELLIPSIS)
4333 || c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
4334 goto array_desig_after_first;
4335 /* Expression receiver. So far only one part
4336 without commas has been parsed; there might be
4337 more of the expression. */
4338 rec = first;
4339 while (c_parser_next_token_is (parser, CPP_COMMA))
4341 struct c_expr next;
4342 location_t comma_loc, exp_loc;
4343 comma_loc = c_parser_peek_token (parser)->location;
4344 c_parser_consume_token (parser);
4345 exp_loc = c_parser_peek_token (parser)->location;
4346 next = c_parser_expr_no_commas (parser, NULL);
4347 next = convert_lvalue_to_rvalue (exp_loc, next,
4348 true, true);
4349 rec = build_compound_expr (comma_loc, rec, next.value);
4351 parse_message_args:
4352 /* Now parse the objc-message-args. */
4353 args = c_parser_objc_message_args (parser);
4354 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
4355 "expected %<]%>");
4356 mexpr.value
4357 = objc_build_message_expr (rec, args);
4358 mexpr.original_code = ERROR_MARK;
4359 mexpr.original_type = NULL;
4360 /* Now parse and process the remainder of the
4361 initializer, starting with this message
4362 expression as a primary-expression. */
4363 c_parser_initval (parser, &mexpr, braced_init_obstack);
4364 return;
4366 c_parser_consume_token (parser);
4367 array_index_loc = c_parser_peek_token (parser)->location;
4368 first = c_parser_expr_no_commas (parser, NULL).value;
4369 mark_exp_read (first);
4370 array_desig_after_first:
4371 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
4373 ellipsis_loc = c_parser_peek_token (parser)->location;
4374 c_parser_consume_token (parser);
4375 second = c_parser_expr_no_commas (parser, NULL).value;
4376 mark_exp_read (second);
4378 else
4379 second = NULL_TREE;
4380 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
4382 c_parser_consume_token (parser);
4383 set_init_index (array_index_loc, first, second,
4384 braced_init_obstack);
4385 if (second)
4386 pedwarn (ellipsis_loc, OPT_Wpedantic,
4387 "ISO C forbids specifying range of elements to initialize");
4389 else
4390 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
4391 "expected %<]%>");
4394 if (des_seen >= 1)
4396 if (c_parser_next_token_is (parser, CPP_EQ))
4398 pedwarn_c90 (des_loc, OPT_Wpedantic,
4399 "ISO C90 forbids specifying subobject "
4400 "to initialize");
4401 c_parser_consume_token (parser);
4403 else
4405 if (des_seen == 1)
4406 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
4407 "obsolete use of designated initializer without %<=%>");
4408 else
4410 struct c_expr init;
4411 init.value = error_mark_node;
4412 init.original_code = ERROR_MARK;
4413 init.original_type = NULL;
4414 c_parser_error (parser, "expected %<=%>");
4415 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
4416 process_init_element (input_location, init, false,
4417 braced_init_obstack);
4418 return;
4423 c_parser_initval (parser, NULL, braced_init_obstack);
4426 /* Parse a nested initializer; as c_parser_initializer but parses
4427 initializers within braced lists, after any designators have been
4428 applied. If AFTER is not NULL then it is an Objective-C message
4429 expression which is the primary-expression starting the
4430 initializer. */
4432 static void
4433 c_parser_initval (c_parser *parser, struct c_expr *after,
4434 struct obstack * braced_init_obstack)
4436 struct c_expr init;
4437 gcc_assert (!after || c_dialect_objc ());
4438 location_t loc = c_parser_peek_token (parser)->location;
4440 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE) && !after)
4441 init = c_parser_braced_init (parser, NULL_TREE, true);
4442 else
4444 init = c_parser_expr_no_commas (parser, after);
4445 if (init.value != NULL_TREE
4446 && TREE_CODE (init.value) != STRING_CST
4447 && TREE_CODE (init.value) != COMPOUND_LITERAL_EXPR)
4448 init = convert_lvalue_to_rvalue (loc, init, true, true);
4450 process_init_element (loc, init, false, braced_init_obstack);
4453 /* Parse a compound statement (possibly a function body) (C90 6.6.2,
4454 C99 6.8.2).
4456 compound-statement:
4457 { block-item-list[opt] }
4458 { label-declarations block-item-list }
4460 block-item-list:
4461 block-item
4462 block-item-list block-item
4464 block-item:
4465 nested-declaration
4466 statement
4468 nested-declaration:
4469 declaration
4471 GNU extensions:
4473 compound-statement:
4474 { label-declarations block-item-list }
4476 nested-declaration:
4477 __extension__ nested-declaration
4478 nested-function-definition
4480 label-declarations:
4481 label-declaration
4482 label-declarations label-declaration
4484 label-declaration:
4485 __label__ identifier-list ;
4487 Allowing the mixing of declarations and code is new in C99. The
4488 GNU syntax also permits (not shown above) labels at the end of
4489 compound statements, which yield an error. We don't allow labels
4490 on declarations; this might seem like a natural extension, but
4491 there would be a conflict between attributes on the label and
4492 prefix attributes on the declaration. ??? The syntax follows the
4493 old parser in requiring something after label declarations.
4494 Although they are erroneous if the labels declared aren't defined,
4495 is it useful for the syntax to be this way?
4497 OpenACC:
4499 block-item:
4500 openacc-directive
4502 openacc-directive:
4503 update-directive
4505 OpenMP:
4507 block-item:
4508 openmp-directive
4510 openmp-directive:
4511 barrier-directive
4512 flush-directive
4513 taskwait-directive
4514 taskyield-directive
4515 cancel-directive
4516 cancellation-point-directive */
4518 static tree
4519 c_parser_compound_statement (c_parser *parser)
4521 tree stmt;
4522 location_t brace_loc;
4523 brace_loc = c_parser_peek_token (parser)->location;
4524 if (!c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
4526 /* Ensure a scope is entered and left anyway to avoid confusion
4527 if we have just prepared to enter a function body. */
4528 stmt = c_begin_compound_stmt (true);
4529 c_end_compound_stmt (brace_loc, stmt, true);
4530 return error_mark_node;
4532 stmt = c_begin_compound_stmt (true);
4533 c_parser_compound_statement_nostart (parser);
4535 /* If the compound stmt contains array notations, then we expand them. */
4536 if (flag_cilkplus && contains_array_notation_expr (stmt))
4537 stmt = expand_array_notation_exprs (stmt);
4538 return c_end_compound_stmt (brace_loc, stmt, true);
4541 /* Parse a compound statement except for the opening brace. This is
4542 used for parsing both compound statements and statement expressions
4543 (which follow different paths to handling the opening). */
4545 static void
4546 c_parser_compound_statement_nostart (c_parser *parser)
4548 bool last_stmt = false;
4549 bool last_label = false;
4550 bool save_valid_for_pragma = valid_location_for_stdc_pragma_p ();
4551 location_t label_loc = UNKNOWN_LOCATION; /* Quiet warning. */
4552 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
4554 c_parser_consume_token (parser);
4555 return;
4557 mark_valid_location_for_stdc_pragma (true);
4558 if (c_parser_next_token_is_keyword (parser, RID_LABEL))
4560 /* Read zero or more forward-declarations for labels that nested
4561 functions can jump to. */
4562 mark_valid_location_for_stdc_pragma (false);
4563 while (c_parser_next_token_is_keyword (parser, RID_LABEL))
4565 label_loc = c_parser_peek_token (parser)->location;
4566 c_parser_consume_token (parser);
4567 /* Any identifiers, including those declared as type names,
4568 are OK here. */
4569 while (true)
4571 tree label;
4572 if (c_parser_next_token_is_not (parser, CPP_NAME))
4574 c_parser_error (parser, "expected identifier");
4575 break;
4577 label
4578 = declare_label (c_parser_peek_token (parser)->value);
4579 C_DECLARED_LABEL_FLAG (label) = 1;
4580 add_stmt (build_stmt (label_loc, DECL_EXPR, label));
4581 c_parser_consume_token (parser);
4582 if (c_parser_next_token_is (parser, CPP_COMMA))
4583 c_parser_consume_token (parser);
4584 else
4585 break;
4587 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
4589 pedwarn (label_loc, OPT_Wpedantic, "ISO C forbids label declarations");
4591 /* We must now have at least one statement, label or declaration. */
4592 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
4594 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
4595 c_parser_error (parser, "expected declaration or statement");
4596 c_parser_consume_token (parser);
4597 return;
4599 while (c_parser_next_token_is_not (parser, CPP_CLOSE_BRACE))
4601 location_t loc = c_parser_peek_token (parser)->location;
4602 if (c_parser_next_token_is_keyword (parser, RID_CASE)
4603 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
4604 || (c_parser_next_token_is (parser, CPP_NAME)
4605 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
4607 if (c_parser_next_token_is_keyword (parser, RID_CASE))
4608 label_loc = c_parser_peek_2nd_token (parser)->location;
4609 else
4610 label_loc = c_parser_peek_token (parser)->location;
4611 last_label = true;
4612 last_stmt = false;
4613 mark_valid_location_for_stdc_pragma (false);
4614 c_parser_label (parser);
4616 else if (!last_label
4617 && c_parser_next_tokens_start_declaration (parser))
4619 last_label = false;
4620 mark_valid_location_for_stdc_pragma (false);
4621 c_parser_declaration_or_fndef (parser, true, true, true, true,
4622 true, NULL, vNULL);
4623 if (last_stmt)
4624 pedwarn_c90 (loc, OPT_Wdeclaration_after_statement,
4625 "ISO C90 forbids mixed declarations and code");
4626 last_stmt = false;
4628 else if (!last_label
4629 && c_parser_next_token_is_keyword (parser, RID_EXTENSION))
4631 /* __extension__ can start a declaration, but is also an
4632 unary operator that can start an expression. Consume all
4633 but the last of a possible series of __extension__ to
4634 determine which. */
4635 while (c_parser_peek_2nd_token (parser)->type == CPP_KEYWORD
4636 && (c_parser_peek_2nd_token (parser)->keyword
4637 == RID_EXTENSION))
4638 c_parser_consume_token (parser);
4639 if (c_token_starts_declaration (c_parser_peek_2nd_token (parser)))
4641 int ext;
4642 ext = disable_extension_diagnostics ();
4643 c_parser_consume_token (parser);
4644 last_label = false;
4645 mark_valid_location_for_stdc_pragma (false);
4646 c_parser_declaration_or_fndef (parser, true, true, true, true,
4647 true, NULL, vNULL);
4648 /* Following the old parser, __extension__ does not
4649 disable this diagnostic. */
4650 restore_extension_diagnostics (ext);
4651 if (last_stmt)
4652 pedwarn_c90 (loc, OPT_Wdeclaration_after_statement,
4653 "ISO C90 forbids mixed declarations and code");
4654 last_stmt = false;
4656 else
4657 goto statement;
4659 else if (c_parser_next_token_is (parser, CPP_PRAGMA))
4661 /* External pragmas, and some omp pragmas, are not associated
4662 with regular c code, and so are not to be considered statements
4663 syntactically. This ensures that the user doesn't put them
4664 places that would turn into syntax errors if the directive
4665 were ignored. */
4666 if (c_parser_pragma (parser, pragma_compound))
4667 last_label = false, last_stmt = true;
4669 else if (c_parser_next_token_is (parser, CPP_EOF))
4671 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
4672 c_parser_error (parser, "expected declaration or statement");
4673 return;
4675 else if (c_parser_next_token_is_keyword (parser, RID_ELSE))
4677 if (parser->in_if_block)
4679 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
4680 error_at (loc, """expected %<}%> before %<else%>");
4681 return;
4683 else
4685 error_at (loc, "%<else%> without a previous %<if%>");
4686 c_parser_consume_token (parser);
4687 continue;
4690 else
4692 statement:
4693 last_label = false;
4694 last_stmt = true;
4695 mark_valid_location_for_stdc_pragma (false);
4696 c_parser_statement_after_labels (parser);
4699 parser->error = false;
4701 if (last_label)
4702 error_at (label_loc, "label at end of compound statement");
4703 c_parser_consume_token (parser);
4704 /* Restore the value we started with. */
4705 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
4708 /* Parse all consecutive labels. */
4710 static void
4711 c_parser_all_labels (c_parser *parser)
4713 while (c_parser_next_token_is_keyword (parser, RID_CASE)
4714 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
4715 || (c_parser_next_token_is (parser, CPP_NAME)
4716 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
4717 c_parser_label (parser);
4720 /* Parse a label (C90 6.6.1, C99 6.8.1).
4722 label:
4723 identifier : attributes[opt]
4724 case constant-expression :
4725 default :
4727 GNU extensions:
4729 label:
4730 case constant-expression ... constant-expression :
4732 The use of attributes on labels is a GNU extension. The syntax in
4733 GNU C accepts any expressions without commas, non-constant
4734 expressions being rejected later. */
4736 static void
4737 c_parser_label (c_parser *parser)
4739 location_t loc1 = c_parser_peek_token (parser)->location;
4740 tree label = NULL_TREE;
4741 if (c_parser_next_token_is_keyword (parser, RID_CASE))
4743 tree exp1, exp2;
4744 c_parser_consume_token (parser);
4745 exp1 = c_parser_expr_no_commas (parser, NULL).value;
4746 if (c_parser_next_token_is (parser, CPP_COLON))
4748 c_parser_consume_token (parser);
4749 label = do_case (loc1, exp1, NULL_TREE);
4751 else if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
4753 c_parser_consume_token (parser);
4754 exp2 = c_parser_expr_no_commas (parser, NULL).value;
4755 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
4756 label = do_case (loc1, exp1, exp2);
4758 else
4759 c_parser_error (parser, "expected %<:%> or %<...%>");
4761 else if (c_parser_next_token_is_keyword (parser, RID_DEFAULT))
4763 c_parser_consume_token (parser);
4764 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
4765 label = do_case (loc1, NULL_TREE, NULL_TREE);
4767 else
4769 tree name = c_parser_peek_token (parser)->value;
4770 tree tlab;
4771 tree attrs;
4772 location_t loc2 = c_parser_peek_token (parser)->location;
4773 gcc_assert (c_parser_next_token_is (parser, CPP_NAME));
4774 c_parser_consume_token (parser);
4775 gcc_assert (c_parser_next_token_is (parser, CPP_COLON));
4776 c_parser_consume_token (parser);
4777 attrs = c_parser_attributes (parser);
4778 tlab = define_label (loc2, name);
4779 if (tlab)
4781 decl_attributes (&tlab, attrs, 0);
4782 label = add_stmt (build_stmt (loc1, LABEL_EXPR, tlab));
4785 if (label)
4787 if (c_parser_next_tokens_start_declaration (parser))
4789 error_at (c_parser_peek_token (parser)->location,
4790 "a label can only be part of a statement and "
4791 "a declaration is not a statement");
4792 c_parser_declaration_or_fndef (parser, /*fndef_ok*/ false,
4793 /*static_assert_ok*/ true,
4794 /*empty_ok*/ true, /*nested*/ true,
4795 /*start_attr_ok*/ true, NULL,
4796 vNULL);
4801 /* Parse a statement (C90 6.6, C99 6.8).
4803 statement:
4804 labeled-statement
4805 compound-statement
4806 expression-statement
4807 selection-statement
4808 iteration-statement
4809 jump-statement
4811 labeled-statement:
4812 label statement
4814 expression-statement:
4815 expression[opt] ;
4817 selection-statement:
4818 if-statement
4819 switch-statement
4821 iteration-statement:
4822 while-statement
4823 do-statement
4824 for-statement
4826 jump-statement:
4827 goto identifier ;
4828 continue ;
4829 break ;
4830 return expression[opt] ;
4832 GNU extensions:
4834 statement:
4835 asm-statement
4837 jump-statement:
4838 goto * expression ;
4840 Objective-C:
4842 statement:
4843 objc-throw-statement
4844 objc-try-catch-statement
4845 objc-synchronized-statement
4847 objc-throw-statement:
4848 @throw expression ;
4849 @throw ;
4851 OpenACC:
4853 statement:
4854 openacc-construct
4856 openacc-construct:
4857 parallel-construct
4858 kernels-construct
4859 data-construct
4860 loop-construct
4862 parallel-construct:
4863 parallel-directive structured-block
4865 kernels-construct:
4866 kernels-directive structured-block
4868 data-construct:
4869 data-directive structured-block
4871 loop-construct:
4872 loop-directive structured-block
4874 OpenMP:
4876 statement:
4877 openmp-construct
4879 openmp-construct:
4880 parallel-construct
4881 for-construct
4882 simd-construct
4883 for-simd-construct
4884 sections-construct
4885 single-construct
4886 parallel-for-construct
4887 parallel-for-simd-construct
4888 parallel-sections-construct
4889 master-construct
4890 critical-construct
4891 atomic-construct
4892 ordered-construct
4894 parallel-construct:
4895 parallel-directive structured-block
4897 for-construct:
4898 for-directive iteration-statement
4900 simd-construct:
4901 simd-directive iteration-statements
4903 for-simd-construct:
4904 for-simd-directive iteration-statements
4906 sections-construct:
4907 sections-directive section-scope
4909 single-construct:
4910 single-directive structured-block
4912 parallel-for-construct:
4913 parallel-for-directive iteration-statement
4915 parallel-for-simd-construct:
4916 parallel-for-simd-directive iteration-statement
4918 parallel-sections-construct:
4919 parallel-sections-directive section-scope
4921 master-construct:
4922 master-directive structured-block
4924 critical-construct:
4925 critical-directive structured-block
4927 atomic-construct:
4928 atomic-directive expression-statement
4930 ordered-construct:
4931 ordered-directive structured-block
4933 Transactional Memory:
4935 statement:
4936 transaction-statement
4937 transaction-cancel-statement
4940 static void
4941 c_parser_statement (c_parser *parser)
4943 c_parser_all_labels (parser);
4944 c_parser_statement_after_labels (parser);
4947 /* Parse a statement, other than a labeled statement. */
4949 static void
4950 c_parser_statement_after_labels (c_parser *parser)
4952 location_t loc = c_parser_peek_token (parser)->location;
4953 tree stmt = NULL_TREE;
4954 bool in_if_block = parser->in_if_block;
4955 parser->in_if_block = false;
4956 switch (c_parser_peek_token (parser)->type)
4958 case CPP_OPEN_BRACE:
4959 add_stmt (c_parser_compound_statement (parser));
4960 break;
4961 case CPP_KEYWORD:
4962 switch (c_parser_peek_token (parser)->keyword)
4964 case RID_IF:
4965 c_parser_if_statement (parser);
4966 break;
4967 case RID_SWITCH:
4968 c_parser_switch_statement (parser);
4969 break;
4970 case RID_WHILE:
4971 c_parser_while_statement (parser, false);
4972 break;
4973 case RID_DO:
4974 c_parser_do_statement (parser, false);
4975 break;
4976 case RID_FOR:
4977 c_parser_for_statement (parser, false);
4978 break;
4979 case RID_CILK_FOR:
4980 if (!flag_cilkplus)
4982 error_at (c_parser_peek_token (parser)->location,
4983 "-fcilkplus must be enabled to use %<_Cilk_for%>");
4984 c_parser_skip_to_end_of_block_or_statement (parser);
4986 else
4987 c_parser_cilk_for (parser, integer_zero_node);
4988 break;
4989 case RID_CILK_SYNC:
4990 c_parser_consume_token (parser);
4991 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
4992 if (!flag_cilkplus)
4993 error_at (loc, "-fcilkplus must be enabled to use %<_Cilk_sync%>");
4994 else
4995 add_stmt (build_cilk_sync ());
4996 break;
4997 case RID_GOTO:
4998 c_parser_consume_token (parser);
4999 if (c_parser_next_token_is (parser, CPP_NAME))
5001 stmt = c_finish_goto_label (loc,
5002 c_parser_peek_token (parser)->value);
5003 c_parser_consume_token (parser);
5005 else if (c_parser_next_token_is (parser, CPP_MULT))
5007 struct c_expr val;
5009 c_parser_consume_token (parser);
5010 val = c_parser_expression (parser);
5011 if (check_no_cilk (val.value,
5012 "Cilk array notation cannot be used as a computed goto expression",
5013 "%<_Cilk_spawn%> statement cannot be used as a computed goto expression",
5014 loc))
5015 val.value = error_mark_node;
5016 val = convert_lvalue_to_rvalue (loc, val, false, true);
5017 stmt = c_finish_goto_ptr (loc, val.value);
5019 else
5020 c_parser_error (parser, "expected identifier or %<*%>");
5021 goto expect_semicolon;
5022 case RID_CONTINUE:
5023 c_parser_consume_token (parser);
5024 stmt = c_finish_bc_stmt (loc, &c_cont_label, false);
5025 goto expect_semicolon;
5026 case RID_BREAK:
5027 c_parser_consume_token (parser);
5028 stmt = c_finish_bc_stmt (loc, &c_break_label, true);
5029 goto expect_semicolon;
5030 case RID_RETURN:
5031 c_parser_consume_token (parser);
5032 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5034 stmt = c_finish_return (loc, NULL_TREE, NULL_TREE);
5035 c_parser_consume_token (parser);
5037 else
5039 location_t xloc = c_parser_peek_token (parser)->location;
5040 struct c_expr expr = c_parser_expression_conv (parser);
5041 mark_exp_read (expr.value);
5042 stmt = c_finish_return (xloc, expr.value, expr.original_type);
5043 goto expect_semicolon;
5045 break;
5046 case RID_ASM:
5047 stmt = c_parser_asm_statement (parser);
5048 break;
5049 case RID_TRANSACTION_ATOMIC:
5050 case RID_TRANSACTION_RELAXED:
5051 stmt = c_parser_transaction (parser,
5052 c_parser_peek_token (parser)->keyword);
5053 break;
5054 case RID_TRANSACTION_CANCEL:
5055 stmt = c_parser_transaction_cancel (parser);
5056 goto expect_semicolon;
5057 case RID_AT_THROW:
5058 gcc_assert (c_dialect_objc ());
5059 c_parser_consume_token (parser);
5060 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5062 stmt = objc_build_throw_stmt (loc, NULL_TREE);
5063 c_parser_consume_token (parser);
5065 else
5067 struct c_expr expr = c_parser_expression (parser);
5068 expr = convert_lvalue_to_rvalue (loc, expr, false, false);
5069 if (check_no_cilk (expr.value,
5070 "Cilk array notation cannot be used for a throw expression",
5071 "%<_Cilk_spawn%> statement cannot be used for a throw expression"))
5072 expr.value = error_mark_node;
5073 else
5075 expr.value = c_fully_fold (expr.value, false, NULL);
5076 stmt = objc_build_throw_stmt (loc, expr.value);
5078 goto expect_semicolon;
5080 break;
5081 case RID_AT_TRY:
5082 gcc_assert (c_dialect_objc ());
5083 c_parser_objc_try_catch_finally_statement (parser);
5084 break;
5085 case RID_AT_SYNCHRONIZED:
5086 gcc_assert (c_dialect_objc ());
5087 c_parser_objc_synchronized_statement (parser);
5088 break;
5089 default:
5090 goto expr_stmt;
5092 break;
5093 case CPP_SEMICOLON:
5094 c_parser_consume_token (parser);
5095 break;
5096 case CPP_CLOSE_PAREN:
5097 case CPP_CLOSE_SQUARE:
5098 /* Avoid infinite loop in error recovery:
5099 c_parser_skip_until_found stops at a closing nesting
5100 delimiter without consuming it, but here we need to consume
5101 it to proceed further. */
5102 c_parser_error (parser, "expected statement");
5103 c_parser_consume_token (parser);
5104 break;
5105 case CPP_PRAGMA:
5106 c_parser_pragma (parser, pragma_stmt);
5107 break;
5108 default:
5109 expr_stmt:
5110 stmt = c_finish_expr_stmt (loc, c_parser_expression_conv (parser).value);
5111 expect_semicolon:
5112 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
5113 break;
5115 /* Two cases cannot and do not have line numbers associated: If stmt
5116 is degenerate, such as "2;", then stmt is an INTEGER_CST, which
5117 cannot hold line numbers. But that's OK because the statement
5118 will either be changed to a MODIFY_EXPR during gimplification of
5119 the statement expr, or discarded. If stmt was compound, but
5120 without new variables, we will have skipped the creation of a
5121 BIND and will have a bare STATEMENT_LIST. But that's OK because
5122 (recursively) all of the component statements should already have
5123 line numbers assigned. ??? Can we discard no-op statements
5124 earlier? */
5125 if (CAN_HAVE_LOCATION_P (stmt)
5126 && EXPR_LOCATION (stmt) == UNKNOWN_LOCATION)
5127 SET_EXPR_LOCATION (stmt, loc);
5129 parser->in_if_block = in_if_block;
5132 /* Parse the condition from an if, do, while or for statements. */
5134 static tree
5135 c_parser_condition (c_parser *parser)
5137 location_t loc = c_parser_peek_token (parser)->location;
5138 tree cond;
5139 cond = c_parser_expression_conv (parser).value;
5140 cond = c_objc_common_truthvalue_conversion (loc, cond);
5141 cond = c_fully_fold (cond, false, NULL);
5142 if (warn_sequence_point)
5143 verify_sequence_points (cond);
5144 return cond;
5147 /* Parse a parenthesized condition from an if, do or while statement.
5149 condition:
5150 ( expression )
5152 static tree
5153 c_parser_paren_condition (c_parser *parser)
5155 tree cond;
5156 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5157 return error_mark_node;
5158 cond = c_parser_condition (parser);
5159 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
5160 return cond;
5163 /* Parse a statement which is a block in C99. */
5165 static tree
5166 c_parser_c99_block_statement (c_parser *parser)
5168 tree block = c_begin_compound_stmt (flag_isoc99);
5169 location_t loc = c_parser_peek_token (parser)->location;
5170 c_parser_statement (parser);
5171 return c_end_compound_stmt (loc, block, flag_isoc99);
5174 /* Parse the body of an if statement. This is just parsing a
5175 statement but (a) it is a block in C99, (b) we track whether the
5176 body is an if statement for the sake of -Wparentheses warnings, (c)
5177 we handle an empty body specially for the sake of -Wempty-body
5178 warnings, and (d) we call parser_compound_statement directly
5179 because c_parser_statement_after_labels resets
5180 parser->in_if_block. */
5182 static tree
5183 c_parser_if_body (c_parser *parser, bool *if_p, location_t if_loc)
5185 tree block = c_begin_compound_stmt (flag_isoc99);
5186 location_t body_loc = c_parser_peek_token (parser)->location;
5187 c_parser_all_labels (parser);
5188 *if_p = c_parser_next_token_is_keyword (parser, RID_IF);
5189 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5191 location_t loc = c_parser_peek_token (parser)->location;
5192 add_stmt (build_empty_stmt (loc));
5193 c_parser_consume_token (parser);
5194 if (!c_parser_next_token_is_keyword (parser, RID_ELSE))
5195 warning_at (loc, OPT_Wempty_body,
5196 "suggest braces around empty body in an %<if%> statement");
5198 else if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
5199 add_stmt (c_parser_compound_statement (parser));
5200 else
5202 c_parser_statement_after_labels (parser);
5203 if (!c_parser_next_token_is_keyword (parser, RID_ELSE))
5204 warn_for_misleading_indentation (if_loc, body_loc,
5205 c_parser_peek_token (parser)->location,
5206 c_parser_peek_token (parser)->type,
5207 "if");
5210 return c_end_compound_stmt (body_loc, block, flag_isoc99);
5213 /* Parse the else body of an if statement. This is just parsing a
5214 statement but (a) it is a block in C99, (b) we handle an empty body
5215 specially for the sake of -Wempty-body warnings. */
5217 static tree
5218 c_parser_else_body (c_parser *parser, location_t else_loc)
5220 location_t body_loc = c_parser_peek_token (parser)->location;
5221 tree block = c_begin_compound_stmt (flag_isoc99);
5222 c_parser_all_labels (parser);
5223 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5225 location_t loc = c_parser_peek_token (parser)->location;
5226 warning_at (loc,
5227 OPT_Wempty_body,
5228 "suggest braces around empty body in an %<else%> statement");
5229 add_stmt (build_empty_stmt (loc));
5230 c_parser_consume_token (parser);
5232 else
5234 c_parser_statement_after_labels (parser);
5235 warn_for_misleading_indentation (else_loc, body_loc,
5236 c_parser_peek_token (parser)->location,
5237 c_parser_peek_token (parser)->type,
5238 "else");
5240 return c_end_compound_stmt (body_loc, block, flag_isoc99);
5243 /* Parse an if statement (C90 6.6.4, C99 6.8.4).
5245 if-statement:
5246 if ( expression ) statement
5247 if ( expression ) statement else statement
5250 static void
5251 c_parser_if_statement (c_parser *parser)
5253 tree block;
5254 location_t loc;
5255 tree cond;
5256 bool first_if = false;
5257 tree first_body, second_body;
5258 bool in_if_block;
5259 tree if_stmt;
5261 gcc_assert (c_parser_next_token_is_keyword (parser, RID_IF));
5262 location_t if_loc = c_parser_peek_token (parser)->location;
5263 c_parser_consume_token (parser);
5264 block = c_begin_compound_stmt (flag_isoc99);
5265 loc = c_parser_peek_token (parser)->location;
5266 cond = c_parser_paren_condition (parser);
5267 if (flag_cilkplus && contains_cilk_spawn_stmt (cond))
5269 error_at (loc, "if statement cannot contain %<Cilk_spawn%>");
5270 cond = error_mark_node;
5272 in_if_block = parser->in_if_block;
5273 parser->in_if_block = true;
5274 first_body = c_parser_if_body (parser, &first_if, if_loc);
5275 parser->in_if_block = in_if_block;
5276 if (c_parser_next_token_is_keyword (parser, RID_ELSE))
5278 location_t else_loc = c_parser_peek_token (parser)->location;
5279 c_parser_consume_token (parser);
5280 second_body = c_parser_else_body (parser, else_loc);
5282 else
5283 second_body = NULL_TREE;
5284 c_finish_if_stmt (loc, cond, first_body, second_body, first_if);
5285 if_stmt = c_end_compound_stmt (loc, block, flag_isoc99);
5287 /* If the if statement contains array notations, then we expand them. */
5288 if (flag_cilkplus && contains_array_notation_expr (if_stmt))
5289 if_stmt = fix_conditional_array_notations (if_stmt);
5290 add_stmt (if_stmt);
5293 /* Parse a switch statement (C90 6.6.4, C99 6.8.4).
5295 switch-statement:
5296 switch (expression) statement
5299 static void
5300 c_parser_switch_statement (c_parser *parser)
5302 struct c_expr ce;
5303 tree block, expr, body, save_break;
5304 location_t switch_loc = c_parser_peek_token (parser)->location;
5305 location_t switch_cond_loc;
5306 gcc_assert (c_parser_next_token_is_keyword (parser, RID_SWITCH));
5307 c_parser_consume_token (parser);
5308 block = c_begin_compound_stmt (flag_isoc99);
5309 bool explicit_cast_p = false;
5310 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5312 switch_cond_loc = c_parser_peek_token (parser)->location;
5313 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
5314 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
5315 explicit_cast_p = true;
5316 ce = c_parser_expression (parser);
5317 ce = convert_lvalue_to_rvalue (switch_cond_loc, ce, true, false);
5318 expr = ce.value;
5319 /* ??? expr has no valid location? */
5320 if (check_no_cilk (expr,
5321 "Cilk array notation cannot be used as a condition for switch statement",
5322 "%<_Cilk_spawn%> statement cannot be used as a condition for switch statement",
5323 switch_cond_loc))
5324 expr = error_mark_node;
5325 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
5327 else
5329 switch_cond_loc = UNKNOWN_LOCATION;
5330 expr = error_mark_node;
5332 c_start_case (switch_loc, switch_cond_loc, expr, explicit_cast_p);
5333 save_break = c_break_label;
5334 c_break_label = NULL_TREE;
5335 body = c_parser_c99_block_statement (parser);
5336 c_finish_case (body, ce.original_type);
5337 if (c_break_label)
5339 location_t here = c_parser_peek_token (parser)->location;
5340 tree t = build1 (LABEL_EXPR, void_type_node, c_break_label);
5341 SET_EXPR_LOCATION (t, here);
5342 add_stmt (t);
5344 c_break_label = save_break;
5345 add_stmt (c_end_compound_stmt (switch_loc, block, flag_isoc99));
5348 /* Parse a while statement (C90 6.6.5, C99 6.8.5).
5350 while-statement:
5351 while (expression) statement
5354 static void
5355 c_parser_while_statement (c_parser *parser, bool ivdep)
5357 tree block, cond, body, save_break, save_cont;
5358 location_t loc;
5359 gcc_assert (c_parser_next_token_is_keyword (parser, RID_WHILE));
5360 location_t while_loc = c_parser_peek_token (parser)->location;
5361 c_parser_consume_token (parser);
5362 block = c_begin_compound_stmt (flag_isoc99);
5363 loc = c_parser_peek_token (parser)->location;
5364 cond = c_parser_paren_condition (parser);
5365 if (check_no_cilk (cond,
5366 "Cilk array notation cannot be used as a condition for while statement",
5367 "%<_Cilk_spawn%> statement cannot be used as a condition for while statement"))
5368 cond = error_mark_node;
5369 if (ivdep && cond != error_mark_node)
5370 cond = build2 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
5371 build_int_cst (integer_type_node,
5372 annot_expr_ivdep_kind));
5373 save_break = c_break_label;
5374 c_break_label = NULL_TREE;
5375 save_cont = c_cont_label;
5376 c_cont_label = NULL_TREE;
5378 location_t body_loc = UNKNOWN_LOCATION;
5379 if (c_parser_peek_token (parser)->type != CPP_OPEN_BRACE)
5380 body_loc = c_parser_peek_token (parser)->location;
5381 body = c_parser_c99_block_statement (parser);
5382 warn_for_misleading_indentation (while_loc, body_loc,
5383 c_parser_peek_token (parser)->location,
5384 c_parser_peek_token (parser)->type,
5385 "while");
5387 c_finish_loop (loc, cond, NULL, body, c_break_label, c_cont_label, true);
5388 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99));
5389 c_break_label = save_break;
5390 c_cont_label = save_cont;
5393 /* Parse a do statement (C90 6.6.5, C99 6.8.5).
5395 do-statement:
5396 do statement while ( expression ) ;
5399 static void
5400 c_parser_do_statement (c_parser *parser, bool ivdep)
5402 tree block, cond, body, save_break, save_cont, new_break, new_cont;
5403 location_t loc;
5404 gcc_assert (c_parser_next_token_is_keyword (parser, RID_DO));
5405 c_parser_consume_token (parser);
5406 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5407 warning_at (c_parser_peek_token (parser)->location,
5408 OPT_Wempty_body,
5409 "suggest braces around empty body in %<do%> statement");
5410 block = c_begin_compound_stmt (flag_isoc99);
5411 loc = c_parser_peek_token (parser)->location;
5412 save_break = c_break_label;
5413 c_break_label = NULL_TREE;
5414 save_cont = c_cont_label;
5415 c_cont_label = NULL_TREE;
5416 body = c_parser_c99_block_statement (parser);
5417 c_parser_require_keyword (parser, RID_WHILE, "expected %<while%>");
5418 new_break = c_break_label;
5419 c_break_label = save_break;
5420 new_cont = c_cont_label;
5421 c_cont_label = save_cont;
5422 cond = c_parser_paren_condition (parser);
5423 if (check_no_cilk (cond,
5424 "Cilk array notation cannot be used as a condition for a do-while statement",
5425 "%<_Cilk_spawn%> statement cannot be used as a condition for a do-while statement"))
5426 cond = error_mark_node;
5427 if (ivdep && cond != error_mark_node)
5428 cond = build2 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
5429 build_int_cst (integer_type_node,
5430 annot_expr_ivdep_kind));
5431 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
5432 c_parser_skip_to_end_of_block_or_statement (parser);
5433 c_finish_loop (loc, cond, NULL, body, new_break, new_cont, false);
5434 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99));
5437 /* Parse a for statement (C90 6.6.5, C99 6.8.5).
5439 for-statement:
5440 for ( expression[opt] ; expression[opt] ; expression[opt] ) statement
5441 for ( nested-declaration expression[opt] ; expression[opt] ) statement
5443 The form with a declaration is new in C99.
5445 ??? In accordance with the old parser, the declaration may be a
5446 nested function, which is then rejected in check_for_loop_decls,
5447 but does it make any sense for this to be included in the grammar?
5448 Note in particular that the nested function does not include a
5449 trailing ';', whereas the "declaration" production includes one.
5450 Also, can we reject bad declarations earlier and cheaper than
5451 check_for_loop_decls?
5453 In Objective-C, there are two additional variants:
5455 foreach-statement:
5456 for ( expression in expresssion ) statement
5457 for ( declaration in expression ) statement
5459 This is inconsistent with C, because the second variant is allowed
5460 even if c99 is not enabled.
5462 The rest of the comment documents these Objective-C foreach-statement.
5464 Here is the canonical example of the first variant:
5465 for (object in array) { do something with object }
5466 we call the first expression ("object") the "object_expression" and
5467 the second expression ("array") the "collection_expression".
5468 object_expression must be an lvalue of type "id" (a generic Objective-C
5469 object) because the loop works by assigning to object_expression the
5470 various objects from the collection_expression. collection_expression
5471 must evaluate to something of type "id" which responds to the method
5472 countByEnumeratingWithState:objects:count:.
5474 The canonical example of the second variant is:
5475 for (id object in array) { do something with object }
5476 which is completely equivalent to
5478 id object;
5479 for (object in array) { do something with object }
5481 Note that initizializing 'object' in some way (eg, "for ((object =
5482 xxx) in array) { do something with object }") is possibly
5483 technically valid, but completely pointless as 'object' will be
5484 assigned to something else as soon as the loop starts. We should
5485 most likely reject it (TODO).
5487 The beginning of the Objective-C foreach-statement looks exactly
5488 like the beginning of the for-statement, and we can tell it is a
5489 foreach-statement only because the initial declaration or
5490 expression is terminated by 'in' instead of ';'.
5493 static void
5494 c_parser_for_statement (c_parser *parser, bool ivdep)
5496 tree block, cond, incr, save_break, save_cont, body;
5497 /* The following are only used when parsing an ObjC foreach statement. */
5498 tree object_expression;
5499 /* Silence the bogus uninitialized warning. */
5500 tree collection_expression = NULL;
5501 location_t loc = c_parser_peek_token (parser)->location;
5502 location_t for_loc = c_parser_peek_token (parser)->location;
5503 bool is_foreach_statement = false;
5504 gcc_assert (c_parser_next_token_is_keyword (parser, RID_FOR));
5505 c_parser_consume_token (parser);
5506 /* Open a compound statement in Objective-C as well, just in case this is
5507 as foreach expression. */
5508 block = c_begin_compound_stmt (flag_isoc99 || c_dialect_objc ());
5509 cond = error_mark_node;
5510 incr = error_mark_node;
5511 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5513 /* Parse the initialization declaration or expression. */
5514 object_expression = error_mark_node;
5515 parser->objc_could_be_foreach_context = c_dialect_objc ();
5516 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5518 parser->objc_could_be_foreach_context = false;
5519 c_parser_consume_token (parser);
5520 c_finish_expr_stmt (loc, NULL_TREE);
5522 else if (c_parser_next_tokens_start_declaration (parser))
5524 c_parser_declaration_or_fndef (parser, true, true, true, true, true,
5525 &object_expression, vNULL);
5526 parser->objc_could_be_foreach_context = false;
5528 if (c_parser_next_token_is_keyword (parser, RID_IN))
5530 c_parser_consume_token (parser);
5531 is_foreach_statement = true;
5532 if (check_for_loop_decls (for_loc, true) == NULL_TREE)
5533 c_parser_error (parser, "multiple iterating variables in fast enumeration");
5535 else
5536 check_for_loop_decls (for_loc, flag_isoc99);
5538 else if (c_parser_next_token_is_keyword (parser, RID_EXTENSION))
5540 /* __extension__ can start a declaration, but is also an
5541 unary operator that can start an expression. Consume all
5542 but the last of a possible series of __extension__ to
5543 determine which. */
5544 while (c_parser_peek_2nd_token (parser)->type == CPP_KEYWORD
5545 && (c_parser_peek_2nd_token (parser)->keyword
5546 == RID_EXTENSION))
5547 c_parser_consume_token (parser);
5548 if (c_token_starts_declaration (c_parser_peek_2nd_token (parser)))
5550 int ext;
5551 ext = disable_extension_diagnostics ();
5552 c_parser_consume_token (parser);
5553 c_parser_declaration_or_fndef (parser, true, true, true, true,
5554 true, &object_expression, vNULL);
5555 parser->objc_could_be_foreach_context = false;
5557 restore_extension_diagnostics (ext);
5558 if (c_parser_next_token_is_keyword (parser, RID_IN))
5560 c_parser_consume_token (parser);
5561 is_foreach_statement = true;
5562 if (check_for_loop_decls (for_loc, true) == NULL_TREE)
5563 c_parser_error (parser, "multiple iterating variables in fast enumeration");
5565 else
5566 check_for_loop_decls (for_loc, flag_isoc99);
5568 else
5569 goto init_expr;
5571 else
5573 init_expr:
5575 struct c_expr ce;
5576 tree init_expression;
5577 ce = c_parser_expression (parser);
5578 /* In theory we could forbid _Cilk_spawn here, as the spec says "only in top
5579 level statement", but it works just fine, so allow it. */
5580 init_expression = ce.value;
5581 parser->objc_could_be_foreach_context = false;
5582 if (c_parser_next_token_is_keyword (parser, RID_IN))
5584 c_parser_consume_token (parser);
5585 is_foreach_statement = true;
5586 if (! lvalue_p (init_expression))
5587 c_parser_error (parser, "invalid iterating variable in fast enumeration");
5588 object_expression = c_fully_fold (init_expression, false, NULL);
5590 else
5592 ce = convert_lvalue_to_rvalue (loc, ce, true, false);
5593 init_expression = ce.value;
5594 c_finish_expr_stmt (loc, init_expression);
5595 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
5599 /* Parse the loop condition. In the case of a foreach
5600 statement, there is no loop condition. */
5601 gcc_assert (!parser->objc_could_be_foreach_context);
5602 if (!is_foreach_statement)
5604 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5606 if (ivdep)
5608 c_parser_error (parser, "missing loop condition in loop with "
5609 "%<GCC ivdep%> pragma");
5610 cond = error_mark_node;
5612 else
5614 c_parser_consume_token (parser);
5615 cond = NULL_TREE;
5618 else
5620 cond = c_parser_condition (parser);
5621 if (check_no_cilk (cond,
5622 "Cilk array notation cannot be used in a condition for a for-loop",
5623 "%<_Cilk_spawn%> statement cannot be used in a condition for a for-loop"))
5624 cond = error_mark_node;
5625 c_parser_skip_until_found (parser, CPP_SEMICOLON,
5626 "expected %<;%>");
5628 if (ivdep && cond != error_mark_node)
5629 cond = build2 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
5630 build_int_cst (integer_type_node,
5631 annot_expr_ivdep_kind));
5633 /* Parse the increment expression (the third expression in a
5634 for-statement). In the case of a foreach-statement, this is
5635 the expression that follows the 'in'. */
5636 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
5638 if (is_foreach_statement)
5640 c_parser_error (parser, "missing collection in fast enumeration");
5641 collection_expression = error_mark_node;
5643 else
5644 incr = c_process_expr_stmt (loc, NULL_TREE);
5646 else
5648 if (is_foreach_statement)
5649 collection_expression = c_fully_fold (c_parser_expression (parser).value,
5650 false, NULL);
5651 else
5653 struct c_expr ce = c_parser_expression (parser);
5654 ce = convert_lvalue_to_rvalue (loc, ce, true, false);
5655 incr = c_process_expr_stmt (loc, ce.value);
5658 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
5660 save_break = c_break_label;
5661 c_break_label = NULL_TREE;
5662 save_cont = c_cont_label;
5663 c_cont_label = NULL_TREE;
5665 location_t body_loc = UNKNOWN_LOCATION;
5666 if (c_parser_peek_token (parser)->type != CPP_OPEN_BRACE)
5667 body_loc = c_parser_peek_token (parser)->location;
5668 body = c_parser_c99_block_statement (parser);
5669 warn_for_misleading_indentation (for_loc, body_loc,
5670 c_parser_peek_token (parser)->location,
5671 c_parser_peek_token (parser)->type,
5672 "for");
5674 if (is_foreach_statement)
5675 objc_finish_foreach_loop (loc, object_expression, collection_expression, body, c_break_label, c_cont_label);
5676 else
5677 c_finish_loop (loc, cond, incr, body, c_break_label, c_cont_label, true);
5678 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99 || c_dialect_objc ()));
5679 c_break_label = save_break;
5680 c_cont_label = save_cont;
5683 /* Parse an asm statement, a GNU extension. This is a full-blown asm
5684 statement with inputs, outputs, clobbers, and volatile tag
5685 allowed.
5687 asm-statement:
5688 asm type-qualifier[opt] ( asm-argument ) ;
5689 asm type-qualifier[opt] goto ( asm-goto-argument ) ;
5691 asm-argument:
5692 asm-string-literal
5693 asm-string-literal : asm-operands[opt]
5694 asm-string-literal : asm-operands[opt] : asm-operands[opt]
5695 asm-string-literal : asm-operands[opt] : asm-operands[opt] : asm-clobbers[opt]
5697 asm-goto-argument:
5698 asm-string-literal : : asm-operands[opt] : asm-clobbers[opt] \
5699 : asm-goto-operands
5701 Qualifiers other than volatile are accepted in the syntax but
5702 warned for. */
5704 static tree
5705 c_parser_asm_statement (c_parser *parser)
5707 tree quals, str, outputs, inputs, clobbers, labels, ret;
5708 bool simple, is_goto;
5709 location_t asm_loc = c_parser_peek_token (parser)->location;
5710 int section, nsections;
5712 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ASM));
5713 c_parser_consume_token (parser);
5714 if (c_parser_next_token_is_keyword (parser, RID_VOLATILE))
5716 quals = c_parser_peek_token (parser)->value;
5717 c_parser_consume_token (parser);
5719 else if (c_parser_next_token_is_keyword (parser, RID_CONST)
5720 || c_parser_next_token_is_keyword (parser, RID_RESTRICT))
5722 warning_at (c_parser_peek_token (parser)->location,
5724 "%E qualifier ignored on asm",
5725 c_parser_peek_token (parser)->value);
5726 quals = NULL_TREE;
5727 c_parser_consume_token (parser);
5729 else
5730 quals = NULL_TREE;
5732 is_goto = false;
5733 if (c_parser_next_token_is_keyword (parser, RID_GOTO))
5735 c_parser_consume_token (parser);
5736 is_goto = true;
5739 /* ??? Follow the C++ parser rather than using the
5740 lex_untranslated_string kludge. */
5741 parser->lex_untranslated_string = true;
5742 ret = NULL;
5744 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5745 goto error;
5747 str = c_parser_asm_string_literal (parser);
5748 if (str == NULL_TREE)
5749 goto error_close_paren;
5751 simple = true;
5752 outputs = NULL_TREE;
5753 inputs = NULL_TREE;
5754 clobbers = NULL_TREE;
5755 labels = NULL_TREE;
5757 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN) && !is_goto)
5758 goto done_asm;
5760 /* Parse each colon-delimited section of operands. */
5761 nsections = 3 + is_goto;
5762 for (section = 0; section < nsections; ++section)
5764 if (!c_parser_require (parser, CPP_COLON,
5765 is_goto
5766 ? "expected %<:%>"
5767 : "expected %<:%> or %<)%>"))
5768 goto error_close_paren;
5770 /* Once past any colon, we're no longer a simple asm. */
5771 simple = false;
5773 if ((!c_parser_next_token_is (parser, CPP_COLON)
5774 && !c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
5775 || section == 3)
5776 switch (section)
5778 case 0:
5779 /* For asm goto, we don't allow output operands, but reserve
5780 the slot for a future extension that does allow them. */
5781 if (!is_goto)
5782 outputs = c_parser_asm_operands (parser);
5783 break;
5784 case 1:
5785 inputs = c_parser_asm_operands (parser);
5786 break;
5787 case 2:
5788 clobbers = c_parser_asm_clobbers (parser);
5789 break;
5790 case 3:
5791 labels = c_parser_asm_goto_operands (parser);
5792 break;
5793 default:
5794 gcc_unreachable ();
5797 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN) && !is_goto)
5798 goto done_asm;
5801 done_asm:
5802 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
5804 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5805 goto error;
5808 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
5809 c_parser_skip_to_end_of_block_or_statement (parser);
5811 ret = build_asm_stmt (quals, build_asm_expr (asm_loc, str, outputs, inputs,
5812 clobbers, labels, simple));
5814 error:
5815 parser->lex_untranslated_string = false;
5816 return ret;
5818 error_close_paren:
5819 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5820 goto error;
5823 /* Parse asm operands, a GNU extension.
5825 asm-operands:
5826 asm-operand
5827 asm-operands , asm-operand
5829 asm-operand:
5830 asm-string-literal ( expression )
5831 [ identifier ] asm-string-literal ( expression )
5834 static tree
5835 c_parser_asm_operands (c_parser *parser)
5837 tree list = NULL_TREE;
5838 while (true)
5840 tree name, str;
5841 struct c_expr expr;
5842 if (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
5844 c_parser_consume_token (parser);
5845 if (c_parser_next_token_is (parser, CPP_NAME))
5847 tree id = c_parser_peek_token (parser)->value;
5848 c_parser_consume_token (parser);
5849 name = build_string (IDENTIFIER_LENGTH (id),
5850 IDENTIFIER_POINTER (id));
5852 else
5854 c_parser_error (parser, "expected identifier");
5855 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
5856 return NULL_TREE;
5858 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
5859 "expected %<]%>");
5861 else
5862 name = NULL_TREE;
5863 str = c_parser_asm_string_literal (parser);
5864 if (str == NULL_TREE)
5865 return NULL_TREE;
5866 parser->lex_untranslated_string = false;
5867 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5869 parser->lex_untranslated_string = true;
5870 return NULL_TREE;
5872 expr = c_parser_expression (parser);
5873 mark_exp_read (expr.value);
5874 parser->lex_untranslated_string = true;
5875 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
5877 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5878 return NULL_TREE;
5880 list = chainon (list, build_tree_list (build_tree_list (name, str),
5881 expr.value));
5882 if (c_parser_next_token_is (parser, CPP_COMMA))
5883 c_parser_consume_token (parser);
5884 else
5885 break;
5887 return list;
5890 /* Parse asm clobbers, a GNU extension.
5892 asm-clobbers:
5893 asm-string-literal
5894 asm-clobbers , asm-string-literal
5897 static tree
5898 c_parser_asm_clobbers (c_parser *parser)
5900 tree list = NULL_TREE;
5901 while (true)
5903 tree str = c_parser_asm_string_literal (parser);
5904 if (str)
5905 list = tree_cons (NULL_TREE, str, list);
5906 else
5907 return NULL_TREE;
5908 if (c_parser_next_token_is (parser, CPP_COMMA))
5909 c_parser_consume_token (parser);
5910 else
5911 break;
5913 return list;
5916 /* Parse asm goto labels, a GNU extension.
5918 asm-goto-operands:
5919 identifier
5920 asm-goto-operands , identifier
5923 static tree
5924 c_parser_asm_goto_operands (c_parser *parser)
5926 tree list = NULL_TREE;
5927 while (true)
5929 tree name, label;
5931 if (c_parser_next_token_is (parser, CPP_NAME))
5933 c_token *tok = c_parser_peek_token (parser);
5934 name = tok->value;
5935 label = lookup_label_for_goto (tok->location, name);
5936 c_parser_consume_token (parser);
5937 TREE_USED (label) = 1;
5939 else
5941 c_parser_error (parser, "expected identifier");
5942 return NULL_TREE;
5945 name = build_string (IDENTIFIER_LENGTH (name),
5946 IDENTIFIER_POINTER (name));
5947 list = tree_cons (name, label, list);
5948 if (c_parser_next_token_is (parser, CPP_COMMA))
5949 c_parser_consume_token (parser);
5950 else
5951 return nreverse (list);
5955 /* Parse an expression other than a compound expression; that is, an
5956 assignment expression (C90 6.3.16, C99 6.5.16). If AFTER is not
5957 NULL then it is an Objective-C message expression which is the
5958 primary-expression starting the expression as an initializer.
5960 assignment-expression:
5961 conditional-expression
5962 unary-expression assignment-operator assignment-expression
5964 assignment-operator: one of
5965 = *= /= %= += -= <<= >>= &= ^= |=
5967 In GNU C we accept any conditional expression on the LHS and
5968 diagnose the invalid lvalue rather than producing a syntax
5969 error. */
5971 static struct c_expr
5972 c_parser_expr_no_commas (c_parser *parser, struct c_expr *after,
5973 tree omp_atomic_lhs)
5975 struct c_expr lhs, rhs, ret;
5976 enum tree_code code;
5977 location_t op_location, exp_location;
5978 gcc_assert (!after || c_dialect_objc ());
5979 lhs = c_parser_conditional_expression (parser, after, omp_atomic_lhs);
5980 op_location = c_parser_peek_token (parser)->location;
5981 switch (c_parser_peek_token (parser)->type)
5983 case CPP_EQ:
5984 code = NOP_EXPR;
5985 break;
5986 case CPP_MULT_EQ:
5987 code = MULT_EXPR;
5988 break;
5989 case CPP_DIV_EQ:
5990 code = TRUNC_DIV_EXPR;
5991 break;
5992 case CPP_MOD_EQ:
5993 code = TRUNC_MOD_EXPR;
5994 break;
5995 case CPP_PLUS_EQ:
5996 code = PLUS_EXPR;
5997 break;
5998 case CPP_MINUS_EQ:
5999 code = MINUS_EXPR;
6000 break;
6001 case CPP_LSHIFT_EQ:
6002 code = LSHIFT_EXPR;
6003 break;
6004 case CPP_RSHIFT_EQ:
6005 code = RSHIFT_EXPR;
6006 break;
6007 case CPP_AND_EQ:
6008 code = BIT_AND_EXPR;
6009 break;
6010 case CPP_XOR_EQ:
6011 code = BIT_XOR_EXPR;
6012 break;
6013 case CPP_OR_EQ:
6014 code = BIT_IOR_EXPR;
6015 break;
6016 default:
6017 return lhs;
6019 c_parser_consume_token (parser);
6020 exp_location = c_parser_peek_token (parser)->location;
6021 rhs = c_parser_expr_no_commas (parser, NULL);
6022 rhs = convert_lvalue_to_rvalue (exp_location, rhs, true, true);
6024 ret.value = build_modify_expr (op_location, lhs.value, lhs.original_type,
6025 code, exp_location, rhs.value,
6026 rhs.original_type);
6027 if (code == NOP_EXPR)
6028 ret.original_code = MODIFY_EXPR;
6029 else
6031 TREE_NO_WARNING (ret.value) = 1;
6032 ret.original_code = ERROR_MARK;
6034 ret.original_type = NULL;
6035 return ret;
6038 /* Parse a conditional expression (C90 6.3.15, C99 6.5.15). If AFTER
6039 is not NULL then it is an Objective-C message expression which is
6040 the primary-expression starting the expression as an initializer.
6042 conditional-expression:
6043 logical-OR-expression
6044 logical-OR-expression ? expression : conditional-expression
6046 GNU extensions:
6048 conditional-expression:
6049 logical-OR-expression ? : conditional-expression
6052 static struct c_expr
6053 c_parser_conditional_expression (c_parser *parser, struct c_expr *after,
6054 tree omp_atomic_lhs)
6056 struct c_expr cond, exp1, exp2, ret;
6057 location_t cond_loc, colon_loc, middle_loc;
6059 gcc_assert (!after || c_dialect_objc ());
6061 cond = c_parser_binary_expression (parser, after, omp_atomic_lhs);
6063 if (c_parser_next_token_is_not (parser, CPP_QUERY))
6064 return cond;
6065 cond_loc = c_parser_peek_token (parser)->location;
6066 cond = convert_lvalue_to_rvalue (cond_loc, cond, true, true);
6067 c_parser_consume_token (parser);
6068 if (c_parser_next_token_is (parser, CPP_COLON))
6070 tree eptype = NULL_TREE;
6072 middle_loc = c_parser_peek_token (parser)->location;
6073 pedwarn (middle_loc, OPT_Wpedantic,
6074 "ISO C forbids omitting the middle term of a ?: expression");
6075 warn_for_omitted_condop (middle_loc, cond.value);
6076 if (TREE_CODE (cond.value) == EXCESS_PRECISION_EXPR)
6078 eptype = TREE_TYPE (cond.value);
6079 cond.value = TREE_OPERAND (cond.value, 0);
6081 /* Make sure first operand is calculated only once. */
6082 exp1.value = c_save_expr (default_conversion (cond.value));
6083 if (eptype)
6084 exp1.value = build1 (EXCESS_PRECISION_EXPR, eptype, exp1.value);
6085 exp1.original_type = NULL;
6086 cond.value = c_objc_common_truthvalue_conversion (cond_loc, exp1.value);
6087 c_inhibit_evaluation_warnings += cond.value == truthvalue_true_node;
6089 else
6091 cond.value
6092 = c_objc_common_truthvalue_conversion
6093 (cond_loc, default_conversion (cond.value));
6094 c_inhibit_evaluation_warnings += cond.value == truthvalue_false_node;
6095 exp1 = c_parser_expression_conv (parser);
6096 mark_exp_read (exp1.value);
6097 c_inhibit_evaluation_warnings +=
6098 ((cond.value == truthvalue_true_node)
6099 - (cond.value == truthvalue_false_node));
6102 colon_loc = c_parser_peek_token (parser)->location;
6103 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
6105 c_inhibit_evaluation_warnings -= cond.value == truthvalue_true_node;
6106 ret.value = error_mark_node;
6107 ret.original_code = ERROR_MARK;
6108 ret.original_type = NULL;
6109 return ret;
6112 location_t exp2_loc = c_parser_peek_token (parser)->location;
6113 exp2 = c_parser_conditional_expression (parser, NULL, NULL_TREE);
6114 exp2 = convert_lvalue_to_rvalue (exp2_loc, exp2, true, true);
6116 c_inhibit_evaluation_warnings -= cond.value == truthvalue_true_node;
6117 ret.value = build_conditional_expr (colon_loc, cond.value,
6118 cond.original_code == C_MAYBE_CONST_EXPR,
6119 exp1.value, exp1.original_type,
6120 exp2.value, exp2.original_type);
6121 ret.original_code = ERROR_MARK;
6122 if (exp1.value == error_mark_node || exp2.value == error_mark_node)
6123 ret.original_type = NULL;
6124 else
6126 tree t1, t2;
6128 /* If both sides are enum type, the default conversion will have
6129 made the type of the result be an integer type. We want to
6130 remember the enum types we started with. */
6131 t1 = exp1.original_type ? exp1.original_type : TREE_TYPE (exp1.value);
6132 t2 = exp2.original_type ? exp2.original_type : TREE_TYPE (exp2.value);
6133 ret.original_type = ((t1 != error_mark_node
6134 && t2 != error_mark_node
6135 && (TYPE_MAIN_VARIANT (t1)
6136 == TYPE_MAIN_VARIANT (t2)))
6137 ? t1
6138 : NULL);
6140 return ret;
6143 /* Parse a binary expression; that is, a logical-OR-expression (C90
6144 6.3.5-6.3.14, C99 6.5.5-6.5.14). If AFTER is not NULL then it is
6145 an Objective-C message expression which is the primary-expression
6146 starting the expression as an initializer.
6148 OMP_ATOMIC_LHS is NULL, unless parsing OpenMP #pragma omp atomic,
6149 when it should be the unfolded lhs. In a valid OpenMP source,
6150 one of the operands of the toplevel binary expression must be equal
6151 to it. In that case, just return a build2 created binary operation
6152 rather than result of parser_build_binary_op.
6154 multiplicative-expression:
6155 cast-expression
6156 multiplicative-expression * cast-expression
6157 multiplicative-expression / cast-expression
6158 multiplicative-expression % cast-expression
6160 additive-expression:
6161 multiplicative-expression
6162 additive-expression + multiplicative-expression
6163 additive-expression - multiplicative-expression
6165 shift-expression:
6166 additive-expression
6167 shift-expression << additive-expression
6168 shift-expression >> additive-expression
6170 relational-expression:
6171 shift-expression
6172 relational-expression < shift-expression
6173 relational-expression > shift-expression
6174 relational-expression <= shift-expression
6175 relational-expression >= shift-expression
6177 equality-expression:
6178 relational-expression
6179 equality-expression == relational-expression
6180 equality-expression != relational-expression
6182 AND-expression:
6183 equality-expression
6184 AND-expression & equality-expression
6186 exclusive-OR-expression:
6187 AND-expression
6188 exclusive-OR-expression ^ AND-expression
6190 inclusive-OR-expression:
6191 exclusive-OR-expression
6192 inclusive-OR-expression | exclusive-OR-expression
6194 logical-AND-expression:
6195 inclusive-OR-expression
6196 logical-AND-expression && inclusive-OR-expression
6198 logical-OR-expression:
6199 logical-AND-expression
6200 logical-OR-expression || logical-AND-expression
6203 static struct c_expr
6204 c_parser_binary_expression (c_parser *parser, struct c_expr *after,
6205 tree omp_atomic_lhs)
6207 /* A binary expression is parsed using operator-precedence parsing,
6208 with the operands being cast expressions. All the binary
6209 operators are left-associative. Thus a binary expression is of
6210 form:
6212 E0 op1 E1 op2 E2 ...
6214 which we represent on a stack. On the stack, the precedence
6215 levels are strictly increasing. When a new operator is
6216 encountered of higher precedence than that at the top of the
6217 stack, it is pushed; its LHS is the top expression, and its RHS
6218 is everything parsed until it is popped. When a new operator is
6219 encountered with precedence less than or equal to that at the top
6220 of the stack, triples E[i-1] op[i] E[i] are popped and replaced
6221 by the result of the operation until the operator at the top of
6222 the stack has lower precedence than the new operator or there is
6223 only one element on the stack; then the top expression is the LHS
6224 of the new operator. In the case of logical AND and OR
6225 expressions, we also need to adjust c_inhibit_evaluation_warnings
6226 as appropriate when the operators are pushed and popped. */
6228 struct {
6229 /* The expression at this stack level. */
6230 struct c_expr expr;
6231 /* The precedence of the operator on its left, PREC_NONE at the
6232 bottom of the stack. */
6233 enum c_parser_prec prec;
6234 /* The operation on its left. */
6235 enum tree_code op;
6236 /* The source location of this operation. */
6237 location_t loc;
6238 } stack[NUM_PRECS];
6239 int sp;
6240 /* Location of the binary operator. */
6241 location_t binary_loc = UNKNOWN_LOCATION; /* Quiet warning. */
6242 #define POP \
6243 do { \
6244 switch (stack[sp].op) \
6246 case TRUTH_ANDIF_EXPR: \
6247 c_inhibit_evaluation_warnings -= (stack[sp - 1].expr.value \
6248 == truthvalue_false_node); \
6249 break; \
6250 case TRUTH_ORIF_EXPR: \
6251 c_inhibit_evaluation_warnings -= (stack[sp - 1].expr.value \
6252 == truthvalue_true_node); \
6253 break; \
6254 default: \
6255 break; \
6257 stack[sp - 1].expr \
6258 = convert_lvalue_to_rvalue (stack[sp - 1].loc, \
6259 stack[sp - 1].expr, true, true); \
6260 stack[sp].expr \
6261 = convert_lvalue_to_rvalue (stack[sp].loc, \
6262 stack[sp].expr, true, true); \
6263 if (__builtin_expect (omp_atomic_lhs != NULL_TREE, 0) && sp == 1 \
6264 && c_parser_peek_token (parser)->type == CPP_SEMICOLON \
6265 && ((1 << stack[sp].prec) \
6266 & ((1 << PREC_BITOR) | (1 << PREC_BITXOR) | (1 << PREC_BITAND) \
6267 | (1 << PREC_SHIFT) | (1 << PREC_ADD) | (1 << PREC_MULT))) \
6268 && stack[sp].op != TRUNC_MOD_EXPR \
6269 && stack[0].expr.value != error_mark_node \
6270 && stack[1].expr.value != error_mark_node \
6271 && (c_tree_equal (stack[0].expr.value, omp_atomic_lhs) \
6272 || c_tree_equal (stack[1].expr.value, omp_atomic_lhs))) \
6273 stack[0].expr.value \
6274 = build2 (stack[1].op, TREE_TYPE (stack[0].expr.value), \
6275 stack[0].expr.value, stack[1].expr.value); \
6276 else \
6277 stack[sp - 1].expr = parser_build_binary_op (stack[sp].loc, \
6278 stack[sp].op, \
6279 stack[sp - 1].expr, \
6280 stack[sp].expr); \
6281 sp--; \
6282 } while (0)
6283 gcc_assert (!after || c_dialect_objc ());
6284 stack[0].loc = c_parser_peek_token (parser)->location;
6285 stack[0].expr = c_parser_cast_expression (parser, after);
6286 stack[0].prec = PREC_NONE;
6287 sp = 0;
6288 while (true)
6290 enum c_parser_prec oprec;
6291 enum tree_code ocode;
6292 if (parser->error)
6293 goto out;
6294 switch (c_parser_peek_token (parser)->type)
6296 case CPP_MULT:
6297 oprec = PREC_MULT;
6298 ocode = MULT_EXPR;
6299 break;
6300 case CPP_DIV:
6301 oprec = PREC_MULT;
6302 ocode = TRUNC_DIV_EXPR;
6303 break;
6304 case CPP_MOD:
6305 oprec = PREC_MULT;
6306 ocode = TRUNC_MOD_EXPR;
6307 break;
6308 case CPP_PLUS:
6309 oprec = PREC_ADD;
6310 ocode = PLUS_EXPR;
6311 break;
6312 case CPP_MINUS:
6313 oprec = PREC_ADD;
6314 ocode = MINUS_EXPR;
6315 break;
6316 case CPP_LSHIFT:
6317 oprec = PREC_SHIFT;
6318 ocode = LSHIFT_EXPR;
6319 break;
6320 case CPP_RSHIFT:
6321 oprec = PREC_SHIFT;
6322 ocode = RSHIFT_EXPR;
6323 break;
6324 case CPP_LESS:
6325 oprec = PREC_REL;
6326 ocode = LT_EXPR;
6327 break;
6328 case CPP_GREATER:
6329 oprec = PREC_REL;
6330 ocode = GT_EXPR;
6331 break;
6332 case CPP_LESS_EQ:
6333 oprec = PREC_REL;
6334 ocode = LE_EXPR;
6335 break;
6336 case CPP_GREATER_EQ:
6337 oprec = PREC_REL;
6338 ocode = GE_EXPR;
6339 break;
6340 case CPP_EQ_EQ:
6341 oprec = PREC_EQ;
6342 ocode = EQ_EXPR;
6343 break;
6344 case CPP_NOT_EQ:
6345 oprec = PREC_EQ;
6346 ocode = NE_EXPR;
6347 break;
6348 case CPP_AND:
6349 oprec = PREC_BITAND;
6350 ocode = BIT_AND_EXPR;
6351 break;
6352 case CPP_XOR:
6353 oprec = PREC_BITXOR;
6354 ocode = BIT_XOR_EXPR;
6355 break;
6356 case CPP_OR:
6357 oprec = PREC_BITOR;
6358 ocode = BIT_IOR_EXPR;
6359 break;
6360 case CPP_AND_AND:
6361 oprec = PREC_LOGAND;
6362 ocode = TRUTH_ANDIF_EXPR;
6363 break;
6364 case CPP_OR_OR:
6365 oprec = PREC_LOGOR;
6366 ocode = TRUTH_ORIF_EXPR;
6367 break;
6368 default:
6369 /* Not a binary operator, so end of the binary
6370 expression. */
6371 goto out;
6373 binary_loc = c_parser_peek_token (parser)->location;
6374 while (oprec <= stack[sp].prec)
6375 POP;
6376 c_parser_consume_token (parser);
6377 switch (ocode)
6379 case TRUTH_ANDIF_EXPR:
6380 stack[sp].expr
6381 = convert_lvalue_to_rvalue (stack[sp].loc,
6382 stack[sp].expr, true, true);
6383 stack[sp].expr.value = c_objc_common_truthvalue_conversion
6384 (stack[sp].loc, default_conversion (stack[sp].expr.value));
6385 c_inhibit_evaluation_warnings += (stack[sp].expr.value
6386 == truthvalue_false_node);
6387 break;
6388 case TRUTH_ORIF_EXPR:
6389 stack[sp].expr
6390 = convert_lvalue_to_rvalue (stack[sp].loc,
6391 stack[sp].expr, true, true);
6392 stack[sp].expr.value = c_objc_common_truthvalue_conversion
6393 (stack[sp].loc, default_conversion (stack[sp].expr.value));
6394 c_inhibit_evaluation_warnings += (stack[sp].expr.value
6395 == truthvalue_true_node);
6396 break;
6397 default:
6398 break;
6400 sp++;
6401 stack[sp].loc = binary_loc;
6402 stack[sp].expr = c_parser_cast_expression (parser, NULL);
6403 stack[sp].prec = oprec;
6404 stack[sp].op = ocode;
6406 out:
6407 while (sp > 0)
6408 POP;
6409 return stack[0].expr;
6410 #undef POP
6413 /* Parse a cast expression (C90 6.3.4, C99 6.5.4). If AFTER is not
6414 NULL then it is an Objective-C message expression which is the
6415 primary-expression starting the expression as an initializer.
6417 cast-expression:
6418 unary-expression
6419 ( type-name ) unary-expression
6422 static struct c_expr
6423 c_parser_cast_expression (c_parser *parser, struct c_expr *after)
6425 location_t cast_loc = c_parser_peek_token (parser)->location;
6426 gcc_assert (!after || c_dialect_objc ());
6427 if (after)
6428 return c_parser_postfix_expression_after_primary (parser,
6429 cast_loc, *after);
6430 /* If the expression begins with a parenthesized type name, it may
6431 be either a cast or a compound literal; we need to see whether
6432 the next character is '{' to tell the difference. If not, it is
6433 an unary expression. Full detection of unknown typenames here
6434 would require a 3-token lookahead. */
6435 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
6436 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
6438 struct c_type_name *type_name;
6439 struct c_expr ret;
6440 struct c_expr expr;
6441 c_parser_consume_token (parser);
6442 type_name = c_parser_type_name (parser);
6443 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
6444 if (type_name == NULL)
6446 ret.value = error_mark_node;
6447 ret.original_code = ERROR_MARK;
6448 ret.original_type = NULL;
6449 return ret;
6452 /* Save casted types in the function's used types hash table. */
6453 used_types_insert (type_name->specs->type);
6455 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
6456 return c_parser_postfix_expression_after_paren_type (parser, type_name,
6457 cast_loc);
6459 location_t expr_loc = c_parser_peek_token (parser)->location;
6460 expr = c_parser_cast_expression (parser, NULL);
6461 expr = convert_lvalue_to_rvalue (expr_loc, expr, true, true);
6463 ret.value = c_cast_expr (cast_loc, type_name, expr.value);
6464 ret.original_code = ERROR_MARK;
6465 ret.original_type = NULL;
6466 return ret;
6468 else
6469 return c_parser_unary_expression (parser);
6472 /* Parse an unary expression (C90 6.3.3, C99 6.5.3).
6474 unary-expression:
6475 postfix-expression
6476 ++ unary-expression
6477 -- unary-expression
6478 unary-operator cast-expression
6479 sizeof unary-expression
6480 sizeof ( type-name )
6482 unary-operator: one of
6483 & * + - ~ !
6485 GNU extensions:
6487 unary-expression:
6488 __alignof__ unary-expression
6489 __alignof__ ( type-name )
6490 && identifier
6492 (C11 permits _Alignof with type names only.)
6494 unary-operator: one of
6495 __extension__ __real__ __imag__
6497 Transactional Memory:
6499 unary-expression:
6500 transaction-expression
6502 In addition, the GNU syntax treats ++ and -- as unary operators, so
6503 they may be applied to cast expressions with errors for non-lvalues
6504 given later. */
6506 static struct c_expr
6507 c_parser_unary_expression (c_parser *parser)
6509 int ext;
6510 struct c_expr ret, op;
6511 location_t op_loc = c_parser_peek_token (parser)->location;
6512 location_t exp_loc;
6513 ret.original_code = ERROR_MARK;
6514 ret.original_type = NULL;
6515 switch (c_parser_peek_token (parser)->type)
6517 case CPP_PLUS_PLUS:
6518 c_parser_consume_token (parser);
6519 exp_loc = c_parser_peek_token (parser)->location;
6520 op = c_parser_cast_expression (parser, NULL);
6522 /* If there is array notations in op, we expand them. */
6523 if (flag_cilkplus && TREE_CODE (op.value) == ARRAY_NOTATION_REF)
6524 return fix_array_notation_expr (exp_loc, PREINCREMENT_EXPR, op);
6525 else
6527 op = default_function_array_read_conversion (exp_loc, op);
6528 return parser_build_unary_op (op_loc, PREINCREMENT_EXPR, op);
6530 case CPP_MINUS_MINUS:
6531 c_parser_consume_token (parser);
6532 exp_loc = c_parser_peek_token (parser)->location;
6533 op = c_parser_cast_expression (parser, NULL);
6535 /* If there is array notations in op, we expand them. */
6536 if (flag_cilkplus && TREE_CODE (op.value) == ARRAY_NOTATION_REF)
6537 return fix_array_notation_expr (exp_loc, PREDECREMENT_EXPR, op);
6538 else
6540 op = default_function_array_read_conversion (exp_loc, op);
6541 return parser_build_unary_op (op_loc, PREDECREMENT_EXPR, op);
6543 case CPP_AND:
6544 c_parser_consume_token (parser);
6545 op = c_parser_cast_expression (parser, NULL);
6546 mark_exp_read (op.value);
6547 return parser_build_unary_op (op_loc, ADDR_EXPR, op);
6548 case CPP_MULT:
6549 c_parser_consume_token (parser);
6550 exp_loc = c_parser_peek_token (parser)->location;
6551 op = c_parser_cast_expression (parser, NULL);
6552 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
6553 ret.value = build_indirect_ref (op_loc, op.value, RO_UNARY_STAR);
6554 return ret;
6555 case CPP_PLUS:
6556 if (!c_dialect_objc () && !in_system_header_at (input_location))
6557 warning_at (op_loc,
6558 OPT_Wtraditional,
6559 "traditional C rejects the unary plus operator");
6560 c_parser_consume_token (parser);
6561 exp_loc = c_parser_peek_token (parser)->location;
6562 op = c_parser_cast_expression (parser, NULL);
6563 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
6564 return parser_build_unary_op (op_loc, CONVERT_EXPR, op);
6565 case CPP_MINUS:
6566 c_parser_consume_token (parser);
6567 exp_loc = c_parser_peek_token (parser)->location;
6568 op = c_parser_cast_expression (parser, NULL);
6569 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
6570 return parser_build_unary_op (op_loc, NEGATE_EXPR, op);
6571 case CPP_COMPL:
6572 c_parser_consume_token (parser);
6573 exp_loc = c_parser_peek_token (parser)->location;
6574 op = c_parser_cast_expression (parser, NULL);
6575 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
6576 return parser_build_unary_op (op_loc, BIT_NOT_EXPR, op);
6577 case CPP_NOT:
6578 c_parser_consume_token (parser);
6579 exp_loc = c_parser_peek_token (parser)->location;
6580 op = c_parser_cast_expression (parser, NULL);
6581 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
6582 return parser_build_unary_op (op_loc, TRUTH_NOT_EXPR, op);
6583 case CPP_AND_AND:
6584 /* Refer to the address of a label as a pointer. */
6585 c_parser_consume_token (parser);
6586 if (c_parser_next_token_is (parser, CPP_NAME))
6588 ret.value = finish_label_address_expr
6589 (c_parser_peek_token (parser)->value, op_loc);
6590 c_parser_consume_token (parser);
6592 else
6594 c_parser_error (parser, "expected identifier");
6595 ret.value = error_mark_node;
6597 return ret;
6598 case CPP_KEYWORD:
6599 switch (c_parser_peek_token (parser)->keyword)
6601 case RID_SIZEOF:
6602 return c_parser_sizeof_expression (parser);
6603 case RID_ALIGNOF:
6604 return c_parser_alignof_expression (parser);
6605 case RID_EXTENSION:
6606 c_parser_consume_token (parser);
6607 ext = disable_extension_diagnostics ();
6608 ret = c_parser_cast_expression (parser, NULL);
6609 restore_extension_diagnostics (ext);
6610 return ret;
6611 case RID_REALPART:
6612 c_parser_consume_token (parser);
6613 exp_loc = c_parser_peek_token (parser)->location;
6614 op = c_parser_cast_expression (parser, NULL);
6615 op = default_function_array_conversion (exp_loc, op);
6616 return parser_build_unary_op (op_loc, REALPART_EXPR, op);
6617 case RID_IMAGPART:
6618 c_parser_consume_token (parser);
6619 exp_loc = c_parser_peek_token (parser)->location;
6620 op = c_parser_cast_expression (parser, NULL);
6621 op = default_function_array_conversion (exp_loc, op);
6622 return parser_build_unary_op (op_loc, IMAGPART_EXPR, op);
6623 case RID_TRANSACTION_ATOMIC:
6624 case RID_TRANSACTION_RELAXED:
6625 return c_parser_transaction_expression (parser,
6626 c_parser_peek_token (parser)->keyword);
6627 default:
6628 return c_parser_postfix_expression (parser);
6630 default:
6631 return c_parser_postfix_expression (parser);
6635 /* Parse a sizeof expression. */
6637 static struct c_expr
6638 c_parser_sizeof_expression (c_parser *parser)
6640 struct c_expr expr;
6641 location_t expr_loc;
6642 gcc_assert (c_parser_next_token_is_keyword (parser, RID_SIZEOF));
6643 c_parser_consume_token (parser);
6644 c_inhibit_evaluation_warnings++;
6645 in_sizeof++;
6646 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
6647 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
6649 /* Either sizeof ( type-name ) or sizeof unary-expression
6650 starting with a compound literal. */
6651 struct c_type_name *type_name;
6652 c_parser_consume_token (parser);
6653 expr_loc = c_parser_peek_token (parser)->location;
6654 type_name = c_parser_type_name (parser);
6655 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
6656 if (type_name == NULL)
6658 struct c_expr ret;
6659 c_inhibit_evaluation_warnings--;
6660 in_sizeof--;
6661 ret.value = error_mark_node;
6662 ret.original_code = ERROR_MARK;
6663 ret.original_type = NULL;
6664 return ret;
6666 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
6668 expr = c_parser_postfix_expression_after_paren_type (parser,
6669 type_name,
6670 expr_loc);
6671 goto sizeof_expr;
6673 /* sizeof ( type-name ). */
6674 c_inhibit_evaluation_warnings--;
6675 in_sizeof--;
6676 return c_expr_sizeof_type (expr_loc, type_name);
6678 else
6680 expr_loc = c_parser_peek_token (parser)->location;
6681 expr = c_parser_unary_expression (parser);
6682 sizeof_expr:
6683 c_inhibit_evaluation_warnings--;
6684 in_sizeof--;
6685 mark_exp_read (expr.value);
6686 if (TREE_CODE (expr.value) == COMPONENT_REF
6687 && DECL_C_BIT_FIELD (TREE_OPERAND (expr.value, 1)))
6688 error_at (expr_loc, "%<sizeof%> applied to a bit-field");
6689 return c_expr_sizeof_expr (expr_loc, expr);
6693 /* Parse an alignof expression. */
6695 static struct c_expr
6696 c_parser_alignof_expression (c_parser *parser)
6698 struct c_expr expr;
6699 location_t loc = c_parser_peek_token (parser)->location;
6700 tree alignof_spelling = c_parser_peek_token (parser)->value;
6701 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ALIGNOF));
6702 bool is_c11_alignof = strcmp (IDENTIFIER_POINTER (alignof_spelling),
6703 "_Alignof") == 0;
6704 /* A diagnostic is not required for the use of this identifier in
6705 the implementation namespace; only diagnose it for the C11
6706 spelling because of existing code using the other spellings. */
6707 if (is_c11_alignof)
6709 if (flag_isoc99)
6710 pedwarn_c99 (loc, OPT_Wpedantic, "ISO C99 does not support %qE",
6711 alignof_spelling);
6712 else
6713 pedwarn_c99 (loc, OPT_Wpedantic, "ISO C90 does not support %qE",
6714 alignof_spelling);
6716 c_parser_consume_token (parser);
6717 c_inhibit_evaluation_warnings++;
6718 in_alignof++;
6719 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
6720 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
6722 /* Either __alignof__ ( type-name ) or __alignof__
6723 unary-expression starting with a compound literal. */
6724 location_t loc;
6725 struct c_type_name *type_name;
6726 struct c_expr ret;
6727 c_parser_consume_token (parser);
6728 loc = c_parser_peek_token (parser)->location;
6729 type_name = c_parser_type_name (parser);
6730 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
6731 if (type_name == NULL)
6733 struct c_expr ret;
6734 c_inhibit_evaluation_warnings--;
6735 in_alignof--;
6736 ret.value = error_mark_node;
6737 ret.original_code = ERROR_MARK;
6738 ret.original_type = NULL;
6739 return ret;
6741 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
6743 expr = c_parser_postfix_expression_after_paren_type (parser,
6744 type_name,
6745 loc);
6746 goto alignof_expr;
6748 /* alignof ( type-name ). */
6749 c_inhibit_evaluation_warnings--;
6750 in_alignof--;
6751 ret.value = c_sizeof_or_alignof_type (loc, groktypename (type_name,
6752 NULL, NULL),
6753 false, is_c11_alignof, 1);
6754 ret.original_code = ERROR_MARK;
6755 ret.original_type = NULL;
6756 return ret;
6758 else
6760 struct c_expr ret;
6761 expr = c_parser_unary_expression (parser);
6762 alignof_expr:
6763 mark_exp_read (expr.value);
6764 c_inhibit_evaluation_warnings--;
6765 in_alignof--;
6766 pedwarn (loc, OPT_Wpedantic, "ISO C does not allow %<%E (expression)%>",
6767 alignof_spelling);
6768 ret.value = c_alignof_expr (loc, expr.value);
6769 ret.original_code = ERROR_MARK;
6770 ret.original_type = NULL;
6771 return ret;
6775 /* Helper function to read arguments of builtins which are interfaces
6776 for the middle-end nodes like COMPLEX_EXPR, VEC_PERM_EXPR and
6777 others. The name of the builtin is passed using BNAME parameter.
6778 Function returns true if there were no errors while parsing and
6779 stores the arguments in CEXPR_LIST. */
6780 static bool
6781 c_parser_get_builtin_args (c_parser *parser, const char *bname,
6782 vec<c_expr_t, va_gc> **ret_cexpr_list,
6783 bool choose_expr_p)
6785 location_t loc = c_parser_peek_token (parser)->location;
6786 vec<c_expr_t, va_gc> *cexpr_list;
6787 c_expr_t expr;
6788 bool saved_force_folding_builtin_constant_p;
6790 *ret_cexpr_list = NULL;
6791 if (c_parser_next_token_is_not (parser, CPP_OPEN_PAREN))
6793 error_at (loc, "cannot take address of %qs", bname);
6794 return false;
6797 c_parser_consume_token (parser);
6799 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
6801 c_parser_consume_token (parser);
6802 return true;
6805 saved_force_folding_builtin_constant_p
6806 = force_folding_builtin_constant_p;
6807 force_folding_builtin_constant_p |= choose_expr_p;
6808 expr = c_parser_expr_no_commas (parser, NULL);
6809 force_folding_builtin_constant_p
6810 = saved_force_folding_builtin_constant_p;
6811 vec_alloc (cexpr_list, 1);
6812 vec_safe_push (cexpr_list, expr);
6813 while (c_parser_next_token_is (parser, CPP_COMMA))
6815 c_parser_consume_token (parser);
6816 expr = c_parser_expr_no_commas (parser, NULL);
6817 vec_safe_push (cexpr_list, expr);
6820 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
6821 return false;
6823 *ret_cexpr_list = cexpr_list;
6824 return true;
6827 /* This represents a single generic-association. */
6829 struct c_generic_association
6831 /* The location of the starting token of the type. */
6832 location_t type_location;
6833 /* The association's type, or NULL_TREE for 'default'. */
6834 tree type;
6835 /* The association's expression. */
6836 struct c_expr expression;
6839 /* Parse a generic-selection. (C11 6.5.1.1).
6841 generic-selection:
6842 _Generic ( assignment-expression , generic-assoc-list )
6844 generic-assoc-list:
6845 generic-association
6846 generic-assoc-list , generic-association
6848 generic-association:
6849 type-name : assignment-expression
6850 default : assignment-expression
6853 static struct c_expr
6854 c_parser_generic_selection (c_parser *parser)
6856 vec<c_generic_association> associations = vNULL;
6857 struct c_expr selector, error_expr;
6858 tree selector_type;
6859 struct c_generic_association matched_assoc;
6860 bool match_found = false;
6861 location_t generic_loc, selector_loc;
6863 error_expr.original_code = ERROR_MARK;
6864 error_expr.original_type = NULL;
6865 error_expr.value = error_mark_node;
6866 matched_assoc.type_location = UNKNOWN_LOCATION;
6867 matched_assoc.type = NULL_TREE;
6868 matched_assoc.expression = error_expr;
6870 gcc_assert (c_parser_next_token_is_keyword (parser, RID_GENERIC));
6871 generic_loc = c_parser_peek_token (parser)->location;
6872 c_parser_consume_token (parser);
6873 if (flag_isoc99)
6874 pedwarn_c99 (generic_loc, OPT_Wpedantic,
6875 "ISO C99 does not support %<_Generic%>");
6876 else
6877 pedwarn_c99 (generic_loc, OPT_Wpedantic,
6878 "ISO C90 does not support %<_Generic%>");
6880 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6881 return error_expr;
6883 c_inhibit_evaluation_warnings++;
6884 selector_loc = c_parser_peek_token (parser)->location;
6885 selector = c_parser_expr_no_commas (parser, NULL);
6886 selector = default_function_array_conversion (selector_loc, selector);
6887 c_inhibit_evaluation_warnings--;
6889 if (selector.value == error_mark_node)
6891 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6892 return selector;
6894 selector_type = TREE_TYPE (selector.value);
6895 /* In ISO C terms, rvalues (including the controlling expression of
6896 _Generic) do not have qualified types. */
6897 if (TREE_CODE (selector_type) != ARRAY_TYPE)
6898 selector_type = TYPE_MAIN_VARIANT (selector_type);
6899 /* In ISO C terms, _Noreturn is not part of the type of expressions
6900 such as &abort, but in GCC it is represented internally as a type
6901 qualifier. */
6902 if (FUNCTION_POINTER_TYPE_P (selector_type)
6903 && TYPE_QUALS (TREE_TYPE (selector_type)) != TYPE_UNQUALIFIED)
6904 selector_type
6905 = build_pointer_type (TYPE_MAIN_VARIANT (TREE_TYPE (selector_type)));
6907 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
6909 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6910 return error_expr;
6913 while (1)
6915 struct c_generic_association assoc, *iter;
6916 unsigned int ix;
6917 c_token *token = c_parser_peek_token (parser);
6919 assoc.type_location = token->location;
6920 if (token->type == CPP_KEYWORD && token->keyword == RID_DEFAULT)
6922 c_parser_consume_token (parser);
6923 assoc.type = NULL_TREE;
6925 else
6927 struct c_type_name *type_name;
6929 type_name = c_parser_type_name (parser);
6930 if (type_name == NULL)
6932 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6933 goto error_exit;
6935 assoc.type = groktypename (type_name, NULL, NULL);
6936 if (assoc.type == error_mark_node)
6938 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6939 goto error_exit;
6942 if (TREE_CODE (assoc.type) == FUNCTION_TYPE)
6943 error_at (assoc.type_location,
6944 "%<_Generic%> association has function type");
6945 else if (!COMPLETE_TYPE_P (assoc.type))
6946 error_at (assoc.type_location,
6947 "%<_Generic%> association has incomplete type");
6949 if (variably_modified_type_p (assoc.type, NULL_TREE))
6950 error_at (assoc.type_location,
6951 "%<_Generic%> association has "
6952 "variable length type");
6955 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
6957 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6958 goto error_exit;
6961 assoc.expression = c_parser_expr_no_commas (parser, NULL);
6962 if (assoc.expression.value == error_mark_node)
6964 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6965 goto error_exit;
6968 for (ix = 0; associations.iterate (ix, &iter); ++ix)
6970 if (assoc.type == NULL_TREE)
6972 if (iter->type == NULL_TREE)
6974 error_at (assoc.type_location,
6975 "duplicate %<default%> case in %<_Generic%>");
6976 inform (iter->type_location, "original %<default%> is here");
6979 else if (iter->type != NULL_TREE)
6981 if (comptypes (assoc.type, iter->type))
6983 error_at (assoc.type_location,
6984 "%<_Generic%> specifies two compatible types");
6985 inform (iter->type_location, "compatible type is here");
6990 if (assoc.type == NULL_TREE)
6992 if (!match_found)
6994 matched_assoc = assoc;
6995 match_found = true;
6998 else if (comptypes (assoc.type, selector_type))
7000 if (!match_found || matched_assoc.type == NULL_TREE)
7002 matched_assoc = assoc;
7003 match_found = true;
7005 else
7007 error_at (assoc.type_location,
7008 "%<_Generic> selector matches multiple associations");
7009 inform (matched_assoc.type_location,
7010 "other match is here");
7014 associations.safe_push (assoc);
7016 if (c_parser_peek_token (parser)->type != CPP_COMMA)
7017 break;
7018 c_parser_consume_token (parser);
7021 associations.release ();
7023 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
7025 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7026 return error_expr;
7029 if (!match_found)
7031 error_at (selector_loc, "%<_Generic%> selector of type %qT is not "
7032 "compatible with any association",
7033 selector_type);
7034 return error_expr;
7037 return matched_assoc.expression;
7039 error_exit:
7040 associations.release ();
7041 return error_expr;
7044 /* Parse a postfix expression (C90 6.3.1-6.3.2, C99 6.5.1-6.5.2).
7046 postfix-expression:
7047 primary-expression
7048 postfix-expression [ expression ]
7049 postfix-expression ( argument-expression-list[opt] )
7050 postfix-expression . identifier
7051 postfix-expression -> identifier
7052 postfix-expression ++
7053 postfix-expression --
7054 ( type-name ) { initializer-list }
7055 ( type-name ) { initializer-list , }
7057 argument-expression-list:
7058 argument-expression
7059 argument-expression-list , argument-expression
7061 primary-expression:
7062 identifier
7063 constant
7064 string-literal
7065 ( expression )
7066 generic-selection
7068 GNU extensions:
7070 primary-expression:
7071 __func__
7072 (treated as a keyword in GNU C)
7073 __FUNCTION__
7074 __PRETTY_FUNCTION__
7075 ( compound-statement )
7076 __builtin_va_arg ( assignment-expression , type-name )
7077 __builtin_offsetof ( type-name , offsetof-member-designator )
7078 __builtin_choose_expr ( assignment-expression ,
7079 assignment-expression ,
7080 assignment-expression )
7081 __builtin_types_compatible_p ( type-name , type-name )
7082 __builtin_complex ( assignment-expression , assignment-expression )
7083 __builtin_shuffle ( assignment-expression , assignment-expression )
7084 __builtin_shuffle ( assignment-expression ,
7085 assignment-expression ,
7086 assignment-expression, )
7088 offsetof-member-designator:
7089 identifier
7090 offsetof-member-designator . identifier
7091 offsetof-member-designator [ expression ]
7093 Objective-C:
7095 primary-expression:
7096 [ objc-receiver objc-message-args ]
7097 @selector ( objc-selector-arg )
7098 @protocol ( identifier )
7099 @encode ( type-name )
7100 objc-string-literal
7101 Classname . identifier
7104 static struct c_expr
7105 c_parser_postfix_expression (c_parser *parser)
7107 struct c_expr expr, e1;
7108 struct c_type_name *t1, *t2;
7109 location_t loc = c_parser_peek_token (parser)->location;;
7110 expr.original_code = ERROR_MARK;
7111 expr.original_type = NULL;
7112 switch (c_parser_peek_token (parser)->type)
7114 case CPP_NUMBER:
7115 expr.value = c_parser_peek_token (parser)->value;
7116 loc = c_parser_peek_token (parser)->location;
7117 c_parser_consume_token (parser);
7118 if (TREE_CODE (expr.value) == FIXED_CST
7119 && !targetm.fixed_point_supported_p ())
7121 error_at (loc, "fixed-point types not supported for this target");
7122 expr.value = error_mark_node;
7124 break;
7125 case CPP_CHAR:
7126 case CPP_CHAR16:
7127 case CPP_CHAR32:
7128 case CPP_WCHAR:
7129 expr.value = c_parser_peek_token (parser)->value;
7130 c_parser_consume_token (parser);
7131 break;
7132 case CPP_STRING:
7133 case CPP_STRING16:
7134 case CPP_STRING32:
7135 case CPP_WSTRING:
7136 case CPP_UTF8STRING:
7137 expr.value = c_parser_peek_token (parser)->value;
7138 expr.original_code = STRING_CST;
7139 c_parser_consume_token (parser);
7140 break;
7141 case CPP_OBJC_STRING:
7142 gcc_assert (c_dialect_objc ());
7143 expr.value
7144 = objc_build_string_object (c_parser_peek_token (parser)->value);
7145 c_parser_consume_token (parser);
7146 break;
7147 case CPP_NAME:
7148 switch (c_parser_peek_token (parser)->id_kind)
7150 case C_ID_ID:
7152 tree id = c_parser_peek_token (parser)->value;
7153 c_parser_consume_token (parser);
7154 expr.value = build_external_ref (loc, id,
7155 (c_parser_peek_token (parser)->type
7156 == CPP_OPEN_PAREN),
7157 &expr.original_type);
7158 break;
7160 case C_ID_CLASSNAME:
7162 /* Here we parse the Objective-C 2.0 Class.name dot
7163 syntax. */
7164 tree class_name = c_parser_peek_token (parser)->value;
7165 tree component;
7166 c_parser_consume_token (parser);
7167 gcc_assert (c_dialect_objc ());
7168 if (!c_parser_require (parser, CPP_DOT, "expected %<.%>"))
7170 expr.value = error_mark_node;
7171 break;
7173 if (c_parser_next_token_is_not (parser, CPP_NAME))
7175 c_parser_error (parser, "expected identifier");
7176 expr.value = error_mark_node;
7177 break;
7179 component = c_parser_peek_token (parser)->value;
7180 c_parser_consume_token (parser);
7181 expr.value = objc_build_class_component_ref (class_name,
7182 component);
7183 break;
7185 default:
7186 c_parser_error (parser, "expected expression");
7187 expr.value = error_mark_node;
7188 break;
7190 break;
7191 case CPP_OPEN_PAREN:
7192 /* A parenthesized expression, statement expression or compound
7193 literal. */
7194 if (c_parser_peek_2nd_token (parser)->type == CPP_OPEN_BRACE)
7196 /* A statement expression. */
7197 tree stmt;
7198 location_t brace_loc;
7199 c_parser_consume_token (parser);
7200 brace_loc = c_parser_peek_token (parser)->location;
7201 c_parser_consume_token (parser);
7202 if (!building_stmt_list_p ())
7204 error_at (loc, "braced-group within expression allowed "
7205 "only inside a function");
7206 parser->error = true;
7207 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
7208 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7209 expr.value = error_mark_node;
7210 break;
7212 stmt = c_begin_stmt_expr ();
7213 c_parser_compound_statement_nostart (parser);
7214 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7215 "expected %<)%>");
7216 pedwarn (loc, OPT_Wpedantic,
7217 "ISO C forbids braced-groups within expressions");
7218 expr.value = c_finish_stmt_expr (brace_loc, stmt);
7219 mark_exp_read (expr.value);
7221 else if (c_token_starts_typename (c_parser_peek_2nd_token (parser)))
7223 /* A compound literal. ??? Can we actually get here rather
7224 than going directly to
7225 c_parser_postfix_expression_after_paren_type from
7226 elsewhere? */
7227 location_t loc;
7228 struct c_type_name *type_name;
7229 c_parser_consume_token (parser);
7230 loc = c_parser_peek_token (parser)->location;
7231 type_name = c_parser_type_name (parser);
7232 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7233 "expected %<)%>");
7234 if (type_name == NULL)
7236 expr.value = error_mark_node;
7238 else
7239 expr = c_parser_postfix_expression_after_paren_type (parser,
7240 type_name,
7241 loc);
7243 else
7245 /* A parenthesized expression. */
7246 c_parser_consume_token (parser);
7247 expr = c_parser_expression (parser);
7248 if (TREE_CODE (expr.value) == MODIFY_EXPR)
7249 TREE_NO_WARNING (expr.value) = 1;
7250 if (expr.original_code != C_MAYBE_CONST_EXPR)
7251 expr.original_code = ERROR_MARK;
7252 /* Don't change EXPR.ORIGINAL_TYPE. */
7253 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7254 "expected %<)%>");
7256 break;
7257 case CPP_KEYWORD:
7258 switch (c_parser_peek_token (parser)->keyword)
7260 case RID_FUNCTION_NAME:
7261 pedwarn (loc, OPT_Wpedantic, "ISO C does not support "
7262 "%<__FUNCTION__%> predefined identifier");
7263 expr.value = fname_decl (loc,
7264 c_parser_peek_token (parser)->keyword,
7265 c_parser_peek_token (parser)->value);
7266 c_parser_consume_token (parser);
7267 break;
7268 case RID_PRETTY_FUNCTION_NAME:
7269 pedwarn (loc, OPT_Wpedantic, "ISO C does not support "
7270 "%<__PRETTY_FUNCTION__%> predefined identifier");
7271 expr.value = fname_decl (loc,
7272 c_parser_peek_token (parser)->keyword,
7273 c_parser_peek_token (parser)->value);
7274 c_parser_consume_token (parser);
7275 break;
7276 case RID_C99_FUNCTION_NAME:
7277 pedwarn_c90 (loc, OPT_Wpedantic, "ISO C90 does not support "
7278 "%<__func__%> predefined identifier");
7279 expr.value = fname_decl (loc,
7280 c_parser_peek_token (parser)->keyword,
7281 c_parser_peek_token (parser)->value);
7282 c_parser_consume_token (parser);
7283 break;
7284 case RID_VA_ARG:
7285 c_parser_consume_token (parser);
7286 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7288 expr.value = error_mark_node;
7289 break;
7291 e1 = c_parser_expr_no_commas (parser, NULL);
7292 mark_exp_read (e1.value);
7293 e1.value = c_fully_fold (e1.value, false, NULL);
7294 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
7296 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7297 expr.value = error_mark_node;
7298 break;
7300 loc = c_parser_peek_token (parser)->location;
7301 t1 = c_parser_type_name (parser);
7302 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7303 "expected %<)%>");
7304 if (t1 == NULL)
7306 expr.value = error_mark_node;
7308 else
7310 tree type_expr = NULL_TREE;
7311 expr.value = c_build_va_arg (loc, e1.value,
7312 groktypename (t1, &type_expr, NULL));
7313 if (type_expr)
7315 expr.value = build2 (C_MAYBE_CONST_EXPR,
7316 TREE_TYPE (expr.value), type_expr,
7317 expr.value);
7318 C_MAYBE_CONST_EXPR_NON_CONST (expr.value) = true;
7321 break;
7322 case RID_OFFSETOF:
7323 c_parser_consume_token (parser);
7324 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7326 expr.value = error_mark_node;
7327 break;
7329 t1 = c_parser_type_name (parser);
7330 if (t1 == NULL)
7331 parser->error = true;
7332 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
7333 gcc_assert (parser->error);
7334 if (parser->error)
7336 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7337 expr.value = error_mark_node;
7338 break;
7342 tree type = groktypename (t1, NULL, NULL);
7343 tree offsetof_ref;
7344 if (type == error_mark_node)
7345 offsetof_ref = error_mark_node;
7346 else
7348 offsetof_ref = build1 (INDIRECT_REF, type, null_pointer_node);
7349 SET_EXPR_LOCATION (offsetof_ref, loc);
7351 /* Parse the second argument to __builtin_offsetof. We
7352 must have one identifier, and beyond that we want to
7353 accept sub structure and sub array references. */
7354 if (c_parser_next_token_is (parser, CPP_NAME))
7356 offsetof_ref = build_component_ref
7357 (loc, offsetof_ref, c_parser_peek_token (parser)->value);
7358 c_parser_consume_token (parser);
7359 while (c_parser_next_token_is (parser, CPP_DOT)
7360 || c_parser_next_token_is (parser,
7361 CPP_OPEN_SQUARE)
7362 || c_parser_next_token_is (parser,
7363 CPP_DEREF))
7365 if (c_parser_next_token_is (parser, CPP_DEREF))
7367 loc = c_parser_peek_token (parser)->location;
7368 offsetof_ref = build_array_ref (loc,
7369 offsetof_ref,
7370 integer_zero_node);
7371 goto do_dot;
7373 else if (c_parser_next_token_is (parser, CPP_DOT))
7375 do_dot:
7376 c_parser_consume_token (parser);
7377 if (c_parser_next_token_is_not (parser,
7378 CPP_NAME))
7380 c_parser_error (parser, "expected identifier");
7381 break;
7383 offsetof_ref = build_component_ref
7384 (loc, offsetof_ref,
7385 c_parser_peek_token (parser)->value);
7386 c_parser_consume_token (parser);
7388 else
7390 struct c_expr ce;
7391 tree idx;
7392 loc = c_parser_peek_token (parser)->location;
7393 c_parser_consume_token (parser);
7394 ce = c_parser_expression (parser);
7395 ce = convert_lvalue_to_rvalue (loc, ce, false, false);
7396 idx = ce.value;
7397 idx = c_fully_fold (idx, false, NULL);
7398 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
7399 "expected %<]%>");
7400 offsetof_ref = build_array_ref (loc, offsetof_ref, idx);
7404 else
7405 c_parser_error (parser, "expected identifier");
7406 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7407 "expected %<)%>");
7408 expr.value = fold_offsetof (offsetof_ref);
7410 break;
7411 case RID_CHOOSE_EXPR:
7413 vec<c_expr_t, va_gc> *cexpr_list;
7414 c_expr_t *e1_p, *e2_p, *e3_p;
7415 tree c;
7417 c_parser_consume_token (parser);
7418 if (!c_parser_get_builtin_args (parser,
7419 "__builtin_choose_expr",
7420 &cexpr_list, true))
7422 expr.value = error_mark_node;
7423 break;
7426 if (vec_safe_length (cexpr_list) != 3)
7428 error_at (loc, "wrong number of arguments to "
7429 "%<__builtin_choose_expr%>");
7430 expr.value = error_mark_node;
7431 break;
7434 e1_p = &(*cexpr_list)[0];
7435 e2_p = &(*cexpr_list)[1];
7436 e3_p = &(*cexpr_list)[2];
7438 c = e1_p->value;
7439 mark_exp_read (e2_p->value);
7440 mark_exp_read (e3_p->value);
7441 if (TREE_CODE (c) != INTEGER_CST
7442 || !INTEGRAL_TYPE_P (TREE_TYPE (c)))
7443 error_at (loc,
7444 "first argument to %<__builtin_choose_expr%> not"
7445 " a constant");
7446 constant_expression_warning (c);
7447 expr = integer_zerop (c) ? *e3_p : *e2_p;
7448 break;
7450 case RID_TYPES_COMPATIBLE_P:
7451 c_parser_consume_token (parser);
7452 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7454 expr.value = error_mark_node;
7455 break;
7457 t1 = c_parser_type_name (parser);
7458 if (t1 == NULL)
7460 expr.value = error_mark_node;
7461 break;
7463 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
7465 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7466 expr.value = error_mark_node;
7467 break;
7469 t2 = c_parser_type_name (parser);
7470 if (t2 == NULL)
7472 expr.value = error_mark_node;
7473 break;
7475 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7476 "expected %<)%>");
7478 tree e1, e2;
7479 e1 = groktypename (t1, NULL, NULL);
7480 e2 = groktypename (t2, NULL, NULL);
7481 if (e1 == error_mark_node || e2 == error_mark_node)
7483 expr.value = error_mark_node;
7484 break;
7487 e1 = TYPE_MAIN_VARIANT (e1);
7488 e2 = TYPE_MAIN_VARIANT (e2);
7490 expr.value
7491 = comptypes (e1, e2) ? integer_one_node : integer_zero_node;
7493 break;
7494 case RID_BUILTIN_CALL_WITH_STATIC_CHAIN:
7496 vec<c_expr_t, va_gc> *cexpr_list;
7497 c_expr_t *e2_p;
7498 tree chain_value;
7500 c_parser_consume_token (parser);
7501 if (!c_parser_get_builtin_args (parser,
7502 "__builtin_call_with_static_chain",
7503 &cexpr_list, false))
7505 expr.value = error_mark_node;
7506 break;
7508 if (vec_safe_length (cexpr_list) != 2)
7510 error_at (loc, "wrong number of arguments to "
7511 "%<__builtin_call_with_static_chain%>");
7512 expr.value = error_mark_node;
7513 break;
7516 expr = (*cexpr_list)[0];
7517 e2_p = &(*cexpr_list)[1];
7518 *e2_p = convert_lvalue_to_rvalue (loc, *e2_p, true, true);
7519 chain_value = e2_p->value;
7520 mark_exp_read (chain_value);
7522 if (TREE_CODE (expr.value) != CALL_EXPR)
7523 error_at (loc, "first argument to "
7524 "%<__builtin_call_with_static_chain%> "
7525 "must be a call expression");
7526 else if (TREE_CODE (TREE_TYPE (chain_value)) != POINTER_TYPE)
7527 error_at (loc, "second argument to "
7528 "%<__builtin_call_with_static_chain%> "
7529 "must be a pointer type");
7530 else
7531 CALL_EXPR_STATIC_CHAIN (expr.value) = chain_value;
7532 break;
7534 case RID_BUILTIN_COMPLEX:
7536 vec<c_expr_t, va_gc> *cexpr_list;
7537 c_expr_t *e1_p, *e2_p;
7539 c_parser_consume_token (parser);
7540 if (!c_parser_get_builtin_args (parser,
7541 "__builtin_complex",
7542 &cexpr_list, false))
7544 expr.value = error_mark_node;
7545 break;
7548 if (vec_safe_length (cexpr_list) != 2)
7550 error_at (loc, "wrong number of arguments to "
7551 "%<__builtin_complex%>");
7552 expr.value = error_mark_node;
7553 break;
7556 e1_p = &(*cexpr_list)[0];
7557 e2_p = &(*cexpr_list)[1];
7559 *e1_p = convert_lvalue_to_rvalue (loc, *e1_p, true, true);
7560 if (TREE_CODE (e1_p->value) == EXCESS_PRECISION_EXPR)
7561 e1_p->value = convert (TREE_TYPE (e1_p->value),
7562 TREE_OPERAND (e1_p->value, 0));
7563 *e2_p = convert_lvalue_to_rvalue (loc, *e2_p, true, true);
7564 if (TREE_CODE (e2_p->value) == EXCESS_PRECISION_EXPR)
7565 e2_p->value = convert (TREE_TYPE (e2_p->value),
7566 TREE_OPERAND (e2_p->value, 0));
7567 if (!SCALAR_FLOAT_TYPE_P (TREE_TYPE (e1_p->value))
7568 || DECIMAL_FLOAT_TYPE_P (TREE_TYPE (e1_p->value))
7569 || !SCALAR_FLOAT_TYPE_P (TREE_TYPE (e2_p->value))
7570 || DECIMAL_FLOAT_TYPE_P (TREE_TYPE (e2_p->value)))
7572 error_at (loc, "%<__builtin_complex%> operand "
7573 "not of real binary floating-point type");
7574 expr.value = error_mark_node;
7575 break;
7577 if (TYPE_MAIN_VARIANT (TREE_TYPE (e1_p->value))
7578 != TYPE_MAIN_VARIANT (TREE_TYPE (e2_p->value)))
7580 error_at (loc,
7581 "%<__builtin_complex%> operands of different types");
7582 expr.value = error_mark_node;
7583 break;
7585 pedwarn_c90 (loc, OPT_Wpedantic,
7586 "ISO C90 does not support complex types");
7587 expr.value = build2 (COMPLEX_EXPR,
7588 build_complex_type
7589 (TYPE_MAIN_VARIANT
7590 (TREE_TYPE (e1_p->value))),
7591 e1_p->value, e2_p->value);
7592 break;
7594 case RID_BUILTIN_SHUFFLE:
7596 vec<c_expr_t, va_gc> *cexpr_list;
7597 unsigned int i;
7598 c_expr_t *p;
7600 c_parser_consume_token (parser);
7601 if (!c_parser_get_builtin_args (parser,
7602 "__builtin_shuffle",
7603 &cexpr_list, false))
7605 expr.value = error_mark_node;
7606 break;
7609 FOR_EACH_VEC_SAFE_ELT (cexpr_list, i, p)
7610 *p = convert_lvalue_to_rvalue (loc, *p, true, true);
7612 if (vec_safe_length (cexpr_list) == 2)
7613 expr.value =
7614 c_build_vec_perm_expr
7615 (loc, (*cexpr_list)[0].value,
7616 NULL_TREE, (*cexpr_list)[1].value);
7618 else if (vec_safe_length (cexpr_list) == 3)
7619 expr.value =
7620 c_build_vec_perm_expr
7621 (loc, (*cexpr_list)[0].value,
7622 (*cexpr_list)[1].value,
7623 (*cexpr_list)[2].value);
7624 else
7626 error_at (loc, "wrong number of arguments to "
7627 "%<__builtin_shuffle%>");
7628 expr.value = error_mark_node;
7630 break;
7632 case RID_AT_SELECTOR:
7633 gcc_assert (c_dialect_objc ());
7634 c_parser_consume_token (parser);
7635 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7637 expr.value = error_mark_node;
7638 break;
7641 tree sel = c_parser_objc_selector_arg (parser);
7642 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7643 "expected %<)%>");
7644 expr.value = objc_build_selector_expr (loc, sel);
7646 break;
7647 case RID_AT_PROTOCOL:
7648 gcc_assert (c_dialect_objc ());
7649 c_parser_consume_token (parser);
7650 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7652 expr.value = error_mark_node;
7653 break;
7655 if (c_parser_next_token_is_not (parser, CPP_NAME))
7657 c_parser_error (parser, "expected identifier");
7658 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7659 expr.value = error_mark_node;
7660 break;
7663 tree id = c_parser_peek_token (parser)->value;
7664 c_parser_consume_token (parser);
7665 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7666 "expected %<)%>");
7667 expr.value = objc_build_protocol_expr (id);
7669 break;
7670 case RID_AT_ENCODE:
7671 /* Extension to support C-structures in the archiver. */
7672 gcc_assert (c_dialect_objc ());
7673 c_parser_consume_token (parser);
7674 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7676 expr.value = error_mark_node;
7677 break;
7679 t1 = c_parser_type_name (parser);
7680 if (t1 == NULL)
7682 expr.value = error_mark_node;
7683 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7684 break;
7686 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7687 "expected %<)%>");
7689 tree type = groktypename (t1, NULL, NULL);
7690 expr.value = objc_build_encode_expr (type);
7692 break;
7693 case RID_GENERIC:
7694 expr = c_parser_generic_selection (parser);
7695 break;
7696 case RID_CILK_SPAWN:
7697 c_parser_consume_token (parser);
7698 if (!flag_cilkplus)
7700 error_at (loc, "-fcilkplus must be enabled to use "
7701 "%<_Cilk_spawn%>");
7702 expr = c_parser_postfix_expression (parser);
7703 expr.value = error_mark_node;
7705 else if (c_parser_peek_token (parser)->keyword == RID_CILK_SPAWN)
7707 error_at (loc, "consecutive %<_Cilk_spawn%> keywords "
7708 "are not permitted");
7709 /* Now flush out all the _Cilk_spawns. */
7710 while (c_parser_peek_token (parser)->keyword == RID_CILK_SPAWN)
7711 c_parser_consume_token (parser);
7712 expr = c_parser_postfix_expression (parser);
7714 else
7716 expr = c_parser_postfix_expression (parser);
7717 expr.value = build_cilk_spawn (loc, expr.value);
7719 break;
7720 default:
7721 c_parser_error (parser, "expected expression");
7722 expr.value = error_mark_node;
7723 break;
7725 break;
7726 case CPP_OPEN_SQUARE:
7727 if (c_dialect_objc ())
7729 tree receiver, args;
7730 c_parser_consume_token (parser);
7731 receiver = c_parser_objc_receiver (parser);
7732 args = c_parser_objc_message_args (parser);
7733 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
7734 "expected %<]%>");
7735 expr.value = objc_build_message_expr (receiver, args);
7736 break;
7738 /* Else fall through to report error. */
7739 default:
7740 c_parser_error (parser, "expected expression");
7741 expr.value = error_mark_node;
7742 break;
7744 return c_parser_postfix_expression_after_primary (parser, loc, expr);
7747 /* Parse a postfix expression after a parenthesized type name: the
7748 brace-enclosed initializer of a compound literal, possibly followed
7749 by some postfix operators. This is separate because it is not
7750 possible to tell until after the type name whether a cast
7751 expression has a cast or a compound literal, or whether the operand
7752 of sizeof is a parenthesized type name or starts with a compound
7753 literal. TYPE_LOC is the location where TYPE_NAME starts--the
7754 location of the first token after the parentheses around the type
7755 name. */
7757 static struct c_expr
7758 c_parser_postfix_expression_after_paren_type (c_parser *parser,
7759 struct c_type_name *type_name,
7760 location_t type_loc)
7762 tree type;
7763 struct c_expr init;
7764 bool non_const;
7765 struct c_expr expr;
7766 location_t start_loc;
7767 tree type_expr = NULL_TREE;
7768 bool type_expr_const = true;
7769 check_compound_literal_type (type_loc, type_name);
7770 start_init (NULL_TREE, NULL, 0);
7771 type = groktypename (type_name, &type_expr, &type_expr_const);
7772 start_loc = c_parser_peek_token (parser)->location;
7773 if (type != error_mark_node && C_TYPE_VARIABLE_SIZE (type))
7775 error_at (type_loc, "compound literal has variable size");
7776 type = error_mark_node;
7778 init = c_parser_braced_init (parser, type, false);
7779 finish_init ();
7780 maybe_warn_string_init (type_loc, type, init);
7782 if (type != error_mark_node
7783 && !ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (type))
7784 && current_function_decl)
7786 error ("compound literal qualified by address-space qualifier");
7787 type = error_mark_node;
7790 pedwarn_c90 (start_loc, OPT_Wpedantic, "ISO C90 forbids compound literals");
7791 non_const = ((init.value && TREE_CODE (init.value) == CONSTRUCTOR)
7792 ? CONSTRUCTOR_NON_CONST (init.value)
7793 : init.original_code == C_MAYBE_CONST_EXPR);
7794 non_const |= !type_expr_const;
7795 expr.value = build_compound_literal (start_loc, type, init.value, non_const);
7796 expr.original_code = ERROR_MARK;
7797 expr.original_type = NULL;
7798 if (type_expr)
7800 if (TREE_CODE (expr.value) == C_MAYBE_CONST_EXPR)
7802 gcc_assert (C_MAYBE_CONST_EXPR_PRE (expr.value) == NULL_TREE);
7803 C_MAYBE_CONST_EXPR_PRE (expr.value) = type_expr;
7805 else
7807 gcc_assert (!non_const);
7808 expr.value = build2 (C_MAYBE_CONST_EXPR, type,
7809 type_expr, expr.value);
7812 return c_parser_postfix_expression_after_primary (parser, start_loc, expr);
7815 /* Callback function for sizeof_pointer_memaccess_warning to compare
7816 types. */
7818 static bool
7819 sizeof_ptr_memacc_comptypes (tree type1, tree type2)
7821 return comptypes (type1, type2) == 1;
7824 /* Parse a postfix expression after the initial primary or compound
7825 literal; that is, parse a series of postfix operators.
7827 EXPR_LOC is the location of the primary expression. */
7829 static struct c_expr
7830 c_parser_postfix_expression_after_primary (c_parser *parser,
7831 location_t expr_loc,
7832 struct c_expr expr)
7834 struct c_expr orig_expr;
7835 tree ident, idx;
7836 location_t sizeof_arg_loc[3];
7837 tree sizeof_arg[3];
7838 unsigned int literal_zero_mask;
7839 unsigned int i;
7840 vec<tree, va_gc> *exprlist;
7841 vec<tree, va_gc> *origtypes = NULL;
7842 vec<location_t> arg_loc = vNULL;
7844 while (true)
7846 location_t op_loc = c_parser_peek_token (parser)->location;
7847 switch (c_parser_peek_token (parser)->type)
7849 case CPP_OPEN_SQUARE:
7850 /* Array reference. */
7851 c_parser_consume_token (parser);
7852 if (flag_cilkplus
7853 && c_parser_peek_token (parser)->type == CPP_COLON)
7854 /* If we are here, then we have something like this:
7855 Array [ : ]
7857 expr.value = c_parser_array_notation (expr_loc, parser, NULL_TREE,
7858 expr.value);
7859 else
7861 idx = c_parser_expression (parser).value;
7862 /* Here we have 3 options:
7863 1. Array [EXPR] -- Normal Array call.
7864 2. Array [EXPR : EXPR] -- Array notation without stride.
7865 3. Array [EXPR : EXPR : EXPR] -- Array notation with stride.
7867 For 1, we just handle it just like a normal array expression.
7868 For 2 and 3 we handle it like we handle array notations. The
7869 idx value we have above becomes the initial/start index.
7871 if (flag_cilkplus
7872 && c_parser_peek_token (parser)->type == CPP_COLON)
7873 expr.value = c_parser_array_notation (expr_loc, parser, idx,
7874 expr.value);
7875 else
7877 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
7878 "expected %<]%>");
7879 expr.value = build_array_ref (op_loc, expr.value, idx);
7882 expr.original_code = ERROR_MARK;
7883 expr.original_type = NULL;
7884 break;
7885 case CPP_OPEN_PAREN:
7886 /* Function call. */
7887 c_parser_consume_token (parser);
7888 for (i = 0; i < 3; i++)
7890 sizeof_arg[i] = NULL_TREE;
7891 sizeof_arg_loc[i] = UNKNOWN_LOCATION;
7893 literal_zero_mask = 0;
7894 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
7895 exprlist = NULL;
7896 else
7897 exprlist = c_parser_expr_list (parser, true, false, &origtypes,
7898 sizeof_arg_loc, sizeof_arg,
7899 &arg_loc, &literal_zero_mask);
7900 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7901 "expected %<)%>");
7902 orig_expr = expr;
7903 mark_exp_read (expr.value);
7904 if (warn_sizeof_pointer_memaccess)
7905 sizeof_pointer_memaccess_warning (sizeof_arg_loc,
7906 expr.value, exprlist,
7907 sizeof_arg,
7908 sizeof_ptr_memacc_comptypes);
7909 if (warn_memset_transposed_args
7910 && TREE_CODE (expr.value) == FUNCTION_DECL
7911 && DECL_BUILT_IN_CLASS (expr.value) == BUILT_IN_NORMAL
7912 && DECL_FUNCTION_CODE (expr.value) == BUILT_IN_MEMSET
7913 && vec_safe_length (exprlist) == 3
7914 && integer_zerop ((*exprlist)[2])
7915 && (literal_zero_mask & (1 << 2)) != 0
7916 && (!integer_zerop ((*exprlist)[1])
7917 || (literal_zero_mask & (1 << 1)) == 0))
7918 warning_at (expr_loc, OPT_Wmemset_transposed_args,
7919 "%<memset%> used with constant zero length parameter; "
7920 "this could be due to transposed parameters");
7922 expr.value
7923 = c_build_function_call_vec (expr_loc, arg_loc, expr.value,
7924 exprlist, origtypes);
7925 expr.original_code = ERROR_MARK;
7926 if (TREE_CODE (expr.value) == INTEGER_CST
7927 && TREE_CODE (orig_expr.value) == FUNCTION_DECL
7928 && DECL_BUILT_IN_CLASS (orig_expr.value) == BUILT_IN_NORMAL
7929 && DECL_FUNCTION_CODE (orig_expr.value) == BUILT_IN_CONSTANT_P)
7930 expr.original_code = C_MAYBE_CONST_EXPR;
7931 expr.original_type = NULL;
7932 if (exprlist)
7934 release_tree_vector (exprlist);
7935 release_tree_vector (origtypes);
7937 arg_loc.release ();
7938 break;
7939 case CPP_DOT:
7940 /* Structure element reference. */
7941 c_parser_consume_token (parser);
7942 expr = default_function_array_conversion (expr_loc, expr);
7943 if (c_parser_next_token_is (parser, CPP_NAME))
7944 ident = c_parser_peek_token (parser)->value;
7945 else
7947 c_parser_error (parser, "expected identifier");
7948 expr.value = error_mark_node;
7949 expr.original_code = ERROR_MARK;
7950 expr.original_type = NULL;
7951 return expr;
7953 c_parser_consume_token (parser);
7954 expr.value = build_component_ref (op_loc, expr.value, ident);
7955 expr.original_code = ERROR_MARK;
7956 if (TREE_CODE (expr.value) != COMPONENT_REF)
7957 expr.original_type = NULL;
7958 else
7960 /* Remember the original type of a bitfield. */
7961 tree field = TREE_OPERAND (expr.value, 1);
7962 if (TREE_CODE (field) != FIELD_DECL)
7963 expr.original_type = NULL;
7964 else
7965 expr.original_type = DECL_BIT_FIELD_TYPE (field);
7967 break;
7968 case CPP_DEREF:
7969 /* Structure element reference. */
7970 c_parser_consume_token (parser);
7971 expr = convert_lvalue_to_rvalue (expr_loc, expr, true, false);
7972 if (c_parser_next_token_is (parser, CPP_NAME))
7973 ident = c_parser_peek_token (parser)->value;
7974 else
7976 c_parser_error (parser, "expected identifier");
7977 expr.value = error_mark_node;
7978 expr.original_code = ERROR_MARK;
7979 expr.original_type = NULL;
7980 return expr;
7982 c_parser_consume_token (parser);
7983 expr.value = build_component_ref (op_loc,
7984 build_indirect_ref (op_loc,
7985 expr.value,
7986 RO_ARROW),
7987 ident);
7988 expr.original_code = ERROR_MARK;
7989 if (TREE_CODE (expr.value) != COMPONENT_REF)
7990 expr.original_type = NULL;
7991 else
7993 /* Remember the original type of a bitfield. */
7994 tree field = TREE_OPERAND (expr.value, 1);
7995 if (TREE_CODE (field) != FIELD_DECL)
7996 expr.original_type = NULL;
7997 else
7998 expr.original_type = DECL_BIT_FIELD_TYPE (field);
8000 break;
8001 case CPP_PLUS_PLUS:
8002 /* Postincrement. */
8003 c_parser_consume_token (parser);
8004 /* If the expressions have array notations, we expand them. */
8005 if (flag_cilkplus
8006 && TREE_CODE (expr.value) == ARRAY_NOTATION_REF)
8007 expr = fix_array_notation_expr (expr_loc, POSTINCREMENT_EXPR, expr);
8008 else
8010 expr = default_function_array_read_conversion (expr_loc, expr);
8011 expr.value = build_unary_op (op_loc,
8012 POSTINCREMENT_EXPR, expr.value, 0);
8014 expr.original_code = ERROR_MARK;
8015 expr.original_type = NULL;
8016 break;
8017 case CPP_MINUS_MINUS:
8018 /* Postdecrement. */
8019 c_parser_consume_token (parser);
8020 /* If the expressions have array notations, we expand them. */
8021 if (flag_cilkplus
8022 && TREE_CODE (expr.value) == ARRAY_NOTATION_REF)
8023 expr = fix_array_notation_expr (expr_loc, POSTDECREMENT_EXPR, expr);
8024 else
8026 expr = default_function_array_read_conversion (expr_loc, expr);
8027 expr.value = build_unary_op (op_loc,
8028 POSTDECREMENT_EXPR, expr.value, 0);
8030 expr.original_code = ERROR_MARK;
8031 expr.original_type = NULL;
8032 break;
8033 default:
8034 return expr;
8039 /* Parse an expression (C90 6.3.17, C99 6.5.17).
8041 expression:
8042 assignment-expression
8043 expression , assignment-expression
8046 static struct c_expr
8047 c_parser_expression (c_parser *parser)
8049 location_t tloc = c_parser_peek_token (parser)->location;
8050 struct c_expr expr;
8051 expr = c_parser_expr_no_commas (parser, NULL);
8052 if (c_parser_next_token_is (parser, CPP_COMMA))
8053 expr = convert_lvalue_to_rvalue (tloc, expr, true, false);
8054 while (c_parser_next_token_is (parser, CPP_COMMA))
8056 struct c_expr next;
8057 tree lhsval;
8058 location_t loc = c_parser_peek_token (parser)->location;
8059 location_t expr_loc;
8060 c_parser_consume_token (parser);
8061 expr_loc = c_parser_peek_token (parser)->location;
8062 lhsval = expr.value;
8063 while (TREE_CODE (lhsval) == COMPOUND_EXPR)
8064 lhsval = TREE_OPERAND (lhsval, 1);
8065 if (DECL_P (lhsval) || handled_component_p (lhsval))
8066 mark_exp_read (lhsval);
8067 next = c_parser_expr_no_commas (parser, NULL);
8068 next = convert_lvalue_to_rvalue (expr_loc, next, true, false);
8069 expr.value = build_compound_expr (loc, expr.value, next.value);
8070 expr.original_code = COMPOUND_EXPR;
8071 expr.original_type = next.original_type;
8073 return expr;
8076 /* Parse an expression and convert functions or arrays to pointers and
8077 lvalues to rvalues. */
8079 static struct c_expr
8080 c_parser_expression_conv (c_parser *parser)
8082 struct c_expr expr;
8083 location_t loc = c_parser_peek_token (parser)->location;
8084 expr = c_parser_expression (parser);
8085 expr = convert_lvalue_to_rvalue (loc, expr, true, false);
8086 return expr;
8089 /* Helper function of c_parser_expr_list. Check if IDXth (0 based)
8090 argument is a literal zero alone and if so, set it in literal_zero_mask. */
8092 static inline void
8093 c_parser_check_literal_zero (c_parser *parser, unsigned *literal_zero_mask,
8094 unsigned int idx)
8096 if (idx >= HOST_BITS_PER_INT)
8097 return;
8099 c_token *tok = c_parser_peek_token (parser);
8100 switch (tok->type)
8102 case CPP_NUMBER:
8103 case CPP_CHAR:
8104 case CPP_WCHAR:
8105 case CPP_CHAR16:
8106 case CPP_CHAR32:
8107 /* If a parameter is literal zero alone, remember it
8108 for -Wmemset-transposed-args warning. */
8109 if (integer_zerop (tok->value)
8110 && !TREE_OVERFLOW (tok->value)
8111 && (c_parser_peek_2nd_token (parser)->type == CPP_COMMA
8112 || c_parser_peek_2nd_token (parser)->type == CPP_CLOSE_PAREN))
8113 *literal_zero_mask |= 1U << idx;
8114 default:
8115 break;
8119 /* Parse a non-empty list of expressions. If CONVERT_P, convert
8120 functions and arrays to pointers and lvalues to rvalues. If
8121 FOLD_P, fold the expressions. If LOCATIONS is non-NULL, save the
8122 locations of function arguments into this vector.
8124 nonempty-expr-list:
8125 assignment-expression
8126 nonempty-expr-list , assignment-expression
8129 static vec<tree, va_gc> *
8130 c_parser_expr_list (c_parser *parser, bool convert_p, bool fold_p,
8131 vec<tree, va_gc> **p_orig_types,
8132 location_t *sizeof_arg_loc, tree *sizeof_arg,
8133 vec<location_t> *locations,
8134 unsigned int *literal_zero_mask)
8136 vec<tree, va_gc> *ret;
8137 vec<tree, va_gc> *orig_types;
8138 struct c_expr expr;
8139 location_t loc = c_parser_peek_token (parser)->location;
8140 location_t cur_sizeof_arg_loc = UNKNOWN_LOCATION;
8141 unsigned int idx = 0;
8143 ret = make_tree_vector ();
8144 if (p_orig_types == NULL)
8145 orig_types = NULL;
8146 else
8147 orig_types = make_tree_vector ();
8149 if (sizeof_arg != NULL
8150 && c_parser_next_token_is_keyword (parser, RID_SIZEOF))
8151 cur_sizeof_arg_loc = c_parser_peek_2nd_token (parser)->location;
8152 if (literal_zero_mask)
8153 c_parser_check_literal_zero (parser, literal_zero_mask, 0);
8154 expr = c_parser_expr_no_commas (parser, NULL);
8155 if (convert_p)
8156 expr = convert_lvalue_to_rvalue (loc, expr, true, true);
8157 if (fold_p)
8158 expr.value = c_fully_fold (expr.value, false, NULL);
8159 ret->quick_push (expr.value);
8160 if (orig_types)
8161 orig_types->quick_push (expr.original_type);
8162 if (locations)
8163 locations->safe_push (loc);
8164 if (sizeof_arg != NULL
8165 && cur_sizeof_arg_loc != UNKNOWN_LOCATION
8166 && expr.original_code == SIZEOF_EXPR)
8168 sizeof_arg[0] = c_last_sizeof_arg;
8169 sizeof_arg_loc[0] = cur_sizeof_arg_loc;
8171 while (c_parser_next_token_is (parser, CPP_COMMA))
8173 c_parser_consume_token (parser);
8174 loc = c_parser_peek_token (parser)->location;
8175 if (sizeof_arg != NULL
8176 && c_parser_next_token_is_keyword (parser, RID_SIZEOF))
8177 cur_sizeof_arg_loc = c_parser_peek_2nd_token (parser)->location;
8178 else
8179 cur_sizeof_arg_loc = UNKNOWN_LOCATION;
8180 if (literal_zero_mask)
8181 c_parser_check_literal_zero (parser, literal_zero_mask, idx + 1);
8182 expr = c_parser_expr_no_commas (parser, NULL);
8183 if (convert_p)
8184 expr = convert_lvalue_to_rvalue (loc, expr, true, true);
8185 if (fold_p)
8186 expr.value = c_fully_fold (expr.value, false, NULL);
8187 vec_safe_push (ret, expr.value);
8188 if (orig_types)
8189 vec_safe_push (orig_types, expr.original_type);
8190 if (locations)
8191 locations->safe_push (loc);
8192 if (++idx < 3
8193 && sizeof_arg != NULL
8194 && cur_sizeof_arg_loc != UNKNOWN_LOCATION
8195 && expr.original_code == SIZEOF_EXPR)
8197 sizeof_arg[idx] = c_last_sizeof_arg;
8198 sizeof_arg_loc[idx] = cur_sizeof_arg_loc;
8201 if (orig_types)
8202 *p_orig_types = orig_types;
8203 return ret;
8206 /* Parse Objective-C-specific constructs. */
8208 /* Parse an objc-class-definition.
8210 objc-class-definition:
8211 @interface identifier objc-superclass[opt] objc-protocol-refs[opt]
8212 objc-class-instance-variables[opt] objc-methodprotolist @end
8213 @implementation identifier objc-superclass[opt]
8214 objc-class-instance-variables[opt]
8215 @interface identifier ( identifier ) objc-protocol-refs[opt]
8216 objc-methodprotolist @end
8217 @interface identifier ( ) objc-protocol-refs[opt]
8218 objc-methodprotolist @end
8219 @implementation identifier ( identifier )
8221 objc-superclass:
8222 : identifier
8224 "@interface identifier (" must start "@interface identifier (
8225 identifier ) ...": objc-methodprotolist in the first production may
8226 not start with a parenthesized identifier as a declarator of a data
8227 definition with no declaration specifiers if the objc-superclass,
8228 objc-protocol-refs and objc-class-instance-variables are omitted. */
8230 static void
8231 c_parser_objc_class_definition (c_parser *parser, tree attributes)
8233 bool iface_p;
8234 tree id1;
8235 tree superclass;
8236 if (c_parser_next_token_is_keyword (parser, RID_AT_INTERFACE))
8237 iface_p = true;
8238 else if (c_parser_next_token_is_keyword (parser, RID_AT_IMPLEMENTATION))
8239 iface_p = false;
8240 else
8241 gcc_unreachable ();
8243 c_parser_consume_token (parser);
8244 if (c_parser_next_token_is_not (parser, CPP_NAME))
8246 c_parser_error (parser, "expected identifier");
8247 return;
8249 id1 = c_parser_peek_token (parser)->value;
8250 c_parser_consume_token (parser);
8251 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
8253 /* We have a category or class extension. */
8254 tree id2;
8255 tree proto = NULL_TREE;
8256 c_parser_consume_token (parser);
8257 if (c_parser_next_token_is_not (parser, CPP_NAME))
8259 if (iface_p && c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
8261 /* We have a class extension. */
8262 id2 = NULL_TREE;
8264 else
8266 c_parser_error (parser, "expected identifier or %<)%>");
8267 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
8268 return;
8271 else
8273 id2 = c_parser_peek_token (parser)->value;
8274 c_parser_consume_token (parser);
8276 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
8277 if (!iface_p)
8279 objc_start_category_implementation (id1, id2);
8280 return;
8282 if (c_parser_next_token_is (parser, CPP_LESS))
8283 proto = c_parser_objc_protocol_refs (parser);
8284 objc_start_category_interface (id1, id2, proto, attributes);
8285 c_parser_objc_methodprotolist (parser);
8286 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
8287 objc_finish_interface ();
8288 return;
8290 if (c_parser_next_token_is (parser, CPP_COLON))
8292 c_parser_consume_token (parser);
8293 if (c_parser_next_token_is_not (parser, CPP_NAME))
8295 c_parser_error (parser, "expected identifier");
8296 return;
8298 superclass = c_parser_peek_token (parser)->value;
8299 c_parser_consume_token (parser);
8301 else
8302 superclass = NULL_TREE;
8303 if (iface_p)
8305 tree proto = NULL_TREE;
8306 if (c_parser_next_token_is (parser, CPP_LESS))
8307 proto = c_parser_objc_protocol_refs (parser);
8308 objc_start_class_interface (id1, superclass, proto, attributes);
8310 else
8311 objc_start_class_implementation (id1, superclass);
8312 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
8313 c_parser_objc_class_instance_variables (parser);
8314 if (iface_p)
8316 objc_continue_interface ();
8317 c_parser_objc_methodprotolist (parser);
8318 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
8319 objc_finish_interface ();
8321 else
8323 objc_continue_implementation ();
8324 return;
8328 /* Parse objc-class-instance-variables.
8330 objc-class-instance-variables:
8331 { objc-instance-variable-decl-list[opt] }
8333 objc-instance-variable-decl-list:
8334 objc-visibility-spec
8335 objc-instance-variable-decl ;
8337 objc-instance-variable-decl-list objc-visibility-spec
8338 objc-instance-variable-decl-list objc-instance-variable-decl ;
8339 objc-instance-variable-decl-list ;
8341 objc-visibility-spec:
8342 @private
8343 @protected
8344 @public
8346 objc-instance-variable-decl:
8347 struct-declaration
8350 static void
8351 c_parser_objc_class_instance_variables (c_parser *parser)
8353 gcc_assert (c_parser_next_token_is (parser, CPP_OPEN_BRACE));
8354 c_parser_consume_token (parser);
8355 while (c_parser_next_token_is_not (parser, CPP_EOF))
8357 tree decls;
8358 /* Parse any stray semicolon. */
8359 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
8361 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
8362 "extra semicolon");
8363 c_parser_consume_token (parser);
8364 continue;
8366 /* Stop if at the end of the instance variables. */
8367 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
8369 c_parser_consume_token (parser);
8370 break;
8372 /* Parse any objc-visibility-spec. */
8373 if (c_parser_next_token_is_keyword (parser, RID_AT_PRIVATE))
8375 c_parser_consume_token (parser);
8376 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
8377 continue;
8379 else if (c_parser_next_token_is_keyword (parser, RID_AT_PROTECTED))
8381 c_parser_consume_token (parser);
8382 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
8383 continue;
8385 else if (c_parser_next_token_is_keyword (parser, RID_AT_PUBLIC))
8387 c_parser_consume_token (parser);
8388 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
8389 continue;
8391 else if (c_parser_next_token_is_keyword (parser, RID_AT_PACKAGE))
8393 c_parser_consume_token (parser);
8394 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
8395 continue;
8397 else if (c_parser_next_token_is (parser, CPP_PRAGMA))
8399 c_parser_pragma (parser, pragma_external);
8400 continue;
8403 /* Parse some comma-separated declarations. */
8404 decls = c_parser_struct_declaration (parser);
8405 if (decls == NULL)
8407 /* There is a syntax error. We want to skip the offending
8408 tokens up to the next ';' (included) or '}'
8409 (excluded). */
8411 /* First, skip manually a ')' or ']'. This is because they
8412 reduce the nesting level, so c_parser_skip_until_found()
8413 wouldn't be able to skip past them. */
8414 c_token *token = c_parser_peek_token (parser);
8415 if (token->type == CPP_CLOSE_PAREN || token->type == CPP_CLOSE_SQUARE)
8416 c_parser_consume_token (parser);
8418 /* Then, do the standard skipping. */
8419 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
8421 /* We hopefully recovered. Start normal parsing again. */
8422 parser->error = false;
8423 continue;
8425 else
8427 /* Comma-separated instance variables are chained together
8428 in reverse order; add them one by one. */
8429 tree ivar = nreverse (decls);
8430 for (; ivar; ivar = DECL_CHAIN (ivar))
8431 objc_add_instance_variable (copy_node (ivar));
8433 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
8437 /* Parse an objc-class-declaration.
8439 objc-class-declaration:
8440 @class identifier-list ;
8443 static void
8444 c_parser_objc_class_declaration (c_parser *parser)
8446 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_CLASS));
8447 c_parser_consume_token (parser);
8448 /* Any identifiers, including those declared as type names, are OK
8449 here. */
8450 while (true)
8452 tree id;
8453 if (c_parser_next_token_is_not (parser, CPP_NAME))
8455 c_parser_error (parser, "expected identifier");
8456 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
8457 parser->error = false;
8458 return;
8460 id = c_parser_peek_token (parser)->value;
8461 objc_declare_class (id);
8462 c_parser_consume_token (parser);
8463 if (c_parser_next_token_is (parser, CPP_COMMA))
8464 c_parser_consume_token (parser);
8465 else
8466 break;
8468 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
8471 /* Parse an objc-alias-declaration.
8473 objc-alias-declaration:
8474 @compatibility_alias identifier identifier ;
8477 static void
8478 c_parser_objc_alias_declaration (c_parser *parser)
8480 tree id1, id2;
8481 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_ALIAS));
8482 c_parser_consume_token (parser);
8483 if (c_parser_next_token_is_not (parser, CPP_NAME))
8485 c_parser_error (parser, "expected identifier");
8486 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
8487 return;
8489 id1 = c_parser_peek_token (parser)->value;
8490 c_parser_consume_token (parser);
8491 if (c_parser_next_token_is_not (parser, CPP_NAME))
8493 c_parser_error (parser, "expected identifier");
8494 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
8495 return;
8497 id2 = c_parser_peek_token (parser)->value;
8498 c_parser_consume_token (parser);
8499 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
8500 objc_declare_alias (id1, id2);
8503 /* Parse an objc-protocol-definition.
8505 objc-protocol-definition:
8506 @protocol identifier objc-protocol-refs[opt] objc-methodprotolist @end
8507 @protocol identifier-list ;
8509 "@protocol identifier ;" should be resolved as "@protocol
8510 identifier-list ;": objc-methodprotolist may not start with a
8511 semicolon in the first alternative if objc-protocol-refs are
8512 omitted. */
8514 static void
8515 c_parser_objc_protocol_definition (c_parser *parser, tree attributes)
8517 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_PROTOCOL));
8519 c_parser_consume_token (parser);
8520 if (c_parser_next_token_is_not (parser, CPP_NAME))
8522 c_parser_error (parser, "expected identifier");
8523 return;
8525 if (c_parser_peek_2nd_token (parser)->type == CPP_COMMA
8526 || c_parser_peek_2nd_token (parser)->type == CPP_SEMICOLON)
8528 /* Any identifiers, including those declared as type names, are
8529 OK here. */
8530 while (true)
8532 tree id;
8533 if (c_parser_next_token_is_not (parser, CPP_NAME))
8535 c_parser_error (parser, "expected identifier");
8536 break;
8538 id = c_parser_peek_token (parser)->value;
8539 objc_declare_protocol (id, attributes);
8540 c_parser_consume_token (parser);
8541 if (c_parser_next_token_is (parser, CPP_COMMA))
8542 c_parser_consume_token (parser);
8543 else
8544 break;
8546 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
8548 else
8550 tree id = c_parser_peek_token (parser)->value;
8551 tree proto = NULL_TREE;
8552 c_parser_consume_token (parser);
8553 if (c_parser_next_token_is (parser, CPP_LESS))
8554 proto = c_parser_objc_protocol_refs (parser);
8555 parser->objc_pq_context = true;
8556 objc_start_protocol (id, proto, attributes);
8557 c_parser_objc_methodprotolist (parser);
8558 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
8559 parser->objc_pq_context = false;
8560 objc_finish_interface ();
8564 /* Parse an objc-method-type.
8566 objc-method-type:
8570 Return true if it is a class method (+) and false if it is
8571 an instance method (-).
8573 static inline bool
8574 c_parser_objc_method_type (c_parser *parser)
8576 switch (c_parser_peek_token (parser)->type)
8578 case CPP_PLUS:
8579 c_parser_consume_token (parser);
8580 return true;
8581 case CPP_MINUS:
8582 c_parser_consume_token (parser);
8583 return false;
8584 default:
8585 gcc_unreachable ();
8589 /* Parse an objc-method-definition.
8591 objc-method-definition:
8592 objc-method-type objc-method-decl ;[opt] compound-statement
8595 static void
8596 c_parser_objc_method_definition (c_parser *parser)
8598 bool is_class_method = c_parser_objc_method_type (parser);
8599 tree decl, attributes = NULL_TREE, expr = NULL_TREE;
8600 parser->objc_pq_context = true;
8601 decl = c_parser_objc_method_decl (parser, is_class_method, &attributes,
8602 &expr);
8603 if (decl == error_mark_node)
8604 return; /* Bail here. */
8606 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
8608 c_parser_consume_token (parser);
8609 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
8610 "extra semicolon in method definition specified");
8613 if (!c_parser_next_token_is (parser, CPP_OPEN_BRACE))
8615 c_parser_error (parser, "expected %<{%>");
8616 return;
8619 parser->objc_pq_context = false;
8620 if (objc_start_method_definition (is_class_method, decl, attributes, expr))
8622 add_stmt (c_parser_compound_statement (parser));
8623 objc_finish_method_definition (current_function_decl);
8625 else
8627 /* This code is executed when we find a method definition
8628 outside of an @implementation context (or invalid for other
8629 reasons). Parse the method (to keep going) but do not emit
8630 any code.
8632 c_parser_compound_statement (parser);
8636 /* Parse an objc-methodprotolist.
8638 objc-methodprotolist:
8639 empty
8640 objc-methodprotolist objc-methodproto
8641 objc-methodprotolist declaration
8642 objc-methodprotolist ;
8643 @optional
8644 @required
8646 The declaration is a data definition, which may be missing
8647 declaration specifiers under the same rules and diagnostics as
8648 other data definitions outside functions, and the stray semicolon
8649 is diagnosed the same way as a stray semicolon outside a
8650 function. */
8652 static void
8653 c_parser_objc_methodprotolist (c_parser *parser)
8655 while (true)
8657 /* The list is terminated by @end. */
8658 switch (c_parser_peek_token (parser)->type)
8660 case CPP_SEMICOLON:
8661 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
8662 "ISO C does not allow extra %<;%> outside of a function");
8663 c_parser_consume_token (parser);
8664 break;
8665 case CPP_PLUS:
8666 case CPP_MINUS:
8667 c_parser_objc_methodproto (parser);
8668 break;
8669 case CPP_PRAGMA:
8670 c_parser_pragma (parser, pragma_external);
8671 break;
8672 case CPP_EOF:
8673 return;
8674 default:
8675 if (c_parser_next_token_is_keyword (parser, RID_AT_END))
8676 return;
8677 else if (c_parser_next_token_is_keyword (parser, RID_AT_PROPERTY))
8678 c_parser_objc_at_property_declaration (parser);
8679 else if (c_parser_next_token_is_keyword (parser, RID_AT_OPTIONAL))
8681 objc_set_method_opt (true);
8682 c_parser_consume_token (parser);
8684 else if (c_parser_next_token_is_keyword (parser, RID_AT_REQUIRED))
8686 objc_set_method_opt (false);
8687 c_parser_consume_token (parser);
8689 else
8690 c_parser_declaration_or_fndef (parser, false, false, true,
8691 false, true, NULL, vNULL);
8692 break;
8697 /* Parse an objc-methodproto.
8699 objc-methodproto:
8700 objc-method-type objc-method-decl ;
8703 static void
8704 c_parser_objc_methodproto (c_parser *parser)
8706 bool is_class_method = c_parser_objc_method_type (parser);
8707 tree decl, attributes = NULL_TREE;
8709 /* Remember protocol qualifiers in prototypes. */
8710 parser->objc_pq_context = true;
8711 decl = c_parser_objc_method_decl (parser, is_class_method, &attributes,
8712 NULL);
8713 /* Forget protocol qualifiers now. */
8714 parser->objc_pq_context = false;
8716 /* Do not allow the presence of attributes to hide an erroneous
8717 method implementation in the interface section. */
8718 if (!c_parser_next_token_is (parser, CPP_SEMICOLON))
8720 c_parser_error (parser, "expected %<;%>");
8721 return;
8724 if (decl != error_mark_node)
8725 objc_add_method_declaration (is_class_method, decl, attributes);
8727 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
8730 /* If we are at a position that method attributes may be present, check that
8731 there are not any parsed already (a syntax error) and then collect any
8732 specified at the current location. Finally, if new attributes were present,
8733 check that the next token is legal ( ';' for decls and '{' for defs). */
8735 static bool
8736 c_parser_objc_maybe_method_attributes (c_parser* parser, tree* attributes)
8738 bool bad = false;
8739 if (*attributes)
8741 c_parser_error (parser,
8742 "method attributes must be specified at the end only");
8743 *attributes = NULL_TREE;
8744 bad = true;
8747 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
8748 *attributes = c_parser_attributes (parser);
8750 /* If there were no attributes here, just report any earlier error. */
8751 if (*attributes == NULL_TREE || bad)
8752 return bad;
8754 /* If the attributes are followed by a ; or {, then just report any earlier
8755 error. */
8756 if (c_parser_next_token_is (parser, CPP_SEMICOLON)
8757 || c_parser_next_token_is (parser, CPP_OPEN_BRACE))
8758 return bad;
8760 /* We've got attributes, but not at the end. */
8761 c_parser_error (parser,
8762 "expected %<;%> or %<{%> after method attribute definition");
8763 return true;
8766 /* Parse an objc-method-decl.
8768 objc-method-decl:
8769 ( objc-type-name ) objc-selector
8770 objc-selector
8771 ( objc-type-name ) objc-keyword-selector objc-optparmlist
8772 objc-keyword-selector objc-optparmlist
8773 attributes
8775 objc-keyword-selector:
8776 objc-keyword-decl
8777 objc-keyword-selector objc-keyword-decl
8779 objc-keyword-decl:
8780 objc-selector : ( objc-type-name ) identifier
8781 objc-selector : identifier
8782 : ( objc-type-name ) identifier
8783 : identifier
8785 objc-optparmlist:
8786 objc-optparms objc-optellipsis
8788 objc-optparms:
8789 empty
8790 objc-opt-parms , parameter-declaration
8792 objc-optellipsis:
8793 empty
8794 , ...
8797 static tree
8798 c_parser_objc_method_decl (c_parser *parser, bool is_class_method,
8799 tree *attributes, tree *expr)
8801 tree type = NULL_TREE;
8802 tree sel;
8803 tree parms = NULL_TREE;
8804 bool ellipsis = false;
8805 bool attr_err = false;
8807 *attributes = NULL_TREE;
8808 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
8810 c_parser_consume_token (parser);
8811 type = c_parser_objc_type_name (parser);
8812 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
8814 sel = c_parser_objc_selector (parser);
8815 /* If there is no selector, or a colon follows, we have an
8816 objc-keyword-selector. If there is a selector, and a colon does
8817 not follow, that selector ends the objc-method-decl. */
8818 if (!sel || c_parser_next_token_is (parser, CPP_COLON))
8820 tree tsel = sel;
8821 tree list = NULL_TREE;
8822 while (true)
8824 tree atype = NULL_TREE, id, keyworddecl;
8825 tree param_attr = NULL_TREE;
8826 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
8827 break;
8828 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
8830 c_parser_consume_token (parser);
8831 atype = c_parser_objc_type_name (parser);
8832 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
8833 "expected %<)%>");
8835 /* New ObjC allows attributes on method parameters. */
8836 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
8837 param_attr = c_parser_attributes (parser);
8838 if (c_parser_next_token_is_not (parser, CPP_NAME))
8840 c_parser_error (parser, "expected identifier");
8841 return error_mark_node;
8843 id = c_parser_peek_token (parser)->value;
8844 c_parser_consume_token (parser);
8845 keyworddecl = objc_build_keyword_decl (tsel, atype, id, param_attr);
8846 list = chainon (list, keyworddecl);
8847 tsel = c_parser_objc_selector (parser);
8848 if (!tsel && c_parser_next_token_is_not (parser, CPP_COLON))
8849 break;
8852 attr_err |= c_parser_objc_maybe_method_attributes (parser, attributes) ;
8854 /* Parse the optional parameter list. Optional Objective-C
8855 method parameters follow the C syntax, and may include '...'
8856 to denote a variable number of arguments. */
8857 parms = make_node (TREE_LIST);
8858 while (c_parser_next_token_is (parser, CPP_COMMA))
8860 struct c_parm *parm;
8861 c_parser_consume_token (parser);
8862 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
8864 ellipsis = true;
8865 c_parser_consume_token (parser);
8866 attr_err |= c_parser_objc_maybe_method_attributes
8867 (parser, attributes) ;
8868 break;
8870 parm = c_parser_parameter_declaration (parser, NULL_TREE);
8871 if (parm == NULL)
8872 break;
8873 parms = chainon (parms,
8874 build_tree_list (NULL_TREE, grokparm (parm, expr)));
8876 sel = list;
8878 else
8879 attr_err |= c_parser_objc_maybe_method_attributes (parser, attributes) ;
8881 if (sel == NULL)
8883 c_parser_error (parser, "objective-c method declaration is expected");
8884 return error_mark_node;
8887 if (attr_err)
8888 return error_mark_node;
8890 return objc_build_method_signature (is_class_method, type, sel, parms, ellipsis);
8893 /* Parse an objc-type-name.
8895 objc-type-name:
8896 objc-type-qualifiers[opt] type-name
8897 objc-type-qualifiers[opt]
8899 objc-type-qualifiers:
8900 objc-type-qualifier
8901 objc-type-qualifiers objc-type-qualifier
8903 objc-type-qualifier: one of
8904 in out inout bycopy byref oneway
8907 static tree
8908 c_parser_objc_type_name (c_parser *parser)
8910 tree quals = NULL_TREE;
8911 struct c_type_name *type_name = NULL;
8912 tree type = NULL_TREE;
8913 while (true)
8915 c_token *token = c_parser_peek_token (parser);
8916 if (token->type == CPP_KEYWORD
8917 && (token->keyword == RID_IN
8918 || token->keyword == RID_OUT
8919 || token->keyword == RID_INOUT
8920 || token->keyword == RID_BYCOPY
8921 || token->keyword == RID_BYREF
8922 || token->keyword == RID_ONEWAY))
8924 quals = chainon (build_tree_list (NULL_TREE, token->value), quals);
8925 c_parser_consume_token (parser);
8927 else
8928 break;
8930 if (c_parser_next_tokens_start_typename (parser, cla_prefer_type))
8931 type_name = c_parser_type_name (parser);
8932 if (type_name)
8933 type = groktypename (type_name, NULL, NULL);
8935 /* If the type is unknown, and error has already been produced and
8936 we need to recover from the error. In that case, use NULL_TREE
8937 for the type, as if no type had been specified; this will use the
8938 default type ('id') which is good for error recovery. */
8939 if (type == error_mark_node)
8940 type = NULL_TREE;
8942 return build_tree_list (quals, type);
8945 /* Parse objc-protocol-refs.
8947 objc-protocol-refs:
8948 < identifier-list >
8951 static tree
8952 c_parser_objc_protocol_refs (c_parser *parser)
8954 tree list = NULL_TREE;
8955 gcc_assert (c_parser_next_token_is (parser, CPP_LESS));
8956 c_parser_consume_token (parser);
8957 /* Any identifiers, including those declared as type names, are OK
8958 here. */
8959 while (true)
8961 tree id;
8962 if (c_parser_next_token_is_not (parser, CPP_NAME))
8964 c_parser_error (parser, "expected identifier");
8965 break;
8967 id = c_parser_peek_token (parser)->value;
8968 list = chainon (list, build_tree_list (NULL_TREE, id));
8969 c_parser_consume_token (parser);
8970 if (c_parser_next_token_is (parser, CPP_COMMA))
8971 c_parser_consume_token (parser);
8972 else
8973 break;
8975 c_parser_require (parser, CPP_GREATER, "expected %<>%>");
8976 return list;
8979 /* Parse an objc-try-catch-finally-statement.
8981 objc-try-catch-finally-statement:
8982 @try compound-statement objc-catch-list[opt]
8983 @try compound-statement objc-catch-list[opt] @finally compound-statement
8985 objc-catch-list:
8986 @catch ( objc-catch-parameter-declaration ) compound-statement
8987 objc-catch-list @catch ( objc-catch-parameter-declaration ) compound-statement
8989 objc-catch-parameter-declaration:
8990 parameter-declaration
8991 '...'
8993 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
8995 PS: This function is identical to cp_parser_objc_try_catch_finally_statement
8996 for C++. Keep them in sync. */
8998 static void
8999 c_parser_objc_try_catch_finally_statement (c_parser *parser)
9001 location_t location;
9002 tree stmt;
9004 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_TRY));
9005 c_parser_consume_token (parser);
9006 location = c_parser_peek_token (parser)->location;
9007 objc_maybe_warn_exceptions (location);
9008 stmt = c_parser_compound_statement (parser);
9009 objc_begin_try_stmt (location, stmt);
9011 while (c_parser_next_token_is_keyword (parser, RID_AT_CATCH))
9013 struct c_parm *parm;
9014 tree parameter_declaration = error_mark_node;
9015 bool seen_open_paren = false;
9017 c_parser_consume_token (parser);
9018 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
9019 seen_open_paren = true;
9020 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
9022 /* We have "@catch (...)" (where the '...' are literally
9023 what is in the code). Skip the '...'.
9024 parameter_declaration is set to NULL_TREE, and
9025 objc_being_catch_clauses() knows that that means
9026 '...'. */
9027 c_parser_consume_token (parser);
9028 parameter_declaration = NULL_TREE;
9030 else
9032 /* We have "@catch (NSException *exception)" or something
9033 like that. Parse the parameter declaration. */
9034 parm = c_parser_parameter_declaration (parser, NULL_TREE);
9035 if (parm == NULL)
9036 parameter_declaration = error_mark_node;
9037 else
9038 parameter_declaration = grokparm (parm, NULL);
9040 if (seen_open_paren)
9041 c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
9042 else
9044 /* If there was no open parenthesis, we are recovering from
9045 an error, and we are trying to figure out what mistake
9046 the user has made. */
9048 /* If there is an immediate closing parenthesis, the user
9049 probably forgot the opening one (ie, they typed "@catch
9050 NSException *e)". Parse the closing parenthesis and keep
9051 going. */
9052 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
9053 c_parser_consume_token (parser);
9055 /* If these is no immediate closing parenthesis, the user
9056 probably doesn't know that parenthesis are required at
9057 all (ie, they typed "@catch NSException *e"). So, just
9058 forget about the closing parenthesis and keep going. */
9060 objc_begin_catch_clause (parameter_declaration);
9061 if (c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
9062 c_parser_compound_statement_nostart (parser);
9063 objc_finish_catch_clause ();
9065 if (c_parser_next_token_is_keyword (parser, RID_AT_FINALLY))
9067 c_parser_consume_token (parser);
9068 location = c_parser_peek_token (parser)->location;
9069 stmt = c_parser_compound_statement (parser);
9070 objc_build_finally_clause (location, stmt);
9072 objc_finish_try_stmt ();
9075 /* Parse an objc-synchronized-statement.
9077 objc-synchronized-statement:
9078 @synchronized ( expression ) compound-statement
9081 static void
9082 c_parser_objc_synchronized_statement (c_parser *parser)
9084 location_t loc;
9085 tree expr, stmt;
9086 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_SYNCHRONIZED));
9087 c_parser_consume_token (parser);
9088 loc = c_parser_peek_token (parser)->location;
9089 objc_maybe_warn_exceptions (loc);
9090 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
9092 struct c_expr ce = c_parser_expression (parser);
9093 ce = convert_lvalue_to_rvalue (loc, ce, false, false);
9094 expr = ce.value;
9095 expr = c_fully_fold (expr, false, NULL);
9096 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
9098 else
9099 expr = error_mark_node;
9100 stmt = c_parser_compound_statement (parser);
9101 objc_build_synchronized (loc, expr, stmt);
9104 /* Parse an objc-selector; return NULL_TREE without an error if the
9105 next token is not an objc-selector.
9107 objc-selector:
9108 identifier
9109 one of
9110 enum struct union if else while do for switch case default
9111 break continue return goto asm sizeof typeof __alignof
9112 unsigned long const short volatile signed restrict _Complex
9113 in out inout bycopy byref oneway int char float double void _Bool
9114 _Atomic
9116 ??? Why this selection of keywords but not, for example, storage
9117 class specifiers? */
9119 static tree
9120 c_parser_objc_selector (c_parser *parser)
9122 c_token *token = c_parser_peek_token (parser);
9123 tree value = token->value;
9124 if (token->type == CPP_NAME)
9126 c_parser_consume_token (parser);
9127 return value;
9129 if (token->type != CPP_KEYWORD)
9130 return NULL_TREE;
9131 switch (token->keyword)
9133 case RID_ENUM:
9134 case RID_STRUCT:
9135 case RID_UNION:
9136 case RID_IF:
9137 case RID_ELSE:
9138 case RID_WHILE:
9139 case RID_DO:
9140 case RID_FOR:
9141 case RID_SWITCH:
9142 case RID_CASE:
9143 case RID_DEFAULT:
9144 case RID_BREAK:
9145 case RID_CONTINUE:
9146 case RID_RETURN:
9147 case RID_GOTO:
9148 case RID_ASM:
9149 case RID_SIZEOF:
9150 case RID_TYPEOF:
9151 case RID_ALIGNOF:
9152 case RID_UNSIGNED:
9153 case RID_LONG:
9154 case RID_CONST:
9155 case RID_SHORT:
9156 case RID_VOLATILE:
9157 case RID_SIGNED:
9158 case RID_RESTRICT:
9159 case RID_COMPLEX:
9160 case RID_IN:
9161 case RID_OUT:
9162 case RID_INOUT:
9163 case RID_BYCOPY:
9164 case RID_BYREF:
9165 case RID_ONEWAY:
9166 case RID_INT:
9167 case RID_CHAR:
9168 case RID_FLOAT:
9169 case RID_DOUBLE:
9170 case RID_VOID:
9171 case RID_BOOL:
9172 case RID_ATOMIC:
9173 case RID_AUTO_TYPE:
9174 case RID_INT_N_0:
9175 case RID_INT_N_1:
9176 case RID_INT_N_2:
9177 case RID_INT_N_3:
9178 c_parser_consume_token (parser);
9179 return value;
9180 default:
9181 return NULL_TREE;
9185 /* Parse an objc-selector-arg.
9187 objc-selector-arg:
9188 objc-selector
9189 objc-keywordname-list
9191 objc-keywordname-list:
9192 objc-keywordname
9193 objc-keywordname-list objc-keywordname
9195 objc-keywordname:
9196 objc-selector :
9200 static tree
9201 c_parser_objc_selector_arg (c_parser *parser)
9203 tree sel = c_parser_objc_selector (parser);
9204 tree list = NULL_TREE;
9205 if (sel && c_parser_next_token_is_not (parser, CPP_COLON))
9206 return sel;
9207 while (true)
9209 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
9210 return list;
9211 list = chainon (list, build_tree_list (sel, NULL_TREE));
9212 sel = c_parser_objc_selector (parser);
9213 if (!sel && c_parser_next_token_is_not (parser, CPP_COLON))
9214 break;
9216 return list;
9219 /* Parse an objc-receiver.
9221 objc-receiver:
9222 expression
9223 class-name
9224 type-name
9227 static tree
9228 c_parser_objc_receiver (c_parser *parser)
9230 location_t loc = c_parser_peek_token (parser)->location;
9232 if (c_parser_peek_token (parser)->type == CPP_NAME
9233 && (c_parser_peek_token (parser)->id_kind == C_ID_TYPENAME
9234 || c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME))
9236 tree id = c_parser_peek_token (parser)->value;
9237 c_parser_consume_token (parser);
9238 return objc_get_class_reference (id);
9240 struct c_expr ce = c_parser_expression (parser);
9241 ce = convert_lvalue_to_rvalue (loc, ce, false, false);
9242 return c_fully_fold (ce.value, false, NULL);
9245 /* Parse objc-message-args.
9247 objc-message-args:
9248 objc-selector
9249 objc-keywordarg-list
9251 objc-keywordarg-list:
9252 objc-keywordarg
9253 objc-keywordarg-list objc-keywordarg
9255 objc-keywordarg:
9256 objc-selector : objc-keywordexpr
9257 : objc-keywordexpr
9260 static tree
9261 c_parser_objc_message_args (c_parser *parser)
9263 tree sel = c_parser_objc_selector (parser);
9264 tree list = NULL_TREE;
9265 if (sel && c_parser_next_token_is_not (parser, CPP_COLON))
9266 return sel;
9267 while (true)
9269 tree keywordexpr;
9270 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
9271 return error_mark_node;
9272 keywordexpr = c_parser_objc_keywordexpr (parser);
9273 list = chainon (list, build_tree_list (sel, keywordexpr));
9274 sel = c_parser_objc_selector (parser);
9275 if (!sel && c_parser_next_token_is_not (parser, CPP_COLON))
9276 break;
9278 return list;
9281 /* Parse an objc-keywordexpr.
9283 objc-keywordexpr:
9284 nonempty-expr-list
9287 static tree
9288 c_parser_objc_keywordexpr (c_parser *parser)
9290 tree ret;
9291 vec<tree, va_gc> *expr_list = c_parser_expr_list (parser, true, true,
9292 NULL, NULL, NULL, NULL);
9293 if (vec_safe_length (expr_list) == 1)
9295 /* Just return the expression, remove a level of
9296 indirection. */
9297 ret = (*expr_list)[0];
9299 else
9301 /* We have a comma expression, we will collapse later. */
9302 ret = build_tree_list_vec (expr_list);
9304 release_tree_vector (expr_list);
9305 return ret;
9308 /* A check, needed in several places, that ObjC interface, implementation or
9309 method definitions are not prefixed by incorrect items. */
9310 static bool
9311 c_parser_objc_diagnose_bad_element_prefix (c_parser *parser,
9312 struct c_declspecs *specs)
9314 if (!specs->declspecs_seen_p || specs->non_sc_seen_p
9315 || specs->typespec_kind != ctsk_none)
9317 c_parser_error (parser,
9318 "no type or storage class may be specified here,");
9319 c_parser_skip_to_end_of_block_or_statement (parser);
9320 return true;
9322 return false;
9325 /* Parse an Objective-C @property declaration. The syntax is:
9327 objc-property-declaration:
9328 '@property' objc-property-attributes[opt] struct-declaration ;
9330 objc-property-attributes:
9331 '(' objc-property-attribute-list ')'
9333 objc-property-attribute-list:
9334 objc-property-attribute
9335 objc-property-attribute-list, objc-property-attribute
9337 objc-property-attribute
9338 'getter' = identifier
9339 'setter' = identifier
9340 'readonly'
9341 'readwrite'
9342 'assign'
9343 'retain'
9344 'copy'
9345 'nonatomic'
9347 For example:
9348 @property NSString *name;
9349 @property (readonly) id object;
9350 @property (retain, nonatomic, getter=getTheName) id name;
9351 @property int a, b, c;
9353 PS: This function is identical to cp_parser_objc_at_propery_declaration
9354 for C++. Keep them in sync. */
9355 static void
9356 c_parser_objc_at_property_declaration (c_parser *parser)
9358 /* The following variables hold the attributes of the properties as
9359 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
9360 seen. When we see an attribute, we set them to 'true' (if they
9361 are boolean properties) or to the identifier (if they have an
9362 argument, ie, for getter and setter). Note that here we only
9363 parse the list of attributes, check the syntax and accumulate the
9364 attributes that we find. objc_add_property_declaration() will
9365 then process the information. */
9366 bool property_assign = false;
9367 bool property_copy = false;
9368 tree property_getter_ident = NULL_TREE;
9369 bool property_nonatomic = false;
9370 bool property_readonly = false;
9371 bool property_readwrite = false;
9372 bool property_retain = false;
9373 tree property_setter_ident = NULL_TREE;
9375 /* 'properties' is the list of properties that we read. Usually a
9376 single one, but maybe more (eg, in "@property int a, b, c;" there
9377 are three). */
9378 tree properties;
9379 location_t loc;
9381 loc = c_parser_peek_token (parser)->location;
9382 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_PROPERTY));
9384 c_parser_consume_token (parser); /* Eat '@property'. */
9386 /* Parse the optional attribute list... */
9387 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
9389 /* Eat the '(' */
9390 c_parser_consume_token (parser);
9392 /* Property attribute keywords are valid now. */
9393 parser->objc_property_attr_context = true;
9395 while (true)
9397 bool syntax_error = false;
9398 c_token *token = c_parser_peek_token (parser);
9399 enum rid keyword;
9401 if (token->type != CPP_KEYWORD)
9403 if (token->type == CPP_CLOSE_PAREN)
9404 c_parser_error (parser, "expected identifier");
9405 else
9407 c_parser_consume_token (parser);
9408 c_parser_error (parser, "unknown property attribute");
9410 break;
9412 keyword = token->keyword;
9413 c_parser_consume_token (parser);
9414 switch (keyword)
9416 case RID_ASSIGN: property_assign = true; break;
9417 case RID_COPY: property_copy = true; break;
9418 case RID_NONATOMIC: property_nonatomic = true; break;
9419 case RID_READONLY: property_readonly = true; break;
9420 case RID_READWRITE: property_readwrite = true; break;
9421 case RID_RETAIN: property_retain = true; break;
9423 case RID_GETTER:
9424 case RID_SETTER:
9425 if (c_parser_next_token_is_not (parser, CPP_EQ))
9427 if (keyword == RID_GETTER)
9428 c_parser_error (parser,
9429 "missing %<=%> (after %<getter%> attribute)");
9430 else
9431 c_parser_error (parser,
9432 "missing %<=%> (after %<setter%> attribute)");
9433 syntax_error = true;
9434 break;
9436 c_parser_consume_token (parser); /* eat the = */
9437 if (c_parser_next_token_is_not (parser, CPP_NAME))
9439 c_parser_error (parser, "expected identifier");
9440 syntax_error = true;
9441 break;
9443 if (keyword == RID_SETTER)
9445 if (property_setter_ident != NULL_TREE)
9446 c_parser_error (parser, "the %<setter%> attribute may only be specified once");
9447 else
9448 property_setter_ident = c_parser_peek_token (parser)->value;
9449 c_parser_consume_token (parser);
9450 if (c_parser_next_token_is_not (parser, CPP_COLON))
9451 c_parser_error (parser, "setter name must terminate with %<:%>");
9452 else
9453 c_parser_consume_token (parser);
9455 else
9457 if (property_getter_ident != NULL_TREE)
9458 c_parser_error (parser, "the %<getter%> attribute may only be specified once");
9459 else
9460 property_getter_ident = c_parser_peek_token (parser)->value;
9461 c_parser_consume_token (parser);
9463 break;
9464 default:
9465 c_parser_error (parser, "unknown property attribute");
9466 syntax_error = true;
9467 break;
9470 if (syntax_error)
9471 break;
9473 if (c_parser_next_token_is (parser, CPP_COMMA))
9474 c_parser_consume_token (parser);
9475 else
9476 break;
9478 parser->objc_property_attr_context = false;
9479 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
9481 /* ... and the property declaration(s). */
9482 properties = c_parser_struct_declaration (parser);
9484 if (properties == error_mark_node)
9486 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
9487 parser->error = false;
9488 return;
9491 if (properties == NULL_TREE)
9492 c_parser_error (parser, "expected identifier");
9493 else
9495 /* Comma-separated properties are chained together in
9496 reverse order; add them one by one. */
9497 properties = nreverse (properties);
9499 for (; properties; properties = TREE_CHAIN (properties))
9500 objc_add_property_declaration (loc, copy_node (properties),
9501 property_readonly, property_readwrite,
9502 property_assign, property_retain,
9503 property_copy, property_nonatomic,
9504 property_getter_ident, property_setter_ident);
9507 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
9508 parser->error = false;
9511 /* Parse an Objective-C @synthesize declaration. The syntax is:
9513 objc-synthesize-declaration:
9514 @synthesize objc-synthesize-identifier-list ;
9516 objc-synthesize-identifier-list:
9517 objc-synthesize-identifier
9518 objc-synthesize-identifier-list, objc-synthesize-identifier
9520 objc-synthesize-identifier
9521 identifier
9522 identifier = identifier
9524 For example:
9525 @synthesize MyProperty;
9526 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
9528 PS: This function is identical to cp_parser_objc_at_synthesize_declaration
9529 for C++. Keep them in sync.
9531 static void
9532 c_parser_objc_at_synthesize_declaration (c_parser *parser)
9534 tree list = NULL_TREE;
9535 location_t loc;
9536 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_SYNTHESIZE));
9537 loc = c_parser_peek_token (parser)->location;
9539 c_parser_consume_token (parser);
9540 while (true)
9542 tree property, ivar;
9543 if (c_parser_next_token_is_not (parser, CPP_NAME))
9545 c_parser_error (parser, "expected identifier");
9546 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
9547 /* Once we find the semicolon, we can resume normal parsing.
9548 We have to reset parser->error manually because
9549 c_parser_skip_until_found() won't reset it for us if the
9550 next token is precisely a semicolon. */
9551 parser->error = false;
9552 return;
9554 property = c_parser_peek_token (parser)->value;
9555 c_parser_consume_token (parser);
9556 if (c_parser_next_token_is (parser, CPP_EQ))
9558 c_parser_consume_token (parser);
9559 if (c_parser_next_token_is_not (parser, CPP_NAME))
9561 c_parser_error (parser, "expected identifier");
9562 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
9563 parser->error = false;
9564 return;
9566 ivar = c_parser_peek_token (parser)->value;
9567 c_parser_consume_token (parser);
9569 else
9570 ivar = NULL_TREE;
9571 list = chainon (list, build_tree_list (ivar, property));
9572 if (c_parser_next_token_is (parser, CPP_COMMA))
9573 c_parser_consume_token (parser);
9574 else
9575 break;
9577 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
9578 objc_add_synthesize_declaration (loc, list);
9581 /* Parse an Objective-C @dynamic declaration. The syntax is:
9583 objc-dynamic-declaration:
9584 @dynamic identifier-list ;
9586 For example:
9587 @dynamic MyProperty;
9588 @dynamic MyProperty, AnotherProperty;
9590 PS: This function is identical to cp_parser_objc_at_dynamic_declaration
9591 for C++. Keep them in sync.
9593 static void
9594 c_parser_objc_at_dynamic_declaration (c_parser *parser)
9596 tree list = NULL_TREE;
9597 location_t loc;
9598 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_DYNAMIC));
9599 loc = c_parser_peek_token (parser)->location;
9601 c_parser_consume_token (parser);
9602 while (true)
9604 tree property;
9605 if (c_parser_next_token_is_not (parser, CPP_NAME))
9607 c_parser_error (parser, "expected identifier");
9608 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
9609 parser->error = false;
9610 return;
9612 property = c_parser_peek_token (parser)->value;
9613 list = chainon (list, build_tree_list (NULL_TREE, property));
9614 c_parser_consume_token (parser);
9615 if (c_parser_next_token_is (parser, CPP_COMMA))
9616 c_parser_consume_token (parser);
9617 else
9618 break;
9620 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
9621 objc_add_dynamic_declaration (loc, list);
9625 /* Handle pragmas. Some OpenMP pragmas are associated with, and therefore
9626 should be considered, statements. ALLOW_STMT is true if we're within
9627 the context of a function and such pragmas are to be allowed. Returns
9628 true if we actually parsed such a pragma. */
9630 static bool
9631 c_parser_pragma (c_parser *parser, enum pragma_context context)
9633 unsigned int id;
9635 id = c_parser_peek_token (parser)->pragma_kind;
9636 gcc_assert (id != PRAGMA_NONE);
9638 switch (id)
9640 case PRAGMA_OACC_ENTER_DATA:
9641 c_parser_oacc_enter_exit_data (parser, true);
9642 return false;
9644 case PRAGMA_OACC_EXIT_DATA:
9645 c_parser_oacc_enter_exit_data (parser, false);
9646 return false;
9648 case PRAGMA_OACC_UPDATE:
9649 if (context != pragma_compound)
9651 if (context == pragma_stmt)
9652 c_parser_error (parser, "%<#pragma acc update%> may only be "
9653 "used in compound statements");
9654 goto bad_stmt;
9656 c_parser_oacc_update (parser);
9657 return false;
9659 case PRAGMA_OMP_BARRIER:
9660 if (context != pragma_compound)
9662 if (context == pragma_stmt)
9663 c_parser_error (parser, "%<#pragma omp barrier%> may only be "
9664 "used in compound statements");
9665 goto bad_stmt;
9667 c_parser_omp_barrier (parser);
9668 return false;
9670 case PRAGMA_OMP_FLUSH:
9671 if (context != pragma_compound)
9673 if (context == pragma_stmt)
9674 c_parser_error (parser, "%<#pragma omp flush%> may only be "
9675 "used in compound statements");
9676 goto bad_stmt;
9678 c_parser_omp_flush (parser);
9679 return false;
9681 case PRAGMA_OMP_TASKWAIT:
9682 if (context != pragma_compound)
9684 if (context == pragma_stmt)
9685 c_parser_error (parser, "%<#pragma omp taskwait%> may only be "
9686 "used in compound statements");
9687 goto bad_stmt;
9689 c_parser_omp_taskwait (parser);
9690 return false;
9692 case PRAGMA_OMP_TASKYIELD:
9693 if (context != pragma_compound)
9695 if (context == pragma_stmt)
9696 c_parser_error (parser, "%<#pragma omp taskyield%> may only be "
9697 "used in compound statements");
9698 goto bad_stmt;
9700 c_parser_omp_taskyield (parser);
9701 return false;
9703 case PRAGMA_OMP_CANCEL:
9704 if (context != pragma_compound)
9706 if (context == pragma_stmt)
9707 c_parser_error (parser, "%<#pragma omp cancel%> may only be "
9708 "used in compound statements");
9709 goto bad_stmt;
9711 c_parser_omp_cancel (parser);
9712 return false;
9714 case PRAGMA_OMP_CANCELLATION_POINT:
9715 if (context != pragma_compound)
9717 if (context == pragma_stmt)
9718 c_parser_error (parser, "%<#pragma omp cancellation point%> may "
9719 "only be used in compound statements");
9720 goto bad_stmt;
9722 c_parser_omp_cancellation_point (parser);
9723 return false;
9725 case PRAGMA_OMP_THREADPRIVATE:
9726 c_parser_omp_threadprivate (parser);
9727 return false;
9729 case PRAGMA_OMP_TARGET:
9730 return c_parser_omp_target (parser, context);
9732 case PRAGMA_OMP_END_DECLARE_TARGET:
9733 c_parser_omp_end_declare_target (parser);
9734 return false;
9736 case PRAGMA_OMP_SECTION:
9737 error_at (c_parser_peek_token (parser)->location,
9738 "%<#pragma omp section%> may only be used in "
9739 "%<#pragma omp sections%> construct");
9740 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
9741 return false;
9743 case PRAGMA_OMP_DECLARE_REDUCTION:
9744 c_parser_omp_declare (parser, context);
9745 return false;
9746 case PRAGMA_IVDEP:
9747 c_parser_consume_pragma (parser);
9748 c_parser_skip_to_pragma_eol (parser);
9749 if (!c_parser_next_token_is_keyword (parser, RID_FOR)
9750 && !c_parser_next_token_is_keyword (parser, RID_WHILE)
9751 && !c_parser_next_token_is_keyword (parser, RID_DO))
9753 c_parser_error (parser, "for, while or do statement expected");
9754 return false;
9756 if (c_parser_next_token_is_keyword (parser, RID_FOR))
9757 c_parser_for_statement (parser, true);
9758 else if (c_parser_next_token_is_keyword (parser, RID_WHILE))
9759 c_parser_while_statement (parser, true);
9760 else
9761 c_parser_do_statement (parser, true);
9762 return false;
9764 case PRAGMA_GCC_PCH_PREPROCESS:
9765 c_parser_error (parser, "%<#pragma GCC pch_preprocess%> must be first");
9766 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
9767 return false;
9769 case PRAGMA_CILK_SIMD:
9770 if (!c_parser_cilk_verify_simd (parser, context))
9771 return false;
9772 c_parser_consume_pragma (parser);
9773 c_parser_cilk_simd (parser);
9774 return false;
9775 case PRAGMA_CILK_GRAINSIZE:
9776 if (!flag_cilkplus)
9778 warning (0, "%<#pragma grainsize%> ignored because -fcilkplus is not"
9779 " enabled");
9780 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
9781 return false;
9783 if (context == pragma_external)
9785 error_at (c_parser_peek_token (parser)->location,
9786 "%<#pragma grainsize%> must be inside a function");
9787 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
9788 return false;
9790 c_parser_cilk_grainsize (parser);
9791 return false;
9793 default:
9794 if (id < PRAGMA_FIRST_EXTERNAL)
9796 if (context != pragma_stmt && context != pragma_compound)
9798 bad_stmt:
9799 c_parser_error (parser, "expected declaration specifiers");
9800 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
9801 return false;
9803 c_parser_omp_construct (parser);
9804 return true;
9806 break;
9809 c_parser_consume_pragma (parser);
9810 c_invoke_pragma_handler (id);
9812 /* Skip to EOL, but suppress any error message. Those will have been
9813 generated by the handler routine through calling error, as opposed
9814 to calling c_parser_error. */
9815 parser->error = true;
9816 c_parser_skip_to_pragma_eol (parser);
9818 return false;
9821 /* The interface the pragma parsers have to the lexer. */
9823 enum cpp_ttype
9824 pragma_lex (tree *value)
9826 c_token *tok = c_parser_peek_token (the_parser);
9827 enum cpp_ttype ret = tok->type;
9829 *value = tok->value;
9830 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
9831 ret = CPP_EOF;
9832 else
9834 if (ret == CPP_KEYWORD)
9835 ret = CPP_NAME;
9836 c_parser_consume_token (the_parser);
9839 return ret;
9842 static void
9843 c_parser_pragma_pch_preprocess (c_parser *parser)
9845 tree name = NULL;
9847 c_parser_consume_pragma (parser);
9848 if (c_parser_next_token_is (parser, CPP_STRING))
9850 name = c_parser_peek_token (parser)->value;
9851 c_parser_consume_token (parser);
9853 else
9854 c_parser_error (parser, "expected string literal");
9855 c_parser_skip_to_pragma_eol (parser);
9857 if (name)
9858 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
9861 /* OpenACC and OpenMP parsing routines. */
9863 /* Returns name of the next clause.
9864 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
9865 the token is not consumed. Otherwise appropriate pragma_omp_clause is
9866 returned and the token is consumed. */
9868 static pragma_omp_clause
9869 c_parser_omp_clause_name (c_parser *parser)
9871 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
9873 if (c_parser_next_token_is_keyword (parser, RID_AUTO))
9874 result = PRAGMA_OACC_CLAUSE_AUTO;
9875 else if (c_parser_next_token_is_keyword (parser, RID_IF))
9876 result = PRAGMA_OMP_CLAUSE_IF;
9877 else if (c_parser_next_token_is_keyword (parser, RID_DEFAULT))
9878 result = PRAGMA_OMP_CLAUSE_DEFAULT;
9879 else if (c_parser_next_token_is_keyword (parser, RID_FOR))
9880 result = PRAGMA_OMP_CLAUSE_FOR;
9881 else if (c_parser_next_token_is (parser, CPP_NAME))
9883 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
9885 switch (p[0])
9887 case 'a':
9888 if (!strcmp ("aligned", p))
9889 result = PRAGMA_OMP_CLAUSE_ALIGNED;
9890 else if (!strcmp ("async", p))
9891 result = PRAGMA_OACC_CLAUSE_ASYNC;
9892 break;
9893 case 'c':
9894 if (!strcmp ("collapse", p))
9895 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
9896 else if (!strcmp ("copy", p))
9897 result = PRAGMA_OACC_CLAUSE_COPY;
9898 else if (!strcmp ("copyin", p))
9899 result = PRAGMA_OMP_CLAUSE_COPYIN;
9900 else if (!strcmp ("copyout", p))
9901 result = PRAGMA_OACC_CLAUSE_COPYOUT;
9902 else if (!strcmp ("copyprivate", p))
9903 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
9904 else if (!strcmp ("create", p))
9905 result = PRAGMA_OACC_CLAUSE_CREATE;
9906 break;
9907 case 'd':
9908 if (!strcmp ("delete", p))
9909 result = PRAGMA_OACC_CLAUSE_DELETE;
9910 else if (!strcmp ("depend", p))
9911 result = PRAGMA_OMP_CLAUSE_DEPEND;
9912 else if (!strcmp ("device", p))
9913 result = PRAGMA_OMP_CLAUSE_DEVICE;
9914 else if (!strcmp ("deviceptr", p))
9915 result = PRAGMA_OACC_CLAUSE_DEVICEPTR;
9916 else if (!strcmp ("dist_schedule", p))
9917 result = PRAGMA_OMP_CLAUSE_DIST_SCHEDULE;
9918 break;
9919 case 'f':
9920 if (!strcmp ("final", p))
9921 result = PRAGMA_OMP_CLAUSE_FINAL;
9922 else if (!strcmp ("firstprivate", p))
9923 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
9924 else if (!strcmp ("from", p))
9925 result = PRAGMA_OMP_CLAUSE_FROM;
9926 break;
9927 case 'g':
9928 if (!strcmp ("gang", p))
9929 result = PRAGMA_OACC_CLAUSE_GANG;
9930 break;
9931 case 'h':
9932 if (!strcmp ("host", p))
9933 result = PRAGMA_OACC_CLAUSE_HOST;
9934 break;
9935 case 'i':
9936 if (!strcmp ("inbranch", p))
9937 result = PRAGMA_OMP_CLAUSE_INBRANCH;
9938 break;
9939 case 'l':
9940 if (!strcmp ("lastprivate", p))
9941 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
9942 else if (!strcmp ("linear", p))
9943 result = PRAGMA_OMP_CLAUSE_LINEAR;
9944 break;
9945 case 'm':
9946 if (!strcmp ("map", p))
9947 result = PRAGMA_OMP_CLAUSE_MAP;
9948 else if (!strcmp ("mergeable", p))
9949 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
9950 else if (flag_cilkplus && !strcmp ("mask", p))
9951 result = PRAGMA_CILK_CLAUSE_MASK;
9952 break;
9953 case 'n':
9954 if (!strcmp ("notinbranch", p))
9955 result = PRAGMA_OMP_CLAUSE_NOTINBRANCH;
9956 else if (!strcmp ("nowait", p))
9957 result = PRAGMA_OMP_CLAUSE_NOWAIT;
9958 else if (!strcmp ("num_gangs", p))
9959 result = PRAGMA_OACC_CLAUSE_NUM_GANGS;
9960 else if (!strcmp ("num_teams", p))
9961 result = PRAGMA_OMP_CLAUSE_NUM_TEAMS;
9962 else if (!strcmp ("num_threads", p))
9963 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
9964 else if (!strcmp ("num_workers", p))
9965 result = PRAGMA_OACC_CLAUSE_NUM_WORKERS;
9966 else if (flag_cilkplus && !strcmp ("nomask", p))
9967 result = PRAGMA_CILK_CLAUSE_NOMASK;
9968 break;
9969 case 'o':
9970 if (!strcmp ("ordered", p))
9971 result = PRAGMA_OMP_CLAUSE_ORDERED;
9972 break;
9973 case 'p':
9974 if (!strcmp ("parallel", p))
9975 result = PRAGMA_OMP_CLAUSE_PARALLEL;
9976 else if (!strcmp ("present", p))
9977 result = PRAGMA_OACC_CLAUSE_PRESENT;
9978 else if (!strcmp ("present_or_copy", p)
9979 || !strcmp ("pcopy", p))
9980 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY;
9981 else if (!strcmp ("present_or_copyin", p)
9982 || !strcmp ("pcopyin", p))
9983 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN;
9984 else if (!strcmp ("present_or_copyout", p)
9985 || !strcmp ("pcopyout", p))
9986 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT;
9987 else if (!strcmp ("present_or_create", p)
9988 || !strcmp ("pcreate", p))
9989 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE;
9990 else if (!strcmp ("private", p))
9991 result = PRAGMA_OMP_CLAUSE_PRIVATE;
9992 else if (!strcmp ("proc_bind", p))
9993 result = PRAGMA_OMP_CLAUSE_PROC_BIND;
9994 break;
9995 case 'r':
9996 if (!strcmp ("reduction", p))
9997 result = PRAGMA_OMP_CLAUSE_REDUCTION;
9998 break;
9999 case 's':
10000 if (!strcmp ("safelen", p))
10001 result = PRAGMA_OMP_CLAUSE_SAFELEN;
10002 else if (!strcmp ("schedule", p))
10003 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
10004 else if (!strcmp ("sections", p))
10005 result = PRAGMA_OMP_CLAUSE_SECTIONS;
10006 else if (!strcmp ("seq", p))
10007 result = PRAGMA_OACC_CLAUSE_SEQ;
10008 else if (!strcmp ("shared", p))
10009 result = PRAGMA_OMP_CLAUSE_SHARED;
10010 else if (!strcmp ("simdlen", p))
10011 result = PRAGMA_OMP_CLAUSE_SIMDLEN;
10012 else if (!strcmp ("self", p))
10013 result = PRAGMA_OACC_CLAUSE_SELF;
10014 break;
10015 case 't':
10016 if (!strcmp ("taskgroup", p))
10017 result = PRAGMA_OMP_CLAUSE_TASKGROUP;
10018 else if (!strcmp ("thread_limit", p))
10019 result = PRAGMA_OMP_CLAUSE_THREAD_LIMIT;
10020 else if (!strcmp ("to", p))
10021 result = PRAGMA_OMP_CLAUSE_TO;
10022 break;
10023 case 'u':
10024 if (!strcmp ("uniform", p))
10025 result = PRAGMA_OMP_CLAUSE_UNIFORM;
10026 else if (!strcmp ("untied", p))
10027 result = PRAGMA_OMP_CLAUSE_UNTIED;
10028 break;
10029 case 'v':
10030 if (!strcmp ("vector", p))
10031 result = PRAGMA_OACC_CLAUSE_VECTOR;
10032 else if (!strcmp ("vector_length", p))
10033 result = PRAGMA_OACC_CLAUSE_VECTOR_LENGTH;
10034 else if (flag_cilkplus && !strcmp ("vectorlength", p))
10035 result = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
10036 break;
10037 case 'w':
10038 if (!strcmp ("wait", p))
10039 result = PRAGMA_OACC_CLAUSE_WAIT;
10040 else if (!strcmp ("worker", p))
10041 result = PRAGMA_OACC_CLAUSE_WORKER;
10042 break;
10046 if (result != PRAGMA_OMP_CLAUSE_NONE)
10047 c_parser_consume_token (parser);
10049 return result;
10052 /* Validate that a clause of the given type does not already exist. */
10054 static void
10055 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
10056 const char *name)
10058 tree c;
10060 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
10061 if (OMP_CLAUSE_CODE (c) == code)
10063 location_t loc = OMP_CLAUSE_LOCATION (c);
10064 error_at (loc, "too many %qs clauses", name);
10065 break;
10069 /* OpenACC 2.0
10070 Parse wait clause or wait directive parameters. */
10072 static tree
10073 c_parser_oacc_wait_list (c_parser *parser, location_t clause_loc, tree list)
10075 vec<tree, va_gc> *args;
10076 tree t, args_tree;
10078 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10079 return list;
10081 args = c_parser_expr_list (parser, false, true, NULL, NULL, NULL, NULL);
10083 if (args->length () == 0)
10085 c_parser_error (parser, "expected integer expression before ')'");
10086 release_tree_vector (args);
10087 return list;
10090 args_tree = build_tree_list_vec (args);
10092 for (t = args_tree; t; t = TREE_CHAIN (t))
10094 tree targ = TREE_VALUE (t);
10096 if (targ != error_mark_node)
10098 if (!INTEGRAL_TYPE_P (TREE_TYPE (targ)))
10100 c_parser_error (parser, "expression must be integral");
10101 targ = error_mark_node;
10103 else
10105 tree c = build_omp_clause (clause_loc, OMP_CLAUSE_WAIT);
10107 OMP_CLAUSE_DECL (c) = targ;
10108 OMP_CLAUSE_CHAIN (c) = list;
10109 list = c;
10114 release_tree_vector (args);
10115 c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10116 return list;
10119 /* OpenACC 2.0, OpenMP 2.5:
10120 variable-list:
10121 identifier
10122 variable-list , identifier
10124 If KIND is nonzero, create the appropriate node and install the
10125 decl in OMP_CLAUSE_DECL and add the node to the head of the list.
10126 If KIND is nonzero, CLAUSE_LOC is the location of the clause.
10128 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
10129 return the list created. */
10131 static tree
10132 c_parser_omp_variable_list (c_parser *parser,
10133 location_t clause_loc,
10134 enum omp_clause_code kind, tree list)
10136 if (c_parser_next_token_is_not (parser, CPP_NAME)
10137 || c_parser_peek_token (parser)->id_kind != C_ID_ID)
10138 c_parser_error (parser, "expected identifier");
10140 while (c_parser_next_token_is (parser, CPP_NAME)
10141 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
10143 tree t = lookup_name (c_parser_peek_token (parser)->value);
10145 if (t == NULL_TREE)
10147 undeclared_variable (c_parser_peek_token (parser)->location,
10148 c_parser_peek_token (parser)->value);
10149 t = error_mark_node;
10152 c_parser_consume_token (parser);
10154 if (t == error_mark_node)
10156 else if (kind != 0)
10158 switch (kind)
10160 case OMP_CLAUSE__CACHE_:
10161 if (c_parser_peek_token (parser)->type != CPP_OPEN_SQUARE)
10163 c_parser_error (parser, "expected %<[%>");
10164 t = error_mark_node;
10165 break;
10167 /* FALL THROUGH. */
10168 case OMP_CLAUSE_MAP:
10169 case OMP_CLAUSE_FROM:
10170 case OMP_CLAUSE_TO:
10171 case OMP_CLAUSE_DEPEND:
10172 while (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
10174 tree low_bound = NULL_TREE, length = NULL_TREE;
10176 c_parser_consume_token (parser);
10177 if (!c_parser_next_token_is (parser, CPP_COLON))
10179 low_bound = c_parser_expression (parser).value;
10180 mark_exp_read (low_bound);
10182 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
10183 length = integer_one_node;
10184 else
10186 /* Look for `:'. */
10187 if (!c_parser_require (parser, CPP_COLON,
10188 "expected %<:%>"))
10190 t = error_mark_node;
10191 break;
10193 if (!c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
10195 length = c_parser_expression (parser).value;
10196 mark_exp_read (length);
10199 /* Look for the closing `]'. */
10200 if (!c_parser_require (parser, CPP_CLOSE_SQUARE,
10201 "expected %<]%>"))
10203 t = error_mark_node;
10204 break;
10207 if (kind == OMP_CLAUSE__CACHE_)
10209 if (TREE_CODE (low_bound) != INTEGER_CST
10210 && !TREE_READONLY (low_bound))
10212 error_at (clause_loc,
10213 "%qD is not a constant", low_bound);
10214 t = error_mark_node;
10217 if (TREE_CODE (length) != INTEGER_CST
10218 && !TREE_READONLY (length))
10220 error_at (clause_loc,
10221 "%qD is not a constant", length);
10222 t = error_mark_node;
10226 t = tree_cons (low_bound, length, t);
10228 break;
10229 default:
10230 break;
10233 if (t != error_mark_node)
10235 tree u = build_omp_clause (clause_loc, kind);
10236 OMP_CLAUSE_DECL (u) = t;
10237 OMP_CLAUSE_CHAIN (u) = list;
10238 list = u;
10241 else
10242 list = tree_cons (t, NULL_TREE, list);
10244 if (c_parser_next_token_is_not (parser, CPP_COMMA))
10245 break;
10247 c_parser_consume_token (parser);
10250 return list;
10253 /* Similarly, but expect leading and trailing parenthesis. This is a very
10254 common case for OpenACC and OpenMP clauses. */
10256 static tree
10257 c_parser_omp_var_list_parens (c_parser *parser, enum omp_clause_code kind,
10258 tree list)
10260 /* The clauses location. */
10261 location_t loc = c_parser_peek_token (parser)->location;
10263 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10265 list = c_parser_omp_variable_list (parser, loc, kind, list);
10266 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10268 return list;
10271 /* OpenACC 2.0:
10272 copy ( variable-list )
10273 copyin ( variable-list )
10274 copyout ( variable-list )
10275 create ( variable-list )
10276 delete ( variable-list )
10277 present ( variable-list )
10278 present_or_copy ( variable-list )
10279 pcopy ( variable-list )
10280 present_or_copyin ( variable-list )
10281 pcopyin ( variable-list )
10282 present_or_copyout ( variable-list )
10283 pcopyout ( variable-list )
10284 present_or_create ( variable-list )
10285 pcreate ( variable-list ) */
10287 static tree
10288 c_parser_oacc_data_clause (c_parser *parser, pragma_omp_clause c_kind,
10289 tree list)
10291 enum gomp_map_kind kind;
10292 switch (c_kind)
10294 case PRAGMA_OACC_CLAUSE_COPY:
10295 kind = GOMP_MAP_FORCE_TOFROM;
10296 break;
10297 case PRAGMA_OACC_CLAUSE_COPYIN:
10298 kind = GOMP_MAP_FORCE_TO;
10299 break;
10300 case PRAGMA_OACC_CLAUSE_COPYOUT:
10301 kind = GOMP_MAP_FORCE_FROM;
10302 break;
10303 case PRAGMA_OACC_CLAUSE_CREATE:
10304 kind = GOMP_MAP_FORCE_ALLOC;
10305 break;
10306 case PRAGMA_OACC_CLAUSE_DELETE:
10307 kind = GOMP_MAP_FORCE_DEALLOC;
10308 break;
10309 case PRAGMA_OACC_CLAUSE_DEVICE:
10310 kind = GOMP_MAP_FORCE_TO;
10311 break;
10312 case PRAGMA_OACC_CLAUSE_HOST:
10313 case PRAGMA_OACC_CLAUSE_SELF:
10314 kind = GOMP_MAP_FORCE_FROM;
10315 break;
10316 case PRAGMA_OACC_CLAUSE_PRESENT:
10317 kind = GOMP_MAP_FORCE_PRESENT;
10318 break;
10319 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY:
10320 kind = GOMP_MAP_TOFROM;
10321 break;
10322 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN:
10323 kind = GOMP_MAP_TO;
10324 break;
10325 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT:
10326 kind = GOMP_MAP_FROM;
10327 break;
10328 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE:
10329 kind = GOMP_MAP_ALLOC;
10330 break;
10331 default:
10332 gcc_unreachable ();
10334 tree nl, c;
10335 nl = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_MAP, list);
10337 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
10338 OMP_CLAUSE_SET_MAP_KIND (c, kind);
10340 return nl;
10343 /* OpenACC 2.0:
10344 deviceptr ( variable-list ) */
10346 static tree
10347 c_parser_oacc_data_clause_deviceptr (c_parser *parser, tree list)
10349 location_t loc = c_parser_peek_token (parser)->location;
10350 tree vars, t;
10352 /* Can't use OMP_CLAUSE_MAP here (that is, can't use the generic
10353 c_parser_oacc_data_clause), as for PRAGMA_OACC_CLAUSE_DEVICEPTR,
10354 variable-list must only allow for pointer variables. */
10355 vars = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_ERROR, NULL);
10356 for (t = vars; t && t; t = TREE_CHAIN (t))
10358 tree v = TREE_PURPOSE (t);
10360 /* FIXME diagnostics: Ideally we should keep individual
10361 locations for all the variables in the var list to make the
10362 following errors more precise. Perhaps
10363 c_parser_omp_var_list_parens() should construct a list of
10364 locations to go along with the var list. */
10366 if (!VAR_P (v))
10367 error_at (loc, "%qD is not a variable", v);
10368 else if (TREE_TYPE (v) == error_mark_node)
10370 else if (!POINTER_TYPE_P (TREE_TYPE (v)))
10371 error_at (loc, "%qD is not a pointer variable", v);
10373 tree u = build_omp_clause (loc, OMP_CLAUSE_MAP);
10374 OMP_CLAUSE_SET_MAP_KIND (u, GOMP_MAP_FORCE_DEVICEPTR);
10375 OMP_CLAUSE_DECL (u) = v;
10376 OMP_CLAUSE_CHAIN (u) = list;
10377 list = u;
10380 return list;
10383 /* OpenACC 2.0, OpenMP 3.0:
10384 collapse ( constant-expression ) */
10386 static tree
10387 c_parser_omp_clause_collapse (c_parser *parser, tree list)
10389 tree c, num = error_mark_node;
10390 HOST_WIDE_INT n;
10391 location_t loc;
10393 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse");
10395 loc = c_parser_peek_token (parser)->location;
10396 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10398 num = c_parser_expr_no_commas (parser, NULL).value;
10399 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10401 if (num == error_mark_node)
10402 return list;
10403 mark_exp_read (num);
10404 num = c_fully_fold (num, false, NULL);
10405 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
10406 || !tree_fits_shwi_p (num)
10407 || (n = tree_to_shwi (num)) <= 0
10408 || (int) n != n)
10410 error_at (loc,
10411 "collapse argument needs positive constant integer expression");
10412 return list;
10414 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
10415 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
10416 OMP_CLAUSE_CHAIN (c) = list;
10417 return c;
10420 /* OpenMP 2.5:
10421 copyin ( variable-list ) */
10423 static tree
10424 c_parser_omp_clause_copyin (c_parser *parser, tree list)
10426 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_COPYIN, list);
10429 /* OpenMP 2.5:
10430 copyprivate ( variable-list ) */
10432 static tree
10433 c_parser_omp_clause_copyprivate (c_parser *parser, tree list)
10435 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_COPYPRIVATE, list);
10438 /* OpenMP 2.5:
10439 default ( shared | none ) */
10441 static tree
10442 c_parser_omp_clause_default (c_parser *parser, tree list)
10444 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
10445 location_t loc = c_parser_peek_token (parser)->location;
10446 tree c;
10448 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10449 return list;
10450 if (c_parser_next_token_is (parser, CPP_NAME))
10452 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
10454 switch (p[0])
10456 case 'n':
10457 if (strcmp ("none", p) != 0)
10458 goto invalid_kind;
10459 kind = OMP_CLAUSE_DEFAULT_NONE;
10460 break;
10462 case 's':
10463 if (strcmp ("shared", p) != 0)
10464 goto invalid_kind;
10465 kind = OMP_CLAUSE_DEFAULT_SHARED;
10466 break;
10468 default:
10469 goto invalid_kind;
10472 c_parser_consume_token (parser);
10474 else
10476 invalid_kind:
10477 c_parser_error (parser, "expected %<none%> or %<shared%>");
10479 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10481 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
10482 return list;
10484 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default");
10485 c = build_omp_clause (loc, OMP_CLAUSE_DEFAULT);
10486 OMP_CLAUSE_CHAIN (c) = list;
10487 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
10489 return c;
10492 /* OpenMP 2.5:
10493 firstprivate ( variable-list ) */
10495 static tree
10496 c_parser_omp_clause_firstprivate (c_parser *parser, tree list)
10498 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_FIRSTPRIVATE, list);
10501 /* OpenMP 3.1:
10502 final ( expression ) */
10504 static tree
10505 c_parser_omp_clause_final (c_parser *parser, tree list)
10507 location_t loc = c_parser_peek_token (parser)->location;
10508 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
10510 tree t = c_parser_paren_condition (parser);
10511 tree c;
10513 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final");
10515 c = build_omp_clause (loc, OMP_CLAUSE_FINAL);
10516 OMP_CLAUSE_FINAL_EXPR (c) = t;
10517 OMP_CLAUSE_CHAIN (c) = list;
10518 list = c;
10520 else
10521 c_parser_error (parser, "expected %<(%>");
10523 return list;
10526 /* OpenACC, OpenMP 2.5:
10527 if ( expression ) */
10529 static tree
10530 c_parser_omp_clause_if (c_parser *parser, tree list)
10532 location_t loc = c_parser_peek_token (parser)->location;
10533 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
10535 tree t = c_parser_paren_condition (parser);
10536 tree c;
10538 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if");
10540 c = build_omp_clause (loc, OMP_CLAUSE_IF);
10541 OMP_CLAUSE_IF_EXPR (c) = t;
10542 OMP_CLAUSE_CHAIN (c) = list;
10543 list = c;
10545 else
10546 c_parser_error (parser, "expected %<(%>");
10548 return list;
10551 /* OpenMP 2.5:
10552 lastprivate ( variable-list ) */
10554 static tree
10555 c_parser_omp_clause_lastprivate (c_parser *parser, tree list)
10557 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_LASTPRIVATE, list);
10560 /* OpenMP 3.1:
10561 mergeable */
10563 static tree
10564 c_parser_omp_clause_mergeable (c_parser *parser ATTRIBUTE_UNUSED, tree list)
10566 tree c;
10568 /* FIXME: Should we allow duplicates? */
10569 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable");
10571 c = build_omp_clause (c_parser_peek_token (parser)->location,
10572 OMP_CLAUSE_MERGEABLE);
10573 OMP_CLAUSE_CHAIN (c) = list;
10575 return c;
10578 /* OpenMP 2.5:
10579 nowait */
10581 static tree
10582 c_parser_omp_clause_nowait (c_parser *parser ATTRIBUTE_UNUSED, tree list)
10584 tree c;
10585 location_t loc = c_parser_peek_token (parser)->location;
10587 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait");
10589 c = build_omp_clause (loc, OMP_CLAUSE_NOWAIT);
10590 OMP_CLAUSE_CHAIN (c) = list;
10591 return c;
10594 /* OpenACC:
10595 num_gangs ( expression ) */
10597 static tree
10598 c_parser_omp_clause_num_gangs (c_parser *parser, tree list)
10600 location_t num_gangs_loc = c_parser_peek_token (parser)->location;
10601 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10603 location_t expr_loc = c_parser_peek_token (parser)->location;
10604 tree c, t = c_parser_expression (parser).value;
10605 mark_exp_read (t);
10606 t = c_fully_fold (t, false, NULL);
10608 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10610 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
10612 c_parser_error (parser, "expected integer expression");
10613 return list;
10616 /* Attempt to statically determine when the number isn't positive. */
10617 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
10618 build_int_cst (TREE_TYPE (t), 0));
10619 if (CAN_HAVE_LOCATION_P (c))
10620 SET_EXPR_LOCATION (c, expr_loc);
10621 if (c == boolean_true_node)
10623 warning_at (expr_loc, 0,
10624 "%<num_gangs%> value must be positive");
10625 t = integer_one_node;
10628 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_GANGS, "num_gangs");
10630 c = build_omp_clause (num_gangs_loc, OMP_CLAUSE_NUM_GANGS);
10631 OMP_CLAUSE_NUM_GANGS_EXPR (c) = t;
10632 OMP_CLAUSE_CHAIN (c) = list;
10633 list = c;
10636 return list;
10639 /* OpenMP 2.5:
10640 num_threads ( expression ) */
10642 static tree
10643 c_parser_omp_clause_num_threads (c_parser *parser, tree list)
10645 location_t num_threads_loc = c_parser_peek_token (parser)->location;
10646 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10648 location_t expr_loc = c_parser_peek_token (parser)->location;
10649 tree c, t = c_parser_expression (parser).value;
10650 mark_exp_read (t);
10651 t = c_fully_fold (t, false, NULL);
10653 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10655 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
10657 c_parser_error (parser, "expected integer expression");
10658 return list;
10661 /* Attempt to statically determine when the number isn't positive. */
10662 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
10663 build_int_cst (TREE_TYPE (t), 0));
10664 if (CAN_HAVE_LOCATION_P (c))
10665 SET_EXPR_LOCATION (c, expr_loc);
10666 if (c == boolean_true_node)
10668 warning_at (expr_loc, 0,
10669 "%<num_threads%> value must be positive");
10670 t = integer_one_node;
10673 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS, "num_threads");
10675 c = build_omp_clause (num_threads_loc, OMP_CLAUSE_NUM_THREADS);
10676 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
10677 OMP_CLAUSE_CHAIN (c) = list;
10678 list = c;
10681 return list;
10684 /* OpenACC:
10685 num_workers ( expression ) */
10687 static tree
10688 c_parser_omp_clause_num_workers (c_parser *parser, tree list)
10690 location_t num_workers_loc = c_parser_peek_token (parser)->location;
10691 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10693 location_t expr_loc = c_parser_peek_token (parser)->location;
10694 tree c, t = c_parser_expression (parser).value;
10695 mark_exp_read (t);
10696 t = c_fully_fold (t, false, NULL);
10698 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10700 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
10702 c_parser_error (parser, "expected integer expression");
10703 return list;
10706 /* Attempt to statically determine when the number isn't positive. */
10707 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
10708 build_int_cst (TREE_TYPE (t), 0));
10709 if (CAN_HAVE_LOCATION_P (c))
10710 SET_EXPR_LOCATION (c, expr_loc);
10711 if (c == boolean_true_node)
10713 warning_at (expr_loc, 0,
10714 "%<num_workers%> value must be positive");
10715 t = integer_one_node;
10718 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_WORKERS, "num_workers");
10720 c = build_omp_clause (num_workers_loc, OMP_CLAUSE_NUM_WORKERS);
10721 OMP_CLAUSE_NUM_WORKERS_EXPR (c) = t;
10722 OMP_CLAUSE_CHAIN (c) = list;
10723 list = c;
10726 return list;
10729 /* OpenACC:
10730 async [( int-expr )] */
10732 static tree
10733 c_parser_oacc_clause_async (c_parser *parser, tree list)
10735 tree c, t;
10736 location_t loc = c_parser_peek_token (parser)->location;
10738 t = build_int_cst (integer_type_node, GOMP_ASYNC_NOVAL);
10740 if (c_parser_peek_token (parser)->type == CPP_OPEN_PAREN)
10742 c_parser_consume_token (parser);
10744 t = c_parser_expression (parser).value;
10745 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
10746 c_parser_error (parser, "expected integer expression");
10747 else if (t == error_mark_node
10748 || !c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
10749 return list;
10751 else
10752 t = c_fully_fold (t, false, NULL);
10754 check_no_duplicate_clause (list, OMP_CLAUSE_ASYNC, "async");
10756 c = build_omp_clause (loc, OMP_CLAUSE_ASYNC);
10757 OMP_CLAUSE_ASYNC_EXPR (c) = t;
10758 OMP_CLAUSE_CHAIN (c) = list;
10759 list = c;
10761 return list;
10764 /* OpenACC:
10765 wait ( int-expr-list ) */
10767 static tree
10768 c_parser_oacc_clause_wait (c_parser *parser, tree list)
10770 location_t clause_loc = c_parser_peek_token (parser)->location;
10772 if (c_parser_peek_token (parser)->type == CPP_OPEN_PAREN)
10773 list = c_parser_oacc_wait_list (parser, clause_loc, list);
10775 return list;
10778 /* OpenMP 2.5:
10779 ordered */
10781 static tree
10782 c_parser_omp_clause_ordered (c_parser *parser, tree list)
10784 tree c;
10786 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED, "ordered");
10788 c = build_omp_clause (c_parser_peek_token (parser)->location,
10789 OMP_CLAUSE_ORDERED);
10790 OMP_CLAUSE_CHAIN (c) = list;
10792 return c;
10795 /* OpenMP 2.5:
10796 private ( variable-list ) */
10798 static tree
10799 c_parser_omp_clause_private (c_parser *parser, tree list)
10801 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_PRIVATE, list);
10804 /* OpenMP 2.5:
10805 reduction ( reduction-operator : variable-list )
10807 reduction-operator:
10808 One of: + * - & ^ | && ||
10810 OpenMP 3.1:
10812 reduction-operator:
10813 One of: + * - & ^ | && || max min
10815 OpenMP 4.0:
10817 reduction-operator:
10818 One of: + * - & ^ | && ||
10819 identifier */
10821 static tree
10822 c_parser_omp_clause_reduction (c_parser *parser, tree list)
10824 location_t clause_loc = c_parser_peek_token (parser)->location;
10825 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10827 enum tree_code code = ERROR_MARK;
10828 tree reduc_id = NULL_TREE;
10830 switch (c_parser_peek_token (parser)->type)
10832 case CPP_PLUS:
10833 code = PLUS_EXPR;
10834 break;
10835 case CPP_MULT:
10836 code = MULT_EXPR;
10837 break;
10838 case CPP_MINUS:
10839 code = MINUS_EXPR;
10840 break;
10841 case CPP_AND:
10842 code = BIT_AND_EXPR;
10843 break;
10844 case CPP_XOR:
10845 code = BIT_XOR_EXPR;
10846 break;
10847 case CPP_OR:
10848 code = BIT_IOR_EXPR;
10849 break;
10850 case CPP_AND_AND:
10851 code = TRUTH_ANDIF_EXPR;
10852 break;
10853 case CPP_OR_OR:
10854 code = TRUTH_ORIF_EXPR;
10855 break;
10856 case CPP_NAME:
10858 const char *p
10859 = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
10860 if (strcmp (p, "min") == 0)
10862 code = MIN_EXPR;
10863 break;
10865 if (strcmp (p, "max") == 0)
10867 code = MAX_EXPR;
10868 break;
10870 reduc_id = c_parser_peek_token (parser)->value;
10871 break;
10873 default:
10874 c_parser_error (parser,
10875 "expected %<+%>, %<*%>, %<-%>, %<&%>, "
10876 "%<^%>, %<|%>, %<&&%>, %<||%>, %<min%> or %<max%>");
10877 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 0);
10878 return list;
10880 c_parser_consume_token (parser);
10881 reduc_id = c_omp_reduction_id (code, reduc_id);
10882 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
10884 tree nl, c;
10886 nl = c_parser_omp_variable_list (parser, clause_loc,
10887 OMP_CLAUSE_REDUCTION, list);
10888 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
10890 tree type = TREE_TYPE (OMP_CLAUSE_DECL (c));
10891 OMP_CLAUSE_REDUCTION_CODE (c) = code;
10892 if (code == ERROR_MARK
10893 || !(INTEGRAL_TYPE_P (type)
10894 || TREE_CODE (type) == REAL_TYPE
10895 || TREE_CODE (type) == COMPLEX_TYPE))
10896 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c)
10897 = c_omp_reduction_lookup (reduc_id,
10898 TYPE_MAIN_VARIANT (type));
10901 list = nl;
10903 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10905 return list;
10908 /* OpenMP 2.5:
10909 schedule ( schedule-kind )
10910 schedule ( schedule-kind , expression )
10912 schedule-kind:
10913 static | dynamic | guided | runtime | auto
10916 static tree
10917 c_parser_omp_clause_schedule (c_parser *parser, tree list)
10919 tree c, t;
10920 location_t loc = c_parser_peek_token (parser)->location;
10922 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10923 return list;
10925 c = build_omp_clause (loc, OMP_CLAUSE_SCHEDULE);
10927 if (c_parser_next_token_is (parser, CPP_NAME))
10929 tree kind = c_parser_peek_token (parser)->value;
10930 const char *p = IDENTIFIER_POINTER (kind);
10932 switch (p[0])
10934 case 'd':
10935 if (strcmp ("dynamic", p) != 0)
10936 goto invalid_kind;
10937 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
10938 break;
10940 case 'g':
10941 if (strcmp ("guided", p) != 0)
10942 goto invalid_kind;
10943 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
10944 break;
10946 case 'r':
10947 if (strcmp ("runtime", p) != 0)
10948 goto invalid_kind;
10949 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
10950 break;
10952 default:
10953 goto invalid_kind;
10956 else if (c_parser_next_token_is_keyword (parser, RID_STATIC))
10957 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
10958 else if (c_parser_next_token_is_keyword (parser, RID_AUTO))
10959 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
10960 else
10961 goto invalid_kind;
10963 c_parser_consume_token (parser);
10964 if (c_parser_next_token_is (parser, CPP_COMMA))
10966 location_t here;
10967 c_parser_consume_token (parser);
10969 here = c_parser_peek_token (parser)->location;
10970 t = c_parser_expr_no_commas (parser, NULL).value;
10971 mark_exp_read (t);
10972 t = c_fully_fold (t, false, NULL);
10974 if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
10975 error_at (here, "schedule %<runtime%> does not take "
10976 "a %<chunk_size%> parameter");
10977 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
10978 error_at (here,
10979 "schedule %<auto%> does not take "
10980 "a %<chunk_size%> parameter");
10981 else if (TREE_CODE (TREE_TYPE (t)) == INTEGER_TYPE)
10982 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
10983 else
10984 c_parser_error (parser, "expected integer expression");
10986 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10988 else
10989 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
10990 "expected %<,%> or %<)%>");
10992 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule");
10993 OMP_CLAUSE_CHAIN (c) = list;
10994 return c;
10996 invalid_kind:
10997 c_parser_error (parser, "invalid schedule kind");
10998 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 0);
10999 return list;
11002 /* OpenMP 2.5:
11003 shared ( variable-list ) */
11005 static tree
11006 c_parser_omp_clause_shared (c_parser *parser, tree list)
11008 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_SHARED, list);
11011 /* OpenMP 3.0:
11012 untied */
11014 static tree
11015 c_parser_omp_clause_untied (c_parser *parser ATTRIBUTE_UNUSED, tree list)
11017 tree c;
11019 /* FIXME: Should we allow duplicates? */
11020 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied");
11022 c = build_omp_clause (c_parser_peek_token (parser)->location,
11023 OMP_CLAUSE_UNTIED);
11024 OMP_CLAUSE_CHAIN (c) = list;
11026 return c;
11029 /* OpenACC:
11030 vector_length ( expression ) */
11032 static tree
11033 c_parser_omp_clause_vector_length (c_parser *parser, tree list)
11035 location_t vector_length_loc = c_parser_peek_token (parser)->location;
11036 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11038 location_t expr_loc = c_parser_peek_token (parser)->location;
11039 tree c, t = c_parser_expression (parser).value;
11040 mark_exp_read (t);
11041 t = c_fully_fold (t, false, NULL);
11043 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11045 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
11047 c_parser_error (parser, "expected integer expression");
11048 return list;
11051 /* Attempt to statically determine when the number isn't positive. */
11052 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
11053 build_int_cst (TREE_TYPE (t), 0));
11054 if (CAN_HAVE_LOCATION_P (c))
11055 SET_EXPR_LOCATION (c, expr_loc);
11056 if (c == boolean_true_node)
11058 warning_at (expr_loc, 0,
11059 "%<vector_length%> value must be positive");
11060 t = integer_one_node;
11063 check_no_duplicate_clause (list, OMP_CLAUSE_VECTOR_LENGTH, "vector_length");
11065 c = build_omp_clause (vector_length_loc, OMP_CLAUSE_VECTOR_LENGTH);
11066 OMP_CLAUSE_VECTOR_LENGTH_EXPR (c) = t;
11067 OMP_CLAUSE_CHAIN (c) = list;
11068 list = c;
11071 return list;
11074 /* OpenMP 4.0:
11075 inbranch
11076 notinbranch */
11078 static tree
11079 c_parser_omp_clause_branch (c_parser *parser ATTRIBUTE_UNUSED,
11080 enum omp_clause_code code, tree list)
11082 check_no_duplicate_clause (list, code, omp_clause_code_name[code]);
11084 tree c = build_omp_clause (c_parser_peek_token (parser)->location, code);
11085 OMP_CLAUSE_CHAIN (c) = list;
11087 return c;
11090 /* OpenMP 4.0:
11091 parallel
11093 sections
11094 taskgroup */
11096 static tree
11097 c_parser_omp_clause_cancelkind (c_parser *parser ATTRIBUTE_UNUSED,
11098 enum omp_clause_code code, tree list)
11100 tree c = build_omp_clause (c_parser_peek_token (parser)->location, code);
11101 OMP_CLAUSE_CHAIN (c) = list;
11103 return c;
11106 /* OpenMP 4.0:
11107 num_teams ( expression ) */
11109 static tree
11110 c_parser_omp_clause_num_teams (c_parser *parser, tree list)
11112 location_t num_teams_loc = c_parser_peek_token (parser)->location;
11113 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11115 location_t expr_loc = c_parser_peek_token (parser)->location;
11116 tree c, t = c_parser_expression (parser).value;
11117 mark_exp_read (t);
11118 t = c_fully_fold (t, false, NULL);
11120 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11122 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
11124 c_parser_error (parser, "expected integer expression");
11125 return list;
11128 /* Attempt to statically determine when the number isn't positive. */
11129 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
11130 build_int_cst (TREE_TYPE (t), 0));
11131 if (CAN_HAVE_LOCATION_P (c))
11132 SET_EXPR_LOCATION (c, expr_loc);
11133 if (c == boolean_true_node)
11135 warning_at (expr_loc, 0, "%<num_teams%> value must be positive");
11136 t = integer_one_node;
11139 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TEAMS, "num_teams");
11141 c = build_omp_clause (num_teams_loc, OMP_CLAUSE_NUM_TEAMS);
11142 OMP_CLAUSE_NUM_TEAMS_EXPR (c) = t;
11143 OMP_CLAUSE_CHAIN (c) = list;
11144 list = c;
11147 return list;
11150 /* OpenMP 4.0:
11151 thread_limit ( expression ) */
11153 static tree
11154 c_parser_omp_clause_thread_limit (c_parser *parser, tree list)
11156 location_t num_thread_limit_loc = c_parser_peek_token (parser)->location;
11157 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11159 location_t expr_loc = c_parser_peek_token (parser)->location;
11160 tree c, t = c_parser_expression (parser).value;
11161 mark_exp_read (t);
11162 t = c_fully_fold (t, false, NULL);
11164 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11166 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
11168 c_parser_error (parser, "expected integer expression");
11169 return list;
11172 /* Attempt to statically determine when the number isn't positive. */
11173 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
11174 build_int_cst (TREE_TYPE (t), 0));
11175 if (CAN_HAVE_LOCATION_P (c))
11176 SET_EXPR_LOCATION (c, expr_loc);
11177 if (c == boolean_true_node)
11179 warning_at (expr_loc, 0, "%<thread_limit%> value must be positive");
11180 t = integer_one_node;
11183 check_no_duplicate_clause (list, OMP_CLAUSE_THREAD_LIMIT,
11184 "thread_limit");
11186 c = build_omp_clause (num_thread_limit_loc, OMP_CLAUSE_THREAD_LIMIT);
11187 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
11188 OMP_CLAUSE_CHAIN (c) = list;
11189 list = c;
11192 return list;
11195 /* OpenMP 4.0:
11196 aligned ( variable-list )
11197 aligned ( variable-list : constant-expression ) */
11199 static tree
11200 c_parser_omp_clause_aligned (c_parser *parser, tree list)
11202 location_t clause_loc = c_parser_peek_token (parser)->location;
11203 tree nl, c;
11205 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11206 return list;
11208 nl = c_parser_omp_variable_list (parser, clause_loc,
11209 OMP_CLAUSE_ALIGNED, list);
11211 if (c_parser_next_token_is (parser, CPP_COLON))
11213 c_parser_consume_token (parser);
11214 tree alignment = c_parser_expr_no_commas (parser, NULL).value;
11215 mark_exp_read (alignment);
11216 alignment = c_fully_fold (alignment, false, NULL);
11217 if (!INTEGRAL_TYPE_P (TREE_TYPE (alignment))
11218 && TREE_CODE (alignment) != INTEGER_CST
11219 && tree_int_cst_sgn (alignment) != 1)
11221 error_at (clause_loc, "%<aligned%> clause alignment expression must "
11222 "be positive constant integer expression");
11223 alignment = NULL_TREE;
11226 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
11227 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = alignment;
11230 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11231 return nl;
11234 /* OpenMP 4.0:
11235 linear ( variable-list )
11236 linear ( variable-list : expression ) */
11238 static tree
11239 c_parser_omp_clause_linear (c_parser *parser, tree list, bool is_cilk_simd_fn)
11241 location_t clause_loc = c_parser_peek_token (parser)->location;
11242 tree nl, c, step;
11244 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11245 return list;
11247 nl = c_parser_omp_variable_list (parser, clause_loc,
11248 OMP_CLAUSE_LINEAR, list);
11250 if (c_parser_next_token_is (parser, CPP_COLON))
11252 c_parser_consume_token (parser);
11253 step = c_parser_expression (parser).value;
11254 mark_exp_read (step);
11255 step = c_fully_fold (step, false, NULL);
11256 if (is_cilk_simd_fn && TREE_CODE (step) == PARM_DECL)
11258 sorry ("using parameters for %<linear%> step is not supported yet");
11259 step = integer_one_node;
11261 if (!INTEGRAL_TYPE_P (TREE_TYPE (step)))
11263 error_at (clause_loc, "%<linear%> clause step expression must "
11264 "be integral");
11265 step = integer_one_node;
11269 else
11270 step = integer_one_node;
11272 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
11274 OMP_CLAUSE_LINEAR_STEP (c) = step;
11277 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11278 return nl;
11281 /* OpenMP 4.0:
11282 safelen ( constant-expression ) */
11284 static tree
11285 c_parser_omp_clause_safelen (c_parser *parser, tree list)
11287 location_t clause_loc = c_parser_peek_token (parser)->location;
11288 tree c, t;
11290 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11291 return list;
11293 t = c_parser_expr_no_commas (parser, NULL).value;
11294 mark_exp_read (t);
11295 t = c_fully_fold (t, false, NULL);
11296 if (!INTEGRAL_TYPE_P (TREE_TYPE (t))
11297 && TREE_CODE (t) != INTEGER_CST
11298 && tree_int_cst_sgn (t) != 1)
11300 error_at (clause_loc, "%<safelen%> clause expression must "
11301 "be positive constant integer expression");
11302 t = NULL_TREE;
11305 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11306 if (t == NULL_TREE || t == error_mark_node)
11307 return list;
11309 check_no_duplicate_clause (list, OMP_CLAUSE_SAFELEN, "safelen");
11311 c = build_omp_clause (clause_loc, OMP_CLAUSE_SAFELEN);
11312 OMP_CLAUSE_SAFELEN_EXPR (c) = t;
11313 OMP_CLAUSE_CHAIN (c) = list;
11314 return c;
11317 /* OpenMP 4.0:
11318 simdlen ( constant-expression ) */
11320 static tree
11321 c_parser_omp_clause_simdlen (c_parser *parser, tree list)
11323 location_t clause_loc = c_parser_peek_token (parser)->location;
11324 tree c, t;
11326 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11327 return list;
11329 t = c_parser_expr_no_commas (parser, NULL).value;
11330 mark_exp_read (t);
11331 t = c_fully_fold (t, false, NULL);
11332 if (!INTEGRAL_TYPE_P (TREE_TYPE (t))
11333 && TREE_CODE (t) != INTEGER_CST
11334 && tree_int_cst_sgn (t) != 1)
11336 error_at (clause_loc, "%<simdlen%> clause expression must "
11337 "be positive constant integer expression");
11338 t = NULL_TREE;
11341 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11342 if (t == NULL_TREE || t == error_mark_node)
11343 return list;
11345 check_no_duplicate_clause (list, OMP_CLAUSE_SIMDLEN, "simdlen");
11347 c = build_omp_clause (clause_loc, OMP_CLAUSE_SIMDLEN);
11348 OMP_CLAUSE_SIMDLEN_EXPR (c) = t;
11349 OMP_CLAUSE_CHAIN (c) = list;
11350 return c;
11353 /* OpenMP 4.0:
11354 depend ( depend-kind: variable-list )
11356 depend-kind:
11357 in | out | inout */
11359 static tree
11360 c_parser_omp_clause_depend (c_parser *parser, tree list)
11362 location_t clause_loc = c_parser_peek_token (parser)->location;
11363 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_INOUT;
11364 tree nl, c;
11366 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11367 return list;
11369 if (c_parser_next_token_is (parser, CPP_NAME))
11371 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
11372 if (strcmp ("in", p) == 0)
11373 kind = OMP_CLAUSE_DEPEND_IN;
11374 else if (strcmp ("inout", p) == 0)
11375 kind = OMP_CLAUSE_DEPEND_INOUT;
11376 else if (strcmp ("out", p) == 0)
11377 kind = OMP_CLAUSE_DEPEND_OUT;
11378 else
11379 goto invalid_kind;
11381 else
11382 goto invalid_kind;
11384 c_parser_consume_token (parser);
11385 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
11386 goto resync_fail;
11388 nl = c_parser_omp_variable_list (parser, clause_loc,
11389 OMP_CLAUSE_DEPEND, list);
11391 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
11392 OMP_CLAUSE_DEPEND_KIND (c) = kind;
11394 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11395 return nl;
11397 invalid_kind:
11398 c_parser_error (parser, "invalid depend kind");
11399 resync_fail:
11400 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11401 return list;
11404 /* OpenMP 4.0:
11405 map ( map-kind: variable-list )
11406 map ( variable-list )
11408 map-kind:
11409 alloc | to | from | tofrom */
11411 static tree
11412 c_parser_omp_clause_map (c_parser *parser, tree list)
11414 location_t clause_loc = c_parser_peek_token (parser)->location;
11415 enum gomp_map_kind kind = GOMP_MAP_TOFROM;
11416 tree nl, c;
11418 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11419 return list;
11421 if (c_parser_next_token_is (parser, CPP_NAME)
11422 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
11424 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
11425 if (strcmp ("alloc", p) == 0)
11426 kind = GOMP_MAP_ALLOC;
11427 else if (strcmp ("to", p) == 0)
11428 kind = GOMP_MAP_TO;
11429 else if (strcmp ("from", p) == 0)
11430 kind = GOMP_MAP_FROM;
11431 else if (strcmp ("tofrom", p) == 0)
11432 kind = GOMP_MAP_TOFROM;
11433 else
11435 c_parser_error (parser, "invalid map kind");
11436 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
11437 "expected %<)%>");
11438 return list;
11440 c_parser_consume_token (parser);
11441 c_parser_consume_token (parser);
11444 nl = c_parser_omp_variable_list (parser, clause_loc, OMP_CLAUSE_MAP, list);
11446 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
11447 OMP_CLAUSE_SET_MAP_KIND (c, kind);
11449 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11450 return nl;
11453 /* OpenMP 4.0:
11454 device ( expression ) */
11456 static tree
11457 c_parser_omp_clause_device (c_parser *parser, tree list)
11459 location_t clause_loc = c_parser_peek_token (parser)->location;
11460 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11462 tree c, t = c_parser_expr_no_commas (parser, NULL).value;
11463 mark_exp_read (t);
11464 t = c_fully_fold (t, false, NULL);
11466 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11468 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
11470 c_parser_error (parser, "expected integer expression");
11471 return list;
11474 check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE, "device");
11476 c = build_omp_clause (clause_loc, OMP_CLAUSE_DEVICE);
11477 OMP_CLAUSE_DEVICE_ID (c) = t;
11478 OMP_CLAUSE_CHAIN (c) = list;
11479 list = c;
11482 return list;
11485 /* OpenMP 4.0:
11486 dist_schedule ( static )
11487 dist_schedule ( static , expression ) */
11489 static tree
11490 c_parser_omp_clause_dist_schedule (c_parser *parser, tree list)
11492 tree c, t = NULL_TREE;
11493 location_t loc = c_parser_peek_token (parser)->location;
11495 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11496 return list;
11498 if (!c_parser_next_token_is_keyword (parser, RID_STATIC))
11500 c_parser_error (parser, "invalid dist_schedule kind");
11501 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
11502 "expected %<)%>");
11503 return list;
11506 c_parser_consume_token (parser);
11507 if (c_parser_next_token_is (parser, CPP_COMMA))
11509 c_parser_consume_token (parser);
11511 t = c_parser_expr_no_commas (parser, NULL).value;
11512 mark_exp_read (t);
11513 t = c_fully_fold (t, false, NULL);
11514 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11516 else
11517 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
11518 "expected %<,%> or %<)%>");
11520 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule");
11521 if (t == error_mark_node)
11522 return list;
11524 c = build_omp_clause (loc, OMP_CLAUSE_DIST_SCHEDULE);
11525 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
11526 OMP_CLAUSE_CHAIN (c) = list;
11527 return c;
11530 /* OpenMP 4.0:
11531 proc_bind ( proc-bind-kind )
11533 proc-bind-kind:
11534 master | close | spread */
11536 static tree
11537 c_parser_omp_clause_proc_bind (c_parser *parser, tree list)
11539 location_t clause_loc = c_parser_peek_token (parser)->location;
11540 enum omp_clause_proc_bind_kind kind;
11541 tree c;
11543 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11544 return list;
11546 if (c_parser_next_token_is (parser, CPP_NAME))
11548 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
11549 if (strcmp ("master", p) == 0)
11550 kind = OMP_CLAUSE_PROC_BIND_MASTER;
11551 else if (strcmp ("close", p) == 0)
11552 kind = OMP_CLAUSE_PROC_BIND_CLOSE;
11553 else if (strcmp ("spread", p) == 0)
11554 kind = OMP_CLAUSE_PROC_BIND_SPREAD;
11555 else
11556 goto invalid_kind;
11558 else
11559 goto invalid_kind;
11561 c_parser_consume_token (parser);
11562 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11563 c = build_omp_clause (clause_loc, OMP_CLAUSE_PROC_BIND);
11564 OMP_CLAUSE_PROC_BIND_KIND (c) = kind;
11565 OMP_CLAUSE_CHAIN (c) = list;
11566 return c;
11568 invalid_kind:
11569 c_parser_error (parser, "invalid proc_bind kind");
11570 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11571 return list;
11574 /* OpenMP 4.0:
11575 to ( variable-list ) */
11577 static tree
11578 c_parser_omp_clause_to (c_parser *parser, tree list)
11580 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_TO, list);
11583 /* OpenMP 4.0:
11584 from ( variable-list ) */
11586 static tree
11587 c_parser_omp_clause_from (c_parser *parser, tree list)
11589 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_FROM, list);
11592 /* OpenMP 4.0:
11593 uniform ( variable-list ) */
11595 static tree
11596 c_parser_omp_clause_uniform (c_parser *parser, tree list)
11598 /* The clauses location. */
11599 location_t loc = c_parser_peek_token (parser)->location;
11601 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11603 list = c_parser_omp_variable_list (parser, loc, OMP_CLAUSE_UNIFORM,
11604 list);
11605 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11607 return list;
11610 /* Parse all OpenACC clauses. The set clauses allowed by the directive
11611 is a bitmask in MASK. Return the list of clauses found. */
11613 static tree
11614 c_parser_oacc_all_clauses (c_parser *parser, omp_clause_mask mask,
11615 const char *where, bool finish_p = true)
11617 tree clauses = NULL;
11618 bool first = true;
11620 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
11622 location_t here;
11623 pragma_omp_clause c_kind;
11624 const char *c_name;
11625 tree prev = clauses;
11627 if (!first && c_parser_next_token_is (parser, CPP_COMMA))
11628 c_parser_consume_token (parser);
11630 here = c_parser_peek_token (parser)->location;
11631 c_kind = c_parser_omp_clause_name (parser);
11633 switch (c_kind)
11635 case PRAGMA_OACC_CLAUSE_ASYNC:
11636 clauses = c_parser_oacc_clause_async (parser, clauses);
11637 c_name = "async";
11638 break;
11639 case PRAGMA_OACC_CLAUSE_COLLAPSE:
11640 clauses = c_parser_omp_clause_collapse (parser, clauses);
11641 c_name = "collapse";
11642 break;
11643 case PRAGMA_OACC_CLAUSE_COPY:
11644 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
11645 c_name = "copy";
11646 break;
11647 case PRAGMA_OACC_CLAUSE_COPYIN:
11648 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
11649 c_name = "copyin";
11650 break;
11651 case PRAGMA_OACC_CLAUSE_COPYOUT:
11652 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
11653 c_name = "copyout";
11654 break;
11655 case PRAGMA_OACC_CLAUSE_CREATE:
11656 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
11657 c_name = "create";
11658 break;
11659 case PRAGMA_OACC_CLAUSE_DELETE:
11660 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
11661 c_name = "delete";
11662 break;
11663 case PRAGMA_OACC_CLAUSE_DEVICE:
11664 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
11665 c_name = "device";
11666 break;
11667 case PRAGMA_OACC_CLAUSE_DEVICEPTR:
11668 clauses = c_parser_oacc_data_clause_deviceptr (parser, clauses);
11669 c_name = "deviceptr";
11670 break;
11671 case PRAGMA_OACC_CLAUSE_FIRSTPRIVATE:
11672 clauses = c_parser_omp_clause_firstprivate (parser, clauses);
11673 c_name = "firstprivate";
11674 break;
11675 case PRAGMA_OACC_CLAUSE_HOST:
11676 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
11677 c_name = "host";
11678 break;
11679 case PRAGMA_OACC_CLAUSE_IF:
11680 clauses = c_parser_omp_clause_if (parser, clauses);
11681 c_name = "if";
11682 break;
11683 case PRAGMA_OACC_CLAUSE_NUM_GANGS:
11684 clauses = c_parser_omp_clause_num_gangs (parser, clauses);
11685 c_name = "num_gangs";
11686 break;
11687 case PRAGMA_OACC_CLAUSE_NUM_WORKERS:
11688 clauses = c_parser_omp_clause_num_workers (parser, clauses);
11689 c_name = "num_workers";
11690 break;
11691 case PRAGMA_OACC_CLAUSE_PRESENT:
11692 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
11693 c_name = "present";
11694 break;
11695 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY:
11696 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
11697 c_name = "present_or_copy";
11698 break;
11699 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN:
11700 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
11701 c_name = "present_or_copyin";
11702 break;
11703 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT:
11704 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
11705 c_name = "present_or_copyout";
11706 break;
11707 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE:
11708 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
11709 c_name = "present_or_create";
11710 break;
11711 case PRAGMA_OACC_CLAUSE_PRIVATE:
11712 clauses = c_parser_omp_clause_private (parser, clauses);
11713 c_name = "private";
11714 break;
11715 case PRAGMA_OACC_CLAUSE_REDUCTION:
11716 clauses = c_parser_omp_clause_reduction (parser, clauses);
11717 c_name = "reduction";
11718 break;
11719 case PRAGMA_OACC_CLAUSE_SELF:
11720 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
11721 c_name = "self";
11722 break;
11723 case PRAGMA_OACC_CLAUSE_VECTOR_LENGTH:
11724 clauses = c_parser_omp_clause_vector_length (parser, clauses);
11725 c_name = "vector_length";
11726 break;
11727 case PRAGMA_OACC_CLAUSE_WAIT:
11728 clauses = c_parser_oacc_clause_wait (parser, clauses);
11729 c_name = "wait";
11730 break;
11731 default:
11732 c_parser_error (parser, "expected %<#pragma acc%> clause");
11733 goto saw_error;
11736 first = false;
11738 if (((mask >> c_kind) & 1) == 0 && !parser->error)
11740 /* Remove the invalid clause(s) from the list to avoid
11741 confusing the rest of the compiler. */
11742 clauses = prev;
11743 error_at (here, "%qs is not valid for %qs", c_name, where);
11747 saw_error:
11748 c_parser_skip_to_pragma_eol (parser);
11750 if (finish_p)
11751 return c_finish_omp_clauses (clauses);
11753 return clauses;
11756 /* Parse all OpenMP clauses. The set clauses allowed by the directive
11757 is a bitmask in MASK. Return the list of clauses found. */
11759 static tree
11760 c_parser_omp_all_clauses (c_parser *parser, omp_clause_mask mask,
11761 const char *where, bool finish_p = true)
11763 tree clauses = NULL;
11764 bool first = true, cilk_simd_fn = false;
11766 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
11768 location_t here;
11769 pragma_omp_clause c_kind;
11770 const char *c_name;
11771 tree prev = clauses;
11773 if (!first && c_parser_next_token_is (parser, CPP_COMMA))
11774 c_parser_consume_token (parser);
11776 here = c_parser_peek_token (parser)->location;
11777 c_kind = c_parser_omp_clause_name (parser);
11779 switch (c_kind)
11781 case PRAGMA_OMP_CLAUSE_COLLAPSE:
11782 clauses = c_parser_omp_clause_collapse (parser, clauses);
11783 c_name = "collapse";
11784 break;
11785 case PRAGMA_OMP_CLAUSE_COPYIN:
11786 clauses = c_parser_omp_clause_copyin (parser, clauses);
11787 c_name = "copyin";
11788 break;
11789 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
11790 clauses = c_parser_omp_clause_copyprivate (parser, clauses);
11791 c_name = "copyprivate";
11792 break;
11793 case PRAGMA_OMP_CLAUSE_DEFAULT:
11794 clauses = c_parser_omp_clause_default (parser, clauses);
11795 c_name = "default";
11796 break;
11797 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
11798 clauses = c_parser_omp_clause_firstprivate (parser, clauses);
11799 c_name = "firstprivate";
11800 break;
11801 case PRAGMA_OMP_CLAUSE_FINAL:
11802 clauses = c_parser_omp_clause_final (parser, clauses);
11803 c_name = "final";
11804 break;
11805 case PRAGMA_OMP_CLAUSE_IF:
11806 clauses = c_parser_omp_clause_if (parser, clauses);
11807 c_name = "if";
11808 break;
11809 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
11810 clauses = c_parser_omp_clause_lastprivate (parser, clauses);
11811 c_name = "lastprivate";
11812 break;
11813 case PRAGMA_OMP_CLAUSE_MERGEABLE:
11814 clauses = c_parser_omp_clause_mergeable (parser, clauses);
11815 c_name = "mergeable";
11816 break;
11817 case PRAGMA_OMP_CLAUSE_NOWAIT:
11818 clauses = c_parser_omp_clause_nowait (parser, clauses);
11819 c_name = "nowait";
11820 break;
11821 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
11822 clauses = c_parser_omp_clause_num_threads (parser, clauses);
11823 c_name = "num_threads";
11824 break;
11825 case PRAGMA_OMP_CLAUSE_ORDERED:
11826 clauses = c_parser_omp_clause_ordered (parser, clauses);
11827 c_name = "ordered";
11828 break;
11829 case PRAGMA_OMP_CLAUSE_PRIVATE:
11830 clauses = c_parser_omp_clause_private (parser, clauses);
11831 c_name = "private";
11832 break;
11833 case PRAGMA_OMP_CLAUSE_REDUCTION:
11834 clauses = c_parser_omp_clause_reduction (parser, clauses);
11835 c_name = "reduction";
11836 break;
11837 case PRAGMA_OMP_CLAUSE_SCHEDULE:
11838 clauses = c_parser_omp_clause_schedule (parser, clauses);
11839 c_name = "schedule";
11840 break;
11841 case PRAGMA_OMP_CLAUSE_SHARED:
11842 clauses = c_parser_omp_clause_shared (parser, clauses);
11843 c_name = "shared";
11844 break;
11845 case PRAGMA_OMP_CLAUSE_UNTIED:
11846 clauses = c_parser_omp_clause_untied (parser, clauses);
11847 c_name = "untied";
11848 break;
11849 case PRAGMA_OMP_CLAUSE_INBRANCH:
11850 case PRAGMA_CILK_CLAUSE_MASK:
11851 clauses = c_parser_omp_clause_branch (parser, OMP_CLAUSE_INBRANCH,
11852 clauses);
11853 c_name = "inbranch";
11854 break;
11855 case PRAGMA_OMP_CLAUSE_NOTINBRANCH:
11856 case PRAGMA_CILK_CLAUSE_NOMASK:
11857 clauses = c_parser_omp_clause_branch (parser, OMP_CLAUSE_NOTINBRANCH,
11858 clauses);
11859 c_name = "notinbranch";
11860 break;
11861 case PRAGMA_OMP_CLAUSE_PARALLEL:
11862 clauses
11863 = c_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_PARALLEL,
11864 clauses);
11865 c_name = "parallel";
11866 if (!first)
11868 clause_not_first:
11869 error_at (here, "%qs must be the first clause of %qs",
11870 c_name, where);
11871 clauses = prev;
11873 break;
11874 case PRAGMA_OMP_CLAUSE_FOR:
11875 clauses
11876 = c_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_FOR,
11877 clauses);
11878 c_name = "for";
11879 if (!first)
11880 goto clause_not_first;
11881 break;
11882 case PRAGMA_OMP_CLAUSE_SECTIONS:
11883 clauses
11884 = c_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_SECTIONS,
11885 clauses);
11886 c_name = "sections";
11887 if (!first)
11888 goto clause_not_first;
11889 break;
11890 case PRAGMA_OMP_CLAUSE_TASKGROUP:
11891 clauses
11892 = c_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_TASKGROUP,
11893 clauses);
11894 c_name = "taskgroup";
11895 if (!first)
11896 goto clause_not_first;
11897 break;
11898 case PRAGMA_OMP_CLAUSE_TO:
11899 clauses = c_parser_omp_clause_to (parser, clauses);
11900 c_name = "to";
11901 break;
11902 case PRAGMA_OMP_CLAUSE_FROM:
11903 clauses = c_parser_omp_clause_from (parser, clauses);
11904 c_name = "from";
11905 break;
11906 case PRAGMA_OMP_CLAUSE_UNIFORM:
11907 clauses = c_parser_omp_clause_uniform (parser, clauses);
11908 c_name = "uniform";
11909 break;
11910 case PRAGMA_OMP_CLAUSE_NUM_TEAMS:
11911 clauses = c_parser_omp_clause_num_teams (parser, clauses);
11912 c_name = "num_teams";
11913 break;
11914 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT:
11915 clauses = c_parser_omp_clause_thread_limit (parser, clauses);
11916 c_name = "thread_limit";
11917 break;
11918 case PRAGMA_OMP_CLAUSE_ALIGNED:
11919 clauses = c_parser_omp_clause_aligned (parser, clauses);
11920 c_name = "aligned";
11921 break;
11922 case PRAGMA_OMP_CLAUSE_LINEAR:
11923 if (((mask >> PRAGMA_CILK_CLAUSE_VECTORLENGTH) & 1) != 0)
11924 cilk_simd_fn = true;
11925 clauses = c_parser_omp_clause_linear (parser, clauses, cilk_simd_fn);
11926 c_name = "linear";
11927 break;
11928 case PRAGMA_OMP_CLAUSE_DEPEND:
11929 clauses = c_parser_omp_clause_depend (parser, clauses);
11930 c_name = "depend";
11931 break;
11932 case PRAGMA_OMP_CLAUSE_MAP:
11933 clauses = c_parser_omp_clause_map (parser, clauses);
11934 c_name = "map";
11935 break;
11936 case PRAGMA_OMP_CLAUSE_DEVICE:
11937 clauses = c_parser_omp_clause_device (parser, clauses);
11938 c_name = "device";
11939 break;
11940 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE:
11941 clauses = c_parser_omp_clause_dist_schedule (parser, clauses);
11942 c_name = "dist_schedule";
11943 break;
11944 case PRAGMA_OMP_CLAUSE_PROC_BIND:
11945 clauses = c_parser_omp_clause_proc_bind (parser, clauses);
11946 c_name = "proc_bind";
11947 break;
11948 case PRAGMA_OMP_CLAUSE_SAFELEN:
11949 clauses = c_parser_omp_clause_safelen (parser, clauses);
11950 c_name = "safelen";
11951 break;
11952 case PRAGMA_CILK_CLAUSE_VECTORLENGTH:
11953 clauses = c_parser_cilk_clause_vectorlength (parser, clauses, true);
11954 c_name = "simdlen";
11955 break;
11956 case PRAGMA_OMP_CLAUSE_SIMDLEN:
11957 clauses = c_parser_omp_clause_simdlen (parser, clauses);
11958 c_name = "simdlen";
11959 break;
11960 default:
11961 c_parser_error (parser, "expected %<#pragma omp%> clause");
11962 goto saw_error;
11965 first = false;
11967 if (((mask >> c_kind) & 1) == 0 && !parser->error)
11969 /* Remove the invalid clause(s) from the list to avoid
11970 confusing the rest of the compiler. */
11971 clauses = prev;
11972 error_at (here, "%qs is not valid for %qs", c_name, where);
11976 saw_error:
11977 c_parser_skip_to_pragma_eol (parser);
11979 if (finish_p)
11980 return c_finish_omp_clauses (clauses);
11982 return clauses;
11985 /* OpenACC 2.0, OpenMP 2.5:
11986 structured-block:
11987 statement
11989 In practice, we're also interested in adding the statement to an
11990 outer node. So it is convenient if we work around the fact that
11991 c_parser_statement calls add_stmt. */
11993 static tree
11994 c_parser_omp_structured_block (c_parser *parser)
11996 tree stmt = push_stmt_list ();
11997 c_parser_statement (parser);
11998 return pop_stmt_list (stmt);
12001 /* OpenACC 2.0:
12002 # pragma acc cache (variable-list) new-line
12004 LOC is the location of the #pragma token.
12007 static tree
12008 c_parser_oacc_cache (location_t loc, c_parser *parser)
12010 tree stmt, clauses;
12012 clauses = c_parser_omp_var_list_parens (parser, OMP_CLAUSE__CACHE_, NULL);
12013 clauses = c_finish_omp_clauses (clauses);
12015 c_parser_skip_to_pragma_eol (parser);
12017 stmt = make_node (OACC_CACHE);
12018 TREE_TYPE (stmt) = void_type_node;
12019 OACC_CACHE_CLAUSES (stmt) = clauses;
12020 SET_EXPR_LOCATION (stmt, loc);
12021 add_stmt (stmt);
12023 return stmt;
12026 /* OpenACC 2.0:
12027 # pragma acc data oacc-data-clause[optseq] new-line
12028 structured-block
12030 LOC is the location of the #pragma token.
12033 #define OACC_DATA_CLAUSE_MASK \
12034 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
12035 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
12036 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
12037 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
12038 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
12039 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
12040 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
12041 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
12042 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
12043 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
12044 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) )
12046 static tree
12047 c_parser_oacc_data (location_t loc, c_parser *parser)
12049 tree stmt, clauses, block;
12051 clauses = c_parser_oacc_all_clauses (parser, OACC_DATA_CLAUSE_MASK,
12052 "#pragma acc data");
12054 block = c_begin_omp_parallel ();
12055 add_stmt (c_parser_omp_structured_block (parser));
12057 stmt = c_finish_oacc_data (loc, clauses, block);
12059 return stmt;
12062 /* OpenACC 2.0:
12063 # pragma acc kernels oacc-kernels-clause[optseq] new-line
12064 structured-block
12066 LOC is the location of the #pragma token.
12069 #define OACC_KERNELS_CLAUSE_MASK \
12070 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
12071 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
12072 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
12073 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
12074 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
12075 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
12076 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
12077 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
12078 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
12079 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
12080 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
12081 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
12082 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
12084 static tree
12085 c_parser_oacc_kernels (location_t loc, c_parser *parser, char *p_name)
12087 tree stmt, clauses = NULL_TREE, block;
12089 strcat (p_name, " kernels");
12091 if (c_parser_next_token_is (parser, CPP_NAME))
12093 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
12094 if (strcmp (p, "loop") == 0)
12096 c_parser_consume_token (parser);
12097 block = c_begin_omp_parallel ();
12098 c_parser_oacc_loop (loc, parser, p_name);
12099 stmt = c_finish_oacc_kernels (loc, clauses, block);
12100 OACC_KERNELS_COMBINED (stmt) = 1;
12101 return stmt;
12105 clauses = c_parser_oacc_all_clauses (parser, OACC_KERNELS_CLAUSE_MASK,
12106 p_name);
12108 block = c_begin_omp_parallel ();
12109 add_stmt (c_parser_omp_structured_block (parser));
12111 stmt = c_finish_oacc_kernels (loc, clauses, block);
12113 return stmt;
12116 /* OpenACC 2.0:
12117 # pragma acc enter data oacc-enter-data-clause[optseq] new-line
12121 # pragma acc exit data oacc-exit-data-clause[optseq] new-line
12124 LOC is the location of the #pragma token.
12127 #define OACC_ENTER_DATA_CLAUSE_MASK \
12128 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
12129 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
12130 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
12131 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
12132 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
12133 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
12134 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
12136 #define OACC_EXIT_DATA_CLAUSE_MASK \
12137 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
12138 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
12139 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
12140 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DELETE) \
12141 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
12143 static void
12144 c_parser_oacc_enter_exit_data (c_parser *parser, bool enter)
12146 location_t loc = c_parser_peek_token (parser)->location;
12147 tree clauses, stmt;
12149 c_parser_consume_pragma (parser);
12151 if (!c_parser_next_token_is (parser, CPP_NAME))
12153 c_parser_error (parser, enter
12154 ? "expected %<data%> in %<#pragma acc enter data%>"
12155 : "expected %<data%> in %<#pragma acc exit data%>");
12156 c_parser_skip_to_pragma_eol (parser);
12157 return;
12160 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
12161 if (strcmp (p, "data") != 0)
12163 c_parser_error (parser, "invalid pragma");
12164 c_parser_skip_to_pragma_eol (parser);
12165 return;
12168 c_parser_consume_token (parser);
12170 if (enter)
12171 clauses = c_parser_oacc_all_clauses (parser, OACC_ENTER_DATA_CLAUSE_MASK,
12172 "#pragma acc enter data");
12173 else
12174 clauses = c_parser_oacc_all_clauses (parser, OACC_EXIT_DATA_CLAUSE_MASK,
12175 "#pragma acc exit data");
12177 if (find_omp_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
12179 error_at (loc, enter
12180 ? "%<#pragma acc enter data%> has no data movement clause"
12181 : "%<#pragma acc exit data%> has no data movement clause");
12182 return;
12185 stmt = enter ? make_node (OACC_ENTER_DATA) : make_node (OACC_EXIT_DATA);
12186 TREE_TYPE (stmt) = void_type_node;
12187 OMP_STANDALONE_CLAUSES (stmt) = clauses;
12188 SET_EXPR_LOCATION (stmt, loc);
12189 add_stmt (stmt);
12193 /* OpenACC 2.0:
12195 # pragma acc loop oacc-loop-clause[optseq] new-line
12196 structured-block
12198 LOC is the location of the #pragma token.
12201 #define OACC_LOOP_CLAUSE_MASK \
12202 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COLLAPSE) \
12203 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) )
12205 static tree
12206 c_parser_oacc_loop (location_t loc, c_parser *parser, char *p_name)
12208 tree stmt, clauses, block;
12210 strcat (p_name, " loop");
12212 clauses = c_parser_oacc_all_clauses (parser, OACC_LOOP_CLAUSE_MASK, p_name);
12214 block = c_begin_compound_stmt (true);
12215 stmt = c_parser_omp_for_loop (loc, parser, OACC_LOOP, clauses, NULL);
12216 block = c_end_compound_stmt (loc, block, true);
12217 add_stmt (block);
12219 return stmt;
12222 /* OpenACC 2.0:
12223 # pragma acc parallel oacc-parallel-clause[optseq] new-line
12224 structured-block
12226 LOC is the location of the #pragma token.
12229 #define OACC_PARALLEL_CLAUSE_MASK \
12230 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
12231 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
12232 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
12233 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
12234 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
12235 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
12236 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
12237 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
12238 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
12239 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
12240 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
12241 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
12242 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
12243 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
12244 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
12245 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
12246 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
12248 static tree
12249 c_parser_oacc_parallel (location_t loc, c_parser *parser, char *p_name)
12251 tree stmt, clauses = NULL_TREE, block;
12253 strcat (p_name, " parallel");
12255 if (c_parser_next_token_is (parser, CPP_NAME))
12257 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
12258 if (strcmp (p, "loop") == 0)
12260 c_parser_consume_token (parser);
12261 block = c_begin_omp_parallel ();
12262 c_parser_oacc_loop (loc, parser, p_name);
12263 stmt = c_finish_oacc_parallel (loc, clauses, block);
12264 OACC_PARALLEL_COMBINED (stmt) = 1;
12265 return stmt;
12269 clauses = c_parser_oacc_all_clauses (parser, OACC_PARALLEL_CLAUSE_MASK,
12270 p_name);
12272 block = c_begin_omp_parallel ();
12273 add_stmt (c_parser_omp_structured_block (parser));
12275 stmt = c_finish_oacc_parallel (loc, clauses, block);
12277 return stmt;
12280 /* OpenACC 2.0:
12281 # pragma acc update oacc-update-clause[optseq] new-line
12284 #define OACC_UPDATE_CLAUSE_MASK \
12285 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
12286 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE) \
12287 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_HOST) \
12288 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
12289 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SELF) \
12290 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
12292 static void
12293 c_parser_oacc_update (c_parser *parser)
12295 location_t loc = c_parser_peek_token (parser)->location;
12297 c_parser_consume_pragma (parser);
12299 tree clauses = c_parser_oacc_all_clauses (parser, OACC_UPDATE_CLAUSE_MASK,
12300 "#pragma acc update");
12301 if (find_omp_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
12303 error_at (loc,
12304 "%<#pragma acc update%> must contain at least one "
12305 "%<device%> or %<host/self%> clause");
12306 return;
12309 if (parser->error)
12310 return;
12312 tree stmt = make_node (OACC_UPDATE);
12313 TREE_TYPE (stmt) = void_type_node;
12314 OACC_UPDATE_CLAUSES (stmt) = clauses;
12315 SET_EXPR_LOCATION (stmt, loc);
12316 add_stmt (stmt);
12319 /* OpenACC 2.0:
12320 # pragma acc wait [(intseq)] oacc-wait-clause[optseq] new-line
12322 LOC is the location of the #pragma token.
12325 #define OACC_WAIT_CLAUSE_MASK \
12326 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) )
12328 static tree
12329 c_parser_oacc_wait (location_t loc, c_parser *parser, char *p_name)
12331 tree clauses, list = NULL_TREE, stmt = NULL_TREE;
12333 if (c_parser_peek_token (parser)->type == CPP_OPEN_PAREN)
12334 list = c_parser_oacc_wait_list (parser, loc, list);
12336 strcpy (p_name, " wait");
12337 clauses = c_parser_oacc_all_clauses (parser, OACC_WAIT_CLAUSE_MASK, p_name);
12338 stmt = c_finish_oacc_wait (loc, list, clauses);
12340 return stmt;
12343 /* OpenMP 2.5:
12344 # pragma omp atomic new-line
12345 expression-stmt
12347 expression-stmt:
12348 x binop= expr | x++ | ++x | x-- | --x
12349 binop:
12350 +, *, -, /, &, ^, |, <<, >>
12352 where x is an lvalue expression with scalar type.
12354 OpenMP 3.1:
12355 # pragma omp atomic new-line
12356 update-stmt
12358 # pragma omp atomic read new-line
12359 read-stmt
12361 # pragma omp atomic write new-line
12362 write-stmt
12364 # pragma omp atomic update new-line
12365 update-stmt
12367 # pragma omp atomic capture new-line
12368 capture-stmt
12370 # pragma omp atomic capture new-line
12371 capture-block
12373 read-stmt:
12374 v = x
12375 write-stmt:
12376 x = expr
12377 update-stmt:
12378 expression-stmt | x = x binop expr
12379 capture-stmt:
12380 v = expression-stmt
12381 capture-block:
12382 { v = x; update-stmt; } | { update-stmt; v = x; }
12384 OpenMP 4.0:
12385 update-stmt:
12386 expression-stmt | x = x binop expr | x = expr binop x
12387 capture-stmt:
12388 v = update-stmt
12389 capture-block:
12390 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
12392 where x and v are lvalue expressions with scalar type.
12394 LOC is the location of the #pragma token. */
12396 static void
12397 c_parser_omp_atomic (location_t loc, c_parser *parser)
12399 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE;
12400 tree lhs1 = NULL_TREE, rhs1 = NULL_TREE;
12401 tree stmt, orig_lhs, unfolded_lhs = NULL_TREE, unfolded_lhs1 = NULL_TREE;
12402 enum tree_code code = OMP_ATOMIC, opcode = NOP_EXPR;
12403 struct c_expr expr;
12404 location_t eloc;
12405 bool structured_block = false;
12406 bool swapped = false;
12407 bool seq_cst = false;
12409 if (c_parser_next_token_is (parser, CPP_NAME))
12411 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
12412 if (!strcmp (p, "seq_cst"))
12414 seq_cst = true;
12415 c_parser_consume_token (parser);
12416 if (c_parser_next_token_is (parser, CPP_COMMA)
12417 && c_parser_peek_2nd_token (parser)->type == CPP_NAME)
12418 c_parser_consume_token (parser);
12421 if (c_parser_next_token_is (parser, CPP_NAME))
12423 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
12425 if (!strcmp (p, "read"))
12426 code = OMP_ATOMIC_READ;
12427 else if (!strcmp (p, "write"))
12428 code = NOP_EXPR;
12429 else if (!strcmp (p, "update"))
12430 code = OMP_ATOMIC;
12431 else if (!strcmp (p, "capture"))
12432 code = OMP_ATOMIC_CAPTURE_NEW;
12433 else
12434 p = NULL;
12435 if (p)
12436 c_parser_consume_token (parser);
12438 if (!seq_cst)
12440 if (c_parser_next_token_is (parser, CPP_COMMA)
12441 && c_parser_peek_2nd_token (parser)->type == CPP_NAME)
12442 c_parser_consume_token (parser);
12444 if (c_parser_next_token_is (parser, CPP_NAME))
12446 const char *p
12447 = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
12448 if (!strcmp (p, "seq_cst"))
12450 seq_cst = true;
12451 c_parser_consume_token (parser);
12455 c_parser_skip_to_pragma_eol (parser);
12457 switch (code)
12459 case OMP_ATOMIC_READ:
12460 case NOP_EXPR: /* atomic write */
12461 v = c_parser_unary_expression (parser).value;
12462 v = c_fully_fold (v, false, NULL);
12463 if (v == error_mark_node)
12464 goto saw_error;
12465 loc = c_parser_peek_token (parser)->location;
12466 if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
12467 goto saw_error;
12468 if (code == NOP_EXPR)
12469 lhs = c_parser_expression (parser).value;
12470 else
12471 lhs = c_parser_unary_expression (parser).value;
12472 lhs = c_fully_fold (lhs, false, NULL);
12473 if (lhs == error_mark_node)
12474 goto saw_error;
12475 if (code == NOP_EXPR)
12477 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
12478 opcode. */
12479 code = OMP_ATOMIC;
12480 rhs = lhs;
12481 lhs = v;
12482 v = NULL_TREE;
12484 goto done;
12485 case OMP_ATOMIC_CAPTURE_NEW:
12486 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
12488 c_parser_consume_token (parser);
12489 structured_block = true;
12491 else
12493 v = c_parser_unary_expression (parser).value;
12494 v = c_fully_fold (v, false, NULL);
12495 if (v == error_mark_node)
12496 goto saw_error;
12497 if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
12498 goto saw_error;
12500 break;
12501 default:
12502 break;
12505 /* For structured_block case we don't know yet whether
12506 old or new x should be captured. */
12507 restart:
12508 eloc = c_parser_peek_token (parser)->location;
12509 expr = c_parser_unary_expression (parser);
12510 lhs = expr.value;
12511 expr = default_function_array_conversion (eloc, expr);
12512 unfolded_lhs = expr.value;
12513 lhs = c_fully_fold (lhs, false, NULL);
12514 orig_lhs = lhs;
12515 switch (TREE_CODE (lhs))
12517 case ERROR_MARK:
12518 saw_error:
12519 c_parser_skip_to_end_of_block_or_statement (parser);
12520 if (structured_block)
12522 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
12523 c_parser_consume_token (parser);
12524 else if (code == OMP_ATOMIC_CAPTURE_NEW)
12526 c_parser_skip_to_end_of_block_or_statement (parser);
12527 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
12528 c_parser_consume_token (parser);
12531 return;
12533 case POSTINCREMENT_EXPR:
12534 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
12535 code = OMP_ATOMIC_CAPTURE_OLD;
12536 /* FALLTHROUGH */
12537 case PREINCREMENT_EXPR:
12538 lhs = TREE_OPERAND (lhs, 0);
12539 unfolded_lhs = NULL_TREE;
12540 opcode = PLUS_EXPR;
12541 rhs = integer_one_node;
12542 break;
12544 case POSTDECREMENT_EXPR:
12545 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
12546 code = OMP_ATOMIC_CAPTURE_OLD;
12547 /* FALLTHROUGH */
12548 case PREDECREMENT_EXPR:
12549 lhs = TREE_OPERAND (lhs, 0);
12550 unfolded_lhs = NULL_TREE;
12551 opcode = MINUS_EXPR;
12552 rhs = integer_one_node;
12553 break;
12555 case COMPOUND_EXPR:
12556 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
12557 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
12558 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
12559 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
12560 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
12561 (TREE_OPERAND (lhs, 1), 0), 0)))
12562 == BOOLEAN_TYPE)
12563 /* Undo effects of boolean_increment for post {in,de}crement. */
12564 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
12565 /* FALLTHRU */
12566 case MODIFY_EXPR:
12567 if (TREE_CODE (lhs) == MODIFY_EXPR
12568 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
12570 /* Undo effects of boolean_increment. */
12571 if (integer_onep (TREE_OPERAND (lhs, 1)))
12573 /* This is pre or post increment. */
12574 rhs = TREE_OPERAND (lhs, 1);
12575 lhs = TREE_OPERAND (lhs, 0);
12576 unfolded_lhs = NULL_TREE;
12577 opcode = NOP_EXPR;
12578 if (code == OMP_ATOMIC_CAPTURE_NEW
12579 && !structured_block
12580 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
12581 code = OMP_ATOMIC_CAPTURE_OLD;
12582 break;
12584 if (TREE_CODE (TREE_OPERAND (lhs, 1)) == TRUTH_NOT_EXPR
12585 && TREE_OPERAND (lhs, 0)
12586 == TREE_OPERAND (TREE_OPERAND (lhs, 1), 0))
12588 /* This is pre or post decrement. */
12589 rhs = TREE_OPERAND (lhs, 1);
12590 lhs = TREE_OPERAND (lhs, 0);
12591 unfolded_lhs = NULL_TREE;
12592 opcode = NOP_EXPR;
12593 if (code == OMP_ATOMIC_CAPTURE_NEW
12594 && !structured_block
12595 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
12596 code = OMP_ATOMIC_CAPTURE_OLD;
12597 break;
12600 /* FALLTHRU */
12601 default:
12602 switch (c_parser_peek_token (parser)->type)
12604 case CPP_MULT_EQ:
12605 opcode = MULT_EXPR;
12606 break;
12607 case CPP_DIV_EQ:
12608 opcode = TRUNC_DIV_EXPR;
12609 break;
12610 case CPP_PLUS_EQ:
12611 opcode = PLUS_EXPR;
12612 break;
12613 case CPP_MINUS_EQ:
12614 opcode = MINUS_EXPR;
12615 break;
12616 case CPP_LSHIFT_EQ:
12617 opcode = LSHIFT_EXPR;
12618 break;
12619 case CPP_RSHIFT_EQ:
12620 opcode = RSHIFT_EXPR;
12621 break;
12622 case CPP_AND_EQ:
12623 opcode = BIT_AND_EXPR;
12624 break;
12625 case CPP_OR_EQ:
12626 opcode = BIT_IOR_EXPR;
12627 break;
12628 case CPP_XOR_EQ:
12629 opcode = BIT_XOR_EXPR;
12630 break;
12631 case CPP_EQ:
12632 c_parser_consume_token (parser);
12633 eloc = c_parser_peek_token (parser)->location;
12634 expr = c_parser_expr_no_commas (parser, NULL, unfolded_lhs);
12635 rhs1 = expr.value;
12636 switch (TREE_CODE (rhs1))
12638 case MULT_EXPR:
12639 case TRUNC_DIV_EXPR:
12640 case RDIV_EXPR:
12641 case PLUS_EXPR:
12642 case MINUS_EXPR:
12643 case LSHIFT_EXPR:
12644 case RSHIFT_EXPR:
12645 case BIT_AND_EXPR:
12646 case BIT_IOR_EXPR:
12647 case BIT_XOR_EXPR:
12648 if (c_tree_equal (TREE_OPERAND (rhs1, 0), unfolded_lhs))
12650 opcode = TREE_CODE (rhs1);
12651 rhs = c_fully_fold (TREE_OPERAND (rhs1, 1), false, NULL);
12652 rhs1 = c_fully_fold (TREE_OPERAND (rhs1, 0), false, NULL);
12653 goto stmt_done;
12655 if (c_tree_equal (TREE_OPERAND (rhs1, 1), unfolded_lhs))
12657 opcode = TREE_CODE (rhs1);
12658 rhs = c_fully_fold (TREE_OPERAND (rhs1, 0), false, NULL);
12659 rhs1 = c_fully_fold (TREE_OPERAND (rhs1, 1), false, NULL);
12660 swapped = !commutative_tree_code (opcode);
12661 goto stmt_done;
12663 break;
12664 case ERROR_MARK:
12665 goto saw_error;
12666 default:
12667 break;
12669 if (c_parser_peek_token (parser)->type == CPP_SEMICOLON)
12671 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
12673 code = OMP_ATOMIC_CAPTURE_OLD;
12674 v = lhs;
12675 lhs = NULL_TREE;
12676 expr = default_function_array_read_conversion (eloc, expr);
12677 unfolded_lhs1 = expr.value;
12678 lhs1 = c_fully_fold (unfolded_lhs1, false, NULL);
12679 rhs1 = NULL_TREE;
12680 c_parser_consume_token (parser);
12681 goto restart;
12683 if (structured_block)
12685 opcode = NOP_EXPR;
12686 expr = default_function_array_read_conversion (eloc, expr);
12687 rhs = c_fully_fold (expr.value, false, NULL);
12688 rhs1 = NULL_TREE;
12689 goto stmt_done;
12692 c_parser_error (parser, "invalid form of %<#pragma omp atomic%>");
12693 goto saw_error;
12694 default:
12695 c_parser_error (parser,
12696 "invalid operator for %<#pragma omp atomic%>");
12697 goto saw_error;
12700 /* Arrange to pass the location of the assignment operator to
12701 c_finish_omp_atomic. */
12702 loc = c_parser_peek_token (parser)->location;
12703 c_parser_consume_token (parser);
12704 eloc = c_parser_peek_token (parser)->location;
12705 expr = c_parser_expression (parser);
12706 expr = default_function_array_read_conversion (eloc, expr);
12707 rhs = expr.value;
12708 rhs = c_fully_fold (rhs, false, NULL);
12709 break;
12711 stmt_done:
12712 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
12714 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
12715 goto saw_error;
12716 v = c_parser_unary_expression (parser).value;
12717 v = c_fully_fold (v, false, NULL);
12718 if (v == error_mark_node)
12719 goto saw_error;
12720 if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
12721 goto saw_error;
12722 eloc = c_parser_peek_token (parser)->location;
12723 expr = c_parser_unary_expression (parser);
12724 lhs1 = expr.value;
12725 expr = default_function_array_read_conversion (eloc, expr);
12726 unfolded_lhs1 = expr.value;
12727 lhs1 = c_fully_fold (lhs1, false, NULL);
12728 if (lhs1 == error_mark_node)
12729 goto saw_error;
12731 if (structured_block)
12733 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
12734 c_parser_require (parser, CPP_CLOSE_BRACE, "expected %<}%>");
12736 done:
12737 if (unfolded_lhs && unfolded_lhs1
12738 && !c_tree_equal (unfolded_lhs, unfolded_lhs1))
12740 error ("%<#pragma omp atomic capture%> uses two different "
12741 "expressions for memory");
12742 stmt = error_mark_node;
12744 else
12745 stmt = c_finish_omp_atomic (loc, code, opcode, lhs, rhs, v, lhs1, rhs1,
12746 swapped, seq_cst);
12747 if (stmt != error_mark_node)
12748 add_stmt (stmt);
12750 if (!structured_block)
12751 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
12755 /* OpenMP 2.5:
12756 # pragma omp barrier new-line
12759 static void
12760 c_parser_omp_barrier (c_parser *parser)
12762 location_t loc = c_parser_peek_token (parser)->location;
12763 c_parser_consume_pragma (parser);
12764 c_parser_skip_to_pragma_eol (parser);
12766 c_finish_omp_barrier (loc);
12769 /* OpenMP 2.5:
12770 # pragma omp critical [(name)] new-line
12771 structured-block
12773 LOC is the location of the #pragma itself. */
12775 static tree
12776 c_parser_omp_critical (location_t loc, c_parser *parser)
12778 tree stmt, name = NULL;
12780 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
12782 c_parser_consume_token (parser);
12783 if (c_parser_next_token_is (parser, CPP_NAME))
12785 name = c_parser_peek_token (parser)->value;
12786 c_parser_consume_token (parser);
12787 c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
12789 else
12790 c_parser_error (parser, "expected identifier");
12792 else if (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
12793 c_parser_error (parser, "expected %<(%> or end of line");
12794 c_parser_skip_to_pragma_eol (parser);
12796 stmt = c_parser_omp_structured_block (parser);
12797 return c_finish_omp_critical (loc, stmt, name);
12800 /* OpenMP 2.5:
12801 # pragma omp flush flush-vars[opt] new-line
12803 flush-vars:
12804 ( variable-list ) */
12806 static void
12807 c_parser_omp_flush (c_parser *parser)
12809 location_t loc = c_parser_peek_token (parser)->location;
12810 c_parser_consume_pragma (parser);
12811 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
12812 c_parser_omp_var_list_parens (parser, OMP_CLAUSE_ERROR, NULL);
12813 else if (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
12814 c_parser_error (parser, "expected %<(%> or end of line");
12815 c_parser_skip_to_pragma_eol (parser);
12817 c_finish_omp_flush (loc);
12820 /* Parse the restricted form of loop statements allowed by OpenACC and OpenMP.
12821 The real trick here is to determine the loop control variable early
12822 so that we can push a new decl if necessary to make it private.
12823 LOC is the location of the "acc" or "omp" in "#pragma acc" or "#pragma omp",
12824 respectively. */
12826 static tree
12827 c_parser_omp_for_loop (location_t loc, c_parser *parser, enum tree_code code,
12828 tree clauses, tree *cclauses)
12830 tree decl, cond, incr, save_break, save_cont, body, init, stmt, cl;
12831 tree declv, condv, incrv, initv, ret = NULL;
12832 bool fail = false, open_brace_parsed = false;
12833 int i, collapse = 1, nbraces = 0;
12834 location_t for_loc;
12835 vec<tree, va_gc> *for_block = make_tree_vector ();
12837 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
12838 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
12839 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl));
12841 gcc_assert (collapse >= 1);
12843 declv = make_tree_vec (collapse);
12844 initv = make_tree_vec (collapse);
12845 condv = make_tree_vec (collapse);
12846 incrv = make_tree_vec (collapse);
12848 if (code != CILK_FOR
12849 && !c_parser_next_token_is_keyword (parser, RID_FOR))
12851 c_parser_error (parser, "for statement expected");
12852 return NULL;
12854 if (code == CILK_FOR
12855 && !c_parser_next_token_is_keyword (parser, RID_CILK_FOR))
12857 c_parser_error (parser, "_Cilk_for statement expected");
12858 return NULL;
12860 for_loc = c_parser_peek_token (parser)->location;
12861 c_parser_consume_token (parser);
12863 for (i = 0; i < collapse; i++)
12865 int bracecount = 0;
12867 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
12868 goto pop_scopes;
12870 /* Parse the initialization declaration or expression. */
12871 if (c_parser_next_tokens_start_declaration (parser))
12873 if (i > 0)
12874 vec_safe_push (for_block, c_begin_compound_stmt (true));
12875 c_parser_declaration_or_fndef (parser, true, true, true, true, true,
12876 NULL, vNULL);
12877 decl = check_for_loop_decls (for_loc, flag_isoc99);
12878 if (decl == NULL)
12879 goto error_init;
12880 if (DECL_INITIAL (decl) == error_mark_node)
12881 decl = error_mark_node;
12882 init = decl;
12884 else if (c_parser_next_token_is (parser, CPP_NAME)
12885 && c_parser_peek_2nd_token (parser)->type == CPP_EQ)
12887 struct c_expr decl_exp;
12888 struct c_expr init_exp;
12889 location_t init_loc;
12891 decl_exp = c_parser_postfix_expression (parser);
12892 decl = decl_exp.value;
12894 c_parser_require (parser, CPP_EQ, "expected %<=%>");
12896 init_loc = c_parser_peek_token (parser)->location;
12897 init_exp = c_parser_expr_no_commas (parser, NULL);
12898 init_exp = default_function_array_read_conversion (init_loc,
12899 init_exp);
12900 init = build_modify_expr (init_loc, decl, decl_exp.original_type,
12901 NOP_EXPR, init_loc, init_exp.value,
12902 init_exp.original_type);
12903 init = c_process_expr_stmt (init_loc, init);
12905 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
12907 else
12909 error_init:
12910 c_parser_error (parser,
12911 "expected iteration declaration or initialization");
12912 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
12913 "expected %<)%>");
12914 fail = true;
12915 goto parse_next;
12918 /* Parse the loop condition. */
12919 cond = NULL_TREE;
12920 if (c_parser_next_token_is_not (parser, CPP_SEMICOLON))
12922 location_t cond_loc = c_parser_peek_token (parser)->location;
12923 struct c_expr cond_expr
12924 = c_parser_binary_expression (parser, NULL, NULL_TREE);
12926 cond = cond_expr.value;
12927 cond = c_objc_common_truthvalue_conversion (cond_loc, cond);
12928 cond = c_fully_fold (cond, false, NULL);
12929 switch (cond_expr.original_code)
12931 case GT_EXPR:
12932 case GE_EXPR:
12933 case LT_EXPR:
12934 case LE_EXPR:
12935 break;
12936 case NE_EXPR:
12937 if (code == CILK_SIMD || code == CILK_FOR)
12938 break;
12939 /* FALLTHRU. */
12940 default:
12941 /* Can't be cond = error_mark_node, because we want to preserve
12942 the location until c_finish_omp_for. */
12943 cond = build1 (NOP_EXPR, boolean_type_node, error_mark_node);
12944 break;
12946 protected_set_expr_location (cond, cond_loc);
12948 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
12950 /* Parse the increment expression. */
12951 incr = NULL_TREE;
12952 if (c_parser_next_token_is_not (parser, CPP_CLOSE_PAREN))
12954 location_t incr_loc = c_parser_peek_token (parser)->location;
12956 incr = c_process_expr_stmt (incr_loc,
12957 c_parser_expression (parser).value);
12959 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
12961 if (decl == NULL || decl == error_mark_node || init == error_mark_node)
12962 fail = true;
12963 else
12965 TREE_VEC_ELT (declv, i) = decl;
12966 TREE_VEC_ELT (initv, i) = init;
12967 TREE_VEC_ELT (condv, i) = cond;
12968 TREE_VEC_ELT (incrv, i) = incr;
12971 parse_next:
12972 if (i == collapse - 1)
12973 break;
12975 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
12976 in between the collapsed for loops to be still considered perfectly
12977 nested. Hopefully the final version clarifies this.
12978 For now handle (multiple) {'s and empty statements. */
12981 if (c_parser_next_token_is_keyword (parser, RID_FOR))
12983 c_parser_consume_token (parser);
12984 break;
12986 else if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
12988 c_parser_consume_token (parser);
12989 bracecount++;
12991 else if (bracecount
12992 && c_parser_next_token_is (parser, CPP_SEMICOLON))
12993 c_parser_consume_token (parser);
12994 else
12996 c_parser_error (parser, "not enough perfectly nested loops");
12997 if (bracecount)
12999 open_brace_parsed = true;
13000 bracecount--;
13002 fail = true;
13003 collapse = 0;
13004 break;
13007 while (1);
13009 nbraces += bracecount;
13012 save_break = c_break_label;
13013 if (code == CILK_SIMD)
13014 c_break_label = build_int_cst (size_type_node, 2);
13015 else
13016 c_break_label = size_one_node;
13017 save_cont = c_cont_label;
13018 c_cont_label = NULL_TREE;
13019 body = push_stmt_list ();
13021 if (open_brace_parsed)
13023 location_t here = c_parser_peek_token (parser)->location;
13024 stmt = c_begin_compound_stmt (true);
13025 c_parser_compound_statement_nostart (parser);
13026 add_stmt (c_end_compound_stmt (here, stmt, true));
13028 else
13029 add_stmt (c_parser_c99_block_statement (parser));
13030 if (c_cont_label)
13032 tree t = build1 (LABEL_EXPR, void_type_node, c_cont_label);
13033 SET_EXPR_LOCATION (t, loc);
13034 add_stmt (t);
13037 body = pop_stmt_list (body);
13038 c_break_label = save_break;
13039 c_cont_label = save_cont;
13041 while (nbraces)
13043 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
13045 c_parser_consume_token (parser);
13046 nbraces--;
13048 else if (c_parser_next_token_is (parser, CPP_SEMICOLON))
13049 c_parser_consume_token (parser);
13050 else
13052 c_parser_error (parser, "collapsed loops not perfectly nested");
13053 while (nbraces)
13055 location_t here = c_parser_peek_token (parser)->location;
13056 stmt = c_begin_compound_stmt (true);
13057 add_stmt (body);
13058 c_parser_compound_statement_nostart (parser);
13059 body = c_end_compound_stmt (here, stmt, true);
13060 nbraces--;
13062 goto pop_scopes;
13066 /* Only bother calling c_finish_omp_for if we haven't already generated
13067 an error from the initialization parsing. */
13068 if (!fail)
13070 stmt = c_finish_omp_for (loc, code, declv, initv, condv,
13071 incrv, body, NULL);
13072 if (stmt)
13074 if (cclauses != NULL
13075 && cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL] != NULL)
13077 tree *c;
13078 for (c = &cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL]; *c ; )
13079 if (OMP_CLAUSE_CODE (*c) != OMP_CLAUSE_FIRSTPRIVATE
13080 && OMP_CLAUSE_CODE (*c) != OMP_CLAUSE_LASTPRIVATE)
13081 c = &OMP_CLAUSE_CHAIN (*c);
13082 else
13084 for (i = 0; i < collapse; i++)
13085 if (TREE_VEC_ELT (declv, i) == OMP_CLAUSE_DECL (*c))
13086 break;
13087 if (i == collapse)
13088 c = &OMP_CLAUSE_CHAIN (*c);
13089 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE)
13091 error_at (loc,
13092 "iteration variable %qD should not be firstprivate",
13093 OMP_CLAUSE_DECL (*c));
13094 *c = OMP_CLAUSE_CHAIN (*c);
13096 else
13098 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
13099 tree l = *c;
13100 *c = OMP_CLAUSE_CHAIN (*c);
13101 if (code == OMP_SIMD)
13103 OMP_CLAUSE_CHAIN (l)
13104 = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
13105 cclauses[C_OMP_CLAUSE_SPLIT_FOR] = l;
13107 else
13109 OMP_CLAUSE_CHAIN (l) = clauses;
13110 clauses = l;
13115 OMP_FOR_CLAUSES (stmt) = clauses;
13117 ret = stmt;
13119 pop_scopes:
13120 while (!for_block->is_empty ())
13122 /* FIXME diagnostics: LOC below should be the actual location of
13123 this particular for block. We need to build a list of
13124 locations to go along with FOR_BLOCK. */
13125 stmt = c_end_compound_stmt (loc, for_block->pop (), true);
13126 add_stmt (stmt);
13128 release_tree_vector (for_block);
13129 return ret;
13132 /* Helper function for OpenMP parsing, split clauses and call
13133 finish_omp_clauses on each of the set of clauses afterwards. */
13135 static void
13136 omp_split_clauses (location_t loc, enum tree_code code,
13137 omp_clause_mask mask, tree clauses, tree *cclauses)
13139 int i;
13140 c_omp_split_clauses (loc, code, mask, clauses, cclauses);
13141 for (i = 0; i < C_OMP_CLAUSE_SPLIT_COUNT; i++)
13142 if (cclauses[i])
13143 cclauses[i] = c_finish_omp_clauses (cclauses[i]);
13146 /* OpenMP 4.0:
13147 #pragma omp simd simd-clause[optseq] new-line
13148 for-loop
13150 LOC is the location of the #pragma token.
13153 #define OMP_SIMD_CLAUSE_MASK \
13154 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
13155 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
13156 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
13157 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
13158 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
13159 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
13160 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
13162 static tree
13163 c_parser_omp_simd (location_t loc, c_parser *parser,
13164 char *p_name, omp_clause_mask mask, tree *cclauses)
13166 tree block, clauses, ret;
13168 strcat (p_name, " simd");
13169 mask |= OMP_SIMD_CLAUSE_MASK;
13170 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED);
13172 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
13173 if (cclauses)
13175 omp_split_clauses (loc, OMP_SIMD, mask, clauses, cclauses);
13176 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SIMD];
13179 block = c_begin_compound_stmt (true);
13180 ret = c_parser_omp_for_loop (loc, parser, OMP_SIMD, clauses, cclauses);
13181 block = c_end_compound_stmt (loc, block, true);
13182 add_stmt (block);
13184 return ret;
13187 /* OpenMP 2.5:
13188 #pragma omp for for-clause[optseq] new-line
13189 for-loop
13191 OpenMP 4.0:
13192 #pragma omp for simd for-simd-clause[optseq] new-line
13193 for-loop
13195 LOC is the location of the #pragma token.
13198 #define OMP_FOR_CLAUSE_MASK \
13199 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
13200 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
13201 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
13202 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
13203 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
13204 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
13205 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
13206 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
13208 static tree
13209 c_parser_omp_for (location_t loc, c_parser *parser,
13210 char *p_name, omp_clause_mask mask, tree *cclauses)
13212 tree block, clauses, ret;
13214 strcat (p_name, " for");
13215 mask |= OMP_FOR_CLAUSE_MASK;
13216 if (cclauses)
13217 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
13219 if (c_parser_next_token_is (parser, CPP_NAME))
13221 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
13223 if (strcmp (p, "simd") == 0)
13225 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
13226 if (cclauses == NULL)
13227 cclauses = cclauses_buf;
13229 c_parser_consume_token (parser);
13230 if (!flag_openmp) /* flag_openmp_simd */
13231 return c_parser_omp_simd (loc, parser, p_name, mask, cclauses);
13232 block = c_begin_compound_stmt (true);
13233 ret = c_parser_omp_simd (loc, parser, p_name, mask, cclauses);
13234 block = c_end_compound_stmt (loc, block, true);
13235 if (ret == NULL_TREE)
13236 return ret;
13237 ret = make_node (OMP_FOR);
13238 TREE_TYPE (ret) = void_type_node;
13239 OMP_FOR_BODY (ret) = block;
13240 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
13241 SET_EXPR_LOCATION (ret, loc);
13242 add_stmt (ret);
13243 return ret;
13246 if (!flag_openmp) /* flag_openmp_simd */
13248 c_parser_skip_to_pragma_eol (parser, false);
13249 return NULL_TREE;
13252 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
13253 if (cclauses)
13255 omp_split_clauses (loc, OMP_FOR, mask, clauses, cclauses);
13256 clauses = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
13259 block = c_begin_compound_stmt (true);
13260 ret = c_parser_omp_for_loop (loc, parser, OMP_FOR, clauses, cclauses);
13261 block = c_end_compound_stmt (loc, block, true);
13262 add_stmt (block);
13264 return ret;
13267 /* OpenMP 2.5:
13268 # pragma omp master new-line
13269 structured-block
13271 LOC is the location of the #pragma token.
13274 static tree
13275 c_parser_omp_master (location_t loc, c_parser *parser)
13277 c_parser_skip_to_pragma_eol (parser);
13278 return c_finish_omp_master (loc, c_parser_omp_structured_block (parser));
13281 /* OpenMP 2.5:
13282 # pragma omp ordered new-line
13283 structured-block
13285 LOC is the location of the #pragma itself.
13288 static tree
13289 c_parser_omp_ordered (location_t loc, c_parser *parser)
13291 c_parser_skip_to_pragma_eol (parser);
13292 return c_finish_omp_ordered (loc, c_parser_omp_structured_block (parser));
13295 /* OpenMP 2.5:
13297 section-scope:
13298 { section-sequence }
13300 section-sequence:
13301 section-directive[opt] structured-block
13302 section-sequence section-directive structured-block
13304 SECTIONS_LOC is the location of the #pragma omp sections. */
13306 static tree
13307 c_parser_omp_sections_scope (location_t sections_loc, c_parser *parser)
13309 tree stmt, substmt;
13310 bool error_suppress = false;
13311 location_t loc;
13313 loc = c_parser_peek_token (parser)->location;
13314 if (!c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
13316 /* Avoid skipping until the end of the block. */
13317 parser->error = false;
13318 return NULL_TREE;
13321 stmt = push_stmt_list ();
13323 if (c_parser_peek_token (parser)->pragma_kind != PRAGMA_OMP_SECTION)
13325 substmt = c_parser_omp_structured_block (parser);
13326 substmt = build1 (OMP_SECTION, void_type_node, substmt);
13327 SET_EXPR_LOCATION (substmt, loc);
13328 add_stmt (substmt);
13331 while (1)
13333 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
13334 break;
13335 if (c_parser_next_token_is (parser, CPP_EOF))
13336 break;
13338 loc = c_parser_peek_token (parser)->location;
13339 if (c_parser_peek_token (parser)->pragma_kind == PRAGMA_OMP_SECTION)
13341 c_parser_consume_pragma (parser);
13342 c_parser_skip_to_pragma_eol (parser);
13343 error_suppress = false;
13345 else if (!error_suppress)
13347 error_at (loc, "expected %<#pragma omp section%> or %<}%>");
13348 error_suppress = true;
13351 substmt = c_parser_omp_structured_block (parser);
13352 substmt = build1 (OMP_SECTION, void_type_node, substmt);
13353 SET_EXPR_LOCATION (substmt, loc);
13354 add_stmt (substmt);
13356 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE,
13357 "expected %<#pragma omp section%> or %<}%>");
13359 substmt = pop_stmt_list (stmt);
13361 stmt = make_node (OMP_SECTIONS);
13362 SET_EXPR_LOCATION (stmt, sections_loc);
13363 TREE_TYPE (stmt) = void_type_node;
13364 OMP_SECTIONS_BODY (stmt) = substmt;
13366 return add_stmt (stmt);
13369 /* OpenMP 2.5:
13370 # pragma omp sections sections-clause[optseq] newline
13371 sections-scope
13373 LOC is the location of the #pragma token.
13376 #define OMP_SECTIONS_CLAUSE_MASK \
13377 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
13378 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
13379 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
13380 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
13381 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
13383 static tree
13384 c_parser_omp_sections (location_t loc, c_parser *parser,
13385 char *p_name, omp_clause_mask mask, tree *cclauses)
13387 tree block, clauses, ret;
13389 strcat (p_name, " sections");
13390 mask |= OMP_SECTIONS_CLAUSE_MASK;
13391 if (cclauses)
13392 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
13394 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
13395 if (cclauses)
13397 omp_split_clauses (loc, OMP_SECTIONS, mask, clauses, cclauses);
13398 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SECTIONS];
13401 block = c_begin_compound_stmt (true);
13402 ret = c_parser_omp_sections_scope (loc, parser);
13403 if (ret)
13404 OMP_SECTIONS_CLAUSES (ret) = clauses;
13405 block = c_end_compound_stmt (loc, block, true);
13406 add_stmt (block);
13408 return ret;
13411 /* OpenMP 2.5:
13412 # pragma omp parallel parallel-clause[optseq] new-line
13413 structured-block
13414 # pragma omp parallel for parallel-for-clause[optseq] new-line
13415 structured-block
13416 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
13417 structured-block
13419 OpenMP 4.0:
13420 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
13421 structured-block
13423 LOC is the location of the #pragma token.
13426 #define OMP_PARALLEL_CLAUSE_MASK \
13427 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
13428 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
13429 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
13430 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
13431 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
13432 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
13433 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
13434 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
13435 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
13437 static tree
13438 c_parser_omp_parallel (location_t loc, c_parser *parser,
13439 char *p_name, omp_clause_mask mask, tree *cclauses)
13441 tree stmt, clauses, block;
13443 strcat (p_name, " parallel");
13444 mask |= OMP_PARALLEL_CLAUSE_MASK;
13446 if (c_parser_next_token_is_keyword (parser, RID_FOR))
13448 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
13449 if (cclauses == NULL)
13450 cclauses = cclauses_buf;
13452 c_parser_consume_token (parser);
13453 if (!flag_openmp) /* flag_openmp_simd */
13454 return c_parser_omp_for (loc, parser, p_name, mask, cclauses);
13455 block = c_begin_omp_parallel ();
13456 tree ret = c_parser_omp_for (loc, parser, p_name, mask, cclauses);
13457 stmt
13458 = c_finish_omp_parallel (loc, cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
13459 block);
13460 if (ret == NULL_TREE)
13461 return ret;
13462 OMP_PARALLEL_COMBINED (stmt) = 1;
13463 return stmt;
13465 else if (cclauses)
13467 error_at (loc, "expected %<for%> after %qs", p_name);
13468 c_parser_skip_to_pragma_eol (parser);
13469 return NULL_TREE;
13471 else if (!flag_openmp) /* flag_openmp_simd */
13473 c_parser_skip_to_pragma_eol (parser, false);
13474 return NULL_TREE;
13476 else if (c_parser_next_token_is (parser, CPP_NAME))
13478 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
13479 if (strcmp (p, "sections") == 0)
13481 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
13482 if (cclauses == NULL)
13483 cclauses = cclauses_buf;
13485 c_parser_consume_token (parser);
13486 block = c_begin_omp_parallel ();
13487 c_parser_omp_sections (loc, parser, p_name, mask, cclauses);
13488 stmt = c_finish_omp_parallel (loc,
13489 cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
13490 block);
13491 OMP_PARALLEL_COMBINED (stmt) = 1;
13492 return stmt;
13496 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
13498 block = c_begin_omp_parallel ();
13499 c_parser_statement (parser);
13500 stmt = c_finish_omp_parallel (loc, clauses, block);
13502 return stmt;
13505 /* OpenMP 2.5:
13506 # pragma omp single single-clause[optseq] new-line
13507 structured-block
13509 LOC is the location of the #pragma.
13512 #define OMP_SINGLE_CLAUSE_MASK \
13513 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
13514 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
13515 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
13516 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
13518 static tree
13519 c_parser_omp_single (location_t loc, c_parser *parser)
13521 tree stmt = make_node (OMP_SINGLE);
13522 SET_EXPR_LOCATION (stmt, loc);
13523 TREE_TYPE (stmt) = void_type_node;
13525 OMP_SINGLE_CLAUSES (stmt)
13526 = c_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
13527 "#pragma omp single");
13528 OMP_SINGLE_BODY (stmt) = c_parser_omp_structured_block (parser);
13530 return add_stmt (stmt);
13533 /* OpenMP 3.0:
13534 # pragma omp task task-clause[optseq] new-line
13536 LOC is the location of the #pragma.
13539 #define OMP_TASK_CLAUSE_MASK \
13540 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
13541 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
13542 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
13543 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
13544 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
13545 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
13546 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
13547 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
13548 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND))
13550 static tree
13551 c_parser_omp_task (location_t loc, c_parser *parser)
13553 tree clauses, block;
13555 clauses = c_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
13556 "#pragma omp task");
13558 block = c_begin_omp_task ();
13559 c_parser_statement (parser);
13560 return c_finish_omp_task (loc, clauses, block);
13563 /* OpenMP 3.0:
13564 # pragma omp taskwait new-line
13567 static void
13568 c_parser_omp_taskwait (c_parser *parser)
13570 location_t loc = c_parser_peek_token (parser)->location;
13571 c_parser_consume_pragma (parser);
13572 c_parser_skip_to_pragma_eol (parser);
13574 c_finish_omp_taskwait (loc);
13577 /* OpenMP 3.1:
13578 # pragma omp taskyield new-line
13581 static void
13582 c_parser_omp_taskyield (c_parser *parser)
13584 location_t loc = c_parser_peek_token (parser)->location;
13585 c_parser_consume_pragma (parser);
13586 c_parser_skip_to_pragma_eol (parser);
13588 c_finish_omp_taskyield (loc);
13591 /* OpenMP 4.0:
13592 # pragma omp taskgroup new-line
13595 static tree
13596 c_parser_omp_taskgroup (c_parser *parser)
13598 location_t loc = c_parser_peek_token (parser)->location;
13599 c_parser_skip_to_pragma_eol (parser);
13600 return c_finish_omp_taskgroup (loc, c_parser_omp_structured_block (parser));
13603 /* OpenMP 4.0:
13604 # pragma omp cancel cancel-clause[optseq] new-line
13606 LOC is the location of the #pragma.
13609 #define OMP_CANCEL_CLAUSE_MASK \
13610 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
13611 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
13612 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
13613 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
13614 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
13616 static void
13617 c_parser_omp_cancel (c_parser *parser)
13619 location_t loc = c_parser_peek_token (parser)->location;
13621 c_parser_consume_pragma (parser);
13622 tree clauses = c_parser_omp_all_clauses (parser, OMP_CANCEL_CLAUSE_MASK,
13623 "#pragma omp cancel");
13625 c_finish_omp_cancel (loc, clauses);
13628 /* OpenMP 4.0:
13629 # pragma omp cancellation point cancelpt-clause[optseq] new-line
13631 LOC is the location of the #pragma.
13634 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
13635 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
13636 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
13637 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
13638 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
13640 static void
13641 c_parser_omp_cancellation_point (c_parser *parser)
13643 location_t loc = c_parser_peek_token (parser)->location;
13644 tree clauses;
13645 bool point_seen = false;
13647 c_parser_consume_pragma (parser);
13648 if (c_parser_next_token_is (parser, CPP_NAME))
13650 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
13651 if (strcmp (p, "point") == 0)
13653 c_parser_consume_token (parser);
13654 point_seen = true;
13657 if (!point_seen)
13659 c_parser_error (parser, "expected %<point%>");
13660 c_parser_skip_to_pragma_eol (parser);
13661 return;
13664 clauses
13665 = c_parser_omp_all_clauses (parser, OMP_CANCELLATION_POINT_CLAUSE_MASK,
13666 "#pragma omp cancellation point");
13668 c_finish_omp_cancellation_point (loc, clauses);
13671 /* OpenMP 4.0:
13672 #pragma omp distribute distribute-clause[optseq] new-line
13673 for-loop */
13675 #define OMP_DISTRIBUTE_CLAUSE_MASK \
13676 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
13677 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
13678 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
13679 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
13681 static tree
13682 c_parser_omp_distribute (location_t loc, c_parser *parser,
13683 char *p_name, omp_clause_mask mask, tree *cclauses)
13685 tree clauses, block, ret;
13687 strcat (p_name, " distribute");
13688 mask |= OMP_DISTRIBUTE_CLAUSE_MASK;
13690 if (c_parser_next_token_is (parser, CPP_NAME))
13692 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
13693 bool simd = false;
13694 bool parallel = false;
13696 if (strcmp (p, "simd") == 0)
13697 simd = true;
13698 else
13699 parallel = strcmp (p, "parallel") == 0;
13700 if (parallel || simd)
13702 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
13703 if (cclauses == NULL)
13704 cclauses = cclauses_buf;
13705 c_parser_consume_token (parser);
13706 if (!flag_openmp) /* flag_openmp_simd */
13708 if (simd)
13709 return c_parser_omp_simd (loc, parser, p_name, mask, cclauses);
13710 else
13711 return c_parser_omp_parallel (loc, parser, p_name, mask,
13712 cclauses);
13714 block = c_begin_compound_stmt (true);
13715 if (simd)
13716 ret = c_parser_omp_simd (loc, parser, p_name, mask, cclauses);
13717 else
13718 ret = c_parser_omp_parallel (loc, parser, p_name, mask, cclauses);
13719 block = c_end_compound_stmt (loc, block, true);
13720 if (ret == NULL)
13721 return ret;
13722 ret = make_node (OMP_DISTRIBUTE);
13723 TREE_TYPE (ret) = void_type_node;
13724 OMP_FOR_BODY (ret) = block;
13725 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
13726 SET_EXPR_LOCATION (ret, loc);
13727 add_stmt (ret);
13728 return ret;
13731 if (!flag_openmp) /* flag_openmp_simd */
13733 c_parser_skip_to_pragma_eol (parser, false);
13734 return NULL_TREE;
13737 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
13738 if (cclauses)
13740 omp_split_clauses (loc, OMP_DISTRIBUTE, mask, clauses, cclauses);
13741 clauses = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
13744 block = c_begin_compound_stmt (true);
13745 ret = c_parser_omp_for_loop (loc, parser, OMP_DISTRIBUTE, clauses, NULL);
13746 block = c_end_compound_stmt (loc, block, true);
13747 add_stmt (block);
13749 return ret;
13752 /* OpenMP 4.0:
13753 # pragma omp teams teams-clause[optseq] new-line
13754 structured-block */
13756 #define OMP_TEAMS_CLAUSE_MASK \
13757 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
13758 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
13759 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
13760 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
13761 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
13762 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
13763 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
13765 static tree
13766 c_parser_omp_teams (location_t loc, c_parser *parser,
13767 char *p_name, omp_clause_mask mask, tree *cclauses)
13769 tree clauses, block, ret;
13771 strcat (p_name, " teams");
13772 mask |= OMP_TEAMS_CLAUSE_MASK;
13774 if (c_parser_next_token_is (parser, CPP_NAME))
13776 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
13777 if (strcmp (p, "distribute") == 0)
13779 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
13780 if (cclauses == NULL)
13781 cclauses = cclauses_buf;
13783 c_parser_consume_token (parser);
13784 if (!flag_openmp) /* flag_openmp_simd */
13785 return c_parser_omp_distribute (loc, parser, p_name, mask, cclauses);
13786 block = c_begin_compound_stmt (true);
13787 ret = c_parser_omp_distribute (loc, parser, p_name, mask, cclauses);
13788 block = c_end_compound_stmt (loc, block, true);
13789 if (ret == NULL)
13790 return ret;
13791 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
13792 ret = make_node (OMP_TEAMS);
13793 TREE_TYPE (ret) = void_type_node;
13794 OMP_TEAMS_CLAUSES (ret) = clauses;
13795 OMP_TEAMS_BODY (ret) = block;
13796 OMP_TEAMS_COMBINED (ret) = 1;
13797 return add_stmt (ret);
13800 if (!flag_openmp) /* flag_openmp_simd */
13802 c_parser_skip_to_pragma_eol (parser, false);
13803 return NULL_TREE;
13806 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
13807 if (cclauses)
13809 omp_split_clauses (loc, OMP_TEAMS, mask, clauses, cclauses);
13810 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
13813 tree stmt = make_node (OMP_TEAMS);
13814 TREE_TYPE (stmt) = void_type_node;
13815 OMP_TEAMS_CLAUSES (stmt) = clauses;
13816 OMP_TEAMS_BODY (stmt) = c_parser_omp_structured_block (parser);
13818 return add_stmt (stmt);
13821 /* OpenMP 4.0:
13822 # pragma omp target data target-data-clause[optseq] new-line
13823 structured-block */
13825 #define OMP_TARGET_DATA_CLAUSE_MASK \
13826 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
13827 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
13828 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
13830 static tree
13831 c_parser_omp_target_data (location_t loc, c_parser *parser)
13833 tree stmt = make_node (OMP_TARGET_DATA);
13834 TREE_TYPE (stmt) = void_type_node;
13836 OMP_TARGET_DATA_CLAUSES (stmt)
13837 = c_parser_omp_all_clauses (parser, OMP_TARGET_DATA_CLAUSE_MASK,
13838 "#pragma omp target data");
13839 keep_next_level ();
13840 tree block = c_begin_compound_stmt (true);
13841 add_stmt (c_parser_omp_structured_block (parser));
13842 OMP_TARGET_DATA_BODY (stmt) = c_end_compound_stmt (loc, block, true);
13844 SET_EXPR_LOCATION (stmt, loc);
13845 return add_stmt (stmt);
13848 /* OpenMP 4.0:
13849 # pragma omp target update target-update-clause[optseq] new-line */
13851 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
13852 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
13853 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
13854 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
13855 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
13857 static bool
13858 c_parser_omp_target_update (location_t loc, c_parser *parser,
13859 enum pragma_context context)
13861 if (context == pragma_stmt)
13863 error_at (loc,
13864 "%<#pragma omp target update%> may only be "
13865 "used in compound statements");
13866 c_parser_skip_to_pragma_eol (parser);
13867 return false;
13870 tree clauses
13871 = c_parser_omp_all_clauses (parser, OMP_TARGET_UPDATE_CLAUSE_MASK,
13872 "#pragma omp target update");
13873 if (find_omp_clause (clauses, OMP_CLAUSE_TO) == NULL_TREE
13874 && find_omp_clause (clauses, OMP_CLAUSE_FROM) == NULL_TREE)
13876 error_at (loc,
13877 "%<#pragma omp target update%> must contain at least one "
13878 "%<from%> or %<to%> clauses");
13879 return false;
13882 tree stmt = make_node (OMP_TARGET_UPDATE);
13883 TREE_TYPE (stmt) = void_type_node;
13884 OMP_TARGET_UPDATE_CLAUSES (stmt) = clauses;
13885 SET_EXPR_LOCATION (stmt, loc);
13886 add_stmt (stmt);
13887 return false;
13890 /* OpenMP 4.0:
13891 # pragma omp target target-clause[optseq] new-line
13892 structured-block */
13894 #define OMP_TARGET_CLAUSE_MASK \
13895 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
13896 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
13897 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
13899 static bool
13900 c_parser_omp_target (c_parser *parser, enum pragma_context context)
13902 location_t loc = c_parser_peek_token (parser)->location;
13903 c_parser_consume_pragma (parser);
13905 if (context != pragma_stmt && context != pragma_compound)
13907 c_parser_error (parser, "expected declaration specifiers");
13908 c_parser_skip_to_pragma_eol (parser);
13909 return false;
13912 if (c_parser_next_token_is (parser, CPP_NAME))
13914 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
13916 if (strcmp (p, "teams") == 0)
13918 tree cclauses[C_OMP_CLAUSE_SPLIT_COUNT];
13919 char p_name[sizeof ("#pragma omp target teams distribute "
13920 "parallel for simd")];
13922 c_parser_consume_token (parser);
13923 strcpy (p_name, "#pragma omp target");
13924 if (!flag_openmp) /* flag_openmp_simd */
13926 tree stmt = c_parser_omp_teams (loc, parser, p_name,
13927 OMP_TARGET_CLAUSE_MASK,
13928 cclauses);
13929 return stmt != NULL_TREE;
13931 keep_next_level ();
13932 tree block = c_begin_compound_stmt (true);
13933 tree ret = c_parser_omp_teams (loc, parser, p_name,
13934 OMP_TARGET_CLAUSE_MASK, cclauses);
13935 block = c_end_compound_stmt (loc, block, true);
13936 if (ret == NULL_TREE)
13937 return false;
13938 tree stmt = make_node (OMP_TARGET);
13939 TREE_TYPE (stmt) = void_type_node;
13940 OMP_TARGET_CLAUSES (stmt) = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
13941 OMP_TARGET_BODY (stmt) = block;
13942 add_stmt (stmt);
13943 return true;
13945 else if (!flag_openmp) /* flag_openmp_simd */
13947 c_parser_skip_to_pragma_eol (parser, false);
13948 return false;
13950 else if (strcmp (p, "data") == 0)
13952 c_parser_consume_token (parser);
13953 c_parser_omp_target_data (loc, parser);
13954 return true;
13956 else if (strcmp (p, "update") == 0)
13958 c_parser_consume_token (parser);
13959 return c_parser_omp_target_update (loc, parser, context);
13963 tree stmt = make_node (OMP_TARGET);
13964 TREE_TYPE (stmt) = void_type_node;
13966 OMP_TARGET_CLAUSES (stmt)
13967 = c_parser_omp_all_clauses (parser, OMP_TARGET_CLAUSE_MASK,
13968 "#pragma omp target");
13969 keep_next_level ();
13970 tree block = c_begin_compound_stmt (true);
13971 add_stmt (c_parser_omp_structured_block (parser));
13972 OMP_TARGET_BODY (stmt) = c_end_compound_stmt (loc, block, true);
13974 SET_EXPR_LOCATION (stmt, loc);
13975 add_stmt (stmt);
13976 return true;
13979 /* OpenMP 4.0:
13980 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
13982 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
13983 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
13984 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
13985 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
13986 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
13987 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
13988 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
13990 static void
13991 c_parser_omp_declare_simd (c_parser *parser, enum pragma_context context)
13993 vec<c_token> clauses = vNULL;
13994 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
13996 c_token *token = c_parser_peek_token (parser);
13997 if (token->type == CPP_EOF)
13999 c_parser_skip_to_pragma_eol (parser);
14000 clauses.release ();
14001 return;
14003 clauses.safe_push (*token);
14004 c_parser_consume_token (parser);
14006 clauses.safe_push (*c_parser_peek_token (parser));
14007 c_parser_skip_to_pragma_eol (parser);
14009 while (c_parser_next_token_is (parser, CPP_PRAGMA))
14011 if (c_parser_peek_token (parser)->pragma_kind
14012 != PRAGMA_OMP_DECLARE_REDUCTION
14013 || c_parser_peek_2nd_token (parser)->type != CPP_NAME
14014 || strcmp (IDENTIFIER_POINTER
14015 (c_parser_peek_2nd_token (parser)->value),
14016 "simd") != 0)
14018 c_parser_error (parser,
14019 "%<#pragma omp declare simd%> must be followed by "
14020 "function declaration or definition or another "
14021 "%<#pragma omp declare simd%>");
14022 clauses.release ();
14023 return;
14025 c_parser_consume_pragma (parser);
14026 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
14028 c_token *token = c_parser_peek_token (parser);
14029 if (token->type == CPP_EOF)
14031 c_parser_skip_to_pragma_eol (parser);
14032 clauses.release ();
14033 return;
14035 clauses.safe_push (*token);
14036 c_parser_consume_token (parser);
14038 clauses.safe_push (*c_parser_peek_token (parser));
14039 c_parser_skip_to_pragma_eol (parser);
14042 /* Make sure nothing tries to read past the end of the tokens. */
14043 c_token eof_token;
14044 memset (&eof_token, 0, sizeof (eof_token));
14045 eof_token.type = CPP_EOF;
14046 clauses.safe_push (eof_token);
14047 clauses.safe_push (eof_token);
14049 switch (context)
14051 case pragma_external:
14052 if (c_parser_next_token_is (parser, CPP_KEYWORD)
14053 && c_parser_peek_token (parser)->keyword == RID_EXTENSION)
14055 int ext = disable_extension_diagnostics ();
14057 c_parser_consume_token (parser);
14058 while (c_parser_next_token_is (parser, CPP_KEYWORD)
14059 && c_parser_peek_token (parser)->keyword == RID_EXTENSION);
14060 c_parser_declaration_or_fndef (parser, true, true, true, false, true,
14061 NULL, clauses);
14062 restore_extension_diagnostics (ext);
14064 else
14065 c_parser_declaration_or_fndef (parser, true, true, true, false, true,
14066 NULL, clauses);
14067 break;
14068 case pragma_struct:
14069 case pragma_param:
14070 c_parser_error (parser, "%<#pragma omp declare simd%> must be followed by "
14071 "function declaration or definition");
14072 break;
14073 case pragma_compound:
14074 case pragma_stmt:
14075 if (c_parser_next_token_is (parser, CPP_KEYWORD)
14076 && c_parser_peek_token (parser)->keyword == RID_EXTENSION)
14078 int ext = disable_extension_diagnostics ();
14080 c_parser_consume_token (parser);
14081 while (c_parser_next_token_is (parser, CPP_KEYWORD)
14082 && c_parser_peek_token (parser)->keyword == RID_EXTENSION);
14083 if (c_parser_next_tokens_start_declaration (parser))
14085 c_parser_declaration_or_fndef (parser, true, true, true, true,
14086 true, NULL, clauses);
14087 restore_extension_diagnostics (ext);
14088 break;
14090 restore_extension_diagnostics (ext);
14092 else if (c_parser_next_tokens_start_declaration (parser))
14094 c_parser_declaration_or_fndef (parser, true, true, true, true, true,
14095 NULL, clauses);
14096 break;
14098 c_parser_error (parser, "%<#pragma omp declare simd%> must be followed by "
14099 "function declaration or definition");
14100 break;
14101 default:
14102 gcc_unreachable ();
14104 clauses.release ();
14107 /* Finalize #pragma omp declare simd clauses after FNDECL has been parsed,
14108 and put that into "omp declare simd" attribute. */
14110 static void
14111 c_finish_omp_declare_simd (c_parser *parser, tree fndecl, tree parms,
14112 vec<c_token> clauses)
14114 if (flag_cilkplus
14115 && clauses.exists () && !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
14117 error ("%<#pragma omp declare simd%> cannot be used in the same "
14118 "function marked as a Cilk Plus SIMD-enabled function");
14119 vec_free (parser->cilk_simd_fn_tokens);
14120 return;
14123 /* Normally first token is CPP_NAME "simd". CPP_EOF there indicates
14124 error has been reported and CPP_PRAGMA that c_finish_omp_declare_simd
14125 has already processed the tokens. */
14126 if (clauses.exists () && clauses[0].type == CPP_EOF)
14127 return;
14128 if (fndecl == NULL_TREE || TREE_CODE (fndecl) != FUNCTION_DECL)
14130 error ("%<#pragma omp declare simd%> not immediately followed by "
14131 "a function declaration or definition");
14132 clauses[0].type = CPP_EOF;
14133 return;
14135 if (clauses.exists () && clauses[0].type != CPP_NAME)
14137 error_at (DECL_SOURCE_LOCATION (fndecl),
14138 "%<#pragma omp declare simd%> not immediately followed by "
14139 "a single function declaration or definition");
14140 clauses[0].type = CPP_EOF;
14141 return;
14144 if (parms == NULL_TREE)
14145 parms = DECL_ARGUMENTS (fndecl);
14147 unsigned int tokens_avail = parser->tokens_avail;
14148 gcc_assert (parser->tokens == &parser->tokens_buf[0]);
14149 bool is_cilkplus_cilk_simd_fn = false;
14151 if (flag_cilkplus && !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
14153 parser->tokens = parser->cilk_simd_fn_tokens->address ();
14154 parser->tokens_avail = vec_safe_length (parser->cilk_simd_fn_tokens);
14155 is_cilkplus_cilk_simd_fn = true;
14157 else
14159 parser->tokens = clauses.address ();
14160 parser->tokens_avail = clauses.length ();
14163 /* c_parser_omp_declare_simd pushed 2 extra CPP_EOF tokens at the end. */
14164 while (parser->tokens_avail > 3)
14166 c_token *token = c_parser_peek_token (parser);
14167 if (!is_cilkplus_cilk_simd_fn)
14168 gcc_assert (token->type == CPP_NAME
14169 && strcmp (IDENTIFIER_POINTER (token->value), "simd") == 0);
14170 else
14171 gcc_assert (token->type == CPP_NAME
14172 && is_cilkplus_vector_p (token->value));
14173 c_parser_consume_token (parser);
14174 parser->in_pragma = true;
14176 tree c = NULL_TREE;
14177 if (is_cilkplus_cilk_simd_fn)
14178 c = c_parser_omp_all_clauses (parser, CILK_SIMD_FN_CLAUSE_MASK,
14179 "SIMD-enabled functions attribute");
14180 else
14181 c = c_parser_omp_all_clauses (parser, OMP_DECLARE_SIMD_CLAUSE_MASK,
14182 "#pragma omp declare simd");
14183 c = c_omp_declare_simd_clauses_to_numbers (parms, c);
14184 if (c != NULL_TREE)
14185 c = tree_cons (NULL_TREE, c, NULL_TREE);
14186 if (is_cilkplus_cilk_simd_fn)
14188 tree k = build_tree_list (get_identifier ("cilk simd function"),
14189 NULL_TREE);
14190 TREE_CHAIN (k) = DECL_ATTRIBUTES (fndecl);
14191 DECL_ATTRIBUTES (fndecl) = k;
14193 c = build_tree_list (get_identifier ("omp declare simd"), c);
14194 TREE_CHAIN (c) = DECL_ATTRIBUTES (fndecl);
14195 DECL_ATTRIBUTES (fndecl) = c;
14198 parser->tokens = &parser->tokens_buf[0];
14199 parser->tokens_avail = tokens_avail;
14200 if (clauses.exists ())
14201 clauses[0].type = CPP_PRAGMA;
14203 if (!vec_safe_is_empty (parser->cilk_simd_fn_tokens))
14204 vec_free (parser->cilk_simd_fn_tokens);
14208 /* OpenMP 4.0:
14209 # pragma omp declare target new-line
14210 declarations and definitions
14211 # pragma omp end declare target new-line */
14213 static void
14214 c_parser_omp_declare_target (c_parser *parser)
14216 c_parser_skip_to_pragma_eol (parser);
14217 current_omp_declare_target_attribute++;
14220 static void
14221 c_parser_omp_end_declare_target (c_parser *parser)
14223 location_t loc = c_parser_peek_token (parser)->location;
14224 c_parser_consume_pragma (parser);
14225 if (c_parser_next_token_is (parser, CPP_NAME)
14226 && strcmp (IDENTIFIER_POINTER (c_parser_peek_token (parser)->value),
14227 "declare") == 0)
14229 c_parser_consume_token (parser);
14230 if (c_parser_next_token_is (parser, CPP_NAME)
14231 && strcmp (IDENTIFIER_POINTER (c_parser_peek_token (parser)->value),
14232 "target") == 0)
14233 c_parser_consume_token (parser);
14234 else
14236 c_parser_error (parser, "expected %<target%>");
14237 c_parser_skip_to_pragma_eol (parser);
14238 return;
14241 else
14243 c_parser_error (parser, "expected %<declare%>");
14244 c_parser_skip_to_pragma_eol (parser);
14245 return;
14247 c_parser_skip_to_pragma_eol (parser);
14248 if (!current_omp_declare_target_attribute)
14249 error_at (loc, "%<#pragma omp end declare target%> without corresponding "
14250 "%<#pragma omp declare target%>");
14251 else
14252 current_omp_declare_target_attribute--;
14256 /* OpenMP 4.0
14257 #pragma omp declare reduction (reduction-id : typename-list : expression) \
14258 initializer-clause[opt] new-line
14260 initializer-clause:
14261 initializer (omp_priv = initializer)
14262 initializer (function-name (argument-list)) */
14264 static void
14265 c_parser_omp_declare_reduction (c_parser *parser, enum pragma_context context)
14267 unsigned int tokens_avail = 0, i;
14268 vec<tree> types = vNULL;
14269 vec<c_token> clauses = vNULL;
14270 enum tree_code reduc_code = ERROR_MARK;
14271 tree reduc_id = NULL_TREE;
14272 tree type;
14273 location_t rloc = c_parser_peek_token (parser)->location;
14275 if (context == pragma_struct || context == pragma_param)
14277 error ("%<#pragma omp declare reduction%> not at file or block scope");
14278 goto fail;
14281 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
14282 goto fail;
14284 switch (c_parser_peek_token (parser)->type)
14286 case CPP_PLUS:
14287 reduc_code = PLUS_EXPR;
14288 break;
14289 case CPP_MULT:
14290 reduc_code = MULT_EXPR;
14291 break;
14292 case CPP_MINUS:
14293 reduc_code = MINUS_EXPR;
14294 break;
14295 case CPP_AND:
14296 reduc_code = BIT_AND_EXPR;
14297 break;
14298 case CPP_XOR:
14299 reduc_code = BIT_XOR_EXPR;
14300 break;
14301 case CPP_OR:
14302 reduc_code = BIT_IOR_EXPR;
14303 break;
14304 case CPP_AND_AND:
14305 reduc_code = TRUTH_ANDIF_EXPR;
14306 break;
14307 case CPP_OR_OR:
14308 reduc_code = TRUTH_ORIF_EXPR;
14309 break;
14310 case CPP_NAME:
14311 const char *p;
14312 p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
14313 if (strcmp (p, "min") == 0)
14315 reduc_code = MIN_EXPR;
14316 break;
14318 if (strcmp (p, "max") == 0)
14320 reduc_code = MAX_EXPR;
14321 break;
14323 reduc_id = c_parser_peek_token (parser)->value;
14324 break;
14325 default:
14326 c_parser_error (parser,
14327 "expected %<+%>, %<*%>, %<-%>, %<&%>, "
14328 "%<^%>, %<|%>, %<&&%>, %<||%>, %<min%> or identifier");
14329 goto fail;
14332 tree orig_reduc_id, reduc_decl;
14333 orig_reduc_id = reduc_id;
14334 reduc_id = c_omp_reduction_id (reduc_code, reduc_id);
14335 reduc_decl = c_omp_reduction_decl (reduc_id);
14336 c_parser_consume_token (parser);
14338 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
14339 goto fail;
14341 while (true)
14343 location_t loc = c_parser_peek_token (parser)->location;
14344 struct c_type_name *ctype = c_parser_type_name (parser);
14345 if (ctype != NULL)
14347 type = groktypename (ctype, NULL, NULL);
14348 if (type == error_mark_node)
14350 else if ((INTEGRAL_TYPE_P (type)
14351 || TREE_CODE (type) == REAL_TYPE
14352 || TREE_CODE (type) == COMPLEX_TYPE)
14353 && orig_reduc_id == NULL_TREE)
14354 error_at (loc, "predeclared arithmetic type in "
14355 "%<#pragma omp declare reduction%>");
14356 else if (TREE_CODE (type) == FUNCTION_TYPE
14357 || TREE_CODE (type) == ARRAY_TYPE)
14358 error_at (loc, "function or array type in "
14359 "%<#pragma omp declare reduction%>");
14360 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
14361 error_at (loc, "const, volatile or restrict qualified type in "
14362 "%<#pragma omp declare reduction%>");
14363 else
14365 tree t;
14366 for (t = DECL_INITIAL (reduc_decl); t; t = TREE_CHAIN (t))
14367 if (comptypes (TREE_PURPOSE (t), type))
14369 error_at (loc, "redeclaration of %qs "
14370 "%<#pragma omp declare reduction%> for "
14371 "type %qT",
14372 IDENTIFIER_POINTER (reduc_id)
14373 + sizeof ("omp declare reduction ") - 1,
14374 type);
14375 location_t ploc
14376 = DECL_SOURCE_LOCATION (TREE_VEC_ELT (TREE_VALUE (t),
14377 0));
14378 error_at (ploc, "previous %<#pragma omp declare "
14379 "reduction%>");
14380 break;
14382 if (t == NULL_TREE)
14383 types.safe_push (type);
14385 if (c_parser_next_token_is (parser, CPP_COMMA))
14386 c_parser_consume_token (parser);
14387 else
14388 break;
14390 else
14391 break;
14394 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>")
14395 || types.is_empty ())
14397 fail:
14398 clauses.release ();
14399 types.release ();
14400 while (true)
14402 c_token *token = c_parser_peek_token (parser);
14403 if (token->type == CPP_EOF || token->type == CPP_PRAGMA_EOL)
14404 break;
14405 c_parser_consume_token (parser);
14407 c_parser_skip_to_pragma_eol (parser);
14408 return;
14411 if (types.length () > 1)
14413 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
14415 c_token *token = c_parser_peek_token (parser);
14416 if (token->type == CPP_EOF)
14417 goto fail;
14418 clauses.safe_push (*token);
14419 c_parser_consume_token (parser);
14421 clauses.safe_push (*c_parser_peek_token (parser));
14422 c_parser_skip_to_pragma_eol (parser);
14424 /* Make sure nothing tries to read past the end of the tokens. */
14425 c_token eof_token;
14426 memset (&eof_token, 0, sizeof (eof_token));
14427 eof_token.type = CPP_EOF;
14428 clauses.safe_push (eof_token);
14429 clauses.safe_push (eof_token);
14432 int errs = errorcount;
14433 FOR_EACH_VEC_ELT (types, i, type)
14435 tokens_avail = parser->tokens_avail;
14436 gcc_assert (parser->tokens == &parser->tokens_buf[0]);
14437 if (!clauses.is_empty ())
14439 parser->tokens = clauses.address ();
14440 parser->tokens_avail = clauses.length ();
14441 parser->in_pragma = true;
14444 bool nested = current_function_decl != NULL_TREE;
14445 if (nested)
14446 c_push_function_context ();
14447 tree fndecl = build_decl (BUILTINS_LOCATION, FUNCTION_DECL,
14448 reduc_id, default_function_type);
14449 current_function_decl = fndecl;
14450 allocate_struct_function (fndecl, true);
14451 push_scope ();
14452 tree stmt = push_stmt_list ();
14453 /* Intentionally BUILTINS_LOCATION, so that -Wshadow doesn't
14454 warn about these. */
14455 tree omp_out = build_decl (BUILTINS_LOCATION, VAR_DECL,
14456 get_identifier ("omp_out"), type);
14457 DECL_ARTIFICIAL (omp_out) = 1;
14458 DECL_CONTEXT (omp_out) = fndecl;
14459 pushdecl (omp_out);
14460 tree omp_in = build_decl (BUILTINS_LOCATION, VAR_DECL,
14461 get_identifier ("omp_in"), type);
14462 DECL_ARTIFICIAL (omp_in) = 1;
14463 DECL_CONTEXT (omp_in) = fndecl;
14464 pushdecl (omp_in);
14465 struct c_expr combiner = c_parser_expression (parser);
14466 struct c_expr initializer;
14467 tree omp_priv = NULL_TREE, omp_orig = NULL_TREE;
14468 bool bad = false;
14469 initializer.value = error_mark_node;
14470 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
14471 bad = true;
14472 else if (c_parser_next_token_is (parser, CPP_NAME)
14473 && strcmp (IDENTIFIER_POINTER
14474 (c_parser_peek_token (parser)->value),
14475 "initializer") == 0)
14477 c_parser_consume_token (parser);
14478 pop_scope ();
14479 push_scope ();
14480 omp_priv = build_decl (BUILTINS_LOCATION, VAR_DECL,
14481 get_identifier ("omp_priv"), type);
14482 DECL_ARTIFICIAL (omp_priv) = 1;
14483 DECL_INITIAL (omp_priv) = error_mark_node;
14484 DECL_CONTEXT (omp_priv) = fndecl;
14485 pushdecl (omp_priv);
14486 omp_orig = build_decl (BUILTINS_LOCATION, VAR_DECL,
14487 get_identifier ("omp_orig"), type);
14488 DECL_ARTIFICIAL (omp_orig) = 1;
14489 DECL_CONTEXT (omp_orig) = fndecl;
14490 pushdecl (omp_orig);
14491 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
14492 bad = true;
14493 else if (!c_parser_next_token_is (parser, CPP_NAME))
14495 c_parser_error (parser, "expected %<omp_priv%> or "
14496 "function-name");
14497 bad = true;
14499 else if (strcmp (IDENTIFIER_POINTER
14500 (c_parser_peek_token (parser)->value),
14501 "omp_priv") != 0)
14503 if (c_parser_peek_2nd_token (parser)->type != CPP_OPEN_PAREN
14504 || c_parser_peek_token (parser)->id_kind != C_ID_ID)
14506 c_parser_error (parser, "expected function-name %<(%>");
14507 bad = true;
14509 else
14510 initializer = c_parser_postfix_expression (parser);
14511 if (initializer.value
14512 && TREE_CODE (initializer.value) == CALL_EXPR)
14514 int j;
14515 tree c = initializer.value;
14516 for (j = 0; j < call_expr_nargs (c); j++)
14517 if (TREE_CODE (CALL_EXPR_ARG (c, j)) == ADDR_EXPR
14518 && TREE_OPERAND (CALL_EXPR_ARG (c, j), 0) == omp_priv)
14519 break;
14520 if (j == call_expr_nargs (c))
14521 error ("one of the initializer call arguments should be "
14522 "%<&omp_priv%>");
14525 else
14527 c_parser_consume_token (parser);
14528 if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
14529 bad = true;
14530 else
14532 tree st = push_stmt_list ();
14533 start_init (omp_priv, NULL_TREE, 0);
14534 location_t loc = c_parser_peek_token (parser)->location;
14535 struct c_expr init = c_parser_initializer (parser);
14536 finish_init ();
14537 finish_decl (omp_priv, loc, init.value,
14538 init.original_type, NULL_TREE);
14539 pop_stmt_list (st);
14542 if (!bad
14543 && !c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
14544 bad = true;
14547 if (!bad)
14549 c_parser_skip_to_pragma_eol (parser);
14551 tree t = tree_cons (type, make_tree_vec (omp_priv ? 6 : 3),
14552 DECL_INITIAL (reduc_decl));
14553 DECL_INITIAL (reduc_decl) = t;
14554 DECL_SOURCE_LOCATION (omp_out) = rloc;
14555 TREE_VEC_ELT (TREE_VALUE (t), 0) = omp_out;
14556 TREE_VEC_ELT (TREE_VALUE (t), 1) = omp_in;
14557 TREE_VEC_ELT (TREE_VALUE (t), 2) = combiner.value;
14558 walk_tree (&combiner.value, c_check_omp_declare_reduction_r,
14559 &TREE_VEC_ELT (TREE_VALUE (t), 0), NULL);
14560 if (omp_priv)
14562 DECL_SOURCE_LOCATION (omp_priv) = rloc;
14563 TREE_VEC_ELT (TREE_VALUE (t), 3) = omp_priv;
14564 TREE_VEC_ELT (TREE_VALUE (t), 4) = omp_orig;
14565 TREE_VEC_ELT (TREE_VALUE (t), 5) = initializer.value;
14566 walk_tree (&initializer.value, c_check_omp_declare_reduction_r,
14567 &TREE_VEC_ELT (TREE_VALUE (t), 3), NULL);
14568 walk_tree (&DECL_INITIAL (omp_priv),
14569 c_check_omp_declare_reduction_r,
14570 &TREE_VEC_ELT (TREE_VALUE (t), 3), NULL);
14574 pop_stmt_list (stmt);
14575 pop_scope ();
14576 if (cfun->language != NULL)
14578 ggc_free (cfun->language);
14579 cfun->language = NULL;
14581 set_cfun (NULL);
14582 current_function_decl = NULL_TREE;
14583 if (nested)
14584 c_pop_function_context ();
14586 if (!clauses.is_empty ())
14588 parser->tokens = &parser->tokens_buf[0];
14589 parser->tokens_avail = tokens_avail;
14591 if (bad)
14592 goto fail;
14593 if (errs != errorcount)
14594 break;
14597 clauses.release ();
14598 types.release ();
14602 /* OpenMP 4.0
14603 #pragma omp declare simd declare-simd-clauses[optseq] new-line
14604 #pragma omp declare reduction (reduction-id : typename-list : expression) \
14605 initializer-clause[opt] new-line
14606 #pragma omp declare target new-line */
14608 static void
14609 c_parser_omp_declare (c_parser *parser, enum pragma_context context)
14611 c_parser_consume_pragma (parser);
14612 if (c_parser_next_token_is (parser, CPP_NAME))
14614 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
14615 if (strcmp (p, "simd") == 0)
14617 /* c_parser_consume_token (parser); done in
14618 c_parser_omp_declare_simd. */
14619 c_parser_omp_declare_simd (parser, context);
14620 return;
14622 if (strcmp (p, "reduction") == 0)
14624 c_parser_consume_token (parser);
14625 c_parser_omp_declare_reduction (parser, context);
14626 return;
14628 if (!flag_openmp) /* flag_openmp_simd */
14630 c_parser_skip_to_pragma_eol (parser, false);
14631 return;
14633 if (strcmp (p, "target") == 0)
14635 c_parser_consume_token (parser);
14636 c_parser_omp_declare_target (parser);
14637 return;
14641 c_parser_error (parser, "expected %<simd%> or %<reduction%> "
14642 "or %<target%>");
14643 c_parser_skip_to_pragma_eol (parser);
14646 /* Main entry point to parsing most OpenMP pragmas. */
14648 static void
14649 c_parser_omp_construct (c_parser *parser)
14651 enum pragma_kind p_kind;
14652 location_t loc;
14653 tree stmt;
14654 char p_name[sizeof "#pragma omp teams distribute parallel for simd"];
14655 omp_clause_mask mask (0);
14657 loc = c_parser_peek_token (parser)->location;
14658 p_kind = c_parser_peek_token (parser)->pragma_kind;
14659 c_parser_consume_pragma (parser);
14661 switch (p_kind)
14663 case PRAGMA_OACC_CACHE:
14664 strcpy (p_name, "#pragma acc");
14665 stmt = c_parser_oacc_cache (loc, parser);
14666 break;
14667 case PRAGMA_OACC_DATA:
14668 stmt = c_parser_oacc_data (loc, parser);
14669 break;
14670 case PRAGMA_OACC_KERNELS:
14671 strcpy (p_name, "#pragma acc");
14672 stmt = c_parser_oacc_kernels (loc, parser, p_name);
14673 break;
14674 case PRAGMA_OACC_LOOP:
14675 strcpy (p_name, "#pragma acc");
14676 stmt = c_parser_oacc_loop (loc, parser, p_name);
14677 break;
14678 case PRAGMA_OACC_PARALLEL:
14679 strcpy (p_name, "#pragma acc");
14680 stmt = c_parser_oacc_parallel (loc, parser, p_name);
14681 break;
14682 case PRAGMA_OACC_WAIT:
14683 strcpy (p_name, "#pragma wait");
14684 stmt = c_parser_oacc_wait (loc, parser, p_name);
14685 break;
14686 case PRAGMA_OMP_ATOMIC:
14687 c_parser_omp_atomic (loc, parser);
14688 return;
14689 case PRAGMA_OMP_CRITICAL:
14690 stmt = c_parser_omp_critical (loc, parser);
14691 break;
14692 case PRAGMA_OMP_DISTRIBUTE:
14693 strcpy (p_name, "#pragma omp");
14694 stmt = c_parser_omp_distribute (loc, parser, p_name, mask, NULL);
14695 break;
14696 case PRAGMA_OMP_FOR:
14697 strcpy (p_name, "#pragma omp");
14698 stmt = c_parser_omp_for (loc, parser, p_name, mask, NULL);
14699 break;
14700 case PRAGMA_OMP_MASTER:
14701 stmt = c_parser_omp_master (loc, parser);
14702 break;
14703 case PRAGMA_OMP_ORDERED:
14704 stmt = c_parser_omp_ordered (loc, parser);
14705 break;
14706 case PRAGMA_OMP_PARALLEL:
14707 strcpy (p_name, "#pragma omp");
14708 stmt = c_parser_omp_parallel (loc, parser, p_name, mask, NULL);
14709 break;
14710 case PRAGMA_OMP_SECTIONS:
14711 strcpy (p_name, "#pragma omp");
14712 stmt = c_parser_omp_sections (loc, parser, p_name, mask, NULL);
14713 break;
14714 case PRAGMA_OMP_SIMD:
14715 strcpy (p_name, "#pragma omp");
14716 stmt = c_parser_omp_simd (loc, parser, p_name, mask, NULL);
14717 break;
14718 case PRAGMA_OMP_SINGLE:
14719 stmt = c_parser_omp_single (loc, parser);
14720 break;
14721 case PRAGMA_OMP_TASK:
14722 stmt = c_parser_omp_task (loc, parser);
14723 break;
14724 case PRAGMA_OMP_TASKGROUP:
14725 stmt = c_parser_omp_taskgroup (parser);
14726 break;
14727 case PRAGMA_OMP_TEAMS:
14728 strcpy (p_name, "#pragma omp");
14729 stmt = c_parser_omp_teams (loc, parser, p_name, mask, NULL);
14730 break;
14731 default:
14732 gcc_unreachable ();
14735 if (stmt)
14736 gcc_assert (EXPR_LOCATION (stmt) != UNKNOWN_LOCATION);
14740 /* OpenMP 2.5:
14741 # pragma omp threadprivate (variable-list) */
14743 static void
14744 c_parser_omp_threadprivate (c_parser *parser)
14746 tree vars, t;
14747 location_t loc;
14749 c_parser_consume_pragma (parser);
14750 loc = c_parser_peek_token (parser)->location;
14751 vars = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_ERROR, NULL);
14753 /* Mark every variable in VARS to be assigned thread local storage. */
14754 for (t = vars; t; t = TREE_CHAIN (t))
14756 tree v = TREE_PURPOSE (t);
14758 /* FIXME diagnostics: Ideally we should keep individual
14759 locations for all the variables in the var list to make the
14760 following errors more precise. Perhaps
14761 c_parser_omp_var_list_parens() should construct a list of
14762 locations to go along with the var list. */
14764 /* If V had already been marked threadprivate, it doesn't matter
14765 whether it had been used prior to this point. */
14766 if (!VAR_P (v))
14767 error_at (loc, "%qD is not a variable", v);
14768 else if (TREE_USED (v) && !C_DECL_THREADPRIVATE_P (v))
14769 error_at (loc, "%qE declared %<threadprivate%> after first use", v);
14770 else if (! is_global_var (v))
14771 error_at (loc, "automatic variable %qE cannot be %<threadprivate%>", v);
14772 else if (TREE_TYPE (v) == error_mark_node)
14774 else if (! COMPLETE_TYPE_P (TREE_TYPE (v)))
14775 error_at (loc, "%<threadprivate%> %qE has incomplete type", v);
14776 else
14778 if (! DECL_THREAD_LOCAL_P (v))
14780 set_decl_tls_model (v, decl_default_tls_model (v));
14781 /* If rtl has been already set for this var, call
14782 make_decl_rtl once again, so that encode_section_info
14783 has a chance to look at the new decl flags. */
14784 if (DECL_RTL_SET_P (v))
14785 make_decl_rtl (v);
14787 C_DECL_THREADPRIVATE_P (v) = 1;
14791 c_parser_skip_to_pragma_eol (parser);
14794 /* Cilk Plus <#pragma simd> parsing routines. */
14796 /* Helper function for c_parser_pragma. Perform some sanity checking
14797 for <#pragma simd> constructs. Returns FALSE if there was a
14798 problem. */
14800 static bool
14801 c_parser_cilk_verify_simd (c_parser *parser,
14802 enum pragma_context context)
14804 if (!flag_cilkplus)
14806 warning (0, "pragma simd ignored because -fcilkplus is not enabled");
14807 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
14808 return false;
14810 if (context == pragma_external)
14812 c_parser_error (parser,"pragma simd must be inside a function");
14813 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
14814 return false;
14816 return true;
14819 /* Cilk Plus:
14820 This function is shared by SIMD-enabled functions and #pragma simd.
14821 If IS_SIMD_FN is true then it is parsing a SIMD-enabled function and
14822 CLAUSES is unused. The main purpose of this function is to parse a
14823 vectorlength attribute or clause and check for parse errors.
14824 When IS_SIMD_FN is true then the function is merely caching the tokens
14825 in PARSER->CILK_SIMD_FN_TOKENS. If errors are found then the token
14826 cache is cleared since there is no reason to continue.
14827 Syntax:
14828 vectorlength ( constant-expression ) */
14830 static tree
14831 c_parser_cilk_clause_vectorlength (c_parser *parser, tree clauses,
14832 bool is_simd_fn)
14834 if (is_simd_fn)
14835 check_no_duplicate_clause (clauses, OMP_CLAUSE_SIMDLEN, "vectorlength");
14836 else
14837 /* The vectorlength clause behaves exactly like OpenMP's safelen
14838 clause. Represent it in OpenMP terms. */
14839 check_no_duplicate_clause (clauses, OMP_CLAUSE_SAFELEN, "vectorlength");
14841 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
14842 return clauses;
14844 location_t loc = c_parser_peek_token (parser)->location;
14845 tree expr = c_parser_expr_no_commas (parser, NULL).value;
14846 expr = c_fully_fold (expr, false, NULL);
14848 /* If expr is an error_mark_node then the above function would have
14849 emitted an error. No reason to do it twice. */
14850 if (expr == error_mark_node)
14852 else if (!TREE_TYPE (expr)
14853 || !TREE_CONSTANT (expr)
14854 || !INTEGRAL_TYPE_P (TREE_TYPE (expr)))
14856 error_at (loc, "vectorlength must be an integer constant");
14857 else if (wi::exact_log2 (expr) == -1)
14858 error_at (loc, "vectorlength must be a power of 2");
14859 else
14861 if (is_simd_fn)
14863 tree u = build_omp_clause (loc, OMP_CLAUSE_SIMDLEN);
14864 OMP_CLAUSE_SIMDLEN_EXPR (u) = expr;
14865 OMP_CLAUSE_CHAIN (u) = clauses;
14866 clauses = u;
14868 else
14870 tree u = build_omp_clause (loc, OMP_CLAUSE_SAFELEN);
14871 OMP_CLAUSE_SAFELEN_EXPR (u) = expr;
14872 OMP_CLAUSE_CHAIN (u) = clauses;
14873 clauses = u;
14877 c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
14879 return clauses;
14882 /* Cilk Plus:
14883 linear ( simd-linear-variable-list )
14885 simd-linear-variable-list:
14886 simd-linear-variable
14887 simd-linear-variable-list , simd-linear-variable
14889 simd-linear-variable:
14890 id-expression
14891 id-expression : simd-linear-step
14893 simd-linear-step:
14894 conditional-expression */
14896 static tree
14897 c_parser_cilk_clause_linear (c_parser *parser, tree clauses)
14899 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
14900 return clauses;
14902 location_t loc = c_parser_peek_token (parser)->location;
14904 if (c_parser_next_token_is_not (parser, CPP_NAME)
14905 || c_parser_peek_token (parser)->id_kind != C_ID_ID)
14906 c_parser_error (parser, "expected identifier");
14908 while (c_parser_next_token_is (parser, CPP_NAME)
14909 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
14911 tree var = lookup_name (c_parser_peek_token (parser)->value);
14913 if (var == NULL)
14915 undeclared_variable (c_parser_peek_token (parser)->location,
14916 c_parser_peek_token (parser)->value);
14917 c_parser_consume_token (parser);
14919 else if (var == error_mark_node)
14920 c_parser_consume_token (parser);
14921 else
14923 tree step = integer_one_node;
14925 /* Parse the linear step if present. */
14926 if (c_parser_peek_2nd_token (parser)->type == CPP_COLON)
14928 c_parser_consume_token (parser);
14929 c_parser_consume_token (parser);
14931 tree expr = c_parser_expr_no_commas (parser, NULL).value;
14932 expr = c_fully_fold (expr, false, NULL);
14934 if (TREE_TYPE (expr)
14935 && INTEGRAL_TYPE_P (TREE_TYPE (expr))
14936 && (TREE_CONSTANT (expr)
14937 || DECL_P (expr)))
14938 step = expr;
14939 else
14940 c_parser_error (parser,
14941 "step size must be an integer constant "
14942 "expression or an integer variable");
14944 else
14945 c_parser_consume_token (parser);
14947 /* Use OMP_CLAUSE_LINEAR, which has the same semantics. */
14948 tree u = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
14949 OMP_CLAUSE_DECL (u) = var;
14950 OMP_CLAUSE_LINEAR_STEP (u) = step;
14951 OMP_CLAUSE_CHAIN (u) = clauses;
14952 clauses = u;
14955 if (c_parser_next_token_is_not (parser, CPP_COMMA))
14956 break;
14958 c_parser_consume_token (parser);
14961 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
14963 return clauses;
14966 /* Returns the name of the next clause. If the clause is not
14967 recognized SIMD_OMP_CLAUSE_NONE is returned and the next token is
14968 not consumed. Otherwise, the appropriate pragma_simd_clause is
14969 returned and the token is consumed. */
14971 static pragma_omp_clause
14972 c_parser_cilk_clause_name (c_parser *parser)
14974 pragma_omp_clause result;
14975 c_token *token = c_parser_peek_token (parser);
14977 if (!token->value || token->type != CPP_NAME)
14978 return PRAGMA_CILK_CLAUSE_NONE;
14980 const char *p = IDENTIFIER_POINTER (token->value);
14982 if (!strcmp (p, "vectorlength"))
14983 result = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
14984 else if (!strcmp (p, "linear"))
14985 result = PRAGMA_CILK_CLAUSE_LINEAR;
14986 else if (!strcmp (p, "private"))
14987 result = PRAGMA_CILK_CLAUSE_PRIVATE;
14988 else if (!strcmp (p, "firstprivate"))
14989 result = PRAGMA_CILK_CLAUSE_FIRSTPRIVATE;
14990 else if (!strcmp (p, "lastprivate"))
14991 result = PRAGMA_CILK_CLAUSE_LASTPRIVATE;
14992 else if (!strcmp (p, "reduction"))
14993 result = PRAGMA_CILK_CLAUSE_REDUCTION;
14994 else
14995 return PRAGMA_CILK_CLAUSE_NONE;
14997 c_parser_consume_token (parser);
14998 return result;
15001 /* Parse all #<pragma simd> clauses. Return the list of clauses
15002 found. */
15004 static tree
15005 c_parser_cilk_all_clauses (c_parser *parser)
15007 tree clauses = NULL;
15009 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
15011 pragma_omp_clause c_kind;
15013 c_kind = c_parser_cilk_clause_name (parser);
15015 switch (c_kind)
15017 case PRAGMA_CILK_CLAUSE_VECTORLENGTH:
15018 clauses = c_parser_cilk_clause_vectorlength (parser, clauses, false);
15019 break;
15020 case PRAGMA_CILK_CLAUSE_LINEAR:
15021 clauses = c_parser_cilk_clause_linear (parser, clauses);
15022 break;
15023 case PRAGMA_CILK_CLAUSE_PRIVATE:
15024 /* Use the OpenMP counterpart. */
15025 clauses = c_parser_omp_clause_private (parser, clauses);
15026 break;
15027 case PRAGMA_CILK_CLAUSE_FIRSTPRIVATE:
15028 /* Use the OpenMP counterpart. */
15029 clauses = c_parser_omp_clause_firstprivate (parser, clauses);
15030 break;
15031 case PRAGMA_CILK_CLAUSE_LASTPRIVATE:
15032 /* Use the OpenMP counterpart. */
15033 clauses = c_parser_omp_clause_lastprivate (parser, clauses);
15034 break;
15035 case PRAGMA_CILK_CLAUSE_REDUCTION:
15036 /* Use the OpenMP counterpart. */
15037 clauses = c_parser_omp_clause_reduction (parser, clauses);
15038 break;
15039 default:
15040 c_parser_error (parser, "expected %<#pragma simd%> clause");
15041 goto saw_error;
15045 saw_error:
15046 c_parser_skip_to_pragma_eol (parser);
15047 return c_finish_cilk_clauses (clauses);
15050 /* This function helps parse the grainsize pragma for a _Cilk_for statement.
15051 Here is the correct syntax of this pragma:
15052 #pragma cilk grainsize = <EXP>
15055 static void
15056 c_parser_cilk_grainsize (c_parser *parser)
15058 extern tree convert_to_integer (tree, tree);
15060 /* consume the 'grainsize' keyword. */
15061 c_parser_consume_pragma (parser);
15063 if (c_parser_require (parser, CPP_EQ, "expected %<=%>") != 0)
15065 struct c_expr g_expr = c_parser_binary_expression (parser, NULL, NULL);
15066 if (g_expr.value == error_mark_node)
15068 c_parser_skip_to_pragma_eol (parser);
15069 return;
15071 tree grain = convert_to_integer (long_integer_type_node,
15072 c_fully_fold (g_expr.value, false,
15073 NULL));
15074 c_parser_skip_to_pragma_eol (parser);
15075 c_token *token = c_parser_peek_token (parser);
15076 if (token && token->type == CPP_KEYWORD
15077 && token->keyword == RID_CILK_FOR)
15079 if (grain == NULL_TREE || grain == error_mark_node)
15080 grain = integer_zero_node;
15081 c_parser_cilk_for (parser, grain);
15083 else
15084 warning (0, "%<#pragma cilk grainsize%> is not followed by "
15085 "%<_Cilk_for%>");
15087 else
15088 c_parser_skip_to_pragma_eol (parser);
15091 /* Main entry point for parsing Cilk Plus <#pragma simd> for loops. */
15093 static void
15094 c_parser_cilk_simd (c_parser *parser)
15096 tree clauses = c_parser_cilk_all_clauses (parser);
15097 tree block = c_begin_compound_stmt (true);
15098 location_t loc = c_parser_peek_token (parser)->location;
15099 c_parser_omp_for_loop (loc, parser, CILK_SIMD, clauses, NULL);
15100 block = c_end_compound_stmt (loc, block, true);
15101 add_stmt (block);
15104 /* Create an artificial decl with TYPE and emit initialization of it with
15105 INIT. */
15107 static tree
15108 c_get_temp_regvar (tree type, tree init)
15110 location_t loc = EXPR_LOCATION (init);
15111 tree decl = build_decl (loc, VAR_DECL, NULL_TREE, type);
15112 DECL_ARTIFICIAL (decl) = 1;
15113 DECL_IGNORED_P (decl) = 1;
15114 pushdecl (decl);
15115 tree t = build2 (INIT_EXPR, type, decl, init);
15116 add_stmt (t);
15117 return decl;
15120 /* Main entry point for parsing Cilk Plus _Cilk_for loops.
15121 GRAIN is the grain value passed in through pragma or 0. */
15123 static void
15124 c_parser_cilk_for (c_parser *parser, tree grain)
15126 tree clauses = build_omp_clause (EXPR_LOCATION (grain), OMP_CLAUSE_SCHEDULE);
15127 OMP_CLAUSE_SCHEDULE_KIND (clauses) = OMP_CLAUSE_SCHEDULE_CILKFOR;
15128 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (clauses) = grain;
15129 clauses = c_finish_omp_clauses (clauses);
15131 tree block = c_begin_compound_stmt (true);
15132 tree sb = push_stmt_list ();
15133 location_t loc = c_parser_peek_token (parser)->location;
15134 tree omp_for = c_parser_omp_for_loop (loc, parser, CILK_FOR, clauses, NULL);
15135 sb = pop_stmt_list (sb);
15137 if (omp_for)
15139 tree omp_par = make_node (OMP_PARALLEL);
15140 TREE_TYPE (omp_par) = void_type_node;
15141 OMP_PARALLEL_CLAUSES (omp_par) = NULL_TREE;
15142 tree bind = build3 (BIND_EXPR, void_type_node, NULL, NULL, NULL);
15143 TREE_SIDE_EFFECTS (bind) = 1;
15144 BIND_EXPR_BODY (bind) = sb;
15145 OMP_PARALLEL_BODY (omp_par) = bind;
15146 if (OMP_FOR_PRE_BODY (omp_for))
15148 add_stmt (OMP_FOR_PRE_BODY (omp_for));
15149 OMP_FOR_PRE_BODY (omp_for) = NULL_TREE;
15151 tree init = TREE_VEC_ELT (OMP_FOR_INIT (omp_for), 0);
15152 tree decl = TREE_OPERAND (init, 0);
15153 tree cond = TREE_VEC_ELT (OMP_FOR_COND (omp_for), 0);
15154 tree incr = TREE_VEC_ELT (OMP_FOR_INCR (omp_for), 0);
15155 tree t = TREE_OPERAND (cond, 1), c, clauses = NULL_TREE;
15156 if (TREE_CODE (t) != INTEGER_CST)
15158 TREE_OPERAND (cond, 1) = c_get_temp_regvar (TREE_TYPE (t), t);
15159 c = build_omp_clause (input_location, OMP_CLAUSE_FIRSTPRIVATE);
15160 OMP_CLAUSE_DECL (c) = TREE_OPERAND (cond, 1);
15161 OMP_CLAUSE_CHAIN (c) = clauses;
15162 clauses = c;
15164 if (TREE_CODE (incr) == MODIFY_EXPR)
15166 t = TREE_OPERAND (TREE_OPERAND (incr, 1), 1);
15167 if (TREE_CODE (t) != INTEGER_CST)
15169 TREE_OPERAND (TREE_OPERAND (incr, 1), 1)
15170 = c_get_temp_regvar (TREE_TYPE (t), t);
15171 c = build_omp_clause (input_location, OMP_CLAUSE_FIRSTPRIVATE);
15172 OMP_CLAUSE_DECL (c) = TREE_OPERAND (TREE_OPERAND (incr, 1), 1);
15173 OMP_CLAUSE_CHAIN (c) = clauses;
15174 clauses = c;
15177 t = TREE_OPERAND (init, 1);
15178 if (TREE_CODE (t) != INTEGER_CST)
15180 TREE_OPERAND (init, 1) = c_get_temp_regvar (TREE_TYPE (t), t);
15181 c = build_omp_clause (input_location, OMP_CLAUSE_FIRSTPRIVATE);
15182 OMP_CLAUSE_DECL (c) = TREE_OPERAND (init, 1);
15183 OMP_CLAUSE_CHAIN (c) = clauses;
15184 clauses = c;
15186 c = build_omp_clause (input_location, OMP_CLAUSE_PRIVATE);
15187 OMP_CLAUSE_DECL (c) = decl;
15188 OMP_CLAUSE_CHAIN (c) = clauses;
15189 clauses = c;
15190 c = build_omp_clause (input_location, OMP_CLAUSE__CILK_FOR_COUNT_);
15191 OMP_CLAUSE_OPERAND (c, 0)
15192 = cilk_for_number_of_iterations (omp_for);
15193 OMP_CLAUSE_CHAIN (c) = clauses;
15194 OMP_PARALLEL_CLAUSES (omp_par) = c_finish_omp_clauses (c);
15195 add_stmt (omp_par);
15198 block = c_end_compound_stmt (loc, block, true);
15199 add_stmt (block);
15203 /* Parse a transaction attribute (GCC Extension).
15205 transaction-attribute:
15206 attributes
15207 [ [ any-word ] ]
15209 The transactional memory language description is written for C++,
15210 and uses the C++0x attribute syntax. For compatibility, allow the
15211 bracket style for transactions in C as well. */
15213 static tree
15214 c_parser_transaction_attributes (c_parser *parser)
15216 tree attr_name, attr = NULL;
15218 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
15219 return c_parser_attributes (parser);
15221 if (!c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
15222 return NULL_TREE;
15223 c_parser_consume_token (parser);
15224 if (!c_parser_require (parser, CPP_OPEN_SQUARE, "expected %<[%>"))
15225 goto error1;
15227 attr_name = c_parser_attribute_any_word (parser);
15228 if (attr_name)
15230 c_parser_consume_token (parser);
15231 attr = build_tree_list (attr_name, NULL_TREE);
15233 else
15234 c_parser_error (parser, "expected identifier");
15236 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, "expected %<]%>");
15237 error1:
15238 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, "expected %<]%>");
15239 return attr;
15242 /* Parse a __transaction_atomic or __transaction_relaxed statement
15243 (GCC Extension).
15245 transaction-statement:
15246 __transaction_atomic transaction-attribute[opt] compound-statement
15247 __transaction_relaxed compound-statement
15249 Note that the only valid attribute is: "outer".
15252 static tree
15253 c_parser_transaction (c_parser *parser, enum rid keyword)
15255 unsigned int old_in = parser->in_transaction;
15256 unsigned int this_in = 1, new_in;
15257 location_t loc = c_parser_peek_token (parser)->location;
15258 tree stmt, attrs;
15260 gcc_assert ((keyword == RID_TRANSACTION_ATOMIC
15261 || keyword == RID_TRANSACTION_RELAXED)
15262 && c_parser_next_token_is_keyword (parser, keyword));
15263 c_parser_consume_token (parser);
15265 if (keyword == RID_TRANSACTION_RELAXED)
15266 this_in |= TM_STMT_ATTR_RELAXED;
15267 else
15269 attrs = c_parser_transaction_attributes (parser);
15270 if (attrs)
15271 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
15274 /* Keep track if we're in the lexical scope of an outer transaction. */
15275 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
15277 parser->in_transaction = new_in;
15278 stmt = c_parser_compound_statement (parser);
15279 parser->in_transaction = old_in;
15281 if (flag_tm)
15282 stmt = c_finish_transaction (loc, stmt, this_in);
15283 else
15284 error_at (loc, (keyword == RID_TRANSACTION_ATOMIC ?
15285 "%<__transaction_atomic%> without transactional memory support enabled"
15286 : "%<__transaction_relaxed %> "
15287 "without transactional memory support enabled"));
15289 return stmt;
15292 /* Parse a __transaction_atomic or __transaction_relaxed expression
15293 (GCC Extension).
15295 transaction-expression:
15296 __transaction_atomic ( expression )
15297 __transaction_relaxed ( expression )
15300 static struct c_expr
15301 c_parser_transaction_expression (c_parser *parser, enum rid keyword)
15303 struct c_expr ret;
15304 unsigned int old_in = parser->in_transaction;
15305 unsigned int this_in = 1;
15306 location_t loc = c_parser_peek_token (parser)->location;
15307 tree attrs;
15309 gcc_assert ((keyword == RID_TRANSACTION_ATOMIC
15310 || keyword == RID_TRANSACTION_RELAXED)
15311 && c_parser_next_token_is_keyword (parser, keyword));
15312 c_parser_consume_token (parser);
15314 if (keyword == RID_TRANSACTION_RELAXED)
15315 this_in |= TM_STMT_ATTR_RELAXED;
15316 else
15318 attrs = c_parser_transaction_attributes (parser);
15319 if (attrs)
15320 this_in |= parse_tm_stmt_attr (attrs, 0);
15323 parser->in_transaction = this_in;
15324 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
15326 tree expr = c_parser_expression (parser).value;
15327 ret.original_type = TREE_TYPE (expr);
15328 ret.value = build1 (TRANSACTION_EXPR, ret.original_type, expr);
15329 if (this_in & TM_STMT_ATTR_RELAXED)
15330 TRANSACTION_EXPR_RELAXED (ret.value) = 1;
15331 SET_EXPR_LOCATION (ret.value, loc);
15332 ret.original_code = TRANSACTION_EXPR;
15333 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
15335 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
15336 goto error;
15339 else
15341 error:
15342 ret.value = error_mark_node;
15343 ret.original_code = ERROR_MARK;
15344 ret.original_type = NULL;
15346 parser->in_transaction = old_in;
15348 if (!flag_tm)
15349 error_at (loc, (keyword == RID_TRANSACTION_ATOMIC ?
15350 "%<__transaction_atomic%> without transactional memory support enabled"
15351 : "%<__transaction_relaxed %> "
15352 "without transactional memory support enabled"));
15354 return ret;
15357 /* Parse a __transaction_cancel statement (GCC Extension).
15359 transaction-cancel-statement:
15360 __transaction_cancel transaction-attribute[opt] ;
15362 Note that the only valid attribute is "outer".
15365 static tree
15366 c_parser_transaction_cancel (c_parser *parser)
15368 location_t loc = c_parser_peek_token (parser)->location;
15369 tree attrs;
15370 bool is_outer = false;
15372 gcc_assert (c_parser_next_token_is_keyword (parser, RID_TRANSACTION_CANCEL));
15373 c_parser_consume_token (parser);
15375 attrs = c_parser_transaction_attributes (parser);
15376 if (attrs)
15377 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
15379 if (!flag_tm)
15381 error_at (loc, "%<__transaction_cancel%> without "
15382 "transactional memory support enabled");
15383 goto ret_error;
15385 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
15387 error_at (loc, "%<__transaction_cancel%> within a "
15388 "%<__transaction_relaxed%>");
15389 goto ret_error;
15391 else if (is_outer)
15393 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
15394 && !is_tm_may_cancel_outer (current_function_decl))
15396 error_at (loc, "outer %<__transaction_cancel%> not "
15397 "within outer %<__transaction_atomic%>");
15398 error_at (loc, " or a %<transaction_may_cancel_outer%> function");
15399 goto ret_error;
15402 else if (parser->in_transaction == 0)
15404 error_at (loc, "%<__transaction_cancel%> not within "
15405 "%<__transaction_atomic%>");
15406 goto ret_error;
15409 return add_stmt (build_tm_abort_call (loc, is_outer));
15411 ret_error:
15412 return build1 (NOP_EXPR, void_type_node, error_mark_node);
15415 /* Parse a single source file. */
15417 void
15418 c_parse_file (void)
15420 /* Use local storage to begin. If the first token is a pragma, parse it.
15421 If it is #pragma GCC pch_preprocess, then this will load a PCH file
15422 which will cause garbage collection. */
15423 c_parser tparser;
15425 memset (&tparser, 0, sizeof tparser);
15426 tparser.tokens = &tparser.tokens_buf[0];
15427 the_parser = &tparser;
15429 if (c_parser_peek_token (&tparser)->pragma_kind == PRAGMA_GCC_PCH_PREPROCESS)
15430 c_parser_pragma_pch_preprocess (&tparser);
15432 the_parser = ggc_alloc<c_parser> ();
15433 *the_parser = tparser;
15434 if (tparser.tokens == &tparser.tokens_buf[0])
15435 the_parser->tokens = &the_parser->tokens_buf[0];
15437 /* Initialize EH, if we've been told to do so. */
15438 if (flag_exceptions)
15439 using_eh_for_cleanups ();
15441 c_parser_translation_unit (the_parser);
15442 the_parser = NULL;
15445 /* This function parses Cilk Plus array notation. The starting index is
15446 passed in INITIAL_INDEX and the array name is passes in ARRAY_VALUE. The
15447 return value of this function is a tree_node called VALUE_TREE of type
15448 ARRAY_NOTATION_REF. */
15450 static tree
15451 c_parser_array_notation (location_t loc, c_parser *parser, tree initial_index,
15452 tree array_value)
15454 c_token *token = NULL;
15455 tree start_index = NULL_TREE, end_index = NULL_TREE, stride = NULL_TREE;
15456 tree value_tree = NULL_TREE, type = NULL_TREE, array_type = NULL_TREE;
15457 tree array_type_domain = NULL_TREE;
15459 if (array_value == error_mark_node || initial_index == error_mark_node)
15461 /* No need to continue. If either of these 2 were true, then an error
15462 must be emitted already. Thus, no need to emit them twice. */
15463 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
15464 return error_mark_node;
15467 array_type = TREE_TYPE (array_value);
15468 gcc_assert (array_type);
15469 if (TREE_CODE (array_type) != ARRAY_TYPE
15470 && TREE_CODE (array_type) != POINTER_TYPE)
15472 error_at (loc, "base of array section must be pointer or array type");
15473 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
15474 return error_mark_node;
15476 type = TREE_TYPE (array_type);
15477 token = c_parser_peek_token (parser);
15479 if (token->type == CPP_EOF)
15481 c_parser_error (parser, "expected %<:%> or numeral");
15482 return value_tree;
15484 else if (token->type == CPP_COLON)
15486 if (!initial_index)
15488 /* If we are here, then we have a case like this A[:]. */
15489 c_parser_consume_token (parser);
15490 if (TREE_CODE (array_type) == POINTER_TYPE)
15492 error_at (loc, "start-index and length fields necessary for "
15493 "using array notations in pointers");
15494 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
15495 return error_mark_node;
15497 if (TREE_CODE (array_type) == FUNCTION_TYPE)
15499 error_at (loc, "array notations cannot be used with function "
15500 "type");
15501 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
15502 return error_mark_node;
15504 array_type_domain = TYPE_DOMAIN (array_type);
15506 if (!array_type_domain)
15508 error_at (loc, "start-index and length fields necessary for "
15509 "using array notations in dimensionless arrays");
15510 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
15511 return error_mark_node;
15514 start_index = TYPE_MINVAL (array_type_domain);
15515 start_index = fold_build1 (CONVERT_EXPR, ptrdiff_type_node,
15516 start_index);
15517 if (!TYPE_MAXVAL (array_type_domain)
15518 || !TREE_CONSTANT (TYPE_MAXVAL (array_type_domain)))
15520 error_at (loc, "start-index and length fields necessary for "
15521 "using array notations in variable-length arrays");
15522 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
15523 return error_mark_node;
15525 end_index = TYPE_MAXVAL (array_type_domain);
15526 end_index = fold_build2 (PLUS_EXPR, TREE_TYPE (end_index),
15527 end_index, integer_one_node);
15528 end_index = fold_build1 (CONVERT_EXPR, ptrdiff_type_node, end_index);
15529 stride = build_int_cst (integer_type_node, 1);
15530 stride = fold_build1 (CONVERT_EXPR, ptrdiff_type_node, stride);
15532 else if (initial_index != error_mark_node)
15534 /* If we are here, then there should be 2 possibilities:
15535 1. Array [EXPR : EXPR]
15536 2. Array [EXPR : EXPR : EXPR]
15538 start_index = initial_index;
15540 if (TREE_CODE (array_type) == FUNCTION_TYPE)
15542 error_at (loc, "array notations cannot be used with function "
15543 "type");
15544 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
15545 return error_mark_node;
15547 c_parser_consume_token (parser); /* consume the ':' */
15548 struct c_expr ce = c_parser_expression (parser);
15549 ce = convert_lvalue_to_rvalue (loc, ce, false, false);
15550 end_index = ce.value;
15551 if (!end_index || end_index == error_mark_node)
15553 c_parser_skip_to_end_of_block_or_statement (parser);
15554 return error_mark_node;
15556 if (c_parser_peek_token (parser)->type == CPP_COLON)
15558 c_parser_consume_token (parser);
15559 ce = c_parser_expression (parser);
15560 ce = convert_lvalue_to_rvalue (loc, ce, false, false);
15561 stride = ce.value;
15562 if (!stride || stride == error_mark_node)
15564 c_parser_skip_to_end_of_block_or_statement (parser);
15565 return error_mark_node;
15569 else
15570 c_parser_error (parser, "expected array notation expression");
15572 else
15573 c_parser_error (parser, "expected array notation expression");
15575 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, "expected %<]%>");
15577 value_tree = build_array_notation_ref (loc, array_value, start_index,
15578 end_index, stride, type);
15579 if (value_tree != error_mark_node)
15580 SET_EXPR_LOCATION (value_tree, loc);
15581 return value_tree;
15584 #include "gt-c-c-parser.h"