Fixed a bug in expansion of array notations in if-statement conditions.
[official-gcc.git] / gcc / c / c-parser.c
blobd6a500e72b37e41faa621e085c2f348556193efe
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;
6236 /* Parse a postfix expression (C90 6.3.1-6.3.2, C99 6.5.1-6.5.2).
6238 postfix-expression:
6239 primary-expression
6240 postfix-expression [ expression ]
6241 postfix-expression ( argument-expression-list[opt] )
6242 postfix-expression . identifier
6243 postfix-expression -> identifier
6244 postfix-expression ++
6245 postfix-expression --
6246 ( type-name ) { initializer-list }
6247 ( type-name ) { initializer-list , }
6249 argument-expression-list:
6250 argument-expression
6251 argument-expression-list , argument-expression
6253 primary-expression:
6254 identifier
6255 constant
6256 string-literal
6257 ( expression )
6259 GNU extensions:
6261 primary-expression:
6262 __func__
6263 (treated as a keyword in GNU C)
6264 __FUNCTION__
6265 __PRETTY_FUNCTION__
6266 ( compound-statement )
6267 __builtin_va_arg ( assignment-expression , type-name )
6268 __builtin_offsetof ( type-name , offsetof-member-designator )
6269 __builtin_choose_expr ( assignment-expression ,
6270 assignment-expression ,
6271 assignment-expression )
6272 __builtin_types_compatible_p ( type-name , type-name )
6273 __builtin_complex ( assignment-expression , assignment-expression )
6274 __builtin_shuffle ( assignment-expression , assignment-expression )
6275 __builtin_shuffle ( assignment-expression ,
6276 assignment-expression ,
6277 assignment-expression, )
6279 offsetof-member-designator:
6280 identifier
6281 offsetof-member-designator . identifier
6282 offsetof-member-designator [ expression ]
6284 Objective-C:
6286 primary-expression:
6287 [ objc-receiver objc-message-args ]
6288 @selector ( objc-selector-arg )
6289 @protocol ( identifier )
6290 @encode ( type-name )
6291 objc-string-literal
6292 Classname . identifier
6295 static struct c_expr
6296 c_parser_postfix_expression (c_parser *parser)
6298 struct c_expr expr, e1;
6299 struct c_type_name *t1, *t2;
6300 location_t loc = c_parser_peek_token (parser)->location;;
6301 expr.original_code = ERROR_MARK;
6302 expr.original_type = NULL;
6303 switch (c_parser_peek_token (parser)->type)
6305 case CPP_NUMBER:
6306 expr.value = c_parser_peek_token (parser)->value;
6307 loc = c_parser_peek_token (parser)->location;
6308 c_parser_consume_token (parser);
6309 if (TREE_CODE (expr.value) == FIXED_CST
6310 && !targetm.fixed_point_supported_p ())
6312 error_at (loc, "fixed-point types not supported for this target");
6313 expr.value = error_mark_node;
6315 break;
6316 case CPP_CHAR:
6317 case CPP_CHAR16:
6318 case CPP_CHAR32:
6319 case CPP_WCHAR:
6320 expr.value = c_parser_peek_token (parser)->value;
6321 c_parser_consume_token (parser);
6322 break;
6323 case CPP_STRING:
6324 case CPP_STRING16:
6325 case CPP_STRING32:
6326 case CPP_WSTRING:
6327 case CPP_UTF8STRING:
6328 expr.value = c_parser_peek_token (parser)->value;
6329 expr.original_code = STRING_CST;
6330 c_parser_consume_token (parser);
6331 break;
6332 case CPP_OBJC_STRING:
6333 gcc_assert (c_dialect_objc ());
6334 expr.value
6335 = objc_build_string_object (c_parser_peek_token (parser)->value);
6336 c_parser_consume_token (parser);
6337 break;
6338 case CPP_NAME:
6339 switch (c_parser_peek_token (parser)->id_kind)
6341 case C_ID_ID:
6343 tree id = c_parser_peek_token (parser)->value;
6344 c_parser_consume_token (parser);
6345 expr.value = build_external_ref (loc, id,
6346 (c_parser_peek_token (parser)->type
6347 == CPP_OPEN_PAREN),
6348 &expr.original_type);
6349 break;
6351 case C_ID_CLASSNAME:
6353 /* Here we parse the Objective-C 2.0 Class.name dot
6354 syntax. */
6355 tree class_name = c_parser_peek_token (parser)->value;
6356 tree component;
6357 c_parser_consume_token (parser);
6358 gcc_assert (c_dialect_objc ());
6359 if (!c_parser_require (parser, CPP_DOT, "expected %<.%>"))
6361 expr.value = error_mark_node;
6362 break;
6364 if (c_parser_next_token_is_not (parser, CPP_NAME))
6366 c_parser_error (parser, "expected identifier");
6367 expr.value = error_mark_node;
6368 break;
6370 component = c_parser_peek_token (parser)->value;
6371 c_parser_consume_token (parser);
6372 expr.value = objc_build_class_component_ref (class_name,
6373 component);
6374 break;
6376 default:
6377 c_parser_error (parser, "expected expression");
6378 expr.value = error_mark_node;
6379 break;
6381 break;
6382 case CPP_OPEN_PAREN:
6383 /* A parenthesized expression, statement expression or compound
6384 literal. */
6385 if (c_parser_peek_2nd_token (parser)->type == CPP_OPEN_BRACE)
6387 /* A statement expression. */
6388 tree stmt;
6389 location_t brace_loc;
6390 c_parser_consume_token (parser);
6391 brace_loc = c_parser_peek_token (parser)->location;
6392 c_parser_consume_token (parser);
6393 if (!building_stmt_list_p ())
6395 error_at (loc, "braced-group within expression allowed "
6396 "only inside a function");
6397 parser->error = true;
6398 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
6399 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6400 expr.value = error_mark_node;
6401 break;
6403 stmt = c_begin_stmt_expr ();
6404 c_parser_compound_statement_nostart (parser);
6405 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
6406 "expected %<)%>");
6407 pedwarn (loc, OPT_Wpedantic,
6408 "ISO C forbids braced-groups within expressions");
6409 expr.value = c_finish_stmt_expr (brace_loc, stmt);
6410 mark_exp_read (expr.value);
6412 else if (c_token_starts_typename (c_parser_peek_2nd_token (parser)))
6414 /* A compound literal. ??? Can we actually get here rather
6415 than going directly to
6416 c_parser_postfix_expression_after_paren_type from
6417 elsewhere? */
6418 location_t loc;
6419 struct c_type_name *type_name;
6420 c_parser_consume_token (parser);
6421 loc = c_parser_peek_token (parser)->location;
6422 type_name = c_parser_type_name (parser);
6423 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
6424 "expected %<)%>");
6425 if (type_name == NULL)
6427 expr.value = error_mark_node;
6429 else
6430 expr = c_parser_postfix_expression_after_paren_type (parser,
6431 type_name,
6432 loc);
6434 else
6436 /* A parenthesized expression. */
6437 c_parser_consume_token (parser);
6438 expr = c_parser_expression (parser);
6439 if (TREE_CODE (expr.value) == MODIFY_EXPR)
6440 TREE_NO_WARNING (expr.value) = 1;
6441 if (expr.original_code != C_MAYBE_CONST_EXPR)
6442 expr.original_code = ERROR_MARK;
6443 /* Don't change EXPR.ORIGINAL_TYPE. */
6444 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
6445 "expected %<)%>");
6447 break;
6448 case CPP_KEYWORD:
6449 switch (c_parser_peek_token (parser)->keyword)
6451 case RID_FUNCTION_NAME:
6452 case RID_PRETTY_FUNCTION_NAME:
6453 case RID_C99_FUNCTION_NAME:
6454 expr.value = fname_decl (loc,
6455 c_parser_peek_token (parser)->keyword,
6456 c_parser_peek_token (parser)->value);
6457 c_parser_consume_token (parser);
6458 break;
6459 case RID_VA_ARG:
6460 c_parser_consume_token (parser);
6461 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6463 expr.value = error_mark_node;
6464 break;
6466 e1 = c_parser_expr_no_commas (parser, NULL);
6467 mark_exp_read (e1.value);
6468 e1.value = c_fully_fold (e1.value, false, NULL);
6469 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
6471 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6472 expr.value = error_mark_node;
6473 break;
6475 loc = c_parser_peek_token (parser)->location;
6476 t1 = c_parser_type_name (parser);
6477 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
6478 "expected %<)%>");
6479 if (t1 == NULL)
6481 expr.value = error_mark_node;
6483 else
6485 tree type_expr = NULL_TREE;
6486 expr.value = c_build_va_arg (loc, e1.value,
6487 groktypename (t1, &type_expr, NULL));
6488 if (type_expr)
6490 expr.value = build2 (C_MAYBE_CONST_EXPR,
6491 TREE_TYPE (expr.value), type_expr,
6492 expr.value);
6493 C_MAYBE_CONST_EXPR_NON_CONST (expr.value) = true;
6496 break;
6497 case RID_OFFSETOF:
6498 c_parser_consume_token (parser);
6499 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6501 expr.value = error_mark_node;
6502 break;
6504 t1 = c_parser_type_name (parser);
6505 if (t1 == NULL)
6506 parser->error = true;
6507 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
6508 gcc_assert (parser->error);
6509 if (parser->error)
6511 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6512 expr.value = error_mark_node;
6513 break;
6517 tree type = groktypename (t1, NULL, NULL);
6518 tree offsetof_ref;
6519 if (type == error_mark_node)
6520 offsetof_ref = error_mark_node;
6521 else
6523 offsetof_ref = build1 (INDIRECT_REF, type, null_pointer_node);
6524 SET_EXPR_LOCATION (offsetof_ref, loc);
6526 /* Parse the second argument to __builtin_offsetof. We
6527 must have one identifier, and beyond that we want to
6528 accept sub structure and sub array references. */
6529 if (c_parser_next_token_is (parser, CPP_NAME))
6531 offsetof_ref = build_component_ref
6532 (loc, offsetof_ref, c_parser_peek_token (parser)->value);
6533 c_parser_consume_token (parser);
6534 while (c_parser_next_token_is (parser, CPP_DOT)
6535 || c_parser_next_token_is (parser,
6536 CPP_OPEN_SQUARE)
6537 || c_parser_next_token_is (parser,
6538 CPP_DEREF))
6540 if (c_parser_next_token_is (parser, CPP_DEREF))
6542 loc = c_parser_peek_token (parser)->location;
6543 offsetof_ref = build_array_ref (loc,
6544 offsetof_ref,
6545 integer_zero_node);
6546 goto do_dot;
6548 else if (c_parser_next_token_is (parser, CPP_DOT))
6550 do_dot:
6551 c_parser_consume_token (parser);
6552 if (c_parser_next_token_is_not (parser,
6553 CPP_NAME))
6555 c_parser_error (parser, "expected identifier");
6556 break;
6558 offsetof_ref = build_component_ref
6559 (loc, offsetof_ref,
6560 c_parser_peek_token (parser)->value);
6561 c_parser_consume_token (parser);
6563 else
6565 tree idx;
6566 loc = c_parser_peek_token (parser)->location;
6567 c_parser_consume_token (parser);
6568 idx = c_parser_expression (parser).value;
6569 idx = c_fully_fold (idx, false, NULL);
6570 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
6571 "expected %<]%>");
6572 offsetof_ref = build_array_ref (loc, offsetof_ref, idx);
6576 else
6577 c_parser_error (parser, "expected identifier");
6578 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
6579 "expected %<)%>");
6580 expr.value = fold_offsetof (offsetof_ref);
6582 break;
6583 case RID_CHOOSE_EXPR:
6585 vec<c_expr_t, va_gc> *cexpr_list;
6586 c_expr_t *e1_p, *e2_p, *e3_p;
6587 tree c;
6589 c_parser_consume_token (parser);
6590 if (!c_parser_get_builtin_args (parser,
6591 "__builtin_choose_expr",
6592 &cexpr_list, true))
6594 expr.value = error_mark_node;
6595 break;
6598 if (vec_safe_length (cexpr_list) != 3)
6600 error_at (loc, "wrong number of arguments to "
6601 "%<__builtin_choose_expr%>");
6602 expr.value = error_mark_node;
6603 break;
6606 e1_p = &(*cexpr_list)[0];
6607 e2_p = &(*cexpr_list)[1];
6608 e3_p = &(*cexpr_list)[2];
6610 c = e1_p->value;
6611 mark_exp_read (e2_p->value);
6612 mark_exp_read (e3_p->value);
6613 if (TREE_CODE (c) != INTEGER_CST
6614 || !INTEGRAL_TYPE_P (TREE_TYPE (c)))
6615 error_at (loc,
6616 "first argument to %<__builtin_choose_expr%> not"
6617 " a constant");
6618 constant_expression_warning (c);
6619 expr = integer_zerop (c) ? *e3_p : *e2_p;
6620 break;
6622 case RID_TYPES_COMPATIBLE_P:
6623 c_parser_consume_token (parser);
6624 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6626 expr.value = error_mark_node;
6627 break;
6629 t1 = c_parser_type_name (parser);
6630 if (t1 == NULL)
6632 expr.value = error_mark_node;
6633 break;
6635 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
6637 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6638 expr.value = error_mark_node;
6639 break;
6641 t2 = c_parser_type_name (parser);
6642 if (t2 == NULL)
6644 expr.value = error_mark_node;
6645 break;
6647 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
6648 "expected %<)%>");
6650 tree e1, e2;
6651 e1 = groktypename (t1, NULL, NULL);
6652 e2 = groktypename (t2, NULL, NULL);
6653 if (e1 == error_mark_node || e2 == error_mark_node)
6655 expr.value = error_mark_node;
6656 break;
6659 e1 = TYPE_MAIN_VARIANT (e1);
6660 e2 = TYPE_MAIN_VARIANT (e2);
6662 expr.value
6663 = comptypes (e1, e2) ? integer_one_node : integer_zero_node;
6665 break;
6666 case RID_BUILTIN_COMPLEX:
6668 vec<c_expr_t, va_gc> *cexpr_list;
6669 c_expr_t *e1_p, *e2_p;
6671 c_parser_consume_token (parser);
6672 if (!c_parser_get_builtin_args (parser,
6673 "__builtin_complex",
6674 &cexpr_list, false))
6676 expr.value = error_mark_node;
6677 break;
6680 if (vec_safe_length (cexpr_list) != 2)
6682 error_at (loc, "wrong number of arguments to "
6683 "%<__builtin_complex%>");
6684 expr.value = error_mark_node;
6685 break;
6688 e1_p = &(*cexpr_list)[0];
6689 e2_p = &(*cexpr_list)[1];
6691 mark_exp_read (e1_p->value);
6692 if (TREE_CODE (e1_p->value) == EXCESS_PRECISION_EXPR)
6693 e1_p->value = convert (TREE_TYPE (e1_p->value),
6694 TREE_OPERAND (e1_p->value, 0));
6695 mark_exp_read (e2_p->value);
6696 if (TREE_CODE (e2_p->value) == EXCESS_PRECISION_EXPR)
6697 e2_p->value = convert (TREE_TYPE (e2_p->value),
6698 TREE_OPERAND (e2_p->value, 0));
6699 if (!SCALAR_FLOAT_TYPE_P (TREE_TYPE (e1_p->value))
6700 || DECIMAL_FLOAT_TYPE_P (TREE_TYPE (e1_p->value))
6701 || !SCALAR_FLOAT_TYPE_P (TREE_TYPE (e2_p->value))
6702 || DECIMAL_FLOAT_TYPE_P (TREE_TYPE (e2_p->value)))
6704 error_at (loc, "%<__builtin_complex%> operand "
6705 "not of real binary floating-point type");
6706 expr.value = error_mark_node;
6707 break;
6709 if (TYPE_MAIN_VARIANT (TREE_TYPE (e1_p->value))
6710 != TYPE_MAIN_VARIANT (TREE_TYPE (e2_p->value)))
6712 error_at (loc,
6713 "%<__builtin_complex%> operands of different types");
6714 expr.value = error_mark_node;
6715 break;
6717 if (!flag_isoc99)
6718 pedwarn (loc, OPT_Wpedantic,
6719 "ISO C90 does not support complex types");
6720 expr.value = build2 (COMPLEX_EXPR,
6721 build_complex_type
6722 (TYPE_MAIN_VARIANT
6723 (TREE_TYPE (e1_p->value))),
6724 e1_p->value, e2_p->value);
6725 break;
6727 case RID_BUILTIN_SHUFFLE:
6729 vec<c_expr_t, va_gc> *cexpr_list;
6730 unsigned int i;
6731 c_expr_t *p;
6733 c_parser_consume_token (parser);
6734 if (!c_parser_get_builtin_args (parser,
6735 "__builtin_shuffle",
6736 &cexpr_list, false))
6738 expr.value = error_mark_node;
6739 break;
6742 FOR_EACH_VEC_SAFE_ELT (cexpr_list, i, p)
6743 mark_exp_read (p->value);
6745 if (vec_safe_length (cexpr_list) == 2)
6746 expr.value =
6747 c_build_vec_perm_expr
6748 (loc, (*cexpr_list)[0].value,
6749 NULL_TREE, (*cexpr_list)[1].value);
6751 else if (vec_safe_length (cexpr_list) == 3)
6752 expr.value =
6753 c_build_vec_perm_expr
6754 (loc, (*cexpr_list)[0].value,
6755 (*cexpr_list)[1].value,
6756 (*cexpr_list)[2].value);
6757 else
6759 error_at (loc, "wrong number of arguments to "
6760 "%<__builtin_shuffle%>");
6761 expr.value = error_mark_node;
6763 break;
6765 case RID_AT_SELECTOR:
6766 gcc_assert (c_dialect_objc ());
6767 c_parser_consume_token (parser);
6768 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6770 expr.value = error_mark_node;
6771 break;
6774 tree sel = c_parser_objc_selector_arg (parser);
6775 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
6776 "expected %<)%>");
6777 expr.value = objc_build_selector_expr (loc, sel);
6779 break;
6780 case RID_AT_PROTOCOL:
6781 gcc_assert (c_dialect_objc ());
6782 c_parser_consume_token (parser);
6783 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6785 expr.value = error_mark_node;
6786 break;
6788 if (c_parser_next_token_is_not (parser, CPP_NAME))
6790 c_parser_error (parser, "expected identifier");
6791 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6792 expr.value = error_mark_node;
6793 break;
6796 tree id = c_parser_peek_token (parser)->value;
6797 c_parser_consume_token (parser);
6798 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
6799 "expected %<)%>");
6800 expr.value = objc_build_protocol_expr (id);
6802 break;
6803 case RID_AT_ENCODE:
6804 /* Extension to support C-structures in the archiver. */
6805 gcc_assert (c_dialect_objc ());
6806 c_parser_consume_token (parser);
6807 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6809 expr.value = error_mark_node;
6810 break;
6812 t1 = c_parser_type_name (parser);
6813 if (t1 == NULL)
6815 expr.value = error_mark_node;
6816 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6817 break;
6819 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
6820 "expected %<)%>");
6822 tree type = groktypename (t1, NULL, NULL);
6823 expr.value = objc_build_encode_expr (type);
6825 break;
6826 default:
6827 c_parser_error (parser, "expected expression");
6828 expr.value = error_mark_node;
6829 break;
6831 break;
6832 case CPP_OPEN_SQUARE:
6833 if (c_dialect_objc ())
6835 tree receiver, args;
6836 c_parser_consume_token (parser);
6837 receiver = c_parser_objc_receiver (parser);
6838 args = c_parser_objc_message_args (parser);
6839 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
6840 "expected %<]%>");
6841 expr.value = objc_build_message_expr (receiver, args);
6842 break;
6844 /* Else fall through to report error. */
6845 default:
6846 c_parser_error (parser, "expected expression");
6847 expr.value = error_mark_node;
6848 break;
6850 return c_parser_postfix_expression_after_primary (parser, loc, expr);
6853 /* Parse a postfix expression after a parenthesized type name: the
6854 brace-enclosed initializer of a compound literal, possibly followed
6855 by some postfix operators. This is separate because it is not
6856 possible to tell until after the type name whether a cast
6857 expression has a cast or a compound literal, or whether the operand
6858 of sizeof is a parenthesized type name or starts with a compound
6859 literal. TYPE_LOC is the location where TYPE_NAME starts--the
6860 location of the first token after the parentheses around the type
6861 name. */
6863 static struct c_expr
6864 c_parser_postfix_expression_after_paren_type (c_parser *parser,
6865 struct c_type_name *type_name,
6866 location_t type_loc)
6868 tree type;
6869 struct c_expr init;
6870 bool non_const;
6871 struct c_expr expr;
6872 location_t start_loc;
6873 tree type_expr = NULL_TREE;
6874 bool type_expr_const = true;
6875 check_compound_literal_type (type_loc, type_name);
6876 start_init (NULL_TREE, NULL, 0);
6877 type = groktypename (type_name, &type_expr, &type_expr_const);
6878 start_loc = c_parser_peek_token (parser)->location;
6879 if (type != error_mark_node && C_TYPE_VARIABLE_SIZE (type))
6881 error_at (type_loc, "compound literal has variable size");
6882 type = error_mark_node;
6884 init = c_parser_braced_init (parser, type, false);
6885 finish_init ();
6886 maybe_warn_string_init (type, init);
6888 if (type != error_mark_node
6889 && !ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (type))
6890 && current_function_decl)
6892 error ("compound literal qualified by address-space qualifier");
6893 type = error_mark_node;
6896 if (!flag_isoc99)
6897 pedwarn (start_loc, OPT_Wpedantic, "ISO C90 forbids compound literals");
6898 non_const = ((init.value && TREE_CODE (init.value) == CONSTRUCTOR)
6899 ? CONSTRUCTOR_NON_CONST (init.value)
6900 : init.original_code == C_MAYBE_CONST_EXPR);
6901 non_const |= !type_expr_const;
6902 expr.value = build_compound_literal (start_loc, type, init.value, non_const);
6903 expr.original_code = ERROR_MARK;
6904 expr.original_type = NULL;
6905 if (type_expr)
6907 if (TREE_CODE (expr.value) == C_MAYBE_CONST_EXPR)
6909 gcc_assert (C_MAYBE_CONST_EXPR_PRE (expr.value) == NULL_TREE);
6910 C_MAYBE_CONST_EXPR_PRE (expr.value) = type_expr;
6912 else
6914 gcc_assert (!non_const);
6915 expr.value = build2 (C_MAYBE_CONST_EXPR, type,
6916 type_expr, expr.value);
6919 return c_parser_postfix_expression_after_primary (parser, start_loc, expr);
6922 /* Callback function for sizeof_pointer_memaccess_warning to compare
6923 types. */
6925 static bool
6926 sizeof_ptr_memacc_comptypes (tree type1, tree type2)
6928 return comptypes (type1, type2) == 1;
6931 /* Parse a postfix expression after the initial primary or compound
6932 literal; that is, parse a series of postfix operators.
6934 EXPR_LOC is the location of the primary expression. */
6936 static struct c_expr
6937 c_parser_postfix_expression_after_primary (c_parser *parser,
6938 location_t expr_loc,
6939 struct c_expr expr)
6941 struct c_expr orig_expr;
6942 tree ident, idx;
6943 location_t sizeof_arg_loc[3];
6944 tree sizeof_arg[3];
6945 unsigned int i;
6946 vec<tree, va_gc> *exprlist;
6947 vec<tree, va_gc> *origtypes = NULL;
6948 while (true)
6950 location_t op_loc = c_parser_peek_token (parser)->location;
6951 switch (c_parser_peek_token (parser)->type)
6953 case CPP_OPEN_SQUARE:
6954 /* Array reference. */
6955 c_parser_consume_token (parser);
6956 if (flag_enable_cilkplus
6957 && c_parser_peek_token (parser)->type == CPP_COLON)
6958 /* If we are here, then we have something like this:
6959 Array [ : ]
6961 expr.value = c_parser_array_notation (expr_loc, parser, NULL_TREE,
6962 expr.value);
6963 else
6965 idx = c_parser_expression (parser).value;
6966 /* Here we have 3 options:
6967 1. Array [EXPR] -- Normal Array call.
6968 2. Array [EXPR : EXPR] -- Array notation without stride.
6969 3. Array [EXPR : EXPR : EXPR] -- Array notation with stride.
6971 For 1, we just handle it just like a normal array expression.
6972 For 2 and 3 we handle it like we handle array notations. The
6973 idx value we have above becomes the initial/start index.
6975 if (flag_enable_cilkplus
6976 && c_parser_peek_token (parser)->type == CPP_COLON)
6977 expr.value = c_parser_array_notation (expr_loc, parser, idx,
6978 expr.value);
6979 else
6981 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
6982 "expected %<]%>");
6983 expr.value = build_array_ref (op_loc, expr.value, idx);
6986 expr.original_code = ERROR_MARK;
6987 expr.original_type = NULL;
6988 break;
6989 case CPP_OPEN_PAREN:
6990 /* Function call. */
6991 c_parser_consume_token (parser);
6992 for (i = 0; i < 3; i++)
6994 sizeof_arg[i] = NULL_TREE;
6995 sizeof_arg_loc[i] = UNKNOWN_LOCATION;
6997 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
6998 exprlist = NULL;
6999 else
7000 exprlist = c_parser_expr_list (parser, true, false, &origtypes,
7001 sizeof_arg_loc, sizeof_arg);
7002 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7003 "expected %<)%>");
7004 orig_expr = expr;
7005 mark_exp_read (expr.value);
7006 if (warn_sizeof_pointer_memaccess)
7007 sizeof_pointer_memaccess_warning (sizeof_arg_loc,
7008 expr.value, exprlist,
7009 sizeof_arg,
7010 sizeof_ptr_memacc_comptypes);
7011 /* FIXME diagnostics: Ideally we want the FUNCNAME, not the
7012 "(" after the FUNCNAME, which is what we have now. */
7013 expr.value = build_function_call_vec (op_loc, expr.value, exprlist,
7014 origtypes);
7015 expr.original_code = ERROR_MARK;
7016 if (TREE_CODE (expr.value) == INTEGER_CST
7017 && TREE_CODE (orig_expr.value) == FUNCTION_DECL
7018 && DECL_BUILT_IN_CLASS (orig_expr.value) == BUILT_IN_NORMAL
7019 && DECL_FUNCTION_CODE (orig_expr.value) == BUILT_IN_CONSTANT_P)
7020 expr.original_code = C_MAYBE_CONST_EXPR;
7021 expr.original_type = NULL;
7022 if (exprlist)
7024 release_tree_vector (exprlist);
7025 release_tree_vector (origtypes);
7027 break;
7028 case CPP_DOT:
7029 /* Structure element reference. */
7030 c_parser_consume_token (parser);
7031 expr = default_function_array_conversion (expr_loc, expr);
7032 if (c_parser_next_token_is (parser, CPP_NAME))
7033 ident = c_parser_peek_token (parser)->value;
7034 else
7036 c_parser_error (parser, "expected identifier");
7037 expr.value = error_mark_node;
7038 expr.original_code = ERROR_MARK;
7039 expr.original_type = NULL;
7040 return expr;
7042 c_parser_consume_token (parser);
7043 expr.value = build_component_ref (op_loc, expr.value, ident);
7044 expr.original_code = ERROR_MARK;
7045 if (TREE_CODE (expr.value) != COMPONENT_REF)
7046 expr.original_type = NULL;
7047 else
7049 /* Remember the original type of a bitfield. */
7050 tree field = TREE_OPERAND (expr.value, 1);
7051 if (TREE_CODE (field) != FIELD_DECL)
7052 expr.original_type = NULL;
7053 else
7054 expr.original_type = DECL_BIT_FIELD_TYPE (field);
7056 break;
7057 case CPP_DEREF:
7058 /* Structure element reference. */
7059 c_parser_consume_token (parser);
7060 expr = default_function_array_conversion (expr_loc, expr);
7061 if (c_parser_next_token_is (parser, CPP_NAME))
7062 ident = c_parser_peek_token (parser)->value;
7063 else
7065 c_parser_error (parser, "expected identifier");
7066 expr.value = error_mark_node;
7067 expr.original_code = ERROR_MARK;
7068 expr.original_type = NULL;
7069 return expr;
7071 c_parser_consume_token (parser);
7072 expr.value = build_component_ref (op_loc,
7073 build_indirect_ref (op_loc,
7074 expr.value,
7075 RO_ARROW),
7076 ident);
7077 expr.original_code = ERROR_MARK;
7078 if (TREE_CODE (expr.value) != COMPONENT_REF)
7079 expr.original_type = NULL;
7080 else
7082 /* Remember the original type of a bitfield. */
7083 tree field = TREE_OPERAND (expr.value, 1);
7084 if (TREE_CODE (field) != FIELD_DECL)
7085 expr.original_type = NULL;
7086 else
7087 expr.original_type = DECL_BIT_FIELD_TYPE (field);
7089 break;
7090 case CPP_PLUS_PLUS:
7091 /* Postincrement. */
7092 c_parser_consume_token (parser);
7093 /* If the expressions have array notations, we expand them. */
7094 if (flag_enable_cilkplus
7095 && TREE_CODE (expr.value) == ARRAY_NOTATION_REF)
7096 expr = fix_array_notation_expr (expr_loc, POSTINCREMENT_EXPR, expr);
7097 else
7099 expr = default_function_array_read_conversion (expr_loc, expr);
7100 expr.value = build_unary_op (op_loc,
7101 POSTINCREMENT_EXPR, expr.value, 0);
7103 expr.original_code = ERROR_MARK;
7104 expr.original_type = NULL;
7105 break;
7106 case CPP_MINUS_MINUS:
7107 /* Postdecrement. */
7108 c_parser_consume_token (parser);
7109 /* If the expressions have array notations, we expand them. */
7110 if (flag_enable_cilkplus
7111 && TREE_CODE (expr.value) == ARRAY_NOTATION_REF)
7112 expr = fix_array_notation_expr (expr_loc, POSTDECREMENT_EXPR, expr);
7113 else
7115 expr = default_function_array_read_conversion (expr_loc, expr);
7116 expr.value = build_unary_op (op_loc,
7117 POSTDECREMENT_EXPR, expr.value, 0);
7119 expr.original_code = ERROR_MARK;
7120 expr.original_type = NULL;
7121 break;
7122 default:
7123 return expr;
7128 /* Parse an expression (C90 6.3.17, C99 6.5.17).
7130 expression:
7131 assignment-expression
7132 expression , assignment-expression
7135 static struct c_expr
7136 c_parser_expression (c_parser *parser)
7138 struct c_expr expr;
7139 expr = c_parser_expr_no_commas (parser, NULL);
7140 while (c_parser_next_token_is (parser, CPP_COMMA))
7142 struct c_expr next;
7143 tree lhsval;
7144 location_t loc = c_parser_peek_token (parser)->location;
7145 location_t expr_loc;
7146 c_parser_consume_token (parser);
7147 expr_loc = c_parser_peek_token (parser)->location;
7148 lhsval = expr.value;
7149 while (TREE_CODE (lhsval) == COMPOUND_EXPR)
7150 lhsval = TREE_OPERAND (lhsval, 1);
7151 if (DECL_P (lhsval) || handled_component_p (lhsval))
7152 mark_exp_read (lhsval);
7153 next = c_parser_expr_no_commas (parser, NULL);
7154 next = default_function_array_conversion (expr_loc, next);
7155 expr.value = build_compound_expr (loc, expr.value, next.value);
7156 expr.original_code = COMPOUND_EXPR;
7157 expr.original_type = next.original_type;
7159 return expr;
7162 /* Parse an expression and convert functions or arrays to
7163 pointers. */
7165 static struct c_expr
7166 c_parser_expression_conv (c_parser *parser)
7168 struct c_expr expr;
7169 location_t loc = c_parser_peek_token (parser)->location;
7170 expr = c_parser_expression (parser);
7171 expr = default_function_array_conversion (loc, expr);
7172 return expr;
7175 /* Parse a non-empty list of expressions. If CONVERT_P, convert
7176 functions and arrays to pointers. If FOLD_P, fold the expressions.
7178 nonempty-expr-list:
7179 assignment-expression
7180 nonempty-expr-list , assignment-expression
7183 static vec<tree, va_gc> *
7184 c_parser_expr_list (c_parser *parser, bool convert_p, bool fold_p,
7185 vec<tree, va_gc> **p_orig_types,
7186 location_t *sizeof_arg_loc, tree *sizeof_arg)
7188 vec<tree, va_gc> *ret;
7189 vec<tree, va_gc> *orig_types;
7190 struct c_expr expr;
7191 location_t loc = c_parser_peek_token (parser)->location;
7192 location_t cur_sizeof_arg_loc = UNKNOWN_LOCATION;
7193 unsigned int idx = 0;
7195 ret = make_tree_vector ();
7196 if (p_orig_types == NULL)
7197 orig_types = NULL;
7198 else
7199 orig_types = make_tree_vector ();
7201 if (sizeof_arg != NULL
7202 && c_parser_next_token_is_keyword (parser, RID_SIZEOF))
7203 cur_sizeof_arg_loc = c_parser_peek_2nd_token (parser)->location;
7204 expr = c_parser_expr_no_commas (parser, NULL);
7205 if (convert_p)
7206 expr = default_function_array_read_conversion (loc, expr);
7207 if (fold_p)
7208 expr.value = c_fully_fold (expr.value, false, NULL);
7209 ret->quick_push (expr.value);
7210 if (orig_types)
7211 orig_types->quick_push (expr.original_type);
7212 if (sizeof_arg != NULL
7213 && cur_sizeof_arg_loc != UNKNOWN_LOCATION
7214 && expr.original_code == SIZEOF_EXPR)
7216 sizeof_arg[0] = c_last_sizeof_arg;
7217 sizeof_arg_loc[0] = cur_sizeof_arg_loc;
7219 while (c_parser_next_token_is (parser, CPP_COMMA))
7221 c_parser_consume_token (parser);
7222 loc = c_parser_peek_token (parser)->location;
7223 if (sizeof_arg != NULL
7224 && c_parser_next_token_is_keyword (parser, RID_SIZEOF))
7225 cur_sizeof_arg_loc = c_parser_peek_2nd_token (parser)->location;
7226 else
7227 cur_sizeof_arg_loc = UNKNOWN_LOCATION;
7228 expr = c_parser_expr_no_commas (parser, NULL);
7229 if (convert_p)
7230 expr = default_function_array_read_conversion (loc, expr);
7231 if (fold_p)
7232 expr.value = c_fully_fold (expr.value, false, NULL);
7233 vec_safe_push (ret, expr.value);
7234 if (orig_types)
7235 vec_safe_push (orig_types, expr.original_type);
7236 if (++idx < 3
7237 && sizeof_arg != NULL
7238 && cur_sizeof_arg_loc != UNKNOWN_LOCATION
7239 && expr.original_code == SIZEOF_EXPR)
7241 sizeof_arg[idx] = c_last_sizeof_arg;
7242 sizeof_arg_loc[idx] = cur_sizeof_arg_loc;
7245 if (orig_types)
7246 *p_orig_types = orig_types;
7247 return ret;
7250 /* Parse Objective-C-specific constructs. */
7252 /* Parse an objc-class-definition.
7254 objc-class-definition:
7255 @interface identifier objc-superclass[opt] objc-protocol-refs[opt]
7256 objc-class-instance-variables[opt] objc-methodprotolist @end
7257 @implementation identifier objc-superclass[opt]
7258 objc-class-instance-variables[opt]
7259 @interface identifier ( identifier ) objc-protocol-refs[opt]
7260 objc-methodprotolist @end
7261 @interface identifier ( ) objc-protocol-refs[opt]
7262 objc-methodprotolist @end
7263 @implementation identifier ( identifier )
7265 objc-superclass:
7266 : identifier
7268 "@interface identifier (" must start "@interface identifier (
7269 identifier ) ...": objc-methodprotolist in the first production may
7270 not start with a parenthesized identifier as a declarator of a data
7271 definition with no declaration specifiers if the objc-superclass,
7272 objc-protocol-refs and objc-class-instance-variables are omitted. */
7274 static void
7275 c_parser_objc_class_definition (c_parser *parser, tree attributes)
7277 bool iface_p;
7278 tree id1;
7279 tree superclass;
7280 if (c_parser_next_token_is_keyword (parser, RID_AT_INTERFACE))
7281 iface_p = true;
7282 else if (c_parser_next_token_is_keyword (parser, RID_AT_IMPLEMENTATION))
7283 iface_p = false;
7284 else
7285 gcc_unreachable ();
7287 c_parser_consume_token (parser);
7288 if (c_parser_next_token_is_not (parser, CPP_NAME))
7290 c_parser_error (parser, "expected identifier");
7291 return;
7293 id1 = c_parser_peek_token (parser)->value;
7294 c_parser_consume_token (parser);
7295 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
7297 /* We have a category or class extension. */
7298 tree id2;
7299 tree proto = NULL_TREE;
7300 c_parser_consume_token (parser);
7301 if (c_parser_next_token_is_not (parser, CPP_NAME))
7303 if (iface_p && c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
7305 /* We have a class extension. */
7306 id2 = NULL_TREE;
7308 else
7310 c_parser_error (parser, "expected identifier or %<)%>");
7311 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7312 return;
7315 else
7317 id2 = c_parser_peek_token (parser)->value;
7318 c_parser_consume_token (parser);
7320 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
7321 if (!iface_p)
7323 objc_start_category_implementation (id1, id2);
7324 return;
7326 if (c_parser_next_token_is (parser, CPP_LESS))
7327 proto = c_parser_objc_protocol_refs (parser);
7328 objc_start_category_interface (id1, id2, proto, attributes);
7329 c_parser_objc_methodprotolist (parser);
7330 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
7331 objc_finish_interface ();
7332 return;
7334 if (c_parser_next_token_is (parser, CPP_COLON))
7336 c_parser_consume_token (parser);
7337 if (c_parser_next_token_is_not (parser, CPP_NAME))
7339 c_parser_error (parser, "expected identifier");
7340 return;
7342 superclass = c_parser_peek_token (parser)->value;
7343 c_parser_consume_token (parser);
7345 else
7346 superclass = NULL_TREE;
7347 if (iface_p)
7349 tree proto = NULL_TREE;
7350 if (c_parser_next_token_is (parser, CPP_LESS))
7351 proto = c_parser_objc_protocol_refs (parser);
7352 objc_start_class_interface (id1, superclass, proto, attributes);
7354 else
7355 objc_start_class_implementation (id1, superclass);
7356 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
7357 c_parser_objc_class_instance_variables (parser);
7358 if (iface_p)
7360 objc_continue_interface ();
7361 c_parser_objc_methodprotolist (parser);
7362 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
7363 objc_finish_interface ();
7365 else
7367 objc_continue_implementation ();
7368 return;
7372 /* Parse objc-class-instance-variables.
7374 objc-class-instance-variables:
7375 { objc-instance-variable-decl-list[opt] }
7377 objc-instance-variable-decl-list:
7378 objc-visibility-spec
7379 objc-instance-variable-decl ;
7381 objc-instance-variable-decl-list objc-visibility-spec
7382 objc-instance-variable-decl-list objc-instance-variable-decl ;
7383 objc-instance-variable-decl-list ;
7385 objc-visibility-spec:
7386 @private
7387 @protected
7388 @public
7390 objc-instance-variable-decl:
7391 struct-declaration
7394 static void
7395 c_parser_objc_class_instance_variables (c_parser *parser)
7397 gcc_assert (c_parser_next_token_is (parser, CPP_OPEN_BRACE));
7398 c_parser_consume_token (parser);
7399 while (c_parser_next_token_is_not (parser, CPP_EOF))
7401 tree decls;
7402 /* Parse any stray semicolon. */
7403 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
7405 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
7406 "extra semicolon");
7407 c_parser_consume_token (parser);
7408 continue;
7410 /* Stop if at the end of the instance variables. */
7411 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
7413 c_parser_consume_token (parser);
7414 break;
7416 /* Parse any objc-visibility-spec. */
7417 if (c_parser_next_token_is_keyword (parser, RID_AT_PRIVATE))
7419 c_parser_consume_token (parser);
7420 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
7421 continue;
7423 else if (c_parser_next_token_is_keyword (parser, RID_AT_PROTECTED))
7425 c_parser_consume_token (parser);
7426 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
7427 continue;
7429 else if (c_parser_next_token_is_keyword (parser, RID_AT_PUBLIC))
7431 c_parser_consume_token (parser);
7432 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
7433 continue;
7435 else if (c_parser_next_token_is_keyword (parser, RID_AT_PACKAGE))
7437 c_parser_consume_token (parser);
7438 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
7439 continue;
7441 else if (c_parser_next_token_is (parser, CPP_PRAGMA))
7443 c_parser_pragma (parser, pragma_external);
7444 continue;
7447 /* Parse some comma-separated declarations. */
7448 decls = c_parser_struct_declaration (parser);
7449 if (decls == NULL)
7451 /* There is a syntax error. We want to skip the offending
7452 tokens up to the next ';' (included) or '}'
7453 (excluded). */
7455 /* First, skip manually a ')' or ']'. This is because they
7456 reduce the nesting level, so c_parser_skip_until_found()
7457 wouldn't be able to skip past them. */
7458 c_token *token = c_parser_peek_token (parser);
7459 if (token->type == CPP_CLOSE_PAREN || token->type == CPP_CLOSE_SQUARE)
7460 c_parser_consume_token (parser);
7462 /* Then, do the standard skipping. */
7463 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
7465 /* We hopefully recovered. Start normal parsing again. */
7466 parser->error = false;
7467 continue;
7469 else
7471 /* Comma-separated instance variables are chained together
7472 in reverse order; add them one by one. */
7473 tree ivar = nreverse (decls);
7474 for (; ivar; ivar = DECL_CHAIN (ivar))
7475 objc_add_instance_variable (copy_node (ivar));
7477 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
7481 /* Parse an objc-class-declaration.
7483 objc-class-declaration:
7484 @class identifier-list ;
7487 static void
7488 c_parser_objc_class_declaration (c_parser *parser)
7490 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_CLASS));
7491 c_parser_consume_token (parser);
7492 /* Any identifiers, including those declared as type names, are OK
7493 here. */
7494 while (true)
7496 tree id;
7497 if (c_parser_next_token_is_not (parser, CPP_NAME))
7499 c_parser_error (parser, "expected identifier");
7500 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
7501 parser->error = false;
7502 return;
7504 id = c_parser_peek_token (parser)->value;
7505 objc_declare_class (id);
7506 c_parser_consume_token (parser);
7507 if (c_parser_next_token_is (parser, CPP_COMMA))
7508 c_parser_consume_token (parser);
7509 else
7510 break;
7512 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
7515 /* Parse an objc-alias-declaration.
7517 objc-alias-declaration:
7518 @compatibility_alias identifier identifier ;
7521 static void
7522 c_parser_objc_alias_declaration (c_parser *parser)
7524 tree id1, id2;
7525 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_ALIAS));
7526 c_parser_consume_token (parser);
7527 if (c_parser_next_token_is_not (parser, CPP_NAME))
7529 c_parser_error (parser, "expected identifier");
7530 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
7531 return;
7533 id1 = c_parser_peek_token (parser)->value;
7534 c_parser_consume_token (parser);
7535 if (c_parser_next_token_is_not (parser, CPP_NAME))
7537 c_parser_error (parser, "expected identifier");
7538 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
7539 return;
7541 id2 = c_parser_peek_token (parser)->value;
7542 c_parser_consume_token (parser);
7543 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
7544 objc_declare_alias (id1, id2);
7547 /* Parse an objc-protocol-definition.
7549 objc-protocol-definition:
7550 @protocol identifier objc-protocol-refs[opt] objc-methodprotolist @end
7551 @protocol identifier-list ;
7553 "@protocol identifier ;" should be resolved as "@protocol
7554 identifier-list ;": objc-methodprotolist may not start with a
7555 semicolon in the first alternative if objc-protocol-refs are
7556 omitted. */
7558 static void
7559 c_parser_objc_protocol_definition (c_parser *parser, tree attributes)
7561 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_PROTOCOL));
7563 c_parser_consume_token (parser);
7564 if (c_parser_next_token_is_not (parser, CPP_NAME))
7566 c_parser_error (parser, "expected identifier");
7567 return;
7569 if (c_parser_peek_2nd_token (parser)->type == CPP_COMMA
7570 || c_parser_peek_2nd_token (parser)->type == CPP_SEMICOLON)
7572 /* Any identifiers, including those declared as type names, are
7573 OK here. */
7574 while (true)
7576 tree id;
7577 if (c_parser_next_token_is_not (parser, CPP_NAME))
7579 c_parser_error (parser, "expected identifier");
7580 break;
7582 id = c_parser_peek_token (parser)->value;
7583 objc_declare_protocol (id, attributes);
7584 c_parser_consume_token (parser);
7585 if (c_parser_next_token_is (parser, CPP_COMMA))
7586 c_parser_consume_token (parser);
7587 else
7588 break;
7590 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
7592 else
7594 tree id = c_parser_peek_token (parser)->value;
7595 tree proto = NULL_TREE;
7596 c_parser_consume_token (parser);
7597 if (c_parser_next_token_is (parser, CPP_LESS))
7598 proto = c_parser_objc_protocol_refs (parser);
7599 parser->objc_pq_context = true;
7600 objc_start_protocol (id, proto, attributes);
7601 c_parser_objc_methodprotolist (parser);
7602 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
7603 parser->objc_pq_context = false;
7604 objc_finish_interface ();
7608 /* Parse an objc-method-type.
7610 objc-method-type:
7614 Return true if it is a class method (+) and false if it is
7615 an instance method (-).
7617 static inline bool
7618 c_parser_objc_method_type (c_parser *parser)
7620 switch (c_parser_peek_token (parser)->type)
7622 case CPP_PLUS:
7623 c_parser_consume_token (parser);
7624 return true;
7625 case CPP_MINUS:
7626 c_parser_consume_token (parser);
7627 return false;
7628 default:
7629 gcc_unreachable ();
7633 /* Parse an objc-method-definition.
7635 objc-method-definition:
7636 objc-method-type objc-method-decl ;[opt] compound-statement
7639 static void
7640 c_parser_objc_method_definition (c_parser *parser)
7642 bool is_class_method = c_parser_objc_method_type (parser);
7643 tree decl, attributes = NULL_TREE, expr = NULL_TREE;
7644 parser->objc_pq_context = true;
7645 decl = c_parser_objc_method_decl (parser, is_class_method, &attributes,
7646 &expr);
7647 if (decl == error_mark_node)
7648 return; /* Bail here. */
7650 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
7652 c_parser_consume_token (parser);
7653 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
7654 "extra semicolon in method definition specified");
7657 if (!c_parser_next_token_is (parser, CPP_OPEN_BRACE))
7659 c_parser_error (parser, "expected %<{%>");
7660 return;
7663 parser->objc_pq_context = false;
7664 if (objc_start_method_definition (is_class_method, decl, attributes, expr))
7666 add_stmt (c_parser_compound_statement (parser));
7667 objc_finish_method_definition (current_function_decl);
7669 else
7671 /* This code is executed when we find a method definition
7672 outside of an @implementation context (or invalid for other
7673 reasons). Parse the method (to keep going) but do not emit
7674 any code.
7676 c_parser_compound_statement (parser);
7680 /* Parse an objc-methodprotolist.
7682 objc-methodprotolist:
7683 empty
7684 objc-methodprotolist objc-methodproto
7685 objc-methodprotolist declaration
7686 objc-methodprotolist ;
7687 @optional
7688 @required
7690 The declaration is a data definition, which may be missing
7691 declaration specifiers under the same rules and diagnostics as
7692 other data definitions outside functions, and the stray semicolon
7693 is diagnosed the same way as a stray semicolon outside a
7694 function. */
7696 static void
7697 c_parser_objc_methodprotolist (c_parser *parser)
7699 while (true)
7701 /* The list is terminated by @end. */
7702 switch (c_parser_peek_token (parser)->type)
7704 case CPP_SEMICOLON:
7705 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
7706 "ISO C does not allow extra %<;%> outside of a function");
7707 c_parser_consume_token (parser);
7708 break;
7709 case CPP_PLUS:
7710 case CPP_MINUS:
7711 c_parser_objc_methodproto (parser);
7712 break;
7713 case CPP_PRAGMA:
7714 c_parser_pragma (parser, pragma_external);
7715 break;
7716 case CPP_EOF:
7717 return;
7718 default:
7719 if (c_parser_next_token_is_keyword (parser, RID_AT_END))
7720 return;
7721 else if (c_parser_next_token_is_keyword (parser, RID_AT_PROPERTY))
7722 c_parser_objc_at_property_declaration (parser);
7723 else if (c_parser_next_token_is_keyword (parser, RID_AT_OPTIONAL))
7725 objc_set_method_opt (true);
7726 c_parser_consume_token (parser);
7728 else if (c_parser_next_token_is_keyword (parser, RID_AT_REQUIRED))
7730 objc_set_method_opt (false);
7731 c_parser_consume_token (parser);
7733 else
7734 c_parser_declaration_or_fndef (parser, false, false, true,
7735 false, true, NULL);
7736 break;
7741 /* Parse an objc-methodproto.
7743 objc-methodproto:
7744 objc-method-type objc-method-decl ;
7747 static void
7748 c_parser_objc_methodproto (c_parser *parser)
7750 bool is_class_method = c_parser_objc_method_type (parser);
7751 tree decl, attributes = NULL_TREE;
7753 /* Remember protocol qualifiers in prototypes. */
7754 parser->objc_pq_context = true;
7755 decl = c_parser_objc_method_decl (parser, is_class_method, &attributes,
7756 NULL);
7757 /* Forget protocol qualifiers now. */
7758 parser->objc_pq_context = false;
7760 /* Do not allow the presence of attributes to hide an erroneous
7761 method implementation in the interface section. */
7762 if (!c_parser_next_token_is (parser, CPP_SEMICOLON))
7764 c_parser_error (parser, "expected %<;%>");
7765 return;
7768 if (decl != error_mark_node)
7769 objc_add_method_declaration (is_class_method, decl, attributes);
7771 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
7774 /* If we are at a position that method attributes may be present, check that
7775 there are not any parsed already (a syntax error) and then collect any
7776 specified at the current location. Finally, if new attributes were present,
7777 check that the next token is legal ( ';' for decls and '{' for defs). */
7779 static bool
7780 c_parser_objc_maybe_method_attributes (c_parser* parser, tree* attributes)
7782 bool bad = false;
7783 if (*attributes)
7785 c_parser_error (parser,
7786 "method attributes must be specified at the end only");
7787 *attributes = NULL_TREE;
7788 bad = true;
7791 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
7792 *attributes = c_parser_attributes (parser);
7794 /* If there were no attributes here, just report any earlier error. */
7795 if (*attributes == NULL_TREE || bad)
7796 return bad;
7798 /* If the attributes are followed by a ; or {, then just report any earlier
7799 error. */
7800 if (c_parser_next_token_is (parser, CPP_SEMICOLON)
7801 || c_parser_next_token_is (parser, CPP_OPEN_BRACE))
7802 return bad;
7804 /* We've got attributes, but not at the end. */
7805 c_parser_error (parser,
7806 "expected %<;%> or %<{%> after method attribute definition");
7807 return true;
7810 /* Parse an objc-method-decl.
7812 objc-method-decl:
7813 ( objc-type-name ) objc-selector
7814 objc-selector
7815 ( objc-type-name ) objc-keyword-selector objc-optparmlist
7816 objc-keyword-selector objc-optparmlist
7817 attributes
7819 objc-keyword-selector:
7820 objc-keyword-decl
7821 objc-keyword-selector objc-keyword-decl
7823 objc-keyword-decl:
7824 objc-selector : ( objc-type-name ) identifier
7825 objc-selector : identifier
7826 : ( objc-type-name ) identifier
7827 : identifier
7829 objc-optparmlist:
7830 objc-optparms objc-optellipsis
7832 objc-optparms:
7833 empty
7834 objc-opt-parms , parameter-declaration
7836 objc-optellipsis:
7837 empty
7838 , ...
7841 static tree
7842 c_parser_objc_method_decl (c_parser *parser, bool is_class_method,
7843 tree *attributes, tree *expr)
7845 tree type = NULL_TREE;
7846 tree sel;
7847 tree parms = NULL_TREE;
7848 bool ellipsis = false;
7849 bool attr_err = false;
7851 *attributes = NULL_TREE;
7852 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
7854 c_parser_consume_token (parser);
7855 type = c_parser_objc_type_name (parser);
7856 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
7858 sel = c_parser_objc_selector (parser);
7859 /* If there is no selector, or a colon follows, we have an
7860 objc-keyword-selector. If there is a selector, and a colon does
7861 not follow, that selector ends the objc-method-decl. */
7862 if (!sel || c_parser_next_token_is (parser, CPP_COLON))
7864 tree tsel = sel;
7865 tree list = NULL_TREE;
7866 while (true)
7868 tree atype = NULL_TREE, id, keyworddecl;
7869 tree param_attr = NULL_TREE;
7870 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
7871 break;
7872 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
7874 c_parser_consume_token (parser);
7875 atype = c_parser_objc_type_name (parser);
7876 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7877 "expected %<)%>");
7879 /* New ObjC allows attributes on method parameters. */
7880 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
7881 param_attr = c_parser_attributes (parser);
7882 if (c_parser_next_token_is_not (parser, CPP_NAME))
7884 c_parser_error (parser, "expected identifier");
7885 return error_mark_node;
7887 id = c_parser_peek_token (parser)->value;
7888 c_parser_consume_token (parser);
7889 keyworddecl = objc_build_keyword_decl (tsel, atype, id, param_attr);
7890 list = chainon (list, keyworddecl);
7891 tsel = c_parser_objc_selector (parser);
7892 if (!tsel && c_parser_next_token_is_not (parser, CPP_COLON))
7893 break;
7896 attr_err |= c_parser_objc_maybe_method_attributes (parser, attributes) ;
7898 /* Parse the optional parameter list. Optional Objective-C
7899 method parameters follow the C syntax, and may include '...'
7900 to denote a variable number of arguments. */
7901 parms = make_node (TREE_LIST);
7902 while (c_parser_next_token_is (parser, CPP_COMMA))
7904 struct c_parm *parm;
7905 c_parser_consume_token (parser);
7906 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
7908 ellipsis = true;
7909 c_parser_consume_token (parser);
7910 attr_err |= c_parser_objc_maybe_method_attributes
7911 (parser, attributes) ;
7912 break;
7914 parm = c_parser_parameter_declaration (parser, NULL_TREE);
7915 if (parm == NULL)
7916 break;
7917 parms = chainon (parms,
7918 build_tree_list (NULL_TREE, grokparm (parm, expr)));
7920 sel = list;
7922 else
7923 attr_err |= c_parser_objc_maybe_method_attributes (parser, attributes) ;
7925 if (sel == NULL)
7927 c_parser_error (parser, "objective-c method declaration is expected");
7928 return error_mark_node;
7931 if (attr_err)
7932 return error_mark_node;
7934 return objc_build_method_signature (is_class_method, type, sel, parms, ellipsis);
7937 /* Parse an objc-type-name.
7939 objc-type-name:
7940 objc-type-qualifiers[opt] type-name
7941 objc-type-qualifiers[opt]
7943 objc-type-qualifiers:
7944 objc-type-qualifier
7945 objc-type-qualifiers objc-type-qualifier
7947 objc-type-qualifier: one of
7948 in out inout bycopy byref oneway
7951 static tree
7952 c_parser_objc_type_name (c_parser *parser)
7954 tree quals = NULL_TREE;
7955 struct c_type_name *type_name = NULL;
7956 tree type = NULL_TREE;
7957 while (true)
7959 c_token *token = c_parser_peek_token (parser);
7960 if (token->type == CPP_KEYWORD
7961 && (token->keyword == RID_IN
7962 || token->keyword == RID_OUT
7963 || token->keyword == RID_INOUT
7964 || token->keyword == RID_BYCOPY
7965 || token->keyword == RID_BYREF
7966 || token->keyword == RID_ONEWAY))
7968 quals = chainon (build_tree_list (NULL_TREE, token->value), quals);
7969 c_parser_consume_token (parser);
7971 else
7972 break;
7974 if (c_parser_next_tokens_start_typename (parser, cla_prefer_type))
7975 type_name = c_parser_type_name (parser);
7976 if (type_name)
7977 type = groktypename (type_name, NULL, NULL);
7979 /* If the type is unknown, and error has already been produced and
7980 we need to recover from the error. In that case, use NULL_TREE
7981 for the type, as if no type had been specified; this will use the
7982 default type ('id') which is good for error recovery. */
7983 if (type == error_mark_node)
7984 type = NULL_TREE;
7986 return build_tree_list (quals, type);
7989 /* Parse objc-protocol-refs.
7991 objc-protocol-refs:
7992 < identifier-list >
7995 static tree
7996 c_parser_objc_protocol_refs (c_parser *parser)
7998 tree list = NULL_TREE;
7999 gcc_assert (c_parser_next_token_is (parser, CPP_LESS));
8000 c_parser_consume_token (parser);
8001 /* Any identifiers, including those declared as type names, are OK
8002 here. */
8003 while (true)
8005 tree id;
8006 if (c_parser_next_token_is_not (parser, CPP_NAME))
8008 c_parser_error (parser, "expected identifier");
8009 break;
8011 id = c_parser_peek_token (parser)->value;
8012 list = chainon (list, build_tree_list (NULL_TREE, id));
8013 c_parser_consume_token (parser);
8014 if (c_parser_next_token_is (parser, CPP_COMMA))
8015 c_parser_consume_token (parser);
8016 else
8017 break;
8019 c_parser_require (parser, CPP_GREATER, "expected %<>%>");
8020 return list;
8023 /* Parse an objc-try-catch-finally-statement.
8025 objc-try-catch-finally-statement:
8026 @try compound-statement objc-catch-list[opt]
8027 @try compound-statement objc-catch-list[opt] @finally compound-statement
8029 objc-catch-list:
8030 @catch ( objc-catch-parameter-declaration ) compound-statement
8031 objc-catch-list @catch ( objc-catch-parameter-declaration ) compound-statement
8033 objc-catch-parameter-declaration:
8034 parameter-declaration
8035 '...'
8037 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
8039 PS: This function is identical to cp_parser_objc_try_catch_finally_statement
8040 for C++. Keep them in sync. */
8042 static void
8043 c_parser_objc_try_catch_finally_statement (c_parser *parser)
8045 location_t location;
8046 tree stmt;
8048 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_TRY));
8049 c_parser_consume_token (parser);
8050 location = c_parser_peek_token (parser)->location;
8051 objc_maybe_warn_exceptions (location);
8052 stmt = c_parser_compound_statement (parser);
8053 objc_begin_try_stmt (location, stmt);
8055 while (c_parser_next_token_is_keyword (parser, RID_AT_CATCH))
8057 struct c_parm *parm;
8058 tree parameter_declaration = error_mark_node;
8059 bool seen_open_paren = false;
8061 c_parser_consume_token (parser);
8062 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
8063 seen_open_paren = true;
8064 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
8066 /* We have "@catch (...)" (where the '...' are literally
8067 what is in the code). Skip the '...'.
8068 parameter_declaration is set to NULL_TREE, and
8069 objc_being_catch_clauses() knows that that means
8070 '...'. */
8071 c_parser_consume_token (parser);
8072 parameter_declaration = NULL_TREE;
8074 else
8076 /* We have "@catch (NSException *exception)" or something
8077 like that. Parse the parameter declaration. */
8078 parm = c_parser_parameter_declaration (parser, NULL_TREE);
8079 if (parm == NULL)
8080 parameter_declaration = error_mark_node;
8081 else
8082 parameter_declaration = grokparm (parm, NULL);
8084 if (seen_open_paren)
8085 c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
8086 else
8088 /* If there was no open parenthesis, we are recovering from
8089 an error, and we are trying to figure out what mistake
8090 the user has made. */
8092 /* If there is an immediate closing parenthesis, the user
8093 probably forgot the opening one (ie, they typed "@catch
8094 NSException *e)". Parse the closing parenthesis and keep
8095 going. */
8096 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
8097 c_parser_consume_token (parser);
8099 /* If these is no immediate closing parenthesis, the user
8100 probably doesn't know that parenthesis are required at
8101 all (ie, they typed "@catch NSException *e"). So, just
8102 forget about the closing parenthesis and keep going. */
8104 objc_begin_catch_clause (parameter_declaration);
8105 if (c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
8106 c_parser_compound_statement_nostart (parser);
8107 objc_finish_catch_clause ();
8109 if (c_parser_next_token_is_keyword (parser, RID_AT_FINALLY))
8111 c_parser_consume_token (parser);
8112 location = c_parser_peek_token (parser)->location;
8113 stmt = c_parser_compound_statement (parser);
8114 objc_build_finally_clause (location, stmt);
8116 objc_finish_try_stmt ();
8119 /* Parse an objc-synchronized-statement.
8121 objc-synchronized-statement:
8122 @synchronized ( expression ) compound-statement
8125 static void
8126 c_parser_objc_synchronized_statement (c_parser *parser)
8128 location_t loc;
8129 tree expr, stmt;
8130 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_SYNCHRONIZED));
8131 c_parser_consume_token (parser);
8132 loc = c_parser_peek_token (parser)->location;
8133 objc_maybe_warn_exceptions (loc);
8134 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
8136 expr = c_parser_expression (parser).value;
8137 expr = c_fully_fold (expr, false, NULL);
8138 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
8140 else
8141 expr = error_mark_node;
8142 stmt = c_parser_compound_statement (parser);
8143 objc_build_synchronized (loc, expr, stmt);
8146 /* Parse an objc-selector; return NULL_TREE without an error if the
8147 next token is not an objc-selector.
8149 objc-selector:
8150 identifier
8151 one of
8152 enum struct union if else while do for switch case default
8153 break continue return goto asm sizeof typeof __alignof
8154 unsigned long const short volatile signed restrict _Complex
8155 in out inout bycopy byref oneway int char float double void _Bool
8157 ??? Why this selection of keywords but not, for example, storage
8158 class specifiers? */
8160 static tree
8161 c_parser_objc_selector (c_parser *parser)
8163 c_token *token = c_parser_peek_token (parser);
8164 tree value = token->value;
8165 if (token->type == CPP_NAME)
8167 c_parser_consume_token (parser);
8168 return value;
8170 if (token->type != CPP_KEYWORD)
8171 return NULL_TREE;
8172 switch (token->keyword)
8174 case RID_ENUM:
8175 case RID_STRUCT:
8176 case RID_UNION:
8177 case RID_IF:
8178 case RID_ELSE:
8179 case RID_WHILE:
8180 case RID_DO:
8181 case RID_FOR:
8182 case RID_SWITCH:
8183 case RID_CASE:
8184 case RID_DEFAULT:
8185 case RID_BREAK:
8186 case RID_CONTINUE:
8187 case RID_RETURN:
8188 case RID_GOTO:
8189 case RID_ASM:
8190 case RID_SIZEOF:
8191 case RID_TYPEOF:
8192 case RID_ALIGNOF:
8193 case RID_UNSIGNED:
8194 case RID_LONG:
8195 case RID_INT128:
8196 case RID_CONST:
8197 case RID_SHORT:
8198 case RID_VOLATILE:
8199 case RID_SIGNED:
8200 case RID_RESTRICT:
8201 case RID_COMPLEX:
8202 case RID_IN:
8203 case RID_OUT:
8204 case RID_INOUT:
8205 case RID_BYCOPY:
8206 case RID_BYREF:
8207 case RID_ONEWAY:
8208 case RID_INT:
8209 case RID_CHAR:
8210 case RID_FLOAT:
8211 case RID_DOUBLE:
8212 case RID_VOID:
8213 case RID_BOOL:
8214 c_parser_consume_token (parser);
8215 return value;
8216 default:
8217 return NULL_TREE;
8221 /* Parse an objc-selector-arg.
8223 objc-selector-arg:
8224 objc-selector
8225 objc-keywordname-list
8227 objc-keywordname-list:
8228 objc-keywordname
8229 objc-keywordname-list objc-keywordname
8231 objc-keywordname:
8232 objc-selector :
8236 static tree
8237 c_parser_objc_selector_arg (c_parser *parser)
8239 tree sel = c_parser_objc_selector (parser);
8240 tree list = NULL_TREE;
8241 if (sel && c_parser_next_token_is_not (parser, CPP_COLON))
8242 return sel;
8243 while (true)
8245 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
8246 return list;
8247 list = chainon (list, build_tree_list (sel, NULL_TREE));
8248 sel = c_parser_objc_selector (parser);
8249 if (!sel && c_parser_next_token_is_not (parser, CPP_COLON))
8250 break;
8252 return list;
8255 /* Parse an objc-receiver.
8257 objc-receiver:
8258 expression
8259 class-name
8260 type-name
8263 static tree
8264 c_parser_objc_receiver (c_parser *parser)
8266 if (c_parser_peek_token (parser)->type == CPP_NAME
8267 && (c_parser_peek_token (parser)->id_kind == C_ID_TYPENAME
8268 || c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME))
8270 tree id = c_parser_peek_token (parser)->value;
8271 c_parser_consume_token (parser);
8272 return objc_get_class_reference (id);
8274 return c_fully_fold (c_parser_expression (parser).value, false, NULL);
8277 /* Parse objc-message-args.
8279 objc-message-args:
8280 objc-selector
8281 objc-keywordarg-list
8283 objc-keywordarg-list:
8284 objc-keywordarg
8285 objc-keywordarg-list objc-keywordarg
8287 objc-keywordarg:
8288 objc-selector : objc-keywordexpr
8289 : objc-keywordexpr
8292 static tree
8293 c_parser_objc_message_args (c_parser *parser)
8295 tree sel = c_parser_objc_selector (parser);
8296 tree list = NULL_TREE;
8297 if (sel && c_parser_next_token_is_not (parser, CPP_COLON))
8298 return sel;
8299 while (true)
8301 tree keywordexpr;
8302 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
8303 return error_mark_node;
8304 keywordexpr = c_parser_objc_keywordexpr (parser);
8305 list = chainon (list, build_tree_list (sel, keywordexpr));
8306 sel = c_parser_objc_selector (parser);
8307 if (!sel && c_parser_next_token_is_not (parser, CPP_COLON))
8308 break;
8310 return list;
8313 /* Parse an objc-keywordexpr.
8315 objc-keywordexpr:
8316 nonempty-expr-list
8319 static tree
8320 c_parser_objc_keywordexpr (c_parser *parser)
8322 tree ret;
8323 vec<tree, va_gc> *expr_list = c_parser_expr_list (parser, true, true,
8324 NULL, NULL, NULL);
8325 if (vec_safe_length (expr_list) == 1)
8327 /* Just return the expression, remove a level of
8328 indirection. */
8329 ret = (*expr_list)[0];
8331 else
8333 /* We have a comma expression, we will collapse later. */
8334 ret = build_tree_list_vec (expr_list);
8336 release_tree_vector (expr_list);
8337 return ret;
8340 /* A check, needed in several places, that ObjC interface, implementation or
8341 method definitions are not prefixed by incorrect items. */
8342 static bool
8343 c_parser_objc_diagnose_bad_element_prefix (c_parser *parser,
8344 struct c_declspecs *specs)
8346 if (!specs->declspecs_seen_p || specs->non_sc_seen_p
8347 || specs->typespec_kind != ctsk_none)
8349 c_parser_error (parser,
8350 "no type or storage class may be specified here,");
8351 c_parser_skip_to_end_of_block_or_statement (parser);
8352 return true;
8354 return false;
8357 /* Parse an Objective-C @property declaration. The syntax is:
8359 objc-property-declaration:
8360 '@property' objc-property-attributes[opt] struct-declaration ;
8362 objc-property-attributes:
8363 '(' objc-property-attribute-list ')'
8365 objc-property-attribute-list:
8366 objc-property-attribute
8367 objc-property-attribute-list, objc-property-attribute
8369 objc-property-attribute
8370 'getter' = identifier
8371 'setter' = identifier
8372 'readonly'
8373 'readwrite'
8374 'assign'
8375 'retain'
8376 'copy'
8377 'nonatomic'
8379 For example:
8380 @property NSString *name;
8381 @property (readonly) id object;
8382 @property (retain, nonatomic, getter=getTheName) id name;
8383 @property int a, b, c;
8385 PS: This function is identical to cp_parser_objc_at_propery_declaration
8386 for C++. Keep them in sync. */
8387 static void
8388 c_parser_objc_at_property_declaration (c_parser *parser)
8390 /* The following variables hold the attributes of the properties as
8391 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
8392 seen. When we see an attribute, we set them to 'true' (if they
8393 are boolean properties) or to the identifier (if they have an
8394 argument, ie, for getter and setter). Note that here we only
8395 parse the list of attributes, check the syntax and accumulate the
8396 attributes that we find. objc_add_property_declaration() will
8397 then process the information. */
8398 bool property_assign = false;
8399 bool property_copy = false;
8400 tree property_getter_ident = NULL_TREE;
8401 bool property_nonatomic = false;
8402 bool property_readonly = false;
8403 bool property_readwrite = false;
8404 bool property_retain = false;
8405 tree property_setter_ident = NULL_TREE;
8407 /* 'properties' is the list of properties that we read. Usually a
8408 single one, but maybe more (eg, in "@property int a, b, c;" there
8409 are three). */
8410 tree properties;
8411 location_t loc;
8413 loc = c_parser_peek_token (parser)->location;
8414 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_PROPERTY));
8416 c_parser_consume_token (parser); /* Eat '@property'. */
8418 /* Parse the optional attribute list... */
8419 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
8421 /* Eat the '(' */
8422 c_parser_consume_token (parser);
8424 /* Property attribute keywords are valid now. */
8425 parser->objc_property_attr_context = true;
8427 while (true)
8429 bool syntax_error = false;
8430 c_token *token = c_parser_peek_token (parser);
8431 enum rid keyword;
8433 if (token->type != CPP_KEYWORD)
8435 if (token->type == CPP_CLOSE_PAREN)
8436 c_parser_error (parser, "expected identifier");
8437 else
8439 c_parser_consume_token (parser);
8440 c_parser_error (parser, "unknown property attribute");
8442 break;
8444 keyword = token->keyword;
8445 c_parser_consume_token (parser);
8446 switch (keyword)
8448 case RID_ASSIGN: property_assign = true; break;
8449 case RID_COPY: property_copy = true; break;
8450 case RID_NONATOMIC: property_nonatomic = true; break;
8451 case RID_READONLY: property_readonly = true; break;
8452 case RID_READWRITE: property_readwrite = true; break;
8453 case RID_RETAIN: property_retain = true; break;
8455 case RID_GETTER:
8456 case RID_SETTER:
8457 if (c_parser_next_token_is_not (parser, CPP_EQ))
8459 if (keyword == RID_GETTER)
8460 c_parser_error (parser,
8461 "missing %<=%> (after %<getter%> attribute)");
8462 else
8463 c_parser_error (parser,
8464 "missing %<=%> (after %<setter%> attribute)");
8465 syntax_error = true;
8466 break;
8468 c_parser_consume_token (parser); /* eat the = */
8469 if (c_parser_next_token_is_not (parser, CPP_NAME))
8471 c_parser_error (parser, "expected identifier");
8472 syntax_error = true;
8473 break;
8475 if (keyword == RID_SETTER)
8477 if (property_setter_ident != NULL_TREE)
8478 c_parser_error (parser, "the %<setter%> attribute may only be specified once");
8479 else
8480 property_setter_ident = c_parser_peek_token (parser)->value;
8481 c_parser_consume_token (parser);
8482 if (c_parser_next_token_is_not (parser, CPP_COLON))
8483 c_parser_error (parser, "setter name must terminate with %<:%>");
8484 else
8485 c_parser_consume_token (parser);
8487 else
8489 if (property_getter_ident != NULL_TREE)
8490 c_parser_error (parser, "the %<getter%> attribute may only be specified once");
8491 else
8492 property_getter_ident = c_parser_peek_token (parser)->value;
8493 c_parser_consume_token (parser);
8495 break;
8496 default:
8497 c_parser_error (parser, "unknown property attribute");
8498 syntax_error = true;
8499 break;
8502 if (syntax_error)
8503 break;
8505 if (c_parser_next_token_is (parser, CPP_COMMA))
8506 c_parser_consume_token (parser);
8507 else
8508 break;
8510 parser->objc_property_attr_context = false;
8511 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
8513 /* ... and the property declaration(s). */
8514 properties = c_parser_struct_declaration (parser);
8516 if (properties == error_mark_node)
8518 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
8519 parser->error = false;
8520 return;
8523 if (properties == NULL_TREE)
8524 c_parser_error (parser, "expected identifier");
8525 else
8527 /* Comma-separated properties are chained together in
8528 reverse order; add them one by one. */
8529 properties = nreverse (properties);
8531 for (; properties; properties = TREE_CHAIN (properties))
8532 objc_add_property_declaration (loc, copy_node (properties),
8533 property_readonly, property_readwrite,
8534 property_assign, property_retain,
8535 property_copy, property_nonatomic,
8536 property_getter_ident, property_setter_ident);
8539 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
8540 parser->error = false;
8543 /* Parse an Objective-C @synthesize declaration. The syntax is:
8545 objc-synthesize-declaration:
8546 @synthesize objc-synthesize-identifier-list ;
8548 objc-synthesize-identifier-list:
8549 objc-synthesize-identifier
8550 objc-synthesize-identifier-list, objc-synthesize-identifier
8552 objc-synthesize-identifier
8553 identifier
8554 identifier = identifier
8556 For example:
8557 @synthesize MyProperty;
8558 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
8560 PS: This function is identical to cp_parser_objc_at_synthesize_declaration
8561 for C++. Keep them in sync.
8563 static void
8564 c_parser_objc_at_synthesize_declaration (c_parser *parser)
8566 tree list = NULL_TREE;
8567 location_t loc;
8568 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_SYNTHESIZE));
8569 loc = c_parser_peek_token (parser)->location;
8571 c_parser_consume_token (parser);
8572 while (true)
8574 tree property, ivar;
8575 if (c_parser_next_token_is_not (parser, CPP_NAME))
8577 c_parser_error (parser, "expected identifier");
8578 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
8579 /* Once we find the semicolon, we can resume normal parsing.
8580 We have to reset parser->error manually because
8581 c_parser_skip_until_found() won't reset it for us if the
8582 next token is precisely a semicolon. */
8583 parser->error = false;
8584 return;
8586 property = c_parser_peek_token (parser)->value;
8587 c_parser_consume_token (parser);
8588 if (c_parser_next_token_is (parser, CPP_EQ))
8590 c_parser_consume_token (parser);
8591 if (c_parser_next_token_is_not (parser, CPP_NAME))
8593 c_parser_error (parser, "expected identifier");
8594 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
8595 parser->error = false;
8596 return;
8598 ivar = c_parser_peek_token (parser)->value;
8599 c_parser_consume_token (parser);
8601 else
8602 ivar = NULL_TREE;
8603 list = chainon (list, build_tree_list (ivar, property));
8604 if (c_parser_next_token_is (parser, CPP_COMMA))
8605 c_parser_consume_token (parser);
8606 else
8607 break;
8609 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
8610 objc_add_synthesize_declaration (loc, list);
8613 /* Parse an Objective-C @dynamic declaration. The syntax is:
8615 objc-dynamic-declaration:
8616 @dynamic identifier-list ;
8618 For example:
8619 @dynamic MyProperty;
8620 @dynamic MyProperty, AnotherProperty;
8622 PS: This function is identical to cp_parser_objc_at_dynamic_declaration
8623 for C++. Keep them in sync.
8625 static void
8626 c_parser_objc_at_dynamic_declaration (c_parser *parser)
8628 tree list = NULL_TREE;
8629 location_t loc;
8630 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_DYNAMIC));
8631 loc = c_parser_peek_token (parser)->location;
8633 c_parser_consume_token (parser);
8634 while (true)
8636 tree property;
8637 if (c_parser_next_token_is_not (parser, CPP_NAME))
8639 c_parser_error (parser, "expected identifier");
8640 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
8641 parser->error = false;
8642 return;
8644 property = c_parser_peek_token (parser)->value;
8645 list = chainon (list, build_tree_list (NULL_TREE, property));
8646 c_parser_consume_token (parser);
8647 if (c_parser_next_token_is (parser, CPP_COMMA))
8648 c_parser_consume_token (parser);
8649 else
8650 break;
8652 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
8653 objc_add_dynamic_declaration (loc, list);
8657 /* Handle pragmas. Some OpenMP pragmas are associated with, and therefore
8658 should be considered, statements. ALLOW_STMT is true if we're within
8659 the context of a function and such pragmas are to be allowed. Returns
8660 true if we actually parsed such a pragma. */
8662 static bool
8663 c_parser_pragma (c_parser *parser, enum pragma_context context)
8665 unsigned int id;
8667 id = c_parser_peek_token (parser)->pragma_kind;
8668 gcc_assert (id != PRAGMA_NONE);
8670 switch (id)
8672 case PRAGMA_OMP_BARRIER:
8673 if (context != pragma_compound)
8675 if (context == pragma_stmt)
8676 c_parser_error (parser, "%<#pragma omp barrier%> may only be "
8677 "used in compound statements");
8678 goto bad_stmt;
8680 c_parser_omp_barrier (parser);
8681 return false;
8683 case PRAGMA_OMP_FLUSH:
8684 if (context != pragma_compound)
8686 if (context == pragma_stmt)
8687 c_parser_error (parser, "%<#pragma omp flush%> may only be "
8688 "used in compound statements");
8689 goto bad_stmt;
8691 c_parser_omp_flush (parser);
8692 return false;
8694 case PRAGMA_OMP_TASKWAIT:
8695 if (context != pragma_compound)
8697 if (context == pragma_stmt)
8698 c_parser_error (parser, "%<#pragma omp taskwait%> may only be "
8699 "used in compound statements");
8700 goto bad_stmt;
8702 c_parser_omp_taskwait (parser);
8703 return false;
8705 case PRAGMA_OMP_TASKYIELD:
8706 if (context != pragma_compound)
8708 if (context == pragma_stmt)
8709 c_parser_error (parser, "%<#pragma omp taskyield%> may only be "
8710 "used in compound statements");
8711 goto bad_stmt;
8713 c_parser_omp_taskyield (parser);
8714 return false;
8716 case PRAGMA_OMP_THREADPRIVATE:
8717 c_parser_omp_threadprivate (parser);
8718 return false;
8720 case PRAGMA_OMP_SECTION:
8721 error_at (c_parser_peek_token (parser)->location,
8722 "%<#pragma omp section%> may only be used in "
8723 "%<#pragma omp sections%> construct");
8724 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
8725 return false;
8727 case PRAGMA_GCC_PCH_PREPROCESS:
8728 c_parser_error (parser, "%<#pragma GCC pch_preprocess%> must be first");
8729 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
8730 return false;
8732 default:
8733 if (id < PRAGMA_FIRST_EXTERNAL)
8735 if (context == pragma_external)
8737 bad_stmt:
8738 c_parser_error (parser, "expected declaration specifiers");
8739 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
8740 return false;
8742 c_parser_omp_construct (parser);
8743 return true;
8745 break;
8748 c_parser_consume_pragma (parser);
8749 c_invoke_pragma_handler (id);
8751 /* Skip to EOL, but suppress any error message. Those will have been
8752 generated by the handler routine through calling error, as opposed
8753 to calling c_parser_error. */
8754 parser->error = true;
8755 c_parser_skip_to_pragma_eol (parser);
8757 return false;
8760 /* The interface the pragma parsers have to the lexer. */
8762 enum cpp_ttype
8763 pragma_lex (tree *value)
8765 c_token *tok = c_parser_peek_token (the_parser);
8766 enum cpp_ttype ret = tok->type;
8768 *value = tok->value;
8769 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
8770 ret = CPP_EOF;
8771 else
8773 if (ret == CPP_KEYWORD)
8774 ret = CPP_NAME;
8775 c_parser_consume_token (the_parser);
8778 return ret;
8781 static void
8782 c_parser_pragma_pch_preprocess (c_parser *parser)
8784 tree name = NULL;
8786 c_parser_consume_pragma (parser);
8787 if (c_parser_next_token_is (parser, CPP_STRING))
8789 name = c_parser_peek_token (parser)->value;
8790 c_parser_consume_token (parser);
8792 else
8793 c_parser_error (parser, "expected string literal");
8794 c_parser_skip_to_pragma_eol (parser);
8796 if (name)
8797 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
8800 /* OpenMP 2.5 parsing routines. */
8802 /* Returns name of the next clause.
8803 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
8804 the token is not consumed. Otherwise appropriate pragma_omp_clause is
8805 returned and the token is consumed. */
8807 static pragma_omp_clause
8808 c_parser_omp_clause_name (c_parser *parser)
8810 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
8812 if (c_parser_next_token_is_keyword (parser, RID_IF))
8813 result = PRAGMA_OMP_CLAUSE_IF;
8814 else if (c_parser_next_token_is_keyword (parser, RID_DEFAULT))
8815 result = PRAGMA_OMP_CLAUSE_DEFAULT;
8816 else if (c_parser_next_token_is (parser, CPP_NAME))
8818 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
8820 switch (p[0])
8822 case 'c':
8823 if (!strcmp ("collapse", p))
8824 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
8825 else if (!strcmp ("copyin", p))
8826 result = PRAGMA_OMP_CLAUSE_COPYIN;
8827 else if (!strcmp ("copyprivate", p))
8828 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
8829 break;
8830 case 'f':
8831 if (!strcmp ("final", p))
8832 result = PRAGMA_OMP_CLAUSE_FINAL;
8833 else if (!strcmp ("firstprivate", p))
8834 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
8835 break;
8836 case 'l':
8837 if (!strcmp ("lastprivate", p))
8838 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
8839 break;
8840 case 'm':
8841 if (!strcmp ("mergeable", p))
8842 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
8843 break;
8844 case 'n':
8845 if (!strcmp ("nowait", p))
8846 result = PRAGMA_OMP_CLAUSE_NOWAIT;
8847 else if (!strcmp ("num_threads", p))
8848 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
8849 break;
8850 case 'o':
8851 if (!strcmp ("ordered", p))
8852 result = PRAGMA_OMP_CLAUSE_ORDERED;
8853 break;
8854 case 'p':
8855 if (!strcmp ("private", p))
8856 result = PRAGMA_OMP_CLAUSE_PRIVATE;
8857 break;
8858 case 'r':
8859 if (!strcmp ("reduction", p))
8860 result = PRAGMA_OMP_CLAUSE_REDUCTION;
8861 break;
8862 case 's':
8863 if (!strcmp ("schedule", p))
8864 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
8865 else if (!strcmp ("shared", p))
8866 result = PRAGMA_OMP_CLAUSE_SHARED;
8867 break;
8868 case 'u':
8869 if (!strcmp ("untied", p))
8870 result = PRAGMA_OMP_CLAUSE_UNTIED;
8871 break;
8875 if (result != PRAGMA_OMP_CLAUSE_NONE)
8876 c_parser_consume_token (parser);
8878 return result;
8881 /* Validate that a clause of the given type does not already exist. */
8883 static void
8884 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
8885 const char *name)
8887 tree c;
8889 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
8890 if (OMP_CLAUSE_CODE (c) == code)
8892 location_t loc = OMP_CLAUSE_LOCATION (c);
8893 error_at (loc, "too many %qs clauses", name);
8894 break;
8898 /* OpenMP 2.5:
8899 variable-list:
8900 identifier
8901 variable-list , identifier
8903 If KIND is nonzero, create the appropriate node and install the
8904 decl in OMP_CLAUSE_DECL and add the node to the head of the list.
8905 If KIND is nonzero, CLAUSE_LOC is the location of the clause.
8907 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
8908 return the list created. */
8910 static tree
8911 c_parser_omp_variable_list (c_parser *parser,
8912 location_t clause_loc,
8913 enum omp_clause_code kind,
8914 tree list)
8916 if (c_parser_next_token_is_not (parser, CPP_NAME)
8917 || c_parser_peek_token (parser)->id_kind != C_ID_ID)
8918 c_parser_error (parser, "expected identifier");
8920 while (c_parser_next_token_is (parser, CPP_NAME)
8921 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
8923 tree t = lookup_name (c_parser_peek_token (parser)->value);
8925 if (t == NULL_TREE)
8926 undeclared_variable (c_parser_peek_token (parser)->location,
8927 c_parser_peek_token (parser)->value);
8928 else if (t == error_mark_node)
8930 else if (kind != 0)
8932 tree u = build_omp_clause (clause_loc, kind);
8933 OMP_CLAUSE_DECL (u) = t;
8934 OMP_CLAUSE_CHAIN (u) = list;
8935 list = u;
8937 else
8938 list = tree_cons (t, NULL_TREE, list);
8940 c_parser_consume_token (parser);
8942 if (c_parser_next_token_is_not (parser, CPP_COMMA))
8943 break;
8945 c_parser_consume_token (parser);
8948 return list;
8951 /* Similarly, but expect leading and trailing parenthesis. This is a very
8952 common case for omp clauses. */
8954 static tree
8955 c_parser_omp_var_list_parens (c_parser *parser, enum omp_clause_code kind,
8956 tree list)
8958 /* The clauses location. */
8959 location_t loc = c_parser_peek_token (parser)->location;
8961 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
8963 list = c_parser_omp_variable_list (parser, loc, kind, list);
8964 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
8966 return list;
8969 /* OpenMP 3.0:
8970 collapse ( constant-expression ) */
8972 static tree
8973 c_parser_omp_clause_collapse (c_parser *parser, tree list)
8975 tree c, num = error_mark_node;
8976 HOST_WIDE_INT n;
8977 location_t loc;
8979 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse");
8981 loc = c_parser_peek_token (parser)->location;
8982 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
8984 num = c_parser_expr_no_commas (parser, NULL).value;
8985 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
8987 if (num == error_mark_node)
8988 return list;
8989 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
8990 || !host_integerp (num, 0)
8991 || (n = tree_low_cst (num, 0)) <= 0
8992 || (int) n != n)
8994 error_at (loc,
8995 "collapse argument needs positive constant integer expression");
8996 return list;
8998 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
8999 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
9000 OMP_CLAUSE_CHAIN (c) = list;
9001 return c;
9004 /* OpenMP 2.5:
9005 copyin ( variable-list ) */
9007 static tree
9008 c_parser_omp_clause_copyin (c_parser *parser, tree list)
9010 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_COPYIN, list);
9013 /* OpenMP 2.5:
9014 copyprivate ( variable-list ) */
9016 static tree
9017 c_parser_omp_clause_copyprivate (c_parser *parser, tree list)
9019 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_COPYPRIVATE, list);
9022 /* OpenMP 2.5:
9023 default ( shared | none ) */
9025 static tree
9026 c_parser_omp_clause_default (c_parser *parser, tree list)
9028 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
9029 location_t loc = c_parser_peek_token (parser)->location;
9030 tree c;
9032 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
9033 return list;
9034 if (c_parser_next_token_is (parser, CPP_NAME))
9036 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
9038 switch (p[0])
9040 case 'n':
9041 if (strcmp ("none", p) != 0)
9042 goto invalid_kind;
9043 kind = OMP_CLAUSE_DEFAULT_NONE;
9044 break;
9046 case 's':
9047 if (strcmp ("shared", p) != 0)
9048 goto invalid_kind;
9049 kind = OMP_CLAUSE_DEFAULT_SHARED;
9050 break;
9052 default:
9053 goto invalid_kind;
9056 c_parser_consume_token (parser);
9058 else
9060 invalid_kind:
9061 c_parser_error (parser, "expected %<none%> or %<shared%>");
9063 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
9065 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
9066 return list;
9068 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default");
9069 c = build_omp_clause (loc, OMP_CLAUSE_DEFAULT);
9070 OMP_CLAUSE_CHAIN (c) = list;
9071 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
9073 return c;
9076 /* OpenMP 2.5:
9077 firstprivate ( variable-list ) */
9079 static tree
9080 c_parser_omp_clause_firstprivate (c_parser *parser, tree list)
9082 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_FIRSTPRIVATE, list);
9085 /* OpenMP 3.1:
9086 final ( expression ) */
9088 static tree
9089 c_parser_omp_clause_final (c_parser *parser, tree list)
9091 location_t loc = c_parser_peek_token (parser)->location;
9092 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
9094 tree t = c_parser_paren_condition (parser);
9095 tree c;
9097 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final");
9099 c = build_omp_clause (loc, OMP_CLAUSE_FINAL);
9100 OMP_CLAUSE_FINAL_EXPR (c) = t;
9101 OMP_CLAUSE_CHAIN (c) = list;
9102 list = c;
9104 else
9105 c_parser_error (parser, "expected %<(%>");
9107 return list;
9110 /* OpenMP 2.5:
9111 if ( expression ) */
9113 static tree
9114 c_parser_omp_clause_if (c_parser *parser, tree list)
9116 location_t loc = c_parser_peek_token (parser)->location;
9117 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
9119 tree t = c_parser_paren_condition (parser);
9120 tree c;
9122 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if");
9124 c = build_omp_clause (loc, OMP_CLAUSE_IF);
9125 OMP_CLAUSE_IF_EXPR (c) = t;
9126 OMP_CLAUSE_CHAIN (c) = list;
9127 list = c;
9129 else
9130 c_parser_error (parser, "expected %<(%>");
9132 return list;
9135 /* OpenMP 2.5:
9136 lastprivate ( variable-list ) */
9138 static tree
9139 c_parser_omp_clause_lastprivate (c_parser *parser, tree list)
9141 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_LASTPRIVATE, list);
9144 /* OpenMP 3.1:
9145 mergeable */
9147 static tree
9148 c_parser_omp_clause_mergeable (c_parser *parser ATTRIBUTE_UNUSED, tree list)
9150 tree c;
9152 /* FIXME: Should we allow duplicates? */
9153 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable");
9155 c = build_omp_clause (c_parser_peek_token (parser)->location,
9156 OMP_CLAUSE_MERGEABLE);
9157 OMP_CLAUSE_CHAIN (c) = list;
9159 return c;
9162 /* OpenMP 2.5:
9163 nowait */
9165 static tree
9166 c_parser_omp_clause_nowait (c_parser *parser ATTRIBUTE_UNUSED, tree list)
9168 tree c;
9169 location_t loc = c_parser_peek_token (parser)->location;
9171 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait");
9173 c = build_omp_clause (loc, OMP_CLAUSE_NOWAIT);
9174 OMP_CLAUSE_CHAIN (c) = list;
9175 return c;
9178 /* OpenMP 2.5:
9179 num_threads ( expression ) */
9181 static tree
9182 c_parser_omp_clause_num_threads (c_parser *parser, tree list)
9184 location_t num_threads_loc = c_parser_peek_token (parser)->location;
9185 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
9187 location_t expr_loc = c_parser_peek_token (parser)->location;
9188 tree c, t = c_parser_expression (parser).value;
9189 mark_exp_read (t);
9190 t = c_fully_fold (t, false, NULL);
9192 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
9194 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
9196 c_parser_error (parser, "expected integer expression");
9197 return list;
9200 /* Attempt to statically determine when the number isn't positive. */
9201 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
9202 build_int_cst (TREE_TYPE (t), 0));
9203 if (CAN_HAVE_LOCATION_P (c))
9204 SET_EXPR_LOCATION (c, expr_loc);
9205 if (c == boolean_true_node)
9207 warning_at (expr_loc, 0,
9208 "%<num_threads%> value must be positive");
9209 t = integer_one_node;
9212 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS, "num_threads");
9214 c = build_omp_clause (num_threads_loc, OMP_CLAUSE_NUM_THREADS);
9215 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
9216 OMP_CLAUSE_CHAIN (c) = list;
9217 list = c;
9220 return list;
9223 /* OpenMP 2.5:
9224 ordered */
9226 static tree
9227 c_parser_omp_clause_ordered (c_parser *parser, tree list)
9229 tree c;
9231 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED, "ordered");
9233 c = build_omp_clause (c_parser_peek_token (parser)->location,
9234 OMP_CLAUSE_ORDERED);
9235 OMP_CLAUSE_CHAIN (c) = list;
9237 return c;
9240 /* OpenMP 2.5:
9241 private ( variable-list ) */
9243 static tree
9244 c_parser_omp_clause_private (c_parser *parser, tree list)
9246 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_PRIVATE, list);
9249 /* OpenMP 2.5:
9250 reduction ( reduction-operator : variable-list )
9252 reduction-operator:
9253 One of: + * - & ^ | && ||
9255 OpenMP 3.1:
9257 reduction-operator:
9258 One of: + * - & ^ | && || max min */
9260 static tree
9261 c_parser_omp_clause_reduction (c_parser *parser, tree list)
9263 location_t clause_loc = c_parser_peek_token (parser)->location;
9264 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
9266 enum tree_code code;
9268 switch (c_parser_peek_token (parser)->type)
9270 case CPP_PLUS:
9271 code = PLUS_EXPR;
9272 break;
9273 case CPP_MULT:
9274 code = MULT_EXPR;
9275 break;
9276 case CPP_MINUS:
9277 code = MINUS_EXPR;
9278 break;
9279 case CPP_AND:
9280 code = BIT_AND_EXPR;
9281 break;
9282 case CPP_XOR:
9283 code = BIT_XOR_EXPR;
9284 break;
9285 case CPP_OR:
9286 code = BIT_IOR_EXPR;
9287 break;
9288 case CPP_AND_AND:
9289 code = TRUTH_ANDIF_EXPR;
9290 break;
9291 case CPP_OR_OR:
9292 code = TRUTH_ORIF_EXPR;
9293 break;
9294 case CPP_NAME:
9296 const char *p
9297 = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
9298 if (strcmp (p, "min") == 0)
9300 code = MIN_EXPR;
9301 break;
9303 if (strcmp (p, "max") == 0)
9305 code = MAX_EXPR;
9306 break;
9309 /* FALLTHRU */
9310 default:
9311 c_parser_error (parser,
9312 "expected %<+%>, %<*%>, %<-%>, %<&%>, "
9313 "%<^%>, %<|%>, %<&&%>, %<||%>, %<min%> or %<max%>");
9314 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 0);
9315 return list;
9317 c_parser_consume_token (parser);
9318 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
9320 tree nl, c;
9322 nl = c_parser_omp_variable_list (parser, clause_loc,
9323 OMP_CLAUSE_REDUCTION, list);
9324 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
9325 OMP_CLAUSE_REDUCTION_CODE (c) = code;
9327 list = nl;
9329 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
9331 return list;
9334 /* OpenMP 2.5:
9335 schedule ( schedule-kind )
9336 schedule ( schedule-kind , expression )
9338 schedule-kind:
9339 static | dynamic | guided | runtime | auto
9342 static tree
9343 c_parser_omp_clause_schedule (c_parser *parser, tree list)
9345 tree c, t;
9346 location_t loc = c_parser_peek_token (parser)->location;
9348 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
9349 return list;
9351 c = build_omp_clause (loc, OMP_CLAUSE_SCHEDULE);
9353 if (c_parser_next_token_is (parser, CPP_NAME))
9355 tree kind = c_parser_peek_token (parser)->value;
9356 const char *p = IDENTIFIER_POINTER (kind);
9358 switch (p[0])
9360 case 'd':
9361 if (strcmp ("dynamic", p) != 0)
9362 goto invalid_kind;
9363 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
9364 break;
9366 case 'g':
9367 if (strcmp ("guided", p) != 0)
9368 goto invalid_kind;
9369 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
9370 break;
9372 case 'r':
9373 if (strcmp ("runtime", p) != 0)
9374 goto invalid_kind;
9375 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
9376 break;
9378 default:
9379 goto invalid_kind;
9382 else if (c_parser_next_token_is_keyword (parser, RID_STATIC))
9383 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
9384 else if (c_parser_next_token_is_keyword (parser, RID_AUTO))
9385 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
9386 else
9387 goto invalid_kind;
9389 c_parser_consume_token (parser);
9390 if (c_parser_next_token_is (parser, CPP_COMMA))
9392 location_t here;
9393 c_parser_consume_token (parser);
9395 here = c_parser_peek_token (parser)->location;
9396 t = c_parser_expr_no_commas (parser, NULL).value;
9397 mark_exp_read (t);
9398 t = c_fully_fold (t, false, NULL);
9400 if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
9401 error_at (here, "schedule %<runtime%> does not take "
9402 "a %<chunk_size%> parameter");
9403 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
9404 error_at (here,
9405 "schedule %<auto%> does not take "
9406 "a %<chunk_size%> parameter");
9407 else if (TREE_CODE (TREE_TYPE (t)) == INTEGER_TYPE)
9408 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
9409 else
9410 c_parser_error (parser, "expected integer expression");
9412 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
9414 else
9415 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
9416 "expected %<,%> or %<)%>");
9418 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule");
9419 OMP_CLAUSE_CHAIN (c) = list;
9420 return c;
9422 invalid_kind:
9423 c_parser_error (parser, "invalid schedule kind");
9424 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 0);
9425 return list;
9428 /* OpenMP 2.5:
9429 shared ( variable-list ) */
9431 static tree
9432 c_parser_omp_clause_shared (c_parser *parser, tree list)
9434 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_SHARED, list);
9437 /* OpenMP 3.0:
9438 untied */
9440 static tree
9441 c_parser_omp_clause_untied (c_parser *parser ATTRIBUTE_UNUSED, tree list)
9443 tree c;
9445 /* FIXME: Should we allow duplicates? */
9446 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied");
9448 c = build_omp_clause (c_parser_peek_token (parser)->location,
9449 OMP_CLAUSE_UNTIED);
9450 OMP_CLAUSE_CHAIN (c) = list;
9452 return c;
9455 /* Parse all OpenMP clauses. The set clauses allowed by the directive
9456 is a bitmask in MASK. Return the list of clauses found; the result
9457 of clause default goes in *pdefault. */
9459 static tree
9460 c_parser_omp_all_clauses (c_parser *parser, unsigned int mask,
9461 const char *where)
9463 tree clauses = NULL;
9464 bool first = true;
9466 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
9468 location_t here;
9469 pragma_omp_clause c_kind;
9470 const char *c_name;
9471 tree prev = clauses;
9473 if (!first && c_parser_next_token_is (parser, CPP_COMMA))
9474 c_parser_consume_token (parser);
9476 first = false;
9477 here = c_parser_peek_token (parser)->location;
9478 c_kind = c_parser_omp_clause_name (parser);
9480 switch (c_kind)
9482 case PRAGMA_OMP_CLAUSE_COLLAPSE:
9483 clauses = c_parser_omp_clause_collapse (parser, clauses);
9484 c_name = "collapse";
9485 break;
9486 case PRAGMA_OMP_CLAUSE_COPYIN:
9487 clauses = c_parser_omp_clause_copyin (parser, clauses);
9488 c_name = "copyin";
9489 break;
9490 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
9491 clauses = c_parser_omp_clause_copyprivate (parser, clauses);
9492 c_name = "copyprivate";
9493 break;
9494 case PRAGMA_OMP_CLAUSE_DEFAULT:
9495 clauses = c_parser_omp_clause_default (parser, clauses);
9496 c_name = "default";
9497 break;
9498 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
9499 clauses = c_parser_omp_clause_firstprivate (parser, clauses);
9500 c_name = "firstprivate";
9501 break;
9502 case PRAGMA_OMP_CLAUSE_FINAL:
9503 clauses = c_parser_omp_clause_final (parser, clauses);
9504 c_name = "final";
9505 break;
9506 case PRAGMA_OMP_CLAUSE_IF:
9507 clauses = c_parser_omp_clause_if (parser, clauses);
9508 c_name = "if";
9509 break;
9510 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
9511 clauses = c_parser_omp_clause_lastprivate (parser, clauses);
9512 c_name = "lastprivate";
9513 break;
9514 case PRAGMA_OMP_CLAUSE_MERGEABLE:
9515 clauses = c_parser_omp_clause_mergeable (parser, clauses);
9516 c_name = "mergeable";
9517 break;
9518 case PRAGMA_OMP_CLAUSE_NOWAIT:
9519 clauses = c_parser_omp_clause_nowait (parser, clauses);
9520 c_name = "nowait";
9521 break;
9522 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
9523 clauses = c_parser_omp_clause_num_threads (parser, clauses);
9524 c_name = "num_threads";
9525 break;
9526 case PRAGMA_OMP_CLAUSE_ORDERED:
9527 clauses = c_parser_omp_clause_ordered (parser, clauses);
9528 c_name = "ordered";
9529 break;
9530 case PRAGMA_OMP_CLAUSE_PRIVATE:
9531 clauses = c_parser_omp_clause_private (parser, clauses);
9532 c_name = "private";
9533 break;
9534 case PRAGMA_OMP_CLAUSE_REDUCTION:
9535 clauses = c_parser_omp_clause_reduction (parser, clauses);
9536 c_name = "reduction";
9537 break;
9538 case PRAGMA_OMP_CLAUSE_SCHEDULE:
9539 clauses = c_parser_omp_clause_schedule (parser, clauses);
9540 c_name = "schedule";
9541 break;
9542 case PRAGMA_OMP_CLAUSE_SHARED:
9543 clauses = c_parser_omp_clause_shared (parser, clauses);
9544 c_name = "shared";
9545 break;
9546 case PRAGMA_OMP_CLAUSE_UNTIED:
9547 clauses = c_parser_omp_clause_untied (parser, clauses);
9548 c_name = "untied";
9549 break;
9550 default:
9551 c_parser_error (parser, "expected %<#pragma omp%> clause");
9552 goto saw_error;
9555 if (((mask >> c_kind) & 1) == 0 && !parser->error)
9557 /* Remove the invalid clause(s) from the list to avoid
9558 confusing the rest of the compiler. */
9559 clauses = prev;
9560 error_at (here, "%qs is not valid for %qs", c_name, where);
9564 saw_error:
9565 c_parser_skip_to_pragma_eol (parser);
9567 return c_finish_omp_clauses (clauses);
9570 /* OpenMP 2.5:
9571 structured-block:
9572 statement
9574 In practice, we're also interested in adding the statement to an
9575 outer node. So it is convenient if we work around the fact that
9576 c_parser_statement calls add_stmt. */
9578 static tree
9579 c_parser_omp_structured_block (c_parser *parser)
9581 tree stmt = push_stmt_list ();
9582 c_parser_statement (parser);
9583 return pop_stmt_list (stmt);
9586 /* OpenMP 2.5:
9587 # pragma omp atomic new-line
9588 expression-stmt
9590 expression-stmt:
9591 x binop= expr | x++ | ++x | x-- | --x
9592 binop:
9593 +, *, -, /, &, ^, |, <<, >>
9595 where x is an lvalue expression with scalar type.
9597 OpenMP 3.1:
9598 # pragma omp atomic new-line
9599 update-stmt
9601 # pragma omp atomic read new-line
9602 read-stmt
9604 # pragma omp atomic write new-line
9605 write-stmt
9607 # pragma omp atomic update new-line
9608 update-stmt
9610 # pragma omp atomic capture new-line
9611 capture-stmt
9613 # pragma omp atomic capture new-line
9614 capture-block
9616 read-stmt:
9617 v = x
9618 write-stmt:
9619 x = expr
9620 update-stmt:
9621 expression-stmt | x = x binop expr
9622 capture-stmt:
9623 v = x binop= expr | v = x++ | v = ++x | v = x-- | v = --x
9624 capture-block:
9625 { v = x; update-stmt; } | { update-stmt; v = x; }
9627 where x and v are lvalue expressions with scalar type.
9629 LOC is the location of the #pragma token. */
9631 static void
9632 c_parser_omp_atomic (location_t loc, c_parser *parser)
9634 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE;
9635 tree lhs1 = NULL_TREE, rhs1 = NULL_TREE;
9636 tree stmt, orig_lhs;
9637 enum tree_code code = OMP_ATOMIC, opcode = NOP_EXPR;
9638 struct c_expr rhs_expr;
9639 bool structured_block = false;
9641 if (c_parser_next_token_is (parser, CPP_NAME))
9643 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
9645 if (!strcmp (p, "read"))
9646 code = OMP_ATOMIC_READ;
9647 else if (!strcmp (p, "write"))
9648 code = NOP_EXPR;
9649 else if (!strcmp (p, "update"))
9650 code = OMP_ATOMIC;
9651 else if (!strcmp (p, "capture"))
9652 code = OMP_ATOMIC_CAPTURE_NEW;
9653 else
9654 p = NULL;
9655 if (p)
9656 c_parser_consume_token (parser);
9658 c_parser_skip_to_pragma_eol (parser);
9660 switch (code)
9662 case OMP_ATOMIC_READ:
9663 case NOP_EXPR: /* atomic write */
9664 v = c_parser_unary_expression (parser).value;
9665 v = c_fully_fold (v, false, NULL);
9666 if (v == error_mark_node)
9667 goto saw_error;
9668 loc = c_parser_peek_token (parser)->location;
9669 if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
9670 goto saw_error;
9671 if (code == NOP_EXPR)
9672 lhs = c_parser_expression (parser).value;
9673 else
9674 lhs = c_parser_unary_expression (parser).value;
9675 lhs = c_fully_fold (lhs, false, NULL);
9676 if (lhs == error_mark_node)
9677 goto saw_error;
9678 if (code == NOP_EXPR)
9680 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
9681 opcode. */
9682 code = OMP_ATOMIC;
9683 rhs = lhs;
9684 lhs = v;
9685 v = NULL_TREE;
9687 goto done;
9688 case OMP_ATOMIC_CAPTURE_NEW:
9689 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
9691 c_parser_consume_token (parser);
9692 structured_block = true;
9694 else
9696 v = c_parser_unary_expression (parser).value;
9697 v = c_fully_fold (v, false, NULL);
9698 if (v == error_mark_node)
9699 goto saw_error;
9700 if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
9701 goto saw_error;
9703 break;
9704 default:
9705 break;
9708 /* For structured_block case we don't know yet whether
9709 old or new x should be captured. */
9710 restart:
9711 lhs = c_parser_unary_expression (parser).value;
9712 lhs = c_fully_fold (lhs, false, NULL);
9713 orig_lhs = lhs;
9714 switch (TREE_CODE (lhs))
9716 case ERROR_MARK:
9717 saw_error:
9718 c_parser_skip_to_end_of_block_or_statement (parser);
9719 if (structured_block)
9721 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
9722 c_parser_consume_token (parser);
9723 else if (code == OMP_ATOMIC_CAPTURE_NEW)
9725 c_parser_skip_to_end_of_block_or_statement (parser);
9726 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
9727 c_parser_consume_token (parser);
9730 return;
9732 case POSTINCREMENT_EXPR:
9733 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
9734 code = OMP_ATOMIC_CAPTURE_OLD;
9735 /* FALLTHROUGH */
9736 case PREINCREMENT_EXPR:
9737 lhs = TREE_OPERAND (lhs, 0);
9738 opcode = PLUS_EXPR;
9739 rhs = integer_one_node;
9740 break;
9742 case POSTDECREMENT_EXPR:
9743 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
9744 code = OMP_ATOMIC_CAPTURE_OLD;
9745 /* FALLTHROUGH */
9746 case PREDECREMENT_EXPR:
9747 lhs = TREE_OPERAND (lhs, 0);
9748 opcode = MINUS_EXPR;
9749 rhs = integer_one_node;
9750 break;
9752 case COMPOUND_EXPR:
9753 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
9754 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
9755 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
9756 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
9757 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
9758 (TREE_OPERAND (lhs, 1), 0), 0)))
9759 == BOOLEAN_TYPE)
9760 /* Undo effects of boolean_increment for post {in,de}crement. */
9761 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
9762 /* FALLTHRU */
9763 case MODIFY_EXPR:
9764 if (TREE_CODE (lhs) == MODIFY_EXPR
9765 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
9767 /* Undo effects of boolean_increment. */
9768 if (integer_onep (TREE_OPERAND (lhs, 1)))
9770 /* This is pre or post increment. */
9771 rhs = TREE_OPERAND (lhs, 1);
9772 lhs = TREE_OPERAND (lhs, 0);
9773 opcode = NOP_EXPR;
9774 if (code == OMP_ATOMIC_CAPTURE_NEW
9775 && !structured_block
9776 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
9777 code = OMP_ATOMIC_CAPTURE_OLD;
9778 break;
9780 if (TREE_CODE (TREE_OPERAND (lhs, 1)) == TRUTH_NOT_EXPR
9781 && TREE_OPERAND (lhs, 0)
9782 == TREE_OPERAND (TREE_OPERAND (lhs, 1), 0))
9784 /* This is pre or post decrement. */
9785 rhs = TREE_OPERAND (lhs, 1);
9786 lhs = TREE_OPERAND (lhs, 0);
9787 opcode = NOP_EXPR;
9788 if (code == OMP_ATOMIC_CAPTURE_NEW
9789 && !structured_block
9790 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
9791 code = OMP_ATOMIC_CAPTURE_OLD;
9792 break;
9795 /* FALLTHRU */
9796 default:
9797 switch (c_parser_peek_token (parser)->type)
9799 case CPP_MULT_EQ:
9800 opcode = MULT_EXPR;
9801 break;
9802 case CPP_DIV_EQ:
9803 opcode = TRUNC_DIV_EXPR;
9804 break;
9805 case CPP_PLUS_EQ:
9806 opcode = PLUS_EXPR;
9807 break;
9808 case CPP_MINUS_EQ:
9809 opcode = MINUS_EXPR;
9810 break;
9811 case CPP_LSHIFT_EQ:
9812 opcode = LSHIFT_EXPR;
9813 break;
9814 case CPP_RSHIFT_EQ:
9815 opcode = RSHIFT_EXPR;
9816 break;
9817 case CPP_AND_EQ:
9818 opcode = BIT_AND_EXPR;
9819 break;
9820 case CPP_OR_EQ:
9821 opcode = BIT_IOR_EXPR;
9822 break;
9823 case CPP_XOR_EQ:
9824 opcode = BIT_XOR_EXPR;
9825 break;
9826 case CPP_EQ:
9827 if (structured_block || code == OMP_ATOMIC)
9829 location_t aloc = c_parser_peek_token (parser)->location;
9830 location_t rhs_loc;
9831 enum c_parser_prec oprec = PREC_NONE;
9833 c_parser_consume_token (parser);
9834 rhs1 = c_parser_unary_expression (parser).value;
9835 rhs1 = c_fully_fold (rhs1, false, NULL);
9836 if (rhs1 == error_mark_node)
9837 goto saw_error;
9838 switch (c_parser_peek_token (parser)->type)
9840 case CPP_SEMICOLON:
9841 if (code == OMP_ATOMIC_CAPTURE_NEW)
9843 code = OMP_ATOMIC_CAPTURE_OLD;
9844 v = lhs;
9845 lhs = NULL_TREE;
9846 lhs1 = rhs1;
9847 rhs1 = NULL_TREE;
9848 c_parser_consume_token (parser);
9849 goto restart;
9851 c_parser_error (parser,
9852 "invalid form of %<#pragma omp atomic%>");
9853 goto saw_error;
9854 case CPP_MULT:
9855 opcode = MULT_EXPR;
9856 oprec = PREC_MULT;
9857 break;
9858 case CPP_DIV:
9859 opcode = TRUNC_DIV_EXPR;
9860 oprec = PREC_MULT;
9861 break;
9862 case CPP_PLUS:
9863 opcode = PLUS_EXPR;
9864 oprec = PREC_ADD;
9865 break;
9866 case CPP_MINUS:
9867 opcode = MINUS_EXPR;
9868 oprec = PREC_ADD;
9869 break;
9870 case CPP_LSHIFT:
9871 opcode = LSHIFT_EXPR;
9872 oprec = PREC_SHIFT;
9873 break;
9874 case CPP_RSHIFT:
9875 opcode = RSHIFT_EXPR;
9876 oprec = PREC_SHIFT;
9877 break;
9878 case CPP_AND:
9879 opcode = BIT_AND_EXPR;
9880 oprec = PREC_BITAND;
9881 break;
9882 case CPP_OR:
9883 opcode = BIT_IOR_EXPR;
9884 oprec = PREC_BITOR;
9885 break;
9886 case CPP_XOR:
9887 opcode = BIT_XOR_EXPR;
9888 oprec = PREC_BITXOR;
9889 break;
9890 default:
9891 c_parser_error (parser,
9892 "invalid operator for %<#pragma omp atomic%>");
9893 goto saw_error;
9895 loc = aloc;
9896 c_parser_consume_token (parser);
9897 rhs_loc = c_parser_peek_token (parser)->location;
9898 if (commutative_tree_code (opcode))
9899 oprec = (enum c_parser_prec) (oprec - 1);
9900 rhs_expr = c_parser_binary_expression (parser, NULL, oprec);
9901 rhs_expr = default_function_array_read_conversion (rhs_loc,
9902 rhs_expr);
9903 rhs = rhs_expr.value;
9904 rhs = c_fully_fold (rhs, false, NULL);
9905 goto stmt_done;
9907 /* FALLTHROUGH */
9908 default:
9909 c_parser_error (parser,
9910 "invalid operator for %<#pragma omp atomic%>");
9911 goto saw_error;
9914 /* Arrange to pass the location of the assignment operator to
9915 c_finish_omp_atomic. */
9916 loc = c_parser_peek_token (parser)->location;
9917 c_parser_consume_token (parser);
9919 location_t rhs_loc = c_parser_peek_token (parser)->location;
9920 rhs_expr = c_parser_expression (parser);
9921 rhs_expr = default_function_array_read_conversion (rhs_loc, rhs_expr);
9923 rhs = rhs_expr.value;
9924 rhs = c_fully_fold (rhs, false, NULL);
9925 break;
9927 stmt_done:
9928 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
9930 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
9931 goto saw_error;
9932 v = c_parser_unary_expression (parser).value;
9933 v = c_fully_fold (v, false, NULL);
9934 if (v == error_mark_node)
9935 goto saw_error;
9936 if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
9937 goto saw_error;
9938 lhs1 = c_parser_unary_expression (parser).value;
9939 lhs1 = c_fully_fold (lhs1, false, NULL);
9940 if (lhs1 == error_mark_node)
9941 goto saw_error;
9943 if (structured_block)
9945 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
9946 c_parser_require (parser, CPP_CLOSE_BRACE, "expected %<}%>");
9948 done:
9949 stmt = c_finish_omp_atomic (loc, code, opcode, lhs, rhs, v, lhs1, rhs1);
9950 if (stmt != error_mark_node)
9951 add_stmt (stmt);
9953 if (!structured_block)
9954 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
9958 /* OpenMP 2.5:
9959 # pragma omp barrier new-line
9962 static void
9963 c_parser_omp_barrier (c_parser *parser)
9965 location_t loc = c_parser_peek_token (parser)->location;
9966 c_parser_consume_pragma (parser);
9967 c_parser_skip_to_pragma_eol (parser);
9969 c_finish_omp_barrier (loc);
9972 /* OpenMP 2.5:
9973 # pragma omp critical [(name)] new-line
9974 structured-block
9976 LOC is the location of the #pragma itself. */
9978 static tree
9979 c_parser_omp_critical (location_t loc, c_parser *parser)
9981 tree stmt, name = NULL;
9983 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
9985 c_parser_consume_token (parser);
9986 if (c_parser_next_token_is (parser, CPP_NAME))
9988 name = c_parser_peek_token (parser)->value;
9989 c_parser_consume_token (parser);
9990 c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
9992 else
9993 c_parser_error (parser, "expected identifier");
9995 else if (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
9996 c_parser_error (parser, "expected %<(%> or end of line");
9997 c_parser_skip_to_pragma_eol (parser);
9999 stmt = c_parser_omp_structured_block (parser);
10000 return c_finish_omp_critical (loc, stmt, name);
10003 /* OpenMP 2.5:
10004 # pragma omp flush flush-vars[opt] new-line
10006 flush-vars:
10007 ( variable-list ) */
10009 static void
10010 c_parser_omp_flush (c_parser *parser)
10012 location_t loc = c_parser_peek_token (parser)->location;
10013 c_parser_consume_pragma (parser);
10014 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
10015 c_parser_omp_var_list_parens (parser, OMP_CLAUSE_ERROR, NULL);
10016 else if (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
10017 c_parser_error (parser, "expected %<(%> or end of line");
10018 c_parser_skip_to_pragma_eol (parser);
10020 c_finish_omp_flush (loc);
10023 /* Parse the restricted form of the for statement allowed by OpenMP.
10024 The real trick here is to determine the loop control variable early
10025 so that we can push a new decl if necessary to make it private.
10026 LOC is the location of the OMP in "#pragma omp". */
10028 static tree
10029 c_parser_omp_for_loop (location_t loc,
10030 c_parser *parser, tree clauses, tree *par_clauses)
10032 tree decl, cond, incr, save_break, save_cont, body, init, stmt, cl;
10033 tree declv, condv, incrv, initv, ret = NULL;
10034 bool fail = false, open_brace_parsed = false;
10035 int i, collapse = 1, nbraces = 0;
10036 location_t for_loc;
10037 vec<tree, va_gc> *for_block = make_tree_vector ();
10039 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
10040 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
10041 collapse = tree_low_cst (OMP_CLAUSE_COLLAPSE_EXPR (cl), 0);
10043 gcc_assert (collapse >= 1);
10045 declv = make_tree_vec (collapse);
10046 initv = make_tree_vec (collapse);
10047 condv = make_tree_vec (collapse);
10048 incrv = make_tree_vec (collapse);
10050 if (!c_parser_next_token_is_keyword (parser, RID_FOR))
10052 c_parser_error (parser, "for statement expected");
10053 return NULL;
10055 for_loc = c_parser_peek_token (parser)->location;
10056 c_parser_consume_token (parser);
10058 for (i = 0; i < collapse; i++)
10060 int bracecount = 0;
10062 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10063 goto pop_scopes;
10065 /* Parse the initialization declaration or expression. */
10066 if (c_parser_next_tokens_start_declaration (parser))
10068 if (i > 0)
10069 vec_safe_push (for_block, c_begin_compound_stmt (true));
10070 c_parser_declaration_or_fndef (parser, true, true, true, true, true, NULL);
10071 decl = check_for_loop_decls (for_loc, flag_isoc99);
10072 if (decl == NULL)
10073 goto error_init;
10074 if (DECL_INITIAL (decl) == error_mark_node)
10075 decl = error_mark_node;
10076 init = decl;
10078 else if (c_parser_next_token_is (parser, CPP_NAME)
10079 && c_parser_peek_2nd_token (parser)->type == CPP_EQ)
10081 struct c_expr decl_exp;
10082 struct c_expr init_exp;
10083 location_t init_loc;
10085 decl_exp = c_parser_postfix_expression (parser);
10086 decl = decl_exp.value;
10088 c_parser_require (parser, CPP_EQ, "expected %<=%>");
10090 init_loc = c_parser_peek_token (parser)->location;
10091 init_exp = c_parser_expr_no_commas (parser, NULL);
10092 init_exp = default_function_array_read_conversion (init_loc,
10093 init_exp);
10094 init = build_modify_expr (init_loc, decl, decl_exp.original_type,
10095 NOP_EXPR, init_loc, init_exp.value,
10096 init_exp.original_type);
10097 init = c_process_expr_stmt (init_loc, init);
10099 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
10101 else
10103 error_init:
10104 c_parser_error (parser,
10105 "expected iteration declaration or initialization");
10106 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
10107 "expected %<)%>");
10108 fail = true;
10109 goto parse_next;
10112 /* Parse the loop condition. */
10113 cond = NULL_TREE;
10114 if (c_parser_next_token_is_not (parser, CPP_SEMICOLON))
10116 location_t cond_loc = c_parser_peek_token (parser)->location;
10117 struct c_expr cond_expr = c_parser_binary_expression (parser, NULL,
10118 PREC_NONE);
10120 cond = cond_expr.value;
10121 cond = c_objc_common_truthvalue_conversion (cond_loc, cond);
10122 cond = c_fully_fold (cond, false, NULL);
10123 switch (cond_expr.original_code)
10125 case GT_EXPR:
10126 case GE_EXPR:
10127 case LT_EXPR:
10128 case LE_EXPR:
10129 break;
10130 default:
10131 /* Can't be cond = error_mark_node, because we want to preserve
10132 the location until c_finish_omp_for. */
10133 cond = build1 (NOP_EXPR, boolean_type_node, error_mark_node);
10134 break;
10136 protected_set_expr_location (cond, cond_loc);
10138 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
10140 /* Parse the increment expression. */
10141 incr = NULL_TREE;
10142 if (c_parser_next_token_is_not (parser, CPP_CLOSE_PAREN))
10144 location_t incr_loc = c_parser_peek_token (parser)->location;
10146 incr = c_process_expr_stmt (incr_loc,
10147 c_parser_expression (parser).value);
10149 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10151 if (decl == NULL || decl == error_mark_node || init == error_mark_node)
10152 fail = true;
10153 else
10155 TREE_VEC_ELT (declv, i) = decl;
10156 TREE_VEC_ELT (initv, i) = init;
10157 TREE_VEC_ELT (condv, i) = cond;
10158 TREE_VEC_ELT (incrv, i) = incr;
10161 parse_next:
10162 if (i == collapse - 1)
10163 break;
10165 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
10166 in between the collapsed for loops to be still considered perfectly
10167 nested. Hopefully the final version clarifies this.
10168 For now handle (multiple) {'s and empty statements. */
10171 if (c_parser_next_token_is_keyword (parser, RID_FOR))
10173 c_parser_consume_token (parser);
10174 break;
10176 else if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
10178 c_parser_consume_token (parser);
10179 bracecount++;
10181 else if (bracecount
10182 && c_parser_next_token_is (parser, CPP_SEMICOLON))
10183 c_parser_consume_token (parser);
10184 else
10186 c_parser_error (parser, "not enough perfectly nested loops");
10187 if (bracecount)
10189 open_brace_parsed = true;
10190 bracecount--;
10192 fail = true;
10193 collapse = 0;
10194 break;
10197 while (1);
10199 nbraces += bracecount;
10202 save_break = c_break_label;
10203 c_break_label = size_one_node;
10204 save_cont = c_cont_label;
10205 c_cont_label = NULL_TREE;
10206 body = push_stmt_list ();
10208 if (open_brace_parsed)
10210 location_t here = c_parser_peek_token (parser)->location;
10211 stmt = c_begin_compound_stmt (true);
10212 c_parser_compound_statement_nostart (parser);
10213 add_stmt (c_end_compound_stmt (here, stmt, true));
10215 else
10216 add_stmt (c_parser_c99_block_statement (parser));
10217 if (c_cont_label)
10219 tree t = build1 (LABEL_EXPR, void_type_node, c_cont_label);
10220 SET_EXPR_LOCATION (t, loc);
10221 add_stmt (t);
10224 body = pop_stmt_list (body);
10225 c_break_label = save_break;
10226 c_cont_label = save_cont;
10228 while (nbraces)
10230 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
10232 c_parser_consume_token (parser);
10233 nbraces--;
10235 else if (c_parser_next_token_is (parser, CPP_SEMICOLON))
10236 c_parser_consume_token (parser);
10237 else
10239 c_parser_error (parser, "collapsed loops not perfectly nested");
10240 while (nbraces)
10242 location_t here = c_parser_peek_token (parser)->location;
10243 stmt = c_begin_compound_stmt (true);
10244 add_stmt (body);
10245 c_parser_compound_statement_nostart (parser);
10246 body = c_end_compound_stmt (here, stmt, true);
10247 nbraces--;
10249 goto pop_scopes;
10253 /* Only bother calling c_finish_omp_for if we haven't already generated
10254 an error from the initialization parsing. */
10255 if (!fail)
10257 stmt = c_finish_omp_for (loc, declv, initv, condv, incrv, body, NULL);
10258 if (stmt)
10260 if (par_clauses != NULL)
10262 tree *c;
10263 for (c = par_clauses; *c ; )
10264 if (OMP_CLAUSE_CODE (*c) != OMP_CLAUSE_FIRSTPRIVATE
10265 && OMP_CLAUSE_CODE (*c) != OMP_CLAUSE_LASTPRIVATE)
10266 c = &OMP_CLAUSE_CHAIN (*c);
10267 else
10269 for (i = 0; i < collapse; i++)
10270 if (TREE_VEC_ELT (declv, i) == OMP_CLAUSE_DECL (*c))
10271 break;
10272 if (i == collapse)
10273 c = &OMP_CLAUSE_CHAIN (*c);
10274 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE)
10276 error_at (loc,
10277 "iteration variable %qD should not be firstprivate",
10278 OMP_CLAUSE_DECL (*c));
10279 *c = OMP_CLAUSE_CHAIN (*c);
10281 else
10283 /* Copy lastprivate (decl) clause to OMP_FOR_CLAUSES,
10284 change it to shared (decl) in
10285 OMP_PARALLEL_CLAUSES. */
10286 tree l = build_omp_clause (OMP_CLAUSE_LOCATION (*c),
10287 OMP_CLAUSE_LASTPRIVATE);
10288 OMP_CLAUSE_DECL (l) = OMP_CLAUSE_DECL (*c);
10289 OMP_CLAUSE_CHAIN (l) = clauses;
10290 clauses = l;
10291 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
10295 OMP_FOR_CLAUSES (stmt) = clauses;
10297 ret = stmt;
10299 pop_scopes:
10300 while (!for_block->is_empty ())
10302 /* FIXME diagnostics: LOC below should be the actual location of
10303 this particular for block. We need to build a list of
10304 locations to go along with FOR_BLOCK. */
10305 stmt = c_end_compound_stmt (loc, for_block->pop (), true);
10306 add_stmt (stmt);
10308 release_tree_vector (for_block);
10309 return ret;
10312 /* OpenMP 2.5:
10313 #pragma omp for for-clause[optseq] new-line
10314 for-loop
10316 LOC is the location of the #pragma token.
10319 #define OMP_FOR_CLAUSE_MASK \
10320 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
10321 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
10322 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
10323 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
10324 | (1u << PRAGMA_OMP_CLAUSE_ORDERED) \
10325 | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE) \
10326 | (1u << PRAGMA_OMP_CLAUSE_COLLAPSE) \
10327 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
10329 static tree
10330 c_parser_omp_for (location_t loc, c_parser *parser)
10332 tree block, clauses, ret;
10334 clauses = c_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
10335 "#pragma omp for");
10337 block = c_begin_compound_stmt (true);
10338 ret = c_parser_omp_for_loop (loc, parser, clauses, NULL);
10339 block = c_end_compound_stmt (loc, block, true);
10340 add_stmt (block);
10342 return ret;
10345 /* OpenMP 2.5:
10346 # pragma omp master new-line
10347 structured-block
10349 LOC is the location of the #pragma token.
10352 static tree
10353 c_parser_omp_master (location_t loc, c_parser *parser)
10355 c_parser_skip_to_pragma_eol (parser);
10356 return c_finish_omp_master (loc, c_parser_omp_structured_block (parser));
10359 /* OpenMP 2.5:
10360 # pragma omp ordered new-line
10361 structured-block
10363 LOC is the location of the #pragma itself.
10366 static tree
10367 c_parser_omp_ordered (location_t loc, c_parser *parser)
10369 c_parser_skip_to_pragma_eol (parser);
10370 return c_finish_omp_ordered (loc, c_parser_omp_structured_block (parser));
10373 /* OpenMP 2.5:
10375 section-scope:
10376 { section-sequence }
10378 section-sequence:
10379 section-directive[opt] structured-block
10380 section-sequence section-directive structured-block
10382 SECTIONS_LOC is the location of the #pragma omp sections. */
10384 static tree
10385 c_parser_omp_sections_scope (location_t sections_loc, c_parser *parser)
10387 tree stmt, substmt;
10388 bool error_suppress = false;
10389 location_t loc;
10391 loc = c_parser_peek_token (parser)->location;
10392 if (!c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
10394 /* Avoid skipping until the end of the block. */
10395 parser->error = false;
10396 return NULL_TREE;
10399 stmt = push_stmt_list ();
10401 if (c_parser_peek_token (parser)->pragma_kind != PRAGMA_OMP_SECTION)
10403 substmt = push_stmt_list ();
10405 while (1)
10407 c_parser_statement (parser);
10409 if (c_parser_peek_token (parser)->pragma_kind == PRAGMA_OMP_SECTION)
10410 break;
10411 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
10412 break;
10413 if (c_parser_next_token_is (parser, CPP_EOF))
10414 break;
10417 substmt = pop_stmt_list (substmt);
10418 substmt = build1 (OMP_SECTION, void_type_node, substmt);
10419 SET_EXPR_LOCATION (substmt, loc);
10420 add_stmt (substmt);
10423 while (1)
10425 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
10426 break;
10427 if (c_parser_next_token_is (parser, CPP_EOF))
10428 break;
10430 loc = c_parser_peek_token (parser)->location;
10431 if (c_parser_peek_token (parser)->pragma_kind == PRAGMA_OMP_SECTION)
10433 c_parser_consume_pragma (parser);
10434 c_parser_skip_to_pragma_eol (parser);
10435 error_suppress = false;
10437 else if (!error_suppress)
10439 error_at (loc, "expected %<#pragma omp section%> or %<}%>");
10440 error_suppress = true;
10443 substmt = c_parser_omp_structured_block (parser);
10444 substmt = build1 (OMP_SECTION, void_type_node, substmt);
10445 SET_EXPR_LOCATION (substmt, loc);
10446 add_stmt (substmt);
10448 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE,
10449 "expected %<#pragma omp section%> or %<}%>");
10451 substmt = pop_stmt_list (stmt);
10453 stmt = make_node (OMP_SECTIONS);
10454 SET_EXPR_LOCATION (stmt, sections_loc);
10455 TREE_TYPE (stmt) = void_type_node;
10456 OMP_SECTIONS_BODY (stmt) = substmt;
10458 return add_stmt (stmt);
10461 /* OpenMP 2.5:
10462 # pragma omp sections sections-clause[optseq] newline
10463 sections-scope
10465 LOC is the location of the #pragma token.
10468 #define OMP_SECTIONS_CLAUSE_MASK \
10469 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
10470 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
10471 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
10472 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
10473 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
10475 static tree
10476 c_parser_omp_sections (location_t loc, c_parser *parser)
10478 tree block, clauses, ret;
10480 clauses = c_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
10481 "#pragma omp sections");
10483 block = c_begin_compound_stmt (true);
10484 ret = c_parser_omp_sections_scope (loc, parser);
10485 if (ret)
10486 OMP_SECTIONS_CLAUSES (ret) = clauses;
10487 block = c_end_compound_stmt (loc, block, true);
10488 add_stmt (block);
10490 return ret;
10493 /* OpenMP 2.5:
10494 # pragma parallel parallel-clause new-line
10495 # pragma parallel for parallel-for-clause new-line
10496 # pragma parallel sections parallel-sections-clause new-line
10498 LOC is the location of the #pragma token.
10501 #define OMP_PARALLEL_CLAUSE_MASK \
10502 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
10503 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
10504 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
10505 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
10506 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
10507 | (1u << PRAGMA_OMP_CLAUSE_COPYIN) \
10508 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
10509 | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
10511 static tree
10512 c_parser_omp_parallel (location_t loc, c_parser *parser)
10514 enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
10515 const char *p_name = "#pragma omp parallel";
10516 tree stmt, clauses, par_clause, ws_clause, block;
10517 unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
10519 if (c_parser_next_token_is_keyword (parser, RID_FOR))
10521 c_parser_consume_token (parser);
10522 p_kind = PRAGMA_OMP_PARALLEL_FOR;
10523 p_name = "#pragma omp parallel for";
10524 mask |= OMP_FOR_CLAUSE_MASK;
10525 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
10527 else if (c_parser_next_token_is (parser, CPP_NAME))
10529 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
10530 if (strcmp (p, "sections") == 0)
10532 c_parser_consume_token (parser);
10533 p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
10534 p_name = "#pragma omp parallel sections";
10535 mask |= OMP_SECTIONS_CLAUSE_MASK;
10536 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
10540 clauses = c_parser_omp_all_clauses (parser, mask, p_name);
10542 switch (p_kind)
10544 case PRAGMA_OMP_PARALLEL:
10545 block = c_begin_omp_parallel ();
10546 c_parser_statement (parser);
10547 stmt = c_finish_omp_parallel (loc, clauses, block);
10548 break;
10550 case PRAGMA_OMP_PARALLEL_FOR:
10551 block = c_begin_omp_parallel ();
10552 c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
10553 c_parser_omp_for_loop (loc, parser, ws_clause, &par_clause);
10554 stmt = c_finish_omp_parallel (loc, par_clause, block);
10555 OMP_PARALLEL_COMBINED (stmt) = 1;
10556 break;
10558 case PRAGMA_OMP_PARALLEL_SECTIONS:
10559 block = c_begin_omp_parallel ();
10560 c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
10561 stmt = c_parser_omp_sections_scope (loc, parser);
10562 if (stmt)
10563 OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
10564 stmt = c_finish_omp_parallel (loc, par_clause, block);
10565 OMP_PARALLEL_COMBINED (stmt) = 1;
10566 break;
10568 default:
10569 gcc_unreachable ();
10572 return stmt;
10575 /* OpenMP 2.5:
10576 # pragma omp single single-clause[optseq] new-line
10577 structured-block
10579 LOC is the location of the #pragma.
10582 #define OMP_SINGLE_CLAUSE_MASK \
10583 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
10584 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
10585 | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
10586 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
10588 static tree
10589 c_parser_omp_single (location_t loc, c_parser *parser)
10591 tree stmt = make_node (OMP_SINGLE);
10592 SET_EXPR_LOCATION (stmt, loc);
10593 TREE_TYPE (stmt) = void_type_node;
10595 OMP_SINGLE_CLAUSES (stmt)
10596 = c_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
10597 "#pragma omp single");
10598 OMP_SINGLE_BODY (stmt) = c_parser_omp_structured_block (parser);
10600 return add_stmt (stmt);
10603 /* OpenMP 3.0:
10604 # pragma omp task task-clause[optseq] new-line
10606 LOC is the location of the #pragma.
10609 #define OMP_TASK_CLAUSE_MASK \
10610 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
10611 | (1u << PRAGMA_OMP_CLAUSE_UNTIED) \
10612 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
10613 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
10614 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
10615 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
10616 | (1u << PRAGMA_OMP_CLAUSE_FINAL) \
10617 | (1u << PRAGMA_OMP_CLAUSE_MERGEABLE))
10619 static tree
10620 c_parser_omp_task (location_t loc, c_parser *parser)
10622 tree clauses, block;
10624 clauses = c_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
10625 "#pragma omp task");
10627 block = c_begin_omp_task ();
10628 c_parser_statement (parser);
10629 return c_finish_omp_task (loc, clauses, block);
10632 /* OpenMP 3.0:
10633 # pragma omp taskwait new-line
10636 static void
10637 c_parser_omp_taskwait (c_parser *parser)
10639 location_t loc = c_parser_peek_token (parser)->location;
10640 c_parser_consume_pragma (parser);
10641 c_parser_skip_to_pragma_eol (parser);
10643 c_finish_omp_taskwait (loc);
10646 /* OpenMP 3.1:
10647 # pragma omp taskyield new-line
10650 static void
10651 c_parser_omp_taskyield (c_parser *parser)
10653 location_t loc = c_parser_peek_token (parser)->location;
10654 c_parser_consume_pragma (parser);
10655 c_parser_skip_to_pragma_eol (parser);
10657 c_finish_omp_taskyield (loc);
10660 /* Main entry point to parsing most OpenMP pragmas. */
10662 static void
10663 c_parser_omp_construct (c_parser *parser)
10665 enum pragma_kind p_kind;
10666 location_t loc;
10667 tree stmt;
10669 loc = c_parser_peek_token (parser)->location;
10670 p_kind = c_parser_peek_token (parser)->pragma_kind;
10671 c_parser_consume_pragma (parser);
10673 switch (p_kind)
10675 case PRAGMA_OMP_ATOMIC:
10676 c_parser_omp_atomic (loc, parser);
10677 return;
10678 case PRAGMA_OMP_CRITICAL:
10679 stmt = c_parser_omp_critical (loc, parser);
10680 break;
10681 case PRAGMA_OMP_FOR:
10682 stmt = c_parser_omp_for (loc, parser);
10683 break;
10684 case PRAGMA_OMP_MASTER:
10685 stmt = c_parser_omp_master (loc, parser);
10686 break;
10687 case PRAGMA_OMP_ORDERED:
10688 stmt = c_parser_omp_ordered (loc, parser);
10689 break;
10690 case PRAGMA_OMP_PARALLEL:
10691 stmt = c_parser_omp_parallel (loc, parser);
10692 break;
10693 case PRAGMA_OMP_SECTIONS:
10694 stmt = c_parser_omp_sections (loc, parser);
10695 break;
10696 case PRAGMA_OMP_SINGLE:
10697 stmt = c_parser_omp_single (loc, parser);
10698 break;
10699 case PRAGMA_OMP_TASK:
10700 stmt = c_parser_omp_task (loc, parser);
10701 break;
10702 default:
10703 gcc_unreachable ();
10706 if (stmt)
10707 gcc_assert (EXPR_LOCATION (stmt) != UNKNOWN_LOCATION);
10711 /* OpenMP 2.5:
10712 # pragma omp threadprivate (variable-list) */
10714 static void
10715 c_parser_omp_threadprivate (c_parser *parser)
10717 tree vars, t;
10718 location_t loc;
10720 c_parser_consume_pragma (parser);
10721 loc = c_parser_peek_token (parser)->location;
10722 vars = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_ERROR, NULL);
10724 /* Mark every variable in VARS to be assigned thread local storage. */
10725 for (t = vars; t; t = TREE_CHAIN (t))
10727 tree v = TREE_PURPOSE (t);
10729 /* FIXME diagnostics: Ideally we should keep individual
10730 locations for all the variables in the var list to make the
10731 following errors more precise. Perhaps
10732 c_parser_omp_var_list_parens() should construct a list of
10733 locations to go along with the var list. */
10735 /* If V had already been marked threadprivate, it doesn't matter
10736 whether it had been used prior to this point. */
10737 if (TREE_CODE (v) != VAR_DECL)
10738 error_at (loc, "%qD is not a variable", v);
10739 else if (TREE_USED (v) && !C_DECL_THREADPRIVATE_P (v))
10740 error_at (loc, "%qE declared %<threadprivate%> after first use", v);
10741 else if (! TREE_STATIC (v) && ! DECL_EXTERNAL (v))
10742 error_at (loc, "automatic variable %qE cannot be %<threadprivate%>", v);
10743 else if (TREE_TYPE (v) == error_mark_node)
10745 else if (! COMPLETE_TYPE_P (TREE_TYPE (v)))
10746 error_at (loc, "%<threadprivate%> %qE has incomplete type", v);
10747 else
10749 if (! DECL_THREAD_LOCAL_P (v))
10751 DECL_TLS_MODEL (v) = decl_default_tls_model (v);
10752 /* If rtl has been already set for this var, call
10753 make_decl_rtl once again, so that encode_section_info
10754 has a chance to look at the new decl flags. */
10755 if (DECL_RTL_SET_P (v))
10756 make_decl_rtl (v);
10758 C_DECL_THREADPRIVATE_P (v) = 1;
10762 c_parser_skip_to_pragma_eol (parser);
10765 /* Parse a transaction attribute (GCC Extension).
10767 transaction-attribute:
10768 attributes
10769 [ [ any-word ] ]
10771 The transactional memory language description is written for C++,
10772 and uses the C++0x attribute syntax. For compatibility, allow the
10773 bracket style for transactions in C as well. */
10775 static tree
10776 c_parser_transaction_attributes (c_parser *parser)
10778 tree attr_name, attr = NULL;
10780 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
10781 return c_parser_attributes (parser);
10783 if (!c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
10784 return NULL_TREE;
10785 c_parser_consume_token (parser);
10786 if (!c_parser_require (parser, CPP_OPEN_SQUARE, "expected %<[%>"))
10787 goto error1;
10789 attr_name = c_parser_attribute_any_word (parser);
10790 if (attr_name)
10792 c_parser_consume_token (parser);
10793 attr = build_tree_list (attr_name, NULL_TREE);
10795 else
10796 c_parser_error (parser, "expected identifier");
10798 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, "expected %<]%>");
10799 error1:
10800 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, "expected %<]%>");
10801 return attr;
10804 /* Parse a __transaction_atomic or __transaction_relaxed statement
10805 (GCC Extension).
10807 transaction-statement:
10808 __transaction_atomic transaction-attribute[opt] compound-statement
10809 __transaction_relaxed compound-statement
10811 Note that the only valid attribute is: "outer".
10814 static tree
10815 c_parser_transaction (c_parser *parser, enum rid keyword)
10817 unsigned int old_in = parser->in_transaction;
10818 unsigned int this_in = 1, new_in;
10819 location_t loc = c_parser_peek_token (parser)->location;
10820 tree stmt, attrs;
10822 gcc_assert ((keyword == RID_TRANSACTION_ATOMIC
10823 || keyword == RID_TRANSACTION_RELAXED)
10824 && c_parser_next_token_is_keyword (parser, keyword));
10825 c_parser_consume_token (parser);
10827 if (keyword == RID_TRANSACTION_RELAXED)
10828 this_in |= TM_STMT_ATTR_RELAXED;
10829 else
10831 attrs = c_parser_transaction_attributes (parser);
10832 if (attrs)
10833 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
10836 /* Keep track if we're in the lexical scope of an outer transaction. */
10837 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
10839 parser->in_transaction = new_in;
10840 stmt = c_parser_compound_statement (parser);
10841 parser->in_transaction = old_in;
10843 if (flag_tm)
10844 stmt = c_finish_transaction (loc, stmt, this_in);
10845 else
10846 error_at (loc, (keyword == RID_TRANSACTION_ATOMIC ?
10847 "%<__transaction_atomic%> without transactional memory support enabled"
10848 : "%<__transaction_relaxed %> "
10849 "without transactional memory support enabled"));
10851 return stmt;
10854 /* Parse a __transaction_atomic or __transaction_relaxed expression
10855 (GCC Extension).
10857 transaction-expression:
10858 __transaction_atomic ( expression )
10859 __transaction_relaxed ( expression )
10862 static struct c_expr
10863 c_parser_transaction_expression (c_parser *parser, enum rid keyword)
10865 struct c_expr ret;
10866 unsigned int old_in = parser->in_transaction;
10867 unsigned int this_in = 1;
10868 location_t loc = c_parser_peek_token (parser)->location;
10869 tree attrs;
10871 gcc_assert ((keyword == RID_TRANSACTION_ATOMIC
10872 || keyword == RID_TRANSACTION_RELAXED)
10873 && c_parser_next_token_is_keyword (parser, keyword));
10874 c_parser_consume_token (parser);
10876 if (keyword == RID_TRANSACTION_RELAXED)
10877 this_in |= TM_STMT_ATTR_RELAXED;
10878 else
10880 attrs = c_parser_transaction_attributes (parser);
10881 if (attrs)
10882 this_in |= parse_tm_stmt_attr (attrs, 0);
10885 parser->in_transaction = this_in;
10886 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10888 tree expr = c_parser_expression (parser).value;
10889 ret.original_type = TREE_TYPE (expr);
10890 ret.value = build1 (TRANSACTION_EXPR, ret.original_type, expr);
10891 if (this_in & TM_STMT_ATTR_RELAXED)
10892 TRANSACTION_EXPR_RELAXED (ret.value) = 1;
10893 SET_EXPR_LOCATION (ret.value, loc);
10894 ret.original_code = TRANSACTION_EXPR;
10895 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
10897 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
10898 goto error;
10901 else
10903 error:
10904 ret.value = error_mark_node;
10905 ret.original_code = ERROR_MARK;
10906 ret.original_type = NULL;
10908 parser->in_transaction = old_in;
10910 if (!flag_tm)
10911 error_at (loc, (keyword == RID_TRANSACTION_ATOMIC ?
10912 "%<__transaction_atomic%> without transactional memory support enabled"
10913 : "%<__transaction_relaxed %> "
10914 "without transactional memory support enabled"));
10916 return ret;
10919 /* Parse a __transaction_cancel statement (GCC Extension).
10921 transaction-cancel-statement:
10922 __transaction_cancel transaction-attribute[opt] ;
10924 Note that the only valid attribute is "outer".
10927 static tree
10928 c_parser_transaction_cancel(c_parser *parser)
10930 location_t loc = c_parser_peek_token (parser)->location;
10931 tree attrs;
10932 bool is_outer = false;
10934 gcc_assert (c_parser_next_token_is_keyword (parser, RID_TRANSACTION_CANCEL));
10935 c_parser_consume_token (parser);
10937 attrs = c_parser_transaction_attributes (parser);
10938 if (attrs)
10939 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
10941 if (!flag_tm)
10943 error_at (loc, "%<__transaction_cancel%> without "
10944 "transactional memory support enabled");
10945 goto ret_error;
10947 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
10949 error_at (loc, "%<__transaction_cancel%> within a "
10950 "%<__transaction_relaxed%>");
10951 goto ret_error;
10953 else if (is_outer)
10955 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
10956 && !is_tm_may_cancel_outer (current_function_decl))
10958 error_at (loc, "outer %<__transaction_cancel%> not "
10959 "within outer %<__transaction_atomic%>");
10960 error_at (loc, " or a %<transaction_may_cancel_outer%> function");
10961 goto ret_error;
10964 else if (parser->in_transaction == 0)
10966 error_at (loc, "%<__transaction_cancel%> not within "
10967 "%<__transaction_atomic%>");
10968 goto ret_error;
10971 return add_stmt (build_tm_abort_call (loc, is_outer));
10973 ret_error:
10974 return build1 (NOP_EXPR, void_type_node, error_mark_node);
10977 /* Parse a single source file. */
10979 void
10980 c_parse_file (void)
10982 /* Use local storage to begin. If the first token is a pragma, parse it.
10983 If it is #pragma GCC pch_preprocess, then this will load a PCH file
10984 which will cause garbage collection. */
10985 c_parser tparser;
10987 memset (&tparser, 0, sizeof tparser);
10988 the_parser = &tparser;
10990 if (c_parser_peek_token (&tparser)->pragma_kind == PRAGMA_GCC_PCH_PREPROCESS)
10991 c_parser_pragma_pch_preprocess (&tparser);
10993 the_parser = ggc_alloc_c_parser ();
10994 *the_parser = tparser;
10996 /* Initialize EH, if we've been told to do so. */
10997 if (flag_exceptions)
10998 using_eh_for_cleanups ();
11000 c_parser_translation_unit (the_parser);
11001 the_parser = NULL;
11004 /* This function parses Cilk Plus array notation. The starting index is
11005 passed in INITIAL_INDEX and the array name is passes in ARRAY_VALUE. The
11006 return value of this function is a tree_node called VALUE_TREE of type
11007 ARRAY_NOTATION_REF. */
11009 static tree
11010 c_parser_array_notation (location_t loc, c_parser *parser, tree initial_index,
11011 tree array_value)
11013 c_token *token = NULL;
11014 tree start_index = NULL_TREE, end_index = NULL_TREE, stride = NULL_TREE;
11015 tree value_tree = NULL_TREE, type = NULL_TREE, array_type = NULL_TREE;
11016 tree array_type_domain = NULL_TREE;
11018 if (array_value == error_mark_node)
11020 /* No need to continue. If either of these 2 were true, then an error
11021 must be emitted already. Thus, no need to emit them twice. */
11022 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
11023 return error_mark_node;
11026 array_type = TREE_TYPE (array_value);
11027 gcc_assert (array_type);
11028 type = TREE_TYPE (array_type);
11029 token = c_parser_peek_token (parser);
11031 if (token->type == CPP_EOF)
11033 c_parser_error (parser, "expected %<:%> or numeral");
11034 return value_tree;
11036 else if (token->type == CPP_COLON)
11038 if (!initial_index)
11040 /* If we are here, then we have a case like this A[:]. */
11041 c_parser_consume_token (parser);
11042 if (TREE_CODE (array_type) == POINTER_TYPE)
11044 error_at (loc, "start-index and length fields necessary for "
11045 "using array notations in pointers");
11046 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
11047 return error_mark_node;
11049 if (TREE_CODE (array_type) == FUNCTION_TYPE)
11051 error_at (loc, "array notations cannot be used with function "
11052 "type");
11053 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
11054 return error_mark_node;
11056 if (TREE_CODE (array_type) == ARRAY_TYPE)
11058 tree subtype = TREE_TYPE (array_type);
11059 while (subtype && TREE_CODE (subtype) == POINTER_TYPE)
11061 /* Now this could be a function pointer. Find them and
11062 give out an error. */
11063 subtype = TREE_TYPE (subtype);
11064 if (subtype && TREE_CODE (subtype) == FUNCTION_TYPE)
11066 error_at (loc, "array notations cannot be used with "
11067 "function pointer arrays");
11068 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
11069 NULL);
11070 return error_mark_node;
11074 array_type_domain = TYPE_DOMAIN (array_type);
11076 if (!array_type_domain)
11078 error_at (loc, "start-index and length fields necessary for "
11079 "using array notations in dimensionless arrays");
11080 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
11081 return error_mark_node;
11084 start_index = TYPE_MINVAL (array_type_domain);
11085 start_index = fold_build1 (CONVERT_EXPR, ptrdiff_type_node,
11086 start_index);
11087 if (!TYPE_MAXVAL (array_type_domain)
11088 || !TREE_CONSTANT (TYPE_MAXVAL (array_type_domain)))
11090 error_at (loc, "start-index and length fields necessary for "
11091 "using array notations in variable-length arrays");
11092 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
11093 return error_mark_node;
11095 end_index = TYPE_MAXVAL (array_type_domain);
11096 end_index = fold_build2 (PLUS_EXPR, TREE_TYPE (end_index),
11097 end_index, integer_one_node);
11098 end_index = fold_build1 (CONVERT_EXPR, ptrdiff_type_node, end_index);
11099 stride = build_int_cst (integer_type_node, 1);
11100 stride = fold_build1 (CONVERT_EXPR, ptrdiff_type_node, stride);
11102 else if (initial_index != error_mark_node)
11104 /* If we are here, then there should be 2 possibilities:
11105 1. Array [EXPR : EXPR]
11106 2. Array [EXPR : EXPR : EXPR]
11108 start_index = initial_index;
11110 if (TREE_CODE (array_type) == FUNCTION_TYPE)
11112 error_at (loc, "array notations cannot be used with function "
11113 "type");
11114 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
11115 return error_mark_node;
11117 if (TREE_CODE (array_type) == ARRAY_TYPE
11118 || TREE_CODE (array_type) == POINTER_TYPE)
11120 tree subtype = TREE_TYPE (array_type);
11121 while (subtype
11122 && (TREE_CODE (subtype) == POINTER_TYPE
11123 || TREE_CODE (subtype) == ARRAY_TYPE))
11125 /* Now this could be a function pointer. Find them and
11126 give out an error. */
11127 subtype = TREE_TYPE (subtype);
11128 if (subtype && TREE_CODE (subtype) == FUNCTION_TYPE)
11130 error_at (loc, "array notations cannot be used with "
11131 "function pointer arrays");
11132 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
11133 NULL);
11134 return error_mark_node;
11138 c_parser_consume_token (parser); /* consume the ':' */
11139 end_index = c_parser_expression (parser).value;
11140 if (!end_index || end_index == error_mark_node)
11142 c_parser_skip_to_end_of_block_or_statement (parser);
11143 return error_mark_node;
11145 if (c_parser_peek_token (parser)->type == CPP_COLON)
11147 c_parser_consume_token (parser);
11148 stride = c_parser_expression (parser).value;
11149 if (!stride || stride == error_mark_node)
11151 c_parser_skip_to_end_of_block_or_statement (parser);
11152 return error_mark_node;
11156 else
11157 c_parser_error (parser, "expected array notation expression");
11159 else
11160 c_parser_error (parser, "expected array notation expression");
11162 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, "expected %<]%>");
11164 value_tree = build_array_notation_ref (loc, array_value, start_index,
11165 end_index, stride, type);
11166 if (value_tree != error_mark_node)
11167 SET_EXPR_LOCATION (value_tree, loc);
11168 return value_tree;
11171 #include "gt-c-c-parser.h"