Fix copy'n'pasto.
[official-gcc.git] / gcc / c / c-parser.c
bloba7e33b01f57d02b1dfc9688ac7915c4fa6ddaca7
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_cleared_vec_alloc<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))
1692 postfix_attrs = c_parser_attributes (parser);
1693 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
1695 /* This means there is an attribute specifier after
1696 the declarator in a function definition. Provide
1697 some more information for the user. */
1698 error_at (here, "attributes should be specified before the "
1699 "declarator in a function definition");
1700 c_parser_skip_to_end_of_block_or_statement (parser);
1701 return;
1704 if (c_parser_next_token_is (parser, CPP_EQ))
1706 tree d;
1707 struct c_expr init;
1708 location_t init_loc;
1709 c_parser_consume_token (parser);
1710 if (auto_type_p)
1712 start_init (NULL_TREE, asm_name, global_bindings_p ());
1713 init_loc = c_parser_peek_token (parser)->location;
1714 init = c_parser_expr_no_commas (parser, NULL);
1715 if (TREE_CODE (init.value) == COMPONENT_REF
1716 && DECL_C_BIT_FIELD (TREE_OPERAND (init.value, 1)))
1717 error_at (here,
1718 "%<__auto_type%> used with a bit-field"
1719 " initializer");
1720 init = convert_lvalue_to_rvalue (init_loc, init, true, true);
1721 tree init_type = TREE_TYPE (init.value);
1722 /* As with typeof, remove _Atomic and const
1723 qualifiers from atomic types. */
1724 if (init_type != error_mark_node && TYPE_ATOMIC (init_type))
1725 init_type
1726 = c_build_qualified_type (init_type,
1727 (TYPE_QUALS (init_type)
1728 & ~(TYPE_QUAL_ATOMIC
1729 | TYPE_QUAL_CONST)));
1730 bool vm_type = variably_modified_type_p (init_type,
1731 NULL_TREE);
1732 if (vm_type)
1733 init.value = c_save_expr (init.value);
1734 finish_init ();
1735 specs->typespec_kind = ctsk_typeof;
1736 specs->locations[cdw_typedef] = init_loc;
1737 specs->typedef_p = true;
1738 specs->type = init_type;
1739 if (vm_type)
1741 bool maybe_const = true;
1742 tree type_expr = c_fully_fold (init.value, false,
1743 &maybe_const);
1744 specs->expr_const_operands &= maybe_const;
1745 if (specs->expr)
1746 specs->expr = build2 (COMPOUND_EXPR,
1747 TREE_TYPE (type_expr),
1748 specs->expr, type_expr);
1749 else
1750 specs->expr = type_expr;
1752 d = start_decl (declarator, specs, true,
1753 chainon (postfix_attrs, all_prefix_attrs));
1754 if (!d)
1755 d = error_mark_node;
1756 if (omp_declare_simd_clauses.exists ()
1757 || !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
1758 c_finish_omp_declare_simd (parser, d, NULL_TREE,
1759 omp_declare_simd_clauses);
1761 else
1763 /* The declaration of the variable is in effect while
1764 its initializer is parsed. */
1765 d = start_decl (declarator, specs, true,
1766 chainon (postfix_attrs, all_prefix_attrs));
1767 if (!d)
1768 d = error_mark_node;
1769 if (omp_declare_simd_clauses.exists ()
1770 || !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
1771 c_finish_omp_declare_simd (parser, d, NULL_TREE,
1772 omp_declare_simd_clauses);
1773 start_init (d, asm_name, global_bindings_p ());
1774 init_loc = c_parser_peek_token (parser)->location;
1775 init = c_parser_initializer (parser);
1776 finish_init ();
1778 if (d != error_mark_node)
1780 maybe_warn_string_init (init_loc, TREE_TYPE (d), init);
1781 finish_decl (d, init_loc, init.value,
1782 init.original_type, asm_name);
1785 else
1787 if (auto_type_p)
1789 error_at (here,
1790 "%<__auto_type%> requires an initialized "
1791 "data declaration");
1792 c_parser_skip_to_end_of_block_or_statement (parser);
1793 return;
1795 tree d = start_decl (declarator, specs, false,
1796 chainon (postfix_attrs,
1797 all_prefix_attrs));
1798 if (omp_declare_simd_clauses.exists ()
1799 || !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
1801 tree parms = NULL_TREE;
1802 if (d && TREE_CODE (d) == FUNCTION_DECL)
1804 struct c_declarator *ce = declarator;
1805 while (ce != NULL)
1806 if (ce->kind == cdk_function)
1808 parms = ce->u.arg_info->parms;
1809 break;
1811 else
1812 ce = ce->declarator;
1814 if (parms)
1815 temp_store_parm_decls (d, parms);
1816 c_finish_omp_declare_simd (parser, d, parms,
1817 omp_declare_simd_clauses);
1818 if (parms)
1819 temp_pop_parm_decls ();
1821 if (d)
1822 finish_decl (d, UNKNOWN_LOCATION, NULL_TREE,
1823 NULL_TREE, asm_name);
1825 if (c_parser_next_token_is_keyword (parser, RID_IN))
1827 if (d)
1828 *objc_foreach_object_declaration = d;
1829 else
1830 *objc_foreach_object_declaration = error_mark_node;
1833 if (c_parser_next_token_is (parser, CPP_COMMA))
1835 if (auto_type_p)
1837 error_at (here,
1838 "%<__auto_type%> may only be used with"
1839 " a single declarator");
1840 c_parser_skip_to_end_of_block_or_statement (parser);
1841 return;
1843 c_parser_consume_token (parser);
1844 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
1845 all_prefix_attrs = chainon (c_parser_attributes (parser),
1846 prefix_attrs);
1847 else
1848 all_prefix_attrs = prefix_attrs;
1849 continue;
1851 else if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1853 c_parser_consume_token (parser);
1854 return;
1856 else if (c_parser_next_token_is_keyword (parser, RID_IN))
1858 /* This can only happen in Objective-C: we found the
1859 'in' that terminates the declaration inside an
1860 Objective-C foreach statement. Do not consume the
1861 token, so that the caller can use it to determine
1862 that this indeed is a foreach context. */
1863 return;
1865 else
1867 c_parser_error (parser, "expected %<,%> or %<;%>");
1868 c_parser_skip_to_end_of_block_or_statement (parser);
1869 return;
1872 else if (auto_type_p)
1874 error_at (here,
1875 "%<__auto_type%> requires an initialized data declaration");
1876 c_parser_skip_to_end_of_block_or_statement (parser);
1877 return;
1879 else if (!fndef_ok)
1881 c_parser_error (parser, "expected %<=%>, %<,%>, %<;%>, "
1882 "%<asm%> or %<__attribute__%>");
1883 c_parser_skip_to_end_of_block_or_statement (parser);
1884 return;
1886 /* Function definition (nested or otherwise). */
1887 if (nested)
1889 pedwarn (here, OPT_Wpedantic, "ISO C forbids nested functions");
1890 c_push_function_context ();
1892 if (!start_function (specs, declarator, all_prefix_attrs))
1894 /* This can appear in many cases looking nothing like a
1895 function definition, so we don't give a more specific
1896 error suggesting there was one. */
1897 c_parser_error (parser, "expected %<=%>, %<,%>, %<;%>, %<asm%> "
1898 "or %<__attribute__%>");
1899 if (nested)
1900 c_pop_function_context ();
1901 break;
1904 if (DECL_DECLARED_INLINE_P (current_function_decl))
1905 tv = TV_PARSE_INLINE;
1906 else
1907 tv = TV_PARSE_FUNC;
1908 timevar_push (tv);
1910 /* Parse old-style parameter declarations. ??? Attributes are
1911 not allowed to start declaration specifiers here because of a
1912 syntax conflict between a function declaration with attribute
1913 suffix and a function definition with an attribute prefix on
1914 first old-style parameter declaration. Following the old
1915 parser, they are not accepted on subsequent old-style
1916 parameter declarations either. However, there is no
1917 ambiguity after the first declaration, nor indeed on the
1918 first as long as we don't allow postfix attributes after a
1919 declarator with a nonempty identifier list in a definition;
1920 and postfix attributes have never been accepted here in
1921 function definitions either. */
1922 while (c_parser_next_token_is_not (parser, CPP_EOF)
1923 && c_parser_next_token_is_not (parser, CPP_OPEN_BRACE))
1924 c_parser_declaration_or_fndef (parser, false, false, false,
1925 true, false, NULL, vNULL);
1926 store_parm_decls ();
1927 if (omp_declare_simd_clauses.exists ()
1928 || !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
1929 c_finish_omp_declare_simd (parser, current_function_decl, NULL_TREE,
1930 omp_declare_simd_clauses);
1931 DECL_STRUCT_FUNCTION (current_function_decl)->function_start_locus
1932 = c_parser_peek_token (parser)->location;
1933 fnbody = c_parser_compound_statement (parser);
1934 if (flag_cilkplus && contains_array_notation_expr (fnbody))
1935 fnbody = expand_array_notation_exprs (fnbody);
1936 if (nested)
1938 tree decl = current_function_decl;
1939 /* Mark nested functions as needing static-chain initially.
1940 lower_nested_functions will recompute it but the
1941 DECL_STATIC_CHAIN flag is also used before that happens,
1942 by initializer_constant_valid_p. See gcc.dg/nested-fn-2.c. */
1943 DECL_STATIC_CHAIN (decl) = 1;
1944 add_stmt (fnbody);
1945 finish_function ();
1946 c_pop_function_context ();
1947 add_stmt (build_stmt (DECL_SOURCE_LOCATION (decl), DECL_EXPR, decl));
1949 else
1951 add_stmt (fnbody);
1952 finish_function ();
1955 timevar_pop (tv);
1956 break;
1960 /* Parse an asm-definition (asm() outside a function body). This is a
1961 GNU extension.
1963 asm-definition:
1964 simple-asm-expr ;
1967 static void
1968 c_parser_asm_definition (c_parser *parser)
1970 tree asm_str = c_parser_simple_asm_expr (parser);
1971 if (asm_str)
1972 add_asm_node (asm_str);
1973 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
1976 /* Parse a static assertion (C11 6.7.10).
1978 static_assert-declaration:
1979 static_assert-declaration-no-semi ;
1982 static void
1983 c_parser_static_assert_declaration (c_parser *parser)
1985 c_parser_static_assert_declaration_no_semi (parser);
1986 if (parser->error
1987 || !c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
1988 c_parser_skip_to_end_of_block_or_statement (parser);
1991 /* Parse a static assertion (C11 6.7.10), without the trailing
1992 semicolon.
1994 static_assert-declaration-no-semi:
1995 _Static_assert ( constant-expression , string-literal )
1998 static void
1999 c_parser_static_assert_declaration_no_semi (c_parser *parser)
2001 location_t assert_loc, value_loc;
2002 tree value;
2003 tree string;
2005 gcc_assert (c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT));
2006 assert_loc = c_parser_peek_token (parser)->location;
2007 if (!flag_isoc11)
2009 if (flag_isoc99)
2010 pedwarn (assert_loc, OPT_Wpedantic,
2011 "ISO C99 does not support %<_Static_assert%>");
2012 else
2013 pedwarn (assert_loc, OPT_Wpedantic,
2014 "ISO C90 does not support %<_Static_assert%>");
2016 c_parser_consume_token (parser);
2017 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2018 return;
2019 value_loc = c_parser_peek_token (parser)->location;
2020 value = c_parser_expr_no_commas (parser, NULL).value;
2021 parser->lex_untranslated_string = true;
2022 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
2024 parser->lex_untranslated_string = false;
2025 return;
2027 switch (c_parser_peek_token (parser)->type)
2029 case CPP_STRING:
2030 case CPP_STRING16:
2031 case CPP_STRING32:
2032 case CPP_WSTRING:
2033 case CPP_UTF8STRING:
2034 string = c_parser_peek_token (parser)->value;
2035 c_parser_consume_token (parser);
2036 parser->lex_untranslated_string = false;
2037 break;
2038 default:
2039 c_parser_error (parser, "expected string literal");
2040 parser->lex_untranslated_string = false;
2041 return;
2043 c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
2045 if (!INTEGRAL_TYPE_P (TREE_TYPE (value)))
2047 error_at (value_loc, "expression in static assertion is not an integer");
2048 return;
2050 if (TREE_CODE (value) != INTEGER_CST)
2052 value = c_fully_fold (value, false, NULL);
2053 if (TREE_CODE (value) == INTEGER_CST)
2054 pedwarn (value_loc, OPT_Wpedantic, "expression in static assertion "
2055 "is not an integer constant expression");
2057 if (TREE_CODE (value) != INTEGER_CST)
2059 error_at (value_loc, "expression in static assertion is not constant");
2060 return;
2062 constant_expression_warning (value);
2063 if (integer_zerop (value))
2064 error_at (assert_loc, "static assertion failed: %E", string);
2067 /* Parse some declaration specifiers (possibly none) (C90 6.5, C99
2068 6.7), adding them to SPECS (which may already include some).
2069 Storage class specifiers are accepted iff SCSPEC_OK; type
2070 specifiers are accepted iff TYPESPEC_OK; alignment specifiers are
2071 accepted iff ALIGNSPEC_OK; attributes are accepted at the start
2072 iff START_ATTR_OK; __auto_type is accepted iff AUTO_TYPE_OK.
2074 declaration-specifiers:
2075 storage-class-specifier declaration-specifiers[opt]
2076 type-specifier declaration-specifiers[opt]
2077 type-qualifier declaration-specifiers[opt]
2078 function-specifier declaration-specifiers[opt]
2079 alignment-specifier declaration-specifiers[opt]
2081 Function specifiers (inline) are from C99, and are currently
2082 handled as storage class specifiers, as is __thread. Alignment
2083 specifiers are from C11.
2085 C90 6.5.1, C99 6.7.1:
2086 storage-class-specifier:
2087 typedef
2088 extern
2089 static
2090 auto
2091 register
2092 _Thread_local
2094 (_Thread_local is new in C11.)
2096 C99 6.7.4:
2097 function-specifier:
2098 inline
2099 _Noreturn
2101 (_Noreturn is new in C11.)
2103 C90 6.5.2, C99 6.7.2:
2104 type-specifier:
2105 void
2106 char
2107 short
2109 long
2110 float
2111 double
2112 signed
2113 unsigned
2114 _Bool
2115 _Complex
2116 [_Imaginary removed in C99 TC2]
2117 struct-or-union-specifier
2118 enum-specifier
2119 typedef-name
2120 atomic-type-specifier
2122 (_Bool and _Complex are new in C99.)
2123 (atomic-type-specifier is new in C11.)
2125 C90 6.5.3, C99 6.7.3:
2127 type-qualifier:
2128 const
2129 restrict
2130 volatile
2131 address-space-qualifier
2132 _Atomic
2134 (restrict is new in C99.)
2135 (_Atomic is new in C11.)
2137 GNU extensions:
2139 declaration-specifiers:
2140 attributes declaration-specifiers[opt]
2142 type-qualifier:
2143 address-space
2145 address-space:
2146 identifier recognized by the target
2148 storage-class-specifier:
2149 __thread
2151 type-specifier:
2152 typeof-specifier
2153 __auto_type
2154 __int128
2155 _Decimal32
2156 _Decimal64
2157 _Decimal128
2158 _Fract
2159 _Accum
2160 _Sat
2162 (_Fract, _Accum, and _Sat are new from ISO/IEC DTR 18037:
2163 http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1169.pdf)
2165 atomic-type-specifier
2166 _Atomic ( type-name )
2168 Objective-C:
2170 type-specifier:
2171 class-name objc-protocol-refs[opt]
2172 typedef-name objc-protocol-refs
2173 objc-protocol-refs
2176 static void
2177 c_parser_declspecs (c_parser *parser, struct c_declspecs *specs,
2178 bool scspec_ok, bool typespec_ok, bool start_attr_ok,
2179 bool alignspec_ok, bool auto_type_ok,
2180 enum c_lookahead_kind la)
2182 bool attrs_ok = start_attr_ok;
2183 bool seen_type = specs->typespec_kind != ctsk_none;
2185 if (!typespec_ok)
2186 gcc_assert (la == cla_prefer_id);
2188 while (c_parser_next_token_is (parser, CPP_NAME)
2189 || c_parser_next_token_is (parser, CPP_KEYWORD)
2190 || (c_dialect_objc () && c_parser_next_token_is (parser, CPP_LESS)))
2192 struct c_typespec t;
2193 tree attrs;
2194 tree align;
2195 location_t loc = c_parser_peek_token (parser)->location;
2197 /* If we cannot accept a type, exit if the next token must start
2198 one. Also, if we already have seen a tagged definition,
2199 a typename would be an error anyway and likely the user
2200 has simply forgotten a semicolon, so we exit. */
2201 if ((!typespec_ok || specs->typespec_kind == ctsk_tagdef)
2202 && c_parser_next_tokens_start_typename (parser, la)
2203 && !c_parser_next_token_is_qualifier (parser))
2204 break;
2206 if (c_parser_next_token_is (parser, CPP_NAME))
2208 c_token *name_token = c_parser_peek_token (parser);
2209 tree value = name_token->value;
2210 c_id_kind kind = name_token->id_kind;
2212 if (kind == C_ID_ADDRSPACE)
2214 addr_space_t as
2215 = name_token->keyword - RID_FIRST_ADDR_SPACE;
2216 declspecs_add_addrspace (name_token->location, specs, as);
2217 c_parser_consume_token (parser);
2218 attrs_ok = true;
2219 continue;
2222 gcc_assert (!c_parser_next_token_is_qualifier (parser));
2224 /* If we cannot accept a type, and the next token must start one,
2225 exit. Do the same if we already have seen a tagged definition,
2226 since it would be an error anyway and likely the user has simply
2227 forgotten a semicolon. */
2228 if (seen_type || !c_parser_next_tokens_start_typename (parser, la))
2229 break;
2231 /* Now at an unknown typename (C_ID_ID), a C_ID_TYPENAME or
2232 a C_ID_CLASSNAME. */
2233 c_parser_consume_token (parser);
2234 seen_type = true;
2235 attrs_ok = true;
2236 if (kind == C_ID_ID)
2238 error_at (loc, "unknown type name %qE", value);
2239 t.kind = ctsk_typedef;
2240 t.spec = error_mark_node;
2242 else if (kind == C_ID_TYPENAME
2243 && (!c_dialect_objc ()
2244 || c_parser_next_token_is_not (parser, CPP_LESS)))
2246 t.kind = ctsk_typedef;
2247 /* For a typedef name, record the meaning, not the name.
2248 In case of 'foo foo, bar;'. */
2249 t.spec = lookup_name (value);
2251 else
2253 tree proto = NULL_TREE;
2254 gcc_assert (c_dialect_objc ());
2255 t.kind = ctsk_objc;
2256 if (c_parser_next_token_is (parser, CPP_LESS))
2257 proto = c_parser_objc_protocol_refs (parser);
2258 t.spec = objc_get_protocol_qualified_type (value, proto);
2260 t.expr = NULL_TREE;
2261 t.expr_const_operands = true;
2262 declspecs_add_type (name_token->location, specs, t);
2263 continue;
2265 if (c_parser_next_token_is (parser, CPP_LESS))
2267 /* Make "<SomeProtocol>" equivalent to "id <SomeProtocol>" -
2268 nisse@lysator.liu.se. */
2269 tree proto;
2270 gcc_assert (c_dialect_objc ());
2271 if (!typespec_ok || seen_type)
2272 break;
2273 proto = c_parser_objc_protocol_refs (parser);
2274 t.kind = ctsk_objc;
2275 t.spec = objc_get_protocol_qualified_type (NULL_TREE, proto);
2276 t.expr = NULL_TREE;
2277 t.expr_const_operands = true;
2278 declspecs_add_type (loc, specs, t);
2279 continue;
2281 gcc_assert (c_parser_next_token_is (parser, CPP_KEYWORD));
2282 switch (c_parser_peek_token (parser)->keyword)
2284 case RID_STATIC:
2285 case RID_EXTERN:
2286 case RID_REGISTER:
2287 case RID_TYPEDEF:
2288 case RID_INLINE:
2289 case RID_NORETURN:
2290 case RID_AUTO:
2291 case RID_THREAD:
2292 if (!scspec_ok)
2293 goto out;
2294 attrs_ok = true;
2295 /* TODO: Distinguish between function specifiers (inline, noreturn)
2296 and storage class specifiers, either here or in
2297 declspecs_add_scspec. */
2298 declspecs_add_scspec (loc, specs,
2299 c_parser_peek_token (parser)->value);
2300 c_parser_consume_token (parser);
2301 break;
2302 case RID_AUTO_TYPE:
2303 if (!auto_type_ok)
2304 goto out;
2305 /* Fall through. */
2306 case RID_UNSIGNED:
2307 case RID_LONG:
2308 case RID_INT128:
2309 case RID_SHORT:
2310 case RID_SIGNED:
2311 case RID_COMPLEX:
2312 case RID_INT:
2313 case RID_CHAR:
2314 case RID_FLOAT:
2315 case RID_DOUBLE:
2316 case RID_VOID:
2317 case RID_DFLOAT32:
2318 case RID_DFLOAT64:
2319 case RID_DFLOAT128:
2320 case RID_BOOL:
2321 case RID_FRACT:
2322 case RID_ACCUM:
2323 case RID_SAT:
2324 if (!typespec_ok)
2325 goto out;
2326 attrs_ok = true;
2327 seen_type = true;
2328 if (c_dialect_objc ())
2329 parser->objc_need_raw_identifier = true;
2330 t.kind = ctsk_resword;
2331 t.spec = c_parser_peek_token (parser)->value;
2332 t.expr = NULL_TREE;
2333 t.expr_const_operands = true;
2334 declspecs_add_type (loc, specs, t);
2335 c_parser_consume_token (parser);
2336 break;
2337 case RID_ENUM:
2338 if (!typespec_ok)
2339 goto out;
2340 attrs_ok = true;
2341 seen_type = true;
2342 t = c_parser_enum_specifier (parser);
2343 declspecs_add_type (loc, specs, t);
2344 break;
2345 case RID_STRUCT:
2346 case RID_UNION:
2347 if (!typespec_ok)
2348 goto out;
2349 attrs_ok = true;
2350 seen_type = true;
2351 t = c_parser_struct_or_union_specifier (parser);
2352 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, t.spec);
2353 declspecs_add_type (loc, specs, t);
2354 break;
2355 case RID_TYPEOF:
2356 /* ??? The old parser rejected typeof after other type
2357 specifiers, but is a syntax error the best way of
2358 handling this? */
2359 if (!typespec_ok || seen_type)
2360 goto out;
2361 attrs_ok = true;
2362 seen_type = true;
2363 t = c_parser_typeof_specifier (parser);
2364 declspecs_add_type (loc, specs, t);
2365 break;
2366 case RID_ATOMIC:
2367 /* C parser handling of Objective-C constructs needs
2368 checking for correct lvalue-to-rvalue conversions, and
2369 the code in build_modify_expr handling various
2370 Objective-C cases, and that in build_unary_op handling
2371 Objective-C cases for increment / decrement, also needs
2372 updating; uses of TYPE_MAIN_VARIANT in objc_compare_types
2373 and objc_types_are_equivalent may also need updates. */
2374 if (c_dialect_objc ())
2375 sorry ("%<_Atomic%> in Objective-C");
2376 /* C parser handling of OpenMP constructs needs checking for
2377 correct lvalue-to-rvalue conversions. */
2378 if (flag_openmp)
2379 sorry ("%<_Atomic%> with OpenMP");
2380 if (!flag_isoc11)
2382 if (flag_isoc99)
2383 pedwarn (loc, OPT_Wpedantic,
2384 "ISO C99 does not support the %<_Atomic%> qualifier");
2385 else
2386 pedwarn (loc, OPT_Wpedantic,
2387 "ISO C90 does not support the %<_Atomic%> qualifier");
2389 attrs_ok = true;
2390 tree value;
2391 value = c_parser_peek_token (parser)->value;
2392 c_parser_consume_token (parser);
2393 if (typespec_ok && c_parser_next_token_is (parser, CPP_OPEN_PAREN))
2395 /* _Atomic ( type-name ). */
2396 seen_type = true;
2397 c_parser_consume_token (parser);
2398 struct c_type_name *type = c_parser_type_name (parser);
2399 t.kind = ctsk_typeof;
2400 t.spec = error_mark_node;
2401 t.expr = NULL_TREE;
2402 t.expr_const_operands = true;
2403 if (type != NULL)
2404 t.spec = groktypename (type, &t.expr,
2405 &t.expr_const_operands);
2406 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2407 "expected %<)%>");
2408 if (t.spec != error_mark_node)
2410 if (TREE_CODE (t.spec) == ARRAY_TYPE)
2411 error_at (loc, "%<_Atomic%>-qualified array type");
2412 else if (TREE_CODE (t.spec) == FUNCTION_TYPE)
2413 error_at (loc, "%<_Atomic%>-qualified function type");
2414 else if (TYPE_QUALS (t.spec) != TYPE_UNQUALIFIED)
2415 error_at (loc, "%<_Atomic%> applied to a qualified type");
2416 else
2417 t.spec = c_build_qualified_type (t.spec, TYPE_QUAL_ATOMIC);
2419 declspecs_add_type (loc, specs, t);
2421 else
2422 declspecs_add_qual (loc, specs, value);
2423 break;
2424 case RID_CONST:
2425 case RID_VOLATILE:
2426 case RID_RESTRICT:
2427 attrs_ok = true;
2428 declspecs_add_qual (loc, specs, c_parser_peek_token (parser)->value);
2429 c_parser_consume_token (parser);
2430 break;
2431 case RID_ATTRIBUTE:
2432 if (!attrs_ok)
2433 goto out;
2434 attrs = c_parser_attributes (parser);
2435 declspecs_add_attrs (loc, specs, attrs);
2436 break;
2437 case RID_ALIGNAS:
2438 if (!alignspec_ok)
2439 goto out;
2440 align = c_parser_alignas_specifier (parser);
2441 declspecs_add_alignas (loc, specs, align);
2442 break;
2443 default:
2444 goto out;
2447 out: ;
2450 /* Parse an enum specifier (C90 6.5.2.2, C99 6.7.2.2).
2452 enum-specifier:
2453 enum attributes[opt] identifier[opt] { enumerator-list } attributes[opt]
2454 enum attributes[opt] identifier[opt] { enumerator-list , } attributes[opt]
2455 enum attributes[opt] identifier
2457 The form with trailing comma is new in C99. The forms with
2458 attributes are GNU extensions. In GNU C, we accept any expression
2459 without commas in the syntax (assignment expressions, not just
2460 conditional expressions); assignment expressions will be diagnosed
2461 as non-constant.
2463 enumerator-list:
2464 enumerator
2465 enumerator-list , enumerator
2467 enumerator:
2468 enumeration-constant
2469 enumeration-constant = constant-expression
2472 static struct c_typespec
2473 c_parser_enum_specifier (c_parser *parser)
2475 struct c_typespec ret;
2476 tree attrs;
2477 tree ident = NULL_TREE;
2478 location_t enum_loc;
2479 location_t ident_loc = UNKNOWN_LOCATION; /* Quiet warning. */
2480 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ENUM));
2481 enum_loc = c_parser_peek_token (parser)->location;
2482 c_parser_consume_token (parser);
2483 attrs = c_parser_attributes (parser);
2484 enum_loc = c_parser_peek_token (parser)->location;
2485 /* Set the location in case we create a decl now. */
2486 c_parser_set_source_position_from_token (c_parser_peek_token (parser));
2487 if (c_parser_next_token_is (parser, CPP_NAME))
2489 ident = c_parser_peek_token (parser)->value;
2490 ident_loc = c_parser_peek_token (parser)->location;
2491 enum_loc = ident_loc;
2492 c_parser_consume_token (parser);
2494 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
2496 /* Parse an enum definition. */
2497 struct c_enum_contents the_enum;
2498 tree type;
2499 tree postfix_attrs;
2500 /* We chain the enumerators in reverse order, then put them in
2501 forward order at the end. */
2502 tree values;
2503 timevar_push (TV_PARSE_ENUM);
2504 type = start_enum (enum_loc, &the_enum, ident);
2505 values = NULL_TREE;
2506 c_parser_consume_token (parser);
2507 while (true)
2509 tree enum_id;
2510 tree enum_value;
2511 tree enum_decl;
2512 bool seen_comma;
2513 c_token *token;
2514 location_t comma_loc = UNKNOWN_LOCATION; /* Quiet warning. */
2515 location_t decl_loc, value_loc;
2516 if (c_parser_next_token_is_not (parser, CPP_NAME))
2518 c_parser_error (parser, "expected identifier");
2519 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
2520 values = error_mark_node;
2521 break;
2523 token = c_parser_peek_token (parser);
2524 enum_id = token->value;
2525 /* Set the location in case we create a decl now. */
2526 c_parser_set_source_position_from_token (token);
2527 decl_loc = value_loc = token->location;
2528 c_parser_consume_token (parser);
2529 if (c_parser_next_token_is (parser, CPP_EQ))
2531 c_parser_consume_token (parser);
2532 value_loc = c_parser_peek_token (parser)->location;
2533 enum_value = c_parser_expr_no_commas (parser, NULL).value;
2535 else
2536 enum_value = NULL_TREE;
2537 enum_decl = build_enumerator (decl_loc, value_loc,
2538 &the_enum, enum_id, enum_value);
2539 TREE_CHAIN (enum_decl) = values;
2540 values = enum_decl;
2541 seen_comma = false;
2542 if (c_parser_next_token_is (parser, CPP_COMMA))
2544 comma_loc = c_parser_peek_token (parser)->location;
2545 seen_comma = true;
2546 c_parser_consume_token (parser);
2548 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2550 if (seen_comma && !flag_isoc99)
2551 pedwarn (comma_loc, OPT_Wpedantic, "comma at end of enumerator list");
2552 c_parser_consume_token (parser);
2553 break;
2555 if (!seen_comma)
2557 c_parser_error (parser, "expected %<,%> or %<}%>");
2558 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
2559 values = error_mark_node;
2560 break;
2563 postfix_attrs = c_parser_attributes (parser);
2564 ret.spec = finish_enum (type, nreverse (values),
2565 chainon (attrs, postfix_attrs));
2566 ret.kind = ctsk_tagdef;
2567 ret.expr = NULL_TREE;
2568 ret.expr_const_operands = true;
2569 timevar_pop (TV_PARSE_ENUM);
2570 return ret;
2572 else if (!ident)
2574 c_parser_error (parser, "expected %<{%>");
2575 ret.spec = error_mark_node;
2576 ret.kind = ctsk_tagref;
2577 ret.expr = NULL_TREE;
2578 ret.expr_const_operands = true;
2579 return ret;
2581 ret = parser_xref_tag (ident_loc, ENUMERAL_TYPE, ident);
2582 /* In ISO C, enumerated types can be referred to only if already
2583 defined. */
2584 if (pedantic && !COMPLETE_TYPE_P (ret.spec))
2586 gcc_assert (ident);
2587 pedwarn (enum_loc, OPT_Wpedantic,
2588 "ISO C forbids forward references to %<enum%> types");
2590 return ret;
2593 /* Parse a struct or union specifier (C90 6.5.2.1, C99 6.7.2.1).
2595 struct-or-union-specifier:
2596 struct-or-union attributes[opt] identifier[opt]
2597 { struct-contents } attributes[opt]
2598 struct-or-union attributes[opt] identifier
2600 struct-contents:
2601 struct-declaration-list
2603 struct-declaration-list:
2604 struct-declaration ;
2605 struct-declaration-list struct-declaration ;
2607 GNU extensions:
2609 struct-contents:
2610 empty
2611 struct-declaration
2612 struct-declaration-list struct-declaration
2614 struct-declaration-list:
2615 struct-declaration-list ;
2618 (Note that in the syntax here, unlike that in ISO C, the semicolons
2619 are included here rather than in struct-declaration, in order to
2620 describe the syntax with extra semicolons and missing semicolon at
2621 end.)
2623 Objective-C:
2625 struct-declaration-list:
2626 @defs ( class-name )
2628 (Note this does not include a trailing semicolon, but can be
2629 followed by further declarations, and gets a pedwarn-if-pedantic
2630 when followed by a semicolon.) */
2632 static struct c_typespec
2633 c_parser_struct_or_union_specifier (c_parser *parser)
2635 struct c_typespec ret;
2636 tree attrs;
2637 tree ident = NULL_TREE;
2638 location_t struct_loc;
2639 location_t ident_loc = UNKNOWN_LOCATION;
2640 enum tree_code code;
2641 switch (c_parser_peek_token (parser)->keyword)
2643 case RID_STRUCT:
2644 code = RECORD_TYPE;
2645 break;
2646 case RID_UNION:
2647 code = UNION_TYPE;
2648 break;
2649 default:
2650 gcc_unreachable ();
2652 struct_loc = c_parser_peek_token (parser)->location;
2653 c_parser_consume_token (parser);
2654 attrs = c_parser_attributes (parser);
2656 /* Set the location in case we create a decl now. */
2657 c_parser_set_source_position_from_token (c_parser_peek_token (parser));
2659 if (c_parser_next_token_is (parser, CPP_NAME))
2661 ident = c_parser_peek_token (parser)->value;
2662 ident_loc = c_parser_peek_token (parser)->location;
2663 struct_loc = ident_loc;
2664 c_parser_consume_token (parser);
2666 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
2668 /* Parse a struct or union definition. Start the scope of the
2669 tag before parsing components. */
2670 struct c_struct_parse_info *struct_info;
2671 tree type = start_struct (struct_loc, code, ident, &struct_info);
2672 tree postfix_attrs;
2673 /* We chain the components in reverse order, then put them in
2674 forward order at the end. Each struct-declaration may
2675 declare multiple components (comma-separated), so we must use
2676 chainon to join them, although when parsing each
2677 struct-declaration we can use TREE_CHAIN directly.
2679 The theory behind all this is that there will be more
2680 semicolon separated fields than comma separated fields, and
2681 so we'll be minimizing the number of node traversals required
2682 by chainon. */
2683 tree contents;
2684 timevar_push (TV_PARSE_STRUCT);
2685 contents = NULL_TREE;
2686 c_parser_consume_token (parser);
2687 /* Handle the Objective-C @defs construct,
2688 e.g. foo(sizeof(struct{ @defs(ClassName) }));. */
2689 if (c_parser_next_token_is_keyword (parser, RID_AT_DEFS))
2691 tree name;
2692 gcc_assert (c_dialect_objc ());
2693 c_parser_consume_token (parser);
2694 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2695 goto end_at_defs;
2696 if (c_parser_next_token_is (parser, CPP_NAME)
2697 && c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME)
2699 name = c_parser_peek_token (parser)->value;
2700 c_parser_consume_token (parser);
2702 else
2704 c_parser_error (parser, "expected class name");
2705 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
2706 goto end_at_defs;
2708 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2709 "expected %<)%>");
2710 contents = nreverse (objc_get_class_ivars (name));
2712 end_at_defs:
2713 /* Parse the struct-declarations and semicolons. Problems with
2714 semicolons are diagnosed here; empty structures are diagnosed
2715 elsewhere. */
2716 while (true)
2718 tree decls;
2719 /* Parse any stray semicolon. */
2720 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
2722 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
2723 "extra semicolon in struct or union specified");
2724 c_parser_consume_token (parser);
2725 continue;
2727 /* Stop if at the end of the struct or union contents. */
2728 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2730 c_parser_consume_token (parser);
2731 break;
2733 /* Accept #pragmas at struct scope. */
2734 if (c_parser_next_token_is (parser, CPP_PRAGMA))
2736 c_parser_pragma (parser, pragma_struct);
2737 continue;
2739 /* Parse some comma-separated declarations, but not the
2740 trailing semicolon if any. */
2741 decls = c_parser_struct_declaration (parser);
2742 contents = chainon (decls, contents);
2743 /* If no semicolon follows, either we have a parse error or
2744 are at the end of the struct or union and should
2745 pedwarn. */
2746 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
2747 c_parser_consume_token (parser);
2748 else
2750 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2751 pedwarn (c_parser_peek_token (parser)->location, 0,
2752 "no semicolon at end of struct or union");
2753 else if (parser->error
2754 || !c_parser_next_token_starts_declspecs (parser))
2756 c_parser_error (parser, "expected %<;%>");
2757 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
2758 break;
2761 /* If we come here, we have already emitted an error
2762 for an expected `;', identifier or `(', and we also
2763 recovered already. Go on with the next field. */
2766 postfix_attrs = c_parser_attributes (parser);
2767 ret.spec = finish_struct (struct_loc, type, nreverse (contents),
2768 chainon (attrs, postfix_attrs), struct_info);
2769 ret.kind = ctsk_tagdef;
2770 ret.expr = NULL_TREE;
2771 ret.expr_const_operands = true;
2772 timevar_pop (TV_PARSE_STRUCT);
2773 return ret;
2775 else if (!ident)
2777 c_parser_error (parser, "expected %<{%>");
2778 ret.spec = error_mark_node;
2779 ret.kind = ctsk_tagref;
2780 ret.expr = NULL_TREE;
2781 ret.expr_const_operands = true;
2782 return ret;
2784 ret = parser_xref_tag (ident_loc, code, ident);
2785 return ret;
2788 /* Parse a struct-declaration (C90 6.5.2.1, C99 6.7.2.1), *without*
2789 the trailing semicolon.
2791 struct-declaration:
2792 specifier-qualifier-list struct-declarator-list
2793 static_assert-declaration-no-semi
2795 specifier-qualifier-list:
2796 type-specifier specifier-qualifier-list[opt]
2797 type-qualifier specifier-qualifier-list[opt]
2798 attributes specifier-qualifier-list[opt]
2800 struct-declarator-list:
2801 struct-declarator
2802 struct-declarator-list , attributes[opt] struct-declarator
2804 struct-declarator:
2805 declarator attributes[opt]
2806 declarator[opt] : constant-expression attributes[opt]
2808 GNU extensions:
2810 struct-declaration:
2811 __extension__ struct-declaration
2812 specifier-qualifier-list
2814 Unlike the ISO C syntax, semicolons are handled elsewhere. The use
2815 of attributes where shown is a GNU extension. In GNU C, we accept
2816 any expression without commas in the syntax (assignment
2817 expressions, not just conditional expressions); assignment
2818 expressions will be diagnosed as non-constant. */
2820 static tree
2821 c_parser_struct_declaration (c_parser *parser)
2823 struct c_declspecs *specs;
2824 tree prefix_attrs;
2825 tree all_prefix_attrs;
2826 tree decls;
2827 location_t decl_loc;
2828 if (c_parser_next_token_is_keyword (parser, RID_EXTENSION))
2830 int ext;
2831 tree decl;
2832 ext = disable_extension_diagnostics ();
2833 c_parser_consume_token (parser);
2834 decl = c_parser_struct_declaration (parser);
2835 restore_extension_diagnostics (ext);
2836 return decl;
2838 if (c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT))
2840 c_parser_static_assert_declaration_no_semi (parser);
2841 return NULL_TREE;
2843 specs = build_null_declspecs ();
2844 decl_loc = c_parser_peek_token (parser)->location;
2845 /* Strictly by the standard, we shouldn't allow _Alignas here,
2846 but it appears to have been intended to allow it there, so
2847 we're keeping it as it is until WG14 reaches a conclusion
2848 of N1731.
2849 <http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1731.pdf> */
2850 c_parser_declspecs (parser, specs, false, true, true,
2851 true, false, cla_nonabstract_decl);
2852 if (parser->error)
2853 return NULL_TREE;
2854 if (!specs->declspecs_seen_p)
2856 c_parser_error (parser, "expected specifier-qualifier-list");
2857 return NULL_TREE;
2859 finish_declspecs (specs);
2860 if (c_parser_next_token_is (parser, CPP_SEMICOLON)
2861 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2863 tree ret;
2864 if (specs->typespec_kind == ctsk_none)
2866 pedwarn (decl_loc, OPT_Wpedantic,
2867 "ISO C forbids member declarations with no members");
2868 shadow_tag_warned (specs, pedantic);
2869 ret = NULL_TREE;
2871 else
2873 /* Support for unnamed structs or unions as members of
2874 structs or unions (which is [a] useful and [b] supports
2875 MS P-SDK). */
2876 tree attrs = NULL;
2878 ret = grokfield (c_parser_peek_token (parser)->location,
2879 build_id_declarator (NULL_TREE), specs,
2880 NULL_TREE, &attrs);
2881 if (ret)
2882 decl_attributes (&ret, attrs, 0);
2884 return ret;
2887 /* Provide better error recovery. Note that a type name here is valid,
2888 and will be treated as a field name. */
2889 if (specs->typespec_kind == ctsk_tagdef
2890 && TREE_CODE (specs->type) != ENUMERAL_TYPE
2891 && c_parser_next_token_starts_declspecs (parser)
2892 && !c_parser_next_token_is (parser, CPP_NAME))
2894 c_parser_error (parser, "expected %<;%>, identifier or %<(%>");
2895 parser->error = false;
2896 return NULL_TREE;
2899 pending_xref_error ();
2900 prefix_attrs = specs->attrs;
2901 all_prefix_attrs = prefix_attrs;
2902 specs->attrs = NULL_TREE;
2903 decls = NULL_TREE;
2904 while (true)
2906 /* Declaring one or more declarators or un-named bit-fields. */
2907 struct c_declarator *declarator;
2908 bool dummy = false;
2909 if (c_parser_next_token_is (parser, CPP_COLON))
2910 declarator = build_id_declarator (NULL_TREE);
2911 else
2912 declarator = c_parser_declarator (parser,
2913 specs->typespec_kind != ctsk_none,
2914 C_DTR_NORMAL, &dummy);
2915 if (declarator == NULL)
2917 c_parser_skip_to_end_of_block_or_statement (parser);
2918 break;
2920 if (c_parser_next_token_is (parser, CPP_COLON)
2921 || c_parser_next_token_is (parser, CPP_COMMA)
2922 || c_parser_next_token_is (parser, CPP_SEMICOLON)
2923 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE)
2924 || c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2926 tree postfix_attrs = NULL_TREE;
2927 tree width = NULL_TREE;
2928 tree d;
2929 if (c_parser_next_token_is (parser, CPP_COLON))
2931 c_parser_consume_token (parser);
2932 width = c_parser_expr_no_commas (parser, NULL).value;
2934 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2935 postfix_attrs = c_parser_attributes (parser);
2936 d = grokfield (c_parser_peek_token (parser)->location,
2937 declarator, specs, width, &all_prefix_attrs);
2938 decl_attributes (&d, chainon (postfix_attrs,
2939 all_prefix_attrs), 0);
2940 DECL_CHAIN (d) = decls;
2941 decls = d;
2942 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2943 all_prefix_attrs = chainon (c_parser_attributes (parser),
2944 prefix_attrs);
2945 else
2946 all_prefix_attrs = prefix_attrs;
2947 if (c_parser_next_token_is (parser, CPP_COMMA))
2948 c_parser_consume_token (parser);
2949 else if (c_parser_next_token_is (parser, CPP_SEMICOLON)
2950 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2952 /* Semicolon consumed in caller. */
2953 break;
2955 else
2957 c_parser_error (parser, "expected %<,%>, %<;%> or %<}%>");
2958 break;
2961 else
2963 c_parser_error (parser,
2964 "expected %<:%>, %<,%>, %<;%>, %<}%> or "
2965 "%<__attribute__%>");
2966 break;
2969 return decls;
2972 /* Parse a typeof specifier (a GNU extension).
2974 typeof-specifier:
2975 typeof ( expression )
2976 typeof ( type-name )
2979 static struct c_typespec
2980 c_parser_typeof_specifier (c_parser *parser)
2982 struct c_typespec ret;
2983 ret.kind = ctsk_typeof;
2984 ret.spec = error_mark_node;
2985 ret.expr = NULL_TREE;
2986 ret.expr_const_operands = true;
2987 gcc_assert (c_parser_next_token_is_keyword (parser, RID_TYPEOF));
2988 c_parser_consume_token (parser);
2989 c_inhibit_evaluation_warnings++;
2990 in_typeof++;
2991 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2993 c_inhibit_evaluation_warnings--;
2994 in_typeof--;
2995 return ret;
2997 if (c_parser_next_tokens_start_typename (parser, cla_prefer_id))
2999 struct c_type_name *type = c_parser_type_name (parser);
3000 c_inhibit_evaluation_warnings--;
3001 in_typeof--;
3002 if (type != NULL)
3004 ret.spec = groktypename (type, &ret.expr, &ret.expr_const_operands);
3005 pop_maybe_used (variably_modified_type_p (ret.spec, NULL_TREE));
3008 else
3010 bool was_vm;
3011 location_t here = c_parser_peek_token (parser)->location;
3012 struct c_expr expr = c_parser_expression (parser);
3013 c_inhibit_evaluation_warnings--;
3014 in_typeof--;
3015 if (TREE_CODE (expr.value) == COMPONENT_REF
3016 && DECL_C_BIT_FIELD (TREE_OPERAND (expr.value, 1)))
3017 error_at (here, "%<typeof%> applied to a bit-field");
3018 mark_exp_read (expr.value);
3019 ret.spec = TREE_TYPE (expr.value);
3020 was_vm = variably_modified_type_p (ret.spec, NULL_TREE);
3021 /* This is returned with the type so that when the type is
3022 evaluated, this can be evaluated. */
3023 if (was_vm)
3024 ret.expr = c_fully_fold (expr.value, false, &ret.expr_const_operands);
3025 pop_maybe_used (was_vm);
3026 /* For use in macros such as those in <stdatomic.h>, remove
3027 _Atomic and const qualifiers from atomic types. (Possibly
3028 all qualifiers should be removed; const can be an issue for
3029 more macros using typeof than just the <stdatomic.h>
3030 ones.) */
3031 if (ret.spec != error_mark_node && TYPE_ATOMIC (ret.spec))
3032 ret.spec = c_build_qualified_type (ret.spec,
3033 (TYPE_QUALS (ret.spec)
3034 & ~(TYPE_QUAL_ATOMIC
3035 | TYPE_QUAL_CONST)));
3037 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
3038 return ret;
3041 /* Parse an alignment-specifier.
3043 C11 6.7.5:
3045 alignment-specifier:
3046 _Alignas ( type-name )
3047 _Alignas ( constant-expression )
3050 static tree
3051 c_parser_alignas_specifier (c_parser * parser)
3053 tree ret = error_mark_node;
3054 location_t loc = c_parser_peek_token (parser)->location;
3055 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ALIGNAS));
3056 c_parser_consume_token (parser);
3057 if (!flag_isoc11)
3059 if (flag_isoc99)
3060 pedwarn (loc, OPT_Wpedantic,
3061 "ISO C99 does not support %<_Alignas%>");
3062 else
3063 pedwarn (loc, OPT_Wpedantic,
3064 "ISO C90 does not support %<_Alignas%>");
3066 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3067 return ret;
3068 if (c_parser_next_tokens_start_typename (parser, cla_prefer_id))
3070 struct c_type_name *type = c_parser_type_name (parser);
3071 if (type != NULL)
3072 ret = c_sizeof_or_alignof_type (loc, groktypename (type, NULL, NULL),
3073 false, true, 1);
3075 else
3076 ret = c_parser_expr_no_commas (parser, NULL).value;
3077 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
3078 return ret;
3081 /* Parse a declarator, possibly an abstract declarator (C90 6.5.4,
3082 6.5.5, C99 6.7.5, 6.7.6). If TYPE_SEEN_P then a typedef name may
3083 be redeclared; otherwise it may not. KIND indicates which kind of
3084 declarator is wanted. Returns a valid declarator except in the
3085 case of a syntax error in which case NULL is returned. *SEEN_ID is
3086 set to true if an identifier being declared is seen; this is used
3087 to diagnose bad forms of abstract array declarators and to
3088 determine whether an identifier list is syntactically permitted.
3090 declarator:
3091 pointer[opt] direct-declarator
3093 direct-declarator:
3094 identifier
3095 ( attributes[opt] declarator )
3096 direct-declarator array-declarator
3097 direct-declarator ( parameter-type-list )
3098 direct-declarator ( identifier-list[opt] )
3100 pointer:
3101 * type-qualifier-list[opt]
3102 * type-qualifier-list[opt] pointer
3104 type-qualifier-list:
3105 type-qualifier
3106 attributes
3107 type-qualifier-list type-qualifier
3108 type-qualifier-list attributes
3110 array-declarator:
3111 [ type-qualifier-list[opt] assignment-expression[opt] ]
3112 [ static type-qualifier-list[opt] assignment-expression ]
3113 [ type-qualifier-list static assignment-expression ]
3114 [ type-qualifier-list[opt] * ]
3116 parameter-type-list:
3117 parameter-list
3118 parameter-list , ...
3120 parameter-list:
3121 parameter-declaration
3122 parameter-list , parameter-declaration
3124 parameter-declaration:
3125 declaration-specifiers declarator attributes[opt]
3126 declaration-specifiers abstract-declarator[opt] attributes[opt]
3128 identifier-list:
3129 identifier
3130 identifier-list , identifier
3132 abstract-declarator:
3133 pointer
3134 pointer[opt] direct-abstract-declarator
3136 direct-abstract-declarator:
3137 ( attributes[opt] abstract-declarator )
3138 direct-abstract-declarator[opt] array-declarator
3139 direct-abstract-declarator[opt] ( parameter-type-list[opt] )
3141 GNU extensions:
3143 direct-declarator:
3144 direct-declarator ( parameter-forward-declarations
3145 parameter-type-list[opt] )
3147 direct-abstract-declarator:
3148 direct-abstract-declarator[opt] ( parameter-forward-declarations
3149 parameter-type-list[opt] )
3151 parameter-forward-declarations:
3152 parameter-list ;
3153 parameter-forward-declarations parameter-list ;
3155 The uses of attributes shown above are GNU extensions.
3157 Some forms of array declarator are not included in C99 in the
3158 syntax for abstract declarators; these are disallowed elsewhere.
3159 This may be a defect (DR#289).
3161 This function also accepts an omitted abstract declarator as being
3162 an abstract declarator, although not part of the formal syntax. */
3164 static struct c_declarator *
3165 c_parser_declarator (c_parser *parser, bool type_seen_p, c_dtr_syn kind,
3166 bool *seen_id)
3168 /* Parse any initial pointer part. */
3169 if (c_parser_next_token_is (parser, CPP_MULT))
3171 struct c_declspecs *quals_attrs = build_null_declspecs ();
3172 struct c_declarator *inner;
3173 c_parser_consume_token (parser);
3174 c_parser_declspecs (parser, quals_attrs, false, false, true,
3175 false, false, cla_prefer_id);
3176 inner = c_parser_declarator (parser, type_seen_p, kind, seen_id);
3177 if (inner == NULL)
3178 return NULL;
3179 else
3180 return make_pointer_declarator (quals_attrs, inner);
3182 /* Now we have a direct declarator, direct abstract declarator or
3183 nothing (which counts as a direct abstract declarator here). */
3184 return c_parser_direct_declarator (parser, type_seen_p, kind, seen_id);
3187 /* Parse a direct declarator or direct abstract declarator; arguments
3188 as c_parser_declarator. */
3190 static struct c_declarator *
3191 c_parser_direct_declarator (c_parser *parser, bool type_seen_p, c_dtr_syn kind,
3192 bool *seen_id)
3194 /* The direct declarator must start with an identifier (possibly
3195 omitted) or a parenthesized declarator (possibly abstract). In
3196 an ordinary declarator, initial parentheses must start a
3197 parenthesized declarator. In an abstract declarator or parameter
3198 declarator, they could start a parenthesized declarator or a
3199 parameter list. To tell which, the open parenthesis and any
3200 following attributes must be read. If a declaration specifier
3201 follows, then it is a parameter list; if the specifier is a
3202 typedef name, there might be an ambiguity about redeclaring it,
3203 which is resolved in the direction of treating it as a typedef
3204 name. If a close parenthesis follows, it is also an empty
3205 parameter list, as the syntax does not permit empty abstract
3206 declarators. Otherwise, it is a parenthesized declarator (in
3207 which case the analysis may be repeated inside it, recursively).
3209 ??? There is an ambiguity in a parameter declaration "int
3210 (__attribute__((foo)) x)", where x is not a typedef name: it
3211 could be an abstract declarator for a function, or declare x with
3212 parentheses. The proper resolution of this ambiguity needs
3213 documenting. At present we follow an accident of the old
3214 parser's implementation, whereby the first parameter must have
3215 some declaration specifiers other than just attributes. Thus as
3216 a parameter declaration it is treated as a parenthesized
3217 parameter named x, and as an abstract declarator it is
3218 rejected.
3220 ??? Also following the old parser, attributes inside an empty
3221 parameter list are ignored, making it a list not yielding a
3222 prototype, rather than giving an error or making it have one
3223 parameter with implicit type int.
3225 ??? Also following the old parser, typedef names may be
3226 redeclared in declarators, but not Objective-C class names. */
3228 if (kind != C_DTR_ABSTRACT
3229 && c_parser_next_token_is (parser, CPP_NAME)
3230 && ((type_seen_p
3231 && (c_parser_peek_token (parser)->id_kind == C_ID_TYPENAME
3232 || c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME))
3233 || c_parser_peek_token (parser)->id_kind == C_ID_ID))
3235 struct c_declarator *inner
3236 = build_id_declarator (c_parser_peek_token (parser)->value);
3237 *seen_id = true;
3238 inner->id_loc = c_parser_peek_token (parser)->location;
3239 c_parser_consume_token (parser);
3240 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
3243 if (kind != C_DTR_NORMAL
3244 && c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
3246 struct c_declarator *inner = build_id_declarator (NULL_TREE);
3247 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
3250 /* Either we are at the end of an abstract declarator, or we have
3251 parentheses. */
3253 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
3255 tree attrs;
3256 struct c_declarator *inner;
3257 c_parser_consume_token (parser);
3258 attrs = c_parser_attributes (parser);
3259 if (kind != C_DTR_NORMAL
3260 && (c_parser_next_token_starts_declspecs (parser)
3261 || c_parser_next_token_is (parser, CPP_CLOSE_PAREN)))
3263 struct c_arg_info *args
3264 = c_parser_parms_declarator (parser, kind == C_DTR_NORMAL,
3265 attrs);
3266 if (args == NULL)
3267 return NULL;
3268 else
3270 inner
3271 = build_function_declarator (args,
3272 build_id_declarator (NULL_TREE));
3273 return c_parser_direct_declarator_inner (parser, *seen_id,
3274 inner);
3277 /* A parenthesized declarator. */
3278 inner = c_parser_declarator (parser, type_seen_p, kind, seen_id);
3279 if (inner != NULL && attrs != NULL)
3280 inner = build_attrs_declarator (attrs, inner);
3281 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3283 c_parser_consume_token (parser);
3284 if (inner == NULL)
3285 return NULL;
3286 else
3287 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
3289 else
3291 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3292 "expected %<)%>");
3293 return NULL;
3296 else
3298 if (kind == C_DTR_NORMAL)
3300 c_parser_error (parser, "expected identifier or %<(%>");
3301 return NULL;
3303 else
3304 return build_id_declarator (NULL_TREE);
3308 /* Parse part of a direct declarator or direct abstract declarator,
3309 given that some (in INNER) has already been parsed; ID_PRESENT is
3310 true if an identifier is present, false for an abstract
3311 declarator. */
3313 static struct c_declarator *
3314 c_parser_direct_declarator_inner (c_parser *parser, bool id_present,
3315 struct c_declarator *inner)
3317 /* Parse a sequence of array declarators and parameter lists. */
3318 if (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
3320 location_t brace_loc = c_parser_peek_token (parser)->location;
3321 struct c_declarator *declarator;
3322 struct c_declspecs *quals_attrs = build_null_declspecs ();
3323 bool static_seen;
3324 bool star_seen;
3325 struct c_expr dimen;
3326 dimen.value = NULL_TREE;
3327 dimen.original_code = ERROR_MARK;
3328 dimen.original_type = NULL_TREE;
3329 c_parser_consume_token (parser);
3330 c_parser_declspecs (parser, quals_attrs, false, false, true,
3331 false, false, cla_prefer_id);
3332 static_seen = c_parser_next_token_is_keyword (parser, RID_STATIC);
3333 if (static_seen)
3334 c_parser_consume_token (parser);
3335 if (static_seen && !quals_attrs->declspecs_seen_p)
3336 c_parser_declspecs (parser, quals_attrs, false, false, true,
3337 false, false, cla_prefer_id);
3338 if (!quals_attrs->declspecs_seen_p)
3339 quals_attrs = NULL;
3340 /* If "static" is present, there must be an array dimension.
3341 Otherwise, there may be a dimension, "*", or no
3342 dimension. */
3343 if (static_seen)
3345 star_seen = false;
3346 dimen = c_parser_expr_no_commas (parser, NULL);
3348 else
3350 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
3352 dimen.value = NULL_TREE;
3353 star_seen = false;
3355 else if (flag_cilkplus
3356 && c_parser_next_token_is (parser, CPP_COLON))
3358 dimen.value = error_mark_node;
3359 star_seen = false;
3360 error_at (c_parser_peek_token (parser)->location,
3361 "array notations cannot be used in declaration");
3362 c_parser_consume_token (parser);
3364 else if (c_parser_next_token_is (parser, CPP_MULT))
3366 if (c_parser_peek_2nd_token (parser)->type == CPP_CLOSE_SQUARE)
3368 dimen.value = NULL_TREE;
3369 star_seen = true;
3370 c_parser_consume_token (parser);
3372 else
3374 star_seen = false;
3375 dimen = c_parser_expr_no_commas (parser, NULL);
3378 else
3380 star_seen = false;
3381 dimen = c_parser_expr_no_commas (parser, NULL);
3384 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
3385 c_parser_consume_token (parser);
3386 else if (flag_cilkplus
3387 && c_parser_next_token_is (parser, CPP_COLON))
3389 error_at (c_parser_peek_token (parser)->location,
3390 "array notations cannot be used in declaration");
3391 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
3392 return NULL;
3394 else
3396 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
3397 "expected %<]%>");
3398 return NULL;
3400 if (dimen.value)
3401 dimen = convert_lvalue_to_rvalue (brace_loc, dimen, true, true);
3402 declarator = build_array_declarator (brace_loc, dimen.value, quals_attrs,
3403 static_seen, star_seen);
3404 if (declarator == NULL)
3405 return NULL;
3406 inner = set_array_declarator_inner (declarator, inner);
3407 return c_parser_direct_declarator_inner (parser, id_present, inner);
3409 else if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
3411 tree attrs;
3412 struct c_arg_info *args;
3413 c_parser_consume_token (parser);
3414 attrs = c_parser_attributes (parser);
3415 args = c_parser_parms_declarator (parser, id_present, attrs);
3416 if (args == NULL)
3417 return NULL;
3418 else
3420 inner = build_function_declarator (args, inner);
3421 return c_parser_direct_declarator_inner (parser, id_present, inner);
3424 return inner;
3427 /* Parse a parameter list or identifier list, including the closing
3428 parenthesis but not the opening one. ATTRS are the attributes at
3429 the start of the list. ID_LIST_OK is true if an identifier list is
3430 acceptable; such a list must not have attributes at the start. */
3432 static struct c_arg_info *
3433 c_parser_parms_declarator (c_parser *parser, bool id_list_ok, tree attrs)
3435 push_scope ();
3436 declare_parm_level ();
3437 /* If the list starts with an identifier, it is an identifier list.
3438 Otherwise, it is either a prototype list or an empty list. */
3439 if (id_list_ok
3440 && !attrs
3441 && c_parser_next_token_is (parser, CPP_NAME)
3442 && c_parser_peek_token (parser)->id_kind == C_ID_ID
3444 /* Look ahead to detect typos in type names. */
3445 && c_parser_peek_2nd_token (parser)->type != CPP_NAME
3446 && c_parser_peek_2nd_token (parser)->type != CPP_MULT
3447 && c_parser_peek_2nd_token (parser)->type != CPP_OPEN_PAREN
3448 && c_parser_peek_2nd_token (parser)->type != CPP_OPEN_SQUARE)
3450 tree list = NULL_TREE, *nextp = &list;
3451 while (c_parser_next_token_is (parser, CPP_NAME)
3452 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
3454 *nextp = build_tree_list (NULL_TREE,
3455 c_parser_peek_token (parser)->value);
3456 nextp = & TREE_CHAIN (*nextp);
3457 c_parser_consume_token (parser);
3458 if (c_parser_next_token_is_not (parser, CPP_COMMA))
3459 break;
3460 c_parser_consume_token (parser);
3461 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3463 c_parser_error (parser, "expected identifier");
3464 break;
3467 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3469 struct c_arg_info *ret = build_arg_info ();
3470 ret->types = list;
3471 c_parser_consume_token (parser);
3472 pop_scope ();
3473 return ret;
3475 else
3477 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3478 "expected %<)%>");
3479 pop_scope ();
3480 return NULL;
3483 else
3485 struct c_arg_info *ret = c_parser_parms_list_declarator (parser, attrs,
3486 NULL);
3487 pop_scope ();
3488 return ret;
3492 /* Parse a parameter list (possibly empty), including the closing
3493 parenthesis but not the opening one. ATTRS are the attributes at
3494 the start of the list. EXPR is NULL or an expression that needs to
3495 be evaluated for the side effects of array size expressions in the
3496 parameters. */
3498 static struct c_arg_info *
3499 c_parser_parms_list_declarator (c_parser *parser, tree attrs, tree expr)
3501 bool bad_parm = false;
3503 /* ??? Following the old parser, forward parameter declarations may
3504 use abstract declarators, and if no real parameter declarations
3505 follow the forward declarations then this is not diagnosed. Also
3506 note as above that attributes are ignored as the only contents of
3507 the parentheses, or as the only contents after forward
3508 declarations. */
3509 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3511 struct c_arg_info *ret = build_arg_info ();
3512 c_parser_consume_token (parser);
3513 return ret;
3515 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
3517 struct c_arg_info *ret = build_arg_info ();
3519 if (flag_allow_parameterless_variadic_functions)
3521 /* F (...) is allowed. */
3522 ret->types = NULL_TREE;
3524 else
3526 /* Suppress -Wold-style-definition for this case. */
3527 ret->types = error_mark_node;
3528 error_at (c_parser_peek_token (parser)->location,
3529 "ISO C requires a named argument before %<...%>");
3531 c_parser_consume_token (parser);
3532 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3534 c_parser_consume_token (parser);
3535 return ret;
3537 else
3539 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3540 "expected %<)%>");
3541 return NULL;
3544 /* Nonempty list of parameters, either terminated with semicolon
3545 (forward declarations; recurse) or with close parenthesis (normal
3546 function) or with ", ... )" (variadic function). */
3547 while (true)
3549 /* Parse a parameter. */
3550 struct c_parm *parm = c_parser_parameter_declaration (parser, attrs);
3551 attrs = NULL_TREE;
3552 if (parm == NULL)
3553 bad_parm = true;
3554 else
3555 push_parm_decl (parm, &expr);
3556 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
3558 tree new_attrs;
3559 c_parser_consume_token (parser);
3560 mark_forward_parm_decls ();
3561 new_attrs = c_parser_attributes (parser);
3562 return c_parser_parms_list_declarator (parser, new_attrs, expr);
3564 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3566 c_parser_consume_token (parser);
3567 if (bad_parm)
3568 return NULL;
3569 else
3570 return get_parm_info (false, expr);
3572 if (!c_parser_require (parser, CPP_COMMA,
3573 "expected %<;%>, %<,%> or %<)%>"))
3575 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
3576 return NULL;
3578 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
3580 c_parser_consume_token (parser);
3581 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3583 c_parser_consume_token (parser);
3584 if (bad_parm)
3585 return NULL;
3586 else
3587 return get_parm_info (true, expr);
3589 else
3591 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3592 "expected %<)%>");
3593 return NULL;
3599 /* Parse a parameter declaration. ATTRS are the attributes at the
3600 start of the declaration if it is the first parameter. */
3602 static struct c_parm *
3603 c_parser_parameter_declaration (c_parser *parser, tree attrs)
3605 struct c_declspecs *specs;
3606 struct c_declarator *declarator;
3607 tree prefix_attrs;
3608 tree postfix_attrs = NULL_TREE;
3609 bool dummy = false;
3611 /* Accept #pragmas between parameter declarations. */
3612 while (c_parser_next_token_is (parser, CPP_PRAGMA))
3613 c_parser_pragma (parser, pragma_param);
3615 if (!c_parser_next_token_starts_declspecs (parser))
3617 c_token *token = c_parser_peek_token (parser);
3618 if (parser->error)
3619 return NULL;
3620 c_parser_set_source_position_from_token (token);
3621 if (c_parser_next_tokens_start_typename (parser, cla_prefer_type))
3623 error_at (token->location, "unknown type name %qE", token->value);
3624 parser->error = true;
3626 /* ??? In some Objective-C cases '...' isn't applicable so there
3627 should be a different message. */
3628 else
3629 c_parser_error (parser,
3630 "expected declaration specifiers or %<...%>");
3631 c_parser_skip_to_end_of_parameter (parser);
3632 return NULL;
3634 specs = build_null_declspecs ();
3635 if (attrs)
3637 declspecs_add_attrs (input_location, specs, attrs);
3638 attrs = NULL_TREE;
3640 c_parser_declspecs (parser, specs, true, true, true, true, false,
3641 cla_nonabstract_decl);
3642 finish_declspecs (specs);
3643 pending_xref_error ();
3644 prefix_attrs = specs->attrs;
3645 specs->attrs = NULL_TREE;
3646 declarator = c_parser_declarator (parser,
3647 specs->typespec_kind != ctsk_none,
3648 C_DTR_PARM, &dummy);
3649 if (declarator == NULL)
3651 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
3652 return NULL;
3654 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
3655 postfix_attrs = c_parser_attributes (parser);
3656 return build_c_parm (specs, chainon (postfix_attrs, prefix_attrs),
3657 declarator);
3660 /* Parse a string literal in an asm expression. It should not be
3661 translated, and wide string literals are an error although
3662 permitted by the syntax. This is a GNU extension.
3664 asm-string-literal:
3665 string-literal
3667 ??? At present, following the old parser, the caller needs to have
3668 set lex_untranslated_string to 1. It would be better to follow the
3669 C++ parser rather than using this kludge. */
3671 static tree
3672 c_parser_asm_string_literal (c_parser *parser)
3674 tree str;
3675 int save_flag = warn_overlength_strings;
3676 warn_overlength_strings = 0;
3677 if (c_parser_next_token_is (parser, CPP_STRING))
3679 str = c_parser_peek_token (parser)->value;
3680 c_parser_consume_token (parser);
3682 else if (c_parser_next_token_is (parser, CPP_WSTRING))
3684 error_at (c_parser_peek_token (parser)->location,
3685 "wide string literal in %<asm%>");
3686 str = build_string (1, "");
3687 c_parser_consume_token (parser);
3689 else
3691 c_parser_error (parser, "expected string literal");
3692 str = NULL_TREE;
3694 warn_overlength_strings = save_flag;
3695 return str;
3698 /* Parse a simple asm expression. This is used in restricted
3699 contexts, where a full expression with inputs and outputs does not
3700 make sense. This is a GNU extension.
3702 simple-asm-expr:
3703 asm ( asm-string-literal )
3706 static tree
3707 c_parser_simple_asm_expr (c_parser *parser)
3709 tree str;
3710 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ASM));
3711 /* ??? Follow the C++ parser rather than using the
3712 lex_untranslated_string kludge. */
3713 parser->lex_untranslated_string = true;
3714 c_parser_consume_token (parser);
3715 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3717 parser->lex_untranslated_string = false;
3718 return NULL_TREE;
3720 str = c_parser_asm_string_literal (parser);
3721 parser->lex_untranslated_string = false;
3722 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
3724 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
3725 return NULL_TREE;
3727 return str;
3730 static tree
3731 c_parser_attribute_any_word (c_parser *parser)
3733 tree attr_name = NULL_TREE;
3735 if (c_parser_next_token_is (parser, CPP_KEYWORD))
3737 /* ??? See comment above about what keywords are accepted here. */
3738 bool ok;
3739 switch (c_parser_peek_token (parser)->keyword)
3741 case RID_STATIC:
3742 case RID_UNSIGNED:
3743 case RID_LONG:
3744 case RID_INT128:
3745 case RID_CONST:
3746 case RID_EXTERN:
3747 case RID_REGISTER:
3748 case RID_TYPEDEF:
3749 case RID_SHORT:
3750 case RID_INLINE:
3751 case RID_NORETURN:
3752 case RID_VOLATILE:
3753 case RID_SIGNED:
3754 case RID_AUTO:
3755 case RID_RESTRICT:
3756 case RID_COMPLEX:
3757 case RID_THREAD:
3758 case RID_INT:
3759 case RID_CHAR:
3760 case RID_FLOAT:
3761 case RID_DOUBLE:
3762 case RID_VOID:
3763 case RID_DFLOAT32:
3764 case RID_DFLOAT64:
3765 case RID_DFLOAT128:
3766 case RID_BOOL:
3767 case RID_FRACT:
3768 case RID_ACCUM:
3769 case RID_SAT:
3770 case RID_TRANSACTION_ATOMIC:
3771 case RID_TRANSACTION_CANCEL:
3772 case RID_ATOMIC:
3773 case RID_AUTO_TYPE:
3774 ok = true;
3775 break;
3776 default:
3777 ok = false;
3778 break;
3780 if (!ok)
3781 return NULL_TREE;
3783 /* Accept __attribute__((__const)) as __attribute__((const)) etc. */
3784 attr_name = ridpointers[(int) c_parser_peek_token (parser)->keyword];
3786 else if (c_parser_next_token_is (parser, CPP_NAME))
3787 attr_name = c_parser_peek_token (parser)->value;
3789 return attr_name;
3792 /* Returns true of NAME is an IDENTIFIER_NODE with identiifer "vector,"
3793 "__vector" or "__vector__." */
3795 static inline bool
3796 is_cilkplus_vector_p (tree name)
3798 if (flag_cilkplus && is_attribute_p ("vector", name))
3799 return true;
3800 return false;
3803 #define CILK_SIMD_FN_CLAUSE_MASK \
3804 ((OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_VECTORLENGTH) \
3805 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_LINEAR) \
3806 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_UNIFORM) \
3807 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_MASK) \
3808 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_NOMASK))
3810 /* Parses the vector attribute of SIMD enabled functions in Cilk Plus.
3811 VEC_TOKEN is the "vector" token that is replaced with "simd" and
3812 pushed into the token list.
3813 Syntax:
3814 vector
3815 vector (<vector attributes>). */
3817 static void
3818 c_parser_cilk_simd_fn_vector_attrs (c_parser *parser, c_token vec_token)
3820 gcc_assert (is_cilkplus_vector_p (vec_token.value));
3822 int paren_scope = 0;
3823 vec_safe_push (parser->cilk_simd_fn_tokens, vec_token);
3824 /* Consume the "vector" token. */
3825 c_parser_consume_token (parser);
3827 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
3829 c_parser_consume_token (parser);
3830 paren_scope++;
3832 while (paren_scope > 0)
3834 c_token *token = c_parser_peek_token (parser);
3835 if (token->type == CPP_OPEN_PAREN)
3836 paren_scope++;
3837 else if (token->type == CPP_CLOSE_PAREN)
3838 paren_scope--;
3839 /* Do not push the last ')' since we are not pushing the '('. */
3840 if (!(token->type == CPP_CLOSE_PAREN && paren_scope == 0))
3841 vec_safe_push (parser->cilk_simd_fn_tokens, *token);
3842 c_parser_consume_token (parser);
3845 /* Since we are converting an attribute to a pragma, we need to end the
3846 attribute with PRAGMA_EOL. */
3847 c_token eol_token;
3848 memset (&eol_token, 0, sizeof (eol_token));
3849 eol_token.type = CPP_PRAGMA_EOL;
3850 vec_safe_push (parser->cilk_simd_fn_tokens, eol_token);
3853 /* Add 2 CPP_EOF at the end of PARSER->ELEM_FN_TOKENS vector. */
3855 static void
3856 c_finish_cilk_simd_fn_tokens (c_parser *parser)
3858 c_token last_token = parser->cilk_simd_fn_tokens->last ();
3860 /* c_parser_attributes is called in several places, so if these EOF
3861 tokens are already inserted, then don't do them again. */
3862 if (last_token.type == CPP_EOF)
3863 return;
3865 /* Two CPP_EOF token are added as a safety net since the normal C
3866 front-end has two token look-ahead. */
3867 c_token eof_token;
3868 eof_token.type = CPP_EOF;
3869 vec_safe_push (parser->cilk_simd_fn_tokens, eof_token);
3870 vec_safe_push (parser->cilk_simd_fn_tokens, eof_token);
3873 /* Parse (possibly empty) attributes. This is a GNU extension.
3875 attributes:
3876 empty
3877 attributes attribute
3879 attribute:
3880 __attribute__ ( ( attribute-list ) )
3882 attribute-list:
3883 attrib
3884 attribute_list , attrib
3886 attrib:
3887 empty
3888 any-word
3889 any-word ( identifier )
3890 any-word ( identifier , nonempty-expr-list )
3891 any-word ( expr-list )
3893 where the "identifier" must not be declared as a type, and
3894 "any-word" may be any identifier (including one declared as a
3895 type), a reserved word storage class specifier, type specifier or
3896 type qualifier. ??? This still leaves out most reserved keywords
3897 (following the old parser), shouldn't we include them, and why not
3898 allow identifiers declared as types to start the arguments? */
3900 static tree
3901 c_parser_attributes (c_parser *parser)
3903 tree attrs = NULL_TREE;
3904 while (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
3906 /* ??? Follow the C++ parser rather than using the
3907 lex_untranslated_string kludge. */
3908 parser->lex_untranslated_string = true;
3909 c_parser_consume_token (parser);
3910 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3912 parser->lex_untranslated_string = false;
3913 return attrs;
3915 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3917 parser->lex_untranslated_string = false;
3918 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
3919 return attrs;
3921 /* Parse the attribute list. */
3922 while (c_parser_next_token_is (parser, CPP_COMMA)
3923 || c_parser_next_token_is (parser, CPP_NAME)
3924 || c_parser_next_token_is (parser, CPP_KEYWORD))
3926 tree attr, attr_name, attr_args;
3927 vec<tree, va_gc> *expr_list;
3928 if (c_parser_next_token_is (parser, CPP_COMMA))
3930 c_parser_consume_token (parser);
3931 continue;
3934 attr_name = c_parser_attribute_any_word (parser);
3935 if (attr_name == NULL)
3936 break;
3937 if (is_cilkplus_vector_p (attr_name))
3939 c_token *v_token = c_parser_peek_token (parser);
3940 c_parser_cilk_simd_fn_vector_attrs (parser, *v_token);
3941 continue;
3943 c_parser_consume_token (parser);
3944 if (c_parser_next_token_is_not (parser, CPP_OPEN_PAREN))
3946 attr = build_tree_list (attr_name, NULL_TREE);
3947 attrs = chainon (attrs, attr);
3948 continue;
3950 c_parser_consume_token (parser);
3951 /* Parse the attribute contents. If they start with an
3952 identifier which is followed by a comma or close
3953 parenthesis, then the arguments start with that
3954 identifier; otherwise they are an expression list.
3955 In objective-c the identifier may be a classname. */
3956 if (c_parser_next_token_is (parser, CPP_NAME)
3957 && (c_parser_peek_token (parser)->id_kind == C_ID_ID
3958 || (c_dialect_objc ()
3959 && c_parser_peek_token (parser)->id_kind
3960 == C_ID_CLASSNAME))
3961 && ((c_parser_peek_2nd_token (parser)->type == CPP_COMMA)
3962 || (c_parser_peek_2nd_token (parser)->type
3963 == CPP_CLOSE_PAREN))
3964 && (attribute_takes_identifier_p (attr_name)
3965 || (c_dialect_objc ()
3966 && c_parser_peek_token (parser)->id_kind
3967 == C_ID_CLASSNAME)))
3969 tree arg1 = c_parser_peek_token (parser)->value;
3970 c_parser_consume_token (parser);
3971 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3972 attr_args = build_tree_list (NULL_TREE, arg1);
3973 else
3975 tree tree_list;
3976 c_parser_consume_token (parser);
3977 expr_list = c_parser_expr_list (parser, false, true,
3978 NULL, NULL, NULL, NULL);
3979 tree_list = build_tree_list_vec (expr_list);
3980 attr_args = tree_cons (NULL_TREE, arg1, tree_list);
3981 release_tree_vector (expr_list);
3984 else
3986 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3987 attr_args = NULL_TREE;
3988 else
3990 expr_list = c_parser_expr_list (parser, false, true,
3991 NULL, NULL, NULL, NULL);
3992 attr_args = build_tree_list_vec (expr_list);
3993 release_tree_vector (expr_list);
3996 attr = build_tree_list (attr_name, attr_args);
3997 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3998 c_parser_consume_token (parser);
3999 else
4001 parser->lex_untranslated_string = false;
4002 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
4003 "expected %<)%>");
4004 return attrs;
4006 attrs = chainon (attrs, attr);
4008 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4009 c_parser_consume_token (parser);
4010 else
4012 parser->lex_untranslated_string = false;
4013 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
4014 "expected %<)%>");
4015 return attrs;
4017 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4018 c_parser_consume_token (parser);
4019 else
4021 parser->lex_untranslated_string = false;
4022 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
4023 "expected %<)%>");
4024 return attrs;
4026 parser->lex_untranslated_string = false;
4029 if (flag_cilkplus && !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
4030 c_finish_cilk_simd_fn_tokens (parser);
4031 return attrs;
4034 /* Parse a type name (C90 6.5.5, C99 6.7.6).
4036 type-name:
4037 specifier-qualifier-list abstract-declarator[opt]
4040 static struct c_type_name *
4041 c_parser_type_name (c_parser *parser)
4043 struct c_declspecs *specs = build_null_declspecs ();
4044 struct c_declarator *declarator;
4045 struct c_type_name *ret;
4046 bool dummy = false;
4047 c_parser_declspecs (parser, specs, false, true, true, false, false,
4048 cla_prefer_type);
4049 if (!specs->declspecs_seen_p)
4051 c_parser_error (parser, "expected specifier-qualifier-list");
4052 return NULL;
4054 if (specs->type != error_mark_node)
4056 pending_xref_error ();
4057 finish_declspecs (specs);
4059 declarator = c_parser_declarator (parser,
4060 specs->typespec_kind != ctsk_none,
4061 C_DTR_ABSTRACT, &dummy);
4062 if (declarator == NULL)
4063 return NULL;
4064 ret = XOBNEW (&parser_obstack, struct c_type_name);
4065 ret->specs = specs;
4066 ret->declarator = declarator;
4067 return ret;
4070 /* Parse an initializer (C90 6.5.7, C99 6.7.8).
4072 initializer:
4073 assignment-expression
4074 { initializer-list }
4075 { initializer-list , }
4077 initializer-list:
4078 designation[opt] initializer
4079 initializer-list , designation[opt] initializer
4081 designation:
4082 designator-list =
4084 designator-list:
4085 designator
4086 designator-list designator
4088 designator:
4089 array-designator
4090 . identifier
4092 array-designator:
4093 [ constant-expression ]
4095 GNU extensions:
4097 initializer:
4100 designation:
4101 array-designator
4102 identifier :
4104 array-designator:
4105 [ constant-expression ... constant-expression ]
4107 Any expression without commas is accepted in the syntax for the
4108 constant-expressions, with non-constant expressions rejected later.
4110 This function is only used for top-level initializers; for nested
4111 ones, see c_parser_initval. */
4113 static struct c_expr
4114 c_parser_initializer (c_parser *parser)
4116 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
4117 return c_parser_braced_init (parser, NULL_TREE, false);
4118 else
4120 struct c_expr ret;
4121 location_t loc = c_parser_peek_token (parser)->location;
4122 ret = c_parser_expr_no_commas (parser, NULL);
4123 if (TREE_CODE (ret.value) != STRING_CST
4124 && TREE_CODE (ret.value) != COMPOUND_LITERAL_EXPR)
4125 ret = convert_lvalue_to_rvalue (loc, ret, true, true);
4126 return ret;
4130 /* Parse a braced initializer list. TYPE is the type specified for a
4131 compound literal, and NULL_TREE for other initializers and for
4132 nested braced lists. NESTED_P is true for nested braced lists,
4133 false for the list of a compound literal or the list that is the
4134 top-level initializer in a declaration. */
4136 static struct c_expr
4137 c_parser_braced_init (c_parser *parser, tree type, bool nested_p)
4139 struct c_expr ret;
4140 struct obstack braced_init_obstack;
4141 location_t brace_loc = c_parser_peek_token (parser)->location;
4142 gcc_obstack_init (&braced_init_obstack);
4143 gcc_assert (c_parser_next_token_is (parser, CPP_OPEN_BRACE));
4144 c_parser_consume_token (parser);
4145 if (nested_p)
4146 push_init_level (brace_loc, 0, &braced_init_obstack);
4147 else
4148 really_start_incremental_init (type);
4149 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
4151 pedwarn (brace_loc, OPT_Wpedantic, "ISO C forbids empty initializer braces");
4153 else
4155 /* Parse a non-empty initializer list, possibly with a trailing
4156 comma. */
4157 while (true)
4159 c_parser_initelt (parser, &braced_init_obstack);
4160 if (parser->error)
4161 break;
4162 if (c_parser_next_token_is (parser, CPP_COMMA))
4163 c_parser_consume_token (parser);
4164 else
4165 break;
4166 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
4167 break;
4170 if (c_parser_next_token_is_not (parser, CPP_CLOSE_BRACE))
4172 ret.value = error_mark_node;
4173 ret.original_code = ERROR_MARK;
4174 ret.original_type = NULL;
4175 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, "expected %<}%>");
4176 pop_init_level (brace_loc, 0, &braced_init_obstack);
4177 obstack_free (&braced_init_obstack, NULL);
4178 return ret;
4180 c_parser_consume_token (parser);
4181 ret = pop_init_level (brace_loc, 0, &braced_init_obstack);
4182 obstack_free (&braced_init_obstack, NULL);
4183 return ret;
4186 /* Parse a nested initializer, including designators. */
4188 static void
4189 c_parser_initelt (c_parser *parser, struct obstack * braced_init_obstack)
4191 /* Parse any designator or designator list. A single array
4192 designator may have the subsequent "=" omitted in GNU C, but a
4193 longer list or a structure member designator may not. */
4194 if (c_parser_next_token_is (parser, CPP_NAME)
4195 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
4197 /* Old-style structure member designator. */
4198 set_init_label (c_parser_peek_token (parser)->location,
4199 c_parser_peek_token (parser)->value,
4200 braced_init_obstack);
4201 /* Use the colon as the error location. */
4202 pedwarn (c_parser_peek_2nd_token (parser)->location, OPT_Wpedantic,
4203 "obsolete use of designated initializer with %<:%>");
4204 c_parser_consume_token (parser);
4205 c_parser_consume_token (parser);
4207 else
4209 /* des_seen is 0 if there have been no designators, 1 if there
4210 has been a single array designator and 2 otherwise. */
4211 int des_seen = 0;
4212 /* Location of a designator. */
4213 location_t des_loc = UNKNOWN_LOCATION; /* Quiet warning. */
4214 while (c_parser_next_token_is (parser, CPP_OPEN_SQUARE)
4215 || c_parser_next_token_is (parser, CPP_DOT))
4217 int des_prev = des_seen;
4218 if (!des_seen)
4219 des_loc = c_parser_peek_token (parser)->location;
4220 if (des_seen < 2)
4221 des_seen++;
4222 if (c_parser_next_token_is (parser, CPP_DOT))
4224 des_seen = 2;
4225 c_parser_consume_token (parser);
4226 if (c_parser_next_token_is (parser, CPP_NAME))
4228 set_init_label (des_loc, c_parser_peek_token (parser)->value,
4229 braced_init_obstack);
4230 c_parser_consume_token (parser);
4232 else
4234 struct c_expr init;
4235 init.value = error_mark_node;
4236 init.original_code = ERROR_MARK;
4237 init.original_type = NULL;
4238 c_parser_error (parser, "expected identifier");
4239 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
4240 process_init_element (input_location, init, false,
4241 braced_init_obstack);
4242 return;
4245 else
4247 tree first, second;
4248 location_t ellipsis_loc = UNKNOWN_LOCATION; /* Quiet warning. */
4249 location_t array_index_loc = UNKNOWN_LOCATION;
4250 /* ??? Following the old parser, [ objc-receiver
4251 objc-message-args ] is accepted as an initializer,
4252 being distinguished from a designator by what follows
4253 the first assignment expression inside the square
4254 brackets, but after a first array designator a
4255 subsequent square bracket is for Objective-C taken to
4256 start an expression, using the obsolete form of
4257 designated initializer without '=', rather than
4258 possibly being a second level of designation: in LALR
4259 terms, the '[' is shifted rather than reducing
4260 designator to designator-list. */
4261 if (des_prev == 1 && c_dialect_objc ())
4263 des_seen = des_prev;
4264 break;
4266 if (des_prev == 0 && c_dialect_objc ())
4268 /* This might be an array designator or an
4269 Objective-C message expression. If the former,
4270 continue parsing here; if the latter, parse the
4271 remainder of the initializer given the starting
4272 primary-expression. ??? It might make sense to
4273 distinguish when des_prev == 1 as well; see
4274 previous comment. */
4275 tree rec, args;
4276 struct c_expr mexpr;
4277 c_parser_consume_token (parser);
4278 if (c_parser_peek_token (parser)->type == CPP_NAME
4279 && ((c_parser_peek_token (parser)->id_kind
4280 == C_ID_TYPENAME)
4281 || (c_parser_peek_token (parser)->id_kind
4282 == C_ID_CLASSNAME)))
4284 /* Type name receiver. */
4285 tree id = c_parser_peek_token (parser)->value;
4286 c_parser_consume_token (parser);
4287 rec = objc_get_class_reference (id);
4288 goto parse_message_args;
4290 first = c_parser_expr_no_commas (parser, NULL).value;
4291 mark_exp_read (first);
4292 if (c_parser_next_token_is (parser, CPP_ELLIPSIS)
4293 || c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
4294 goto array_desig_after_first;
4295 /* Expression receiver. So far only one part
4296 without commas has been parsed; there might be
4297 more of the expression. */
4298 rec = first;
4299 while (c_parser_next_token_is (parser, CPP_COMMA))
4301 struct c_expr next;
4302 location_t comma_loc, exp_loc;
4303 comma_loc = c_parser_peek_token (parser)->location;
4304 c_parser_consume_token (parser);
4305 exp_loc = c_parser_peek_token (parser)->location;
4306 next = c_parser_expr_no_commas (parser, NULL);
4307 next = convert_lvalue_to_rvalue (exp_loc, next,
4308 true, true);
4309 rec = build_compound_expr (comma_loc, rec, next.value);
4311 parse_message_args:
4312 /* Now parse the objc-message-args. */
4313 args = c_parser_objc_message_args (parser);
4314 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
4315 "expected %<]%>");
4316 mexpr.value
4317 = objc_build_message_expr (rec, args);
4318 mexpr.original_code = ERROR_MARK;
4319 mexpr.original_type = NULL;
4320 /* Now parse and process the remainder of the
4321 initializer, starting with this message
4322 expression as a primary-expression. */
4323 c_parser_initval (parser, &mexpr, braced_init_obstack);
4324 return;
4326 c_parser_consume_token (parser);
4327 array_index_loc = c_parser_peek_token (parser)->location;
4328 first = c_parser_expr_no_commas (parser, NULL).value;
4329 mark_exp_read (first);
4330 array_desig_after_first:
4331 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
4333 ellipsis_loc = c_parser_peek_token (parser)->location;
4334 c_parser_consume_token (parser);
4335 second = c_parser_expr_no_commas (parser, NULL).value;
4336 mark_exp_read (second);
4338 else
4339 second = NULL_TREE;
4340 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
4342 c_parser_consume_token (parser);
4343 set_init_index (array_index_loc, first, second,
4344 braced_init_obstack);
4345 if (second)
4346 pedwarn (ellipsis_loc, OPT_Wpedantic,
4347 "ISO C forbids specifying range of elements to initialize");
4349 else
4350 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
4351 "expected %<]%>");
4354 if (des_seen >= 1)
4356 if (c_parser_next_token_is (parser, CPP_EQ))
4358 if (!flag_isoc99)
4359 pedwarn (des_loc, OPT_Wpedantic,
4360 "ISO C90 forbids specifying subobject to initialize");
4361 c_parser_consume_token (parser);
4363 else
4365 if (des_seen == 1)
4366 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
4367 "obsolete use of designated initializer without %<=%>");
4368 else
4370 struct c_expr init;
4371 init.value = error_mark_node;
4372 init.original_code = ERROR_MARK;
4373 init.original_type = NULL;
4374 c_parser_error (parser, "expected %<=%>");
4375 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
4376 process_init_element (input_location, init, false,
4377 braced_init_obstack);
4378 return;
4383 c_parser_initval (parser, NULL, braced_init_obstack);
4386 /* Parse a nested initializer; as c_parser_initializer but parses
4387 initializers within braced lists, after any designators have been
4388 applied. If AFTER is not NULL then it is an Objective-C message
4389 expression which is the primary-expression starting the
4390 initializer. */
4392 static void
4393 c_parser_initval (c_parser *parser, struct c_expr *after,
4394 struct obstack * braced_init_obstack)
4396 struct c_expr init;
4397 gcc_assert (!after || c_dialect_objc ());
4398 location_t loc = c_parser_peek_token (parser)->location;
4400 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE) && !after)
4401 init = c_parser_braced_init (parser, NULL_TREE, true);
4402 else
4404 init = c_parser_expr_no_commas (parser, after);
4405 if (init.value != NULL_TREE
4406 && TREE_CODE (init.value) != STRING_CST
4407 && TREE_CODE (init.value) != COMPOUND_LITERAL_EXPR)
4408 init = convert_lvalue_to_rvalue (loc, init, true, true);
4410 process_init_element (loc, init, false, braced_init_obstack);
4413 /* Parse a compound statement (possibly a function body) (C90 6.6.2,
4414 C99 6.8.2).
4416 compound-statement:
4417 { block-item-list[opt] }
4418 { label-declarations block-item-list }
4420 block-item-list:
4421 block-item
4422 block-item-list block-item
4424 block-item:
4425 nested-declaration
4426 statement
4428 nested-declaration:
4429 declaration
4431 GNU extensions:
4433 compound-statement:
4434 { label-declarations block-item-list }
4436 nested-declaration:
4437 __extension__ nested-declaration
4438 nested-function-definition
4440 label-declarations:
4441 label-declaration
4442 label-declarations label-declaration
4444 label-declaration:
4445 __label__ identifier-list ;
4447 Allowing the mixing of declarations and code is new in C99. The
4448 GNU syntax also permits (not shown above) labels at the end of
4449 compound statements, which yield an error. We don't allow labels
4450 on declarations; this might seem like a natural extension, but
4451 there would be a conflict between attributes on the label and
4452 prefix attributes on the declaration. ??? The syntax follows the
4453 old parser in requiring something after label declarations.
4454 Although they are erroneous if the labels declared aren't defined,
4455 is it useful for the syntax to be this way?
4457 OpenMP:
4459 block-item:
4460 openmp-directive
4462 openmp-directive:
4463 barrier-directive
4464 flush-directive
4465 taskwait-directive
4466 taskyield-directive
4467 cancel-directive
4468 cancellation-point-directive */
4470 static tree
4471 c_parser_compound_statement (c_parser *parser)
4473 tree stmt;
4474 location_t brace_loc;
4475 brace_loc = c_parser_peek_token (parser)->location;
4476 if (!c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
4478 /* Ensure a scope is entered and left anyway to avoid confusion
4479 if we have just prepared to enter a function body. */
4480 stmt = c_begin_compound_stmt (true);
4481 c_end_compound_stmt (brace_loc, stmt, true);
4482 return error_mark_node;
4484 stmt = c_begin_compound_stmt (true);
4485 c_parser_compound_statement_nostart (parser);
4487 /* If the compound stmt contains array notations, then we expand them. */
4488 if (flag_cilkplus && contains_array_notation_expr (stmt))
4489 stmt = expand_array_notation_exprs (stmt);
4490 return c_end_compound_stmt (brace_loc, stmt, true);
4493 /* Parse a compound statement except for the opening brace. This is
4494 used for parsing both compound statements and statement expressions
4495 (which follow different paths to handling the opening). */
4497 static void
4498 c_parser_compound_statement_nostart (c_parser *parser)
4500 bool last_stmt = false;
4501 bool last_label = false;
4502 bool save_valid_for_pragma = valid_location_for_stdc_pragma_p ();
4503 location_t label_loc = UNKNOWN_LOCATION; /* Quiet warning. */
4504 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
4506 c_parser_consume_token (parser);
4507 return;
4509 mark_valid_location_for_stdc_pragma (true);
4510 if (c_parser_next_token_is_keyword (parser, RID_LABEL))
4512 /* Read zero or more forward-declarations for labels that nested
4513 functions can jump to. */
4514 mark_valid_location_for_stdc_pragma (false);
4515 while (c_parser_next_token_is_keyword (parser, RID_LABEL))
4517 label_loc = c_parser_peek_token (parser)->location;
4518 c_parser_consume_token (parser);
4519 /* Any identifiers, including those declared as type names,
4520 are OK here. */
4521 while (true)
4523 tree label;
4524 if (c_parser_next_token_is_not (parser, CPP_NAME))
4526 c_parser_error (parser, "expected identifier");
4527 break;
4529 label
4530 = declare_label (c_parser_peek_token (parser)->value);
4531 C_DECLARED_LABEL_FLAG (label) = 1;
4532 add_stmt (build_stmt (label_loc, DECL_EXPR, label));
4533 c_parser_consume_token (parser);
4534 if (c_parser_next_token_is (parser, CPP_COMMA))
4535 c_parser_consume_token (parser);
4536 else
4537 break;
4539 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
4541 pedwarn (label_loc, OPT_Wpedantic, "ISO C forbids label declarations");
4543 /* We must now have at least one statement, label or declaration. */
4544 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
4546 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
4547 c_parser_error (parser, "expected declaration or statement");
4548 c_parser_consume_token (parser);
4549 return;
4551 while (c_parser_next_token_is_not (parser, CPP_CLOSE_BRACE))
4553 location_t loc = c_parser_peek_token (parser)->location;
4554 if (c_parser_next_token_is_keyword (parser, RID_CASE)
4555 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
4556 || (c_parser_next_token_is (parser, CPP_NAME)
4557 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
4559 if (c_parser_next_token_is_keyword (parser, RID_CASE))
4560 label_loc = c_parser_peek_2nd_token (parser)->location;
4561 else
4562 label_loc = c_parser_peek_token (parser)->location;
4563 last_label = true;
4564 last_stmt = false;
4565 mark_valid_location_for_stdc_pragma (false);
4566 c_parser_label (parser);
4568 else if (!last_label
4569 && c_parser_next_tokens_start_declaration (parser))
4571 last_label = false;
4572 mark_valid_location_for_stdc_pragma (false);
4573 c_parser_declaration_or_fndef (parser, true, true, true, true,
4574 true, NULL, vNULL);
4575 if (last_stmt)
4576 pedwarn_c90 (loc,
4577 (pedantic && !flag_isoc99)
4578 ? OPT_Wpedantic
4579 : OPT_Wdeclaration_after_statement,
4580 "ISO C90 forbids mixed declarations and code");
4581 last_stmt = false;
4583 else if (!last_label
4584 && c_parser_next_token_is_keyword (parser, RID_EXTENSION))
4586 /* __extension__ can start a declaration, but is also an
4587 unary operator that can start an expression. Consume all
4588 but the last of a possible series of __extension__ to
4589 determine which. */
4590 while (c_parser_peek_2nd_token (parser)->type == CPP_KEYWORD
4591 && (c_parser_peek_2nd_token (parser)->keyword
4592 == RID_EXTENSION))
4593 c_parser_consume_token (parser);
4594 if (c_token_starts_declaration (c_parser_peek_2nd_token (parser)))
4596 int ext;
4597 ext = disable_extension_diagnostics ();
4598 c_parser_consume_token (parser);
4599 last_label = false;
4600 mark_valid_location_for_stdc_pragma (false);
4601 c_parser_declaration_or_fndef (parser, true, true, true, true,
4602 true, NULL, vNULL);
4603 /* Following the old parser, __extension__ does not
4604 disable this diagnostic. */
4605 restore_extension_diagnostics (ext);
4606 if (last_stmt)
4607 pedwarn_c90 (loc, (pedantic && !flag_isoc99)
4608 ? OPT_Wpedantic
4609 : OPT_Wdeclaration_after_statement,
4610 "ISO C90 forbids mixed declarations and code");
4611 last_stmt = false;
4613 else
4614 goto statement;
4616 else if (c_parser_next_token_is (parser, CPP_PRAGMA))
4618 /* External pragmas, and some omp pragmas, are not associated
4619 with regular c code, and so are not to be considered statements
4620 syntactically. This ensures that the user doesn't put them
4621 places that would turn into syntax errors if the directive
4622 were ignored. */
4623 if (c_parser_pragma (parser, pragma_compound))
4624 last_label = false, last_stmt = true;
4626 else if (c_parser_next_token_is (parser, CPP_EOF))
4628 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
4629 c_parser_error (parser, "expected declaration or statement");
4630 return;
4632 else if (c_parser_next_token_is_keyword (parser, RID_ELSE))
4634 if (parser->in_if_block)
4636 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
4637 error_at (loc, """expected %<}%> before %<else%>");
4638 return;
4640 else
4642 error_at (loc, "%<else%> without a previous %<if%>");
4643 c_parser_consume_token (parser);
4644 continue;
4647 else
4649 statement:
4650 last_label = false;
4651 last_stmt = true;
4652 mark_valid_location_for_stdc_pragma (false);
4653 c_parser_statement_after_labels (parser);
4656 parser->error = false;
4658 if (last_label)
4659 error_at (label_loc, "label at end of compound statement");
4660 c_parser_consume_token (parser);
4661 /* Restore the value we started with. */
4662 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
4665 /* Parse a label (C90 6.6.1, C99 6.8.1).
4667 label:
4668 identifier : attributes[opt]
4669 case constant-expression :
4670 default :
4672 GNU extensions:
4674 label:
4675 case constant-expression ... constant-expression :
4677 The use of attributes on labels is a GNU extension. The syntax in
4678 GNU C accepts any expressions without commas, non-constant
4679 expressions being rejected later. */
4681 static void
4682 c_parser_label (c_parser *parser)
4684 location_t loc1 = c_parser_peek_token (parser)->location;
4685 tree label = NULL_TREE;
4686 if (c_parser_next_token_is_keyword (parser, RID_CASE))
4688 tree exp1, exp2;
4689 c_parser_consume_token (parser);
4690 exp1 = c_parser_expr_no_commas (parser, NULL).value;
4691 if (c_parser_next_token_is (parser, CPP_COLON))
4693 c_parser_consume_token (parser);
4694 label = do_case (loc1, exp1, NULL_TREE);
4696 else if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
4698 c_parser_consume_token (parser);
4699 exp2 = c_parser_expr_no_commas (parser, NULL).value;
4700 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
4701 label = do_case (loc1, exp1, exp2);
4703 else
4704 c_parser_error (parser, "expected %<:%> or %<...%>");
4706 else if (c_parser_next_token_is_keyword (parser, RID_DEFAULT))
4708 c_parser_consume_token (parser);
4709 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
4710 label = do_case (loc1, NULL_TREE, NULL_TREE);
4712 else
4714 tree name = c_parser_peek_token (parser)->value;
4715 tree tlab;
4716 tree attrs;
4717 location_t loc2 = c_parser_peek_token (parser)->location;
4718 gcc_assert (c_parser_next_token_is (parser, CPP_NAME));
4719 c_parser_consume_token (parser);
4720 gcc_assert (c_parser_next_token_is (parser, CPP_COLON));
4721 c_parser_consume_token (parser);
4722 attrs = c_parser_attributes (parser);
4723 tlab = define_label (loc2, name);
4724 if (tlab)
4726 decl_attributes (&tlab, attrs, 0);
4727 label = add_stmt (build_stmt (loc1, LABEL_EXPR, tlab));
4730 if (label)
4732 if (c_parser_next_tokens_start_declaration (parser))
4734 error_at (c_parser_peek_token (parser)->location,
4735 "a label can only be part of a statement and "
4736 "a declaration is not a statement");
4737 c_parser_declaration_or_fndef (parser, /*fndef_ok*/ false,
4738 /*static_assert_ok*/ true,
4739 /*empty_ok*/ true, /*nested*/ true,
4740 /*start_attr_ok*/ true, NULL,
4741 vNULL);
4746 /* Parse a statement (C90 6.6, C99 6.8).
4748 statement:
4749 labeled-statement
4750 compound-statement
4751 expression-statement
4752 selection-statement
4753 iteration-statement
4754 jump-statement
4756 labeled-statement:
4757 label statement
4759 expression-statement:
4760 expression[opt] ;
4762 selection-statement:
4763 if-statement
4764 switch-statement
4766 iteration-statement:
4767 while-statement
4768 do-statement
4769 for-statement
4771 jump-statement:
4772 goto identifier ;
4773 continue ;
4774 break ;
4775 return expression[opt] ;
4777 GNU extensions:
4779 statement:
4780 asm-statement
4782 jump-statement:
4783 goto * expression ;
4785 Objective-C:
4787 statement:
4788 objc-throw-statement
4789 objc-try-catch-statement
4790 objc-synchronized-statement
4792 objc-throw-statement:
4793 @throw expression ;
4794 @throw ;
4796 OpenMP:
4798 statement:
4799 openmp-construct
4801 openmp-construct:
4802 parallel-construct
4803 for-construct
4804 simd-construct
4805 for-simd-construct
4806 sections-construct
4807 single-construct
4808 parallel-for-construct
4809 parallel-for-simd-construct
4810 parallel-sections-construct
4811 master-construct
4812 critical-construct
4813 atomic-construct
4814 ordered-construct
4816 parallel-construct:
4817 parallel-directive structured-block
4819 for-construct:
4820 for-directive iteration-statement
4822 simd-construct:
4823 simd-directive iteration-statements
4825 for-simd-construct:
4826 for-simd-directive iteration-statements
4828 sections-construct:
4829 sections-directive section-scope
4831 single-construct:
4832 single-directive structured-block
4834 parallel-for-construct:
4835 parallel-for-directive iteration-statement
4837 parallel-for-simd-construct:
4838 parallel-for-simd-directive iteration-statement
4840 parallel-sections-construct:
4841 parallel-sections-directive section-scope
4843 master-construct:
4844 master-directive structured-block
4846 critical-construct:
4847 critical-directive structured-block
4849 atomic-construct:
4850 atomic-directive expression-statement
4852 ordered-construct:
4853 ordered-directive structured-block
4855 Transactional Memory:
4857 statement:
4858 transaction-statement
4859 transaction-cancel-statement
4862 static void
4863 c_parser_statement (c_parser *parser)
4865 while (c_parser_next_token_is_keyword (parser, RID_CASE)
4866 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
4867 || (c_parser_next_token_is (parser, CPP_NAME)
4868 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
4869 c_parser_label (parser);
4870 c_parser_statement_after_labels (parser);
4873 /* Parse a statement, other than a labeled statement. */
4875 static void
4876 c_parser_statement_after_labels (c_parser *parser)
4878 location_t loc = c_parser_peek_token (parser)->location;
4879 tree stmt = NULL_TREE;
4880 bool in_if_block = parser->in_if_block;
4881 parser->in_if_block = false;
4882 switch (c_parser_peek_token (parser)->type)
4884 case CPP_OPEN_BRACE:
4885 add_stmt (c_parser_compound_statement (parser));
4886 break;
4887 case CPP_KEYWORD:
4888 switch (c_parser_peek_token (parser)->keyword)
4890 case RID_IF:
4891 c_parser_if_statement (parser);
4892 break;
4893 case RID_SWITCH:
4894 c_parser_switch_statement (parser);
4895 break;
4896 case RID_WHILE:
4897 c_parser_while_statement (parser, false);
4898 break;
4899 case RID_DO:
4900 c_parser_do_statement (parser, false);
4901 break;
4902 case RID_FOR:
4903 c_parser_for_statement (parser, false);
4904 break;
4905 case RID_CILK_SYNC:
4906 c_parser_consume_token (parser);
4907 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
4908 if (!flag_cilkplus)
4909 error_at (loc, "-fcilkplus must be enabled to use %<_Cilk_sync%>");
4910 else
4911 add_stmt (build_cilk_sync ());
4912 break;
4913 case RID_GOTO:
4914 c_parser_consume_token (parser);
4915 if (c_parser_next_token_is (parser, CPP_NAME))
4917 stmt = c_finish_goto_label (loc,
4918 c_parser_peek_token (parser)->value);
4919 c_parser_consume_token (parser);
4921 else if (c_parser_next_token_is (parser, CPP_MULT))
4923 struct c_expr val;
4925 c_parser_consume_token (parser);
4926 val = c_parser_expression (parser);
4927 val = convert_lvalue_to_rvalue (loc, val, false, true);
4928 stmt = c_finish_goto_ptr (loc, val.value);
4930 else
4931 c_parser_error (parser, "expected identifier or %<*%>");
4932 goto expect_semicolon;
4933 case RID_CONTINUE:
4934 c_parser_consume_token (parser);
4935 stmt = c_finish_bc_stmt (loc, &c_cont_label, false);
4936 goto expect_semicolon;
4937 case RID_BREAK:
4938 c_parser_consume_token (parser);
4939 stmt = c_finish_bc_stmt (loc, &c_break_label, true);
4940 goto expect_semicolon;
4941 case RID_RETURN:
4942 c_parser_consume_token (parser);
4943 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
4945 stmt = c_finish_return (loc, NULL_TREE, NULL_TREE);
4946 c_parser_consume_token (parser);
4948 else
4950 struct c_expr expr = c_parser_expression_conv (parser);
4951 mark_exp_read (expr.value);
4952 stmt = c_finish_return (loc, expr.value, expr.original_type);
4953 goto expect_semicolon;
4955 break;
4956 case RID_ASM:
4957 stmt = c_parser_asm_statement (parser);
4958 break;
4959 case RID_TRANSACTION_ATOMIC:
4960 case RID_TRANSACTION_RELAXED:
4961 stmt = c_parser_transaction (parser,
4962 c_parser_peek_token (parser)->keyword);
4963 break;
4964 case RID_TRANSACTION_CANCEL:
4965 stmt = c_parser_transaction_cancel (parser);
4966 goto expect_semicolon;
4967 case RID_AT_THROW:
4968 gcc_assert (c_dialect_objc ());
4969 c_parser_consume_token (parser);
4970 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
4972 stmt = objc_build_throw_stmt (loc, NULL_TREE);
4973 c_parser_consume_token (parser);
4975 else
4977 struct c_expr expr = c_parser_expression (parser);
4978 expr = convert_lvalue_to_rvalue (loc, expr, false, false);
4979 expr.value = c_fully_fold (expr.value, false, NULL);
4980 stmt = objc_build_throw_stmt (loc, expr.value);
4981 goto expect_semicolon;
4983 break;
4984 case RID_AT_TRY:
4985 gcc_assert (c_dialect_objc ());
4986 c_parser_objc_try_catch_finally_statement (parser);
4987 break;
4988 case RID_AT_SYNCHRONIZED:
4989 gcc_assert (c_dialect_objc ());
4990 c_parser_objc_synchronized_statement (parser);
4991 break;
4992 default:
4993 goto expr_stmt;
4995 break;
4996 case CPP_SEMICOLON:
4997 c_parser_consume_token (parser);
4998 break;
4999 case CPP_CLOSE_PAREN:
5000 case CPP_CLOSE_SQUARE:
5001 /* Avoid infinite loop in error recovery:
5002 c_parser_skip_until_found stops at a closing nesting
5003 delimiter without consuming it, but here we need to consume
5004 it to proceed further. */
5005 c_parser_error (parser, "expected statement");
5006 c_parser_consume_token (parser);
5007 break;
5008 case CPP_PRAGMA:
5009 c_parser_pragma (parser, pragma_stmt);
5010 break;
5011 default:
5012 expr_stmt:
5013 stmt = c_finish_expr_stmt (loc, c_parser_expression_conv (parser).value);
5014 expect_semicolon:
5015 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
5016 break;
5018 /* Two cases cannot and do not have line numbers associated: If stmt
5019 is degenerate, such as "2;", then stmt is an INTEGER_CST, which
5020 cannot hold line numbers. But that's OK because the statement
5021 will either be changed to a MODIFY_EXPR during gimplification of
5022 the statement expr, or discarded. If stmt was compound, but
5023 without new variables, we will have skipped the creation of a
5024 BIND and will have a bare STATEMENT_LIST. But that's OK because
5025 (recursively) all of the component statements should already have
5026 line numbers assigned. ??? Can we discard no-op statements
5027 earlier? */
5028 if (CAN_HAVE_LOCATION_P (stmt)
5029 && EXPR_LOCATION (stmt) == UNKNOWN_LOCATION)
5030 SET_EXPR_LOCATION (stmt, loc);
5032 parser->in_if_block = in_if_block;
5035 /* Parse the condition from an if, do, while or for statements. */
5037 static tree
5038 c_parser_condition (c_parser *parser)
5040 location_t loc = c_parser_peek_token (parser)->location;
5041 tree cond;
5042 cond = c_parser_expression_conv (parser).value;
5043 cond = c_objc_common_truthvalue_conversion (loc, cond);
5044 cond = c_fully_fold (cond, false, NULL);
5045 if (warn_sequence_point)
5046 verify_sequence_points (cond);
5047 return cond;
5050 /* Parse a parenthesized condition from an if, do or while statement.
5052 condition:
5053 ( expression )
5055 static tree
5056 c_parser_paren_condition (c_parser *parser)
5058 tree cond;
5059 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5060 return error_mark_node;
5061 cond = c_parser_condition (parser);
5062 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
5063 return cond;
5066 /* Parse a statement which is a block in C99. */
5068 static tree
5069 c_parser_c99_block_statement (c_parser *parser)
5071 tree block = c_begin_compound_stmt (flag_isoc99);
5072 location_t loc = c_parser_peek_token (parser)->location;
5073 c_parser_statement (parser);
5074 return c_end_compound_stmt (loc, block, flag_isoc99);
5077 /* Parse the body of an if statement. This is just parsing a
5078 statement but (a) it is a block in C99, (b) we track whether the
5079 body is an if statement for the sake of -Wparentheses warnings, (c)
5080 we handle an empty body specially for the sake of -Wempty-body
5081 warnings, and (d) we call parser_compound_statement directly
5082 because c_parser_statement_after_labels resets
5083 parser->in_if_block. */
5085 static tree
5086 c_parser_if_body (c_parser *parser, bool *if_p)
5088 tree block = c_begin_compound_stmt (flag_isoc99);
5089 location_t body_loc = c_parser_peek_token (parser)->location;
5090 while (c_parser_next_token_is_keyword (parser, RID_CASE)
5091 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
5092 || (c_parser_next_token_is (parser, CPP_NAME)
5093 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
5094 c_parser_label (parser);
5095 *if_p = c_parser_next_token_is_keyword (parser, RID_IF);
5096 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5098 location_t loc = c_parser_peek_token (parser)->location;
5099 add_stmt (build_empty_stmt (loc));
5100 c_parser_consume_token (parser);
5101 if (!c_parser_next_token_is_keyword (parser, RID_ELSE))
5102 warning_at (loc, OPT_Wempty_body,
5103 "suggest braces around empty body in an %<if%> statement");
5105 else if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
5106 add_stmt (c_parser_compound_statement (parser));
5107 else
5108 c_parser_statement_after_labels (parser);
5109 return c_end_compound_stmt (body_loc, block, flag_isoc99);
5112 /* Parse the else body of an if statement. This is just parsing a
5113 statement but (a) it is a block in C99, (b) we handle an empty body
5114 specially for the sake of -Wempty-body warnings. */
5116 static tree
5117 c_parser_else_body (c_parser *parser)
5119 location_t else_loc = c_parser_peek_token (parser)->location;
5120 tree block = c_begin_compound_stmt (flag_isoc99);
5121 while (c_parser_next_token_is_keyword (parser, RID_CASE)
5122 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
5123 || (c_parser_next_token_is (parser, CPP_NAME)
5124 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
5125 c_parser_label (parser);
5126 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5128 location_t loc = c_parser_peek_token (parser)->location;
5129 warning_at (loc,
5130 OPT_Wempty_body,
5131 "suggest braces around empty body in an %<else%> statement");
5132 add_stmt (build_empty_stmt (loc));
5133 c_parser_consume_token (parser);
5135 else
5136 c_parser_statement_after_labels (parser);
5137 return c_end_compound_stmt (else_loc, block, flag_isoc99);
5140 /* Parse an if statement (C90 6.6.4, C99 6.8.4).
5142 if-statement:
5143 if ( expression ) statement
5144 if ( expression ) statement else statement
5147 static void
5148 c_parser_if_statement (c_parser *parser)
5150 tree block;
5151 location_t loc;
5152 tree cond;
5153 bool first_if = false;
5154 tree first_body, second_body;
5155 bool in_if_block;
5156 tree if_stmt;
5158 gcc_assert (c_parser_next_token_is_keyword (parser, RID_IF));
5159 c_parser_consume_token (parser);
5160 block = c_begin_compound_stmt (flag_isoc99);
5161 loc = c_parser_peek_token (parser)->location;
5162 cond = c_parser_paren_condition (parser);
5163 in_if_block = parser->in_if_block;
5164 parser->in_if_block = true;
5165 first_body = c_parser_if_body (parser, &first_if);
5166 parser->in_if_block = in_if_block;
5167 if (c_parser_next_token_is_keyword (parser, RID_ELSE))
5169 c_parser_consume_token (parser);
5170 second_body = c_parser_else_body (parser);
5172 else
5173 second_body = NULL_TREE;
5174 c_finish_if_stmt (loc, cond, first_body, second_body, first_if);
5175 if_stmt = c_end_compound_stmt (loc, block, flag_isoc99);
5177 /* If the if statement contains array notations, then we expand them. */
5178 if (flag_cilkplus && contains_array_notation_expr (if_stmt))
5179 if_stmt = fix_conditional_array_notations (if_stmt);
5180 add_stmt (if_stmt);
5183 /* Parse a switch statement (C90 6.6.4, C99 6.8.4).
5185 switch-statement:
5186 switch (expression) statement
5189 static void
5190 c_parser_switch_statement (c_parser *parser)
5192 struct c_expr ce;
5193 tree block, expr, body, save_break;
5194 location_t switch_loc = c_parser_peek_token (parser)->location;
5195 location_t switch_cond_loc;
5196 gcc_assert (c_parser_next_token_is_keyword (parser, RID_SWITCH));
5197 c_parser_consume_token (parser);
5198 block = c_begin_compound_stmt (flag_isoc99);
5199 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5201 switch_cond_loc = c_parser_peek_token (parser)->location;
5202 ce = c_parser_expression (parser);
5203 ce = convert_lvalue_to_rvalue (switch_cond_loc, ce, true, false);
5204 expr = ce.value;
5205 if (flag_cilkplus && contains_array_notation_expr (expr))
5207 error_at (switch_cond_loc,
5208 "array notations cannot be used as a condition for switch "
5209 "statement");
5210 expr = error_mark_node;
5212 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
5214 else
5216 switch_cond_loc = UNKNOWN_LOCATION;
5217 expr = error_mark_node;
5219 c_start_case (switch_loc, switch_cond_loc, expr);
5220 save_break = c_break_label;
5221 c_break_label = NULL_TREE;
5222 body = c_parser_c99_block_statement (parser);
5223 c_finish_case (body);
5224 if (c_break_label)
5226 location_t here = c_parser_peek_token (parser)->location;
5227 tree t = build1 (LABEL_EXPR, void_type_node, c_break_label);
5228 SET_EXPR_LOCATION (t, here);
5229 add_stmt (t);
5231 c_break_label = save_break;
5232 add_stmt (c_end_compound_stmt (switch_loc, block, flag_isoc99));
5235 /* Parse a while statement (C90 6.6.5, C99 6.8.5).
5237 while-statement:
5238 while (expression) statement
5241 static void
5242 c_parser_while_statement (c_parser *parser, bool ivdep)
5244 tree block, cond, body, save_break, save_cont;
5245 location_t loc;
5246 gcc_assert (c_parser_next_token_is_keyword (parser, RID_WHILE));
5247 c_parser_consume_token (parser);
5248 block = c_begin_compound_stmt (flag_isoc99);
5249 loc = c_parser_peek_token (parser)->location;
5250 cond = c_parser_paren_condition (parser);
5251 if (flag_cilkplus && contains_array_notation_expr (cond))
5253 error_at (loc, "array notations cannot be used as a condition for while "
5254 "statement");
5255 cond = error_mark_node;
5258 if (ivdep && cond != error_mark_node)
5259 cond = build2 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
5260 build_int_cst (integer_type_node,
5261 annot_expr_ivdep_kind));
5262 save_break = c_break_label;
5263 c_break_label = NULL_TREE;
5264 save_cont = c_cont_label;
5265 c_cont_label = NULL_TREE;
5266 body = c_parser_c99_block_statement (parser);
5267 c_finish_loop (loc, cond, NULL, body, c_break_label, c_cont_label, true);
5268 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99));
5269 c_break_label = save_break;
5270 c_cont_label = save_cont;
5273 /* Parse a do statement (C90 6.6.5, C99 6.8.5).
5275 do-statement:
5276 do statement while ( expression ) ;
5279 static void
5280 c_parser_do_statement (c_parser *parser, bool ivdep)
5282 tree block, cond, body, save_break, save_cont, new_break, new_cont;
5283 location_t loc;
5284 gcc_assert (c_parser_next_token_is_keyword (parser, RID_DO));
5285 c_parser_consume_token (parser);
5286 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5287 warning_at (c_parser_peek_token (parser)->location,
5288 OPT_Wempty_body,
5289 "suggest braces around empty body in %<do%> statement");
5290 block = c_begin_compound_stmt (flag_isoc99);
5291 loc = c_parser_peek_token (parser)->location;
5292 save_break = c_break_label;
5293 c_break_label = NULL_TREE;
5294 save_cont = c_cont_label;
5295 c_cont_label = NULL_TREE;
5296 body = c_parser_c99_block_statement (parser);
5297 c_parser_require_keyword (parser, RID_WHILE, "expected %<while%>");
5298 new_break = c_break_label;
5299 c_break_label = save_break;
5300 new_cont = c_cont_label;
5301 c_cont_label = save_cont;
5302 cond = c_parser_paren_condition (parser);
5303 if (flag_cilkplus && contains_array_notation_expr (cond))
5305 error_at (loc, "array notations cannot be used as a condition for a "
5306 "do-while statement");
5307 cond = error_mark_node;
5309 if (ivdep && cond != error_mark_node)
5310 cond = build2 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
5311 build_int_cst (integer_type_node,
5312 annot_expr_ivdep_kind));
5313 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
5314 c_parser_skip_to_end_of_block_or_statement (parser);
5315 c_finish_loop (loc, cond, NULL, body, new_break, new_cont, false);
5316 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99));
5319 /* Parse a for statement (C90 6.6.5, C99 6.8.5).
5321 for-statement:
5322 for ( expression[opt] ; expression[opt] ; expression[opt] ) statement
5323 for ( nested-declaration expression[opt] ; expression[opt] ) statement
5325 The form with a declaration is new in C99.
5327 ??? In accordance with the old parser, the declaration may be a
5328 nested function, which is then rejected in check_for_loop_decls,
5329 but does it make any sense for this to be included in the grammar?
5330 Note in particular that the nested function does not include a
5331 trailing ';', whereas the "declaration" production includes one.
5332 Also, can we reject bad declarations earlier and cheaper than
5333 check_for_loop_decls?
5335 In Objective-C, there are two additional variants:
5337 foreach-statement:
5338 for ( expression in expresssion ) statement
5339 for ( declaration in expression ) statement
5341 This is inconsistent with C, because the second variant is allowed
5342 even if c99 is not enabled.
5344 The rest of the comment documents these Objective-C foreach-statement.
5346 Here is the canonical example of the first variant:
5347 for (object in array) { do something with object }
5348 we call the first expression ("object") the "object_expression" and
5349 the second expression ("array") the "collection_expression".
5350 object_expression must be an lvalue of type "id" (a generic Objective-C
5351 object) because the loop works by assigning to object_expression the
5352 various objects from the collection_expression. collection_expression
5353 must evaluate to something of type "id" which responds to the method
5354 countByEnumeratingWithState:objects:count:.
5356 The canonical example of the second variant is:
5357 for (id object in array) { do something with object }
5358 which is completely equivalent to
5360 id object;
5361 for (object in array) { do something with object }
5363 Note that initizializing 'object' in some way (eg, "for ((object =
5364 xxx) in array) { do something with object }") is possibly
5365 technically valid, but completely pointless as 'object' will be
5366 assigned to something else as soon as the loop starts. We should
5367 most likely reject it (TODO).
5369 The beginning of the Objective-C foreach-statement looks exactly
5370 like the beginning of the for-statement, and we can tell it is a
5371 foreach-statement only because the initial declaration or
5372 expression is terminated by 'in' instead of ';'.
5375 static void
5376 c_parser_for_statement (c_parser *parser, bool ivdep)
5378 tree block, cond, incr, save_break, save_cont, body;
5379 /* The following are only used when parsing an ObjC foreach statement. */
5380 tree object_expression;
5381 /* Silence the bogus uninitialized warning. */
5382 tree collection_expression = NULL;
5383 location_t loc = c_parser_peek_token (parser)->location;
5384 location_t for_loc = c_parser_peek_token (parser)->location;
5385 bool is_foreach_statement = false;
5386 gcc_assert (c_parser_next_token_is_keyword (parser, RID_FOR));
5387 c_parser_consume_token (parser);
5388 /* Open a compound statement in Objective-C as well, just in case this is
5389 as foreach expression. */
5390 block = c_begin_compound_stmt (flag_isoc99 || c_dialect_objc ());
5391 cond = error_mark_node;
5392 incr = error_mark_node;
5393 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5395 /* Parse the initialization declaration or expression. */
5396 object_expression = error_mark_node;
5397 parser->objc_could_be_foreach_context = c_dialect_objc ();
5398 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5400 parser->objc_could_be_foreach_context = false;
5401 c_parser_consume_token (parser);
5402 c_finish_expr_stmt (loc, NULL_TREE);
5404 else if (c_parser_next_tokens_start_declaration (parser))
5406 c_parser_declaration_or_fndef (parser, true, true, true, true, true,
5407 &object_expression, vNULL);
5408 parser->objc_could_be_foreach_context = false;
5410 if (c_parser_next_token_is_keyword (parser, RID_IN))
5412 c_parser_consume_token (parser);
5413 is_foreach_statement = true;
5414 if (check_for_loop_decls (for_loc, true) == NULL_TREE)
5415 c_parser_error (parser, "multiple iterating variables in fast enumeration");
5417 else
5418 check_for_loop_decls (for_loc, flag_isoc99);
5420 else if (c_parser_next_token_is_keyword (parser, RID_EXTENSION))
5422 /* __extension__ can start a declaration, but is also an
5423 unary operator that can start an expression. Consume all
5424 but the last of a possible series of __extension__ to
5425 determine which. */
5426 while (c_parser_peek_2nd_token (parser)->type == CPP_KEYWORD
5427 && (c_parser_peek_2nd_token (parser)->keyword
5428 == RID_EXTENSION))
5429 c_parser_consume_token (parser);
5430 if (c_token_starts_declaration (c_parser_peek_2nd_token (parser)))
5432 int ext;
5433 ext = disable_extension_diagnostics ();
5434 c_parser_consume_token (parser);
5435 c_parser_declaration_or_fndef (parser, true, true, true, true,
5436 true, &object_expression, vNULL);
5437 parser->objc_could_be_foreach_context = false;
5439 restore_extension_diagnostics (ext);
5440 if (c_parser_next_token_is_keyword (parser, RID_IN))
5442 c_parser_consume_token (parser);
5443 is_foreach_statement = true;
5444 if (check_for_loop_decls (for_loc, true) == NULL_TREE)
5445 c_parser_error (parser, "multiple iterating variables in fast enumeration");
5447 else
5448 check_for_loop_decls (for_loc, flag_isoc99);
5450 else
5451 goto init_expr;
5453 else
5455 init_expr:
5457 struct c_expr ce;
5458 tree init_expression;
5459 ce = c_parser_expression (parser);
5460 init_expression = ce.value;
5461 parser->objc_could_be_foreach_context = false;
5462 if (c_parser_next_token_is_keyword (parser, RID_IN))
5464 c_parser_consume_token (parser);
5465 is_foreach_statement = true;
5466 if (! lvalue_p (init_expression))
5467 c_parser_error (parser, "invalid iterating variable in fast enumeration");
5468 object_expression = c_fully_fold (init_expression, false, NULL);
5470 else
5472 ce = convert_lvalue_to_rvalue (loc, ce, true, false);
5473 init_expression = ce.value;
5474 c_finish_expr_stmt (loc, init_expression);
5475 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
5479 /* Parse the loop condition. In the case of a foreach
5480 statement, there is no loop condition. */
5481 gcc_assert (!parser->objc_could_be_foreach_context);
5482 if (!is_foreach_statement)
5484 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5486 if (ivdep)
5488 c_parser_error (parser, "missing loop condition in loop with "
5489 "%<GCC ivdep%> pragma");
5490 cond = error_mark_node;
5492 else
5494 c_parser_consume_token (parser);
5495 cond = NULL_TREE;
5498 else
5500 cond = c_parser_condition (parser);
5501 if (flag_cilkplus && contains_array_notation_expr (cond))
5503 error_at (loc, "array notations cannot be used in a "
5504 "condition for a for-loop");
5505 cond = error_mark_node;
5507 c_parser_skip_until_found (parser, CPP_SEMICOLON,
5508 "expected %<;%>");
5510 if (ivdep && cond != error_mark_node)
5511 cond = build2 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
5512 build_int_cst (integer_type_node,
5513 annot_expr_ivdep_kind));
5515 /* Parse the increment expression (the third expression in a
5516 for-statement). In the case of a foreach-statement, this is
5517 the expression that follows the 'in'. */
5518 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
5520 if (is_foreach_statement)
5522 c_parser_error (parser, "missing collection in fast enumeration");
5523 collection_expression = error_mark_node;
5525 else
5526 incr = c_process_expr_stmt (loc, NULL_TREE);
5528 else
5530 if (is_foreach_statement)
5531 collection_expression = c_fully_fold (c_parser_expression (parser).value,
5532 false, NULL);
5533 else
5535 struct c_expr ce = c_parser_expression (parser);
5536 ce = convert_lvalue_to_rvalue (loc, ce, true, false);
5537 incr = c_process_expr_stmt (loc, ce.value);
5540 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
5542 save_break = c_break_label;
5543 c_break_label = NULL_TREE;
5544 save_cont = c_cont_label;
5545 c_cont_label = NULL_TREE;
5546 body = c_parser_c99_block_statement (parser);
5547 if (is_foreach_statement)
5548 objc_finish_foreach_loop (loc, object_expression, collection_expression, body, c_break_label, c_cont_label);
5549 else
5550 c_finish_loop (loc, cond, incr, body, c_break_label, c_cont_label, true);
5551 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99 || c_dialect_objc ()));
5552 c_break_label = save_break;
5553 c_cont_label = save_cont;
5556 /* Parse an asm statement, a GNU extension. This is a full-blown asm
5557 statement with inputs, outputs, clobbers, and volatile tag
5558 allowed.
5560 asm-statement:
5561 asm type-qualifier[opt] ( asm-argument ) ;
5562 asm type-qualifier[opt] goto ( asm-goto-argument ) ;
5564 asm-argument:
5565 asm-string-literal
5566 asm-string-literal : asm-operands[opt]
5567 asm-string-literal : asm-operands[opt] : asm-operands[opt]
5568 asm-string-literal : asm-operands[opt] : asm-operands[opt] : asm-clobbers[opt]
5570 asm-goto-argument:
5571 asm-string-literal : : asm-operands[opt] : asm-clobbers[opt] \
5572 : asm-goto-operands
5574 Qualifiers other than volatile are accepted in the syntax but
5575 warned for. */
5577 static tree
5578 c_parser_asm_statement (c_parser *parser)
5580 tree quals, str, outputs, inputs, clobbers, labels, ret;
5581 bool simple, is_goto;
5582 location_t asm_loc = c_parser_peek_token (parser)->location;
5583 int section, nsections;
5585 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ASM));
5586 c_parser_consume_token (parser);
5587 if (c_parser_next_token_is_keyword (parser, RID_VOLATILE))
5589 quals = c_parser_peek_token (parser)->value;
5590 c_parser_consume_token (parser);
5592 else if (c_parser_next_token_is_keyword (parser, RID_CONST)
5593 || c_parser_next_token_is_keyword (parser, RID_RESTRICT))
5595 warning_at (c_parser_peek_token (parser)->location,
5597 "%E qualifier ignored on asm",
5598 c_parser_peek_token (parser)->value);
5599 quals = NULL_TREE;
5600 c_parser_consume_token (parser);
5602 else
5603 quals = NULL_TREE;
5605 is_goto = false;
5606 if (c_parser_next_token_is_keyword (parser, RID_GOTO))
5608 c_parser_consume_token (parser);
5609 is_goto = true;
5612 /* ??? Follow the C++ parser rather than using the
5613 lex_untranslated_string kludge. */
5614 parser->lex_untranslated_string = true;
5615 ret = NULL;
5617 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5618 goto error;
5620 str = c_parser_asm_string_literal (parser);
5621 if (str == NULL_TREE)
5622 goto error_close_paren;
5624 simple = true;
5625 outputs = NULL_TREE;
5626 inputs = NULL_TREE;
5627 clobbers = NULL_TREE;
5628 labels = NULL_TREE;
5630 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN) && !is_goto)
5631 goto done_asm;
5633 /* Parse each colon-delimited section of operands. */
5634 nsections = 3 + is_goto;
5635 for (section = 0; section < nsections; ++section)
5637 if (!c_parser_require (parser, CPP_COLON,
5638 is_goto
5639 ? "expected %<:%>"
5640 : "expected %<:%> or %<)%>"))
5641 goto error_close_paren;
5643 /* Once past any colon, we're no longer a simple asm. */
5644 simple = false;
5646 if ((!c_parser_next_token_is (parser, CPP_COLON)
5647 && !c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
5648 || section == 3)
5649 switch (section)
5651 case 0:
5652 /* For asm goto, we don't allow output operands, but reserve
5653 the slot for a future extension that does allow them. */
5654 if (!is_goto)
5655 outputs = c_parser_asm_operands (parser);
5656 break;
5657 case 1:
5658 inputs = c_parser_asm_operands (parser);
5659 break;
5660 case 2:
5661 clobbers = c_parser_asm_clobbers (parser);
5662 break;
5663 case 3:
5664 labels = c_parser_asm_goto_operands (parser);
5665 break;
5666 default:
5667 gcc_unreachable ();
5670 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN) && !is_goto)
5671 goto done_asm;
5674 done_asm:
5675 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
5677 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5678 goto error;
5681 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
5682 c_parser_skip_to_end_of_block_or_statement (parser);
5684 ret = build_asm_stmt (quals, build_asm_expr (asm_loc, str, outputs, inputs,
5685 clobbers, labels, simple));
5687 error:
5688 parser->lex_untranslated_string = false;
5689 return ret;
5691 error_close_paren:
5692 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5693 goto error;
5696 /* Parse asm operands, a GNU extension.
5698 asm-operands:
5699 asm-operand
5700 asm-operands , asm-operand
5702 asm-operand:
5703 asm-string-literal ( expression )
5704 [ identifier ] asm-string-literal ( expression )
5707 static tree
5708 c_parser_asm_operands (c_parser *parser)
5710 tree list = NULL_TREE;
5711 while (true)
5713 tree name, str;
5714 struct c_expr expr;
5715 if (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
5717 c_parser_consume_token (parser);
5718 if (c_parser_next_token_is (parser, CPP_NAME))
5720 tree id = c_parser_peek_token (parser)->value;
5721 c_parser_consume_token (parser);
5722 name = build_string (IDENTIFIER_LENGTH (id),
5723 IDENTIFIER_POINTER (id));
5725 else
5727 c_parser_error (parser, "expected identifier");
5728 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
5729 return NULL_TREE;
5731 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
5732 "expected %<]%>");
5734 else
5735 name = NULL_TREE;
5736 str = c_parser_asm_string_literal (parser);
5737 if (str == NULL_TREE)
5738 return NULL_TREE;
5739 parser->lex_untranslated_string = false;
5740 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5742 parser->lex_untranslated_string = true;
5743 return NULL_TREE;
5745 expr = c_parser_expression (parser);
5746 mark_exp_read (expr.value);
5747 parser->lex_untranslated_string = true;
5748 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
5750 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5751 return NULL_TREE;
5753 list = chainon (list, build_tree_list (build_tree_list (name, str),
5754 expr.value));
5755 if (c_parser_next_token_is (parser, CPP_COMMA))
5756 c_parser_consume_token (parser);
5757 else
5758 break;
5760 return list;
5763 /* Parse asm clobbers, a GNU extension.
5765 asm-clobbers:
5766 asm-string-literal
5767 asm-clobbers , asm-string-literal
5770 static tree
5771 c_parser_asm_clobbers (c_parser *parser)
5773 tree list = NULL_TREE;
5774 while (true)
5776 tree str = c_parser_asm_string_literal (parser);
5777 if (str)
5778 list = tree_cons (NULL_TREE, str, list);
5779 else
5780 return NULL_TREE;
5781 if (c_parser_next_token_is (parser, CPP_COMMA))
5782 c_parser_consume_token (parser);
5783 else
5784 break;
5786 return list;
5789 /* Parse asm goto labels, a GNU extension.
5791 asm-goto-operands:
5792 identifier
5793 asm-goto-operands , identifier
5796 static tree
5797 c_parser_asm_goto_operands (c_parser *parser)
5799 tree list = NULL_TREE;
5800 while (true)
5802 tree name, label;
5804 if (c_parser_next_token_is (parser, CPP_NAME))
5806 c_token *tok = c_parser_peek_token (parser);
5807 name = tok->value;
5808 label = lookup_label_for_goto (tok->location, name);
5809 c_parser_consume_token (parser);
5810 TREE_USED (label) = 1;
5812 else
5814 c_parser_error (parser, "expected identifier");
5815 return NULL_TREE;
5818 name = build_string (IDENTIFIER_LENGTH (name),
5819 IDENTIFIER_POINTER (name));
5820 list = tree_cons (name, label, list);
5821 if (c_parser_next_token_is (parser, CPP_COMMA))
5822 c_parser_consume_token (parser);
5823 else
5824 return nreverse (list);
5828 /* Parse an expression other than a compound expression; that is, an
5829 assignment expression (C90 6.3.16, C99 6.5.16). If AFTER is not
5830 NULL then it is an Objective-C message expression which is the
5831 primary-expression starting the expression as an initializer.
5833 assignment-expression:
5834 conditional-expression
5835 unary-expression assignment-operator assignment-expression
5837 assignment-operator: one of
5838 = *= /= %= += -= <<= >>= &= ^= |=
5840 In GNU C we accept any conditional expression on the LHS and
5841 diagnose the invalid lvalue rather than producing a syntax
5842 error. */
5844 static struct c_expr
5845 c_parser_expr_no_commas (c_parser *parser, struct c_expr *after,
5846 tree omp_atomic_lhs)
5848 struct c_expr lhs, rhs, ret;
5849 enum tree_code code;
5850 location_t op_location, exp_location;
5851 gcc_assert (!after || c_dialect_objc ());
5852 lhs = c_parser_conditional_expression (parser, after, omp_atomic_lhs);
5853 op_location = c_parser_peek_token (parser)->location;
5854 switch (c_parser_peek_token (parser)->type)
5856 case CPP_EQ:
5857 code = NOP_EXPR;
5858 break;
5859 case CPP_MULT_EQ:
5860 code = MULT_EXPR;
5861 break;
5862 case CPP_DIV_EQ:
5863 code = TRUNC_DIV_EXPR;
5864 break;
5865 case CPP_MOD_EQ:
5866 code = TRUNC_MOD_EXPR;
5867 break;
5868 case CPP_PLUS_EQ:
5869 code = PLUS_EXPR;
5870 break;
5871 case CPP_MINUS_EQ:
5872 code = MINUS_EXPR;
5873 break;
5874 case CPP_LSHIFT_EQ:
5875 code = LSHIFT_EXPR;
5876 break;
5877 case CPP_RSHIFT_EQ:
5878 code = RSHIFT_EXPR;
5879 break;
5880 case CPP_AND_EQ:
5881 code = BIT_AND_EXPR;
5882 break;
5883 case CPP_XOR_EQ:
5884 code = BIT_XOR_EXPR;
5885 break;
5886 case CPP_OR_EQ:
5887 code = BIT_IOR_EXPR;
5888 break;
5889 default:
5890 return lhs;
5892 c_parser_consume_token (parser);
5893 exp_location = c_parser_peek_token (parser)->location;
5894 rhs = c_parser_expr_no_commas (parser, NULL);
5895 rhs = convert_lvalue_to_rvalue (exp_location, rhs, true, true);
5897 ret.value = build_modify_expr (op_location, lhs.value, lhs.original_type,
5898 code, exp_location, rhs.value,
5899 rhs.original_type);
5900 if (code == NOP_EXPR)
5901 ret.original_code = MODIFY_EXPR;
5902 else
5904 TREE_NO_WARNING (ret.value) = 1;
5905 ret.original_code = ERROR_MARK;
5907 ret.original_type = NULL;
5908 return ret;
5911 /* Parse a conditional expression (C90 6.3.15, C99 6.5.15). If AFTER
5912 is not NULL then it is an Objective-C message expression which is
5913 the primary-expression starting the expression as an initializer.
5915 conditional-expression:
5916 logical-OR-expression
5917 logical-OR-expression ? expression : conditional-expression
5919 GNU extensions:
5921 conditional-expression:
5922 logical-OR-expression ? : conditional-expression
5925 static struct c_expr
5926 c_parser_conditional_expression (c_parser *parser, struct c_expr *after,
5927 tree omp_atomic_lhs)
5929 struct c_expr cond, exp1, exp2, ret;
5930 location_t cond_loc, colon_loc, middle_loc;
5932 gcc_assert (!after || c_dialect_objc ());
5934 cond = c_parser_binary_expression (parser, after, omp_atomic_lhs);
5936 if (c_parser_next_token_is_not (parser, CPP_QUERY))
5937 return cond;
5938 cond_loc = c_parser_peek_token (parser)->location;
5939 cond = convert_lvalue_to_rvalue (cond_loc, cond, true, true);
5940 c_parser_consume_token (parser);
5941 if (c_parser_next_token_is (parser, CPP_COLON))
5943 tree eptype = NULL_TREE;
5945 middle_loc = c_parser_peek_token (parser)->location;
5946 pedwarn (middle_loc, OPT_Wpedantic,
5947 "ISO C forbids omitting the middle term of a ?: expression");
5948 warn_for_omitted_condop (middle_loc, cond.value);
5949 if (TREE_CODE (cond.value) == EXCESS_PRECISION_EXPR)
5951 eptype = TREE_TYPE (cond.value);
5952 cond.value = TREE_OPERAND (cond.value, 0);
5954 /* Make sure first operand is calculated only once. */
5955 exp1.value = c_save_expr (default_conversion (cond.value));
5956 if (eptype)
5957 exp1.value = build1 (EXCESS_PRECISION_EXPR, eptype, exp1.value);
5958 exp1.original_type = NULL;
5959 cond.value = c_objc_common_truthvalue_conversion (cond_loc, exp1.value);
5960 c_inhibit_evaluation_warnings += cond.value == truthvalue_true_node;
5962 else
5964 cond.value
5965 = c_objc_common_truthvalue_conversion
5966 (cond_loc, default_conversion (cond.value));
5967 c_inhibit_evaluation_warnings += cond.value == truthvalue_false_node;
5968 exp1 = c_parser_expression_conv (parser);
5969 mark_exp_read (exp1.value);
5970 c_inhibit_evaluation_warnings +=
5971 ((cond.value == truthvalue_true_node)
5972 - (cond.value == truthvalue_false_node));
5975 colon_loc = c_parser_peek_token (parser)->location;
5976 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
5978 c_inhibit_evaluation_warnings -= cond.value == truthvalue_true_node;
5979 ret.value = error_mark_node;
5980 ret.original_code = ERROR_MARK;
5981 ret.original_type = NULL;
5982 return ret;
5985 location_t exp2_loc = c_parser_peek_token (parser)->location;
5986 exp2 = c_parser_conditional_expression (parser, NULL, NULL_TREE);
5987 exp2 = convert_lvalue_to_rvalue (exp2_loc, exp2, true, true);
5989 c_inhibit_evaluation_warnings -= cond.value == truthvalue_true_node;
5990 ret.value = build_conditional_expr (colon_loc, cond.value,
5991 cond.original_code == C_MAYBE_CONST_EXPR,
5992 exp1.value, exp1.original_type,
5993 exp2.value, exp2.original_type);
5994 ret.original_code = ERROR_MARK;
5995 if (exp1.value == error_mark_node || exp2.value == error_mark_node)
5996 ret.original_type = NULL;
5997 else
5999 tree t1, t2;
6001 /* If both sides are enum type, the default conversion will have
6002 made the type of the result be an integer type. We want to
6003 remember the enum types we started with. */
6004 t1 = exp1.original_type ? exp1.original_type : TREE_TYPE (exp1.value);
6005 t2 = exp2.original_type ? exp2.original_type : TREE_TYPE (exp2.value);
6006 ret.original_type = ((t1 != error_mark_node
6007 && t2 != error_mark_node
6008 && (TYPE_MAIN_VARIANT (t1)
6009 == TYPE_MAIN_VARIANT (t2)))
6010 ? t1
6011 : NULL);
6013 return ret;
6016 /* Parse a binary expression; that is, a logical-OR-expression (C90
6017 6.3.5-6.3.14, C99 6.5.5-6.5.14). If AFTER is not NULL then it is
6018 an Objective-C message expression which is the primary-expression
6019 starting the expression as an initializer.
6021 OMP_ATOMIC_LHS is NULL, unless parsing OpenMP #pragma omp atomic,
6022 when it should be the unfolded lhs. In a valid OpenMP source,
6023 one of the operands of the toplevel binary expression must be equal
6024 to it. In that case, just return a build2 created binary operation
6025 rather than result of parser_build_binary_op.
6027 multiplicative-expression:
6028 cast-expression
6029 multiplicative-expression * cast-expression
6030 multiplicative-expression / cast-expression
6031 multiplicative-expression % cast-expression
6033 additive-expression:
6034 multiplicative-expression
6035 additive-expression + multiplicative-expression
6036 additive-expression - multiplicative-expression
6038 shift-expression:
6039 additive-expression
6040 shift-expression << additive-expression
6041 shift-expression >> additive-expression
6043 relational-expression:
6044 shift-expression
6045 relational-expression < shift-expression
6046 relational-expression > shift-expression
6047 relational-expression <= shift-expression
6048 relational-expression >= shift-expression
6050 equality-expression:
6051 relational-expression
6052 equality-expression == relational-expression
6053 equality-expression != relational-expression
6055 AND-expression:
6056 equality-expression
6057 AND-expression & equality-expression
6059 exclusive-OR-expression:
6060 AND-expression
6061 exclusive-OR-expression ^ AND-expression
6063 inclusive-OR-expression:
6064 exclusive-OR-expression
6065 inclusive-OR-expression | exclusive-OR-expression
6067 logical-AND-expression:
6068 inclusive-OR-expression
6069 logical-AND-expression && inclusive-OR-expression
6071 logical-OR-expression:
6072 logical-AND-expression
6073 logical-OR-expression || logical-AND-expression
6076 static struct c_expr
6077 c_parser_binary_expression (c_parser *parser, struct c_expr *after,
6078 tree omp_atomic_lhs)
6080 /* A binary expression is parsed using operator-precedence parsing,
6081 with the operands being cast expressions. All the binary
6082 operators are left-associative. Thus a binary expression is of
6083 form:
6085 E0 op1 E1 op2 E2 ...
6087 which we represent on a stack. On the stack, the precedence
6088 levels are strictly increasing. When a new operator is
6089 encountered of higher precedence than that at the top of the
6090 stack, it is pushed; its LHS is the top expression, and its RHS
6091 is everything parsed until it is popped. When a new operator is
6092 encountered with precedence less than or equal to that at the top
6093 of the stack, triples E[i-1] op[i] E[i] are popped and replaced
6094 by the result of the operation until the operator at the top of
6095 the stack has lower precedence than the new operator or there is
6096 only one element on the stack; then the top expression is the LHS
6097 of the new operator. In the case of logical AND and OR
6098 expressions, we also need to adjust c_inhibit_evaluation_warnings
6099 as appropriate when the operators are pushed and popped. */
6101 struct {
6102 /* The expression at this stack level. */
6103 struct c_expr expr;
6104 /* The precedence of the operator on its left, PREC_NONE at the
6105 bottom of the stack. */
6106 enum c_parser_prec prec;
6107 /* The operation on its left. */
6108 enum tree_code op;
6109 /* The source location of this operation. */
6110 location_t loc;
6111 } stack[NUM_PRECS];
6112 int sp;
6113 /* Location of the binary operator. */
6114 location_t binary_loc = UNKNOWN_LOCATION; /* Quiet warning. */
6115 #define POP \
6116 do { \
6117 switch (stack[sp].op) \
6119 case TRUTH_ANDIF_EXPR: \
6120 c_inhibit_evaluation_warnings -= (stack[sp - 1].expr.value \
6121 == truthvalue_false_node); \
6122 break; \
6123 case TRUTH_ORIF_EXPR: \
6124 c_inhibit_evaluation_warnings -= (stack[sp - 1].expr.value \
6125 == truthvalue_true_node); \
6126 break; \
6127 default: \
6128 break; \
6130 stack[sp - 1].expr \
6131 = convert_lvalue_to_rvalue (stack[sp - 1].loc, \
6132 stack[sp - 1].expr, true, true); \
6133 stack[sp].expr \
6134 = convert_lvalue_to_rvalue (stack[sp].loc, \
6135 stack[sp].expr, true, true); \
6136 if (__builtin_expect (omp_atomic_lhs != NULL_TREE, 0) && sp == 1 \
6137 && c_parser_peek_token (parser)->type == CPP_SEMICOLON \
6138 && ((1 << stack[sp].prec) \
6139 & (1 << (PREC_BITOR | PREC_BITXOR | PREC_BITAND | PREC_SHIFT \
6140 | PREC_ADD | PREC_MULT))) \
6141 && stack[sp].op != TRUNC_MOD_EXPR \
6142 && stack[0].expr.value != error_mark_node \
6143 && stack[1].expr.value != error_mark_node \
6144 && (c_tree_equal (stack[0].expr.value, omp_atomic_lhs) \
6145 || c_tree_equal (stack[1].expr.value, omp_atomic_lhs))) \
6146 stack[0].expr.value \
6147 = build2 (stack[1].op, TREE_TYPE (stack[0].expr.value), \
6148 stack[0].expr.value, stack[1].expr.value); \
6149 else \
6150 stack[sp - 1].expr = parser_build_binary_op (stack[sp].loc, \
6151 stack[sp].op, \
6152 stack[sp - 1].expr, \
6153 stack[sp].expr); \
6154 sp--; \
6155 } while (0)
6156 gcc_assert (!after || c_dialect_objc ());
6157 stack[0].loc = c_parser_peek_token (parser)->location;
6158 stack[0].expr = c_parser_cast_expression (parser, after);
6159 stack[0].prec = PREC_NONE;
6160 sp = 0;
6161 while (true)
6163 enum c_parser_prec oprec;
6164 enum tree_code ocode;
6165 if (parser->error)
6166 goto out;
6167 switch (c_parser_peek_token (parser)->type)
6169 case CPP_MULT:
6170 oprec = PREC_MULT;
6171 ocode = MULT_EXPR;
6172 break;
6173 case CPP_DIV:
6174 oprec = PREC_MULT;
6175 ocode = TRUNC_DIV_EXPR;
6176 break;
6177 case CPP_MOD:
6178 oprec = PREC_MULT;
6179 ocode = TRUNC_MOD_EXPR;
6180 break;
6181 case CPP_PLUS:
6182 oprec = PREC_ADD;
6183 ocode = PLUS_EXPR;
6184 break;
6185 case CPP_MINUS:
6186 oprec = PREC_ADD;
6187 ocode = MINUS_EXPR;
6188 break;
6189 case CPP_LSHIFT:
6190 oprec = PREC_SHIFT;
6191 ocode = LSHIFT_EXPR;
6192 break;
6193 case CPP_RSHIFT:
6194 oprec = PREC_SHIFT;
6195 ocode = RSHIFT_EXPR;
6196 break;
6197 case CPP_LESS:
6198 oprec = PREC_REL;
6199 ocode = LT_EXPR;
6200 break;
6201 case CPP_GREATER:
6202 oprec = PREC_REL;
6203 ocode = GT_EXPR;
6204 break;
6205 case CPP_LESS_EQ:
6206 oprec = PREC_REL;
6207 ocode = LE_EXPR;
6208 break;
6209 case CPP_GREATER_EQ:
6210 oprec = PREC_REL;
6211 ocode = GE_EXPR;
6212 break;
6213 case CPP_EQ_EQ:
6214 oprec = PREC_EQ;
6215 ocode = EQ_EXPR;
6216 break;
6217 case CPP_NOT_EQ:
6218 oprec = PREC_EQ;
6219 ocode = NE_EXPR;
6220 break;
6221 case CPP_AND:
6222 oprec = PREC_BITAND;
6223 ocode = BIT_AND_EXPR;
6224 break;
6225 case CPP_XOR:
6226 oprec = PREC_BITXOR;
6227 ocode = BIT_XOR_EXPR;
6228 break;
6229 case CPP_OR:
6230 oprec = PREC_BITOR;
6231 ocode = BIT_IOR_EXPR;
6232 break;
6233 case CPP_AND_AND:
6234 oprec = PREC_LOGAND;
6235 ocode = TRUTH_ANDIF_EXPR;
6236 break;
6237 case CPP_OR_OR:
6238 oprec = PREC_LOGOR;
6239 ocode = TRUTH_ORIF_EXPR;
6240 break;
6241 default:
6242 /* Not a binary operator, so end of the binary
6243 expression. */
6244 goto out;
6246 binary_loc = c_parser_peek_token (parser)->location;
6247 while (oprec <= stack[sp].prec)
6248 POP;
6249 c_parser_consume_token (parser);
6250 switch (ocode)
6252 case TRUTH_ANDIF_EXPR:
6253 stack[sp].expr
6254 = convert_lvalue_to_rvalue (stack[sp].loc,
6255 stack[sp].expr, true, true);
6256 stack[sp].expr.value = c_objc_common_truthvalue_conversion
6257 (stack[sp].loc, default_conversion (stack[sp].expr.value));
6258 c_inhibit_evaluation_warnings += (stack[sp].expr.value
6259 == truthvalue_false_node);
6260 break;
6261 case TRUTH_ORIF_EXPR:
6262 stack[sp].expr
6263 = convert_lvalue_to_rvalue (stack[sp].loc,
6264 stack[sp].expr, true, true);
6265 stack[sp].expr.value = c_objc_common_truthvalue_conversion
6266 (stack[sp].loc, default_conversion (stack[sp].expr.value));
6267 c_inhibit_evaluation_warnings += (stack[sp].expr.value
6268 == truthvalue_true_node);
6269 break;
6270 default:
6271 break;
6273 sp++;
6274 stack[sp].loc = binary_loc;
6275 stack[sp].expr = c_parser_cast_expression (parser, NULL);
6276 stack[sp].prec = oprec;
6277 stack[sp].op = ocode;
6278 stack[sp].loc = binary_loc;
6280 out:
6281 while (sp > 0)
6282 POP;
6283 return stack[0].expr;
6284 #undef POP
6287 /* Parse a cast expression (C90 6.3.4, C99 6.5.4). If AFTER is not
6288 NULL then it is an Objective-C message expression which is the
6289 primary-expression starting the expression as an initializer.
6291 cast-expression:
6292 unary-expression
6293 ( type-name ) unary-expression
6296 static struct c_expr
6297 c_parser_cast_expression (c_parser *parser, struct c_expr *after)
6299 location_t cast_loc = c_parser_peek_token (parser)->location;
6300 gcc_assert (!after || c_dialect_objc ());
6301 if (after)
6302 return c_parser_postfix_expression_after_primary (parser,
6303 cast_loc, *after);
6304 /* If the expression begins with a parenthesized type name, it may
6305 be either a cast or a compound literal; we need to see whether
6306 the next character is '{' to tell the difference. If not, it is
6307 an unary expression. Full detection of unknown typenames here
6308 would require a 3-token lookahead. */
6309 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
6310 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
6312 struct c_type_name *type_name;
6313 struct c_expr ret;
6314 struct c_expr expr;
6315 c_parser_consume_token (parser);
6316 type_name = c_parser_type_name (parser);
6317 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
6318 if (type_name == NULL)
6320 ret.value = error_mark_node;
6321 ret.original_code = ERROR_MARK;
6322 ret.original_type = NULL;
6323 return ret;
6326 /* Save casted types in the function's used types hash table. */
6327 used_types_insert (type_name->specs->type);
6329 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
6330 return c_parser_postfix_expression_after_paren_type (parser, type_name,
6331 cast_loc);
6333 location_t expr_loc = c_parser_peek_token (parser)->location;
6334 expr = c_parser_cast_expression (parser, NULL);
6335 expr = convert_lvalue_to_rvalue (expr_loc, expr, true, true);
6337 ret.value = c_cast_expr (cast_loc, type_name, expr.value);
6338 ret.original_code = ERROR_MARK;
6339 ret.original_type = NULL;
6340 return ret;
6342 else
6343 return c_parser_unary_expression (parser);
6346 /* Parse an unary expression (C90 6.3.3, C99 6.5.3).
6348 unary-expression:
6349 postfix-expression
6350 ++ unary-expression
6351 -- unary-expression
6352 unary-operator cast-expression
6353 sizeof unary-expression
6354 sizeof ( type-name )
6356 unary-operator: one of
6357 & * + - ~ !
6359 GNU extensions:
6361 unary-expression:
6362 __alignof__ unary-expression
6363 __alignof__ ( type-name )
6364 && identifier
6366 (C11 permits _Alignof with type names only.)
6368 unary-operator: one of
6369 __extension__ __real__ __imag__
6371 Transactional Memory:
6373 unary-expression:
6374 transaction-expression
6376 In addition, the GNU syntax treats ++ and -- as unary operators, so
6377 they may be applied to cast expressions with errors for non-lvalues
6378 given later. */
6380 static struct c_expr
6381 c_parser_unary_expression (c_parser *parser)
6383 int ext;
6384 struct c_expr ret, op;
6385 location_t op_loc = c_parser_peek_token (parser)->location;
6386 location_t exp_loc;
6387 ret.original_code = ERROR_MARK;
6388 ret.original_type = NULL;
6389 switch (c_parser_peek_token (parser)->type)
6391 case CPP_PLUS_PLUS:
6392 c_parser_consume_token (parser);
6393 exp_loc = c_parser_peek_token (parser)->location;
6394 op = c_parser_cast_expression (parser, NULL);
6396 /* If there is array notations in op, we expand them. */
6397 if (flag_cilkplus && TREE_CODE (op.value) == ARRAY_NOTATION_REF)
6398 return fix_array_notation_expr (exp_loc, PREINCREMENT_EXPR, op);
6399 else
6401 op = default_function_array_read_conversion (exp_loc, op);
6402 return parser_build_unary_op (op_loc, PREINCREMENT_EXPR, op);
6404 case CPP_MINUS_MINUS:
6405 c_parser_consume_token (parser);
6406 exp_loc = c_parser_peek_token (parser)->location;
6407 op = c_parser_cast_expression (parser, NULL);
6409 /* If there is array notations in op, we expand them. */
6410 if (flag_cilkplus && TREE_CODE (op.value) == ARRAY_NOTATION_REF)
6411 return fix_array_notation_expr (exp_loc, PREDECREMENT_EXPR, op);
6412 else
6414 op = default_function_array_read_conversion (exp_loc, op);
6415 return parser_build_unary_op (op_loc, PREDECREMENT_EXPR, op);
6417 case CPP_AND:
6418 c_parser_consume_token (parser);
6419 op = c_parser_cast_expression (parser, NULL);
6420 mark_exp_read (op.value);
6421 return parser_build_unary_op (op_loc, ADDR_EXPR, op);
6422 case CPP_MULT:
6423 c_parser_consume_token (parser);
6424 exp_loc = c_parser_peek_token (parser)->location;
6425 op = c_parser_cast_expression (parser, NULL);
6426 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
6427 ret.value = build_indirect_ref (op_loc, op.value, RO_UNARY_STAR);
6428 return ret;
6429 case CPP_PLUS:
6430 if (!c_dialect_objc () && !in_system_header_at (input_location))
6431 warning_at (op_loc,
6432 OPT_Wtraditional,
6433 "traditional C rejects the unary plus operator");
6434 c_parser_consume_token (parser);
6435 exp_loc = c_parser_peek_token (parser)->location;
6436 op = c_parser_cast_expression (parser, NULL);
6437 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
6438 return parser_build_unary_op (op_loc, CONVERT_EXPR, op);
6439 case CPP_MINUS:
6440 c_parser_consume_token (parser);
6441 exp_loc = c_parser_peek_token (parser)->location;
6442 op = c_parser_cast_expression (parser, NULL);
6443 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
6444 return parser_build_unary_op (op_loc, NEGATE_EXPR, op);
6445 case CPP_COMPL:
6446 c_parser_consume_token (parser);
6447 exp_loc = c_parser_peek_token (parser)->location;
6448 op = c_parser_cast_expression (parser, NULL);
6449 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
6450 return parser_build_unary_op (op_loc, BIT_NOT_EXPR, op);
6451 case CPP_NOT:
6452 c_parser_consume_token (parser);
6453 exp_loc = c_parser_peek_token (parser)->location;
6454 op = c_parser_cast_expression (parser, NULL);
6455 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
6456 return parser_build_unary_op (op_loc, TRUTH_NOT_EXPR, op);
6457 case CPP_AND_AND:
6458 /* Refer to the address of a label as a pointer. */
6459 c_parser_consume_token (parser);
6460 if (c_parser_next_token_is (parser, CPP_NAME))
6462 ret.value = finish_label_address_expr
6463 (c_parser_peek_token (parser)->value, op_loc);
6464 c_parser_consume_token (parser);
6466 else
6468 c_parser_error (parser, "expected identifier");
6469 ret.value = error_mark_node;
6471 return ret;
6472 case CPP_KEYWORD:
6473 switch (c_parser_peek_token (parser)->keyword)
6475 case RID_SIZEOF:
6476 return c_parser_sizeof_expression (parser);
6477 case RID_ALIGNOF:
6478 return c_parser_alignof_expression (parser);
6479 case RID_EXTENSION:
6480 c_parser_consume_token (parser);
6481 ext = disable_extension_diagnostics ();
6482 ret = c_parser_cast_expression (parser, NULL);
6483 restore_extension_diagnostics (ext);
6484 return ret;
6485 case RID_REALPART:
6486 c_parser_consume_token (parser);
6487 exp_loc = c_parser_peek_token (parser)->location;
6488 op = c_parser_cast_expression (parser, NULL);
6489 op = default_function_array_conversion (exp_loc, op);
6490 return parser_build_unary_op (op_loc, REALPART_EXPR, op);
6491 case RID_IMAGPART:
6492 c_parser_consume_token (parser);
6493 exp_loc = c_parser_peek_token (parser)->location;
6494 op = c_parser_cast_expression (parser, NULL);
6495 op = default_function_array_conversion (exp_loc, op);
6496 return parser_build_unary_op (op_loc, IMAGPART_EXPR, op);
6497 case RID_TRANSACTION_ATOMIC:
6498 case RID_TRANSACTION_RELAXED:
6499 return c_parser_transaction_expression (parser,
6500 c_parser_peek_token (parser)->keyword);
6501 default:
6502 return c_parser_postfix_expression (parser);
6504 default:
6505 return c_parser_postfix_expression (parser);
6509 /* Parse a sizeof expression. */
6511 static struct c_expr
6512 c_parser_sizeof_expression (c_parser *parser)
6514 struct c_expr expr;
6515 location_t expr_loc;
6516 gcc_assert (c_parser_next_token_is_keyword (parser, RID_SIZEOF));
6517 c_parser_consume_token (parser);
6518 c_inhibit_evaluation_warnings++;
6519 in_sizeof++;
6520 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
6521 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
6523 /* Either sizeof ( type-name ) or sizeof unary-expression
6524 starting with a compound literal. */
6525 struct c_type_name *type_name;
6526 c_parser_consume_token (parser);
6527 expr_loc = c_parser_peek_token (parser)->location;
6528 type_name = c_parser_type_name (parser);
6529 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
6530 if (type_name == NULL)
6532 struct c_expr ret;
6533 c_inhibit_evaluation_warnings--;
6534 in_sizeof--;
6535 ret.value = error_mark_node;
6536 ret.original_code = ERROR_MARK;
6537 ret.original_type = NULL;
6538 return ret;
6540 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
6542 expr = c_parser_postfix_expression_after_paren_type (parser,
6543 type_name,
6544 expr_loc);
6545 goto sizeof_expr;
6547 /* sizeof ( type-name ). */
6548 c_inhibit_evaluation_warnings--;
6549 in_sizeof--;
6550 return c_expr_sizeof_type (expr_loc, type_name);
6552 else
6554 expr_loc = c_parser_peek_token (parser)->location;
6555 expr = c_parser_unary_expression (parser);
6556 sizeof_expr:
6557 c_inhibit_evaluation_warnings--;
6558 in_sizeof--;
6559 mark_exp_read (expr.value);
6560 if (TREE_CODE (expr.value) == COMPONENT_REF
6561 && DECL_C_BIT_FIELD (TREE_OPERAND (expr.value, 1)))
6562 error_at (expr_loc, "%<sizeof%> applied to a bit-field");
6563 return c_expr_sizeof_expr (expr_loc, expr);
6567 /* Parse an alignof expression. */
6569 static struct c_expr
6570 c_parser_alignof_expression (c_parser *parser)
6572 struct c_expr expr;
6573 location_t loc = c_parser_peek_token (parser)->location;
6574 tree alignof_spelling = c_parser_peek_token (parser)->value;
6575 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ALIGNOF));
6576 bool is_c11_alignof = strcmp (IDENTIFIER_POINTER (alignof_spelling),
6577 "_Alignof") == 0;
6578 /* A diagnostic is not required for the use of this identifier in
6579 the implementation namespace; only diagnose it for the C11
6580 spelling because of existing code using the other spellings. */
6581 if (!flag_isoc11 && is_c11_alignof)
6583 if (flag_isoc99)
6584 pedwarn (loc, OPT_Wpedantic, "ISO C99 does not support %qE",
6585 alignof_spelling);
6586 else
6587 pedwarn (loc, OPT_Wpedantic, "ISO C90 does not support %qE",
6588 alignof_spelling);
6590 c_parser_consume_token (parser);
6591 c_inhibit_evaluation_warnings++;
6592 in_alignof++;
6593 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
6594 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
6596 /* Either __alignof__ ( type-name ) or __alignof__
6597 unary-expression starting with a compound literal. */
6598 location_t loc;
6599 struct c_type_name *type_name;
6600 struct c_expr ret;
6601 c_parser_consume_token (parser);
6602 loc = c_parser_peek_token (parser)->location;
6603 type_name = c_parser_type_name (parser);
6604 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
6605 if (type_name == NULL)
6607 struct c_expr ret;
6608 c_inhibit_evaluation_warnings--;
6609 in_alignof--;
6610 ret.value = error_mark_node;
6611 ret.original_code = ERROR_MARK;
6612 ret.original_type = NULL;
6613 return ret;
6615 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
6617 expr = c_parser_postfix_expression_after_paren_type (parser,
6618 type_name,
6619 loc);
6620 goto alignof_expr;
6622 /* alignof ( type-name ). */
6623 c_inhibit_evaluation_warnings--;
6624 in_alignof--;
6625 ret.value = c_sizeof_or_alignof_type (loc, groktypename (type_name,
6626 NULL, NULL),
6627 false, is_c11_alignof, 1);
6628 ret.original_code = ERROR_MARK;
6629 ret.original_type = NULL;
6630 return ret;
6632 else
6634 struct c_expr ret;
6635 expr = c_parser_unary_expression (parser);
6636 alignof_expr:
6637 mark_exp_read (expr.value);
6638 c_inhibit_evaluation_warnings--;
6639 in_alignof--;
6640 pedwarn (loc, OPT_Wpedantic, "ISO C does not allow %<%E (expression)%>",
6641 alignof_spelling);
6642 ret.value = c_alignof_expr (loc, expr.value);
6643 ret.original_code = ERROR_MARK;
6644 ret.original_type = NULL;
6645 return ret;
6649 /* Helper function to read arguments of builtins which are interfaces
6650 for the middle-end nodes like COMPLEX_EXPR, VEC_PERM_EXPR and
6651 others. The name of the builtin is passed using BNAME parameter.
6652 Function returns true if there were no errors while parsing and
6653 stores the arguments in CEXPR_LIST. */
6654 static bool
6655 c_parser_get_builtin_args (c_parser *parser, const char *bname,
6656 vec<c_expr_t, va_gc> **ret_cexpr_list,
6657 bool choose_expr_p)
6659 location_t loc = c_parser_peek_token (parser)->location;
6660 vec<c_expr_t, va_gc> *cexpr_list;
6661 c_expr_t expr;
6662 bool saved_force_folding_builtin_constant_p;
6664 *ret_cexpr_list = NULL;
6665 if (c_parser_next_token_is_not (parser, CPP_OPEN_PAREN))
6667 error_at (loc, "cannot take address of %qs", bname);
6668 return false;
6671 c_parser_consume_token (parser);
6673 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
6675 c_parser_consume_token (parser);
6676 return true;
6679 saved_force_folding_builtin_constant_p
6680 = force_folding_builtin_constant_p;
6681 force_folding_builtin_constant_p |= choose_expr_p;
6682 expr = c_parser_expr_no_commas (parser, NULL);
6683 force_folding_builtin_constant_p
6684 = saved_force_folding_builtin_constant_p;
6685 vec_alloc (cexpr_list, 1);
6686 vec_safe_push (cexpr_list, expr);
6687 while (c_parser_next_token_is (parser, CPP_COMMA))
6689 c_parser_consume_token (parser);
6690 expr = c_parser_expr_no_commas (parser, NULL);
6691 vec_safe_push (cexpr_list, expr);
6694 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
6695 return false;
6697 *ret_cexpr_list = cexpr_list;
6698 return true;
6701 /* This represents a single generic-association. */
6703 struct c_generic_association
6705 /* The location of the starting token of the type. */
6706 location_t type_location;
6707 /* The association's type, or NULL_TREE for 'default'. */
6708 tree type;
6709 /* The association's expression. */
6710 struct c_expr expression;
6713 /* Parse a generic-selection. (C11 6.5.1.1).
6715 generic-selection:
6716 _Generic ( assignment-expression , generic-assoc-list )
6718 generic-assoc-list:
6719 generic-association
6720 generic-assoc-list , generic-association
6722 generic-association:
6723 type-name : assignment-expression
6724 default : assignment-expression
6727 static struct c_expr
6728 c_parser_generic_selection (c_parser *parser)
6730 vec<c_generic_association> associations = vNULL;
6731 struct c_expr selector, error_expr;
6732 tree selector_type;
6733 struct c_generic_association matched_assoc;
6734 bool match_found = false;
6735 location_t generic_loc, selector_loc;
6737 error_expr.original_code = ERROR_MARK;
6738 error_expr.original_type = NULL;
6739 error_expr.value = error_mark_node;
6740 matched_assoc.type_location = UNKNOWN_LOCATION;
6741 matched_assoc.type = NULL_TREE;
6742 matched_assoc.expression = error_expr;
6744 gcc_assert (c_parser_next_token_is_keyword (parser, RID_GENERIC));
6745 generic_loc = c_parser_peek_token (parser)->location;
6746 c_parser_consume_token (parser);
6747 if (!flag_isoc11)
6749 if (flag_isoc99)
6750 pedwarn (generic_loc, OPT_Wpedantic,
6751 "ISO C99 does not support %<_Generic%>");
6752 else
6753 pedwarn (generic_loc, OPT_Wpedantic,
6754 "ISO C90 does not support %<_Generic%>");
6757 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6758 return error_expr;
6760 c_inhibit_evaluation_warnings++;
6761 selector_loc = c_parser_peek_token (parser)->location;
6762 selector = c_parser_expr_no_commas (parser, NULL);
6763 selector = default_function_array_conversion (selector_loc, selector);
6764 c_inhibit_evaluation_warnings--;
6766 if (selector.value == error_mark_node)
6768 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6769 return selector;
6771 selector_type = TREE_TYPE (selector.value);
6772 /* In ISO C terms, rvalues (including the controlling expression of
6773 _Generic) do not have qualified types. */
6774 if (TREE_CODE (selector_type) != ARRAY_TYPE)
6775 selector_type = TYPE_MAIN_VARIANT (selector_type);
6776 /* In ISO C terms, _Noreturn is not part of the type of expressions
6777 such as &abort, but in GCC it is represented internally as a type
6778 qualifier. */
6779 if (FUNCTION_POINTER_TYPE_P (selector_type)
6780 && TYPE_QUALS (TREE_TYPE (selector_type)) != TYPE_UNQUALIFIED)
6781 selector_type
6782 = build_pointer_type (TYPE_MAIN_VARIANT (TREE_TYPE (selector_type)));
6784 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
6786 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6787 return error_expr;
6790 while (1)
6792 struct c_generic_association assoc, *iter;
6793 unsigned int ix;
6794 c_token *token = c_parser_peek_token (parser);
6796 assoc.type_location = token->location;
6797 if (token->type == CPP_KEYWORD && token->keyword == RID_DEFAULT)
6799 c_parser_consume_token (parser);
6800 assoc.type = NULL_TREE;
6802 else
6804 struct c_type_name *type_name;
6806 type_name = c_parser_type_name (parser);
6807 if (type_name == NULL)
6809 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6810 goto error_exit;
6812 assoc.type = groktypename (type_name, NULL, NULL);
6813 if (assoc.type == error_mark_node)
6815 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6816 goto error_exit;
6819 if (TREE_CODE (assoc.type) == FUNCTION_TYPE)
6820 error_at (assoc.type_location,
6821 "%<_Generic%> association has function type");
6822 else if (!COMPLETE_TYPE_P (assoc.type))
6823 error_at (assoc.type_location,
6824 "%<_Generic%> association has incomplete type");
6826 if (variably_modified_type_p (assoc.type, NULL_TREE))
6827 error_at (assoc.type_location,
6828 "%<_Generic%> association has "
6829 "variable length type");
6832 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
6834 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6835 goto error_exit;
6838 assoc.expression = c_parser_expr_no_commas (parser, NULL);
6839 if (assoc.expression.value == error_mark_node)
6841 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6842 goto error_exit;
6845 for (ix = 0; associations.iterate (ix, &iter); ++ix)
6847 if (assoc.type == NULL_TREE)
6849 if (iter->type == NULL_TREE)
6851 error_at (assoc.type_location,
6852 "duplicate %<default%> case in %<_Generic%>");
6853 inform (iter->type_location, "original %<default%> is here");
6856 else if (iter->type != NULL_TREE)
6858 if (comptypes (assoc.type, iter->type))
6860 error_at (assoc.type_location,
6861 "%<_Generic%> specifies two compatible types");
6862 inform (iter->type_location, "compatible type is here");
6867 if (assoc.type == NULL_TREE)
6869 if (!match_found)
6871 matched_assoc = assoc;
6872 match_found = true;
6875 else if (comptypes (assoc.type, selector_type))
6877 if (!match_found || matched_assoc.type == NULL_TREE)
6879 matched_assoc = assoc;
6880 match_found = true;
6882 else
6884 error_at (assoc.type_location,
6885 "%<_Generic> selector matches multiple associations");
6886 inform (matched_assoc.type_location,
6887 "other match is here");
6891 associations.safe_push (assoc);
6893 if (c_parser_peek_token (parser)->type != CPP_COMMA)
6894 break;
6895 c_parser_consume_token (parser);
6898 associations.release ();
6900 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
6902 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6903 return error_expr;
6906 if (!match_found)
6908 error_at (selector_loc, "%<_Generic%> selector of type %qT is not "
6909 "compatible with any association",
6910 selector_type);
6911 return error_expr;
6914 return matched_assoc.expression;
6916 error_exit:
6917 associations.release ();
6918 return error_expr;
6921 /* Parse a postfix expression (C90 6.3.1-6.3.2, C99 6.5.1-6.5.2).
6923 postfix-expression:
6924 primary-expression
6925 postfix-expression [ expression ]
6926 postfix-expression ( argument-expression-list[opt] )
6927 postfix-expression . identifier
6928 postfix-expression -> identifier
6929 postfix-expression ++
6930 postfix-expression --
6931 ( type-name ) { initializer-list }
6932 ( type-name ) { initializer-list , }
6934 argument-expression-list:
6935 argument-expression
6936 argument-expression-list , argument-expression
6938 primary-expression:
6939 identifier
6940 constant
6941 string-literal
6942 ( expression )
6943 generic-selection
6945 GNU extensions:
6947 primary-expression:
6948 __func__
6949 (treated as a keyword in GNU C)
6950 __FUNCTION__
6951 __PRETTY_FUNCTION__
6952 ( compound-statement )
6953 __builtin_va_arg ( assignment-expression , type-name )
6954 __builtin_offsetof ( type-name , offsetof-member-designator )
6955 __builtin_choose_expr ( assignment-expression ,
6956 assignment-expression ,
6957 assignment-expression )
6958 __builtin_types_compatible_p ( type-name , type-name )
6959 __builtin_complex ( assignment-expression , assignment-expression )
6960 __builtin_shuffle ( assignment-expression , assignment-expression )
6961 __builtin_shuffle ( assignment-expression ,
6962 assignment-expression ,
6963 assignment-expression, )
6965 offsetof-member-designator:
6966 identifier
6967 offsetof-member-designator . identifier
6968 offsetof-member-designator [ expression ]
6970 Objective-C:
6972 primary-expression:
6973 [ objc-receiver objc-message-args ]
6974 @selector ( objc-selector-arg )
6975 @protocol ( identifier )
6976 @encode ( type-name )
6977 objc-string-literal
6978 Classname . identifier
6981 static struct c_expr
6982 c_parser_postfix_expression (c_parser *parser)
6984 struct c_expr expr, e1;
6985 struct c_type_name *t1, *t2;
6986 location_t loc = c_parser_peek_token (parser)->location;;
6987 expr.original_code = ERROR_MARK;
6988 expr.original_type = NULL;
6989 switch (c_parser_peek_token (parser)->type)
6991 case CPP_NUMBER:
6992 expr.value = c_parser_peek_token (parser)->value;
6993 loc = c_parser_peek_token (parser)->location;
6994 c_parser_consume_token (parser);
6995 if (TREE_CODE (expr.value) == FIXED_CST
6996 && !targetm.fixed_point_supported_p ())
6998 error_at (loc, "fixed-point types not supported for this target");
6999 expr.value = error_mark_node;
7001 break;
7002 case CPP_CHAR:
7003 case CPP_CHAR16:
7004 case CPP_CHAR32:
7005 case CPP_WCHAR:
7006 expr.value = c_parser_peek_token (parser)->value;
7007 c_parser_consume_token (parser);
7008 break;
7009 case CPP_STRING:
7010 case CPP_STRING16:
7011 case CPP_STRING32:
7012 case CPP_WSTRING:
7013 case CPP_UTF8STRING:
7014 expr.value = c_parser_peek_token (parser)->value;
7015 expr.original_code = STRING_CST;
7016 c_parser_consume_token (parser);
7017 break;
7018 case CPP_OBJC_STRING:
7019 gcc_assert (c_dialect_objc ());
7020 expr.value
7021 = objc_build_string_object (c_parser_peek_token (parser)->value);
7022 c_parser_consume_token (parser);
7023 break;
7024 case CPP_NAME:
7025 switch (c_parser_peek_token (parser)->id_kind)
7027 case C_ID_ID:
7029 tree id = c_parser_peek_token (parser)->value;
7030 c_parser_consume_token (parser);
7031 expr.value = build_external_ref (loc, id,
7032 (c_parser_peek_token (parser)->type
7033 == CPP_OPEN_PAREN),
7034 &expr.original_type);
7035 break;
7037 case C_ID_CLASSNAME:
7039 /* Here we parse the Objective-C 2.0 Class.name dot
7040 syntax. */
7041 tree class_name = c_parser_peek_token (parser)->value;
7042 tree component;
7043 c_parser_consume_token (parser);
7044 gcc_assert (c_dialect_objc ());
7045 if (!c_parser_require (parser, CPP_DOT, "expected %<.%>"))
7047 expr.value = error_mark_node;
7048 break;
7050 if (c_parser_next_token_is_not (parser, CPP_NAME))
7052 c_parser_error (parser, "expected identifier");
7053 expr.value = error_mark_node;
7054 break;
7056 component = c_parser_peek_token (parser)->value;
7057 c_parser_consume_token (parser);
7058 expr.value = objc_build_class_component_ref (class_name,
7059 component);
7060 break;
7062 default:
7063 c_parser_error (parser, "expected expression");
7064 expr.value = error_mark_node;
7065 break;
7067 break;
7068 case CPP_OPEN_PAREN:
7069 /* A parenthesized expression, statement expression or compound
7070 literal. */
7071 if (c_parser_peek_2nd_token (parser)->type == CPP_OPEN_BRACE)
7073 /* A statement expression. */
7074 tree stmt;
7075 location_t brace_loc;
7076 c_parser_consume_token (parser);
7077 brace_loc = c_parser_peek_token (parser)->location;
7078 c_parser_consume_token (parser);
7079 if (!building_stmt_list_p ())
7081 error_at (loc, "braced-group within expression allowed "
7082 "only inside a function");
7083 parser->error = true;
7084 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
7085 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7086 expr.value = error_mark_node;
7087 break;
7089 stmt = c_begin_stmt_expr ();
7090 c_parser_compound_statement_nostart (parser);
7091 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7092 "expected %<)%>");
7093 pedwarn (loc, OPT_Wpedantic,
7094 "ISO C forbids braced-groups within expressions");
7095 expr.value = c_finish_stmt_expr (brace_loc, stmt);
7096 mark_exp_read (expr.value);
7098 else if (c_token_starts_typename (c_parser_peek_2nd_token (parser)))
7100 /* A compound literal. ??? Can we actually get here rather
7101 than going directly to
7102 c_parser_postfix_expression_after_paren_type from
7103 elsewhere? */
7104 location_t loc;
7105 struct c_type_name *type_name;
7106 c_parser_consume_token (parser);
7107 loc = c_parser_peek_token (parser)->location;
7108 type_name = c_parser_type_name (parser);
7109 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7110 "expected %<)%>");
7111 if (type_name == NULL)
7113 expr.value = error_mark_node;
7115 else
7116 expr = c_parser_postfix_expression_after_paren_type (parser,
7117 type_name,
7118 loc);
7120 else
7122 /* A parenthesized expression. */
7123 c_parser_consume_token (parser);
7124 expr = c_parser_expression (parser);
7125 if (TREE_CODE (expr.value) == MODIFY_EXPR)
7126 TREE_NO_WARNING (expr.value) = 1;
7127 if (expr.original_code != C_MAYBE_CONST_EXPR)
7128 expr.original_code = ERROR_MARK;
7129 /* Don't change EXPR.ORIGINAL_TYPE. */
7130 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7131 "expected %<)%>");
7133 break;
7134 case CPP_KEYWORD:
7135 switch (c_parser_peek_token (parser)->keyword)
7137 case RID_FUNCTION_NAME:
7138 case RID_PRETTY_FUNCTION_NAME:
7139 case RID_C99_FUNCTION_NAME:
7140 expr.value = fname_decl (loc,
7141 c_parser_peek_token (parser)->keyword,
7142 c_parser_peek_token (parser)->value);
7143 c_parser_consume_token (parser);
7144 break;
7145 case RID_VA_ARG:
7146 c_parser_consume_token (parser);
7147 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7149 expr.value = error_mark_node;
7150 break;
7152 e1 = c_parser_expr_no_commas (parser, NULL);
7153 mark_exp_read (e1.value);
7154 e1.value = c_fully_fold (e1.value, false, NULL);
7155 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
7157 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7158 expr.value = error_mark_node;
7159 break;
7161 loc = c_parser_peek_token (parser)->location;
7162 t1 = c_parser_type_name (parser);
7163 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7164 "expected %<)%>");
7165 if (t1 == NULL)
7167 expr.value = error_mark_node;
7169 else
7171 tree type_expr = NULL_TREE;
7172 expr.value = c_build_va_arg (loc, e1.value,
7173 groktypename (t1, &type_expr, NULL));
7174 if (type_expr)
7176 expr.value = build2 (C_MAYBE_CONST_EXPR,
7177 TREE_TYPE (expr.value), type_expr,
7178 expr.value);
7179 C_MAYBE_CONST_EXPR_NON_CONST (expr.value) = true;
7182 break;
7183 case RID_OFFSETOF:
7184 c_parser_consume_token (parser);
7185 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7187 expr.value = error_mark_node;
7188 break;
7190 t1 = c_parser_type_name (parser);
7191 if (t1 == NULL)
7192 parser->error = true;
7193 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
7194 gcc_assert (parser->error);
7195 if (parser->error)
7197 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7198 expr.value = error_mark_node;
7199 break;
7203 tree type = groktypename (t1, NULL, NULL);
7204 tree offsetof_ref;
7205 if (type == error_mark_node)
7206 offsetof_ref = error_mark_node;
7207 else
7209 offsetof_ref = build1 (INDIRECT_REF, type, null_pointer_node);
7210 SET_EXPR_LOCATION (offsetof_ref, loc);
7212 /* Parse the second argument to __builtin_offsetof. We
7213 must have one identifier, and beyond that we want to
7214 accept sub structure and sub array references. */
7215 if (c_parser_next_token_is (parser, CPP_NAME))
7217 offsetof_ref = build_component_ref
7218 (loc, offsetof_ref, c_parser_peek_token (parser)->value);
7219 c_parser_consume_token (parser);
7220 while (c_parser_next_token_is (parser, CPP_DOT)
7221 || c_parser_next_token_is (parser,
7222 CPP_OPEN_SQUARE)
7223 || c_parser_next_token_is (parser,
7224 CPP_DEREF))
7226 if (c_parser_next_token_is (parser, CPP_DEREF))
7228 loc = c_parser_peek_token (parser)->location;
7229 offsetof_ref = build_array_ref (loc,
7230 offsetof_ref,
7231 integer_zero_node);
7232 goto do_dot;
7234 else if (c_parser_next_token_is (parser, CPP_DOT))
7236 do_dot:
7237 c_parser_consume_token (parser);
7238 if (c_parser_next_token_is_not (parser,
7239 CPP_NAME))
7241 c_parser_error (parser, "expected identifier");
7242 break;
7244 offsetof_ref = build_component_ref
7245 (loc, offsetof_ref,
7246 c_parser_peek_token (parser)->value);
7247 c_parser_consume_token (parser);
7249 else
7251 struct c_expr ce;
7252 tree idx;
7253 loc = c_parser_peek_token (parser)->location;
7254 c_parser_consume_token (parser);
7255 ce = c_parser_expression (parser);
7256 ce = convert_lvalue_to_rvalue (loc, ce, false, false);
7257 idx = ce.value;
7258 idx = c_fully_fold (idx, false, NULL);
7259 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
7260 "expected %<]%>");
7261 offsetof_ref = build_array_ref (loc, offsetof_ref, idx);
7265 else
7266 c_parser_error (parser, "expected identifier");
7267 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7268 "expected %<)%>");
7269 expr.value = fold_offsetof (offsetof_ref);
7271 break;
7272 case RID_CHOOSE_EXPR:
7274 vec<c_expr_t, va_gc> *cexpr_list;
7275 c_expr_t *e1_p, *e2_p, *e3_p;
7276 tree c;
7278 c_parser_consume_token (parser);
7279 if (!c_parser_get_builtin_args (parser,
7280 "__builtin_choose_expr",
7281 &cexpr_list, true))
7283 expr.value = error_mark_node;
7284 break;
7287 if (vec_safe_length (cexpr_list) != 3)
7289 error_at (loc, "wrong number of arguments to "
7290 "%<__builtin_choose_expr%>");
7291 expr.value = error_mark_node;
7292 break;
7295 e1_p = &(*cexpr_list)[0];
7296 e2_p = &(*cexpr_list)[1];
7297 e3_p = &(*cexpr_list)[2];
7299 c = e1_p->value;
7300 mark_exp_read (e2_p->value);
7301 mark_exp_read (e3_p->value);
7302 if (TREE_CODE (c) != INTEGER_CST
7303 || !INTEGRAL_TYPE_P (TREE_TYPE (c)))
7304 error_at (loc,
7305 "first argument to %<__builtin_choose_expr%> not"
7306 " a constant");
7307 constant_expression_warning (c);
7308 expr = integer_zerop (c) ? *e3_p : *e2_p;
7309 break;
7311 case RID_TYPES_COMPATIBLE_P:
7312 c_parser_consume_token (parser);
7313 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7315 expr.value = error_mark_node;
7316 break;
7318 t1 = c_parser_type_name (parser);
7319 if (t1 == NULL)
7321 expr.value = error_mark_node;
7322 break;
7324 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
7326 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7327 expr.value = error_mark_node;
7328 break;
7330 t2 = c_parser_type_name (parser);
7331 if (t2 == NULL)
7333 expr.value = error_mark_node;
7334 break;
7336 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7337 "expected %<)%>");
7339 tree e1, e2;
7340 e1 = groktypename (t1, NULL, NULL);
7341 e2 = groktypename (t2, NULL, NULL);
7342 if (e1 == error_mark_node || e2 == error_mark_node)
7344 expr.value = error_mark_node;
7345 break;
7348 e1 = TYPE_MAIN_VARIANT (e1);
7349 e2 = TYPE_MAIN_VARIANT (e2);
7351 expr.value
7352 = comptypes (e1, e2) ? integer_one_node : integer_zero_node;
7354 break;
7355 case RID_BUILTIN_COMPLEX:
7357 vec<c_expr_t, va_gc> *cexpr_list;
7358 c_expr_t *e1_p, *e2_p;
7360 c_parser_consume_token (parser);
7361 if (!c_parser_get_builtin_args (parser,
7362 "__builtin_complex",
7363 &cexpr_list, false))
7365 expr.value = error_mark_node;
7366 break;
7369 if (vec_safe_length (cexpr_list) != 2)
7371 error_at (loc, "wrong number of arguments to "
7372 "%<__builtin_complex%>");
7373 expr.value = error_mark_node;
7374 break;
7377 e1_p = &(*cexpr_list)[0];
7378 e2_p = &(*cexpr_list)[1];
7380 *e1_p = convert_lvalue_to_rvalue (loc, *e1_p, true, true);
7381 if (TREE_CODE (e1_p->value) == EXCESS_PRECISION_EXPR)
7382 e1_p->value = convert (TREE_TYPE (e1_p->value),
7383 TREE_OPERAND (e1_p->value, 0));
7384 *e2_p = convert_lvalue_to_rvalue (loc, *e2_p, true, true);
7385 if (TREE_CODE (e2_p->value) == EXCESS_PRECISION_EXPR)
7386 e2_p->value = convert (TREE_TYPE (e2_p->value),
7387 TREE_OPERAND (e2_p->value, 0));
7388 if (!SCALAR_FLOAT_TYPE_P (TREE_TYPE (e1_p->value))
7389 || DECIMAL_FLOAT_TYPE_P (TREE_TYPE (e1_p->value))
7390 || !SCALAR_FLOAT_TYPE_P (TREE_TYPE (e2_p->value))
7391 || DECIMAL_FLOAT_TYPE_P (TREE_TYPE (e2_p->value)))
7393 error_at (loc, "%<__builtin_complex%> operand "
7394 "not of real binary floating-point type");
7395 expr.value = error_mark_node;
7396 break;
7398 if (TYPE_MAIN_VARIANT (TREE_TYPE (e1_p->value))
7399 != TYPE_MAIN_VARIANT (TREE_TYPE (e2_p->value)))
7401 error_at (loc,
7402 "%<__builtin_complex%> operands of different types");
7403 expr.value = error_mark_node;
7404 break;
7406 if (!flag_isoc99)
7407 pedwarn (loc, OPT_Wpedantic,
7408 "ISO C90 does not support complex types");
7409 expr.value = build2 (COMPLEX_EXPR,
7410 build_complex_type
7411 (TYPE_MAIN_VARIANT
7412 (TREE_TYPE (e1_p->value))),
7413 e1_p->value, e2_p->value);
7414 break;
7416 case RID_BUILTIN_SHUFFLE:
7418 vec<c_expr_t, va_gc> *cexpr_list;
7419 unsigned int i;
7420 c_expr_t *p;
7422 c_parser_consume_token (parser);
7423 if (!c_parser_get_builtin_args (parser,
7424 "__builtin_shuffle",
7425 &cexpr_list, false))
7427 expr.value = error_mark_node;
7428 break;
7431 FOR_EACH_VEC_SAFE_ELT (cexpr_list, i, p)
7432 *p = convert_lvalue_to_rvalue (loc, *p, true, true);
7434 if (vec_safe_length (cexpr_list) == 2)
7435 expr.value =
7436 c_build_vec_perm_expr
7437 (loc, (*cexpr_list)[0].value,
7438 NULL_TREE, (*cexpr_list)[1].value);
7440 else if (vec_safe_length (cexpr_list) == 3)
7441 expr.value =
7442 c_build_vec_perm_expr
7443 (loc, (*cexpr_list)[0].value,
7444 (*cexpr_list)[1].value,
7445 (*cexpr_list)[2].value);
7446 else
7448 error_at (loc, "wrong number of arguments to "
7449 "%<__builtin_shuffle%>");
7450 expr.value = error_mark_node;
7452 break;
7454 case RID_AT_SELECTOR:
7455 gcc_assert (c_dialect_objc ());
7456 c_parser_consume_token (parser);
7457 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7459 expr.value = error_mark_node;
7460 break;
7463 tree sel = c_parser_objc_selector_arg (parser);
7464 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7465 "expected %<)%>");
7466 expr.value = objc_build_selector_expr (loc, sel);
7468 break;
7469 case RID_AT_PROTOCOL:
7470 gcc_assert (c_dialect_objc ());
7471 c_parser_consume_token (parser);
7472 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7474 expr.value = error_mark_node;
7475 break;
7477 if (c_parser_next_token_is_not (parser, CPP_NAME))
7479 c_parser_error (parser, "expected identifier");
7480 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7481 expr.value = error_mark_node;
7482 break;
7485 tree id = c_parser_peek_token (parser)->value;
7486 c_parser_consume_token (parser);
7487 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7488 "expected %<)%>");
7489 expr.value = objc_build_protocol_expr (id);
7491 break;
7492 case RID_AT_ENCODE:
7493 /* Extension to support C-structures in the archiver. */
7494 gcc_assert (c_dialect_objc ());
7495 c_parser_consume_token (parser);
7496 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7498 expr.value = error_mark_node;
7499 break;
7501 t1 = c_parser_type_name (parser);
7502 if (t1 == NULL)
7504 expr.value = error_mark_node;
7505 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7506 break;
7508 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7509 "expected %<)%>");
7511 tree type = groktypename (t1, NULL, NULL);
7512 expr.value = objc_build_encode_expr (type);
7514 break;
7515 case RID_GENERIC:
7516 expr = c_parser_generic_selection (parser);
7517 break;
7518 case RID_CILK_SPAWN:
7519 c_parser_consume_token (parser);
7520 if (!flag_cilkplus)
7522 error_at (loc, "-fcilkplus must be enabled to use "
7523 "%<_Cilk_spawn%>");
7524 expr = c_parser_postfix_expression (parser);
7525 expr.value = error_mark_node;
7527 else if (c_parser_peek_token (parser)->keyword == RID_CILK_SPAWN)
7529 error_at (loc, "consecutive %<_Cilk_spawn%> keywords "
7530 "are not permitted");
7531 /* Now flush out all the _Cilk_spawns. */
7532 while (c_parser_peek_token (parser)->keyword == RID_CILK_SPAWN)
7533 c_parser_consume_token (parser);
7534 expr = c_parser_postfix_expression (parser);
7536 else
7538 expr = c_parser_postfix_expression (parser);
7539 expr.value = build_cilk_spawn (loc, expr.value);
7541 break;
7542 default:
7543 c_parser_error (parser, "expected expression");
7544 expr.value = error_mark_node;
7545 break;
7547 break;
7548 case CPP_OPEN_SQUARE:
7549 if (c_dialect_objc ())
7551 tree receiver, args;
7552 c_parser_consume_token (parser);
7553 receiver = c_parser_objc_receiver (parser);
7554 args = c_parser_objc_message_args (parser);
7555 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
7556 "expected %<]%>");
7557 expr.value = objc_build_message_expr (receiver, args);
7558 break;
7560 /* Else fall through to report error. */
7561 default:
7562 c_parser_error (parser, "expected expression");
7563 expr.value = error_mark_node;
7564 break;
7566 return c_parser_postfix_expression_after_primary (parser, loc, expr);
7569 /* Parse a postfix expression after a parenthesized type name: the
7570 brace-enclosed initializer of a compound literal, possibly followed
7571 by some postfix operators. This is separate because it is not
7572 possible to tell until after the type name whether a cast
7573 expression has a cast or a compound literal, or whether the operand
7574 of sizeof is a parenthesized type name or starts with a compound
7575 literal. TYPE_LOC is the location where TYPE_NAME starts--the
7576 location of the first token after the parentheses around the type
7577 name. */
7579 static struct c_expr
7580 c_parser_postfix_expression_after_paren_type (c_parser *parser,
7581 struct c_type_name *type_name,
7582 location_t type_loc)
7584 tree type;
7585 struct c_expr init;
7586 bool non_const;
7587 struct c_expr expr;
7588 location_t start_loc;
7589 tree type_expr = NULL_TREE;
7590 bool type_expr_const = true;
7591 check_compound_literal_type (type_loc, type_name);
7592 start_init (NULL_TREE, NULL, 0);
7593 type = groktypename (type_name, &type_expr, &type_expr_const);
7594 start_loc = c_parser_peek_token (parser)->location;
7595 if (type != error_mark_node && C_TYPE_VARIABLE_SIZE (type))
7597 error_at (type_loc, "compound literal has variable size");
7598 type = error_mark_node;
7600 init = c_parser_braced_init (parser, type, false);
7601 finish_init ();
7602 maybe_warn_string_init (type_loc, type, init);
7604 if (type != error_mark_node
7605 && !ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (type))
7606 && current_function_decl)
7608 error ("compound literal qualified by address-space qualifier");
7609 type = error_mark_node;
7612 if (!flag_isoc99)
7613 pedwarn (start_loc, OPT_Wpedantic, "ISO C90 forbids compound literals");
7614 non_const = ((init.value && TREE_CODE (init.value) == CONSTRUCTOR)
7615 ? CONSTRUCTOR_NON_CONST (init.value)
7616 : init.original_code == C_MAYBE_CONST_EXPR);
7617 non_const |= !type_expr_const;
7618 expr.value = build_compound_literal (start_loc, type, init.value, non_const);
7619 expr.original_code = ERROR_MARK;
7620 expr.original_type = NULL;
7621 if (type_expr)
7623 if (TREE_CODE (expr.value) == C_MAYBE_CONST_EXPR)
7625 gcc_assert (C_MAYBE_CONST_EXPR_PRE (expr.value) == NULL_TREE);
7626 C_MAYBE_CONST_EXPR_PRE (expr.value) = type_expr;
7628 else
7630 gcc_assert (!non_const);
7631 expr.value = build2 (C_MAYBE_CONST_EXPR, type,
7632 type_expr, expr.value);
7635 return c_parser_postfix_expression_after_primary (parser, start_loc, expr);
7638 /* Callback function for sizeof_pointer_memaccess_warning to compare
7639 types. */
7641 static bool
7642 sizeof_ptr_memacc_comptypes (tree type1, tree type2)
7644 return comptypes (type1, type2) == 1;
7647 /* Parse a postfix expression after the initial primary or compound
7648 literal; that is, parse a series of postfix operators.
7650 EXPR_LOC is the location of the primary expression. */
7652 static struct c_expr
7653 c_parser_postfix_expression_after_primary (c_parser *parser,
7654 location_t expr_loc,
7655 struct c_expr expr)
7657 struct c_expr orig_expr;
7658 tree ident, idx;
7659 location_t sizeof_arg_loc[3];
7660 tree sizeof_arg[3];
7661 unsigned int i;
7662 vec<tree, va_gc> *exprlist;
7663 vec<tree, va_gc> *origtypes = NULL;
7664 vec<location_t> arg_loc = vNULL;
7666 while (true)
7668 location_t op_loc = c_parser_peek_token (parser)->location;
7669 switch (c_parser_peek_token (parser)->type)
7671 case CPP_OPEN_SQUARE:
7672 /* Array reference. */
7673 c_parser_consume_token (parser);
7674 if (flag_cilkplus
7675 && c_parser_peek_token (parser)->type == CPP_COLON)
7676 /* If we are here, then we have something like this:
7677 Array [ : ]
7679 expr.value = c_parser_array_notation (expr_loc, parser, NULL_TREE,
7680 expr.value);
7681 else
7683 idx = c_parser_expression (parser).value;
7684 /* Here we have 3 options:
7685 1. Array [EXPR] -- Normal Array call.
7686 2. Array [EXPR : EXPR] -- Array notation without stride.
7687 3. Array [EXPR : EXPR : EXPR] -- Array notation with stride.
7689 For 1, we just handle it just like a normal array expression.
7690 For 2 and 3 we handle it like we handle array notations. The
7691 idx value we have above becomes the initial/start index.
7693 if (flag_cilkplus
7694 && c_parser_peek_token (parser)->type == CPP_COLON)
7695 expr.value = c_parser_array_notation (expr_loc, parser, idx,
7696 expr.value);
7697 else
7699 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
7700 "expected %<]%>");
7701 expr.value = build_array_ref (op_loc, expr.value, idx);
7704 expr.original_code = ERROR_MARK;
7705 expr.original_type = NULL;
7706 break;
7707 case CPP_OPEN_PAREN:
7708 /* Function call. */
7709 c_parser_consume_token (parser);
7710 for (i = 0; i < 3; i++)
7712 sizeof_arg[i] = NULL_TREE;
7713 sizeof_arg_loc[i] = UNKNOWN_LOCATION;
7715 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
7716 exprlist = NULL;
7717 else
7718 exprlist = c_parser_expr_list (parser, true, false, &origtypes,
7719 sizeof_arg_loc, sizeof_arg,
7720 &arg_loc);
7721 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7722 "expected %<)%>");
7723 orig_expr = expr;
7724 mark_exp_read (expr.value);
7725 if (warn_sizeof_pointer_memaccess)
7726 sizeof_pointer_memaccess_warning (sizeof_arg_loc,
7727 expr.value, exprlist,
7728 sizeof_arg,
7729 sizeof_ptr_memacc_comptypes);
7730 expr.value
7731 = c_build_function_call_vec (expr_loc, arg_loc, expr.value,
7732 exprlist, origtypes);
7733 expr.original_code = ERROR_MARK;
7734 if (TREE_CODE (expr.value) == INTEGER_CST
7735 && TREE_CODE (orig_expr.value) == FUNCTION_DECL
7736 && DECL_BUILT_IN_CLASS (orig_expr.value) == BUILT_IN_NORMAL
7737 && DECL_FUNCTION_CODE (orig_expr.value) == BUILT_IN_CONSTANT_P)
7738 expr.original_code = C_MAYBE_CONST_EXPR;
7739 expr.original_type = NULL;
7740 if (exprlist)
7742 release_tree_vector (exprlist);
7743 release_tree_vector (origtypes);
7745 arg_loc.release ();
7746 break;
7747 case CPP_DOT:
7748 /* Structure element reference. */
7749 c_parser_consume_token (parser);
7750 expr = default_function_array_conversion (expr_loc, expr);
7751 if (c_parser_next_token_is (parser, CPP_NAME))
7752 ident = c_parser_peek_token (parser)->value;
7753 else
7755 c_parser_error (parser, "expected identifier");
7756 expr.value = error_mark_node;
7757 expr.original_code = ERROR_MARK;
7758 expr.original_type = NULL;
7759 return expr;
7761 c_parser_consume_token (parser);
7762 expr.value = build_component_ref (op_loc, expr.value, 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_DEREF:
7777 /* Structure element reference. */
7778 c_parser_consume_token (parser);
7779 expr = convert_lvalue_to_rvalue (expr_loc, expr, true, false);
7780 if (c_parser_next_token_is (parser, CPP_NAME))
7781 ident = c_parser_peek_token (parser)->value;
7782 else
7784 c_parser_error (parser, "expected identifier");
7785 expr.value = error_mark_node;
7786 expr.original_code = ERROR_MARK;
7787 expr.original_type = NULL;
7788 return expr;
7790 c_parser_consume_token (parser);
7791 expr.value = build_component_ref (op_loc,
7792 build_indirect_ref (op_loc,
7793 expr.value,
7794 RO_ARROW),
7795 ident);
7796 expr.original_code = ERROR_MARK;
7797 if (TREE_CODE (expr.value) != COMPONENT_REF)
7798 expr.original_type = NULL;
7799 else
7801 /* Remember the original type of a bitfield. */
7802 tree field = TREE_OPERAND (expr.value, 1);
7803 if (TREE_CODE (field) != FIELD_DECL)
7804 expr.original_type = NULL;
7805 else
7806 expr.original_type = DECL_BIT_FIELD_TYPE (field);
7808 break;
7809 case CPP_PLUS_PLUS:
7810 /* Postincrement. */
7811 c_parser_consume_token (parser);
7812 /* If the expressions have array notations, we expand them. */
7813 if (flag_cilkplus
7814 && TREE_CODE (expr.value) == ARRAY_NOTATION_REF)
7815 expr = fix_array_notation_expr (expr_loc, POSTINCREMENT_EXPR, expr);
7816 else
7818 expr = default_function_array_read_conversion (expr_loc, expr);
7819 expr.value = build_unary_op (op_loc,
7820 POSTINCREMENT_EXPR, expr.value, 0);
7822 expr.original_code = ERROR_MARK;
7823 expr.original_type = NULL;
7824 break;
7825 case CPP_MINUS_MINUS:
7826 /* Postdecrement. */
7827 c_parser_consume_token (parser);
7828 /* If the expressions have array notations, we expand them. */
7829 if (flag_cilkplus
7830 && TREE_CODE (expr.value) == ARRAY_NOTATION_REF)
7831 expr = fix_array_notation_expr (expr_loc, POSTDECREMENT_EXPR, expr);
7832 else
7834 expr = default_function_array_read_conversion (expr_loc, expr);
7835 expr.value = build_unary_op (op_loc,
7836 POSTDECREMENT_EXPR, expr.value, 0);
7838 expr.original_code = ERROR_MARK;
7839 expr.original_type = NULL;
7840 break;
7841 default:
7842 return expr;
7847 /* Parse an expression (C90 6.3.17, C99 6.5.17).
7849 expression:
7850 assignment-expression
7851 expression , assignment-expression
7854 static struct c_expr
7855 c_parser_expression (c_parser *parser)
7857 location_t tloc = c_parser_peek_token (parser)->location;
7858 struct c_expr expr;
7859 expr = c_parser_expr_no_commas (parser, NULL);
7860 if (c_parser_next_token_is (parser, CPP_COMMA))
7861 expr = convert_lvalue_to_rvalue (tloc, expr, true, false);
7862 while (c_parser_next_token_is (parser, CPP_COMMA))
7864 struct c_expr next;
7865 tree lhsval;
7866 location_t loc = c_parser_peek_token (parser)->location;
7867 location_t expr_loc;
7868 c_parser_consume_token (parser);
7869 expr_loc = c_parser_peek_token (parser)->location;
7870 lhsval = expr.value;
7871 while (TREE_CODE (lhsval) == COMPOUND_EXPR)
7872 lhsval = TREE_OPERAND (lhsval, 1);
7873 if (DECL_P (lhsval) || handled_component_p (lhsval))
7874 mark_exp_read (lhsval);
7875 next = c_parser_expr_no_commas (parser, NULL);
7876 next = convert_lvalue_to_rvalue (expr_loc, next, true, false);
7877 expr.value = build_compound_expr (loc, expr.value, next.value);
7878 expr.original_code = COMPOUND_EXPR;
7879 expr.original_type = next.original_type;
7881 return expr;
7884 /* Parse an expression and convert functions or arrays to pointers and
7885 lvalues to rvalues. */
7887 static struct c_expr
7888 c_parser_expression_conv (c_parser *parser)
7890 struct c_expr expr;
7891 location_t loc = c_parser_peek_token (parser)->location;
7892 expr = c_parser_expression (parser);
7893 expr = convert_lvalue_to_rvalue (loc, expr, true, false);
7894 return expr;
7897 /* Parse a non-empty list of expressions. If CONVERT_P, convert
7898 functions and arrays to pointers and lvalues to rvalues. If
7899 FOLD_P, fold the expressions. If LOCATIONS is non-NULL, save the
7900 locations of function arguments into this vector.
7902 nonempty-expr-list:
7903 assignment-expression
7904 nonempty-expr-list , assignment-expression
7907 static vec<tree, va_gc> *
7908 c_parser_expr_list (c_parser *parser, bool convert_p, bool fold_p,
7909 vec<tree, va_gc> **p_orig_types,
7910 location_t *sizeof_arg_loc, tree *sizeof_arg,
7911 vec<location_t> *locations)
7913 vec<tree, va_gc> *ret;
7914 vec<tree, va_gc> *orig_types;
7915 struct c_expr expr;
7916 location_t loc = c_parser_peek_token (parser)->location;
7917 location_t cur_sizeof_arg_loc = UNKNOWN_LOCATION;
7918 unsigned int idx = 0;
7920 ret = make_tree_vector ();
7921 if (p_orig_types == NULL)
7922 orig_types = NULL;
7923 else
7924 orig_types = make_tree_vector ();
7926 if (sizeof_arg != NULL
7927 && c_parser_next_token_is_keyword (parser, RID_SIZEOF))
7928 cur_sizeof_arg_loc = c_parser_peek_2nd_token (parser)->location;
7929 expr = c_parser_expr_no_commas (parser, NULL);
7930 if (convert_p)
7931 expr = convert_lvalue_to_rvalue (loc, expr, true, true);
7932 if (fold_p)
7933 expr.value = c_fully_fold (expr.value, false, NULL);
7934 ret->quick_push (expr.value);
7935 if (orig_types)
7936 orig_types->quick_push (expr.original_type);
7937 if (locations)
7938 locations->safe_push (loc);
7939 if (sizeof_arg != NULL
7940 && cur_sizeof_arg_loc != UNKNOWN_LOCATION
7941 && expr.original_code == SIZEOF_EXPR)
7943 sizeof_arg[0] = c_last_sizeof_arg;
7944 sizeof_arg_loc[0] = cur_sizeof_arg_loc;
7946 while (c_parser_next_token_is (parser, CPP_COMMA))
7948 c_parser_consume_token (parser);
7949 loc = c_parser_peek_token (parser)->location;
7950 if (sizeof_arg != NULL
7951 && c_parser_next_token_is_keyword (parser, RID_SIZEOF))
7952 cur_sizeof_arg_loc = c_parser_peek_2nd_token (parser)->location;
7953 else
7954 cur_sizeof_arg_loc = UNKNOWN_LOCATION;
7955 expr = c_parser_expr_no_commas (parser, NULL);
7956 if (convert_p)
7957 expr = convert_lvalue_to_rvalue (loc, expr, true, true);
7958 if (fold_p)
7959 expr.value = c_fully_fold (expr.value, false, NULL);
7960 vec_safe_push (ret, expr.value);
7961 if (orig_types)
7962 vec_safe_push (orig_types, expr.original_type);
7963 if (locations)
7964 locations->safe_push (loc);
7965 if (++idx < 3
7966 && sizeof_arg != NULL
7967 && cur_sizeof_arg_loc != UNKNOWN_LOCATION
7968 && expr.original_code == SIZEOF_EXPR)
7970 sizeof_arg[idx] = c_last_sizeof_arg;
7971 sizeof_arg_loc[idx] = cur_sizeof_arg_loc;
7974 if (orig_types)
7975 *p_orig_types = orig_types;
7976 return ret;
7979 /* Parse Objective-C-specific constructs. */
7981 /* Parse an objc-class-definition.
7983 objc-class-definition:
7984 @interface identifier objc-superclass[opt] objc-protocol-refs[opt]
7985 objc-class-instance-variables[opt] objc-methodprotolist @end
7986 @implementation identifier objc-superclass[opt]
7987 objc-class-instance-variables[opt]
7988 @interface identifier ( identifier ) objc-protocol-refs[opt]
7989 objc-methodprotolist @end
7990 @interface identifier ( ) objc-protocol-refs[opt]
7991 objc-methodprotolist @end
7992 @implementation identifier ( identifier )
7994 objc-superclass:
7995 : identifier
7997 "@interface identifier (" must start "@interface identifier (
7998 identifier ) ...": objc-methodprotolist in the first production may
7999 not start with a parenthesized identifier as a declarator of a data
8000 definition with no declaration specifiers if the objc-superclass,
8001 objc-protocol-refs and objc-class-instance-variables are omitted. */
8003 static void
8004 c_parser_objc_class_definition (c_parser *parser, tree attributes)
8006 bool iface_p;
8007 tree id1;
8008 tree superclass;
8009 if (c_parser_next_token_is_keyword (parser, RID_AT_INTERFACE))
8010 iface_p = true;
8011 else if (c_parser_next_token_is_keyword (parser, RID_AT_IMPLEMENTATION))
8012 iface_p = false;
8013 else
8014 gcc_unreachable ();
8016 c_parser_consume_token (parser);
8017 if (c_parser_next_token_is_not (parser, CPP_NAME))
8019 c_parser_error (parser, "expected identifier");
8020 return;
8022 id1 = c_parser_peek_token (parser)->value;
8023 c_parser_consume_token (parser);
8024 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
8026 /* We have a category or class extension. */
8027 tree id2;
8028 tree proto = NULL_TREE;
8029 c_parser_consume_token (parser);
8030 if (c_parser_next_token_is_not (parser, CPP_NAME))
8032 if (iface_p && c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
8034 /* We have a class extension. */
8035 id2 = NULL_TREE;
8037 else
8039 c_parser_error (parser, "expected identifier or %<)%>");
8040 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
8041 return;
8044 else
8046 id2 = c_parser_peek_token (parser)->value;
8047 c_parser_consume_token (parser);
8049 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
8050 if (!iface_p)
8052 objc_start_category_implementation (id1, id2);
8053 return;
8055 if (c_parser_next_token_is (parser, CPP_LESS))
8056 proto = c_parser_objc_protocol_refs (parser);
8057 objc_start_category_interface (id1, id2, proto, attributes);
8058 c_parser_objc_methodprotolist (parser);
8059 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
8060 objc_finish_interface ();
8061 return;
8063 if (c_parser_next_token_is (parser, CPP_COLON))
8065 c_parser_consume_token (parser);
8066 if (c_parser_next_token_is_not (parser, CPP_NAME))
8068 c_parser_error (parser, "expected identifier");
8069 return;
8071 superclass = c_parser_peek_token (parser)->value;
8072 c_parser_consume_token (parser);
8074 else
8075 superclass = NULL_TREE;
8076 if (iface_p)
8078 tree proto = NULL_TREE;
8079 if (c_parser_next_token_is (parser, CPP_LESS))
8080 proto = c_parser_objc_protocol_refs (parser);
8081 objc_start_class_interface (id1, superclass, proto, attributes);
8083 else
8084 objc_start_class_implementation (id1, superclass);
8085 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
8086 c_parser_objc_class_instance_variables (parser);
8087 if (iface_p)
8089 objc_continue_interface ();
8090 c_parser_objc_methodprotolist (parser);
8091 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
8092 objc_finish_interface ();
8094 else
8096 objc_continue_implementation ();
8097 return;
8101 /* Parse objc-class-instance-variables.
8103 objc-class-instance-variables:
8104 { objc-instance-variable-decl-list[opt] }
8106 objc-instance-variable-decl-list:
8107 objc-visibility-spec
8108 objc-instance-variable-decl ;
8110 objc-instance-variable-decl-list objc-visibility-spec
8111 objc-instance-variable-decl-list objc-instance-variable-decl ;
8112 objc-instance-variable-decl-list ;
8114 objc-visibility-spec:
8115 @private
8116 @protected
8117 @public
8119 objc-instance-variable-decl:
8120 struct-declaration
8123 static void
8124 c_parser_objc_class_instance_variables (c_parser *parser)
8126 gcc_assert (c_parser_next_token_is (parser, CPP_OPEN_BRACE));
8127 c_parser_consume_token (parser);
8128 while (c_parser_next_token_is_not (parser, CPP_EOF))
8130 tree decls;
8131 /* Parse any stray semicolon. */
8132 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
8134 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
8135 "extra semicolon");
8136 c_parser_consume_token (parser);
8137 continue;
8139 /* Stop if at the end of the instance variables. */
8140 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
8142 c_parser_consume_token (parser);
8143 break;
8145 /* Parse any objc-visibility-spec. */
8146 if (c_parser_next_token_is_keyword (parser, RID_AT_PRIVATE))
8148 c_parser_consume_token (parser);
8149 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
8150 continue;
8152 else if (c_parser_next_token_is_keyword (parser, RID_AT_PROTECTED))
8154 c_parser_consume_token (parser);
8155 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
8156 continue;
8158 else if (c_parser_next_token_is_keyword (parser, RID_AT_PUBLIC))
8160 c_parser_consume_token (parser);
8161 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
8162 continue;
8164 else if (c_parser_next_token_is_keyword (parser, RID_AT_PACKAGE))
8166 c_parser_consume_token (parser);
8167 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
8168 continue;
8170 else if (c_parser_next_token_is (parser, CPP_PRAGMA))
8172 c_parser_pragma (parser, pragma_external);
8173 continue;
8176 /* Parse some comma-separated declarations. */
8177 decls = c_parser_struct_declaration (parser);
8178 if (decls == NULL)
8180 /* There is a syntax error. We want to skip the offending
8181 tokens up to the next ';' (included) or '}'
8182 (excluded). */
8184 /* First, skip manually a ')' or ']'. This is because they
8185 reduce the nesting level, so c_parser_skip_until_found()
8186 wouldn't be able to skip past them. */
8187 c_token *token = c_parser_peek_token (parser);
8188 if (token->type == CPP_CLOSE_PAREN || token->type == CPP_CLOSE_SQUARE)
8189 c_parser_consume_token (parser);
8191 /* Then, do the standard skipping. */
8192 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
8194 /* We hopefully recovered. Start normal parsing again. */
8195 parser->error = false;
8196 continue;
8198 else
8200 /* Comma-separated instance variables are chained together
8201 in reverse order; add them one by one. */
8202 tree ivar = nreverse (decls);
8203 for (; ivar; ivar = DECL_CHAIN (ivar))
8204 objc_add_instance_variable (copy_node (ivar));
8206 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
8210 /* Parse an objc-class-declaration.
8212 objc-class-declaration:
8213 @class identifier-list ;
8216 static void
8217 c_parser_objc_class_declaration (c_parser *parser)
8219 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_CLASS));
8220 c_parser_consume_token (parser);
8221 /* Any identifiers, including those declared as type names, are OK
8222 here. */
8223 while (true)
8225 tree id;
8226 if (c_parser_next_token_is_not (parser, CPP_NAME))
8228 c_parser_error (parser, "expected identifier");
8229 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
8230 parser->error = false;
8231 return;
8233 id = c_parser_peek_token (parser)->value;
8234 objc_declare_class (id);
8235 c_parser_consume_token (parser);
8236 if (c_parser_next_token_is (parser, CPP_COMMA))
8237 c_parser_consume_token (parser);
8238 else
8239 break;
8241 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
8244 /* Parse an objc-alias-declaration.
8246 objc-alias-declaration:
8247 @compatibility_alias identifier identifier ;
8250 static void
8251 c_parser_objc_alias_declaration (c_parser *parser)
8253 tree id1, id2;
8254 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_ALIAS));
8255 c_parser_consume_token (parser);
8256 if (c_parser_next_token_is_not (parser, CPP_NAME))
8258 c_parser_error (parser, "expected identifier");
8259 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
8260 return;
8262 id1 = c_parser_peek_token (parser)->value;
8263 c_parser_consume_token (parser);
8264 if (c_parser_next_token_is_not (parser, CPP_NAME))
8266 c_parser_error (parser, "expected identifier");
8267 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
8268 return;
8270 id2 = c_parser_peek_token (parser)->value;
8271 c_parser_consume_token (parser);
8272 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
8273 objc_declare_alias (id1, id2);
8276 /* Parse an objc-protocol-definition.
8278 objc-protocol-definition:
8279 @protocol identifier objc-protocol-refs[opt] objc-methodprotolist @end
8280 @protocol identifier-list ;
8282 "@protocol identifier ;" should be resolved as "@protocol
8283 identifier-list ;": objc-methodprotolist may not start with a
8284 semicolon in the first alternative if objc-protocol-refs are
8285 omitted. */
8287 static void
8288 c_parser_objc_protocol_definition (c_parser *parser, tree attributes)
8290 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_PROTOCOL));
8292 c_parser_consume_token (parser);
8293 if (c_parser_next_token_is_not (parser, CPP_NAME))
8295 c_parser_error (parser, "expected identifier");
8296 return;
8298 if (c_parser_peek_2nd_token (parser)->type == CPP_COMMA
8299 || c_parser_peek_2nd_token (parser)->type == CPP_SEMICOLON)
8301 /* Any identifiers, including those declared as type names, are
8302 OK here. */
8303 while (true)
8305 tree id;
8306 if (c_parser_next_token_is_not (parser, CPP_NAME))
8308 c_parser_error (parser, "expected identifier");
8309 break;
8311 id = c_parser_peek_token (parser)->value;
8312 objc_declare_protocol (id, attributes);
8313 c_parser_consume_token (parser);
8314 if (c_parser_next_token_is (parser, CPP_COMMA))
8315 c_parser_consume_token (parser);
8316 else
8317 break;
8319 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
8321 else
8323 tree id = c_parser_peek_token (parser)->value;
8324 tree proto = NULL_TREE;
8325 c_parser_consume_token (parser);
8326 if (c_parser_next_token_is (parser, CPP_LESS))
8327 proto = c_parser_objc_protocol_refs (parser);
8328 parser->objc_pq_context = true;
8329 objc_start_protocol (id, proto, attributes);
8330 c_parser_objc_methodprotolist (parser);
8331 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
8332 parser->objc_pq_context = false;
8333 objc_finish_interface ();
8337 /* Parse an objc-method-type.
8339 objc-method-type:
8343 Return true if it is a class method (+) and false if it is
8344 an instance method (-).
8346 static inline bool
8347 c_parser_objc_method_type (c_parser *parser)
8349 switch (c_parser_peek_token (parser)->type)
8351 case CPP_PLUS:
8352 c_parser_consume_token (parser);
8353 return true;
8354 case CPP_MINUS:
8355 c_parser_consume_token (parser);
8356 return false;
8357 default:
8358 gcc_unreachable ();
8362 /* Parse an objc-method-definition.
8364 objc-method-definition:
8365 objc-method-type objc-method-decl ;[opt] compound-statement
8368 static void
8369 c_parser_objc_method_definition (c_parser *parser)
8371 bool is_class_method = c_parser_objc_method_type (parser);
8372 tree decl, attributes = NULL_TREE, expr = NULL_TREE;
8373 parser->objc_pq_context = true;
8374 decl = c_parser_objc_method_decl (parser, is_class_method, &attributes,
8375 &expr);
8376 if (decl == error_mark_node)
8377 return; /* Bail here. */
8379 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
8381 c_parser_consume_token (parser);
8382 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
8383 "extra semicolon in method definition specified");
8386 if (!c_parser_next_token_is (parser, CPP_OPEN_BRACE))
8388 c_parser_error (parser, "expected %<{%>");
8389 return;
8392 parser->objc_pq_context = false;
8393 if (objc_start_method_definition (is_class_method, decl, attributes, expr))
8395 add_stmt (c_parser_compound_statement (parser));
8396 objc_finish_method_definition (current_function_decl);
8398 else
8400 /* This code is executed when we find a method definition
8401 outside of an @implementation context (or invalid for other
8402 reasons). Parse the method (to keep going) but do not emit
8403 any code.
8405 c_parser_compound_statement (parser);
8409 /* Parse an objc-methodprotolist.
8411 objc-methodprotolist:
8412 empty
8413 objc-methodprotolist objc-methodproto
8414 objc-methodprotolist declaration
8415 objc-methodprotolist ;
8416 @optional
8417 @required
8419 The declaration is a data definition, which may be missing
8420 declaration specifiers under the same rules and diagnostics as
8421 other data definitions outside functions, and the stray semicolon
8422 is diagnosed the same way as a stray semicolon outside a
8423 function. */
8425 static void
8426 c_parser_objc_methodprotolist (c_parser *parser)
8428 while (true)
8430 /* The list is terminated by @end. */
8431 switch (c_parser_peek_token (parser)->type)
8433 case CPP_SEMICOLON:
8434 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
8435 "ISO C does not allow extra %<;%> outside of a function");
8436 c_parser_consume_token (parser);
8437 break;
8438 case CPP_PLUS:
8439 case CPP_MINUS:
8440 c_parser_objc_methodproto (parser);
8441 break;
8442 case CPP_PRAGMA:
8443 c_parser_pragma (parser, pragma_external);
8444 break;
8445 case CPP_EOF:
8446 return;
8447 default:
8448 if (c_parser_next_token_is_keyword (parser, RID_AT_END))
8449 return;
8450 else if (c_parser_next_token_is_keyword (parser, RID_AT_PROPERTY))
8451 c_parser_objc_at_property_declaration (parser);
8452 else if (c_parser_next_token_is_keyword (parser, RID_AT_OPTIONAL))
8454 objc_set_method_opt (true);
8455 c_parser_consume_token (parser);
8457 else if (c_parser_next_token_is_keyword (parser, RID_AT_REQUIRED))
8459 objc_set_method_opt (false);
8460 c_parser_consume_token (parser);
8462 else
8463 c_parser_declaration_or_fndef (parser, false, false, true,
8464 false, true, NULL, vNULL);
8465 break;
8470 /* Parse an objc-methodproto.
8472 objc-methodproto:
8473 objc-method-type objc-method-decl ;
8476 static void
8477 c_parser_objc_methodproto (c_parser *parser)
8479 bool is_class_method = c_parser_objc_method_type (parser);
8480 tree decl, attributes = NULL_TREE;
8482 /* Remember protocol qualifiers in prototypes. */
8483 parser->objc_pq_context = true;
8484 decl = c_parser_objc_method_decl (parser, is_class_method, &attributes,
8485 NULL);
8486 /* Forget protocol qualifiers now. */
8487 parser->objc_pq_context = false;
8489 /* Do not allow the presence of attributes to hide an erroneous
8490 method implementation in the interface section. */
8491 if (!c_parser_next_token_is (parser, CPP_SEMICOLON))
8493 c_parser_error (parser, "expected %<;%>");
8494 return;
8497 if (decl != error_mark_node)
8498 objc_add_method_declaration (is_class_method, decl, attributes);
8500 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
8503 /* If we are at a position that method attributes may be present, check that
8504 there are not any parsed already (a syntax error) and then collect any
8505 specified at the current location. Finally, if new attributes were present,
8506 check that the next token is legal ( ';' for decls and '{' for defs). */
8508 static bool
8509 c_parser_objc_maybe_method_attributes (c_parser* parser, tree* attributes)
8511 bool bad = false;
8512 if (*attributes)
8514 c_parser_error (parser,
8515 "method attributes must be specified at the end only");
8516 *attributes = NULL_TREE;
8517 bad = true;
8520 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
8521 *attributes = c_parser_attributes (parser);
8523 /* If there were no attributes here, just report any earlier error. */
8524 if (*attributes == NULL_TREE || bad)
8525 return bad;
8527 /* If the attributes are followed by a ; or {, then just report any earlier
8528 error. */
8529 if (c_parser_next_token_is (parser, CPP_SEMICOLON)
8530 || c_parser_next_token_is (parser, CPP_OPEN_BRACE))
8531 return bad;
8533 /* We've got attributes, but not at the end. */
8534 c_parser_error (parser,
8535 "expected %<;%> or %<{%> after method attribute definition");
8536 return true;
8539 /* Parse an objc-method-decl.
8541 objc-method-decl:
8542 ( objc-type-name ) objc-selector
8543 objc-selector
8544 ( objc-type-name ) objc-keyword-selector objc-optparmlist
8545 objc-keyword-selector objc-optparmlist
8546 attributes
8548 objc-keyword-selector:
8549 objc-keyword-decl
8550 objc-keyword-selector objc-keyword-decl
8552 objc-keyword-decl:
8553 objc-selector : ( objc-type-name ) identifier
8554 objc-selector : identifier
8555 : ( objc-type-name ) identifier
8556 : identifier
8558 objc-optparmlist:
8559 objc-optparms objc-optellipsis
8561 objc-optparms:
8562 empty
8563 objc-opt-parms , parameter-declaration
8565 objc-optellipsis:
8566 empty
8567 , ...
8570 static tree
8571 c_parser_objc_method_decl (c_parser *parser, bool is_class_method,
8572 tree *attributes, tree *expr)
8574 tree type = NULL_TREE;
8575 tree sel;
8576 tree parms = NULL_TREE;
8577 bool ellipsis = false;
8578 bool attr_err = false;
8580 *attributes = NULL_TREE;
8581 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
8583 c_parser_consume_token (parser);
8584 type = c_parser_objc_type_name (parser);
8585 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
8587 sel = c_parser_objc_selector (parser);
8588 /* If there is no selector, or a colon follows, we have an
8589 objc-keyword-selector. If there is a selector, and a colon does
8590 not follow, that selector ends the objc-method-decl. */
8591 if (!sel || c_parser_next_token_is (parser, CPP_COLON))
8593 tree tsel = sel;
8594 tree list = NULL_TREE;
8595 while (true)
8597 tree atype = NULL_TREE, id, keyworddecl;
8598 tree param_attr = NULL_TREE;
8599 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
8600 break;
8601 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
8603 c_parser_consume_token (parser);
8604 atype = c_parser_objc_type_name (parser);
8605 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
8606 "expected %<)%>");
8608 /* New ObjC allows attributes on method parameters. */
8609 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
8610 param_attr = c_parser_attributes (parser);
8611 if (c_parser_next_token_is_not (parser, CPP_NAME))
8613 c_parser_error (parser, "expected identifier");
8614 return error_mark_node;
8616 id = c_parser_peek_token (parser)->value;
8617 c_parser_consume_token (parser);
8618 keyworddecl = objc_build_keyword_decl (tsel, atype, id, param_attr);
8619 list = chainon (list, keyworddecl);
8620 tsel = c_parser_objc_selector (parser);
8621 if (!tsel && c_parser_next_token_is_not (parser, CPP_COLON))
8622 break;
8625 attr_err |= c_parser_objc_maybe_method_attributes (parser, attributes) ;
8627 /* Parse the optional parameter list. Optional Objective-C
8628 method parameters follow the C syntax, and may include '...'
8629 to denote a variable number of arguments. */
8630 parms = make_node (TREE_LIST);
8631 while (c_parser_next_token_is (parser, CPP_COMMA))
8633 struct c_parm *parm;
8634 c_parser_consume_token (parser);
8635 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
8637 ellipsis = true;
8638 c_parser_consume_token (parser);
8639 attr_err |= c_parser_objc_maybe_method_attributes
8640 (parser, attributes) ;
8641 break;
8643 parm = c_parser_parameter_declaration (parser, NULL_TREE);
8644 if (parm == NULL)
8645 break;
8646 parms = chainon (parms,
8647 build_tree_list (NULL_TREE, grokparm (parm, expr)));
8649 sel = list;
8651 else
8652 attr_err |= c_parser_objc_maybe_method_attributes (parser, attributes) ;
8654 if (sel == NULL)
8656 c_parser_error (parser, "objective-c method declaration is expected");
8657 return error_mark_node;
8660 if (attr_err)
8661 return error_mark_node;
8663 return objc_build_method_signature (is_class_method, type, sel, parms, ellipsis);
8666 /* Parse an objc-type-name.
8668 objc-type-name:
8669 objc-type-qualifiers[opt] type-name
8670 objc-type-qualifiers[opt]
8672 objc-type-qualifiers:
8673 objc-type-qualifier
8674 objc-type-qualifiers objc-type-qualifier
8676 objc-type-qualifier: one of
8677 in out inout bycopy byref oneway
8680 static tree
8681 c_parser_objc_type_name (c_parser *parser)
8683 tree quals = NULL_TREE;
8684 struct c_type_name *type_name = NULL;
8685 tree type = NULL_TREE;
8686 while (true)
8688 c_token *token = c_parser_peek_token (parser);
8689 if (token->type == CPP_KEYWORD
8690 && (token->keyword == RID_IN
8691 || token->keyword == RID_OUT
8692 || token->keyword == RID_INOUT
8693 || token->keyword == RID_BYCOPY
8694 || token->keyword == RID_BYREF
8695 || token->keyword == RID_ONEWAY))
8697 quals = chainon (build_tree_list (NULL_TREE, token->value), quals);
8698 c_parser_consume_token (parser);
8700 else
8701 break;
8703 if (c_parser_next_tokens_start_typename (parser, cla_prefer_type))
8704 type_name = c_parser_type_name (parser);
8705 if (type_name)
8706 type = groktypename (type_name, NULL, NULL);
8708 /* If the type is unknown, and error has already been produced and
8709 we need to recover from the error. In that case, use NULL_TREE
8710 for the type, as if no type had been specified; this will use the
8711 default type ('id') which is good for error recovery. */
8712 if (type == error_mark_node)
8713 type = NULL_TREE;
8715 return build_tree_list (quals, type);
8718 /* Parse objc-protocol-refs.
8720 objc-protocol-refs:
8721 < identifier-list >
8724 static tree
8725 c_parser_objc_protocol_refs (c_parser *parser)
8727 tree list = NULL_TREE;
8728 gcc_assert (c_parser_next_token_is (parser, CPP_LESS));
8729 c_parser_consume_token (parser);
8730 /* Any identifiers, including those declared as type names, are OK
8731 here. */
8732 while (true)
8734 tree id;
8735 if (c_parser_next_token_is_not (parser, CPP_NAME))
8737 c_parser_error (parser, "expected identifier");
8738 break;
8740 id = c_parser_peek_token (parser)->value;
8741 list = chainon (list, build_tree_list (NULL_TREE, id));
8742 c_parser_consume_token (parser);
8743 if (c_parser_next_token_is (parser, CPP_COMMA))
8744 c_parser_consume_token (parser);
8745 else
8746 break;
8748 c_parser_require (parser, CPP_GREATER, "expected %<>%>");
8749 return list;
8752 /* Parse an objc-try-catch-finally-statement.
8754 objc-try-catch-finally-statement:
8755 @try compound-statement objc-catch-list[opt]
8756 @try compound-statement objc-catch-list[opt] @finally compound-statement
8758 objc-catch-list:
8759 @catch ( objc-catch-parameter-declaration ) compound-statement
8760 objc-catch-list @catch ( objc-catch-parameter-declaration ) compound-statement
8762 objc-catch-parameter-declaration:
8763 parameter-declaration
8764 '...'
8766 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
8768 PS: This function is identical to cp_parser_objc_try_catch_finally_statement
8769 for C++. Keep them in sync. */
8771 static void
8772 c_parser_objc_try_catch_finally_statement (c_parser *parser)
8774 location_t location;
8775 tree stmt;
8777 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_TRY));
8778 c_parser_consume_token (parser);
8779 location = c_parser_peek_token (parser)->location;
8780 objc_maybe_warn_exceptions (location);
8781 stmt = c_parser_compound_statement (parser);
8782 objc_begin_try_stmt (location, stmt);
8784 while (c_parser_next_token_is_keyword (parser, RID_AT_CATCH))
8786 struct c_parm *parm;
8787 tree parameter_declaration = error_mark_node;
8788 bool seen_open_paren = false;
8790 c_parser_consume_token (parser);
8791 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
8792 seen_open_paren = true;
8793 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
8795 /* We have "@catch (...)" (where the '...' are literally
8796 what is in the code). Skip the '...'.
8797 parameter_declaration is set to NULL_TREE, and
8798 objc_being_catch_clauses() knows that that means
8799 '...'. */
8800 c_parser_consume_token (parser);
8801 parameter_declaration = NULL_TREE;
8803 else
8805 /* We have "@catch (NSException *exception)" or something
8806 like that. Parse the parameter declaration. */
8807 parm = c_parser_parameter_declaration (parser, NULL_TREE);
8808 if (parm == NULL)
8809 parameter_declaration = error_mark_node;
8810 else
8811 parameter_declaration = grokparm (parm, NULL);
8813 if (seen_open_paren)
8814 c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
8815 else
8817 /* If there was no open parenthesis, we are recovering from
8818 an error, and we are trying to figure out what mistake
8819 the user has made. */
8821 /* If there is an immediate closing parenthesis, the user
8822 probably forgot the opening one (ie, they typed "@catch
8823 NSException *e)". Parse the closing parenthesis and keep
8824 going. */
8825 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
8826 c_parser_consume_token (parser);
8828 /* If these is no immediate closing parenthesis, the user
8829 probably doesn't know that parenthesis are required at
8830 all (ie, they typed "@catch NSException *e"). So, just
8831 forget about the closing parenthesis and keep going. */
8833 objc_begin_catch_clause (parameter_declaration);
8834 if (c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
8835 c_parser_compound_statement_nostart (parser);
8836 objc_finish_catch_clause ();
8838 if (c_parser_next_token_is_keyword (parser, RID_AT_FINALLY))
8840 c_parser_consume_token (parser);
8841 location = c_parser_peek_token (parser)->location;
8842 stmt = c_parser_compound_statement (parser);
8843 objc_build_finally_clause (location, stmt);
8845 objc_finish_try_stmt ();
8848 /* Parse an objc-synchronized-statement.
8850 objc-synchronized-statement:
8851 @synchronized ( expression ) compound-statement
8854 static void
8855 c_parser_objc_synchronized_statement (c_parser *parser)
8857 location_t loc;
8858 tree expr, stmt;
8859 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_SYNCHRONIZED));
8860 c_parser_consume_token (parser);
8861 loc = c_parser_peek_token (parser)->location;
8862 objc_maybe_warn_exceptions (loc);
8863 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
8865 struct c_expr ce = c_parser_expression (parser);
8866 ce = convert_lvalue_to_rvalue (loc, ce, false, false);
8867 expr = ce.value;
8868 expr = c_fully_fold (expr, false, NULL);
8869 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
8871 else
8872 expr = error_mark_node;
8873 stmt = c_parser_compound_statement (parser);
8874 objc_build_synchronized (loc, expr, stmt);
8877 /* Parse an objc-selector; return NULL_TREE without an error if the
8878 next token is not an objc-selector.
8880 objc-selector:
8881 identifier
8882 one of
8883 enum struct union if else while do for switch case default
8884 break continue return goto asm sizeof typeof __alignof
8885 unsigned long const short volatile signed restrict _Complex
8886 in out inout bycopy byref oneway int char float double void _Bool
8887 _Atomic
8889 ??? Why this selection of keywords but not, for example, storage
8890 class specifiers? */
8892 static tree
8893 c_parser_objc_selector (c_parser *parser)
8895 c_token *token = c_parser_peek_token (parser);
8896 tree value = token->value;
8897 if (token->type == CPP_NAME)
8899 c_parser_consume_token (parser);
8900 return value;
8902 if (token->type != CPP_KEYWORD)
8903 return NULL_TREE;
8904 switch (token->keyword)
8906 case RID_ENUM:
8907 case RID_STRUCT:
8908 case RID_UNION:
8909 case RID_IF:
8910 case RID_ELSE:
8911 case RID_WHILE:
8912 case RID_DO:
8913 case RID_FOR:
8914 case RID_SWITCH:
8915 case RID_CASE:
8916 case RID_DEFAULT:
8917 case RID_BREAK:
8918 case RID_CONTINUE:
8919 case RID_RETURN:
8920 case RID_GOTO:
8921 case RID_ASM:
8922 case RID_SIZEOF:
8923 case RID_TYPEOF:
8924 case RID_ALIGNOF:
8925 case RID_UNSIGNED:
8926 case RID_LONG:
8927 case RID_INT128:
8928 case RID_CONST:
8929 case RID_SHORT:
8930 case RID_VOLATILE:
8931 case RID_SIGNED:
8932 case RID_RESTRICT:
8933 case RID_COMPLEX:
8934 case RID_IN:
8935 case RID_OUT:
8936 case RID_INOUT:
8937 case RID_BYCOPY:
8938 case RID_BYREF:
8939 case RID_ONEWAY:
8940 case RID_INT:
8941 case RID_CHAR:
8942 case RID_FLOAT:
8943 case RID_DOUBLE:
8944 case RID_VOID:
8945 case RID_BOOL:
8946 case RID_ATOMIC:
8947 case RID_AUTO_TYPE:
8948 c_parser_consume_token (parser);
8949 return value;
8950 default:
8951 return NULL_TREE;
8955 /* Parse an objc-selector-arg.
8957 objc-selector-arg:
8958 objc-selector
8959 objc-keywordname-list
8961 objc-keywordname-list:
8962 objc-keywordname
8963 objc-keywordname-list objc-keywordname
8965 objc-keywordname:
8966 objc-selector :
8970 static tree
8971 c_parser_objc_selector_arg (c_parser *parser)
8973 tree sel = c_parser_objc_selector (parser);
8974 tree list = NULL_TREE;
8975 if (sel && c_parser_next_token_is_not (parser, CPP_COLON))
8976 return sel;
8977 while (true)
8979 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
8980 return list;
8981 list = chainon (list, build_tree_list (sel, NULL_TREE));
8982 sel = c_parser_objc_selector (parser);
8983 if (!sel && c_parser_next_token_is_not (parser, CPP_COLON))
8984 break;
8986 return list;
8989 /* Parse an objc-receiver.
8991 objc-receiver:
8992 expression
8993 class-name
8994 type-name
8997 static tree
8998 c_parser_objc_receiver (c_parser *parser)
9000 location_t loc = c_parser_peek_token (parser)->location;
9002 if (c_parser_peek_token (parser)->type == CPP_NAME
9003 && (c_parser_peek_token (parser)->id_kind == C_ID_TYPENAME
9004 || c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME))
9006 tree id = c_parser_peek_token (parser)->value;
9007 c_parser_consume_token (parser);
9008 return objc_get_class_reference (id);
9010 struct c_expr ce = c_parser_expression (parser);
9011 ce = convert_lvalue_to_rvalue (loc, ce, false, false);
9012 return c_fully_fold (ce.value, false, NULL);
9015 /* Parse objc-message-args.
9017 objc-message-args:
9018 objc-selector
9019 objc-keywordarg-list
9021 objc-keywordarg-list:
9022 objc-keywordarg
9023 objc-keywordarg-list objc-keywordarg
9025 objc-keywordarg:
9026 objc-selector : objc-keywordexpr
9027 : objc-keywordexpr
9030 static tree
9031 c_parser_objc_message_args (c_parser *parser)
9033 tree sel = c_parser_objc_selector (parser);
9034 tree list = NULL_TREE;
9035 if (sel && c_parser_next_token_is_not (parser, CPP_COLON))
9036 return sel;
9037 while (true)
9039 tree keywordexpr;
9040 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
9041 return error_mark_node;
9042 keywordexpr = c_parser_objc_keywordexpr (parser);
9043 list = chainon (list, build_tree_list (sel, keywordexpr));
9044 sel = c_parser_objc_selector (parser);
9045 if (!sel && c_parser_next_token_is_not (parser, CPP_COLON))
9046 break;
9048 return list;
9051 /* Parse an objc-keywordexpr.
9053 objc-keywordexpr:
9054 nonempty-expr-list
9057 static tree
9058 c_parser_objc_keywordexpr (c_parser *parser)
9060 tree ret;
9061 vec<tree, va_gc> *expr_list = c_parser_expr_list (parser, true, true,
9062 NULL, NULL, NULL, NULL);
9063 if (vec_safe_length (expr_list) == 1)
9065 /* Just return the expression, remove a level of
9066 indirection. */
9067 ret = (*expr_list)[0];
9069 else
9071 /* We have a comma expression, we will collapse later. */
9072 ret = build_tree_list_vec (expr_list);
9074 release_tree_vector (expr_list);
9075 return ret;
9078 /* A check, needed in several places, that ObjC interface, implementation or
9079 method definitions are not prefixed by incorrect items. */
9080 static bool
9081 c_parser_objc_diagnose_bad_element_prefix (c_parser *parser,
9082 struct c_declspecs *specs)
9084 if (!specs->declspecs_seen_p || specs->non_sc_seen_p
9085 || specs->typespec_kind != ctsk_none)
9087 c_parser_error (parser,
9088 "no type or storage class may be specified here,");
9089 c_parser_skip_to_end_of_block_or_statement (parser);
9090 return true;
9092 return false;
9095 /* Parse an Objective-C @property declaration. The syntax is:
9097 objc-property-declaration:
9098 '@property' objc-property-attributes[opt] struct-declaration ;
9100 objc-property-attributes:
9101 '(' objc-property-attribute-list ')'
9103 objc-property-attribute-list:
9104 objc-property-attribute
9105 objc-property-attribute-list, objc-property-attribute
9107 objc-property-attribute
9108 'getter' = identifier
9109 'setter' = identifier
9110 'readonly'
9111 'readwrite'
9112 'assign'
9113 'retain'
9114 'copy'
9115 'nonatomic'
9117 For example:
9118 @property NSString *name;
9119 @property (readonly) id object;
9120 @property (retain, nonatomic, getter=getTheName) id name;
9121 @property int a, b, c;
9123 PS: This function is identical to cp_parser_objc_at_propery_declaration
9124 for C++. Keep them in sync. */
9125 static void
9126 c_parser_objc_at_property_declaration (c_parser *parser)
9128 /* The following variables hold the attributes of the properties as
9129 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
9130 seen. When we see an attribute, we set them to 'true' (if they
9131 are boolean properties) or to the identifier (if they have an
9132 argument, ie, for getter and setter). Note that here we only
9133 parse the list of attributes, check the syntax and accumulate the
9134 attributes that we find. objc_add_property_declaration() will
9135 then process the information. */
9136 bool property_assign = false;
9137 bool property_copy = false;
9138 tree property_getter_ident = NULL_TREE;
9139 bool property_nonatomic = false;
9140 bool property_readonly = false;
9141 bool property_readwrite = false;
9142 bool property_retain = false;
9143 tree property_setter_ident = NULL_TREE;
9145 /* 'properties' is the list of properties that we read. Usually a
9146 single one, but maybe more (eg, in "@property int a, b, c;" there
9147 are three). */
9148 tree properties;
9149 location_t loc;
9151 loc = c_parser_peek_token (parser)->location;
9152 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_PROPERTY));
9154 c_parser_consume_token (parser); /* Eat '@property'. */
9156 /* Parse the optional attribute list... */
9157 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
9159 /* Eat the '(' */
9160 c_parser_consume_token (parser);
9162 /* Property attribute keywords are valid now. */
9163 parser->objc_property_attr_context = true;
9165 while (true)
9167 bool syntax_error = false;
9168 c_token *token = c_parser_peek_token (parser);
9169 enum rid keyword;
9171 if (token->type != CPP_KEYWORD)
9173 if (token->type == CPP_CLOSE_PAREN)
9174 c_parser_error (parser, "expected identifier");
9175 else
9177 c_parser_consume_token (parser);
9178 c_parser_error (parser, "unknown property attribute");
9180 break;
9182 keyword = token->keyword;
9183 c_parser_consume_token (parser);
9184 switch (keyword)
9186 case RID_ASSIGN: property_assign = true; break;
9187 case RID_COPY: property_copy = true; break;
9188 case RID_NONATOMIC: property_nonatomic = true; break;
9189 case RID_READONLY: property_readonly = true; break;
9190 case RID_READWRITE: property_readwrite = true; break;
9191 case RID_RETAIN: property_retain = true; break;
9193 case RID_GETTER:
9194 case RID_SETTER:
9195 if (c_parser_next_token_is_not (parser, CPP_EQ))
9197 if (keyword == RID_GETTER)
9198 c_parser_error (parser,
9199 "missing %<=%> (after %<getter%> attribute)");
9200 else
9201 c_parser_error (parser,
9202 "missing %<=%> (after %<setter%> attribute)");
9203 syntax_error = true;
9204 break;
9206 c_parser_consume_token (parser); /* eat the = */
9207 if (c_parser_next_token_is_not (parser, CPP_NAME))
9209 c_parser_error (parser, "expected identifier");
9210 syntax_error = true;
9211 break;
9213 if (keyword == RID_SETTER)
9215 if (property_setter_ident != NULL_TREE)
9216 c_parser_error (parser, "the %<setter%> attribute may only be specified once");
9217 else
9218 property_setter_ident = c_parser_peek_token (parser)->value;
9219 c_parser_consume_token (parser);
9220 if (c_parser_next_token_is_not (parser, CPP_COLON))
9221 c_parser_error (parser, "setter name must terminate with %<:%>");
9222 else
9223 c_parser_consume_token (parser);
9225 else
9227 if (property_getter_ident != NULL_TREE)
9228 c_parser_error (parser, "the %<getter%> attribute may only be specified once");
9229 else
9230 property_getter_ident = c_parser_peek_token (parser)->value;
9231 c_parser_consume_token (parser);
9233 break;
9234 default:
9235 c_parser_error (parser, "unknown property attribute");
9236 syntax_error = true;
9237 break;
9240 if (syntax_error)
9241 break;
9243 if (c_parser_next_token_is (parser, CPP_COMMA))
9244 c_parser_consume_token (parser);
9245 else
9246 break;
9248 parser->objc_property_attr_context = false;
9249 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
9251 /* ... and the property declaration(s). */
9252 properties = c_parser_struct_declaration (parser);
9254 if (properties == error_mark_node)
9256 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
9257 parser->error = false;
9258 return;
9261 if (properties == NULL_TREE)
9262 c_parser_error (parser, "expected identifier");
9263 else
9265 /* Comma-separated properties are chained together in
9266 reverse order; add them one by one. */
9267 properties = nreverse (properties);
9269 for (; properties; properties = TREE_CHAIN (properties))
9270 objc_add_property_declaration (loc, copy_node (properties),
9271 property_readonly, property_readwrite,
9272 property_assign, property_retain,
9273 property_copy, property_nonatomic,
9274 property_getter_ident, property_setter_ident);
9277 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
9278 parser->error = false;
9281 /* Parse an Objective-C @synthesize declaration. The syntax is:
9283 objc-synthesize-declaration:
9284 @synthesize objc-synthesize-identifier-list ;
9286 objc-synthesize-identifier-list:
9287 objc-synthesize-identifier
9288 objc-synthesize-identifier-list, objc-synthesize-identifier
9290 objc-synthesize-identifier
9291 identifier
9292 identifier = identifier
9294 For example:
9295 @synthesize MyProperty;
9296 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
9298 PS: This function is identical to cp_parser_objc_at_synthesize_declaration
9299 for C++. Keep them in sync.
9301 static void
9302 c_parser_objc_at_synthesize_declaration (c_parser *parser)
9304 tree list = NULL_TREE;
9305 location_t loc;
9306 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_SYNTHESIZE));
9307 loc = c_parser_peek_token (parser)->location;
9309 c_parser_consume_token (parser);
9310 while (true)
9312 tree property, ivar;
9313 if (c_parser_next_token_is_not (parser, CPP_NAME))
9315 c_parser_error (parser, "expected identifier");
9316 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
9317 /* Once we find the semicolon, we can resume normal parsing.
9318 We have to reset parser->error manually because
9319 c_parser_skip_until_found() won't reset it for us if the
9320 next token is precisely a semicolon. */
9321 parser->error = false;
9322 return;
9324 property = c_parser_peek_token (parser)->value;
9325 c_parser_consume_token (parser);
9326 if (c_parser_next_token_is (parser, CPP_EQ))
9328 c_parser_consume_token (parser);
9329 if (c_parser_next_token_is_not (parser, CPP_NAME))
9331 c_parser_error (parser, "expected identifier");
9332 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
9333 parser->error = false;
9334 return;
9336 ivar = c_parser_peek_token (parser)->value;
9337 c_parser_consume_token (parser);
9339 else
9340 ivar = NULL_TREE;
9341 list = chainon (list, build_tree_list (ivar, property));
9342 if (c_parser_next_token_is (parser, CPP_COMMA))
9343 c_parser_consume_token (parser);
9344 else
9345 break;
9347 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
9348 objc_add_synthesize_declaration (loc, list);
9351 /* Parse an Objective-C @dynamic declaration. The syntax is:
9353 objc-dynamic-declaration:
9354 @dynamic identifier-list ;
9356 For example:
9357 @dynamic MyProperty;
9358 @dynamic MyProperty, AnotherProperty;
9360 PS: This function is identical to cp_parser_objc_at_dynamic_declaration
9361 for C++. Keep them in sync.
9363 static void
9364 c_parser_objc_at_dynamic_declaration (c_parser *parser)
9366 tree list = NULL_TREE;
9367 location_t loc;
9368 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_DYNAMIC));
9369 loc = c_parser_peek_token (parser)->location;
9371 c_parser_consume_token (parser);
9372 while (true)
9374 tree property;
9375 if (c_parser_next_token_is_not (parser, CPP_NAME))
9377 c_parser_error (parser, "expected identifier");
9378 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
9379 parser->error = false;
9380 return;
9382 property = c_parser_peek_token (parser)->value;
9383 list = chainon (list, build_tree_list (NULL_TREE, property));
9384 c_parser_consume_token (parser);
9385 if (c_parser_next_token_is (parser, CPP_COMMA))
9386 c_parser_consume_token (parser);
9387 else
9388 break;
9390 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
9391 objc_add_dynamic_declaration (loc, list);
9395 /* Handle pragmas. Some OpenMP pragmas are associated with, and therefore
9396 should be considered, statements. ALLOW_STMT is true if we're within
9397 the context of a function and such pragmas are to be allowed. Returns
9398 true if we actually parsed such a pragma. */
9400 static bool
9401 c_parser_pragma (c_parser *parser, enum pragma_context context)
9403 unsigned int id;
9405 id = c_parser_peek_token (parser)->pragma_kind;
9406 gcc_assert (id != PRAGMA_NONE);
9408 switch (id)
9410 case PRAGMA_OMP_BARRIER:
9411 if (context != pragma_compound)
9413 if (context == pragma_stmt)
9414 c_parser_error (parser, "%<#pragma omp barrier%> may only be "
9415 "used in compound statements");
9416 goto bad_stmt;
9418 c_parser_omp_barrier (parser);
9419 return false;
9421 case PRAGMA_OMP_FLUSH:
9422 if (context != pragma_compound)
9424 if (context == pragma_stmt)
9425 c_parser_error (parser, "%<#pragma omp flush%> may only be "
9426 "used in compound statements");
9427 goto bad_stmt;
9429 c_parser_omp_flush (parser);
9430 return false;
9432 case PRAGMA_OMP_TASKWAIT:
9433 if (context != pragma_compound)
9435 if (context == pragma_stmt)
9436 c_parser_error (parser, "%<#pragma omp taskwait%> may only be "
9437 "used in compound statements");
9438 goto bad_stmt;
9440 c_parser_omp_taskwait (parser);
9441 return false;
9443 case PRAGMA_OMP_TASKYIELD:
9444 if (context != pragma_compound)
9446 if (context == pragma_stmt)
9447 c_parser_error (parser, "%<#pragma omp taskyield%> may only be "
9448 "used in compound statements");
9449 goto bad_stmt;
9451 c_parser_omp_taskyield (parser);
9452 return false;
9454 case PRAGMA_OMP_CANCEL:
9455 if (context != pragma_compound)
9457 if (context == pragma_stmt)
9458 c_parser_error (parser, "%<#pragma omp cancel%> may only be "
9459 "used in compound statements");
9460 goto bad_stmt;
9462 c_parser_omp_cancel (parser);
9463 return false;
9465 case PRAGMA_OMP_CANCELLATION_POINT:
9466 if (context != pragma_compound)
9468 if (context == pragma_stmt)
9469 c_parser_error (parser, "%<#pragma omp cancellation point%> may "
9470 "only be used in compound statements");
9471 goto bad_stmt;
9473 c_parser_omp_cancellation_point (parser);
9474 return false;
9476 case PRAGMA_OMP_THREADPRIVATE:
9477 c_parser_omp_threadprivate (parser);
9478 return false;
9480 case PRAGMA_OMP_TARGET:
9481 return c_parser_omp_target (parser, context);
9483 case PRAGMA_OMP_END_DECLARE_TARGET:
9484 c_parser_omp_end_declare_target (parser);
9485 return false;
9487 case PRAGMA_OMP_SECTION:
9488 error_at (c_parser_peek_token (parser)->location,
9489 "%<#pragma omp section%> may only be used in "
9490 "%<#pragma omp sections%> construct");
9491 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
9492 return false;
9494 case PRAGMA_OMP_DECLARE_REDUCTION:
9495 c_parser_omp_declare (parser, context);
9496 return false;
9497 case PRAGMA_IVDEP:
9498 c_parser_consume_pragma (parser);
9499 c_parser_skip_to_pragma_eol (parser);
9500 if (!c_parser_next_token_is_keyword (parser, RID_FOR)
9501 && !c_parser_next_token_is_keyword (parser, RID_WHILE)
9502 && !c_parser_next_token_is_keyword (parser, RID_DO))
9504 c_parser_error (parser, "for, while or do statement expected");
9505 return false;
9507 if (c_parser_next_token_is_keyword (parser, RID_FOR))
9508 c_parser_for_statement (parser, true);
9509 else if (c_parser_next_token_is_keyword (parser, RID_WHILE))
9510 c_parser_while_statement (parser, true);
9511 else
9512 c_parser_do_statement (parser, true);
9513 return false;
9515 case PRAGMA_GCC_PCH_PREPROCESS:
9516 c_parser_error (parser, "%<#pragma GCC pch_preprocess%> must be first");
9517 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
9518 return false;
9520 case PRAGMA_CILK_SIMD:
9521 if (!c_parser_cilk_verify_simd (parser, context))
9522 return false;
9523 c_parser_consume_pragma (parser);
9524 c_parser_cilk_simd (parser);
9525 return false;
9527 default:
9528 if (id < PRAGMA_FIRST_EXTERNAL)
9530 if (context != pragma_stmt && context != pragma_compound)
9532 bad_stmt:
9533 c_parser_error (parser, "expected declaration specifiers");
9534 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
9535 return false;
9537 c_parser_omp_construct (parser);
9538 return true;
9540 break;
9543 c_parser_consume_pragma (parser);
9544 c_invoke_pragma_handler (id);
9546 /* Skip to EOL, but suppress any error message. Those will have been
9547 generated by the handler routine through calling error, as opposed
9548 to calling c_parser_error. */
9549 parser->error = true;
9550 c_parser_skip_to_pragma_eol (parser);
9552 return false;
9555 /* The interface the pragma parsers have to the lexer. */
9557 enum cpp_ttype
9558 pragma_lex (tree *value)
9560 c_token *tok = c_parser_peek_token (the_parser);
9561 enum cpp_ttype ret = tok->type;
9563 *value = tok->value;
9564 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
9565 ret = CPP_EOF;
9566 else
9568 if (ret == CPP_KEYWORD)
9569 ret = CPP_NAME;
9570 c_parser_consume_token (the_parser);
9573 return ret;
9576 static void
9577 c_parser_pragma_pch_preprocess (c_parser *parser)
9579 tree name = NULL;
9581 c_parser_consume_pragma (parser);
9582 if (c_parser_next_token_is (parser, CPP_STRING))
9584 name = c_parser_peek_token (parser)->value;
9585 c_parser_consume_token (parser);
9587 else
9588 c_parser_error (parser, "expected string literal");
9589 c_parser_skip_to_pragma_eol (parser);
9591 if (name)
9592 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
9595 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 parsing routines. */
9597 /* Returns name of the next clause.
9598 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
9599 the token is not consumed. Otherwise appropriate pragma_omp_clause is
9600 returned and the token is consumed. */
9602 static pragma_omp_clause
9603 c_parser_omp_clause_name (c_parser *parser)
9605 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
9607 if (c_parser_next_token_is_keyword (parser, RID_IF))
9608 result = PRAGMA_OMP_CLAUSE_IF;
9609 else if (c_parser_next_token_is_keyword (parser, RID_DEFAULT))
9610 result = PRAGMA_OMP_CLAUSE_DEFAULT;
9611 else if (c_parser_next_token_is_keyword (parser, RID_FOR))
9612 result = PRAGMA_OMP_CLAUSE_FOR;
9613 else if (c_parser_next_token_is (parser, CPP_NAME))
9615 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
9617 switch (p[0])
9619 case 'a':
9620 if (!strcmp ("aligned", p))
9621 result = PRAGMA_OMP_CLAUSE_ALIGNED;
9622 break;
9623 case 'c':
9624 if (!strcmp ("collapse", p))
9625 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
9626 else if (!strcmp ("copyin", p))
9627 result = PRAGMA_OMP_CLAUSE_COPYIN;
9628 else if (!strcmp ("copyprivate", p))
9629 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
9630 break;
9631 case 'd':
9632 if (!strcmp ("depend", p))
9633 result = PRAGMA_OMP_CLAUSE_DEPEND;
9634 else if (!strcmp ("device", p))
9635 result = PRAGMA_OMP_CLAUSE_DEVICE;
9636 else if (!strcmp ("dist_schedule", p))
9637 result = PRAGMA_OMP_CLAUSE_DIST_SCHEDULE;
9638 break;
9639 case 'f':
9640 if (!strcmp ("final", p))
9641 result = PRAGMA_OMP_CLAUSE_FINAL;
9642 else if (!strcmp ("firstprivate", p))
9643 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
9644 else if (!strcmp ("from", p))
9645 result = PRAGMA_OMP_CLAUSE_FROM;
9646 break;
9647 case 'i':
9648 if (!strcmp ("inbranch", p))
9649 result = PRAGMA_OMP_CLAUSE_INBRANCH;
9650 break;
9651 case 'l':
9652 if (!strcmp ("lastprivate", p))
9653 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
9654 else if (!strcmp ("linear", p))
9655 result = PRAGMA_OMP_CLAUSE_LINEAR;
9656 break;
9657 case 'm':
9658 if (!strcmp ("map", p))
9659 result = PRAGMA_OMP_CLAUSE_MAP;
9660 else if (!strcmp ("mergeable", p))
9661 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
9662 else if (flag_cilkplus && !strcmp ("mask", p))
9663 result = PRAGMA_CILK_CLAUSE_MASK;
9664 break;
9665 case 'n':
9666 if (!strcmp ("notinbranch", p))
9667 result = PRAGMA_OMP_CLAUSE_NOTINBRANCH;
9668 else if (!strcmp ("nowait", p))
9669 result = PRAGMA_OMP_CLAUSE_NOWAIT;
9670 else if (!strcmp ("num_teams", p))
9671 result = PRAGMA_OMP_CLAUSE_NUM_TEAMS;
9672 else if (!strcmp ("num_threads", p))
9673 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
9674 else if (flag_cilkplus && !strcmp ("nomask", p))
9675 result = PRAGMA_CILK_CLAUSE_NOMASK;
9676 break;
9677 case 'o':
9678 if (!strcmp ("ordered", p))
9679 result = PRAGMA_OMP_CLAUSE_ORDERED;
9680 break;
9681 case 'p':
9682 if (!strcmp ("parallel", p))
9683 result = PRAGMA_OMP_CLAUSE_PARALLEL;
9684 else if (!strcmp ("private", p))
9685 result = PRAGMA_OMP_CLAUSE_PRIVATE;
9686 else if (!strcmp ("proc_bind", p))
9687 result = PRAGMA_OMP_CLAUSE_PROC_BIND;
9688 break;
9689 case 'r':
9690 if (!strcmp ("reduction", p))
9691 result = PRAGMA_OMP_CLAUSE_REDUCTION;
9692 break;
9693 case 's':
9694 if (!strcmp ("safelen", p))
9695 result = PRAGMA_OMP_CLAUSE_SAFELEN;
9696 else if (!strcmp ("schedule", p))
9697 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
9698 else if (!strcmp ("sections", p))
9699 result = PRAGMA_OMP_CLAUSE_SECTIONS;
9700 else if (!strcmp ("shared", p))
9701 result = PRAGMA_OMP_CLAUSE_SHARED;
9702 else if (!strcmp ("simdlen", p))
9703 result = PRAGMA_OMP_CLAUSE_SIMDLEN;
9704 break;
9705 case 't':
9706 if (!strcmp ("taskgroup", p))
9707 result = PRAGMA_OMP_CLAUSE_TASKGROUP;
9708 else if (!strcmp ("thread_limit", p))
9709 result = PRAGMA_OMP_CLAUSE_THREAD_LIMIT;
9710 else if (!strcmp ("to", p))
9711 result = PRAGMA_OMP_CLAUSE_TO;
9712 break;
9713 case 'u':
9714 if (!strcmp ("uniform", p))
9715 result = PRAGMA_OMP_CLAUSE_UNIFORM;
9716 else if (!strcmp ("untied", p))
9717 result = PRAGMA_OMP_CLAUSE_UNTIED;
9718 break;
9719 case 'v':
9720 if (flag_cilkplus && !strcmp ("vectorlength", p))
9721 result = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
9722 break;
9726 if (result != PRAGMA_OMP_CLAUSE_NONE)
9727 c_parser_consume_token (parser);
9729 return result;
9732 /* Validate that a clause of the given type does not already exist. */
9734 static void
9735 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
9736 const char *name)
9738 tree c;
9740 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
9741 if (OMP_CLAUSE_CODE (c) == code)
9743 location_t loc = OMP_CLAUSE_LOCATION (c);
9744 error_at (loc, "too many %qs clauses", name);
9745 break;
9749 /* OpenMP 2.5:
9750 variable-list:
9751 identifier
9752 variable-list , identifier
9754 If KIND is nonzero, create the appropriate node and install the
9755 decl in OMP_CLAUSE_DECL and add the node to the head of the list.
9756 If KIND is nonzero, CLAUSE_LOC is the location of the clause.
9758 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
9759 return the list created. */
9761 static tree
9762 c_parser_omp_variable_list (c_parser *parser,
9763 location_t clause_loc,
9764 enum omp_clause_code kind, tree list)
9766 if (c_parser_next_token_is_not (parser, CPP_NAME)
9767 || c_parser_peek_token (parser)->id_kind != C_ID_ID)
9768 c_parser_error (parser, "expected identifier");
9770 while (c_parser_next_token_is (parser, CPP_NAME)
9771 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
9773 tree t = lookup_name (c_parser_peek_token (parser)->value);
9775 if (t == NULL_TREE)
9777 undeclared_variable (c_parser_peek_token (parser)->location,
9778 c_parser_peek_token (parser)->value);
9779 t = error_mark_node;
9782 c_parser_consume_token (parser);
9784 if (t == error_mark_node)
9786 else if (kind != 0)
9788 switch (kind)
9790 case OMP_CLAUSE_MAP:
9791 case OMP_CLAUSE_FROM:
9792 case OMP_CLAUSE_TO:
9793 case OMP_CLAUSE_DEPEND:
9794 while (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
9796 tree low_bound = NULL_TREE, length = NULL_TREE;
9798 c_parser_consume_token (parser);
9799 if (!c_parser_next_token_is (parser, CPP_COLON))
9800 low_bound = c_parser_expression (parser).value;
9801 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
9802 length = integer_one_node;
9803 else
9805 /* Look for `:'. */
9806 if (!c_parser_require (parser, CPP_COLON,
9807 "expected %<:%>"))
9809 t = error_mark_node;
9810 break;
9812 if (!c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
9813 length = c_parser_expression (parser).value;
9815 /* Look for the closing `]'. */
9816 if (!c_parser_require (parser, CPP_CLOSE_SQUARE,
9817 "expected %<]%>"))
9819 t = error_mark_node;
9820 break;
9822 t = tree_cons (low_bound, length, t);
9824 break;
9825 default:
9826 break;
9829 if (t != error_mark_node)
9831 tree u = build_omp_clause (clause_loc, kind);
9832 OMP_CLAUSE_DECL (u) = t;
9833 OMP_CLAUSE_CHAIN (u) = list;
9834 list = u;
9837 else
9838 list = tree_cons (t, NULL_TREE, list);
9840 if (c_parser_next_token_is_not (parser, CPP_COMMA))
9841 break;
9843 c_parser_consume_token (parser);
9846 return list;
9849 /* Similarly, but expect leading and trailing parenthesis. This is a very
9850 common case for omp clauses. */
9852 static tree
9853 c_parser_omp_var_list_parens (c_parser *parser, enum omp_clause_code kind,
9854 tree list)
9856 /* The clauses location. */
9857 location_t loc = c_parser_peek_token (parser)->location;
9859 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
9861 list = c_parser_omp_variable_list (parser, loc, kind, list);
9862 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
9864 return list;
9867 /* OpenMP 3.0:
9868 collapse ( constant-expression ) */
9870 static tree
9871 c_parser_omp_clause_collapse (c_parser *parser, tree list)
9873 tree c, num = error_mark_node;
9874 HOST_WIDE_INT n;
9875 location_t loc;
9877 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse");
9879 loc = c_parser_peek_token (parser)->location;
9880 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
9882 num = c_parser_expr_no_commas (parser, NULL).value;
9883 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
9885 if (num == error_mark_node)
9886 return list;
9887 mark_exp_read (num);
9888 num = c_fully_fold (num, false, NULL);
9889 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
9890 || !tree_fits_shwi_p (num)
9891 || (n = tree_to_shwi (num)) <= 0
9892 || (int) n != n)
9894 error_at (loc,
9895 "collapse argument needs positive constant integer expression");
9896 return list;
9898 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
9899 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
9900 OMP_CLAUSE_CHAIN (c) = list;
9901 return c;
9904 /* OpenMP 2.5:
9905 copyin ( variable-list ) */
9907 static tree
9908 c_parser_omp_clause_copyin (c_parser *parser, tree list)
9910 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_COPYIN, list);
9913 /* OpenMP 2.5:
9914 copyprivate ( variable-list ) */
9916 static tree
9917 c_parser_omp_clause_copyprivate (c_parser *parser, tree list)
9919 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_COPYPRIVATE, list);
9922 /* OpenMP 2.5:
9923 default ( shared | none ) */
9925 static tree
9926 c_parser_omp_clause_default (c_parser *parser, tree list)
9928 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
9929 location_t loc = c_parser_peek_token (parser)->location;
9930 tree c;
9932 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
9933 return list;
9934 if (c_parser_next_token_is (parser, CPP_NAME))
9936 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
9938 switch (p[0])
9940 case 'n':
9941 if (strcmp ("none", p) != 0)
9942 goto invalid_kind;
9943 kind = OMP_CLAUSE_DEFAULT_NONE;
9944 break;
9946 case 's':
9947 if (strcmp ("shared", p) != 0)
9948 goto invalid_kind;
9949 kind = OMP_CLAUSE_DEFAULT_SHARED;
9950 break;
9952 default:
9953 goto invalid_kind;
9956 c_parser_consume_token (parser);
9958 else
9960 invalid_kind:
9961 c_parser_error (parser, "expected %<none%> or %<shared%>");
9963 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
9965 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
9966 return list;
9968 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default");
9969 c = build_omp_clause (loc, OMP_CLAUSE_DEFAULT);
9970 OMP_CLAUSE_CHAIN (c) = list;
9971 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
9973 return c;
9976 /* OpenMP 2.5:
9977 firstprivate ( variable-list ) */
9979 static tree
9980 c_parser_omp_clause_firstprivate (c_parser *parser, tree list)
9982 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_FIRSTPRIVATE, list);
9985 /* OpenMP 3.1:
9986 final ( expression ) */
9988 static tree
9989 c_parser_omp_clause_final (c_parser *parser, tree list)
9991 location_t loc = c_parser_peek_token (parser)->location;
9992 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
9994 tree t = c_parser_paren_condition (parser);
9995 tree c;
9997 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final");
9999 c = build_omp_clause (loc, OMP_CLAUSE_FINAL);
10000 OMP_CLAUSE_FINAL_EXPR (c) = t;
10001 OMP_CLAUSE_CHAIN (c) = list;
10002 list = c;
10004 else
10005 c_parser_error (parser, "expected %<(%>");
10007 return list;
10010 /* OpenMP 2.5:
10011 if ( expression ) */
10013 static tree
10014 c_parser_omp_clause_if (c_parser *parser, tree list)
10016 location_t loc = c_parser_peek_token (parser)->location;
10017 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
10019 tree t = c_parser_paren_condition (parser);
10020 tree c;
10022 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if");
10024 c = build_omp_clause (loc, OMP_CLAUSE_IF);
10025 OMP_CLAUSE_IF_EXPR (c) = t;
10026 OMP_CLAUSE_CHAIN (c) = list;
10027 list = c;
10029 else
10030 c_parser_error (parser, "expected %<(%>");
10032 return list;
10035 /* OpenMP 2.5:
10036 lastprivate ( variable-list ) */
10038 static tree
10039 c_parser_omp_clause_lastprivate (c_parser *parser, tree list)
10041 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_LASTPRIVATE, list);
10044 /* OpenMP 3.1:
10045 mergeable */
10047 static tree
10048 c_parser_omp_clause_mergeable (c_parser *parser ATTRIBUTE_UNUSED, tree list)
10050 tree c;
10052 /* FIXME: Should we allow duplicates? */
10053 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable");
10055 c = build_omp_clause (c_parser_peek_token (parser)->location,
10056 OMP_CLAUSE_MERGEABLE);
10057 OMP_CLAUSE_CHAIN (c) = list;
10059 return c;
10062 /* OpenMP 2.5:
10063 nowait */
10065 static tree
10066 c_parser_omp_clause_nowait (c_parser *parser ATTRIBUTE_UNUSED, tree list)
10068 tree c;
10069 location_t loc = c_parser_peek_token (parser)->location;
10071 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait");
10073 c = build_omp_clause (loc, OMP_CLAUSE_NOWAIT);
10074 OMP_CLAUSE_CHAIN (c) = list;
10075 return c;
10078 /* OpenMP 2.5:
10079 num_threads ( expression ) */
10081 static tree
10082 c_parser_omp_clause_num_threads (c_parser *parser, tree list)
10084 location_t num_threads_loc = c_parser_peek_token (parser)->location;
10085 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10087 location_t expr_loc = c_parser_peek_token (parser)->location;
10088 tree c, t = c_parser_expression (parser).value;
10089 mark_exp_read (t);
10090 t = c_fully_fold (t, false, NULL);
10092 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10094 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
10096 c_parser_error (parser, "expected integer expression");
10097 return list;
10100 /* Attempt to statically determine when the number isn't positive. */
10101 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
10102 build_int_cst (TREE_TYPE (t), 0));
10103 if (CAN_HAVE_LOCATION_P (c))
10104 SET_EXPR_LOCATION (c, expr_loc);
10105 if (c == boolean_true_node)
10107 warning_at (expr_loc, 0,
10108 "%<num_threads%> value must be positive");
10109 t = integer_one_node;
10112 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS, "num_threads");
10114 c = build_omp_clause (num_threads_loc, OMP_CLAUSE_NUM_THREADS);
10115 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
10116 OMP_CLAUSE_CHAIN (c) = list;
10117 list = c;
10120 return list;
10123 /* OpenMP 2.5:
10124 ordered */
10126 static tree
10127 c_parser_omp_clause_ordered (c_parser *parser, tree list)
10129 tree c;
10131 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED, "ordered");
10133 c = build_omp_clause (c_parser_peek_token (parser)->location,
10134 OMP_CLAUSE_ORDERED);
10135 OMP_CLAUSE_CHAIN (c) = list;
10137 return c;
10140 /* OpenMP 2.5:
10141 private ( variable-list ) */
10143 static tree
10144 c_parser_omp_clause_private (c_parser *parser, tree list)
10146 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_PRIVATE, list);
10149 /* OpenMP 2.5:
10150 reduction ( reduction-operator : variable-list )
10152 reduction-operator:
10153 One of: + * - & ^ | && ||
10155 OpenMP 3.1:
10157 reduction-operator:
10158 One of: + * - & ^ | && || max min
10160 OpenMP 4.0:
10162 reduction-operator:
10163 One of: + * - & ^ | && ||
10164 identifier */
10166 static tree
10167 c_parser_omp_clause_reduction (c_parser *parser, tree list)
10169 location_t clause_loc = c_parser_peek_token (parser)->location;
10170 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10172 enum tree_code code = ERROR_MARK;
10173 tree reduc_id = NULL_TREE;
10175 switch (c_parser_peek_token (parser)->type)
10177 case CPP_PLUS:
10178 code = PLUS_EXPR;
10179 break;
10180 case CPP_MULT:
10181 code = MULT_EXPR;
10182 break;
10183 case CPP_MINUS:
10184 code = MINUS_EXPR;
10185 break;
10186 case CPP_AND:
10187 code = BIT_AND_EXPR;
10188 break;
10189 case CPP_XOR:
10190 code = BIT_XOR_EXPR;
10191 break;
10192 case CPP_OR:
10193 code = BIT_IOR_EXPR;
10194 break;
10195 case CPP_AND_AND:
10196 code = TRUTH_ANDIF_EXPR;
10197 break;
10198 case CPP_OR_OR:
10199 code = TRUTH_ORIF_EXPR;
10200 break;
10201 case CPP_NAME:
10203 const char *p
10204 = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
10205 if (strcmp (p, "min") == 0)
10207 code = MIN_EXPR;
10208 break;
10210 if (strcmp (p, "max") == 0)
10212 code = MAX_EXPR;
10213 break;
10215 reduc_id = c_parser_peek_token (parser)->value;
10216 break;
10218 default:
10219 c_parser_error (parser,
10220 "expected %<+%>, %<*%>, %<-%>, %<&%>, "
10221 "%<^%>, %<|%>, %<&&%>, %<||%>, %<min%> or %<max%>");
10222 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 0);
10223 return list;
10225 c_parser_consume_token (parser);
10226 reduc_id = c_omp_reduction_id (code, reduc_id);
10227 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
10229 tree nl, c;
10231 nl = c_parser_omp_variable_list (parser, clause_loc,
10232 OMP_CLAUSE_REDUCTION, list);
10233 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
10235 tree type = TREE_TYPE (OMP_CLAUSE_DECL (c));
10236 OMP_CLAUSE_REDUCTION_CODE (c) = code;
10237 if (code == ERROR_MARK
10238 || !(INTEGRAL_TYPE_P (type)
10239 || TREE_CODE (type) == REAL_TYPE
10240 || TREE_CODE (type) == COMPLEX_TYPE))
10241 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c)
10242 = c_omp_reduction_lookup (reduc_id,
10243 TYPE_MAIN_VARIANT (type));
10246 list = nl;
10248 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10250 return list;
10253 /* OpenMP 2.5:
10254 schedule ( schedule-kind )
10255 schedule ( schedule-kind , expression )
10257 schedule-kind:
10258 static | dynamic | guided | runtime | auto
10261 static tree
10262 c_parser_omp_clause_schedule (c_parser *parser, tree list)
10264 tree c, t;
10265 location_t loc = c_parser_peek_token (parser)->location;
10267 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10268 return list;
10270 c = build_omp_clause (loc, OMP_CLAUSE_SCHEDULE);
10272 if (c_parser_next_token_is (parser, CPP_NAME))
10274 tree kind = c_parser_peek_token (parser)->value;
10275 const char *p = IDENTIFIER_POINTER (kind);
10277 switch (p[0])
10279 case 'd':
10280 if (strcmp ("dynamic", p) != 0)
10281 goto invalid_kind;
10282 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
10283 break;
10285 case 'g':
10286 if (strcmp ("guided", p) != 0)
10287 goto invalid_kind;
10288 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
10289 break;
10291 case 'r':
10292 if (strcmp ("runtime", p) != 0)
10293 goto invalid_kind;
10294 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
10295 break;
10297 default:
10298 goto invalid_kind;
10301 else if (c_parser_next_token_is_keyword (parser, RID_STATIC))
10302 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
10303 else if (c_parser_next_token_is_keyword (parser, RID_AUTO))
10304 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
10305 else
10306 goto invalid_kind;
10308 c_parser_consume_token (parser);
10309 if (c_parser_next_token_is (parser, CPP_COMMA))
10311 location_t here;
10312 c_parser_consume_token (parser);
10314 here = c_parser_peek_token (parser)->location;
10315 t = c_parser_expr_no_commas (parser, NULL).value;
10316 mark_exp_read (t);
10317 t = c_fully_fold (t, false, NULL);
10319 if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
10320 error_at (here, "schedule %<runtime%> does not take "
10321 "a %<chunk_size%> parameter");
10322 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
10323 error_at (here,
10324 "schedule %<auto%> does not take "
10325 "a %<chunk_size%> parameter");
10326 else if (TREE_CODE (TREE_TYPE (t)) == INTEGER_TYPE)
10327 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
10328 else
10329 c_parser_error (parser, "expected integer expression");
10331 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10333 else
10334 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
10335 "expected %<,%> or %<)%>");
10337 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule");
10338 OMP_CLAUSE_CHAIN (c) = list;
10339 return c;
10341 invalid_kind:
10342 c_parser_error (parser, "invalid schedule kind");
10343 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 0);
10344 return list;
10347 /* OpenMP 2.5:
10348 shared ( variable-list ) */
10350 static tree
10351 c_parser_omp_clause_shared (c_parser *parser, tree list)
10353 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_SHARED, list);
10356 /* OpenMP 3.0:
10357 untied */
10359 static tree
10360 c_parser_omp_clause_untied (c_parser *parser ATTRIBUTE_UNUSED, tree list)
10362 tree c;
10364 /* FIXME: Should we allow duplicates? */
10365 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied");
10367 c = build_omp_clause (c_parser_peek_token (parser)->location,
10368 OMP_CLAUSE_UNTIED);
10369 OMP_CLAUSE_CHAIN (c) = list;
10371 return c;
10374 /* OpenMP 4.0:
10375 inbranch
10376 notinbranch */
10378 static tree
10379 c_parser_omp_clause_branch (c_parser *parser ATTRIBUTE_UNUSED,
10380 enum omp_clause_code code, tree list)
10382 check_no_duplicate_clause (list, code, omp_clause_code_name[code]);
10384 tree c = build_omp_clause (c_parser_peek_token (parser)->location, code);
10385 OMP_CLAUSE_CHAIN (c) = list;
10387 return c;
10390 /* OpenMP 4.0:
10391 parallel
10393 sections
10394 taskgroup */
10396 static tree
10397 c_parser_omp_clause_cancelkind (c_parser *parser ATTRIBUTE_UNUSED,
10398 enum omp_clause_code code, tree list)
10400 tree c = build_omp_clause (c_parser_peek_token (parser)->location, code);
10401 OMP_CLAUSE_CHAIN (c) = list;
10403 return c;
10406 /* OpenMP 4.0:
10407 num_teams ( expression ) */
10409 static tree
10410 c_parser_omp_clause_num_teams (c_parser *parser, tree list)
10412 location_t num_teams_loc = c_parser_peek_token (parser)->location;
10413 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10415 location_t expr_loc = c_parser_peek_token (parser)->location;
10416 tree c, t = c_parser_expression (parser).value;
10417 mark_exp_read (t);
10418 t = c_fully_fold (t, false, NULL);
10420 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10422 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
10424 c_parser_error (parser, "expected integer expression");
10425 return list;
10428 /* Attempt to statically determine when the number isn't positive. */
10429 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
10430 build_int_cst (TREE_TYPE (t), 0));
10431 if (CAN_HAVE_LOCATION_P (c))
10432 SET_EXPR_LOCATION (c, expr_loc);
10433 if (c == boolean_true_node)
10435 warning_at (expr_loc, 0, "%<num_teams%> value must be positive");
10436 t = integer_one_node;
10439 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TEAMS, "num_teams");
10441 c = build_omp_clause (num_teams_loc, OMP_CLAUSE_NUM_TEAMS);
10442 OMP_CLAUSE_NUM_TEAMS_EXPR (c) = t;
10443 OMP_CLAUSE_CHAIN (c) = list;
10444 list = c;
10447 return list;
10450 /* OpenMP 4.0:
10451 thread_limit ( expression ) */
10453 static tree
10454 c_parser_omp_clause_thread_limit (c_parser *parser, tree list)
10456 location_t num_thread_limit_loc = c_parser_peek_token (parser)->location;
10457 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10459 location_t expr_loc = c_parser_peek_token (parser)->location;
10460 tree c, t = c_parser_expression (parser).value;
10461 mark_exp_read (t);
10462 t = c_fully_fold (t, false, NULL);
10464 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10466 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
10468 c_parser_error (parser, "expected integer expression");
10469 return list;
10472 /* Attempt to statically determine when the number isn't positive. */
10473 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
10474 build_int_cst (TREE_TYPE (t), 0));
10475 if (CAN_HAVE_LOCATION_P (c))
10476 SET_EXPR_LOCATION (c, expr_loc);
10477 if (c == boolean_true_node)
10479 warning_at (expr_loc, 0, "%<thread_limit%> value must be positive");
10480 t = integer_one_node;
10483 check_no_duplicate_clause (list, OMP_CLAUSE_THREAD_LIMIT,
10484 "thread_limit");
10486 c = build_omp_clause (num_thread_limit_loc, OMP_CLAUSE_THREAD_LIMIT);
10487 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
10488 OMP_CLAUSE_CHAIN (c) = list;
10489 list = c;
10492 return list;
10495 /* OpenMP 4.0:
10496 aligned ( variable-list )
10497 aligned ( variable-list : constant-expression ) */
10499 static tree
10500 c_parser_omp_clause_aligned (c_parser *parser, tree list)
10502 location_t clause_loc = c_parser_peek_token (parser)->location;
10503 tree nl, c;
10505 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10506 return list;
10508 nl = c_parser_omp_variable_list (parser, clause_loc,
10509 OMP_CLAUSE_ALIGNED, list);
10511 if (c_parser_next_token_is (parser, CPP_COLON))
10513 c_parser_consume_token (parser);
10514 tree alignment = c_parser_expr_no_commas (parser, NULL).value;
10515 mark_exp_read (alignment);
10516 alignment = c_fully_fold (alignment, false, NULL);
10517 if (!INTEGRAL_TYPE_P (TREE_TYPE (alignment))
10518 && TREE_CODE (alignment) != INTEGER_CST
10519 && tree_int_cst_sgn (alignment) != 1)
10521 error_at (clause_loc, "%<aligned%> clause alignment expression must "
10522 "be positive constant integer expression");
10523 alignment = NULL_TREE;
10526 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
10527 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = alignment;
10530 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10531 return nl;
10534 /* OpenMP 4.0:
10535 linear ( variable-list )
10536 linear ( variable-list : expression ) */
10538 static tree
10539 c_parser_omp_clause_linear (c_parser *parser, tree list, bool is_cilk_simd_fn)
10541 location_t clause_loc = c_parser_peek_token (parser)->location;
10542 tree nl, c, step;
10544 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10545 return list;
10547 nl = c_parser_omp_variable_list (parser, clause_loc,
10548 OMP_CLAUSE_LINEAR, list);
10550 if (c_parser_next_token_is (parser, CPP_COLON))
10552 c_parser_consume_token (parser);
10553 step = c_parser_expression (parser).value;
10554 mark_exp_read (step);
10555 step = c_fully_fold (step, false, NULL);
10556 if (is_cilk_simd_fn && TREE_CODE (step) == PARM_DECL)
10558 sorry ("using parameters for %<linear%> step is not supported yet");
10559 step = integer_one_node;
10561 if (!INTEGRAL_TYPE_P (TREE_TYPE (step)))
10563 error_at (clause_loc, "%<linear%> clause step expression must "
10564 "be integral");
10565 step = integer_one_node;
10569 else
10570 step = integer_one_node;
10572 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
10574 OMP_CLAUSE_LINEAR_STEP (c) = step;
10577 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10578 return nl;
10581 /* OpenMP 4.0:
10582 safelen ( constant-expression ) */
10584 static tree
10585 c_parser_omp_clause_safelen (c_parser *parser, tree list)
10587 location_t clause_loc = c_parser_peek_token (parser)->location;
10588 tree c, t;
10590 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10591 return list;
10593 t = c_parser_expr_no_commas (parser, NULL).value;
10594 mark_exp_read (t);
10595 t = c_fully_fold (t, false, NULL);
10596 if (!INTEGRAL_TYPE_P (TREE_TYPE (t))
10597 && TREE_CODE (t) != INTEGER_CST
10598 && tree_int_cst_sgn (t) != 1)
10600 error_at (clause_loc, "%<safelen%> clause expression must "
10601 "be positive constant integer expression");
10602 t = NULL_TREE;
10605 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10606 if (t == NULL_TREE || t == error_mark_node)
10607 return list;
10609 check_no_duplicate_clause (list, OMP_CLAUSE_SAFELEN, "safelen");
10611 c = build_omp_clause (clause_loc, OMP_CLAUSE_SAFELEN);
10612 OMP_CLAUSE_SAFELEN_EXPR (c) = t;
10613 OMP_CLAUSE_CHAIN (c) = list;
10614 return c;
10617 /* OpenMP 4.0:
10618 simdlen ( constant-expression ) */
10620 static tree
10621 c_parser_omp_clause_simdlen (c_parser *parser, tree list)
10623 location_t clause_loc = c_parser_peek_token (parser)->location;
10624 tree c, t;
10626 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10627 return list;
10629 t = c_parser_expr_no_commas (parser, NULL).value;
10630 mark_exp_read (t);
10631 t = c_fully_fold (t, false, NULL);
10632 if (!INTEGRAL_TYPE_P (TREE_TYPE (t))
10633 && TREE_CODE (t) != INTEGER_CST
10634 && tree_int_cst_sgn (t) != 1)
10636 error_at (clause_loc, "%<simdlen%> clause expression must "
10637 "be positive constant integer expression");
10638 t = NULL_TREE;
10641 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10642 if (t == NULL_TREE || t == error_mark_node)
10643 return list;
10645 check_no_duplicate_clause (list, OMP_CLAUSE_SIMDLEN, "simdlen");
10647 c = build_omp_clause (clause_loc, OMP_CLAUSE_SIMDLEN);
10648 OMP_CLAUSE_SIMDLEN_EXPR (c) = t;
10649 OMP_CLAUSE_CHAIN (c) = list;
10650 return c;
10653 /* OpenMP 4.0:
10654 depend ( depend-kind: variable-list )
10656 depend-kind:
10657 in | out | inout */
10659 static tree
10660 c_parser_omp_clause_depend (c_parser *parser, tree list)
10662 location_t clause_loc = c_parser_peek_token (parser)->location;
10663 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_INOUT;
10664 tree nl, c;
10666 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10667 return list;
10669 if (c_parser_next_token_is (parser, CPP_NAME))
10671 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
10672 if (strcmp ("in", p) == 0)
10673 kind = OMP_CLAUSE_DEPEND_IN;
10674 else if (strcmp ("inout", p) == 0)
10675 kind = OMP_CLAUSE_DEPEND_INOUT;
10676 else if (strcmp ("out", p) == 0)
10677 kind = OMP_CLAUSE_DEPEND_OUT;
10678 else
10679 goto invalid_kind;
10681 else
10682 goto invalid_kind;
10684 c_parser_consume_token (parser);
10685 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
10686 goto resync_fail;
10688 nl = c_parser_omp_variable_list (parser, clause_loc,
10689 OMP_CLAUSE_DEPEND, list);
10691 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
10692 OMP_CLAUSE_DEPEND_KIND (c) = kind;
10694 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10695 return nl;
10697 invalid_kind:
10698 c_parser_error (parser, "invalid depend kind");
10699 resync_fail:
10700 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10701 return list;
10704 /* OpenMP 4.0:
10705 map ( map-kind: variable-list )
10706 map ( variable-list )
10708 map-kind:
10709 alloc | to | from | tofrom */
10711 static tree
10712 c_parser_omp_clause_map (c_parser *parser, tree list)
10714 location_t clause_loc = c_parser_peek_token (parser)->location;
10715 enum omp_clause_map_kind kind = OMP_CLAUSE_MAP_TOFROM;
10716 tree nl, c;
10718 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10719 return list;
10721 if (c_parser_next_token_is (parser, CPP_NAME)
10722 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
10724 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
10725 if (strcmp ("alloc", p) == 0)
10726 kind = OMP_CLAUSE_MAP_ALLOC;
10727 else if (strcmp ("to", p) == 0)
10728 kind = OMP_CLAUSE_MAP_TO;
10729 else if (strcmp ("from", p) == 0)
10730 kind = OMP_CLAUSE_MAP_FROM;
10731 else if (strcmp ("tofrom", p) == 0)
10732 kind = OMP_CLAUSE_MAP_TOFROM;
10733 else
10735 c_parser_error (parser, "invalid map kind");
10736 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
10737 "expected %<)%>");
10738 return list;
10740 c_parser_consume_token (parser);
10741 c_parser_consume_token (parser);
10744 nl = c_parser_omp_variable_list (parser, clause_loc, OMP_CLAUSE_MAP, list);
10746 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
10747 OMP_CLAUSE_MAP_KIND (c) = kind;
10749 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10750 return nl;
10753 /* OpenMP 4.0:
10754 device ( expression ) */
10756 static tree
10757 c_parser_omp_clause_device (c_parser *parser, tree list)
10759 location_t clause_loc = c_parser_peek_token (parser)->location;
10760 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10762 tree c, t = c_parser_expr_no_commas (parser, NULL).value;
10763 mark_exp_read (t);
10764 t = c_fully_fold (t, false, NULL);
10766 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10768 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
10770 c_parser_error (parser, "expected integer expression");
10771 return list;
10774 check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE, "device");
10776 c = build_omp_clause (clause_loc, OMP_CLAUSE_DEVICE);
10777 OMP_CLAUSE_DEVICE_ID (c) = t;
10778 OMP_CLAUSE_CHAIN (c) = list;
10779 list = c;
10782 return list;
10785 /* OpenMP 4.0:
10786 dist_schedule ( static )
10787 dist_schedule ( static , expression ) */
10789 static tree
10790 c_parser_omp_clause_dist_schedule (c_parser *parser, tree list)
10792 tree c, t = NULL_TREE;
10793 location_t loc = c_parser_peek_token (parser)->location;
10795 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10796 return list;
10798 if (!c_parser_next_token_is_keyword (parser, RID_STATIC))
10800 c_parser_error (parser, "invalid dist_schedule kind");
10801 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
10802 "expected %<)%>");
10803 return list;
10806 c_parser_consume_token (parser);
10807 if (c_parser_next_token_is (parser, CPP_COMMA))
10809 c_parser_consume_token (parser);
10811 t = c_parser_expr_no_commas (parser, NULL).value;
10812 mark_exp_read (t);
10813 t = c_fully_fold (t, false, NULL);
10814 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10816 else
10817 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
10818 "expected %<,%> or %<)%>");
10820 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule");
10821 if (t == error_mark_node)
10822 return list;
10824 c = build_omp_clause (loc, OMP_CLAUSE_DIST_SCHEDULE);
10825 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
10826 OMP_CLAUSE_CHAIN (c) = list;
10827 return c;
10830 /* OpenMP 4.0:
10831 proc_bind ( proc-bind-kind )
10833 proc-bind-kind:
10834 master | close | spread */
10836 static tree
10837 c_parser_omp_clause_proc_bind (c_parser *parser, tree list)
10839 location_t clause_loc = c_parser_peek_token (parser)->location;
10840 enum omp_clause_proc_bind_kind kind;
10841 tree c;
10843 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10844 return list;
10846 if (c_parser_next_token_is (parser, CPP_NAME))
10848 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
10849 if (strcmp ("master", p) == 0)
10850 kind = OMP_CLAUSE_PROC_BIND_MASTER;
10851 else if (strcmp ("close", p) == 0)
10852 kind = OMP_CLAUSE_PROC_BIND_CLOSE;
10853 else if (strcmp ("spread", p) == 0)
10854 kind = OMP_CLAUSE_PROC_BIND_SPREAD;
10855 else
10856 goto invalid_kind;
10858 else
10859 goto invalid_kind;
10861 c_parser_consume_token (parser);
10862 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10863 c = build_omp_clause (clause_loc, OMP_CLAUSE_PROC_BIND);
10864 OMP_CLAUSE_PROC_BIND_KIND (c) = kind;
10865 OMP_CLAUSE_CHAIN (c) = list;
10866 return c;
10868 invalid_kind:
10869 c_parser_error (parser, "invalid proc_bind kind");
10870 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10871 return list;
10874 /* OpenMP 4.0:
10875 to ( variable-list ) */
10877 static tree
10878 c_parser_omp_clause_to (c_parser *parser, tree list)
10880 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_TO, list);
10883 /* OpenMP 4.0:
10884 from ( variable-list ) */
10886 static tree
10887 c_parser_omp_clause_from (c_parser *parser, tree list)
10889 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_FROM, list);
10892 /* OpenMP 4.0:
10893 uniform ( variable-list ) */
10895 static tree
10896 c_parser_omp_clause_uniform (c_parser *parser, tree list)
10898 /* The clauses location. */
10899 location_t loc = c_parser_peek_token (parser)->location;
10901 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10903 list = c_parser_omp_variable_list (parser, loc, OMP_CLAUSE_UNIFORM,
10904 list);
10905 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10907 return list;
10910 /* Parse all OpenMP clauses. The set clauses allowed by the directive
10911 is a bitmask in MASK. Return the list of clauses found; the result
10912 of clause default goes in *pdefault. */
10914 static tree
10915 c_parser_omp_all_clauses (c_parser *parser, omp_clause_mask mask,
10916 const char *where, bool finish_p = true)
10918 tree clauses = NULL;
10919 bool first = true, cilk_simd_fn = false;
10921 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
10923 location_t here;
10924 pragma_omp_clause c_kind;
10925 const char *c_name;
10926 tree prev = clauses;
10928 if (!first && c_parser_next_token_is (parser, CPP_COMMA))
10929 c_parser_consume_token (parser);
10931 here = c_parser_peek_token (parser)->location;
10932 c_kind = c_parser_omp_clause_name (parser);
10934 switch (c_kind)
10936 case PRAGMA_OMP_CLAUSE_COLLAPSE:
10937 clauses = c_parser_omp_clause_collapse (parser, clauses);
10938 c_name = "collapse";
10939 break;
10940 case PRAGMA_OMP_CLAUSE_COPYIN:
10941 clauses = c_parser_omp_clause_copyin (parser, clauses);
10942 c_name = "copyin";
10943 break;
10944 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
10945 clauses = c_parser_omp_clause_copyprivate (parser, clauses);
10946 c_name = "copyprivate";
10947 break;
10948 case PRAGMA_OMP_CLAUSE_DEFAULT:
10949 clauses = c_parser_omp_clause_default (parser, clauses);
10950 c_name = "default";
10951 break;
10952 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
10953 clauses = c_parser_omp_clause_firstprivate (parser, clauses);
10954 c_name = "firstprivate";
10955 break;
10956 case PRAGMA_OMP_CLAUSE_FINAL:
10957 clauses = c_parser_omp_clause_final (parser, clauses);
10958 c_name = "final";
10959 break;
10960 case PRAGMA_OMP_CLAUSE_IF:
10961 clauses = c_parser_omp_clause_if (parser, clauses);
10962 c_name = "if";
10963 break;
10964 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
10965 clauses = c_parser_omp_clause_lastprivate (parser, clauses);
10966 c_name = "lastprivate";
10967 break;
10968 case PRAGMA_OMP_CLAUSE_MERGEABLE:
10969 clauses = c_parser_omp_clause_mergeable (parser, clauses);
10970 c_name = "mergeable";
10971 break;
10972 case PRAGMA_OMP_CLAUSE_NOWAIT:
10973 clauses = c_parser_omp_clause_nowait (parser, clauses);
10974 c_name = "nowait";
10975 break;
10976 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
10977 clauses = c_parser_omp_clause_num_threads (parser, clauses);
10978 c_name = "num_threads";
10979 break;
10980 case PRAGMA_OMP_CLAUSE_ORDERED:
10981 clauses = c_parser_omp_clause_ordered (parser, clauses);
10982 c_name = "ordered";
10983 break;
10984 case PRAGMA_OMP_CLAUSE_PRIVATE:
10985 clauses = c_parser_omp_clause_private (parser, clauses);
10986 c_name = "private";
10987 break;
10988 case PRAGMA_OMP_CLAUSE_REDUCTION:
10989 clauses = c_parser_omp_clause_reduction (parser, clauses);
10990 c_name = "reduction";
10991 break;
10992 case PRAGMA_OMP_CLAUSE_SCHEDULE:
10993 clauses = c_parser_omp_clause_schedule (parser, clauses);
10994 c_name = "schedule";
10995 break;
10996 case PRAGMA_OMP_CLAUSE_SHARED:
10997 clauses = c_parser_omp_clause_shared (parser, clauses);
10998 c_name = "shared";
10999 break;
11000 case PRAGMA_OMP_CLAUSE_UNTIED:
11001 clauses = c_parser_omp_clause_untied (parser, clauses);
11002 c_name = "untied";
11003 break;
11004 case PRAGMA_OMP_CLAUSE_INBRANCH:
11005 case PRAGMA_CILK_CLAUSE_MASK:
11006 clauses = c_parser_omp_clause_branch (parser, OMP_CLAUSE_INBRANCH,
11007 clauses);
11008 c_name = "inbranch";
11009 break;
11010 case PRAGMA_OMP_CLAUSE_NOTINBRANCH:
11011 case PRAGMA_CILK_CLAUSE_NOMASK:
11012 clauses = c_parser_omp_clause_branch (parser, OMP_CLAUSE_NOTINBRANCH,
11013 clauses);
11014 c_name = "notinbranch";
11015 break;
11016 case PRAGMA_OMP_CLAUSE_PARALLEL:
11017 clauses
11018 = c_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_PARALLEL,
11019 clauses);
11020 c_name = "parallel";
11021 if (!first)
11023 clause_not_first:
11024 error_at (here, "%qs must be the first clause of %qs",
11025 c_name, where);
11026 clauses = prev;
11028 break;
11029 case PRAGMA_OMP_CLAUSE_FOR:
11030 clauses
11031 = c_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_FOR,
11032 clauses);
11033 c_name = "for";
11034 if (!first)
11035 goto clause_not_first;
11036 break;
11037 case PRAGMA_OMP_CLAUSE_SECTIONS:
11038 clauses
11039 = c_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_SECTIONS,
11040 clauses);
11041 c_name = "sections";
11042 if (!first)
11043 goto clause_not_first;
11044 break;
11045 case PRAGMA_OMP_CLAUSE_TASKGROUP:
11046 clauses
11047 = c_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_TASKGROUP,
11048 clauses);
11049 c_name = "taskgroup";
11050 if (!first)
11051 goto clause_not_first;
11052 break;
11053 case PRAGMA_OMP_CLAUSE_TO:
11054 clauses = c_parser_omp_clause_to (parser, clauses);
11055 c_name = "to";
11056 break;
11057 case PRAGMA_OMP_CLAUSE_FROM:
11058 clauses = c_parser_omp_clause_from (parser, clauses);
11059 c_name = "from";
11060 break;
11061 case PRAGMA_OMP_CLAUSE_UNIFORM:
11062 clauses = c_parser_omp_clause_uniform (parser, clauses);
11063 c_name = "uniform";
11064 break;
11065 case PRAGMA_OMP_CLAUSE_NUM_TEAMS:
11066 clauses = c_parser_omp_clause_num_teams (parser, clauses);
11067 c_name = "num_teams";
11068 break;
11069 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT:
11070 clauses = c_parser_omp_clause_thread_limit (parser, clauses);
11071 c_name = "thread_limit";
11072 break;
11073 case PRAGMA_OMP_CLAUSE_ALIGNED:
11074 clauses = c_parser_omp_clause_aligned (parser, clauses);
11075 c_name = "aligned";
11076 break;
11077 case PRAGMA_OMP_CLAUSE_LINEAR:
11078 if (((mask >> PRAGMA_CILK_CLAUSE_VECTORLENGTH) & 1) != 0)
11079 cilk_simd_fn = true;
11080 clauses = c_parser_omp_clause_linear (parser, clauses, cilk_simd_fn);
11081 c_name = "linear";
11082 break;
11083 case PRAGMA_OMP_CLAUSE_DEPEND:
11084 clauses = c_parser_omp_clause_depend (parser, clauses);
11085 c_name = "depend";
11086 break;
11087 case PRAGMA_OMP_CLAUSE_MAP:
11088 clauses = c_parser_omp_clause_map (parser, clauses);
11089 c_name = "map";
11090 break;
11091 case PRAGMA_OMP_CLAUSE_DEVICE:
11092 clauses = c_parser_omp_clause_device (parser, clauses);
11093 c_name = "device";
11094 break;
11095 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE:
11096 clauses = c_parser_omp_clause_dist_schedule (parser, clauses);
11097 c_name = "dist_schedule";
11098 break;
11099 case PRAGMA_OMP_CLAUSE_PROC_BIND:
11100 clauses = c_parser_omp_clause_proc_bind (parser, clauses);
11101 c_name = "proc_bind";
11102 break;
11103 case PRAGMA_OMP_CLAUSE_SAFELEN:
11104 clauses = c_parser_omp_clause_safelen (parser, clauses);
11105 c_name = "safelen";
11106 break;
11107 case PRAGMA_CILK_CLAUSE_VECTORLENGTH:
11108 clauses = c_parser_cilk_clause_vectorlength (parser, clauses, true);
11109 c_name = "simdlen";
11110 break;
11111 case PRAGMA_OMP_CLAUSE_SIMDLEN:
11112 clauses = c_parser_omp_clause_simdlen (parser, clauses);
11113 c_name = "simdlen";
11114 break;
11115 default:
11116 c_parser_error (parser, "expected %<#pragma omp%> clause");
11117 goto saw_error;
11120 first = false;
11122 if (((mask >> c_kind) & 1) == 0 && !parser->error)
11124 /* Remove the invalid clause(s) from the list to avoid
11125 confusing the rest of the compiler. */
11126 clauses = prev;
11127 error_at (here, "%qs is not valid for %qs", c_name, where);
11131 saw_error:
11132 c_parser_skip_to_pragma_eol (parser);
11134 if (finish_p)
11135 return c_finish_omp_clauses (clauses);
11137 return clauses;
11140 /* OpenMP 2.5:
11141 structured-block:
11142 statement
11144 In practice, we're also interested in adding the statement to an
11145 outer node. So it is convenient if we work around the fact that
11146 c_parser_statement calls add_stmt. */
11148 static tree
11149 c_parser_omp_structured_block (c_parser *parser)
11151 tree stmt = push_stmt_list ();
11152 c_parser_statement (parser);
11153 return pop_stmt_list (stmt);
11156 /* OpenMP 2.5:
11157 # pragma omp atomic new-line
11158 expression-stmt
11160 expression-stmt:
11161 x binop= expr | x++ | ++x | x-- | --x
11162 binop:
11163 +, *, -, /, &, ^, |, <<, >>
11165 where x is an lvalue expression with scalar type.
11167 OpenMP 3.1:
11168 # pragma omp atomic new-line
11169 update-stmt
11171 # pragma omp atomic read new-line
11172 read-stmt
11174 # pragma omp atomic write new-line
11175 write-stmt
11177 # pragma omp atomic update new-line
11178 update-stmt
11180 # pragma omp atomic capture new-line
11181 capture-stmt
11183 # pragma omp atomic capture new-line
11184 capture-block
11186 read-stmt:
11187 v = x
11188 write-stmt:
11189 x = expr
11190 update-stmt:
11191 expression-stmt | x = x binop expr
11192 capture-stmt:
11193 v = expression-stmt
11194 capture-block:
11195 { v = x; update-stmt; } | { update-stmt; v = x; }
11197 OpenMP 4.0:
11198 update-stmt:
11199 expression-stmt | x = x binop expr | x = expr binop x
11200 capture-stmt:
11201 v = update-stmt
11202 capture-block:
11203 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
11205 where x and v are lvalue expressions with scalar type.
11207 LOC is the location of the #pragma token. */
11209 static void
11210 c_parser_omp_atomic (location_t loc, c_parser *parser)
11212 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE;
11213 tree lhs1 = NULL_TREE, rhs1 = NULL_TREE;
11214 tree stmt, orig_lhs, unfolded_lhs = NULL_TREE, unfolded_lhs1 = NULL_TREE;
11215 enum tree_code code = OMP_ATOMIC, opcode = NOP_EXPR;
11216 struct c_expr expr;
11217 location_t eloc;
11218 bool structured_block = false;
11219 bool swapped = false;
11220 bool seq_cst = false;
11222 if (c_parser_next_token_is (parser, CPP_NAME))
11224 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
11225 if (!strcmp (p, "seq_cst"))
11227 seq_cst = true;
11228 c_parser_consume_token (parser);
11229 if (c_parser_next_token_is (parser, CPP_COMMA)
11230 && c_parser_peek_2nd_token (parser)->type == CPP_NAME)
11231 c_parser_consume_token (parser);
11234 if (c_parser_next_token_is (parser, CPP_NAME))
11236 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
11238 if (!strcmp (p, "read"))
11239 code = OMP_ATOMIC_READ;
11240 else if (!strcmp (p, "write"))
11241 code = NOP_EXPR;
11242 else if (!strcmp (p, "update"))
11243 code = OMP_ATOMIC;
11244 else if (!strcmp (p, "capture"))
11245 code = OMP_ATOMIC_CAPTURE_NEW;
11246 else
11247 p = NULL;
11248 if (p)
11249 c_parser_consume_token (parser);
11251 if (!seq_cst)
11253 if (c_parser_next_token_is (parser, CPP_COMMA)
11254 && c_parser_peek_2nd_token (parser)->type == CPP_NAME)
11255 c_parser_consume_token (parser);
11257 if (c_parser_next_token_is (parser, CPP_NAME))
11259 const char *p
11260 = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
11261 if (!strcmp (p, "seq_cst"))
11263 seq_cst = true;
11264 c_parser_consume_token (parser);
11268 c_parser_skip_to_pragma_eol (parser);
11270 switch (code)
11272 case OMP_ATOMIC_READ:
11273 case NOP_EXPR: /* atomic write */
11274 v = c_parser_unary_expression (parser).value;
11275 v = c_fully_fold (v, false, NULL);
11276 if (v == error_mark_node)
11277 goto saw_error;
11278 loc = c_parser_peek_token (parser)->location;
11279 if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
11280 goto saw_error;
11281 if (code == NOP_EXPR)
11282 lhs = c_parser_expression (parser).value;
11283 else
11284 lhs = c_parser_unary_expression (parser).value;
11285 lhs = c_fully_fold (lhs, false, NULL);
11286 if (lhs == error_mark_node)
11287 goto saw_error;
11288 if (code == NOP_EXPR)
11290 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
11291 opcode. */
11292 code = OMP_ATOMIC;
11293 rhs = lhs;
11294 lhs = v;
11295 v = NULL_TREE;
11297 goto done;
11298 case OMP_ATOMIC_CAPTURE_NEW:
11299 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
11301 c_parser_consume_token (parser);
11302 structured_block = true;
11304 else
11306 v = c_parser_unary_expression (parser).value;
11307 v = c_fully_fold (v, false, NULL);
11308 if (v == error_mark_node)
11309 goto saw_error;
11310 if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
11311 goto saw_error;
11313 break;
11314 default:
11315 break;
11318 /* For structured_block case we don't know yet whether
11319 old or new x should be captured. */
11320 restart:
11321 eloc = c_parser_peek_token (parser)->location;
11322 expr = c_parser_unary_expression (parser);
11323 lhs = expr.value;
11324 expr = default_function_array_conversion (eloc, expr);
11325 unfolded_lhs = expr.value;
11326 lhs = c_fully_fold (lhs, false, NULL);
11327 orig_lhs = lhs;
11328 switch (TREE_CODE (lhs))
11330 case ERROR_MARK:
11331 saw_error:
11332 c_parser_skip_to_end_of_block_or_statement (parser);
11333 if (structured_block)
11335 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
11336 c_parser_consume_token (parser);
11337 else if (code == OMP_ATOMIC_CAPTURE_NEW)
11339 c_parser_skip_to_end_of_block_or_statement (parser);
11340 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
11341 c_parser_consume_token (parser);
11344 return;
11346 case POSTINCREMENT_EXPR:
11347 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
11348 code = OMP_ATOMIC_CAPTURE_OLD;
11349 /* FALLTHROUGH */
11350 case PREINCREMENT_EXPR:
11351 lhs = TREE_OPERAND (lhs, 0);
11352 unfolded_lhs = NULL_TREE;
11353 opcode = PLUS_EXPR;
11354 rhs = integer_one_node;
11355 break;
11357 case POSTDECREMENT_EXPR:
11358 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
11359 code = OMP_ATOMIC_CAPTURE_OLD;
11360 /* FALLTHROUGH */
11361 case PREDECREMENT_EXPR:
11362 lhs = TREE_OPERAND (lhs, 0);
11363 unfolded_lhs = NULL_TREE;
11364 opcode = MINUS_EXPR;
11365 rhs = integer_one_node;
11366 break;
11368 case COMPOUND_EXPR:
11369 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
11370 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
11371 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
11372 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
11373 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
11374 (TREE_OPERAND (lhs, 1), 0), 0)))
11375 == BOOLEAN_TYPE)
11376 /* Undo effects of boolean_increment for post {in,de}crement. */
11377 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
11378 /* FALLTHRU */
11379 case MODIFY_EXPR:
11380 if (TREE_CODE (lhs) == MODIFY_EXPR
11381 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
11383 /* Undo effects of boolean_increment. */
11384 if (integer_onep (TREE_OPERAND (lhs, 1)))
11386 /* This is pre or post increment. */
11387 rhs = TREE_OPERAND (lhs, 1);
11388 lhs = TREE_OPERAND (lhs, 0);
11389 unfolded_lhs = NULL_TREE;
11390 opcode = NOP_EXPR;
11391 if (code == OMP_ATOMIC_CAPTURE_NEW
11392 && !structured_block
11393 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
11394 code = OMP_ATOMIC_CAPTURE_OLD;
11395 break;
11397 if (TREE_CODE (TREE_OPERAND (lhs, 1)) == TRUTH_NOT_EXPR
11398 && TREE_OPERAND (lhs, 0)
11399 == TREE_OPERAND (TREE_OPERAND (lhs, 1), 0))
11401 /* This is pre or post decrement. */
11402 rhs = TREE_OPERAND (lhs, 1);
11403 lhs = TREE_OPERAND (lhs, 0);
11404 unfolded_lhs = NULL_TREE;
11405 opcode = NOP_EXPR;
11406 if (code == OMP_ATOMIC_CAPTURE_NEW
11407 && !structured_block
11408 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
11409 code = OMP_ATOMIC_CAPTURE_OLD;
11410 break;
11413 /* FALLTHRU */
11414 default:
11415 switch (c_parser_peek_token (parser)->type)
11417 case CPP_MULT_EQ:
11418 opcode = MULT_EXPR;
11419 break;
11420 case CPP_DIV_EQ:
11421 opcode = TRUNC_DIV_EXPR;
11422 break;
11423 case CPP_PLUS_EQ:
11424 opcode = PLUS_EXPR;
11425 break;
11426 case CPP_MINUS_EQ:
11427 opcode = MINUS_EXPR;
11428 break;
11429 case CPP_LSHIFT_EQ:
11430 opcode = LSHIFT_EXPR;
11431 break;
11432 case CPP_RSHIFT_EQ:
11433 opcode = RSHIFT_EXPR;
11434 break;
11435 case CPP_AND_EQ:
11436 opcode = BIT_AND_EXPR;
11437 break;
11438 case CPP_OR_EQ:
11439 opcode = BIT_IOR_EXPR;
11440 break;
11441 case CPP_XOR_EQ:
11442 opcode = BIT_XOR_EXPR;
11443 break;
11444 case CPP_EQ:
11445 c_parser_consume_token (parser);
11446 eloc = c_parser_peek_token (parser)->location;
11447 expr = c_parser_expr_no_commas (parser, NULL, unfolded_lhs);
11448 rhs1 = expr.value;
11449 switch (TREE_CODE (rhs1))
11451 case MULT_EXPR:
11452 case TRUNC_DIV_EXPR:
11453 case PLUS_EXPR:
11454 case MINUS_EXPR:
11455 case LSHIFT_EXPR:
11456 case RSHIFT_EXPR:
11457 case BIT_AND_EXPR:
11458 case BIT_IOR_EXPR:
11459 case BIT_XOR_EXPR:
11460 if (c_tree_equal (TREE_OPERAND (rhs1, 0), unfolded_lhs))
11462 opcode = TREE_CODE (rhs1);
11463 rhs = c_fully_fold (TREE_OPERAND (rhs1, 1), false, NULL);
11464 rhs1 = c_fully_fold (TREE_OPERAND (rhs1, 0), false, NULL);
11465 goto stmt_done;
11467 if (c_tree_equal (TREE_OPERAND (rhs1, 1), unfolded_lhs))
11469 opcode = TREE_CODE (rhs1);
11470 rhs = c_fully_fold (TREE_OPERAND (rhs1, 0), false, NULL);
11471 rhs1 = c_fully_fold (TREE_OPERAND (rhs1, 1), false, NULL);
11472 swapped = !commutative_tree_code (opcode);
11473 goto stmt_done;
11475 break;
11476 case ERROR_MARK:
11477 goto saw_error;
11478 default:
11479 break;
11481 if (c_parser_peek_token (parser)->type == CPP_SEMICOLON)
11483 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
11485 code = OMP_ATOMIC_CAPTURE_OLD;
11486 v = lhs;
11487 lhs = NULL_TREE;
11488 expr = default_function_array_read_conversion (eloc, expr);
11489 unfolded_lhs1 = expr.value;
11490 lhs1 = c_fully_fold (unfolded_lhs1, false, NULL);
11491 rhs1 = NULL_TREE;
11492 c_parser_consume_token (parser);
11493 goto restart;
11495 if (structured_block)
11497 opcode = NOP_EXPR;
11498 expr = default_function_array_read_conversion (eloc, expr);
11499 rhs = c_fully_fold (expr.value, false, NULL);
11500 rhs1 = NULL_TREE;
11501 goto stmt_done;
11504 c_parser_error (parser, "invalid form of %<#pragma omp atomic%>");
11505 goto saw_error;
11506 default:
11507 c_parser_error (parser,
11508 "invalid operator for %<#pragma omp atomic%>");
11509 goto saw_error;
11512 /* Arrange to pass the location of the assignment operator to
11513 c_finish_omp_atomic. */
11514 loc = c_parser_peek_token (parser)->location;
11515 c_parser_consume_token (parser);
11516 eloc = c_parser_peek_token (parser)->location;
11517 expr = c_parser_expression (parser);
11518 expr = default_function_array_read_conversion (eloc, expr);
11519 rhs = expr.value;
11520 rhs = c_fully_fold (rhs, false, NULL);
11521 break;
11523 stmt_done:
11524 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
11526 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
11527 goto saw_error;
11528 v = c_parser_unary_expression (parser).value;
11529 v = c_fully_fold (v, false, NULL);
11530 if (v == error_mark_node)
11531 goto saw_error;
11532 if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
11533 goto saw_error;
11534 eloc = c_parser_peek_token (parser)->location;
11535 expr = c_parser_unary_expression (parser);
11536 lhs1 = expr.value;
11537 expr = default_function_array_read_conversion (eloc, expr);
11538 unfolded_lhs1 = expr.value;
11539 lhs1 = c_fully_fold (lhs1, false, NULL);
11540 if (lhs1 == error_mark_node)
11541 goto saw_error;
11543 if (structured_block)
11545 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
11546 c_parser_require (parser, CPP_CLOSE_BRACE, "expected %<}%>");
11548 done:
11549 if (unfolded_lhs && unfolded_lhs1
11550 && !c_tree_equal (unfolded_lhs, unfolded_lhs1))
11552 error ("%<#pragma omp atomic capture%> uses two different "
11553 "expressions for memory");
11554 stmt = error_mark_node;
11556 else
11557 stmt = c_finish_omp_atomic (loc, code, opcode, lhs, rhs, v, lhs1, rhs1,
11558 swapped, seq_cst);
11559 if (stmt != error_mark_node)
11560 add_stmt (stmt);
11562 if (!structured_block)
11563 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
11567 /* OpenMP 2.5:
11568 # pragma omp barrier new-line
11571 static void
11572 c_parser_omp_barrier (c_parser *parser)
11574 location_t loc = c_parser_peek_token (parser)->location;
11575 c_parser_consume_pragma (parser);
11576 c_parser_skip_to_pragma_eol (parser);
11578 c_finish_omp_barrier (loc);
11581 /* OpenMP 2.5:
11582 # pragma omp critical [(name)] new-line
11583 structured-block
11585 LOC is the location of the #pragma itself. */
11587 static tree
11588 c_parser_omp_critical (location_t loc, c_parser *parser)
11590 tree stmt, name = NULL;
11592 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
11594 c_parser_consume_token (parser);
11595 if (c_parser_next_token_is (parser, CPP_NAME))
11597 name = c_parser_peek_token (parser)->value;
11598 c_parser_consume_token (parser);
11599 c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11601 else
11602 c_parser_error (parser, "expected identifier");
11604 else if (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
11605 c_parser_error (parser, "expected %<(%> or end of line");
11606 c_parser_skip_to_pragma_eol (parser);
11608 stmt = c_parser_omp_structured_block (parser);
11609 return c_finish_omp_critical (loc, stmt, name);
11612 /* OpenMP 2.5:
11613 # pragma omp flush flush-vars[opt] new-line
11615 flush-vars:
11616 ( variable-list ) */
11618 static void
11619 c_parser_omp_flush (c_parser *parser)
11621 location_t loc = c_parser_peek_token (parser)->location;
11622 c_parser_consume_pragma (parser);
11623 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
11624 c_parser_omp_var_list_parens (parser, OMP_CLAUSE_ERROR, NULL);
11625 else if (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
11626 c_parser_error (parser, "expected %<(%> or end of line");
11627 c_parser_skip_to_pragma_eol (parser);
11629 c_finish_omp_flush (loc);
11632 /* Parse the restricted form of the for statement allowed by OpenMP.
11633 The real trick here is to determine the loop control variable early
11634 so that we can push a new decl if necessary to make it private.
11635 LOC is the location of the OMP in "#pragma omp". */
11637 static tree
11638 c_parser_omp_for_loop (location_t loc, c_parser *parser, enum tree_code code,
11639 tree clauses, tree *cclauses)
11641 tree decl, cond, incr, save_break, save_cont, body, init, stmt, cl;
11642 tree declv, condv, incrv, initv, ret = NULL;
11643 bool fail = false, open_brace_parsed = false;
11644 int i, collapse = 1, nbraces = 0;
11645 location_t for_loc;
11646 vec<tree, va_gc> *for_block = make_tree_vector ();
11648 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
11649 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
11650 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl));
11652 gcc_assert (collapse >= 1);
11654 declv = make_tree_vec (collapse);
11655 initv = make_tree_vec (collapse);
11656 condv = make_tree_vec (collapse);
11657 incrv = make_tree_vec (collapse);
11659 if (!c_parser_next_token_is_keyword (parser, RID_FOR))
11661 c_parser_error (parser, "for statement expected");
11662 return NULL;
11664 for_loc = c_parser_peek_token (parser)->location;
11665 c_parser_consume_token (parser);
11667 for (i = 0; i < collapse; i++)
11669 int bracecount = 0;
11671 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11672 goto pop_scopes;
11674 /* Parse the initialization declaration or expression. */
11675 if (c_parser_next_tokens_start_declaration (parser))
11677 if (i > 0)
11678 vec_safe_push (for_block, c_begin_compound_stmt (true));
11679 c_parser_declaration_or_fndef (parser, true, true, true, true, true,
11680 NULL, vNULL);
11681 decl = check_for_loop_decls (for_loc, flag_isoc99);
11682 if (decl == NULL)
11683 goto error_init;
11684 if (DECL_INITIAL (decl) == error_mark_node)
11685 decl = error_mark_node;
11686 init = decl;
11688 else if (c_parser_next_token_is (parser, CPP_NAME)
11689 && c_parser_peek_2nd_token (parser)->type == CPP_EQ)
11691 struct c_expr decl_exp;
11692 struct c_expr init_exp;
11693 location_t init_loc;
11695 decl_exp = c_parser_postfix_expression (parser);
11696 decl = decl_exp.value;
11698 c_parser_require (parser, CPP_EQ, "expected %<=%>");
11700 init_loc = c_parser_peek_token (parser)->location;
11701 init_exp = c_parser_expr_no_commas (parser, NULL);
11702 init_exp = default_function_array_read_conversion (init_loc,
11703 init_exp);
11704 init = build_modify_expr (init_loc, decl, decl_exp.original_type,
11705 NOP_EXPR, init_loc, init_exp.value,
11706 init_exp.original_type);
11707 init = c_process_expr_stmt (init_loc, init);
11709 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
11711 else
11713 error_init:
11714 c_parser_error (parser,
11715 "expected iteration declaration or initialization");
11716 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
11717 "expected %<)%>");
11718 fail = true;
11719 goto parse_next;
11722 /* Parse the loop condition. */
11723 cond = NULL_TREE;
11724 if (c_parser_next_token_is_not (parser, CPP_SEMICOLON))
11726 location_t cond_loc = c_parser_peek_token (parser)->location;
11727 struct c_expr cond_expr
11728 = c_parser_binary_expression (parser, NULL, NULL_TREE);
11730 cond = cond_expr.value;
11731 cond = c_objc_common_truthvalue_conversion (cond_loc, cond);
11732 cond = c_fully_fold (cond, false, NULL);
11733 switch (cond_expr.original_code)
11735 case GT_EXPR:
11736 case GE_EXPR:
11737 case LT_EXPR:
11738 case LE_EXPR:
11739 break;
11740 case NE_EXPR:
11741 if (code == CILK_SIMD)
11742 break;
11743 /* FALLTHRU. */
11744 default:
11745 /* Can't be cond = error_mark_node, because we want to preserve
11746 the location until c_finish_omp_for. */
11747 cond = build1 (NOP_EXPR, boolean_type_node, error_mark_node);
11748 break;
11750 protected_set_expr_location (cond, cond_loc);
11752 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
11754 /* Parse the increment expression. */
11755 incr = NULL_TREE;
11756 if (c_parser_next_token_is_not (parser, CPP_CLOSE_PAREN))
11758 location_t incr_loc = c_parser_peek_token (parser)->location;
11760 incr = c_process_expr_stmt (incr_loc,
11761 c_parser_expression (parser).value);
11763 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11765 if (decl == NULL || decl == error_mark_node || init == error_mark_node)
11766 fail = true;
11767 else
11769 TREE_VEC_ELT (declv, i) = decl;
11770 TREE_VEC_ELT (initv, i) = init;
11771 TREE_VEC_ELT (condv, i) = cond;
11772 TREE_VEC_ELT (incrv, i) = incr;
11775 parse_next:
11776 if (i == collapse - 1)
11777 break;
11779 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
11780 in between the collapsed for loops to be still considered perfectly
11781 nested. Hopefully the final version clarifies this.
11782 For now handle (multiple) {'s and empty statements. */
11785 if (c_parser_next_token_is_keyword (parser, RID_FOR))
11787 c_parser_consume_token (parser);
11788 break;
11790 else if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
11792 c_parser_consume_token (parser);
11793 bracecount++;
11795 else if (bracecount
11796 && c_parser_next_token_is (parser, CPP_SEMICOLON))
11797 c_parser_consume_token (parser);
11798 else
11800 c_parser_error (parser, "not enough perfectly nested loops");
11801 if (bracecount)
11803 open_brace_parsed = true;
11804 bracecount--;
11806 fail = true;
11807 collapse = 0;
11808 break;
11811 while (1);
11813 nbraces += bracecount;
11816 save_break = c_break_label;
11817 if (code == CILK_SIMD)
11818 c_break_label = build_int_cst (size_type_node, 2);
11819 else
11820 c_break_label = size_one_node;
11821 save_cont = c_cont_label;
11822 c_cont_label = NULL_TREE;
11823 body = push_stmt_list ();
11825 if (open_brace_parsed)
11827 location_t here = c_parser_peek_token (parser)->location;
11828 stmt = c_begin_compound_stmt (true);
11829 c_parser_compound_statement_nostart (parser);
11830 add_stmt (c_end_compound_stmt (here, stmt, true));
11832 else
11833 add_stmt (c_parser_c99_block_statement (parser));
11834 if (c_cont_label)
11836 tree t = build1 (LABEL_EXPR, void_type_node, c_cont_label);
11837 SET_EXPR_LOCATION (t, loc);
11838 add_stmt (t);
11841 body = pop_stmt_list (body);
11842 c_break_label = save_break;
11843 c_cont_label = save_cont;
11845 while (nbraces)
11847 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
11849 c_parser_consume_token (parser);
11850 nbraces--;
11852 else if (c_parser_next_token_is (parser, CPP_SEMICOLON))
11853 c_parser_consume_token (parser);
11854 else
11856 c_parser_error (parser, "collapsed loops not perfectly nested");
11857 while (nbraces)
11859 location_t here = c_parser_peek_token (parser)->location;
11860 stmt = c_begin_compound_stmt (true);
11861 add_stmt (body);
11862 c_parser_compound_statement_nostart (parser);
11863 body = c_end_compound_stmt (here, stmt, true);
11864 nbraces--;
11866 goto pop_scopes;
11870 /* Only bother calling c_finish_omp_for if we haven't already generated
11871 an error from the initialization parsing. */
11872 if (!fail)
11874 stmt = c_finish_omp_for (loc, code, declv, initv, condv,
11875 incrv, body, NULL);
11876 if (stmt)
11878 if (cclauses != NULL
11879 && cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL] != NULL)
11881 tree *c;
11882 for (c = &cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL]; *c ; )
11883 if (OMP_CLAUSE_CODE (*c) != OMP_CLAUSE_FIRSTPRIVATE
11884 && OMP_CLAUSE_CODE (*c) != OMP_CLAUSE_LASTPRIVATE)
11885 c = &OMP_CLAUSE_CHAIN (*c);
11886 else
11888 for (i = 0; i < collapse; i++)
11889 if (TREE_VEC_ELT (declv, i) == OMP_CLAUSE_DECL (*c))
11890 break;
11891 if (i == collapse)
11892 c = &OMP_CLAUSE_CHAIN (*c);
11893 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE)
11895 error_at (loc,
11896 "iteration variable %qD should not be firstprivate",
11897 OMP_CLAUSE_DECL (*c));
11898 *c = OMP_CLAUSE_CHAIN (*c);
11900 else
11902 /* Copy lastprivate (decl) clause to OMP_FOR_CLAUSES,
11903 change it to shared (decl) in
11904 OMP_PARALLEL_CLAUSES. */
11905 tree l = build_omp_clause (OMP_CLAUSE_LOCATION (*c),
11906 OMP_CLAUSE_LASTPRIVATE);
11907 OMP_CLAUSE_DECL (l) = OMP_CLAUSE_DECL (*c);
11908 OMP_CLAUSE_CHAIN (l) = clauses;
11909 clauses = l;
11910 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
11914 OMP_FOR_CLAUSES (stmt) = clauses;
11916 ret = stmt;
11918 pop_scopes:
11919 while (!for_block->is_empty ())
11921 /* FIXME diagnostics: LOC below should be the actual location of
11922 this particular for block. We need to build a list of
11923 locations to go along with FOR_BLOCK. */
11924 stmt = c_end_compound_stmt (loc, for_block->pop (), true);
11925 add_stmt (stmt);
11927 release_tree_vector (for_block);
11928 return ret;
11931 /* Helper function for OpenMP parsing, split clauses and call
11932 finish_omp_clauses on each of the set of clauses afterwards. */
11934 static void
11935 omp_split_clauses (location_t loc, enum tree_code code,
11936 omp_clause_mask mask, tree clauses, tree *cclauses)
11938 int i;
11939 c_omp_split_clauses (loc, code, mask, clauses, cclauses);
11940 for (i = 0; i < C_OMP_CLAUSE_SPLIT_COUNT; i++)
11941 if (cclauses[i])
11942 cclauses[i] = c_finish_omp_clauses (cclauses[i]);
11945 /* OpenMP 4.0:
11946 #pragma omp simd simd-clause[optseq] new-line
11947 for-loop
11949 LOC is the location of the #pragma token.
11952 #define OMP_SIMD_CLAUSE_MASK \
11953 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
11954 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
11955 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
11956 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
11957 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
11958 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
11959 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
11961 static tree
11962 c_parser_omp_simd (location_t loc, c_parser *parser,
11963 char *p_name, omp_clause_mask mask, tree *cclauses)
11965 tree block, clauses, ret;
11967 strcat (p_name, " simd");
11968 mask |= OMP_SIMD_CLAUSE_MASK;
11969 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED);
11971 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
11972 if (cclauses)
11974 omp_split_clauses (loc, OMP_SIMD, mask, clauses, cclauses);
11975 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SIMD];
11978 block = c_begin_compound_stmt (true);
11979 ret = c_parser_omp_for_loop (loc, parser, OMP_SIMD, clauses, cclauses);
11980 block = c_end_compound_stmt (loc, block, true);
11981 add_stmt (block);
11983 return ret;
11986 /* OpenMP 2.5:
11987 #pragma omp for for-clause[optseq] new-line
11988 for-loop
11990 OpenMP 4.0:
11991 #pragma omp for simd for-simd-clause[optseq] new-line
11992 for-loop
11994 LOC is the location of the #pragma token.
11997 #define OMP_FOR_CLAUSE_MASK \
11998 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
11999 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
12000 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
12001 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
12002 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
12003 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
12004 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
12005 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
12007 static tree
12008 c_parser_omp_for (location_t loc, c_parser *parser,
12009 char *p_name, omp_clause_mask mask, tree *cclauses)
12011 tree block, clauses, ret;
12013 strcat (p_name, " for");
12014 mask |= OMP_FOR_CLAUSE_MASK;
12015 if (cclauses)
12016 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
12018 if (c_parser_next_token_is (parser, CPP_NAME))
12020 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
12022 if (strcmp (p, "simd") == 0)
12024 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
12025 if (cclauses == NULL)
12026 cclauses = cclauses_buf;
12028 c_parser_consume_token (parser);
12029 if (!flag_openmp) /* flag_openmp_simd */
12030 return c_parser_omp_simd (loc, parser, p_name, mask, cclauses);
12031 block = c_begin_compound_stmt (true);
12032 ret = c_parser_omp_simd (loc, parser, p_name, mask, cclauses);
12033 block = c_end_compound_stmt (loc, block, true);
12034 if (ret == NULL_TREE)
12035 return ret;
12036 ret = make_node (OMP_FOR);
12037 TREE_TYPE (ret) = void_type_node;
12038 OMP_FOR_BODY (ret) = block;
12039 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
12040 SET_EXPR_LOCATION (ret, loc);
12041 add_stmt (ret);
12042 return ret;
12045 if (!flag_openmp) /* flag_openmp_simd */
12047 c_parser_skip_to_pragma_eol (parser);
12048 return NULL_TREE;
12051 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
12052 if (cclauses)
12054 omp_split_clauses (loc, OMP_FOR, mask, clauses, cclauses);
12055 clauses = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
12058 block = c_begin_compound_stmt (true);
12059 ret = c_parser_omp_for_loop (loc, parser, OMP_FOR, clauses, cclauses);
12060 block = c_end_compound_stmt (loc, block, true);
12061 add_stmt (block);
12063 return ret;
12066 /* OpenMP 2.5:
12067 # pragma omp master new-line
12068 structured-block
12070 LOC is the location of the #pragma token.
12073 static tree
12074 c_parser_omp_master (location_t loc, c_parser *parser)
12076 c_parser_skip_to_pragma_eol (parser);
12077 return c_finish_omp_master (loc, c_parser_omp_structured_block (parser));
12080 /* OpenMP 2.5:
12081 # pragma omp ordered new-line
12082 structured-block
12084 LOC is the location of the #pragma itself.
12087 static tree
12088 c_parser_omp_ordered (location_t loc, c_parser *parser)
12090 c_parser_skip_to_pragma_eol (parser);
12091 return c_finish_omp_ordered (loc, c_parser_omp_structured_block (parser));
12094 /* OpenMP 2.5:
12096 section-scope:
12097 { section-sequence }
12099 section-sequence:
12100 section-directive[opt] structured-block
12101 section-sequence section-directive structured-block
12103 SECTIONS_LOC is the location of the #pragma omp sections. */
12105 static tree
12106 c_parser_omp_sections_scope (location_t sections_loc, c_parser *parser)
12108 tree stmt, substmt;
12109 bool error_suppress = false;
12110 location_t loc;
12112 loc = c_parser_peek_token (parser)->location;
12113 if (!c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
12115 /* Avoid skipping until the end of the block. */
12116 parser->error = false;
12117 return NULL_TREE;
12120 stmt = push_stmt_list ();
12122 if (c_parser_peek_token (parser)->pragma_kind != PRAGMA_OMP_SECTION)
12124 substmt = c_parser_omp_structured_block (parser);
12125 substmt = build1 (OMP_SECTION, void_type_node, substmt);
12126 SET_EXPR_LOCATION (substmt, loc);
12127 add_stmt (substmt);
12130 while (1)
12132 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
12133 break;
12134 if (c_parser_next_token_is (parser, CPP_EOF))
12135 break;
12137 loc = c_parser_peek_token (parser)->location;
12138 if (c_parser_peek_token (parser)->pragma_kind == PRAGMA_OMP_SECTION)
12140 c_parser_consume_pragma (parser);
12141 c_parser_skip_to_pragma_eol (parser);
12142 error_suppress = false;
12144 else if (!error_suppress)
12146 error_at (loc, "expected %<#pragma omp section%> or %<}%>");
12147 error_suppress = true;
12150 substmt = c_parser_omp_structured_block (parser);
12151 substmt = build1 (OMP_SECTION, void_type_node, substmt);
12152 SET_EXPR_LOCATION (substmt, loc);
12153 add_stmt (substmt);
12155 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE,
12156 "expected %<#pragma omp section%> or %<}%>");
12158 substmt = pop_stmt_list (stmt);
12160 stmt = make_node (OMP_SECTIONS);
12161 SET_EXPR_LOCATION (stmt, sections_loc);
12162 TREE_TYPE (stmt) = void_type_node;
12163 OMP_SECTIONS_BODY (stmt) = substmt;
12165 return add_stmt (stmt);
12168 /* OpenMP 2.5:
12169 # pragma omp sections sections-clause[optseq] newline
12170 sections-scope
12172 LOC is the location of the #pragma token.
12175 #define OMP_SECTIONS_CLAUSE_MASK \
12176 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
12177 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
12178 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
12179 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
12180 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
12182 static tree
12183 c_parser_omp_sections (location_t loc, c_parser *parser,
12184 char *p_name, omp_clause_mask mask, tree *cclauses)
12186 tree block, clauses, ret;
12188 strcat (p_name, " sections");
12189 mask |= OMP_SECTIONS_CLAUSE_MASK;
12190 if (cclauses)
12191 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
12193 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
12194 if (cclauses)
12196 omp_split_clauses (loc, OMP_SECTIONS, mask, clauses, cclauses);
12197 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SECTIONS];
12200 block = c_begin_compound_stmt (true);
12201 ret = c_parser_omp_sections_scope (loc, parser);
12202 if (ret)
12203 OMP_SECTIONS_CLAUSES (ret) = clauses;
12204 block = c_end_compound_stmt (loc, block, true);
12205 add_stmt (block);
12207 return ret;
12210 /* OpenMP 2.5:
12211 # pragma omp parallel parallel-clause[optseq] new-line
12212 structured-block
12213 # pragma omp parallel for parallel-for-clause[optseq] new-line
12214 structured-block
12215 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
12216 structured-block
12218 OpenMP 4.0:
12219 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
12220 structured-block
12222 LOC is the location of the #pragma token.
12225 #define OMP_PARALLEL_CLAUSE_MASK \
12226 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
12227 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
12228 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
12229 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
12230 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
12231 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
12232 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
12233 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
12234 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
12236 static tree
12237 c_parser_omp_parallel (location_t loc, c_parser *parser,
12238 char *p_name, omp_clause_mask mask, tree *cclauses)
12240 tree stmt, clauses, block;
12242 strcat (p_name, " parallel");
12243 mask |= OMP_PARALLEL_CLAUSE_MASK;
12245 if (c_parser_next_token_is_keyword (parser, RID_FOR))
12247 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
12248 if (cclauses == NULL)
12249 cclauses = cclauses_buf;
12251 c_parser_consume_token (parser);
12252 if (!flag_openmp) /* flag_openmp_simd */
12253 return c_parser_omp_for (loc, parser, p_name, mask, cclauses);
12254 block = c_begin_omp_parallel ();
12255 tree ret = c_parser_omp_for (loc, parser, p_name, mask, cclauses);
12256 stmt
12257 = c_finish_omp_parallel (loc, cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
12258 block);
12259 if (ret == NULL_TREE)
12260 return ret;
12261 OMP_PARALLEL_COMBINED (stmt) = 1;
12262 return stmt;
12264 else if (cclauses)
12266 error_at (loc, "expected %<for%> after %qs", p_name);
12267 c_parser_skip_to_pragma_eol (parser);
12268 return NULL_TREE;
12270 else if (!flag_openmp) /* flag_openmp_simd */
12272 c_parser_skip_to_pragma_eol (parser);
12273 return NULL_TREE;
12275 else if (c_parser_next_token_is (parser, CPP_NAME))
12277 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
12278 if (strcmp (p, "sections") == 0)
12280 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
12281 if (cclauses == NULL)
12282 cclauses = cclauses_buf;
12284 c_parser_consume_token (parser);
12285 block = c_begin_omp_parallel ();
12286 c_parser_omp_sections (loc, parser, p_name, mask, cclauses);
12287 stmt = c_finish_omp_parallel (loc,
12288 cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
12289 block);
12290 OMP_PARALLEL_COMBINED (stmt) = 1;
12291 return stmt;
12295 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
12297 block = c_begin_omp_parallel ();
12298 c_parser_statement (parser);
12299 stmt = c_finish_omp_parallel (loc, clauses, block);
12301 return stmt;
12304 /* OpenMP 2.5:
12305 # pragma omp single single-clause[optseq] new-line
12306 structured-block
12308 LOC is the location of the #pragma.
12311 #define OMP_SINGLE_CLAUSE_MASK \
12312 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
12313 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
12314 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
12315 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
12317 static tree
12318 c_parser_omp_single (location_t loc, c_parser *parser)
12320 tree stmt = make_node (OMP_SINGLE);
12321 SET_EXPR_LOCATION (stmt, loc);
12322 TREE_TYPE (stmt) = void_type_node;
12324 OMP_SINGLE_CLAUSES (stmt)
12325 = c_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
12326 "#pragma omp single");
12327 OMP_SINGLE_BODY (stmt) = c_parser_omp_structured_block (parser);
12329 return add_stmt (stmt);
12332 /* OpenMP 3.0:
12333 # pragma omp task task-clause[optseq] new-line
12335 LOC is the location of the #pragma.
12338 #define OMP_TASK_CLAUSE_MASK \
12339 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
12340 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
12341 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
12342 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
12343 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
12344 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
12345 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
12346 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
12347 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND))
12349 static tree
12350 c_parser_omp_task (location_t loc, c_parser *parser)
12352 tree clauses, block;
12354 clauses = c_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
12355 "#pragma omp task");
12357 block = c_begin_omp_task ();
12358 c_parser_statement (parser);
12359 return c_finish_omp_task (loc, clauses, block);
12362 /* OpenMP 3.0:
12363 # pragma omp taskwait new-line
12366 static void
12367 c_parser_omp_taskwait (c_parser *parser)
12369 location_t loc = c_parser_peek_token (parser)->location;
12370 c_parser_consume_pragma (parser);
12371 c_parser_skip_to_pragma_eol (parser);
12373 c_finish_omp_taskwait (loc);
12376 /* OpenMP 3.1:
12377 # pragma omp taskyield new-line
12380 static void
12381 c_parser_omp_taskyield (c_parser *parser)
12383 location_t loc = c_parser_peek_token (parser)->location;
12384 c_parser_consume_pragma (parser);
12385 c_parser_skip_to_pragma_eol (parser);
12387 c_finish_omp_taskyield (loc);
12390 /* OpenMP 4.0:
12391 # pragma omp taskgroup new-line
12394 static tree
12395 c_parser_omp_taskgroup (c_parser *parser)
12397 location_t loc = c_parser_peek_token (parser)->location;
12398 c_parser_skip_to_pragma_eol (parser);
12399 return c_finish_omp_taskgroup (loc, c_parser_omp_structured_block (parser));
12402 /* OpenMP 4.0:
12403 # pragma omp cancel cancel-clause[optseq] new-line
12405 LOC is the location of the #pragma.
12408 #define OMP_CANCEL_CLAUSE_MASK \
12409 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
12410 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
12411 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
12412 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
12413 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
12415 static void
12416 c_parser_omp_cancel (c_parser *parser)
12418 location_t loc = c_parser_peek_token (parser)->location;
12420 c_parser_consume_pragma (parser);
12421 tree clauses = c_parser_omp_all_clauses (parser, OMP_CANCEL_CLAUSE_MASK,
12422 "#pragma omp cancel");
12424 c_finish_omp_cancel (loc, clauses);
12427 /* OpenMP 4.0:
12428 # pragma omp cancellation point cancelpt-clause[optseq] new-line
12430 LOC is the location of the #pragma.
12433 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
12434 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
12435 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
12436 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
12437 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
12439 static void
12440 c_parser_omp_cancellation_point (c_parser *parser)
12442 location_t loc = c_parser_peek_token (parser)->location;
12443 tree clauses;
12444 bool point_seen = false;
12446 c_parser_consume_pragma (parser);
12447 if (c_parser_next_token_is (parser, CPP_NAME))
12449 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
12450 if (strcmp (p, "point") == 0)
12452 c_parser_consume_token (parser);
12453 point_seen = true;
12456 if (!point_seen)
12458 c_parser_error (parser, "expected %<point%>");
12459 c_parser_skip_to_pragma_eol (parser);
12460 return;
12463 clauses
12464 = c_parser_omp_all_clauses (parser, OMP_CANCELLATION_POINT_CLAUSE_MASK,
12465 "#pragma omp cancellation point");
12467 c_finish_omp_cancellation_point (loc, clauses);
12470 /* OpenMP 4.0:
12471 #pragma omp distribute distribute-clause[optseq] new-line
12472 for-loop */
12474 #define OMP_DISTRIBUTE_CLAUSE_MASK \
12475 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
12476 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
12477 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
12478 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
12480 static tree
12481 c_parser_omp_distribute (location_t loc, c_parser *parser,
12482 char *p_name, omp_clause_mask mask, tree *cclauses)
12484 tree clauses, block, ret;
12486 strcat (p_name, " distribute");
12487 mask |= OMP_DISTRIBUTE_CLAUSE_MASK;
12489 if (c_parser_next_token_is (parser, CPP_NAME))
12491 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
12492 bool simd = false;
12493 bool parallel = false;
12495 if (strcmp (p, "simd") == 0)
12496 simd = true;
12497 else
12498 parallel = strcmp (p, "parallel") == 0;
12499 if (parallel || simd)
12501 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
12502 if (cclauses == NULL)
12503 cclauses = cclauses_buf;
12504 c_parser_consume_token (parser);
12505 if (!flag_openmp) /* flag_openmp_simd */
12507 if (simd)
12508 return c_parser_omp_simd (loc, parser, p_name, mask, cclauses);
12509 else
12510 return c_parser_omp_parallel (loc, parser, p_name, mask,
12511 cclauses);
12513 block = c_begin_compound_stmt (true);
12514 if (simd)
12515 ret = c_parser_omp_simd (loc, parser, p_name, mask, cclauses);
12516 else
12517 ret = c_parser_omp_parallel (loc, parser, p_name, mask, cclauses);
12518 block = c_end_compound_stmt (loc, block, true);
12519 if (ret == NULL)
12520 return ret;
12521 ret = make_node (OMP_DISTRIBUTE);
12522 TREE_TYPE (ret) = void_type_node;
12523 OMP_FOR_BODY (ret) = block;
12524 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
12525 SET_EXPR_LOCATION (ret, loc);
12526 add_stmt (ret);
12527 return ret;
12530 if (!flag_openmp) /* flag_openmp_simd */
12532 c_parser_skip_to_pragma_eol (parser);
12533 return NULL_TREE;
12536 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
12537 if (cclauses)
12539 omp_split_clauses (loc, OMP_DISTRIBUTE, mask, clauses, cclauses);
12540 clauses = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
12543 block = c_begin_compound_stmt (true);
12544 ret = c_parser_omp_for_loop (loc, parser, OMP_DISTRIBUTE, clauses, NULL);
12545 block = c_end_compound_stmt (loc, block, true);
12546 add_stmt (block);
12548 return ret;
12551 /* OpenMP 4.0:
12552 # pragma omp teams teams-clause[optseq] new-line
12553 structured-block */
12555 #define OMP_TEAMS_CLAUSE_MASK \
12556 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
12557 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
12558 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
12559 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
12560 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
12561 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
12562 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
12564 static tree
12565 c_parser_omp_teams (location_t loc, c_parser *parser,
12566 char *p_name, omp_clause_mask mask, tree *cclauses)
12568 tree clauses, block, ret;
12570 strcat (p_name, " teams");
12571 mask |= OMP_TEAMS_CLAUSE_MASK;
12573 if (c_parser_next_token_is (parser, CPP_NAME))
12575 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
12576 if (strcmp (p, "distribute") == 0)
12578 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
12579 if (cclauses == NULL)
12580 cclauses = cclauses_buf;
12582 c_parser_consume_token (parser);
12583 if (!flag_openmp) /* flag_openmp_simd */
12584 return c_parser_omp_distribute (loc, parser, p_name, mask, cclauses);
12585 block = c_begin_compound_stmt (true);
12586 ret = c_parser_omp_distribute (loc, parser, p_name, mask, cclauses);
12587 block = c_end_compound_stmt (loc, block, true);
12588 if (ret == NULL)
12589 return ret;
12590 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
12591 ret = make_node (OMP_TEAMS);
12592 TREE_TYPE (ret) = void_type_node;
12593 OMP_TEAMS_CLAUSES (ret) = clauses;
12594 OMP_TEAMS_BODY (ret) = block;
12595 return add_stmt (ret);
12598 if (!flag_openmp) /* flag_openmp_simd */
12600 c_parser_skip_to_pragma_eol (parser);
12601 return NULL_TREE;
12604 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
12605 if (cclauses)
12607 omp_split_clauses (loc, OMP_TEAMS, mask, clauses, cclauses);
12608 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
12611 tree stmt = make_node (OMP_TEAMS);
12612 TREE_TYPE (stmt) = void_type_node;
12613 OMP_TEAMS_CLAUSES (stmt) = clauses;
12614 OMP_TEAMS_BODY (stmt) = c_parser_omp_structured_block (parser);
12616 return add_stmt (stmt);
12619 /* OpenMP 4.0:
12620 # pragma omp target data target-data-clause[optseq] new-line
12621 structured-block */
12623 #define OMP_TARGET_DATA_CLAUSE_MASK \
12624 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
12625 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
12626 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
12628 static tree
12629 c_parser_omp_target_data (location_t loc, c_parser *parser)
12631 tree stmt = make_node (OMP_TARGET_DATA);
12632 TREE_TYPE (stmt) = void_type_node;
12634 OMP_TARGET_DATA_CLAUSES (stmt)
12635 = c_parser_omp_all_clauses (parser, OMP_TARGET_DATA_CLAUSE_MASK,
12636 "#pragma omp target data");
12637 keep_next_level ();
12638 tree block = c_begin_compound_stmt (true);
12639 add_stmt (c_parser_omp_structured_block (parser));
12640 OMP_TARGET_DATA_BODY (stmt) = c_end_compound_stmt (loc, block, true);
12642 SET_EXPR_LOCATION (stmt, loc);
12643 return add_stmt (stmt);
12646 /* OpenMP 4.0:
12647 # pragma omp target update target-update-clause[optseq] new-line */
12649 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
12650 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
12651 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
12652 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
12653 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
12655 static bool
12656 c_parser_omp_target_update (location_t loc, c_parser *parser,
12657 enum pragma_context context)
12659 if (context == pragma_stmt)
12661 error_at (loc,
12662 "%<#pragma omp target update%> may only be "
12663 "used in compound statements");
12664 c_parser_skip_to_pragma_eol (parser);
12665 return false;
12668 tree clauses
12669 = c_parser_omp_all_clauses (parser, OMP_TARGET_UPDATE_CLAUSE_MASK,
12670 "#pragma omp target update");
12671 if (find_omp_clause (clauses, OMP_CLAUSE_TO) == NULL_TREE
12672 && find_omp_clause (clauses, OMP_CLAUSE_FROM) == NULL_TREE)
12674 error_at (loc,
12675 "%<#pragma omp target update must contain at least one "
12676 "%<from%> or %<to%> clauses");
12677 return false;
12680 tree stmt = make_node (OMP_TARGET_UPDATE);
12681 TREE_TYPE (stmt) = void_type_node;
12682 OMP_TARGET_UPDATE_CLAUSES (stmt) = clauses;
12683 SET_EXPR_LOCATION (stmt, loc);
12684 add_stmt (stmt);
12685 return false;
12688 /* OpenMP 4.0:
12689 # pragma omp target target-clause[optseq] new-line
12690 structured-block */
12692 #define OMP_TARGET_CLAUSE_MASK \
12693 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
12694 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
12695 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
12697 static bool
12698 c_parser_omp_target (c_parser *parser, enum pragma_context context)
12700 location_t loc = c_parser_peek_token (parser)->location;
12701 c_parser_consume_pragma (parser);
12703 if (context != pragma_stmt && context != pragma_compound)
12705 c_parser_error (parser, "expected declaration specifiers");
12706 c_parser_skip_to_pragma_eol (parser);
12707 return false;
12710 if (c_parser_next_token_is (parser, CPP_NAME))
12712 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
12714 if (strcmp (p, "teams") == 0)
12716 tree cclauses[C_OMP_CLAUSE_SPLIT_COUNT];
12717 char p_name[sizeof ("#pragma omp target teams distribute "
12718 "parallel for simd")];
12720 c_parser_consume_token (parser);
12721 strcpy (p_name, "#pragma omp target");
12722 if (!flag_openmp) /* flag_openmp_simd */
12723 return c_parser_omp_teams (loc, parser, p_name,
12724 OMP_TARGET_CLAUSE_MASK, cclauses);
12725 keep_next_level ();
12726 tree block = c_begin_compound_stmt (true);
12727 tree ret = c_parser_omp_teams (loc, parser, p_name,
12728 OMP_TARGET_CLAUSE_MASK, cclauses);
12729 block = c_end_compound_stmt (loc, block, true);
12730 if (ret == NULL)
12731 return ret;
12732 tree stmt = make_node (OMP_TARGET);
12733 TREE_TYPE (stmt) = void_type_node;
12734 OMP_TARGET_CLAUSES (stmt) = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
12735 OMP_TARGET_BODY (stmt) = block;
12736 add_stmt (stmt);
12737 return true;
12739 else if (!flag_openmp) /* flag_openmp_simd */
12741 c_parser_skip_to_pragma_eol (parser);
12742 return NULL_TREE;
12744 else if (strcmp (p, "data") == 0)
12746 c_parser_consume_token (parser);
12747 c_parser_omp_target_data (loc, parser);
12748 return true;
12750 else if (strcmp (p, "update") == 0)
12752 c_parser_consume_token (parser);
12753 return c_parser_omp_target_update (loc, parser, context);
12757 tree stmt = make_node (OMP_TARGET);
12758 TREE_TYPE (stmt) = void_type_node;
12760 OMP_TARGET_CLAUSES (stmt)
12761 = c_parser_omp_all_clauses (parser, OMP_TARGET_CLAUSE_MASK,
12762 "#pragma omp target");
12763 keep_next_level ();
12764 tree block = c_begin_compound_stmt (true);
12765 add_stmt (c_parser_omp_structured_block (parser));
12766 OMP_TARGET_BODY (stmt) = c_end_compound_stmt (loc, block, true);
12768 SET_EXPR_LOCATION (stmt, loc);
12769 add_stmt (stmt);
12770 return true;
12773 /* OpenMP 4.0:
12774 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
12776 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
12777 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
12778 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
12779 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
12780 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
12781 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
12782 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
12784 static void
12785 c_parser_omp_declare_simd (c_parser *parser, enum pragma_context context)
12787 vec<c_token> clauses = vNULL;
12788 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
12790 c_token *token = c_parser_peek_token (parser);
12791 if (token->type == CPP_EOF)
12793 c_parser_skip_to_pragma_eol (parser);
12794 clauses.release ();
12795 return;
12797 clauses.safe_push (*token);
12798 c_parser_consume_token (parser);
12800 clauses.safe_push (*c_parser_peek_token (parser));
12801 c_parser_skip_to_pragma_eol (parser);
12803 while (c_parser_next_token_is (parser, CPP_PRAGMA))
12805 if (c_parser_peek_token (parser)->pragma_kind
12806 != PRAGMA_OMP_DECLARE_REDUCTION
12807 || c_parser_peek_2nd_token (parser)->type != CPP_NAME
12808 || strcmp (IDENTIFIER_POINTER
12809 (c_parser_peek_2nd_token (parser)->value),
12810 "simd") != 0)
12812 c_parser_error (parser,
12813 "%<#pragma omp declare simd%> must be followed by "
12814 "function declaration or definition or another "
12815 "%<#pragma omp declare simd%>");
12816 clauses.release ();
12817 return;
12819 c_parser_consume_pragma (parser);
12820 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
12822 c_token *token = c_parser_peek_token (parser);
12823 if (token->type == CPP_EOF)
12825 c_parser_skip_to_pragma_eol (parser);
12826 clauses.release ();
12827 return;
12829 clauses.safe_push (*token);
12830 c_parser_consume_token (parser);
12832 clauses.safe_push (*c_parser_peek_token (parser));
12833 c_parser_skip_to_pragma_eol (parser);
12836 /* Make sure nothing tries to read past the end of the tokens. */
12837 c_token eof_token;
12838 memset (&eof_token, 0, sizeof (eof_token));
12839 eof_token.type = CPP_EOF;
12840 clauses.safe_push (eof_token);
12841 clauses.safe_push (eof_token);
12843 switch (context)
12845 case pragma_external:
12846 if (c_parser_next_token_is (parser, CPP_KEYWORD)
12847 && c_parser_peek_token (parser)->keyword == RID_EXTENSION)
12849 int ext = disable_extension_diagnostics ();
12851 c_parser_consume_token (parser);
12852 while (c_parser_next_token_is (parser, CPP_KEYWORD)
12853 && c_parser_peek_token (parser)->keyword == RID_EXTENSION);
12854 c_parser_declaration_or_fndef (parser, true, true, true, false, true,
12855 NULL, clauses);
12856 restore_extension_diagnostics (ext);
12858 else
12859 c_parser_declaration_or_fndef (parser, true, true, true, false, true,
12860 NULL, clauses);
12861 break;
12862 case pragma_struct:
12863 case pragma_param:
12864 c_parser_error (parser, "%<#pragma omp declare simd%> must be followed by "
12865 "function declaration or definition");
12866 break;
12867 case pragma_compound:
12868 case pragma_stmt:
12869 if (c_parser_next_token_is (parser, CPP_KEYWORD)
12870 && c_parser_peek_token (parser)->keyword == RID_EXTENSION)
12872 int ext = disable_extension_diagnostics ();
12874 c_parser_consume_token (parser);
12875 while (c_parser_next_token_is (parser, CPP_KEYWORD)
12876 && c_parser_peek_token (parser)->keyword == RID_EXTENSION);
12877 if (c_parser_next_tokens_start_declaration (parser))
12879 c_parser_declaration_or_fndef (parser, true, true, true, true,
12880 true, NULL, clauses);
12881 restore_extension_diagnostics (ext);
12882 break;
12884 restore_extension_diagnostics (ext);
12886 else if (c_parser_next_tokens_start_declaration (parser))
12888 c_parser_declaration_or_fndef (parser, true, true, true, true, true,
12889 NULL, clauses);
12890 break;
12892 c_parser_error (parser, "%<#pragma omp declare simd%> must be followed by "
12893 "function declaration or definition");
12894 break;
12895 default:
12896 gcc_unreachable ();
12898 clauses.release ();
12901 /* Finalize #pragma omp declare simd clauses after FNDECL has been parsed,
12902 and put that into "omp declare simd" attribute. */
12904 static void
12905 c_finish_omp_declare_simd (c_parser *parser, tree fndecl, tree parms,
12906 vec<c_token> clauses)
12908 if (flag_cilkplus
12909 && clauses.exists () && !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
12911 error ("%<#pragma omp declare simd%> cannot be used in the same "
12912 "function marked as a Cilk Plus SIMD-enabled function");
12913 vec_free (parser->cilk_simd_fn_tokens);
12914 return;
12917 /* Normally first token is CPP_NAME "simd". CPP_EOF there indicates
12918 error has been reported and CPP_PRAGMA that c_finish_omp_declare_simd
12919 has already processed the tokens. */
12920 if (clauses.exists () && clauses[0].type == CPP_EOF)
12921 return;
12922 if (fndecl == NULL_TREE || TREE_CODE (fndecl) != FUNCTION_DECL)
12924 error ("%<#pragma omp declare simd%> not immediately followed by "
12925 "a function declaration or definition");
12926 clauses[0].type = CPP_EOF;
12927 return;
12929 if (clauses.exists () && clauses[0].type != CPP_NAME)
12931 error_at (DECL_SOURCE_LOCATION (fndecl),
12932 "%<#pragma omp declare simd%> not immediately followed by "
12933 "a single function declaration or definition");
12934 clauses[0].type = CPP_EOF;
12935 return;
12938 if (parms == NULL_TREE)
12939 parms = DECL_ARGUMENTS (fndecl);
12941 unsigned int tokens_avail = parser->tokens_avail;
12942 gcc_assert (parser->tokens == &parser->tokens_buf[0]);
12943 bool is_cilkplus_cilk_simd_fn = false;
12945 if (flag_cilkplus && !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
12947 parser->tokens = parser->cilk_simd_fn_tokens->address ();
12948 parser->tokens_avail = vec_safe_length (parser->cilk_simd_fn_tokens);
12949 is_cilkplus_cilk_simd_fn = true;
12951 else
12953 parser->tokens = clauses.address ();
12954 parser->tokens_avail = clauses.length ();
12957 /* c_parser_omp_declare_simd pushed 2 extra CPP_EOF tokens at the end. */
12958 while (parser->tokens_avail > 3)
12960 c_token *token = c_parser_peek_token (parser);
12961 if (!is_cilkplus_cilk_simd_fn)
12962 gcc_assert (token->type == CPP_NAME
12963 && strcmp (IDENTIFIER_POINTER (token->value), "simd") == 0);
12964 else
12965 gcc_assert (token->type == CPP_NAME
12966 && is_cilkplus_vector_p (token->value));
12967 c_parser_consume_token (parser);
12968 parser->in_pragma = true;
12970 tree c = NULL_TREE;
12971 if (is_cilkplus_cilk_simd_fn)
12972 c = c_parser_omp_all_clauses (parser, CILK_SIMD_FN_CLAUSE_MASK,
12973 "SIMD-enabled functions attribute");
12974 else
12975 c = c_parser_omp_all_clauses (parser, OMP_DECLARE_SIMD_CLAUSE_MASK,
12976 "#pragma omp declare simd");
12977 c = c_omp_declare_simd_clauses_to_numbers (parms, c);
12978 if (c != NULL_TREE)
12979 c = tree_cons (NULL_TREE, c, NULL_TREE);
12980 if (is_cilkplus_cilk_simd_fn)
12982 tree k = build_tree_list (get_identifier ("cilk simd function"),
12983 NULL_TREE);
12984 TREE_CHAIN (k) = DECL_ATTRIBUTES (fndecl);
12985 DECL_ATTRIBUTES (fndecl) = k;
12987 c = build_tree_list (get_identifier ("omp declare simd"), c);
12988 TREE_CHAIN (c) = DECL_ATTRIBUTES (fndecl);
12989 DECL_ATTRIBUTES (fndecl) = c;
12992 parser->tokens = &parser->tokens_buf[0];
12993 parser->tokens_avail = tokens_avail;
12994 if (clauses.exists ())
12995 clauses[0].type = CPP_PRAGMA;
12997 if (!vec_safe_is_empty (parser->cilk_simd_fn_tokens))
12998 vec_free (parser->cilk_simd_fn_tokens);
13002 /* OpenMP 4.0:
13003 # pragma omp declare target new-line
13004 declarations and definitions
13005 # pragma omp end declare target new-line */
13007 static void
13008 c_parser_omp_declare_target (c_parser *parser)
13010 c_parser_skip_to_pragma_eol (parser);
13011 current_omp_declare_target_attribute++;
13014 static void
13015 c_parser_omp_end_declare_target (c_parser *parser)
13017 location_t loc = c_parser_peek_token (parser)->location;
13018 c_parser_consume_pragma (parser);
13019 if (c_parser_next_token_is (parser, CPP_NAME)
13020 && strcmp (IDENTIFIER_POINTER (c_parser_peek_token (parser)->value),
13021 "declare") == 0)
13023 c_parser_consume_token (parser);
13024 if (c_parser_next_token_is (parser, CPP_NAME)
13025 && strcmp (IDENTIFIER_POINTER (c_parser_peek_token (parser)->value),
13026 "target") == 0)
13027 c_parser_consume_token (parser);
13028 else
13030 c_parser_error (parser, "expected %<target%>");
13031 c_parser_skip_to_pragma_eol (parser);
13032 return;
13035 else
13037 c_parser_error (parser, "expected %<declare%>");
13038 c_parser_skip_to_pragma_eol (parser);
13039 return;
13041 c_parser_skip_to_pragma_eol (parser);
13042 if (!current_omp_declare_target_attribute)
13043 error_at (loc, "%<#pragma omp end declare target%> without corresponding "
13044 "%<#pragma omp declare target%>");
13045 else
13046 current_omp_declare_target_attribute--;
13050 /* OpenMP 4.0
13051 #pragma omp declare reduction (reduction-id : typename-list : expression) \
13052 initializer-clause[opt] new-line
13054 initializer-clause:
13055 initializer (omp_priv = initializer)
13056 initializer (function-name (argument-list)) */
13058 static void
13059 c_parser_omp_declare_reduction (c_parser *parser, enum pragma_context context)
13061 unsigned int tokens_avail = 0, i;
13062 vec<tree> types = vNULL;
13063 vec<c_token> clauses = vNULL;
13064 enum tree_code reduc_code = ERROR_MARK;
13065 tree reduc_id = NULL_TREE;
13066 tree type;
13067 location_t rloc = c_parser_peek_token (parser)->location;
13069 if (context == pragma_struct || context == pragma_param)
13071 error ("%<#pragma omp declare reduction%> not at file or block scope");
13072 goto fail;
13075 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
13076 goto fail;
13078 switch (c_parser_peek_token (parser)->type)
13080 case CPP_PLUS:
13081 reduc_code = PLUS_EXPR;
13082 break;
13083 case CPP_MULT:
13084 reduc_code = MULT_EXPR;
13085 break;
13086 case CPP_MINUS:
13087 reduc_code = MINUS_EXPR;
13088 break;
13089 case CPP_AND:
13090 reduc_code = BIT_AND_EXPR;
13091 break;
13092 case CPP_XOR:
13093 reduc_code = BIT_XOR_EXPR;
13094 break;
13095 case CPP_OR:
13096 reduc_code = BIT_IOR_EXPR;
13097 break;
13098 case CPP_AND_AND:
13099 reduc_code = TRUTH_ANDIF_EXPR;
13100 break;
13101 case CPP_OR_OR:
13102 reduc_code = TRUTH_ORIF_EXPR;
13103 break;
13104 case CPP_NAME:
13105 const char *p;
13106 p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
13107 if (strcmp (p, "min") == 0)
13109 reduc_code = MIN_EXPR;
13110 break;
13112 if (strcmp (p, "max") == 0)
13114 reduc_code = MAX_EXPR;
13115 break;
13117 reduc_id = c_parser_peek_token (parser)->value;
13118 break;
13119 default:
13120 c_parser_error (parser,
13121 "expected %<+%>, %<*%>, %<-%>, %<&%>, "
13122 "%<^%>, %<|%>, %<&&%>, %<||%>, %<min%> or identifier");
13123 goto fail;
13126 tree orig_reduc_id, reduc_decl;
13127 orig_reduc_id = reduc_id;
13128 reduc_id = c_omp_reduction_id (reduc_code, reduc_id);
13129 reduc_decl = c_omp_reduction_decl (reduc_id);
13130 c_parser_consume_token (parser);
13132 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
13133 goto fail;
13135 while (true)
13137 location_t loc = c_parser_peek_token (parser)->location;
13138 struct c_type_name *ctype = c_parser_type_name (parser);
13139 if (ctype != NULL)
13141 type = groktypename (ctype, NULL, NULL);
13142 if (type == error_mark_node)
13144 else if ((INTEGRAL_TYPE_P (type)
13145 || TREE_CODE (type) == REAL_TYPE
13146 || TREE_CODE (type) == COMPLEX_TYPE)
13147 && orig_reduc_id == NULL_TREE)
13148 error_at (loc, "predeclared arithmetic type in "
13149 "%<#pragma omp declare reduction%>");
13150 else if (TREE_CODE (type) == FUNCTION_TYPE
13151 || TREE_CODE (type) == ARRAY_TYPE)
13152 error_at (loc, "function or array type in "
13153 "%<#pragma omp declare reduction%>");
13154 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
13155 error_at (loc, "const, volatile or restrict qualified type in "
13156 "%<#pragma omp declare reduction%>");
13157 else
13159 tree t;
13160 for (t = DECL_INITIAL (reduc_decl); t; t = TREE_CHAIN (t))
13161 if (comptypes (TREE_PURPOSE (t), type))
13163 error_at (loc, "redeclaration of %qs "
13164 "%<#pragma omp declare reduction%> for "
13165 "type %qT",
13166 IDENTIFIER_POINTER (reduc_id)
13167 + sizeof ("omp declare reduction ") - 1,
13168 type);
13169 location_t ploc
13170 = DECL_SOURCE_LOCATION (TREE_VEC_ELT (TREE_VALUE (t),
13171 0));
13172 error_at (ploc, "previous %<#pragma omp declare "
13173 "reduction%>");
13174 break;
13176 if (t == NULL_TREE)
13177 types.safe_push (type);
13179 if (c_parser_next_token_is (parser, CPP_COMMA))
13180 c_parser_consume_token (parser);
13181 else
13182 break;
13184 else
13185 break;
13188 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>")
13189 || types.is_empty ())
13191 fail:
13192 clauses.release ();
13193 types.release ();
13194 while (true)
13196 c_token *token = c_parser_peek_token (parser);
13197 if (token->type == CPP_EOF || token->type == CPP_PRAGMA_EOL)
13198 break;
13199 c_parser_consume_token (parser);
13201 c_parser_skip_to_pragma_eol (parser);
13202 return;
13205 if (types.length () > 1)
13207 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
13209 c_token *token = c_parser_peek_token (parser);
13210 if (token->type == CPP_EOF)
13211 goto fail;
13212 clauses.safe_push (*token);
13213 c_parser_consume_token (parser);
13215 clauses.safe_push (*c_parser_peek_token (parser));
13216 c_parser_skip_to_pragma_eol (parser);
13218 /* Make sure nothing tries to read past the end of the tokens. */
13219 c_token eof_token;
13220 memset (&eof_token, 0, sizeof (eof_token));
13221 eof_token.type = CPP_EOF;
13222 clauses.safe_push (eof_token);
13223 clauses.safe_push (eof_token);
13226 int errs = errorcount;
13227 FOR_EACH_VEC_ELT (types, i, type)
13229 tokens_avail = parser->tokens_avail;
13230 gcc_assert (parser->tokens == &parser->tokens_buf[0]);
13231 if (!clauses.is_empty ())
13233 parser->tokens = clauses.address ();
13234 parser->tokens_avail = clauses.length ();
13235 parser->in_pragma = true;
13238 bool nested = current_function_decl != NULL_TREE;
13239 if (nested)
13240 c_push_function_context ();
13241 tree fndecl = build_decl (BUILTINS_LOCATION, FUNCTION_DECL,
13242 reduc_id, default_function_type);
13243 current_function_decl = fndecl;
13244 allocate_struct_function (fndecl, true);
13245 push_scope ();
13246 tree stmt = push_stmt_list ();
13247 /* Intentionally BUILTINS_LOCATION, so that -Wshadow doesn't
13248 warn about these. */
13249 tree omp_out = build_decl (BUILTINS_LOCATION, VAR_DECL,
13250 get_identifier ("omp_out"), type);
13251 DECL_ARTIFICIAL (omp_out) = 1;
13252 DECL_CONTEXT (omp_out) = fndecl;
13253 pushdecl (omp_out);
13254 tree omp_in = build_decl (BUILTINS_LOCATION, VAR_DECL,
13255 get_identifier ("omp_in"), type);
13256 DECL_ARTIFICIAL (omp_in) = 1;
13257 DECL_CONTEXT (omp_in) = fndecl;
13258 pushdecl (omp_in);
13259 struct c_expr combiner = c_parser_expression (parser);
13260 struct c_expr initializer;
13261 tree omp_priv = NULL_TREE, omp_orig = NULL_TREE;
13262 bool bad = false;
13263 initializer.value = error_mark_node;
13264 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
13265 bad = true;
13266 else if (c_parser_next_token_is (parser, CPP_NAME)
13267 && strcmp (IDENTIFIER_POINTER
13268 (c_parser_peek_token (parser)->value),
13269 "initializer") == 0)
13271 c_parser_consume_token (parser);
13272 pop_scope ();
13273 push_scope ();
13274 omp_priv = build_decl (BUILTINS_LOCATION, VAR_DECL,
13275 get_identifier ("omp_priv"), type);
13276 DECL_ARTIFICIAL (omp_priv) = 1;
13277 DECL_INITIAL (omp_priv) = error_mark_node;
13278 DECL_CONTEXT (omp_priv) = fndecl;
13279 pushdecl (omp_priv);
13280 omp_orig = build_decl (BUILTINS_LOCATION, VAR_DECL,
13281 get_identifier ("omp_orig"), type);
13282 DECL_ARTIFICIAL (omp_orig) = 1;
13283 DECL_CONTEXT (omp_orig) = fndecl;
13284 pushdecl (omp_orig);
13285 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
13286 bad = true;
13287 else if (!c_parser_next_token_is (parser, CPP_NAME))
13289 c_parser_error (parser, "expected %<omp_priv%> or "
13290 "function-name");
13291 bad = true;
13293 else if (strcmp (IDENTIFIER_POINTER
13294 (c_parser_peek_token (parser)->value),
13295 "omp_priv") != 0)
13297 if (c_parser_peek_2nd_token (parser)->type != CPP_OPEN_PAREN
13298 || c_parser_peek_token (parser)->id_kind != C_ID_ID)
13300 c_parser_error (parser, "expected function-name %<(%>");
13301 bad = true;
13303 else
13304 initializer = c_parser_postfix_expression (parser);
13305 if (initializer.value
13306 && TREE_CODE (initializer.value) == CALL_EXPR)
13308 int j;
13309 tree c = initializer.value;
13310 for (j = 0; j < call_expr_nargs (c); j++)
13311 if (TREE_CODE (CALL_EXPR_ARG (c, j)) == ADDR_EXPR
13312 && TREE_OPERAND (CALL_EXPR_ARG (c, j), 0) == omp_priv)
13313 break;
13314 if (j == call_expr_nargs (c))
13315 error ("one of the initializer call arguments should be "
13316 "%<&omp_priv%>");
13319 else
13321 c_parser_consume_token (parser);
13322 if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
13323 bad = true;
13324 else
13326 tree st = push_stmt_list ();
13327 start_init (omp_priv, NULL_TREE, 0);
13328 location_t loc = c_parser_peek_token (parser)->location;
13329 struct c_expr init = c_parser_initializer (parser);
13330 finish_init ();
13331 finish_decl (omp_priv, loc, init.value,
13332 init.original_type, NULL_TREE);
13333 pop_stmt_list (st);
13336 if (!bad
13337 && !c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
13338 bad = true;
13341 if (!bad)
13343 c_parser_skip_to_pragma_eol (parser);
13345 tree t = tree_cons (type, make_tree_vec (omp_priv ? 6 : 3),
13346 DECL_INITIAL (reduc_decl));
13347 DECL_INITIAL (reduc_decl) = t;
13348 DECL_SOURCE_LOCATION (omp_out) = rloc;
13349 TREE_VEC_ELT (TREE_VALUE (t), 0) = omp_out;
13350 TREE_VEC_ELT (TREE_VALUE (t), 1) = omp_in;
13351 TREE_VEC_ELT (TREE_VALUE (t), 2) = combiner.value;
13352 walk_tree (&combiner.value, c_check_omp_declare_reduction_r,
13353 &TREE_VEC_ELT (TREE_VALUE (t), 0), NULL);
13354 if (omp_priv)
13356 DECL_SOURCE_LOCATION (omp_priv) = rloc;
13357 TREE_VEC_ELT (TREE_VALUE (t), 3) = omp_priv;
13358 TREE_VEC_ELT (TREE_VALUE (t), 4) = omp_orig;
13359 TREE_VEC_ELT (TREE_VALUE (t), 5) = initializer.value;
13360 walk_tree (&initializer.value, c_check_omp_declare_reduction_r,
13361 &TREE_VEC_ELT (TREE_VALUE (t), 3), NULL);
13362 walk_tree (&DECL_INITIAL (omp_priv),
13363 c_check_omp_declare_reduction_r,
13364 &TREE_VEC_ELT (TREE_VALUE (t), 3), NULL);
13368 pop_stmt_list (stmt);
13369 pop_scope ();
13370 if (cfun->language != NULL)
13372 ggc_free (cfun->language);
13373 cfun->language = NULL;
13375 set_cfun (NULL);
13376 current_function_decl = NULL_TREE;
13377 if (nested)
13378 c_pop_function_context ();
13380 if (!clauses.is_empty ())
13382 parser->tokens = &parser->tokens_buf[0];
13383 parser->tokens_avail = tokens_avail;
13385 if (bad)
13386 goto fail;
13387 if (errs != errorcount)
13388 break;
13391 clauses.release ();
13392 types.release ();
13396 /* OpenMP 4.0
13397 #pragma omp declare simd declare-simd-clauses[optseq] new-line
13398 #pragma omp declare reduction (reduction-id : typename-list : expression) \
13399 initializer-clause[opt] new-line
13400 #pragma omp declare target new-line */
13402 static void
13403 c_parser_omp_declare (c_parser *parser, enum pragma_context context)
13405 c_parser_consume_pragma (parser);
13406 if (c_parser_next_token_is (parser, CPP_NAME))
13408 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
13409 if (strcmp (p, "simd") == 0)
13411 /* c_parser_consume_token (parser); done in
13412 c_parser_omp_declare_simd. */
13413 c_parser_omp_declare_simd (parser, context);
13414 return;
13416 if (strcmp (p, "reduction") == 0)
13418 c_parser_consume_token (parser);
13419 c_parser_omp_declare_reduction (parser, context);
13420 return;
13422 if (!flag_openmp) /* flag_openmp_simd */
13424 c_parser_skip_to_pragma_eol (parser);
13425 return;
13427 if (strcmp (p, "target") == 0)
13429 c_parser_consume_token (parser);
13430 c_parser_omp_declare_target (parser);
13431 return;
13435 c_parser_error (parser, "expected %<simd%> or %<reduction%> "
13436 "or %<target%>");
13437 c_parser_skip_to_pragma_eol (parser);
13440 /* Main entry point to parsing most OpenMP pragmas. */
13442 static void
13443 c_parser_omp_construct (c_parser *parser)
13445 enum pragma_kind p_kind;
13446 location_t loc;
13447 tree stmt;
13448 char p_name[sizeof "#pragma omp teams distribute parallel for simd"];
13449 omp_clause_mask mask (0);
13451 loc = c_parser_peek_token (parser)->location;
13452 p_kind = c_parser_peek_token (parser)->pragma_kind;
13453 c_parser_consume_pragma (parser);
13455 switch (p_kind)
13457 case PRAGMA_OMP_ATOMIC:
13458 c_parser_omp_atomic (loc, parser);
13459 return;
13460 case PRAGMA_OMP_CRITICAL:
13461 stmt = c_parser_omp_critical (loc, parser);
13462 break;
13463 case PRAGMA_OMP_DISTRIBUTE:
13464 strcpy (p_name, "#pragma omp");
13465 stmt = c_parser_omp_distribute (loc, parser, p_name, mask, NULL);
13466 break;
13467 case PRAGMA_OMP_FOR:
13468 strcpy (p_name, "#pragma omp");
13469 stmt = c_parser_omp_for (loc, parser, p_name, mask, NULL);
13470 break;
13471 case PRAGMA_OMP_MASTER:
13472 stmt = c_parser_omp_master (loc, parser);
13473 break;
13474 case PRAGMA_OMP_ORDERED:
13475 stmt = c_parser_omp_ordered (loc, parser);
13476 break;
13477 case PRAGMA_OMP_PARALLEL:
13478 strcpy (p_name, "#pragma omp");
13479 stmt = c_parser_omp_parallel (loc, parser, p_name, mask, NULL);
13480 break;
13481 case PRAGMA_OMP_SECTIONS:
13482 strcpy (p_name, "#pragma omp");
13483 stmt = c_parser_omp_sections (loc, parser, p_name, mask, NULL);
13484 break;
13485 case PRAGMA_OMP_SIMD:
13486 strcpy (p_name, "#pragma omp");
13487 stmt = c_parser_omp_simd (loc, parser, p_name, mask, NULL);
13488 break;
13489 case PRAGMA_OMP_SINGLE:
13490 stmt = c_parser_omp_single (loc, parser);
13491 break;
13492 case PRAGMA_OMP_TASK:
13493 stmt = c_parser_omp_task (loc, parser);
13494 break;
13495 case PRAGMA_OMP_TASKGROUP:
13496 stmt = c_parser_omp_taskgroup (parser);
13497 break;
13498 case PRAGMA_OMP_TEAMS:
13499 strcpy (p_name, "#pragma omp");
13500 stmt = c_parser_omp_teams (loc, parser, p_name, mask, NULL);
13501 break;
13502 default:
13503 gcc_unreachable ();
13506 if (stmt)
13507 gcc_assert (EXPR_LOCATION (stmt) != UNKNOWN_LOCATION);
13511 /* OpenMP 2.5:
13512 # pragma omp threadprivate (variable-list) */
13514 static void
13515 c_parser_omp_threadprivate (c_parser *parser)
13517 tree vars, t;
13518 location_t loc;
13520 c_parser_consume_pragma (parser);
13521 loc = c_parser_peek_token (parser)->location;
13522 vars = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_ERROR, NULL);
13524 /* Mark every variable in VARS to be assigned thread local storage. */
13525 for (t = vars; t; t = TREE_CHAIN (t))
13527 tree v = TREE_PURPOSE (t);
13529 /* FIXME diagnostics: Ideally we should keep individual
13530 locations for all the variables in the var list to make the
13531 following errors more precise. Perhaps
13532 c_parser_omp_var_list_parens() should construct a list of
13533 locations to go along with the var list. */
13535 /* If V had already been marked threadprivate, it doesn't matter
13536 whether it had been used prior to this point. */
13537 if (TREE_CODE (v) != VAR_DECL)
13538 error_at (loc, "%qD is not a variable", v);
13539 else if (TREE_USED (v) && !C_DECL_THREADPRIVATE_P (v))
13540 error_at (loc, "%qE declared %<threadprivate%> after first use", v);
13541 else if (! TREE_STATIC (v) && ! DECL_EXTERNAL (v))
13542 error_at (loc, "automatic variable %qE cannot be %<threadprivate%>", v);
13543 else if (TREE_TYPE (v) == error_mark_node)
13545 else if (! COMPLETE_TYPE_P (TREE_TYPE (v)))
13546 error_at (loc, "%<threadprivate%> %qE has incomplete type", v);
13547 else
13549 if (! DECL_THREAD_LOCAL_P (v))
13551 DECL_TLS_MODEL (v) = decl_default_tls_model (v);
13552 /* If rtl has been already set for this var, call
13553 make_decl_rtl once again, so that encode_section_info
13554 has a chance to look at the new decl flags. */
13555 if (DECL_RTL_SET_P (v))
13556 make_decl_rtl (v);
13558 C_DECL_THREADPRIVATE_P (v) = 1;
13562 c_parser_skip_to_pragma_eol (parser);
13565 /* Cilk Plus <#pragma simd> parsing routines. */
13567 /* Helper function for c_parser_pragma. Perform some sanity checking
13568 for <#pragma simd> constructs. Returns FALSE if there was a
13569 problem. */
13571 static bool
13572 c_parser_cilk_verify_simd (c_parser *parser,
13573 enum pragma_context context)
13575 if (!flag_cilkplus)
13577 warning (0, "pragma simd ignored because -fcilkplus is not enabled");
13578 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
13579 return false;
13581 if (context == pragma_external)
13583 c_parser_error (parser,"pragma simd must be inside a function");
13584 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
13585 return false;
13587 return true;
13590 /* Cilk Plus:
13591 This function is shared by SIMD-enabled functions and #pragma simd.
13592 If IS_SIMD_FN is true then it is parsing a SIMD-enabled function and
13593 CLAUSES is unused. The main purpose of this function is to parse a
13594 vectorlength attribute or clause and check for parse errors.
13595 When IS_SIMD_FN is true then the function is merely caching the tokens
13596 in PARSER->CILK_SIMD_FN_TOKENS. If errors are found then the token
13597 cache is cleared since there is no reason to continue.
13598 Syntax:
13599 vectorlength ( constant-expression ) */
13601 static tree
13602 c_parser_cilk_clause_vectorlength (c_parser *parser, tree clauses,
13603 bool is_simd_fn)
13605 if (is_simd_fn)
13606 check_no_duplicate_clause (clauses, OMP_CLAUSE_SIMDLEN, "vectorlength");
13607 else
13608 /* The vectorlength clause behaves exactly like OpenMP's safelen
13609 clause. Represent it in OpenMP terms. */
13610 check_no_duplicate_clause (clauses, OMP_CLAUSE_SAFELEN, "vectorlength");
13612 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
13613 return clauses;
13615 location_t loc = c_parser_peek_token (parser)->location;
13616 tree expr = c_parser_expr_no_commas (parser, NULL).value;
13617 expr = c_fully_fold (expr, false, NULL);
13619 /* If expr is an error_mark_node then the above function would have
13620 emitted an error. No reason to do it twice. */
13621 if (expr == error_mark_node)
13623 else if (!TREE_TYPE (expr)
13624 || !TREE_CONSTANT (expr)
13625 || !INTEGRAL_TYPE_P (TREE_TYPE (expr)))
13627 error_at (loc, "vectorlength must be an integer constant");
13628 else if (wi::exact_log2 (expr) == -1)
13629 error_at (loc, "vectorlength must be a power of 2");
13630 else
13632 if (is_simd_fn)
13634 tree u = build_omp_clause (loc, OMP_CLAUSE_SIMDLEN);
13635 OMP_CLAUSE_SIMDLEN_EXPR (u) = expr;
13636 OMP_CLAUSE_CHAIN (u) = clauses;
13637 clauses = u;
13639 else
13641 tree u = build_omp_clause (loc, OMP_CLAUSE_SAFELEN);
13642 OMP_CLAUSE_SAFELEN_EXPR (u) = expr;
13643 OMP_CLAUSE_CHAIN (u) = clauses;
13644 clauses = u;
13648 c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
13650 return clauses;
13653 /* Cilk Plus:
13654 linear ( simd-linear-variable-list )
13656 simd-linear-variable-list:
13657 simd-linear-variable
13658 simd-linear-variable-list , simd-linear-variable
13660 simd-linear-variable:
13661 id-expression
13662 id-expression : simd-linear-step
13664 simd-linear-step:
13665 conditional-expression */
13667 static tree
13668 c_parser_cilk_clause_linear (c_parser *parser, tree clauses)
13670 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
13671 return clauses;
13673 location_t loc = c_parser_peek_token (parser)->location;
13675 if (c_parser_next_token_is_not (parser, CPP_NAME)
13676 || c_parser_peek_token (parser)->id_kind != C_ID_ID)
13677 c_parser_error (parser, "expected identifier");
13679 while (c_parser_next_token_is (parser, CPP_NAME)
13680 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
13682 tree var = lookup_name (c_parser_peek_token (parser)->value);
13684 if (var == NULL)
13686 undeclared_variable (c_parser_peek_token (parser)->location,
13687 c_parser_peek_token (parser)->value);
13688 c_parser_consume_token (parser);
13690 else if (var == error_mark_node)
13691 c_parser_consume_token (parser);
13692 else
13694 tree step = integer_one_node;
13696 /* Parse the linear step if present. */
13697 if (c_parser_peek_2nd_token (parser)->type == CPP_COLON)
13699 c_parser_consume_token (parser);
13700 c_parser_consume_token (parser);
13702 tree expr = c_parser_expr_no_commas (parser, NULL).value;
13703 expr = c_fully_fold (expr, false, NULL);
13705 if (TREE_TYPE (expr)
13706 && INTEGRAL_TYPE_P (TREE_TYPE (expr))
13707 && (TREE_CONSTANT (expr)
13708 || DECL_P (expr)))
13709 step = expr;
13710 else
13711 c_parser_error (parser,
13712 "step size must be an integer constant "
13713 "expression or an integer variable");
13715 else
13716 c_parser_consume_token (parser);
13718 /* Use OMP_CLAUSE_LINEAR, which has the same semantics. */
13719 tree u = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
13720 OMP_CLAUSE_DECL (u) = var;
13721 OMP_CLAUSE_LINEAR_STEP (u) = step;
13722 OMP_CLAUSE_CHAIN (u) = clauses;
13723 clauses = u;
13726 if (c_parser_next_token_is_not (parser, CPP_COMMA))
13727 break;
13729 c_parser_consume_token (parser);
13732 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
13734 return clauses;
13737 /* Returns the name of the next clause. If the clause is not
13738 recognized SIMD_OMP_CLAUSE_NONE is returned and the next token is
13739 not consumed. Otherwise, the appropriate pragma_simd_clause is
13740 returned and the token is consumed. */
13742 static pragma_omp_clause
13743 c_parser_cilk_clause_name (c_parser *parser)
13745 pragma_omp_clause result;
13746 c_token *token = c_parser_peek_token (parser);
13748 if (!token->value || token->type != CPP_NAME)
13749 return PRAGMA_CILK_CLAUSE_NONE;
13751 const char *p = IDENTIFIER_POINTER (token->value);
13753 if (!strcmp (p, "vectorlength"))
13754 result = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
13755 else if (!strcmp (p, "linear"))
13756 result = PRAGMA_CILK_CLAUSE_LINEAR;
13757 else if (!strcmp (p, "private"))
13758 result = PRAGMA_CILK_CLAUSE_PRIVATE;
13759 else if (!strcmp (p, "firstprivate"))
13760 result = PRAGMA_CILK_CLAUSE_FIRSTPRIVATE;
13761 else if (!strcmp (p, "lastprivate"))
13762 result = PRAGMA_CILK_CLAUSE_LASTPRIVATE;
13763 else if (!strcmp (p, "reduction"))
13764 result = PRAGMA_CILK_CLAUSE_REDUCTION;
13765 else
13766 return PRAGMA_CILK_CLAUSE_NONE;
13768 c_parser_consume_token (parser);
13769 return result;
13772 /* Parse all #<pragma simd> clauses. Return the list of clauses
13773 found. */
13775 static tree
13776 c_parser_cilk_all_clauses (c_parser *parser)
13778 tree clauses = NULL;
13780 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
13782 pragma_omp_clause c_kind;
13784 c_kind = c_parser_cilk_clause_name (parser);
13786 switch (c_kind)
13788 case PRAGMA_CILK_CLAUSE_VECTORLENGTH:
13789 clauses = c_parser_cilk_clause_vectorlength (parser, clauses, false);
13790 break;
13791 case PRAGMA_CILK_CLAUSE_LINEAR:
13792 clauses = c_parser_cilk_clause_linear (parser, clauses);
13793 break;
13794 case PRAGMA_CILK_CLAUSE_PRIVATE:
13795 /* Use the OpenMP counterpart. */
13796 clauses = c_parser_omp_clause_private (parser, clauses);
13797 break;
13798 case PRAGMA_CILK_CLAUSE_FIRSTPRIVATE:
13799 /* Use the OpenMP counterpart. */
13800 clauses = c_parser_omp_clause_firstprivate (parser, clauses);
13801 break;
13802 case PRAGMA_CILK_CLAUSE_LASTPRIVATE:
13803 /* Use the OpenMP counterpart. */
13804 clauses = c_parser_omp_clause_lastprivate (parser, clauses);
13805 break;
13806 case PRAGMA_CILK_CLAUSE_REDUCTION:
13807 /* Use the OpenMP counterpart. */
13808 clauses = c_parser_omp_clause_reduction (parser, clauses);
13809 break;
13810 default:
13811 c_parser_error (parser, "expected %<#pragma simd%> clause");
13812 goto saw_error;
13816 saw_error:
13817 c_parser_skip_to_pragma_eol (parser);
13818 return c_finish_cilk_clauses (clauses);
13821 /* Main entry point for parsing Cilk Plus <#pragma simd> for
13822 loops. */
13824 static void
13825 c_parser_cilk_simd (c_parser *parser)
13827 tree clauses = c_parser_cilk_all_clauses (parser);
13828 tree block = c_begin_compound_stmt (true);
13829 location_t loc = c_parser_peek_token (parser)->location;
13830 c_parser_omp_for_loop (loc, parser, CILK_SIMD, clauses, NULL);
13831 block = c_end_compound_stmt (loc, block, true);
13832 add_stmt (block);
13835 /* Parse a transaction attribute (GCC Extension).
13837 transaction-attribute:
13838 attributes
13839 [ [ any-word ] ]
13841 The transactional memory language description is written for C++,
13842 and uses the C++0x attribute syntax. For compatibility, allow the
13843 bracket style for transactions in C as well. */
13845 static tree
13846 c_parser_transaction_attributes (c_parser *parser)
13848 tree attr_name, attr = NULL;
13850 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
13851 return c_parser_attributes (parser);
13853 if (!c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
13854 return NULL_TREE;
13855 c_parser_consume_token (parser);
13856 if (!c_parser_require (parser, CPP_OPEN_SQUARE, "expected %<[%>"))
13857 goto error1;
13859 attr_name = c_parser_attribute_any_word (parser);
13860 if (attr_name)
13862 c_parser_consume_token (parser);
13863 attr = build_tree_list (attr_name, NULL_TREE);
13865 else
13866 c_parser_error (parser, "expected identifier");
13868 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, "expected %<]%>");
13869 error1:
13870 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, "expected %<]%>");
13871 return attr;
13874 /* Parse a __transaction_atomic or __transaction_relaxed statement
13875 (GCC Extension).
13877 transaction-statement:
13878 __transaction_atomic transaction-attribute[opt] compound-statement
13879 __transaction_relaxed compound-statement
13881 Note that the only valid attribute is: "outer".
13884 static tree
13885 c_parser_transaction (c_parser *parser, enum rid keyword)
13887 unsigned int old_in = parser->in_transaction;
13888 unsigned int this_in = 1, new_in;
13889 location_t loc = c_parser_peek_token (parser)->location;
13890 tree stmt, attrs;
13892 gcc_assert ((keyword == RID_TRANSACTION_ATOMIC
13893 || keyword == RID_TRANSACTION_RELAXED)
13894 && c_parser_next_token_is_keyword (parser, keyword));
13895 c_parser_consume_token (parser);
13897 if (keyword == RID_TRANSACTION_RELAXED)
13898 this_in |= TM_STMT_ATTR_RELAXED;
13899 else
13901 attrs = c_parser_transaction_attributes (parser);
13902 if (attrs)
13903 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
13906 /* Keep track if we're in the lexical scope of an outer transaction. */
13907 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
13909 parser->in_transaction = new_in;
13910 stmt = c_parser_compound_statement (parser);
13911 parser->in_transaction = old_in;
13913 if (flag_tm)
13914 stmt = c_finish_transaction (loc, stmt, this_in);
13915 else
13916 error_at (loc, (keyword == RID_TRANSACTION_ATOMIC ?
13917 "%<__transaction_atomic%> without transactional memory support enabled"
13918 : "%<__transaction_relaxed %> "
13919 "without transactional memory support enabled"));
13921 return stmt;
13924 /* Parse a __transaction_atomic or __transaction_relaxed expression
13925 (GCC Extension).
13927 transaction-expression:
13928 __transaction_atomic ( expression )
13929 __transaction_relaxed ( expression )
13932 static struct c_expr
13933 c_parser_transaction_expression (c_parser *parser, enum rid keyword)
13935 struct c_expr ret;
13936 unsigned int old_in = parser->in_transaction;
13937 unsigned int this_in = 1;
13938 location_t loc = c_parser_peek_token (parser)->location;
13939 tree attrs;
13941 gcc_assert ((keyword == RID_TRANSACTION_ATOMIC
13942 || keyword == RID_TRANSACTION_RELAXED)
13943 && c_parser_next_token_is_keyword (parser, keyword));
13944 c_parser_consume_token (parser);
13946 if (keyword == RID_TRANSACTION_RELAXED)
13947 this_in |= TM_STMT_ATTR_RELAXED;
13948 else
13950 attrs = c_parser_transaction_attributes (parser);
13951 if (attrs)
13952 this_in |= parse_tm_stmt_attr (attrs, 0);
13955 parser->in_transaction = this_in;
13956 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
13958 tree expr = c_parser_expression (parser).value;
13959 ret.original_type = TREE_TYPE (expr);
13960 ret.value = build1 (TRANSACTION_EXPR, ret.original_type, expr);
13961 if (this_in & TM_STMT_ATTR_RELAXED)
13962 TRANSACTION_EXPR_RELAXED (ret.value) = 1;
13963 SET_EXPR_LOCATION (ret.value, loc);
13964 ret.original_code = TRANSACTION_EXPR;
13965 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
13967 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
13968 goto error;
13971 else
13973 error:
13974 ret.value = error_mark_node;
13975 ret.original_code = ERROR_MARK;
13976 ret.original_type = NULL;
13978 parser->in_transaction = old_in;
13980 if (!flag_tm)
13981 error_at (loc, (keyword == RID_TRANSACTION_ATOMIC ?
13982 "%<__transaction_atomic%> without transactional memory support enabled"
13983 : "%<__transaction_relaxed %> "
13984 "without transactional memory support enabled"));
13986 return ret;
13989 /* Parse a __transaction_cancel statement (GCC Extension).
13991 transaction-cancel-statement:
13992 __transaction_cancel transaction-attribute[opt] ;
13994 Note that the only valid attribute is "outer".
13997 static tree
13998 c_parser_transaction_cancel (c_parser *parser)
14000 location_t loc = c_parser_peek_token (parser)->location;
14001 tree attrs;
14002 bool is_outer = false;
14004 gcc_assert (c_parser_next_token_is_keyword (parser, RID_TRANSACTION_CANCEL));
14005 c_parser_consume_token (parser);
14007 attrs = c_parser_transaction_attributes (parser);
14008 if (attrs)
14009 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
14011 if (!flag_tm)
14013 error_at (loc, "%<__transaction_cancel%> without "
14014 "transactional memory support enabled");
14015 goto ret_error;
14017 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
14019 error_at (loc, "%<__transaction_cancel%> within a "
14020 "%<__transaction_relaxed%>");
14021 goto ret_error;
14023 else if (is_outer)
14025 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
14026 && !is_tm_may_cancel_outer (current_function_decl))
14028 error_at (loc, "outer %<__transaction_cancel%> not "
14029 "within outer %<__transaction_atomic%>");
14030 error_at (loc, " or a %<transaction_may_cancel_outer%> function");
14031 goto ret_error;
14034 else if (parser->in_transaction == 0)
14036 error_at (loc, "%<__transaction_cancel%> not within "
14037 "%<__transaction_atomic%>");
14038 goto ret_error;
14041 return add_stmt (build_tm_abort_call (loc, is_outer));
14043 ret_error:
14044 return build1 (NOP_EXPR, void_type_node, error_mark_node);
14047 /* Parse a single source file. */
14049 void
14050 c_parse_file (void)
14052 /* Use local storage to begin. If the first token is a pragma, parse it.
14053 If it is #pragma GCC pch_preprocess, then this will load a PCH file
14054 which will cause garbage collection. */
14055 c_parser tparser;
14057 memset (&tparser, 0, sizeof tparser);
14058 tparser.tokens = &tparser.tokens_buf[0];
14059 the_parser = &tparser;
14061 if (c_parser_peek_token (&tparser)->pragma_kind == PRAGMA_GCC_PCH_PREPROCESS)
14062 c_parser_pragma_pch_preprocess (&tparser);
14064 the_parser = ggc_alloc<c_parser> ();
14065 *the_parser = tparser;
14066 if (tparser.tokens == &tparser.tokens_buf[0])
14067 the_parser->tokens = &the_parser->tokens_buf[0];
14069 /* Initialize EH, if we've been told to do so. */
14070 if (flag_exceptions)
14071 using_eh_for_cleanups ();
14073 c_parser_translation_unit (the_parser);
14074 the_parser = NULL;
14077 /* This function parses Cilk Plus array notation. The starting index is
14078 passed in INITIAL_INDEX and the array name is passes in ARRAY_VALUE. The
14079 return value of this function is a tree_node called VALUE_TREE of type
14080 ARRAY_NOTATION_REF. */
14082 static tree
14083 c_parser_array_notation (location_t loc, c_parser *parser, tree initial_index,
14084 tree array_value)
14086 c_token *token = NULL;
14087 tree start_index = NULL_TREE, end_index = NULL_TREE, stride = NULL_TREE;
14088 tree value_tree = NULL_TREE, type = NULL_TREE, array_type = NULL_TREE;
14089 tree array_type_domain = NULL_TREE;
14091 if (array_value == error_mark_node)
14093 /* No need to continue. If either of these 2 were true, then an error
14094 must be emitted already. Thus, no need to emit them twice. */
14095 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
14096 return error_mark_node;
14099 array_type = TREE_TYPE (array_value);
14100 gcc_assert (array_type);
14101 type = TREE_TYPE (array_type);
14102 token = c_parser_peek_token (parser);
14104 if (token->type == CPP_EOF)
14106 c_parser_error (parser, "expected %<:%> or numeral");
14107 return value_tree;
14109 else if (token->type == CPP_COLON)
14111 if (!initial_index)
14113 /* If we are here, then we have a case like this A[:]. */
14114 c_parser_consume_token (parser);
14115 if (TREE_CODE (array_type) == POINTER_TYPE)
14117 error_at (loc, "start-index and length fields necessary for "
14118 "using array notations in pointers");
14119 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
14120 return error_mark_node;
14122 if (TREE_CODE (array_type) == FUNCTION_TYPE)
14124 error_at (loc, "array notations cannot be used with function "
14125 "type");
14126 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
14127 return error_mark_node;
14129 array_type_domain = TYPE_DOMAIN (array_type);
14131 if (!array_type_domain)
14133 error_at (loc, "start-index and length fields necessary for "
14134 "using array notations in dimensionless arrays");
14135 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
14136 return error_mark_node;
14139 start_index = TYPE_MINVAL (array_type_domain);
14140 start_index = fold_build1 (CONVERT_EXPR, ptrdiff_type_node,
14141 start_index);
14142 if (!TYPE_MAXVAL (array_type_domain)
14143 || !TREE_CONSTANT (TYPE_MAXVAL (array_type_domain)))
14145 error_at (loc, "start-index and length fields necessary for "
14146 "using array notations in variable-length arrays");
14147 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
14148 return error_mark_node;
14150 end_index = TYPE_MAXVAL (array_type_domain);
14151 end_index = fold_build2 (PLUS_EXPR, TREE_TYPE (end_index),
14152 end_index, integer_one_node);
14153 end_index = fold_build1 (CONVERT_EXPR, ptrdiff_type_node, end_index);
14154 stride = build_int_cst (integer_type_node, 1);
14155 stride = fold_build1 (CONVERT_EXPR, ptrdiff_type_node, stride);
14157 else if (initial_index != error_mark_node)
14159 /* If we are here, then there should be 2 possibilities:
14160 1. Array [EXPR : EXPR]
14161 2. Array [EXPR : EXPR : EXPR]
14163 start_index = initial_index;
14165 if (TREE_CODE (array_type) == FUNCTION_TYPE)
14167 error_at (loc, "array notations cannot be used with function "
14168 "type");
14169 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
14170 return error_mark_node;
14172 c_parser_consume_token (parser); /* consume the ':' */
14173 struct c_expr ce = c_parser_expression (parser);
14174 ce = convert_lvalue_to_rvalue (loc, ce, false, false);
14175 end_index = ce.value;
14176 if (!end_index || end_index == error_mark_node)
14178 c_parser_skip_to_end_of_block_or_statement (parser);
14179 return error_mark_node;
14181 if (c_parser_peek_token (parser)->type == CPP_COLON)
14183 c_parser_consume_token (parser);
14184 ce = c_parser_expression (parser);
14185 ce = convert_lvalue_to_rvalue (loc, ce, false, false);
14186 stride = ce.value;
14187 if (!stride || stride == error_mark_node)
14189 c_parser_skip_to_end_of_block_or_statement (parser);
14190 return error_mark_node;
14194 else
14195 c_parser_error (parser, "expected array notation expression");
14197 else
14198 c_parser_error (parser, "expected array notation expression");
14200 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, "expected %<]%>");
14202 value_tree = build_array_notation_ref (loc, array_value, start_index,
14203 end_index, stride, type);
14204 if (value_tree != error_mark_node)
14205 SET_EXPR_LOCATION (value_tree, loc);
14206 return value_tree;
14209 #include "gt-c-c-parser.h"