PR middle-end/57541
[official-gcc.git] / gcc / c / c-parser.c
blob99e7fc8694c2afedc82e7e46c20e65c1125604c7
1 /* Parser for C and Objective-C.
2 Copyright (C) 1987-2014 Free Software Foundation, Inc.
4 Parser actions based on the old Bison parser; structure somewhat
5 influenced by and fragments based on the C++ parser.
7 This file is part of GCC.
9 GCC is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 3, or (at your option) any later
12 version.
14 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 for more details.
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3. If not see
21 <http://www.gnu.org/licenses/>. */
23 /* TODO:
25 Make sure all relevant comments, and all relevant code from all
26 actions, brought over from old parser. Verify exact correspondence
27 of syntax accepted.
29 Add testcases covering every input symbol in every state in old and
30 new parsers.
32 Include full syntax for GNU C, including erroneous cases accepted
33 with error messages, in syntax productions in comments.
35 Make more diagnostics in the front end generally take an explicit
36 location rather than implicitly using input_location. */
38 #include "config.h"
39 #include "system.h"
40 #include "coretypes.h"
41 #include "tm.h" /* For rtl.h: needs enum reg_class. */
42 #include "tree.h"
43 #include "stringpool.h"
44 #include "attribs.h"
45 #include "stor-layout.h"
46 #include "varasm.h"
47 #include "trans-mem.h"
48 #include "langhooks.h"
49 #include "input.h"
50 #include "cpplib.h"
51 #include "timevar.h"
52 #include "c-family/c-pragma.h"
53 #include "c-tree.h"
54 #include "c-lang.h"
55 #include "flags.h"
56 #include "ggc.h"
57 #include "c-family/c-common.h"
58 #include "c-family/c-objc.h"
59 #include "vec.h"
60 #include "target.h"
61 #include "cgraph.h"
62 #include "plugin.h"
63 #include "omp-low.h"
64 #include "builtins.h"
67 /* Initialization routine for this file. */
69 void
70 c_parse_init (void)
72 /* The only initialization required is of the reserved word
73 identifiers. */
74 unsigned int i;
75 tree id;
76 int mask = 0;
78 /* Make sure RID_MAX hasn't grown past the 8 bits used to hold the keyword in
79 the c_token structure. */
80 gcc_assert (RID_MAX <= 255);
82 mask |= D_CXXONLY;
83 if (!flag_isoc99)
84 mask |= D_C99;
85 if (flag_no_asm)
87 mask |= D_ASM | D_EXT;
88 if (!flag_isoc99)
89 mask |= D_EXT89;
91 if (!c_dialect_objc ())
92 mask |= D_OBJC | D_CXX_OBJC;
94 ridpointers = ggc_cleared_vec_alloc<tree> ((int) RID_MAX);
95 for (i = 0; i < num_c_common_reswords; i++)
97 /* If a keyword is disabled, do not enter it into the table
98 and so create a canonical spelling that isn't a keyword. */
99 if (c_common_reswords[i].disable & mask)
101 if (warn_cxx_compat
102 && (c_common_reswords[i].disable & D_CXXWARN))
104 id = get_identifier (c_common_reswords[i].word);
105 C_SET_RID_CODE (id, RID_CXX_COMPAT_WARN);
106 C_IS_RESERVED_WORD (id) = 1;
108 continue;
111 id = get_identifier (c_common_reswords[i].word);
112 C_SET_RID_CODE (id, c_common_reswords[i].rid);
113 C_IS_RESERVED_WORD (id) = 1;
114 ridpointers [(int) c_common_reswords[i].rid] = id;
118 /* The C lexer intermediates between the lexer in cpplib and c-lex.c
119 and the C parser. Unlike the C++ lexer, the parser structure
120 stores the lexer information instead of using a separate structure.
121 Identifiers are separated into ordinary identifiers, type names,
122 keywords and some other Objective-C types of identifiers, and some
123 look-ahead is maintained.
125 ??? It might be a good idea to lex the whole file up front (as for
126 C++). It would then be possible to share more of the C and C++
127 lexer code, if desired. */
129 /* The following local token type is used. */
131 /* A keyword. */
132 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
134 /* More information about the type of a CPP_NAME token. */
135 typedef enum c_id_kind {
136 /* An ordinary identifier. */
137 C_ID_ID,
138 /* An identifier declared as a typedef name. */
139 C_ID_TYPENAME,
140 /* An identifier declared as an Objective-C class name. */
141 C_ID_CLASSNAME,
142 /* An address space identifier. */
143 C_ID_ADDRSPACE,
144 /* Not an identifier. */
145 C_ID_NONE
146 } c_id_kind;
148 /* A single C token after string literal concatenation and conversion
149 of preprocessing tokens to tokens. */
150 typedef struct GTY (()) c_token {
151 /* The kind of token. */
152 ENUM_BITFIELD (cpp_ttype) type : 8;
153 /* If this token is a CPP_NAME, this value indicates whether also
154 declared as some kind of type. Otherwise, it is C_ID_NONE. */
155 ENUM_BITFIELD (c_id_kind) id_kind : 8;
156 /* If this token is a keyword, this value indicates which keyword.
157 Otherwise, this value is RID_MAX. */
158 ENUM_BITFIELD (rid) keyword : 8;
159 /* If this token is a CPP_PRAGMA, this indicates the pragma that
160 was seen. Otherwise it is PRAGMA_NONE. */
161 ENUM_BITFIELD (pragma_kind) pragma_kind : 8;
162 /* The location at which this token was found. */
163 location_t location;
164 /* The value associated with this token, if any. */
165 tree value;
166 } c_token;
168 /* A parser structure recording information about the state and
169 context of parsing. Includes lexer information with up to two
170 tokens of look-ahead; more are not needed for C. */
171 typedef struct GTY(()) c_parser {
172 /* The look-ahead tokens. */
173 c_token * GTY((skip)) tokens;
174 /* Buffer for look-ahead tokens. */
175 c_token tokens_buf[2];
176 /* How many look-ahead tokens are available (0, 1 or 2, or
177 more if parsing from pre-lexed tokens). */
178 unsigned int tokens_avail;
179 /* True if a syntax error is being recovered from; false otherwise.
180 c_parser_error sets this flag. It should clear this flag when
181 enough tokens have been consumed to recover from the error. */
182 BOOL_BITFIELD error : 1;
183 /* True if we're processing a pragma, and shouldn't automatically
184 consume CPP_PRAGMA_EOL. */
185 BOOL_BITFIELD in_pragma : 1;
186 /* True if we're parsing the outermost block of an if statement. */
187 BOOL_BITFIELD in_if_block : 1;
188 /* True if we want to lex an untranslated string. */
189 BOOL_BITFIELD lex_untranslated_string : 1;
191 /* Objective-C specific parser/lexer information. */
193 /* True if we are in a context where the Objective-C "PQ" keywords
194 are considered keywords. */
195 BOOL_BITFIELD objc_pq_context : 1;
196 /* True if we are parsing a (potential) Objective-C foreach
197 statement. This is set to true after we parsed 'for (' and while
198 we wait for 'in' or ';' to decide if it's a standard C for loop or an
199 Objective-C foreach loop. */
200 BOOL_BITFIELD objc_could_be_foreach_context : 1;
201 /* The following flag is needed to contextualize Objective-C lexical
202 analysis. In some cases (e.g., 'int NSObject;'), it is
203 undesirable to bind an identifier to an Objective-C class, even
204 if a class with that name exists. */
205 BOOL_BITFIELD objc_need_raw_identifier : 1;
206 /* Nonzero if we're processing a __transaction statement. The value
207 is 1 | TM_STMT_ATTR_*. */
208 unsigned int in_transaction : 4;
209 /* True if we are in a context where the Objective-C "Property attribute"
210 keywords are valid. */
211 BOOL_BITFIELD objc_property_attr_context : 1;
213 /* Cilk Plus specific parser/lexer information. */
215 /* Buffer to hold all the tokens from parsing the vector attribute for the
216 SIMD-enabled functions (formerly known as elemental functions). */
217 vec <c_token, va_gc> *cilk_simd_fn_tokens;
218 } c_parser;
221 /* The actual parser and external interface. ??? Does this need to be
222 garbage-collected? */
224 static GTY (()) c_parser *the_parser;
226 /* Read in and lex a single token, storing it in *TOKEN. */
228 static void
229 c_lex_one_token (c_parser *parser, c_token *token)
231 timevar_push (TV_LEX);
233 token->type = c_lex_with_flags (&token->value, &token->location, NULL,
234 (parser->lex_untranslated_string
235 ? C_LEX_STRING_NO_TRANSLATE : 0));
236 token->id_kind = C_ID_NONE;
237 token->keyword = RID_MAX;
238 token->pragma_kind = PRAGMA_NONE;
240 switch (token->type)
242 case CPP_NAME:
244 tree decl;
246 bool objc_force_identifier = parser->objc_need_raw_identifier;
247 if (c_dialect_objc ())
248 parser->objc_need_raw_identifier = false;
250 if (C_IS_RESERVED_WORD (token->value))
252 enum rid rid_code = C_RID_CODE (token->value);
254 if (rid_code == RID_CXX_COMPAT_WARN)
256 warning_at (token->location,
257 OPT_Wc___compat,
258 "identifier %qE conflicts with C++ keyword",
259 token->value);
261 else if (rid_code >= RID_FIRST_ADDR_SPACE
262 && rid_code <= RID_LAST_ADDR_SPACE)
264 token->id_kind = C_ID_ADDRSPACE;
265 token->keyword = rid_code;
266 break;
268 else if (c_dialect_objc () && OBJC_IS_PQ_KEYWORD (rid_code))
270 /* We found an Objective-C "pq" keyword (in, out,
271 inout, bycopy, byref, oneway). They need special
272 care because the interpretation depends on the
273 context. */
274 if (parser->objc_pq_context)
276 token->type = CPP_KEYWORD;
277 token->keyword = rid_code;
278 break;
280 else if (parser->objc_could_be_foreach_context
281 && rid_code == RID_IN)
283 /* We are in Objective-C, inside a (potential)
284 foreach context (which means after having
285 parsed 'for (', but before having parsed ';'),
286 and we found 'in'. We consider it the keyword
287 which terminates the declaration at the
288 beginning of a foreach-statement. Note that
289 this means you can't use 'in' for anything else
290 in that context; in particular, in Objective-C
291 you can't use 'in' as the name of the running
292 variable in a C for loop. We could potentially
293 try to add code here to disambiguate, but it
294 seems a reasonable limitation. */
295 token->type = CPP_KEYWORD;
296 token->keyword = rid_code;
297 break;
299 /* Else, "pq" keywords outside of the "pq" context are
300 not keywords, and we fall through to the code for
301 normal tokens. */
303 else if (c_dialect_objc () && OBJC_IS_PATTR_KEYWORD (rid_code))
305 /* We found an Objective-C "property attribute"
306 keyword (getter, setter, readonly, etc). These are
307 only valid in the property context. */
308 if (parser->objc_property_attr_context)
310 token->type = CPP_KEYWORD;
311 token->keyword = rid_code;
312 break;
314 /* Else they are not special keywords.
317 else if (c_dialect_objc ()
318 && (OBJC_IS_AT_KEYWORD (rid_code)
319 || OBJC_IS_CXX_KEYWORD (rid_code)))
321 /* We found one of the Objective-C "@" keywords (defs,
322 selector, synchronized, etc) or one of the
323 Objective-C "cxx" keywords (class, private,
324 protected, public, try, catch, throw) without a
325 preceding '@' sign. Do nothing and fall through to
326 the code for normal tokens (in C++ we would still
327 consider the CXX ones keywords, but not in C). */
330 else
332 token->type = CPP_KEYWORD;
333 token->keyword = rid_code;
334 break;
338 decl = lookup_name (token->value);
339 if (decl)
341 if (TREE_CODE (decl) == TYPE_DECL)
343 token->id_kind = C_ID_TYPENAME;
344 break;
347 else if (c_dialect_objc ())
349 tree objc_interface_decl = objc_is_class_name (token->value);
350 /* Objective-C class names are in the same namespace as
351 variables and typedefs, and hence are shadowed by local
352 declarations. */
353 if (objc_interface_decl
354 && (!objc_force_identifier || global_bindings_p ()))
356 token->value = objc_interface_decl;
357 token->id_kind = C_ID_CLASSNAME;
358 break;
361 token->id_kind = C_ID_ID;
363 break;
364 case CPP_AT_NAME:
365 /* This only happens in Objective-C; it must be a keyword. */
366 token->type = CPP_KEYWORD;
367 switch (C_RID_CODE (token->value))
369 /* Replace 'class' with '@class', 'private' with '@private',
370 etc. This prevents confusion with the C++ keyword
371 'class', and makes the tokens consistent with other
372 Objective-C 'AT' keywords. For example '@class' is
373 reported as RID_AT_CLASS which is consistent with
374 '@synchronized', which is reported as
375 RID_AT_SYNCHRONIZED.
377 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
378 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
379 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
380 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
381 case RID_THROW: token->keyword = RID_AT_THROW; break;
382 case RID_TRY: token->keyword = RID_AT_TRY; break;
383 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
384 default: token->keyword = C_RID_CODE (token->value);
386 break;
387 case CPP_COLON:
388 case CPP_COMMA:
389 case CPP_CLOSE_PAREN:
390 case CPP_SEMICOLON:
391 /* These tokens may affect the interpretation of any identifiers
392 following, if doing Objective-C. */
393 if (c_dialect_objc ())
394 parser->objc_need_raw_identifier = false;
395 break;
396 case CPP_PRAGMA:
397 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
398 token->pragma_kind = (enum pragma_kind) TREE_INT_CST_LOW (token->value);
399 token->value = NULL;
400 break;
401 default:
402 break;
404 timevar_pop (TV_LEX);
407 /* Return a pointer to the next token from PARSER, reading it in if
408 necessary. */
410 static inline c_token *
411 c_parser_peek_token (c_parser *parser)
413 if (parser->tokens_avail == 0)
415 c_lex_one_token (parser, &parser->tokens[0]);
416 parser->tokens_avail = 1;
418 return &parser->tokens[0];
421 /* Return true if the next token from PARSER has the indicated
422 TYPE. */
424 static inline bool
425 c_parser_next_token_is (c_parser *parser, enum cpp_ttype type)
427 return c_parser_peek_token (parser)->type == type;
430 /* Return true if the next token from PARSER does not have the
431 indicated TYPE. */
433 static inline bool
434 c_parser_next_token_is_not (c_parser *parser, enum cpp_ttype type)
436 return !c_parser_next_token_is (parser, type);
439 /* Return true if the next token from PARSER is the indicated
440 KEYWORD. */
442 static inline bool
443 c_parser_next_token_is_keyword (c_parser *parser, enum rid keyword)
445 return c_parser_peek_token (parser)->keyword == keyword;
448 /* Return a pointer to the next-but-one token from PARSER, reading it
449 in if necessary. The next token is already read in. */
451 static c_token *
452 c_parser_peek_2nd_token (c_parser *parser)
454 if (parser->tokens_avail >= 2)
455 return &parser->tokens[1];
456 gcc_assert (parser->tokens_avail == 1);
457 gcc_assert (parser->tokens[0].type != CPP_EOF);
458 gcc_assert (parser->tokens[0].type != CPP_PRAGMA_EOL);
459 c_lex_one_token (parser, &parser->tokens[1]);
460 parser->tokens_avail = 2;
461 return &parser->tokens[1];
464 /* Return true if TOKEN can start a type name,
465 false otherwise. */
466 static bool
467 c_token_starts_typename (c_token *token)
469 switch (token->type)
471 case CPP_NAME:
472 switch (token->id_kind)
474 case C_ID_ID:
475 return false;
476 case C_ID_ADDRSPACE:
477 return true;
478 case C_ID_TYPENAME:
479 return true;
480 case C_ID_CLASSNAME:
481 gcc_assert (c_dialect_objc ());
482 return true;
483 default:
484 gcc_unreachable ();
486 case CPP_KEYWORD:
487 switch (token->keyword)
489 case RID_UNSIGNED:
490 case RID_LONG:
491 case RID_INT128:
492 case RID_SHORT:
493 case RID_SIGNED:
494 case RID_COMPLEX:
495 case RID_INT:
496 case RID_CHAR:
497 case RID_FLOAT:
498 case RID_DOUBLE:
499 case RID_VOID:
500 case RID_DFLOAT32:
501 case RID_DFLOAT64:
502 case RID_DFLOAT128:
503 case RID_BOOL:
504 case RID_ENUM:
505 case RID_STRUCT:
506 case RID_UNION:
507 case RID_TYPEOF:
508 case RID_CONST:
509 case RID_ATOMIC:
510 case RID_VOLATILE:
511 case RID_RESTRICT:
512 case RID_ATTRIBUTE:
513 case RID_FRACT:
514 case RID_ACCUM:
515 case RID_SAT:
516 case RID_AUTO_TYPE:
517 return true;
518 default:
519 return false;
521 case CPP_LESS:
522 if (c_dialect_objc ())
523 return true;
524 return false;
525 default:
526 return false;
530 enum c_lookahead_kind {
531 /* Always treat unknown identifiers as typenames. */
532 cla_prefer_type,
534 /* Could be parsing a nonabstract declarator. Only treat an identifier
535 as a typename if followed by another identifier or a star. */
536 cla_nonabstract_decl,
538 /* Never treat identifiers as typenames. */
539 cla_prefer_id
542 /* Return true if the next token from PARSER can start a type name,
543 false otherwise. LA specifies how to do lookahead in order to
544 detect unknown type names. If unsure, pick CLA_PREFER_ID. */
546 static inline bool
547 c_parser_next_tokens_start_typename (c_parser *parser, enum c_lookahead_kind la)
549 c_token *token = c_parser_peek_token (parser);
550 if (c_token_starts_typename (token))
551 return true;
553 /* Try a bit harder to detect an unknown typename. */
554 if (la != cla_prefer_id
555 && token->type == CPP_NAME
556 && token->id_kind == C_ID_ID
558 /* Do not try too hard when we could have "object in array". */
559 && !parser->objc_could_be_foreach_context
561 && (la == cla_prefer_type
562 || c_parser_peek_2nd_token (parser)->type == CPP_NAME
563 || c_parser_peek_2nd_token (parser)->type == CPP_MULT)
565 /* Only unknown identifiers. */
566 && !lookup_name (token->value))
567 return true;
569 return false;
572 /* Return true if TOKEN is a type qualifier, false otherwise. */
573 static bool
574 c_token_is_qualifier (c_token *token)
576 switch (token->type)
578 case CPP_NAME:
579 switch (token->id_kind)
581 case C_ID_ADDRSPACE:
582 return true;
583 default:
584 return false;
586 case CPP_KEYWORD:
587 switch (token->keyword)
589 case RID_CONST:
590 case RID_VOLATILE:
591 case RID_RESTRICT:
592 case RID_ATTRIBUTE:
593 case RID_ATOMIC:
594 return true;
595 default:
596 return false;
598 case CPP_LESS:
599 return false;
600 default:
601 gcc_unreachable ();
605 /* Return true if the next token from PARSER is a type qualifier,
606 false otherwise. */
607 static inline bool
608 c_parser_next_token_is_qualifier (c_parser *parser)
610 c_token *token = c_parser_peek_token (parser);
611 return c_token_is_qualifier (token);
614 /* Return true if TOKEN can start declaration specifiers, false
615 otherwise. */
616 static bool
617 c_token_starts_declspecs (c_token *token)
619 switch (token->type)
621 case CPP_NAME:
622 switch (token->id_kind)
624 case C_ID_ID:
625 return false;
626 case C_ID_ADDRSPACE:
627 return true;
628 case C_ID_TYPENAME:
629 return true;
630 case C_ID_CLASSNAME:
631 gcc_assert (c_dialect_objc ());
632 return true;
633 default:
634 gcc_unreachable ();
636 case CPP_KEYWORD:
637 switch (token->keyword)
639 case RID_STATIC:
640 case RID_EXTERN:
641 case RID_REGISTER:
642 case RID_TYPEDEF:
643 case RID_INLINE:
644 case RID_NORETURN:
645 case RID_AUTO:
646 case RID_THREAD:
647 case RID_UNSIGNED:
648 case RID_LONG:
649 case RID_INT128:
650 case RID_SHORT:
651 case RID_SIGNED:
652 case RID_COMPLEX:
653 case RID_INT:
654 case RID_CHAR:
655 case RID_FLOAT:
656 case RID_DOUBLE:
657 case RID_VOID:
658 case RID_DFLOAT32:
659 case RID_DFLOAT64:
660 case RID_DFLOAT128:
661 case RID_BOOL:
662 case RID_ENUM:
663 case RID_STRUCT:
664 case RID_UNION:
665 case RID_TYPEOF:
666 case RID_CONST:
667 case RID_VOLATILE:
668 case RID_RESTRICT:
669 case RID_ATTRIBUTE:
670 case RID_FRACT:
671 case RID_ACCUM:
672 case RID_SAT:
673 case RID_ALIGNAS:
674 case RID_ATOMIC:
675 case RID_AUTO_TYPE:
676 return true;
677 default:
678 return false;
680 case CPP_LESS:
681 if (c_dialect_objc ())
682 return true;
683 return false;
684 default:
685 return false;
690 /* Return true if TOKEN can start declaration specifiers or a static
691 assertion, false otherwise. */
692 static bool
693 c_token_starts_declaration (c_token *token)
695 if (c_token_starts_declspecs (token)
696 || token->keyword == RID_STATIC_ASSERT)
697 return true;
698 else
699 return false;
702 /* Return true if the next token from PARSER can start declaration
703 specifiers, false otherwise. */
704 static inline bool
705 c_parser_next_token_starts_declspecs (c_parser *parser)
707 c_token *token = c_parser_peek_token (parser);
709 /* In Objective-C, a classname normally starts a declspecs unless it
710 is immediately followed by a dot. In that case, it is the
711 Objective-C 2.0 "dot-syntax" for class objects, ie, calls the
712 setter/getter on the class. c_token_starts_declspecs() can't
713 differentiate between the two cases because it only checks the
714 current token, so we have a special check here. */
715 if (c_dialect_objc ()
716 && token->type == CPP_NAME
717 && token->id_kind == C_ID_CLASSNAME
718 && c_parser_peek_2nd_token (parser)->type == CPP_DOT)
719 return false;
721 return c_token_starts_declspecs (token);
724 /* Return true if the next tokens from PARSER can start declaration
725 specifiers or a static assertion, false otherwise. */
726 static inline bool
727 c_parser_next_tokens_start_declaration (c_parser *parser)
729 c_token *token = c_parser_peek_token (parser);
731 /* Same as above. */
732 if (c_dialect_objc ()
733 && token->type == CPP_NAME
734 && token->id_kind == C_ID_CLASSNAME
735 && c_parser_peek_2nd_token (parser)->type == CPP_DOT)
736 return false;
738 /* Labels do not start declarations. */
739 if (token->type == CPP_NAME
740 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
741 return false;
743 if (c_token_starts_declaration (token))
744 return true;
746 if (c_parser_next_tokens_start_typename (parser, cla_nonabstract_decl))
747 return true;
749 return false;
752 /* Consume the next token from PARSER. */
754 static void
755 c_parser_consume_token (c_parser *parser)
757 gcc_assert (parser->tokens_avail >= 1);
758 gcc_assert (parser->tokens[0].type != CPP_EOF);
759 gcc_assert (!parser->in_pragma || parser->tokens[0].type != CPP_PRAGMA_EOL);
760 gcc_assert (parser->error || parser->tokens[0].type != CPP_PRAGMA);
761 if (parser->tokens != &parser->tokens_buf[0])
762 parser->tokens++;
763 else if (parser->tokens_avail == 2)
764 parser->tokens[0] = parser->tokens[1];
765 parser->tokens_avail--;
768 /* Expect the current token to be a #pragma. Consume it and remember
769 that we've begun parsing a pragma. */
771 static void
772 c_parser_consume_pragma (c_parser *parser)
774 gcc_assert (!parser->in_pragma);
775 gcc_assert (parser->tokens_avail >= 1);
776 gcc_assert (parser->tokens[0].type == CPP_PRAGMA);
777 if (parser->tokens != &parser->tokens_buf[0])
778 parser->tokens++;
779 else if (parser->tokens_avail == 2)
780 parser->tokens[0] = parser->tokens[1];
781 parser->tokens_avail--;
782 parser->in_pragma = true;
785 /* Update the global input_location from TOKEN. */
786 static inline void
787 c_parser_set_source_position_from_token (c_token *token)
789 if (token->type != CPP_EOF)
791 input_location = token->location;
795 /* Issue a diagnostic of the form
796 FILE:LINE: MESSAGE before TOKEN
797 where TOKEN is the next token in the input stream of PARSER.
798 MESSAGE (specified by the caller) is usually of the form "expected
799 OTHER-TOKEN".
801 Do not issue a diagnostic if still recovering from an error.
803 ??? This is taken from the C++ parser, but building up messages in
804 this way is not i18n-friendly and some other approach should be
805 used. */
807 static void
808 c_parser_error (c_parser *parser, const char *gmsgid)
810 c_token *token = c_parser_peek_token (parser);
811 if (parser->error)
812 return;
813 parser->error = true;
814 if (!gmsgid)
815 return;
816 /* This diagnostic makes more sense if it is tagged to the line of
817 the token we just peeked at. */
818 c_parser_set_source_position_from_token (token);
819 c_parse_error (gmsgid,
820 /* Because c_parse_error does not understand
821 CPP_KEYWORD, keywords are treated like
822 identifiers. */
823 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
824 /* ??? The C parser does not save the cpp flags of a
825 token, we need to pass 0 here and we will not get
826 the source spelling of some tokens but rather the
827 canonical spelling. */
828 token->value, /*flags=*/0);
831 /* If the next token is of the indicated TYPE, consume it. Otherwise,
832 issue the error MSGID. If MSGID is NULL then a message has already
833 been produced and no message will be produced this time. Returns
834 true if found, false otherwise. */
836 static bool
837 c_parser_require (c_parser *parser,
838 enum cpp_ttype type,
839 const char *msgid)
841 if (c_parser_next_token_is (parser, type))
843 c_parser_consume_token (parser);
844 return true;
846 else
848 c_parser_error (parser, msgid);
849 return false;
853 /* If the next token is the indicated keyword, consume it. Otherwise,
854 issue the error MSGID. Returns true if found, false otherwise. */
856 static bool
857 c_parser_require_keyword (c_parser *parser,
858 enum rid keyword,
859 const char *msgid)
861 if (c_parser_next_token_is_keyword (parser, keyword))
863 c_parser_consume_token (parser);
864 return true;
866 else
868 c_parser_error (parser, msgid);
869 return false;
873 /* Like c_parser_require, except that tokens will be skipped until the
874 desired token is found. An error message is still produced if the
875 next token is not as expected. If MSGID is NULL then a message has
876 already been produced and no message will be produced this
877 time. */
879 static void
880 c_parser_skip_until_found (c_parser *parser,
881 enum cpp_ttype type,
882 const char *msgid)
884 unsigned nesting_depth = 0;
886 if (c_parser_require (parser, type, msgid))
887 return;
889 /* Skip tokens until the desired token is found. */
890 while (true)
892 /* Peek at the next token. */
893 c_token *token = c_parser_peek_token (parser);
894 /* If we've reached the token we want, consume it and stop. */
895 if (token->type == type && !nesting_depth)
897 c_parser_consume_token (parser);
898 break;
901 /* If we've run out of tokens, stop. */
902 if (token->type == CPP_EOF)
903 return;
904 if (token->type == CPP_PRAGMA_EOL && parser->in_pragma)
905 return;
906 if (token->type == CPP_OPEN_BRACE
907 || token->type == CPP_OPEN_PAREN
908 || token->type == CPP_OPEN_SQUARE)
909 ++nesting_depth;
910 else if (token->type == CPP_CLOSE_BRACE
911 || token->type == CPP_CLOSE_PAREN
912 || token->type == CPP_CLOSE_SQUARE)
914 if (nesting_depth-- == 0)
915 break;
917 /* Consume this token. */
918 c_parser_consume_token (parser);
920 parser->error = false;
923 /* Skip tokens until the end of a parameter is found, but do not
924 consume the comma, semicolon or closing delimiter. */
926 static void
927 c_parser_skip_to_end_of_parameter (c_parser *parser)
929 unsigned nesting_depth = 0;
931 while (true)
933 c_token *token = c_parser_peek_token (parser);
934 if ((token->type == CPP_COMMA || token->type == CPP_SEMICOLON)
935 && !nesting_depth)
936 break;
937 /* If we've run out of tokens, stop. */
938 if (token->type == CPP_EOF)
939 return;
940 if (token->type == CPP_PRAGMA_EOL && parser->in_pragma)
941 return;
942 if (token->type == CPP_OPEN_BRACE
943 || token->type == CPP_OPEN_PAREN
944 || token->type == CPP_OPEN_SQUARE)
945 ++nesting_depth;
946 else if (token->type == CPP_CLOSE_BRACE
947 || token->type == CPP_CLOSE_PAREN
948 || token->type == CPP_CLOSE_SQUARE)
950 if (nesting_depth-- == 0)
951 break;
953 /* Consume this token. */
954 c_parser_consume_token (parser);
956 parser->error = false;
959 /* Expect to be at the end of the pragma directive and consume an
960 end of line marker. */
962 static void
963 c_parser_skip_to_pragma_eol (c_parser *parser)
965 gcc_assert (parser->in_pragma);
966 parser->in_pragma = false;
968 if (!c_parser_require (parser, CPP_PRAGMA_EOL, "expected end of line"))
969 while (true)
971 c_token *token = c_parser_peek_token (parser);
972 if (token->type == CPP_EOF)
973 break;
974 if (token->type == CPP_PRAGMA_EOL)
976 c_parser_consume_token (parser);
977 break;
979 c_parser_consume_token (parser);
982 parser->error = false;
985 /* Skip tokens until we have consumed an entire block, or until we
986 have consumed a non-nested ';'. */
988 static void
989 c_parser_skip_to_end_of_block_or_statement (c_parser *parser)
991 unsigned nesting_depth = 0;
992 bool save_error = parser->error;
994 while (true)
996 c_token *token;
998 /* Peek at the next token. */
999 token = c_parser_peek_token (parser);
1001 switch (token->type)
1003 case CPP_EOF:
1004 return;
1006 case CPP_PRAGMA_EOL:
1007 if (parser->in_pragma)
1008 return;
1009 break;
1011 case CPP_SEMICOLON:
1012 /* If the next token is a ';', we have reached the
1013 end of the statement. */
1014 if (!nesting_depth)
1016 /* Consume the ';'. */
1017 c_parser_consume_token (parser);
1018 goto finished;
1020 break;
1022 case CPP_CLOSE_BRACE:
1023 /* If the next token is a non-nested '}', then we have
1024 reached the end of the current block. */
1025 if (nesting_depth == 0 || --nesting_depth == 0)
1027 c_parser_consume_token (parser);
1028 goto finished;
1030 break;
1032 case CPP_OPEN_BRACE:
1033 /* If it the next token is a '{', then we are entering a new
1034 block. Consume the entire block. */
1035 ++nesting_depth;
1036 break;
1038 case CPP_PRAGMA:
1039 /* If we see a pragma, consume the whole thing at once. We
1040 have some safeguards against consuming pragmas willy-nilly.
1041 Normally, we'd expect to be here with parser->error set,
1042 which disables these safeguards. But it's possible to get
1043 here for secondary error recovery, after parser->error has
1044 been cleared. */
1045 c_parser_consume_pragma (parser);
1046 c_parser_skip_to_pragma_eol (parser);
1047 parser->error = save_error;
1048 continue;
1050 default:
1051 break;
1054 c_parser_consume_token (parser);
1057 finished:
1058 parser->error = false;
1061 /* CPP's options (initialized by c-opts.c). */
1062 extern cpp_options *cpp_opts;
1064 /* Save the warning flags which are controlled by __extension__. */
1066 static inline int
1067 disable_extension_diagnostics (void)
1069 int ret = (pedantic
1070 | (warn_pointer_arith << 1)
1071 | (warn_traditional << 2)
1072 | (flag_iso << 3)
1073 | (warn_long_long << 4)
1074 | (warn_cxx_compat << 5)
1075 | (warn_overlength_strings << 6));
1076 cpp_opts->cpp_pedantic = pedantic = 0;
1077 warn_pointer_arith = 0;
1078 cpp_opts->cpp_warn_traditional = warn_traditional = 0;
1079 flag_iso = 0;
1080 cpp_opts->cpp_warn_long_long = warn_long_long = 0;
1081 warn_cxx_compat = 0;
1082 warn_overlength_strings = 0;
1083 return ret;
1086 /* Restore the warning flags which are controlled by __extension__.
1087 FLAGS is the return value from disable_extension_diagnostics. */
1089 static inline void
1090 restore_extension_diagnostics (int flags)
1092 cpp_opts->cpp_pedantic = pedantic = flags & 1;
1093 warn_pointer_arith = (flags >> 1) & 1;
1094 cpp_opts->cpp_warn_traditional = warn_traditional = (flags >> 2) & 1;
1095 flag_iso = (flags >> 3) & 1;
1096 cpp_opts->cpp_warn_long_long = warn_long_long = (flags >> 4) & 1;
1097 warn_cxx_compat = (flags >> 5) & 1;
1098 warn_overlength_strings = (flags >> 6) & 1;
1101 /* Possibly kinds of declarator to parse. */
1102 typedef enum c_dtr_syn {
1103 /* A normal declarator with an identifier. */
1104 C_DTR_NORMAL,
1105 /* An abstract declarator (maybe empty). */
1106 C_DTR_ABSTRACT,
1107 /* A parameter declarator: may be either, but after a type name does
1108 not redeclare a typedef name as an identifier if it can
1109 alternatively be interpreted as a typedef name; see DR#009,
1110 applied in C90 TC1, omitted from C99 and reapplied in C99 TC2
1111 following DR#249. For example, given a typedef T, "int T" and
1112 "int *T" are valid parameter declarations redeclaring T, while
1113 "int (T)" and "int * (T)" and "int (T[])" and "int (T (int))" are
1114 abstract declarators rather than involving redundant parentheses;
1115 the same applies with attributes inside the parentheses before
1116 "T". */
1117 C_DTR_PARM
1118 } c_dtr_syn;
1120 /* The binary operation precedence levels, where 0 is a dummy lowest level
1121 used for the bottom of the stack. */
1122 enum c_parser_prec {
1123 PREC_NONE,
1124 PREC_LOGOR,
1125 PREC_LOGAND,
1126 PREC_BITOR,
1127 PREC_BITXOR,
1128 PREC_BITAND,
1129 PREC_EQ,
1130 PREC_REL,
1131 PREC_SHIFT,
1132 PREC_ADD,
1133 PREC_MULT,
1134 NUM_PRECS
1137 static void c_parser_external_declaration (c_parser *);
1138 static void c_parser_asm_definition (c_parser *);
1139 static void c_parser_declaration_or_fndef (c_parser *, bool, bool, bool,
1140 bool, bool, tree *, vec<c_token>);
1141 static void c_parser_static_assert_declaration_no_semi (c_parser *);
1142 static void c_parser_static_assert_declaration (c_parser *);
1143 static void c_parser_declspecs (c_parser *, struct c_declspecs *, bool, bool,
1144 bool, bool, bool, enum c_lookahead_kind);
1145 static struct c_typespec c_parser_enum_specifier (c_parser *);
1146 static struct c_typespec c_parser_struct_or_union_specifier (c_parser *);
1147 static tree c_parser_struct_declaration (c_parser *);
1148 static struct c_typespec c_parser_typeof_specifier (c_parser *);
1149 static tree c_parser_alignas_specifier (c_parser *);
1150 static struct c_declarator *c_parser_declarator (c_parser *, bool, c_dtr_syn,
1151 bool *);
1152 static struct c_declarator *c_parser_direct_declarator (c_parser *, bool,
1153 c_dtr_syn, bool *);
1154 static struct c_declarator *c_parser_direct_declarator_inner (c_parser *,
1155 bool,
1156 struct c_declarator *);
1157 static struct c_arg_info *c_parser_parms_declarator (c_parser *, bool, tree);
1158 static struct c_arg_info *c_parser_parms_list_declarator (c_parser *, tree,
1159 tree);
1160 static struct c_parm *c_parser_parameter_declaration (c_parser *, tree);
1161 static tree c_parser_simple_asm_expr (c_parser *);
1162 static tree c_parser_attributes (c_parser *);
1163 static struct c_type_name *c_parser_type_name (c_parser *);
1164 static struct c_expr c_parser_initializer (c_parser *);
1165 static struct c_expr c_parser_braced_init (c_parser *, tree, bool);
1166 static void c_parser_initelt (c_parser *, struct obstack *);
1167 static void c_parser_initval (c_parser *, struct c_expr *,
1168 struct obstack *);
1169 static tree c_parser_compound_statement (c_parser *);
1170 static void c_parser_compound_statement_nostart (c_parser *);
1171 static void c_parser_label (c_parser *);
1172 static void c_parser_statement (c_parser *);
1173 static void c_parser_statement_after_labels (c_parser *);
1174 static void c_parser_if_statement (c_parser *);
1175 static void c_parser_switch_statement (c_parser *);
1176 static void c_parser_while_statement (c_parser *, bool);
1177 static void c_parser_do_statement (c_parser *, bool);
1178 static void c_parser_for_statement (c_parser *, bool);
1179 static tree c_parser_asm_statement (c_parser *);
1180 static tree c_parser_asm_operands (c_parser *);
1181 static tree c_parser_asm_goto_operands (c_parser *);
1182 static tree c_parser_asm_clobbers (c_parser *);
1183 static struct c_expr c_parser_expr_no_commas (c_parser *, struct c_expr *,
1184 tree = NULL_TREE);
1185 static struct c_expr c_parser_conditional_expression (c_parser *,
1186 struct c_expr *, tree);
1187 static struct c_expr c_parser_binary_expression (c_parser *, struct c_expr *,
1188 tree);
1189 static struct c_expr c_parser_cast_expression (c_parser *, struct c_expr *);
1190 static struct c_expr c_parser_unary_expression (c_parser *);
1191 static struct c_expr c_parser_sizeof_expression (c_parser *);
1192 static struct c_expr c_parser_alignof_expression (c_parser *);
1193 static struct c_expr c_parser_postfix_expression (c_parser *);
1194 static struct c_expr c_parser_postfix_expression_after_paren_type (c_parser *,
1195 struct c_type_name *,
1196 location_t);
1197 static struct c_expr c_parser_postfix_expression_after_primary (c_parser *,
1198 location_t loc,
1199 struct c_expr);
1200 static tree c_parser_transaction (c_parser *, enum rid);
1201 static struct c_expr c_parser_transaction_expression (c_parser *, enum rid);
1202 static tree c_parser_transaction_cancel (c_parser *);
1203 static struct c_expr c_parser_expression (c_parser *);
1204 static struct c_expr c_parser_expression_conv (c_parser *);
1205 static vec<tree, va_gc> *c_parser_expr_list (c_parser *, bool, bool,
1206 vec<tree, va_gc> **, location_t *,
1207 tree *, vec<location_t> *);
1208 static void c_parser_omp_construct (c_parser *);
1209 static void c_parser_omp_threadprivate (c_parser *);
1210 static void c_parser_omp_barrier (c_parser *);
1211 static void c_parser_omp_flush (c_parser *);
1212 static void c_parser_omp_taskwait (c_parser *);
1213 static void c_parser_omp_taskyield (c_parser *);
1214 static void c_parser_omp_cancel (c_parser *);
1215 static void c_parser_omp_cancellation_point (c_parser *);
1217 enum pragma_context { pragma_external, pragma_struct, pragma_param,
1218 pragma_stmt, pragma_compound };
1219 static bool c_parser_pragma (c_parser *, enum pragma_context);
1220 static bool c_parser_omp_target (c_parser *, enum pragma_context);
1221 static void c_parser_omp_end_declare_target (c_parser *);
1222 static void c_parser_omp_declare (c_parser *, enum pragma_context);
1224 /* These Objective-C parser functions are only ever called when
1225 compiling Objective-C. */
1226 static void c_parser_objc_class_definition (c_parser *, tree);
1227 static void c_parser_objc_class_instance_variables (c_parser *);
1228 static void c_parser_objc_class_declaration (c_parser *);
1229 static void c_parser_objc_alias_declaration (c_parser *);
1230 static void c_parser_objc_protocol_definition (c_parser *, tree);
1231 static bool c_parser_objc_method_type (c_parser *);
1232 static void c_parser_objc_method_definition (c_parser *);
1233 static void c_parser_objc_methodprotolist (c_parser *);
1234 static void c_parser_objc_methodproto (c_parser *);
1235 static tree c_parser_objc_method_decl (c_parser *, bool, tree *, tree *);
1236 static tree c_parser_objc_type_name (c_parser *);
1237 static tree c_parser_objc_protocol_refs (c_parser *);
1238 static void c_parser_objc_try_catch_finally_statement (c_parser *);
1239 static void c_parser_objc_synchronized_statement (c_parser *);
1240 static tree c_parser_objc_selector (c_parser *);
1241 static tree c_parser_objc_selector_arg (c_parser *);
1242 static tree c_parser_objc_receiver (c_parser *);
1243 static tree c_parser_objc_message_args (c_parser *);
1244 static tree c_parser_objc_keywordexpr (c_parser *);
1245 static void c_parser_objc_at_property_declaration (c_parser *);
1246 static void c_parser_objc_at_synthesize_declaration (c_parser *);
1247 static void c_parser_objc_at_dynamic_declaration (c_parser *);
1248 static bool c_parser_objc_diagnose_bad_element_prefix
1249 (c_parser *, struct c_declspecs *);
1251 /* Cilk Plus supporting routines. */
1252 static void c_parser_cilk_simd (c_parser *);
1253 static bool c_parser_cilk_verify_simd (c_parser *, enum pragma_context);
1254 static tree c_parser_array_notation (location_t, c_parser *, tree, tree);
1255 static tree c_parser_cilk_clause_vectorlength (c_parser *, tree, bool);
1257 /* Parse a translation unit (C90 6.7, C99 6.9).
1259 translation-unit:
1260 external-declarations
1262 external-declarations:
1263 external-declaration
1264 external-declarations external-declaration
1266 GNU extensions:
1268 translation-unit:
1269 empty
1272 static void
1273 c_parser_translation_unit (c_parser *parser)
1275 if (c_parser_next_token_is (parser, CPP_EOF))
1277 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
1278 "ISO C forbids an empty translation unit");
1280 else
1282 void *obstack_position = obstack_alloc (&parser_obstack, 0);
1283 mark_valid_location_for_stdc_pragma (false);
1286 ggc_collect ();
1287 c_parser_external_declaration (parser);
1288 obstack_free (&parser_obstack, obstack_position);
1290 while (c_parser_next_token_is_not (parser, CPP_EOF));
1294 /* Parse an external declaration (C90 6.7, C99 6.9).
1296 external-declaration:
1297 function-definition
1298 declaration
1300 GNU extensions:
1302 external-declaration:
1303 asm-definition
1305 __extension__ external-declaration
1307 Objective-C:
1309 external-declaration:
1310 objc-class-definition
1311 objc-class-declaration
1312 objc-alias-declaration
1313 objc-protocol-definition
1314 objc-method-definition
1315 @end
1318 static void
1319 c_parser_external_declaration (c_parser *parser)
1321 int ext;
1322 switch (c_parser_peek_token (parser)->type)
1324 case CPP_KEYWORD:
1325 switch (c_parser_peek_token (parser)->keyword)
1327 case RID_EXTENSION:
1328 ext = disable_extension_diagnostics ();
1329 c_parser_consume_token (parser);
1330 c_parser_external_declaration (parser);
1331 restore_extension_diagnostics (ext);
1332 break;
1333 case RID_ASM:
1334 c_parser_asm_definition (parser);
1335 break;
1336 case RID_AT_INTERFACE:
1337 case RID_AT_IMPLEMENTATION:
1338 gcc_assert (c_dialect_objc ());
1339 c_parser_objc_class_definition (parser, NULL_TREE);
1340 break;
1341 case RID_AT_CLASS:
1342 gcc_assert (c_dialect_objc ());
1343 c_parser_objc_class_declaration (parser);
1344 break;
1345 case RID_AT_ALIAS:
1346 gcc_assert (c_dialect_objc ());
1347 c_parser_objc_alias_declaration (parser);
1348 break;
1349 case RID_AT_PROTOCOL:
1350 gcc_assert (c_dialect_objc ());
1351 c_parser_objc_protocol_definition (parser, NULL_TREE);
1352 break;
1353 case RID_AT_PROPERTY:
1354 gcc_assert (c_dialect_objc ());
1355 c_parser_objc_at_property_declaration (parser);
1356 break;
1357 case RID_AT_SYNTHESIZE:
1358 gcc_assert (c_dialect_objc ());
1359 c_parser_objc_at_synthesize_declaration (parser);
1360 break;
1361 case RID_AT_DYNAMIC:
1362 gcc_assert (c_dialect_objc ());
1363 c_parser_objc_at_dynamic_declaration (parser);
1364 break;
1365 case RID_AT_END:
1366 gcc_assert (c_dialect_objc ());
1367 c_parser_consume_token (parser);
1368 objc_finish_implementation ();
1369 break;
1370 default:
1371 goto decl_or_fndef;
1373 break;
1374 case CPP_SEMICOLON:
1375 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
1376 "ISO C does not allow extra %<;%> outside of a function");
1377 c_parser_consume_token (parser);
1378 break;
1379 case CPP_PRAGMA:
1380 mark_valid_location_for_stdc_pragma (true);
1381 c_parser_pragma (parser, pragma_external);
1382 mark_valid_location_for_stdc_pragma (false);
1383 break;
1384 case CPP_PLUS:
1385 case CPP_MINUS:
1386 if (c_dialect_objc ())
1388 c_parser_objc_method_definition (parser);
1389 break;
1391 /* Else fall through, and yield a syntax error trying to parse
1392 as a declaration or function definition. */
1393 default:
1394 decl_or_fndef:
1395 /* A declaration or a function definition (or, in Objective-C,
1396 an @interface or @protocol with prefix attributes). We can
1397 only tell which after parsing the declaration specifiers, if
1398 any, and the first declarator. */
1399 c_parser_declaration_or_fndef (parser, true, true, true, false, true,
1400 NULL, vNULL);
1401 break;
1405 static void c_finish_omp_declare_simd (c_parser *, tree, tree, vec<c_token>);
1407 /* Parse a declaration or function definition (C90 6.5, 6.7.1, C99
1408 6.7, 6.9.1). If FNDEF_OK is true, a function definition is
1409 accepted; otherwise (old-style parameter declarations) only other
1410 declarations are accepted. If STATIC_ASSERT_OK is true, a static
1411 assertion is accepted; otherwise (old-style parameter declarations)
1412 it is not. If NESTED is true, we are inside a function or parsing
1413 old-style parameter declarations; any functions encountered are
1414 nested functions and declaration specifiers are required; otherwise
1415 we are at top level and functions are normal functions and
1416 declaration specifiers may be optional. If EMPTY_OK is true, empty
1417 declarations are OK (subject to all other constraints); otherwise
1418 (old-style parameter declarations) they are diagnosed. If
1419 START_ATTR_OK is true, the declaration specifiers may start with
1420 attributes; otherwise they may not.
1421 OBJC_FOREACH_OBJECT_DECLARATION can be used to get back the parsed
1422 declaration when parsing an Objective-C foreach statement.
1424 declaration:
1425 declaration-specifiers init-declarator-list[opt] ;
1426 static_assert-declaration
1428 function-definition:
1429 declaration-specifiers[opt] declarator declaration-list[opt]
1430 compound-statement
1432 declaration-list:
1433 declaration
1434 declaration-list declaration
1436 init-declarator-list:
1437 init-declarator
1438 init-declarator-list , init-declarator
1440 init-declarator:
1441 declarator simple-asm-expr[opt] attributes[opt]
1442 declarator simple-asm-expr[opt] attributes[opt] = initializer
1444 GNU extensions:
1446 nested-function-definition:
1447 declaration-specifiers declarator declaration-list[opt]
1448 compound-statement
1450 Objective-C:
1451 attributes objc-class-definition
1452 attributes objc-category-definition
1453 attributes objc-protocol-definition
1455 The simple-asm-expr and attributes are GNU extensions.
1457 This function does not handle __extension__; that is handled in its
1458 callers. ??? Following the old parser, __extension__ may start
1459 external declarations, declarations in functions and declarations
1460 at the start of "for" loops, but not old-style parameter
1461 declarations.
1463 C99 requires declaration specifiers in a function definition; the
1464 absence is diagnosed through the diagnosis of implicit int. In GNU
1465 C we also allow but diagnose declarations without declaration
1466 specifiers, but only at top level (elsewhere they conflict with
1467 other syntax).
1469 In Objective-C, declarations of the looping variable in a foreach
1470 statement are exceptionally terminated by 'in' (for example, 'for
1471 (NSObject *object in array) { ... }').
1473 OpenMP:
1475 declaration:
1476 threadprivate-directive */
1478 static void
1479 c_parser_declaration_or_fndef (c_parser *parser, bool fndef_ok,
1480 bool static_assert_ok, bool empty_ok,
1481 bool nested, bool start_attr_ok,
1482 tree *objc_foreach_object_declaration,
1483 vec<c_token> omp_declare_simd_clauses)
1485 struct c_declspecs *specs;
1486 tree prefix_attrs;
1487 tree all_prefix_attrs;
1488 bool diagnosed_no_specs = false;
1489 location_t here = c_parser_peek_token (parser)->location;
1491 if (static_assert_ok
1492 && c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT))
1494 c_parser_static_assert_declaration (parser);
1495 return;
1497 specs = build_null_declspecs ();
1499 /* Try to detect an unknown type name when we have "A B" or "A *B". */
1500 if (c_parser_peek_token (parser)->type == CPP_NAME
1501 && c_parser_peek_token (parser)->id_kind == C_ID_ID
1502 && (c_parser_peek_2nd_token (parser)->type == CPP_NAME
1503 || c_parser_peek_2nd_token (parser)->type == CPP_MULT)
1504 && (!nested || !lookup_name (c_parser_peek_token (parser)->value)))
1506 error_at (here, "unknown type name %qE",
1507 c_parser_peek_token (parser)->value);
1509 /* Parse declspecs normally to get a correct pointer type, but avoid
1510 a further "fails to be a type name" error. Refuse nested functions
1511 since it is not how the user likely wants us to recover. */
1512 c_parser_peek_token (parser)->type = CPP_KEYWORD;
1513 c_parser_peek_token (parser)->keyword = RID_VOID;
1514 c_parser_peek_token (parser)->value = error_mark_node;
1515 fndef_ok = !nested;
1518 c_parser_declspecs (parser, specs, true, true, start_attr_ok,
1519 true, true, cla_nonabstract_decl);
1520 if (parser->error)
1522 c_parser_skip_to_end_of_block_or_statement (parser);
1523 return;
1525 if (nested && !specs->declspecs_seen_p)
1527 c_parser_error (parser, "expected declaration specifiers");
1528 c_parser_skip_to_end_of_block_or_statement (parser);
1529 return;
1531 finish_declspecs (specs);
1532 bool auto_type_p = specs->typespec_word == cts_auto_type;
1533 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1535 if (auto_type_p)
1536 error_at (here, "%<__auto_type%> in empty declaration");
1537 else if (empty_ok)
1538 shadow_tag (specs);
1539 else
1541 shadow_tag_warned (specs, 1);
1542 pedwarn (here, 0, "empty declaration");
1544 c_parser_consume_token (parser);
1545 return;
1548 /* Provide better error recovery. Note that a type name here is usually
1549 better diagnosed as a redeclaration. */
1550 if (empty_ok
1551 && specs->typespec_kind == ctsk_tagdef
1552 && c_parser_next_token_starts_declspecs (parser)
1553 && !c_parser_next_token_is (parser, CPP_NAME))
1555 c_parser_error (parser, "expected %<;%>, identifier or %<(%>");
1556 parser->error = false;
1557 shadow_tag_warned (specs, 1);
1558 return;
1560 else if (c_dialect_objc () && !auto_type_p)
1562 /* Prefix attributes are an error on method decls. */
1563 switch (c_parser_peek_token (parser)->type)
1565 case CPP_PLUS:
1566 case CPP_MINUS:
1567 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1568 return;
1569 if (specs->attrs)
1571 warning_at (c_parser_peek_token (parser)->location,
1572 OPT_Wattributes,
1573 "prefix attributes are ignored for methods");
1574 specs->attrs = NULL_TREE;
1576 if (fndef_ok)
1577 c_parser_objc_method_definition (parser);
1578 else
1579 c_parser_objc_methodproto (parser);
1580 return;
1581 break;
1582 default:
1583 break;
1585 /* This is where we parse 'attributes @interface ...',
1586 'attributes @implementation ...', 'attributes @protocol ...'
1587 (where attributes could be, for example, __attribute__
1588 ((deprecated)).
1590 switch (c_parser_peek_token (parser)->keyword)
1592 case RID_AT_INTERFACE:
1594 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1595 return;
1596 c_parser_objc_class_definition (parser, specs->attrs);
1597 return;
1599 break;
1600 case RID_AT_IMPLEMENTATION:
1602 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1603 return;
1604 if (specs->attrs)
1606 warning_at (c_parser_peek_token (parser)->location,
1607 OPT_Wattributes,
1608 "prefix attributes are ignored for implementations");
1609 specs->attrs = NULL_TREE;
1611 c_parser_objc_class_definition (parser, NULL_TREE);
1612 return;
1614 break;
1615 case RID_AT_PROTOCOL:
1617 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1618 return;
1619 c_parser_objc_protocol_definition (parser, specs->attrs);
1620 return;
1622 break;
1623 case RID_AT_ALIAS:
1624 case RID_AT_CLASS:
1625 case RID_AT_END:
1626 case RID_AT_PROPERTY:
1627 if (specs->attrs)
1629 c_parser_error (parser, "unexpected attribute");
1630 specs->attrs = NULL;
1632 break;
1633 default:
1634 break;
1638 pending_xref_error ();
1639 prefix_attrs = specs->attrs;
1640 all_prefix_attrs = prefix_attrs;
1641 specs->attrs = NULL_TREE;
1642 while (true)
1644 struct c_declarator *declarator;
1645 bool dummy = false;
1646 timevar_id_t tv;
1647 tree fnbody;
1648 /* Declaring either one or more declarators (in which case we
1649 should diagnose if there were no declaration specifiers) or a
1650 function definition (in which case the diagnostic for
1651 implicit int suffices). */
1652 declarator = c_parser_declarator (parser,
1653 specs->typespec_kind != ctsk_none,
1654 C_DTR_NORMAL, &dummy);
1655 if (declarator == NULL)
1657 if (omp_declare_simd_clauses.exists ()
1658 || !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
1659 c_finish_omp_declare_simd (parser, NULL_TREE, NULL_TREE,
1660 omp_declare_simd_clauses);
1661 c_parser_skip_to_end_of_block_or_statement (parser);
1662 return;
1664 if (auto_type_p && declarator->kind != cdk_id)
1666 error_at (here,
1667 "%<__auto_type%> requires a plain identifier"
1668 " as declarator");
1669 c_parser_skip_to_end_of_block_or_statement (parser);
1670 return;
1672 if (c_parser_next_token_is (parser, CPP_EQ)
1673 || c_parser_next_token_is (parser, CPP_COMMA)
1674 || c_parser_next_token_is (parser, CPP_SEMICOLON)
1675 || c_parser_next_token_is_keyword (parser, RID_ASM)
1676 || c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE)
1677 || c_parser_next_token_is_keyword (parser, RID_IN))
1679 tree asm_name = NULL_TREE;
1680 tree postfix_attrs = NULL_TREE;
1681 if (!diagnosed_no_specs && !specs->declspecs_seen_p)
1683 diagnosed_no_specs = true;
1684 pedwarn (here, 0, "data definition has no type or storage class");
1686 /* Having seen a data definition, there cannot now be a
1687 function definition. */
1688 fndef_ok = false;
1689 if (c_parser_next_token_is_keyword (parser, RID_ASM))
1690 asm_name = c_parser_simple_asm_expr (parser);
1691 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
1693 postfix_attrs = c_parser_attributes (parser);
1694 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
1696 /* This means there is an attribute specifier after
1697 the declarator in a function definition. Provide
1698 some more information for the user. */
1699 error_at (here, "attributes should be specified before the "
1700 "declarator in a function definition");
1701 c_parser_skip_to_end_of_block_or_statement (parser);
1702 return;
1705 if (c_parser_next_token_is (parser, CPP_EQ))
1707 tree d;
1708 struct c_expr init;
1709 location_t init_loc;
1710 c_parser_consume_token (parser);
1711 if (auto_type_p)
1713 start_init (NULL_TREE, asm_name, global_bindings_p ());
1714 init_loc = c_parser_peek_token (parser)->location;
1715 init = c_parser_expr_no_commas (parser, NULL);
1716 if (TREE_CODE (init.value) == COMPONENT_REF
1717 && DECL_C_BIT_FIELD (TREE_OPERAND (init.value, 1)))
1718 error_at (here,
1719 "%<__auto_type%> used with a bit-field"
1720 " initializer");
1721 init = convert_lvalue_to_rvalue (init_loc, init, true, true);
1722 tree init_type = TREE_TYPE (init.value);
1723 /* As with typeof, remove all 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, TYPE_UNQUALIFIED);
1727 bool vm_type = variably_modified_type_p (init_type,
1728 NULL_TREE);
1729 if (vm_type)
1730 init.value = c_save_expr (init.value);
1731 finish_init ();
1732 specs->typespec_kind = ctsk_typeof;
1733 specs->locations[cdw_typedef] = init_loc;
1734 specs->typedef_p = true;
1735 specs->type = init_type;
1736 if (vm_type)
1738 bool maybe_const = true;
1739 tree type_expr = c_fully_fold (init.value, false,
1740 &maybe_const);
1741 specs->expr_const_operands &= maybe_const;
1742 if (specs->expr)
1743 specs->expr = build2 (COMPOUND_EXPR,
1744 TREE_TYPE (type_expr),
1745 specs->expr, type_expr);
1746 else
1747 specs->expr = type_expr;
1749 d = start_decl (declarator, specs, true,
1750 chainon (postfix_attrs, all_prefix_attrs));
1751 if (!d)
1752 d = error_mark_node;
1753 if (omp_declare_simd_clauses.exists ()
1754 || !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
1755 c_finish_omp_declare_simd (parser, d, NULL_TREE,
1756 omp_declare_simd_clauses);
1758 else
1760 /* The declaration of the variable is in effect while
1761 its initializer is parsed. */
1762 d = start_decl (declarator, specs, true,
1763 chainon (postfix_attrs, all_prefix_attrs));
1764 if (!d)
1765 d = error_mark_node;
1766 if (omp_declare_simd_clauses.exists ()
1767 || !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
1768 c_finish_omp_declare_simd (parser, d, NULL_TREE,
1769 omp_declare_simd_clauses);
1770 start_init (d, asm_name, global_bindings_p ());
1771 init_loc = c_parser_peek_token (parser)->location;
1772 init = c_parser_initializer (parser);
1773 finish_init ();
1775 if (d != error_mark_node)
1777 maybe_warn_string_init (init_loc, TREE_TYPE (d), init);
1778 finish_decl (d, init_loc, init.value,
1779 init.original_type, asm_name);
1782 else
1784 if (auto_type_p)
1786 error_at (here,
1787 "%<__auto_type%> requires an initialized "
1788 "data declaration");
1789 c_parser_skip_to_end_of_block_or_statement (parser);
1790 return;
1792 tree d = start_decl (declarator, specs, false,
1793 chainon (postfix_attrs,
1794 all_prefix_attrs));
1795 if (omp_declare_simd_clauses.exists ()
1796 || !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
1798 tree parms = NULL_TREE;
1799 if (d && TREE_CODE (d) == FUNCTION_DECL)
1801 struct c_declarator *ce = declarator;
1802 while (ce != NULL)
1803 if (ce->kind == cdk_function)
1805 parms = ce->u.arg_info->parms;
1806 break;
1808 else
1809 ce = ce->declarator;
1811 if (parms)
1812 temp_store_parm_decls (d, parms);
1813 c_finish_omp_declare_simd (parser, d, parms,
1814 omp_declare_simd_clauses);
1815 if (parms)
1816 temp_pop_parm_decls ();
1818 if (d)
1819 finish_decl (d, UNKNOWN_LOCATION, NULL_TREE,
1820 NULL_TREE, asm_name);
1822 if (c_parser_next_token_is_keyword (parser, RID_IN))
1824 if (d)
1825 *objc_foreach_object_declaration = d;
1826 else
1827 *objc_foreach_object_declaration = error_mark_node;
1830 if (c_parser_next_token_is (parser, CPP_COMMA))
1832 if (auto_type_p)
1834 error_at (here,
1835 "%<__auto_type%> may only be used with"
1836 " a single declarator");
1837 c_parser_skip_to_end_of_block_or_statement (parser);
1838 return;
1840 c_parser_consume_token (parser);
1841 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
1842 all_prefix_attrs = chainon (c_parser_attributes (parser),
1843 prefix_attrs);
1844 else
1845 all_prefix_attrs = prefix_attrs;
1846 continue;
1848 else if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1850 c_parser_consume_token (parser);
1851 return;
1853 else if (c_parser_next_token_is_keyword (parser, RID_IN))
1855 /* This can only happen in Objective-C: we found the
1856 'in' that terminates the declaration inside an
1857 Objective-C foreach statement. Do not consume the
1858 token, so that the caller can use it to determine
1859 that this indeed is a foreach context. */
1860 return;
1862 else
1864 c_parser_error (parser, "expected %<,%> or %<;%>");
1865 c_parser_skip_to_end_of_block_or_statement (parser);
1866 return;
1869 else if (auto_type_p)
1871 error_at (here,
1872 "%<__auto_type%> requires an initialized data declaration");
1873 c_parser_skip_to_end_of_block_or_statement (parser);
1874 return;
1876 else if (!fndef_ok)
1878 c_parser_error (parser, "expected %<=%>, %<,%>, %<;%>, "
1879 "%<asm%> or %<__attribute__%>");
1880 c_parser_skip_to_end_of_block_or_statement (parser);
1881 return;
1883 /* Function definition (nested or otherwise). */
1884 if (nested)
1886 pedwarn (here, OPT_Wpedantic, "ISO C forbids nested functions");
1887 c_push_function_context ();
1889 if (!start_function (specs, declarator, all_prefix_attrs))
1891 /* This can appear in many cases looking nothing like a
1892 function definition, so we don't give a more specific
1893 error suggesting there was one. */
1894 c_parser_error (parser, "expected %<=%>, %<,%>, %<;%>, %<asm%> "
1895 "or %<__attribute__%>");
1896 if (nested)
1897 c_pop_function_context ();
1898 break;
1901 if (DECL_DECLARED_INLINE_P (current_function_decl))
1902 tv = TV_PARSE_INLINE;
1903 else
1904 tv = TV_PARSE_FUNC;
1905 timevar_push (tv);
1907 /* Parse old-style parameter declarations. ??? Attributes are
1908 not allowed to start declaration specifiers here because of a
1909 syntax conflict between a function declaration with attribute
1910 suffix and a function definition with an attribute prefix on
1911 first old-style parameter declaration. Following the old
1912 parser, they are not accepted on subsequent old-style
1913 parameter declarations either. However, there is no
1914 ambiguity after the first declaration, nor indeed on the
1915 first as long as we don't allow postfix attributes after a
1916 declarator with a nonempty identifier list in a definition;
1917 and postfix attributes have never been accepted here in
1918 function definitions either. */
1919 while (c_parser_next_token_is_not (parser, CPP_EOF)
1920 && c_parser_next_token_is_not (parser, CPP_OPEN_BRACE))
1921 c_parser_declaration_or_fndef (parser, false, false, false,
1922 true, false, NULL, vNULL);
1923 store_parm_decls ();
1924 if (omp_declare_simd_clauses.exists ()
1925 || !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
1926 c_finish_omp_declare_simd (parser, current_function_decl, NULL_TREE,
1927 omp_declare_simd_clauses);
1928 DECL_STRUCT_FUNCTION (current_function_decl)->function_start_locus
1929 = c_parser_peek_token (parser)->location;
1930 fnbody = c_parser_compound_statement (parser);
1931 if (flag_cilkplus && contains_array_notation_expr (fnbody))
1932 fnbody = expand_array_notation_exprs (fnbody);
1933 if (nested)
1935 tree decl = current_function_decl;
1936 /* Mark nested functions as needing static-chain initially.
1937 lower_nested_functions will recompute it but the
1938 DECL_STATIC_CHAIN flag is also used before that happens,
1939 by initializer_constant_valid_p. See gcc.dg/nested-fn-2.c. */
1940 DECL_STATIC_CHAIN (decl) = 1;
1941 add_stmt (fnbody);
1942 finish_function ();
1943 c_pop_function_context ();
1944 add_stmt (build_stmt (DECL_SOURCE_LOCATION (decl), DECL_EXPR, decl));
1946 else
1948 add_stmt (fnbody);
1949 finish_function ();
1952 timevar_pop (tv);
1953 break;
1957 /* Parse an asm-definition (asm() outside a function body). This is a
1958 GNU extension.
1960 asm-definition:
1961 simple-asm-expr ;
1964 static void
1965 c_parser_asm_definition (c_parser *parser)
1967 tree asm_str = c_parser_simple_asm_expr (parser);
1968 if (asm_str)
1969 add_asm_node (asm_str);
1970 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
1973 /* Parse a static assertion (C11 6.7.10).
1975 static_assert-declaration:
1976 static_assert-declaration-no-semi ;
1979 static void
1980 c_parser_static_assert_declaration (c_parser *parser)
1982 c_parser_static_assert_declaration_no_semi (parser);
1983 if (parser->error
1984 || !c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
1985 c_parser_skip_to_end_of_block_or_statement (parser);
1988 /* Parse a static assertion (C11 6.7.10), without the trailing
1989 semicolon.
1991 static_assert-declaration-no-semi:
1992 _Static_assert ( constant-expression , string-literal )
1995 static void
1996 c_parser_static_assert_declaration_no_semi (c_parser *parser)
1998 location_t assert_loc, value_loc;
1999 tree value;
2000 tree string;
2002 gcc_assert (c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT));
2003 assert_loc = c_parser_peek_token (parser)->location;
2004 if (!flag_isoc11)
2006 if (flag_isoc99)
2007 pedwarn (assert_loc, OPT_Wpedantic,
2008 "ISO C99 does not support %<_Static_assert%>");
2009 else
2010 pedwarn (assert_loc, OPT_Wpedantic,
2011 "ISO C90 does not support %<_Static_assert%>");
2013 c_parser_consume_token (parser);
2014 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2015 return;
2016 value_loc = c_parser_peek_token (parser)->location;
2017 value = c_parser_expr_no_commas (parser, NULL).value;
2018 parser->lex_untranslated_string = true;
2019 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
2021 parser->lex_untranslated_string = false;
2022 return;
2024 switch (c_parser_peek_token (parser)->type)
2026 case CPP_STRING:
2027 case CPP_STRING16:
2028 case CPP_STRING32:
2029 case CPP_WSTRING:
2030 case CPP_UTF8STRING:
2031 string = c_parser_peek_token (parser)->value;
2032 c_parser_consume_token (parser);
2033 parser->lex_untranslated_string = false;
2034 break;
2035 default:
2036 c_parser_error (parser, "expected string literal");
2037 parser->lex_untranslated_string = false;
2038 return;
2040 c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
2042 if (!INTEGRAL_TYPE_P (TREE_TYPE (value)))
2044 error_at (value_loc, "expression in static assertion is not an integer");
2045 return;
2047 if (TREE_CODE (value) != INTEGER_CST)
2049 value = c_fully_fold (value, false, NULL);
2050 if (TREE_CODE (value) == INTEGER_CST)
2051 pedwarn (value_loc, OPT_Wpedantic, "expression in static assertion "
2052 "is not an integer constant expression");
2054 if (TREE_CODE (value) != INTEGER_CST)
2056 error_at (value_loc, "expression in static assertion is not constant");
2057 return;
2059 constant_expression_warning (value);
2060 if (integer_zerop (value))
2061 error_at (assert_loc, "static assertion failed: %E", string);
2064 /* Parse some declaration specifiers (possibly none) (C90 6.5, C99
2065 6.7), adding them to SPECS (which may already include some).
2066 Storage class specifiers are accepted iff SCSPEC_OK; type
2067 specifiers are accepted iff TYPESPEC_OK; alignment specifiers are
2068 accepted iff ALIGNSPEC_OK; attributes are accepted at the start
2069 iff START_ATTR_OK; __auto_type is accepted iff AUTO_TYPE_OK.
2071 declaration-specifiers:
2072 storage-class-specifier declaration-specifiers[opt]
2073 type-specifier declaration-specifiers[opt]
2074 type-qualifier declaration-specifiers[opt]
2075 function-specifier declaration-specifiers[opt]
2076 alignment-specifier declaration-specifiers[opt]
2078 Function specifiers (inline) are from C99, and are currently
2079 handled as storage class specifiers, as is __thread. Alignment
2080 specifiers are from C11.
2082 C90 6.5.1, C99 6.7.1:
2083 storage-class-specifier:
2084 typedef
2085 extern
2086 static
2087 auto
2088 register
2089 _Thread_local
2091 (_Thread_local is new in C11.)
2093 C99 6.7.4:
2094 function-specifier:
2095 inline
2096 _Noreturn
2098 (_Noreturn is new in C11.)
2100 C90 6.5.2, C99 6.7.2:
2101 type-specifier:
2102 void
2103 char
2104 short
2106 long
2107 float
2108 double
2109 signed
2110 unsigned
2111 _Bool
2112 _Complex
2113 [_Imaginary removed in C99 TC2]
2114 struct-or-union-specifier
2115 enum-specifier
2116 typedef-name
2117 atomic-type-specifier
2119 (_Bool and _Complex are new in C99.)
2120 (atomic-type-specifier is new in C11.)
2122 C90 6.5.3, C99 6.7.3:
2124 type-qualifier:
2125 const
2126 restrict
2127 volatile
2128 address-space-qualifier
2129 _Atomic
2131 (restrict is new in C99.)
2132 (_Atomic is new in C11.)
2134 GNU extensions:
2136 declaration-specifiers:
2137 attributes declaration-specifiers[opt]
2139 type-qualifier:
2140 address-space
2142 address-space:
2143 identifier recognized by the target
2145 storage-class-specifier:
2146 __thread
2148 type-specifier:
2149 typeof-specifier
2150 __auto_type
2151 __int128
2152 _Decimal32
2153 _Decimal64
2154 _Decimal128
2155 _Fract
2156 _Accum
2157 _Sat
2159 (_Fract, _Accum, and _Sat are new from ISO/IEC DTR 18037:
2160 http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1169.pdf)
2162 atomic-type-specifier
2163 _Atomic ( type-name )
2165 Objective-C:
2167 type-specifier:
2168 class-name objc-protocol-refs[opt]
2169 typedef-name objc-protocol-refs
2170 objc-protocol-refs
2173 static void
2174 c_parser_declspecs (c_parser *parser, struct c_declspecs *specs,
2175 bool scspec_ok, bool typespec_ok, bool start_attr_ok,
2176 bool alignspec_ok, bool auto_type_ok,
2177 enum c_lookahead_kind la)
2179 bool attrs_ok = start_attr_ok;
2180 bool seen_type = specs->typespec_kind != ctsk_none;
2182 if (!typespec_ok)
2183 gcc_assert (la == cla_prefer_id);
2185 while (c_parser_next_token_is (parser, CPP_NAME)
2186 || c_parser_next_token_is (parser, CPP_KEYWORD)
2187 || (c_dialect_objc () && c_parser_next_token_is (parser, CPP_LESS)))
2189 struct c_typespec t;
2190 tree attrs;
2191 tree align;
2192 location_t loc = c_parser_peek_token (parser)->location;
2194 /* If we cannot accept a type, exit if the next token must start
2195 one. Also, if we already have seen a tagged definition,
2196 a typename would be an error anyway and likely the user
2197 has simply forgotten a semicolon, so we exit. */
2198 if ((!typespec_ok || specs->typespec_kind == ctsk_tagdef)
2199 && c_parser_next_tokens_start_typename (parser, la)
2200 && !c_parser_next_token_is_qualifier (parser))
2201 break;
2203 if (c_parser_next_token_is (parser, CPP_NAME))
2205 c_token *name_token = c_parser_peek_token (parser);
2206 tree value = name_token->value;
2207 c_id_kind kind = name_token->id_kind;
2209 if (kind == C_ID_ADDRSPACE)
2211 addr_space_t as
2212 = name_token->keyword - RID_FIRST_ADDR_SPACE;
2213 declspecs_add_addrspace (name_token->location, specs, as);
2214 c_parser_consume_token (parser);
2215 attrs_ok = true;
2216 continue;
2219 gcc_assert (!c_parser_next_token_is_qualifier (parser));
2221 /* If we cannot accept a type, and the next token must start one,
2222 exit. Do the same if we already have seen a tagged definition,
2223 since it would be an error anyway and likely the user has simply
2224 forgotten a semicolon. */
2225 if (seen_type || !c_parser_next_tokens_start_typename (parser, la))
2226 break;
2228 /* Now at an unknown typename (C_ID_ID), a C_ID_TYPENAME or
2229 a C_ID_CLASSNAME. */
2230 c_parser_consume_token (parser);
2231 seen_type = true;
2232 attrs_ok = true;
2233 if (kind == C_ID_ID)
2235 error_at (loc, "unknown type name %qE", value);
2236 t.kind = ctsk_typedef;
2237 t.spec = error_mark_node;
2239 else if (kind == C_ID_TYPENAME
2240 && (!c_dialect_objc ()
2241 || c_parser_next_token_is_not (parser, CPP_LESS)))
2243 t.kind = ctsk_typedef;
2244 /* For a typedef name, record the meaning, not the name.
2245 In case of 'foo foo, bar;'. */
2246 t.spec = lookup_name (value);
2248 else
2250 tree proto = NULL_TREE;
2251 gcc_assert (c_dialect_objc ());
2252 t.kind = ctsk_objc;
2253 if (c_parser_next_token_is (parser, CPP_LESS))
2254 proto = c_parser_objc_protocol_refs (parser);
2255 t.spec = objc_get_protocol_qualified_type (value, proto);
2257 t.expr = NULL_TREE;
2258 t.expr_const_operands = true;
2259 declspecs_add_type (name_token->location, specs, t);
2260 continue;
2262 if (c_parser_next_token_is (parser, CPP_LESS))
2264 /* Make "<SomeProtocol>" equivalent to "id <SomeProtocol>" -
2265 nisse@lysator.liu.se. */
2266 tree proto;
2267 gcc_assert (c_dialect_objc ());
2268 if (!typespec_ok || seen_type)
2269 break;
2270 proto = c_parser_objc_protocol_refs (parser);
2271 t.kind = ctsk_objc;
2272 t.spec = objc_get_protocol_qualified_type (NULL_TREE, proto);
2273 t.expr = NULL_TREE;
2274 t.expr_const_operands = true;
2275 declspecs_add_type (loc, specs, t);
2276 continue;
2278 gcc_assert (c_parser_next_token_is (parser, CPP_KEYWORD));
2279 switch (c_parser_peek_token (parser)->keyword)
2281 case RID_STATIC:
2282 case RID_EXTERN:
2283 case RID_REGISTER:
2284 case RID_TYPEDEF:
2285 case RID_INLINE:
2286 case RID_NORETURN:
2287 case RID_AUTO:
2288 case RID_THREAD:
2289 if (!scspec_ok)
2290 goto out;
2291 attrs_ok = true;
2292 /* TODO: Distinguish between function specifiers (inline, noreturn)
2293 and storage class specifiers, either here or in
2294 declspecs_add_scspec. */
2295 declspecs_add_scspec (loc, specs,
2296 c_parser_peek_token (parser)->value);
2297 c_parser_consume_token (parser);
2298 break;
2299 case RID_AUTO_TYPE:
2300 if (!auto_type_ok)
2301 goto out;
2302 /* Fall through. */
2303 case RID_UNSIGNED:
2304 case RID_LONG:
2305 case RID_INT128:
2306 case RID_SHORT:
2307 case RID_SIGNED:
2308 case RID_COMPLEX:
2309 case RID_INT:
2310 case RID_CHAR:
2311 case RID_FLOAT:
2312 case RID_DOUBLE:
2313 case RID_VOID:
2314 case RID_DFLOAT32:
2315 case RID_DFLOAT64:
2316 case RID_DFLOAT128:
2317 case RID_BOOL:
2318 case RID_FRACT:
2319 case RID_ACCUM:
2320 case RID_SAT:
2321 if (!typespec_ok)
2322 goto out;
2323 attrs_ok = true;
2324 seen_type = true;
2325 if (c_dialect_objc ())
2326 parser->objc_need_raw_identifier = true;
2327 t.kind = ctsk_resword;
2328 t.spec = c_parser_peek_token (parser)->value;
2329 t.expr = NULL_TREE;
2330 t.expr_const_operands = true;
2331 declspecs_add_type (loc, specs, t);
2332 c_parser_consume_token (parser);
2333 break;
2334 case RID_ENUM:
2335 if (!typespec_ok)
2336 goto out;
2337 attrs_ok = true;
2338 seen_type = true;
2339 t = c_parser_enum_specifier (parser);
2340 declspecs_add_type (loc, specs, t);
2341 break;
2342 case RID_STRUCT:
2343 case RID_UNION:
2344 if (!typespec_ok)
2345 goto out;
2346 attrs_ok = true;
2347 seen_type = true;
2348 t = c_parser_struct_or_union_specifier (parser);
2349 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, t.spec);
2350 declspecs_add_type (loc, specs, t);
2351 break;
2352 case RID_TYPEOF:
2353 /* ??? The old parser rejected typeof after other type
2354 specifiers, but is a syntax error the best way of
2355 handling this? */
2356 if (!typespec_ok || seen_type)
2357 goto out;
2358 attrs_ok = true;
2359 seen_type = true;
2360 t = c_parser_typeof_specifier (parser);
2361 declspecs_add_type (loc, specs, t);
2362 break;
2363 case RID_ATOMIC:
2364 /* C parser handling of Objective-C constructs needs
2365 checking for correct lvalue-to-rvalue conversions, and
2366 the code in build_modify_expr handling various
2367 Objective-C cases, and that in build_unary_op handling
2368 Objective-C cases for increment / decrement, also needs
2369 updating; uses of TYPE_MAIN_VARIANT in objc_compare_types
2370 and objc_types_are_equivalent may also need updates. */
2371 if (c_dialect_objc ())
2372 sorry ("%<_Atomic%> in Objective-C");
2373 /* C parser handling of OpenMP constructs needs checking for
2374 correct lvalue-to-rvalue conversions. */
2375 if (flag_openmp)
2376 sorry ("%<_Atomic%> with OpenMP");
2377 if (!flag_isoc11)
2379 if (flag_isoc99)
2380 pedwarn (loc, OPT_Wpedantic,
2381 "ISO C99 does not support the %<_Atomic%> qualifier");
2382 else
2383 pedwarn (loc, OPT_Wpedantic,
2384 "ISO C90 does not support the %<_Atomic%> qualifier");
2386 attrs_ok = true;
2387 tree value;
2388 value = c_parser_peek_token (parser)->value;
2389 c_parser_consume_token (parser);
2390 if (typespec_ok && c_parser_next_token_is (parser, CPP_OPEN_PAREN))
2392 /* _Atomic ( type-name ). */
2393 seen_type = true;
2394 c_parser_consume_token (parser);
2395 struct c_type_name *type = c_parser_type_name (parser);
2396 t.kind = ctsk_typeof;
2397 t.spec = error_mark_node;
2398 t.expr = NULL_TREE;
2399 t.expr_const_operands = true;
2400 if (type != NULL)
2401 t.spec = groktypename (type, &t.expr,
2402 &t.expr_const_operands);
2403 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2404 "expected %<)%>");
2405 if (t.spec != error_mark_node)
2407 if (TREE_CODE (t.spec) == ARRAY_TYPE)
2408 error_at (loc, "%<_Atomic%>-qualified array type");
2409 else if (TREE_CODE (t.spec) == FUNCTION_TYPE)
2410 error_at (loc, "%<_Atomic%>-qualified function type");
2411 else if (TYPE_QUALS (t.spec) != TYPE_UNQUALIFIED)
2412 error_at (loc, "%<_Atomic%> applied to a qualified type");
2413 else
2414 t.spec = c_build_qualified_type (t.spec, TYPE_QUAL_ATOMIC);
2416 declspecs_add_type (loc, specs, t);
2418 else
2419 declspecs_add_qual (loc, specs, value);
2420 break;
2421 case RID_CONST:
2422 case RID_VOLATILE:
2423 case RID_RESTRICT:
2424 attrs_ok = true;
2425 declspecs_add_qual (loc, specs, c_parser_peek_token (parser)->value);
2426 c_parser_consume_token (parser);
2427 break;
2428 case RID_ATTRIBUTE:
2429 if (!attrs_ok)
2430 goto out;
2431 attrs = c_parser_attributes (parser);
2432 declspecs_add_attrs (loc, specs, attrs);
2433 break;
2434 case RID_ALIGNAS:
2435 if (!alignspec_ok)
2436 goto out;
2437 align = c_parser_alignas_specifier (parser);
2438 declspecs_add_alignas (loc, specs, align);
2439 break;
2440 default:
2441 goto out;
2444 out: ;
2447 /* Parse an enum specifier (C90 6.5.2.2, C99 6.7.2.2).
2449 enum-specifier:
2450 enum attributes[opt] identifier[opt] { enumerator-list } attributes[opt]
2451 enum attributes[opt] identifier[opt] { enumerator-list , } attributes[opt]
2452 enum attributes[opt] identifier
2454 The form with trailing comma is new in C99. The forms with
2455 attributes are GNU extensions. In GNU C, we accept any expression
2456 without commas in the syntax (assignment expressions, not just
2457 conditional expressions); assignment expressions will be diagnosed
2458 as non-constant.
2460 enumerator-list:
2461 enumerator
2462 enumerator-list , enumerator
2464 enumerator:
2465 enumeration-constant
2466 enumeration-constant = constant-expression
2469 static struct c_typespec
2470 c_parser_enum_specifier (c_parser *parser)
2472 struct c_typespec ret;
2473 tree attrs;
2474 tree ident = NULL_TREE;
2475 location_t enum_loc;
2476 location_t ident_loc = UNKNOWN_LOCATION; /* Quiet warning. */
2477 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ENUM));
2478 enum_loc = c_parser_peek_token (parser)->location;
2479 c_parser_consume_token (parser);
2480 attrs = c_parser_attributes (parser);
2481 enum_loc = c_parser_peek_token (parser)->location;
2482 /* Set the location in case we create a decl now. */
2483 c_parser_set_source_position_from_token (c_parser_peek_token (parser));
2484 if (c_parser_next_token_is (parser, CPP_NAME))
2486 ident = c_parser_peek_token (parser)->value;
2487 ident_loc = c_parser_peek_token (parser)->location;
2488 enum_loc = ident_loc;
2489 c_parser_consume_token (parser);
2491 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
2493 /* Parse an enum definition. */
2494 struct c_enum_contents the_enum;
2495 tree type;
2496 tree postfix_attrs;
2497 /* We chain the enumerators in reverse order, then put them in
2498 forward order at the end. */
2499 tree values;
2500 timevar_push (TV_PARSE_ENUM);
2501 type = start_enum (enum_loc, &the_enum, ident);
2502 values = NULL_TREE;
2503 c_parser_consume_token (parser);
2504 while (true)
2506 tree enum_id;
2507 tree enum_value;
2508 tree enum_decl;
2509 bool seen_comma;
2510 c_token *token;
2511 location_t comma_loc = UNKNOWN_LOCATION; /* Quiet warning. */
2512 location_t decl_loc, value_loc;
2513 if (c_parser_next_token_is_not (parser, CPP_NAME))
2515 c_parser_error (parser, "expected identifier");
2516 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
2517 values = error_mark_node;
2518 break;
2520 token = c_parser_peek_token (parser);
2521 enum_id = token->value;
2522 /* Set the location in case we create a decl now. */
2523 c_parser_set_source_position_from_token (token);
2524 decl_loc = value_loc = token->location;
2525 c_parser_consume_token (parser);
2526 if (c_parser_next_token_is (parser, CPP_EQ))
2528 c_parser_consume_token (parser);
2529 value_loc = c_parser_peek_token (parser)->location;
2530 enum_value = c_parser_expr_no_commas (parser, NULL).value;
2532 else
2533 enum_value = NULL_TREE;
2534 enum_decl = build_enumerator (decl_loc, value_loc,
2535 &the_enum, enum_id, enum_value);
2536 TREE_CHAIN (enum_decl) = values;
2537 values = enum_decl;
2538 seen_comma = false;
2539 if (c_parser_next_token_is (parser, CPP_COMMA))
2541 comma_loc = c_parser_peek_token (parser)->location;
2542 seen_comma = true;
2543 c_parser_consume_token (parser);
2545 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2547 if (seen_comma && !flag_isoc99)
2548 pedwarn (comma_loc, OPT_Wpedantic, "comma at end of enumerator list");
2549 c_parser_consume_token (parser);
2550 break;
2552 if (!seen_comma)
2554 c_parser_error (parser, "expected %<,%> or %<}%>");
2555 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
2556 values = error_mark_node;
2557 break;
2560 postfix_attrs = c_parser_attributes (parser);
2561 ret.spec = finish_enum (type, nreverse (values),
2562 chainon (attrs, postfix_attrs));
2563 ret.kind = ctsk_tagdef;
2564 ret.expr = NULL_TREE;
2565 ret.expr_const_operands = true;
2566 timevar_pop (TV_PARSE_ENUM);
2567 return ret;
2569 else if (!ident)
2571 c_parser_error (parser, "expected %<{%>");
2572 ret.spec = error_mark_node;
2573 ret.kind = ctsk_tagref;
2574 ret.expr = NULL_TREE;
2575 ret.expr_const_operands = true;
2576 return ret;
2578 ret = parser_xref_tag (ident_loc, ENUMERAL_TYPE, ident);
2579 /* In ISO C, enumerated types can be referred to only if already
2580 defined. */
2581 if (pedantic && !COMPLETE_TYPE_P (ret.spec))
2583 gcc_assert (ident);
2584 pedwarn (enum_loc, OPT_Wpedantic,
2585 "ISO C forbids forward references to %<enum%> types");
2587 return ret;
2590 /* Parse a struct or union specifier (C90 6.5.2.1, C99 6.7.2.1).
2592 struct-or-union-specifier:
2593 struct-or-union attributes[opt] identifier[opt]
2594 { struct-contents } attributes[opt]
2595 struct-or-union attributes[opt] identifier
2597 struct-contents:
2598 struct-declaration-list
2600 struct-declaration-list:
2601 struct-declaration ;
2602 struct-declaration-list struct-declaration ;
2604 GNU extensions:
2606 struct-contents:
2607 empty
2608 struct-declaration
2609 struct-declaration-list struct-declaration
2611 struct-declaration-list:
2612 struct-declaration-list ;
2615 (Note that in the syntax here, unlike that in ISO C, the semicolons
2616 are included here rather than in struct-declaration, in order to
2617 describe the syntax with extra semicolons and missing semicolon at
2618 end.)
2620 Objective-C:
2622 struct-declaration-list:
2623 @defs ( class-name )
2625 (Note this does not include a trailing semicolon, but can be
2626 followed by further declarations, and gets a pedwarn-if-pedantic
2627 when followed by a semicolon.) */
2629 static struct c_typespec
2630 c_parser_struct_or_union_specifier (c_parser *parser)
2632 struct c_typespec ret;
2633 tree attrs;
2634 tree ident = NULL_TREE;
2635 location_t struct_loc;
2636 location_t ident_loc = UNKNOWN_LOCATION;
2637 enum tree_code code;
2638 switch (c_parser_peek_token (parser)->keyword)
2640 case RID_STRUCT:
2641 code = RECORD_TYPE;
2642 break;
2643 case RID_UNION:
2644 code = UNION_TYPE;
2645 break;
2646 default:
2647 gcc_unreachable ();
2649 struct_loc = c_parser_peek_token (parser)->location;
2650 c_parser_consume_token (parser);
2651 attrs = c_parser_attributes (parser);
2653 /* Set the location in case we create a decl now. */
2654 c_parser_set_source_position_from_token (c_parser_peek_token (parser));
2656 if (c_parser_next_token_is (parser, CPP_NAME))
2658 ident = c_parser_peek_token (parser)->value;
2659 ident_loc = c_parser_peek_token (parser)->location;
2660 struct_loc = ident_loc;
2661 c_parser_consume_token (parser);
2663 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
2665 /* Parse a struct or union definition. Start the scope of the
2666 tag before parsing components. */
2667 struct c_struct_parse_info *struct_info;
2668 tree type = start_struct (struct_loc, code, ident, &struct_info);
2669 tree postfix_attrs;
2670 /* We chain the components in reverse order, then put them in
2671 forward order at the end. Each struct-declaration may
2672 declare multiple components (comma-separated), so we must use
2673 chainon to join them, although when parsing each
2674 struct-declaration we can use TREE_CHAIN directly.
2676 The theory behind all this is that there will be more
2677 semicolon separated fields than comma separated fields, and
2678 so we'll be minimizing the number of node traversals required
2679 by chainon. */
2680 tree contents;
2681 timevar_push (TV_PARSE_STRUCT);
2682 contents = NULL_TREE;
2683 c_parser_consume_token (parser);
2684 /* Handle the Objective-C @defs construct,
2685 e.g. foo(sizeof(struct{ @defs(ClassName) }));. */
2686 if (c_parser_next_token_is_keyword (parser, RID_AT_DEFS))
2688 tree name;
2689 gcc_assert (c_dialect_objc ());
2690 c_parser_consume_token (parser);
2691 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2692 goto end_at_defs;
2693 if (c_parser_next_token_is (parser, CPP_NAME)
2694 && c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME)
2696 name = c_parser_peek_token (parser)->value;
2697 c_parser_consume_token (parser);
2699 else
2701 c_parser_error (parser, "expected class name");
2702 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
2703 goto end_at_defs;
2705 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2706 "expected %<)%>");
2707 contents = nreverse (objc_get_class_ivars (name));
2709 end_at_defs:
2710 /* Parse the struct-declarations and semicolons. Problems with
2711 semicolons are diagnosed here; empty structures are diagnosed
2712 elsewhere. */
2713 while (true)
2715 tree decls;
2716 /* Parse any stray semicolon. */
2717 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
2719 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
2720 "extra semicolon in struct or union specified");
2721 c_parser_consume_token (parser);
2722 continue;
2724 /* Stop if at the end of the struct or union contents. */
2725 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2727 c_parser_consume_token (parser);
2728 break;
2730 /* Accept #pragmas at struct scope. */
2731 if (c_parser_next_token_is (parser, CPP_PRAGMA))
2733 c_parser_pragma (parser, pragma_struct);
2734 continue;
2736 /* Parse some comma-separated declarations, but not the
2737 trailing semicolon if any. */
2738 decls = c_parser_struct_declaration (parser);
2739 contents = chainon (decls, contents);
2740 /* If no semicolon follows, either we have a parse error or
2741 are at the end of the struct or union and should
2742 pedwarn. */
2743 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
2744 c_parser_consume_token (parser);
2745 else
2747 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2748 pedwarn (c_parser_peek_token (parser)->location, 0,
2749 "no semicolon at end of struct or union");
2750 else if (parser->error
2751 || !c_parser_next_token_starts_declspecs (parser))
2753 c_parser_error (parser, "expected %<;%>");
2754 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
2755 break;
2758 /* If we come here, we have already emitted an error
2759 for an expected `;', identifier or `(', and we also
2760 recovered already. Go on with the next field. */
2763 postfix_attrs = c_parser_attributes (parser);
2764 ret.spec = finish_struct (struct_loc, type, nreverse (contents),
2765 chainon (attrs, postfix_attrs), struct_info);
2766 ret.kind = ctsk_tagdef;
2767 ret.expr = NULL_TREE;
2768 ret.expr_const_operands = true;
2769 timevar_pop (TV_PARSE_STRUCT);
2770 return ret;
2772 else if (!ident)
2774 c_parser_error (parser, "expected %<{%>");
2775 ret.spec = error_mark_node;
2776 ret.kind = ctsk_tagref;
2777 ret.expr = NULL_TREE;
2778 ret.expr_const_operands = true;
2779 return ret;
2781 ret = parser_xref_tag (ident_loc, code, ident);
2782 return ret;
2785 /* Parse a struct-declaration (C90 6.5.2.1, C99 6.7.2.1), *without*
2786 the trailing semicolon.
2788 struct-declaration:
2789 specifier-qualifier-list struct-declarator-list
2790 static_assert-declaration-no-semi
2792 specifier-qualifier-list:
2793 type-specifier specifier-qualifier-list[opt]
2794 type-qualifier specifier-qualifier-list[opt]
2795 attributes specifier-qualifier-list[opt]
2797 struct-declarator-list:
2798 struct-declarator
2799 struct-declarator-list , attributes[opt] struct-declarator
2801 struct-declarator:
2802 declarator attributes[opt]
2803 declarator[opt] : constant-expression attributes[opt]
2805 GNU extensions:
2807 struct-declaration:
2808 __extension__ struct-declaration
2809 specifier-qualifier-list
2811 Unlike the ISO C syntax, semicolons are handled elsewhere. The use
2812 of attributes where shown is a GNU extension. In GNU C, we accept
2813 any expression without commas in the syntax (assignment
2814 expressions, not just conditional expressions); assignment
2815 expressions will be diagnosed as non-constant. */
2817 static tree
2818 c_parser_struct_declaration (c_parser *parser)
2820 struct c_declspecs *specs;
2821 tree prefix_attrs;
2822 tree all_prefix_attrs;
2823 tree decls;
2824 location_t decl_loc;
2825 if (c_parser_next_token_is_keyword (parser, RID_EXTENSION))
2827 int ext;
2828 tree decl;
2829 ext = disable_extension_diagnostics ();
2830 c_parser_consume_token (parser);
2831 decl = c_parser_struct_declaration (parser);
2832 restore_extension_diagnostics (ext);
2833 return decl;
2835 if (c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT))
2837 c_parser_static_assert_declaration_no_semi (parser);
2838 return NULL_TREE;
2840 specs = build_null_declspecs ();
2841 decl_loc = c_parser_peek_token (parser)->location;
2842 /* Strictly by the standard, we shouldn't allow _Alignas here,
2843 but it appears to have been intended to allow it there, so
2844 we're keeping it as it is until WG14 reaches a conclusion
2845 of N1731.
2846 <http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1731.pdf> */
2847 c_parser_declspecs (parser, specs, false, true, true,
2848 true, false, cla_nonabstract_decl);
2849 if (parser->error)
2850 return NULL_TREE;
2851 if (!specs->declspecs_seen_p)
2853 c_parser_error (parser, "expected specifier-qualifier-list");
2854 return NULL_TREE;
2856 finish_declspecs (specs);
2857 if (c_parser_next_token_is (parser, CPP_SEMICOLON)
2858 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2860 tree ret;
2861 if (specs->typespec_kind == ctsk_none)
2863 pedwarn (decl_loc, OPT_Wpedantic,
2864 "ISO C forbids member declarations with no members");
2865 shadow_tag_warned (specs, pedantic);
2866 ret = NULL_TREE;
2868 else
2870 /* Support for unnamed structs or unions as members of
2871 structs or unions (which is [a] useful and [b] supports
2872 MS P-SDK). */
2873 tree attrs = NULL;
2875 ret = grokfield (c_parser_peek_token (parser)->location,
2876 build_id_declarator (NULL_TREE), specs,
2877 NULL_TREE, &attrs);
2878 if (ret)
2879 decl_attributes (&ret, attrs, 0);
2881 return ret;
2884 /* Provide better error recovery. Note that a type name here is valid,
2885 and will be treated as a field name. */
2886 if (specs->typespec_kind == ctsk_tagdef
2887 && TREE_CODE (specs->type) != ENUMERAL_TYPE
2888 && c_parser_next_token_starts_declspecs (parser)
2889 && !c_parser_next_token_is (parser, CPP_NAME))
2891 c_parser_error (parser, "expected %<;%>, identifier or %<(%>");
2892 parser->error = false;
2893 return NULL_TREE;
2896 pending_xref_error ();
2897 prefix_attrs = specs->attrs;
2898 all_prefix_attrs = prefix_attrs;
2899 specs->attrs = NULL_TREE;
2900 decls = NULL_TREE;
2901 while (true)
2903 /* Declaring one or more declarators or un-named bit-fields. */
2904 struct c_declarator *declarator;
2905 bool dummy = false;
2906 if (c_parser_next_token_is (parser, CPP_COLON))
2907 declarator = build_id_declarator (NULL_TREE);
2908 else
2909 declarator = c_parser_declarator (parser,
2910 specs->typespec_kind != ctsk_none,
2911 C_DTR_NORMAL, &dummy);
2912 if (declarator == NULL)
2914 c_parser_skip_to_end_of_block_or_statement (parser);
2915 break;
2917 if (c_parser_next_token_is (parser, CPP_COLON)
2918 || c_parser_next_token_is (parser, CPP_COMMA)
2919 || c_parser_next_token_is (parser, CPP_SEMICOLON)
2920 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE)
2921 || c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2923 tree postfix_attrs = NULL_TREE;
2924 tree width = NULL_TREE;
2925 tree d;
2926 if (c_parser_next_token_is (parser, CPP_COLON))
2928 c_parser_consume_token (parser);
2929 width = c_parser_expr_no_commas (parser, NULL).value;
2931 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2932 postfix_attrs = c_parser_attributes (parser);
2933 d = grokfield (c_parser_peek_token (parser)->location,
2934 declarator, specs, width, &all_prefix_attrs);
2935 decl_attributes (&d, chainon (postfix_attrs,
2936 all_prefix_attrs), 0);
2937 DECL_CHAIN (d) = decls;
2938 decls = d;
2939 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2940 all_prefix_attrs = chainon (c_parser_attributes (parser),
2941 prefix_attrs);
2942 else
2943 all_prefix_attrs = prefix_attrs;
2944 if (c_parser_next_token_is (parser, CPP_COMMA))
2945 c_parser_consume_token (parser);
2946 else if (c_parser_next_token_is (parser, CPP_SEMICOLON)
2947 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2949 /* Semicolon consumed in caller. */
2950 break;
2952 else
2954 c_parser_error (parser, "expected %<,%>, %<;%> or %<}%>");
2955 break;
2958 else
2960 c_parser_error (parser,
2961 "expected %<:%>, %<,%>, %<;%>, %<}%> or "
2962 "%<__attribute__%>");
2963 break;
2966 return decls;
2969 /* Parse a typeof specifier (a GNU extension).
2971 typeof-specifier:
2972 typeof ( expression )
2973 typeof ( type-name )
2976 static struct c_typespec
2977 c_parser_typeof_specifier (c_parser *parser)
2979 struct c_typespec ret;
2980 ret.kind = ctsk_typeof;
2981 ret.spec = error_mark_node;
2982 ret.expr = NULL_TREE;
2983 ret.expr_const_operands = true;
2984 gcc_assert (c_parser_next_token_is_keyword (parser, RID_TYPEOF));
2985 c_parser_consume_token (parser);
2986 c_inhibit_evaluation_warnings++;
2987 in_typeof++;
2988 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2990 c_inhibit_evaluation_warnings--;
2991 in_typeof--;
2992 return ret;
2994 if (c_parser_next_tokens_start_typename (parser, cla_prefer_id))
2996 struct c_type_name *type = c_parser_type_name (parser);
2997 c_inhibit_evaluation_warnings--;
2998 in_typeof--;
2999 if (type != NULL)
3001 ret.spec = groktypename (type, &ret.expr, &ret.expr_const_operands);
3002 pop_maybe_used (variably_modified_type_p (ret.spec, NULL_TREE));
3005 else
3007 bool was_vm;
3008 location_t here = c_parser_peek_token (parser)->location;
3009 struct c_expr expr = c_parser_expression (parser);
3010 c_inhibit_evaluation_warnings--;
3011 in_typeof--;
3012 if (TREE_CODE (expr.value) == COMPONENT_REF
3013 && DECL_C_BIT_FIELD (TREE_OPERAND (expr.value, 1)))
3014 error_at (here, "%<typeof%> applied to a bit-field");
3015 mark_exp_read (expr.value);
3016 ret.spec = TREE_TYPE (expr.value);
3017 was_vm = variably_modified_type_p (ret.spec, NULL_TREE);
3018 /* This is returned with the type so that when the type is
3019 evaluated, this can be evaluated. */
3020 if (was_vm)
3021 ret.expr = c_fully_fold (expr.value, false, &ret.expr_const_operands);
3022 pop_maybe_used (was_vm);
3023 /* For use in macros such as those in <stdatomic.h>, remove all
3024 qualifiers from atomic types. (const can be an issue for more macros
3025 using typeof than just the <stdatomic.h> ones.) */
3026 if (ret.spec != error_mark_node && TYPE_ATOMIC (ret.spec))
3027 ret.spec = c_build_qualified_type (ret.spec, TYPE_UNQUALIFIED);
3029 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
3030 return ret;
3033 /* Parse an alignment-specifier.
3035 C11 6.7.5:
3037 alignment-specifier:
3038 _Alignas ( type-name )
3039 _Alignas ( constant-expression )
3042 static tree
3043 c_parser_alignas_specifier (c_parser * parser)
3045 tree ret = error_mark_node;
3046 location_t loc = c_parser_peek_token (parser)->location;
3047 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ALIGNAS));
3048 c_parser_consume_token (parser);
3049 if (!flag_isoc11)
3051 if (flag_isoc99)
3052 pedwarn (loc, OPT_Wpedantic,
3053 "ISO C99 does not support %<_Alignas%>");
3054 else
3055 pedwarn (loc, OPT_Wpedantic,
3056 "ISO C90 does not support %<_Alignas%>");
3058 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3059 return ret;
3060 if (c_parser_next_tokens_start_typename (parser, cla_prefer_id))
3062 struct c_type_name *type = c_parser_type_name (parser);
3063 if (type != NULL)
3064 ret = c_sizeof_or_alignof_type (loc, groktypename (type, NULL, NULL),
3065 false, true, 1);
3067 else
3068 ret = c_parser_expr_no_commas (parser, NULL).value;
3069 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
3070 return ret;
3073 /* Parse a declarator, possibly an abstract declarator (C90 6.5.4,
3074 6.5.5, C99 6.7.5, 6.7.6). If TYPE_SEEN_P then a typedef name may
3075 be redeclared; otherwise it may not. KIND indicates which kind of
3076 declarator is wanted. Returns a valid declarator except in the
3077 case of a syntax error in which case NULL is returned. *SEEN_ID is
3078 set to true if an identifier being declared is seen; this is used
3079 to diagnose bad forms of abstract array declarators and to
3080 determine whether an identifier list is syntactically permitted.
3082 declarator:
3083 pointer[opt] direct-declarator
3085 direct-declarator:
3086 identifier
3087 ( attributes[opt] declarator )
3088 direct-declarator array-declarator
3089 direct-declarator ( parameter-type-list )
3090 direct-declarator ( identifier-list[opt] )
3092 pointer:
3093 * type-qualifier-list[opt]
3094 * type-qualifier-list[opt] pointer
3096 type-qualifier-list:
3097 type-qualifier
3098 attributes
3099 type-qualifier-list type-qualifier
3100 type-qualifier-list attributes
3102 array-declarator:
3103 [ type-qualifier-list[opt] assignment-expression[opt] ]
3104 [ static type-qualifier-list[opt] assignment-expression ]
3105 [ type-qualifier-list static assignment-expression ]
3106 [ type-qualifier-list[opt] * ]
3108 parameter-type-list:
3109 parameter-list
3110 parameter-list , ...
3112 parameter-list:
3113 parameter-declaration
3114 parameter-list , parameter-declaration
3116 parameter-declaration:
3117 declaration-specifiers declarator attributes[opt]
3118 declaration-specifiers abstract-declarator[opt] attributes[opt]
3120 identifier-list:
3121 identifier
3122 identifier-list , identifier
3124 abstract-declarator:
3125 pointer
3126 pointer[opt] direct-abstract-declarator
3128 direct-abstract-declarator:
3129 ( attributes[opt] abstract-declarator )
3130 direct-abstract-declarator[opt] array-declarator
3131 direct-abstract-declarator[opt] ( parameter-type-list[opt] )
3133 GNU extensions:
3135 direct-declarator:
3136 direct-declarator ( parameter-forward-declarations
3137 parameter-type-list[opt] )
3139 direct-abstract-declarator:
3140 direct-abstract-declarator[opt] ( parameter-forward-declarations
3141 parameter-type-list[opt] )
3143 parameter-forward-declarations:
3144 parameter-list ;
3145 parameter-forward-declarations parameter-list ;
3147 The uses of attributes shown above are GNU extensions.
3149 Some forms of array declarator are not included in C99 in the
3150 syntax for abstract declarators; these are disallowed elsewhere.
3151 This may be a defect (DR#289).
3153 This function also accepts an omitted abstract declarator as being
3154 an abstract declarator, although not part of the formal syntax. */
3156 static struct c_declarator *
3157 c_parser_declarator (c_parser *parser, bool type_seen_p, c_dtr_syn kind,
3158 bool *seen_id)
3160 /* Parse any initial pointer part. */
3161 if (c_parser_next_token_is (parser, CPP_MULT))
3163 struct c_declspecs *quals_attrs = build_null_declspecs ();
3164 struct c_declarator *inner;
3165 c_parser_consume_token (parser);
3166 c_parser_declspecs (parser, quals_attrs, false, false, true,
3167 false, false, cla_prefer_id);
3168 inner = c_parser_declarator (parser, type_seen_p, kind, seen_id);
3169 if (inner == NULL)
3170 return NULL;
3171 else
3172 return make_pointer_declarator (quals_attrs, inner);
3174 /* Now we have a direct declarator, direct abstract declarator or
3175 nothing (which counts as a direct abstract declarator here). */
3176 return c_parser_direct_declarator (parser, type_seen_p, kind, seen_id);
3179 /* Parse a direct declarator or direct abstract declarator; arguments
3180 as c_parser_declarator. */
3182 static struct c_declarator *
3183 c_parser_direct_declarator (c_parser *parser, bool type_seen_p, c_dtr_syn kind,
3184 bool *seen_id)
3186 /* The direct declarator must start with an identifier (possibly
3187 omitted) or a parenthesized declarator (possibly abstract). In
3188 an ordinary declarator, initial parentheses must start a
3189 parenthesized declarator. In an abstract declarator or parameter
3190 declarator, they could start a parenthesized declarator or a
3191 parameter list. To tell which, the open parenthesis and any
3192 following attributes must be read. If a declaration specifier
3193 follows, then it is a parameter list; if the specifier is a
3194 typedef name, there might be an ambiguity about redeclaring it,
3195 which is resolved in the direction of treating it as a typedef
3196 name. If a close parenthesis follows, it is also an empty
3197 parameter list, as the syntax does not permit empty abstract
3198 declarators. Otherwise, it is a parenthesized declarator (in
3199 which case the analysis may be repeated inside it, recursively).
3201 ??? There is an ambiguity in a parameter declaration "int
3202 (__attribute__((foo)) x)", where x is not a typedef name: it
3203 could be an abstract declarator for a function, or declare x with
3204 parentheses. The proper resolution of this ambiguity needs
3205 documenting. At present we follow an accident of the old
3206 parser's implementation, whereby the first parameter must have
3207 some declaration specifiers other than just attributes. Thus as
3208 a parameter declaration it is treated as a parenthesized
3209 parameter named x, and as an abstract declarator it is
3210 rejected.
3212 ??? Also following the old parser, attributes inside an empty
3213 parameter list are ignored, making it a list not yielding a
3214 prototype, rather than giving an error or making it have one
3215 parameter with implicit type int.
3217 ??? Also following the old parser, typedef names may be
3218 redeclared in declarators, but not Objective-C class names. */
3220 if (kind != C_DTR_ABSTRACT
3221 && c_parser_next_token_is (parser, CPP_NAME)
3222 && ((type_seen_p
3223 && (c_parser_peek_token (parser)->id_kind == C_ID_TYPENAME
3224 || c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME))
3225 || c_parser_peek_token (parser)->id_kind == C_ID_ID))
3227 struct c_declarator *inner
3228 = build_id_declarator (c_parser_peek_token (parser)->value);
3229 *seen_id = true;
3230 inner->id_loc = c_parser_peek_token (parser)->location;
3231 c_parser_consume_token (parser);
3232 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
3235 if (kind != C_DTR_NORMAL
3236 && c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
3238 struct c_declarator *inner = build_id_declarator (NULL_TREE);
3239 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
3242 /* Either we are at the end of an abstract declarator, or we have
3243 parentheses. */
3245 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
3247 tree attrs;
3248 struct c_declarator *inner;
3249 c_parser_consume_token (parser);
3250 attrs = c_parser_attributes (parser);
3251 if (kind != C_DTR_NORMAL
3252 && (c_parser_next_token_starts_declspecs (parser)
3253 || c_parser_next_token_is (parser, CPP_CLOSE_PAREN)))
3255 struct c_arg_info *args
3256 = c_parser_parms_declarator (parser, kind == C_DTR_NORMAL,
3257 attrs);
3258 if (args == NULL)
3259 return NULL;
3260 else
3262 inner
3263 = build_function_declarator (args,
3264 build_id_declarator (NULL_TREE));
3265 return c_parser_direct_declarator_inner (parser, *seen_id,
3266 inner);
3269 /* A parenthesized declarator. */
3270 inner = c_parser_declarator (parser, type_seen_p, kind, seen_id);
3271 if (inner != NULL && attrs != NULL)
3272 inner = build_attrs_declarator (attrs, inner);
3273 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3275 c_parser_consume_token (parser);
3276 if (inner == NULL)
3277 return NULL;
3278 else
3279 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
3281 else
3283 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3284 "expected %<)%>");
3285 return NULL;
3288 else
3290 if (kind == C_DTR_NORMAL)
3292 c_parser_error (parser, "expected identifier or %<(%>");
3293 return NULL;
3295 else
3296 return build_id_declarator (NULL_TREE);
3300 /* Parse part of a direct declarator or direct abstract declarator,
3301 given that some (in INNER) has already been parsed; ID_PRESENT is
3302 true if an identifier is present, false for an abstract
3303 declarator. */
3305 static struct c_declarator *
3306 c_parser_direct_declarator_inner (c_parser *parser, bool id_present,
3307 struct c_declarator *inner)
3309 /* Parse a sequence of array declarators and parameter lists. */
3310 if (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
3312 location_t brace_loc = c_parser_peek_token (parser)->location;
3313 struct c_declarator *declarator;
3314 struct c_declspecs *quals_attrs = build_null_declspecs ();
3315 bool static_seen;
3316 bool star_seen;
3317 struct c_expr dimen;
3318 dimen.value = NULL_TREE;
3319 dimen.original_code = ERROR_MARK;
3320 dimen.original_type = NULL_TREE;
3321 c_parser_consume_token (parser);
3322 c_parser_declspecs (parser, quals_attrs, false, false, true,
3323 false, false, cla_prefer_id);
3324 static_seen = c_parser_next_token_is_keyword (parser, RID_STATIC);
3325 if (static_seen)
3326 c_parser_consume_token (parser);
3327 if (static_seen && !quals_attrs->declspecs_seen_p)
3328 c_parser_declspecs (parser, quals_attrs, false, false, true,
3329 false, false, cla_prefer_id);
3330 if (!quals_attrs->declspecs_seen_p)
3331 quals_attrs = NULL;
3332 /* If "static" is present, there must be an array dimension.
3333 Otherwise, there may be a dimension, "*", or no
3334 dimension. */
3335 if (static_seen)
3337 star_seen = false;
3338 dimen = c_parser_expr_no_commas (parser, NULL);
3340 else
3342 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
3344 dimen.value = NULL_TREE;
3345 star_seen = false;
3347 else if (flag_cilkplus
3348 && c_parser_next_token_is (parser, CPP_COLON))
3350 dimen.value = error_mark_node;
3351 star_seen = false;
3352 error_at (c_parser_peek_token (parser)->location,
3353 "array notations cannot be used in declaration");
3354 c_parser_consume_token (parser);
3356 else if (c_parser_next_token_is (parser, CPP_MULT))
3358 if (c_parser_peek_2nd_token (parser)->type == CPP_CLOSE_SQUARE)
3360 dimen.value = NULL_TREE;
3361 star_seen = true;
3362 c_parser_consume_token (parser);
3364 else
3366 star_seen = false;
3367 dimen = c_parser_expr_no_commas (parser, NULL);
3370 else
3372 star_seen = false;
3373 dimen = c_parser_expr_no_commas (parser, NULL);
3376 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
3377 c_parser_consume_token (parser);
3378 else if (flag_cilkplus
3379 && c_parser_next_token_is (parser, CPP_COLON))
3381 error_at (c_parser_peek_token (parser)->location,
3382 "array notations cannot be used in declaration");
3383 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
3384 return NULL;
3386 else
3388 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
3389 "expected %<]%>");
3390 return NULL;
3392 if (dimen.value)
3393 dimen = convert_lvalue_to_rvalue (brace_loc, dimen, true, true);
3394 declarator = build_array_declarator (brace_loc, dimen.value, quals_attrs,
3395 static_seen, star_seen);
3396 if (declarator == NULL)
3397 return NULL;
3398 inner = set_array_declarator_inner (declarator, inner);
3399 return c_parser_direct_declarator_inner (parser, id_present, inner);
3401 else if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
3403 tree attrs;
3404 struct c_arg_info *args;
3405 c_parser_consume_token (parser);
3406 attrs = c_parser_attributes (parser);
3407 args = c_parser_parms_declarator (parser, id_present, attrs);
3408 if (args == NULL)
3409 return NULL;
3410 else
3412 inner = build_function_declarator (args, inner);
3413 return c_parser_direct_declarator_inner (parser, id_present, inner);
3416 return inner;
3419 /* Parse a parameter list or identifier list, including the closing
3420 parenthesis but not the opening one. ATTRS are the attributes at
3421 the start of the list. ID_LIST_OK is true if an identifier list is
3422 acceptable; such a list must not have attributes at the start. */
3424 static struct c_arg_info *
3425 c_parser_parms_declarator (c_parser *parser, bool id_list_ok, tree attrs)
3427 push_scope ();
3428 declare_parm_level ();
3429 /* If the list starts with an identifier, it is an identifier list.
3430 Otherwise, it is either a prototype list or an empty list. */
3431 if (id_list_ok
3432 && !attrs
3433 && c_parser_next_token_is (parser, CPP_NAME)
3434 && c_parser_peek_token (parser)->id_kind == C_ID_ID
3436 /* Look ahead to detect typos in type names. */
3437 && c_parser_peek_2nd_token (parser)->type != CPP_NAME
3438 && c_parser_peek_2nd_token (parser)->type != CPP_MULT
3439 && c_parser_peek_2nd_token (parser)->type != CPP_OPEN_PAREN
3440 && c_parser_peek_2nd_token (parser)->type != CPP_OPEN_SQUARE)
3442 tree list = NULL_TREE, *nextp = &list;
3443 while (c_parser_next_token_is (parser, CPP_NAME)
3444 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
3446 *nextp = build_tree_list (NULL_TREE,
3447 c_parser_peek_token (parser)->value);
3448 nextp = & TREE_CHAIN (*nextp);
3449 c_parser_consume_token (parser);
3450 if (c_parser_next_token_is_not (parser, CPP_COMMA))
3451 break;
3452 c_parser_consume_token (parser);
3453 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3455 c_parser_error (parser, "expected identifier");
3456 break;
3459 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3461 struct c_arg_info *ret = build_arg_info ();
3462 ret->types = list;
3463 c_parser_consume_token (parser);
3464 pop_scope ();
3465 return ret;
3467 else
3469 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3470 "expected %<)%>");
3471 pop_scope ();
3472 return NULL;
3475 else
3477 struct c_arg_info *ret = c_parser_parms_list_declarator (parser, attrs,
3478 NULL);
3479 pop_scope ();
3480 return ret;
3484 /* Parse a parameter list (possibly empty), including the closing
3485 parenthesis but not the opening one. ATTRS are the attributes at
3486 the start of the list. EXPR is NULL or an expression that needs to
3487 be evaluated for the side effects of array size expressions in the
3488 parameters. */
3490 static struct c_arg_info *
3491 c_parser_parms_list_declarator (c_parser *parser, tree attrs, tree expr)
3493 bool bad_parm = false;
3495 /* ??? Following the old parser, forward parameter declarations may
3496 use abstract declarators, and if no real parameter declarations
3497 follow the forward declarations then this is not diagnosed. Also
3498 note as above that attributes are ignored as the only contents of
3499 the parentheses, or as the only contents after forward
3500 declarations. */
3501 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3503 struct c_arg_info *ret = build_arg_info ();
3504 c_parser_consume_token (parser);
3505 return ret;
3507 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
3509 struct c_arg_info *ret = build_arg_info ();
3511 if (flag_allow_parameterless_variadic_functions)
3513 /* F (...) is allowed. */
3514 ret->types = NULL_TREE;
3516 else
3518 /* Suppress -Wold-style-definition for this case. */
3519 ret->types = error_mark_node;
3520 error_at (c_parser_peek_token (parser)->location,
3521 "ISO C requires a named argument before %<...%>");
3523 c_parser_consume_token (parser);
3524 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3526 c_parser_consume_token (parser);
3527 return ret;
3529 else
3531 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3532 "expected %<)%>");
3533 return NULL;
3536 /* Nonempty list of parameters, either terminated with semicolon
3537 (forward declarations; recurse) or with close parenthesis (normal
3538 function) or with ", ... )" (variadic function). */
3539 while (true)
3541 /* Parse a parameter. */
3542 struct c_parm *parm = c_parser_parameter_declaration (parser, attrs);
3543 attrs = NULL_TREE;
3544 if (parm == NULL)
3545 bad_parm = true;
3546 else
3547 push_parm_decl (parm, &expr);
3548 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
3550 tree new_attrs;
3551 c_parser_consume_token (parser);
3552 mark_forward_parm_decls ();
3553 new_attrs = c_parser_attributes (parser);
3554 return c_parser_parms_list_declarator (parser, new_attrs, expr);
3556 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3558 c_parser_consume_token (parser);
3559 if (bad_parm)
3560 return NULL;
3561 else
3562 return get_parm_info (false, expr);
3564 if (!c_parser_require (parser, CPP_COMMA,
3565 "expected %<;%>, %<,%> or %<)%>"))
3567 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
3568 return NULL;
3570 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
3572 c_parser_consume_token (parser);
3573 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3575 c_parser_consume_token (parser);
3576 if (bad_parm)
3577 return NULL;
3578 else
3579 return get_parm_info (true, expr);
3581 else
3583 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3584 "expected %<)%>");
3585 return NULL;
3591 /* Parse a parameter declaration. ATTRS are the attributes at the
3592 start of the declaration if it is the first parameter. */
3594 static struct c_parm *
3595 c_parser_parameter_declaration (c_parser *parser, tree attrs)
3597 struct c_declspecs *specs;
3598 struct c_declarator *declarator;
3599 tree prefix_attrs;
3600 tree postfix_attrs = NULL_TREE;
3601 bool dummy = false;
3603 /* Accept #pragmas between parameter declarations. */
3604 while (c_parser_next_token_is (parser, CPP_PRAGMA))
3605 c_parser_pragma (parser, pragma_param);
3607 if (!c_parser_next_token_starts_declspecs (parser))
3609 c_token *token = c_parser_peek_token (parser);
3610 if (parser->error)
3611 return NULL;
3612 c_parser_set_source_position_from_token (token);
3613 if (c_parser_next_tokens_start_typename (parser, cla_prefer_type))
3615 error_at (token->location, "unknown type name %qE", token->value);
3616 parser->error = true;
3618 /* ??? In some Objective-C cases '...' isn't applicable so there
3619 should be a different message. */
3620 else
3621 c_parser_error (parser,
3622 "expected declaration specifiers or %<...%>");
3623 c_parser_skip_to_end_of_parameter (parser);
3624 return NULL;
3626 specs = build_null_declspecs ();
3627 if (attrs)
3629 declspecs_add_attrs (input_location, specs, attrs);
3630 attrs = NULL_TREE;
3632 c_parser_declspecs (parser, specs, true, true, true, true, false,
3633 cla_nonabstract_decl);
3634 finish_declspecs (specs);
3635 pending_xref_error ();
3636 prefix_attrs = specs->attrs;
3637 specs->attrs = NULL_TREE;
3638 declarator = c_parser_declarator (parser,
3639 specs->typespec_kind != ctsk_none,
3640 C_DTR_PARM, &dummy);
3641 if (declarator == NULL)
3643 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
3644 return NULL;
3646 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
3647 postfix_attrs = c_parser_attributes (parser);
3648 return build_c_parm (specs, chainon (postfix_attrs, prefix_attrs),
3649 declarator);
3652 /* Parse a string literal in an asm expression. It should not be
3653 translated, and wide string literals are an error although
3654 permitted by the syntax. This is a GNU extension.
3656 asm-string-literal:
3657 string-literal
3659 ??? At present, following the old parser, the caller needs to have
3660 set lex_untranslated_string to 1. It would be better to follow the
3661 C++ parser rather than using this kludge. */
3663 static tree
3664 c_parser_asm_string_literal (c_parser *parser)
3666 tree str;
3667 int save_flag = warn_overlength_strings;
3668 warn_overlength_strings = 0;
3669 if (c_parser_next_token_is (parser, CPP_STRING))
3671 str = c_parser_peek_token (parser)->value;
3672 c_parser_consume_token (parser);
3674 else if (c_parser_next_token_is (parser, CPP_WSTRING))
3676 error_at (c_parser_peek_token (parser)->location,
3677 "wide string literal in %<asm%>");
3678 str = build_string (1, "");
3679 c_parser_consume_token (parser);
3681 else
3683 c_parser_error (parser, "expected string literal");
3684 str = NULL_TREE;
3686 warn_overlength_strings = save_flag;
3687 return str;
3690 /* Parse a simple asm expression. This is used in restricted
3691 contexts, where a full expression with inputs and outputs does not
3692 make sense. This is a GNU extension.
3694 simple-asm-expr:
3695 asm ( asm-string-literal )
3698 static tree
3699 c_parser_simple_asm_expr (c_parser *parser)
3701 tree str;
3702 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ASM));
3703 /* ??? Follow the C++ parser rather than using the
3704 lex_untranslated_string kludge. */
3705 parser->lex_untranslated_string = true;
3706 c_parser_consume_token (parser);
3707 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3709 parser->lex_untranslated_string = false;
3710 return NULL_TREE;
3712 str = c_parser_asm_string_literal (parser);
3713 parser->lex_untranslated_string = false;
3714 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
3716 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
3717 return NULL_TREE;
3719 return str;
3722 static tree
3723 c_parser_attribute_any_word (c_parser *parser)
3725 tree attr_name = NULL_TREE;
3727 if (c_parser_next_token_is (parser, CPP_KEYWORD))
3729 /* ??? See comment above about what keywords are accepted here. */
3730 bool ok;
3731 switch (c_parser_peek_token (parser)->keyword)
3733 case RID_STATIC:
3734 case RID_UNSIGNED:
3735 case RID_LONG:
3736 case RID_INT128:
3737 case RID_CONST:
3738 case RID_EXTERN:
3739 case RID_REGISTER:
3740 case RID_TYPEDEF:
3741 case RID_SHORT:
3742 case RID_INLINE:
3743 case RID_NORETURN:
3744 case RID_VOLATILE:
3745 case RID_SIGNED:
3746 case RID_AUTO:
3747 case RID_RESTRICT:
3748 case RID_COMPLEX:
3749 case RID_THREAD:
3750 case RID_INT:
3751 case RID_CHAR:
3752 case RID_FLOAT:
3753 case RID_DOUBLE:
3754 case RID_VOID:
3755 case RID_DFLOAT32:
3756 case RID_DFLOAT64:
3757 case RID_DFLOAT128:
3758 case RID_BOOL:
3759 case RID_FRACT:
3760 case RID_ACCUM:
3761 case RID_SAT:
3762 case RID_TRANSACTION_ATOMIC:
3763 case RID_TRANSACTION_CANCEL:
3764 case RID_ATOMIC:
3765 case RID_AUTO_TYPE:
3766 ok = true;
3767 break;
3768 default:
3769 ok = false;
3770 break;
3772 if (!ok)
3773 return NULL_TREE;
3775 /* Accept __attribute__((__const)) as __attribute__((const)) etc. */
3776 attr_name = ridpointers[(int) c_parser_peek_token (parser)->keyword];
3778 else if (c_parser_next_token_is (parser, CPP_NAME))
3779 attr_name = c_parser_peek_token (parser)->value;
3781 return attr_name;
3784 /* Returns true of NAME is an IDENTIFIER_NODE with identiifer "vector,"
3785 "__vector" or "__vector__." */
3787 static inline bool
3788 is_cilkplus_vector_p (tree name)
3790 if (flag_cilkplus && is_attribute_p ("vector", name))
3791 return true;
3792 return false;
3795 #define CILK_SIMD_FN_CLAUSE_MASK \
3796 ((OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_VECTORLENGTH) \
3797 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_LINEAR) \
3798 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_UNIFORM) \
3799 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_MASK) \
3800 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_NOMASK))
3802 /* Parses the vector attribute of SIMD enabled functions in Cilk Plus.
3803 VEC_TOKEN is the "vector" token that is replaced with "simd" and
3804 pushed into the token list.
3805 Syntax:
3806 vector
3807 vector (<vector attributes>). */
3809 static void
3810 c_parser_cilk_simd_fn_vector_attrs (c_parser *parser, c_token vec_token)
3812 gcc_assert (is_cilkplus_vector_p (vec_token.value));
3814 int paren_scope = 0;
3815 vec_safe_push (parser->cilk_simd_fn_tokens, vec_token);
3816 /* Consume the "vector" token. */
3817 c_parser_consume_token (parser);
3819 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
3821 c_parser_consume_token (parser);
3822 paren_scope++;
3824 while (paren_scope > 0)
3826 c_token *token = c_parser_peek_token (parser);
3827 if (token->type == CPP_OPEN_PAREN)
3828 paren_scope++;
3829 else if (token->type == CPP_CLOSE_PAREN)
3830 paren_scope--;
3831 /* Do not push the last ')' since we are not pushing the '('. */
3832 if (!(token->type == CPP_CLOSE_PAREN && paren_scope == 0))
3833 vec_safe_push (parser->cilk_simd_fn_tokens, *token);
3834 c_parser_consume_token (parser);
3837 /* Since we are converting an attribute to a pragma, we need to end the
3838 attribute with PRAGMA_EOL. */
3839 c_token eol_token;
3840 memset (&eol_token, 0, sizeof (eol_token));
3841 eol_token.type = CPP_PRAGMA_EOL;
3842 vec_safe_push (parser->cilk_simd_fn_tokens, eol_token);
3845 /* Add 2 CPP_EOF at the end of PARSER->ELEM_FN_TOKENS vector. */
3847 static void
3848 c_finish_cilk_simd_fn_tokens (c_parser *parser)
3850 c_token last_token = parser->cilk_simd_fn_tokens->last ();
3852 /* c_parser_attributes is called in several places, so if these EOF
3853 tokens are already inserted, then don't do them again. */
3854 if (last_token.type == CPP_EOF)
3855 return;
3857 /* Two CPP_EOF token are added as a safety net since the normal C
3858 front-end has two token look-ahead. */
3859 c_token eof_token;
3860 eof_token.type = CPP_EOF;
3861 vec_safe_push (parser->cilk_simd_fn_tokens, eof_token);
3862 vec_safe_push (parser->cilk_simd_fn_tokens, eof_token);
3865 /* Parse (possibly empty) attributes. This is a GNU extension.
3867 attributes:
3868 empty
3869 attributes attribute
3871 attribute:
3872 __attribute__ ( ( attribute-list ) )
3874 attribute-list:
3875 attrib
3876 attribute_list , attrib
3878 attrib:
3879 empty
3880 any-word
3881 any-word ( identifier )
3882 any-word ( identifier , nonempty-expr-list )
3883 any-word ( expr-list )
3885 where the "identifier" must not be declared as a type, and
3886 "any-word" may be any identifier (including one declared as a
3887 type), a reserved word storage class specifier, type specifier or
3888 type qualifier. ??? This still leaves out most reserved keywords
3889 (following the old parser), shouldn't we include them, and why not
3890 allow identifiers declared as types to start the arguments? */
3892 static tree
3893 c_parser_attributes (c_parser *parser)
3895 tree attrs = NULL_TREE;
3896 while (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
3898 /* ??? Follow the C++ parser rather than using the
3899 lex_untranslated_string kludge. */
3900 parser->lex_untranslated_string = true;
3901 c_parser_consume_token (parser);
3902 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3904 parser->lex_untranslated_string = false;
3905 return attrs;
3907 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3909 parser->lex_untranslated_string = false;
3910 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
3911 return attrs;
3913 /* Parse the attribute list. */
3914 while (c_parser_next_token_is (parser, CPP_COMMA)
3915 || c_parser_next_token_is (parser, CPP_NAME)
3916 || c_parser_next_token_is (parser, CPP_KEYWORD))
3918 tree attr, attr_name, attr_args;
3919 vec<tree, va_gc> *expr_list;
3920 if (c_parser_next_token_is (parser, CPP_COMMA))
3922 c_parser_consume_token (parser);
3923 continue;
3926 attr_name = c_parser_attribute_any_word (parser);
3927 if (attr_name == NULL)
3928 break;
3929 if (is_cilkplus_vector_p (attr_name))
3931 c_token *v_token = c_parser_peek_token (parser);
3932 c_parser_cilk_simd_fn_vector_attrs (parser, *v_token);
3933 continue;
3935 c_parser_consume_token (parser);
3936 if (c_parser_next_token_is_not (parser, CPP_OPEN_PAREN))
3938 attr = build_tree_list (attr_name, NULL_TREE);
3939 attrs = chainon (attrs, attr);
3940 continue;
3942 c_parser_consume_token (parser);
3943 /* Parse the attribute contents. If they start with an
3944 identifier which is followed by a comma or close
3945 parenthesis, then the arguments start with that
3946 identifier; otherwise they are an expression list.
3947 In objective-c the identifier may be a classname. */
3948 if (c_parser_next_token_is (parser, CPP_NAME)
3949 && (c_parser_peek_token (parser)->id_kind == C_ID_ID
3950 || (c_dialect_objc ()
3951 && c_parser_peek_token (parser)->id_kind
3952 == C_ID_CLASSNAME))
3953 && ((c_parser_peek_2nd_token (parser)->type == CPP_COMMA)
3954 || (c_parser_peek_2nd_token (parser)->type
3955 == CPP_CLOSE_PAREN))
3956 && (attribute_takes_identifier_p (attr_name)
3957 || (c_dialect_objc ()
3958 && c_parser_peek_token (parser)->id_kind
3959 == C_ID_CLASSNAME)))
3961 tree arg1 = c_parser_peek_token (parser)->value;
3962 c_parser_consume_token (parser);
3963 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3964 attr_args = build_tree_list (NULL_TREE, arg1);
3965 else
3967 tree tree_list;
3968 c_parser_consume_token (parser);
3969 expr_list = c_parser_expr_list (parser, false, true,
3970 NULL, NULL, NULL, NULL);
3971 tree_list = build_tree_list_vec (expr_list);
3972 attr_args = tree_cons (NULL_TREE, arg1, tree_list);
3973 release_tree_vector (expr_list);
3976 else
3978 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3979 attr_args = NULL_TREE;
3980 else
3982 expr_list = c_parser_expr_list (parser, false, true,
3983 NULL, NULL, NULL, NULL);
3984 attr_args = build_tree_list_vec (expr_list);
3985 release_tree_vector (expr_list);
3988 attr = build_tree_list (attr_name, attr_args);
3989 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3990 c_parser_consume_token (parser);
3991 else
3993 parser->lex_untranslated_string = false;
3994 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3995 "expected %<)%>");
3996 return attrs;
3998 attrs = chainon (attrs, attr);
4000 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4001 c_parser_consume_token (parser);
4002 else
4004 parser->lex_untranslated_string = false;
4005 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
4006 "expected %<)%>");
4007 return attrs;
4009 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4010 c_parser_consume_token (parser);
4011 else
4013 parser->lex_untranslated_string = false;
4014 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
4015 "expected %<)%>");
4016 return attrs;
4018 parser->lex_untranslated_string = false;
4021 if (flag_cilkplus && !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
4022 c_finish_cilk_simd_fn_tokens (parser);
4023 return attrs;
4026 /* Parse a type name (C90 6.5.5, C99 6.7.6).
4028 type-name:
4029 specifier-qualifier-list abstract-declarator[opt]
4032 static struct c_type_name *
4033 c_parser_type_name (c_parser *parser)
4035 struct c_declspecs *specs = build_null_declspecs ();
4036 struct c_declarator *declarator;
4037 struct c_type_name *ret;
4038 bool dummy = false;
4039 c_parser_declspecs (parser, specs, false, true, true, false, false,
4040 cla_prefer_type);
4041 if (!specs->declspecs_seen_p)
4043 c_parser_error (parser, "expected specifier-qualifier-list");
4044 return NULL;
4046 if (specs->type != error_mark_node)
4048 pending_xref_error ();
4049 finish_declspecs (specs);
4051 declarator = c_parser_declarator (parser,
4052 specs->typespec_kind != ctsk_none,
4053 C_DTR_ABSTRACT, &dummy);
4054 if (declarator == NULL)
4055 return NULL;
4056 ret = XOBNEW (&parser_obstack, struct c_type_name);
4057 ret->specs = specs;
4058 ret->declarator = declarator;
4059 return ret;
4062 /* Parse an initializer (C90 6.5.7, C99 6.7.8).
4064 initializer:
4065 assignment-expression
4066 { initializer-list }
4067 { initializer-list , }
4069 initializer-list:
4070 designation[opt] initializer
4071 initializer-list , designation[opt] initializer
4073 designation:
4074 designator-list =
4076 designator-list:
4077 designator
4078 designator-list designator
4080 designator:
4081 array-designator
4082 . identifier
4084 array-designator:
4085 [ constant-expression ]
4087 GNU extensions:
4089 initializer:
4092 designation:
4093 array-designator
4094 identifier :
4096 array-designator:
4097 [ constant-expression ... constant-expression ]
4099 Any expression without commas is accepted in the syntax for the
4100 constant-expressions, with non-constant expressions rejected later.
4102 This function is only used for top-level initializers; for nested
4103 ones, see c_parser_initval. */
4105 static struct c_expr
4106 c_parser_initializer (c_parser *parser)
4108 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
4109 return c_parser_braced_init (parser, NULL_TREE, false);
4110 else
4112 struct c_expr ret;
4113 location_t loc = c_parser_peek_token (parser)->location;
4114 ret = c_parser_expr_no_commas (parser, NULL);
4115 if (TREE_CODE (ret.value) != STRING_CST
4116 && TREE_CODE (ret.value) != COMPOUND_LITERAL_EXPR)
4117 ret = convert_lvalue_to_rvalue (loc, ret, true, true);
4118 return ret;
4122 /* Parse a braced initializer list. TYPE is the type specified for a
4123 compound literal, and NULL_TREE for other initializers and for
4124 nested braced lists. NESTED_P is true for nested braced lists,
4125 false for the list of a compound literal or the list that is the
4126 top-level initializer in a declaration. */
4128 static struct c_expr
4129 c_parser_braced_init (c_parser *parser, tree type, bool nested_p)
4131 struct c_expr ret;
4132 struct obstack braced_init_obstack;
4133 location_t brace_loc = c_parser_peek_token (parser)->location;
4134 gcc_obstack_init (&braced_init_obstack);
4135 gcc_assert (c_parser_next_token_is (parser, CPP_OPEN_BRACE));
4136 c_parser_consume_token (parser);
4137 if (nested_p)
4138 push_init_level (brace_loc, 0, &braced_init_obstack);
4139 else
4140 really_start_incremental_init (type);
4141 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
4143 pedwarn (brace_loc, OPT_Wpedantic, "ISO C forbids empty initializer braces");
4145 else
4147 /* Parse a non-empty initializer list, possibly with a trailing
4148 comma. */
4149 while (true)
4151 c_parser_initelt (parser, &braced_init_obstack);
4152 if (parser->error)
4153 break;
4154 if (c_parser_next_token_is (parser, CPP_COMMA))
4155 c_parser_consume_token (parser);
4156 else
4157 break;
4158 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
4159 break;
4162 if (c_parser_next_token_is_not (parser, CPP_CLOSE_BRACE))
4164 ret.value = error_mark_node;
4165 ret.original_code = ERROR_MARK;
4166 ret.original_type = NULL;
4167 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, "expected %<}%>");
4168 pop_init_level (brace_loc, 0, &braced_init_obstack);
4169 obstack_free (&braced_init_obstack, NULL);
4170 return ret;
4172 c_parser_consume_token (parser);
4173 ret = pop_init_level (brace_loc, 0, &braced_init_obstack);
4174 obstack_free (&braced_init_obstack, NULL);
4175 return ret;
4178 /* Parse a nested initializer, including designators. */
4180 static void
4181 c_parser_initelt (c_parser *parser, struct obstack * braced_init_obstack)
4183 /* Parse any designator or designator list. A single array
4184 designator may have the subsequent "=" omitted in GNU C, but a
4185 longer list or a structure member designator may not. */
4186 if (c_parser_next_token_is (parser, CPP_NAME)
4187 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
4189 /* Old-style structure member designator. */
4190 set_init_label (c_parser_peek_token (parser)->location,
4191 c_parser_peek_token (parser)->value,
4192 braced_init_obstack);
4193 /* Use the colon as the error location. */
4194 pedwarn (c_parser_peek_2nd_token (parser)->location, OPT_Wpedantic,
4195 "obsolete use of designated initializer with %<:%>");
4196 c_parser_consume_token (parser);
4197 c_parser_consume_token (parser);
4199 else
4201 /* des_seen is 0 if there have been no designators, 1 if there
4202 has been a single array designator and 2 otherwise. */
4203 int des_seen = 0;
4204 /* Location of a designator. */
4205 location_t des_loc = UNKNOWN_LOCATION; /* Quiet warning. */
4206 while (c_parser_next_token_is (parser, CPP_OPEN_SQUARE)
4207 || c_parser_next_token_is (parser, CPP_DOT))
4209 int des_prev = des_seen;
4210 if (!des_seen)
4211 des_loc = c_parser_peek_token (parser)->location;
4212 if (des_seen < 2)
4213 des_seen++;
4214 if (c_parser_next_token_is (parser, CPP_DOT))
4216 des_seen = 2;
4217 c_parser_consume_token (parser);
4218 if (c_parser_next_token_is (parser, CPP_NAME))
4220 set_init_label (des_loc, c_parser_peek_token (parser)->value,
4221 braced_init_obstack);
4222 c_parser_consume_token (parser);
4224 else
4226 struct c_expr init;
4227 init.value = error_mark_node;
4228 init.original_code = ERROR_MARK;
4229 init.original_type = NULL;
4230 c_parser_error (parser, "expected identifier");
4231 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
4232 process_init_element (input_location, init, false,
4233 braced_init_obstack);
4234 return;
4237 else
4239 tree first, second;
4240 location_t ellipsis_loc = UNKNOWN_LOCATION; /* Quiet warning. */
4241 location_t array_index_loc = UNKNOWN_LOCATION;
4242 /* ??? Following the old parser, [ objc-receiver
4243 objc-message-args ] is accepted as an initializer,
4244 being distinguished from a designator by what follows
4245 the first assignment expression inside the square
4246 brackets, but after a first array designator a
4247 subsequent square bracket is for Objective-C taken to
4248 start an expression, using the obsolete form of
4249 designated initializer without '=', rather than
4250 possibly being a second level of designation: in LALR
4251 terms, the '[' is shifted rather than reducing
4252 designator to designator-list. */
4253 if (des_prev == 1 && c_dialect_objc ())
4255 des_seen = des_prev;
4256 break;
4258 if (des_prev == 0 && c_dialect_objc ())
4260 /* This might be an array designator or an
4261 Objective-C message expression. If the former,
4262 continue parsing here; if the latter, parse the
4263 remainder of the initializer given the starting
4264 primary-expression. ??? It might make sense to
4265 distinguish when des_prev == 1 as well; see
4266 previous comment. */
4267 tree rec, args;
4268 struct c_expr mexpr;
4269 c_parser_consume_token (parser);
4270 if (c_parser_peek_token (parser)->type == CPP_NAME
4271 && ((c_parser_peek_token (parser)->id_kind
4272 == C_ID_TYPENAME)
4273 || (c_parser_peek_token (parser)->id_kind
4274 == C_ID_CLASSNAME)))
4276 /* Type name receiver. */
4277 tree id = c_parser_peek_token (parser)->value;
4278 c_parser_consume_token (parser);
4279 rec = objc_get_class_reference (id);
4280 goto parse_message_args;
4282 first = c_parser_expr_no_commas (parser, NULL).value;
4283 mark_exp_read (first);
4284 if (c_parser_next_token_is (parser, CPP_ELLIPSIS)
4285 || c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
4286 goto array_desig_after_first;
4287 /* Expression receiver. So far only one part
4288 without commas has been parsed; there might be
4289 more of the expression. */
4290 rec = first;
4291 while (c_parser_next_token_is (parser, CPP_COMMA))
4293 struct c_expr next;
4294 location_t comma_loc, exp_loc;
4295 comma_loc = c_parser_peek_token (parser)->location;
4296 c_parser_consume_token (parser);
4297 exp_loc = c_parser_peek_token (parser)->location;
4298 next = c_parser_expr_no_commas (parser, NULL);
4299 next = convert_lvalue_to_rvalue (exp_loc, next,
4300 true, true);
4301 rec = build_compound_expr (comma_loc, rec, next.value);
4303 parse_message_args:
4304 /* Now parse the objc-message-args. */
4305 args = c_parser_objc_message_args (parser);
4306 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
4307 "expected %<]%>");
4308 mexpr.value
4309 = objc_build_message_expr (rec, args);
4310 mexpr.original_code = ERROR_MARK;
4311 mexpr.original_type = NULL;
4312 /* Now parse and process the remainder of the
4313 initializer, starting with this message
4314 expression as a primary-expression. */
4315 c_parser_initval (parser, &mexpr, braced_init_obstack);
4316 return;
4318 c_parser_consume_token (parser);
4319 array_index_loc = c_parser_peek_token (parser)->location;
4320 first = c_parser_expr_no_commas (parser, NULL).value;
4321 mark_exp_read (first);
4322 array_desig_after_first:
4323 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
4325 ellipsis_loc = c_parser_peek_token (parser)->location;
4326 c_parser_consume_token (parser);
4327 second = c_parser_expr_no_commas (parser, NULL).value;
4328 mark_exp_read (second);
4330 else
4331 second = NULL_TREE;
4332 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
4334 c_parser_consume_token (parser);
4335 set_init_index (array_index_loc, first, second,
4336 braced_init_obstack);
4337 if (second)
4338 pedwarn (ellipsis_loc, OPT_Wpedantic,
4339 "ISO C forbids specifying range of elements to initialize");
4341 else
4342 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
4343 "expected %<]%>");
4346 if (des_seen >= 1)
4348 if (c_parser_next_token_is (parser, CPP_EQ))
4350 if (!flag_isoc99)
4351 pedwarn (des_loc, OPT_Wpedantic,
4352 "ISO C90 forbids specifying subobject to initialize");
4353 c_parser_consume_token (parser);
4355 else
4357 if (des_seen == 1)
4358 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
4359 "obsolete use of designated initializer without %<=%>");
4360 else
4362 struct c_expr init;
4363 init.value = error_mark_node;
4364 init.original_code = ERROR_MARK;
4365 init.original_type = NULL;
4366 c_parser_error (parser, "expected %<=%>");
4367 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
4368 process_init_element (input_location, init, false,
4369 braced_init_obstack);
4370 return;
4375 c_parser_initval (parser, NULL, braced_init_obstack);
4378 /* Parse a nested initializer; as c_parser_initializer but parses
4379 initializers within braced lists, after any designators have been
4380 applied. If AFTER is not NULL then it is an Objective-C message
4381 expression which is the primary-expression starting the
4382 initializer. */
4384 static void
4385 c_parser_initval (c_parser *parser, struct c_expr *after,
4386 struct obstack * braced_init_obstack)
4388 struct c_expr init;
4389 gcc_assert (!after || c_dialect_objc ());
4390 location_t loc = c_parser_peek_token (parser)->location;
4392 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE) && !after)
4393 init = c_parser_braced_init (parser, NULL_TREE, true);
4394 else
4396 init = c_parser_expr_no_commas (parser, after);
4397 if (init.value != NULL_TREE
4398 && TREE_CODE (init.value) != STRING_CST
4399 && TREE_CODE (init.value) != COMPOUND_LITERAL_EXPR)
4400 init = convert_lvalue_to_rvalue (loc, init, true, true);
4402 process_init_element (loc, init, false, braced_init_obstack);
4405 /* Parse a compound statement (possibly a function body) (C90 6.6.2,
4406 C99 6.8.2).
4408 compound-statement:
4409 { block-item-list[opt] }
4410 { label-declarations block-item-list }
4412 block-item-list:
4413 block-item
4414 block-item-list block-item
4416 block-item:
4417 nested-declaration
4418 statement
4420 nested-declaration:
4421 declaration
4423 GNU extensions:
4425 compound-statement:
4426 { label-declarations block-item-list }
4428 nested-declaration:
4429 __extension__ nested-declaration
4430 nested-function-definition
4432 label-declarations:
4433 label-declaration
4434 label-declarations label-declaration
4436 label-declaration:
4437 __label__ identifier-list ;
4439 Allowing the mixing of declarations and code is new in C99. The
4440 GNU syntax also permits (not shown above) labels at the end of
4441 compound statements, which yield an error. We don't allow labels
4442 on declarations; this might seem like a natural extension, but
4443 there would be a conflict between attributes on the label and
4444 prefix attributes on the declaration. ??? The syntax follows the
4445 old parser in requiring something after label declarations.
4446 Although they are erroneous if the labels declared aren't defined,
4447 is it useful for the syntax to be this way?
4449 OpenMP:
4451 block-item:
4452 openmp-directive
4454 openmp-directive:
4455 barrier-directive
4456 flush-directive
4457 taskwait-directive
4458 taskyield-directive
4459 cancel-directive
4460 cancellation-point-directive */
4462 static tree
4463 c_parser_compound_statement (c_parser *parser)
4465 tree stmt;
4466 location_t brace_loc;
4467 brace_loc = c_parser_peek_token (parser)->location;
4468 if (!c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
4470 /* Ensure a scope is entered and left anyway to avoid confusion
4471 if we have just prepared to enter a function body. */
4472 stmt = c_begin_compound_stmt (true);
4473 c_end_compound_stmt (brace_loc, stmt, true);
4474 return error_mark_node;
4476 stmt = c_begin_compound_stmt (true);
4477 c_parser_compound_statement_nostart (parser);
4479 /* If the compound stmt contains array notations, then we expand them. */
4480 if (flag_cilkplus && contains_array_notation_expr (stmt))
4481 stmt = expand_array_notation_exprs (stmt);
4482 return c_end_compound_stmt (brace_loc, stmt, true);
4485 /* Parse a compound statement except for the opening brace. This is
4486 used for parsing both compound statements and statement expressions
4487 (which follow different paths to handling the opening). */
4489 static void
4490 c_parser_compound_statement_nostart (c_parser *parser)
4492 bool last_stmt = false;
4493 bool last_label = false;
4494 bool save_valid_for_pragma = valid_location_for_stdc_pragma_p ();
4495 location_t label_loc = UNKNOWN_LOCATION; /* Quiet warning. */
4496 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
4498 c_parser_consume_token (parser);
4499 return;
4501 mark_valid_location_for_stdc_pragma (true);
4502 if (c_parser_next_token_is_keyword (parser, RID_LABEL))
4504 /* Read zero or more forward-declarations for labels that nested
4505 functions can jump to. */
4506 mark_valid_location_for_stdc_pragma (false);
4507 while (c_parser_next_token_is_keyword (parser, RID_LABEL))
4509 label_loc = c_parser_peek_token (parser)->location;
4510 c_parser_consume_token (parser);
4511 /* Any identifiers, including those declared as type names,
4512 are OK here. */
4513 while (true)
4515 tree label;
4516 if (c_parser_next_token_is_not (parser, CPP_NAME))
4518 c_parser_error (parser, "expected identifier");
4519 break;
4521 label
4522 = declare_label (c_parser_peek_token (parser)->value);
4523 C_DECLARED_LABEL_FLAG (label) = 1;
4524 add_stmt (build_stmt (label_loc, DECL_EXPR, label));
4525 c_parser_consume_token (parser);
4526 if (c_parser_next_token_is (parser, CPP_COMMA))
4527 c_parser_consume_token (parser);
4528 else
4529 break;
4531 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
4533 pedwarn (label_loc, OPT_Wpedantic, "ISO C forbids label declarations");
4535 /* We must now have at least one statement, label or declaration. */
4536 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
4538 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
4539 c_parser_error (parser, "expected declaration or statement");
4540 c_parser_consume_token (parser);
4541 return;
4543 while (c_parser_next_token_is_not (parser, CPP_CLOSE_BRACE))
4545 location_t loc = c_parser_peek_token (parser)->location;
4546 if (c_parser_next_token_is_keyword (parser, RID_CASE)
4547 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
4548 || (c_parser_next_token_is (parser, CPP_NAME)
4549 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
4551 if (c_parser_next_token_is_keyword (parser, RID_CASE))
4552 label_loc = c_parser_peek_2nd_token (parser)->location;
4553 else
4554 label_loc = c_parser_peek_token (parser)->location;
4555 last_label = true;
4556 last_stmt = false;
4557 mark_valid_location_for_stdc_pragma (false);
4558 c_parser_label (parser);
4560 else if (!last_label
4561 && c_parser_next_tokens_start_declaration (parser))
4563 last_label = false;
4564 mark_valid_location_for_stdc_pragma (false);
4565 c_parser_declaration_or_fndef (parser, true, true, true, true,
4566 true, NULL, vNULL);
4567 if (last_stmt)
4568 pedwarn_c90 (loc,
4569 (pedantic && !flag_isoc99)
4570 ? OPT_Wpedantic
4571 : OPT_Wdeclaration_after_statement,
4572 "ISO C90 forbids mixed declarations and code");
4573 last_stmt = false;
4575 else if (!last_label
4576 && c_parser_next_token_is_keyword (parser, RID_EXTENSION))
4578 /* __extension__ can start a declaration, but is also an
4579 unary operator that can start an expression. Consume all
4580 but the last of a possible series of __extension__ to
4581 determine which. */
4582 while (c_parser_peek_2nd_token (parser)->type == CPP_KEYWORD
4583 && (c_parser_peek_2nd_token (parser)->keyword
4584 == RID_EXTENSION))
4585 c_parser_consume_token (parser);
4586 if (c_token_starts_declaration (c_parser_peek_2nd_token (parser)))
4588 int ext;
4589 ext = disable_extension_diagnostics ();
4590 c_parser_consume_token (parser);
4591 last_label = false;
4592 mark_valid_location_for_stdc_pragma (false);
4593 c_parser_declaration_or_fndef (parser, true, true, true, true,
4594 true, NULL, vNULL);
4595 /* Following the old parser, __extension__ does not
4596 disable this diagnostic. */
4597 restore_extension_diagnostics (ext);
4598 if (last_stmt)
4599 pedwarn_c90 (loc, (pedantic && !flag_isoc99)
4600 ? OPT_Wpedantic
4601 : OPT_Wdeclaration_after_statement,
4602 "ISO C90 forbids mixed declarations and code");
4603 last_stmt = false;
4605 else
4606 goto statement;
4608 else if (c_parser_next_token_is (parser, CPP_PRAGMA))
4610 /* External pragmas, and some omp pragmas, are not associated
4611 with regular c code, and so are not to be considered statements
4612 syntactically. This ensures that the user doesn't put them
4613 places that would turn into syntax errors if the directive
4614 were ignored. */
4615 if (c_parser_pragma (parser, pragma_compound))
4616 last_label = false, last_stmt = true;
4618 else if (c_parser_next_token_is (parser, CPP_EOF))
4620 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
4621 c_parser_error (parser, "expected declaration or statement");
4622 return;
4624 else if (c_parser_next_token_is_keyword (parser, RID_ELSE))
4626 if (parser->in_if_block)
4628 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
4629 error_at (loc, """expected %<}%> before %<else%>");
4630 return;
4632 else
4634 error_at (loc, "%<else%> without a previous %<if%>");
4635 c_parser_consume_token (parser);
4636 continue;
4639 else
4641 statement:
4642 last_label = false;
4643 last_stmt = true;
4644 mark_valid_location_for_stdc_pragma (false);
4645 c_parser_statement_after_labels (parser);
4648 parser->error = false;
4650 if (last_label)
4651 error_at (label_loc, "label at end of compound statement");
4652 c_parser_consume_token (parser);
4653 /* Restore the value we started with. */
4654 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
4657 /* Parse a label (C90 6.6.1, C99 6.8.1).
4659 label:
4660 identifier : attributes[opt]
4661 case constant-expression :
4662 default :
4664 GNU extensions:
4666 label:
4667 case constant-expression ... constant-expression :
4669 The use of attributes on labels is a GNU extension. The syntax in
4670 GNU C accepts any expressions without commas, non-constant
4671 expressions being rejected later. */
4673 static void
4674 c_parser_label (c_parser *parser)
4676 location_t loc1 = c_parser_peek_token (parser)->location;
4677 tree label = NULL_TREE;
4678 if (c_parser_next_token_is_keyword (parser, RID_CASE))
4680 tree exp1, exp2;
4681 c_parser_consume_token (parser);
4682 exp1 = c_parser_expr_no_commas (parser, NULL).value;
4683 if (c_parser_next_token_is (parser, CPP_COLON))
4685 c_parser_consume_token (parser);
4686 label = do_case (loc1, exp1, NULL_TREE);
4688 else if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
4690 c_parser_consume_token (parser);
4691 exp2 = c_parser_expr_no_commas (parser, NULL).value;
4692 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
4693 label = do_case (loc1, exp1, exp2);
4695 else
4696 c_parser_error (parser, "expected %<:%> or %<...%>");
4698 else if (c_parser_next_token_is_keyword (parser, RID_DEFAULT))
4700 c_parser_consume_token (parser);
4701 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
4702 label = do_case (loc1, NULL_TREE, NULL_TREE);
4704 else
4706 tree name = c_parser_peek_token (parser)->value;
4707 tree tlab;
4708 tree attrs;
4709 location_t loc2 = c_parser_peek_token (parser)->location;
4710 gcc_assert (c_parser_next_token_is (parser, CPP_NAME));
4711 c_parser_consume_token (parser);
4712 gcc_assert (c_parser_next_token_is (parser, CPP_COLON));
4713 c_parser_consume_token (parser);
4714 attrs = c_parser_attributes (parser);
4715 tlab = define_label (loc2, name);
4716 if (tlab)
4718 decl_attributes (&tlab, attrs, 0);
4719 label = add_stmt (build_stmt (loc1, LABEL_EXPR, tlab));
4722 if (label)
4724 if (c_parser_next_tokens_start_declaration (parser))
4726 error_at (c_parser_peek_token (parser)->location,
4727 "a label can only be part of a statement and "
4728 "a declaration is not a statement");
4729 c_parser_declaration_or_fndef (parser, /*fndef_ok*/ false,
4730 /*static_assert_ok*/ true,
4731 /*empty_ok*/ true, /*nested*/ true,
4732 /*start_attr_ok*/ true, NULL,
4733 vNULL);
4738 /* Parse a statement (C90 6.6, C99 6.8).
4740 statement:
4741 labeled-statement
4742 compound-statement
4743 expression-statement
4744 selection-statement
4745 iteration-statement
4746 jump-statement
4748 labeled-statement:
4749 label statement
4751 expression-statement:
4752 expression[opt] ;
4754 selection-statement:
4755 if-statement
4756 switch-statement
4758 iteration-statement:
4759 while-statement
4760 do-statement
4761 for-statement
4763 jump-statement:
4764 goto identifier ;
4765 continue ;
4766 break ;
4767 return expression[opt] ;
4769 GNU extensions:
4771 statement:
4772 asm-statement
4774 jump-statement:
4775 goto * expression ;
4777 Objective-C:
4779 statement:
4780 objc-throw-statement
4781 objc-try-catch-statement
4782 objc-synchronized-statement
4784 objc-throw-statement:
4785 @throw expression ;
4786 @throw ;
4788 OpenMP:
4790 statement:
4791 openmp-construct
4793 openmp-construct:
4794 parallel-construct
4795 for-construct
4796 simd-construct
4797 for-simd-construct
4798 sections-construct
4799 single-construct
4800 parallel-for-construct
4801 parallel-for-simd-construct
4802 parallel-sections-construct
4803 master-construct
4804 critical-construct
4805 atomic-construct
4806 ordered-construct
4808 parallel-construct:
4809 parallel-directive structured-block
4811 for-construct:
4812 for-directive iteration-statement
4814 simd-construct:
4815 simd-directive iteration-statements
4817 for-simd-construct:
4818 for-simd-directive iteration-statements
4820 sections-construct:
4821 sections-directive section-scope
4823 single-construct:
4824 single-directive structured-block
4826 parallel-for-construct:
4827 parallel-for-directive iteration-statement
4829 parallel-for-simd-construct:
4830 parallel-for-simd-directive iteration-statement
4832 parallel-sections-construct:
4833 parallel-sections-directive section-scope
4835 master-construct:
4836 master-directive structured-block
4838 critical-construct:
4839 critical-directive structured-block
4841 atomic-construct:
4842 atomic-directive expression-statement
4844 ordered-construct:
4845 ordered-directive structured-block
4847 Transactional Memory:
4849 statement:
4850 transaction-statement
4851 transaction-cancel-statement
4854 static void
4855 c_parser_statement (c_parser *parser)
4857 while (c_parser_next_token_is_keyword (parser, RID_CASE)
4858 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
4859 || (c_parser_next_token_is (parser, CPP_NAME)
4860 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
4861 c_parser_label (parser);
4862 c_parser_statement_after_labels (parser);
4865 /* Parse a statement, other than a labeled statement. */
4867 static void
4868 c_parser_statement_after_labels (c_parser *parser)
4870 location_t loc = c_parser_peek_token (parser)->location;
4871 tree stmt = NULL_TREE;
4872 bool in_if_block = parser->in_if_block;
4873 parser->in_if_block = false;
4874 switch (c_parser_peek_token (parser)->type)
4876 case CPP_OPEN_BRACE:
4877 add_stmt (c_parser_compound_statement (parser));
4878 break;
4879 case CPP_KEYWORD:
4880 switch (c_parser_peek_token (parser)->keyword)
4882 case RID_IF:
4883 c_parser_if_statement (parser);
4884 break;
4885 case RID_SWITCH:
4886 c_parser_switch_statement (parser);
4887 break;
4888 case RID_WHILE:
4889 c_parser_while_statement (parser, false);
4890 break;
4891 case RID_DO:
4892 c_parser_do_statement (parser, false);
4893 break;
4894 case RID_FOR:
4895 c_parser_for_statement (parser, false);
4896 break;
4897 case RID_CILK_SYNC:
4898 c_parser_consume_token (parser);
4899 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
4900 if (!flag_cilkplus)
4901 error_at (loc, "-fcilkplus must be enabled to use %<_Cilk_sync%>");
4902 else
4903 add_stmt (build_cilk_sync ());
4904 break;
4905 case RID_GOTO:
4906 c_parser_consume_token (parser);
4907 if (c_parser_next_token_is (parser, CPP_NAME))
4909 stmt = c_finish_goto_label (loc,
4910 c_parser_peek_token (parser)->value);
4911 c_parser_consume_token (parser);
4913 else if (c_parser_next_token_is (parser, CPP_MULT))
4915 struct c_expr val;
4917 c_parser_consume_token (parser);
4918 val = c_parser_expression (parser);
4919 val = convert_lvalue_to_rvalue (loc, val, false, true);
4920 stmt = c_finish_goto_ptr (loc, val.value);
4922 else
4923 c_parser_error (parser, "expected identifier or %<*%>");
4924 goto expect_semicolon;
4925 case RID_CONTINUE:
4926 c_parser_consume_token (parser);
4927 stmt = c_finish_bc_stmt (loc, &c_cont_label, false);
4928 goto expect_semicolon;
4929 case RID_BREAK:
4930 c_parser_consume_token (parser);
4931 stmt = c_finish_bc_stmt (loc, &c_break_label, true);
4932 goto expect_semicolon;
4933 case RID_RETURN:
4934 c_parser_consume_token (parser);
4935 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
4937 stmt = c_finish_return (loc, NULL_TREE, NULL_TREE);
4938 c_parser_consume_token (parser);
4940 else
4942 location_t xloc = c_parser_peek_token (parser)->location;
4943 struct c_expr expr = c_parser_expression_conv (parser);
4944 mark_exp_read (expr.value);
4945 stmt = c_finish_return (xloc, expr.value, expr.original_type);
4946 goto expect_semicolon;
4948 break;
4949 case RID_ASM:
4950 stmt = c_parser_asm_statement (parser);
4951 break;
4952 case RID_TRANSACTION_ATOMIC:
4953 case RID_TRANSACTION_RELAXED:
4954 stmt = c_parser_transaction (parser,
4955 c_parser_peek_token (parser)->keyword);
4956 break;
4957 case RID_TRANSACTION_CANCEL:
4958 stmt = c_parser_transaction_cancel (parser);
4959 goto expect_semicolon;
4960 case RID_AT_THROW:
4961 gcc_assert (c_dialect_objc ());
4962 c_parser_consume_token (parser);
4963 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
4965 stmt = objc_build_throw_stmt (loc, NULL_TREE);
4966 c_parser_consume_token (parser);
4968 else
4970 struct c_expr expr = c_parser_expression (parser);
4971 expr = convert_lvalue_to_rvalue (loc, expr, false, false);
4972 expr.value = c_fully_fold (expr.value, false, NULL);
4973 stmt = objc_build_throw_stmt (loc, expr.value);
4974 goto expect_semicolon;
4976 break;
4977 case RID_AT_TRY:
4978 gcc_assert (c_dialect_objc ());
4979 c_parser_objc_try_catch_finally_statement (parser);
4980 break;
4981 case RID_AT_SYNCHRONIZED:
4982 gcc_assert (c_dialect_objc ());
4983 c_parser_objc_synchronized_statement (parser);
4984 break;
4985 default:
4986 goto expr_stmt;
4988 break;
4989 case CPP_SEMICOLON:
4990 c_parser_consume_token (parser);
4991 break;
4992 case CPP_CLOSE_PAREN:
4993 case CPP_CLOSE_SQUARE:
4994 /* Avoid infinite loop in error recovery:
4995 c_parser_skip_until_found stops at a closing nesting
4996 delimiter without consuming it, but here we need to consume
4997 it to proceed further. */
4998 c_parser_error (parser, "expected statement");
4999 c_parser_consume_token (parser);
5000 break;
5001 case CPP_PRAGMA:
5002 c_parser_pragma (parser, pragma_stmt);
5003 break;
5004 default:
5005 expr_stmt:
5006 stmt = c_finish_expr_stmt (loc, c_parser_expression_conv (parser).value);
5007 expect_semicolon:
5008 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
5009 break;
5011 /* Two cases cannot and do not have line numbers associated: If stmt
5012 is degenerate, such as "2;", then stmt is an INTEGER_CST, which
5013 cannot hold line numbers. But that's OK because the statement
5014 will either be changed to a MODIFY_EXPR during gimplification of
5015 the statement expr, or discarded. If stmt was compound, but
5016 without new variables, we will have skipped the creation of a
5017 BIND and will have a bare STATEMENT_LIST. But that's OK because
5018 (recursively) all of the component statements should already have
5019 line numbers assigned. ??? Can we discard no-op statements
5020 earlier? */
5021 if (CAN_HAVE_LOCATION_P (stmt)
5022 && EXPR_LOCATION (stmt) == UNKNOWN_LOCATION)
5023 SET_EXPR_LOCATION (stmt, loc);
5025 parser->in_if_block = in_if_block;
5028 /* Parse the condition from an if, do, while or for statements. */
5030 static tree
5031 c_parser_condition (c_parser *parser)
5033 location_t loc = c_parser_peek_token (parser)->location;
5034 tree cond;
5035 cond = c_parser_expression_conv (parser).value;
5036 cond = c_objc_common_truthvalue_conversion (loc, cond);
5037 cond = c_fully_fold (cond, false, NULL);
5038 if (warn_sequence_point)
5039 verify_sequence_points (cond);
5040 return cond;
5043 /* Parse a parenthesized condition from an if, do or while statement.
5045 condition:
5046 ( expression )
5048 static tree
5049 c_parser_paren_condition (c_parser *parser)
5051 tree cond;
5052 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5053 return error_mark_node;
5054 cond = c_parser_condition (parser);
5055 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
5056 return cond;
5059 /* Parse a statement which is a block in C99. */
5061 static tree
5062 c_parser_c99_block_statement (c_parser *parser)
5064 tree block = c_begin_compound_stmt (flag_isoc99);
5065 location_t loc = c_parser_peek_token (parser)->location;
5066 c_parser_statement (parser);
5067 return c_end_compound_stmt (loc, block, flag_isoc99);
5070 /* Parse the body of an if statement. This is just parsing a
5071 statement but (a) it is a block in C99, (b) we track whether the
5072 body is an if statement for the sake of -Wparentheses warnings, (c)
5073 we handle an empty body specially for the sake of -Wempty-body
5074 warnings, and (d) we call parser_compound_statement directly
5075 because c_parser_statement_after_labels resets
5076 parser->in_if_block. */
5078 static tree
5079 c_parser_if_body (c_parser *parser, bool *if_p)
5081 tree block = c_begin_compound_stmt (flag_isoc99);
5082 location_t body_loc = c_parser_peek_token (parser)->location;
5083 while (c_parser_next_token_is_keyword (parser, RID_CASE)
5084 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
5085 || (c_parser_next_token_is (parser, CPP_NAME)
5086 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
5087 c_parser_label (parser);
5088 *if_p = c_parser_next_token_is_keyword (parser, RID_IF);
5089 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5091 location_t loc = c_parser_peek_token (parser)->location;
5092 add_stmt (build_empty_stmt (loc));
5093 c_parser_consume_token (parser);
5094 if (!c_parser_next_token_is_keyword (parser, RID_ELSE))
5095 warning_at (loc, OPT_Wempty_body,
5096 "suggest braces around empty body in an %<if%> statement");
5098 else if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
5099 add_stmt (c_parser_compound_statement (parser));
5100 else
5101 c_parser_statement_after_labels (parser);
5102 return c_end_compound_stmt (body_loc, block, flag_isoc99);
5105 /* Parse the else body of an if statement. This is just parsing a
5106 statement but (a) it is a block in C99, (b) we handle an empty body
5107 specially for the sake of -Wempty-body warnings. */
5109 static tree
5110 c_parser_else_body (c_parser *parser)
5112 location_t else_loc = c_parser_peek_token (parser)->location;
5113 tree block = c_begin_compound_stmt (flag_isoc99);
5114 while (c_parser_next_token_is_keyword (parser, RID_CASE)
5115 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
5116 || (c_parser_next_token_is (parser, CPP_NAME)
5117 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
5118 c_parser_label (parser);
5119 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5121 location_t loc = c_parser_peek_token (parser)->location;
5122 warning_at (loc,
5123 OPT_Wempty_body,
5124 "suggest braces around empty body in an %<else%> statement");
5125 add_stmt (build_empty_stmt (loc));
5126 c_parser_consume_token (parser);
5128 else
5129 c_parser_statement_after_labels (parser);
5130 return c_end_compound_stmt (else_loc, block, flag_isoc99);
5133 /* Parse an if statement (C90 6.6.4, C99 6.8.4).
5135 if-statement:
5136 if ( expression ) statement
5137 if ( expression ) statement else statement
5140 static void
5141 c_parser_if_statement (c_parser *parser)
5143 tree block;
5144 location_t loc;
5145 tree cond;
5146 bool first_if = false;
5147 tree first_body, second_body;
5148 bool in_if_block;
5149 tree if_stmt;
5151 gcc_assert (c_parser_next_token_is_keyword (parser, RID_IF));
5152 c_parser_consume_token (parser);
5153 block = c_begin_compound_stmt (flag_isoc99);
5154 loc = c_parser_peek_token (parser)->location;
5155 cond = c_parser_paren_condition (parser);
5156 in_if_block = parser->in_if_block;
5157 parser->in_if_block = true;
5158 first_body = c_parser_if_body (parser, &first_if);
5159 parser->in_if_block = in_if_block;
5160 if (c_parser_next_token_is_keyword (parser, RID_ELSE))
5162 c_parser_consume_token (parser);
5163 second_body = c_parser_else_body (parser);
5165 else
5166 second_body = NULL_TREE;
5167 c_finish_if_stmt (loc, cond, first_body, second_body, first_if);
5168 if_stmt = c_end_compound_stmt (loc, block, flag_isoc99);
5170 /* If the if statement contains array notations, then we expand them. */
5171 if (flag_cilkplus && contains_array_notation_expr (if_stmt))
5172 if_stmt = fix_conditional_array_notations (if_stmt);
5173 add_stmt (if_stmt);
5176 /* Parse a switch statement (C90 6.6.4, C99 6.8.4).
5178 switch-statement:
5179 switch (expression) statement
5182 static void
5183 c_parser_switch_statement (c_parser *parser)
5185 struct c_expr ce;
5186 tree block, expr, body, save_break;
5187 location_t switch_loc = c_parser_peek_token (parser)->location;
5188 location_t switch_cond_loc;
5189 gcc_assert (c_parser_next_token_is_keyword (parser, RID_SWITCH));
5190 c_parser_consume_token (parser);
5191 block = c_begin_compound_stmt (flag_isoc99);
5192 bool explicit_cast_p = false;
5193 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5195 switch_cond_loc = c_parser_peek_token (parser)->location;
5196 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
5197 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
5198 explicit_cast_p = true;
5199 ce = c_parser_expression (parser);
5200 ce = convert_lvalue_to_rvalue (switch_cond_loc, ce, true, false);
5201 expr = ce.value;
5202 if (flag_cilkplus && contains_array_notation_expr (expr))
5204 error_at (switch_cond_loc,
5205 "array notations cannot be used as a condition for switch "
5206 "statement");
5207 expr = error_mark_node;
5209 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
5211 else
5213 switch_cond_loc = UNKNOWN_LOCATION;
5214 expr = error_mark_node;
5216 c_start_case (switch_loc, switch_cond_loc, expr, explicit_cast_p);
5217 save_break = c_break_label;
5218 c_break_label = NULL_TREE;
5219 body = c_parser_c99_block_statement (parser);
5220 c_finish_case (body);
5221 if (c_break_label)
5223 location_t here = c_parser_peek_token (parser)->location;
5224 tree t = build1 (LABEL_EXPR, void_type_node, c_break_label);
5225 SET_EXPR_LOCATION (t, here);
5226 add_stmt (t);
5228 c_break_label = save_break;
5229 add_stmt (c_end_compound_stmt (switch_loc, block, flag_isoc99));
5232 /* Parse a while statement (C90 6.6.5, C99 6.8.5).
5234 while-statement:
5235 while (expression) statement
5238 static void
5239 c_parser_while_statement (c_parser *parser, bool ivdep)
5241 tree block, cond, body, save_break, save_cont;
5242 location_t loc;
5243 gcc_assert (c_parser_next_token_is_keyword (parser, RID_WHILE));
5244 c_parser_consume_token (parser);
5245 block = c_begin_compound_stmt (flag_isoc99);
5246 loc = c_parser_peek_token (parser)->location;
5247 cond = c_parser_paren_condition (parser);
5248 if (flag_cilkplus && contains_array_notation_expr (cond))
5250 error_at (loc, "array notations cannot be used as a condition for while "
5251 "statement");
5252 cond = error_mark_node;
5255 if (ivdep && cond != error_mark_node)
5256 cond = build2 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
5257 build_int_cst (integer_type_node,
5258 annot_expr_ivdep_kind));
5259 save_break = c_break_label;
5260 c_break_label = NULL_TREE;
5261 save_cont = c_cont_label;
5262 c_cont_label = NULL_TREE;
5263 body = c_parser_c99_block_statement (parser);
5264 c_finish_loop (loc, cond, NULL, body, c_break_label, c_cont_label, true);
5265 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99));
5266 c_break_label = save_break;
5267 c_cont_label = save_cont;
5270 /* Parse a do statement (C90 6.6.5, C99 6.8.5).
5272 do-statement:
5273 do statement while ( expression ) ;
5276 static void
5277 c_parser_do_statement (c_parser *parser, bool ivdep)
5279 tree block, cond, body, save_break, save_cont, new_break, new_cont;
5280 location_t loc;
5281 gcc_assert (c_parser_next_token_is_keyword (parser, RID_DO));
5282 c_parser_consume_token (parser);
5283 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5284 warning_at (c_parser_peek_token (parser)->location,
5285 OPT_Wempty_body,
5286 "suggest braces around empty body in %<do%> statement");
5287 block = c_begin_compound_stmt (flag_isoc99);
5288 loc = c_parser_peek_token (parser)->location;
5289 save_break = c_break_label;
5290 c_break_label = NULL_TREE;
5291 save_cont = c_cont_label;
5292 c_cont_label = NULL_TREE;
5293 body = c_parser_c99_block_statement (parser);
5294 c_parser_require_keyword (parser, RID_WHILE, "expected %<while%>");
5295 new_break = c_break_label;
5296 c_break_label = save_break;
5297 new_cont = c_cont_label;
5298 c_cont_label = save_cont;
5299 cond = c_parser_paren_condition (parser);
5300 if (flag_cilkplus && contains_array_notation_expr (cond))
5302 error_at (loc, "array notations cannot be used as a condition for a "
5303 "do-while statement");
5304 cond = error_mark_node;
5306 if (ivdep && cond != error_mark_node)
5307 cond = build2 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
5308 build_int_cst (integer_type_node,
5309 annot_expr_ivdep_kind));
5310 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
5311 c_parser_skip_to_end_of_block_or_statement (parser);
5312 c_finish_loop (loc, cond, NULL, body, new_break, new_cont, false);
5313 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99));
5316 /* Parse a for statement (C90 6.6.5, C99 6.8.5).
5318 for-statement:
5319 for ( expression[opt] ; expression[opt] ; expression[opt] ) statement
5320 for ( nested-declaration expression[opt] ; expression[opt] ) statement
5322 The form with a declaration is new in C99.
5324 ??? In accordance with the old parser, the declaration may be a
5325 nested function, which is then rejected in check_for_loop_decls,
5326 but does it make any sense for this to be included in the grammar?
5327 Note in particular that the nested function does not include a
5328 trailing ';', whereas the "declaration" production includes one.
5329 Also, can we reject bad declarations earlier and cheaper than
5330 check_for_loop_decls?
5332 In Objective-C, there are two additional variants:
5334 foreach-statement:
5335 for ( expression in expresssion ) statement
5336 for ( declaration in expression ) statement
5338 This is inconsistent with C, because the second variant is allowed
5339 even if c99 is not enabled.
5341 The rest of the comment documents these Objective-C foreach-statement.
5343 Here is the canonical example of the first variant:
5344 for (object in array) { do something with object }
5345 we call the first expression ("object") the "object_expression" and
5346 the second expression ("array") the "collection_expression".
5347 object_expression must be an lvalue of type "id" (a generic Objective-C
5348 object) because the loop works by assigning to object_expression the
5349 various objects from the collection_expression. collection_expression
5350 must evaluate to something of type "id" which responds to the method
5351 countByEnumeratingWithState:objects:count:.
5353 The canonical example of the second variant is:
5354 for (id object in array) { do something with object }
5355 which is completely equivalent to
5357 id object;
5358 for (object in array) { do something with object }
5360 Note that initizializing 'object' in some way (eg, "for ((object =
5361 xxx) in array) { do something with object }") is possibly
5362 technically valid, but completely pointless as 'object' will be
5363 assigned to something else as soon as the loop starts. We should
5364 most likely reject it (TODO).
5366 The beginning of the Objective-C foreach-statement looks exactly
5367 like the beginning of the for-statement, and we can tell it is a
5368 foreach-statement only because the initial declaration or
5369 expression is terminated by 'in' instead of ';'.
5372 static void
5373 c_parser_for_statement (c_parser *parser, bool ivdep)
5375 tree block, cond, incr, save_break, save_cont, body;
5376 /* The following are only used when parsing an ObjC foreach statement. */
5377 tree object_expression;
5378 /* Silence the bogus uninitialized warning. */
5379 tree collection_expression = NULL;
5380 location_t loc = c_parser_peek_token (parser)->location;
5381 location_t for_loc = c_parser_peek_token (parser)->location;
5382 bool is_foreach_statement = false;
5383 gcc_assert (c_parser_next_token_is_keyword (parser, RID_FOR));
5384 c_parser_consume_token (parser);
5385 /* Open a compound statement in Objective-C as well, just in case this is
5386 as foreach expression. */
5387 block = c_begin_compound_stmt (flag_isoc99 || c_dialect_objc ());
5388 cond = error_mark_node;
5389 incr = error_mark_node;
5390 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5392 /* Parse the initialization declaration or expression. */
5393 object_expression = error_mark_node;
5394 parser->objc_could_be_foreach_context = c_dialect_objc ();
5395 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5397 parser->objc_could_be_foreach_context = false;
5398 c_parser_consume_token (parser);
5399 c_finish_expr_stmt (loc, NULL_TREE);
5401 else if (c_parser_next_tokens_start_declaration (parser))
5403 c_parser_declaration_or_fndef (parser, true, true, true, true, true,
5404 &object_expression, vNULL);
5405 parser->objc_could_be_foreach_context = false;
5407 if (c_parser_next_token_is_keyword (parser, RID_IN))
5409 c_parser_consume_token (parser);
5410 is_foreach_statement = true;
5411 if (check_for_loop_decls (for_loc, true) == NULL_TREE)
5412 c_parser_error (parser, "multiple iterating variables in fast enumeration");
5414 else
5415 check_for_loop_decls (for_loc, flag_isoc99);
5417 else if (c_parser_next_token_is_keyword (parser, RID_EXTENSION))
5419 /* __extension__ can start a declaration, but is also an
5420 unary operator that can start an expression. Consume all
5421 but the last of a possible series of __extension__ to
5422 determine which. */
5423 while (c_parser_peek_2nd_token (parser)->type == CPP_KEYWORD
5424 && (c_parser_peek_2nd_token (parser)->keyword
5425 == RID_EXTENSION))
5426 c_parser_consume_token (parser);
5427 if (c_token_starts_declaration (c_parser_peek_2nd_token (parser)))
5429 int ext;
5430 ext = disable_extension_diagnostics ();
5431 c_parser_consume_token (parser);
5432 c_parser_declaration_or_fndef (parser, true, true, true, true,
5433 true, &object_expression, vNULL);
5434 parser->objc_could_be_foreach_context = false;
5436 restore_extension_diagnostics (ext);
5437 if (c_parser_next_token_is_keyword (parser, RID_IN))
5439 c_parser_consume_token (parser);
5440 is_foreach_statement = true;
5441 if (check_for_loop_decls (for_loc, true) == NULL_TREE)
5442 c_parser_error (parser, "multiple iterating variables in fast enumeration");
5444 else
5445 check_for_loop_decls (for_loc, flag_isoc99);
5447 else
5448 goto init_expr;
5450 else
5452 init_expr:
5454 struct c_expr ce;
5455 tree init_expression;
5456 ce = c_parser_expression (parser);
5457 init_expression = ce.value;
5458 parser->objc_could_be_foreach_context = false;
5459 if (c_parser_next_token_is_keyword (parser, RID_IN))
5461 c_parser_consume_token (parser);
5462 is_foreach_statement = true;
5463 if (! lvalue_p (init_expression))
5464 c_parser_error (parser, "invalid iterating variable in fast enumeration");
5465 object_expression = c_fully_fold (init_expression, false, NULL);
5467 else
5469 ce = convert_lvalue_to_rvalue (loc, ce, true, false);
5470 init_expression = ce.value;
5471 c_finish_expr_stmt (loc, init_expression);
5472 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
5476 /* Parse the loop condition. In the case of a foreach
5477 statement, there is no loop condition. */
5478 gcc_assert (!parser->objc_could_be_foreach_context);
5479 if (!is_foreach_statement)
5481 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5483 if (ivdep)
5485 c_parser_error (parser, "missing loop condition in loop with "
5486 "%<GCC ivdep%> pragma");
5487 cond = error_mark_node;
5489 else
5491 c_parser_consume_token (parser);
5492 cond = NULL_TREE;
5495 else
5497 cond = c_parser_condition (parser);
5498 if (flag_cilkplus && contains_array_notation_expr (cond))
5500 error_at (loc, "array notations cannot be used in a "
5501 "condition for a for-loop");
5502 cond = error_mark_node;
5504 c_parser_skip_until_found (parser, CPP_SEMICOLON,
5505 "expected %<;%>");
5507 if (ivdep && cond != error_mark_node)
5508 cond = build2 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
5509 build_int_cst (integer_type_node,
5510 annot_expr_ivdep_kind));
5512 /* Parse the increment expression (the third expression in a
5513 for-statement). In the case of a foreach-statement, this is
5514 the expression that follows the 'in'. */
5515 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
5517 if (is_foreach_statement)
5519 c_parser_error (parser, "missing collection in fast enumeration");
5520 collection_expression = error_mark_node;
5522 else
5523 incr = c_process_expr_stmt (loc, NULL_TREE);
5525 else
5527 if (is_foreach_statement)
5528 collection_expression = c_fully_fold (c_parser_expression (parser).value,
5529 false, NULL);
5530 else
5532 struct c_expr ce = c_parser_expression (parser);
5533 ce = convert_lvalue_to_rvalue (loc, ce, true, false);
5534 incr = c_process_expr_stmt (loc, ce.value);
5537 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
5539 save_break = c_break_label;
5540 c_break_label = NULL_TREE;
5541 save_cont = c_cont_label;
5542 c_cont_label = NULL_TREE;
5543 body = c_parser_c99_block_statement (parser);
5544 if (is_foreach_statement)
5545 objc_finish_foreach_loop (loc, object_expression, collection_expression, body, c_break_label, c_cont_label);
5546 else
5547 c_finish_loop (loc, cond, incr, body, c_break_label, c_cont_label, true);
5548 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99 || c_dialect_objc ()));
5549 c_break_label = save_break;
5550 c_cont_label = save_cont;
5553 /* Parse an asm statement, a GNU extension. This is a full-blown asm
5554 statement with inputs, outputs, clobbers, and volatile tag
5555 allowed.
5557 asm-statement:
5558 asm type-qualifier[opt] ( asm-argument ) ;
5559 asm type-qualifier[opt] goto ( asm-goto-argument ) ;
5561 asm-argument:
5562 asm-string-literal
5563 asm-string-literal : asm-operands[opt]
5564 asm-string-literal : asm-operands[opt] : asm-operands[opt]
5565 asm-string-literal : asm-operands[opt] : asm-operands[opt] : asm-clobbers[opt]
5567 asm-goto-argument:
5568 asm-string-literal : : asm-operands[opt] : asm-clobbers[opt] \
5569 : asm-goto-operands
5571 Qualifiers other than volatile are accepted in the syntax but
5572 warned for. */
5574 static tree
5575 c_parser_asm_statement (c_parser *parser)
5577 tree quals, str, outputs, inputs, clobbers, labels, ret;
5578 bool simple, is_goto;
5579 location_t asm_loc = c_parser_peek_token (parser)->location;
5580 int section, nsections;
5582 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ASM));
5583 c_parser_consume_token (parser);
5584 if (c_parser_next_token_is_keyword (parser, RID_VOLATILE))
5586 quals = c_parser_peek_token (parser)->value;
5587 c_parser_consume_token (parser);
5589 else if (c_parser_next_token_is_keyword (parser, RID_CONST)
5590 || c_parser_next_token_is_keyword (parser, RID_RESTRICT))
5592 warning_at (c_parser_peek_token (parser)->location,
5594 "%E qualifier ignored on asm",
5595 c_parser_peek_token (parser)->value);
5596 quals = NULL_TREE;
5597 c_parser_consume_token (parser);
5599 else
5600 quals = NULL_TREE;
5602 is_goto = false;
5603 if (c_parser_next_token_is_keyword (parser, RID_GOTO))
5605 c_parser_consume_token (parser);
5606 is_goto = true;
5609 /* ??? Follow the C++ parser rather than using the
5610 lex_untranslated_string kludge. */
5611 parser->lex_untranslated_string = true;
5612 ret = NULL;
5614 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5615 goto error;
5617 str = c_parser_asm_string_literal (parser);
5618 if (str == NULL_TREE)
5619 goto error_close_paren;
5621 simple = true;
5622 outputs = NULL_TREE;
5623 inputs = NULL_TREE;
5624 clobbers = NULL_TREE;
5625 labels = NULL_TREE;
5627 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN) && !is_goto)
5628 goto done_asm;
5630 /* Parse each colon-delimited section of operands. */
5631 nsections = 3 + is_goto;
5632 for (section = 0; section < nsections; ++section)
5634 if (!c_parser_require (parser, CPP_COLON,
5635 is_goto
5636 ? "expected %<:%>"
5637 : "expected %<:%> or %<)%>"))
5638 goto error_close_paren;
5640 /* Once past any colon, we're no longer a simple asm. */
5641 simple = false;
5643 if ((!c_parser_next_token_is (parser, CPP_COLON)
5644 && !c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
5645 || section == 3)
5646 switch (section)
5648 case 0:
5649 /* For asm goto, we don't allow output operands, but reserve
5650 the slot for a future extension that does allow them. */
5651 if (!is_goto)
5652 outputs = c_parser_asm_operands (parser);
5653 break;
5654 case 1:
5655 inputs = c_parser_asm_operands (parser);
5656 break;
5657 case 2:
5658 clobbers = c_parser_asm_clobbers (parser);
5659 break;
5660 case 3:
5661 labels = c_parser_asm_goto_operands (parser);
5662 break;
5663 default:
5664 gcc_unreachable ();
5667 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN) && !is_goto)
5668 goto done_asm;
5671 done_asm:
5672 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
5674 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5675 goto error;
5678 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
5679 c_parser_skip_to_end_of_block_or_statement (parser);
5681 ret = build_asm_stmt (quals, build_asm_expr (asm_loc, str, outputs, inputs,
5682 clobbers, labels, simple));
5684 error:
5685 parser->lex_untranslated_string = false;
5686 return ret;
5688 error_close_paren:
5689 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5690 goto error;
5693 /* Parse asm operands, a GNU extension.
5695 asm-operands:
5696 asm-operand
5697 asm-operands , asm-operand
5699 asm-operand:
5700 asm-string-literal ( expression )
5701 [ identifier ] asm-string-literal ( expression )
5704 static tree
5705 c_parser_asm_operands (c_parser *parser)
5707 tree list = NULL_TREE;
5708 while (true)
5710 tree name, str;
5711 struct c_expr expr;
5712 if (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
5714 c_parser_consume_token (parser);
5715 if (c_parser_next_token_is (parser, CPP_NAME))
5717 tree id = c_parser_peek_token (parser)->value;
5718 c_parser_consume_token (parser);
5719 name = build_string (IDENTIFIER_LENGTH (id),
5720 IDENTIFIER_POINTER (id));
5722 else
5724 c_parser_error (parser, "expected identifier");
5725 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
5726 return NULL_TREE;
5728 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
5729 "expected %<]%>");
5731 else
5732 name = NULL_TREE;
5733 str = c_parser_asm_string_literal (parser);
5734 if (str == NULL_TREE)
5735 return NULL_TREE;
5736 parser->lex_untranslated_string = false;
5737 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5739 parser->lex_untranslated_string = true;
5740 return NULL_TREE;
5742 expr = c_parser_expression (parser);
5743 mark_exp_read (expr.value);
5744 parser->lex_untranslated_string = true;
5745 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
5747 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5748 return NULL_TREE;
5750 list = chainon (list, build_tree_list (build_tree_list (name, str),
5751 expr.value));
5752 if (c_parser_next_token_is (parser, CPP_COMMA))
5753 c_parser_consume_token (parser);
5754 else
5755 break;
5757 return list;
5760 /* Parse asm clobbers, a GNU extension.
5762 asm-clobbers:
5763 asm-string-literal
5764 asm-clobbers , asm-string-literal
5767 static tree
5768 c_parser_asm_clobbers (c_parser *parser)
5770 tree list = NULL_TREE;
5771 while (true)
5773 tree str = c_parser_asm_string_literal (parser);
5774 if (str)
5775 list = tree_cons (NULL_TREE, str, list);
5776 else
5777 return NULL_TREE;
5778 if (c_parser_next_token_is (parser, CPP_COMMA))
5779 c_parser_consume_token (parser);
5780 else
5781 break;
5783 return list;
5786 /* Parse asm goto labels, a GNU extension.
5788 asm-goto-operands:
5789 identifier
5790 asm-goto-operands , identifier
5793 static tree
5794 c_parser_asm_goto_operands (c_parser *parser)
5796 tree list = NULL_TREE;
5797 while (true)
5799 tree name, label;
5801 if (c_parser_next_token_is (parser, CPP_NAME))
5803 c_token *tok = c_parser_peek_token (parser);
5804 name = tok->value;
5805 label = lookup_label_for_goto (tok->location, name);
5806 c_parser_consume_token (parser);
5807 TREE_USED (label) = 1;
5809 else
5811 c_parser_error (parser, "expected identifier");
5812 return NULL_TREE;
5815 name = build_string (IDENTIFIER_LENGTH (name),
5816 IDENTIFIER_POINTER (name));
5817 list = tree_cons (name, label, list);
5818 if (c_parser_next_token_is (parser, CPP_COMMA))
5819 c_parser_consume_token (parser);
5820 else
5821 return nreverse (list);
5825 /* Parse an expression other than a compound expression; that is, an
5826 assignment expression (C90 6.3.16, C99 6.5.16). If AFTER is not
5827 NULL then it is an Objective-C message expression which is the
5828 primary-expression starting the expression as an initializer.
5830 assignment-expression:
5831 conditional-expression
5832 unary-expression assignment-operator assignment-expression
5834 assignment-operator: one of
5835 = *= /= %= += -= <<= >>= &= ^= |=
5837 In GNU C we accept any conditional expression on the LHS and
5838 diagnose the invalid lvalue rather than producing a syntax
5839 error. */
5841 static struct c_expr
5842 c_parser_expr_no_commas (c_parser *parser, struct c_expr *after,
5843 tree omp_atomic_lhs)
5845 struct c_expr lhs, rhs, ret;
5846 enum tree_code code;
5847 location_t op_location, exp_location;
5848 gcc_assert (!after || c_dialect_objc ());
5849 lhs = c_parser_conditional_expression (parser, after, omp_atomic_lhs);
5850 op_location = c_parser_peek_token (parser)->location;
5851 switch (c_parser_peek_token (parser)->type)
5853 case CPP_EQ:
5854 code = NOP_EXPR;
5855 break;
5856 case CPP_MULT_EQ:
5857 code = MULT_EXPR;
5858 break;
5859 case CPP_DIV_EQ:
5860 code = TRUNC_DIV_EXPR;
5861 break;
5862 case CPP_MOD_EQ:
5863 code = TRUNC_MOD_EXPR;
5864 break;
5865 case CPP_PLUS_EQ:
5866 code = PLUS_EXPR;
5867 break;
5868 case CPP_MINUS_EQ:
5869 code = MINUS_EXPR;
5870 break;
5871 case CPP_LSHIFT_EQ:
5872 code = LSHIFT_EXPR;
5873 break;
5874 case CPP_RSHIFT_EQ:
5875 code = RSHIFT_EXPR;
5876 break;
5877 case CPP_AND_EQ:
5878 code = BIT_AND_EXPR;
5879 break;
5880 case CPP_XOR_EQ:
5881 code = BIT_XOR_EXPR;
5882 break;
5883 case CPP_OR_EQ:
5884 code = BIT_IOR_EXPR;
5885 break;
5886 default:
5887 return lhs;
5889 c_parser_consume_token (parser);
5890 exp_location = c_parser_peek_token (parser)->location;
5891 rhs = c_parser_expr_no_commas (parser, NULL);
5892 rhs = convert_lvalue_to_rvalue (exp_location, rhs, true, true);
5894 ret.value = build_modify_expr (op_location, lhs.value, lhs.original_type,
5895 code, exp_location, rhs.value,
5896 rhs.original_type);
5897 if (code == NOP_EXPR)
5898 ret.original_code = MODIFY_EXPR;
5899 else
5901 TREE_NO_WARNING (ret.value) = 1;
5902 ret.original_code = ERROR_MARK;
5904 ret.original_type = NULL;
5905 return ret;
5908 /* Parse a conditional expression (C90 6.3.15, C99 6.5.15). If AFTER
5909 is not NULL then it is an Objective-C message expression which is
5910 the primary-expression starting the expression as an initializer.
5912 conditional-expression:
5913 logical-OR-expression
5914 logical-OR-expression ? expression : conditional-expression
5916 GNU extensions:
5918 conditional-expression:
5919 logical-OR-expression ? : conditional-expression
5922 static struct c_expr
5923 c_parser_conditional_expression (c_parser *parser, struct c_expr *after,
5924 tree omp_atomic_lhs)
5926 struct c_expr cond, exp1, exp2, ret;
5927 location_t cond_loc, colon_loc, middle_loc;
5929 gcc_assert (!after || c_dialect_objc ());
5931 cond = c_parser_binary_expression (parser, after, omp_atomic_lhs);
5933 if (c_parser_next_token_is_not (parser, CPP_QUERY))
5934 return cond;
5935 cond_loc = c_parser_peek_token (parser)->location;
5936 cond = convert_lvalue_to_rvalue (cond_loc, cond, true, true);
5937 c_parser_consume_token (parser);
5938 if (c_parser_next_token_is (parser, CPP_COLON))
5940 tree eptype = NULL_TREE;
5942 middle_loc = c_parser_peek_token (parser)->location;
5943 pedwarn (middle_loc, OPT_Wpedantic,
5944 "ISO C forbids omitting the middle term of a ?: expression");
5945 warn_for_omitted_condop (middle_loc, cond.value);
5946 if (TREE_CODE (cond.value) == EXCESS_PRECISION_EXPR)
5948 eptype = TREE_TYPE (cond.value);
5949 cond.value = TREE_OPERAND (cond.value, 0);
5951 /* Make sure first operand is calculated only once. */
5952 exp1.value = c_save_expr (default_conversion (cond.value));
5953 if (eptype)
5954 exp1.value = build1 (EXCESS_PRECISION_EXPR, eptype, exp1.value);
5955 exp1.original_type = NULL;
5956 cond.value = c_objc_common_truthvalue_conversion (cond_loc, exp1.value);
5957 c_inhibit_evaluation_warnings += cond.value == truthvalue_true_node;
5959 else
5961 cond.value
5962 = c_objc_common_truthvalue_conversion
5963 (cond_loc, default_conversion (cond.value));
5964 c_inhibit_evaluation_warnings += cond.value == truthvalue_false_node;
5965 exp1 = c_parser_expression_conv (parser);
5966 mark_exp_read (exp1.value);
5967 c_inhibit_evaluation_warnings +=
5968 ((cond.value == truthvalue_true_node)
5969 - (cond.value == truthvalue_false_node));
5972 colon_loc = c_parser_peek_token (parser)->location;
5973 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
5975 c_inhibit_evaluation_warnings -= cond.value == truthvalue_true_node;
5976 ret.value = error_mark_node;
5977 ret.original_code = ERROR_MARK;
5978 ret.original_type = NULL;
5979 return ret;
5982 location_t exp2_loc = c_parser_peek_token (parser)->location;
5983 exp2 = c_parser_conditional_expression (parser, NULL, NULL_TREE);
5984 exp2 = convert_lvalue_to_rvalue (exp2_loc, exp2, true, true);
5986 c_inhibit_evaluation_warnings -= cond.value == truthvalue_true_node;
5987 ret.value = build_conditional_expr (colon_loc, cond.value,
5988 cond.original_code == C_MAYBE_CONST_EXPR,
5989 exp1.value, exp1.original_type,
5990 exp2.value, exp2.original_type);
5991 ret.original_code = ERROR_MARK;
5992 if (exp1.value == error_mark_node || exp2.value == error_mark_node)
5993 ret.original_type = NULL;
5994 else
5996 tree t1, t2;
5998 /* If both sides are enum type, the default conversion will have
5999 made the type of the result be an integer type. We want to
6000 remember the enum types we started with. */
6001 t1 = exp1.original_type ? exp1.original_type : TREE_TYPE (exp1.value);
6002 t2 = exp2.original_type ? exp2.original_type : TREE_TYPE (exp2.value);
6003 ret.original_type = ((t1 != error_mark_node
6004 && t2 != error_mark_node
6005 && (TYPE_MAIN_VARIANT (t1)
6006 == TYPE_MAIN_VARIANT (t2)))
6007 ? t1
6008 : NULL);
6010 return ret;
6013 /* Parse a binary expression; that is, a logical-OR-expression (C90
6014 6.3.5-6.3.14, C99 6.5.5-6.5.14). If AFTER is not NULL then it is
6015 an Objective-C message expression which is the primary-expression
6016 starting the expression as an initializer.
6018 OMP_ATOMIC_LHS is NULL, unless parsing OpenMP #pragma omp atomic,
6019 when it should be the unfolded lhs. In a valid OpenMP source,
6020 one of the operands of the toplevel binary expression must be equal
6021 to it. In that case, just return a build2 created binary operation
6022 rather than result of parser_build_binary_op.
6024 multiplicative-expression:
6025 cast-expression
6026 multiplicative-expression * cast-expression
6027 multiplicative-expression / cast-expression
6028 multiplicative-expression % cast-expression
6030 additive-expression:
6031 multiplicative-expression
6032 additive-expression + multiplicative-expression
6033 additive-expression - multiplicative-expression
6035 shift-expression:
6036 additive-expression
6037 shift-expression << additive-expression
6038 shift-expression >> additive-expression
6040 relational-expression:
6041 shift-expression
6042 relational-expression < shift-expression
6043 relational-expression > shift-expression
6044 relational-expression <= shift-expression
6045 relational-expression >= shift-expression
6047 equality-expression:
6048 relational-expression
6049 equality-expression == relational-expression
6050 equality-expression != relational-expression
6052 AND-expression:
6053 equality-expression
6054 AND-expression & equality-expression
6056 exclusive-OR-expression:
6057 AND-expression
6058 exclusive-OR-expression ^ AND-expression
6060 inclusive-OR-expression:
6061 exclusive-OR-expression
6062 inclusive-OR-expression | exclusive-OR-expression
6064 logical-AND-expression:
6065 inclusive-OR-expression
6066 logical-AND-expression && inclusive-OR-expression
6068 logical-OR-expression:
6069 logical-AND-expression
6070 logical-OR-expression || logical-AND-expression
6073 static struct c_expr
6074 c_parser_binary_expression (c_parser *parser, struct c_expr *after,
6075 tree omp_atomic_lhs)
6077 /* A binary expression is parsed using operator-precedence parsing,
6078 with the operands being cast expressions. All the binary
6079 operators are left-associative. Thus a binary expression is of
6080 form:
6082 E0 op1 E1 op2 E2 ...
6084 which we represent on a stack. On the stack, the precedence
6085 levels are strictly increasing. When a new operator is
6086 encountered of higher precedence than that at the top of the
6087 stack, it is pushed; its LHS is the top expression, and its RHS
6088 is everything parsed until it is popped. When a new operator is
6089 encountered with precedence less than or equal to that at the top
6090 of the stack, triples E[i-1] op[i] E[i] are popped and replaced
6091 by the result of the operation until the operator at the top of
6092 the stack has lower precedence than the new operator or there is
6093 only one element on the stack; then the top expression is the LHS
6094 of the new operator. In the case of logical AND and OR
6095 expressions, we also need to adjust c_inhibit_evaluation_warnings
6096 as appropriate when the operators are pushed and popped. */
6098 struct {
6099 /* The expression at this stack level. */
6100 struct c_expr expr;
6101 /* The precedence of the operator on its left, PREC_NONE at the
6102 bottom of the stack. */
6103 enum c_parser_prec prec;
6104 /* The operation on its left. */
6105 enum tree_code op;
6106 /* The source location of this operation. */
6107 location_t loc;
6108 } stack[NUM_PRECS];
6109 int sp;
6110 /* Location of the binary operator. */
6111 location_t binary_loc = UNKNOWN_LOCATION; /* Quiet warning. */
6112 #define POP \
6113 do { \
6114 switch (stack[sp].op) \
6116 case TRUTH_ANDIF_EXPR: \
6117 c_inhibit_evaluation_warnings -= (stack[sp - 1].expr.value \
6118 == truthvalue_false_node); \
6119 break; \
6120 case TRUTH_ORIF_EXPR: \
6121 c_inhibit_evaluation_warnings -= (stack[sp - 1].expr.value \
6122 == truthvalue_true_node); \
6123 break; \
6124 default: \
6125 break; \
6127 stack[sp - 1].expr \
6128 = convert_lvalue_to_rvalue (stack[sp - 1].loc, \
6129 stack[sp - 1].expr, true, true); \
6130 stack[sp].expr \
6131 = convert_lvalue_to_rvalue (stack[sp].loc, \
6132 stack[sp].expr, true, true); \
6133 if (__builtin_expect (omp_atomic_lhs != NULL_TREE, 0) && sp == 1 \
6134 && c_parser_peek_token (parser)->type == CPP_SEMICOLON \
6135 && ((1 << stack[sp].prec) \
6136 & (1 << (PREC_BITOR | PREC_BITXOR | PREC_BITAND | PREC_SHIFT \
6137 | PREC_ADD | PREC_MULT))) \
6138 && stack[sp].op != TRUNC_MOD_EXPR \
6139 && stack[0].expr.value != error_mark_node \
6140 && stack[1].expr.value != error_mark_node \
6141 && (c_tree_equal (stack[0].expr.value, omp_atomic_lhs) \
6142 || c_tree_equal (stack[1].expr.value, omp_atomic_lhs))) \
6143 stack[0].expr.value \
6144 = build2 (stack[1].op, TREE_TYPE (stack[0].expr.value), \
6145 stack[0].expr.value, stack[1].expr.value); \
6146 else \
6147 stack[sp - 1].expr = parser_build_binary_op (stack[sp].loc, \
6148 stack[sp].op, \
6149 stack[sp - 1].expr, \
6150 stack[sp].expr); \
6151 sp--; \
6152 } while (0)
6153 gcc_assert (!after || c_dialect_objc ());
6154 stack[0].loc = c_parser_peek_token (parser)->location;
6155 stack[0].expr = c_parser_cast_expression (parser, after);
6156 stack[0].prec = PREC_NONE;
6157 sp = 0;
6158 while (true)
6160 enum c_parser_prec oprec;
6161 enum tree_code ocode;
6162 if (parser->error)
6163 goto out;
6164 switch (c_parser_peek_token (parser)->type)
6166 case CPP_MULT:
6167 oprec = PREC_MULT;
6168 ocode = MULT_EXPR;
6169 break;
6170 case CPP_DIV:
6171 oprec = PREC_MULT;
6172 ocode = TRUNC_DIV_EXPR;
6173 break;
6174 case CPP_MOD:
6175 oprec = PREC_MULT;
6176 ocode = TRUNC_MOD_EXPR;
6177 break;
6178 case CPP_PLUS:
6179 oprec = PREC_ADD;
6180 ocode = PLUS_EXPR;
6181 break;
6182 case CPP_MINUS:
6183 oprec = PREC_ADD;
6184 ocode = MINUS_EXPR;
6185 break;
6186 case CPP_LSHIFT:
6187 oprec = PREC_SHIFT;
6188 ocode = LSHIFT_EXPR;
6189 break;
6190 case CPP_RSHIFT:
6191 oprec = PREC_SHIFT;
6192 ocode = RSHIFT_EXPR;
6193 break;
6194 case CPP_LESS:
6195 oprec = PREC_REL;
6196 ocode = LT_EXPR;
6197 break;
6198 case CPP_GREATER:
6199 oprec = PREC_REL;
6200 ocode = GT_EXPR;
6201 break;
6202 case CPP_LESS_EQ:
6203 oprec = PREC_REL;
6204 ocode = LE_EXPR;
6205 break;
6206 case CPP_GREATER_EQ:
6207 oprec = PREC_REL;
6208 ocode = GE_EXPR;
6209 break;
6210 case CPP_EQ_EQ:
6211 oprec = PREC_EQ;
6212 ocode = EQ_EXPR;
6213 break;
6214 case CPP_NOT_EQ:
6215 oprec = PREC_EQ;
6216 ocode = NE_EXPR;
6217 break;
6218 case CPP_AND:
6219 oprec = PREC_BITAND;
6220 ocode = BIT_AND_EXPR;
6221 break;
6222 case CPP_XOR:
6223 oprec = PREC_BITXOR;
6224 ocode = BIT_XOR_EXPR;
6225 break;
6226 case CPP_OR:
6227 oprec = PREC_BITOR;
6228 ocode = BIT_IOR_EXPR;
6229 break;
6230 case CPP_AND_AND:
6231 oprec = PREC_LOGAND;
6232 ocode = TRUTH_ANDIF_EXPR;
6233 break;
6234 case CPP_OR_OR:
6235 oprec = PREC_LOGOR;
6236 ocode = TRUTH_ORIF_EXPR;
6237 break;
6238 default:
6239 /* Not a binary operator, so end of the binary
6240 expression. */
6241 goto out;
6243 binary_loc = c_parser_peek_token (parser)->location;
6244 while (oprec <= stack[sp].prec)
6245 POP;
6246 c_parser_consume_token (parser);
6247 switch (ocode)
6249 case TRUTH_ANDIF_EXPR:
6250 stack[sp].expr
6251 = convert_lvalue_to_rvalue (stack[sp].loc,
6252 stack[sp].expr, true, true);
6253 stack[sp].expr.value = c_objc_common_truthvalue_conversion
6254 (stack[sp].loc, default_conversion (stack[sp].expr.value));
6255 c_inhibit_evaluation_warnings += (stack[sp].expr.value
6256 == truthvalue_false_node);
6257 break;
6258 case TRUTH_ORIF_EXPR:
6259 stack[sp].expr
6260 = convert_lvalue_to_rvalue (stack[sp].loc,
6261 stack[sp].expr, true, true);
6262 stack[sp].expr.value = c_objc_common_truthvalue_conversion
6263 (stack[sp].loc, default_conversion (stack[sp].expr.value));
6264 c_inhibit_evaluation_warnings += (stack[sp].expr.value
6265 == truthvalue_true_node);
6266 break;
6267 default:
6268 break;
6270 sp++;
6271 stack[sp].loc = binary_loc;
6272 stack[sp].expr = c_parser_cast_expression (parser, NULL);
6273 stack[sp].prec = oprec;
6274 stack[sp].op = ocode;
6275 stack[sp].loc = binary_loc;
6277 out:
6278 while (sp > 0)
6279 POP;
6280 return stack[0].expr;
6281 #undef POP
6284 /* Parse a cast expression (C90 6.3.4, C99 6.5.4). If AFTER is not
6285 NULL then it is an Objective-C message expression which is the
6286 primary-expression starting the expression as an initializer.
6288 cast-expression:
6289 unary-expression
6290 ( type-name ) unary-expression
6293 static struct c_expr
6294 c_parser_cast_expression (c_parser *parser, struct c_expr *after)
6296 location_t cast_loc = c_parser_peek_token (parser)->location;
6297 gcc_assert (!after || c_dialect_objc ());
6298 if (after)
6299 return c_parser_postfix_expression_after_primary (parser,
6300 cast_loc, *after);
6301 /* If the expression begins with a parenthesized type name, it may
6302 be either a cast or a compound literal; we need to see whether
6303 the next character is '{' to tell the difference. If not, it is
6304 an unary expression. Full detection of unknown typenames here
6305 would require a 3-token lookahead. */
6306 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
6307 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
6309 struct c_type_name *type_name;
6310 struct c_expr ret;
6311 struct c_expr expr;
6312 c_parser_consume_token (parser);
6313 type_name = c_parser_type_name (parser);
6314 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
6315 if (type_name == NULL)
6317 ret.value = error_mark_node;
6318 ret.original_code = ERROR_MARK;
6319 ret.original_type = NULL;
6320 return ret;
6323 /* Save casted types in the function's used types hash table. */
6324 used_types_insert (type_name->specs->type);
6326 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
6327 return c_parser_postfix_expression_after_paren_type (parser, type_name,
6328 cast_loc);
6330 location_t expr_loc = c_parser_peek_token (parser)->location;
6331 expr = c_parser_cast_expression (parser, NULL);
6332 expr = convert_lvalue_to_rvalue (expr_loc, expr, true, true);
6334 ret.value = c_cast_expr (cast_loc, type_name, expr.value);
6335 ret.original_code = ERROR_MARK;
6336 ret.original_type = NULL;
6337 return ret;
6339 else
6340 return c_parser_unary_expression (parser);
6343 /* Parse an unary expression (C90 6.3.3, C99 6.5.3).
6345 unary-expression:
6346 postfix-expression
6347 ++ unary-expression
6348 -- unary-expression
6349 unary-operator cast-expression
6350 sizeof unary-expression
6351 sizeof ( type-name )
6353 unary-operator: one of
6354 & * + - ~ !
6356 GNU extensions:
6358 unary-expression:
6359 __alignof__ unary-expression
6360 __alignof__ ( type-name )
6361 && identifier
6363 (C11 permits _Alignof with type names only.)
6365 unary-operator: one of
6366 __extension__ __real__ __imag__
6368 Transactional Memory:
6370 unary-expression:
6371 transaction-expression
6373 In addition, the GNU syntax treats ++ and -- as unary operators, so
6374 they may be applied to cast expressions with errors for non-lvalues
6375 given later. */
6377 static struct c_expr
6378 c_parser_unary_expression (c_parser *parser)
6380 int ext;
6381 struct c_expr ret, op;
6382 location_t op_loc = c_parser_peek_token (parser)->location;
6383 location_t exp_loc;
6384 ret.original_code = ERROR_MARK;
6385 ret.original_type = NULL;
6386 switch (c_parser_peek_token (parser)->type)
6388 case CPP_PLUS_PLUS:
6389 c_parser_consume_token (parser);
6390 exp_loc = c_parser_peek_token (parser)->location;
6391 op = c_parser_cast_expression (parser, NULL);
6393 /* If there is array notations in op, we expand them. */
6394 if (flag_cilkplus && TREE_CODE (op.value) == ARRAY_NOTATION_REF)
6395 return fix_array_notation_expr (exp_loc, PREINCREMENT_EXPR, op);
6396 else
6398 op = default_function_array_read_conversion (exp_loc, op);
6399 return parser_build_unary_op (op_loc, PREINCREMENT_EXPR, op);
6401 case CPP_MINUS_MINUS:
6402 c_parser_consume_token (parser);
6403 exp_loc = c_parser_peek_token (parser)->location;
6404 op = c_parser_cast_expression (parser, NULL);
6406 /* If there is array notations in op, we expand them. */
6407 if (flag_cilkplus && TREE_CODE (op.value) == ARRAY_NOTATION_REF)
6408 return fix_array_notation_expr (exp_loc, PREDECREMENT_EXPR, op);
6409 else
6411 op = default_function_array_read_conversion (exp_loc, op);
6412 return parser_build_unary_op (op_loc, PREDECREMENT_EXPR, op);
6414 case CPP_AND:
6415 c_parser_consume_token (parser);
6416 op = c_parser_cast_expression (parser, NULL);
6417 mark_exp_read (op.value);
6418 return parser_build_unary_op (op_loc, ADDR_EXPR, op);
6419 case CPP_MULT:
6420 c_parser_consume_token (parser);
6421 exp_loc = c_parser_peek_token (parser)->location;
6422 op = c_parser_cast_expression (parser, NULL);
6423 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
6424 ret.value = build_indirect_ref (op_loc, op.value, RO_UNARY_STAR);
6425 return ret;
6426 case CPP_PLUS:
6427 if (!c_dialect_objc () && !in_system_header_at (input_location))
6428 warning_at (op_loc,
6429 OPT_Wtraditional,
6430 "traditional C rejects the unary plus operator");
6431 c_parser_consume_token (parser);
6432 exp_loc = c_parser_peek_token (parser)->location;
6433 op = c_parser_cast_expression (parser, NULL);
6434 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
6435 return parser_build_unary_op (op_loc, CONVERT_EXPR, op);
6436 case CPP_MINUS:
6437 c_parser_consume_token (parser);
6438 exp_loc = c_parser_peek_token (parser)->location;
6439 op = c_parser_cast_expression (parser, NULL);
6440 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
6441 return parser_build_unary_op (op_loc, NEGATE_EXPR, op);
6442 case CPP_COMPL:
6443 c_parser_consume_token (parser);
6444 exp_loc = c_parser_peek_token (parser)->location;
6445 op = c_parser_cast_expression (parser, NULL);
6446 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
6447 return parser_build_unary_op (op_loc, BIT_NOT_EXPR, op);
6448 case CPP_NOT:
6449 c_parser_consume_token (parser);
6450 exp_loc = c_parser_peek_token (parser)->location;
6451 op = c_parser_cast_expression (parser, NULL);
6452 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
6453 return parser_build_unary_op (op_loc, TRUTH_NOT_EXPR, op);
6454 case CPP_AND_AND:
6455 /* Refer to the address of a label as a pointer. */
6456 c_parser_consume_token (parser);
6457 if (c_parser_next_token_is (parser, CPP_NAME))
6459 ret.value = finish_label_address_expr
6460 (c_parser_peek_token (parser)->value, op_loc);
6461 c_parser_consume_token (parser);
6463 else
6465 c_parser_error (parser, "expected identifier");
6466 ret.value = error_mark_node;
6468 return ret;
6469 case CPP_KEYWORD:
6470 switch (c_parser_peek_token (parser)->keyword)
6472 case RID_SIZEOF:
6473 return c_parser_sizeof_expression (parser);
6474 case RID_ALIGNOF:
6475 return c_parser_alignof_expression (parser);
6476 case RID_EXTENSION:
6477 c_parser_consume_token (parser);
6478 ext = disable_extension_diagnostics ();
6479 ret = c_parser_cast_expression (parser, NULL);
6480 restore_extension_diagnostics (ext);
6481 return ret;
6482 case RID_REALPART:
6483 c_parser_consume_token (parser);
6484 exp_loc = c_parser_peek_token (parser)->location;
6485 op = c_parser_cast_expression (parser, NULL);
6486 op = default_function_array_conversion (exp_loc, op);
6487 return parser_build_unary_op (op_loc, REALPART_EXPR, op);
6488 case RID_IMAGPART:
6489 c_parser_consume_token (parser);
6490 exp_loc = c_parser_peek_token (parser)->location;
6491 op = c_parser_cast_expression (parser, NULL);
6492 op = default_function_array_conversion (exp_loc, op);
6493 return parser_build_unary_op (op_loc, IMAGPART_EXPR, op);
6494 case RID_TRANSACTION_ATOMIC:
6495 case RID_TRANSACTION_RELAXED:
6496 return c_parser_transaction_expression (parser,
6497 c_parser_peek_token (parser)->keyword);
6498 default:
6499 return c_parser_postfix_expression (parser);
6501 default:
6502 return c_parser_postfix_expression (parser);
6506 /* Parse a sizeof expression. */
6508 static struct c_expr
6509 c_parser_sizeof_expression (c_parser *parser)
6511 struct c_expr expr;
6512 location_t expr_loc;
6513 gcc_assert (c_parser_next_token_is_keyword (parser, RID_SIZEOF));
6514 c_parser_consume_token (parser);
6515 c_inhibit_evaluation_warnings++;
6516 in_sizeof++;
6517 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
6518 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
6520 /* Either sizeof ( type-name ) or sizeof unary-expression
6521 starting with a compound literal. */
6522 struct c_type_name *type_name;
6523 c_parser_consume_token (parser);
6524 expr_loc = c_parser_peek_token (parser)->location;
6525 type_name = c_parser_type_name (parser);
6526 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
6527 if (type_name == NULL)
6529 struct c_expr ret;
6530 c_inhibit_evaluation_warnings--;
6531 in_sizeof--;
6532 ret.value = error_mark_node;
6533 ret.original_code = ERROR_MARK;
6534 ret.original_type = NULL;
6535 return ret;
6537 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
6539 expr = c_parser_postfix_expression_after_paren_type (parser,
6540 type_name,
6541 expr_loc);
6542 goto sizeof_expr;
6544 /* sizeof ( type-name ). */
6545 c_inhibit_evaluation_warnings--;
6546 in_sizeof--;
6547 return c_expr_sizeof_type (expr_loc, type_name);
6549 else
6551 expr_loc = c_parser_peek_token (parser)->location;
6552 expr = c_parser_unary_expression (parser);
6553 sizeof_expr:
6554 c_inhibit_evaluation_warnings--;
6555 in_sizeof--;
6556 mark_exp_read (expr.value);
6557 if (TREE_CODE (expr.value) == COMPONENT_REF
6558 && DECL_C_BIT_FIELD (TREE_OPERAND (expr.value, 1)))
6559 error_at (expr_loc, "%<sizeof%> applied to a bit-field");
6560 return c_expr_sizeof_expr (expr_loc, expr);
6564 /* Parse an alignof expression. */
6566 static struct c_expr
6567 c_parser_alignof_expression (c_parser *parser)
6569 struct c_expr expr;
6570 location_t loc = c_parser_peek_token (parser)->location;
6571 tree alignof_spelling = c_parser_peek_token (parser)->value;
6572 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ALIGNOF));
6573 bool is_c11_alignof = strcmp (IDENTIFIER_POINTER (alignof_spelling),
6574 "_Alignof") == 0;
6575 /* A diagnostic is not required for the use of this identifier in
6576 the implementation namespace; only diagnose it for the C11
6577 spelling because of existing code using the other spellings. */
6578 if (!flag_isoc11 && is_c11_alignof)
6580 if (flag_isoc99)
6581 pedwarn (loc, OPT_Wpedantic, "ISO C99 does not support %qE",
6582 alignof_spelling);
6583 else
6584 pedwarn (loc, OPT_Wpedantic, "ISO C90 does not support %qE",
6585 alignof_spelling);
6587 c_parser_consume_token (parser);
6588 c_inhibit_evaluation_warnings++;
6589 in_alignof++;
6590 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
6591 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
6593 /* Either __alignof__ ( type-name ) or __alignof__
6594 unary-expression starting with a compound literal. */
6595 location_t loc;
6596 struct c_type_name *type_name;
6597 struct c_expr ret;
6598 c_parser_consume_token (parser);
6599 loc = c_parser_peek_token (parser)->location;
6600 type_name = c_parser_type_name (parser);
6601 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
6602 if (type_name == NULL)
6604 struct c_expr ret;
6605 c_inhibit_evaluation_warnings--;
6606 in_alignof--;
6607 ret.value = error_mark_node;
6608 ret.original_code = ERROR_MARK;
6609 ret.original_type = NULL;
6610 return ret;
6612 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
6614 expr = c_parser_postfix_expression_after_paren_type (parser,
6615 type_name,
6616 loc);
6617 goto alignof_expr;
6619 /* alignof ( type-name ). */
6620 c_inhibit_evaluation_warnings--;
6621 in_alignof--;
6622 ret.value = c_sizeof_or_alignof_type (loc, groktypename (type_name,
6623 NULL, NULL),
6624 false, is_c11_alignof, 1);
6625 ret.original_code = ERROR_MARK;
6626 ret.original_type = NULL;
6627 return ret;
6629 else
6631 struct c_expr ret;
6632 expr = c_parser_unary_expression (parser);
6633 alignof_expr:
6634 mark_exp_read (expr.value);
6635 c_inhibit_evaluation_warnings--;
6636 in_alignof--;
6637 pedwarn (loc, OPT_Wpedantic, "ISO C does not allow %<%E (expression)%>",
6638 alignof_spelling);
6639 ret.value = c_alignof_expr (loc, expr.value);
6640 ret.original_code = ERROR_MARK;
6641 ret.original_type = NULL;
6642 return ret;
6646 /* Helper function to read arguments of builtins which are interfaces
6647 for the middle-end nodes like COMPLEX_EXPR, VEC_PERM_EXPR and
6648 others. The name of the builtin is passed using BNAME parameter.
6649 Function returns true if there were no errors while parsing and
6650 stores the arguments in CEXPR_LIST. */
6651 static bool
6652 c_parser_get_builtin_args (c_parser *parser, const char *bname,
6653 vec<c_expr_t, va_gc> **ret_cexpr_list,
6654 bool choose_expr_p)
6656 location_t loc = c_parser_peek_token (parser)->location;
6657 vec<c_expr_t, va_gc> *cexpr_list;
6658 c_expr_t expr;
6659 bool saved_force_folding_builtin_constant_p;
6661 *ret_cexpr_list = NULL;
6662 if (c_parser_next_token_is_not (parser, CPP_OPEN_PAREN))
6664 error_at (loc, "cannot take address of %qs", bname);
6665 return false;
6668 c_parser_consume_token (parser);
6670 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
6672 c_parser_consume_token (parser);
6673 return true;
6676 saved_force_folding_builtin_constant_p
6677 = force_folding_builtin_constant_p;
6678 force_folding_builtin_constant_p |= choose_expr_p;
6679 expr = c_parser_expr_no_commas (parser, NULL);
6680 force_folding_builtin_constant_p
6681 = saved_force_folding_builtin_constant_p;
6682 vec_alloc (cexpr_list, 1);
6683 vec_safe_push (cexpr_list, expr);
6684 while (c_parser_next_token_is (parser, CPP_COMMA))
6686 c_parser_consume_token (parser);
6687 expr = c_parser_expr_no_commas (parser, NULL);
6688 vec_safe_push (cexpr_list, expr);
6691 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
6692 return false;
6694 *ret_cexpr_list = cexpr_list;
6695 return true;
6698 /* This represents a single generic-association. */
6700 struct c_generic_association
6702 /* The location of the starting token of the type. */
6703 location_t type_location;
6704 /* The association's type, or NULL_TREE for 'default'. */
6705 tree type;
6706 /* The association's expression. */
6707 struct c_expr expression;
6710 /* Parse a generic-selection. (C11 6.5.1.1).
6712 generic-selection:
6713 _Generic ( assignment-expression , generic-assoc-list )
6715 generic-assoc-list:
6716 generic-association
6717 generic-assoc-list , generic-association
6719 generic-association:
6720 type-name : assignment-expression
6721 default : assignment-expression
6724 static struct c_expr
6725 c_parser_generic_selection (c_parser *parser)
6727 vec<c_generic_association> associations = vNULL;
6728 struct c_expr selector, error_expr;
6729 tree selector_type;
6730 struct c_generic_association matched_assoc;
6731 bool match_found = false;
6732 location_t generic_loc, selector_loc;
6734 error_expr.original_code = ERROR_MARK;
6735 error_expr.original_type = NULL;
6736 error_expr.value = error_mark_node;
6737 matched_assoc.type_location = UNKNOWN_LOCATION;
6738 matched_assoc.type = NULL_TREE;
6739 matched_assoc.expression = error_expr;
6741 gcc_assert (c_parser_next_token_is_keyword (parser, RID_GENERIC));
6742 generic_loc = c_parser_peek_token (parser)->location;
6743 c_parser_consume_token (parser);
6744 if (!flag_isoc11)
6746 if (flag_isoc99)
6747 pedwarn (generic_loc, OPT_Wpedantic,
6748 "ISO C99 does not support %<_Generic%>");
6749 else
6750 pedwarn (generic_loc, OPT_Wpedantic,
6751 "ISO C90 does not support %<_Generic%>");
6754 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6755 return error_expr;
6757 c_inhibit_evaluation_warnings++;
6758 selector_loc = c_parser_peek_token (parser)->location;
6759 selector = c_parser_expr_no_commas (parser, NULL);
6760 selector = default_function_array_conversion (selector_loc, selector);
6761 c_inhibit_evaluation_warnings--;
6763 if (selector.value == error_mark_node)
6765 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6766 return selector;
6768 selector_type = TREE_TYPE (selector.value);
6769 /* In ISO C terms, rvalues (including the controlling expression of
6770 _Generic) do not have qualified types. */
6771 if (TREE_CODE (selector_type) != ARRAY_TYPE)
6772 selector_type = TYPE_MAIN_VARIANT (selector_type);
6773 /* In ISO C terms, _Noreturn is not part of the type of expressions
6774 such as &abort, but in GCC it is represented internally as a type
6775 qualifier. */
6776 if (FUNCTION_POINTER_TYPE_P (selector_type)
6777 && TYPE_QUALS (TREE_TYPE (selector_type)) != TYPE_UNQUALIFIED)
6778 selector_type
6779 = build_pointer_type (TYPE_MAIN_VARIANT (TREE_TYPE (selector_type)));
6781 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
6783 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6784 return error_expr;
6787 while (1)
6789 struct c_generic_association assoc, *iter;
6790 unsigned int ix;
6791 c_token *token = c_parser_peek_token (parser);
6793 assoc.type_location = token->location;
6794 if (token->type == CPP_KEYWORD && token->keyword == RID_DEFAULT)
6796 c_parser_consume_token (parser);
6797 assoc.type = NULL_TREE;
6799 else
6801 struct c_type_name *type_name;
6803 type_name = c_parser_type_name (parser);
6804 if (type_name == NULL)
6806 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6807 goto error_exit;
6809 assoc.type = groktypename (type_name, NULL, NULL);
6810 if (assoc.type == error_mark_node)
6812 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6813 goto error_exit;
6816 if (TREE_CODE (assoc.type) == FUNCTION_TYPE)
6817 error_at (assoc.type_location,
6818 "%<_Generic%> association has function type");
6819 else if (!COMPLETE_TYPE_P (assoc.type))
6820 error_at (assoc.type_location,
6821 "%<_Generic%> association has incomplete type");
6823 if (variably_modified_type_p (assoc.type, NULL_TREE))
6824 error_at (assoc.type_location,
6825 "%<_Generic%> association has "
6826 "variable length type");
6829 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
6831 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6832 goto error_exit;
6835 assoc.expression = c_parser_expr_no_commas (parser, NULL);
6836 if (assoc.expression.value == error_mark_node)
6838 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6839 goto error_exit;
6842 for (ix = 0; associations.iterate (ix, &iter); ++ix)
6844 if (assoc.type == NULL_TREE)
6846 if (iter->type == NULL_TREE)
6848 error_at (assoc.type_location,
6849 "duplicate %<default%> case in %<_Generic%>");
6850 inform (iter->type_location, "original %<default%> is here");
6853 else if (iter->type != NULL_TREE)
6855 if (comptypes (assoc.type, iter->type))
6857 error_at (assoc.type_location,
6858 "%<_Generic%> specifies two compatible types");
6859 inform (iter->type_location, "compatible type is here");
6864 if (assoc.type == NULL_TREE)
6866 if (!match_found)
6868 matched_assoc = assoc;
6869 match_found = true;
6872 else if (comptypes (assoc.type, selector_type))
6874 if (!match_found || matched_assoc.type == NULL_TREE)
6876 matched_assoc = assoc;
6877 match_found = true;
6879 else
6881 error_at (assoc.type_location,
6882 "%<_Generic> selector matches multiple associations");
6883 inform (matched_assoc.type_location,
6884 "other match is here");
6888 associations.safe_push (assoc);
6890 if (c_parser_peek_token (parser)->type != CPP_COMMA)
6891 break;
6892 c_parser_consume_token (parser);
6895 associations.release ();
6897 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
6899 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6900 return error_expr;
6903 if (!match_found)
6905 error_at (selector_loc, "%<_Generic%> selector of type %qT is not "
6906 "compatible with any association",
6907 selector_type);
6908 return error_expr;
6911 return matched_assoc.expression;
6913 error_exit:
6914 associations.release ();
6915 return error_expr;
6918 /* Parse a postfix expression (C90 6.3.1-6.3.2, C99 6.5.1-6.5.2).
6920 postfix-expression:
6921 primary-expression
6922 postfix-expression [ expression ]
6923 postfix-expression ( argument-expression-list[opt] )
6924 postfix-expression . identifier
6925 postfix-expression -> identifier
6926 postfix-expression ++
6927 postfix-expression --
6928 ( type-name ) { initializer-list }
6929 ( type-name ) { initializer-list , }
6931 argument-expression-list:
6932 argument-expression
6933 argument-expression-list , argument-expression
6935 primary-expression:
6936 identifier
6937 constant
6938 string-literal
6939 ( expression )
6940 generic-selection
6942 GNU extensions:
6944 primary-expression:
6945 __func__
6946 (treated as a keyword in GNU C)
6947 __FUNCTION__
6948 __PRETTY_FUNCTION__
6949 ( compound-statement )
6950 __builtin_va_arg ( assignment-expression , type-name )
6951 __builtin_offsetof ( type-name , offsetof-member-designator )
6952 __builtin_choose_expr ( assignment-expression ,
6953 assignment-expression ,
6954 assignment-expression )
6955 __builtin_types_compatible_p ( type-name , type-name )
6956 __builtin_complex ( assignment-expression , assignment-expression )
6957 __builtin_shuffle ( assignment-expression , assignment-expression )
6958 __builtin_shuffle ( assignment-expression ,
6959 assignment-expression ,
6960 assignment-expression, )
6962 offsetof-member-designator:
6963 identifier
6964 offsetof-member-designator . identifier
6965 offsetof-member-designator [ expression ]
6967 Objective-C:
6969 primary-expression:
6970 [ objc-receiver objc-message-args ]
6971 @selector ( objc-selector-arg )
6972 @protocol ( identifier )
6973 @encode ( type-name )
6974 objc-string-literal
6975 Classname . identifier
6978 static struct c_expr
6979 c_parser_postfix_expression (c_parser *parser)
6981 struct c_expr expr, e1;
6982 struct c_type_name *t1, *t2;
6983 location_t loc = c_parser_peek_token (parser)->location;;
6984 expr.original_code = ERROR_MARK;
6985 expr.original_type = NULL;
6986 switch (c_parser_peek_token (parser)->type)
6988 case CPP_NUMBER:
6989 expr.value = c_parser_peek_token (parser)->value;
6990 loc = c_parser_peek_token (parser)->location;
6991 c_parser_consume_token (parser);
6992 if (TREE_CODE (expr.value) == FIXED_CST
6993 && !targetm.fixed_point_supported_p ())
6995 error_at (loc, "fixed-point types not supported for this target");
6996 expr.value = error_mark_node;
6998 break;
6999 case CPP_CHAR:
7000 case CPP_CHAR16:
7001 case CPP_CHAR32:
7002 case CPP_WCHAR:
7003 expr.value = c_parser_peek_token (parser)->value;
7004 c_parser_consume_token (parser);
7005 break;
7006 case CPP_STRING:
7007 case CPP_STRING16:
7008 case CPP_STRING32:
7009 case CPP_WSTRING:
7010 case CPP_UTF8STRING:
7011 expr.value = c_parser_peek_token (parser)->value;
7012 expr.original_code = STRING_CST;
7013 c_parser_consume_token (parser);
7014 break;
7015 case CPP_OBJC_STRING:
7016 gcc_assert (c_dialect_objc ());
7017 expr.value
7018 = objc_build_string_object (c_parser_peek_token (parser)->value);
7019 c_parser_consume_token (parser);
7020 break;
7021 case CPP_NAME:
7022 switch (c_parser_peek_token (parser)->id_kind)
7024 case C_ID_ID:
7026 tree id = c_parser_peek_token (parser)->value;
7027 c_parser_consume_token (parser);
7028 expr.value = build_external_ref (loc, id,
7029 (c_parser_peek_token (parser)->type
7030 == CPP_OPEN_PAREN),
7031 &expr.original_type);
7032 break;
7034 case C_ID_CLASSNAME:
7036 /* Here we parse the Objective-C 2.0 Class.name dot
7037 syntax. */
7038 tree class_name = c_parser_peek_token (parser)->value;
7039 tree component;
7040 c_parser_consume_token (parser);
7041 gcc_assert (c_dialect_objc ());
7042 if (!c_parser_require (parser, CPP_DOT, "expected %<.%>"))
7044 expr.value = error_mark_node;
7045 break;
7047 if (c_parser_next_token_is_not (parser, CPP_NAME))
7049 c_parser_error (parser, "expected identifier");
7050 expr.value = error_mark_node;
7051 break;
7053 component = c_parser_peek_token (parser)->value;
7054 c_parser_consume_token (parser);
7055 expr.value = objc_build_class_component_ref (class_name,
7056 component);
7057 break;
7059 default:
7060 c_parser_error (parser, "expected expression");
7061 expr.value = error_mark_node;
7062 break;
7064 break;
7065 case CPP_OPEN_PAREN:
7066 /* A parenthesized expression, statement expression or compound
7067 literal. */
7068 if (c_parser_peek_2nd_token (parser)->type == CPP_OPEN_BRACE)
7070 /* A statement expression. */
7071 tree stmt;
7072 location_t brace_loc;
7073 c_parser_consume_token (parser);
7074 brace_loc = c_parser_peek_token (parser)->location;
7075 c_parser_consume_token (parser);
7076 if (!building_stmt_list_p ())
7078 error_at (loc, "braced-group within expression allowed "
7079 "only inside a function");
7080 parser->error = true;
7081 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
7082 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7083 expr.value = error_mark_node;
7084 break;
7086 stmt = c_begin_stmt_expr ();
7087 c_parser_compound_statement_nostart (parser);
7088 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7089 "expected %<)%>");
7090 pedwarn (loc, OPT_Wpedantic,
7091 "ISO C forbids braced-groups within expressions");
7092 expr.value = c_finish_stmt_expr (brace_loc, stmt);
7093 mark_exp_read (expr.value);
7095 else if (c_token_starts_typename (c_parser_peek_2nd_token (parser)))
7097 /* A compound literal. ??? Can we actually get here rather
7098 than going directly to
7099 c_parser_postfix_expression_after_paren_type from
7100 elsewhere? */
7101 location_t loc;
7102 struct c_type_name *type_name;
7103 c_parser_consume_token (parser);
7104 loc = c_parser_peek_token (parser)->location;
7105 type_name = c_parser_type_name (parser);
7106 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7107 "expected %<)%>");
7108 if (type_name == NULL)
7110 expr.value = error_mark_node;
7112 else
7113 expr = c_parser_postfix_expression_after_paren_type (parser,
7114 type_name,
7115 loc);
7117 else
7119 /* A parenthesized expression. */
7120 c_parser_consume_token (parser);
7121 expr = c_parser_expression (parser);
7122 if (TREE_CODE (expr.value) == MODIFY_EXPR)
7123 TREE_NO_WARNING (expr.value) = 1;
7124 if (expr.original_code != C_MAYBE_CONST_EXPR)
7125 expr.original_code = ERROR_MARK;
7126 /* Don't change EXPR.ORIGINAL_TYPE. */
7127 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7128 "expected %<)%>");
7130 break;
7131 case CPP_KEYWORD:
7132 switch (c_parser_peek_token (parser)->keyword)
7134 case RID_FUNCTION_NAME:
7135 case RID_PRETTY_FUNCTION_NAME:
7136 case RID_C99_FUNCTION_NAME:
7137 expr.value = fname_decl (loc,
7138 c_parser_peek_token (parser)->keyword,
7139 c_parser_peek_token (parser)->value);
7140 c_parser_consume_token (parser);
7141 break;
7142 case RID_VA_ARG:
7143 c_parser_consume_token (parser);
7144 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7146 expr.value = error_mark_node;
7147 break;
7149 e1 = c_parser_expr_no_commas (parser, NULL);
7150 mark_exp_read (e1.value);
7151 e1.value = c_fully_fold (e1.value, false, NULL);
7152 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
7154 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7155 expr.value = error_mark_node;
7156 break;
7158 loc = c_parser_peek_token (parser)->location;
7159 t1 = c_parser_type_name (parser);
7160 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7161 "expected %<)%>");
7162 if (t1 == NULL)
7164 expr.value = error_mark_node;
7166 else
7168 tree type_expr = NULL_TREE;
7169 expr.value = c_build_va_arg (loc, e1.value,
7170 groktypename (t1, &type_expr, NULL));
7171 if (type_expr)
7173 expr.value = build2 (C_MAYBE_CONST_EXPR,
7174 TREE_TYPE (expr.value), type_expr,
7175 expr.value);
7176 C_MAYBE_CONST_EXPR_NON_CONST (expr.value) = true;
7179 break;
7180 case RID_OFFSETOF:
7181 c_parser_consume_token (parser);
7182 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7184 expr.value = error_mark_node;
7185 break;
7187 t1 = c_parser_type_name (parser);
7188 if (t1 == NULL)
7189 parser->error = true;
7190 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
7191 gcc_assert (parser->error);
7192 if (parser->error)
7194 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7195 expr.value = error_mark_node;
7196 break;
7200 tree type = groktypename (t1, NULL, NULL);
7201 tree offsetof_ref;
7202 if (type == error_mark_node)
7203 offsetof_ref = error_mark_node;
7204 else
7206 offsetof_ref = build1 (INDIRECT_REF, type, null_pointer_node);
7207 SET_EXPR_LOCATION (offsetof_ref, loc);
7209 /* Parse the second argument to __builtin_offsetof. We
7210 must have one identifier, and beyond that we want to
7211 accept sub structure and sub array references. */
7212 if (c_parser_next_token_is (parser, CPP_NAME))
7214 offsetof_ref = build_component_ref
7215 (loc, offsetof_ref, c_parser_peek_token (parser)->value);
7216 c_parser_consume_token (parser);
7217 while (c_parser_next_token_is (parser, CPP_DOT)
7218 || c_parser_next_token_is (parser,
7219 CPP_OPEN_SQUARE)
7220 || c_parser_next_token_is (parser,
7221 CPP_DEREF))
7223 if (c_parser_next_token_is (parser, CPP_DEREF))
7225 loc = c_parser_peek_token (parser)->location;
7226 offsetof_ref = build_array_ref (loc,
7227 offsetof_ref,
7228 integer_zero_node);
7229 goto do_dot;
7231 else if (c_parser_next_token_is (parser, CPP_DOT))
7233 do_dot:
7234 c_parser_consume_token (parser);
7235 if (c_parser_next_token_is_not (parser,
7236 CPP_NAME))
7238 c_parser_error (parser, "expected identifier");
7239 break;
7241 offsetof_ref = build_component_ref
7242 (loc, offsetof_ref,
7243 c_parser_peek_token (parser)->value);
7244 c_parser_consume_token (parser);
7246 else
7248 struct c_expr ce;
7249 tree idx;
7250 loc = c_parser_peek_token (parser)->location;
7251 c_parser_consume_token (parser);
7252 ce = c_parser_expression (parser);
7253 ce = convert_lvalue_to_rvalue (loc, ce, false, false);
7254 idx = ce.value;
7255 idx = c_fully_fold (idx, false, NULL);
7256 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
7257 "expected %<]%>");
7258 offsetof_ref = build_array_ref (loc, offsetof_ref, idx);
7262 else
7263 c_parser_error (parser, "expected identifier");
7264 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7265 "expected %<)%>");
7266 expr.value = fold_offsetof (offsetof_ref);
7268 break;
7269 case RID_CHOOSE_EXPR:
7271 vec<c_expr_t, va_gc> *cexpr_list;
7272 c_expr_t *e1_p, *e2_p, *e3_p;
7273 tree c;
7275 c_parser_consume_token (parser);
7276 if (!c_parser_get_builtin_args (parser,
7277 "__builtin_choose_expr",
7278 &cexpr_list, true))
7280 expr.value = error_mark_node;
7281 break;
7284 if (vec_safe_length (cexpr_list) != 3)
7286 error_at (loc, "wrong number of arguments to "
7287 "%<__builtin_choose_expr%>");
7288 expr.value = error_mark_node;
7289 break;
7292 e1_p = &(*cexpr_list)[0];
7293 e2_p = &(*cexpr_list)[1];
7294 e3_p = &(*cexpr_list)[2];
7296 c = e1_p->value;
7297 mark_exp_read (e2_p->value);
7298 mark_exp_read (e3_p->value);
7299 if (TREE_CODE (c) != INTEGER_CST
7300 || !INTEGRAL_TYPE_P (TREE_TYPE (c)))
7301 error_at (loc,
7302 "first argument to %<__builtin_choose_expr%> not"
7303 " a constant");
7304 constant_expression_warning (c);
7305 expr = integer_zerop (c) ? *e3_p : *e2_p;
7306 break;
7308 case RID_TYPES_COMPATIBLE_P:
7309 c_parser_consume_token (parser);
7310 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7312 expr.value = error_mark_node;
7313 break;
7315 t1 = c_parser_type_name (parser);
7316 if (t1 == NULL)
7318 expr.value = error_mark_node;
7319 break;
7321 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
7323 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7324 expr.value = error_mark_node;
7325 break;
7327 t2 = c_parser_type_name (parser);
7328 if (t2 == NULL)
7330 expr.value = error_mark_node;
7331 break;
7333 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7334 "expected %<)%>");
7336 tree e1, e2;
7337 e1 = groktypename (t1, NULL, NULL);
7338 e2 = groktypename (t2, NULL, NULL);
7339 if (e1 == error_mark_node || e2 == error_mark_node)
7341 expr.value = error_mark_node;
7342 break;
7345 e1 = TYPE_MAIN_VARIANT (e1);
7346 e2 = TYPE_MAIN_VARIANT (e2);
7348 expr.value
7349 = comptypes (e1, e2) ? integer_one_node : integer_zero_node;
7351 break;
7352 case RID_BUILTIN_COMPLEX:
7354 vec<c_expr_t, va_gc> *cexpr_list;
7355 c_expr_t *e1_p, *e2_p;
7357 c_parser_consume_token (parser);
7358 if (!c_parser_get_builtin_args (parser,
7359 "__builtin_complex",
7360 &cexpr_list, false))
7362 expr.value = error_mark_node;
7363 break;
7366 if (vec_safe_length (cexpr_list) != 2)
7368 error_at (loc, "wrong number of arguments to "
7369 "%<__builtin_complex%>");
7370 expr.value = error_mark_node;
7371 break;
7374 e1_p = &(*cexpr_list)[0];
7375 e2_p = &(*cexpr_list)[1];
7377 *e1_p = convert_lvalue_to_rvalue (loc, *e1_p, true, true);
7378 if (TREE_CODE (e1_p->value) == EXCESS_PRECISION_EXPR)
7379 e1_p->value = convert (TREE_TYPE (e1_p->value),
7380 TREE_OPERAND (e1_p->value, 0));
7381 *e2_p = convert_lvalue_to_rvalue (loc, *e2_p, true, true);
7382 if (TREE_CODE (e2_p->value) == EXCESS_PRECISION_EXPR)
7383 e2_p->value = convert (TREE_TYPE (e2_p->value),
7384 TREE_OPERAND (e2_p->value, 0));
7385 if (!SCALAR_FLOAT_TYPE_P (TREE_TYPE (e1_p->value))
7386 || DECIMAL_FLOAT_TYPE_P (TREE_TYPE (e1_p->value))
7387 || !SCALAR_FLOAT_TYPE_P (TREE_TYPE (e2_p->value))
7388 || DECIMAL_FLOAT_TYPE_P (TREE_TYPE (e2_p->value)))
7390 error_at (loc, "%<__builtin_complex%> operand "
7391 "not of real binary floating-point type");
7392 expr.value = error_mark_node;
7393 break;
7395 if (TYPE_MAIN_VARIANT (TREE_TYPE (e1_p->value))
7396 != TYPE_MAIN_VARIANT (TREE_TYPE (e2_p->value)))
7398 error_at (loc,
7399 "%<__builtin_complex%> operands of different types");
7400 expr.value = error_mark_node;
7401 break;
7403 if (!flag_isoc99)
7404 pedwarn (loc, OPT_Wpedantic,
7405 "ISO C90 does not support complex types");
7406 expr.value = build2 (COMPLEX_EXPR,
7407 build_complex_type
7408 (TYPE_MAIN_VARIANT
7409 (TREE_TYPE (e1_p->value))),
7410 e1_p->value, e2_p->value);
7411 break;
7413 case RID_BUILTIN_SHUFFLE:
7415 vec<c_expr_t, va_gc> *cexpr_list;
7416 unsigned int i;
7417 c_expr_t *p;
7419 c_parser_consume_token (parser);
7420 if (!c_parser_get_builtin_args (parser,
7421 "__builtin_shuffle",
7422 &cexpr_list, false))
7424 expr.value = error_mark_node;
7425 break;
7428 FOR_EACH_VEC_SAFE_ELT (cexpr_list, i, p)
7429 *p = convert_lvalue_to_rvalue (loc, *p, true, true);
7431 if (vec_safe_length (cexpr_list) == 2)
7432 expr.value =
7433 c_build_vec_perm_expr
7434 (loc, (*cexpr_list)[0].value,
7435 NULL_TREE, (*cexpr_list)[1].value);
7437 else if (vec_safe_length (cexpr_list) == 3)
7438 expr.value =
7439 c_build_vec_perm_expr
7440 (loc, (*cexpr_list)[0].value,
7441 (*cexpr_list)[1].value,
7442 (*cexpr_list)[2].value);
7443 else
7445 error_at (loc, "wrong number of arguments to "
7446 "%<__builtin_shuffle%>");
7447 expr.value = error_mark_node;
7449 break;
7451 case RID_AT_SELECTOR:
7452 gcc_assert (c_dialect_objc ());
7453 c_parser_consume_token (parser);
7454 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7456 expr.value = error_mark_node;
7457 break;
7460 tree sel = c_parser_objc_selector_arg (parser);
7461 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7462 "expected %<)%>");
7463 expr.value = objc_build_selector_expr (loc, sel);
7465 break;
7466 case RID_AT_PROTOCOL:
7467 gcc_assert (c_dialect_objc ());
7468 c_parser_consume_token (parser);
7469 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7471 expr.value = error_mark_node;
7472 break;
7474 if (c_parser_next_token_is_not (parser, CPP_NAME))
7476 c_parser_error (parser, "expected identifier");
7477 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7478 expr.value = error_mark_node;
7479 break;
7482 tree id = c_parser_peek_token (parser)->value;
7483 c_parser_consume_token (parser);
7484 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7485 "expected %<)%>");
7486 expr.value = objc_build_protocol_expr (id);
7488 break;
7489 case RID_AT_ENCODE:
7490 /* Extension to support C-structures in the archiver. */
7491 gcc_assert (c_dialect_objc ());
7492 c_parser_consume_token (parser);
7493 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7495 expr.value = error_mark_node;
7496 break;
7498 t1 = c_parser_type_name (parser);
7499 if (t1 == NULL)
7501 expr.value = error_mark_node;
7502 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7503 break;
7505 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7506 "expected %<)%>");
7508 tree type = groktypename (t1, NULL, NULL);
7509 expr.value = objc_build_encode_expr (type);
7511 break;
7512 case RID_GENERIC:
7513 expr = c_parser_generic_selection (parser);
7514 break;
7515 case RID_CILK_SPAWN:
7516 c_parser_consume_token (parser);
7517 if (!flag_cilkplus)
7519 error_at (loc, "-fcilkplus must be enabled to use "
7520 "%<_Cilk_spawn%>");
7521 expr = c_parser_postfix_expression (parser);
7522 expr.value = error_mark_node;
7524 else if (c_parser_peek_token (parser)->keyword == RID_CILK_SPAWN)
7526 error_at (loc, "consecutive %<_Cilk_spawn%> keywords "
7527 "are not permitted");
7528 /* Now flush out all the _Cilk_spawns. */
7529 while (c_parser_peek_token (parser)->keyword == RID_CILK_SPAWN)
7530 c_parser_consume_token (parser);
7531 expr = c_parser_postfix_expression (parser);
7533 else
7535 expr = c_parser_postfix_expression (parser);
7536 expr.value = build_cilk_spawn (loc, expr.value);
7538 break;
7539 default:
7540 c_parser_error (parser, "expected expression");
7541 expr.value = error_mark_node;
7542 break;
7544 break;
7545 case CPP_OPEN_SQUARE:
7546 if (c_dialect_objc ())
7548 tree receiver, args;
7549 c_parser_consume_token (parser);
7550 receiver = c_parser_objc_receiver (parser);
7551 args = c_parser_objc_message_args (parser);
7552 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
7553 "expected %<]%>");
7554 expr.value = objc_build_message_expr (receiver, args);
7555 break;
7557 /* Else fall through to report error. */
7558 default:
7559 c_parser_error (parser, "expected expression");
7560 expr.value = error_mark_node;
7561 break;
7563 return c_parser_postfix_expression_after_primary (parser, loc, expr);
7566 /* Parse a postfix expression after a parenthesized type name: the
7567 brace-enclosed initializer of a compound literal, possibly followed
7568 by some postfix operators. This is separate because it is not
7569 possible to tell until after the type name whether a cast
7570 expression has a cast or a compound literal, or whether the operand
7571 of sizeof is a parenthesized type name or starts with a compound
7572 literal. TYPE_LOC is the location where TYPE_NAME starts--the
7573 location of the first token after the parentheses around the type
7574 name. */
7576 static struct c_expr
7577 c_parser_postfix_expression_after_paren_type (c_parser *parser,
7578 struct c_type_name *type_name,
7579 location_t type_loc)
7581 tree type;
7582 struct c_expr init;
7583 bool non_const;
7584 struct c_expr expr;
7585 location_t start_loc;
7586 tree type_expr = NULL_TREE;
7587 bool type_expr_const = true;
7588 check_compound_literal_type (type_loc, type_name);
7589 start_init (NULL_TREE, NULL, 0);
7590 type = groktypename (type_name, &type_expr, &type_expr_const);
7591 start_loc = c_parser_peek_token (parser)->location;
7592 if (type != error_mark_node && C_TYPE_VARIABLE_SIZE (type))
7594 error_at (type_loc, "compound literal has variable size");
7595 type = error_mark_node;
7597 init = c_parser_braced_init (parser, type, false);
7598 finish_init ();
7599 maybe_warn_string_init (type_loc, type, init);
7601 if (type != error_mark_node
7602 && !ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (type))
7603 && current_function_decl)
7605 error ("compound literal qualified by address-space qualifier");
7606 type = error_mark_node;
7609 if (!flag_isoc99)
7610 pedwarn (start_loc, OPT_Wpedantic, "ISO C90 forbids compound literals");
7611 non_const = ((init.value && TREE_CODE (init.value) == CONSTRUCTOR)
7612 ? CONSTRUCTOR_NON_CONST (init.value)
7613 : init.original_code == C_MAYBE_CONST_EXPR);
7614 non_const |= !type_expr_const;
7615 expr.value = build_compound_literal (start_loc, type, init.value, non_const);
7616 expr.original_code = ERROR_MARK;
7617 expr.original_type = NULL;
7618 if (type_expr)
7620 if (TREE_CODE (expr.value) == C_MAYBE_CONST_EXPR)
7622 gcc_assert (C_MAYBE_CONST_EXPR_PRE (expr.value) == NULL_TREE);
7623 C_MAYBE_CONST_EXPR_PRE (expr.value) = type_expr;
7625 else
7627 gcc_assert (!non_const);
7628 expr.value = build2 (C_MAYBE_CONST_EXPR, type,
7629 type_expr, expr.value);
7632 return c_parser_postfix_expression_after_primary (parser, start_loc, expr);
7635 /* Callback function for sizeof_pointer_memaccess_warning to compare
7636 types. */
7638 static bool
7639 sizeof_ptr_memacc_comptypes (tree type1, tree type2)
7641 return comptypes (type1, type2) == 1;
7644 /* Parse a postfix expression after the initial primary or compound
7645 literal; that is, parse a series of postfix operators.
7647 EXPR_LOC is the location of the primary expression. */
7649 static struct c_expr
7650 c_parser_postfix_expression_after_primary (c_parser *parser,
7651 location_t expr_loc,
7652 struct c_expr expr)
7654 struct c_expr orig_expr;
7655 tree ident, idx;
7656 location_t sizeof_arg_loc[3];
7657 tree sizeof_arg[3];
7658 unsigned int i;
7659 vec<tree, va_gc> *exprlist;
7660 vec<tree, va_gc> *origtypes = NULL;
7661 vec<location_t> arg_loc = vNULL;
7663 while (true)
7665 location_t op_loc = c_parser_peek_token (parser)->location;
7666 switch (c_parser_peek_token (parser)->type)
7668 case CPP_OPEN_SQUARE:
7669 /* Array reference. */
7670 c_parser_consume_token (parser);
7671 if (flag_cilkplus
7672 && c_parser_peek_token (parser)->type == CPP_COLON)
7673 /* If we are here, then we have something like this:
7674 Array [ : ]
7676 expr.value = c_parser_array_notation (expr_loc, parser, NULL_TREE,
7677 expr.value);
7678 else
7680 idx = c_parser_expression (parser).value;
7681 /* Here we have 3 options:
7682 1. Array [EXPR] -- Normal Array call.
7683 2. Array [EXPR : EXPR] -- Array notation without stride.
7684 3. Array [EXPR : EXPR : EXPR] -- Array notation with stride.
7686 For 1, we just handle it just like a normal array expression.
7687 For 2 and 3 we handle it like we handle array notations. The
7688 idx value we have above becomes the initial/start index.
7690 if (flag_cilkplus
7691 && c_parser_peek_token (parser)->type == CPP_COLON)
7692 expr.value = c_parser_array_notation (expr_loc, parser, idx,
7693 expr.value);
7694 else
7696 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
7697 "expected %<]%>");
7698 expr.value = build_array_ref (op_loc, expr.value, idx);
7701 expr.original_code = ERROR_MARK;
7702 expr.original_type = NULL;
7703 break;
7704 case CPP_OPEN_PAREN:
7705 /* Function call. */
7706 c_parser_consume_token (parser);
7707 for (i = 0; i < 3; i++)
7709 sizeof_arg[i] = NULL_TREE;
7710 sizeof_arg_loc[i] = UNKNOWN_LOCATION;
7712 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
7713 exprlist = NULL;
7714 else
7715 exprlist = c_parser_expr_list (parser, true, false, &origtypes,
7716 sizeof_arg_loc, sizeof_arg,
7717 &arg_loc);
7718 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7719 "expected %<)%>");
7720 orig_expr = expr;
7721 mark_exp_read (expr.value);
7722 if (warn_sizeof_pointer_memaccess)
7723 sizeof_pointer_memaccess_warning (sizeof_arg_loc,
7724 expr.value, exprlist,
7725 sizeof_arg,
7726 sizeof_ptr_memacc_comptypes);
7727 expr.value
7728 = c_build_function_call_vec (expr_loc, arg_loc, expr.value,
7729 exprlist, origtypes);
7730 expr.original_code = ERROR_MARK;
7731 if (TREE_CODE (expr.value) == INTEGER_CST
7732 && TREE_CODE (orig_expr.value) == FUNCTION_DECL
7733 && DECL_BUILT_IN_CLASS (orig_expr.value) == BUILT_IN_NORMAL
7734 && DECL_FUNCTION_CODE (orig_expr.value) == BUILT_IN_CONSTANT_P)
7735 expr.original_code = C_MAYBE_CONST_EXPR;
7736 expr.original_type = NULL;
7737 if (exprlist)
7739 release_tree_vector (exprlist);
7740 release_tree_vector (origtypes);
7742 arg_loc.release ();
7743 break;
7744 case CPP_DOT:
7745 /* Structure element reference. */
7746 c_parser_consume_token (parser);
7747 expr = default_function_array_conversion (expr_loc, expr);
7748 if (c_parser_next_token_is (parser, CPP_NAME))
7749 ident = c_parser_peek_token (parser)->value;
7750 else
7752 c_parser_error (parser, "expected identifier");
7753 expr.value = error_mark_node;
7754 expr.original_code = ERROR_MARK;
7755 expr.original_type = NULL;
7756 return expr;
7758 c_parser_consume_token (parser);
7759 expr.value = build_component_ref (op_loc, expr.value, ident);
7760 expr.original_code = ERROR_MARK;
7761 if (TREE_CODE (expr.value) != COMPONENT_REF)
7762 expr.original_type = NULL;
7763 else
7765 /* Remember the original type of a bitfield. */
7766 tree field = TREE_OPERAND (expr.value, 1);
7767 if (TREE_CODE (field) != FIELD_DECL)
7768 expr.original_type = NULL;
7769 else
7770 expr.original_type = DECL_BIT_FIELD_TYPE (field);
7772 break;
7773 case CPP_DEREF:
7774 /* Structure element reference. */
7775 c_parser_consume_token (parser);
7776 expr = convert_lvalue_to_rvalue (expr_loc, expr, true, false);
7777 if (c_parser_next_token_is (parser, CPP_NAME))
7778 ident = c_parser_peek_token (parser)->value;
7779 else
7781 c_parser_error (parser, "expected identifier");
7782 expr.value = error_mark_node;
7783 expr.original_code = ERROR_MARK;
7784 expr.original_type = NULL;
7785 return expr;
7787 c_parser_consume_token (parser);
7788 expr.value = build_component_ref (op_loc,
7789 build_indirect_ref (op_loc,
7790 expr.value,
7791 RO_ARROW),
7792 ident);
7793 expr.original_code = ERROR_MARK;
7794 if (TREE_CODE (expr.value) != COMPONENT_REF)
7795 expr.original_type = NULL;
7796 else
7798 /* Remember the original type of a bitfield. */
7799 tree field = TREE_OPERAND (expr.value, 1);
7800 if (TREE_CODE (field) != FIELD_DECL)
7801 expr.original_type = NULL;
7802 else
7803 expr.original_type = DECL_BIT_FIELD_TYPE (field);
7805 break;
7806 case CPP_PLUS_PLUS:
7807 /* Postincrement. */
7808 c_parser_consume_token (parser);
7809 /* If the expressions have array notations, we expand them. */
7810 if (flag_cilkplus
7811 && TREE_CODE (expr.value) == ARRAY_NOTATION_REF)
7812 expr = fix_array_notation_expr (expr_loc, POSTINCREMENT_EXPR, expr);
7813 else
7815 expr = default_function_array_read_conversion (expr_loc, expr);
7816 expr.value = build_unary_op (op_loc,
7817 POSTINCREMENT_EXPR, expr.value, 0);
7819 expr.original_code = ERROR_MARK;
7820 expr.original_type = NULL;
7821 break;
7822 case CPP_MINUS_MINUS:
7823 /* Postdecrement. */
7824 c_parser_consume_token (parser);
7825 /* If the expressions have array notations, we expand them. */
7826 if (flag_cilkplus
7827 && TREE_CODE (expr.value) == ARRAY_NOTATION_REF)
7828 expr = fix_array_notation_expr (expr_loc, POSTDECREMENT_EXPR, expr);
7829 else
7831 expr = default_function_array_read_conversion (expr_loc, expr);
7832 expr.value = build_unary_op (op_loc,
7833 POSTDECREMENT_EXPR, expr.value, 0);
7835 expr.original_code = ERROR_MARK;
7836 expr.original_type = NULL;
7837 break;
7838 default:
7839 return expr;
7844 /* Parse an expression (C90 6.3.17, C99 6.5.17).
7846 expression:
7847 assignment-expression
7848 expression , assignment-expression
7851 static struct c_expr
7852 c_parser_expression (c_parser *parser)
7854 location_t tloc = c_parser_peek_token (parser)->location;
7855 struct c_expr expr;
7856 expr = c_parser_expr_no_commas (parser, NULL);
7857 if (c_parser_next_token_is (parser, CPP_COMMA))
7858 expr = convert_lvalue_to_rvalue (tloc, expr, true, false);
7859 while (c_parser_next_token_is (parser, CPP_COMMA))
7861 struct c_expr next;
7862 tree lhsval;
7863 location_t loc = c_parser_peek_token (parser)->location;
7864 location_t expr_loc;
7865 c_parser_consume_token (parser);
7866 expr_loc = c_parser_peek_token (parser)->location;
7867 lhsval = expr.value;
7868 while (TREE_CODE (lhsval) == COMPOUND_EXPR)
7869 lhsval = TREE_OPERAND (lhsval, 1);
7870 if (DECL_P (lhsval) || handled_component_p (lhsval))
7871 mark_exp_read (lhsval);
7872 next = c_parser_expr_no_commas (parser, NULL);
7873 next = convert_lvalue_to_rvalue (expr_loc, next, true, false);
7874 expr.value = build_compound_expr (loc, expr.value, next.value);
7875 expr.original_code = COMPOUND_EXPR;
7876 expr.original_type = next.original_type;
7878 return expr;
7881 /* Parse an expression and convert functions or arrays to pointers and
7882 lvalues to rvalues. */
7884 static struct c_expr
7885 c_parser_expression_conv (c_parser *parser)
7887 struct c_expr expr;
7888 location_t loc = c_parser_peek_token (parser)->location;
7889 expr = c_parser_expression (parser);
7890 expr = convert_lvalue_to_rvalue (loc, expr, true, false);
7891 return expr;
7894 /* Parse a non-empty list of expressions. If CONVERT_P, convert
7895 functions and arrays to pointers and lvalues to rvalues. If
7896 FOLD_P, fold the expressions. If LOCATIONS is non-NULL, save the
7897 locations of function arguments into this vector.
7899 nonempty-expr-list:
7900 assignment-expression
7901 nonempty-expr-list , assignment-expression
7904 static vec<tree, va_gc> *
7905 c_parser_expr_list (c_parser *parser, bool convert_p, bool fold_p,
7906 vec<tree, va_gc> **p_orig_types,
7907 location_t *sizeof_arg_loc, tree *sizeof_arg,
7908 vec<location_t> *locations)
7910 vec<tree, va_gc> *ret;
7911 vec<tree, va_gc> *orig_types;
7912 struct c_expr expr;
7913 location_t loc = c_parser_peek_token (parser)->location;
7914 location_t cur_sizeof_arg_loc = UNKNOWN_LOCATION;
7915 unsigned int idx = 0;
7917 ret = make_tree_vector ();
7918 if (p_orig_types == NULL)
7919 orig_types = NULL;
7920 else
7921 orig_types = make_tree_vector ();
7923 if (sizeof_arg != NULL
7924 && c_parser_next_token_is_keyword (parser, RID_SIZEOF))
7925 cur_sizeof_arg_loc = c_parser_peek_2nd_token (parser)->location;
7926 expr = c_parser_expr_no_commas (parser, NULL);
7927 if (convert_p)
7928 expr = convert_lvalue_to_rvalue (loc, expr, true, true);
7929 if (fold_p)
7930 expr.value = c_fully_fold (expr.value, false, NULL);
7931 ret->quick_push (expr.value);
7932 if (orig_types)
7933 orig_types->quick_push (expr.original_type);
7934 if (locations)
7935 locations->safe_push (loc);
7936 if (sizeof_arg != NULL
7937 && cur_sizeof_arg_loc != UNKNOWN_LOCATION
7938 && expr.original_code == SIZEOF_EXPR)
7940 sizeof_arg[0] = c_last_sizeof_arg;
7941 sizeof_arg_loc[0] = cur_sizeof_arg_loc;
7943 while (c_parser_next_token_is (parser, CPP_COMMA))
7945 c_parser_consume_token (parser);
7946 loc = c_parser_peek_token (parser)->location;
7947 if (sizeof_arg != NULL
7948 && c_parser_next_token_is_keyword (parser, RID_SIZEOF))
7949 cur_sizeof_arg_loc = c_parser_peek_2nd_token (parser)->location;
7950 else
7951 cur_sizeof_arg_loc = UNKNOWN_LOCATION;
7952 expr = c_parser_expr_no_commas (parser, NULL);
7953 if (convert_p)
7954 expr = convert_lvalue_to_rvalue (loc, expr, true, true);
7955 if (fold_p)
7956 expr.value = c_fully_fold (expr.value, false, NULL);
7957 vec_safe_push (ret, expr.value);
7958 if (orig_types)
7959 vec_safe_push (orig_types, expr.original_type);
7960 if (locations)
7961 locations->safe_push (loc);
7962 if (++idx < 3
7963 && sizeof_arg != NULL
7964 && cur_sizeof_arg_loc != UNKNOWN_LOCATION
7965 && expr.original_code == SIZEOF_EXPR)
7967 sizeof_arg[idx] = c_last_sizeof_arg;
7968 sizeof_arg_loc[idx] = cur_sizeof_arg_loc;
7971 if (orig_types)
7972 *p_orig_types = orig_types;
7973 return ret;
7976 /* Parse Objective-C-specific constructs. */
7978 /* Parse an objc-class-definition.
7980 objc-class-definition:
7981 @interface identifier objc-superclass[opt] objc-protocol-refs[opt]
7982 objc-class-instance-variables[opt] objc-methodprotolist @end
7983 @implementation identifier objc-superclass[opt]
7984 objc-class-instance-variables[opt]
7985 @interface identifier ( identifier ) objc-protocol-refs[opt]
7986 objc-methodprotolist @end
7987 @interface identifier ( ) objc-protocol-refs[opt]
7988 objc-methodprotolist @end
7989 @implementation identifier ( identifier )
7991 objc-superclass:
7992 : identifier
7994 "@interface identifier (" must start "@interface identifier (
7995 identifier ) ...": objc-methodprotolist in the first production may
7996 not start with a parenthesized identifier as a declarator of a data
7997 definition with no declaration specifiers if the objc-superclass,
7998 objc-protocol-refs and objc-class-instance-variables are omitted. */
8000 static void
8001 c_parser_objc_class_definition (c_parser *parser, tree attributes)
8003 bool iface_p;
8004 tree id1;
8005 tree superclass;
8006 if (c_parser_next_token_is_keyword (parser, RID_AT_INTERFACE))
8007 iface_p = true;
8008 else if (c_parser_next_token_is_keyword (parser, RID_AT_IMPLEMENTATION))
8009 iface_p = false;
8010 else
8011 gcc_unreachable ();
8013 c_parser_consume_token (parser);
8014 if (c_parser_next_token_is_not (parser, CPP_NAME))
8016 c_parser_error (parser, "expected identifier");
8017 return;
8019 id1 = c_parser_peek_token (parser)->value;
8020 c_parser_consume_token (parser);
8021 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
8023 /* We have a category or class extension. */
8024 tree id2;
8025 tree proto = NULL_TREE;
8026 c_parser_consume_token (parser);
8027 if (c_parser_next_token_is_not (parser, CPP_NAME))
8029 if (iface_p && c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
8031 /* We have a class extension. */
8032 id2 = NULL_TREE;
8034 else
8036 c_parser_error (parser, "expected identifier or %<)%>");
8037 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
8038 return;
8041 else
8043 id2 = c_parser_peek_token (parser)->value;
8044 c_parser_consume_token (parser);
8046 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
8047 if (!iface_p)
8049 objc_start_category_implementation (id1, id2);
8050 return;
8052 if (c_parser_next_token_is (parser, CPP_LESS))
8053 proto = c_parser_objc_protocol_refs (parser);
8054 objc_start_category_interface (id1, id2, proto, attributes);
8055 c_parser_objc_methodprotolist (parser);
8056 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
8057 objc_finish_interface ();
8058 return;
8060 if (c_parser_next_token_is (parser, CPP_COLON))
8062 c_parser_consume_token (parser);
8063 if (c_parser_next_token_is_not (parser, CPP_NAME))
8065 c_parser_error (parser, "expected identifier");
8066 return;
8068 superclass = c_parser_peek_token (parser)->value;
8069 c_parser_consume_token (parser);
8071 else
8072 superclass = NULL_TREE;
8073 if (iface_p)
8075 tree proto = NULL_TREE;
8076 if (c_parser_next_token_is (parser, CPP_LESS))
8077 proto = c_parser_objc_protocol_refs (parser);
8078 objc_start_class_interface (id1, superclass, proto, attributes);
8080 else
8081 objc_start_class_implementation (id1, superclass);
8082 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
8083 c_parser_objc_class_instance_variables (parser);
8084 if (iface_p)
8086 objc_continue_interface ();
8087 c_parser_objc_methodprotolist (parser);
8088 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
8089 objc_finish_interface ();
8091 else
8093 objc_continue_implementation ();
8094 return;
8098 /* Parse objc-class-instance-variables.
8100 objc-class-instance-variables:
8101 { objc-instance-variable-decl-list[opt] }
8103 objc-instance-variable-decl-list:
8104 objc-visibility-spec
8105 objc-instance-variable-decl ;
8107 objc-instance-variable-decl-list objc-visibility-spec
8108 objc-instance-variable-decl-list objc-instance-variable-decl ;
8109 objc-instance-variable-decl-list ;
8111 objc-visibility-spec:
8112 @private
8113 @protected
8114 @public
8116 objc-instance-variable-decl:
8117 struct-declaration
8120 static void
8121 c_parser_objc_class_instance_variables (c_parser *parser)
8123 gcc_assert (c_parser_next_token_is (parser, CPP_OPEN_BRACE));
8124 c_parser_consume_token (parser);
8125 while (c_parser_next_token_is_not (parser, CPP_EOF))
8127 tree decls;
8128 /* Parse any stray semicolon. */
8129 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
8131 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
8132 "extra semicolon");
8133 c_parser_consume_token (parser);
8134 continue;
8136 /* Stop if at the end of the instance variables. */
8137 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
8139 c_parser_consume_token (parser);
8140 break;
8142 /* Parse any objc-visibility-spec. */
8143 if (c_parser_next_token_is_keyword (parser, RID_AT_PRIVATE))
8145 c_parser_consume_token (parser);
8146 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
8147 continue;
8149 else if (c_parser_next_token_is_keyword (parser, RID_AT_PROTECTED))
8151 c_parser_consume_token (parser);
8152 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
8153 continue;
8155 else if (c_parser_next_token_is_keyword (parser, RID_AT_PUBLIC))
8157 c_parser_consume_token (parser);
8158 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
8159 continue;
8161 else if (c_parser_next_token_is_keyword (parser, RID_AT_PACKAGE))
8163 c_parser_consume_token (parser);
8164 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
8165 continue;
8167 else if (c_parser_next_token_is (parser, CPP_PRAGMA))
8169 c_parser_pragma (parser, pragma_external);
8170 continue;
8173 /* Parse some comma-separated declarations. */
8174 decls = c_parser_struct_declaration (parser);
8175 if (decls == NULL)
8177 /* There is a syntax error. We want to skip the offending
8178 tokens up to the next ';' (included) or '}'
8179 (excluded). */
8181 /* First, skip manually a ')' or ']'. This is because they
8182 reduce the nesting level, so c_parser_skip_until_found()
8183 wouldn't be able to skip past them. */
8184 c_token *token = c_parser_peek_token (parser);
8185 if (token->type == CPP_CLOSE_PAREN || token->type == CPP_CLOSE_SQUARE)
8186 c_parser_consume_token (parser);
8188 /* Then, do the standard skipping. */
8189 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
8191 /* We hopefully recovered. Start normal parsing again. */
8192 parser->error = false;
8193 continue;
8195 else
8197 /* Comma-separated instance variables are chained together
8198 in reverse order; add them one by one. */
8199 tree ivar = nreverse (decls);
8200 for (; ivar; ivar = DECL_CHAIN (ivar))
8201 objc_add_instance_variable (copy_node (ivar));
8203 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
8207 /* Parse an objc-class-declaration.
8209 objc-class-declaration:
8210 @class identifier-list ;
8213 static void
8214 c_parser_objc_class_declaration (c_parser *parser)
8216 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_CLASS));
8217 c_parser_consume_token (parser);
8218 /* Any identifiers, including those declared as type names, are OK
8219 here. */
8220 while (true)
8222 tree id;
8223 if (c_parser_next_token_is_not (parser, CPP_NAME))
8225 c_parser_error (parser, "expected identifier");
8226 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
8227 parser->error = false;
8228 return;
8230 id = c_parser_peek_token (parser)->value;
8231 objc_declare_class (id);
8232 c_parser_consume_token (parser);
8233 if (c_parser_next_token_is (parser, CPP_COMMA))
8234 c_parser_consume_token (parser);
8235 else
8236 break;
8238 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
8241 /* Parse an objc-alias-declaration.
8243 objc-alias-declaration:
8244 @compatibility_alias identifier identifier ;
8247 static void
8248 c_parser_objc_alias_declaration (c_parser *parser)
8250 tree id1, id2;
8251 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_ALIAS));
8252 c_parser_consume_token (parser);
8253 if (c_parser_next_token_is_not (parser, CPP_NAME))
8255 c_parser_error (parser, "expected identifier");
8256 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
8257 return;
8259 id1 = c_parser_peek_token (parser)->value;
8260 c_parser_consume_token (parser);
8261 if (c_parser_next_token_is_not (parser, CPP_NAME))
8263 c_parser_error (parser, "expected identifier");
8264 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
8265 return;
8267 id2 = c_parser_peek_token (parser)->value;
8268 c_parser_consume_token (parser);
8269 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
8270 objc_declare_alias (id1, id2);
8273 /* Parse an objc-protocol-definition.
8275 objc-protocol-definition:
8276 @protocol identifier objc-protocol-refs[opt] objc-methodprotolist @end
8277 @protocol identifier-list ;
8279 "@protocol identifier ;" should be resolved as "@protocol
8280 identifier-list ;": objc-methodprotolist may not start with a
8281 semicolon in the first alternative if objc-protocol-refs are
8282 omitted. */
8284 static void
8285 c_parser_objc_protocol_definition (c_parser *parser, tree attributes)
8287 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_PROTOCOL));
8289 c_parser_consume_token (parser);
8290 if (c_parser_next_token_is_not (parser, CPP_NAME))
8292 c_parser_error (parser, "expected identifier");
8293 return;
8295 if (c_parser_peek_2nd_token (parser)->type == CPP_COMMA
8296 || c_parser_peek_2nd_token (parser)->type == CPP_SEMICOLON)
8298 /* Any identifiers, including those declared as type names, are
8299 OK here. */
8300 while (true)
8302 tree id;
8303 if (c_parser_next_token_is_not (parser, CPP_NAME))
8305 c_parser_error (parser, "expected identifier");
8306 break;
8308 id = c_parser_peek_token (parser)->value;
8309 objc_declare_protocol (id, attributes);
8310 c_parser_consume_token (parser);
8311 if (c_parser_next_token_is (parser, CPP_COMMA))
8312 c_parser_consume_token (parser);
8313 else
8314 break;
8316 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
8318 else
8320 tree id = c_parser_peek_token (parser)->value;
8321 tree proto = NULL_TREE;
8322 c_parser_consume_token (parser);
8323 if (c_parser_next_token_is (parser, CPP_LESS))
8324 proto = c_parser_objc_protocol_refs (parser);
8325 parser->objc_pq_context = true;
8326 objc_start_protocol (id, proto, attributes);
8327 c_parser_objc_methodprotolist (parser);
8328 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
8329 parser->objc_pq_context = false;
8330 objc_finish_interface ();
8334 /* Parse an objc-method-type.
8336 objc-method-type:
8340 Return true if it is a class method (+) and false if it is
8341 an instance method (-).
8343 static inline bool
8344 c_parser_objc_method_type (c_parser *parser)
8346 switch (c_parser_peek_token (parser)->type)
8348 case CPP_PLUS:
8349 c_parser_consume_token (parser);
8350 return true;
8351 case CPP_MINUS:
8352 c_parser_consume_token (parser);
8353 return false;
8354 default:
8355 gcc_unreachable ();
8359 /* Parse an objc-method-definition.
8361 objc-method-definition:
8362 objc-method-type objc-method-decl ;[opt] compound-statement
8365 static void
8366 c_parser_objc_method_definition (c_parser *parser)
8368 bool is_class_method = c_parser_objc_method_type (parser);
8369 tree decl, attributes = NULL_TREE, expr = NULL_TREE;
8370 parser->objc_pq_context = true;
8371 decl = c_parser_objc_method_decl (parser, is_class_method, &attributes,
8372 &expr);
8373 if (decl == error_mark_node)
8374 return; /* Bail here. */
8376 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
8378 c_parser_consume_token (parser);
8379 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
8380 "extra semicolon in method definition specified");
8383 if (!c_parser_next_token_is (parser, CPP_OPEN_BRACE))
8385 c_parser_error (parser, "expected %<{%>");
8386 return;
8389 parser->objc_pq_context = false;
8390 if (objc_start_method_definition (is_class_method, decl, attributes, expr))
8392 add_stmt (c_parser_compound_statement (parser));
8393 objc_finish_method_definition (current_function_decl);
8395 else
8397 /* This code is executed when we find a method definition
8398 outside of an @implementation context (or invalid for other
8399 reasons). Parse the method (to keep going) but do not emit
8400 any code.
8402 c_parser_compound_statement (parser);
8406 /* Parse an objc-methodprotolist.
8408 objc-methodprotolist:
8409 empty
8410 objc-methodprotolist objc-methodproto
8411 objc-methodprotolist declaration
8412 objc-methodprotolist ;
8413 @optional
8414 @required
8416 The declaration is a data definition, which may be missing
8417 declaration specifiers under the same rules and diagnostics as
8418 other data definitions outside functions, and the stray semicolon
8419 is diagnosed the same way as a stray semicolon outside a
8420 function. */
8422 static void
8423 c_parser_objc_methodprotolist (c_parser *parser)
8425 while (true)
8427 /* The list is terminated by @end. */
8428 switch (c_parser_peek_token (parser)->type)
8430 case CPP_SEMICOLON:
8431 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
8432 "ISO C does not allow extra %<;%> outside of a function");
8433 c_parser_consume_token (parser);
8434 break;
8435 case CPP_PLUS:
8436 case CPP_MINUS:
8437 c_parser_objc_methodproto (parser);
8438 break;
8439 case CPP_PRAGMA:
8440 c_parser_pragma (parser, pragma_external);
8441 break;
8442 case CPP_EOF:
8443 return;
8444 default:
8445 if (c_parser_next_token_is_keyword (parser, RID_AT_END))
8446 return;
8447 else if (c_parser_next_token_is_keyword (parser, RID_AT_PROPERTY))
8448 c_parser_objc_at_property_declaration (parser);
8449 else if (c_parser_next_token_is_keyword (parser, RID_AT_OPTIONAL))
8451 objc_set_method_opt (true);
8452 c_parser_consume_token (parser);
8454 else if (c_parser_next_token_is_keyword (parser, RID_AT_REQUIRED))
8456 objc_set_method_opt (false);
8457 c_parser_consume_token (parser);
8459 else
8460 c_parser_declaration_or_fndef (parser, false, false, true,
8461 false, true, NULL, vNULL);
8462 break;
8467 /* Parse an objc-methodproto.
8469 objc-methodproto:
8470 objc-method-type objc-method-decl ;
8473 static void
8474 c_parser_objc_methodproto (c_parser *parser)
8476 bool is_class_method = c_parser_objc_method_type (parser);
8477 tree decl, attributes = NULL_TREE;
8479 /* Remember protocol qualifiers in prototypes. */
8480 parser->objc_pq_context = true;
8481 decl = c_parser_objc_method_decl (parser, is_class_method, &attributes,
8482 NULL);
8483 /* Forget protocol qualifiers now. */
8484 parser->objc_pq_context = false;
8486 /* Do not allow the presence of attributes to hide an erroneous
8487 method implementation in the interface section. */
8488 if (!c_parser_next_token_is (parser, CPP_SEMICOLON))
8490 c_parser_error (parser, "expected %<;%>");
8491 return;
8494 if (decl != error_mark_node)
8495 objc_add_method_declaration (is_class_method, decl, attributes);
8497 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
8500 /* If we are at a position that method attributes may be present, check that
8501 there are not any parsed already (a syntax error) and then collect any
8502 specified at the current location. Finally, if new attributes were present,
8503 check that the next token is legal ( ';' for decls and '{' for defs). */
8505 static bool
8506 c_parser_objc_maybe_method_attributes (c_parser* parser, tree* attributes)
8508 bool bad = false;
8509 if (*attributes)
8511 c_parser_error (parser,
8512 "method attributes must be specified at the end only");
8513 *attributes = NULL_TREE;
8514 bad = true;
8517 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
8518 *attributes = c_parser_attributes (parser);
8520 /* If there were no attributes here, just report any earlier error. */
8521 if (*attributes == NULL_TREE || bad)
8522 return bad;
8524 /* If the attributes are followed by a ; or {, then just report any earlier
8525 error. */
8526 if (c_parser_next_token_is (parser, CPP_SEMICOLON)
8527 || c_parser_next_token_is (parser, CPP_OPEN_BRACE))
8528 return bad;
8530 /* We've got attributes, but not at the end. */
8531 c_parser_error (parser,
8532 "expected %<;%> or %<{%> after method attribute definition");
8533 return true;
8536 /* Parse an objc-method-decl.
8538 objc-method-decl:
8539 ( objc-type-name ) objc-selector
8540 objc-selector
8541 ( objc-type-name ) objc-keyword-selector objc-optparmlist
8542 objc-keyword-selector objc-optparmlist
8543 attributes
8545 objc-keyword-selector:
8546 objc-keyword-decl
8547 objc-keyword-selector objc-keyword-decl
8549 objc-keyword-decl:
8550 objc-selector : ( objc-type-name ) identifier
8551 objc-selector : identifier
8552 : ( objc-type-name ) identifier
8553 : identifier
8555 objc-optparmlist:
8556 objc-optparms objc-optellipsis
8558 objc-optparms:
8559 empty
8560 objc-opt-parms , parameter-declaration
8562 objc-optellipsis:
8563 empty
8564 , ...
8567 static tree
8568 c_parser_objc_method_decl (c_parser *parser, bool is_class_method,
8569 tree *attributes, tree *expr)
8571 tree type = NULL_TREE;
8572 tree sel;
8573 tree parms = NULL_TREE;
8574 bool ellipsis = false;
8575 bool attr_err = false;
8577 *attributes = NULL_TREE;
8578 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
8580 c_parser_consume_token (parser);
8581 type = c_parser_objc_type_name (parser);
8582 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
8584 sel = c_parser_objc_selector (parser);
8585 /* If there is no selector, or a colon follows, we have an
8586 objc-keyword-selector. If there is a selector, and a colon does
8587 not follow, that selector ends the objc-method-decl. */
8588 if (!sel || c_parser_next_token_is (parser, CPP_COLON))
8590 tree tsel = sel;
8591 tree list = NULL_TREE;
8592 while (true)
8594 tree atype = NULL_TREE, id, keyworddecl;
8595 tree param_attr = NULL_TREE;
8596 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
8597 break;
8598 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
8600 c_parser_consume_token (parser);
8601 atype = c_parser_objc_type_name (parser);
8602 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
8603 "expected %<)%>");
8605 /* New ObjC allows attributes on method parameters. */
8606 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
8607 param_attr = c_parser_attributes (parser);
8608 if (c_parser_next_token_is_not (parser, CPP_NAME))
8610 c_parser_error (parser, "expected identifier");
8611 return error_mark_node;
8613 id = c_parser_peek_token (parser)->value;
8614 c_parser_consume_token (parser);
8615 keyworddecl = objc_build_keyword_decl (tsel, atype, id, param_attr);
8616 list = chainon (list, keyworddecl);
8617 tsel = c_parser_objc_selector (parser);
8618 if (!tsel && c_parser_next_token_is_not (parser, CPP_COLON))
8619 break;
8622 attr_err |= c_parser_objc_maybe_method_attributes (parser, attributes) ;
8624 /* Parse the optional parameter list. Optional Objective-C
8625 method parameters follow the C syntax, and may include '...'
8626 to denote a variable number of arguments. */
8627 parms = make_node (TREE_LIST);
8628 while (c_parser_next_token_is (parser, CPP_COMMA))
8630 struct c_parm *parm;
8631 c_parser_consume_token (parser);
8632 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
8634 ellipsis = true;
8635 c_parser_consume_token (parser);
8636 attr_err |= c_parser_objc_maybe_method_attributes
8637 (parser, attributes) ;
8638 break;
8640 parm = c_parser_parameter_declaration (parser, NULL_TREE);
8641 if (parm == NULL)
8642 break;
8643 parms = chainon (parms,
8644 build_tree_list (NULL_TREE, grokparm (parm, expr)));
8646 sel = list;
8648 else
8649 attr_err |= c_parser_objc_maybe_method_attributes (parser, attributes) ;
8651 if (sel == NULL)
8653 c_parser_error (parser, "objective-c method declaration is expected");
8654 return error_mark_node;
8657 if (attr_err)
8658 return error_mark_node;
8660 return objc_build_method_signature (is_class_method, type, sel, parms, ellipsis);
8663 /* Parse an objc-type-name.
8665 objc-type-name:
8666 objc-type-qualifiers[opt] type-name
8667 objc-type-qualifiers[opt]
8669 objc-type-qualifiers:
8670 objc-type-qualifier
8671 objc-type-qualifiers objc-type-qualifier
8673 objc-type-qualifier: one of
8674 in out inout bycopy byref oneway
8677 static tree
8678 c_parser_objc_type_name (c_parser *parser)
8680 tree quals = NULL_TREE;
8681 struct c_type_name *type_name = NULL;
8682 tree type = NULL_TREE;
8683 while (true)
8685 c_token *token = c_parser_peek_token (parser);
8686 if (token->type == CPP_KEYWORD
8687 && (token->keyword == RID_IN
8688 || token->keyword == RID_OUT
8689 || token->keyword == RID_INOUT
8690 || token->keyword == RID_BYCOPY
8691 || token->keyword == RID_BYREF
8692 || token->keyword == RID_ONEWAY))
8694 quals = chainon (build_tree_list (NULL_TREE, token->value), quals);
8695 c_parser_consume_token (parser);
8697 else
8698 break;
8700 if (c_parser_next_tokens_start_typename (parser, cla_prefer_type))
8701 type_name = c_parser_type_name (parser);
8702 if (type_name)
8703 type = groktypename (type_name, NULL, NULL);
8705 /* If the type is unknown, and error has already been produced and
8706 we need to recover from the error. In that case, use NULL_TREE
8707 for the type, as if no type had been specified; this will use the
8708 default type ('id') which is good for error recovery. */
8709 if (type == error_mark_node)
8710 type = NULL_TREE;
8712 return build_tree_list (quals, type);
8715 /* Parse objc-protocol-refs.
8717 objc-protocol-refs:
8718 < identifier-list >
8721 static tree
8722 c_parser_objc_protocol_refs (c_parser *parser)
8724 tree list = NULL_TREE;
8725 gcc_assert (c_parser_next_token_is (parser, CPP_LESS));
8726 c_parser_consume_token (parser);
8727 /* Any identifiers, including those declared as type names, are OK
8728 here. */
8729 while (true)
8731 tree id;
8732 if (c_parser_next_token_is_not (parser, CPP_NAME))
8734 c_parser_error (parser, "expected identifier");
8735 break;
8737 id = c_parser_peek_token (parser)->value;
8738 list = chainon (list, build_tree_list (NULL_TREE, id));
8739 c_parser_consume_token (parser);
8740 if (c_parser_next_token_is (parser, CPP_COMMA))
8741 c_parser_consume_token (parser);
8742 else
8743 break;
8745 c_parser_require (parser, CPP_GREATER, "expected %<>%>");
8746 return list;
8749 /* Parse an objc-try-catch-finally-statement.
8751 objc-try-catch-finally-statement:
8752 @try compound-statement objc-catch-list[opt]
8753 @try compound-statement objc-catch-list[opt] @finally compound-statement
8755 objc-catch-list:
8756 @catch ( objc-catch-parameter-declaration ) compound-statement
8757 objc-catch-list @catch ( objc-catch-parameter-declaration ) compound-statement
8759 objc-catch-parameter-declaration:
8760 parameter-declaration
8761 '...'
8763 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
8765 PS: This function is identical to cp_parser_objc_try_catch_finally_statement
8766 for C++. Keep them in sync. */
8768 static void
8769 c_parser_objc_try_catch_finally_statement (c_parser *parser)
8771 location_t location;
8772 tree stmt;
8774 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_TRY));
8775 c_parser_consume_token (parser);
8776 location = c_parser_peek_token (parser)->location;
8777 objc_maybe_warn_exceptions (location);
8778 stmt = c_parser_compound_statement (parser);
8779 objc_begin_try_stmt (location, stmt);
8781 while (c_parser_next_token_is_keyword (parser, RID_AT_CATCH))
8783 struct c_parm *parm;
8784 tree parameter_declaration = error_mark_node;
8785 bool seen_open_paren = false;
8787 c_parser_consume_token (parser);
8788 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
8789 seen_open_paren = true;
8790 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
8792 /* We have "@catch (...)" (where the '...' are literally
8793 what is in the code). Skip the '...'.
8794 parameter_declaration is set to NULL_TREE, and
8795 objc_being_catch_clauses() knows that that means
8796 '...'. */
8797 c_parser_consume_token (parser);
8798 parameter_declaration = NULL_TREE;
8800 else
8802 /* We have "@catch (NSException *exception)" or something
8803 like that. Parse the parameter declaration. */
8804 parm = c_parser_parameter_declaration (parser, NULL_TREE);
8805 if (parm == NULL)
8806 parameter_declaration = error_mark_node;
8807 else
8808 parameter_declaration = grokparm (parm, NULL);
8810 if (seen_open_paren)
8811 c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
8812 else
8814 /* If there was no open parenthesis, we are recovering from
8815 an error, and we are trying to figure out what mistake
8816 the user has made. */
8818 /* If there is an immediate closing parenthesis, the user
8819 probably forgot the opening one (ie, they typed "@catch
8820 NSException *e)". Parse the closing parenthesis and keep
8821 going. */
8822 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
8823 c_parser_consume_token (parser);
8825 /* If these is no immediate closing parenthesis, the user
8826 probably doesn't know that parenthesis are required at
8827 all (ie, they typed "@catch NSException *e"). So, just
8828 forget about the closing parenthesis and keep going. */
8830 objc_begin_catch_clause (parameter_declaration);
8831 if (c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
8832 c_parser_compound_statement_nostart (parser);
8833 objc_finish_catch_clause ();
8835 if (c_parser_next_token_is_keyword (parser, RID_AT_FINALLY))
8837 c_parser_consume_token (parser);
8838 location = c_parser_peek_token (parser)->location;
8839 stmt = c_parser_compound_statement (parser);
8840 objc_build_finally_clause (location, stmt);
8842 objc_finish_try_stmt ();
8845 /* Parse an objc-synchronized-statement.
8847 objc-synchronized-statement:
8848 @synchronized ( expression ) compound-statement
8851 static void
8852 c_parser_objc_synchronized_statement (c_parser *parser)
8854 location_t loc;
8855 tree expr, stmt;
8856 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_SYNCHRONIZED));
8857 c_parser_consume_token (parser);
8858 loc = c_parser_peek_token (parser)->location;
8859 objc_maybe_warn_exceptions (loc);
8860 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
8862 struct c_expr ce = c_parser_expression (parser);
8863 ce = convert_lvalue_to_rvalue (loc, ce, false, false);
8864 expr = ce.value;
8865 expr = c_fully_fold (expr, false, NULL);
8866 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
8868 else
8869 expr = error_mark_node;
8870 stmt = c_parser_compound_statement (parser);
8871 objc_build_synchronized (loc, expr, stmt);
8874 /* Parse an objc-selector; return NULL_TREE without an error if the
8875 next token is not an objc-selector.
8877 objc-selector:
8878 identifier
8879 one of
8880 enum struct union if else while do for switch case default
8881 break continue return goto asm sizeof typeof __alignof
8882 unsigned long const short volatile signed restrict _Complex
8883 in out inout bycopy byref oneway int char float double void _Bool
8884 _Atomic
8886 ??? Why this selection of keywords but not, for example, storage
8887 class specifiers? */
8889 static tree
8890 c_parser_objc_selector (c_parser *parser)
8892 c_token *token = c_parser_peek_token (parser);
8893 tree value = token->value;
8894 if (token->type == CPP_NAME)
8896 c_parser_consume_token (parser);
8897 return value;
8899 if (token->type != CPP_KEYWORD)
8900 return NULL_TREE;
8901 switch (token->keyword)
8903 case RID_ENUM:
8904 case RID_STRUCT:
8905 case RID_UNION:
8906 case RID_IF:
8907 case RID_ELSE:
8908 case RID_WHILE:
8909 case RID_DO:
8910 case RID_FOR:
8911 case RID_SWITCH:
8912 case RID_CASE:
8913 case RID_DEFAULT:
8914 case RID_BREAK:
8915 case RID_CONTINUE:
8916 case RID_RETURN:
8917 case RID_GOTO:
8918 case RID_ASM:
8919 case RID_SIZEOF:
8920 case RID_TYPEOF:
8921 case RID_ALIGNOF:
8922 case RID_UNSIGNED:
8923 case RID_LONG:
8924 case RID_INT128:
8925 case RID_CONST:
8926 case RID_SHORT:
8927 case RID_VOLATILE:
8928 case RID_SIGNED:
8929 case RID_RESTRICT:
8930 case RID_COMPLEX:
8931 case RID_IN:
8932 case RID_OUT:
8933 case RID_INOUT:
8934 case RID_BYCOPY:
8935 case RID_BYREF:
8936 case RID_ONEWAY:
8937 case RID_INT:
8938 case RID_CHAR:
8939 case RID_FLOAT:
8940 case RID_DOUBLE:
8941 case RID_VOID:
8942 case RID_BOOL:
8943 case RID_ATOMIC:
8944 case RID_AUTO_TYPE:
8945 c_parser_consume_token (parser);
8946 return value;
8947 default:
8948 return NULL_TREE;
8952 /* Parse an objc-selector-arg.
8954 objc-selector-arg:
8955 objc-selector
8956 objc-keywordname-list
8958 objc-keywordname-list:
8959 objc-keywordname
8960 objc-keywordname-list objc-keywordname
8962 objc-keywordname:
8963 objc-selector :
8967 static tree
8968 c_parser_objc_selector_arg (c_parser *parser)
8970 tree sel = c_parser_objc_selector (parser);
8971 tree list = NULL_TREE;
8972 if (sel && c_parser_next_token_is_not (parser, CPP_COLON))
8973 return sel;
8974 while (true)
8976 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
8977 return list;
8978 list = chainon (list, build_tree_list (sel, NULL_TREE));
8979 sel = c_parser_objc_selector (parser);
8980 if (!sel && c_parser_next_token_is_not (parser, CPP_COLON))
8981 break;
8983 return list;
8986 /* Parse an objc-receiver.
8988 objc-receiver:
8989 expression
8990 class-name
8991 type-name
8994 static tree
8995 c_parser_objc_receiver (c_parser *parser)
8997 location_t loc = c_parser_peek_token (parser)->location;
8999 if (c_parser_peek_token (parser)->type == CPP_NAME
9000 && (c_parser_peek_token (parser)->id_kind == C_ID_TYPENAME
9001 || c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME))
9003 tree id = c_parser_peek_token (parser)->value;
9004 c_parser_consume_token (parser);
9005 return objc_get_class_reference (id);
9007 struct c_expr ce = c_parser_expression (parser);
9008 ce = convert_lvalue_to_rvalue (loc, ce, false, false);
9009 return c_fully_fold (ce.value, false, NULL);
9012 /* Parse objc-message-args.
9014 objc-message-args:
9015 objc-selector
9016 objc-keywordarg-list
9018 objc-keywordarg-list:
9019 objc-keywordarg
9020 objc-keywordarg-list objc-keywordarg
9022 objc-keywordarg:
9023 objc-selector : objc-keywordexpr
9024 : objc-keywordexpr
9027 static tree
9028 c_parser_objc_message_args (c_parser *parser)
9030 tree sel = c_parser_objc_selector (parser);
9031 tree list = NULL_TREE;
9032 if (sel && c_parser_next_token_is_not (parser, CPP_COLON))
9033 return sel;
9034 while (true)
9036 tree keywordexpr;
9037 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
9038 return error_mark_node;
9039 keywordexpr = c_parser_objc_keywordexpr (parser);
9040 list = chainon (list, build_tree_list (sel, keywordexpr));
9041 sel = c_parser_objc_selector (parser);
9042 if (!sel && c_parser_next_token_is_not (parser, CPP_COLON))
9043 break;
9045 return list;
9048 /* Parse an objc-keywordexpr.
9050 objc-keywordexpr:
9051 nonempty-expr-list
9054 static tree
9055 c_parser_objc_keywordexpr (c_parser *parser)
9057 tree ret;
9058 vec<tree, va_gc> *expr_list = c_parser_expr_list (parser, true, true,
9059 NULL, NULL, NULL, NULL);
9060 if (vec_safe_length (expr_list) == 1)
9062 /* Just return the expression, remove a level of
9063 indirection. */
9064 ret = (*expr_list)[0];
9066 else
9068 /* We have a comma expression, we will collapse later. */
9069 ret = build_tree_list_vec (expr_list);
9071 release_tree_vector (expr_list);
9072 return ret;
9075 /* A check, needed in several places, that ObjC interface, implementation or
9076 method definitions are not prefixed by incorrect items. */
9077 static bool
9078 c_parser_objc_diagnose_bad_element_prefix (c_parser *parser,
9079 struct c_declspecs *specs)
9081 if (!specs->declspecs_seen_p || specs->non_sc_seen_p
9082 || specs->typespec_kind != ctsk_none)
9084 c_parser_error (parser,
9085 "no type or storage class may be specified here,");
9086 c_parser_skip_to_end_of_block_or_statement (parser);
9087 return true;
9089 return false;
9092 /* Parse an Objective-C @property declaration. The syntax is:
9094 objc-property-declaration:
9095 '@property' objc-property-attributes[opt] struct-declaration ;
9097 objc-property-attributes:
9098 '(' objc-property-attribute-list ')'
9100 objc-property-attribute-list:
9101 objc-property-attribute
9102 objc-property-attribute-list, objc-property-attribute
9104 objc-property-attribute
9105 'getter' = identifier
9106 'setter' = identifier
9107 'readonly'
9108 'readwrite'
9109 'assign'
9110 'retain'
9111 'copy'
9112 'nonatomic'
9114 For example:
9115 @property NSString *name;
9116 @property (readonly) id object;
9117 @property (retain, nonatomic, getter=getTheName) id name;
9118 @property int a, b, c;
9120 PS: This function is identical to cp_parser_objc_at_propery_declaration
9121 for C++. Keep them in sync. */
9122 static void
9123 c_parser_objc_at_property_declaration (c_parser *parser)
9125 /* The following variables hold the attributes of the properties as
9126 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
9127 seen. When we see an attribute, we set them to 'true' (if they
9128 are boolean properties) or to the identifier (if they have an
9129 argument, ie, for getter and setter). Note that here we only
9130 parse the list of attributes, check the syntax and accumulate the
9131 attributes that we find. objc_add_property_declaration() will
9132 then process the information. */
9133 bool property_assign = false;
9134 bool property_copy = false;
9135 tree property_getter_ident = NULL_TREE;
9136 bool property_nonatomic = false;
9137 bool property_readonly = false;
9138 bool property_readwrite = false;
9139 bool property_retain = false;
9140 tree property_setter_ident = NULL_TREE;
9142 /* 'properties' is the list of properties that we read. Usually a
9143 single one, but maybe more (eg, in "@property int a, b, c;" there
9144 are three). */
9145 tree properties;
9146 location_t loc;
9148 loc = c_parser_peek_token (parser)->location;
9149 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_PROPERTY));
9151 c_parser_consume_token (parser); /* Eat '@property'. */
9153 /* Parse the optional attribute list... */
9154 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
9156 /* Eat the '(' */
9157 c_parser_consume_token (parser);
9159 /* Property attribute keywords are valid now. */
9160 parser->objc_property_attr_context = true;
9162 while (true)
9164 bool syntax_error = false;
9165 c_token *token = c_parser_peek_token (parser);
9166 enum rid keyword;
9168 if (token->type != CPP_KEYWORD)
9170 if (token->type == CPP_CLOSE_PAREN)
9171 c_parser_error (parser, "expected identifier");
9172 else
9174 c_parser_consume_token (parser);
9175 c_parser_error (parser, "unknown property attribute");
9177 break;
9179 keyword = token->keyword;
9180 c_parser_consume_token (parser);
9181 switch (keyword)
9183 case RID_ASSIGN: property_assign = true; break;
9184 case RID_COPY: property_copy = true; break;
9185 case RID_NONATOMIC: property_nonatomic = true; break;
9186 case RID_READONLY: property_readonly = true; break;
9187 case RID_READWRITE: property_readwrite = true; break;
9188 case RID_RETAIN: property_retain = true; break;
9190 case RID_GETTER:
9191 case RID_SETTER:
9192 if (c_parser_next_token_is_not (parser, CPP_EQ))
9194 if (keyword == RID_GETTER)
9195 c_parser_error (parser,
9196 "missing %<=%> (after %<getter%> attribute)");
9197 else
9198 c_parser_error (parser,
9199 "missing %<=%> (after %<setter%> attribute)");
9200 syntax_error = true;
9201 break;
9203 c_parser_consume_token (parser); /* eat the = */
9204 if (c_parser_next_token_is_not (parser, CPP_NAME))
9206 c_parser_error (parser, "expected identifier");
9207 syntax_error = true;
9208 break;
9210 if (keyword == RID_SETTER)
9212 if (property_setter_ident != NULL_TREE)
9213 c_parser_error (parser, "the %<setter%> attribute may only be specified once");
9214 else
9215 property_setter_ident = c_parser_peek_token (parser)->value;
9216 c_parser_consume_token (parser);
9217 if (c_parser_next_token_is_not (parser, CPP_COLON))
9218 c_parser_error (parser, "setter name must terminate with %<:%>");
9219 else
9220 c_parser_consume_token (parser);
9222 else
9224 if (property_getter_ident != NULL_TREE)
9225 c_parser_error (parser, "the %<getter%> attribute may only be specified once");
9226 else
9227 property_getter_ident = c_parser_peek_token (parser)->value;
9228 c_parser_consume_token (parser);
9230 break;
9231 default:
9232 c_parser_error (parser, "unknown property attribute");
9233 syntax_error = true;
9234 break;
9237 if (syntax_error)
9238 break;
9240 if (c_parser_next_token_is (parser, CPP_COMMA))
9241 c_parser_consume_token (parser);
9242 else
9243 break;
9245 parser->objc_property_attr_context = false;
9246 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
9248 /* ... and the property declaration(s). */
9249 properties = c_parser_struct_declaration (parser);
9251 if (properties == error_mark_node)
9253 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
9254 parser->error = false;
9255 return;
9258 if (properties == NULL_TREE)
9259 c_parser_error (parser, "expected identifier");
9260 else
9262 /* Comma-separated properties are chained together in
9263 reverse order; add them one by one. */
9264 properties = nreverse (properties);
9266 for (; properties; properties = TREE_CHAIN (properties))
9267 objc_add_property_declaration (loc, copy_node (properties),
9268 property_readonly, property_readwrite,
9269 property_assign, property_retain,
9270 property_copy, property_nonatomic,
9271 property_getter_ident, property_setter_ident);
9274 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
9275 parser->error = false;
9278 /* Parse an Objective-C @synthesize declaration. The syntax is:
9280 objc-synthesize-declaration:
9281 @synthesize objc-synthesize-identifier-list ;
9283 objc-synthesize-identifier-list:
9284 objc-synthesize-identifier
9285 objc-synthesize-identifier-list, objc-synthesize-identifier
9287 objc-synthesize-identifier
9288 identifier
9289 identifier = identifier
9291 For example:
9292 @synthesize MyProperty;
9293 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
9295 PS: This function is identical to cp_parser_objc_at_synthesize_declaration
9296 for C++. Keep them in sync.
9298 static void
9299 c_parser_objc_at_synthesize_declaration (c_parser *parser)
9301 tree list = NULL_TREE;
9302 location_t loc;
9303 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_SYNTHESIZE));
9304 loc = c_parser_peek_token (parser)->location;
9306 c_parser_consume_token (parser);
9307 while (true)
9309 tree property, ivar;
9310 if (c_parser_next_token_is_not (parser, CPP_NAME))
9312 c_parser_error (parser, "expected identifier");
9313 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
9314 /* Once we find the semicolon, we can resume normal parsing.
9315 We have to reset parser->error manually because
9316 c_parser_skip_until_found() won't reset it for us if the
9317 next token is precisely a semicolon. */
9318 parser->error = false;
9319 return;
9321 property = c_parser_peek_token (parser)->value;
9322 c_parser_consume_token (parser);
9323 if (c_parser_next_token_is (parser, CPP_EQ))
9325 c_parser_consume_token (parser);
9326 if (c_parser_next_token_is_not (parser, CPP_NAME))
9328 c_parser_error (parser, "expected identifier");
9329 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
9330 parser->error = false;
9331 return;
9333 ivar = c_parser_peek_token (parser)->value;
9334 c_parser_consume_token (parser);
9336 else
9337 ivar = NULL_TREE;
9338 list = chainon (list, build_tree_list (ivar, property));
9339 if (c_parser_next_token_is (parser, CPP_COMMA))
9340 c_parser_consume_token (parser);
9341 else
9342 break;
9344 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
9345 objc_add_synthesize_declaration (loc, list);
9348 /* Parse an Objective-C @dynamic declaration. The syntax is:
9350 objc-dynamic-declaration:
9351 @dynamic identifier-list ;
9353 For example:
9354 @dynamic MyProperty;
9355 @dynamic MyProperty, AnotherProperty;
9357 PS: This function is identical to cp_parser_objc_at_dynamic_declaration
9358 for C++. Keep them in sync.
9360 static void
9361 c_parser_objc_at_dynamic_declaration (c_parser *parser)
9363 tree list = NULL_TREE;
9364 location_t loc;
9365 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_DYNAMIC));
9366 loc = c_parser_peek_token (parser)->location;
9368 c_parser_consume_token (parser);
9369 while (true)
9371 tree property;
9372 if (c_parser_next_token_is_not (parser, CPP_NAME))
9374 c_parser_error (parser, "expected identifier");
9375 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
9376 parser->error = false;
9377 return;
9379 property = c_parser_peek_token (parser)->value;
9380 list = chainon (list, build_tree_list (NULL_TREE, property));
9381 c_parser_consume_token (parser);
9382 if (c_parser_next_token_is (parser, CPP_COMMA))
9383 c_parser_consume_token (parser);
9384 else
9385 break;
9387 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
9388 objc_add_dynamic_declaration (loc, list);
9392 /* Handle pragmas. Some OpenMP pragmas are associated with, and therefore
9393 should be considered, statements. ALLOW_STMT is true if we're within
9394 the context of a function and such pragmas are to be allowed. Returns
9395 true if we actually parsed such a pragma. */
9397 static bool
9398 c_parser_pragma (c_parser *parser, enum pragma_context context)
9400 unsigned int id;
9402 id = c_parser_peek_token (parser)->pragma_kind;
9403 gcc_assert (id != PRAGMA_NONE);
9405 switch (id)
9407 case PRAGMA_OMP_BARRIER:
9408 if (context != pragma_compound)
9410 if (context == pragma_stmt)
9411 c_parser_error (parser, "%<#pragma omp barrier%> may only be "
9412 "used in compound statements");
9413 goto bad_stmt;
9415 c_parser_omp_barrier (parser);
9416 return false;
9418 case PRAGMA_OMP_FLUSH:
9419 if (context != pragma_compound)
9421 if (context == pragma_stmt)
9422 c_parser_error (parser, "%<#pragma omp flush%> may only be "
9423 "used in compound statements");
9424 goto bad_stmt;
9426 c_parser_omp_flush (parser);
9427 return false;
9429 case PRAGMA_OMP_TASKWAIT:
9430 if (context != pragma_compound)
9432 if (context == pragma_stmt)
9433 c_parser_error (parser, "%<#pragma omp taskwait%> may only be "
9434 "used in compound statements");
9435 goto bad_stmt;
9437 c_parser_omp_taskwait (parser);
9438 return false;
9440 case PRAGMA_OMP_TASKYIELD:
9441 if (context != pragma_compound)
9443 if (context == pragma_stmt)
9444 c_parser_error (parser, "%<#pragma omp taskyield%> may only be "
9445 "used in compound statements");
9446 goto bad_stmt;
9448 c_parser_omp_taskyield (parser);
9449 return false;
9451 case PRAGMA_OMP_CANCEL:
9452 if (context != pragma_compound)
9454 if (context == pragma_stmt)
9455 c_parser_error (parser, "%<#pragma omp cancel%> may only be "
9456 "used in compound statements");
9457 goto bad_stmt;
9459 c_parser_omp_cancel (parser);
9460 return false;
9462 case PRAGMA_OMP_CANCELLATION_POINT:
9463 if (context != pragma_compound)
9465 if (context == pragma_stmt)
9466 c_parser_error (parser, "%<#pragma omp cancellation point%> may "
9467 "only be used in compound statements");
9468 goto bad_stmt;
9470 c_parser_omp_cancellation_point (parser);
9471 return false;
9473 case PRAGMA_OMP_THREADPRIVATE:
9474 c_parser_omp_threadprivate (parser);
9475 return false;
9477 case PRAGMA_OMP_TARGET:
9478 return c_parser_omp_target (parser, context);
9480 case PRAGMA_OMP_END_DECLARE_TARGET:
9481 c_parser_omp_end_declare_target (parser);
9482 return false;
9484 case PRAGMA_OMP_SECTION:
9485 error_at (c_parser_peek_token (parser)->location,
9486 "%<#pragma omp section%> may only be used in "
9487 "%<#pragma omp sections%> construct");
9488 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
9489 return false;
9491 case PRAGMA_OMP_DECLARE_REDUCTION:
9492 c_parser_omp_declare (parser, context);
9493 return false;
9494 case PRAGMA_IVDEP:
9495 c_parser_consume_pragma (parser);
9496 c_parser_skip_to_pragma_eol (parser);
9497 if (!c_parser_next_token_is_keyword (parser, RID_FOR)
9498 && !c_parser_next_token_is_keyword (parser, RID_WHILE)
9499 && !c_parser_next_token_is_keyword (parser, RID_DO))
9501 c_parser_error (parser, "for, while or do statement expected");
9502 return false;
9504 if (c_parser_next_token_is_keyword (parser, RID_FOR))
9505 c_parser_for_statement (parser, true);
9506 else if (c_parser_next_token_is_keyword (parser, RID_WHILE))
9507 c_parser_while_statement (parser, true);
9508 else
9509 c_parser_do_statement (parser, true);
9510 return false;
9512 case PRAGMA_GCC_PCH_PREPROCESS:
9513 c_parser_error (parser, "%<#pragma GCC pch_preprocess%> must be first");
9514 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
9515 return false;
9517 case PRAGMA_CILK_SIMD:
9518 if (!c_parser_cilk_verify_simd (parser, context))
9519 return false;
9520 c_parser_consume_pragma (parser);
9521 c_parser_cilk_simd (parser);
9522 return false;
9524 default:
9525 if (id < PRAGMA_FIRST_EXTERNAL)
9527 if (context != pragma_stmt && context != pragma_compound)
9529 bad_stmt:
9530 c_parser_error (parser, "expected declaration specifiers");
9531 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
9532 return false;
9534 c_parser_omp_construct (parser);
9535 return true;
9537 break;
9540 c_parser_consume_pragma (parser);
9541 c_invoke_pragma_handler (id);
9543 /* Skip to EOL, but suppress any error message. Those will have been
9544 generated by the handler routine through calling error, as opposed
9545 to calling c_parser_error. */
9546 parser->error = true;
9547 c_parser_skip_to_pragma_eol (parser);
9549 return false;
9552 /* The interface the pragma parsers have to the lexer. */
9554 enum cpp_ttype
9555 pragma_lex (tree *value)
9557 c_token *tok = c_parser_peek_token (the_parser);
9558 enum cpp_ttype ret = tok->type;
9560 *value = tok->value;
9561 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
9562 ret = CPP_EOF;
9563 else
9565 if (ret == CPP_KEYWORD)
9566 ret = CPP_NAME;
9567 c_parser_consume_token (the_parser);
9570 return ret;
9573 static void
9574 c_parser_pragma_pch_preprocess (c_parser *parser)
9576 tree name = NULL;
9578 c_parser_consume_pragma (parser);
9579 if (c_parser_next_token_is (parser, CPP_STRING))
9581 name = c_parser_peek_token (parser)->value;
9582 c_parser_consume_token (parser);
9584 else
9585 c_parser_error (parser, "expected string literal");
9586 c_parser_skip_to_pragma_eol (parser);
9588 if (name)
9589 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
9592 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 parsing routines. */
9594 /* Returns name of the next clause.
9595 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
9596 the token is not consumed. Otherwise appropriate pragma_omp_clause is
9597 returned and the token is consumed. */
9599 static pragma_omp_clause
9600 c_parser_omp_clause_name (c_parser *parser)
9602 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
9604 if (c_parser_next_token_is_keyword (parser, RID_IF))
9605 result = PRAGMA_OMP_CLAUSE_IF;
9606 else if (c_parser_next_token_is_keyword (parser, RID_DEFAULT))
9607 result = PRAGMA_OMP_CLAUSE_DEFAULT;
9608 else if (c_parser_next_token_is_keyword (parser, RID_FOR))
9609 result = PRAGMA_OMP_CLAUSE_FOR;
9610 else if (c_parser_next_token_is (parser, CPP_NAME))
9612 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
9614 switch (p[0])
9616 case 'a':
9617 if (!strcmp ("aligned", p))
9618 result = PRAGMA_OMP_CLAUSE_ALIGNED;
9619 break;
9620 case 'c':
9621 if (!strcmp ("collapse", p))
9622 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
9623 else if (!strcmp ("copyin", p))
9624 result = PRAGMA_OMP_CLAUSE_COPYIN;
9625 else if (!strcmp ("copyprivate", p))
9626 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
9627 break;
9628 case 'd':
9629 if (!strcmp ("depend", p))
9630 result = PRAGMA_OMP_CLAUSE_DEPEND;
9631 else if (!strcmp ("device", p))
9632 result = PRAGMA_OMP_CLAUSE_DEVICE;
9633 else if (!strcmp ("dist_schedule", p))
9634 result = PRAGMA_OMP_CLAUSE_DIST_SCHEDULE;
9635 break;
9636 case 'f':
9637 if (!strcmp ("final", p))
9638 result = PRAGMA_OMP_CLAUSE_FINAL;
9639 else if (!strcmp ("firstprivate", p))
9640 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
9641 else if (!strcmp ("from", p))
9642 result = PRAGMA_OMP_CLAUSE_FROM;
9643 break;
9644 case 'i':
9645 if (!strcmp ("inbranch", p))
9646 result = PRAGMA_OMP_CLAUSE_INBRANCH;
9647 break;
9648 case 'l':
9649 if (!strcmp ("lastprivate", p))
9650 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
9651 else if (!strcmp ("linear", p))
9652 result = PRAGMA_OMP_CLAUSE_LINEAR;
9653 break;
9654 case 'm':
9655 if (!strcmp ("map", p))
9656 result = PRAGMA_OMP_CLAUSE_MAP;
9657 else if (!strcmp ("mergeable", p))
9658 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
9659 else if (flag_cilkplus && !strcmp ("mask", p))
9660 result = PRAGMA_CILK_CLAUSE_MASK;
9661 break;
9662 case 'n':
9663 if (!strcmp ("notinbranch", p))
9664 result = PRAGMA_OMP_CLAUSE_NOTINBRANCH;
9665 else if (!strcmp ("nowait", p))
9666 result = PRAGMA_OMP_CLAUSE_NOWAIT;
9667 else if (!strcmp ("num_teams", p))
9668 result = PRAGMA_OMP_CLAUSE_NUM_TEAMS;
9669 else if (!strcmp ("num_threads", p))
9670 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
9671 else if (flag_cilkplus && !strcmp ("nomask", p))
9672 result = PRAGMA_CILK_CLAUSE_NOMASK;
9673 break;
9674 case 'o':
9675 if (!strcmp ("ordered", p))
9676 result = PRAGMA_OMP_CLAUSE_ORDERED;
9677 break;
9678 case 'p':
9679 if (!strcmp ("parallel", p))
9680 result = PRAGMA_OMP_CLAUSE_PARALLEL;
9681 else if (!strcmp ("private", p))
9682 result = PRAGMA_OMP_CLAUSE_PRIVATE;
9683 else if (!strcmp ("proc_bind", p))
9684 result = PRAGMA_OMP_CLAUSE_PROC_BIND;
9685 break;
9686 case 'r':
9687 if (!strcmp ("reduction", p))
9688 result = PRAGMA_OMP_CLAUSE_REDUCTION;
9689 break;
9690 case 's':
9691 if (!strcmp ("safelen", p))
9692 result = PRAGMA_OMP_CLAUSE_SAFELEN;
9693 else if (!strcmp ("schedule", p))
9694 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
9695 else if (!strcmp ("sections", p))
9696 result = PRAGMA_OMP_CLAUSE_SECTIONS;
9697 else if (!strcmp ("shared", p))
9698 result = PRAGMA_OMP_CLAUSE_SHARED;
9699 else if (!strcmp ("simdlen", p))
9700 result = PRAGMA_OMP_CLAUSE_SIMDLEN;
9701 break;
9702 case 't':
9703 if (!strcmp ("taskgroup", p))
9704 result = PRAGMA_OMP_CLAUSE_TASKGROUP;
9705 else if (!strcmp ("thread_limit", p))
9706 result = PRAGMA_OMP_CLAUSE_THREAD_LIMIT;
9707 else if (!strcmp ("to", p))
9708 result = PRAGMA_OMP_CLAUSE_TO;
9709 break;
9710 case 'u':
9711 if (!strcmp ("uniform", p))
9712 result = PRAGMA_OMP_CLAUSE_UNIFORM;
9713 else if (!strcmp ("untied", p))
9714 result = PRAGMA_OMP_CLAUSE_UNTIED;
9715 break;
9716 case 'v':
9717 if (flag_cilkplus && !strcmp ("vectorlength", p))
9718 result = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
9719 break;
9723 if (result != PRAGMA_OMP_CLAUSE_NONE)
9724 c_parser_consume_token (parser);
9726 return result;
9729 /* Validate that a clause of the given type does not already exist. */
9731 static void
9732 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
9733 const char *name)
9735 tree c;
9737 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
9738 if (OMP_CLAUSE_CODE (c) == code)
9740 location_t loc = OMP_CLAUSE_LOCATION (c);
9741 error_at (loc, "too many %qs clauses", name);
9742 break;
9746 /* OpenMP 2.5:
9747 variable-list:
9748 identifier
9749 variable-list , identifier
9751 If KIND is nonzero, create the appropriate node and install the
9752 decl in OMP_CLAUSE_DECL and add the node to the head of the list.
9753 If KIND is nonzero, CLAUSE_LOC is the location of the clause.
9755 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
9756 return the list created. */
9758 static tree
9759 c_parser_omp_variable_list (c_parser *parser,
9760 location_t clause_loc,
9761 enum omp_clause_code kind, tree list)
9763 if (c_parser_next_token_is_not (parser, CPP_NAME)
9764 || c_parser_peek_token (parser)->id_kind != C_ID_ID)
9765 c_parser_error (parser, "expected identifier");
9767 while (c_parser_next_token_is (parser, CPP_NAME)
9768 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
9770 tree t = lookup_name (c_parser_peek_token (parser)->value);
9772 if (t == NULL_TREE)
9774 undeclared_variable (c_parser_peek_token (parser)->location,
9775 c_parser_peek_token (parser)->value);
9776 t = error_mark_node;
9779 c_parser_consume_token (parser);
9781 if (t == error_mark_node)
9783 else if (kind != 0)
9785 switch (kind)
9787 case OMP_CLAUSE_MAP:
9788 case OMP_CLAUSE_FROM:
9789 case OMP_CLAUSE_TO:
9790 case OMP_CLAUSE_DEPEND:
9791 while (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
9793 tree low_bound = NULL_TREE, length = NULL_TREE;
9795 c_parser_consume_token (parser);
9796 if (!c_parser_next_token_is (parser, CPP_COLON))
9797 low_bound = c_parser_expression (parser).value;
9798 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
9799 length = integer_one_node;
9800 else
9802 /* Look for `:'. */
9803 if (!c_parser_require (parser, CPP_COLON,
9804 "expected %<:%>"))
9806 t = error_mark_node;
9807 break;
9809 if (!c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
9810 length = c_parser_expression (parser).value;
9812 /* Look for the closing `]'. */
9813 if (!c_parser_require (parser, CPP_CLOSE_SQUARE,
9814 "expected %<]%>"))
9816 t = error_mark_node;
9817 break;
9819 t = tree_cons (low_bound, length, t);
9821 break;
9822 default:
9823 break;
9826 if (t != error_mark_node)
9828 tree u = build_omp_clause (clause_loc, kind);
9829 OMP_CLAUSE_DECL (u) = t;
9830 OMP_CLAUSE_CHAIN (u) = list;
9831 list = u;
9834 else
9835 list = tree_cons (t, NULL_TREE, list);
9837 if (c_parser_next_token_is_not (parser, CPP_COMMA))
9838 break;
9840 c_parser_consume_token (parser);
9843 return list;
9846 /* Similarly, but expect leading and trailing parenthesis. This is a very
9847 common case for omp clauses. */
9849 static tree
9850 c_parser_omp_var_list_parens (c_parser *parser, enum omp_clause_code kind,
9851 tree list)
9853 /* The clauses location. */
9854 location_t loc = c_parser_peek_token (parser)->location;
9856 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
9858 list = c_parser_omp_variable_list (parser, loc, kind, list);
9859 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
9861 return list;
9864 /* OpenMP 3.0:
9865 collapse ( constant-expression ) */
9867 static tree
9868 c_parser_omp_clause_collapse (c_parser *parser, tree list)
9870 tree c, num = error_mark_node;
9871 HOST_WIDE_INT n;
9872 location_t loc;
9874 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse");
9876 loc = c_parser_peek_token (parser)->location;
9877 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
9879 num = c_parser_expr_no_commas (parser, NULL).value;
9880 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
9882 if (num == error_mark_node)
9883 return list;
9884 mark_exp_read (num);
9885 num = c_fully_fold (num, false, NULL);
9886 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
9887 || !tree_fits_shwi_p (num)
9888 || (n = tree_to_shwi (num)) <= 0
9889 || (int) n != n)
9891 error_at (loc,
9892 "collapse argument needs positive constant integer expression");
9893 return list;
9895 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
9896 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
9897 OMP_CLAUSE_CHAIN (c) = list;
9898 return c;
9901 /* OpenMP 2.5:
9902 copyin ( variable-list ) */
9904 static tree
9905 c_parser_omp_clause_copyin (c_parser *parser, tree list)
9907 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_COPYIN, list);
9910 /* OpenMP 2.5:
9911 copyprivate ( variable-list ) */
9913 static tree
9914 c_parser_omp_clause_copyprivate (c_parser *parser, tree list)
9916 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_COPYPRIVATE, list);
9919 /* OpenMP 2.5:
9920 default ( shared | none ) */
9922 static tree
9923 c_parser_omp_clause_default (c_parser *parser, tree list)
9925 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
9926 location_t loc = c_parser_peek_token (parser)->location;
9927 tree c;
9929 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
9930 return list;
9931 if (c_parser_next_token_is (parser, CPP_NAME))
9933 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
9935 switch (p[0])
9937 case 'n':
9938 if (strcmp ("none", p) != 0)
9939 goto invalid_kind;
9940 kind = OMP_CLAUSE_DEFAULT_NONE;
9941 break;
9943 case 's':
9944 if (strcmp ("shared", p) != 0)
9945 goto invalid_kind;
9946 kind = OMP_CLAUSE_DEFAULT_SHARED;
9947 break;
9949 default:
9950 goto invalid_kind;
9953 c_parser_consume_token (parser);
9955 else
9957 invalid_kind:
9958 c_parser_error (parser, "expected %<none%> or %<shared%>");
9960 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
9962 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
9963 return list;
9965 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default");
9966 c = build_omp_clause (loc, OMP_CLAUSE_DEFAULT);
9967 OMP_CLAUSE_CHAIN (c) = list;
9968 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
9970 return c;
9973 /* OpenMP 2.5:
9974 firstprivate ( variable-list ) */
9976 static tree
9977 c_parser_omp_clause_firstprivate (c_parser *parser, tree list)
9979 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_FIRSTPRIVATE, list);
9982 /* OpenMP 3.1:
9983 final ( expression ) */
9985 static tree
9986 c_parser_omp_clause_final (c_parser *parser, tree list)
9988 location_t loc = c_parser_peek_token (parser)->location;
9989 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
9991 tree t = c_parser_paren_condition (parser);
9992 tree c;
9994 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final");
9996 c = build_omp_clause (loc, OMP_CLAUSE_FINAL);
9997 OMP_CLAUSE_FINAL_EXPR (c) = t;
9998 OMP_CLAUSE_CHAIN (c) = list;
9999 list = c;
10001 else
10002 c_parser_error (parser, "expected %<(%>");
10004 return list;
10007 /* OpenMP 2.5:
10008 if ( expression ) */
10010 static tree
10011 c_parser_omp_clause_if (c_parser *parser, tree list)
10013 location_t loc = c_parser_peek_token (parser)->location;
10014 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
10016 tree t = c_parser_paren_condition (parser);
10017 tree c;
10019 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if");
10021 c = build_omp_clause (loc, OMP_CLAUSE_IF);
10022 OMP_CLAUSE_IF_EXPR (c) = t;
10023 OMP_CLAUSE_CHAIN (c) = list;
10024 list = c;
10026 else
10027 c_parser_error (parser, "expected %<(%>");
10029 return list;
10032 /* OpenMP 2.5:
10033 lastprivate ( variable-list ) */
10035 static tree
10036 c_parser_omp_clause_lastprivate (c_parser *parser, tree list)
10038 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_LASTPRIVATE, list);
10041 /* OpenMP 3.1:
10042 mergeable */
10044 static tree
10045 c_parser_omp_clause_mergeable (c_parser *parser ATTRIBUTE_UNUSED, tree list)
10047 tree c;
10049 /* FIXME: Should we allow duplicates? */
10050 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable");
10052 c = build_omp_clause (c_parser_peek_token (parser)->location,
10053 OMP_CLAUSE_MERGEABLE);
10054 OMP_CLAUSE_CHAIN (c) = list;
10056 return c;
10059 /* OpenMP 2.5:
10060 nowait */
10062 static tree
10063 c_parser_omp_clause_nowait (c_parser *parser ATTRIBUTE_UNUSED, tree list)
10065 tree c;
10066 location_t loc = c_parser_peek_token (parser)->location;
10068 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait");
10070 c = build_omp_clause (loc, OMP_CLAUSE_NOWAIT);
10071 OMP_CLAUSE_CHAIN (c) = list;
10072 return c;
10075 /* OpenMP 2.5:
10076 num_threads ( expression ) */
10078 static tree
10079 c_parser_omp_clause_num_threads (c_parser *parser, tree list)
10081 location_t num_threads_loc = c_parser_peek_token (parser)->location;
10082 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10084 location_t expr_loc = c_parser_peek_token (parser)->location;
10085 tree c, t = c_parser_expression (parser).value;
10086 mark_exp_read (t);
10087 t = c_fully_fold (t, false, NULL);
10089 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10091 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
10093 c_parser_error (parser, "expected integer expression");
10094 return list;
10097 /* Attempt to statically determine when the number isn't positive. */
10098 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
10099 build_int_cst (TREE_TYPE (t), 0));
10100 if (CAN_HAVE_LOCATION_P (c))
10101 SET_EXPR_LOCATION (c, expr_loc);
10102 if (c == boolean_true_node)
10104 warning_at (expr_loc, 0,
10105 "%<num_threads%> value must be positive");
10106 t = integer_one_node;
10109 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS, "num_threads");
10111 c = build_omp_clause (num_threads_loc, OMP_CLAUSE_NUM_THREADS);
10112 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
10113 OMP_CLAUSE_CHAIN (c) = list;
10114 list = c;
10117 return list;
10120 /* OpenMP 2.5:
10121 ordered */
10123 static tree
10124 c_parser_omp_clause_ordered (c_parser *parser, tree list)
10126 tree c;
10128 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED, "ordered");
10130 c = build_omp_clause (c_parser_peek_token (parser)->location,
10131 OMP_CLAUSE_ORDERED);
10132 OMP_CLAUSE_CHAIN (c) = list;
10134 return c;
10137 /* OpenMP 2.5:
10138 private ( variable-list ) */
10140 static tree
10141 c_parser_omp_clause_private (c_parser *parser, tree list)
10143 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_PRIVATE, list);
10146 /* OpenMP 2.5:
10147 reduction ( reduction-operator : variable-list )
10149 reduction-operator:
10150 One of: + * - & ^ | && ||
10152 OpenMP 3.1:
10154 reduction-operator:
10155 One of: + * - & ^ | && || max min
10157 OpenMP 4.0:
10159 reduction-operator:
10160 One of: + * - & ^ | && ||
10161 identifier */
10163 static tree
10164 c_parser_omp_clause_reduction (c_parser *parser, tree list)
10166 location_t clause_loc = c_parser_peek_token (parser)->location;
10167 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10169 enum tree_code code = ERROR_MARK;
10170 tree reduc_id = NULL_TREE;
10172 switch (c_parser_peek_token (parser)->type)
10174 case CPP_PLUS:
10175 code = PLUS_EXPR;
10176 break;
10177 case CPP_MULT:
10178 code = MULT_EXPR;
10179 break;
10180 case CPP_MINUS:
10181 code = MINUS_EXPR;
10182 break;
10183 case CPP_AND:
10184 code = BIT_AND_EXPR;
10185 break;
10186 case CPP_XOR:
10187 code = BIT_XOR_EXPR;
10188 break;
10189 case CPP_OR:
10190 code = BIT_IOR_EXPR;
10191 break;
10192 case CPP_AND_AND:
10193 code = TRUTH_ANDIF_EXPR;
10194 break;
10195 case CPP_OR_OR:
10196 code = TRUTH_ORIF_EXPR;
10197 break;
10198 case CPP_NAME:
10200 const char *p
10201 = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
10202 if (strcmp (p, "min") == 0)
10204 code = MIN_EXPR;
10205 break;
10207 if (strcmp (p, "max") == 0)
10209 code = MAX_EXPR;
10210 break;
10212 reduc_id = c_parser_peek_token (parser)->value;
10213 break;
10215 default:
10216 c_parser_error (parser,
10217 "expected %<+%>, %<*%>, %<-%>, %<&%>, "
10218 "%<^%>, %<|%>, %<&&%>, %<||%>, %<min%> or %<max%>");
10219 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 0);
10220 return list;
10222 c_parser_consume_token (parser);
10223 reduc_id = c_omp_reduction_id (code, reduc_id);
10224 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
10226 tree nl, c;
10228 nl = c_parser_omp_variable_list (parser, clause_loc,
10229 OMP_CLAUSE_REDUCTION, list);
10230 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
10232 tree type = TREE_TYPE (OMP_CLAUSE_DECL (c));
10233 OMP_CLAUSE_REDUCTION_CODE (c) = code;
10234 if (code == ERROR_MARK
10235 || !(INTEGRAL_TYPE_P (type)
10236 || TREE_CODE (type) == REAL_TYPE
10237 || TREE_CODE (type) == COMPLEX_TYPE))
10238 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c)
10239 = c_omp_reduction_lookup (reduc_id,
10240 TYPE_MAIN_VARIANT (type));
10243 list = nl;
10245 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10247 return list;
10250 /* OpenMP 2.5:
10251 schedule ( schedule-kind )
10252 schedule ( schedule-kind , expression )
10254 schedule-kind:
10255 static | dynamic | guided | runtime | auto
10258 static tree
10259 c_parser_omp_clause_schedule (c_parser *parser, tree list)
10261 tree c, t;
10262 location_t loc = c_parser_peek_token (parser)->location;
10264 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10265 return list;
10267 c = build_omp_clause (loc, OMP_CLAUSE_SCHEDULE);
10269 if (c_parser_next_token_is (parser, CPP_NAME))
10271 tree kind = c_parser_peek_token (parser)->value;
10272 const char *p = IDENTIFIER_POINTER (kind);
10274 switch (p[0])
10276 case 'd':
10277 if (strcmp ("dynamic", p) != 0)
10278 goto invalid_kind;
10279 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
10280 break;
10282 case 'g':
10283 if (strcmp ("guided", p) != 0)
10284 goto invalid_kind;
10285 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
10286 break;
10288 case 'r':
10289 if (strcmp ("runtime", p) != 0)
10290 goto invalid_kind;
10291 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
10292 break;
10294 default:
10295 goto invalid_kind;
10298 else if (c_parser_next_token_is_keyword (parser, RID_STATIC))
10299 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
10300 else if (c_parser_next_token_is_keyword (parser, RID_AUTO))
10301 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
10302 else
10303 goto invalid_kind;
10305 c_parser_consume_token (parser);
10306 if (c_parser_next_token_is (parser, CPP_COMMA))
10308 location_t here;
10309 c_parser_consume_token (parser);
10311 here = c_parser_peek_token (parser)->location;
10312 t = c_parser_expr_no_commas (parser, NULL).value;
10313 mark_exp_read (t);
10314 t = c_fully_fold (t, false, NULL);
10316 if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
10317 error_at (here, "schedule %<runtime%> does not take "
10318 "a %<chunk_size%> parameter");
10319 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
10320 error_at (here,
10321 "schedule %<auto%> does not take "
10322 "a %<chunk_size%> parameter");
10323 else if (TREE_CODE (TREE_TYPE (t)) == INTEGER_TYPE)
10324 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
10325 else
10326 c_parser_error (parser, "expected integer expression");
10328 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10330 else
10331 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
10332 "expected %<,%> or %<)%>");
10334 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule");
10335 OMP_CLAUSE_CHAIN (c) = list;
10336 return c;
10338 invalid_kind:
10339 c_parser_error (parser, "invalid schedule kind");
10340 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 0);
10341 return list;
10344 /* OpenMP 2.5:
10345 shared ( variable-list ) */
10347 static tree
10348 c_parser_omp_clause_shared (c_parser *parser, tree list)
10350 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_SHARED, list);
10353 /* OpenMP 3.0:
10354 untied */
10356 static tree
10357 c_parser_omp_clause_untied (c_parser *parser ATTRIBUTE_UNUSED, tree list)
10359 tree c;
10361 /* FIXME: Should we allow duplicates? */
10362 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied");
10364 c = build_omp_clause (c_parser_peek_token (parser)->location,
10365 OMP_CLAUSE_UNTIED);
10366 OMP_CLAUSE_CHAIN (c) = list;
10368 return c;
10371 /* OpenMP 4.0:
10372 inbranch
10373 notinbranch */
10375 static tree
10376 c_parser_omp_clause_branch (c_parser *parser ATTRIBUTE_UNUSED,
10377 enum omp_clause_code code, tree list)
10379 check_no_duplicate_clause (list, code, omp_clause_code_name[code]);
10381 tree c = build_omp_clause (c_parser_peek_token (parser)->location, code);
10382 OMP_CLAUSE_CHAIN (c) = list;
10384 return c;
10387 /* OpenMP 4.0:
10388 parallel
10390 sections
10391 taskgroup */
10393 static tree
10394 c_parser_omp_clause_cancelkind (c_parser *parser ATTRIBUTE_UNUSED,
10395 enum omp_clause_code code, tree list)
10397 tree c = build_omp_clause (c_parser_peek_token (parser)->location, code);
10398 OMP_CLAUSE_CHAIN (c) = list;
10400 return c;
10403 /* OpenMP 4.0:
10404 num_teams ( expression ) */
10406 static tree
10407 c_parser_omp_clause_num_teams (c_parser *parser, tree list)
10409 location_t num_teams_loc = c_parser_peek_token (parser)->location;
10410 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10412 location_t expr_loc = c_parser_peek_token (parser)->location;
10413 tree c, t = c_parser_expression (parser).value;
10414 mark_exp_read (t);
10415 t = c_fully_fold (t, false, NULL);
10417 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10419 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
10421 c_parser_error (parser, "expected integer expression");
10422 return list;
10425 /* Attempt to statically determine when the number isn't positive. */
10426 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
10427 build_int_cst (TREE_TYPE (t), 0));
10428 if (CAN_HAVE_LOCATION_P (c))
10429 SET_EXPR_LOCATION (c, expr_loc);
10430 if (c == boolean_true_node)
10432 warning_at (expr_loc, 0, "%<num_teams%> value must be positive");
10433 t = integer_one_node;
10436 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TEAMS, "num_teams");
10438 c = build_omp_clause (num_teams_loc, OMP_CLAUSE_NUM_TEAMS);
10439 OMP_CLAUSE_NUM_TEAMS_EXPR (c) = t;
10440 OMP_CLAUSE_CHAIN (c) = list;
10441 list = c;
10444 return list;
10447 /* OpenMP 4.0:
10448 thread_limit ( expression ) */
10450 static tree
10451 c_parser_omp_clause_thread_limit (c_parser *parser, tree list)
10453 location_t num_thread_limit_loc = c_parser_peek_token (parser)->location;
10454 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10456 location_t expr_loc = c_parser_peek_token (parser)->location;
10457 tree c, t = c_parser_expression (parser).value;
10458 mark_exp_read (t);
10459 t = c_fully_fold (t, false, NULL);
10461 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10463 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
10465 c_parser_error (parser, "expected integer expression");
10466 return list;
10469 /* Attempt to statically determine when the number isn't positive. */
10470 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
10471 build_int_cst (TREE_TYPE (t), 0));
10472 if (CAN_HAVE_LOCATION_P (c))
10473 SET_EXPR_LOCATION (c, expr_loc);
10474 if (c == boolean_true_node)
10476 warning_at (expr_loc, 0, "%<thread_limit%> value must be positive");
10477 t = integer_one_node;
10480 check_no_duplicate_clause (list, OMP_CLAUSE_THREAD_LIMIT,
10481 "thread_limit");
10483 c = build_omp_clause (num_thread_limit_loc, OMP_CLAUSE_THREAD_LIMIT);
10484 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
10485 OMP_CLAUSE_CHAIN (c) = list;
10486 list = c;
10489 return list;
10492 /* OpenMP 4.0:
10493 aligned ( variable-list )
10494 aligned ( variable-list : constant-expression ) */
10496 static tree
10497 c_parser_omp_clause_aligned (c_parser *parser, tree list)
10499 location_t clause_loc = c_parser_peek_token (parser)->location;
10500 tree nl, c;
10502 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10503 return list;
10505 nl = c_parser_omp_variable_list (parser, clause_loc,
10506 OMP_CLAUSE_ALIGNED, list);
10508 if (c_parser_next_token_is (parser, CPP_COLON))
10510 c_parser_consume_token (parser);
10511 tree alignment = c_parser_expr_no_commas (parser, NULL).value;
10512 mark_exp_read (alignment);
10513 alignment = c_fully_fold (alignment, false, NULL);
10514 if (!INTEGRAL_TYPE_P (TREE_TYPE (alignment))
10515 && TREE_CODE (alignment) != INTEGER_CST
10516 && tree_int_cst_sgn (alignment) != 1)
10518 error_at (clause_loc, "%<aligned%> clause alignment expression must "
10519 "be positive constant integer expression");
10520 alignment = NULL_TREE;
10523 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
10524 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = alignment;
10527 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10528 return nl;
10531 /* OpenMP 4.0:
10532 linear ( variable-list )
10533 linear ( variable-list : expression ) */
10535 static tree
10536 c_parser_omp_clause_linear (c_parser *parser, tree list, bool is_cilk_simd_fn)
10538 location_t clause_loc = c_parser_peek_token (parser)->location;
10539 tree nl, c, step;
10541 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10542 return list;
10544 nl = c_parser_omp_variable_list (parser, clause_loc,
10545 OMP_CLAUSE_LINEAR, list);
10547 if (c_parser_next_token_is (parser, CPP_COLON))
10549 c_parser_consume_token (parser);
10550 step = c_parser_expression (parser).value;
10551 mark_exp_read (step);
10552 step = c_fully_fold (step, false, NULL);
10553 if (is_cilk_simd_fn && TREE_CODE (step) == PARM_DECL)
10555 sorry ("using parameters for %<linear%> step is not supported yet");
10556 step = integer_one_node;
10558 if (!INTEGRAL_TYPE_P (TREE_TYPE (step)))
10560 error_at (clause_loc, "%<linear%> clause step expression must "
10561 "be integral");
10562 step = integer_one_node;
10566 else
10567 step = integer_one_node;
10569 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
10571 OMP_CLAUSE_LINEAR_STEP (c) = step;
10574 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10575 return nl;
10578 /* OpenMP 4.0:
10579 safelen ( constant-expression ) */
10581 static tree
10582 c_parser_omp_clause_safelen (c_parser *parser, tree list)
10584 location_t clause_loc = c_parser_peek_token (parser)->location;
10585 tree c, t;
10587 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10588 return list;
10590 t = c_parser_expr_no_commas (parser, NULL).value;
10591 mark_exp_read (t);
10592 t = c_fully_fold (t, false, NULL);
10593 if (!INTEGRAL_TYPE_P (TREE_TYPE (t))
10594 && TREE_CODE (t) != INTEGER_CST
10595 && tree_int_cst_sgn (t) != 1)
10597 error_at (clause_loc, "%<safelen%> clause expression must "
10598 "be positive constant integer expression");
10599 t = NULL_TREE;
10602 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10603 if (t == NULL_TREE || t == error_mark_node)
10604 return list;
10606 check_no_duplicate_clause (list, OMP_CLAUSE_SAFELEN, "safelen");
10608 c = build_omp_clause (clause_loc, OMP_CLAUSE_SAFELEN);
10609 OMP_CLAUSE_SAFELEN_EXPR (c) = t;
10610 OMP_CLAUSE_CHAIN (c) = list;
10611 return c;
10614 /* OpenMP 4.0:
10615 simdlen ( constant-expression ) */
10617 static tree
10618 c_parser_omp_clause_simdlen (c_parser *parser, tree list)
10620 location_t clause_loc = c_parser_peek_token (parser)->location;
10621 tree c, t;
10623 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10624 return list;
10626 t = c_parser_expr_no_commas (parser, NULL).value;
10627 mark_exp_read (t);
10628 t = c_fully_fold (t, false, NULL);
10629 if (!INTEGRAL_TYPE_P (TREE_TYPE (t))
10630 && TREE_CODE (t) != INTEGER_CST
10631 && tree_int_cst_sgn (t) != 1)
10633 error_at (clause_loc, "%<simdlen%> clause expression must "
10634 "be positive constant integer expression");
10635 t = NULL_TREE;
10638 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10639 if (t == NULL_TREE || t == error_mark_node)
10640 return list;
10642 check_no_duplicate_clause (list, OMP_CLAUSE_SIMDLEN, "simdlen");
10644 c = build_omp_clause (clause_loc, OMP_CLAUSE_SIMDLEN);
10645 OMP_CLAUSE_SIMDLEN_EXPR (c) = t;
10646 OMP_CLAUSE_CHAIN (c) = list;
10647 return c;
10650 /* OpenMP 4.0:
10651 depend ( depend-kind: variable-list )
10653 depend-kind:
10654 in | out | inout */
10656 static tree
10657 c_parser_omp_clause_depend (c_parser *parser, tree list)
10659 location_t clause_loc = c_parser_peek_token (parser)->location;
10660 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_INOUT;
10661 tree nl, c;
10663 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10664 return list;
10666 if (c_parser_next_token_is (parser, CPP_NAME))
10668 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
10669 if (strcmp ("in", p) == 0)
10670 kind = OMP_CLAUSE_DEPEND_IN;
10671 else if (strcmp ("inout", p) == 0)
10672 kind = OMP_CLAUSE_DEPEND_INOUT;
10673 else if (strcmp ("out", p) == 0)
10674 kind = OMP_CLAUSE_DEPEND_OUT;
10675 else
10676 goto invalid_kind;
10678 else
10679 goto invalid_kind;
10681 c_parser_consume_token (parser);
10682 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
10683 goto resync_fail;
10685 nl = c_parser_omp_variable_list (parser, clause_loc,
10686 OMP_CLAUSE_DEPEND, list);
10688 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
10689 OMP_CLAUSE_DEPEND_KIND (c) = kind;
10691 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10692 return nl;
10694 invalid_kind:
10695 c_parser_error (parser, "invalid depend kind");
10696 resync_fail:
10697 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10698 return list;
10701 /* OpenMP 4.0:
10702 map ( map-kind: variable-list )
10703 map ( variable-list )
10705 map-kind:
10706 alloc | to | from | tofrom */
10708 static tree
10709 c_parser_omp_clause_map (c_parser *parser, tree list)
10711 location_t clause_loc = c_parser_peek_token (parser)->location;
10712 enum omp_clause_map_kind kind = OMP_CLAUSE_MAP_TOFROM;
10713 tree nl, c;
10715 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10716 return list;
10718 if (c_parser_next_token_is (parser, CPP_NAME)
10719 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
10721 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
10722 if (strcmp ("alloc", p) == 0)
10723 kind = OMP_CLAUSE_MAP_ALLOC;
10724 else if (strcmp ("to", p) == 0)
10725 kind = OMP_CLAUSE_MAP_TO;
10726 else if (strcmp ("from", p) == 0)
10727 kind = OMP_CLAUSE_MAP_FROM;
10728 else if (strcmp ("tofrom", p) == 0)
10729 kind = OMP_CLAUSE_MAP_TOFROM;
10730 else
10732 c_parser_error (parser, "invalid map kind");
10733 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
10734 "expected %<)%>");
10735 return list;
10737 c_parser_consume_token (parser);
10738 c_parser_consume_token (parser);
10741 nl = c_parser_omp_variable_list (parser, clause_loc, OMP_CLAUSE_MAP, list);
10743 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
10744 OMP_CLAUSE_MAP_KIND (c) = kind;
10746 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10747 return nl;
10750 /* OpenMP 4.0:
10751 device ( expression ) */
10753 static tree
10754 c_parser_omp_clause_device (c_parser *parser, tree list)
10756 location_t clause_loc = c_parser_peek_token (parser)->location;
10757 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10759 tree c, t = c_parser_expr_no_commas (parser, NULL).value;
10760 mark_exp_read (t);
10761 t = c_fully_fold (t, false, NULL);
10763 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10765 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
10767 c_parser_error (parser, "expected integer expression");
10768 return list;
10771 check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE, "device");
10773 c = build_omp_clause (clause_loc, OMP_CLAUSE_DEVICE);
10774 OMP_CLAUSE_DEVICE_ID (c) = t;
10775 OMP_CLAUSE_CHAIN (c) = list;
10776 list = c;
10779 return list;
10782 /* OpenMP 4.0:
10783 dist_schedule ( static )
10784 dist_schedule ( static , expression ) */
10786 static tree
10787 c_parser_omp_clause_dist_schedule (c_parser *parser, tree list)
10789 tree c, t = NULL_TREE;
10790 location_t loc = c_parser_peek_token (parser)->location;
10792 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10793 return list;
10795 if (!c_parser_next_token_is_keyword (parser, RID_STATIC))
10797 c_parser_error (parser, "invalid dist_schedule kind");
10798 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
10799 "expected %<)%>");
10800 return list;
10803 c_parser_consume_token (parser);
10804 if (c_parser_next_token_is (parser, CPP_COMMA))
10806 c_parser_consume_token (parser);
10808 t = c_parser_expr_no_commas (parser, NULL).value;
10809 mark_exp_read (t);
10810 t = c_fully_fold (t, false, NULL);
10811 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10813 else
10814 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
10815 "expected %<,%> or %<)%>");
10817 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule");
10818 if (t == error_mark_node)
10819 return list;
10821 c = build_omp_clause (loc, OMP_CLAUSE_DIST_SCHEDULE);
10822 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
10823 OMP_CLAUSE_CHAIN (c) = list;
10824 return c;
10827 /* OpenMP 4.0:
10828 proc_bind ( proc-bind-kind )
10830 proc-bind-kind:
10831 master | close | spread */
10833 static tree
10834 c_parser_omp_clause_proc_bind (c_parser *parser, tree list)
10836 location_t clause_loc = c_parser_peek_token (parser)->location;
10837 enum omp_clause_proc_bind_kind kind;
10838 tree c;
10840 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10841 return list;
10843 if (c_parser_next_token_is (parser, CPP_NAME))
10845 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
10846 if (strcmp ("master", p) == 0)
10847 kind = OMP_CLAUSE_PROC_BIND_MASTER;
10848 else if (strcmp ("close", p) == 0)
10849 kind = OMP_CLAUSE_PROC_BIND_CLOSE;
10850 else if (strcmp ("spread", p) == 0)
10851 kind = OMP_CLAUSE_PROC_BIND_SPREAD;
10852 else
10853 goto invalid_kind;
10855 else
10856 goto invalid_kind;
10858 c_parser_consume_token (parser);
10859 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10860 c = build_omp_clause (clause_loc, OMP_CLAUSE_PROC_BIND);
10861 OMP_CLAUSE_PROC_BIND_KIND (c) = kind;
10862 OMP_CLAUSE_CHAIN (c) = list;
10863 return c;
10865 invalid_kind:
10866 c_parser_error (parser, "invalid proc_bind kind");
10867 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10868 return list;
10871 /* OpenMP 4.0:
10872 to ( variable-list ) */
10874 static tree
10875 c_parser_omp_clause_to (c_parser *parser, tree list)
10877 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_TO, list);
10880 /* OpenMP 4.0:
10881 from ( variable-list ) */
10883 static tree
10884 c_parser_omp_clause_from (c_parser *parser, tree list)
10886 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_FROM, list);
10889 /* OpenMP 4.0:
10890 uniform ( variable-list ) */
10892 static tree
10893 c_parser_omp_clause_uniform (c_parser *parser, tree list)
10895 /* The clauses location. */
10896 location_t loc = c_parser_peek_token (parser)->location;
10898 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10900 list = c_parser_omp_variable_list (parser, loc, OMP_CLAUSE_UNIFORM,
10901 list);
10902 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10904 return list;
10907 /* Parse all OpenMP clauses. The set clauses allowed by the directive
10908 is a bitmask in MASK. Return the list of clauses found; the result
10909 of clause default goes in *pdefault. */
10911 static tree
10912 c_parser_omp_all_clauses (c_parser *parser, omp_clause_mask mask,
10913 const char *where, bool finish_p = true)
10915 tree clauses = NULL;
10916 bool first = true, cilk_simd_fn = false;
10918 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
10920 location_t here;
10921 pragma_omp_clause c_kind;
10922 const char *c_name;
10923 tree prev = clauses;
10925 if (!first && c_parser_next_token_is (parser, CPP_COMMA))
10926 c_parser_consume_token (parser);
10928 here = c_parser_peek_token (parser)->location;
10929 c_kind = c_parser_omp_clause_name (parser);
10931 switch (c_kind)
10933 case PRAGMA_OMP_CLAUSE_COLLAPSE:
10934 clauses = c_parser_omp_clause_collapse (parser, clauses);
10935 c_name = "collapse";
10936 break;
10937 case PRAGMA_OMP_CLAUSE_COPYIN:
10938 clauses = c_parser_omp_clause_copyin (parser, clauses);
10939 c_name = "copyin";
10940 break;
10941 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
10942 clauses = c_parser_omp_clause_copyprivate (parser, clauses);
10943 c_name = "copyprivate";
10944 break;
10945 case PRAGMA_OMP_CLAUSE_DEFAULT:
10946 clauses = c_parser_omp_clause_default (parser, clauses);
10947 c_name = "default";
10948 break;
10949 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
10950 clauses = c_parser_omp_clause_firstprivate (parser, clauses);
10951 c_name = "firstprivate";
10952 break;
10953 case PRAGMA_OMP_CLAUSE_FINAL:
10954 clauses = c_parser_omp_clause_final (parser, clauses);
10955 c_name = "final";
10956 break;
10957 case PRAGMA_OMP_CLAUSE_IF:
10958 clauses = c_parser_omp_clause_if (parser, clauses);
10959 c_name = "if";
10960 break;
10961 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
10962 clauses = c_parser_omp_clause_lastprivate (parser, clauses);
10963 c_name = "lastprivate";
10964 break;
10965 case PRAGMA_OMP_CLAUSE_MERGEABLE:
10966 clauses = c_parser_omp_clause_mergeable (parser, clauses);
10967 c_name = "mergeable";
10968 break;
10969 case PRAGMA_OMP_CLAUSE_NOWAIT:
10970 clauses = c_parser_omp_clause_nowait (parser, clauses);
10971 c_name = "nowait";
10972 break;
10973 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
10974 clauses = c_parser_omp_clause_num_threads (parser, clauses);
10975 c_name = "num_threads";
10976 break;
10977 case PRAGMA_OMP_CLAUSE_ORDERED:
10978 clauses = c_parser_omp_clause_ordered (parser, clauses);
10979 c_name = "ordered";
10980 break;
10981 case PRAGMA_OMP_CLAUSE_PRIVATE:
10982 clauses = c_parser_omp_clause_private (parser, clauses);
10983 c_name = "private";
10984 break;
10985 case PRAGMA_OMP_CLAUSE_REDUCTION:
10986 clauses = c_parser_omp_clause_reduction (parser, clauses);
10987 c_name = "reduction";
10988 break;
10989 case PRAGMA_OMP_CLAUSE_SCHEDULE:
10990 clauses = c_parser_omp_clause_schedule (parser, clauses);
10991 c_name = "schedule";
10992 break;
10993 case PRAGMA_OMP_CLAUSE_SHARED:
10994 clauses = c_parser_omp_clause_shared (parser, clauses);
10995 c_name = "shared";
10996 break;
10997 case PRAGMA_OMP_CLAUSE_UNTIED:
10998 clauses = c_parser_omp_clause_untied (parser, clauses);
10999 c_name = "untied";
11000 break;
11001 case PRAGMA_OMP_CLAUSE_INBRANCH:
11002 case PRAGMA_CILK_CLAUSE_MASK:
11003 clauses = c_parser_omp_clause_branch (parser, OMP_CLAUSE_INBRANCH,
11004 clauses);
11005 c_name = "inbranch";
11006 break;
11007 case PRAGMA_OMP_CLAUSE_NOTINBRANCH:
11008 case PRAGMA_CILK_CLAUSE_NOMASK:
11009 clauses = c_parser_omp_clause_branch (parser, OMP_CLAUSE_NOTINBRANCH,
11010 clauses);
11011 c_name = "notinbranch";
11012 break;
11013 case PRAGMA_OMP_CLAUSE_PARALLEL:
11014 clauses
11015 = c_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_PARALLEL,
11016 clauses);
11017 c_name = "parallel";
11018 if (!first)
11020 clause_not_first:
11021 error_at (here, "%qs must be the first clause of %qs",
11022 c_name, where);
11023 clauses = prev;
11025 break;
11026 case PRAGMA_OMP_CLAUSE_FOR:
11027 clauses
11028 = c_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_FOR,
11029 clauses);
11030 c_name = "for";
11031 if (!first)
11032 goto clause_not_first;
11033 break;
11034 case PRAGMA_OMP_CLAUSE_SECTIONS:
11035 clauses
11036 = c_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_SECTIONS,
11037 clauses);
11038 c_name = "sections";
11039 if (!first)
11040 goto clause_not_first;
11041 break;
11042 case PRAGMA_OMP_CLAUSE_TASKGROUP:
11043 clauses
11044 = c_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_TASKGROUP,
11045 clauses);
11046 c_name = "taskgroup";
11047 if (!first)
11048 goto clause_not_first;
11049 break;
11050 case PRAGMA_OMP_CLAUSE_TO:
11051 clauses = c_parser_omp_clause_to (parser, clauses);
11052 c_name = "to";
11053 break;
11054 case PRAGMA_OMP_CLAUSE_FROM:
11055 clauses = c_parser_omp_clause_from (parser, clauses);
11056 c_name = "from";
11057 break;
11058 case PRAGMA_OMP_CLAUSE_UNIFORM:
11059 clauses = c_parser_omp_clause_uniform (parser, clauses);
11060 c_name = "uniform";
11061 break;
11062 case PRAGMA_OMP_CLAUSE_NUM_TEAMS:
11063 clauses = c_parser_omp_clause_num_teams (parser, clauses);
11064 c_name = "num_teams";
11065 break;
11066 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT:
11067 clauses = c_parser_omp_clause_thread_limit (parser, clauses);
11068 c_name = "thread_limit";
11069 break;
11070 case PRAGMA_OMP_CLAUSE_ALIGNED:
11071 clauses = c_parser_omp_clause_aligned (parser, clauses);
11072 c_name = "aligned";
11073 break;
11074 case PRAGMA_OMP_CLAUSE_LINEAR:
11075 if (((mask >> PRAGMA_CILK_CLAUSE_VECTORLENGTH) & 1) != 0)
11076 cilk_simd_fn = true;
11077 clauses = c_parser_omp_clause_linear (parser, clauses, cilk_simd_fn);
11078 c_name = "linear";
11079 break;
11080 case PRAGMA_OMP_CLAUSE_DEPEND:
11081 clauses = c_parser_omp_clause_depend (parser, clauses);
11082 c_name = "depend";
11083 break;
11084 case PRAGMA_OMP_CLAUSE_MAP:
11085 clauses = c_parser_omp_clause_map (parser, clauses);
11086 c_name = "map";
11087 break;
11088 case PRAGMA_OMP_CLAUSE_DEVICE:
11089 clauses = c_parser_omp_clause_device (parser, clauses);
11090 c_name = "device";
11091 break;
11092 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE:
11093 clauses = c_parser_omp_clause_dist_schedule (parser, clauses);
11094 c_name = "dist_schedule";
11095 break;
11096 case PRAGMA_OMP_CLAUSE_PROC_BIND:
11097 clauses = c_parser_omp_clause_proc_bind (parser, clauses);
11098 c_name = "proc_bind";
11099 break;
11100 case PRAGMA_OMP_CLAUSE_SAFELEN:
11101 clauses = c_parser_omp_clause_safelen (parser, clauses);
11102 c_name = "safelen";
11103 break;
11104 case PRAGMA_CILK_CLAUSE_VECTORLENGTH:
11105 clauses = c_parser_cilk_clause_vectorlength (parser, clauses, true);
11106 c_name = "simdlen";
11107 break;
11108 case PRAGMA_OMP_CLAUSE_SIMDLEN:
11109 clauses = c_parser_omp_clause_simdlen (parser, clauses);
11110 c_name = "simdlen";
11111 break;
11112 default:
11113 c_parser_error (parser, "expected %<#pragma omp%> clause");
11114 goto saw_error;
11117 first = false;
11119 if (((mask >> c_kind) & 1) == 0 && !parser->error)
11121 /* Remove the invalid clause(s) from the list to avoid
11122 confusing the rest of the compiler. */
11123 clauses = prev;
11124 error_at (here, "%qs is not valid for %qs", c_name, where);
11128 saw_error:
11129 c_parser_skip_to_pragma_eol (parser);
11131 if (finish_p)
11132 return c_finish_omp_clauses (clauses);
11134 return clauses;
11137 /* OpenMP 2.5:
11138 structured-block:
11139 statement
11141 In practice, we're also interested in adding the statement to an
11142 outer node. So it is convenient if we work around the fact that
11143 c_parser_statement calls add_stmt. */
11145 static tree
11146 c_parser_omp_structured_block (c_parser *parser)
11148 tree stmt = push_stmt_list ();
11149 c_parser_statement (parser);
11150 return pop_stmt_list (stmt);
11153 /* OpenMP 2.5:
11154 # pragma omp atomic new-line
11155 expression-stmt
11157 expression-stmt:
11158 x binop= expr | x++ | ++x | x-- | --x
11159 binop:
11160 +, *, -, /, &, ^, |, <<, >>
11162 where x is an lvalue expression with scalar type.
11164 OpenMP 3.1:
11165 # pragma omp atomic new-line
11166 update-stmt
11168 # pragma omp atomic read new-line
11169 read-stmt
11171 # pragma omp atomic write new-line
11172 write-stmt
11174 # pragma omp atomic update new-line
11175 update-stmt
11177 # pragma omp atomic capture new-line
11178 capture-stmt
11180 # pragma omp atomic capture new-line
11181 capture-block
11183 read-stmt:
11184 v = x
11185 write-stmt:
11186 x = expr
11187 update-stmt:
11188 expression-stmt | x = x binop expr
11189 capture-stmt:
11190 v = expression-stmt
11191 capture-block:
11192 { v = x; update-stmt; } | { update-stmt; v = x; }
11194 OpenMP 4.0:
11195 update-stmt:
11196 expression-stmt | x = x binop expr | x = expr binop x
11197 capture-stmt:
11198 v = update-stmt
11199 capture-block:
11200 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
11202 where x and v are lvalue expressions with scalar type.
11204 LOC is the location of the #pragma token. */
11206 static void
11207 c_parser_omp_atomic (location_t loc, c_parser *parser)
11209 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE;
11210 tree lhs1 = NULL_TREE, rhs1 = NULL_TREE;
11211 tree stmt, orig_lhs, unfolded_lhs = NULL_TREE, unfolded_lhs1 = NULL_TREE;
11212 enum tree_code code = OMP_ATOMIC, opcode = NOP_EXPR;
11213 struct c_expr expr;
11214 location_t eloc;
11215 bool structured_block = false;
11216 bool swapped = false;
11217 bool seq_cst = false;
11219 if (c_parser_next_token_is (parser, CPP_NAME))
11221 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
11222 if (!strcmp (p, "seq_cst"))
11224 seq_cst = true;
11225 c_parser_consume_token (parser);
11226 if (c_parser_next_token_is (parser, CPP_COMMA)
11227 && c_parser_peek_2nd_token (parser)->type == CPP_NAME)
11228 c_parser_consume_token (parser);
11231 if (c_parser_next_token_is (parser, CPP_NAME))
11233 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
11235 if (!strcmp (p, "read"))
11236 code = OMP_ATOMIC_READ;
11237 else if (!strcmp (p, "write"))
11238 code = NOP_EXPR;
11239 else if (!strcmp (p, "update"))
11240 code = OMP_ATOMIC;
11241 else if (!strcmp (p, "capture"))
11242 code = OMP_ATOMIC_CAPTURE_NEW;
11243 else
11244 p = NULL;
11245 if (p)
11246 c_parser_consume_token (parser);
11248 if (!seq_cst)
11250 if (c_parser_next_token_is (parser, CPP_COMMA)
11251 && c_parser_peek_2nd_token (parser)->type == CPP_NAME)
11252 c_parser_consume_token (parser);
11254 if (c_parser_next_token_is (parser, CPP_NAME))
11256 const char *p
11257 = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
11258 if (!strcmp (p, "seq_cst"))
11260 seq_cst = true;
11261 c_parser_consume_token (parser);
11265 c_parser_skip_to_pragma_eol (parser);
11267 switch (code)
11269 case OMP_ATOMIC_READ:
11270 case NOP_EXPR: /* atomic write */
11271 v = c_parser_unary_expression (parser).value;
11272 v = c_fully_fold (v, false, NULL);
11273 if (v == error_mark_node)
11274 goto saw_error;
11275 loc = c_parser_peek_token (parser)->location;
11276 if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
11277 goto saw_error;
11278 if (code == NOP_EXPR)
11279 lhs = c_parser_expression (parser).value;
11280 else
11281 lhs = c_parser_unary_expression (parser).value;
11282 lhs = c_fully_fold (lhs, false, NULL);
11283 if (lhs == error_mark_node)
11284 goto saw_error;
11285 if (code == NOP_EXPR)
11287 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
11288 opcode. */
11289 code = OMP_ATOMIC;
11290 rhs = lhs;
11291 lhs = v;
11292 v = NULL_TREE;
11294 goto done;
11295 case OMP_ATOMIC_CAPTURE_NEW:
11296 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
11298 c_parser_consume_token (parser);
11299 structured_block = true;
11301 else
11303 v = c_parser_unary_expression (parser).value;
11304 v = c_fully_fold (v, false, NULL);
11305 if (v == error_mark_node)
11306 goto saw_error;
11307 if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
11308 goto saw_error;
11310 break;
11311 default:
11312 break;
11315 /* For structured_block case we don't know yet whether
11316 old or new x should be captured. */
11317 restart:
11318 eloc = c_parser_peek_token (parser)->location;
11319 expr = c_parser_unary_expression (parser);
11320 lhs = expr.value;
11321 expr = default_function_array_conversion (eloc, expr);
11322 unfolded_lhs = expr.value;
11323 lhs = c_fully_fold (lhs, false, NULL);
11324 orig_lhs = lhs;
11325 switch (TREE_CODE (lhs))
11327 case ERROR_MARK:
11328 saw_error:
11329 c_parser_skip_to_end_of_block_or_statement (parser);
11330 if (structured_block)
11332 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
11333 c_parser_consume_token (parser);
11334 else if (code == OMP_ATOMIC_CAPTURE_NEW)
11336 c_parser_skip_to_end_of_block_or_statement (parser);
11337 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
11338 c_parser_consume_token (parser);
11341 return;
11343 case POSTINCREMENT_EXPR:
11344 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
11345 code = OMP_ATOMIC_CAPTURE_OLD;
11346 /* FALLTHROUGH */
11347 case PREINCREMENT_EXPR:
11348 lhs = TREE_OPERAND (lhs, 0);
11349 unfolded_lhs = NULL_TREE;
11350 opcode = PLUS_EXPR;
11351 rhs = integer_one_node;
11352 break;
11354 case POSTDECREMENT_EXPR:
11355 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
11356 code = OMP_ATOMIC_CAPTURE_OLD;
11357 /* FALLTHROUGH */
11358 case PREDECREMENT_EXPR:
11359 lhs = TREE_OPERAND (lhs, 0);
11360 unfolded_lhs = NULL_TREE;
11361 opcode = MINUS_EXPR;
11362 rhs = integer_one_node;
11363 break;
11365 case COMPOUND_EXPR:
11366 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
11367 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
11368 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
11369 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
11370 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
11371 (TREE_OPERAND (lhs, 1), 0), 0)))
11372 == BOOLEAN_TYPE)
11373 /* Undo effects of boolean_increment for post {in,de}crement. */
11374 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
11375 /* FALLTHRU */
11376 case MODIFY_EXPR:
11377 if (TREE_CODE (lhs) == MODIFY_EXPR
11378 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
11380 /* Undo effects of boolean_increment. */
11381 if (integer_onep (TREE_OPERAND (lhs, 1)))
11383 /* This is pre or post increment. */
11384 rhs = TREE_OPERAND (lhs, 1);
11385 lhs = TREE_OPERAND (lhs, 0);
11386 unfolded_lhs = NULL_TREE;
11387 opcode = NOP_EXPR;
11388 if (code == OMP_ATOMIC_CAPTURE_NEW
11389 && !structured_block
11390 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
11391 code = OMP_ATOMIC_CAPTURE_OLD;
11392 break;
11394 if (TREE_CODE (TREE_OPERAND (lhs, 1)) == TRUTH_NOT_EXPR
11395 && TREE_OPERAND (lhs, 0)
11396 == TREE_OPERAND (TREE_OPERAND (lhs, 1), 0))
11398 /* This is pre or post decrement. */
11399 rhs = TREE_OPERAND (lhs, 1);
11400 lhs = TREE_OPERAND (lhs, 0);
11401 unfolded_lhs = NULL_TREE;
11402 opcode = NOP_EXPR;
11403 if (code == OMP_ATOMIC_CAPTURE_NEW
11404 && !structured_block
11405 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
11406 code = OMP_ATOMIC_CAPTURE_OLD;
11407 break;
11410 /* FALLTHRU */
11411 default:
11412 switch (c_parser_peek_token (parser)->type)
11414 case CPP_MULT_EQ:
11415 opcode = MULT_EXPR;
11416 break;
11417 case CPP_DIV_EQ:
11418 opcode = TRUNC_DIV_EXPR;
11419 break;
11420 case CPP_PLUS_EQ:
11421 opcode = PLUS_EXPR;
11422 break;
11423 case CPP_MINUS_EQ:
11424 opcode = MINUS_EXPR;
11425 break;
11426 case CPP_LSHIFT_EQ:
11427 opcode = LSHIFT_EXPR;
11428 break;
11429 case CPP_RSHIFT_EQ:
11430 opcode = RSHIFT_EXPR;
11431 break;
11432 case CPP_AND_EQ:
11433 opcode = BIT_AND_EXPR;
11434 break;
11435 case CPP_OR_EQ:
11436 opcode = BIT_IOR_EXPR;
11437 break;
11438 case CPP_XOR_EQ:
11439 opcode = BIT_XOR_EXPR;
11440 break;
11441 case CPP_EQ:
11442 c_parser_consume_token (parser);
11443 eloc = c_parser_peek_token (parser)->location;
11444 expr = c_parser_expr_no_commas (parser, NULL, unfolded_lhs);
11445 rhs1 = expr.value;
11446 switch (TREE_CODE (rhs1))
11448 case MULT_EXPR:
11449 case TRUNC_DIV_EXPR:
11450 case PLUS_EXPR:
11451 case MINUS_EXPR:
11452 case LSHIFT_EXPR:
11453 case RSHIFT_EXPR:
11454 case BIT_AND_EXPR:
11455 case BIT_IOR_EXPR:
11456 case BIT_XOR_EXPR:
11457 if (c_tree_equal (TREE_OPERAND (rhs1, 0), unfolded_lhs))
11459 opcode = TREE_CODE (rhs1);
11460 rhs = c_fully_fold (TREE_OPERAND (rhs1, 1), false, NULL);
11461 rhs1 = c_fully_fold (TREE_OPERAND (rhs1, 0), false, NULL);
11462 goto stmt_done;
11464 if (c_tree_equal (TREE_OPERAND (rhs1, 1), unfolded_lhs))
11466 opcode = TREE_CODE (rhs1);
11467 rhs = c_fully_fold (TREE_OPERAND (rhs1, 0), false, NULL);
11468 rhs1 = c_fully_fold (TREE_OPERAND (rhs1, 1), false, NULL);
11469 swapped = !commutative_tree_code (opcode);
11470 goto stmt_done;
11472 break;
11473 case ERROR_MARK:
11474 goto saw_error;
11475 default:
11476 break;
11478 if (c_parser_peek_token (parser)->type == CPP_SEMICOLON)
11480 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
11482 code = OMP_ATOMIC_CAPTURE_OLD;
11483 v = lhs;
11484 lhs = NULL_TREE;
11485 expr = default_function_array_read_conversion (eloc, expr);
11486 unfolded_lhs1 = expr.value;
11487 lhs1 = c_fully_fold (unfolded_lhs1, false, NULL);
11488 rhs1 = NULL_TREE;
11489 c_parser_consume_token (parser);
11490 goto restart;
11492 if (structured_block)
11494 opcode = NOP_EXPR;
11495 expr = default_function_array_read_conversion (eloc, expr);
11496 rhs = c_fully_fold (expr.value, false, NULL);
11497 rhs1 = NULL_TREE;
11498 goto stmt_done;
11501 c_parser_error (parser, "invalid form of %<#pragma omp atomic%>");
11502 goto saw_error;
11503 default:
11504 c_parser_error (parser,
11505 "invalid operator for %<#pragma omp atomic%>");
11506 goto saw_error;
11509 /* Arrange to pass the location of the assignment operator to
11510 c_finish_omp_atomic. */
11511 loc = c_parser_peek_token (parser)->location;
11512 c_parser_consume_token (parser);
11513 eloc = c_parser_peek_token (parser)->location;
11514 expr = c_parser_expression (parser);
11515 expr = default_function_array_read_conversion (eloc, expr);
11516 rhs = expr.value;
11517 rhs = c_fully_fold (rhs, false, NULL);
11518 break;
11520 stmt_done:
11521 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
11523 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
11524 goto saw_error;
11525 v = c_parser_unary_expression (parser).value;
11526 v = c_fully_fold (v, false, NULL);
11527 if (v == error_mark_node)
11528 goto saw_error;
11529 if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
11530 goto saw_error;
11531 eloc = c_parser_peek_token (parser)->location;
11532 expr = c_parser_unary_expression (parser);
11533 lhs1 = expr.value;
11534 expr = default_function_array_read_conversion (eloc, expr);
11535 unfolded_lhs1 = expr.value;
11536 lhs1 = c_fully_fold (lhs1, false, NULL);
11537 if (lhs1 == error_mark_node)
11538 goto saw_error;
11540 if (structured_block)
11542 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
11543 c_parser_require (parser, CPP_CLOSE_BRACE, "expected %<}%>");
11545 done:
11546 if (unfolded_lhs && unfolded_lhs1
11547 && !c_tree_equal (unfolded_lhs, unfolded_lhs1))
11549 error ("%<#pragma omp atomic capture%> uses two different "
11550 "expressions for memory");
11551 stmt = error_mark_node;
11553 else
11554 stmt = c_finish_omp_atomic (loc, code, opcode, lhs, rhs, v, lhs1, rhs1,
11555 swapped, seq_cst);
11556 if (stmt != error_mark_node)
11557 add_stmt (stmt);
11559 if (!structured_block)
11560 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
11564 /* OpenMP 2.5:
11565 # pragma omp barrier new-line
11568 static void
11569 c_parser_omp_barrier (c_parser *parser)
11571 location_t loc = c_parser_peek_token (parser)->location;
11572 c_parser_consume_pragma (parser);
11573 c_parser_skip_to_pragma_eol (parser);
11575 c_finish_omp_barrier (loc);
11578 /* OpenMP 2.5:
11579 # pragma omp critical [(name)] new-line
11580 structured-block
11582 LOC is the location of the #pragma itself. */
11584 static tree
11585 c_parser_omp_critical (location_t loc, c_parser *parser)
11587 tree stmt, name = NULL;
11589 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
11591 c_parser_consume_token (parser);
11592 if (c_parser_next_token_is (parser, CPP_NAME))
11594 name = c_parser_peek_token (parser)->value;
11595 c_parser_consume_token (parser);
11596 c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11598 else
11599 c_parser_error (parser, "expected identifier");
11601 else if (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
11602 c_parser_error (parser, "expected %<(%> or end of line");
11603 c_parser_skip_to_pragma_eol (parser);
11605 stmt = c_parser_omp_structured_block (parser);
11606 return c_finish_omp_critical (loc, stmt, name);
11609 /* OpenMP 2.5:
11610 # pragma omp flush flush-vars[opt] new-line
11612 flush-vars:
11613 ( variable-list ) */
11615 static void
11616 c_parser_omp_flush (c_parser *parser)
11618 location_t loc = c_parser_peek_token (parser)->location;
11619 c_parser_consume_pragma (parser);
11620 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
11621 c_parser_omp_var_list_parens (parser, OMP_CLAUSE_ERROR, NULL);
11622 else if (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
11623 c_parser_error (parser, "expected %<(%> or end of line");
11624 c_parser_skip_to_pragma_eol (parser);
11626 c_finish_omp_flush (loc);
11629 /* Parse the restricted form of the for statement allowed by OpenMP.
11630 The real trick here is to determine the loop control variable early
11631 so that we can push a new decl if necessary to make it private.
11632 LOC is the location of the OMP in "#pragma omp". */
11634 static tree
11635 c_parser_omp_for_loop (location_t loc, c_parser *parser, enum tree_code code,
11636 tree clauses, tree *cclauses)
11638 tree decl, cond, incr, save_break, save_cont, body, init, stmt, cl;
11639 tree declv, condv, incrv, initv, ret = NULL;
11640 bool fail = false, open_brace_parsed = false;
11641 int i, collapse = 1, nbraces = 0;
11642 location_t for_loc;
11643 vec<tree, va_gc> *for_block = make_tree_vector ();
11645 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
11646 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
11647 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl));
11649 gcc_assert (collapse >= 1);
11651 declv = make_tree_vec (collapse);
11652 initv = make_tree_vec (collapse);
11653 condv = make_tree_vec (collapse);
11654 incrv = make_tree_vec (collapse);
11656 if (!c_parser_next_token_is_keyword (parser, RID_FOR))
11658 c_parser_error (parser, "for statement expected");
11659 return NULL;
11661 for_loc = c_parser_peek_token (parser)->location;
11662 c_parser_consume_token (parser);
11664 for (i = 0; i < collapse; i++)
11666 int bracecount = 0;
11668 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11669 goto pop_scopes;
11671 /* Parse the initialization declaration or expression. */
11672 if (c_parser_next_tokens_start_declaration (parser))
11674 if (i > 0)
11675 vec_safe_push (for_block, c_begin_compound_stmt (true));
11676 c_parser_declaration_or_fndef (parser, true, true, true, true, true,
11677 NULL, vNULL);
11678 decl = check_for_loop_decls (for_loc, flag_isoc99);
11679 if (decl == NULL)
11680 goto error_init;
11681 if (DECL_INITIAL (decl) == error_mark_node)
11682 decl = error_mark_node;
11683 init = decl;
11685 else if (c_parser_next_token_is (parser, CPP_NAME)
11686 && c_parser_peek_2nd_token (parser)->type == CPP_EQ)
11688 struct c_expr decl_exp;
11689 struct c_expr init_exp;
11690 location_t init_loc;
11692 decl_exp = c_parser_postfix_expression (parser);
11693 decl = decl_exp.value;
11695 c_parser_require (parser, CPP_EQ, "expected %<=%>");
11697 init_loc = c_parser_peek_token (parser)->location;
11698 init_exp = c_parser_expr_no_commas (parser, NULL);
11699 init_exp = default_function_array_read_conversion (init_loc,
11700 init_exp);
11701 init = build_modify_expr (init_loc, decl, decl_exp.original_type,
11702 NOP_EXPR, init_loc, init_exp.value,
11703 init_exp.original_type);
11704 init = c_process_expr_stmt (init_loc, init);
11706 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
11708 else
11710 error_init:
11711 c_parser_error (parser,
11712 "expected iteration declaration or initialization");
11713 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
11714 "expected %<)%>");
11715 fail = true;
11716 goto parse_next;
11719 /* Parse the loop condition. */
11720 cond = NULL_TREE;
11721 if (c_parser_next_token_is_not (parser, CPP_SEMICOLON))
11723 location_t cond_loc = c_parser_peek_token (parser)->location;
11724 struct c_expr cond_expr
11725 = c_parser_binary_expression (parser, NULL, NULL_TREE);
11727 cond = cond_expr.value;
11728 cond = c_objc_common_truthvalue_conversion (cond_loc, cond);
11729 cond = c_fully_fold (cond, false, NULL);
11730 switch (cond_expr.original_code)
11732 case GT_EXPR:
11733 case GE_EXPR:
11734 case LT_EXPR:
11735 case LE_EXPR:
11736 break;
11737 case NE_EXPR:
11738 if (code == CILK_SIMD)
11739 break;
11740 /* FALLTHRU. */
11741 default:
11742 /* Can't be cond = error_mark_node, because we want to preserve
11743 the location until c_finish_omp_for. */
11744 cond = build1 (NOP_EXPR, boolean_type_node, error_mark_node);
11745 break;
11747 protected_set_expr_location (cond, cond_loc);
11749 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
11751 /* Parse the increment expression. */
11752 incr = NULL_TREE;
11753 if (c_parser_next_token_is_not (parser, CPP_CLOSE_PAREN))
11755 location_t incr_loc = c_parser_peek_token (parser)->location;
11757 incr = c_process_expr_stmt (incr_loc,
11758 c_parser_expression (parser).value);
11760 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11762 if (decl == NULL || decl == error_mark_node || init == error_mark_node)
11763 fail = true;
11764 else
11766 TREE_VEC_ELT (declv, i) = decl;
11767 TREE_VEC_ELT (initv, i) = init;
11768 TREE_VEC_ELT (condv, i) = cond;
11769 TREE_VEC_ELT (incrv, i) = incr;
11772 parse_next:
11773 if (i == collapse - 1)
11774 break;
11776 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
11777 in between the collapsed for loops to be still considered perfectly
11778 nested. Hopefully the final version clarifies this.
11779 For now handle (multiple) {'s and empty statements. */
11782 if (c_parser_next_token_is_keyword (parser, RID_FOR))
11784 c_parser_consume_token (parser);
11785 break;
11787 else if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
11789 c_parser_consume_token (parser);
11790 bracecount++;
11792 else if (bracecount
11793 && c_parser_next_token_is (parser, CPP_SEMICOLON))
11794 c_parser_consume_token (parser);
11795 else
11797 c_parser_error (parser, "not enough perfectly nested loops");
11798 if (bracecount)
11800 open_brace_parsed = true;
11801 bracecount--;
11803 fail = true;
11804 collapse = 0;
11805 break;
11808 while (1);
11810 nbraces += bracecount;
11813 save_break = c_break_label;
11814 if (code == CILK_SIMD)
11815 c_break_label = build_int_cst (size_type_node, 2);
11816 else
11817 c_break_label = size_one_node;
11818 save_cont = c_cont_label;
11819 c_cont_label = NULL_TREE;
11820 body = push_stmt_list ();
11822 if (open_brace_parsed)
11824 location_t here = c_parser_peek_token (parser)->location;
11825 stmt = c_begin_compound_stmt (true);
11826 c_parser_compound_statement_nostart (parser);
11827 add_stmt (c_end_compound_stmt (here, stmt, true));
11829 else
11830 add_stmt (c_parser_c99_block_statement (parser));
11831 if (c_cont_label)
11833 tree t = build1 (LABEL_EXPR, void_type_node, c_cont_label);
11834 SET_EXPR_LOCATION (t, loc);
11835 add_stmt (t);
11838 body = pop_stmt_list (body);
11839 c_break_label = save_break;
11840 c_cont_label = save_cont;
11842 while (nbraces)
11844 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
11846 c_parser_consume_token (parser);
11847 nbraces--;
11849 else if (c_parser_next_token_is (parser, CPP_SEMICOLON))
11850 c_parser_consume_token (parser);
11851 else
11853 c_parser_error (parser, "collapsed loops not perfectly nested");
11854 while (nbraces)
11856 location_t here = c_parser_peek_token (parser)->location;
11857 stmt = c_begin_compound_stmt (true);
11858 add_stmt (body);
11859 c_parser_compound_statement_nostart (parser);
11860 body = c_end_compound_stmt (here, stmt, true);
11861 nbraces--;
11863 goto pop_scopes;
11867 /* Only bother calling c_finish_omp_for if we haven't already generated
11868 an error from the initialization parsing. */
11869 if (!fail)
11871 stmt = c_finish_omp_for (loc, code, declv, initv, condv,
11872 incrv, body, NULL);
11873 if (stmt)
11875 if (cclauses != NULL
11876 && cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL] != NULL)
11878 tree *c;
11879 for (c = &cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL]; *c ; )
11880 if (OMP_CLAUSE_CODE (*c) != OMP_CLAUSE_FIRSTPRIVATE
11881 && OMP_CLAUSE_CODE (*c) != OMP_CLAUSE_LASTPRIVATE)
11882 c = &OMP_CLAUSE_CHAIN (*c);
11883 else
11885 for (i = 0; i < collapse; i++)
11886 if (TREE_VEC_ELT (declv, i) == OMP_CLAUSE_DECL (*c))
11887 break;
11888 if (i == collapse)
11889 c = &OMP_CLAUSE_CHAIN (*c);
11890 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE)
11892 error_at (loc,
11893 "iteration variable %qD should not be firstprivate",
11894 OMP_CLAUSE_DECL (*c));
11895 *c = OMP_CLAUSE_CHAIN (*c);
11897 else
11899 /* Copy lastprivate (decl) clause to OMP_FOR_CLAUSES,
11900 change it to shared (decl) in
11901 OMP_PARALLEL_CLAUSES. */
11902 tree l = build_omp_clause (OMP_CLAUSE_LOCATION (*c),
11903 OMP_CLAUSE_LASTPRIVATE);
11904 OMP_CLAUSE_DECL (l) = OMP_CLAUSE_DECL (*c);
11905 if (code == OMP_SIMD)
11907 OMP_CLAUSE_CHAIN (l)
11908 = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
11909 cclauses[C_OMP_CLAUSE_SPLIT_FOR] = l;
11911 else
11913 OMP_CLAUSE_CHAIN (l) = clauses;
11914 clauses = l;
11916 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
11920 OMP_FOR_CLAUSES (stmt) = clauses;
11922 ret = stmt;
11924 pop_scopes:
11925 while (!for_block->is_empty ())
11927 /* FIXME diagnostics: LOC below should be the actual location of
11928 this particular for block. We need to build a list of
11929 locations to go along with FOR_BLOCK. */
11930 stmt = c_end_compound_stmt (loc, for_block->pop (), true);
11931 add_stmt (stmt);
11933 release_tree_vector (for_block);
11934 return ret;
11937 /* Helper function for OpenMP parsing, split clauses and call
11938 finish_omp_clauses on each of the set of clauses afterwards. */
11940 static void
11941 omp_split_clauses (location_t loc, enum tree_code code,
11942 omp_clause_mask mask, tree clauses, tree *cclauses)
11944 int i;
11945 c_omp_split_clauses (loc, code, mask, clauses, cclauses);
11946 for (i = 0; i < C_OMP_CLAUSE_SPLIT_COUNT; i++)
11947 if (cclauses[i])
11948 cclauses[i] = c_finish_omp_clauses (cclauses[i]);
11951 /* OpenMP 4.0:
11952 #pragma omp simd simd-clause[optseq] new-line
11953 for-loop
11955 LOC is the location of the #pragma token.
11958 #define OMP_SIMD_CLAUSE_MASK \
11959 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
11960 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
11961 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
11962 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
11963 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
11964 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
11965 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
11967 static tree
11968 c_parser_omp_simd (location_t loc, c_parser *parser,
11969 char *p_name, omp_clause_mask mask, tree *cclauses)
11971 tree block, clauses, ret;
11973 strcat (p_name, " simd");
11974 mask |= OMP_SIMD_CLAUSE_MASK;
11975 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED);
11977 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
11978 if (cclauses)
11980 omp_split_clauses (loc, OMP_SIMD, mask, clauses, cclauses);
11981 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SIMD];
11984 block = c_begin_compound_stmt (true);
11985 ret = c_parser_omp_for_loop (loc, parser, OMP_SIMD, clauses, cclauses);
11986 block = c_end_compound_stmt (loc, block, true);
11987 add_stmt (block);
11989 return ret;
11992 /* OpenMP 2.5:
11993 #pragma omp for for-clause[optseq] new-line
11994 for-loop
11996 OpenMP 4.0:
11997 #pragma omp for simd for-simd-clause[optseq] new-line
11998 for-loop
12000 LOC is the location of the #pragma token.
12003 #define OMP_FOR_CLAUSE_MASK \
12004 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
12005 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
12006 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
12007 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
12008 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
12009 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
12010 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
12011 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
12013 static tree
12014 c_parser_omp_for (location_t loc, c_parser *parser,
12015 char *p_name, omp_clause_mask mask, tree *cclauses)
12017 tree block, clauses, ret;
12019 strcat (p_name, " for");
12020 mask |= OMP_FOR_CLAUSE_MASK;
12021 if (cclauses)
12022 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
12024 if (c_parser_next_token_is (parser, CPP_NAME))
12026 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
12028 if (strcmp (p, "simd") == 0)
12030 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
12031 if (cclauses == NULL)
12032 cclauses = cclauses_buf;
12034 c_parser_consume_token (parser);
12035 if (!flag_openmp) /* flag_openmp_simd */
12036 return c_parser_omp_simd (loc, parser, p_name, mask, cclauses);
12037 block = c_begin_compound_stmt (true);
12038 ret = c_parser_omp_simd (loc, parser, p_name, mask, cclauses);
12039 block = c_end_compound_stmt (loc, block, true);
12040 if (ret == NULL_TREE)
12041 return ret;
12042 ret = make_node (OMP_FOR);
12043 TREE_TYPE (ret) = void_type_node;
12044 OMP_FOR_BODY (ret) = block;
12045 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
12046 SET_EXPR_LOCATION (ret, loc);
12047 add_stmt (ret);
12048 return ret;
12051 if (!flag_openmp) /* flag_openmp_simd */
12053 c_parser_skip_to_pragma_eol (parser);
12054 return NULL_TREE;
12057 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
12058 if (cclauses)
12060 omp_split_clauses (loc, OMP_FOR, mask, clauses, cclauses);
12061 clauses = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
12064 block = c_begin_compound_stmt (true);
12065 ret = c_parser_omp_for_loop (loc, parser, OMP_FOR, clauses, cclauses);
12066 block = c_end_compound_stmt (loc, block, true);
12067 add_stmt (block);
12069 return ret;
12072 /* OpenMP 2.5:
12073 # pragma omp master new-line
12074 structured-block
12076 LOC is the location of the #pragma token.
12079 static tree
12080 c_parser_omp_master (location_t loc, c_parser *parser)
12082 c_parser_skip_to_pragma_eol (parser);
12083 return c_finish_omp_master (loc, c_parser_omp_structured_block (parser));
12086 /* OpenMP 2.5:
12087 # pragma omp ordered new-line
12088 structured-block
12090 LOC is the location of the #pragma itself.
12093 static tree
12094 c_parser_omp_ordered (location_t loc, c_parser *parser)
12096 c_parser_skip_to_pragma_eol (parser);
12097 return c_finish_omp_ordered (loc, c_parser_omp_structured_block (parser));
12100 /* OpenMP 2.5:
12102 section-scope:
12103 { section-sequence }
12105 section-sequence:
12106 section-directive[opt] structured-block
12107 section-sequence section-directive structured-block
12109 SECTIONS_LOC is the location of the #pragma omp sections. */
12111 static tree
12112 c_parser_omp_sections_scope (location_t sections_loc, c_parser *parser)
12114 tree stmt, substmt;
12115 bool error_suppress = false;
12116 location_t loc;
12118 loc = c_parser_peek_token (parser)->location;
12119 if (!c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
12121 /* Avoid skipping until the end of the block. */
12122 parser->error = false;
12123 return NULL_TREE;
12126 stmt = push_stmt_list ();
12128 if (c_parser_peek_token (parser)->pragma_kind != PRAGMA_OMP_SECTION)
12130 substmt = c_parser_omp_structured_block (parser);
12131 substmt = build1 (OMP_SECTION, void_type_node, substmt);
12132 SET_EXPR_LOCATION (substmt, loc);
12133 add_stmt (substmt);
12136 while (1)
12138 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
12139 break;
12140 if (c_parser_next_token_is (parser, CPP_EOF))
12141 break;
12143 loc = c_parser_peek_token (parser)->location;
12144 if (c_parser_peek_token (parser)->pragma_kind == PRAGMA_OMP_SECTION)
12146 c_parser_consume_pragma (parser);
12147 c_parser_skip_to_pragma_eol (parser);
12148 error_suppress = false;
12150 else if (!error_suppress)
12152 error_at (loc, "expected %<#pragma omp section%> or %<}%>");
12153 error_suppress = true;
12156 substmt = c_parser_omp_structured_block (parser);
12157 substmt = build1 (OMP_SECTION, void_type_node, substmt);
12158 SET_EXPR_LOCATION (substmt, loc);
12159 add_stmt (substmt);
12161 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE,
12162 "expected %<#pragma omp section%> or %<}%>");
12164 substmt = pop_stmt_list (stmt);
12166 stmt = make_node (OMP_SECTIONS);
12167 SET_EXPR_LOCATION (stmt, sections_loc);
12168 TREE_TYPE (stmt) = void_type_node;
12169 OMP_SECTIONS_BODY (stmt) = substmt;
12171 return add_stmt (stmt);
12174 /* OpenMP 2.5:
12175 # pragma omp sections sections-clause[optseq] newline
12176 sections-scope
12178 LOC is the location of the #pragma token.
12181 #define OMP_SECTIONS_CLAUSE_MASK \
12182 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
12183 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
12184 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
12185 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
12186 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
12188 static tree
12189 c_parser_omp_sections (location_t loc, c_parser *parser,
12190 char *p_name, omp_clause_mask mask, tree *cclauses)
12192 tree block, clauses, ret;
12194 strcat (p_name, " sections");
12195 mask |= OMP_SECTIONS_CLAUSE_MASK;
12196 if (cclauses)
12197 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
12199 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
12200 if (cclauses)
12202 omp_split_clauses (loc, OMP_SECTIONS, mask, clauses, cclauses);
12203 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SECTIONS];
12206 block = c_begin_compound_stmt (true);
12207 ret = c_parser_omp_sections_scope (loc, parser);
12208 if (ret)
12209 OMP_SECTIONS_CLAUSES (ret) = clauses;
12210 block = c_end_compound_stmt (loc, block, true);
12211 add_stmt (block);
12213 return ret;
12216 /* OpenMP 2.5:
12217 # pragma omp parallel parallel-clause[optseq] new-line
12218 structured-block
12219 # pragma omp parallel for parallel-for-clause[optseq] new-line
12220 structured-block
12221 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
12222 structured-block
12224 OpenMP 4.0:
12225 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
12226 structured-block
12228 LOC is the location of the #pragma token.
12231 #define OMP_PARALLEL_CLAUSE_MASK \
12232 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
12233 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
12234 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
12235 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
12236 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
12237 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
12238 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
12239 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
12240 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
12242 static tree
12243 c_parser_omp_parallel (location_t loc, c_parser *parser,
12244 char *p_name, omp_clause_mask mask, tree *cclauses)
12246 tree stmt, clauses, block;
12248 strcat (p_name, " parallel");
12249 mask |= OMP_PARALLEL_CLAUSE_MASK;
12251 if (c_parser_next_token_is_keyword (parser, RID_FOR))
12253 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
12254 if (cclauses == NULL)
12255 cclauses = cclauses_buf;
12257 c_parser_consume_token (parser);
12258 if (!flag_openmp) /* flag_openmp_simd */
12259 return c_parser_omp_for (loc, parser, p_name, mask, cclauses);
12260 block = c_begin_omp_parallel ();
12261 tree ret = c_parser_omp_for (loc, parser, p_name, mask, cclauses);
12262 stmt
12263 = c_finish_omp_parallel (loc, cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
12264 block);
12265 if (ret == NULL_TREE)
12266 return ret;
12267 OMP_PARALLEL_COMBINED (stmt) = 1;
12268 return stmt;
12270 else if (cclauses)
12272 error_at (loc, "expected %<for%> after %qs", p_name);
12273 c_parser_skip_to_pragma_eol (parser);
12274 return NULL_TREE;
12276 else if (!flag_openmp) /* flag_openmp_simd */
12278 c_parser_skip_to_pragma_eol (parser);
12279 return NULL_TREE;
12281 else if (c_parser_next_token_is (parser, CPP_NAME))
12283 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
12284 if (strcmp (p, "sections") == 0)
12286 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
12287 if (cclauses == NULL)
12288 cclauses = cclauses_buf;
12290 c_parser_consume_token (parser);
12291 block = c_begin_omp_parallel ();
12292 c_parser_omp_sections (loc, parser, p_name, mask, cclauses);
12293 stmt = c_finish_omp_parallel (loc,
12294 cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
12295 block);
12296 OMP_PARALLEL_COMBINED (stmt) = 1;
12297 return stmt;
12301 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
12303 block = c_begin_omp_parallel ();
12304 c_parser_statement (parser);
12305 stmt = c_finish_omp_parallel (loc, clauses, block);
12307 return stmt;
12310 /* OpenMP 2.5:
12311 # pragma omp single single-clause[optseq] new-line
12312 structured-block
12314 LOC is the location of the #pragma.
12317 #define OMP_SINGLE_CLAUSE_MASK \
12318 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
12319 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
12320 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
12321 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
12323 static tree
12324 c_parser_omp_single (location_t loc, c_parser *parser)
12326 tree stmt = make_node (OMP_SINGLE);
12327 SET_EXPR_LOCATION (stmt, loc);
12328 TREE_TYPE (stmt) = void_type_node;
12330 OMP_SINGLE_CLAUSES (stmt)
12331 = c_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
12332 "#pragma omp single");
12333 OMP_SINGLE_BODY (stmt) = c_parser_omp_structured_block (parser);
12335 return add_stmt (stmt);
12338 /* OpenMP 3.0:
12339 # pragma omp task task-clause[optseq] new-line
12341 LOC is the location of the #pragma.
12344 #define OMP_TASK_CLAUSE_MASK \
12345 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
12346 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
12347 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
12348 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
12349 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
12350 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
12351 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
12352 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
12353 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND))
12355 static tree
12356 c_parser_omp_task (location_t loc, c_parser *parser)
12358 tree clauses, block;
12360 clauses = c_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
12361 "#pragma omp task");
12363 block = c_begin_omp_task ();
12364 c_parser_statement (parser);
12365 return c_finish_omp_task (loc, clauses, block);
12368 /* OpenMP 3.0:
12369 # pragma omp taskwait new-line
12372 static void
12373 c_parser_omp_taskwait (c_parser *parser)
12375 location_t loc = c_parser_peek_token (parser)->location;
12376 c_parser_consume_pragma (parser);
12377 c_parser_skip_to_pragma_eol (parser);
12379 c_finish_omp_taskwait (loc);
12382 /* OpenMP 3.1:
12383 # pragma omp taskyield new-line
12386 static void
12387 c_parser_omp_taskyield (c_parser *parser)
12389 location_t loc = c_parser_peek_token (parser)->location;
12390 c_parser_consume_pragma (parser);
12391 c_parser_skip_to_pragma_eol (parser);
12393 c_finish_omp_taskyield (loc);
12396 /* OpenMP 4.0:
12397 # pragma omp taskgroup new-line
12400 static tree
12401 c_parser_omp_taskgroup (c_parser *parser)
12403 location_t loc = c_parser_peek_token (parser)->location;
12404 c_parser_skip_to_pragma_eol (parser);
12405 return c_finish_omp_taskgroup (loc, c_parser_omp_structured_block (parser));
12408 /* OpenMP 4.0:
12409 # pragma omp cancel cancel-clause[optseq] new-line
12411 LOC is the location of the #pragma.
12414 #define OMP_CANCEL_CLAUSE_MASK \
12415 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
12416 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
12417 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
12418 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
12419 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
12421 static void
12422 c_parser_omp_cancel (c_parser *parser)
12424 location_t loc = c_parser_peek_token (parser)->location;
12426 c_parser_consume_pragma (parser);
12427 tree clauses = c_parser_omp_all_clauses (parser, OMP_CANCEL_CLAUSE_MASK,
12428 "#pragma omp cancel");
12430 c_finish_omp_cancel (loc, clauses);
12433 /* OpenMP 4.0:
12434 # pragma omp cancellation point cancelpt-clause[optseq] new-line
12436 LOC is the location of the #pragma.
12439 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
12440 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
12441 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
12442 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
12443 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
12445 static void
12446 c_parser_omp_cancellation_point (c_parser *parser)
12448 location_t loc = c_parser_peek_token (parser)->location;
12449 tree clauses;
12450 bool point_seen = false;
12452 c_parser_consume_pragma (parser);
12453 if (c_parser_next_token_is (parser, CPP_NAME))
12455 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
12456 if (strcmp (p, "point") == 0)
12458 c_parser_consume_token (parser);
12459 point_seen = true;
12462 if (!point_seen)
12464 c_parser_error (parser, "expected %<point%>");
12465 c_parser_skip_to_pragma_eol (parser);
12466 return;
12469 clauses
12470 = c_parser_omp_all_clauses (parser, OMP_CANCELLATION_POINT_CLAUSE_MASK,
12471 "#pragma omp cancellation point");
12473 c_finish_omp_cancellation_point (loc, clauses);
12476 /* OpenMP 4.0:
12477 #pragma omp distribute distribute-clause[optseq] new-line
12478 for-loop */
12480 #define OMP_DISTRIBUTE_CLAUSE_MASK \
12481 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
12482 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
12483 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
12484 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
12486 static tree
12487 c_parser_omp_distribute (location_t loc, c_parser *parser,
12488 char *p_name, omp_clause_mask mask, tree *cclauses)
12490 tree clauses, block, ret;
12492 strcat (p_name, " distribute");
12493 mask |= OMP_DISTRIBUTE_CLAUSE_MASK;
12495 if (c_parser_next_token_is (parser, CPP_NAME))
12497 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
12498 bool simd = false;
12499 bool parallel = false;
12501 if (strcmp (p, "simd") == 0)
12502 simd = true;
12503 else
12504 parallel = strcmp (p, "parallel") == 0;
12505 if (parallel || simd)
12507 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
12508 if (cclauses == NULL)
12509 cclauses = cclauses_buf;
12510 c_parser_consume_token (parser);
12511 if (!flag_openmp) /* flag_openmp_simd */
12513 if (simd)
12514 return c_parser_omp_simd (loc, parser, p_name, mask, cclauses);
12515 else
12516 return c_parser_omp_parallel (loc, parser, p_name, mask,
12517 cclauses);
12519 block = c_begin_compound_stmt (true);
12520 if (simd)
12521 ret = c_parser_omp_simd (loc, parser, p_name, mask, cclauses);
12522 else
12523 ret = c_parser_omp_parallel (loc, parser, p_name, mask, cclauses);
12524 block = c_end_compound_stmt (loc, block, true);
12525 if (ret == NULL)
12526 return ret;
12527 ret = make_node (OMP_DISTRIBUTE);
12528 TREE_TYPE (ret) = void_type_node;
12529 OMP_FOR_BODY (ret) = block;
12530 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
12531 SET_EXPR_LOCATION (ret, loc);
12532 add_stmt (ret);
12533 return ret;
12536 if (!flag_openmp) /* flag_openmp_simd */
12538 c_parser_skip_to_pragma_eol (parser);
12539 return NULL_TREE;
12542 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
12543 if (cclauses)
12545 omp_split_clauses (loc, OMP_DISTRIBUTE, mask, clauses, cclauses);
12546 clauses = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
12549 block = c_begin_compound_stmt (true);
12550 ret = c_parser_omp_for_loop (loc, parser, OMP_DISTRIBUTE, clauses, NULL);
12551 block = c_end_compound_stmt (loc, block, true);
12552 add_stmt (block);
12554 return ret;
12557 /* OpenMP 4.0:
12558 # pragma omp teams teams-clause[optseq] new-line
12559 structured-block */
12561 #define OMP_TEAMS_CLAUSE_MASK \
12562 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
12563 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
12564 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
12565 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
12566 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
12567 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
12568 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
12570 static tree
12571 c_parser_omp_teams (location_t loc, c_parser *parser,
12572 char *p_name, omp_clause_mask mask, tree *cclauses)
12574 tree clauses, block, ret;
12576 strcat (p_name, " teams");
12577 mask |= OMP_TEAMS_CLAUSE_MASK;
12579 if (c_parser_next_token_is (parser, CPP_NAME))
12581 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
12582 if (strcmp (p, "distribute") == 0)
12584 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
12585 if (cclauses == NULL)
12586 cclauses = cclauses_buf;
12588 c_parser_consume_token (parser);
12589 if (!flag_openmp) /* flag_openmp_simd */
12590 return c_parser_omp_distribute (loc, parser, p_name, mask, cclauses);
12591 block = c_begin_compound_stmt (true);
12592 ret = c_parser_omp_distribute (loc, parser, p_name, mask, cclauses);
12593 block = c_end_compound_stmt (loc, block, true);
12594 if (ret == NULL)
12595 return ret;
12596 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
12597 ret = make_node (OMP_TEAMS);
12598 TREE_TYPE (ret) = void_type_node;
12599 OMP_TEAMS_CLAUSES (ret) = clauses;
12600 OMP_TEAMS_BODY (ret) = block;
12601 return add_stmt (ret);
12604 if (!flag_openmp) /* flag_openmp_simd */
12606 c_parser_skip_to_pragma_eol (parser);
12607 return NULL_TREE;
12610 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
12611 if (cclauses)
12613 omp_split_clauses (loc, OMP_TEAMS, mask, clauses, cclauses);
12614 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
12617 tree stmt = make_node (OMP_TEAMS);
12618 TREE_TYPE (stmt) = void_type_node;
12619 OMP_TEAMS_CLAUSES (stmt) = clauses;
12620 OMP_TEAMS_BODY (stmt) = c_parser_omp_structured_block (parser);
12622 return add_stmt (stmt);
12625 /* OpenMP 4.0:
12626 # pragma omp target data target-data-clause[optseq] new-line
12627 structured-block */
12629 #define OMP_TARGET_DATA_CLAUSE_MASK \
12630 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
12631 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
12632 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
12634 static tree
12635 c_parser_omp_target_data (location_t loc, c_parser *parser)
12637 tree stmt = make_node (OMP_TARGET_DATA);
12638 TREE_TYPE (stmt) = void_type_node;
12640 OMP_TARGET_DATA_CLAUSES (stmt)
12641 = c_parser_omp_all_clauses (parser, OMP_TARGET_DATA_CLAUSE_MASK,
12642 "#pragma omp target data");
12643 keep_next_level ();
12644 tree block = c_begin_compound_stmt (true);
12645 add_stmt (c_parser_omp_structured_block (parser));
12646 OMP_TARGET_DATA_BODY (stmt) = c_end_compound_stmt (loc, block, true);
12648 SET_EXPR_LOCATION (stmt, loc);
12649 return add_stmt (stmt);
12652 /* OpenMP 4.0:
12653 # pragma omp target update target-update-clause[optseq] new-line */
12655 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
12656 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
12657 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
12658 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
12659 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
12661 static bool
12662 c_parser_omp_target_update (location_t loc, c_parser *parser,
12663 enum pragma_context context)
12665 if (context == pragma_stmt)
12667 error_at (loc,
12668 "%<#pragma omp target update%> may only be "
12669 "used in compound statements");
12670 c_parser_skip_to_pragma_eol (parser);
12671 return false;
12674 tree clauses
12675 = c_parser_omp_all_clauses (parser, OMP_TARGET_UPDATE_CLAUSE_MASK,
12676 "#pragma omp target update");
12677 if (find_omp_clause (clauses, OMP_CLAUSE_TO) == NULL_TREE
12678 && find_omp_clause (clauses, OMP_CLAUSE_FROM) == NULL_TREE)
12680 error_at (loc,
12681 "%<#pragma omp target update must contain at least one "
12682 "%<from%> or %<to%> clauses");
12683 return false;
12686 tree stmt = make_node (OMP_TARGET_UPDATE);
12687 TREE_TYPE (stmt) = void_type_node;
12688 OMP_TARGET_UPDATE_CLAUSES (stmt) = clauses;
12689 SET_EXPR_LOCATION (stmt, loc);
12690 add_stmt (stmt);
12691 return false;
12694 /* OpenMP 4.0:
12695 # pragma omp target target-clause[optseq] new-line
12696 structured-block */
12698 #define OMP_TARGET_CLAUSE_MASK \
12699 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
12700 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
12701 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
12703 static bool
12704 c_parser_omp_target (c_parser *parser, enum pragma_context context)
12706 location_t loc = c_parser_peek_token (parser)->location;
12707 c_parser_consume_pragma (parser);
12709 if (context != pragma_stmt && context != pragma_compound)
12711 c_parser_error (parser, "expected declaration specifiers");
12712 c_parser_skip_to_pragma_eol (parser);
12713 return false;
12716 if (c_parser_next_token_is (parser, CPP_NAME))
12718 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
12720 if (strcmp (p, "teams") == 0)
12722 tree cclauses[C_OMP_CLAUSE_SPLIT_COUNT];
12723 char p_name[sizeof ("#pragma omp target teams distribute "
12724 "parallel for simd")];
12726 c_parser_consume_token (parser);
12727 strcpy (p_name, "#pragma omp target");
12728 if (!flag_openmp) /* flag_openmp_simd */
12730 tree stmt = c_parser_omp_teams (loc, parser, p_name,
12731 OMP_TARGET_CLAUSE_MASK,
12732 cclauses);
12733 return stmt != NULL_TREE;
12735 keep_next_level ();
12736 tree block = c_begin_compound_stmt (true);
12737 tree ret = c_parser_omp_teams (loc, parser, p_name,
12738 OMP_TARGET_CLAUSE_MASK, cclauses);
12739 block = c_end_compound_stmt (loc, block, true);
12740 if (ret == NULL_TREE)
12741 return false;
12742 tree stmt = make_node (OMP_TARGET);
12743 TREE_TYPE (stmt) = void_type_node;
12744 OMP_TARGET_CLAUSES (stmt) = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
12745 OMP_TARGET_BODY (stmt) = block;
12746 add_stmt (stmt);
12747 return true;
12749 else if (!flag_openmp) /* flag_openmp_simd */
12751 c_parser_skip_to_pragma_eol (parser);
12752 return false;
12754 else if (strcmp (p, "data") == 0)
12756 c_parser_consume_token (parser);
12757 c_parser_omp_target_data (loc, parser);
12758 return true;
12760 else if (strcmp (p, "update") == 0)
12762 c_parser_consume_token (parser);
12763 return c_parser_omp_target_update (loc, parser, context);
12767 tree stmt = make_node (OMP_TARGET);
12768 TREE_TYPE (stmt) = void_type_node;
12770 OMP_TARGET_CLAUSES (stmt)
12771 = c_parser_omp_all_clauses (parser, OMP_TARGET_CLAUSE_MASK,
12772 "#pragma omp target");
12773 keep_next_level ();
12774 tree block = c_begin_compound_stmt (true);
12775 add_stmt (c_parser_omp_structured_block (parser));
12776 OMP_TARGET_BODY (stmt) = c_end_compound_stmt (loc, block, true);
12778 SET_EXPR_LOCATION (stmt, loc);
12779 add_stmt (stmt);
12780 return true;
12783 /* OpenMP 4.0:
12784 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
12786 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
12787 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
12788 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
12789 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
12790 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
12791 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
12792 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
12794 static void
12795 c_parser_omp_declare_simd (c_parser *parser, enum pragma_context context)
12797 vec<c_token> clauses = vNULL;
12798 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
12800 c_token *token = c_parser_peek_token (parser);
12801 if (token->type == CPP_EOF)
12803 c_parser_skip_to_pragma_eol (parser);
12804 clauses.release ();
12805 return;
12807 clauses.safe_push (*token);
12808 c_parser_consume_token (parser);
12810 clauses.safe_push (*c_parser_peek_token (parser));
12811 c_parser_skip_to_pragma_eol (parser);
12813 while (c_parser_next_token_is (parser, CPP_PRAGMA))
12815 if (c_parser_peek_token (parser)->pragma_kind
12816 != PRAGMA_OMP_DECLARE_REDUCTION
12817 || c_parser_peek_2nd_token (parser)->type != CPP_NAME
12818 || strcmp (IDENTIFIER_POINTER
12819 (c_parser_peek_2nd_token (parser)->value),
12820 "simd") != 0)
12822 c_parser_error (parser,
12823 "%<#pragma omp declare simd%> must be followed by "
12824 "function declaration or definition or another "
12825 "%<#pragma omp declare simd%>");
12826 clauses.release ();
12827 return;
12829 c_parser_consume_pragma (parser);
12830 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
12832 c_token *token = c_parser_peek_token (parser);
12833 if (token->type == CPP_EOF)
12835 c_parser_skip_to_pragma_eol (parser);
12836 clauses.release ();
12837 return;
12839 clauses.safe_push (*token);
12840 c_parser_consume_token (parser);
12842 clauses.safe_push (*c_parser_peek_token (parser));
12843 c_parser_skip_to_pragma_eol (parser);
12846 /* Make sure nothing tries to read past the end of the tokens. */
12847 c_token eof_token;
12848 memset (&eof_token, 0, sizeof (eof_token));
12849 eof_token.type = CPP_EOF;
12850 clauses.safe_push (eof_token);
12851 clauses.safe_push (eof_token);
12853 switch (context)
12855 case pragma_external:
12856 if (c_parser_next_token_is (parser, CPP_KEYWORD)
12857 && c_parser_peek_token (parser)->keyword == RID_EXTENSION)
12859 int ext = disable_extension_diagnostics ();
12861 c_parser_consume_token (parser);
12862 while (c_parser_next_token_is (parser, CPP_KEYWORD)
12863 && c_parser_peek_token (parser)->keyword == RID_EXTENSION);
12864 c_parser_declaration_or_fndef (parser, true, true, true, false, true,
12865 NULL, clauses);
12866 restore_extension_diagnostics (ext);
12868 else
12869 c_parser_declaration_or_fndef (parser, true, true, true, false, true,
12870 NULL, clauses);
12871 break;
12872 case pragma_struct:
12873 case pragma_param:
12874 c_parser_error (parser, "%<#pragma omp declare simd%> must be followed by "
12875 "function declaration or definition");
12876 break;
12877 case pragma_compound:
12878 case pragma_stmt:
12879 if (c_parser_next_token_is (parser, CPP_KEYWORD)
12880 && c_parser_peek_token (parser)->keyword == RID_EXTENSION)
12882 int ext = disable_extension_diagnostics ();
12884 c_parser_consume_token (parser);
12885 while (c_parser_next_token_is (parser, CPP_KEYWORD)
12886 && c_parser_peek_token (parser)->keyword == RID_EXTENSION);
12887 if (c_parser_next_tokens_start_declaration (parser))
12889 c_parser_declaration_or_fndef (parser, true, true, true, true,
12890 true, NULL, clauses);
12891 restore_extension_diagnostics (ext);
12892 break;
12894 restore_extension_diagnostics (ext);
12896 else if (c_parser_next_tokens_start_declaration (parser))
12898 c_parser_declaration_or_fndef (parser, true, true, true, true, true,
12899 NULL, clauses);
12900 break;
12902 c_parser_error (parser, "%<#pragma omp declare simd%> must be followed by "
12903 "function declaration or definition");
12904 break;
12905 default:
12906 gcc_unreachable ();
12908 clauses.release ();
12911 /* Finalize #pragma omp declare simd clauses after FNDECL has been parsed,
12912 and put that into "omp declare simd" attribute. */
12914 static void
12915 c_finish_omp_declare_simd (c_parser *parser, tree fndecl, tree parms,
12916 vec<c_token> clauses)
12918 if (flag_cilkplus
12919 && clauses.exists () && !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
12921 error ("%<#pragma omp declare simd%> cannot be used in the same "
12922 "function marked as a Cilk Plus SIMD-enabled function");
12923 vec_free (parser->cilk_simd_fn_tokens);
12924 return;
12927 /* Normally first token is CPP_NAME "simd". CPP_EOF there indicates
12928 error has been reported and CPP_PRAGMA that c_finish_omp_declare_simd
12929 has already processed the tokens. */
12930 if (clauses.exists () && clauses[0].type == CPP_EOF)
12931 return;
12932 if (fndecl == NULL_TREE || TREE_CODE (fndecl) != FUNCTION_DECL)
12934 error ("%<#pragma omp declare simd%> not immediately followed by "
12935 "a function declaration or definition");
12936 clauses[0].type = CPP_EOF;
12937 return;
12939 if (clauses.exists () && clauses[0].type != CPP_NAME)
12941 error_at (DECL_SOURCE_LOCATION (fndecl),
12942 "%<#pragma omp declare simd%> not immediately followed by "
12943 "a single function declaration or definition");
12944 clauses[0].type = CPP_EOF;
12945 return;
12948 if (parms == NULL_TREE)
12949 parms = DECL_ARGUMENTS (fndecl);
12951 unsigned int tokens_avail = parser->tokens_avail;
12952 gcc_assert (parser->tokens == &parser->tokens_buf[0]);
12953 bool is_cilkplus_cilk_simd_fn = false;
12955 if (flag_cilkplus && !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
12957 parser->tokens = parser->cilk_simd_fn_tokens->address ();
12958 parser->tokens_avail = vec_safe_length (parser->cilk_simd_fn_tokens);
12959 is_cilkplus_cilk_simd_fn = true;
12961 else
12963 parser->tokens = clauses.address ();
12964 parser->tokens_avail = clauses.length ();
12967 /* c_parser_omp_declare_simd pushed 2 extra CPP_EOF tokens at the end. */
12968 while (parser->tokens_avail > 3)
12970 c_token *token = c_parser_peek_token (parser);
12971 if (!is_cilkplus_cilk_simd_fn)
12972 gcc_assert (token->type == CPP_NAME
12973 && strcmp (IDENTIFIER_POINTER (token->value), "simd") == 0);
12974 else
12975 gcc_assert (token->type == CPP_NAME
12976 && is_cilkplus_vector_p (token->value));
12977 c_parser_consume_token (parser);
12978 parser->in_pragma = true;
12980 tree c = NULL_TREE;
12981 if (is_cilkplus_cilk_simd_fn)
12982 c = c_parser_omp_all_clauses (parser, CILK_SIMD_FN_CLAUSE_MASK,
12983 "SIMD-enabled functions attribute");
12984 else
12985 c = c_parser_omp_all_clauses (parser, OMP_DECLARE_SIMD_CLAUSE_MASK,
12986 "#pragma omp declare simd");
12987 c = c_omp_declare_simd_clauses_to_numbers (parms, c);
12988 if (c != NULL_TREE)
12989 c = tree_cons (NULL_TREE, c, NULL_TREE);
12990 if (is_cilkplus_cilk_simd_fn)
12992 tree k = build_tree_list (get_identifier ("cilk simd function"),
12993 NULL_TREE);
12994 TREE_CHAIN (k) = DECL_ATTRIBUTES (fndecl);
12995 DECL_ATTRIBUTES (fndecl) = k;
12997 c = build_tree_list (get_identifier ("omp declare simd"), c);
12998 TREE_CHAIN (c) = DECL_ATTRIBUTES (fndecl);
12999 DECL_ATTRIBUTES (fndecl) = c;
13002 parser->tokens = &parser->tokens_buf[0];
13003 parser->tokens_avail = tokens_avail;
13004 if (clauses.exists ())
13005 clauses[0].type = CPP_PRAGMA;
13007 if (!vec_safe_is_empty (parser->cilk_simd_fn_tokens))
13008 vec_free (parser->cilk_simd_fn_tokens);
13012 /* OpenMP 4.0:
13013 # pragma omp declare target new-line
13014 declarations and definitions
13015 # pragma omp end declare target new-line */
13017 static void
13018 c_parser_omp_declare_target (c_parser *parser)
13020 c_parser_skip_to_pragma_eol (parser);
13021 current_omp_declare_target_attribute++;
13024 static void
13025 c_parser_omp_end_declare_target (c_parser *parser)
13027 location_t loc = c_parser_peek_token (parser)->location;
13028 c_parser_consume_pragma (parser);
13029 if (c_parser_next_token_is (parser, CPP_NAME)
13030 && strcmp (IDENTIFIER_POINTER (c_parser_peek_token (parser)->value),
13031 "declare") == 0)
13033 c_parser_consume_token (parser);
13034 if (c_parser_next_token_is (parser, CPP_NAME)
13035 && strcmp (IDENTIFIER_POINTER (c_parser_peek_token (parser)->value),
13036 "target") == 0)
13037 c_parser_consume_token (parser);
13038 else
13040 c_parser_error (parser, "expected %<target%>");
13041 c_parser_skip_to_pragma_eol (parser);
13042 return;
13045 else
13047 c_parser_error (parser, "expected %<declare%>");
13048 c_parser_skip_to_pragma_eol (parser);
13049 return;
13051 c_parser_skip_to_pragma_eol (parser);
13052 if (!current_omp_declare_target_attribute)
13053 error_at (loc, "%<#pragma omp end declare target%> without corresponding "
13054 "%<#pragma omp declare target%>");
13055 else
13056 current_omp_declare_target_attribute--;
13060 /* OpenMP 4.0
13061 #pragma omp declare reduction (reduction-id : typename-list : expression) \
13062 initializer-clause[opt] new-line
13064 initializer-clause:
13065 initializer (omp_priv = initializer)
13066 initializer (function-name (argument-list)) */
13068 static void
13069 c_parser_omp_declare_reduction (c_parser *parser, enum pragma_context context)
13071 unsigned int tokens_avail = 0, i;
13072 vec<tree> types = vNULL;
13073 vec<c_token> clauses = vNULL;
13074 enum tree_code reduc_code = ERROR_MARK;
13075 tree reduc_id = NULL_TREE;
13076 tree type;
13077 location_t rloc = c_parser_peek_token (parser)->location;
13079 if (context == pragma_struct || context == pragma_param)
13081 error ("%<#pragma omp declare reduction%> not at file or block scope");
13082 goto fail;
13085 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
13086 goto fail;
13088 switch (c_parser_peek_token (parser)->type)
13090 case CPP_PLUS:
13091 reduc_code = PLUS_EXPR;
13092 break;
13093 case CPP_MULT:
13094 reduc_code = MULT_EXPR;
13095 break;
13096 case CPP_MINUS:
13097 reduc_code = MINUS_EXPR;
13098 break;
13099 case CPP_AND:
13100 reduc_code = BIT_AND_EXPR;
13101 break;
13102 case CPP_XOR:
13103 reduc_code = BIT_XOR_EXPR;
13104 break;
13105 case CPP_OR:
13106 reduc_code = BIT_IOR_EXPR;
13107 break;
13108 case CPP_AND_AND:
13109 reduc_code = TRUTH_ANDIF_EXPR;
13110 break;
13111 case CPP_OR_OR:
13112 reduc_code = TRUTH_ORIF_EXPR;
13113 break;
13114 case CPP_NAME:
13115 const char *p;
13116 p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
13117 if (strcmp (p, "min") == 0)
13119 reduc_code = MIN_EXPR;
13120 break;
13122 if (strcmp (p, "max") == 0)
13124 reduc_code = MAX_EXPR;
13125 break;
13127 reduc_id = c_parser_peek_token (parser)->value;
13128 break;
13129 default:
13130 c_parser_error (parser,
13131 "expected %<+%>, %<*%>, %<-%>, %<&%>, "
13132 "%<^%>, %<|%>, %<&&%>, %<||%>, %<min%> or identifier");
13133 goto fail;
13136 tree orig_reduc_id, reduc_decl;
13137 orig_reduc_id = reduc_id;
13138 reduc_id = c_omp_reduction_id (reduc_code, reduc_id);
13139 reduc_decl = c_omp_reduction_decl (reduc_id);
13140 c_parser_consume_token (parser);
13142 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
13143 goto fail;
13145 while (true)
13147 location_t loc = c_parser_peek_token (parser)->location;
13148 struct c_type_name *ctype = c_parser_type_name (parser);
13149 if (ctype != NULL)
13151 type = groktypename (ctype, NULL, NULL);
13152 if (type == error_mark_node)
13154 else if ((INTEGRAL_TYPE_P (type)
13155 || TREE_CODE (type) == REAL_TYPE
13156 || TREE_CODE (type) == COMPLEX_TYPE)
13157 && orig_reduc_id == NULL_TREE)
13158 error_at (loc, "predeclared arithmetic type in "
13159 "%<#pragma omp declare reduction%>");
13160 else if (TREE_CODE (type) == FUNCTION_TYPE
13161 || TREE_CODE (type) == ARRAY_TYPE)
13162 error_at (loc, "function or array type in "
13163 "%<#pragma omp declare reduction%>");
13164 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
13165 error_at (loc, "const, volatile or restrict qualified type in "
13166 "%<#pragma omp declare reduction%>");
13167 else
13169 tree t;
13170 for (t = DECL_INITIAL (reduc_decl); t; t = TREE_CHAIN (t))
13171 if (comptypes (TREE_PURPOSE (t), type))
13173 error_at (loc, "redeclaration of %qs "
13174 "%<#pragma omp declare reduction%> for "
13175 "type %qT",
13176 IDENTIFIER_POINTER (reduc_id)
13177 + sizeof ("omp declare reduction ") - 1,
13178 type);
13179 location_t ploc
13180 = DECL_SOURCE_LOCATION (TREE_VEC_ELT (TREE_VALUE (t),
13181 0));
13182 error_at (ploc, "previous %<#pragma omp declare "
13183 "reduction%>");
13184 break;
13186 if (t == NULL_TREE)
13187 types.safe_push (type);
13189 if (c_parser_next_token_is (parser, CPP_COMMA))
13190 c_parser_consume_token (parser);
13191 else
13192 break;
13194 else
13195 break;
13198 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>")
13199 || types.is_empty ())
13201 fail:
13202 clauses.release ();
13203 types.release ();
13204 while (true)
13206 c_token *token = c_parser_peek_token (parser);
13207 if (token->type == CPP_EOF || token->type == CPP_PRAGMA_EOL)
13208 break;
13209 c_parser_consume_token (parser);
13211 c_parser_skip_to_pragma_eol (parser);
13212 return;
13215 if (types.length () > 1)
13217 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
13219 c_token *token = c_parser_peek_token (parser);
13220 if (token->type == CPP_EOF)
13221 goto fail;
13222 clauses.safe_push (*token);
13223 c_parser_consume_token (parser);
13225 clauses.safe_push (*c_parser_peek_token (parser));
13226 c_parser_skip_to_pragma_eol (parser);
13228 /* Make sure nothing tries to read past the end of the tokens. */
13229 c_token eof_token;
13230 memset (&eof_token, 0, sizeof (eof_token));
13231 eof_token.type = CPP_EOF;
13232 clauses.safe_push (eof_token);
13233 clauses.safe_push (eof_token);
13236 int errs = errorcount;
13237 FOR_EACH_VEC_ELT (types, i, type)
13239 tokens_avail = parser->tokens_avail;
13240 gcc_assert (parser->tokens == &parser->tokens_buf[0]);
13241 if (!clauses.is_empty ())
13243 parser->tokens = clauses.address ();
13244 parser->tokens_avail = clauses.length ();
13245 parser->in_pragma = true;
13248 bool nested = current_function_decl != NULL_TREE;
13249 if (nested)
13250 c_push_function_context ();
13251 tree fndecl = build_decl (BUILTINS_LOCATION, FUNCTION_DECL,
13252 reduc_id, default_function_type);
13253 current_function_decl = fndecl;
13254 allocate_struct_function (fndecl, true);
13255 push_scope ();
13256 tree stmt = push_stmt_list ();
13257 /* Intentionally BUILTINS_LOCATION, so that -Wshadow doesn't
13258 warn about these. */
13259 tree omp_out = build_decl (BUILTINS_LOCATION, VAR_DECL,
13260 get_identifier ("omp_out"), type);
13261 DECL_ARTIFICIAL (omp_out) = 1;
13262 DECL_CONTEXT (omp_out) = fndecl;
13263 pushdecl (omp_out);
13264 tree omp_in = build_decl (BUILTINS_LOCATION, VAR_DECL,
13265 get_identifier ("omp_in"), type);
13266 DECL_ARTIFICIAL (omp_in) = 1;
13267 DECL_CONTEXT (omp_in) = fndecl;
13268 pushdecl (omp_in);
13269 struct c_expr combiner = c_parser_expression (parser);
13270 struct c_expr initializer;
13271 tree omp_priv = NULL_TREE, omp_orig = NULL_TREE;
13272 bool bad = false;
13273 initializer.value = error_mark_node;
13274 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
13275 bad = true;
13276 else if (c_parser_next_token_is (parser, CPP_NAME)
13277 && strcmp (IDENTIFIER_POINTER
13278 (c_parser_peek_token (parser)->value),
13279 "initializer") == 0)
13281 c_parser_consume_token (parser);
13282 pop_scope ();
13283 push_scope ();
13284 omp_priv = build_decl (BUILTINS_LOCATION, VAR_DECL,
13285 get_identifier ("omp_priv"), type);
13286 DECL_ARTIFICIAL (omp_priv) = 1;
13287 DECL_INITIAL (omp_priv) = error_mark_node;
13288 DECL_CONTEXT (omp_priv) = fndecl;
13289 pushdecl (omp_priv);
13290 omp_orig = build_decl (BUILTINS_LOCATION, VAR_DECL,
13291 get_identifier ("omp_orig"), type);
13292 DECL_ARTIFICIAL (omp_orig) = 1;
13293 DECL_CONTEXT (omp_orig) = fndecl;
13294 pushdecl (omp_orig);
13295 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
13296 bad = true;
13297 else if (!c_parser_next_token_is (parser, CPP_NAME))
13299 c_parser_error (parser, "expected %<omp_priv%> or "
13300 "function-name");
13301 bad = true;
13303 else if (strcmp (IDENTIFIER_POINTER
13304 (c_parser_peek_token (parser)->value),
13305 "omp_priv") != 0)
13307 if (c_parser_peek_2nd_token (parser)->type != CPP_OPEN_PAREN
13308 || c_parser_peek_token (parser)->id_kind != C_ID_ID)
13310 c_parser_error (parser, "expected function-name %<(%>");
13311 bad = true;
13313 else
13314 initializer = c_parser_postfix_expression (parser);
13315 if (initializer.value
13316 && TREE_CODE (initializer.value) == CALL_EXPR)
13318 int j;
13319 tree c = initializer.value;
13320 for (j = 0; j < call_expr_nargs (c); j++)
13321 if (TREE_CODE (CALL_EXPR_ARG (c, j)) == ADDR_EXPR
13322 && TREE_OPERAND (CALL_EXPR_ARG (c, j), 0) == omp_priv)
13323 break;
13324 if (j == call_expr_nargs (c))
13325 error ("one of the initializer call arguments should be "
13326 "%<&omp_priv%>");
13329 else
13331 c_parser_consume_token (parser);
13332 if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
13333 bad = true;
13334 else
13336 tree st = push_stmt_list ();
13337 start_init (omp_priv, NULL_TREE, 0);
13338 location_t loc = c_parser_peek_token (parser)->location;
13339 struct c_expr init = c_parser_initializer (parser);
13340 finish_init ();
13341 finish_decl (omp_priv, loc, init.value,
13342 init.original_type, NULL_TREE);
13343 pop_stmt_list (st);
13346 if (!bad
13347 && !c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
13348 bad = true;
13351 if (!bad)
13353 c_parser_skip_to_pragma_eol (parser);
13355 tree t = tree_cons (type, make_tree_vec (omp_priv ? 6 : 3),
13356 DECL_INITIAL (reduc_decl));
13357 DECL_INITIAL (reduc_decl) = t;
13358 DECL_SOURCE_LOCATION (omp_out) = rloc;
13359 TREE_VEC_ELT (TREE_VALUE (t), 0) = omp_out;
13360 TREE_VEC_ELT (TREE_VALUE (t), 1) = omp_in;
13361 TREE_VEC_ELT (TREE_VALUE (t), 2) = combiner.value;
13362 walk_tree (&combiner.value, c_check_omp_declare_reduction_r,
13363 &TREE_VEC_ELT (TREE_VALUE (t), 0), NULL);
13364 if (omp_priv)
13366 DECL_SOURCE_LOCATION (omp_priv) = rloc;
13367 TREE_VEC_ELT (TREE_VALUE (t), 3) = omp_priv;
13368 TREE_VEC_ELT (TREE_VALUE (t), 4) = omp_orig;
13369 TREE_VEC_ELT (TREE_VALUE (t), 5) = initializer.value;
13370 walk_tree (&initializer.value, c_check_omp_declare_reduction_r,
13371 &TREE_VEC_ELT (TREE_VALUE (t), 3), NULL);
13372 walk_tree (&DECL_INITIAL (omp_priv),
13373 c_check_omp_declare_reduction_r,
13374 &TREE_VEC_ELT (TREE_VALUE (t), 3), NULL);
13378 pop_stmt_list (stmt);
13379 pop_scope ();
13380 if (cfun->language != NULL)
13382 ggc_free (cfun->language);
13383 cfun->language = NULL;
13385 set_cfun (NULL);
13386 current_function_decl = NULL_TREE;
13387 if (nested)
13388 c_pop_function_context ();
13390 if (!clauses.is_empty ())
13392 parser->tokens = &parser->tokens_buf[0];
13393 parser->tokens_avail = tokens_avail;
13395 if (bad)
13396 goto fail;
13397 if (errs != errorcount)
13398 break;
13401 clauses.release ();
13402 types.release ();
13406 /* OpenMP 4.0
13407 #pragma omp declare simd declare-simd-clauses[optseq] new-line
13408 #pragma omp declare reduction (reduction-id : typename-list : expression) \
13409 initializer-clause[opt] new-line
13410 #pragma omp declare target new-line */
13412 static void
13413 c_parser_omp_declare (c_parser *parser, enum pragma_context context)
13415 c_parser_consume_pragma (parser);
13416 if (c_parser_next_token_is (parser, CPP_NAME))
13418 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
13419 if (strcmp (p, "simd") == 0)
13421 /* c_parser_consume_token (parser); done in
13422 c_parser_omp_declare_simd. */
13423 c_parser_omp_declare_simd (parser, context);
13424 return;
13426 if (strcmp (p, "reduction") == 0)
13428 c_parser_consume_token (parser);
13429 c_parser_omp_declare_reduction (parser, context);
13430 return;
13432 if (!flag_openmp) /* flag_openmp_simd */
13434 c_parser_skip_to_pragma_eol (parser);
13435 return;
13437 if (strcmp (p, "target") == 0)
13439 c_parser_consume_token (parser);
13440 c_parser_omp_declare_target (parser);
13441 return;
13445 c_parser_error (parser, "expected %<simd%> or %<reduction%> "
13446 "or %<target%>");
13447 c_parser_skip_to_pragma_eol (parser);
13450 /* Main entry point to parsing most OpenMP pragmas. */
13452 static void
13453 c_parser_omp_construct (c_parser *parser)
13455 enum pragma_kind p_kind;
13456 location_t loc;
13457 tree stmt;
13458 char p_name[sizeof "#pragma omp teams distribute parallel for simd"];
13459 omp_clause_mask mask (0);
13461 loc = c_parser_peek_token (parser)->location;
13462 p_kind = c_parser_peek_token (parser)->pragma_kind;
13463 c_parser_consume_pragma (parser);
13465 switch (p_kind)
13467 case PRAGMA_OMP_ATOMIC:
13468 c_parser_omp_atomic (loc, parser);
13469 return;
13470 case PRAGMA_OMP_CRITICAL:
13471 stmt = c_parser_omp_critical (loc, parser);
13472 break;
13473 case PRAGMA_OMP_DISTRIBUTE:
13474 strcpy (p_name, "#pragma omp");
13475 stmt = c_parser_omp_distribute (loc, parser, p_name, mask, NULL);
13476 break;
13477 case PRAGMA_OMP_FOR:
13478 strcpy (p_name, "#pragma omp");
13479 stmt = c_parser_omp_for (loc, parser, p_name, mask, NULL);
13480 break;
13481 case PRAGMA_OMP_MASTER:
13482 stmt = c_parser_omp_master (loc, parser);
13483 break;
13484 case PRAGMA_OMP_ORDERED:
13485 stmt = c_parser_omp_ordered (loc, parser);
13486 break;
13487 case PRAGMA_OMP_PARALLEL:
13488 strcpy (p_name, "#pragma omp");
13489 stmt = c_parser_omp_parallel (loc, parser, p_name, mask, NULL);
13490 break;
13491 case PRAGMA_OMP_SECTIONS:
13492 strcpy (p_name, "#pragma omp");
13493 stmt = c_parser_omp_sections (loc, parser, p_name, mask, NULL);
13494 break;
13495 case PRAGMA_OMP_SIMD:
13496 strcpy (p_name, "#pragma omp");
13497 stmt = c_parser_omp_simd (loc, parser, p_name, mask, NULL);
13498 break;
13499 case PRAGMA_OMP_SINGLE:
13500 stmt = c_parser_omp_single (loc, parser);
13501 break;
13502 case PRAGMA_OMP_TASK:
13503 stmt = c_parser_omp_task (loc, parser);
13504 break;
13505 case PRAGMA_OMP_TASKGROUP:
13506 stmt = c_parser_omp_taskgroup (parser);
13507 break;
13508 case PRAGMA_OMP_TEAMS:
13509 strcpy (p_name, "#pragma omp");
13510 stmt = c_parser_omp_teams (loc, parser, p_name, mask, NULL);
13511 break;
13512 default:
13513 gcc_unreachable ();
13516 if (stmt)
13517 gcc_assert (EXPR_LOCATION (stmt) != UNKNOWN_LOCATION);
13521 /* OpenMP 2.5:
13522 # pragma omp threadprivate (variable-list) */
13524 static void
13525 c_parser_omp_threadprivate (c_parser *parser)
13527 tree vars, t;
13528 location_t loc;
13530 c_parser_consume_pragma (parser);
13531 loc = c_parser_peek_token (parser)->location;
13532 vars = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_ERROR, NULL);
13534 /* Mark every variable in VARS to be assigned thread local storage. */
13535 for (t = vars; t; t = TREE_CHAIN (t))
13537 tree v = TREE_PURPOSE (t);
13539 /* FIXME diagnostics: Ideally we should keep individual
13540 locations for all the variables in the var list to make the
13541 following errors more precise. Perhaps
13542 c_parser_omp_var_list_parens() should construct a list of
13543 locations to go along with the var list. */
13545 /* If V had already been marked threadprivate, it doesn't matter
13546 whether it had been used prior to this point. */
13547 if (TREE_CODE (v) != VAR_DECL)
13548 error_at (loc, "%qD is not a variable", v);
13549 else if (TREE_USED (v) && !C_DECL_THREADPRIVATE_P (v))
13550 error_at (loc, "%qE declared %<threadprivate%> after first use", v);
13551 else if (! TREE_STATIC (v) && ! DECL_EXTERNAL (v))
13552 error_at (loc, "automatic variable %qE cannot be %<threadprivate%>", v);
13553 else if (TREE_TYPE (v) == error_mark_node)
13555 else if (! COMPLETE_TYPE_P (TREE_TYPE (v)))
13556 error_at (loc, "%<threadprivate%> %qE has incomplete type", v);
13557 else
13559 if (! DECL_THREAD_LOCAL_P (v))
13561 set_decl_tls_model (v, decl_default_tls_model (v));
13562 /* If rtl has been already set for this var, call
13563 make_decl_rtl once again, so that encode_section_info
13564 has a chance to look at the new decl flags. */
13565 if (DECL_RTL_SET_P (v))
13566 make_decl_rtl (v);
13568 C_DECL_THREADPRIVATE_P (v) = 1;
13572 c_parser_skip_to_pragma_eol (parser);
13575 /* Cilk Plus <#pragma simd> parsing routines. */
13577 /* Helper function for c_parser_pragma. Perform some sanity checking
13578 for <#pragma simd> constructs. Returns FALSE if there was a
13579 problem. */
13581 static bool
13582 c_parser_cilk_verify_simd (c_parser *parser,
13583 enum pragma_context context)
13585 if (!flag_cilkplus)
13587 warning (0, "pragma simd ignored because -fcilkplus is not enabled");
13588 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
13589 return false;
13591 if (context == pragma_external)
13593 c_parser_error (parser,"pragma simd must be inside a function");
13594 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
13595 return false;
13597 return true;
13600 /* Cilk Plus:
13601 This function is shared by SIMD-enabled functions and #pragma simd.
13602 If IS_SIMD_FN is true then it is parsing a SIMD-enabled function and
13603 CLAUSES is unused. The main purpose of this function is to parse a
13604 vectorlength attribute or clause and check for parse errors.
13605 When IS_SIMD_FN is true then the function is merely caching the tokens
13606 in PARSER->CILK_SIMD_FN_TOKENS. If errors are found then the token
13607 cache is cleared since there is no reason to continue.
13608 Syntax:
13609 vectorlength ( constant-expression ) */
13611 static tree
13612 c_parser_cilk_clause_vectorlength (c_parser *parser, tree clauses,
13613 bool is_simd_fn)
13615 if (is_simd_fn)
13616 check_no_duplicate_clause (clauses, OMP_CLAUSE_SIMDLEN, "vectorlength");
13617 else
13618 /* The vectorlength clause behaves exactly like OpenMP's safelen
13619 clause. Represent it in OpenMP terms. */
13620 check_no_duplicate_clause (clauses, OMP_CLAUSE_SAFELEN, "vectorlength");
13622 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
13623 return clauses;
13625 location_t loc = c_parser_peek_token (parser)->location;
13626 tree expr = c_parser_expr_no_commas (parser, NULL).value;
13627 expr = c_fully_fold (expr, false, NULL);
13629 /* If expr is an error_mark_node then the above function would have
13630 emitted an error. No reason to do it twice. */
13631 if (expr == error_mark_node)
13633 else if (!TREE_TYPE (expr)
13634 || !TREE_CONSTANT (expr)
13635 || !INTEGRAL_TYPE_P (TREE_TYPE (expr)))
13637 error_at (loc, "vectorlength must be an integer constant");
13638 else if (wi::exact_log2 (expr) == -1)
13639 error_at (loc, "vectorlength must be a power of 2");
13640 else
13642 if (is_simd_fn)
13644 tree u = build_omp_clause (loc, OMP_CLAUSE_SIMDLEN);
13645 OMP_CLAUSE_SIMDLEN_EXPR (u) = expr;
13646 OMP_CLAUSE_CHAIN (u) = clauses;
13647 clauses = u;
13649 else
13651 tree u = build_omp_clause (loc, OMP_CLAUSE_SAFELEN);
13652 OMP_CLAUSE_SAFELEN_EXPR (u) = expr;
13653 OMP_CLAUSE_CHAIN (u) = clauses;
13654 clauses = u;
13658 c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
13660 return clauses;
13663 /* Cilk Plus:
13664 linear ( simd-linear-variable-list )
13666 simd-linear-variable-list:
13667 simd-linear-variable
13668 simd-linear-variable-list , simd-linear-variable
13670 simd-linear-variable:
13671 id-expression
13672 id-expression : simd-linear-step
13674 simd-linear-step:
13675 conditional-expression */
13677 static tree
13678 c_parser_cilk_clause_linear (c_parser *parser, tree clauses)
13680 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
13681 return clauses;
13683 location_t loc = c_parser_peek_token (parser)->location;
13685 if (c_parser_next_token_is_not (parser, CPP_NAME)
13686 || c_parser_peek_token (parser)->id_kind != C_ID_ID)
13687 c_parser_error (parser, "expected identifier");
13689 while (c_parser_next_token_is (parser, CPP_NAME)
13690 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
13692 tree var = lookup_name (c_parser_peek_token (parser)->value);
13694 if (var == NULL)
13696 undeclared_variable (c_parser_peek_token (parser)->location,
13697 c_parser_peek_token (parser)->value);
13698 c_parser_consume_token (parser);
13700 else if (var == error_mark_node)
13701 c_parser_consume_token (parser);
13702 else
13704 tree step = integer_one_node;
13706 /* Parse the linear step if present. */
13707 if (c_parser_peek_2nd_token (parser)->type == CPP_COLON)
13709 c_parser_consume_token (parser);
13710 c_parser_consume_token (parser);
13712 tree expr = c_parser_expr_no_commas (parser, NULL).value;
13713 expr = c_fully_fold (expr, false, NULL);
13715 if (TREE_TYPE (expr)
13716 && INTEGRAL_TYPE_P (TREE_TYPE (expr))
13717 && (TREE_CONSTANT (expr)
13718 || DECL_P (expr)))
13719 step = expr;
13720 else
13721 c_parser_error (parser,
13722 "step size must be an integer constant "
13723 "expression or an integer variable");
13725 else
13726 c_parser_consume_token (parser);
13728 /* Use OMP_CLAUSE_LINEAR, which has the same semantics. */
13729 tree u = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
13730 OMP_CLAUSE_DECL (u) = var;
13731 OMP_CLAUSE_LINEAR_STEP (u) = step;
13732 OMP_CLAUSE_CHAIN (u) = clauses;
13733 clauses = u;
13736 if (c_parser_next_token_is_not (parser, CPP_COMMA))
13737 break;
13739 c_parser_consume_token (parser);
13742 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
13744 return clauses;
13747 /* Returns the name of the next clause. If the clause is not
13748 recognized SIMD_OMP_CLAUSE_NONE is returned and the next token is
13749 not consumed. Otherwise, the appropriate pragma_simd_clause is
13750 returned and the token is consumed. */
13752 static pragma_omp_clause
13753 c_parser_cilk_clause_name (c_parser *parser)
13755 pragma_omp_clause result;
13756 c_token *token = c_parser_peek_token (parser);
13758 if (!token->value || token->type != CPP_NAME)
13759 return PRAGMA_CILK_CLAUSE_NONE;
13761 const char *p = IDENTIFIER_POINTER (token->value);
13763 if (!strcmp (p, "vectorlength"))
13764 result = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
13765 else if (!strcmp (p, "linear"))
13766 result = PRAGMA_CILK_CLAUSE_LINEAR;
13767 else if (!strcmp (p, "private"))
13768 result = PRAGMA_CILK_CLAUSE_PRIVATE;
13769 else if (!strcmp (p, "firstprivate"))
13770 result = PRAGMA_CILK_CLAUSE_FIRSTPRIVATE;
13771 else if (!strcmp (p, "lastprivate"))
13772 result = PRAGMA_CILK_CLAUSE_LASTPRIVATE;
13773 else if (!strcmp (p, "reduction"))
13774 result = PRAGMA_CILK_CLAUSE_REDUCTION;
13775 else
13776 return PRAGMA_CILK_CLAUSE_NONE;
13778 c_parser_consume_token (parser);
13779 return result;
13782 /* Parse all #<pragma simd> clauses. Return the list of clauses
13783 found. */
13785 static tree
13786 c_parser_cilk_all_clauses (c_parser *parser)
13788 tree clauses = NULL;
13790 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
13792 pragma_omp_clause c_kind;
13794 c_kind = c_parser_cilk_clause_name (parser);
13796 switch (c_kind)
13798 case PRAGMA_CILK_CLAUSE_VECTORLENGTH:
13799 clauses = c_parser_cilk_clause_vectorlength (parser, clauses, false);
13800 break;
13801 case PRAGMA_CILK_CLAUSE_LINEAR:
13802 clauses = c_parser_cilk_clause_linear (parser, clauses);
13803 break;
13804 case PRAGMA_CILK_CLAUSE_PRIVATE:
13805 /* Use the OpenMP counterpart. */
13806 clauses = c_parser_omp_clause_private (parser, clauses);
13807 break;
13808 case PRAGMA_CILK_CLAUSE_FIRSTPRIVATE:
13809 /* Use the OpenMP counterpart. */
13810 clauses = c_parser_omp_clause_firstprivate (parser, clauses);
13811 break;
13812 case PRAGMA_CILK_CLAUSE_LASTPRIVATE:
13813 /* Use the OpenMP counterpart. */
13814 clauses = c_parser_omp_clause_lastprivate (parser, clauses);
13815 break;
13816 case PRAGMA_CILK_CLAUSE_REDUCTION:
13817 /* Use the OpenMP counterpart. */
13818 clauses = c_parser_omp_clause_reduction (parser, clauses);
13819 break;
13820 default:
13821 c_parser_error (parser, "expected %<#pragma simd%> clause");
13822 goto saw_error;
13826 saw_error:
13827 c_parser_skip_to_pragma_eol (parser);
13828 return c_finish_cilk_clauses (clauses);
13831 /* Main entry point for parsing Cilk Plus <#pragma simd> for
13832 loops. */
13834 static void
13835 c_parser_cilk_simd (c_parser *parser)
13837 tree clauses = c_parser_cilk_all_clauses (parser);
13838 tree block = c_begin_compound_stmt (true);
13839 location_t loc = c_parser_peek_token (parser)->location;
13840 c_parser_omp_for_loop (loc, parser, CILK_SIMD, clauses, NULL);
13841 block = c_end_compound_stmt (loc, block, true);
13842 add_stmt (block);
13845 /* Parse a transaction attribute (GCC Extension).
13847 transaction-attribute:
13848 attributes
13849 [ [ any-word ] ]
13851 The transactional memory language description is written for C++,
13852 and uses the C++0x attribute syntax. For compatibility, allow the
13853 bracket style for transactions in C as well. */
13855 static tree
13856 c_parser_transaction_attributes (c_parser *parser)
13858 tree attr_name, attr = NULL;
13860 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
13861 return c_parser_attributes (parser);
13863 if (!c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
13864 return NULL_TREE;
13865 c_parser_consume_token (parser);
13866 if (!c_parser_require (parser, CPP_OPEN_SQUARE, "expected %<[%>"))
13867 goto error1;
13869 attr_name = c_parser_attribute_any_word (parser);
13870 if (attr_name)
13872 c_parser_consume_token (parser);
13873 attr = build_tree_list (attr_name, NULL_TREE);
13875 else
13876 c_parser_error (parser, "expected identifier");
13878 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, "expected %<]%>");
13879 error1:
13880 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, "expected %<]%>");
13881 return attr;
13884 /* Parse a __transaction_atomic or __transaction_relaxed statement
13885 (GCC Extension).
13887 transaction-statement:
13888 __transaction_atomic transaction-attribute[opt] compound-statement
13889 __transaction_relaxed compound-statement
13891 Note that the only valid attribute is: "outer".
13894 static tree
13895 c_parser_transaction (c_parser *parser, enum rid keyword)
13897 unsigned int old_in = parser->in_transaction;
13898 unsigned int this_in = 1, new_in;
13899 location_t loc = c_parser_peek_token (parser)->location;
13900 tree stmt, attrs;
13902 gcc_assert ((keyword == RID_TRANSACTION_ATOMIC
13903 || keyword == RID_TRANSACTION_RELAXED)
13904 && c_parser_next_token_is_keyword (parser, keyword));
13905 c_parser_consume_token (parser);
13907 if (keyword == RID_TRANSACTION_RELAXED)
13908 this_in |= TM_STMT_ATTR_RELAXED;
13909 else
13911 attrs = c_parser_transaction_attributes (parser);
13912 if (attrs)
13913 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
13916 /* Keep track if we're in the lexical scope of an outer transaction. */
13917 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
13919 parser->in_transaction = new_in;
13920 stmt = c_parser_compound_statement (parser);
13921 parser->in_transaction = old_in;
13923 if (flag_tm)
13924 stmt = c_finish_transaction (loc, stmt, this_in);
13925 else
13926 error_at (loc, (keyword == RID_TRANSACTION_ATOMIC ?
13927 "%<__transaction_atomic%> without transactional memory support enabled"
13928 : "%<__transaction_relaxed %> "
13929 "without transactional memory support enabled"));
13931 return stmt;
13934 /* Parse a __transaction_atomic or __transaction_relaxed expression
13935 (GCC Extension).
13937 transaction-expression:
13938 __transaction_atomic ( expression )
13939 __transaction_relaxed ( expression )
13942 static struct c_expr
13943 c_parser_transaction_expression (c_parser *parser, enum rid keyword)
13945 struct c_expr ret;
13946 unsigned int old_in = parser->in_transaction;
13947 unsigned int this_in = 1;
13948 location_t loc = c_parser_peek_token (parser)->location;
13949 tree attrs;
13951 gcc_assert ((keyword == RID_TRANSACTION_ATOMIC
13952 || keyword == RID_TRANSACTION_RELAXED)
13953 && c_parser_next_token_is_keyword (parser, keyword));
13954 c_parser_consume_token (parser);
13956 if (keyword == RID_TRANSACTION_RELAXED)
13957 this_in |= TM_STMT_ATTR_RELAXED;
13958 else
13960 attrs = c_parser_transaction_attributes (parser);
13961 if (attrs)
13962 this_in |= parse_tm_stmt_attr (attrs, 0);
13965 parser->in_transaction = this_in;
13966 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
13968 tree expr = c_parser_expression (parser).value;
13969 ret.original_type = TREE_TYPE (expr);
13970 ret.value = build1 (TRANSACTION_EXPR, ret.original_type, expr);
13971 if (this_in & TM_STMT_ATTR_RELAXED)
13972 TRANSACTION_EXPR_RELAXED (ret.value) = 1;
13973 SET_EXPR_LOCATION (ret.value, loc);
13974 ret.original_code = TRANSACTION_EXPR;
13975 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
13977 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
13978 goto error;
13981 else
13983 error:
13984 ret.value = error_mark_node;
13985 ret.original_code = ERROR_MARK;
13986 ret.original_type = NULL;
13988 parser->in_transaction = old_in;
13990 if (!flag_tm)
13991 error_at (loc, (keyword == RID_TRANSACTION_ATOMIC ?
13992 "%<__transaction_atomic%> without transactional memory support enabled"
13993 : "%<__transaction_relaxed %> "
13994 "without transactional memory support enabled"));
13996 return ret;
13999 /* Parse a __transaction_cancel statement (GCC Extension).
14001 transaction-cancel-statement:
14002 __transaction_cancel transaction-attribute[opt] ;
14004 Note that the only valid attribute is "outer".
14007 static tree
14008 c_parser_transaction_cancel (c_parser *parser)
14010 location_t loc = c_parser_peek_token (parser)->location;
14011 tree attrs;
14012 bool is_outer = false;
14014 gcc_assert (c_parser_next_token_is_keyword (parser, RID_TRANSACTION_CANCEL));
14015 c_parser_consume_token (parser);
14017 attrs = c_parser_transaction_attributes (parser);
14018 if (attrs)
14019 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
14021 if (!flag_tm)
14023 error_at (loc, "%<__transaction_cancel%> without "
14024 "transactional memory support enabled");
14025 goto ret_error;
14027 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
14029 error_at (loc, "%<__transaction_cancel%> within a "
14030 "%<__transaction_relaxed%>");
14031 goto ret_error;
14033 else if (is_outer)
14035 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
14036 && !is_tm_may_cancel_outer (current_function_decl))
14038 error_at (loc, "outer %<__transaction_cancel%> not "
14039 "within outer %<__transaction_atomic%>");
14040 error_at (loc, " or a %<transaction_may_cancel_outer%> function");
14041 goto ret_error;
14044 else if (parser->in_transaction == 0)
14046 error_at (loc, "%<__transaction_cancel%> not within "
14047 "%<__transaction_atomic%>");
14048 goto ret_error;
14051 return add_stmt (build_tm_abort_call (loc, is_outer));
14053 ret_error:
14054 return build1 (NOP_EXPR, void_type_node, error_mark_node);
14057 /* Parse a single source file. */
14059 void
14060 c_parse_file (void)
14062 /* Use local storage to begin. If the first token is a pragma, parse it.
14063 If it is #pragma GCC pch_preprocess, then this will load a PCH file
14064 which will cause garbage collection. */
14065 c_parser tparser;
14067 memset (&tparser, 0, sizeof tparser);
14068 tparser.tokens = &tparser.tokens_buf[0];
14069 the_parser = &tparser;
14071 if (c_parser_peek_token (&tparser)->pragma_kind == PRAGMA_GCC_PCH_PREPROCESS)
14072 c_parser_pragma_pch_preprocess (&tparser);
14074 the_parser = ggc_alloc<c_parser> ();
14075 *the_parser = tparser;
14076 if (tparser.tokens == &tparser.tokens_buf[0])
14077 the_parser->tokens = &the_parser->tokens_buf[0];
14079 /* Initialize EH, if we've been told to do so. */
14080 if (flag_exceptions)
14081 using_eh_for_cleanups ();
14083 c_parser_translation_unit (the_parser);
14084 the_parser = NULL;
14087 /* This function parses Cilk Plus array notation. The starting index is
14088 passed in INITIAL_INDEX and the array name is passes in ARRAY_VALUE. The
14089 return value of this function is a tree_node called VALUE_TREE of type
14090 ARRAY_NOTATION_REF. */
14092 static tree
14093 c_parser_array_notation (location_t loc, c_parser *parser, tree initial_index,
14094 tree array_value)
14096 c_token *token = NULL;
14097 tree start_index = NULL_TREE, end_index = NULL_TREE, stride = NULL_TREE;
14098 tree value_tree = NULL_TREE, type = NULL_TREE, array_type = NULL_TREE;
14099 tree array_type_domain = NULL_TREE;
14101 if (array_value == error_mark_node || initial_index == error_mark_node)
14103 /* No need to continue. If either of these 2 were true, then an error
14104 must be emitted already. Thus, no need to emit them twice. */
14105 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
14106 return error_mark_node;
14109 array_type = TREE_TYPE (array_value);
14110 gcc_assert (array_type);
14111 type = TREE_TYPE (array_type);
14112 token = c_parser_peek_token (parser);
14114 if (token->type == CPP_EOF)
14116 c_parser_error (parser, "expected %<:%> or numeral");
14117 return value_tree;
14119 else if (token->type == CPP_COLON)
14121 if (!initial_index)
14123 /* If we are here, then we have a case like this A[:]. */
14124 c_parser_consume_token (parser);
14125 if (TREE_CODE (array_type) == POINTER_TYPE)
14127 error_at (loc, "start-index and length fields necessary for "
14128 "using array notations in pointers");
14129 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
14130 return error_mark_node;
14132 if (TREE_CODE (array_type) == FUNCTION_TYPE)
14134 error_at (loc, "array notations cannot be used with function "
14135 "type");
14136 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
14137 return error_mark_node;
14139 array_type_domain = TYPE_DOMAIN (array_type);
14141 if (!array_type_domain)
14143 error_at (loc, "start-index and length fields necessary for "
14144 "using array notations in dimensionless arrays");
14145 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
14146 return error_mark_node;
14149 start_index = TYPE_MINVAL (array_type_domain);
14150 start_index = fold_build1 (CONVERT_EXPR, ptrdiff_type_node,
14151 start_index);
14152 if (!TYPE_MAXVAL (array_type_domain)
14153 || !TREE_CONSTANT (TYPE_MAXVAL (array_type_domain)))
14155 error_at (loc, "start-index and length fields necessary for "
14156 "using array notations in variable-length arrays");
14157 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
14158 return error_mark_node;
14160 end_index = TYPE_MAXVAL (array_type_domain);
14161 end_index = fold_build2 (PLUS_EXPR, TREE_TYPE (end_index),
14162 end_index, integer_one_node);
14163 end_index = fold_build1 (CONVERT_EXPR, ptrdiff_type_node, end_index);
14164 stride = build_int_cst (integer_type_node, 1);
14165 stride = fold_build1 (CONVERT_EXPR, ptrdiff_type_node, stride);
14167 else if (initial_index != error_mark_node)
14169 /* If we are here, then there should be 2 possibilities:
14170 1. Array [EXPR : EXPR]
14171 2. Array [EXPR : EXPR : EXPR]
14173 start_index = initial_index;
14175 if (TREE_CODE (array_type) == FUNCTION_TYPE)
14177 error_at (loc, "array notations cannot be used with function "
14178 "type");
14179 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
14180 return error_mark_node;
14182 c_parser_consume_token (parser); /* consume the ':' */
14183 struct c_expr ce = c_parser_expression (parser);
14184 ce = convert_lvalue_to_rvalue (loc, ce, false, false);
14185 end_index = ce.value;
14186 if (!end_index || end_index == error_mark_node)
14188 c_parser_skip_to_end_of_block_or_statement (parser);
14189 return error_mark_node;
14191 if (c_parser_peek_token (parser)->type == CPP_COLON)
14193 c_parser_consume_token (parser);
14194 ce = c_parser_expression (parser);
14195 ce = convert_lvalue_to_rvalue (loc, ce, false, false);
14196 stride = ce.value;
14197 if (!stride || stride == error_mark_node)
14199 c_parser_skip_to_end_of_block_or_statement (parser);
14200 return error_mark_node;
14204 else
14205 c_parser_error (parser, "expected array notation expression");
14207 else
14208 c_parser_error (parser, "expected array notation expression");
14210 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, "expected %<]%>");
14212 value_tree = build_array_notation_ref (loc, array_value, start_index,
14213 end_index, stride, type);
14214 if (value_tree != error_mark_node)
14215 SET_EXPR_LOCATION (value_tree, loc);
14216 return value_tree;
14219 #include "gt-c-c-parser.h"