1 /* Parser for C and Objective-C.
2 Copyright (C) 1987, 1988, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
5 Parser actions based on the old Bison parser; structure somewhat
6 influenced by and fragments based on the C++ parser.
8 This file is part of GCC.
10 GCC is free software; you can redistribute it and/or modify it under
11 the terms of the GNU General Public License as published by the Free
12 Software Foundation; either version 2, or (at your option) any later
15 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
16 WARRANTY; without even the implied warranty of MERCHANTABILITY or
17 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
20 You should have received a copy of the GNU General Public License
21 along with GCC; see the file COPYING. If not, write to the Free
22 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
27 Make sure all relevant comments, and all relevant code from all
28 actions, brought over from old parser. Verify exact correspondence
31 Add testcases covering every input symbol in every state in old and
34 Include full syntax for GNU C, including erroneous cases accepted
35 with error messages, in syntax productions in comments.
37 Make more diagnostics in the front end generally take an explicit
38 location rather than implicitly using input_location. */
42 #include "coretypes.h"
45 #include "langhooks.h"
58 /* Miscellaneous data and functions needed for the parser. */
62 /* Objective-C specific parser/lexer information. */
64 static int objc_pq_context = 0;
66 /* The following flag is needed to contextualize Objective-C lexical
67 analysis. In some cases (e.g., 'int NSObject;'), it is undesirable
68 to bind an identifier to an Objective-C class, even if a class with
70 static int objc_need_raw_identifier = 0;
71 #define OBJC_NEED_RAW_IDENTIFIER(VAL) \
73 if (c_dialect_objc ()) \
74 objc_need_raw_identifier = VAL; \
77 /* The reserved keyword table. */
81 ENUM_BITFIELD(rid) rid : 16;
82 unsigned int disable : 16;
85 /* Disable mask. Keywords are disabled if (reswords[i].disable &
87 #define D_C89 0x01 /* not in C89 */
88 #define D_EXT 0x02 /* GCC extension */
89 #define D_EXT89 0x04 /* GCC extension incorporated in C99 */
90 #define D_OBJC 0x08 /* Objective C only */
92 static const struct resword reswords[] =
94 { "_Bool", RID_BOOL, 0 },
95 { "_Complex", RID_COMPLEX, 0 },
96 { "__FUNCTION__", RID_FUNCTION_NAME, 0 },
97 { "__PRETTY_FUNCTION__", RID_PRETTY_FUNCTION_NAME, 0 },
98 { "__alignof", RID_ALIGNOF, 0 },
99 { "__alignof__", RID_ALIGNOF, 0 },
100 { "__asm", RID_ASM, 0 },
101 { "__asm__", RID_ASM, 0 },
102 { "__attribute", RID_ATTRIBUTE, 0 },
103 { "__attribute__", RID_ATTRIBUTE, 0 },
104 { "__builtin_choose_expr", RID_CHOOSE_EXPR, 0 },
105 { "__builtin_offsetof", RID_OFFSETOF, 0 },
106 { "__builtin_types_compatible_p", RID_TYPES_COMPATIBLE_P, 0 },
107 { "__builtin_va_arg", RID_VA_ARG, 0 },
108 { "__complex", RID_COMPLEX, 0 },
109 { "__complex__", RID_COMPLEX, 0 },
110 { "__const", RID_CONST, 0 },
111 { "__const__", RID_CONST, 0 },
112 { "__extension__", RID_EXTENSION, 0 },
113 { "__func__", RID_C99_FUNCTION_NAME, 0 },
114 { "__imag", RID_IMAGPART, 0 },
115 { "__imag__", RID_IMAGPART, 0 },
116 { "__inline", RID_INLINE, 0 },
117 { "__inline__", RID_INLINE, 0 },
118 { "__label__", RID_LABEL, 0 },
119 { "__real", RID_REALPART, 0 },
120 { "__real__", RID_REALPART, 0 },
121 { "__restrict", RID_RESTRICT, 0 },
122 { "__restrict__", RID_RESTRICT, 0 },
123 { "__signed", RID_SIGNED, 0 },
124 { "__signed__", RID_SIGNED, 0 },
125 { "__thread", RID_THREAD, 0 },
126 { "__typeof", RID_TYPEOF, 0 },
127 { "__typeof__", RID_TYPEOF, 0 },
128 { "__volatile", RID_VOLATILE, 0 },
129 { "__volatile__", RID_VOLATILE, 0 },
130 { "asm", RID_ASM, D_EXT },
131 { "auto", RID_AUTO, 0 },
132 { "break", RID_BREAK, 0 },
133 { "case", RID_CASE, 0 },
134 { "char", RID_CHAR, 0 },
135 { "const", RID_CONST, 0 },
136 { "continue", RID_CONTINUE, 0 },
137 { "default", RID_DEFAULT, 0 },
139 { "double", RID_DOUBLE, 0 },
140 { "else", RID_ELSE, 0 },
141 { "enum", RID_ENUM, 0 },
142 { "extern", RID_EXTERN, 0 },
143 { "float", RID_FLOAT, 0 },
144 { "for", RID_FOR, 0 },
145 { "goto", RID_GOTO, 0 },
147 { "inline", RID_INLINE, D_EXT89 },
148 { "int", RID_INT, 0 },
149 { "long", RID_LONG, 0 },
150 { "register", RID_REGISTER, 0 },
151 { "restrict", RID_RESTRICT, D_C89 },
152 { "return", RID_RETURN, 0 },
153 { "short", RID_SHORT, 0 },
154 { "signed", RID_SIGNED, 0 },
155 { "sizeof", RID_SIZEOF, 0 },
156 { "static", RID_STATIC, 0 },
157 { "struct", RID_STRUCT, 0 },
158 { "switch", RID_SWITCH, 0 },
159 { "typedef", RID_TYPEDEF, 0 },
160 { "typeof", RID_TYPEOF, D_EXT },
161 { "union", RID_UNION, 0 },
162 { "unsigned", RID_UNSIGNED, 0 },
163 { "void", RID_VOID, 0 },
164 { "volatile", RID_VOLATILE, 0 },
165 { "while", RID_WHILE, 0 },
166 /* These Objective-C keywords are recognized only immediately after
168 { "class", RID_AT_CLASS, D_OBJC },
169 { "compatibility_alias", RID_AT_ALIAS, D_OBJC },
170 { "defs", RID_AT_DEFS, D_OBJC },
171 { "encode", RID_AT_ENCODE, D_OBJC },
172 { "end", RID_AT_END, D_OBJC },
173 { "implementation", RID_AT_IMPLEMENTATION, D_OBJC },
174 { "interface", RID_AT_INTERFACE, D_OBJC },
175 { "private", RID_AT_PRIVATE, D_OBJC },
176 { "protected", RID_AT_PROTECTED, D_OBJC },
177 { "protocol", RID_AT_PROTOCOL, D_OBJC },
178 { "public", RID_AT_PUBLIC, D_OBJC },
179 { "selector", RID_AT_SELECTOR, D_OBJC },
180 { "throw", RID_AT_THROW, D_OBJC },
181 { "try", RID_AT_TRY, D_OBJC },
182 { "catch", RID_AT_CATCH, D_OBJC },
183 { "finally", RID_AT_FINALLY, D_OBJC },
184 { "synchronized", RID_AT_SYNCHRONIZED, D_OBJC },
185 /* These are recognized only in protocol-qualifier context
187 { "bycopy", RID_BYCOPY, D_OBJC },
188 { "byref", RID_BYREF, D_OBJC },
189 { "in", RID_IN, D_OBJC },
190 { "inout", RID_INOUT, D_OBJC },
191 { "oneway", RID_ONEWAY, D_OBJC },
192 { "out", RID_OUT, D_OBJC },
194 #define N_reswords (sizeof reswords / sizeof (struct resword))
196 /* Initialization routine for this file. */
201 /* The only initialization required is of the reserved word
205 int mask = (flag_isoc99 ? 0 : D_C89)
206 | (flag_no_asm ? (flag_isoc99 ? D_EXT : D_EXT|D_EXT89) : 0);
208 if (!c_dialect_objc ())
211 ridpointers = GGC_CNEWVEC (tree, (int) RID_MAX);
212 for (i = 0; i < N_reswords; i++)
214 /* If a keyword is disabled, do not enter it into the table
215 and so create a canonical spelling that isn't a keyword. */
216 if (reswords[i].disable & mask)
219 id = get_identifier (reswords[i].word);
220 C_RID_CODE (id) = reswords[i].rid;
221 C_IS_RESERVED_WORD (id) = 1;
222 ridpointers [(int) reswords[i].rid] = id;
226 /* The C lexer intermediates between the lexer in cpplib and c-lex.c
227 and the C parser. Unlike the C++ lexer, the parser structure
228 stores the lexer information instead of using a separate structure.
229 Identifiers are separated into ordinary identifiers, type names,
230 keywords and some other Objective-C types of identifiers, and some
231 look-ahead is maintained.
233 ??? It might be a good idea to lex the whole file up front (as for
234 C++). It would then be possible to share more of the C and C++
235 lexer code, if desired. */
237 /* The following local token type is used. */
240 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
242 /* The number of token types, including C-specific ones. */
243 #define N_C_TTYPES ((int) (CPP_KEYWORD + 1))
245 /* More information about the type of a CPP_NAME token. */
246 typedef enum c_id_kind {
247 /* An ordinary identifier. */
249 /* An identifier declared as a typedef name. */
251 /* An identifier declared as an Objective-C class name. */
253 /* Not an identifier. */
257 /* A single C token after string literal concatenation and conversion
258 of preprocessing tokens to tokens. */
259 typedef struct c_token GTY (())
261 /* The kind of token. */
262 ENUM_BITFIELD (cpp_ttype) type : 8;
263 /* If this token is a CPP_NAME, this value indicates whether also
264 declared as some kind of type. Otherwise, it is C_ID_NONE. */
265 ENUM_BITFIELD (c_id_kind) id_kind : 8;
266 /* If this token is a keyword, this value indicates which keyword.
267 Otherwise, this value is RID_MAX. */
268 ENUM_BITFIELD (rid) keyword : 8;
269 /* True if this token is from a system header. */
270 BOOL_BITFIELD in_system_header : 1;
271 /* The value associated with this token, if any. */
273 /* The location at which this token was found. */
277 /* A parser structure recording information about the state and
278 context of parsing. Includes lexer information with up to two
279 tokens of look-ahead; more are not needed for C. */
280 typedef struct c_parser GTY(())
282 /* The look-ahead tokens. */
284 /* How many look-ahead tokens are available (0, 1 or 2). */
286 /* True if a syntax error is being recovered from; false otherwise.
287 c_parser_error sets this flag. It should clear this flag when
288 enough tokens have been consumed to recover from the error. */
289 BOOL_BITFIELD error : 1;
292 /* Read in and lex a single token, storing it in *TOKEN. */
295 c_lex_one_token (c_token *token)
297 timevar_push (TV_LEX);
298 token->type = c_lex (&token->value);
299 token->location = input_location;
300 token->in_system_header = in_system_header;
304 token->id_kind = C_ID_NONE;
305 token->keyword = RID_MAX;
309 int objc_force_identifier = objc_need_raw_identifier;
310 OBJC_NEED_RAW_IDENTIFIER (0);
312 if (C_IS_RESERVED_WORD (token->value))
314 enum rid rid_code = C_RID_CODE (token->value);
316 if (c_dialect_objc ())
318 if (!OBJC_IS_AT_KEYWORD (rid_code)
319 && (!OBJC_IS_PQ_KEYWORD (rid_code) || objc_pq_context))
321 /* Return the canonical spelling for this keyword. */
322 token->value = ridpointers[(int) rid_code];
323 token->type = CPP_KEYWORD;
324 token->keyword = rid_code;
330 /* Return the canonical spelling for this keyword. */
331 token->value = ridpointers[(int) rid_code];
332 token->type = CPP_KEYWORD;
333 token->keyword = rid_code;
338 decl = lookup_name (token->value);
341 if (TREE_CODE (decl) == TYPE_DECL)
343 token->id_kind = C_ID_TYPENAME;
347 else if (c_dialect_objc ())
349 tree objc_interface_decl = objc_is_class_name (token->value);
350 /* Objective-C class names are in the same namespace as
351 variables and typedefs, and hence are shadowed by local
353 if (objc_interface_decl
354 && (global_bindings_p ()
355 || (!objc_force_identifier && !decl)))
357 token->value = objc_interface_decl;
358 token->id_kind = C_ID_CLASSNAME;
363 token->id_kind = C_ID_ID;
366 /* This only happens in Objective-C; it must be a keyword. */
367 token->type = CPP_KEYWORD;
368 token->id_kind = C_ID_NONE;
369 token->keyword = C_RID_CODE (token->value);
373 case CPP_CLOSE_PAREN:
375 /* These tokens may affect the interpretation of any identifiers
376 following, if doing Objective-C. */
377 OBJC_NEED_RAW_IDENTIFIER (0);
378 token->id_kind = C_ID_NONE;
379 token->keyword = RID_MAX;
382 token->id_kind = C_ID_NONE;
383 token->keyword = RID_MAX;
386 timevar_pop (TV_LEX);
389 /* Return a pointer to the next token from PARSER, reading it in if
392 static inline c_token *
393 c_parser_peek_token (c_parser *parser)
395 if (parser->tokens_avail == 0)
397 c_lex_one_token (&parser->tokens[0]);
398 parser->tokens_avail = 1;
400 return &parser->tokens[0];
403 /* Return true if the next token from PARSER has the indicated
407 c_parser_next_token_is (c_parser *parser, enum cpp_ttype type)
409 return c_parser_peek_token (parser)->type == type;
412 /* Return true if the next token from PARSER does not have the
416 c_parser_next_token_is_not (c_parser *parser, enum cpp_ttype type)
418 return !c_parser_next_token_is (parser, type);
421 /* Return true if the next token from PARSER is the indicated
425 c_parser_next_token_is_keyword (c_parser *parser, enum rid keyword)
429 /* Peek at the next token. */
430 token = c_parser_peek_token (parser);
431 /* Check to see if it is the indicated keyword. */
432 return token->keyword == keyword;
435 /* Return true if TOKEN can start a type name,
438 c_token_starts_typename (c_token *token)
443 switch (token->id_kind)
450 gcc_assert (c_dialect_objc ());
456 switch (token->keyword)
482 if (c_dialect_objc ())
490 /* Return true if the next token from PARSER can start a type name,
493 c_parser_next_token_starts_typename (c_parser *parser)
495 c_token *token = c_parser_peek_token (parser);
496 return c_token_starts_typename (token);
499 /* Return true if TOKEN can start declaration specifiers, false
502 c_token_starts_declspecs (c_token *token)
507 switch (token->id_kind)
514 gcc_assert (c_dialect_objc ());
520 switch (token->keyword)
553 if (c_dialect_objc ())
561 /* Return true if the next token from PARSER can start declaration
562 specifiers, false otherwise. */
564 c_parser_next_token_starts_declspecs (c_parser *parser)
566 c_token *token = c_parser_peek_token (parser);
567 return c_token_starts_declspecs (token);
570 /* Return a pointer to the next-but-one token from PARSER, reading it
571 in if necessary. The next token is already read in. */
574 c_parser_peek_2nd_token (c_parser *parser)
576 if (parser->tokens_avail >= 2)
577 return &parser->tokens[1];
578 gcc_assert (parser->tokens_avail == 1);
579 gcc_assert (parser->tokens[0].type != CPP_EOF);
580 c_lex_one_token (&parser->tokens[1]);
581 parser->tokens_avail = 2;
582 return &parser->tokens[1];
585 /* Consume the next token from PARSER. */
588 c_parser_consume_token (c_parser *parser)
590 if (parser->tokens_avail == 2)
591 parser->tokens[0] = parser->tokens[1];
594 gcc_assert (parser->tokens_avail == 1);
595 gcc_assert (parser->tokens[0].type != CPP_EOF);
597 parser->tokens_avail--;
600 /* Update the globals input_location and in_system_header from
603 c_parser_set_source_position_from_token (c_token *token)
605 if (token->type != CPP_EOF)
607 input_location = token->location;
608 in_system_header = token->in_system_header;
612 /* Allocate a new parser. */
617 /* Use local storage to lex the first token because loading a PCH
618 file may cause garbage collection. */
621 memset (&tparser, 0, sizeof tparser);
622 c_lex_one_token (&tparser.tokens[0]);
623 tparser.tokens_avail = 1;
624 ret = GGC_NEW (c_parser);
625 memcpy (ret, &tparser, sizeof tparser);
629 /* Issue a diagnostic of the form
630 FILE:LINE: MESSAGE before TOKEN
631 where TOKEN is the next token in the input stream of PARSER.
632 MESSAGE (specified by the caller) is usually of the form "expected
635 Do not issue a diagnostic if still recovering from an error.
637 ??? This is taken from the C++ parser, but building up messages in
638 this way is not i18n-friendly and some other approach should be
642 c_parser_error (c_parser *parser, const char *msgid)
644 c_token *token = c_parser_peek_token (parser);
647 parser->error = true;
650 /* This diagnostic makes more sense if it is tagged to the line of
651 the token we just peeked at. */
652 c_parser_set_source_position_from_token (token);
653 c_parse_error (msgid,
654 /* Because c_parse_error does not understand
655 CPP_KEYWORD, keywords are treated like
657 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
661 /* If the next token is of the indicated TYPE, consume it. Otherwise,
662 issue the error MSGID. If MSGID is NULL then a message has already
663 been produced and no message will be produced this time. Returns
664 true if found, false otherwise. */
667 c_parser_require (c_parser *parser,
671 if (c_parser_next_token_is (parser, type))
673 c_parser_consume_token (parser);
678 c_parser_error (parser, msgid);
683 /* If the next token is the indicated keyword, consume it. Otherwise,
684 issue the error MSGID. Returns true if found, false otherwise. */
687 c_parser_require_keyword (c_parser *parser,
691 if (c_parser_next_token_is_keyword (parser, keyword))
693 c_parser_consume_token (parser);
698 c_parser_error (parser, msgid);
703 /* Like c_parser_require, except that tokens will be skipped until the
704 desired token is found. An error message is still produced if the
705 next token is not as expected. If MSGID is NULL then a message has
706 already been produced and no message will be produced this
710 c_parser_skip_until_found (c_parser *parser,
714 unsigned nesting_depth = 0;
716 if (c_parser_require (parser, type, msgid))
719 /* Skip tokens until the desired token is found. */
722 /* Peek at the next token. */
723 c_token *token = c_parser_peek_token (parser);
724 /* If we've reached the token we want, consume it and stop. */
725 if (token->type == type && !nesting_depth)
727 c_parser_consume_token (parser);
730 /* If we've run out of tokens, stop. */
731 if (token->type == CPP_EOF)
733 if (token->type == CPP_OPEN_BRACE
734 || token->type == CPP_OPEN_PAREN
735 || token->type == CPP_OPEN_SQUARE)
737 else if (token->type == CPP_CLOSE_BRACE
738 || token->type == CPP_CLOSE_PAREN
739 || token->type == CPP_CLOSE_SQUARE)
741 if (nesting_depth-- == 0)
744 /* Consume this token. */
745 c_parser_consume_token (parser);
747 parser->error = false;
750 /* Skip tokens until the end of a parameter is found, but do not
751 consume the comma, semicolon or closing delimiter. */
754 c_parser_skip_to_end_of_parameter (c_parser *parser)
756 unsigned nesting_depth = 0;
760 c_token *token = c_parser_peek_token (parser);
761 if ((token->type == CPP_COMMA || token->type == CPP_SEMICOLON)
764 /* If we've run out of tokens, stop. */
765 if (token->type == CPP_EOF)
767 if (token->type == CPP_OPEN_BRACE
768 || token->type == CPP_OPEN_PAREN
769 || token->type == CPP_OPEN_SQUARE)
771 else if (token->type == CPP_CLOSE_BRACE
772 || token->type == CPP_CLOSE_PAREN
773 || token->type == CPP_CLOSE_SQUARE)
775 if (nesting_depth-- == 0)
778 /* Consume this token. */
779 c_parser_consume_token (parser);
781 parser->error = false;
784 /* Skip tokens until we have consumed an entire block, or until we
785 have consumed a non-nested ';'. */
788 c_parser_skip_to_end_of_block_or_statement (c_parser *parser)
790 unsigned nesting_depth = 0;
796 /* Peek at the next token. */
797 token = c_parser_peek_token (parser);
798 /* If we've run out of tokens, stop. */
799 if (token->type == CPP_EOF)
801 /* If the next token is a ';', we have reached the end of the
803 if (token->type == CPP_SEMICOLON && !nesting_depth)
805 /* Consume the ';'. */
806 c_parser_consume_token (parser);
809 /* If the next token is a non-nested '}', then we have reached
810 the end of the current block. */
811 if (token->type == CPP_CLOSE_BRACE
812 && (nesting_depth == 0 || --nesting_depth == 0))
814 c_parser_consume_token (parser);
817 /* If it the next token is a '{', then we are entering a new
818 block. Consume the entire block. */
819 if (token->type == CPP_OPEN_BRACE)
821 c_parser_consume_token (parser);
823 parser->error = false;
827 /* Save the warning flags which are controlled by __extension__. */
830 disable_extension_diagnostics (void)
833 | (warn_pointer_arith << 1)
834 | (warn_traditional << 2)
837 warn_pointer_arith = 0;
838 warn_traditional = 0;
843 /* Restore the warning flags which are controlled by __extension__.
844 FLAGS is the return value from disable_extension_diagnostics. */
847 restore_extension_diagnostics (int flags)
849 pedantic = flags & 1;
850 warn_pointer_arith = (flags >> 1) & 1;
851 warn_traditional = (flags >> 2) & 1;
852 flag_iso = (flags >> 3) & 1;
855 /* Possibly kinds of declarator to parse. */
856 typedef enum c_dtr_syn {
857 /* A normal declarator with an identifier. */
859 /* An abstract declarator (maybe empty). */
861 /* A parameter declarator: may be either, but after a type name does
862 not redeclare a typedef name as an identifier if it can
863 alternatively be interpreted as a typedef name; see DR#009,
864 applied in C90 TC1, omitted from C99 and reapplied in C99 TC2
865 following DR#249. For example, given a typedef T, "int T" and
866 "int *T" are valid parameter declarations redeclaring T, while
867 "int (T)" and "int * (T)" and "int (T[])" and "int (T (int))" are
868 abstract declarators rather than involving redundant parentheses;
869 the same applies with attributes inside the parentheses before
874 static void c_parser_external_declaration (c_parser *);
875 static void c_parser_asm_definition (c_parser *);
876 static void c_parser_declaration_or_fndef (c_parser *, bool, bool, bool, bool);
877 static void c_parser_declspecs (c_parser *, struct c_declspecs *, bool, bool,
879 static struct c_typespec c_parser_enum_specifier (c_parser *);
880 static struct c_typespec c_parser_struct_or_union_specifier (c_parser *);
881 static tree c_parser_struct_declaration (c_parser *);
882 static struct c_typespec c_parser_typeof_specifier (c_parser *);
883 static struct c_declarator *c_parser_declarator (c_parser *, bool, c_dtr_syn,
885 static struct c_declarator *c_parser_direct_declarator (c_parser *, bool,
887 static struct c_declarator *c_parser_direct_declarator_inner (c_parser *,
889 struct c_declarator *);
890 static struct c_arg_info *c_parser_parms_declarator (c_parser *, bool, tree);
891 static struct c_arg_info *c_parser_parms_list_declarator (c_parser *, tree);
892 static struct c_parm *c_parser_parameter_declaration (c_parser *, tree);
893 static tree c_parser_simple_asm_expr (c_parser *);
894 static tree c_parser_attributes (c_parser *);
895 static struct c_type_name *c_parser_type_name (c_parser *);
896 static struct c_expr c_parser_initializer (c_parser *);
897 static struct c_expr c_parser_braced_init (c_parser *, tree, bool);
898 static void c_parser_initelt (c_parser *);
899 static void c_parser_initval (c_parser *, struct c_expr *);
900 static tree c_parser_compound_statement (c_parser *);
901 static void c_parser_compound_statement_nostart (c_parser *);
902 static void c_parser_label (c_parser *);
903 static void c_parser_statement (c_parser *);
904 static void c_parser_statement_after_labels (c_parser *);
905 static void c_parser_if_statement (c_parser *);
906 static void c_parser_switch_statement (c_parser *);
907 static void c_parser_while_statement (c_parser *);
908 static void c_parser_do_statement (c_parser *);
909 static void c_parser_for_statement (c_parser *);
910 static tree c_parser_asm_statement (c_parser *);
911 static tree c_parser_asm_operands (c_parser *);
912 static tree c_parser_asm_clobbers (c_parser *);
913 static struct c_expr c_parser_expr_no_commas (c_parser *, struct c_expr *);
914 static struct c_expr c_parser_conditional_expression (c_parser *,
916 static struct c_expr c_parser_binary_expression (c_parser *, struct c_expr *);
917 static struct c_expr c_parser_cast_expression (c_parser *, struct c_expr *);
918 static struct c_expr c_parser_unary_expression (c_parser *);
919 static struct c_expr c_parser_sizeof_expression (c_parser *);
920 static struct c_expr c_parser_alignof_expression (c_parser *);
921 static struct c_expr c_parser_postfix_expression (c_parser *);
922 static struct c_expr c_parser_postfix_expression_after_paren_type (c_parser *,
923 struct c_type_name *);
924 static struct c_expr c_parser_postfix_expression_after_primary (c_parser *,
926 static struct c_expr c_parser_expression (c_parser *);
927 static tree c_parser_expr_list (c_parser *);
929 /* These Objective-C parser functions are only ever called when
930 compiling Objective-C. */
931 static void c_parser_objc_class_definition (c_parser *);
932 static void c_parser_objc_class_instance_variables (c_parser *);
933 static void c_parser_objc_class_declaration (c_parser *);
934 static void c_parser_objc_alias_declaration (c_parser *);
935 static void c_parser_objc_protocol_definition (c_parser *);
936 static enum tree_code c_parser_objc_method_type (c_parser *);
937 static void c_parser_objc_method_definition (c_parser *);
938 static void c_parser_objc_methodprotolist (c_parser *);
939 static void c_parser_objc_methodproto (c_parser *);
940 static tree c_parser_objc_method_decl (c_parser *);
941 static tree c_parser_objc_type_name (c_parser *);
942 static tree c_parser_objc_protocol_refs (c_parser *);
943 static void c_parser_objc_try_catch_statement (c_parser *);
944 static void c_parser_objc_synchronized_statement (c_parser *);
945 static tree c_parser_objc_selector (c_parser *);
946 static tree c_parser_objc_selector_arg (c_parser *);
947 static tree c_parser_objc_receiver (c_parser *);
948 static tree c_parser_objc_message_args (c_parser *);
949 static tree c_parser_objc_keywordexpr (c_parser *);
951 /* Parse a translation unit (C90 6.7, C99 6.9).
954 external-declarations
956 external-declarations:
958 external-declarations external-declaration
967 c_parser_translation_unit (c_parser *parser)
969 if (c_parser_next_token_is (parser, CPP_EOF))
972 pedwarn ("ISO C forbids an empty source file");
976 void *obstack_position = obstack_alloc (&parser_obstack, 0);
980 c_parser_external_declaration (parser);
981 obstack_free (&parser_obstack, obstack_position);
983 while (c_parser_next_token_is_not (parser, CPP_EOF));
987 /* Parse an external declaration (C90 6.7, C99 6.9).
989 external-declaration:
995 external-declaration:
998 __extension__ external-declaration
1002 external-declaration:
1003 objc-class-definition
1004 objc-class-declaration
1005 objc-alias-declaration
1006 objc-protocol-definition
1007 objc-method-definition
1012 c_parser_external_declaration (c_parser *parser)
1015 switch (c_parser_peek_token (parser)->type)
1018 switch (c_parser_peek_token (parser)->keyword)
1021 ext = disable_extension_diagnostics ();
1022 c_parser_consume_token (parser);
1023 c_parser_external_declaration (parser);
1024 restore_extension_diagnostics (ext);
1027 c_parser_asm_definition (parser);
1029 case RID_AT_INTERFACE:
1030 case RID_AT_IMPLEMENTATION:
1031 gcc_assert (c_dialect_objc ());
1032 c_parser_objc_class_definition (parser);
1035 gcc_assert (c_dialect_objc ());
1036 c_parser_objc_class_declaration (parser);
1039 gcc_assert (c_dialect_objc ());
1040 c_parser_objc_alias_declaration (parser);
1042 case RID_AT_PROTOCOL:
1043 gcc_assert (c_dialect_objc ());
1044 c_parser_objc_protocol_definition (parser);
1047 gcc_assert (c_dialect_objc ());
1048 c_parser_consume_token (parser);
1049 objc_finish_implementation ();
1057 pedwarn ("ISO C does not allow extra %<;%> outside of a function");
1058 c_parser_consume_token (parser);
1062 if (c_dialect_objc ())
1064 c_parser_objc_method_definition (parser);
1067 /* Else fall through, and yield a syntax error trying to parse
1068 as a declaration or function definition. */
1071 /* A declaration or a function definition. We can only tell
1072 which after parsing the declaration specifiers, if any, and
1073 the first declarator. */
1074 c_parser_declaration_or_fndef (parser, true, true, false, true);
1079 /* Parse a declaration or function definition (C90 6.5, 6.7.1, C99
1080 6.7, 6.9.1). If FNDEF_OK is true, a function definition is
1081 accepted; otherwise (old-style parameter declarations) only other
1082 declarations are accepted. If NESTED is true, we are inside a
1083 function or parsing old-style parameter declarations; any functions
1084 encountered are nested functions and declaration specifiers are
1085 required; otherwise we are at top level and functions are normal
1086 functions and declaration specifiers may be optional. If EMPTY_OK
1087 is true, empty declarations are OK (subject to all other
1088 constraints); otherwise (old-style parameter declarations) they are
1089 diagnosed. If START_ATTR_OK is true, the declaration specifiers
1090 may start with attributes; otherwise they may not.
1093 declaration-specifiers init-declarator-list[opt] ;
1095 function-definition:
1096 declaration-specifiers[opt] declarator declaration-list[opt]
1101 declaration-list declaration
1103 init-declarator-list:
1105 init-declarator-list , init-declarator
1108 declarator simple-asm-expr[opt] attributes[opt]
1109 declarator simple-asm-expr[opt] attributes[opt] = initializer
1113 nested-function-definition:
1114 declaration-specifiers declarator declaration-list[opt]
1117 The simple-asm-expr and attributes are GNU extensions.
1119 This function does not handle __extension__; that is handled in its
1120 callers. ??? Following the old parser, __extension__ may start
1121 external declarations, declarations in functions and declarations
1122 at the start of "for" loops, but not old-style parameter
1125 C99 requires declaration specifiers in a function definition; the
1126 absence is diagnosed through the diagnosis of implicit int. In GNU
1127 C we also allow but diagnose declarations without declaration
1128 specifiers, but only at top level (elsewhere they conflict with
1132 c_parser_declaration_or_fndef (c_parser *parser, bool fndef_ok, bool empty_ok,
1133 bool nested, bool start_attr_ok)
1135 struct c_declspecs *specs;
1137 tree all_prefix_attrs;
1138 bool diagnosed_no_specs = false;
1139 specs = build_null_declspecs ();
1140 c_parser_declspecs (parser, specs, true, true, start_attr_ok);
1143 c_parser_skip_to_end_of_block_or_statement (parser);
1146 if (nested && !specs->declspecs_seen_p)
1148 c_parser_error (parser, "expected declaration specifiers");
1149 c_parser_skip_to_end_of_block_or_statement (parser);
1152 finish_declspecs (specs);
1153 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1159 shadow_tag_warned (specs, 1);
1160 pedwarn ("empty declaration");
1162 c_parser_consume_token (parser);
1165 pending_xref_error ();
1166 prefix_attrs = specs->attrs;
1167 all_prefix_attrs = prefix_attrs;
1168 specs->attrs = NULL_TREE;
1171 struct c_declarator *declarator;
1174 /* Declaring either one or more declarators (in which case we
1175 should diagnose if there were no declaration specifiers) or a
1176 function definition (in which case the diagnostic for
1177 implicit int suffices). */
1178 declarator = c_parser_declarator (parser, specs->type_seen_p,
1179 C_DTR_NORMAL, &dummy);
1180 if (declarator == NULL)
1182 c_parser_skip_to_end_of_block_or_statement (parser);
1185 if (c_parser_next_token_is (parser, CPP_EQ)
1186 || c_parser_next_token_is (parser, CPP_COMMA)
1187 || c_parser_next_token_is (parser, CPP_SEMICOLON)
1188 || c_parser_next_token_is_keyword (parser, RID_ASM)
1189 || c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
1191 tree asm_name = NULL_TREE;
1192 tree postfix_attrs = NULL_TREE;
1193 if (!diagnosed_no_specs && !specs->declspecs_seen_p)
1195 diagnosed_no_specs = true;
1196 pedwarn ("data definition has no type or storage class");
1198 /* Having seen a data definition, there cannot now be a
1199 function definition. */
1201 if (c_parser_next_token_is_keyword (parser, RID_ASM))
1202 asm_name = c_parser_simple_asm_expr (parser);
1203 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
1204 postfix_attrs = c_parser_attributes (parser);
1205 if (c_parser_next_token_is (parser, CPP_EQ))
1209 c_parser_consume_token (parser);
1210 /* The declaration of the variable is in effect while
1211 its initializer is parsed. */
1212 d = start_decl (declarator, specs, true,
1213 chainon (postfix_attrs, all_prefix_attrs));
1215 d = error_mark_node;
1216 start_init (d, asm_name, global_bindings_p ());
1217 init = c_parser_initializer (parser);
1219 if (d != error_mark_node)
1221 maybe_warn_string_init (TREE_TYPE (d), init);
1222 finish_decl (d, init.value, asm_name);
1227 tree d = start_decl (declarator, specs, false,
1228 chainon (postfix_attrs,
1231 finish_decl (d, NULL_TREE, asm_name);
1233 if (c_parser_next_token_is (parser, CPP_COMMA))
1235 c_parser_consume_token (parser);
1236 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
1237 all_prefix_attrs = chainon (c_parser_attributes (parser),
1240 all_prefix_attrs = prefix_attrs;
1243 else if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1245 c_parser_consume_token (parser);
1250 c_parser_error (parser, "expected %<,%> or %<;%>");
1251 c_parser_skip_to_end_of_block_or_statement (parser);
1257 c_parser_error (parser, "expected %<=%>, %<,%>, %<;%>, "
1258 "%<asm%> or %<__attribute__%>");
1259 c_parser_skip_to_end_of_block_or_statement (parser);
1262 /* Function definition (nested or otherwise). */
1266 pedwarn ("ISO C forbids nested functions");
1267 push_function_context ();
1269 if (!start_function (specs, declarator, all_prefix_attrs))
1271 /* This can appear in many cases looking nothing like a
1272 function definition, so we don't give a more specific
1273 error suggesting there was one. */
1274 c_parser_error (parser, "expected %<=%>, %<,%>, %<;%>, %<asm%> "
1275 "or %<__attribute__%>");
1277 pop_function_context ();
1280 /* Parse old-style parameter declarations. ??? Attributes are
1281 not allowed to start declaration specifiers here because of a
1282 syntax conflict between a function declaration with attribute
1283 suffix and a function definition with an attribute prefix on
1284 first old-style parameter declaration. Following the old
1285 parser, they are not accepted on subsequent old-style
1286 parameter declarations either. However, there is no
1287 ambiguity after the first declaration, nor indeed on the
1288 first as long as we don't allow postfix attributes after a
1289 declarator with a nonempty identifier list in a definition;
1290 and postfix attributes have never been accepted here in
1291 function definitions either. */
1292 while (c_parser_next_token_is_not (parser, CPP_EOF)
1293 && c_parser_next_token_is_not (parser, CPP_OPEN_BRACE))
1294 c_parser_declaration_or_fndef (parser, false, false, true, false);
1295 DECL_SOURCE_LOCATION (current_function_decl)
1296 = c_parser_peek_token (parser)->location;
1297 store_parm_decls ();
1298 fnbody = c_parser_compound_statement (parser);
1301 tree decl = current_function_decl;
1304 pop_function_context ();
1305 add_stmt (build_stmt (DECL_EXPR, decl));
1316 /* Parse an asm-definition (asm() outside a function body). This is a
1324 c_parser_asm_definition (c_parser *parser)
1326 tree asm_str = c_parser_simple_asm_expr (parser);
1327 /* ??? This only works sensibly in the presence of
1328 -fno-unit-at-a-time; file-scope asms really need to be passed to
1329 cgraph which needs to preserve the order of functions and
1332 assemble_asm (asm_str);
1333 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
1336 /* Parse some declaration specifiers (possibly none) (C90 6.5, C99
1337 6.7), adding them to SPECS (which may already include some).
1338 Storage class specifiers are accepted iff SCSPEC_OK; type
1339 specifiers are accepted iff TYPESPEC_OK; attributes are accepted at
1340 the start iff START_ATTR_OK.
1342 declaration-specifiers:
1343 storage-class-specifier declaration-specifiers[opt]
1344 type-specifier declaration-specifiers[opt]
1345 type-qualifier declaration-specifiers[opt]
1346 function-specifier declaration-specifiers[opt]
1348 Function specifiers (inline) are from C99, and are currently
1349 handled as storage class specifiers, as is __thread.
1351 C90 6.5.1, C99 6.7.1:
1352 storage-class-specifier:
1363 C90 6.5.2, C99 6.7.2:
1376 [_Imaginary removed in C99 TC2]
1377 struct-or-union-specifier
1381 (_Bool and _Complex are new in C99.)
1383 C90 6.5.3, C99 6.7.3:
1390 (restrict is new in C99.)
1394 declaration-specifiers:
1395 attributes declaration-specifiers[opt]
1397 storage-class-specifier:
1406 class-name objc-protocol-refs[opt]
1407 typedef-name objc-protocol-refs
1412 c_parser_declspecs (c_parser *parser, struct c_declspecs *specs,
1413 bool scspec_ok, bool typespec_ok, bool start_attr_ok)
1415 bool attrs_ok = start_attr_ok;
1416 bool seen_type = specs->type_seen_p;
1417 while (c_parser_next_token_is (parser, CPP_NAME)
1418 || c_parser_next_token_is (parser, CPP_KEYWORD)
1419 || (c_dialect_objc () && c_parser_next_token_is (parser, CPP_LESS)))
1421 struct c_typespec t;
1423 if (c_parser_next_token_is (parser, CPP_NAME))
1425 tree value = c_parser_peek_token (parser)->value;
1426 c_id_kind kind = c_parser_peek_token (parser)->id_kind;
1427 /* This finishes the specifiers unless a type name is OK, it
1428 is declared as a type name and a type name hasn't yet
1430 if (!typespec_ok || seen_type
1431 || (kind != C_ID_TYPENAME && kind != C_ID_CLASSNAME))
1433 c_parser_consume_token (parser);
1436 if (kind == C_ID_TYPENAME
1437 && (!c_dialect_objc ()
1438 || c_parser_next_token_is_not (parser, CPP_LESS)))
1440 t.kind = ctsk_typedef;
1441 /* For a typedef name, record the meaning, not the name.
1442 In case of 'foo foo, bar;'. */
1443 t.spec = lookup_name (value);
1447 tree proto = NULL_TREE;
1448 gcc_assert (c_dialect_objc ());
1450 if (c_parser_next_token_is (parser, CPP_LESS))
1451 proto = c_parser_objc_protocol_refs (parser);
1452 t.spec = objc_get_protocol_qualified_type (value, proto);
1454 declspecs_add_type (specs, t);
1457 if (c_parser_next_token_is (parser, CPP_LESS))
1459 /* Make "<SomeProtocol>" equivalent to "id <SomeProtocol>" -
1460 nisse@lysator.liu.se. */
1462 gcc_assert (c_dialect_objc ());
1463 if (!typespec_ok || seen_type)
1465 proto = c_parser_objc_protocol_refs (parser);
1467 t.spec = objc_get_protocol_qualified_type (NULL_TREE, proto);
1468 declspecs_add_type (specs, t);
1471 gcc_assert (c_parser_next_token_is (parser, CPP_KEYWORD));
1472 switch (c_parser_peek_token (parser)->keyword)
1484 /* TODO: Distinguish between function specifiers (inline)
1485 and storage class specifiers, either here or in
1486 declspecs_add_scspec. */
1487 declspecs_add_scspec (specs, c_parser_peek_token (parser)->value);
1488 c_parser_consume_token (parser);
1505 OBJC_NEED_RAW_IDENTIFIER (1);
1506 t.kind = ctsk_resword;
1507 t.spec = c_parser_peek_token (parser)->value;
1508 declspecs_add_type (specs, t);
1509 c_parser_consume_token (parser);
1516 t = c_parser_enum_specifier (parser);
1517 declspecs_add_type (specs, t);
1525 t = c_parser_struct_or_union_specifier (parser);
1526 declspecs_add_type (specs, t);
1529 /* ??? The old parser rejected typeof after other type
1530 specifiers, but is a syntax error the best way of
1532 if (!typespec_ok || seen_type)
1536 t = c_parser_typeof_specifier (parser);
1537 declspecs_add_type (specs, t);
1543 declspecs_add_qual (specs, c_parser_peek_token (parser)->value);
1544 c_parser_consume_token (parser);
1549 attrs = c_parser_attributes (parser);
1550 declspecs_add_attrs (specs, attrs);
1559 /* Parse an enum specifier (C90 6.5.2.2, C99 6.7.2.2).
1562 enum attributes[opt] identifier[opt] { enumerator-list } attributes[opt]
1563 enum attributes[opt] identifier[opt] { enumerator-list , } attributes[opt]
1564 enum attributes[opt] identifier
1566 The form with trailing comma is new in C99. The forms with
1567 attributes are GNU extensions. In GNU C, we accept any expression
1568 without commas in the syntax (assignment expressions, not just
1569 conditional expressions); assignment expressions will be diagnosed
1574 enumerator-list , enumerator
1577 enumeration-constant
1578 enumeration-constant = constant-expression
1581 static struct c_typespec
1582 c_parser_enum_specifier (c_parser *parser)
1584 struct c_typespec ret;
1586 tree ident = NULL_TREE;
1587 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ENUM));
1588 c_parser_consume_token (parser);
1589 attrs = c_parser_attributes (parser);
1590 if (c_parser_next_token_is (parser, CPP_NAME))
1592 ident = c_parser_peek_token (parser)->value;
1593 c_parser_consume_token (parser);
1595 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
1597 /* Parse an enum definition. */
1598 tree type = start_enum (ident);
1600 /* We chain the enumerators in reverse order, then put them in
1601 forward order at the end. */
1602 tree values = NULL_TREE;
1603 c_parser_consume_token (parser);
1610 if (c_parser_next_token_is_not (parser, CPP_NAME))
1612 c_parser_error (parser, "expected identifier");
1613 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
1614 values = error_mark_node;
1617 enum_id = c_parser_peek_token (parser)->value;
1618 c_parser_consume_token (parser);
1619 if (c_parser_next_token_is (parser, CPP_EQ))
1621 c_parser_consume_token (parser);
1622 enum_value = c_parser_expr_no_commas (parser, NULL).value;
1625 enum_value = NULL_TREE;
1626 enum_decl = build_enumerator (enum_id, enum_value);
1627 TREE_CHAIN (enum_decl) = values;
1630 if (c_parser_next_token_is (parser, CPP_COMMA))
1633 c_parser_consume_token (parser);
1635 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
1637 if (seen_comma && pedantic && !flag_isoc99)
1638 pedwarn ("comma at end of enumerator list");
1639 c_parser_consume_token (parser);
1644 c_parser_error (parser, "expected %<,%> or %<}%>");
1645 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
1646 values = error_mark_node;
1650 postfix_attrs = c_parser_attributes (parser);
1651 ret.spec = finish_enum (type, nreverse (values),
1652 chainon (attrs, postfix_attrs));
1653 ret.kind = ctsk_tagdef;
1658 c_parser_error (parser, "expected %<{%>");
1659 ret.spec = error_mark_node;
1660 ret.kind = ctsk_tagref;
1663 ret = parser_xref_tag (ENUMERAL_TYPE, ident);
1664 /* In ISO C, enumerated types can be referred to only if already
1666 if (pedantic && !COMPLETE_TYPE_P (ret.spec))
1667 pedwarn ("ISO C forbids forward references to %<enum%> types");
1671 /* Parse a struct or union specifier (C90 6.5.2.1, C99 6.7.2.1).
1673 struct-or-union-specifier:
1674 struct-or-union attributes[opt] identifier[opt]
1675 { struct-contents } attributes[opt]
1676 struct-or-union attributes[opt] identifier
1679 struct-declaration-list
1681 struct-declaration-list:
1682 struct-declaration ;
1683 struct-declaration-list struct-declaration ;
1690 struct-declaration-list struct-declaration
1692 struct-declaration-list:
1693 struct-declaration-list ;
1696 (Note that in the syntax here, unlike that in ISO C, the semicolons
1697 are included here rather than in struct-declaration, in order to
1698 describe the syntax with extra semicolons and missing semicolon at
1703 struct-declaration-list:
1704 @defs ( class-name )
1706 (Note this does not include a trailing semicolon, but can be
1707 followed by further declarations, and gets a pedwarn-if-pedantic
1708 when followed by a semicolon.) */
1710 static struct c_typespec
1711 c_parser_struct_or_union_specifier (c_parser *parser)
1713 struct c_typespec ret;
1715 tree ident = NULL_TREE;
1716 enum tree_code code;
1717 switch (c_parser_peek_token (parser)->keyword)
1728 c_parser_consume_token (parser);
1729 attrs = c_parser_attributes (parser);
1730 if (c_parser_next_token_is (parser, CPP_NAME))
1732 ident = c_parser_peek_token (parser)->value;
1733 c_parser_consume_token (parser);
1735 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
1737 /* Parse a struct or union definition. Start the scope of the
1738 tag before parsing components. */
1739 tree type = start_struct (code, ident);
1741 /* We chain the components in reverse order, then put them in
1742 forward order at the end. Each struct-declaration may
1743 declare multiple components (comma-separated), so we must use
1744 chainon to join them, although when parsing each
1745 struct-declaration we can use TREE_CHAIN directly.
1747 The theory behind all this is that there will be more
1748 semicolon separated fields than comma separated fields, and
1749 so we'll be minimizing the number of node traversals required
1751 tree contents = NULL_TREE;
1752 c_parser_consume_token (parser);
1753 /* Handle the Objective-C @defs construct,
1754 e.g. foo(sizeof(struct{ @defs(ClassName) }));. */
1755 if (c_parser_next_token_is_keyword (parser, RID_AT_DEFS))
1758 gcc_assert (c_dialect_objc ());
1759 c_parser_consume_token (parser);
1760 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
1762 if (c_parser_next_token_is (parser, CPP_NAME)
1763 && c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME)
1765 name = c_parser_peek_token (parser)->value;
1766 c_parser_consume_token (parser);
1770 c_parser_error (parser, "expected class name");
1771 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
1774 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
1776 contents = nreverse (objc_get_class_ivars (name));
1779 /* Parse the struct-declarations and semicolons. Problems with
1780 semicolons are diagnosed here; empty structures are diagnosed
1785 /* Parse any stray semicolon. */
1786 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1789 pedwarn ("extra semicolon in struct or union specified");
1790 c_parser_consume_token (parser);
1793 /* Stop if at the end of the struct or union contents. */
1794 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
1796 c_parser_consume_token (parser);
1799 /* Parse some comma-separated declarations, but not the
1800 trailing semicolon if any. */
1801 decls = c_parser_struct_declaration (parser);
1802 contents = chainon (decls, contents);
1803 /* If no semicolon follows, either we have a parse error or
1804 are at the end of the struct or union and should
1806 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1807 c_parser_consume_token (parser);
1810 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
1811 pedwarn ("no semicolon at end of struct or union");
1814 c_parser_error (parser, "expected %<;%>");
1815 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
1820 postfix_attrs = c_parser_attributes (parser);
1821 ret.spec = finish_struct (type, nreverse (contents),
1822 chainon (attrs, postfix_attrs));
1823 ret.kind = ctsk_tagdef;
1828 c_parser_error (parser, "expected %<{%>");
1829 ret.spec = error_mark_node;
1830 ret.kind = ctsk_tagref;
1832 ret = parser_xref_tag (code, ident);
1836 /* Parse a struct-declaration (C90 6.5.2.1, C99 6.7.2.1), *without*
1837 the trailing semicolon.
1840 specifier-qualifier-list struct-declarator-list
1842 specifier-qualifier-list:
1843 type-specifier specifier-qualifier-list[opt]
1844 type-qualifier specifier-qualifier-list[opt]
1845 attributes specifier-qualifier-list[opt]
1847 struct-declarator-list:
1849 struct-declarator-list , attributes[opt] struct-declarator
1852 declarator attributes[opt]
1853 declarator[opt] : constant-expression attributes[opt]
1858 __extension__ struct-declaration
1859 specifier-qualifier-list
1861 Unlike the ISO C syntax, semicolons are handled elsewhere. The use
1862 of attributes where shown is a GNU extension. In GNU C, we accept
1863 any expression without commas in the syntax (assignment
1864 expressions, not just conditional expressions); assignment
1865 expressions will be diagnosed as non-constant. */
1868 c_parser_struct_declaration (c_parser *parser)
1870 struct c_declspecs *specs;
1872 tree all_prefix_attrs;
1874 if (c_parser_next_token_is_keyword (parser, RID_EXTENSION))
1878 ext = disable_extension_diagnostics ();
1879 c_parser_consume_token (parser);
1880 decl = c_parser_struct_declaration (parser);
1881 restore_extension_diagnostics (ext);
1884 specs = build_null_declspecs ();
1885 c_parser_declspecs (parser, specs, false, true, true);
1887 return error_mark_node;
1888 if (!specs->declspecs_seen_p)
1890 c_parser_error (parser, "expected specifier-qualifier-list");
1893 finish_declspecs (specs);
1894 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1897 if (!specs->type_seen_p)
1900 pedwarn ("ISO C forbids member declarations with no members");
1901 shadow_tag_warned (specs, pedantic);
1906 /* Support for unnamed structs or unions as members of
1907 structs or unions (which is [a] useful and [b] supports
1909 ret = grokfield (build_id_declarator (NULL_TREE), specs, NULL_TREE);
1913 pending_xref_error ();
1914 prefix_attrs = specs->attrs;
1915 all_prefix_attrs = prefix_attrs;
1916 specs->attrs = NULL_TREE;
1920 /* Declaring one or more declarators or un-named bit-fields. */
1921 struct c_declarator *declarator;
1923 if (c_parser_next_token_is (parser, CPP_COLON))
1924 declarator = build_id_declarator (NULL_TREE);
1926 declarator = c_parser_declarator (parser, specs->type_seen_p,
1927 C_DTR_NORMAL, &dummy);
1928 if (declarator == NULL)
1930 c_parser_skip_to_end_of_block_or_statement (parser);
1933 if (c_parser_next_token_is (parser, CPP_COLON)
1934 || c_parser_next_token_is (parser, CPP_COMMA)
1935 || c_parser_next_token_is (parser, CPP_SEMICOLON)
1936 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE)
1937 || c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
1939 tree postfix_attrs = NULL_TREE;
1940 tree width = NULL_TREE;
1942 if (c_parser_next_token_is (parser, CPP_COLON))
1944 c_parser_consume_token (parser);
1945 width = c_parser_expr_no_commas (parser, NULL).value;
1947 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
1948 postfix_attrs = c_parser_attributes (parser);
1949 d = grokfield (declarator, specs, width);
1950 decl_attributes (&d, chainon (postfix_attrs,
1951 all_prefix_attrs), 0);
1952 TREE_CHAIN (d) = decls;
1954 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
1955 all_prefix_attrs = chainon (c_parser_attributes (parser),
1958 all_prefix_attrs = prefix_attrs;
1959 if (c_parser_next_token_is (parser, CPP_COMMA))
1960 c_parser_consume_token (parser);
1961 else if (c_parser_next_token_is (parser, CPP_SEMICOLON)
1962 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
1964 /* Semicolon consumed in caller. */
1969 c_parser_error (parser, "expected %<,%>, %<;%> or %<}%>");
1975 c_parser_error (parser,
1976 "expected %<:%>, %<,%>, %<;%>, %<}%> or "
1977 "%<__attribute__%>");
1984 /* Parse a typeof specifier (a GNU extension).
1987 typeof ( expression )
1988 typeof ( type-name )
1991 static struct c_typespec
1992 c_parser_typeof_specifier (c_parser *parser)
1994 struct c_typespec ret;
1995 ret.kind = ctsk_typeof;
1996 ret.spec = error_mark_node;
1997 gcc_assert (c_parser_next_token_is_keyword (parser, RID_TYPEOF));
1998 c_parser_consume_token (parser);
2001 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2007 if (c_parser_next_token_starts_typename (parser))
2009 struct c_type_name *type = c_parser_type_name (parser);
2014 ret.spec = groktypename (type);
2015 pop_maybe_used (variably_modified_type_p (ret.spec, NULL_TREE));
2020 struct c_expr expr = c_parser_expression (parser);
2023 if (TREE_CODE (expr.value) == COMPONENT_REF
2024 && DECL_C_BIT_FIELD (TREE_OPERAND (expr.value, 1)))
2025 error ("%<typeof%> applied to a bit-field");
2026 ret.spec = TREE_TYPE (expr.value);
2027 pop_maybe_used (variably_modified_type_p (ret.spec, NULL_TREE));
2029 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
2033 /* Parse a declarator, possibly an abstract declarator (C90 6.5.4,
2034 6.5.5, C99 6.7.5, 6.7.6). If TYPE_SEEN_P then a typedef name may
2035 be redeclared; otherwise it may not. KIND indicates which kind of
2036 declarator is wanted. Returns a valid declarator except in the
2037 case of a syntax error in which case NULL is returned. *SEEN_ID is
2038 set to true if an identifier being declared is seen; this is used
2039 to diagnose bad forms of abstract array declarators and to
2040 determine whether an identifier list is syntactically permitted.
2043 pointer[opt] direct-declarator
2047 ( attributes[opt] declarator )
2048 direct-declarator array-declarator
2049 direct-declarator ( parameter-type-list )
2050 direct-declarator ( identifier-list[opt] )
2053 * type-qualifier-list[opt]
2054 * type-qualifier-list[opt] pointer
2056 type-qualifier-list:
2059 type-qualifier-list type-qualifier
2060 type-qualifier-list attributes
2062 parameter-type-list:
2064 parameter-list , ...
2067 parameter-declaration
2068 parameter-list , parameter-declaration
2070 parameter-declaration:
2071 declaration-specifiers declarator attributes[opt]
2072 declaration-specifiers abstract-declarator[opt] attributes[opt]
2076 identifier-list , identifier
2078 abstract-declarator:
2080 pointer[opt] direct-abstract-declarator
2082 direct-abstract-declarator:
2083 ( attributes[opt] abstract-declarator )
2084 direct-abstract-declarator[opt] array-declarator
2085 direct-abstract-declarator[opt] ( parameter-type-list[opt] )
2090 direct-declarator ( parameter-forward-declarations
2091 parameter-type-list[opt] )
2093 direct-abstract-declarator:
2094 direct-abstract-declarator[opt] ( parameter-forward-declarations
2095 parameter-type-list[opt] )
2097 parameter-forward-declarations:
2099 parameter-forward-declarations parameter-list ;
2101 The uses of attributes shown above are GNU extensions.
2103 Some forms of array declarator are not included in C99 in the
2104 syntax for abstract declarators; these are disallowed elsewhere.
2105 This may be a defect (DR#289).
2107 This function also accepts an omitted abstract declarator as being
2108 an abstract declarator, although not part of the formal syntax. */
2110 static struct c_declarator *
2111 c_parser_declarator (c_parser *parser, bool type_seen_p, c_dtr_syn kind,
2114 /* Parse any initial pointer part. */
2115 if (c_parser_next_token_is (parser, CPP_MULT))
2117 struct c_declspecs *quals_attrs = build_null_declspecs ();
2118 struct c_declarator *inner;
2119 c_parser_consume_token (parser);
2120 c_parser_declspecs (parser, quals_attrs, false, false, true);
2121 inner = c_parser_declarator (parser, type_seen_p, kind, seen_id);
2125 return make_pointer_declarator (quals_attrs, inner);
2127 /* Now we have a direct declarator, direct abstract declarator or
2128 nothing (which counts as a direct abstract declarator here). */
2129 return c_parser_direct_declarator (parser, type_seen_p, kind, seen_id);
2132 /* Parse a direct declarator or direct abstract declarator; arguments
2133 as c_parser_declarator. */
2135 static struct c_declarator *
2136 c_parser_direct_declarator (c_parser *parser, bool type_seen_p, c_dtr_syn kind,
2139 /* The direct declarator must start with an identifier (possibly
2140 omitted) or a parenthesized declarator (possibly abstract). In
2141 an ordinary declarator, initial parentheses must start a
2142 parenthesized declarator. In an abstract declarator or parameter
2143 declarator, they could start a parenthesized declarator or a
2144 parameter list. To tell which, the open parenthesis and any
2145 following attributes must be read. If a declaration specifier
2146 follows, then it is a parameter list; if the specifier is a
2147 typedef name, there might be an ambiguity about redeclaring it,
2148 which is resolved in the direction of treating it as a typedef
2149 name. If a close parenthesis follows, it is also an empty
2150 parameter list, as the syntax does not permit empty abstract
2151 declarators. Otherwise, it is a parenthesised declarator (in
2152 which case the analysis may be repeated inside it, recursively).
2154 ??? There is an ambiguity in a parameter declaration "int
2155 (__attribute__((foo)) x)", where x is not a typedef name: it
2156 could be an abstract declarator for a function, or declare x with
2157 parentheses. The proper resolution of this ambiguity needs
2158 documenting. At present we follow an accident of the old
2159 parser's implementation, whereby the first parameter must have
2160 some declaration specifiers other than just attributes. Thus as
2161 a parameter declaration it is treated as a parenthesised
2162 parameter named x, and as an abstract declarator it is
2165 ??? Also following the old parser, attributes inside an empty
2166 parameter list are ignored, making it a list not yielding a
2167 prototype, rather than giving an error or making it have one
2168 parameter with implicit type int.
2170 ??? Also following the old parser, typedef names may be
2171 redeclared in declarators, but not Objective-C class names. */
2173 if (kind != C_DTR_ABSTRACT
2174 && c_parser_next_token_is (parser, CPP_NAME)
2176 && c_parser_peek_token (parser)->id_kind == C_ID_TYPENAME)
2177 || c_parser_peek_token (parser)->id_kind == C_ID_ID))
2179 struct c_declarator *inner
2180 = build_id_declarator (c_parser_peek_token (parser)->value);
2182 c_parser_consume_token (parser);
2183 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
2186 if (kind != C_DTR_NORMAL
2187 && c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
2189 struct c_declarator *inner = build_id_declarator (NULL_TREE);
2190 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
2193 /* Either we are at the end of an abstract declarator, or we have
2196 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
2199 struct c_declarator *inner;
2200 c_parser_consume_token (parser);
2201 attrs = c_parser_attributes (parser);
2202 if (kind != C_DTR_NORMAL
2203 && (c_parser_next_token_starts_declspecs (parser)
2204 || c_parser_next_token_is (parser, CPP_CLOSE_PAREN)))
2206 struct c_arg_info *args
2207 = c_parser_parms_declarator (parser, kind == C_DTR_NORMAL,
2214 = build_function_declarator (args,
2215 build_id_declarator (NULL_TREE));
2216 return c_parser_direct_declarator_inner (parser, *seen_id,
2220 /* A parenthesized declarator. */
2221 inner = c_parser_declarator (parser, type_seen_p, kind, seen_id);
2222 if (inner != NULL && attrs != NULL)
2223 inner = build_attrs_declarator (attrs, inner);
2224 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2226 c_parser_consume_token (parser);
2230 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
2234 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2241 if (kind == C_DTR_NORMAL)
2243 c_parser_error (parser, "expected identifier or %<(%>");
2247 return build_id_declarator (NULL_TREE);
2251 /* Parse part of a direct declarator or direct abstract declarator,
2252 given that some (in INNER) has already been parsed; ID_PRESENT is
2253 true if an identifier is present, false for an abstract
2256 static struct c_declarator *
2257 c_parser_direct_declarator_inner (c_parser *parser, bool id_present,
2258 struct c_declarator *inner)
2260 /* Parse a sequence of array declarators and parameter lists. */
2261 if (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
2263 struct c_declarator *declarator;
2264 struct c_declspecs *quals_attrs = build_null_declspecs ();
2268 c_parser_consume_token (parser);
2269 c_parser_declspecs (parser, quals_attrs, false, false, true);
2270 static_seen = c_parser_next_token_is_keyword (parser, RID_STATIC);
2272 c_parser_consume_token (parser);
2273 if (static_seen && !quals_attrs->declspecs_seen_p)
2274 c_parser_declspecs (parser, quals_attrs, false, false, true);
2275 if (!quals_attrs->declspecs_seen_p)
2277 /* If "static" is present, there must be an array dimension.
2278 Otherwise, there may be a dimension, "*", or no
2283 dimen = c_parser_expr_no_commas (parser, NULL).value;
2287 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
2292 else if (c_parser_next_token_is (parser, CPP_MULT))
2294 if (c_parser_peek_2nd_token (parser)->type == CPP_CLOSE_SQUARE)
2298 c_parser_consume_token (parser);
2303 dimen = c_parser_expr_no_commas (parser, NULL).value;
2309 dimen = c_parser_expr_no_commas (parser, NULL).value;
2312 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
2313 c_parser_consume_token (parser);
2316 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
2320 declarator = build_array_declarator (dimen, quals_attrs, static_seen,
2322 inner = set_array_declarator_inner (declarator, inner, !id_present);
2323 return c_parser_direct_declarator_inner (parser, id_present, inner);
2325 else if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
2328 struct c_arg_info *args;
2329 c_parser_consume_token (parser);
2330 attrs = c_parser_attributes (parser);
2331 args = c_parser_parms_declarator (parser, id_present, attrs);
2336 inner = build_function_declarator (args, inner);
2337 return c_parser_direct_declarator_inner (parser, id_present, inner);
2343 /* Parse a parameter list or identifier list, including the closing
2344 parenthesis but not the opening one. ATTRS are the attributes at
2345 the start of the list. ID_LIST_OK is true if an identifier list is
2346 acceptable; such a list must not have attributes at the start. */
2348 static struct c_arg_info *
2349 c_parser_parms_declarator (c_parser *parser, bool id_list_ok, tree attrs)
2352 declare_parm_level ();
2353 /* If the list starts with an identifier, it is an identifier list.
2354 Otherwise, it is either a prototype list or an empty list. */
2357 && c_parser_next_token_is (parser, CPP_NAME)
2358 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
2360 tree list = NULL_TREE;
2361 while (c_parser_next_token_is (parser, CPP_NAME)
2362 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
2364 list = chainon (list, build_tree_list (NULL_TREE,
2365 c_parser_peek_token (parser)->value));
2366 c_parser_consume_token (parser);
2367 if (c_parser_next_token_is_not (parser, CPP_COMMA))
2369 c_parser_consume_token (parser);
2370 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2372 c_parser_error (parser, "expected identifier");
2376 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2378 struct c_arg_info *ret = XOBNEW (&parser_obstack, struct c_arg_info);
2383 c_parser_consume_token (parser);
2389 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2397 struct c_arg_info *ret = c_parser_parms_list_declarator (parser, attrs);
2403 /* Parse a parameter list (possibly empty), including the closing
2404 parenthesis but not the opening one. ATTRS are the attributes at
2405 the start of the list. */
2407 static struct c_arg_info *
2408 c_parser_parms_list_declarator (c_parser *parser, tree attrs)
2410 bool good_parm = false;
2411 /* ??? Following the old parser, forward parameter declarations may
2412 use abstract declarators, and if no real parameter declarations
2413 follow the forward declarations then this is not diagnosed. Also
2414 note as above that attributes are ignored as the only contents of
2415 the parentheses, or as the only contents after forward
2417 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2419 struct c_arg_info *ret = XOBNEW (&parser_obstack, struct c_arg_info);
2424 c_parser_consume_token (parser);
2427 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
2429 struct c_arg_info *ret = XOBNEW (&parser_obstack, struct c_arg_info);
2433 /* Suppress -Wold-style-definition for this case. */
2434 ret->types = error_mark_node;
2435 error ("ISO C requires a named argument before %<...%>");
2436 c_parser_consume_token (parser);
2437 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2439 c_parser_consume_token (parser);
2444 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2449 /* Nonempty list of parameters, either terminated with semicolon
2450 (forward declarations; recurse) or with close parenthesis (normal
2451 function) or with ", ... )" (variadic function). */
2454 /* Parse a parameter. */
2455 struct c_parm *parm = c_parser_parameter_declaration (parser, attrs);
2460 push_parm_decl (parm);
2462 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
2465 c_parser_consume_token (parser);
2466 new_attrs = c_parser_attributes (parser);
2467 return c_parser_parms_list_declarator (parser, new_attrs);
2469 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2471 c_parser_consume_token (parser);
2473 return get_parm_info (false);
2476 struct c_arg_info *ret
2477 = XOBNEW (&parser_obstack, struct c_arg_info);
2485 if (!c_parser_require (parser, CPP_COMMA,
2486 "expected %<;%>, %<,%> or %<)%>"))
2488 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
2491 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
2493 c_parser_consume_token (parser);
2494 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2496 c_parser_consume_token (parser);
2498 return get_parm_info (true);
2501 struct c_arg_info *ret
2502 = XOBNEW (&parser_obstack, struct c_arg_info);
2512 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2520 /* Parse a parameter declaration. ATTRS are the attributes at the
2521 start of the declaration if it is the first parameter. */
2523 static struct c_parm *
2524 c_parser_parameter_declaration (c_parser *parser, tree attrs)
2526 struct c_declspecs *specs;
2527 struct c_declarator *declarator;
2529 tree postfix_attrs = NULL_TREE;
2531 if (!c_parser_next_token_starts_declspecs (parser))
2533 /* ??? In some Objective-C cases '...' isn't applicable so there
2534 should be a different message. */
2535 c_parser_error (parser,
2536 "expected declaration specifiers or %<...%>");
2537 c_parser_skip_to_end_of_parameter (parser);
2540 specs = build_null_declspecs ();
2543 declspecs_add_attrs (specs, attrs);
2546 c_parser_declspecs (parser, specs, true, true, true);
2547 finish_declspecs (specs);
2548 pending_xref_error ();
2549 prefix_attrs = specs->attrs;
2550 specs->attrs = NULL_TREE;
2551 declarator = c_parser_declarator (parser, specs->type_seen_p,
2552 C_DTR_PARM, &dummy);
2553 if (declarator == NULL)
2555 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
2558 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2559 postfix_attrs = c_parser_attributes (parser);
2560 return build_c_parm (specs, chainon (postfix_attrs, prefix_attrs),
2564 /* Parse a string literal in an asm expression. It should not be
2565 translated, and wide string literals are an error although
2566 permitted by the syntax. This is a GNU extension.
2571 ??? At present, following the old parser, the caller needs to have
2572 set c_lex_string_translate to 0. It would be better to follow the
2573 C++ parser rather than using the c_lex_string_translate kludge. */
2576 c_parser_asm_string_literal (c_parser *parser)
2579 if (c_parser_next_token_is (parser, CPP_STRING))
2581 str = c_parser_peek_token (parser)->value;
2582 c_parser_consume_token (parser);
2584 else if (c_parser_next_token_is (parser, CPP_WSTRING))
2586 error ("wide string literal in %<asm%>");
2587 str = build_string (1, "");
2588 c_parser_consume_token (parser);
2592 c_parser_error (parser, "expected string literal");
2598 /* Parse a simple asm expression. This is used in restricted
2599 contexts, where a full expression with inputs and outputs does not
2600 make sense. This is a GNU extension.
2603 asm ( asm-string-literal )
2607 c_parser_simple_asm_expr (c_parser *parser)
2610 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ASM));
2611 /* ??? Follow the C++ parser rather than using the
2612 c_lex_string_translate kludge. */
2613 c_lex_string_translate = 0;
2614 c_parser_consume_token (parser);
2615 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2617 c_lex_string_translate = 1;
2620 str = c_parser_asm_string_literal (parser);
2621 c_lex_string_translate = 1;
2622 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
2624 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
2630 /* Parse (possibly empty) attributes. This is a GNU extension.
2634 attributes attribute
2637 __attribute__ ( ( attribute-list ) )
2641 attribute_list , attrib
2646 any-word ( identifier )
2647 any-word ( identifier , nonempty-expr-list )
2648 any-word ( expr-list )
2650 where the "identifier" must not be declared as a type, and
2651 "any-word" may be any identifier (including one declared as a
2652 type), a reserved word storage class specifier, type specifier or
2653 type qualifier. ??? This still leaves out most reserved keywords
2654 (following the old parser), shouldn't we include them, and why not
2655 allow identifiers declared as types to start the arguments? */
2658 c_parser_attributes (c_parser *parser)
2660 tree attrs = NULL_TREE;
2661 while (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2663 /* ??? Follow the C++ parser rather than using the
2664 c_lex_string_translate kludge. */
2665 c_lex_string_translate = 0;
2666 c_parser_consume_token (parser);
2667 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2669 c_lex_string_translate = 1;
2672 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2674 c_lex_string_translate = 1;
2675 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
2678 /* Parse the attribute list. */
2679 while (c_parser_next_token_is (parser, CPP_COMMA)
2680 || c_parser_next_token_is (parser, CPP_NAME)
2681 || c_parser_next_token_is (parser, CPP_KEYWORD))
2683 tree attr, attr_name, attr_args;
2684 if (c_parser_next_token_is (parser, CPP_COMMA))
2686 c_parser_consume_token (parser);
2689 if (c_parser_next_token_is (parser, CPP_KEYWORD))
2691 /* ??? See comment above about what keywords are
2694 switch (c_parser_peek_token (parser)->keyword)
2726 attr_name = c_parser_peek_token (parser)->value;
2727 c_parser_consume_token (parser);
2728 if (c_parser_next_token_is_not (parser, CPP_OPEN_PAREN))
2730 attr = build_tree_list (attr_name, NULL_TREE);
2731 attrs = chainon (attrs, attr);
2734 c_parser_consume_token (parser);
2735 /* Parse the attribute contents. If they start with an
2736 identifier which is followed by a comma or close
2737 parenthesis, then the arguments start with that
2738 identifier; otherwise they are an expression list. */
2739 if (c_parser_next_token_is (parser, CPP_NAME)
2740 && c_parser_peek_token (parser)->id_kind == C_ID_ID
2741 && ((c_parser_peek_2nd_token (parser)->type == CPP_COMMA)
2742 || (c_parser_peek_2nd_token (parser)->type
2743 == CPP_CLOSE_PAREN)))
2745 tree arg1 = c_parser_peek_token (parser)->value;
2746 c_parser_consume_token (parser);
2747 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2748 attr_args = build_tree_list (NULL_TREE, arg1);
2751 c_parser_consume_token (parser);
2752 attr_args = tree_cons (NULL_TREE, arg1,
2753 c_parser_expr_list (parser));
2758 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2759 attr_args = NULL_TREE;
2761 attr_args = c_parser_expr_list (parser);
2763 attr = build_tree_list (attr_name, attr_args);
2764 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2765 c_parser_consume_token (parser);
2768 c_lex_string_translate = 1;
2769 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2773 attrs = chainon (attrs, attr);
2775 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2776 c_parser_consume_token (parser);
2779 c_lex_string_translate = 1;
2780 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2784 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2785 c_parser_consume_token (parser);
2788 c_lex_string_translate = 1;
2789 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2793 c_lex_string_translate = 1;
2798 /* Parse a type name (C90 6.5.5, C99 6.7.6).
2801 specifier-qualifier-list abstract-declarator[opt]
2804 static struct c_type_name *
2805 c_parser_type_name (c_parser *parser)
2807 struct c_declspecs *specs = build_null_declspecs ();
2808 struct c_declarator *declarator;
2809 struct c_type_name *ret;
2811 c_parser_declspecs (parser, specs, false, true, true);
2812 if (!specs->declspecs_seen_p)
2814 c_parser_error (parser, "expected specifier-qualifier-list");
2817 pending_xref_error ();
2818 finish_declspecs (specs);
2819 declarator = c_parser_declarator (parser, specs->type_seen_p,
2820 C_DTR_ABSTRACT, &dummy);
2821 if (declarator == NULL)
2823 ret = XOBNEW (&parser_obstack, struct c_type_name);
2825 ret->declarator = declarator;
2829 /* Parse an initializer (C90 6.5.7, C99 6.7.8).
2832 assignment-expression
2833 { initializer-list }
2834 { initializer-list , }
2837 designation[opt] initializer
2838 initializer-list , designation[opt] initializer
2845 designator-list designator
2852 [ constant-expression ]
2864 [ constant-expression ... constant-expression ]
2866 Any expression without commas is accepted in the syntax for the
2867 constant-expressions, with non-constant expressions rejected later.
2869 This function is only used for top-level initializers; for nested
2870 ones, see c_parser_initval. */
2872 static struct c_expr
2873 c_parser_initializer (c_parser *parser)
2875 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
2876 return c_parser_braced_init (parser, NULL_TREE, false);
2878 return c_parser_expr_no_commas (parser, NULL);
2881 /* Parse a braced initializer list. TYPE is the type specified for a
2882 compound literal, and NULL_TREE for other initializers and for
2883 nested braced lists. NESTED_P is true for nested braced lists,
2884 false for the list of a compound literal or the list that is the
2885 top-level initializer in a declaration. */
2887 static struct c_expr
2888 c_parser_braced_init (c_parser *parser, tree type, bool nested_p)
2890 gcc_assert (c_parser_next_token_is (parser, CPP_OPEN_BRACE));
2891 c_parser_consume_token (parser);
2893 push_init_level (0);
2895 really_start_incremental_init (type);
2896 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2899 pedwarn ("ISO C forbids empty initializer braces");
2903 /* Parse a non-empty initializer list, possibly with a trailing
2907 c_parser_initelt (parser);
2910 if (c_parser_next_token_is (parser, CPP_COMMA))
2911 c_parser_consume_token (parser);
2914 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2918 if (c_parser_next_token_is_not (parser, CPP_CLOSE_BRACE))
2921 ret.value = error_mark_node;
2922 ret.original_code = ERROR_MARK;
2923 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, "expected %<}%>");
2926 c_parser_consume_token (parser);
2927 return pop_init_level (0);
2930 /* Parse a nested initializer, including designators. */
2933 c_parser_initelt (c_parser *parser)
2935 /* Parse any designator or designator list. A single array
2936 designator may have the subsequent "=" omitted in GNU C, but a
2937 longer list or a structure member designator may not. */
2938 if (c_parser_next_token_is (parser, CPP_NAME)
2939 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
2941 /* Old-style structure member designator. */
2942 set_init_label (c_parser_peek_token (parser)->value);
2944 pedwarn ("obsolete use of designated initializer with %<:%>");
2945 c_parser_consume_token (parser);
2946 c_parser_consume_token (parser);
2950 /* des_seen is 0 if there have been no designators, 1 if there
2951 has been a single array designator and 2 otherwise. */
2953 while (c_parser_next_token_is (parser, CPP_OPEN_SQUARE)
2954 || c_parser_next_token_is (parser, CPP_DOT))
2956 int des_prev = des_seen;
2959 if (c_parser_next_token_is (parser, CPP_DOT))
2962 c_parser_consume_token (parser);
2963 if (c_parser_next_token_is (parser, CPP_NAME))
2965 set_init_label (c_parser_peek_token (parser)->value);
2966 c_parser_consume_token (parser);
2971 init.value = error_mark_node;
2972 init.original_code = ERROR_MARK;
2973 c_parser_error (parser, "expected identifier");
2974 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
2975 process_init_element (init);
2982 /* ??? Following the old parser, [ objc-receiver
2983 objc-message-args ] is accepted as an initializer,
2984 being distinguished from a designator by what follows
2985 the first assignment expression inside the square
2986 brackets, but after a first array designator a
2987 subsequent square bracket is for Objective-C taken to
2988 start an expression, using the obsolete form of
2989 designated initializer without '=', rather than
2990 possibly being a second level of designation: in LALR
2991 terms, the '[' is shifted rather than reducing
2992 designator to designator-list. */
2993 if (des_prev == 1 && c_dialect_objc ())
2995 des_seen = des_prev;
2998 if (des_prev == 0 && c_dialect_objc ())
3000 /* This might be an array designator or an
3001 Objective-C message expression. If the former,
3002 continue parsing here; if the latter, parse the
3003 remainder of the initializer given the starting
3004 primary-expression. ??? It might make sense to
3005 distinguish when des_prev == 1 as well; see
3006 previous comment. */
3008 struct c_expr mexpr;
3009 c_parser_consume_token (parser);
3010 if (c_parser_peek_token (parser)->type == CPP_NAME
3011 && ((c_parser_peek_token (parser)->id_kind
3013 || (c_parser_peek_token (parser)->id_kind
3014 == C_ID_CLASSNAME)))
3016 /* Type name receiver. */
3017 tree id = c_parser_peek_token (parser)->value;
3018 c_parser_consume_token (parser);
3019 rec = objc_get_class_reference (id);
3020 goto parse_message_args;
3022 first = c_parser_expr_no_commas (parser, NULL).value;
3023 if (c_parser_next_token_is (parser, CPP_ELLIPSIS)
3024 || c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
3025 goto array_desig_after_first;
3026 /* Expression receiver. So far only one part
3027 without commas has been parsed; there might be
3028 more of the expression. */
3030 while (c_parser_next_token_is (parser, CPP_COMMA))
3033 c_parser_consume_token (parser);
3034 next = c_parser_expr_no_commas (parser, NULL).value;
3035 rec = build_compound_expr (rec, next);
3038 /* Now parse the objc-message-args. */
3039 args = c_parser_objc_message_args (parser);
3040 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
3043 = objc_build_message_expr (build_tree_list (rec, args));
3044 mexpr.original_code = ERROR_MARK;
3045 /* Now parse and process the remainder of the
3046 initializer, starting with this message
3047 expression as a primary-expression. */
3048 c_parser_initval (parser, &mexpr);
3051 c_parser_consume_token (parser);
3052 first = c_parser_expr_no_commas (parser, NULL).value;
3053 array_desig_after_first:
3054 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
3056 c_parser_consume_token (parser);
3057 second = c_parser_expr_no_commas (parser, NULL).value;
3061 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
3063 c_parser_consume_token (parser);
3064 set_init_index (first, second);
3065 if (pedantic && second)
3066 pedwarn ("ISO C forbids specifying range of "
3067 "elements to initialize");
3070 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
3076 if (c_parser_next_token_is (parser, CPP_EQ))
3078 if (pedantic && !flag_isoc99)
3079 pedwarn ("ISO C90 forbids specifying subobject to initialize");
3080 c_parser_consume_token (parser);
3087 pedwarn ("obsolete use of designated initializer "
3093 init.value = error_mark_node;
3094 init.original_code = ERROR_MARK;
3095 c_parser_error (parser, "expected %<=%>");
3096 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
3097 process_init_element (init);
3103 c_parser_initval (parser, NULL);
3106 /* Parse a nested initializer; as c_parser_initializer but parses
3107 initializers within braced lists, after any designators have been
3108 applied. If AFTER is not NULL then it is an Objective-C message
3109 expression which is the primary-expression starting the
3113 c_parser_initval (c_parser *parser, struct c_expr *after)
3116 gcc_assert (!after || c_dialect_objc ());
3117 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE) && !after)
3118 init = c_parser_braced_init (parser, NULL_TREE, true);
3120 init = c_parser_expr_no_commas (parser, after);
3121 process_init_element (init);
3124 /* Parse a compound statement (possibly a function body) (C90 6.6.2,
3128 { block-item-list[opt] }
3129 { label-declarations block-item-list }
3133 block-item-list block-item
3145 { label-declarations block-item-list }
3148 __extension__ nested-declaration
3149 nested-function-definition
3153 label-declarations label-declaration
3156 __label__ identifier-list ;
3158 Allowing the mixing of declarations and code is new in C99. The
3159 GNU syntax also permits (not shown above) labels at the end of
3160 compound statements, which yield an error. We don't allow labels
3161 on declarations; this might seem like a natural extension, but
3162 there would be a conflict between attributes on the label and
3163 prefix attributes on the declaration. ??? The syntax follows the
3164 old parser in requiring something after label declarations.
3165 Although they are erroneous if the labels declared aren't defined,
3166 is it useful for the syntax to be this way? */
3169 c_parser_compound_statement (c_parser *parser)
3172 if (!c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
3173 return error_mark_node;
3174 stmt = c_begin_compound_stmt (true);
3175 c_parser_compound_statement_nostart (parser);
3176 return c_end_compound_stmt (stmt, true);
3179 /* Parse a compound statement except for the opening brace. This is
3180 used for parsing both compound statements and statement expressions
3181 (which follow different paths to handling the opening). */
3184 c_parser_compound_statement_nostart (c_parser *parser)
3186 bool last_stmt = false;
3187 bool last_label = false;
3188 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3190 c_parser_consume_token (parser);
3193 if (c_parser_next_token_is_keyword (parser, RID_LABEL))
3195 /* Read zero or more forward-declarations for labels that nested
3196 functions can jump to. */
3197 while (c_parser_next_token_is_keyword (parser, RID_LABEL))
3199 c_parser_consume_token (parser);
3200 /* Any identifiers, including those declared as type names,
3205 if (c_parser_next_token_is_not (parser, CPP_NAME))
3207 c_parser_error (parser, "expected identifier");
3211 = declare_label (c_parser_peek_token (parser)->value);
3212 C_DECLARED_LABEL_FLAG (label) = 1;
3213 add_stmt (build_stmt (DECL_EXPR, label));
3214 c_parser_consume_token (parser);
3215 if (c_parser_next_token_is (parser, CPP_COMMA))
3216 c_parser_consume_token (parser);
3220 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
3222 /* ??? Locating this diagnostic on the token after the
3223 declarations end follows the old parser, but it might be
3224 better to locate it where the declarations start instead. */
3226 pedwarn ("ISO C forbids label declarations");
3228 /* We must now have at least one statement, label or declaration. */
3229 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3231 c_parser_error (parser, "expected declaration or statement");
3232 c_parser_consume_token (parser);
3235 while (c_parser_next_token_is_not (parser, CPP_CLOSE_BRACE))
3237 location_t loc = c_parser_peek_token (parser)->location;
3238 if (c_parser_next_token_is (parser, CPP_EOF))
3240 parser->error = true;
3243 if (c_parser_next_token_is_keyword (parser, RID_CASE)
3244 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
3245 || (c_parser_next_token_is (parser, CPP_NAME)
3246 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
3250 c_parser_label (parser);
3252 else if (!last_label
3253 && c_parser_next_token_starts_declspecs (parser))
3256 c_parser_declaration_or_fndef (parser, true, true, true, true);
3258 && ((pedantic && !flag_isoc99)
3259 || warn_declaration_after_statement))
3260 pedwarn_c90 ("%HISO C90 forbids mixed declarations and code",
3264 else if (!last_label
3265 && c_parser_next_token_is_keyword (parser, RID_EXTENSION))
3267 /* __extension__ can start a declaration, but is also an
3268 unary operator that can start an expression. Consume all
3269 but the last of a possible series of __extension__ to
3271 while (c_parser_peek_2nd_token (parser)->type == CPP_KEYWORD
3272 && (c_parser_peek_2nd_token (parser)->keyword
3274 c_parser_consume_token (parser);
3275 if (c_token_starts_declspecs (c_parser_peek_2nd_token (parser)))
3278 ext = disable_extension_diagnostics ();
3279 c_parser_consume_token (parser);
3281 c_parser_declaration_or_fndef (parser, true, true, true, true);
3282 /* Following the old parser, __extension__ does not
3283 disable this diagnostic. */
3284 restore_extension_diagnostics (ext);
3286 && ((pedantic && !flag_isoc99)
3287 || warn_declaration_after_statement))
3288 pedwarn_c90 ("%HISO C90 forbids mixed declarations and code",
3300 c_parser_statement_after_labels (parser);
3304 error ("label at end of compound statement");
3305 c_parser_consume_token (parser);
3308 /* Parse a label (C90 6.6.1, C99 6.8.1).
3311 identifier : attributes[opt]
3312 case constant-expression :
3318 case constant-expression ... constant-expression :
3320 The use of attributes on labels is a GNU extension. The syntax in
3321 GNU C accepts any expressions without commas, non-constant
3322 expressions being rejected later. */
3325 c_parser_label (c_parser *parser)
3327 location_t loc1 = c_parser_peek_token (parser)->location;
3328 tree label = NULL_TREE;
3329 if (c_parser_next_token_is_keyword (parser, RID_CASE))
3332 c_parser_consume_token (parser);
3333 exp1 = c_parser_expr_no_commas (parser, NULL).value;
3334 if (c_parser_next_token_is (parser, CPP_COLON))
3336 c_parser_consume_token (parser);
3337 label = do_case (exp1, NULL_TREE);
3339 else if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
3341 c_parser_consume_token (parser);
3342 exp2 = c_parser_expr_no_commas (parser, NULL).value;
3343 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
3344 label = do_case (exp1, exp2);
3347 c_parser_error (parser, "expected %<:%> or %<...%>");
3349 else if (c_parser_next_token_is_keyword (parser, RID_DEFAULT))
3351 c_parser_consume_token (parser);
3352 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
3353 label = do_case (NULL_TREE, NULL_TREE);
3357 tree name = c_parser_peek_token (parser)->value;
3361 gcc_assert (c_parser_next_token_is (parser, CPP_NAME));
3362 c_parser_consume_token (parser);
3363 gcc_assert (c_parser_next_token_is (parser, CPP_COLON));
3364 loc2 = c_parser_peek_token (parser)->location;
3365 c_parser_consume_token (parser);
3366 attrs = c_parser_attributes (parser);
3367 tlab = define_label (loc2, name);
3370 decl_attributes (&tlab, attrs, 0);
3371 label = add_stmt (build_stmt (LABEL_EXPR, tlab));
3375 SET_EXPR_LOCATION (label, loc1);
3378 /* Parse a statement (C90 6.6, C99 6.8).
3383 expression-statement
3391 expression-statement:
3394 selection-statement:
3398 iteration-statement:
3407 return expression[opt] ;
3420 objc-throw-statement
3421 objc-try-catch-statement
3422 objc-synchronized-statement
3424 objc-throw-statement:
3430 c_parser_statement (c_parser *parser)
3432 while (c_parser_next_token_is_keyword (parser, RID_CASE)
3433 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
3434 || (c_parser_next_token_is (parser, CPP_NAME)
3435 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
3436 c_parser_label (parser);
3437 c_parser_statement_after_labels (parser);
3440 /* Parse a statement, other than a labeled statement. */
3443 c_parser_statement_after_labels (c_parser *parser)
3445 location_t loc = c_parser_peek_token (parser)->location;
3446 tree stmt = NULL_TREE;
3447 switch (c_parser_peek_token (parser)->type)
3449 case CPP_OPEN_BRACE:
3450 add_stmt (c_parser_compound_statement (parser));
3453 switch (c_parser_peek_token (parser)->keyword)
3456 c_parser_if_statement (parser);
3459 c_parser_switch_statement (parser);
3462 c_parser_while_statement (parser);
3465 c_parser_do_statement (parser);
3468 c_parser_for_statement (parser);
3471 c_parser_consume_token (parser);
3472 if (c_parser_next_token_is (parser, CPP_NAME))
3474 stmt = c_finish_goto_label (c_parser_peek_token (parser)->value);
3475 c_parser_consume_token (parser);
3477 else if (c_parser_next_token_is (parser, CPP_MULT))
3479 c_parser_consume_token (parser);
3480 stmt = c_finish_goto_ptr (c_parser_expression (parser).value);
3483 c_parser_error (parser, "expected identifier or %<*%>");
3484 goto expect_semicolon;
3486 c_parser_consume_token (parser);
3487 stmt = c_finish_bc_stmt (&c_cont_label, false);
3488 goto expect_semicolon;
3490 c_parser_consume_token (parser);
3491 stmt = c_finish_bc_stmt (&c_break_label, true);
3492 goto expect_semicolon;
3494 c_parser_consume_token (parser);
3495 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
3497 stmt = c_finish_return (NULL_TREE);
3498 c_parser_consume_token (parser);
3502 stmt = c_finish_return (c_parser_expression (parser).value);
3503 goto expect_semicolon;
3507 stmt = c_parser_asm_statement (parser);
3510 gcc_assert (c_dialect_objc ());
3511 c_parser_consume_token (parser);
3512 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
3514 stmt = objc_build_throw_stmt (NULL_TREE);
3515 c_parser_consume_token (parser);
3520 = objc_build_throw_stmt (c_parser_expression (parser).value);
3521 goto expect_semicolon;
3525 gcc_assert (c_dialect_objc ());
3526 c_parser_objc_try_catch_statement (parser);
3528 case RID_AT_SYNCHRONIZED:
3529 gcc_assert (c_dialect_objc ());
3530 c_parser_objc_synchronized_statement (parser);
3537 c_parser_consume_token (parser);
3539 case CPP_CLOSE_PAREN:
3540 case CPP_CLOSE_SQUARE:
3541 /* Avoid infinite loop in error recovery:
3542 c_parser_skip_until_found stops at a closing nesting
3543 delimiter without consuming it, but here we need to consume
3544 it to proceed further. */
3545 c_parser_error (parser, "expected statement");
3546 c_parser_consume_token (parser);
3550 stmt = c_finish_expr_stmt (c_parser_expression (parser).value);
3552 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
3555 /* Two cases cannot and do not have line numbers associated: If stmt
3556 is degenerate, such as "2;", then stmt is an INTEGER_CST, which
3557 cannot hold line numbers. But that's OK because the statement
3558 will either be changed to a MODIFY_EXPR during gimplification of
3559 the statement expr, or discarded. If stmt was compound, but
3560 without new variables, we will have skipped the creation of a
3561 BIND and will have a bare STATEMENT_LIST. But that's OK because
3562 (recursively) all of the component statements should already have
3563 line numbers assigned. ??? Can we discard no-op statements
3565 if (stmt && EXPR_P (stmt))
3566 SET_EXPR_LOCATION (stmt, loc);
3569 /* Parse a parenthesized condition from an if, do or while statement.
3575 c_parser_paren_condition (c_parser *parser)
3579 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3580 return error_mark_node;
3581 loc = c_parser_peek_token (parser)->location;
3582 cond = lang_hooks.truthvalue_conversion (c_parser_expression (parser).value);
3584 SET_EXPR_LOCATION (cond, loc);
3585 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
3589 /* Parse a statement which is a block in C99. */
3592 c_parser_c99_block_statement (c_parser *parser)
3594 tree block = c_begin_compound_stmt (flag_isoc99);
3595 c_parser_statement (parser);
3596 return c_end_compound_stmt (block, flag_isoc99);
3599 /* Parse the body of an if statement or the else half thereof. This
3600 is just parsing a statement but (a) it is a block in C99, (b) we
3601 track whether the body is an if statement for the sake of
3602 -Wparentheses warnings, (c) we handle an empty body specially for
3603 the sake of -Wextra warnings. */
3606 c_parser_if_body (c_parser *parser, bool *if_p)
3608 tree block = c_begin_compound_stmt (flag_isoc99);
3609 while (c_parser_next_token_is_keyword (parser, RID_CASE)
3610 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
3611 || (c_parser_next_token_is (parser, CPP_NAME)
3612 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
3613 c_parser_label (parser);
3614 *if_p = c_parser_next_token_is_keyword (parser, RID_IF);
3615 if (extra_warnings && c_parser_next_token_is (parser, CPP_SEMICOLON))
3616 add_stmt (build (NOP_EXPR, NULL_TREE, NULL_TREE));
3617 c_parser_statement_after_labels (parser);
3618 return c_end_compound_stmt (block, flag_isoc99);
3621 /* Parse an if statement (C90 6.6.4, C99 6.8.4).
3624 if ( expression ) statement
3625 if ( expression ) statement else statement
3629 c_parser_if_statement (c_parser *parser)
3634 bool first_if = false, second_if = false;
3635 tree first_body, second_body;
3636 gcc_assert (c_parser_next_token_is_keyword (parser, RID_IF));
3637 c_parser_consume_token (parser);
3638 block = c_begin_compound_stmt (flag_isoc99);
3639 loc = c_parser_peek_token (parser)->location;
3640 cond = c_parser_paren_condition (parser);
3641 first_body = c_parser_if_body (parser, &first_if);
3642 if (c_parser_next_token_is_keyword (parser, RID_ELSE))
3644 c_parser_consume_token (parser);
3645 second_body = c_parser_if_body (parser, &second_if);
3648 second_body = NULL_TREE;
3649 c_finish_if_stmt (loc, cond, first_body, second_body, first_if);
3650 add_stmt (c_end_compound_stmt (block, flag_isoc99));
3653 /* Parse a switch statement (C90 6.6.4, C99 6.8.4).
3656 switch (expression) statement
3660 c_parser_switch_statement (c_parser *parser)
3662 tree block, expr, body, save_break;
3663 gcc_assert (c_parser_next_token_is_keyword (parser, RID_SWITCH));
3664 c_parser_consume_token (parser);
3665 block = c_begin_compound_stmt (flag_isoc99);
3666 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3668 expr = c_parser_expression (parser).value;
3669 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
3672 expr = error_mark_node;
3673 c_start_case (expr);
3674 save_break = c_break_label;
3675 c_break_label = NULL_TREE;
3676 body = c_parser_c99_block_statement (parser);
3677 c_finish_case (body);
3679 add_stmt (build (LABEL_EXPR, void_type_node, c_break_label));
3680 c_break_label = save_break;
3681 add_stmt (c_end_compound_stmt (block, flag_isoc99));
3684 /* Parse a while statement (C90 6.6.5, C99 6.8.5).
3687 while (expression) statement
3691 c_parser_while_statement (c_parser *parser)
3693 tree block, cond, body, save_break, save_cont;
3695 gcc_assert (c_parser_next_token_is_keyword (parser, RID_WHILE));
3696 c_parser_consume_token (parser);
3697 block = c_begin_compound_stmt (flag_isoc99);
3698 loc = c_parser_peek_token (parser)->location;
3699 cond = c_parser_paren_condition (parser);
3700 save_break = c_break_label;
3701 c_break_label = NULL_TREE;
3702 save_cont = c_cont_label;
3703 c_cont_label = NULL_TREE;
3704 body = c_parser_c99_block_statement (parser);
3705 c_finish_loop (loc, cond, NULL, body, c_break_label, c_cont_label, true);
3706 add_stmt (c_end_compound_stmt (block, flag_isoc99));
3707 c_break_label = save_break;
3708 c_cont_label = save_cont;
3711 /* Parse a do statement (C90 6.6.5, C99 6.8.5).
3714 do statement while ( expression ) ;
3718 c_parser_do_statement (c_parser *parser)
3720 tree block, cond, body, save_break, save_cont, new_break, new_cont;
3722 gcc_assert (c_parser_next_token_is_keyword (parser, RID_DO));
3723 c_parser_consume_token (parser);
3724 block = c_begin_compound_stmt (flag_isoc99);
3725 loc = c_parser_peek_token (parser)->location;
3726 save_break = c_break_label;
3727 c_break_label = NULL_TREE;
3728 save_cont = c_cont_label;
3729 c_cont_label = NULL_TREE;
3730 body = c_parser_c99_block_statement (parser);
3731 c_parser_require_keyword (parser, RID_WHILE, "expected %<while%>");
3732 new_break = c_break_label;
3733 c_break_label = save_break;
3734 new_cont = c_cont_label;
3735 c_cont_label = save_cont;
3736 cond = c_parser_paren_condition (parser);
3737 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
3738 c_parser_skip_to_end_of_block_or_statement (parser);
3739 c_finish_loop (loc, cond, NULL, body, new_break, new_cont, false);
3740 add_stmt (c_end_compound_stmt (block, flag_isoc99));
3743 /* Parse a for statement (C90 6.6.5, C99 6.8.5).
3746 for ( expression[opt] ; expression[opt] ; expression[opt] ) statement
3747 for ( nested-declaration expression[opt] ; expression[opt] ) statement
3749 The form with a declaration is new in C99.
3751 ??? In accordance with the old parser, the declaration may be a
3752 nested function, which is then rejected in check_for_loop_decls,
3753 but does it make any sense for this to be included in the grammar?
3754 Note in particular that the nested function does not include a
3755 trailing ';', whereas the "declaration" production includes one.
3756 Also, can we reject bad declarations earlier and cheaper than
3757 check_for_loop_decls? */
3760 c_parser_for_statement (c_parser *parser)
3762 tree block, cond, incr, save_break, save_cont, body;
3763 location_t loc = UNKNOWN_LOCATION;
3764 gcc_assert (c_parser_next_token_is_keyword (parser, RID_FOR));
3765 c_parser_consume_token (parser);
3766 block = c_begin_compound_stmt (flag_isoc99);
3767 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3769 /* Parse the initialization declaration or expression. */
3770 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
3772 c_parser_consume_token (parser);
3773 c_finish_expr_stmt (NULL_TREE);
3775 else if (c_parser_next_token_starts_declspecs (parser))
3777 c_parser_declaration_or_fndef (parser, true, true, true, true);
3778 check_for_loop_decls ();
3780 else if (c_parser_next_token_is_keyword (parser, RID_EXTENSION))
3782 /* __extension__ can start a declaration, but is also an
3783 unary operator that can start an expression. Consume all
3784 but the last of a possible series of __extension__ to
3786 while (c_parser_peek_2nd_token (parser)->type == CPP_KEYWORD
3787 && (c_parser_peek_2nd_token (parser)->keyword
3789 c_parser_consume_token (parser);
3790 if (c_token_starts_declspecs (c_parser_peek_2nd_token (parser)))
3793 ext = disable_extension_diagnostics ();
3794 c_parser_consume_token (parser);
3795 c_parser_declaration_or_fndef (parser, true, true, true, true);
3796 restore_extension_diagnostics (ext);
3797 check_for_loop_decls ();
3805 c_finish_expr_stmt (c_parser_expression (parser).value);
3806 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
3808 /* Parse the loop condition. */
3809 loc = c_parser_peek_token (parser)->location;
3810 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
3812 c_parser_consume_token (parser);
3817 tree ocond = c_parser_expression (parser).value;
3818 cond = lang_hooks.truthvalue_conversion (ocond);
3820 SET_EXPR_LOCATION (cond, loc);
3821 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
3823 /* Parse the increment expression. */
3824 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3825 incr = c_process_expr_stmt (NULL_TREE);
3827 incr = c_process_expr_stmt (c_parser_expression (parser).value);
3828 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
3832 cond = error_mark_node;
3833 incr = error_mark_node;
3835 save_break = c_break_label;
3836 c_break_label = NULL_TREE;
3837 save_cont = c_cont_label;
3838 c_cont_label = NULL_TREE;
3839 body = c_parser_c99_block_statement (parser);
3840 c_finish_loop (loc, cond, incr, body, c_break_label, c_cont_label, true);
3841 add_stmt (c_end_compound_stmt (block, flag_isoc99));
3842 c_break_label = save_break;
3843 c_cont_label = save_cont;
3846 /* Parse an asm statement, a GNU extension. This is a full-blown asm
3847 statement with inputs, outputs, clobbers, and volatile tag
3851 asm type-qualifier[opt] ( asm-argument ) ;
3855 asm-string-literal : asm-operands[opt]
3856 asm-string-literal : asm-operands[opt] : asm-operands[opt]
3857 asm-string-literal : asm-operands[opt] : asm-operands[opt] : asm-clobbers
3859 Qualifiers other than volatile are accepted in the syntax but
3863 c_parser_asm_statement (c_parser *parser)
3865 tree quals, str, outputs, inputs, clobbers, ret;
3867 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ASM));
3868 c_parser_consume_token (parser);
3869 if (c_parser_next_token_is_keyword (parser, RID_VOLATILE))
3871 quals = c_parser_peek_token (parser)->value;
3872 c_parser_consume_token (parser);
3874 else if (c_parser_next_token_is_keyword (parser, RID_CONST)
3875 || c_parser_next_token_is_keyword (parser, RID_RESTRICT))
3877 warning ("%E qualifier ignored on asm",
3878 c_parser_peek_token (parser)->value);
3880 c_parser_consume_token (parser);
3884 /* ??? Follow the C++ parser rather than using the
3885 c_lex_string_translate kludge. */
3886 c_lex_string_translate = 0;
3887 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3889 c_lex_string_translate = 1;
3892 str = c_parser_asm_string_literal (parser);
3893 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3896 outputs = NULL_TREE;
3898 clobbers = NULL_TREE;
3901 if (!c_parser_require (parser, CPP_COLON, "expected %<:%> or %<)%>"))
3903 c_lex_string_translate = 1;
3904 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
3908 /* Parse outputs. */
3909 if (c_parser_next_token_is (parser, CPP_COLON)
3910 || c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3911 outputs = NULL_TREE;
3913 outputs = c_parser_asm_operands (parser);
3914 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3917 clobbers = NULL_TREE;
3920 if (!c_parser_require (parser, CPP_COLON, "expected %<:%> or %<)%>"))
3922 c_lex_string_translate = 1;
3923 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
3927 if (c_parser_next_token_is (parser, CPP_COLON)
3928 || c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3931 inputs = c_parser_asm_operands (parser);
3932 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3934 clobbers = NULL_TREE;
3937 if (!c_parser_require (parser, CPP_COLON, "expected %<:%> or %<)%>"))
3939 c_lex_string_translate = 1;
3940 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
3943 /* Parse clobbers. */
3944 clobbers = c_parser_asm_clobbers (parser);
3946 c_lex_string_translate = 1;
3947 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
3949 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
3952 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
3953 c_parser_skip_to_end_of_block_or_statement (parser);
3954 ret = build_asm_stmt (quals, build_asm_expr (str, outputs, inputs,
3959 /* Parse asm operands, a GNU extension.
3963 asm-operands , asm-operand
3966 asm-string-literal ( expression )
3967 [ identifier ] asm-string-literal ( expression )
3971 c_parser_asm_operands (c_parser *parser)
3973 tree list = NULL_TREE;
3976 tree name, str, expr;
3977 if (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
3979 c_parser_consume_token (parser);
3980 if (c_parser_next_token_is (parser, CPP_NAME))
3982 tree id = c_parser_peek_token (parser)->value;
3983 c_parser_consume_token (parser);
3984 name = build_string (IDENTIFIER_LENGTH (id),
3985 IDENTIFIER_POINTER (id));
3989 c_parser_error (parser, "expected identifier");
3990 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
3993 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
3998 str = c_parser_asm_string_literal (parser);
3999 if (str == NULL_TREE)
4001 c_lex_string_translate = 1;
4002 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4004 c_lex_string_translate = 0;
4007 expr = c_parser_expression (parser).value;
4008 c_lex_string_translate = 0;
4009 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
4011 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
4014 list = chainon (list, build_tree_list (build_tree_list (name, str),
4016 if (c_parser_next_token_is (parser, CPP_COMMA))
4017 c_parser_consume_token (parser);
4024 /* Parse asm clobbers, a GNU extension.
4028 asm-clobbers , asm-string-literal
4032 c_parser_asm_clobbers (c_parser *parser)
4034 tree list = NULL_TREE;
4037 tree str = c_parser_asm_string_literal (parser);
4039 list = tree_cons (NULL_TREE, str, list);
4042 if (c_parser_next_token_is (parser, CPP_COMMA))
4043 c_parser_consume_token (parser);
4050 /* Parse an expression other than a compound expression; that is, an
4051 assignment expression (C90 6.3.16, C99 6.5.16). If AFTER is not
4052 NULL then it is an Objective-C message expression which is the
4053 primary-expression starting the expression as an initializer.
4055 assignment-expression:
4056 conditional-expression
4057 unary-expression assignment-operator assignment-expression
4059 assignment-operator: one of
4060 = *= /= %= += -= <<= >>= &= ^= |=
4062 In GNU C we accept any conditional expression on the LHS and
4063 diagnose the invalid lvalue rather than producing a syntax
4066 static struct c_expr
4067 c_parser_expr_no_commas (c_parser *parser, struct c_expr *after)
4069 struct c_expr lhs, rhs, ret;
4070 enum tree_code code;
4071 gcc_assert (!after || c_dialect_objc ());
4072 lhs = c_parser_conditional_expression (parser, after);
4073 switch (c_parser_peek_token (parser)->type)
4082 code = TRUNC_DIV_EXPR;
4085 code = TRUNC_MOD_EXPR;
4100 code = BIT_AND_EXPR;
4103 code = BIT_XOR_EXPR;
4106 code = BIT_IOR_EXPR;
4111 c_parser_consume_token (parser);
4112 rhs = c_parser_expr_no_commas (parser, NULL);
4113 ret.value = build_modify_expr (lhs.value, code, rhs.value);
4114 if (code == NOP_EXPR)
4115 ret.original_code = MODIFY_EXPR;
4118 TREE_NO_WARNING (ret.value) = 1;
4119 ret.original_code = ERROR_MARK;
4124 /* Parse a conditional expression (C90 6.3.15, C99 6.5.15). If AFTER
4125 is not NULL then it is an Objective-C message expression which is
4126 the primary-expression starting the expression as an initializer.
4128 conditional-expression:
4129 logical-OR-expression
4130 logical-OR-expression ? expression : conditional-expression
4134 conditional-expression:
4135 logical-OR-expression ? : conditional-expression
4138 static struct c_expr
4139 c_parser_conditional_expression (c_parser *parser, struct c_expr *after)
4141 struct c_expr cond, exp1, exp2, ret;
4142 gcc_assert (!after || c_dialect_objc ());
4143 cond = c_parser_binary_expression (parser, after);
4144 if (c_parser_next_token_is_not (parser, CPP_QUERY))
4146 c_parser_consume_token (parser);
4147 if (c_parser_next_token_is (parser, CPP_COLON))
4150 pedwarn ("ISO C forbids omitting the middle term of a ?: expression");
4151 /* Make sure first operand is calculated only once. */
4152 exp1.value = save_expr (default_conversion (cond.value));
4153 cond.value = lang_hooks.truthvalue_conversion (exp1.value);
4154 skip_evaluation += cond.value == truthvalue_true_node;
4159 = lang_hooks.truthvalue_conversion (default_conversion (cond.value));
4160 skip_evaluation += cond.value == truthvalue_false_node;
4161 exp1 = c_parser_expression (parser);
4162 skip_evaluation += ((cond.value == truthvalue_true_node)
4163 - (cond.value == truthvalue_false_node));
4165 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
4167 skip_evaluation -= cond.value == truthvalue_true_node;
4168 ret.value = error_mark_node;
4169 ret.original_code = ERROR_MARK;
4172 exp2 = c_parser_conditional_expression (parser, NULL);
4173 skip_evaluation -= cond.value == truthvalue_true_node;
4174 ret.value = build_conditional_expr (cond.value, exp1.value, exp2.value);
4175 ret.original_code = ERROR_MARK;
4179 /* Parse a binary expression; that is, a logical-OR-expression (C90
4180 6.3.5-6.3.14, C99 6.5.5-6.5.14). If AFTER is not NULL then it is
4181 an Objective-C message expression which is the primary-expression
4182 starting the expression as an initializer.
4184 multiplicative-expression:
4186 multiplicative-expression * cast-expression
4187 multiplicative-expression / cast-expression
4188 multiplicative-expression % cast-expression
4190 additive-expression:
4191 multiplicative-expression
4192 additive-expression + multiplicative-expression
4193 additive-expression - multiplicative-expression
4197 shift-expression << additive-expression
4198 shift-expression >> additive-expression
4200 relational-expression:
4202 relational-expression < shift-expression
4203 relational-expression > shift-expression
4204 relational-expression <= shift-expression
4205 relational-expression >= shift-expression
4207 equality-expression:
4208 relational-expression
4209 equality-expression == relational-expression
4210 equality-expression != relational-expression
4214 AND-expression & equality-expression
4216 exclusive-OR-expression:
4218 exclusive-OR-expression ^ AND-expression
4220 inclusive-OR-expression:
4221 exclusive-OR-expression
4222 inclusive-OR-expression | exclusive-OR-expression
4224 logical-AND-expression:
4225 inclusive-OR-expression
4226 logical-AND-expression && inclusive-OR-expression
4228 logical-OR-expression:
4229 logical-AND-expression
4230 logical-OR-expression || logical-AND-expression
4233 static struct c_expr
4234 c_parser_binary_expression (c_parser *parser, struct c_expr *after)
4236 /* A binary expression is parsed using operator-precedence parsing,
4237 with the operands being cast expressions. All the binary
4238 operators are left-associative. Thus a binary expression is of
4241 E0 op1 E1 op2 E2 ...
4243 which we represent on a stack. On the stack, the precedence
4244 levels are strictly increasing. When a new operator is
4245 encountered of higher precedence than that at the top of the
4246 stack, it is pushed; its LHS is the top expression, and its RHS
4247 is everything parsed until it is popped. When a new operator is
4248 encountered with precedence less than or equal to that at the top
4249 of the stack, triples E[i-1] op[i] E[i] are popped and replaced
4250 by the result of the operation until the operator at the top of
4251 the stack has lower precedence than the new operator or there is
4252 only one element on the stack; then the top expression is the LHS
4253 of the new operator. In the case of logical AND and OR
4254 expressions, we also need to adjust skip_evaluation as
4255 appropriate when the operators are pushed and popped. */
4257 /* The precedence levels, where 0 is a dummy lowest level used for
4258 the bottom of the stack. */
4274 /* The expression at this stack level. */
4276 /* The precedence of the operator on its left, PREC_NONE at the
4277 bottom of the stack. */
4279 /* The operation on its left. */
4285 switch (stack[sp].op) \
4287 case TRUTH_ANDIF_EXPR: \
4288 skip_evaluation -= stack[sp - 1].expr.value == truthvalue_false_node; \
4290 case TRUTH_ORIF_EXPR: \
4291 skip_evaluation -= stack[sp - 1].expr.value == truthvalue_true_node; \
4296 stack[sp - 1].expr = parser_build_binary_op (stack[sp].op, \
4297 stack[sp - 1].expr, \
4301 gcc_assert (!after || c_dialect_objc ());
4302 stack[0].expr = c_parser_cast_expression (parser, after);
4303 stack[0].prec = PREC_NONE;
4308 enum tree_code ocode;
4311 switch (c_parser_peek_token (parser)->type)
4319 ocode = TRUNC_DIV_EXPR;
4323 ocode = TRUNC_MOD_EXPR;
4335 ocode = LSHIFT_EXPR;
4339 ocode = RSHIFT_EXPR;
4353 case CPP_GREATER_EQ:
4366 oprec = PREC_BITAND;
4367 ocode = BIT_AND_EXPR;
4370 oprec = PREC_BITXOR;
4371 ocode = BIT_XOR_EXPR;
4375 ocode = BIT_IOR_EXPR;
4378 oprec = PREC_LOGAND;
4379 ocode = TRUTH_ANDIF_EXPR;
4383 ocode = TRUTH_ORIF_EXPR;
4386 /* Not a binary operator, so end of the binary
4390 c_parser_consume_token (parser);
4391 while (oprec <= stack[sp].prec)
4395 case TRUTH_ANDIF_EXPR:
4396 stack[sp].expr.value = lang_hooks.truthvalue_conversion
4397 (default_conversion (stack[sp].expr.value));
4398 skip_evaluation += stack[sp].expr.value == truthvalue_false_node;
4400 case TRUTH_ORIF_EXPR:
4401 stack[sp].expr.value = lang_hooks.truthvalue_conversion
4402 (default_conversion (stack[sp].expr.value));
4403 skip_evaluation += stack[sp].expr.value == truthvalue_true_node;
4409 stack[sp].expr = c_parser_cast_expression (parser, NULL);
4410 stack[sp].prec = oprec;
4411 stack[sp].op = ocode;
4416 return stack[0].expr;
4420 /* Parse a cast expression (C90 6.3.4, C99 6.5.4). If AFTER is not
4421 NULL then it is an Objective-C message expression which is the
4422 primary-expression starting the expression as an initializer.
4426 ( type-name ) unary-expression
4429 static struct c_expr
4430 c_parser_cast_expression (c_parser *parser, struct c_expr *after)
4432 gcc_assert (!after || c_dialect_objc ());
4434 return c_parser_postfix_expression_after_primary (parser, *after);
4435 /* If the expression begins with a parenthesized type name, it may
4436 be either a cast or a compound literal; we need to see whether
4437 the next character is '{' to tell the difference. If not, it is
4438 an unary expression. */
4439 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
4440 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
4442 struct c_type_name *type_name;
4445 c_parser_consume_token (parser);
4446 type_name = c_parser_type_name (parser);
4447 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
4448 if (type_name == NULL)
4450 ret.value = error_mark_node;
4451 ret.original_code = ERROR_MARK;
4454 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
4455 return c_parser_postfix_expression_after_paren_type (parser,
4457 expr = c_parser_cast_expression (parser, NULL).value;
4458 ret.value = c_cast_expr (type_name, expr);
4459 ret.original_code = ERROR_MARK;
4463 return c_parser_unary_expression (parser);
4466 /* Parse an unary expression (C90 6.3.3, C99 6.5.3).
4472 unary-operator cast-expression
4473 sizeof unary-expression
4474 sizeof ( type-name )
4476 unary-operator: one of
4482 __alignof__ unary-expression
4483 __alignof__ ( type-name )
4486 unary-operator: one of
4487 __extension__ __real__ __imag__
4489 In addition, the GNU syntax treats ++ and -- as unary operators, so
4490 they may be applied to cast expressions with errors for non-lvalues
4493 static struct c_expr
4494 c_parser_unary_expression (c_parser *parser)
4498 ret.original_code = ERROR_MARK;
4499 switch (c_parser_peek_token (parser)->type)
4502 c_parser_consume_token (parser);
4504 = build_unary_op (PREINCREMENT_EXPR,
4505 c_parser_cast_expression (parser, NULL).value, 0);
4506 overflow_warning (ret.value);
4508 case CPP_MINUS_MINUS:
4509 c_parser_consume_token (parser);
4511 = build_unary_op (PREDECREMENT_EXPR,
4512 c_parser_cast_expression (parser, NULL).value, 0);
4513 overflow_warning (ret.value);
4516 c_parser_consume_token (parser);
4518 = build_unary_op (ADDR_EXPR,
4519 c_parser_cast_expression (parser, NULL).value, 0);
4520 overflow_warning (ret.value);
4523 c_parser_consume_token (parser);
4525 = build_indirect_ref (c_parser_cast_expression (parser, NULL).value,
4529 c_parser_consume_token (parser);
4530 if (!c_dialect_objc () && warn_traditional && !in_system_header)
4531 warning ("traditional C rejects the unary plus operator");
4533 = build_unary_op (CONVERT_EXPR,
4534 c_parser_cast_expression (parser, NULL).value, 0);
4535 overflow_warning (ret.value);
4538 c_parser_consume_token (parser);
4540 = build_unary_op (NEGATE_EXPR,
4541 c_parser_cast_expression (parser, NULL).value, 0);
4542 overflow_warning (ret.value);
4545 c_parser_consume_token (parser);
4547 = build_unary_op (BIT_NOT_EXPR,
4548 c_parser_cast_expression (parser, NULL).value, 0);
4549 overflow_warning (ret.value);
4552 c_parser_consume_token (parser);
4554 = build_unary_op (TRUTH_NOT_EXPR,
4555 c_parser_cast_expression (parser, NULL).value, 0);
4556 overflow_warning (ret.value);
4559 /* Refer to the address of a label as a pointer. */
4560 c_parser_consume_token (parser);
4561 if (c_parser_next_token_is (parser, CPP_NAME))
4563 ret.value = finish_label_address_expr
4564 (c_parser_peek_token (parser)->value);
4565 c_parser_consume_token (parser);
4570 c_parser_error (parser, "expected identifier");
4571 ret.value = error_mark_node;
4575 switch (c_parser_peek_token (parser)->keyword)
4578 return c_parser_sizeof_expression (parser);
4580 return c_parser_alignof_expression (parser);
4582 c_parser_consume_token (parser);
4583 ext = disable_extension_diagnostics ();
4584 ret = c_parser_cast_expression (parser, NULL);
4585 restore_extension_diagnostics (ext);
4588 c_parser_consume_token (parser);
4590 = build_unary_op (REALPART_EXPR,
4591 c_parser_cast_expression (parser, NULL).value,
4595 c_parser_consume_token (parser);
4597 = build_unary_op (IMAGPART_EXPR,
4598 c_parser_cast_expression (parser, NULL).value,
4602 return c_parser_postfix_expression (parser);
4605 return c_parser_postfix_expression (parser);
4609 /* Parse a sizeof expression. */
4611 static struct c_expr
4612 c_parser_sizeof_expression (c_parser *parser)
4615 gcc_assert (c_parser_next_token_is_keyword (parser, RID_SIZEOF));
4616 c_parser_consume_token (parser);
4619 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
4620 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
4622 /* Either sizeof ( type-name ) or sizeof unary-expression
4623 starting with a compound literal. */
4624 struct c_type_name *type_name;
4625 c_parser_consume_token (parser);
4626 type_name = c_parser_type_name (parser);
4627 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
4628 if (type_name == NULL)
4633 ret.value = error_mark_node;
4634 ret.original_code = ERROR_MARK;
4637 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
4639 expr = c_parser_postfix_expression_after_paren_type (parser,
4643 /* sizeof ( type-name ). */
4646 return c_expr_sizeof_type (type_name);
4650 expr = c_parser_unary_expression (parser);
4654 if (TREE_CODE (expr.value) == COMPONENT_REF
4655 && DECL_C_BIT_FIELD (TREE_OPERAND (expr.value, 1)))
4656 error ("%<sizeof%> applied to a bit-field");
4657 return c_expr_sizeof_expr (expr);
4661 /* Parse an alignof expression. */
4663 static struct c_expr
4664 c_parser_alignof_expression (c_parser *parser)
4667 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ALIGNOF));
4668 c_parser_consume_token (parser);
4671 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
4672 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
4674 /* Either __alignof__ ( type-name ) or __alignof__
4675 unary-expression starting with a compound literal. */
4676 struct c_type_name *type_name;
4678 c_parser_consume_token (parser);
4679 type_name = c_parser_type_name (parser);
4680 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
4681 if (type_name == NULL)
4686 ret.value = error_mark_node;
4687 ret.original_code = ERROR_MARK;
4690 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
4692 expr = c_parser_postfix_expression_after_paren_type (parser,
4696 /* alignof ( type-name ). */
4699 ret.value = c_alignof (groktypename (type_name));
4700 ret.original_code = ERROR_MARK;
4706 expr = c_parser_unary_expression (parser);
4710 ret.value = c_alignof_expr (expr.value);
4711 ret.original_code = ERROR_MARK;
4716 /* Parse a postfix expression (C90 6.3.1-6.3.2, C99 6.5.1-6.5.2).
4720 postfix-expression [ expression ]
4721 postfix-expression ( argument-expression-list[opt] )
4722 postfix-expression . identifier
4723 postfix-expression -> identifier
4724 postfix-expression ++
4725 postfix-expression --
4726 ( type-name ) { initializer-list }
4727 ( type-name ) { initializer-list , }
4729 argument-expression-list:
4731 argument-expression-list , argument-expression
4743 (treated as a keyword in GNU C)
4746 ( compound-statement )
4747 __builtin_va_arg ( assignment-expression , type-name )
4748 __builtin_offsetof ( type-name , offsetof-member-designator )
4749 __builtin_choose_expr ( assignment-expression ,
4750 assignment-expression ,
4751 assignment-expression )
4752 __builtin_types_compatible_p ( type-name , type-name )
4754 offsetof-member-designator:
4756 offsetof-member-designator . identifier
4757 offsetof-member-designator [ expression ]
4762 [ objc-receiver objc-message-args ]
4763 @selector ( objc-selector-arg )
4764 @protocol ( identifier )
4765 @encode ( type-name )
4769 static struct c_expr
4770 c_parser_postfix_expression (c_parser *parser)
4772 struct c_expr expr, e1, e2, e3;
4773 struct c_type_name *t1, *t2;
4774 switch (c_parser_peek_token (parser)->type)
4779 expr.value = c_parser_peek_token (parser)->value;
4780 expr.original_code = ERROR_MARK;
4781 c_parser_consume_token (parser);
4785 expr.value = c_parser_peek_token (parser)->value;
4786 expr.original_code = STRING_CST;
4787 c_parser_consume_token (parser);
4789 case CPP_OBJC_STRING:
4790 gcc_assert (c_dialect_objc ());
4792 = objc_build_string_object (c_parser_peek_token (parser)->value);
4793 expr.original_code = ERROR_MARK;
4794 c_parser_consume_token (parser);
4797 if (c_parser_peek_token (parser)->id_kind != C_ID_ID)
4799 c_parser_error (parser, "expected expression");
4800 expr.value = error_mark_node;
4801 expr.original_code = ERROR_MARK;
4805 tree id = c_parser_peek_token (parser)->value;
4806 location_t loc = c_parser_peek_token (parser)->location;
4807 c_parser_consume_token (parser);
4808 expr.value = build_external_ref (id,
4809 (c_parser_peek_token (parser)->type
4810 == CPP_OPEN_PAREN), loc);
4811 expr.original_code = ERROR_MARK;
4814 case CPP_OPEN_PAREN:
4815 /* A parenthesized expression, statement expression or compound
4817 if (c_parser_peek_2nd_token (parser)->type == CPP_OPEN_BRACE)
4819 /* A statement expression. */
4821 c_parser_consume_token (parser);
4822 c_parser_consume_token (parser);
4823 if (cur_stmt_list == NULL)
4825 error ("braced-group within expression allowed "
4826 "only inside a function");
4827 parser->error = true;
4828 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
4829 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
4830 expr.value = error_mark_node;
4831 expr.original_code = ERROR_MARK;
4834 stmt = c_begin_stmt_expr ();
4835 c_parser_compound_statement_nostart (parser);
4836 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
4839 pedwarn ("ISO C forbids braced-groups within expressions");
4840 expr.value = c_finish_stmt_expr (stmt);
4841 expr.original_code = ERROR_MARK;
4843 else if (c_token_starts_typename (c_parser_peek_2nd_token (parser)))
4845 /* A compound literal. ??? Can we actually get here rather
4846 than going directly to
4847 c_parser_postfix_expression_after_paren_type from
4849 struct c_type_name *type_name;
4850 c_parser_consume_token (parser);
4851 type_name = c_parser_type_name (parser);
4852 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
4854 if (type_name == NULL)
4856 expr.value = error_mark_node;
4857 expr.original_code = ERROR_MARK;
4860 expr = c_parser_postfix_expression_after_paren_type (parser,
4865 /* A parenthesized expression. */
4866 c_parser_consume_token (parser);
4867 expr = c_parser_expression (parser);
4868 if (TREE_CODE (expr.value) == MODIFY_EXPR)
4869 TREE_NO_WARNING (expr.value) = 1;
4870 expr.original_code = ERROR_MARK;
4871 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
4876 switch (c_parser_peek_token (parser)->keyword)
4878 case RID_FUNCTION_NAME:
4879 case RID_PRETTY_FUNCTION_NAME:
4880 case RID_C99_FUNCTION_NAME:
4881 expr.value = fname_decl (c_parser_peek_token (parser)->keyword,
4882 c_parser_peek_token (parser)->value);
4883 expr.original_code = ERROR_MARK;
4884 c_parser_consume_token (parser);
4887 c_parser_consume_token (parser);
4888 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4890 expr.value = error_mark_node;
4891 expr.original_code = ERROR_MARK;
4894 e1 = c_parser_expr_no_commas (parser, NULL);
4895 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
4897 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
4898 expr.value = error_mark_node;
4899 expr.original_code = ERROR_MARK;
4902 t1 = c_parser_type_name (parser);
4903 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
4907 expr.value = error_mark_node;
4908 expr.original_code = ERROR_MARK;
4912 expr.value = build_va_arg (e1.value, groktypename (t1));
4913 expr.original_code = ERROR_MARK;
4917 c_parser_consume_token (parser);
4918 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4920 expr.value = error_mark_node;
4921 expr.original_code = ERROR_MARK;
4924 t1 = c_parser_type_name (parser);
4927 expr.value = error_mark_node;
4928 expr.original_code = ERROR_MARK;
4931 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
4933 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
4934 expr.value = error_mark_node;
4935 expr.original_code = ERROR_MARK;
4939 tree type = groktypename (t1);
4941 if (type == error_mark_node)
4942 offsetof_ref = error_mark_node;
4944 offsetof_ref = build1 (INDIRECT_REF, type, NULL);
4945 /* Parse the second argument to __builtin_offsetof. We
4946 must have one identifier, and beyond that we want to
4947 accept sub structure and sub array references. */
4948 if (c_parser_next_token_is (parser, CPP_NAME))
4950 offsetof_ref = build_component_ref
4951 (offsetof_ref, c_parser_peek_token (parser)->value);
4952 c_parser_consume_token (parser);
4953 while (c_parser_next_token_is (parser, CPP_DOT)
4954 || c_parser_next_token_is (parser,
4957 if (c_parser_next_token_is (parser, CPP_DOT))
4959 c_parser_consume_token (parser);
4960 if (c_parser_next_token_is_not (parser,
4963 c_parser_error (parser, "expected identifier");
4966 offsetof_ref = build_component_ref
4968 c_parser_peek_token (parser)->value);
4969 c_parser_consume_token (parser);
4974 c_parser_consume_token (parser);
4975 idx = c_parser_expression (parser).value;
4976 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
4978 offsetof_ref = build_array_ref (offsetof_ref, idx);
4983 c_parser_error (parser, "expected identifier");
4984 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
4986 expr.value = fold_offsetof (offsetof_ref);
4987 expr.original_code = ERROR_MARK;
4990 case RID_CHOOSE_EXPR:
4991 c_parser_consume_token (parser);
4992 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4994 expr.value = error_mark_node;
4995 expr.original_code = ERROR_MARK;
4998 e1 = c_parser_expr_no_commas (parser, NULL);
4999 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
5001 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5002 expr.value = error_mark_node;
5003 expr.original_code = ERROR_MARK;
5006 e2 = c_parser_expr_no_commas (parser, NULL);
5007 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
5009 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5010 expr.value = error_mark_node;
5011 expr.original_code = ERROR_MARK;
5014 e3 = c_parser_expr_no_commas (parser, NULL);
5015 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5020 c = fold (e1.value);
5022 if (TREE_CODE (c) != INTEGER_CST)
5023 error ("first argument to %<__builtin_choose_expr%> not"
5025 expr = integer_zerop (c) ? e3 : e2;
5028 case RID_TYPES_COMPATIBLE_P:
5029 c_parser_consume_token (parser);
5030 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5032 expr.value = error_mark_node;
5033 expr.original_code = ERROR_MARK;
5036 t1 = c_parser_type_name (parser);
5039 expr.value = error_mark_node;
5040 expr.original_code = ERROR_MARK;
5043 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
5045 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5046 expr.value = error_mark_node;
5047 expr.original_code = ERROR_MARK;
5050 t2 = c_parser_type_name (parser);
5053 expr.value = error_mark_node;
5054 expr.original_code = ERROR_MARK;
5057 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5062 e1 = TYPE_MAIN_VARIANT (groktypename (t1));
5063 e2 = TYPE_MAIN_VARIANT (groktypename (t2));
5065 expr.value = comptypes (e1, e2)
5066 ? build_int_cst (NULL_TREE, 1)
5067 : build_int_cst (NULL_TREE, 0);
5068 expr.original_code = ERROR_MARK;
5071 case RID_AT_SELECTOR:
5072 gcc_assert (c_dialect_objc ());
5073 c_parser_consume_token (parser);
5074 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5076 expr.value = error_mark_node;
5077 expr.original_code = ERROR_MARK;
5081 tree sel = c_parser_objc_selector_arg (parser);
5082 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5084 expr.value = objc_build_selector_expr (sel);
5085 expr.original_code = ERROR_MARK;
5088 case RID_AT_PROTOCOL:
5089 gcc_assert (c_dialect_objc ());
5090 c_parser_consume_token (parser);
5091 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5093 expr.value = error_mark_node;
5094 expr.original_code = ERROR_MARK;
5097 if (c_parser_next_token_is_not (parser, CPP_NAME))
5099 c_parser_error (parser, "expected identifier");
5100 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5101 expr.value = error_mark_node;
5102 expr.original_code = ERROR_MARK;
5106 tree id = c_parser_peek_token (parser)->value;
5107 c_parser_consume_token (parser);
5108 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5110 expr.value = objc_build_protocol_expr (id);
5111 expr.original_code = ERROR_MARK;
5115 /* Extension to support C-structures in the archiver. */
5116 gcc_assert (c_dialect_objc ());
5117 c_parser_consume_token (parser);
5118 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5120 expr.value = error_mark_node;
5121 expr.original_code = ERROR_MARK;
5124 t1 = c_parser_type_name (parser);
5127 expr.value = error_mark_node;
5128 expr.original_code = ERROR_MARK;
5129 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5132 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5135 tree type = groktypename (t1);
5136 expr.value = objc_build_encode_expr (type);
5137 expr.original_code = ERROR_MARK;
5141 c_parser_error (parser, "expected expression");
5142 expr.value = error_mark_node;
5143 expr.original_code = ERROR_MARK;
5147 case CPP_OPEN_SQUARE:
5148 if (c_dialect_objc ())
5150 tree receiver, args;
5151 c_parser_consume_token (parser);
5152 receiver = c_parser_objc_receiver (parser);
5153 args = c_parser_objc_message_args (parser);
5154 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
5156 expr.value = objc_build_message_expr (build_tree_list (receiver,
5158 expr.original_code = ERROR_MARK;
5161 /* Else fall through to report error. */
5163 c_parser_error (parser, "expected expression");
5164 expr.value = error_mark_node;
5165 expr.original_code = ERROR_MARK;
5168 return c_parser_postfix_expression_after_primary (parser, expr);
5171 /* Parse a postfix expression after a parenthesized type name: the
5172 brace-enclosed initializer of a compound literal, possibly followed
5173 by some postfix operators. This is separate because it is not
5174 possible to tell until after the type name whether a cast
5175 expression has a cast or a compound literal, or whether the operand
5176 of sizeof is a parenthesized type name or starts with a compound
5179 static struct c_expr
5180 c_parser_postfix_expression_after_paren_type (c_parser *parser,
5181 struct c_type_name *type_name)
5186 start_init (NULL_TREE, NULL, 0);
5187 type = groktypename (type_name);
5188 if (C_TYPE_VARIABLE_SIZE (type))
5190 error ("compound literal has variable size");
5191 type = error_mark_node;
5193 init = c_parser_braced_init (parser, type, false);
5195 maybe_warn_string_init (type, init);
5197 if (pedantic && !flag_isoc99)
5198 pedwarn ("ISO C90 forbids compound literals");
5199 expr.value = build_compound_literal (type, init.value);
5200 expr.original_code = ERROR_MARK;
5201 return c_parser_postfix_expression_after_primary (parser, expr);
5204 /* Parse a postfix expression after the initial primary or compound
5205 literal; that is, parse a series of postfix operators. */
5207 static struct c_expr
5208 c_parser_postfix_expression_after_primary (c_parser *parser,
5211 tree ident, idx, exprlist;
5214 switch (c_parser_peek_token (parser)->type)
5216 case CPP_OPEN_SQUARE:
5217 /* Array reference. */
5218 c_parser_consume_token (parser);
5219 idx = c_parser_expression (parser).value;
5220 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
5222 expr.value = build_array_ref (expr.value, idx);
5223 expr.original_code = ERROR_MARK;
5225 case CPP_OPEN_PAREN:
5226 /* Function call. */
5227 c_parser_consume_token (parser);
5228 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
5229 exprlist = NULL_TREE;
5231 exprlist = c_parser_expr_list (parser);
5232 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5234 expr.value = build_function_call (expr.value, exprlist);
5235 expr.original_code = ERROR_MARK;
5238 /* Structure element reference. */
5239 c_parser_consume_token (parser);
5240 if (c_parser_next_token_is (parser, CPP_NAME))
5241 ident = c_parser_peek_token (parser)->value;
5244 c_parser_error (parser, "expected identifier");
5245 expr.value = error_mark_node;
5246 expr.original_code = ERROR_MARK;
5249 c_parser_consume_token (parser);
5250 expr.value = build_component_ref (expr.value, ident);
5251 expr.original_code = ERROR_MARK;
5254 /* Structure element reference. */
5255 c_parser_consume_token (parser);
5256 if (c_parser_next_token_is (parser, CPP_NAME))
5257 ident = c_parser_peek_token (parser)->value;
5260 c_parser_error (parser, "expected identifier");
5261 expr.value = error_mark_node;
5262 expr.original_code = ERROR_MARK;
5265 c_parser_consume_token (parser);
5266 expr.value = build_component_ref (build_indirect_ref (expr.value,
5268 expr.original_code = ERROR_MARK;
5271 /* Postincrement. */
5272 c_parser_consume_token (parser);
5273 expr.value = build_unary_op (POSTINCREMENT_EXPR, expr.value, 0);
5274 expr.original_code = ERROR_MARK;
5276 case CPP_MINUS_MINUS:
5277 /* Postdecrement. */
5278 c_parser_consume_token (parser);
5279 expr.value = build_unary_op (POSTDECREMENT_EXPR, expr.value, 0);
5280 expr.original_code = ERROR_MARK;
5288 /* Parse an expression (C90 6.3.17, C99 6.5.17).
5291 assignment-expression
5292 expression , assignment-expression
5295 static struct c_expr
5296 c_parser_expression (c_parser *parser)
5299 expr = c_parser_expr_no_commas (parser, NULL);
5300 while (c_parser_next_token_is (parser, CPP_COMMA))
5303 c_parser_consume_token (parser);
5304 next = c_parser_expr_no_commas (parser, NULL);
5305 expr.value = build_compound_expr (expr.value, next.value);
5306 expr.original_code = COMPOUND_EXPR;
5311 /* Parse a non-empty list of expressions.
5314 assignment-expression
5315 nonempty-expr-list , assignment-expression
5319 c_parser_expr_list (c_parser *parser)
5323 expr = c_parser_expr_no_commas (parser, NULL);
5324 ret = build_tree_list (NULL_TREE, expr.value);
5325 while (c_parser_next_token_is (parser, CPP_COMMA))
5327 c_parser_consume_token (parser);
5328 expr = c_parser_expr_no_commas (parser, NULL);
5329 ret = chainon (ret, build_tree_list (NULL_TREE, expr.value));
5335 /* Parse Objective-C-specific constructs. */
5337 /* Parse an objc-class-definition.
5339 objc-class-definition:
5340 @interface identifier objc-superclass[opt] objc-protocol-refs[opt]
5341 objc-class-instance-variables[opt] objc-methodprotolist @end
5342 @implementation identifier objc-superclass[opt]
5343 objc-class-instance-variables[opt]
5344 @interface identifier ( identifier ) objc-protocol-refs[opt]
5345 objc-methodprotolist @end
5346 @implementation identifier ( identifier )
5351 "@interface identifier (" must start "@interface identifier (
5352 identifier ) ...": objc-methodprotolist in the first production may
5353 not start with a parenthesised identifier as a declarator of a data
5354 definition with no declaration specifiers if the objc-superclass,
5355 objc-protocol-refs and objc-class-instance-variables are omitted. */
5358 c_parser_objc_class_definition (c_parser *parser)
5363 if (c_parser_next_token_is_keyword (parser, RID_AT_INTERFACE))
5365 else if (c_parser_next_token_is_keyword (parser, RID_AT_IMPLEMENTATION))
5369 c_parser_consume_token (parser);
5370 if (c_parser_next_token_is_not (parser, CPP_NAME))
5372 c_parser_error (parser, "expected identifier");
5375 id1 = c_parser_peek_token (parser)->value;
5376 c_parser_consume_token (parser);
5377 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
5380 tree proto = NULL_TREE;
5381 c_parser_consume_token (parser);
5382 if (c_parser_next_token_is_not (parser, CPP_NAME))
5384 c_parser_error (parser, "expected identifier");
5385 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5388 id2 = c_parser_peek_token (parser)->value;
5389 c_parser_consume_token (parser);
5390 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
5393 objc_start_category_implementation (id1, id2);
5396 if (c_parser_next_token_is (parser, CPP_LESS))
5397 proto = c_parser_objc_protocol_refs (parser);
5398 objc_start_category_interface (id1, id2, proto);
5399 c_parser_objc_methodprotolist (parser);
5400 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
5401 objc_finish_interface ();
5404 if (c_parser_next_token_is (parser, CPP_COLON))
5406 c_parser_consume_token (parser);
5407 if (c_parser_next_token_is_not (parser, CPP_NAME))
5409 c_parser_error (parser, "expected identifier");
5412 superclass = c_parser_peek_token (parser)->value;
5413 c_parser_consume_token (parser);
5416 superclass = NULL_TREE;
5419 tree proto = NULL_TREE;
5420 if (c_parser_next_token_is (parser, CPP_LESS))
5421 proto = c_parser_objc_protocol_refs (parser);
5422 objc_start_class_interface (id1, superclass, proto);
5425 objc_start_class_implementation (id1, superclass);
5426 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
5427 c_parser_objc_class_instance_variables (parser);
5430 objc_continue_interface ();
5431 c_parser_objc_methodprotolist (parser);
5432 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
5433 objc_finish_interface ();
5437 objc_continue_implementation ();
5442 /* Parse objc-class-instance-variables.
5444 objc-class-instance-variables:
5445 { objc-instance-variable-decl-list[opt] }
5447 objc-instance-variable-decl-list:
5448 objc-visibility-spec
5449 objc-instance-variable-decl ;
5451 objc-instance-variable-decl-list objc-visibility-spec
5452 objc-instance-variable-decl-list objc-instance-variable-decl ;
5453 objc-instance-variable-decl-list ;
5455 objc-visibility-spec:
5460 objc-instance-variable-decl:
5465 c_parser_objc_class_instance_variables (c_parser *parser)
5467 gcc_assert (c_parser_next_token_is (parser, CPP_OPEN_BRACE));
5468 c_parser_consume_token (parser);
5469 while (c_parser_next_token_is_not (parser, CPP_EOF))
5472 /* Parse any stray semicolon. */
5473 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5476 pedwarn ("extra semicolon in struct or union specified");
5477 c_parser_consume_token (parser);
5480 /* Stop if at the end of the instance variables. */
5481 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
5483 c_parser_consume_token (parser);
5486 /* Parse any objc-visibility-spec. */
5487 if (c_parser_next_token_is_keyword (parser, RID_AT_PRIVATE))
5489 c_parser_consume_token (parser);
5490 objc_set_visibility (2);
5493 else if (c_parser_next_token_is_keyword (parser, RID_AT_PROTECTED))
5495 c_parser_consume_token (parser);
5496 objc_set_visibility (0);
5499 else if (c_parser_next_token_is_keyword (parser, RID_AT_PUBLIC))
5501 c_parser_consume_token (parser);
5502 objc_set_visibility (1);
5505 /* Parse some comma-separated declarations. */
5506 decls = c_parser_struct_declaration (parser);
5508 /* Comma-separated instance variables are chained together in
5509 reverse order; add them one by one. */
5510 tree ivar = nreverse (decls);
5511 for (; ivar; ivar = TREE_CHAIN (ivar))
5512 objc_add_instance_variable (copy_node (ivar));
5514 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
5518 /* Parse an objc-class-declaration.
5520 objc-class-declaration:
5521 @class identifier-list ;
5525 c_parser_objc_class_declaration (c_parser *parser)
5527 tree list = NULL_TREE;
5528 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_CLASS));
5529 c_parser_consume_token (parser);
5530 /* Any identifiers, including those declared as type names, are OK
5535 if (c_parser_next_token_is_not (parser, CPP_NAME))
5537 c_parser_error (parser, "expected identifier");
5540 id = c_parser_peek_token (parser)->value;
5541 list = chainon (list, build_tree_list (NULL_TREE, id));
5542 c_parser_consume_token (parser);
5543 if (c_parser_next_token_is (parser, CPP_COMMA))
5544 c_parser_consume_token (parser);
5548 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
5549 objc_declare_class (list);
5552 /* Parse an objc-alias-declaration.
5554 objc-alias-declaration:
5555 @compatibility_alias identifier identifier ;
5559 c_parser_objc_alias_declaration (c_parser *parser)
5562 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_ALIAS));
5563 c_parser_consume_token (parser);
5564 if (c_parser_next_token_is_not (parser, CPP_NAME))
5566 c_parser_error (parser, "expected identifier");
5567 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
5570 id1 = c_parser_peek_token (parser)->value;
5571 c_parser_consume_token (parser);
5572 if (c_parser_next_token_is_not (parser, CPP_NAME))
5574 c_parser_error (parser, "expected identifier");
5575 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
5578 id2 = c_parser_peek_token (parser)->value;
5579 c_parser_consume_token (parser);
5580 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
5581 objc_declare_alias (id1, id2);
5584 /* Parse an objc-protocol-definition.
5586 objc-protocol-definition:
5587 @protocol identifier objc-protocol-refs[opt] objc-methodprotolist @end
5588 @protocol identifier-list ;
5590 "@protocol identifier ;" should be resolved as "@protocol
5591 identifier-list ;": objc-methodprotolist may not start with a
5592 semicolon in the first alternative if objc-protocol-refs are
5596 c_parser_objc_protocol_definition (c_parser *parser)
5598 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_PROTOCOL));
5599 c_parser_consume_token (parser);
5600 if (c_parser_next_token_is_not (parser, CPP_NAME))
5602 c_parser_error (parser, "expected identifier");
5605 if (c_parser_peek_2nd_token (parser)->type == CPP_COMMA
5606 || c_parser_peek_2nd_token (parser)->type == CPP_SEMICOLON)
5608 tree list = NULL_TREE;
5609 /* Any identifiers, including those declared as type names, are
5614 if (c_parser_next_token_is_not (parser, CPP_NAME))
5616 c_parser_error (parser, "expected identifier");
5619 id = c_parser_peek_token (parser)->value;
5620 list = chainon (list, build_tree_list (NULL_TREE, id));
5621 c_parser_consume_token (parser);
5622 if (c_parser_next_token_is (parser, CPP_COMMA))
5623 c_parser_consume_token (parser);
5627 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
5628 objc_declare_protocols (list);
5632 tree id = c_parser_peek_token (parser)->value;
5633 tree proto = NULL_TREE;
5634 c_parser_consume_token (parser);
5635 if (c_parser_next_token_is (parser, CPP_LESS))
5636 proto = c_parser_objc_protocol_refs (parser);
5637 objc_pq_context = 1;
5638 objc_start_protocol (id, proto);
5639 c_parser_objc_methodprotolist (parser);
5640 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
5641 objc_pq_context = 0;
5642 objc_finish_interface ();
5646 /* Parse an objc-method-type.
5653 static enum tree_code
5654 c_parser_objc_method_type (c_parser *parser)
5656 switch (c_parser_peek_token (parser)->type)
5659 c_parser_consume_token (parser);
5662 c_parser_consume_token (parser);
5669 /* Parse an objc-method-definition.
5671 objc-method-definition:
5672 objc-method-type objc-method-decl ;[opt] compound-statement
5676 c_parser_objc_method_definition (c_parser *parser)
5678 enum tree_code type = c_parser_objc_method_type (parser);
5680 objc_set_method_type (type);
5681 objc_pq_context = 1;
5682 decl = c_parser_objc_method_decl (parser);
5683 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5685 c_parser_consume_token (parser);
5687 pedwarn ("extra semicolon in method definition specified");
5689 objc_pq_context = 0;
5690 objc_start_method_definition (decl);
5691 add_stmt (c_parser_compound_statement (parser));
5692 objc_finish_method_definition (current_function_decl);
5695 /* Parse an objc-methodprotolist.
5697 objc-methodprotolist:
5699 objc-methodprotolist objc-methodproto
5700 objc-methodprotolist declaration
5701 objc-methodprotolist ;
5703 The declaration is a data definition, which may be missing
5704 declaration specifiers under the same rules and diagnostics as
5705 other data definitions outside functions, and the stray semicolon
5706 is diagnosed the same way as a stray semicolon outside a
5710 c_parser_objc_methodprotolist (c_parser *parser)
5714 /* The list is terminated by @end. */
5715 switch (c_parser_peek_token (parser)->type)
5719 pedwarn ("ISO C does not allow extra %<;%> outside of a function");
5720 c_parser_consume_token (parser);
5724 c_parser_objc_methodproto (parser);
5729 if (c_parser_next_token_is_keyword (parser, RID_AT_END))
5731 c_parser_declaration_or_fndef (parser, false, true, false, true);
5737 /* Parse an objc-methodproto.
5740 objc-method-type objc-method-decl ;
5744 c_parser_objc_methodproto (c_parser *parser)
5746 enum tree_code type = c_parser_objc_method_type (parser);
5748 objc_set_method_type (type);
5749 /* Remember protocol qualifiers in prototypes. */
5750 objc_pq_context = 1;
5751 decl = c_parser_objc_method_decl (parser);
5752 /* Forget protocol qualifiers here. */
5753 objc_pq_context = 0;
5754 objc_add_method_declaration (decl);
5755 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
5758 /* Parse an objc-method-decl.
5761 ( objc-type-name ) objc-selector
5763 ( objc-type-name ) objc-keyword-selector objc-optparmlist
5764 objc-keyword-selector objc-optparmlist
5766 objc-keyword-selector:
5768 objc-keyword-selector objc-keyword-decl
5771 objc-selector : ( objc-type-name ) identifier
5772 objc-selector : identifier
5773 : ( objc-type-name ) identifier
5777 objc-optparms objc-optellipsis
5781 objc-opt-parms , parameter-declaration
5789 c_parser_objc_method_decl (c_parser *parser)
5791 tree type = NULL_TREE;
5793 tree parms = NULL_TREE;
5794 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
5796 c_parser_consume_token (parser);
5797 type = c_parser_objc_type_name (parser);
5798 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
5800 sel = c_parser_objc_selector (parser);
5801 /* If there is no selector, or a colon follows, we have an
5802 objc-keyword-selector. If there is a selector, and a colon does
5803 not follow, that selector ends the objc-method-decl. */
5804 if (!sel || c_parser_next_token_is (parser, CPP_COLON))
5807 tree list = NULL_TREE;
5811 tree atype = NULL_TREE, id, keyworddecl;
5812 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
5814 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
5816 c_parser_consume_token (parser);
5817 atype = c_parser_objc_type_name (parser);
5818 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5821 if (c_parser_next_token_is_not (parser, CPP_NAME))
5823 c_parser_error (parser, "expected identifier");
5824 return error_mark_node;
5826 id = c_parser_peek_token (parser)->value;
5827 c_parser_consume_token (parser);
5828 keyworddecl = objc_build_keyword_decl (tsel, atype, id);
5829 list = chainon (list, keyworddecl);
5830 tsel = c_parser_objc_selector (parser);
5831 if (!tsel && c_parser_next_token_is_not (parser, CPP_COLON))
5834 /* Parse the optional parameter list. Optional Objective-C
5835 method parameters follow the C syntax, and may include '...'
5836 to denote a variable number of arguments. */
5837 parms = make_node (TREE_LIST);
5839 while (c_parser_next_token_is (parser, CPP_COMMA))
5841 struct c_parm *parm;
5842 c_parser_consume_token (parser);
5843 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
5846 c_parser_consume_token (parser);
5849 parm = c_parser_parameter_declaration (parser, NULL_TREE);
5852 parms = chainon (parms,
5853 build_tree_list (NULL_TREE, grokparm (parm)));
5855 TREE_OVERFLOW (parms) = ellipsis;
5858 return objc_build_method_signature (type, sel, parms);
5861 /* Parse an objc-type-name.
5864 objc-type-qualifiers[opt] type-name
5865 objc-type-qualifiers[opt]
5867 objc-type-qualifiers:
5869 objc-type-qualifiers objc-type-qualifier
5871 objc-type-qualifier: one of
5872 in out inout bycopy byref oneway
5876 c_parser_objc_type_name (c_parser *parser)
5878 tree quals = NULL_TREE;
5879 struct c_type_name *typename = NULL;
5880 tree type = NULL_TREE;
5883 c_token *token = c_parser_peek_token (parser);
5884 if (token->type == CPP_KEYWORD
5885 && (token->keyword == RID_IN
5886 || token->keyword == RID_OUT
5887 || token->keyword == RID_INOUT
5888 || token->keyword == RID_BYCOPY
5889 || token->keyword == RID_BYREF
5890 || token->keyword == RID_ONEWAY))
5892 quals = chainon (quals, build_tree_list (NULL_TREE, token->value));
5893 c_parser_consume_token (parser);
5898 if (c_parser_next_token_starts_typename (parser))
5899 typename = c_parser_type_name (parser);
5901 type = groktypename (typename);
5902 return build_tree_list (quals, type);
5905 /* Parse objc-protocol-refs.
5912 c_parser_objc_protocol_refs (c_parser *parser)
5914 tree list = NULL_TREE;
5915 gcc_assert (c_parser_next_token_is (parser, CPP_LESS));
5916 c_parser_consume_token (parser);
5917 /* Any identifiers, including those declared as type names, are OK
5922 if (c_parser_next_token_is_not (parser, CPP_NAME))
5924 c_parser_error (parser, "expected identifier");
5927 id = c_parser_peek_token (parser)->value;
5928 list = chainon (list, build_tree_list (NULL_TREE, id));
5929 c_parser_consume_token (parser);
5930 if (c_parser_next_token_is (parser, CPP_COMMA))
5931 c_parser_consume_token (parser);
5935 c_parser_require (parser, CPP_GREATER, "expected %<>%>");
5939 /* Parse an objc-try-catch-statement.
5941 objc-try-catch-statement:
5942 @try compound-statement objc-catch-list[opt]
5943 @try compound-statement objc-catch-list[opt] @finally compound-statement
5946 @catch ( parameter-declaration ) compound-statement
5947 objc-catch-list @catch ( parameter-declaration ) compound-statement
5951 c_parser_objc_try_catch_statement (c_parser *parser)
5955 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_TRY));
5956 c_parser_consume_token (parser);
5957 loc = c_parser_peek_token (parser)->location;
5958 stmt = c_parser_compound_statement (parser);
5959 objc_begin_try_stmt (loc, stmt);
5960 while (c_parser_next_token_is_keyword (parser, RID_AT_CATCH))
5962 struct c_parm *parm;
5963 c_parser_consume_token (parser);
5964 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5966 parm = c_parser_parameter_declaration (parser, NULL_TREE);
5969 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5972 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
5973 objc_begin_catch_clause (grokparm (parm));
5974 if (c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
5975 c_parser_compound_statement_nostart (parser);
5976 objc_finish_catch_clause ();
5978 if (c_parser_next_token_is_keyword (parser, RID_AT_FINALLY))
5982 c_parser_consume_token (parser);
5983 finloc = c_parser_peek_token (parser)->location;
5984 finstmt = c_parser_compound_statement (parser);
5985 objc_build_finally_clause (finloc, finstmt);
5987 objc_finish_try_stmt ();
5990 /* Parse an objc-synchronized-statement.
5992 objc-synchronized-statement:
5993 @synchronized ( expression ) compound-statement
5997 c_parser_objc_synchronized_statement (c_parser *parser)
6001 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_SYNCHRONIZED));
6002 c_parser_consume_token (parser);
6003 loc = c_parser_peek_token (parser)->location;
6004 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6006 expr = c_parser_expression (parser).value;
6007 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
6010 expr = error_mark_node;
6011 stmt = c_parser_compound_statement (parser);
6012 objc_build_synchronized (loc, expr, stmt);
6015 /* Parse an objc-selector; return NULL_TREE without an error if the
6016 next token is not an objc-selector.
6021 enum struct union if else while do for switch case default
6022 break continue return goto asm sizeof typeof __alignof
6023 unsigned long const short volatile signed restrict _Complex
6024 in out inout bycopy byref oneway int char float double void _Bool
6026 ??? Why this selection of keywords but not, for example, storage
6027 class specifiers? */
6030 c_parser_objc_selector (c_parser *parser)
6032 c_token *token = c_parser_peek_token (parser);
6033 tree value = token->value;
6034 if (token->type == CPP_NAME)
6036 c_parser_consume_token (parser);
6039 if (token->type != CPP_KEYWORD)
6041 switch (token->keyword)
6082 c_parser_consume_token (parser);
6089 /* Parse an objc-selector-arg.
6093 objc-keywordname-list
6095 objc-keywordname-list:
6097 objc-keywordname-list objc-keywordname
6105 c_parser_objc_selector_arg (c_parser *parser)
6107 tree sel = c_parser_objc_selector (parser);
6108 tree list = NULL_TREE;
6109 if (sel && c_parser_next_token_is_not (parser, CPP_COLON))
6113 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
6115 list = chainon (list, build_tree_list (sel, NULL_TREE));
6116 sel = c_parser_objc_selector (parser);
6117 if (!sel && c_parser_next_token_is_not (parser, CPP_COLON))
6123 /* Parse an objc-receiver.
6132 c_parser_objc_receiver (c_parser *parser)
6134 if (c_parser_peek_token (parser)->type == CPP_NAME
6135 && (c_parser_peek_token (parser)->id_kind == C_ID_TYPENAME
6136 || c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME))
6138 tree id = c_parser_peek_token (parser)->value;
6139 c_parser_consume_token (parser);
6140 return objc_get_class_reference (id);
6142 return c_parser_expression (parser).value;
6145 /* Parse objc-message-args.
6149 objc-keywordarg-list
6151 objc-keywordarg-list:
6153 objc-keywordarg-list objc-keywordarg
6156 objc-selector : objc-keywordexpr
6161 c_parser_objc_message_args (c_parser *parser)
6163 tree sel = c_parser_objc_selector (parser);
6164 tree list = NULL_TREE;
6165 if (sel && c_parser_next_token_is_not (parser, CPP_COLON))
6170 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
6172 keywordexpr = c_parser_objc_keywordexpr (parser);
6173 list = chainon (list, build_tree_list (sel, keywordexpr));
6174 sel = c_parser_objc_selector (parser);
6175 if (!sel && c_parser_next_token_is_not (parser, CPP_COLON))
6181 /* Parse an objc-keywordexpr.
6188 c_parser_objc_keywordexpr (c_parser *parser)
6190 tree list = c_parser_expr_list (parser);
6191 if (TREE_CHAIN (list) == NULL_TREE)
6193 /* Just return the expression, remove a level of
6195 return TREE_VALUE (list);
6199 /* We have a comma expression, we will collapse later. */
6205 /* The actual parser and external interface. ??? Does this need to be
6206 garbage-collected? */
6208 static GTY (()) c_parser *the_parser;
6210 /* Parse a single source file. */
6215 the_parser = c_parser_new ();
6216 c_parser_translation_unit (the_parser);
6220 #include "gt-c-parser.h"