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 /* More information about the type of a CPP_NAME token. */
243 typedef enum c_id_kind {
244 /* An ordinary identifier. */
246 /* An identifier declared as a typedef name. */
248 /* An identifier declared as an Objective-C class name. */
250 /* Not an identifier. */
254 /* A single C token after string literal concatenation and conversion
255 of preprocessing tokens to tokens. */
256 typedef struct c_token GTY (())
258 /* The kind of token. */
259 ENUM_BITFIELD (cpp_ttype) type : 8;
260 /* If this token is a CPP_NAME, this value indicates whether also
261 declared as some kind of type. Otherwise, it is C_ID_NONE. */
262 ENUM_BITFIELD (c_id_kind) id_kind : 8;
263 /* If this token is a keyword, this value indicates which keyword.
264 Otherwise, this value is RID_MAX. */
265 ENUM_BITFIELD (rid) keyword : 8;
266 /* True if this token is from a system header. */
267 BOOL_BITFIELD in_system_header : 1;
268 /* The value associated with this token, if any. */
270 /* The location at which this token was found. */
274 /* A parser structure recording information about the state and
275 context of parsing. Includes lexer information with up to two
276 tokens of look-ahead; more are not needed for C. */
277 typedef struct c_parser GTY(())
279 /* The look-ahead tokens. */
281 /* How many look-ahead tokens are available (0, 1 or 2). */
283 /* True if a syntax error is being recovered from; false otherwise.
284 c_parser_error sets this flag. It should clear this flag when
285 enough tokens have been consumed to recover from the error. */
286 BOOL_BITFIELD error : 1;
289 /* Read in and lex a single token, storing it in *TOKEN. */
292 c_lex_one_token (c_token *token)
294 timevar_push (TV_LEX);
295 token->type = c_lex_with_flags (&token->value, &token->location, NULL);
296 token->in_system_header = in_system_header;
300 token->id_kind = C_ID_NONE;
301 token->keyword = RID_MAX;
305 int objc_force_identifier = objc_need_raw_identifier;
306 OBJC_NEED_RAW_IDENTIFIER (0);
308 if (C_IS_RESERVED_WORD (token->value))
310 enum rid rid_code = C_RID_CODE (token->value);
312 if (c_dialect_objc ())
314 if (!OBJC_IS_AT_KEYWORD (rid_code)
315 && (!OBJC_IS_PQ_KEYWORD (rid_code) || objc_pq_context))
317 /* Return the canonical spelling for this keyword. */
318 token->value = ridpointers[(int) rid_code];
319 token->type = CPP_KEYWORD;
320 token->keyword = rid_code;
326 /* Return the canonical spelling for this keyword. */
327 token->value = ridpointers[(int) rid_code];
328 token->type = CPP_KEYWORD;
329 token->keyword = rid_code;
334 decl = lookup_name (token->value);
337 if (TREE_CODE (decl) == TYPE_DECL)
339 token->id_kind = C_ID_TYPENAME;
343 else if (c_dialect_objc ())
345 tree objc_interface_decl = objc_is_class_name (token->value);
346 /* Objective-C class names are in the same namespace as
347 variables and typedefs, and hence are shadowed by local
349 if (objc_interface_decl
350 && (global_bindings_p ()
351 || (!objc_force_identifier && !decl)))
353 token->value = objc_interface_decl;
354 token->id_kind = C_ID_CLASSNAME;
359 token->id_kind = C_ID_ID;
362 /* This only happens in Objective-C; it must be a keyword. */
363 token->type = CPP_KEYWORD;
364 token->id_kind = C_ID_NONE;
365 token->keyword = C_RID_CODE (token->value);
369 case CPP_CLOSE_PAREN:
371 /* These tokens may affect the interpretation of any identifiers
372 following, if doing Objective-C. */
373 OBJC_NEED_RAW_IDENTIFIER (0);
374 token->id_kind = C_ID_NONE;
375 token->keyword = RID_MAX;
378 token->id_kind = C_ID_NONE;
379 token->keyword = RID_MAX;
382 timevar_pop (TV_LEX);
385 /* Return a pointer to the next token from PARSER, reading it in if
388 static inline c_token *
389 c_parser_peek_token (c_parser *parser)
391 if (parser->tokens_avail == 0)
393 c_lex_one_token (&parser->tokens[0]);
394 parser->tokens_avail = 1;
396 return &parser->tokens[0];
399 /* Return true if the next token from PARSER has the indicated
403 c_parser_next_token_is (c_parser *parser, enum cpp_ttype type)
405 return c_parser_peek_token (parser)->type == type;
408 /* Return true if the next token from PARSER does not have the
412 c_parser_next_token_is_not (c_parser *parser, enum cpp_ttype type)
414 return !c_parser_next_token_is (parser, type);
417 /* Return true if the next token from PARSER is the indicated
421 c_parser_next_token_is_keyword (c_parser *parser, enum rid keyword)
425 /* Peek at the next token. */
426 token = c_parser_peek_token (parser);
427 /* Check to see if it is the indicated keyword. */
428 return token->keyword == keyword;
431 /* Return true if TOKEN can start a type name,
434 c_token_starts_typename (c_token *token)
439 switch (token->id_kind)
446 gcc_assert (c_dialect_objc ());
452 switch (token->keyword)
478 if (c_dialect_objc ())
486 /* Return true if the next token from PARSER can start a type name,
489 c_parser_next_token_starts_typename (c_parser *parser)
491 c_token *token = c_parser_peek_token (parser);
492 return c_token_starts_typename (token);
495 /* Return true if TOKEN can start declaration specifiers, false
498 c_token_starts_declspecs (c_token *token)
503 switch (token->id_kind)
510 gcc_assert (c_dialect_objc ());
516 switch (token->keyword)
549 if (c_dialect_objc ())
557 /* Return true if the next token from PARSER can start declaration
558 specifiers, false otherwise. */
560 c_parser_next_token_starts_declspecs (c_parser *parser)
562 c_token *token = c_parser_peek_token (parser);
563 return c_token_starts_declspecs (token);
566 /* Return a pointer to the next-but-one token from PARSER, reading it
567 in if necessary. The next token is already read in. */
570 c_parser_peek_2nd_token (c_parser *parser)
572 if (parser->tokens_avail >= 2)
573 return &parser->tokens[1];
574 gcc_assert (parser->tokens_avail == 1);
575 gcc_assert (parser->tokens[0].type != CPP_EOF);
576 c_lex_one_token (&parser->tokens[1]);
577 parser->tokens_avail = 2;
578 return &parser->tokens[1];
581 /* Consume the next token from PARSER. */
584 c_parser_consume_token (c_parser *parser)
586 if (parser->tokens_avail == 2)
587 parser->tokens[0] = parser->tokens[1];
590 gcc_assert (parser->tokens_avail == 1);
591 gcc_assert (parser->tokens[0].type != CPP_EOF);
593 parser->tokens_avail--;
596 /* Update the globals input_location and in_system_header from
599 c_parser_set_source_position_from_token (c_token *token)
601 if (token->type != CPP_EOF)
603 input_location = token->location;
604 in_system_header = token->in_system_header;
608 /* Allocate a new parser. */
613 /* Use local storage to lex the first token because loading a PCH
614 file may cause garbage collection. */
617 memset (&tparser, 0, sizeof tparser);
618 c_lex_one_token (&tparser.tokens[0]);
619 tparser.tokens_avail = 1;
620 ret = GGC_NEW (c_parser);
621 memcpy (ret, &tparser, sizeof tparser);
625 /* Issue a diagnostic of the form
626 FILE:LINE: MESSAGE before TOKEN
627 where TOKEN is the next token in the input stream of PARSER.
628 MESSAGE (specified by the caller) is usually of the form "expected
631 Do not issue a diagnostic if still recovering from an error.
633 ??? This is taken from the C++ parser, but building up messages in
634 this way is not i18n-friendly and some other approach should be
638 c_parser_error (c_parser *parser, const char *msgid)
640 c_token *token = c_parser_peek_token (parser);
643 parser->error = true;
646 /* This diagnostic makes more sense if it is tagged to the line of
647 the token we just peeked at. */
648 c_parser_set_source_position_from_token (token);
649 c_parse_error (msgid,
650 /* Because c_parse_error does not understand
651 CPP_KEYWORD, keywords are treated like
653 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
657 /* If the next token is of the indicated TYPE, consume it. Otherwise,
658 issue the error MSGID. If MSGID is NULL then a message has already
659 been produced and no message will be produced this time. Returns
660 true if found, false otherwise. */
663 c_parser_require (c_parser *parser,
667 if (c_parser_next_token_is (parser, type))
669 c_parser_consume_token (parser);
674 c_parser_error (parser, msgid);
679 /* If the next token is the indicated keyword, consume it. Otherwise,
680 issue the error MSGID. Returns true if found, false otherwise. */
683 c_parser_require_keyword (c_parser *parser,
687 if (c_parser_next_token_is_keyword (parser, keyword))
689 c_parser_consume_token (parser);
694 c_parser_error (parser, msgid);
699 /* Like c_parser_require, except that tokens will be skipped until the
700 desired token is found. An error message is still produced if the
701 next token is not as expected. If MSGID is NULL then a message has
702 already been produced and no message will be produced this
706 c_parser_skip_until_found (c_parser *parser,
710 unsigned nesting_depth = 0;
712 if (c_parser_require (parser, type, msgid))
715 /* Skip tokens until the desired token is found. */
718 /* Peek at the next token. */
719 c_token *token = c_parser_peek_token (parser);
720 /* If we've reached the token we want, consume it and stop. */
721 if (token->type == type && !nesting_depth)
723 c_parser_consume_token (parser);
726 /* If we've run out of tokens, stop. */
727 if (token->type == CPP_EOF)
729 if (token->type == CPP_OPEN_BRACE
730 || token->type == CPP_OPEN_PAREN
731 || token->type == CPP_OPEN_SQUARE)
733 else if (token->type == CPP_CLOSE_BRACE
734 || token->type == CPP_CLOSE_PAREN
735 || token->type == CPP_CLOSE_SQUARE)
737 if (nesting_depth-- == 0)
740 /* Consume this token. */
741 c_parser_consume_token (parser);
743 parser->error = false;
746 /* Skip tokens until the end of a parameter is found, but do not
747 consume the comma, semicolon or closing delimiter. */
750 c_parser_skip_to_end_of_parameter (c_parser *parser)
752 unsigned nesting_depth = 0;
756 c_token *token = c_parser_peek_token (parser);
757 if ((token->type == CPP_COMMA || token->type == CPP_SEMICOLON)
760 /* If we've run out of tokens, stop. */
761 if (token->type == CPP_EOF)
763 if (token->type == CPP_OPEN_BRACE
764 || token->type == CPP_OPEN_PAREN
765 || token->type == CPP_OPEN_SQUARE)
767 else if (token->type == CPP_CLOSE_BRACE
768 || token->type == CPP_CLOSE_PAREN
769 || token->type == CPP_CLOSE_SQUARE)
771 if (nesting_depth-- == 0)
774 /* Consume this token. */
775 c_parser_consume_token (parser);
777 parser->error = false;
780 /* Skip tokens until we have consumed an entire block, or until we
781 have consumed a non-nested ';'. */
784 c_parser_skip_to_end_of_block_or_statement (c_parser *parser)
786 unsigned nesting_depth = 0;
792 /* Peek at the next token. */
793 token = c_parser_peek_token (parser);
794 /* If we've run out of tokens, stop. */
795 if (token->type == CPP_EOF)
797 /* If the next token is a ';', we have reached the end of the
799 if (token->type == CPP_SEMICOLON && !nesting_depth)
801 /* Consume the ';'. */
802 c_parser_consume_token (parser);
805 /* If the next token is a non-nested '}', then we have reached
806 the end of the current block. */
807 if (token->type == CPP_CLOSE_BRACE
808 && (nesting_depth == 0 || --nesting_depth == 0))
810 c_parser_consume_token (parser);
813 /* If it the next token is a '{', then we are entering a new
814 block. Consume the entire block. */
815 if (token->type == CPP_OPEN_BRACE)
817 c_parser_consume_token (parser);
819 parser->error = false;
823 /* Save the warning flags which are controlled by __extension__. */
826 disable_extension_diagnostics (void)
829 | (warn_pointer_arith << 1)
830 | (warn_traditional << 2)
833 warn_pointer_arith = 0;
834 warn_traditional = 0;
839 /* Restore the warning flags which are controlled by __extension__.
840 FLAGS is the return value from disable_extension_diagnostics. */
843 restore_extension_diagnostics (int flags)
845 pedantic = flags & 1;
846 warn_pointer_arith = (flags >> 1) & 1;
847 warn_traditional = (flags >> 2) & 1;
848 flag_iso = (flags >> 3) & 1;
851 /* Possibly kinds of declarator to parse. */
852 typedef enum c_dtr_syn {
853 /* A normal declarator with an identifier. */
855 /* An abstract declarator (maybe empty). */
857 /* A parameter declarator: may be either, but after a type name does
858 not redeclare a typedef name as an identifier if it can
859 alternatively be interpreted as a typedef name; see DR#009,
860 applied in C90 TC1, omitted from C99 and reapplied in C99 TC2
861 following DR#249. For example, given a typedef T, "int T" and
862 "int *T" are valid parameter declarations redeclaring T, while
863 "int (T)" and "int * (T)" and "int (T[])" and "int (T (int))" are
864 abstract declarators rather than involving redundant parentheses;
865 the same applies with attributes inside the parentheses before
870 static void c_parser_external_declaration (c_parser *);
871 static void c_parser_asm_definition (c_parser *);
872 static void c_parser_declaration_or_fndef (c_parser *, bool, bool, bool, bool);
873 static void c_parser_declspecs (c_parser *, struct c_declspecs *, bool, bool,
875 static struct c_typespec c_parser_enum_specifier (c_parser *);
876 static struct c_typespec c_parser_struct_or_union_specifier (c_parser *);
877 static tree c_parser_struct_declaration (c_parser *);
878 static struct c_typespec c_parser_typeof_specifier (c_parser *);
879 static struct c_declarator *c_parser_declarator (c_parser *, bool, c_dtr_syn,
881 static struct c_declarator *c_parser_direct_declarator (c_parser *, bool,
883 static struct c_declarator *c_parser_direct_declarator_inner (c_parser *,
885 struct c_declarator *);
886 static struct c_arg_info *c_parser_parms_declarator (c_parser *, bool, tree);
887 static struct c_arg_info *c_parser_parms_list_declarator (c_parser *, tree);
888 static struct c_parm *c_parser_parameter_declaration (c_parser *, tree);
889 static tree c_parser_simple_asm_expr (c_parser *);
890 static tree c_parser_attributes (c_parser *);
891 static struct c_type_name *c_parser_type_name (c_parser *);
892 static struct c_expr c_parser_initializer (c_parser *);
893 static struct c_expr c_parser_braced_init (c_parser *, tree, bool);
894 static void c_parser_initelt (c_parser *);
895 static void c_parser_initval (c_parser *, struct c_expr *);
896 static tree c_parser_compound_statement (c_parser *);
897 static void c_parser_compound_statement_nostart (c_parser *);
898 static void c_parser_label (c_parser *);
899 static void c_parser_statement (c_parser *);
900 static void c_parser_statement_after_labels (c_parser *);
901 static void c_parser_if_statement (c_parser *);
902 static void c_parser_switch_statement (c_parser *);
903 static void c_parser_while_statement (c_parser *);
904 static void c_parser_do_statement (c_parser *);
905 static void c_parser_for_statement (c_parser *);
906 static tree c_parser_asm_statement (c_parser *);
907 static tree c_parser_asm_operands (c_parser *);
908 static tree c_parser_asm_clobbers (c_parser *);
909 static struct c_expr c_parser_expr_no_commas (c_parser *, struct c_expr *);
910 static struct c_expr c_parser_conditional_expression (c_parser *,
912 static struct c_expr c_parser_binary_expression (c_parser *, struct c_expr *);
913 static struct c_expr c_parser_cast_expression (c_parser *, struct c_expr *);
914 static struct c_expr c_parser_unary_expression (c_parser *);
915 static struct c_expr c_parser_sizeof_expression (c_parser *);
916 static struct c_expr c_parser_alignof_expression (c_parser *);
917 static struct c_expr c_parser_postfix_expression (c_parser *);
918 static struct c_expr c_parser_postfix_expression_after_paren_type (c_parser *,
919 struct c_type_name *);
920 static struct c_expr c_parser_postfix_expression_after_primary (c_parser *,
922 static struct c_expr c_parser_expression (c_parser *);
923 static tree c_parser_expr_list (c_parser *);
925 /* These Objective-C parser functions are only ever called when
926 compiling Objective-C. */
927 static void c_parser_objc_class_definition (c_parser *);
928 static void c_parser_objc_class_instance_variables (c_parser *);
929 static void c_parser_objc_class_declaration (c_parser *);
930 static void c_parser_objc_alias_declaration (c_parser *);
931 static void c_parser_objc_protocol_definition (c_parser *);
932 static enum tree_code c_parser_objc_method_type (c_parser *);
933 static void c_parser_objc_method_definition (c_parser *);
934 static void c_parser_objc_methodprotolist (c_parser *);
935 static void c_parser_objc_methodproto (c_parser *);
936 static tree c_parser_objc_method_decl (c_parser *);
937 static tree c_parser_objc_type_name (c_parser *);
938 static tree c_parser_objc_protocol_refs (c_parser *);
939 static void c_parser_objc_try_catch_statement (c_parser *);
940 static void c_parser_objc_synchronized_statement (c_parser *);
941 static tree c_parser_objc_selector (c_parser *);
942 static tree c_parser_objc_selector_arg (c_parser *);
943 static tree c_parser_objc_receiver (c_parser *);
944 static tree c_parser_objc_message_args (c_parser *);
945 static tree c_parser_objc_keywordexpr (c_parser *);
947 /* Parse a translation unit (C90 6.7, C99 6.9).
950 external-declarations
952 external-declarations:
954 external-declarations external-declaration
963 c_parser_translation_unit (c_parser *parser)
965 if (c_parser_next_token_is (parser, CPP_EOF))
968 pedwarn ("ISO C forbids an empty source file");
972 void *obstack_position = obstack_alloc (&parser_obstack, 0);
976 c_parser_external_declaration (parser);
977 obstack_free (&parser_obstack, obstack_position);
979 while (c_parser_next_token_is_not (parser, CPP_EOF));
983 /* Parse an external declaration (C90 6.7, C99 6.9).
985 external-declaration:
991 external-declaration:
994 __extension__ external-declaration
998 external-declaration:
999 objc-class-definition
1000 objc-class-declaration
1001 objc-alias-declaration
1002 objc-protocol-definition
1003 objc-method-definition
1008 c_parser_external_declaration (c_parser *parser)
1011 switch (c_parser_peek_token (parser)->type)
1014 switch (c_parser_peek_token (parser)->keyword)
1017 ext = disable_extension_diagnostics ();
1018 c_parser_consume_token (parser);
1019 c_parser_external_declaration (parser);
1020 restore_extension_diagnostics (ext);
1023 c_parser_asm_definition (parser);
1025 case RID_AT_INTERFACE:
1026 case RID_AT_IMPLEMENTATION:
1027 gcc_assert (c_dialect_objc ());
1028 c_parser_objc_class_definition (parser);
1031 gcc_assert (c_dialect_objc ());
1032 c_parser_objc_class_declaration (parser);
1035 gcc_assert (c_dialect_objc ());
1036 c_parser_objc_alias_declaration (parser);
1038 case RID_AT_PROTOCOL:
1039 gcc_assert (c_dialect_objc ());
1040 c_parser_objc_protocol_definition (parser);
1043 gcc_assert (c_dialect_objc ());
1044 c_parser_consume_token (parser);
1045 objc_finish_implementation ();
1053 pedwarn ("ISO C does not allow extra %<;%> outside of a function");
1054 c_parser_consume_token (parser);
1058 if (c_dialect_objc ())
1060 c_parser_objc_method_definition (parser);
1063 /* Else fall through, and yield a syntax error trying to parse
1064 as a declaration or function definition. */
1067 /* A declaration or a function definition. We can only tell
1068 which after parsing the declaration specifiers, if any, and
1069 the first declarator. */
1070 c_parser_declaration_or_fndef (parser, true, true, false, true);
1075 /* Parse a declaration or function definition (C90 6.5, 6.7.1, C99
1076 6.7, 6.9.1). If FNDEF_OK is true, a function definition is
1077 accepted; otherwise (old-style parameter declarations) only other
1078 declarations are accepted. If NESTED is true, we are inside a
1079 function or parsing old-style parameter declarations; any functions
1080 encountered are nested functions and declaration specifiers are
1081 required; otherwise we are at top level and functions are normal
1082 functions and declaration specifiers may be optional. If EMPTY_OK
1083 is true, empty declarations are OK (subject to all other
1084 constraints); otherwise (old-style parameter declarations) they are
1085 diagnosed. If START_ATTR_OK is true, the declaration specifiers
1086 may start with attributes; otherwise they may not.
1089 declaration-specifiers init-declarator-list[opt] ;
1091 function-definition:
1092 declaration-specifiers[opt] declarator declaration-list[opt]
1097 declaration-list declaration
1099 init-declarator-list:
1101 init-declarator-list , init-declarator
1104 declarator simple-asm-expr[opt] attributes[opt]
1105 declarator simple-asm-expr[opt] attributes[opt] = initializer
1109 nested-function-definition:
1110 declaration-specifiers declarator declaration-list[opt]
1113 The simple-asm-expr and attributes are GNU extensions.
1115 This function does not handle __extension__; that is handled in its
1116 callers. ??? Following the old parser, __extension__ may start
1117 external declarations, declarations in functions and declarations
1118 at the start of "for" loops, but not old-style parameter
1121 C99 requires declaration specifiers in a function definition; the
1122 absence is diagnosed through the diagnosis of implicit int. In GNU
1123 C we also allow but diagnose declarations without declaration
1124 specifiers, but only at top level (elsewhere they conflict with
1128 c_parser_declaration_or_fndef (c_parser *parser, bool fndef_ok, bool empty_ok,
1129 bool nested, bool start_attr_ok)
1131 struct c_declspecs *specs;
1133 tree all_prefix_attrs;
1134 bool diagnosed_no_specs = false;
1135 specs = build_null_declspecs ();
1136 c_parser_declspecs (parser, specs, true, true, start_attr_ok);
1139 c_parser_skip_to_end_of_block_or_statement (parser);
1142 if (nested && !specs->declspecs_seen_p)
1144 c_parser_error (parser, "expected declaration specifiers");
1145 c_parser_skip_to_end_of_block_or_statement (parser);
1148 finish_declspecs (specs);
1149 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1155 shadow_tag_warned (specs, 1);
1156 pedwarn ("empty declaration");
1158 c_parser_consume_token (parser);
1161 pending_xref_error ();
1162 prefix_attrs = specs->attrs;
1163 all_prefix_attrs = prefix_attrs;
1164 specs->attrs = NULL_TREE;
1167 struct c_declarator *declarator;
1170 /* Declaring either one or more declarators (in which case we
1171 should diagnose if there were no declaration specifiers) or a
1172 function definition (in which case the diagnostic for
1173 implicit int suffices). */
1174 declarator = c_parser_declarator (parser, specs->type_seen_p,
1175 C_DTR_NORMAL, &dummy);
1176 if (declarator == NULL)
1178 c_parser_skip_to_end_of_block_or_statement (parser);
1181 if (c_parser_next_token_is (parser, CPP_EQ)
1182 || c_parser_next_token_is (parser, CPP_COMMA)
1183 || c_parser_next_token_is (parser, CPP_SEMICOLON)
1184 || c_parser_next_token_is_keyword (parser, RID_ASM)
1185 || c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
1187 tree asm_name = NULL_TREE;
1188 tree postfix_attrs = NULL_TREE;
1189 if (!diagnosed_no_specs && !specs->declspecs_seen_p)
1191 diagnosed_no_specs = true;
1192 pedwarn ("data definition has no type or storage class");
1194 /* Having seen a data definition, there cannot now be a
1195 function definition. */
1197 if (c_parser_next_token_is_keyword (parser, RID_ASM))
1198 asm_name = c_parser_simple_asm_expr (parser);
1199 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
1200 postfix_attrs = c_parser_attributes (parser);
1201 if (c_parser_next_token_is (parser, CPP_EQ))
1205 c_parser_consume_token (parser);
1206 /* The declaration of the variable is in effect while
1207 its initializer is parsed. */
1208 d = start_decl (declarator, specs, true,
1209 chainon (postfix_attrs, all_prefix_attrs));
1211 d = error_mark_node;
1212 start_init (d, asm_name, global_bindings_p ());
1213 init = c_parser_initializer (parser);
1215 if (d != error_mark_node)
1217 maybe_warn_string_init (TREE_TYPE (d), init);
1218 finish_decl (d, init.value, asm_name);
1223 tree d = start_decl (declarator, specs, false,
1224 chainon (postfix_attrs,
1227 finish_decl (d, NULL_TREE, asm_name);
1229 if (c_parser_next_token_is (parser, CPP_COMMA))
1231 c_parser_consume_token (parser);
1232 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
1233 all_prefix_attrs = chainon (c_parser_attributes (parser),
1236 all_prefix_attrs = prefix_attrs;
1239 else if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1241 c_parser_consume_token (parser);
1246 c_parser_error (parser, "expected %<,%> or %<;%>");
1247 c_parser_skip_to_end_of_block_or_statement (parser);
1253 c_parser_error (parser, "expected %<=%>, %<,%>, %<;%>, "
1254 "%<asm%> or %<__attribute__%>");
1255 c_parser_skip_to_end_of_block_or_statement (parser);
1258 /* Function definition (nested or otherwise). */
1262 pedwarn ("ISO C forbids nested functions");
1263 push_function_context ();
1265 if (!start_function (specs, declarator, all_prefix_attrs))
1267 /* This can appear in many cases looking nothing like a
1268 function definition, so we don't give a more specific
1269 error suggesting there was one. */
1270 c_parser_error (parser, "expected %<=%>, %<,%>, %<;%>, %<asm%> "
1271 "or %<__attribute__%>");
1273 pop_function_context ();
1276 /* Parse old-style parameter declarations. ??? Attributes are
1277 not allowed to start declaration specifiers here because of a
1278 syntax conflict between a function declaration with attribute
1279 suffix and a function definition with an attribute prefix on
1280 first old-style parameter declaration. Following the old
1281 parser, they are not accepted on subsequent old-style
1282 parameter declarations either. However, there is no
1283 ambiguity after the first declaration, nor indeed on the
1284 first as long as we don't allow postfix attributes after a
1285 declarator with a nonempty identifier list in a definition;
1286 and postfix attributes have never been accepted here in
1287 function definitions either. */
1288 while (c_parser_next_token_is_not (parser, CPP_EOF)
1289 && c_parser_next_token_is_not (parser, CPP_OPEN_BRACE))
1290 c_parser_declaration_or_fndef (parser, false, false, true, false);
1291 DECL_SOURCE_LOCATION (current_function_decl)
1292 = c_parser_peek_token (parser)->location;
1293 store_parm_decls ();
1294 fnbody = c_parser_compound_statement (parser);
1297 tree decl = current_function_decl;
1300 pop_function_context ();
1301 add_stmt (build_stmt (DECL_EXPR, decl));
1312 /* Parse an asm-definition (asm() outside a function body). This is a
1320 c_parser_asm_definition (c_parser *parser)
1322 tree asm_str = c_parser_simple_asm_expr (parser);
1323 /* ??? This only works sensibly in the presence of
1324 -fno-unit-at-a-time; file-scope asms really need to be passed to
1325 cgraph which needs to preserve the order of functions and
1328 assemble_asm (asm_str);
1329 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
1332 /* Parse some declaration specifiers (possibly none) (C90 6.5, C99
1333 6.7), adding them to SPECS (which may already include some).
1334 Storage class specifiers are accepted iff SCSPEC_OK; type
1335 specifiers are accepted iff TYPESPEC_OK; attributes are accepted at
1336 the start iff START_ATTR_OK.
1338 declaration-specifiers:
1339 storage-class-specifier declaration-specifiers[opt]
1340 type-specifier declaration-specifiers[opt]
1341 type-qualifier declaration-specifiers[opt]
1342 function-specifier declaration-specifiers[opt]
1344 Function specifiers (inline) are from C99, and are currently
1345 handled as storage class specifiers, as is __thread.
1347 C90 6.5.1, C99 6.7.1:
1348 storage-class-specifier:
1359 C90 6.5.2, C99 6.7.2:
1372 [_Imaginary removed in C99 TC2]
1373 struct-or-union-specifier
1377 (_Bool and _Complex are new in C99.)
1379 C90 6.5.3, C99 6.7.3:
1386 (restrict is new in C99.)
1390 declaration-specifiers:
1391 attributes declaration-specifiers[opt]
1393 storage-class-specifier:
1402 class-name objc-protocol-refs[opt]
1403 typedef-name objc-protocol-refs
1408 c_parser_declspecs (c_parser *parser, struct c_declspecs *specs,
1409 bool scspec_ok, bool typespec_ok, bool start_attr_ok)
1411 bool attrs_ok = start_attr_ok;
1412 bool seen_type = specs->type_seen_p;
1413 while (c_parser_next_token_is (parser, CPP_NAME)
1414 || c_parser_next_token_is (parser, CPP_KEYWORD)
1415 || (c_dialect_objc () && c_parser_next_token_is (parser, CPP_LESS)))
1417 struct c_typespec t;
1419 if (c_parser_next_token_is (parser, CPP_NAME))
1421 tree value = c_parser_peek_token (parser)->value;
1422 c_id_kind kind = c_parser_peek_token (parser)->id_kind;
1423 /* This finishes the specifiers unless a type name is OK, it
1424 is declared as a type name and a type name hasn't yet
1426 if (!typespec_ok || seen_type
1427 || (kind != C_ID_TYPENAME && kind != C_ID_CLASSNAME))
1429 c_parser_consume_token (parser);
1432 if (kind == C_ID_TYPENAME
1433 && (!c_dialect_objc ()
1434 || c_parser_next_token_is_not (parser, CPP_LESS)))
1436 t.kind = ctsk_typedef;
1437 /* For a typedef name, record the meaning, not the name.
1438 In case of 'foo foo, bar;'. */
1439 t.spec = lookup_name (value);
1443 tree proto = NULL_TREE;
1444 gcc_assert (c_dialect_objc ());
1446 if (c_parser_next_token_is (parser, CPP_LESS))
1447 proto = c_parser_objc_protocol_refs (parser);
1448 t.spec = objc_get_protocol_qualified_type (value, proto);
1450 declspecs_add_type (specs, t);
1453 if (c_parser_next_token_is (parser, CPP_LESS))
1455 /* Make "<SomeProtocol>" equivalent to "id <SomeProtocol>" -
1456 nisse@lysator.liu.se. */
1458 gcc_assert (c_dialect_objc ());
1459 if (!typespec_ok || seen_type)
1461 proto = c_parser_objc_protocol_refs (parser);
1463 t.spec = objc_get_protocol_qualified_type (NULL_TREE, proto);
1464 declspecs_add_type (specs, t);
1467 gcc_assert (c_parser_next_token_is (parser, CPP_KEYWORD));
1468 switch (c_parser_peek_token (parser)->keyword)
1480 /* TODO: Distinguish between function specifiers (inline)
1481 and storage class specifiers, either here or in
1482 declspecs_add_scspec. */
1483 declspecs_add_scspec (specs, c_parser_peek_token (parser)->value);
1484 c_parser_consume_token (parser);
1501 OBJC_NEED_RAW_IDENTIFIER (1);
1502 t.kind = ctsk_resword;
1503 t.spec = c_parser_peek_token (parser)->value;
1504 declspecs_add_type (specs, t);
1505 c_parser_consume_token (parser);
1512 t = c_parser_enum_specifier (parser);
1513 declspecs_add_type (specs, t);
1521 t = c_parser_struct_or_union_specifier (parser);
1522 declspecs_add_type (specs, t);
1525 /* ??? The old parser rejected typeof after other type
1526 specifiers, but is a syntax error the best way of
1528 if (!typespec_ok || seen_type)
1532 t = c_parser_typeof_specifier (parser);
1533 declspecs_add_type (specs, t);
1539 declspecs_add_qual (specs, c_parser_peek_token (parser)->value);
1540 c_parser_consume_token (parser);
1545 attrs = c_parser_attributes (parser);
1546 declspecs_add_attrs (specs, attrs);
1555 /* Parse an enum specifier (C90 6.5.2.2, C99 6.7.2.2).
1558 enum attributes[opt] identifier[opt] { enumerator-list } attributes[opt]
1559 enum attributes[opt] identifier[opt] { enumerator-list , } attributes[opt]
1560 enum attributes[opt] identifier
1562 The form with trailing comma is new in C99. The forms with
1563 attributes are GNU extensions. In GNU C, we accept any expression
1564 without commas in the syntax (assignment expressions, not just
1565 conditional expressions); assignment expressions will be diagnosed
1570 enumerator-list , enumerator
1573 enumeration-constant
1574 enumeration-constant = constant-expression
1577 static struct c_typespec
1578 c_parser_enum_specifier (c_parser *parser)
1580 struct c_typespec ret;
1582 tree ident = NULL_TREE;
1583 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ENUM));
1584 c_parser_consume_token (parser);
1585 attrs = c_parser_attributes (parser);
1586 if (c_parser_next_token_is (parser, CPP_NAME))
1588 ident = c_parser_peek_token (parser)->value;
1589 c_parser_consume_token (parser);
1591 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
1593 /* Parse an enum definition. */
1594 tree type = start_enum (ident);
1596 /* We chain the enumerators in reverse order, then put them in
1597 forward order at the end. */
1598 tree values = NULL_TREE;
1599 c_parser_consume_token (parser);
1606 if (c_parser_next_token_is_not (parser, CPP_NAME))
1608 c_parser_error (parser, "expected identifier");
1609 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
1610 values = error_mark_node;
1613 enum_id = c_parser_peek_token (parser)->value;
1614 c_parser_consume_token (parser);
1615 if (c_parser_next_token_is (parser, CPP_EQ))
1617 c_parser_consume_token (parser);
1618 enum_value = c_parser_expr_no_commas (parser, NULL).value;
1621 enum_value = NULL_TREE;
1622 enum_decl = build_enumerator (enum_id, enum_value);
1623 TREE_CHAIN (enum_decl) = values;
1626 if (c_parser_next_token_is (parser, CPP_COMMA))
1629 c_parser_consume_token (parser);
1631 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
1633 if (seen_comma && pedantic && !flag_isoc99)
1634 pedwarn ("comma at end of enumerator list");
1635 c_parser_consume_token (parser);
1640 c_parser_error (parser, "expected %<,%> or %<}%>");
1641 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
1642 values = error_mark_node;
1646 postfix_attrs = c_parser_attributes (parser);
1647 ret.spec = finish_enum (type, nreverse (values),
1648 chainon (attrs, postfix_attrs));
1649 ret.kind = ctsk_tagdef;
1654 c_parser_error (parser, "expected %<{%>");
1655 ret.spec = error_mark_node;
1656 ret.kind = ctsk_tagref;
1659 ret = parser_xref_tag (ENUMERAL_TYPE, ident);
1660 /* In ISO C, enumerated types can be referred to only if already
1662 if (pedantic && !COMPLETE_TYPE_P (ret.spec))
1663 pedwarn ("ISO C forbids forward references to %<enum%> types");
1667 /* Parse a struct or union specifier (C90 6.5.2.1, C99 6.7.2.1).
1669 struct-or-union-specifier:
1670 struct-or-union attributes[opt] identifier[opt]
1671 { struct-contents } attributes[opt]
1672 struct-or-union attributes[opt] identifier
1675 struct-declaration-list
1677 struct-declaration-list:
1678 struct-declaration ;
1679 struct-declaration-list struct-declaration ;
1686 struct-declaration-list struct-declaration
1688 struct-declaration-list:
1689 struct-declaration-list ;
1692 (Note that in the syntax here, unlike that in ISO C, the semicolons
1693 are included here rather than in struct-declaration, in order to
1694 describe the syntax with extra semicolons and missing semicolon at
1699 struct-declaration-list:
1700 @defs ( class-name )
1702 (Note this does not include a trailing semicolon, but can be
1703 followed by further declarations, and gets a pedwarn-if-pedantic
1704 when followed by a semicolon.) */
1706 static struct c_typespec
1707 c_parser_struct_or_union_specifier (c_parser *parser)
1709 struct c_typespec ret;
1711 tree ident = NULL_TREE;
1712 enum tree_code code;
1713 switch (c_parser_peek_token (parser)->keyword)
1724 c_parser_consume_token (parser);
1725 attrs = c_parser_attributes (parser);
1726 if (c_parser_next_token_is (parser, CPP_NAME))
1728 ident = c_parser_peek_token (parser)->value;
1729 c_parser_consume_token (parser);
1731 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
1733 /* Parse a struct or union definition. Start the scope of the
1734 tag before parsing components. */
1735 tree type = start_struct (code, ident);
1737 /* We chain the components in reverse order, then put them in
1738 forward order at the end. Each struct-declaration may
1739 declare multiple components (comma-separated), so we must use
1740 chainon to join them, although when parsing each
1741 struct-declaration we can use TREE_CHAIN directly.
1743 The theory behind all this is that there will be more
1744 semicolon separated fields than comma separated fields, and
1745 so we'll be minimizing the number of node traversals required
1747 tree contents = NULL_TREE;
1748 c_parser_consume_token (parser);
1749 /* Handle the Objective-C @defs construct,
1750 e.g. foo(sizeof(struct{ @defs(ClassName) }));. */
1751 if (c_parser_next_token_is_keyword (parser, RID_AT_DEFS))
1754 gcc_assert (c_dialect_objc ());
1755 c_parser_consume_token (parser);
1756 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
1758 if (c_parser_next_token_is (parser, CPP_NAME)
1759 && c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME)
1761 name = c_parser_peek_token (parser)->value;
1762 c_parser_consume_token (parser);
1766 c_parser_error (parser, "expected class name");
1767 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
1770 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
1772 contents = nreverse (objc_get_class_ivars (name));
1775 /* Parse the struct-declarations and semicolons. Problems with
1776 semicolons are diagnosed here; empty structures are diagnosed
1781 /* Parse any stray semicolon. */
1782 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1785 pedwarn ("extra semicolon in struct or union specified");
1786 c_parser_consume_token (parser);
1789 /* Stop if at the end of the struct or union contents. */
1790 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
1792 c_parser_consume_token (parser);
1795 /* Parse some comma-separated declarations, but not the
1796 trailing semicolon if any. */
1797 decls = c_parser_struct_declaration (parser);
1798 contents = chainon (decls, contents);
1799 /* If no semicolon follows, either we have a parse error or
1800 are at the end of the struct or union and should
1802 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1803 c_parser_consume_token (parser);
1806 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
1807 pedwarn ("no semicolon at end of struct or union");
1810 c_parser_error (parser, "expected %<;%>");
1811 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
1816 postfix_attrs = c_parser_attributes (parser);
1817 ret.spec = finish_struct (type, nreverse (contents),
1818 chainon (attrs, postfix_attrs));
1819 ret.kind = ctsk_tagdef;
1824 c_parser_error (parser, "expected %<{%>");
1825 ret.spec = error_mark_node;
1826 ret.kind = ctsk_tagref;
1829 ret = parser_xref_tag (code, ident);
1833 /* Parse a struct-declaration (C90 6.5.2.1, C99 6.7.2.1), *without*
1834 the trailing semicolon.
1837 specifier-qualifier-list struct-declarator-list
1839 specifier-qualifier-list:
1840 type-specifier specifier-qualifier-list[opt]
1841 type-qualifier specifier-qualifier-list[opt]
1842 attributes specifier-qualifier-list[opt]
1844 struct-declarator-list:
1846 struct-declarator-list , attributes[opt] struct-declarator
1849 declarator attributes[opt]
1850 declarator[opt] : constant-expression attributes[opt]
1855 __extension__ struct-declaration
1856 specifier-qualifier-list
1858 Unlike the ISO C syntax, semicolons are handled elsewhere. The use
1859 of attributes where shown is a GNU extension. In GNU C, we accept
1860 any expression without commas in the syntax (assignment
1861 expressions, not just conditional expressions); assignment
1862 expressions will be diagnosed as non-constant. */
1865 c_parser_struct_declaration (c_parser *parser)
1867 struct c_declspecs *specs;
1869 tree all_prefix_attrs;
1871 if (c_parser_next_token_is_keyword (parser, RID_EXTENSION))
1875 ext = disable_extension_diagnostics ();
1876 c_parser_consume_token (parser);
1877 decl = c_parser_struct_declaration (parser);
1878 restore_extension_diagnostics (ext);
1881 specs = build_null_declspecs ();
1882 c_parser_declspecs (parser, specs, false, true, true);
1885 if (!specs->declspecs_seen_p)
1887 c_parser_error (parser, "expected specifier-qualifier-list");
1890 finish_declspecs (specs);
1891 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1894 if (!specs->type_seen_p)
1897 pedwarn ("ISO C forbids member declarations with no members");
1898 shadow_tag_warned (specs, pedantic);
1903 /* Support for unnamed structs or unions as members of
1904 structs or unions (which is [a] useful and [b] supports
1906 ret = grokfield (build_id_declarator (NULL_TREE), specs, NULL_TREE);
1910 pending_xref_error ();
1911 prefix_attrs = specs->attrs;
1912 all_prefix_attrs = prefix_attrs;
1913 specs->attrs = NULL_TREE;
1917 /* Declaring one or more declarators or un-named bit-fields. */
1918 struct c_declarator *declarator;
1920 if (c_parser_next_token_is (parser, CPP_COLON))
1921 declarator = build_id_declarator (NULL_TREE);
1923 declarator = c_parser_declarator (parser, specs->type_seen_p,
1924 C_DTR_NORMAL, &dummy);
1925 if (declarator == NULL)
1927 c_parser_skip_to_end_of_block_or_statement (parser);
1930 if (c_parser_next_token_is (parser, CPP_COLON)
1931 || c_parser_next_token_is (parser, CPP_COMMA)
1932 || c_parser_next_token_is (parser, CPP_SEMICOLON)
1933 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE)
1934 || c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
1936 tree postfix_attrs = NULL_TREE;
1937 tree width = NULL_TREE;
1939 if (c_parser_next_token_is (parser, CPP_COLON))
1941 c_parser_consume_token (parser);
1942 width = c_parser_expr_no_commas (parser, NULL).value;
1944 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
1945 postfix_attrs = c_parser_attributes (parser);
1946 d = grokfield (declarator, specs, width);
1947 decl_attributes (&d, chainon (postfix_attrs,
1948 all_prefix_attrs), 0);
1949 TREE_CHAIN (d) = decls;
1951 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
1952 all_prefix_attrs = chainon (c_parser_attributes (parser),
1955 all_prefix_attrs = prefix_attrs;
1956 if (c_parser_next_token_is (parser, CPP_COMMA))
1957 c_parser_consume_token (parser);
1958 else if (c_parser_next_token_is (parser, CPP_SEMICOLON)
1959 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
1961 /* Semicolon consumed in caller. */
1966 c_parser_error (parser, "expected %<,%>, %<;%> or %<}%>");
1972 c_parser_error (parser,
1973 "expected %<:%>, %<,%>, %<;%>, %<}%> or "
1974 "%<__attribute__%>");
1981 /* Parse a typeof specifier (a GNU extension).
1984 typeof ( expression )
1985 typeof ( type-name )
1988 static struct c_typespec
1989 c_parser_typeof_specifier (c_parser *parser)
1991 struct c_typespec ret;
1992 ret.kind = ctsk_typeof;
1993 ret.spec = error_mark_node;
1994 gcc_assert (c_parser_next_token_is_keyword (parser, RID_TYPEOF));
1995 c_parser_consume_token (parser);
1998 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2004 if (c_parser_next_token_starts_typename (parser))
2006 struct c_type_name *type = c_parser_type_name (parser);
2011 ret.spec = groktypename (type);
2012 pop_maybe_used (variably_modified_type_p (ret.spec, NULL_TREE));
2017 struct c_expr expr = c_parser_expression (parser);
2020 if (TREE_CODE (expr.value) == COMPONENT_REF
2021 && DECL_C_BIT_FIELD (TREE_OPERAND (expr.value, 1)))
2022 error ("%<typeof%> applied to a bit-field");
2023 ret.spec = TREE_TYPE (expr.value);
2024 pop_maybe_used (variably_modified_type_p (ret.spec, NULL_TREE));
2026 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
2030 /* Parse a declarator, possibly an abstract declarator (C90 6.5.4,
2031 6.5.5, C99 6.7.5, 6.7.6). If TYPE_SEEN_P then a typedef name may
2032 be redeclared; otherwise it may not. KIND indicates which kind of
2033 declarator is wanted. Returns a valid declarator except in the
2034 case of a syntax error in which case NULL is returned. *SEEN_ID is
2035 set to true if an identifier being declared is seen; this is used
2036 to diagnose bad forms of abstract array declarators and to
2037 determine whether an identifier list is syntactically permitted.
2040 pointer[opt] direct-declarator
2044 ( attributes[opt] declarator )
2045 direct-declarator array-declarator
2046 direct-declarator ( parameter-type-list )
2047 direct-declarator ( identifier-list[opt] )
2050 * type-qualifier-list[opt]
2051 * type-qualifier-list[opt] pointer
2053 type-qualifier-list:
2056 type-qualifier-list type-qualifier
2057 type-qualifier-list attributes
2059 parameter-type-list:
2061 parameter-list , ...
2064 parameter-declaration
2065 parameter-list , parameter-declaration
2067 parameter-declaration:
2068 declaration-specifiers declarator attributes[opt]
2069 declaration-specifiers abstract-declarator[opt] attributes[opt]
2073 identifier-list , identifier
2075 abstract-declarator:
2077 pointer[opt] direct-abstract-declarator
2079 direct-abstract-declarator:
2080 ( attributes[opt] abstract-declarator )
2081 direct-abstract-declarator[opt] array-declarator
2082 direct-abstract-declarator[opt] ( parameter-type-list[opt] )
2087 direct-declarator ( parameter-forward-declarations
2088 parameter-type-list[opt] )
2090 direct-abstract-declarator:
2091 direct-abstract-declarator[opt] ( parameter-forward-declarations
2092 parameter-type-list[opt] )
2094 parameter-forward-declarations:
2096 parameter-forward-declarations parameter-list ;
2098 The uses of attributes shown above are GNU extensions.
2100 Some forms of array declarator are not included in C99 in the
2101 syntax for abstract declarators; these are disallowed elsewhere.
2102 This may be a defect (DR#289).
2104 This function also accepts an omitted abstract declarator as being
2105 an abstract declarator, although not part of the formal syntax. */
2107 static struct c_declarator *
2108 c_parser_declarator (c_parser *parser, bool type_seen_p, c_dtr_syn kind,
2111 /* Parse any initial pointer part. */
2112 if (c_parser_next_token_is (parser, CPP_MULT))
2114 struct c_declspecs *quals_attrs = build_null_declspecs ();
2115 struct c_declarator *inner;
2116 c_parser_consume_token (parser);
2117 c_parser_declspecs (parser, quals_attrs, false, false, true);
2118 inner = c_parser_declarator (parser, type_seen_p, kind, seen_id);
2122 return make_pointer_declarator (quals_attrs, inner);
2124 /* Now we have a direct declarator, direct abstract declarator or
2125 nothing (which counts as a direct abstract declarator here). */
2126 return c_parser_direct_declarator (parser, type_seen_p, kind, seen_id);
2129 /* Parse a direct declarator or direct abstract declarator; arguments
2130 as c_parser_declarator. */
2132 static struct c_declarator *
2133 c_parser_direct_declarator (c_parser *parser, bool type_seen_p, c_dtr_syn kind,
2136 /* The direct declarator must start with an identifier (possibly
2137 omitted) or a parenthesized declarator (possibly abstract). In
2138 an ordinary declarator, initial parentheses must start a
2139 parenthesized declarator. In an abstract declarator or parameter
2140 declarator, they could start a parenthesized declarator or a
2141 parameter list. To tell which, the open parenthesis and any
2142 following attributes must be read. If a declaration specifier
2143 follows, then it is a parameter list; if the specifier is a
2144 typedef name, there might be an ambiguity about redeclaring it,
2145 which is resolved in the direction of treating it as a typedef
2146 name. If a close parenthesis follows, it is also an empty
2147 parameter list, as the syntax does not permit empty abstract
2148 declarators. Otherwise, it is a parenthesized declarator (in
2149 which case the analysis may be repeated inside it, recursively).
2151 ??? There is an ambiguity in a parameter declaration "int
2152 (__attribute__((foo)) x)", where x is not a typedef name: it
2153 could be an abstract declarator for a function, or declare x with
2154 parentheses. The proper resolution of this ambiguity needs
2155 documenting. At present we follow an accident of the old
2156 parser's implementation, whereby the first parameter must have
2157 some declaration specifiers other than just attributes. Thus as
2158 a parameter declaration it is treated as a parenthesized
2159 parameter named x, and as an abstract declarator it is
2162 ??? Also following the old parser, attributes inside an empty
2163 parameter list are ignored, making it a list not yielding a
2164 prototype, rather than giving an error or making it have one
2165 parameter with implicit type int.
2167 ??? Also following the old parser, typedef names may be
2168 redeclared in declarators, but not Objective-C class names. */
2170 if (kind != C_DTR_ABSTRACT
2171 && c_parser_next_token_is (parser, CPP_NAME)
2173 && c_parser_peek_token (parser)->id_kind == C_ID_TYPENAME)
2174 || c_parser_peek_token (parser)->id_kind == C_ID_ID))
2176 struct c_declarator *inner
2177 = build_id_declarator (c_parser_peek_token (parser)->value);
2179 inner->id_loc = c_parser_peek_token (parser)->location;
2180 c_parser_consume_token (parser);
2181 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
2184 if (kind != C_DTR_NORMAL
2185 && c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
2187 struct c_declarator *inner = build_id_declarator (NULL_TREE);
2188 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
2191 /* Either we are at the end of an abstract declarator, or we have
2194 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
2197 struct c_declarator *inner;
2198 c_parser_consume_token (parser);
2199 attrs = c_parser_attributes (parser);
2200 if (kind != C_DTR_NORMAL
2201 && (c_parser_next_token_starts_declspecs (parser)
2202 || c_parser_next_token_is (parser, CPP_CLOSE_PAREN)))
2204 struct c_arg_info *args
2205 = c_parser_parms_declarator (parser, kind == C_DTR_NORMAL,
2212 = build_function_declarator (args,
2213 build_id_declarator (NULL_TREE));
2214 return c_parser_direct_declarator_inner (parser, *seen_id,
2218 /* A parenthesized declarator. */
2219 inner = c_parser_declarator (parser, type_seen_p, kind, seen_id);
2220 if (inner != NULL && attrs != NULL)
2221 inner = build_attrs_declarator (attrs, inner);
2222 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2224 c_parser_consume_token (parser);
2228 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
2232 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2239 if (kind == C_DTR_NORMAL)
2241 c_parser_error (parser, "expected identifier or %<(%>");
2245 return build_id_declarator (NULL_TREE);
2249 /* Parse part of a direct declarator or direct abstract declarator,
2250 given that some (in INNER) has already been parsed; ID_PRESENT is
2251 true if an identifier is present, false for an abstract
2254 static struct c_declarator *
2255 c_parser_direct_declarator_inner (c_parser *parser, bool id_present,
2256 struct c_declarator *inner)
2258 /* Parse a sequence of array declarators and parameter lists. */
2259 if (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
2261 struct c_declarator *declarator;
2262 struct c_declspecs *quals_attrs = build_null_declspecs ();
2266 c_parser_consume_token (parser);
2267 c_parser_declspecs (parser, quals_attrs, false, false, true);
2268 static_seen = c_parser_next_token_is_keyword (parser, RID_STATIC);
2270 c_parser_consume_token (parser);
2271 if (static_seen && !quals_attrs->declspecs_seen_p)
2272 c_parser_declspecs (parser, quals_attrs, false, false, true);
2273 if (!quals_attrs->declspecs_seen_p)
2275 /* If "static" is present, there must be an array dimension.
2276 Otherwise, there may be a dimension, "*", or no
2281 dimen = c_parser_expr_no_commas (parser, NULL).value;
2285 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
2290 else if (c_parser_next_token_is (parser, CPP_MULT))
2292 if (c_parser_peek_2nd_token (parser)->type == CPP_CLOSE_SQUARE)
2296 c_parser_consume_token (parser);
2301 dimen = c_parser_expr_no_commas (parser, NULL).value;
2307 dimen = c_parser_expr_no_commas (parser, NULL).value;
2310 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
2311 c_parser_consume_token (parser);
2314 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
2318 declarator = build_array_declarator (dimen, quals_attrs, static_seen,
2320 inner = set_array_declarator_inner (declarator, inner, !id_present);
2321 return c_parser_direct_declarator_inner (parser, id_present, inner);
2323 else if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
2326 struct c_arg_info *args;
2327 c_parser_consume_token (parser);
2328 attrs = c_parser_attributes (parser);
2329 args = c_parser_parms_declarator (parser, id_present, attrs);
2334 inner = build_function_declarator (args, inner);
2335 return c_parser_direct_declarator_inner (parser, id_present, inner);
2341 /* Parse a parameter list or identifier list, including the closing
2342 parenthesis but not the opening one. ATTRS are the attributes at
2343 the start of the list. ID_LIST_OK is true if an identifier list is
2344 acceptable; such a list must not have attributes at the start. */
2346 static struct c_arg_info *
2347 c_parser_parms_declarator (c_parser *parser, bool id_list_ok, tree attrs)
2350 declare_parm_level ();
2351 /* If the list starts with an identifier, it is an identifier list.
2352 Otherwise, it is either a prototype list or an empty list. */
2355 && c_parser_next_token_is (parser, CPP_NAME)
2356 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
2358 tree list = NULL_TREE, *nextp = &list;
2359 while (c_parser_next_token_is (parser, CPP_NAME)
2360 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
2362 *nextp = build_tree_list (NULL_TREE,
2363 c_parser_peek_token (parser)->value);
2364 nextp = & TREE_CHAIN (*nextp);
2365 c_parser_consume_token (parser);
2366 if (c_parser_next_token_is_not (parser, CPP_COMMA))
2368 c_parser_consume_token (parser);
2369 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2371 c_parser_error (parser, "expected identifier");
2375 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2377 struct c_arg_info *ret = XOBNEW (&parser_obstack, struct c_arg_info);
2382 c_parser_consume_token (parser);
2388 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2396 struct c_arg_info *ret = c_parser_parms_list_declarator (parser, attrs);
2402 /* Parse a parameter list (possibly empty), including the closing
2403 parenthesis but not the opening one. ATTRS are the attributes at
2404 the start of the list. */
2406 static struct c_arg_info *
2407 c_parser_parms_list_declarator (c_parser *parser, tree attrs)
2409 bool good_parm = false;
2410 /* ??? Following the old parser, forward parameter declarations may
2411 use abstract declarators, and if no real parameter declarations
2412 follow the forward declarations then this is not diagnosed. Also
2413 note as above that attributes are ignored as the only contents of
2414 the parentheses, or as the only contents after forward
2416 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2418 struct c_arg_info *ret = XOBNEW (&parser_obstack, struct c_arg_info);
2423 c_parser_consume_token (parser);
2426 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
2428 struct c_arg_info *ret = XOBNEW (&parser_obstack, struct c_arg_info);
2432 /* Suppress -Wold-style-definition for this case. */
2433 ret->types = error_mark_node;
2434 error ("ISO C requires a named argument before %<...%>");
2435 c_parser_consume_token (parser);
2436 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2438 c_parser_consume_token (parser);
2443 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2448 /* Nonempty list of parameters, either terminated with semicolon
2449 (forward declarations; recurse) or with close parenthesis (normal
2450 function) or with ", ... )" (variadic function). */
2453 /* Parse a parameter. */
2454 struct c_parm *parm = c_parser_parameter_declaration (parser, attrs);
2459 push_parm_decl (parm);
2461 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
2464 c_parser_consume_token (parser);
2465 new_attrs = c_parser_attributes (parser);
2466 return c_parser_parms_list_declarator (parser, new_attrs);
2468 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2470 c_parser_consume_token (parser);
2472 return get_parm_info (false);
2475 struct c_arg_info *ret
2476 = XOBNEW (&parser_obstack, struct c_arg_info);
2484 if (!c_parser_require (parser, CPP_COMMA,
2485 "expected %<;%>, %<,%> or %<)%>"))
2487 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
2490 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
2492 c_parser_consume_token (parser);
2493 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2495 c_parser_consume_token (parser);
2497 return get_parm_info (true);
2500 struct c_arg_info *ret
2501 = XOBNEW (&parser_obstack, struct c_arg_info);
2511 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2519 /* Parse a parameter declaration. ATTRS are the attributes at the
2520 start of the declaration if it is the first parameter. */
2522 static struct c_parm *
2523 c_parser_parameter_declaration (c_parser *parser, tree attrs)
2525 struct c_declspecs *specs;
2526 struct c_declarator *declarator;
2528 tree postfix_attrs = NULL_TREE;
2530 if (!c_parser_next_token_starts_declspecs (parser))
2532 /* ??? In some Objective-C cases '...' isn't applicable so there
2533 should be a different message. */
2534 c_parser_error (parser,
2535 "expected declaration specifiers or %<...%>");
2536 c_parser_skip_to_end_of_parameter (parser);
2539 specs = build_null_declspecs ();
2542 declspecs_add_attrs (specs, attrs);
2545 c_parser_declspecs (parser, specs, true, true, true);
2546 finish_declspecs (specs);
2547 pending_xref_error ();
2548 prefix_attrs = specs->attrs;
2549 specs->attrs = NULL_TREE;
2550 declarator = c_parser_declarator (parser, specs->type_seen_p,
2551 C_DTR_PARM, &dummy);
2552 if (declarator == NULL)
2554 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
2557 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2558 postfix_attrs = c_parser_attributes (parser);
2559 return build_c_parm (specs, chainon (postfix_attrs, prefix_attrs),
2563 /* Parse a string literal in an asm expression. It should not be
2564 translated, and wide string literals are an error although
2565 permitted by the syntax. This is a GNU extension.
2570 ??? At present, following the old parser, the caller needs to have
2571 set c_lex_string_translate to 0. It would be better to follow the
2572 C++ parser rather than using the c_lex_string_translate kludge. */
2575 c_parser_asm_string_literal (c_parser *parser)
2578 if (c_parser_next_token_is (parser, CPP_STRING))
2580 str = c_parser_peek_token (parser)->value;
2581 c_parser_consume_token (parser);
2583 else if (c_parser_next_token_is (parser, CPP_WSTRING))
2585 error ("wide string literal in %<asm%>");
2586 str = build_string (1, "");
2587 c_parser_consume_token (parser);
2591 c_parser_error (parser, "expected string literal");
2597 /* Parse a simple asm expression. This is used in restricted
2598 contexts, where a full expression with inputs and outputs does not
2599 make sense. This is a GNU extension.
2602 asm ( asm-string-literal )
2606 c_parser_simple_asm_expr (c_parser *parser)
2609 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ASM));
2610 /* ??? Follow the C++ parser rather than using the
2611 c_lex_string_translate kludge. */
2612 c_lex_string_translate = 0;
2613 c_parser_consume_token (parser);
2614 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2616 c_lex_string_translate = 1;
2619 str = c_parser_asm_string_literal (parser);
2620 c_lex_string_translate = 1;
2621 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
2623 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
2629 /* Parse (possibly empty) attributes. This is a GNU extension.
2633 attributes attribute
2636 __attribute__ ( ( attribute-list ) )
2640 attribute_list , attrib
2645 any-word ( identifier )
2646 any-word ( identifier , nonempty-expr-list )
2647 any-word ( expr-list )
2649 where the "identifier" must not be declared as a type, and
2650 "any-word" may be any identifier (including one declared as a
2651 type), a reserved word storage class specifier, type specifier or
2652 type qualifier. ??? This still leaves out most reserved keywords
2653 (following the old parser), shouldn't we include them, and why not
2654 allow identifiers declared as types to start the arguments? */
2657 c_parser_attributes (c_parser *parser)
2659 tree attrs = NULL_TREE;
2660 while (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2662 /* ??? Follow the C++ parser rather than using the
2663 c_lex_string_translate kludge. */
2664 c_lex_string_translate = 0;
2665 c_parser_consume_token (parser);
2666 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2668 c_lex_string_translate = 1;
2671 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2673 c_lex_string_translate = 1;
2674 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
2677 /* Parse the attribute list. */
2678 while (c_parser_next_token_is (parser, CPP_COMMA)
2679 || c_parser_next_token_is (parser, CPP_NAME)
2680 || c_parser_next_token_is (parser, CPP_KEYWORD))
2682 tree attr, attr_name, attr_args;
2683 if (c_parser_next_token_is (parser, CPP_COMMA))
2685 c_parser_consume_token (parser);
2688 if (c_parser_next_token_is (parser, CPP_KEYWORD))
2690 /* ??? See comment above about what keywords are
2693 switch (c_parser_peek_token (parser)->keyword)
2725 attr_name = c_parser_peek_token (parser)->value;
2726 c_parser_consume_token (parser);
2727 if (c_parser_next_token_is_not (parser, CPP_OPEN_PAREN))
2729 attr = build_tree_list (attr_name, NULL_TREE);
2730 attrs = chainon (attrs, attr);
2733 c_parser_consume_token (parser);
2734 /* Parse the attribute contents. If they start with an
2735 identifier which is followed by a comma or close
2736 parenthesis, then the arguments start with that
2737 identifier; otherwise they are an expression list. */
2738 if (c_parser_next_token_is (parser, CPP_NAME)
2739 && c_parser_peek_token (parser)->id_kind == C_ID_ID
2740 && ((c_parser_peek_2nd_token (parser)->type == CPP_COMMA)
2741 || (c_parser_peek_2nd_token (parser)->type
2742 == CPP_CLOSE_PAREN)))
2744 tree arg1 = c_parser_peek_token (parser)->value;
2745 c_parser_consume_token (parser);
2746 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2747 attr_args = build_tree_list (NULL_TREE, arg1);
2750 c_parser_consume_token (parser);
2751 attr_args = tree_cons (NULL_TREE, arg1,
2752 c_parser_expr_list (parser));
2757 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2758 attr_args = NULL_TREE;
2760 attr_args = c_parser_expr_list (parser);
2762 attr = build_tree_list (attr_name, attr_args);
2763 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2764 c_parser_consume_token (parser);
2767 c_lex_string_translate = 1;
2768 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2772 attrs = chainon (attrs, attr);
2774 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2775 c_parser_consume_token (parser);
2778 c_lex_string_translate = 1;
2779 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2783 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2784 c_parser_consume_token (parser);
2787 c_lex_string_translate = 1;
2788 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2792 c_lex_string_translate = 1;
2797 /* Parse a type name (C90 6.5.5, C99 6.7.6).
2800 specifier-qualifier-list abstract-declarator[opt]
2803 static struct c_type_name *
2804 c_parser_type_name (c_parser *parser)
2806 struct c_declspecs *specs = build_null_declspecs ();
2807 struct c_declarator *declarator;
2808 struct c_type_name *ret;
2810 c_parser_declspecs (parser, specs, false, true, true);
2811 if (!specs->declspecs_seen_p)
2813 c_parser_error (parser, "expected specifier-qualifier-list");
2816 pending_xref_error ();
2817 finish_declspecs (specs);
2818 declarator = c_parser_declarator (parser, specs->type_seen_p,
2819 C_DTR_ABSTRACT, &dummy);
2820 if (declarator == NULL)
2822 ret = XOBNEW (&parser_obstack, struct c_type_name);
2824 ret->declarator = declarator;
2828 /* Parse an initializer (C90 6.5.7, C99 6.7.8).
2831 assignment-expression
2832 { initializer-list }
2833 { initializer-list , }
2836 designation[opt] initializer
2837 initializer-list , designation[opt] initializer
2844 designator-list designator
2851 [ constant-expression ]
2863 [ constant-expression ... constant-expression ]
2865 Any expression without commas is accepted in the syntax for the
2866 constant-expressions, with non-constant expressions rejected later.
2868 This function is only used for top-level initializers; for nested
2869 ones, see c_parser_initval. */
2871 static struct c_expr
2872 c_parser_initializer (c_parser *parser)
2874 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
2875 return c_parser_braced_init (parser, NULL_TREE, false);
2877 return c_parser_expr_no_commas (parser, NULL);
2880 /* Parse a braced initializer list. TYPE is the type specified for a
2881 compound literal, and NULL_TREE for other initializers and for
2882 nested braced lists. NESTED_P is true for nested braced lists,
2883 false for the list of a compound literal or the list that is the
2884 top-level initializer in a declaration. */
2886 static struct c_expr
2887 c_parser_braced_init (c_parser *parser, tree type, bool nested_p)
2889 gcc_assert (c_parser_next_token_is (parser, CPP_OPEN_BRACE));
2890 c_parser_consume_token (parser);
2892 push_init_level (0);
2894 really_start_incremental_init (type);
2895 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2898 pedwarn ("ISO C forbids empty initializer braces");
2902 /* Parse a non-empty initializer list, possibly with a trailing
2906 c_parser_initelt (parser);
2909 if (c_parser_next_token_is (parser, CPP_COMMA))
2910 c_parser_consume_token (parser);
2913 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2917 if (c_parser_next_token_is_not (parser, CPP_CLOSE_BRACE))
2920 ret.value = error_mark_node;
2921 ret.original_code = ERROR_MARK;
2922 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, "expected %<}%>");
2925 c_parser_consume_token (parser);
2926 return pop_init_level (0);
2929 /* Parse a nested initializer, including designators. */
2932 c_parser_initelt (c_parser *parser)
2934 /* Parse any designator or designator list. A single array
2935 designator may have the subsequent "=" omitted in GNU C, but a
2936 longer list or a structure member designator may not. */
2937 if (c_parser_next_token_is (parser, CPP_NAME)
2938 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
2940 /* Old-style structure member designator. */
2941 set_init_label (c_parser_peek_token (parser)->value);
2943 pedwarn ("obsolete use of designated initializer with %<:%>");
2944 c_parser_consume_token (parser);
2945 c_parser_consume_token (parser);
2949 /* des_seen is 0 if there have been no designators, 1 if there
2950 has been a single array designator and 2 otherwise. */
2952 while (c_parser_next_token_is (parser, CPP_OPEN_SQUARE)
2953 || c_parser_next_token_is (parser, CPP_DOT))
2955 int des_prev = des_seen;
2958 if (c_parser_next_token_is (parser, CPP_DOT))
2961 c_parser_consume_token (parser);
2962 if (c_parser_next_token_is (parser, CPP_NAME))
2964 set_init_label (c_parser_peek_token (parser)->value);
2965 c_parser_consume_token (parser);
2970 init.value = error_mark_node;
2971 init.original_code = ERROR_MARK;
2972 c_parser_error (parser, "expected identifier");
2973 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
2974 process_init_element (init);
2981 /* ??? Following the old parser, [ objc-receiver
2982 objc-message-args ] is accepted as an initializer,
2983 being distinguished from a designator by what follows
2984 the first assignment expression inside the square
2985 brackets, but after a first array designator a
2986 subsequent square bracket is for Objective-C taken to
2987 start an expression, using the obsolete form of
2988 designated initializer without '=', rather than
2989 possibly being a second level of designation: in LALR
2990 terms, the '[' is shifted rather than reducing
2991 designator to designator-list. */
2992 if (des_prev == 1 && c_dialect_objc ())
2994 des_seen = des_prev;
2997 if (des_prev == 0 && c_dialect_objc ())
2999 /* This might be an array designator or an
3000 Objective-C message expression. If the former,
3001 continue parsing here; if the latter, parse the
3002 remainder of the initializer given the starting
3003 primary-expression. ??? It might make sense to
3004 distinguish when des_prev == 1 as well; see
3005 previous comment. */
3007 struct c_expr mexpr;
3008 c_parser_consume_token (parser);
3009 if (c_parser_peek_token (parser)->type == CPP_NAME
3010 && ((c_parser_peek_token (parser)->id_kind
3012 || (c_parser_peek_token (parser)->id_kind
3013 == C_ID_CLASSNAME)))
3015 /* Type name receiver. */
3016 tree id = c_parser_peek_token (parser)->value;
3017 c_parser_consume_token (parser);
3018 rec = objc_get_class_reference (id);
3019 goto parse_message_args;
3021 first = c_parser_expr_no_commas (parser, NULL).value;
3022 if (c_parser_next_token_is (parser, CPP_ELLIPSIS)
3023 || c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
3024 goto array_desig_after_first;
3025 /* Expression receiver. So far only one part
3026 without commas has been parsed; there might be
3027 more of the expression. */
3029 while (c_parser_next_token_is (parser, CPP_COMMA))
3032 c_parser_consume_token (parser);
3033 next = c_parser_expr_no_commas (parser, NULL).value;
3034 rec = build_compound_expr (rec, next);
3037 /* Now parse the objc-message-args. */
3038 args = c_parser_objc_message_args (parser);
3039 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
3042 = objc_build_message_expr (build_tree_list (rec, args));
3043 mexpr.original_code = ERROR_MARK;
3044 /* Now parse and process the remainder of the
3045 initializer, starting with this message
3046 expression as a primary-expression. */
3047 c_parser_initval (parser, &mexpr);
3050 c_parser_consume_token (parser);
3051 first = c_parser_expr_no_commas (parser, NULL).value;
3052 array_desig_after_first:
3053 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
3055 c_parser_consume_token (parser);
3056 second = c_parser_expr_no_commas (parser, NULL).value;
3060 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
3062 c_parser_consume_token (parser);
3063 set_init_index (first, second);
3064 if (pedantic && second)
3065 pedwarn ("ISO C forbids specifying range of "
3066 "elements to initialize");
3069 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
3075 if (c_parser_next_token_is (parser, CPP_EQ))
3077 if (pedantic && !flag_isoc99)
3078 pedwarn ("ISO C90 forbids specifying subobject to initialize");
3079 c_parser_consume_token (parser);
3086 pedwarn ("obsolete use of designated initializer "
3092 init.value = error_mark_node;
3093 init.original_code = ERROR_MARK;
3094 c_parser_error (parser, "expected %<=%>");
3095 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
3096 process_init_element (init);
3102 c_parser_initval (parser, NULL);
3105 /* Parse a nested initializer; as c_parser_initializer but parses
3106 initializers within braced lists, after any designators have been
3107 applied. If AFTER is not NULL then it is an Objective-C message
3108 expression which is the primary-expression starting the
3112 c_parser_initval (c_parser *parser, struct c_expr *after)
3115 gcc_assert (!after || c_dialect_objc ());
3116 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE) && !after)
3117 init = c_parser_braced_init (parser, NULL_TREE, true);
3119 init = c_parser_expr_no_commas (parser, after);
3120 process_init_element (init);
3123 /* Parse a compound statement (possibly a function body) (C90 6.6.2,
3127 { block-item-list[opt] }
3128 { label-declarations block-item-list }
3132 block-item-list block-item
3144 { label-declarations block-item-list }
3147 __extension__ nested-declaration
3148 nested-function-definition
3152 label-declarations label-declaration
3155 __label__ identifier-list ;
3157 Allowing the mixing of declarations and code is new in C99. The
3158 GNU syntax also permits (not shown above) labels at the end of
3159 compound statements, which yield an error. We don't allow labels
3160 on declarations; this might seem like a natural extension, but
3161 there would be a conflict between attributes on the label and
3162 prefix attributes on the declaration. ??? The syntax follows the
3163 old parser in requiring something after label declarations.
3164 Although they are erroneous if the labels declared aren't defined,
3165 is it useful for the syntax to be this way? */
3168 c_parser_compound_statement (c_parser *parser)
3171 if (!c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
3172 return error_mark_node;
3173 stmt = c_begin_compound_stmt (true);
3174 c_parser_compound_statement_nostart (parser);
3175 return c_end_compound_stmt (stmt, true);
3178 /* Parse a compound statement except for the opening brace. This is
3179 used for parsing both compound statements and statement expressions
3180 (which follow different paths to handling the opening). */
3183 c_parser_compound_statement_nostart (c_parser *parser)
3185 bool last_stmt = false;
3186 bool last_label = false;
3187 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3189 c_parser_consume_token (parser);
3192 if (c_parser_next_token_is_keyword (parser, RID_LABEL))
3194 /* Read zero or more forward-declarations for labels that nested
3195 functions can jump to. */
3196 while (c_parser_next_token_is_keyword (parser, RID_LABEL))
3198 c_parser_consume_token (parser);
3199 /* Any identifiers, including those declared as type names,
3204 if (c_parser_next_token_is_not (parser, CPP_NAME))
3206 c_parser_error (parser, "expected identifier");
3210 = declare_label (c_parser_peek_token (parser)->value);
3211 C_DECLARED_LABEL_FLAG (label) = 1;
3212 add_stmt (build_stmt (DECL_EXPR, label));
3213 c_parser_consume_token (parser);
3214 if (c_parser_next_token_is (parser, CPP_COMMA))
3215 c_parser_consume_token (parser);
3219 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
3221 /* ??? Locating this diagnostic on the token after the
3222 declarations end follows the old parser, but it might be
3223 better to locate it where the declarations start instead. */
3225 pedwarn ("ISO C forbids label declarations");
3227 /* We must now have at least one statement, label or declaration. */
3228 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3230 c_parser_error (parser, "expected declaration or statement");
3231 c_parser_consume_token (parser);
3234 while (c_parser_next_token_is_not (parser, CPP_CLOSE_BRACE))
3236 location_t loc = c_parser_peek_token (parser)->location;
3237 if (c_parser_next_token_is (parser, CPP_EOF))
3239 c_parser_error (parser, "expected declaration or statement");
3242 if (c_parser_next_token_is_keyword (parser, RID_CASE)
3243 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
3244 || (c_parser_next_token_is (parser, CPP_NAME)
3245 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
3249 c_parser_label (parser);
3251 else if (!last_label
3252 && c_parser_next_token_starts_declspecs (parser))
3255 c_parser_declaration_or_fndef (parser, true, true, true, true);
3257 && ((pedantic && !flag_isoc99)
3258 || warn_declaration_after_statement))
3259 pedwarn_c90 ("%HISO C90 forbids mixed declarations and code",
3263 else if (!last_label
3264 && c_parser_next_token_is_keyword (parser, RID_EXTENSION))
3266 /* __extension__ can start a declaration, but is also an
3267 unary operator that can start an expression. Consume all
3268 but the last of a possible series of __extension__ to
3270 while (c_parser_peek_2nd_token (parser)->type == CPP_KEYWORD
3271 && (c_parser_peek_2nd_token (parser)->keyword
3273 c_parser_consume_token (parser);
3274 if (c_token_starts_declspecs (c_parser_peek_2nd_token (parser)))
3277 ext = disable_extension_diagnostics ();
3278 c_parser_consume_token (parser);
3280 c_parser_declaration_or_fndef (parser, true, true, true, true);
3281 /* Following the old parser, __extension__ does not
3282 disable this diagnostic. */
3283 restore_extension_diagnostics (ext);
3285 && ((pedantic && !flag_isoc99)
3286 || warn_declaration_after_statement))
3287 pedwarn_c90 ("%HISO C90 forbids mixed declarations and code",
3299 c_parser_statement_after_labels (parser);
3303 error ("label at end of compound statement");
3304 c_parser_consume_token (parser);
3307 /* Parse a label (C90 6.6.1, C99 6.8.1).
3310 identifier : attributes[opt]
3311 case constant-expression :
3317 case constant-expression ... constant-expression :
3319 The use of attributes on labels is a GNU extension. The syntax in
3320 GNU C accepts any expressions without commas, non-constant
3321 expressions being rejected later. */
3324 c_parser_label (c_parser *parser)
3326 location_t loc1 = c_parser_peek_token (parser)->location;
3327 tree label = NULL_TREE;
3328 if (c_parser_next_token_is_keyword (parser, RID_CASE))
3331 c_parser_consume_token (parser);
3332 exp1 = c_parser_expr_no_commas (parser, NULL).value;
3333 if (c_parser_next_token_is (parser, CPP_COLON))
3335 c_parser_consume_token (parser);
3336 label = do_case (exp1, NULL_TREE);
3338 else if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
3340 c_parser_consume_token (parser);
3341 exp2 = c_parser_expr_no_commas (parser, NULL).value;
3342 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
3343 label = do_case (exp1, exp2);
3346 c_parser_error (parser, "expected %<:%> or %<...%>");
3348 else if (c_parser_next_token_is_keyword (parser, RID_DEFAULT))
3350 c_parser_consume_token (parser);
3351 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
3352 label = do_case (NULL_TREE, NULL_TREE);
3356 tree name = c_parser_peek_token (parser)->value;
3360 gcc_assert (c_parser_next_token_is (parser, CPP_NAME));
3361 c_parser_consume_token (parser);
3362 gcc_assert (c_parser_next_token_is (parser, CPP_COLON));
3363 loc2 = c_parser_peek_token (parser)->location;
3364 c_parser_consume_token (parser);
3365 attrs = c_parser_attributes (parser);
3366 tlab = define_label (loc2, name);
3369 decl_attributes (&tlab, attrs, 0);
3370 label = add_stmt (build_stmt (LABEL_EXPR, tlab));
3374 SET_EXPR_LOCATION (label, loc1);
3377 /* Parse a statement (C90 6.6, C99 6.8).
3382 expression-statement
3390 expression-statement:
3393 selection-statement:
3397 iteration-statement:
3406 return expression[opt] ;
3419 objc-throw-statement
3420 objc-try-catch-statement
3421 objc-synchronized-statement
3423 objc-throw-statement:
3429 c_parser_statement (c_parser *parser)
3431 while (c_parser_next_token_is_keyword (parser, RID_CASE)
3432 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
3433 || (c_parser_next_token_is (parser, CPP_NAME)
3434 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
3435 c_parser_label (parser);
3436 c_parser_statement_after_labels (parser);
3439 /* Parse a statement, other than a labeled statement. */
3442 c_parser_statement_after_labels (c_parser *parser)
3444 location_t loc = c_parser_peek_token (parser)->location;
3445 tree stmt = NULL_TREE;
3446 switch (c_parser_peek_token (parser)->type)
3448 case CPP_OPEN_BRACE:
3449 add_stmt (c_parser_compound_statement (parser));
3452 switch (c_parser_peek_token (parser)->keyword)
3455 c_parser_if_statement (parser);
3458 c_parser_switch_statement (parser);
3461 c_parser_while_statement (parser);
3464 c_parser_do_statement (parser);
3467 c_parser_for_statement (parser);
3470 c_parser_consume_token (parser);
3471 if (c_parser_next_token_is (parser, CPP_NAME))
3473 stmt = c_finish_goto_label (c_parser_peek_token (parser)->value);
3474 c_parser_consume_token (parser);
3476 else if (c_parser_next_token_is (parser, CPP_MULT))
3478 c_parser_consume_token (parser);
3479 stmt = c_finish_goto_ptr (c_parser_expression (parser).value);
3482 c_parser_error (parser, "expected identifier or %<*%>");
3483 goto expect_semicolon;
3485 c_parser_consume_token (parser);
3486 stmt = c_finish_bc_stmt (&c_cont_label, false);
3487 goto expect_semicolon;
3489 c_parser_consume_token (parser);
3490 stmt = c_finish_bc_stmt (&c_break_label, true);
3491 goto expect_semicolon;
3493 c_parser_consume_token (parser);
3494 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
3496 stmt = c_finish_return (NULL_TREE);
3497 c_parser_consume_token (parser);
3501 stmt = c_finish_return (c_parser_expression (parser).value);
3502 goto expect_semicolon;
3506 stmt = c_parser_asm_statement (parser);
3509 gcc_assert (c_dialect_objc ());
3510 c_parser_consume_token (parser);
3511 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
3513 stmt = objc_build_throw_stmt (NULL_TREE);
3514 c_parser_consume_token (parser);
3519 = objc_build_throw_stmt (c_parser_expression (parser).value);
3520 goto expect_semicolon;
3524 gcc_assert (c_dialect_objc ());
3525 c_parser_objc_try_catch_statement (parser);
3527 case RID_AT_SYNCHRONIZED:
3528 gcc_assert (c_dialect_objc ());
3529 c_parser_objc_synchronized_statement (parser);
3536 c_parser_consume_token (parser);
3538 case CPP_CLOSE_PAREN:
3539 case CPP_CLOSE_SQUARE:
3540 /* Avoid infinite loop in error recovery:
3541 c_parser_skip_until_found stops at a closing nesting
3542 delimiter without consuming it, but here we need to consume
3543 it to proceed further. */
3544 c_parser_error (parser, "expected statement");
3545 c_parser_consume_token (parser);
3549 stmt = c_finish_expr_stmt (c_parser_expression (parser).value);
3551 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
3554 /* Two cases cannot and do not have line numbers associated: If stmt
3555 is degenerate, such as "2;", then stmt is an INTEGER_CST, which
3556 cannot hold line numbers. But that's OK because the statement
3557 will either be changed to a MODIFY_EXPR during gimplification of
3558 the statement expr, or discarded. If stmt was compound, but
3559 without new variables, we will have skipped the creation of a
3560 BIND and will have a bare STATEMENT_LIST. But that's OK because
3561 (recursively) all of the component statements should already have
3562 line numbers assigned. ??? Can we discard no-op statements
3564 if (stmt && EXPR_P (stmt))
3565 SET_EXPR_LOCATION (stmt, loc);
3568 /* Parse a parenthesized condition from an if, do or while statement.
3574 c_parser_paren_condition (c_parser *parser)
3578 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3579 return error_mark_node;
3580 loc = c_parser_peek_token (parser)->location;
3581 cond = c_objc_common_truthvalue_conversion
3582 (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 = c_objc_common_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 (0, "%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 = c_objc_common_truthvalue_conversion (exp1.value);
4154 skip_evaluation += cond.value == truthvalue_true_node;
4159 = c_objc_common_truthvalue_conversion
4160 (default_conversion (cond.value));
4161 skip_evaluation += cond.value == truthvalue_false_node;
4162 exp1 = c_parser_expression (parser);
4163 skip_evaluation += ((cond.value == truthvalue_true_node)
4164 - (cond.value == truthvalue_false_node));
4166 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
4168 skip_evaluation -= cond.value == truthvalue_true_node;
4169 ret.value = error_mark_node;
4170 ret.original_code = ERROR_MARK;
4173 exp2 = c_parser_conditional_expression (parser, NULL);
4174 skip_evaluation -= cond.value == truthvalue_true_node;
4175 ret.value = build_conditional_expr (cond.value, exp1.value, exp2.value);
4176 ret.original_code = ERROR_MARK;
4180 /* Parse a binary expression; that is, a logical-OR-expression (C90
4181 6.3.5-6.3.14, C99 6.5.5-6.5.14). If AFTER is not NULL then it is
4182 an Objective-C message expression which is the primary-expression
4183 starting the expression as an initializer.
4185 multiplicative-expression:
4187 multiplicative-expression * cast-expression
4188 multiplicative-expression / cast-expression
4189 multiplicative-expression % cast-expression
4191 additive-expression:
4192 multiplicative-expression
4193 additive-expression + multiplicative-expression
4194 additive-expression - multiplicative-expression
4198 shift-expression << additive-expression
4199 shift-expression >> additive-expression
4201 relational-expression:
4203 relational-expression < shift-expression
4204 relational-expression > shift-expression
4205 relational-expression <= shift-expression
4206 relational-expression >= shift-expression
4208 equality-expression:
4209 relational-expression
4210 equality-expression == relational-expression
4211 equality-expression != relational-expression
4215 AND-expression & equality-expression
4217 exclusive-OR-expression:
4219 exclusive-OR-expression ^ AND-expression
4221 inclusive-OR-expression:
4222 exclusive-OR-expression
4223 inclusive-OR-expression | exclusive-OR-expression
4225 logical-AND-expression:
4226 inclusive-OR-expression
4227 logical-AND-expression && inclusive-OR-expression
4229 logical-OR-expression:
4230 logical-AND-expression
4231 logical-OR-expression || logical-AND-expression
4234 static struct c_expr
4235 c_parser_binary_expression (c_parser *parser, struct c_expr *after)
4237 /* A binary expression is parsed using operator-precedence parsing,
4238 with the operands being cast expressions. All the binary
4239 operators are left-associative. Thus a binary expression is of
4242 E0 op1 E1 op2 E2 ...
4244 which we represent on a stack. On the stack, the precedence
4245 levels are strictly increasing. When a new operator is
4246 encountered of higher precedence than that at the top of the
4247 stack, it is pushed; its LHS is the top expression, and its RHS
4248 is everything parsed until it is popped. When a new operator is
4249 encountered with precedence less than or equal to that at the top
4250 of the stack, triples E[i-1] op[i] E[i] are popped and replaced
4251 by the result of the operation until the operator at the top of
4252 the stack has lower precedence than the new operator or there is
4253 only one element on the stack; then the top expression is the LHS
4254 of the new operator. In the case of logical AND and OR
4255 expressions, we also need to adjust skip_evaluation as
4256 appropriate when the operators are pushed and popped. */
4258 /* The precedence levels, where 0 is a dummy lowest level used for
4259 the bottom of the stack. */
4275 /* The expression at this stack level. */
4277 /* The precedence of the operator on its left, PREC_NONE at the
4278 bottom of the stack. */
4280 /* The operation on its left. */
4286 switch (stack[sp].op) \
4288 case TRUTH_ANDIF_EXPR: \
4289 skip_evaluation -= stack[sp - 1].expr.value == truthvalue_false_node; \
4291 case TRUTH_ORIF_EXPR: \
4292 skip_evaluation -= stack[sp - 1].expr.value == truthvalue_true_node; \
4297 stack[sp - 1].expr = parser_build_binary_op (stack[sp].op, \
4298 stack[sp - 1].expr, \
4302 gcc_assert (!after || c_dialect_objc ());
4303 stack[0].expr = c_parser_cast_expression (parser, after);
4304 stack[0].prec = PREC_NONE;
4309 enum tree_code ocode;
4312 switch (c_parser_peek_token (parser)->type)
4320 ocode = TRUNC_DIV_EXPR;
4324 ocode = TRUNC_MOD_EXPR;
4336 ocode = LSHIFT_EXPR;
4340 ocode = RSHIFT_EXPR;
4354 case CPP_GREATER_EQ:
4367 oprec = PREC_BITAND;
4368 ocode = BIT_AND_EXPR;
4371 oprec = PREC_BITXOR;
4372 ocode = BIT_XOR_EXPR;
4376 ocode = BIT_IOR_EXPR;
4379 oprec = PREC_LOGAND;
4380 ocode = TRUTH_ANDIF_EXPR;
4384 ocode = TRUTH_ORIF_EXPR;
4387 /* Not a binary operator, so end of the binary
4391 c_parser_consume_token (parser);
4392 while (oprec <= stack[sp].prec)
4396 case TRUTH_ANDIF_EXPR:
4397 stack[sp].expr.value = c_objc_common_truthvalue_conversion
4398 (default_conversion (stack[sp].expr.value));
4399 skip_evaluation += stack[sp].expr.value == truthvalue_false_node;
4401 case TRUTH_ORIF_EXPR:
4402 stack[sp].expr.value = c_objc_common_truthvalue_conversion
4403 (default_conversion (stack[sp].expr.value));
4404 skip_evaluation += stack[sp].expr.value == truthvalue_true_node;
4410 stack[sp].expr = c_parser_cast_expression (parser, NULL);
4411 stack[sp].prec = oprec;
4412 stack[sp].op = ocode;
4417 return stack[0].expr;
4421 /* Parse a cast expression (C90 6.3.4, C99 6.5.4). If AFTER is not
4422 NULL then it is an Objective-C message expression which is the
4423 primary-expression starting the expression as an initializer.
4427 ( type-name ) unary-expression
4430 static struct c_expr
4431 c_parser_cast_expression (c_parser *parser, struct c_expr *after)
4433 gcc_assert (!after || c_dialect_objc ());
4435 return c_parser_postfix_expression_after_primary (parser, *after);
4436 /* If the expression begins with a parenthesized type name, it may
4437 be either a cast or a compound literal; we need to see whether
4438 the next character is '{' to tell the difference. If not, it is
4439 an unary expression. */
4440 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
4441 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
4443 struct c_type_name *type_name;
4446 c_parser_consume_token (parser);
4447 type_name = c_parser_type_name (parser);
4448 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
4449 if (type_name == NULL)
4451 ret.value = error_mark_node;
4452 ret.original_code = ERROR_MARK;
4455 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
4456 return c_parser_postfix_expression_after_paren_type (parser,
4458 expr = c_parser_cast_expression (parser, NULL).value;
4459 ret.value = c_cast_expr (type_name, expr);
4460 ret.original_code = ERROR_MARK;
4464 return c_parser_unary_expression (parser);
4467 /* Parse an unary expression (C90 6.3.3, C99 6.5.3).
4473 unary-operator cast-expression
4474 sizeof unary-expression
4475 sizeof ( type-name )
4477 unary-operator: one of
4483 __alignof__ unary-expression
4484 __alignof__ ( type-name )
4487 unary-operator: one of
4488 __extension__ __real__ __imag__
4490 In addition, the GNU syntax treats ++ and -- as unary operators, so
4491 they may be applied to cast expressions with errors for non-lvalues
4494 static struct c_expr
4495 c_parser_unary_expression (c_parser *parser)
4499 switch (c_parser_peek_token (parser)->type)
4502 c_parser_consume_token (parser);
4503 return parser_build_unary_op (PREINCREMENT_EXPR,
4504 c_parser_cast_expression (parser, NULL));
4505 case CPP_MINUS_MINUS:
4506 c_parser_consume_token (parser);
4507 return parser_build_unary_op (PREDECREMENT_EXPR,
4508 c_parser_cast_expression (parser, NULL));
4510 c_parser_consume_token (parser);
4511 return parser_build_unary_op (ADDR_EXPR,
4512 c_parser_cast_expression (parser, NULL));
4514 c_parser_consume_token (parser);
4516 = build_indirect_ref (c_parser_cast_expression (parser, NULL).value,
4518 ret.original_code = ERROR_MARK;
4521 c_parser_consume_token (parser);
4522 if (!c_dialect_objc () && !in_system_header)
4523 warning (OPT_Wtraditional,
4524 "traditional C rejects the unary plus operator");
4525 return parser_build_unary_op (CONVERT_EXPR,
4526 c_parser_cast_expression (parser, NULL));
4528 c_parser_consume_token (parser);
4529 return parser_build_unary_op (NEGATE_EXPR,
4530 c_parser_cast_expression (parser, NULL));
4532 c_parser_consume_token (parser);
4533 return parser_build_unary_op (BIT_NOT_EXPR,
4534 c_parser_cast_expression (parser, NULL));
4536 c_parser_consume_token (parser);
4537 return parser_build_unary_op (TRUTH_NOT_EXPR,
4538 c_parser_cast_expression (parser, NULL));
4540 /* Refer to the address of a label as a pointer. */
4541 c_parser_consume_token (parser);
4542 if (c_parser_next_token_is (parser, CPP_NAME))
4544 ret.value = finish_label_address_expr
4545 (c_parser_peek_token (parser)->value);
4546 c_parser_consume_token (parser);
4550 c_parser_error (parser, "expected identifier");
4551 ret.value = error_mark_node;
4553 ret.original_code = ERROR_MARK;
4556 switch (c_parser_peek_token (parser)->keyword)
4559 return c_parser_sizeof_expression (parser);
4561 return c_parser_alignof_expression (parser);
4563 c_parser_consume_token (parser);
4564 ext = disable_extension_diagnostics ();
4565 ret = c_parser_cast_expression (parser, NULL);
4566 restore_extension_diagnostics (ext);
4569 c_parser_consume_token (parser);
4570 return parser_build_unary_op (REALPART_EXPR,
4571 c_parser_cast_expression (parser,
4574 c_parser_consume_token (parser);
4575 return parser_build_unary_op (IMAGPART_EXPR,
4576 c_parser_cast_expression (parser,
4579 return c_parser_postfix_expression (parser);
4582 return c_parser_postfix_expression (parser);
4586 /* Parse a sizeof expression. */
4588 static struct c_expr
4589 c_parser_sizeof_expression (c_parser *parser)
4592 gcc_assert (c_parser_next_token_is_keyword (parser, RID_SIZEOF));
4593 c_parser_consume_token (parser);
4596 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
4597 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
4599 /* Either sizeof ( type-name ) or sizeof unary-expression
4600 starting with a compound literal. */
4601 struct c_type_name *type_name;
4602 c_parser_consume_token (parser);
4603 type_name = c_parser_type_name (parser);
4604 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
4605 if (type_name == NULL)
4610 ret.value = error_mark_node;
4611 ret.original_code = ERROR_MARK;
4614 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
4616 expr = c_parser_postfix_expression_after_paren_type (parser,
4620 /* sizeof ( type-name ). */
4623 return c_expr_sizeof_type (type_name);
4627 expr = c_parser_unary_expression (parser);
4631 if (TREE_CODE (expr.value) == COMPONENT_REF
4632 && DECL_C_BIT_FIELD (TREE_OPERAND (expr.value, 1)))
4633 error ("%<sizeof%> applied to a bit-field");
4634 return c_expr_sizeof_expr (expr);
4638 /* Parse an alignof expression. */
4640 static struct c_expr
4641 c_parser_alignof_expression (c_parser *parser)
4644 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ALIGNOF));
4645 c_parser_consume_token (parser);
4648 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
4649 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
4651 /* Either __alignof__ ( type-name ) or __alignof__
4652 unary-expression starting with a compound literal. */
4653 struct c_type_name *type_name;
4655 c_parser_consume_token (parser);
4656 type_name = c_parser_type_name (parser);
4657 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
4658 if (type_name == NULL)
4663 ret.value = error_mark_node;
4664 ret.original_code = ERROR_MARK;
4667 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
4669 expr = c_parser_postfix_expression_after_paren_type (parser,
4673 /* alignof ( type-name ). */
4676 ret.value = c_alignof (groktypename (type_name));
4677 ret.original_code = ERROR_MARK;
4683 expr = c_parser_unary_expression (parser);
4687 ret.value = c_alignof_expr (expr.value);
4688 ret.original_code = ERROR_MARK;
4693 /* Parse a postfix expression (C90 6.3.1-6.3.2, C99 6.5.1-6.5.2).
4697 postfix-expression [ expression ]
4698 postfix-expression ( argument-expression-list[opt] )
4699 postfix-expression . identifier
4700 postfix-expression -> identifier
4701 postfix-expression ++
4702 postfix-expression --
4703 ( type-name ) { initializer-list }
4704 ( type-name ) { initializer-list , }
4706 argument-expression-list:
4708 argument-expression-list , argument-expression
4720 (treated as a keyword in GNU C)
4723 ( compound-statement )
4724 __builtin_va_arg ( assignment-expression , type-name )
4725 __builtin_offsetof ( type-name , offsetof-member-designator )
4726 __builtin_choose_expr ( assignment-expression ,
4727 assignment-expression ,
4728 assignment-expression )
4729 __builtin_types_compatible_p ( type-name , type-name )
4731 offsetof-member-designator:
4733 offsetof-member-designator . identifier
4734 offsetof-member-designator [ expression ]
4739 [ objc-receiver objc-message-args ]
4740 @selector ( objc-selector-arg )
4741 @protocol ( identifier )
4742 @encode ( type-name )
4746 static struct c_expr
4747 c_parser_postfix_expression (c_parser *parser)
4749 struct c_expr expr, e1, e2, e3;
4750 struct c_type_name *t1, *t2;
4751 switch (c_parser_peek_token (parser)->type)
4756 expr.value = c_parser_peek_token (parser)->value;
4757 expr.original_code = ERROR_MARK;
4758 c_parser_consume_token (parser);
4762 expr.value = c_parser_peek_token (parser)->value;
4763 expr.original_code = STRING_CST;
4764 c_parser_consume_token (parser);
4766 case CPP_OBJC_STRING:
4767 gcc_assert (c_dialect_objc ());
4769 = objc_build_string_object (c_parser_peek_token (parser)->value);
4770 expr.original_code = ERROR_MARK;
4771 c_parser_consume_token (parser);
4774 if (c_parser_peek_token (parser)->id_kind != C_ID_ID)
4776 c_parser_error (parser, "expected expression");
4777 expr.value = error_mark_node;
4778 expr.original_code = ERROR_MARK;
4782 tree id = c_parser_peek_token (parser)->value;
4783 location_t loc = c_parser_peek_token (parser)->location;
4784 c_parser_consume_token (parser);
4785 expr.value = build_external_ref (id,
4786 (c_parser_peek_token (parser)->type
4787 == CPP_OPEN_PAREN), loc);
4788 expr.original_code = ERROR_MARK;
4791 case CPP_OPEN_PAREN:
4792 /* A parenthesized expression, statement expression or compound
4794 if (c_parser_peek_2nd_token (parser)->type == CPP_OPEN_BRACE)
4796 /* A statement expression. */
4798 c_parser_consume_token (parser);
4799 c_parser_consume_token (parser);
4800 if (cur_stmt_list == NULL)
4802 error ("braced-group within expression allowed "
4803 "only inside a function");
4804 parser->error = true;
4805 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
4806 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
4807 expr.value = error_mark_node;
4808 expr.original_code = ERROR_MARK;
4811 stmt = c_begin_stmt_expr ();
4812 c_parser_compound_statement_nostart (parser);
4813 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
4816 pedwarn ("ISO C forbids braced-groups within expressions");
4817 expr.value = c_finish_stmt_expr (stmt);
4818 expr.original_code = ERROR_MARK;
4820 else if (c_token_starts_typename (c_parser_peek_2nd_token (parser)))
4822 /* A compound literal. ??? Can we actually get here rather
4823 than going directly to
4824 c_parser_postfix_expression_after_paren_type from
4826 struct c_type_name *type_name;
4827 c_parser_consume_token (parser);
4828 type_name = c_parser_type_name (parser);
4829 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
4831 if (type_name == NULL)
4833 expr.value = error_mark_node;
4834 expr.original_code = ERROR_MARK;
4837 expr = c_parser_postfix_expression_after_paren_type (parser,
4842 /* A parenthesized expression. */
4843 c_parser_consume_token (parser);
4844 expr = c_parser_expression (parser);
4845 if (TREE_CODE (expr.value) == MODIFY_EXPR)
4846 TREE_NO_WARNING (expr.value) = 1;
4847 expr.original_code = ERROR_MARK;
4848 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
4853 switch (c_parser_peek_token (parser)->keyword)
4855 case RID_FUNCTION_NAME:
4856 case RID_PRETTY_FUNCTION_NAME:
4857 case RID_C99_FUNCTION_NAME:
4858 expr.value = fname_decl (c_parser_peek_token (parser)->keyword,
4859 c_parser_peek_token (parser)->value);
4860 expr.original_code = ERROR_MARK;
4861 c_parser_consume_token (parser);
4864 c_parser_consume_token (parser);
4865 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4867 expr.value = error_mark_node;
4868 expr.original_code = ERROR_MARK;
4871 e1 = c_parser_expr_no_commas (parser, NULL);
4872 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
4874 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
4875 expr.value = error_mark_node;
4876 expr.original_code = ERROR_MARK;
4879 t1 = c_parser_type_name (parser);
4880 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
4884 expr.value = error_mark_node;
4885 expr.original_code = ERROR_MARK;
4889 expr.value = build_va_arg (e1.value, groktypename (t1));
4890 expr.original_code = ERROR_MARK;
4894 c_parser_consume_token (parser);
4895 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4897 expr.value = error_mark_node;
4898 expr.original_code = ERROR_MARK;
4901 t1 = c_parser_type_name (parser);
4904 expr.value = error_mark_node;
4905 expr.original_code = ERROR_MARK;
4908 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
4910 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
4911 expr.value = error_mark_node;
4912 expr.original_code = ERROR_MARK;
4916 tree type = groktypename (t1);
4918 if (type == error_mark_node)
4919 offsetof_ref = error_mark_node;
4921 offsetof_ref = build1 (INDIRECT_REF, type, NULL);
4922 /* Parse the second argument to __builtin_offsetof. We
4923 must have one identifier, and beyond that we want to
4924 accept sub structure and sub array references. */
4925 if (c_parser_next_token_is (parser, CPP_NAME))
4927 offsetof_ref = build_component_ref
4928 (offsetof_ref, c_parser_peek_token (parser)->value);
4929 c_parser_consume_token (parser);
4930 while (c_parser_next_token_is (parser, CPP_DOT)
4931 || c_parser_next_token_is (parser,
4934 if (c_parser_next_token_is (parser, CPP_DOT))
4936 c_parser_consume_token (parser);
4937 if (c_parser_next_token_is_not (parser,
4940 c_parser_error (parser, "expected identifier");
4943 offsetof_ref = build_component_ref
4945 c_parser_peek_token (parser)->value);
4946 c_parser_consume_token (parser);
4951 c_parser_consume_token (parser);
4952 idx = c_parser_expression (parser).value;
4953 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
4955 offsetof_ref = build_array_ref (offsetof_ref, idx);
4960 c_parser_error (parser, "expected identifier");
4961 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
4963 expr.value = fold_offsetof (offsetof_ref);
4964 expr.original_code = ERROR_MARK;
4967 case RID_CHOOSE_EXPR:
4968 c_parser_consume_token (parser);
4969 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4971 expr.value = error_mark_node;
4972 expr.original_code = ERROR_MARK;
4975 e1 = c_parser_expr_no_commas (parser, NULL);
4976 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
4978 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
4979 expr.value = error_mark_node;
4980 expr.original_code = ERROR_MARK;
4983 e2 = c_parser_expr_no_commas (parser, NULL);
4984 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
4986 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
4987 expr.value = error_mark_node;
4988 expr.original_code = ERROR_MARK;
4991 e3 = c_parser_expr_no_commas (parser, NULL);
4992 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
4997 c = fold (e1.value);
4998 if (TREE_CODE (c) != INTEGER_CST)
4999 error ("first argument to %<__builtin_choose_expr%> not"
5001 expr = integer_zerop (c) ? e3 : e2;
5004 case RID_TYPES_COMPATIBLE_P:
5005 c_parser_consume_token (parser);
5006 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5008 expr.value = error_mark_node;
5009 expr.original_code = ERROR_MARK;
5012 t1 = c_parser_type_name (parser);
5015 expr.value = error_mark_node;
5016 expr.original_code = ERROR_MARK;
5019 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
5021 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5022 expr.value = error_mark_node;
5023 expr.original_code = ERROR_MARK;
5026 t2 = c_parser_type_name (parser);
5029 expr.value = error_mark_node;
5030 expr.original_code = ERROR_MARK;
5033 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5038 e1 = TYPE_MAIN_VARIANT (groktypename (t1));
5039 e2 = TYPE_MAIN_VARIANT (groktypename (t2));
5041 expr.value = comptypes (e1, e2)
5042 ? build_int_cst (NULL_TREE, 1)
5043 : build_int_cst (NULL_TREE, 0);
5044 expr.original_code = ERROR_MARK;
5047 case RID_AT_SELECTOR:
5048 gcc_assert (c_dialect_objc ());
5049 c_parser_consume_token (parser);
5050 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5052 expr.value = error_mark_node;
5053 expr.original_code = ERROR_MARK;
5057 tree sel = c_parser_objc_selector_arg (parser);
5058 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5060 expr.value = objc_build_selector_expr (sel);
5061 expr.original_code = ERROR_MARK;
5064 case RID_AT_PROTOCOL:
5065 gcc_assert (c_dialect_objc ());
5066 c_parser_consume_token (parser);
5067 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5069 expr.value = error_mark_node;
5070 expr.original_code = ERROR_MARK;
5073 if (c_parser_next_token_is_not (parser, CPP_NAME))
5075 c_parser_error (parser, "expected identifier");
5076 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5077 expr.value = error_mark_node;
5078 expr.original_code = ERROR_MARK;
5082 tree id = c_parser_peek_token (parser)->value;
5083 c_parser_consume_token (parser);
5084 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5086 expr.value = objc_build_protocol_expr (id);
5087 expr.original_code = ERROR_MARK;
5091 /* Extension to support C-structures in the archiver. */
5092 gcc_assert (c_dialect_objc ());
5093 c_parser_consume_token (parser);
5094 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5096 expr.value = error_mark_node;
5097 expr.original_code = ERROR_MARK;
5100 t1 = c_parser_type_name (parser);
5103 expr.value = error_mark_node;
5104 expr.original_code = ERROR_MARK;
5105 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5108 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5111 tree type = groktypename (t1);
5112 expr.value = objc_build_encode_expr (type);
5113 expr.original_code = ERROR_MARK;
5117 c_parser_error (parser, "expected expression");
5118 expr.value = error_mark_node;
5119 expr.original_code = ERROR_MARK;
5123 case CPP_OPEN_SQUARE:
5124 if (c_dialect_objc ())
5126 tree receiver, args;
5127 c_parser_consume_token (parser);
5128 receiver = c_parser_objc_receiver (parser);
5129 args = c_parser_objc_message_args (parser);
5130 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
5132 expr.value = objc_build_message_expr (build_tree_list (receiver,
5134 expr.original_code = ERROR_MARK;
5137 /* Else fall through to report error. */
5139 c_parser_error (parser, "expected expression");
5140 expr.value = error_mark_node;
5141 expr.original_code = ERROR_MARK;
5144 return c_parser_postfix_expression_after_primary (parser, expr);
5147 /* Parse a postfix expression after a parenthesized type name: the
5148 brace-enclosed initializer of a compound literal, possibly followed
5149 by some postfix operators. This is separate because it is not
5150 possible to tell until after the type name whether a cast
5151 expression has a cast or a compound literal, or whether the operand
5152 of sizeof is a parenthesized type name or starts with a compound
5155 static struct c_expr
5156 c_parser_postfix_expression_after_paren_type (c_parser *parser,
5157 struct c_type_name *type_name)
5162 start_init (NULL_TREE, NULL, 0);
5163 type = groktypename (type_name);
5164 if (C_TYPE_VARIABLE_SIZE (type))
5166 error ("compound literal has variable size");
5167 type = error_mark_node;
5169 init = c_parser_braced_init (parser, type, false);
5171 maybe_warn_string_init (type, init);
5173 if (pedantic && !flag_isoc99)
5174 pedwarn ("ISO C90 forbids compound literals");
5175 expr.value = build_compound_literal (type, init.value);
5176 expr.original_code = ERROR_MARK;
5177 return c_parser_postfix_expression_after_primary (parser, expr);
5180 /* Parse a postfix expression after the initial primary or compound
5181 literal; that is, parse a series of postfix operators. */
5183 static struct c_expr
5184 c_parser_postfix_expression_after_primary (c_parser *parser,
5187 tree ident, idx, exprlist;
5190 switch (c_parser_peek_token (parser)->type)
5192 case CPP_OPEN_SQUARE:
5193 /* Array reference. */
5194 c_parser_consume_token (parser);
5195 idx = c_parser_expression (parser).value;
5196 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
5198 expr.value = build_array_ref (expr.value, idx);
5199 expr.original_code = ERROR_MARK;
5201 case CPP_OPEN_PAREN:
5202 /* Function call. */
5203 c_parser_consume_token (parser);
5204 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
5205 exprlist = NULL_TREE;
5207 exprlist = c_parser_expr_list (parser);
5208 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5210 expr.value = build_function_call (expr.value, exprlist);
5211 expr.original_code = ERROR_MARK;
5214 /* Structure element reference. */
5215 c_parser_consume_token (parser);
5216 if (c_parser_next_token_is (parser, CPP_NAME))
5217 ident = c_parser_peek_token (parser)->value;
5220 c_parser_error (parser, "expected identifier");
5221 expr.value = error_mark_node;
5222 expr.original_code = ERROR_MARK;
5225 c_parser_consume_token (parser);
5226 expr.value = build_component_ref (expr.value, ident);
5227 expr.original_code = ERROR_MARK;
5230 /* Structure element reference. */
5231 c_parser_consume_token (parser);
5232 if (c_parser_next_token_is (parser, CPP_NAME))
5233 ident = c_parser_peek_token (parser)->value;
5236 c_parser_error (parser, "expected identifier");
5237 expr.value = error_mark_node;
5238 expr.original_code = ERROR_MARK;
5241 c_parser_consume_token (parser);
5242 expr.value = build_component_ref (build_indirect_ref (expr.value,
5244 expr.original_code = ERROR_MARK;
5247 /* Postincrement. */
5248 c_parser_consume_token (parser);
5249 expr.value = build_unary_op (POSTINCREMENT_EXPR, expr.value, 0);
5250 expr.original_code = ERROR_MARK;
5252 case CPP_MINUS_MINUS:
5253 /* Postdecrement. */
5254 c_parser_consume_token (parser);
5255 expr.value = build_unary_op (POSTDECREMENT_EXPR, expr.value, 0);
5256 expr.original_code = ERROR_MARK;
5264 /* Parse an expression (C90 6.3.17, C99 6.5.17).
5267 assignment-expression
5268 expression , assignment-expression
5271 static struct c_expr
5272 c_parser_expression (c_parser *parser)
5275 expr = c_parser_expr_no_commas (parser, NULL);
5276 while (c_parser_next_token_is (parser, CPP_COMMA))
5279 c_parser_consume_token (parser);
5280 next = c_parser_expr_no_commas (parser, NULL);
5281 expr.value = build_compound_expr (expr.value, next.value);
5282 expr.original_code = COMPOUND_EXPR;
5287 /* Parse a non-empty list of expressions.
5290 assignment-expression
5291 nonempty-expr-list , assignment-expression
5295 c_parser_expr_list (c_parser *parser)
5299 expr = c_parser_expr_no_commas (parser, NULL);
5300 ret = cur = build_tree_list (NULL_TREE, expr.value);
5301 while (c_parser_next_token_is (parser, CPP_COMMA))
5303 c_parser_consume_token (parser);
5304 expr = c_parser_expr_no_commas (parser, NULL);
5305 cur = TREE_CHAIN (cur) = build_tree_list (NULL_TREE, expr.value);
5311 /* Parse Objective-C-specific constructs. */
5313 /* Parse an objc-class-definition.
5315 objc-class-definition:
5316 @interface identifier objc-superclass[opt] objc-protocol-refs[opt]
5317 objc-class-instance-variables[opt] objc-methodprotolist @end
5318 @implementation identifier objc-superclass[opt]
5319 objc-class-instance-variables[opt]
5320 @interface identifier ( identifier ) objc-protocol-refs[opt]
5321 objc-methodprotolist @end
5322 @implementation identifier ( identifier )
5327 "@interface identifier (" must start "@interface identifier (
5328 identifier ) ...": objc-methodprotolist in the first production may
5329 not start with a parenthesized identifier as a declarator of a data
5330 definition with no declaration specifiers if the objc-superclass,
5331 objc-protocol-refs and objc-class-instance-variables are omitted. */
5334 c_parser_objc_class_definition (c_parser *parser)
5339 if (c_parser_next_token_is_keyword (parser, RID_AT_INTERFACE))
5341 else if (c_parser_next_token_is_keyword (parser, RID_AT_IMPLEMENTATION))
5345 c_parser_consume_token (parser);
5346 if (c_parser_next_token_is_not (parser, CPP_NAME))
5348 c_parser_error (parser, "expected identifier");
5351 id1 = c_parser_peek_token (parser)->value;
5352 c_parser_consume_token (parser);
5353 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
5356 tree proto = NULL_TREE;
5357 c_parser_consume_token (parser);
5358 if (c_parser_next_token_is_not (parser, CPP_NAME))
5360 c_parser_error (parser, "expected identifier");
5361 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5364 id2 = c_parser_peek_token (parser)->value;
5365 c_parser_consume_token (parser);
5366 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
5369 objc_start_category_implementation (id1, id2);
5372 if (c_parser_next_token_is (parser, CPP_LESS))
5373 proto = c_parser_objc_protocol_refs (parser);
5374 objc_start_category_interface (id1, id2, proto);
5375 c_parser_objc_methodprotolist (parser);
5376 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
5377 objc_finish_interface ();
5380 if (c_parser_next_token_is (parser, CPP_COLON))
5382 c_parser_consume_token (parser);
5383 if (c_parser_next_token_is_not (parser, CPP_NAME))
5385 c_parser_error (parser, "expected identifier");
5388 superclass = c_parser_peek_token (parser)->value;
5389 c_parser_consume_token (parser);
5392 superclass = NULL_TREE;
5395 tree proto = NULL_TREE;
5396 if (c_parser_next_token_is (parser, CPP_LESS))
5397 proto = c_parser_objc_protocol_refs (parser);
5398 objc_start_class_interface (id1, superclass, proto);
5401 objc_start_class_implementation (id1, superclass);
5402 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
5403 c_parser_objc_class_instance_variables (parser);
5406 objc_continue_interface ();
5407 c_parser_objc_methodprotolist (parser);
5408 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
5409 objc_finish_interface ();
5413 objc_continue_implementation ();
5418 /* Parse objc-class-instance-variables.
5420 objc-class-instance-variables:
5421 { objc-instance-variable-decl-list[opt] }
5423 objc-instance-variable-decl-list:
5424 objc-visibility-spec
5425 objc-instance-variable-decl ;
5427 objc-instance-variable-decl-list objc-visibility-spec
5428 objc-instance-variable-decl-list objc-instance-variable-decl ;
5429 objc-instance-variable-decl-list ;
5431 objc-visibility-spec:
5436 objc-instance-variable-decl:
5441 c_parser_objc_class_instance_variables (c_parser *parser)
5443 gcc_assert (c_parser_next_token_is (parser, CPP_OPEN_BRACE));
5444 c_parser_consume_token (parser);
5445 while (c_parser_next_token_is_not (parser, CPP_EOF))
5448 /* Parse any stray semicolon. */
5449 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5452 pedwarn ("extra semicolon in struct or union specified");
5453 c_parser_consume_token (parser);
5456 /* Stop if at the end of the instance variables. */
5457 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
5459 c_parser_consume_token (parser);
5462 /* Parse any objc-visibility-spec. */
5463 if (c_parser_next_token_is_keyword (parser, RID_AT_PRIVATE))
5465 c_parser_consume_token (parser);
5466 objc_set_visibility (2);
5469 else if (c_parser_next_token_is_keyword (parser, RID_AT_PROTECTED))
5471 c_parser_consume_token (parser);
5472 objc_set_visibility (0);
5475 else if (c_parser_next_token_is_keyword (parser, RID_AT_PUBLIC))
5477 c_parser_consume_token (parser);
5478 objc_set_visibility (1);
5481 /* Parse some comma-separated declarations. */
5482 decls = c_parser_struct_declaration (parser);
5484 /* Comma-separated instance variables are chained together in
5485 reverse order; add them one by one. */
5486 tree ivar = nreverse (decls);
5487 for (; ivar; ivar = TREE_CHAIN (ivar))
5488 objc_add_instance_variable (copy_node (ivar));
5490 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
5494 /* Parse an objc-class-declaration.
5496 objc-class-declaration:
5497 @class identifier-list ;
5501 c_parser_objc_class_declaration (c_parser *parser)
5503 tree list = NULL_TREE;
5504 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_CLASS));
5505 c_parser_consume_token (parser);
5506 /* Any identifiers, including those declared as type names, are OK
5511 if (c_parser_next_token_is_not (parser, CPP_NAME))
5513 c_parser_error (parser, "expected identifier");
5516 id = c_parser_peek_token (parser)->value;
5517 list = chainon (list, build_tree_list (NULL_TREE, id));
5518 c_parser_consume_token (parser);
5519 if (c_parser_next_token_is (parser, CPP_COMMA))
5520 c_parser_consume_token (parser);
5524 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
5525 objc_declare_class (list);
5528 /* Parse an objc-alias-declaration.
5530 objc-alias-declaration:
5531 @compatibility_alias identifier identifier ;
5535 c_parser_objc_alias_declaration (c_parser *parser)
5538 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_ALIAS));
5539 c_parser_consume_token (parser);
5540 if (c_parser_next_token_is_not (parser, CPP_NAME))
5542 c_parser_error (parser, "expected identifier");
5543 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
5546 id1 = c_parser_peek_token (parser)->value;
5547 c_parser_consume_token (parser);
5548 if (c_parser_next_token_is_not (parser, CPP_NAME))
5550 c_parser_error (parser, "expected identifier");
5551 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
5554 id2 = c_parser_peek_token (parser)->value;
5555 c_parser_consume_token (parser);
5556 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
5557 objc_declare_alias (id1, id2);
5560 /* Parse an objc-protocol-definition.
5562 objc-protocol-definition:
5563 @protocol identifier objc-protocol-refs[opt] objc-methodprotolist @end
5564 @protocol identifier-list ;
5566 "@protocol identifier ;" should be resolved as "@protocol
5567 identifier-list ;": objc-methodprotolist may not start with a
5568 semicolon in the first alternative if objc-protocol-refs are
5572 c_parser_objc_protocol_definition (c_parser *parser)
5574 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_PROTOCOL));
5575 c_parser_consume_token (parser);
5576 if (c_parser_next_token_is_not (parser, CPP_NAME))
5578 c_parser_error (parser, "expected identifier");
5581 if (c_parser_peek_2nd_token (parser)->type == CPP_COMMA
5582 || c_parser_peek_2nd_token (parser)->type == CPP_SEMICOLON)
5584 tree list = NULL_TREE;
5585 /* Any identifiers, including those declared as type names, are
5590 if (c_parser_next_token_is_not (parser, CPP_NAME))
5592 c_parser_error (parser, "expected identifier");
5595 id = c_parser_peek_token (parser)->value;
5596 list = chainon (list, build_tree_list (NULL_TREE, id));
5597 c_parser_consume_token (parser);
5598 if (c_parser_next_token_is (parser, CPP_COMMA))
5599 c_parser_consume_token (parser);
5603 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
5604 objc_declare_protocols (list);
5608 tree id = c_parser_peek_token (parser)->value;
5609 tree proto = NULL_TREE;
5610 c_parser_consume_token (parser);
5611 if (c_parser_next_token_is (parser, CPP_LESS))
5612 proto = c_parser_objc_protocol_refs (parser);
5613 objc_pq_context = 1;
5614 objc_start_protocol (id, proto);
5615 c_parser_objc_methodprotolist (parser);
5616 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
5617 objc_pq_context = 0;
5618 objc_finish_interface ();
5622 /* Parse an objc-method-type.
5629 static enum tree_code
5630 c_parser_objc_method_type (c_parser *parser)
5632 switch (c_parser_peek_token (parser)->type)
5635 c_parser_consume_token (parser);
5638 c_parser_consume_token (parser);
5645 /* Parse an objc-method-definition.
5647 objc-method-definition:
5648 objc-method-type objc-method-decl ;[opt] compound-statement
5652 c_parser_objc_method_definition (c_parser *parser)
5654 enum tree_code type = c_parser_objc_method_type (parser);
5656 objc_set_method_type (type);
5657 objc_pq_context = 1;
5658 decl = c_parser_objc_method_decl (parser);
5659 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5661 c_parser_consume_token (parser);
5663 pedwarn ("extra semicolon in method definition specified");
5665 objc_pq_context = 0;
5666 objc_start_method_definition (decl);
5667 add_stmt (c_parser_compound_statement (parser));
5668 objc_finish_method_definition (current_function_decl);
5671 /* Parse an objc-methodprotolist.
5673 objc-methodprotolist:
5675 objc-methodprotolist objc-methodproto
5676 objc-methodprotolist declaration
5677 objc-methodprotolist ;
5679 The declaration is a data definition, which may be missing
5680 declaration specifiers under the same rules and diagnostics as
5681 other data definitions outside functions, and the stray semicolon
5682 is diagnosed the same way as a stray semicolon outside a
5686 c_parser_objc_methodprotolist (c_parser *parser)
5690 /* The list is terminated by @end. */
5691 switch (c_parser_peek_token (parser)->type)
5695 pedwarn ("ISO C does not allow extra %<;%> outside of a function");
5696 c_parser_consume_token (parser);
5700 c_parser_objc_methodproto (parser);
5705 if (c_parser_next_token_is_keyword (parser, RID_AT_END))
5707 c_parser_declaration_or_fndef (parser, false, true, false, true);
5713 /* Parse an objc-methodproto.
5716 objc-method-type objc-method-decl ;
5720 c_parser_objc_methodproto (c_parser *parser)
5722 enum tree_code type = c_parser_objc_method_type (parser);
5724 objc_set_method_type (type);
5725 /* Remember protocol qualifiers in prototypes. */
5726 objc_pq_context = 1;
5727 decl = c_parser_objc_method_decl (parser);
5728 /* Forget protocol qualifiers here. */
5729 objc_pq_context = 0;
5730 objc_add_method_declaration (decl);
5731 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
5734 /* Parse an objc-method-decl.
5737 ( objc-type-name ) objc-selector
5739 ( objc-type-name ) objc-keyword-selector objc-optparmlist
5740 objc-keyword-selector objc-optparmlist
5742 objc-keyword-selector:
5744 objc-keyword-selector objc-keyword-decl
5747 objc-selector : ( objc-type-name ) identifier
5748 objc-selector : identifier
5749 : ( objc-type-name ) identifier
5753 objc-optparms objc-optellipsis
5757 objc-opt-parms , parameter-declaration
5765 c_parser_objc_method_decl (c_parser *parser)
5767 tree type = NULL_TREE;
5769 tree parms = NULL_TREE;
5770 bool ellipsis = false;
5772 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
5774 c_parser_consume_token (parser);
5775 type = c_parser_objc_type_name (parser);
5776 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
5778 sel = c_parser_objc_selector (parser);
5779 /* If there is no selector, or a colon follows, we have an
5780 objc-keyword-selector. If there is a selector, and a colon does
5781 not follow, that selector ends the objc-method-decl. */
5782 if (!sel || c_parser_next_token_is (parser, CPP_COLON))
5785 tree list = NULL_TREE;
5788 tree atype = NULL_TREE, id, keyworddecl;
5789 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
5791 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
5793 c_parser_consume_token (parser);
5794 atype = c_parser_objc_type_name (parser);
5795 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5798 if (c_parser_next_token_is_not (parser, CPP_NAME))
5800 c_parser_error (parser, "expected identifier");
5801 return error_mark_node;
5803 id = c_parser_peek_token (parser)->value;
5804 c_parser_consume_token (parser);
5805 keyworddecl = objc_build_keyword_decl (tsel, atype, id);
5806 list = chainon (list, keyworddecl);
5807 tsel = c_parser_objc_selector (parser);
5808 if (!tsel && c_parser_next_token_is_not (parser, CPP_COLON))
5811 /* Parse the optional parameter list. Optional Objective-C
5812 method parameters follow the C syntax, and may include '...'
5813 to denote a variable number of arguments. */
5814 parms = make_node (TREE_LIST);
5815 while (c_parser_next_token_is (parser, CPP_COMMA))
5817 struct c_parm *parm;
5818 c_parser_consume_token (parser);
5819 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
5822 c_parser_consume_token (parser);
5825 parm = c_parser_parameter_declaration (parser, NULL_TREE);
5828 parms = chainon (parms,
5829 build_tree_list (NULL_TREE, grokparm (parm)));
5833 return objc_build_method_signature (type, sel, parms, ellipsis);
5836 /* Parse an objc-type-name.
5839 objc-type-qualifiers[opt] type-name
5840 objc-type-qualifiers[opt]
5842 objc-type-qualifiers:
5844 objc-type-qualifiers objc-type-qualifier
5846 objc-type-qualifier: one of
5847 in out inout bycopy byref oneway
5851 c_parser_objc_type_name (c_parser *parser)
5853 tree quals = NULL_TREE;
5854 struct c_type_name *typename = NULL;
5855 tree type = NULL_TREE;
5858 c_token *token = c_parser_peek_token (parser);
5859 if (token->type == CPP_KEYWORD
5860 && (token->keyword == RID_IN
5861 || token->keyword == RID_OUT
5862 || token->keyword == RID_INOUT
5863 || token->keyword == RID_BYCOPY
5864 || token->keyword == RID_BYREF
5865 || token->keyword == RID_ONEWAY))
5867 quals = chainon (quals, build_tree_list (NULL_TREE, token->value));
5868 c_parser_consume_token (parser);
5873 if (c_parser_next_token_starts_typename (parser))
5874 typename = c_parser_type_name (parser);
5876 type = groktypename (typename);
5877 return build_tree_list (quals, type);
5880 /* Parse objc-protocol-refs.
5887 c_parser_objc_protocol_refs (c_parser *parser)
5889 tree list = NULL_TREE;
5890 gcc_assert (c_parser_next_token_is (parser, CPP_LESS));
5891 c_parser_consume_token (parser);
5892 /* Any identifiers, including those declared as type names, are OK
5897 if (c_parser_next_token_is_not (parser, CPP_NAME))
5899 c_parser_error (parser, "expected identifier");
5902 id = c_parser_peek_token (parser)->value;
5903 list = chainon (list, build_tree_list (NULL_TREE, id));
5904 c_parser_consume_token (parser);
5905 if (c_parser_next_token_is (parser, CPP_COMMA))
5906 c_parser_consume_token (parser);
5910 c_parser_require (parser, CPP_GREATER, "expected %<>%>");
5914 /* Parse an objc-try-catch-statement.
5916 objc-try-catch-statement:
5917 @try compound-statement objc-catch-list[opt]
5918 @try compound-statement objc-catch-list[opt] @finally compound-statement
5921 @catch ( parameter-declaration ) compound-statement
5922 objc-catch-list @catch ( parameter-declaration ) compound-statement
5926 c_parser_objc_try_catch_statement (c_parser *parser)
5930 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_TRY));
5931 c_parser_consume_token (parser);
5932 loc = c_parser_peek_token (parser)->location;
5933 stmt = c_parser_compound_statement (parser);
5934 objc_begin_try_stmt (loc, stmt);
5935 while (c_parser_next_token_is_keyword (parser, RID_AT_CATCH))
5937 struct c_parm *parm;
5938 c_parser_consume_token (parser);
5939 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5941 parm = c_parser_parameter_declaration (parser, NULL_TREE);
5944 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5947 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
5948 objc_begin_catch_clause (grokparm (parm));
5949 if (c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
5950 c_parser_compound_statement_nostart (parser);
5951 objc_finish_catch_clause ();
5953 if (c_parser_next_token_is_keyword (parser, RID_AT_FINALLY))
5957 c_parser_consume_token (parser);
5958 finloc = c_parser_peek_token (parser)->location;
5959 finstmt = c_parser_compound_statement (parser);
5960 objc_build_finally_clause (finloc, finstmt);
5962 objc_finish_try_stmt ();
5965 /* Parse an objc-synchronized-statement.
5967 objc-synchronized-statement:
5968 @synchronized ( expression ) compound-statement
5972 c_parser_objc_synchronized_statement (c_parser *parser)
5976 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_SYNCHRONIZED));
5977 c_parser_consume_token (parser);
5978 loc = c_parser_peek_token (parser)->location;
5979 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5981 expr = c_parser_expression (parser).value;
5982 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
5985 expr = error_mark_node;
5986 stmt = c_parser_compound_statement (parser);
5987 objc_build_synchronized (loc, expr, stmt);
5990 /* Parse an objc-selector; return NULL_TREE without an error if the
5991 next token is not an objc-selector.
5996 enum struct union if else while do for switch case default
5997 break continue return goto asm sizeof typeof __alignof
5998 unsigned long const short volatile signed restrict _Complex
5999 in out inout bycopy byref oneway int char float double void _Bool
6001 ??? Why this selection of keywords but not, for example, storage
6002 class specifiers? */
6005 c_parser_objc_selector (c_parser *parser)
6007 c_token *token = c_parser_peek_token (parser);
6008 tree value = token->value;
6009 if (token->type == CPP_NAME)
6011 c_parser_consume_token (parser);
6014 if (token->type != CPP_KEYWORD)
6016 switch (token->keyword)
6057 c_parser_consume_token (parser);
6064 /* Parse an objc-selector-arg.
6068 objc-keywordname-list
6070 objc-keywordname-list:
6072 objc-keywordname-list objc-keywordname
6080 c_parser_objc_selector_arg (c_parser *parser)
6082 tree sel = c_parser_objc_selector (parser);
6083 tree list = NULL_TREE;
6084 if (sel && c_parser_next_token_is_not (parser, CPP_COLON))
6088 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
6090 list = chainon (list, build_tree_list (sel, NULL_TREE));
6091 sel = c_parser_objc_selector (parser);
6092 if (!sel && c_parser_next_token_is_not (parser, CPP_COLON))
6098 /* Parse an objc-receiver.
6107 c_parser_objc_receiver (c_parser *parser)
6109 if (c_parser_peek_token (parser)->type == CPP_NAME
6110 && (c_parser_peek_token (parser)->id_kind == C_ID_TYPENAME
6111 || c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME))
6113 tree id = c_parser_peek_token (parser)->value;
6114 c_parser_consume_token (parser);
6115 return objc_get_class_reference (id);
6117 return c_parser_expression (parser).value;
6120 /* Parse objc-message-args.
6124 objc-keywordarg-list
6126 objc-keywordarg-list:
6128 objc-keywordarg-list objc-keywordarg
6131 objc-selector : objc-keywordexpr
6136 c_parser_objc_message_args (c_parser *parser)
6138 tree sel = c_parser_objc_selector (parser);
6139 tree list = NULL_TREE;
6140 if (sel && c_parser_next_token_is_not (parser, CPP_COLON))
6145 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
6147 keywordexpr = c_parser_objc_keywordexpr (parser);
6148 list = chainon (list, build_tree_list (sel, keywordexpr));
6149 sel = c_parser_objc_selector (parser);
6150 if (!sel && c_parser_next_token_is_not (parser, CPP_COLON))
6156 /* Parse an objc-keywordexpr.
6163 c_parser_objc_keywordexpr (c_parser *parser)
6165 tree list = c_parser_expr_list (parser);
6166 if (TREE_CHAIN (list) == NULL_TREE)
6168 /* Just return the expression, remove a level of
6170 return TREE_VALUE (list);
6174 /* We have a comma expression, we will collapse later. */
6180 /* The actual parser and external interface. ??? Does this need to be
6181 garbage-collected? */
6183 static GTY (()) c_parser *the_parser;
6185 /* Parse a single source file. */
6190 the_parser = c_parser_new ();
6191 c_parser_translation_unit (the_parser);
6195 #include "gt-c-parser.h"