gcc/
[official-gcc.git] / gcc / c / c-parser.c
blob34f1e4701f1ab482b9addf39ca2b2beefc97ccbf
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 /* warn_c90_c99_compat has three states: -1/0/1, so we must
1077 play tricks to properly restore it. */
1078 | ((warn_c90_c99_compat == 1) << 7)
1079 | ((warn_c90_c99_compat == -1) << 8)
1080 /* Similarly for warn_c99_c11_compat. */
1081 | ((warn_c99_c11_compat == 1) << 9)
1082 | ((warn_c99_c11_compat == -1) << 10)
1084 cpp_opts->cpp_pedantic = pedantic = 0;
1085 warn_pointer_arith = 0;
1086 cpp_opts->cpp_warn_traditional = warn_traditional = 0;
1087 flag_iso = 0;
1088 cpp_opts->cpp_warn_long_long = warn_long_long = 0;
1089 warn_cxx_compat = 0;
1090 warn_overlength_strings = 0;
1091 warn_c90_c99_compat = 0;
1092 warn_c99_c11_compat = 0;
1093 return ret;
1096 /* Restore the warning flags which are controlled by __extension__.
1097 FLAGS is the return value from disable_extension_diagnostics. */
1099 static inline void
1100 restore_extension_diagnostics (int flags)
1102 cpp_opts->cpp_pedantic = pedantic = flags & 1;
1103 warn_pointer_arith = (flags >> 1) & 1;
1104 cpp_opts->cpp_warn_traditional = warn_traditional = (flags >> 2) & 1;
1105 flag_iso = (flags >> 3) & 1;
1106 cpp_opts->cpp_warn_long_long = warn_long_long = (flags >> 4) & 1;
1107 warn_cxx_compat = (flags >> 5) & 1;
1108 warn_overlength_strings = (flags >> 6) & 1;
1109 /* See above for why is this needed. */
1110 warn_c90_c99_compat = (flags >> 7) & 1 ? 1 : ((flags >> 8) & 1 ? -1 : 0);
1111 warn_c99_c11_compat = (flags >> 9) & 1 ? 1 : ((flags >> 10) & 1 ? -1 : 0);
1114 /* Possibly kinds of declarator to parse. */
1115 typedef enum c_dtr_syn {
1116 /* A normal declarator with an identifier. */
1117 C_DTR_NORMAL,
1118 /* An abstract declarator (maybe empty). */
1119 C_DTR_ABSTRACT,
1120 /* A parameter declarator: may be either, but after a type name does
1121 not redeclare a typedef name as an identifier if it can
1122 alternatively be interpreted as a typedef name; see DR#009,
1123 applied in C90 TC1, omitted from C99 and reapplied in C99 TC2
1124 following DR#249. For example, given a typedef T, "int T" and
1125 "int *T" are valid parameter declarations redeclaring T, while
1126 "int (T)" and "int * (T)" and "int (T[])" and "int (T (int))" are
1127 abstract declarators rather than involving redundant parentheses;
1128 the same applies with attributes inside the parentheses before
1129 "T". */
1130 C_DTR_PARM
1131 } c_dtr_syn;
1133 /* The binary operation precedence levels, where 0 is a dummy lowest level
1134 used for the bottom of the stack. */
1135 enum c_parser_prec {
1136 PREC_NONE,
1137 PREC_LOGOR,
1138 PREC_LOGAND,
1139 PREC_BITOR,
1140 PREC_BITXOR,
1141 PREC_BITAND,
1142 PREC_EQ,
1143 PREC_REL,
1144 PREC_SHIFT,
1145 PREC_ADD,
1146 PREC_MULT,
1147 NUM_PRECS
1150 static void c_parser_external_declaration (c_parser *);
1151 static void c_parser_asm_definition (c_parser *);
1152 static void c_parser_declaration_or_fndef (c_parser *, bool, bool, bool,
1153 bool, bool, tree *, vec<c_token>);
1154 static void c_parser_static_assert_declaration_no_semi (c_parser *);
1155 static void c_parser_static_assert_declaration (c_parser *);
1156 static void c_parser_declspecs (c_parser *, struct c_declspecs *, bool, bool,
1157 bool, bool, bool, enum c_lookahead_kind);
1158 static struct c_typespec c_parser_enum_specifier (c_parser *);
1159 static struct c_typespec c_parser_struct_or_union_specifier (c_parser *);
1160 static tree c_parser_struct_declaration (c_parser *);
1161 static struct c_typespec c_parser_typeof_specifier (c_parser *);
1162 static tree c_parser_alignas_specifier (c_parser *);
1163 static struct c_declarator *c_parser_declarator (c_parser *, bool, c_dtr_syn,
1164 bool *);
1165 static struct c_declarator *c_parser_direct_declarator (c_parser *, bool,
1166 c_dtr_syn, bool *);
1167 static struct c_declarator *c_parser_direct_declarator_inner (c_parser *,
1168 bool,
1169 struct c_declarator *);
1170 static struct c_arg_info *c_parser_parms_declarator (c_parser *, bool, tree);
1171 static struct c_arg_info *c_parser_parms_list_declarator (c_parser *, tree,
1172 tree);
1173 static struct c_parm *c_parser_parameter_declaration (c_parser *, tree);
1174 static tree c_parser_simple_asm_expr (c_parser *);
1175 static tree c_parser_attributes (c_parser *);
1176 static struct c_type_name *c_parser_type_name (c_parser *);
1177 static struct c_expr c_parser_initializer (c_parser *);
1178 static struct c_expr c_parser_braced_init (c_parser *, tree, bool);
1179 static void c_parser_initelt (c_parser *, struct obstack *);
1180 static void c_parser_initval (c_parser *, struct c_expr *,
1181 struct obstack *);
1182 static tree c_parser_compound_statement (c_parser *);
1183 static void c_parser_compound_statement_nostart (c_parser *);
1184 static void c_parser_label (c_parser *);
1185 static void c_parser_statement (c_parser *);
1186 static void c_parser_statement_after_labels (c_parser *);
1187 static void c_parser_if_statement (c_parser *);
1188 static void c_parser_switch_statement (c_parser *);
1189 static void c_parser_while_statement (c_parser *, bool);
1190 static void c_parser_do_statement (c_parser *, bool);
1191 static void c_parser_for_statement (c_parser *, bool);
1192 static tree c_parser_asm_statement (c_parser *);
1193 static tree c_parser_asm_operands (c_parser *);
1194 static tree c_parser_asm_goto_operands (c_parser *);
1195 static tree c_parser_asm_clobbers (c_parser *);
1196 static struct c_expr c_parser_expr_no_commas (c_parser *, struct c_expr *,
1197 tree = NULL_TREE);
1198 static struct c_expr c_parser_conditional_expression (c_parser *,
1199 struct c_expr *, tree);
1200 static struct c_expr c_parser_binary_expression (c_parser *, struct c_expr *,
1201 tree);
1202 static struct c_expr c_parser_cast_expression (c_parser *, struct c_expr *);
1203 static struct c_expr c_parser_unary_expression (c_parser *);
1204 static struct c_expr c_parser_sizeof_expression (c_parser *);
1205 static struct c_expr c_parser_alignof_expression (c_parser *);
1206 static struct c_expr c_parser_postfix_expression (c_parser *);
1207 static struct c_expr c_parser_postfix_expression_after_paren_type (c_parser *,
1208 struct c_type_name *,
1209 location_t);
1210 static struct c_expr c_parser_postfix_expression_after_primary (c_parser *,
1211 location_t loc,
1212 struct c_expr);
1213 static tree c_parser_transaction (c_parser *, enum rid);
1214 static struct c_expr c_parser_transaction_expression (c_parser *, enum rid);
1215 static tree c_parser_transaction_cancel (c_parser *);
1216 static struct c_expr c_parser_expression (c_parser *);
1217 static struct c_expr c_parser_expression_conv (c_parser *);
1218 static vec<tree, va_gc> *c_parser_expr_list (c_parser *, bool, bool,
1219 vec<tree, va_gc> **, location_t *,
1220 tree *, vec<location_t> *,
1221 unsigned int * = NULL);
1222 static void c_parser_omp_construct (c_parser *);
1223 static void c_parser_omp_threadprivate (c_parser *);
1224 static void c_parser_omp_barrier (c_parser *);
1225 static void c_parser_omp_flush (c_parser *);
1226 static void c_parser_omp_taskwait (c_parser *);
1227 static void c_parser_omp_taskyield (c_parser *);
1228 static void c_parser_omp_cancel (c_parser *);
1229 static void c_parser_omp_cancellation_point (c_parser *);
1231 enum pragma_context { pragma_external, pragma_struct, pragma_param,
1232 pragma_stmt, pragma_compound };
1233 static bool c_parser_pragma (c_parser *, enum pragma_context);
1234 static bool c_parser_omp_target (c_parser *, enum pragma_context);
1235 static void c_parser_omp_end_declare_target (c_parser *);
1236 static void c_parser_omp_declare (c_parser *, enum pragma_context);
1238 /* These Objective-C parser functions are only ever called when
1239 compiling Objective-C. */
1240 static void c_parser_objc_class_definition (c_parser *, tree);
1241 static void c_parser_objc_class_instance_variables (c_parser *);
1242 static void c_parser_objc_class_declaration (c_parser *);
1243 static void c_parser_objc_alias_declaration (c_parser *);
1244 static void c_parser_objc_protocol_definition (c_parser *, tree);
1245 static bool c_parser_objc_method_type (c_parser *);
1246 static void c_parser_objc_method_definition (c_parser *);
1247 static void c_parser_objc_methodprotolist (c_parser *);
1248 static void c_parser_objc_methodproto (c_parser *);
1249 static tree c_parser_objc_method_decl (c_parser *, bool, tree *, tree *);
1250 static tree c_parser_objc_type_name (c_parser *);
1251 static tree c_parser_objc_protocol_refs (c_parser *);
1252 static void c_parser_objc_try_catch_finally_statement (c_parser *);
1253 static void c_parser_objc_synchronized_statement (c_parser *);
1254 static tree c_parser_objc_selector (c_parser *);
1255 static tree c_parser_objc_selector_arg (c_parser *);
1256 static tree c_parser_objc_receiver (c_parser *);
1257 static tree c_parser_objc_message_args (c_parser *);
1258 static tree c_parser_objc_keywordexpr (c_parser *);
1259 static void c_parser_objc_at_property_declaration (c_parser *);
1260 static void c_parser_objc_at_synthesize_declaration (c_parser *);
1261 static void c_parser_objc_at_dynamic_declaration (c_parser *);
1262 static bool c_parser_objc_diagnose_bad_element_prefix
1263 (c_parser *, struct c_declspecs *);
1265 /* Cilk Plus supporting routines. */
1266 static void c_parser_cilk_simd (c_parser *);
1267 static bool c_parser_cilk_verify_simd (c_parser *, enum pragma_context);
1268 static tree c_parser_array_notation (location_t, c_parser *, tree, tree);
1269 static tree c_parser_cilk_clause_vectorlength (c_parser *, tree, bool);
1271 /* Parse a translation unit (C90 6.7, C99 6.9).
1273 translation-unit:
1274 external-declarations
1276 external-declarations:
1277 external-declaration
1278 external-declarations external-declaration
1280 GNU extensions:
1282 translation-unit:
1283 empty
1286 static void
1287 c_parser_translation_unit (c_parser *parser)
1289 if (c_parser_next_token_is (parser, CPP_EOF))
1291 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
1292 "ISO C forbids an empty translation unit");
1294 else
1296 void *obstack_position = obstack_alloc (&parser_obstack, 0);
1297 mark_valid_location_for_stdc_pragma (false);
1300 ggc_collect ();
1301 c_parser_external_declaration (parser);
1302 obstack_free (&parser_obstack, obstack_position);
1304 while (c_parser_next_token_is_not (parser, CPP_EOF));
1308 /* Parse an external declaration (C90 6.7, C99 6.9).
1310 external-declaration:
1311 function-definition
1312 declaration
1314 GNU extensions:
1316 external-declaration:
1317 asm-definition
1319 __extension__ external-declaration
1321 Objective-C:
1323 external-declaration:
1324 objc-class-definition
1325 objc-class-declaration
1326 objc-alias-declaration
1327 objc-protocol-definition
1328 objc-method-definition
1329 @end
1332 static void
1333 c_parser_external_declaration (c_parser *parser)
1335 int ext;
1336 switch (c_parser_peek_token (parser)->type)
1338 case CPP_KEYWORD:
1339 switch (c_parser_peek_token (parser)->keyword)
1341 case RID_EXTENSION:
1342 ext = disable_extension_diagnostics ();
1343 c_parser_consume_token (parser);
1344 c_parser_external_declaration (parser);
1345 restore_extension_diagnostics (ext);
1346 break;
1347 case RID_ASM:
1348 c_parser_asm_definition (parser);
1349 break;
1350 case RID_AT_INTERFACE:
1351 case RID_AT_IMPLEMENTATION:
1352 gcc_assert (c_dialect_objc ());
1353 c_parser_objc_class_definition (parser, NULL_TREE);
1354 break;
1355 case RID_AT_CLASS:
1356 gcc_assert (c_dialect_objc ());
1357 c_parser_objc_class_declaration (parser);
1358 break;
1359 case RID_AT_ALIAS:
1360 gcc_assert (c_dialect_objc ());
1361 c_parser_objc_alias_declaration (parser);
1362 break;
1363 case RID_AT_PROTOCOL:
1364 gcc_assert (c_dialect_objc ());
1365 c_parser_objc_protocol_definition (parser, NULL_TREE);
1366 break;
1367 case RID_AT_PROPERTY:
1368 gcc_assert (c_dialect_objc ());
1369 c_parser_objc_at_property_declaration (parser);
1370 break;
1371 case RID_AT_SYNTHESIZE:
1372 gcc_assert (c_dialect_objc ());
1373 c_parser_objc_at_synthesize_declaration (parser);
1374 break;
1375 case RID_AT_DYNAMIC:
1376 gcc_assert (c_dialect_objc ());
1377 c_parser_objc_at_dynamic_declaration (parser);
1378 break;
1379 case RID_AT_END:
1380 gcc_assert (c_dialect_objc ());
1381 c_parser_consume_token (parser);
1382 objc_finish_implementation ();
1383 break;
1384 default:
1385 goto decl_or_fndef;
1387 break;
1388 case CPP_SEMICOLON:
1389 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
1390 "ISO C does not allow extra %<;%> outside of a function");
1391 c_parser_consume_token (parser);
1392 break;
1393 case CPP_PRAGMA:
1394 mark_valid_location_for_stdc_pragma (true);
1395 c_parser_pragma (parser, pragma_external);
1396 mark_valid_location_for_stdc_pragma (false);
1397 break;
1398 case CPP_PLUS:
1399 case CPP_MINUS:
1400 if (c_dialect_objc ())
1402 c_parser_objc_method_definition (parser);
1403 break;
1405 /* Else fall through, and yield a syntax error trying to parse
1406 as a declaration or function definition. */
1407 default:
1408 decl_or_fndef:
1409 /* A declaration or a function definition (or, in Objective-C,
1410 an @interface or @protocol with prefix attributes). We can
1411 only tell which after parsing the declaration specifiers, if
1412 any, and the first declarator. */
1413 c_parser_declaration_or_fndef (parser, true, true, true, false, true,
1414 NULL, vNULL);
1415 break;
1419 static void c_finish_omp_declare_simd (c_parser *, tree, tree, vec<c_token>);
1421 /* Parse a declaration or function definition (C90 6.5, 6.7.1, C99
1422 6.7, 6.9.1). If FNDEF_OK is true, a function definition is
1423 accepted; otherwise (old-style parameter declarations) only other
1424 declarations are accepted. If STATIC_ASSERT_OK is true, a static
1425 assertion is accepted; otherwise (old-style parameter declarations)
1426 it is not. If NESTED is true, we are inside a function or parsing
1427 old-style parameter declarations; any functions encountered are
1428 nested functions and declaration specifiers are required; otherwise
1429 we are at top level and functions are normal functions and
1430 declaration specifiers may be optional. If EMPTY_OK is true, empty
1431 declarations are OK (subject to all other constraints); otherwise
1432 (old-style parameter declarations) they are diagnosed. If
1433 START_ATTR_OK is true, the declaration specifiers may start with
1434 attributes; otherwise they may not.
1435 OBJC_FOREACH_OBJECT_DECLARATION can be used to get back the parsed
1436 declaration when parsing an Objective-C foreach statement.
1438 declaration:
1439 declaration-specifiers init-declarator-list[opt] ;
1440 static_assert-declaration
1442 function-definition:
1443 declaration-specifiers[opt] declarator declaration-list[opt]
1444 compound-statement
1446 declaration-list:
1447 declaration
1448 declaration-list declaration
1450 init-declarator-list:
1451 init-declarator
1452 init-declarator-list , init-declarator
1454 init-declarator:
1455 declarator simple-asm-expr[opt] attributes[opt]
1456 declarator simple-asm-expr[opt] attributes[opt] = initializer
1458 GNU extensions:
1460 nested-function-definition:
1461 declaration-specifiers declarator declaration-list[opt]
1462 compound-statement
1464 Objective-C:
1465 attributes objc-class-definition
1466 attributes objc-category-definition
1467 attributes objc-protocol-definition
1469 The simple-asm-expr and attributes are GNU extensions.
1471 This function does not handle __extension__; that is handled in its
1472 callers. ??? Following the old parser, __extension__ may start
1473 external declarations, declarations in functions and declarations
1474 at the start of "for" loops, but not old-style parameter
1475 declarations.
1477 C99 requires declaration specifiers in a function definition; the
1478 absence is diagnosed through the diagnosis of implicit int. In GNU
1479 C we also allow but diagnose declarations without declaration
1480 specifiers, but only at top level (elsewhere they conflict with
1481 other syntax).
1483 In Objective-C, declarations of the looping variable in a foreach
1484 statement are exceptionally terminated by 'in' (for example, 'for
1485 (NSObject *object in array) { ... }').
1487 OpenMP:
1489 declaration:
1490 threadprivate-directive */
1492 static void
1493 c_parser_declaration_or_fndef (c_parser *parser, bool fndef_ok,
1494 bool static_assert_ok, bool empty_ok,
1495 bool nested, bool start_attr_ok,
1496 tree *objc_foreach_object_declaration,
1497 vec<c_token> omp_declare_simd_clauses)
1499 struct c_declspecs *specs;
1500 tree prefix_attrs;
1501 tree all_prefix_attrs;
1502 bool diagnosed_no_specs = false;
1503 location_t here = c_parser_peek_token (parser)->location;
1505 if (static_assert_ok
1506 && c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT))
1508 c_parser_static_assert_declaration (parser);
1509 return;
1511 specs = build_null_declspecs ();
1513 /* Try to detect an unknown type name when we have "A B" or "A *B". */
1514 if (c_parser_peek_token (parser)->type == CPP_NAME
1515 && c_parser_peek_token (parser)->id_kind == C_ID_ID
1516 && (c_parser_peek_2nd_token (parser)->type == CPP_NAME
1517 || c_parser_peek_2nd_token (parser)->type == CPP_MULT)
1518 && (!nested || !lookup_name (c_parser_peek_token (parser)->value)))
1520 error_at (here, "unknown type name %qE",
1521 c_parser_peek_token (parser)->value);
1523 /* Parse declspecs normally to get a correct pointer type, but avoid
1524 a further "fails to be a type name" error. Refuse nested functions
1525 since it is not how the user likely wants us to recover. */
1526 c_parser_peek_token (parser)->type = CPP_KEYWORD;
1527 c_parser_peek_token (parser)->keyword = RID_VOID;
1528 c_parser_peek_token (parser)->value = error_mark_node;
1529 fndef_ok = !nested;
1532 c_parser_declspecs (parser, specs, true, true, start_attr_ok,
1533 true, true, cla_nonabstract_decl);
1534 if (parser->error)
1536 c_parser_skip_to_end_of_block_or_statement (parser);
1537 return;
1539 if (nested && !specs->declspecs_seen_p)
1541 c_parser_error (parser, "expected declaration specifiers");
1542 c_parser_skip_to_end_of_block_or_statement (parser);
1543 return;
1545 finish_declspecs (specs);
1546 bool auto_type_p = specs->typespec_word == cts_auto_type;
1547 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1549 if (auto_type_p)
1550 error_at (here, "%<__auto_type%> in empty declaration");
1551 else if (empty_ok)
1552 shadow_tag (specs);
1553 else
1555 shadow_tag_warned (specs, 1);
1556 pedwarn (here, 0, "empty declaration");
1558 c_parser_consume_token (parser);
1559 return;
1562 /* Provide better error recovery. Note that a type name here is usually
1563 better diagnosed as a redeclaration. */
1564 if (empty_ok
1565 && specs->typespec_kind == ctsk_tagdef
1566 && c_parser_next_token_starts_declspecs (parser)
1567 && !c_parser_next_token_is (parser, CPP_NAME))
1569 c_parser_error (parser, "expected %<;%>, identifier or %<(%>");
1570 parser->error = false;
1571 shadow_tag_warned (specs, 1);
1572 return;
1574 else if (c_dialect_objc () && !auto_type_p)
1576 /* Prefix attributes are an error on method decls. */
1577 switch (c_parser_peek_token (parser)->type)
1579 case CPP_PLUS:
1580 case CPP_MINUS:
1581 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1582 return;
1583 if (specs->attrs)
1585 warning_at (c_parser_peek_token (parser)->location,
1586 OPT_Wattributes,
1587 "prefix attributes are ignored for methods");
1588 specs->attrs = NULL_TREE;
1590 if (fndef_ok)
1591 c_parser_objc_method_definition (parser);
1592 else
1593 c_parser_objc_methodproto (parser);
1594 return;
1595 break;
1596 default:
1597 break;
1599 /* This is where we parse 'attributes @interface ...',
1600 'attributes @implementation ...', 'attributes @protocol ...'
1601 (where attributes could be, for example, __attribute__
1602 ((deprecated)).
1604 switch (c_parser_peek_token (parser)->keyword)
1606 case RID_AT_INTERFACE:
1608 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1609 return;
1610 c_parser_objc_class_definition (parser, specs->attrs);
1611 return;
1613 break;
1614 case RID_AT_IMPLEMENTATION:
1616 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1617 return;
1618 if (specs->attrs)
1620 warning_at (c_parser_peek_token (parser)->location,
1621 OPT_Wattributes,
1622 "prefix attributes are ignored for implementations");
1623 specs->attrs = NULL_TREE;
1625 c_parser_objc_class_definition (parser, NULL_TREE);
1626 return;
1628 break;
1629 case RID_AT_PROTOCOL:
1631 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1632 return;
1633 c_parser_objc_protocol_definition (parser, specs->attrs);
1634 return;
1636 break;
1637 case RID_AT_ALIAS:
1638 case RID_AT_CLASS:
1639 case RID_AT_END:
1640 case RID_AT_PROPERTY:
1641 if (specs->attrs)
1643 c_parser_error (parser, "unexpected attribute");
1644 specs->attrs = NULL;
1646 break;
1647 default:
1648 break;
1652 pending_xref_error ();
1653 prefix_attrs = specs->attrs;
1654 all_prefix_attrs = prefix_attrs;
1655 specs->attrs = NULL_TREE;
1656 while (true)
1658 struct c_declarator *declarator;
1659 bool dummy = false;
1660 timevar_id_t tv;
1661 tree fnbody;
1662 /* Declaring either one or more declarators (in which case we
1663 should diagnose if there were no declaration specifiers) or a
1664 function definition (in which case the diagnostic for
1665 implicit int suffices). */
1666 declarator = c_parser_declarator (parser,
1667 specs->typespec_kind != ctsk_none,
1668 C_DTR_NORMAL, &dummy);
1669 if (declarator == NULL)
1671 if (omp_declare_simd_clauses.exists ()
1672 || !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
1673 c_finish_omp_declare_simd (parser, NULL_TREE, NULL_TREE,
1674 omp_declare_simd_clauses);
1675 c_parser_skip_to_end_of_block_or_statement (parser);
1676 return;
1678 if (auto_type_p && declarator->kind != cdk_id)
1680 error_at (here,
1681 "%<__auto_type%> requires a plain identifier"
1682 " as declarator");
1683 c_parser_skip_to_end_of_block_or_statement (parser);
1684 return;
1686 if (c_parser_next_token_is (parser, CPP_EQ)
1687 || c_parser_next_token_is (parser, CPP_COMMA)
1688 || c_parser_next_token_is (parser, CPP_SEMICOLON)
1689 || c_parser_next_token_is_keyword (parser, RID_ASM)
1690 || c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE)
1691 || c_parser_next_token_is_keyword (parser, RID_IN))
1693 tree asm_name = NULL_TREE;
1694 tree postfix_attrs = NULL_TREE;
1695 if (!diagnosed_no_specs && !specs->declspecs_seen_p)
1697 diagnosed_no_specs = true;
1698 pedwarn (here, 0, "data definition has no type or storage class");
1700 /* Having seen a data definition, there cannot now be a
1701 function definition. */
1702 fndef_ok = false;
1703 if (c_parser_next_token_is_keyword (parser, RID_ASM))
1704 asm_name = c_parser_simple_asm_expr (parser);
1705 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
1707 postfix_attrs = c_parser_attributes (parser);
1708 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
1710 /* This means there is an attribute specifier after
1711 the declarator in a function definition. Provide
1712 some more information for the user. */
1713 error_at (here, "attributes should be specified before the "
1714 "declarator in a function definition");
1715 c_parser_skip_to_end_of_block_or_statement (parser);
1716 return;
1719 if (c_parser_next_token_is (parser, CPP_EQ))
1721 tree d;
1722 struct c_expr init;
1723 location_t init_loc;
1724 c_parser_consume_token (parser);
1725 if (auto_type_p)
1727 start_init (NULL_TREE, asm_name, global_bindings_p ());
1728 init_loc = c_parser_peek_token (parser)->location;
1729 init = c_parser_expr_no_commas (parser, NULL);
1730 if (TREE_CODE (init.value) == COMPONENT_REF
1731 && DECL_C_BIT_FIELD (TREE_OPERAND (init.value, 1)))
1732 error_at (here,
1733 "%<__auto_type%> used with a bit-field"
1734 " initializer");
1735 init = convert_lvalue_to_rvalue (init_loc, init, true, true);
1736 tree init_type = TREE_TYPE (init.value);
1737 /* As with typeof, remove all qualifiers from atomic types. */
1738 if (init_type != error_mark_node && TYPE_ATOMIC (init_type))
1739 init_type
1740 = c_build_qualified_type (init_type, TYPE_UNQUALIFIED);
1741 bool vm_type = variably_modified_type_p (init_type,
1742 NULL_TREE);
1743 if (vm_type)
1744 init.value = c_save_expr (init.value);
1745 finish_init ();
1746 specs->typespec_kind = ctsk_typeof;
1747 specs->locations[cdw_typedef] = init_loc;
1748 specs->typedef_p = true;
1749 specs->type = init_type;
1750 if (vm_type)
1752 bool maybe_const = true;
1753 tree type_expr = c_fully_fold (init.value, false,
1754 &maybe_const);
1755 specs->expr_const_operands &= maybe_const;
1756 if (specs->expr)
1757 specs->expr = build2 (COMPOUND_EXPR,
1758 TREE_TYPE (type_expr),
1759 specs->expr, type_expr);
1760 else
1761 specs->expr = type_expr;
1763 d = start_decl (declarator, specs, true,
1764 chainon (postfix_attrs, all_prefix_attrs));
1765 if (!d)
1766 d = error_mark_node;
1767 if (omp_declare_simd_clauses.exists ()
1768 || !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
1769 c_finish_omp_declare_simd (parser, d, NULL_TREE,
1770 omp_declare_simd_clauses);
1772 else
1774 /* The declaration of the variable is in effect while
1775 its initializer is parsed. */
1776 d = start_decl (declarator, specs, true,
1777 chainon (postfix_attrs, all_prefix_attrs));
1778 if (!d)
1779 d = error_mark_node;
1780 if (omp_declare_simd_clauses.exists ()
1781 || !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
1782 c_finish_omp_declare_simd (parser, d, NULL_TREE,
1783 omp_declare_simd_clauses);
1784 start_init (d, asm_name, global_bindings_p ());
1785 init_loc = c_parser_peek_token (parser)->location;
1786 init = c_parser_initializer (parser);
1787 finish_init ();
1789 if (d != error_mark_node)
1791 maybe_warn_string_init (init_loc, TREE_TYPE (d), init);
1792 finish_decl (d, init_loc, init.value,
1793 init.original_type, asm_name);
1796 else
1798 if (auto_type_p)
1800 error_at (here,
1801 "%<__auto_type%> requires an initialized "
1802 "data declaration");
1803 c_parser_skip_to_end_of_block_or_statement (parser);
1804 return;
1806 tree d = start_decl (declarator, specs, false,
1807 chainon (postfix_attrs,
1808 all_prefix_attrs));
1809 if (omp_declare_simd_clauses.exists ()
1810 || !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
1812 tree parms = NULL_TREE;
1813 if (d && TREE_CODE (d) == FUNCTION_DECL)
1815 struct c_declarator *ce = declarator;
1816 while (ce != NULL)
1817 if (ce->kind == cdk_function)
1819 parms = ce->u.arg_info->parms;
1820 break;
1822 else
1823 ce = ce->declarator;
1825 if (parms)
1826 temp_store_parm_decls (d, parms);
1827 c_finish_omp_declare_simd (parser, d, parms,
1828 omp_declare_simd_clauses);
1829 if (parms)
1830 temp_pop_parm_decls ();
1832 if (d)
1833 finish_decl (d, UNKNOWN_LOCATION, NULL_TREE,
1834 NULL_TREE, asm_name);
1836 if (c_parser_next_token_is_keyword (parser, RID_IN))
1838 if (d)
1839 *objc_foreach_object_declaration = d;
1840 else
1841 *objc_foreach_object_declaration = error_mark_node;
1844 if (c_parser_next_token_is (parser, CPP_COMMA))
1846 if (auto_type_p)
1848 error_at (here,
1849 "%<__auto_type%> may only be used with"
1850 " a single declarator");
1851 c_parser_skip_to_end_of_block_or_statement (parser);
1852 return;
1854 c_parser_consume_token (parser);
1855 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
1856 all_prefix_attrs = chainon (c_parser_attributes (parser),
1857 prefix_attrs);
1858 else
1859 all_prefix_attrs = prefix_attrs;
1860 continue;
1862 else if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1864 c_parser_consume_token (parser);
1865 return;
1867 else if (c_parser_next_token_is_keyword (parser, RID_IN))
1869 /* This can only happen in Objective-C: we found the
1870 'in' that terminates the declaration inside an
1871 Objective-C foreach statement. Do not consume the
1872 token, so that the caller can use it to determine
1873 that this indeed is a foreach context. */
1874 return;
1876 else
1878 c_parser_error (parser, "expected %<,%> or %<;%>");
1879 c_parser_skip_to_end_of_block_or_statement (parser);
1880 return;
1883 else if (auto_type_p)
1885 error_at (here,
1886 "%<__auto_type%> requires an initialized data declaration");
1887 c_parser_skip_to_end_of_block_or_statement (parser);
1888 return;
1890 else if (!fndef_ok)
1892 c_parser_error (parser, "expected %<=%>, %<,%>, %<;%>, "
1893 "%<asm%> or %<__attribute__%>");
1894 c_parser_skip_to_end_of_block_or_statement (parser);
1895 return;
1897 /* Function definition (nested or otherwise). */
1898 if (nested)
1900 pedwarn (here, OPT_Wpedantic, "ISO C forbids nested functions");
1901 c_push_function_context ();
1903 if (!start_function (specs, declarator, all_prefix_attrs))
1905 /* This can appear in many cases looking nothing like a
1906 function definition, so we don't give a more specific
1907 error suggesting there was one. */
1908 c_parser_error (parser, "expected %<=%>, %<,%>, %<;%>, %<asm%> "
1909 "or %<__attribute__%>");
1910 if (nested)
1911 c_pop_function_context ();
1912 break;
1915 if (DECL_DECLARED_INLINE_P (current_function_decl))
1916 tv = TV_PARSE_INLINE;
1917 else
1918 tv = TV_PARSE_FUNC;
1919 timevar_push (tv);
1921 /* Parse old-style parameter declarations. ??? Attributes are
1922 not allowed to start declaration specifiers here because of a
1923 syntax conflict between a function declaration with attribute
1924 suffix and a function definition with an attribute prefix on
1925 first old-style parameter declaration. Following the old
1926 parser, they are not accepted on subsequent old-style
1927 parameter declarations either. However, there is no
1928 ambiguity after the first declaration, nor indeed on the
1929 first as long as we don't allow postfix attributes after a
1930 declarator with a nonempty identifier list in a definition;
1931 and postfix attributes have never been accepted here in
1932 function definitions either. */
1933 while (c_parser_next_token_is_not (parser, CPP_EOF)
1934 && c_parser_next_token_is_not (parser, CPP_OPEN_BRACE))
1935 c_parser_declaration_or_fndef (parser, false, false, false,
1936 true, false, NULL, vNULL);
1937 store_parm_decls ();
1938 if (omp_declare_simd_clauses.exists ()
1939 || !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
1940 c_finish_omp_declare_simd (parser, current_function_decl, NULL_TREE,
1941 omp_declare_simd_clauses);
1942 DECL_STRUCT_FUNCTION (current_function_decl)->function_start_locus
1943 = c_parser_peek_token (parser)->location;
1944 fnbody = c_parser_compound_statement (parser);
1945 if (flag_cilkplus && contains_array_notation_expr (fnbody))
1946 fnbody = expand_array_notation_exprs (fnbody);
1947 if (nested)
1949 tree decl = current_function_decl;
1950 /* Mark nested functions as needing static-chain initially.
1951 lower_nested_functions will recompute it but the
1952 DECL_STATIC_CHAIN flag is also used before that happens,
1953 by initializer_constant_valid_p. See gcc.dg/nested-fn-2.c. */
1954 DECL_STATIC_CHAIN (decl) = 1;
1955 add_stmt (fnbody);
1956 finish_function ();
1957 c_pop_function_context ();
1958 add_stmt (build_stmt (DECL_SOURCE_LOCATION (decl), DECL_EXPR, decl));
1960 else
1962 add_stmt (fnbody);
1963 finish_function ();
1966 timevar_pop (tv);
1967 break;
1971 /* Parse an asm-definition (asm() outside a function body). This is a
1972 GNU extension.
1974 asm-definition:
1975 simple-asm-expr ;
1978 static void
1979 c_parser_asm_definition (c_parser *parser)
1981 tree asm_str = c_parser_simple_asm_expr (parser);
1982 if (asm_str)
1983 symtab->finalize_toplevel_asm (asm_str);
1984 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
1987 /* Parse a static assertion (C11 6.7.10).
1989 static_assert-declaration:
1990 static_assert-declaration-no-semi ;
1993 static void
1994 c_parser_static_assert_declaration (c_parser *parser)
1996 c_parser_static_assert_declaration_no_semi (parser);
1997 if (parser->error
1998 || !c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
1999 c_parser_skip_to_end_of_block_or_statement (parser);
2002 /* Parse a static assertion (C11 6.7.10), without the trailing
2003 semicolon.
2005 static_assert-declaration-no-semi:
2006 _Static_assert ( constant-expression , string-literal )
2009 static void
2010 c_parser_static_assert_declaration_no_semi (c_parser *parser)
2012 location_t assert_loc, value_loc;
2013 tree value;
2014 tree string;
2016 gcc_assert (c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT));
2017 assert_loc = c_parser_peek_token (parser)->location;
2018 if (flag_isoc99)
2019 pedwarn_c99 (assert_loc, OPT_Wpedantic,
2020 "ISO C99 does not support %<_Static_assert%>");
2021 else
2022 pedwarn_c99 (assert_loc, OPT_Wpedantic,
2023 "ISO C90 does not support %<_Static_assert%>");
2024 c_parser_consume_token (parser);
2025 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2026 return;
2027 value_loc = c_parser_peek_token (parser)->location;
2028 value = c_parser_expr_no_commas (parser, NULL).value;
2029 parser->lex_untranslated_string = true;
2030 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
2032 parser->lex_untranslated_string = false;
2033 return;
2035 switch (c_parser_peek_token (parser)->type)
2037 case CPP_STRING:
2038 case CPP_STRING16:
2039 case CPP_STRING32:
2040 case CPP_WSTRING:
2041 case CPP_UTF8STRING:
2042 string = c_parser_peek_token (parser)->value;
2043 c_parser_consume_token (parser);
2044 parser->lex_untranslated_string = false;
2045 break;
2046 default:
2047 c_parser_error (parser, "expected string literal");
2048 parser->lex_untranslated_string = false;
2049 return;
2051 c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
2053 if (!INTEGRAL_TYPE_P (TREE_TYPE (value)))
2055 error_at (value_loc, "expression in static assertion is not an integer");
2056 return;
2058 if (TREE_CODE (value) != INTEGER_CST)
2060 value = c_fully_fold (value, false, NULL);
2061 if (TREE_CODE (value) == INTEGER_CST)
2062 pedwarn (value_loc, OPT_Wpedantic, "expression in static assertion "
2063 "is not an integer constant expression");
2065 if (TREE_CODE (value) != INTEGER_CST)
2067 error_at (value_loc, "expression in static assertion is not constant");
2068 return;
2070 constant_expression_warning (value);
2071 if (integer_zerop (value))
2072 error_at (assert_loc, "static assertion failed: %E", string);
2075 /* Parse some declaration specifiers (possibly none) (C90 6.5, C99
2076 6.7), adding them to SPECS (which may already include some).
2077 Storage class specifiers are accepted iff SCSPEC_OK; type
2078 specifiers are accepted iff TYPESPEC_OK; alignment specifiers are
2079 accepted iff ALIGNSPEC_OK; attributes are accepted at the start
2080 iff START_ATTR_OK; __auto_type is accepted iff AUTO_TYPE_OK.
2082 declaration-specifiers:
2083 storage-class-specifier declaration-specifiers[opt]
2084 type-specifier declaration-specifiers[opt]
2085 type-qualifier declaration-specifiers[opt]
2086 function-specifier declaration-specifiers[opt]
2087 alignment-specifier declaration-specifiers[opt]
2089 Function specifiers (inline) are from C99, and are currently
2090 handled as storage class specifiers, as is __thread. Alignment
2091 specifiers are from C11.
2093 C90 6.5.1, C99 6.7.1:
2094 storage-class-specifier:
2095 typedef
2096 extern
2097 static
2098 auto
2099 register
2100 _Thread_local
2102 (_Thread_local is new in C11.)
2104 C99 6.7.4:
2105 function-specifier:
2106 inline
2107 _Noreturn
2109 (_Noreturn is new in C11.)
2111 C90 6.5.2, C99 6.7.2:
2112 type-specifier:
2113 void
2114 char
2115 short
2117 long
2118 float
2119 double
2120 signed
2121 unsigned
2122 _Bool
2123 _Complex
2124 [_Imaginary removed in C99 TC2]
2125 struct-or-union-specifier
2126 enum-specifier
2127 typedef-name
2128 atomic-type-specifier
2130 (_Bool and _Complex are new in C99.)
2131 (atomic-type-specifier is new in C11.)
2133 C90 6.5.3, C99 6.7.3:
2135 type-qualifier:
2136 const
2137 restrict
2138 volatile
2139 address-space-qualifier
2140 _Atomic
2142 (restrict is new in C99.)
2143 (_Atomic is new in C11.)
2145 GNU extensions:
2147 declaration-specifiers:
2148 attributes declaration-specifiers[opt]
2150 type-qualifier:
2151 address-space
2153 address-space:
2154 identifier recognized by the target
2156 storage-class-specifier:
2157 __thread
2159 type-specifier:
2160 typeof-specifier
2161 __auto_type
2162 __int128
2163 _Decimal32
2164 _Decimal64
2165 _Decimal128
2166 _Fract
2167 _Accum
2168 _Sat
2170 (_Fract, _Accum, and _Sat are new from ISO/IEC DTR 18037:
2171 http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1169.pdf)
2173 atomic-type-specifier
2174 _Atomic ( type-name )
2176 Objective-C:
2178 type-specifier:
2179 class-name objc-protocol-refs[opt]
2180 typedef-name objc-protocol-refs
2181 objc-protocol-refs
2184 static void
2185 c_parser_declspecs (c_parser *parser, struct c_declspecs *specs,
2186 bool scspec_ok, bool typespec_ok, bool start_attr_ok,
2187 bool alignspec_ok, bool auto_type_ok,
2188 enum c_lookahead_kind la)
2190 bool attrs_ok = start_attr_ok;
2191 bool seen_type = specs->typespec_kind != ctsk_none;
2193 if (!typespec_ok)
2194 gcc_assert (la == cla_prefer_id);
2196 while (c_parser_next_token_is (parser, CPP_NAME)
2197 || c_parser_next_token_is (parser, CPP_KEYWORD)
2198 || (c_dialect_objc () && c_parser_next_token_is (parser, CPP_LESS)))
2200 struct c_typespec t;
2201 tree attrs;
2202 tree align;
2203 location_t loc = c_parser_peek_token (parser)->location;
2205 /* If we cannot accept a type, exit if the next token must start
2206 one. Also, if we already have seen a tagged definition,
2207 a typename would be an error anyway and likely the user
2208 has simply forgotten a semicolon, so we exit. */
2209 if ((!typespec_ok || specs->typespec_kind == ctsk_tagdef)
2210 && c_parser_next_tokens_start_typename (parser, la)
2211 && !c_parser_next_token_is_qualifier (parser))
2212 break;
2214 if (c_parser_next_token_is (parser, CPP_NAME))
2216 c_token *name_token = c_parser_peek_token (parser);
2217 tree value = name_token->value;
2218 c_id_kind kind = name_token->id_kind;
2220 if (kind == C_ID_ADDRSPACE)
2222 addr_space_t as
2223 = name_token->keyword - RID_FIRST_ADDR_SPACE;
2224 declspecs_add_addrspace (name_token->location, specs, as);
2225 c_parser_consume_token (parser);
2226 attrs_ok = true;
2227 continue;
2230 gcc_assert (!c_parser_next_token_is_qualifier (parser));
2232 /* If we cannot accept a type, and the next token must start one,
2233 exit. Do the same if we already have seen a tagged definition,
2234 since it would be an error anyway and likely the user has simply
2235 forgotten a semicolon. */
2236 if (seen_type || !c_parser_next_tokens_start_typename (parser, la))
2237 break;
2239 /* Now at an unknown typename (C_ID_ID), a C_ID_TYPENAME or
2240 a C_ID_CLASSNAME. */
2241 c_parser_consume_token (parser);
2242 seen_type = true;
2243 attrs_ok = true;
2244 if (kind == C_ID_ID)
2246 error_at (loc, "unknown type name %qE", value);
2247 t.kind = ctsk_typedef;
2248 t.spec = error_mark_node;
2250 else if (kind == C_ID_TYPENAME
2251 && (!c_dialect_objc ()
2252 || c_parser_next_token_is_not (parser, CPP_LESS)))
2254 t.kind = ctsk_typedef;
2255 /* For a typedef name, record the meaning, not the name.
2256 In case of 'foo foo, bar;'. */
2257 t.spec = lookup_name (value);
2259 else
2261 tree proto = NULL_TREE;
2262 gcc_assert (c_dialect_objc ());
2263 t.kind = ctsk_objc;
2264 if (c_parser_next_token_is (parser, CPP_LESS))
2265 proto = c_parser_objc_protocol_refs (parser);
2266 t.spec = objc_get_protocol_qualified_type (value, proto);
2268 t.expr = NULL_TREE;
2269 t.expr_const_operands = true;
2270 declspecs_add_type (name_token->location, specs, t);
2271 continue;
2273 if (c_parser_next_token_is (parser, CPP_LESS))
2275 /* Make "<SomeProtocol>" equivalent to "id <SomeProtocol>" -
2276 nisse@lysator.liu.se. */
2277 tree proto;
2278 gcc_assert (c_dialect_objc ());
2279 if (!typespec_ok || seen_type)
2280 break;
2281 proto = c_parser_objc_protocol_refs (parser);
2282 t.kind = ctsk_objc;
2283 t.spec = objc_get_protocol_qualified_type (NULL_TREE, proto);
2284 t.expr = NULL_TREE;
2285 t.expr_const_operands = true;
2286 declspecs_add_type (loc, specs, t);
2287 continue;
2289 gcc_assert (c_parser_next_token_is (parser, CPP_KEYWORD));
2290 switch (c_parser_peek_token (parser)->keyword)
2292 case RID_STATIC:
2293 case RID_EXTERN:
2294 case RID_REGISTER:
2295 case RID_TYPEDEF:
2296 case RID_INLINE:
2297 case RID_NORETURN:
2298 case RID_AUTO:
2299 case RID_THREAD:
2300 if (!scspec_ok)
2301 goto out;
2302 attrs_ok = true;
2303 /* TODO: Distinguish between function specifiers (inline, noreturn)
2304 and storage class specifiers, either here or in
2305 declspecs_add_scspec. */
2306 declspecs_add_scspec (loc, specs,
2307 c_parser_peek_token (parser)->value);
2308 c_parser_consume_token (parser);
2309 break;
2310 case RID_AUTO_TYPE:
2311 if (!auto_type_ok)
2312 goto out;
2313 /* Fall through. */
2314 case RID_UNSIGNED:
2315 case RID_LONG:
2316 case RID_INT128:
2317 case RID_SHORT:
2318 case RID_SIGNED:
2319 case RID_COMPLEX:
2320 case RID_INT:
2321 case RID_CHAR:
2322 case RID_FLOAT:
2323 case RID_DOUBLE:
2324 case RID_VOID:
2325 case RID_DFLOAT32:
2326 case RID_DFLOAT64:
2327 case RID_DFLOAT128:
2328 case RID_BOOL:
2329 case RID_FRACT:
2330 case RID_ACCUM:
2331 case RID_SAT:
2332 if (!typespec_ok)
2333 goto out;
2334 attrs_ok = true;
2335 seen_type = true;
2336 if (c_dialect_objc ())
2337 parser->objc_need_raw_identifier = true;
2338 t.kind = ctsk_resword;
2339 t.spec = c_parser_peek_token (parser)->value;
2340 t.expr = NULL_TREE;
2341 t.expr_const_operands = true;
2342 declspecs_add_type (loc, specs, t);
2343 c_parser_consume_token (parser);
2344 break;
2345 case RID_ENUM:
2346 if (!typespec_ok)
2347 goto out;
2348 attrs_ok = true;
2349 seen_type = true;
2350 t = c_parser_enum_specifier (parser);
2351 declspecs_add_type (loc, specs, t);
2352 break;
2353 case RID_STRUCT:
2354 case RID_UNION:
2355 if (!typespec_ok)
2356 goto out;
2357 attrs_ok = true;
2358 seen_type = true;
2359 t = c_parser_struct_or_union_specifier (parser);
2360 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, t.spec);
2361 declspecs_add_type (loc, specs, t);
2362 break;
2363 case RID_TYPEOF:
2364 /* ??? The old parser rejected typeof after other type
2365 specifiers, but is a syntax error the best way of
2366 handling this? */
2367 if (!typespec_ok || seen_type)
2368 goto out;
2369 attrs_ok = true;
2370 seen_type = true;
2371 t = c_parser_typeof_specifier (parser);
2372 declspecs_add_type (loc, specs, t);
2373 break;
2374 case RID_ATOMIC:
2375 /* C parser handling of Objective-C constructs needs
2376 checking for correct lvalue-to-rvalue conversions, and
2377 the code in build_modify_expr handling various
2378 Objective-C cases, and that in build_unary_op handling
2379 Objective-C cases for increment / decrement, also needs
2380 updating; uses of TYPE_MAIN_VARIANT in objc_compare_types
2381 and objc_types_are_equivalent may also need updates. */
2382 if (c_dialect_objc ())
2383 sorry ("%<_Atomic%> in Objective-C");
2384 /* C parser handling of OpenMP constructs needs checking for
2385 correct lvalue-to-rvalue conversions. */
2386 if (flag_openmp)
2387 sorry ("%<_Atomic%> with OpenMP");
2388 if (flag_isoc99)
2389 pedwarn_c99 (loc, OPT_Wpedantic,
2390 "ISO C99 does not support the %<_Atomic%> qualifier");
2391 else
2392 pedwarn_c99 (loc, OPT_Wpedantic,
2393 "ISO C90 does not support the %<_Atomic%> qualifier");
2394 attrs_ok = true;
2395 tree value;
2396 value = c_parser_peek_token (parser)->value;
2397 c_parser_consume_token (parser);
2398 if (typespec_ok && c_parser_next_token_is (parser, CPP_OPEN_PAREN))
2400 /* _Atomic ( type-name ). */
2401 seen_type = true;
2402 c_parser_consume_token (parser);
2403 struct c_type_name *type = c_parser_type_name (parser);
2404 t.kind = ctsk_typeof;
2405 t.spec = error_mark_node;
2406 t.expr = NULL_TREE;
2407 t.expr_const_operands = true;
2408 if (type != NULL)
2409 t.spec = groktypename (type, &t.expr,
2410 &t.expr_const_operands);
2411 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2412 "expected %<)%>");
2413 if (t.spec != error_mark_node)
2415 if (TREE_CODE (t.spec) == ARRAY_TYPE)
2416 error_at (loc, "%<_Atomic%>-qualified array type");
2417 else if (TREE_CODE (t.spec) == FUNCTION_TYPE)
2418 error_at (loc, "%<_Atomic%>-qualified function type");
2419 else if (TYPE_QUALS (t.spec) != TYPE_UNQUALIFIED)
2420 error_at (loc, "%<_Atomic%> applied to a qualified type");
2421 else
2422 t.spec = c_build_qualified_type (t.spec, TYPE_QUAL_ATOMIC);
2424 declspecs_add_type (loc, specs, t);
2426 else
2427 declspecs_add_qual (loc, specs, value);
2428 break;
2429 case RID_CONST:
2430 case RID_VOLATILE:
2431 case RID_RESTRICT:
2432 attrs_ok = true;
2433 declspecs_add_qual (loc, specs, c_parser_peek_token (parser)->value);
2434 c_parser_consume_token (parser);
2435 break;
2436 case RID_ATTRIBUTE:
2437 if (!attrs_ok)
2438 goto out;
2439 attrs = c_parser_attributes (parser);
2440 declspecs_add_attrs (loc, specs, attrs);
2441 break;
2442 case RID_ALIGNAS:
2443 if (!alignspec_ok)
2444 goto out;
2445 align = c_parser_alignas_specifier (parser);
2446 declspecs_add_alignas (loc, specs, align);
2447 break;
2448 default:
2449 goto out;
2452 out: ;
2455 /* Parse an enum specifier (C90 6.5.2.2, C99 6.7.2.2).
2457 enum-specifier:
2458 enum attributes[opt] identifier[opt] { enumerator-list } attributes[opt]
2459 enum attributes[opt] identifier[opt] { enumerator-list , } attributes[opt]
2460 enum attributes[opt] identifier
2462 The form with trailing comma is new in C99. The forms with
2463 attributes are GNU extensions. In GNU C, we accept any expression
2464 without commas in the syntax (assignment expressions, not just
2465 conditional expressions); assignment expressions will be diagnosed
2466 as non-constant.
2468 enumerator-list:
2469 enumerator
2470 enumerator-list , enumerator
2472 enumerator:
2473 enumeration-constant
2474 enumeration-constant = constant-expression
2477 static struct c_typespec
2478 c_parser_enum_specifier (c_parser *parser)
2480 struct c_typespec ret;
2481 tree attrs;
2482 tree ident = NULL_TREE;
2483 location_t enum_loc;
2484 location_t ident_loc = UNKNOWN_LOCATION; /* Quiet warning. */
2485 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ENUM));
2486 enum_loc = c_parser_peek_token (parser)->location;
2487 c_parser_consume_token (parser);
2488 attrs = c_parser_attributes (parser);
2489 enum_loc = c_parser_peek_token (parser)->location;
2490 /* Set the location in case we create a decl now. */
2491 c_parser_set_source_position_from_token (c_parser_peek_token (parser));
2492 if (c_parser_next_token_is (parser, CPP_NAME))
2494 ident = c_parser_peek_token (parser)->value;
2495 ident_loc = c_parser_peek_token (parser)->location;
2496 enum_loc = ident_loc;
2497 c_parser_consume_token (parser);
2499 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
2501 /* Parse an enum definition. */
2502 struct c_enum_contents the_enum;
2503 tree type;
2504 tree postfix_attrs;
2505 /* We chain the enumerators in reverse order, then put them in
2506 forward order at the end. */
2507 tree values;
2508 timevar_push (TV_PARSE_ENUM);
2509 type = start_enum (enum_loc, &the_enum, ident);
2510 values = NULL_TREE;
2511 c_parser_consume_token (parser);
2512 while (true)
2514 tree enum_id;
2515 tree enum_value;
2516 tree enum_decl;
2517 bool seen_comma;
2518 c_token *token;
2519 location_t comma_loc = UNKNOWN_LOCATION; /* Quiet warning. */
2520 location_t decl_loc, value_loc;
2521 if (c_parser_next_token_is_not (parser, CPP_NAME))
2523 c_parser_error (parser, "expected identifier");
2524 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
2525 values = error_mark_node;
2526 break;
2528 token = c_parser_peek_token (parser);
2529 enum_id = token->value;
2530 /* Set the location in case we create a decl now. */
2531 c_parser_set_source_position_from_token (token);
2532 decl_loc = value_loc = token->location;
2533 c_parser_consume_token (parser);
2534 if (c_parser_next_token_is (parser, CPP_EQ))
2536 c_parser_consume_token (parser);
2537 value_loc = c_parser_peek_token (parser)->location;
2538 enum_value = c_parser_expr_no_commas (parser, NULL).value;
2540 else
2541 enum_value = NULL_TREE;
2542 enum_decl = build_enumerator (decl_loc, value_loc,
2543 &the_enum, enum_id, enum_value);
2544 TREE_CHAIN (enum_decl) = values;
2545 values = enum_decl;
2546 seen_comma = false;
2547 if (c_parser_next_token_is (parser, CPP_COMMA))
2549 comma_loc = c_parser_peek_token (parser)->location;
2550 seen_comma = true;
2551 c_parser_consume_token (parser);
2553 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2555 if (seen_comma)
2556 pedwarn_c90 (comma_loc, OPT_Wpedantic,
2557 "comma at end of enumerator list");
2558 c_parser_consume_token (parser);
2559 break;
2561 if (!seen_comma)
2563 c_parser_error (parser, "expected %<,%> or %<}%>");
2564 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
2565 values = error_mark_node;
2566 break;
2569 postfix_attrs = c_parser_attributes (parser);
2570 ret.spec = finish_enum (type, nreverse (values),
2571 chainon (attrs, postfix_attrs));
2572 ret.kind = ctsk_tagdef;
2573 ret.expr = NULL_TREE;
2574 ret.expr_const_operands = true;
2575 timevar_pop (TV_PARSE_ENUM);
2576 return ret;
2578 else if (!ident)
2580 c_parser_error (parser, "expected %<{%>");
2581 ret.spec = error_mark_node;
2582 ret.kind = ctsk_tagref;
2583 ret.expr = NULL_TREE;
2584 ret.expr_const_operands = true;
2585 return ret;
2587 ret = parser_xref_tag (ident_loc, ENUMERAL_TYPE, ident);
2588 /* In ISO C, enumerated types can be referred to only if already
2589 defined. */
2590 if (pedantic && !COMPLETE_TYPE_P (ret.spec))
2592 gcc_assert (ident);
2593 pedwarn (enum_loc, OPT_Wpedantic,
2594 "ISO C forbids forward references to %<enum%> types");
2596 return ret;
2599 /* Parse a struct or union specifier (C90 6.5.2.1, C99 6.7.2.1).
2601 struct-or-union-specifier:
2602 struct-or-union attributes[opt] identifier[opt]
2603 { struct-contents } attributes[opt]
2604 struct-or-union attributes[opt] identifier
2606 struct-contents:
2607 struct-declaration-list
2609 struct-declaration-list:
2610 struct-declaration ;
2611 struct-declaration-list struct-declaration ;
2613 GNU extensions:
2615 struct-contents:
2616 empty
2617 struct-declaration
2618 struct-declaration-list struct-declaration
2620 struct-declaration-list:
2621 struct-declaration-list ;
2624 (Note that in the syntax here, unlike that in ISO C, the semicolons
2625 are included here rather than in struct-declaration, in order to
2626 describe the syntax with extra semicolons and missing semicolon at
2627 end.)
2629 Objective-C:
2631 struct-declaration-list:
2632 @defs ( class-name )
2634 (Note this does not include a trailing semicolon, but can be
2635 followed by further declarations, and gets a pedwarn-if-pedantic
2636 when followed by a semicolon.) */
2638 static struct c_typespec
2639 c_parser_struct_or_union_specifier (c_parser *parser)
2641 struct c_typespec ret;
2642 tree attrs;
2643 tree ident = NULL_TREE;
2644 location_t struct_loc;
2645 location_t ident_loc = UNKNOWN_LOCATION;
2646 enum tree_code code;
2647 switch (c_parser_peek_token (parser)->keyword)
2649 case RID_STRUCT:
2650 code = RECORD_TYPE;
2651 break;
2652 case RID_UNION:
2653 code = UNION_TYPE;
2654 break;
2655 default:
2656 gcc_unreachable ();
2658 struct_loc = c_parser_peek_token (parser)->location;
2659 c_parser_consume_token (parser);
2660 attrs = c_parser_attributes (parser);
2662 /* Set the location in case we create a decl now. */
2663 c_parser_set_source_position_from_token (c_parser_peek_token (parser));
2665 if (c_parser_next_token_is (parser, CPP_NAME))
2667 ident = c_parser_peek_token (parser)->value;
2668 ident_loc = c_parser_peek_token (parser)->location;
2669 struct_loc = ident_loc;
2670 c_parser_consume_token (parser);
2672 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
2674 /* Parse a struct or union definition. Start the scope of the
2675 tag before parsing components. */
2676 struct c_struct_parse_info *struct_info;
2677 tree type = start_struct (struct_loc, code, ident, &struct_info);
2678 tree postfix_attrs;
2679 /* We chain the components in reverse order, then put them in
2680 forward order at the end. Each struct-declaration may
2681 declare multiple components (comma-separated), so we must use
2682 chainon to join them, although when parsing each
2683 struct-declaration we can use TREE_CHAIN directly.
2685 The theory behind all this is that there will be more
2686 semicolon separated fields than comma separated fields, and
2687 so we'll be minimizing the number of node traversals required
2688 by chainon. */
2689 tree contents;
2690 timevar_push (TV_PARSE_STRUCT);
2691 contents = NULL_TREE;
2692 c_parser_consume_token (parser);
2693 /* Handle the Objective-C @defs construct,
2694 e.g. foo(sizeof(struct{ @defs(ClassName) }));. */
2695 if (c_parser_next_token_is_keyword (parser, RID_AT_DEFS))
2697 tree name;
2698 gcc_assert (c_dialect_objc ());
2699 c_parser_consume_token (parser);
2700 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2701 goto end_at_defs;
2702 if (c_parser_next_token_is (parser, CPP_NAME)
2703 && c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME)
2705 name = c_parser_peek_token (parser)->value;
2706 c_parser_consume_token (parser);
2708 else
2710 c_parser_error (parser, "expected class name");
2711 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
2712 goto end_at_defs;
2714 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2715 "expected %<)%>");
2716 contents = nreverse (objc_get_class_ivars (name));
2718 end_at_defs:
2719 /* Parse the struct-declarations and semicolons. Problems with
2720 semicolons are diagnosed here; empty structures are diagnosed
2721 elsewhere. */
2722 while (true)
2724 tree decls;
2725 /* Parse any stray semicolon. */
2726 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
2728 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
2729 "extra semicolon in struct or union specified");
2730 c_parser_consume_token (parser);
2731 continue;
2733 /* Stop if at the end of the struct or union contents. */
2734 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2736 c_parser_consume_token (parser);
2737 break;
2739 /* Accept #pragmas at struct scope. */
2740 if (c_parser_next_token_is (parser, CPP_PRAGMA))
2742 c_parser_pragma (parser, pragma_struct);
2743 continue;
2745 /* Parse some comma-separated declarations, but not the
2746 trailing semicolon if any. */
2747 decls = c_parser_struct_declaration (parser);
2748 contents = chainon (decls, contents);
2749 /* If no semicolon follows, either we have a parse error or
2750 are at the end of the struct or union and should
2751 pedwarn. */
2752 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
2753 c_parser_consume_token (parser);
2754 else
2756 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2757 pedwarn (c_parser_peek_token (parser)->location, 0,
2758 "no semicolon at end of struct or union");
2759 else if (parser->error
2760 || !c_parser_next_token_starts_declspecs (parser))
2762 c_parser_error (parser, "expected %<;%>");
2763 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
2764 break;
2767 /* If we come here, we have already emitted an error
2768 for an expected `;', identifier or `(', and we also
2769 recovered already. Go on with the next field. */
2772 postfix_attrs = c_parser_attributes (parser);
2773 ret.spec = finish_struct (struct_loc, type, nreverse (contents),
2774 chainon (attrs, postfix_attrs), struct_info);
2775 ret.kind = ctsk_tagdef;
2776 ret.expr = NULL_TREE;
2777 ret.expr_const_operands = true;
2778 timevar_pop (TV_PARSE_STRUCT);
2779 return ret;
2781 else if (!ident)
2783 c_parser_error (parser, "expected %<{%>");
2784 ret.spec = error_mark_node;
2785 ret.kind = ctsk_tagref;
2786 ret.expr = NULL_TREE;
2787 ret.expr_const_operands = true;
2788 return ret;
2790 ret = parser_xref_tag (ident_loc, code, ident);
2791 return ret;
2794 /* Parse a struct-declaration (C90 6.5.2.1, C99 6.7.2.1), *without*
2795 the trailing semicolon.
2797 struct-declaration:
2798 specifier-qualifier-list struct-declarator-list
2799 static_assert-declaration-no-semi
2801 specifier-qualifier-list:
2802 type-specifier specifier-qualifier-list[opt]
2803 type-qualifier specifier-qualifier-list[opt]
2804 attributes specifier-qualifier-list[opt]
2806 struct-declarator-list:
2807 struct-declarator
2808 struct-declarator-list , attributes[opt] struct-declarator
2810 struct-declarator:
2811 declarator attributes[opt]
2812 declarator[opt] : constant-expression attributes[opt]
2814 GNU extensions:
2816 struct-declaration:
2817 __extension__ struct-declaration
2818 specifier-qualifier-list
2820 Unlike the ISO C syntax, semicolons are handled elsewhere. The use
2821 of attributes where shown is a GNU extension. In GNU C, we accept
2822 any expression without commas in the syntax (assignment
2823 expressions, not just conditional expressions); assignment
2824 expressions will be diagnosed as non-constant. */
2826 static tree
2827 c_parser_struct_declaration (c_parser *parser)
2829 struct c_declspecs *specs;
2830 tree prefix_attrs;
2831 tree all_prefix_attrs;
2832 tree decls;
2833 location_t decl_loc;
2834 if (c_parser_next_token_is_keyword (parser, RID_EXTENSION))
2836 int ext;
2837 tree decl;
2838 ext = disable_extension_diagnostics ();
2839 c_parser_consume_token (parser);
2840 decl = c_parser_struct_declaration (parser);
2841 restore_extension_diagnostics (ext);
2842 return decl;
2844 if (c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT))
2846 c_parser_static_assert_declaration_no_semi (parser);
2847 return NULL_TREE;
2849 specs = build_null_declspecs ();
2850 decl_loc = c_parser_peek_token (parser)->location;
2851 /* Strictly by the standard, we shouldn't allow _Alignas here,
2852 but it appears to have been intended to allow it there, so
2853 we're keeping it as it is until WG14 reaches a conclusion
2854 of N1731.
2855 <http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1731.pdf> */
2856 c_parser_declspecs (parser, specs, false, true, true,
2857 true, false, cla_nonabstract_decl);
2858 if (parser->error)
2859 return NULL_TREE;
2860 if (!specs->declspecs_seen_p)
2862 c_parser_error (parser, "expected specifier-qualifier-list");
2863 return NULL_TREE;
2865 finish_declspecs (specs);
2866 if (c_parser_next_token_is (parser, CPP_SEMICOLON)
2867 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2869 tree ret;
2870 if (specs->typespec_kind == ctsk_none)
2872 pedwarn (decl_loc, OPT_Wpedantic,
2873 "ISO C forbids member declarations with no members");
2874 shadow_tag_warned (specs, pedantic);
2875 ret = NULL_TREE;
2877 else
2879 /* Support for unnamed structs or unions as members of
2880 structs or unions (which is [a] useful and [b] supports
2881 MS P-SDK). */
2882 tree attrs = NULL;
2884 ret = grokfield (c_parser_peek_token (parser)->location,
2885 build_id_declarator (NULL_TREE), specs,
2886 NULL_TREE, &attrs);
2887 if (ret)
2888 decl_attributes (&ret, attrs, 0);
2890 return ret;
2893 /* Provide better error recovery. Note that a type name here is valid,
2894 and will be treated as a field name. */
2895 if (specs->typespec_kind == ctsk_tagdef
2896 && TREE_CODE (specs->type) != ENUMERAL_TYPE
2897 && c_parser_next_token_starts_declspecs (parser)
2898 && !c_parser_next_token_is (parser, CPP_NAME))
2900 c_parser_error (parser, "expected %<;%>, identifier or %<(%>");
2901 parser->error = false;
2902 return NULL_TREE;
2905 pending_xref_error ();
2906 prefix_attrs = specs->attrs;
2907 all_prefix_attrs = prefix_attrs;
2908 specs->attrs = NULL_TREE;
2909 decls = NULL_TREE;
2910 while (true)
2912 /* Declaring one or more declarators or un-named bit-fields. */
2913 struct c_declarator *declarator;
2914 bool dummy = false;
2915 if (c_parser_next_token_is (parser, CPP_COLON))
2916 declarator = build_id_declarator (NULL_TREE);
2917 else
2918 declarator = c_parser_declarator (parser,
2919 specs->typespec_kind != ctsk_none,
2920 C_DTR_NORMAL, &dummy);
2921 if (declarator == NULL)
2923 c_parser_skip_to_end_of_block_or_statement (parser);
2924 break;
2926 if (c_parser_next_token_is (parser, CPP_COLON)
2927 || c_parser_next_token_is (parser, CPP_COMMA)
2928 || c_parser_next_token_is (parser, CPP_SEMICOLON)
2929 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE)
2930 || c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2932 tree postfix_attrs = NULL_TREE;
2933 tree width = NULL_TREE;
2934 tree d;
2935 if (c_parser_next_token_is (parser, CPP_COLON))
2937 c_parser_consume_token (parser);
2938 width = c_parser_expr_no_commas (parser, NULL).value;
2940 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2941 postfix_attrs = c_parser_attributes (parser);
2942 d = grokfield (c_parser_peek_token (parser)->location,
2943 declarator, specs, width, &all_prefix_attrs);
2944 decl_attributes (&d, chainon (postfix_attrs,
2945 all_prefix_attrs), 0);
2946 DECL_CHAIN (d) = decls;
2947 decls = d;
2948 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2949 all_prefix_attrs = chainon (c_parser_attributes (parser),
2950 prefix_attrs);
2951 else
2952 all_prefix_attrs = prefix_attrs;
2953 if (c_parser_next_token_is (parser, CPP_COMMA))
2954 c_parser_consume_token (parser);
2955 else if (c_parser_next_token_is (parser, CPP_SEMICOLON)
2956 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2958 /* Semicolon consumed in caller. */
2959 break;
2961 else
2963 c_parser_error (parser, "expected %<,%>, %<;%> or %<}%>");
2964 break;
2967 else
2969 c_parser_error (parser,
2970 "expected %<:%>, %<,%>, %<;%>, %<}%> or "
2971 "%<__attribute__%>");
2972 break;
2975 return decls;
2978 /* Parse a typeof specifier (a GNU extension).
2980 typeof-specifier:
2981 typeof ( expression )
2982 typeof ( type-name )
2985 static struct c_typespec
2986 c_parser_typeof_specifier (c_parser *parser)
2988 struct c_typespec ret;
2989 ret.kind = ctsk_typeof;
2990 ret.spec = error_mark_node;
2991 ret.expr = NULL_TREE;
2992 ret.expr_const_operands = true;
2993 gcc_assert (c_parser_next_token_is_keyword (parser, RID_TYPEOF));
2994 c_parser_consume_token (parser);
2995 c_inhibit_evaluation_warnings++;
2996 in_typeof++;
2997 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2999 c_inhibit_evaluation_warnings--;
3000 in_typeof--;
3001 return ret;
3003 if (c_parser_next_tokens_start_typename (parser, cla_prefer_id))
3005 struct c_type_name *type = c_parser_type_name (parser);
3006 c_inhibit_evaluation_warnings--;
3007 in_typeof--;
3008 if (type != NULL)
3010 ret.spec = groktypename (type, &ret.expr, &ret.expr_const_operands);
3011 pop_maybe_used (variably_modified_type_p (ret.spec, NULL_TREE));
3014 else
3016 bool was_vm;
3017 location_t here = c_parser_peek_token (parser)->location;
3018 struct c_expr expr = c_parser_expression (parser);
3019 c_inhibit_evaluation_warnings--;
3020 in_typeof--;
3021 if (TREE_CODE (expr.value) == COMPONENT_REF
3022 && DECL_C_BIT_FIELD (TREE_OPERAND (expr.value, 1)))
3023 error_at (here, "%<typeof%> applied to a bit-field");
3024 mark_exp_read (expr.value);
3025 ret.spec = TREE_TYPE (expr.value);
3026 was_vm = variably_modified_type_p (ret.spec, NULL_TREE);
3027 /* This is returned with the type so that when the type is
3028 evaluated, this can be evaluated. */
3029 if (was_vm)
3030 ret.expr = c_fully_fold (expr.value, false, &ret.expr_const_operands);
3031 pop_maybe_used (was_vm);
3032 /* For use in macros such as those in <stdatomic.h>, remove all
3033 qualifiers from atomic types. (const can be an issue for more macros
3034 using typeof than just the <stdatomic.h> ones.) */
3035 if (ret.spec != error_mark_node && TYPE_ATOMIC (ret.spec))
3036 ret.spec = c_build_qualified_type (ret.spec, TYPE_UNQUALIFIED);
3038 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
3039 return ret;
3042 /* Parse an alignment-specifier.
3044 C11 6.7.5:
3046 alignment-specifier:
3047 _Alignas ( type-name )
3048 _Alignas ( constant-expression )
3051 static tree
3052 c_parser_alignas_specifier (c_parser * parser)
3054 tree ret = error_mark_node;
3055 location_t loc = c_parser_peek_token (parser)->location;
3056 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ALIGNAS));
3057 c_parser_consume_token (parser);
3058 if (flag_isoc99)
3059 pedwarn_c99 (loc, OPT_Wpedantic,
3060 "ISO C99 does not support %<_Alignas%>");
3061 else
3062 pedwarn_c99 (loc, OPT_Wpedantic,
3063 "ISO C90 does not support %<_Alignas%>");
3064 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3065 return ret;
3066 if (c_parser_next_tokens_start_typename (parser, cla_prefer_id))
3068 struct c_type_name *type = c_parser_type_name (parser);
3069 if (type != NULL)
3070 ret = c_sizeof_or_alignof_type (loc, groktypename (type, NULL, NULL),
3071 false, true, 1);
3073 else
3074 ret = c_parser_expr_no_commas (parser, NULL).value;
3075 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
3076 return ret;
3079 /* Parse a declarator, possibly an abstract declarator (C90 6.5.4,
3080 6.5.5, C99 6.7.5, 6.7.6). If TYPE_SEEN_P then a typedef name may
3081 be redeclared; otherwise it may not. KIND indicates which kind of
3082 declarator is wanted. Returns a valid declarator except in the
3083 case of a syntax error in which case NULL is returned. *SEEN_ID is
3084 set to true if an identifier being declared is seen; this is used
3085 to diagnose bad forms of abstract array declarators and to
3086 determine whether an identifier list is syntactically permitted.
3088 declarator:
3089 pointer[opt] direct-declarator
3091 direct-declarator:
3092 identifier
3093 ( attributes[opt] declarator )
3094 direct-declarator array-declarator
3095 direct-declarator ( parameter-type-list )
3096 direct-declarator ( identifier-list[opt] )
3098 pointer:
3099 * type-qualifier-list[opt]
3100 * type-qualifier-list[opt] pointer
3102 type-qualifier-list:
3103 type-qualifier
3104 attributes
3105 type-qualifier-list type-qualifier
3106 type-qualifier-list attributes
3108 array-declarator:
3109 [ type-qualifier-list[opt] assignment-expression[opt] ]
3110 [ static type-qualifier-list[opt] assignment-expression ]
3111 [ type-qualifier-list static assignment-expression ]
3112 [ type-qualifier-list[opt] * ]
3114 parameter-type-list:
3115 parameter-list
3116 parameter-list , ...
3118 parameter-list:
3119 parameter-declaration
3120 parameter-list , parameter-declaration
3122 parameter-declaration:
3123 declaration-specifiers declarator attributes[opt]
3124 declaration-specifiers abstract-declarator[opt] attributes[opt]
3126 identifier-list:
3127 identifier
3128 identifier-list , identifier
3130 abstract-declarator:
3131 pointer
3132 pointer[opt] direct-abstract-declarator
3134 direct-abstract-declarator:
3135 ( attributes[opt] abstract-declarator )
3136 direct-abstract-declarator[opt] array-declarator
3137 direct-abstract-declarator[opt] ( parameter-type-list[opt] )
3139 GNU extensions:
3141 direct-declarator:
3142 direct-declarator ( parameter-forward-declarations
3143 parameter-type-list[opt] )
3145 direct-abstract-declarator:
3146 direct-abstract-declarator[opt] ( parameter-forward-declarations
3147 parameter-type-list[opt] )
3149 parameter-forward-declarations:
3150 parameter-list ;
3151 parameter-forward-declarations parameter-list ;
3153 The uses of attributes shown above are GNU extensions.
3155 Some forms of array declarator are not included in C99 in the
3156 syntax for abstract declarators; these are disallowed elsewhere.
3157 This may be a defect (DR#289).
3159 This function also accepts an omitted abstract declarator as being
3160 an abstract declarator, although not part of the formal syntax. */
3162 static struct c_declarator *
3163 c_parser_declarator (c_parser *parser, bool type_seen_p, c_dtr_syn kind,
3164 bool *seen_id)
3166 /* Parse any initial pointer part. */
3167 if (c_parser_next_token_is (parser, CPP_MULT))
3169 struct c_declspecs *quals_attrs = build_null_declspecs ();
3170 struct c_declarator *inner;
3171 c_parser_consume_token (parser);
3172 c_parser_declspecs (parser, quals_attrs, false, false, true,
3173 false, false, cla_prefer_id);
3174 inner = c_parser_declarator (parser, type_seen_p, kind, seen_id);
3175 if (inner == NULL)
3176 return NULL;
3177 else
3178 return make_pointer_declarator (quals_attrs, inner);
3180 /* Now we have a direct declarator, direct abstract declarator or
3181 nothing (which counts as a direct abstract declarator here). */
3182 return c_parser_direct_declarator (parser, type_seen_p, kind, seen_id);
3185 /* Parse a direct declarator or direct abstract declarator; arguments
3186 as c_parser_declarator. */
3188 static struct c_declarator *
3189 c_parser_direct_declarator (c_parser *parser, bool type_seen_p, c_dtr_syn kind,
3190 bool *seen_id)
3192 /* The direct declarator must start with an identifier (possibly
3193 omitted) or a parenthesized declarator (possibly abstract). In
3194 an ordinary declarator, initial parentheses must start a
3195 parenthesized declarator. In an abstract declarator or parameter
3196 declarator, they could start a parenthesized declarator or a
3197 parameter list. To tell which, the open parenthesis and any
3198 following attributes must be read. If a declaration specifier
3199 follows, then it is a parameter list; if the specifier is a
3200 typedef name, there might be an ambiguity about redeclaring it,
3201 which is resolved in the direction of treating it as a typedef
3202 name. If a close parenthesis follows, it is also an empty
3203 parameter list, as the syntax does not permit empty abstract
3204 declarators. Otherwise, it is a parenthesized declarator (in
3205 which case the analysis may be repeated inside it, recursively).
3207 ??? There is an ambiguity in a parameter declaration "int
3208 (__attribute__((foo)) x)", where x is not a typedef name: it
3209 could be an abstract declarator for a function, or declare x with
3210 parentheses. The proper resolution of this ambiguity needs
3211 documenting. At present we follow an accident of the old
3212 parser's implementation, whereby the first parameter must have
3213 some declaration specifiers other than just attributes. Thus as
3214 a parameter declaration it is treated as a parenthesized
3215 parameter named x, and as an abstract declarator it is
3216 rejected.
3218 ??? Also following the old parser, attributes inside an empty
3219 parameter list are ignored, making it a list not yielding a
3220 prototype, rather than giving an error or making it have one
3221 parameter with implicit type int.
3223 ??? Also following the old parser, typedef names may be
3224 redeclared in declarators, but not Objective-C class names. */
3226 if (kind != C_DTR_ABSTRACT
3227 && c_parser_next_token_is (parser, CPP_NAME)
3228 && ((type_seen_p
3229 && (c_parser_peek_token (parser)->id_kind == C_ID_TYPENAME
3230 || c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME))
3231 || c_parser_peek_token (parser)->id_kind == C_ID_ID))
3233 struct c_declarator *inner
3234 = build_id_declarator (c_parser_peek_token (parser)->value);
3235 *seen_id = true;
3236 inner->id_loc = c_parser_peek_token (parser)->location;
3237 c_parser_consume_token (parser);
3238 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
3241 if (kind != C_DTR_NORMAL
3242 && c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
3244 struct c_declarator *inner = build_id_declarator (NULL_TREE);
3245 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
3248 /* Either we are at the end of an abstract declarator, or we have
3249 parentheses. */
3251 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
3253 tree attrs;
3254 struct c_declarator *inner;
3255 c_parser_consume_token (parser);
3256 attrs = c_parser_attributes (parser);
3257 if (kind != C_DTR_NORMAL
3258 && (c_parser_next_token_starts_declspecs (parser)
3259 || c_parser_next_token_is (parser, CPP_CLOSE_PAREN)))
3261 struct c_arg_info *args
3262 = c_parser_parms_declarator (parser, kind == C_DTR_NORMAL,
3263 attrs);
3264 if (args == NULL)
3265 return NULL;
3266 else
3268 inner
3269 = build_function_declarator (args,
3270 build_id_declarator (NULL_TREE));
3271 return c_parser_direct_declarator_inner (parser, *seen_id,
3272 inner);
3275 /* A parenthesized declarator. */
3276 inner = c_parser_declarator (parser, type_seen_p, kind, seen_id);
3277 if (inner != NULL && attrs != NULL)
3278 inner = build_attrs_declarator (attrs, inner);
3279 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3281 c_parser_consume_token (parser);
3282 if (inner == NULL)
3283 return NULL;
3284 else
3285 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
3287 else
3289 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3290 "expected %<)%>");
3291 return NULL;
3294 else
3296 if (kind == C_DTR_NORMAL)
3298 c_parser_error (parser, "expected identifier or %<(%>");
3299 return NULL;
3301 else
3302 return build_id_declarator (NULL_TREE);
3306 /* Parse part of a direct declarator or direct abstract declarator,
3307 given that some (in INNER) has already been parsed; ID_PRESENT is
3308 true if an identifier is present, false for an abstract
3309 declarator. */
3311 static struct c_declarator *
3312 c_parser_direct_declarator_inner (c_parser *parser, bool id_present,
3313 struct c_declarator *inner)
3315 /* Parse a sequence of array declarators and parameter lists. */
3316 if (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
3318 location_t brace_loc = c_parser_peek_token (parser)->location;
3319 struct c_declarator *declarator;
3320 struct c_declspecs *quals_attrs = build_null_declspecs ();
3321 bool static_seen;
3322 bool star_seen;
3323 struct c_expr dimen;
3324 dimen.value = NULL_TREE;
3325 dimen.original_code = ERROR_MARK;
3326 dimen.original_type = NULL_TREE;
3327 c_parser_consume_token (parser);
3328 c_parser_declspecs (parser, quals_attrs, false, false, true,
3329 false, false, cla_prefer_id);
3330 static_seen = c_parser_next_token_is_keyword (parser, RID_STATIC);
3331 if (static_seen)
3332 c_parser_consume_token (parser);
3333 if (static_seen && !quals_attrs->declspecs_seen_p)
3334 c_parser_declspecs (parser, quals_attrs, false, false, true,
3335 false, false, cla_prefer_id);
3336 if (!quals_attrs->declspecs_seen_p)
3337 quals_attrs = NULL;
3338 /* If "static" is present, there must be an array dimension.
3339 Otherwise, there may be a dimension, "*", or no
3340 dimension. */
3341 if (static_seen)
3343 star_seen = false;
3344 dimen = c_parser_expr_no_commas (parser, NULL);
3346 else
3348 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
3350 dimen.value = NULL_TREE;
3351 star_seen = false;
3353 else if (flag_cilkplus
3354 && c_parser_next_token_is (parser, CPP_COLON))
3356 dimen.value = error_mark_node;
3357 star_seen = false;
3358 error_at (c_parser_peek_token (parser)->location,
3359 "array notations cannot be used in declaration");
3360 c_parser_consume_token (parser);
3362 else if (c_parser_next_token_is (parser, CPP_MULT))
3364 if (c_parser_peek_2nd_token (parser)->type == CPP_CLOSE_SQUARE)
3366 dimen.value = NULL_TREE;
3367 star_seen = true;
3368 c_parser_consume_token (parser);
3370 else
3372 star_seen = false;
3373 dimen = c_parser_expr_no_commas (parser, NULL);
3376 else
3378 star_seen = false;
3379 dimen = c_parser_expr_no_commas (parser, NULL);
3382 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
3383 c_parser_consume_token (parser);
3384 else if (flag_cilkplus
3385 && c_parser_next_token_is (parser, CPP_COLON))
3387 error_at (c_parser_peek_token (parser)->location,
3388 "array notations cannot be used in declaration");
3389 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
3390 return NULL;
3392 else
3394 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
3395 "expected %<]%>");
3396 return NULL;
3398 if (dimen.value)
3399 dimen = convert_lvalue_to_rvalue (brace_loc, dimen, true, true);
3400 declarator = build_array_declarator (brace_loc, dimen.value, quals_attrs,
3401 static_seen, star_seen);
3402 if (declarator == NULL)
3403 return NULL;
3404 inner = set_array_declarator_inner (declarator, inner);
3405 return c_parser_direct_declarator_inner (parser, id_present, inner);
3407 else if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
3409 tree attrs;
3410 struct c_arg_info *args;
3411 c_parser_consume_token (parser);
3412 attrs = c_parser_attributes (parser);
3413 args = c_parser_parms_declarator (parser, id_present, attrs);
3414 if (args == NULL)
3415 return NULL;
3416 else
3418 inner = build_function_declarator (args, inner);
3419 return c_parser_direct_declarator_inner (parser, id_present, inner);
3422 return inner;
3425 /* Parse a parameter list or identifier list, including the closing
3426 parenthesis but not the opening one. ATTRS are the attributes at
3427 the start of the list. ID_LIST_OK is true if an identifier list is
3428 acceptable; such a list must not have attributes at the start. */
3430 static struct c_arg_info *
3431 c_parser_parms_declarator (c_parser *parser, bool id_list_ok, tree attrs)
3433 push_scope ();
3434 declare_parm_level ();
3435 /* If the list starts with an identifier, it is an identifier list.
3436 Otherwise, it is either a prototype list or an empty list. */
3437 if (id_list_ok
3438 && !attrs
3439 && c_parser_next_token_is (parser, CPP_NAME)
3440 && c_parser_peek_token (parser)->id_kind == C_ID_ID
3442 /* Look ahead to detect typos in type names. */
3443 && c_parser_peek_2nd_token (parser)->type != CPP_NAME
3444 && c_parser_peek_2nd_token (parser)->type != CPP_MULT
3445 && c_parser_peek_2nd_token (parser)->type != CPP_OPEN_PAREN
3446 && c_parser_peek_2nd_token (parser)->type != CPP_OPEN_SQUARE)
3448 tree list = NULL_TREE, *nextp = &list;
3449 while (c_parser_next_token_is (parser, CPP_NAME)
3450 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
3452 *nextp = build_tree_list (NULL_TREE,
3453 c_parser_peek_token (parser)->value);
3454 nextp = & TREE_CHAIN (*nextp);
3455 c_parser_consume_token (parser);
3456 if (c_parser_next_token_is_not (parser, CPP_COMMA))
3457 break;
3458 c_parser_consume_token (parser);
3459 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3461 c_parser_error (parser, "expected identifier");
3462 break;
3465 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3467 struct c_arg_info *ret = build_arg_info ();
3468 ret->types = list;
3469 c_parser_consume_token (parser);
3470 pop_scope ();
3471 return ret;
3473 else
3475 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3476 "expected %<)%>");
3477 pop_scope ();
3478 return NULL;
3481 else
3483 struct c_arg_info *ret = c_parser_parms_list_declarator (parser, attrs,
3484 NULL);
3485 pop_scope ();
3486 return ret;
3490 /* Parse a parameter list (possibly empty), including the closing
3491 parenthesis but not the opening one. ATTRS are the attributes at
3492 the start of the list. EXPR is NULL or an expression that needs to
3493 be evaluated for the side effects of array size expressions in the
3494 parameters. */
3496 static struct c_arg_info *
3497 c_parser_parms_list_declarator (c_parser *parser, tree attrs, tree expr)
3499 bool bad_parm = false;
3501 /* ??? Following the old parser, forward parameter declarations may
3502 use abstract declarators, and if no real parameter declarations
3503 follow the forward declarations then this is not diagnosed. Also
3504 note as above that attributes are ignored as the only contents of
3505 the parentheses, or as the only contents after forward
3506 declarations. */
3507 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3509 struct c_arg_info *ret = build_arg_info ();
3510 c_parser_consume_token (parser);
3511 return ret;
3513 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
3515 struct c_arg_info *ret = build_arg_info ();
3517 if (flag_allow_parameterless_variadic_functions)
3519 /* F (...) is allowed. */
3520 ret->types = NULL_TREE;
3522 else
3524 /* Suppress -Wold-style-definition for this case. */
3525 ret->types = error_mark_node;
3526 error_at (c_parser_peek_token (parser)->location,
3527 "ISO C requires a named argument before %<...%>");
3529 c_parser_consume_token (parser);
3530 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3532 c_parser_consume_token (parser);
3533 return ret;
3535 else
3537 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3538 "expected %<)%>");
3539 return NULL;
3542 /* Nonempty list of parameters, either terminated with semicolon
3543 (forward declarations; recurse) or with close parenthesis (normal
3544 function) or with ", ... )" (variadic function). */
3545 while (true)
3547 /* Parse a parameter. */
3548 struct c_parm *parm = c_parser_parameter_declaration (parser, attrs);
3549 attrs = NULL_TREE;
3550 if (parm == NULL)
3551 bad_parm = true;
3552 else
3553 push_parm_decl (parm, &expr);
3554 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
3556 tree new_attrs;
3557 c_parser_consume_token (parser);
3558 mark_forward_parm_decls ();
3559 new_attrs = c_parser_attributes (parser);
3560 return c_parser_parms_list_declarator (parser, new_attrs, expr);
3562 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3564 c_parser_consume_token (parser);
3565 if (bad_parm)
3566 return NULL;
3567 else
3568 return get_parm_info (false, expr);
3570 if (!c_parser_require (parser, CPP_COMMA,
3571 "expected %<;%>, %<,%> or %<)%>"))
3573 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
3574 return NULL;
3576 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
3578 c_parser_consume_token (parser);
3579 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3581 c_parser_consume_token (parser);
3582 if (bad_parm)
3583 return NULL;
3584 else
3585 return get_parm_info (true, expr);
3587 else
3589 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3590 "expected %<)%>");
3591 return NULL;
3597 /* Parse a parameter declaration. ATTRS are the attributes at the
3598 start of the declaration if it is the first parameter. */
3600 static struct c_parm *
3601 c_parser_parameter_declaration (c_parser *parser, tree attrs)
3603 struct c_declspecs *specs;
3604 struct c_declarator *declarator;
3605 tree prefix_attrs;
3606 tree postfix_attrs = NULL_TREE;
3607 bool dummy = false;
3609 /* Accept #pragmas between parameter declarations. */
3610 while (c_parser_next_token_is (parser, CPP_PRAGMA))
3611 c_parser_pragma (parser, pragma_param);
3613 if (!c_parser_next_token_starts_declspecs (parser))
3615 c_token *token = c_parser_peek_token (parser);
3616 if (parser->error)
3617 return NULL;
3618 c_parser_set_source_position_from_token (token);
3619 if (c_parser_next_tokens_start_typename (parser, cla_prefer_type))
3621 error_at (token->location, "unknown type name %qE", token->value);
3622 parser->error = true;
3624 /* ??? In some Objective-C cases '...' isn't applicable so there
3625 should be a different message. */
3626 else
3627 c_parser_error (parser,
3628 "expected declaration specifiers or %<...%>");
3629 c_parser_skip_to_end_of_parameter (parser);
3630 return NULL;
3632 specs = build_null_declspecs ();
3633 if (attrs)
3635 declspecs_add_attrs (input_location, specs, attrs);
3636 attrs = NULL_TREE;
3638 c_parser_declspecs (parser, specs, true, true, true, true, false,
3639 cla_nonabstract_decl);
3640 finish_declspecs (specs);
3641 pending_xref_error ();
3642 prefix_attrs = specs->attrs;
3643 specs->attrs = NULL_TREE;
3644 declarator = c_parser_declarator (parser,
3645 specs->typespec_kind != ctsk_none,
3646 C_DTR_PARM, &dummy);
3647 if (declarator == NULL)
3649 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
3650 return NULL;
3652 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
3653 postfix_attrs = c_parser_attributes (parser);
3654 return build_c_parm (specs, chainon (postfix_attrs, prefix_attrs),
3655 declarator);
3658 /* Parse a string literal in an asm expression. It should not be
3659 translated, and wide string literals are an error although
3660 permitted by the syntax. This is a GNU extension.
3662 asm-string-literal:
3663 string-literal
3665 ??? At present, following the old parser, the caller needs to have
3666 set lex_untranslated_string to 1. It would be better to follow the
3667 C++ parser rather than using this kludge. */
3669 static tree
3670 c_parser_asm_string_literal (c_parser *parser)
3672 tree str;
3673 int save_flag = warn_overlength_strings;
3674 warn_overlength_strings = 0;
3675 if (c_parser_next_token_is (parser, CPP_STRING))
3677 str = c_parser_peek_token (parser)->value;
3678 c_parser_consume_token (parser);
3680 else if (c_parser_next_token_is (parser, CPP_WSTRING))
3682 error_at (c_parser_peek_token (parser)->location,
3683 "wide string literal in %<asm%>");
3684 str = build_string (1, "");
3685 c_parser_consume_token (parser);
3687 else
3689 c_parser_error (parser, "expected string literal");
3690 str = NULL_TREE;
3692 warn_overlength_strings = save_flag;
3693 return str;
3696 /* Parse a simple asm expression. This is used in restricted
3697 contexts, where a full expression with inputs and outputs does not
3698 make sense. This is a GNU extension.
3700 simple-asm-expr:
3701 asm ( asm-string-literal )
3704 static tree
3705 c_parser_simple_asm_expr (c_parser *parser)
3707 tree str;
3708 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ASM));
3709 /* ??? Follow the C++ parser rather than using the
3710 lex_untranslated_string kludge. */
3711 parser->lex_untranslated_string = true;
3712 c_parser_consume_token (parser);
3713 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3715 parser->lex_untranslated_string = false;
3716 return NULL_TREE;
3718 str = c_parser_asm_string_literal (parser);
3719 parser->lex_untranslated_string = false;
3720 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
3722 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
3723 return NULL_TREE;
3725 return str;
3728 static tree
3729 c_parser_attribute_any_word (c_parser *parser)
3731 tree attr_name = NULL_TREE;
3733 if (c_parser_next_token_is (parser, CPP_KEYWORD))
3735 /* ??? See comment above about what keywords are accepted here. */
3736 bool ok;
3737 switch (c_parser_peek_token (parser)->keyword)
3739 case RID_STATIC:
3740 case RID_UNSIGNED:
3741 case RID_LONG:
3742 case RID_INT128:
3743 case RID_CONST:
3744 case RID_EXTERN:
3745 case RID_REGISTER:
3746 case RID_TYPEDEF:
3747 case RID_SHORT:
3748 case RID_INLINE:
3749 case RID_NORETURN:
3750 case RID_VOLATILE:
3751 case RID_SIGNED:
3752 case RID_AUTO:
3753 case RID_RESTRICT:
3754 case RID_COMPLEX:
3755 case RID_THREAD:
3756 case RID_INT:
3757 case RID_CHAR:
3758 case RID_FLOAT:
3759 case RID_DOUBLE:
3760 case RID_VOID:
3761 case RID_DFLOAT32:
3762 case RID_DFLOAT64:
3763 case RID_DFLOAT128:
3764 case RID_BOOL:
3765 case RID_FRACT:
3766 case RID_ACCUM:
3767 case RID_SAT:
3768 case RID_TRANSACTION_ATOMIC:
3769 case RID_TRANSACTION_CANCEL:
3770 case RID_ATOMIC:
3771 case RID_AUTO_TYPE:
3772 ok = true;
3773 break;
3774 default:
3775 ok = false;
3776 break;
3778 if (!ok)
3779 return NULL_TREE;
3781 /* Accept __attribute__((__const)) as __attribute__((const)) etc. */
3782 attr_name = ridpointers[(int) c_parser_peek_token (parser)->keyword];
3784 else if (c_parser_next_token_is (parser, CPP_NAME))
3785 attr_name = c_parser_peek_token (parser)->value;
3787 return attr_name;
3790 /* Returns true of NAME is an IDENTIFIER_NODE with identiifer "vector,"
3791 "__vector" or "__vector__." */
3793 static inline bool
3794 is_cilkplus_vector_p (tree name)
3796 if (flag_cilkplus && is_attribute_p ("vector", name))
3797 return true;
3798 return false;
3801 #define CILK_SIMD_FN_CLAUSE_MASK \
3802 ((OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_VECTORLENGTH) \
3803 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_LINEAR) \
3804 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_UNIFORM) \
3805 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_MASK) \
3806 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_NOMASK))
3808 /* Parses the vector attribute of SIMD enabled functions in Cilk Plus.
3809 VEC_TOKEN is the "vector" token that is replaced with "simd" and
3810 pushed into the token list.
3811 Syntax:
3812 vector
3813 vector (<vector attributes>). */
3815 static void
3816 c_parser_cilk_simd_fn_vector_attrs (c_parser *parser, c_token vec_token)
3818 gcc_assert (is_cilkplus_vector_p (vec_token.value));
3820 int paren_scope = 0;
3821 vec_safe_push (parser->cilk_simd_fn_tokens, vec_token);
3822 /* Consume the "vector" token. */
3823 c_parser_consume_token (parser);
3825 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
3827 c_parser_consume_token (parser);
3828 paren_scope++;
3830 while (paren_scope > 0)
3832 c_token *token = c_parser_peek_token (parser);
3833 if (token->type == CPP_OPEN_PAREN)
3834 paren_scope++;
3835 else if (token->type == CPP_CLOSE_PAREN)
3836 paren_scope--;
3837 /* Do not push the last ')' since we are not pushing the '('. */
3838 if (!(token->type == CPP_CLOSE_PAREN && paren_scope == 0))
3839 vec_safe_push (parser->cilk_simd_fn_tokens, *token);
3840 c_parser_consume_token (parser);
3843 /* Since we are converting an attribute to a pragma, we need to end the
3844 attribute with PRAGMA_EOL. */
3845 c_token eol_token;
3846 memset (&eol_token, 0, sizeof (eol_token));
3847 eol_token.type = CPP_PRAGMA_EOL;
3848 vec_safe_push (parser->cilk_simd_fn_tokens, eol_token);
3851 /* Add 2 CPP_EOF at the end of PARSER->ELEM_FN_TOKENS vector. */
3853 static void
3854 c_finish_cilk_simd_fn_tokens (c_parser *parser)
3856 c_token last_token = parser->cilk_simd_fn_tokens->last ();
3858 /* c_parser_attributes is called in several places, so if these EOF
3859 tokens are already inserted, then don't do them again. */
3860 if (last_token.type == CPP_EOF)
3861 return;
3863 /* Two CPP_EOF token are added as a safety net since the normal C
3864 front-end has two token look-ahead. */
3865 c_token eof_token;
3866 eof_token.type = CPP_EOF;
3867 vec_safe_push (parser->cilk_simd_fn_tokens, eof_token);
3868 vec_safe_push (parser->cilk_simd_fn_tokens, eof_token);
3871 /* Parse (possibly empty) attributes. This is a GNU extension.
3873 attributes:
3874 empty
3875 attributes attribute
3877 attribute:
3878 __attribute__ ( ( attribute-list ) )
3880 attribute-list:
3881 attrib
3882 attribute_list , attrib
3884 attrib:
3885 empty
3886 any-word
3887 any-word ( identifier )
3888 any-word ( identifier , nonempty-expr-list )
3889 any-word ( expr-list )
3891 where the "identifier" must not be declared as a type, and
3892 "any-word" may be any identifier (including one declared as a
3893 type), a reserved word storage class specifier, type specifier or
3894 type qualifier. ??? This still leaves out most reserved keywords
3895 (following the old parser), shouldn't we include them, and why not
3896 allow identifiers declared as types to start the arguments? */
3898 static tree
3899 c_parser_attributes (c_parser *parser)
3901 tree attrs = NULL_TREE;
3902 while (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
3904 /* ??? Follow the C++ parser rather than using the
3905 lex_untranslated_string kludge. */
3906 parser->lex_untranslated_string = true;
3907 c_parser_consume_token (parser);
3908 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3910 parser->lex_untranslated_string = false;
3911 return attrs;
3913 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3915 parser->lex_untranslated_string = false;
3916 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
3917 return attrs;
3919 /* Parse the attribute list. */
3920 while (c_parser_next_token_is (parser, CPP_COMMA)
3921 || c_parser_next_token_is (parser, CPP_NAME)
3922 || c_parser_next_token_is (parser, CPP_KEYWORD))
3924 tree attr, attr_name, attr_args;
3925 vec<tree, va_gc> *expr_list;
3926 if (c_parser_next_token_is (parser, CPP_COMMA))
3928 c_parser_consume_token (parser);
3929 continue;
3932 attr_name = c_parser_attribute_any_word (parser);
3933 if (attr_name == NULL)
3934 break;
3935 if (is_cilkplus_vector_p (attr_name))
3937 c_token *v_token = c_parser_peek_token (parser);
3938 c_parser_cilk_simd_fn_vector_attrs (parser, *v_token);
3939 continue;
3941 c_parser_consume_token (parser);
3942 if (c_parser_next_token_is_not (parser, CPP_OPEN_PAREN))
3944 attr = build_tree_list (attr_name, NULL_TREE);
3945 attrs = chainon (attrs, attr);
3946 continue;
3948 c_parser_consume_token (parser);
3949 /* Parse the attribute contents. If they start with an
3950 identifier which is followed by a comma or close
3951 parenthesis, then the arguments start with that
3952 identifier; otherwise they are an expression list.
3953 In objective-c the identifier may be a classname. */
3954 if (c_parser_next_token_is (parser, CPP_NAME)
3955 && (c_parser_peek_token (parser)->id_kind == C_ID_ID
3956 || (c_dialect_objc ()
3957 && c_parser_peek_token (parser)->id_kind
3958 == C_ID_CLASSNAME))
3959 && ((c_parser_peek_2nd_token (parser)->type == CPP_COMMA)
3960 || (c_parser_peek_2nd_token (parser)->type
3961 == CPP_CLOSE_PAREN))
3962 && (attribute_takes_identifier_p (attr_name)
3963 || (c_dialect_objc ()
3964 && c_parser_peek_token (parser)->id_kind
3965 == C_ID_CLASSNAME)))
3967 tree arg1 = c_parser_peek_token (parser)->value;
3968 c_parser_consume_token (parser);
3969 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3970 attr_args = build_tree_list (NULL_TREE, arg1);
3971 else
3973 tree tree_list;
3974 c_parser_consume_token (parser);
3975 expr_list = c_parser_expr_list (parser, false, true,
3976 NULL, NULL, NULL, NULL);
3977 tree_list = build_tree_list_vec (expr_list);
3978 attr_args = tree_cons (NULL_TREE, arg1, tree_list);
3979 release_tree_vector (expr_list);
3982 else
3984 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3985 attr_args = NULL_TREE;
3986 else
3988 expr_list = c_parser_expr_list (parser, false, true,
3989 NULL, NULL, NULL, NULL);
3990 attr_args = build_tree_list_vec (expr_list);
3991 release_tree_vector (expr_list);
3994 attr = build_tree_list (attr_name, attr_args);
3995 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3996 c_parser_consume_token (parser);
3997 else
3999 parser->lex_untranslated_string = false;
4000 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
4001 "expected %<)%>");
4002 return attrs;
4004 attrs = chainon (attrs, attr);
4006 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4007 c_parser_consume_token (parser);
4008 else
4010 parser->lex_untranslated_string = false;
4011 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
4012 "expected %<)%>");
4013 return attrs;
4015 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4016 c_parser_consume_token (parser);
4017 else
4019 parser->lex_untranslated_string = false;
4020 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
4021 "expected %<)%>");
4022 return attrs;
4024 parser->lex_untranslated_string = false;
4027 if (flag_cilkplus && !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
4028 c_finish_cilk_simd_fn_tokens (parser);
4029 return attrs;
4032 /* Parse a type name (C90 6.5.5, C99 6.7.6).
4034 type-name:
4035 specifier-qualifier-list abstract-declarator[opt]
4038 static struct c_type_name *
4039 c_parser_type_name (c_parser *parser)
4041 struct c_declspecs *specs = build_null_declspecs ();
4042 struct c_declarator *declarator;
4043 struct c_type_name *ret;
4044 bool dummy = false;
4045 c_parser_declspecs (parser, specs, false, true, true, false, false,
4046 cla_prefer_type);
4047 if (!specs->declspecs_seen_p)
4049 c_parser_error (parser, "expected specifier-qualifier-list");
4050 return NULL;
4052 if (specs->type != error_mark_node)
4054 pending_xref_error ();
4055 finish_declspecs (specs);
4057 declarator = c_parser_declarator (parser,
4058 specs->typespec_kind != ctsk_none,
4059 C_DTR_ABSTRACT, &dummy);
4060 if (declarator == NULL)
4061 return NULL;
4062 ret = XOBNEW (&parser_obstack, struct c_type_name);
4063 ret->specs = specs;
4064 ret->declarator = declarator;
4065 return ret;
4068 /* Parse an initializer (C90 6.5.7, C99 6.7.8).
4070 initializer:
4071 assignment-expression
4072 { initializer-list }
4073 { initializer-list , }
4075 initializer-list:
4076 designation[opt] initializer
4077 initializer-list , designation[opt] initializer
4079 designation:
4080 designator-list =
4082 designator-list:
4083 designator
4084 designator-list designator
4086 designator:
4087 array-designator
4088 . identifier
4090 array-designator:
4091 [ constant-expression ]
4093 GNU extensions:
4095 initializer:
4098 designation:
4099 array-designator
4100 identifier :
4102 array-designator:
4103 [ constant-expression ... constant-expression ]
4105 Any expression without commas is accepted in the syntax for the
4106 constant-expressions, with non-constant expressions rejected later.
4108 This function is only used for top-level initializers; for nested
4109 ones, see c_parser_initval. */
4111 static struct c_expr
4112 c_parser_initializer (c_parser *parser)
4114 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
4115 return c_parser_braced_init (parser, NULL_TREE, false);
4116 else
4118 struct c_expr ret;
4119 location_t loc = c_parser_peek_token (parser)->location;
4120 ret = c_parser_expr_no_commas (parser, NULL);
4121 if (TREE_CODE (ret.value) != STRING_CST
4122 && TREE_CODE (ret.value) != COMPOUND_LITERAL_EXPR)
4123 ret = convert_lvalue_to_rvalue (loc, ret, true, true);
4124 return ret;
4128 /* Parse a braced initializer list. TYPE is the type specified for a
4129 compound literal, and NULL_TREE for other initializers and for
4130 nested braced lists. NESTED_P is true for nested braced lists,
4131 false for the list of a compound literal or the list that is the
4132 top-level initializer in a declaration. */
4134 static struct c_expr
4135 c_parser_braced_init (c_parser *parser, tree type, bool nested_p)
4137 struct c_expr ret;
4138 struct obstack braced_init_obstack;
4139 location_t brace_loc = c_parser_peek_token (parser)->location;
4140 gcc_obstack_init (&braced_init_obstack);
4141 gcc_assert (c_parser_next_token_is (parser, CPP_OPEN_BRACE));
4142 c_parser_consume_token (parser);
4143 if (nested_p)
4144 push_init_level (brace_loc, 0, &braced_init_obstack);
4145 else
4146 really_start_incremental_init (type);
4147 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
4149 pedwarn (brace_loc, OPT_Wpedantic, "ISO C forbids empty initializer braces");
4151 else
4153 /* Parse a non-empty initializer list, possibly with a trailing
4154 comma. */
4155 while (true)
4157 c_parser_initelt (parser, &braced_init_obstack);
4158 if (parser->error)
4159 break;
4160 if (c_parser_next_token_is (parser, CPP_COMMA))
4161 c_parser_consume_token (parser);
4162 else
4163 break;
4164 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
4165 break;
4168 if (c_parser_next_token_is_not (parser, CPP_CLOSE_BRACE))
4170 ret.value = error_mark_node;
4171 ret.original_code = ERROR_MARK;
4172 ret.original_type = NULL;
4173 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, "expected %<}%>");
4174 pop_init_level (brace_loc, 0, &braced_init_obstack);
4175 obstack_free (&braced_init_obstack, NULL);
4176 return ret;
4178 c_parser_consume_token (parser);
4179 ret = pop_init_level (brace_loc, 0, &braced_init_obstack);
4180 obstack_free (&braced_init_obstack, NULL);
4181 return ret;
4184 /* Parse a nested initializer, including designators. */
4186 static void
4187 c_parser_initelt (c_parser *parser, struct obstack * braced_init_obstack)
4189 /* Parse any designator or designator list. A single array
4190 designator may have the subsequent "=" omitted in GNU C, but a
4191 longer list or a structure member designator may not. */
4192 if (c_parser_next_token_is (parser, CPP_NAME)
4193 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
4195 /* Old-style structure member designator. */
4196 set_init_label (c_parser_peek_token (parser)->location,
4197 c_parser_peek_token (parser)->value,
4198 braced_init_obstack);
4199 /* Use the colon as the error location. */
4200 pedwarn (c_parser_peek_2nd_token (parser)->location, OPT_Wpedantic,
4201 "obsolete use of designated initializer with %<:%>");
4202 c_parser_consume_token (parser);
4203 c_parser_consume_token (parser);
4205 else
4207 /* des_seen is 0 if there have been no designators, 1 if there
4208 has been a single array designator and 2 otherwise. */
4209 int des_seen = 0;
4210 /* Location of a designator. */
4211 location_t des_loc = UNKNOWN_LOCATION; /* Quiet warning. */
4212 while (c_parser_next_token_is (parser, CPP_OPEN_SQUARE)
4213 || c_parser_next_token_is (parser, CPP_DOT))
4215 int des_prev = des_seen;
4216 if (!des_seen)
4217 des_loc = c_parser_peek_token (parser)->location;
4218 if (des_seen < 2)
4219 des_seen++;
4220 if (c_parser_next_token_is (parser, CPP_DOT))
4222 des_seen = 2;
4223 c_parser_consume_token (parser);
4224 if (c_parser_next_token_is (parser, CPP_NAME))
4226 set_init_label (des_loc, c_parser_peek_token (parser)->value,
4227 braced_init_obstack);
4228 c_parser_consume_token (parser);
4230 else
4232 struct c_expr init;
4233 init.value = error_mark_node;
4234 init.original_code = ERROR_MARK;
4235 init.original_type = NULL;
4236 c_parser_error (parser, "expected identifier");
4237 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
4238 process_init_element (input_location, init, false,
4239 braced_init_obstack);
4240 return;
4243 else
4245 tree first, second;
4246 location_t ellipsis_loc = UNKNOWN_LOCATION; /* Quiet warning. */
4247 location_t array_index_loc = UNKNOWN_LOCATION;
4248 /* ??? Following the old parser, [ objc-receiver
4249 objc-message-args ] is accepted as an initializer,
4250 being distinguished from a designator by what follows
4251 the first assignment expression inside the square
4252 brackets, but after a first array designator a
4253 subsequent square bracket is for Objective-C taken to
4254 start an expression, using the obsolete form of
4255 designated initializer without '=', rather than
4256 possibly being a second level of designation: in LALR
4257 terms, the '[' is shifted rather than reducing
4258 designator to designator-list. */
4259 if (des_prev == 1 && c_dialect_objc ())
4261 des_seen = des_prev;
4262 break;
4264 if (des_prev == 0 && c_dialect_objc ())
4266 /* This might be an array designator or an
4267 Objective-C message expression. If the former,
4268 continue parsing here; if the latter, parse the
4269 remainder of the initializer given the starting
4270 primary-expression. ??? It might make sense to
4271 distinguish when des_prev == 1 as well; see
4272 previous comment. */
4273 tree rec, args;
4274 struct c_expr mexpr;
4275 c_parser_consume_token (parser);
4276 if (c_parser_peek_token (parser)->type == CPP_NAME
4277 && ((c_parser_peek_token (parser)->id_kind
4278 == C_ID_TYPENAME)
4279 || (c_parser_peek_token (parser)->id_kind
4280 == C_ID_CLASSNAME)))
4282 /* Type name receiver. */
4283 tree id = c_parser_peek_token (parser)->value;
4284 c_parser_consume_token (parser);
4285 rec = objc_get_class_reference (id);
4286 goto parse_message_args;
4288 first = c_parser_expr_no_commas (parser, NULL).value;
4289 mark_exp_read (first);
4290 if (c_parser_next_token_is (parser, CPP_ELLIPSIS)
4291 || c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
4292 goto array_desig_after_first;
4293 /* Expression receiver. So far only one part
4294 without commas has been parsed; there might be
4295 more of the expression. */
4296 rec = first;
4297 while (c_parser_next_token_is (parser, CPP_COMMA))
4299 struct c_expr next;
4300 location_t comma_loc, exp_loc;
4301 comma_loc = c_parser_peek_token (parser)->location;
4302 c_parser_consume_token (parser);
4303 exp_loc = c_parser_peek_token (parser)->location;
4304 next = c_parser_expr_no_commas (parser, NULL);
4305 next = convert_lvalue_to_rvalue (exp_loc, next,
4306 true, true);
4307 rec = build_compound_expr (comma_loc, rec, next.value);
4309 parse_message_args:
4310 /* Now parse the objc-message-args. */
4311 args = c_parser_objc_message_args (parser);
4312 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
4313 "expected %<]%>");
4314 mexpr.value
4315 = objc_build_message_expr (rec, args);
4316 mexpr.original_code = ERROR_MARK;
4317 mexpr.original_type = NULL;
4318 /* Now parse and process the remainder of the
4319 initializer, starting with this message
4320 expression as a primary-expression. */
4321 c_parser_initval (parser, &mexpr, braced_init_obstack);
4322 return;
4324 c_parser_consume_token (parser);
4325 array_index_loc = c_parser_peek_token (parser)->location;
4326 first = c_parser_expr_no_commas (parser, NULL).value;
4327 mark_exp_read (first);
4328 array_desig_after_first:
4329 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
4331 ellipsis_loc = c_parser_peek_token (parser)->location;
4332 c_parser_consume_token (parser);
4333 second = c_parser_expr_no_commas (parser, NULL).value;
4334 mark_exp_read (second);
4336 else
4337 second = NULL_TREE;
4338 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
4340 c_parser_consume_token (parser);
4341 set_init_index (array_index_loc, first, second,
4342 braced_init_obstack);
4343 if (second)
4344 pedwarn (ellipsis_loc, OPT_Wpedantic,
4345 "ISO C forbids specifying range of elements to initialize");
4347 else
4348 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
4349 "expected %<]%>");
4352 if (des_seen >= 1)
4354 if (c_parser_next_token_is (parser, CPP_EQ))
4356 pedwarn_c90 (des_loc, OPT_Wpedantic,
4357 "ISO C90 forbids specifying subobject "
4358 "to initialize");
4359 c_parser_consume_token (parser);
4361 else
4363 if (des_seen == 1)
4364 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
4365 "obsolete use of designated initializer without %<=%>");
4366 else
4368 struct c_expr init;
4369 init.value = error_mark_node;
4370 init.original_code = ERROR_MARK;
4371 init.original_type = NULL;
4372 c_parser_error (parser, "expected %<=%>");
4373 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
4374 process_init_element (input_location, init, false,
4375 braced_init_obstack);
4376 return;
4381 c_parser_initval (parser, NULL, braced_init_obstack);
4384 /* Parse a nested initializer; as c_parser_initializer but parses
4385 initializers within braced lists, after any designators have been
4386 applied. If AFTER is not NULL then it is an Objective-C message
4387 expression which is the primary-expression starting the
4388 initializer. */
4390 static void
4391 c_parser_initval (c_parser *parser, struct c_expr *after,
4392 struct obstack * braced_init_obstack)
4394 struct c_expr init;
4395 gcc_assert (!after || c_dialect_objc ());
4396 location_t loc = c_parser_peek_token (parser)->location;
4398 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE) && !after)
4399 init = c_parser_braced_init (parser, NULL_TREE, true);
4400 else
4402 init = c_parser_expr_no_commas (parser, after);
4403 if (init.value != NULL_TREE
4404 && TREE_CODE (init.value) != STRING_CST
4405 && TREE_CODE (init.value) != COMPOUND_LITERAL_EXPR)
4406 init = convert_lvalue_to_rvalue (loc, init, true, true);
4408 process_init_element (loc, init, false, braced_init_obstack);
4411 /* Parse a compound statement (possibly a function body) (C90 6.6.2,
4412 C99 6.8.2).
4414 compound-statement:
4415 { block-item-list[opt] }
4416 { label-declarations block-item-list }
4418 block-item-list:
4419 block-item
4420 block-item-list block-item
4422 block-item:
4423 nested-declaration
4424 statement
4426 nested-declaration:
4427 declaration
4429 GNU extensions:
4431 compound-statement:
4432 { label-declarations block-item-list }
4434 nested-declaration:
4435 __extension__ nested-declaration
4436 nested-function-definition
4438 label-declarations:
4439 label-declaration
4440 label-declarations label-declaration
4442 label-declaration:
4443 __label__ identifier-list ;
4445 Allowing the mixing of declarations and code is new in C99. The
4446 GNU syntax also permits (not shown above) labels at the end of
4447 compound statements, which yield an error. We don't allow labels
4448 on declarations; this might seem like a natural extension, but
4449 there would be a conflict between attributes on the label and
4450 prefix attributes on the declaration. ??? The syntax follows the
4451 old parser in requiring something after label declarations.
4452 Although they are erroneous if the labels declared aren't defined,
4453 is it useful for the syntax to be this way?
4455 OpenMP:
4457 block-item:
4458 openmp-directive
4460 openmp-directive:
4461 barrier-directive
4462 flush-directive
4463 taskwait-directive
4464 taskyield-directive
4465 cancel-directive
4466 cancellation-point-directive */
4468 static tree
4469 c_parser_compound_statement (c_parser *parser)
4471 tree stmt;
4472 location_t brace_loc;
4473 brace_loc = c_parser_peek_token (parser)->location;
4474 if (!c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
4476 /* Ensure a scope is entered and left anyway to avoid confusion
4477 if we have just prepared to enter a function body. */
4478 stmt = c_begin_compound_stmt (true);
4479 c_end_compound_stmt (brace_loc, stmt, true);
4480 return error_mark_node;
4482 stmt = c_begin_compound_stmt (true);
4483 c_parser_compound_statement_nostart (parser);
4485 /* If the compound stmt contains array notations, then we expand them. */
4486 if (flag_cilkplus && contains_array_notation_expr (stmt))
4487 stmt = expand_array_notation_exprs (stmt);
4488 return c_end_compound_stmt (brace_loc, stmt, true);
4491 /* Parse a compound statement except for the opening brace. This is
4492 used for parsing both compound statements and statement expressions
4493 (which follow different paths to handling the opening). */
4495 static void
4496 c_parser_compound_statement_nostart (c_parser *parser)
4498 bool last_stmt = false;
4499 bool last_label = false;
4500 bool save_valid_for_pragma = valid_location_for_stdc_pragma_p ();
4501 location_t label_loc = UNKNOWN_LOCATION; /* Quiet warning. */
4502 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
4504 c_parser_consume_token (parser);
4505 return;
4507 mark_valid_location_for_stdc_pragma (true);
4508 if (c_parser_next_token_is_keyword (parser, RID_LABEL))
4510 /* Read zero or more forward-declarations for labels that nested
4511 functions can jump to. */
4512 mark_valid_location_for_stdc_pragma (false);
4513 while (c_parser_next_token_is_keyword (parser, RID_LABEL))
4515 label_loc = c_parser_peek_token (parser)->location;
4516 c_parser_consume_token (parser);
4517 /* Any identifiers, including those declared as type names,
4518 are OK here. */
4519 while (true)
4521 tree label;
4522 if (c_parser_next_token_is_not (parser, CPP_NAME))
4524 c_parser_error (parser, "expected identifier");
4525 break;
4527 label
4528 = declare_label (c_parser_peek_token (parser)->value);
4529 C_DECLARED_LABEL_FLAG (label) = 1;
4530 add_stmt (build_stmt (label_loc, DECL_EXPR, label));
4531 c_parser_consume_token (parser);
4532 if (c_parser_next_token_is (parser, CPP_COMMA))
4533 c_parser_consume_token (parser);
4534 else
4535 break;
4537 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
4539 pedwarn (label_loc, OPT_Wpedantic, "ISO C forbids label declarations");
4541 /* We must now have at least one statement, label or declaration. */
4542 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
4544 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
4545 c_parser_error (parser, "expected declaration or statement");
4546 c_parser_consume_token (parser);
4547 return;
4549 while (c_parser_next_token_is_not (parser, CPP_CLOSE_BRACE))
4551 location_t loc = c_parser_peek_token (parser)->location;
4552 if (c_parser_next_token_is_keyword (parser, RID_CASE)
4553 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
4554 || (c_parser_next_token_is (parser, CPP_NAME)
4555 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
4557 if (c_parser_next_token_is_keyword (parser, RID_CASE))
4558 label_loc = c_parser_peek_2nd_token (parser)->location;
4559 else
4560 label_loc = c_parser_peek_token (parser)->location;
4561 last_label = true;
4562 last_stmt = false;
4563 mark_valid_location_for_stdc_pragma (false);
4564 c_parser_label (parser);
4566 else if (!last_label
4567 && c_parser_next_tokens_start_declaration (parser))
4569 last_label = false;
4570 mark_valid_location_for_stdc_pragma (false);
4571 c_parser_declaration_or_fndef (parser, true, true, true, true,
4572 true, NULL, vNULL);
4573 if (last_stmt)
4574 pedwarn_c90 (loc, OPT_Wdeclaration_after_statement,
4575 "ISO C90 forbids mixed declarations and code");
4576 last_stmt = false;
4578 else if (!last_label
4579 && c_parser_next_token_is_keyword (parser, RID_EXTENSION))
4581 /* __extension__ can start a declaration, but is also an
4582 unary operator that can start an expression. Consume all
4583 but the last of a possible series of __extension__ to
4584 determine which. */
4585 while (c_parser_peek_2nd_token (parser)->type == CPP_KEYWORD
4586 && (c_parser_peek_2nd_token (parser)->keyword
4587 == RID_EXTENSION))
4588 c_parser_consume_token (parser);
4589 if (c_token_starts_declaration (c_parser_peek_2nd_token (parser)))
4591 int ext;
4592 ext = disable_extension_diagnostics ();
4593 c_parser_consume_token (parser);
4594 last_label = false;
4595 mark_valid_location_for_stdc_pragma (false);
4596 c_parser_declaration_or_fndef (parser, true, true, true, true,
4597 true, NULL, vNULL);
4598 /* Following the old parser, __extension__ does not
4599 disable this diagnostic. */
4600 restore_extension_diagnostics (ext);
4601 if (last_stmt)
4602 pedwarn_c90 (loc, OPT_Wdeclaration_after_statement,
4603 "ISO C90 forbids mixed declarations and code");
4604 last_stmt = false;
4606 else
4607 goto statement;
4609 else if (c_parser_next_token_is (parser, CPP_PRAGMA))
4611 /* External pragmas, and some omp pragmas, are not associated
4612 with regular c code, and so are not to be considered statements
4613 syntactically. This ensures that the user doesn't put them
4614 places that would turn into syntax errors if the directive
4615 were ignored. */
4616 if (c_parser_pragma (parser, pragma_compound))
4617 last_label = false, last_stmt = true;
4619 else if (c_parser_next_token_is (parser, CPP_EOF))
4621 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
4622 c_parser_error (parser, "expected declaration or statement");
4623 return;
4625 else if (c_parser_next_token_is_keyword (parser, RID_ELSE))
4627 if (parser->in_if_block)
4629 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
4630 error_at (loc, """expected %<}%> before %<else%>");
4631 return;
4633 else
4635 error_at (loc, "%<else%> without a previous %<if%>");
4636 c_parser_consume_token (parser);
4637 continue;
4640 else
4642 statement:
4643 last_label = false;
4644 last_stmt = true;
4645 mark_valid_location_for_stdc_pragma (false);
4646 c_parser_statement_after_labels (parser);
4649 parser->error = false;
4651 if (last_label)
4652 error_at (label_loc, "label at end of compound statement");
4653 c_parser_consume_token (parser);
4654 /* Restore the value we started with. */
4655 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
4658 /* Parse a label (C90 6.6.1, C99 6.8.1).
4660 label:
4661 identifier : attributes[opt]
4662 case constant-expression :
4663 default :
4665 GNU extensions:
4667 label:
4668 case constant-expression ... constant-expression :
4670 The use of attributes on labels is a GNU extension. The syntax in
4671 GNU C accepts any expressions without commas, non-constant
4672 expressions being rejected later. */
4674 static void
4675 c_parser_label (c_parser *parser)
4677 location_t loc1 = c_parser_peek_token (parser)->location;
4678 tree label = NULL_TREE;
4679 if (c_parser_next_token_is_keyword (parser, RID_CASE))
4681 tree exp1, exp2;
4682 c_parser_consume_token (parser);
4683 exp1 = c_parser_expr_no_commas (parser, NULL).value;
4684 if (c_parser_next_token_is (parser, CPP_COLON))
4686 c_parser_consume_token (parser);
4687 label = do_case (loc1, exp1, NULL_TREE);
4689 else if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
4691 c_parser_consume_token (parser);
4692 exp2 = c_parser_expr_no_commas (parser, NULL).value;
4693 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
4694 label = do_case (loc1, exp1, exp2);
4696 else
4697 c_parser_error (parser, "expected %<:%> or %<...%>");
4699 else if (c_parser_next_token_is_keyword (parser, RID_DEFAULT))
4701 c_parser_consume_token (parser);
4702 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
4703 label = do_case (loc1, NULL_TREE, NULL_TREE);
4705 else
4707 tree name = c_parser_peek_token (parser)->value;
4708 tree tlab;
4709 tree attrs;
4710 location_t loc2 = c_parser_peek_token (parser)->location;
4711 gcc_assert (c_parser_next_token_is (parser, CPP_NAME));
4712 c_parser_consume_token (parser);
4713 gcc_assert (c_parser_next_token_is (parser, CPP_COLON));
4714 c_parser_consume_token (parser);
4715 attrs = c_parser_attributes (parser);
4716 tlab = define_label (loc2, name);
4717 if (tlab)
4719 decl_attributes (&tlab, attrs, 0);
4720 label = add_stmt (build_stmt (loc1, LABEL_EXPR, tlab));
4723 if (label)
4725 if (c_parser_next_tokens_start_declaration (parser))
4727 error_at (c_parser_peek_token (parser)->location,
4728 "a label can only be part of a statement and "
4729 "a declaration is not a statement");
4730 c_parser_declaration_or_fndef (parser, /*fndef_ok*/ false,
4731 /*static_assert_ok*/ true,
4732 /*empty_ok*/ true, /*nested*/ true,
4733 /*start_attr_ok*/ true, NULL,
4734 vNULL);
4739 /* Parse a statement (C90 6.6, C99 6.8).
4741 statement:
4742 labeled-statement
4743 compound-statement
4744 expression-statement
4745 selection-statement
4746 iteration-statement
4747 jump-statement
4749 labeled-statement:
4750 label statement
4752 expression-statement:
4753 expression[opt] ;
4755 selection-statement:
4756 if-statement
4757 switch-statement
4759 iteration-statement:
4760 while-statement
4761 do-statement
4762 for-statement
4764 jump-statement:
4765 goto identifier ;
4766 continue ;
4767 break ;
4768 return expression[opt] ;
4770 GNU extensions:
4772 statement:
4773 asm-statement
4775 jump-statement:
4776 goto * expression ;
4778 Objective-C:
4780 statement:
4781 objc-throw-statement
4782 objc-try-catch-statement
4783 objc-synchronized-statement
4785 objc-throw-statement:
4786 @throw expression ;
4787 @throw ;
4789 OpenMP:
4791 statement:
4792 openmp-construct
4794 openmp-construct:
4795 parallel-construct
4796 for-construct
4797 simd-construct
4798 for-simd-construct
4799 sections-construct
4800 single-construct
4801 parallel-for-construct
4802 parallel-for-simd-construct
4803 parallel-sections-construct
4804 master-construct
4805 critical-construct
4806 atomic-construct
4807 ordered-construct
4809 parallel-construct:
4810 parallel-directive structured-block
4812 for-construct:
4813 for-directive iteration-statement
4815 simd-construct:
4816 simd-directive iteration-statements
4818 for-simd-construct:
4819 for-simd-directive iteration-statements
4821 sections-construct:
4822 sections-directive section-scope
4824 single-construct:
4825 single-directive structured-block
4827 parallel-for-construct:
4828 parallel-for-directive iteration-statement
4830 parallel-for-simd-construct:
4831 parallel-for-simd-directive iteration-statement
4833 parallel-sections-construct:
4834 parallel-sections-directive section-scope
4836 master-construct:
4837 master-directive structured-block
4839 critical-construct:
4840 critical-directive structured-block
4842 atomic-construct:
4843 atomic-directive expression-statement
4845 ordered-construct:
4846 ordered-directive structured-block
4848 Transactional Memory:
4850 statement:
4851 transaction-statement
4852 transaction-cancel-statement
4855 static void
4856 c_parser_statement (c_parser *parser)
4858 while (c_parser_next_token_is_keyword (parser, RID_CASE)
4859 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
4860 || (c_parser_next_token_is (parser, CPP_NAME)
4861 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
4862 c_parser_label (parser);
4863 c_parser_statement_after_labels (parser);
4866 /* Parse a statement, other than a labeled statement. */
4868 static void
4869 c_parser_statement_after_labels (c_parser *parser)
4871 location_t loc = c_parser_peek_token (parser)->location;
4872 tree stmt = NULL_TREE;
4873 bool in_if_block = parser->in_if_block;
4874 parser->in_if_block = false;
4875 switch (c_parser_peek_token (parser)->type)
4877 case CPP_OPEN_BRACE:
4878 add_stmt (c_parser_compound_statement (parser));
4879 break;
4880 case CPP_KEYWORD:
4881 switch (c_parser_peek_token (parser)->keyword)
4883 case RID_IF:
4884 c_parser_if_statement (parser);
4885 break;
4886 case RID_SWITCH:
4887 c_parser_switch_statement (parser);
4888 break;
4889 case RID_WHILE:
4890 c_parser_while_statement (parser, false);
4891 break;
4892 case RID_DO:
4893 c_parser_do_statement (parser, false);
4894 break;
4895 case RID_FOR:
4896 c_parser_for_statement (parser, false);
4897 break;
4898 case RID_CILK_SYNC:
4899 c_parser_consume_token (parser);
4900 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
4901 if (!flag_cilkplus)
4902 error_at (loc, "-fcilkplus must be enabled to use %<_Cilk_sync%>");
4903 else
4904 add_stmt (build_cilk_sync ());
4905 break;
4906 case RID_GOTO:
4907 c_parser_consume_token (parser);
4908 if (c_parser_next_token_is (parser, CPP_NAME))
4910 stmt = c_finish_goto_label (loc,
4911 c_parser_peek_token (parser)->value);
4912 c_parser_consume_token (parser);
4914 else if (c_parser_next_token_is (parser, CPP_MULT))
4916 struct c_expr val;
4918 c_parser_consume_token (parser);
4919 val = c_parser_expression (parser);
4920 val = convert_lvalue_to_rvalue (loc, val, false, true);
4921 stmt = c_finish_goto_ptr (loc, val.value);
4923 else
4924 c_parser_error (parser, "expected identifier or %<*%>");
4925 goto expect_semicolon;
4926 case RID_CONTINUE:
4927 c_parser_consume_token (parser);
4928 stmt = c_finish_bc_stmt (loc, &c_cont_label, false);
4929 goto expect_semicolon;
4930 case RID_BREAK:
4931 c_parser_consume_token (parser);
4932 stmt = c_finish_bc_stmt (loc, &c_break_label, true);
4933 goto expect_semicolon;
4934 case RID_RETURN:
4935 c_parser_consume_token (parser);
4936 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
4938 stmt = c_finish_return (loc, NULL_TREE, NULL_TREE);
4939 c_parser_consume_token (parser);
4941 else
4943 location_t xloc = c_parser_peek_token (parser)->location;
4944 struct c_expr expr = c_parser_expression_conv (parser);
4945 mark_exp_read (expr.value);
4946 stmt = c_finish_return (xloc, expr.value, expr.original_type);
4947 goto expect_semicolon;
4949 break;
4950 case RID_ASM:
4951 stmt = c_parser_asm_statement (parser);
4952 break;
4953 case RID_TRANSACTION_ATOMIC:
4954 case RID_TRANSACTION_RELAXED:
4955 stmt = c_parser_transaction (parser,
4956 c_parser_peek_token (parser)->keyword);
4957 break;
4958 case RID_TRANSACTION_CANCEL:
4959 stmt = c_parser_transaction_cancel (parser);
4960 goto expect_semicolon;
4961 case RID_AT_THROW:
4962 gcc_assert (c_dialect_objc ());
4963 c_parser_consume_token (parser);
4964 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
4966 stmt = objc_build_throw_stmt (loc, NULL_TREE);
4967 c_parser_consume_token (parser);
4969 else
4971 struct c_expr expr = c_parser_expression (parser);
4972 expr = convert_lvalue_to_rvalue (loc, expr, false, false);
4973 expr.value = c_fully_fold (expr.value, false, NULL);
4974 stmt = objc_build_throw_stmt (loc, expr.value);
4975 goto expect_semicolon;
4977 break;
4978 case RID_AT_TRY:
4979 gcc_assert (c_dialect_objc ());
4980 c_parser_objc_try_catch_finally_statement (parser);
4981 break;
4982 case RID_AT_SYNCHRONIZED:
4983 gcc_assert (c_dialect_objc ());
4984 c_parser_objc_synchronized_statement (parser);
4985 break;
4986 default:
4987 goto expr_stmt;
4989 break;
4990 case CPP_SEMICOLON:
4991 c_parser_consume_token (parser);
4992 break;
4993 case CPP_CLOSE_PAREN:
4994 case CPP_CLOSE_SQUARE:
4995 /* Avoid infinite loop in error recovery:
4996 c_parser_skip_until_found stops at a closing nesting
4997 delimiter without consuming it, but here we need to consume
4998 it to proceed further. */
4999 c_parser_error (parser, "expected statement");
5000 c_parser_consume_token (parser);
5001 break;
5002 case CPP_PRAGMA:
5003 c_parser_pragma (parser, pragma_stmt);
5004 break;
5005 default:
5006 expr_stmt:
5007 stmt = c_finish_expr_stmt (loc, c_parser_expression_conv (parser).value);
5008 expect_semicolon:
5009 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
5010 break;
5012 /* Two cases cannot and do not have line numbers associated: If stmt
5013 is degenerate, such as "2;", then stmt is an INTEGER_CST, which
5014 cannot hold line numbers. But that's OK because the statement
5015 will either be changed to a MODIFY_EXPR during gimplification of
5016 the statement expr, or discarded. If stmt was compound, but
5017 without new variables, we will have skipped the creation of a
5018 BIND and will have a bare STATEMENT_LIST. But that's OK because
5019 (recursively) all of the component statements should already have
5020 line numbers assigned. ??? Can we discard no-op statements
5021 earlier? */
5022 if (CAN_HAVE_LOCATION_P (stmt)
5023 && EXPR_LOCATION (stmt) == UNKNOWN_LOCATION)
5024 SET_EXPR_LOCATION (stmt, loc);
5026 parser->in_if_block = in_if_block;
5029 /* Parse the condition from an if, do, while or for statements. */
5031 static tree
5032 c_parser_condition (c_parser *parser)
5034 location_t loc = c_parser_peek_token (parser)->location;
5035 tree cond;
5036 cond = c_parser_expression_conv (parser).value;
5037 cond = c_objc_common_truthvalue_conversion (loc, cond);
5038 cond = c_fully_fold (cond, false, NULL);
5039 if (warn_sequence_point)
5040 verify_sequence_points (cond);
5041 return cond;
5044 /* Parse a parenthesized condition from an if, do or while statement.
5046 condition:
5047 ( expression )
5049 static tree
5050 c_parser_paren_condition (c_parser *parser)
5052 tree cond;
5053 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5054 return error_mark_node;
5055 cond = c_parser_condition (parser);
5056 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
5057 return cond;
5060 /* Parse a statement which is a block in C99. */
5062 static tree
5063 c_parser_c99_block_statement (c_parser *parser)
5065 tree block = c_begin_compound_stmt (flag_isoc99);
5066 location_t loc = c_parser_peek_token (parser)->location;
5067 c_parser_statement (parser);
5068 return c_end_compound_stmt (loc, block, flag_isoc99);
5071 /* Parse the body of an if statement. This is just parsing a
5072 statement but (a) it is a block in C99, (b) we track whether the
5073 body is an if statement for the sake of -Wparentheses warnings, (c)
5074 we handle an empty body specially for the sake of -Wempty-body
5075 warnings, and (d) we call parser_compound_statement directly
5076 because c_parser_statement_after_labels resets
5077 parser->in_if_block. */
5079 static tree
5080 c_parser_if_body (c_parser *parser, bool *if_p)
5082 tree block = c_begin_compound_stmt (flag_isoc99);
5083 location_t body_loc = c_parser_peek_token (parser)->location;
5084 while (c_parser_next_token_is_keyword (parser, RID_CASE)
5085 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
5086 || (c_parser_next_token_is (parser, CPP_NAME)
5087 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
5088 c_parser_label (parser);
5089 *if_p = c_parser_next_token_is_keyword (parser, RID_IF);
5090 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5092 location_t loc = c_parser_peek_token (parser)->location;
5093 add_stmt (build_empty_stmt (loc));
5094 c_parser_consume_token (parser);
5095 if (!c_parser_next_token_is_keyword (parser, RID_ELSE))
5096 warning_at (loc, OPT_Wempty_body,
5097 "suggest braces around empty body in an %<if%> statement");
5099 else if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
5100 add_stmt (c_parser_compound_statement (parser));
5101 else
5102 c_parser_statement_after_labels (parser);
5103 return c_end_compound_stmt (body_loc, block, flag_isoc99);
5106 /* Parse the else body of an if statement. This is just parsing a
5107 statement but (a) it is a block in C99, (b) we handle an empty body
5108 specially for the sake of -Wempty-body warnings. */
5110 static tree
5111 c_parser_else_body (c_parser *parser)
5113 location_t else_loc = c_parser_peek_token (parser)->location;
5114 tree block = c_begin_compound_stmt (flag_isoc99);
5115 while (c_parser_next_token_is_keyword (parser, RID_CASE)
5116 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
5117 || (c_parser_next_token_is (parser, CPP_NAME)
5118 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
5119 c_parser_label (parser);
5120 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5122 location_t loc = c_parser_peek_token (parser)->location;
5123 warning_at (loc,
5124 OPT_Wempty_body,
5125 "suggest braces around empty body in an %<else%> statement");
5126 add_stmt (build_empty_stmt (loc));
5127 c_parser_consume_token (parser);
5129 else
5130 c_parser_statement_after_labels (parser);
5131 return c_end_compound_stmt (else_loc, block, flag_isoc99);
5134 /* Parse an if statement (C90 6.6.4, C99 6.8.4).
5136 if-statement:
5137 if ( expression ) statement
5138 if ( expression ) statement else statement
5141 static void
5142 c_parser_if_statement (c_parser *parser)
5144 tree block;
5145 location_t loc;
5146 tree cond;
5147 bool first_if = false;
5148 tree first_body, second_body;
5149 bool in_if_block;
5150 tree if_stmt;
5152 gcc_assert (c_parser_next_token_is_keyword (parser, RID_IF));
5153 c_parser_consume_token (parser);
5154 block = c_begin_compound_stmt (flag_isoc99);
5155 loc = c_parser_peek_token (parser)->location;
5156 cond = c_parser_paren_condition (parser);
5157 in_if_block = parser->in_if_block;
5158 parser->in_if_block = true;
5159 first_body = c_parser_if_body (parser, &first_if);
5160 parser->in_if_block = in_if_block;
5161 if (c_parser_next_token_is_keyword (parser, RID_ELSE))
5163 c_parser_consume_token (parser);
5164 second_body = c_parser_else_body (parser);
5166 else
5167 second_body = NULL_TREE;
5168 c_finish_if_stmt (loc, cond, first_body, second_body, first_if);
5169 if_stmt = c_end_compound_stmt (loc, block, flag_isoc99);
5171 /* If the if statement contains array notations, then we expand them. */
5172 if (flag_cilkplus && contains_array_notation_expr (if_stmt))
5173 if_stmt = fix_conditional_array_notations (if_stmt);
5174 add_stmt (if_stmt);
5177 /* Parse a switch statement (C90 6.6.4, C99 6.8.4).
5179 switch-statement:
5180 switch (expression) statement
5183 static void
5184 c_parser_switch_statement (c_parser *parser)
5186 struct c_expr ce;
5187 tree block, expr, body, save_break;
5188 location_t switch_loc = c_parser_peek_token (parser)->location;
5189 location_t switch_cond_loc;
5190 gcc_assert (c_parser_next_token_is_keyword (parser, RID_SWITCH));
5191 c_parser_consume_token (parser);
5192 block = c_begin_compound_stmt (flag_isoc99);
5193 bool explicit_cast_p = false;
5194 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5196 switch_cond_loc = c_parser_peek_token (parser)->location;
5197 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
5198 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
5199 explicit_cast_p = true;
5200 ce = c_parser_expression (parser);
5201 ce = convert_lvalue_to_rvalue (switch_cond_loc, ce, true, false);
5202 expr = ce.value;
5203 if (flag_cilkplus && contains_array_notation_expr (expr))
5205 error_at (switch_cond_loc,
5206 "array notations cannot be used as a condition for switch "
5207 "statement");
5208 expr = error_mark_node;
5210 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
5212 else
5214 switch_cond_loc = UNKNOWN_LOCATION;
5215 expr = error_mark_node;
5217 c_start_case (switch_loc, switch_cond_loc, expr, explicit_cast_p);
5218 save_break = c_break_label;
5219 c_break_label = NULL_TREE;
5220 body = c_parser_c99_block_statement (parser);
5221 c_finish_case (body);
5222 if (c_break_label)
5224 location_t here = c_parser_peek_token (parser)->location;
5225 tree t = build1 (LABEL_EXPR, void_type_node, c_break_label);
5226 SET_EXPR_LOCATION (t, here);
5227 add_stmt (t);
5229 c_break_label = save_break;
5230 add_stmt (c_end_compound_stmt (switch_loc, block, flag_isoc99));
5233 /* Parse a while statement (C90 6.6.5, C99 6.8.5).
5235 while-statement:
5236 while (expression) statement
5239 static void
5240 c_parser_while_statement (c_parser *parser, bool ivdep)
5242 tree block, cond, body, save_break, save_cont;
5243 location_t loc;
5244 gcc_assert (c_parser_next_token_is_keyword (parser, RID_WHILE));
5245 c_parser_consume_token (parser);
5246 block = c_begin_compound_stmt (flag_isoc99);
5247 loc = c_parser_peek_token (parser)->location;
5248 cond = c_parser_paren_condition (parser);
5249 if (flag_cilkplus && contains_array_notation_expr (cond))
5251 error_at (loc, "array notations cannot be used as a condition for while "
5252 "statement");
5253 cond = error_mark_node;
5256 if (ivdep && cond != error_mark_node)
5257 cond = build2 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
5258 build_int_cst (integer_type_node,
5259 annot_expr_ivdep_kind));
5260 save_break = c_break_label;
5261 c_break_label = NULL_TREE;
5262 save_cont = c_cont_label;
5263 c_cont_label = NULL_TREE;
5264 body = c_parser_c99_block_statement (parser);
5265 c_finish_loop (loc, cond, NULL, body, c_break_label, c_cont_label, true);
5266 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99));
5267 c_break_label = save_break;
5268 c_cont_label = save_cont;
5271 /* Parse a do statement (C90 6.6.5, C99 6.8.5).
5273 do-statement:
5274 do statement while ( expression ) ;
5277 static void
5278 c_parser_do_statement (c_parser *parser, bool ivdep)
5280 tree block, cond, body, save_break, save_cont, new_break, new_cont;
5281 location_t loc;
5282 gcc_assert (c_parser_next_token_is_keyword (parser, RID_DO));
5283 c_parser_consume_token (parser);
5284 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5285 warning_at (c_parser_peek_token (parser)->location,
5286 OPT_Wempty_body,
5287 "suggest braces around empty body in %<do%> statement");
5288 block = c_begin_compound_stmt (flag_isoc99);
5289 loc = c_parser_peek_token (parser)->location;
5290 save_break = c_break_label;
5291 c_break_label = NULL_TREE;
5292 save_cont = c_cont_label;
5293 c_cont_label = NULL_TREE;
5294 body = c_parser_c99_block_statement (parser);
5295 c_parser_require_keyword (parser, RID_WHILE, "expected %<while%>");
5296 new_break = c_break_label;
5297 c_break_label = save_break;
5298 new_cont = c_cont_label;
5299 c_cont_label = save_cont;
5300 cond = c_parser_paren_condition (parser);
5301 if (flag_cilkplus && contains_array_notation_expr (cond))
5303 error_at (loc, "array notations cannot be used as a condition for a "
5304 "do-while statement");
5305 cond = error_mark_node;
5307 if (ivdep && cond != error_mark_node)
5308 cond = build2 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
5309 build_int_cst (integer_type_node,
5310 annot_expr_ivdep_kind));
5311 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
5312 c_parser_skip_to_end_of_block_or_statement (parser);
5313 c_finish_loop (loc, cond, NULL, body, new_break, new_cont, false);
5314 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99));
5317 /* Parse a for statement (C90 6.6.5, C99 6.8.5).
5319 for-statement:
5320 for ( expression[opt] ; expression[opt] ; expression[opt] ) statement
5321 for ( nested-declaration expression[opt] ; expression[opt] ) statement
5323 The form with a declaration is new in C99.
5325 ??? In accordance with the old parser, the declaration may be a
5326 nested function, which is then rejected in check_for_loop_decls,
5327 but does it make any sense for this to be included in the grammar?
5328 Note in particular that the nested function does not include a
5329 trailing ';', whereas the "declaration" production includes one.
5330 Also, can we reject bad declarations earlier and cheaper than
5331 check_for_loop_decls?
5333 In Objective-C, there are two additional variants:
5335 foreach-statement:
5336 for ( expression in expresssion ) statement
5337 for ( declaration in expression ) statement
5339 This is inconsistent with C, because the second variant is allowed
5340 even if c99 is not enabled.
5342 The rest of the comment documents these Objective-C foreach-statement.
5344 Here is the canonical example of the first variant:
5345 for (object in array) { do something with object }
5346 we call the first expression ("object") the "object_expression" and
5347 the second expression ("array") the "collection_expression".
5348 object_expression must be an lvalue of type "id" (a generic Objective-C
5349 object) because the loop works by assigning to object_expression the
5350 various objects from the collection_expression. collection_expression
5351 must evaluate to something of type "id" which responds to the method
5352 countByEnumeratingWithState:objects:count:.
5354 The canonical example of the second variant is:
5355 for (id object in array) { do something with object }
5356 which is completely equivalent to
5358 id object;
5359 for (object in array) { do something with object }
5361 Note that initizializing 'object' in some way (eg, "for ((object =
5362 xxx) in array) { do something with object }") is possibly
5363 technically valid, but completely pointless as 'object' will be
5364 assigned to something else as soon as the loop starts. We should
5365 most likely reject it (TODO).
5367 The beginning of the Objective-C foreach-statement looks exactly
5368 like the beginning of the for-statement, and we can tell it is a
5369 foreach-statement only because the initial declaration or
5370 expression is terminated by 'in' instead of ';'.
5373 static void
5374 c_parser_for_statement (c_parser *parser, bool ivdep)
5376 tree block, cond, incr, save_break, save_cont, body;
5377 /* The following are only used when parsing an ObjC foreach statement. */
5378 tree object_expression;
5379 /* Silence the bogus uninitialized warning. */
5380 tree collection_expression = NULL;
5381 location_t loc = c_parser_peek_token (parser)->location;
5382 location_t for_loc = c_parser_peek_token (parser)->location;
5383 bool is_foreach_statement = false;
5384 gcc_assert (c_parser_next_token_is_keyword (parser, RID_FOR));
5385 c_parser_consume_token (parser);
5386 /* Open a compound statement in Objective-C as well, just in case this is
5387 as foreach expression. */
5388 block = c_begin_compound_stmt (flag_isoc99 || c_dialect_objc ());
5389 cond = error_mark_node;
5390 incr = error_mark_node;
5391 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5393 /* Parse the initialization declaration or expression. */
5394 object_expression = error_mark_node;
5395 parser->objc_could_be_foreach_context = c_dialect_objc ();
5396 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5398 parser->objc_could_be_foreach_context = false;
5399 c_parser_consume_token (parser);
5400 c_finish_expr_stmt (loc, NULL_TREE);
5402 else if (c_parser_next_tokens_start_declaration (parser))
5404 c_parser_declaration_or_fndef (parser, true, true, true, true, true,
5405 &object_expression, vNULL);
5406 parser->objc_could_be_foreach_context = false;
5408 if (c_parser_next_token_is_keyword (parser, RID_IN))
5410 c_parser_consume_token (parser);
5411 is_foreach_statement = true;
5412 if (check_for_loop_decls (for_loc, true) == NULL_TREE)
5413 c_parser_error (parser, "multiple iterating variables in fast enumeration");
5415 else
5416 check_for_loop_decls (for_loc, flag_isoc99);
5418 else if (c_parser_next_token_is_keyword (parser, RID_EXTENSION))
5420 /* __extension__ can start a declaration, but is also an
5421 unary operator that can start an expression. Consume all
5422 but the last of a possible series of __extension__ to
5423 determine which. */
5424 while (c_parser_peek_2nd_token (parser)->type == CPP_KEYWORD
5425 && (c_parser_peek_2nd_token (parser)->keyword
5426 == RID_EXTENSION))
5427 c_parser_consume_token (parser);
5428 if (c_token_starts_declaration (c_parser_peek_2nd_token (parser)))
5430 int ext;
5431 ext = disable_extension_diagnostics ();
5432 c_parser_consume_token (parser);
5433 c_parser_declaration_or_fndef (parser, true, true, true, true,
5434 true, &object_expression, vNULL);
5435 parser->objc_could_be_foreach_context = false;
5437 restore_extension_diagnostics (ext);
5438 if (c_parser_next_token_is_keyword (parser, RID_IN))
5440 c_parser_consume_token (parser);
5441 is_foreach_statement = true;
5442 if (check_for_loop_decls (for_loc, true) == NULL_TREE)
5443 c_parser_error (parser, "multiple iterating variables in fast enumeration");
5445 else
5446 check_for_loop_decls (for_loc, flag_isoc99);
5448 else
5449 goto init_expr;
5451 else
5453 init_expr:
5455 struct c_expr ce;
5456 tree init_expression;
5457 ce = c_parser_expression (parser);
5458 init_expression = ce.value;
5459 parser->objc_could_be_foreach_context = false;
5460 if (c_parser_next_token_is_keyword (parser, RID_IN))
5462 c_parser_consume_token (parser);
5463 is_foreach_statement = true;
5464 if (! lvalue_p (init_expression))
5465 c_parser_error (parser, "invalid iterating variable in fast enumeration");
5466 object_expression = c_fully_fold (init_expression, false, NULL);
5468 else
5470 ce = convert_lvalue_to_rvalue (loc, ce, true, false);
5471 init_expression = ce.value;
5472 c_finish_expr_stmt (loc, init_expression);
5473 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
5477 /* Parse the loop condition. In the case of a foreach
5478 statement, there is no loop condition. */
5479 gcc_assert (!parser->objc_could_be_foreach_context);
5480 if (!is_foreach_statement)
5482 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5484 if (ivdep)
5486 c_parser_error (parser, "missing loop condition in loop with "
5487 "%<GCC ivdep%> pragma");
5488 cond = error_mark_node;
5490 else
5492 c_parser_consume_token (parser);
5493 cond = NULL_TREE;
5496 else
5498 cond = c_parser_condition (parser);
5499 if (flag_cilkplus && contains_array_notation_expr (cond))
5501 error_at (loc, "array notations cannot be used in a "
5502 "condition for a for-loop");
5503 cond = error_mark_node;
5505 c_parser_skip_until_found (parser, CPP_SEMICOLON,
5506 "expected %<;%>");
5508 if (ivdep && cond != error_mark_node)
5509 cond = build2 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
5510 build_int_cst (integer_type_node,
5511 annot_expr_ivdep_kind));
5513 /* Parse the increment expression (the third expression in a
5514 for-statement). In the case of a foreach-statement, this is
5515 the expression that follows the 'in'. */
5516 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
5518 if (is_foreach_statement)
5520 c_parser_error (parser, "missing collection in fast enumeration");
5521 collection_expression = error_mark_node;
5523 else
5524 incr = c_process_expr_stmt (loc, NULL_TREE);
5526 else
5528 if (is_foreach_statement)
5529 collection_expression = c_fully_fold (c_parser_expression (parser).value,
5530 false, NULL);
5531 else
5533 struct c_expr ce = c_parser_expression (parser);
5534 ce = convert_lvalue_to_rvalue (loc, ce, true, false);
5535 incr = c_process_expr_stmt (loc, ce.value);
5538 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
5540 save_break = c_break_label;
5541 c_break_label = NULL_TREE;
5542 save_cont = c_cont_label;
5543 c_cont_label = NULL_TREE;
5544 body = c_parser_c99_block_statement (parser);
5545 if (is_foreach_statement)
5546 objc_finish_foreach_loop (loc, object_expression, collection_expression, body, c_break_label, c_cont_label);
5547 else
5548 c_finish_loop (loc, cond, incr, body, c_break_label, c_cont_label, true);
5549 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99 || c_dialect_objc ()));
5550 c_break_label = save_break;
5551 c_cont_label = save_cont;
5554 /* Parse an asm statement, a GNU extension. This is a full-blown asm
5555 statement with inputs, outputs, clobbers, and volatile tag
5556 allowed.
5558 asm-statement:
5559 asm type-qualifier[opt] ( asm-argument ) ;
5560 asm type-qualifier[opt] goto ( asm-goto-argument ) ;
5562 asm-argument:
5563 asm-string-literal
5564 asm-string-literal : asm-operands[opt]
5565 asm-string-literal : asm-operands[opt] : asm-operands[opt]
5566 asm-string-literal : asm-operands[opt] : asm-operands[opt] : asm-clobbers[opt]
5568 asm-goto-argument:
5569 asm-string-literal : : asm-operands[opt] : asm-clobbers[opt] \
5570 : asm-goto-operands
5572 Qualifiers other than volatile are accepted in the syntax but
5573 warned for. */
5575 static tree
5576 c_parser_asm_statement (c_parser *parser)
5578 tree quals, str, outputs, inputs, clobbers, labels, ret;
5579 bool simple, is_goto;
5580 location_t asm_loc = c_parser_peek_token (parser)->location;
5581 int section, nsections;
5583 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ASM));
5584 c_parser_consume_token (parser);
5585 if (c_parser_next_token_is_keyword (parser, RID_VOLATILE))
5587 quals = c_parser_peek_token (parser)->value;
5588 c_parser_consume_token (parser);
5590 else if (c_parser_next_token_is_keyword (parser, RID_CONST)
5591 || c_parser_next_token_is_keyword (parser, RID_RESTRICT))
5593 warning_at (c_parser_peek_token (parser)->location,
5595 "%E qualifier ignored on asm",
5596 c_parser_peek_token (parser)->value);
5597 quals = NULL_TREE;
5598 c_parser_consume_token (parser);
5600 else
5601 quals = NULL_TREE;
5603 is_goto = false;
5604 if (c_parser_next_token_is_keyword (parser, RID_GOTO))
5606 c_parser_consume_token (parser);
5607 is_goto = true;
5610 /* ??? Follow the C++ parser rather than using the
5611 lex_untranslated_string kludge. */
5612 parser->lex_untranslated_string = true;
5613 ret = NULL;
5615 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5616 goto error;
5618 str = c_parser_asm_string_literal (parser);
5619 if (str == NULL_TREE)
5620 goto error_close_paren;
5622 simple = true;
5623 outputs = NULL_TREE;
5624 inputs = NULL_TREE;
5625 clobbers = NULL_TREE;
5626 labels = NULL_TREE;
5628 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN) && !is_goto)
5629 goto done_asm;
5631 /* Parse each colon-delimited section of operands. */
5632 nsections = 3 + is_goto;
5633 for (section = 0; section < nsections; ++section)
5635 if (!c_parser_require (parser, CPP_COLON,
5636 is_goto
5637 ? "expected %<:%>"
5638 : "expected %<:%> or %<)%>"))
5639 goto error_close_paren;
5641 /* Once past any colon, we're no longer a simple asm. */
5642 simple = false;
5644 if ((!c_parser_next_token_is (parser, CPP_COLON)
5645 && !c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
5646 || section == 3)
5647 switch (section)
5649 case 0:
5650 /* For asm goto, we don't allow output operands, but reserve
5651 the slot for a future extension that does allow them. */
5652 if (!is_goto)
5653 outputs = c_parser_asm_operands (parser);
5654 break;
5655 case 1:
5656 inputs = c_parser_asm_operands (parser);
5657 break;
5658 case 2:
5659 clobbers = c_parser_asm_clobbers (parser);
5660 break;
5661 case 3:
5662 labels = c_parser_asm_goto_operands (parser);
5663 break;
5664 default:
5665 gcc_unreachable ();
5668 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN) && !is_goto)
5669 goto done_asm;
5672 done_asm:
5673 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
5675 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5676 goto error;
5679 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
5680 c_parser_skip_to_end_of_block_or_statement (parser);
5682 ret = build_asm_stmt (quals, build_asm_expr (asm_loc, str, outputs, inputs,
5683 clobbers, labels, simple));
5685 error:
5686 parser->lex_untranslated_string = false;
5687 return ret;
5689 error_close_paren:
5690 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5691 goto error;
5694 /* Parse asm operands, a GNU extension.
5696 asm-operands:
5697 asm-operand
5698 asm-operands , asm-operand
5700 asm-operand:
5701 asm-string-literal ( expression )
5702 [ identifier ] asm-string-literal ( expression )
5705 static tree
5706 c_parser_asm_operands (c_parser *parser)
5708 tree list = NULL_TREE;
5709 while (true)
5711 tree name, str;
5712 struct c_expr expr;
5713 if (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
5715 c_parser_consume_token (parser);
5716 if (c_parser_next_token_is (parser, CPP_NAME))
5718 tree id = c_parser_peek_token (parser)->value;
5719 c_parser_consume_token (parser);
5720 name = build_string (IDENTIFIER_LENGTH (id),
5721 IDENTIFIER_POINTER (id));
5723 else
5725 c_parser_error (parser, "expected identifier");
5726 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
5727 return NULL_TREE;
5729 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
5730 "expected %<]%>");
5732 else
5733 name = NULL_TREE;
5734 str = c_parser_asm_string_literal (parser);
5735 if (str == NULL_TREE)
5736 return NULL_TREE;
5737 parser->lex_untranslated_string = false;
5738 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5740 parser->lex_untranslated_string = true;
5741 return NULL_TREE;
5743 expr = c_parser_expression (parser);
5744 mark_exp_read (expr.value);
5745 parser->lex_untranslated_string = true;
5746 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
5748 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5749 return NULL_TREE;
5751 list = chainon (list, build_tree_list (build_tree_list (name, str),
5752 expr.value));
5753 if (c_parser_next_token_is (parser, CPP_COMMA))
5754 c_parser_consume_token (parser);
5755 else
5756 break;
5758 return list;
5761 /* Parse asm clobbers, a GNU extension.
5763 asm-clobbers:
5764 asm-string-literal
5765 asm-clobbers , asm-string-literal
5768 static tree
5769 c_parser_asm_clobbers (c_parser *parser)
5771 tree list = NULL_TREE;
5772 while (true)
5774 tree str = c_parser_asm_string_literal (parser);
5775 if (str)
5776 list = tree_cons (NULL_TREE, str, list);
5777 else
5778 return NULL_TREE;
5779 if (c_parser_next_token_is (parser, CPP_COMMA))
5780 c_parser_consume_token (parser);
5781 else
5782 break;
5784 return list;
5787 /* Parse asm goto labels, a GNU extension.
5789 asm-goto-operands:
5790 identifier
5791 asm-goto-operands , identifier
5794 static tree
5795 c_parser_asm_goto_operands (c_parser *parser)
5797 tree list = NULL_TREE;
5798 while (true)
5800 tree name, label;
5802 if (c_parser_next_token_is (parser, CPP_NAME))
5804 c_token *tok = c_parser_peek_token (parser);
5805 name = tok->value;
5806 label = lookup_label_for_goto (tok->location, name);
5807 c_parser_consume_token (parser);
5808 TREE_USED (label) = 1;
5810 else
5812 c_parser_error (parser, "expected identifier");
5813 return NULL_TREE;
5816 name = build_string (IDENTIFIER_LENGTH (name),
5817 IDENTIFIER_POINTER (name));
5818 list = tree_cons (name, label, list);
5819 if (c_parser_next_token_is (parser, CPP_COMMA))
5820 c_parser_consume_token (parser);
5821 else
5822 return nreverse (list);
5826 /* Parse an expression other than a compound expression; that is, an
5827 assignment expression (C90 6.3.16, C99 6.5.16). If AFTER is not
5828 NULL then it is an Objective-C message expression which is the
5829 primary-expression starting the expression as an initializer.
5831 assignment-expression:
5832 conditional-expression
5833 unary-expression assignment-operator assignment-expression
5835 assignment-operator: one of
5836 = *= /= %= += -= <<= >>= &= ^= |=
5838 In GNU C we accept any conditional expression on the LHS and
5839 diagnose the invalid lvalue rather than producing a syntax
5840 error. */
5842 static struct c_expr
5843 c_parser_expr_no_commas (c_parser *parser, struct c_expr *after,
5844 tree omp_atomic_lhs)
5846 struct c_expr lhs, rhs, ret;
5847 enum tree_code code;
5848 location_t op_location, exp_location;
5849 gcc_assert (!after || c_dialect_objc ());
5850 lhs = c_parser_conditional_expression (parser, after, omp_atomic_lhs);
5851 op_location = c_parser_peek_token (parser)->location;
5852 switch (c_parser_peek_token (parser)->type)
5854 case CPP_EQ:
5855 code = NOP_EXPR;
5856 break;
5857 case CPP_MULT_EQ:
5858 code = MULT_EXPR;
5859 break;
5860 case CPP_DIV_EQ:
5861 code = TRUNC_DIV_EXPR;
5862 break;
5863 case CPP_MOD_EQ:
5864 code = TRUNC_MOD_EXPR;
5865 break;
5866 case CPP_PLUS_EQ:
5867 code = PLUS_EXPR;
5868 break;
5869 case CPP_MINUS_EQ:
5870 code = MINUS_EXPR;
5871 break;
5872 case CPP_LSHIFT_EQ:
5873 code = LSHIFT_EXPR;
5874 break;
5875 case CPP_RSHIFT_EQ:
5876 code = RSHIFT_EXPR;
5877 break;
5878 case CPP_AND_EQ:
5879 code = BIT_AND_EXPR;
5880 break;
5881 case CPP_XOR_EQ:
5882 code = BIT_XOR_EXPR;
5883 break;
5884 case CPP_OR_EQ:
5885 code = BIT_IOR_EXPR;
5886 break;
5887 default:
5888 return lhs;
5890 c_parser_consume_token (parser);
5891 exp_location = c_parser_peek_token (parser)->location;
5892 rhs = c_parser_expr_no_commas (parser, NULL);
5893 rhs = convert_lvalue_to_rvalue (exp_location, rhs, true, true);
5895 ret.value = build_modify_expr (op_location, lhs.value, lhs.original_type,
5896 code, exp_location, rhs.value,
5897 rhs.original_type);
5898 if (code == NOP_EXPR)
5899 ret.original_code = MODIFY_EXPR;
5900 else
5902 TREE_NO_WARNING (ret.value) = 1;
5903 ret.original_code = ERROR_MARK;
5905 ret.original_type = NULL;
5906 return ret;
5909 /* Parse a conditional expression (C90 6.3.15, C99 6.5.15). If AFTER
5910 is not NULL then it is an Objective-C message expression which is
5911 the primary-expression starting the expression as an initializer.
5913 conditional-expression:
5914 logical-OR-expression
5915 logical-OR-expression ? expression : conditional-expression
5917 GNU extensions:
5919 conditional-expression:
5920 logical-OR-expression ? : conditional-expression
5923 static struct c_expr
5924 c_parser_conditional_expression (c_parser *parser, struct c_expr *after,
5925 tree omp_atomic_lhs)
5927 struct c_expr cond, exp1, exp2, ret;
5928 location_t cond_loc, colon_loc, middle_loc;
5930 gcc_assert (!after || c_dialect_objc ());
5932 cond = c_parser_binary_expression (parser, after, omp_atomic_lhs);
5934 if (c_parser_next_token_is_not (parser, CPP_QUERY))
5935 return cond;
5936 cond_loc = c_parser_peek_token (parser)->location;
5937 cond = convert_lvalue_to_rvalue (cond_loc, cond, true, true);
5938 c_parser_consume_token (parser);
5939 if (c_parser_next_token_is (parser, CPP_COLON))
5941 tree eptype = NULL_TREE;
5943 middle_loc = c_parser_peek_token (parser)->location;
5944 pedwarn (middle_loc, OPT_Wpedantic,
5945 "ISO C forbids omitting the middle term of a ?: expression");
5946 warn_for_omitted_condop (middle_loc, cond.value);
5947 if (TREE_CODE (cond.value) == EXCESS_PRECISION_EXPR)
5949 eptype = TREE_TYPE (cond.value);
5950 cond.value = TREE_OPERAND (cond.value, 0);
5952 /* Make sure first operand is calculated only once. */
5953 exp1.value = c_save_expr (default_conversion (cond.value));
5954 if (eptype)
5955 exp1.value = build1 (EXCESS_PRECISION_EXPR, eptype, exp1.value);
5956 exp1.original_type = NULL;
5957 cond.value = c_objc_common_truthvalue_conversion (cond_loc, exp1.value);
5958 c_inhibit_evaluation_warnings += cond.value == truthvalue_true_node;
5960 else
5962 cond.value
5963 = c_objc_common_truthvalue_conversion
5964 (cond_loc, default_conversion (cond.value));
5965 c_inhibit_evaluation_warnings += cond.value == truthvalue_false_node;
5966 exp1 = c_parser_expression_conv (parser);
5967 mark_exp_read (exp1.value);
5968 c_inhibit_evaluation_warnings +=
5969 ((cond.value == truthvalue_true_node)
5970 - (cond.value == truthvalue_false_node));
5973 colon_loc = c_parser_peek_token (parser)->location;
5974 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
5976 c_inhibit_evaluation_warnings -= cond.value == truthvalue_true_node;
5977 ret.value = error_mark_node;
5978 ret.original_code = ERROR_MARK;
5979 ret.original_type = NULL;
5980 return ret;
5983 location_t exp2_loc = c_parser_peek_token (parser)->location;
5984 exp2 = c_parser_conditional_expression (parser, NULL, NULL_TREE);
5985 exp2 = convert_lvalue_to_rvalue (exp2_loc, exp2, true, true);
5987 c_inhibit_evaluation_warnings -= cond.value == truthvalue_true_node;
5988 ret.value = build_conditional_expr (colon_loc, cond.value,
5989 cond.original_code == C_MAYBE_CONST_EXPR,
5990 exp1.value, exp1.original_type,
5991 exp2.value, exp2.original_type);
5992 ret.original_code = ERROR_MARK;
5993 if (exp1.value == error_mark_node || exp2.value == error_mark_node)
5994 ret.original_type = NULL;
5995 else
5997 tree t1, t2;
5999 /* If both sides are enum type, the default conversion will have
6000 made the type of the result be an integer type. We want to
6001 remember the enum types we started with. */
6002 t1 = exp1.original_type ? exp1.original_type : TREE_TYPE (exp1.value);
6003 t2 = exp2.original_type ? exp2.original_type : TREE_TYPE (exp2.value);
6004 ret.original_type = ((t1 != error_mark_node
6005 && t2 != error_mark_node
6006 && (TYPE_MAIN_VARIANT (t1)
6007 == TYPE_MAIN_VARIANT (t2)))
6008 ? t1
6009 : NULL);
6011 return ret;
6014 /* Parse a binary expression; that is, a logical-OR-expression (C90
6015 6.3.5-6.3.14, C99 6.5.5-6.5.14). If AFTER is not NULL then it is
6016 an Objective-C message expression which is the primary-expression
6017 starting the expression as an initializer.
6019 OMP_ATOMIC_LHS is NULL, unless parsing OpenMP #pragma omp atomic,
6020 when it should be the unfolded lhs. In a valid OpenMP source,
6021 one of the operands of the toplevel binary expression must be equal
6022 to it. In that case, just return a build2 created binary operation
6023 rather than result of parser_build_binary_op.
6025 multiplicative-expression:
6026 cast-expression
6027 multiplicative-expression * cast-expression
6028 multiplicative-expression / cast-expression
6029 multiplicative-expression % cast-expression
6031 additive-expression:
6032 multiplicative-expression
6033 additive-expression + multiplicative-expression
6034 additive-expression - multiplicative-expression
6036 shift-expression:
6037 additive-expression
6038 shift-expression << additive-expression
6039 shift-expression >> additive-expression
6041 relational-expression:
6042 shift-expression
6043 relational-expression < shift-expression
6044 relational-expression > shift-expression
6045 relational-expression <= shift-expression
6046 relational-expression >= shift-expression
6048 equality-expression:
6049 relational-expression
6050 equality-expression == relational-expression
6051 equality-expression != relational-expression
6053 AND-expression:
6054 equality-expression
6055 AND-expression & equality-expression
6057 exclusive-OR-expression:
6058 AND-expression
6059 exclusive-OR-expression ^ AND-expression
6061 inclusive-OR-expression:
6062 exclusive-OR-expression
6063 inclusive-OR-expression | exclusive-OR-expression
6065 logical-AND-expression:
6066 inclusive-OR-expression
6067 logical-AND-expression && inclusive-OR-expression
6069 logical-OR-expression:
6070 logical-AND-expression
6071 logical-OR-expression || logical-AND-expression
6074 static struct c_expr
6075 c_parser_binary_expression (c_parser *parser, struct c_expr *after,
6076 tree omp_atomic_lhs)
6078 /* A binary expression is parsed using operator-precedence parsing,
6079 with the operands being cast expressions. All the binary
6080 operators are left-associative. Thus a binary expression is of
6081 form:
6083 E0 op1 E1 op2 E2 ...
6085 which we represent on a stack. On the stack, the precedence
6086 levels are strictly increasing. When a new operator is
6087 encountered of higher precedence than that at the top of the
6088 stack, it is pushed; its LHS is the top expression, and its RHS
6089 is everything parsed until it is popped. When a new operator is
6090 encountered with precedence less than or equal to that at the top
6091 of the stack, triples E[i-1] op[i] E[i] are popped and replaced
6092 by the result of the operation until the operator at the top of
6093 the stack has lower precedence than the new operator or there is
6094 only one element on the stack; then the top expression is the LHS
6095 of the new operator. In the case of logical AND and OR
6096 expressions, we also need to adjust c_inhibit_evaluation_warnings
6097 as appropriate when the operators are pushed and popped. */
6099 struct {
6100 /* The expression at this stack level. */
6101 struct c_expr expr;
6102 /* The precedence of the operator on its left, PREC_NONE at the
6103 bottom of the stack. */
6104 enum c_parser_prec prec;
6105 /* The operation on its left. */
6106 enum tree_code op;
6107 /* The source location of this operation. */
6108 location_t loc;
6109 } stack[NUM_PRECS];
6110 int sp;
6111 /* Location of the binary operator. */
6112 location_t binary_loc = UNKNOWN_LOCATION; /* Quiet warning. */
6113 #define POP \
6114 do { \
6115 switch (stack[sp].op) \
6117 case TRUTH_ANDIF_EXPR: \
6118 c_inhibit_evaluation_warnings -= (stack[sp - 1].expr.value \
6119 == truthvalue_false_node); \
6120 break; \
6121 case TRUTH_ORIF_EXPR: \
6122 c_inhibit_evaluation_warnings -= (stack[sp - 1].expr.value \
6123 == truthvalue_true_node); \
6124 break; \
6125 default: \
6126 break; \
6128 stack[sp - 1].expr \
6129 = convert_lvalue_to_rvalue (stack[sp - 1].loc, \
6130 stack[sp - 1].expr, true, true); \
6131 stack[sp].expr \
6132 = convert_lvalue_to_rvalue (stack[sp].loc, \
6133 stack[sp].expr, true, true); \
6134 if (__builtin_expect (omp_atomic_lhs != NULL_TREE, 0) && sp == 1 \
6135 && c_parser_peek_token (parser)->type == CPP_SEMICOLON \
6136 && ((1 << stack[sp].prec) \
6137 & (1 << (PREC_BITOR | PREC_BITXOR | PREC_BITAND | PREC_SHIFT \
6138 | PREC_ADD | PREC_MULT))) \
6139 && stack[sp].op != TRUNC_MOD_EXPR \
6140 && stack[0].expr.value != error_mark_node \
6141 && stack[1].expr.value != error_mark_node \
6142 && (c_tree_equal (stack[0].expr.value, omp_atomic_lhs) \
6143 || c_tree_equal (stack[1].expr.value, omp_atomic_lhs))) \
6144 stack[0].expr.value \
6145 = build2 (stack[1].op, TREE_TYPE (stack[0].expr.value), \
6146 stack[0].expr.value, stack[1].expr.value); \
6147 else \
6148 stack[sp - 1].expr = parser_build_binary_op (stack[sp].loc, \
6149 stack[sp].op, \
6150 stack[sp - 1].expr, \
6151 stack[sp].expr); \
6152 sp--; \
6153 } while (0)
6154 gcc_assert (!after || c_dialect_objc ());
6155 stack[0].loc = c_parser_peek_token (parser)->location;
6156 stack[0].expr = c_parser_cast_expression (parser, after);
6157 stack[0].prec = PREC_NONE;
6158 sp = 0;
6159 while (true)
6161 enum c_parser_prec oprec;
6162 enum tree_code ocode;
6163 if (parser->error)
6164 goto out;
6165 switch (c_parser_peek_token (parser)->type)
6167 case CPP_MULT:
6168 oprec = PREC_MULT;
6169 ocode = MULT_EXPR;
6170 break;
6171 case CPP_DIV:
6172 oprec = PREC_MULT;
6173 ocode = TRUNC_DIV_EXPR;
6174 break;
6175 case CPP_MOD:
6176 oprec = PREC_MULT;
6177 ocode = TRUNC_MOD_EXPR;
6178 break;
6179 case CPP_PLUS:
6180 oprec = PREC_ADD;
6181 ocode = PLUS_EXPR;
6182 break;
6183 case CPP_MINUS:
6184 oprec = PREC_ADD;
6185 ocode = MINUS_EXPR;
6186 break;
6187 case CPP_LSHIFT:
6188 oprec = PREC_SHIFT;
6189 ocode = LSHIFT_EXPR;
6190 break;
6191 case CPP_RSHIFT:
6192 oprec = PREC_SHIFT;
6193 ocode = RSHIFT_EXPR;
6194 break;
6195 case CPP_LESS:
6196 oprec = PREC_REL;
6197 ocode = LT_EXPR;
6198 break;
6199 case CPP_GREATER:
6200 oprec = PREC_REL;
6201 ocode = GT_EXPR;
6202 break;
6203 case CPP_LESS_EQ:
6204 oprec = PREC_REL;
6205 ocode = LE_EXPR;
6206 break;
6207 case CPP_GREATER_EQ:
6208 oprec = PREC_REL;
6209 ocode = GE_EXPR;
6210 break;
6211 case CPP_EQ_EQ:
6212 oprec = PREC_EQ;
6213 ocode = EQ_EXPR;
6214 break;
6215 case CPP_NOT_EQ:
6216 oprec = PREC_EQ;
6217 ocode = NE_EXPR;
6218 break;
6219 case CPP_AND:
6220 oprec = PREC_BITAND;
6221 ocode = BIT_AND_EXPR;
6222 break;
6223 case CPP_XOR:
6224 oprec = PREC_BITXOR;
6225 ocode = BIT_XOR_EXPR;
6226 break;
6227 case CPP_OR:
6228 oprec = PREC_BITOR;
6229 ocode = BIT_IOR_EXPR;
6230 break;
6231 case CPP_AND_AND:
6232 oprec = PREC_LOGAND;
6233 ocode = TRUTH_ANDIF_EXPR;
6234 break;
6235 case CPP_OR_OR:
6236 oprec = PREC_LOGOR;
6237 ocode = TRUTH_ORIF_EXPR;
6238 break;
6239 default:
6240 /* Not a binary operator, so end of the binary
6241 expression. */
6242 goto out;
6244 binary_loc = c_parser_peek_token (parser)->location;
6245 while (oprec <= stack[sp].prec)
6246 POP;
6247 c_parser_consume_token (parser);
6248 switch (ocode)
6250 case TRUTH_ANDIF_EXPR:
6251 stack[sp].expr
6252 = convert_lvalue_to_rvalue (stack[sp].loc,
6253 stack[sp].expr, true, true);
6254 stack[sp].expr.value = c_objc_common_truthvalue_conversion
6255 (stack[sp].loc, default_conversion (stack[sp].expr.value));
6256 c_inhibit_evaluation_warnings += (stack[sp].expr.value
6257 == truthvalue_false_node);
6258 break;
6259 case TRUTH_ORIF_EXPR:
6260 stack[sp].expr
6261 = convert_lvalue_to_rvalue (stack[sp].loc,
6262 stack[sp].expr, true, true);
6263 stack[sp].expr.value = c_objc_common_truthvalue_conversion
6264 (stack[sp].loc, default_conversion (stack[sp].expr.value));
6265 c_inhibit_evaluation_warnings += (stack[sp].expr.value
6266 == truthvalue_true_node);
6267 break;
6268 default:
6269 break;
6271 sp++;
6272 stack[sp].loc = binary_loc;
6273 stack[sp].expr = c_parser_cast_expression (parser, NULL);
6274 stack[sp].prec = oprec;
6275 stack[sp].op = ocode;
6276 stack[sp].loc = binary_loc;
6278 out:
6279 while (sp > 0)
6280 POP;
6281 return stack[0].expr;
6282 #undef POP
6285 /* Parse a cast expression (C90 6.3.4, C99 6.5.4). If AFTER is not
6286 NULL then it is an Objective-C message expression which is the
6287 primary-expression starting the expression as an initializer.
6289 cast-expression:
6290 unary-expression
6291 ( type-name ) unary-expression
6294 static struct c_expr
6295 c_parser_cast_expression (c_parser *parser, struct c_expr *after)
6297 location_t cast_loc = c_parser_peek_token (parser)->location;
6298 gcc_assert (!after || c_dialect_objc ());
6299 if (after)
6300 return c_parser_postfix_expression_after_primary (parser,
6301 cast_loc, *after);
6302 /* If the expression begins with a parenthesized type name, it may
6303 be either a cast or a compound literal; we need to see whether
6304 the next character is '{' to tell the difference. If not, it is
6305 an unary expression. Full detection of unknown typenames here
6306 would require a 3-token lookahead. */
6307 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
6308 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
6310 struct c_type_name *type_name;
6311 struct c_expr ret;
6312 struct c_expr expr;
6313 c_parser_consume_token (parser);
6314 type_name = c_parser_type_name (parser);
6315 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
6316 if (type_name == NULL)
6318 ret.value = error_mark_node;
6319 ret.original_code = ERROR_MARK;
6320 ret.original_type = NULL;
6321 return ret;
6324 /* Save casted types in the function's used types hash table. */
6325 used_types_insert (type_name->specs->type);
6327 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
6328 return c_parser_postfix_expression_after_paren_type (parser, type_name,
6329 cast_loc);
6331 location_t expr_loc = c_parser_peek_token (parser)->location;
6332 expr = c_parser_cast_expression (parser, NULL);
6333 expr = convert_lvalue_to_rvalue (expr_loc, expr, true, true);
6335 ret.value = c_cast_expr (cast_loc, type_name, expr.value);
6336 ret.original_code = ERROR_MARK;
6337 ret.original_type = NULL;
6338 return ret;
6340 else
6341 return c_parser_unary_expression (parser);
6344 /* Parse an unary expression (C90 6.3.3, C99 6.5.3).
6346 unary-expression:
6347 postfix-expression
6348 ++ unary-expression
6349 -- unary-expression
6350 unary-operator cast-expression
6351 sizeof unary-expression
6352 sizeof ( type-name )
6354 unary-operator: one of
6355 & * + - ~ !
6357 GNU extensions:
6359 unary-expression:
6360 __alignof__ unary-expression
6361 __alignof__ ( type-name )
6362 && identifier
6364 (C11 permits _Alignof with type names only.)
6366 unary-operator: one of
6367 __extension__ __real__ __imag__
6369 Transactional Memory:
6371 unary-expression:
6372 transaction-expression
6374 In addition, the GNU syntax treats ++ and -- as unary operators, so
6375 they may be applied to cast expressions with errors for non-lvalues
6376 given later. */
6378 static struct c_expr
6379 c_parser_unary_expression (c_parser *parser)
6381 int ext;
6382 struct c_expr ret, op;
6383 location_t op_loc = c_parser_peek_token (parser)->location;
6384 location_t exp_loc;
6385 ret.original_code = ERROR_MARK;
6386 ret.original_type = NULL;
6387 switch (c_parser_peek_token (parser)->type)
6389 case CPP_PLUS_PLUS:
6390 c_parser_consume_token (parser);
6391 exp_loc = c_parser_peek_token (parser)->location;
6392 op = c_parser_cast_expression (parser, NULL);
6394 /* If there is array notations in op, we expand them. */
6395 if (flag_cilkplus && TREE_CODE (op.value) == ARRAY_NOTATION_REF)
6396 return fix_array_notation_expr (exp_loc, PREINCREMENT_EXPR, op);
6397 else
6399 op = default_function_array_read_conversion (exp_loc, op);
6400 return parser_build_unary_op (op_loc, PREINCREMENT_EXPR, op);
6402 case CPP_MINUS_MINUS:
6403 c_parser_consume_token (parser);
6404 exp_loc = c_parser_peek_token (parser)->location;
6405 op = c_parser_cast_expression (parser, NULL);
6407 /* If there is array notations in op, we expand them. */
6408 if (flag_cilkplus && TREE_CODE (op.value) == ARRAY_NOTATION_REF)
6409 return fix_array_notation_expr (exp_loc, PREDECREMENT_EXPR, op);
6410 else
6412 op = default_function_array_read_conversion (exp_loc, op);
6413 return parser_build_unary_op (op_loc, PREDECREMENT_EXPR, op);
6415 case CPP_AND:
6416 c_parser_consume_token (parser);
6417 op = c_parser_cast_expression (parser, NULL);
6418 mark_exp_read (op.value);
6419 return parser_build_unary_op (op_loc, ADDR_EXPR, op);
6420 case CPP_MULT:
6421 c_parser_consume_token (parser);
6422 exp_loc = c_parser_peek_token (parser)->location;
6423 op = c_parser_cast_expression (parser, NULL);
6424 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
6425 ret.value = build_indirect_ref (op_loc, op.value, RO_UNARY_STAR);
6426 return ret;
6427 case CPP_PLUS:
6428 if (!c_dialect_objc () && !in_system_header_at (input_location))
6429 warning_at (op_loc,
6430 OPT_Wtraditional,
6431 "traditional C rejects the unary plus operator");
6432 c_parser_consume_token (parser);
6433 exp_loc = c_parser_peek_token (parser)->location;
6434 op = c_parser_cast_expression (parser, NULL);
6435 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
6436 return parser_build_unary_op (op_loc, CONVERT_EXPR, op);
6437 case CPP_MINUS:
6438 c_parser_consume_token (parser);
6439 exp_loc = c_parser_peek_token (parser)->location;
6440 op = c_parser_cast_expression (parser, NULL);
6441 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
6442 return parser_build_unary_op (op_loc, NEGATE_EXPR, op);
6443 case CPP_COMPL:
6444 c_parser_consume_token (parser);
6445 exp_loc = c_parser_peek_token (parser)->location;
6446 op = c_parser_cast_expression (parser, NULL);
6447 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
6448 return parser_build_unary_op (op_loc, BIT_NOT_EXPR, op);
6449 case CPP_NOT:
6450 c_parser_consume_token (parser);
6451 exp_loc = c_parser_peek_token (parser)->location;
6452 op = c_parser_cast_expression (parser, NULL);
6453 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
6454 return parser_build_unary_op (op_loc, TRUTH_NOT_EXPR, op);
6455 case CPP_AND_AND:
6456 /* Refer to the address of a label as a pointer. */
6457 c_parser_consume_token (parser);
6458 if (c_parser_next_token_is (parser, CPP_NAME))
6460 ret.value = finish_label_address_expr
6461 (c_parser_peek_token (parser)->value, op_loc);
6462 c_parser_consume_token (parser);
6464 else
6466 c_parser_error (parser, "expected identifier");
6467 ret.value = error_mark_node;
6469 return ret;
6470 case CPP_KEYWORD:
6471 switch (c_parser_peek_token (parser)->keyword)
6473 case RID_SIZEOF:
6474 return c_parser_sizeof_expression (parser);
6475 case RID_ALIGNOF:
6476 return c_parser_alignof_expression (parser);
6477 case RID_EXTENSION:
6478 c_parser_consume_token (parser);
6479 ext = disable_extension_diagnostics ();
6480 ret = c_parser_cast_expression (parser, NULL);
6481 restore_extension_diagnostics (ext);
6482 return ret;
6483 case RID_REALPART:
6484 c_parser_consume_token (parser);
6485 exp_loc = c_parser_peek_token (parser)->location;
6486 op = c_parser_cast_expression (parser, NULL);
6487 op = default_function_array_conversion (exp_loc, op);
6488 return parser_build_unary_op (op_loc, REALPART_EXPR, op);
6489 case RID_IMAGPART:
6490 c_parser_consume_token (parser);
6491 exp_loc = c_parser_peek_token (parser)->location;
6492 op = c_parser_cast_expression (parser, NULL);
6493 op = default_function_array_conversion (exp_loc, op);
6494 return parser_build_unary_op (op_loc, IMAGPART_EXPR, op);
6495 case RID_TRANSACTION_ATOMIC:
6496 case RID_TRANSACTION_RELAXED:
6497 return c_parser_transaction_expression (parser,
6498 c_parser_peek_token (parser)->keyword);
6499 default:
6500 return c_parser_postfix_expression (parser);
6502 default:
6503 return c_parser_postfix_expression (parser);
6507 /* Parse a sizeof expression. */
6509 static struct c_expr
6510 c_parser_sizeof_expression (c_parser *parser)
6512 struct c_expr expr;
6513 location_t expr_loc;
6514 gcc_assert (c_parser_next_token_is_keyword (parser, RID_SIZEOF));
6515 c_parser_consume_token (parser);
6516 c_inhibit_evaluation_warnings++;
6517 in_sizeof++;
6518 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
6519 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
6521 /* Either sizeof ( type-name ) or sizeof unary-expression
6522 starting with a compound literal. */
6523 struct c_type_name *type_name;
6524 c_parser_consume_token (parser);
6525 expr_loc = c_parser_peek_token (parser)->location;
6526 type_name = c_parser_type_name (parser);
6527 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
6528 if (type_name == NULL)
6530 struct c_expr ret;
6531 c_inhibit_evaluation_warnings--;
6532 in_sizeof--;
6533 ret.value = error_mark_node;
6534 ret.original_code = ERROR_MARK;
6535 ret.original_type = NULL;
6536 return ret;
6538 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
6540 expr = c_parser_postfix_expression_after_paren_type (parser,
6541 type_name,
6542 expr_loc);
6543 goto sizeof_expr;
6545 /* sizeof ( type-name ). */
6546 c_inhibit_evaluation_warnings--;
6547 in_sizeof--;
6548 return c_expr_sizeof_type (expr_loc, type_name);
6550 else
6552 expr_loc = c_parser_peek_token (parser)->location;
6553 expr = c_parser_unary_expression (parser);
6554 sizeof_expr:
6555 c_inhibit_evaluation_warnings--;
6556 in_sizeof--;
6557 mark_exp_read (expr.value);
6558 if (TREE_CODE (expr.value) == COMPONENT_REF
6559 && DECL_C_BIT_FIELD (TREE_OPERAND (expr.value, 1)))
6560 error_at (expr_loc, "%<sizeof%> applied to a bit-field");
6561 return c_expr_sizeof_expr (expr_loc, expr);
6565 /* Parse an alignof expression. */
6567 static struct c_expr
6568 c_parser_alignof_expression (c_parser *parser)
6570 struct c_expr expr;
6571 location_t loc = c_parser_peek_token (parser)->location;
6572 tree alignof_spelling = c_parser_peek_token (parser)->value;
6573 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ALIGNOF));
6574 bool is_c11_alignof = strcmp (IDENTIFIER_POINTER (alignof_spelling),
6575 "_Alignof") == 0;
6576 /* A diagnostic is not required for the use of this identifier in
6577 the implementation namespace; only diagnose it for the C11
6578 spelling because of existing code using the other spellings. */
6579 if (is_c11_alignof)
6581 if (flag_isoc99)
6582 pedwarn_c99 (loc, OPT_Wpedantic, "ISO C99 does not support %qE",
6583 alignof_spelling);
6584 else
6585 pedwarn_c99 (loc, OPT_Wpedantic, "ISO C90 does not support %qE",
6586 alignof_spelling);
6588 c_parser_consume_token (parser);
6589 c_inhibit_evaluation_warnings++;
6590 in_alignof++;
6591 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
6592 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
6594 /* Either __alignof__ ( type-name ) or __alignof__
6595 unary-expression starting with a compound literal. */
6596 location_t loc;
6597 struct c_type_name *type_name;
6598 struct c_expr ret;
6599 c_parser_consume_token (parser);
6600 loc = c_parser_peek_token (parser)->location;
6601 type_name = c_parser_type_name (parser);
6602 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
6603 if (type_name == NULL)
6605 struct c_expr ret;
6606 c_inhibit_evaluation_warnings--;
6607 in_alignof--;
6608 ret.value = error_mark_node;
6609 ret.original_code = ERROR_MARK;
6610 ret.original_type = NULL;
6611 return ret;
6613 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
6615 expr = c_parser_postfix_expression_after_paren_type (parser,
6616 type_name,
6617 loc);
6618 goto alignof_expr;
6620 /* alignof ( type-name ). */
6621 c_inhibit_evaluation_warnings--;
6622 in_alignof--;
6623 ret.value = c_sizeof_or_alignof_type (loc, groktypename (type_name,
6624 NULL, NULL),
6625 false, is_c11_alignof, 1);
6626 ret.original_code = ERROR_MARK;
6627 ret.original_type = NULL;
6628 return ret;
6630 else
6632 struct c_expr ret;
6633 expr = c_parser_unary_expression (parser);
6634 alignof_expr:
6635 mark_exp_read (expr.value);
6636 c_inhibit_evaluation_warnings--;
6637 in_alignof--;
6638 pedwarn (loc, OPT_Wpedantic, "ISO C does not allow %<%E (expression)%>",
6639 alignof_spelling);
6640 ret.value = c_alignof_expr (loc, expr.value);
6641 ret.original_code = ERROR_MARK;
6642 ret.original_type = NULL;
6643 return ret;
6647 /* Helper function to read arguments of builtins which are interfaces
6648 for the middle-end nodes like COMPLEX_EXPR, VEC_PERM_EXPR and
6649 others. The name of the builtin is passed using BNAME parameter.
6650 Function returns true if there were no errors while parsing and
6651 stores the arguments in CEXPR_LIST. */
6652 static bool
6653 c_parser_get_builtin_args (c_parser *parser, const char *bname,
6654 vec<c_expr_t, va_gc> **ret_cexpr_list,
6655 bool choose_expr_p)
6657 location_t loc = c_parser_peek_token (parser)->location;
6658 vec<c_expr_t, va_gc> *cexpr_list;
6659 c_expr_t expr;
6660 bool saved_force_folding_builtin_constant_p;
6662 *ret_cexpr_list = NULL;
6663 if (c_parser_next_token_is_not (parser, CPP_OPEN_PAREN))
6665 error_at (loc, "cannot take address of %qs", bname);
6666 return false;
6669 c_parser_consume_token (parser);
6671 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
6673 c_parser_consume_token (parser);
6674 return true;
6677 saved_force_folding_builtin_constant_p
6678 = force_folding_builtin_constant_p;
6679 force_folding_builtin_constant_p |= choose_expr_p;
6680 expr = c_parser_expr_no_commas (parser, NULL);
6681 force_folding_builtin_constant_p
6682 = saved_force_folding_builtin_constant_p;
6683 vec_alloc (cexpr_list, 1);
6684 vec_safe_push (cexpr_list, expr);
6685 while (c_parser_next_token_is (parser, CPP_COMMA))
6687 c_parser_consume_token (parser);
6688 expr = c_parser_expr_no_commas (parser, NULL);
6689 vec_safe_push (cexpr_list, expr);
6692 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
6693 return false;
6695 *ret_cexpr_list = cexpr_list;
6696 return true;
6699 /* This represents a single generic-association. */
6701 struct c_generic_association
6703 /* The location of the starting token of the type. */
6704 location_t type_location;
6705 /* The association's type, or NULL_TREE for 'default'. */
6706 tree type;
6707 /* The association's expression. */
6708 struct c_expr expression;
6711 /* Parse a generic-selection. (C11 6.5.1.1).
6713 generic-selection:
6714 _Generic ( assignment-expression , generic-assoc-list )
6716 generic-assoc-list:
6717 generic-association
6718 generic-assoc-list , generic-association
6720 generic-association:
6721 type-name : assignment-expression
6722 default : assignment-expression
6725 static struct c_expr
6726 c_parser_generic_selection (c_parser *parser)
6728 vec<c_generic_association> associations = vNULL;
6729 struct c_expr selector, error_expr;
6730 tree selector_type;
6731 struct c_generic_association matched_assoc;
6732 bool match_found = false;
6733 location_t generic_loc, selector_loc;
6735 error_expr.original_code = ERROR_MARK;
6736 error_expr.original_type = NULL;
6737 error_expr.value = error_mark_node;
6738 matched_assoc.type_location = UNKNOWN_LOCATION;
6739 matched_assoc.type = NULL_TREE;
6740 matched_assoc.expression = error_expr;
6742 gcc_assert (c_parser_next_token_is_keyword (parser, RID_GENERIC));
6743 generic_loc = c_parser_peek_token (parser)->location;
6744 c_parser_consume_token (parser);
6745 if (flag_isoc99)
6746 pedwarn_c99 (generic_loc, OPT_Wpedantic,
6747 "ISO C99 does not support %<_Generic%>");
6748 else
6749 pedwarn_c99 (generic_loc, OPT_Wpedantic,
6750 "ISO C90 does not support %<_Generic%>");
6752 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6753 return error_expr;
6755 c_inhibit_evaluation_warnings++;
6756 selector_loc = c_parser_peek_token (parser)->location;
6757 selector = c_parser_expr_no_commas (parser, NULL);
6758 selector = default_function_array_conversion (selector_loc, selector);
6759 c_inhibit_evaluation_warnings--;
6761 if (selector.value == error_mark_node)
6763 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6764 return selector;
6766 selector_type = TREE_TYPE (selector.value);
6767 /* In ISO C terms, rvalues (including the controlling expression of
6768 _Generic) do not have qualified types. */
6769 if (TREE_CODE (selector_type) != ARRAY_TYPE)
6770 selector_type = TYPE_MAIN_VARIANT (selector_type);
6771 /* In ISO C terms, _Noreturn is not part of the type of expressions
6772 such as &abort, but in GCC it is represented internally as a type
6773 qualifier. */
6774 if (FUNCTION_POINTER_TYPE_P (selector_type)
6775 && TYPE_QUALS (TREE_TYPE (selector_type)) != TYPE_UNQUALIFIED)
6776 selector_type
6777 = build_pointer_type (TYPE_MAIN_VARIANT (TREE_TYPE (selector_type)));
6779 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
6781 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6782 return error_expr;
6785 while (1)
6787 struct c_generic_association assoc, *iter;
6788 unsigned int ix;
6789 c_token *token = c_parser_peek_token (parser);
6791 assoc.type_location = token->location;
6792 if (token->type == CPP_KEYWORD && token->keyword == RID_DEFAULT)
6794 c_parser_consume_token (parser);
6795 assoc.type = NULL_TREE;
6797 else
6799 struct c_type_name *type_name;
6801 type_name = c_parser_type_name (parser);
6802 if (type_name == NULL)
6804 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6805 goto error_exit;
6807 assoc.type = groktypename (type_name, NULL, NULL);
6808 if (assoc.type == error_mark_node)
6810 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6811 goto error_exit;
6814 if (TREE_CODE (assoc.type) == FUNCTION_TYPE)
6815 error_at (assoc.type_location,
6816 "%<_Generic%> association has function type");
6817 else if (!COMPLETE_TYPE_P (assoc.type))
6818 error_at (assoc.type_location,
6819 "%<_Generic%> association has incomplete type");
6821 if (variably_modified_type_p (assoc.type, NULL_TREE))
6822 error_at (assoc.type_location,
6823 "%<_Generic%> association has "
6824 "variable length type");
6827 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
6829 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6830 goto error_exit;
6833 assoc.expression = c_parser_expr_no_commas (parser, NULL);
6834 if (assoc.expression.value == error_mark_node)
6836 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6837 goto error_exit;
6840 for (ix = 0; associations.iterate (ix, &iter); ++ix)
6842 if (assoc.type == NULL_TREE)
6844 if (iter->type == NULL_TREE)
6846 error_at (assoc.type_location,
6847 "duplicate %<default%> case in %<_Generic%>");
6848 inform (iter->type_location, "original %<default%> is here");
6851 else if (iter->type != NULL_TREE)
6853 if (comptypes (assoc.type, iter->type))
6855 error_at (assoc.type_location,
6856 "%<_Generic%> specifies two compatible types");
6857 inform (iter->type_location, "compatible type is here");
6862 if (assoc.type == NULL_TREE)
6864 if (!match_found)
6866 matched_assoc = assoc;
6867 match_found = true;
6870 else if (comptypes (assoc.type, selector_type))
6872 if (!match_found || matched_assoc.type == NULL_TREE)
6874 matched_assoc = assoc;
6875 match_found = true;
6877 else
6879 error_at (assoc.type_location,
6880 "%<_Generic> selector matches multiple associations");
6881 inform (matched_assoc.type_location,
6882 "other match is here");
6886 associations.safe_push (assoc);
6888 if (c_parser_peek_token (parser)->type != CPP_COMMA)
6889 break;
6890 c_parser_consume_token (parser);
6893 associations.release ();
6895 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
6897 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6898 return error_expr;
6901 if (!match_found)
6903 error_at (selector_loc, "%<_Generic%> selector of type %qT is not "
6904 "compatible with any association",
6905 selector_type);
6906 return error_expr;
6909 return matched_assoc.expression;
6911 error_exit:
6912 associations.release ();
6913 return error_expr;
6916 /* Parse a postfix expression (C90 6.3.1-6.3.2, C99 6.5.1-6.5.2).
6918 postfix-expression:
6919 primary-expression
6920 postfix-expression [ expression ]
6921 postfix-expression ( argument-expression-list[opt] )
6922 postfix-expression . identifier
6923 postfix-expression -> identifier
6924 postfix-expression ++
6925 postfix-expression --
6926 ( type-name ) { initializer-list }
6927 ( type-name ) { initializer-list , }
6929 argument-expression-list:
6930 argument-expression
6931 argument-expression-list , argument-expression
6933 primary-expression:
6934 identifier
6935 constant
6936 string-literal
6937 ( expression )
6938 generic-selection
6940 GNU extensions:
6942 primary-expression:
6943 __func__
6944 (treated as a keyword in GNU C)
6945 __FUNCTION__
6946 __PRETTY_FUNCTION__
6947 ( compound-statement )
6948 __builtin_va_arg ( assignment-expression , type-name )
6949 __builtin_offsetof ( type-name , offsetof-member-designator )
6950 __builtin_choose_expr ( assignment-expression ,
6951 assignment-expression ,
6952 assignment-expression )
6953 __builtin_types_compatible_p ( type-name , type-name )
6954 __builtin_complex ( assignment-expression , assignment-expression )
6955 __builtin_shuffle ( assignment-expression , assignment-expression )
6956 __builtin_shuffle ( assignment-expression ,
6957 assignment-expression ,
6958 assignment-expression, )
6960 offsetof-member-designator:
6961 identifier
6962 offsetof-member-designator . identifier
6963 offsetof-member-designator [ expression ]
6965 Objective-C:
6967 primary-expression:
6968 [ objc-receiver objc-message-args ]
6969 @selector ( objc-selector-arg )
6970 @protocol ( identifier )
6971 @encode ( type-name )
6972 objc-string-literal
6973 Classname . identifier
6976 static struct c_expr
6977 c_parser_postfix_expression (c_parser *parser)
6979 struct c_expr expr, e1;
6980 struct c_type_name *t1, *t2;
6981 location_t loc = c_parser_peek_token (parser)->location;;
6982 expr.original_code = ERROR_MARK;
6983 expr.original_type = NULL;
6984 switch (c_parser_peek_token (parser)->type)
6986 case CPP_NUMBER:
6987 expr.value = c_parser_peek_token (parser)->value;
6988 loc = c_parser_peek_token (parser)->location;
6989 c_parser_consume_token (parser);
6990 if (TREE_CODE (expr.value) == FIXED_CST
6991 && !targetm.fixed_point_supported_p ())
6993 error_at (loc, "fixed-point types not supported for this target");
6994 expr.value = error_mark_node;
6996 break;
6997 case CPP_CHAR:
6998 case CPP_CHAR16:
6999 case CPP_CHAR32:
7000 case CPP_WCHAR:
7001 expr.value = c_parser_peek_token (parser)->value;
7002 c_parser_consume_token (parser);
7003 break;
7004 case CPP_STRING:
7005 case CPP_STRING16:
7006 case CPP_STRING32:
7007 case CPP_WSTRING:
7008 case CPP_UTF8STRING:
7009 expr.value = c_parser_peek_token (parser)->value;
7010 expr.original_code = STRING_CST;
7011 c_parser_consume_token (parser);
7012 break;
7013 case CPP_OBJC_STRING:
7014 gcc_assert (c_dialect_objc ());
7015 expr.value
7016 = objc_build_string_object (c_parser_peek_token (parser)->value);
7017 c_parser_consume_token (parser);
7018 break;
7019 case CPP_NAME:
7020 switch (c_parser_peek_token (parser)->id_kind)
7022 case C_ID_ID:
7024 tree id = c_parser_peek_token (parser)->value;
7025 c_parser_consume_token (parser);
7026 expr.value = build_external_ref (loc, id,
7027 (c_parser_peek_token (parser)->type
7028 == CPP_OPEN_PAREN),
7029 &expr.original_type);
7030 break;
7032 case C_ID_CLASSNAME:
7034 /* Here we parse the Objective-C 2.0 Class.name dot
7035 syntax. */
7036 tree class_name = c_parser_peek_token (parser)->value;
7037 tree component;
7038 c_parser_consume_token (parser);
7039 gcc_assert (c_dialect_objc ());
7040 if (!c_parser_require (parser, CPP_DOT, "expected %<.%>"))
7042 expr.value = error_mark_node;
7043 break;
7045 if (c_parser_next_token_is_not (parser, CPP_NAME))
7047 c_parser_error (parser, "expected identifier");
7048 expr.value = error_mark_node;
7049 break;
7051 component = c_parser_peek_token (parser)->value;
7052 c_parser_consume_token (parser);
7053 expr.value = objc_build_class_component_ref (class_name,
7054 component);
7055 break;
7057 default:
7058 c_parser_error (parser, "expected expression");
7059 expr.value = error_mark_node;
7060 break;
7062 break;
7063 case CPP_OPEN_PAREN:
7064 /* A parenthesized expression, statement expression or compound
7065 literal. */
7066 if (c_parser_peek_2nd_token (parser)->type == CPP_OPEN_BRACE)
7068 /* A statement expression. */
7069 tree stmt;
7070 location_t brace_loc;
7071 c_parser_consume_token (parser);
7072 brace_loc = c_parser_peek_token (parser)->location;
7073 c_parser_consume_token (parser);
7074 if (!building_stmt_list_p ())
7076 error_at (loc, "braced-group within expression allowed "
7077 "only inside a function");
7078 parser->error = true;
7079 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
7080 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7081 expr.value = error_mark_node;
7082 break;
7084 stmt = c_begin_stmt_expr ();
7085 c_parser_compound_statement_nostart (parser);
7086 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7087 "expected %<)%>");
7088 pedwarn (loc, OPT_Wpedantic,
7089 "ISO C forbids braced-groups within expressions");
7090 expr.value = c_finish_stmt_expr (brace_loc, stmt);
7091 mark_exp_read (expr.value);
7093 else if (c_token_starts_typename (c_parser_peek_2nd_token (parser)))
7095 /* A compound literal. ??? Can we actually get here rather
7096 than going directly to
7097 c_parser_postfix_expression_after_paren_type from
7098 elsewhere? */
7099 location_t loc;
7100 struct c_type_name *type_name;
7101 c_parser_consume_token (parser);
7102 loc = c_parser_peek_token (parser)->location;
7103 type_name = c_parser_type_name (parser);
7104 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7105 "expected %<)%>");
7106 if (type_name == NULL)
7108 expr.value = error_mark_node;
7110 else
7111 expr = c_parser_postfix_expression_after_paren_type (parser,
7112 type_name,
7113 loc);
7115 else
7117 /* A parenthesized expression. */
7118 c_parser_consume_token (parser);
7119 expr = c_parser_expression (parser);
7120 if (TREE_CODE (expr.value) == MODIFY_EXPR)
7121 TREE_NO_WARNING (expr.value) = 1;
7122 if (expr.original_code != C_MAYBE_CONST_EXPR)
7123 expr.original_code = ERROR_MARK;
7124 /* Don't change EXPR.ORIGINAL_TYPE. */
7125 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7126 "expected %<)%>");
7128 break;
7129 case CPP_KEYWORD:
7130 switch (c_parser_peek_token (parser)->keyword)
7132 case RID_FUNCTION_NAME:
7133 pedwarn (loc, OPT_Wpedantic, "ISO C does not support "
7134 "%<__FUNCTION__%> predefined identifier");
7135 expr.value = fname_decl (loc,
7136 c_parser_peek_token (parser)->keyword,
7137 c_parser_peek_token (parser)->value);
7138 c_parser_consume_token (parser);
7139 break;
7140 case RID_PRETTY_FUNCTION_NAME:
7141 pedwarn (loc, OPT_Wpedantic, "ISO C does not support "
7142 "%<__PRETTY_FUNCTION__%> predefined identifier");
7143 expr.value = fname_decl (loc,
7144 c_parser_peek_token (parser)->keyword,
7145 c_parser_peek_token (parser)->value);
7146 c_parser_consume_token (parser);
7147 break;
7148 case RID_C99_FUNCTION_NAME:
7149 pedwarn_c90 (loc, OPT_Wpedantic, "ISO C90 does not support "
7150 "%<__func__%> predefined identifier");
7151 expr.value = fname_decl (loc,
7152 c_parser_peek_token (parser)->keyword,
7153 c_parser_peek_token (parser)->value);
7154 c_parser_consume_token (parser);
7155 break;
7156 case RID_VA_ARG:
7157 c_parser_consume_token (parser);
7158 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7160 expr.value = error_mark_node;
7161 break;
7163 e1 = c_parser_expr_no_commas (parser, NULL);
7164 mark_exp_read (e1.value);
7165 e1.value = c_fully_fold (e1.value, false, NULL);
7166 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
7168 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7169 expr.value = error_mark_node;
7170 break;
7172 loc = c_parser_peek_token (parser)->location;
7173 t1 = c_parser_type_name (parser);
7174 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7175 "expected %<)%>");
7176 if (t1 == NULL)
7178 expr.value = error_mark_node;
7180 else
7182 tree type_expr = NULL_TREE;
7183 expr.value = c_build_va_arg (loc, e1.value,
7184 groktypename (t1, &type_expr, NULL));
7185 if (type_expr)
7187 expr.value = build2 (C_MAYBE_CONST_EXPR,
7188 TREE_TYPE (expr.value), type_expr,
7189 expr.value);
7190 C_MAYBE_CONST_EXPR_NON_CONST (expr.value) = true;
7193 break;
7194 case RID_OFFSETOF:
7195 c_parser_consume_token (parser);
7196 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7198 expr.value = error_mark_node;
7199 break;
7201 t1 = c_parser_type_name (parser);
7202 if (t1 == NULL)
7203 parser->error = true;
7204 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
7205 gcc_assert (parser->error);
7206 if (parser->error)
7208 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7209 expr.value = error_mark_node;
7210 break;
7214 tree type = groktypename (t1, NULL, NULL);
7215 tree offsetof_ref;
7216 if (type == error_mark_node)
7217 offsetof_ref = error_mark_node;
7218 else
7220 offsetof_ref = build1 (INDIRECT_REF, type, null_pointer_node);
7221 SET_EXPR_LOCATION (offsetof_ref, loc);
7223 /* Parse the second argument to __builtin_offsetof. We
7224 must have one identifier, and beyond that we want to
7225 accept sub structure and sub array references. */
7226 if (c_parser_next_token_is (parser, CPP_NAME))
7228 offsetof_ref = build_component_ref
7229 (loc, offsetof_ref, c_parser_peek_token (parser)->value);
7230 c_parser_consume_token (parser);
7231 while (c_parser_next_token_is (parser, CPP_DOT)
7232 || c_parser_next_token_is (parser,
7233 CPP_OPEN_SQUARE)
7234 || c_parser_next_token_is (parser,
7235 CPP_DEREF))
7237 if (c_parser_next_token_is (parser, CPP_DEREF))
7239 loc = c_parser_peek_token (parser)->location;
7240 offsetof_ref = build_array_ref (loc,
7241 offsetof_ref,
7242 integer_zero_node);
7243 goto do_dot;
7245 else if (c_parser_next_token_is (parser, CPP_DOT))
7247 do_dot:
7248 c_parser_consume_token (parser);
7249 if (c_parser_next_token_is_not (parser,
7250 CPP_NAME))
7252 c_parser_error (parser, "expected identifier");
7253 break;
7255 offsetof_ref = build_component_ref
7256 (loc, offsetof_ref,
7257 c_parser_peek_token (parser)->value);
7258 c_parser_consume_token (parser);
7260 else
7262 struct c_expr ce;
7263 tree idx;
7264 loc = c_parser_peek_token (parser)->location;
7265 c_parser_consume_token (parser);
7266 ce = c_parser_expression (parser);
7267 ce = convert_lvalue_to_rvalue (loc, ce, false, false);
7268 idx = ce.value;
7269 idx = c_fully_fold (idx, false, NULL);
7270 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
7271 "expected %<]%>");
7272 offsetof_ref = build_array_ref (loc, offsetof_ref, idx);
7276 else
7277 c_parser_error (parser, "expected identifier");
7278 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7279 "expected %<)%>");
7280 expr.value = fold_offsetof (offsetof_ref);
7282 break;
7283 case RID_CHOOSE_EXPR:
7285 vec<c_expr_t, va_gc> *cexpr_list;
7286 c_expr_t *e1_p, *e2_p, *e3_p;
7287 tree c;
7289 c_parser_consume_token (parser);
7290 if (!c_parser_get_builtin_args (parser,
7291 "__builtin_choose_expr",
7292 &cexpr_list, true))
7294 expr.value = error_mark_node;
7295 break;
7298 if (vec_safe_length (cexpr_list) != 3)
7300 error_at (loc, "wrong number of arguments to "
7301 "%<__builtin_choose_expr%>");
7302 expr.value = error_mark_node;
7303 break;
7306 e1_p = &(*cexpr_list)[0];
7307 e2_p = &(*cexpr_list)[1];
7308 e3_p = &(*cexpr_list)[2];
7310 c = e1_p->value;
7311 mark_exp_read (e2_p->value);
7312 mark_exp_read (e3_p->value);
7313 if (TREE_CODE (c) != INTEGER_CST
7314 || !INTEGRAL_TYPE_P (TREE_TYPE (c)))
7315 error_at (loc,
7316 "first argument to %<__builtin_choose_expr%> not"
7317 " a constant");
7318 constant_expression_warning (c);
7319 expr = integer_zerop (c) ? *e3_p : *e2_p;
7320 break;
7322 case RID_TYPES_COMPATIBLE_P:
7323 c_parser_consume_token (parser);
7324 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7326 expr.value = error_mark_node;
7327 break;
7329 t1 = c_parser_type_name (parser);
7330 if (t1 == NULL)
7332 expr.value = error_mark_node;
7333 break;
7335 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
7337 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7338 expr.value = error_mark_node;
7339 break;
7341 t2 = c_parser_type_name (parser);
7342 if (t2 == NULL)
7344 expr.value = error_mark_node;
7345 break;
7347 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7348 "expected %<)%>");
7350 tree e1, e2;
7351 e1 = groktypename (t1, NULL, NULL);
7352 e2 = groktypename (t2, NULL, NULL);
7353 if (e1 == error_mark_node || e2 == error_mark_node)
7355 expr.value = error_mark_node;
7356 break;
7359 e1 = TYPE_MAIN_VARIANT (e1);
7360 e2 = TYPE_MAIN_VARIANT (e2);
7362 expr.value
7363 = comptypes (e1, e2) ? integer_one_node : integer_zero_node;
7365 break;
7366 case RID_BUILTIN_COMPLEX:
7368 vec<c_expr_t, va_gc> *cexpr_list;
7369 c_expr_t *e1_p, *e2_p;
7371 c_parser_consume_token (parser);
7372 if (!c_parser_get_builtin_args (parser,
7373 "__builtin_complex",
7374 &cexpr_list, false))
7376 expr.value = error_mark_node;
7377 break;
7380 if (vec_safe_length (cexpr_list) != 2)
7382 error_at (loc, "wrong number of arguments to "
7383 "%<__builtin_complex%>");
7384 expr.value = error_mark_node;
7385 break;
7388 e1_p = &(*cexpr_list)[0];
7389 e2_p = &(*cexpr_list)[1];
7391 *e1_p = convert_lvalue_to_rvalue (loc, *e1_p, true, true);
7392 if (TREE_CODE (e1_p->value) == EXCESS_PRECISION_EXPR)
7393 e1_p->value = convert (TREE_TYPE (e1_p->value),
7394 TREE_OPERAND (e1_p->value, 0));
7395 *e2_p = convert_lvalue_to_rvalue (loc, *e2_p, true, true);
7396 if (TREE_CODE (e2_p->value) == EXCESS_PRECISION_EXPR)
7397 e2_p->value = convert (TREE_TYPE (e2_p->value),
7398 TREE_OPERAND (e2_p->value, 0));
7399 if (!SCALAR_FLOAT_TYPE_P (TREE_TYPE (e1_p->value))
7400 || DECIMAL_FLOAT_TYPE_P (TREE_TYPE (e1_p->value))
7401 || !SCALAR_FLOAT_TYPE_P (TREE_TYPE (e2_p->value))
7402 || DECIMAL_FLOAT_TYPE_P (TREE_TYPE (e2_p->value)))
7404 error_at (loc, "%<__builtin_complex%> operand "
7405 "not of real binary floating-point type");
7406 expr.value = error_mark_node;
7407 break;
7409 if (TYPE_MAIN_VARIANT (TREE_TYPE (e1_p->value))
7410 != TYPE_MAIN_VARIANT (TREE_TYPE (e2_p->value)))
7412 error_at (loc,
7413 "%<__builtin_complex%> operands of different types");
7414 expr.value = error_mark_node;
7415 break;
7417 pedwarn_c90 (loc, OPT_Wpedantic,
7418 "ISO C90 does not support complex types");
7419 expr.value = build2 (COMPLEX_EXPR,
7420 build_complex_type
7421 (TYPE_MAIN_VARIANT
7422 (TREE_TYPE (e1_p->value))),
7423 e1_p->value, e2_p->value);
7424 break;
7426 case RID_BUILTIN_SHUFFLE:
7428 vec<c_expr_t, va_gc> *cexpr_list;
7429 unsigned int i;
7430 c_expr_t *p;
7432 c_parser_consume_token (parser);
7433 if (!c_parser_get_builtin_args (parser,
7434 "__builtin_shuffle",
7435 &cexpr_list, false))
7437 expr.value = error_mark_node;
7438 break;
7441 FOR_EACH_VEC_SAFE_ELT (cexpr_list, i, p)
7442 *p = convert_lvalue_to_rvalue (loc, *p, true, true);
7444 if (vec_safe_length (cexpr_list) == 2)
7445 expr.value =
7446 c_build_vec_perm_expr
7447 (loc, (*cexpr_list)[0].value,
7448 NULL_TREE, (*cexpr_list)[1].value);
7450 else if (vec_safe_length (cexpr_list) == 3)
7451 expr.value =
7452 c_build_vec_perm_expr
7453 (loc, (*cexpr_list)[0].value,
7454 (*cexpr_list)[1].value,
7455 (*cexpr_list)[2].value);
7456 else
7458 error_at (loc, "wrong number of arguments to "
7459 "%<__builtin_shuffle%>");
7460 expr.value = error_mark_node;
7462 break;
7464 case RID_AT_SELECTOR:
7465 gcc_assert (c_dialect_objc ());
7466 c_parser_consume_token (parser);
7467 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7469 expr.value = error_mark_node;
7470 break;
7473 tree sel = c_parser_objc_selector_arg (parser);
7474 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7475 "expected %<)%>");
7476 expr.value = objc_build_selector_expr (loc, sel);
7478 break;
7479 case RID_AT_PROTOCOL:
7480 gcc_assert (c_dialect_objc ());
7481 c_parser_consume_token (parser);
7482 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7484 expr.value = error_mark_node;
7485 break;
7487 if (c_parser_next_token_is_not (parser, CPP_NAME))
7489 c_parser_error (parser, "expected identifier");
7490 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7491 expr.value = error_mark_node;
7492 break;
7495 tree id = c_parser_peek_token (parser)->value;
7496 c_parser_consume_token (parser);
7497 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7498 "expected %<)%>");
7499 expr.value = objc_build_protocol_expr (id);
7501 break;
7502 case RID_AT_ENCODE:
7503 /* Extension to support C-structures in the archiver. */
7504 gcc_assert (c_dialect_objc ());
7505 c_parser_consume_token (parser);
7506 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7508 expr.value = error_mark_node;
7509 break;
7511 t1 = c_parser_type_name (parser);
7512 if (t1 == NULL)
7514 expr.value = error_mark_node;
7515 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7516 break;
7518 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7519 "expected %<)%>");
7521 tree type = groktypename (t1, NULL, NULL);
7522 expr.value = objc_build_encode_expr (type);
7524 break;
7525 case RID_GENERIC:
7526 expr = c_parser_generic_selection (parser);
7527 break;
7528 case RID_CILK_SPAWN:
7529 c_parser_consume_token (parser);
7530 if (!flag_cilkplus)
7532 error_at (loc, "-fcilkplus must be enabled to use "
7533 "%<_Cilk_spawn%>");
7534 expr = c_parser_postfix_expression (parser);
7535 expr.value = error_mark_node;
7537 else if (c_parser_peek_token (parser)->keyword == RID_CILK_SPAWN)
7539 error_at (loc, "consecutive %<_Cilk_spawn%> keywords "
7540 "are not permitted");
7541 /* Now flush out all the _Cilk_spawns. */
7542 while (c_parser_peek_token (parser)->keyword == RID_CILK_SPAWN)
7543 c_parser_consume_token (parser);
7544 expr = c_parser_postfix_expression (parser);
7546 else
7548 expr = c_parser_postfix_expression (parser);
7549 expr.value = build_cilk_spawn (loc, expr.value);
7551 break;
7552 default:
7553 c_parser_error (parser, "expected expression");
7554 expr.value = error_mark_node;
7555 break;
7557 break;
7558 case CPP_OPEN_SQUARE:
7559 if (c_dialect_objc ())
7561 tree receiver, args;
7562 c_parser_consume_token (parser);
7563 receiver = c_parser_objc_receiver (parser);
7564 args = c_parser_objc_message_args (parser);
7565 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
7566 "expected %<]%>");
7567 expr.value = objc_build_message_expr (receiver, args);
7568 break;
7570 /* Else fall through to report error. */
7571 default:
7572 c_parser_error (parser, "expected expression");
7573 expr.value = error_mark_node;
7574 break;
7576 return c_parser_postfix_expression_after_primary (parser, loc, expr);
7579 /* Parse a postfix expression after a parenthesized type name: the
7580 brace-enclosed initializer of a compound literal, possibly followed
7581 by some postfix operators. This is separate because it is not
7582 possible to tell until after the type name whether a cast
7583 expression has a cast or a compound literal, or whether the operand
7584 of sizeof is a parenthesized type name or starts with a compound
7585 literal. TYPE_LOC is the location where TYPE_NAME starts--the
7586 location of the first token after the parentheses around the type
7587 name. */
7589 static struct c_expr
7590 c_parser_postfix_expression_after_paren_type (c_parser *parser,
7591 struct c_type_name *type_name,
7592 location_t type_loc)
7594 tree type;
7595 struct c_expr init;
7596 bool non_const;
7597 struct c_expr expr;
7598 location_t start_loc;
7599 tree type_expr = NULL_TREE;
7600 bool type_expr_const = true;
7601 check_compound_literal_type (type_loc, type_name);
7602 start_init (NULL_TREE, NULL, 0);
7603 type = groktypename (type_name, &type_expr, &type_expr_const);
7604 start_loc = c_parser_peek_token (parser)->location;
7605 if (type != error_mark_node && C_TYPE_VARIABLE_SIZE (type))
7607 error_at (type_loc, "compound literal has variable size");
7608 type = error_mark_node;
7610 init = c_parser_braced_init (parser, type, false);
7611 finish_init ();
7612 maybe_warn_string_init (type_loc, type, init);
7614 if (type != error_mark_node
7615 && !ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (type))
7616 && current_function_decl)
7618 error ("compound literal qualified by address-space qualifier");
7619 type = error_mark_node;
7622 pedwarn_c90 (start_loc, OPT_Wpedantic, "ISO C90 forbids compound literals");
7623 non_const = ((init.value && TREE_CODE (init.value) == CONSTRUCTOR)
7624 ? CONSTRUCTOR_NON_CONST (init.value)
7625 : init.original_code == C_MAYBE_CONST_EXPR);
7626 non_const |= !type_expr_const;
7627 expr.value = build_compound_literal (start_loc, type, init.value, non_const);
7628 expr.original_code = ERROR_MARK;
7629 expr.original_type = NULL;
7630 if (type_expr)
7632 if (TREE_CODE (expr.value) == C_MAYBE_CONST_EXPR)
7634 gcc_assert (C_MAYBE_CONST_EXPR_PRE (expr.value) == NULL_TREE);
7635 C_MAYBE_CONST_EXPR_PRE (expr.value) = type_expr;
7637 else
7639 gcc_assert (!non_const);
7640 expr.value = build2 (C_MAYBE_CONST_EXPR, type,
7641 type_expr, expr.value);
7644 return c_parser_postfix_expression_after_primary (parser, start_loc, expr);
7647 /* Callback function for sizeof_pointer_memaccess_warning to compare
7648 types. */
7650 static bool
7651 sizeof_ptr_memacc_comptypes (tree type1, tree type2)
7653 return comptypes (type1, type2) == 1;
7656 /* Parse a postfix expression after the initial primary or compound
7657 literal; that is, parse a series of postfix operators.
7659 EXPR_LOC is the location of the primary expression. */
7661 static struct c_expr
7662 c_parser_postfix_expression_after_primary (c_parser *parser,
7663 location_t expr_loc,
7664 struct c_expr expr)
7666 struct c_expr orig_expr;
7667 tree ident, idx;
7668 location_t sizeof_arg_loc[3];
7669 tree sizeof_arg[3];
7670 unsigned int literal_zero_mask;
7671 unsigned int i;
7672 vec<tree, va_gc> *exprlist;
7673 vec<tree, va_gc> *origtypes = NULL;
7674 vec<location_t> arg_loc = vNULL;
7676 while (true)
7678 location_t op_loc = c_parser_peek_token (parser)->location;
7679 switch (c_parser_peek_token (parser)->type)
7681 case CPP_OPEN_SQUARE:
7682 /* Array reference. */
7683 c_parser_consume_token (parser);
7684 if (flag_cilkplus
7685 && c_parser_peek_token (parser)->type == CPP_COLON)
7686 /* If we are here, then we have something like this:
7687 Array [ : ]
7689 expr.value = c_parser_array_notation (expr_loc, parser, NULL_TREE,
7690 expr.value);
7691 else
7693 idx = c_parser_expression (parser).value;
7694 /* Here we have 3 options:
7695 1. Array [EXPR] -- Normal Array call.
7696 2. Array [EXPR : EXPR] -- Array notation without stride.
7697 3. Array [EXPR : EXPR : EXPR] -- Array notation with stride.
7699 For 1, we just handle it just like a normal array expression.
7700 For 2 and 3 we handle it like we handle array notations. The
7701 idx value we have above becomes the initial/start index.
7703 if (flag_cilkplus
7704 && c_parser_peek_token (parser)->type == CPP_COLON)
7705 expr.value = c_parser_array_notation (expr_loc, parser, idx,
7706 expr.value);
7707 else
7709 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
7710 "expected %<]%>");
7711 expr.value = build_array_ref (op_loc, expr.value, idx);
7714 expr.original_code = ERROR_MARK;
7715 expr.original_type = NULL;
7716 break;
7717 case CPP_OPEN_PAREN:
7718 /* Function call. */
7719 c_parser_consume_token (parser);
7720 for (i = 0; i < 3; i++)
7722 sizeof_arg[i] = NULL_TREE;
7723 sizeof_arg_loc[i] = UNKNOWN_LOCATION;
7725 literal_zero_mask = 0;
7726 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
7727 exprlist = NULL;
7728 else
7729 exprlist = c_parser_expr_list (parser, true, false, &origtypes,
7730 sizeof_arg_loc, sizeof_arg,
7731 &arg_loc, &literal_zero_mask);
7732 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7733 "expected %<)%>");
7734 orig_expr = expr;
7735 mark_exp_read (expr.value);
7736 if (warn_sizeof_pointer_memaccess)
7737 sizeof_pointer_memaccess_warning (sizeof_arg_loc,
7738 expr.value, exprlist,
7739 sizeof_arg,
7740 sizeof_ptr_memacc_comptypes);
7741 if (warn_memset_transposed_args
7742 && TREE_CODE (expr.value) == FUNCTION_DECL
7743 && DECL_BUILT_IN_CLASS (expr.value) == BUILT_IN_NORMAL
7744 && DECL_FUNCTION_CODE (expr.value) == BUILT_IN_MEMSET
7745 && vec_safe_length (exprlist) == 3
7746 && integer_zerop ((*exprlist)[2])
7747 && (literal_zero_mask & (1 << 2)) != 0
7748 && (!integer_zerop ((*exprlist)[1])
7749 || (literal_zero_mask & (1 << 1)) == 0))
7750 warning_at (expr_loc, OPT_Wmemset_transposed_args,
7751 "%<memset%> used with constant zero length parameter; "
7752 "this could be due to transposed parameters");
7754 expr.value
7755 = c_build_function_call_vec (expr_loc, arg_loc, expr.value,
7756 exprlist, origtypes);
7757 expr.original_code = ERROR_MARK;
7758 if (TREE_CODE (expr.value) == INTEGER_CST
7759 && TREE_CODE (orig_expr.value) == FUNCTION_DECL
7760 && DECL_BUILT_IN_CLASS (orig_expr.value) == BUILT_IN_NORMAL
7761 && DECL_FUNCTION_CODE (orig_expr.value) == BUILT_IN_CONSTANT_P)
7762 expr.original_code = C_MAYBE_CONST_EXPR;
7763 expr.original_type = NULL;
7764 if (exprlist)
7766 release_tree_vector (exprlist);
7767 release_tree_vector (origtypes);
7769 arg_loc.release ();
7770 break;
7771 case CPP_DOT:
7772 /* Structure element reference. */
7773 c_parser_consume_token (parser);
7774 expr = default_function_array_conversion (expr_loc, expr);
7775 if (c_parser_next_token_is (parser, CPP_NAME))
7776 ident = c_parser_peek_token (parser)->value;
7777 else
7779 c_parser_error (parser, "expected identifier");
7780 expr.value = error_mark_node;
7781 expr.original_code = ERROR_MARK;
7782 expr.original_type = NULL;
7783 return expr;
7785 c_parser_consume_token (parser);
7786 expr.value = build_component_ref (op_loc, expr.value, ident);
7787 expr.original_code = ERROR_MARK;
7788 if (TREE_CODE (expr.value) != COMPONENT_REF)
7789 expr.original_type = NULL;
7790 else
7792 /* Remember the original type of a bitfield. */
7793 tree field = TREE_OPERAND (expr.value, 1);
7794 if (TREE_CODE (field) != FIELD_DECL)
7795 expr.original_type = NULL;
7796 else
7797 expr.original_type = DECL_BIT_FIELD_TYPE (field);
7799 break;
7800 case CPP_DEREF:
7801 /* Structure element reference. */
7802 c_parser_consume_token (parser);
7803 expr = convert_lvalue_to_rvalue (expr_loc, expr, true, false);
7804 if (c_parser_next_token_is (parser, CPP_NAME))
7805 ident = c_parser_peek_token (parser)->value;
7806 else
7808 c_parser_error (parser, "expected identifier");
7809 expr.value = error_mark_node;
7810 expr.original_code = ERROR_MARK;
7811 expr.original_type = NULL;
7812 return expr;
7814 c_parser_consume_token (parser);
7815 expr.value = build_component_ref (op_loc,
7816 build_indirect_ref (op_loc,
7817 expr.value,
7818 RO_ARROW),
7819 ident);
7820 expr.original_code = ERROR_MARK;
7821 if (TREE_CODE (expr.value) != COMPONENT_REF)
7822 expr.original_type = NULL;
7823 else
7825 /* Remember the original type of a bitfield. */
7826 tree field = TREE_OPERAND (expr.value, 1);
7827 if (TREE_CODE (field) != FIELD_DECL)
7828 expr.original_type = NULL;
7829 else
7830 expr.original_type = DECL_BIT_FIELD_TYPE (field);
7832 break;
7833 case CPP_PLUS_PLUS:
7834 /* Postincrement. */
7835 c_parser_consume_token (parser);
7836 /* If the expressions have array notations, we expand them. */
7837 if (flag_cilkplus
7838 && TREE_CODE (expr.value) == ARRAY_NOTATION_REF)
7839 expr = fix_array_notation_expr (expr_loc, POSTINCREMENT_EXPR, expr);
7840 else
7842 expr = default_function_array_read_conversion (expr_loc, expr);
7843 expr.value = build_unary_op (op_loc,
7844 POSTINCREMENT_EXPR, expr.value, 0);
7846 expr.original_code = ERROR_MARK;
7847 expr.original_type = NULL;
7848 break;
7849 case CPP_MINUS_MINUS:
7850 /* Postdecrement. */
7851 c_parser_consume_token (parser);
7852 /* If the expressions have array notations, we expand them. */
7853 if (flag_cilkplus
7854 && TREE_CODE (expr.value) == ARRAY_NOTATION_REF)
7855 expr = fix_array_notation_expr (expr_loc, POSTDECREMENT_EXPR, expr);
7856 else
7858 expr = default_function_array_read_conversion (expr_loc, expr);
7859 expr.value = build_unary_op (op_loc,
7860 POSTDECREMENT_EXPR, expr.value, 0);
7862 expr.original_code = ERROR_MARK;
7863 expr.original_type = NULL;
7864 break;
7865 default:
7866 return expr;
7871 /* Parse an expression (C90 6.3.17, C99 6.5.17).
7873 expression:
7874 assignment-expression
7875 expression , assignment-expression
7878 static struct c_expr
7879 c_parser_expression (c_parser *parser)
7881 location_t tloc = c_parser_peek_token (parser)->location;
7882 struct c_expr expr;
7883 expr = c_parser_expr_no_commas (parser, NULL);
7884 if (c_parser_next_token_is (parser, CPP_COMMA))
7885 expr = convert_lvalue_to_rvalue (tloc, expr, true, false);
7886 while (c_parser_next_token_is (parser, CPP_COMMA))
7888 struct c_expr next;
7889 tree lhsval;
7890 location_t loc = c_parser_peek_token (parser)->location;
7891 location_t expr_loc;
7892 c_parser_consume_token (parser);
7893 expr_loc = c_parser_peek_token (parser)->location;
7894 lhsval = expr.value;
7895 while (TREE_CODE (lhsval) == COMPOUND_EXPR)
7896 lhsval = TREE_OPERAND (lhsval, 1);
7897 if (DECL_P (lhsval) || handled_component_p (lhsval))
7898 mark_exp_read (lhsval);
7899 next = c_parser_expr_no_commas (parser, NULL);
7900 next = convert_lvalue_to_rvalue (expr_loc, next, true, false);
7901 expr.value = build_compound_expr (loc, expr.value, next.value);
7902 expr.original_code = COMPOUND_EXPR;
7903 expr.original_type = next.original_type;
7905 return expr;
7908 /* Parse an expression and convert functions or arrays to pointers and
7909 lvalues to rvalues. */
7911 static struct c_expr
7912 c_parser_expression_conv (c_parser *parser)
7914 struct c_expr expr;
7915 location_t loc = c_parser_peek_token (parser)->location;
7916 expr = c_parser_expression (parser);
7917 expr = convert_lvalue_to_rvalue (loc, expr, true, false);
7918 return expr;
7921 /* Helper function of c_parser_expr_list. Check if IDXth (0 based)
7922 argument is a literal zero alone and if so, set it in literal_zero_mask. */
7924 static inline void
7925 c_parser_check_literal_zero (c_parser *parser, unsigned *literal_zero_mask,
7926 unsigned int idx)
7928 if (idx >= HOST_BITS_PER_INT)
7929 return;
7931 c_token *tok = c_parser_peek_token (parser);
7932 switch (tok->type)
7934 case CPP_NUMBER:
7935 case CPP_CHAR:
7936 case CPP_WCHAR:
7937 case CPP_CHAR16:
7938 case CPP_CHAR32:
7939 /* If a parameter is literal zero alone, remember it
7940 for -Wmemset-transposed-args warning. */
7941 if (integer_zerop (tok->value)
7942 && !TREE_OVERFLOW (tok->value)
7943 && (c_parser_peek_2nd_token (parser)->type == CPP_COMMA
7944 || c_parser_peek_2nd_token (parser)->type == CPP_CLOSE_PAREN))
7945 *literal_zero_mask |= 1U << idx;
7946 default:
7947 break;
7951 /* Parse a non-empty list of expressions. If CONVERT_P, convert
7952 functions and arrays to pointers and lvalues to rvalues. If
7953 FOLD_P, fold the expressions. If LOCATIONS is non-NULL, save the
7954 locations of function arguments into this vector.
7956 nonempty-expr-list:
7957 assignment-expression
7958 nonempty-expr-list , assignment-expression
7961 static vec<tree, va_gc> *
7962 c_parser_expr_list (c_parser *parser, bool convert_p, bool fold_p,
7963 vec<tree, va_gc> **p_orig_types,
7964 location_t *sizeof_arg_loc, tree *sizeof_arg,
7965 vec<location_t> *locations,
7966 unsigned int *literal_zero_mask)
7968 vec<tree, va_gc> *ret;
7969 vec<tree, va_gc> *orig_types;
7970 struct c_expr expr;
7971 location_t loc = c_parser_peek_token (parser)->location;
7972 location_t cur_sizeof_arg_loc = UNKNOWN_LOCATION;
7973 unsigned int idx = 0;
7975 ret = make_tree_vector ();
7976 if (p_orig_types == NULL)
7977 orig_types = NULL;
7978 else
7979 orig_types = make_tree_vector ();
7981 if (sizeof_arg != NULL
7982 && c_parser_next_token_is_keyword (parser, RID_SIZEOF))
7983 cur_sizeof_arg_loc = c_parser_peek_2nd_token (parser)->location;
7984 if (literal_zero_mask)
7985 c_parser_check_literal_zero (parser, literal_zero_mask, 0);
7986 expr = c_parser_expr_no_commas (parser, NULL);
7987 if (convert_p)
7988 expr = convert_lvalue_to_rvalue (loc, expr, true, true);
7989 if (fold_p)
7990 expr.value = c_fully_fold (expr.value, false, NULL);
7991 ret->quick_push (expr.value);
7992 if (orig_types)
7993 orig_types->quick_push (expr.original_type);
7994 if (locations)
7995 locations->safe_push (loc);
7996 if (sizeof_arg != NULL
7997 && cur_sizeof_arg_loc != UNKNOWN_LOCATION
7998 && expr.original_code == SIZEOF_EXPR)
8000 sizeof_arg[0] = c_last_sizeof_arg;
8001 sizeof_arg_loc[0] = cur_sizeof_arg_loc;
8003 while (c_parser_next_token_is (parser, CPP_COMMA))
8005 c_parser_consume_token (parser);
8006 loc = c_parser_peek_token (parser)->location;
8007 if (sizeof_arg != NULL
8008 && c_parser_next_token_is_keyword (parser, RID_SIZEOF))
8009 cur_sizeof_arg_loc = c_parser_peek_2nd_token (parser)->location;
8010 else
8011 cur_sizeof_arg_loc = UNKNOWN_LOCATION;
8012 if (literal_zero_mask)
8013 c_parser_check_literal_zero (parser, literal_zero_mask, idx + 1);
8014 expr = c_parser_expr_no_commas (parser, NULL);
8015 if (convert_p)
8016 expr = convert_lvalue_to_rvalue (loc, expr, true, true);
8017 if (fold_p)
8018 expr.value = c_fully_fold (expr.value, false, NULL);
8019 vec_safe_push (ret, expr.value);
8020 if (orig_types)
8021 vec_safe_push (orig_types, expr.original_type);
8022 if (locations)
8023 locations->safe_push (loc);
8024 if (++idx < 3
8025 && sizeof_arg != NULL
8026 && cur_sizeof_arg_loc != UNKNOWN_LOCATION
8027 && expr.original_code == SIZEOF_EXPR)
8029 sizeof_arg[idx] = c_last_sizeof_arg;
8030 sizeof_arg_loc[idx] = cur_sizeof_arg_loc;
8033 if (orig_types)
8034 *p_orig_types = orig_types;
8035 return ret;
8038 /* Parse Objective-C-specific constructs. */
8040 /* Parse an objc-class-definition.
8042 objc-class-definition:
8043 @interface identifier objc-superclass[opt] objc-protocol-refs[opt]
8044 objc-class-instance-variables[opt] objc-methodprotolist @end
8045 @implementation identifier objc-superclass[opt]
8046 objc-class-instance-variables[opt]
8047 @interface identifier ( identifier ) objc-protocol-refs[opt]
8048 objc-methodprotolist @end
8049 @interface identifier ( ) objc-protocol-refs[opt]
8050 objc-methodprotolist @end
8051 @implementation identifier ( identifier )
8053 objc-superclass:
8054 : identifier
8056 "@interface identifier (" must start "@interface identifier (
8057 identifier ) ...": objc-methodprotolist in the first production may
8058 not start with a parenthesized identifier as a declarator of a data
8059 definition with no declaration specifiers if the objc-superclass,
8060 objc-protocol-refs and objc-class-instance-variables are omitted. */
8062 static void
8063 c_parser_objc_class_definition (c_parser *parser, tree attributes)
8065 bool iface_p;
8066 tree id1;
8067 tree superclass;
8068 if (c_parser_next_token_is_keyword (parser, RID_AT_INTERFACE))
8069 iface_p = true;
8070 else if (c_parser_next_token_is_keyword (parser, RID_AT_IMPLEMENTATION))
8071 iface_p = false;
8072 else
8073 gcc_unreachable ();
8075 c_parser_consume_token (parser);
8076 if (c_parser_next_token_is_not (parser, CPP_NAME))
8078 c_parser_error (parser, "expected identifier");
8079 return;
8081 id1 = c_parser_peek_token (parser)->value;
8082 c_parser_consume_token (parser);
8083 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
8085 /* We have a category or class extension. */
8086 tree id2;
8087 tree proto = NULL_TREE;
8088 c_parser_consume_token (parser);
8089 if (c_parser_next_token_is_not (parser, CPP_NAME))
8091 if (iface_p && c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
8093 /* We have a class extension. */
8094 id2 = NULL_TREE;
8096 else
8098 c_parser_error (parser, "expected identifier or %<)%>");
8099 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
8100 return;
8103 else
8105 id2 = c_parser_peek_token (parser)->value;
8106 c_parser_consume_token (parser);
8108 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
8109 if (!iface_p)
8111 objc_start_category_implementation (id1, id2);
8112 return;
8114 if (c_parser_next_token_is (parser, CPP_LESS))
8115 proto = c_parser_objc_protocol_refs (parser);
8116 objc_start_category_interface (id1, id2, proto, attributes);
8117 c_parser_objc_methodprotolist (parser);
8118 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
8119 objc_finish_interface ();
8120 return;
8122 if (c_parser_next_token_is (parser, CPP_COLON))
8124 c_parser_consume_token (parser);
8125 if (c_parser_next_token_is_not (parser, CPP_NAME))
8127 c_parser_error (parser, "expected identifier");
8128 return;
8130 superclass = c_parser_peek_token (parser)->value;
8131 c_parser_consume_token (parser);
8133 else
8134 superclass = NULL_TREE;
8135 if (iface_p)
8137 tree proto = NULL_TREE;
8138 if (c_parser_next_token_is (parser, CPP_LESS))
8139 proto = c_parser_objc_protocol_refs (parser);
8140 objc_start_class_interface (id1, superclass, proto, attributes);
8142 else
8143 objc_start_class_implementation (id1, superclass);
8144 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
8145 c_parser_objc_class_instance_variables (parser);
8146 if (iface_p)
8148 objc_continue_interface ();
8149 c_parser_objc_methodprotolist (parser);
8150 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
8151 objc_finish_interface ();
8153 else
8155 objc_continue_implementation ();
8156 return;
8160 /* Parse objc-class-instance-variables.
8162 objc-class-instance-variables:
8163 { objc-instance-variable-decl-list[opt] }
8165 objc-instance-variable-decl-list:
8166 objc-visibility-spec
8167 objc-instance-variable-decl ;
8169 objc-instance-variable-decl-list objc-visibility-spec
8170 objc-instance-variable-decl-list objc-instance-variable-decl ;
8171 objc-instance-variable-decl-list ;
8173 objc-visibility-spec:
8174 @private
8175 @protected
8176 @public
8178 objc-instance-variable-decl:
8179 struct-declaration
8182 static void
8183 c_parser_objc_class_instance_variables (c_parser *parser)
8185 gcc_assert (c_parser_next_token_is (parser, CPP_OPEN_BRACE));
8186 c_parser_consume_token (parser);
8187 while (c_parser_next_token_is_not (parser, CPP_EOF))
8189 tree decls;
8190 /* Parse any stray semicolon. */
8191 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
8193 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
8194 "extra semicolon");
8195 c_parser_consume_token (parser);
8196 continue;
8198 /* Stop if at the end of the instance variables. */
8199 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
8201 c_parser_consume_token (parser);
8202 break;
8204 /* Parse any objc-visibility-spec. */
8205 if (c_parser_next_token_is_keyword (parser, RID_AT_PRIVATE))
8207 c_parser_consume_token (parser);
8208 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
8209 continue;
8211 else if (c_parser_next_token_is_keyword (parser, RID_AT_PROTECTED))
8213 c_parser_consume_token (parser);
8214 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
8215 continue;
8217 else if (c_parser_next_token_is_keyword (parser, RID_AT_PUBLIC))
8219 c_parser_consume_token (parser);
8220 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
8221 continue;
8223 else if (c_parser_next_token_is_keyword (parser, RID_AT_PACKAGE))
8225 c_parser_consume_token (parser);
8226 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
8227 continue;
8229 else if (c_parser_next_token_is (parser, CPP_PRAGMA))
8231 c_parser_pragma (parser, pragma_external);
8232 continue;
8235 /* Parse some comma-separated declarations. */
8236 decls = c_parser_struct_declaration (parser);
8237 if (decls == NULL)
8239 /* There is a syntax error. We want to skip the offending
8240 tokens up to the next ';' (included) or '}'
8241 (excluded). */
8243 /* First, skip manually a ')' or ']'. This is because they
8244 reduce the nesting level, so c_parser_skip_until_found()
8245 wouldn't be able to skip past them. */
8246 c_token *token = c_parser_peek_token (parser);
8247 if (token->type == CPP_CLOSE_PAREN || token->type == CPP_CLOSE_SQUARE)
8248 c_parser_consume_token (parser);
8250 /* Then, do the standard skipping. */
8251 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
8253 /* We hopefully recovered. Start normal parsing again. */
8254 parser->error = false;
8255 continue;
8257 else
8259 /* Comma-separated instance variables are chained together
8260 in reverse order; add them one by one. */
8261 tree ivar = nreverse (decls);
8262 for (; ivar; ivar = DECL_CHAIN (ivar))
8263 objc_add_instance_variable (copy_node (ivar));
8265 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
8269 /* Parse an objc-class-declaration.
8271 objc-class-declaration:
8272 @class identifier-list ;
8275 static void
8276 c_parser_objc_class_declaration (c_parser *parser)
8278 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_CLASS));
8279 c_parser_consume_token (parser);
8280 /* Any identifiers, including those declared as type names, are OK
8281 here. */
8282 while (true)
8284 tree id;
8285 if (c_parser_next_token_is_not (parser, CPP_NAME))
8287 c_parser_error (parser, "expected identifier");
8288 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
8289 parser->error = false;
8290 return;
8292 id = c_parser_peek_token (parser)->value;
8293 objc_declare_class (id);
8294 c_parser_consume_token (parser);
8295 if (c_parser_next_token_is (parser, CPP_COMMA))
8296 c_parser_consume_token (parser);
8297 else
8298 break;
8300 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
8303 /* Parse an objc-alias-declaration.
8305 objc-alias-declaration:
8306 @compatibility_alias identifier identifier ;
8309 static void
8310 c_parser_objc_alias_declaration (c_parser *parser)
8312 tree id1, id2;
8313 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_ALIAS));
8314 c_parser_consume_token (parser);
8315 if (c_parser_next_token_is_not (parser, CPP_NAME))
8317 c_parser_error (parser, "expected identifier");
8318 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
8319 return;
8321 id1 = c_parser_peek_token (parser)->value;
8322 c_parser_consume_token (parser);
8323 if (c_parser_next_token_is_not (parser, CPP_NAME))
8325 c_parser_error (parser, "expected identifier");
8326 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
8327 return;
8329 id2 = c_parser_peek_token (parser)->value;
8330 c_parser_consume_token (parser);
8331 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
8332 objc_declare_alias (id1, id2);
8335 /* Parse an objc-protocol-definition.
8337 objc-protocol-definition:
8338 @protocol identifier objc-protocol-refs[opt] objc-methodprotolist @end
8339 @protocol identifier-list ;
8341 "@protocol identifier ;" should be resolved as "@protocol
8342 identifier-list ;": objc-methodprotolist may not start with a
8343 semicolon in the first alternative if objc-protocol-refs are
8344 omitted. */
8346 static void
8347 c_parser_objc_protocol_definition (c_parser *parser, tree attributes)
8349 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_PROTOCOL));
8351 c_parser_consume_token (parser);
8352 if (c_parser_next_token_is_not (parser, CPP_NAME))
8354 c_parser_error (parser, "expected identifier");
8355 return;
8357 if (c_parser_peek_2nd_token (parser)->type == CPP_COMMA
8358 || c_parser_peek_2nd_token (parser)->type == CPP_SEMICOLON)
8360 /* Any identifiers, including those declared as type names, are
8361 OK here. */
8362 while (true)
8364 tree id;
8365 if (c_parser_next_token_is_not (parser, CPP_NAME))
8367 c_parser_error (parser, "expected identifier");
8368 break;
8370 id = c_parser_peek_token (parser)->value;
8371 objc_declare_protocol (id, attributes);
8372 c_parser_consume_token (parser);
8373 if (c_parser_next_token_is (parser, CPP_COMMA))
8374 c_parser_consume_token (parser);
8375 else
8376 break;
8378 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
8380 else
8382 tree id = c_parser_peek_token (parser)->value;
8383 tree proto = NULL_TREE;
8384 c_parser_consume_token (parser);
8385 if (c_parser_next_token_is (parser, CPP_LESS))
8386 proto = c_parser_objc_protocol_refs (parser);
8387 parser->objc_pq_context = true;
8388 objc_start_protocol (id, proto, attributes);
8389 c_parser_objc_methodprotolist (parser);
8390 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
8391 parser->objc_pq_context = false;
8392 objc_finish_interface ();
8396 /* Parse an objc-method-type.
8398 objc-method-type:
8402 Return true if it is a class method (+) and false if it is
8403 an instance method (-).
8405 static inline bool
8406 c_parser_objc_method_type (c_parser *parser)
8408 switch (c_parser_peek_token (parser)->type)
8410 case CPP_PLUS:
8411 c_parser_consume_token (parser);
8412 return true;
8413 case CPP_MINUS:
8414 c_parser_consume_token (parser);
8415 return false;
8416 default:
8417 gcc_unreachable ();
8421 /* Parse an objc-method-definition.
8423 objc-method-definition:
8424 objc-method-type objc-method-decl ;[opt] compound-statement
8427 static void
8428 c_parser_objc_method_definition (c_parser *parser)
8430 bool is_class_method = c_parser_objc_method_type (parser);
8431 tree decl, attributes = NULL_TREE, expr = NULL_TREE;
8432 parser->objc_pq_context = true;
8433 decl = c_parser_objc_method_decl (parser, is_class_method, &attributes,
8434 &expr);
8435 if (decl == error_mark_node)
8436 return; /* Bail here. */
8438 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
8440 c_parser_consume_token (parser);
8441 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
8442 "extra semicolon in method definition specified");
8445 if (!c_parser_next_token_is (parser, CPP_OPEN_BRACE))
8447 c_parser_error (parser, "expected %<{%>");
8448 return;
8451 parser->objc_pq_context = false;
8452 if (objc_start_method_definition (is_class_method, decl, attributes, expr))
8454 add_stmt (c_parser_compound_statement (parser));
8455 objc_finish_method_definition (current_function_decl);
8457 else
8459 /* This code is executed when we find a method definition
8460 outside of an @implementation context (or invalid for other
8461 reasons). Parse the method (to keep going) but do not emit
8462 any code.
8464 c_parser_compound_statement (parser);
8468 /* Parse an objc-methodprotolist.
8470 objc-methodprotolist:
8471 empty
8472 objc-methodprotolist objc-methodproto
8473 objc-methodprotolist declaration
8474 objc-methodprotolist ;
8475 @optional
8476 @required
8478 The declaration is a data definition, which may be missing
8479 declaration specifiers under the same rules and diagnostics as
8480 other data definitions outside functions, and the stray semicolon
8481 is diagnosed the same way as a stray semicolon outside a
8482 function. */
8484 static void
8485 c_parser_objc_methodprotolist (c_parser *parser)
8487 while (true)
8489 /* The list is terminated by @end. */
8490 switch (c_parser_peek_token (parser)->type)
8492 case CPP_SEMICOLON:
8493 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
8494 "ISO C does not allow extra %<;%> outside of a function");
8495 c_parser_consume_token (parser);
8496 break;
8497 case CPP_PLUS:
8498 case CPP_MINUS:
8499 c_parser_objc_methodproto (parser);
8500 break;
8501 case CPP_PRAGMA:
8502 c_parser_pragma (parser, pragma_external);
8503 break;
8504 case CPP_EOF:
8505 return;
8506 default:
8507 if (c_parser_next_token_is_keyword (parser, RID_AT_END))
8508 return;
8509 else if (c_parser_next_token_is_keyword (parser, RID_AT_PROPERTY))
8510 c_parser_objc_at_property_declaration (parser);
8511 else if (c_parser_next_token_is_keyword (parser, RID_AT_OPTIONAL))
8513 objc_set_method_opt (true);
8514 c_parser_consume_token (parser);
8516 else if (c_parser_next_token_is_keyword (parser, RID_AT_REQUIRED))
8518 objc_set_method_opt (false);
8519 c_parser_consume_token (parser);
8521 else
8522 c_parser_declaration_or_fndef (parser, false, false, true,
8523 false, true, NULL, vNULL);
8524 break;
8529 /* Parse an objc-methodproto.
8531 objc-methodproto:
8532 objc-method-type objc-method-decl ;
8535 static void
8536 c_parser_objc_methodproto (c_parser *parser)
8538 bool is_class_method = c_parser_objc_method_type (parser);
8539 tree decl, attributes = NULL_TREE;
8541 /* Remember protocol qualifiers in prototypes. */
8542 parser->objc_pq_context = true;
8543 decl = c_parser_objc_method_decl (parser, is_class_method, &attributes,
8544 NULL);
8545 /* Forget protocol qualifiers now. */
8546 parser->objc_pq_context = false;
8548 /* Do not allow the presence of attributes to hide an erroneous
8549 method implementation in the interface section. */
8550 if (!c_parser_next_token_is (parser, CPP_SEMICOLON))
8552 c_parser_error (parser, "expected %<;%>");
8553 return;
8556 if (decl != error_mark_node)
8557 objc_add_method_declaration (is_class_method, decl, attributes);
8559 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
8562 /* If we are at a position that method attributes may be present, check that
8563 there are not any parsed already (a syntax error) and then collect any
8564 specified at the current location. Finally, if new attributes were present,
8565 check that the next token is legal ( ';' for decls and '{' for defs). */
8567 static bool
8568 c_parser_objc_maybe_method_attributes (c_parser* parser, tree* attributes)
8570 bool bad = false;
8571 if (*attributes)
8573 c_parser_error (parser,
8574 "method attributes must be specified at the end only");
8575 *attributes = NULL_TREE;
8576 bad = true;
8579 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
8580 *attributes = c_parser_attributes (parser);
8582 /* If there were no attributes here, just report any earlier error. */
8583 if (*attributes == NULL_TREE || bad)
8584 return bad;
8586 /* If the attributes are followed by a ; or {, then just report any earlier
8587 error. */
8588 if (c_parser_next_token_is (parser, CPP_SEMICOLON)
8589 || c_parser_next_token_is (parser, CPP_OPEN_BRACE))
8590 return bad;
8592 /* We've got attributes, but not at the end. */
8593 c_parser_error (parser,
8594 "expected %<;%> or %<{%> after method attribute definition");
8595 return true;
8598 /* Parse an objc-method-decl.
8600 objc-method-decl:
8601 ( objc-type-name ) objc-selector
8602 objc-selector
8603 ( objc-type-name ) objc-keyword-selector objc-optparmlist
8604 objc-keyword-selector objc-optparmlist
8605 attributes
8607 objc-keyword-selector:
8608 objc-keyword-decl
8609 objc-keyword-selector objc-keyword-decl
8611 objc-keyword-decl:
8612 objc-selector : ( objc-type-name ) identifier
8613 objc-selector : identifier
8614 : ( objc-type-name ) identifier
8615 : identifier
8617 objc-optparmlist:
8618 objc-optparms objc-optellipsis
8620 objc-optparms:
8621 empty
8622 objc-opt-parms , parameter-declaration
8624 objc-optellipsis:
8625 empty
8626 , ...
8629 static tree
8630 c_parser_objc_method_decl (c_parser *parser, bool is_class_method,
8631 tree *attributes, tree *expr)
8633 tree type = NULL_TREE;
8634 tree sel;
8635 tree parms = NULL_TREE;
8636 bool ellipsis = false;
8637 bool attr_err = false;
8639 *attributes = NULL_TREE;
8640 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
8642 c_parser_consume_token (parser);
8643 type = c_parser_objc_type_name (parser);
8644 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
8646 sel = c_parser_objc_selector (parser);
8647 /* If there is no selector, or a colon follows, we have an
8648 objc-keyword-selector. If there is a selector, and a colon does
8649 not follow, that selector ends the objc-method-decl. */
8650 if (!sel || c_parser_next_token_is (parser, CPP_COLON))
8652 tree tsel = sel;
8653 tree list = NULL_TREE;
8654 while (true)
8656 tree atype = NULL_TREE, id, keyworddecl;
8657 tree param_attr = NULL_TREE;
8658 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
8659 break;
8660 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
8662 c_parser_consume_token (parser);
8663 atype = c_parser_objc_type_name (parser);
8664 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
8665 "expected %<)%>");
8667 /* New ObjC allows attributes on method parameters. */
8668 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
8669 param_attr = c_parser_attributes (parser);
8670 if (c_parser_next_token_is_not (parser, CPP_NAME))
8672 c_parser_error (parser, "expected identifier");
8673 return error_mark_node;
8675 id = c_parser_peek_token (parser)->value;
8676 c_parser_consume_token (parser);
8677 keyworddecl = objc_build_keyword_decl (tsel, atype, id, param_attr);
8678 list = chainon (list, keyworddecl);
8679 tsel = c_parser_objc_selector (parser);
8680 if (!tsel && c_parser_next_token_is_not (parser, CPP_COLON))
8681 break;
8684 attr_err |= c_parser_objc_maybe_method_attributes (parser, attributes) ;
8686 /* Parse the optional parameter list. Optional Objective-C
8687 method parameters follow the C syntax, and may include '...'
8688 to denote a variable number of arguments. */
8689 parms = make_node (TREE_LIST);
8690 while (c_parser_next_token_is (parser, CPP_COMMA))
8692 struct c_parm *parm;
8693 c_parser_consume_token (parser);
8694 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
8696 ellipsis = true;
8697 c_parser_consume_token (parser);
8698 attr_err |= c_parser_objc_maybe_method_attributes
8699 (parser, attributes) ;
8700 break;
8702 parm = c_parser_parameter_declaration (parser, NULL_TREE);
8703 if (parm == NULL)
8704 break;
8705 parms = chainon (parms,
8706 build_tree_list (NULL_TREE, grokparm (parm, expr)));
8708 sel = list;
8710 else
8711 attr_err |= c_parser_objc_maybe_method_attributes (parser, attributes) ;
8713 if (sel == NULL)
8715 c_parser_error (parser, "objective-c method declaration is expected");
8716 return error_mark_node;
8719 if (attr_err)
8720 return error_mark_node;
8722 return objc_build_method_signature (is_class_method, type, sel, parms, ellipsis);
8725 /* Parse an objc-type-name.
8727 objc-type-name:
8728 objc-type-qualifiers[opt] type-name
8729 objc-type-qualifiers[opt]
8731 objc-type-qualifiers:
8732 objc-type-qualifier
8733 objc-type-qualifiers objc-type-qualifier
8735 objc-type-qualifier: one of
8736 in out inout bycopy byref oneway
8739 static tree
8740 c_parser_objc_type_name (c_parser *parser)
8742 tree quals = NULL_TREE;
8743 struct c_type_name *type_name = NULL;
8744 tree type = NULL_TREE;
8745 while (true)
8747 c_token *token = c_parser_peek_token (parser);
8748 if (token->type == CPP_KEYWORD
8749 && (token->keyword == RID_IN
8750 || token->keyword == RID_OUT
8751 || token->keyword == RID_INOUT
8752 || token->keyword == RID_BYCOPY
8753 || token->keyword == RID_BYREF
8754 || token->keyword == RID_ONEWAY))
8756 quals = chainon (build_tree_list (NULL_TREE, token->value), quals);
8757 c_parser_consume_token (parser);
8759 else
8760 break;
8762 if (c_parser_next_tokens_start_typename (parser, cla_prefer_type))
8763 type_name = c_parser_type_name (parser);
8764 if (type_name)
8765 type = groktypename (type_name, NULL, NULL);
8767 /* If the type is unknown, and error has already been produced and
8768 we need to recover from the error. In that case, use NULL_TREE
8769 for the type, as if no type had been specified; this will use the
8770 default type ('id') which is good for error recovery. */
8771 if (type == error_mark_node)
8772 type = NULL_TREE;
8774 return build_tree_list (quals, type);
8777 /* Parse objc-protocol-refs.
8779 objc-protocol-refs:
8780 < identifier-list >
8783 static tree
8784 c_parser_objc_protocol_refs (c_parser *parser)
8786 tree list = NULL_TREE;
8787 gcc_assert (c_parser_next_token_is (parser, CPP_LESS));
8788 c_parser_consume_token (parser);
8789 /* Any identifiers, including those declared as type names, are OK
8790 here. */
8791 while (true)
8793 tree id;
8794 if (c_parser_next_token_is_not (parser, CPP_NAME))
8796 c_parser_error (parser, "expected identifier");
8797 break;
8799 id = c_parser_peek_token (parser)->value;
8800 list = chainon (list, build_tree_list (NULL_TREE, id));
8801 c_parser_consume_token (parser);
8802 if (c_parser_next_token_is (parser, CPP_COMMA))
8803 c_parser_consume_token (parser);
8804 else
8805 break;
8807 c_parser_require (parser, CPP_GREATER, "expected %<>%>");
8808 return list;
8811 /* Parse an objc-try-catch-finally-statement.
8813 objc-try-catch-finally-statement:
8814 @try compound-statement objc-catch-list[opt]
8815 @try compound-statement objc-catch-list[opt] @finally compound-statement
8817 objc-catch-list:
8818 @catch ( objc-catch-parameter-declaration ) compound-statement
8819 objc-catch-list @catch ( objc-catch-parameter-declaration ) compound-statement
8821 objc-catch-parameter-declaration:
8822 parameter-declaration
8823 '...'
8825 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
8827 PS: This function is identical to cp_parser_objc_try_catch_finally_statement
8828 for C++. Keep them in sync. */
8830 static void
8831 c_parser_objc_try_catch_finally_statement (c_parser *parser)
8833 location_t location;
8834 tree stmt;
8836 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_TRY));
8837 c_parser_consume_token (parser);
8838 location = c_parser_peek_token (parser)->location;
8839 objc_maybe_warn_exceptions (location);
8840 stmt = c_parser_compound_statement (parser);
8841 objc_begin_try_stmt (location, stmt);
8843 while (c_parser_next_token_is_keyword (parser, RID_AT_CATCH))
8845 struct c_parm *parm;
8846 tree parameter_declaration = error_mark_node;
8847 bool seen_open_paren = false;
8849 c_parser_consume_token (parser);
8850 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
8851 seen_open_paren = true;
8852 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
8854 /* We have "@catch (...)" (where the '...' are literally
8855 what is in the code). Skip the '...'.
8856 parameter_declaration is set to NULL_TREE, and
8857 objc_being_catch_clauses() knows that that means
8858 '...'. */
8859 c_parser_consume_token (parser);
8860 parameter_declaration = NULL_TREE;
8862 else
8864 /* We have "@catch (NSException *exception)" or something
8865 like that. Parse the parameter declaration. */
8866 parm = c_parser_parameter_declaration (parser, NULL_TREE);
8867 if (parm == NULL)
8868 parameter_declaration = error_mark_node;
8869 else
8870 parameter_declaration = grokparm (parm, NULL);
8872 if (seen_open_paren)
8873 c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
8874 else
8876 /* If there was no open parenthesis, we are recovering from
8877 an error, and we are trying to figure out what mistake
8878 the user has made. */
8880 /* If there is an immediate closing parenthesis, the user
8881 probably forgot the opening one (ie, they typed "@catch
8882 NSException *e)". Parse the closing parenthesis and keep
8883 going. */
8884 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
8885 c_parser_consume_token (parser);
8887 /* If these is no immediate closing parenthesis, the user
8888 probably doesn't know that parenthesis are required at
8889 all (ie, they typed "@catch NSException *e"). So, just
8890 forget about the closing parenthesis and keep going. */
8892 objc_begin_catch_clause (parameter_declaration);
8893 if (c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
8894 c_parser_compound_statement_nostart (parser);
8895 objc_finish_catch_clause ();
8897 if (c_parser_next_token_is_keyword (parser, RID_AT_FINALLY))
8899 c_parser_consume_token (parser);
8900 location = c_parser_peek_token (parser)->location;
8901 stmt = c_parser_compound_statement (parser);
8902 objc_build_finally_clause (location, stmt);
8904 objc_finish_try_stmt ();
8907 /* Parse an objc-synchronized-statement.
8909 objc-synchronized-statement:
8910 @synchronized ( expression ) compound-statement
8913 static void
8914 c_parser_objc_synchronized_statement (c_parser *parser)
8916 location_t loc;
8917 tree expr, stmt;
8918 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_SYNCHRONIZED));
8919 c_parser_consume_token (parser);
8920 loc = c_parser_peek_token (parser)->location;
8921 objc_maybe_warn_exceptions (loc);
8922 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
8924 struct c_expr ce = c_parser_expression (parser);
8925 ce = convert_lvalue_to_rvalue (loc, ce, false, false);
8926 expr = ce.value;
8927 expr = c_fully_fold (expr, false, NULL);
8928 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
8930 else
8931 expr = error_mark_node;
8932 stmt = c_parser_compound_statement (parser);
8933 objc_build_synchronized (loc, expr, stmt);
8936 /* Parse an objc-selector; return NULL_TREE without an error if the
8937 next token is not an objc-selector.
8939 objc-selector:
8940 identifier
8941 one of
8942 enum struct union if else while do for switch case default
8943 break continue return goto asm sizeof typeof __alignof
8944 unsigned long const short volatile signed restrict _Complex
8945 in out inout bycopy byref oneway int char float double void _Bool
8946 _Atomic
8948 ??? Why this selection of keywords but not, for example, storage
8949 class specifiers? */
8951 static tree
8952 c_parser_objc_selector (c_parser *parser)
8954 c_token *token = c_parser_peek_token (parser);
8955 tree value = token->value;
8956 if (token->type == CPP_NAME)
8958 c_parser_consume_token (parser);
8959 return value;
8961 if (token->type != CPP_KEYWORD)
8962 return NULL_TREE;
8963 switch (token->keyword)
8965 case RID_ENUM:
8966 case RID_STRUCT:
8967 case RID_UNION:
8968 case RID_IF:
8969 case RID_ELSE:
8970 case RID_WHILE:
8971 case RID_DO:
8972 case RID_FOR:
8973 case RID_SWITCH:
8974 case RID_CASE:
8975 case RID_DEFAULT:
8976 case RID_BREAK:
8977 case RID_CONTINUE:
8978 case RID_RETURN:
8979 case RID_GOTO:
8980 case RID_ASM:
8981 case RID_SIZEOF:
8982 case RID_TYPEOF:
8983 case RID_ALIGNOF:
8984 case RID_UNSIGNED:
8985 case RID_LONG:
8986 case RID_INT128:
8987 case RID_CONST:
8988 case RID_SHORT:
8989 case RID_VOLATILE:
8990 case RID_SIGNED:
8991 case RID_RESTRICT:
8992 case RID_COMPLEX:
8993 case RID_IN:
8994 case RID_OUT:
8995 case RID_INOUT:
8996 case RID_BYCOPY:
8997 case RID_BYREF:
8998 case RID_ONEWAY:
8999 case RID_INT:
9000 case RID_CHAR:
9001 case RID_FLOAT:
9002 case RID_DOUBLE:
9003 case RID_VOID:
9004 case RID_BOOL:
9005 case RID_ATOMIC:
9006 case RID_AUTO_TYPE:
9007 c_parser_consume_token (parser);
9008 return value;
9009 default:
9010 return NULL_TREE;
9014 /* Parse an objc-selector-arg.
9016 objc-selector-arg:
9017 objc-selector
9018 objc-keywordname-list
9020 objc-keywordname-list:
9021 objc-keywordname
9022 objc-keywordname-list objc-keywordname
9024 objc-keywordname:
9025 objc-selector :
9029 static tree
9030 c_parser_objc_selector_arg (c_parser *parser)
9032 tree sel = c_parser_objc_selector (parser);
9033 tree list = NULL_TREE;
9034 if (sel && c_parser_next_token_is_not (parser, CPP_COLON))
9035 return sel;
9036 while (true)
9038 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
9039 return list;
9040 list = chainon (list, build_tree_list (sel, NULL_TREE));
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-receiver.
9050 objc-receiver:
9051 expression
9052 class-name
9053 type-name
9056 static tree
9057 c_parser_objc_receiver (c_parser *parser)
9059 location_t loc = c_parser_peek_token (parser)->location;
9061 if (c_parser_peek_token (parser)->type == CPP_NAME
9062 && (c_parser_peek_token (parser)->id_kind == C_ID_TYPENAME
9063 || c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME))
9065 tree id = c_parser_peek_token (parser)->value;
9066 c_parser_consume_token (parser);
9067 return objc_get_class_reference (id);
9069 struct c_expr ce = c_parser_expression (parser);
9070 ce = convert_lvalue_to_rvalue (loc, ce, false, false);
9071 return c_fully_fold (ce.value, false, NULL);
9074 /* Parse objc-message-args.
9076 objc-message-args:
9077 objc-selector
9078 objc-keywordarg-list
9080 objc-keywordarg-list:
9081 objc-keywordarg
9082 objc-keywordarg-list objc-keywordarg
9084 objc-keywordarg:
9085 objc-selector : objc-keywordexpr
9086 : objc-keywordexpr
9089 static tree
9090 c_parser_objc_message_args (c_parser *parser)
9092 tree sel = c_parser_objc_selector (parser);
9093 tree list = NULL_TREE;
9094 if (sel && c_parser_next_token_is_not (parser, CPP_COLON))
9095 return sel;
9096 while (true)
9098 tree keywordexpr;
9099 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
9100 return error_mark_node;
9101 keywordexpr = c_parser_objc_keywordexpr (parser);
9102 list = chainon (list, build_tree_list (sel, keywordexpr));
9103 sel = c_parser_objc_selector (parser);
9104 if (!sel && c_parser_next_token_is_not (parser, CPP_COLON))
9105 break;
9107 return list;
9110 /* Parse an objc-keywordexpr.
9112 objc-keywordexpr:
9113 nonempty-expr-list
9116 static tree
9117 c_parser_objc_keywordexpr (c_parser *parser)
9119 tree ret;
9120 vec<tree, va_gc> *expr_list = c_parser_expr_list (parser, true, true,
9121 NULL, NULL, NULL, NULL);
9122 if (vec_safe_length (expr_list) == 1)
9124 /* Just return the expression, remove a level of
9125 indirection. */
9126 ret = (*expr_list)[0];
9128 else
9130 /* We have a comma expression, we will collapse later. */
9131 ret = build_tree_list_vec (expr_list);
9133 release_tree_vector (expr_list);
9134 return ret;
9137 /* A check, needed in several places, that ObjC interface, implementation or
9138 method definitions are not prefixed by incorrect items. */
9139 static bool
9140 c_parser_objc_diagnose_bad_element_prefix (c_parser *parser,
9141 struct c_declspecs *specs)
9143 if (!specs->declspecs_seen_p || specs->non_sc_seen_p
9144 || specs->typespec_kind != ctsk_none)
9146 c_parser_error (parser,
9147 "no type or storage class may be specified here,");
9148 c_parser_skip_to_end_of_block_or_statement (parser);
9149 return true;
9151 return false;
9154 /* Parse an Objective-C @property declaration. The syntax is:
9156 objc-property-declaration:
9157 '@property' objc-property-attributes[opt] struct-declaration ;
9159 objc-property-attributes:
9160 '(' objc-property-attribute-list ')'
9162 objc-property-attribute-list:
9163 objc-property-attribute
9164 objc-property-attribute-list, objc-property-attribute
9166 objc-property-attribute
9167 'getter' = identifier
9168 'setter' = identifier
9169 'readonly'
9170 'readwrite'
9171 'assign'
9172 'retain'
9173 'copy'
9174 'nonatomic'
9176 For example:
9177 @property NSString *name;
9178 @property (readonly) id object;
9179 @property (retain, nonatomic, getter=getTheName) id name;
9180 @property int a, b, c;
9182 PS: This function is identical to cp_parser_objc_at_propery_declaration
9183 for C++. Keep them in sync. */
9184 static void
9185 c_parser_objc_at_property_declaration (c_parser *parser)
9187 /* The following variables hold the attributes of the properties as
9188 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
9189 seen. When we see an attribute, we set them to 'true' (if they
9190 are boolean properties) or to the identifier (if they have an
9191 argument, ie, for getter and setter). Note that here we only
9192 parse the list of attributes, check the syntax and accumulate the
9193 attributes that we find. objc_add_property_declaration() will
9194 then process the information. */
9195 bool property_assign = false;
9196 bool property_copy = false;
9197 tree property_getter_ident = NULL_TREE;
9198 bool property_nonatomic = false;
9199 bool property_readonly = false;
9200 bool property_readwrite = false;
9201 bool property_retain = false;
9202 tree property_setter_ident = NULL_TREE;
9204 /* 'properties' is the list of properties that we read. Usually a
9205 single one, but maybe more (eg, in "@property int a, b, c;" there
9206 are three). */
9207 tree properties;
9208 location_t loc;
9210 loc = c_parser_peek_token (parser)->location;
9211 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_PROPERTY));
9213 c_parser_consume_token (parser); /* Eat '@property'. */
9215 /* Parse the optional attribute list... */
9216 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
9218 /* Eat the '(' */
9219 c_parser_consume_token (parser);
9221 /* Property attribute keywords are valid now. */
9222 parser->objc_property_attr_context = true;
9224 while (true)
9226 bool syntax_error = false;
9227 c_token *token = c_parser_peek_token (parser);
9228 enum rid keyword;
9230 if (token->type != CPP_KEYWORD)
9232 if (token->type == CPP_CLOSE_PAREN)
9233 c_parser_error (parser, "expected identifier");
9234 else
9236 c_parser_consume_token (parser);
9237 c_parser_error (parser, "unknown property attribute");
9239 break;
9241 keyword = token->keyword;
9242 c_parser_consume_token (parser);
9243 switch (keyword)
9245 case RID_ASSIGN: property_assign = true; break;
9246 case RID_COPY: property_copy = true; break;
9247 case RID_NONATOMIC: property_nonatomic = true; break;
9248 case RID_READONLY: property_readonly = true; break;
9249 case RID_READWRITE: property_readwrite = true; break;
9250 case RID_RETAIN: property_retain = true; break;
9252 case RID_GETTER:
9253 case RID_SETTER:
9254 if (c_parser_next_token_is_not (parser, CPP_EQ))
9256 if (keyword == RID_GETTER)
9257 c_parser_error (parser,
9258 "missing %<=%> (after %<getter%> attribute)");
9259 else
9260 c_parser_error (parser,
9261 "missing %<=%> (after %<setter%> attribute)");
9262 syntax_error = true;
9263 break;
9265 c_parser_consume_token (parser); /* eat the = */
9266 if (c_parser_next_token_is_not (parser, CPP_NAME))
9268 c_parser_error (parser, "expected identifier");
9269 syntax_error = true;
9270 break;
9272 if (keyword == RID_SETTER)
9274 if (property_setter_ident != NULL_TREE)
9275 c_parser_error (parser, "the %<setter%> attribute may only be specified once");
9276 else
9277 property_setter_ident = c_parser_peek_token (parser)->value;
9278 c_parser_consume_token (parser);
9279 if (c_parser_next_token_is_not (parser, CPP_COLON))
9280 c_parser_error (parser, "setter name must terminate with %<:%>");
9281 else
9282 c_parser_consume_token (parser);
9284 else
9286 if (property_getter_ident != NULL_TREE)
9287 c_parser_error (parser, "the %<getter%> attribute may only be specified once");
9288 else
9289 property_getter_ident = c_parser_peek_token (parser)->value;
9290 c_parser_consume_token (parser);
9292 break;
9293 default:
9294 c_parser_error (parser, "unknown property attribute");
9295 syntax_error = true;
9296 break;
9299 if (syntax_error)
9300 break;
9302 if (c_parser_next_token_is (parser, CPP_COMMA))
9303 c_parser_consume_token (parser);
9304 else
9305 break;
9307 parser->objc_property_attr_context = false;
9308 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
9310 /* ... and the property declaration(s). */
9311 properties = c_parser_struct_declaration (parser);
9313 if (properties == error_mark_node)
9315 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
9316 parser->error = false;
9317 return;
9320 if (properties == NULL_TREE)
9321 c_parser_error (parser, "expected identifier");
9322 else
9324 /* Comma-separated properties are chained together in
9325 reverse order; add them one by one. */
9326 properties = nreverse (properties);
9328 for (; properties; properties = TREE_CHAIN (properties))
9329 objc_add_property_declaration (loc, copy_node (properties),
9330 property_readonly, property_readwrite,
9331 property_assign, property_retain,
9332 property_copy, property_nonatomic,
9333 property_getter_ident, property_setter_ident);
9336 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
9337 parser->error = false;
9340 /* Parse an Objective-C @synthesize declaration. The syntax is:
9342 objc-synthesize-declaration:
9343 @synthesize objc-synthesize-identifier-list ;
9345 objc-synthesize-identifier-list:
9346 objc-synthesize-identifier
9347 objc-synthesize-identifier-list, objc-synthesize-identifier
9349 objc-synthesize-identifier
9350 identifier
9351 identifier = identifier
9353 For example:
9354 @synthesize MyProperty;
9355 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
9357 PS: This function is identical to cp_parser_objc_at_synthesize_declaration
9358 for C++. Keep them in sync.
9360 static void
9361 c_parser_objc_at_synthesize_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_SYNTHESIZE));
9366 loc = c_parser_peek_token (parser)->location;
9368 c_parser_consume_token (parser);
9369 while (true)
9371 tree property, ivar;
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 /* Once we find the semicolon, we can resume normal parsing.
9377 We have to reset parser->error manually because
9378 c_parser_skip_until_found() won't reset it for us if the
9379 next token is precisely a semicolon. */
9380 parser->error = false;
9381 return;
9383 property = c_parser_peek_token (parser)->value;
9384 c_parser_consume_token (parser);
9385 if (c_parser_next_token_is (parser, CPP_EQ))
9387 c_parser_consume_token (parser);
9388 if (c_parser_next_token_is_not (parser, CPP_NAME))
9390 c_parser_error (parser, "expected identifier");
9391 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
9392 parser->error = false;
9393 return;
9395 ivar = c_parser_peek_token (parser)->value;
9396 c_parser_consume_token (parser);
9398 else
9399 ivar = NULL_TREE;
9400 list = chainon (list, build_tree_list (ivar, property));
9401 if (c_parser_next_token_is (parser, CPP_COMMA))
9402 c_parser_consume_token (parser);
9403 else
9404 break;
9406 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
9407 objc_add_synthesize_declaration (loc, list);
9410 /* Parse an Objective-C @dynamic declaration. The syntax is:
9412 objc-dynamic-declaration:
9413 @dynamic identifier-list ;
9415 For example:
9416 @dynamic MyProperty;
9417 @dynamic MyProperty, AnotherProperty;
9419 PS: This function is identical to cp_parser_objc_at_dynamic_declaration
9420 for C++. Keep them in sync.
9422 static void
9423 c_parser_objc_at_dynamic_declaration (c_parser *parser)
9425 tree list = NULL_TREE;
9426 location_t loc;
9427 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_DYNAMIC));
9428 loc = c_parser_peek_token (parser)->location;
9430 c_parser_consume_token (parser);
9431 while (true)
9433 tree property;
9434 if (c_parser_next_token_is_not (parser, CPP_NAME))
9436 c_parser_error (parser, "expected identifier");
9437 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
9438 parser->error = false;
9439 return;
9441 property = c_parser_peek_token (parser)->value;
9442 list = chainon (list, build_tree_list (NULL_TREE, property));
9443 c_parser_consume_token (parser);
9444 if (c_parser_next_token_is (parser, CPP_COMMA))
9445 c_parser_consume_token (parser);
9446 else
9447 break;
9449 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
9450 objc_add_dynamic_declaration (loc, list);
9454 /* Handle pragmas. Some OpenMP pragmas are associated with, and therefore
9455 should be considered, statements. ALLOW_STMT is true if we're within
9456 the context of a function and such pragmas are to be allowed. Returns
9457 true if we actually parsed such a pragma. */
9459 static bool
9460 c_parser_pragma (c_parser *parser, enum pragma_context context)
9462 unsigned int id;
9464 id = c_parser_peek_token (parser)->pragma_kind;
9465 gcc_assert (id != PRAGMA_NONE);
9467 switch (id)
9469 case PRAGMA_OMP_BARRIER:
9470 if (context != pragma_compound)
9472 if (context == pragma_stmt)
9473 c_parser_error (parser, "%<#pragma omp barrier%> may only be "
9474 "used in compound statements");
9475 goto bad_stmt;
9477 c_parser_omp_barrier (parser);
9478 return false;
9480 case PRAGMA_OMP_FLUSH:
9481 if (context != pragma_compound)
9483 if (context == pragma_stmt)
9484 c_parser_error (parser, "%<#pragma omp flush%> may only be "
9485 "used in compound statements");
9486 goto bad_stmt;
9488 c_parser_omp_flush (parser);
9489 return false;
9491 case PRAGMA_OMP_TASKWAIT:
9492 if (context != pragma_compound)
9494 if (context == pragma_stmt)
9495 c_parser_error (parser, "%<#pragma omp taskwait%> may only be "
9496 "used in compound statements");
9497 goto bad_stmt;
9499 c_parser_omp_taskwait (parser);
9500 return false;
9502 case PRAGMA_OMP_TASKYIELD:
9503 if (context != pragma_compound)
9505 if (context == pragma_stmt)
9506 c_parser_error (parser, "%<#pragma omp taskyield%> may only be "
9507 "used in compound statements");
9508 goto bad_stmt;
9510 c_parser_omp_taskyield (parser);
9511 return false;
9513 case PRAGMA_OMP_CANCEL:
9514 if (context != pragma_compound)
9516 if (context == pragma_stmt)
9517 c_parser_error (parser, "%<#pragma omp cancel%> may only be "
9518 "used in compound statements");
9519 goto bad_stmt;
9521 c_parser_omp_cancel (parser);
9522 return false;
9524 case PRAGMA_OMP_CANCELLATION_POINT:
9525 if (context != pragma_compound)
9527 if (context == pragma_stmt)
9528 c_parser_error (parser, "%<#pragma omp cancellation point%> may "
9529 "only be used in compound statements");
9530 goto bad_stmt;
9532 c_parser_omp_cancellation_point (parser);
9533 return false;
9535 case PRAGMA_OMP_THREADPRIVATE:
9536 c_parser_omp_threadprivate (parser);
9537 return false;
9539 case PRAGMA_OMP_TARGET:
9540 return c_parser_omp_target (parser, context);
9542 case PRAGMA_OMP_END_DECLARE_TARGET:
9543 c_parser_omp_end_declare_target (parser);
9544 return false;
9546 case PRAGMA_OMP_SECTION:
9547 error_at (c_parser_peek_token (parser)->location,
9548 "%<#pragma omp section%> may only be used in "
9549 "%<#pragma omp sections%> construct");
9550 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
9551 return false;
9553 case PRAGMA_OMP_DECLARE_REDUCTION:
9554 c_parser_omp_declare (parser, context);
9555 return false;
9556 case PRAGMA_IVDEP:
9557 c_parser_consume_pragma (parser);
9558 c_parser_skip_to_pragma_eol (parser);
9559 if (!c_parser_next_token_is_keyword (parser, RID_FOR)
9560 && !c_parser_next_token_is_keyword (parser, RID_WHILE)
9561 && !c_parser_next_token_is_keyword (parser, RID_DO))
9563 c_parser_error (parser, "for, while or do statement expected");
9564 return false;
9566 if (c_parser_next_token_is_keyword (parser, RID_FOR))
9567 c_parser_for_statement (parser, true);
9568 else if (c_parser_next_token_is_keyword (parser, RID_WHILE))
9569 c_parser_while_statement (parser, true);
9570 else
9571 c_parser_do_statement (parser, true);
9572 return false;
9574 case PRAGMA_GCC_PCH_PREPROCESS:
9575 c_parser_error (parser, "%<#pragma GCC pch_preprocess%> must be first");
9576 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
9577 return false;
9579 case PRAGMA_CILK_SIMD:
9580 if (!c_parser_cilk_verify_simd (parser, context))
9581 return false;
9582 c_parser_consume_pragma (parser);
9583 c_parser_cilk_simd (parser);
9584 return false;
9586 default:
9587 if (id < PRAGMA_FIRST_EXTERNAL)
9589 if (context != pragma_stmt && context != pragma_compound)
9591 bad_stmt:
9592 c_parser_error (parser, "expected declaration specifiers");
9593 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
9594 return false;
9596 c_parser_omp_construct (parser);
9597 return true;
9599 break;
9602 c_parser_consume_pragma (parser);
9603 c_invoke_pragma_handler (id);
9605 /* Skip to EOL, but suppress any error message. Those will have been
9606 generated by the handler routine through calling error, as opposed
9607 to calling c_parser_error. */
9608 parser->error = true;
9609 c_parser_skip_to_pragma_eol (parser);
9611 return false;
9614 /* The interface the pragma parsers have to the lexer. */
9616 enum cpp_ttype
9617 pragma_lex (tree *value)
9619 c_token *tok = c_parser_peek_token (the_parser);
9620 enum cpp_ttype ret = tok->type;
9622 *value = tok->value;
9623 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
9624 ret = CPP_EOF;
9625 else
9627 if (ret == CPP_KEYWORD)
9628 ret = CPP_NAME;
9629 c_parser_consume_token (the_parser);
9632 return ret;
9635 static void
9636 c_parser_pragma_pch_preprocess (c_parser *parser)
9638 tree name = NULL;
9640 c_parser_consume_pragma (parser);
9641 if (c_parser_next_token_is (parser, CPP_STRING))
9643 name = c_parser_peek_token (parser)->value;
9644 c_parser_consume_token (parser);
9646 else
9647 c_parser_error (parser, "expected string literal");
9648 c_parser_skip_to_pragma_eol (parser);
9650 if (name)
9651 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
9654 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 parsing routines. */
9656 /* Returns name of the next clause.
9657 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
9658 the token is not consumed. Otherwise appropriate pragma_omp_clause is
9659 returned and the token is consumed. */
9661 static pragma_omp_clause
9662 c_parser_omp_clause_name (c_parser *parser)
9664 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
9666 if (c_parser_next_token_is_keyword (parser, RID_IF))
9667 result = PRAGMA_OMP_CLAUSE_IF;
9668 else if (c_parser_next_token_is_keyword (parser, RID_DEFAULT))
9669 result = PRAGMA_OMP_CLAUSE_DEFAULT;
9670 else if (c_parser_next_token_is_keyword (parser, RID_FOR))
9671 result = PRAGMA_OMP_CLAUSE_FOR;
9672 else if (c_parser_next_token_is (parser, CPP_NAME))
9674 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
9676 switch (p[0])
9678 case 'a':
9679 if (!strcmp ("aligned", p))
9680 result = PRAGMA_OMP_CLAUSE_ALIGNED;
9681 break;
9682 case 'c':
9683 if (!strcmp ("collapse", p))
9684 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
9685 else if (!strcmp ("copyin", p))
9686 result = PRAGMA_OMP_CLAUSE_COPYIN;
9687 else if (!strcmp ("copyprivate", p))
9688 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
9689 break;
9690 case 'd':
9691 if (!strcmp ("depend", p))
9692 result = PRAGMA_OMP_CLAUSE_DEPEND;
9693 else if (!strcmp ("device", p))
9694 result = PRAGMA_OMP_CLAUSE_DEVICE;
9695 else if (!strcmp ("dist_schedule", p))
9696 result = PRAGMA_OMP_CLAUSE_DIST_SCHEDULE;
9697 break;
9698 case 'f':
9699 if (!strcmp ("final", p))
9700 result = PRAGMA_OMP_CLAUSE_FINAL;
9701 else if (!strcmp ("firstprivate", p))
9702 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
9703 else if (!strcmp ("from", p))
9704 result = PRAGMA_OMP_CLAUSE_FROM;
9705 break;
9706 case 'i':
9707 if (!strcmp ("inbranch", p))
9708 result = PRAGMA_OMP_CLAUSE_INBRANCH;
9709 break;
9710 case 'l':
9711 if (!strcmp ("lastprivate", p))
9712 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
9713 else if (!strcmp ("linear", p))
9714 result = PRAGMA_OMP_CLAUSE_LINEAR;
9715 break;
9716 case 'm':
9717 if (!strcmp ("map", p))
9718 result = PRAGMA_OMP_CLAUSE_MAP;
9719 else if (!strcmp ("mergeable", p))
9720 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
9721 else if (flag_cilkplus && !strcmp ("mask", p))
9722 result = PRAGMA_CILK_CLAUSE_MASK;
9723 break;
9724 case 'n':
9725 if (!strcmp ("notinbranch", p))
9726 result = PRAGMA_OMP_CLAUSE_NOTINBRANCH;
9727 else if (!strcmp ("nowait", p))
9728 result = PRAGMA_OMP_CLAUSE_NOWAIT;
9729 else if (!strcmp ("num_teams", p))
9730 result = PRAGMA_OMP_CLAUSE_NUM_TEAMS;
9731 else if (!strcmp ("num_threads", p))
9732 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
9733 else if (flag_cilkplus && !strcmp ("nomask", p))
9734 result = PRAGMA_CILK_CLAUSE_NOMASK;
9735 break;
9736 case 'o':
9737 if (!strcmp ("ordered", p))
9738 result = PRAGMA_OMP_CLAUSE_ORDERED;
9739 break;
9740 case 'p':
9741 if (!strcmp ("parallel", p))
9742 result = PRAGMA_OMP_CLAUSE_PARALLEL;
9743 else if (!strcmp ("private", p))
9744 result = PRAGMA_OMP_CLAUSE_PRIVATE;
9745 else if (!strcmp ("proc_bind", p))
9746 result = PRAGMA_OMP_CLAUSE_PROC_BIND;
9747 break;
9748 case 'r':
9749 if (!strcmp ("reduction", p))
9750 result = PRAGMA_OMP_CLAUSE_REDUCTION;
9751 break;
9752 case 's':
9753 if (!strcmp ("safelen", p))
9754 result = PRAGMA_OMP_CLAUSE_SAFELEN;
9755 else if (!strcmp ("schedule", p))
9756 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
9757 else if (!strcmp ("sections", p))
9758 result = PRAGMA_OMP_CLAUSE_SECTIONS;
9759 else if (!strcmp ("shared", p))
9760 result = PRAGMA_OMP_CLAUSE_SHARED;
9761 else if (!strcmp ("simdlen", p))
9762 result = PRAGMA_OMP_CLAUSE_SIMDLEN;
9763 break;
9764 case 't':
9765 if (!strcmp ("taskgroup", p))
9766 result = PRAGMA_OMP_CLAUSE_TASKGROUP;
9767 else if (!strcmp ("thread_limit", p))
9768 result = PRAGMA_OMP_CLAUSE_THREAD_LIMIT;
9769 else if (!strcmp ("to", p))
9770 result = PRAGMA_OMP_CLAUSE_TO;
9771 break;
9772 case 'u':
9773 if (!strcmp ("uniform", p))
9774 result = PRAGMA_OMP_CLAUSE_UNIFORM;
9775 else if (!strcmp ("untied", p))
9776 result = PRAGMA_OMP_CLAUSE_UNTIED;
9777 break;
9778 case 'v':
9779 if (flag_cilkplus && !strcmp ("vectorlength", p))
9780 result = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
9781 break;
9785 if (result != PRAGMA_OMP_CLAUSE_NONE)
9786 c_parser_consume_token (parser);
9788 return result;
9791 /* Validate that a clause of the given type does not already exist. */
9793 static void
9794 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
9795 const char *name)
9797 tree c;
9799 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
9800 if (OMP_CLAUSE_CODE (c) == code)
9802 location_t loc = OMP_CLAUSE_LOCATION (c);
9803 error_at (loc, "too many %qs clauses", name);
9804 break;
9808 /* OpenMP 2.5:
9809 variable-list:
9810 identifier
9811 variable-list , identifier
9813 If KIND is nonzero, create the appropriate node and install the
9814 decl in OMP_CLAUSE_DECL and add the node to the head of the list.
9815 If KIND is nonzero, CLAUSE_LOC is the location of the clause.
9817 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
9818 return the list created. */
9820 static tree
9821 c_parser_omp_variable_list (c_parser *parser,
9822 location_t clause_loc,
9823 enum omp_clause_code kind, tree list)
9825 if (c_parser_next_token_is_not (parser, CPP_NAME)
9826 || c_parser_peek_token (parser)->id_kind != C_ID_ID)
9827 c_parser_error (parser, "expected identifier");
9829 while (c_parser_next_token_is (parser, CPP_NAME)
9830 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
9832 tree t = lookup_name (c_parser_peek_token (parser)->value);
9834 if (t == NULL_TREE)
9836 undeclared_variable (c_parser_peek_token (parser)->location,
9837 c_parser_peek_token (parser)->value);
9838 t = error_mark_node;
9841 c_parser_consume_token (parser);
9843 if (t == error_mark_node)
9845 else if (kind != 0)
9847 switch (kind)
9849 case OMP_CLAUSE_MAP:
9850 case OMP_CLAUSE_FROM:
9851 case OMP_CLAUSE_TO:
9852 case OMP_CLAUSE_DEPEND:
9853 while (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
9855 tree low_bound = NULL_TREE, length = NULL_TREE;
9857 c_parser_consume_token (parser);
9858 if (!c_parser_next_token_is (parser, CPP_COLON))
9859 low_bound = c_parser_expression (parser).value;
9860 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
9861 length = integer_one_node;
9862 else
9864 /* Look for `:'. */
9865 if (!c_parser_require (parser, CPP_COLON,
9866 "expected %<:%>"))
9868 t = error_mark_node;
9869 break;
9871 if (!c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
9872 length = c_parser_expression (parser).value;
9874 /* Look for the closing `]'. */
9875 if (!c_parser_require (parser, CPP_CLOSE_SQUARE,
9876 "expected %<]%>"))
9878 t = error_mark_node;
9879 break;
9881 t = tree_cons (low_bound, length, t);
9883 break;
9884 default:
9885 break;
9888 if (t != error_mark_node)
9890 tree u = build_omp_clause (clause_loc, kind);
9891 OMP_CLAUSE_DECL (u) = t;
9892 OMP_CLAUSE_CHAIN (u) = list;
9893 list = u;
9896 else
9897 list = tree_cons (t, NULL_TREE, list);
9899 if (c_parser_next_token_is_not (parser, CPP_COMMA))
9900 break;
9902 c_parser_consume_token (parser);
9905 return list;
9908 /* Similarly, but expect leading and trailing parenthesis. This is a very
9909 common case for omp clauses. */
9911 static tree
9912 c_parser_omp_var_list_parens (c_parser *parser, enum omp_clause_code kind,
9913 tree list)
9915 /* The clauses location. */
9916 location_t loc = c_parser_peek_token (parser)->location;
9918 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
9920 list = c_parser_omp_variable_list (parser, loc, kind, list);
9921 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
9923 return list;
9926 /* OpenMP 3.0:
9927 collapse ( constant-expression ) */
9929 static tree
9930 c_parser_omp_clause_collapse (c_parser *parser, tree list)
9932 tree c, num = error_mark_node;
9933 HOST_WIDE_INT n;
9934 location_t loc;
9936 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse");
9938 loc = c_parser_peek_token (parser)->location;
9939 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
9941 num = c_parser_expr_no_commas (parser, NULL).value;
9942 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
9944 if (num == error_mark_node)
9945 return list;
9946 mark_exp_read (num);
9947 num = c_fully_fold (num, false, NULL);
9948 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
9949 || !tree_fits_shwi_p (num)
9950 || (n = tree_to_shwi (num)) <= 0
9951 || (int) n != n)
9953 error_at (loc,
9954 "collapse argument needs positive constant integer expression");
9955 return list;
9957 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
9958 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
9959 OMP_CLAUSE_CHAIN (c) = list;
9960 return c;
9963 /* OpenMP 2.5:
9964 copyin ( variable-list ) */
9966 static tree
9967 c_parser_omp_clause_copyin (c_parser *parser, tree list)
9969 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_COPYIN, list);
9972 /* OpenMP 2.5:
9973 copyprivate ( variable-list ) */
9975 static tree
9976 c_parser_omp_clause_copyprivate (c_parser *parser, tree list)
9978 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_COPYPRIVATE, list);
9981 /* OpenMP 2.5:
9982 default ( shared | none ) */
9984 static tree
9985 c_parser_omp_clause_default (c_parser *parser, tree list)
9987 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
9988 location_t loc = c_parser_peek_token (parser)->location;
9989 tree c;
9991 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
9992 return list;
9993 if (c_parser_next_token_is (parser, CPP_NAME))
9995 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
9997 switch (p[0])
9999 case 'n':
10000 if (strcmp ("none", p) != 0)
10001 goto invalid_kind;
10002 kind = OMP_CLAUSE_DEFAULT_NONE;
10003 break;
10005 case 's':
10006 if (strcmp ("shared", p) != 0)
10007 goto invalid_kind;
10008 kind = OMP_CLAUSE_DEFAULT_SHARED;
10009 break;
10011 default:
10012 goto invalid_kind;
10015 c_parser_consume_token (parser);
10017 else
10019 invalid_kind:
10020 c_parser_error (parser, "expected %<none%> or %<shared%>");
10022 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10024 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
10025 return list;
10027 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default");
10028 c = build_omp_clause (loc, OMP_CLAUSE_DEFAULT);
10029 OMP_CLAUSE_CHAIN (c) = list;
10030 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
10032 return c;
10035 /* OpenMP 2.5:
10036 firstprivate ( variable-list ) */
10038 static tree
10039 c_parser_omp_clause_firstprivate (c_parser *parser, tree list)
10041 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_FIRSTPRIVATE, list);
10044 /* OpenMP 3.1:
10045 final ( expression ) */
10047 static tree
10048 c_parser_omp_clause_final (c_parser *parser, tree list)
10050 location_t loc = c_parser_peek_token (parser)->location;
10051 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
10053 tree t = c_parser_paren_condition (parser);
10054 tree c;
10056 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final");
10058 c = build_omp_clause (loc, OMP_CLAUSE_FINAL);
10059 OMP_CLAUSE_FINAL_EXPR (c) = t;
10060 OMP_CLAUSE_CHAIN (c) = list;
10061 list = c;
10063 else
10064 c_parser_error (parser, "expected %<(%>");
10066 return list;
10069 /* OpenMP 2.5:
10070 if ( expression ) */
10072 static tree
10073 c_parser_omp_clause_if (c_parser *parser, tree list)
10075 location_t loc = c_parser_peek_token (parser)->location;
10076 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
10078 tree t = c_parser_paren_condition (parser);
10079 tree c;
10081 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if");
10083 c = build_omp_clause (loc, OMP_CLAUSE_IF);
10084 OMP_CLAUSE_IF_EXPR (c) = t;
10085 OMP_CLAUSE_CHAIN (c) = list;
10086 list = c;
10088 else
10089 c_parser_error (parser, "expected %<(%>");
10091 return list;
10094 /* OpenMP 2.5:
10095 lastprivate ( variable-list ) */
10097 static tree
10098 c_parser_omp_clause_lastprivate (c_parser *parser, tree list)
10100 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_LASTPRIVATE, list);
10103 /* OpenMP 3.1:
10104 mergeable */
10106 static tree
10107 c_parser_omp_clause_mergeable (c_parser *parser ATTRIBUTE_UNUSED, tree list)
10109 tree c;
10111 /* FIXME: Should we allow duplicates? */
10112 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable");
10114 c = build_omp_clause (c_parser_peek_token (parser)->location,
10115 OMP_CLAUSE_MERGEABLE);
10116 OMP_CLAUSE_CHAIN (c) = list;
10118 return c;
10121 /* OpenMP 2.5:
10122 nowait */
10124 static tree
10125 c_parser_omp_clause_nowait (c_parser *parser ATTRIBUTE_UNUSED, tree list)
10127 tree c;
10128 location_t loc = c_parser_peek_token (parser)->location;
10130 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait");
10132 c = build_omp_clause (loc, OMP_CLAUSE_NOWAIT);
10133 OMP_CLAUSE_CHAIN (c) = list;
10134 return c;
10137 /* OpenMP 2.5:
10138 num_threads ( expression ) */
10140 static tree
10141 c_parser_omp_clause_num_threads (c_parser *parser, tree list)
10143 location_t num_threads_loc = c_parser_peek_token (parser)->location;
10144 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10146 location_t expr_loc = c_parser_peek_token (parser)->location;
10147 tree c, t = c_parser_expression (parser).value;
10148 mark_exp_read (t);
10149 t = c_fully_fold (t, false, NULL);
10151 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10153 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
10155 c_parser_error (parser, "expected integer expression");
10156 return list;
10159 /* Attempt to statically determine when the number isn't positive. */
10160 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
10161 build_int_cst (TREE_TYPE (t), 0));
10162 if (CAN_HAVE_LOCATION_P (c))
10163 SET_EXPR_LOCATION (c, expr_loc);
10164 if (c == boolean_true_node)
10166 warning_at (expr_loc, 0,
10167 "%<num_threads%> value must be positive");
10168 t = integer_one_node;
10171 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS, "num_threads");
10173 c = build_omp_clause (num_threads_loc, OMP_CLAUSE_NUM_THREADS);
10174 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
10175 OMP_CLAUSE_CHAIN (c) = list;
10176 list = c;
10179 return list;
10182 /* OpenMP 2.5:
10183 ordered */
10185 static tree
10186 c_parser_omp_clause_ordered (c_parser *parser, tree list)
10188 tree c;
10190 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED, "ordered");
10192 c = build_omp_clause (c_parser_peek_token (parser)->location,
10193 OMP_CLAUSE_ORDERED);
10194 OMP_CLAUSE_CHAIN (c) = list;
10196 return c;
10199 /* OpenMP 2.5:
10200 private ( variable-list ) */
10202 static tree
10203 c_parser_omp_clause_private (c_parser *parser, tree list)
10205 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_PRIVATE, list);
10208 /* OpenMP 2.5:
10209 reduction ( reduction-operator : variable-list )
10211 reduction-operator:
10212 One of: + * - & ^ | && ||
10214 OpenMP 3.1:
10216 reduction-operator:
10217 One of: + * - & ^ | && || max min
10219 OpenMP 4.0:
10221 reduction-operator:
10222 One of: + * - & ^ | && ||
10223 identifier */
10225 static tree
10226 c_parser_omp_clause_reduction (c_parser *parser, tree list)
10228 location_t clause_loc = c_parser_peek_token (parser)->location;
10229 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10231 enum tree_code code = ERROR_MARK;
10232 tree reduc_id = NULL_TREE;
10234 switch (c_parser_peek_token (parser)->type)
10236 case CPP_PLUS:
10237 code = PLUS_EXPR;
10238 break;
10239 case CPP_MULT:
10240 code = MULT_EXPR;
10241 break;
10242 case CPP_MINUS:
10243 code = MINUS_EXPR;
10244 break;
10245 case CPP_AND:
10246 code = BIT_AND_EXPR;
10247 break;
10248 case CPP_XOR:
10249 code = BIT_XOR_EXPR;
10250 break;
10251 case CPP_OR:
10252 code = BIT_IOR_EXPR;
10253 break;
10254 case CPP_AND_AND:
10255 code = TRUTH_ANDIF_EXPR;
10256 break;
10257 case CPP_OR_OR:
10258 code = TRUTH_ORIF_EXPR;
10259 break;
10260 case CPP_NAME:
10262 const char *p
10263 = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
10264 if (strcmp (p, "min") == 0)
10266 code = MIN_EXPR;
10267 break;
10269 if (strcmp (p, "max") == 0)
10271 code = MAX_EXPR;
10272 break;
10274 reduc_id = c_parser_peek_token (parser)->value;
10275 break;
10277 default:
10278 c_parser_error (parser,
10279 "expected %<+%>, %<*%>, %<-%>, %<&%>, "
10280 "%<^%>, %<|%>, %<&&%>, %<||%>, %<min%> or %<max%>");
10281 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 0);
10282 return list;
10284 c_parser_consume_token (parser);
10285 reduc_id = c_omp_reduction_id (code, reduc_id);
10286 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
10288 tree nl, c;
10290 nl = c_parser_omp_variable_list (parser, clause_loc,
10291 OMP_CLAUSE_REDUCTION, list);
10292 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
10294 tree type = TREE_TYPE (OMP_CLAUSE_DECL (c));
10295 OMP_CLAUSE_REDUCTION_CODE (c) = code;
10296 if (code == ERROR_MARK
10297 || !(INTEGRAL_TYPE_P (type)
10298 || TREE_CODE (type) == REAL_TYPE
10299 || TREE_CODE (type) == COMPLEX_TYPE))
10300 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c)
10301 = c_omp_reduction_lookup (reduc_id,
10302 TYPE_MAIN_VARIANT (type));
10305 list = nl;
10307 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10309 return list;
10312 /* OpenMP 2.5:
10313 schedule ( schedule-kind )
10314 schedule ( schedule-kind , expression )
10316 schedule-kind:
10317 static | dynamic | guided | runtime | auto
10320 static tree
10321 c_parser_omp_clause_schedule (c_parser *parser, tree list)
10323 tree c, t;
10324 location_t loc = c_parser_peek_token (parser)->location;
10326 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10327 return list;
10329 c = build_omp_clause (loc, OMP_CLAUSE_SCHEDULE);
10331 if (c_parser_next_token_is (parser, CPP_NAME))
10333 tree kind = c_parser_peek_token (parser)->value;
10334 const char *p = IDENTIFIER_POINTER (kind);
10336 switch (p[0])
10338 case 'd':
10339 if (strcmp ("dynamic", p) != 0)
10340 goto invalid_kind;
10341 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
10342 break;
10344 case 'g':
10345 if (strcmp ("guided", p) != 0)
10346 goto invalid_kind;
10347 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
10348 break;
10350 case 'r':
10351 if (strcmp ("runtime", p) != 0)
10352 goto invalid_kind;
10353 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
10354 break;
10356 default:
10357 goto invalid_kind;
10360 else if (c_parser_next_token_is_keyword (parser, RID_STATIC))
10361 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
10362 else if (c_parser_next_token_is_keyword (parser, RID_AUTO))
10363 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
10364 else
10365 goto invalid_kind;
10367 c_parser_consume_token (parser);
10368 if (c_parser_next_token_is (parser, CPP_COMMA))
10370 location_t here;
10371 c_parser_consume_token (parser);
10373 here = c_parser_peek_token (parser)->location;
10374 t = c_parser_expr_no_commas (parser, NULL).value;
10375 mark_exp_read (t);
10376 t = c_fully_fold (t, false, NULL);
10378 if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
10379 error_at (here, "schedule %<runtime%> does not take "
10380 "a %<chunk_size%> parameter");
10381 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
10382 error_at (here,
10383 "schedule %<auto%> does not take "
10384 "a %<chunk_size%> parameter");
10385 else if (TREE_CODE (TREE_TYPE (t)) == INTEGER_TYPE)
10386 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
10387 else
10388 c_parser_error (parser, "expected integer expression");
10390 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10392 else
10393 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
10394 "expected %<,%> or %<)%>");
10396 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule");
10397 OMP_CLAUSE_CHAIN (c) = list;
10398 return c;
10400 invalid_kind:
10401 c_parser_error (parser, "invalid schedule kind");
10402 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 0);
10403 return list;
10406 /* OpenMP 2.5:
10407 shared ( variable-list ) */
10409 static tree
10410 c_parser_omp_clause_shared (c_parser *parser, tree list)
10412 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_SHARED, list);
10415 /* OpenMP 3.0:
10416 untied */
10418 static tree
10419 c_parser_omp_clause_untied (c_parser *parser ATTRIBUTE_UNUSED, tree list)
10421 tree c;
10423 /* FIXME: Should we allow duplicates? */
10424 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied");
10426 c = build_omp_clause (c_parser_peek_token (parser)->location,
10427 OMP_CLAUSE_UNTIED);
10428 OMP_CLAUSE_CHAIN (c) = list;
10430 return c;
10433 /* OpenMP 4.0:
10434 inbranch
10435 notinbranch */
10437 static tree
10438 c_parser_omp_clause_branch (c_parser *parser ATTRIBUTE_UNUSED,
10439 enum omp_clause_code code, tree list)
10441 check_no_duplicate_clause (list, code, omp_clause_code_name[code]);
10443 tree c = build_omp_clause (c_parser_peek_token (parser)->location, code);
10444 OMP_CLAUSE_CHAIN (c) = list;
10446 return c;
10449 /* OpenMP 4.0:
10450 parallel
10452 sections
10453 taskgroup */
10455 static tree
10456 c_parser_omp_clause_cancelkind (c_parser *parser ATTRIBUTE_UNUSED,
10457 enum omp_clause_code code, tree list)
10459 tree c = build_omp_clause (c_parser_peek_token (parser)->location, code);
10460 OMP_CLAUSE_CHAIN (c) = list;
10462 return c;
10465 /* OpenMP 4.0:
10466 num_teams ( expression ) */
10468 static tree
10469 c_parser_omp_clause_num_teams (c_parser *parser, tree list)
10471 location_t num_teams_loc = c_parser_peek_token (parser)->location;
10472 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10474 location_t expr_loc = c_parser_peek_token (parser)->location;
10475 tree c, t = c_parser_expression (parser).value;
10476 mark_exp_read (t);
10477 t = c_fully_fold (t, false, NULL);
10479 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10481 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
10483 c_parser_error (parser, "expected integer expression");
10484 return list;
10487 /* Attempt to statically determine when the number isn't positive. */
10488 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
10489 build_int_cst (TREE_TYPE (t), 0));
10490 if (CAN_HAVE_LOCATION_P (c))
10491 SET_EXPR_LOCATION (c, expr_loc);
10492 if (c == boolean_true_node)
10494 warning_at (expr_loc, 0, "%<num_teams%> value must be positive");
10495 t = integer_one_node;
10498 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TEAMS, "num_teams");
10500 c = build_omp_clause (num_teams_loc, OMP_CLAUSE_NUM_TEAMS);
10501 OMP_CLAUSE_NUM_TEAMS_EXPR (c) = t;
10502 OMP_CLAUSE_CHAIN (c) = list;
10503 list = c;
10506 return list;
10509 /* OpenMP 4.0:
10510 thread_limit ( expression ) */
10512 static tree
10513 c_parser_omp_clause_thread_limit (c_parser *parser, tree list)
10515 location_t num_thread_limit_loc = c_parser_peek_token (parser)->location;
10516 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10518 location_t expr_loc = c_parser_peek_token (parser)->location;
10519 tree c, t = c_parser_expression (parser).value;
10520 mark_exp_read (t);
10521 t = c_fully_fold (t, false, NULL);
10523 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10525 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
10527 c_parser_error (parser, "expected integer expression");
10528 return list;
10531 /* Attempt to statically determine when the number isn't positive. */
10532 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
10533 build_int_cst (TREE_TYPE (t), 0));
10534 if (CAN_HAVE_LOCATION_P (c))
10535 SET_EXPR_LOCATION (c, expr_loc);
10536 if (c == boolean_true_node)
10538 warning_at (expr_loc, 0, "%<thread_limit%> value must be positive");
10539 t = integer_one_node;
10542 check_no_duplicate_clause (list, OMP_CLAUSE_THREAD_LIMIT,
10543 "thread_limit");
10545 c = build_omp_clause (num_thread_limit_loc, OMP_CLAUSE_THREAD_LIMIT);
10546 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
10547 OMP_CLAUSE_CHAIN (c) = list;
10548 list = c;
10551 return list;
10554 /* OpenMP 4.0:
10555 aligned ( variable-list )
10556 aligned ( variable-list : constant-expression ) */
10558 static tree
10559 c_parser_omp_clause_aligned (c_parser *parser, tree list)
10561 location_t clause_loc = c_parser_peek_token (parser)->location;
10562 tree nl, c;
10564 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10565 return list;
10567 nl = c_parser_omp_variable_list (parser, clause_loc,
10568 OMP_CLAUSE_ALIGNED, list);
10570 if (c_parser_next_token_is (parser, CPP_COLON))
10572 c_parser_consume_token (parser);
10573 tree alignment = c_parser_expr_no_commas (parser, NULL).value;
10574 mark_exp_read (alignment);
10575 alignment = c_fully_fold (alignment, false, NULL);
10576 if (!INTEGRAL_TYPE_P (TREE_TYPE (alignment))
10577 && TREE_CODE (alignment) != INTEGER_CST
10578 && tree_int_cst_sgn (alignment) != 1)
10580 error_at (clause_loc, "%<aligned%> clause alignment expression must "
10581 "be positive constant integer expression");
10582 alignment = NULL_TREE;
10585 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
10586 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = alignment;
10589 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10590 return nl;
10593 /* OpenMP 4.0:
10594 linear ( variable-list )
10595 linear ( variable-list : expression ) */
10597 static tree
10598 c_parser_omp_clause_linear (c_parser *parser, tree list, bool is_cilk_simd_fn)
10600 location_t clause_loc = c_parser_peek_token (parser)->location;
10601 tree nl, c, step;
10603 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10604 return list;
10606 nl = c_parser_omp_variable_list (parser, clause_loc,
10607 OMP_CLAUSE_LINEAR, list);
10609 if (c_parser_next_token_is (parser, CPP_COLON))
10611 c_parser_consume_token (parser);
10612 step = c_parser_expression (parser).value;
10613 mark_exp_read (step);
10614 step = c_fully_fold (step, false, NULL);
10615 if (is_cilk_simd_fn && TREE_CODE (step) == PARM_DECL)
10617 sorry ("using parameters for %<linear%> step is not supported yet");
10618 step = integer_one_node;
10620 if (!INTEGRAL_TYPE_P (TREE_TYPE (step)))
10622 error_at (clause_loc, "%<linear%> clause step expression must "
10623 "be integral");
10624 step = integer_one_node;
10628 else
10629 step = integer_one_node;
10631 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
10633 OMP_CLAUSE_LINEAR_STEP (c) = step;
10636 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10637 return nl;
10640 /* OpenMP 4.0:
10641 safelen ( constant-expression ) */
10643 static tree
10644 c_parser_omp_clause_safelen (c_parser *parser, tree list)
10646 location_t clause_loc = c_parser_peek_token (parser)->location;
10647 tree c, t;
10649 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10650 return list;
10652 t = c_parser_expr_no_commas (parser, NULL).value;
10653 mark_exp_read (t);
10654 t = c_fully_fold (t, false, NULL);
10655 if (!INTEGRAL_TYPE_P (TREE_TYPE (t))
10656 && TREE_CODE (t) != INTEGER_CST
10657 && tree_int_cst_sgn (t) != 1)
10659 error_at (clause_loc, "%<safelen%> clause expression must "
10660 "be positive constant integer expression");
10661 t = NULL_TREE;
10664 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10665 if (t == NULL_TREE || t == error_mark_node)
10666 return list;
10668 check_no_duplicate_clause (list, OMP_CLAUSE_SAFELEN, "safelen");
10670 c = build_omp_clause (clause_loc, OMP_CLAUSE_SAFELEN);
10671 OMP_CLAUSE_SAFELEN_EXPR (c) = t;
10672 OMP_CLAUSE_CHAIN (c) = list;
10673 return c;
10676 /* OpenMP 4.0:
10677 simdlen ( constant-expression ) */
10679 static tree
10680 c_parser_omp_clause_simdlen (c_parser *parser, tree list)
10682 location_t clause_loc = c_parser_peek_token (parser)->location;
10683 tree c, t;
10685 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10686 return list;
10688 t = c_parser_expr_no_commas (parser, NULL).value;
10689 mark_exp_read (t);
10690 t = c_fully_fold (t, false, NULL);
10691 if (!INTEGRAL_TYPE_P (TREE_TYPE (t))
10692 && TREE_CODE (t) != INTEGER_CST
10693 && tree_int_cst_sgn (t) != 1)
10695 error_at (clause_loc, "%<simdlen%> clause expression must "
10696 "be positive constant integer expression");
10697 t = NULL_TREE;
10700 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10701 if (t == NULL_TREE || t == error_mark_node)
10702 return list;
10704 check_no_duplicate_clause (list, OMP_CLAUSE_SIMDLEN, "simdlen");
10706 c = build_omp_clause (clause_loc, OMP_CLAUSE_SIMDLEN);
10707 OMP_CLAUSE_SIMDLEN_EXPR (c) = t;
10708 OMP_CLAUSE_CHAIN (c) = list;
10709 return c;
10712 /* OpenMP 4.0:
10713 depend ( depend-kind: variable-list )
10715 depend-kind:
10716 in | out | inout */
10718 static tree
10719 c_parser_omp_clause_depend (c_parser *parser, tree list)
10721 location_t clause_loc = c_parser_peek_token (parser)->location;
10722 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_INOUT;
10723 tree nl, c;
10725 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10726 return list;
10728 if (c_parser_next_token_is (parser, CPP_NAME))
10730 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
10731 if (strcmp ("in", p) == 0)
10732 kind = OMP_CLAUSE_DEPEND_IN;
10733 else if (strcmp ("inout", p) == 0)
10734 kind = OMP_CLAUSE_DEPEND_INOUT;
10735 else if (strcmp ("out", p) == 0)
10736 kind = OMP_CLAUSE_DEPEND_OUT;
10737 else
10738 goto invalid_kind;
10740 else
10741 goto invalid_kind;
10743 c_parser_consume_token (parser);
10744 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
10745 goto resync_fail;
10747 nl = c_parser_omp_variable_list (parser, clause_loc,
10748 OMP_CLAUSE_DEPEND, list);
10750 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
10751 OMP_CLAUSE_DEPEND_KIND (c) = kind;
10753 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10754 return nl;
10756 invalid_kind:
10757 c_parser_error (parser, "invalid depend kind");
10758 resync_fail:
10759 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10760 return list;
10763 /* OpenMP 4.0:
10764 map ( map-kind: variable-list )
10765 map ( variable-list )
10767 map-kind:
10768 alloc | to | from | tofrom */
10770 static tree
10771 c_parser_omp_clause_map (c_parser *parser, tree list)
10773 location_t clause_loc = c_parser_peek_token (parser)->location;
10774 enum omp_clause_map_kind kind = OMP_CLAUSE_MAP_TOFROM;
10775 tree nl, c;
10777 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10778 return list;
10780 if (c_parser_next_token_is (parser, CPP_NAME)
10781 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
10783 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
10784 if (strcmp ("alloc", p) == 0)
10785 kind = OMP_CLAUSE_MAP_ALLOC;
10786 else if (strcmp ("to", p) == 0)
10787 kind = OMP_CLAUSE_MAP_TO;
10788 else if (strcmp ("from", p) == 0)
10789 kind = OMP_CLAUSE_MAP_FROM;
10790 else if (strcmp ("tofrom", p) == 0)
10791 kind = OMP_CLAUSE_MAP_TOFROM;
10792 else
10794 c_parser_error (parser, "invalid map kind");
10795 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
10796 "expected %<)%>");
10797 return list;
10799 c_parser_consume_token (parser);
10800 c_parser_consume_token (parser);
10803 nl = c_parser_omp_variable_list (parser, clause_loc, OMP_CLAUSE_MAP, list);
10805 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
10806 OMP_CLAUSE_MAP_KIND (c) = kind;
10808 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10809 return nl;
10812 /* OpenMP 4.0:
10813 device ( expression ) */
10815 static tree
10816 c_parser_omp_clause_device (c_parser *parser, tree list)
10818 location_t clause_loc = c_parser_peek_token (parser)->location;
10819 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10821 tree c, t = c_parser_expr_no_commas (parser, NULL).value;
10822 mark_exp_read (t);
10823 t = c_fully_fold (t, false, NULL);
10825 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10827 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
10829 c_parser_error (parser, "expected integer expression");
10830 return list;
10833 check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE, "device");
10835 c = build_omp_clause (clause_loc, OMP_CLAUSE_DEVICE);
10836 OMP_CLAUSE_DEVICE_ID (c) = t;
10837 OMP_CLAUSE_CHAIN (c) = list;
10838 list = c;
10841 return list;
10844 /* OpenMP 4.0:
10845 dist_schedule ( static )
10846 dist_schedule ( static , expression ) */
10848 static tree
10849 c_parser_omp_clause_dist_schedule (c_parser *parser, tree list)
10851 tree c, t = NULL_TREE;
10852 location_t loc = c_parser_peek_token (parser)->location;
10854 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10855 return list;
10857 if (!c_parser_next_token_is_keyword (parser, RID_STATIC))
10859 c_parser_error (parser, "invalid dist_schedule kind");
10860 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
10861 "expected %<)%>");
10862 return list;
10865 c_parser_consume_token (parser);
10866 if (c_parser_next_token_is (parser, CPP_COMMA))
10868 c_parser_consume_token (parser);
10870 t = c_parser_expr_no_commas (parser, NULL).value;
10871 mark_exp_read (t);
10872 t = c_fully_fold (t, false, NULL);
10873 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10875 else
10876 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
10877 "expected %<,%> or %<)%>");
10879 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule");
10880 if (t == error_mark_node)
10881 return list;
10883 c = build_omp_clause (loc, OMP_CLAUSE_DIST_SCHEDULE);
10884 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
10885 OMP_CLAUSE_CHAIN (c) = list;
10886 return c;
10889 /* OpenMP 4.0:
10890 proc_bind ( proc-bind-kind )
10892 proc-bind-kind:
10893 master | close | spread */
10895 static tree
10896 c_parser_omp_clause_proc_bind (c_parser *parser, tree list)
10898 location_t clause_loc = c_parser_peek_token (parser)->location;
10899 enum omp_clause_proc_bind_kind kind;
10900 tree c;
10902 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10903 return list;
10905 if (c_parser_next_token_is (parser, CPP_NAME))
10907 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
10908 if (strcmp ("master", p) == 0)
10909 kind = OMP_CLAUSE_PROC_BIND_MASTER;
10910 else if (strcmp ("close", p) == 0)
10911 kind = OMP_CLAUSE_PROC_BIND_CLOSE;
10912 else if (strcmp ("spread", p) == 0)
10913 kind = OMP_CLAUSE_PROC_BIND_SPREAD;
10914 else
10915 goto invalid_kind;
10917 else
10918 goto invalid_kind;
10920 c_parser_consume_token (parser);
10921 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10922 c = build_omp_clause (clause_loc, OMP_CLAUSE_PROC_BIND);
10923 OMP_CLAUSE_PROC_BIND_KIND (c) = kind;
10924 OMP_CLAUSE_CHAIN (c) = list;
10925 return c;
10927 invalid_kind:
10928 c_parser_error (parser, "invalid proc_bind kind");
10929 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10930 return list;
10933 /* OpenMP 4.0:
10934 to ( variable-list ) */
10936 static tree
10937 c_parser_omp_clause_to (c_parser *parser, tree list)
10939 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_TO, list);
10942 /* OpenMP 4.0:
10943 from ( variable-list ) */
10945 static tree
10946 c_parser_omp_clause_from (c_parser *parser, tree list)
10948 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_FROM, list);
10951 /* OpenMP 4.0:
10952 uniform ( variable-list ) */
10954 static tree
10955 c_parser_omp_clause_uniform (c_parser *parser, tree list)
10957 /* The clauses location. */
10958 location_t loc = c_parser_peek_token (parser)->location;
10960 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10962 list = c_parser_omp_variable_list (parser, loc, OMP_CLAUSE_UNIFORM,
10963 list);
10964 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10966 return list;
10969 /* Parse all OpenMP clauses. The set clauses allowed by the directive
10970 is a bitmask in MASK. Return the list of clauses found; the result
10971 of clause default goes in *pdefault. */
10973 static tree
10974 c_parser_omp_all_clauses (c_parser *parser, omp_clause_mask mask,
10975 const char *where, bool finish_p = true)
10977 tree clauses = NULL;
10978 bool first = true, cilk_simd_fn = false;
10980 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
10982 location_t here;
10983 pragma_omp_clause c_kind;
10984 const char *c_name;
10985 tree prev = clauses;
10987 if (!first && c_parser_next_token_is (parser, CPP_COMMA))
10988 c_parser_consume_token (parser);
10990 here = c_parser_peek_token (parser)->location;
10991 c_kind = c_parser_omp_clause_name (parser);
10993 switch (c_kind)
10995 case PRAGMA_OMP_CLAUSE_COLLAPSE:
10996 clauses = c_parser_omp_clause_collapse (parser, clauses);
10997 c_name = "collapse";
10998 break;
10999 case PRAGMA_OMP_CLAUSE_COPYIN:
11000 clauses = c_parser_omp_clause_copyin (parser, clauses);
11001 c_name = "copyin";
11002 break;
11003 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
11004 clauses = c_parser_omp_clause_copyprivate (parser, clauses);
11005 c_name = "copyprivate";
11006 break;
11007 case PRAGMA_OMP_CLAUSE_DEFAULT:
11008 clauses = c_parser_omp_clause_default (parser, clauses);
11009 c_name = "default";
11010 break;
11011 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
11012 clauses = c_parser_omp_clause_firstprivate (parser, clauses);
11013 c_name = "firstprivate";
11014 break;
11015 case PRAGMA_OMP_CLAUSE_FINAL:
11016 clauses = c_parser_omp_clause_final (parser, clauses);
11017 c_name = "final";
11018 break;
11019 case PRAGMA_OMP_CLAUSE_IF:
11020 clauses = c_parser_omp_clause_if (parser, clauses);
11021 c_name = "if";
11022 break;
11023 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
11024 clauses = c_parser_omp_clause_lastprivate (parser, clauses);
11025 c_name = "lastprivate";
11026 break;
11027 case PRAGMA_OMP_CLAUSE_MERGEABLE:
11028 clauses = c_parser_omp_clause_mergeable (parser, clauses);
11029 c_name = "mergeable";
11030 break;
11031 case PRAGMA_OMP_CLAUSE_NOWAIT:
11032 clauses = c_parser_omp_clause_nowait (parser, clauses);
11033 c_name = "nowait";
11034 break;
11035 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
11036 clauses = c_parser_omp_clause_num_threads (parser, clauses);
11037 c_name = "num_threads";
11038 break;
11039 case PRAGMA_OMP_CLAUSE_ORDERED:
11040 clauses = c_parser_omp_clause_ordered (parser, clauses);
11041 c_name = "ordered";
11042 break;
11043 case PRAGMA_OMP_CLAUSE_PRIVATE:
11044 clauses = c_parser_omp_clause_private (parser, clauses);
11045 c_name = "private";
11046 break;
11047 case PRAGMA_OMP_CLAUSE_REDUCTION:
11048 clauses = c_parser_omp_clause_reduction (parser, clauses);
11049 c_name = "reduction";
11050 break;
11051 case PRAGMA_OMP_CLAUSE_SCHEDULE:
11052 clauses = c_parser_omp_clause_schedule (parser, clauses);
11053 c_name = "schedule";
11054 break;
11055 case PRAGMA_OMP_CLAUSE_SHARED:
11056 clauses = c_parser_omp_clause_shared (parser, clauses);
11057 c_name = "shared";
11058 break;
11059 case PRAGMA_OMP_CLAUSE_UNTIED:
11060 clauses = c_parser_omp_clause_untied (parser, clauses);
11061 c_name = "untied";
11062 break;
11063 case PRAGMA_OMP_CLAUSE_INBRANCH:
11064 case PRAGMA_CILK_CLAUSE_MASK:
11065 clauses = c_parser_omp_clause_branch (parser, OMP_CLAUSE_INBRANCH,
11066 clauses);
11067 c_name = "inbranch";
11068 break;
11069 case PRAGMA_OMP_CLAUSE_NOTINBRANCH:
11070 case PRAGMA_CILK_CLAUSE_NOMASK:
11071 clauses = c_parser_omp_clause_branch (parser, OMP_CLAUSE_NOTINBRANCH,
11072 clauses);
11073 c_name = "notinbranch";
11074 break;
11075 case PRAGMA_OMP_CLAUSE_PARALLEL:
11076 clauses
11077 = c_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_PARALLEL,
11078 clauses);
11079 c_name = "parallel";
11080 if (!first)
11082 clause_not_first:
11083 error_at (here, "%qs must be the first clause of %qs",
11084 c_name, where);
11085 clauses = prev;
11087 break;
11088 case PRAGMA_OMP_CLAUSE_FOR:
11089 clauses
11090 = c_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_FOR,
11091 clauses);
11092 c_name = "for";
11093 if (!first)
11094 goto clause_not_first;
11095 break;
11096 case PRAGMA_OMP_CLAUSE_SECTIONS:
11097 clauses
11098 = c_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_SECTIONS,
11099 clauses);
11100 c_name = "sections";
11101 if (!first)
11102 goto clause_not_first;
11103 break;
11104 case PRAGMA_OMP_CLAUSE_TASKGROUP:
11105 clauses
11106 = c_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_TASKGROUP,
11107 clauses);
11108 c_name = "taskgroup";
11109 if (!first)
11110 goto clause_not_first;
11111 break;
11112 case PRAGMA_OMP_CLAUSE_TO:
11113 clauses = c_parser_omp_clause_to (parser, clauses);
11114 c_name = "to";
11115 break;
11116 case PRAGMA_OMP_CLAUSE_FROM:
11117 clauses = c_parser_omp_clause_from (parser, clauses);
11118 c_name = "from";
11119 break;
11120 case PRAGMA_OMP_CLAUSE_UNIFORM:
11121 clauses = c_parser_omp_clause_uniform (parser, clauses);
11122 c_name = "uniform";
11123 break;
11124 case PRAGMA_OMP_CLAUSE_NUM_TEAMS:
11125 clauses = c_parser_omp_clause_num_teams (parser, clauses);
11126 c_name = "num_teams";
11127 break;
11128 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT:
11129 clauses = c_parser_omp_clause_thread_limit (parser, clauses);
11130 c_name = "thread_limit";
11131 break;
11132 case PRAGMA_OMP_CLAUSE_ALIGNED:
11133 clauses = c_parser_omp_clause_aligned (parser, clauses);
11134 c_name = "aligned";
11135 break;
11136 case PRAGMA_OMP_CLAUSE_LINEAR:
11137 if (((mask >> PRAGMA_CILK_CLAUSE_VECTORLENGTH) & 1) != 0)
11138 cilk_simd_fn = true;
11139 clauses = c_parser_omp_clause_linear (parser, clauses, cilk_simd_fn);
11140 c_name = "linear";
11141 break;
11142 case PRAGMA_OMP_CLAUSE_DEPEND:
11143 clauses = c_parser_omp_clause_depend (parser, clauses);
11144 c_name = "depend";
11145 break;
11146 case PRAGMA_OMP_CLAUSE_MAP:
11147 clauses = c_parser_omp_clause_map (parser, clauses);
11148 c_name = "map";
11149 break;
11150 case PRAGMA_OMP_CLAUSE_DEVICE:
11151 clauses = c_parser_omp_clause_device (parser, clauses);
11152 c_name = "device";
11153 break;
11154 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE:
11155 clauses = c_parser_omp_clause_dist_schedule (parser, clauses);
11156 c_name = "dist_schedule";
11157 break;
11158 case PRAGMA_OMP_CLAUSE_PROC_BIND:
11159 clauses = c_parser_omp_clause_proc_bind (parser, clauses);
11160 c_name = "proc_bind";
11161 break;
11162 case PRAGMA_OMP_CLAUSE_SAFELEN:
11163 clauses = c_parser_omp_clause_safelen (parser, clauses);
11164 c_name = "safelen";
11165 break;
11166 case PRAGMA_CILK_CLAUSE_VECTORLENGTH:
11167 clauses = c_parser_cilk_clause_vectorlength (parser, clauses, true);
11168 c_name = "simdlen";
11169 break;
11170 case PRAGMA_OMP_CLAUSE_SIMDLEN:
11171 clauses = c_parser_omp_clause_simdlen (parser, clauses);
11172 c_name = "simdlen";
11173 break;
11174 default:
11175 c_parser_error (parser, "expected %<#pragma omp%> clause");
11176 goto saw_error;
11179 first = false;
11181 if (((mask >> c_kind) & 1) == 0 && !parser->error)
11183 /* Remove the invalid clause(s) from the list to avoid
11184 confusing the rest of the compiler. */
11185 clauses = prev;
11186 error_at (here, "%qs is not valid for %qs", c_name, where);
11190 saw_error:
11191 c_parser_skip_to_pragma_eol (parser);
11193 if (finish_p)
11194 return c_finish_omp_clauses (clauses);
11196 return clauses;
11199 /* OpenMP 2.5:
11200 structured-block:
11201 statement
11203 In practice, we're also interested in adding the statement to an
11204 outer node. So it is convenient if we work around the fact that
11205 c_parser_statement calls add_stmt. */
11207 static tree
11208 c_parser_omp_structured_block (c_parser *parser)
11210 tree stmt = push_stmt_list ();
11211 c_parser_statement (parser);
11212 return pop_stmt_list (stmt);
11215 /* OpenMP 2.5:
11216 # pragma omp atomic new-line
11217 expression-stmt
11219 expression-stmt:
11220 x binop= expr | x++ | ++x | x-- | --x
11221 binop:
11222 +, *, -, /, &, ^, |, <<, >>
11224 where x is an lvalue expression with scalar type.
11226 OpenMP 3.1:
11227 # pragma omp atomic new-line
11228 update-stmt
11230 # pragma omp atomic read new-line
11231 read-stmt
11233 # pragma omp atomic write new-line
11234 write-stmt
11236 # pragma omp atomic update new-line
11237 update-stmt
11239 # pragma omp atomic capture new-line
11240 capture-stmt
11242 # pragma omp atomic capture new-line
11243 capture-block
11245 read-stmt:
11246 v = x
11247 write-stmt:
11248 x = expr
11249 update-stmt:
11250 expression-stmt | x = x binop expr
11251 capture-stmt:
11252 v = expression-stmt
11253 capture-block:
11254 { v = x; update-stmt; } | { update-stmt; v = x; }
11256 OpenMP 4.0:
11257 update-stmt:
11258 expression-stmt | x = x binop expr | x = expr binop x
11259 capture-stmt:
11260 v = update-stmt
11261 capture-block:
11262 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
11264 where x and v are lvalue expressions with scalar type.
11266 LOC is the location of the #pragma token. */
11268 static void
11269 c_parser_omp_atomic (location_t loc, c_parser *parser)
11271 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE;
11272 tree lhs1 = NULL_TREE, rhs1 = NULL_TREE;
11273 tree stmt, orig_lhs, unfolded_lhs = NULL_TREE, unfolded_lhs1 = NULL_TREE;
11274 enum tree_code code = OMP_ATOMIC, opcode = NOP_EXPR;
11275 struct c_expr expr;
11276 location_t eloc;
11277 bool structured_block = false;
11278 bool swapped = false;
11279 bool seq_cst = false;
11281 if (c_parser_next_token_is (parser, CPP_NAME))
11283 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
11284 if (!strcmp (p, "seq_cst"))
11286 seq_cst = true;
11287 c_parser_consume_token (parser);
11288 if (c_parser_next_token_is (parser, CPP_COMMA)
11289 && c_parser_peek_2nd_token (parser)->type == CPP_NAME)
11290 c_parser_consume_token (parser);
11293 if (c_parser_next_token_is (parser, CPP_NAME))
11295 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
11297 if (!strcmp (p, "read"))
11298 code = OMP_ATOMIC_READ;
11299 else if (!strcmp (p, "write"))
11300 code = NOP_EXPR;
11301 else if (!strcmp (p, "update"))
11302 code = OMP_ATOMIC;
11303 else if (!strcmp (p, "capture"))
11304 code = OMP_ATOMIC_CAPTURE_NEW;
11305 else
11306 p = NULL;
11307 if (p)
11308 c_parser_consume_token (parser);
11310 if (!seq_cst)
11312 if (c_parser_next_token_is (parser, CPP_COMMA)
11313 && c_parser_peek_2nd_token (parser)->type == CPP_NAME)
11314 c_parser_consume_token (parser);
11316 if (c_parser_next_token_is (parser, CPP_NAME))
11318 const char *p
11319 = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
11320 if (!strcmp (p, "seq_cst"))
11322 seq_cst = true;
11323 c_parser_consume_token (parser);
11327 c_parser_skip_to_pragma_eol (parser);
11329 switch (code)
11331 case OMP_ATOMIC_READ:
11332 case NOP_EXPR: /* atomic write */
11333 v = c_parser_unary_expression (parser).value;
11334 v = c_fully_fold (v, false, NULL);
11335 if (v == error_mark_node)
11336 goto saw_error;
11337 loc = c_parser_peek_token (parser)->location;
11338 if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
11339 goto saw_error;
11340 if (code == NOP_EXPR)
11341 lhs = c_parser_expression (parser).value;
11342 else
11343 lhs = c_parser_unary_expression (parser).value;
11344 lhs = c_fully_fold (lhs, false, NULL);
11345 if (lhs == error_mark_node)
11346 goto saw_error;
11347 if (code == NOP_EXPR)
11349 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
11350 opcode. */
11351 code = OMP_ATOMIC;
11352 rhs = lhs;
11353 lhs = v;
11354 v = NULL_TREE;
11356 goto done;
11357 case OMP_ATOMIC_CAPTURE_NEW:
11358 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
11360 c_parser_consume_token (parser);
11361 structured_block = true;
11363 else
11365 v = c_parser_unary_expression (parser).value;
11366 v = c_fully_fold (v, false, NULL);
11367 if (v == error_mark_node)
11368 goto saw_error;
11369 if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
11370 goto saw_error;
11372 break;
11373 default:
11374 break;
11377 /* For structured_block case we don't know yet whether
11378 old or new x should be captured. */
11379 restart:
11380 eloc = c_parser_peek_token (parser)->location;
11381 expr = c_parser_unary_expression (parser);
11382 lhs = expr.value;
11383 expr = default_function_array_conversion (eloc, expr);
11384 unfolded_lhs = expr.value;
11385 lhs = c_fully_fold (lhs, false, NULL);
11386 orig_lhs = lhs;
11387 switch (TREE_CODE (lhs))
11389 case ERROR_MARK:
11390 saw_error:
11391 c_parser_skip_to_end_of_block_or_statement (parser);
11392 if (structured_block)
11394 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
11395 c_parser_consume_token (parser);
11396 else if (code == OMP_ATOMIC_CAPTURE_NEW)
11398 c_parser_skip_to_end_of_block_or_statement (parser);
11399 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
11400 c_parser_consume_token (parser);
11403 return;
11405 case POSTINCREMENT_EXPR:
11406 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
11407 code = OMP_ATOMIC_CAPTURE_OLD;
11408 /* FALLTHROUGH */
11409 case PREINCREMENT_EXPR:
11410 lhs = TREE_OPERAND (lhs, 0);
11411 unfolded_lhs = NULL_TREE;
11412 opcode = PLUS_EXPR;
11413 rhs = integer_one_node;
11414 break;
11416 case POSTDECREMENT_EXPR:
11417 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
11418 code = OMP_ATOMIC_CAPTURE_OLD;
11419 /* FALLTHROUGH */
11420 case PREDECREMENT_EXPR:
11421 lhs = TREE_OPERAND (lhs, 0);
11422 unfolded_lhs = NULL_TREE;
11423 opcode = MINUS_EXPR;
11424 rhs = integer_one_node;
11425 break;
11427 case COMPOUND_EXPR:
11428 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
11429 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
11430 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
11431 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
11432 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
11433 (TREE_OPERAND (lhs, 1), 0), 0)))
11434 == BOOLEAN_TYPE)
11435 /* Undo effects of boolean_increment for post {in,de}crement. */
11436 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
11437 /* FALLTHRU */
11438 case MODIFY_EXPR:
11439 if (TREE_CODE (lhs) == MODIFY_EXPR
11440 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
11442 /* Undo effects of boolean_increment. */
11443 if (integer_onep (TREE_OPERAND (lhs, 1)))
11445 /* This is pre or post increment. */
11446 rhs = TREE_OPERAND (lhs, 1);
11447 lhs = TREE_OPERAND (lhs, 0);
11448 unfolded_lhs = NULL_TREE;
11449 opcode = NOP_EXPR;
11450 if (code == OMP_ATOMIC_CAPTURE_NEW
11451 && !structured_block
11452 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
11453 code = OMP_ATOMIC_CAPTURE_OLD;
11454 break;
11456 if (TREE_CODE (TREE_OPERAND (lhs, 1)) == TRUTH_NOT_EXPR
11457 && TREE_OPERAND (lhs, 0)
11458 == TREE_OPERAND (TREE_OPERAND (lhs, 1), 0))
11460 /* This is pre or post decrement. */
11461 rhs = TREE_OPERAND (lhs, 1);
11462 lhs = TREE_OPERAND (lhs, 0);
11463 unfolded_lhs = NULL_TREE;
11464 opcode = NOP_EXPR;
11465 if (code == OMP_ATOMIC_CAPTURE_NEW
11466 && !structured_block
11467 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
11468 code = OMP_ATOMIC_CAPTURE_OLD;
11469 break;
11472 /* FALLTHRU */
11473 default:
11474 switch (c_parser_peek_token (parser)->type)
11476 case CPP_MULT_EQ:
11477 opcode = MULT_EXPR;
11478 break;
11479 case CPP_DIV_EQ:
11480 opcode = TRUNC_DIV_EXPR;
11481 break;
11482 case CPP_PLUS_EQ:
11483 opcode = PLUS_EXPR;
11484 break;
11485 case CPP_MINUS_EQ:
11486 opcode = MINUS_EXPR;
11487 break;
11488 case CPP_LSHIFT_EQ:
11489 opcode = LSHIFT_EXPR;
11490 break;
11491 case CPP_RSHIFT_EQ:
11492 opcode = RSHIFT_EXPR;
11493 break;
11494 case CPP_AND_EQ:
11495 opcode = BIT_AND_EXPR;
11496 break;
11497 case CPP_OR_EQ:
11498 opcode = BIT_IOR_EXPR;
11499 break;
11500 case CPP_XOR_EQ:
11501 opcode = BIT_XOR_EXPR;
11502 break;
11503 case CPP_EQ:
11504 c_parser_consume_token (parser);
11505 eloc = c_parser_peek_token (parser)->location;
11506 expr = c_parser_expr_no_commas (parser, NULL, unfolded_lhs);
11507 rhs1 = expr.value;
11508 switch (TREE_CODE (rhs1))
11510 case MULT_EXPR:
11511 case TRUNC_DIV_EXPR:
11512 case PLUS_EXPR:
11513 case MINUS_EXPR:
11514 case LSHIFT_EXPR:
11515 case RSHIFT_EXPR:
11516 case BIT_AND_EXPR:
11517 case BIT_IOR_EXPR:
11518 case BIT_XOR_EXPR:
11519 if (c_tree_equal (TREE_OPERAND (rhs1, 0), unfolded_lhs))
11521 opcode = TREE_CODE (rhs1);
11522 rhs = c_fully_fold (TREE_OPERAND (rhs1, 1), false, NULL);
11523 rhs1 = c_fully_fold (TREE_OPERAND (rhs1, 0), false, NULL);
11524 goto stmt_done;
11526 if (c_tree_equal (TREE_OPERAND (rhs1, 1), unfolded_lhs))
11528 opcode = TREE_CODE (rhs1);
11529 rhs = c_fully_fold (TREE_OPERAND (rhs1, 0), false, NULL);
11530 rhs1 = c_fully_fold (TREE_OPERAND (rhs1, 1), false, NULL);
11531 swapped = !commutative_tree_code (opcode);
11532 goto stmt_done;
11534 break;
11535 case ERROR_MARK:
11536 goto saw_error;
11537 default:
11538 break;
11540 if (c_parser_peek_token (parser)->type == CPP_SEMICOLON)
11542 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
11544 code = OMP_ATOMIC_CAPTURE_OLD;
11545 v = lhs;
11546 lhs = NULL_TREE;
11547 expr = default_function_array_read_conversion (eloc, expr);
11548 unfolded_lhs1 = expr.value;
11549 lhs1 = c_fully_fold (unfolded_lhs1, false, NULL);
11550 rhs1 = NULL_TREE;
11551 c_parser_consume_token (parser);
11552 goto restart;
11554 if (structured_block)
11556 opcode = NOP_EXPR;
11557 expr = default_function_array_read_conversion (eloc, expr);
11558 rhs = c_fully_fold (expr.value, false, NULL);
11559 rhs1 = NULL_TREE;
11560 goto stmt_done;
11563 c_parser_error (parser, "invalid form of %<#pragma omp atomic%>");
11564 goto saw_error;
11565 default:
11566 c_parser_error (parser,
11567 "invalid operator for %<#pragma omp atomic%>");
11568 goto saw_error;
11571 /* Arrange to pass the location of the assignment operator to
11572 c_finish_omp_atomic. */
11573 loc = c_parser_peek_token (parser)->location;
11574 c_parser_consume_token (parser);
11575 eloc = c_parser_peek_token (parser)->location;
11576 expr = c_parser_expression (parser);
11577 expr = default_function_array_read_conversion (eloc, expr);
11578 rhs = expr.value;
11579 rhs = c_fully_fold (rhs, false, NULL);
11580 break;
11582 stmt_done:
11583 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
11585 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
11586 goto saw_error;
11587 v = c_parser_unary_expression (parser).value;
11588 v = c_fully_fold (v, false, NULL);
11589 if (v == error_mark_node)
11590 goto saw_error;
11591 if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
11592 goto saw_error;
11593 eloc = c_parser_peek_token (parser)->location;
11594 expr = c_parser_unary_expression (parser);
11595 lhs1 = expr.value;
11596 expr = default_function_array_read_conversion (eloc, expr);
11597 unfolded_lhs1 = expr.value;
11598 lhs1 = c_fully_fold (lhs1, false, NULL);
11599 if (lhs1 == error_mark_node)
11600 goto saw_error;
11602 if (structured_block)
11604 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
11605 c_parser_require (parser, CPP_CLOSE_BRACE, "expected %<}%>");
11607 done:
11608 if (unfolded_lhs && unfolded_lhs1
11609 && !c_tree_equal (unfolded_lhs, unfolded_lhs1))
11611 error ("%<#pragma omp atomic capture%> uses two different "
11612 "expressions for memory");
11613 stmt = error_mark_node;
11615 else
11616 stmt = c_finish_omp_atomic (loc, code, opcode, lhs, rhs, v, lhs1, rhs1,
11617 swapped, seq_cst);
11618 if (stmt != error_mark_node)
11619 add_stmt (stmt);
11621 if (!structured_block)
11622 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
11626 /* OpenMP 2.5:
11627 # pragma omp barrier new-line
11630 static void
11631 c_parser_omp_barrier (c_parser *parser)
11633 location_t loc = c_parser_peek_token (parser)->location;
11634 c_parser_consume_pragma (parser);
11635 c_parser_skip_to_pragma_eol (parser);
11637 c_finish_omp_barrier (loc);
11640 /* OpenMP 2.5:
11641 # pragma omp critical [(name)] new-line
11642 structured-block
11644 LOC is the location of the #pragma itself. */
11646 static tree
11647 c_parser_omp_critical (location_t loc, c_parser *parser)
11649 tree stmt, name = NULL;
11651 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
11653 c_parser_consume_token (parser);
11654 if (c_parser_next_token_is (parser, CPP_NAME))
11656 name = c_parser_peek_token (parser)->value;
11657 c_parser_consume_token (parser);
11658 c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11660 else
11661 c_parser_error (parser, "expected identifier");
11663 else if (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
11664 c_parser_error (parser, "expected %<(%> or end of line");
11665 c_parser_skip_to_pragma_eol (parser);
11667 stmt = c_parser_omp_structured_block (parser);
11668 return c_finish_omp_critical (loc, stmt, name);
11671 /* OpenMP 2.5:
11672 # pragma omp flush flush-vars[opt] new-line
11674 flush-vars:
11675 ( variable-list ) */
11677 static void
11678 c_parser_omp_flush (c_parser *parser)
11680 location_t loc = c_parser_peek_token (parser)->location;
11681 c_parser_consume_pragma (parser);
11682 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
11683 c_parser_omp_var_list_parens (parser, OMP_CLAUSE_ERROR, NULL);
11684 else if (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
11685 c_parser_error (parser, "expected %<(%> or end of line");
11686 c_parser_skip_to_pragma_eol (parser);
11688 c_finish_omp_flush (loc);
11691 /* Parse the restricted form of the for statement allowed by OpenMP.
11692 The real trick here is to determine the loop control variable early
11693 so that we can push a new decl if necessary to make it private.
11694 LOC is the location of the OMP in "#pragma omp". */
11696 static tree
11697 c_parser_omp_for_loop (location_t loc, c_parser *parser, enum tree_code code,
11698 tree clauses, tree *cclauses)
11700 tree decl, cond, incr, save_break, save_cont, body, init, stmt, cl;
11701 tree declv, condv, incrv, initv, ret = NULL;
11702 bool fail = false, open_brace_parsed = false;
11703 int i, collapse = 1, nbraces = 0;
11704 location_t for_loc;
11705 vec<tree, va_gc> *for_block = make_tree_vector ();
11707 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
11708 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
11709 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl));
11711 gcc_assert (collapse >= 1);
11713 declv = make_tree_vec (collapse);
11714 initv = make_tree_vec (collapse);
11715 condv = make_tree_vec (collapse);
11716 incrv = make_tree_vec (collapse);
11718 if (!c_parser_next_token_is_keyword (parser, RID_FOR))
11720 c_parser_error (parser, "for statement expected");
11721 return NULL;
11723 for_loc = c_parser_peek_token (parser)->location;
11724 c_parser_consume_token (parser);
11726 for (i = 0; i < collapse; i++)
11728 int bracecount = 0;
11730 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11731 goto pop_scopes;
11733 /* Parse the initialization declaration or expression. */
11734 if (c_parser_next_tokens_start_declaration (parser))
11736 if (i > 0)
11737 vec_safe_push (for_block, c_begin_compound_stmt (true));
11738 c_parser_declaration_or_fndef (parser, true, true, true, true, true,
11739 NULL, vNULL);
11740 decl = check_for_loop_decls (for_loc, flag_isoc99);
11741 if (decl == NULL)
11742 goto error_init;
11743 if (DECL_INITIAL (decl) == error_mark_node)
11744 decl = error_mark_node;
11745 init = decl;
11747 else if (c_parser_next_token_is (parser, CPP_NAME)
11748 && c_parser_peek_2nd_token (parser)->type == CPP_EQ)
11750 struct c_expr decl_exp;
11751 struct c_expr init_exp;
11752 location_t init_loc;
11754 decl_exp = c_parser_postfix_expression (parser);
11755 decl = decl_exp.value;
11757 c_parser_require (parser, CPP_EQ, "expected %<=%>");
11759 init_loc = c_parser_peek_token (parser)->location;
11760 init_exp = c_parser_expr_no_commas (parser, NULL);
11761 init_exp = default_function_array_read_conversion (init_loc,
11762 init_exp);
11763 init = build_modify_expr (init_loc, decl, decl_exp.original_type,
11764 NOP_EXPR, init_loc, init_exp.value,
11765 init_exp.original_type);
11766 init = c_process_expr_stmt (init_loc, init);
11768 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
11770 else
11772 error_init:
11773 c_parser_error (parser,
11774 "expected iteration declaration or initialization");
11775 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
11776 "expected %<)%>");
11777 fail = true;
11778 goto parse_next;
11781 /* Parse the loop condition. */
11782 cond = NULL_TREE;
11783 if (c_parser_next_token_is_not (parser, CPP_SEMICOLON))
11785 location_t cond_loc = c_parser_peek_token (parser)->location;
11786 struct c_expr cond_expr
11787 = c_parser_binary_expression (parser, NULL, NULL_TREE);
11789 cond = cond_expr.value;
11790 cond = c_objc_common_truthvalue_conversion (cond_loc, cond);
11791 cond = c_fully_fold (cond, false, NULL);
11792 switch (cond_expr.original_code)
11794 case GT_EXPR:
11795 case GE_EXPR:
11796 case LT_EXPR:
11797 case LE_EXPR:
11798 break;
11799 case NE_EXPR:
11800 if (code == CILK_SIMD)
11801 break;
11802 /* FALLTHRU. */
11803 default:
11804 /* Can't be cond = error_mark_node, because we want to preserve
11805 the location until c_finish_omp_for. */
11806 cond = build1 (NOP_EXPR, boolean_type_node, error_mark_node);
11807 break;
11809 protected_set_expr_location (cond, cond_loc);
11811 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
11813 /* Parse the increment expression. */
11814 incr = NULL_TREE;
11815 if (c_parser_next_token_is_not (parser, CPP_CLOSE_PAREN))
11817 location_t incr_loc = c_parser_peek_token (parser)->location;
11819 incr = c_process_expr_stmt (incr_loc,
11820 c_parser_expression (parser).value);
11822 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11824 if (decl == NULL || decl == error_mark_node || init == error_mark_node)
11825 fail = true;
11826 else
11828 TREE_VEC_ELT (declv, i) = decl;
11829 TREE_VEC_ELT (initv, i) = init;
11830 TREE_VEC_ELT (condv, i) = cond;
11831 TREE_VEC_ELT (incrv, i) = incr;
11834 parse_next:
11835 if (i == collapse - 1)
11836 break;
11838 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
11839 in between the collapsed for loops to be still considered perfectly
11840 nested. Hopefully the final version clarifies this.
11841 For now handle (multiple) {'s and empty statements. */
11844 if (c_parser_next_token_is_keyword (parser, RID_FOR))
11846 c_parser_consume_token (parser);
11847 break;
11849 else if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
11851 c_parser_consume_token (parser);
11852 bracecount++;
11854 else if (bracecount
11855 && c_parser_next_token_is (parser, CPP_SEMICOLON))
11856 c_parser_consume_token (parser);
11857 else
11859 c_parser_error (parser, "not enough perfectly nested loops");
11860 if (bracecount)
11862 open_brace_parsed = true;
11863 bracecount--;
11865 fail = true;
11866 collapse = 0;
11867 break;
11870 while (1);
11872 nbraces += bracecount;
11875 save_break = c_break_label;
11876 if (code == CILK_SIMD)
11877 c_break_label = build_int_cst (size_type_node, 2);
11878 else
11879 c_break_label = size_one_node;
11880 save_cont = c_cont_label;
11881 c_cont_label = NULL_TREE;
11882 body = push_stmt_list ();
11884 if (open_brace_parsed)
11886 location_t here = c_parser_peek_token (parser)->location;
11887 stmt = c_begin_compound_stmt (true);
11888 c_parser_compound_statement_nostart (parser);
11889 add_stmt (c_end_compound_stmt (here, stmt, true));
11891 else
11892 add_stmt (c_parser_c99_block_statement (parser));
11893 if (c_cont_label)
11895 tree t = build1 (LABEL_EXPR, void_type_node, c_cont_label);
11896 SET_EXPR_LOCATION (t, loc);
11897 add_stmt (t);
11900 body = pop_stmt_list (body);
11901 c_break_label = save_break;
11902 c_cont_label = save_cont;
11904 while (nbraces)
11906 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
11908 c_parser_consume_token (parser);
11909 nbraces--;
11911 else if (c_parser_next_token_is (parser, CPP_SEMICOLON))
11912 c_parser_consume_token (parser);
11913 else
11915 c_parser_error (parser, "collapsed loops not perfectly nested");
11916 while (nbraces)
11918 location_t here = c_parser_peek_token (parser)->location;
11919 stmt = c_begin_compound_stmt (true);
11920 add_stmt (body);
11921 c_parser_compound_statement_nostart (parser);
11922 body = c_end_compound_stmt (here, stmt, true);
11923 nbraces--;
11925 goto pop_scopes;
11929 /* Only bother calling c_finish_omp_for if we haven't already generated
11930 an error from the initialization parsing. */
11931 if (!fail)
11933 stmt = c_finish_omp_for (loc, code, declv, initv, condv,
11934 incrv, body, NULL);
11935 if (stmt)
11937 if (cclauses != NULL
11938 && cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL] != NULL)
11940 tree *c;
11941 for (c = &cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL]; *c ; )
11942 if (OMP_CLAUSE_CODE (*c) != OMP_CLAUSE_FIRSTPRIVATE
11943 && OMP_CLAUSE_CODE (*c) != OMP_CLAUSE_LASTPRIVATE)
11944 c = &OMP_CLAUSE_CHAIN (*c);
11945 else
11947 for (i = 0; i < collapse; i++)
11948 if (TREE_VEC_ELT (declv, i) == OMP_CLAUSE_DECL (*c))
11949 break;
11950 if (i == collapse)
11951 c = &OMP_CLAUSE_CHAIN (*c);
11952 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE)
11954 error_at (loc,
11955 "iteration variable %qD should not be firstprivate",
11956 OMP_CLAUSE_DECL (*c));
11957 *c = OMP_CLAUSE_CHAIN (*c);
11959 else
11961 /* Copy lastprivate (decl) clause to OMP_FOR_CLAUSES,
11962 change it to shared (decl) in
11963 OMP_PARALLEL_CLAUSES. */
11964 tree l = build_omp_clause (OMP_CLAUSE_LOCATION (*c),
11965 OMP_CLAUSE_LASTPRIVATE);
11966 OMP_CLAUSE_DECL (l) = OMP_CLAUSE_DECL (*c);
11967 if (code == OMP_SIMD)
11969 OMP_CLAUSE_CHAIN (l)
11970 = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
11971 cclauses[C_OMP_CLAUSE_SPLIT_FOR] = l;
11973 else
11975 OMP_CLAUSE_CHAIN (l) = clauses;
11976 clauses = l;
11978 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
11982 OMP_FOR_CLAUSES (stmt) = clauses;
11984 ret = stmt;
11986 pop_scopes:
11987 while (!for_block->is_empty ())
11989 /* FIXME diagnostics: LOC below should be the actual location of
11990 this particular for block. We need to build a list of
11991 locations to go along with FOR_BLOCK. */
11992 stmt = c_end_compound_stmt (loc, for_block->pop (), true);
11993 add_stmt (stmt);
11995 release_tree_vector (for_block);
11996 return ret;
11999 /* Helper function for OpenMP parsing, split clauses and call
12000 finish_omp_clauses on each of the set of clauses afterwards. */
12002 static void
12003 omp_split_clauses (location_t loc, enum tree_code code,
12004 omp_clause_mask mask, tree clauses, tree *cclauses)
12006 int i;
12007 c_omp_split_clauses (loc, code, mask, clauses, cclauses);
12008 for (i = 0; i < C_OMP_CLAUSE_SPLIT_COUNT; i++)
12009 if (cclauses[i])
12010 cclauses[i] = c_finish_omp_clauses (cclauses[i]);
12013 /* OpenMP 4.0:
12014 #pragma omp simd simd-clause[optseq] new-line
12015 for-loop
12017 LOC is the location of the #pragma token.
12020 #define OMP_SIMD_CLAUSE_MASK \
12021 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
12022 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
12023 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
12024 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
12025 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
12026 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
12027 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
12029 static tree
12030 c_parser_omp_simd (location_t loc, c_parser *parser,
12031 char *p_name, omp_clause_mask mask, tree *cclauses)
12033 tree block, clauses, ret;
12035 strcat (p_name, " simd");
12036 mask |= OMP_SIMD_CLAUSE_MASK;
12037 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED);
12039 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
12040 if (cclauses)
12042 omp_split_clauses (loc, OMP_SIMD, mask, clauses, cclauses);
12043 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SIMD];
12046 block = c_begin_compound_stmt (true);
12047 ret = c_parser_omp_for_loop (loc, parser, OMP_SIMD, clauses, cclauses);
12048 block = c_end_compound_stmt (loc, block, true);
12049 add_stmt (block);
12051 return ret;
12054 /* OpenMP 2.5:
12055 #pragma omp for for-clause[optseq] new-line
12056 for-loop
12058 OpenMP 4.0:
12059 #pragma omp for simd for-simd-clause[optseq] new-line
12060 for-loop
12062 LOC is the location of the #pragma token.
12065 #define OMP_FOR_CLAUSE_MASK \
12066 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
12067 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
12068 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
12069 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
12070 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
12071 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
12072 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
12073 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
12075 static tree
12076 c_parser_omp_for (location_t loc, c_parser *parser,
12077 char *p_name, omp_clause_mask mask, tree *cclauses)
12079 tree block, clauses, ret;
12081 strcat (p_name, " for");
12082 mask |= OMP_FOR_CLAUSE_MASK;
12083 if (cclauses)
12084 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
12086 if (c_parser_next_token_is (parser, CPP_NAME))
12088 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
12090 if (strcmp (p, "simd") == 0)
12092 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
12093 if (cclauses == NULL)
12094 cclauses = cclauses_buf;
12096 c_parser_consume_token (parser);
12097 if (!flag_openmp) /* flag_openmp_simd */
12098 return c_parser_omp_simd (loc, parser, p_name, mask, cclauses);
12099 block = c_begin_compound_stmt (true);
12100 ret = c_parser_omp_simd (loc, parser, p_name, mask, cclauses);
12101 block = c_end_compound_stmt (loc, block, true);
12102 if (ret == NULL_TREE)
12103 return ret;
12104 ret = make_node (OMP_FOR);
12105 TREE_TYPE (ret) = void_type_node;
12106 OMP_FOR_BODY (ret) = block;
12107 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
12108 SET_EXPR_LOCATION (ret, loc);
12109 add_stmt (ret);
12110 return ret;
12113 if (!flag_openmp) /* flag_openmp_simd */
12115 c_parser_skip_to_pragma_eol (parser);
12116 return NULL_TREE;
12119 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
12120 if (cclauses)
12122 omp_split_clauses (loc, OMP_FOR, mask, clauses, cclauses);
12123 clauses = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
12126 block = c_begin_compound_stmt (true);
12127 ret = c_parser_omp_for_loop (loc, parser, OMP_FOR, clauses, cclauses);
12128 block = c_end_compound_stmt (loc, block, true);
12129 add_stmt (block);
12131 return ret;
12134 /* OpenMP 2.5:
12135 # pragma omp master new-line
12136 structured-block
12138 LOC is the location of the #pragma token.
12141 static tree
12142 c_parser_omp_master (location_t loc, c_parser *parser)
12144 c_parser_skip_to_pragma_eol (parser);
12145 return c_finish_omp_master (loc, c_parser_omp_structured_block (parser));
12148 /* OpenMP 2.5:
12149 # pragma omp ordered new-line
12150 structured-block
12152 LOC is the location of the #pragma itself.
12155 static tree
12156 c_parser_omp_ordered (location_t loc, c_parser *parser)
12158 c_parser_skip_to_pragma_eol (parser);
12159 return c_finish_omp_ordered (loc, c_parser_omp_structured_block (parser));
12162 /* OpenMP 2.5:
12164 section-scope:
12165 { section-sequence }
12167 section-sequence:
12168 section-directive[opt] structured-block
12169 section-sequence section-directive structured-block
12171 SECTIONS_LOC is the location of the #pragma omp sections. */
12173 static tree
12174 c_parser_omp_sections_scope (location_t sections_loc, c_parser *parser)
12176 tree stmt, substmt;
12177 bool error_suppress = false;
12178 location_t loc;
12180 loc = c_parser_peek_token (parser)->location;
12181 if (!c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
12183 /* Avoid skipping until the end of the block. */
12184 parser->error = false;
12185 return NULL_TREE;
12188 stmt = push_stmt_list ();
12190 if (c_parser_peek_token (parser)->pragma_kind != PRAGMA_OMP_SECTION)
12192 substmt = c_parser_omp_structured_block (parser);
12193 substmt = build1 (OMP_SECTION, void_type_node, substmt);
12194 SET_EXPR_LOCATION (substmt, loc);
12195 add_stmt (substmt);
12198 while (1)
12200 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
12201 break;
12202 if (c_parser_next_token_is (parser, CPP_EOF))
12203 break;
12205 loc = c_parser_peek_token (parser)->location;
12206 if (c_parser_peek_token (parser)->pragma_kind == PRAGMA_OMP_SECTION)
12208 c_parser_consume_pragma (parser);
12209 c_parser_skip_to_pragma_eol (parser);
12210 error_suppress = false;
12212 else if (!error_suppress)
12214 error_at (loc, "expected %<#pragma omp section%> or %<}%>");
12215 error_suppress = true;
12218 substmt = c_parser_omp_structured_block (parser);
12219 substmt = build1 (OMP_SECTION, void_type_node, substmt);
12220 SET_EXPR_LOCATION (substmt, loc);
12221 add_stmt (substmt);
12223 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE,
12224 "expected %<#pragma omp section%> or %<}%>");
12226 substmt = pop_stmt_list (stmt);
12228 stmt = make_node (OMP_SECTIONS);
12229 SET_EXPR_LOCATION (stmt, sections_loc);
12230 TREE_TYPE (stmt) = void_type_node;
12231 OMP_SECTIONS_BODY (stmt) = substmt;
12233 return add_stmt (stmt);
12236 /* OpenMP 2.5:
12237 # pragma omp sections sections-clause[optseq] newline
12238 sections-scope
12240 LOC is the location of the #pragma token.
12243 #define OMP_SECTIONS_CLAUSE_MASK \
12244 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
12245 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
12246 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
12247 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
12248 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
12250 static tree
12251 c_parser_omp_sections (location_t loc, c_parser *parser,
12252 char *p_name, omp_clause_mask mask, tree *cclauses)
12254 tree block, clauses, ret;
12256 strcat (p_name, " sections");
12257 mask |= OMP_SECTIONS_CLAUSE_MASK;
12258 if (cclauses)
12259 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
12261 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
12262 if (cclauses)
12264 omp_split_clauses (loc, OMP_SECTIONS, mask, clauses, cclauses);
12265 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SECTIONS];
12268 block = c_begin_compound_stmt (true);
12269 ret = c_parser_omp_sections_scope (loc, parser);
12270 if (ret)
12271 OMP_SECTIONS_CLAUSES (ret) = clauses;
12272 block = c_end_compound_stmt (loc, block, true);
12273 add_stmt (block);
12275 return ret;
12278 /* OpenMP 2.5:
12279 # pragma omp parallel parallel-clause[optseq] new-line
12280 structured-block
12281 # pragma omp parallel for parallel-for-clause[optseq] new-line
12282 structured-block
12283 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
12284 structured-block
12286 OpenMP 4.0:
12287 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
12288 structured-block
12290 LOC is the location of the #pragma token.
12293 #define OMP_PARALLEL_CLAUSE_MASK \
12294 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
12295 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
12296 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
12297 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
12298 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
12299 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
12300 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
12301 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
12302 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
12304 static tree
12305 c_parser_omp_parallel (location_t loc, c_parser *parser,
12306 char *p_name, omp_clause_mask mask, tree *cclauses)
12308 tree stmt, clauses, block;
12310 strcat (p_name, " parallel");
12311 mask |= OMP_PARALLEL_CLAUSE_MASK;
12313 if (c_parser_next_token_is_keyword (parser, RID_FOR))
12315 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
12316 if (cclauses == NULL)
12317 cclauses = cclauses_buf;
12319 c_parser_consume_token (parser);
12320 if (!flag_openmp) /* flag_openmp_simd */
12321 return c_parser_omp_for (loc, parser, p_name, mask, cclauses);
12322 block = c_begin_omp_parallel ();
12323 tree ret = c_parser_omp_for (loc, parser, p_name, mask, cclauses);
12324 stmt
12325 = c_finish_omp_parallel (loc, cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
12326 block);
12327 if (ret == NULL_TREE)
12328 return ret;
12329 OMP_PARALLEL_COMBINED (stmt) = 1;
12330 return stmt;
12332 else if (cclauses)
12334 error_at (loc, "expected %<for%> after %qs", p_name);
12335 c_parser_skip_to_pragma_eol (parser);
12336 return NULL_TREE;
12338 else if (!flag_openmp) /* flag_openmp_simd */
12340 c_parser_skip_to_pragma_eol (parser);
12341 return NULL_TREE;
12343 else if (c_parser_next_token_is (parser, CPP_NAME))
12345 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
12346 if (strcmp (p, "sections") == 0)
12348 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
12349 if (cclauses == NULL)
12350 cclauses = cclauses_buf;
12352 c_parser_consume_token (parser);
12353 block = c_begin_omp_parallel ();
12354 c_parser_omp_sections (loc, parser, p_name, mask, cclauses);
12355 stmt = c_finish_omp_parallel (loc,
12356 cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
12357 block);
12358 OMP_PARALLEL_COMBINED (stmt) = 1;
12359 return stmt;
12363 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
12365 block = c_begin_omp_parallel ();
12366 c_parser_statement (parser);
12367 stmt = c_finish_omp_parallel (loc, clauses, block);
12369 return stmt;
12372 /* OpenMP 2.5:
12373 # pragma omp single single-clause[optseq] new-line
12374 structured-block
12376 LOC is the location of the #pragma.
12379 #define OMP_SINGLE_CLAUSE_MASK \
12380 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
12381 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
12382 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
12383 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
12385 static tree
12386 c_parser_omp_single (location_t loc, c_parser *parser)
12388 tree stmt = make_node (OMP_SINGLE);
12389 SET_EXPR_LOCATION (stmt, loc);
12390 TREE_TYPE (stmt) = void_type_node;
12392 OMP_SINGLE_CLAUSES (stmt)
12393 = c_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
12394 "#pragma omp single");
12395 OMP_SINGLE_BODY (stmt) = c_parser_omp_structured_block (parser);
12397 return add_stmt (stmt);
12400 /* OpenMP 3.0:
12401 # pragma omp task task-clause[optseq] new-line
12403 LOC is the location of the #pragma.
12406 #define OMP_TASK_CLAUSE_MASK \
12407 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
12408 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
12409 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
12410 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
12411 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
12412 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
12413 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
12414 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
12415 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND))
12417 static tree
12418 c_parser_omp_task (location_t loc, c_parser *parser)
12420 tree clauses, block;
12422 clauses = c_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
12423 "#pragma omp task");
12425 block = c_begin_omp_task ();
12426 c_parser_statement (parser);
12427 return c_finish_omp_task (loc, clauses, block);
12430 /* OpenMP 3.0:
12431 # pragma omp taskwait new-line
12434 static void
12435 c_parser_omp_taskwait (c_parser *parser)
12437 location_t loc = c_parser_peek_token (parser)->location;
12438 c_parser_consume_pragma (parser);
12439 c_parser_skip_to_pragma_eol (parser);
12441 c_finish_omp_taskwait (loc);
12444 /* OpenMP 3.1:
12445 # pragma omp taskyield new-line
12448 static void
12449 c_parser_omp_taskyield (c_parser *parser)
12451 location_t loc = c_parser_peek_token (parser)->location;
12452 c_parser_consume_pragma (parser);
12453 c_parser_skip_to_pragma_eol (parser);
12455 c_finish_omp_taskyield (loc);
12458 /* OpenMP 4.0:
12459 # pragma omp taskgroup new-line
12462 static tree
12463 c_parser_omp_taskgroup (c_parser *parser)
12465 location_t loc = c_parser_peek_token (parser)->location;
12466 c_parser_skip_to_pragma_eol (parser);
12467 return c_finish_omp_taskgroup (loc, c_parser_omp_structured_block (parser));
12470 /* OpenMP 4.0:
12471 # pragma omp cancel cancel-clause[optseq] new-line
12473 LOC is the location of the #pragma.
12476 #define OMP_CANCEL_CLAUSE_MASK \
12477 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
12478 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
12479 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
12480 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
12481 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
12483 static void
12484 c_parser_omp_cancel (c_parser *parser)
12486 location_t loc = c_parser_peek_token (parser)->location;
12488 c_parser_consume_pragma (parser);
12489 tree clauses = c_parser_omp_all_clauses (parser, OMP_CANCEL_CLAUSE_MASK,
12490 "#pragma omp cancel");
12492 c_finish_omp_cancel (loc, clauses);
12495 /* OpenMP 4.0:
12496 # pragma omp cancellation point cancelpt-clause[optseq] new-line
12498 LOC is the location of the #pragma.
12501 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
12502 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
12503 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
12504 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
12505 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
12507 static void
12508 c_parser_omp_cancellation_point (c_parser *parser)
12510 location_t loc = c_parser_peek_token (parser)->location;
12511 tree clauses;
12512 bool point_seen = false;
12514 c_parser_consume_pragma (parser);
12515 if (c_parser_next_token_is (parser, CPP_NAME))
12517 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
12518 if (strcmp (p, "point") == 0)
12520 c_parser_consume_token (parser);
12521 point_seen = true;
12524 if (!point_seen)
12526 c_parser_error (parser, "expected %<point%>");
12527 c_parser_skip_to_pragma_eol (parser);
12528 return;
12531 clauses
12532 = c_parser_omp_all_clauses (parser, OMP_CANCELLATION_POINT_CLAUSE_MASK,
12533 "#pragma omp cancellation point");
12535 c_finish_omp_cancellation_point (loc, clauses);
12538 /* OpenMP 4.0:
12539 #pragma omp distribute distribute-clause[optseq] new-line
12540 for-loop */
12542 #define OMP_DISTRIBUTE_CLAUSE_MASK \
12543 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
12544 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
12545 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
12546 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
12548 static tree
12549 c_parser_omp_distribute (location_t loc, c_parser *parser,
12550 char *p_name, omp_clause_mask mask, tree *cclauses)
12552 tree clauses, block, ret;
12554 strcat (p_name, " distribute");
12555 mask |= OMP_DISTRIBUTE_CLAUSE_MASK;
12557 if (c_parser_next_token_is (parser, CPP_NAME))
12559 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
12560 bool simd = false;
12561 bool parallel = false;
12563 if (strcmp (p, "simd") == 0)
12564 simd = true;
12565 else
12566 parallel = strcmp (p, "parallel") == 0;
12567 if (parallel || simd)
12569 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
12570 if (cclauses == NULL)
12571 cclauses = cclauses_buf;
12572 c_parser_consume_token (parser);
12573 if (!flag_openmp) /* flag_openmp_simd */
12575 if (simd)
12576 return c_parser_omp_simd (loc, parser, p_name, mask, cclauses);
12577 else
12578 return c_parser_omp_parallel (loc, parser, p_name, mask,
12579 cclauses);
12581 block = c_begin_compound_stmt (true);
12582 if (simd)
12583 ret = c_parser_omp_simd (loc, parser, p_name, mask, cclauses);
12584 else
12585 ret = c_parser_omp_parallel (loc, parser, p_name, mask, cclauses);
12586 block = c_end_compound_stmt (loc, block, true);
12587 if (ret == NULL)
12588 return ret;
12589 ret = make_node (OMP_DISTRIBUTE);
12590 TREE_TYPE (ret) = void_type_node;
12591 OMP_FOR_BODY (ret) = block;
12592 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
12593 SET_EXPR_LOCATION (ret, loc);
12594 add_stmt (ret);
12595 return ret;
12598 if (!flag_openmp) /* flag_openmp_simd */
12600 c_parser_skip_to_pragma_eol (parser);
12601 return NULL_TREE;
12604 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
12605 if (cclauses)
12607 omp_split_clauses (loc, OMP_DISTRIBUTE, mask, clauses, cclauses);
12608 clauses = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
12611 block = c_begin_compound_stmt (true);
12612 ret = c_parser_omp_for_loop (loc, parser, OMP_DISTRIBUTE, clauses, NULL);
12613 block = c_end_compound_stmt (loc, block, true);
12614 add_stmt (block);
12616 return ret;
12619 /* OpenMP 4.0:
12620 # pragma omp teams teams-clause[optseq] new-line
12621 structured-block */
12623 #define OMP_TEAMS_CLAUSE_MASK \
12624 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
12625 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
12626 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
12627 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
12628 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
12629 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
12630 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
12632 static tree
12633 c_parser_omp_teams (location_t loc, c_parser *parser,
12634 char *p_name, omp_clause_mask mask, tree *cclauses)
12636 tree clauses, block, ret;
12638 strcat (p_name, " teams");
12639 mask |= OMP_TEAMS_CLAUSE_MASK;
12641 if (c_parser_next_token_is (parser, CPP_NAME))
12643 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
12644 if (strcmp (p, "distribute") == 0)
12646 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
12647 if (cclauses == NULL)
12648 cclauses = cclauses_buf;
12650 c_parser_consume_token (parser);
12651 if (!flag_openmp) /* flag_openmp_simd */
12652 return c_parser_omp_distribute (loc, parser, p_name, mask, cclauses);
12653 block = c_begin_compound_stmt (true);
12654 ret = c_parser_omp_distribute (loc, parser, p_name, mask, cclauses);
12655 block = c_end_compound_stmt (loc, block, true);
12656 if (ret == NULL)
12657 return ret;
12658 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
12659 ret = make_node (OMP_TEAMS);
12660 TREE_TYPE (ret) = void_type_node;
12661 OMP_TEAMS_CLAUSES (ret) = clauses;
12662 OMP_TEAMS_BODY (ret) = block;
12663 return add_stmt (ret);
12666 if (!flag_openmp) /* flag_openmp_simd */
12668 c_parser_skip_to_pragma_eol (parser);
12669 return NULL_TREE;
12672 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
12673 if (cclauses)
12675 omp_split_clauses (loc, OMP_TEAMS, mask, clauses, cclauses);
12676 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
12679 tree stmt = make_node (OMP_TEAMS);
12680 TREE_TYPE (stmt) = void_type_node;
12681 OMP_TEAMS_CLAUSES (stmt) = clauses;
12682 OMP_TEAMS_BODY (stmt) = c_parser_omp_structured_block (parser);
12684 return add_stmt (stmt);
12687 /* OpenMP 4.0:
12688 # pragma omp target data target-data-clause[optseq] new-line
12689 structured-block */
12691 #define OMP_TARGET_DATA_CLAUSE_MASK \
12692 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
12693 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
12694 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
12696 static tree
12697 c_parser_omp_target_data (location_t loc, c_parser *parser)
12699 tree stmt = make_node (OMP_TARGET_DATA);
12700 TREE_TYPE (stmt) = void_type_node;
12702 OMP_TARGET_DATA_CLAUSES (stmt)
12703 = c_parser_omp_all_clauses (parser, OMP_TARGET_DATA_CLAUSE_MASK,
12704 "#pragma omp target data");
12705 keep_next_level ();
12706 tree block = c_begin_compound_stmt (true);
12707 add_stmt (c_parser_omp_structured_block (parser));
12708 OMP_TARGET_DATA_BODY (stmt) = c_end_compound_stmt (loc, block, true);
12710 SET_EXPR_LOCATION (stmt, loc);
12711 return add_stmt (stmt);
12714 /* OpenMP 4.0:
12715 # pragma omp target update target-update-clause[optseq] new-line */
12717 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
12718 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
12719 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
12720 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
12721 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
12723 static bool
12724 c_parser_omp_target_update (location_t loc, c_parser *parser,
12725 enum pragma_context context)
12727 if (context == pragma_stmt)
12729 error_at (loc,
12730 "%<#pragma omp target update%> may only be "
12731 "used in compound statements");
12732 c_parser_skip_to_pragma_eol (parser);
12733 return false;
12736 tree clauses
12737 = c_parser_omp_all_clauses (parser, OMP_TARGET_UPDATE_CLAUSE_MASK,
12738 "#pragma omp target update");
12739 if (find_omp_clause (clauses, OMP_CLAUSE_TO) == NULL_TREE
12740 && find_omp_clause (clauses, OMP_CLAUSE_FROM) == NULL_TREE)
12742 error_at (loc,
12743 "%<#pragma omp target update must contain at least one "
12744 "%<from%> or %<to%> clauses");
12745 return false;
12748 tree stmt = make_node (OMP_TARGET_UPDATE);
12749 TREE_TYPE (stmt) = void_type_node;
12750 OMP_TARGET_UPDATE_CLAUSES (stmt) = clauses;
12751 SET_EXPR_LOCATION (stmt, loc);
12752 add_stmt (stmt);
12753 return false;
12756 /* OpenMP 4.0:
12757 # pragma omp target target-clause[optseq] new-line
12758 structured-block */
12760 #define OMP_TARGET_CLAUSE_MASK \
12761 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
12762 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
12763 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
12765 static bool
12766 c_parser_omp_target (c_parser *parser, enum pragma_context context)
12768 location_t loc = c_parser_peek_token (parser)->location;
12769 c_parser_consume_pragma (parser);
12771 if (context != pragma_stmt && context != pragma_compound)
12773 c_parser_error (parser, "expected declaration specifiers");
12774 c_parser_skip_to_pragma_eol (parser);
12775 return false;
12778 if (c_parser_next_token_is (parser, CPP_NAME))
12780 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
12782 if (strcmp (p, "teams") == 0)
12784 tree cclauses[C_OMP_CLAUSE_SPLIT_COUNT];
12785 char p_name[sizeof ("#pragma omp target teams distribute "
12786 "parallel for simd")];
12788 c_parser_consume_token (parser);
12789 strcpy (p_name, "#pragma omp target");
12790 if (!flag_openmp) /* flag_openmp_simd */
12792 tree stmt = c_parser_omp_teams (loc, parser, p_name,
12793 OMP_TARGET_CLAUSE_MASK,
12794 cclauses);
12795 return stmt != NULL_TREE;
12797 keep_next_level ();
12798 tree block = c_begin_compound_stmt (true);
12799 tree ret = c_parser_omp_teams (loc, parser, p_name,
12800 OMP_TARGET_CLAUSE_MASK, cclauses);
12801 block = c_end_compound_stmt (loc, block, true);
12802 if (ret == NULL_TREE)
12803 return false;
12804 tree stmt = make_node (OMP_TARGET);
12805 TREE_TYPE (stmt) = void_type_node;
12806 OMP_TARGET_CLAUSES (stmt) = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
12807 OMP_TARGET_BODY (stmt) = block;
12808 add_stmt (stmt);
12809 return true;
12811 else if (!flag_openmp) /* flag_openmp_simd */
12813 c_parser_skip_to_pragma_eol (parser);
12814 return false;
12816 else if (strcmp (p, "data") == 0)
12818 c_parser_consume_token (parser);
12819 c_parser_omp_target_data (loc, parser);
12820 return true;
12822 else if (strcmp (p, "update") == 0)
12824 c_parser_consume_token (parser);
12825 return c_parser_omp_target_update (loc, parser, context);
12829 tree stmt = make_node (OMP_TARGET);
12830 TREE_TYPE (stmt) = void_type_node;
12832 OMP_TARGET_CLAUSES (stmt)
12833 = c_parser_omp_all_clauses (parser, OMP_TARGET_CLAUSE_MASK,
12834 "#pragma omp target");
12835 keep_next_level ();
12836 tree block = c_begin_compound_stmt (true);
12837 add_stmt (c_parser_omp_structured_block (parser));
12838 OMP_TARGET_BODY (stmt) = c_end_compound_stmt (loc, block, true);
12840 SET_EXPR_LOCATION (stmt, loc);
12841 add_stmt (stmt);
12842 return true;
12845 /* OpenMP 4.0:
12846 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
12848 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
12849 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
12850 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
12851 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
12852 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
12853 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
12854 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
12856 static void
12857 c_parser_omp_declare_simd (c_parser *parser, enum pragma_context context)
12859 vec<c_token> clauses = vNULL;
12860 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
12862 c_token *token = c_parser_peek_token (parser);
12863 if (token->type == CPP_EOF)
12865 c_parser_skip_to_pragma_eol (parser);
12866 clauses.release ();
12867 return;
12869 clauses.safe_push (*token);
12870 c_parser_consume_token (parser);
12872 clauses.safe_push (*c_parser_peek_token (parser));
12873 c_parser_skip_to_pragma_eol (parser);
12875 while (c_parser_next_token_is (parser, CPP_PRAGMA))
12877 if (c_parser_peek_token (parser)->pragma_kind
12878 != PRAGMA_OMP_DECLARE_REDUCTION
12879 || c_parser_peek_2nd_token (parser)->type != CPP_NAME
12880 || strcmp (IDENTIFIER_POINTER
12881 (c_parser_peek_2nd_token (parser)->value),
12882 "simd") != 0)
12884 c_parser_error (parser,
12885 "%<#pragma omp declare simd%> must be followed by "
12886 "function declaration or definition or another "
12887 "%<#pragma omp declare simd%>");
12888 clauses.release ();
12889 return;
12891 c_parser_consume_pragma (parser);
12892 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
12894 c_token *token = c_parser_peek_token (parser);
12895 if (token->type == CPP_EOF)
12897 c_parser_skip_to_pragma_eol (parser);
12898 clauses.release ();
12899 return;
12901 clauses.safe_push (*token);
12902 c_parser_consume_token (parser);
12904 clauses.safe_push (*c_parser_peek_token (parser));
12905 c_parser_skip_to_pragma_eol (parser);
12908 /* Make sure nothing tries to read past the end of the tokens. */
12909 c_token eof_token;
12910 memset (&eof_token, 0, sizeof (eof_token));
12911 eof_token.type = CPP_EOF;
12912 clauses.safe_push (eof_token);
12913 clauses.safe_push (eof_token);
12915 switch (context)
12917 case pragma_external:
12918 if (c_parser_next_token_is (parser, CPP_KEYWORD)
12919 && c_parser_peek_token (parser)->keyword == RID_EXTENSION)
12921 int ext = disable_extension_diagnostics ();
12923 c_parser_consume_token (parser);
12924 while (c_parser_next_token_is (parser, CPP_KEYWORD)
12925 && c_parser_peek_token (parser)->keyword == RID_EXTENSION);
12926 c_parser_declaration_or_fndef (parser, true, true, true, false, true,
12927 NULL, clauses);
12928 restore_extension_diagnostics (ext);
12930 else
12931 c_parser_declaration_or_fndef (parser, true, true, true, false, true,
12932 NULL, clauses);
12933 break;
12934 case pragma_struct:
12935 case pragma_param:
12936 c_parser_error (parser, "%<#pragma omp declare simd%> must be followed by "
12937 "function declaration or definition");
12938 break;
12939 case pragma_compound:
12940 case pragma_stmt:
12941 if (c_parser_next_token_is (parser, CPP_KEYWORD)
12942 && c_parser_peek_token (parser)->keyword == RID_EXTENSION)
12944 int ext = disable_extension_diagnostics ();
12946 c_parser_consume_token (parser);
12947 while (c_parser_next_token_is (parser, CPP_KEYWORD)
12948 && c_parser_peek_token (parser)->keyword == RID_EXTENSION);
12949 if (c_parser_next_tokens_start_declaration (parser))
12951 c_parser_declaration_or_fndef (parser, true, true, true, true,
12952 true, NULL, clauses);
12953 restore_extension_diagnostics (ext);
12954 break;
12956 restore_extension_diagnostics (ext);
12958 else if (c_parser_next_tokens_start_declaration (parser))
12960 c_parser_declaration_or_fndef (parser, true, true, true, true, true,
12961 NULL, clauses);
12962 break;
12964 c_parser_error (parser, "%<#pragma omp declare simd%> must be followed by "
12965 "function declaration or definition");
12966 break;
12967 default:
12968 gcc_unreachable ();
12970 clauses.release ();
12973 /* Finalize #pragma omp declare simd clauses after FNDECL has been parsed,
12974 and put that into "omp declare simd" attribute. */
12976 static void
12977 c_finish_omp_declare_simd (c_parser *parser, tree fndecl, tree parms,
12978 vec<c_token> clauses)
12980 if (flag_cilkplus
12981 && clauses.exists () && !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
12983 error ("%<#pragma omp declare simd%> cannot be used in the same "
12984 "function marked as a Cilk Plus SIMD-enabled function");
12985 vec_free (parser->cilk_simd_fn_tokens);
12986 return;
12989 /* Normally first token is CPP_NAME "simd". CPP_EOF there indicates
12990 error has been reported and CPP_PRAGMA that c_finish_omp_declare_simd
12991 has already processed the tokens. */
12992 if (clauses.exists () && clauses[0].type == CPP_EOF)
12993 return;
12994 if (fndecl == NULL_TREE || TREE_CODE (fndecl) != FUNCTION_DECL)
12996 error ("%<#pragma omp declare simd%> not immediately followed by "
12997 "a function declaration or definition");
12998 clauses[0].type = CPP_EOF;
12999 return;
13001 if (clauses.exists () && clauses[0].type != CPP_NAME)
13003 error_at (DECL_SOURCE_LOCATION (fndecl),
13004 "%<#pragma omp declare simd%> not immediately followed by "
13005 "a single function declaration or definition");
13006 clauses[0].type = CPP_EOF;
13007 return;
13010 if (parms == NULL_TREE)
13011 parms = DECL_ARGUMENTS (fndecl);
13013 unsigned int tokens_avail = parser->tokens_avail;
13014 gcc_assert (parser->tokens == &parser->tokens_buf[0]);
13015 bool is_cilkplus_cilk_simd_fn = false;
13017 if (flag_cilkplus && !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
13019 parser->tokens = parser->cilk_simd_fn_tokens->address ();
13020 parser->tokens_avail = vec_safe_length (parser->cilk_simd_fn_tokens);
13021 is_cilkplus_cilk_simd_fn = true;
13023 else
13025 parser->tokens = clauses.address ();
13026 parser->tokens_avail = clauses.length ();
13029 /* c_parser_omp_declare_simd pushed 2 extra CPP_EOF tokens at the end. */
13030 while (parser->tokens_avail > 3)
13032 c_token *token = c_parser_peek_token (parser);
13033 if (!is_cilkplus_cilk_simd_fn)
13034 gcc_assert (token->type == CPP_NAME
13035 && strcmp (IDENTIFIER_POINTER (token->value), "simd") == 0);
13036 else
13037 gcc_assert (token->type == CPP_NAME
13038 && is_cilkplus_vector_p (token->value));
13039 c_parser_consume_token (parser);
13040 parser->in_pragma = true;
13042 tree c = NULL_TREE;
13043 if (is_cilkplus_cilk_simd_fn)
13044 c = c_parser_omp_all_clauses (parser, CILK_SIMD_FN_CLAUSE_MASK,
13045 "SIMD-enabled functions attribute");
13046 else
13047 c = c_parser_omp_all_clauses (parser, OMP_DECLARE_SIMD_CLAUSE_MASK,
13048 "#pragma omp declare simd");
13049 c = c_omp_declare_simd_clauses_to_numbers (parms, c);
13050 if (c != NULL_TREE)
13051 c = tree_cons (NULL_TREE, c, NULL_TREE);
13052 if (is_cilkplus_cilk_simd_fn)
13054 tree k = build_tree_list (get_identifier ("cilk simd function"),
13055 NULL_TREE);
13056 TREE_CHAIN (k) = DECL_ATTRIBUTES (fndecl);
13057 DECL_ATTRIBUTES (fndecl) = k;
13059 c = build_tree_list (get_identifier ("omp declare simd"), c);
13060 TREE_CHAIN (c) = DECL_ATTRIBUTES (fndecl);
13061 DECL_ATTRIBUTES (fndecl) = c;
13064 parser->tokens = &parser->tokens_buf[0];
13065 parser->tokens_avail = tokens_avail;
13066 if (clauses.exists ())
13067 clauses[0].type = CPP_PRAGMA;
13069 if (!vec_safe_is_empty (parser->cilk_simd_fn_tokens))
13070 vec_free (parser->cilk_simd_fn_tokens);
13074 /* OpenMP 4.0:
13075 # pragma omp declare target new-line
13076 declarations and definitions
13077 # pragma omp end declare target new-line */
13079 static void
13080 c_parser_omp_declare_target (c_parser *parser)
13082 c_parser_skip_to_pragma_eol (parser);
13083 current_omp_declare_target_attribute++;
13086 static void
13087 c_parser_omp_end_declare_target (c_parser *parser)
13089 location_t loc = c_parser_peek_token (parser)->location;
13090 c_parser_consume_pragma (parser);
13091 if (c_parser_next_token_is (parser, CPP_NAME)
13092 && strcmp (IDENTIFIER_POINTER (c_parser_peek_token (parser)->value),
13093 "declare") == 0)
13095 c_parser_consume_token (parser);
13096 if (c_parser_next_token_is (parser, CPP_NAME)
13097 && strcmp (IDENTIFIER_POINTER (c_parser_peek_token (parser)->value),
13098 "target") == 0)
13099 c_parser_consume_token (parser);
13100 else
13102 c_parser_error (parser, "expected %<target%>");
13103 c_parser_skip_to_pragma_eol (parser);
13104 return;
13107 else
13109 c_parser_error (parser, "expected %<declare%>");
13110 c_parser_skip_to_pragma_eol (parser);
13111 return;
13113 c_parser_skip_to_pragma_eol (parser);
13114 if (!current_omp_declare_target_attribute)
13115 error_at (loc, "%<#pragma omp end declare target%> without corresponding "
13116 "%<#pragma omp declare target%>");
13117 else
13118 current_omp_declare_target_attribute--;
13122 /* OpenMP 4.0
13123 #pragma omp declare reduction (reduction-id : typename-list : expression) \
13124 initializer-clause[opt] new-line
13126 initializer-clause:
13127 initializer (omp_priv = initializer)
13128 initializer (function-name (argument-list)) */
13130 static void
13131 c_parser_omp_declare_reduction (c_parser *parser, enum pragma_context context)
13133 unsigned int tokens_avail = 0, i;
13134 vec<tree> types = vNULL;
13135 vec<c_token> clauses = vNULL;
13136 enum tree_code reduc_code = ERROR_MARK;
13137 tree reduc_id = NULL_TREE;
13138 tree type;
13139 location_t rloc = c_parser_peek_token (parser)->location;
13141 if (context == pragma_struct || context == pragma_param)
13143 error ("%<#pragma omp declare reduction%> not at file or block scope");
13144 goto fail;
13147 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
13148 goto fail;
13150 switch (c_parser_peek_token (parser)->type)
13152 case CPP_PLUS:
13153 reduc_code = PLUS_EXPR;
13154 break;
13155 case CPP_MULT:
13156 reduc_code = MULT_EXPR;
13157 break;
13158 case CPP_MINUS:
13159 reduc_code = MINUS_EXPR;
13160 break;
13161 case CPP_AND:
13162 reduc_code = BIT_AND_EXPR;
13163 break;
13164 case CPP_XOR:
13165 reduc_code = BIT_XOR_EXPR;
13166 break;
13167 case CPP_OR:
13168 reduc_code = BIT_IOR_EXPR;
13169 break;
13170 case CPP_AND_AND:
13171 reduc_code = TRUTH_ANDIF_EXPR;
13172 break;
13173 case CPP_OR_OR:
13174 reduc_code = TRUTH_ORIF_EXPR;
13175 break;
13176 case CPP_NAME:
13177 const char *p;
13178 p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
13179 if (strcmp (p, "min") == 0)
13181 reduc_code = MIN_EXPR;
13182 break;
13184 if (strcmp (p, "max") == 0)
13186 reduc_code = MAX_EXPR;
13187 break;
13189 reduc_id = c_parser_peek_token (parser)->value;
13190 break;
13191 default:
13192 c_parser_error (parser,
13193 "expected %<+%>, %<*%>, %<-%>, %<&%>, "
13194 "%<^%>, %<|%>, %<&&%>, %<||%>, %<min%> or identifier");
13195 goto fail;
13198 tree orig_reduc_id, reduc_decl;
13199 orig_reduc_id = reduc_id;
13200 reduc_id = c_omp_reduction_id (reduc_code, reduc_id);
13201 reduc_decl = c_omp_reduction_decl (reduc_id);
13202 c_parser_consume_token (parser);
13204 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
13205 goto fail;
13207 while (true)
13209 location_t loc = c_parser_peek_token (parser)->location;
13210 struct c_type_name *ctype = c_parser_type_name (parser);
13211 if (ctype != NULL)
13213 type = groktypename (ctype, NULL, NULL);
13214 if (type == error_mark_node)
13216 else if ((INTEGRAL_TYPE_P (type)
13217 || TREE_CODE (type) == REAL_TYPE
13218 || TREE_CODE (type) == COMPLEX_TYPE)
13219 && orig_reduc_id == NULL_TREE)
13220 error_at (loc, "predeclared arithmetic type in "
13221 "%<#pragma omp declare reduction%>");
13222 else if (TREE_CODE (type) == FUNCTION_TYPE
13223 || TREE_CODE (type) == ARRAY_TYPE)
13224 error_at (loc, "function or array type in "
13225 "%<#pragma omp declare reduction%>");
13226 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
13227 error_at (loc, "const, volatile or restrict qualified type in "
13228 "%<#pragma omp declare reduction%>");
13229 else
13231 tree t;
13232 for (t = DECL_INITIAL (reduc_decl); t; t = TREE_CHAIN (t))
13233 if (comptypes (TREE_PURPOSE (t), type))
13235 error_at (loc, "redeclaration of %qs "
13236 "%<#pragma omp declare reduction%> for "
13237 "type %qT",
13238 IDENTIFIER_POINTER (reduc_id)
13239 + sizeof ("omp declare reduction ") - 1,
13240 type);
13241 location_t ploc
13242 = DECL_SOURCE_LOCATION (TREE_VEC_ELT (TREE_VALUE (t),
13243 0));
13244 error_at (ploc, "previous %<#pragma omp declare "
13245 "reduction%>");
13246 break;
13248 if (t == NULL_TREE)
13249 types.safe_push (type);
13251 if (c_parser_next_token_is (parser, CPP_COMMA))
13252 c_parser_consume_token (parser);
13253 else
13254 break;
13256 else
13257 break;
13260 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>")
13261 || types.is_empty ())
13263 fail:
13264 clauses.release ();
13265 types.release ();
13266 while (true)
13268 c_token *token = c_parser_peek_token (parser);
13269 if (token->type == CPP_EOF || token->type == CPP_PRAGMA_EOL)
13270 break;
13271 c_parser_consume_token (parser);
13273 c_parser_skip_to_pragma_eol (parser);
13274 return;
13277 if (types.length () > 1)
13279 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
13281 c_token *token = c_parser_peek_token (parser);
13282 if (token->type == CPP_EOF)
13283 goto fail;
13284 clauses.safe_push (*token);
13285 c_parser_consume_token (parser);
13287 clauses.safe_push (*c_parser_peek_token (parser));
13288 c_parser_skip_to_pragma_eol (parser);
13290 /* Make sure nothing tries to read past the end of the tokens. */
13291 c_token eof_token;
13292 memset (&eof_token, 0, sizeof (eof_token));
13293 eof_token.type = CPP_EOF;
13294 clauses.safe_push (eof_token);
13295 clauses.safe_push (eof_token);
13298 int errs = errorcount;
13299 FOR_EACH_VEC_ELT (types, i, type)
13301 tokens_avail = parser->tokens_avail;
13302 gcc_assert (parser->tokens == &parser->tokens_buf[0]);
13303 if (!clauses.is_empty ())
13305 parser->tokens = clauses.address ();
13306 parser->tokens_avail = clauses.length ();
13307 parser->in_pragma = true;
13310 bool nested = current_function_decl != NULL_TREE;
13311 if (nested)
13312 c_push_function_context ();
13313 tree fndecl = build_decl (BUILTINS_LOCATION, FUNCTION_DECL,
13314 reduc_id, default_function_type);
13315 current_function_decl = fndecl;
13316 allocate_struct_function (fndecl, true);
13317 push_scope ();
13318 tree stmt = push_stmt_list ();
13319 /* Intentionally BUILTINS_LOCATION, so that -Wshadow doesn't
13320 warn about these. */
13321 tree omp_out = build_decl (BUILTINS_LOCATION, VAR_DECL,
13322 get_identifier ("omp_out"), type);
13323 DECL_ARTIFICIAL (omp_out) = 1;
13324 DECL_CONTEXT (omp_out) = fndecl;
13325 pushdecl (omp_out);
13326 tree omp_in = build_decl (BUILTINS_LOCATION, VAR_DECL,
13327 get_identifier ("omp_in"), type);
13328 DECL_ARTIFICIAL (omp_in) = 1;
13329 DECL_CONTEXT (omp_in) = fndecl;
13330 pushdecl (omp_in);
13331 struct c_expr combiner = c_parser_expression (parser);
13332 struct c_expr initializer;
13333 tree omp_priv = NULL_TREE, omp_orig = NULL_TREE;
13334 bool bad = false;
13335 initializer.value = error_mark_node;
13336 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
13337 bad = true;
13338 else if (c_parser_next_token_is (parser, CPP_NAME)
13339 && strcmp (IDENTIFIER_POINTER
13340 (c_parser_peek_token (parser)->value),
13341 "initializer") == 0)
13343 c_parser_consume_token (parser);
13344 pop_scope ();
13345 push_scope ();
13346 omp_priv = build_decl (BUILTINS_LOCATION, VAR_DECL,
13347 get_identifier ("omp_priv"), type);
13348 DECL_ARTIFICIAL (omp_priv) = 1;
13349 DECL_INITIAL (omp_priv) = error_mark_node;
13350 DECL_CONTEXT (omp_priv) = fndecl;
13351 pushdecl (omp_priv);
13352 omp_orig = build_decl (BUILTINS_LOCATION, VAR_DECL,
13353 get_identifier ("omp_orig"), type);
13354 DECL_ARTIFICIAL (omp_orig) = 1;
13355 DECL_CONTEXT (omp_orig) = fndecl;
13356 pushdecl (omp_orig);
13357 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
13358 bad = true;
13359 else if (!c_parser_next_token_is (parser, CPP_NAME))
13361 c_parser_error (parser, "expected %<omp_priv%> or "
13362 "function-name");
13363 bad = true;
13365 else if (strcmp (IDENTIFIER_POINTER
13366 (c_parser_peek_token (parser)->value),
13367 "omp_priv") != 0)
13369 if (c_parser_peek_2nd_token (parser)->type != CPP_OPEN_PAREN
13370 || c_parser_peek_token (parser)->id_kind != C_ID_ID)
13372 c_parser_error (parser, "expected function-name %<(%>");
13373 bad = true;
13375 else
13376 initializer = c_parser_postfix_expression (parser);
13377 if (initializer.value
13378 && TREE_CODE (initializer.value) == CALL_EXPR)
13380 int j;
13381 tree c = initializer.value;
13382 for (j = 0; j < call_expr_nargs (c); j++)
13383 if (TREE_CODE (CALL_EXPR_ARG (c, j)) == ADDR_EXPR
13384 && TREE_OPERAND (CALL_EXPR_ARG (c, j), 0) == omp_priv)
13385 break;
13386 if (j == call_expr_nargs (c))
13387 error ("one of the initializer call arguments should be "
13388 "%<&omp_priv%>");
13391 else
13393 c_parser_consume_token (parser);
13394 if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
13395 bad = true;
13396 else
13398 tree st = push_stmt_list ();
13399 start_init (omp_priv, NULL_TREE, 0);
13400 location_t loc = c_parser_peek_token (parser)->location;
13401 struct c_expr init = c_parser_initializer (parser);
13402 finish_init ();
13403 finish_decl (omp_priv, loc, init.value,
13404 init.original_type, NULL_TREE);
13405 pop_stmt_list (st);
13408 if (!bad
13409 && !c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
13410 bad = true;
13413 if (!bad)
13415 c_parser_skip_to_pragma_eol (parser);
13417 tree t = tree_cons (type, make_tree_vec (omp_priv ? 6 : 3),
13418 DECL_INITIAL (reduc_decl));
13419 DECL_INITIAL (reduc_decl) = t;
13420 DECL_SOURCE_LOCATION (omp_out) = rloc;
13421 TREE_VEC_ELT (TREE_VALUE (t), 0) = omp_out;
13422 TREE_VEC_ELT (TREE_VALUE (t), 1) = omp_in;
13423 TREE_VEC_ELT (TREE_VALUE (t), 2) = combiner.value;
13424 walk_tree (&combiner.value, c_check_omp_declare_reduction_r,
13425 &TREE_VEC_ELT (TREE_VALUE (t), 0), NULL);
13426 if (omp_priv)
13428 DECL_SOURCE_LOCATION (omp_priv) = rloc;
13429 TREE_VEC_ELT (TREE_VALUE (t), 3) = omp_priv;
13430 TREE_VEC_ELT (TREE_VALUE (t), 4) = omp_orig;
13431 TREE_VEC_ELT (TREE_VALUE (t), 5) = initializer.value;
13432 walk_tree (&initializer.value, c_check_omp_declare_reduction_r,
13433 &TREE_VEC_ELT (TREE_VALUE (t), 3), NULL);
13434 walk_tree (&DECL_INITIAL (omp_priv),
13435 c_check_omp_declare_reduction_r,
13436 &TREE_VEC_ELT (TREE_VALUE (t), 3), NULL);
13440 pop_stmt_list (stmt);
13441 pop_scope ();
13442 if (cfun->language != NULL)
13444 ggc_free (cfun->language);
13445 cfun->language = NULL;
13447 set_cfun (NULL);
13448 current_function_decl = NULL_TREE;
13449 if (nested)
13450 c_pop_function_context ();
13452 if (!clauses.is_empty ())
13454 parser->tokens = &parser->tokens_buf[0];
13455 parser->tokens_avail = tokens_avail;
13457 if (bad)
13458 goto fail;
13459 if (errs != errorcount)
13460 break;
13463 clauses.release ();
13464 types.release ();
13468 /* OpenMP 4.0
13469 #pragma omp declare simd declare-simd-clauses[optseq] new-line
13470 #pragma omp declare reduction (reduction-id : typename-list : expression) \
13471 initializer-clause[opt] new-line
13472 #pragma omp declare target new-line */
13474 static void
13475 c_parser_omp_declare (c_parser *parser, enum pragma_context context)
13477 c_parser_consume_pragma (parser);
13478 if (c_parser_next_token_is (parser, CPP_NAME))
13480 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
13481 if (strcmp (p, "simd") == 0)
13483 /* c_parser_consume_token (parser); done in
13484 c_parser_omp_declare_simd. */
13485 c_parser_omp_declare_simd (parser, context);
13486 return;
13488 if (strcmp (p, "reduction") == 0)
13490 c_parser_consume_token (parser);
13491 c_parser_omp_declare_reduction (parser, context);
13492 return;
13494 if (!flag_openmp) /* flag_openmp_simd */
13496 c_parser_skip_to_pragma_eol (parser);
13497 return;
13499 if (strcmp (p, "target") == 0)
13501 c_parser_consume_token (parser);
13502 c_parser_omp_declare_target (parser);
13503 return;
13507 c_parser_error (parser, "expected %<simd%> or %<reduction%> "
13508 "or %<target%>");
13509 c_parser_skip_to_pragma_eol (parser);
13512 /* Main entry point to parsing most OpenMP pragmas. */
13514 static void
13515 c_parser_omp_construct (c_parser *parser)
13517 enum pragma_kind p_kind;
13518 location_t loc;
13519 tree stmt;
13520 char p_name[sizeof "#pragma omp teams distribute parallel for simd"];
13521 omp_clause_mask mask (0);
13523 loc = c_parser_peek_token (parser)->location;
13524 p_kind = c_parser_peek_token (parser)->pragma_kind;
13525 c_parser_consume_pragma (parser);
13527 switch (p_kind)
13529 case PRAGMA_OMP_ATOMIC:
13530 c_parser_omp_atomic (loc, parser);
13531 return;
13532 case PRAGMA_OMP_CRITICAL:
13533 stmt = c_parser_omp_critical (loc, parser);
13534 break;
13535 case PRAGMA_OMP_DISTRIBUTE:
13536 strcpy (p_name, "#pragma omp");
13537 stmt = c_parser_omp_distribute (loc, parser, p_name, mask, NULL);
13538 break;
13539 case PRAGMA_OMP_FOR:
13540 strcpy (p_name, "#pragma omp");
13541 stmt = c_parser_omp_for (loc, parser, p_name, mask, NULL);
13542 break;
13543 case PRAGMA_OMP_MASTER:
13544 stmt = c_parser_omp_master (loc, parser);
13545 break;
13546 case PRAGMA_OMP_ORDERED:
13547 stmt = c_parser_omp_ordered (loc, parser);
13548 break;
13549 case PRAGMA_OMP_PARALLEL:
13550 strcpy (p_name, "#pragma omp");
13551 stmt = c_parser_omp_parallel (loc, parser, p_name, mask, NULL);
13552 break;
13553 case PRAGMA_OMP_SECTIONS:
13554 strcpy (p_name, "#pragma omp");
13555 stmt = c_parser_omp_sections (loc, parser, p_name, mask, NULL);
13556 break;
13557 case PRAGMA_OMP_SIMD:
13558 strcpy (p_name, "#pragma omp");
13559 stmt = c_parser_omp_simd (loc, parser, p_name, mask, NULL);
13560 break;
13561 case PRAGMA_OMP_SINGLE:
13562 stmt = c_parser_omp_single (loc, parser);
13563 break;
13564 case PRAGMA_OMP_TASK:
13565 stmt = c_parser_omp_task (loc, parser);
13566 break;
13567 case PRAGMA_OMP_TASKGROUP:
13568 stmt = c_parser_omp_taskgroup (parser);
13569 break;
13570 case PRAGMA_OMP_TEAMS:
13571 strcpy (p_name, "#pragma omp");
13572 stmt = c_parser_omp_teams (loc, parser, p_name, mask, NULL);
13573 break;
13574 default:
13575 gcc_unreachable ();
13578 if (stmt)
13579 gcc_assert (EXPR_LOCATION (stmt) != UNKNOWN_LOCATION);
13583 /* OpenMP 2.5:
13584 # pragma omp threadprivate (variable-list) */
13586 static void
13587 c_parser_omp_threadprivate (c_parser *parser)
13589 tree vars, t;
13590 location_t loc;
13592 c_parser_consume_pragma (parser);
13593 loc = c_parser_peek_token (parser)->location;
13594 vars = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_ERROR, NULL);
13596 /* Mark every variable in VARS to be assigned thread local storage. */
13597 for (t = vars; t; t = TREE_CHAIN (t))
13599 tree v = TREE_PURPOSE (t);
13601 /* FIXME diagnostics: Ideally we should keep individual
13602 locations for all the variables in the var list to make the
13603 following errors more precise. Perhaps
13604 c_parser_omp_var_list_parens() should construct a list of
13605 locations to go along with the var list. */
13607 /* If V had already been marked threadprivate, it doesn't matter
13608 whether it had been used prior to this point. */
13609 if (TREE_CODE (v) != VAR_DECL)
13610 error_at (loc, "%qD is not a variable", v);
13611 else if (TREE_USED (v) && !C_DECL_THREADPRIVATE_P (v))
13612 error_at (loc, "%qE declared %<threadprivate%> after first use", v);
13613 else if (! TREE_STATIC (v) && ! DECL_EXTERNAL (v))
13614 error_at (loc, "automatic variable %qE cannot be %<threadprivate%>", v);
13615 else if (TREE_TYPE (v) == error_mark_node)
13617 else if (! COMPLETE_TYPE_P (TREE_TYPE (v)))
13618 error_at (loc, "%<threadprivate%> %qE has incomplete type", v);
13619 else
13621 if (! DECL_THREAD_LOCAL_P (v))
13623 set_decl_tls_model (v, decl_default_tls_model (v));
13624 /* If rtl has been already set for this var, call
13625 make_decl_rtl once again, so that encode_section_info
13626 has a chance to look at the new decl flags. */
13627 if (DECL_RTL_SET_P (v))
13628 make_decl_rtl (v);
13630 C_DECL_THREADPRIVATE_P (v) = 1;
13634 c_parser_skip_to_pragma_eol (parser);
13637 /* Cilk Plus <#pragma simd> parsing routines. */
13639 /* Helper function for c_parser_pragma. Perform some sanity checking
13640 for <#pragma simd> constructs. Returns FALSE if there was a
13641 problem. */
13643 static bool
13644 c_parser_cilk_verify_simd (c_parser *parser,
13645 enum pragma_context context)
13647 if (!flag_cilkplus)
13649 warning (0, "pragma simd ignored because -fcilkplus is not enabled");
13650 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
13651 return false;
13653 if (context == pragma_external)
13655 c_parser_error (parser,"pragma simd must be inside a function");
13656 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
13657 return false;
13659 return true;
13662 /* Cilk Plus:
13663 This function is shared by SIMD-enabled functions and #pragma simd.
13664 If IS_SIMD_FN is true then it is parsing a SIMD-enabled function and
13665 CLAUSES is unused. The main purpose of this function is to parse a
13666 vectorlength attribute or clause and check for parse errors.
13667 When IS_SIMD_FN is true then the function is merely caching the tokens
13668 in PARSER->CILK_SIMD_FN_TOKENS. If errors are found then the token
13669 cache is cleared since there is no reason to continue.
13670 Syntax:
13671 vectorlength ( constant-expression ) */
13673 static tree
13674 c_parser_cilk_clause_vectorlength (c_parser *parser, tree clauses,
13675 bool is_simd_fn)
13677 if (is_simd_fn)
13678 check_no_duplicate_clause (clauses, OMP_CLAUSE_SIMDLEN, "vectorlength");
13679 else
13680 /* The vectorlength clause behaves exactly like OpenMP's safelen
13681 clause. Represent it in OpenMP terms. */
13682 check_no_duplicate_clause (clauses, OMP_CLAUSE_SAFELEN, "vectorlength");
13684 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
13685 return clauses;
13687 location_t loc = c_parser_peek_token (parser)->location;
13688 tree expr = c_parser_expr_no_commas (parser, NULL).value;
13689 expr = c_fully_fold (expr, false, NULL);
13691 /* If expr is an error_mark_node then the above function would have
13692 emitted an error. No reason to do it twice. */
13693 if (expr == error_mark_node)
13695 else if (!TREE_TYPE (expr)
13696 || !TREE_CONSTANT (expr)
13697 || !INTEGRAL_TYPE_P (TREE_TYPE (expr)))
13699 error_at (loc, "vectorlength must be an integer constant");
13700 else if (wi::exact_log2 (expr) == -1)
13701 error_at (loc, "vectorlength must be a power of 2");
13702 else
13704 if (is_simd_fn)
13706 tree u = build_omp_clause (loc, OMP_CLAUSE_SIMDLEN);
13707 OMP_CLAUSE_SIMDLEN_EXPR (u) = expr;
13708 OMP_CLAUSE_CHAIN (u) = clauses;
13709 clauses = u;
13711 else
13713 tree u = build_omp_clause (loc, OMP_CLAUSE_SAFELEN);
13714 OMP_CLAUSE_SAFELEN_EXPR (u) = expr;
13715 OMP_CLAUSE_CHAIN (u) = clauses;
13716 clauses = u;
13720 c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
13722 return clauses;
13725 /* Cilk Plus:
13726 linear ( simd-linear-variable-list )
13728 simd-linear-variable-list:
13729 simd-linear-variable
13730 simd-linear-variable-list , simd-linear-variable
13732 simd-linear-variable:
13733 id-expression
13734 id-expression : simd-linear-step
13736 simd-linear-step:
13737 conditional-expression */
13739 static tree
13740 c_parser_cilk_clause_linear (c_parser *parser, tree clauses)
13742 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
13743 return clauses;
13745 location_t loc = c_parser_peek_token (parser)->location;
13747 if (c_parser_next_token_is_not (parser, CPP_NAME)
13748 || c_parser_peek_token (parser)->id_kind != C_ID_ID)
13749 c_parser_error (parser, "expected identifier");
13751 while (c_parser_next_token_is (parser, CPP_NAME)
13752 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
13754 tree var = lookup_name (c_parser_peek_token (parser)->value);
13756 if (var == NULL)
13758 undeclared_variable (c_parser_peek_token (parser)->location,
13759 c_parser_peek_token (parser)->value);
13760 c_parser_consume_token (parser);
13762 else if (var == error_mark_node)
13763 c_parser_consume_token (parser);
13764 else
13766 tree step = integer_one_node;
13768 /* Parse the linear step if present. */
13769 if (c_parser_peek_2nd_token (parser)->type == CPP_COLON)
13771 c_parser_consume_token (parser);
13772 c_parser_consume_token (parser);
13774 tree expr = c_parser_expr_no_commas (parser, NULL).value;
13775 expr = c_fully_fold (expr, false, NULL);
13777 if (TREE_TYPE (expr)
13778 && INTEGRAL_TYPE_P (TREE_TYPE (expr))
13779 && (TREE_CONSTANT (expr)
13780 || DECL_P (expr)))
13781 step = expr;
13782 else
13783 c_parser_error (parser,
13784 "step size must be an integer constant "
13785 "expression or an integer variable");
13787 else
13788 c_parser_consume_token (parser);
13790 /* Use OMP_CLAUSE_LINEAR, which has the same semantics. */
13791 tree u = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
13792 OMP_CLAUSE_DECL (u) = var;
13793 OMP_CLAUSE_LINEAR_STEP (u) = step;
13794 OMP_CLAUSE_CHAIN (u) = clauses;
13795 clauses = u;
13798 if (c_parser_next_token_is_not (parser, CPP_COMMA))
13799 break;
13801 c_parser_consume_token (parser);
13804 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
13806 return clauses;
13809 /* Returns the name of the next clause. If the clause is not
13810 recognized SIMD_OMP_CLAUSE_NONE is returned and the next token is
13811 not consumed. Otherwise, the appropriate pragma_simd_clause is
13812 returned and the token is consumed. */
13814 static pragma_omp_clause
13815 c_parser_cilk_clause_name (c_parser *parser)
13817 pragma_omp_clause result;
13818 c_token *token = c_parser_peek_token (parser);
13820 if (!token->value || token->type != CPP_NAME)
13821 return PRAGMA_CILK_CLAUSE_NONE;
13823 const char *p = IDENTIFIER_POINTER (token->value);
13825 if (!strcmp (p, "vectorlength"))
13826 result = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
13827 else if (!strcmp (p, "linear"))
13828 result = PRAGMA_CILK_CLAUSE_LINEAR;
13829 else if (!strcmp (p, "private"))
13830 result = PRAGMA_CILK_CLAUSE_PRIVATE;
13831 else if (!strcmp (p, "firstprivate"))
13832 result = PRAGMA_CILK_CLAUSE_FIRSTPRIVATE;
13833 else if (!strcmp (p, "lastprivate"))
13834 result = PRAGMA_CILK_CLAUSE_LASTPRIVATE;
13835 else if (!strcmp (p, "reduction"))
13836 result = PRAGMA_CILK_CLAUSE_REDUCTION;
13837 else
13838 return PRAGMA_CILK_CLAUSE_NONE;
13840 c_parser_consume_token (parser);
13841 return result;
13844 /* Parse all #<pragma simd> clauses. Return the list of clauses
13845 found. */
13847 static tree
13848 c_parser_cilk_all_clauses (c_parser *parser)
13850 tree clauses = NULL;
13852 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
13854 pragma_omp_clause c_kind;
13856 c_kind = c_parser_cilk_clause_name (parser);
13858 switch (c_kind)
13860 case PRAGMA_CILK_CLAUSE_VECTORLENGTH:
13861 clauses = c_parser_cilk_clause_vectorlength (parser, clauses, false);
13862 break;
13863 case PRAGMA_CILK_CLAUSE_LINEAR:
13864 clauses = c_parser_cilk_clause_linear (parser, clauses);
13865 break;
13866 case PRAGMA_CILK_CLAUSE_PRIVATE:
13867 /* Use the OpenMP counterpart. */
13868 clauses = c_parser_omp_clause_private (parser, clauses);
13869 break;
13870 case PRAGMA_CILK_CLAUSE_FIRSTPRIVATE:
13871 /* Use the OpenMP counterpart. */
13872 clauses = c_parser_omp_clause_firstprivate (parser, clauses);
13873 break;
13874 case PRAGMA_CILK_CLAUSE_LASTPRIVATE:
13875 /* Use the OpenMP counterpart. */
13876 clauses = c_parser_omp_clause_lastprivate (parser, clauses);
13877 break;
13878 case PRAGMA_CILK_CLAUSE_REDUCTION:
13879 /* Use the OpenMP counterpart. */
13880 clauses = c_parser_omp_clause_reduction (parser, clauses);
13881 break;
13882 default:
13883 c_parser_error (parser, "expected %<#pragma simd%> clause");
13884 goto saw_error;
13888 saw_error:
13889 c_parser_skip_to_pragma_eol (parser);
13890 return c_finish_cilk_clauses (clauses);
13893 /* Main entry point for parsing Cilk Plus <#pragma simd> for
13894 loops. */
13896 static void
13897 c_parser_cilk_simd (c_parser *parser)
13899 tree clauses = c_parser_cilk_all_clauses (parser);
13900 tree block = c_begin_compound_stmt (true);
13901 location_t loc = c_parser_peek_token (parser)->location;
13902 c_parser_omp_for_loop (loc, parser, CILK_SIMD, clauses, NULL);
13903 block = c_end_compound_stmt (loc, block, true);
13904 add_stmt (block);
13907 /* Parse a transaction attribute (GCC Extension).
13909 transaction-attribute:
13910 attributes
13911 [ [ any-word ] ]
13913 The transactional memory language description is written for C++,
13914 and uses the C++0x attribute syntax. For compatibility, allow the
13915 bracket style for transactions in C as well. */
13917 static tree
13918 c_parser_transaction_attributes (c_parser *parser)
13920 tree attr_name, attr = NULL;
13922 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
13923 return c_parser_attributes (parser);
13925 if (!c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
13926 return NULL_TREE;
13927 c_parser_consume_token (parser);
13928 if (!c_parser_require (parser, CPP_OPEN_SQUARE, "expected %<[%>"))
13929 goto error1;
13931 attr_name = c_parser_attribute_any_word (parser);
13932 if (attr_name)
13934 c_parser_consume_token (parser);
13935 attr = build_tree_list (attr_name, NULL_TREE);
13937 else
13938 c_parser_error (parser, "expected identifier");
13940 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, "expected %<]%>");
13941 error1:
13942 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, "expected %<]%>");
13943 return attr;
13946 /* Parse a __transaction_atomic or __transaction_relaxed statement
13947 (GCC Extension).
13949 transaction-statement:
13950 __transaction_atomic transaction-attribute[opt] compound-statement
13951 __transaction_relaxed compound-statement
13953 Note that the only valid attribute is: "outer".
13956 static tree
13957 c_parser_transaction (c_parser *parser, enum rid keyword)
13959 unsigned int old_in = parser->in_transaction;
13960 unsigned int this_in = 1, new_in;
13961 location_t loc = c_parser_peek_token (parser)->location;
13962 tree stmt, attrs;
13964 gcc_assert ((keyword == RID_TRANSACTION_ATOMIC
13965 || keyword == RID_TRANSACTION_RELAXED)
13966 && c_parser_next_token_is_keyword (parser, keyword));
13967 c_parser_consume_token (parser);
13969 if (keyword == RID_TRANSACTION_RELAXED)
13970 this_in |= TM_STMT_ATTR_RELAXED;
13971 else
13973 attrs = c_parser_transaction_attributes (parser);
13974 if (attrs)
13975 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
13978 /* Keep track if we're in the lexical scope of an outer transaction. */
13979 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
13981 parser->in_transaction = new_in;
13982 stmt = c_parser_compound_statement (parser);
13983 parser->in_transaction = old_in;
13985 if (flag_tm)
13986 stmt = c_finish_transaction (loc, stmt, this_in);
13987 else
13988 error_at (loc, (keyword == RID_TRANSACTION_ATOMIC ?
13989 "%<__transaction_atomic%> without transactional memory support enabled"
13990 : "%<__transaction_relaxed %> "
13991 "without transactional memory support enabled"));
13993 return stmt;
13996 /* Parse a __transaction_atomic or __transaction_relaxed expression
13997 (GCC Extension).
13999 transaction-expression:
14000 __transaction_atomic ( expression )
14001 __transaction_relaxed ( expression )
14004 static struct c_expr
14005 c_parser_transaction_expression (c_parser *parser, enum rid keyword)
14007 struct c_expr ret;
14008 unsigned int old_in = parser->in_transaction;
14009 unsigned int this_in = 1;
14010 location_t loc = c_parser_peek_token (parser)->location;
14011 tree attrs;
14013 gcc_assert ((keyword == RID_TRANSACTION_ATOMIC
14014 || keyword == RID_TRANSACTION_RELAXED)
14015 && c_parser_next_token_is_keyword (parser, keyword));
14016 c_parser_consume_token (parser);
14018 if (keyword == RID_TRANSACTION_RELAXED)
14019 this_in |= TM_STMT_ATTR_RELAXED;
14020 else
14022 attrs = c_parser_transaction_attributes (parser);
14023 if (attrs)
14024 this_in |= parse_tm_stmt_attr (attrs, 0);
14027 parser->in_transaction = this_in;
14028 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
14030 tree expr = c_parser_expression (parser).value;
14031 ret.original_type = TREE_TYPE (expr);
14032 ret.value = build1 (TRANSACTION_EXPR, ret.original_type, expr);
14033 if (this_in & TM_STMT_ATTR_RELAXED)
14034 TRANSACTION_EXPR_RELAXED (ret.value) = 1;
14035 SET_EXPR_LOCATION (ret.value, loc);
14036 ret.original_code = TRANSACTION_EXPR;
14037 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
14039 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
14040 goto error;
14043 else
14045 error:
14046 ret.value = error_mark_node;
14047 ret.original_code = ERROR_MARK;
14048 ret.original_type = NULL;
14050 parser->in_transaction = old_in;
14052 if (!flag_tm)
14053 error_at (loc, (keyword == RID_TRANSACTION_ATOMIC ?
14054 "%<__transaction_atomic%> without transactional memory support enabled"
14055 : "%<__transaction_relaxed %> "
14056 "without transactional memory support enabled"));
14058 return ret;
14061 /* Parse a __transaction_cancel statement (GCC Extension).
14063 transaction-cancel-statement:
14064 __transaction_cancel transaction-attribute[opt] ;
14066 Note that the only valid attribute is "outer".
14069 static tree
14070 c_parser_transaction_cancel (c_parser *parser)
14072 location_t loc = c_parser_peek_token (parser)->location;
14073 tree attrs;
14074 bool is_outer = false;
14076 gcc_assert (c_parser_next_token_is_keyword (parser, RID_TRANSACTION_CANCEL));
14077 c_parser_consume_token (parser);
14079 attrs = c_parser_transaction_attributes (parser);
14080 if (attrs)
14081 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
14083 if (!flag_tm)
14085 error_at (loc, "%<__transaction_cancel%> without "
14086 "transactional memory support enabled");
14087 goto ret_error;
14089 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
14091 error_at (loc, "%<__transaction_cancel%> within a "
14092 "%<__transaction_relaxed%>");
14093 goto ret_error;
14095 else if (is_outer)
14097 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
14098 && !is_tm_may_cancel_outer (current_function_decl))
14100 error_at (loc, "outer %<__transaction_cancel%> not "
14101 "within outer %<__transaction_atomic%>");
14102 error_at (loc, " or a %<transaction_may_cancel_outer%> function");
14103 goto ret_error;
14106 else if (parser->in_transaction == 0)
14108 error_at (loc, "%<__transaction_cancel%> not within "
14109 "%<__transaction_atomic%>");
14110 goto ret_error;
14113 return add_stmt (build_tm_abort_call (loc, is_outer));
14115 ret_error:
14116 return build1 (NOP_EXPR, void_type_node, error_mark_node);
14119 /* Parse a single source file. */
14121 void
14122 c_parse_file (void)
14124 /* Use local storage to begin. If the first token is a pragma, parse it.
14125 If it is #pragma GCC pch_preprocess, then this will load a PCH file
14126 which will cause garbage collection. */
14127 c_parser tparser;
14129 memset (&tparser, 0, sizeof tparser);
14130 tparser.tokens = &tparser.tokens_buf[0];
14131 the_parser = &tparser;
14133 if (c_parser_peek_token (&tparser)->pragma_kind == PRAGMA_GCC_PCH_PREPROCESS)
14134 c_parser_pragma_pch_preprocess (&tparser);
14136 the_parser = ggc_alloc<c_parser> ();
14137 *the_parser = tparser;
14138 if (tparser.tokens == &tparser.tokens_buf[0])
14139 the_parser->tokens = &the_parser->tokens_buf[0];
14141 /* Initialize EH, if we've been told to do so. */
14142 if (flag_exceptions)
14143 using_eh_for_cleanups ();
14145 c_parser_translation_unit (the_parser);
14146 the_parser = NULL;
14149 /* This function parses Cilk Plus array notation. The starting index is
14150 passed in INITIAL_INDEX and the array name is passes in ARRAY_VALUE. The
14151 return value of this function is a tree_node called VALUE_TREE of type
14152 ARRAY_NOTATION_REF. */
14154 static tree
14155 c_parser_array_notation (location_t loc, c_parser *parser, tree initial_index,
14156 tree array_value)
14158 c_token *token = NULL;
14159 tree start_index = NULL_TREE, end_index = NULL_TREE, stride = NULL_TREE;
14160 tree value_tree = NULL_TREE, type = NULL_TREE, array_type = NULL_TREE;
14161 tree array_type_domain = NULL_TREE;
14163 if (array_value == error_mark_node || initial_index == error_mark_node)
14165 /* No need to continue. If either of these 2 were true, then an error
14166 must be emitted already. Thus, no need to emit them twice. */
14167 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
14168 return error_mark_node;
14171 array_type = TREE_TYPE (array_value);
14172 gcc_assert (array_type);
14173 if (TREE_CODE (array_type) != ARRAY_TYPE
14174 && TREE_CODE (array_type) != POINTER_TYPE)
14176 error_at (loc, "base of array section must be pointer or array type");
14177 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
14178 return error_mark_node;
14180 type = TREE_TYPE (array_type);
14181 token = c_parser_peek_token (parser);
14183 if (token->type == CPP_EOF)
14185 c_parser_error (parser, "expected %<:%> or numeral");
14186 return value_tree;
14188 else if (token->type == CPP_COLON)
14190 if (!initial_index)
14192 /* If we are here, then we have a case like this A[:]. */
14193 c_parser_consume_token (parser);
14194 if (TREE_CODE (array_type) == POINTER_TYPE)
14196 error_at (loc, "start-index and length fields necessary for "
14197 "using array notations in pointers");
14198 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
14199 return error_mark_node;
14201 if (TREE_CODE (array_type) == FUNCTION_TYPE)
14203 error_at (loc, "array notations cannot be used with function "
14204 "type");
14205 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
14206 return error_mark_node;
14208 array_type_domain = TYPE_DOMAIN (array_type);
14210 if (!array_type_domain)
14212 error_at (loc, "start-index and length fields necessary for "
14213 "using array notations in dimensionless arrays");
14214 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
14215 return error_mark_node;
14218 start_index = TYPE_MINVAL (array_type_domain);
14219 start_index = fold_build1 (CONVERT_EXPR, ptrdiff_type_node,
14220 start_index);
14221 if (!TYPE_MAXVAL (array_type_domain)
14222 || !TREE_CONSTANT (TYPE_MAXVAL (array_type_domain)))
14224 error_at (loc, "start-index and length fields necessary for "
14225 "using array notations in variable-length arrays");
14226 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
14227 return error_mark_node;
14229 end_index = TYPE_MAXVAL (array_type_domain);
14230 end_index = fold_build2 (PLUS_EXPR, TREE_TYPE (end_index),
14231 end_index, integer_one_node);
14232 end_index = fold_build1 (CONVERT_EXPR, ptrdiff_type_node, end_index);
14233 stride = build_int_cst (integer_type_node, 1);
14234 stride = fold_build1 (CONVERT_EXPR, ptrdiff_type_node, stride);
14236 else if (initial_index != error_mark_node)
14238 /* If we are here, then there should be 2 possibilities:
14239 1. Array [EXPR : EXPR]
14240 2. Array [EXPR : EXPR : EXPR]
14242 start_index = initial_index;
14244 if (TREE_CODE (array_type) == FUNCTION_TYPE)
14246 error_at (loc, "array notations cannot be used with function "
14247 "type");
14248 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
14249 return error_mark_node;
14251 c_parser_consume_token (parser); /* consume the ':' */
14252 struct c_expr ce = c_parser_expression (parser);
14253 ce = convert_lvalue_to_rvalue (loc, ce, false, false);
14254 end_index = ce.value;
14255 if (!end_index || end_index == error_mark_node)
14257 c_parser_skip_to_end_of_block_or_statement (parser);
14258 return error_mark_node;
14260 if (c_parser_peek_token (parser)->type == CPP_COLON)
14262 c_parser_consume_token (parser);
14263 ce = c_parser_expression (parser);
14264 ce = convert_lvalue_to_rvalue (loc, ce, false, false);
14265 stride = ce.value;
14266 if (!stride || stride == error_mark_node)
14268 c_parser_skip_to_end_of_block_or_statement (parser);
14269 return error_mark_node;
14273 else
14274 c_parser_error (parser, "expected array notation expression");
14276 else
14277 c_parser_error (parser, "expected array notation expression");
14279 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, "expected %<]%>");
14281 value_tree = build_array_notation_ref (loc, array_value, start_index,
14282 end_index, stride, type);
14283 if (value_tree != error_mark_node)
14284 SET_EXPR_LOCATION (value_tree, loc);
14285 return value_tree;
14288 #include "gt-c-c-parser.h"