2013-10-02 Richard Biener <rguenther@suse.de>
[official-gcc.git] / gcc / c / c-parser.c
blobb612e29c852f9848551669471d154a3c0aa322e3
1 /* Parser for C and Objective-C.
2 Copyright (C) 1987-2013 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 "langhooks.h"
44 #include "input.h"
45 #include "cpplib.h"
46 #include "timevar.h"
47 #include "c-family/c-pragma.h"
48 #include "c-tree.h"
49 #include "flags.h"
50 #include "ggc.h"
51 #include "c-family/c-common.h"
52 #include "c-family/c-objc.h"
53 #include "vec.h"
54 #include "target.h"
55 #include "cgraph.h"
56 #include "plugin.h"
59 /* Initialization routine for this file. */
61 void
62 c_parse_init (void)
64 /* The only initialization required is of the reserved word
65 identifiers. */
66 unsigned int i;
67 tree id;
68 int mask = 0;
70 /* Make sure RID_MAX hasn't grown past the 8 bits used to hold the keyword in
71 the c_token structure. */
72 gcc_assert (RID_MAX <= 255);
74 mask |= D_CXXONLY;
75 if (!flag_isoc99)
76 mask |= D_C99;
77 if (flag_no_asm)
79 mask |= D_ASM | D_EXT;
80 if (!flag_isoc99)
81 mask |= D_EXT89;
83 if (!c_dialect_objc ())
84 mask |= D_OBJC | D_CXX_OBJC;
86 ridpointers = ggc_alloc_cleared_vec_tree ((int) RID_MAX);
87 for (i = 0; i < num_c_common_reswords; i++)
89 /* If a keyword is disabled, do not enter it into the table
90 and so create a canonical spelling that isn't a keyword. */
91 if (c_common_reswords[i].disable & mask)
93 if (warn_cxx_compat
94 && (c_common_reswords[i].disable & D_CXXWARN))
96 id = get_identifier (c_common_reswords[i].word);
97 C_SET_RID_CODE (id, RID_CXX_COMPAT_WARN);
98 C_IS_RESERVED_WORD (id) = 1;
100 continue;
103 id = get_identifier (c_common_reswords[i].word);
104 C_SET_RID_CODE (id, c_common_reswords[i].rid);
105 C_IS_RESERVED_WORD (id) = 1;
106 ridpointers [(int) c_common_reswords[i].rid] = id;
110 /* The C lexer intermediates between the lexer in cpplib and c-lex.c
111 and the C parser. Unlike the C++ lexer, the parser structure
112 stores the lexer information instead of using a separate structure.
113 Identifiers are separated into ordinary identifiers, type names,
114 keywords and some other Objective-C types of identifiers, and some
115 look-ahead is maintained.
117 ??? It might be a good idea to lex the whole file up front (as for
118 C++). It would then be possible to share more of the C and C++
119 lexer code, if desired. */
121 /* The following local token type is used. */
123 /* A keyword. */
124 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
126 /* More information about the type of a CPP_NAME token. */
127 typedef enum c_id_kind {
128 /* An ordinary identifier. */
129 C_ID_ID,
130 /* An identifier declared as a typedef name. */
131 C_ID_TYPENAME,
132 /* An identifier declared as an Objective-C class name. */
133 C_ID_CLASSNAME,
134 /* An address space identifier. */
135 C_ID_ADDRSPACE,
136 /* Not an identifier. */
137 C_ID_NONE
138 } c_id_kind;
140 /* A single C token after string literal concatenation and conversion
141 of preprocessing tokens to tokens. */
142 typedef struct GTY (()) c_token {
143 /* The kind of token. */
144 ENUM_BITFIELD (cpp_ttype) type : 8;
145 /* If this token is a CPP_NAME, this value indicates whether also
146 declared as some kind of type. Otherwise, it is C_ID_NONE. */
147 ENUM_BITFIELD (c_id_kind) id_kind : 8;
148 /* If this token is a keyword, this value indicates which keyword.
149 Otherwise, this value is RID_MAX. */
150 ENUM_BITFIELD (rid) keyword : 8;
151 /* If this token is a CPP_PRAGMA, this indicates the pragma that
152 was seen. Otherwise it is PRAGMA_NONE. */
153 ENUM_BITFIELD (pragma_kind) pragma_kind : 8;
154 /* The location at which this token was found. */
155 location_t location;
156 /* The value associated with this token, if any. */
157 tree value;
158 } c_token;
160 /* A parser structure recording information about the state and
161 context of parsing. Includes lexer information with up to two
162 tokens of look-ahead; more are not needed for C. */
163 typedef struct GTY(()) c_parser {
164 /* The look-ahead tokens. */
165 c_token tokens[2];
166 /* How many look-ahead tokens are available (0, 1 or 2). */
167 short tokens_avail;
168 /* True if a syntax error is being recovered from; false otherwise.
169 c_parser_error sets this flag. It should clear this flag when
170 enough tokens have been consumed to recover from the error. */
171 BOOL_BITFIELD error : 1;
172 /* True if we're processing a pragma, and shouldn't automatically
173 consume CPP_PRAGMA_EOL. */
174 BOOL_BITFIELD in_pragma : 1;
175 /* True if we're parsing the outermost block of an if statement. */
176 BOOL_BITFIELD in_if_block : 1;
177 /* True if we want to lex an untranslated string. */
178 BOOL_BITFIELD lex_untranslated_string : 1;
180 /* Objective-C specific parser/lexer information. */
182 /* True if we are in a context where the Objective-C "PQ" keywords
183 are considered keywords. */
184 BOOL_BITFIELD objc_pq_context : 1;
185 /* True if we are parsing a (potential) Objective-C foreach
186 statement. This is set to true after we parsed 'for (' and while
187 we wait for 'in' or ';' to decide if it's a standard C for loop or an
188 Objective-C foreach loop. */
189 BOOL_BITFIELD objc_could_be_foreach_context : 1;
190 /* The following flag is needed to contextualize Objective-C lexical
191 analysis. In some cases (e.g., 'int NSObject;'), it is
192 undesirable to bind an identifier to an Objective-C class, even
193 if a class with that name exists. */
194 BOOL_BITFIELD objc_need_raw_identifier : 1;
195 /* Nonzero if we're processing a __transaction statement. The value
196 is 1 | TM_STMT_ATTR_*. */
197 unsigned int in_transaction : 4;
198 /* True if we are in a context where the Objective-C "Property attribute"
199 keywords are valid. */
200 BOOL_BITFIELD objc_property_attr_context : 1;
201 } c_parser;
204 /* The actual parser and external interface. ??? Does this need to be
205 garbage-collected? */
207 static GTY (()) c_parser *the_parser;
209 /* Read in and lex a single token, storing it in *TOKEN. */
211 static void
212 c_lex_one_token (c_parser *parser, c_token *token)
214 timevar_push (TV_LEX);
216 token->type = c_lex_with_flags (&token->value, &token->location, NULL,
217 (parser->lex_untranslated_string
218 ? C_LEX_STRING_NO_TRANSLATE : 0));
219 token->id_kind = C_ID_NONE;
220 token->keyword = RID_MAX;
221 token->pragma_kind = PRAGMA_NONE;
223 switch (token->type)
225 case CPP_NAME:
227 tree decl;
229 bool objc_force_identifier = parser->objc_need_raw_identifier;
230 if (c_dialect_objc ())
231 parser->objc_need_raw_identifier = false;
233 if (C_IS_RESERVED_WORD (token->value))
235 enum rid rid_code = C_RID_CODE (token->value);
237 if (rid_code == RID_CXX_COMPAT_WARN)
239 warning_at (token->location,
240 OPT_Wc___compat,
241 "identifier %qE conflicts with C++ keyword",
242 token->value);
244 else if (rid_code >= RID_FIRST_ADDR_SPACE
245 && rid_code <= RID_LAST_ADDR_SPACE)
247 token->id_kind = C_ID_ADDRSPACE;
248 token->keyword = rid_code;
249 break;
251 else if (c_dialect_objc () && OBJC_IS_PQ_KEYWORD (rid_code))
253 /* We found an Objective-C "pq" keyword (in, out,
254 inout, bycopy, byref, oneway). They need special
255 care because the interpretation depends on the
256 context. */
257 if (parser->objc_pq_context)
259 token->type = CPP_KEYWORD;
260 token->keyword = rid_code;
261 break;
263 else if (parser->objc_could_be_foreach_context
264 && rid_code == RID_IN)
266 /* We are in Objective-C, inside a (potential)
267 foreach context (which means after having
268 parsed 'for (', but before having parsed ';'),
269 and we found 'in'. We consider it the keyword
270 which terminates the declaration at the
271 beginning of a foreach-statement. Note that
272 this means you can't use 'in' for anything else
273 in that context; in particular, in Objective-C
274 you can't use 'in' as the name of the running
275 variable in a C for loop. We could potentially
276 try to add code here to disambiguate, but it
277 seems a reasonable limitation. */
278 token->type = CPP_KEYWORD;
279 token->keyword = rid_code;
280 break;
282 /* Else, "pq" keywords outside of the "pq" context are
283 not keywords, and we fall through to the code for
284 normal tokens. */
286 else if (c_dialect_objc () && OBJC_IS_PATTR_KEYWORD (rid_code))
288 /* We found an Objective-C "property attribute"
289 keyword (getter, setter, readonly, etc). These are
290 only valid in the property context. */
291 if (parser->objc_property_attr_context)
293 token->type = CPP_KEYWORD;
294 token->keyword = rid_code;
295 break;
297 /* Else they are not special keywords.
300 else if (c_dialect_objc ()
301 && (OBJC_IS_AT_KEYWORD (rid_code)
302 || OBJC_IS_CXX_KEYWORD (rid_code)))
304 /* We found one of the Objective-C "@" keywords (defs,
305 selector, synchronized, etc) or one of the
306 Objective-C "cxx" keywords (class, private,
307 protected, public, try, catch, throw) without a
308 preceding '@' sign. Do nothing and fall through to
309 the code for normal tokens (in C++ we would still
310 consider the CXX ones keywords, but not in C). */
313 else
315 token->type = CPP_KEYWORD;
316 token->keyword = rid_code;
317 break;
321 decl = lookup_name (token->value);
322 if (decl)
324 if (TREE_CODE (decl) == TYPE_DECL)
326 token->id_kind = C_ID_TYPENAME;
327 break;
330 else if (c_dialect_objc ())
332 tree objc_interface_decl = objc_is_class_name (token->value);
333 /* Objective-C class names are in the same namespace as
334 variables and typedefs, and hence are shadowed by local
335 declarations. */
336 if (objc_interface_decl
337 && (!objc_force_identifier || global_bindings_p ()))
339 token->value = objc_interface_decl;
340 token->id_kind = C_ID_CLASSNAME;
341 break;
344 token->id_kind = C_ID_ID;
346 break;
347 case CPP_AT_NAME:
348 /* This only happens in Objective-C; it must be a keyword. */
349 token->type = CPP_KEYWORD;
350 switch (C_RID_CODE (token->value))
352 /* Replace 'class' with '@class', 'private' with '@private',
353 etc. This prevents confusion with the C++ keyword
354 'class', and makes the tokens consistent with other
355 Objective-C 'AT' keywords. For example '@class' is
356 reported as RID_AT_CLASS which is consistent with
357 '@synchronized', which is reported as
358 RID_AT_SYNCHRONIZED.
360 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
361 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
362 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
363 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
364 case RID_THROW: token->keyword = RID_AT_THROW; break;
365 case RID_TRY: token->keyword = RID_AT_TRY; break;
366 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
367 default: token->keyword = C_RID_CODE (token->value);
369 break;
370 case CPP_COLON:
371 case CPP_COMMA:
372 case CPP_CLOSE_PAREN:
373 case CPP_SEMICOLON:
374 /* These tokens may affect the interpretation of any identifiers
375 following, if doing Objective-C. */
376 if (c_dialect_objc ())
377 parser->objc_need_raw_identifier = false;
378 break;
379 case CPP_PRAGMA:
380 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
381 token->pragma_kind = (enum pragma_kind) TREE_INT_CST_LOW (token->value);
382 token->value = NULL;
383 break;
384 default:
385 break;
387 timevar_pop (TV_LEX);
390 /* Return a pointer to the next token from PARSER, reading it in if
391 necessary. */
393 static inline c_token *
394 c_parser_peek_token (c_parser *parser)
396 if (parser->tokens_avail == 0)
398 c_lex_one_token (parser, &parser->tokens[0]);
399 parser->tokens_avail = 1;
401 return &parser->tokens[0];
404 /* Return true if the next token from PARSER has the indicated
405 TYPE. */
407 static inline bool
408 c_parser_next_token_is (c_parser *parser, enum cpp_ttype type)
410 return c_parser_peek_token (parser)->type == type;
413 /* Return true if the next token from PARSER does not have the
414 indicated TYPE. */
416 static inline bool
417 c_parser_next_token_is_not (c_parser *parser, enum cpp_ttype type)
419 return !c_parser_next_token_is (parser, type);
422 /* Return true if the next token from PARSER is the indicated
423 KEYWORD. */
425 static inline bool
426 c_parser_next_token_is_keyword (c_parser *parser, enum rid keyword)
428 return c_parser_peek_token (parser)->keyword == keyword;
431 /* Return a pointer to the next-but-one token from PARSER, reading it
432 in if necessary. The next token is already read in. */
434 static c_token *
435 c_parser_peek_2nd_token (c_parser *parser)
437 if (parser->tokens_avail >= 2)
438 return &parser->tokens[1];
439 gcc_assert (parser->tokens_avail == 1);
440 gcc_assert (parser->tokens[0].type != CPP_EOF);
441 gcc_assert (parser->tokens[0].type != CPP_PRAGMA_EOL);
442 c_lex_one_token (parser, &parser->tokens[1]);
443 parser->tokens_avail = 2;
444 return &parser->tokens[1];
447 /* Return true if TOKEN can start a type name,
448 false otherwise. */
449 static bool
450 c_token_starts_typename (c_token *token)
452 switch (token->type)
454 case CPP_NAME:
455 switch (token->id_kind)
457 case C_ID_ID:
458 return false;
459 case C_ID_ADDRSPACE:
460 return true;
461 case C_ID_TYPENAME:
462 return true;
463 case C_ID_CLASSNAME:
464 gcc_assert (c_dialect_objc ());
465 return true;
466 default:
467 gcc_unreachable ();
469 case CPP_KEYWORD:
470 switch (token->keyword)
472 case RID_UNSIGNED:
473 case RID_LONG:
474 case RID_INT128:
475 case RID_SHORT:
476 case RID_SIGNED:
477 case RID_COMPLEX:
478 case RID_INT:
479 case RID_CHAR:
480 case RID_FLOAT:
481 case RID_DOUBLE:
482 case RID_VOID:
483 case RID_DFLOAT32:
484 case RID_DFLOAT64:
485 case RID_DFLOAT128:
486 case RID_BOOL:
487 case RID_ENUM:
488 case RID_STRUCT:
489 case RID_UNION:
490 case RID_TYPEOF:
491 case RID_CONST:
492 case RID_VOLATILE:
493 case RID_RESTRICT:
494 case RID_ATTRIBUTE:
495 case RID_FRACT:
496 case RID_ACCUM:
497 case RID_SAT:
498 return true;
499 default:
500 return false;
502 case CPP_LESS:
503 if (c_dialect_objc ())
504 return true;
505 return false;
506 default:
507 return false;
511 enum c_lookahead_kind {
512 /* Always treat unknown identifiers as typenames. */
513 cla_prefer_type,
515 /* Could be parsing a nonabstract declarator. Only treat an identifier
516 as a typename if followed by another identifier or a star. */
517 cla_nonabstract_decl,
519 /* Never treat identifiers as typenames. */
520 cla_prefer_id
523 /* Return true if the next token from PARSER can start a type name,
524 false otherwise. LA specifies how to do lookahead in order to
525 detect unknown type names. If unsure, pick CLA_PREFER_ID. */
527 static inline bool
528 c_parser_next_tokens_start_typename (c_parser *parser, enum c_lookahead_kind la)
530 c_token *token = c_parser_peek_token (parser);
531 if (c_token_starts_typename (token))
532 return true;
534 /* Try a bit harder to detect an unknown typename. */
535 if (la != cla_prefer_id
536 && token->type == CPP_NAME
537 && token->id_kind == C_ID_ID
539 /* Do not try too hard when we could have "object in array". */
540 && !parser->objc_could_be_foreach_context
542 && (la == cla_prefer_type
543 || c_parser_peek_2nd_token (parser)->type == CPP_NAME
544 || c_parser_peek_2nd_token (parser)->type == CPP_MULT)
546 /* Only unknown identifiers. */
547 && !lookup_name (token->value))
548 return true;
550 return false;
553 /* Return true if TOKEN is a type qualifier, false otherwise. */
554 static bool
555 c_token_is_qualifier (c_token *token)
557 switch (token->type)
559 case CPP_NAME:
560 switch (token->id_kind)
562 case C_ID_ADDRSPACE:
563 return true;
564 default:
565 return false;
567 case CPP_KEYWORD:
568 switch (token->keyword)
570 case RID_CONST:
571 case RID_VOLATILE:
572 case RID_RESTRICT:
573 case RID_ATTRIBUTE:
574 return true;
575 default:
576 return false;
578 case CPP_LESS:
579 return false;
580 default:
581 gcc_unreachable ();
585 /* Return true if the next token from PARSER is a type qualifier,
586 false otherwise. */
587 static inline bool
588 c_parser_next_token_is_qualifier (c_parser *parser)
590 c_token *token = c_parser_peek_token (parser);
591 return c_token_is_qualifier (token);
594 /* Return true if TOKEN can start declaration specifiers, false
595 otherwise. */
596 static bool
597 c_token_starts_declspecs (c_token *token)
599 switch (token->type)
601 case CPP_NAME:
602 switch (token->id_kind)
604 case C_ID_ID:
605 return false;
606 case C_ID_ADDRSPACE:
607 return true;
608 case C_ID_TYPENAME:
609 return true;
610 case C_ID_CLASSNAME:
611 gcc_assert (c_dialect_objc ());
612 return true;
613 default:
614 gcc_unreachable ();
616 case CPP_KEYWORD:
617 switch (token->keyword)
619 case RID_STATIC:
620 case RID_EXTERN:
621 case RID_REGISTER:
622 case RID_TYPEDEF:
623 case RID_INLINE:
624 case RID_NORETURN:
625 case RID_AUTO:
626 case RID_THREAD:
627 case RID_UNSIGNED:
628 case RID_LONG:
629 case RID_INT128:
630 case RID_SHORT:
631 case RID_SIGNED:
632 case RID_COMPLEX:
633 case RID_INT:
634 case RID_CHAR:
635 case RID_FLOAT:
636 case RID_DOUBLE:
637 case RID_VOID:
638 case RID_DFLOAT32:
639 case RID_DFLOAT64:
640 case RID_DFLOAT128:
641 case RID_BOOL:
642 case RID_ENUM:
643 case RID_STRUCT:
644 case RID_UNION:
645 case RID_TYPEOF:
646 case RID_CONST:
647 case RID_VOLATILE:
648 case RID_RESTRICT:
649 case RID_ATTRIBUTE:
650 case RID_FRACT:
651 case RID_ACCUM:
652 case RID_SAT:
653 case RID_ALIGNAS:
654 return true;
655 default:
656 return false;
658 case CPP_LESS:
659 if (c_dialect_objc ())
660 return true;
661 return false;
662 default:
663 return false;
668 /* Return true if TOKEN can start declaration specifiers or a static
669 assertion, false otherwise. */
670 static bool
671 c_token_starts_declaration (c_token *token)
673 if (c_token_starts_declspecs (token)
674 || token->keyword == RID_STATIC_ASSERT)
675 return true;
676 else
677 return false;
680 /* Return true if the next token from PARSER can start declaration
681 specifiers, false otherwise. */
682 static inline bool
683 c_parser_next_token_starts_declspecs (c_parser *parser)
685 c_token *token = c_parser_peek_token (parser);
687 /* In Objective-C, a classname normally starts a declspecs unless it
688 is immediately followed by a dot. In that case, it is the
689 Objective-C 2.0 "dot-syntax" for class objects, ie, calls the
690 setter/getter on the class. c_token_starts_declspecs() can't
691 differentiate between the two cases because it only checks the
692 current token, so we have a special check here. */
693 if (c_dialect_objc ()
694 && token->type == CPP_NAME
695 && token->id_kind == C_ID_CLASSNAME
696 && c_parser_peek_2nd_token (parser)->type == CPP_DOT)
697 return false;
699 return c_token_starts_declspecs (token);
702 /* Return true if the next tokens from PARSER can start declaration
703 specifiers or a static assertion, false otherwise. */
704 static inline bool
705 c_parser_next_tokens_start_declaration (c_parser *parser)
707 c_token *token = c_parser_peek_token (parser);
709 /* Same as above. */
710 if (c_dialect_objc ()
711 && token->type == CPP_NAME
712 && token->id_kind == C_ID_CLASSNAME
713 && c_parser_peek_2nd_token (parser)->type == CPP_DOT)
714 return false;
716 /* Labels do not start declarations. */
717 if (token->type == CPP_NAME
718 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
719 return false;
721 if (c_token_starts_declaration (token))
722 return true;
724 if (c_parser_next_tokens_start_typename (parser, cla_nonabstract_decl))
725 return true;
727 return false;
730 /* Consume the next token from PARSER. */
732 static void
733 c_parser_consume_token (c_parser *parser)
735 gcc_assert (parser->tokens_avail >= 1);
736 gcc_assert (parser->tokens[0].type != CPP_EOF);
737 gcc_assert (!parser->in_pragma || parser->tokens[0].type != CPP_PRAGMA_EOL);
738 gcc_assert (parser->error || parser->tokens[0].type != CPP_PRAGMA);
739 if (parser->tokens_avail == 2)
740 parser->tokens[0] = parser->tokens[1];
741 parser->tokens_avail--;
744 /* Expect the current token to be a #pragma. Consume it and remember
745 that we've begun parsing a pragma. */
747 static void
748 c_parser_consume_pragma (c_parser *parser)
750 gcc_assert (!parser->in_pragma);
751 gcc_assert (parser->tokens_avail >= 1);
752 gcc_assert (parser->tokens[0].type == CPP_PRAGMA);
753 if (parser->tokens_avail == 2)
754 parser->tokens[0] = parser->tokens[1];
755 parser->tokens_avail--;
756 parser->in_pragma = true;
759 /* Update the globals input_location and in_system_header from
760 TOKEN. */
761 static inline void
762 c_parser_set_source_position_from_token (c_token *token)
764 if (token->type != CPP_EOF)
766 input_location = token->location;
770 /* Issue a diagnostic of the form
771 FILE:LINE: MESSAGE before TOKEN
772 where TOKEN is the next token in the input stream of PARSER.
773 MESSAGE (specified by the caller) is usually of the form "expected
774 OTHER-TOKEN".
776 Do not issue a diagnostic if still recovering from an error.
778 ??? This is taken from the C++ parser, but building up messages in
779 this way is not i18n-friendly and some other approach should be
780 used. */
782 static void
783 c_parser_error (c_parser *parser, const char *gmsgid)
785 c_token *token = c_parser_peek_token (parser);
786 if (parser->error)
787 return;
788 parser->error = true;
789 if (!gmsgid)
790 return;
791 /* This diagnostic makes more sense if it is tagged to the line of
792 the token we just peeked at. */
793 c_parser_set_source_position_from_token (token);
794 c_parse_error (gmsgid,
795 /* Because c_parse_error does not understand
796 CPP_KEYWORD, keywords are treated like
797 identifiers. */
798 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
799 /* ??? The C parser does not save the cpp flags of a
800 token, we need to pass 0 here and we will not get
801 the source spelling of some tokens but rather the
802 canonical spelling. */
803 token->value, /*flags=*/0);
806 /* If the next token is of the indicated TYPE, consume it. Otherwise,
807 issue the error MSGID. If MSGID is NULL then a message has already
808 been produced and no message will be produced this time. Returns
809 true if found, false otherwise. */
811 static bool
812 c_parser_require (c_parser *parser,
813 enum cpp_ttype type,
814 const char *msgid)
816 if (c_parser_next_token_is (parser, type))
818 c_parser_consume_token (parser);
819 return true;
821 else
823 c_parser_error (parser, msgid);
824 return false;
828 /* If the next token is the indicated keyword, consume it. Otherwise,
829 issue the error MSGID. Returns true if found, false otherwise. */
831 static bool
832 c_parser_require_keyword (c_parser *parser,
833 enum rid keyword,
834 const char *msgid)
836 if (c_parser_next_token_is_keyword (parser, keyword))
838 c_parser_consume_token (parser);
839 return true;
841 else
843 c_parser_error (parser, msgid);
844 return false;
848 /* Like c_parser_require, except that tokens will be skipped until the
849 desired token is found. An error message is still produced if the
850 next token is not as expected. If MSGID is NULL then a message has
851 already been produced and no message will be produced this
852 time. */
854 static void
855 c_parser_skip_until_found (c_parser *parser,
856 enum cpp_ttype type,
857 const char *msgid)
859 unsigned nesting_depth = 0;
861 if (c_parser_require (parser, type, msgid))
862 return;
864 /* Skip tokens until the desired token is found. */
865 while (true)
867 /* Peek at the next token. */
868 c_token *token = c_parser_peek_token (parser);
869 /* If we've reached the token we want, consume it and stop. */
870 if (token->type == type && !nesting_depth)
872 c_parser_consume_token (parser);
873 break;
876 /* If we've run out of tokens, stop. */
877 if (token->type == CPP_EOF)
878 return;
879 if (token->type == CPP_PRAGMA_EOL && parser->in_pragma)
880 return;
881 if (token->type == CPP_OPEN_BRACE
882 || token->type == CPP_OPEN_PAREN
883 || token->type == CPP_OPEN_SQUARE)
884 ++nesting_depth;
885 else if (token->type == CPP_CLOSE_BRACE
886 || token->type == CPP_CLOSE_PAREN
887 || token->type == CPP_CLOSE_SQUARE)
889 if (nesting_depth-- == 0)
890 break;
892 /* Consume this token. */
893 c_parser_consume_token (parser);
895 parser->error = false;
898 /* Skip tokens until the end of a parameter is found, but do not
899 consume the comma, semicolon or closing delimiter. */
901 static void
902 c_parser_skip_to_end_of_parameter (c_parser *parser)
904 unsigned nesting_depth = 0;
906 while (true)
908 c_token *token = c_parser_peek_token (parser);
909 if ((token->type == CPP_COMMA || token->type == CPP_SEMICOLON)
910 && !nesting_depth)
911 break;
912 /* If we've run out of tokens, stop. */
913 if (token->type == CPP_EOF)
914 return;
915 if (token->type == CPP_PRAGMA_EOL && parser->in_pragma)
916 return;
917 if (token->type == CPP_OPEN_BRACE
918 || token->type == CPP_OPEN_PAREN
919 || token->type == CPP_OPEN_SQUARE)
920 ++nesting_depth;
921 else if (token->type == CPP_CLOSE_BRACE
922 || token->type == CPP_CLOSE_PAREN
923 || token->type == CPP_CLOSE_SQUARE)
925 if (nesting_depth-- == 0)
926 break;
928 /* Consume this token. */
929 c_parser_consume_token (parser);
931 parser->error = false;
934 /* Expect to be at the end of the pragma directive and consume an
935 end of line marker. */
937 static void
938 c_parser_skip_to_pragma_eol (c_parser *parser)
940 gcc_assert (parser->in_pragma);
941 parser->in_pragma = false;
943 if (!c_parser_require (parser, CPP_PRAGMA_EOL, "expected end of line"))
944 while (true)
946 c_token *token = c_parser_peek_token (parser);
947 if (token->type == CPP_EOF)
948 break;
949 if (token->type == CPP_PRAGMA_EOL)
951 c_parser_consume_token (parser);
952 break;
954 c_parser_consume_token (parser);
957 parser->error = false;
960 /* Skip tokens until we have consumed an entire block, or until we
961 have consumed a non-nested ';'. */
963 static void
964 c_parser_skip_to_end_of_block_or_statement (c_parser *parser)
966 unsigned nesting_depth = 0;
967 bool save_error = parser->error;
969 while (true)
971 c_token *token;
973 /* Peek at the next token. */
974 token = c_parser_peek_token (parser);
976 switch (token->type)
978 case CPP_EOF:
979 return;
981 case CPP_PRAGMA_EOL:
982 if (parser->in_pragma)
983 return;
984 break;
986 case CPP_SEMICOLON:
987 /* If the next token is a ';', we have reached the
988 end of the statement. */
989 if (!nesting_depth)
991 /* Consume the ';'. */
992 c_parser_consume_token (parser);
993 goto finished;
995 break;
997 case CPP_CLOSE_BRACE:
998 /* If the next token is a non-nested '}', then we have
999 reached the end of the current block. */
1000 if (nesting_depth == 0 || --nesting_depth == 0)
1002 c_parser_consume_token (parser);
1003 goto finished;
1005 break;
1007 case CPP_OPEN_BRACE:
1008 /* If it the next token is a '{', then we are entering a new
1009 block. Consume the entire block. */
1010 ++nesting_depth;
1011 break;
1013 case CPP_PRAGMA:
1014 /* If we see a pragma, consume the whole thing at once. We
1015 have some safeguards against consuming pragmas willy-nilly.
1016 Normally, we'd expect to be here with parser->error set,
1017 which disables these safeguards. But it's possible to get
1018 here for secondary error recovery, after parser->error has
1019 been cleared. */
1020 c_parser_consume_pragma (parser);
1021 c_parser_skip_to_pragma_eol (parser);
1022 parser->error = save_error;
1023 continue;
1025 default:
1026 break;
1029 c_parser_consume_token (parser);
1032 finished:
1033 parser->error = false;
1036 /* CPP's options (initialized by c-opts.c). */
1037 extern cpp_options *cpp_opts;
1039 /* Save the warning flags which are controlled by __extension__. */
1041 static inline int
1042 disable_extension_diagnostics (void)
1044 int ret = (pedantic
1045 | (warn_pointer_arith << 1)
1046 | (warn_traditional << 2)
1047 | (flag_iso << 3)
1048 | (warn_long_long << 4)
1049 | (warn_cxx_compat << 5)
1050 | (warn_overlength_strings << 6));
1051 cpp_opts->cpp_pedantic = pedantic = 0;
1052 warn_pointer_arith = 0;
1053 cpp_opts->cpp_warn_traditional = warn_traditional = 0;
1054 flag_iso = 0;
1055 cpp_opts->cpp_warn_long_long = warn_long_long = 0;
1056 warn_cxx_compat = 0;
1057 warn_overlength_strings = 0;
1058 return ret;
1061 /* Restore the warning flags which are controlled by __extension__.
1062 FLAGS is the return value from disable_extension_diagnostics. */
1064 static inline void
1065 restore_extension_diagnostics (int flags)
1067 cpp_opts->cpp_pedantic = pedantic = flags & 1;
1068 warn_pointer_arith = (flags >> 1) & 1;
1069 cpp_opts->cpp_warn_traditional = warn_traditional = (flags >> 2) & 1;
1070 flag_iso = (flags >> 3) & 1;
1071 cpp_opts->cpp_warn_long_long = warn_long_long = (flags >> 4) & 1;
1072 warn_cxx_compat = (flags >> 5) & 1;
1073 warn_overlength_strings = (flags >> 6) & 1;
1076 /* Possibly kinds of declarator to parse. */
1077 typedef enum c_dtr_syn {
1078 /* A normal declarator with an identifier. */
1079 C_DTR_NORMAL,
1080 /* An abstract declarator (maybe empty). */
1081 C_DTR_ABSTRACT,
1082 /* A parameter declarator: may be either, but after a type name does
1083 not redeclare a typedef name as an identifier if it can
1084 alternatively be interpreted as a typedef name; see DR#009,
1085 applied in C90 TC1, omitted from C99 and reapplied in C99 TC2
1086 following DR#249. For example, given a typedef T, "int T" and
1087 "int *T" are valid parameter declarations redeclaring T, while
1088 "int (T)" and "int * (T)" and "int (T[])" and "int (T (int))" are
1089 abstract declarators rather than involving redundant parentheses;
1090 the same applies with attributes inside the parentheses before
1091 "T". */
1092 C_DTR_PARM
1093 } c_dtr_syn;
1095 /* The binary operation precedence levels, where 0 is a dummy lowest level
1096 used for the bottom of the stack. */
1097 enum c_parser_prec {
1098 PREC_NONE,
1099 PREC_LOGOR,
1100 PREC_LOGAND,
1101 PREC_BITOR,
1102 PREC_BITXOR,
1103 PREC_BITAND,
1104 PREC_EQ,
1105 PREC_REL,
1106 PREC_SHIFT,
1107 PREC_ADD,
1108 PREC_MULT,
1109 NUM_PRECS
1112 static void c_parser_external_declaration (c_parser *);
1113 static void c_parser_asm_definition (c_parser *);
1114 static void c_parser_declaration_or_fndef (c_parser *, bool, bool, bool,
1115 bool, bool, tree *);
1116 static void c_parser_static_assert_declaration_no_semi (c_parser *);
1117 static void c_parser_static_assert_declaration (c_parser *);
1118 static void c_parser_declspecs (c_parser *, struct c_declspecs *, bool, bool,
1119 bool, enum c_lookahead_kind);
1120 static struct c_typespec c_parser_enum_specifier (c_parser *);
1121 static struct c_typespec c_parser_struct_or_union_specifier (c_parser *);
1122 static tree c_parser_struct_declaration (c_parser *);
1123 static struct c_typespec c_parser_typeof_specifier (c_parser *);
1124 static tree c_parser_alignas_specifier (c_parser *);
1125 static struct c_declarator *c_parser_declarator (c_parser *, bool, c_dtr_syn,
1126 bool *);
1127 static struct c_declarator *c_parser_direct_declarator (c_parser *, bool,
1128 c_dtr_syn, bool *);
1129 static struct c_declarator *c_parser_direct_declarator_inner (c_parser *,
1130 bool,
1131 struct c_declarator *);
1132 static struct c_arg_info *c_parser_parms_declarator (c_parser *, bool, tree);
1133 static struct c_arg_info *c_parser_parms_list_declarator (c_parser *, tree,
1134 tree);
1135 static struct c_parm *c_parser_parameter_declaration (c_parser *, tree);
1136 static tree c_parser_simple_asm_expr (c_parser *);
1137 static tree c_parser_attributes (c_parser *);
1138 static struct c_type_name *c_parser_type_name (c_parser *);
1139 static struct c_expr c_parser_initializer (c_parser *);
1140 static struct c_expr c_parser_braced_init (c_parser *, tree, bool);
1141 static void c_parser_initelt (c_parser *, struct obstack *);
1142 static void c_parser_initval (c_parser *, struct c_expr *,
1143 struct obstack *);
1144 static tree c_parser_compound_statement (c_parser *);
1145 static void c_parser_compound_statement_nostart (c_parser *);
1146 static void c_parser_label (c_parser *);
1147 static void c_parser_statement (c_parser *);
1148 static void c_parser_statement_after_labels (c_parser *);
1149 static void c_parser_if_statement (c_parser *);
1150 static void c_parser_switch_statement (c_parser *);
1151 static void c_parser_while_statement (c_parser *);
1152 static void c_parser_do_statement (c_parser *);
1153 static void c_parser_for_statement (c_parser *);
1154 static tree c_parser_asm_statement (c_parser *);
1155 static tree c_parser_asm_operands (c_parser *);
1156 static tree c_parser_asm_goto_operands (c_parser *);
1157 static tree c_parser_asm_clobbers (c_parser *);
1158 static struct c_expr c_parser_expr_no_commas (c_parser *, struct c_expr *);
1159 static struct c_expr c_parser_conditional_expression (c_parser *,
1160 struct c_expr *);
1161 static struct c_expr c_parser_binary_expression (c_parser *, struct c_expr *,
1162 enum c_parser_prec);
1163 static struct c_expr c_parser_cast_expression (c_parser *, struct c_expr *);
1164 static struct c_expr c_parser_unary_expression (c_parser *);
1165 static struct c_expr c_parser_sizeof_expression (c_parser *);
1166 static struct c_expr c_parser_alignof_expression (c_parser *);
1167 static struct c_expr c_parser_postfix_expression (c_parser *);
1168 static struct c_expr c_parser_postfix_expression_after_paren_type (c_parser *,
1169 struct c_type_name *,
1170 location_t);
1171 static struct c_expr c_parser_postfix_expression_after_primary (c_parser *,
1172 location_t loc,
1173 struct c_expr);
1174 static tree c_parser_transaction (c_parser *, enum rid);
1175 static struct c_expr c_parser_transaction_expression (c_parser *, enum rid);
1176 static tree c_parser_transaction_cancel (c_parser *);
1177 static struct c_expr c_parser_expression (c_parser *);
1178 static struct c_expr c_parser_expression_conv (c_parser *);
1179 static vec<tree, va_gc> *c_parser_expr_list (c_parser *, bool, bool,
1180 vec<tree, va_gc> **, location_t *,
1181 tree *);
1182 static void c_parser_omp_construct (c_parser *);
1183 static void c_parser_omp_threadprivate (c_parser *);
1184 static void c_parser_omp_barrier (c_parser *);
1185 static void c_parser_omp_flush (c_parser *);
1186 static void c_parser_omp_taskwait (c_parser *);
1187 static void c_parser_omp_taskyield (c_parser *);
1189 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
1190 static bool c_parser_pragma (c_parser *, enum pragma_context);
1192 /* These Objective-C parser functions are only ever called when
1193 compiling Objective-C. */
1194 static void c_parser_objc_class_definition (c_parser *, tree);
1195 static void c_parser_objc_class_instance_variables (c_parser *);
1196 static void c_parser_objc_class_declaration (c_parser *);
1197 static void c_parser_objc_alias_declaration (c_parser *);
1198 static void c_parser_objc_protocol_definition (c_parser *, tree);
1199 static bool c_parser_objc_method_type (c_parser *);
1200 static void c_parser_objc_method_definition (c_parser *);
1201 static void c_parser_objc_methodprotolist (c_parser *);
1202 static void c_parser_objc_methodproto (c_parser *);
1203 static tree c_parser_objc_method_decl (c_parser *, bool, tree *, tree *);
1204 static tree c_parser_objc_type_name (c_parser *);
1205 static tree c_parser_objc_protocol_refs (c_parser *);
1206 static void c_parser_objc_try_catch_finally_statement (c_parser *);
1207 static void c_parser_objc_synchronized_statement (c_parser *);
1208 static tree c_parser_objc_selector (c_parser *);
1209 static tree c_parser_objc_selector_arg (c_parser *);
1210 static tree c_parser_objc_receiver (c_parser *);
1211 static tree c_parser_objc_message_args (c_parser *);
1212 static tree c_parser_objc_keywordexpr (c_parser *);
1213 static void c_parser_objc_at_property_declaration (c_parser *);
1214 static void c_parser_objc_at_synthesize_declaration (c_parser *);
1215 static void c_parser_objc_at_dynamic_declaration (c_parser *);
1216 static bool c_parser_objc_diagnose_bad_element_prefix
1217 (c_parser *, struct c_declspecs *);
1219 static tree c_parser_array_notation (location_t, c_parser *, tree, tree);
1221 /* Parse a translation unit (C90 6.7, C99 6.9).
1223 translation-unit:
1224 external-declarations
1226 external-declarations:
1227 external-declaration
1228 external-declarations external-declaration
1230 GNU extensions:
1232 translation-unit:
1233 empty
1236 static void
1237 c_parser_translation_unit (c_parser *parser)
1239 if (c_parser_next_token_is (parser, CPP_EOF))
1241 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
1242 "ISO C forbids an empty translation unit");
1244 else
1246 void *obstack_position = obstack_alloc (&parser_obstack, 0);
1247 mark_valid_location_for_stdc_pragma (false);
1250 ggc_collect ();
1251 c_parser_external_declaration (parser);
1252 obstack_free (&parser_obstack, obstack_position);
1254 while (c_parser_next_token_is_not (parser, CPP_EOF));
1258 /* Parse an external declaration (C90 6.7, C99 6.9).
1260 external-declaration:
1261 function-definition
1262 declaration
1264 GNU extensions:
1266 external-declaration:
1267 asm-definition
1269 __extension__ external-declaration
1271 Objective-C:
1273 external-declaration:
1274 objc-class-definition
1275 objc-class-declaration
1276 objc-alias-declaration
1277 objc-protocol-definition
1278 objc-method-definition
1279 @end
1282 static void
1283 c_parser_external_declaration (c_parser *parser)
1285 int ext;
1286 switch (c_parser_peek_token (parser)->type)
1288 case CPP_KEYWORD:
1289 switch (c_parser_peek_token (parser)->keyword)
1291 case RID_EXTENSION:
1292 ext = disable_extension_diagnostics ();
1293 c_parser_consume_token (parser);
1294 c_parser_external_declaration (parser);
1295 restore_extension_diagnostics (ext);
1296 break;
1297 case RID_ASM:
1298 c_parser_asm_definition (parser);
1299 break;
1300 case RID_AT_INTERFACE:
1301 case RID_AT_IMPLEMENTATION:
1302 gcc_assert (c_dialect_objc ());
1303 c_parser_objc_class_definition (parser, NULL_TREE);
1304 break;
1305 case RID_AT_CLASS:
1306 gcc_assert (c_dialect_objc ());
1307 c_parser_objc_class_declaration (parser);
1308 break;
1309 case RID_AT_ALIAS:
1310 gcc_assert (c_dialect_objc ());
1311 c_parser_objc_alias_declaration (parser);
1312 break;
1313 case RID_AT_PROTOCOL:
1314 gcc_assert (c_dialect_objc ());
1315 c_parser_objc_protocol_definition (parser, NULL_TREE);
1316 break;
1317 case RID_AT_PROPERTY:
1318 gcc_assert (c_dialect_objc ());
1319 c_parser_objc_at_property_declaration (parser);
1320 break;
1321 case RID_AT_SYNTHESIZE:
1322 gcc_assert (c_dialect_objc ());
1323 c_parser_objc_at_synthesize_declaration (parser);
1324 break;
1325 case RID_AT_DYNAMIC:
1326 gcc_assert (c_dialect_objc ());
1327 c_parser_objc_at_dynamic_declaration (parser);
1328 break;
1329 case RID_AT_END:
1330 gcc_assert (c_dialect_objc ());
1331 c_parser_consume_token (parser);
1332 objc_finish_implementation ();
1333 break;
1334 default:
1335 goto decl_or_fndef;
1337 break;
1338 case CPP_SEMICOLON:
1339 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
1340 "ISO C does not allow extra %<;%> outside of a function");
1341 c_parser_consume_token (parser);
1342 break;
1343 case CPP_PRAGMA:
1344 mark_valid_location_for_stdc_pragma (true);
1345 c_parser_pragma (parser, pragma_external);
1346 mark_valid_location_for_stdc_pragma (false);
1347 break;
1348 case CPP_PLUS:
1349 case CPP_MINUS:
1350 if (c_dialect_objc ())
1352 c_parser_objc_method_definition (parser);
1353 break;
1355 /* Else fall through, and yield a syntax error trying to parse
1356 as a declaration or function definition. */
1357 default:
1358 decl_or_fndef:
1359 /* A declaration or a function definition (or, in Objective-C,
1360 an @interface or @protocol with prefix attributes). We can
1361 only tell which after parsing the declaration specifiers, if
1362 any, and the first declarator. */
1363 c_parser_declaration_or_fndef (parser, true, true, true, false, true, NULL);
1364 break;
1368 /* Parse a declaration or function definition (C90 6.5, 6.7.1, C99
1369 6.7, 6.9.1). If FNDEF_OK is true, a function definition is
1370 accepted; otherwise (old-style parameter declarations) only other
1371 declarations are accepted. If STATIC_ASSERT_OK is true, a static
1372 assertion is accepted; otherwise (old-style parameter declarations)
1373 it is not. If NESTED is true, we are inside a function or parsing
1374 old-style parameter declarations; any functions encountered are
1375 nested functions and declaration specifiers are required; otherwise
1376 we are at top level and functions are normal functions and
1377 declaration specifiers may be optional. If EMPTY_OK is true, empty
1378 declarations are OK (subject to all other constraints); otherwise
1379 (old-style parameter declarations) they are diagnosed. If
1380 START_ATTR_OK is true, the declaration specifiers may start with
1381 attributes; otherwise they may not.
1382 OBJC_FOREACH_OBJECT_DECLARATION can be used to get back the parsed
1383 declaration when parsing an Objective-C foreach statement.
1385 declaration:
1386 declaration-specifiers init-declarator-list[opt] ;
1387 static_assert-declaration
1389 function-definition:
1390 declaration-specifiers[opt] declarator declaration-list[opt]
1391 compound-statement
1393 declaration-list:
1394 declaration
1395 declaration-list declaration
1397 init-declarator-list:
1398 init-declarator
1399 init-declarator-list , init-declarator
1401 init-declarator:
1402 declarator simple-asm-expr[opt] attributes[opt]
1403 declarator simple-asm-expr[opt] attributes[opt] = initializer
1405 GNU extensions:
1407 nested-function-definition:
1408 declaration-specifiers declarator declaration-list[opt]
1409 compound-statement
1411 Objective-C:
1412 attributes objc-class-definition
1413 attributes objc-category-definition
1414 attributes objc-protocol-definition
1416 The simple-asm-expr and attributes are GNU extensions.
1418 This function does not handle __extension__; that is handled in its
1419 callers. ??? Following the old parser, __extension__ may start
1420 external declarations, declarations in functions and declarations
1421 at the start of "for" loops, but not old-style parameter
1422 declarations.
1424 C99 requires declaration specifiers in a function definition; the
1425 absence is diagnosed through the diagnosis of implicit int. In GNU
1426 C we also allow but diagnose declarations without declaration
1427 specifiers, but only at top level (elsewhere they conflict with
1428 other syntax).
1430 In Objective-C, declarations of the looping variable in a foreach
1431 statement are exceptionally terminated by 'in' (for example, 'for
1432 (NSObject *object in array) { ... }').
1434 OpenMP:
1436 declaration:
1437 threadprivate-directive */
1439 static void
1440 c_parser_declaration_or_fndef (c_parser *parser, bool fndef_ok,
1441 bool static_assert_ok, bool empty_ok,
1442 bool nested, bool start_attr_ok,
1443 tree *objc_foreach_object_declaration)
1445 struct c_declspecs *specs;
1446 tree prefix_attrs;
1447 tree all_prefix_attrs;
1448 bool diagnosed_no_specs = false;
1449 location_t here = c_parser_peek_token (parser)->location;
1451 if (static_assert_ok
1452 && c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT))
1454 c_parser_static_assert_declaration (parser);
1455 return;
1457 specs = build_null_declspecs ();
1459 /* Try to detect an unknown type name when we have "A B" or "A *B". */
1460 if (c_parser_peek_token (parser)->type == CPP_NAME
1461 && c_parser_peek_token (parser)->id_kind == C_ID_ID
1462 && (c_parser_peek_2nd_token (parser)->type == CPP_NAME
1463 || c_parser_peek_2nd_token (parser)->type == CPP_MULT)
1464 && (!nested || !lookup_name (c_parser_peek_token (parser)->value)))
1466 error_at (here, "unknown type name %qE",
1467 c_parser_peek_token (parser)->value);
1469 /* Parse declspecs normally to get a correct pointer type, but avoid
1470 a further "fails to be a type name" error. Refuse nested functions
1471 since it is not how the user likely wants us to recover. */
1472 c_parser_peek_token (parser)->type = CPP_KEYWORD;
1473 c_parser_peek_token (parser)->keyword = RID_VOID;
1474 c_parser_peek_token (parser)->value = error_mark_node;
1475 fndef_ok = !nested;
1478 c_parser_declspecs (parser, specs, true, true, start_attr_ok, cla_nonabstract_decl);
1479 if (parser->error)
1481 c_parser_skip_to_end_of_block_or_statement (parser);
1482 return;
1484 if (nested && !specs->declspecs_seen_p)
1486 c_parser_error (parser, "expected declaration specifiers");
1487 c_parser_skip_to_end_of_block_or_statement (parser);
1488 return;
1490 finish_declspecs (specs);
1491 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1493 if (empty_ok)
1494 shadow_tag (specs);
1495 else
1497 shadow_tag_warned (specs, 1);
1498 pedwarn (here, 0, "empty declaration");
1500 c_parser_consume_token (parser);
1501 return;
1504 /* Provide better error recovery. Note that a type name here is usually
1505 better diagnosed as a redeclaration. */
1506 if (empty_ok
1507 && specs->typespec_kind == ctsk_tagdef
1508 && c_parser_next_token_starts_declspecs (parser)
1509 && !c_parser_next_token_is (parser, CPP_NAME))
1511 c_parser_error (parser, "expected %<;%>, identifier or %<(%>");
1512 parser->error = false;
1513 shadow_tag_warned (specs, 1);
1514 return;
1516 else if (c_dialect_objc ())
1518 /* Prefix attributes are an error on method decls. */
1519 switch (c_parser_peek_token (parser)->type)
1521 case CPP_PLUS:
1522 case CPP_MINUS:
1523 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1524 return;
1525 if (specs->attrs)
1527 warning_at (c_parser_peek_token (parser)->location,
1528 OPT_Wattributes,
1529 "prefix attributes are ignored for methods");
1530 specs->attrs = NULL_TREE;
1532 if (fndef_ok)
1533 c_parser_objc_method_definition (parser);
1534 else
1535 c_parser_objc_methodproto (parser);
1536 return;
1537 break;
1538 default:
1539 break;
1541 /* This is where we parse 'attributes @interface ...',
1542 'attributes @implementation ...', 'attributes @protocol ...'
1543 (where attributes could be, for example, __attribute__
1544 ((deprecated)).
1546 switch (c_parser_peek_token (parser)->keyword)
1548 case RID_AT_INTERFACE:
1550 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1551 return;
1552 c_parser_objc_class_definition (parser, specs->attrs);
1553 return;
1555 break;
1556 case RID_AT_IMPLEMENTATION:
1558 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1559 return;
1560 if (specs->attrs)
1562 warning_at (c_parser_peek_token (parser)->location,
1563 OPT_Wattributes,
1564 "prefix attributes are ignored for implementations");
1565 specs->attrs = NULL_TREE;
1567 c_parser_objc_class_definition (parser, NULL_TREE);
1568 return;
1570 break;
1571 case RID_AT_PROTOCOL:
1573 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1574 return;
1575 c_parser_objc_protocol_definition (parser, specs->attrs);
1576 return;
1578 break;
1579 case RID_AT_ALIAS:
1580 case RID_AT_CLASS:
1581 case RID_AT_END:
1582 case RID_AT_PROPERTY:
1583 if (specs->attrs)
1585 c_parser_error (parser, "unexpected attribute");
1586 specs->attrs = NULL;
1588 break;
1589 default:
1590 break;
1594 pending_xref_error ();
1595 prefix_attrs = specs->attrs;
1596 all_prefix_attrs = prefix_attrs;
1597 specs->attrs = NULL_TREE;
1598 while (true)
1600 struct c_declarator *declarator;
1601 bool dummy = false;
1602 timevar_id_t tv;
1603 tree fnbody;
1604 /* Declaring either one or more declarators (in which case we
1605 should diagnose if there were no declaration specifiers) or a
1606 function definition (in which case the diagnostic for
1607 implicit int suffices). */
1608 declarator = c_parser_declarator (parser,
1609 specs->typespec_kind != ctsk_none,
1610 C_DTR_NORMAL, &dummy);
1611 if (declarator == NULL)
1613 c_parser_skip_to_end_of_block_or_statement (parser);
1614 return;
1616 if (c_parser_next_token_is (parser, CPP_EQ)
1617 || c_parser_next_token_is (parser, CPP_COMMA)
1618 || c_parser_next_token_is (parser, CPP_SEMICOLON)
1619 || c_parser_next_token_is_keyword (parser, RID_ASM)
1620 || c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE)
1621 || c_parser_next_token_is_keyword (parser, RID_IN))
1623 tree asm_name = NULL_TREE;
1624 tree postfix_attrs = NULL_TREE;
1625 if (!diagnosed_no_specs && !specs->declspecs_seen_p)
1627 diagnosed_no_specs = true;
1628 pedwarn (here, 0, "data definition has no type or storage class");
1630 /* Having seen a data definition, there cannot now be a
1631 function definition. */
1632 fndef_ok = false;
1633 if (c_parser_next_token_is_keyword (parser, RID_ASM))
1634 asm_name = c_parser_simple_asm_expr (parser);
1635 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
1636 postfix_attrs = c_parser_attributes (parser);
1637 if (c_parser_next_token_is (parser, CPP_EQ))
1639 tree d;
1640 struct c_expr init;
1641 location_t init_loc;
1642 c_parser_consume_token (parser);
1643 /* The declaration of the variable is in effect while
1644 its initializer is parsed. */
1645 d = start_decl (declarator, specs, true,
1646 chainon (postfix_attrs, all_prefix_attrs));
1647 if (!d)
1648 d = error_mark_node;
1649 start_init (d, asm_name, global_bindings_p ());
1650 init_loc = c_parser_peek_token (parser)->location;
1651 init = c_parser_initializer (parser);
1652 finish_init ();
1653 if (d != error_mark_node)
1655 maybe_warn_string_init (TREE_TYPE (d), init);
1656 finish_decl (d, init_loc, init.value,
1657 init.original_type, asm_name);
1660 else
1662 tree d = start_decl (declarator, specs, false,
1663 chainon (postfix_attrs,
1664 all_prefix_attrs));
1665 if (d)
1666 finish_decl (d, UNKNOWN_LOCATION, NULL_TREE,
1667 NULL_TREE, asm_name);
1669 if (c_parser_next_token_is_keyword (parser, RID_IN))
1671 if (d)
1672 *objc_foreach_object_declaration = d;
1673 else
1674 *objc_foreach_object_declaration = error_mark_node;
1677 if (c_parser_next_token_is (parser, CPP_COMMA))
1679 c_parser_consume_token (parser);
1680 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
1681 all_prefix_attrs = chainon (c_parser_attributes (parser),
1682 prefix_attrs);
1683 else
1684 all_prefix_attrs = prefix_attrs;
1685 continue;
1687 else if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1689 c_parser_consume_token (parser);
1690 return;
1692 else if (c_parser_next_token_is_keyword (parser, RID_IN))
1694 /* This can only happen in Objective-C: we found the
1695 'in' that terminates the declaration inside an
1696 Objective-C foreach statement. Do not consume the
1697 token, so that the caller can use it to determine
1698 that this indeed is a foreach context. */
1699 return;
1701 else
1703 c_parser_error (parser, "expected %<,%> or %<;%>");
1704 c_parser_skip_to_end_of_block_or_statement (parser);
1705 return;
1708 else if (!fndef_ok)
1710 c_parser_error (parser, "expected %<=%>, %<,%>, %<;%>, "
1711 "%<asm%> or %<__attribute__%>");
1712 c_parser_skip_to_end_of_block_or_statement (parser);
1713 return;
1715 /* Function definition (nested or otherwise). */
1716 if (nested)
1718 pedwarn (here, OPT_Wpedantic, "ISO C forbids nested functions");
1719 c_push_function_context ();
1721 if (!start_function (specs, declarator, all_prefix_attrs))
1723 /* This can appear in many cases looking nothing like a
1724 function definition, so we don't give a more specific
1725 error suggesting there was one. */
1726 c_parser_error (parser, "expected %<=%>, %<,%>, %<;%>, %<asm%> "
1727 "or %<__attribute__%>");
1728 if (nested)
1729 c_pop_function_context ();
1730 break;
1733 if (DECL_DECLARED_INLINE_P (current_function_decl))
1734 tv = TV_PARSE_INLINE;
1735 else
1736 tv = TV_PARSE_FUNC;
1737 timevar_push (tv);
1739 /* Parse old-style parameter declarations. ??? Attributes are
1740 not allowed to start declaration specifiers here because of a
1741 syntax conflict between a function declaration with attribute
1742 suffix and a function definition with an attribute prefix on
1743 first old-style parameter declaration. Following the old
1744 parser, they are not accepted on subsequent old-style
1745 parameter declarations either. However, there is no
1746 ambiguity after the first declaration, nor indeed on the
1747 first as long as we don't allow postfix attributes after a
1748 declarator with a nonempty identifier list in a definition;
1749 and postfix attributes have never been accepted here in
1750 function definitions either. */
1751 while (c_parser_next_token_is_not (parser, CPP_EOF)
1752 && c_parser_next_token_is_not (parser, CPP_OPEN_BRACE))
1753 c_parser_declaration_or_fndef (parser, false, false, false,
1754 true, false, NULL);
1755 store_parm_decls ();
1756 DECL_STRUCT_FUNCTION (current_function_decl)->function_start_locus
1757 = c_parser_peek_token (parser)->location;
1758 fnbody = c_parser_compound_statement (parser);
1759 if (flag_enable_cilkplus && contains_array_notation_expr (fnbody))
1760 fnbody = expand_array_notation_exprs (fnbody);
1761 if (nested)
1763 tree decl = current_function_decl;
1764 /* Mark nested functions as needing static-chain initially.
1765 lower_nested_functions will recompute it but the
1766 DECL_STATIC_CHAIN flag is also used before that happens,
1767 by initializer_constant_valid_p. See gcc.dg/nested-fn-2.c. */
1768 DECL_STATIC_CHAIN (decl) = 1;
1769 add_stmt (fnbody);
1770 finish_function ();
1771 c_pop_function_context ();
1772 add_stmt (build_stmt (DECL_SOURCE_LOCATION (decl), DECL_EXPR, decl));
1774 else
1776 add_stmt (fnbody);
1777 finish_function ();
1780 timevar_pop (tv);
1781 break;
1785 /* Parse an asm-definition (asm() outside a function body). This is a
1786 GNU extension.
1788 asm-definition:
1789 simple-asm-expr ;
1792 static void
1793 c_parser_asm_definition (c_parser *parser)
1795 tree asm_str = c_parser_simple_asm_expr (parser);
1796 if (asm_str)
1797 add_asm_node (asm_str);
1798 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
1801 /* Parse a static assertion (C11 6.7.10).
1803 static_assert-declaration:
1804 static_assert-declaration-no-semi ;
1807 static void
1808 c_parser_static_assert_declaration (c_parser *parser)
1810 c_parser_static_assert_declaration_no_semi (parser);
1811 if (parser->error
1812 || !c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
1813 c_parser_skip_to_end_of_block_or_statement (parser);
1816 /* Parse a static assertion (C11 6.7.10), without the trailing
1817 semicolon.
1819 static_assert-declaration-no-semi:
1820 _Static_assert ( constant-expression , string-literal )
1823 static void
1824 c_parser_static_assert_declaration_no_semi (c_parser *parser)
1826 location_t assert_loc, value_loc;
1827 tree value;
1828 tree string;
1830 gcc_assert (c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT));
1831 assert_loc = c_parser_peek_token (parser)->location;
1832 if (!flag_isoc11)
1834 if (flag_isoc99)
1835 pedwarn (assert_loc, OPT_Wpedantic,
1836 "ISO C99 does not support %<_Static_assert%>");
1837 else
1838 pedwarn (assert_loc, OPT_Wpedantic,
1839 "ISO C90 does not support %<_Static_assert%>");
1841 c_parser_consume_token (parser);
1842 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
1843 return;
1844 value_loc = c_parser_peek_token (parser)->location;
1845 value = c_parser_expr_no_commas (parser, NULL).value;
1846 parser->lex_untranslated_string = true;
1847 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
1849 parser->lex_untranslated_string = false;
1850 return;
1852 switch (c_parser_peek_token (parser)->type)
1854 case CPP_STRING:
1855 case CPP_STRING16:
1856 case CPP_STRING32:
1857 case CPP_WSTRING:
1858 case CPP_UTF8STRING:
1859 string = c_parser_peek_token (parser)->value;
1860 c_parser_consume_token (parser);
1861 parser->lex_untranslated_string = false;
1862 break;
1863 default:
1864 c_parser_error (parser, "expected string literal");
1865 parser->lex_untranslated_string = false;
1866 return;
1868 c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
1870 if (!INTEGRAL_TYPE_P (TREE_TYPE (value)))
1872 error_at (value_loc, "expression in static assertion is not an integer");
1873 return;
1875 if (TREE_CODE (value) != INTEGER_CST)
1877 value = c_fully_fold (value, false, NULL);
1878 if (TREE_CODE (value) == INTEGER_CST)
1879 pedwarn (value_loc, OPT_Wpedantic, "expression in static assertion "
1880 "is not an integer constant expression");
1882 if (TREE_CODE (value) != INTEGER_CST)
1884 error_at (value_loc, "expression in static assertion is not constant");
1885 return;
1887 constant_expression_warning (value);
1888 if (integer_zerop (value))
1889 error_at (assert_loc, "static assertion failed: %E", string);
1892 /* Parse some declaration specifiers (possibly none) (C90 6.5, C99
1893 6.7), adding them to SPECS (which may already include some).
1894 Storage class specifiers are accepted iff SCSPEC_OK; type
1895 specifiers are accepted iff TYPESPEC_OK; attributes are accepted at
1896 the start iff START_ATTR_OK.
1898 declaration-specifiers:
1899 storage-class-specifier declaration-specifiers[opt]
1900 type-specifier declaration-specifiers[opt]
1901 type-qualifier declaration-specifiers[opt]
1902 function-specifier declaration-specifiers[opt]
1903 alignment-specifier declaration-specifiers[opt]
1905 Function specifiers (inline) are from C99, and are currently
1906 handled as storage class specifiers, as is __thread. Alignment
1907 specifiers are from C11.
1909 C90 6.5.1, C99 6.7.1:
1910 storage-class-specifier:
1911 typedef
1912 extern
1913 static
1914 auto
1915 register
1917 C99 6.7.4:
1918 function-specifier:
1919 inline
1920 _Noreturn
1922 (_Noreturn is new in C11.)
1924 C90 6.5.2, C99 6.7.2:
1925 type-specifier:
1926 void
1927 char
1928 short
1930 long
1931 float
1932 double
1933 signed
1934 unsigned
1935 _Bool
1936 _Complex
1937 [_Imaginary removed in C99 TC2]
1938 struct-or-union-specifier
1939 enum-specifier
1940 typedef-name
1942 (_Bool and _Complex are new in C99.)
1944 C90 6.5.3, C99 6.7.3:
1946 type-qualifier:
1947 const
1948 restrict
1949 volatile
1950 address-space-qualifier
1952 (restrict is new in C99.)
1954 GNU extensions:
1956 declaration-specifiers:
1957 attributes declaration-specifiers[opt]
1959 type-qualifier:
1960 address-space
1962 address-space:
1963 identifier recognized by the target
1965 storage-class-specifier:
1966 __thread
1968 type-specifier:
1969 typeof-specifier
1970 __int128
1971 _Decimal32
1972 _Decimal64
1973 _Decimal128
1974 _Fract
1975 _Accum
1976 _Sat
1978 (_Fract, _Accum, and _Sat are new from ISO/IEC DTR 18037:
1979 http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1169.pdf)
1981 Objective-C:
1983 type-specifier:
1984 class-name objc-protocol-refs[opt]
1985 typedef-name objc-protocol-refs
1986 objc-protocol-refs
1989 static void
1990 c_parser_declspecs (c_parser *parser, struct c_declspecs *specs,
1991 bool scspec_ok, bool typespec_ok, bool start_attr_ok,
1992 enum c_lookahead_kind la)
1994 bool attrs_ok = start_attr_ok;
1995 bool seen_type = specs->typespec_kind != ctsk_none;
1997 if (!typespec_ok)
1998 gcc_assert (la == cla_prefer_id);
2000 while (c_parser_next_token_is (parser, CPP_NAME)
2001 || c_parser_next_token_is (parser, CPP_KEYWORD)
2002 || (c_dialect_objc () && c_parser_next_token_is (parser, CPP_LESS)))
2004 struct c_typespec t;
2005 tree attrs;
2006 tree align;
2007 location_t loc = c_parser_peek_token (parser)->location;
2009 /* If we cannot accept a type, exit if the next token must start
2010 one. Also, if we already have seen a tagged definition,
2011 a typename would be an error anyway and likely the user
2012 has simply forgotten a semicolon, so we exit. */
2013 if ((!typespec_ok || specs->typespec_kind == ctsk_tagdef)
2014 && c_parser_next_tokens_start_typename (parser, la)
2015 && !c_parser_next_token_is_qualifier (parser))
2016 break;
2018 if (c_parser_next_token_is (parser, CPP_NAME))
2020 c_token *name_token = c_parser_peek_token (parser);
2021 tree value = name_token->value;
2022 c_id_kind kind = name_token->id_kind;
2024 if (kind == C_ID_ADDRSPACE)
2026 addr_space_t as
2027 = name_token->keyword - RID_FIRST_ADDR_SPACE;
2028 declspecs_add_addrspace (name_token->location, specs, as);
2029 c_parser_consume_token (parser);
2030 attrs_ok = true;
2031 continue;
2034 gcc_assert (!c_parser_next_token_is_qualifier (parser));
2036 /* If we cannot accept a type, and the next token must start one,
2037 exit. Do the same if we already have seen a tagged definition,
2038 since it would be an error anyway and likely the user has simply
2039 forgotten a semicolon. */
2040 if (seen_type || !c_parser_next_tokens_start_typename (parser, la))
2041 break;
2043 /* Now at an unknown typename (C_ID_ID), a C_ID_TYPENAME or
2044 a C_ID_CLASSNAME. */
2045 c_parser_consume_token (parser);
2046 seen_type = true;
2047 attrs_ok = true;
2048 if (kind == C_ID_ID)
2050 error ("unknown type name %qE", value);
2051 t.kind = ctsk_typedef;
2052 t.spec = error_mark_node;
2054 else if (kind == C_ID_TYPENAME
2055 && (!c_dialect_objc ()
2056 || c_parser_next_token_is_not (parser, CPP_LESS)))
2058 t.kind = ctsk_typedef;
2059 /* For a typedef name, record the meaning, not the name.
2060 In case of 'foo foo, bar;'. */
2061 t.spec = lookup_name (value);
2063 else
2065 tree proto = NULL_TREE;
2066 gcc_assert (c_dialect_objc ());
2067 t.kind = ctsk_objc;
2068 if (c_parser_next_token_is (parser, CPP_LESS))
2069 proto = c_parser_objc_protocol_refs (parser);
2070 t.spec = objc_get_protocol_qualified_type (value, proto);
2072 t.expr = NULL_TREE;
2073 t.expr_const_operands = true;
2074 declspecs_add_type (name_token->location, specs, t);
2075 continue;
2077 if (c_parser_next_token_is (parser, CPP_LESS))
2079 /* Make "<SomeProtocol>" equivalent to "id <SomeProtocol>" -
2080 nisse@lysator.liu.se. */
2081 tree proto;
2082 gcc_assert (c_dialect_objc ());
2083 if (!typespec_ok || seen_type)
2084 break;
2085 proto = c_parser_objc_protocol_refs (parser);
2086 t.kind = ctsk_objc;
2087 t.spec = objc_get_protocol_qualified_type (NULL_TREE, proto);
2088 t.expr = NULL_TREE;
2089 t.expr_const_operands = true;
2090 declspecs_add_type (loc, specs, t);
2091 continue;
2093 gcc_assert (c_parser_next_token_is (parser, CPP_KEYWORD));
2094 switch (c_parser_peek_token (parser)->keyword)
2096 case RID_STATIC:
2097 case RID_EXTERN:
2098 case RID_REGISTER:
2099 case RID_TYPEDEF:
2100 case RID_INLINE:
2101 case RID_NORETURN:
2102 case RID_AUTO:
2103 case RID_THREAD:
2104 if (!scspec_ok)
2105 goto out;
2106 attrs_ok = true;
2107 /* TODO: Distinguish between function specifiers (inline, noreturn)
2108 and storage class specifiers, either here or in
2109 declspecs_add_scspec. */
2110 declspecs_add_scspec (loc, specs,
2111 c_parser_peek_token (parser)->value);
2112 c_parser_consume_token (parser);
2113 break;
2114 case RID_UNSIGNED:
2115 case RID_LONG:
2116 case RID_INT128:
2117 case RID_SHORT:
2118 case RID_SIGNED:
2119 case RID_COMPLEX:
2120 case RID_INT:
2121 case RID_CHAR:
2122 case RID_FLOAT:
2123 case RID_DOUBLE:
2124 case RID_VOID:
2125 case RID_DFLOAT32:
2126 case RID_DFLOAT64:
2127 case RID_DFLOAT128:
2128 case RID_BOOL:
2129 case RID_FRACT:
2130 case RID_ACCUM:
2131 case RID_SAT:
2132 if (!typespec_ok)
2133 goto out;
2134 attrs_ok = true;
2135 seen_type = true;
2136 if (c_dialect_objc ())
2137 parser->objc_need_raw_identifier = true;
2138 t.kind = ctsk_resword;
2139 t.spec = c_parser_peek_token (parser)->value;
2140 t.expr = NULL_TREE;
2141 t.expr_const_operands = true;
2142 declspecs_add_type (loc, specs, t);
2143 c_parser_consume_token (parser);
2144 break;
2145 case RID_ENUM:
2146 if (!typespec_ok)
2147 goto out;
2148 attrs_ok = true;
2149 seen_type = true;
2150 t = c_parser_enum_specifier (parser);
2151 declspecs_add_type (loc, specs, t);
2152 break;
2153 case RID_STRUCT:
2154 case RID_UNION:
2155 if (!typespec_ok)
2156 goto out;
2157 attrs_ok = true;
2158 seen_type = true;
2159 t = c_parser_struct_or_union_specifier (parser);
2160 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, t.spec);
2161 declspecs_add_type (loc, specs, t);
2162 break;
2163 case RID_TYPEOF:
2164 /* ??? The old parser rejected typeof after other type
2165 specifiers, but is a syntax error the best way of
2166 handling this? */
2167 if (!typespec_ok || seen_type)
2168 goto out;
2169 attrs_ok = true;
2170 seen_type = true;
2171 t = c_parser_typeof_specifier (parser);
2172 declspecs_add_type (loc, specs, t);
2173 break;
2174 case RID_CONST:
2175 case RID_VOLATILE:
2176 case RID_RESTRICT:
2177 attrs_ok = true;
2178 declspecs_add_qual (loc, specs, c_parser_peek_token (parser)->value);
2179 c_parser_consume_token (parser);
2180 break;
2181 case RID_ATTRIBUTE:
2182 if (!attrs_ok)
2183 goto out;
2184 attrs = c_parser_attributes (parser);
2185 declspecs_add_attrs (loc, specs, attrs);
2186 break;
2187 case RID_ALIGNAS:
2188 align = c_parser_alignas_specifier (parser);
2189 declspecs_add_alignas (loc, specs, align);
2190 break;
2191 default:
2192 goto out;
2195 out: ;
2198 /* Parse an enum specifier (C90 6.5.2.2, C99 6.7.2.2).
2200 enum-specifier:
2201 enum attributes[opt] identifier[opt] { enumerator-list } attributes[opt]
2202 enum attributes[opt] identifier[opt] { enumerator-list , } attributes[opt]
2203 enum attributes[opt] identifier
2205 The form with trailing comma is new in C99. The forms with
2206 attributes are GNU extensions. In GNU C, we accept any expression
2207 without commas in the syntax (assignment expressions, not just
2208 conditional expressions); assignment expressions will be diagnosed
2209 as non-constant.
2211 enumerator-list:
2212 enumerator
2213 enumerator-list , enumerator
2215 enumerator:
2216 enumeration-constant
2217 enumeration-constant = constant-expression
2220 static struct c_typespec
2221 c_parser_enum_specifier (c_parser *parser)
2223 struct c_typespec ret;
2224 tree attrs;
2225 tree ident = NULL_TREE;
2226 location_t enum_loc;
2227 location_t ident_loc = UNKNOWN_LOCATION; /* Quiet warning. */
2228 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ENUM));
2229 enum_loc = c_parser_peek_token (parser)->location;
2230 c_parser_consume_token (parser);
2231 attrs = c_parser_attributes (parser);
2232 enum_loc = c_parser_peek_token (parser)->location;
2233 /* Set the location in case we create a decl now. */
2234 c_parser_set_source_position_from_token (c_parser_peek_token (parser));
2235 if (c_parser_next_token_is (parser, CPP_NAME))
2237 ident = c_parser_peek_token (parser)->value;
2238 ident_loc = c_parser_peek_token (parser)->location;
2239 enum_loc = ident_loc;
2240 c_parser_consume_token (parser);
2242 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
2244 /* Parse an enum definition. */
2245 struct c_enum_contents the_enum;
2246 tree type;
2247 tree postfix_attrs;
2248 /* We chain the enumerators in reverse order, then put them in
2249 forward order at the end. */
2250 tree values;
2251 timevar_push (TV_PARSE_ENUM);
2252 type = start_enum (enum_loc, &the_enum, ident);
2253 values = NULL_TREE;
2254 c_parser_consume_token (parser);
2255 while (true)
2257 tree enum_id;
2258 tree enum_value;
2259 tree enum_decl;
2260 bool seen_comma;
2261 c_token *token;
2262 location_t comma_loc = UNKNOWN_LOCATION; /* Quiet warning. */
2263 location_t decl_loc, value_loc;
2264 if (c_parser_next_token_is_not (parser, CPP_NAME))
2266 c_parser_error (parser, "expected identifier");
2267 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
2268 values = error_mark_node;
2269 break;
2271 token = c_parser_peek_token (parser);
2272 enum_id = token->value;
2273 /* Set the location in case we create a decl now. */
2274 c_parser_set_source_position_from_token (token);
2275 decl_loc = value_loc = token->location;
2276 c_parser_consume_token (parser);
2277 if (c_parser_next_token_is (parser, CPP_EQ))
2279 c_parser_consume_token (parser);
2280 value_loc = c_parser_peek_token (parser)->location;
2281 enum_value = c_parser_expr_no_commas (parser, NULL).value;
2283 else
2284 enum_value = NULL_TREE;
2285 enum_decl = build_enumerator (decl_loc, value_loc,
2286 &the_enum, enum_id, enum_value);
2287 TREE_CHAIN (enum_decl) = values;
2288 values = enum_decl;
2289 seen_comma = false;
2290 if (c_parser_next_token_is (parser, CPP_COMMA))
2292 comma_loc = c_parser_peek_token (parser)->location;
2293 seen_comma = true;
2294 c_parser_consume_token (parser);
2296 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2298 if (seen_comma && !flag_isoc99)
2299 pedwarn (comma_loc, OPT_Wpedantic, "comma at end of enumerator list");
2300 c_parser_consume_token (parser);
2301 break;
2303 if (!seen_comma)
2305 c_parser_error (parser, "expected %<,%> or %<}%>");
2306 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
2307 values = error_mark_node;
2308 break;
2311 postfix_attrs = c_parser_attributes (parser);
2312 ret.spec = finish_enum (type, nreverse (values),
2313 chainon (attrs, postfix_attrs));
2314 ret.kind = ctsk_tagdef;
2315 ret.expr = NULL_TREE;
2316 ret.expr_const_operands = true;
2317 timevar_pop (TV_PARSE_ENUM);
2318 return ret;
2320 else if (!ident)
2322 c_parser_error (parser, "expected %<{%>");
2323 ret.spec = error_mark_node;
2324 ret.kind = ctsk_tagref;
2325 ret.expr = NULL_TREE;
2326 ret.expr_const_operands = true;
2327 return ret;
2329 ret = parser_xref_tag (ident_loc, ENUMERAL_TYPE, ident);
2330 /* In ISO C, enumerated types can be referred to only if already
2331 defined. */
2332 if (pedantic && !COMPLETE_TYPE_P (ret.spec))
2334 gcc_assert (ident);
2335 pedwarn (enum_loc, OPT_Wpedantic,
2336 "ISO C forbids forward references to %<enum%> types");
2338 return ret;
2341 /* Parse a struct or union specifier (C90 6.5.2.1, C99 6.7.2.1).
2343 struct-or-union-specifier:
2344 struct-or-union attributes[opt] identifier[opt]
2345 { struct-contents } attributes[opt]
2346 struct-or-union attributes[opt] identifier
2348 struct-contents:
2349 struct-declaration-list
2351 struct-declaration-list:
2352 struct-declaration ;
2353 struct-declaration-list struct-declaration ;
2355 GNU extensions:
2357 struct-contents:
2358 empty
2359 struct-declaration
2360 struct-declaration-list struct-declaration
2362 struct-declaration-list:
2363 struct-declaration-list ;
2366 (Note that in the syntax here, unlike that in ISO C, the semicolons
2367 are included here rather than in struct-declaration, in order to
2368 describe the syntax with extra semicolons and missing semicolon at
2369 end.)
2371 Objective-C:
2373 struct-declaration-list:
2374 @defs ( class-name )
2376 (Note this does not include a trailing semicolon, but can be
2377 followed by further declarations, and gets a pedwarn-if-pedantic
2378 when followed by a semicolon.) */
2380 static struct c_typespec
2381 c_parser_struct_or_union_specifier (c_parser *parser)
2383 struct c_typespec ret;
2384 tree attrs;
2385 tree ident = NULL_TREE;
2386 location_t struct_loc;
2387 location_t ident_loc = UNKNOWN_LOCATION;
2388 enum tree_code code;
2389 switch (c_parser_peek_token (parser)->keyword)
2391 case RID_STRUCT:
2392 code = RECORD_TYPE;
2393 break;
2394 case RID_UNION:
2395 code = UNION_TYPE;
2396 break;
2397 default:
2398 gcc_unreachable ();
2400 struct_loc = c_parser_peek_token (parser)->location;
2401 c_parser_consume_token (parser);
2402 attrs = c_parser_attributes (parser);
2404 /* Set the location in case we create a decl now. */
2405 c_parser_set_source_position_from_token (c_parser_peek_token (parser));
2407 if (c_parser_next_token_is (parser, CPP_NAME))
2409 ident = c_parser_peek_token (parser)->value;
2410 ident_loc = c_parser_peek_token (parser)->location;
2411 struct_loc = ident_loc;
2412 c_parser_consume_token (parser);
2414 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
2416 /* Parse a struct or union definition. Start the scope of the
2417 tag before parsing components. */
2418 struct c_struct_parse_info *struct_info;
2419 tree type = start_struct (struct_loc, code, ident, &struct_info);
2420 tree postfix_attrs;
2421 /* We chain the components in reverse order, then put them in
2422 forward order at the end. Each struct-declaration may
2423 declare multiple components (comma-separated), so we must use
2424 chainon to join them, although when parsing each
2425 struct-declaration we can use TREE_CHAIN directly.
2427 The theory behind all this is that there will be more
2428 semicolon separated fields than comma separated fields, and
2429 so we'll be minimizing the number of node traversals required
2430 by chainon. */
2431 tree contents;
2432 timevar_push (TV_PARSE_STRUCT);
2433 contents = NULL_TREE;
2434 c_parser_consume_token (parser);
2435 /* Handle the Objective-C @defs construct,
2436 e.g. foo(sizeof(struct{ @defs(ClassName) }));. */
2437 if (c_parser_next_token_is_keyword (parser, RID_AT_DEFS))
2439 tree name;
2440 gcc_assert (c_dialect_objc ());
2441 c_parser_consume_token (parser);
2442 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2443 goto end_at_defs;
2444 if (c_parser_next_token_is (parser, CPP_NAME)
2445 && c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME)
2447 name = c_parser_peek_token (parser)->value;
2448 c_parser_consume_token (parser);
2450 else
2452 c_parser_error (parser, "expected class name");
2453 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
2454 goto end_at_defs;
2456 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2457 "expected %<)%>");
2458 contents = nreverse (objc_get_class_ivars (name));
2460 end_at_defs:
2461 /* Parse the struct-declarations and semicolons. Problems with
2462 semicolons are diagnosed here; empty structures are diagnosed
2463 elsewhere. */
2464 while (true)
2466 tree decls;
2467 /* Parse any stray semicolon. */
2468 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
2470 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
2471 "extra semicolon in struct or union specified");
2472 c_parser_consume_token (parser);
2473 continue;
2475 /* Stop if at the end of the struct or union contents. */
2476 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2478 c_parser_consume_token (parser);
2479 break;
2481 /* Accept #pragmas at struct scope. */
2482 if (c_parser_next_token_is (parser, CPP_PRAGMA))
2484 c_parser_pragma (parser, pragma_external);
2485 continue;
2487 /* Parse some comma-separated declarations, but not the
2488 trailing semicolon if any. */
2489 decls = c_parser_struct_declaration (parser);
2490 contents = chainon (decls, contents);
2491 /* If no semicolon follows, either we have a parse error or
2492 are at the end of the struct or union and should
2493 pedwarn. */
2494 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
2495 c_parser_consume_token (parser);
2496 else
2498 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2499 pedwarn (c_parser_peek_token (parser)->location, 0,
2500 "no semicolon at end of struct or union");
2501 else if (parser->error
2502 || !c_parser_next_token_starts_declspecs (parser))
2504 c_parser_error (parser, "expected %<;%>");
2505 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
2506 break;
2509 /* If we come here, we have already emitted an error
2510 for an expected `;', identifier or `(', and we also
2511 recovered already. Go on with the next field. */
2514 postfix_attrs = c_parser_attributes (parser);
2515 ret.spec = finish_struct (struct_loc, type, nreverse (contents),
2516 chainon (attrs, postfix_attrs), struct_info);
2517 ret.kind = ctsk_tagdef;
2518 ret.expr = NULL_TREE;
2519 ret.expr_const_operands = true;
2520 timevar_pop (TV_PARSE_STRUCT);
2521 return ret;
2523 else if (!ident)
2525 c_parser_error (parser, "expected %<{%>");
2526 ret.spec = error_mark_node;
2527 ret.kind = ctsk_tagref;
2528 ret.expr = NULL_TREE;
2529 ret.expr_const_operands = true;
2530 return ret;
2532 ret = parser_xref_tag (ident_loc, code, ident);
2533 return ret;
2536 /* Parse a struct-declaration (C90 6.5.2.1, C99 6.7.2.1), *without*
2537 the trailing semicolon.
2539 struct-declaration:
2540 specifier-qualifier-list struct-declarator-list
2541 static_assert-declaration-no-semi
2543 specifier-qualifier-list:
2544 type-specifier specifier-qualifier-list[opt]
2545 type-qualifier specifier-qualifier-list[opt]
2546 attributes specifier-qualifier-list[opt]
2548 struct-declarator-list:
2549 struct-declarator
2550 struct-declarator-list , attributes[opt] struct-declarator
2552 struct-declarator:
2553 declarator attributes[opt]
2554 declarator[opt] : constant-expression attributes[opt]
2556 GNU extensions:
2558 struct-declaration:
2559 __extension__ struct-declaration
2560 specifier-qualifier-list
2562 Unlike the ISO C syntax, semicolons are handled elsewhere. The use
2563 of attributes where shown is a GNU extension. In GNU C, we accept
2564 any expression without commas in the syntax (assignment
2565 expressions, not just conditional expressions); assignment
2566 expressions will be diagnosed as non-constant. */
2568 static tree
2569 c_parser_struct_declaration (c_parser *parser)
2571 struct c_declspecs *specs;
2572 tree prefix_attrs;
2573 tree all_prefix_attrs;
2574 tree decls;
2575 location_t decl_loc;
2576 if (c_parser_next_token_is_keyword (parser, RID_EXTENSION))
2578 int ext;
2579 tree decl;
2580 ext = disable_extension_diagnostics ();
2581 c_parser_consume_token (parser);
2582 decl = c_parser_struct_declaration (parser);
2583 restore_extension_diagnostics (ext);
2584 return decl;
2586 if (c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT))
2588 c_parser_static_assert_declaration_no_semi (parser);
2589 return NULL_TREE;
2591 specs = build_null_declspecs ();
2592 decl_loc = c_parser_peek_token (parser)->location;
2593 c_parser_declspecs (parser, specs, false, true, true, cla_nonabstract_decl);
2594 if (parser->error)
2595 return NULL_TREE;
2596 if (!specs->declspecs_seen_p)
2598 c_parser_error (parser, "expected specifier-qualifier-list");
2599 return NULL_TREE;
2601 finish_declspecs (specs);
2602 if (c_parser_next_token_is (parser, CPP_SEMICOLON)
2603 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2605 tree ret;
2606 if (specs->typespec_kind == ctsk_none)
2608 pedwarn (decl_loc, OPT_Wpedantic,
2609 "ISO C forbids member declarations with no members");
2610 shadow_tag_warned (specs, pedantic);
2611 ret = NULL_TREE;
2613 else
2615 /* Support for unnamed structs or unions as members of
2616 structs or unions (which is [a] useful and [b] supports
2617 MS P-SDK). */
2618 tree attrs = NULL;
2620 ret = grokfield (c_parser_peek_token (parser)->location,
2621 build_id_declarator (NULL_TREE), specs,
2622 NULL_TREE, &attrs);
2623 if (ret)
2624 decl_attributes (&ret, attrs, 0);
2626 return ret;
2629 /* Provide better error recovery. Note that a type name here is valid,
2630 and will be treated as a field name. */
2631 if (specs->typespec_kind == ctsk_tagdef
2632 && TREE_CODE (specs->type) != ENUMERAL_TYPE
2633 && c_parser_next_token_starts_declspecs (parser)
2634 && !c_parser_next_token_is (parser, CPP_NAME))
2636 c_parser_error (parser, "expected %<;%>, identifier or %<(%>");
2637 parser->error = false;
2638 return NULL_TREE;
2641 pending_xref_error ();
2642 prefix_attrs = specs->attrs;
2643 all_prefix_attrs = prefix_attrs;
2644 specs->attrs = NULL_TREE;
2645 decls = NULL_TREE;
2646 while (true)
2648 /* Declaring one or more declarators or un-named bit-fields. */
2649 struct c_declarator *declarator;
2650 bool dummy = false;
2651 if (c_parser_next_token_is (parser, CPP_COLON))
2652 declarator = build_id_declarator (NULL_TREE);
2653 else
2654 declarator = c_parser_declarator (parser,
2655 specs->typespec_kind != ctsk_none,
2656 C_DTR_NORMAL, &dummy);
2657 if (declarator == NULL)
2659 c_parser_skip_to_end_of_block_or_statement (parser);
2660 break;
2662 if (c_parser_next_token_is (parser, CPP_COLON)
2663 || c_parser_next_token_is (parser, CPP_COMMA)
2664 || c_parser_next_token_is (parser, CPP_SEMICOLON)
2665 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE)
2666 || c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2668 tree postfix_attrs = NULL_TREE;
2669 tree width = NULL_TREE;
2670 tree d;
2671 if (c_parser_next_token_is (parser, CPP_COLON))
2673 c_parser_consume_token (parser);
2674 width = c_parser_expr_no_commas (parser, NULL).value;
2676 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2677 postfix_attrs = c_parser_attributes (parser);
2678 d = grokfield (c_parser_peek_token (parser)->location,
2679 declarator, specs, width, &all_prefix_attrs);
2680 decl_attributes (&d, chainon (postfix_attrs,
2681 all_prefix_attrs), 0);
2682 DECL_CHAIN (d) = decls;
2683 decls = d;
2684 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2685 all_prefix_attrs = chainon (c_parser_attributes (parser),
2686 prefix_attrs);
2687 else
2688 all_prefix_attrs = prefix_attrs;
2689 if (c_parser_next_token_is (parser, CPP_COMMA))
2690 c_parser_consume_token (parser);
2691 else if (c_parser_next_token_is (parser, CPP_SEMICOLON)
2692 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2694 /* Semicolon consumed in caller. */
2695 break;
2697 else
2699 c_parser_error (parser, "expected %<,%>, %<;%> or %<}%>");
2700 break;
2703 else
2705 c_parser_error (parser,
2706 "expected %<:%>, %<,%>, %<;%>, %<}%> or "
2707 "%<__attribute__%>");
2708 break;
2711 return decls;
2714 /* Parse a typeof specifier (a GNU extension).
2716 typeof-specifier:
2717 typeof ( expression )
2718 typeof ( type-name )
2721 static struct c_typespec
2722 c_parser_typeof_specifier (c_parser *parser)
2724 struct c_typespec ret;
2725 ret.kind = ctsk_typeof;
2726 ret.spec = error_mark_node;
2727 ret.expr = NULL_TREE;
2728 ret.expr_const_operands = true;
2729 gcc_assert (c_parser_next_token_is_keyword (parser, RID_TYPEOF));
2730 c_parser_consume_token (parser);
2731 c_inhibit_evaluation_warnings++;
2732 in_typeof++;
2733 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2735 c_inhibit_evaluation_warnings--;
2736 in_typeof--;
2737 return ret;
2739 if (c_parser_next_tokens_start_typename (parser, cla_prefer_id))
2741 struct c_type_name *type = c_parser_type_name (parser);
2742 c_inhibit_evaluation_warnings--;
2743 in_typeof--;
2744 if (type != NULL)
2746 ret.spec = groktypename (type, &ret.expr, &ret.expr_const_operands);
2747 pop_maybe_used (variably_modified_type_p (ret.spec, NULL_TREE));
2750 else
2752 bool was_vm;
2753 location_t here = c_parser_peek_token (parser)->location;
2754 struct c_expr expr = c_parser_expression (parser);
2755 c_inhibit_evaluation_warnings--;
2756 in_typeof--;
2757 if (TREE_CODE (expr.value) == COMPONENT_REF
2758 && DECL_C_BIT_FIELD (TREE_OPERAND (expr.value, 1)))
2759 error_at (here, "%<typeof%> applied to a bit-field");
2760 mark_exp_read (expr.value);
2761 ret.spec = TREE_TYPE (expr.value);
2762 was_vm = variably_modified_type_p (ret.spec, NULL_TREE);
2763 /* This is returned with the type so that when the type is
2764 evaluated, this can be evaluated. */
2765 if (was_vm)
2766 ret.expr = c_fully_fold (expr.value, false, &ret.expr_const_operands);
2767 pop_maybe_used (was_vm);
2769 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
2770 return ret;
2773 /* Parse an alignment-specifier.
2775 C11 6.7.5:
2777 alignment-specifier:
2778 _Alignas ( type-name )
2779 _Alignas ( constant-expression )
2782 static tree
2783 c_parser_alignas_specifier (c_parser * parser)
2785 tree ret = error_mark_node;
2786 location_t loc = c_parser_peek_token (parser)->location;
2787 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ALIGNAS));
2788 c_parser_consume_token (parser);
2789 if (!flag_isoc11)
2791 if (flag_isoc99)
2792 pedwarn (loc, OPT_Wpedantic,
2793 "ISO C99 does not support %<_Alignas%>");
2794 else
2795 pedwarn (loc, OPT_Wpedantic,
2796 "ISO C90 does not support %<_Alignas%>");
2798 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2799 return ret;
2800 if (c_parser_next_tokens_start_typename (parser, cla_prefer_id))
2802 struct c_type_name *type = c_parser_type_name (parser);
2803 if (type != NULL)
2804 ret = c_alignof (loc, groktypename (type, NULL, NULL));
2806 else
2807 ret = c_parser_expr_no_commas (parser, NULL).value;
2808 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
2809 return ret;
2812 /* Parse a declarator, possibly an abstract declarator (C90 6.5.4,
2813 6.5.5, C99 6.7.5, 6.7.6). If TYPE_SEEN_P then a typedef name may
2814 be redeclared; otherwise it may not. KIND indicates which kind of
2815 declarator is wanted. Returns a valid declarator except in the
2816 case of a syntax error in which case NULL is returned. *SEEN_ID is
2817 set to true if an identifier being declared is seen; this is used
2818 to diagnose bad forms of abstract array declarators and to
2819 determine whether an identifier list is syntactically permitted.
2821 declarator:
2822 pointer[opt] direct-declarator
2824 direct-declarator:
2825 identifier
2826 ( attributes[opt] declarator )
2827 direct-declarator array-declarator
2828 direct-declarator ( parameter-type-list )
2829 direct-declarator ( identifier-list[opt] )
2831 pointer:
2832 * type-qualifier-list[opt]
2833 * type-qualifier-list[opt] pointer
2835 type-qualifier-list:
2836 type-qualifier
2837 attributes
2838 type-qualifier-list type-qualifier
2839 type-qualifier-list attributes
2841 parameter-type-list:
2842 parameter-list
2843 parameter-list , ...
2845 parameter-list:
2846 parameter-declaration
2847 parameter-list , parameter-declaration
2849 parameter-declaration:
2850 declaration-specifiers declarator attributes[opt]
2851 declaration-specifiers abstract-declarator[opt] attributes[opt]
2853 identifier-list:
2854 identifier
2855 identifier-list , identifier
2857 abstract-declarator:
2858 pointer
2859 pointer[opt] direct-abstract-declarator
2861 direct-abstract-declarator:
2862 ( attributes[opt] abstract-declarator )
2863 direct-abstract-declarator[opt] array-declarator
2864 direct-abstract-declarator[opt] ( parameter-type-list[opt] )
2866 GNU extensions:
2868 direct-declarator:
2869 direct-declarator ( parameter-forward-declarations
2870 parameter-type-list[opt] )
2872 direct-abstract-declarator:
2873 direct-abstract-declarator[opt] ( parameter-forward-declarations
2874 parameter-type-list[opt] )
2876 parameter-forward-declarations:
2877 parameter-list ;
2878 parameter-forward-declarations parameter-list ;
2880 The uses of attributes shown above are GNU extensions.
2882 Some forms of array declarator are not included in C99 in the
2883 syntax for abstract declarators; these are disallowed elsewhere.
2884 This may be a defect (DR#289).
2886 This function also accepts an omitted abstract declarator as being
2887 an abstract declarator, although not part of the formal syntax. */
2889 static struct c_declarator *
2890 c_parser_declarator (c_parser *parser, bool type_seen_p, c_dtr_syn kind,
2891 bool *seen_id)
2893 /* Parse any initial pointer part. */
2894 if (c_parser_next_token_is (parser, CPP_MULT))
2896 struct c_declspecs *quals_attrs = build_null_declspecs ();
2897 struct c_declarator *inner;
2898 c_parser_consume_token (parser);
2899 c_parser_declspecs (parser, quals_attrs, false, false, true, cla_prefer_id);
2900 inner = c_parser_declarator (parser, type_seen_p, kind, seen_id);
2901 if (inner == NULL)
2902 return NULL;
2903 else
2904 return make_pointer_declarator (quals_attrs, inner);
2906 /* Now we have a direct declarator, direct abstract declarator or
2907 nothing (which counts as a direct abstract declarator here). */
2908 return c_parser_direct_declarator (parser, type_seen_p, kind, seen_id);
2911 /* Parse a direct declarator or direct abstract declarator; arguments
2912 as c_parser_declarator. */
2914 static struct c_declarator *
2915 c_parser_direct_declarator (c_parser *parser, bool type_seen_p, c_dtr_syn kind,
2916 bool *seen_id)
2918 /* The direct declarator must start with an identifier (possibly
2919 omitted) or a parenthesized declarator (possibly abstract). In
2920 an ordinary declarator, initial parentheses must start a
2921 parenthesized declarator. In an abstract declarator or parameter
2922 declarator, they could start a parenthesized declarator or a
2923 parameter list. To tell which, the open parenthesis and any
2924 following attributes must be read. If a declaration specifier
2925 follows, then it is a parameter list; if the specifier is a
2926 typedef name, there might be an ambiguity about redeclaring it,
2927 which is resolved in the direction of treating it as a typedef
2928 name. If a close parenthesis follows, it is also an empty
2929 parameter list, as the syntax does not permit empty abstract
2930 declarators. Otherwise, it is a parenthesized declarator (in
2931 which case the analysis may be repeated inside it, recursively).
2933 ??? There is an ambiguity in a parameter declaration "int
2934 (__attribute__((foo)) x)", where x is not a typedef name: it
2935 could be an abstract declarator for a function, or declare x with
2936 parentheses. The proper resolution of this ambiguity needs
2937 documenting. At present we follow an accident of the old
2938 parser's implementation, whereby the first parameter must have
2939 some declaration specifiers other than just attributes. Thus as
2940 a parameter declaration it is treated as a parenthesized
2941 parameter named x, and as an abstract declarator it is
2942 rejected.
2944 ??? Also following the old parser, attributes inside an empty
2945 parameter list are ignored, making it a list not yielding a
2946 prototype, rather than giving an error or making it have one
2947 parameter with implicit type int.
2949 ??? Also following the old parser, typedef names may be
2950 redeclared in declarators, but not Objective-C class names. */
2952 if (kind != C_DTR_ABSTRACT
2953 && c_parser_next_token_is (parser, CPP_NAME)
2954 && ((type_seen_p
2955 && (c_parser_peek_token (parser)->id_kind == C_ID_TYPENAME
2956 || c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME))
2957 || c_parser_peek_token (parser)->id_kind == C_ID_ID))
2959 struct c_declarator *inner
2960 = build_id_declarator (c_parser_peek_token (parser)->value);
2961 *seen_id = true;
2962 inner->id_loc = c_parser_peek_token (parser)->location;
2963 c_parser_consume_token (parser);
2964 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
2967 if (kind != C_DTR_NORMAL
2968 && c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
2970 struct c_declarator *inner = build_id_declarator (NULL_TREE);
2971 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
2974 /* Either we are at the end of an abstract declarator, or we have
2975 parentheses. */
2977 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
2979 tree attrs;
2980 struct c_declarator *inner;
2981 c_parser_consume_token (parser);
2982 attrs = c_parser_attributes (parser);
2983 if (kind != C_DTR_NORMAL
2984 && (c_parser_next_token_starts_declspecs (parser)
2985 || c_parser_next_token_is (parser, CPP_CLOSE_PAREN)))
2987 struct c_arg_info *args
2988 = c_parser_parms_declarator (parser, kind == C_DTR_NORMAL,
2989 attrs);
2990 if (args == NULL)
2991 return NULL;
2992 else
2994 inner
2995 = build_function_declarator (args,
2996 build_id_declarator (NULL_TREE));
2997 return c_parser_direct_declarator_inner (parser, *seen_id,
2998 inner);
3001 /* A parenthesized declarator. */
3002 inner = c_parser_declarator (parser, type_seen_p, kind, seen_id);
3003 if (inner != NULL && attrs != NULL)
3004 inner = build_attrs_declarator (attrs, inner);
3005 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3007 c_parser_consume_token (parser);
3008 if (inner == NULL)
3009 return NULL;
3010 else
3011 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
3013 else
3015 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3016 "expected %<)%>");
3017 return NULL;
3020 else
3022 if (kind == C_DTR_NORMAL)
3024 c_parser_error (parser, "expected identifier or %<(%>");
3025 return NULL;
3027 else
3028 return build_id_declarator (NULL_TREE);
3032 /* Parse part of a direct declarator or direct abstract declarator,
3033 given that some (in INNER) has already been parsed; ID_PRESENT is
3034 true if an identifier is present, false for an abstract
3035 declarator. */
3037 static struct c_declarator *
3038 c_parser_direct_declarator_inner (c_parser *parser, bool id_present,
3039 struct c_declarator *inner)
3041 /* Parse a sequence of array declarators and parameter lists. */
3042 if (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
3044 location_t brace_loc = c_parser_peek_token (parser)->location;
3045 struct c_declarator *declarator;
3046 struct c_declspecs *quals_attrs = build_null_declspecs ();
3047 bool static_seen;
3048 bool star_seen;
3049 tree dimen;
3050 c_parser_consume_token (parser);
3051 c_parser_declspecs (parser, quals_attrs, false, false, true, cla_prefer_id);
3052 static_seen = c_parser_next_token_is_keyword (parser, RID_STATIC);
3053 if (static_seen)
3054 c_parser_consume_token (parser);
3055 if (static_seen && !quals_attrs->declspecs_seen_p)
3056 c_parser_declspecs (parser, quals_attrs, false, false, true, cla_prefer_id);
3057 if (!quals_attrs->declspecs_seen_p)
3058 quals_attrs = NULL;
3059 /* If "static" is present, there must be an array dimension.
3060 Otherwise, there may be a dimension, "*", or no
3061 dimension. */
3062 if (static_seen)
3064 star_seen = false;
3065 dimen = c_parser_expr_no_commas (parser, NULL).value;
3067 else
3069 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
3071 dimen = NULL_TREE;
3072 star_seen = false;
3074 else if (flag_enable_cilkplus
3075 && c_parser_next_token_is (parser, CPP_COLON))
3077 dimen = error_mark_node;
3078 star_seen = false;
3079 error_at (c_parser_peek_token (parser)->location,
3080 "array notations cannot be used in declaration");
3081 c_parser_consume_token (parser);
3083 else if (c_parser_next_token_is (parser, CPP_MULT))
3085 if (c_parser_peek_2nd_token (parser)->type == CPP_CLOSE_SQUARE)
3087 dimen = NULL_TREE;
3088 star_seen = true;
3089 c_parser_consume_token (parser);
3091 else
3093 star_seen = false;
3094 dimen = c_parser_expr_no_commas (parser, NULL).value;
3097 else
3099 star_seen = false;
3100 dimen = c_parser_expr_no_commas (parser, NULL).value;
3103 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
3104 c_parser_consume_token (parser);
3105 else if (flag_enable_cilkplus
3106 && c_parser_next_token_is (parser, CPP_COLON))
3108 error_at (c_parser_peek_token (parser)->location,
3109 "array notations cannot be used in declaration");
3110 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
3111 return NULL;
3113 else
3115 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
3116 "expected %<]%>");
3117 return NULL;
3119 if (dimen)
3120 mark_exp_read (dimen);
3121 declarator = build_array_declarator (brace_loc, dimen, quals_attrs,
3122 static_seen, star_seen);
3123 if (declarator == NULL)
3124 return NULL;
3125 inner = set_array_declarator_inner (declarator, inner);
3126 return c_parser_direct_declarator_inner (parser, id_present, inner);
3128 else if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
3130 tree attrs;
3131 struct c_arg_info *args;
3132 c_parser_consume_token (parser);
3133 attrs = c_parser_attributes (parser);
3134 args = c_parser_parms_declarator (parser, id_present, attrs);
3135 if (args == NULL)
3136 return NULL;
3137 else
3139 inner = build_function_declarator (args, inner);
3140 return c_parser_direct_declarator_inner (parser, id_present, inner);
3143 return inner;
3146 /* Parse a parameter list or identifier list, including the closing
3147 parenthesis but not the opening one. ATTRS are the attributes at
3148 the start of the list. ID_LIST_OK is true if an identifier list is
3149 acceptable; such a list must not have attributes at the start. */
3151 static struct c_arg_info *
3152 c_parser_parms_declarator (c_parser *parser, bool id_list_ok, tree attrs)
3154 push_scope ();
3155 declare_parm_level ();
3156 /* If the list starts with an identifier, it is an identifier list.
3157 Otherwise, it is either a prototype list or an empty list. */
3158 if (id_list_ok
3159 && !attrs
3160 && c_parser_next_token_is (parser, CPP_NAME)
3161 && c_parser_peek_token (parser)->id_kind == C_ID_ID
3163 /* Look ahead to detect typos in type names. */
3164 && c_parser_peek_2nd_token (parser)->type != CPP_NAME
3165 && c_parser_peek_2nd_token (parser)->type != CPP_MULT
3166 && c_parser_peek_2nd_token (parser)->type != CPP_OPEN_PAREN
3167 && c_parser_peek_2nd_token (parser)->type != CPP_OPEN_SQUARE)
3169 tree list = NULL_TREE, *nextp = &list;
3170 while (c_parser_next_token_is (parser, CPP_NAME)
3171 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
3173 *nextp = build_tree_list (NULL_TREE,
3174 c_parser_peek_token (parser)->value);
3175 nextp = & TREE_CHAIN (*nextp);
3176 c_parser_consume_token (parser);
3177 if (c_parser_next_token_is_not (parser, CPP_COMMA))
3178 break;
3179 c_parser_consume_token (parser);
3180 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3182 c_parser_error (parser, "expected identifier");
3183 break;
3186 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3188 struct c_arg_info *ret = build_arg_info ();
3189 ret->types = list;
3190 c_parser_consume_token (parser);
3191 pop_scope ();
3192 return ret;
3194 else
3196 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3197 "expected %<)%>");
3198 pop_scope ();
3199 return NULL;
3202 else
3204 struct c_arg_info *ret = c_parser_parms_list_declarator (parser, attrs,
3205 NULL);
3206 pop_scope ();
3207 return ret;
3211 /* Parse a parameter list (possibly empty), including the closing
3212 parenthesis but not the opening one. ATTRS are the attributes at
3213 the start of the list. EXPR is NULL or an expression that needs to
3214 be evaluated for the side effects of array size expressions in the
3215 parameters. */
3217 static struct c_arg_info *
3218 c_parser_parms_list_declarator (c_parser *parser, tree attrs, tree expr)
3220 bool bad_parm = false;
3222 /* ??? Following the old parser, forward parameter declarations may
3223 use abstract declarators, and if no real parameter declarations
3224 follow the forward declarations then this is not diagnosed. Also
3225 note as above that attributes are ignored as the only contents of
3226 the parentheses, or as the only contents after forward
3227 declarations. */
3228 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3230 struct c_arg_info *ret = build_arg_info ();
3231 c_parser_consume_token (parser);
3232 return ret;
3234 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
3236 struct c_arg_info *ret = build_arg_info ();
3238 if (flag_allow_parameterless_variadic_functions)
3240 /* F (...) is allowed. */
3241 ret->types = NULL_TREE;
3243 else
3245 /* Suppress -Wold-style-definition for this case. */
3246 ret->types = error_mark_node;
3247 error_at (c_parser_peek_token (parser)->location,
3248 "ISO C requires a named argument before %<...%>");
3250 c_parser_consume_token (parser);
3251 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3253 c_parser_consume_token (parser);
3254 return ret;
3256 else
3258 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3259 "expected %<)%>");
3260 return NULL;
3263 /* Nonempty list of parameters, either terminated with semicolon
3264 (forward declarations; recurse) or with close parenthesis (normal
3265 function) or with ", ... )" (variadic function). */
3266 while (true)
3268 /* Parse a parameter. */
3269 struct c_parm *parm = c_parser_parameter_declaration (parser, attrs);
3270 attrs = NULL_TREE;
3271 if (parm == NULL)
3272 bad_parm = true;
3273 else
3274 push_parm_decl (parm, &expr);
3275 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
3277 tree new_attrs;
3278 c_parser_consume_token (parser);
3279 mark_forward_parm_decls ();
3280 new_attrs = c_parser_attributes (parser);
3281 return c_parser_parms_list_declarator (parser, new_attrs, expr);
3283 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3285 c_parser_consume_token (parser);
3286 if (bad_parm)
3287 return NULL;
3288 else
3289 return get_parm_info (false, expr);
3291 if (!c_parser_require (parser, CPP_COMMA,
3292 "expected %<;%>, %<,%> or %<)%>"))
3294 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
3295 return NULL;
3297 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
3299 c_parser_consume_token (parser);
3300 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3302 c_parser_consume_token (parser);
3303 if (bad_parm)
3304 return NULL;
3305 else
3306 return get_parm_info (true, expr);
3308 else
3310 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3311 "expected %<)%>");
3312 return NULL;
3318 /* Parse a parameter declaration. ATTRS are the attributes at the
3319 start of the declaration if it is the first parameter. */
3321 static struct c_parm *
3322 c_parser_parameter_declaration (c_parser *parser, tree attrs)
3324 struct c_declspecs *specs;
3325 struct c_declarator *declarator;
3326 tree prefix_attrs;
3327 tree postfix_attrs = NULL_TREE;
3328 bool dummy = false;
3330 /* Accept #pragmas between parameter declarations. */
3331 while (c_parser_next_token_is (parser, CPP_PRAGMA))
3332 c_parser_pragma (parser, pragma_external);
3334 if (!c_parser_next_token_starts_declspecs (parser))
3336 c_token *token = c_parser_peek_token (parser);
3337 if (parser->error)
3338 return NULL;
3339 c_parser_set_source_position_from_token (token);
3340 if (c_parser_next_tokens_start_typename (parser, cla_prefer_type))
3342 error ("unknown type name %qE", token->value);
3343 parser->error = true;
3345 /* ??? In some Objective-C cases '...' isn't applicable so there
3346 should be a different message. */
3347 else
3348 c_parser_error (parser,
3349 "expected declaration specifiers or %<...%>");
3350 c_parser_skip_to_end_of_parameter (parser);
3351 return NULL;
3353 specs = build_null_declspecs ();
3354 if (attrs)
3356 declspecs_add_attrs (input_location, specs, attrs);
3357 attrs = NULL_TREE;
3359 c_parser_declspecs (parser, specs, true, true, true, cla_nonabstract_decl);
3360 finish_declspecs (specs);
3361 pending_xref_error ();
3362 prefix_attrs = specs->attrs;
3363 specs->attrs = NULL_TREE;
3364 declarator = c_parser_declarator (parser,
3365 specs->typespec_kind != ctsk_none,
3366 C_DTR_PARM, &dummy);
3367 if (declarator == NULL)
3369 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
3370 return NULL;
3372 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
3373 postfix_attrs = c_parser_attributes (parser);
3374 return build_c_parm (specs, chainon (postfix_attrs, prefix_attrs),
3375 declarator);
3378 /* Parse a string literal in an asm expression. It should not be
3379 translated, and wide string literals are an error although
3380 permitted by the syntax. This is a GNU extension.
3382 asm-string-literal:
3383 string-literal
3385 ??? At present, following the old parser, the caller needs to have
3386 set lex_untranslated_string to 1. It would be better to follow the
3387 C++ parser rather than using this kludge. */
3389 static tree
3390 c_parser_asm_string_literal (c_parser *parser)
3392 tree str;
3393 int save_flag = warn_overlength_strings;
3394 warn_overlength_strings = 0;
3395 if (c_parser_next_token_is (parser, CPP_STRING))
3397 str = c_parser_peek_token (parser)->value;
3398 c_parser_consume_token (parser);
3400 else if (c_parser_next_token_is (parser, CPP_WSTRING))
3402 error_at (c_parser_peek_token (parser)->location,
3403 "wide string literal in %<asm%>");
3404 str = build_string (1, "");
3405 c_parser_consume_token (parser);
3407 else
3409 c_parser_error (parser, "expected string literal");
3410 str = NULL_TREE;
3412 warn_overlength_strings = save_flag;
3413 return str;
3416 /* Parse a simple asm expression. This is used in restricted
3417 contexts, where a full expression with inputs and outputs does not
3418 make sense. This is a GNU extension.
3420 simple-asm-expr:
3421 asm ( asm-string-literal )
3424 static tree
3425 c_parser_simple_asm_expr (c_parser *parser)
3427 tree str;
3428 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ASM));
3429 /* ??? Follow the C++ parser rather than using the
3430 lex_untranslated_string kludge. */
3431 parser->lex_untranslated_string = true;
3432 c_parser_consume_token (parser);
3433 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3435 parser->lex_untranslated_string = false;
3436 return NULL_TREE;
3438 str = c_parser_asm_string_literal (parser);
3439 parser->lex_untranslated_string = false;
3440 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
3442 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
3443 return NULL_TREE;
3445 return str;
3448 static tree
3449 c_parser_attribute_any_word (c_parser *parser)
3451 tree attr_name = NULL_TREE;
3453 if (c_parser_next_token_is (parser, CPP_KEYWORD))
3455 /* ??? See comment above about what keywords are accepted here. */
3456 bool ok;
3457 switch (c_parser_peek_token (parser)->keyword)
3459 case RID_STATIC:
3460 case RID_UNSIGNED:
3461 case RID_LONG:
3462 case RID_INT128:
3463 case RID_CONST:
3464 case RID_EXTERN:
3465 case RID_REGISTER:
3466 case RID_TYPEDEF:
3467 case RID_SHORT:
3468 case RID_INLINE:
3469 case RID_NORETURN:
3470 case RID_VOLATILE:
3471 case RID_SIGNED:
3472 case RID_AUTO:
3473 case RID_RESTRICT:
3474 case RID_COMPLEX:
3475 case RID_THREAD:
3476 case RID_INT:
3477 case RID_CHAR:
3478 case RID_FLOAT:
3479 case RID_DOUBLE:
3480 case RID_VOID:
3481 case RID_DFLOAT32:
3482 case RID_DFLOAT64:
3483 case RID_DFLOAT128:
3484 case RID_BOOL:
3485 case RID_FRACT:
3486 case RID_ACCUM:
3487 case RID_SAT:
3488 case RID_TRANSACTION_ATOMIC:
3489 case RID_TRANSACTION_CANCEL:
3490 ok = true;
3491 break;
3492 default:
3493 ok = false;
3494 break;
3496 if (!ok)
3497 return NULL_TREE;
3499 /* Accept __attribute__((__const)) as __attribute__((const)) etc. */
3500 attr_name = ridpointers[(int) c_parser_peek_token (parser)->keyword];
3502 else if (c_parser_next_token_is (parser, CPP_NAME))
3503 attr_name = c_parser_peek_token (parser)->value;
3505 return attr_name;
3508 /* Parse (possibly empty) attributes. This is a GNU extension.
3510 attributes:
3511 empty
3512 attributes attribute
3514 attribute:
3515 __attribute__ ( ( attribute-list ) )
3517 attribute-list:
3518 attrib
3519 attribute_list , attrib
3521 attrib:
3522 empty
3523 any-word
3524 any-word ( identifier )
3525 any-word ( identifier , nonempty-expr-list )
3526 any-word ( expr-list )
3528 where the "identifier" must not be declared as a type, and
3529 "any-word" may be any identifier (including one declared as a
3530 type), a reserved word storage class specifier, type specifier or
3531 type qualifier. ??? This still leaves out most reserved keywords
3532 (following the old parser), shouldn't we include them, and why not
3533 allow identifiers declared as types to start the arguments? */
3535 static tree
3536 c_parser_attributes (c_parser *parser)
3538 tree attrs = NULL_TREE;
3539 while (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
3541 /* ??? Follow the C++ parser rather than using the
3542 lex_untranslated_string kludge. */
3543 parser->lex_untranslated_string = true;
3544 c_parser_consume_token (parser);
3545 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3547 parser->lex_untranslated_string = false;
3548 return attrs;
3550 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3552 parser->lex_untranslated_string = false;
3553 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
3554 return attrs;
3556 /* Parse the attribute list. */
3557 while (c_parser_next_token_is (parser, CPP_COMMA)
3558 || c_parser_next_token_is (parser, CPP_NAME)
3559 || c_parser_next_token_is (parser, CPP_KEYWORD))
3561 tree attr, attr_name, attr_args;
3562 vec<tree, va_gc> *expr_list;
3563 if (c_parser_next_token_is (parser, CPP_COMMA))
3565 c_parser_consume_token (parser);
3566 continue;
3569 attr_name = c_parser_attribute_any_word (parser);
3570 if (attr_name == NULL)
3571 break;
3572 c_parser_consume_token (parser);
3573 if (c_parser_next_token_is_not (parser, CPP_OPEN_PAREN))
3575 attr = build_tree_list (attr_name, NULL_TREE);
3576 attrs = chainon (attrs, attr);
3577 continue;
3579 c_parser_consume_token (parser);
3580 /* Parse the attribute contents. If they start with an
3581 identifier which is followed by a comma or close
3582 parenthesis, then the arguments start with that
3583 identifier; otherwise they are an expression list.
3584 In objective-c the identifier may be a classname. */
3585 if (c_parser_next_token_is (parser, CPP_NAME)
3586 && (c_parser_peek_token (parser)->id_kind == C_ID_ID
3587 || (c_dialect_objc ()
3588 && c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME))
3589 && ((c_parser_peek_2nd_token (parser)->type == CPP_COMMA)
3590 || (c_parser_peek_2nd_token (parser)->type
3591 == CPP_CLOSE_PAREN)))
3593 tree arg1 = c_parser_peek_token (parser)->value;
3594 c_parser_consume_token (parser);
3595 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3596 attr_args = build_tree_list (NULL_TREE, arg1);
3597 else
3599 tree tree_list;
3600 c_parser_consume_token (parser);
3601 expr_list = c_parser_expr_list (parser, false, true,
3602 NULL, NULL, NULL);
3603 tree_list = build_tree_list_vec (expr_list);
3604 attr_args = tree_cons (NULL_TREE, arg1, tree_list);
3605 release_tree_vector (expr_list);
3608 else
3610 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3611 attr_args = NULL_TREE;
3612 else
3614 expr_list = c_parser_expr_list (parser, false, true,
3615 NULL, NULL, NULL);
3616 attr_args = build_tree_list_vec (expr_list);
3617 release_tree_vector (expr_list);
3620 attr = build_tree_list (attr_name, attr_args);
3621 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3622 c_parser_consume_token (parser);
3623 else
3625 parser->lex_untranslated_string = false;
3626 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3627 "expected %<)%>");
3628 return attrs;
3630 attrs = chainon (attrs, attr);
3632 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3633 c_parser_consume_token (parser);
3634 else
3636 parser->lex_untranslated_string = false;
3637 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3638 "expected %<)%>");
3639 return attrs;
3641 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3642 c_parser_consume_token (parser);
3643 else
3645 parser->lex_untranslated_string = false;
3646 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3647 "expected %<)%>");
3648 return attrs;
3650 parser->lex_untranslated_string = false;
3652 return attrs;
3655 /* Parse a type name (C90 6.5.5, C99 6.7.6).
3657 type-name:
3658 specifier-qualifier-list abstract-declarator[opt]
3661 static struct c_type_name *
3662 c_parser_type_name (c_parser *parser)
3664 struct c_declspecs *specs = build_null_declspecs ();
3665 struct c_declarator *declarator;
3666 struct c_type_name *ret;
3667 bool dummy = false;
3668 c_parser_declspecs (parser, specs, false, true, true, cla_prefer_type);
3669 if (!specs->declspecs_seen_p)
3671 c_parser_error (parser, "expected specifier-qualifier-list");
3672 return NULL;
3674 if (specs->type != error_mark_node)
3676 pending_xref_error ();
3677 finish_declspecs (specs);
3679 declarator = c_parser_declarator (parser,
3680 specs->typespec_kind != ctsk_none,
3681 C_DTR_ABSTRACT, &dummy);
3682 if (declarator == NULL)
3683 return NULL;
3684 ret = XOBNEW (&parser_obstack, struct c_type_name);
3685 ret->specs = specs;
3686 ret->declarator = declarator;
3687 return ret;
3690 /* Parse an initializer (C90 6.5.7, C99 6.7.8).
3692 initializer:
3693 assignment-expression
3694 { initializer-list }
3695 { initializer-list , }
3697 initializer-list:
3698 designation[opt] initializer
3699 initializer-list , designation[opt] initializer
3701 designation:
3702 designator-list =
3704 designator-list:
3705 designator
3706 designator-list designator
3708 designator:
3709 array-designator
3710 . identifier
3712 array-designator:
3713 [ constant-expression ]
3715 GNU extensions:
3717 initializer:
3720 designation:
3721 array-designator
3722 identifier :
3724 array-designator:
3725 [ constant-expression ... constant-expression ]
3727 Any expression without commas is accepted in the syntax for the
3728 constant-expressions, with non-constant expressions rejected later.
3730 This function is only used for top-level initializers; for nested
3731 ones, see c_parser_initval. */
3733 static struct c_expr
3734 c_parser_initializer (c_parser *parser)
3736 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
3737 return c_parser_braced_init (parser, NULL_TREE, false);
3738 else
3740 struct c_expr ret;
3741 location_t loc = c_parser_peek_token (parser)->location;
3742 ret = c_parser_expr_no_commas (parser, NULL);
3743 if (TREE_CODE (ret.value) != STRING_CST
3744 && TREE_CODE (ret.value) != COMPOUND_LITERAL_EXPR)
3745 ret = default_function_array_read_conversion (loc, ret);
3746 return ret;
3750 /* Parse a braced initializer list. TYPE is the type specified for a
3751 compound literal, and NULL_TREE for other initializers and for
3752 nested braced lists. NESTED_P is true for nested braced lists,
3753 false for the list of a compound literal or the list that is the
3754 top-level initializer in a declaration. */
3756 static struct c_expr
3757 c_parser_braced_init (c_parser *parser, tree type, bool nested_p)
3759 struct c_expr ret;
3760 struct obstack braced_init_obstack;
3761 location_t brace_loc = c_parser_peek_token (parser)->location;
3762 gcc_obstack_init (&braced_init_obstack);
3763 gcc_assert (c_parser_next_token_is (parser, CPP_OPEN_BRACE));
3764 c_parser_consume_token (parser);
3765 if (nested_p)
3766 push_init_level (0, &braced_init_obstack);
3767 else
3768 really_start_incremental_init (type);
3769 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3771 pedwarn (brace_loc, OPT_Wpedantic, "ISO C forbids empty initializer braces");
3773 else
3775 /* Parse a non-empty initializer list, possibly with a trailing
3776 comma. */
3777 while (true)
3779 c_parser_initelt (parser, &braced_init_obstack);
3780 if (parser->error)
3781 break;
3782 if (c_parser_next_token_is (parser, CPP_COMMA))
3783 c_parser_consume_token (parser);
3784 else
3785 break;
3786 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3787 break;
3790 if (c_parser_next_token_is_not (parser, CPP_CLOSE_BRACE))
3792 ret.value = error_mark_node;
3793 ret.original_code = ERROR_MARK;
3794 ret.original_type = NULL;
3795 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, "expected %<}%>");
3796 pop_init_level (0, &braced_init_obstack);
3797 obstack_free (&braced_init_obstack, NULL);
3798 return ret;
3800 c_parser_consume_token (parser);
3801 ret = pop_init_level (0, &braced_init_obstack);
3802 obstack_free (&braced_init_obstack, NULL);
3803 return ret;
3806 /* Parse a nested initializer, including designators. */
3808 static void
3809 c_parser_initelt (c_parser *parser, struct obstack * braced_init_obstack)
3811 /* Parse any designator or designator list. A single array
3812 designator may have the subsequent "=" omitted in GNU C, but a
3813 longer list or a structure member designator may not. */
3814 if (c_parser_next_token_is (parser, CPP_NAME)
3815 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
3817 /* Old-style structure member designator. */
3818 set_init_label (c_parser_peek_token (parser)->value,
3819 braced_init_obstack);
3820 /* Use the colon as the error location. */
3821 pedwarn (c_parser_peek_2nd_token (parser)->location, OPT_Wpedantic,
3822 "obsolete use of designated initializer with %<:%>");
3823 c_parser_consume_token (parser);
3824 c_parser_consume_token (parser);
3826 else
3828 /* des_seen is 0 if there have been no designators, 1 if there
3829 has been a single array designator and 2 otherwise. */
3830 int des_seen = 0;
3831 /* Location of a designator. */
3832 location_t des_loc = UNKNOWN_LOCATION; /* Quiet warning. */
3833 while (c_parser_next_token_is (parser, CPP_OPEN_SQUARE)
3834 || c_parser_next_token_is (parser, CPP_DOT))
3836 int des_prev = des_seen;
3837 if (!des_seen)
3838 des_loc = c_parser_peek_token (parser)->location;
3839 if (des_seen < 2)
3840 des_seen++;
3841 if (c_parser_next_token_is (parser, CPP_DOT))
3843 des_seen = 2;
3844 c_parser_consume_token (parser);
3845 if (c_parser_next_token_is (parser, CPP_NAME))
3847 set_init_label (c_parser_peek_token (parser)->value,
3848 braced_init_obstack);
3849 c_parser_consume_token (parser);
3851 else
3853 struct c_expr init;
3854 init.value = error_mark_node;
3855 init.original_code = ERROR_MARK;
3856 init.original_type = NULL;
3857 c_parser_error (parser, "expected identifier");
3858 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
3859 process_init_element (init, false, braced_init_obstack);
3860 return;
3863 else
3865 tree first, second;
3866 location_t ellipsis_loc = UNKNOWN_LOCATION; /* Quiet warning. */
3867 /* ??? Following the old parser, [ objc-receiver
3868 objc-message-args ] is accepted as an initializer,
3869 being distinguished from a designator by what follows
3870 the first assignment expression inside the square
3871 brackets, but after a first array designator a
3872 subsequent square bracket is for Objective-C taken to
3873 start an expression, using the obsolete form of
3874 designated initializer without '=', rather than
3875 possibly being a second level of designation: in LALR
3876 terms, the '[' is shifted rather than reducing
3877 designator to designator-list. */
3878 if (des_prev == 1 && c_dialect_objc ())
3880 des_seen = des_prev;
3881 break;
3883 if (des_prev == 0 && c_dialect_objc ())
3885 /* This might be an array designator or an
3886 Objective-C message expression. If the former,
3887 continue parsing here; if the latter, parse the
3888 remainder of the initializer given the starting
3889 primary-expression. ??? It might make sense to
3890 distinguish when des_prev == 1 as well; see
3891 previous comment. */
3892 tree rec, args;
3893 struct c_expr mexpr;
3894 c_parser_consume_token (parser);
3895 if (c_parser_peek_token (parser)->type == CPP_NAME
3896 && ((c_parser_peek_token (parser)->id_kind
3897 == C_ID_TYPENAME)
3898 || (c_parser_peek_token (parser)->id_kind
3899 == C_ID_CLASSNAME)))
3901 /* Type name receiver. */
3902 tree id = c_parser_peek_token (parser)->value;
3903 c_parser_consume_token (parser);
3904 rec = objc_get_class_reference (id);
3905 goto parse_message_args;
3907 first = c_parser_expr_no_commas (parser, NULL).value;
3908 mark_exp_read (first);
3909 if (c_parser_next_token_is (parser, CPP_ELLIPSIS)
3910 || c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
3911 goto array_desig_after_first;
3912 /* Expression receiver. So far only one part
3913 without commas has been parsed; there might be
3914 more of the expression. */
3915 rec = first;
3916 while (c_parser_next_token_is (parser, CPP_COMMA))
3918 struct c_expr next;
3919 location_t comma_loc, exp_loc;
3920 comma_loc = c_parser_peek_token (parser)->location;
3921 c_parser_consume_token (parser);
3922 exp_loc = c_parser_peek_token (parser)->location;
3923 next = c_parser_expr_no_commas (parser, NULL);
3924 next = default_function_array_read_conversion (exp_loc,
3925 next);
3926 rec = build_compound_expr (comma_loc, rec, next.value);
3928 parse_message_args:
3929 /* Now parse the objc-message-args. */
3930 args = c_parser_objc_message_args (parser);
3931 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
3932 "expected %<]%>");
3933 mexpr.value
3934 = objc_build_message_expr (rec, args);
3935 mexpr.original_code = ERROR_MARK;
3936 mexpr.original_type = NULL;
3937 /* Now parse and process the remainder of the
3938 initializer, starting with this message
3939 expression as a primary-expression. */
3940 c_parser_initval (parser, &mexpr, braced_init_obstack);
3941 return;
3943 c_parser_consume_token (parser);
3944 first = c_parser_expr_no_commas (parser, NULL).value;
3945 mark_exp_read (first);
3946 array_desig_after_first:
3947 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
3949 ellipsis_loc = c_parser_peek_token (parser)->location;
3950 c_parser_consume_token (parser);
3951 second = c_parser_expr_no_commas (parser, NULL).value;
3952 mark_exp_read (second);
3954 else
3955 second = NULL_TREE;
3956 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
3958 c_parser_consume_token (parser);
3959 set_init_index (first, second, braced_init_obstack);
3960 if (second)
3961 pedwarn (ellipsis_loc, OPT_Wpedantic,
3962 "ISO C forbids specifying range of elements to initialize");
3964 else
3965 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
3966 "expected %<]%>");
3969 if (des_seen >= 1)
3971 if (c_parser_next_token_is (parser, CPP_EQ))
3973 if (!flag_isoc99)
3974 pedwarn (des_loc, OPT_Wpedantic,
3975 "ISO C90 forbids specifying subobject to initialize");
3976 c_parser_consume_token (parser);
3978 else
3980 if (des_seen == 1)
3981 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
3982 "obsolete use of designated initializer without %<=%>");
3983 else
3985 struct c_expr init;
3986 init.value = error_mark_node;
3987 init.original_code = ERROR_MARK;
3988 init.original_type = NULL;
3989 c_parser_error (parser, "expected %<=%>");
3990 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
3991 process_init_element (init, false, braced_init_obstack);
3992 return;
3997 c_parser_initval (parser, NULL, braced_init_obstack);
4000 /* Parse a nested initializer; as c_parser_initializer but parses
4001 initializers within braced lists, after any designators have been
4002 applied. If AFTER is not NULL then it is an Objective-C message
4003 expression which is the primary-expression starting the
4004 initializer. */
4006 static void
4007 c_parser_initval (c_parser *parser, struct c_expr *after,
4008 struct obstack * braced_init_obstack)
4010 struct c_expr init;
4011 gcc_assert (!after || c_dialect_objc ());
4012 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE) && !after)
4013 init = c_parser_braced_init (parser, NULL_TREE, true);
4014 else
4016 location_t loc = c_parser_peek_token (parser)->location;
4017 init = c_parser_expr_no_commas (parser, after);
4018 if (init.value != NULL_TREE
4019 && TREE_CODE (init.value) != STRING_CST
4020 && TREE_CODE (init.value) != COMPOUND_LITERAL_EXPR)
4021 init = default_function_array_read_conversion (loc, init);
4023 process_init_element (init, false, braced_init_obstack);
4026 /* Parse a compound statement (possibly a function body) (C90 6.6.2,
4027 C99 6.8.2).
4029 compound-statement:
4030 { block-item-list[opt] }
4031 { label-declarations block-item-list }
4033 block-item-list:
4034 block-item
4035 block-item-list block-item
4037 block-item:
4038 nested-declaration
4039 statement
4041 nested-declaration:
4042 declaration
4044 GNU extensions:
4046 compound-statement:
4047 { label-declarations block-item-list }
4049 nested-declaration:
4050 __extension__ nested-declaration
4051 nested-function-definition
4053 label-declarations:
4054 label-declaration
4055 label-declarations label-declaration
4057 label-declaration:
4058 __label__ identifier-list ;
4060 Allowing the mixing of declarations and code is new in C99. The
4061 GNU syntax also permits (not shown above) labels at the end of
4062 compound statements, which yield an error. We don't allow labels
4063 on declarations; this might seem like a natural extension, but
4064 there would be a conflict between attributes on the label and
4065 prefix attributes on the declaration. ??? The syntax follows the
4066 old parser in requiring something after label declarations.
4067 Although they are erroneous if the labels declared aren't defined,
4068 is it useful for the syntax to be this way?
4070 OpenMP:
4072 block-item:
4073 openmp-directive
4075 openmp-directive:
4076 barrier-directive
4077 flush-directive */
4079 static tree
4080 c_parser_compound_statement (c_parser *parser)
4082 tree stmt;
4083 location_t brace_loc;
4084 brace_loc = c_parser_peek_token (parser)->location;
4085 if (!c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
4087 /* Ensure a scope is entered and left anyway to avoid confusion
4088 if we have just prepared to enter a function body. */
4089 stmt = c_begin_compound_stmt (true);
4090 c_end_compound_stmt (brace_loc, stmt, true);
4091 return error_mark_node;
4093 stmt = c_begin_compound_stmt (true);
4094 c_parser_compound_statement_nostart (parser);
4096 /* If the compound stmt contains array notations, then we expand them. */
4097 if (flag_enable_cilkplus && contains_array_notation_expr (stmt))
4098 stmt = expand_array_notation_exprs (stmt);
4099 return c_end_compound_stmt (brace_loc, stmt, true);
4102 /* Parse a compound statement except for the opening brace. This is
4103 used for parsing both compound statements and statement expressions
4104 (which follow different paths to handling the opening). */
4106 static void
4107 c_parser_compound_statement_nostart (c_parser *parser)
4109 bool last_stmt = false;
4110 bool last_label = false;
4111 bool save_valid_for_pragma = valid_location_for_stdc_pragma_p ();
4112 location_t label_loc = UNKNOWN_LOCATION; /* Quiet warning. */
4113 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
4115 c_parser_consume_token (parser);
4116 return;
4118 mark_valid_location_for_stdc_pragma (true);
4119 if (c_parser_next_token_is_keyword (parser, RID_LABEL))
4121 /* Read zero or more forward-declarations for labels that nested
4122 functions can jump to. */
4123 mark_valid_location_for_stdc_pragma (false);
4124 while (c_parser_next_token_is_keyword (parser, RID_LABEL))
4126 label_loc = c_parser_peek_token (parser)->location;
4127 c_parser_consume_token (parser);
4128 /* Any identifiers, including those declared as type names,
4129 are OK here. */
4130 while (true)
4132 tree label;
4133 if (c_parser_next_token_is_not (parser, CPP_NAME))
4135 c_parser_error (parser, "expected identifier");
4136 break;
4138 label
4139 = declare_label (c_parser_peek_token (parser)->value);
4140 C_DECLARED_LABEL_FLAG (label) = 1;
4141 add_stmt (build_stmt (label_loc, DECL_EXPR, label));
4142 c_parser_consume_token (parser);
4143 if (c_parser_next_token_is (parser, CPP_COMMA))
4144 c_parser_consume_token (parser);
4145 else
4146 break;
4148 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
4150 pedwarn (label_loc, OPT_Wpedantic, "ISO C forbids label declarations");
4152 /* We must now have at least one statement, label or declaration. */
4153 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
4155 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
4156 c_parser_error (parser, "expected declaration or statement");
4157 c_parser_consume_token (parser);
4158 return;
4160 while (c_parser_next_token_is_not (parser, CPP_CLOSE_BRACE))
4162 location_t loc = c_parser_peek_token (parser)->location;
4163 if (c_parser_next_token_is_keyword (parser, RID_CASE)
4164 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
4165 || (c_parser_next_token_is (parser, CPP_NAME)
4166 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
4168 if (c_parser_next_token_is_keyword (parser, RID_CASE))
4169 label_loc = c_parser_peek_2nd_token (parser)->location;
4170 else
4171 label_loc = c_parser_peek_token (parser)->location;
4172 last_label = true;
4173 last_stmt = false;
4174 mark_valid_location_for_stdc_pragma (false);
4175 c_parser_label (parser);
4177 else if (!last_label
4178 && c_parser_next_tokens_start_declaration (parser))
4180 last_label = false;
4181 mark_valid_location_for_stdc_pragma (false);
4182 c_parser_declaration_or_fndef (parser, true, true, true, true, true, NULL);
4183 if (last_stmt)
4184 pedwarn_c90 (loc,
4185 (pedantic && !flag_isoc99)
4186 ? OPT_Wpedantic
4187 : OPT_Wdeclaration_after_statement,
4188 "ISO C90 forbids mixed declarations and code");
4189 last_stmt = false;
4191 else if (!last_label
4192 && c_parser_next_token_is_keyword (parser, RID_EXTENSION))
4194 /* __extension__ can start a declaration, but is also an
4195 unary operator that can start an expression. Consume all
4196 but the last of a possible series of __extension__ to
4197 determine which. */
4198 while (c_parser_peek_2nd_token (parser)->type == CPP_KEYWORD
4199 && (c_parser_peek_2nd_token (parser)->keyword
4200 == RID_EXTENSION))
4201 c_parser_consume_token (parser);
4202 if (c_token_starts_declaration (c_parser_peek_2nd_token (parser)))
4204 int ext;
4205 ext = disable_extension_diagnostics ();
4206 c_parser_consume_token (parser);
4207 last_label = false;
4208 mark_valid_location_for_stdc_pragma (false);
4209 c_parser_declaration_or_fndef (parser, true, true, true, true,
4210 true, NULL);
4211 /* Following the old parser, __extension__ does not
4212 disable this diagnostic. */
4213 restore_extension_diagnostics (ext);
4214 if (last_stmt)
4215 pedwarn_c90 (loc, (pedantic && !flag_isoc99)
4216 ? OPT_Wpedantic
4217 : OPT_Wdeclaration_after_statement,
4218 "ISO C90 forbids mixed declarations and code");
4219 last_stmt = false;
4221 else
4222 goto statement;
4224 else if (c_parser_next_token_is (parser, CPP_PRAGMA))
4226 /* External pragmas, and some omp pragmas, are not associated
4227 with regular c code, and so are not to be considered statements
4228 syntactically. This ensures that the user doesn't put them
4229 places that would turn into syntax errors if the directive
4230 were ignored. */
4231 if (c_parser_pragma (parser, pragma_compound))
4232 last_label = false, last_stmt = true;
4234 else if (c_parser_next_token_is (parser, CPP_EOF))
4236 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
4237 c_parser_error (parser, "expected declaration or statement");
4238 return;
4240 else if (c_parser_next_token_is_keyword (parser, RID_ELSE))
4242 if (parser->in_if_block)
4244 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
4245 error_at (loc, """expected %<}%> before %<else%>");
4246 return;
4248 else
4250 error_at (loc, "%<else%> without a previous %<if%>");
4251 c_parser_consume_token (parser);
4252 continue;
4255 else
4257 statement:
4258 last_label = false;
4259 last_stmt = true;
4260 mark_valid_location_for_stdc_pragma (false);
4261 c_parser_statement_after_labels (parser);
4264 parser->error = false;
4266 if (last_label)
4267 error_at (label_loc, "label at end of compound statement");
4268 c_parser_consume_token (parser);
4269 /* Restore the value we started with. */
4270 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
4273 /* Parse a label (C90 6.6.1, C99 6.8.1).
4275 label:
4276 identifier : attributes[opt]
4277 case constant-expression :
4278 default :
4280 GNU extensions:
4282 label:
4283 case constant-expression ... constant-expression :
4285 The use of attributes on labels is a GNU extension. The syntax in
4286 GNU C accepts any expressions without commas, non-constant
4287 expressions being rejected later. */
4289 static void
4290 c_parser_label (c_parser *parser)
4292 location_t loc1 = c_parser_peek_token (parser)->location;
4293 tree label = NULL_TREE;
4294 if (c_parser_next_token_is_keyword (parser, RID_CASE))
4296 tree exp1, exp2;
4297 c_parser_consume_token (parser);
4298 exp1 = c_parser_expr_no_commas (parser, NULL).value;
4299 if (c_parser_next_token_is (parser, CPP_COLON))
4301 c_parser_consume_token (parser);
4302 label = do_case (loc1, exp1, NULL_TREE);
4304 else if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
4306 c_parser_consume_token (parser);
4307 exp2 = c_parser_expr_no_commas (parser, NULL).value;
4308 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
4309 label = do_case (loc1, exp1, exp2);
4311 else
4312 c_parser_error (parser, "expected %<:%> or %<...%>");
4314 else if (c_parser_next_token_is_keyword (parser, RID_DEFAULT))
4316 c_parser_consume_token (parser);
4317 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
4318 label = do_case (loc1, NULL_TREE, NULL_TREE);
4320 else
4322 tree name = c_parser_peek_token (parser)->value;
4323 tree tlab;
4324 tree attrs;
4325 location_t loc2 = c_parser_peek_token (parser)->location;
4326 gcc_assert (c_parser_next_token_is (parser, CPP_NAME));
4327 c_parser_consume_token (parser);
4328 gcc_assert (c_parser_next_token_is (parser, CPP_COLON));
4329 c_parser_consume_token (parser);
4330 attrs = c_parser_attributes (parser);
4331 tlab = define_label (loc2, name);
4332 if (tlab)
4334 decl_attributes (&tlab, attrs, 0);
4335 label = add_stmt (build_stmt (loc1, LABEL_EXPR, tlab));
4338 if (label)
4340 if (c_parser_next_tokens_start_declaration (parser))
4342 error_at (c_parser_peek_token (parser)->location,
4343 "a label can only be part of a statement and "
4344 "a declaration is not a statement");
4345 c_parser_declaration_or_fndef (parser, /*fndef_ok*/ false,
4346 /*static_assert_ok*/ true,
4347 /*empty_ok*/ true, /*nested*/ true,
4348 /*start_attr_ok*/ true, NULL);
4353 /* Parse a statement (C90 6.6, C99 6.8).
4355 statement:
4356 labeled-statement
4357 compound-statement
4358 expression-statement
4359 selection-statement
4360 iteration-statement
4361 jump-statement
4363 labeled-statement:
4364 label statement
4366 expression-statement:
4367 expression[opt] ;
4369 selection-statement:
4370 if-statement
4371 switch-statement
4373 iteration-statement:
4374 while-statement
4375 do-statement
4376 for-statement
4378 jump-statement:
4379 goto identifier ;
4380 continue ;
4381 break ;
4382 return expression[opt] ;
4384 GNU extensions:
4386 statement:
4387 asm-statement
4389 jump-statement:
4390 goto * expression ;
4392 Objective-C:
4394 statement:
4395 objc-throw-statement
4396 objc-try-catch-statement
4397 objc-synchronized-statement
4399 objc-throw-statement:
4400 @throw expression ;
4401 @throw ;
4403 OpenMP:
4405 statement:
4406 openmp-construct
4408 openmp-construct:
4409 parallel-construct
4410 for-construct
4411 sections-construct
4412 single-construct
4413 parallel-for-construct
4414 parallel-sections-construct
4415 master-construct
4416 critical-construct
4417 atomic-construct
4418 ordered-construct
4420 parallel-construct:
4421 parallel-directive structured-block
4423 for-construct:
4424 for-directive iteration-statement
4426 sections-construct:
4427 sections-directive section-scope
4429 single-construct:
4430 single-directive structured-block
4432 parallel-for-construct:
4433 parallel-for-directive iteration-statement
4435 parallel-sections-construct:
4436 parallel-sections-directive section-scope
4438 master-construct:
4439 master-directive structured-block
4441 critical-construct:
4442 critical-directive structured-block
4444 atomic-construct:
4445 atomic-directive expression-statement
4447 ordered-construct:
4448 ordered-directive structured-block
4450 Transactional Memory:
4452 statement:
4453 transaction-statement
4454 transaction-cancel-statement
4457 static void
4458 c_parser_statement (c_parser *parser)
4460 while (c_parser_next_token_is_keyword (parser, RID_CASE)
4461 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
4462 || (c_parser_next_token_is (parser, CPP_NAME)
4463 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
4464 c_parser_label (parser);
4465 c_parser_statement_after_labels (parser);
4468 /* Parse a statement, other than a labeled statement. */
4470 static void
4471 c_parser_statement_after_labels (c_parser *parser)
4473 location_t loc = c_parser_peek_token (parser)->location;
4474 tree stmt = NULL_TREE;
4475 bool in_if_block = parser->in_if_block;
4476 parser->in_if_block = false;
4477 switch (c_parser_peek_token (parser)->type)
4479 case CPP_OPEN_BRACE:
4480 add_stmt (c_parser_compound_statement (parser));
4481 break;
4482 case CPP_KEYWORD:
4483 switch (c_parser_peek_token (parser)->keyword)
4485 case RID_IF:
4486 c_parser_if_statement (parser);
4487 break;
4488 case RID_SWITCH:
4489 c_parser_switch_statement (parser);
4490 break;
4491 case RID_WHILE:
4492 c_parser_while_statement (parser);
4493 break;
4494 case RID_DO:
4495 c_parser_do_statement (parser);
4496 break;
4497 case RID_FOR:
4498 c_parser_for_statement (parser);
4499 break;
4500 case RID_GOTO:
4501 c_parser_consume_token (parser);
4502 if (c_parser_next_token_is (parser, CPP_NAME))
4504 stmt = c_finish_goto_label (loc,
4505 c_parser_peek_token (parser)->value);
4506 c_parser_consume_token (parser);
4508 else if (c_parser_next_token_is (parser, CPP_MULT))
4510 tree val;
4512 c_parser_consume_token (parser);
4513 val = c_parser_expression (parser).value;
4514 mark_exp_read (val);
4515 stmt = c_finish_goto_ptr (loc, val);
4517 else
4518 c_parser_error (parser, "expected identifier or %<*%>");
4519 goto expect_semicolon;
4520 case RID_CONTINUE:
4521 c_parser_consume_token (parser);
4522 stmt = c_finish_bc_stmt (loc, &c_cont_label, false);
4523 goto expect_semicolon;
4524 case RID_BREAK:
4525 c_parser_consume_token (parser);
4526 stmt = c_finish_bc_stmt (loc, &c_break_label, true);
4527 goto expect_semicolon;
4528 case RID_RETURN:
4529 c_parser_consume_token (parser);
4530 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
4532 stmt = c_finish_return (loc, NULL_TREE, NULL_TREE);
4533 c_parser_consume_token (parser);
4535 else
4537 struct c_expr expr = c_parser_expression_conv (parser);
4538 mark_exp_read (expr.value);
4539 stmt = c_finish_return (loc, expr.value, expr.original_type);
4540 goto expect_semicolon;
4542 break;
4543 case RID_ASM:
4544 stmt = c_parser_asm_statement (parser);
4545 break;
4546 case RID_TRANSACTION_ATOMIC:
4547 case RID_TRANSACTION_RELAXED:
4548 stmt = c_parser_transaction (parser,
4549 c_parser_peek_token (parser)->keyword);
4550 break;
4551 case RID_TRANSACTION_CANCEL:
4552 stmt = c_parser_transaction_cancel (parser);
4553 goto expect_semicolon;
4554 case RID_AT_THROW:
4555 gcc_assert (c_dialect_objc ());
4556 c_parser_consume_token (parser);
4557 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
4559 stmt = objc_build_throw_stmt (loc, NULL_TREE);
4560 c_parser_consume_token (parser);
4562 else
4564 tree expr = c_parser_expression (parser).value;
4565 expr = c_fully_fold (expr, false, NULL);
4566 stmt = objc_build_throw_stmt (loc, expr);
4567 goto expect_semicolon;
4569 break;
4570 case RID_AT_TRY:
4571 gcc_assert (c_dialect_objc ());
4572 c_parser_objc_try_catch_finally_statement (parser);
4573 break;
4574 case RID_AT_SYNCHRONIZED:
4575 gcc_assert (c_dialect_objc ());
4576 c_parser_objc_synchronized_statement (parser);
4577 break;
4578 default:
4579 goto expr_stmt;
4581 break;
4582 case CPP_SEMICOLON:
4583 c_parser_consume_token (parser);
4584 break;
4585 case CPP_CLOSE_PAREN:
4586 case CPP_CLOSE_SQUARE:
4587 /* Avoid infinite loop in error recovery:
4588 c_parser_skip_until_found stops at a closing nesting
4589 delimiter without consuming it, but here we need to consume
4590 it to proceed further. */
4591 c_parser_error (parser, "expected statement");
4592 c_parser_consume_token (parser);
4593 break;
4594 case CPP_PRAGMA:
4595 c_parser_pragma (parser, pragma_stmt);
4596 break;
4597 default:
4598 expr_stmt:
4599 stmt = c_finish_expr_stmt (loc, c_parser_expression_conv (parser).value);
4600 expect_semicolon:
4601 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
4602 break;
4604 /* Two cases cannot and do not have line numbers associated: If stmt
4605 is degenerate, such as "2;", then stmt is an INTEGER_CST, which
4606 cannot hold line numbers. But that's OK because the statement
4607 will either be changed to a MODIFY_EXPR during gimplification of
4608 the statement expr, or discarded. If stmt was compound, but
4609 without new variables, we will have skipped the creation of a
4610 BIND and will have a bare STATEMENT_LIST. But that's OK because
4611 (recursively) all of the component statements should already have
4612 line numbers assigned. ??? Can we discard no-op statements
4613 earlier? */
4614 if (CAN_HAVE_LOCATION_P (stmt)
4615 && EXPR_LOCATION (stmt) == UNKNOWN_LOCATION)
4616 SET_EXPR_LOCATION (stmt, loc);
4618 parser->in_if_block = in_if_block;
4621 /* Parse the condition from an if, do, while or for statements. */
4623 static tree
4624 c_parser_condition (c_parser *parser)
4626 location_t loc = c_parser_peek_token (parser)->location;
4627 tree cond;
4628 cond = c_parser_expression_conv (parser).value;
4629 cond = c_objc_common_truthvalue_conversion (loc, cond);
4630 cond = c_fully_fold (cond, false, NULL);
4631 if (warn_sequence_point)
4632 verify_sequence_points (cond);
4633 return cond;
4636 /* Parse a parenthesized condition from an if, do or while statement.
4638 condition:
4639 ( expression )
4641 static tree
4642 c_parser_paren_condition (c_parser *parser)
4644 tree cond;
4645 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4646 return error_mark_node;
4647 cond = c_parser_condition (parser);
4648 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
4649 return cond;
4652 /* Parse a statement which is a block in C99. */
4654 static tree
4655 c_parser_c99_block_statement (c_parser *parser)
4657 tree block = c_begin_compound_stmt (flag_isoc99);
4658 location_t loc = c_parser_peek_token (parser)->location;
4659 c_parser_statement (parser);
4660 return c_end_compound_stmt (loc, block, flag_isoc99);
4663 /* Parse the body of an if statement. This is just parsing a
4664 statement but (a) it is a block in C99, (b) we track whether the
4665 body is an if statement for the sake of -Wparentheses warnings, (c)
4666 we handle an empty body specially for the sake of -Wempty-body
4667 warnings, and (d) we call parser_compound_statement directly
4668 because c_parser_statement_after_labels resets
4669 parser->in_if_block. */
4671 static tree
4672 c_parser_if_body (c_parser *parser, bool *if_p)
4674 tree block = c_begin_compound_stmt (flag_isoc99);
4675 location_t body_loc = c_parser_peek_token (parser)->location;
4676 while (c_parser_next_token_is_keyword (parser, RID_CASE)
4677 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
4678 || (c_parser_next_token_is (parser, CPP_NAME)
4679 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
4680 c_parser_label (parser);
4681 *if_p = c_parser_next_token_is_keyword (parser, RID_IF);
4682 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
4684 location_t loc = c_parser_peek_token (parser)->location;
4685 add_stmt (build_empty_stmt (loc));
4686 c_parser_consume_token (parser);
4687 if (!c_parser_next_token_is_keyword (parser, RID_ELSE))
4688 warning_at (loc, OPT_Wempty_body,
4689 "suggest braces around empty body in an %<if%> statement");
4691 else if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
4692 add_stmt (c_parser_compound_statement (parser));
4693 else
4694 c_parser_statement_after_labels (parser);
4695 return c_end_compound_stmt (body_loc, block, flag_isoc99);
4698 /* Parse the else body of an if statement. This is just parsing a
4699 statement but (a) it is a block in C99, (b) we handle an empty body
4700 specially for the sake of -Wempty-body warnings. */
4702 static tree
4703 c_parser_else_body (c_parser *parser)
4705 location_t else_loc = c_parser_peek_token (parser)->location;
4706 tree block = c_begin_compound_stmt (flag_isoc99);
4707 while (c_parser_next_token_is_keyword (parser, RID_CASE)
4708 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
4709 || (c_parser_next_token_is (parser, CPP_NAME)
4710 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
4711 c_parser_label (parser);
4712 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
4714 location_t loc = c_parser_peek_token (parser)->location;
4715 warning_at (loc,
4716 OPT_Wempty_body,
4717 "suggest braces around empty body in an %<else%> statement");
4718 add_stmt (build_empty_stmt (loc));
4719 c_parser_consume_token (parser);
4721 else
4722 c_parser_statement_after_labels (parser);
4723 return c_end_compound_stmt (else_loc, block, flag_isoc99);
4726 /* Parse an if statement (C90 6.6.4, C99 6.8.4).
4728 if-statement:
4729 if ( expression ) statement
4730 if ( expression ) statement else statement
4733 static void
4734 c_parser_if_statement (c_parser *parser)
4736 tree block;
4737 location_t loc;
4738 tree cond;
4739 bool first_if = false;
4740 tree first_body, second_body;
4741 bool in_if_block;
4742 tree if_stmt;
4744 gcc_assert (c_parser_next_token_is_keyword (parser, RID_IF));
4745 c_parser_consume_token (parser);
4746 block = c_begin_compound_stmt (flag_isoc99);
4747 loc = c_parser_peek_token (parser)->location;
4748 cond = c_parser_paren_condition (parser);
4749 in_if_block = parser->in_if_block;
4750 parser->in_if_block = true;
4751 first_body = c_parser_if_body (parser, &first_if);
4752 parser->in_if_block = in_if_block;
4753 if (c_parser_next_token_is_keyword (parser, RID_ELSE))
4755 c_parser_consume_token (parser);
4756 second_body = c_parser_else_body (parser);
4758 else
4759 second_body = NULL_TREE;
4760 c_finish_if_stmt (loc, cond, first_body, second_body, first_if);
4761 if_stmt = c_end_compound_stmt (loc, block, flag_isoc99);
4763 /* If the if statement contains array notations, then we expand them. */
4764 if (flag_enable_cilkplus && contains_array_notation_expr (if_stmt))
4765 if_stmt = fix_conditional_array_notations (if_stmt);
4766 add_stmt (if_stmt);
4769 /* Parse a switch statement (C90 6.6.4, C99 6.8.4).
4771 switch-statement:
4772 switch (expression) statement
4775 static void
4776 c_parser_switch_statement (c_parser *parser)
4778 tree block, expr, body, save_break;
4779 location_t switch_loc = c_parser_peek_token (parser)->location;
4780 location_t switch_cond_loc;
4781 gcc_assert (c_parser_next_token_is_keyword (parser, RID_SWITCH));
4782 c_parser_consume_token (parser);
4783 block = c_begin_compound_stmt (flag_isoc99);
4784 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4786 switch_cond_loc = c_parser_peek_token (parser)->location;
4787 expr = c_parser_expression (parser).value;
4788 if (flag_enable_cilkplus && contains_array_notation_expr (expr))
4790 error_at (switch_cond_loc,
4791 "array notations cannot be used as a condition for switch "
4792 "statement");
4793 expr = error_mark_node;
4795 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
4797 else
4799 switch_cond_loc = UNKNOWN_LOCATION;
4800 expr = error_mark_node;
4802 c_start_case (switch_loc, switch_cond_loc, expr);
4803 save_break = c_break_label;
4804 c_break_label = NULL_TREE;
4805 body = c_parser_c99_block_statement (parser);
4806 c_finish_case (body);
4807 if (c_break_label)
4809 location_t here = c_parser_peek_token (parser)->location;
4810 tree t = build1 (LABEL_EXPR, void_type_node, c_break_label);
4811 SET_EXPR_LOCATION (t, here);
4812 add_stmt (t);
4814 c_break_label = save_break;
4815 add_stmt (c_end_compound_stmt (switch_loc, block, flag_isoc99));
4818 /* Parse a while statement (C90 6.6.5, C99 6.8.5).
4820 while-statement:
4821 while (expression) statement
4824 static void
4825 c_parser_while_statement (c_parser *parser)
4827 tree block, cond, body, save_break, save_cont;
4828 location_t loc;
4829 gcc_assert (c_parser_next_token_is_keyword (parser, RID_WHILE));
4830 c_parser_consume_token (parser);
4831 block = c_begin_compound_stmt (flag_isoc99);
4832 loc = c_parser_peek_token (parser)->location;
4833 cond = c_parser_paren_condition (parser);
4834 if (flag_enable_cilkplus && contains_array_notation_expr (cond))
4836 error_at (loc, "array notations cannot be used as a condition for while "
4837 "statement");
4838 cond = error_mark_node;
4840 save_break = c_break_label;
4841 c_break_label = NULL_TREE;
4842 save_cont = c_cont_label;
4843 c_cont_label = NULL_TREE;
4844 body = c_parser_c99_block_statement (parser);
4845 c_finish_loop (loc, cond, NULL, body, c_break_label, c_cont_label, true);
4846 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99));
4847 c_break_label = save_break;
4848 c_cont_label = save_cont;
4851 /* Parse a do statement (C90 6.6.5, C99 6.8.5).
4853 do-statement:
4854 do statement while ( expression ) ;
4857 static void
4858 c_parser_do_statement (c_parser *parser)
4860 tree block, cond, body, save_break, save_cont, new_break, new_cont;
4861 location_t loc;
4862 gcc_assert (c_parser_next_token_is_keyword (parser, RID_DO));
4863 c_parser_consume_token (parser);
4864 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
4865 warning_at (c_parser_peek_token (parser)->location,
4866 OPT_Wempty_body,
4867 "suggest braces around empty body in %<do%> statement");
4868 block = c_begin_compound_stmt (flag_isoc99);
4869 loc = c_parser_peek_token (parser)->location;
4870 save_break = c_break_label;
4871 c_break_label = NULL_TREE;
4872 save_cont = c_cont_label;
4873 c_cont_label = NULL_TREE;
4874 body = c_parser_c99_block_statement (parser);
4875 c_parser_require_keyword (parser, RID_WHILE, "expected %<while%>");
4876 new_break = c_break_label;
4877 c_break_label = save_break;
4878 new_cont = c_cont_label;
4879 c_cont_label = save_cont;
4880 cond = c_parser_paren_condition (parser);
4881 if (flag_enable_cilkplus && contains_array_notation_expr (cond))
4883 error_at (loc, "array notations cannot be used as a condition for a "
4884 "do-while statement");
4885 cond = error_mark_node;
4888 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
4889 c_parser_skip_to_end_of_block_or_statement (parser);
4890 c_finish_loop (loc, cond, NULL, body, new_break, new_cont, false);
4891 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99));
4894 /* Parse a for statement (C90 6.6.5, C99 6.8.5).
4896 for-statement:
4897 for ( expression[opt] ; expression[opt] ; expression[opt] ) statement
4898 for ( nested-declaration expression[opt] ; expression[opt] ) statement
4900 The form with a declaration is new in C99.
4902 ??? In accordance with the old parser, the declaration may be a
4903 nested function, which is then rejected in check_for_loop_decls,
4904 but does it make any sense for this to be included in the grammar?
4905 Note in particular that the nested function does not include a
4906 trailing ';', whereas the "declaration" production includes one.
4907 Also, can we reject bad declarations earlier and cheaper than
4908 check_for_loop_decls?
4910 In Objective-C, there are two additional variants:
4912 foreach-statement:
4913 for ( expression in expresssion ) statement
4914 for ( declaration in expression ) statement
4916 This is inconsistent with C, because the second variant is allowed
4917 even if c99 is not enabled.
4919 The rest of the comment documents these Objective-C foreach-statement.
4921 Here is the canonical example of the first variant:
4922 for (object in array) { do something with object }
4923 we call the first expression ("object") the "object_expression" and
4924 the second expression ("array") the "collection_expression".
4925 object_expression must be an lvalue of type "id" (a generic Objective-C
4926 object) because the loop works by assigning to object_expression the
4927 various objects from the collection_expression. collection_expression
4928 must evaluate to something of type "id" which responds to the method
4929 countByEnumeratingWithState:objects:count:.
4931 The canonical example of the second variant is:
4932 for (id object in array) { do something with object }
4933 which is completely equivalent to
4935 id object;
4936 for (object in array) { do something with object }
4938 Note that initizializing 'object' in some way (eg, "for ((object =
4939 xxx) in array) { do something with object }") is possibly
4940 technically valid, but completely pointless as 'object' will be
4941 assigned to something else as soon as the loop starts. We should
4942 most likely reject it (TODO).
4944 The beginning of the Objective-C foreach-statement looks exactly
4945 like the beginning of the for-statement, and we can tell it is a
4946 foreach-statement only because the initial declaration or
4947 expression is terminated by 'in' instead of ';'.
4950 static void
4951 c_parser_for_statement (c_parser *parser)
4953 tree block, cond, incr, save_break, save_cont, body;
4954 /* The following are only used when parsing an ObjC foreach statement. */
4955 tree object_expression;
4956 /* Silence the bogus uninitialized warning. */
4957 tree collection_expression = NULL;
4958 location_t loc = c_parser_peek_token (parser)->location;
4959 location_t for_loc = c_parser_peek_token (parser)->location;
4960 bool is_foreach_statement = false;
4961 gcc_assert (c_parser_next_token_is_keyword (parser, RID_FOR));
4962 c_parser_consume_token (parser);
4963 /* Open a compound statement in Objective-C as well, just in case this is
4964 as foreach expression. */
4965 block = c_begin_compound_stmt (flag_isoc99 || c_dialect_objc ());
4966 cond = error_mark_node;
4967 incr = error_mark_node;
4968 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4970 /* Parse the initialization declaration or expression. */
4971 object_expression = error_mark_node;
4972 parser->objc_could_be_foreach_context = c_dialect_objc ();
4973 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
4975 parser->objc_could_be_foreach_context = false;
4976 c_parser_consume_token (parser);
4977 c_finish_expr_stmt (loc, NULL_TREE);
4979 else if (c_parser_next_tokens_start_declaration (parser))
4981 c_parser_declaration_or_fndef (parser, true, true, true, true, true,
4982 &object_expression);
4983 parser->objc_could_be_foreach_context = false;
4985 if (c_parser_next_token_is_keyword (parser, RID_IN))
4987 c_parser_consume_token (parser);
4988 is_foreach_statement = true;
4989 if (check_for_loop_decls (for_loc, true) == NULL_TREE)
4990 c_parser_error (parser, "multiple iterating variables in fast enumeration");
4992 else
4993 check_for_loop_decls (for_loc, flag_isoc99);
4995 else if (c_parser_next_token_is_keyword (parser, RID_EXTENSION))
4997 /* __extension__ can start a declaration, but is also an
4998 unary operator that can start an expression. Consume all
4999 but the last of a possible series of __extension__ to
5000 determine which. */
5001 while (c_parser_peek_2nd_token (parser)->type == CPP_KEYWORD
5002 && (c_parser_peek_2nd_token (parser)->keyword
5003 == RID_EXTENSION))
5004 c_parser_consume_token (parser);
5005 if (c_token_starts_declaration (c_parser_peek_2nd_token (parser)))
5007 int ext;
5008 ext = disable_extension_diagnostics ();
5009 c_parser_consume_token (parser);
5010 c_parser_declaration_or_fndef (parser, true, true, true, true,
5011 true, &object_expression);
5012 parser->objc_could_be_foreach_context = false;
5014 restore_extension_diagnostics (ext);
5015 if (c_parser_next_token_is_keyword (parser, RID_IN))
5017 c_parser_consume_token (parser);
5018 is_foreach_statement = true;
5019 if (check_for_loop_decls (for_loc, true) == NULL_TREE)
5020 c_parser_error (parser, "multiple iterating variables in fast enumeration");
5022 else
5023 check_for_loop_decls (for_loc, flag_isoc99);
5025 else
5026 goto init_expr;
5028 else
5030 init_expr:
5032 tree init_expression;
5033 init_expression = c_parser_expression (parser).value;
5034 parser->objc_could_be_foreach_context = false;
5035 if (c_parser_next_token_is_keyword (parser, RID_IN))
5037 c_parser_consume_token (parser);
5038 is_foreach_statement = true;
5039 if (! lvalue_p (init_expression))
5040 c_parser_error (parser, "invalid iterating variable in fast enumeration");
5041 object_expression = c_fully_fold (init_expression, false, NULL);
5043 else
5045 c_finish_expr_stmt (loc, init_expression);
5046 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
5050 /* Parse the loop condition. In the case of a foreach
5051 statement, there is no loop condition. */
5052 gcc_assert (!parser->objc_could_be_foreach_context);
5053 if (!is_foreach_statement)
5055 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5057 c_parser_consume_token (parser);
5058 cond = NULL_TREE;
5060 else
5062 cond = c_parser_condition (parser);
5063 if (flag_enable_cilkplus && contains_array_notation_expr (cond))
5065 error_at (loc, "array notations cannot be used in a "
5066 "condition for a for-loop");
5067 cond = error_mark_node;
5069 c_parser_skip_until_found (parser, CPP_SEMICOLON,
5070 "expected %<;%>");
5073 /* Parse the increment expression (the third expression in a
5074 for-statement). In the case of a foreach-statement, this is
5075 the expression that follows the 'in'. */
5076 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
5078 if (is_foreach_statement)
5080 c_parser_error (parser, "missing collection in fast enumeration");
5081 collection_expression = error_mark_node;
5083 else
5084 incr = c_process_expr_stmt (loc, NULL_TREE);
5086 else
5088 if (is_foreach_statement)
5089 collection_expression = c_fully_fold (c_parser_expression (parser).value,
5090 false, NULL);
5091 else
5092 incr = c_process_expr_stmt (loc, c_parser_expression (parser).value);
5094 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
5096 save_break = c_break_label;
5097 c_break_label = NULL_TREE;
5098 save_cont = c_cont_label;
5099 c_cont_label = NULL_TREE;
5100 body = c_parser_c99_block_statement (parser);
5101 if (is_foreach_statement)
5102 objc_finish_foreach_loop (loc, object_expression, collection_expression, body, c_break_label, c_cont_label);
5103 else
5104 c_finish_loop (loc, cond, incr, body, c_break_label, c_cont_label, true);
5105 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99 || c_dialect_objc ()));
5106 c_break_label = save_break;
5107 c_cont_label = save_cont;
5110 /* Parse an asm statement, a GNU extension. This is a full-blown asm
5111 statement with inputs, outputs, clobbers, and volatile tag
5112 allowed.
5114 asm-statement:
5115 asm type-qualifier[opt] ( asm-argument ) ;
5116 asm type-qualifier[opt] goto ( asm-goto-argument ) ;
5118 asm-argument:
5119 asm-string-literal
5120 asm-string-literal : asm-operands[opt]
5121 asm-string-literal : asm-operands[opt] : asm-operands[opt]
5122 asm-string-literal : asm-operands[opt] : asm-operands[opt] : asm-clobbers[opt]
5124 asm-goto-argument:
5125 asm-string-literal : : asm-operands[opt] : asm-clobbers[opt] \
5126 : asm-goto-operands
5128 Qualifiers other than volatile are accepted in the syntax but
5129 warned for. */
5131 static tree
5132 c_parser_asm_statement (c_parser *parser)
5134 tree quals, str, outputs, inputs, clobbers, labels, ret;
5135 bool simple, is_goto;
5136 location_t asm_loc = c_parser_peek_token (parser)->location;
5137 int section, nsections;
5139 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ASM));
5140 c_parser_consume_token (parser);
5141 if (c_parser_next_token_is_keyword (parser, RID_VOLATILE))
5143 quals = c_parser_peek_token (parser)->value;
5144 c_parser_consume_token (parser);
5146 else if (c_parser_next_token_is_keyword (parser, RID_CONST)
5147 || c_parser_next_token_is_keyword (parser, RID_RESTRICT))
5149 warning_at (c_parser_peek_token (parser)->location,
5151 "%E qualifier ignored on asm",
5152 c_parser_peek_token (parser)->value);
5153 quals = NULL_TREE;
5154 c_parser_consume_token (parser);
5156 else
5157 quals = NULL_TREE;
5159 is_goto = false;
5160 if (c_parser_next_token_is_keyword (parser, RID_GOTO))
5162 c_parser_consume_token (parser);
5163 is_goto = true;
5166 /* ??? Follow the C++ parser rather than using the
5167 lex_untranslated_string kludge. */
5168 parser->lex_untranslated_string = true;
5169 ret = NULL;
5171 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5172 goto error;
5174 str = c_parser_asm_string_literal (parser);
5175 if (str == NULL_TREE)
5176 goto error_close_paren;
5178 simple = true;
5179 outputs = NULL_TREE;
5180 inputs = NULL_TREE;
5181 clobbers = NULL_TREE;
5182 labels = NULL_TREE;
5184 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN) && !is_goto)
5185 goto done_asm;
5187 /* Parse each colon-delimited section of operands. */
5188 nsections = 3 + is_goto;
5189 for (section = 0; section < nsections; ++section)
5191 if (!c_parser_require (parser, CPP_COLON,
5192 is_goto
5193 ? "expected %<:%>"
5194 : "expected %<:%> or %<)%>"))
5195 goto error_close_paren;
5197 /* Once past any colon, we're no longer a simple asm. */
5198 simple = false;
5200 if ((!c_parser_next_token_is (parser, CPP_COLON)
5201 && !c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
5202 || section == 3)
5203 switch (section)
5205 case 0:
5206 /* For asm goto, we don't allow output operands, but reserve
5207 the slot for a future extension that does allow them. */
5208 if (!is_goto)
5209 outputs = c_parser_asm_operands (parser);
5210 break;
5211 case 1:
5212 inputs = c_parser_asm_operands (parser);
5213 break;
5214 case 2:
5215 clobbers = c_parser_asm_clobbers (parser);
5216 break;
5217 case 3:
5218 labels = c_parser_asm_goto_operands (parser);
5219 break;
5220 default:
5221 gcc_unreachable ();
5224 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN) && !is_goto)
5225 goto done_asm;
5228 done_asm:
5229 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
5231 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5232 goto error;
5235 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
5236 c_parser_skip_to_end_of_block_or_statement (parser);
5238 ret = build_asm_stmt (quals, build_asm_expr (asm_loc, str, outputs, inputs,
5239 clobbers, labels, simple));
5241 error:
5242 parser->lex_untranslated_string = false;
5243 return ret;
5245 error_close_paren:
5246 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5247 goto error;
5250 /* Parse asm operands, a GNU extension.
5252 asm-operands:
5253 asm-operand
5254 asm-operands , asm-operand
5256 asm-operand:
5257 asm-string-literal ( expression )
5258 [ identifier ] asm-string-literal ( expression )
5261 static tree
5262 c_parser_asm_operands (c_parser *parser)
5264 tree list = NULL_TREE;
5265 while (true)
5267 tree name, str;
5268 struct c_expr expr;
5269 if (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
5271 c_parser_consume_token (parser);
5272 if (c_parser_next_token_is (parser, CPP_NAME))
5274 tree id = c_parser_peek_token (parser)->value;
5275 c_parser_consume_token (parser);
5276 name = build_string (IDENTIFIER_LENGTH (id),
5277 IDENTIFIER_POINTER (id));
5279 else
5281 c_parser_error (parser, "expected identifier");
5282 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
5283 return NULL_TREE;
5285 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
5286 "expected %<]%>");
5288 else
5289 name = NULL_TREE;
5290 str = c_parser_asm_string_literal (parser);
5291 if (str == NULL_TREE)
5292 return NULL_TREE;
5293 parser->lex_untranslated_string = false;
5294 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5296 parser->lex_untranslated_string = true;
5297 return NULL_TREE;
5299 expr = c_parser_expression (parser);
5300 mark_exp_read (expr.value);
5301 parser->lex_untranslated_string = true;
5302 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
5304 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5305 return NULL_TREE;
5307 list = chainon (list, build_tree_list (build_tree_list (name, str),
5308 expr.value));
5309 if (c_parser_next_token_is (parser, CPP_COMMA))
5310 c_parser_consume_token (parser);
5311 else
5312 break;
5314 return list;
5317 /* Parse asm clobbers, a GNU extension.
5319 asm-clobbers:
5320 asm-string-literal
5321 asm-clobbers , asm-string-literal
5324 static tree
5325 c_parser_asm_clobbers (c_parser *parser)
5327 tree list = NULL_TREE;
5328 while (true)
5330 tree str = c_parser_asm_string_literal (parser);
5331 if (str)
5332 list = tree_cons (NULL_TREE, str, list);
5333 else
5334 return NULL_TREE;
5335 if (c_parser_next_token_is (parser, CPP_COMMA))
5336 c_parser_consume_token (parser);
5337 else
5338 break;
5340 return list;
5343 /* Parse asm goto labels, a GNU extension.
5345 asm-goto-operands:
5346 identifier
5347 asm-goto-operands , identifier
5350 static tree
5351 c_parser_asm_goto_operands (c_parser *parser)
5353 tree list = NULL_TREE;
5354 while (true)
5356 tree name, label;
5358 if (c_parser_next_token_is (parser, CPP_NAME))
5360 c_token *tok = c_parser_peek_token (parser);
5361 name = tok->value;
5362 label = lookup_label_for_goto (tok->location, name);
5363 c_parser_consume_token (parser);
5364 TREE_USED (label) = 1;
5366 else
5368 c_parser_error (parser, "expected identifier");
5369 return NULL_TREE;
5372 name = build_string (IDENTIFIER_LENGTH (name),
5373 IDENTIFIER_POINTER (name));
5374 list = tree_cons (name, label, list);
5375 if (c_parser_next_token_is (parser, CPP_COMMA))
5376 c_parser_consume_token (parser);
5377 else
5378 return nreverse (list);
5382 /* Parse an expression other than a compound expression; that is, an
5383 assignment expression (C90 6.3.16, C99 6.5.16). If AFTER is not
5384 NULL then it is an Objective-C message expression which is the
5385 primary-expression starting the expression as an initializer.
5387 assignment-expression:
5388 conditional-expression
5389 unary-expression assignment-operator assignment-expression
5391 assignment-operator: one of
5392 = *= /= %= += -= <<= >>= &= ^= |=
5394 In GNU C we accept any conditional expression on the LHS and
5395 diagnose the invalid lvalue rather than producing a syntax
5396 error. */
5398 static struct c_expr
5399 c_parser_expr_no_commas (c_parser *parser, struct c_expr *after)
5401 struct c_expr lhs, rhs, ret;
5402 enum tree_code code;
5403 location_t op_location, exp_location;
5404 gcc_assert (!after || c_dialect_objc ());
5405 lhs = c_parser_conditional_expression (parser, after);
5406 op_location = c_parser_peek_token (parser)->location;
5407 switch (c_parser_peek_token (parser)->type)
5409 case CPP_EQ:
5410 code = NOP_EXPR;
5411 break;
5412 case CPP_MULT_EQ:
5413 code = MULT_EXPR;
5414 break;
5415 case CPP_DIV_EQ:
5416 code = TRUNC_DIV_EXPR;
5417 break;
5418 case CPP_MOD_EQ:
5419 code = TRUNC_MOD_EXPR;
5420 break;
5421 case CPP_PLUS_EQ:
5422 code = PLUS_EXPR;
5423 break;
5424 case CPP_MINUS_EQ:
5425 code = MINUS_EXPR;
5426 break;
5427 case CPP_LSHIFT_EQ:
5428 code = LSHIFT_EXPR;
5429 break;
5430 case CPP_RSHIFT_EQ:
5431 code = RSHIFT_EXPR;
5432 break;
5433 case CPP_AND_EQ:
5434 code = BIT_AND_EXPR;
5435 break;
5436 case CPP_XOR_EQ:
5437 code = BIT_XOR_EXPR;
5438 break;
5439 case CPP_OR_EQ:
5440 code = BIT_IOR_EXPR;
5441 break;
5442 default:
5443 return lhs;
5445 c_parser_consume_token (parser);
5446 exp_location = c_parser_peek_token (parser)->location;
5447 rhs = c_parser_expr_no_commas (parser, NULL);
5448 rhs = default_function_array_read_conversion (exp_location, rhs);
5450 ret.value = build_modify_expr (op_location, lhs.value, lhs.original_type,
5451 code, exp_location, rhs.value,
5452 rhs.original_type);
5453 if (code == NOP_EXPR)
5454 ret.original_code = MODIFY_EXPR;
5455 else
5457 TREE_NO_WARNING (ret.value) = 1;
5458 ret.original_code = ERROR_MARK;
5460 ret.original_type = NULL;
5461 return ret;
5464 /* Parse a conditional expression (C90 6.3.15, C99 6.5.15). If AFTER
5465 is not NULL then it is an Objective-C message expression which is
5466 the primary-expression starting the expression as an initializer.
5468 conditional-expression:
5469 logical-OR-expression
5470 logical-OR-expression ? expression : conditional-expression
5472 GNU extensions:
5474 conditional-expression:
5475 logical-OR-expression ? : conditional-expression
5478 static struct c_expr
5479 c_parser_conditional_expression (c_parser *parser, struct c_expr *after)
5481 struct c_expr cond, exp1, exp2, ret;
5482 location_t cond_loc, colon_loc, middle_loc;
5484 gcc_assert (!after || c_dialect_objc ());
5486 cond = c_parser_binary_expression (parser, after, PREC_NONE);
5488 if (c_parser_next_token_is_not (parser, CPP_QUERY))
5489 return cond;
5490 cond_loc = c_parser_peek_token (parser)->location;
5491 cond = default_function_array_read_conversion (cond_loc, cond);
5492 c_parser_consume_token (parser);
5493 if (c_parser_next_token_is (parser, CPP_COLON))
5495 tree eptype = NULL_TREE;
5497 middle_loc = c_parser_peek_token (parser)->location;
5498 pedwarn (middle_loc, OPT_Wpedantic,
5499 "ISO C forbids omitting the middle term of a ?: expression");
5500 warn_for_omitted_condop (middle_loc, cond.value);
5501 if (TREE_CODE (cond.value) == EXCESS_PRECISION_EXPR)
5503 eptype = TREE_TYPE (cond.value);
5504 cond.value = TREE_OPERAND (cond.value, 0);
5506 /* Make sure first operand is calculated only once. */
5507 exp1.value = c_save_expr (default_conversion (cond.value));
5508 if (eptype)
5509 exp1.value = build1 (EXCESS_PRECISION_EXPR, eptype, exp1.value);
5510 exp1.original_type = NULL;
5511 cond.value = c_objc_common_truthvalue_conversion (cond_loc, exp1.value);
5512 c_inhibit_evaluation_warnings += cond.value == truthvalue_true_node;
5514 else
5516 cond.value
5517 = c_objc_common_truthvalue_conversion
5518 (cond_loc, default_conversion (cond.value));
5519 c_inhibit_evaluation_warnings += cond.value == truthvalue_false_node;
5520 exp1 = c_parser_expression_conv (parser);
5521 mark_exp_read (exp1.value);
5522 c_inhibit_evaluation_warnings +=
5523 ((cond.value == truthvalue_true_node)
5524 - (cond.value == truthvalue_false_node));
5527 colon_loc = c_parser_peek_token (parser)->location;
5528 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
5530 c_inhibit_evaluation_warnings -= cond.value == truthvalue_true_node;
5531 ret.value = error_mark_node;
5532 ret.original_code = ERROR_MARK;
5533 ret.original_type = NULL;
5534 return ret;
5537 location_t exp2_loc = c_parser_peek_token (parser)->location;
5538 exp2 = c_parser_conditional_expression (parser, NULL);
5539 exp2 = default_function_array_read_conversion (exp2_loc, exp2);
5541 c_inhibit_evaluation_warnings -= cond.value == truthvalue_true_node;
5542 ret.value = build_conditional_expr (colon_loc, cond.value,
5543 cond.original_code == C_MAYBE_CONST_EXPR,
5544 exp1.value, exp1.original_type,
5545 exp2.value, exp2.original_type);
5546 ret.original_code = ERROR_MARK;
5547 if (exp1.value == error_mark_node || exp2.value == error_mark_node)
5548 ret.original_type = NULL;
5549 else
5551 tree t1, t2;
5553 /* If both sides are enum type, the default conversion will have
5554 made the type of the result be an integer type. We want to
5555 remember the enum types we started with. */
5556 t1 = exp1.original_type ? exp1.original_type : TREE_TYPE (exp1.value);
5557 t2 = exp2.original_type ? exp2.original_type : TREE_TYPE (exp2.value);
5558 ret.original_type = ((t1 != error_mark_node
5559 && t2 != error_mark_node
5560 && (TYPE_MAIN_VARIANT (t1)
5561 == TYPE_MAIN_VARIANT (t2)))
5562 ? t1
5563 : NULL);
5565 return ret;
5568 /* Parse a binary expression; that is, a logical-OR-expression (C90
5569 6.3.5-6.3.14, C99 6.5.5-6.5.14). If AFTER is not NULL then it is
5570 an Objective-C message expression which is the primary-expression
5571 starting the expression as an initializer. PREC is the starting
5572 precedence, usually PREC_NONE.
5574 multiplicative-expression:
5575 cast-expression
5576 multiplicative-expression * cast-expression
5577 multiplicative-expression / cast-expression
5578 multiplicative-expression % cast-expression
5580 additive-expression:
5581 multiplicative-expression
5582 additive-expression + multiplicative-expression
5583 additive-expression - multiplicative-expression
5585 shift-expression:
5586 additive-expression
5587 shift-expression << additive-expression
5588 shift-expression >> additive-expression
5590 relational-expression:
5591 shift-expression
5592 relational-expression < shift-expression
5593 relational-expression > shift-expression
5594 relational-expression <= shift-expression
5595 relational-expression >= shift-expression
5597 equality-expression:
5598 relational-expression
5599 equality-expression == relational-expression
5600 equality-expression != relational-expression
5602 AND-expression:
5603 equality-expression
5604 AND-expression & equality-expression
5606 exclusive-OR-expression:
5607 AND-expression
5608 exclusive-OR-expression ^ AND-expression
5610 inclusive-OR-expression:
5611 exclusive-OR-expression
5612 inclusive-OR-expression | exclusive-OR-expression
5614 logical-AND-expression:
5615 inclusive-OR-expression
5616 logical-AND-expression && inclusive-OR-expression
5618 logical-OR-expression:
5619 logical-AND-expression
5620 logical-OR-expression || logical-AND-expression
5623 static struct c_expr
5624 c_parser_binary_expression (c_parser *parser, struct c_expr *after,
5625 enum c_parser_prec prec)
5627 /* A binary expression is parsed using operator-precedence parsing,
5628 with the operands being cast expressions. All the binary
5629 operators are left-associative. Thus a binary expression is of
5630 form:
5632 E0 op1 E1 op2 E2 ...
5634 which we represent on a stack. On the stack, the precedence
5635 levels are strictly increasing. When a new operator is
5636 encountered of higher precedence than that at the top of the
5637 stack, it is pushed; its LHS is the top expression, and its RHS
5638 is everything parsed until it is popped. When a new operator is
5639 encountered with precedence less than or equal to that at the top
5640 of the stack, triples E[i-1] op[i] E[i] are popped and replaced
5641 by the result of the operation until the operator at the top of
5642 the stack has lower precedence than the new operator or there is
5643 only one element on the stack; then the top expression is the LHS
5644 of the new operator. In the case of logical AND and OR
5645 expressions, we also need to adjust c_inhibit_evaluation_warnings
5646 as appropriate when the operators are pushed and popped. */
5648 struct {
5649 /* The expression at this stack level. */
5650 struct c_expr expr;
5651 /* The precedence of the operator on its left, PREC_NONE at the
5652 bottom of the stack. */
5653 enum c_parser_prec prec;
5654 /* The operation on its left. */
5655 enum tree_code op;
5656 /* The source location of this operation. */
5657 location_t loc;
5658 } stack[NUM_PRECS];
5659 int sp;
5660 /* Location of the binary operator. */
5661 location_t binary_loc = UNKNOWN_LOCATION; /* Quiet warning. */
5662 #define POP \
5663 do { \
5664 switch (stack[sp].op) \
5666 case TRUTH_ANDIF_EXPR: \
5667 c_inhibit_evaluation_warnings -= (stack[sp - 1].expr.value \
5668 == truthvalue_false_node); \
5669 break; \
5670 case TRUTH_ORIF_EXPR: \
5671 c_inhibit_evaluation_warnings -= (stack[sp - 1].expr.value \
5672 == truthvalue_true_node); \
5673 break; \
5674 default: \
5675 break; \
5677 stack[sp - 1].expr \
5678 = default_function_array_read_conversion (stack[sp - 1].loc, \
5679 stack[sp - 1].expr); \
5680 stack[sp].expr \
5681 = default_function_array_read_conversion (stack[sp].loc, \
5682 stack[sp].expr); \
5683 stack[sp - 1].expr = parser_build_binary_op (stack[sp].loc, \
5684 stack[sp].op, \
5685 stack[sp - 1].expr, \
5686 stack[sp].expr); \
5687 sp--; \
5688 } while (0)
5689 gcc_assert (!after || c_dialect_objc ());
5690 stack[0].loc = c_parser_peek_token (parser)->location;
5691 stack[0].expr = c_parser_cast_expression (parser, after);
5692 stack[0].prec = prec;
5693 sp = 0;
5694 while (true)
5696 enum c_parser_prec oprec;
5697 enum tree_code ocode;
5698 if (parser->error)
5699 goto out;
5700 switch (c_parser_peek_token (parser)->type)
5702 case CPP_MULT:
5703 oprec = PREC_MULT;
5704 ocode = MULT_EXPR;
5705 break;
5706 case CPP_DIV:
5707 oprec = PREC_MULT;
5708 ocode = TRUNC_DIV_EXPR;
5709 break;
5710 case CPP_MOD:
5711 oprec = PREC_MULT;
5712 ocode = TRUNC_MOD_EXPR;
5713 break;
5714 case CPP_PLUS:
5715 oprec = PREC_ADD;
5716 ocode = PLUS_EXPR;
5717 break;
5718 case CPP_MINUS:
5719 oprec = PREC_ADD;
5720 ocode = MINUS_EXPR;
5721 break;
5722 case CPP_LSHIFT:
5723 oprec = PREC_SHIFT;
5724 ocode = LSHIFT_EXPR;
5725 break;
5726 case CPP_RSHIFT:
5727 oprec = PREC_SHIFT;
5728 ocode = RSHIFT_EXPR;
5729 break;
5730 case CPP_LESS:
5731 oprec = PREC_REL;
5732 ocode = LT_EXPR;
5733 break;
5734 case CPP_GREATER:
5735 oprec = PREC_REL;
5736 ocode = GT_EXPR;
5737 break;
5738 case CPP_LESS_EQ:
5739 oprec = PREC_REL;
5740 ocode = LE_EXPR;
5741 break;
5742 case CPP_GREATER_EQ:
5743 oprec = PREC_REL;
5744 ocode = GE_EXPR;
5745 break;
5746 case CPP_EQ_EQ:
5747 oprec = PREC_EQ;
5748 ocode = EQ_EXPR;
5749 break;
5750 case CPP_NOT_EQ:
5751 oprec = PREC_EQ;
5752 ocode = NE_EXPR;
5753 break;
5754 case CPP_AND:
5755 oprec = PREC_BITAND;
5756 ocode = BIT_AND_EXPR;
5757 break;
5758 case CPP_XOR:
5759 oprec = PREC_BITXOR;
5760 ocode = BIT_XOR_EXPR;
5761 break;
5762 case CPP_OR:
5763 oprec = PREC_BITOR;
5764 ocode = BIT_IOR_EXPR;
5765 break;
5766 case CPP_AND_AND:
5767 oprec = PREC_LOGAND;
5768 ocode = TRUTH_ANDIF_EXPR;
5769 break;
5770 case CPP_OR_OR:
5771 oprec = PREC_LOGOR;
5772 ocode = TRUTH_ORIF_EXPR;
5773 break;
5774 default:
5775 /* Not a binary operator, so end of the binary
5776 expression. */
5777 goto out;
5779 binary_loc = c_parser_peek_token (parser)->location;
5780 while (oprec <= stack[sp].prec)
5782 if (sp == 0)
5783 goto out;
5784 POP;
5786 c_parser_consume_token (parser);
5787 switch (ocode)
5789 case TRUTH_ANDIF_EXPR:
5790 stack[sp].expr
5791 = default_function_array_read_conversion (stack[sp].loc,
5792 stack[sp].expr);
5793 stack[sp].expr.value = c_objc_common_truthvalue_conversion
5794 (stack[sp].loc, default_conversion (stack[sp].expr.value));
5795 c_inhibit_evaluation_warnings += (stack[sp].expr.value
5796 == truthvalue_false_node);
5797 break;
5798 case TRUTH_ORIF_EXPR:
5799 stack[sp].expr
5800 = default_function_array_read_conversion (stack[sp].loc,
5801 stack[sp].expr);
5802 stack[sp].expr.value = c_objc_common_truthvalue_conversion
5803 (stack[sp].loc, default_conversion (stack[sp].expr.value));
5804 c_inhibit_evaluation_warnings += (stack[sp].expr.value
5805 == truthvalue_true_node);
5806 break;
5807 default:
5808 break;
5810 sp++;
5811 stack[sp].loc = binary_loc;
5812 stack[sp].expr = c_parser_cast_expression (parser, NULL);
5813 stack[sp].prec = oprec;
5814 stack[sp].op = ocode;
5815 stack[sp].loc = binary_loc;
5817 out:
5818 while (sp > 0)
5819 POP;
5820 return stack[0].expr;
5821 #undef POP
5824 /* Parse a cast expression (C90 6.3.4, C99 6.5.4). If AFTER is not
5825 NULL then it is an Objective-C message expression which is the
5826 primary-expression starting the expression as an initializer.
5828 cast-expression:
5829 unary-expression
5830 ( type-name ) unary-expression
5833 static struct c_expr
5834 c_parser_cast_expression (c_parser *parser, struct c_expr *after)
5836 location_t cast_loc = c_parser_peek_token (parser)->location;
5837 gcc_assert (!after || c_dialect_objc ());
5838 if (after)
5839 return c_parser_postfix_expression_after_primary (parser,
5840 cast_loc, *after);
5841 /* If the expression begins with a parenthesized type name, it may
5842 be either a cast or a compound literal; we need to see whether
5843 the next character is '{' to tell the difference. If not, it is
5844 an unary expression. Full detection of unknown typenames here
5845 would require a 3-token lookahead. */
5846 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
5847 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
5849 struct c_type_name *type_name;
5850 struct c_expr ret;
5851 struct c_expr expr;
5852 c_parser_consume_token (parser);
5853 type_name = c_parser_type_name (parser);
5854 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
5855 if (type_name == NULL)
5857 ret.value = error_mark_node;
5858 ret.original_code = ERROR_MARK;
5859 ret.original_type = NULL;
5860 return ret;
5863 /* Save casted types in the function's used types hash table. */
5864 used_types_insert (type_name->specs->type);
5866 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
5867 return c_parser_postfix_expression_after_paren_type (parser, type_name,
5868 cast_loc);
5870 location_t expr_loc = c_parser_peek_token (parser)->location;
5871 expr = c_parser_cast_expression (parser, NULL);
5872 expr = default_function_array_read_conversion (expr_loc, expr);
5874 ret.value = c_cast_expr (cast_loc, type_name, expr.value);
5875 ret.original_code = ERROR_MARK;
5876 ret.original_type = NULL;
5877 return ret;
5879 else
5880 return c_parser_unary_expression (parser);
5883 /* Parse an unary expression (C90 6.3.3, C99 6.5.3).
5885 unary-expression:
5886 postfix-expression
5887 ++ unary-expression
5888 -- unary-expression
5889 unary-operator cast-expression
5890 sizeof unary-expression
5891 sizeof ( type-name )
5893 unary-operator: one of
5894 & * + - ~ !
5896 GNU extensions:
5898 unary-expression:
5899 __alignof__ unary-expression
5900 __alignof__ ( type-name )
5901 && identifier
5903 (C11 permits _Alignof with type names only.)
5905 unary-operator: one of
5906 __extension__ __real__ __imag__
5908 Transactional Memory:
5910 unary-expression:
5911 transaction-expression
5913 In addition, the GNU syntax treats ++ and -- as unary operators, so
5914 they may be applied to cast expressions with errors for non-lvalues
5915 given later. */
5917 static struct c_expr
5918 c_parser_unary_expression (c_parser *parser)
5920 int ext;
5921 struct c_expr ret, op;
5922 location_t op_loc = c_parser_peek_token (parser)->location;
5923 location_t exp_loc;
5924 ret.original_code = ERROR_MARK;
5925 ret.original_type = NULL;
5926 switch (c_parser_peek_token (parser)->type)
5928 case CPP_PLUS_PLUS:
5929 c_parser_consume_token (parser);
5930 exp_loc = c_parser_peek_token (parser)->location;
5931 op = c_parser_cast_expression (parser, NULL);
5933 /* If there is array notations in op, we expand them. */
5934 if (flag_enable_cilkplus && TREE_CODE (op.value) == ARRAY_NOTATION_REF)
5935 return fix_array_notation_expr (exp_loc, PREINCREMENT_EXPR, op);
5936 else
5938 op = default_function_array_read_conversion (exp_loc, op);
5939 return parser_build_unary_op (op_loc, PREINCREMENT_EXPR, op);
5941 case CPP_MINUS_MINUS:
5942 c_parser_consume_token (parser);
5943 exp_loc = c_parser_peek_token (parser)->location;
5944 op = c_parser_cast_expression (parser, NULL);
5946 /* If there is array notations in op, we expand them. */
5947 if (flag_enable_cilkplus && TREE_CODE (op.value) == ARRAY_NOTATION_REF)
5948 return fix_array_notation_expr (exp_loc, PREDECREMENT_EXPR, op);
5949 else
5951 op = default_function_array_read_conversion (exp_loc, op);
5952 return parser_build_unary_op (op_loc, PREDECREMENT_EXPR, op);
5954 case CPP_AND:
5955 c_parser_consume_token (parser);
5956 op = c_parser_cast_expression (parser, NULL);
5957 mark_exp_read (op.value);
5958 return parser_build_unary_op (op_loc, ADDR_EXPR, op);
5959 case CPP_MULT:
5960 c_parser_consume_token (parser);
5961 exp_loc = c_parser_peek_token (parser)->location;
5962 op = c_parser_cast_expression (parser, NULL);
5963 op = default_function_array_read_conversion (exp_loc, op);
5964 ret.value = build_indirect_ref (op_loc, op.value, RO_UNARY_STAR);
5965 return ret;
5966 case CPP_PLUS:
5967 if (!c_dialect_objc () && !in_system_header)
5968 warning_at (op_loc,
5969 OPT_Wtraditional,
5970 "traditional C rejects the unary plus operator");
5971 c_parser_consume_token (parser);
5972 exp_loc = c_parser_peek_token (parser)->location;
5973 op = c_parser_cast_expression (parser, NULL);
5974 op = default_function_array_read_conversion (exp_loc, op);
5975 return parser_build_unary_op (op_loc, CONVERT_EXPR, op);
5976 case CPP_MINUS:
5977 c_parser_consume_token (parser);
5978 exp_loc = c_parser_peek_token (parser)->location;
5979 op = c_parser_cast_expression (parser, NULL);
5980 op = default_function_array_read_conversion (exp_loc, op);
5981 return parser_build_unary_op (op_loc, NEGATE_EXPR, op);
5982 case CPP_COMPL:
5983 c_parser_consume_token (parser);
5984 exp_loc = c_parser_peek_token (parser)->location;
5985 op = c_parser_cast_expression (parser, NULL);
5986 op = default_function_array_read_conversion (exp_loc, op);
5987 return parser_build_unary_op (op_loc, BIT_NOT_EXPR, op);
5988 case CPP_NOT:
5989 c_parser_consume_token (parser);
5990 exp_loc = c_parser_peek_token (parser)->location;
5991 op = c_parser_cast_expression (parser, NULL);
5992 op = default_function_array_read_conversion (exp_loc, op);
5993 return parser_build_unary_op (op_loc, TRUTH_NOT_EXPR, op);
5994 case CPP_AND_AND:
5995 /* Refer to the address of a label as a pointer. */
5996 c_parser_consume_token (parser);
5997 if (c_parser_next_token_is (parser, CPP_NAME))
5999 ret.value = finish_label_address_expr
6000 (c_parser_peek_token (parser)->value, op_loc);
6001 c_parser_consume_token (parser);
6003 else
6005 c_parser_error (parser, "expected identifier");
6006 ret.value = error_mark_node;
6008 return ret;
6009 case CPP_KEYWORD:
6010 switch (c_parser_peek_token (parser)->keyword)
6012 case RID_SIZEOF:
6013 return c_parser_sizeof_expression (parser);
6014 case RID_ALIGNOF:
6015 return c_parser_alignof_expression (parser);
6016 case RID_EXTENSION:
6017 c_parser_consume_token (parser);
6018 ext = disable_extension_diagnostics ();
6019 ret = c_parser_cast_expression (parser, NULL);
6020 restore_extension_diagnostics (ext);
6021 return ret;
6022 case RID_REALPART:
6023 c_parser_consume_token (parser);
6024 exp_loc = c_parser_peek_token (parser)->location;
6025 op = c_parser_cast_expression (parser, NULL);
6026 op = default_function_array_conversion (exp_loc, op);
6027 return parser_build_unary_op (op_loc, REALPART_EXPR, op);
6028 case RID_IMAGPART:
6029 c_parser_consume_token (parser);
6030 exp_loc = c_parser_peek_token (parser)->location;
6031 op = c_parser_cast_expression (parser, NULL);
6032 op = default_function_array_conversion (exp_loc, op);
6033 return parser_build_unary_op (op_loc, IMAGPART_EXPR, op);
6034 case RID_TRANSACTION_ATOMIC:
6035 case RID_TRANSACTION_RELAXED:
6036 return c_parser_transaction_expression (parser,
6037 c_parser_peek_token (parser)->keyword);
6038 default:
6039 return c_parser_postfix_expression (parser);
6041 default:
6042 return c_parser_postfix_expression (parser);
6046 /* Parse a sizeof expression. */
6048 static struct c_expr
6049 c_parser_sizeof_expression (c_parser *parser)
6051 struct c_expr expr;
6052 location_t expr_loc;
6053 gcc_assert (c_parser_next_token_is_keyword (parser, RID_SIZEOF));
6054 c_parser_consume_token (parser);
6055 c_inhibit_evaluation_warnings++;
6056 in_sizeof++;
6057 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
6058 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
6060 /* Either sizeof ( type-name ) or sizeof unary-expression
6061 starting with a compound literal. */
6062 struct c_type_name *type_name;
6063 c_parser_consume_token (parser);
6064 expr_loc = c_parser_peek_token (parser)->location;
6065 type_name = c_parser_type_name (parser);
6066 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
6067 if (type_name == NULL)
6069 struct c_expr ret;
6070 c_inhibit_evaluation_warnings--;
6071 in_sizeof--;
6072 ret.value = error_mark_node;
6073 ret.original_code = ERROR_MARK;
6074 ret.original_type = NULL;
6075 return ret;
6077 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
6079 expr = c_parser_postfix_expression_after_paren_type (parser,
6080 type_name,
6081 expr_loc);
6082 goto sizeof_expr;
6084 /* sizeof ( type-name ). */
6085 c_inhibit_evaluation_warnings--;
6086 in_sizeof--;
6087 return c_expr_sizeof_type (expr_loc, type_name);
6089 else
6091 expr_loc = c_parser_peek_token (parser)->location;
6092 expr = c_parser_unary_expression (parser);
6093 sizeof_expr:
6094 c_inhibit_evaluation_warnings--;
6095 in_sizeof--;
6096 mark_exp_read (expr.value);
6097 if (TREE_CODE (expr.value) == COMPONENT_REF
6098 && DECL_C_BIT_FIELD (TREE_OPERAND (expr.value, 1)))
6099 error_at (expr_loc, "%<sizeof%> applied to a bit-field");
6100 return c_expr_sizeof_expr (expr_loc, expr);
6104 /* Parse an alignof expression. */
6106 static struct c_expr
6107 c_parser_alignof_expression (c_parser *parser)
6109 struct c_expr expr;
6110 location_t loc = c_parser_peek_token (parser)->location;
6111 tree alignof_spelling = c_parser_peek_token (parser)->value;
6112 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ALIGNOF));
6113 /* A diagnostic is not required for the use of this identifier in
6114 the implementation namespace; only diagnose it for the C11
6115 spelling because of existing code using the other spellings. */
6116 if (!flag_isoc11
6117 && strcmp (IDENTIFIER_POINTER (alignof_spelling), "_Alignof") == 0)
6119 if (flag_isoc99)
6120 pedwarn (loc, OPT_Wpedantic, "ISO C99 does not support %qE",
6121 alignof_spelling);
6122 else
6123 pedwarn (loc, OPT_Wpedantic, "ISO C90 does not support %qE",
6124 alignof_spelling);
6126 c_parser_consume_token (parser);
6127 c_inhibit_evaluation_warnings++;
6128 in_alignof++;
6129 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
6130 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
6132 /* Either __alignof__ ( type-name ) or __alignof__
6133 unary-expression starting with a compound literal. */
6134 location_t loc;
6135 struct c_type_name *type_name;
6136 struct c_expr ret;
6137 c_parser_consume_token (parser);
6138 loc = c_parser_peek_token (parser)->location;
6139 type_name = c_parser_type_name (parser);
6140 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
6141 if (type_name == NULL)
6143 struct c_expr ret;
6144 c_inhibit_evaluation_warnings--;
6145 in_alignof--;
6146 ret.value = error_mark_node;
6147 ret.original_code = ERROR_MARK;
6148 ret.original_type = NULL;
6149 return ret;
6151 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
6153 expr = c_parser_postfix_expression_after_paren_type (parser,
6154 type_name,
6155 loc);
6156 goto alignof_expr;
6158 /* alignof ( type-name ). */
6159 c_inhibit_evaluation_warnings--;
6160 in_alignof--;
6161 ret.value = c_alignof (loc, groktypename (type_name, NULL, NULL));
6162 ret.original_code = ERROR_MARK;
6163 ret.original_type = NULL;
6164 return ret;
6166 else
6168 struct c_expr ret;
6169 expr = c_parser_unary_expression (parser);
6170 alignof_expr:
6171 mark_exp_read (expr.value);
6172 c_inhibit_evaluation_warnings--;
6173 in_alignof--;
6174 pedwarn (loc, OPT_Wpedantic, "ISO C does not allow %<%E (expression)%>",
6175 alignof_spelling);
6176 ret.value = c_alignof_expr (loc, expr.value);
6177 ret.original_code = ERROR_MARK;
6178 ret.original_type = NULL;
6179 return ret;
6183 /* Helper function to read arguments of builtins which are interfaces
6184 for the middle-end nodes like COMPLEX_EXPR, VEC_PERM_EXPR and
6185 others. The name of the builtin is passed using BNAME parameter.
6186 Function returns true if there were no errors while parsing and
6187 stores the arguments in CEXPR_LIST. */
6188 static bool
6189 c_parser_get_builtin_args (c_parser *parser, const char *bname,
6190 vec<c_expr_t, va_gc> **ret_cexpr_list,
6191 bool choose_expr_p)
6193 location_t loc = c_parser_peek_token (parser)->location;
6194 vec<c_expr_t, va_gc> *cexpr_list;
6195 c_expr_t expr;
6196 bool saved_force_folding_builtin_constant_p;
6198 *ret_cexpr_list = NULL;
6199 if (c_parser_next_token_is_not (parser, CPP_OPEN_PAREN))
6201 error_at (loc, "cannot take address of %qs", bname);
6202 return false;
6205 c_parser_consume_token (parser);
6207 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
6209 c_parser_consume_token (parser);
6210 return true;
6213 saved_force_folding_builtin_constant_p
6214 = force_folding_builtin_constant_p;
6215 force_folding_builtin_constant_p |= choose_expr_p;
6216 expr = c_parser_expr_no_commas (parser, NULL);
6217 force_folding_builtin_constant_p
6218 = saved_force_folding_builtin_constant_p;
6219 vec_alloc (cexpr_list, 1);
6220 C_EXPR_APPEND (cexpr_list, expr);
6221 while (c_parser_next_token_is (parser, CPP_COMMA))
6223 c_parser_consume_token (parser);
6224 expr = c_parser_expr_no_commas (parser, NULL);
6225 C_EXPR_APPEND (cexpr_list, expr);
6228 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
6229 return false;
6231 *ret_cexpr_list = cexpr_list;
6232 return true;
6235 /* This represents a single generic-association. */
6237 struct c_generic_association
6239 /* The location of the starting token of the type. */
6240 location_t type_location;
6241 /* The association's type, or NULL_TREE for 'default'. */
6242 tree type;
6243 /* The association's expression. */
6244 struct c_expr expression;
6247 /* Parse a generic-selection. (C11 6.5.1.1).
6249 generic-selection:
6250 _Generic ( assignment-expression , generic-assoc-list )
6252 generic-assoc-list:
6253 generic-association
6254 generic-assoc-list , generic-association
6256 generic-association:
6257 type-name : assignment-expression
6258 default : assignment-expression
6261 static struct c_expr
6262 c_parser_generic_selection (c_parser *parser)
6264 vec<c_generic_association> associations = vNULL;
6265 struct c_expr selector, error_expr;
6266 tree selector_type;
6267 struct c_generic_association matched_assoc;
6268 bool match_found = false;
6269 location_t generic_loc, selector_loc;
6271 error_expr.original_code = ERROR_MARK;
6272 error_expr.original_type = NULL;
6273 error_expr.value = error_mark_node;
6274 matched_assoc.type_location = UNKNOWN_LOCATION;
6275 matched_assoc.type = NULL_TREE;
6276 matched_assoc.expression = error_expr;
6278 gcc_assert (c_parser_next_token_is_keyword (parser, RID_GENERIC));
6279 generic_loc = c_parser_peek_token (parser)->location;
6280 c_parser_consume_token (parser);
6281 if (!flag_isoc11)
6283 if (flag_isoc99)
6284 pedwarn (generic_loc, OPT_Wpedantic,
6285 "ISO C99 does not support %<_Generic%>");
6286 else
6287 pedwarn (generic_loc, OPT_Wpedantic,
6288 "ISO C90 does not support %<_Generic%>");
6291 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6292 return error_expr;
6294 c_inhibit_evaluation_warnings++;
6295 selector_loc = c_parser_peek_token (parser)->location;
6296 selector = c_parser_expr_no_commas (parser, NULL);
6297 selector = default_function_array_conversion (selector_loc, selector);
6298 c_inhibit_evaluation_warnings--;
6300 if (selector.value == error_mark_node)
6302 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6303 return selector;
6305 selector_type = TREE_TYPE (selector.value);
6306 /* In ISO C terms, rvalues (including the controlling expression of
6307 _Generic) do not have qualified types. */
6308 if (TREE_CODE (selector_type) != ARRAY_TYPE)
6309 selector_type = TYPE_MAIN_VARIANT (selector_type);
6310 /* In ISO C terms, _Noreturn is not part of the type of expressions
6311 such as &abort, but in GCC it is represented internally as a type
6312 qualifier. */
6313 if (FUNCTION_POINTER_TYPE_P (selector_type)
6314 && TYPE_QUALS (TREE_TYPE (selector_type)) != TYPE_UNQUALIFIED)
6315 selector_type
6316 = build_pointer_type (TYPE_MAIN_VARIANT (TREE_TYPE (selector_type)));
6318 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
6320 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6321 return error_expr;
6324 while (1)
6326 struct c_generic_association assoc, *iter;
6327 unsigned int ix;
6328 c_token *token = c_parser_peek_token (parser);
6330 assoc.type_location = token->location;
6331 if (token->type == CPP_KEYWORD && token->keyword == RID_DEFAULT)
6333 c_parser_consume_token (parser);
6334 assoc.type = NULL_TREE;
6336 else
6338 struct c_type_name *type_name;
6340 type_name = c_parser_type_name (parser);
6341 if (type_name == NULL)
6343 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6344 goto error_exit;
6346 assoc.type = groktypename (type_name, NULL, NULL);
6347 if (assoc.type == error_mark_node)
6349 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6350 goto error_exit;
6353 if (TREE_CODE (assoc.type) == FUNCTION_TYPE)
6354 error_at (assoc.type_location,
6355 "%<_Generic%> association has function type");
6356 else if (!COMPLETE_TYPE_P (assoc.type))
6357 error_at (assoc.type_location,
6358 "%<_Generic%> association has incomplete type");
6360 if (variably_modified_type_p (assoc.type, NULL_TREE))
6361 error_at (assoc.type_location,
6362 "%<_Generic%> association has "
6363 "variable length type");
6366 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
6368 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6369 goto error_exit;
6372 assoc.expression = c_parser_expr_no_commas (parser, NULL);
6373 if (assoc.expression.value == error_mark_node)
6375 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6376 goto error_exit;
6379 for (ix = 0; associations.iterate (ix, &iter); ++ix)
6381 if (assoc.type == NULL_TREE)
6383 if (iter->type == NULL_TREE)
6385 error_at (assoc.type_location,
6386 "duplicate %<default%> case in %<_Generic%>");
6387 inform (iter->type_location, "original %<default%> is here");
6390 else if (iter->type != NULL_TREE)
6392 if (comptypes (assoc.type, iter->type))
6394 error_at (assoc.type_location,
6395 "%<_Generic%> specifies two compatible types");
6396 inform (iter->type_location, "compatible type is here");
6401 if (assoc.type == NULL_TREE)
6403 if (!match_found)
6405 matched_assoc = assoc;
6406 match_found = true;
6409 else if (comptypes (assoc.type, selector_type))
6411 if (!match_found || matched_assoc.type == NULL_TREE)
6413 matched_assoc = assoc;
6414 match_found = true;
6416 else
6418 error_at (assoc.type_location,
6419 "%<_Generic> selector matches multiple associations");
6420 inform (matched_assoc.type_location,
6421 "other match is here");
6425 associations.safe_push (assoc);
6427 if (c_parser_peek_token (parser)->type != CPP_COMMA)
6428 break;
6429 c_parser_consume_token (parser);
6432 associations.release ();
6434 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
6436 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6437 return error_expr;
6440 if (!match_found)
6442 error_at (selector_loc, "%<_Generic%> selector of type %qT is not "
6443 "compatible with any association",
6444 selector_type);
6445 return error_expr;
6448 return matched_assoc.expression;
6450 error_exit:
6451 associations.release ();
6452 return error_expr;
6455 /* Parse a postfix expression (C90 6.3.1-6.3.2, C99 6.5.1-6.5.2).
6457 postfix-expression:
6458 primary-expression
6459 postfix-expression [ expression ]
6460 postfix-expression ( argument-expression-list[opt] )
6461 postfix-expression . identifier
6462 postfix-expression -> identifier
6463 postfix-expression ++
6464 postfix-expression --
6465 ( type-name ) { initializer-list }
6466 ( type-name ) { initializer-list , }
6468 argument-expression-list:
6469 argument-expression
6470 argument-expression-list , argument-expression
6472 primary-expression:
6473 identifier
6474 constant
6475 string-literal
6476 ( expression )
6477 generic-selection
6479 GNU extensions:
6481 primary-expression:
6482 __func__
6483 (treated as a keyword in GNU C)
6484 __FUNCTION__
6485 __PRETTY_FUNCTION__
6486 ( compound-statement )
6487 __builtin_va_arg ( assignment-expression , type-name )
6488 __builtin_offsetof ( type-name , offsetof-member-designator )
6489 __builtin_choose_expr ( assignment-expression ,
6490 assignment-expression ,
6491 assignment-expression )
6492 __builtin_types_compatible_p ( type-name , type-name )
6493 __builtin_complex ( assignment-expression , assignment-expression )
6494 __builtin_shuffle ( assignment-expression , assignment-expression )
6495 __builtin_shuffle ( assignment-expression ,
6496 assignment-expression ,
6497 assignment-expression, )
6499 offsetof-member-designator:
6500 identifier
6501 offsetof-member-designator . identifier
6502 offsetof-member-designator [ expression ]
6504 Objective-C:
6506 primary-expression:
6507 [ objc-receiver objc-message-args ]
6508 @selector ( objc-selector-arg )
6509 @protocol ( identifier )
6510 @encode ( type-name )
6511 objc-string-literal
6512 Classname . identifier
6515 static struct c_expr
6516 c_parser_postfix_expression (c_parser *parser)
6518 struct c_expr expr, e1;
6519 struct c_type_name *t1, *t2;
6520 location_t loc = c_parser_peek_token (parser)->location;;
6521 expr.original_code = ERROR_MARK;
6522 expr.original_type = NULL;
6523 switch (c_parser_peek_token (parser)->type)
6525 case CPP_NUMBER:
6526 expr.value = c_parser_peek_token (parser)->value;
6527 loc = c_parser_peek_token (parser)->location;
6528 c_parser_consume_token (parser);
6529 if (TREE_CODE (expr.value) == FIXED_CST
6530 && !targetm.fixed_point_supported_p ())
6532 error_at (loc, "fixed-point types not supported for this target");
6533 expr.value = error_mark_node;
6535 break;
6536 case CPP_CHAR:
6537 case CPP_CHAR16:
6538 case CPP_CHAR32:
6539 case CPP_WCHAR:
6540 expr.value = c_parser_peek_token (parser)->value;
6541 c_parser_consume_token (parser);
6542 break;
6543 case CPP_STRING:
6544 case CPP_STRING16:
6545 case CPP_STRING32:
6546 case CPP_WSTRING:
6547 case CPP_UTF8STRING:
6548 expr.value = c_parser_peek_token (parser)->value;
6549 expr.original_code = STRING_CST;
6550 c_parser_consume_token (parser);
6551 break;
6552 case CPP_OBJC_STRING:
6553 gcc_assert (c_dialect_objc ());
6554 expr.value
6555 = objc_build_string_object (c_parser_peek_token (parser)->value);
6556 c_parser_consume_token (parser);
6557 break;
6558 case CPP_NAME:
6559 switch (c_parser_peek_token (parser)->id_kind)
6561 case C_ID_ID:
6563 tree id = c_parser_peek_token (parser)->value;
6564 c_parser_consume_token (parser);
6565 expr.value = build_external_ref (loc, id,
6566 (c_parser_peek_token (parser)->type
6567 == CPP_OPEN_PAREN),
6568 &expr.original_type);
6569 break;
6571 case C_ID_CLASSNAME:
6573 /* Here we parse the Objective-C 2.0 Class.name dot
6574 syntax. */
6575 tree class_name = c_parser_peek_token (parser)->value;
6576 tree component;
6577 c_parser_consume_token (parser);
6578 gcc_assert (c_dialect_objc ());
6579 if (!c_parser_require (parser, CPP_DOT, "expected %<.%>"))
6581 expr.value = error_mark_node;
6582 break;
6584 if (c_parser_next_token_is_not (parser, CPP_NAME))
6586 c_parser_error (parser, "expected identifier");
6587 expr.value = error_mark_node;
6588 break;
6590 component = c_parser_peek_token (parser)->value;
6591 c_parser_consume_token (parser);
6592 expr.value = objc_build_class_component_ref (class_name,
6593 component);
6594 break;
6596 default:
6597 c_parser_error (parser, "expected expression");
6598 expr.value = error_mark_node;
6599 break;
6601 break;
6602 case CPP_OPEN_PAREN:
6603 /* A parenthesized expression, statement expression or compound
6604 literal. */
6605 if (c_parser_peek_2nd_token (parser)->type == CPP_OPEN_BRACE)
6607 /* A statement expression. */
6608 tree stmt;
6609 location_t brace_loc;
6610 c_parser_consume_token (parser);
6611 brace_loc = c_parser_peek_token (parser)->location;
6612 c_parser_consume_token (parser);
6613 if (!building_stmt_list_p ())
6615 error_at (loc, "braced-group within expression allowed "
6616 "only inside a function");
6617 parser->error = true;
6618 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
6619 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6620 expr.value = error_mark_node;
6621 break;
6623 stmt = c_begin_stmt_expr ();
6624 c_parser_compound_statement_nostart (parser);
6625 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
6626 "expected %<)%>");
6627 pedwarn (loc, OPT_Wpedantic,
6628 "ISO C forbids braced-groups within expressions");
6629 expr.value = c_finish_stmt_expr (brace_loc, stmt);
6630 mark_exp_read (expr.value);
6632 else if (c_token_starts_typename (c_parser_peek_2nd_token (parser)))
6634 /* A compound literal. ??? Can we actually get here rather
6635 than going directly to
6636 c_parser_postfix_expression_after_paren_type from
6637 elsewhere? */
6638 location_t loc;
6639 struct c_type_name *type_name;
6640 c_parser_consume_token (parser);
6641 loc = c_parser_peek_token (parser)->location;
6642 type_name = c_parser_type_name (parser);
6643 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
6644 "expected %<)%>");
6645 if (type_name == NULL)
6647 expr.value = error_mark_node;
6649 else
6650 expr = c_parser_postfix_expression_after_paren_type (parser,
6651 type_name,
6652 loc);
6654 else
6656 /* A parenthesized expression. */
6657 c_parser_consume_token (parser);
6658 expr = c_parser_expression (parser);
6659 if (TREE_CODE (expr.value) == MODIFY_EXPR)
6660 TREE_NO_WARNING (expr.value) = 1;
6661 if (expr.original_code != C_MAYBE_CONST_EXPR)
6662 expr.original_code = ERROR_MARK;
6663 /* Don't change EXPR.ORIGINAL_TYPE. */
6664 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
6665 "expected %<)%>");
6667 break;
6668 case CPP_KEYWORD:
6669 switch (c_parser_peek_token (parser)->keyword)
6671 case RID_FUNCTION_NAME:
6672 case RID_PRETTY_FUNCTION_NAME:
6673 case RID_C99_FUNCTION_NAME:
6674 expr.value = fname_decl (loc,
6675 c_parser_peek_token (parser)->keyword,
6676 c_parser_peek_token (parser)->value);
6677 c_parser_consume_token (parser);
6678 break;
6679 case RID_VA_ARG:
6680 c_parser_consume_token (parser);
6681 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6683 expr.value = error_mark_node;
6684 break;
6686 e1 = c_parser_expr_no_commas (parser, NULL);
6687 mark_exp_read (e1.value);
6688 e1.value = c_fully_fold (e1.value, false, NULL);
6689 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
6691 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6692 expr.value = error_mark_node;
6693 break;
6695 loc = c_parser_peek_token (parser)->location;
6696 t1 = c_parser_type_name (parser);
6697 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
6698 "expected %<)%>");
6699 if (t1 == NULL)
6701 expr.value = error_mark_node;
6703 else
6705 tree type_expr = NULL_TREE;
6706 expr.value = c_build_va_arg (loc, e1.value,
6707 groktypename (t1, &type_expr, NULL));
6708 if (type_expr)
6710 expr.value = build2 (C_MAYBE_CONST_EXPR,
6711 TREE_TYPE (expr.value), type_expr,
6712 expr.value);
6713 C_MAYBE_CONST_EXPR_NON_CONST (expr.value) = true;
6716 break;
6717 case RID_OFFSETOF:
6718 c_parser_consume_token (parser);
6719 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6721 expr.value = error_mark_node;
6722 break;
6724 t1 = c_parser_type_name (parser);
6725 if (t1 == NULL)
6726 parser->error = true;
6727 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
6728 gcc_assert (parser->error);
6729 if (parser->error)
6731 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6732 expr.value = error_mark_node;
6733 break;
6737 tree type = groktypename (t1, NULL, NULL);
6738 tree offsetof_ref;
6739 if (type == error_mark_node)
6740 offsetof_ref = error_mark_node;
6741 else
6743 offsetof_ref = build1 (INDIRECT_REF, type, null_pointer_node);
6744 SET_EXPR_LOCATION (offsetof_ref, loc);
6746 /* Parse the second argument to __builtin_offsetof. We
6747 must have one identifier, and beyond that we want to
6748 accept sub structure and sub array references. */
6749 if (c_parser_next_token_is (parser, CPP_NAME))
6751 offsetof_ref = build_component_ref
6752 (loc, offsetof_ref, c_parser_peek_token (parser)->value);
6753 c_parser_consume_token (parser);
6754 while (c_parser_next_token_is (parser, CPP_DOT)
6755 || c_parser_next_token_is (parser,
6756 CPP_OPEN_SQUARE)
6757 || c_parser_next_token_is (parser,
6758 CPP_DEREF))
6760 if (c_parser_next_token_is (parser, CPP_DEREF))
6762 loc = c_parser_peek_token (parser)->location;
6763 offsetof_ref = build_array_ref (loc,
6764 offsetof_ref,
6765 integer_zero_node);
6766 goto do_dot;
6768 else if (c_parser_next_token_is (parser, CPP_DOT))
6770 do_dot:
6771 c_parser_consume_token (parser);
6772 if (c_parser_next_token_is_not (parser,
6773 CPP_NAME))
6775 c_parser_error (parser, "expected identifier");
6776 break;
6778 offsetof_ref = build_component_ref
6779 (loc, offsetof_ref,
6780 c_parser_peek_token (parser)->value);
6781 c_parser_consume_token (parser);
6783 else
6785 tree idx;
6786 loc = c_parser_peek_token (parser)->location;
6787 c_parser_consume_token (parser);
6788 idx = c_parser_expression (parser).value;
6789 idx = c_fully_fold (idx, false, NULL);
6790 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
6791 "expected %<]%>");
6792 offsetof_ref = build_array_ref (loc, offsetof_ref, idx);
6796 else
6797 c_parser_error (parser, "expected identifier");
6798 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
6799 "expected %<)%>");
6800 expr.value = fold_offsetof (offsetof_ref);
6802 break;
6803 case RID_CHOOSE_EXPR:
6805 vec<c_expr_t, va_gc> *cexpr_list;
6806 c_expr_t *e1_p, *e2_p, *e3_p;
6807 tree c;
6809 c_parser_consume_token (parser);
6810 if (!c_parser_get_builtin_args (parser,
6811 "__builtin_choose_expr",
6812 &cexpr_list, true))
6814 expr.value = error_mark_node;
6815 break;
6818 if (vec_safe_length (cexpr_list) != 3)
6820 error_at (loc, "wrong number of arguments to "
6821 "%<__builtin_choose_expr%>");
6822 expr.value = error_mark_node;
6823 break;
6826 e1_p = &(*cexpr_list)[0];
6827 e2_p = &(*cexpr_list)[1];
6828 e3_p = &(*cexpr_list)[2];
6830 c = e1_p->value;
6831 mark_exp_read (e2_p->value);
6832 mark_exp_read (e3_p->value);
6833 if (TREE_CODE (c) != INTEGER_CST
6834 || !INTEGRAL_TYPE_P (TREE_TYPE (c)))
6835 error_at (loc,
6836 "first argument to %<__builtin_choose_expr%> not"
6837 " a constant");
6838 constant_expression_warning (c);
6839 expr = integer_zerop (c) ? *e3_p : *e2_p;
6840 break;
6842 case RID_TYPES_COMPATIBLE_P:
6843 c_parser_consume_token (parser);
6844 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6846 expr.value = error_mark_node;
6847 break;
6849 t1 = c_parser_type_name (parser);
6850 if (t1 == NULL)
6852 expr.value = error_mark_node;
6853 break;
6855 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
6857 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6858 expr.value = error_mark_node;
6859 break;
6861 t2 = c_parser_type_name (parser);
6862 if (t2 == NULL)
6864 expr.value = error_mark_node;
6865 break;
6867 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
6868 "expected %<)%>");
6870 tree e1, e2;
6871 e1 = groktypename (t1, NULL, NULL);
6872 e2 = groktypename (t2, NULL, NULL);
6873 if (e1 == error_mark_node || e2 == error_mark_node)
6875 expr.value = error_mark_node;
6876 break;
6879 e1 = TYPE_MAIN_VARIANT (e1);
6880 e2 = TYPE_MAIN_VARIANT (e2);
6882 expr.value
6883 = comptypes (e1, e2) ? integer_one_node : integer_zero_node;
6885 break;
6886 case RID_BUILTIN_COMPLEX:
6888 vec<c_expr_t, va_gc> *cexpr_list;
6889 c_expr_t *e1_p, *e2_p;
6891 c_parser_consume_token (parser);
6892 if (!c_parser_get_builtin_args (parser,
6893 "__builtin_complex",
6894 &cexpr_list, false))
6896 expr.value = error_mark_node;
6897 break;
6900 if (vec_safe_length (cexpr_list) != 2)
6902 error_at (loc, "wrong number of arguments to "
6903 "%<__builtin_complex%>");
6904 expr.value = error_mark_node;
6905 break;
6908 e1_p = &(*cexpr_list)[0];
6909 e2_p = &(*cexpr_list)[1];
6911 mark_exp_read (e1_p->value);
6912 if (TREE_CODE (e1_p->value) == EXCESS_PRECISION_EXPR)
6913 e1_p->value = convert (TREE_TYPE (e1_p->value),
6914 TREE_OPERAND (e1_p->value, 0));
6915 mark_exp_read (e2_p->value);
6916 if (TREE_CODE (e2_p->value) == EXCESS_PRECISION_EXPR)
6917 e2_p->value = convert (TREE_TYPE (e2_p->value),
6918 TREE_OPERAND (e2_p->value, 0));
6919 if (!SCALAR_FLOAT_TYPE_P (TREE_TYPE (e1_p->value))
6920 || DECIMAL_FLOAT_TYPE_P (TREE_TYPE (e1_p->value))
6921 || !SCALAR_FLOAT_TYPE_P (TREE_TYPE (e2_p->value))
6922 || DECIMAL_FLOAT_TYPE_P (TREE_TYPE (e2_p->value)))
6924 error_at (loc, "%<__builtin_complex%> operand "
6925 "not of real binary floating-point type");
6926 expr.value = error_mark_node;
6927 break;
6929 if (TYPE_MAIN_VARIANT (TREE_TYPE (e1_p->value))
6930 != TYPE_MAIN_VARIANT (TREE_TYPE (e2_p->value)))
6932 error_at (loc,
6933 "%<__builtin_complex%> operands of different types");
6934 expr.value = error_mark_node;
6935 break;
6937 if (!flag_isoc99)
6938 pedwarn (loc, OPT_Wpedantic,
6939 "ISO C90 does not support complex types");
6940 expr.value = build2 (COMPLEX_EXPR,
6941 build_complex_type
6942 (TYPE_MAIN_VARIANT
6943 (TREE_TYPE (e1_p->value))),
6944 e1_p->value, e2_p->value);
6945 break;
6947 case RID_BUILTIN_SHUFFLE:
6949 vec<c_expr_t, va_gc> *cexpr_list;
6950 unsigned int i;
6951 c_expr_t *p;
6953 c_parser_consume_token (parser);
6954 if (!c_parser_get_builtin_args (parser,
6955 "__builtin_shuffle",
6956 &cexpr_list, false))
6958 expr.value = error_mark_node;
6959 break;
6962 FOR_EACH_VEC_SAFE_ELT (cexpr_list, i, p)
6963 mark_exp_read (p->value);
6965 if (vec_safe_length (cexpr_list) == 2)
6966 expr.value =
6967 c_build_vec_perm_expr
6968 (loc, (*cexpr_list)[0].value,
6969 NULL_TREE, (*cexpr_list)[1].value);
6971 else if (vec_safe_length (cexpr_list) == 3)
6972 expr.value =
6973 c_build_vec_perm_expr
6974 (loc, (*cexpr_list)[0].value,
6975 (*cexpr_list)[1].value,
6976 (*cexpr_list)[2].value);
6977 else
6979 error_at (loc, "wrong number of arguments to "
6980 "%<__builtin_shuffle%>");
6981 expr.value = error_mark_node;
6983 break;
6985 case RID_AT_SELECTOR:
6986 gcc_assert (c_dialect_objc ());
6987 c_parser_consume_token (parser);
6988 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6990 expr.value = error_mark_node;
6991 break;
6994 tree sel = c_parser_objc_selector_arg (parser);
6995 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
6996 "expected %<)%>");
6997 expr.value = objc_build_selector_expr (loc, sel);
6999 break;
7000 case RID_AT_PROTOCOL:
7001 gcc_assert (c_dialect_objc ());
7002 c_parser_consume_token (parser);
7003 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7005 expr.value = error_mark_node;
7006 break;
7008 if (c_parser_next_token_is_not (parser, CPP_NAME))
7010 c_parser_error (parser, "expected identifier");
7011 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7012 expr.value = error_mark_node;
7013 break;
7016 tree id = c_parser_peek_token (parser)->value;
7017 c_parser_consume_token (parser);
7018 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7019 "expected %<)%>");
7020 expr.value = objc_build_protocol_expr (id);
7022 break;
7023 case RID_AT_ENCODE:
7024 /* Extension to support C-structures in the archiver. */
7025 gcc_assert (c_dialect_objc ());
7026 c_parser_consume_token (parser);
7027 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7029 expr.value = error_mark_node;
7030 break;
7032 t1 = c_parser_type_name (parser);
7033 if (t1 == NULL)
7035 expr.value = error_mark_node;
7036 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7037 break;
7039 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7040 "expected %<)%>");
7042 tree type = groktypename (t1, NULL, NULL);
7043 expr.value = objc_build_encode_expr (type);
7045 break;
7046 case RID_GENERIC:
7047 expr = c_parser_generic_selection (parser);
7048 break;
7049 default:
7050 c_parser_error (parser, "expected expression");
7051 expr.value = error_mark_node;
7052 break;
7054 break;
7055 case CPP_OPEN_SQUARE:
7056 if (c_dialect_objc ())
7058 tree receiver, args;
7059 c_parser_consume_token (parser);
7060 receiver = c_parser_objc_receiver (parser);
7061 args = c_parser_objc_message_args (parser);
7062 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
7063 "expected %<]%>");
7064 expr.value = objc_build_message_expr (receiver, args);
7065 break;
7067 /* Else fall through to report error. */
7068 default:
7069 c_parser_error (parser, "expected expression");
7070 expr.value = error_mark_node;
7071 break;
7073 return c_parser_postfix_expression_after_primary (parser, loc, expr);
7076 /* Parse a postfix expression after a parenthesized type name: the
7077 brace-enclosed initializer of a compound literal, possibly followed
7078 by some postfix operators. This is separate because it is not
7079 possible to tell until after the type name whether a cast
7080 expression has a cast or a compound literal, or whether the operand
7081 of sizeof is a parenthesized type name or starts with a compound
7082 literal. TYPE_LOC is the location where TYPE_NAME starts--the
7083 location of the first token after the parentheses around the type
7084 name. */
7086 static struct c_expr
7087 c_parser_postfix_expression_after_paren_type (c_parser *parser,
7088 struct c_type_name *type_name,
7089 location_t type_loc)
7091 tree type;
7092 struct c_expr init;
7093 bool non_const;
7094 struct c_expr expr;
7095 location_t start_loc;
7096 tree type_expr = NULL_TREE;
7097 bool type_expr_const = true;
7098 check_compound_literal_type (type_loc, type_name);
7099 start_init (NULL_TREE, NULL, 0);
7100 type = groktypename (type_name, &type_expr, &type_expr_const);
7101 start_loc = c_parser_peek_token (parser)->location;
7102 if (type != error_mark_node && C_TYPE_VARIABLE_SIZE (type))
7104 error_at (type_loc, "compound literal has variable size");
7105 type = error_mark_node;
7107 init = c_parser_braced_init (parser, type, false);
7108 finish_init ();
7109 maybe_warn_string_init (type, init);
7111 if (type != error_mark_node
7112 && !ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (type))
7113 && current_function_decl)
7115 error ("compound literal qualified by address-space qualifier");
7116 type = error_mark_node;
7119 if (!flag_isoc99)
7120 pedwarn (start_loc, OPT_Wpedantic, "ISO C90 forbids compound literals");
7121 non_const = ((init.value && TREE_CODE (init.value) == CONSTRUCTOR)
7122 ? CONSTRUCTOR_NON_CONST (init.value)
7123 : init.original_code == C_MAYBE_CONST_EXPR);
7124 non_const |= !type_expr_const;
7125 expr.value = build_compound_literal (start_loc, type, init.value, non_const);
7126 expr.original_code = ERROR_MARK;
7127 expr.original_type = NULL;
7128 if (type_expr)
7130 if (TREE_CODE (expr.value) == C_MAYBE_CONST_EXPR)
7132 gcc_assert (C_MAYBE_CONST_EXPR_PRE (expr.value) == NULL_TREE);
7133 C_MAYBE_CONST_EXPR_PRE (expr.value) = type_expr;
7135 else
7137 gcc_assert (!non_const);
7138 expr.value = build2 (C_MAYBE_CONST_EXPR, type,
7139 type_expr, expr.value);
7142 return c_parser_postfix_expression_after_primary (parser, start_loc, expr);
7145 /* Callback function for sizeof_pointer_memaccess_warning to compare
7146 types. */
7148 static bool
7149 sizeof_ptr_memacc_comptypes (tree type1, tree type2)
7151 return comptypes (type1, type2) == 1;
7154 /* Parse a postfix expression after the initial primary or compound
7155 literal; that is, parse a series of postfix operators.
7157 EXPR_LOC is the location of the primary expression. */
7159 static struct c_expr
7160 c_parser_postfix_expression_after_primary (c_parser *parser,
7161 location_t expr_loc,
7162 struct c_expr expr)
7164 struct c_expr orig_expr;
7165 tree ident, idx;
7166 location_t sizeof_arg_loc[3];
7167 tree sizeof_arg[3];
7168 unsigned int i;
7169 vec<tree, va_gc> *exprlist;
7170 vec<tree, va_gc> *origtypes = NULL;
7171 while (true)
7173 location_t op_loc = c_parser_peek_token (parser)->location;
7174 switch (c_parser_peek_token (parser)->type)
7176 case CPP_OPEN_SQUARE:
7177 /* Array reference. */
7178 c_parser_consume_token (parser);
7179 if (flag_enable_cilkplus
7180 && c_parser_peek_token (parser)->type == CPP_COLON)
7181 /* If we are here, then we have something like this:
7182 Array [ : ]
7184 expr.value = c_parser_array_notation (expr_loc, parser, NULL_TREE,
7185 expr.value);
7186 else
7188 idx = c_parser_expression (parser).value;
7189 /* Here we have 3 options:
7190 1. Array [EXPR] -- Normal Array call.
7191 2. Array [EXPR : EXPR] -- Array notation without stride.
7192 3. Array [EXPR : EXPR : EXPR] -- Array notation with stride.
7194 For 1, we just handle it just like a normal array expression.
7195 For 2 and 3 we handle it like we handle array notations. The
7196 idx value we have above becomes the initial/start index.
7198 if (flag_enable_cilkplus
7199 && c_parser_peek_token (parser)->type == CPP_COLON)
7200 expr.value = c_parser_array_notation (expr_loc, parser, idx,
7201 expr.value);
7202 else
7204 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
7205 "expected %<]%>");
7206 expr.value = build_array_ref (op_loc, expr.value, idx);
7209 expr.original_code = ERROR_MARK;
7210 expr.original_type = NULL;
7211 break;
7212 case CPP_OPEN_PAREN:
7213 /* Function call. */
7214 c_parser_consume_token (parser);
7215 for (i = 0; i < 3; i++)
7217 sizeof_arg[i] = NULL_TREE;
7218 sizeof_arg_loc[i] = UNKNOWN_LOCATION;
7220 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
7221 exprlist = NULL;
7222 else
7223 exprlist = c_parser_expr_list (parser, true, false, &origtypes,
7224 sizeof_arg_loc, sizeof_arg);
7225 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7226 "expected %<)%>");
7227 orig_expr = expr;
7228 mark_exp_read (expr.value);
7229 if (warn_sizeof_pointer_memaccess)
7230 sizeof_pointer_memaccess_warning (sizeof_arg_loc,
7231 expr.value, exprlist,
7232 sizeof_arg,
7233 sizeof_ptr_memacc_comptypes);
7234 /* FIXME diagnostics: Ideally we want the FUNCNAME, not the
7235 "(" after the FUNCNAME, which is what we have now. */
7236 expr.value = build_function_call_vec (op_loc, expr.value, exprlist,
7237 origtypes);
7238 expr.original_code = ERROR_MARK;
7239 if (TREE_CODE (expr.value) == INTEGER_CST
7240 && TREE_CODE (orig_expr.value) == FUNCTION_DECL
7241 && DECL_BUILT_IN_CLASS (orig_expr.value) == BUILT_IN_NORMAL
7242 && DECL_FUNCTION_CODE (orig_expr.value) == BUILT_IN_CONSTANT_P)
7243 expr.original_code = C_MAYBE_CONST_EXPR;
7244 expr.original_type = NULL;
7245 if (exprlist)
7247 release_tree_vector (exprlist);
7248 release_tree_vector (origtypes);
7250 break;
7251 case CPP_DOT:
7252 /* Structure element reference. */
7253 c_parser_consume_token (parser);
7254 expr = default_function_array_conversion (expr_loc, expr);
7255 if (c_parser_next_token_is (parser, CPP_NAME))
7256 ident = c_parser_peek_token (parser)->value;
7257 else
7259 c_parser_error (parser, "expected identifier");
7260 expr.value = error_mark_node;
7261 expr.original_code = ERROR_MARK;
7262 expr.original_type = NULL;
7263 return expr;
7265 c_parser_consume_token (parser);
7266 expr.value = build_component_ref (op_loc, expr.value, ident);
7267 expr.original_code = ERROR_MARK;
7268 if (TREE_CODE (expr.value) != COMPONENT_REF)
7269 expr.original_type = NULL;
7270 else
7272 /* Remember the original type of a bitfield. */
7273 tree field = TREE_OPERAND (expr.value, 1);
7274 if (TREE_CODE (field) != FIELD_DECL)
7275 expr.original_type = NULL;
7276 else
7277 expr.original_type = DECL_BIT_FIELD_TYPE (field);
7279 break;
7280 case CPP_DEREF:
7281 /* Structure element reference. */
7282 c_parser_consume_token (parser);
7283 expr = default_function_array_conversion (expr_loc, expr);
7284 if (c_parser_next_token_is (parser, CPP_NAME))
7285 ident = c_parser_peek_token (parser)->value;
7286 else
7288 c_parser_error (parser, "expected identifier");
7289 expr.value = error_mark_node;
7290 expr.original_code = ERROR_MARK;
7291 expr.original_type = NULL;
7292 return expr;
7294 c_parser_consume_token (parser);
7295 expr.value = build_component_ref (op_loc,
7296 build_indirect_ref (op_loc,
7297 expr.value,
7298 RO_ARROW),
7299 ident);
7300 expr.original_code = ERROR_MARK;
7301 if (TREE_CODE (expr.value) != COMPONENT_REF)
7302 expr.original_type = NULL;
7303 else
7305 /* Remember the original type of a bitfield. */
7306 tree field = TREE_OPERAND (expr.value, 1);
7307 if (TREE_CODE (field) != FIELD_DECL)
7308 expr.original_type = NULL;
7309 else
7310 expr.original_type = DECL_BIT_FIELD_TYPE (field);
7312 break;
7313 case CPP_PLUS_PLUS:
7314 /* Postincrement. */
7315 c_parser_consume_token (parser);
7316 /* If the expressions have array notations, we expand them. */
7317 if (flag_enable_cilkplus
7318 && TREE_CODE (expr.value) == ARRAY_NOTATION_REF)
7319 expr = fix_array_notation_expr (expr_loc, POSTINCREMENT_EXPR, expr);
7320 else
7322 expr = default_function_array_read_conversion (expr_loc, expr);
7323 expr.value = build_unary_op (op_loc,
7324 POSTINCREMENT_EXPR, expr.value, 0);
7326 expr.original_code = ERROR_MARK;
7327 expr.original_type = NULL;
7328 break;
7329 case CPP_MINUS_MINUS:
7330 /* Postdecrement. */
7331 c_parser_consume_token (parser);
7332 /* If the expressions have array notations, we expand them. */
7333 if (flag_enable_cilkplus
7334 && TREE_CODE (expr.value) == ARRAY_NOTATION_REF)
7335 expr = fix_array_notation_expr (expr_loc, POSTDECREMENT_EXPR, expr);
7336 else
7338 expr = default_function_array_read_conversion (expr_loc, expr);
7339 expr.value = build_unary_op (op_loc,
7340 POSTDECREMENT_EXPR, expr.value, 0);
7342 expr.original_code = ERROR_MARK;
7343 expr.original_type = NULL;
7344 break;
7345 default:
7346 return expr;
7351 /* Parse an expression (C90 6.3.17, C99 6.5.17).
7353 expression:
7354 assignment-expression
7355 expression , assignment-expression
7358 static struct c_expr
7359 c_parser_expression (c_parser *parser)
7361 struct c_expr expr;
7362 expr = c_parser_expr_no_commas (parser, NULL);
7363 while (c_parser_next_token_is (parser, CPP_COMMA))
7365 struct c_expr next;
7366 tree lhsval;
7367 location_t loc = c_parser_peek_token (parser)->location;
7368 location_t expr_loc;
7369 c_parser_consume_token (parser);
7370 expr_loc = c_parser_peek_token (parser)->location;
7371 lhsval = expr.value;
7372 while (TREE_CODE (lhsval) == COMPOUND_EXPR)
7373 lhsval = TREE_OPERAND (lhsval, 1);
7374 if (DECL_P (lhsval) || handled_component_p (lhsval))
7375 mark_exp_read (lhsval);
7376 next = c_parser_expr_no_commas (parser, NULL);
7377 next = default_function_array_conversion (expr_loc, next);
7378 expr.value = build_compound_expr (loc, expr.value, next.value);
7379 expr.original_code = COMPOUND_EXPR;
7380 expr.original_type = next.original_type;
7382 return expr;
7385 /* Parse an expression and convert functions or arrays to
7386 pointers. */
7388 static struct c_expr
7389 c_parser_expression_conv (c_parser *parser)
7391 struct c_expr expr;
7392 location_t loc = c_parser_peek_token (parser)->location;
7393 expr = c_parser_expression (parser);
7394 expr = default_function_array_conversion (loc, expr);
7395 return expr;
7398 /* Parse a non-empty list of expressions. If CONVERT_P, convert
7399 functions and arrays to pointers. If FOLD_P, fold the expressions.
7401 nonempty-expr-list:
7402 assignment-expression
7403 nonempty-expr-list , assignment-expression
7406 static vec<tree, va_gc> *
7407 c_parser_expr_list (c_parser *parser, bool convert_p, bool fold_p,
7408 vec<tree, va_gc> **p_orig_types,
7409 location_t *sizeof_arg_loc, tree *sizeof_arg)
7411 vec<tree, va_gc> *ret;
7412 vec<tree, va_gc> *orig_types;
7413 struct c_expr expr;
7414 location_t loc = c_parser_peek_token (parser)->location;
7415 location_t cur_sizeof_arg_loc = UNKNOWN_LOCATION;
7416 unsigned int idx = 0;
7418 ret = make_tree_vector ();
7419 if (p_orig_types == NULL)
7420 orig_types = NULL;
7421 else
7422 orig_types = make_tree_vector ();
7424 if (sizeof_arg != NULL
7425 && c_parser_next_token_is_keyword (parser, RID_SIZEOF))
7426 cur_sizeof_arg_loc = c_parser_peek_2nd_token (parser)->location;
7427 expr = c_parser_expr_no_commas (parser, NULL);
7428 if (convert_p)
7429 expr = default_function_array_read_conversion (loc, expr);
7430 if (fold_p)
7431 expr.value = c_fully_fold (expr.value, false, NULL);
7432 ret->quick_push (expr.value);
7433 if (orig_types)
7434 orig_types->quick_push (expr.original_type);
7435 if (sizeof_arg != NULL
7436 && cur_sizeof_arg_loc != UNKNOWN_LOCATION
7437 && expr.original_code == SIZEOF_EXPR)
7439 sizeof_arg[0] = c_last_sizeof_arg;
7440 sizeof_arg_loc[0] = cur_sizeof_arg_loc;
7442 while (c_parser_next_token_is (parser, CPP_COMMA))
7444 c_parser_consume_token (parser);
7445 loc = c_parser_peek_token (parser)->location;
7446 if (sizeof_arg != NULL
7447 && c_parser_next_token_is_keyword (parser, RID_SIZEOF))
7448 cur_sizeof_arg_loc = c_parser_peek_2nd_token (parser)->location;
7449 else
7450 cur_sizeof_arg_loc = UNKNOWN_LOCATION;
7451 expr = c_parser_expr_no_commas (parser, NULL);
7452 if (convert_p)
7453 expr = default_function_array_read_conversion (loc, expr);
7454 if (fold_p)
7455 expr.value = c_fully_fold (expr.value, false, NULL);
7456 vec_safe_push (ret, expr.value);
7457 if (orig_types)
7458 vec_safe_push (orig_types, expr.original_type);
7459 if (++idx < 3
7460 && sizeof_arg != NULL
7461 && cur_sizeof_arg_loc != UNKNOWN_LOCATION
7462 && expr.original_code == SIZEOF_EXPR)
7464 sizeof_arg[idx] = c_last_sizeof_arg;
7465 sizeof_arg_loc[idx] = cur_sizeof_arg_loc;
7468 if (orig_types)
7469 *p_orig_types = orig_types;
7470 return ret;
7473 /* Parse Objective-C-specific constructs. */
7475 /* Parse an objc-class-definition.
7477 objc-class-definition:
7478 @interface identifier objc-superclass[opt] objc-protocol-refs[opt]
7479 objc-class-instance-variables[opt] objc-methodprotolist @end
7480 @implementation identifier objc-superclass[opt]
7481 objc-class-instance-variables[opt]
7482 @interface identifier ( identifier ) objc-protocol-refs[opt]
7483 objc-methodprotolist @end
7484 @interface identifier ( ) objc-protocol-refs[opt]
7485 objc-methodprotolist @end
7486 @implementation identifier ( identifier )
7488 objc-superclass:
7489 : identifier
7491 "@interface identifier (" must start "@interface identifier (
7492 identifier ) ...": objc-methodprotolist in the first production may
7493 not start with a parenthesized identifier as a declarator of a data
7494 definition with no declaration specifiers if the objc-superclass,
7495 objc-protocol-refs and objc-class-instance-variables are omitted. */
7497 static void
7498 c_parser_objc_class_definition (c_parser *parser, tree attributes)
7500 bool iface_p;
7501 tree id1;
7502 tree superclass;
7503 if (c_parser_next_token_is_keyword (parser, RID_AT_INTERFACE))
7504 iface_p = true;
7505 else if (c_parser_next_token_is_keyword (parser, RID_AT_IMPLEMENTATION))
7506 iface_p = false;
7507 else
7508 gcc_unreachable ();
7510 c_parser_consume_token (parser);
7511 if (c_parser_next_token_is_not (parser, CPP_NAME))
7513 c_parser_error (parser, "expected identifier");
7514 return;
7516 id1 = c_parser_peek_token (parser)->value;
7517 c_parser_consume_token (parser);
7518 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
7520 /* We have a category or class extension. */
7521 tree id2;
7522 tree proto = NULL_TREE;
7523 c_parser_consume_token (parser);
7524 if (c_parser_next_token_is_not (parser, CPP_NAME))
7526 if (iface_p && c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
7528 /* We have a class extension. */
7529 id2 = NULL_TREE;
7531 else
7533 c_parser_error (parser, "expected identifier or %<)%>");
7534 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7535 return;
7538 else
7540 id2 = c_parser_peek_token (parser)->value;
7541 c_parser_consume_token (parser);
7543 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
7544 if (!iface_p)
7546 objc_start_category_implementation (id1, id2);
7547 return;
7549 if (c_parser_next_token_is (parser, CPP_LESS))
7550 proto = c_parser_objc_protocol_refs (parser);
7551 objc_start_category_interface (id1, id2, proto, attributes);
7552 c_parser_objc_methodprotolist (parser);
7553 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
7554 objc_finish_interface ();
7555 return;
7557 if (c_parser_next_token_is (parser, CPP_COLON))
7559 c_parser_consume_token (parser);
7560 if (c_parser_next_token_is_not (parser, CPP_NAME))
7562 c_parser_error (parser, "expected identifier");
7563 return;
7565 superclass = c_parser_peek_token (parser)->value;
7566 c_parser_consume_token (parser);
7568 else
7569 superclass = NULL_TREE;
7570 if (iface_p)
7572 tree proto = NULL_TREE;
7573 if (c_parser_next_token_is (parser, CPP_LESS))
7574 proto = c_parser_objc_protocol_refs (parser);
7575 objc_start_class_interface (id1, superclass, proto, attributes);
7577 else
7578 objc_start_class_implementation (id1, superclass);
7579 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
7580 c_parser_objc_class_instance_variables (parser);
7581 if (iface_p)
7583 objc_continue_interface ();
7584 c_parser_objc_methodprotolist (parser);
7585 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
7586 objc_finish_interface ();
7588 else
7590 objc_continue_implementation ();
7591 return;
7595 /* Parse objc-class-instance-variables.
7597 objc-class-instance-variables:
7598 { objc-instance-variable-decl-list[opt] }
7600 objc-instance-variable-decl-list:
7601 objc-visibility-spec
7602 objc-instance-variable-decl ;
7604 objc-instance-variable-decl-list objc-visibility-spec
7605 objc-instance-variable-decl-list objc-instance-variable-decl ;
7606 objc-instance-variable-decl-list ;
7608 objc-visibility-spec:
7609 @private
7610 @protected
7611 @public
7613 objc-instance-variable-decl:
7614 struct-declaration
7617 static void
7618 c_parser_objc_class_instance_variables (c_parser *parser)
7620 gcc_assert (c_parser_next_token_is (parser, CPP_OPEN_BRACE));
7621 c_parser_consume_token (parser);
7622 while (c_parser_next_token_is_not (parser, CPP_EOF))
7624 tree decls;
7625 /* Parse any stray semicolon. */
7626 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
7628 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
7629 "extra semicolon");
7630 c_parser_consume_token (parser);
7631 continue;
7633 /* Stop if at the end of the instance variables. */
7634 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
7636 c_parser_consume_token (parser);
7637 break;
7639 /* Parse any objc-visibility-spec. */
7640 if (c_parser_next_token_is_keyword (parser, RID_AT_PRIVATE))
7642 c_parser_consume_token (parser);
7643 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
7644 continue;
7646 else if (c_parser_next_token_is_keyword (parser, RID_AT_PROTECTED))
7648 c_parser_consume_token (parser);
7649 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
7650 continue;
7652 else if (c_parser_next_token_is_keyword (parser, RID_AT_PUBLIC))
7654 c_parser_consume_token (parser);
7655 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
7656 continue;
7658 else if (c_parser_next_token_is_keyword (parser, RID_AT_PACKAGE))
7660 c_parser_consume_token (parser);
7661 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
7662 continue;
7664 else if (c_parser_next_token_is (parser, CPP_PRAGMA))
7666 c_parser_pragma (parser, pragma_external);
7667 continue;
7670 /* Parse some comma-separated declarations. */
7671 decls = c_parser_struct_declaration (parser);
7672 if (decls == NULL)
7674 /* There is a syntax error. We want to skip the offending
7675 tokens up to the next ';' (included) or '}'
7676 (excluded). */
7678 /* First, skip manually a ')' or ']'. This is because they
7679 reduce the nesting level, so c_parser_skip_until_found()
7680 wouldn't be able to skip past them. */
7681 c_token *token = c_parser_peek_token (parser);
7682 if (token->type == CPP_CLOSE_PAREN || token->type == CPP_CLOSE_SQUARE)
7683 c_parser_consume_token (parser);
7685 /* Then, do the standard skipping. */
7686 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
7688 /* We hopefully recovered. Start normal parsing again. */
7689 parser->error = false;
7690 continue;
7692 else
7694 /* Comma-separated instance variables are chained together
7695 in reverse order; add them one by one. */
7696 tree ivar = nreverse (decls);
7697 for (; ivar; ivar = DECL_CHAIN (ivar))
7698 objc_add_instance_variable (copy_node (ivar));
7700 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
7704 /* Parse an objc-class-declaration.
7706 objc-class-declaration:
7707 @class identifier-list ;
7710 static void
7711 c_parser_objc_class_declaration (c_parser *parser)
7713 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_CLASS));
7714 c_parser_consume_token (parser);
7715 /* Any identifiers, including those declared as type names, are OK
7716 here. */
7717 while (true)
7719 tree id;
7720 if (c_parser_next_token_is_not (parser, CPP_NAME))
7722 c_parser_error (parser, "expected identifier");
7723 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
7724 parser->error = false;
7725 return;
7727 id = c_parser_peek_token (parser)->value;
7728 objc_declare_class (id);
7729 c_parser_consume_token (parser);
7730 if (c_parser_next_token_is (parser, CPP_COMMA))
7731 c_parser_consume_token (parser);
7732 else
7733 break;
7735 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
7738 /* Parse an objc-alias-declaration.
7740 objc-alias-declaration:
7741 @compatibility_alias identifier identifier ;
7744 static void
7745 c_parser_objc_alias_declaration (c_parser *parser)
7747 tree id1, id2;
7748 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_ALIAS));
7749 c_parser_consume_token (parser);
7750 if (c_parser_next_token_is_not (parser, CPP_NAME))
7752 c_parser_error (parser, "expected identifier");
7753 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
7754 return;
7756 id1 = c_parser_peek_token (parser)->value;
7757 c_parser_consume_token (parser);
7758 if (c_parser_next_token_is_not (parser, CPP_NAME))
7760 c_parser_error (parser, "expected identifier");
7761 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
7762 return;
7764 id2 = c_parser_peek_token (parser)->value;
7765 c_parser_consume_token (parser);
7766 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
7767 objc_declare_alias (id1, id2);
7770 /* Parse an objc-protocol-definition.
7772 objc-protocol-definition:
7773 @protocol identifier objc-protocol-refs[opt] objc-methodprotolist @end
7774 @protocol identifier-list ;
7776 "@protocol identifier ;" should be resolved as "@protocol
7777 identifier-list ;": objc-methodprotolist may not start with a
7778 semicolon in the first alternative if objc-protocol-refs are
7779 omitted. */
7781 static void
7782 c_parser_objc_protocol_definition (c_parser *parser, tree attributes)
7784 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_PROTOCOL));
7786 c_parser_consume_token (parser);
7787 if (c_parser_next_token_is_not (parser, CPP_NAME))
7789 c_parser_error (parser, "expected identifier");
7790 return;
7792 if (c_parser_peek_2nd_token (parser)->type == CPP_COMMA
7793 || c_parser_peek_2nd_token (parser)->type == CPP_SEMICOLON)
7795 /* Any identifiers, including those declared as type names, are
7796 OK here. */
7797 while (true)
7799 tree id;
7800 if (c_parser_next_token_is_not (parser, CPP_NAME))
7802 c_parser_error (parser, "expected identifier");
7803 break;
7805 id = c_parser_peek_token (parser)->value;
7806 objc_declare_protocol (id, attributes);
7807 c_parser_consume_token (parser);
7808 if (c_parser_next_token_is (parser, CPP_COMMA))
7809 c_parser_consume_token (parser);
7810 else
7811 break;
7813 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
7815 else
7817 tree id = c_parser_peek_token (parser)->value;
7818 tree proto = NULL_TREE;
7819 c_parser_consume_token (parser);
7820 if (c_parser_next_token_is (parser, CPP_LESS))
7821 proto = c_parser_objc_protocol_refs (parser);
7822 parser->objc_pq_context = true;
7823 objc_start_protocol (id, proto, attributes);
7824 c_parser_objc_methodprotolist (parser);
7825 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
7826 parser->objc_pq_context = false;
7827 objc_finish_interface ();
7831 /* Parse an objc-method-type.
7833 objc-method-type:
7837 Return true if it is a class method (+) and false if it is
7838 an instance method (-).
7840 static inline bool
7841 c_parser_objc_method_type (c_parser *parser)
7843 switch (c_parser_peek_token (parser)->type)
7845 case CPP_PLUS:
7846 c_parser_consume_token (parser);
7847 return true;
7848 case CPP_MINUS:
7849 c_parser_consume_token (parser);
7850 return false;
7851 default:
7852 gcc_unreachable ();
7856 /* Parse an objc-method-definition.
7858 objc-method-definition:
7859 objc-method-type objc-method-decl ;[opt] compound-statement
7862 static void
7863 c_parser_objc_method_definition (c_parser *parser)
7865 bool is_class_method = c_parser_objc_method_type (parser);
7866 tree decl, attributes = NULL_TREE, expr = NULL_TREE;
7867 parser->objc_pq_context = true;
7868 decl = c_parser_objc_method_decl (parser, is_class_method, &attributes,
7869 &expr);
7870 if (decl == error_mark_node)
7871 return; /* Bail here. */
7873 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
7875 c_parser_consume_token (parser);
7876 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
7877 "extra semicolon in method definition specified");
7880 if (!c_parser_next_token_is (parser, CPP_OPEN_BRACE))
7882 c_parser_error (parser, "expected %<{%>");
7883 return;
7886 parser->objc_pq_context = false;
7887 if (objc_start_method_definition (is_class_method, decl, attributes, expr))
7889 add_stmt (c_parser_compound_statement (parser));
7890 objc_finish_method_definition (current_function_decl);
7892 else
7894 /* This code is executed when we find a method definition
7895 outside of an @implementation context (or invalid for other
7896 reasons). Parse the method (to keep going) but do not emit
7897 any code.
7899 c_parser_compound_statement (parser);
7903 /* Parse an objc-methodprotolist.
7905 objc-methodprotolist:
7906 empty
7907 objc-methodprotolist objc-methodproto
7908 objc-methodprotolist declaration
7909 objc-methodprotolist ;
7910 @optional
7911 @required
7913 The declaration is a data definition, which may be missing
7914 declaration specifiers under the same rules and diagnostics as
7915 other data definitions outside functions, and the stray semicolon
7916 is diagnosed the same way as a stray semicolon outside a
7917 function. */
7919 static void
7920 c_parser_objc_methodprotolist (c_parser *parser)
7922 while (true)
7924 /* The list is terminated by @end. */
7925 switch (c_parser_peek_token (parser)->type)
7927 case CPP_SEMICOLON:
7928 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
7929 "ISO C does not allow extra %<;%> outside of a function");
7930 c_parser_consume_token (parser);
7931 break;
7932 case CPP_PLUS:
7933 case CPP_MINUS:
7934 c_parser_objc_methodproto (parser);
7935 break;
7936 case CPP_PRAGMA:
7937 c_parser_pragma (parser, pragma_external);
7938 break;
7939 case CPP_EOF:
7940 return;
7941 default:
7942 if (c_parser_next_token_is_keyword (parser, RID_AT_END))
7943 return;
7944 else if (c_parser_next_token_is_keyword (parser, RID_AT_PROPERTY))
7945 c_parser_objc_at_property_declaration (parser);
7946 else if (c_parser_next_token_is_keyword (parser, RID_AT_OPTIONAL))
7948 objc_set_method_opt (true);
7949 c_parser_consume_token (parser);
7951 else if (c_parser_next_token_is_keyword (parser, RID_AT_REQUIRED))
7953 objc_set_method_opt (false);
7954 c_parser_consume_token (parser);
7956 else
7957 c_parser_declaration_or_fndef (parser, false, false, true,
7958 false, true, NULL);
7959 break;
7964 /* Parse an objc-methodproto.
7966 objc-methodproto:
7967 objc-method-type objc-method-decl ;
7970 static void
7971 c_parser_objc_methodproto (c_parser *parser)
7973 bool is_class_method = c_parser_objc_method_type (parser);
7974 tree decl, attributes = NULL_TREE;
7976 /* Remember protocol qualifiers in prototypes. */
7977 parser->objc_pq_context = true;
7978 decl = c_parser_objc_method_decl (parser, is_class_method, &attributes,
7979 NULL);
7980 /* Forget protocol qualifiers now. */
7981 parser->objc_pq_context = false;
7983 /* Do not allow the presence of attributes to hide an erroneous
7984 method implementation in the interface section. */
7985 if (!c_parser_next_token_is (parser, CPP_SEMICOLON))
7987 c_parser_error (parser, "expected %<;%>");
7988 return;
7991 if (decl != error_mark_node)
7992 objc_add_method_declaration (is_class_method, decl, attributes);
7994 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
7997 /* If we are at a position that method attributes may be present, check that
7998 there are not any parsed already (a syntax error) and then collect any
7999 specified at the current location. Finally, if new attributes were present,
8000 check that the next token is legal ( ';' for decls and '{' for defs). */
8002 static bool
8003 c_parser_objc_maybe_method_attributes (c_parser* parser, tree* attributes)
8005 bool bad = false;
8006 if (*attributes)
8008 c_parser_error (parser,
8009 "method attributes must be specified at the end only");
8010 *attributes = NULL_TREE;
8011 bad = true;
8014 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
8015 *attributes = c_parser_attributes (parser);
8017 /* If there were no attributes here, just report any earlier error. */
8018 if (*attributes == NULL_TREE || bad)
8019 return bad;
8021 /* If the attributes are followed by a ; or {, then just report any earlier
8022 error. */
8023 if (c_parser_next_token_is (parser, CPP_SEMICOLON)
8024 || c_parser_next_token_is (parser, CPP_OPEN_BRACE))
8025 return bad;
8027 /* We've got attributes, but not at the end. */
8028 c_parser_error (parser,
8029 "expected %<;%> or %<{%> after method attribute definition");
8030 return true;
8033 /* Parse an objc-method-decl.
8035 objc-method-decl:
8036 ( objc-type-name ) objc-selector
8037 objc-selector
8038 ( objc-type-name ) objc-keyword-selector objc-optparmlist
8039 objc-keyword-selector objc-optparmlist
8040 attributes
8042 objc-keyword-selector:
8043 objc-keyword-decl
8044 objc-keyword-selector objc-keyword-decl
8046 objc-keyword-decl:
8047 objc-selector : ( objc-type-name ) identifier
8048 objc-selector : identifier
8049 : ( objc-type-name ) identifier
8050 : identifier
8052 objc-optparmlist:
8053 objc-optparms objc-optellipsis
8055 objc-optparms:
8056 empty
8057 objc-opt-parms , parameter-declaration
8059 objc-optellipsis:
8060 empty
8061 , ...
8064 static tree
8065 c_parser_objc_method_decl (c_parser *parser, bool is_class_method,
8066 tree *attributes, tree *expr)
8068 tree type = NULL_TREE;
8069 tree sel;
8070 tree parms = NULL_TREE;
8071 bool ellipsis = false;
8072 bool attr_err = false;
8074 *attributes = NULL_TREE;
8075 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
8077 c_parser_consume_token (parser);
8078 type = c_parser_objc_type_name (parser);
8079 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
8081 sel = c_parser_objc_selector (parser);
8082 /* If there is no selector, or a colon follows, we have an
8083 objc-keyword-selector. If there is a selector, and a colon does
8084 not follow, that selector ends the objc-method-decl. */
8085 if (!sel || c_parser_next_token_is (parser, CPP_COLON))
8087 tree tsel = sel;
8088 tree list = NULL_TREE;
8089 while (true)
8091 tree atype = NULL_TREE, id, keyworddecl;
8092 tree param_attr = NULL_TREE;
8093 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
8094 break;
8095 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
8097 c_parser_consume_token (parser);
8098 atype = c_parser_objc_type_name (parser);
8099 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
8100 "expected %<)%>");
8102 /* New ObjC allows attributes on method parameters. */
8103 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
8104 param_attr = c_parser_attributes (parser);
8105 if (c_parser_next_token_is_not (parser, CPP_NAME))
8107 c_parser_error (parser, "expected identifier");
8108 return error_mark_node;
8110 id = c_parser_peek_token (parser)->value;
8111 c_parser_consume_token (parser);
8112 keyworddecl = objc_build_keyword_decl (tsel, atype, id, param_attr);
8113 list = chainon (list, keyworddecl);
8114 tsel = c_parser_objc_selector (parser);
8115 if (!tsel && c_parser_next_token_is_not (parser, CPP_COLON))
8116 break;
8119 attr_err |= c_parser_objc_maybe_method_attributes (parser, attributes) ;
8121 /* Parse the optional parameter list. Optional Objective-C
8122 method parameters follow the C syntax, and may include '...'
8123 to denote a variable number of arguments. */
8124 parms = make_node (TREE_LIST);
8125 while (c_parser_next_token_is (parser, CPP_COMMA))
8127 struct c_parm *parm;
8128 c_parser_consume_token (parser);
8129 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
8131 ellipsis = true;
8132 c_parser_consume_token (parser);
8133 attr_err |= c_parser_objc_maybe_method_attributes
8134 (parser, attributes) ;
8135 break;
8137 parm = c_parser_parameter_declaration (parser, NULL_TREE);
8138 if (parm == NULL)
8139 break;
8140 parms = chainon (parms,
8141 build_tree_list (NULL_TREE, grokparm (parm, expr)));
8143 sel = list;
8145 else
8146 attr_err |= c_parser_objc_maybe_method_attributes (parser, attributes) ;
8148 if (sel == NULL)
8150 c_parser_error (parser, "objective-c method declaration is expected");
8151 return error_mark_node;
8154 if (attr_err)
8155 return error_mark_node;
8157 return objc_build_method_signature (is_class_method, type, sel, parms, ellipsis);
8160 /* Parse an objc-type-name.
8162 objc-type-name:
8163 objc-type-qualifiers[opt] type-name
8164 objc-type-qualifiers[opt]
8166 objc-type-qualifiers:
8167 objc-type-qualifier
8168 objc-type-qualifiers objc-type-qualifier
8170 objc-type-qualifier: one of
8171 in out inout bycopy byref oneway
8174 static tree
8175 c_parser_objc_type_name (c_parser *parser)
8177 tree quals = NULL_TREE;
8178 struct c_type_name *type_name = NULL;
8179 tree type = NULL_TREE;
8180 while (true)
8182 c_token *token = c_parser_peek_token (parser);
8183 if (token->type == CPP_KEYWORD
8184 && (token->keyword == RID_IN
8185 || token->keyword == RID_OUT
8186 || token->keyword == RID_INOUT
8187 || token->keyword == RID_BYCOPY
8188 || token->keyword == RID_BYREF
8189 || token->keyword == RID_ONEWAY))
8191 quals = chainon (build_tree_list (NULL_TREE, token->value), quals);
8192 c_parser_consume_token (parser);
8194 else
8195 break;
8197 if (c_parser_next_tokens_start_typename (parser, cla_prefer_type))
8198 type_name = c_parser_type_name (parser);
8199 if (type_name)
8200 type = groktypename (type_name, NULL, NULL);
8202 /* If the type is unknown, and error has already been produced and
8203 we need to recover from the error. In that case, use NULL_TREE
8204 for the type, as if no type had been specified; this will use the
8205 default type ('id') which is good for error recovery. */
8206 if (type == error_mark_node)
8207 type = NULL_TREE;
8209 return build_tree_list (quals, type);
8212 /* Parse objc-protocol-refs.
8214 objc-protocol-refs:
8215 < identifier-list >
8218 static tree
8219 c_parser_objc_protocol_refs (c_parser *parser)
8221 tree list = NULL_TREE;
8222 gcc_assert (c_parser_next_token_is (parser, CPP_LESS));
8223 c_parser_consume_token (parser);
8224 /* Any identifiers, including those declared as type names, are OK
8225 here. */
8226 while (true)
8228 tree id;
8229 if (c_parser_next_token_is_not (parser, CPP_NAME))
8231 c_parser_error (parser, "expected identifier");
8232 break;
8234 id = c_parser_peek_token (parser)->value;
8235 list = chainon (list, build_tree_list (NULL_TREE, id));
8236 c_parser_consume_token (parser);
8237 if (c_parser_next_token_is (parser, CPP_COMMA))
8238 c_parser_consume_token (parser);
8239 else
8240 break;
8242 c_parser_require (parser, CPP_GREATER, "expected %<>%>");
8243 return list;
8246 /* Parse an objc-try-catch-finally-statement.
8248 objc-try-catch-finally-statement:
8249 @try compound-statement objc-catch-list[opt]
8250 @try compound-statement objc-catch-list[opt] @finally compound-statement
8252 objc-catch-list:
8253 @catch ( objc-catch-parameter-declaration ) compound-statement
8254 objc-catch-list @catch ( objc-catch-parameter-declaration ) compound-statement
8256 objc-catch-parameter-declaration:
8257 parameter-declaration
8258 '...'
8260 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
8262 PS: This function is identical to cp_parser_objc_try_catch_finally_statement
8263 for C++. Keep them in sync. */
8265 static void
8266 c_parser_objc_try_catch_finally_statement (c_parser *parser)
8268 location_t location;
8269 tree stmt;
8271 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_TRY));
8272 c_parser_consume_token (parser);
8273 location = c_parser_peek_token (parser)->location;
8274 objc_maybe_warn_exceptions (location);
8275 stmt = c_parser_compound_statement (parser);
8276 objc_begin_try_stmt (location, stmt);
8278 while (c_parser_next_token_is_keyword (parser, RID_AT_CATCH))
8280 struct c_parm *parm;
8281 tree parameter_declaration = error_mark_node;
8282 bool seen_open_paren = false;
8284 c_parser_consume_token (parser);
8285 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
8286 seen_open_paren = true;
8287 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
8289 /* We have "@catch (...)" (where the '...' are literally
8290 what is in the code). Skip the '...'.
8291 parameter_declaration is set to NULL_TREE, and
8292 objc_being_catch_clauses() knows that that means
8293 '...'. */
8294 c_parser_consume_token (parser);
8295 parameter_declaration = NULL_TREE;
8297 else
8299 /* We have "@catch (NSException *exception)" or something
8300 like that. Parse the parameter declaration. */
8301 parm = c_parser_parameter_declaration (parser, NULL_TREE);
8302 if (parm == NULL)
8303 parameter_declaration = error_mark_node;
8304 else
8305 parameter_declaration = grokparm (parm, NULL);
8307 if (seen_open_paren)
8308 c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
8309 else
8311 /* If there was no open parenthesis, we are recovering from
8312 an error, and we are trying to figure out what mistake
8313 the user has made. */
8315 /* If there is an immediate closing parenthesis, the user
8316 probably forgot the opening one (ie, they typed "@catch
8317 NSException *e)". Parse the closing parenthesis and keep
8318 going. */
8319 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
8320 c_parser_consume_token (parser);
8322 /* If these is no immediate closing parenthesis, the user
8323 probably doesn't know that parenthesis are required at
8324 all (ie, they typed "@catch NSException *e"). So, just
8325 forget about the closing parenthesis and keep going. */
8327 objc_begin_catch_clause (parameter_declaration);
8328 if (c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
8329 c_parser_compound_statement_nostart (parser);
8330 objc_finish_catch_clause ();
8332 if (c_parser_next_token_is_keyword (parser, RID_AT_FINALLY))
8334 c_parser_consume_token (parser);
8335 location = c_parser_peek_token (parser)->location;
8336 stmt = c_parser_compound_statement (parser);
8337 objc_build_finally_clause (location, stmt);
8339 objc_finish_try_stmt ();
8342 /* Parse an objc-synchronized-statement.
8344 objc-synchronized-statement:
8345 @synchronized ( expression ) compound-statement
8348 static void
8349 c_parser_objc_synchronized_statement (c_parser *parser)
8351 location_t loc;
8352 tree expr, stmt;
8353 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_SYNCHRONIZED));
8354 c_parser_consume_token (parser);
8355 loc = c_parser_peek_token (parser)->location;
8356 objc_maybe_warn_exceptions (loc);
8357 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
8359 expr = c_parser_expression (parser).value;
8360 expr = c_fully_fold (expr, false, NULL);
8361 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
8363 else
8364 expr = error_mark_node;
8365 stmt = c_parser_compound_statement (parser);
8366 objc_build_synchronized (loc, expr, stmt);
8369 /* Parse an objc-selector; return NULL_TREE without an error if the
8370 next token is not an objc-selector.
8372 objc-selector:
8373 identifier
8374 one of
8375 enum struct union if else while do for switch case default
8376 break continue return goto asm sizeof typeof __alignof
8377 unsigned long const short volatile signed restrict _Complex
8378 in out inout bycopy byref oneway int char float double void _Bool
8380 ??? Why this selection of keywords but not, for example, storage
8381 class specifiers? */
8383 static tree
8384 c_parser_objc_selector (c_parser *parser)
8386 c_token *token = c_parser_peek_token (parser);
8387 tree value = token->value;
8388 if (token->type == CPP_NAME)
8390 c_parser_consume_token (parser);
8391 return value;
8393 if (token->type != CPP_KEYWORD)
8394 return NULL_TREE;
8395 switch (token->keyword)
8397 case RID_ENUM:
8398 case RID_STRUCT:
8399 case RID_UNION:
8400 case RID_IF:
8401 case RID_ELSE:
8402 case RID_WHILE:
8403 case RID_DO:
8404 case RID_FOR:
8405 case RID_SWITCH:
8406 case RID_CASE:
8407 case RID_DEFAULT:
8408 case RID_BREAK:
8409 case RID_CONTINUE:
8410 case RID_RETURN:
8411 case RID_GOTO:
8412 case RID_ASM:
8413 case RID_SIZEOF:
8414 case RID_TYPEOF:
8415 case RID_ALIGNOF:
8416 case RID_UNSIGNED:
8417 case RID_LONG:
8418 case RID_INT128:
8419 case RID_CONST:
8420 case RID_SHORT:
8421 case RID_VOLATILE:
8422 case RID_SIGNED:
8423 case RID_RESTRICT:
8424 case RID_COMPLEX:
8425 case RID_IN:
8426 case RID_OUT:
8427 case RID_INOUT:
8428 case RID_BYCOPY:
8429 case RID_BYREF:
8430 case RID_ONEWAY:
8431 case RID_INT:
8432 case RID_CHAR:
8433 case RID_FLOAT:
8434 case RID_DOUBLE:
8435 case RID_VOID:
8436 case RID_BOOL:
8437 c_parser_consume_token (parser);
8438 return value;
8439 default:
8440 return NULL_TREE;
8444 /* Parse an objc-selector-arg.
8446 objc-selector-arg:
8447 objc-selector
8448 objc-keywordname-list
8450 objc-keywordname-list:
8451 objc-keywordname
8452 objc-keywordname-list objc-keywordname
8454 objc-keywordname:
8455 objc-selector :
8459 static tree
8460 c_parser_objc_selector_arg (c_parser *parser)
8462 tree sel = c_parser_objc_selector (parser);
8463 tree list = NULL_TREE;
8464 if (sel && c_parser_next_token_is_not (parser, CPP_COLON))
8465 return sel;
8466 while (true)
8468 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
8469 return list;
8470 list = chainon (list, build_tree_list (sel, NULL_TREE));
8471 sel = c_parser_objc_selector (parser);
8472 if (!sel && c_parser_next_token_is_not (parser, CPP_COLON))
8473 break;
8475 return list;
8478 /* Parse an objc-receiver.
8480 objc-receiver:
8481 expression
8482 class-name
8483 type-name
8486 static tree
8487 c_parser_objc_receiver (c_parser *parser)
8489 if (c_parser_peek_token (parser)->type == CPP_NAME
8490 && (c_parser_peek_token (parser)->id_kind == C_ID_TYPENAME
8491 || c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME))
8493 tree id = c_parser_peek_token (parser)->value;
8494 c_parser_consume_token (parser);
8495 return objc_get_class_reference (id);
8497 return c_fully_fold (c_parser_expression (parser).value, false, NULL);
8500 /* Parse objc-message-args.
8502 objc-message-args:
8503 objc-selector
8504 objc-keywordarg-list
8506 objc-keywordarg-list:
8507 objc-keywordarg
8508 objc-keywordarg-list objc-keywordarg
8510 objc-keywordarg:
8511 objc-selector : objc-keywordexpr
8512 : objc-keywordexpr
8515 static tree
8516 c_parser_objc_message_args (c_parser *parser)
8518 tree sel = c_parser_objc_selector (parser);
8519 tree list = NULL_TREE;
8520 if (sel && c_parser_next_token_is_not (parser, CPP_COLON))
8521 return sel;
8522 while (true)
8524 tree keywordexpr;
8525 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
8526 return error_mark_node;
8527 keywordexpr = c_parser_objc_keywordexpr (parser);
8528 list = chainon (list, build_tree_list (sel, keywordexpr));
8529 sel = c_parser_objc_selector (parser);
8530 if (!sel && c_parser_next_token_is_not (parser, CPP_COLON))
8531 break;
8533 return list;
8536 /* Parse an objc-keywordexpr.
8538 objc-keywordexpr:
8539 nonempty-expr-list
8542 static tree
8543 c_parser_objc_keywordexpr (c_parser *parser)
8545 tree ret;
8546 vec<tree, va_gc> *expr_list = c_parser_expr_list (parser, true, true,
8547 NULL, NULL, NULL);
8548 if (vec_safe_length (expr_list) == 1)
8550 /* Just return the expression, remove a level of
8551 indirection. */
8552 ret = (*expr_list)[0];
8554 else
8556 /* We have a comma expression, we will collapse later. */
8557 ret = build_tree_list_vec (expr_list);
8559 release_tree_vector (expr_list);
8560 return ret;
8563 /* A check, needed in several places, that ObjC interface, implementation or
8564 method definitions are not prefixed by incorrect items. */
8565 static bool
8566 c_parser_objc_diagnose_bad_element_prefix (c_parser *parser,
8567 struct c_declspecs *specs)
8569 if (!specs->declspecs_seen_p || specs->non_sc_seen_p
8570 || specs->typespec_kind != ctsk_none)
8572 c_parser_error (parser,
8573 "no type or storage class may be specified here,");
8574 c_parser_skip_to_end_of_block_or_statement (parser);
8575 return true;
8577 return false;
8580 /* Parse an Objective-C @property declaration. The syntax is:
8582 objc-property-declaration:
8583 '@property' objc-property-attributes[opt] struct-declaration ;
8585 objc-property-attributes:
8586 '(' objc-property-attribute-list ')'
8588 objc-property-attribute-list:
8589 objc-property-attribute
8590 objc-property-attribute-list, objc-property-attribute
8592 objc-property-attribute
8593 'getter' = identifier
8594 'setter' = identifier
8595 'readonly'
8596 'readwrite'
8597 'assign'
8598 'retain'
8599 'copy'
8600 'nonatomic'
8602 For example:
8603 @property NSString *name;
8604 @property (readonly) id object;
8605 @property (retain, nonatomic, getter=getTheName) id name;
8606 @property int a, b, c;
8608 PS: This function is identical to cp_parser_objc_at_propery_declaration
8609 for C++. Keep them in sync. */
8610 static void
8611 c_parser_objc_at_property_declaration (c_parser *parser)
8613 /* The following variables hold the attributes of the properties as
8614 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
8615 seen. When we see an attribute, we set them to 'true' (if they
8616 are boolean properties) or to the identifier (if they have an
8617 argument, ie, for getter and setter). Note that here we only
8618 parse the list of attributes, check the syntax and accumulate the
8619 attributes that we find. objc_add_property_declaration() will
8620 then process the information. */
8621 bool property_assign = false;
8622 bool property_copy = false;
8623 tree property_getter_ident = NULL_TREE;
8624 bool property_nonatomic = false;
8625 bool property_readonly = false;
8626 bool property_readwrite = false;
8627 bool property_retain = false;
8628 tree property_setter_ident = NULL_TREE;
8630 /* 'properties' is the list of properties that we read. Usually a
8631 single one, but maybe more (eg, in "@property int a, b, c;" there
8632 are three). */
8633 tree properties;
8634 location_t loc;
8636 loc = c_parser_peek_token (parser)->location;
8637 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_PROPERTY));
8639 c_parser_consume_token (parser); /* Eat '@property'. */
8641 /* Parse the optional attribute list... */
8642 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
8644 /* Eat the '(' */
8645 c_parser_consume_token (parser);
8647 /* Property attribute keywords are valid now. */
8648 parser->objc_property_attr_context = true;
8650 while (true)
8652 bool syntax_error = false;
8653 c_token *token = c_parser_peek_token (parser);
8654 enum rid keyword;
8656 if (token->type != CPP_KEYWORD)
8658 if (token->type == CPP_CLOSE_PAREN)
8659 c_parser_error (parser, "expected identifier");
8660 else
8662 c_parser_consume_token (parser);
8663 c_parser_error (parser, "unknown property attribute");
8665 break;
8667 keyword = token->keyword;
8668 c_parser_consume_token (parser);
8669 switch (keyword)
8671 case RID_ASSIGN: property_assign = true; break;
8672 case RID_COPY: property_copy = true; break;
8673 case RID_NONATOMIC: property_nonatomic = true; break;
8674 case RID_READONLY: property_readonly = true; break;
8675 case RID_READWRITE: property_readwrite = true; break;
8676 case RID_RETAIN: property_retain = true; break;
8678 case RID_GETTER:
8679 case RID_SETTER:
8680 if (c_parser_next_token_is_not (parser, CPP_EQ))
8682 if (keyword == RID_GETTER)
8683 c_parser_error (parser,
8684 "missing %<=%> (after %<getter%> attribute)");
8685 else
8686 c_parser_error (parser,
8687 "missing %<=%> (after %<setter%> attribute)");
8688 syntax_error = true;
8689 break;
8691 c_parser_consume_token (parser); /* eat the = */
8692 if (c_parser_next_token_is_not (parser, CPP_NAME))
8694 c_parser_error (parser, "expected identifier");
8695 syntax_error = true;
8696 break;
8698 if (keyword == RID_SETTER)
8700 if (property_setter_ident != NULL_TREE)
8701 c_parser_error (parser, "the %<setter%> attribute may only be specified once");
8702 else
8703 property_setter_ident = c_parser_peek_token (parser)->value;
8704 c_parser_consume_token (parser);
8705 if (c_parser_next_token_is_not (parser, CPP_COLON))
8706 c_parser_error (parser, "setter name must terminate with %<:%>");
8707 else
8708 c_parser_consume_token (parser);
8710 else
8712 if (property_getter_ident != NULL_TREE)
8713 c_parser_error (parser, "the %<getter%> attribute may only be specified once");
8714 else
8715 property_getter_ident = c_parser_peek_token (parser)->value;
8716 c_parser_consume_token (parser);
8718 break;
8719 default:
8720 c_parser_error (parser, "unknown property attribute");
8721 syntax_error = true;
8722 break;
8725 if (syntax_error)
8726 break;
8728 if (c_parser_next_token_is (parser, CPP_COMMA))
8729 c_parser_consume_token (parser);
8730 else
8731 break;
8733 parser->objc_property_attr_context = false;
8734 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
8736 /* ... and the property declaration(s). */
8737 properties = c_parser_struct_declaration (parser);
8739 if (properties == error_mark_node)
8741 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
8742 parser->error = false;
8743 return;
8746 if (properties == NULL_TREE)
8747 c_parser_error (parser, "expected identifier");
8748 else
8750 /* Comma-separated properties are chained together in
8751 reverse order; add them one by one. */
8752 properties = nreverse (properties);
8754 for (; properties; properties = TREE_CHAIN (properties))
8755 objc_add_property_declaration (loc, copy_node (properties),
8756 property_readonly, property_readwrite,
8757 property_assign, property_retain,
8758 property_copy, property_nonatomic,
8759 property_getter_ident, property_setter_ident);
8762 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
8763 parser->error = false;
8766 /* Parse an Objective-C @synthesize declaration. The syntax is:
8768 objc-synthesize-declaration:
8769 @synthesize objc-synthesize-identifier-list ;
8771 objc-synthesize-identifier-list:
8772 objc-synthesize-identifier
8773 objc-synthesize-identifier-list, objc-synthesize-identifier
8775 objc-synthesize-identifier
8776 identifier
8777 identifier = identifier
8779 For example:
8780 @synthesize MyProperty;
8781 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
8783 PS: This function is identical to cp_parser_objc_at_synthesize_declaration
8784 for C++. Keep them in sync.
8786 static void
8787 c_parser_objc_at_synthesize_declaration (c_parser *parser)
8789 tree list = NULL_TREE;
8790 location_t loc;
8791 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_SYNTHESIZE));
8792 loc = c_parser_peek_token (parser)->location;
8794 c_parser_consume_token (parser);
8795 while (true)
8797 tree property, ivar;
8798 if (c_parser_next_token_is_not (parser, CPP_NAME))
8800 c_parser_error (parser, "expected identifier");
8801 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
8802 /* Once we find the semicolon, we can resume normal parsing.
8803 We have to reset parser->error manually because
8804 c_parser_skip_until_found() won't reset it for us if the
8805 next token is precisely a semicolon. */
8806 parser->error = false;
8807 return;
8809 property = c_parser_peek_token (parser)->value;
8810 c_parser_consume_token (parser);
8811 if (c_parser_next_token_is (parser, CPP_EQ))
8813 c_parser_consume_token (parser);
8814 if (c_parser_next_token_is_not (parser, CPP_NAME))
8816 c_parser_error (parser, "expected identifier");
8817 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
8818 parser->error = false;
8819 return;
8821 ivar = c_parser_peek_token (parser)->value;
8822 c_parser_consume_token (parser);
8824 else
8825 ivar = NULL_TREE;
8826 list = chainon (list, build_tree_list (ivar, property));
8827 if (c_parser_next_token_is (parser, CPP_COMMA))
8828 c_parser_consume_token (parser);
8829 else
8830 break;
8832 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
8833 objc_add_synthesize_declaration (loc, list);
8836 /* Parse an Objective-C @dynamic declaration. The syntax is:
8838 objc-dynamic-declaration:
8839 @dynamic identifier-list ;
8841 For example:
8842 @dynamic MyProperty;
8843 @dynamic MyProperty, AnotherProperty;
8845 PS: This function is identical to cp_parser_objc_at_dynamic_declaration
8846 for C++. Keep them in sync.
8848 static void
8849 c_parser_objc_at_dynamic_declaration (c_parser *parser)
8851 tree list = NULL_TREE;
8852 location_t loc;
8853 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_DYNAMIC));
8854 loc = c_parser_peek_token (parser)->location;
8856 c_parser_consume_token (parser);
8857 while (true)
8859 tree property;
8860 if (c_parser_next_token_is_not (parser, CPP_NAME))
8862 c_parser_error (parser, "expected identifier");
8863 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
8864 parser->error = false;
8865 return;
8867 property = c_parser_peek_token (parser)->value;
8868 list = chainon (list, build_tree_list (NULL_TREE, property));
8869 c_parser_consume_token (parser);
8870 if (c_parser_next_token_is (parser, CPP_COMMA))
8871 c_parser_consume_token (parser);
8872 else
8873 break;
8875 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
8876 objc_add_dynamic_declaration (loc, list);
8880 /* Handle pragmas. Some OpenMP pragmas are associated with, and therefore
8881 should be considered, statements. ALLOW_STMT is true if we're within
8882 the context of a function and such pragmas are to be allowed. Returns
8883 true if we actually parsed such a pragma. */
8885 static bool
8886 c_parser_pragma (c_parser *parser, enum pragma_context context)
8888 unsigned int id;
8890 id = c_parser_peek_token (parser)->pragma_kind;
8891 gcc_assert (id != PRAGMA_NONE);
8893 switch (id)
8895 case PRAGMA_OMP_BARRIER:
8896 if (context != pragma_compound)
8898 if (context == pragma_stmt)
8899 c_parser_error (parser, "%<#pragma omp barrier%> may only be "
8900 "used in compound statements");
8901 goto bad_stmt;
8903 c_parser_omp_barrier (parser);
8904 return false;
8906 case PRAGMA_OMP_FLUSH:
8907 if (context != pragma_compound)
8909 if (context == pragma_stmt)
8910 c_parser_error (parser, "%<#pragma omp flush%> may only be "
8911 "used in compound statements");
8912 goto bad_stmt;
8914 c_parser_omp_flush (parser);
8915 return false;
8917 case PRAGMA_OMP_TASKWAIT:
8918 if (context != pragma_compound)
8920 if (context == pragma_stmt)
8921 c_parser_error (parser, "%<#pragma omp taskwait%> may only be "
8922 "used in compound statements");
8923 goto bad_stmt;
8925 c_parser_omp_taskwait (parser);
8926 return false;
8928 case PRAGMA_OMP_TASKYIELD:
8929 if (context != pragma_compound)
8931 if (context == pragma_stmt)
8932 c_parser_error (parser, "%<#pragma omp taskyield%> may only be "
8933 "used in compound statements");
8934 goto bad_stmt;
8936 c_parser_omp_taskyield (parser);
8937 return false;
8939 case PRAGMA_OMP_THREADPRIVATE:
8940 c_parser_omp_threadprivate (parser);
8941 return false;
8943 case PRAGMA_OMP_SECTION:
8944 error_at (c_parser_peek_token (parser)->location,
8945 "%<#pragma omp section%> may only be used in "
8946 "%<#pragma omp sections%> construct");
8947 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
8948 return false;
8950 case PRAGMA_GCC_PCH_PREPROCESS:
8951 c_parser_error (parser, "%<#pragma GCC pch_preprocess%> must be first");
8952 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
8953 return false;
8955 default:
8956 if (id < PRAGMA_FIRST_EXTERNAL)
8958 if (context == pragma_external)
8960 bad_stmt:
8961 c_parser_error (parser, "expected declaration specifiers");
8962 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
8963 return false;
8965 c_parser_omp_construct (parser);
8966 return true;
8968 break;
8971 c_parser_consume_pragma (parser);
8972 c_invoke_pragma_handler (id);
8974 /* Skip to EOL, but suppress any error message. Those will have been
8975 generated by the handler routine through calling error, as opposed
8976 to calling c_parser_error. */
8977 parser->error = true;
8978 c_parser_skip_to_pragma_eol (parser);
8980 return false;
8983 /* The interface the pragma parsers have to the lexer. */
8985 enum cpp_ttype
8986 pragma_lex (tree *value)
8988 c_token *tok = c_parser_peek_token (the_parser);
8989 enum cpp_ttype ret = tok->type;
8991 *value = tok->value;
8992 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
8993 ret = CPP_EOF;
8994 else
8996 if (ret == CPP_KEYWORD)
8997 ret = CPP_NAME;
8998 c_parser_consume_token (the_parser);
9001 return ret;
9004 static void
9005 c_parser_pragma_pch_preprocess (c_parser *parser)
9007 tree name = NULL;
9009 c_parser_consume_pragma (parser);
9010 if (c_parser_next_token_is (parser, CPP_STRING))
9012 name = c_parser_peek_token (parser)->value;
9013 c_parser_consume_token (parser);
9015 else
9016 c_parser_error (parser, "expected string literal");
9017 c_parser_skip_to_pragma_eol (parser);
9019 if (name)
9020 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
9023 /* OpenMP 2.5 parsing routines. */
9025 /* Returns name of the next clause.
9026 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
9027 the token is not consumed. Otherwise appropriate pragma_omp_clause is
9028 returned and the token is consumed. */
9030 static pragma_omp_clause
9031 c_parser_omp_clause_name (c_parser *parser)
9033 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
9035 if (c_parser_next_token_is_keyword (parser, RID_IF))
9036 result = PRAGMA_OMP_CLAUSE_IF;
9037 else if (c_parser_next_token_is_keyword (parser, RID_DEFAULT))
9038 result = PRAGMA_OMP_CLAUSE_DEFAULT;
9039 else if (c_parser_next_token_is (parser, CPP_NAME))
9041 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
9043 switch (p[0])
9045 case 'c':
9046 if (!strcmp ("collapse", p))
9047 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
9048 else if (!strcmp ("copyin", p))
9049 result = PRAGMA_OMP_CLAUSE_COPYIN;
9050 else if (!strcmp ("copyprivate", p))
9051 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
9052 break;
9053 case 'f':
9054 if (!strcmp ("final", p))
9055 result = PRAGMA_OMP_CLAUSE_FINAL;
9056 else if (!strcmp ("firstprivate", p))
9057 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
9058 break;
9059 case 'l':
9060 if (!strcmp ("lastprivate", p))
9061 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
9062 break;
9063 case 'm':
9064 if (!strcmp ("mergeable", p))
9065 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
9066 break;
9067 case 'n':
9068 if (!strcmp ("nowait", p))
9069 result = PRAGMA_OMP_CLAUSE_NOWAIT;
9070 else if (!strcmp ("num_threads", p))
9071 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
9072 break;
9073 case 'o':
9074 if (!strcmp ("ordered", p))
9075 result = PRAGMA_OMP_CLAUSE_ORDERED;
9076 break;
9077 case 'p':
9078 if (!strcmp ("private", p))
9079 result = PRAGMA_OMP_CLAUSE_PRIVATE;
9080 break;
9081 case 'r':
9082 if (!strcmp ("reduction", p))
9083 result = PRAGMA_OMP_CLAUSE_REDUCTION;
9084 break;
9085 case 's':
9086 if (!strcmp ("schedule", p))
9087 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
9088 else if (!strcmp ("shared", p))
9089 result = PRAGMA_OMP_CLAUSE_SHARED;
9090 break;
9091 case 'u':
9092 if (!strcmp ("untied", p))
9093 result = PRAGMA_OMP_CLAUSE_UNTIED;
9094 break;
9098 if (result != PRAGMA_OMP_CLAUSE_NONE)
9099 c_parser_consume_token (parser);
9101 return result;
9104 /* Validate that a clause of the given type does not already exist. */
9106 static void
9107 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
9108 const char *name)
9110 tree c;
9112 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
9113 if (OMP_CLAUSE_CODE (c) == code)
9115 location_t loc = OMP_CLAUSE_LOCATION (c);
9116 error_at (loc, "too many %qs clauses", name);
9117 break;
9121 /* OpenMP 2.5:
9122 variable-list:
9123 identifier
9124 variable-list , identifier
9126 If KIND is nonzero, create the appropriate node and install the
9127 decl in OMP_CLAUSE_DECL and add the node to the head of the list.
9128 If KIND is nonzero, CLAUSE_LOC is the location of the clause.
9130 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
9131 return the list created. */
9133 static tree
9134 c_parser_omp_variable_list (c_parser *parser,
9135 location_t clause_loc,
9136 enum omp_clause_code kind,
9137 tree list)
9139 if (c_parser_next_token_is_not (parser, CPP_NAME)
9140 || c_parser_peek_token (parser)->id_kind != C_ID_ID)
9141 c_parser_error (parser, "expected identifier");
9143 while (c_parser_next_token_is (parser, CPP_NAME)
9144 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
9146 tree t = lookup_name (c_parser_peek_token (parser)->value);
9148 if (t == NULL_TREE)
9149 undeclared_variable (c_parser_peek_token (parser)->location,
9150 c_parser_peek_token (parser)->value);
9151 else if (t == error_mark_node)
9153 else if (kind != 0)
9155 tree u = build_omp_clause (clause_loc, kind);
9156 OMP_CLAUSE_DECL (u) = t;
9157 OMP_CLAUSE_CHAIN (u) = list;
9158 list = u;
9160 else
9161 list = tree_cons (t, NULL_TREE, list);
9163 c_parser_consume_token (parser);
9165 if (c_parser_next_token_is_not (parser, CPP_COMMA))
9166 break;
9168 c_parser_consume_token (parser);
9171 return list;
9174 /* Similarly, but expect leading and trailing parenthesis. This is a very
9175 common case for omp clauses. */
9177 static tree
9178 c_parser_omp_var_list_parens (c_parser *parser, enum omp_clause_code kind,
9179 tree list)
9181 /* The clauses location. */
9182 location_t loc = c_parser_peek_token (parser)->location;
9184 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
9186 list = c_parser_omp_variable_list (parser, loc, kind, list);
9187 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
9189 return list;
9192 /* OpenMP 3.0:
9193 collapse ( constant-expression ) */
9195 static tree
9196 c_parser_omp_clause_collapse (c_parser *parser, tree list)
9198 tree c, num = error_mark_node;
9199 HOST_WIDE_INT n;
9200 location_t loc;
9202 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse");
9204 loc = c_parser_peek_token (parser)->location;
9205 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
9207 num = c_parser_expr_no_commas (parser, NULL).value;
9208 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
9210 if (num == error_mark_node)
9211 return list;
9212 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
9213 || !host_integerp (num, 0)
9214 || (n = tree_low_cst (num, 0)) <= 0
9215 || (int) n != n)
9217 error_at (loc,
9218 "collapse argument needs positive constant integer expression");
9219 return list;
9221 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
9222 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
9223 OMP_CLAUSE_CHAIN (c) = list;
9224 return c;
9227 /* OpenMP 2.5:
9228 copyin ( variable-list ) */
9230 static tree
9231 c_parser_omp_clause_copyin (c_parser *parser, tree list)
9233 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_COPYIN, list);
9236 /* OpenMP 2.5:
9237 copyprivate ( variable-list ) */
9239 static tree
9240 c_parser_omp_clause_copyprivate (c_parser *parser, tree list)
9242 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_COPYPRIVATE, list);
9245 /* OpenMP 2.5:
9246 default ( shared | none ) */
9248 static tree
9249 c_parser_omp_clause_default (c_parser *parser, tree list)
9251 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
9252 location_t loc = c_parser_peek_token (parser)->location;
9253 tree c;
9255 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
9256 return list;
9257 if (c_parser_next_token_is (parser, CPP_NAME))
9259 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
9261 switch (p[0])
9263 case 'n':
9264 if (strcmp ("none", p) != 0)
9265 goto invalid_kind;
9266 kind = OMP_CLAUSE_DEFAULT_NONE;
9267 break;
9269 case 's':
9270 if (strcmp ("shared", p) != 0)
9271 goto invalid_kind;
9272 kind = OMP_CLAUSE_DEFAULT_SHARED;
9273 break;
9275 default:
9276 goto invalid_kind;
9279 c_parser_consume_token (parser);
9281 else
9283 invalid_kind:
9284 c_parser_error (parser, "expected %<none%> or %<shared%>");
9286 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
9288 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
9289 return list;
9291 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default");
9292 c = build_omp_clause (loc, OMP_CLAUSE_DEFAULT);
9293 OMP_CLAUSE_CHAIN (c) = list;
9294 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
9296 return c;
9299 /* OpenMP 2.5:
9300 firstprivate ( variable-list ) */
9302 static tree
9303 c_parser_omp_clause_firstprivate (c_parser *parser, tree list)
9305 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_FIRSTPRIVATE, list);
9308 /* OpenMP 3.1:
9309 final ( expression ) */
9311 static tree
9312 c_parser_omp_clause_final (c_parser *parser, tree list)
9314 location_t loc = c_parser_peek_token (parser)->location;
9315 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
9317 tree t = c_parser_paren_condition (parser);
9318 tree c;
9320 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final");
9322 c = build_omp_clause (loc, OMP_CLAUSE_FINAL);
9323 OMP_CLAUSE_FINAL_EXPR (c) = t;
9324 OMP_CLAUSE_CHAIN (c) = list;
9325 list = c;
9327 else
9328 c_parser_error (parser, "expected %<(%>");
9330 return list;
9333 /* OpenMP 2.5:
9334 if ( expression ) */
9336 static tree
9337 c_parser_omp_clause_if (c_parser *parser, tree list)
9339 location_t loc = c_parser_peek_token (parser)->location;
9340 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
9342 tree t = c_parser_paren_condition (parser);
9343 tree c;
9345 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if");
9347 c = build_omp_clause (loc, OMP_CLAUSE_IF);
9348 OMP_CLAUSE_IF_EXPR (c) = t;
9349 OMP_CLAUSE_CHAIN (c) = list;
9350 list = c;
9352 else
9353 c_parser_error (parser, "expected %<(%>");
9355 return list;
9358 /* OpenMP 2.5:
9359 lastprivate ( variable-list ) */
9361 static tree
9362 c_parser_omp_clause_lastprivate (c_parser *parser, tree list)
9364 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_LASTPRIVATE, list);
9367 /* OpenMP 3.1:
9368 mergeable */
9370 static tree
9371 c_parser_omp_clause_mergeable (c_parser *parser ATTRIBUTE_UNUSED, tree list)
9373 tree c;
9375 /* FIXME: Should we allow duplicates? */
9376 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable");
9378 c = build_omp_clause (c_parser_peek_token (parser)->location,
9379 OMP_CLAUSE_MERGEABLE);
9380 OMP_CLAUSE_CHAIN (c) = list;
9382 return c;
9385 /* OpenMP 2.5:
9386 nowait */
9388 static tree
9389 c_parser_omp_clause_nowait (c_parser *parser ATTRIBUTE_UNUSED, tree list)
9391 tree c;
9392 location_t loc = c_parser_peek_token (parser)->location;
9394 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait");
9396 c = build_omp_clause (loc, OMP_CLAUSE_NOWAIT);
9397 OMP_CLAUSE_CHAIN (c) = list;
9398 return c;
9401 /* OpenMP 2.5:
9402 num_threads ( expression ) */
9404 static tree
9405 c_parser_omp_clause_num_threads (c_parser *parser, tree list)
9407 location_t num_threads_loc = c_parser_peek_token (parser)->location;
9408 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
9410 location_t expr_loc = c_parser_peek_token (parser)->location;
9411 tree c, t = c_parser_expression (parser).value;
9412 mark_exp_read (t);
9413 t = c_fully_fold (t, false, NULL);
9415 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
9417 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
9419 c_parser_error (parser, "expected integer expression");
9420 return list;
9423 /* Attempt to statically determine when the number isn't positive. */
9424 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
9425 build_int_cst (TREE_TYPE (t), 0));
9426 if (CAN_HAVE_LOCATION_P (c))
9427 SET_EXPR_LOCATION (c, expr_loc);
9428 if (c == boolean_true_node)
9430 warning_at (expr_loc, 0,
9431 "%<num_threads%> value must be positive");
9432 t = integer_one_node;
9435 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS, "num_threads");
9437 c = build_omp_clause (num_threads_loc, OMP_CLAUSE_NUM_THREADS);
9438 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
9439 OMP_CLAUSE_CHAIN (c) = list;
9440 list = c;
9443 return list;
9446 /* OpenMP 2.5:
9447 ordered */
9449 static tree
9450 c_parser_omp_clause_ordered (c_parser *parser, tree list)
9452 tree c;
9454 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED, "ordered");
9456 c = build_omp_clause (c_parser_peek_token (parser)->location,
9457 OMP_CLAUSE_ORDERED);
9458 OMP_CLAUSE_CHAIN (c) = list;
9460 return c;
9463 /* OpenMP 2.5:
9464 private ( variable-list ) */
9466 static tree
9467 c_parser_omp_clause_private (c_parser *parser, tree list)
9469 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_PRIVATE, list);
9472 /* OpenMP 2.5:
9473 reduction ( reduction-operator : variable-list )
9475 reduction-operator:
9476 One of: + * - & ^ | && ||
9478 OpenMP 3.1:
9480 reduction-operator:
9481 One of: + * - & ^ | && || max min */
9483 static tree
9484 c_parser_omp_clause_reduction (c_parser *parser, tree list)
9486 location_t clause_loc = c_parser_peek_token (parser)->location;
9487 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
9489 enum tree_code code;
9491 switch (c_parser_peek_token (parser)->type)
9493 case CPP_PLUS:
9494 code = PLUS_EXPR;
9495 break;
9496 case CPP_MULT:
9497 code = MULT_EXPR;
9498 break;
9499 case CPP_MINUS:
9500 code = MINUS_EXPR;
9501 break;
9502 case CPP_AND:
9503 code = BIT_AND_EXPR;
9504 break;
9505 case CPP_XOR:
9506 code = BIT_XOR_EXPR;
9507 break;
9508 case CPP_OR:
9509 code = BIT_IOR_EXPR;
9510 break;
9511 case CPP_AND_AND:
9512 code = TRUTH_ANDIF_EXPR;
9513 break;
9514 case CPP_OR_OR:
9515 code = TRUTH_ORIF_EXPR;
9516 break;
9517 case CPP_NAME:
9519 const char *p
9520 = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
9521 if (strcmp (p, "min") == 0)
9523 code = MIN_EXPR;
9524 break;
9526 if (strcmp (p, "max") == 0)
9528 code = MAX_EXPR;
9529 break;
9532 /* FALLTHRU */
9533 default:
9534 c_parser_error (parser,
9535 "expected %<+%>, %<*%>, %<-%>, %<&%>, "
9536 "%<^%>, %<|%>, %<&&%>, %<||%>, %<min%> or %<max%>");
9537 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 0);
9538 return list;
9540 c_parser_consume_token (parser);
9541 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
9543 tree nl, c;
9545 nl = c_parser_omp_variable_list (parser, clause_loc,
9546 OMP_CLAUSE_REDUCTION, list);
9547 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
9548 OMP_CLAUSE_REDUCTION_CODE (c) = code;
9550 list = nl;
9552 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
9554 return list;
9557 /* OpenMP 2.5:
9558 schedule ( schedule-kind )
9559 schedule ( schedule-kind , expression )
9561 schedule-kind:
9562 static | dynamic | guided | runtime | auto
9565 static tree
9566 c_parser_omp_clause_schedule (c_parser *parser, tree list)
9568 tree c, t;
9569 location_t loc = c_parser_peek_token (parser)->location;
9571 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
9572 return list;
9574 c = build_omp_clause (loc, OMP_CLAUSE_SCHEDULE);
9576 if (c_parser_next_token_is (parser, CPP_NAME))
9578 tree kind = c_parser_peek_token (parser)->value;
9579 const char *p = IDENTIFIER_POINTER (kind);
9581 switch (p[0])
9583 case 'd':
9584 if (strcmp ("dynamic", p) != 0)
9585 goto invalid_kind;
9586 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
9587 break;
9589 case 'g':
9590 if (strcmp ("guided", p) != 0)
9591 goto invalid_kind;
9592 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
9593 break;
9595 case 'r':
9596 if (strcmp ("runtime", p) != 0)
9597 goto invalid_kind;
9598 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
9599 break;
9601 default:
9602 goto invalid_kind;
9605 else if (c_parser_next_token_is_keyword (parser, RID_STATIC))
9606 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
9607 else if (c_parser_next_token_is_keyword (parser, RID_AUTO))
9608 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
9609 else
9610 goto invalid_kind;
9612 c_parser_consume_token (parser);
9613 if (c_parser_next_token_is (parser, CPP_COMMA))
9615 location_t here;
9616 c_parser_consume_token (parser);
9618 here = c_parser_peek_token (parser)->location;
9619 t = c_parser_expr_no_commas (parser, NULL).value;
9620 mark_exp_read (t);
9621 t = c_fully_fold (t, false, NULL);
9623 if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
9624 error_at (here, "schedule %<runtime%> does not take "
9625 "a %<chunk_size%> parameter");
9626 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
9627 error_at (here,
9628 "schedule %<auto%> does not take "
9629 "a %<chunk_size%> parameter");
9630 else if (TREE_CODE (TREE_TYPE (t)) == INTEGER_TYPE)
9631 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
9632 else
9633 c_parser_error (parser, "expected integer expression");
9635 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
9637 else
9638 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
9639 "expected %<,%> or %<)%>");
9641 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule");
9642 OMP_CLAUSE_CHAIN (c) = list;
9643 return c;
9645 invalid_kind:
9646 c_parser_error (parser, "invalid schedule kind");
9647 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 0);
9648 return list;
9651 /* OpenMP 2.5:
9652 shared ( variable-list ) */
9654 static tree
9655 c_parser_omp_clause_shared (c_parser *parser, tree list)
9657 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_SHARED, list);
9660 /* OpenMP 3.0:
9661 untied */
9663 static tree
9664 c_parser_omp_clause_untied (c_parser *parser ATTRIBUTE_UNUSED, tree list)
9666 tree c;
9668 /* FIXME: Should we allow duplicates? */
9669 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied");
9671 c = build_omp_clause (c_parser_peek_token (parser)->location,
9672 OMP_CLAUSE_UNTIED);
9673 OMP_CLAUSE_CHAIN (c) = list;
9675 return c;
9678 /* Parse all OpenMP clauses. The set clauses allowed by the directive
9679 is a bitmask in MASK. Return the list of clauses found; the result
9680 of clause default goes in *pdefault. */
9682 static tree
9683 c_parser_omp_all_clauses (c_parser *parser, unsigned int mask,
9684 const char *where)
9686 tree clauses = NULL;
9687 bool first = true;
9689 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
9691 location_t here;
9692 pragma_omp_clause c_kind;
9693 const char *c_name;
9694 tree prev = clauses;
9696 if (!first && c_parser_next_token_is (parser, CPP_COMMA))
9697 c_parser_consume_token (parser);
9699 first = false;
9700 here = c_parser_peek_token (parser)->location;
9701 c_kind = c_parser_omp_clause_name (parser);
9703 switch (c_kind)
9705 case PRAGMA_OMP_CLAUSE_COLLAPSE:
9706 clauses = c_parser_omp_clause_collapse (parser, clauses);
9707 c_name = "collapse";
9708 break;
9709 case PRAGMA_OMP_CLAUSE_COPYIN:
9710 clauses = c_parser_omp_clause_copyin (parser, clauses);
9711 c_name = "copyin";
9712 break;
9713 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
9714 clauses = c_parser_omp_clause_copyprivate (parser, clauses);
9715 c_name = "copyprivate";
9716 break;
9717 case PRAGMA_OMP_CLAUSE_DEFAULT:
9718 clauses = c_parser_omp_clause_default (parser, clauses);
9719 c_name = "default";
9720 break;
9721 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
9722 clauses = c_parser_omp_clause_firstprivate (parser, clauses);
9723 c_name = "firstprivate";
9724 break;
9725 case PRAGMA_OMP_CLAUSE_FINAL:
9726 clauses = c_parser_omp_clause_final (parser, clauses);
9727 c_name = "final";
9728 break;
9729 case PRAGMA_OMP_CLAUSE_IF:
9730 clauses = c_parser_omp_clause_if (parser, clauses);
9731 c_name = "if";
9732 break;
9733 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
9734 clauses = c_parser_omp_clause_lastprivate (parser, clauses);
9735 c_name = "lastprivate";
9736 break;
9737 case PRAGMA_OMP_CLAUSE_MERGEABLE:
9738 clauses = c_parser_omp_clause_mergeable (parser, clauses);
9739 c_name = "mergeable";
9740 break;
9741 case PRAGMA_OMP_CLAUSE_NOWAIT:
9742 clauses = c_parser_omp_clause_nowait (parser, clauses);
9743 c_name = "nowait";
9744 break;
9745 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
9746 clauses = c_parser_omp_clause_num_threads (parser, clauses);
9747 c_name = "num_threads";
9748 break;
9749 case PRAGMA_OMP_CLAUSE_ORDERED:
9750 clauses = c_parser_omp_clause_ordered (parser, clauses);
9751 c_name = "ordered";
9752 break;
9753 case PRAGMA_OMP_CLAUSE_PRIVATE:
9754 clauses = c_parser_omp_clause_private (parser, clauses);
9755 c_name = "private";
9756 break;
9757 case PRAGMA_OMP_CLAUSE_REDUCTION:
9758 clauses = c_parser_omp_clause_reduction (parser, clauses);
9759 c_name = "reduction";
9760 break;
9761 case PRAGMA_OMP_CLAUSE_SCHEDULE:
9762 clauses = c_parser_omp_clause_schedule (parser, clauses);
9763 c_name = "schedule";
9764 break;
9765 case PRAGMA_OMP_CLAUSE_SHARED:
9766 clauses = c_parser_omp_clause_shared (parser, clauses);
9767 c_name = "shared";
9768 break;
9769 case PRAGMA_OMP_CLAUSE_UNTIED:
9770 clauses = c_parser_omp_clause_untied (parser, clauses);
9771 c_name = "untied";
9772 break;
9773 default:
9774 c_parser_error (parser, "expected %<#pragma omp%> clause");
9775 goto saw_error;
9778 if (((mask >> c_kind) & 1) == 0 && !parser->error)
9780 /* Remove the invalid clause(s) from the list to avoid
9781 confusing the rest of the compiler. */
9782 clauses = prev;
9783 error_at (here, "%qs is not valid for %qs", c_name, where);
9787 saw_error:
9788 c_parser_skip_to_pragma_eol (parser);
9790 return c_finish_omp_clauses (clauses);
9793 /* OpenMP 2.5:
9794 structured-block:
9795 statement
9797 In practice, we're also interested in adding the statement to an
9798 outer node. So it is convenient if we work around the fact that
9799 c_parser_statement calls add_stmt. */
9801 static tree
9802 c_parser_omp_structured_block (c_parser *parser)
9804 tree stmt = push_stmt_list ();
9805 c_parser_statement (parser);
9806 return pop_stmt_list (stmt);
9809 /* OpenMP 2.5:
9810 # pragma omp atomic new-line
9811 expression-stmt
9813 expression-stmt:
9814 x binop= expr | x++ | ++x | x-- | --x
9815 binop:
9816 +, *, -, /, &, ^, |, <<, >>
9818 where x is an lvalue expression with scalar type.
9820 OpenMP 3.1:
9821 # pragma omp atomic new-line
9822 update-stmt
9824 # pragma omp atomic read new-line
9825 read-stmt
9827 # pragma omp atomic write new-line
9828 write-stmt
9830 # pragma omp atomic update new-line
9831 update-stmt
9833 # pragma omp atomic capture new-line
9834 capture-stmt
9836 # pragma omp atomic capture new-line
9837 capture-block
9839 read-stmt:
9840 v = x
9841 write-stmt:
9842 x = expr
9843 update-stmt:
9844 expression-stmt | x = x binop expr
9845 capture-stmt:
9846 v = x binop= expr | v = x++ | v = ++x | v = x-- | v = --x
9847 capture-block:
9848 { v = x; update-stmt; } | { update-stmt; v = x; }
9850 where x and v are lvalue expressions with scalar type.
9852 LOC is the location of the #pragma token. */
9854 static void
9855 c_parser_omp_atomic (location_t loc, c_parser *parser)
9857 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE;
9858 tree lhs1 = NULL_TREE, rhs1 = NULL_TREE;
9859 tree stmt, orig_lhs;
9860 enum tree_code code = OMP_ATOMIC, opcode = NOP_EXPR;
9861 struct c_expr rhs_expr;
9862 bool structured_block = false;
9864 if (c_parser_next_token_is (parser, CPP_NAME))
9866 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
9868 if (!strcmp (p, "read"))
9869 code = OMP_ATOMIC_READ;
9870 else if (!strcmp (p, "write"))
9871 code = NOP_EXPR;
9872 else if (!strcmp (p, "update"))
9873 code = OMP_ATOMIC;
9874 else if (!strcmp (p, "capture"))
9875 code = OMP_ATOMIC_CAPTURE_NEW;
9876 else
9877 p = NULL;
9878 if (p)
9879 c_parser_consume_token (parser);
9881 c_parser_skip_to_pragma_eol (parser);
9883 switch (code)
9885 case OMP_ATOMIC_READ:
9886 case NOP_EXPR: /* atomic write */
9887 v = c_parser_unary_expression (parser).value;
9888 v = c_fully_fold (v, false, NULL);
9889 if (v == error_mark_node)
9890 goto saw_error;
9891 loc = c_parser_peek_token (parser)->location;
9892 if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
9893 goto saw_error;
9894 if (code == NOP_EXPR)
9895 lhs = c_parser_expression (parser).value;
9896 else
9897 lhs = c_parser_unary_expression (parser).value;
9898 lhs = c_fully_fold (lhs, false, NULL);
9899 if (lhs == error_mark_node)
9900 goto saw_error;
9901 if (code == NOP_EXPR)
9903 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
9904 opcode. */
9905 code = OMP_ATOMIC;
9906 rhs = lhs;
9907 lhs = v;
9908 v = NULL_TREE;
9910 goto done;
9911 case OMP_ATOMIC_CAPTURE_NEW:
9912 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
9914 c_parser_consume_token (parser);
9915 structured_block = true;
9917 else
9919 v = c_parser_unary_expression (parser).value;
9920 v = c_fully_fold (v, false, NULL);
9921 if (v == error_mark_node)
9922 goto saw_error;
9923 if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
9924 goto saw_error;
9926 break;
9927 default:
9928 break;
9931 /* For structured_block case we don't know yet whether
9932 old or new x should be captured. */
9933 restart:
9934 lhs = c_parser_unary_expression (parser).value;
9935 lhs = c_fully_fold (lhs, false, NULL);
9936 orig_lhs = lhs;
9937 switch (TREE_CODE (lhs))
9939 case ERROR_MARK:
9940 saw_error:
9941 c_parser_skip_to_end_of_block_or_statement (parser);
9942 if (structured_block)
9944 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
9945 c_parser_consume_token (parser);
9946 else if (code == OMP_ATOMIC_CAPTURE_NEW)
9948 c_parser_skip_to_end_of_block_or_statement (parser);
9949 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
9950 c_parser_consume_token (parser);
9953 return;
9955 case POSTINCREMENT_EXPR:
9956 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
9957 code = OMP_ATOMIC_CAPTURE_OLD;
9958 /* FALLTHROUGH */
9959 case PREINCREMENT_EXPR:
9960 lhs = TREE_OPERAND (lhs, 0);
9961 opcode = PLUS_EXPR;
9962 rhs = integer_one_node;
9963 break;
9965 case POSTDECREMENT_EXPR:
9966 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
9967 code = OMP_ATOMIC_CAPTURE_OLD;
9968 /* FALLTHROUGH */
9969 case PREDECREMENT_EXPR:
9970 lhs = TREE_OPERAND (lhs, 0);
9971 opcode = MINUS_EXPR;
9972 rhs = integer_one_node;
9973 break;
9975 case COMPOUND_EXPR:
9976 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
9977 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
9978 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
9979 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
9980 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
9981 (TREE_OPERAND (lhs, 1), 0), 0)))
9982 == BOOLEAN_TYPE)
9983 /* Undo effects of boolean_increment for post {in,de}crement. */
9984 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
9985 /* FALLTHRU */
9986 case MODIFY_EXPR:
9987 if (TREE_CODE (lhs) == MODIFY_EXPR
9988 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
9990 /* Undo effects of boolean_increment. */
9991 if (integer_onep (TREE_OPERAND (lhs, 1)))
9993 /* This is pre or post increment. */
9994 rhs = TREE_OPERAND (lhs, 1);
9995 lhs = TREE_OPERAND (lhs, 0);
9996 opcode = NOP_EXPR;
9997 if (code == OMP_ATOMIC_CAPTURE_NEW
9998 && !structured_block
9999 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
10000 code = OMP_ATOMIC_CAPTURE_OLD;
10001 break;
10003 if (TREE_CODE (TREE_OPERAND (lhs, 1)) == TRUTH_NOT_EXPR
10004 && TREE_OPERAND (lhs, 0)
10005 == TREE_OPERAND (TREE_OPERAND (lhs, 1), 0))
10007 /* This is pre or post decrement. */
10008 rhs = TREE_OPERAND (lhs, 1);
10009 lhs = TREE_OPERAND (lhs, 0);
10010 opcode = NOP_EXPR;
10011 if (code == OMP_ATOMIC_CAPTURE_NEW
10012 && !structured_block
10013 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
10014 code = OMP_ATOMIC_CAPTURE_OLD;
10015 break;
10018 /* FALLTHRU */
10019 default:
10020 switch (c_parser_peek_token (parser)->type)
10022 case CPP_MULT_EQ:
10023 opcode = MULT_EXPR;
10024 break;
10025 case CPP_DIV_EQ:
10026 opcode = TRUNC_DIV_EXPR;
10027 break;
10028 case CPP_PLUS_EQ:
10029 opcode = PLUS_EXPR;
10030 break;
10031 case CPP_MINUS_EQ:
10032 opcode = MINUS_EXPR;
10033 break;
10034 case CPP_LSHIFT_EQ:
10035 opcode = LSHIFT_EXPR;
10036 break;
10037 case CPP_RSHIFT_EQ:
10038 opcode = RSHIFT_EXPR;
10039 break;
10040 case CPP_AND_EQ:
10041 opcode = BIT_AND_EXPR;
10042 break;
10043 case CPP_OR_EQ:
10044 opcode = BIT_IOR_EXPR;
10045 break;
10046 case CPP_XOR_EQ:
10047 opcode = BIT_XOR_EXPR;
10048 break;
10049 case CPP_EQ:
10050 if (structured_block || code == OMP_ATOMIC)
10052 location_t aloc = c_parser_peek_token (parser)->location;
10053 location_t rhs_loc;
10054 enum c_parser_prec oprec = PREC_NONE;
10056 c_parser_consume_token (parser);
10057 rhs1 = c_parser_unary_expression (parser).value;
10058 rhs1 = c_fully_fold (rhs1, false, NULL);
10059 if (rhs1 == error_mark_node)
10060 goto saw_error;
10061 switch (c_parser_peek_token (parser)->type)
10063 case CPP_SEMICOLON:
10064 if (code == OMP_ATOMIC_CAPTURE_NEW)
10066 code = OMP_ATOMIC_CAPTURE_OLD;
10067 v = lhs;
10068 lhs = NULL_TREE;
10069 lhs1 = rhs1;
10070 rhs1 = NULL_TREE;
10071 c_parser_consume_token (parser);
10072 goto restart;
10074 c_parser_error (parser,
10075 "invalid form of %<#pragma omp atomic%>");
10076 goto saw_error;
10077 case CPP_MULT:
10078 opcode = MULT_EXPR;
10079 oprec = PREC_MULT;
10080 break;
10081 case CPP_DIV:
10082 opcode = TRUNC_DIV_EXPR;
10083 oprec = PREC_MULT;
10084 break;
10085 case CPP_PLUS:
10086 opcode = PLUS_EXPR;
10087 oprec = PREC_ADD;
10088 break;
10089 case CPP_MINUS:
10090 opcode = MINUS_EXPR;
10091 oprec = PREC_ADD;
10092 break;
10093 case CPP_LSHIFT:
10094 opcode = LSHIFT_EXPR;
10095 oprec = PREC_SHIFT;
10096 break;
10097 case CPP_RSHIFT:
10098 opcode = RSHIFT_EXPR;
10099 oprec = PREC_SHIFT;
10100 break;
10101 case CPP_AND:
10102 opcode = BIT_AND_EXPR;
10103 oprec = PREC_BITAND;
10104 break;
10105 case CPP_OR:
10106 opcode = BIT_IOR_EXPR;
10107 oprec = PREC_BITOR;
10108 break;
10109 case CPP_XOR:
10110 opcode = BIT_XOR_EXPR;
10111 oprec = PREC_BITXOR;
10112 break;
10113 default:
10114 c_parser_error (parser,
10115 "invalid operator for %<#pragma omp atomic%>");
10116 goto saw_error;
10118 loc = aloc;
10119 c_parser_consume_token (parser);
10120 rhs_loc = c_parser_peek_token (parser)->location;
10121 if (commutative_tree_code (opcode))
10122 oprec = (enum c_parser_prec) (oprec - 1);
10123 rhs_expr = c_parser_binary_expression (parser, NULL, oprec);
10124 rhs_expr = default_function_array_read_conversion (rhs_loc,
10125 rhs_expr);
10126 rhs = rhs_expr.value;
10127 rhs = c_fully_fold (rhs, false, NULL);
10128 goto stmt_done;
10130 /* FALLTHROUGH */
10131 default:
10132 c_parser_error (parser,
10133 "invalid operator for %<#pragma omp atomic%>");
10134 goto saw_error;
10137 /* Arrange to pass the location of the assignment operator to
10138 c_finish_omp_atomic. */
10139 loc = c_parser_peek_token (parser)->location;
10140 c_parser_consume_token (parser);
10142 location_t rhs_loc = c_parser_peek_token (parser)->location;
10143 rhs_expr = c_parser_expression (parser);
10144 rhs_expr = default_function_array_read_conversion (rhs_loc, rhs_expr);
10146 rhs = rhs_expr.value;
10147 rhs = c_fully_fold (rhs, false, NULL);
10148 break;
10150 stmt_done:
10151 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
10153 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
10154 goto saw_error;
10155 v = c_parser_unary_expression (parser).value;
10156 v = c_fully_fold (v, false, NULL);
10157 if (v == error_mark_node)
10158 goto saw_error;
10159 if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
10160 goto saw_error;
10161 lhs1 = c_parser_unary_expression (parser).value;
10162 lhs1 = c_fully_fold (lhs1, false, NULL);
10163 if (lhs1 == error_mark_node)
10164 goto saw_error;
10166 if (structured_block)
10168 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
10169 c_parser_require (parser, CPP_CLOSE_BRACE, "expected %<}%>");
10171 done:
10172 stmt = c_finish_omp_atomic (loc, code, opcode, lhs, rhs, v, lhs1, rhs1);
10173 if (stmt != error_mark_node)
10174 add_stmt (stmt);
10176 if (!structured_block)
10177 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
10181 /* OpenMP 2.5:
10182 # pragma omp barrier new-line
10185 static void
10186 c_parser_omp_barrier (c_parser *parser)
10188 location_t loc = c_parser_peek_token (parser)->location;
10189 c_parser_consume_pragma (parser);
10190 c_parser_skip_to_pragma_eol (parser);
10192 c_finish_omp_barrier (loc);
10195 /* OpenMP 2.5:
10196 # pragma omp critical [(name)] new-line
10197 structured-block
10199 LOC is the location of the #pragma itself. */
10201 static tree
10202 c_parser_omp_critical (location_t loc, c_parser *parser)
10204 tree stmt, name = NULL;
10206 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
10208 c_parser_consume_token (parser);
10209 if (c_parser_next_token_is (parser, CPP_NAME))
10211 name = c_parser_peek_token (parser)->value;
10212 c_parser_consume_token (parser);
10213 c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10215 else
10216 c_parser_error (parser, "expected identifier");
10218 else if (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
10219 c_parser_error (parser, "expected %<(%> or end of line");
10220 c_parser_skip_to_pragma_eol (parser);
10222 stmt = c_parser_omp_structured_block (parser);
10223 return c_finish_omp_critical (loc, stmt, name);
10226 /* OpenMP 2.5:
10227 # pragma omp flush flush-vars[opt] new-line
10229 flush-vars:
10230 ( variable-list ) */
10232 static void
10233 c_parser_omp_flush (c_parser *parser)
10235 location_t loc = c_parser_peek_token (parser)->location;
10236 c_parser_consume_pragma (parser);
10237 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
10238 c_parser_omp_var_list_parens (parser, OMP_CLAUSE_ERROR, NULL);
10239 else if (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
10240 c_parser_error (parser, "expected %<(%> or end of line");
10241 c_parser_skip_to_pragma_eol (parser);
10243 c_finish_omp_flush (loc);
10246 /* Parse the restricted form of the for statement allowed by OpenMP.
10247 The real trick here is to determine the loop control variable early
10248 so that we can push a new decl if necessary to make it private.
10249 LOC is the location of the OMP in "#pragma omp". */
10251 static tree
10252 c_parser_omp_for_loop (location_t loc,
10253 c_parser *parser, tree clauses, tree *par_clauses)
10255 tree decl, cond, incr, save_break, save_cont, body, init, stmt, cl;
10256 tree declv, condv, incrv, initv, ret = NULL;
10257 bool fail = false, open_brace_parsed = false;
10258 int i, collapse = 1, nbraces = 0;
10259 location_t for_loc;
10260 vec<tree, va_gc> *for_block = make_tree_vector ();
10262 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
10263 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
10264 collapse = tree_low_cst (OMP_CLAUSE_COLLAPSE_EXPR (cl), 0);
10266 gcc_assert (collapse >= 1);
10268 declv = make_tree_vec (collapse);
10269 initv = make_tree_vec (collapse);
10270 condv = make_tree_vec (collapse);
10271 incrv = make_tree_vec (collapse);
10273 if (!c_parser_next_token_is_keyword (parser, RID_FOR))
10275 c_parser_error (parser, "for statement expected");
10276 return NULL;
10278 for_loc = c_parser_peek_token (parser)->location;
10279 c_parser_consume_token (parser);
10281 for (i = 0; i < collapse; i++)
10283 int bracecount = 0;
10285 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10286 goto pop_scopes;
10288 /* Parse the initialization declaration or expression. */
10289 if (c_parser_next_tokens_start_declaration (parser))
10291 if (i > 0)
10292 vec_safe_push (for_block, c_begin_compound_stmt (true));
10293 c_parser_declaration_or_fndef (parser, true, true, true, true, true, NULL);
10294 decl = check_for_loop_decls (for_loc, flag_isoc99);
10295 if (decl == NULL)
10296 goto error_init;
10297 if (DECL_INITIAL (decl) == error_mark_node)
10298 decl = error_mark_node;
10299 init = decl;
10301 else if (c_parser_next_token_is (parser, CPP_NAME)
10302 && c_parser_peek_2nd_token (parser)->type == CPP_EQ)
10304 struct c_expr decl_exp;
10305 struct c_expr init_exp;
10306 location_t init_loc;
10308 decl_exp = c_parser_postfix_expression (parser);
10309 decl = decl_exp.value;
10311 c_parser_require (parser, CPP_EQ, "expected %<=%>");
10313 init_loc = c_parser_peek_token (parser)->location;
10314 init_exp = c_parser_expr_no_commas (parser, NULL);
10315 init_exp = default_function_array_read_conversion (init_loc,
10316 init_exp);
10317 init = build_modify_expr (init_loc, decl, decl_exp.original_type,
10318 NOP_EXPR, init_loc, init_exp.value,
10319 init_exp.original_type);
10320 init = c_process_expr_stmt (init_loc, init);
10322 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
10324 else
10326 error_init:
10327 c_parser_error (parser,
10328 "expected iteration declaration or initialization");
10329 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
10330 "expected %<)%>");
10331 fail = true;
10332 goto parse_next;
10335 /* Parse the loop condition. */
10336 cond = NULL_TREE;
10337 if (c_parser_next_token_is_not (parser, CPP_SEMICOLON))
10339 location_t cond_loc = c_parser_peek_token (parser)->location;
10340 struct c_expr cond_expr = c_parser_binary_expression (parser, NULL,
10341 PREC_NONE);
10343 cond = cond_expr.value;
10344 cond = c_objc_common_truthvalue_conversion (cond_loc, cond);
10345 cond = c_fully_fold (cond, false, NULL);
10346 switch (cond_expr.original_code)
10348 case GT_EXPR:
10349 case GE_EXPR:
10350 case LT_EXPR:
10351 case LE_EXPR:
10352 break;
10353 default:
10354 /* Can't be cond = error_mark_node, because we want to preserve
10355 the location until c_finish_omp_for. */
10356 cond = build1 (NOP_EXPR, boolean_type_node, error_mark_node);
10357 break;
10359 protected_set_expr_location (cond, cond_loc);
10361 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
10363 /* Parse the increment expression. */
10364 incr = NULL_TREE;
10365 if (c_parser_next_token_is_not (parser, CPP_CLOSE_PAREN))
10367 location_t incr_loc = c_parser_peek_token (parser)->location;
10369 incr = c_process_expr_stmt (incr_loc,
10370 c_parser_expression (parser).value);
10372 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10374 if (decl == NULL || decl == error_mark_node || init == error_mark_node)
10375 fail = true;
10376 else
10378 TREE_VEC_ELT (declv, i) = decl;
10379 TREE_VEC_ELT (initv, i) = init;
10380 TREE_VEC_ELT (condv, i) = cond;
10381 TREE_VEC_ELT (incrv, i) = incr;
10384 parse_next:
10385 if (i == collapse - 1)
10386 break;
10388 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
10389 in between the collapsed for loops to be still considered perfectly
10390 nested. Hopefully the final version clarifies this.
10391 For now handle (multiple) {'s and empty statements. */
10394 if (c_parser_next_token_is_keyword (parser, RID_FOR))
10396 c_parser_consume_token (parser);
10397 break;
10399 else if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
10401 c_parser_consume_token (parser);
10402 bracecount++;
10404 else if (bracecount
10405 && c_parser_next_token_is (parser, CPP_SEMICOLON))
10406 c_parser_consume_token (parser);
10407 else
10409 c_parser_error (parser, "not enough perfectly nested loops");
10410 if (bracecount)
10412 open_brace_parsed = true;
10413 bracecount--;
10415 fail = true;
10416 collapse = 0;
10417 break;
10420 while (1);
10422 nbraces += bracecount;
10425 save_break = c_break_label;
10426 c_break_label = size_one_node;
10427 save_cont = c_cont_label;
10428 c_cont_label = NULL_TREE;
10429 body = push_stmt_list ();
10431 if (open_brace_parsed)
10433 location_t here = c_parser_peek_token (parser)->location;
10434 stmt = c_begin_compound_stmt (true);
10435 c_parser_compound_statement_nostart (parser);
10436 add_stmt (c_end_compound_stmt (here, stmt, true));
10438 else
10439 add_stmt (c_parser_c99_block_statement (parser));
10440 if (c_cont_label)
10442 tree t = build1 (LABEL_EXPR, void_type_node, c_cont_label);
10443 SET_EXPR_LOCATION (t, loc);
10444 add_stmt (t);
10447 body = pop_stmt_list (body);
10448 c_break_label = save_break;
10449 c_cont_label = save_cont;
10451 while (nbraces)
10453 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
10455 c_parser_consume_token (parser);
10456 nbraces--;
10458 else if (c_parser_next_token_is (parser, CPP_SEMICOLON))
10459 c_parser_consume_token (parser);
10460 else
10462 c_parser_error (parser, "collapsed loops not perfectly nested");
10463 while (nbraces)
10465 location_t here = c_parser_peek_token (parser)->location;
10466 stmt = c_begin_compound_stmt (true);
10467 add_stmt (body);
10468 c_parser_compound_statement_nostart (parser);
10469 body = c_end_compound_stmt (here, stmt, true);
10470 nbraces--;
10472 goto pop_scopes;
10476 /* Only bother calling c_finish_omp_for if we haven't already generated
10477 an error from the initialization parsing. */
10478 if (!fail)
10480 stmt = c_finish_omp_for (loc, declv, initv, condv, incrv, body, NULL);
10481 if (stmt)
10483 if (par_clauses != NULL)
10485 tree *c;
10486 for (c = par_clauses; *c ; )
10487 if (OMP_CLAUSE_CODE (*c) != OMP_CLAUSE_FIRSTPRIVATE
10488 && OMP_CLAUSE_CODE (*c) != OMP_CLAUSE_LASTPRIVATE)
10489 c = &OMP_CLAUSE_CHAIN (*c);
10490 else
10492 for (i = 0; i < collapse; i++)
10493 if (TREE_VEC_ELT (declv, i) == OMP_CLAUSE_DECL (*c))
10494 break;
10495 if (i == collapse)
10496 c = &OMP_CLAUSE_CHAIN (*c);
10497 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE)
10499 error_at (loc,
10500 "iteration variable %qD should not be firstprivate",
10501 OMP_CLAUSE_DECL (*c));
10502 *c = OMP_CLAUSE_CHAIN (*c);
10504 else
10506 /* Copy lastprivate (decl) clause to OMP_FOR_CLAUSES,
10507 change it to shared (decl) in
10508 OMP_PARALLEL_CLAUSES. */
10509 tree l = build_omp_clause (OMP_CLAUSE_LOCATION (*c),
10510 OMP_CLAUSE_LASTPRIVATE);
10511 OMP_CLAUSE_DECL (l) = OMP_CLAUSE_DECL (*c);
10512 OMP_CLAUSE_CHAIN (l) = clauses;
10513 clauses = l;
10514 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
10518 OMP_FOR_CLAUSES (stmt) = clauses;
10520 ret = stmt;
10522 pop_scopes:
10523 while (!for_block->is_empty ())
10525 /* FIXME diagnostics: LOC below should be the actual location of
10526 this particular for block. We need to build a list of
10527 locations to go along with FOR_BLOCK. */
10528 stmt = c_end_compound_stmt (loc, for_block->pop (), true);
10529 add_stmt (stmt);
10531 release_tree_vector (for_block);
10532 return ret;
10535 /* OpenMP 2.5:
10536 #pragma omp for for-clause[optseq] new-line
10537 for-loop
10539 LOC is the location of the #pragma token.
10542 #define OMP_FOR_CLAUSE_MASK \
10543 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
10544 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
10545 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
10546 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
10547 | (1u << PRAGMA_OMP_CLAUSE_ORDERED) \
10548 | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE) \
10549 | (1u << PRAGMA_OMP_CLAUSE_COLLAPSE) \
10550 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
10552 static tree
10553 c_parser_omp_for (location_t loc, c_parser *parser)
10555 tree block, clauses, ret;
10557 clauses = c_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
10558 "#pragma omp for");
10560 block = c_begin_compound_stmt (true);
10561 ret = c_parser_omp_for_loop (loc, parser, clauses, NULL);
10562 block = c_end_compound_stmt (loc, block, true);
10563 add_stmt (block);
10565 return ret;
10568 /* OpenMP 2.5:
10569 # pragma omp master new-line
10570 structured-block
10572 LOC is the location of the #pragma token.
10575 static tree
10576 c_parser_omp_master (location_t loc, c_parser *parser)
10578 c_parser_skip_to_pragma_eol (parser);
10579 return c_finish_omp_master (loc, c_parser_omp_structured_block (parser));
10582 /* OpenMP 2.5:
10583 # pragma omp ordered new-line
10584 structured-block
10586 LOC is the location of the #pragma itself.
10589 static tree
10590 c_parser_omp_ordered (location_t loc, c_parser *parser)
10592 c_parser_skip_to_pragma_eol (parser);
10593 return c_finish_omp_ordered (loc, c_parser_omp_structured_block (parser));
10596 /* OpenMP 2.5:
10598 section-scope:
10599 { section-sequence }
10601 section-sequence:
10602 section-directive[opt] structured-block
10603 section-sequence section-directive structured-block
10605 SECTIONS_LOC is the location of the #pragma omp sections. */
10607 static tree
10608 c_parser_omp_sections_scope (location_t sections_loc, c_parser *parser)
10610 tree stmt, substmt;
10611 bool error_suppress = false;
10612 location_t loc;
10614 loc = c_parser_peek_token (parser)->location;
10615 if (!c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
10617 /* Avoid skipping until the end of the block. */
10618 parser->error = false;
10619 return NULL_TREE;
10622 stmt = push_stmt_list ();
10624 if (c_parser_peek_token (parser)->pragma_kind != PRAGMA_OMP_SECTION)
10626 substmt = push_stmt_list ();
10628 while (1)
10630 c_parser_statement (parser);
10632 if (c_parser_peek_token (parser)->pragma_kind == PRAGMA_OMP_SECTION)
10633 break;
10634 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
10635 break;
10636 if (c_parser_next_token_is (parser, CPP_EOF))
10637 break;
10640 substmt = pop_stmt_list (substmt);
10641 substmt = build1 (OMP_SECTION, void_type_node, substmt);
10642 SET_EXPR_LOCATION (substmt, loc);
10643 add_stmt (substmt);
10646 while (1)
10648 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
10649 break;
10650 if (c_parser_next_token_is (parser, CPP_EOF))
10651 break;
10653 loc = c_parser_peek_token (parser)->location;
10654 if (c_parser_peek_token (parser)->pragma_kind == PRAGMA_OMP_SECTION)
10656 c_parser_consume_pragma (parser);
10657 c_parser_skip_to_pragma_eol (parser);
10658 error_suppress = false;
10660 else if (!error_suppress)
10662 error_at (loc, "expected %<#pragma omp section%> or %<}%>");
10663 error_suppress = true;
10666 substmt = c_parser_omp_structured_block (parser);
10667 substmt = build1 (OMP_SECTION, void_type_node, substmt);
10668 SET_EXPR_LOCATION (substmt, loc);
10669 add_stmt (substmt);
10671 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE,
10672 "expected %<#pragma omp section%> or %<}%>");
10674 substmt = pop_stmt_list (stmt);
10676 stmt = make_node (OMP_SECTIONS);
10677 SET_EXPR_LOCATION (stmt, sections_loc);
10678 TREE_TYPE (stmt) = void_type_node;
10679 OMP_SECTIONS_BODY (stmt) = substmt;
10681 return add_stmt (stmt);
10684 /* OpenMP 2.5:
10685 # pragma omp sections sections-clause[optseq] newline
10686 sections-scope
10688 LOC is the location of the #pragma token.
10691 #define OMP_SECTIONS_CLAUSE_MASK \
10692 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
10693 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
10694 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
10695 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
10696 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
10698 static tree
10699 c_parser_omp_sections (location_t loc, c_parser *parser)
10701 tree block, clauses, ret;
10703 clauses = c_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
10704 "#pragma omp sections");
10706 block = c_begin_compound_stmt (true);
10707 ret = c_parser_omp_sections_scope (loc, parser);
10708 if (ret)
10709 OMP_SECTIONS_CLAUSES (ret) = clauses;
10710 block = c_end_compound_stmt (loc, block, true);
10711 add_stmt (block);
10713 return ret;
10716 /* OpenMP 2.5:
10717 # pragma parallel parallel-clause new-line
10718 # pragma parallel for parallel-for-clause new-line
10719 # pragma parallel sections parallel-sections-clause new-line
10721 LOC is the location of the #pragma token.
10724 #define OMP_PARALLEL_CLAUSE_MASK \
10725 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
10726 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
10727 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
10728 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
10729 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
10730 | (1u << PRAGMA_OMP_CLAUSE_COPYIN) \
10731 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
10732 | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
10734 static tree
10735 c_parser_omp_parallel (location_t loc, c_parser *parser)
10737 enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
10738 const char *p_name = "#pragma omp parallel";
10739 tree stmt, clauses, par_clause, ws_clause, block;
10740 unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
10742 if (c_parser_next_token_is_keyword (parser, RID_FOR))
10744 c_parser_consume_token (parser);
10745 p_kind = PRAGMA_OMP_PARALLEL_FOR;
10746 p_name = "#pragma omp parallel for";
10747 mask |= OMP_FOR_CLAUSE_MASK;
10748 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
10750 else if (c_parser_next_token_is (parser, CPP_NAME))
10752 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
10753 if (strcmp (p, "sections") == 0)
10755 c_parser_consume_token (parser);
10756 p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
10757 p_name = "#pragma omp parallel sections";
10758 mask |= OMP_SECTIONS_CLAUSE_MASK;
10759 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
10763 clauses = c_parser_omp_all_clauses (parser, mask, p_name);
10765 switch (p_kind)
10767 case PRAGMA_OMP_PARALLEL:
10768 block = c_begin_omp_parallel ();
10769 c_parser_statement (parser);
10770 stmt = c_finish_omp_parallel (loc, clauses, block);
10771 break;
10773 case PRAGMA_OMP_PARALLEL_FOR:
10774 block = c_begin_omp_parallel ();
10775 c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
10776 c_parser_omp_for_loop (loc, parser, ws_clause, &par_clause);
10777 stmt = c_finish_omp_parallel (loc, par_clause, block);
10778 OMP_PARALLEL_COMBINED (stmt) = 1;
10779 break;
10781 case PRAGMA_OMP_PARALLEL_SECTIONS:
10782 block = c_begin_omp_parallel ();
10783 c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
10784 stmt = c_parser_omp_sections_scope (loc, parser);
10785 if (stmt)
10786 OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
10787 stmt = c_finish_omp_parallel (loc, par_clause, block);
10788 OMP_PARALLEL_COMBINED (stmt) = 1;
10789 break;
10791 default:
10792 gcc_unreachable ();
10795 return stmt;
10798 /* OpenMP 2.5:
10799 # pragma omp single single-clause[optseq] new-line
10800 structured-block
10802 LOC is the location of the #pragma.
10805 #define OMP_SINGLE_CLAUSE_MASK \
10806 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
10807 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
10808 | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
10809 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
10811 static tree
10812 c_parser_omp_single (location_t loc, c_parser *parser)
10814 tree stmt = make_node (OMP_SINGLE);
10815 SET_EXPR_LOCATION (stmt, loc);
10816 TREE_TYPE (stmt) = void_type_node;
10818 OMP_SINGLE_CLAUSES (stmt)
10819 = c_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
10820 "#pragma omp single");
10821 OMP_SINGLE_BODY (stmt) = c_parser_omp_structured_block (parser);
10823 return add_stmt (stmt);
10826 /* OpenMP 3.0:
10827 # pragma omp task task-clause[optseq] new-line
10829 LOC is the location of the #pragma.
10832 #define OMP_TASK_CLAUSE_MASK \
10833 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
10834 | (1u << PRAGMA_OMP_CLAUSE_UNTIED) \
10835 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
10836 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
10837 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
10838 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
10839 | (1u << PRAGMA_OMP_CLAUSE_FINAL) \
10840 | (1u << PRAGMA_OMP_CLAUSE_MERGEABLE))
10842 static tree
10843 c_parser_omp_task (location_t loc, c_parser *parser)
10845 tree clauses, block;
10847 clauses = c_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
10848 "#pragma omp task");
10850 block = c_begin_omp_task ();
10851 c_parser_statement (parser);
10852 return c_finish_omp_task (loc, clauses, block);
10855 /* OpenMP 3.0:
10856 # pragma omp taskwait new-line
10859 static void
10860 c_parser_omp_taskwait (c_parser *parser)
10862 location_t loc = c_parser_peek_token (parser)->location;
10863 c_parser_consume_pragma (parser);
10864 c_parser_skip_to_pragma_eol (parser);
10866 c_finish_omp_taskwait (loc);
10869 /* OpenMP 3.1:
10870 # pragma omp taskyield new-line
10873 static void
10874 c_parser_omp_taskyield (c_parser *parser)
10876 location_t loc = c_parser_peek_token (parser)->location;
10877 c_parser_consume_pragma (parser);
10878 c_parser_skip_to_pragma_eol (parser);
10880 c_finish_omp_taskyield (loc);
10883 /* Main entry point to parsing most OpenMP pragmas. */
10885 static void
10886 c_parser_omp_construct (c_parser *parser)
10888 enum pragma_kind p_kind;
10889 location_t loc;
10890 tree stmt;
10892 loc = c_parser_peek_token (parser)->location;
10893 p_kind = c_parser_peek_token (parser)->pragma_kind;
10894 c_parser_consume_pragma (parser);
10896 switch (p_kind)
10898 case PRAGMA_OMP_ATOMIC:
10899 c_parser_omp_atomic (loc, parser);
10900 return;
10901 case PRAGMA_OMP_CRITICAL:
10902 stmt = c_parser_omp_critical (loc, parser);
10903 break;
10904 case PRAGMA_OMP_FOR:
10905 stmt = c_parser_omp_for (loc, parser);
10906 break;
10907 case PRAGMA_OMP_MASTER:
10908 stmt = c_parser_omp_master (loc, parser);
10909 break;
10910 case PRAGMA_OMP_ORDERED:
10911 stmt = c_parser_omp_ordered (loc, parser);
10912 break;
10913 case PRAGMA_OMP_PARALLEL:
10914 stmt = c_parser_omp_parallel (loc, parser);
10915 break;
10916 case PRAGMA_OMP_SECTIONS:
10917 stmt = c_parser_omp_sections (loc, parser);
10918 break;
10919 case PRAGMA_OMP_SINGLE:
10920 stmt = c_parser_omp_single (loc, parser);
10921 break;
10922 case PRAGMA_OMP_TASK:
10923 stmt = c_parser_omp_task (loc, parser);
10924 break;
10925 default:
10926 gcc_unreachable ();
10929 if (stmt)
10930 gcc_assert (EXPR_LOCATION (stmt) != UNKNOWN_LOCATION);
10934 /* OpenMP 2.5:
10935 # pragma omp threadprivate (variable-list) */
10937 static void
10938 c_parser_omp_threadprivate (c_parser *parser)
10940 tree vars, t;
10941 location_t loc;
10943 c_parser_consume_pragma (parser);
10944 loc = c_parser_peek_token (parser)->location;
10945 vars = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_ERROR, NULL);
10947 /* Mark every variable in VARS to be assigned thread local storage. */
10948 for (t = vars; t; t = TREE_CHAIN (t))
10950 tree v = TREE_PURPOSE (t);
10952 /* FIXME diagnostics: Ideally we should keep individual
10953 locations for all the variables in the var list to make the
10954 following errors more precise. Perhaps
10955 c_parser_omp_var_list_parens() should construct a list of
10956 locations to go along with the var list. */
10958 /* If V had already been marked threadprivate, it doesn't matter
10959 whether it had been used prior to this point. */
10960 if (TREE_CODE (v) != VAR_DECL)
10961 error_at (loc, "%qD is not a variable", v);
10962 else if (TREE_USED (v) && !C_DECL_THREADPRIVATE_P (v))
10963 error_at (loc, "%qE declared %<threadprivate%> after first use", v);
10964 else if (! TREE_STATIC (v) && ! DECL_EXTERNAL (v))
10965 error_at (loc, "automatic variable %qE cannot be %<threadprivate%>", v);
10966 else if (TREE_TYPE (v) == error_mark_node)
10968 else if (! COMPLETE_TYPE_P (TREE_TYPE (v)))
10969 error_at (loc, "%<threadprivate%> %qE has incomplete type", v);
10970 else
10972 if (! DECL_THREAD_LOCAL_P (v))
10974 DECL_TLS_MODEL (v) = decl_default_tls_model (v);
10975 /* If rtl has been already set for this var, call
10976 make_decl_rtl once again, so that encode_section_info
10977 has a chance to look at the new decl flags. */
10978 if (DECL_RTL_SET_P (v))
10979 make_decl_rtl (v);
10981 C_DECL_THREADPRIVATE_P (v) = 1;
10985 c_parser_skip_to_pragma_eol (parser);
10988 /* Parse a transaction attribute (GCC Extension).
10990 transaction-attribute:
10991 attributes
10992 [ [ any-word ] ]
10994 The transactional memory language description is written for C++,
10995 and uses the C++0x attribute syntax. For compatibility, allow the
10996 bracket style for transactions in C as well. */
10998 static tree
10999 c_parser_transaction_attributes (c_parser *parser)
11001 tree attr_name, attr = NULL;
11003 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
11004 return c_parser_attributes (parser);
11006 if (!c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
11007 return NULL_TREE;
11008 c_parser_consume_token (parser);
11009 if (!c_parser_require (parser, CPP_OPEN_SQUARE, "expected %<[%>"))
11010 goto error1;
11012 attr_name = c_parser_attribute_any_word (parser);
11013 if (attr_name)
11015 c_parser_consume_token (parser);
11016 attr = build_tree_list (attr_name, NULL_TREE);
11018 else
11019 c_parser_error (parser, "expected identifier");
11021 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, "expected %<]%>");
11022 error1:
11023 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, "expected %<]%>");
11024 return attr;
11027 /* Parse a __transaction_atomic or __transaction_relaxed statement
11028 (GCC Extension).
11030 transaction-statement:
11031 __transaction_atomic transaction-attribute[opt] compound-statement
11032 __transaction_relaxed compound-statement
11034 Note that the only valid attribute is: "outer".
11037 static tree
11038 c_parser_transaction (c_parser *parser, enum rid keyword)
11040 unsigned int old_in = parser->in_transaction;
11041 unsigned int this_in = 1, new_in;
11042 location_t loc = c_parser_peek_token (parser)->location;
11043 tree stmt, attrs;
11045 gcc_assert ((keyword == RID_TRANSACTION_ATOMIC
11046 || keyword == RID_TRANSACTION_RELAXED)
11047 && c_parser_next_token_is_keyword (parser, keyword));
11048 c_parser_consume_token (parser);
11050 if (keyword == RID_TRANSACTION_RELAXED)
11051 this_in |= TM_STMT_ATTR_RELAXED;
11052 else
11054 attrs = c_parser_transaction_attributes (parser);
11055 if (attrs)
11056 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
11059 /* Keep track if we're in the lexical scope of an outer transaction. */
11060 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
11062 parser->in_transaction = new_in;
11063 stmt = c_parser_compound_statement (parser);
11064 parser->in_transaction = old_in;
11066 if (flag_tm)
11067 stmt = c_finish_transaction (loc, stmt, this_in);
11068 else
11069 error_at (loc, (keyword == RID_TRANSACTION_ATOMIC ?
11070 "%<__transaction_atomic%> without transactional memory support enabled"
11071 : "%<__transaction_relaxed %> "
11072 "without transactional memory support enabled"));
11074 return stmt;
11077 /* Parse a __transaction_atomic or __transaction_relaxed expression
11078 (GCC Extension).
11080 transaction-expression:
11081 __transaction_atomic ( expression )
11082 __transaction_relaxed ( expression )
11085 static struct c_expr
11086 c_parser_transaction_expression (c_parser *parser, enum rid keyword)
11088 struct c_expr ret;
11089 unsigned int old_in = parser->in_transaction;
11090 unsigned int this_in = 1;
11091 location_t loc = c_parser_peek_token (parser)->location;
11092 tree attrs;
11094 gcc_assert ((keyword == RID_TRANSACTION_ATOMIC
11095 || keyword == RID_TRANSACTION_RELAXED)
11096 && c_parser_next_token_is_keyword (parser, keyword));
11097 c_parser_consume_token (parser);
11099 if (keyword == RID_TRANSACTION_RELAXED)
11100 this_in |= TM_STMT_ATTR_RELAXED;
11101 else
11103 attrs = c_parser_transaction_attributes (parser);
11104 if (attrs)
11105 this_in |= parse_tm_stmt_attr (attrs, 0);
11108 parser->in_transaction = this_in;
11109 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11111 tree expr = c_parser_expression (parser).value;
11112 ret.original_type = TREE_TYPE (expr);
11113 ret.value = build1 (TRANSACTION_EXPR, ret.original_type, expr);
11114 if (this_in & TM_STMT_ATTR_RELAXED)
11115 TRANSACTION_EXPR_RELAXED (ret.value) = 1;
11116 SET_EXPR_LOCATION (ret.value, loc);
11117 ret.original_code = TRANSACTION_EXPR;
11118 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
11120 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
11121 goto error;
11124 else
11126 error:
11127 ret.value = error_mark_node;
11128 ret.original_code = ERROR_MARK;
11129 ret.original_type = NULL;
11131 parser->in_transaction = old_in;
11133 if (!flag_tm)
11134 error_at (loc, (keyword == RID_TRANSACTION_ATOMIC ?
11135 "%<__transaction_atomic%> without transactional memory support enabled"
11136 : "%<__transaction_relaxed %> "
11137 "without transactional memory support enabled"));
11139 return ret;
11142 /* Parse a __transaction_cancel statement (GCC Extension).
11144 transaction-cancel-statement:
11145 __transaction_cancel transaction-attribute[opt] ;
11147 Note that the only valid attribute is "outer".
11150 static tree
11151 c_parser_transaction_cancel(c_parser *parser)
11153 location_t loc = c_parser_peek_token (parser)->location;
11154 tree attrs;
11155 bool is_outer = false;
11157 gcc_assert (c_parser_next_token_is_keyword (parser, RID_TRANSACTION_CANCEL));
11158 c_parser_consume_token (parser);
11160 attrs = c_parser_transaction_attributes (parser);
11161 if (attrs)
11162 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
11164 if (!flag_tm)
11166 error_at (loc, "%<__transaction_cancel%> without "
11167 "transactional memory support enabled");
11168 goto ret_error;
11170 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
11172 error_at (loc, "%<__transaction_cancel%> within a "
11173 "%<__transaction_relaxed%>");
11174 goto ret_error;
11176 else if (is_outer)
11178 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
11179 && !is_tm_may_cancel_outer (current_function_decl))
11181 error_at (loc, "outer %<__transaction_cancel%> not "
11182 "within outer %<__transaction_atomic%>");
11183 error_at (loc, " or a %<transaction_may_cancel_outer%> function");
11184 goto ret_error;
11187 else if (parser->in_transaction == 0)
11189 error_at (loc, "%<__transaction_cancel%> not within "
11190 "%<__transaction_atomic%>");
11191 goto ret_error;
11194 return add_stmt (build_tm_abort_call (loc, is_outer));
11196 ret_error:
11197 return build1 (NOP_EXPR, void_type_node, error_mark_node);
11200 /* Parse a single source file. */
11202 void
11203 c_parse_file (void)
11205 /* Use local storage to begin. If the first token is a pragma, parse it.
11206 If it is #pragma GCC pch_preprocess, then this will load a PCH file
11207 which will cause garbage collection. */
11208 c_parser tparser;
11210 memset (&tparser, 0, sizeof tparser);
11211 the_parser = &tparser;
11213 if (c_parser_peek_token (&tparser)->pragma_kind == PRAGMA_GCC_PCH_PREPROCESS)
11214 c_parser_pragma_pch_preprocess (&tparser);
11216 the_parser = ggc_alloc_c_parser ();
11217 *the_parser = tparser;
11219 /* Initialize EH, if we've been told to do so. */
11220 if (flag_exceptions)
11221 using_eh_for_cleanups ();
11223 c_parser_translation_unit (the_parser);
11224 the_parser = NULL;
11227 /* This function parses Cilk Plus array notation. The starting index is
11228 passed in INITIAL_INDEX and the array name is passes in ARRAY_VALUE. The
11229 return value of this function is a tree_node called VALUE_TREE of type
11230 ARRAY_NOTATION_REF. */
11232 static tree
11233 c_parser_array_notation (location_t loc, c_parser *parser, tree initial_index,
11234 tree array_value)
11236 c_token *token = NULL;
11237 tree start_index = NULL_TREE, end_index = NULL_TREE, stride = NULL_TREE;
11238 tree value_tree = NULL_TREE, type = NULL_TREE, array_type = NULL_TREE;
11239 tree array_type_domain = NULL_TREE;
11241 if (array_value == error_mark_node)
11243 /* No need to continue. If either of these 2 were true, then an error
11244 must be emitted already. Thus, no need to emit them twice. */
11245 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
11246 return error_mark_node;
11249 array_type = TREE_TYPE (array_value);
11250 gcc_assert (array_type);
11251 type = TREE_TYPE (array_type);
11252 token = c_parser_peek_token (parser);
11254 if (token->type == CPP_EOF)
11256 c_parser_error (parser, "expected %<:%> or numeral");
11257 return value_tree;
11259 else if (token->type == CPP_COLON)
11261 if (!initial_index)
11263 /* If we are here, then we have a case like this A[:]. */
11264 c_parser_consume_token (parser);
11265 if (TREE_CODE (array_type) == POINTER_TYPE)
11267 error_at (loc, "start-index and length fields necessary for "
11268 "using array notations in pointers");
11269 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
11270 return error_mark_node;
11272 if (TREE_CODE (array_type) == FUNCTION_TYPE)
11274 error_at (loc, "array notations cannot be used with function "
11275 "type");
11276 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
11277 return error_mark_node;
11279 array_type_domain = TYPE_DOMAIN (array_type);
11281 if (!array_type_domain)
11283 error_at (loc, "start-index and length fields necessary for "
11284 "using array notations in dimensionless arrays");
11285 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
11286 return error_mark_node;
11289 start_index = TYPE_MINVAL (array_type_domain);
11290 start_index = fold_build1 (CONVERT_EXPR, ptrdiff_type_node,
11291 start_index);
11292 if (!TYPE_MAXVAL (array_type_domain)
11293 || !TREE_CONSTANT (TYPE_MAXVAL (array_type_domain)))
11295 error_at (loc, "start-index and length fields necessary for "
11296 "using array notations in variable-length arrays");
11297 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
11298 return error_mark_node;
11300 end_index = TYPE_MAXVAL (array_type_domain);
11301 end_index = fold_build2 (PLUS_EXPR, TREE_TYPE (end_index),
11302 end_index, integer_one_node);
11303 end_index = fold_build1 (CONVERT_EXPR, ptrdiff_type_node, end_index);
11304 stride = build_int_cst (integer_type_node, 1);
11305 stride = fold_build1 (CONVERT_EXPR, ptrdiff_type_node, stride);
11307 else if (initial_index != error_mark_node)
11309 /* If we are here, then there should be 2 possibilities:
11310 1. Array [EXPR : EXPR]
11311 2. Array [EXPR : EXPR : EXPR]
11313 start_index = initial_index;
11315 if (TREE_CODE (array_type) == FUNCTION_TYPE)
11317 error_at (loc, "array notations cannot be used with function "
11318 "type");
11319 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
11320 return error_mark_node;
11322 c_parser_consume_token (parser); /* consume the ':' */
11323 end_index = c_parser_expression (parser).value;
11324 if (!end_index || end_index == error_mark_node)
11326 c_parser_skip_to_end_of_block_or_statement (parser);
11327 return error_mark_node;
11329 if (c_parser_peek_token (parser)->type == CPP_COLON)
11331 c_parser_consume_token (parser);
11332 stride = c_parser_expression (parser).value;
11333 if (!stride || stride == error_mark_node)
11335 c_parser_skip_to_end_of_block_or_statement (parser);
11336 return error_mark_node;
11340 else
11341 c_parser_error (parser, "expected array notation expression");
11343 else
11344 c_parser_error (parser, "expected array notation expression");
11346 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, "expected %<]%>");
11348 value_tree = build_array_notation_ref (loc, array_value, start_index,
11349 end_index, stride, type);
11350 if (value_tree != error_mark_node)
11351 SET_EXPR_LOCATION (value_tree, loc);
11352 return value_tree;
11355 #include "gt-c-c-parser.h"