Merge aosp-toolchain/gcc/gcc-4_9 changes.
[official-gcc.git] / gcc-4_9-mobile / gcc / c / c-parser.c
blob264c17026eb2ba8ee0fb2b6e3950b79fa677af2b
1 /* Parser for C and Objective-C.
2 Copyright (C) 1987-2014 Free Software Foundation, Inc.
4 Parser actions based on the old Bison parser; structure somewhat
5 influenced by and fragments based on the C++ parser.
7 This file is part of GCC.
9 GCC is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 3, or (at your option) any later
12 version.
14 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 for more details.
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3. If not see
21 <http://www.gnu.org/licenses/>. */
23 /* TODO:
25 Make sure all relevant comments, and all relevant code from all
26 actions, brought over from old parser. Verify exact correspondence
27 of syntax accepted.
29 Add testcases covering every input symbol in every state in old and
30 new parsers.
32 Include full syntax for GNU C, including erroneous cases accepted
33 with error messages, in syntax productions in comments.
35 Make more diagnostics in the front end generally take an explicit
36 location rather than implicitly using input_location. */
38 #include "config.h"
39 #include "system.h"
40 #include "coretypes.h"
41 #include "tm.h" /* For rtl.h: needs enum reg_class. */
42 #include "tree.h"
43 #include "stringpool.h"
44 #include "attribs.h"
45 #include "stor-layout.h"
46 #include "varasm.h"
47 #include "trans-mem.h"
48 #include "langhooks.h"
49 #include "input.h"
50 #include "cpplib.h"
51 #include "timevar.h"
52 #include "c-family/c-pragma.h"
53 #include "c-tree.h"
54 #include "c-lang.h"
55 #include "flags.h"
56 #include "ggc.h"
57 #include "c-family/c-common.h"
58 #include "c-family/c-objc.h"
59 #include "vec.h"
60 #include "target.h"
61 #include "cgraph.h"
62 #include "plugin.h"
63 #include "omp-low.h"
66 /* Initialization routine for this file. */
68 void
69 c_parse_init (void)
71 /* The only initialization required is of the reserved word
72 identifiers. */
73 unsigned int i;
74 tree id;
75 int mask = 0;
77 /* Make sure RID_MAX hasn't grown past the 8 bits used to hold the keyword in
78 the c_token structure. */
79 gcc_assert (RID_MAX <= 255);
81 mask |= D_CXXONLY;
82 if (!flag_isoc99)
83 mask |= D_C99;
84 if (flag_no_asm)
86 mask |= D_ASM | D_EXT;
87 if (!flag_isoc99)
88 mask |= D_EXT89;
90 if (!c_dialect_objc ())
91 mask |= D_OBJC | D_CXX_OBJC;
93 ridpointers = ggc_alloc_cleared_vec_tree ((int) RID_MAX);
94 for (i = 0; i < num_c_common_reswords; i++)
96 /* If a keyword is disabled, do not enter it into the table
97 and so create a canonical spelling that isn't a keyword. */
98 if (c_common_reswords[i].disable & mask)
100 if (warn_cxx_compat
101 && (c_common_reswords[i].disable & D_CXXWARN))
103 id = get_identifier (c_common_reswords[i].word);
104 C_SET_RID_CODE (id, RID_CXX_COMPAT_WARN);
105 C_IS_RESERVED_WORD (id) = 1;
107 continue;
110 id = get_identifier (c_common_reswords[i].word);
111 C_SET_RID_CODE (id, c_common_reswords[i].rid);
112 C_IS_RESERVED_WORD (id) = 1;
113 ridpointers [(int) c_common_reswords[i].rid] = id;
117 /* The C lexer intermediates between the lexer in cpplib and c-lex.c
118 and the C parser. Unlike the C++ lexer, the parser structure
119 stores the lexer information instead of using a separate structure.
120 Identifiers are separated into ordinary identifiers, type names,
121 keywords and some other Objective-C types of identifiers, and some
122 look-ahead is maintained.
124 ??? It might be a good idea to lex the whole file up front (as for
125 C++). It would then be possible to share more of the C and C++
126 lexer code, if desired. */
128 /* The following local token type is used. */
130 /* A keyword. */
131 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
133 /* More information about the type of a CPP_NAME token. */
134 typedef enum c_id_kind {
135 /* An ordinary identifier. */
136 C_ID_ID,
137 /* An identifier declared as a typedef name. */
138 C_ID_TYPENAME,
139 /* An identifier declared as an Objective-C class name. */
140 C_ID_CLASSNAME,
141 /* An address space identifier. */
142 C_ID_ADDRSPACE,
143 /* Not an identifier. */
144 C_ID_NONE
145 } c_id_kind;
147 /* A single C token after string literal concatenation and conversion
148 of preprocessing tokens to tokens. */
149 typedef struct GTY (()) c_token {
150 /* The kind of token. */
151 ENUM_BITFIELD (cpp_ttype) type : 8;
152 /* If this token is a CPP_NAME, this value indicates whether also
153 declared as some kind of type. Otherwise, it is C_ID_NONE. */
154 ENUM_BITFIELD (c_id_kind) id_kind : 8;
155 /* If this token is a keyword, this value indicates which keyword.
156 Otherwise, this value is RID_MAX. */
157 ENUM_BITFIELD (rid) keyword : 8;
158 /* If this token is a CPP_PRAGMA, this indicates the pragma that
159 was seen. Otherwise it is PRAGMA_NONE. */
160 ENUM_BITFIELD (pragma_kind) pragma_kind : 8;
161 /* The location at which this token was found. */
162 location_t location;
163 /* The value associated with this token, if any. */
164 tree value;
165 } c_token;
167 /* A parser structure recording information about the state and
168 context of parsing. Includes lexer information with up to two
169 tokens of look-ahead; more are not needed for C. */
170 typedef struct GTY(()) c_parser {
171 /* The look-ahead tokens. */
172 c_token * GTY((skip)) tokens;
173 /* Buffer for look-ahead tokens. */
174 c_token tokens_buf[2];
175 /* How many look-ahead tokens are available (0, 1 or 2, or
176 more if parsing from pre-lexed tokens). */
177 unsigned int tokens_avail;
178 /* True if a syntax error is being recovered from; false otherwise.
179 c_parser_error sets this flag. It should clear this flag when
180 enough tokens have been consumed to recover from the error. */
181 BOOL_BITFIELD error : 1;
182 /* True if we're processing a pragma, and shouldn't automatically
183 consume CPP_PRAGMA_EOL. */
184 BOOL_BITFIELD in_pragma : 1;
185 /* True if we're parsing the outermost block of an if statement. */
186 BOOL_BITFIELD in_if_block : 1;
187 /* True if we want to lex an untranslated string. */
188 BOOL_BITFIELD lex_untranslated_string : 1;
190 /* Objective-C specific parser/lexer information. */
192 /* True if we are in a context where the Objective-C "PQ" keywords
193 are considered keywords. */
194 BOOL_BITFIELD objc_pq_context : 1;
195 /* True if we are parsing a (potential) Objective-C foreach
196 statement. This is set to true after we parsed 'for (' and while
197 we wait for 'in' or ';' to decide if it's a standard C for loop or an
198 Objective-C foreach loop. */
199 BOOL_BITFIELD objc_could_be_foreach_context : 1;
200 /* The following flag is needed to contextualize Objective-C lexical
201 analysis. In some cases (e.g., 'int NSObject;'), it is
202 undesirable to bind an identifier to an Objective-C class, even
203 if a class with that name exists. */
204 BOOL_BITFIELD objc_need_raw_identifier : 1;
205 /* Nonzero if we're processing a __transaction statement. The value
206 is 1 | TM_STMT_ATTR_*. */
207 unsigned int in_transaction : 4;
208 /* True if we are in a context where the Objective-C "Property attribute"
209 keywords are valid. */
210 BOOL_BITFIELD objc_property_attr_context : 1;
212 /* Cilk Plus specific parser/lexer information. */
214 /* Buffer to hold all the tokens from parsing the vector attribute for the
215 SIMD-enabled functions (formerly known as elemental functions). */
216 vec <c_token, va_gc> *cilk_simd_fn_tokens;
217 } c_parser;
220 /* The actual parser and external interface. ??? Does this need to be
221 garbage-collected? */
223 static GTY (()) c_parser *the_parser;
225 /* Read in and lex a single token, storing it in *TOKEN. */
227 static void
228 c_lex_one_token (c_parser *parser, c_token *token)
230 timevar_push (TV_LEX);
232 token->type = c_lex_with_flags (&token->value, &token->location, NULL,
233 (parser->lex_untranslated_string
234 ? C_LEX_STRING_NO_TRANSLATE : 0));
235 token->id_kind = C_ID_NONE;
236 token->keyword = RID_MAX;
237 token->pragma_kind = PRAGMA_NONE;
239 switch (token->type)
241 case CPP_NAME:
243 tree decl;
245 bool objc_force_identifier = parser->objc_need_raw_identifier;
246 if (c_dialect_objc ())
247 parser->objc_need_raw_identifier = false;
249 if (C_IS_RESERVED_WORD (token->value))
251 enum rid rid_code = C_RID_CODE (token->value);
253 if (rid_code == RID_CXX_COMPAT_WARN)
255 warning_at (token->location,
256 OPT_Wc___compat,
257 "identifier %qE conflicts with C++ keyword",
258 token->value);
260 else if (rid_code >= RID_FIRST_ADDR_SPACE
261 && rid_code <= RID_LAST_ADDR_SPACE)
263 token->id_kind = C_ID_ADDRSPACE;
264 token->keyword = rid_code;
265 break;
267 else if (c_dialect_objc () && OBJC_IS_PQ_KEYWORD (rid_code))
269 /* We found an Objective-C "pq" keyword (in, out,
270 inout, bycopy, byref, oneway). They need special
271 care because the interpretation depends on the
272 context. */
273 if (parser->objc_pq_context)
275 token->type = CPP_KEYWORD;
276 token->keyword = rid_code;
277 break;
279 else if (parser->objc_could_be_foreach_context
280 && rid_code == RID_IN)
282 /* We are in Objective-C, inside a (potential)
283 foreach context (which means after having
284 parsed 'for (', but before having parsed ';'),
285 and we found 'in'. We consider it the keyword
286 which terminates the declaration at the
287 beginning of a foreach-statement. Note that
288 this means you can't use 'in' for anything else
289 in that context; in particular, in Objective-C
290 you can't use 'in' as the name of the running
291 variable in a C for loop. We could potentially
292 try to add code here to disambiguate, but it
293 seems a reasonable limitation. */
294 token->type = CPP_KEYWORD;
295 token->keyword = rid_code;
296 break;
298 /* Else, "pq" keywords outside of the "pq" context are
299 not keywords, and we fall through to the code for
300 normal tokens. */
302 else if (c_dialect_objc () && OBJC_IS_PATTR_KEYWORD (rid_code))
304 /* We found an Objective-C "property attribute"
305 keyword (getter, setter, readonly, etc). These are
306 only valid in the property context. */
307 if (parser->objc_property_attr_context)
309 token->type = CPP_KEYWORD;
310 token->keyword = rid_code;
311 break;
313 /* Else they are not special keywords.
316 else if (c_dialect_objc ()
317 && (OBJC_IS_AT_KEYWORD (rid_code)
318 || OBJC_IS_CXX_KEYWORD (rid_code)))
320 /* We found one of the Objective-C "@" keywords (defs,
321 selector, synchronized, etc) or one of the
322 Objective-C "cxx" keywords (class, private,
323 protected, public, try, catch, throw) without a
324 preceding '@' sign. Do nothing and fall through to
325 the code for normal tokens (in C++ we would still
326 consider the CXX ones keywords, but not in C). */
329 else
331 token->type = CPP_KEYWORD;
332 token->keyword = rid_code;
333 break;
337 decl = lookup_name (token->value);
338 if (decl)
340 if (TREE_CODE (decl) == TYPE_DECL)
342 token->id_kind = C_ID_TYPENAME;
343 break;
346 else if (c_dialect_objc ())
348 tree objc_interface_decl = objc_is_class_name (token->value);
349 /* Objective-C class names are in the same namespace as
350 variables and typedefs, and hence are shadowed by local
351 declarations. */
352 if (objc_interface_decl
353 && (!objc_force_identifier || global_bindings_p ()))
355 token->value = objc_interface_decl;
356 token->id_kind = C_ID_CLASSNAME;
357 break;
360 token->id_kind = C_ID_ID;
362 break;
363 case CPP_AT_NAME:
364 /* This only happens in Objective-C; it must be a keyword. */
365 token->type = CPP_KEYWORD;
366 switch (C_RID_CODE (token->value))
368 /* Replace 'class' with '@class', 'private' with '@private',
369 etc. This prevents confusion with the C++ keyword
370 'class', and makes the tokens consistent with other
371 Objective-C 'AT' keywords. For example '@class' is
372 reported as RID_AT_CLASS which is consistent with
373 '@synchronized', which is reported as
374 RID_AT_SYNCHRONIZED.
376 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
377 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
378 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
379 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
380 case RID_THROW: token->keyword = RID_AT_THROW; break;
381 case RID_TRY: token->keyword = RID_AT_TRY; break;
382 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
383 default: token->keyword = C_RID_CODE (token->value);
385 break;
386 case CPP_COLON:
387 case CPP_COMMA:
388 case CPP_CLOSE_PAREN:
389 case CPP_SEMICOLON:
390 /* These tokens may affect the interpretation of any identifiers
391 following, if doing Objective-C. */
392 if (c_dialect_objc ())
393 parser->objc_need_raw_identifier = false;
394 break;
395 case CPP_PRAGMA:
396 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
397 token->pragma_kind = (enum pragma_kind) TREE_INT_CST_LOW (token->value);
398 token->value = NULL;
399 break;
400 default:
401 break;
403 timevar_pop (TV_LEX);
406 /* Return a pointer to the next token from PARSER, reading it in if
407 necessary. */
409 static inline c_token *
410 c_parser_peek_token (c_parser *parser)
412 if (parser->tokens_avail == 0)
414 c_lex_one_token (parser, &parser->tokens[0]);
415 parser->tokens_avail = 1;
417 return &parser->tokens[0];
420 /* Return true if the next token from PARSER has the indicated
421 TYPE. */
423 static inline bool
424 c_parser_next_token_is (c_parser *parser, enum cpp_ttype type)
426 return c_parser_peek_token (parser)->type == type;
429 /* Return true if the next token from PARSER does not have the
430 indicated TYPE. */
432 static inline bool
433 c_parser_next_token_is_not (c_parser *parser, enum cpp_ttype type)
435 return !c_parser_next_token_is (parser, type);
438 /* Return true if the next token from PARSER is the indicated
439 KEYWORD. */
441 static inline bool
442 c_parser_next_token_is_keyword (c_parser *parser, enum rid keyword)
444 return c_parser_peek_token (parser)->keyword == keyword;
447 /* Return a pointer to the next-but-one token from PARSER, reading it
448 in if necessary. The next token is already read in. */
450 static c_token *
451 c_parser_peek_2nd_token (c_parser *parser)
453 if (parser->tokens_avail >= 2)
454 return &parser->tokens[1];
455 gcc_assert (parser->tokens_avail == 1);
456 gcc_assert (parser->tokens[0].type != CPP_EOF);
457 gcc_assert (parser->tokens[0].type != CPP_PRAGMA_EOL);
458 c_lex_one_token (parser, &parser->tokens[1]);
459 parser->tokens_avail = 2;
460 return &parser->tokens[1];
463 /* Return true if TOKEN can start a type name,
464 false otherwise. */
465 static bool
466 c_token_starts_typename (c_token *token)
468 switch (token->type)
470 case CPP_NAME:
471 switch (token->id_kind)
473 case C_ID_ID:
474 return false;
475 case C_ID_ADDRSPACE:
476 return true;
477 case C_ID_TYPENAME:
478 return true;
479 case C_ID_CLASSNAME:
480 gcc_assert (c_dialect_objc ());
481 return true;
482 default:
483 gcc_unreachable ();
485 case CPP_KEYWORD:
486 switch (token->keyword)
488 case RID_UNSIGNED:
489 case RID_LONG:
490 case RID_INT128:
491 case RID_SHORT:
492 case RID_SIGNED:
493 case RID_COMPLEX:
494 case RID_INT:
495 case RID_CHAR:
496 case RID_FLOAT:
497 case RID_DOUBLE:
498 case RID_VOID:
499 case RID_DFLOAT32:
500 case RID_DFLOAT64:
501 case RID_DFLOAT128:
502 case RID_BOOL:
503 case RID_ENUM:
504 case RID_STRUCT:
505 case RID_UNION:
506 case RID_TYPEOF:
507 case RID_CONST:
508 case RID_ATOMIC:
509 case RID_VOLATILE:
510 case RID_RESTRICT:
511 case RID_ATTRIBUTE:
512 case RID_FRACT:
513 case RID_ACCUM:
514 case RID_SAT:
515 case RID_AUTO_TYPE:
516 return true;
517 default:
518 return false;
520 case CPP_LESS:
521 if (c_dialect_objc ())
522 return true;
523 return false;
524 default:
525 return false;
529 enum c_lookahead_kind {
530 /* Always treat unknown identifiers as typenames. */
531 cla_prefer_type,
533 /* Could be parsing a nonabstract declarator. Only treat an identifier
534 as a typename if followed by another identifier or a star. */
535 cla_nonabstract_decl,
537 /* Never treat identifiers as typenames. */
538 cla_prefer_id
541 /* Return true if the next token from PARSER can start a type name,
542 false otherwise. LA specifies how to do lookahead in order to
543 detect unknown type names. If unsure, pick CLA_PREFER_ID. */
545 static inline bool
546 c_parser_next_tokens_start_typename (c_parser *parser, enum c_lookahead_kind la)
548 c_token *token = c_parser_peek_token (parser);
549 if (c_token_starts_typename (token))
550 return true;
552 /* Try a bit harder to detect an unknown typename. */
553 if (la != cla_prefer_id
554 && token->type == CPP_NAME
555 && token->id_kind == C_ID_ID
557 /* Do not try too hard when we could have "object in array". */
558 && !parser->objc_could_be_foreach_context
560 && (la == cla_prefer_type
561 || c_parser_peek_2nd_token (parser)->type == CPP_NAME
562 || c_parser_peek_2nd_token (parser)->type == CPP_MULT)
564 /* Only unknown identifiers. */
565 && !lookup_name (token->value))
566 return true;
568 return false;
571 /* Return true if TOKEN is a type qualifier, false otherwise. */
572 static bool
573 c_token_is_qualifier (c_token *token)
575 switch (token->type)
577 case CPP_NAME:
578 switch (token->id_kind)
580 case C_ID_ADDRSPACE:
581 return true;
582 default:
583 return false;
585 case CPP_KEYWORD:
586 switch (token->keyword)
588 case RID_CONST:
589 case RID_VOLATILE:
590 case RID_RESTRICT:
591 case RID_ATTRIBUTE:
592 case RID_ATOMIC:
593 return true;
594 default:
595 return false;
597 case CPP_LESS:
598 return false;
599 default:
600 gcc_unreachable ();
604 /* Return true if the next token from PARSER is a type qualifier,
605 false otherwise. */
606 static inline bool
607 c_parser_next_token_is_qualifier (c_parser *parser)
609 c_token *token = c_parser_peek_token (parser);
610 return c_token_is_qualifier (token);
613 /* Return true if TOKEN can start declaration specifiers, false
614 otherwise. */
615 static bool
616 c_token_starts_declspecs (c_token *token)
618 switch (token->type)
620 case CPP_NAME:
621 switch (token->id_kind)
623 case C_ID_ID:
624 return false;
625 case C_ID_ADDRSPACE:
626 return true;
627 case C_ID_TYPENAME:
628 return true;
629 case C_ID_CLASSNAME:
630 gcc_assert (c_dialect_objc ());
631 return true;
632 default:
633 gcc_unreachable ();
635 case CPP_KEYWORD:
636 switch (token->keyword)
638 case RID_STATIC:
639 case RID_EXTERN:
640 case RID_REGISTER:
641 case RID_TYPEDEF:
642 case RID_INLINE:
643 case RID_NORETURN:
644 case RID_AUTO:
645 case RID_THREAD:
646 case RID_UNSIGNED:
647 case RID_LONG:
648 case RID_INT128:
649 case RID_SHORT:
650 case RID_SIGNED:
651 case RID_COMPLEX:
652 case RID_INT:
653 case RID_CHAR:
654 case RID_FLOAT:
655 case RID_DOUBLE:
656 case RID_VOID:
657 case RID_DFLOAT32:
658 case RID_DFLOAT64:
659 case RID_DFLOAT128:
660 case RID_BOOL:
661 case RID_ENUM:
662 case RID_STRUCT:
663 case RID_UNION:
664 case RID_TYPEOF:
665 case RID_CONST:
666 case RID_VOLATILE:
667 case RID_RESTRICT:
668 case RID_ATTRIBUTE:
669 case RID_FRACT:
670 case RID_ACCUM:
671 case RID_SAT:
672 case RID_ALIGNAS:
673 case RID_ATOMIC:
674 case RID_AUTO_TYPE:
675 return true;
676 default:
677 return false;
679 case CPP_LESS:
680 if (c_dialect_objc ())
681 return true;
682 return false;
683 default:
684 return false;
689 /* Return true if TOKEN can start declaration specifiers or a static
690 assertion, false otherwise. */
691 static bool
692 c_token_starts_declaration (c_token *token)
694 if (c_token_starts_declspecs (token)
695 || token->keyword == RID_STATIC_ASSERT)
696 return true;
697 else
698 return false;
701 /* Return true if the next token from PARSER can start declaration
702 specifiers, false otherwise. */
703 static inline bool
704 c_parser_next_token_starts_declspecs (c_parser *parser)
706 c_token *token = c_parser_peek_token (parser);
708 /* In Objective-C, a classname normally starts a declspecs unless it
709 is immediately followed by a dot. In that case, it is the
710 Objective-C 2.0 "dot-syntax" for class objects, ie, calls the
711 setter/getter on the class. c_token_starts_declspecs() can't
712 differentiate between the two cases because it only checks the
713 current token, so we have a special check here. */
714 if (c_dialect_objc ()
715 && token->type == CPP_NAME
716 && token->id_kind == C_ID_CLASSNAME
717 && c_parser_peek_2nd_token (parser)->type == CPP_DOT)
718 return false;
720 return c_token_starts_declspecs (token);
723 /* Return true if the next tokens from PARSER can start declaration
724 specifiers or a static assertion, false otherwise. */
725 static inline bool
726 c_parser_next_tokens_start_declaration (c_parser *parser)
728 c_token *token = c_parser_peek_token (parser);
730 /* Same as above. */
731 if (c_dialect_objc ()
732 && token->type == CPP_NAME
733 && token->id_kind == C_ID_CLASSNAME
734 && c_parser_peek_2nd_token (parser)->type == CPP_DOT)
735 return false;
737 /* Labels do not start declarations. */
738 if (token->type == CPP_NAME
739 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
740 return false;
742 if (c_token_starts_declaration (token))
743 return true;
745 if (c_parser_next_tokens_start_typename (parser, cla_nonabstract_decl))
746 return true;
748 return false;
751 /* Consume the next token from PARSER. */
753 static void
754 c_parser_consume_token (c_parser *parser)
756 gcc_assert (parser->tokens_avail >= 1);
757 gcc_assert (parser->tokens[0].type != CPP_EOF);
758 gcc_assert (!parser->in_pragma || parser->tokens[0].type != CPP_PRAGMA_EOL);
759 gcc_assert (parser->error || parser->tokens[0].type != CPP_PRAGMA);
760 if (parser->tokens != &parser->tokens_buf[0])
761 parser->tokens++;
762 else if (parser->tokens_avail == 2)
763 parser->tokens[0] = parser->tokens[1];
764 parser->tokens_avail--;
767 /* Expect the current token to be a #pragma. Consume it and remember
768 that we've begun parsing a pragma. */
770 static void
771 c_parser_consume_pragma (c_parser *parser)
773 gcc_assert (!parser->in_pragma);
774 gcc_assert (parser->tokens_avail >= 1);
775 gcc_assert (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--;
781 parser->in_pragma = true;
784 /* Update the global input_location from TOKEN. */
785 static inline void
786 c_parser_set_source_position_from_token (c_token *token)
788 if (token->type != CPP_EOF)
790 input_location = token->location;
794 /* Issue a diagnostic of the form
795 FILE:LINE: MESSAGE before TOKEN
796 where TOKEN is the next token in the input stream of PARSER.
797 MESSAGE (specified by the caller) is usually of the form "expected
798 OTHER-TOKEN".
800 Do not issue a diagnostic if still recovering from an error.
802 ??? This is taken from the C++ parser, but building up messages in
803 this way is not i18n-friendly and some other approach should be
804 used. */
806 static void
807 c_parser_error (c_parser *parser, const char *gmsgid)
809 c_token *token = c_parser_peek_token (parser);
810 if (parser->error)
811 return;
812 parser->error = true;
813 if (!gmsgid)
814 return;
815 /* This diagnostic makes more sense if it is tagged to the line of
816 the token we just peeked at. */
817 c_parser_set_source_position_from_token (token);
818 c_parse_error (gmsgid,
819 /* Because c_parse_error does not understand
820 CPP_KEYWORD, keywords are treated like
821 identifiers. */
822 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
823 /* ??? The C parser does not save the cpp flags of a
824 token, we need to pass 0 here and we will not get
825 the source spelling of some tokens but rather the
826 canonical spelling. */
827 token->value, /*flags=*/0);
830 /* If the next token is of the indicated TYPE, consume it. Otherwise,
831 issue the error MSGID. If MSGID is NULL then a message has already
832 been produced and no message will be produced this time. Returns
833 true if found, false otherwise. */
835 static bool
836 c_parser_require (c_parser *parser,
837 enum cpp_ttype type,
838 const char *msgid)
840 if (c_parser_next_token_is (parser, type))
842 c_parser_consume_token (parser);
843 return true;
845 else
847 c_parser_error (parser, msgid);
848 return false;
852 /* If the next token is the indicated keyword, consume it. Otherwise,
853 issue the error MSGID. Returns true if found, false otherwise. */
855 static bool
856 c_parser_require_keyword (c_parser *parser,
857 enum rid keyword,
858 const char *msgid)
860 if (c_parser_next_token_is_keyword (parser, keyword))
862 c_parser_consume_token (parser);
863 return true;
865 else
867 c_parser_error (parser, msgid);
868 return false;
872 /* Like c_parser_require, except that tokens will be skipped until the
873 desired token is found. An error message is still produced if the
874 next token is not as expected. If MSGID is NULL then a message has
875 already been produced and no message will be produced this
876 time. */
878 static void
879 c_parser_skip_until_found (c_parser *parser,
880 enum cpp_ttype type,
881 const char *msgid)
883 unsigned nesting_depth = 0;
885 if (c_parser_require (parser, type, msgid))
886 return;
888 /* Skip tokens until the desired token is found. */
889 while (true)
891 /* Peek at the next token. */
892 c_token *token = c_parser_peek_token (parser);
893 /* If we've reached the token we want, consume it and stop. */
894 if (token->type == type && !nesting_depth)
896 c_parser_consume_token (parser);
897 break;
900 /* If we've run out of tokens, stop. */
901 if (token->type == CPP_EOF)
902 return;
903 if (token->type == CPP_PRAGMA_EOL && parser->in_pragma)
904 return;
905 if (token->type == CPP_OPEN_BRACE
906 || token->type == CPP_OPEN_PAREN
907 || token->type == CPP_OPEN_SQUARE)
908 ++nesting_depth;
909 else if (token->type == CPP_CLOSE_BRACE
910 || token->type == CPP_CLOSE_PAREN
911 || token->type == CPP_CLOSE_SQUARE)
913 if (nesting_depth-- == 0)
914 break;
916 /* Consume this token. */
917 c_parser_consume_token (parser);
919 parser->error = false;
922 /* Skip tokens until the end of a parameter is found, but do not
923 consume the comma, semicolon or closing delimiter. */
925 static void
926 c_parser_skip_to_end_of_parameter (c_parser *parser)
928 unsigned nesting_depth = 0;
930 while (true)
932 c_token *token = c_parser_peek_token (parser);
933 if ((token->type == CPP_COMMA || token->type == CPP_SEMICOLON)
934 && !nesting_depth)
935 break;
936 /* If we've run out of tokens, stop. */
937 if (token->type == CPP_EOF)
938 return;
939 if (token->type == CPP_PRAGMA_EOL && parser->in_pragma)
940 return;
941 if (token->type == CPP_OPEN_BRACE
942 || token->type == CPP_OPEN_PAREN
943 || token->type == CPP_OPEN_SQUARE)
944 ++nesting_depth;
945 else if (token->type == CPP_CLOSE_BRACE
946 || token->type == CPP_CLOSE_PAREN
947 || token->type == CPP_CLOSE_SQUARE)
949 if (nesting_depth-- == 0)
950 break;
952 /* Consume this token. */
953 c_parser_consume_token (parser);
955 parser->error = false;
958 /* Expect to be at the end of the pragma directive and consume an
959 end of line marker. */
961 static void
962 c_parser_skip_to_pragma_eol (c_parser *parser)
964 gcc_assert (parser->in_pragma);
965 parser->in_pragma = false;
967 if (!c_parser_require (parser, CPP_PRAGMA_EOL, "expected end of line"))
968 while (true)
970 c_token *token = c_parser_peek_token (parser);
971 if (token->type == CPP_EOF)
972 break;
973 if (token->type == CPP_PRAGMA_EOL)
975 c_parser_consume_token (parser);
976 break;
978 c_parser_consume_token (parser);
981 parser->error = false;
984 /* Skip tokens until we have consumed an entire block, or until we
985 have consumed a non-nested ';'. */
987 static void
988 c_parser_skip_to_end_of_block_or_statement (c_parser *parser)
990 unsigned nesting_depth = 0;
991 bool save_error = parser->error;
993 while (true)
995 c_token *token;
997 /* Peek at the next token. */
998 token = c_parser_peek_token (parser);
1000 switch (token->type)
1002 case CPP_EOF:
1003 return;
1005 case CPP_PRAGMA_EOL:
1006 if (parser->in_pragma)
1007 return;
1008 break;
1010 case CPP_SEMICOLON:
1011 /* If the next token is a ';', we have reached the
1012 end of the statement. */
1013 if (!nesting_depth)
1015 /* Consume the ';'. */
1016 c_parser_consume_token (parser);
1017 goto finished;
1019 break;
1021 case CPP_CLOSE_BRACE:
1022 /* If the next token is a non-nested '}', then we have
1023 reached the end of the current block. */
1024 if (nesting_depth == 0 || --nesting_depth == 0)
1026 c_parser_consume_token (parser);
1027 goto finished;
1029 break;
1031 case CPP_OPEN_BRACE:
1032 /* If it the next token is a '{', then we are entering a new
1033 block. Consume the entire block. */
1034 ++nesting_depth;
1035 break;
1037 case CPP_PRAGMA:
1038 /* If we see a pragma, consume the whole thing at once. We
1039 have some safeguards against consuming pragmas willy-nilly.
1040 Normally, we'd expect to be here with parser->error set,
1041 which disables these safeguards. But it's possible to get
1042 here for secondary error recovery, after parser->error has
1043 been cleared. */
1044 c_parser_consume_pragma (parser);
1045 c_parser_skip_to_pragma_eol (parser);
1046 parser->error = save_error;
1047 continue;
1049 default:
1050 break;
1053 c_parser_consume_token (parser);
1056 finished:
1057 parser->error = false;
1060 /* CPP's options (initialized by c-opts.c). */
1061 extern cpp_options *cpp_opts;
1063 /* Save the warning flags which are controlled by __extension__. */
1065 static inline int
1066 disable_extension_diagnostics (void)
1068 int ret = (pedantic
1069 | (warn_pointer_arith << 1)
1070 | (warn_traditional << 2)
1071 | (flag_iso << 3)
1072 | (warn_long_long << 4)
1073 | (warn_cxx_compat << 5)
1074 | (warn_overlength_strings << 6));
1075 cpp_opts->cpp_pedantic = pedantic = 0;
1076 warn_pointer_arith = 0;
1077 cpp_opts->cpp_warn_traditional = warn_traditional = 0;
1078 flag_iso = 0;
1079 cpp_opts->cpp_warn_long_long = warn_long_long = 0;
1080 warn_cxx_compat = 0;
1081 warn_overlength_strings = 0;
1082 return ret;
1085 /* Restore the warning flags which are controlled by __extension__.
1086 FLAGS is the return value from disable_extension_diagnostics. */
1088 static inline void
1089 restore_extension_diagnostics (int flags)
1091 cpp_opts->cpp_pedantic = pedantic = flags & 1;
1092 warn_pointer_arith = (flags >> 1) & 1;
1093 cpp_opts->cpp_warn_traditional = warn_traditional = (flags >> 2) & 1;
1094 flag_iso = (flags >> 3) & 1;
1095 cpp_opts->cpp_warn_long_long = warn_long_long = (flags >> 4) & 1;
1096 warn_cxx_compat = (flags >> 5) & 1;
1097 warn_overlength_strings = (flags >> 6) & 1;
1100 /* Possibly kinds of declarator to parse. */
1101 typedef enum c_dtr_syn {
1102 /* A normal declarator with an identifier. */
1103 C_DTR_NORMAL,
1104 /* An abstract declarator (maybe empty). */
1105 C_DTR_ABSTRACT,
1106 /* A parameter declarator: may be either, but after a type name does
1107 not redeclare a typedef name as an identifier if it can
1108 alternatively be interpreted as a typedef name; see DR#009,
1109 applied in C90 TC1, omitted from C99 and reapplied in C99 TC2
1110 following DR#249. For example, given a typedef T, "int T" and
1111 "int *T" are valid parameter declarations redeclaring T, while
1112 "int (T)" and "int * (T)" and "int (T[])" and "int (T (int))" are
1113 abstract declarators rather than involving redundant parentheses;
1114 the same applies with attributes inside the parentheses before
1115 "T". */
1116 C_DTR_PARM
1117 } c_dtr_syn;
1119 /* The binary operation precedence levels, where 0 is a dummy lowest level
1120 used for the bottom of the stack. */
1121 enum c_parser_prec {
1122 PREC_NONE,
1123 PREC_LOGOR,
1124 PREC_LOGAND,
1125 PREC_BITOR,
1126 PREC_BITXOR,
1127 PREC_BITAND,
1128 PREC_EQ,
1129 PREC_REL,
1130 PREC_SHIFT,
1131 PREC_ADD,
1132 PREC_MULT,
1133 NUM_PRECS
1136 static void c_parser_external_declaration (c_parser *);
1137 static void c_parser_asm_definition (c_parser *);
1138 static void c_parser_declaration_or_fndef (c_parser *, bool, bool, bool,
1139 bool, bool, tree *, vec<c_token>);
1140 static void c_parser_static_assert_declaration_no_semi (c_parser *);
1141 static void c_parser_static_assert_declaration (c_parser *);
1142 static void c_parser_declspecs (c_parser *, struct c_declspecs *, bool, bool,
1143 bool, bool, bool, enum c_lookahead_kind);
1144 static struct c_typespec c_parser_enum_specifier (c_parser *);
1145 static struct c_typespec c_parser_struct_or_union_specifier (c_parser *);
1146 static tree c_parser_struct_declaration (c_parser *);
1147 static struct c_typespec c_parser_typeof_specifier (c_parser *);
1148 static tree c_parser_alignas_specifier (c_parser *);
1149 static struct c_declarator *c_parser_declarator (c_parser *, bool, c_dtr_syn,
1150 bool *);
1151 static struct c_declarator *c_parser_direct_declarator (c_parser *, bool,
1152 c_dtr_syn, bool *);
1153 static struct c_declarator *c_parser_direct_declarator_inner (c_parser *,
1154 bool,
1155 struct c_declarator *);
1156 static struct c_arg_info *c_parser_parms_declarator (c_parser *, bool, tree);
1157 static struct c_arg_info *c_parser_parms_list_declarator (c_parser *, tree,
1158 tree);
1159 static struct c_parm *c_parser_parameter_declaration (c_parser *, tree);
1160 static tree c_parser_simple_asm_expr (c_parser *);
1161 static tree c_parser_attributes (c_parser *);
1162 static struct c_type_name *c_parser_type_name (c_parser *);
1163 static struct c_expr c_parser_initializer (c_parser *);
1164 static struct c_expr c_parser_braced_init (c_parser *, tree, bool);
1165 static void c_parser_initelt (c_parser *, struct obstack *);
1166 static void c_parser_initval (c_parser *, struct c_expr *,
1167 struct obstack *);
1168 static tree c_parser_compound_statement (c_parser *);
1169 static void c_parser_compound_statement_nostart (c_parser *);
1170 static void c_parser_label (c_parser *);
1171 static void c_parser_statement (c_parser *);
1172 static void c_parser_statement_after_labels (c_parser *);
1173 static void c_parser_if_statement (c_parser *);
1174 static void c_parser_switch_statement (c_parser *);
1175 static void c_parser_while_statement (c_parser *, bool);
1176 static void c_parser_do_statement (c_parser *, bool);
1177 static void c_parser_for_statement (c_parser *, bool);
1178 static tree c_parser_asm_statement (c_parser *);
1179 static tree c_parser_asm_operands (c_parser *);
1180 static tree c_parser_asm_goto_operands (c_parser *);
1181 static tree c_parser_asm_clobbers (c_parser *);
1182 static struct c_expr c_parser_expr_no_commas (c_parser *, struct c_expr *,
1183 tree = NULL_TREE);
1184 static struct c_expr c_parser_conditional_expression (c_parser *,
1185 struct c_expr *, tree);
1186 static struct c_expr c_parser_binary_expression (c_parser *, struct c_expr *,
1187 tree);
1188 static struct c_expr c_parser_cast_expression (c_parser *, struct c_expr *);
1189 static struct c_expr c_parser_unary_expression (c_parser *);
1190 static struct c_expr c_parser_sizeof_expression (c_parser *);
1191 static struct c_expr c_parser_alignof_expression (c_parser *);
1192 static struct c_expr c_parser_postfix_expression (c_parser *);
1193 static struct c_expr c_parser_postfix_expression_after_paren_type (c_parser *,
1194 struct c_type_name *,
1195 location_t);
1196 static struct c_expr c_parser_postfix_expression_after_primary (c_parser *,
1197 location_t loc,
1198 struct c_expr);
1199 static tree c_parser_transaction (c_parser *, enum rid);
1200 static struct c_expr c_parser_transaction_expression (c_parser *, enum rid);
1201 static tree c_parser_transaction_cancel (c_parser *);
1202 static struct c_expr c_parser_expression (c_parser *);
1203 static struct c_expr c_parser_expression_conv (c_parser *);
1204 static vec<tree, va_gc> *c_parser_expr_list (c_parser *, bool, bool,
1205 vec<tree, va_gc> **, location_t *,
1206 tree *, vec<location_t> *);
1207 static void c_parser_omp_construct (c_parser *);
1208 static void c_parser_omp_threadprivate (c_parser *);
1209 static void c_parser_omp_barrier (c_parser *);
1210 static void c_parser_omp_flush (c_parser *);
1211 static void c_parser_omp_taskwait (c_parser *);
1212 static void c_parser_omp_taskyield (c_parser *);
1213 static void c_parser_omp_cancel (c_parser *);
1214 static void c_parser_omp_cancellation_point (c_parser *);
1216 enum pragma_context { pragma_external, pragma_struct, pragma_param,
1217 pragma_stmt, pragma_compound };
1218 static bool c_parser_pragma (c_parser *, enum pragma_context);
1219 static bool c_parser_omp_target (c_parser *, enum pragma_context);
1220 static void c_parser_omp_end_declare_target (c_parser *);
1221 static void c_parser_omp_declare (c_parser *, enum pragma_context);
1223 /* These Objective-C parser functions are only ever called when
1224 compiling Objective-C. */
1225 static void c_parser_objc_class_definition (c_parser *, tree);
1226 static void c_parser_objc_class_instance_variables (c_parser *);
1227 static void c_parser_objc_class_declaration (c_parser *);
1228 static void c_parser_objc_alias_declaration (c_parser *);
1229 static void c_parser_objc_protocol_definition (c_parser *, tree);
1230 static bool c_parser_objc_method_type (c_parser *);
1231 static void c_parser_objc_method_definition (c_parser *);
1232 static void c_parser_objc_methodprotolist (c_parser *);
1233 static void c_parser_objc_methodproto (c_parser *);
1234 static tree c_parser_objc_method_decl (c_parser *, bool, tree *, tree *);
1235 static tree c_parser_objc_type_name (c_parser *);
1236 static tree c_parser_objc_protocol_refs (c_parser *);
1237 static void c_parser_objc_try_catch_finally_statement (c_parser *);
1238 static void c_parser_objc_synchronized_statement (c_parser *);
1239 static tree c_parser_objc_selector (c_parser *);
1240 static tree c_parser_objc_selector_arg (c_parser *);
1241 static tree c_parser_objc_receiver (c_parser *);
1242 static tree c_parser_objc_message_args (c_parser *);
1243 static tree c_parser_objc_keywordexpr (c_parser *);
1244 static void c_parser_objc_at_property_declaration (c_parser *);
1245 static void c_parser_objc_at_synthesize_declaration (c_parser *);
1246 static void c_parser_objc_at_dynamic_declaration (c_parser *);
1247 static bool c_parser_objc_diagnose_bad_element_prefix
1248 (c_parser *, struct c_declspecs *);
1250 /* Cilk Plus supporting routines. */
1251 static void c_parser_cilk_simd (c_parser *);
1252 static bool c_parser_cilk_verify_simd (c_parser *, enum pragma_context);
1253 static tree c_parser_array_notation (location_t, c_parser *, tree, tree);
1254 static tree c_parser_cilk_clause_vectorlength (c_parser *, tree, bool);
1256 /* Parse a translation unit (C90 6.7, C99 6.9).
1258 translation-unit:
1259 external-declarations
1261 external-declarations:
1262 external-declaration
1263 external-declarations external-declaration
1265 GNU extensions:
1267 translation-unit:
1268 empty
1271 static void
1272 c_parser_translation_unit (c_parser *parser)
1274 if (c_parser_next_token_is (parser, CPP_EOF))
1276 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
1277 "ISO C forbids an empty translation unit");
1279 else
1281 void *obstack_position = obstack_alloc (&parser_obstack, 0);
1282 mark_valid_location_for_stdc_pragma (false);
1285 ggc_collect ();
1286 c_parser_external_declaration (parser);
1287 obstack_free (&parser_obstack, obstack_position);
1289 while (c_parser_next_token_is_not (parser, CPP_EOF));
1293 /* Parse an external declaration (C90 6.7, C99 6.9).
1295 external-declaration:
1296 function-definition
1297 declaration
1299 GNU extensions:
1301 external-declaration:
1302 asm-definition
1304 __extension__ external-declaration
1306 Objective-C:
1308 external-declaration:
1309 objc-class-definition
1310 objc-class-declaration
1311 objc-alias-declaration
1312 objc-protocol-definition
1313 objc-method-definition
1314 @end
1317 static void
1318 c_parser_external_declaration (c_parser *parser)
1320 int ext;
1321 switch (c_parser_peek_token (parser)->type)
1323 case CPP_KEYWORD:
1324 switch (c_parser_peek_token (parser)->keyword)
1326 case RID_EXTENSION:
1327 ext = disable_extension_diagnostics ();
1328 c_parser_consume_token (parser);
1329 c_parser_external_declaration (parser);
1330 restore_extension_diagnostics (ext);
1331 break;
1332 case RID_ASM:
1333 c_parser_asm_definition (parser);
1334 break;
1335 case RID_AT_INTERFACE:
1336 case RID_AT_IMPLEMENTATION:
1337 gcc_assert (c_dialect_objc ());
1338 c_parser_objc_class_definition (parser, NULL_TREE);
1339 break;
1340 case RID_AT_CLASS:
1341 gcc_assert (c_dialect_objc ());
1342 c_parser_objc_class_declaration (parser);
1343 break;
1344 case RID_AT_ALIAS:
1345 gcc_assert (c_dialect_objc ());
1346 c_parser_objc_alias_declaration (parser);
1347 break;
1348 case RID_AT_PROTOCOL:
1349 gcc_assert (c_dialect_objc ());
1350 c_parser_objc_protocol_definition (parser, NULL_TREE);
1351 break;
1352 case RID_AT_PROPERTY:
1353 gcc_assert (c_dialect_objc ());
1354 c_parser_objc_at_property_declaration (parser);
1355 break;
1356 case RID_AT_SYNTHESIZE:
1357 gcc_assert (c_dialect_objc ());
1358 c_parser_objc_at_synthesize_declaration (parser);
1359 break;
1360 case RID_AT_DYNAMIC:
1361 gcc_assert (c_dialect_objc ());
1362 c_parser_objc_at_dynamic_declaration (parser);
1363 break;
1364 case RID_AT_END:
1365 gcc_assert (c_dialect_objc ());
1366 c_parser_consume_token (parser);
1367 objc_finish_implementation ();
1368 break;
1369 default:
1370 goto decl_or_fndef;
1372 break;
1373 case CPP_SEMICOLON:
1374 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
1375 "ISO C does not allow extra %<;%> outside of a function");
1376 c_parser_consume_token (parser);
1377 break;
1378 case CPP_PRAGMA:
1379 mark_valid_location_for_stdc_pragma (true);
1380 c_parser_pragma (parser, pragma_external);
1381 mark_valid_location_for_stdc_pragma (false);
1382 break;
1383 case CPP_PLUS:
1384 case CPP_MINUS:
1385 if (c_dialect_objc ())
1387 c_parser_objc_method_definition (parser);
1388 break;
1390 /* Else fall through, and yield a syntax error trying to parse
1391 as a declaration or function definition. */
1392 default:
1393 decl_or_fndef:
1394 /* A declaration or a function definition (or, in Objective-C,
1395 an @interface or @protocol with prefix attributes). We can
1396 only tell which after parsing the declaration specifiers, if
1397 any, and the first declarator. */
1398 c_parser_declaration_or_fndef (parser, true, true, true, false, true,
1399 NULL, vNULL);
1400 break;
1404 static void c_finish_omp_declare_simd (c_parser *, tree, tree, vec<c_token>);
1406 /* Parse a declaration or function definition (C90 6.5, 6.7.1, C99
1407 6.7, 6.9.1). If FNDEF_OK is true, a function definition is
1408 accepted; otherwise (old-style parameter declarations) only other
1409 declarations are accepted. If STATIC_ASSERT_OK is true, a static
1410 assertion is accepted; otherwise (old-style parameter declarations)
1411 it is not. If NESTED is true, we are inside a function or parsing
1412 old-style parameter declarations; any functions encountered are
1413 nested functions and declaration specifiers are required; otherwise
1414 we are at top level and functions are normal functions and
1415 declaration specifiers may be optional. If EMPTY_OK is true, empty
1416 declarations are OK (subject to all other constraints); otherwise
1417 (old-style parameter declarations) they are diagnosed. If
1418 START_ATTR_OK is true, the declaration specifiers may start with
1419 attributes; otherwise they may not.
1420 OBJC_FOREACH_OBJECT_DECLARATION can be used to get back the parsed
1421 declaration when parsing an Objective-C foreach statement.
1423 declaration:
1424 declaration-specifiers init-declarator-list[opt] ;
1425 static_assert-declaration
1427 function-definition:
1428 declaration-specifiers[opt] declarator declaration-list[opt]
1429 compound-statement
1431 declaration-list:
1432 declaration
1433 declaration-list declaration
1435 init-declarator-list:
1436 init-declarator
1437 init-declarator-list , init-declarator
1439 init-declarator:
1440 declarator simple-asm-expr[opt] attributes[opt]
1441 declarator simple-asm-expr[opt] attributes[opt] = initializer
1443 GNU extensions:
1445 nested-function-definition:
1446 declaration-specifiers declarator declaration-list[opt]
1447 compound-statement
1449 Objective-C:
1450 attributes objc-class-definition
1451 attributes objc-category-definition
1452 attributes objc-protocol-definition
1454 The simple-asm-expr and attributes are GNU extensions.
1456 This function does not handle __extension__; that is handled in its
1457 callers. ??? Following the old parser, __extension__ may start
1458 external declarations, declarations in functions and declarations
1459 at the start of "for" loops, but not old-style parameter
1460 declarations.
1462 C99 requires declaration specifiers in a function definition; the
1463 absence is diagnosed through the diagnosis of implicit int. In GNU
1464 C we also allow but diagnose declarations without declaration
1465 specifiers, but only at top level (elsewhere they conflict with
1466 other syntax).
1468 In Objective-C, declarations of the looping variable in a foreach
1469 statement are exceptionally terminated by 'in' (for example, 'for
1470 (NSObject *object in array) { ... }').
1472 OpenMP:
1474 declaration:
1475 threadprivate-directive */
1477 static void
1478 c_parser_declaration_or_fndef (c_parser *parser, bool fndef_ok,
1479 bool static_assert_ok, bool empty_ok,
1480 bool nested, bool start_attr_ok,
1481 tree *objc_foreach_object_declaration,
1482 vec<c_token> omp_declare_simd_clauses)
1484 struct c_declspecs *specs;
1485 tree prefix_attrs;
1486 tree all_prefix_attrs;
1487 bool diagnosed_no_specs = false;
1488 location_t here = c_parser_peek_token (parser)->location;
1490 if (static_assert_ok
1491 && c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT))
1493 c_parser_static_assert_declaration (parser);
1494 return;
1496 specs = build_null_declspecs ();
1498 /* Try to detect an unknown type name when we have "A B" or "A *B". */
1499 if (c_parser_peek_token (parser)->type == CPP_NAME
1500 && c_parser_peek_token (parser)->id_kind == C_ID_ID
1501 && (c_parser_peek_2nd_token (parser)->type == CPP_NAME
1502 || c_parser_peek_2nd_token (parser)->type == CPP_MULT)
1503 && (!nested || !lookup_name (c_parser_peek_token (parser)->value)))
1505 error_at (here, "unknown type name %qE",
1506 c_parser_peek_token (parser)->value);
1508 /* Parse declspecs normally to get a correct pointer type, but avoid
1509 a further "fails to be a type name" error. Refuse nested functions
1510 since it is not how the user likely wants us to recover. */
1511 c_parser_peek_token (parser)->type = CPP_KEYWORD;
1512 c_parser_peek_token (parser)->keyword = RID_VOID;
1513 c_parser_peek_token (parser)->value = error_mark_node;
1514 fndef_ok = !nested;
1517 c_parser_declspecs (parser, specs, true, true, start_attr_ok,
1518 true, true, cla_nonabstract_decl);
1519 if (parser->error)
1521 c_parser_skip_to_end_of_block_or_statement (parser);
1522 return;
1524 if (nested && !specs->declspecs_seen_p)
1526 c_parser_error (parser, "expected declaration specifiers");
1527 c_parser_skip_to_end_of_block_or_statement (parser);
1528 return;
1530 finish_declspecs (specs);
1531 bool auto_type_p = specs->typespec_word == cts_auto_type;
1532 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1534 if (auto_type_p)
1535 error_at (here, "%<__auto_type%> in empty declaration");
1536 else if (empty_ok)
1537 shadow_tag (specs);
1538 else
1540 shadow_tag_warned (specs, 1);
1541 pedwarn (here, 0, "empty declaration");
1543 c_parser_consume_token (parser);
1544 return;
1547 /* Provide better error recovery. Note that a type name here is usually
1548 better diagnosed as a redeclaration. */
1549 if (empty_ok
1550 && specs->typespec_kind == ctsk_tagdef
1551 && c_parser_next_token_starts_declspecs (parser)
1552 && !c_parser_next_token_is (parser, CPP_NAME))
1554 c_parser_error (parser, "expected %<;%>, identifier or %<(%>");
1555 parser->error = false;
1556 shadow_tag_warned (specs, 1);
1557 return;
1559 else if (c_dialect_objc () && !auto_type_p)
1561 /* Prefix attributes are an error on method decls. */
1562 switch (c_parser_peek_token (parser)->type)
1564 case CPP_PLUS:
1565 case CPP_MINUS:
1566 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1567 return;
1568 if (specs->attrs)
1570 warning_at (c_parser_peek_token (parser)->location,
1571 OPT_Wattributes,
1572 "prefix attributes are ignored for methods");
1573 specs->attrs = NULL_TREE;
1575 if (fndef_ok)
1576 c_parser_objc_method_definition (parser);
1577 else
1578 c_parser_objc_methodproto (parser);
1579 return;
1580 break;
1581 default:
1582 break;
1584 /* This is where we parse 'attributes @interface ...',
1585 'attributes @implementation ...', 'attributes @protocol ...'
1586 (where attributes could be, for example, __attribute__
1587 ((deprecated)).
1589 switch (c_parser_peek_token (parser)->keyword)
1591 case RID_AT_INTERFACE:
1593 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1594 return;
1595 c_parser_objc_class_definition (parser, specs->attrs);
1596 return;
1598 break;
1599 case RID_AT_IMPLEMENTATION:
1601 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1602 return;
1603 if (specs->attrs)
1605 warning_at (c_parser_peek_token (parser)->location,
1606 OPT_Wattributes,
1607 "prefix attributes are ignored for implementations");
1608 specs->attrs = NULL_TREE;
1610 c_parser_objc_class_definition (parser, NULL_TREE);
1611 return;
1613 break;
1614 case RID_AT_PROTOCOL:
1616 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1617 return;
1618 c_parser_objc_protocol_definition (parser, specs->attrs);
1619 return;
1621 break;
1622 case RID_AT_ALIAS:
1623 case RID_AT_CLASS:
1624 case RID_AT_END:
1625 case RID_AT_PROPERTY:
1626 if (specs->attrs)
1628 c_parser_error (parser, "unexpected attribute");
1629 specs->attrs = NULL;
1631 break;
1632 default:
1633 break;
1637 pending_xref_error ();
1638 prefix_attrs = specs->attrs;
1639 all_prefix_attrs = prefix_attrs;
1640 specs->attrs = NULL_TREE;
1641 while (true)
1643 struct c_declarator *declarator;
1644 bool dummy = false;
1645 timevar_id_t tv;
1646 tree fnbody;
1647 /* Declaring either one or more declarators (in which case we
1648 should diagnose if there were no declaration specifiers) or a
1649 function definition (in which case the diagnostic for
1650 implicit int suffices). */
1651 declarator = c_parser_declarator (parser,
1652 specs->typespec_kind != ctsk_none,
1653 C_DTR_NORMAL, &dummy);
1654 if (declarator == NULL)
1656 if (omp_declare_simd_clauses.exists ()
1657 || !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
1658 c_finish_omp_declare_simd (parser, NULL_TREE, NULL_TREE,
1659 omp_declare_simd_clauses);
1660 c_parser_skip_to_end_of_block_or_statement (parser);
1661 return;
1663 if (auto_type_p && declarator->kind != cdk_id)
1665 error_at (here,
1666 "%<__auto_type%> requires a plain identifier"
1667 " as declarator");
1668 c_parser_skip_to_end_of_block_or_statement (parser);
1669 return;
1671 if (c_parser_next_token_is (parser, CPP_EQ)
1672 || c_parser_next_token_is (parser, CPP_COMMA)
1673 || c_parser_next_token_is (parser, CPP_SEMICOLON)
1674 || c_parser_next_token_is_keyword (parser, RID_ASM)
1675 || c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE)
1676 || c_parser_next_token_is_keyword (parser, RID_IN))
1678 tree asm_name = NULL_TREE;
1679 tree postfix_attrs = NULL_TREE;
1680 if (!diagnosed_no_specs && !specs->declspecs_seen_p)
1682 diagnosed_no_specs = true;
1683 pedwarn (here, 0, "data definition has no type or storage class");
1685 /* Having seen a data definition, there cannot now be a
1686 function definition. */
1687 fndef_ok = false;
1688 if (c_parser_next_token_is_keyword (parser, RID_ASM))
1689 asm_name = c_parser_simple_asm_expr (parser);
1690 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
1691 postfix_attrs = c_parser_attributes (parser);
1692 if (c_parser_next_token_is (parser, CPP_EQ))
1694 tree d;
1695 struct c_expr init;
1696 location_t init_loc;
1697 c_parser_consume_token (parser);
1698 if (auto_type_p)
1700 start_init (NULL_TREE, asm_name, global_bindings_p ());
1701 init_loc = c_parser_peek_token (parser)->location;
1702 init = c_parser_expr_no_commas (parser, NULL);
1703 if (TREE_CODE (init.value) == COMPONENT_REF
1704 && DECL_C_BIT_FIELD (TREE_OPERAND (init.value, 1)))
1705 error_at (here,
1706 "%<__auto_type%> used with a bit-field"
1707 " initializer");
1708 init = convert_lvalue_to_rvalue (init_loc, init, true, true);
1709 tree init_type = TREE_TYPE (init.value);
1710 /* As with typeof, remove all qualifiers from atomic types. */
1711 if (init_type != error_mark_node && TYPE_ATOMIC (init_type))
1712 init_type
1713 = c_build_qualified_type (init_type, TYPE_UNQUALIFIED);
1714 bool vm_type = variably_modified_type_p (init_type,
1715 NULL_TREE);
1716 if (vm_type)
1717 init.value = c_save_expr (init.value);
1718 finish_init ();
1719 specs->typespec_kind = ctsk_typeof;
1720 specs->locations[cdw_typedef] = init_loc;
1721 specs->typedef_p = true;
1722 specs->type = init_type;
1723 if (vm_type)
1725 bool maybe_const = true;
1726 tree type_expr = c_fully_fold (init.value, false,
1727 &maybe_const);
1728 specs->expr_const_operands &= maybe_const;
1729 if (specs->expr)
1730 specs->expr = build2 (COMPOUND_EXPR,
1731 TREE_TYPE (type_expr),
1732 specs->expr, type_expr);
1733 else
1734 specs->expr = type_expr;
1736 d = start_decl (declarator, specs, true,
1737 chainon (postfix_attrs, all_prefix_attrs));
1738 if (!d)
1739 d = error_mark_node;
1740 if (omp_declare_simd_clauses.exists ()
1741 || !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
1742 c_finish_omp_declare_simd (parser, d, NULL_TREE,
1743 omp_declare_simd_clauses);
1745 else
1747 /* The declaration of the variable is in effect while
1748 its initializer is parsed. */
1749 d = start_decl (declarator, specs, true,
1750 chainon (postfix_attrs, all_prefix_attrs));
1751 if (!d)
1752 d = error_mark_node;
1753 if (omp_declare_simd_clauses.exists ()
1754 || !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
1755 c_finish_omp_declare_simd (parser, d, NULL_TREE,
1756 omp_declare_simd_clauses);
1757 start_init (d, asm_name, global_bindings_p ());
1758 init_loc = c_parser_peek_token (parser)->location;
1759 init = c_parser_initializer (parser);
1760 finish_init ();
1762 if (d != error_mark_node)
1764 maybe_warn_string_init (TREE_TYPE (d), init);
1765 finish_decl (d, init_loc, init.value,
1766 init.original_type, asm_name);
1769 else
1771 if (auto_type_p)
1773 error_at (here,
1774 "%<__auto_type%> requires an initialized "
1775 "data declaration");
1776 c_parser_skip_to_end_of_block_or_statement (parser);
1777 return;
1779 tree d = start_decl (declarator, specs, false,
1780 chainon (postfix_attrs,
1781 all_prefix_attrs));
1782 if (omp_declare_simd_clauses.exists ()
1783 || !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
1785 tree parms = NULL_TREE;
1786 if (d && TREE_CODE (d) == FUNCTION_DECL)
1788 struct c_declarator *ce = declarator;
1789 while (ce != NULL)
1790 if (ce->kind == cdk_function)
1792 parms = ce->u.arg_info->parms;
1793 break;
1795 else
1796 ce = ce->declarator;
1798 if (parms)
1799 temp_store_parm_decls (d, parms);
1800 c_finish_omp_declare_simd (parser, d, parms,
1801 omp_declare_simd_clauses);
1802 if (parms)
1803 temp_pop_parm_decls ();
1805 if (d)
1806 finish_decl (d, UNKNOWN_LOCATION, NULL_TREE,
1807 NULL_TREE, asm_name);
1809 if (c_parser_next_token_is_keyword (parser, RID_IN))
1811 if (d)
1812 *objc_foreach_object_declaration = d;
1813 else
1814 *objc_foreach_object_declaration = error_mark_node;
1817 if (c_parser_next_token_is (parser, CPP_COMMA))
1819 if (auto_type_p)
1821 error_at (here,
1822 "%<__auto_type%> may only be used with"
1823 " a single declarator");
1824 c_parser_skip_to_end_of_block_or_statement (parser);
1825 return;
1827 c_parser_consume_token (parser);
1828 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
1829 all_prefix_attrs = chainon (c_parser_attributes (parser),
1830 prefix_attrs);
1831 else
1832 all_prefix_attrs = prefix_attrs;
1833 continue;
1835 else if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1837 c_parser_consume_token (parser);
1838 return;
1840 else if (c_parser_next_token_is_keyword (parser, RID_IN))
1842 /* This can only happen in Objective-C: we found the
1843 'in' that terminates the declaration inside an
1844 Objective-C foreach statement. Do not consume the
1845 token, so that the caller can use it to determine
1846 that this indeed is a foreach context. */
1847 return;
1849 else
1851 c_parser_error (parser, "expected %<,%> or %<;%>");
1852 c_parser_skip_to_end_of_block_or_statement (parser);
1853 return;
1856 else if (auto_type_p)
1858 error_at (here,
1859 "%<__auto_type%> requires an initialized data declaration");
1860 c_parser_skip_to_end_of_block_or_statement (parser);
1861 return;
1863 else if (!fndef_ok)
1865 c_parser_error (parser, "expected %<=%>, %<,%>, %<;%>, "
1866 "%<asm%> or %<__attribute__%>");
1867 c_parser_skip_to_end_of_block_or_statement (parser);
1868 return;
1870 /* Function definition (nested or otherwise). */
1871 if (nested)
1873 pedwarn (here, OPT_Wpedantic, "ISO C forbids nested functions");
1874 c_push_function_context ();
1876 if (!start_function (specs, declarator, all_prefix_attrs))
1878 /* This can appear in many cases looking nothing like a
1879 function definition, so we don't give a more specific
1880 error suggesting there was one. */
1881 c_parser_error (parser, "expected %<=%>, %<,%>, %<;%>, %<asm%> "
1882 "or %<__attribute__%>");
1883 if (nested)
1884 c_pop_function_context ();
1885 break;
1888 if (DECL_DECLARED_INLINE_P (current_function_decl))
1889 tv = TV_PARSE_INLINE;
1890 else
1891 tv = TV_PARSE_FUNC;
1892 timevar_push (tv);
1894 /* Parse old-style parameter declarations. ??? Attributes are
1895 not allowed to start declaration specifiers here because of a
1896 syntax conflict between a function declaration with attribute
1897 suffix and a function definition with an attribute prefix on
1898 first old-style parameter declaration. Following the old
1899 parser, they are not accepted on subsequent old-style
1900 parameter declarations either. However, there is no
1901 ambiguity after the first declaration, nor indeed on the
1902 first as long as we don't allow postfix attributes after a
1903 declarator with a nonempty identifier list in a definition;
1904 and postfix attributes have never been accepted here in
1905 function definitions either. */
1906 while (c_parser_next_token_is_not (parser, CPP_EOF)
1907 && c_parser_next_token_is_not (parser, CPP_OPEN_BRACE))
1908 c_parser_declaration_or_fndef (parser, false, false, false,
1909 true, false, NULL, vNULL);
1910 store_parm_decls ();
1911 if (omp_declare_simd_clauses.exists ()
1912 || !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
1913 c_finish_omp_declare_simd (parser, current_function_decl, NULL_TREE,
1914 omp_declare_simd_clauses);
1915 DECL_STRUCT_FUNCTION (current_function_decl)->function_start_locus
1916 = c_parser_peek_token (parser)->location;
1917 fnbody = c_parser_compound_statement (parser);
1918 if (flag_cilkplus && contains_array_notation_expr (fnbody))
1919 fnbody = expand_array_notation_exprs (fnbody);
1920 if (nested)
1922 tree decl = current_function_decl;
1923 /* Mark nested functions as needing static-chain initially.
1924 lower_nested_functions will recompute it but the
1925 DECL_STATIC_CHAIN flag is also used before that happens,
1926 by initializer_constant_valid_p. See gcc.dg/nested-fn-2.c. */
1927 DECL_STATIC_CHAIN (decl) = 1;
1928 add_stmt (fnbody);
1929 finish_function ();
1930 c_pop_function_context ();
1931 add_stmt (build_stmt (DECL_SOURCE_LOCATION (decl), DECL_EXPR, decl));
1933 else
1935 add_stmt (fnbody);
1936 finish_function ();
1939 timevar_pop (tv);
1940 break;
1944 /* Parse an asm-definition (asm() outside a function body). This is a
1945 GNU extension.
1947 asm-definition:
1948 simple-asm-expr ;
1951 static void
1952 c_parser_asm_definition (c_parser *parser)
1954 tree asm_str = c_parser_simple_asm_expr (parser);
1955 if (asm_str)
1956 add_asm_node (asm_str);
1957 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
1960 /* Parse a static assertion (C11 6.7.10).
1962 static_assert-declaration:
1963 static_assert-declaration-no-semi ;
1966 static void
1967 c_parser_static_assert_declaration (c_parser *parser)
1969 c_parser_static_assert_declaration_no_semi (parser);
1970 if (parser->error
1971 || !c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
1972 c_parser_skip_to_end_of_block_or_statement (parser);
1975 /* Parse a static assertion (C11 6.7.10), without the trailing
1976 semicolon.
1978 static_assert-declaration-no-semi:
1979 _Static_assert ( constant-expression , string-literal )
1982 static void
1983 c_parser_static_assert_declaration_no_semi (c_parser *parser)
1985 location_t assert_loc, value_loc;
1986 tree value;
1987 tree string;
1989 gcc_assert (c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT));
1990 assert_loc = c_parser_peek_token (parser)->location;
1991 if (!flag_isoc11)
1993 if (flag_isoc99)
1994 pedwarn (assert_loc, OPT_Wpedantic,
1995 "ISO C99 does not support %<_Static_assert%>");
1996 else
1997 pedwarn (assert_loc, OPT_Wpedantic,
1998 "ISO C90 does not support %<_Static_assert%>");
2000 c_parser_consume_token (parser);
2001 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2002 return;
2003 value_loc = c_parser_peek_token (parser)->location;
2004 value = c_parser_expr_no_commas (parser, NULL).value;
2005 parser->lex_untranslated_string = true;
2006 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
2008 parser->lex_untranslated_string = false;
2009 return;
2011 switch (c_parser_peek_token (parser)->type)
2013 case CPP_STRING:
2014 case CPP_STRING16:
2015 case CPP_STRING32:
2016 case CPP_WSTRING:
2017 case CPP_UTF8STRING:
2018 string = c_parser_peek_token (parser)->value;
2019 c_parser_consume_token (parser);
2020 parser->lex_untranslated_string = false;
2021 break;
2022 default:
2023 c_parser_error (parser, "expected string literal");
2024 parser->lex_untranslated_string = false;
2025 return;
2027 c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
2029 if (!INTEGRAL_TYPE_P (TREE_TYPE (value)))
2031 error_at (value_loc, "expression in static assertion is not an integer");
2032 return;
2034 if (TREE_CODE (value) != INTEGER_CST)
2036 value = c_fully_fold (value, false, NULL);
2037 if (TREE_CODE (value) == INTEGER_CST)
2038 pedwarn (value_loc, OPT_Wpedantic, "expression in static assertion "
2039 "is not an integer constant expression");
2041 if (TREE_CODE (value) != INTEGER_CST)
2043 error_at (value_loc, "expression in static assertion is not constant");
2044 return;
2046 constant_expression_warning (value);
2047 if (integer_zerop (value))
2048 error_at (assert_loc, "static assertion failed: %E", string);
2051 /* Parse some declaration specifiers (possibly none) (C90 6.5, C99
2052 6.7), adding them to SPECS (which may already include some).
2053 Storage class specifiers are accepted iff SCSPEC_OK; type
2054 specifiers are accepted iff TYPESPEC_OK; alignment specifiers are
2055 accepted iff ALIGNSPEC_OK; attributes are accepted at the start
2056 iff START_ATTR_OK; __auto_type is accepted iff AUTO_TYPE_OK.
2058 declaration-specifiers:
2059 storage-class-specifier declaration-specifiers[opt]
2060 type-specifier declaration-specifiers[opt]
2061 type-qualifier declaration-specifiers[opt]
2062 function-specifier declaration-specifiers[opt]
2063 alignment-specifier declaration-specifiers[opt]
2065 Function specifiers (inline) are from C99, and are currently
2066 handled as storage class specifiers, as is __thread. Alignment
2067 specifiers are from C11.
2069 C90 6.5.1, C99 6.7.1:
2070 storage-class-specifier:
2071 typedef
2072 extern
2073 static
2074 auto
2075 register
2076 _Thread_local
2078 (_Thread_local is new in C11.)
2080 C99 6.7.4:
2081 function-specifier:
2082 inline
2083 _Noreturn
2085 (_Noreturn is new in C11.)
2087 C90 6.5.2, C99 6.7.2:
2088 type-specifier:
2089 void
2090 char
2091 short
2093 long
2094 float
2095 double
2096 signed
2097 unsigned
2098 _Bool
2099 _Complex
2100 [_Imaginary removed in C99 TC2]
2101 struct-or-union-specifier
2102 enum-specifier
2103 typedef-name
2104 atomic-type-specifier
2106 (_Bool and _Complex are new in C99.)
2107 (atomic-type-specifier is new in C11.)
2109 C90 6.5.3, C99 6.7.3:
2111 type-qualifier:
2112 const
2113 restrict
2114 volatile
2115 address-space-qualifier
2116 _Atomic
2118 (restrict is new in C99.)
2119 (_Atomic is new in C11.)
2121 GNU extensions:
2123 declaration-specifiers:
2124 attributes declaration-specifiers[opt]
2126 type-qualifier:
2127 address-space
2129 address-space:
2130 identifier recognized by the target
2132 storage-class-specifier:
2133 __thread
2135 type-specifier:
2136 typeof-specifier
2137 __auto_type
2138 __int128
2139 _Decimal32
2140 _Decimal64
2141 _Decimal128
2142 _Fract
2143 _Accum
2144 _Sat
2146 (_Fract, _Accum, and _Sat are new from ISO/IEC DTR 18037:
2147 http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1169.pdf)
2149 atomic-type-specifier
2150 _Atomic ( type-name )
2152 Objective-C:
2154 type-specifier:
2155 class-name objc-protocol-refs[opt]
2156 typedef-name objc-protocol-refs
2157 objc-protocol-refs
2160 static void
2161 c_parser_declspecs (c_parser *parser, struct c_declspecs *specs,
2162 bool scspec_ok, bool typespec_ok, bool start_attr_ok,
2163 bool alignspec_ok, bool auto_type_ok,
2164 enum c_lookahead_kind la)
2166 bool attrs_ok = start_attr_ok;
2167 bool seen_type = specs->typespec_kind != ctsk_none;
2169 if (!typespec_ok)
2170 gcc_assert (la == cla_prefer_id);
2172 while (c_parser_next_token_is (parser, CPP_NAME)
2173 || c_parser_next_token_is (parser, CPP_KEYWORD)
2174 || (c_dialect_objc () && c_parser_next_token_is (parser, CPP_LESS)))
2176 struct c_typespec t;
2177 tree attrs;
2178 tree align;
2179 location_t loc = c_parser_peek_token (parser)->location;
2181 /* If we cannot accept a type, exit if the next token must start
2182 one. Also, if we already have seen a tagged definition,
2183 a typename would be an error anyway and likely the user
2184 has simply forgotten a semicolon, so we exit. */
2185 if ((!typespec_ok || specs->typespec_kind == ctsk_tagdef)
2186 && c_parser_next_tokens_start_typename (parser, la)
2187 && !c_parser_next_token_is_qualifier (parser))
2188 break;
2190 if (c_parser_next_token_is (parser, CPP_NAME))
2192 c_token *name_token = c_parser_peek_token (parser);
2193 tree value = name_token->value;
2194 c_id_kind kind = name_token->id_kind;
2196 if (kind == C_ID_ADDRSPACE)
2198 addr_space_t as
2199 = name_token->keyword - RID_FIRST_ADDR_SPACE;
2200 declspecs_add_addrspace (name_token->location, specs, as);
2201 c_parser_consume_token (parser);
2202 attrs_ok = true;
2203 continue;
2206 gcc_assert (!c_parser_next_token_is_qualifier (parser));
2208 /* If we cannot accept a type, and the next token must start one,
2209 exit. Do the same if we already have seen a tagged definition,
2210 since it would be an error anyway and likely the user has simply
2211 forgotten a semicolon. */
2212 if (seen_type || !c_parser_next_tokens_start_typename (parser, la))
2213 break;
2215 /* Now at an unknown typename (C_ID_ID), a C_ID_TYPENAME or
2216 a C_ID_CLASSNAME. */
2217 c_parser_consume_token (parser);
2218 seen_type = true;
2219 attrs_ok = true;
2220 if (kind == C_ID_ID)
2222 error_at (loc, "unknown type name %qE", value);
2223 t.kind = ctsk_typedef;
2224 t.spec = error_mark_node;
2226 else if (kind == C_ID_TYPENAME
2227 && (!c_dialect_objc ()
2228 || c_parser_next_token_is_not (parser, CPP_LESS)))
2230 t.kind = ctsk_typedef;
2231 /* For a typedef name, record the meaning, not the name.
2232 In case of 'foo foo, bar;'. */
2233 t.spec = lookup_name (value);
2235 else
2237 tree proto = NULL_TREE;
2238 gcc_assert (c_dialect_objc ());
2239 t.kind = ctsk_objc;
2240 if (c_parser_next_token_is (parser, CPP_LESS))
2241 proto = c_parser_objc_protocol_refs (parser);
2242 t.spec = objc_get_protocol_qualified_type (value, proto);
2244 t.expr = NULL_TREE;
2245 t.expr_const_operands = true;
2246 declspecs_add_type (name_token->location, specs, t);
2247 continue;
2249 if (c_parser_next_token_is (parser, CPP_LESS))
2251 /* Make "<SomeProtocol>" equivalent to "id <SomeProtocol>" -
2252 nisse@lysator.liu.se. */
2253 tree proto;
2254 gcc_assert (c_dialect_objc ());
2255 if (!typespec_ok || seen_type)
2256 break;
2257 proto = c_parser_objc_protocol_refs (parser);
2258 t.kind = ctsk_objc;
2259 t.spec = objc_get_protocol_qualified_type (NULL_TREE, proto);
2260 t.expr = NULL_TREE;
2261 t.expr_const_operands = true;
2262 declspecs_add_type (loc, specs, t);
2263 continue;
2265 gcc_assert (c_parser_next_token_is (parser, CPP_KEYWORD));
2266 switch (c_parser_peek_token (parser)->keyword)
2268 case RID_STATIC:
2269 case RID_EXTERN:
2270 case RID_REGISTER:
2271 case RID_TYPEDEF:
2272 case RID_INLINE:
2273 case RID_NORETURN:
2274 case RID_AUTO:
2275 case RID_THREAD:
2276 if (!scspec_ok)
2277 goto out;
2278 attrs_ok = true;
2279 /* TODO: Distinguish between function specifiers (inline, noreturn)
2280 and storage class specifiers, either here or in
2281 declspecs_add_scspec. */
2282 declspecs_add_scspec (loc, specs,
2283 c_parser_peek_token (parser)->value);
2284 c_parser_consume_token (parser);
2285 break;
2286 case RID_AUTO_TYPE:
2287 if (!auto_type_ok)
2288 goto out;
2289 /* Fall through. */
2290 case RID_UNSIGNED:
2291 case RID_LONG:
2292 case RID_INT128:
2293 case RID_SHORT:
2294 case RID_SIGNED:
2295 case RID_COMPLEX:
2296 case RID_INT:
2297 case RID_CHAR:
2298 case RID_FLOAT:
2299 case RID_DOUBLE:
2300 case RID_VOID:
2301 case RID_DFLOAT32:
2302 case RID_DFLOAT64:
2303 case RID_DFLOAT128:
2304 case RID_BOOL:
2305 case RID_FRACT:
2306 case RID_ACCUM:
2307 case RID_SAT:
2308 if (!typespec_ok)
2309 goto out;
2310 attrs_ok = true;
2311 seen_type = true;
2312 if (c_dialect_objc ())
2313 parser->objc_need_raw_identifier = true;
2314 t.kind = ctsk_resword;
2315 t.spec = c_parser_peek_token (parser)->value;
2316 t.expr = NULL_TREE;
2317 t.expr_const_operands = true;
2318 declspecs_add_type (loc, specs, t);
2319 c_parser_consume_token (parser);
2320 break;
2321 case RID_ENUM:
2322 if (!typespec_ok)
2323 goto out;
2324 attrs_ok = true;
2325 seen_type = true;
2326 t = c_parser_enum_specifier (parser);
2327 declspecs_add_type (loc, specs, t);
2328 break;
2329 case RID_STRUCT:
2330 case RID_UNION:
2331 if (!typespec_ok)
2332 goto out;
2333 attrs_ok = true;
2334 seen_type = true;
2335 t = c_parser_struct_or_union_specifier (parser);
2336 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, t.spec);
2337 declspecs_add_type (loc, specs, t);
2338 break;
2339 case RID_TYPEOF:
2340 /* ??? The old parser rejected typeof after other type
2341 specifiers, but is a syntax error the best way of
2342 handling this? */
2343 if (!typespec_ok || seen_type)
2344 goto out;
2345 attrs_ok = true;
2346 seen_type = true;
2347 t = c_parser_typeof_specifier (parser);
2348 declspecs_add_type (loc, specs, t);
2349 break;
2350 case RID_ATOMIC:
2351 /* C parser handling of Objective-C constructs needs
2352 checking for correct lvalue-to-rvalue conversions, and
2353 the code in build_modify_expr handling various
2354 Objective-C cases, and that in build_unary_op handling
2355 Objective-C cases for increment / decrement, also needs
2356 updating; uses of TYPE_MAIN_VARIANT in objc_compare_types
2357 and objc_types_are_equivalent may also need updates. */
2358 if (c_dialect_objc ())
2359 sorry ("%<_Atomic%> in Objective-C");
2360 /* C parser handling of OpenMP constructs needs checking for
2361 correct lvalue-to-rvalue conversions. */
2362 if (flag_openmp)
2363 sorry ("%<_Atomic%> with OpenMP");
2364 if (!flag_isoc11)
2366 if (flag_isoc99)
2367 pedwarn (loc, OPT_Wpedantic,
2368 "ISO C99 does not support the %<_Atomic%> qualifier");
2369 else
2370 pedwarn (loc, OPT_Wpedantic,
2371 "ISO C90 does not support the %<_Atomic%> qualifier");
2373 attrs_ok = true;
2374 tree value;
2375 value = c_parser_peek_token (parser)->value;
2376 c_parser_consume_token (parser);
2377 if (typespec_ok && c_parser_next_token_is (parser, CPP_OPEN_PAREN))
2379 /* _Atomic ( type-name ). */
2380 seen_type = true;
2381 c_parser_consume_token (parser);
2382 struct c_type_name *type = c_parser_type_name (parser);
2383 t.kind = ctsk_typeof;
2384 t.spec = error_mark_node;
2385 t.expr = NULL_TREE;
2386 t.expr_const_operands = true;
2387 if (type != NULL)
2388 t.spec = groktypename (type, &t.expr,
2389 &t.expr_const_operands);
2390 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2391 "expected %<)%>");
2392 if (t.spec != error_mark_node)
2394 if (TREE_CODE (t.spec) == ARRAY_TYPE)
2395 error_at (loc, "%<_Atomic%>-qualified array type");
2396 else if (TREE_CODE (t.spec) == FUNCTION_TYPE)
2397 error_at (loc, "%<_Atomic%>-qualified function type");
2398 else if (TYPE_QUALS (t.spec) != TYPE_UNQUALIFIED)
2399 error_at (loc, "%<_Atomic%> applied to a qualified type");
2400 else
2401 t.spec = c_build_qualified_type (t.spec, TYPE_QUAL_ATOMIC);
2403 declspecs_add_type (loc, specs, t);
2405 else
2406 declspecs_add_qual (loc, specs, value);
2407 break;
2408 case RID_CONST:
2409 case RID_VOLATILE:
2410 case RID_RESTRICT:
2411 attrs_ok = true;
2412 declspecs_add_qual (loc, specs, c_parser_peek_token (parser)->value);
2413 c_parser_consume_token (parser);
2414 break;
2415 case RID_ATTRIBUTE:
2416 if (!attrs_ok)
2417 goto out;
2418 attrs = c_parser_attributes (parser);
2419 declspecs_add_attrs (loc, specs, attrs);
2420 break;
2421 case RID_ALIGNAS:
2422 if (!alignspec_ok)
2423 goto out;
2424 align = c_parser_alignas_specifier (parser);
2425 declspecs_add_alignas (loc, specs, align);
2426 break;
2427 default:
2428 goto out;
2431 out: ;
2434 /* Parse an enum specifier (C90 6.5.2.2, C99 6.7.2.2).
2436 enum-specifier:
2437 enum attributes[opt] identifier[opt] { enumerator-list } attributes[opt]
2438 enum attributes[opt] identifier[opt] { enumerator-list , } attributes[opt]
2439 enum attributes[opt] identifier
2441 The form with trailing comma is new in C99. The forms with
2442 attributes are GNU extensions. In GNU C, we accept any expression
2443 without commas in the syntax (assignment expressions, not just
2444 conditional expressions); assignment expressions will be diagnosed
2445 as non-constant.
2447 enumerator-list:
2448 enumerator
2449 enumerator-list , enumerator
2451 enumerator:
2452 enumeration-constant
2453 enumeration-constant = constant-expression
2456 static struct c_typespec
2457 c_parser_enum_specifier (c_parser *parser)
2459 struct c_typespec ret;
2460 tree attrs;
2461 tree ident = NULL_TREE;
2462 location_t enum_loc;
2463 location_t ident_loc = UNKNOWN_LOCATION; /* Quiet warning. */
2464 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ENUM));
2465 enum_loc = c_parser_peek_token (parser)->location;
2466 c_parser_consume_token (parser);
2467 attrs = c_parser_attributes (parser);
2468 enum_loc = c_parser_peek_token (parser)->location;
2469 /* Set the location in case we create a decl now. */
2470 c_parser_set_source_position_from_token (c_parser_peek_token (parser));
2471 if (c_parser_next_token_is (parser, CPP_NAME))
2473 ident = c_parser_peek_token (parser)->value;
2474 ident_loc = c_parser_peek_token (parser)->location;
2475 enum_loc = ident_loc;
2476 c_parser_consume_token (parser);
2478 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
2480 /* Parse an enum definition. */
2481 struct c_enum_contents the_enum;
2482 tree type;
2483 tree postfix_attrs;
2484 /* We chain the enumerators in reverse order, then put them in
2485 forward order at the end. */
2486 tree values;
2487 timevar_push (TV_PARSE_ENUM);
2488 type = start_enum (enum_loc, &the_enum, ident);
2489 values = NULL_TREE;
2490 c_parser_consume_token (parser);
2491 while (true)
2493 tree enum_id;
2494 tree enum_value;
2495 tree enum_decl;
2496 bool seen_comma;
2497 c_token *token;
2498 location_t comma_loc = UNKNOWN_LOCATION; /* Quiet warning. */
2499 location_t decl_loc, value_loc;
2500 if (c_parser_next_token_is_not (parser, CPP_NAME))
2502 c_parser_error (parser, "expected identifier");
2503 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
2504 values = error_mark_node;
2505 break;
2507 token = c_parser_peek_token (parser);
2508 enum_id = token->value;
2509 /* Set the location in case we create a decl now. */
2510 c_parser_set_source_position_from_token (token);
2511 decl_loc = value_loc = token->location;
2512 c_parser_consume_token (parser);
2513 if (c_parser_next_token_is (parser, CPP_EQ))
2515 c_parser_consume_token (parser);
2516 value_loc = c_parser_peek_token (parser)->location;
2517 enum_value = c_parser_expr_no_commas (parser, NULL).value;
2519 else
2520 enum_value = NULL_TREE;
2521 enum_decl = build_enumerator (decl_loc, value_loc,
2522 &the_enum, enum_id, enum_value);
2523 TREE_CHAIN (enum_decl) = values;
2524 values = enum_decl;
2525 seen_comma = false;
2526 if (c_parser_next_token_is (parser, CPP_COMMA))
2528 comma_loc = c_parser_peek_token (parser)->location;
2529 seen_comma = true;
2530 c_parser_consume_token (parser);
2532 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2534 if (seen_comma && !flag_isoc99)
2535 pedwarn (comma_loc, OPT_Wpedantic, "comma at end of enumerator list");
2536 c_parser_consume_token (parser);
2537 break;
2539 if (!seen_comma)
2541 c_parser_error (parser, "expected %<,%> or %<}%>");
2542 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
2543 values = error_mark_node;
2544 break;
2547 postfix_attrs = c_parser_attributes (parser);
2548 ret.spec = finish_enum (type, nreverse (values),
2549 chainon (attrs, postfix_attrs));
2550 ret.kind = ctsk_tagdef;
2551 ret.expr = NULL_TREE;
2552 ret.expr_const_operands = true;
2553 timevar_pop (TV_PARSE_ENUM);
2554 return ret;
2556 else if (!ident)
2558 c_parser_error (parser, "expected %<{%>");
2559 ret.spec = error_mark_node;
2560 ret.kind = ctsk_tagref;
2561 ret.expr = NULL_TREE;
2562 ret.expr_const_operands = true;
2563 return ret;
2565 ret = parser_xref_tag (ident_loc, ENUMERAL_TYPE, ident);
2566 /* In ISO C, enumerated types can be referred to only if already
2567 defined. */
2568 if (pedantic && !COMPLETE_TYPE_P (ret.spec))
2570 gcc_assert (ident);
2571 pedwarn (enum_loc, OPT_Wpedantic,
2572 "ISO C forbids forward references to %<enum%> types");
2574 return ret;
2577 /* Parse a struct or union specifier (C90 6.5.2.1, C99 6.7.2.1).
2579 struct-or-union-specifier:
2580 struct-or-union attributes[opt] identifier[opt]
2581 { struct-contents } attributes[opt]
2582 struct-or-union attributes[opt] identifier
2584 struct-contents:
2585 struct-declaration-list
2587 struct-declaration-list:
2588 struct-declaration ;
2589 struct-declaration-list struct-declaration ;
2591 GNU extensions:
2593 struct-contents:
2594 empty
2595 struct-declaration
2596 struct-declaration-list struct-declaration
2598 struct-declaration-list:
2599 struct-declaration-list ;
2602 (Note that in the syntax here, unlike that in ISO C, the semicolons
2603 are included here rather than in struct-declaration, in order to
2604 describe the syntax with extra semicolons and missing semicolon at
2605 end.)
2607 Objective-C:
2609 struct-declaration-list:
2610 @defs ( class-name )
2612 (Note this does not include a trailing semicolon, but can be
2613 followed by further declarations, and gets a pedwarn-if-pedantic
2614 when followed by a semicolon.) */
2616 static struct c_typespec
2617 c_parser_struct_or_union_specifier (c_parser *parser)
2619 struct c_typespec ret;
2620 tree attrs;
2621 tree ident = NULL_TREE;
2622 location_t struct_loc;
2623 location_t ident_loc = UNKNOWN_LOCATION;
2624 enum tree_code code;
2625 switch (c_parser_peek_token (parser)->keyword)
2627 case RID_STRUCT:
2628 code = RECORD_TYPE;
2629 break;
2630 case RID_UNION:
2631 code = UNION_TYPE;
2632 break;
2633 default:
2634 gcc_unreachable ();
2636 struct_loc = c_parser_peek_token (parser)->location;
2637 c_parser_consume_token (parser);
2638 attrs = c_parser_attributes (parser);
2640 /* Set the location in case we create a decl now. */
2641 c_parser_set_source_position_from_token (c_parser_peek_token (parser));
2643 if (c_parser_next_token_is (parser, CPP_NAME))
2645 ident = c_parser_peek_token (parser)->value;
2646 ident_loc = c_parser_peek_token (parser)->location;
2647 struct_loc = ident_loc;
2648 c_parser_consume_token (parser);
2650 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
2652 /* Parse a struct or union definition. Start the scope of the
2653 tag before parsing components. */
2654 struct c_struct_parse_info *struct_info;
2655 tree type = start_struct (struct_loc, code, ident, &struct_info);
2656 tree postfix_attrs;
2657 /* We chain the components in reverse order, then put them in
2658 forward order at the end. Each struct-declaration may
2659 declare multiple components (comma-separated), so we must use
2660 chainon to join them, although when parsing each
2661 struct-declaration we can use TREE_CHAIN directly.
2663 The theory behind all this is that there will be more
2664 semicolon separated fields than comma separated fields, and
2665 so we'll be minimizing the number of node traversals required
2666 by chainon. */
2667 tree contents;
2668 timevar_push (TV_PARSE_STRUCT);
2669 contents = NULL_TREE;
2670 c_parser_consume_token (parser);
2671 /* Handle the Objective-C @defs construct,
2672 e.g. foo(sizeof(struct{ @defs(ClassName) }));. */
2673 if (c_parser_next_token_is_keyword (parser, RID_AT_DEFS))
2675 tree name;
2676 gcc_assert (c_dialect_objc ());
2677 c_parser_consume_token (parser);
2678 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2679 goto end_at_defs;
2680 if (c_parser_next_token_is (parser, CPP_NAME)
2681 && c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME)
2683 name = c_parser_peek_token (parser)->value;
2684 c_parser_consume_token (parser);
2686 else
2688 c_parser_error (parser, "expected class name");
2689 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
2690 goto end_at_defs;
2692 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2693 "expected %<)%>");
2694 contents = nreverse (objc_get_class_ivars (name));
2696 end_at_defs:
2697 /* Parse the struct-declarations and semicolons. Problems with
2698 semicolons are diagnosed here; empty structures are diagnosed
2699 elsewhere. */
2700 while (true)
2702 tree decls;
2703 /* Parse any stray semicolon. */
2704 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
2706 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
2707 "extra semicolon in struct or union specified");
2708 c_parser_consume_token (parser);
2709 continue;
2711 /* Stop if at the end of the struct or union contents. */
2712 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2714 c_parser_consume_token (parser);
2715 break;
2717 /* Accept #pragmas at struct scope. */
2718 if (c_parser_next_token_is (parser, CPP_PRAGMA))
2720 c_parser_pragma (parser, pragma_struct);
2721 continue;
2723 /* Parse some comma-separated declarations, but not the
2724 trailing semicolon if any. */
2725 decls = c_parser_struct_declaration (parser);
2726 contents = chainon (decls, contents);
2727 /* If no semicolon follows, either we have a parse error or
2728 are at the end of the struct or union and should
2729 pedwarn. */
2730 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
2731 c_parser_consume_token (parser);
2732 else
2734 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2735 pedwarn (c_parser_peek_token (parser)->location, 0,
2736 "no semicolon at end of struct or union");
2737 else if (parser->error
2738 || !c_parser_next_token_starts_declspecs (parser))
2740 c_parser_error (parser, "expected %<;%>");
2741 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
2742 break;
2745 /* If we come here, we have already emitted an error
2746 for an expected `;', identifier or `(', and we also
2747 recovered already. Go on with the next field. */
2750 postfix_attrs = c_parser_attributes (parser);
2751 ret.spec = finish_struct (struct_loc, type, nreverse (contents),
2752 chainon (attrs, postfix_attrs), struct_info);
2753 ret.kind = ctsk_tagdef;
2754 ret.expr = NULL_TREE;
2755 ret.expr_const_operands = true;
2756 timevar_pop (TV_PARSE_STRUCT);
2757 return ret;
2759 else if (!ident)
2761 c_parser_error (parser, "expected %<{%>");
2762 ret.spec = error_mark_node;
2763 ret.kind = ctsk_tagref;
2764 ret.expr = NULL_TREE;
2765 ret.expr_const_operands = true;
2766 return ret;
2768 ret = parser_xref_tag (ident_loc, code, ident);
2769 return ret;
2772 /* Parse a struct-declaration (C90 6.5.2.1, C99 6.7.2.1), *without*
2773 the trailing semicolon.
2775 struct-declaration:
2776 specifier-qualifier-list struct-declarator-list
2777 static_assert-declaration-no-semi
2779 specifier-qualifier-list:
2780 type-specifier specifier-qualifier-list[opt]
2781 type-qualifier specifier-qualifier-list[opt]
2782 attributes specifier-qualifier-list[opt]
2784 struct-declarator-list:
2785 struct-declarator
2786 struct-declarator-list , attributes[opt] struct-declarator
2788 struct-declarator:
2789 declarator attributes[opt]
2790 declarator[opt] : constant-expression attributes[opt]
2792 GNU extensions:
2794 struct-declaration:
2795 __extension__ struct-declaration
2796 specifier-qualifier-list
2798 Unlike the ISO C syntax, semicolons are handled elsewhere. The use
2799 of attributes where shown is a GNU extension. In GNU C, we accept
2800 any expression without commas in the syntax (assignment
2801 expressions, not just conditional expressions); assignment
2802 expressions will be diagnosed as non-constant. */
2804 static tree
2805 c_parser_struct_declaration (c_parser *parser)
2807 struct c_declspecs *specs;
2808 tree prefix_attrs;
2809 tree all_prefix_attrs;
2810 tree decls;
2811 location_t decl_loc;
2812 if (c_parser_next_token_is_keyword (parser, RID_EXTENSION))
2814 int ext;
2815 tree decl;
2816 ext = disable_extension_diagnostics ();
2817 c_parser_consume_token (parser);
2818 decl = c_parser_struct_declaration (parser);
2819 restore_extension_diagnostics (ext);
2820 return decl;
2822 if (c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT))
2824 c_parser_static_assert_declaration_no_semi (parser);
2825 return NULL_TREE;
2827 specs = build_null_declspecs ();
2828 decl_loc = c_parser_peek_token (parser)->location;
2829 /* Strictly by the standard, we shouldn't allow _Alignas here,
2830 but it appears to have been intended to allow it there, so
2831 we're keeping it as it is until WG14 reaches a conclusion
2832 of N1731.
2833 <http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1731.pdf> */
2834 c_parser_declspecs (parser, specs, false, true, true,
2835 true, false, cla_nonabstract_decl);
2836 if (parser->error)
2837 return NULL_TREE;
2838 if (!specs->declspecs_seen_p)
2840 c_parser_error (parser, "expected specifier-qualifier-list");
2841 return NULL_TREE;
2843 finish_declspecs (specs);
2844 if (c_parser_next_token_is (parser, CPP_SEMICOLON)
2845 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2847 tree ret;
2848 if (specs->typespec_kind == ctsk_none)
2850 pedwarn (decl_loc, OPT_Wpedantic,
2851 "ISO C forbids member declarations with no members");
2852 shadow_tag_warned (specs, pedantic);
2853 ret = NULL_TREE;
2855 else
2857 /* Support for unnamed structs or unions as members of
2858 structs or unions (which is [a] useful and [b] supports
2859 MS P-SDK). */
2860 tree attrs = NULL;
2862 ret = grokfield (c_parser_peek_token (parser)->location,
2863 build_id_declarator (NULL_TREE), specs,
2864 NULL_TREE, &attrs);
2865 if (ret)
2866 decl_attributes (&ret, attrs, 0);
2868 return ret;
2871 /* Provide better error recovery. Note that a type name here is valid,
2872 and will be treated as a field name. */
2873 if (specs->typespec_kind == ctsk_tagdef
2874 && TREE_CODE (specs->type) != ENUMERAL_TYPE
2875 && c_parser_next_token_starts_declspecs (parser)
2876 && !c_parser_next_token_is (parser, CPP_NAME))
2878 c_parser_error (parser, "expected %<;%>, identifier or %<(%>");
2879 parser->error = false;
2880 return NULL_TREE;
2883 pending_xref_error ();
2884 prefix_attrs = specs->attrs;
2885 all_prefix_attrs = prefix_attrs;
2886 specs->attrs = NULL_TREE;
2887 decls = NULL_TREE;
2888 while (true)
2890 /* Declaring one or more declarators or un-named bit-fields. */
2891 struct c_declarator *declarator;
2892 bool dummy = false;
2893 if (c_parser_next_token_is (parser, CPP_COLON))
2894 declarator = build_id_declarator (NULL_TREE);
2895 else
2896 declarator = c_parser_declarator (parser,
2897 specs->typespec_kind != ctsk_none,
2898 C_DTR_NORMAL, &dummy);
2899 if (declarator == NULL)
2901 c_parser_skip_to_end_of_block_or_statement (parser);
2902 break;
2904 if (c_parser_next_token_is (parser, CPP_COLON)
2905 || c_parser_next_token_is (parser, CPP_COMMA)
2906 || c_parser_next_token_is (parser, CPP_SEMICOLON)
2907 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE)
2908 || c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2910 tree postfix_attrs = NULL_TREE;
2911 tree width = NULL_TREE;
2912 tree d;
2913 if (c_parser_next_token_is (parser, CPP_COLON))
2915 c_parser_consume_token (parser);
2916 width = c_parser_expr_no_commas (parser, NULL).value;
2918 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2919 postfix_attrs = c_parser_attributes (parser);
2920 d = grokfield (c_parser_peek_token (parser)->location,
2921 declarator, specs, width, &all_prefix_attrs);
2922 decl_attributes (&d, chainon (postfix_attrs,
2923 all_prefix_attrs), 0);
2924 DECL_CHAIN (d) = decls;
2925 decls = d;
2926 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2927 all_prefix_attrs = chainon (c_parser_attributes (parser),
2928 prefix_attrs);
2929 else
2930 all_prefix_attrs = prefix_attrs;
2931 if (c_parser_next_token_is (parser, CPP_COMMA))
2932 c_parser_consume_token (parser);
2933 else if (c_parser_next_token_is (parser, CPP_SEMICOLON)
2934 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2936 /* Semicolon consumed in caller. */
2937 break;
2939 else
2941 c_parser_error (parser, "expected %<,%>, %<;%> or %<}%>");
2942 break;
2945 else
2947 c_parser_error (parser,
2948 "expected %<:%>, %<,%>, %<;%>, %<}%> or "
2949 "%<__attribute__%>");
2950 break;
2953 return decls;
2956 /* Parse a typeof specifier (a GNU extension).
2958 typeof-specifier:
2959 typeof ( expression )
2960 typeof ( type-name )
2963 static struct c_typespec
2964 c_parser_typeof_specifier (c_parser *parser)
2966 struct c_typespec ret;
2967 ret.kind = ctsk_typeof;
2968 ret.spec = error_mark_node;
2969 ret.expr = NULL_TREE;
2970 ret.expr_const_operands = true;
2971 gcc_assert (c_parser_next_token_is_keyword (parser, RID_TYPEOF));
2972 c_parser_consume_token (parser);
2973 c_inhibit_evaluation_warnings++;
2974 in_typeof++;
2975 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2977 c_inhibit_evaluation_warnings--;
2978 in_typeof--;
2979 return ret;
2981 if (c_parser_next_tokens_start_typename (parser, cla_prefer_id))
2983 struct c_type_name *type = c_parser_type_name (parser);
2984 c_inhibit_evaluation_warnings--;
2985 in_typeof--;
2986 if (type != NULL)
2988 ret.spec = groktypename (type, &ret.expr, &ret.expr_const_operands);
2989 pop_maybe_used (variably_modified_type_p (ret.spec, NULL_TREE));
2992 else
2994 bool was_vm;
2995 location_t here = c_parser_peek_token (parser)->location;
2996 struct c_expr expr = c_parser_expression (parser);
2997 c_inhibit_evaluation_warnings--;
2998 in_typeof--;
2999 if (TREE_CODE (expr.value) == COMPONENT_REF
3000 && DECL_C_BIT_FIELD (TREE_OPERAND (expr.value, 1)))
3001 error_at (here, "%<typeof%> applied to a bit-field");
3002 mark_exp_read (expr.value);
3003 ret.spec = TREE_TYPE (expr.value);
3004 was_vm = variably_modified_type_p (ret.spec, NULL_TREE);
3005 /* This is returned with the type so that when the type is
3006 evaluated, this can be evaluated. */
3007 if (was_vm)
3008 ret.expr = c_fully_fold (expr.value, false, &ret.expr_const_operands);
3009 pop_maybe_used (was_vm);
3010 /* For use in macros such as those in <stdatomic.h>, remove all
3011 qualifiers from atomic types. (const can be an issue for more macros
3012 using typeof than just the <stdatomic.h> ones.) */
3013 if (ret.spec != error_mark_node && TYPE_ATOMIC (ret.spec))
3014 ret.spec = c_build_qualified_type (ret.spec, TYPE_UNQUALIFIED);
3016 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
3017 return ret;
3020 /* Parse an alignment-specifier.
3022 C11 6.7.5:
3024 alignment-specifier:
3025 _Alignas ( type-name )
3026 _Alignas ( constant-expression )
3029 static tree
3030 c_parser_alignas_specifier (c_parser * parser)
3032 tree ret = error_mark_node;
3033 location_t loc = c_parser_peek_token (parser)->location;
3034 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ALIGNAS));
3035 c_parser_consume_token (parser);
3036 if (!flag_isoc11)
3038 if (flag_isoc99)
3039 pedwarn (loc, OPT_Wpedantic,
3040 "ISO C99 does not support %<_Alignas%>");
3041 else
3042 pedwarn (loc, OPT_Wpedantic,
3043 "ISO C90 does not support %<_Alignas%>");
3045 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3046 return ret;
3047 if (c_parser_next_tokens_start_typename (parser, cla_prefer_id))
3049 struct c_type_name *type = c_parser_type_name (parser);
3050 if (type != NULL)
3051 ret = c_sizeof_or_alignof_type (loc, groktypename (type, NULL, NULL),
3052 false, true, 1);
3054 else
3055 ret = c_parser_expr_no_commas (parser, NULL).value;
3056 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
3057 return ret;
3060 /* Parse a declarator, possibly an abstract declarator (C90 6.5.4,
3061 6.5.5, C99 6.7.5, 6.7.6). If TYPE_SEEN_P then a typedef name may
3062 be redeclared; otherwise it may not. KIND indicates which kind of
3063 declarator is wanted. Returns a valid declarator except in the
3064 case of a syntax error in which case NULL is returned. *SEEN_ID is
3065 set to true if an identifier being declared is seen; this is used
3066 to diagnose bad forms of abstract array declarators and to
3067 determine whether an identifier list is syntactically permitted.
3069 declarator:
3070 pointer[opt] direct-declarator
3072 direct-declarator:
3073 identifier
3074 ( attributes[opt] declarator )
3075 direct-declarator array-declarator
3076 direct-declarator ( parameter-type-list )
3077 direct-declarator ( identifier-list[opt] )
3079 pointer:
3080 * type-qualifier-list[opt]
3081 * type-qualifier-list[opt] pointer
3083 type-qualifier-list:
3084 type-qualifier
3085 attributes
3086 type-qualifier-list type-qualifier
3087 type-qualifier-list attributes
3089 array-declarator:
3090 [ type-qualifier-list[opt] assignment-expression[opt] ]
3091 [ static type-qualifier-list[opt] assignment-expression ]
3092 [ type-qualifier-list static assignment-expression ]
3093 [ type-qualifier-list[opt] * ]
3095 parameter-type-list:
3096 parameter-list
3097 parameter-list , ...
3099 parameter-list:
3100 parameter-declaration
3101 parameter-list , parameter-declaration
3103 parameter-declaration:
3104 declaration-specifiers declarator attributes[opt]
3105 declaration-specifiers abstract-declarator[opt] attributes[opt]
3107 identifier-list:
3108 identifier
3109 identifier-list , identifier
3111 abstract-declarator:
3112 pointer
3113 pointer[opt] direct-abstract-declarator
3115 direct-abstract-declarator:
3116 ( attributes[opt] abstract-declarator )
3117 direct-abstract-declarator[opt] array-declarator
3118 direct-abstract-declarator[opt] ( parameter-type-list[opt] )
3120 GNU extensions:
3122 direct-declarator:
3123 direct-declarator ( parameter-forward-declarations
3124 parameter-type-list[opt] )
3126 direct-abstract-declarator:
3127 direct-abstract-declarator[opt] ( parameter-forward-declarations
3128 parameter-type-list[opt] )
3130 parameter-forward-declarations:
3131 parameter-list ;
3132 parameter-forward-declarations parameter-list ;
3134 The uses of attributes shown above are GNU extensions.
3136 Some forms of array declarator are not included in C99 in the
3137 syntax for abstract declarators; these are disallowed elsewhere.
3138 This may be a defect (DR#289).
3140 This function also accepts an omitted abstract declarator as being
3141 an abstract declarator, although not part of the formal syntax. */
3143 static struct c_declarator *
3144 c_parser_declarator (c_parser *parser, bool type_seen_p, c_dtr_syn kind,
3145 bool *seen_id)
3147 /* Parse any initial pointer part. */
3148 if (c_parser_next_token_is (parser, CPP_MULT))
3150 struct c_declspecs *quals_attrs = build_null_declspecs ();
3151 struct c_declarator *inner;
3152 c_parser_consume_token (parser);
3153 c_parser_declspecs (parser, quals_attrs, false, false, true,
3154 false, false, cla_prefer_id);
3155 inner = c_parser_declarator (parser, type_seen_p, kind, seen_id);
3156 if (inner == NULL)
3157 return NULL;
3158 else
3159 return make_pointer_declarator (quals_attrs, inner);
3161 /* Now we have a direct declarator, direct abstract declarator or
3162 nothing (which counts as a direct abstract declarator here). */
3163 return c_parser_direct_declarator (parser, type_seen_p, kind, seen_id);
3166 /* Parse a direct declarator or direct abstract declarator; arguments
3167 as c_parser_declarator. */
3169 static struct c_declarator *
3170 c_parser_direct_declarator (c_parser *parser, bool type_seen_p, c_dtr_syn kind,
3171 bool *seen_id)
3173 /* The direct declarator must start with an identifier (possibly
3174 omitted) or a parenthesized declarator (possibly abstract). In
3175 an ordinary declarator, initial parentheses must start a
3176 parenthesized declarator. In an abstract declarator or parameter
3177 declarator, they could start a parenthesized declarator or a
3178 parameter list. To tell which, the open parenthesis and any
3179 following attributes must be read. If a declaration specifier
3180 follows, then it is a parameter list; if the specifier is a
3181 typedef name, there might be an ambiguity about redeclaring it,
3182 which is resolved in the direction of treating it as a typedef
3183 name. If a close parenthesis follows, it is also an empty
3184 parameter list, as the syntax does not permit empty abstract
3185 declarators. Otherwise, it is a parenthesized declarator (in
3186 which case the analysis may be repeated inside it, recursively).
3188 ??? There is an ambiguity in a parameter declaration "int
3189 (__attribute__((foo)) x)", where x is not a typedef name: it
3190 could be an abstract declarator for a function, or declare x with
3191 parentheses. The proper resolution of this ambiguity needs
3192 documenting. At present we follow an accident of the old
3193 parser's implementation, whereby the first parameter must have
3194 some declaration specifiers other than just attributes. Thus as
3195 a parameter declaration it is treated as a parenthesized
3196 parameter named x, and as an abstract declarator it is
3197 rejected.
3199 ??? Also following the old parser, attributes inside an empty
3200 parameter list are ignored, making it a list not yielding a
3201 prototype, rather than giving an error or making it have one
3202 parameter with implicit type int.
3204 ??? Also following the old parser, typedef names may be
3205 redeclared in declarators, but not Objective-C class names. */
3207 if (kind != C_DTR_ABSTRACT
3208 && c_parser_next_token_is (parser, CPP_NAME)
3209 && ((type_seen_p
3210 && (c_parser_peek_token (parser)->id_kind == C_ID_TYPENAME
3211 || c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME))
3212 || c_parser_peek_token (parser)->id_kind == C_ID_ID))
3214 struct c_declarator *inner
3215 = build_id_declarator (c_parser_peek_token (parser)->value);
3216 *seen_id = true;
3217 inner->id_loc = c_parser_peek_token (parser)->location;
3218 c_parser_consume_token (parser);
3219 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
3222 if (kind != C_DTR_NORMAL
3223 && c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
3225 struct c_declarator *inner = build_id_declarator (NULL_TREE);
3226 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
3229 /* Either we are at the end of an abstract declarator, or we have
3230 parentheses. */
3232 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
3234 tree attrs;
3235 struct c_declarator *inner;
3236 c_parser_consume_token (parser);
3237 attrs = c_parser_attributes (parser);
3238 if (kind != C_DTR_NORMAL
3239 && (c_parser_next_token_starts_declspecs (parser)
3240 || c_parser_next_token_is (parser, CPP_CLOSE_PAREN)))
3242 struct c_arg_info *args
3243 = c_parser_parms_declarator (parser, kind == C_DTR_NORMAL,
3244 attrs);
3245 if (args == NULL)
3246 return NULL;
3247 else
3249 inner
3250 = build_function_declarator (args,
3251 build_id_declarator (NULL_TREE));
3252 return c_parser_direct_declarator_inner (parser, *seen_id,
3253 inner);
3256 /* A parenthesized declarator. */
3257 inner = c_parser_declarator (parser, type_seen_p, kind, seen_id);
3258 if (inner != NULL && attrs != NULL)
3259 inner = build_attrs_declarator (attrs, inner);
3260 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3262 c_parser_consume_token (parser);
3263 if (inner == NULL)
3264 return NULL;
3265 else
3266 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
3268 else
3270 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3271 "expected %<)%>");
3272 return NULL;
3275 else
3277 if (kind == C_DTR_NORMAL)
3279 c_parser_error (parser, "expected identifier or %<(%>");
3280 return NULL;
3282 else
3283 return build_id_declarator (NULL_TREE);
3287 /* Parse part of a direct declarator or direct abstract declarator,
3288 given that some (in INNER) has already been parsed; ID_PRESENT is
3289 true if an identifier is present, false for an abstract
3290 declarator. */
3292 static struct c_declarator *
3293 c_parser_direct_declarator_inner (c_parser *parser, bool id_present,
3294 struct c_declarator *inner)
3296 /* Parse a sequence of array declarators and parameter lists. */
3297 if (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
3299 location_t brace_loc = c_parser_peek_token (parser)->location;
3300 struct c_declarator *declarator;
3301 struct c_declspecs *quals_attrs = build_null_declspecs ();
3302 bool static_seen;
3303 bool star_seen;
3304 struct c_expr dimen;
3305 dimen.value = NULL_TREE;
3306 dimen.original_code = ERROR_MARK;
3307 dimen.original_type = NULL_TREE;
3308 c_parser_consume_token (parser);
3309 c_parser_declspecs (parser, quals_attrs, false, false, true,
3310 false, false, cla_prefer_id);
3311 static_seen = c_parser_next_token_is_keyword (parser, RID_STATIC);
3312 if (static_seen)
3313 c_parser_consume_token (parser);
3314 if (static_seen && !quals_attrs->declspecs_seen_p)
3315 c_parser_declspecs (parser, quals_attrs, false, false, true,
3316 false, false, cla_prefer_id);
3317 if (!quals_attrs->declspecs_seen_p)
3318 quals_attrs = NULL;
3319 /* If "static" is present, there must be an array dimension.
3320 Otherwise, there may be a dimension, "*", or no
3321 dimension. */
3322 if (static_seen)
3324 star_seen = false;
3325 dimen = c_parser_expr_no_commas (parser, NULL);
3327 else
3329 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
3331 dimen.value = NULL_TREE;
3332 star_seen = false;
3334 else if (flag_cilkplus
3335 && c_parser_next_token_is (parser, CPP_COLON))
3337 dimen.value = error_mark_node;
3338 star_seen = false;
3339 error_at (c_parser_peek_token (parser)->location,
3340 "array notations cannot be used in declaration");
3341 c_parser_consume_token (parser);
3343 else if (c_parser_next_token_is (parser, CPP_MULT))
3345 if (c_parser_peek_2nd_token (parser)->type == CPP_CLOSE_SQUARE)
3347 dimen.value = NULL_TREE;
3348 star_seen = true;
3349 c_parser_consume_token (parser);
3351 else
3353 star_seen = false;
3354 dimen = c_parser_expr_no_commas (parser, NULL);
3357 else
3359 star_seen = false;
3360 dimen = c_parser_expr_no_commas (parser, NULL);
3363 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
3364 c_parser_consume_token (parser);
3365 else if (flag_cilkplus
3366 && c_parser_next_token_is (parser, CPP_COLON))
3368 error_at (c_parser_peek_token (parser)->location,
3369 "array notations cannot be used in declaration");
3370 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
3371 return NULL;
3373 else
3375 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
3376 "expected %<]%>");
3377 return NULL;
3379 if (dimen.value)
3380 dimen = convert_lvalue_to_rvalue (brace_loc, dimen, true, true);
3381 declarator = build_array_declarator (brace_loc, dimen.value, quals_attrs,
3382 static_seen, star_seen);
3383 if (declarator == NULL)
3384 return NULL;
3385 inner = set_array_declarator_inner (declarator, inner);
3386 return c_parser_direct_declarator_inner (parser, id_present, inner);
3388 else if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
3390 tree attrs;
3391 struct c_arg_info *args;
3392 c_parser_consume_token (parser);
3393 attrs = c_parser_attributes (parser);
3394 args = c_parser_parms_declarator (parser, id_present, attrs);
3395 if (args == NULL)
3396 return NULL;
3397 else
3399 inner = build_function_declarator (args, inner);
3400 return c_parser_direct_declarator_inner (parser, id_present, inner);
3403 return inner;
3406 /* Parse a parameter list or identifier list, including the closing
3407 parenthesis but not the opening one. ATTRS are the attributes at
3408 the start of the list. ID_LIST_OK is true if an identifier list is
3409 acceptable; such a list must not have attributes at the start. */
3411 static struct c_arg_info *
3412 c_parser_parms_declarator (c_parser *parser, bool id_list_ok, tree attrs)
3414 push_scope ();
3415 declare_parm_level ();
3416 /* If the list starts with an identifier, it is an identifier list.
3417 Otherwise, it is either a prototype list or an empty list. */
3418 if (id_list_ok
3419 && !attrs
3420 && c_parser_next_token_is (parser, CPP_NAME)
3421 && c_parser_peek_token (parser)->id_kind == C_ID_ID
3423 /* Look ahead to detect typos in type names. */
3424 && c_parser_peek_2nd_token (parser)->type != CPP_NAME
3425 && c_parser_peek_2nd_token (parser)->type != CPP_MULT
3426 && c_parser_peek_2nd_token (parser)->type != CPP_OPEN_PAREN
3427 && c_parser_peek_2nd_token (parser)->type != CPP_OPEN_SQUARE)
3429 tree list = NULL_TREE, *nextp = &list;
3430 while (c_parser_next_token_is (parser, CPP_NAME)
3431 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
3433 *nextp = build_tree_list (NULL_TREE,
3434 c_parser_peek_token (parser)->value);
3435 nextp = & TREE_CHAIN (*nextp);
3436 c_parser_consume_token (parser);
3437 if (c_parser_next_token_is_not (parser, CPP_COMMA))
3438 break;
3439 c_parser_consume_token (parser);
3440 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3442 c_parser_error (parser, "expected identifier");
3443 break;
3446 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3448 struct c_arg_info *ret = build_arg_info ();
3449 ret->types = list;
3450 c_parser_consume_token (parser);
3451 pop_scope ();
3452 return ret;
3454 else
3456 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3457 "expected %<)%>");
3458 pop_scope ();
3459 return NULL;
3462 else
3464 struct c_arg_info *ret = c_parser_parms_list_declarator (parser, attrs,
3465 NULL);
3466 pop_scope ();
3467 return ret;
3471 /* Parse a parameter list (possibly empty), including the closing
3472 parenthesis but not the opening one. ATTRS are the attributes at
3473 the start of the list. EXPR is NULL or an expression that needs to
3474 be evaluated for the side effects of array size expressions in the
3475 parameters. */
3477 static struct c_arg_info *
3478 c_parser_parms_list_declarator (c_parser *parser, tree attrs, tree expr)
3480 bool bad_parm = false;
3482 /* ??? Following the old parser, forward parameter declarations may
3483 use abstract declarators, and if no real parameter declarations
3484 follow the forward declarations then this is not diagnosed. Also
3485 note as above that attributes are ignored as the only contents of
3486 the parentheses, or as the only contents after forward
3487 declarations. */
3488 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3490 struct c_arg_info *ret = build_arg_info ();
3491 c_parser_consume_token (parser);
3492 return ret;
3494 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
3496 struct c_arg_info *ret = build_arg_info ();
3498 if (flag_allow_parameterless_variadic_functions)
3500 /* F (...) is allowed. */
3501 ret->types = NULL_TREE;
3503 else
3505 /* Suppress -Wold-style-definition for this case. */
3506 ret->types = error_mark_node;
3507 error_at (c_parser_peek_token (parser)->location,
3508 "ISO C requires a named argument before %<...%>");
3510 c_parser_consume_token (parser);
3511 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3513 c_parser_consume_token (parser);
3514 return ret;
3516 else
3518 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3519 "expected %<)%>");
3520 return NULL;
3523 /* Nonempty list of parameters, either terminated with semicolon
3524 (forward declarations; recurse) or with close parenthesis (normal
3525 function) or with ", ... )" (variadic function). */
3526 while (true)
3528 /* Parse a parameter. */
3529 struct c_parm *parm = c_parser_parameter_declaration (parser, attrs);
3530 attrs = NULL_TREE;
3531 if (parm == NULL)
3532 bad_parm = true;
3533 else
3534 push_parm_decl (parm, &expr);
3535 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
3537 tree new_attrs;
3538 c_parser_consume_token (parser);
3539 mark_forward_parm_decls ();
3540 new_attrs = c_parser_attributes (parser);
3541 return c_parser_parms_list_declarator (parser, new_attrs, expr);
3543 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3545 c_parser_consume_token (parser);
3546 if (bad_parm)
3547 return NULL;
3548 else
3549 return get_parm_info (false, expr);
3551 if (!c_parser_require (parser, CPP_COMMA,
3552 "expected %<;%>, %<,%> or %<)%>"))
3554 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
3555 return NULL;
3557 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
3559 c_parser_consume_token (parser);
3560 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3562 c_parser_consume_token (parser);
3563 if (bad_parm)
3564 return NULL;
3565 else
3566 return get_parm_info (true, expr);
3568 else
3570 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3571 "expected %<)%>");
3572 return NULL;
3578 /* Parse a parameter declaration. ATTRS are the attributes at the
3579 start of the declaration if it is the first parameter. */
3581 static struct c_parm *
3582 c_parser_parameter_declaration (c_parser *parser, tree attrs)
3584 struct c_declspecs *specs;
3585 struct c_declarator *declarator;
3586 tree prefix_attrs;
3587 tree postfix_attrs = NULL_TREE;
3588 bool dummy = false;
3590 /* Accept #pragmas between parameter declarations. */
3591 while (c_parser_next_token_is (parser, CPP_PRAGMA))
3592 c_parser_pragma (parser, pragma_param);
3594 if (!c_parser_next_token_starts_declspecs (parser))
3596 c_token *token = c_parser_peek_token (parser);
3597 if (parser->error)
3598 return NULL;
3599 c_parser_set_source_position_from_token (token);
3600 if (c_parser_next_tokens_start_typename (parser, cla_prefer_type))
3602 error_at (token->location, "unknown type name %qE", token->value);
3603 parser->error = true;
3605 /* ??? In some Objective-C cases '...' isn't applicable so there
3606 should be a different message. */
3607 else
3608 c_parser_error (parser,
3609 "expected declaration specifiers or %<...%>");
3610 c_parser_skip_to_end_of_parameter (parser);
3611 return NULL;
3613 specs = build_null_declspecs ();
3614 if (attrs)
3616 declspecs_add_attrs (input_location, specs, attrs);
3617 attrs = NULL_TREE;
3619 c_parser_declspecs (parser, specs, true, true, true, true, false,
3620 cla_nonabstract_decl);
3621 finish_declspecs (specs);
3622 pending_xref_error ();
3623 prefix_attrs = specs->attrs;
3624 specs->attrs = NULL_TREE;
3625 declarator = c_parser_declarator (parser,
3626 specs->typespec_kind != ctsk_none,
3627 C_DTR_PARM, &dummy);
3628 if (declarator == NULL)
3630 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
3631 return NULL;
3633 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
3634 postfix_attrs = c_parser_attributes (parser);
3635 return build_c_parm (specs, chainon (postfix_attrs, prefix_attrs),
3636 declarator);
3639 /* Parse a string literal in an asm expression. It should not be
3640 translated, and wide string literals are an error although
3641 permitted by the syntax. This is a GNU extension.
3643 asm-string-literal:
3644 string-literal
3646 ??? At present, following the old parser, the caller needs to have
3647 set lex_untranslated_string to 1. It would be better to follow the
3648 C++ parser rather than using this kludge. */
3650 static tree
3651 c_parser_asm_string_literal (c_parser *parser)
3653 tree str;
3654 int save_flag = warn_overlength_strings;
3655 warn_overlength_strings = 0;
3656 if (c_parser_next_token_is (parser, CPP_STRING))
3658 str = c_parser_peek_token (parser)->value;
3659 c_parser_consume_token (parser);
3661 else if (c_parser_next_token_is (parser, CPP_WSTRING))
3663 error_at (c_parser_peek_token (parser)->location,
3664 "wide string literal in %<asm%>");
3665 str = build_string (1, "");
3666 c_parser_consume_token (parser);
3668 else
3670 c_parser_error (parser, "expected string literal");
3671 str = NULL_TREE;
3673 warn_overlength_strings = save_flag;
3674 return str;
3677 /* Parse a simple asm expression. This is used in restricted
3678 contexts, where a full expression with inputs and outputs does not
3679 make sense. This is a GNU extension.
3681 simple-asm-expr:
3682 asm ( asm-string-literal )
3685 static tree
3686 c_parser_simple_asm_expr (c_parser *parser)
3688 tree str;
3689 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ASM));
3690 /* ??? Follow the C++ parser rather than using the
3691 lex_untranslated_string kludge. */
3692 parser->lex_untranslated_string = true;
3693 c_parser_consume_token (parser);
3694 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3696 parser->lex_untranslated_string = false;
3697 return NULL_TREE;
3699 str = c_parser_asm_string_literal (parser);
3700 parser->lex_untranslated_string = false;
3701 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
3703 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
3704 return NULL_TREE;
3706 return str;
3709 static tree
3710 c_parser_attribute_any_word (c_parser *parser)
3712 tree attr_name = NULL_TREE;
3714 if (c_parser_next_token_is (parser, CPP_KEYWORD))
3716 /* ??? See comment above about what keywords are accepted here. */
3717 bool ok;
3718 switch (c_parser_peek_token (parser)->keyword)
3720 case RID_STATIC:
3721 case RID_UNSIGNED:
3722 case RID_LONG:
3723 case RID_INT128:
3724 case RID_CONST:
3725 case RID_EXTERN:
3726 case RID_REGISTER:
3727 case RID_TYPEDEF:
3728 case RID_SHORT:
3729 case RID_INLINE:
3730 case RID_NORETURN:
3731 case RID_VOLATILE:
3732 case RID_SIGNED:
3733 case RID_AUTO:
3734 case RID_RESTRICT:
3735 case RID_COMPLEX:
3736 case RID_THREAD:
3737 case RID_INT:
3738 case RID_CHAR:
3739 case RID_FLOAT:
3740 case RID_DOUBLE:
3741 case RID_VOID:
3742 case RID_DFLOAT32:
3743 case RID_DFLOAT64:
3744 case RID_DFLOAT128:
3745 case RID_BOOL:
3746 case RID_FRACT:
3747 case RID_ACCUM:
3748 case RID_SAT:
3749 case RID_TRANSACTION_ATOMIC:
3750 case RID_TRANSACTION_CANCEL:
3751 case RID_ATOMIC:
3752 case RID_AUTO_TYPE:
3753 ok = true;
3754 break;
3755 default:
3756 ok = false;
3757 break;
3759 if (!ok)
3760 return NULL_TREE;
3762 /* Accept __attribute__((__const)) as __attribute__((const)) etc. */
3763 attr_name = ridpointers[(int) c_parser_peek_token (parser)->keyword];
3765 else if (c_parser_next_token_is (parser, CPP_NAME))
3766 attr_name = c_parser_peek_token (parser)->value;
3768 return attr_name;
3771 /* Returns true of NAME is an IDENTIFIER_NODE with identiifer "vector,"
3772 "__vector" or "__vector__." */
3774 static inline bool
3775 is_cilkplus_vector_p (tree name)
3777 if (flag_cilkplus && is_attribute_p ("vector", name))
3778 return true;
3779 return false;
3782 #define CILK_SIMD_FN_CLAUSE_MASK \
3783 ((OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_VECTORLENGTH) \
3784 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_LINEAR) \
3785 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_UNIFORM) \
3786 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_MASK) \
3787 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_NOMASK))
3789 /* Parses the vector attribute of SIMD enabled functions in Cilk Plus.
3790 VEC_TOKEN is the "vector" token that is replaced with "simd" and
3791 pushed into the token list.
3792 Syntax:
3793 vector
3794 vector (<vector attributes>). */
3796 static void
3797 c_parser_cilk_simd_fn_vector_attrs (c_parser *parser, c_token vec_token)
3799 gcc_assert (is_cilkplus_vector_p (vec_token.value));
3801 int paren_scope = 0;
3802 vec_safe_push (parser->cilk_simd_fn_tokens, vec_token);
3803 /* Consume the "vector" token. */
3804 c_parser_consume_token (parser);
3806 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
3808 c_parser_consume_token (parser);
3809 paren_scope++;
3811 while (paren_scope > 0)
3813 c_token *token = c_parser_peek_token (parser);
3814 if (token->type == CPP_OPEN_PAREN)
3815 paren_scope++;
3816 else if (token->type == CPP_CLOSE_PAREN)
3817 paren_scope--;
3818 /* Do not push the last ')' since we are not pushing the '('. */
3819 if (!(token->type == CPP_CLOSE_PAREN && paren_scope == 0))
3820 vec_safe_push (parser->cilk_simd_fn_tokens, *token);
3821 c_parser_consume_token (parser);
3824 /* Since we are converting an attribute to a pragma, we need to end the
3825 attribute with PRAGMA_EOL. */
3826 c_token eol_token;
3827 memset (&eol_token, 0, sizeof (eol_token));
3828 eol_token.type = CPP_PRAGMA_EOL;
3829 vec_safe_push (parser->cilk_simd_fn_tokens, eol_token);
3832 /* Add 2 CPP_EOF at the end of PARSER->ELEM_FN_TOKENS vector. */
3834 static void
3835 c_finish_cilk_simd_fn_tokens (c_parser *parser)
3837 c_token last_token = parser->cilk_simd_fn_tokens->last ();
3839 /* c_parser_attributes is called in several places, so if these EOF
3840 tokens are already inserted, then don't do them again. */
3841 if (last_token.type == CPP_EOF)
3842 return;
3844 /* Two CPP_EOF token are added as a safety net since the normal C
3845 front-end has two token look-ahead. */
3846 c_token eof_token;
3847 eof_token.type = CPP_EOF;
3848 vec_safe_push (parser->cilk_simd_fn_tokens, eof_token);
3849 vec_safe_push (parser->cilk_simd_fn_tokens, eof_token);
3852 /* Parse (possibly empty) attributes. This is a GNU extension.
3854 attributes:
3855 empty
3856 attributes attribute
3858 attribute:
3859 __attribute__ ( ( attribute-list ) )
3861 attribute-list:
3862 attrib
3863 attribute_list , attrib
3865 attrib:
3866 empty
3867 any-word
3868 any-word ( identifier )
3869 any-word ( identifier , nonempty-expr-list )
3870 any-word ( expr-list )
3872 where the "identifier" must not be declared as a type, and
3873 "any-word" may be any identifier (including one declared as a
3874 type), a reserved word storage class specifier, type specifier or
3875 type qualifier. ??? This still leaves out most reserved keywords
3876 (following the old parser), shouldn't we include them, and why not
3877 allow identifiers declared as types to start the arguments? */
3879 static tree
3880 c_parser_attributes (c_parser *parser)
3882 tree attrs = NULL_TREE;
3883 while (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
3885 /* ??? Follow the C++ parser rather than using the
3886 lex_untranslated_string kludge. */
3887 parser->lex_untranslated_string = true;
3888 c_parser_consume_token (parser);
3889 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3891 parser->lex_untranslated_string = false;
3892 return attrs;
3894 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3896 parser->lex_untranslated_string = false;
3897 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
3898 return attrs;
3900 /* Parse the attribute list. */
3901 while (c_parser_next_token_is (parser, CPP_COMMA)
3902 || c_parser_next_token_is (parser, CPP_NAME)
3903 || c_parser_next_token_is (parser, CPP_KEYWORD))
3905 tree attr, attr_name, attr_args;
3906 vec<tree, va_gc> *expr_list;
3907 if (c_parser_next_token_is (parser, CPP_COMMA))
3909 c_parser_consume_token (parser);
3910 continue;
3913 attr_name = c_parser_attribute_any_word (parser);
3914 if (attr_name == NULL)
3915 break;
3916 if (is_cilkplus_vector_p (attr_name))
3918 c_token *v_token = c_parser_peek_token (parser);
3919 c_parser_cilk_simd_fn_vector_attrs (parser, *v_token);
3920 continue;
3922 c_parser_consume_token (parser);
3923 if (c_parser_next_token_is_not (parser, CPP_OPEN_PAREN))
3925 attr = build_tree_list (attr_name, NULL_TREE);
3926 attrs = chainon (attrs, attr);
3927 continue;
3929 c_parser_consume_token (parser);
3930 /* Parse the attribute contents. If they start with an
3931 identifier which is followed by a comma or close
3932 parenthesis, then the arguments start with that
3933 identifier; otherwise they are an expression list.
3934 In objective-c the identifier may be a classname. */
3935 if (c_parser_next_token_is (parser, CPP_NAME)
3936 && (c_parser_peek_token (parser)->id_kind == C_ID_ID
3937 || (c_dialect_objc ()
3938 && c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME))
3939 && ((c_parser_peek_2nd_token (parser)->type == CPP_COMMA)
3940 || (c_parser_peek_2nd_token (parser)->type
3941 == CPP_CLOSE_PAREN)))
3943 tree arg1 = c_parser_peek_token (parser)->value;
3944 c_parser_consume_token (parser);
3945 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3946 attr_args = build_tree_list (NULL_TREE, arg1);
3947 else
3949 tree tree_list;
3950 c_parser_consume_token (parser);
3951 expr_list = c_parser_expr_list (parser, false, true,
3952 NULL, NULL, NULL, NULL);
3953 tree_list = build_tree_list_vec (expr_list);
3954 attr_args = tree_cons (NULL_TREE, arg1, tree_list);
3955 release_tree_vector (expr_list);
3958 else
3960 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3961 attr_args = NULL_TREE;
3962 else
3964 expr_list = c_parser_expr_list (parser, false, true,
3965 NULL, NULL, NULL, NULL);
3966 attr_args = build_tree_list_vec (expr_list);
3967 release_tree_vector (expr_list);
3970 attr = build_tree_list (attr_name, attr_args);
3971 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3972 c_parser_consume_token (parser);
3973 else
3975 parser->lex_untranslated_string = false;
3976 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3977 "expected %<)%>");
3978 return attrs;
3980 attrs = chainon (attrs, attr);
3982 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3983 c_parser_consume_token (parser);
3984 else
3986 parser->lex_untranslated_string = false;
3987 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3988 "expected %<)%>");
3989 return attrs;
3991 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3992 c_parser_consume_token (parser);
3993 else
3995 parser->lex_untranslated_string = false;
3996 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3997 "expected %<)%>");
3998 return attrs;
4000 parser->lex_untranslated_string = false;
4003 if (flag_cilkplus && !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
4004 c_finish_cilk_simd_fn_tokens (parser);
4005 return attrs;
4008 /* Parse a type name (C90 6.5.5, C99 6.7.6).
4010 type-name:
4011 specifier-qualifier-list abstract-declarator[opt]
4014 static struct c_type_name *
4015 c_parser_type_name (c_parser *parser)
4017 struct c_declspecs *specs = build_null_declspecs ();
4018 struct c_declarator *declarator;
4019 struct c_type_name *ret;
4020 bool dummy = false;
4021 c_parser_declspecs (parser, specs, false, true, true, false, false,
4022 cla_prefer_type);
4023 if (!specs->declspecs_seen_p)
4025 c_parser_error (parser, "expected specifier-qualifier-list");
4026 return NULL;
4028 if (specs->type != error_mark_node)
4030 pending_xref_error ();
4031 finish_declspecs (specs);
4033 declarator = c_parser_declarator (parser,
4034 specs->typespec_kind != ctsk_none,
4035 C_DTR_ABSTRACT, &dummy);
4036 if (declarator == NULL)
4037 return NULL;
4038 ret = XOBNEW (&parser_obstack, struct c_type_name);
4039 ret->specs = specs;
4040 ret->declarator = declarator;
4041 return ret;
4044 /* Parse an initializer (C90 6.5.7, C99 6.7.8).
4046 initializer:
4047 assignment-expression
4048 { initializer-list }
4049 { initializer-list , }
4051 initializer-list:
4052 designation[opt] initializer
4053 initializer-list , designation[opt] initializer
4055 designation:
4056 designator-list =
4058 designator-list:
4059 designator
4060 designator-list designator
4062 designator:
4063 array-designator
4064 . identifier
4066 array-designator:
4067 [ constant-expression ]
4069 GNU extensions:
4071 initializer:
4074 designation:
4075 array-designator
4076 identifier :
4078 array-designator:
4079 [ constant-expression ... constant-expression ]
4081 Any expression without commas is accepted in the syntax for the
4082 constant-expressions, with non-constant expressions rejected later.
4084 This function is only used for top-level initializers; for nested
4085 ones, see c_parser_initval. */
4087 static struct c_expr
4088 c_parser_initializer (c_parser *parser)
4090 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
4091 return c_parser_braced_init (parser, NULL_TREE, false);
4092 else
4094 struct c_expr ret;
4095 location_t loc = c_parser_peek_token (parser)->location;
4096 ret = c_parser_expr_no_commas (parser, NULL);
4097 if (TREE_CODE (ret.value) != STRING_CST
4098 && TREE_CODE (ret.value) != COMPOUND_LITERAL_EXPR)
4099 ret = convert_lvalue_to_rvalue (loc, ret, true, true);
4100 return ret;
4104 /* Parse a braced initializer list. TYPE is the type specified for a
4105 compound literal, and NULL_TREE for other initializers and for
4106 nested braced lists. NESTED_P is true for nested braced lists,
4107 false for the list of a compound literal or the list that is the
4108 top-level initializer in a declaration. */
4110 static struct c_expr
4111 c_parser_braced_init (c_parser *parser, tree type, bool nested_p)
4113 struct c_expr ret;
4114 struct obstack braced_init_obstack;
4115 location_t brace_loc = c_parser_peek_token (parser)->location;
4116 gcc_obstack_init (&braced_init_obstack);
4117 gcc_assert (c_parser_next_token_is (parser, CPP_OPEN_BRACE));
4118 c_parser_consume_token (parser);
4119 if (nested_p)
4120 push_init_level (0, &braced_init_obstack);
4121 else
4122 really_start_incremental_init (type);
4123 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
4125 pedwarn (brace_loc, OPT_Wpedantic, "ISO C forbids empty initializer braces");
4127 else
4129 /* Parse a non-empty initializer list, possibly with a trailing
4130 comma. */
4131 while (true)
4133 c_parser_initelt (parser, &braced_init_obstack);
4134 if (parser->error)
4135 break;
4136 if (c_parser_next_token_is (parser, CPP_COMMA))
4137 c_parser_consume_token (parser);
4138 else
4139 break;
4140 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
4141 break;
4144 if (c_parser_next_token_is_not (parser, CPP_CLOSE_BRACE))
4146 ret.value = error_mark_node;
4147 ret.original_code = ERROR_MARK;
4148 ret.original_type = NULL;
4149 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, "expected %<}%>");
4150 pop_init_level (0, &braced_init_obstack);
4151 obstack_free (&braced_init_obstack, NULL);
4152 return ret;
4154 c_parser_consume_token (parser);
4155 ret = pop_init_level (0, &braced_init_obstack);
4156 obstack_free (&braced_init_obstack, NULL);
4157 return ret;
4160 /* Parse a nested initializer, including designators. */
4162 static void
4163 c_parser_initelt (c_parser *parser, struct obstack * braced_init_obstack)
4165 /* Parse any designator or designator list. A single array
4166 designator may have the subsequent "=" omitted in GNU C, but a
4167 longer list or a structure member designator may not. */
4168 if (c_parser_next_token_is (parser, CPP_NAME)
4169 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
4171 /* Old-style structure member designator. */
4172 set_init_label (c_parser_peek_token (parser)->value,
4173 braced_init_obstack);
4174 /* Use the colon as the error location. */
4175 pedwarn (c_parser_peek_2nd_token (parser)->location, OPT_Wpedantic,
4176 "obsolete use of designated initializer with %<:%>");
4177 c_parser_consume_token (parser);
4178 c_parser_consume_token (parser);
4180 else
4182 /* des_seen is 0 if there have been no designators, 1 if there
4183 has been a single array designator and 2 otherwise. */
4184 int des_seen = 0;
4185 /* Location of a designator. */
4186 location_t des_loc = UNKNOWN_LOCATION; /* Quiet warning. */
4187 while (c_parser_next_token_is (parser, CPP_OPEN_SQUARE)
4188 || c_parser_next_token_is (parser, CPP_DOT))
4190 int des_prev = des_seen;
4191 if (!des_seen)
4192 des_loc = c_parser_peek_token (parser)->location;
4193 if (des_seen < 2)
4194 des_seen++;
4195 if (c_parser_next_token_is (parser, CPP_DOT))
4197 des_seen = 2;
4198 c_parser_consume_token (parser);
4199 if (c_parser_next_token_is (parser, CPP_NAME))
4201 set_init_label (c_parser_peek_token (parser)->value,
4202 braced_init_obstack);
4203 c_parser_consume_token (parser);
4205 else
4207 struct c_expr init;
4208 init.value = error_mark_node;
4209 init.original_code = ERROR_MARK;
4210 init.original_type = NULL;
4211 c_parser_error (parser, "expected identifier");
4212 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
4213 process_init_element (init, false, braced_init_obstack);
4214 return;
4217 else
4219 tree first, second;
4220 location_t ellipsis_loc = UNKNOWN_LOCATION; /* Quiet warning. */
4221 /* ??? Following the old parser, [ objc-receiver
4222 objc-message-args ] is accepted as an initializer,
4223 being distinguished from a designator by what follows
4224 the first assignment expression inside the square
4225 brackets, but after a first array designator a
4226 subsequent square bracket is for Objective-C taken to
4227 start an expression, using the obsolete form of
4228 designated initializer without '=', rather than
4229 possibly being a second level of designation: in LALR
4230 terms, the '[' is shifted rather than reducing
4231 designator to designator-list. */
4232 if (des_prev == 1 && c_dialect_objc ())
4234 des_seen = des_prev;
4235 break;
4237 if (des_prev == 0 && c_dialect_objc ())
4239 /* This might be an array designator or an
4240 Objective-C message expression. If the former,
4241 continue parsing here; if the latter, parse the
4242 remainder of the initializer given the starting
4243 primary-expression. ??? It might make sense to
4244 distinguish when des_prev == 1 as well; see
4245 previous comment. */
4246 tree rec, args;
4247 struct c_expr mexpr;
4248 c_parser_consume_token (parser);
4249 if (c_parser_peek_token (parser)->type == CPP_NAME
4250 && ((c_parser_peek_token (parser)->id_kind
4251 == C_ID_TYPENAME)
4252 || (c_parser_peek_token (parser)->id_kind
4253 == C_ID_CLASSNAME)))
4255 /* Type name receiver. */
4256 tree id = c_parser_peek_token (parser)->value;
4257 c_parser_consume_token (parser);
4258 rec = objc_get_class_reference (id);
4259 goto parse_message_args;
4261 first = c_parser_expr_no_commas (parser, NULL).value;
4262 mark_exp_read (first);
4263 if (c_parser_next_token_is (parser, CPP_ELLIPSIS)
4264 || c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
4265 goto array_desig_after_first;
4266 /* Expression receiver. So far only one part
4267 without commas has been parsed; there might be
4268 more of the expression. */
4269 rec = first;
4270 while (c_parser_next_token_is (parser, CPP_COMMA))
4272 struct c_expr next;
4273 location_t comma_loc, exp_loc;
4274 comma_loc = c_parser_peek_token (parser)->location;
4275 c_parser_consume_token (parser);
4276 exp_loc = c_parser_peek_token (parser)->location;
4277 next = c_parser_expr_no_commas (parser, NULL);
4278 next = convert_lvalue_to_rvalue (exp_loc, next,
4279 true, true);
4280 rec = build_compound_expr (comma_loc, rec, next.value);
4282 parse_message_args:
4283 /* Now parse the objc-message-args. */
4284 args = c_parser_objc_message_args (parser);
4285 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
4286 "expected %<]%>");
4287 mexpr.value
4288 = objc_build_message_expr (rec, args);
4289 mexpr.original_code = ERROR_MARK;
4290 mexpr.original_type = NULL;
4291 /* Now parse and process the remainder of the
4292 initializer, starting with this message
4293 expression as a primary-expression. */
4294 c_parser_initval (parser, &mexpr, braced_init_obstack);
4295 return;
4297 c_parser_consume_token (parser);
4298 first = c_parser_expr_no_commas (parser, NULL).value;
4299 mark_exp_read (first);
4300 array_desig_after_first:
4301 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
4303 ellipsis_loc = c_parser_peek_token (parser)->location;
4304 c_parser_consume_token (parser);
4305 second = c_parser_expr_no_commas (parser, NULL).value;
4306 mark_exp_read (second);
4308 else
4309 second = NULL_TREE;
4310 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
4312 c_parser_consume_token (parser);
4313 set_init_index (first, second, braced_init_obstack);
4314 if (second)
4315 pedwarn (ellipsis_loc, OPT_Wpedantic,
4316 "ISO C forbids specifying range of elements to initialize");
4318 else
4319 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
4320 "expected %<]%>");
4323 if (des_seen >= 1)
4325 if (c_parser_next_token_is (parser, CPP_EQ))
4327 if (!flag_isoc99)
4328 pedwarn (des_loc, OPT_Wpedantic,
4329 "ISO C90 forbids specifying subobject to initialize");
4330 c_parser_consume_token (parser);
4332 else
4334 if (des_seen == 1)
4335 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
4336 "obsolete use of designated initializer without %<=%>");
4337 else
4339 struct c_expr init;
4340 init.value = error_mark_node;
4341 init.original_code = ERROR_MARK;
4342 init.original_type = NULL;
4343 c_parser_error (parser, "expected %<=%>");
4344 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
4345 process_init_element (init, false, braced_init_obstack);
4346 return;
4351 c_parser_initval (parser, NULL, braced_init_obstack);
4354 /* Parse a nested initializer; as c_parser_initializer but parses
4355 initializers within braced lists, after any designators have been
4356 applied. If AFTER is not NULL then it is an Objective-C message
4357 expression which is the primary-expression starting the
4358 initializer. */
4360 static void
4361 c_parser_initval (c_parser *parser, struct c_expr *after,
4362 struct obstack * braced_init_obstack)
4364 struct c_expr init;
4365 gcc_assert (!after || c_dialect_objc ());
4366 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE) && !after)
4367 init = c_parser_braced_init (parser, NULL_TREE, true);
4368 else
4370 location_t loc = c_parser_peek_token (parser)->location;
4371 init = c_parser_expr_no_commas (parser, after);
4372 if (init.value != NULL_TREE
4373 && TREE_CODE (init.value) != STRING_CST
4374 && TREE_CODE (init.value) != COMPOUND_LITERAL_EXPR)
4375 init = convert_lvalue_to_rvalue (loc, init, true, true);
4377 process_init_element (init, false, braced_init_obstack);
4380 /* Parse a compound statement (possibly a function body) (C90 6.6.2,
4381 C99 6.8.2).
4383 compound-statement:
4384 { block-item-list[opt] }
4385 { label-declarations block-item-list }
4387 block-item-list:
4388 block-item
4389 block-item-list block-item
4391 block-item:
4392 nested-declaration
4393 statement
4395 nested-declaration:
4396 declaration
4398 GNU extensions:
4400 compound-statement:
4401 { label-declarations block-item-list }
4403 nested-declaration:
4404 __extension__ nested-declaration
4405 nested-function-definition
4407 label-declarations:
4408 label-declaration
4409 label-declarations label-declaration
4411 label-declaration:
4412 __label__ identifier-list ;
4414 Allowing the mixing of declarations and code is new in C99. The
4415 GNU syntax also permits (not shown above) labels at the end of
4416 compound statements, which yield an error. We don't allow labels
4417 on declarations; this might seem like a natural extension, but
4418 there would be a conflict between attributes on the label and
4419 prefix attributes on the declaration. ??? The syntax follows the
4420 old parser in requiring something after label declarations.
4421 Although they are erroneous if the labels declared aren't defined,
4422 is it useful for the syntax to be this way?
4424 OpenMP:
4426 block-item:
4427 openmp-directive
4429 openmp-directive:
4430 barrier-directive
4431 flush-directive
4432 taskwait-directive
4433 taskyield-directive
4434 cancel-directive
4435 cancellation-point-directive */
4437 static tree
4438 c_parser_compound_statement (c_parser *parser)
4440 tree stmt;
4441 location_t brace_loc;
4442 brace_loc = c_parser_peek_token (parser)->location;
4443 if (!c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
4445 /* Ensure a scope is entered and left anyway to avoid confusion
4446 if we have just prepared to enter a function body. */
4447 stmt = c_begin_compound_stmt (true);
4448 c_end_compound_stmt (brace_loc, stmt, true);
4449 return error_mark_node;
4451 stmt = c_begin_compound_stmt (true);
4452 c_parser_compound_statement_nostart (parser);
4454 /* If the compound stmt contains array notations, then we expand them. */
4455 if (flag_cilkplus && contains_array_notation_expr (stmt))
4456 stmt = expand_array_notation_exprs (stmt);
4457 return c_end_compound_stmt (brace_loc, stmt, true);
4460 /* Parse a compound statement except for the opening brace. This is
4461 used for parsing both compound statements and statement expressions
4462 (which follow different paths to handling the opening). */
4464 static void
4465 c_parser_compound_statement_nostart (c_parser *parser)
4467 bool last_stmt = false;
4468 bool last_label = false;
4469 bool save_valid_for_pragma = valid_location_for_stdc_pragma_p ();
4470 location_t label_loc = UNKNOWN_LOCATION; /* Quiet warning. */
4471 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
4473 c_parser_consume_token (parser);
4474 return;
4476 mark_valid_location_for_stdc_pragma (true);
4477 if (c_parser_next_token_is_keyword (parser, RID_LABEL))
4479 /* Read zero or more forward-declarations for labels that nested
4480 functions can jump to. */
4481 mark_valid_location_for_stdc_pragma (false);
4482 while (c_parser_next_token_is_keyword (parser, RID_LABEL))
4484 label_loc = c_parser_peek_token (parser)->location;
4485 c_parser_consume_token (parser);
4486 /* Any identifiers, including those declared as type names,
4487 are OK here. */
4488 while (true)
4490 tree label;
4491 if (c_parser_next_token_is_not (parser, CPP_NAME))
4493 c_parser_error (parser, "expected identifier");
4494 break;
4496 label
4497 = declare_label (c_parser_peek_token (parser)->value);
4498 C_DECLARED_LABEL_FLAG (label) = 1;
4499 add_stmt (build_stmt (label_loc, DECL_EXPR, label));
4500 c_parser_consume_token (parser);
4501 if (c_parser_next_token_is (parser, CPP_COMMA))
4502 c_parser_consume_token (parser);
4503 else
4504 break;
4506 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
4508 pedwarn (label_loc, OPT_Wpedantic, "ISO C forbids label declarations");
4510 /* We must now have at least one statement, label or declaration. */
4511 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
4513 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
4514 c_parser_error (parser, "expected declaration or statement");
4515 c_parser_consume_token (parser);
4516 return;
4518 while (c_parser_next_token_is_not (parser, CPP_CLOSE_BRACE))
4520 location_t loc = c_parser_peek_token (parser)->location;
4521 if (c_parser_next_token_is_keyword (parser, RID_CASE)
4522 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
4523 || (c_parser_next_token_is (parser, CPP_NAME)
4524 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
4526 if (c_parser_next_token_is_keyword (parser, RID_CASE))
4527 label_loc = c_parser_peek_2nd_token (parser)->location;
4528 else
4529 label_loc = c_parser_peek_token (parser)->location;
4530 last_label = true;
4531 last_stmt = false;
4532 mark_valid_location_for_stdc_pragma (false);
4533 c_parser_label (parser);
4535 else if (!last_label
4536 && c_parser_next_tokens_start_declaration (parser))
4538 last_label = false;
4539 mark_valid_location_for_stdc_pragma (false);
4540 c_parser_declaration_or_fndef (parser, true, true, true, true,
4541 true, NULL, vNULL);
4542 if (last_stmt)
4543 pedwarn_c90 (loc,
4544 (pedantic && !flag_isoc99)
4545 ? OPT_Wpedantic
4546 : OPT_Wdeclaration_after_statement,
4547 "ISO C90 forbids mixed declarations and code");
4548 last_stmt = false;
4550 else if (!last_label
4551 && c_parser_next_token_is_keyword (parser, RID_EXTENSION))
4553 /* __extension__ can start a declaration, but is also an
4554 unary operator that can start an expression. Consume all
4555 but the last of a possible series of __extension__ to
4556 determine which. */
4557 while (c_parser_peek_2nd_token (parser)->type == CPP_KEYWORD
4558 && (c_parser_peek_2nd_token (parser)->keyword
4559 == RID_EXTENSION))
4560 c_parser_consume_token (parser);
4561 if (c_token_starts_declaration (c_parser_peek_2nd_token (parser)))
4563 int ext;
4564 ext = disable_extension_diagnostics ();
4565 c_parser_consume_token (parser);
4566 last_label = false;
4567 mark_valid_location_for_stdc_pragma (false);
4568 c_parser_declaration_or_fndef (parser, true, true, true, true,
4569 true, NULL, vNULL);
4570 /* Following the old parser, __extension__ does not
4571 disable this diagnostic. */
4572 restore_extension_diagnostics (ext);
4573 if (last_stmt)
4574 pedwarn_c90 (loc, (pedantic && !flag_isoc99)
4575 ? OPT_Wpedantic
4576 : OPT_Wdeclaration_after_statement,
4577 "ISO C90 forbids mixed declarations and code");
4578 last_stmt = false;
4580 else
4581 goto statement;
4583 else if (c_parser_next_token_is (parser, CPP_PRAGMA))
4585 /* External pragmas, and some omp pragmas, are not associated
4586 with regular c code, and so are not to be considered statements
4587 syntactically. This ensures that the user doesn't put them
4588 places that would turn into syntax errors if the directive
4589 were ignored. */
4590 if (c_parser_pragma (parser, pragma_compound))
4591 last_label = false, last_stmt = true;
4593 else if (c_parser_next_token_is (parser, CPP_EOF))
4595 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
4596 c_parser_error (parser, "expected declaration or statement");
4597 return;
4599 else if (c_parser_next_token_is_keyword (parser, RID_ELSE))
4601 if (parser->in_if_block)
4603 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
4604 error_at (loc, """expected %<}%> before %<else%>");
4605 return;
4607 else
4609 error_at (loc, "%<else%> without a previous %<if%>");
4610 c_parser_consume_token (parser);
4611 continue;
4614 else
4616 statement:
4617 last_label = false;
4618 last_stmt = true;
4619 mark_valid_location_for_stdc_pragma (false);
4620 c_parser_statement_after_labels (parser);
4623 parser->error = false;
4625 if (last_label)
4626 error_at (label_loc, "label at end of compound statement");
4627 c_parser_consume_token (parser);
4628 /* Restore the value we started with. */
4629 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
4632 /* Parse a label (C90 6.6.1, C99 6.8.1).
4634 label:
4635 identifier : attributes[opt]
4636 case constant-expression :
4637 default :
4639 GNU extensions:
4641 label:
4642 case constant-expression ... constant-expression :
4644 The use of attributes on labels is a GNU extension. The syntax in
4645 GNU C accepts any expressions without commas, non-constant
4646 expressions being rejected later. */
4648 static void
4649 c_parser_label (c_parser *parser)
4651 location_t loc1 = c_parser_peek_token (parser)->location;
4652 tree label = NULL_TREE;
4653 if (c_parser_next_token_is_keyword (parser, RID_CASE))
4655 tree exp1, exp2;
4656 c_parser_consume_token (parser);
4657 exp1 = c_parser_expr_no_commas (parser, NULL).value;
4658 if (c_parser_next_token_is (parser, CPP_COLON))
4660 c_parser_consume_token (parser);
4661 label = do_case (loc1, exp1, NULL_TREE);
4663 else if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
4665 c_parser_consume_token (parser);
4666 exp2 = c_parser_expr_no_commas (parser, NULL).value;
4667 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
4668 label = do_case (loc1, exp1, exp2);
4670 else
4671 c_parser_error (parser, "expected %<:%> or %<...%>");
4673 else if (c_parser_next_token_is_keyword (parser, RID_DEFAULT))
4675 c_parser_consume_token (parser);
4676 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
4677 label = do_case (loc1, NULL_TREE, NULL_TREE);
4679 else
4681 tree name = c_parser_peek_token (parser)->value;
4682 tree tlab;
4683 tree attrs;
4684 location_t loc2 = c_parser_peek_token (parser)->location;
4685 gcc_assert (c_parser_next_token_is (parser, CPP_NAME));
4686 c_parser_consume_token (parser);
4687 gcc_assert (c_parser_next_token_is (parser, CPP_COLON));
4688 c_parser_consume_token (parser);
4689 attrs = c_parser_attributes (parser);
4690 tlab = define_label (loc2, name);
4691 if (tlab)
4693 decl_attributes (&tlab, attrs, 0);
4694 label = add_stmt (build_stmt (loc1, LABEL_EXPR, tlab));
4697 if (label)
4699 if (c_parser_next_tokens_start_declaration (parser))
4701 error_at (c_parser_peek_token (parser)->location,
4702 "a label can only be part of a statement and "
4703 "a declaration is not a statement");
4704 c_parser_declaration_or_fndef (parser, /*fndef_ok*/ false,
4705 /*static_assert_ok*/ true,
4706 /*empty_ok*/ true, /*nested*/ true,
4707 /*start_attr_ok*/ true, NULL,
4708 vNULL);
4713 /* Parse a statement (C90 6.6, C99 6.8).
4715 statement:
4716 labeled-statement
4717 compound-statement
4718 expression-statement
4719 selection-statement
4720 iteration-statement
4721 jump-statement
4723 labeled-statement:
4724 label statement
4726 expression-statement:
4727 expression[opt] ;
4729 selection-statement:
4730 if-statement
4731 switch-statement
4733 iteration-statement:
4734 while-statement
4735 do-statement
4736 for-statement
4738 jump-statement:
4739 goto identifier ;
4740 continue ;
4741 break ;
4742 return expression[opt] ;
4744 GNU extensions:
4746 statement:
4747 asm-statement
4749 jump-statement:
4750 goto * expression ;
4752 Objective-C:
4754 statement:
4755 objc-throw-statement
4756 objc-try-catch-statement
4757 objc-synchronized-statement
4759 objc-throw-statement:
4760 @throw expression ;
4761 @throw ;
4763 OpenMP:
4765 statement:
4766 openmp-construct
4768 openmp-construct:
4769 parallel-construct
4770 for-construct
4771 simd-construct
4772 for-simd-construct
4773 sections-construct
4774 single-construct
4775 parallel-for-construct
4776 parallel-for-simd-construct
4777 parallel-sections-construct
4778 master-construct
4779 critical-construct
4780 atomic-construct
4781 ordered-construct
4783 parallel-construct:
4784 parallel-directive structured-block
4786 for-construct:
4787 for-directive iteration-statement
4789 simd-construct:
4790 simd-directive iteration-statements
4792 for-simd-construct:
4793 for-simd-directive iteration-statements
4795 sections-construct:
4796 sections-directive section-scope
4798 single-construct:
4799 single-directive structured-block
4801 parallel-for-construct:
4802 parallel-for-directive iteration-statement
4804 parallel-for-simd-construct:
4805 parallel-for-simd-directive iteration-statement
4807 parallel-sections-construct:
4808 parallel-sections-directive section-scope
4810 master-construct:
4811 master-directive structured-block
4813 critical-construct:
4814 critical-directive structured-block
4816 atomic-construct:
4817 atomic-directive expression-statement
4819 ordered-construct:
4820 ordered-directive structured-block
4822 Transactional Memory:
4824 statement:
4825 transaction-statement
4826 transaction-cancel-statement
4829 static void
4830 c_parser_statement (c_parser *parser)
4832 while (c_parser_next_token_is_keyword (parser, RID_CASE)
4833 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
4834 || (c_parser_next_token_is (parser, CPP_NAME)
4835 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
4836 c_parser_label (parser);
4837 c_parser_statement_after_labels (parser);
4840 /* Parse a statement, other than a labeled statement. */
4842 static void
4843 c_parser_statement_after_labels (c_parser *parser)
4845 location_t loc = c_parser_peek_token (parser)->location;
4846 tree stmt = NULL_TREE;
4847 bool in_if_block = parser->in_if_block;
4848 parser->in_if_block = false;
4849 switch (c_parser_peek_token (parser)->type)
4851 case CPP_OPEN_BRACE:
4852 add_stmt (c_parser_compound_statement (parser));
4853 break;
4854 case CPP_KEYWORD:
4855 switch (c_parser_peek_token (parser)->keyword)
4857 case RID_IF:
4858 c_parser_if_statement (parser);
4859 break;
4860 case RID_SWITCH:
4861 c_parser_switch_statement (parser);
4862 break;
4863 case RID_WHILE:
4864 c_parser_while_statement (parser, false);
4865 break;
4866 case RID_DO:
4867 c_parser_do_statement (parser, false);
4868 break;
4869 case RID_FOR:
4870 c_parser_for_statement (parser, false);
4871 break;
4872 case RID_CILK_SYNC:
4873 c_parser_consume_token (parser);
4874 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
4875 if (!flag_cilkplus)
4876 error_at (loc, "-fcilkplus must be enabled to use %<_Cilk_sync%>");
4877 else
4878 add_stmt (build_cilk_sync ());
4879 break;
4880 case RID_GOTO:
4881 c_parser_consume_token (parser);
4882 if (c_parser_next_token_is (parser, CPP_NAME))
4884 stmt = c_finish_goto_label (loc,
4885 c_parser_peek_token (parser)->value);
4886 c_parser_consume_token (parser);
4888 else if (c_parser_next_token_is (parser, CPP_MULT))
4890 struct c_expr val;
4892 c_parser_consume_token (parser);
4893 val = c_parser_expression (parser);
4894 val = convert_lvalue_to_rvalue (loc, val, false, true);
4895 stmt = c_finish_goto_ptr (loc, val.value);
4897 else
4898 c_parser_error (parser, "expected identifier or %<*%>");
4899 goto expect_semicolon;
4900 case RID_CONTINUE:
4901 c_parser_consume_token (parser);
4902 stmt = c_finish_bc_stmt (loc, &c_cont_label, false);
4903 goto expect_semicolon;
4904 case RID_BREAK:
4905 c_parser_consume_token (parser);
4906 stmt = c_finish_bc_stmt (loc, &c_break_label, true);
4907 goto expect_semicolon;
4908 case RID_RETURN:
4909 c_parser_consume_token (parser);
4910 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
4912 stmt = c_finish_return (loc, NULL_TREE, NULL_TREE);
4913 c_parser_consume_token (parser);
4915 else
4917 struct c_expr expr = c_parser_expression_conv (parser);
4918 mark_exp_read (expr.value);
4919 stmt = c_finish_return (loc, expr.value, expr.original_type);
4920 goto expect_semicolon;
4922 break;
4923 case RID_ASM:
4924 stmt = c_parser_asm_statement (parser);
4925 break;
4926 case RID_TRANSACTION_ATOMIC:
4927 case RID_TRANSACTION_RELAXED:
4928 stmt = c_parser_transaction (parser,
4929 c_parser_peek_token (parser)->keyword);
4930 break;
4931 case RID_TRANSACTION_CANCEL:
4932 stmt = c_parser_transaction_cancel (parser);
4933 goto expect_semicolon;
4934 case RID_AT_THROW:
4935 gcc_assert (c_dialect_objc ());
4936 c_parser_consume_token (parser);
4937 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
4939 stmt = objc_build_throw_stmt (loc, NULL_TREE);
4940 c_parser_consume_token (parser);
4942 else
4944 struct c_expr expr = c_parser_expression (parser);
4945 expr = convert_lvalue_to_rvalue (loc, expr, false, false);
4946 expr.value = c_fully_fold (expr.value, false, NULL);
4947 stmt = objc_build_throw_stmt (loc, expr.value);
4948 goto expect_semicolon;
4950 break;
4951 case RID_AT_TRY:
4952 gcc_assert (c_dialect_objc ());
4953 c_parser_objc_try_catch_finally_statement (parser);
4954 break;
4955 case RID_AT_SYNCHRONIZED:
4956 gcc_assert (c_dialect_objc ());
4957 c_parser_objc_synchronized_statement (parser);
4958 break;
4959 default:
4960 goto expr_stmt;
4962 break;
4963 case CPP_SEMICOLON:
4964 c_parser_consume_token (parser);
4965 break;
4966 case CPP_CLOSE_PAREN:
4967 case CPP_CLOSE_SQUARE:
4968 /* Avoid infinite loop in error recovery:
4969 c_parser_skip_until_found stops at a closing nesting
4970 delimiter without consuming it, but here we need to consume
4971 it to proceed further. */
4972 c_parser_error (parser, "expected statement");
4973 c_parser_consume_token (parser);
4974 break;
4975 case CPP_PRAGMA:
4976 c_parser_pragma (parser, pragma_stmt);
4977 break;
4978 default:
4979 expr_stmt:
4980 stmt = c_finish_expr_stmt (loc, c_parser_expression_conv (parser).value);
4981 expect_semicolon:
4982 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
4983 break;
4985 /* Two cases cannot and do not have line numbers associated: If stmt
4986 is degenerate, such as "2;", then stmt is an INTEGER_CST, which
4987 cannot hold line numbers. But that's OK because the statement
4988 will either be changed to a MODIFY_EXPR during gimplification of
4989 the statement expr, or discarded. If stmt was compound, but
4990 without new variables, we will have skipped the creation of a
4991 BIND and will have a bare STATEMENT_LIST. But that's OK because
4992 (recursively) all of the component statements should already have
4993 line numbers assigned. ??? Can we discard no-op statements
4994 earlier? */
4995 if (CAN_HAVE_LOCATION_P (stmt)
4996 && EXPR_LOCATION (stmt) == UNKNOWN_LOCATION)
4997 SET_EXPR_LOCATION (stmt, loc);
4999 parser->in_if_block = in_if_block;
5002 /* Parse the condition from an if, do, while or for statements. */
5004 static tree
5005 c_parser_condition (c_parser *parser)
5007 location_t loc = c_parser_peek_token (parser)->location;
5008 tree cond;
5009 cond = c_parser_expression_conv (parser).value;
5010 cond = c_objc_common_truthvalue_conversion (loc, cond);
5011 cond = c_fully_fold (cond, false, NULL);
5012 if (warn_sequence_point)
5013 verify_sequence_points (cond);
5014 return cond;
5017 /* Parse a parenthesized condition from an if, do or while statement.
5019 condition:
5020 ( expression )
5022 static tree
5023 c_parser_paren_condition (c_parser *parser)
5025 tree cond;
5026 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5027 return error_mark_node;
5028 cond = c_parser_condition (parser);
5029 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
5030 return cond;
5033 /* Parse a statement which is a block in C99. */
5035 static tree
5036 c_parser_c99_block_statement (c_parser *parser)
5038 tree block = c_begin_compound_stmt (flag_isoc99);
5039 location_t loc = c_parser_peek_token (parser)->location;
5040 c_parser_statement (parser);
5041 return c_end_compound_stmt (loc, block, flag_isoc99);
5044 /* Parse the body of an if statement. This is just parsing a
5045 statement but (a) it is a block in C99, (b) we track whether the
5046 body is an if statement for the sake of -Wparentheses warnings, (c)
5047 we handle an empty body specially for the sake of -Wempty-body
5048 warnings, and (d) we call parser_compound_statement directly
5049 because c_parser_statement_after_labels resets
5050 parser->in_if_block. */
5052 static tree
5053 c_parser_if_body (c_parser *parser, bool *if_p)
5055 tree block = c_begin_compound_stmt (flag_isoc99);
5056 location_t body_loc = c_parser_peek_token (parser)->location;
5057 while (c_parser_next_token_is_keyword (parser, RID_CASE)
5058 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
5059 || (c_parser_next_token_is (parser, CPP_NAME)
5060 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
5061 c_parser_label (parser);
5062 *if_p = c_parser_next_token_is_keyword (parser, RID_IF);
5063 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5065 location_t loc = c_parser_peek_token (parser)->location;
5066 add_stmt (build_empty_stmt (loc));
5067 c_parser_consume_token (parser);
5068 if (!c_parser_next_token_is_keyword (parser, RID_ELSE))
5069 warning_at (loc, OPT_Wempty_body,
5070 "suggest braces around empty body in an %<if%> statement");
5072 else if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
5073 add_stmt (c_parser_compound_statement (parser));
5074 else
5075 c_parser_statement_after_labels (parser);
5076 return c_end_compound_stmt (body_loc, block, flag_isoc99);
5079 /* Parse the else body of an if statement. This is just parsing a
5080 statement but (a) it is a block in C99, (b) we handle an empty body
5081 specially for the sake of -Wempty-body warnings. */
5083 static tree
5084 c_parser_else_body (c_parser *parser)
5086 location_t else_loc = c_parser_peek_token (parser)->location;
5087 tree block = c_begin_compound_stmt (flag_isoc99);
5088 while (c_parser_next_token_is_keyword (parser, RID_CASE)
5089 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
5090 || (c_parser_next_token_is (parser, CPP_NAME)
5091 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
5092 c_parser_label (parser);
5093 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5095 location_t loc = c_parser_peek_token (parser)->location;
5096 warning_at (loc,
5097 OPT_Wempty_body,
5098 "suggest braces around empty body in an %<else%> statement");
5099 add_stmt (build_empty_stmt (loc));
5100 c_parser_consume_token (parser);
5102 else
5103 c_parser_statement_after_labels (parser);
5104 return c_end_compound_stmt (else_loc, block, flag_isoc99);
5107 /* Parse an if statement (C90 6.6.4, C99 6.8.4).
5109 if-statement:
5110 if ( expression ) statement
5111 if ( expression ) statement else statement
5114 static void
5115 c_parser_if_statement (c_parser *parser)
5117 tree block;
5118 location_t loc;
5119 tree cond;
5120 bool first_if = false;
5121 tree first_body, second_body;
5122 bool in_if_block;
5123 tree if_stmt;
5125 gcc_assert (c_parser_next_token_is_keyword (parser, RID_IF));
5126 c_parser_consume_token (parser);
5127 block = c_begin_compound_stmt (flag_isoc99);
5128 loc = c_parser_peek_token (parser)->location;
5129 cond = c_parser_paren_condition (parser);
5130 in_if_block = parser->in_if_block;
5131 parser->in_if_block = true;
5132 first_body = c_parser_if_body (parser, &first_if);
5133 parser->in_if_block = in_if_block;
5134 if (c_parser_next_token_is_keyword (parser, RID_ELSE))
5136 c_parser_consume_token (parser);
5137 second_body = c_parser_else_body (parser);
5139 else
5140 second_body = NULL_TREE;
5141 c_finish_if_stmt (loc, cond, first_body, second_body, first_if);
5142 if_stmt = c_end_compound_stmt (loc, block, flag_isoc99);
5144 /* If the if statement contains array notations, then we expand them. */
5145 if (flag_cilkplus && contains_array_notation_expr (if_stmt))
5146 if_stmt = fix_conditional_array_notations (if_stmt);
5147 add_stmt (if_stmt);
5150 /* Parse a switch statement (C90 6.6.4, C99 6.8.4).
5152 switch-statement:
5153 switch (expression) statement
5156 static void
5157 c_parser_switch_statement (c_parser *parser)
5159 struct c_expr ce;
5160 tree block, expr, body, save_break;
5161 location_t switch_loc = c_parser_peek_token (parser)->location;
5162 location_t switch_cond_loc;
5163 gcc_assert (c_parser_next_token_is_keyword (parser, RID_SWITCH));
5164 c_parser_consume_token (parser);
5165 block = c_begin_compound_stmt (flag_isoc99);
5166 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5168 switch_cond_loc = c_parser_peek_token (parser)->location;
5169 ce = c_parser_expression (parser);
5170 ce = convert_lvalue_to_rvalue (switch_cond_loc, ce, true, false);
5171 expr = ce.value;
5172 if (flag_cilkplus && contains_array_notation_expr (expr))
5174 error_at (switch_cond_loc,
5175 "array notations cannot be used as a condition for switch "
5176 "statement");
5177 expr = error_mark_node;
5179 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
5181 else
5183 switch_cond_loc = UNKNOWN_LOCATION;
5184 expr = error_mark_node;
5186 c_start_case (switch_loc, switch_cond_loc, expr);
5187 save_break = c_break_label;
5188 c_break_label = NULL_TREE;
5189 body = c_parser_c99_block_statement (parser);
5190 c_finish_case (body);
5191 if (c_break_label)
5193 location_t here = c_parser_peek_token (parser)->location;
5194 tree t = build1 (LABEL_EXPR, void_type_node, c_break_label);
5195 SET_EXPR_LOCATION (t, here);
5196 add_stmt (t);
5198 c_break_label = save_break;
5199 add_stmt (c_end_compound_stmt (switch_loc, block, flag_isoc99));
5202 /* Parse a while statement (C90 6.6.5, C99 6.8.5).
5204 while-statement:
5205 while (expression) statement
5208 static void
5209 c_parser_while_statement (c_parser *parser, bool ivdep)
5211 tree block, cond, body, save_break, save_cont;
5212 location_t loc;
5213 gcc_assert (c_parser_next_token_is_keyword (parser, RID_WHILE));
5214 c_parser_consume_token (parser);
5215 block = c_begin_compound_stmt (flag_isoc99);
5216 loc = c_parser_peek_token (parser)->location;
5217 cond = c_parser_paren_condition (parser);
5218 if (flag_cilkplus && contains_array_notation_expr (cond))
5220 error_at (loc, "array notations cannot be used as a condition for while "
5221 "statement");
5222 cond = error_mark_node;
5225 if (ivdep && cond != error_mark_node)
5226 cond = build2 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
5227 build_int_cst (integer_type_node,
5228 annot_expr_ivdep_kind));
5229 save_break = c_break_label;
5230 c_break_label = NULL_TREE;
5231 save_cont = c_cont_label;
5232 c_cont_label = NULL_TREE;
5233 body = c_parser_c99_block_statement (parser);
5234 c_finish_loop (loc, cond, NULL, body, c_break_label, c_cont_label, true);
5235 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99));
5236 c_break_label = save_break;
5237 c_cont_label = save_cont;
5240 /* Parse a do statement (C90 6.6.5, C99 6.8.5).
5242 do-statement:
5243 do statement while ( expression ) ;
5246 static void
5247 c_parser_do_statement (c_parser *parser, bool ivdep)
5249 tree block, cond, body, save_break, save_cont, new_break, new_cont;
5250 location_t loc;
5251 gcc_assert (c_parser_next_token_is_keyword (parser, RID_DO));
5252 c_parser_consume_token (parser);
5253 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5254 warning_at (c_parser_peek_token (parser)->location,
5255 OPT_Wempty_body,
5256 "suggest braces around empty body in %<do%> statement");
5257 block = c_begin_compound_stmt (flag_isoc99);
5258 loc = c_parser_peek_token (parser)->location;
5259 save_break = c_break_label;
5260 c_break_label = NULL_TREE;
5261 save_cont = c_cont_label;
5262 c_cont_label = NULL_TREE;
5263 body = c_parser_c99_block_statement (parser);
5264 c_parser_require_keyword (parser, RID_WHILE, "expected %<while%>");
5265 new_break = c_break_label;
5266 c_break_label = save_break;
5267 new_cont = c_cont_label;
5268 c_cont_label = save_cont;
5269 cond = c_parser_paren_condition (parser);
5270 if (flag_cilkplus && contains_array_notation_expr (cond))
5272 error_at (loc, "array notations cannot be used as a condition for a "
5273 "do-while statement");
5274 cond = error_mark_node;
5276 if (ivdep && cond != error_mark_node)
5277 cond = build2 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
5278 build_int_cst (integer_type_node,
5279 annot_expr_ivdep_kind));
5280 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
5281 c_parser_skip_to_end_of_block_or_statement (parser);
5282 c_finish_loop (loc, cond, NULL, body, new_break, new_cont, false);
5283 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99));
5286 /* Parse a for statement (C90 6.6.5, C99 6.8.5).
5288 for-statement:
5289 for ( expression[opt] ; expression[opt] ; expression[opt] ) statement
5290 for ( nested-declaration expression[opt] ; expression[opt] ) statement
5292 The form with a declaration is new in C99.
5294 ??? In accordance with the old parser, the declaration may be a
5295 nested function, which is then rejected in check_for_loop_decls,
5296 but does it make any sense for this to be included in the grammar?
5297 Note in particular that the nested function does not include a
5298 trailing ';', whereas the "declaration" production includes one.
5299 Also, can we reject bad declarations earlier and cheaper than
5300 check_for_loop_decls?
5302 In Objective-C, there are two additional variants:
5304 foreach-statement:
5305 for ( expression in expresssion ) statement
5306 for ( declaration in expression ) statement
5308 This is inconsistent with C, because the second variant is allowed
5309 even if c99 is not enabled.
5311 The rest of the comment documents these Objective-C foreach-statement.
5313 Here is the canonical example of the first variant:
5314 for (object in array) { do something with object }
5315 we call the first expression ("object") the "object_expression" and
5316 the second expression ("array") the "collection_expression".
5317 object_expression must be an lvalue of type "id" (a generic Objective-C
5318 object) because the loop works by assigning to object_expression the
5319 various objects from the collection_expression. collection_expression
5320 must evaluate to something of type "id" which responds to the method
5321 countByEnumeratingWithState:objects:count:.
5323 The canonical example of the second variant is:
5324 for (id object in array) { do something with object }
5325 which is completely equivalent to
5327 id object;
5328 for (object in array) { do something with object }
5330 Note that initizializing 'object' in some way (eg, "for ((object =
5331 xxx) in array) { do something with object }") is possibly
5332 technically valid, but completely pointless as 'object' will be
5333 assigned to something else as soon as the loop starts. We should
5334 most likely reject it (TODO).
5336 The beginning of the Objective-C foreach-statement looks exactly
5337 like the beginning of the for-statement, and we can tell it is a
5338 foreach-statement only because the initial declaration or
5339 expression is terminated by 'in' instead of ';'.
5342 static void
5343 c_parser_for_statement (c_parser *parser, bool ivdep)
5345 tree block, cond, incr, save_break, save_cont, body;
5346 /* The following are only used when parsing an ObjC foreach statement. */
5347 tree object_expression;
5348 /* Silence the bogus uninitialized warning. */
5349 tree collection_expression = NULL;
5350 location_t loc = c_parser_peek_token (parser)->location;
5351 location_t for_loc = c_parser_peek_token (parser)->location;
5352 bool is_foreach_statement = false;
5353 gcc_assert (c_parser_next_token_is_keyword (parser, RID_FOR));
5354 c_parser_consume_token (parser);
5355 /* Open a compound statement in Objective-C as well, just in case this is
5356 as foreach expression. */
5357 block = c_begin_compound_stmt (flag_isoc99 || c_dialect_objc ());
5358 cond = error_mark_node;
5359 incr = error_mark_node;
5360 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5362 /* Parse the initialization declaration or expression. */
5363 object_expression = error_mark_node;
5364 parser->objc_could_be_foreach_context = c_dialect_objc ();
5365 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5367 parser->objc_could_be_foreach_context = false;
5368 c_parser_consume_token (parser);
5369 c_finish_expr_stmt (loc, NULL_TREE);
5371 else if (c_parser_next_tokens_start_declaration (parser))
5373 c_parser_declaration_or_fndef (parser, true, true, true, true, true,
5374 &object_expression, vNULL);
5375 parser->objc_could_be_foreach_context = false;
5377 if (c_parser_next_token_is_keyword (parser, RID_IN))
5379 c_parser_consume_token (parser);
5380 is_foreach_statement = true;
5381 if (check_for_loop_decls (for_loc, true) == NULL_TREE)
5382 c_parser_error (parser, "multiple iterating variables in fast enumeration");
5384 else
5385 check_for_loop_decls (for_loc, flag_isoc99);
5387 else if (c_parser_next_token_is_keyword (parser, RID_EXTENSION))
5389 /* __extension__ can start a declaration, but is also an
5390 unary operator that can start an expression. Consume all
5391 but the last of a possible series of __extension__ to
5392 determine which. */
5393 while (c_parser_peek_2nd_token (parser)->type == CPP_KEYWORD
5394 && (c_parser_peek_2nd_token (parser)->keyword
5395 == RID_EXTENSION))
5396 c_parser_consume_token (parser);
5397 if (c_token_starts_declaration (c_parser_peek_2nd_token (parser)))
5399 int ext;
5400 ext = disable_extension_diagnostics ();
5401 c_parser_consume_token (parser);
5402 c_parser_declaration_or_fndef (parser, true, true, true, true,
5403 true, &object_expression, vNULL);
5404 parser->objc_could_be_foreach_context = false;
5406 restore_extension_diagnostics (ext);
5407 if (c_parser_next_token_is_keyword (parser, RID_IN))
5409 c_parser_consume_token (parser);
5410 is_foreach_statement = true;
5411 if (check_for_loop_decls (for_loc, true) == NULL_TREE)
5412 c_parser_error (parser, "multiple iterating variables in fast enumeration");
5414 else
5415 check_for_loop_decls (for_loc, flag_isoc99);
5417 else
5418 goto init_expr;
5420 else
5422 init_expr:
5424 struct c_expr ce;
5425 tree init_expression;
5426 ce = c_parser_expression (parser);
5427 init_expression = ce.value;
5428 parser->objc_could_be_foreach_context = false;
5429 if (c_parser_next_token_is_keyword (parser, RID_IN))
5431 c_parser_consume_token (parser);
5432 is_foreach_statement = true;
5433 if (! lvalue_p (init_expression))
5434 c_parser_error (parser, "invalid iterating variable in fast enumeration");
5435 object_expression = c_fully_fold (init_expression, false, NULL);
5437 else
5439 ce = convert_lvalue_to_rvalue (loc, ce, true, false);
5440 init_expression = ce.value;
5441 c_finish_expr_stmt (loc, init_expression);
5442 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
5446 /* Parse the loop condition. In the case of a foreach
5447 statement, there is no loop condition. */
5448 gcc_assert (!parser->objc_could_be_foreach_context);
5449 if (!is_foreach_statement)
5451 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5453 if (ivdep)
5455 c_parser_error (parser, "missing loop condition in loop with "
5456 "%<GCC ivdep%> pragma");
5457 cond = error_mark_node;
5459 else
5461 c_parser_consume_token (parser);
5462 cond = NULL_TREE;
5465 else
5467 cond = c_parser_condition (parser);
5468 if (flag_cilkplus && contains_array_notation_expr (cond))
5470 error_at (loc, "array notations cannot be used in a "
5471 "condition for a for-loop");
5472 cond = error_mark_node;
5474 c_parser_skip_until_found (parser, CPP_SEMICOLON,
5475 "expected %<;%>");
5477 if (ivdep && cond != error_mark_node)
5478 cond = build2 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
5479 build_int_cst (integer_type_node,
5480 annot_expr_ivdep_kind));
5482 /* Parse the increment expression (the third expression in a
5483 for-statement). In the case of a foreach-statement, this is
5484 the expression that follows the 'in'. */
5485 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
5487 if (is_foreach_statement)
5489 c_parser_error (parser, "missing collection in fast enumeration");
5490 collection_expression = error_mark_node;
5492 else
5493 incr = c_process_expr_stmt (loc, NULL_TREE);
5495 else
5497 if (is_foreach_statement)
5498 collection_expression = c_fully_fold (c_parser_expression (parser).value,
5499 false, NULL);
5500 else
5502 struct c_expr ce = c_parser_expression (parser);
5503 ce = convert_lvalue_to_rvalue (loc, ce, true, false);
5504 incr = c_process_expr_stmt (loc, ce.value);
5507 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
5509 save_break = c_break_label;
5510 c_break_label = NULL_TREE;
5511 save_cont = c_cont_label;
5512 c_cont_label = NULL_TREE;
5513 body = c_parser_c99_block_statement (parser);
5514 if (is_foreach_statement)
5515 objc_finish_foreach_loop (loc, object_expression, collection_expression, body, c_break_label, c_cont_label);
5516 else
5517 c_finish_loop (loc, cond, incr, body, c_break_label, c_cont_label, true);
5518 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99 || c_dialect_objc ()));
5519 c_break_label = save_break;
5520 c_cont_label = save_cont;
5523 /* Parse an asm statement, a GNU extension. This is a full-blown asm
5524 statement with inputs, outputs, clobbers, and volatile tag
5525 allowed.
5527 asm-statement:
5528 asm type-qualifier[opt] ( asm-argument ) ;
5529 asm type-qualifier[opt] goto ( asm-goto-argument ) ;
5531 asm-argument:
5532 asm-string-literal
5533 asm-string-literal : asm-operands[opt]
5534 asm-string-literal : asm-operands[opt] : asm-operands[opt]
5535 asm-string-literal : asm-operands[opt] : asm-operands[opt] : asm-clobbers[opt]
5537 asm-goto-argument:
5538 asm-string-literal : : asm-operands[opt] : asm-clobbers[opt] \
5539 : asm-goto-operands
5541 Qualifiers other than volatile are accepted in the syntax but
5542 warned for. */
5544 static tree
5545 c_parser_asm_statement (c_parser *parser)
5547 tree quals, str, outputs, inputs, clobbers, labels, ret;
5548 bool simple, is_goto;
5549 location_t asm_loc = c_parser_peek_token (parser)->location;
5550 int section, nsections;
5552 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ASM));
5553 c_parser_consume_token (parser);
5554 if (c_parser_next_token_is_keyword (parser, RID_VOLATILE))
5556 quals = c_parser_peek_token (parser)->value;
5557 c_parser_consume_token (parser);
5559 else if (c_parser_next_token_is_keyword (parser, RID_CONST)
5560 || c_parser_next_token_is_keyword (parser, RID_RESTRICT))
5562 warning_at (c_parser_peek_token (parser)->location,
5564 "%E qualifier ignored on asm",
5565 c_parser_peek_token (parser)->value);
5566 quals = NULL_TREE;
5567 c_parser_consume_token (parser);
5569 else
5570 quals = NULL_TREE;
5572 is_goto = false;
5573 if (c_parser_next_token_is_keyword (parser, RID_GOTO))
5575 c_parser_consume_token (parser);
5576 is_goto = true;
5579 /* ??? Follow the C++ parser rather than using the
5580 lex_untranslated_string kludge. */
5581 parser->lex_untranslated_string = true;
5582 ret = NULL;
5584 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5585 goto error;
5587 str = c_parser_asm_string_literal (parser);
5588 if (str == NULL_TREE)
5589 goto error_close_paren;
5591 simple = true;
5592 outputs = NULL_TREE;
5593 inputs = NULL_TREE;
5594 clobbers = NULL_TREE;
5595 labels = NULL_TREE;
5597 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN) && !is_goto)
5598 goto done_asm;
5600 /* Parse each colon-delimited section of operands. */
5601 nsections = 3 + is_goto;
5602 for (section = 0; section < nsections; ++section)
5604 if (!c_parser_require (parser, CPP_COLON,
5605 is_goto
5606 ? "expected %<:%>"
5607 : "expected %<:%> or %<)%>"))
5608 goto error_close_paren;
5610 /* Once past any colon, we're no longer a simple asm. */
5611 simple = false;
5613 if ((!c_parser_next_token_is (parser, CPP_COLON)
5614 && !c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
5615 || section == 3)
5616 switch (section)
5618 case 0:
5619 /* For asm goto, we don't allow output operands, but reserve
5620 the slot for a future extension that does allow them. */
5621 if (!is_goto)
5622 outputs = c_parser_asm_operands (parser);
5623 break;
5624 case 1:
5625 inputs = c_parser_asm_operands (parser);
5626 break;
5627 case 2:
5628 clobbers = c_parser_asm_clobbers (parser);
5629 break;
5630 case 3:
5631 labels = c_parser_asm_goto_operands (parser);
5632 break;
5633 default:
5634 gcc_unreachable ();
5637 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN) && !is_goto)
5638 goto done_asm;
5641 done_asm:
5642 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
5644 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5645 goto error;
5648 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
5649 c_parser_skip_to_end_of_block_or_statement (parser);
5651 ret = build_asm_stmt (quals, build_asm_expr (asm_loc, str, outputs, inputs,
5652 clobbers, labels, simple));
5654 error:
5655 parser->lex_untranslated_string = false;
5656 return ret;
5658 error_close_paren:
5659 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5660 goto error;
5663 /* Parse asm operands, a GNU extension.
5665 asm-operands:
5666 asm-operand
5667 asm-operands , asm-operand
5669 asm-operand:
5670 asm-string-literal ( expression )
5671 [ identifier ] asm-string-literal ( expression )
5674 static tree
5675 c_parser_asm_operands (c_parser *parser)
5677 tree list = NULL_TREE;
5678 while (true)
5680 tree name, str;
5681 struct c_expr expr;
5682 if (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
5684 c_parser_consume_token (parser);
5685 if (c_parser_next_token_is (parser, CPP_NAME))
5687 tree id = c_parser_peek_token (parser)->value;
5688 c_parser_consume_token (parser);
5689 name = build_string (IDENTIFIER_LENGTH (id),
5690 IDENTIFIER_POINTER (id));
5692 else
5694 c_parser_error (parser, "expected identifier");
5695 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
5696 return NULL_TREE;
5698 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
5699 "expected %<]%>");
5701 else
5702 name = NULL_TREE;
5703 str = c_parser_asm_string_literal (parser);
5704 if (str == NULL_TREE)
5705 return NULL_TREE;
5706 parser->lex_untranslated_string = false;
5707 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5709 parser->lex_untranslated_string = true;
5710 return NULL_TREE;
5712 expr = c_parser_expression (parser);
5713 mark_exp_read (expr.value);
5714 parser->lex_untranslated_string = true;
5715 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
5717 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5718 return NULL_TREE;
5720 list = chainon (list, build_tree_list (build_tree_list (name, str),
5721 expr.value));
5722 if (c_parser_next_token_is (parser, CPP_COMMA))
5723 c_parser_consume_token (parser);
5724 else
5725 break;
5727 return list;
5730 /* Parse asm clobbers, a GNU extension.
5732 asm-clobbers:
5733 asm-string-literal
5734 asm-clobbers , asm-string-literal
5737 static tree
5738 c_parser_asm_clobbers (c_parser *parser)
5740 tree list = NULL_TREE;
5741 while (true)
5743 tree str = c_parser_asm_string_literal (parser);
5744 if (str)
5745 list = tree_cons (NULL_TREE, str, list);
5746 else
5747 return NULL_TREE;
5748 if (c_parser_next_token_is (parser, CPP_COMMA))
5749 c_parser_consume_token (parser);
5750 else
5751 break;
5753 return list;
5756 /* Parse asm goto labels, a GNU extension.
5758 asm-goto-operands:
5759 identifier
5760 asm-goto-operands , identifier
5763 static tree
5764 c_parser_asm_goto_operands (c_parser *parser)
5766 tree list = NULL_TREE;
5767 while (true)
5769 tree name, label;
5771 if (c_parser_next_token_is (parser, CPP_NAME))
5773 c_token *tok = c_parser_peek_token (parser);
5774 name = tok->value;
5775 label = lookup_label_for_goto (tok->location, name);
5776 c_parser_consume_token (parser);
5777 TREE_USED (label) = 1;
5779 else
5781 c_parser_error (parser, "expected identifier");
5782 return NULL_TREE;
5785 name = build_string (IDENTIFIER_LENGTH (name),
5786 IDENTIFIER_POINTER (name));
5787 list = tree_cons (name, label, list);
5788 if (c_parser_next_token_is (parser, CPP_COMMA))
5789 c_parser_consume_token (parser);
5790 else
5791 return nreverse (list);
5795 /* Parse an expression other than a compound expression; that is, an
5796 assignment expression (C90 6.3.16, C99 6.5.16). If AFTER is not
5797 NULL then it is an Objective-C message expression which is the
5798 primary-expression starting the expression as an initializer.
5800 assignment-expression:
5801 conditional-expression
5802 unary-expression assignment-operator assignment-expression
5804 assignment-operator: one of
5805 = *= /= %= += -= <<= >>= &= ^= |=
5807 In GNU C we accept any conditional expression on the LHS and
5808 diagnose the invalid lvalue rather than producing a syntax
5809 error. */
5811 static struct c_expr
5812 c_parser_expr_no_commas (c_parser *parser, struct c_expr *after,
5813 tree omp_atomic_lhs)
5815 struct c_expr lhs, rhs, ret;
5816 enum tree_code code;
5817 location_t op_location, exp_location;
5818 gcc_assert (!after || c_dialect_objc ());
5819 lhs = c_parser_conditional_expression (parser, after, omp_atomic_lhs);
5820 op_location = c_parser_peek_token (parser)->location;
5821 switch (c_parser_peek_token (parser)->type)
5823 case CPP_EQ:
5824 code = NOP_EXPR;
5825 break;
5826 case CPP_MULT_EQ:
5827 code = MULT_EXPR;
5828 break;
5829 case CPP_DIV_EQ:
5830 code = TRUNC_DIV_EXPR;
5831 break;
5832 case CPP_MOD_EQ:
5833 code = TRUNC_MOD_EXPR;
5834 break;
5835 case CPP_PLUS_EQ:
5836 code = PLUS_EXPR;
5837 break;
5838 case CPP_MINUS_EQ:
5839 code = MINUS_EXPR;
5840 break;
5841 case CPP_LSHIFT_EQ:
5842 code = LSHIFT_EXPR;
5843 break;
5844 case CPP_RSHIFT_EQ:
5845 code = RSHIFT_EXPR;
5846 break;
5847 case CPP_AND_EQ:
5848 code = BIT_AND_EXPR;
5849 break;
5850 case CPP_XOR_EQ:
5851 code = BIT_XOR_EXPR;
5852 break;
5853 case CPP_OR_EQ:
5854 code = BIT_IOR_EXPR;
5855 break;
5856 default:
5857 return lhs;
5859 c_parser_consume_token (parser);
5860 exp_location = c_parser_peek_token (parser)->location;
5861 rhs = c_parser_expr_no_commas (parser, NULL);
5862 rhs = convert_lvalue_to_rvalue (exp_location, rhs, true, true);
5864 ret.value = build_modify_expr (op_location, lhs.value, lhs.original_type,
5865 code, exp_location, rhs.value,
5866 rhs.original_type);
5867 if (code == NOP_EXPR)
5868 ret.original_code = MODIFY_EXPR;
5869 else
5871 TREE_NO_WARNING (ret.value) = 1;
5872 ret.original_code = ERROR_MARK;
5874 ret.original_type = NULL;
5875 return ret;
5878 /* Parse a conditional expression (C90 6.3.15, C99 6.5.15). If AFTER
5879 is not NULL then it is an Objective-C message expression which is
5880 the primary-expression starting the expression as an initializer.
5882 conditional-expression:
5883 logical-OR-expression
5884 logical-OR-expression ? expression : conditional-expression
5886 GNU extensions:
5888 conditional-expression:
5889 logical-OR-expression ? : conditional-expression
5892 static struct c_expr
5893 c_parser_conditional_expression (c_parser *parser, struct c_expr *after,
5894 tree omp_atomic_lhs)
5896 struct c_expr cond, exp1, exp2, ret;
5897 location_t cond_loc, colon_loc, middle_loc;
5899 gcc_assert (!after || c_dialect_objc ());
5901 cond = c_parser_binary_expression (parser, after, omp_atomic_lhs);
5903 if (c_parser_next_token_is_not (parser, CPP_QUERY))
5904 return cond;
5905 cond_loc = c_parser_peek_token (parser)->location;
5906 cond = convert_lvalue_to_rvalue (cond_loc, cond, true, true);
5907 c_parser_consume_token (parser);
5908 if (c_parser_next_token_is (parser, CPP_COLON))
5910 tree eptype = NULL_TREE;
5912 middle_loc = c_parser_peek_token (parser)->location;
5913 pedwarn (middle_loc, OPT_Wpedantic,
5914 "ISO C forbids omitting the middle term of a ?: expression");
5915 warn_for_omitted_condop (middle_loc, cond.value);
5916 if (TREE_CODE (cond.value) == EXCESS_PRECISION_EXPR)
5918 eptype = TREE_TYPE (cond.value);
5919 cond.value = TREE_OPERAND (cond.value, 0);
5921 /* Make sure first operand is calculated only once. */
5922 exp1.value = c_save_expr (default_conversion (cond.value));
5923 if (eptype)
5924 exp1.value = build1 (EXCESS_PRECISION_EXPR, eptype, exp1.value);
5925 exp1.original_type = NULL;
5926 cond.value = c_objc_common_truthvalue_conversion (cond_loc, exp1.value);
5927 c_inhibit_evaluation_warnings += cond.value == truthvalue_true_node;
5929 else
5931 cond.value
5932 = c_objc_common_truthvalue_conversion
5933 (cond_loc, default_conversion (cond.value));
5934 c_inhibit_evaluation_warnings += cond.value == truthvalue_false_node;
5935 exp1 = c_parser_expression_conv (parser);
5936 mark_exp_read (exp1.value);
5937 c_inhibit_evaluation_warnings +=
5938 ((cond.value == truthvalue_true_node)
5939 - (cond.value == truthvalue_false_node));
5942 colon_loc = c_parser_peek_token (parser)->location;
5943 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
5945 c_inhibit_evaluation_warnings -= cond.value == truthvalue_true_node;
5946 ret.value = error_mark_node;
5947 ret.original_code = ERROR_MARK;
5948 ret.original_type = NULL;
5949 return ret;
5952 location_t exp2_loc = c_parser_peek_token (parser)->location;
5953 exp2 = c_parser_conditional_expression (parser, NULL, NULL_TREE);
5954 exp2 = convert_lvalue_to_rvalue (exp2_loc, exp2, true, true);
5956 c_inhibit_evaluation_warnings -= cond.value == truthvalue_true_node;
5957 ret.value = build_conditional_expr (colon_loc, cond.value,
5958 cond.original_code == C_MAYBE_CONST_EXPR,
5959 exp1.value, exp1.original_type,
5960 exp2.value, exp2.original_type);
5961 ret.original_code = ERROR_MARK;
5962 if (exp1.value == error_mark_node || exp2.value == error_mark_node)
5963 ret.original_type = NULL;
5964 else
5966 tree t1, t2;
5968 /* If both sides are enum type, the default conversion will have
5969 made the type of the result be an integer type. We want to
5970 remember the enum types we started with. */
5971 t1 = exp1.original_type ? exp1.original_type : TREE_TYPE (exp1.value);
5972 t2 = exp2.original_type ? exp2.original_type : TREE_TYPE (exp2.value);
5973 ret.original_type = ((t1 != error_mark_node
5974 && t2 != error_mark_node
5975 && (TYPE_MAIN_VARIANT (t1)
5976 == TYPE_MAIN_VARIANT (t2)))
5977 ? t1
5978 : NULL);
5980 return ret;
5983 /* Parse a binary expression; that is, a logical-OR-expression (C90
5984 6.3.5-6.3.14, C99 6.5.5-6.5.14). If AFTER is not NULL then it is
5985 an Objective-C message expression which is the primary-expression
5986 starting the expression as an initializer.
5988 OMP_ATOMIC_LHS is NULL, unless parsing OpenMP #pragma omp atomic,
5989 when it should be the unfolded lhs. In a valid OpenMP source,
5990 one of the operands of the toplevel binary expression must be equal
5991 to it. In that case, just return a build2 created binary operation
5992 rather than result of parser_build_binary_op.
5994 multiplicative-expression:
5995 cast-expression
5996 multiplicative-expression * cast-expression
5997 multiplicative-expression / cast-expression
5998 multiplicative-expression % cast-expression
6000 additive-expression:
6001 multiplicative-expression
6002 additive-expression + multiplicative-expression
6003 additive-expression - multiplicative-expression
6005 shift-expression:
6006 additive-expression
6007 shift-expression << additive-expression
6008 shift-expression >> additive-expression
6010 relational-expression:
6011 shift-expression
6012 relational-expression < shift-expression
6013 relational-expression > shift-expression
6014 relational-expression <= shift-expression
6015 relational-expression >= shift-expression
6017 equality-expression:
6018 relational-expression
6019 equality-expression == relational-expression
6020 equality-expression != relational-expression
6022 AND-expression:
6023 equality-expression
6024 AND-expression & equality-expression
6026 exclusive-OR-expression:
6027 AND-expression
6028 exclusive-OR-expression ^ AND-expression
6030 inclusive-OR-expression:
6031 exclusive-OR-expression
6032 inclusive-OR-expression | exclusive-OR-expression
6034 logical-AND-expression:
6035 inclusive-OR-expression
6036 logical-AND-expression && inclusive-OR-expression
6038 logical-OR-expression:
6039 logical-AND-expression
6040 logical-OR-expression || logical-AND-expression
6043 static struct c_expr
6044 c_parser_binary_expression (c_parser *parser, struct c_expr *after,
6045 tree omp_atomic_lhs)
6047 /* A binary expression is parsed using operator-precedence parsing,
6048 with the operands being cast expressions. All the binary
6049 operators are left-associative. Thus a binary expression is of
6050 form:
6052 E0 op1 E1 op2 E2 ...
6054 which we represent on a stack. On the stack, the precedence
6055 levels are strictly increasing. When a new operator is
6056 encountered of higher precedence than that at the top of the
6057 stack, it is pushed; its LHS is the top expression, and its RHS
6058 is everything parsed until it is popped. When a new operator is
6059 encountered with precedence less than or equal to that at the top
6060 of the stack, triples E[i-1] op[i] E[i] are popped and replaced
6061 by the result of the operation until the operator at the top of
6062 the stack has lower precedence than the new operator or there is
6063 only one element on the stack; then the top expression is the LHS
6064 of the new operator. In the case of logical AND and OR
6065 expressions, we also need to adjust c_inhibit_evaluation_warnings
6066 as appropriate when the operators are pushed and popped. */
6068 struct {
6069 /* The expression at this stack level. */
6070 struct c_expr expr;
6071 /* The precedence of the operator on its left, PREC_NONE at the
6072 bottom of the stack. */
6073 enum c_parser_prec prec;
6074 /* The operation on its left. */
6075 enum tree_code op;
6076 /* The source location of this operation. */
6077 location_t loc;
6078 } stack[NUM_PRECS];
6079 int sp;
6080 /* Location of the binary operator. */
6081 location_t binary_loc = UNKNOWN_LOCATION; /* Quiet warning. */
6082 #define POP \
6083 do { \
6084 switch (stack[sp].op) \
6086 case TRUTH_ANDIF_EXPR: \
6087 c_inhibit_evaluation_warnings -= (stack[sp - 1].expr.value \
6088 == truthvalue_false_node); \
6089 break; \
6090 case TRUTH_ORIF_EXPR: \
6091 c_inhibit_evaluation_warnings -= (stack[sp - 1].expr.value \
6092 == truthvalue_true_node); \
6093 break; \
6094 default: \
6095 break; \
6097 stack[sp - 1].expr \
6098 = convert_lvalue_to_rvalue (stack[sp - 1].loc, \
6099 stack[sp - 1].expr, true, true); \
6100 stack[sp].expr \
6101 = convert_lvalue_to_rvalue (stack[sp].loc, \
6102 stack[sp].expr, true, true); \
6103 if (__builtin_expect (omp_atomic_lhs != NULL_TREE, 0) && sp == 1 \
6104 && c_parser_peek_token (parser)->type == CPP_SEMICOLON \
6105 && ((1 << stack[sp].prec) \
6106 & (1 << (PREC_BITOR | PREC_BITXOR | PREC_BITAND | PREC_SHIFT \
6107 | PREC_ADD | PREC_MULT))) \
6108 && stack[sp].op != TRUNC_MOD_EXPR \
6109 && stack[0].expr.value != error_mark_node \
6110 && stack[1].expr.value != error_mark_node \
6111 && (c_tree_equal (stack[0].expr.value, omp_atomic_lhs) \
6112 || c_tree_equal (stack[1].expr.value, omp_atomic_lhs))) \
6113 stack[0].expr.value \
6114 = build2 (stack[1].op, TREE_TYPE (stack[0].expr.value), \
6115 stack[0].expr.value, stack[1].expr.value); \
6116 else \
6117 stack[sp - 1].expr = parser_build_binary_op (stack[sp].loc, \
6118 stack[sp].op, \
6119 stack[sp - 1].expr, \
6120 stack[sp].expr); \
6121 sp--; \
6122 } while (0)
6123 gcc_assert (!after || c_dialect_objc ());
6124 stack[0].loc = c_parser_peek_token (parser)->location;
6125 stack[0].expr = c_parser_cast_expression (parser, after);
6126 stack[0].prec = PREC_NONE;
6127 sp = 0;
6128 while (true)
6130 enum c_parser_prec oprec;
6131 enum tree_code ocode;
6132 if (parser->error)
6133 goto out;
6134 switch (c_parser_peek_token (parser)->type)
6136 case CPP_MULT:
6137 oprec = PREC_MULT;
6138 ocode = MULT_EXPR;
6139 break;
6140 case CPP_DIV:
6141 oprec = PREC_MULT;
6142 ocode = TRUNC_DIV_EXPR;
6143 break;
6144 case CPP_MOD:
6145 oprec = PREC_MULT;
6146 ocode = TRUNC_MOD_EXPR;
6147 break;
6148 case CPP_PLUS:
6149 oprec = PREC_ADD;
6150 ocode = PLUS_EXPR;
6151 break;
6152 case CPP_MINUS:
6153 oprec = PREC_ADD;
6154 ocode = MINUS_EXPR;
6155 break;
6156 case CPP_LSHIFT:
6157 oprec = PREC_SHIFT;
6158 ocode = LSHIFT_EXPR;
6159 break;
6160 case CPP_RSHIFT:
6161 oprec = PREC_SHIFT;
6162 ocode = RSHIFT_EXPR;
6163 break;
6164 case CPP_LESS:
6165 oprec = PREC_REL;
6166 ocode = LT_EXPR;
6167 break;
6168 case CPP_GREATER:
6169 oprec = PREC_REL;
6170 ocode = GT_EXPR;
6171 break;
6172 case CPP_LESS_EQ:
6173 oprec = PREC_REL;
6174 ocode = LE_EXPR;
6175 break;
6176 case CPP_GREATER_EQ:
6177 oprec = PREC_REL;
6178 ocode = GE_EXPR;
6179 break;
6180 case CPP_EQ_EQ:
6181 oprec = PREC_EQ;
6182 ocode = EQ_EXPR;
6183 break;
6184 case CPP_NOT_EQ:
6185 oprec = PREC_EQ;
6186 ocode = NE_EXPR;
6187 break;
6188 case CPP_AND:
6189 oprec = PREC_BITAND;
6190 ocode = BIT_AND_EXPR;
6191 break;
6192 case CPP_XOR:
6193 oprec = PREC_BITXOR;
6194 ocode = BIT_XOR_EXPR;
6195 break;
6196 case CPP_OR:
6197 oprec = PREC_BITOR;
6198 ocode = BIT_IOR_EXPR;
6199 break;
6200 case CPP_AND_AND:
6201 oprec = PREC_LOGAND;
6202 ocode = TRUTH_ANDIF_EXPR;
6203 break;
6204 case CPP_OR_OR:
6205 oprec = PREC_LOGOR;
6206 ocode = TRUTH_ORIF_EXPR;
6207 break;
6208 default:
6209 /* Not a binary operator, so end of the binary
6210 expression. */
6211 goto out;
6213 binary_loc = c_parser_peek_token (parser)->location;
6214 while (oprec <= stack[sp].prec)
6215 POP;
6216 c_parser_consume_token (parser);
6217 switch (ocode)
6219 case TRUTH_ANDIF_EXPR:
6220 stack[sp].expr
6221 = convert_lvalue_to_rvalue (stack[sp].loc,
6222 stack[sp].expr, true, true);
6223 stack[sp].expr.value = c_objc_common_truthvalue_conversion
6224 (stack[sp].loc, default_conversion (stack[sp].expr.value));
6225 c_inhibit_evaluation_warnings += (stack[sp].expr.value
6226 == truthvalue_false_node);
6227 break;
6228 case TRUTH_ORIF_EXPR:
6229 stack[sp].expr
6230 = convert_lvalue_to_rvalue (stack[sp].loc,
6231 stack[sp].expr, true, true);
6232 stack[sp].expr.value = c_objc_common_truthvalue_conversion
6233 (stack[sp].loc, default_conversion (stack[sp].expr.value));
6234 c_inhibit_evaluation_warnings += (stack[sp].expr.value
6235 == truthvalue_true_node);
6236 break;
6237 default:
6238 break;
6240 sp++;
6241 stack[sp].loc = binary_loc;
6242 stack[sp].expr = c_parser_cast_expression (parser, NULL);
6243 stack[sp].prec = oprec;
6244 stack[sp].op = ocode;
6245 stack[sp].loc = binary_loc;
6247 out:
6248 while (sp > 0)
6249 POP;
6250 return stack[0].expr;
6251 #undef POP
6254 /* Parse a cast expression (C90 6.3.4, C99 6.5.4). If AFTER is not
6255 NULL then it is an Objective-C message expression which is the
6256 primary-expression starting the expression as an initializer.
6258 cast-expression:
6259 unary-expression
6260 ( type-name ) unary-expression
6263 static struct c_expr
6264 c_parser_cast_expression (c_parser *parser, struct c_expr *after)
6266 location_t cast_loc = c_parser_peek_token (parser)->location;
6267 gcc_assert (!after || c_dialect_objc ());
6268 if (after)
6269 return c_parser_postfix_expression_after_primary (parser,
6270 cast_loc, *after);
6271 /* If the expression begins with a parenthesized type name, it may
6272 be either a cast or a compound literal; we need to see whether
6273 the next character is '{' to tell the difference. If not, it is
6274 an unary expression. Full detection of unknown typenames here
6275 would require a 3-token lookahead. */
6276 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
6277 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
6279 struct c_type_name *type_name;
6280 struct c_expr ret;
6281 struct c_expr expr;
6282 c_parser_consume_token (parser);
6283 type_name = c_parser_type_name (parser);
6284 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
6285 if (type_name == NULL)
6287 ret.value = error_mark_node;
6288 ret.original_code = ERROR_MARK;
6289 ret.original_type = NULL;
6290 return ret;
6293 /* Save casted types in the function's used types hash table. */
6294 used_types_insert (type_name->specs->type);
6296 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
6297 return c_parser_postfix_expression_after_paren_type (parser, type_name,
6298 cast_loc);
6300 location_t expr_loc = c_parser_peek_token (parser)->location;
6301 expr = c_parser_cast_expression (parser, NULL);
6302 expr = convert_lvalue_to_rvalue (expr_loc, expr, true, true);
6304 ret.value = c_cast_expr (cast_loc, type_name, expr.value);
6305 ret.original_code = ERROR_MARK;
6306 ret.original_type = NULL;
6307 return ret;
6309 else
6310 return c_parser_unary_expression (parser);
6313 /* Parse an unary expression (C90 6.3.3, C99 6.5.3).
6315 unary-expression:
6316 postfix-expression
6317 ++ unary-expression
6318 -- unary-expression
6319 unary-operator cast-expression
6320 sizeof unary-expression
6321 sizeof ( type-name )
6323 unary-operator: one of
6324 & * + - ~ !
6326 GNU extensions:
6328 unary-expression:
6329 __alignof__ unary-expression
6330 __alignof__ ( type-name )
6331 && identifier
6333 (C11 permits _Alignof with type names only.)
6335 unary-operator: one of
6336 __extension__ __real__ __imag__
6338 Transactional Memory:
6340 unary-expression:
6341 transaction-expression
6343 In addition, the GNU syntax treats ++ and -- as unary operators, so
6344 they may be applied to cast expressions with errors for non-lvalues
6345 given later. */
6347 static struct c_expr
6348 c_parser_unary_expression (c_parser *parser)
6350 int ext;
6351 struct c_expr ret, op;
6352 location_t op_loc = c_parser_peek_token (parser)->location;
6353 location_t exp_loc;
6354 ret.original_code = ERROR_MARK;
6355 ret.original_type = NULL;
6356 switch (c_parser_peek_token (parser)->type)
6358 case CPP_PLUS_PLUS:
6359 c_parser_consume_token (parser);
6360 exp_loc = c_parser_peek_token (parser)->location;
6361 op = c_parser_cast_expression (parser, NULL);
6363 /* If there is array notations in op, we expand them. */
6364 if (flag_cilkplus && TREE_CODE (op.value) == ARRAY_NOTATION_REF)
6365 return fix_array_notation_expr (exp_loc, PREINCREMENT_EXPR, op);
6366 else
6368 op = default_function_array_read_conversion (exp_loc, op);
6369 return parser_build_unary_op (op_loc, PREINCREMENT_EXPR, op);
6371 case CPP_MINUS_MINUS:
6372 c_parser_consume_token (parser);
6373 exp_loc = c_parser_peek_token (parser)->location;
6374 op = c_parser_cast_expression (parser, NULL);
6376 /* If there is array notations in op, we expand them. */
6377 if (flag_cilkplus && TREE_CODE (op.value) == ARRAY_NOTATION_REF)
6378 return fix_array_notation_expr (exp_loc, PREDECREMENT_EXPR, op);
6379 else
6381 op = default_function_array_read_conversion (exp_loc, op);
6382 return parser_build_unary_op (op_loc, PREDECREMENT_EXPR, op);
6384 case CPP_AND:
6385 c_parser_consume_token (parser);
6386 op = c_parser_cast_expression (parser, NULL);
6387 mark_exp_read (op.value);
6388 return parser_build_unary_op (op_loc, ADDR_EXPR, op);
6389 case CPP_MULT:
6390 c_parser_consume_token (parser);
6391 exp_loc = c_parser_peek_token (parser)->location;
6392 op = c_parser_cast_expression (parser, NULL);
6393 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
6394 ret.value = build_indirect_ref (op_loc, op.value, RO_UNARY_STAR);
6395 return ret;
6396 case CPP_PLUS:
6397 if (!c_dialect_objc () && !in_system_header_at (input_location))
6398 warning_at (op_loc,
6399 OPT_Wtraditional,
6400 "traditional C rejects the unary plus operator");
6401 c_parser_consume_token (parser);
6402 exp_loc = c_parser_peek_token (parser)->location;
6403 op = c_parser_cast_expression (parser, NULL);
6404 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
6405 return parser_build_unary_op (op_loc, CONVERT_EXPR, op);
6406 case CPP_MINUS:
6407 c_parser_consume_token (parser);
6408 exp_loc = c_parser_peek_token (parser)->location;
6409 op = c_parser_cast_expression (parser, NULL);
6410 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
6411 return parser_build_unary_op (op_loc, NEGATE_EXPR, op);
6412 case CPP_COMPL:
6413 c_parser_consume_token (parser);
6414 exp_loc = c_parser_peek_token (parser)->location;
6415 op = c_parser_cast_expression (parser, NULL);
6416 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
6417 return parser_build_unary_op (op_loc, BIT_NOT_EXPR, op);
6418 case CPP_NOT:
6419 c_parser_consume_token (parser);
6420 exp_loc = c_parser_peek_token (parser)->location;
6421 op = c_parser_cast_expression (parser, NULL);
6422 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
6423 return parser_build_unary_op (op_loc, TRUTH_NOT_EXPR, op);
6424 case CPP_AND_AND:
6425 /* Refer to the address of a label as a pointer. */
6426 c_parser_consume_token (parser);
6427 if (c_parser_next_token_is (parser, CPP_NAME))
6429 ret.value = finish_label_address_expr
6430 (c_parser_peek_token (parser)->value, op_loc);
6431 c_parser_consume_token (parser);
6433 else
6435 c_parser_error (parser, "expected identifier");
6436 ret.value = error_mark_node;
6438 return ret;
6439 case CPP_KEYWORD:
6440 switch (c_parser_peek_token (parser)->keyword)
6442 case RID_SIZEOF:
6443 return c_parser_sizeof_expression (parser);
6444 case RID_ALIGNOF:
6445 return c_parser_alignof_expression (parser);
6446 case RID_EXTENSION:
6447 c_parser_consume_token (parser);
6448 ext = disable_extension_diagnostics ();
6449 ret = c_parser_cast_expression (parser, NULL);
6450 restore_extension_diagnostics (ext);
6451 return ret;
6452 case RID_REALPART:
6453 c_parser_consume_token (parser);
6454 exp_loc = c_parser_peek_token (parser)->location;
6455 op = c_parser_cast_expression (parser, NULL);
6456 op = default_function_array_conversion (exp_loc, op);
6457 return parser_build_unary_op (op_loc, REALPART_EXPR, op);
6458 case RID_IMAGPART:
6459 c_parser_consume_token (parser);
6460 exp_loc = c_parser_peek_token (parser)->location;
6461 op = c_parser_cast_expression (parser, NULL);
6462 op = default_function_array_conversion (exp_loc, op);
6463 return parser_build_unary_op (op_loc, IMAGPART_EXPR, op);
6464 case RID_TRANSACTION_ATOMIC:
6465 case RID_TRANSACTION_RELAXED:
6466 return c_parser_transaction_expression (parser,
6467 c_parser_peek_token (parser)->keyword);
6468 default:
6469 return c_parser_postfix_expression (parser);
6471 default:
6472 return c_parser_postfix_expression (parser);
6476 /* Parse a sizeof expression. */
6478 static struct c_expr
6479 c_parser_sizeof_expression (c_parser *parser)
6481 struct c_expr expr;
6482 location_t expr_loc;
6483 gcc_assert (c_parser_next_token_is_keyword (parser, RID_SIZEOF));
6484 c_parser_consume_token (parser);
6485 c_inhibit_evaluation_warnings++;
6486 in_sizeof++;
6487 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
6488 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
6490 /* Either sizeof ( type-name ) or sizeof unary-expression
6491 starting with a compound literal. */
6492 struct c_type_name *type_name;
6493 c_parser_consume_token (parser);
6494 expr_loc = c_parser_peek_token (parser)->location;
6495 type_name = c_parser_type_name (parser);
6496 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
6497 if (type_name == NULL)
6499 struct c_expr ret;
6500 c_inhibit_evaluation_warnings--;
6501 in_sizeof--;
6502 ret.value = error_mark_node;
6503 ret.original_code = ERROR_MARK;
6504 ret.original_type = NULL;
6505 return ret;
6507 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
6509 expr = c_parser_postfix_expression_after_paren_type (parser,
6510 type_name,
6511 expr_loc);
6512 goto sizeof_expr;
6514 /* sizeof ( type-name ). */
6515 c_inhibit_evaluation_warnings--;
6516 in_sizeof--;
6517 return c_expr_sizeof_type (expr_loc, type_name);
6519 else
6521 expr_loc = c_parser_peek_token (parser)->location;
6522 expr = c_parser_unary_expression (parser);
6523 sizeof_expr:
6524 c_inhibit_evaluation_warnings--;
6525 in_sizeof--;
6526 mark_exp_read (expr.value);
6527 if (TREE_CODE (expr.value) == COMPONENT_REF
6528 && DECL_C_BIT_FIELD (TREE_OPERAND (expr.value, 1)))
6529 error_at (expr_loc, "%<sizeof%> applied to a bit-field");
6530 return c_expr_sizeof_expr (expr_loc, expr);
6534 /* Parse an alignof expression. */
6536 static struct c_expr
6537 c_parser_alignof_expression (c_parser *parser)
6539 struct c_expr expr;
6540 location_t loc = c_parser_peek_token (parser)->location;
6541 tree alignof_spelling = c_parser_peek_token (parser)->value;
6542 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ALIGNOF));
6543 bool is_c11_alignof = strcmp (IDENTIFIER_POINTER (alignof_spelling),
6544 "_Alignof") == 0;
6545 /* A diagnostic is not required for the use of this identifier in
6546 the implementation namespace; only diagnose it for the C11
6547 spelling because of existing code using the other spellings. */
6548 if (!flag_isoc11 && is_c11_alignof)
6550 if (flag_isoc99)
6551 pedwarn (loc, OPT_Wpedantic, "ISO C99 does not support %qE",
6552 alignof_spelling);
6553 else
6554 pedwarn (loc, OPT_Wpedantic, "ISO C90 does not support %qE",
6555 alignof_spelling);
6557 c_parser_consume_token (parser);
6558 c_inhibit_evaluation_warnings++;
6559 in_alignof++;
6560 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
6561 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
6563 /* Either __alignof__ ( type-name ) or __alignof__
6564 unary-expression starting with a compound literal. */
6565 location_t loc;
6566 struct c_type_name *type_name;
6567 struct c_expr ret;
6568 c_parser_consume_token (parser);
6569 loc = c_parser_peek_token (parser)->location;
6570 type_name = c_parser_type_name (parser);
6571 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
6572 if (type_name == NULL)
6574 struct c_expr ret;
6575 c_inhibit_evaluation_warnings--;
6576 in_alignof--;
6577 ret.value = error_mark_node;
6578 ret.original_code = ERROR_MARK;
6579 ret.original_type = NULL;
6580 return ret;
6582 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
6584 expr = c_parser_postfix_expression_after_paren_type (parser,
6585 type_name,
6586 loc);
6587 goto alignof_expr;
6589 /* alignof ( type-name ). */
6590 c_inhibit_evaluation_warnings--;
6591 in_alignof--;
6592 ret.value = c_sizeof_or_alignof_type (loc, groktypename (type_name,
6593 NULL, NULL),
6594 false, is_c11_alignof, 1);
6595 ret.original_code = ERROR_MARK;
6596 ret.original_type = NULL;
6597 return ret;
6599 else
6601 struct c_expr ret;
6602 expr = c_parser_unary_expression (parser);
6603 alignof_expr:
6604 mark_exp_read (expr.value);
6605 c_inhibit_evaluation_warnings--;
6606 in_alignof--;
6607 pedwarn (loc, OPT_Wpedantic, "ISO C does not allow %<%E (expression)%>",
6608 alignof_spelling);
6609 ret.value = c_alignof_expr (loc, expr.value);
6610 ret.original_code = ERROR_MARK;
6611 ret.original_type = NULL;
6612 return ret;
6616 /* Helper function to read arguments of builtins which are interfaces
6617 for the middle-end nodes like COMPLEX_EXPR, VEC_PERM_EXPR and
6618 others. The name of the builtin is passed using BNAME parameter.
6619 Function returns true if there were no errors while parsing and
6620 stores the arguments in CEXPR_LIST. */
6621 static bool
6622 c_parser_get_builtin_args (c_parser *parser, const char *bname,
6623 vec<c_expr_t, va_gc> **ret_cexpr_list,
6624 bool choose_expr_p)
6626 location_t loc = c_parser_peek_token (parser)->location;
6627 vec<c_expr_t, va_gc> *cexpr_list;
6628 c_expr_t expr;
6629 bool saved_force_folding_builtin_constant_p;
6631 *ret_cexpr_list = NULL;
6632 if (c_parser_next_token_is_not (parser, CPP_OPEN_PAREN))
6634 error_at (loc, "cannot take address of %qs", bname);
6635 return false;
6638 c_parser_consume_token (parser);
6640 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
6642 c_parser_consume_token (parser);
6643 return true;
6646 saved_force_folding_builtin_constant_p
6647 = force_folding_builtin_constant_p;
6648 force_folding_builtin_constant_p |= choose_expr_p;
6649 expr = c_parser_expr_no_commas (parser, NULL);
6650 force_folding_builtin_constant_p
6651 = saved_force_folding_builtin_constant_p;
6652 vec_alloc (cexpr_list, 1);
6653 vec_safe_push (cexpr_list, expr);
6654 while (c_parser_next_token_is (parser, CPP_COMMA))
6656 c_parser_consume_token (parser);
6657 expr = c_parser_expr_no_commas (parser, NULL);
6658 vec_safe_push (cexpr_list, expr);
6661 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
6662 return false;
6664 *ret_cexpr_list = cexpr_list;
6665 return true;
6668 /* This represents a single generic-association. */
6670 struct c_generic_association
6672 /* The location of the starting token of the type. */
6673 location_t type_location;
6674 /* The association's type, or NULL_TREE for 'default'. */
6675 tree type;
6676 /* The association's expression. */
6677 struct c_expr expression;
6680 /* Parse a generic-selection. (C11 6.5.1.1).
6682 generic-selection:
6683 _Generic ( assignment-expression , generic-assoc-list )
6685 generic-assoc-list:
6686 generic-association
6687 generic-assoc-list , generic-association
6689 generic-association:
6690 type-name : assignment-expression
6691 default : assignment-expression
6694 static struct c_expr
6695 c_parser_generic_selection (c_parser *parser)
6697 vec<c_generic_association> associations = vNULL;
6698 struct c_expr selector, error_expr;
6699 tree selector_type;
6700 struct c_generic_association matched_assoc;
6701 bool match_found = false;
6702 location_t generic_loc, selector_loc;
6704 error_expr.original_code = ERROR_MARK;
6705 error_expr.original_type = NULL;
6706 error_expr.value = error_mark_node;
6707 matched_assoc.type_location = UNKNOWN_LOCATION;
6708 matched_assoc.type = NULL_TREE;
6709 matched_assoc.expression = error_expr;
6711 gcc_assert (c_parser_next_token_is_keyword (parser, RID_GENERIC));
6712 generic_loc = c_parser_peek_token (parser)->location;
6713 c_parser_consume_token (parser);
6714 if (!flag_isoc11)
6716 if (flag_isoc99)
6717 pedwarn (generic_loc, OPT_Wpedantic,
6718 "ISO C99 does not support %<_Generic%>");
6719 else
6720 pedwarn (generic_loc, OPT_Wpedantic,
6721 "ISO C90 does not support %<_Generic%>");
6724 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6725 return error_expr;
6727 c_inhibit_evaluation_warnings++;
6728 selector_loc = c_parser_peek_token (parser)->location;
6729 selector = c_parser_expr_no_commas (parser, NULL);
6730 selector = default_function_array_conversion (selector_loc, selector);
6731 c_inhibit_evaluation_warnings--;
6733 if (selector.value == error_mark_node)
6735 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6736 return selector;
6738 selector_type = TREE_TYPE (selector.value);
6739 /* In ISO C terms, rvalues (including the controlling expression of
6740 _Generic) do not have qualified types. */
6741 if (TREE_CODE (selector_type) != ARRAY_TYPE)
6742 selector_type = TYPE_MAIN_VARIANT (selector_type);
6743 /* In ISO C terms, _Noreturn is not part of the type of expressions
6744 such as &abort, but in GCC it is represented internally as a type
6745 qualifier. */
6746 if (FUNCTION_POINTER_TYPE_P (selector_type)
6747 && TYPE_QUALS (TREE_TYPE (selector_type)) != TYPE_UNQUALIFIED)
6748 selector_type
6749 = build_pointer_type (TYPE_MAIN_VARIANT (TREE_TYPE (selector_type)));
6751 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
6753 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6754 return error_expr;
6757 while (1)
6759 struct c_generic_association assoc, *iter;
6760 unsigned int ix;
6761 c_token *token = c_parser_peek_token (parser);
6763 assoc.type_location = token->location;
6764 if (token->type == CPP_KEYWORD && token->keyword == RID_DEFAULT)
6766 c_parser_consume_token (parser);
6767 assoc.type = NULL_TREE;
6769 else
6771 struct c_type_name *type_name;
6773 type_name = c_parser_type_name (parser);
6774 if (type_name == NULL)
6776 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6777 goto error_exit;
6779 assoc.type = groktypename (type_name, NULL, NULL);
6780 if (assoc.type == error_mark_node)
6782 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6783 goto error_exit;
6786 if (TREE_CODE (assoc.type) == FUNCTION_TYPE)
6787 error_at (assoc.type_location,
6788 "%<_Generic%> association has function type");
6789 else if (!COMPLETE_TYPE_P (assoc.type))
6790 error_at (assoc.type_location,
6791 "%<_Generic%> association has incomplete type");
6793 if (variably_modified_type_p (assoc.type, NULL_TREE))
6794 error_at (assoc.type_location,
6795 "%<_Generic%> association has "
6796 "variable length type");
6799 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
6801 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6802 goto error_exit;
6805 assoc.expression = c_parser_expr_no_commas (parser, NULL);
6806 if (assoc.expression.value == error_mark_node)
6808 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6809 goto error_exit;
6812 for (ix = 0; associations.iterate (ix, &iter); ++ix)
6814 if (assoc.type == NULL_TREE)
6816 if (iter->type == NULL_TREE)
6818 error_at (assoc.type_location,
6819 "duplicate %<default%> case in %<_Generic%>");
6820 inform (iter->type_location, "original %<default%> is here");
6823 else if (iter->type != NULL_TREE)
6825 if (comptypes (assoc.type, iter->type))
6827 error_at (assoc.type_location,
6828 "%<_Generic%> specifies two compatible types");
6829 inform (iter->type_location, "compatible type is here");
6834 if (assoc.type == NULL_TREE)
6836 if (!match_found)
6838 matched_assoc = assoc;
6839 match_found = true;
6842 else if (comptypes (assoc.type, selector_type))
6844 if (!match_found || matched_assoc.type == NULL_TREE)
6846 matched_assoc = assoc;
6847 match_found = true;
6849 else
6851 error_at (assoc.type_location,
6852 "%<_Generic> selector matches multiple associations");
6853 inform (matched_assoc.type_location,
6854 "other match is here");
6858 associations.safe_push (assoc);
6860 if (c_parser_peek_token (parser)->type != CPP_COMMA)
6861 break;
6862 c_parser_consume_token (parser);
6865 associations.release ();
6867 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
6869 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6870 return error_expr;
6873 if (!match_found)
6875 error_at (selector_loc, "%<_Generic%> selector of type %qT is not "
6876 "compatible with any association",
6877 selector_type);
6878 return error_expr;
6881 return matched_assoc.expression;
6883 error_exit:
6884 associations.release ();
6885 return error_expr;
6888 /* Parse a postfix expression (C90 6.3.1-6.3.2, C99 6.5.1-6.5.2).
6890 postfix-expression:
6891 primary-expression
6892 postfix-expression [ expression ]
6893 postfix-expression ( argument-expression-list[opt] )
6894 postfix-expression . identifier
6895 postfix-expression -> identifier
6896 postfix-expression ++
6897 postfix-expression --
6898 ( type-name ) { initializer-list }
6899 ( type-name ) { initializer-list , }
6901 argument-expression-list:
6902 argument-expression
6903 argument-expression-list , argument-expression
6905 primary-expression:
6906 identifier
6907 constant
6908 string-literal
6909 ( expression )
6910 generic-selection
6912 GNU extensions:
6914 primary-expression:
6915 __func__
6916 (treated as a keyword in GNU C)
6917 __FUNCTION__
6918 __PRETTY_FUNCTION__
6919 ( compound-statement )
6920 __builtin_va_arg ( assignment-expression , type-name )
6921 __builtin_offsetof ( type-name , offsetof-member-designator )
6922 __builtin_choose_expr ( assignment-expression ,
6923 assignment-expression ,
6924 assignment-expression )
6925 __builtin_types_compatible_p ( type-name , type-name )
6926 __builtin_complex ( assignment-expression , assignment-expression )
6927 __builtin_shuffle ( assignment-expression , assignment-expression )
6928 __builtin_shuffle ( assignment-expression ,
6929 assignment-expression ,
6930 assignment-expression, )
6932 offsetof-member-designator:
6933 identifier
6934 offsetof-member-designator . identifier
6935 offsetof-member-designator [ expression ]
6937 Objective-C:
6939 primary-expression:
6940 [ objc-receiver objc-message-args ]
6941 @selector ( objc-selector-arg )
6942 @protocol ( identifier )
6943 @encode ( type-name )
6944 objc-string-literal
6945 Classname . identifier
6948 static struct c_expr
6949 c_parser_postfix_expression (c_parser *parser)
6951 struct c_expr expr, e1;
6952 struct c_type_name *t1, *t2;
6953 location_t loc = c_parser_peek_token (parser)->location;;
6954 expr.original_code = ERROR_MARK;
6955 expr.original_type = NULL;
6956 switch (c_parser_peek_token (parser)->type)
6958 case CPP_NUMBER:
6959 expr.value = c_parser_peek_token (parser)->value;
6960 loc = c_parser_peek_token (parser)->location;
6961 c_parser_consume_token (parser);
6962 if (TREE_CODE (expr.value) == FIXED_CST
6963 && !targetm.fixed_point_supported_p ())
6965 error_at (loc, "fixed-point types not supported for this target");
6966 expr.value = error_mark_node;
6968 break;
6969 case CPP_CHAR:
6970 case CPP_CHAR16:
6971 case CPP_CHAR32:
6972 case CPP_WCHAR:
6973 expr.value = c_parser_peek_token (parser)->value;
6974 c_parser_consume_token (parser);
6975 break;
6976 case CPP_STRING:
6977 case CPP_STRING16:
6978 case CPP_STRING32:
6979 case CPP_WSTRING:
6980 case CPP_UTF8STRING:
6981 expr.value = c_parser_peek_token (parser)->value;
6982 expr.original_code = STRING_CST;
6983 c_parser_consume_token (parser);
6984 break;
6985 case CPP_OBJC_STRING:
6986 gcc_assert (c_dialect_objc ());
6987 expr.value
6988 = objc_build_string_object (c_parser_peek_token (parser)->value);
6989 c_parser_consume_token (parser);
6990 break;
6991 case CPP_NAME:
6992 switch (c_parser_peek_token (parser)->id_kind)
6994 case C_ID_ID:
6996 tree id = c_parser_peek_token (parser)->value;
6997 c_parser_consume_token (parser);
6998 expr.value = build_external_ref (loc, id,
6999 (c_parser_peek_token (parser)->type
7000 == CPP_OPEN_PAREN),
7001 &expr.original_type);
7002 break;
7004 case C_ID_CLASSNAME:
7006 /* Here we parse the Objective-C 2.0 Class.name dot
7007 syntax. */
7008 tree class_name = c_parser_peek_token (parser)->value;
7009 tree component;
7010 c_parser_consume_token (parser);
7011 gcc_assert (c_dialect_objc ());
7012 if (!c_parser_require (parser, CPP_DOT, "expected %<.%>"))
7014 expr.value = error_mark_node;
7015 break;
7017 if (c_parser_next_token_is_not (parser, CPP_NAME))
7019 c_parser_error (parser, "expected identifier");
7020 expr.value = error_mark_node;
7021 break;
7023 component = c_parser_peek_token (parser)->value;
7024 c_parser_consume_token (parser);
7025 expr.value = objc_build_class_component_ref (class_name,
7026 component);
7027 break;
7029 default:
7030 c_parser_error (parser, "expected expression");
7031 expr.value = error_mark_node;
7032 break;
7034 break;
7035 case CPP_OPEN_PAREN:
7036 /* A parenthesized expression, statement expression or compound
7037 literal. */
7038 if (c_parser_peek_2nd_token (parser)->type == CPP_OPEN_BRACE)
7040 /* A statement expression. */
7041 tree stmt;
7042 location_t brace_loc;
7043 c_parser_consume_token (parser);
7044 brace_loc = c_parser_peek_token (parser)->location;
7045 c_parser_consume_token (parser);
7046 if (!building_stmt_list_p ())
7048 error_at (loc, "braced-group within expression allowed "
7049 "only inside a function");
7050 parser->error = true;
7051 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
7052 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7053 expr.value = error_mark_node;
7054 break;
7056 stmt = c_begin_stmt_expr ();
7057 c_parser_compound_statement_nostart (parser);
7058 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7059 "expected %<)%>");
7060 pedwarn (loc, OPT_Wpedantic,
7061 "ISO C forbids braced-groups within expressions");
7062 expr.value = c_finish_stmt_expr (brace_loc, stmt);
7063 mark_exp_read (expr.value);
7065 else if (c_token_starts_typename (c_parser_peek_2nd_token (parser)))
7067 /* A compound literal. ??? Can we actually get here rather
7068 than going directly to
7069 c_parser_postfix_expression_after_paren_type from
7070 elsewhere? */
7071 location_t loc;
7072 struct c_type_name *type_name;
7073 c_parser_consume_token (parser);
7074 loc = c_parser_peek_token (parser)->location;
7075 type_name = c_parser_type_name (parser);
7076 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7077 "expected %<)%>");
7078 if (type_name == NULL)
7080 expr.value = error_mark_node;
7082 else
7083 expr = c_parser_postfix_expression_after_paren_type (parser,
7084 type_name,
7085 loc);
7087 else
7089 /* A parenthesized expression. */
7090 c_parser_consume_token (parser);
7091 expr = c_parser_expression (parser);
7092 if (TREE_CODE (expr.value) == MODIFY_EXPR)
7093 TREE_NO_WARNING (expr.value) = 1;
7094 if (expr.original_code != C_MAYBE_CONST_EXPR)
7095 expr.original_code = ERROR_MARK;
7096 /* Don't change EXPR.ORIGINAL_TYPE. */
7097 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7098 "expected %<)%>");
7100 break;
7101 case CPP_KEYWORD:
7102 switch (c_parser_peek_token (parser)->keyword)
7104 case RID_FUNCTION_NAME:
7105 case RID_PRETTY_FUNCTION_NAME:
7106 case RID_C99_FUNCTION_NAME:
7107 expr.value = fname_decl (loc,
7108 c_parser_peek_token (parser)->keyword,
7109 c_parser_peek_token (parser)->value);
7110 c_parser_consume_token (parser);
7111 break;
7112 case RID_VA_ARG:
7113 c_parser_consume_token (parser);
7114 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7116 expr.value = error_mark_node;
7117 break;
7119 e1 = c_parser_expr_no_commas (parser, NULL);
7120 mark_exp_read (e1.value);
7121 e1.value = c_fully_fold (e1.value, false, NULL);
7122 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
7124 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7125 expr.value = error_mark_node;
7126 break;
7128 loc = c_parser_peek_token (parser)->location;
7129 t1 = c_parser_type_name (parser);
7130 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7131 "expected %<)%>");
7132 if (t1 == NULL)
7134 expr.value = error_mark_node;
7136 else
7138 tree type_expr = NULL_TREE;
7139 expr.value = c_build_va_arg (loc, e1.value,
7140 groktypename (t1, &type_expr, NULL));
7141 if (type_expr)
7143 expr.value = build2 (C_MAYBE_CONST_EXPR,
7144 TREE_TYPE (expr.value), type_expr,
7145 expr.value);
7146 C_MAYBE_CONST_EXPR_NON_CONST (expr.value) = true;
7149 break;
7150 case RID_OFFSETOF:
7151 c_parser_consume_token (parser);
7152 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7154 expr.value = error_mark_node;
7155 break;
7157 t1 = c_parser_type_name (parser);
7158 if (t1 == NULL)
7159 parser->error = true;
7160 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
7161 gcc_assert (parser->error);
7162 if (parser->error)
7164 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7165 expr.value = error_mark_node;
7166 break;
7170 tree type = groktypename (t1, NULL, NULL);
7171 tree offsetof_ref;
7172 if (type == error_mark_node)
7173 offsetof_ref = error_mark_node;
7174 else
7176 offsetof_ref = build1 (INDIRECT_REF, type, null_pointer_node);
7177 SET_EXPR_LOCATION (offsetof_ref, loc);
7179 /* Parse the second argument to __builtin_offsetof. We
7180 must have one identifier, and beyond that we want to
7181 accept sub structure and sub array references. */
7182 if (c_parser_next_token_is (parser, CPP_NAME))
7184 offsetof_ref = build_component_ref
7185 (loc, offsetof_ref, c_parser_peek_token (parser)->value);
7186 c_parser_consume_token (parser);
7187 while (c_parser_next_token_is (parser, CPP_DOT)
7188 || c_parser_next_token_is (parser,
7189 CPP_OPEN_SQUARE)
7190 || c_parser_next_token_is (parser,
7191 CPP_DEREF))
7193 if (c_parser_next_token_is (parser, CPP_DEREF))
7195 loc = c_parser_peek_token (parser)->location;
7196 offsetof_ref = build_array_ref (loc,
7197 offsetof_ref,
7198 integer_zero_node);
7199 goto do_dot;
7201 else if (c_parser_next_token_is (parser, CPP_DOT))
7203 do_dot:
7204 c_parser_consume_token (parser);
7205 if (c_parser_next_token_is_not (parser,
7206 CPP_NAME))
7208 c_parser_error (parser, "expected identifier");
7209 break;
7211 offsetof_ref = build_component_ref
7212 (loc, offsetof_ref,
7213 c_parser_peek_token (parser)->value);
7214 c_parser_consume_token (parser);
7216 else
7218 struct c_expr ce;
7219 tree idx;
7220 loc = c_parser_peek_token (parser)->location;
7221 c_parser_consume_token (parser);
7222 ce = c_parser_expression (parser);
7223 ce = convert_lvalue_to_rvalue (loc, ce, false, false);
7224 idx = ce.value;
7225 idx = c_fully_fold (idx, false, NULL);
7226 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
7227 "expected %<]%>");
7228 offsetof_ref = build_array_ref (loc, offsetof_ref, idx);
7232 else
7233 c_parser_error (parser, "expected identifier");
7234 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7235 "expected %<)%>");
7236 expr.value = fold_offsetof (offsetof_ref);
7238 break;
7239 case RID_CHOOSE_EXPR:
7241 vec<c_expr_t, va_gc> *cexpr_list;
7242 c_expr_t *e1_p, *e2_p, *e3_p;
7243 tree c;
7245 c_parser_consume_token (parser);
7246 if (!c_parser_get_builtin_args (parser,
7247 "__builtin_choose_expr",
7248 &cexpr_list, true))
7250 expr.value = error_mark_node;
7251 break;
7254 if (vec_safe_length (cexpr_list) != 3)
7256 error_at (loc, "wrong number of arguments to "
7257 "%<__builtin_choose_expr%>");
7258 expr.value = error_mark_node;
7259 break;
7262 e1_p = &(*cexpr_list)[0];
7263 e2_p = &(*cexpr_list)[1];
7264 e3_p = &(*cexpr_list)[2];
7266 c = e1_p->value;
7267 mark_exp_read (e2_p->value);
7268 mark_exp_read (e3_p->value);
7269 if (TREE_CODE (c) != INTEGER_CST
7270 || !INTEGRAL_TYPE_P (TREE_TYPE (c)))
7271 error_at (loc,
7272 "first argument to %<__builtin_choose_expr%> not"
7273 " a constant");
7274 constant_expression_warning (c);
7275 expr = integer_zerop (c) ? *e3_p : *e2_p;
7276 break;
7278 case RID_TYPES_COMPATIBLE_P:
7279 c_parser_consume_token (parser);
7280 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7282 expr.value = error_mark_node;
7283 break;
7285 t1 = c_parser_type_name (parser);
7286 if (t1 == NULL)
7288 expr.value = error_mark_node;
7289 break;
7291 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
7293 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7294 expr.value = error_mark_node;
7295 break;
7297 t2 = c_parser_type_name (parser);
7298 if (t2 == NULL)
7300 expr.value = error_mark_node;
7301 break;
7303 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7304 "expected %<)%>");
7306 tree e1, e2;
7307 e1 = groktypename (t1, NULL, NULL);
7308 e2 = groktypename (t2, NULL, NULL);
7309 if (e1 == error_mark_node || e2 == error_mark_node)
7311 expr.value = error_mark_node;
7312 break;
7315 e1 = TYPE_MAIN_VARIANT (e1);
7316 e2 = TYPE_MAIN_VARIANT (e2);
7318 expr.value
7319 = comptypes (e1, e2) ? integer_one_node : integer_zero_node;
7321 break;
7322 case RID_BUILTIN_COMPLEX:
7324 vec<c_expr_t, va_gc> *cexpr_list;
7325 c_expr_t *e1_p, *e2_p;
7327 c_parser_consume_token (parser);
7328 if (!c_parser_get_builtin_args (parser,
7329 "__builtin_complex",
7330 &cexpr_list, false))
7332 expr.value = error_mark_node;
7333 break;
7336 if (vec_safe_length (cexpr_list) != 2)
7338 error_at (loc, "wrong number of arguments to "
7339 "%<__builtin_complex%>");
7340 expr.value = error_mark_node;
7341 break;
7344 e1_p = &(*cexpr_list)[0];
7345 e2_p = &(*cexpr_list)[1];
7347 *e1_p = convert_lvalue_to_rvalue (loc, *e1_p, true, true);
7348 if (TREE_CODE (e1_p->value) == EXCESS_PRECISION_EXPR)
7349 e1_p->value = convert (TREE_TYPE (e1_p->value),
7350 TREE_OPERAND (e1_p->value, 0));
7351 *e2_p = convert_lvalue_to_rvalue (loc, *e2_p, true, true);
7352 if (TREE_CODE (e2_p->value) == EXCESS_PRECISION_EXPR)
7353 e2_p->value = convert (TREE_TYPE (e2_p->value),
7354 TREE_OPERAND (e2_p->value, 0));
7355 if (!SCALAR_FLOAT_TYPE_P (TREE_TYPE (e1_p->value))
7356 || DECIMAL_FLOAT_TYPE_P (TREE_TYPE (e1_p->value))
7357 || !SCALAR_FLOAT_TYPE_P (TREE_TYPE (e2_p->value))
7358 || DECIMAL_FLOAT_TYPE_P (TREE_TYPE (e2_p->value)))
7360 error_at (loc, "%<__builtin_complex%> operand "
7361 "not of real binary floating-point type");
7362 expr.value = error_mark_node;
7363 break;
7365 if (TYPE_MAIN_VARIANT (TREE_TYPE (e1_p->value))
7366 != TYPE_MAIN_VARIANT (TREE_TYPE (e2_p->value)))
7368 error_at (loc,
7369 "%<__builtin_complex%> operands of different types");
7370 expr.value = error_mark_node;
7371 break;
7373 if (!flag_isoc99)
7374 pedwarn (loc, OPT_Wpedantic,
7375 "ISO C90 does not support complex types");
7376 expr.value = build2 (COMPLEX_EXPR,
7377 build_complex_type
7378 (TYPE_MAIN_VARIANT
7379 (TREE_TYPE (e1_p->value))),
7380 e1_p->value, e2_p->value);
7381 break;
7383 case RID_BUILTIN_SHUFFLE:
7385 vec<c_expr_t, va_gc> *cexpr_list;
7386 unsigned int i;
7387 c_expr_t *p;
7389 c_parser_consume_token (parser);
7390 if (!c_parser_get_builtin_args (parser,
7391 "__builtin_shuffle",
7392 &cexpr_list, false))
7394 expr.value = error_mark_node;
7395 break;
7398 FOR_EACH_VEC_SAFE_ELT (cexpr_list, i, p)
7399 *p = convert_lvalue_to_rvalue (loc, *p, true, true);
7401 if (vec_safe_length (cexpr_list) == 2)
7402 expr.value =
7403 c_build_vec_perm_expr
7404 (loc, (*cexpr_list)[0].value,
7405 NULL_TREE, (*cexpr_list)[1].value);
7407 else if (vec_safe_length (cexpr_list) == 3)
7408 expr.value =
7409 c_build_vec_perm_expr
7410 (loc, (*cexpr_list)[0].value,
7411 (*cexpr_list)[1].value,
7412 (*cexpr_list)[2].value);
7413 else
7415 error_at (loc, "wrong number of arguments to "
7416 "%<__builtin_shuffle%>");
7417 expr.value = error_mark_node;
7419 break;
7421 case RID_AT_SELECTOR:
7422 gcc_assert (c_dialect_objc ());
7423 c_parser_consume_token (parser);
7424 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7426 expr.value = error_mark_node;
7427 break;
7430 tree sel = c_parser_objc_selector_arg (parser);
7431 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7432 "expected %<)%>");
7433 expr.value = objc_build_selector_expr (loc, sel);
7435 break;
7436 case RID_AT_PROTOCOL:
7437 gcc_assert (c_dialect_objc ());
7438 c_parser_consume_token (parser);
7439 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7441 expr.value = error_mark_node;
7442 break;
7444 if (c_parser_next_token_is_not (parser, CPP_NAME))
7446 c_parser_error (parser, "expected identifier");
7447 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7448 expr.value = error_mark_node;
7449 break;
7452 tree id = c_parser_peek_token (parser)->value;
7453 c_parser_consume_token (parser);
7454 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7455 "expected %<)%>");
7456 expr.value = objc_build_protocol_expr (id);
7458 break;
7459 case RID_AT_ENCODE:
7460 /* Extension to support C-structures in the archiver. */
7461 gcc_assert (c_dialect_objc ());
7462 c_parser_consume_token (parser);
7463 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7465 expr.value = error_mark_node;
7466 break;
7468 t1 = c_parser_type_name (parser);
7469 if (t1 == NULL)
7471 expr.value = error_mark_node;
7472 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7473 break;
7475 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7476 "expected %<)%>");
7478 tree type = groktypename (t1, NULL, NULL);
7479 expr.value = objc_build_encode_expr (type);
7481 break;
7482 case RID_GENERIC:
7483 expr = c_parser_generic_selection (parser);
7484 break;
7485 case RID_CILK_SPAWN:
7486 c_parser_consume_token (parser);
7487 if (!flag_cilkplus)
7489 error_at (loc, "-fcilkplus must be enabled to use "
7490 "%<_Cilk_spawn%>");
7491 expr = c_parser_postfix_expression (parser);
7492 expr.value = error_mark_node;
7494 else if (c_parser_peek_token (parser)->keyword == RID_CILK_SPAWN)
7496 error_at (loc, "consecutive %<_Cilk_spawn%> keywords "
7497 "are not permitted");
7498 /* Now flush out all the _Cilk_spawns. */
7499 while (c_parser_peek_token (parser)->keyword == RID_CILK_SPAWN)
7500 c_parser_consume_token (parser);
7501 expr = c_parser_postfix_expression (parser);
7503 else
7505 expr = c_parser_postfix_expression (parser);
7506 expr.value = build_cilk_spawn (loc, expr.value);
7508 break;
7509 default:
7510 c_parser_error (parser, "expected expression");
7511 expr.value = error_mark_node;
7512 break;
7514 break;
7515 case CPP_OPEN_SQUARE:
7516 if (c_dialect_objc ())
7518 tree receiver, args;
7519 c_parser_consume_token (parser);
7520 receiver = c_parser_objc_receiver (parser);
7521 args = c_parser_objc_message_args (parser);
7522 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
7523 "expected %<]%>");
7524 expr.value = objc_build_message_expr (receiver, args);
7525 break;
7527 /* Else fall through to report error. */
7528 default:
7529 c_parser_error (parser, "expected expression");
7530 expr.value = error_mark_node;
7531 break;
7533 return c_parser_postfix_expression_after_primary (parser, loc, expr);
7536 /* Parse a postfix expression after a parenthesized type name: the
7537 brace-enclosed initializer of a compound literal, possibly followed
7538 by some postfix operators. This is separate because it is not
7539 possible to tell until after the type name whether a cast
7540 expression has a cast or a compound literal, or whether the operand
7541 of sizeof is a parenthesized type name or starts with a compound
7542 literal. TYPE_LOC is the location where TYPE_NAME starts--the
7543 location of the first token after the parentheses around the type
7544 name. */
7546 static struct c_expr
7547 c_parser_postfix_expression_after_paren_type (c_parser *parser,
7548 struct c_type_name *type_name,
7549 location_t type_loc)
7551 tree type;
7552 struct c_expr init;
7553 bool non_const;
7554 struct c_expr expr;
7555 location_t start_loc;
7556 tree type_expr = NULL_TREE;
7557 bool type_expr_const = true;
7558 check_compound_literal_type (type_loc, type_name);
7559 start_init (NULL_TREE, NULL, 0);
7560 type = groktypename (type_name, &type_expr, &type_expr_const);
7561 start_loc = c_parser_peek_token (parser)->location;
7562 if (type != error_mark_node && C_TYPE_VARIABLE_SIZE (type))
7564 error_at (type_loc, "compound literal has variable size");
7565 type = error_mark_node;
7567 init = c_parser_braced_init (parser, type, false);
7568 finish_init ();
7569 maybe_warn_string_init (type, init);
7571 if (type != error_mark_node
7572 && !ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (type))
7573 && current_function_decl)
7575 error ("compound literal qualified by address-space qualifier");
7576 type = error_mark_node;
7579 if (!flag_isoc99)
7580 pedwarn (start_loc, OPT_Wpedantic, "ISO C90 forbids compound literals");
7581 non_const = ((init.value && TREE_CODE (init.value) == CONSTRUCTOR)
7582 ? CONSTRUCTOR_NON_CONST (init.value)
7583 : init.original_code == C_MAYBE_CONST_EXPR);
7584 non_const |= !type_expr_const;
7585 expr.value = build_compound_literal (start_loc, type, init.value, non_const);
7586 expr.original_code = ERROR_MARK;
7587 expr.original_type = NULL;
7588 if (type_expr)
7590 if (TREE_CODE (expr.value) == C_MAYBE_CONST_EXPR)
7592 gcc_assert (C_MAYBE_CONST_EXPR_PRE (expr.value) == NULL_TREE);
7593 C_MAYBE_CONST_EXPR_PRE (expr.value) = type_expr;
7595 else
7597 gcc_assert (!non_const);
7598 expr.value = build2 (C_MAYBE_CONST_EXPR, type,
7599 type_expr, expr.value);
7602 return c_parser_postfix_expression_after_primary (parser, start_loc, expr);
7605 /* Callback function for sizeof_pointer_memaccess_warning to compare
7606 types. */
7608 static bool
7609 sizeof_ptr_memacc_comptypes (tree type1, tree type2)
7611 return comptypes (type1, type2) == 1;
7614 /* Parse a postfix expression after the initial primary or compound
7615 literal; that is, parse a series of postfix operators.
7617 EXPR_LOC is the location of the primary expression. */
7619 static struct c_expr
7620 c_parser_postfix_expression_after_primary (c_parser *parser,
7621 location_t expr_loc,
7622 struct c_expr expr)
7624 struct c_expr orig_expr;
7625 tree ident, idx;
7626 location_t sizeof_arg_loc[3];
7627 tree sizeof_arg[3];
7628 unsigned int i;
7629 vec<tree, va_gc> *exprlist;
7630 vec<tree, va_gc> *origtypes = NULL;
7631 vec<location_t> arg_loc = vNULL;
7633 while (true)
7635 location_t op_loc = c_parser_peek_token (parser)->location;
7636 switch (c_parser_peek_token (parser)->type)
7638 case CPP_OPEN_SQUARE:
7639 /* Array reference. */
7640 c_parser_consume_token (parser);
7641 if (flag_cilkplus
7642 && c_parser_peek_token (parser)->type == CPP_COLON)
7643 /* If we are here, then we have something like this:
7644 Array [ : ]
7646 expr.value = c_parser_array_notation (expr_loc, parser, NULL_TREE,
7647 expr.value);
7648 else
7650 idx = c_parser_expression (parser).value;
7651 /* Here we have 3 options:
7652 1. Array [EXPR] -- Normal Array call.
7653 2. Array [EXPR : EXPR] -- Array notation without stride.
7654 3. Array [EXPR : EXPR : EXPR] -- Array notation with stride.
7656 For 1, we just handle it just like a normal array expression.
7657 For 2 and 3 we handle it like we handle array notations. The
7658 idx value we have above becomes the initial/start index.
7660 if (flag_cilkplus
7661 && c_parser_peek_token (parser)->type == CPP_COLON)
7662 expr.value = c_parser_array_notation (expr_loc, parser, idx,
7663 expr.value);
7664 else
7666 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
7667 "expected %<]%>");
7668 expr.value = build_array_ref (op_loc, expr.value, idx);
7671 expr.original_code = ERROR_MARK;
7672 expr.original_type = NULL;
7673 break;
7674 case CPP_OPEN_PAREN:
7675 /* Function call. */
7676 c_parser_consume_token (parser);
7677 for (i = 0; i < 3; i++)
7679 sizeof_arg[i] = NULL_TREE;
7680 sizeof_arg_loc[i] = UNKNOWN_LOCATION;
7682 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
7683 exprlist = NULL;
7684 else
7685 exprlist = c_parser_expr_list (parser, true, false, &origtypes,
7686 sizeof_arg_loc, sizeof_arg,
7687 &arg_loc);
7688 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7689 "expected %<)%>");
7690 orig_expr = expr;
7691 mark_exp_read (expr.value);
7692 if (warn_sizeof_pointer_memaccess)
7693 sizeof_pointer_memaccess_warning (sizeof_arg_loc,
7694 expr.value, exprlist,
7695 sizeof_arg,
7696 sizeof_ptr_memacc_comptypes);
7697 expr.value
7698 = c_build_function_call_vec (expr_loc, arg_loc, expr.value,
7699 exprlist, origtypes);
7700 expr.original_code = ERROR_MARK;
7701 if (TREE_CODE (expr.value) == INTEGER_CST
7702 && TREE_CODE (orig_expr.value) == FUNCTION_DECL
7703 && DECL_BUILT_IN_CLASS (orig_expr.value) == BUILT_IN_NORMAL
7704 && DECL_FUNCTION_CODE (orig_expr.value) == BUILT_IN_CONSTANT_P)
7705 expr.original_code = C_MAYBE_CONST_EXPR;
7706 expr.original_type = NULL;
7707 if (exprlist)
7709 release_tree_vector (exprlist);
7710 release_tree_vector (origtypes);
7712 arg_loc.release ();
7713 break;
7714 case CPP_DOT:
7715 /* Structure element reference. */
7716 c_parser_consume_token (parser);
7717 expr = default_function_array_conversion (expr_loc, expr);
7718 if (c_parser_next_token_is (parser, CPP_NAME))
7719 ident = c_parser_peek_token (parser)->value;
7720 else
7722 c_parser_error (parser, "expected identifier");
7723 expr.value = error_mark_node;
7724 expr.original_code = ERROR_MARK;
7725 expr.original_type = NULL;
7726 return expr;
7728 c_parser_consume_token (parser);
7729 expr.value = build_component_ref (op_loc, expr.value, ident);
7730 expr.original_code = ERROR_MARK;
7731 if (TREE_CODE (expr.value) != COMPONENT_REF)
7732 expr.original_type = NULL;
7733 else
7735 /* Remember the original type of a bitfield. */
7736 tree field = TREE_OPERAND (expr.value, 1);
7737 if (TREE_CODE (field) != FIELD_DECL)
7738 expr.original_type = NULL;
7739 else
7740 expr.original_type = DECL_BIT_FIELD_TYPE (field);
7742 break;
7743 case CPP_DEREF:
7744 /* Structure element reference. */
7745 c_parser_consume_token (parser);
7746 expr = convert_lvalue_to_rvalue (expr_loc, expr, true, false);
7747 if (c_parser_next_token_is (parser, CPP_NAME))
7748 ident = c_parser_peek_token (parser)->value;
7749 else
7751 c_parser_error (parser, "expected identifier");
7752 expr.value = error_mark_node;
7753 expr.original_code = ERROR_MARK;
7754 expr.original_type = NULL;
7755 return expr;
7757 c_parser_consume_token (parser);
7758 expr.value = build_component_ref (op_loc,
7759 build_indirect_ref (op_loc,
7760 expr.value,
7761 RO_ARROW),
7762 ident);
7763 expr.original_code = ERROR_MARK;
7764 if (TREE_CODE (expr.value) != COMPONENT_REF)
7765 expr.original_type = NULL;
7766 else
7768 /* Remember the original type of a bitfield. */
7769 tree field = TREE_OPERAND (expr.value, 1);
7770 if (TREE_CODE (field) != FIELD_DECL)
7771 expr.original_type = NULL;
7772 else
7773 expr.original_type = DECL_BIT_FIELD_TYPE (field);
7775 break;
7776 case CPP_PLUS_PLUS:
7777 /* Postincrement. */
7778 c_parser_consume_token (parser);
7779 /* If the expressions have array notations, we expand them. */
7780 if (flag_cilkplus
7781 && TREE_CODE (expr.value) == ARRAY_NOTATION_REF)
7782 expr = fix_array_notation_expr (expr_loc, POSTINCREMENT_EXPR, expr);
7783 else
7785 expr = default_function_array_read_conversion (expr_loc, expr);
7786 expr.value = build_unary_op (op_loc,
7787 POSTINCREMENT_EXPR, expr.value, 0);
7789 expr.original_code = ERROR_MARK;
7790 expr.original_type = NULL;
7791 break;
7792 case CPP_MINUS_MINUS:
7793 /* Postdecrement. */
7794 c_parser_consume_token (parser);
7795 /* If the expressions have array notations, we expand them. */
7796 if (flag_cilkplus
7797 && TREE_CODE (expr.value) == ARRAY_NOTATION_REF)
7798 expr = fix_array_notation_expr (expr_loc, POSTDECREMENT_EXPR, expr);
7799 else
7801 expr = default_function_array_read_conversion (expr_loc, expr);
7802 expr.value = build_unary_op (op_loc,
7803 POSTDECREMENT_EXPR, expr.value, 0);
7805 expr.original_code = ERROR_MARK;
7806 expr.original_type = NULL;
7807 break;
7808 default:
7809 return expr;
7814 /* Parse an expression (C90 6.3.17, C99 6.5.17).
7816 expression:
7817 assignment-expression
7818 expression , assignment-expression
7821 static struct c_expr
7822 c_parser_expression (c_parser *parser)
7824 location_t tloc = c_parser_peek_token (parser)->location;
7825 struct c_expr expr;
7826 expr = c_parser_expr_no_commas (parser, NULL);
7827 if (c_parser_next_token_is (parser, CPP_COMMA))
7828 expr = convert_lvalue_to_rvalue (tloc, expr, true, false);
7829 while (c_parser_next_token_is (parser, CPP_COMMA))
7831 struct c_expr next;
7832 tree lhsval;
7833 location_t loc = c_parser_peek_token (parser)->location;
7834 location_t expr_loc;
7835 c_parser_consume_token (parser);
7836 expr_loc = c_parser_peek_token (parser)->location;
7837 lhsval = expr.value;
7838 while (TREE_CODE (lhsval) == COMPOUND_EXPR)
7839 lhsval = TREE_OPERAND (lhsval, 1);
7840 if (DECL_P (lhsval) || handled_component_p (lhsval))
7841 mark_exp_read (lhsval);
7842 next = c_parser_expr_no_commas (parser, NULL);
7843 next = convert_lvalue_to_rvalue (expr_loc, next, true, false);
7844 expr.value = build_compound_expr (loc, expr.value, next.value);
7845 expr.original_code = COMPOUND_EXPR;
7846 expr.original_type = next.original_type;
7848 return expr;
7851 /* Parse an expression and convert functions or arrays to pointers and
7852 lvalues to rvalues. */
7854 static struct c_expr
7855 c_parser_expression_conv (c_parser *parser)
7857 struct c_expr expr;
7858 location_t loc = c_parser_peek_token (parser)->location;
7859 expr = c_parser_expression (parser);
7860 expr = convert_lvalue_to_rvalue (loc, expr, true, false);
7861 return expr;
7864 /* Parse a non-empty list of expressions. If CONVERT_P, convert
7865 functions and arrays to pointers and lvalues to rvalues. If
7866 FOLD_P, fold the expressions. If LOCATIONS is non-NULL, save the
7867 locations of function arguments into this vector.
7869 nonempty-expr-list:
7870 assignment-expression
7871 nonempty-expr-list , assignment-expression
7874 static vec<tree, va_gc> *
7875 c_parser_expr_list (c_parser *parser, bool convert_p, bool fold_p,
7876 vec<tree, va_gc> **p_orig_types,
7877 location_t *sizeof_arg_loc, tree *sizeof_arg,
7878 vec<location_t> *locations)
7880 vec<tree, va_gc> *ret;
7881 vec<tree, va_gc> *orig_types;
7882 struct c_expr expr;
7883 location_t loc = c_parser_peek_token (parser)->location;
7884 location_t cur_sizeof_arg_loc = UNKNOWN_LOCATION;
7885 unsigned int idx = 0;
7887 ret = make_tree_vector ();
7888 if (p_orig_types == NULL)
7889 orig_types = NULL;
7890 else
7891 orig_types = make_tree_vector ();
7893 if (sizeof_arg != NULL
7894 && c_parser_next_token_is_keyword (parser, RID_SIZEOF))
7895 cur_sizeof_arg_loc = c_parser_peek_2nd_token (parser)->location;
7896 expr = c_parser_expr_no_commas (parser, NULL);
7897 if (convert_p)
7898 expr = convert_lvalue_to_rvalue (loc, expr, true, true);
7899 if (fold_p)
7900 expr.value = c_fully_fold (expr.value, false, NULL);
7901 ret->quick_push (expr.value);
7902 if (orig_types)
7903 orig_types->quick_push (expr.original_type);
7904 if (locations)
7905 locations->safe_push (loc);
7906 if (sizeof_arg != NULL
7907 && cur_sizeof_arg_loc != UNKNOWN_LOCATION
7908 && expr.original_code == SIZEOF_EXPR)
7910 sizeof_arg[0] = c_last_sizeof_arg;
7911 sizeof_arg_loc[0] = cur_sizeof_arg_loc;
7913 while (c_parser_next_token_is (parser, CPP_COMMA))
7915 c_parser_consume_token (parser);
7916 loc = c_parser_peek_token (parser)->location;
7917 if (sizeof_arg != NULL
7918 && c_parser_next_token_is_keyword (parser, RID_SIZEOF))
7919 cur_sizeof_arg_loc = c_parser_peek_2nd_token (parser)->location;
7920 else
7921 cur_sizeof_arg_loc = UNKNOWN_LOCATION;
7922 expr = c_parser_expr_no_commas (parser, NULL);
7923 if (convert_p)
7924 expr = convert_lvalue_to_rvalue (loc, expr, true, true);
7925 if (fold_p)
7926 expr.value = c_fully_fold (expr.value, false, NULL);
7927 vec_safe_push (ret, expr.value);
7928 if (orig_types)
7929 vec_safe_push (orig_types, expr.original_type);
7930 if (locations)
7931 locations->safe_push (loc);
7932 if (++idx < 3
7933 && sizeof_arg != NULL
7934 && cur_sizeof_arg_loc != UNKNOWN_LOCATION
7935 && expr.original_code == SIZEOF_EXPR)
7937 sizeof_arg[idx] = c_last_sizeof_arg;
7938 sizeof_arg_loc[idx] = cur_sizeof_arg_loc;
7941 if (orig_types)
7942 *p_orig_types = orig_types;
7943 return ret;
7946 /* Parse Objective-C-specific constructs. */
7948 /* Parse an objc-class-definition.
7950 objc-class-definition:
7951 @interface identifier objc-superclass[opt] objc-protocol-refs[opt]
7952 objc-class-instance-variables[opt] objc-methodprotolist @end
7953 @implementation identifier objc-superclass[opt]
7954 objc-class-instance-variables[opt]
7955 @interface identifier ( identifier ) objc-protocol-refs[opt]
7956 objc-methodprotolist @end
7957 @interface identifier ( ) objc-protocol-refs[opt]
7958 objc-methodprotolist @end
7959 @implementation identifier ( identifier )
7961 objc-superclass:
7962 : identifier
7964 "@interface identifier (" must start "@interface identifier (
7965 identifier ) ...": objc-methodprotolist in the first production may
7966 not start with a parenthesized identifier as a declarator of a data
7967 definition with no declaration specifiers if the objc-superclass,
7968 objc-protocol-refs and objc-class-instance-variables are omitted. */
7970 static void
7971 c_parser_objc_class_definition (c_parser *parser, tree attributes)
7973 bool iface_p;
7974 tree id1;
7975 tree superclass;
7976 if (c_parser_next_token_is_keyword (parser, RID_AT_INTERFACE))
7977 iface_p = true;
7978 else if (c_parser_next_token_is_keyword (parser, RID_AT_IMPLEMENTATION))
7979 iface_p = false;
7980 else
7981 gcc_unreachable ();
7983 c_parser_consume_token (parser);
7984 if (c_parser_next_token_is_not (parser, CPP_NAME))
7986 c_parser_error (parser, "expected identifier");
7987 return;
7989 id1 = c_parser_peek_token (parser)->value;
7990 c_parser_consume_token (parser);
7991 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
7993 /* We have a category or class extension. */
7994 tree id2;
7995 tree proto = NULL_TREE;
7996 c_parser_consume_token (parser);
7997 if (c_parser_next_token_is_not (parser, CPP_NAME))
7999 if (iface_p && c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
8001 /* We have a class extension. */
8002 id2 = NULL_TREE;
8004 else
8006 c_parser_error (parser, "expected identifier or %<)%>");
8007 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
8008 return;
8011 else
8013 id2 = c_parser_peek_token (parser)->value;
8014 c_parser_consume_token (parser);
8016 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
8017 if (!iface_p)
8019 objc_start_category_implementation (id1, id2);
8020 return;
8022 if (c_parser_next_token_is (parser, CPP_LESS))
8023 proto = c_parser_objc_protocol_refs (parser);
8024 objc_start_category_interface (id1, id2, proto, attributes);
8025 c_parser_objc_methodprotolist (parser);
8026 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
8027 objc_finish_interface ();
8028 return;
8030 if (c_parser_next_token_is (parser, CPP_COLON))
8032 c_parser_consume_token (parser);
8033 if (c_parser_next_token_is_not (parser, CPP_NAME))
8035 c_parser_error (parser, "expected identifier");
8036 return;
8038 superclass = c_parser_peek_token (parser)->value;
8039 c_parser_consume_token (parser);
8041 else
8042 superclass = NULL_TREE;
8043 if (iface_p)
8045 tree proto = NULL_TREE;
8046 if (c_parser_next_token_is (parser, CPP_LESS))
8047 proto = c_parser_objc_protocol_refs (parser);
8048 objc_start_class_interface (id1, superclass, proto, attributes);
8050 else
8051 objc_start_class_implementation (id1, superclass);
8052 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
8053 c_parser_objc_class_instance_variables (parser);
8054 if (iface_p)
8056 objc_continue_interface ();
8057 c_parser_objc_methodprotolist (parser);
8058 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
8059 objc_finish_interface ();
8061 else
8063 objc_continue_implementation ();
8064 return;
8068 /* Parse objc-class-instance-variables.
8070 objc-class-instance-variables:
8071 { objc-instance-variable-decl-list[opt] }
8073 objc-instance-variable-decl-list:
8074 objc-visibility-spec
8075 objc-instance-variable-decl ;
8077 objc-instance-variable-decl-list objc-visibility-spec
8078 objc-instance-variable-decl-list objc-instance-variable-decl ;
8079 objc-instance-variable-decl-list ;
8081 objc-visibility-spec:
8082 @private
8083 @protected
8084 @public
8086 objc-instance-variable-decl:
8087 struct-declaration
8090 static void
8091 c_parser_objc_class_instance_variables (c_parser *parser)
8093 gcc_assert (c_parser_next_token_is (parser, CPP_OPEN_BRACE));
8094 c_parser_consume_token (parser);
8095 while (c_parser_next_token_is_not (parser, CPP_EOF))
8097 tree decls;
8098 /* Parse any stray semicolon. */
8099 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
8101 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
8102 "extra semicolon");
8103 c_parser_consume_token (parser);
8104 continue;
8106 /* Stop if at the end of the instance variables. */
8107 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
8109 c_parser_consume_token (parser);
8110 break;
8112 /* Parse any objc-visibility-spec. */
8113 if (c_parser_next_token_is_keyword (parser, RID_AT_PRIVATE))
8115 c_parser_consume_token (parser);
8116 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
8117 continue;
8119 else if (c_parser_next_token_is_keyword (parser, RID_AT_PROTECTED))
8121 c_parser_consume_token (parser);
8122 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
8123 continue;
8125 else if (c_parser_next_token_is_keyword (parser, RID_AT_PUBLIC))
8127 c_parser_consume_token (parser);
8128 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
8129 continue;
8131 else if (c_parser_next_token_is_keyword (parser, RID_AT_PACKAGE))
8133 c_parser_consume_token (parser);
8134 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
8135 continue;
8137 else if (c_parser_next_token_is (parser, CPP_PRAGMA))
8139 c_parser_pragma (parser, pragma_external);
8140 continue;
8143 /* Parse some comma-separated declarations. */
8144 decls = c_parser_struct_declaration (parser);
8145 if (decls == NULL)
8147 /* There is a syntax error. We want to skip the offending
8148 tokens up to the next ';' (included) or '}'
8149 (excluded). */
8151 /* First, skip manually a ')' or ']'. This is because they
8152 reduce the nesting level, so c_parser_skip_until_found()
8153 wouldn't be able to skip past them. */
8154 c_token *token = c_parser_peek_token (parser);
8155 if (token->type == CPP_CLOSE_PAREN || token->type == CPP_CLOSE_SQUARE)
8156 c_parser_consume_token (parser);
8158 /* Then, do the standard skipping. */
8159 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
8161 /* We hopefully recovered. Start normal parsing again. */
8162 parser->error = false;
8163 continue;
8165 else
8167 /* Comma-separated instance variables are chained together
8168 in reverse order; add them one by one. */
8169 tree ivar = nreverse (decls);
8170 for (; ivar; ivar = DECL_CHAIN (ivar))
8171 objc_add_instance_variable (copy_node (ivar));
8173 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
8177 /* Parse an objc-class-declaration.
8179 objc-class-declaration:
8180 @class identifier-list ;
8183 static void
8184 c_parser_objc_class_declaration (c_parser *parser)
8186 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_CLASS));
8187 c_parser_consume_token (parser);
8188 /* Any identifiers, including those declared as type names, are OK
8189 here. */
8190 while (true)
8192 tree id;
8193 if (c_parser_next_token_is_not (parser, CPP_NAME))
8195 c_parser_error (parser, "expected identifier");
8196 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
8197 parser->error = false;
8198 return;
8200 id = c_parser_peek_token (parser)->value;
8201 objc_declare_class (id);
8202 c_parser_consume_token (parser);
8203 if (c_parser_next_token_is (parser, CPP_COMMA))
8204 c_parser_consume_token (parser);
8205 else
8206 break;
8208 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
8211 /* Parse an objc-alias-declaration.
8213 objc-alias-declaration:
8214 @compatibility_alias identifier identifier ;
8217 static void
8218 c_parser_objc_alias_declaration (c_parser *parser)
8220 tree id1, id2;
8221 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_ALIAS));
8222 c_parser_consume_token (parser);
8223 if (c_parser_next_token_is_not (parser, CPP_NAME))
8225 c_parser_error (parser, "expected identifier");
8226 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
8227 return;
8229 id1 = c_parser_peek_token (parser)->value;
8230 c_parser_consume_token (parser);
8231 if (c_parser_next_token_is_not (parser, CPP_NAME))
8233 c_parser_error (parser, "expected identifier");
8234 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
8235 return;
8237 id2 = c_parser_peek_token (parser)->value;
8238 c_parser_consume_token (parser);
8239 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
8240 objc_declare_alias (id1, id2);
8243 /* Parse an objc-protocol-definition.
8245 objc-protocol-definition:
8246 @protocol identifier objc-protocol-refs[opt] objc-methodprotolist @end
8247 @protocol identifier-list ;
8249 "@protocol identifier ;" should be resolved as "@protocol
8250 identifier-list ;": objc-methodprotolist may not start with a
8251 semicolon in the first alternative if objc-protocol-refs are
8252 omitted. */
8254 static void
8255 c_parser_objc_protocol_definition (c_parser *parser, tree attributes)
8257 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_PROTOCOL));
8259 c_parser_consume_token (parser);
8260 if (c_parser_next_token_is_not (parser, CPP_NAME))
8262 c_parser_error (parser, "expected identifier");
8263 return;
8265 if (c_parser_peek_2nd_token (parser)->type == CPP_COMMA
8266 || c_parser_peek_2nd_token (parser)->type == CPP_SEMICOLON)
8268 /* Any identifiers, including those declared as type names, are
8269 OK here. */
8270 while (true)
8272 tree id;
8273 if (c_parser_next_token_is_not (parser, CPP_NAME))
8275 c_parser_error (parser, "expected identifier");
8276 break;
8278 id = c_parser_peek_token (parser)->value;
8279 objc_declare_protocol (id, attributes);
8280 c_parser_consume_token (parser);
8281 if (c_parser_next_token_is (parser, CPP_COMMA))
8282 c_parser_consume_token (parser);
8283 else
8284 break;
8286 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
8288 else
8290 tree id = c_parser_peek_token (parser)->value;
8291 tree proto = NULL_TREE;
8292 c_parser_consume_token (parser);
8293 if (c_parser_next_token_is (parser, CPP_LESS))
8294 proto = c_parser_objc_protocol_refs (parser);
8295 parser->objc_pq_context = true;
8296 objc_start_protocol (id, proto, attributes);
8297 c_parser_objc_methodprotolist (parser);
8298 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
8299 parser->objc_pq_context = false;
8300 objc_finish_interface ();
8304 /* Parse an objc-method-type.
8306 objc-method-type:
8310 Return true if it is a class method (+) and false if it is
8311 an instance method (-).
8313 static inline bool
8314 c_parser_objc_method_type (c_parser *parser)
8316 switch (c_parser_peek_token (parser)->type)
8318 case CPP_PLUS:
8319 c_parser_consume_token (parser);
8320 return true;
8321 case CPP_MINUS:
8322 c_parser_consume_token (parser);
8323 return false;
8324 default:
8325 gcc_unreachable ();
8329 /* Parse an objc-method-definition.
8331 objc-method-definition:
8332 objc-method-type objc-method-decl ;[opt] compound-statement
8335 static void
8336 c_parser_objc_method_definition (c_parser *parser)
8338 bool is_class_method = c_parser_objc_method_type (parser);
8339 tree decl, attributes = NULL_TREE, expr = NULL_TREE;
8340 parser->objc_pq_context = true;
8341 decl = c_parser_objc_method_decl (parser, is_class_method, &attributes,
8342 &expr);
8343 if (decl == error_mark_node)
8344 return; /* Bail here. */
8346 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
8348 c_parser_consume_token (parser);
8349 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
8350 "extra semicolon in method definition specified");
8353 if (!c_parser_next_token_is (parser, CPP_OPEN_BRACE))
8355 c_parser_error (parser, "expected %<{%>");
8356 return;
8359 parser->objc_pq_context = false;
8360 if (objc_start_method_definition (is_class_method, decl, attributes, expr))
8362 add_stmt (c_parser_compound_statement (parser));
8363 objc_finish_method_definition (current_function_decl);
8365 else
8367 /* This code is executed when we find a method definition
8368 outside of an @implementation context (or invalid for other
8369 reasons). Parse the method (to keep going) but do not emit
8370 any code.
8372 c_parser_compound_statement (parser);
8376 /* Parse an objc-methodprotolist.
8378 objc-methodprotolist:
8379 empty
8380 objc-methodprotolist objc-methodproto
8381 objc-methodprotolist declaration
8382 objc-methodprotolist ;
8383 @optional
8384 @required
8386 The declaration is a data definition, which may be missing
8387 declaration specifiers under the same rules and diagnostics as
8388 other data definitions outside functions, and the stray semicolon
8389 is diagnosed the same way as a stray semicolon outside a
8390 function. */
8392 static void
8393 c_parser_objc_methodprotolist (c_parser *parser)
8395 while (true)
8397 /* The list is terminated by @end. */
8398 switch (c_parser_peek_token (parser)->type)
8400 case CPP_SEMICOLON:
8401 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
8402 "ISO C does not allow extra %<;%> outside of a function");
8403 c_parser_consume_token (parser);
8404 break;
8405 case CPP_PLUS:
8406 case CPP_MINUS:
8407 c_parser_objc_methodproto (parser);
8408 break;
8409 case CPP_PRAGMA:
8410 c_parser_pragma (parser, pragma_external);
8411 break;
8412 case CPP_EOF:
8413 return;
8414 default:
8415 if (c_parser_next_token_is_keyword (parser, RID_AT_END))
8416 return;
8417 else if (c_parser_next_token_is_keyword (parser, RID_AT_PROPERTY))
8418 c_parser_objc_at_property_declaration (parser);
8419 else if (c_parser_next_token_is_keyword (parser, RID_AT_OPTIONAL))
8421 objc_set_method_opt (true);
8422 c_parser_consume_token (parser);
8424 else if (c_parser_next_token_is_keyword (parser, RID_AT_REQUIRED))
8426 objc_set_method_opt (false);
8427 c_parser_consume_token (parser);
8429 else
8430 c_parser_declaration_or_fndef (parser, false, false, true,
8431 false, true, NULL, vNULL);
8432 break;
8437 /* Parse an objc-methodproto.
8439 objc-methodproto:
8440 objc-method-type objc-method-decl ;
8443 static void
8444 c_parser_objc_methodproto (c_parser *parser)
8446 bool is_class_method = c_parser_objc_method_type (parser);
8447 tree decl, attributes = NULL_TREE;
8449 /* Remember protocol qualifiers in prototypes. */
8450 parser->objc_pq_context = true;
8451 decl = c_parser_objc_method_decl (parser, is_class_method, &attributes,
8452 NULL);
8453 /* Forget protocol qualifiers now. */
8454 parser->objc_pq_context = false;
8456 /* Do not allow the presence of attributes to hide an erroneous
8457 method implementation in the interface section. */
8458 if (!c_parser_next_token_is (parser, CPP_SEMICOLON))
8460 c_parser_error (parser, "expected %<;%>");
8461 return;
8464 if (decl != error_mark_node)
8465 objc_add_method_declaration (is_class_method, decl, attributes);
8467 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
8470 /* If we are at a position that method attributes may be present, check that
8471 there are not any parsed already (a syntax error) and then collect any
8472 specified at the current location. Finally, if new attributes were present,
8473 check that the next token is legal ( ';' for decls and '{' for defs). */
8475 static bool
8476 c_parser_objc_maybe_method_attributes (c_parser* parser, tree* attributes)
8478 bool bad = false;
8479 if (*attributes)
8481 c_parser_error (parser,
8482 "method attributes must be specified at the end only");
8483 *attributes = NULL_TREE;
8484 bad = true;
8487 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
8488 *attributes = c_parser_attributes (parser);
8490 /* If there were no attributes here, just report any earlier error. */
8491 if (*attributes == NULL_TREE || bad)
8492 return bad;
8494 /* If the attributes are followed by a ; or {, then just report any earlier
8495 error. */
8496 if (c_parser_next_token_is (parser, CPP_SEMICOLON)
8497 || c_parser_next_token_is (parser, CPP_OPEN_BRACE))
8498 return bad;
8500 /* We've got attributes, but not at the end. */
8501 c_parser_error (parser,
8502 "expected %<;%> or %<{%> after method attribute definition");
8503 return true;
8506 /* Parse an objc-method-decl.
8508 objc-method-decl:
8509 ( objc-type-name ) objc-selector
8510 objc-selector
8511 ( objc-type-name ) objc-keyword-selector objc-optparmlist
8512 objc-keyword-selector objc-optparmlist
8513 attributes
8515 objc-keyword-selector:
8516 objc-keyword-decl
8517 objc-keyword-selector objc-keyword-decl
8519 objc-keyword-decl:
8520 objc-selector : ( objc-type-name ) identifier
8521 objc-selector : identifier
8522 : ( objc-type-name ) identifier
8523 : identifier
8525 objc-optparmlist:
8526 objc-optparms objc-optellipsis
8528 objc-optparms:
8529 empty
8530 objc-opt-parms , parameter-declaration
8532 objc-optellipsis:
8533 empty
8534 , ...
8537 static tree
8538 c_parser_objc_method_decl (c_parser *parser, bool is_class_method,
8539 tree *attributes, tree *expr)
8541 tree type = NULL_TREE;
8542 tree sel;
8543 tree parms = NULL_TREE;
8544 bool ellipsis = false;
8545 bool attr_err = false;
8547 *attributes = NULL_TREE;
8548 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
8550 c_parser_consume_token (parser);
8551 type = c_parser_objc_type_name (parser);
8552 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
8554 sel = c_parser_objc_selector (parser);
8555 /* If there is no selector, or a colon follows, we have an
8556 objc-keyword-selector. If there is a selector, and a colon does
8557 not follow, that selector ends the objc-method-decl. */
8558 if (!sel || c_parser_next_token_is (parser, CPP_COLON))
8560 tree tsel = sel;
8561 tree list = NULL_TREE;
8562 while (true)
8564 tree atype = NULL_TREE, id, keyworddecl;
8565 tree param_attr = NULL_TREE;
8566 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
8567 break;
8568 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
8570 c_parser_consume_token (parser);
8571 atype = c_parser_objc_type_name (parser);
8572 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
8573 "expected %<)%>");
8575 /* New ObjC allows attributes on method parameters. */
8576 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
8577 param_attr = c_parser_attributes (parser);
8578 if (c_parser_next_token_is_not (parser, CPP_NAME))
8580 c_parser_error (parser, "expected identifier");
8581 return error_mark_node;
8583 id = c_parser_peek_token (parser)->value;
8584 c_parser_consume_token (parser);
8585 keyworddecl = objc_build_keyword_decl (tsel, atype, id, param_attr);
8586 list = chainon (list, keyworddecl);
8587 tsel = c_parser_objc_selector (parser);
8588 if (!tsel && c_parser_next_token_is_not (parser, CPP_COLON))
8589 break;
8592 attr_err |= c_parser_objc_maybe_method_attributes (parser, attributes) ;
8594 /* Parse the optional parameter list. Optional Objective-C
8595 method parameters follow the C syntax, and may include '...'
8596 to denote a variable number of arguments. */
8597 parms = make_node (TREE_LIST);
8598 while (c_parser_next_token_is (parser, CPP_COMMA))
8600 struct c_parm *parm;
8601 c_parser_consume_token (parser);
8602 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
8604 ellipsis = true;
8605 c_parser_consume_token (parser);
8606 attr_err |= c_parser_objc_maybe_method_attributes
8607 (parser, attributes) ;
8608 break;
8610 parm = c_parser_parameter_declaration (parser, NULL_TREE);
8611 if (parm == NULL)
8612 break;
8613 parms = chainon (parms,
8614 build_tree_list (NULL_TREE, grokparm (parm, expr)));
8616 sel = list;
8618 else
8619 attr_err |= c_parser_objc_maybe_method_attributes (parser, attributes) ;
8621 if (sel == NULL)
8623 c_parser_error (parser, "objective-c method declaration is expected");
8624 return error_mark_node;
8627 if (attr_err)
8628 return error_mark_node;
8630 return objc_build_method_signature (is_class_method, type, sel, parms, ellipsis);
8633 /* Parse an objc-type-name.
8635 objc-type-name:
8636 objc-type-qualifiers[opt] type-name
8637 objc-type-qualifiers[opt]
8639 objc-type-qualifiers:
8640 objc-type-qualifier
8641 objc-type-qualifiers objc-type-qualifier
8643 objc-type-qualifier: one of
8644 in out inout bycopy byref oneway
8647 static tree
8648 c_parser_objc_type_name (c_parser *parser)
8650 tree quals = NULL_TREE;
8651 struct c_type_name *type_name = NULL;
8652 tree type = NULL_TREE;
8653 while (true)
8655 c_token *token = c_parser_peek_token (parser);
8656 if (token->type == CPP_KEYWORD
8657 && (token->keyword == RID_IN
8658 || token->keyword == RID_OUT
8659 || token->keyword == RID_INOUT
8660 || token->keyword == RID_BYCOPY
8661 || token->keyword == RID_BYREF
8662 || token->keyword == RID_ONEWAY))
8664 quals = chainon (build_tree_list (NULL_TREE, token->value), quals);
8665 c_parser_consume_token (parser);
8667 else
8668 break;
8670 if (c_parser_next_tokens_start_typename (parser, cla_prefer_type))
8671 type_name = c_parser_type_name (parser);
8672 if (type_name)
8673 type = groktypename (type_name, NULL, NULL);
8675 /* If the type is unknown, and error has already been produced and
8676 we need to recover from the error. In that case, use NULL_TREE
8677 for the type, as if no type had been specified; this will use the
8678 default type ('id') which is good for error recovery. */
8679 if (type == error_mark_node)
8680 type = NULL_TREE;
8682 return build_tree_list (quals, type);
8685 /* Parse objc-protocol-refs.
8687 objc-protocol-refs:
8688 < identifier-list >
8691 static tree
8692 c_parser_objc_protocol_refs (c_parser *parser)
8694 tree list = NULL_TREE;
8695 gcc_assert (c_parser_next_token_is (parser, CPP_LESS));
8696 c_parser_consume_token (parser);
8697 /* Any identifiers, including those declared as type names, are OK
8698 here. */
8699 while (true)
8701 tree id;
8702 if (c_parser_next_token_is_not (parser, CPP_NAME))
8704 c_parser_error (parser, "expected identifier");
8705 break;
8707 id = c_parser_peek_token (parser)->value;
8708 list = chainon (list, build_tree_list (NULL_TREE, id));
8709 c_parser_consume_token (parser);
8710 if (c_parser_next_token_is (parser, CPP_COMMA))
8711 c_parser_consume_token (parser);
8712 else
8713 break;
8715 c_parser_require (parser, CPP_GREATER, "expected %<>%>");
8716 return list;
8719 /* Parse an objc-try-catch-finally-statement.
8721 objc-try-catch-finally-statement:
8722 @try compound-statement objc-catch-list[opt]
8723 @try compound-statement objc-catch-list[opt] @finally compound-statement
8725 objc-catch-list:
8726 @catch ( objc-catch-parameter-declaration ) compound-statement
8727 objc-catch-list @catch ( objc-catch-parameter-declaration ) compound-statement
8729 objc-catch-parameter-declaration:
8730 parameter-declaration
8731 '...'
8733 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
8735 PS: This function is identical to cp_parser_objc_try_catch_finally_statement
8736 for C++. Keep them in sync. */
8738 static void
8739 c_parser_objc_try_catch_finally_statement (c_parser *parser)
8741 location_t location;
8742 tree stmt;
8744 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_TRY));
8745 c_parser_consume_token (parser);
8746 location = c_parser_peek_token (parser)->location;
8747 objc_maybe_warn_exceptions (location);
8748 stmt = c_parser_compound_statement (parser);
8749 objc_begin_try_stmt (location, stmt);
8751 while (c_parser_next_token_is_keyword (parser, RID_AT_CATCH))
8753 struct c_parm *parm;
8754 tree parameter_declaration = error_mark_node;
8755 bool seen_open_paren = false;
8757 c_parser_consume_token (parser);
8758 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
8759 seen_open_paren = true;
8760 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
8762 /* We have "@catch (...)" (where the '...' are literally
8763 what is in the code). Skip the '...'.
8764 parameter_declaration is set to NULL_TREE, and
8765 objc_being_catch_clauses() knows that that means
8766 '...'. */
8767 c_parser_consume_token (parser);
8768 parameter_declaration = NULL_TREE;
8770 else
8772 /* We have "@catch (NSException *exception)" or something
8773 like that. Parse the parameter declaration. */
8774 parm = c_parser_parameter_declaration (parser, NULL_TREE);
8775 if (parm == NULL)
8776 parameter_declaration = error_mark_node;
8777 else
8778 parameter_declaration = grokparm (parm, NULL);
8780 if (seen_open_paren)
8781 c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
8782 else
8784 /* If there was no open parenthesis, we are recovering from
8785 an error, and we are trying to figure out what mistake
8786 the user has made. */
8788 /* If there is an immediate closing parenthesis, the user
8789 probably forgot the opening one (ie, they typed "@catch
8790 NSException *e)". Parse the closing parenthesis and keep
8791 going. */
8792 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
8793 c_parser_consume_token (parser);
8795 /* If these is no immediate closing parenthesis, the user
8796 probably doesn't know that parenthesis are required at
8797 all (ie, they typed "@catch NSException *e"). So, just
8798 forget about the closing parenthesis and keep going. */
8800 objc_begin_catch_clause (parameter_declaration);
8801 if (c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
8802 c_parser_compound_statement_nostart (parser);
8803 objc_finish_catch_clause ();
8805 if (c_parser_next_token_is_keyword (parser, RID_AT_FINALLY))
8807 c_parser_consume_token (parser);
8808 location = c_parser_peek_token (parser)->location;
8809 stmt = c_parser_compound_statement (parser);
8810 objc_build_finally_clause (location, stmt);
8812 objc_finish_try_stmt ();
8815 /* Parse an objc-synchronized-statement.
8817 objc-synchronized-statement:
8818 @synchronized ( expression ) compound-statement
8821 static void
8822 c_parser_objc_synchronized_statement (c_parser *parser)
8824 location_t loc;
8825 tree expr, stmt;
8826 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_SYNCHRONIZED));
8827 c_parser_consume_token (parser);
8828 loc = c_parser_peek_token (parser)->location;
8829 objc_maybe_warn_exceptions (loc);
8830 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
8832 struct c_expr ce = c_parser_expression (parser);
8833 ce = convert_lvalue_to_rvalue (loc, ce, false, false);
8834 expr = ce.value;
8835 expr = c_fully_fold (expr, false, NULL);
8836 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
8838 else
8839 expr = error_mark_node;
8840 stmt = c_parser_compound_statement (parser);
8841 objc_build_synchronized (loc, expr, stmt);
8844 /* Parse an objc-selector; return NULL_TREE without an error if the
8845 next token is not an objc-selector.
8847 objc-selector:
8848 identifier
8849 one of
8850 enum struct union if else while do for switch case default
8851 break continue return goto asm sizeof typeof __alignof
8852 unsigned long const short volatile signed restrict _Complex
8853 in out inout bycopy byref oneway int char float double void _Bool
8854 _Atomic
8856 ??? Why this selection of keywords but not, for example, storage
8857 class specifiers? */
8859 static tree
8860 c_parser_objc_selector (c_parser *parser)
8862 c_token *token = c_parser_peek_token (parser);
8863 tree value = token->value;
8864 if (token->type == CPP_NAME)
8866 c_parser_consume_token (parser);
8867 return value;
8869 if (token->type != CPP_KEYWORD)
8870 return NULL_TREE;
8871 switch (token->keyword)
8873 case RID_ENUM:
8874 case RID_STRUCT:
8875 case RID_UNION:
8876 case RID_IF:
8877 case RID_ELSE:
8878 case RID_WHILE:
8879 case RID_DO:
8880 case RID_FOR:
8881 case RID_SWITCH:
8882 case RID_CASE:
8883 case RID_DEFAULT:
8884 case RID_BREAK:
8885 case RID_CONTINUE:
8886 case RID_RETURN:
8887 case RID_GOTO:
8888 case RID_ASM:
8889 case RID_SIZEOF:
8890 case RID_TYPEOF:
8891 case RID_ALIGNOF:
8892 case RID_UNSIGNED:
8893 case RID_LONG:
8894 case RID_INT128:
8895 case RID_CONST:
8896 case RID_SHORT:
8897 case RID_VOLATILE:
8898 case RID_SIGNED:
8899 case RID_RESTRICT:
8900 case RID_COMPLEX:
8901 case RID_IN:
8902 case RID_OUT:
8903 case RID_INOUT:
8904 case RID_BYCOPY:
8905 case RID_BYREF:
8906 case RID_ONEWAY:
8907 case RID_INT:
8908 case RID_CHAR:
8909 case RID_FLOAT:
8910 case RID_DOUBLE:
8911 case RID_VOID:
8912 case RID_BOOL:
8913 case RID_ATOMIC:
8914 case RID_AUTO_TYPE:
8915 c_parser_consume_token (parser);
8916 return value;
8917 default:
8918 return NULL_TREE;
8922 /* Parse an objc-selector-arg.
8924 objc-selector-arg:
8925 objc-selector
8926 objc-keywordname-list
8928 objc-keywordname-list:
8929 objc-keywordname
8930 objc-keywordname-list objc-keywordname
8932 objc-keywordname:
8933 objc-selector :
8937 static tree
8938 c_parser_objc_selector_arg (c_parser *parser)
8940 tree sel = c_parser_objc_selector (parser);
8941 tree list = NULL_TREE;
8942 if (sel && c_parser_next_token_is_not (parser, CPP_COLON))
8943 return sel;
8944 while (true)
8946 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
8947 return list;
8948 list = chainon (list, build_tree_list (sel, NULL_TREE));
8949 sel = c_parser_objc_selector (parser);
8950 if (!sel && c_parser_next_token_is_not (parser, CPP_COLON))
8951 break;
8953 return list;
8956 /* Parse an objc-receiver.
8958 objc-receiver:
8959 expression
8960 class-name
8961 type-name
8964 static tree
8965 c_parser_objc_receiver (c_parser *parser)
8967 location_t loc = c_parser_peek_token (parser)->location;
8969 if (c_parser_peek_token (parser)->type == CPP_NAME
8970 && (c_parser_peek_token (parser)->id_kind == C_ID_TYPENAME
8971 || c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME))
8973 tree id = c_parser_peek_token (parser)->value;
8974 c_parser_consume_token (parser);
8975 return objc_get_class_reference (id);
8977 struct c_expr ce = c_parser_expression (parser);
8978 ce = convert_lvalue_to_rvalue (loc, ce, false, false);
8979 return c_fully_fold (ce.value, false, NULL);
8982 /* Parse objc-message-args.
8984 objc-message-args:
8985 objc-selector
8986 objc-keywordarg-list
8988 objc-keywordarg-list:
8989 objc-keywordarg
8990 objc-keywordarg-list objc-keywordarg
8992 objc-keywordarg:
8993 objc-selector : objc-keywordexpr
8994 : objc-keywordexpr
8997 static tree
8998 c_parser_objc_message_args (c_parser *parser)
9000 tree sel = c_parser_objc_selector (parser);
9001 tree list = NULL_TREE;
9002 if (sel && c_parser_next_token_is_not (parser, CPP_COLON))
9003 return sel;
9004 while (true)
9006 tree keywordexpr;
9007 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
9008 return error_mark_node;
9009 keywordexpr = c_parser_objc_keywordexpr (parser);
9010 list = chainon (list, build_tree_list (sel, keywordexpr));
9011 sel = c_parser_objc_selector (parser);
9012 if (!sel && c_parser_next_token_is_not (parser, CPP_COLON))
9013 break;
9015 return list;
9018 /* Parse an objc-keywordexpr.
9020 objc-keywordexpr:
9021 nonempty-expr-list
9024 static tree
9025 c_parser_objc_keywordexpr (c_parser *parser)
9027 tree ret;
9028 vec<tree, va_gc> *expr_list = c_parser_expr_list (parser, true, true,
9029 NULL, NULL, NULL, NULL);
9030 if (vec_safe_length (expr_list) == 1)
9032 /* Just return the expression, remove a level of
9033 indirection. */
9034 ret = (*expr_list)[0];
9036 else
9038 /* We have a comma expression, we will collapse later. */
9039 ret = build_tree_list_vec (expr_list);
9041 release_tree_vector (expr_list);
9042 return ret;
9045 /* A check, needed in several places, that ObjC interface, implementation or
9046 method definitions are not prefixed by incorrect items. */
9047 static bool
9048 c_parser_objc_diagnose_bad_element_prefix (c_parser *parser,
9049 struct c_declspecs *specs)
9051 if (!specs->declspecs_seen_p || specs->non_sc_seen_p
9052 || specs->typespec_kind != ctsk_none)
9054 c_parser_error (parser,
9055 "no type or storage class may be specified here,");
9056 c_parser_skip_to_end_of_block_or_statement (parser);
9057 return true;
9059 return false;
9062 /* Parse an Objective-C @property declaration. The syntax is:
9064 objc-property-declaration:
9065 '@property' objc-property-attributes[opt] struct-declaration ;
9067 objc-property-attributes:
9068 '(' objc-property-attribute-list ')'
9070 objc-property-attribute-list:
9071 objc-property-attribute
9072 objc-property-attribute-list, objc-property-attribute
9074 objc-property-attribute
9075 'getter' = identifier
9076 'setter' = identifier
9077 'readonly'
9078 'readwrite'
9079 'assign'
9080 'retain'
9081 'copy'
9082 'nonatomic'
9084 For example:
9085 @property NSString *name;
9086 @property (readonly) id object;
9087 @property (retain, nonatomic, getter=getTheName) id name;
9088 @property int a, b, c;
9090 PS: This function is identical to cp_parser_objc_at_propery_declaration
9091 for C++. Keep them in sync. */
9092 static void
9093 c_parser_objc_at_property_declaration (c_parser *parser)
9095 /* The following variables hold the attributes of the properties as
9096 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
9097 seen. When we see an attribute, we set them to 'true' (if they
9098 are boolean properties) or to the identifier (if they have an
9099 argument, ie, for getter and setter). Note that here we only
9100 parse the list of attributes, check the syntax and accumulate the
9101 attributes that we find. objc_add_property_declaration() will
9102 then process the information. */
9103 bool property_assign = false;
9104 bool property_copy = false;
9105 tree property_getter_ident = NULL_TREE;
9106 bool property_nonatomic = false;
9107 bool property_readonly = false;
9108 bool property_readwrite = false;
9109 bool property_retain = false;
9110 tree property_setter_ident = NULL_TREE;
9112 /* 'properties' is the list of properties that we read. Usually a
9113 single one, but maybe more (eg, in "@property int a, b, c;" there
9114 are three). */
9115 tree properties;
9116 location_t loc;
9118 loc = c_parser_peek_token (parser)->location;
9119 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_PROPERTY));
9121 c_parser_consume_token (parser); /* Eat '@property'. */
9123 /* Parse the optional attribute list... */
9124 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
9126 /* Eat the '(' */
9127 c_parser_consume_token (parser);
9129 /* Property attribute keywords are valid now. */
9130 parser->objc_property_attr_context = true;
9132 while (true)
9134 bool syntax_error = false;
9135 c_token *token = c_parser_peek_token (parser);
9136 enum rid keyword;
9138 if (token->type != CPP_KEYWORD)
9140 if (token->type == CPP_CLOSE_PAREN)
9141 c_parser_error (parser, "expected identifier");
9142 else
9144 c_parser_consume_token (parser);
9145 c_parser_error (parser, "unknown property attribute");
9147 break;
9149 keyword = token->keyword;
9150 c_parser_consume_token (parser);
9151 switch (keyword)
9153 case RID_ASSIGN: property_assign = true; break;
9154 case RID_COPY: property_copy = true; break;
9155 case RID_NONATOMIC: property_nonatomic = true; break;
9156 case RID_READONLY: property_readonly = true; break;
9157 case RID_READWRITE: property_readwrite = true; break;
9158 case RID_RETAIN: property_retain = true; break;
9160 case RID_GETTER:
9161 case RID_SETTER:
9162 if (c_parser_next_token_is_not (parser, CPP_EQ))
9164 if (keyword == RID_GETTER)
9165 c_parser_error (parser,
9166 "missing %<=%> (after %<getter%> attribute)");
9167 else
9168 c_parser_error (parser,
9169 "missing %<=%> (after %<setter%> attribute)");
9170 syntax_error = true;
9171 break;
9173 c_parser_consume_token (parser); /* eat the = */
9174 if (c_parser_next_token_is_not (parser, CPP_NAME))
9176 c_parser_error (parser, "expected identifier");
9177 syntax_error = true;
9178 break;
9180 if (keyword == RID_SETTER)
9182 if (property_setter_ident != NULL_TREE)
9183 c_parser_error (parser, "the %<setter%> attribute may only be specified once");
9184 else
9185 property_setter_ident = c_parser_peek_token (parser)->value;
9186 c_parser_consume_token (parser);
9187 if (c_parser_next_token_is_not (parser, CPP_COLON))
9188 c_parser_error (parser, "setter name must terminate with %<:%>");
9189 else
9190 c_parser_consume_token (parser);
9192 else
9194 if (property_getter_ident != NULL_TREE)
9195 c_parser_error (parser, "the %<getter%> attribute may only be specified once");
9196 else
9197 property_getter_ident = c_parser_peek_token (parser)->value;
9198 c_parser_consume_token (parser);
9200 break;
9201 default:
9202 c_parser_error (parser, "unknown property attribute");
9203 syntax_error = true;
9204 break;
9207 if (syntax_error)
9208 break;
9210 if (c_parser_next_token_is (parser, CPP_COMMA))
9211 c_parser_consume_token (parser);
9212 else
9213 break;
9215 parser->objc_property_attr_context = false;
9216 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
9218 /* ... and the property declaration(s). */
9219 properties = c_parser_struct_declaration (parser);
9221 if (properties == error_mark_node)
9223 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
9224 parser->error = false;
9225 return;
9228 if (properties == NULL_TREE)
9229 c_parser_error (parser, "expected identifier");
9230 else
9232 /* Comma-separated properties are chained together in
9233 reverse order; add them one by one. */
9234 properties = nreverse (properties);
9236 for (; properties; properties = TREE_CHAIN (properties))
9237 objc_add_property_declaration (loc, copy_node (properties),
9238 property_readonly, property_readwrite,
9239 property_assign, property_retain,
9240 property_copy, property_nonatomic,
9241 property_getter_ident, property_setter_ident);
9244 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
9245 parser->error = false;
9248 /* Parse an Objective-C @synthesize declaration. The syntax is:
9250 objc-synthesize-declaration:
9251 @synthesize objc-synthesize-identifier-list ;
9253 objc-synthesize-identifier-list:
9254 objc-synthesize-identifier
9255 objc-synthesize-identifier-list, objc-synthesize-identifier
9257 objc-synthesize-identifier
9258 identifier
9259 identifier = identifier
9261 For example:
9262 @synthesize MyProperty;
9263 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
9265 PS: This function is identical to cp_parser_objc_at_synthesize_declaration
9266 for C++. Keep them in sync.
9268 static void
9269 c_parser_objc_at_synthesize_declaration (c_parser *parser)
9271 tree list = NULL_TREE;
9272 location_t loc;
9273 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_SYNTHESIZE));
9274 loc = c_parser_peek_token (parser)->location;
9276 c_parser_consume_token (parser);
9277 while (true)
9279 tree property, ivar;
9280 if (c_parser_next_token_is_not (parser, CPP_NAME))
9282 c_parser_error (parser, "expected identifier");
9283 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
9284 /* Once we find the semicolon, we can resume normal parsing.
9285 We have to reset parser->error manually because
9286 c_parser_skip_until_found() won't reset it for us if the
9287 next token is precisely a semicolon. */
9288 parser->error = false;
9289 return;
9291 property = c_parser_peek_token (parser)->value;
9292 c_parser_consume_token (parser);
9293 if (c_parser_next_token_is (parser, CPP_EQ))
9295 c_parser_consume_token (parser);
9296 if (c_parser_next_token_is_not (parser, CPP_NAME))
9298 c_parser_error (parser, "expected identifier");
9299 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
9300 parser->error = false;
9301 return;
9303 ivar = c_parser_peek_token (parser)->value;
9304 c_parser_consume_token (parser);
9306 else
9307 ivar = NULL_TREE;
9308 list = chainon (list, build_tree_list (ivar, property));
9309 if (c_parser_next_token_is (parser, CPP_COMMA))
9310 c_parser_consume_token (parser);
9311 else
9312 break;
9314 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
9315 objc_add_synthesize_declaration (loc, list);
9318 /* Parse an Objective-C @dynamic declaration. The syntax is:
9320 objc-dynamic-declaration:
9321 @dynamic identifier-list ;
9323 For example:
9324 @dynamic MyProperty;
9325 @dynamic MyProperty, AnotherProperty;
9327 PS: This function is identical to cp_parser_objc_at_dynamic_declaration
9328 for C++. Keep them in sync.
9330 static void
9331 c_parser_objc_at_dynamic_declaration (c_parser *parser)
9333 tree list = NULL_TREE;
9334 location_t loc;
9335 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_DYNAMIC));
9336 loc = c_parser_peek_token (parser)->location;
9338 c_parser_consume_token (parser);
9339 while (true)
9341 tree property;
9342 if (c_parser_next_token_is_not (parser, CPP_NAME))
9344 c_parser_error (parser, "expected identifier");
9345 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
9346 parser->error = false;
9347 return;
9349 property = c_parser_peek_token (parser)->value;
9350 list = chainon (list, build_tree_list (NULL_TREE, property));
9351 c_parser_consume_token (parser);
9352 if (c_parser_next_token_is (parser, CPP_COMMA))
9353 c_parser_consume_token (parser);
9354 else
9355 break;
9357 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
9358 objc_add_dynamic_declaration (loc, list);
9362 /* Handle pragmas. Some OpenMP pragmas are associated with, and therefore
9363 should be considered, statements. ALLOW_STMT is true if we're within
9364 the context of a function and such pragmas are to be allowed. Returns
9365 true if we actually parsed such a pragma. */
9367 static bool
9368 c_parser_pragma (c_parser *parser, enum pragma_context context)
9370 unsigned int id;
9372 id = c_parser_peek_token (parser)->pragma_kind;
9373 gcc_assert (id != PRAGMA_NONE);
9375 switch (id)
9377 case PRAGMA_OMP_BARRIER:
9378 if (context != pragma_compound)
9380 if (context == pragma_stmt)
9381 c_parser_error (parser, "%<#pragma omp barrier%> may only be "
9382 "used in compound statements");
9383 goto bad_stmt;
9385 c_parser_omp_barrier (parser);
9386 return false;
9388 case PRAGMA_OMP_FLUSH:
9389 if (context != pragma_compound)
9391 if (context == pragma_stmt)
9392 c_parser_error (parser, "%<#pragma omp flush%> may only be "
9393 "used in compound statements");
9394 goto bad_stmt;
9396 c_parser_omp_flush (parser);
9397 return false;
9399 case PRAGMA_OMP_TASKWAIT:
9400 if (context != pragma_compound)
9402 if (context == pragma_stmt)
9403 c_parser_error (parser, "%<#pragma omp taskwait%> may only be "
9404 "used in compound statements");
9405 goto bad_stmt;
9407 c_parser_omp_taskwait (parser);
9408 return false;
9410 case PRAGMA_OMP_TASKYIELD:
9411 if (context != pragma_compound)
9413 if (context == pragma_stmt)
9414 c_parser_error (parser, "%<#pragma omp taskyield%> may only be "
9415 "used in compound statements");
9416 goto bad_stmt;
9418 c_parser_omp_taskyield (parser);
9419 return false;
9421 case PRAGMA_OMP_CANCEL:
9422 if (context != pragma_compound)
9424 if (context == pragma_stmt)
9425 c_parser_error (parser, "%<#pragma omp cancel%> may only be "
9426 "used in compound statements");
9427 goto bad_stmt;
9429 c_parser_omp_cancel (parser);
9430 return false;
9432 case PRAGMA_OMP_CANCELLATION_POINT:
9433 if (context != pragma_compound)
9435 if (context == pragma_stmt)
9436 c_parser_error (parser, "%<#pragma omp cancellation point%> may "
9437 "only be used in compound statements");
9438 goto bad_stmt;
9440 c_parser_omp_cancellation_point (parser);
9441 return false;
9443 case PRAGMA_OMP_THREADPRIVATE:
9444 c_parser_omp_threadprivate (parser);
9445 return false;
9447 case PRAGMA_OMP_TARGET:
9448 return c_parser_omp_target (parser, context);
9450 case PRAGMA_OMP_END_DECLARE_TARGET:
9451 c_parser_omp_end_declare_target (parser);
9452 return false;
9454 case PRAGMA_OMP_SECTION:
9455 error_at (c_parser_peek_token (parser)->location,
9456 "%<#pragma omp section%> may only be used in "
9457 "%<#pragma omp sections%> construct");
9458 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
9459 return false;
9461 case PRAGMA_OMP_DECLARE_REDUCTION:
9462 c_parser_omp_declare (parser, context);
9463 return false;
9464 case PRAGMA_IVDEP:
9465 c_parser_consume_pragma (parser);
9466 c_parser_skip_to_pragma_eol (parser);
9467 if (!c_parser_next_token_is_keyword (parser, RID_FOR)
9468 && !c_parser_next_token_is_keyword (parser, RID_WHILE)
9469 && !c_parser_next_token_is_keyword (parser, RID_DO))
9471 c_parser_error (parser, "for, while or do statement expected");
9472 return false;
9474 if (c_parser_next_token_is_keyword (parser, RID_FOR))
9475 c_parser_for_statement (parser, true);
9476 else if (c_parser_next_token_is_keyword (parser, RID_WHILE))
9477 c_parser_while_statement (parser, true);
9478 else
9479 c_parser_do_statement (parser, true);
9480 return false;
9482 case PRAGMA_GCC_PCH_PREPROCESS:
9483 c_parser_error (parser, "%<#pragma GCC pch_preprocess%> must be first");
9484 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
9485 return false;
9487 case PRAGMA_CILK_SIMD:
9488 if (!c_parser_cilk_verify_simd (parser, context))
9489 return false;
9490 c_parser_consume_pragma (parser);
9491 c_parser_cilk_simd (parser);
9492 return false;
9494 default:
9495 if (id < PRAGMA_FIRST_EXTERNAL)
9497 if (context != pragma_stmt && context != pragma_compound)
9499 bad_stmt:
9500 c_parser_error (parser, "expected declaration specifiers");
9501 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
9502 return false;
9504 c_parser_omp_construct (parser);
9505 return true;
9507 break;
9510 c_parser_consume_pragma (parser);
9511 c_invoke_pragma_handler (id);
9513 /* Skip to EOL, but suppress any error message. Those will have been
9514 generated by the handler routine through calling error, as opposed
9515 to calling c_parser_error. */
9516 parser->error = true;
9517 c_parser_skip_to_pragma_eol (parser);
9519 return false;
9522 /* The interface the pragma parsers have to the lexer. */
9524 enum cpp_ttype
9525 pragma_lex (tree *value)
9527 c_token *tok = c_parser_peek_token (the_parser);
9528 enum cpp_ttype ret = tok->type;
9530 *value = tok->value;
9531 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
9532 ret = CPP_EOF;
9533 else
9535 if (ret == CPP_KEYWORD)
9536 ret = CPP_NAME;
9537 c_parser_consume_token (the_parser);
9540 return ret;
9543 static void
9544 c_parser_pragma_pch_preprocess (c_parser *parser)
9546 tree name = NULL;
9548 c_parser_consume_pragma (parser);
9549 if (c_parser_next_token_is (parser, CPP_STRING))
9551 name = c_parser_peek_token (parser)->value;
9552 c_parser_consume_token (parser);
9554 else
9555 c_parser_error (parser, "expected string literal");
9556 c_parser_skip_to_pragma_eol (parser);
9558 if (name)
9559 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
9562 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 parsing routines. */
9564 /* Returns name of the next clause.
9565 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
9566 the token is not consumed. Otherwise appropriate pragma_omp_clause is
9567 returned and the token is consumed. */
9569 static pragma_omp_clause
9570 c_parser_omp_clause_name (c_parser *parser)
9572 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
9574 if (c_parser_next_token_is_keyword (parser, RID_IF))
9575 result = PRAGMA_OMP_CLAUSE_IF;
9576 else if (c_parser_next_token_is_keyword (parser, RID_DEFAULT))
9577 result = PRAGMA_OMP_CLAUSE_DEFAULT;
9578 else if (c_parser_next_token_is_keyword (parser, RID_FOR))
9579 result = PRAGMA_OMP_CLAUSE_FOR;
9580 else if (c_parser_next_token_is (parser, CPP_NAME))
9582 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
9584 switch (p[0])
9586 case 'a':
9587 if (!strcmp ("aligned", p))
9588 result = PRAGMA_OMP_CLAUSE_ALIGNED;
9589 break;
9590 case 'c':
9591 if (!strcmp ("collapse", p))
9592 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
9593 else if (!strcmp ("copyin", p))
9594 result = PRAGMA_OMP_CLAUSE_COPYIN;
9595 else if (!strcmp ("copyprivate", p))
9596 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
9597 break;
9598 case 'd':
9599 if (!strcmp ("depend", p))
9600 result = PRAGMA_OMP_CLAUSE_DEPEND;
9601 else if (!strcmp ("device", p))
9602 result = PRAGMA_OMP_CLAUSE_DEVICE;
9603 else if (!strcmp ("dist_schedule", p))
9604 result = PRAGMA_OMP_CLAUSE_DIST_SCHEDULE;
9605 break;
9606 case 'f':
9607 if (!strcmp ("final", p))
9608 result = PRAGMA_OMP_CLAUSE_FINAL;
9609 else if (!strcmp ("firstprivate", p))
9610 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
9611 else if (!strcmp ("from", p))
9612 result = PRAGMA_OMP_CLAUSE_FROM;
9613 break;
9614 case 'i':
9615 if (!strcmp ("inbranch", p))
9616 result = PRAGMA_OMP_CLAUSE_INBRANCH;
9617 break;
9618 case 'l':
9619 if (!strcmp ("lastprivate", p))
9620 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
9621 else if (!strcmp ("linear", p))
9622 result = PRAGMA_OMP_CLAUSE_LINEAR;
9623 break;
9624 case 'm':
9625 if (!strcmp ("map", p))
9626 result = PRAGMA_OMP_CLAUSE_MAP;
9627 else if (!strcmp ("mergeable", p))
9628 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
9629 else if (flag_cilkplus && !strcmp ("mask", p))
9630 result = PRAGMA_CILK_CLAUSE_MASK;
9631 break;
9632 case 'n':
9633 if (!strcmp ("notinbranch", p))
9634 result = PRAGMA_OMP_CLAUSE_NOTINBRANCH;
9635 else if (!strcmp ("nowait", p))
9636 result = PRAGMA_OMP_CLAUSE_NOWAIT;
9637 else if (!strcmp ("num_teams", p))
9638 result = PRAGMA_OMP_CLAUSE_NUM_TEAMS;
9639 else if (!strcmp ("num_threads", p))
9640 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
9641 else if (flag_cilkplus && !strcmp ("nomask", p))
9642 result = PRAGMA_CILK_CLAUSE_NOMASK;
9643 break;
9644 case 'o':
9645 if (!strcmp ("ordered", p))
9646 result = PRAGMA_OMP_CLAUSE_ORDERED;
9647 break;
9648 case 'p':
9649 if (!strcmp ("parallel", p))
9650 result = PRAGMA_OMP_CLAUSE_PARALLEL;
9651 else if (!strcmp ("private", p))
9652 result = PRAGMA_OMP_CLAUSE_PRIVATE;
9653 else if (!strcmp ("proc_bind", p))
9654 result = PRAGMA_OMP_CLAUSE_PROC_BIND;
9655 break;
9656 case 'r':
9657 if (!strcmp ("reduction", p))
9658 result = PRAGMA_OMP_CLAUSE_REDUCTION;
9659 break;
9660 case 's':
9661 if (!strcmp ("safelen", p))
9662 result = PRAGMA_OMP_CLAUSE_SAFELEN;
9663 else if (!strcmp ("schedule", p))
9664 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
9665 else if (!strcmp ("sections", p))
9666 result = PRAGMA_OMP_CLAUSE_SECTIONS;
9667 else if (!strcmp ("shared", p))
9668 result = PRAGMA_OMP_CLAUSE_SHARED;
9669 else if (!strcmp ("simdlen", p))
9670 result = PRAGMA_OMP_CLAUSE_SIMDLEN;
9671 break;
9672 case 't':
9673 if (!strcmp ("taskgroup", p))
9674 result = PRAGMA_OMP_CLAUSE_TASKGROUP;
9675 else if (!strcmp ("thread_limit", p))
9676 result = PRAGMA_OMP_CLAUSE_THREAD_LIMIT;
9677 else if (!strcmp ("to", p))
9678 result = PRAGMA_OMP_CLAUSE_TO;
9679 break;
9680 case 'u':
9681 if (!strcmp ("uniform", p))
9682 result = PRAGMA_OMP_CLAUSE_UNIFORM;
9683 else if (!strcmp ("untied", p))
9684 result = PRAGMA_OMP_CLAUSE_UNTIED;
9685 break;
9686 case 'v':
9687 if (flag_cilkplus && !strcmp ("vectorlength", p))
9688 result = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
9689 break;
9693 if (result != PRAGMA_OMP_CLAUSE_NONE)
9694 c_parser_consume_token (parser);
9696 return result;
9699 /* Validate that a clause of the given type does not already exist. */
9701 static void
9702 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
9703 const char *name)
9705 tree c;
9707 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
9708 if (OMP_CLAUSE_CODE (c) == code)
9710 location_t loc = OMP_CLAUSE_LOCATION (c);
9711 error_at (loc, "too many %qs clauses", name);
9712 break;
9716 /* OpenMP 2.5:
9717 variable-list:
9718 identifier
9719 variable-list , identifier
9721 If KIND is nonzero, create the appropriate node and install the
9722 decl in OMP_CLAUSE_DECL and add the node to the head of the list.
9723 If KIND is nonzero, CLAUSE_LOC is the location of the clause.
9725 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
9726 return the list created. */
9728 static tree
9729 c_parser_omp_variable_list (c_parser *parser,
9730 location_t clause_loc,
9731 enum omp_clause_code kind, tree list)
9733 if (c_parser_next_token_is_not (parser, CPP_NAME)
9734 || c_parser_peek_token (parser)->id_kind != C_ID_ID)
9735 c_parser_error (parser, "expected identifier");
9737 while (c_parser_next_token_is (parser, CPP_NAME)
9738 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
9740 tree t = lookup_name (c_parser_peek_token (parser)->value);
9742 if (t == NULL_TREE)
9744 undeclared_variable (c_parser_peek_token (parser)->location,
9745 c_parser_peek_token (parser)->value);
9746 t = error_mark_node;
9749 c_parser_consume_token (parser);
9751 if (t == error_mark_node)
9753 else if (kind != 0)
9755 switch (kind)
9757 case OMP_CLAUSE_MAP:
9758 case OMP_CLAUSE_FROM:
9759 case OMP_CLAUSE_TO:
9760 case OMP_CLAUSE_DEPEND:
9761 while (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
9763 tree low_bound = NULL_TREE, length = NULL_TREE;
9765 c_parser_consume_token (parser);
9766 if (!c_parser_next_token_is (parser, CPP_COLON))
9768 low_bound = c_parser_expression (parser).value;
9769 mark_exp_read (low_bound);
9771 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
9772 length = integer_one_node;
9773 else
9775 /* Look for `:'. */
9776 if (!c_parser_require (parser, CPP_COLON,
9777 "expected %<:%>"))
9779 t = error_mark_node;
9780 break;
9782 if (!c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
9784 length = c_parser_expression (parser).value;
9785 mark_exp_read (length);
9788 /* Look for the closing `]'. */
9789 if (!c_parser_require (parser, CPP_CLOSE_SQUARE,
9790 "expected %<]%>"))
9792 t = error_mark_node;
9793 break;
9795 t = tree_cons (low_bound, length, t);
9797 break;
9798 default:
9799 break;
9802 if (t != error_mark_node)
9804 tree u = build_omp_clause (clause_loc, kind);
9805 OMP_CLAUSE_DECL (u) = t;
9806 OMP_CLAUSE_CHAIN (u) = list;
9807 list = u;
9810 else
9811 list = tree_cons (t, NULL_TREE, list);
9813 if (c_parser_next_token_is_not (parser, CPP_COMMA))
9814 break;
9816 c_parser_consume_token (parser);
9819 return list;
9822 /* Similarly, but expect leading and trailing parenthesis. This is a very
9823 common case for omp clauses. */
9825 static tree
9826 c_parser_omp_var_list_parens (c_parser *parser, enum omp_clause_code kind,
9827 tree list)
9829 /* The clauses location. */
9830 location_t loc = c_parser_peek_token (parser)->location;
9832 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
9834 list = c_parser_omp_variable_list (parser, loc, kind, list);
9835 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
9837 return list;
9840 /* OpenMP 3.0:
9841 collapse ( constant-expression ) */
9843 static tree
9844 c_parser_omp_clause_collapse (c_parser *parser, tree list)
9846 tree c, num = error_mark_node;
9847 HOST_WIDE_INT n;
9848 location_t loc;
9850 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse");
9852 loc = c_parser_peek_token (parser)->location;
9853 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
9855 num = c_parser_expr_no_commas (parser, NULL).value;
9856 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
9858 if (num == error_mark_node)
9859 return list;
9860 mark_exp_read (num);
9861 num = c_fully_fold (num, false, NULL);
9862 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
9863 || !tree_fits_shwi_p (num)
9864 || (n = tree_to_shwi (num)) <= 0
9865 || (int) n != n)
9867 error_at (loc,
9868 "collapse argument needs positive constant integer expression");
9869 return list;
9871 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
9872 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
9873 OMP_CLAUSE_CHAIN (c) = list;
9874 return c;
9877 /* OpenMP 2.5:
9878 copyin ( variable-list ) */
9880 static tree
9881 c_parser_omp_clause_copyin (c_parser *parser, tree list)
9883 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_COPYIN, list);
9886 /* OpenMP 2.5:
9887 copyprivate ( variable-list ) */
9889 static tree
9890 c_parser_omp_clause_copyprivate (c_parser *parser, tree list)
9892 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_COPYPRIVATE, list);
9895 /* OpenMP 2.5:
9896 default ( shared | none ) */
9898 static tree
9899 c_parser_omp_clause_default (c_parser *parser, tree list)
9901 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
9902 location_t loc = c_parser_peek_token (parser)->location;
9903 tree c;
9905 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
9906 return list;
9907 if (c_parser_next_token_is (parser, CPP_NAME))
9909 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
9911 switch (p[0])
9913 case 'n':
9914 if (strcmp ("none", p) != 0)
9915 goto invalid_kind;
9916 kind = OMP_CLAUSE_DEFAULT_NONE;
9917 break;
9919 case 's':
9920 if (strcmp ("shared", p) != 0)
9921 goto invalid_kind;
9922 kind = OMP_CLAUSE_DEFAULT_SHARED;
9923 break;
9925 default:
9926 goto invalid_kind;
9929 c_parser_consume_token (parser);
9931 else
9933 invalid_kind:
9934 c_parser_error (parser, "expected %<none%> or %<shared%>");
9936 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
9938 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
9939 return list;
9941 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default");
9942 c = build_omp_clause (loc, OMP_CLAUSE_DEFAULT);
9943 OMP_CLAUSE_CHAIN (c) = list;
9944 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
9946 return c;
9949 /* OpenMP 2.5:
9950 firstprivate ( variable-list ) */
9952 static tree
9953 c_parser_omp_clause_firstprivate (c_parser *parser, tree list)
9955 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_FIRSTPRIVATE, list);
9958 /* OpenMP 3.1:
9959 final ( expression ) */
9961 static tree
9962 c_parser_omp_clause_final (c_parser *parser, tree list)
9964 location_t loc = c_parser_peek_token (parser)->location;
9965 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
9967 tree t = c_parser_paren_condition (parser);
9968 tree c;
9970 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final");
9972 c = build_omp_clause (loc, OMP_CLAUSE_FINAL);
9973 OMP_CLAUSE_FINAL_EXPR (c) = t;
9974 OMP_CLAUSE_CHAIN (c) = list;
9975 list = c;
9977 else
9978 c_parser_error (parser, "expected %<(%>");
9980 return list;
9983 /* OpenMP 2.5:
9984 if ( expression ) */
9986 static tree
9987 c_parser_omp_clause_if (c_parser *parser, tree list)
9989 location_t loc = c_parser_peek_token (parser)->location;
9990 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
9992 tree t = c_parser_paren_condition (parser);
9993 tree c;
9995 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if");
9997 c = build_omp_clause (loc, OMP_CLAUSE_IF);
9998 OMP_CLAUSE_IF_EXPR (c) = t;
9999 OMP_CLAUSE_CHAIN (c) = list;
10000 list = c;
10002 else
10003 c_parser_error (parser, "expected %<(%>");
10005 return list;
10008 /* OpenMP 2.5:
10009 lastprivate ( variable-list ) */
10011 static tree
10012 c_parser_omp_clause_lastprivate (c_parser *parser, tree list)
10014 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_LASTPRIVATE, list);
10017 /* OpenMP 3.1:
10018 mergeable */
10020 static tree
10021 c_parser_omp_clause_mergeable (c_parser *parser ATTRIBUTE_UNUSED, tree list)
10023 tree c;
10025 /* FIXME: Should we allow duplicates? */
10026 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable");
10028 c = build_omp_clause (c_parser_peek_token (parser)->location,
10029 OMP_CLAUSE_MERGEABLE);
10030 OMP_CLAUSE_CHAIN (c) = list;
10032 return c;
10035 /* OpenMP 2.5:
10036 nowait */
10038 static tree
10039 c_parser_omp_clause_nowait (c_parser *parser ATTRIBUTE_UNUSED, tree list)
10041 tree c;
10042 location_t loc = c_parser_peek_token (parser)->location;
10044 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait");
10046 c = build_omp_clause (loc, OMP_CLAUSE_NOWAIT);
10047 OMP_CLAUSE_CHAIN (c) = list;
10048 return c;
10051 /* OpenMP 2.5:
10052 num_threads ( expression ) */
10054 static tree
10055 c_parser_omp_clause_num_threads (c_parser *parser, tree list)
10057 location_t num_threads_loc = c_parser_peek_token (parser)->location;
10058 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10060 location_t expr_loc = c_parser_peek_token (parser)->location;
10061 tree c, t = c_parser_expression (parser).value;
10062 mark_exp_read (t);
10063 t = c_fully_fold (t, false, NULL);
10065 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10067 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
10069 c_parser_error (parser, "expected integer expression");
10070 return list;
10073 /* Attempt to statically determine when the number isn't positive. */
10074 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
10075 build_int_cst (TREE_TYPE (t), 0));
10076 if (CAN_HAVE_LOCATION_P (c))
10077 SET_EXPR_LOCATION (c, expr_loc);
10078 if (c == boolean_true_node)
10080 warning_at (expr_loc, 0,
10081 "%<num_threads%> value must be positive");
10082 t = integer_one_node;
10085 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS, "num_threads");
10087 c = build_omp_clause (num_threads_loc, OMP_CLAUSE_NUM_THREADS);
10088 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
10089 OMP_CLAUSE_CHAIN (c) = list;
10090 list = c;
10093 return list;
10096 /* OpenMP 2.5:
10097 ordered */
10099 static tree
10100 c_parser_omp_clause_ordered (c_parser *parser, tree list)
10102 tree c;
10104 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED, "ordered");
10106 c = build_omp_clause (c_parser_peek_token (parser)->location,
10107 OMP_CLAUSE_ORDERED);
10108 OMP_CLAUSE_CHAIN (c) = list;
10110 return c;
10113 /* OpenMP 2.5:
10114 private ( variable-list ) */
10116 static tree
10117 c_parser_omp_clause_private (c_parser *parser, tree list)
10119 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_PRIVATE, list);
10122 /* OpenMP 2.5:
10123 reduction ( reduction-operator : variable-list )
10125 reduction-operator:
10126 One of: + * - & ^ | && ||
10128 OpenMP 3.1:
10130 reduction-operator:
10131 One of: + * - & ^ | && || max min
10133 OpenMP 4.0:
10135 reduction-operator:
10136 One of: + * - & ^ | && ||
10137 identifier */
10139 static tree
10140 c_parser_omp_clause_reduction (c_parser *parser, tree list)
10142 location_t clause_loc = c_parser_peek_token (parser)->location;
10143 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10145 enum tree_code code = ERROR_MARK;
10146 tree reduc_id = NULL_TREE;
10148 switch (c_parser_peek_token (parser)->type)
10150 case CPP_PLUS:
10151 code = PLUS_EXPR;
10152 break;
10153 case CPP_MULT:
10154 code = MULT_EXPR;
10155 break;
10156 case CPP_MINUS:
10157 code = MINUS_EXPR;
10158 break;
10159 case CPP_AND:
10160 code = BIT_AND_EXPR;
10161 break;
10162 case CPP_XOR:
10163 code = BIT_XOR_EXPR;
10164 break;
10165 case CPP_OR:
10166 code = BIT_IOR_EXPR;
10167 break;
10168 case CPP_AND_AND:
10169 code = TRUTH_ANDIF_EXPR;
10170 break;
10171 case CPP_OR_OR:
10172 code = TRUTH_ORIF_EXPR;
10173 break;
10174 case CPP_NAME:
10176 const char *p
10177 = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
10178 if (strcmp (p, "min") == 0)
10180 code = MIN_EXPR;
10181 break;
10183 if (strcmp (p, "max") == 0)
10185 code = MAX_EXPR;
10186 break;
10188 reduc_id = c_parser_peek_token (parser)->value;
10189 break;
10191 default:
10192 c_parser_error (parser,
10193 "expected %<+%>, %<*%>, %<-%>, %<&%>, "
10194 "%<^%>, %<|%>, %<&&%>, %<||%>, %<min%> or %<max%>");
10195 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 0);
10196 return list;
10198 c_parser_consume_token (parser);
10199 reduc_id = c_omp_reduction_id (code, reduc_id);
10200 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
10202 tree nl, c;
10204 nl = c_parser_omp_variable_list (parser, clause_loc,
10205 OMP_CLAUSE_REDUCTION, list);
10206 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
10208 tree type = TREE_TYPE (OMP_CLAUSE_DECL (c));
10209 OMP_CLAUSE_REDUCTION_CODE (c) = code;
10210 if (code == ERROR_MARK
10211 || !(INTEGRAL_TYPE_P (type)
10212 || TREE_CODE (type) == REAL_TYPE
10213 || TREE_CODE (type) == COMPLEX_TYPE))
10214 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c)
10215 = c_omp_reduction_lookup (reduc_id,
10216 TYPE_MAIN_VARIANT (type));
10219 list = nl;
10221 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10223 return list;
10226 /* OpenMP 2.5:
10227 schedule ( schedule-kind )
10228 schedule ( schedule-kind , expression )
10230 schedule-kind:
10231 static | dynamic | guided | runtime | auto
10234 static tree
10235 c_parser_omp_clause_schedule (c_parser *parser, tree list)
10237 tree c, t;
10238 location_t loc = c_parser_peek_token (parser)->location;
10240 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10241 return list;
10243 c = build_omp_clause (loc, OMP_CLAUSE_SCHEDULE);
10245 if (c_parser_next_token_is (parser, CPP_NAME))
10247 tree kind = c_parser_peek_token (parser)->value;
10248 const char *p = IDENTIFIER_POINTER (kind);
10250 switch (p[0])
10252 case 'd':
10253 if (strcmp ("dynamic", p) != 0)
10254 goto invalid_kind;
10255 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
10256 break;
10258 case 'g':
10259 if (strcmp ("guided", p) != 0)
10260 goto invalid_kind;
10261 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
10262 break;
10264 case 'r':
10265 if (strcmp ("runtime", p) != 0)
10266 goto invalid_kind;
10267 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
10268 break;
10270 default:
10271 goto invalid_kind;
10274 else if (c_parser_next_token_is_keyword (parser, RID_STATIC))
10275 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
10276 else if (c_parser_next_token_is_keyword (parser, RID_AUTO))
10277 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
10278 else
10279 goto invalid_kind;
10281 c_parser_consume_token (parser);
10282 if (c_parser_next_token_is (parser, CPP_COMMA))
10284 location_t here;
10285 c_parser_consume_token (parser);
10287 here = c_parser_peek_token (parser)->location;
10288 t = c_parser_expr_no_commas (parser, NULL).value;
10289 mark_exp_read (t);
10290 t = c_fully_fold (t, false, NULL);
10292 if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
10293 error_at (here, "schedule %<runtime%> does not take "
10294 "a %<chunk_size%> parameter");
10295 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
10296 error_at (here,
10297 "schedule %<auto%> does not take "
10298 "a %<chunk_size%> parameter");
10299 else if (TREE_CODE (TREE_TYPE (t)) == INTEGER_TYPE)
10300 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
10301 else
10302 c_parser_error (parser, "expected integer expression");
10304 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10306 else
10307 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
10308 "expected %<,%> or %<)%>");
10310 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule");
10311 OMP_CLAUSE_CHAIN (c) = list;
10312 return c;
10314 invalid_kind:
10315 c_parser_error (parser, "invalid schedule kind");
10316 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 0);
10317 return list;
10320 /* OpenMP 2.5:
10321 shared ( variable-list ) */
10323 static tree
10324 c_parser_omp_clause_shared (c_parser *parser, tree list)
10326 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_SHARED, list);
10329 /* OpenMP 3.0:
10330 untied */
10332 static tree
10333 c_parser_omp_clause_untied (c_parser *parser ATTRIBUTE_UNUSED, tree list)
10335 tree c;
10337 /* FIXME: Should we allow duplicates? */
10338 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied");
10340 c = build_omp_clause (c_parser_peek_token (parser)->location,
10341 OMP_CLAUSE_UNTIED);
10342 OMP_CLAUSE_CHAIN (c) = list;
10344 return c;
10347 /* OpenMP 4.0:
10348 inbranch
10349 notinbranch */
10351 static tree
10352 c_parser_omp_clause_branch (c_parser *parser ATTRIBUTE_UNUSED,
10353 enum omp_clause_code code, tree list)
10355 check_no_duplicate_clause (list, code, omp_clause_code_name[code]);
10357 tree c = build_omp_clause (c_parser_peek_token (parser)->location, code);
10358 OMP_CLAUSE_CHAIN (c) = list;
10360 return c;
10363 /* OpenMP 4.0:
10364 parallel
10366 sections
10367 taskgroup */
10369 static tree
10370 c_parser_omp_clause_cancelkind (c_parser *parser ATTRIBUTE_UNUSED,
10371 enum omp_clause_code code, tree list)
10373 tree c = build_omp_clause (c_parser_peek_token (parser)->location, code);
10374 OMP_CLAUSE_CHAIN (c) = list;
10376 return c;
10379 /* OpenMP 4.0:
10380 num_teams ( expression ) */
10382 static tree
10383 c_parser_omp_clause_num_teams (c_parser *parser, tree list)
10385 location_t num_teams_loc = c_parser_peek_token (parser)->location;
10386 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10388 location_t expr_loc = c_parser_peek_token (parser)->location;
10389 tree c, t = c_parser_expression (parser).value;
10390 mark_exp_read (t);
10391 t = c_fully_fold (t, false, NULL);
10393 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10395 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
10397 c_parser_error (parser, "expected integer expression");
10398 return list;
10401 /* Attempt to statically determine when the number isn't positive. */
10402 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
10403 build_int_cst (TREE_TYPE (t), 0));
10404 if (CAN_HAVE_LOCATION_P (c))
10405 SET_EXPR_LOCATION (c, expr_loc);
10406 if (c == boolean_true_node)
10408 warning_at (expr_loc, 0, "%<num_teams%> value must be positive");
10409 t = integer_one_node;
10412 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TEAMS, "num_teams");
10414 c = build_omp_clause (num_teams_loc, OMP_CLAUSE_NUM_TEAMS);
10415 OMP_CLAUSE_NUM_TEAMS_EXPR (c) = t;
10416 OMP_CLAUSE_CHAIN (c) = list;
10417 list = c;
10420 return list;
10423 /* OpenMP 4.0:
10424 thread_limit ( expression ) */
10426 static tree
10427 c_parser_omp_clause_thread_limit (c_parser *parser, tree list)
10429 location_t num_teams_loc = c_parser_peek_token (parser)->location;
10430 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10432 location_t expr_loc = c_parser_peek_token (parser)->location;
10433 tree c, t = c_parser_expression (parser).value;
10434 mark_exp_read (t);
10435 t = c_fully_fold (t, false, NULL);
10437 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10439 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
10441 c_parser_error (parser, "expected integer expression");
10442 return list;
10445 /* Attempt to statically determine when the number isn't positive. */
10446 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
10447 build_int_cst (TREE_TYPE (t), 0));
10448 if (CAN_HAVE_LOCATION_P (c))
10449 SET_EXPR_LOCATION (c, expr_loc);
10450 if (c == boolean_true_node)
10452 warning_at (expr_loc, 0, "%<thread_limit%> value must be positive");
10453 t = integer_one_node;
10456 check_no_duplicate_clause (list, OMP_CLAUSE_THREAD_LIMIT,
10457 "thread_limit");
10459 c = build_omp_clause (num_teams_loc, OMP_CLAUSE_THREAD_LIMIT);
10460 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
10461 OMP_CLAUSE_CHAIN (c) = list;
10462 list = c;
10465 return list;
10468 /* OpenMP 4.0:
10469 aligned ( variable-list )
10470 aligned ( variable-list : constant-expression ) */
10472 static tree
10473 c_parser_omp_clause_aligned (c_parser *parser, tree list)
10475 location_t clause_loc = c_parser_peek_token (parser)->location;
10476 tree nl, c;
10478 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10479 return list;
10481 nl = c_parser_omp_variable_list (parser, clause_loc,
10482 OMP_CLAUSE_ALIGNED, list);
10484 if (c_parser_next_token_is (parser, CPP_COLON))
10486 c_parser_consume_token (parser);
10487 tree alignment = c_parser_expr_no_commas (parser, NULL).value;
10488 mark_exp_read (alignment);
10489 alignment = c_fully_fold (alignment, false, NULL);
10490 if (!INTEGRAL_TYPE_P (TREE_TYPE (alignment))
10491 && TREE_CODE (alignment) != INTEGER_CST
10492 && tree_int_cst_sgn (alignment) != 1)
10494 error_at (clause_loc, "%<aligned%> clause alignment expression must "
10495 "be positive constant integer expression");
10496 alignment = NULL_TREE;
10499 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
10500 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = alignment;
10503 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10504 return nl;
10507 /* OpenMP 4.0:
10508 linear ( variable-list )
10509 linear ( variable-list : expression ) */
10511 static tree
10512 c_parser_omp_clause_linear (c_parser *parser, tree list, bool is_cilk_simd_fn)
10514 location_t clause_loc = c_parser_peek_token (parser)->location;
10515 tree nl, c, step;
10517 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10518 return list;
10520 nl = c_parser_omp_variable_list (parser, clause_loc,
10521 OMP_CLAUSE_LINEAR, list);
10523 if (c_parser_next_token_is (parser, CPP_COLON))
10525 c_parser_consume_token (parser);
10526 step = c_parser_expression (parser).value;
10527 mark_exp_read (step);
10528 step = c_fully_fold (step, false, NULL);
10529 if (is_cilk_simd_fn && TREE_CODE (step) == PARM_DECL)
10531 sorry ("using parameters for %<linear%> step is not supported yet");
10532 step = integer_one_node;
10534 if (!INTEGRAL_TYPE_P (TREE_TYPE (step)))
10536 error_at (clause_loc, "%<linear%> clause step expression must "
10537 "be integral");
10538 step = integer_one_node;
10542 else
10543 step = integer_one_node;
10545 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
10547 OMP_CLAUSE_LINEAR_STEP (c) = step;
10550 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10551 return nl;
10554 /* OpenMP 4.0:
10555 safelen ( constant-expression ) */
10557 static tree
10558 c_parser_omp_clause_safelen (c_parser *parser, tree list)
10560 location_t clause_loc = c_parser_peek_token (parser)->location;
10561 tree c, t;
10563 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10564 return list;
10566 t = c_parser_expr_no_commas (parser, NULL).value;
10567 mark_exp_read (t);
10568 t = c_fully_fold (t, false, NULL);
10569 if (!INTEGRAL_TYPE_P (TREE_TYPE (t))
10570 && TREE_CODE (t) != INTEGER_CST
10571 && tree_int_cst_sgn (t) != 1)
10573 error_at (clause_loc, "%<safelen%> clause expression must "
10574 "be positive constant integer expression");
10575 t = NULL_TREE;
10578 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10579 if (t == NULL_TREE || t == error_mark_node)
10580 return list;
10582 check_no_duplicate_clause (list, OMP_CLAUSE_SAFELEN, "safelen");
10584 c = build_omp_clause (clause_loc, OMP_CLAUSE_SAFELEN);
10585 OMP_CLAUSE_SAFELEN_EXPR (c) = t;
10586 OMP_CLAUSE_CHAIN (c) = list;
10587 return c;
10590 /* OpenMP 4.0:
10591 simdlen ( constant-expression ) */
10593 static tree
10594 c_parser_omp_clause_simdlen (c_parser *parser, tree list)
10596 location_t clause_loc = c_parser_peek_token (parser)->location;
10597 tree c, t;
10599 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10600 return list;
10602 t = c_parser_expr_no_commas (parser, NULL).value;
10603 mark_exp_read (t);
10604 t = c_fully_fold (t, false, NULL);
10605 if (!INTEGRAL_TYPE_P (TREE_TYPE (t))
10606 && TREE_CODE (t) != INTEGER_CST
10607 && tree_int_cst_sgn (t) != 1)
10609 error_at (clause_loc, "%<simdlen%> clause expression must "
10610 "be positive constant integer expression");
10611 t = NULL_TREE;
10614 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10615 if (t == NULL_TREE || t == error_mark_node)
10616 return list;
10618 check_no_duplicate_clause (list, OMP_CLAUSE_SIMDLEN, "simdlen");
10620 c = build_omp_clause (clause_loc, OMP_CLAUSE_SIMDLEN);
10621 OMP_CLAUSE_SIMDLEN_EXPR (c) = t;
10622 OMP_CLAUSE_CHAIN (c) = list;
10623 return c;
10626 /* OpenMP 4.0:
10627 depend ( depend-kind: variable-list )
10629 depend-kind:
10630 in | out | inout */
10632 static tree
10633 c_parser_omp_clause_depend (c_parser *parser, tree list)
10635 location_t clause_loc = c_parser_peek_token (parser)->location;
10636 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_INOUT;
10637 tree nl, c;
10639 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10640 return list;
10642 if (c_parser_next_token_is (parser, CPP_NAME))
10644 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
10645 if (strcmp ("in", p) == 0)
10646 kind = OMP_CLAUSE_DEPEND_IN;
10647 else if (strcmp ("inout", p) == 0)
10648 kind = OMP_CLAUSE_DEPEND_INOUT;
10649 else if (strcmp ("out", p) == 0)
10650 kind = OMP_CLAUSE_DEPEND_OUT;
10651 else
10652 goto invalid_kind;
10654 else
10655 goto invalid_kind;
10657 c_parser_consume_token (parser);
10658 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
10659 goto resync_fail;
10661 nl = c_parser_omp_variable_list (parser, clause_loc,
10662 OMP_CLAUSE_DEPEND, list);
10664 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
10665 OMP_CLAUSE_DEPEND_KIND (c) = kind;
10667 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10668 return nl;
10670 invalid_kind:
10671 c_parser_error (parser, "invalid depend kind");
10672 resync_fail:
10673 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10674 return list;
10677 /* OpenMP 4.0:
10678 map ( map-kind: variable-list )
10679 map ( variable-list )
10681 map-kind:
10682 alloc | to | from | tofrom */
10684 static tree
10685 c_parser_omp_clause_map (c_parser *parser, tree list)
10687 location_t clause_loc = c_parser_peek_token (parser)->location;
10688 enum omp_clause_map_kind kind = OMP_CLAUSE_MAP_TOFROM;
10689 tree nl, c;
10691 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10692 return list;
10694 if (c_parser_next_token_is (parser, CPP_NAME)
10695 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
10697 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
10698 if (strcmp ("alloc", p) == 0)
10699 kind = OMP_CLAUSE_MAP_ALLOC;
10700 else if (strcmp ("to", p) == 0)
10701 kind = OMP_CLAUSE_MAP_TO;
10702 else if (strcmp ("from", p) == 0)
10703 kind = OMP_CLAUSE_MAP_FROM;
10704 else if (strcmp ("tofrom", p) == 0)
10705 kind = OMP_CLAUSE_MAP_TOFROM;
10706 else
10708 c_parser_error (parser, "invalid map kind");
10709 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
10710 "expected %<)%>");
10711 return list;
10713 c_parser_consume_token (parser);
10714 c_parser_consume_token (parser);
10717 nl = c_parser_omp_variable_list (parser, clause_loc, OMP_CLAUSE_MAP, list);
10719 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
10720 OMP_CLAUSE_MAP_KIND (c) = kind;
10722 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10723 return nl;
10726 /* OpenMP 4.0:
10727 device ( expression ) */
10729 static tree
10730 c_parser_omp_clause_device (c_parser *parser, tree list)
10732 location_t clause_loc = c_parser_peek_token (parser)->location;
10733 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10735 tree c, t = c_parser_expr_no_commas (parser, NULL).value;
10736 mark_exp_read (t);
10737 t = c_fully_fold (t, false, NULL);
10739 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10741 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
10743 c_parser_error (parser, "expected integer expression");
10744 return list;
10747 check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE, "device");
10749 c = build_omp_clause (clause_loc, OMP_CLAUSE_DEVICE);
10750 OMP_CLAUSE_DEVICE_ID (c) = t;
10751 OMP_CLAUSE_CHAIN (c) = list;
10752 list = c;
10755 return list;
10758 /* OpenMP 4.0:
10759 dist_schedule ( static )
10760 dist_schedule ( static , expression ) */
10762 static tree
10763 c_parser_omp_clause_dist_schedule (c_parser *parser, tree list)
10765 tree c, t = NULL_TREE;
10766 location_t loc = c_parser_peek_token (parser)->location;
10768 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10769 return list;
10771 if (!c_parser_next_token_is_keyword (parser, RID_STATIC))
10773 c_parser_error (parser, "invalid dist_schedule kind");
10774 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
10775 "expected %<)%>");
10776 return list;
10779 c_parser_consume_token (parser);
10780 if (c_parser_next_token_is (parser, CPP_COMMA))
10782 c_parser_consume_token (parser);
10784 t = c_parser_expr_no_commas (parser, NULL).value;
10785 mark_exp_read (t);
10786 t = c_fully_fold (t, false, NULL);
10787 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10789 else
10790 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
10791 "expected %<,%> or %<)%>");
10793 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule");
10794 if (t == error_mark_node)
10795 return list;
10797 c = build_omp_clause (loc, OMP_CLAUSE_DIST_SCHEDULE);
10798 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
10799 OMP_CLAUSE_CHAIN (c) = list;
10800 return c;
10803 /* OpenMP 4.0:
10804 proc_bind ( proc-bind-kind )
10806 proc-bind-kind:
10807 master | close | spread */
10809 static tree
10810 c_parser_omp_clause_proc_bind (c_parser *parser, tree list)
10812 location_t clause_loc = c_parser_peek_token (parser)->location;
10813 enum omp_clause_proc_bind_kind kind;
10814 tree c;
10816 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10817 return list;
10819 if (c_parser_next_token_is (parser, CPP_NAME))
10821 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
10822 if (strcmp ("master", p) == 0)
10823 kind = OMP_CLAUSE_PROC_BIND_MASTER;
10824 else if (strcmp ("close", p) == 0)
10825 kind = OMP_CLAUSE_PROC_BIND_CLOSE;
10826 else if (strcmp ("spread", p) == 0)
10827 kind = OMP_CLAUSE_PROC_BIND_SPREAD;
10828 else
10829 goto invalid_kind;
10831 else
10832 goto invalid_kind;
10834 c_parser_consume_token (parser);
10835 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10836 c = build_omp_clause (clause_loc, OMP_CLAUSE_PROC_BIND);
10837 OMP_CLAUSE_PROC_BIND_KIND (c) = kind;
10838 OMP_CLAUSE_CHAIN (c) = list;
10839 return c;
10841 invalid_kind:
10842 c_parser_error (parser, "invalid proc_bind kind");
10843 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10844 return list;
10847 /* OpenMP 4.0:
10848 to ( variable-list ) */
10850 static tree
10851 c_parser_omp_clause_to (c_parser *parser, tree list)
10853 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_TO, list);
10856 /* OpenMP 4.0:
10857 from ( variable-list ) */
10859 static tree
10860 c_parser_omp_clause_from (c_parser *parser, tree list)
10862 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_FROM, list);
10865 /* OpenMP 4.0:
10866 uniform ( variable-list ) */
10868 static tree
10869 c_parser_omp_clause_uniform (c_parser *parser, tree list)
10871 /* The clauses location. */
10872 location_t loc = c_parser_peek_token (parser)->location;
10874 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10876 list = c_parser_omp_variable_list (parser, loc, OMP_CLAUSE_UNIFORM,
10877 list);
10878 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10880 return list;
10883 /* Parse all OpenMP clauses. The set clauses allowed by the directive
10884 is a bitmask in MASK. Return the list of clauses found; the result
10885 of clause default goes in *pdefault. */
10887 static tree
10888 c_parser_omp_all_clauses (c_parser *parser, omp_clause_mask mask,
10889 const char *where, bool finish_p = true)
10891 tree clauses = NULL;
10892 bool first = true, cilk_simd_fn = false;
10894 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
10896 location_t here;
10897 pragma_omp_clause c_kind;
10898 const char *c_name;
10899 tree prev = clauses;
10901 if (!first && c_parser_next_token_is (parser, CPP_COMMA))
10902 c_parser_consume_token (parser);
10904 here = c_parser_peek_token (parser)->location;
10905 c_kind = c_parser_omp_clause_name (parser);
10907 switch (c_kind)
10909 case PRAGMA_OMP_CLAUSE_COLLAPSE:
10910 clauses = c_parser_omp_clause_collapse (parser, clauses);
10911 c_name = "collapse";
10912 break;
10913 case PRAGMA_OMP_CLAUSE_COPYIN:
10914 clauses = c_parser_omp_clause_copyin (parser, clauses);
10915 c_name = "copyin";
10916 break;
10917 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
10918 clauses = c_parser_omp_clause_copyprivate (parser, clauses);
10919 c_name = "copyprivate";
10920 break;
10921 case PRAGMA_OMP_CLAUSE_DEFAULT:
10922 clauses = c_parser_omp_clause_default (parser, clauses);
10923 c_name = "default";
10924 break;
10925 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
10926 clauses = c_parser_omp_clause_firstprivate (parser, clauses);
10927 c_name = "firstprivate";
10928 break;
10929 case PRAGMA_OMP_CLAUSE_FINAL:
10930 clauses = c_parser_omp_clause_final (parser, clauses);
10931 c_name = "final";
10932 break;
10933 case PRAGMA_OMP_CLAUSE_IF:
10934 clauses = c_parser_omp_clause_if (parser, clauses);
10935 c_name = "if";
10936 break;
10937 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
10938 clauses = c_parser_omp_clause_lastprivate (parser, clauses);
10939 c_name = "lastprivate";
10940 break;
10941 case PRAGMA_OMP_CLAUSE_MERGEABLE:
10942 clauses = c_parser_omp_clause_mergeable (parser, clauses);
10943 c_name = "mergeable";
10944 break;
10945 case PRAGMA_OMP_CLAUSE_NOWAIT:
10946 clauses = c_parser_omp_clause_nowait (parser, clauses);
10947 c_name = "nowait";
10948 break;
10949 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
10950 clauses = c_parser_omp_clause_num_threads (parser, clauses);
10951 c_name = "num_threads";
10952 break;
10953 case PRAGMA_OMP_CLAUSE_ORDERED:
10954 clauses = c_parser_omp_clause_ordered (parser, clauses);
10955 c_name = "ordered";
10956 break;
10957 case PRAGMA_OMP_CLAUSE_PRIVATE:
10958 clauses = c_parser_omp_clause_private (parser, clauses);
10959 c_name = "private";
10960 break;
10961 case PRAGMA_OMP_CLAUSE_REDUCTION:
10962 clauses = c_parser_omp_clause_reduction (parser, clauses);
10963 c_name = "reduction";
10964 break;
10965 case PRAGMA_OMP_CLAUSE_SCHEDULE:
10966 clauses = c_parser_omp_clause_schedule (parser, clauses);
10967 c_name = "schedule";
10968 break;
10969 case PRAGMA_OMP_CLAUSE_SHARED:
10970 clauses = c_parser_omp_clause_shared (parser, clauses);
10971 c_name = "shared";
10972 break;
10973 case PRAGMA_OMP_CLAUSE_UNTIED:
10974 clauses = c_parser_omp_clause_untied (parser, clauses);
10975 c_name = "untied";
10976 break;
10977 case PRAGMA_OMP_CLAUSE_INBRANCH:
10978 case PRAGMA_CILK_CLAUSE_MASK:
10979 clauses = c_parser_omp_clause_branch (parser, OMP_CLAUSE_INBRANCH,
10980 clauses);
10981 c_name = "inbranch";
10982 break;
10983 case PRAGMA_OMP_CLAUSE_NOTINBRANCH:
10984 case PRAGMA_CILK_CLAUSE_NOMASK:
10985 clauses = c_parser_omp_clause_branch (parser, OMP_CLAUSE_NOTINBRANCH,
10986 clauses);
10987 c_name = "notinbranch";
10988 break;
10989 case PRAGMA_OMP_CLAUSE_PARALLEL:
10990 clauses
10991 = c_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_PARALLEL,
10992 clauses);
10993 c_name = "parallel";
10994 if (!first)
10996 clause_not_first:
10997 error_at (here, "%qs must be the first clause of %qs",
10998 c_name, where);
10999 clauses = prev;
11001 break;
11002 case PRAGMA_OMP_CLAUSE_FOR:
11003 clauses
11004 = c_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_FOR,
11005 clauses);
11006 c_name = "for";
11007 if (!first)
11008 goto clause_not_first;
11009 break;
11010 case PRAGMA_OMP_CLAUSE_SECTIONS:
11011 clauses
11012 = c_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_SECTIONS,
11013 clauses);
11014 c_name = "sections";
11015 if (!first)
11016 goto clause_not_first;
11017 break;
11018 case PRAGMA_OMP_CLAUSE_TASKGROUP:
11019 clauses
11020 = c_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_TASKGROUP,
11021 clauses);
11022 c_name = "taskgroup";
11023 if (!first)
11024 goto clause_not_first;
11025 break;
11026 case PRAGMA_OMP_CLAUSE_TO:
11027 clauses = c_parser_omp_clause_to (parser, clauses);
11028 c_name = "to";
11029 break;
11030 case PRAGMA_OMP_CLAUSE_FROM:
11031 clauses = c_parser_omp_clause_from (parser, clauses);
11032 c_name = "from";
11033 break;
11034 case PRAGMA_OMP_CLAUSE_UNIFORM:
11035 clauses = c_parser_omp_clause_uniform (parser, clauses);
11036 c_name = "uniform";
11037 break;
11038 case PRAGMA_OMP_CLAUSE_NUM_TEAMS:
11039 clauses = c_parser_omp_clause_num_teams (parser, clauses);
11040 c_name = "num_teams";
11041 break;
11042 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT:
11043 clauses = c_parser_omp_clause_thread_limit (parser, clauses);
11044 c_name = "thread_limit";
11045 break;
11046 case PRAGMA_OMP_CLAUSE_ALIGNED:
11047 clauses = c_parser_omp_clause_aligned (parser, clauses);
11048 c_name = "aligned";
11049 break;
11050 case PRAGMA_OMP_CLAUSE_LINEAR:
11051 if (((mask >> PRAGMA_CILK_CLAUSE_VECTORLENGTH) & 1) != 0)
11052 cilk_simd_fn = true;
11053 clauses = c_parser_omp_clause_linear (parser, clauses, cilk_simd_fn);
11054 c_name = "linear";
11055 break;
11056 case PRAGMA_OMP_CLAUSE_DEPEND:
11057 clauses = c_parser_omp_clause_depend (parser, clauses);
11058 c_name = "depend";
11059 break;
11060 case PRAGMA_OMP_CLAUSE_MAP:
11061 clauses = c_parser_omp_clause_map (parser, clauses);
11062 c_name = "map";
11063 break;
11064 case PRAGMA_OMP_CLAUSE_DEVICE:
11065 clauses = c_parser_omp_clause_device (parser, clauses);
11066 c_name = "device";
11067 break;
11068 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE:
11069 clauses = c_parser_omp_clause_dist_schedule (parser, clauses);
11070 c_name = "dist_schedule";
11071 break;
11072 case PRAGMA_OMP_CLAUSE_PROC_BIND:
11073 clauses = c_parser_omp_clause_proc_bind (parser, clauses);
11074 c_name = "proc_bind";
11075 break;
11076 case PRAGMA_OMP_CLAUSE_SAFELEN:
11077 clauses = c_parser_omp_clause_safelen (parser, clauses);
11078 c_name = "safelen";
11079 break;
11080 case PRAGMA_CILK_CLAUSE_VECTORLENGTH:
11081 clauses = c_parser_cilk_clause_vectorlength (parser, clauses, true);
11082 c_name = "simdlen";
11083 break;
11084 case PRAGMA_OMP_CLAUSE_SIMDLEN:
11085 clauses = c_parser_omp_clause_simdlen (parser, clauses);
11086 c_name = "simdlen";
11087 break;
11088 default:
11089 c_parser_error (parser, "expected %<#pragma omp%> clause");
11090 goto saw_error;
11093 first = false;
11095 if (((mask >> c_kind) & 1) == 0 && !parser->error)
11097 /* Remove the invalid clause(s) from the list to avoid
11098 confusing the rest of the compiler. */
11099 clauses = prev;
11100 error_at (here, "%qs is not valid for %qs", c_name, where);
11104 saw_error:
11105 c_parser_skip_to_pragma_eol (parser);
11107 if (finish_p)
11108 return c_finish_omp_clauses (clauses);
11110 return clauses;
11113 /* OpenMP 2.5:
11114 structured-block:
11115 statement
11117 In practice, we're also interested in adding the statement to an
11118 outer node. So it is convenient if we work around the fact that
11119 c_parser_statement calls add_stmt. */
11121 static tree
11122 c_parser_omp_structured_block (c_parser *parser)
11124 tree stmt = push_stmt_list ();
11125 c_parser_statement (parser);
11126 return pop_stmt_list (stmt);
11129 /* OpenMP 2.5:
11130 # pragma omp atomic new-line
11131 expression-stmt
11133 expression-stmt:
11134 x binop= expr | x++ | ++x | x-- | --x
11135 binop:
11136 +, *, -, /, &, ^, |, <<, >>
11138 where x is an lvalue expression with scalar type.
11140 OpenMP 3.1:
11141 # pragma omp atomic new-line
11142 update-stmt
11144 # pragma omp atomic read new-line
11145 read-stmt
11147 # pragma omp atomic write new-line
11148 write-stmt
11150 # pragma omp atomic update new-line
11151 update-stmt
11153 # pragma omp atomic capture new-line
11154 capture-stmt
11156 # pragma omp atomic capture new-line
11157 capture-block
11159 read-stmt:
11160 v = x
11161 write-stmt:
11162 x = expr
11163 update-stmt:
11164 expression-stmt | x = x binop expr
11165 capture-stmt:
11166 v = expression-stmt
11167 capture-block:
11168 { v = x; update-stmt; } | { update-stmt; v = x; }
11170 OpenMP 4.0:
11171 update-stmt:
11172 expression-stmt | x = x binop expr | x = expr binop x
11173 capture-stmt:
11174 v = update-stmt
11175 capture-block:
11176 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
11178 where x and v are lvalue expressions with scalar type.
11180 LOC is the location of the #pragma token. */
11182 static void
11183 c_parser_omp_atomic (location_t loc, c_parser *parser)
11185 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE;
11186 tree lhs1 = NULL_TREE, rhs1 = NULL_TREE;
11187 tree stmt, orig_lhs, unfolded_lhs = NULL_TREE, unfolded_lhs1 = NULL_TREE;
11188 enum tree_code code = OMP_ATOMIC, opcode = NOP_EXPR;
11189 struct c_expr expr;
11190 location_t eloc;
11191 bool structured_block = false;
11192 bool swapped = false;
11193 bool seq_cst = false;
11195 if (c_parser_next_token_is (parser, CPP_NAME))
11197 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
11198 if (!strcmp (p, "seq_cst"))
11200 seq_cst = true;
11201 c_parser_consume_token (parser);
11202 if (c_parser_next_token_is (parser, CPP_COMMA)
11203 && c_parser_peek_2nd_token (parser)->type == CPP_NAME)
11204 c_parser_consume_token (parser);
11207 if (c_parser_next_token_is (parser, CPP_NAME))
11209 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
11211 if (!strcmp (p, "read"))
11212 code = OMP_ATOMIC_READ;
11213 else if (!strcmp (p, "write"))
11214 code = NOP_EXPR;
11215 else if (!strcmp (p, "update"))
11216 code = OMP_ATOMIC;
11217 else if (!strcmp (p, "capture"))
11218 code = OMP_ATOMIC_CAPTURE_NEW;
11219 else
11220 p = NULL;
11221 if (p)
11222 c_parser_consume_token (parser);
11224 if (!seq_cst)
11226 if (c_parser_next_token_is (parser, CPP_COMMA)
11227 && c_parser_peek_2nd_token (parser)->type == CPP_NAME)
11228 c_parser_consume_token (parser);
11230 if (c_parser_next_token_is (parser, CPP_NAME))
11232 const char *p
11233 = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
11234 if (!strcmp (p, "seq_cst"))
11236 seq_cst = true;
11237 c_parser_consume_token (parser);
11241 c_parser_skip_to_pragma_eol (parser);
11243 switch (code)
11245 case OMP_ATOMIC_READ:
11246 case NOP_EXPR: /* atomic write */
11247 v = c_parser_unary_expression (parser).value;
11248 v = c_fully_fold (v, false, NULL);
11249 if (v == error_mark_node)
11250 goto saw_error;
11251 loc = c_parser_peek_token (parser)->location;
11252 if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
11253 goto saw_error;
11254 if (code == NOP_EXPR)
11255 lhs = c_parser_expression (parser).value;
11256 else
11257 lhs = c_parser_unary_expression (parser).value;
11258 lhs = c_fully_fold (lhs, false, NULL);
11259 if (lhs == error_mark_node)
11260 goto saw_error;
11261 if (code == NOP_EXPR)
11263 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
11264 opcode. */
11265 code = OMP_ATOMIC;
11266 rhs = lhs;
11267 lhs = v;
11268 v = NULL_TREE;
11270 goto done;
11271 case OMP_ATOMIC_CAPTURE_NEW:
11272 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
11274 c_parser_consume_token (parser);
11275 structured_block = true;
11277 else
11279 v = c_parser_unary_expression (parser).value;
11280 v = c_fully_fold (v, false, NULL);
11281 if (v == error_mark_node)
11282 goto saw_error;
11283 if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
11284 goto saw_error;
11286 break;
11287 default:
11288 break;
11291 /* For structured_block case we don't know yet whether
11292 old or new x should be captured. */
11293 restart:
11294 eloc = c_parser_peek_token (parser)->location;
11295 expr = c_parser_unary_expression (parser);
11296 lhs = expr.value;
11297 expr = default_function_array_conversion (eloc, expr);
11298 unfolded_lhs = expr.value;
11299 lhs = c_fully_fold (lhs, false, NULL);
11300 orig_lhs = lhs;
11301 switch (TREE_CODE (lhs))
11303 case ERROR_MARK:
11304 saw_error:
11305 c_parser_skip_to_end_of_block_or_statement (parser);
11306 if (structured_block)
11308 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
11309 c_parser_consume_token (parser);
11310 else if (code == OMP_ATOMIC_CAPTURE_NEW)
11312 c_parser_skip_to_end_of_block_or_statement (parser);
11313 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
11314 c_parser_consume_token (parser);
11317 return;
11319 case POSTINCREMENT_EXPR:
11320 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
11321 code = OMP_ATOMIC_CAPTURE_OLD;
11322 /* FALLTHROUGH */
11323 case PREINCREMENT_EXPR:
11324 lhs = TREE_OPERAND (lhs, 0);
11325 unfolded_lhs = NULL_TREE;
11326 opcode = PLUS_EXPR;
11327 rhs = integer_one_node;
11328 break;
11330 case POSTDECREMENT_EXPR:
11331 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
11332 code = OMP_ATOMIC_CAPTURE_OLD;
11333 /* FALLTHROUGH */
11334 case PREDECREMENT_EXPR:
11335 lhs = TREE_OPERAND (lhs, 0);
11336 unfolded_lhs = NULL_TREE;
11337 opcode = MINUS_EXPR;
11338 rhs = integer_one_node;
11339 break;
11341 case COMPOUND_EXPR:
11342 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
11343 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
11344 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
11345 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
11346 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
11347 (TREE_OPERAND (lhs, 1), 0), 0)))
11348 == BOOLEAN_TYPE)
11349 /* Undo effects of boolean_increment for post {in,de}crement. */
11350 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
11351 /* FALLTHRU */
11352 case MODIFY_EXPR:
11353 if (TREE_CODE (lhs) == MODIFY_EXPR
11354 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
11356 /* Undo effects of boolean_increment. */
11357 if (integer_onep (TREE_OPERAND (lhs, 1)))
11359 /* This is pre or post increment. */
11360 rhs = TREE_OPERAND (lhs, 1);
11361 lhs = TREE_OPERAND (lhs, 0);
11362 unfolded_lhs = NULL_TREE;
11363 opcode = NOP_EXPR;
11364 if (code == OMP_ATOMIC_CAPTURE_NEW
11365 && !structured_block
11366 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
11367 code = OMP_ATOMIC_CAPTURE_OLD;
11368 break;
11370 if (TREE_CODE (TREE_OPERAND (lhs, 1)) == TRUTH_NOT_EXPR
11371 && TREE_OPERAND (lhs, 0)
11372 == TREE_OPERAND (TREE_OPERAND (lhs, 1), 0))
11374 /* This is pre or post decrement. */
11375 rhs = TREE_OPERAND (lhs, 1);
11376 lhs = TREE_OPERAND (lhs, 0);
11377 unfolded_lhs = NULL_TREE;
11378 opcode = NOP_EXPR;
11379 if (code == OMP_ATOMIC_CAPTURE_NEW
11380 && !structured_block
11381 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
11382 code = OMP_ATOMIC_CAPTURE_OLD;
11383 break;
11386 /* FALLTHRU */
11387 default:
11388 switch (c_parser_peek_token (parser)->type)
11390 case CPP_MULT_EQ:
11391 opcode = MULT_EXPR;
11392 break;
11393 case CPP_DIV_EQ:
11394 opcode = TRUNC_DIV_EXPR;
11395 break;
11396 case CPP_PLUS_EQ:
11397 opcode = PLUS_EXPR;
11398 break;
11399 case CPP_MINUS_EQ:
11400 opcode = MINUS_EXPR;
11401 break;
11402 case CPP_LSHIFT_EQ:
11403 opcode = LSHIFT_EXPR;
11404 break;
11405 case CPP_RSHIFT_EQ:
11406 opcode = RSHIFT_EXPR;
11407 break;
11408 case CPP_AND_EQ:
11409 opcode = BIT_AND_EXPR;
11410 break;
11411 case CPP_OR_EQ:
11412 opcode = BIT_IOR_EXPR;
11413 break;
11414 case CPP_XOR_EQ:
11415 opcode = BIT_XOR_EXPR;
11416 break;
11417 case CPP_EQ:
11418 c_parser_consume_token (parser);
11419 eloc = c_parser_peek_token (parser)->location;
11420 expr = c_parser_expr_no_commas (parser, NULL, unfolded_lhs);
11421 rhs1 = expr.value;
11422 switch (TREE_CODE (rhs1))
11424 case MULT_EXPR:
11425 case TRUNC_DIV_EXPR:
11426 case PLUS_EXPR:
11427 case MINUS_EXPR:
11428 case LSHIFT_EXPR:
11429 case RSHIFT_EXPR:
11430 case BIT_AND_EXPR:
11431 case BIT_IOR_EXPR:
11432 case BIT_XOR_EXPR:
11433 if (c_tree_equal (TREE_OPERAND (rhs1, 0), unfolded_lhs))
11435 opcode = TREE_CODE (rhs1);
11436 rhs = c_fully_fold (TREE_OPERAND (rhs1, 1), false, NULL);
11437 rhs1 = c_fully_fold (TREE_OPERAND (rhs1, 0), false, NULL);
11438 goto stmt_done;
11440 if (c_tree_equal (TREE_OPERAND (rhs1, 1), unfolded_lhs))
11442 opcode = TREE_CODE (rhs1);
11443 rhs = c_fully_fold (TREE_OPERAND (rhs1, 0), false, NULL);
11444 rhs1 = c_fully_fold (TREE_OPERAND (rhs1, 1), false, NULL);
11445 swapped = !commutative_tree_code (opcode);
11446 goto stmt_done;
11448 break;
11449 case ERROR_MARK:
11450 goto saw_error;
11451 default:
11452 break;
11454 if (c_parser_peek_token (parser)->type == CPP_SEMICOLON)
11456 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
11458 code = OMP_ATOMIC_CAPTURE_OLD;
11459 v = lhs;
11460 lhs = NULL_TREE;
11461 expr = default_function_array_read_conversion (eloc, expr);
11462 unfolded_lhs1 = expr.value;
11463 lhs1 = c_fully_fold (unfolded_lhs1, false, NULL);
11464 rhs1 = NULL_TREE;
11465 c_parser_consume_token (parser);
11466 goto restart;
11468 if (structured_block)
11470 opcode = NOP_EXPR;
11471 expr = default_function_array_read_conversion (eloc, expr);
11472 rhs = c_fully_fold (expr.value, false, NULL);
11473 rhs1 = NULL_TREE;
11474 goto stmt_done;
11477 c_parser_error (parser, "invalid form of %<#pragma omp atomic%>");
11478 goto saw_error;
11479 default:
11480 c_parser_error (parser,
11481 "invalid operator for %<#pragma omp atomic%>");
11482 goto saw_error;
11485 /* Arrange to pass the location of the assignment operator to
11486 c_finish_omp_atomic. */
11487 loc = c_parser_peek_token (parser)->location;
11488 c_parser_consume_token (parser);
11489 eloc = c_parser_peek_token (parser)->location;
11490 expr = c_parser_expression (parser);
11491 expr = default_function_array_read_conversion (eloc, expr);
11492 rhs = expr.value;
11493 rhs = c_fully_fold (rhs, false, NULL);
11494 break;
11496 stmt_done:
11497 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
11499 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
11500 goto saw_error;
11501 v = c_parser_unary_expression (parser).value;
11502 v = c_fully_fold (v, false, NULL);
11503 if (v == error_mark_node)
11504 goto saw_error;
11505 if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
11506 goto saw_error;
11507 eloc = c_parser_peek_token (parser)->location;
11508 expr = c_parser_unary_expression (parser);
11509 lhs1 = expr.value;
11510 expr = default_function_array_read_conversion (eloc, expr);
11511 unfolded_lhs1 = expr.value;
11512 lhs1 = c_fully_fold (lhs1, false, NULL);
11513 if (lhs1 == error_mark_node)
11514 goto saw_error;
11516 if (structured_block)
11518 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
11519 c_parser_require (parser, CPP_CLOSE_BRACE, "expected %<}%>");
11521 done:
11522 if (unfolded_lhs && unfolded_lhs1
11523 && !c_tree_equal (unfolded_lhs, unfolded_lhs1))
11525 error ("%<#pragma omp atomic capture%> uses two different "
11526 "expressions for memory");
11527 stmt = error_mark_node;
11529 else
11530 stmt = c_finish_omp_atomic (loc, code, opcode, lhs, rhs, v, lhs1, rhs1,
11531 swapped, seq_cst);
11532 if (stmt != error_mark_node)
11533 add_stmt (stmt);
11535 if (!structured_block)
11536 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
11540 /* OpenMP 2.5:
11541 # pragma omp barrier new-line
11544 static void
11545 c_parser_omp_barrier (c_parser *parser)
11547 location_t loc = c_parser_peek_token (parser)->location;
11548 c_parser_consume_pragma (parser);
11549 c_parser_skip_to_pragma_eol (parser);
11551 c_finish_omp_barrier (loc);
11554 /* OpenMP 2.5:
11555 # pragma omp critical [(name)] new-line
11556 structured-block
11558 LOC is the location of the #pragma itself. */
11560 static tree
11561 c_parser_omp_critical (location_t loc, c_parser *parser)
11563 tree stmt, name = NULL;
11565 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
11567 c_parser_consume_token (parser);
11568 if (c_parser_next_token_is (parser, CPP_NAME))
11570 name = c_parser_peek_token (parser)->value;
11571 c_parser_consume_token (parser);
11572 c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11574 else
11575 c_parser_error (parser, "expected identifier");
11577 else if (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
11578 c_parser_error (parser, "expected %<(%> or end of line");
11579 c_parser_skip_to_pragma_eol (parser);
11581 stmt = c_parser_omp_structured_block (parser);
11582 return c_finish_omp_critical (loc, stmt, name);
11585 /* OpenMP 2.5:
11586 # pragma omp flush flush-vars[opt] new-line
11588 flush-vars:
11589 ( variable-list ) */
11591 static void
11592 c_parser_omp_flush (c_parser *parser)
11594 location_t loc = c_parser_peek_token (parser)->location;
11595 c_parser_consume_pragma (parser);
11596 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
11597 c_parser_omp_var_list_parens (parser, OMP_CLAUSE_ERROR, NULL);
11598 else if (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
11599 c_parser_error (parser, "expected %<(%> or end of line");
11600 c_parser_skip_to_pragma_eol (parser);
11602 c_finish_omp_flush (loc);
11605 /* Parse the restricted form of the for statement allowed by OpenMP.
11606 The real trick here is to determine the loop control variable early
11607 so that we can push a new decl if necessary to make it private.
11608 LOC is the location of the OMP in "#pragma omp". */
11610 static tree
11611 c_parser_omp_for_loop (location_t loc, c_parser *parser, enum tree_code code,
11612 tree clauses, tree *cclauses)
11614 tree decl, cond, incr, save_break, save_cont, body, init, stmt, cl;
11615 tree declv, condv, incrv, initv, ret = NULL;
11616 bool fail = false, open_brace_parsed = false;
11617 int i, collapse = 1, nbraces = 0;
11618 location_t for_loc;
11619 vec<tree, va_gc> *for_block = make_tree_vector ();
11621 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
11622 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
11623 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl));
11625 gcc_assert (collapse >= 1);
11627 declv = make_tree_vec (collapse);
11628 initv = make_tree_vec (collapse);
11629 condv = make_tree_vec (collapse);
11630 incrv = make_tree_vec (collapse);
11632 if (!c_parser_next_token_is_keyword (parser, RID_FOR))
11634 c_parser_error (parser, "for statement expected");
11635 return NULL;
11637 for_loc = c_parser_peek_token (parser)->location;
11638 c_parser_consume_token (parser);
11640 for (i = 0; i < collapse; i++)
11642 int bracecount = 0;
11644 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11645 goto pop_scopes;
11647 /* Parse the initialization declaration or expression. */
11648 if (c_parser_next_tokens_start_declaration (parser))
11650 if (i > 0)
11651 vec_safe_push (for_block, c_begin_compound_stmt (true));
11652 c_parser_declaration_or_fndef (parser, true, true, true, true, true,
11653 NULL, vNULL);
11654 decl = check_for_loop_decls (for_loc, flag_isoc99);
11655 if (decl == NULL)
11656 goto error_init;
11657 if (DECL_INITIAL (decl) == error_mark_node)
11658 decl = error_mark_node;
11659 init = decl;
11661 else if (c_parser_next_token_is (parser, CPP_NAME)
11662 && c_parser_peek_2nd_token (parser)->type == CPP_EQ)
11664 struct c_expr decl_exp;
11665 struct c_expr init_exp;
11666 location_t init_loc;
11668 decl_exp = c_parser_postfix_expression (parser);
11669 decl = decl_exp.value;
11671 c_parser_require (parser, CPP_EQ, "expected %<=%>");
11673 init_loc = c_parser_peek_token (parser)->location;
11674 init_exp = c_parser_expr_no_commas (parser, NULL);
11675 init_exp = default_function_array_read_conversion (init_loc,
11676 init_exp);
11677 init = build_modify_expr (init_loc, decl, decl_exp.original_type,
11678 NOP_EXPR, init_loc, init_exp.value,
11679 init_exp.original_type);
11680 init = c_process_expr_stmt (init_loc, init);
11682 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
11684 else
11686 error_init:
11687 c_parser_error (parser,
11688 "expected iteration declaration or initialization");
11689 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
11690 "expected %<)%>");
11691 fail = true;
11692 goto parse_next;
11695 /* Parse the loop condition. */
11696 cond = NULL_TREE;
11697 if (c_parser_next_token_is_not (parser, CPP_SEMICOLON))
11699 location_t cond_loc = c_parser_peek_token (parser)->location;
11700 struct c_expr cond_expr
11701 = c_parser_binary_expression (parser, NULL, NULL_TREE);
11703 cond = cond_expr.value;
11704 cond = c_objc_common_truthvalue_conversion (cond_loc, cond);
11705 cond = c_fully_fold (cond, false, NULL);
11706 switch (cond_expr.original_code)
11708 case GT_EXPR:
11709 case GE_EXPR:
11710 case LT_EXPR:
11711 case LE_EXPR:
11712 break;
11713 case NE_EXPR:
11714 if (code == CILK_SIMD)
11715 break;
11716 /* FALLTHRU. */
11717 default:
11718 /* Can't be cond = error_mark_node, because we want to preserve
11719 the location until c_finish_omp_for. */
11720 cond = build1 (NOP_EXPR, boolean_type_node, error_mark_node);
11721 break;
11723 protected_set_expr_location (cond, cond_loc);
11725 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
11727 /* Parse the increment expression. */
11728 incr = NULL_TREE;
11729 if (c_parser_next_token_is_not (parser, CPP_CLOSE_PAREN))
11731 location_t incr_loc = c_parser_peek_token (parser)->location;
11733 incr = c_process_expr_stmt (incr_loc,
11734 c_parser_expression (parser).value);
11736 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11738 if (decl == NULL || decl == error_mark_node || init == error_mark_node)
11739 fail = true;
11740 else
11742 TREE_VEC_ELT (declv, i) = decl;
11743 TREE_VEC_ELT (initv, i) = init;
11744 TREE_VEC_ELT (condv, i) = cond;
11745 TREE_VEC_ELT (incrv, i) = incr;
11748 parse_next:
11749 if (i == collapse - 1)
11750 break;
11752 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
11753 in between the collapsed for loops to be still considered perfectly
11754 nested. Hopefully the final version clarifies this.
11755 For now handle (multiple) {'s and empty statements. */
11758 if (c_parser_next_token_is_keyword (parser, RID_FOR))
11760 c_parser_consume_token (parser);
11761 break;
11763 else if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
11765 c_parser_consume_token (parser);
11766 bracecount++;
11768 else if (bracecount
11769 && c_parser_next_token_is (parser, CPP_SEMICOLON))
11770 c_parser_consume_token (parser);
11771 else
11773 c_parser_error (parser, "not enough perfectly nested loops");
11774 if (bracecount)
11776 open_brace_parsed = true;
11777 bracecount--;
11779 fail = true;
11780 collapse = 0;
11781 break;
11784 while (1);
11786 nbraces += bracecount;
11789 save_break = c_break_label;
11790 if (code == CILK_SIMD)
11791 c_break_label = build_int_cst (size_type_node, 2);
11792 else
11793 c_break_label = size_one_node;
11794 save_cont = c_cont_label;
11795 c_cont_label = NULL_TREE;
11796 body = push_stmt_list ();
11798 if (open_brace_parsed)
11800 location_t here = c_parser_peek_token (parser)->location;
11801 stmt = c_begin_compound_stmt (true);
11802 c_parser_compound_statement_nostart (parser);
11803 add_stmt (c_end_compound_stmt (here, stmt, true));
11805 else
11806 add_stmt (c_parser_c99_block_statement (parser));
11807 if (c_cont_label)
11809 tree t = build1 (LABEL_EXPR, void_type_node, c_cont_label);
11810 SET_EXPR_LOCATION (t, loc);
11811 add_stmt (t);
11814 body = pop_stmt_list (body);
11815 c_break_label = save_break;
11816 c_cont_label = save_cont;
11818 while (nbraces)
11820 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
11822 c_parser_consume_token (parser);
11823 nbraces--;
11825 else if (c_parser_next_token_is (parser, CPP_SEMICOLON))
11826 c_parser_consume_token (parser);
11827 else
11829 c_parser_error (parser, "collapsed loops not perfectly nested");
11830 while (nbraces)
11832 location_t here = c_parser_peek_token (parser)->location;
11833 stmt = c_begin_compound_stmt (true);
11834 add_stmt (body);
11835 c_parser_compound_statement_nostart (parser);
11836 body = c_end_compound_stmt (here, stmt, true);
11837 nbraces--;
11839 goto pop_scopes;
11843 /* Only bother calling c_finish_omp_for if we haven't already generated
11844 an error from the initialization parsing. */
11845 if (!fail)
11847 stmt = c_finish_omp_for (loc, code, declv, initv, condv,
11848 incrv, body, NULL);
11849 if (stmt)
11851 if (cclauses != NULL
11852 && cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL] != NULL)
11854 tree *c;
11855 for (c = &cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL]; *c ; )
11856 if (OMP_CLAUSE_CODE (*c) != OMP_CLAUSE_FIRSTPRIVATE
11857 && OMP_CLAUSE_CODE (*c) != OMP_CLAUSE_LASTPRIVATE)
11858 c = &OMP_CLAUSE_CHAIN (*c);
11859 else
11861 for (i = 0; i < collapse; i++)
11862 if (TREE_VEC_ELT (declv, i) == OMP_CLAUSE_DECL (*c))
11863 break;
11864 if (i == collapse)
11865 c = &OMP_CLAUSE_CHAIN (*c);
11866 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE)
11868 error_at (loc,
11869 "iteration variable %qD should not be firstprivate",
11870 OMP_CLAUSE_DECL (*c));
11871 *c = OMP_CLAUSE_CHAIN (*c);
11873 else
11875 /* Copy lastprivate (decl) clause to OMP_FOR_CLAUSES,
11876 change it to shared (decl) in
11877 OMP_PARALLEL_CLAUSES. */
11878 tree l = build_omp_clause (OMP_CLAUSE_LOCATION (*c),
11879 OMP_CLAUSE_LASTPRIVATE);
11880 OMP_CLAUSE_DECL (l) = OMP_CLAUSE_DECL (*c);
11881 if (code == OMP_SIMD)
11883 OMP_CLAUSE_CHAIN (l)
11884 = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
11885 cclauses[C_OMP_CLAUSE_SPLIT_FOR] = l;
11887 else
11889 OMP_CLAUSE_CHAIN (l) = clauses;
11890 clauses = l;
11892 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
11896 OMP_FOR_CLAUSES (stmt) = clauses;
11898 ret = stmt;
11900 pop_scopes:
11901 while (!for_block->is_empty ())
11903 /* FIXME diagnostics: LOC below should be the actual location of
11904 this particular for block. We need to build a list of
11905 locations to go along with FOR_BLOCK. */
11906 stmt = c_end_compound_stmt (loc, for_block->pop (), true);
11907 add_stmt (stmt);
11909 release_tree_vector (for_block);
11910 return ret;
11913 /* Helper function for OpenMP parsing, split clauses and call
11914 finish_omp_clauses on each of the set of clauses afterwards. */
11916 static void
11917 omp_split_clauses (location_t loc, enum tree_code code,
11918 omp_clause_mask mask, tree clauses, tree *cclauses)
11920 int i;
11921 c_omp_split_clauses (loc, code, mask, clauses, cclauses);
11922 for (i = 0; i < C_OMP_CLAUSE_SPLIT_COUNT; i++)
11923 if (cclauses[i])
11924 cclauses[i] = c_finish_omp_clauses (cclauses[i]);
11927 /* OpenMP 4.0:
11928 #pragma omp simd simd-clause[optseq] new-line
11929 for-loop
11931 LOC is the location of the #pragma token.
11934 #define OMP_SIMD_CLAUSE_MASK \
11935 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
11936 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
11937 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
11938 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
11939 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
11940 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
11941 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
11943 static tree
11944 c_parser_omp_simd (location_t loc, c_parser *parser,
11945 char *p_name, omp_clause_mask mask, tree *cclauses)
11947 tree block, clauses, ret;
11949 strcat (p_name, " simd");
11950 mask |= OMP_SIMD_CLAUSE_MASK;
11951 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED);
11953 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
11954 if (cclauses)
11956 omp_split_clauses (loc, OMP_SIMD, mask, clauses, cclauses);
11957 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SIMD];
11960 block = c_begin_compound_stmt (true);
11961 ret = c_parser_omp_for_loop (loc, parser, OMP_SIMD, clauses, cclauses);
11962 block = c_end_compound_stmt (loc, block, true);
11963 add_stmt (block);
11965 return ret;
11968 /* OpenMP 2.5:
11969 #pragma omp for for-clause[optseq] new-line
11970 for-loop
11972 OpenMP 4.0:
11973 #pragma omp for simd for-simd-clause[optseq] new-line
11974 for-loop
11976 LOC is the location of the #pragma token.
11979 #define OMP_FOR_CLAUSE_MASK \
11980 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
11981 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
11982 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
11983 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
11984 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
11985 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
11986 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
11987 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
11989 static tree
11990 c_parser_omp_for (location_t loc, c_parser *parser,
11991 char *p_name, omp_clause_mask mask, tree *cclauses)
11993 tree block, clauses, ret;
11995 strcat (p_name, " for");
11996 mask |= OMP_FOR_CLAUSE_MASK;
11997 if (cclauses)
11998 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
12000 if (c_parser_next_token_is (parser, CPP_NAME))
12002 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
12004 if (strcmp (p, "simd") == 0)
12006 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
12007 if (cclauses == NULL)
12008 cclauses = cclauses_buf;
12010 c_parser_consume_token (parser);
12011 if (!flag_openmp) /* flag_openmp_simd */
12012 return c_parser_omp_simd (loc, parser, p_name, mask, cclauses);
12013 block = c_begin_compound_stmt (true);
12014 ret = c_parser_omp_simd (loc, parser, p_name, mask, cclauses);
12015 block = c_end_compound_stmt (loc, block, true);
12016 if (ret == NULL_TREE)
12017 return ret;
12018 ret = make_node (OMP_FOR);
12019 TREE_TYPE (ret) = void_type_node;
12020 OMP_FOR_BODY (ret) = block;
12021 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
12022 SET_EXPR_LOCATION (ret, loc);
12023 add_stmt (ret);
12024 return ret;
12027 if (!flag_openmp) /* flag_openmp_simd */
12029 c_parser_skip_to_pragma_eol (parser);
12030 return NULL_TREE;
12033 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
12034 if (cclauses)
12036 omp_split_clauses (loc, OMP_FOR, mask, clauses, cclauses);
12037 clauses = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
12040 block = c_begin_compound_stmt (true);
12041 ret = c_parser_omp_for_loop (loc, parser, OMP_FOR, clauses, cclauses);
12042 block = c_end_compound_stmt (loc, block, true);
12043 add_stmt (block);
12045 return ret;
12048 /* OpenMP 2.5:
12049 # pragma omp master new-line
12050 structured-block
12052 LOC is the location of the #pragma token.
12055 static tree
12056 c_parser_omp_master (location_t loc, c_parser *parser)
12058 c_parser_skip_to_pragma_eol (parser);
12059 return c_finish_omp_master (loc, c_parser_omp_structured_block (parser));
12062 /* OpenMP 2.5:
12063 # pragma omp ordered new-line
12064 structured-block
12066 LOC is the location of the #pragma itself.
12069 static tree
12070 c_parser_omp_ordered (location_t loc, c_parser *parser)
12072 c_parser_skip_to_pragma_eol (parser);
12073 return c_finish_omp_ordered (loc, c_parser_omp_structured_block (parser));
12076 /* OpenMP 2.5:
12078 section-scope:
12079 { section-sequence }
12081 section-sequence:
12082 section-directive[opt] structured-block
12083 section-sequence section-directive structured-block
12085 SECTIONS_LOC is the location of the #pragma omp sections. */
12087 static tree
12088 c_parser_omp_sections_scope (location_t sections_loc, c_parser *parser)
12090 tree stmt, substmt;
12091 bool error_suppress = false;
12092 location_t loc;
12094 loc = c_parser_peek_token (parser)->location;
12095 if (!c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
12097 /* Avoid skipping until the end of the block. */
12098 parser->error = false;
12099 return NULL_TREE;
12102 stmt = push_stmt_list ();
12104 if (c_parser_peek_token (parser)->pragma_kind != PRAGMA_OMP_SECTION)
12106 substmt = c_parser_omp_structured_block (parser);
12107 substmt = build1 (OMP_SECTION, void_type_node, substmt);
12108 SET_EXPR_LOCATION (substmt, loc);
12109 add_stmt (substmt);
12112 while (1)
12114 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
12115 break;
12116 if (c_parser_next_token_is (parser, CPP_EOF))
12117 break;
12119 loc = c_parser_peek_token (parser)->location;
12120 if (c_parser_peek_token (parser)->pragma_kind == PRAGMA_OMP_SECTION)
12122 c_parser_consume_pragma (parser);
12123 c_parser_skip_to_pragma_eol (parser);
12124 error_suppress = false;
12126 else if (!error_suppress)
12128 error_at (loc, "expected %<#pragma omp section%> or %<}%>");
12129 error_suppress = true;
12132 substmt = c_parser_omp_structured_block (parser);
12133 substmt = build1 (OMP_SECTION, void_type_node, substmt);
12134 SET_EXPR_LOCATION (substmt, loc);
12135 add_stmt (substmt);
12137 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE,
12138 "expected %<#pragma omp section%> or %<}%>");
12140 substmt = pop_stmt_list (stmt);
12142 stmt = make_node (OMP_SECTIONS);
12143 SET_EXPR_LOCATION (stmt, sections_loc);
12144 TREE_TYPE (stmt) = void_type_node;
12145 OMP_SECTIONS_BODY (stmt) = substmt;
12147 return add_stmt (stmt);
12150 /* OpenMP 2.5:
12151 # pragma omp sections sections-clause[optseq] newline
12152 sections-scope
12154 LOC is the location of the #pragma token.
12157 #define OMP_SECTIONS_CLAUSE_MASK \
12158 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
12159 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
12160 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
12161 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
12162 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
12164 static tree
12165 c_parser_omp_sections (location_t loc, c_parser *parser,
12166 char *p_name, omp_clause_mask mask, tree *cclauses)
12168 tree block, clauses, ret;
12170 strcat (p_name, " sections");
12171 mask |= OMP_SECTIONS_CLAUSE_MASK;
12172 if (cclauses)
12173 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
12175 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
12176 if (cclauses)
12178 omp_split_clauses (loc, OMP_SECTIONS, mask, clauses, cclauses);
12179 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SECTIONS];
12182 block = c_begin_compound_stmt (true);
12183 ret = c_parser_omp_sections_scope (loc, parser);
12184 if (ret)
12185 OMP_SECTIONS_CLAUSES (ret) = clauses;
12186 block = c_end_compound_stmt (loc, block, true);
12187 add_stmt (block);
12189 return ret;
12192 /* OpenMP 2.5:
12193 # pragma omp parallel parallel-clause[optseq] new-line
12194 structured-block
12195 # pragma omp parallel for parallel-for-clause[optseq] new-line
12196 structured-block
12197 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
12198 structured-block
12200 OpenMP 4.0:
12201 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
12202 structured-block
12204 LOC is the location of the #pragma token.
12207 #define OMP_PARALLEL_CLAUSE_MASK \
12208 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
12209 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
12210 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
12211 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
12212 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
12213 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
12214 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
12215 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
12216 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
12218 static tree
12219 c_parser_omp_parallel (location_t loc, c_parser *parser,
12220 char *p_name, omp_clause_mask mask, tree *cclauses)
12222 tree stmt, clauses, block;
12224 strcat (p_name, " parallel");
12225 mask |= OMP_PARALLEL_CLAUSE_MASK;
12227 if (c_parser_next_token_is_keyword (parser, RID_FOR))
12229 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
12230 if (cclauses == NULL)
12231 cclauses = cclauses_buf;
12233 c_parser_consume_token (parser);
12234 if (!flag_openmp) /* flag_openmp_simd */
12235 return c_parser_omp_for (loc, parser, p_name, mask, cclauses);
12236 block = c_begin_omp_parallel ();
12237 tree ret = c_parser_omp_for (loc, parser, p_name, mask, cclauses);
12238 stmt
12239 = c_finish_omp_parallel (loc, cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
12240 block);
12241 if (ret == NULL_TREE)
12242 return ret;
12243 OMP_PARALLEL_COMBINED (stmt) = 1;
12244 return stmt;
12246 else if (cclauses)
12248 error_at (loc, "expected %<for%> after %qs", p_name);
12249 c_parser_skip_to_pragma_eol (parser);
12250 return NULL_TREE;
12252 else if (!flag_openmp) /* flag_openmp_simd */
12254 c_parser_skip_to_pragma_eol (parser);
12255 return NULL_TREE;
12257 else if (c_parser_next_token_is (parser, CPP_NAME))
12259 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
12260 if (strcmp (p, "sections") == 0)
12262 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
12263 if (cclauses == NULL)
12264 cclauses = cclauses_buf;
12266 c_parser_consume_token (parser);
12267 block = c_begin_omp_parallel ();
12268 c_parser_omp_sections (loc, parser, p_name, mask, cclauses);
12269 stmt = c_finish_omp_parallel (loc,
12270 cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
12271 block);
12272 OMP_PARALLEL_COMBINED (stmt) = 1;
12273 return stmt;
12277 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
12279 block = c_begin_omp_parallel ();
12280 c_parser_statement (parser);
12281 stmt = c_finish_omp_parallel (loc, clauses, block);
12283 return stmt;
12286 /* OpenMP 2.5:
12287 # pragma omp single single-clause[optseq] new-line
12288 structured-block
12290 LOC is the location of the #pragma.
12293 #define OMP_SINGLE_CLAUSE_MASK \
12294 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
12295 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
12296 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
12297 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
12299 static tree
12300 c_parser_omp_single (location_t loc, c_parser *parser)
12302 tree stmt = make_node (OMP_SINGLE);
12303 SET_EXPR_LOCATION (stmt, loc);
12304 TREE_TYPE (stmt) = void_type_node;
12306 OMP_SINGLE_CLAUSES (stmt)
12307 = c_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
12308 "#pragma omp single");
12309 OMP_SINGLE_BODY (stmt) = c_parser_omp_structured_block (parser);
12311 return add_stmt (stmt);
12314 /* OpenMP 3.0:
12315 # pragma omp task task-clause[optseq] new-line
12317 LOC is the location of the #pragma.
12320 #define OMP_TASK_CLAUSE_MASK \
12321 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
12322 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
12323 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
12324 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
12325 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
12326 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
12327 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
12328 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
12329 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND))
12331 static tree
12332 c_parser_omp_task (location_t loc, c_parser *parser)
12334 tree clauses, block;
12336 clauses = c_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
12337 "#pragma omp task");
12339 block = c_begin_omp_task ();
12340 c_parser_statement (parser);
12341 return c_finish_omp_task (loc, clauses, block);
12344 /* OpenMP 3.0:
12345 # pragma omp taskwait new-line
12348 static void
12349 c_parser_omp_taskwait (c_parser *parser)
12351 location_t loc = c_parser_peek_token (parser)->location;
12352 c_parser_consume_pragma (parser);
12353 c_parser_skip_to_pragma_eol (parser);
12355 c_finish_omp_taskwait (loc);
12358 /* OpenMP 3.1:
12359 # pragma omp taskyield new-line
12362 static void
12363 c_parser_omp_taskyield (c_parser *parser)
12365 location_t loc = c_parser_peek_token (parser)->location;
12366 c_parser_consume_pragma (parser);
12367 c_parser_skip_to_pragma_eol (parser);
12369 c_finish_omp_taskyield (loc);
12372 /* OpenMP 4.0:
12373 # pragma omp taskgroup new-line
12376 static tree
12377 c_parser_omp_taskgroup (c_parser *parser)
12379 location_t loc = c_parser_peek_token (parser)->location;
12380 c_parser_skip_to_pragma_eol (parser);
12381 return c_finish_omp_taskgroup (loc, c_parser_omp_structured_block (parser));
12384 /* OpenMP 4.0:
12385 # pragma omp cancel cancel-clause[optseq] new-line
12387 LOC is the location of the #pragma.
12390 #define OMP_CANCEL_CLAUSE_MASK \
12391 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
12392 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
12393 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
12394 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
12395 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
12397 static void
12398 c_parser_omp_cancel (c_parser *parser)
12400 location_t loc = c_parser_peek_token (parser)->location;
12402 c_parser_consume_pragma (parser);
12403 tree clauses = c_parser_omp_all_clauses (parser, OMP_CANCEL_CLAUSE_MASK,
12404 "#pragma omp cancel");
12406 c_finish_omp_cancel (loc, clauses);
12409 /* OpenMP 4.0:
12410 # pragma omp cancellation point cancelpt-clause[optseq] new-line
12412 LOC is the location of the #pragma.
12415 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
12416 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
12417 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
12418 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
12419 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
12421 static void
12422 c_parser_omp_cancellation_point (c_parser *parser)
12424 location_t loc = c_parser_peek_token (parser)->location;
12425 tree clauses;
12426 bool point_seen = false;
12428 c_parser_consume_pragma (parser);
12429 if (c_parser_next_token_is (parser, CPP_NAME))
12431 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
12432 if (strcmp (p, "point") == 0)
12434 c_parser_consume_token (parser);
12435 point_seen = true;
12438 if (!point_seen)
12440 c_parser_error (parser, "expected %<point%>");
12441 c_parser_skip_to_pragma_eol (parser);
12442 return;
12445 clauses
12446 = c_parser_omp_all_clauses (parser, OMP_CANCELLATION_POINT_CLAUSE_MASK,
12447 "#pragma omp cancellation point");
12449 c_finish_omp_cancellation_point (loc, clauses);
12452 /* OpenMP 4.0:
12453 #pragma omp distribute distribute-clause[optseq] new-line
12454 for-loop */
12456 #define OMP_DISTRIBUTE_CLAUSE_MASK \
12457 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
12458 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
12459 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
12460 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
12462 static tree
12463 c_parser_omp_distribute (location_t loc, c_parser *parser,
12464 char *p_name, omp_clause_mask mask, tree *cclauses)
12466 tree clauses, block, ret;
12468 strcat (p_name, " distribute");
12469 mask |= OMP_DISTRIBUTE_CLAUSE_MASK;
12471 if (c_parser_next_token_is (parser, CPP_NAME))
12473 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
12474 bool simd = false;
12475 bool parallel = false;
12477 if (strcmp (p, "simd") == 0)
12478 simd = true;
12479 else
12480 parallel = strcmp (p, "parallel") == 0;
12481 if (parallel || simd)
12483 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
12484 if (cclauses == NULL)
12485 cclauses = cclauses_buf;
12486 c_parser_consume_token (parser);
12487 if (!flag_openmp) /* flag_openmp_simd */
12489 if (simd)
12490 return c_parser_omp_simd (loc, parser, p_name, mask, cclauses);
12491 else
12492 return c_parser_omp_parallel (loc, parser, p_name, mask,
12493 cclauses);
12495 block = c_begin_compound_stmt (true);
12496 if (simd)
12497 ret = c_parser_omp_simd (loc, parser, p_name, mask, cclauses);
12498 else
12499 ret = c_parser_omp_parallel (loc, parser, p_name, mask, cclauses);
12500 block = c_end_compound_stmt (loc, block, true);
12501 if (ret == NULL)
12502 return ret;
12503 ret = make_node (OMP_DISTRIBUTE);
12504 TREE_TYPE (ret) = void_type_node;
12505 OMP_FOR_BODY (ret) = block;
12506 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
12507 SET_EXPR_LOCATION (ret, loc);
12508 add_stmt (ret);
12509 return ret;
12512 if (!flag_openmp) /* flag_openmp_simd */
12514 c_parser_skip_to_pragma_eol (parser);
12515 return NULL_TREE;
12518 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
12519 if (cclauses)
12521 omp_split_clauses (loc, OMP_DISTRIBUTE, mask, clauses, cclauses);
12522 clauses = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
12525 block = c_begin_compound_stmt (true);
12526 ret = c_parser_omp_for_loop (loc, parser, OMP_DISTRIBUTE, clauses, NULL);
12527 block = c_end_compound_stmt (loc, block, true);
12528 add_stmt (block);
12530 return ret;
12533 /* OpenMP 4.0:
12534 # pragma omp teams teams-clause[optseq] new-line
12535 structured-block */
12537 #define OMP_TEAMS_CLAUSE_MASK \
12538 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
12539 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
12540 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
12541 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
12542 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
12543 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
12544 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
12546 static tree
12547 c_parser_omp_teams (location_t loc, c_parser *parser,
12548 char *p_name, omp_clause_mask mask, tree *cclauses)
12550 tree clauses, block, ret;
12552 strcat (p_name, " teams");
12553 mask |= OMP_TEAMS_CLAUSE_MASK;
12555 if (c_parser_next_token_is (parser, CPP_NAME))
12557 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
12558 if (strcmp (p, "distribute") == 0)
12560 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
12561 if (cclauses == NULL)
12562 cclauses = cclauses_buf;
12564 c_parser_consume_token (parser);
12565 if (!flag_openmp) /* flag_openmp_simd */
12566 return c_parser_omp_distribute (loc, parser, p_name, mask, cclauses);
12567 block = c_begin_compound_stmt (true);
12568 ret = c_parser_omp_distribute (loc, parser, p_name, mask, cclauses);
12569 block = c_end_compound_stmt (loc, block, true);
12570 if (ret == NULL)
12571 return ret;
12572 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
12573 ret = make_node (OMP_TEAMS);
12574 TREE_TYPE (ret) = void_type_node;
12575 OMP_TEAMS_CLAUSES (ret) = clauses;
12576 OMP_TEAMS_BODY (ret) = block;
12577 return add_stmt (ret);
12580 if (!flag_openmp) /* flag_openmp_simd */
12582 c_parser_skip_to_pragma_eol (parser);
12583 return NULL_TREE;
12586 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
12587 if (cclauses)
12589 omp_split_clauses (loc, OMP_TEAMS, mask, clauses, cclauses);
12590 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
12593 tree stmt = make_node (OMP_TEAMS);
12594 TREE_TYPE (stmt) = void_type_node;
12595 OMP_TEAMS_CLAUSES (stmt) = clauses;
12596 OMP_TEAMS_BODY (stmt) = c_parser_omp_structured_block (parser);
12598 return add_stmt (stmt);
12601 /* OpenMP 4.0:
12602 # pragma omp target data target-data-clause[optseq] new-line
12603 structured-block */
12605 #define OMP_TARGET_DATA_CLAUSE_MASK \
12606 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
12607 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
12608 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
12610 static tree
12611 c_parser_omp_target_data (location_t loc, c_parser *parser)
12613 tree stmt = make_node (OMP_TARGET_DATA);
12614 TREE_TYPE (stmt) = void_type_node;
12616 OMP_TARGET_DATA_CLAUSES (stmt)
12617 = c_parser_omp_all_clauses (parser, OMP_TARGET_DATA_CLAUSE_MASK,
12618 "#pragma omp target data");
12619 keep_next_level ();
12620 tree block = c_begin_compound_stmt (true);
12621 add_stmt (c_parser_omp_structured_block (parser));
12622 OMP_TARGET_DATA_BODY (stmt) = c_end_compound_stmt (loc, block, true);
12624 SET_EXPR_LOCATION (stmt, loc);
12625 return add_stmt (stmt);
12628 /* OpenMP 4.0:
12629 # pragma omp target update target-update-clause[optseq] new-line */
12631 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
12632 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
12633 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
12634 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
12635 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
12637 static bool
12638 c_parser_omp_target_update (location_t loc, c_parser *parser,
12639 enum pragma_context context)
12641 if (context == pragma_stmt)
12643 error_at (loc,
12644 "%<#pragma omp target update%> may only be "
12645 "used in compound statements");
12646 c_parser_skip_to_pragma_eol (parser);
12647 return false;
12650 tree clauses
12651 = c_parser_omp_all_clauses (parser, OMP_TARGET_UPDATE_CLAUSE_MASK,
12652 "#pragma omp target update");
12653 if (find_omp_clause (clauses, OMP_CLAUSE_TO) == NULL_TREE
12654 && find_omp_clause (clauses, OMP_CLAUSE_FROM) == NULL_TREE)
12656 error_at (loc,
12657 "%<#pragma omp target update must contain at least one "
12658 "%<from%> or %<to%> clauses");
12659 return false;
12662 tree stmt = make_node (OMP_TARGET_UPDATE);
12663 TREE_TYPE (stmt) = void_type_node;
12664 OMP_TARGET_UPDATE_CLAUSES (stmt) = clauses;
12665 SET_EXPR_LOCATION (stmt, loc);
12666 add_stmt (stmt);
12667 return false;
12670 /* OpenMP 4.0:
12671 # pragma omp target target-clause[optseq] new-line
12672 structured-block */
12674 #define OMP_TARGET_CLAUSE_MASK \
12675 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
12676 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
12677 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
12679 static bool
12680 c_parser_omp_target (c_parser *parser, enum pragma_context context)
12682 location_t loc = c_parser_peek_token (parser)->location;
12683 c_parser_consume_pragma (parser);
12685 if (context != pragma_stmt && context != pragma_compound)
12687 c_parser_error (parser, "expected declaration specifiers");
12688 c_parser_skip_to_pragma_eol (parser);
12689 return false;
12692 if (c_parser_next_token_is (parser, CPP_NAME))
12694 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
12696 if (strcmp (p, "teams") == 0)
12698 tree cclauses[C_OMP_CLAUSE_SPLIT_COUNT];
12699 char p_name[sizeof ("#pragma omp target teams distribute "
12700 "parallel for simd")];
12702 c_parser_consume_token (parser);
12703 strcpy (p_name, "#pragma omp target");
12704 if (!flag_openmp) /* flag_openmp_simd */
12705 return c_parser_omp_teams (loc, parser, p_name,
12706 OMP_TARGET_CLAUSE_MASK, cclauses);
12707 keep_next_level ();
12708 tree block = c_begin_compound_stmt (true);
12709 tree ret = c_parser_omp_teams (loc, parser, p_name,
12710 OMP_TARGET_CLAUSE_MASK, cclauses);
12711 block = c_end_compound_stmt (loc, block, true);
12712 if (ret == NULL)
12713 return ret;
12714 tree stmt = make_node (OMP_TARGET);
12715 TREE_TYPE (stmt) = void_type_node;
12716 OMP_TARGET_CLAUSES (stmt) = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
12717 OMP_TARGET_BODY (stmt) = block;
12718 add_stmt (stmt);
12719 return true;
12721 else if (!flag_openmp) /* flag_openmp_simd */
12723 c_parser_skip_to_pragma_eol (parser);
12724 return NULL_TREE;
12726 else if (strcmp (p, "data") == 0)
12728 c_parser_consume_token (parser);
12729 c_parser_omp_target_data (loc, parser);
12730 return true;
12732 else if (strcmp (p, "update") == 0)
12734 c_parser_consume_token (parser);
12735 return c_parser_omp_target_update (loc, parser, context);
12739 tree stmt = make_node (OMP_TARGET);
12740 TREE_TYPE (stmt) = void_type_node;
12742 OMP_TARGET_CLAUSES (stmt)
12743 = c_parser_omp_all_clauses (parser, OMP_TARGET_CLAUSE_MASK,
12744 "#pragma omp target");
12745 keep_next_level ();
12746 tree block = c_begin_compound_stmt (true);
12747 add_stmt (c_parser_omp_structured_block (parser));
12748 OMP_TARGET_BODY (stmt) = c_end_compound_stmt (loc, block, true);
12750 SET_EXPR_LOCATION (stmt, loc);
12751 add_stmt (stmt);
12752 return true;
12755 /* OpenMP 4.0:
12756 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
12758 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
12759 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
12760 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
12761 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
12762 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
12763 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
12764 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
12766 static void
12767 c_parser_omp_declare_simd (c_parser *parser, enum pragma_context context)
12769 vec<c_token> clauses = vNULL;
12770 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
12772 c_token *token = c_parser_peek_token (parser);
12773 if (token->type == CPP_EOF)
12775 c_parser_skip_to_pragma_eol (parser);
12776 clauses.release ();
12777 return;
12779 clauses.safe_push (*token);
12780 c_parser_consume_token (parser);
12782 clauses.safe_push (*c_parser_peek_token (parser));
12783 c_parser_skip_to_pragma_eol (parser);
12785 while (c_parser_next_token_is (parser, CPP_PRAGMA))
12787 if (c_parser_peek_token (parser)->pragma_kind
12788 != PRAGMA_OMP_DECLARE_REDUCTION
12789 || c_parser_peek_2nd_token (parser)->type != CPP_NAME
12790 || strcmp (IDENTIFIER_POINTER
12791 (c_parser_peek_2nd_token (parser)->value),
12792 "simd") != 0)
12794 c_parser_error (parser,
12795 "%<#pragma omp declare simd%> must be followed by "
12796 "function declaration or definition or another "
12797 "%<#pragma omp declare simd%>");
12798 clauses.release ();
12799 return;
12801 c_parser_consume_pragma (parser);
12802 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
12804 c_token *token = c_parser_peek_token (parser);
12805 if (token->type == CPP_EOF)
12807 c_parser_skip_to_pragma_eol (parser);
12808 clauses.release ();
12809 return;
12811 clauses.safe_push (*token);
12812 c_parser_consume_token (parser);
12814 clauses.safe_push (*c_parser_peek_token (parser));
12815 c_parser_skip_to_pragma_eol (parser);
12818 /* Make sure nothing tries to read past the end of the tokens. */
12819 c_token eof_token;
12820 memset (&eof_token, 0, sizeof (eof_token));
12821 eof_token.type = CPP_EOF;
12822 clauses.safe_push (eof_token);
12823 clauses.safe_push (eof_token);
12825 switch (context)
12827 case pragma_external:
12828 if (c_parser_next_token_is (parser, CPP_KEYWORD)
12829 && c_parser_peek_token (parser)->keyword == RID_EXTENSION)
12831 int ext = disable_extension_diagnostics ();
12833 c_parser_consume_token (parser);
12834 while (c_parser_next_token_is (parser, CPP_KEYWORD)
12835 && c_parser_peek_token (parser)->keyword == RID_EXTENSION);
12836 c_parser_declaration_or_fndef (parser, true, true, true, false, true,
12837 NULL, clauses);
12838 restore_extension_diagnostics (ext);
12840 else
12841 c_parser_declaration_or_fndef (parser, true, true, true, false, true,
12842 NULL, clauses);
12843 break;
12844 case pragma_struct:
12845 case pragma_param:
12846 c_parser_error (parser, "%<#pragma omp declare simd%> must be followed by "
12847 "function declaration or definition");
12848 break;
12849 case pragma_compound:
12850 case pragma_stmt:
12851 if (c_parser_next_token_is (parser, CPP_KEYWORD)
12852 && c_parser_peek_token (parser)->keyword == RID_EXTENSION)
12854 int ext = disable_extension_diagnostics ();
12856 c_parser_consume_token (parser);
12857 while (c_parser_next_token_is (parser, CPP_KEYWORD)
12858 && c_parser_peek_token (parser)->keyword == RID_EXTENSION);
12859 if (c_parser_next_tokens_start_declaration (parser))
12861 c_parser_declaration_or_fndef (parser, true, true, true, true,
12862 true, NULL, clauses);
12863 restore_extension_diagnostics (ext);
12864 break;
12866 restore_extension_diagnostics (ext);
12868 else if (c_parser_next_tokens_start_declaration (parser))
12870 c_parser_declaration_or_fndef (parser, true, true, true, true, true,
12871 NULL, clauses);
12872 break;
12874 c_parser_error (parser, "%<#pragma omp declare simd%> must be followed by "
12875 "function declaration or definition");
12876 break;
12877 default:
12878 gcc_unreachable ();
12880 clauses.release ();
12883 /* Finalize #pragma omp declare simd clauses after FNDECL has been parsed,
12884 and put that into "omp declare simd" attribute. */
12886 static void
12887 c_finish_omp_declare_simd (c_parser *parser, tree fndecl, tree parms,
12888 vec<c_token> clauses)
12890 if (flag_cilkplus
12891 && clauses.exists () && !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
12893 error ("%<#pragma omp declare simd%> cannot be used in the same "
12894 "function marked as a Cilk Plus SIMD-enabled function");
12895 vec_free (parser->cilk_simd_fn_tokens);
12896 return;
12899 /* Normally first token is CPP_NAME "simd". CPP_EOF there indicates
12900 error has been reported and CPP_PRAGMA that c_finish_omp_declare_simd
12901 has already processed the tokens. */
12902 if (clauses.exists () && clauses[0].type == CPP_EOF)
12903 return;
12904 if (fndecl == NULL_TREE || TREE_CODE (fndecl) != FUNCTION_DECL)
12906 error ("%<#pragma omp declare simd%> not immediately followed by "
12907 "a function declaration or definition");
12908 clauses[0].type = CPP_EOF;
12909 return;
12911 if (clauses.exists () && clauses[0].type != CPP_NAME)
12913 error_at (DECL_SOURCE_LOCATION (fndecl),
12914 "%<#pragma omp declare simd%> not immediately followed by "
12915 "a single function declaration or definition");
12916 clauses[0].type = CPP_EOF;
12917 return;
12920 if (parms == NULL_TREE)
12921 parms = DECL_ARGUMENTS (fndecl);
12923 unsigned int tokens_avail = parser->tokens_avail;
12924 gcc_assert (parser->tokens == &parser->tokens_buf[0]);
12925 bool is_cilkplus_cilk_simd_fn = false;
12927 if (flag_cilkplus && !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
12929 parser->tokens = parser->cilk_simd_fn_tokens->address ();
12930 parser->tokens_avail = vec_safe_length (parser->cilk_simd_fn_tokens);
12931 is_cilkplus_cilk_simd_fn = true;
12933 else
12935 parser->tokens = clauses.address ();
12936 parser->tokens_avail = clauses.length ();
12939 /* c_parser_omp_declare_simd pushed 2 extra CPP_EOF tokens at the end. */
12940 while (parser->tokens_avail > 3)
12942 c_token *token = c_parser_peek_token (parser);
12943 if (!is_cilkplus_cilk_simd_fn)
12944 gcc_assert (token->type == CPP_NAME
12945 && strcmp (IDENTIFIER_POINTER (token->value), "simd") == 0);
12946 else
12947 gcc_assert (token->type == CPP_NAME
12948 && is_cilkplus_vector_p (token->value));
12949 c_parser_consume_token (parser);
12950 parser->in_pragma = true;
12952 tree c = NULL_TREE;
12953 if (is_cilkplus_cilk_simd_fn)
12954 c = c_parser_omp_all_clauses (parser, CILK_SIMD_FN_CLAUSE_MASK,
12955 "SIMD-enabled functions attribute");
12956 else
12957 c = c_parser_omp_all_clauses (parser, OMP_DECLARE_SIMD_CLAUSE_MASK,
12958 "#pragma omp declare simd");
12959 c = c_omp_declare_simd_clauses_to_numbers (parms, c);
12960 if (c != NULL_TREE)
12961 c = tree_cons (NULL_TREE, c, NULL_TREE);
12962 if (is_cilkplus_cilk_simd_fn)
12964 tree k = build_tree_list (get_identifier ("cilk simd function"),
12965 NULL_TREE);
12966 TREE_CHAIN (k) = DECL_ATTRIBUTES (fndecl);
12967 DECL_ATTRIBUTES (fndecl) = k;
12969 c = build_tree_list (get_identifier ("omp declare simd"), c);
12970 TREE_CHAIN (c) = DECL_ATTRIBUTES (fndecl);
12971 DECL_ATTRIBUTES (fndecl) = c;
12974 parser->tokens = &parser->tokens_buf[0];
12975 parser->tokens_avail = tokens_avail;
12976 if (clauses.exists ())
12977 clauses[0].type = CPP_PRAGMA;
12979 if (!vec_safe_is_empty (parser->cilk_simd_fn_tokens))
12980 vec_free (parser->cilk_simd_fn_tokens);
12984 /* OpenMP 4.0:
12985 # pragma omp declare target new-line
12986 declarations and definitions
12987 # pragma omp end declare target new-line */
12989 static void
12990 c_parser_omp_declare_target (c_parser *parser)
12992 c_parser_skip_to_pragma_eol (parser);
12993 current_omp_declare_target_attribute++;
12996 static void
12997 c_parser_omp_end_declare_target (c_parser *parser)
12999 location_t loc = c_parser_peek_token (parser)->location;
13000 c_parser_consume_pragma (parser);
13001 if (c_parser_next_token_is (parser, CPP_NAME)
13002 && strcmp (IDENTIFIER_POINTER (c_parser_peek_token (parser)->value),
13003 "declare") == 0)
13005 c_parser_consume_token (parser);
13006 if (c_parser_next_token_is (parser, CPP_NAME)
13007 && strcmp (IDENTIFIER_POINTER (c_parser_peek_token (parser)->value),
13008 "target") == 0)
13009 c_parser_consume_token (parser);
13010 else
13012 c_parser_error (parser, "expected %<target%>");
13013 c_parser_skip_to_pragma_eol (parser);
13014 return;
13017 else
13019 c_parser_error (parser, "expected %<declare%>");
13020 c_parser_skip_to_pragma_eol (parser);
13021 return;
13023 c_parser_skip_to_pragma_eol (parser);
13024 if (!current_omp_declare_target_attribute)
13025 error_at (loc, "%<#pragma omp end declare target%> without corresponding "
13026 "%<#pragma omp declare target%>");
13027 else
13028 current_omp_declare_target_attribute--;
13032 /* OpenMP 4.0
13033 #pragma omp declare reduction (reduction-id : typename-list : expression) \
13034 initializer-clause[opt] new-line
13036 initializer-clause:
13037 initializer (omp_priv = initializer)
13038 initializer (function-name (argument-list)) */
13040 static void
13041 c_parser_omp_declare_reduction (c_parser *parser, enum pragma_context context)
13043 unsigned int tokens_avail = 0, i;
13044 vec<tree> types = vNULL;
13045 vec<c_token> clauses = vNULL;
13046 enum tree_code reduc_code = ERROR_MARK;
13047 tree reduc_id = NULL_TREE;
13048 tree type;
13049 location_t rloc = c_parser_peek_token (parser)->location;
13051 if (context == pragma_struct || context == pragma_param)
13053 error ("%<#pragma omp declare reduction%> not at file or block scope");
13054 goto fail;
13057 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
13058 goto fail;
13060 switch (c_parser_peek_token (parser)->type)
13062 case CPP_PLUS:
13063 reduc_code = PLUS_EXPR;
13064 break;
13065 case CPP_MULT:
13066 reduc_code = MULT_EXPR;
13067 break;
13068 case CPP_MINUS:
13069 reduc_code = MINUS_EXPR;
13070 break;
13071 case CPP_AND:
13072 reduc_code = BIT_AND_EXPR;
13073 break;
13074 case CPP_XOR:
13075 reduc_code = BIT_XOR_EXPR;
13076 break;
13077 case CPP_OR:
13078 reduc_code = BIT_IOR_EXPR;
13079 break;
13080 case CPP_AND_AND:
13081 reduc_code = TRUTH_ANDIF_EXPR;
13082 break;
13083 case CPP_OR_OR:
13084 reduc_code = TRUTH_ORIF_EXPR;
13085 break;
13086 case CPP_NAME:
13087 const char *p;
13088 p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
13089 if (strcmp (p, "min") == 0)
13091 reduc_code = MIN_EXPR;
13092 break;
13094 if (strcmp (p, "max") == 0)
13096 reduc_code = MAX_EXPR;
13097 break;
13099 reduc_id = c_parser_peek_token (parser)->value;
13100 break;
13101 default:
13102 c_parser_error (parser,
13103 "expected %<+%>, %<*%>, %<-%>, %<&%>, "
13104 "%<^%>, %<|%>, %<&&%>, %<||%>, %<min%> or identifier");
13105 goto fail;
13108 tree orig_reduc_id, reduc_decl;
13109 orig_reduc_id = reduc_id;
13110 reduc_id = c_omp_reduction_id (reduc_code, reduc_id);
13111 reduc_decl = c_omp_reduction_decl (reduc_id);
13112 c_parser_consume_token (parser);
13114 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
13115 goto fail;
13117 while (true)
13119 location_t loc = c_parser_peek_token (parser)->location;
13120 struct c_type_name *ctype = c_parser_type_name (parser);
13121 if (ctype != NULL)
13123 type = groktypename (ctype, NULL, NULL);
13124 if (type == error_mark_node)
13126 else if ((INTEGRAL_TYPE_P (type)
13127 || TREE_CODE (type) == REAL_TYPE
13128 || TREE_CODE (type) == COMPLEX_TYPE)
13129 && orig_reduc_id == NULL_TREE)
13130 error_at (loc, "predeclared arithmetic type in "
13131 "%<#pragma omp declare reduction%>");
13132 else if (TREE_CODE (type) == FUNCTION_TYPE
13133 || TREE_CODE (type) == ARRAY_TYPE)
13134 error_at (loc, "function or array type in "
13135 "%<#pragma omp declare reduction%>");
13136 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
13137 error_at (loc, "const, volatile or restrict qualified type in "
13138 "%<#pragma omp declare reduction%>");
13139 else
13141 tree t;
13142 for (t = DECL_INITIAL (reduc_decl); t; t = TREE_CHAIN (t))
13143 if (comptypes (TREE_PURPOSE (t), type))
13145 error_at (loc, "redeclaration of %qs "
13146 "%<#pragma omp declare reduction%> for "
13147 "type %qT",
13148 IDENTIFIER_POINTER (reduc_id)
13149 + sizeof ("omp declare reduction ") - 1,
13150 type);
13151 location_t ploc
13152 = DECL_SOURCE_LOCATION (TREE_VEC_ELT (TREE_VALUE (t),
13153 0));
13154 error_at (ploc, "previous %<#pragma omp declare "
13155 "reduction%>");
13156 break;
13158 if (t == NULL_TREE)
13159 types.safe_push (type);
13161 if (c_parser_next_token_is (parser, CPP_COMMA))
13162 c_parser_consume_token (parser);
13163 else
13164 break;
13166 else
13167 break;
13170 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>")
13171 || types.is_empty ())
13173 fail:
13174 clauses.release ();
13175 types.release ();
13176 while (true)
13178 c_token *token = c_parser_peek_token (parser);
13179 if (token->type == CPP_EOF || token->type == CPP_PRAGMA_EOL)
13180 break;
13181 c_parser_consume_token (parser);
13183 c_parser_skip_to_pragma_eol (parser);
13184 return;
13187 if (types.length () > 1)
13189 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
13191 c_token *token = c_parser_peek_token (parser);
13192 if (token->type == CPP_EOF)
13193 goto fail;
13194 clauses.safe_push (*token);
13195 c_parser_consume_token (parser);
13197 clauses.safe_push (*c_parser_peek_token (parser));
13198 c_parser_skip_to_pragma_eol (parser);
13200 /* Make sure nothing tries to read past the end of the tokens. */
13201 c_token eof_token;
13202 memset (&eof_token, 0, sizeof (eof_token));
13203 eof_token.type = CPP_EOF;
13204 clauses.safe_push (eof_token);
13205 clauses.safe_push (eof_token);
13208 int errs = errorcount;
13209 FOR_EACH_VEC_ELT (types, i, type)
13211 tokens_avail = parser->tokens_avail;
13212 gcc_assert (parser->tokens == &parser->tokens_buf[0]);
13213 if (!clauses.is_empty ())
13215 parser->tokens = clauses.address ();
13216 parser->tokens_avail = clauses.length ();
13217 parser->in_pragma = true;
13220 bool nested = current_function_decl != NULL_TREE;
13221 if (nested)
13222 c_push_function_context ();
13223 tree fndecl = build_decl (BUILTINS_LOCATION, FUNCTION_DECL,
13224 reduc_id, default_function_type);
13225 current_function_decl = fndecl;
13226 allocate_struct_function (fndecl, true);
13227 push_scope ();
13228 tree stmt = push_stmt_list ();
13229 /* Intentionally BUILTINS_LOCATION, so that -Wshadow doesn't
13230 warn about these. */
13231 tree omp_out = build_decl (BUILTINS_LOCATION, VAR_DECL,
13232 get_identifier ("omp_out"), type);
13233 DECL_ARTIFICIAL (omp_out) = 1;
13234 DECL_CONTEXT (omp_out) = fndecl;
13235 pushdecl (omp_out);
13236 tree omp_in = build_decl (BUILTINS_LOCATION, VAR_DECL,
13237 get_identifier ("omp_in"), type);
13238 DECL_ARTIFICIAL (omp_in) = 1;
13239 DECL_CONTEXT (omp_in) = fndecl;
13240 pushdecl (omp_in);
13241 struct c_expr combiner = c_parser_expression (parser);
13242 struct c_expr initializer;
13243 tree omp_priv = NULL_TREE, omp_orig = NULL_TREE;
13244 bool bad = false;
13245 initializer.value = error_mark_node;
13246 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
13247 bad = true;
13248 else if (c_parser_next_token_is (parser, CPP_NAME)
13249 && strcmp (IDENTIFIER_POINTER
13250 (c_parser_peek_token (parser)->value),
13251 "initializer") == 0)
13253 c_parser_consume_token (parser);
13254 pop_scope ();
13255 push_scope ();
13256 omp_priv = build_decl (BUILTINS_LOCATION, VAR_DECL,
13257 get_identifier ("omp_priv"), type);
13258 DECL_ARTIFICIAL (omp_priv) = 1;
13259 DECL_INITIAL (omp_priv) = error_mark_node;
13260 DECL_CONTEXT (omp_priv) = fndecl;
13261 pushdecl (omp_priv);
13262 omp_orig = build_decl (BUILTINS_LOCATION, VAR_DECL,
13263 get_identifier ("omp_orig"), type);
13264 DECL_ARTIFICIAL (omp_orig) = 1;
13265 DECL_CONTEXT (omp_orig) = fndecl;
13266 pushdecl (omp_orig);
13267 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
13268 bad = true;
13269 else if (!c_parser_next_token_is (parser, CPP_NAME))
13271 c_parser_error (parser, "expected %<omp_priv%> or "
13272 "function-name");
13273 bad = true;
13275 else if (strcmp (IDENTIFIER_POINTER
13276 (c_parser_peek_token (parser)->value),
13277 "omp_priv") != 0)
13279 if (c_parser_peek_2nd_token (parser)->type != CPP_OPEN_PAREN
13280 || c_parser_peek_token (parser)->id_kind != C_ID_ID)
13282 c_parser_error (parser, "expected function-name %<(%>");
13283 bad = true;
13285 else
13286 initializer = c_parser_postfix_expression (parser);
13287 if (initializer.value
13288 && TREE_CODE (initializer.value) == CALL_EXPR)
13290 int j;
13291 tree c = initializer.value;
13292 for (j = 0; j < call_expr_nargs (c); j++)
13293 if (TREE_CODE (CALL_EXPR_ARG (c, j)) == ADDR_EXPR
13294 && TREE_OPERAND (CALL_EXPR_ARG (c, j), 0) == omp_priv)
13295 break;
13296 if (j == call_expr_nargs (c))
13297 error ("one of the initializer call arguments should be "
13298 "%<&omp_priv%>");
13301 else
13303 c_parser_consume_token (parser);
13304 if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
13305 bad = true;
13306 else
13308 tree st = push_stmt_list ();
13309 start_init (omp_priv, NULL_TREE, 0);
13310 location_t loc = c_parser_peek_token (parser)->location;
13311 struct c_expr init = c_parser_initializer (parser);
13312 finish_init ();
13313 finish_decl (omp_priv, loc, init.value,
13314 init.original_type, NULL_TREE);
13315 pop_stmt_list (st);
13318 if (!bad
13319 && !c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
13320 bad = true;
13323 if (!bad)
13325 c_parser_skip_to_pragma_eol (parser);
13327 tree t = tree_cons (type, make_tree_vec (omp_priv ? 6 : 3),
13328 DECL_INITIAL (reduc_decl));
13329 DECL_INITIAL (reduc_decl) = t;
13330 DECL_SOURCE_LOCATION (omp_out) = rloc;
13331 TREE_VEC_ELT (TREE_VALUE (t), 0) = omp_out;
13332 TREE_VEC_ELT (TREE_VALUE (t), 1) = omp_in;
13333 TREE_VEC_ELT (TREE_VALUE (t), 2) = combiner.value;
13334 walk_tree (&combiner.value, c_check_omp_declare_reduction_r,
13335 &TREE_VEC_ELT (TREE_VALUE (t), 0), NULL);
13336 if (omp_priv)
13338 DECL_SOURCE_LOCATION (omp_priv) = rloc;
13339 TREE_VEC_ELT (TREE_VALUE (t), 3) = omp_priv;
13340 TREE_VEC_ELT (TREE_VALUE (t), 4) = omp_orig;
13341 TREE_VEC_ELT (TREE_VALUE (t), 5) = initializer.value;
13342 walk_tree (&initializer.value, c_check_omp_declare_reduction_r,
13343 &TREE_VEC_ELT (TREE_VALUE (t), 3), NULL);
13344 walk_tree (&DECL_INITIAL (omp_priv),
13345 c_check_omp_declare_reduction_r,
13346 &TREE_VEC_ELT (TREE_VALUE (t), 3), NULL);
13350 pop_stmt_list (stmt);
13351 pop_scope ();
13352 if (cfun->language != NULL)
13354 ggc_free (cfun->language);
13355 cfun->language = NULL;
13357 set_cfun (NULL);
13358 current_function_decl = NULL_TREE;
13359 if (nested)
13360 c_pop_function_context ();
13362 if (!clauses.is_empty ())
13364 parser->tokens = &parser->tokens_buf[0];
13365 parser->tokens_avail = tokens_avail;
13367 if (bad)
13368 goto fail;
13369 if (errs != errorcount)
13370 break;
13373 clauses.release ();
13374 types.release ();
13378 /* OpenMP 4.0
13379 #pragma omp declare simd declare-simd-clauses[optseq] new-line
13380 #pragma omp declare reduction (reduction-id : typename-list : expression) \
13381 initializer-clause[opt] new-line
13382 #pragma omp declare target new-line */
13384 static void
13385 c_parser_omp_declare (c_parser *parser, enum pragma_context context)
13387 c_parser_consume_pragma (parser);
13388 if (c_parser_next_token_is (parser, CPP_NAME))
13390 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
13391 if (strcmp (p, "simd") == 0)
13393 /* c_parser_consume_token (parser); done in
13394 c_parser_omp_declare_simd. */
13395 c_parser_omp_declare_simd (parser, context);
13396 return;
13398 if (strcmp (p, "reduction") == 0)
13400 c_parser_consume_token (parser);
13401 c_parser_omp_declare_reduction (parser, context);
13402 return;
13404 if (!flag_openmp) /* flag_openmp_simd */
13406 c_parser_skip_to_pragma_eol (parser);
13407 return;
13409 if (strcmp (p, "target") == 0)
13411 c_parser_consume_token (parser);
13412 c_parser_omp_declare_target (parser);
13413 return;
13417 c_parser_error (parser, "expected %<simd%> or %<reduction%> "
13418 "or %<target%>");
13419 c_parser_skip_to_pragma_eol (parser);
13422 /* Main entry point to parsing most OpenMP pragmas. */
13424 static void
13425 c_parser_omp_construct (c_parser *parser)
13427 enum pragma_kind p_kind;
13428 location_t loc;
13429 tree stmt;
13430 char p_name[sizeof "#pragma omp teams distribute parallel for simd"];
13431 omp_clause_mask mask (0);
13433 loc = c_parser_peek_token (parser)->location;
13434 p_kind = c_parser_peek_token (parser)->pragma_kind;
13435 c_parser_consume_pragma (parser);
13437 switch (p_kind)
13439 case PRAGMA_OMP_ATOMIC:
13440 c_parser_omp_atomic (loc, parser);
13441 return;
13442 case PRAGMA_OMP_CRITICAL:
13443 stmt = c_parser_omp_critical (loc, parser);
13444 break;
13445 case PRAGMA_OMP_DISTRIBUTE:
13446 strcpy (p_name, "#pragma omp");
13447 stmt = c_parser_omp_distribute (loc, parser, p_name, mask, NULL);
13448 break;
13449 case PRAGMA_OMP_FOR:
13450 strcpy (p_name, "#pragma omp");
13451 stmt = c_parser_omp_for (loc, parser, p_name, mask, NULL);
13452 break;
13453 case PRAGMA_OMP_MASTER:
13454 stmt = c_parser_omp_master (loc, parser);
13455 break;
13456 case PRAGMA_OMP_ORDERED:
13457 stmt = c_parser_omp_ordered (loc, parser);
13458 break;
13459 case PRAGMA_OMP_PARALLEL:
13460 strcpy (p_name, "#pragma omp");
13461 stmt = c_parser_omp_parallel (loc, parser, p_name, mask, NULL);
13462 break;
13463 case PRAGMA_OMP_SECTIONS:
13464 strcpy (p_name, "#pragma omp");
13465 stmt = c_parser_omp_sections (loc, parser, p_name, mask, NULL);
13466 break;
13467 case PRAGMA_OMP_SIMD:
13468 strcpy (p_name, "#pragma omp");
13469 stmt = c_parser_omp_simd (loc, parser, p_name, mask, NULL);
13470 break;
13471 case PRAGMA_OMP_SINGLE:
13472 stmt = c_parser_omp_single (loc, parser);
13473 break;
13474 case PRAGMA_OMP_TASK:
13475 stmt = c_parser_omp_task (loc, parser);
13476 break;
13477 case PRAGMA_OMP_TASKGROUP:
13478 stmt = c_parser_omp_taskgroup (parser);
13479 break;
13480 case PRAGMA_OMP_TEAMS:
13481 strcpy (p_name, "#pragma omp");
13482 stmt = c_parser_omp_teams (loc, parser, p_name, mask, NULL);
13483 break;
13484 default:
13485 gcc_unreachable ();
13488 if (stmt)
13489 gcc_assert (EXPR_LOCATION (stmt) != UNKNOWN_LOCATION);
13493 /* OpenMP 2.5:
13494 # pragma omp threadprivate (variable-list) */
13496 static void
13497 c_parser_omp_threadprivate (c_parser *parser)
13499 tree vars, t;
13500 location_t loc;
13502 c_parser_consume_pragma (parser);
13503 loc = c_parser_peek_token (parser)->location;
13504 vars = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_ERROR, NULL);
13506 /* Mark every variable in VARS to be assigned thread local storage. */
13507 for (t = vars; t; t = TREE_CHAIN (t))
13509 tree v = TREE_PURPOSE (t);
13511 /* FIXME diagnostics: Ideally we should keep individual
13512 locations for all the variables in the var list to make the
13513 following errors more precise. Perhaps
13514 c_parser_omp_var_list_parens() should construct a list of
13515 locations to go along with the var list. */
13517 /* If V had already been marked threadprivate, it doesn't matter
13518 whether it had been used prior to this point. */
13519 if (TREE_CODE (v) != VAR_DECL)
13520 error_at (loc, "%qD is not a variable", v);
13521 else if (TREE_USED (v) && !C_DECL_THREADPRIVATE_P (v))
13522 error_at (loc, "%qE declared %<threadprivate%> after first use", v);
13523 else if (! TREE_STATIC (v) && ! DECL_EXTERNAL (v))
13524 error_at (loc, "automatic variable %qE cannot be %<threadprivate%>", v);
13525 else if (TREE_TYPE (v) == error_mark_node)
13527 else if (! COMPLETE_TYPE_P (TREE_TYPE (v)))
13528 error_at (loc, "%<threadprivate%> %qE has incomplete type", v);
13529 else
13531 if (! DECL_THREAD_LOCAL_P (v))
13533 DECL_TLS_MODEL (v) = decl_default_tls_model (v);
13534 /* If rtl has been already set for this var, call
13535 make_decl_rtl once again, so that encode_section_info
13536 has a chance to look at the new decl flags. */
13537 if (DECL_RTL_SET_P (v))
13538 make_decl_rtl (v);
13540 C_DECL_THREADPRIVATE_P (v) = 1;
13544 c_parser_skip_to_pragma_eol (parser);
13547 /* Cilk Plus <#pragma simd> parsing routines. */
13549 /* Helper function for c_parser_pragma. Perform some sanity checking
13550 for <#pragma simd> constructs. Returns FALSE if there was a
13551 problem. */
13553 static bool
13554 c_parser_cilk_verify_simd (c_parser *parser,
13555 enum pragma_context context)
13557 if (!flag_cilkplus)
13559 warning (0, "pragma simd ignored because -fcilkplus is not enabled");
13560 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
13561 return false;
13563 if (context == pragma_external)
13565 c_parser_error (parser,"pragma simd must be inside a function");
13566 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
13567 return false;
13569 return true;
13572 /* Cilk Plus:
13573 This function is shared by SIMD-enabled functions and #pragma simd.
13574 If IS_SIMD_FN is true then it is parsing a SIMD-enabled function and
13575 CLAUSES is unused. The main purpose of this function is to parse a
13576 vectorlength attribute or clause and check for parse errors.
13577 When IS_SIMD_FN is true then the function is merely caching the tokens
13578 in PARSER->CILK_SIMD_FN_TOKENS. If errors are found then the token
13579 cache is cleared since there is no reason to continue.
13580 Syntax:
13581 vectorlength ( constant-expression ) */
13583 static tree
13584 c_parser_cilk_clause_vectorlength (c_parser *parser, tree clauses,
13585 bool is_simd_fn)
13587 if (is_simd_fn)
13588 check_no_duplicate_clause (clauses, OMP_CLAUSE_SIMDLEN, "vectorlength");
13589 else
13590 /* The vectorlength clause behaves exactly like OpenMP's safelen
13591 clause. Represent it in OpenMP terms. */
13592 check_no_duplicate_clause (clauses, OMP_CLAUSE_SAFELEN, "vectorlength");
13594 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
13595 return clauses;
13597 location_t loc = c_parser_peek_token (parser)->location;
13598 tree expr = c_parser_expr_no_commas (parser, NULL).value;
13599 expr = c_fully_fold (expr, false, NULL);
13601 /* If expr is an error_mark_node then the above function would have
13602 emitted an error. No reason to do it twice. */
13603 if (expr == error_mark_node)
13605 else if (!TREE_TYPE (expr)
13606 || !TREE_CONSTANT (expr)
13607 || !INTEGRAL_TYPE_P (TREE_TYPE (expr)))
13609 error_at (loc, "vectorlength must be an integer constant");
13610 else if (exact_log2 (TREE_INT_CST_LOW (expr)) == -1)
13611 error_at (loc, "vectorlength must be a power of 2");
13612 else
13614 if (is_simd_fn)
13616 tree u = build_omp_clause (loc, OMP_CLAUSE_SIMDLEN);
13617 OMP_CLAUSE_SIMDLEN_EXPR (u) = expr;
13618 OMP_CLAUSE_CHAIN (u) = clauses;
13619 clauses = u;
13621 else
13623 tree u = build_omp_clause (loc, OMP_CLAUSE_SAFELEN);
13624 OMP_CLAUSE_SAFELEN_EXPR (u) = expr;
13625 OMP_CLAUSE_CHAIN (u) = clauses;
13626 clauses = u;
13630 c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
13632 return clauses;
13635 /* Cilk Plus:
13636 linear ( simd-linear-variable-list )
13638 simd-linear-variable-list:
13639 simd-linear-variable
13640 simd-linear-variable-list , simd-linear-variable
13642 simd-linear-variable:
13643 id-expression
13644 id-expression : simd-linear-step
13646 simd-linear-step:
13647 conditional-expression */
13649 static tree
13650 c_parser_cilk_clause_linear (c_parser *parser, tree clauses)
13652 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
13653 return clauses;
13655 location_t loc = c_parser_peek_token (parser)->location;
13657 if (c_parser_next_token_is_not (parser, CPP_NAME)
13658 || c_parser_peek_token (parser)->id_kind != C_ID_ID)
13659 c_parser_error (parser, "expected identifier");
13661 while (c_parser_next_token_is (parser, CPP_NAME)
13662 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
13664 tree var = lookup_name (c_parser_peek_token (parser)->value);
13666 if (var == NULL)
13668 undeclared_variable (c_parser_peek_token (parser)->location,
13669 c_parser_peek_token (parser)->value);
13670 c_parser_consume_token (parser);
13672 else if (var == error_mark_node)
13673 c_parser_consume_token (parser);
13674 else
13676 tree step = integer_one_node;
13678 /* Parse the linear step if present. */
13679 if (c_parser_peek_2nd_token (parser)->type == CPP_COLON)
13681 c_parser_consume_token (parser);
13682 c_parser_consume_token (parser);
13684 tree expr = c_parser_expr_no_commas (parser, NULL).value;
13685 expr = c_fully_fold (expr, false, NULL);
13687 if (TREE_TYPE (expr)
13688 && INTEGRAL_TYPE_P (TREE_TYPE (expr))
13689 && (TREE_CONSTANT (expr)
13690 || DECL_P (expr)))
13691 step = expr;
13692 else
13693 c_parser_error (parser,
13694 "step size must be an integer constant "
13695 "expression or an integer variable");
13697 else
13698 c_parser_consume_token (parser);
13700 /* Use OMP_CLAUSE_LINEAR, which has the same semantics. */
13701 tree u = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
13702 OMP_CLAUSE_DECL (u) = var;
13703 OMP_CLAUSE_LINEAR_STEP (u) = step;
13704 OMP_CLAUSE_CHAIN (u) = clauses;
13705 clauses = u;
13708 if (c_parser_next_token_is_not (parser, CPP_COMMA))
13709 break;
13711 c_parser_consume_token (parser);
13714 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
13716 return clauses;
13719 /* Returns the name of the next clause. If the clause is not
13720 recognized SIMD_OMP_CLAUSE_NONE is returned and the next token is
13721 not consumed. Otherwise, the appropriate pragma_simd_clause is
13722 returned and the token is consumed. */
13724 static pragma_omp_clause
13725 c_parser_cilk_clause_name (c_parser *parser)
13727 pragma_omp_clause result;
13728 c_token *token = c_parser_peek_token (parser);
13730 if (!token->value || token->type != CPP_NAME)
13731 return PRAGMA_CILK_CLAUSE_NONE;
13733 const char *p = IDENTIFIER_POINTER (token->value);
13735 if (!strcmp (p, "vectorlength"))
13736 result = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
13737 else if (!strcmp (p, "linear"))
13738 result = PRAGMA_CILK_CLAUSE_LINEAR;
13739 else if (!strcmp (p, "private"))
13740 result = PRAGMA_CILK_CLAUSE_PRIVATE;
13741 else if (!strcmp (p, "firstprivate"))
13742 result = PRAGMA_CILK_CLAUSE_FIRSTPRIVATE;
13743 else if (!strcmp (p, "lastprivate"))
13744 result = PRAGMA_CILK_CLAUSE_LASTPRIVATE;
13745 else if (!strcmp (p, "reduction"))
13746 result = PRAGMA_CILK_CLAUSE_REDUCTION;
13747 else
13748 return PRAGMA_CILK_CLAUSE_NONE;
13750 c_parser_consume_token (parser);
13751 return result;
13754 /* Parse all #<pragma simd> clauses. Return the list of clauses
13755 found. */
13757 static tree
13758 c_parser_cilk_all_clauses (c_parser *parser)
13760 tree clauses = NULL;
13762 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
13764 pragma_omp_clause c_kind;
13766 c_kind = c_parser_cilk_clause_name (parser);
13768 switch (c_kind)
13770 case PRAGMA_CILK_CLAUSE_VECTORLENGTH:
13771 clauses = c_parser_cilk_clause_vectorlength (parser, clauses, false);
13772 break;
13773 case PRAGMA_CILK_CLAUSE_LINEAR:
13774 clauses = c_parser_cilk_clause_linear (parser, clauses);
13775 break;
13776 case PRAGMA_CILK_CLAUSE_PRIVATE:
13777 /* Use the OpenMP counterpart. */
13778 clauses = c_parser_omp_clause_private (parser, clauses);
13779 break;
13780 case PRAGMA_CILK_CLAUSE_FIRSTPRIVATE:
13781 /* Use the OpenMP counterpart. */
13782 clauses = c_parser_omp_clause_firstprivate (parser, clauses);
13783 break;
13784 case PRAGMA_CILK_CLAUSE_LASTPRIVATE:
13785 /* Use the OpenMP counterpart. */
13786 clauses = c_parser_omp_clause_lastprivate (parser, clauses);
13787 break;
13788 case PRAGMA_CILK_CLAUSE_REDUCTION:
13789 /* Use the OpenMP counterpart. */
13790 clauses = c_parser_omp_clause_reduction (parser, clauses);
13791 break;
13792 default:
13793 c_parser_error (parser, "expected %<#pragma simd%> clause");
13794 goto saw_error;
13798 saw_error:
13799 c_parser_skip_to_pragma_eol (parser);
13800 return c_finish_cilk_clauses (clauses);
13803 /* Main entry point for parsing Cilk Plus <#pragma simd> for
13804 loops. */
13806 static void
13807 c_parser_cilk_simd (c_parser *parser)
13809 tree clauses = c_parser_cilk_all_clauses (parser);
13810 tree block = c_begin_compound_stmt (true);
13811 location_t loc = c_parser_peek_token (parser)->location;
13812 c_parser_omp_for_loop (loc, parser, CILK_SIMD, clauses, NULL);
13813 block = c_end_compound_stmt (loc, block, true);
13814 add_stmt (block);
13817 /* Parse a transaction attribute (GCC Extension).
13819 transaction-attribute:
13820 attributes
13821 [ [ any-word ] ]
13823 The transactional memory language description is written for C++,
13824 and uses the C++0x attribute syntax. For compatibility, allow the
13825 bracket style for transactions in C as well. */
13827 static tree
13828 c_parser_transaction_attributes (c_parser *parser)
13830 tree attr_name, attr = NULL;
13832 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
13833 return c_parser_attributes (parser);
13835 if (!c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
13836 return NULL_TREE;
13837 c_parser_consume_token (parser);
13838 if (!c_parser_require (parser, CPP_OPEN_SQUARE, "expected %<[%>"))
13839 goto error1;
13841 attr_name = c_parser_attribute_any_word (parser);
13842 if (attr_name)
13844 c_parser_consume_token (parser);
13845 attr = build_tree_list (attr_name, NULL_TREE);
13847 else
13848 c_parser_error (parser, "expected identifier");
13850 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, "expected %<]%>");
13851 error1:
13852 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, "expected %<]%>");
13853 return attr;
13856 /* Parse a __transaction_atomic or __transaction_relaxed statement
13857 (GCC Extension).
13859 transaction-statement:
13860 __transaction_atomic transaction-attribute[opt] compound-statement
13861 __transaction_relaxed compound-statement
13863 Note that the only valid attribute is: "outer".
13866 static tree
13867 c_parser_transaction (c_parser *parser, enum rid keyword)
13869 unsigned int old_in = parser->in_transaction;
13870 unsigned int this_in = 1, new_in;
13871 location_t loc = c_parser_peek_token (parser)->location;
13872 tree stmt, attrs;
13874 gcc_assert ((keyword == RID_TRANSACTION_ATOMIC
13875 || keyword == RID_TRANSACTION_RELAXED)
13876 && c_parser_next_token_is_keyword (parser, keyword));
13877 c_parser_consume_token (parser);
13879 if (keyword == RID_TRANSACTION_RELAXED)
13880 this_in |= TM_STMT_ATTR_RELAXED;
13881 else
13883 attrs = c_parser_transaction_attributes (parser);
13884 if (attrs)
13885 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
13888 /* Keep track if we're in the lexical scope of an outer transaction. */
13889 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
13891 parser->in_transaction = new_in;
13892 stmt = c_parser_compound_statement (parser);
13893 parser->in_transaction = old_in;
13895 if (flag_tm)
13896 stmt = c_finish_transaction (loc, stmt, this_in);
13897 else
13898 error_at (loc, (keyword == RID_TRANSACTION_ATOMIC ?
13899 "%<__transaction_atomic%> without transactional memory support enabled"
13900 : "%<__transaction_relaxed %> "
13901 "without transactional memory support enabled"));
13903 return stmt;
13906 /* Parse a __transaction_atomic or __transaction_relaxed expression
13907 (GCC Extension).
13909 transaction-expression:
13910 __transaction_atomic ( expression )
13911 __transaction_relaxed ( expression )
13914 static struct c_expr
13915 c_parser_transaction_expression (c_parser *parser, enum rid keyword)
13917 struct c_expr ret;
13918 unsigned int old_in = parser->in_transaction;
13919 unsigned int this_in = 1;
13920 location_t loc = c_parser_peek_token (parser)->location;
13921 tree attrs;
13923 gcc_assert ((keyword == RID_TRANSACTION_ATOMIC
13924 || keyword == RID_TRANSACTION_RELAXED)
13925 && c_parser_next_token_is_keyword (parser, keyword));
13926 c_parser_consume_token (parser);
13928 if (keyword == RID_TRANSACTION_RELAXED)
13929 this_in |= TM_STMT_ATTR_RELAXED;
13930 else
13932 attrs = c_parser_transaction_attributes (parser);
13933 if (attrs)
13934 this_in |= parse_tm_stmt_attr (attrs, 0);
13937 parser->in_transaction = this_in;
13938 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
13940 tree expr = c_parser_expression (parser).value;
13941 ret.original_type = TREE_TYPE (expr);
13942 ret.value = build1 (TRANSACTION_EXPR, ret.original_type, expr);
13943 if (this_in & TM_STMT_ATTR_RELAXED)
13944 TRANSACTION_EXPR_RELAXED (ret.value) = 1;
13945 SET_EXPR_LOCATION (ret.value, loc);
13946 ret.original_code = TRANSACTION_EXPR;
13947 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
13949 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
13950 goto error;
13953 else
13955 error:
13956 ret.value = error_mark_node;
13957 ret.original_code = ERROR_MARK;
13958 ret.original_type = NULL;
13960 parser->in_transaction = old_in;
13962 if (!flag_tm)
13963 error_at (loc, (keyword == RID_TRANSACTION_ATOMIC ?
13964 "%<__transaction_atomic%> without transactional memory support enabled"
13965 : "%<__transaction_relaxed %> "
13966 "without transactional memory support enabled"));
13968 return ret;
13971 /* Parse a __transaction_cancel statement (GCC Extension).
13973 transaction-cancel-statement:
13974 __transaction_cancel transaction-attribute[opt] ;
13976 Note that the only valid attribute is "outer".
13979 static tree
13980 c_parser_transaction_cancel (c_parser *parser)
13982 location_t loc = c_parser_peek_token (parser)->location;
13983 tree attrs;
13984 bool is_outer = false;
13986 gcc_assert (c_parser_next_token_is_keyword (parser, RID_TRANSACTION_CANCEL));
13987 c_parser_consume_token (parser);
13989 attrs = c_parser_transaction_attributes (parser);
13990 if (attrs)
13991 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
13993 if (!flag_tm)
13995 error_at (loc, "%<__transaction_cancel%> without "
13996 "transactional memory support enabled");
13997 goto ret_error;
13999 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
14001 error_at (loc, "%<__transaction_cancel%> within a "
14002 "%<__transaction_relaxed%>");
14003 goto ret_error;
14005 else if (is_outer)
14007 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
14008 && !is_tm_may_cancel_outer (current_function_decl))
14010 error_at (loc, "outer %<__transaction_cancel%> not "
14011 "within outer %<__transaction_atomic%>");
14012 error_at (loc, " or a %<transaction_may_cancel_outer%> function");
14013 goto ret_error;
14016 else if (parser->in_transaction == 0)
14018 error_at (loc, "%<__transaction_cancel%> not within "
14019 "%<__transaction_atomic%>");
14020 goto ret_error;
14023 return add_stmt (build_tm_abort_call (loc, is_outer));
14025 ret_error:
14026 return build1 (NOP_EXPR, void_type_node, error_mark_node);
14029 /* Parse a single source file. */
14031 void
14032 c_parse_file (void)
14034 /* Use local storage to begin. If the first token is a pragma, parse it.
14035 If it is #pragma GCC pch_preprocess, then this will load a PCH file
14036 which will cause garbage collection. */
14037 c_parser tparser;
14039 memset (&tparser, 0, sizeof tparser);
14040 tparser.tokens = &tparser.tokens_buf[0];
14041 the_parser = &tparser;
14043 if (c_parser_peek_token (&tparser)->pragma_kind == PRAGMA_GCC_PCH_PREPROCESS)
14044 c_parser_pragma_pch_preprocess (&tparser);
14046 the_parser = ggc_alloc_c_parser ();
14047 *the_parser = tparser;
14048 if (tparser.tokens == &tparser.tokens_buf[0])
14049 the_parser->tokens = &the_parser->tokens_buf[0];
14051 /* Initialize EH, if we've been told to do so. */
14052 if (flag_exceptions)
14053 using_eh_for_cleanups ();
14055 c_parser_translation_unit (the_parser);
14056 the_parser = NULL;
14059 /* This function parses Cilk Plus array notation. The starting index is
14060 passed in INITIAL_INDEX and the array name is passes in ARRAY_VALUE. The
14061 return value of this function is a tree_node called VALUE_TREE of type
14062 ARRAY_NOTATION_REF. */
14064 static tree
14065 c_parser_array_notation (location_t loc, c_parser *parser, tree initial_index,
14066 tree array_value)
14068 c_token *token = NULL;
14069 tree start_index = NULL_TREE, end_index = NULL_TREE, stride = NULL_TREE;
14070 tree value_tree = NULL_TREE, type = NULL_TREE, array_type = NULL_TREE;
14071 tree array_type_domain = NULL_TREE;
14073 if (array_value == error_mark_node || initial_index == error_mark_node)
14075 /* No need to continue. If either of these 2 were true, then an error
14076 must be emitted already. Thus, no need to emit them twice. */
14077 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
14078 return error_mark_node;
14081 array_type = TREE_TYPE (array_value);
14082 gcc_assert (array_type);
14083 if (TREE_CODE (array_type) != ARRAY_TYPE
14084 && TREE_CODE (array_type) != POINTER_TYPE)
14086 error_at (loc, "base of array section must be pointer or array type");
14087 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
14088 return error_mark_node;
14090 type = TREE_TYPE (array_type);
14091 token = c_parser_peek_token (parser);
14093 if (token->type == CPP_EOF)
14095 c_parser_error (parser, "expected %<:%> or numeral");
14096 return value_tree;
14098 else if (token->type == CPP_COLON)
14100 if (!initial_index)
14102 /* If we are here, then we have a case like this A[:]. */
14103 c_parser_consume_token (parser);
14104 if (TREE_CODE (array_type) == POINTER_TYPE)
14106 error_at (loc, "start-index and length fields necessary for "
14107 "using array notations in pointers");
14108 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
14109 return error_mark_node;
14111 if (TREE_CODE (array_type) == FUNCTION_TYPE)
14113 error_at (loc, "array notations cannot be used with function "
14114 "type");
14115 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
14116 return error_mark_node;
14118 array_type_domain = TYPE_DOMAIN (array_type);
14120 if (!array_type_domain)
14122 error_at (loc, "start-index and length fields necessary for "
14123 "using array notations in dimensionless arrays");
14124 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
14125 return error_mark_node;
14128 start_index = TYPE_MINVAL (array_type_domain);
14129 start_index = fold_build1 (CONVERT_EXPR, ptrdiff_type_node,
14130 start_index);
14131 if (!TYPE_MAXVAL (array_type_domain)
14132 || !TREE_CONSTANT (TYPE_MAXVAL (array_type_domain)))
14134 error_at (loc, "start-index and length fields necessary for "
14135 "using array notations in variable-length arrays");
14136 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
14137 return error_mark_node;
14139 end_index = TYPE_MAXVAL (array_type_domain);
14140 end_index = fold_build2 (PLUS_EXPR, TREE_TYPE (end_index),
14141 end_index, integer_one_node);
14142 end_index = fold_build1 (CONVERT_EXPR, ptrdiff_type_node, end_index);
14143 stride = build_int_cst (integer_type_node, 1);
14144 stride = fold_build1 (CONVERT_EXPR, ptrdiff_type_node, stride);
14146 else if (initial_index != error_mark_node)
14148 /* If we are here, then there should be 2 possibilities:
14149 1. Array [EXPR : EXPR]
14150 2. Array [EXPR : EXPR : EXPR]
14152 start_index = initial_index;
14154 if (TREE_CODE (array_type) == FUNCTION_TYPE)
14156 error_at (loc, "array notations cannot be used with function "
14157 "type");
14158 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
14159 return error_mark_node;
14161 c_parser_consume_token (parser); /* consume the ':' */
14162 struct c_expr ce = c_parser_expression (parser);
14163 ce = convert_lvalue_to_rvalue (loc, ce, false, false);
14164 end_index = ce.value;
14165 if (!end_index || end_index == error_mark_node)
14167 c_parser_skip_to_end_of_block_or_statement (parser);
14168 return error_mark_node;
14170 if (c_parser_peek_token (parser)->type == CPP_COLON)
14172 c_parser_consume_token (parser);
14173 ce = c_parser_expression (parser);
14174 ce = convert_lvalue_to_rvalue (loc, ce, false, false);
14175 stride = ce.value;
14176 if (!stride || stride == error_mark_node)
14178 c_parser_skip_to_end_of_block_or_statement (parser);
14179 return error_mark_node;
14183 else
14184 c_parser_error (parser, "expected array notation expression");
14186 else
14187 c_parser_error (parser, "expected array notation expression");
14189 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, "expected %<]%>");
14191 value_tree = build_array_notation_ref (loc, array_value, start_index,
14192 end_index, stride, type);
14193 if (value_tree != error_mark_node)
14194 SET_EXPR_LOCATION (value_tree, loc);
14195 return value_tree;
14198 #include "gt-c-c-parser.h"