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, 2007 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, 51 Franklin Street, Fifth Floor, 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"
46 #include "langhooks.h"
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 { "_Decimal32", RID_DFLOAT32, D_EXT },
97 { "_Decimal64", RID_DFLOAT64, D_EXT },
98 { "_Decimal128", RID_DFLOAT128, D_EXT },
99 { "__FUNCTION__", RID_FUNCTION_NAME, 0 },
100 { "__PRETTY_FUNCTION__", RID_PRETTY_FUNCTION_NAME, 0 },
101 { "__alignof", RID_ALIGNOF, 0 },
102 { "__alignof__", RID_ALIGNOF, 0 },
103 { "__asm", RID_ASM, 0 },
104 { "__asm__", RID_ASM, 0 },
105 { "__attribute", RID_ATTRIBUTE, 0 },
106 { "__attribute__", RID_ATTRIBUTE, 0 },
107 { "__builtin_choose_expr", RID_CHOOSE_EXPR, 0 },
108 { "__builtin_offsetof", RID_OFFSETOF, 0 },
109 { "__builtin_types_compatible_p", RID_TYPES_COMPATIBLE_P, 0 },
110 { "__builtin_va_arg", RID_VA_ARG, 0 },
111 { "__complex", RID_COMPLEX, 0 },
112 { "__complex__", RID_COMPLEX, 0 },
113 { "__const", RID_CONST, 0 },
114 { "__const__", RID_CONST, 0 },
115 { "__extension__", RID_EXTENSION, 0 },
116 { "__func__", RID_C99_FUNCTION_NAME, 0 },
117 { "__imag", RID_IMAGPART, 0 },
118 { "__imag__", RID_IMAGPART, 0 },
119 { "__inline", RID_INLINE, 0 },
120 { "__inline__", RID_INLINE, 0 },
121 { "__label__", RID_LABEL, 0 },
122 { "__real", RID_REALPART, 0 },
123 { "__real__", RID_REALPART, 0 },
124 { "__restrict", RID_RESTRICT, 0 },
125 { "__restrict__", RID_RESTRICT, 0 },
126 { "__signed", RID_SIGNED, 0 },
127 { "__signed__", RID_SIGNED, 0 },
128 { "__thread", RID_THREAD, 0 },
129 { "__typeof", RID_TYPEOF, 0 },
130 { "__typeof__", RID_TYPEOF, 0 },
131 { "__volatile", RID_VOLATILE, 0 },
132 { "__volatile__", RID_VOLATILE, 0 },
133 { "asm", RID_ASM, D_EXT },
134 { "auto", RID_AUTO, 0 },
135 { "break", RID_BREAK, 0 },
136 { "case", RID_CASE, 0 },
137 { "char", RID_CHAR, 0 },
138 { "const", RID_CONST, 0 },
139 { "continue", RID_CONTINUE, 0 },
140 { "default", RID_DEFAULT, 0 },
142 { "double", RID_DOUBLE, 0 },
143 { "else", RID_ELSE, 0 },
144 { "enum", RID_ENUM, 0 },
145 { "extern", RID_EXTERN, 0 },
146 { "float", RID_FLOAT, 0 },
147 { "for", RID_FOR, 0 },
148 { "goto", RID_GOTO, 0 },
150 { "inline", RID_INLINE, D_EXT89 },
151 { "int", RID_INT, 0 },
152 { "long", RID_LONG, 0 },
153 { "register", RID_REGISTER, 0 },
154 { "restrict", RID_RESTRICT, D_C89 },
155 { "return", RID_RETURN, 0 },
156 { "short", RID_SHORT, 0 },
157 { "signed", RID_SIGNED, 0 },
158 { "sizeof", RID_SIZEOF, 0 },
159 { "static", RID_STATIC, 0 },
160 { "struct", RID_STRUCT, 0 },
161 { "switch", RID_SWITCH, 0 },
162 { "typedef", RID_TYPEDEF, 0 },
163 { "typeof", RID_TYPEOF, D_EXT },
164 { "union", RID_UNION, 0 },
165 { "unsigned", RID_UNSIGNED, 0 },
166 { "void", RID_VOID, 0 },
167 { "volatile", RID_VOLATILE, 0 },
168 { "while", RID_WHILE, 0 },
169 /* These Objective-C keywords are recognized only immediately after
171 { "class", RID_AT_CLASS, D_OBJC },
172 { "compatibility_alias", RID_AT_ALIAS, D_OBJC },
173 { "defs", RID_AT_DEFS, D_OBJC },
174 { "encode", RID_AT_ENCODE, D_OBJC },
175 { "end", RID_AT_END, D_OBJC },
176 { "implementation", RID_AT_IMPLEMENTATION, D_OBJC },
177 { "interface", RID_AT_INTERFACE, D_OBJC },
178 { "private", RID_AT_PRIVATE, D_OBJC },
179 { "protected", RID_AT_PROTECTED, D_OBJC },
180 { "protocol", RID_AT_PROTOCOL, D_OBJC },
181 { "public", RID_AT_PUBLIC, D_OBJC },
182 { "selector", RID_AT_SELECTOR, D_OBJC },
183 { "throw", RID_AT_THROW, D_OBJC },
184 { "try", RID_AT_TRY, D_OBJC },
185 { "catch", RID_AT_CATCH, D_OBJC },
186 { "finally", RID_AT_FINALLY, D_OBJC },
187 { "synchronized", RID_AT_SYNCHRONIZED, D_OBJC },
188 /* These are recognized only in protocol-qualifier context
190 { "bycopy", RID_BYCOPY, D_OBJC },
191 { "byref", RID_BYREF, D_OBJC },
192 { "in", RID_IN, D_OBJC },
193 { "inout", RID_INOUT, D_OBJC },
194 { "oneway", RID_ONEWAY, D_OBJC },
195 { "out", RID_OUT, D_OBJC },
197 #define N_reswords (sizeof reswords / sizeof (struct resword))
199 /* Initialization routine for this file. */
204 /* The only initialization required is of the reserved word
208 int mask = (flag_isoc99 ? 0 : D_C89)
209 | (flag_no_asm ? (flag_isoc99 ? D_EXT : D_EXT|D_EXT89) : 0);
211 if (!c_dialect_objc ())
214 ridpointers = GGC_CNEWVEC (tree, (int) RID_MAX);
215 for (i = 0; i < N_reswords; i++)
217 /* If a keyword is disabled, do not enter it into the table
218 and so create a canonical spelling that isn't a keyword. */
219 if (reswords[i].disable & mask)
222 id = get_identifier (reswords[i].word);
223 C_RID_CODE (id) = reswords[i].rid;
224 C_IS_RESERVED_WORD (id) = 1;
225 ridpointers [(int) reswords[i].rid] = id;
229 /* The C lexer intermediates between the lexer in cpplib and c-lex.c
230 and the C parser. Unlike the C++ lexer, the parser structure
231 stores the lexer information instead of using a separate structure.
232 Identifiers are separated into ordinary identifiers, type names,
233 keywords and some other Objective-C types of identifiers, and some
234 look-ahead is maintained.
236 ??? It might be a good idea to lex the whole file up front (as for
237 C++). It would then be possible to share more of the C and C++
238 lexer code, if desired. */
240 /* The following local token type is used. */
243 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
245 /* More information about the type of a CPP_NAME token. */
246 typedef enum c_id_kind {
247 /* An ordinary identifier. */
249 /* An identifier declared as a typedef name. */
251 /* An identifier declared as an Objective-C class name. */
253 /* Not an identifier. */
257 /* A single C token after string literal concatenation and conversion
258 of preprocessing tokens to tokens. */
259 typedef struct c_token GTY (())
261 /* The kind of token. */
262 ENUM_BITFIELD (cpp_ttype) type : 8;
263 /* If this token is a CPP_NAME, this value indicates whether also
264 declared as some kind of type. Otherwise, it is C_ID_NONE. */
265 ENUM_BITFIELD (c_id_kind) id_kind : 8;
266 /* If this token is a keyword, this value indicates which keyword.
267 Otherwise, this value is RID_MAX. */
268 ENUM_BITFIELD (rid) keyword : 8;
269 /* If this token is a CPP_PRAGMA, this indicates the pragma that
270 was seen. Otherwise it is PRAGMA_NONE. */
271 ENUM_BITFIELD (pragma_kind) pragma_kind : 7;
272 /* True if this token is from a system header. */
273 BOOL_BITFIELD in_system_header : 1;
274 /* The value associated with this token, if any. */
276 /* The location at which this token was found. */
280 /* A parser structure recording information about the state and
281 context of parsing. Includes lexer information with up to two
282 tokens of look-ahead; more are not needed for C. */
283 typedef struct c_parser GTY(())
285 /* The look-ahead tokens. */
287 /* How many look-ahead tokens are available (0, 1 or 2). */
289 /* True if a syntax error is being recovered from; false otherwise.
290 c_parser_error sets this flag. It should clear this flag when
291 enough tokens have been consumed to recover from the error. */
292 BOOL_BITFIELD error : 1;
293 /* True if we're processing a pragma, and shouldn't automatically
294 consume CPP_PRAGMA_EOL. */
295 BOOL_BITFIELD in_pragma : 1;
299 /* The actual parser and external interface. ??? Does this need to be
300 garbage-collected? */
302 static GTY (()) c_parser *the_parser;
305 /* Read in and lex a single token, storing it in *TOKEN. */
308 c_lex_one_token (c_token *token)
310 timevar_push (TV_LEX);
312 token->type = c_lex_with_flags (&token->value, &token->location, NULL);
313 token->id_kind = C_ID_NONE;
314 token->keyword = RID_MAX;
315 token->pragma_kind = PRAGMA_NONE;
316 token->in_system_header = in_system_header;
324 int objc_force_identifier = objc_need_raw_identifier;
325 OBJC_NEED_RAW_IDENTIFIER (0);
327 if (C_IS_RESERVED_WORD (token->value))
329 enum rid rid_code = C_RID_CODE (token->value);
331 if (c_dialect_objc ())
333 if (!OBJC_IS_AT_KEYWORD (rid_code)
334 && (!OBJC_IS_PQ_KEYWORD (rid_code) || objc_pq_context))
336 /* Return the canonical spelling for this keyword. */
337 token->value = ridpointers[(int) rid_code];
338 token->type = CPP_KEYWORD;
339 token->keyword = rid_code;
345 /* Return the canonical spelling for this keyword. */
346 token->value = ridpointers[(int) rid_code];
347 token->type = CPP_KEYWORD;
348 token->keyword = rid_code;
353 decl = lookup_name (token->value);
356 if (TREE_CODE (decl) == TYPE_DECL)
358 token->id_kind = C_ID_TYPENAME;
362 else if (c_dialect_objc ())
364 tree objc_interface_decl = objc_is_class_name (token->value);
365 /* Objective-C class names are in the same namespace as
366 variables and typedefs, and hence are shadowed by local
368 if (objc_interface_decl
369 && (global_bindings_p ()
370 || (!objc_force_identifier && !decl)))
372 token->value = objc_interface_decl;
373 token->id_kind = C_ID_CLASSNAME;
377 token->id_kind = C_ID_ID;
381 /* This only happens in Objective-C; it must be a keyword. */
382 token->type = CPP_KEYWORD;
383 token->keyword = C_RID_CODE (token->value);
387 case CPP_CLOSE_PAREN:
389 /* These tokens may affect the interpretation of any identifiers
390 following, if doing Objective-C. */
391 OBJC_NEED_RAW_IDENTIFIER (0);
394 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
395 token->pragma_kind = TREE_INT_CST_LOW (token->value);
401 timevar_pop (TV_LEX);
404 /* Return a pointer to the next token from PARSER, reading it in if
407 static inline c_token *
408 c_parser_peek_token (c_parser *parser)
410 if (parser->tokens_avail == 0)
412 c_lex_one_token (&parser->tokens[0]);
413 parser->tokens_avail = 1;
415 return &parser->tokens[0];
418 /* Return true if the next token from PARSER has the indicated
422 c_parser_next_token_is (c_parser *parser, enum cpp_ttype type)
424 return c_parser_peek_token (parser)->type == type;
427 /* Return true if the next token from PARSER does not have the
431 c_parser_next_token_is_not (c_parser *parser, enum cpp_ttype type)
433 return !c_parser_next_token_is (parser, type);
436 /* Return true if the next token from PARSER is the indicated
440 c_parser_next_token_is_keyword (c_parser *parser, enum rid keyword)
444 /* Peek at the next token. */
445 token = c_parser_peek_token (parser);
446 /* Check to see if it is the indicated keyword. */
447 return token->keyword == keyword;
450 /* Return true if TOKEN can start a type name,
453 c_token_starts_typename (c_token *token)
458 switch (token->id_kind)
465 gcc_assert (c_dialect_objc ());
471 switch (token->keyword)
500 if (c_dialect_objc ())
508 /* Return true if the next token from PARSER can start a type name,
511 c_parser_next_token_starts_typename (c_parser *parser)
513 c_token *token = c_parser_peek_token (parser);
514 return c_token_starts_typename (token);
517 /* Return true if TOKEN can start declaration specifiers, false
520 c_token_starts_declspecs (c_token *token)
525 switch (token->id_kind)
532 gcc_assert (c_dialect_objc ());
538 switch (token->keyword)
574 if (c_dialect_objc ())
582 /* Return true if the next token from PARSER can start declaration
583 specifiers, false otherwise. */
585 c_parser_next_token_starts_declspecs (c_parser *parser)
587 c_token *token = c_parser_peek_token (parser);
588 return c_token_starts_declspecs (token);
591 /* Return a pointer to the next-but-one token from PARSER, reading it
592 in if necessary. The next token is already read in. */
595 c_parser_peek_2nd_token (c_parser *parser)
597 if (parser->tokens_avail >= 2)
598 return &parser->tokens[1];
599 gcc_assert (parser->tokens_avail == 1);
600 gcc_assert (parser->tokens[0].type != CPP_EOF);
601 gcc_assert (parser->tokens[0].type != CPP_PRAGMA_EOL);
602 c_lex_one_token (&parser->tokens[1]);
603 parser->tokens_avail = 2;
604 return &parser->tokens[1];
607 /* Consume the next token from PARSER. */
610 c_parser_consume_token (c_parser *parser)
612 gcc_assert (parser->tokens_avail >= 1);
613 gcc_assert (parser->tokens[0].type != CPP_EOF);
614 gcc_assert (!parser->in_pragma || parser->tokens[0].type != CPP_PRAGMA_EOL);
615 gcc_assert (parser->error || parser->tokens[0].type != CPP_PRAGMA);
616 if (parser->tokens_avail == 2)
617 parser->tokens[0] = parser->tokens[1];
618 parser->tokens_avail--;
621 /* Expect the current token to be a #pragma. Consume it and remember
622 that we've begun parsing a pragma. */
625 c_parser_consume_pragma (c_parser *parser)
627 gcc_assert (!parser->in_pragma);
628 gcc_assert (parser->tokens_avail >= 1);
629 gcc_assert (parser->tokens[0].type == CPP_PRAGMA);
630 if (parser->tokens_avail == 2)
631 parser->tokens[0] = parser->tokens[1];
632 parser->tokens_avail--;
633 parser->in_pragma = true;
636 /* Update the globals input_location and in_system_header from
639 c_parser_set_source_position_from_token (c_token *token)
641 if (token->type != CPP_EOF)
643 input_location = token->location;
644 in_system_header = token->in_system_header;
648 /* Issue a diagnostic of the form
649 FILE:LINE: MESSAGE before TOKEN
650 where TOKEN is the next token in the input stream of PARSER.
651 MESSAGE (specified by the caller) is usually of the form "expected
654 Do not issue a diagnostic if still recovering from an error.
656 ??? This is taken from the C++ parser, but building up messages in
657 this way is not i18n-friendly and some other approach should be
661 c_parser_error (c_parser *parser, const char *gmsgid)
663 c_token *token = c_parser_peek_token (parser);
666 parser->error = true;
669 /* This diagnostic makes more sense if it is tagged to the line of
670 the token we just peeked at. */
671 c_parser_set_source_position_from_token (token);
672 c_parse_error (gmsgid,
673 /* Because c_parse_error does not understand
674 CPP_KEYWORD, keywords are treated like
676 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
680 /* If the next token is of the indicated TYPE, consume it. Otherwise,
681 issue the error MSGID. If MSGID is NULL then a message has already
682 been produced and no message will be produced this time. Returns
683 true if found, false otherwise. */
686 c_parser_require (c_parser *parser,
690 if (c_parser_next_token_is (parser, type))
692 c_parser_consume_token (parser);
697 c_parser_error (parser, msgid);
702 /* If the next token is the indicated keyword, consume it. Otherwise,
703 issue the error MSGID. Returns true if found, false otherwise. */
706 c_parser_require_keyword (c_parser *parser,
710 if (c_parser_next_token_is_keyword (parser, keyword))
712 c_parser_consume_token (parser);
717 c_parser_error (parser, msgid);
722 /* Like c_parser_require, except that tokens will be skipped until the
723 desired token is found. An error message is still produced if the
724 next token is not as expected. If MSGID is NULL then a message has
725 already been produced and no message will be produced this
729 c_parser_skip_until_found (c_parser *parser,
733 unsigned nesting_depth = 0;
735 if (c_parser_require (parser, type, msgid))
738 /* Skip tokens until the desired token is found. */
741 /* Peek at the next token. */
742 c_token *token = c_parser_peek_token (parser);
743 /* If we've reached the token we want, consume it and stop. */
744 if (token->type == type && !nesting_depth)
746 c_parser_consume_token (parser);
750 /* If we've run out of tokens, stop. */
751 if (token->type == CPP_EOF)
753 if (token->type == CPP_PRAGMA_EOL && parser->in_pragma)
755 if (token->type == CPP_OPEN_BRACE
756 || token->type == CPP_OPEN_PAREN
757 || token->type == CPP_OPEN_SQUARE)
759 else if (token->type == CPP_CLOSE_BRACE
760 || token->type == CPP_CLOSE_PAREN
761 || token->type == CPP_CLOSE_SQUARE)
763 if (nesting_depth-- == 0)
766 /* Consume this token. */
767 c_parser_consume_token (parser);
769 parser->error = false;
772 /* Skip tokens until the end of a parameter is found, but do not
773 consume the comma, semicolon or closing delimiter. */
776 c_parser_skip_to_end_of_parameter (c_parser *parser)
778 unsigned nesting_depth = 0;
782 c_token *token = c_parser_peek_token (parser);
783 if ((token->type == CPP_COMMA || token->type == CPP_SEMICOLON)
786 /* If we've run out of tokens, stop. */
787 if (token->type == CPP_EOF)
789 if (token->type == CPP_PRAGMA_EOL && parser->in_pragma)
791 if (token->type == CPP_OPEN_BRACE
792 || token->type == CPP_OPEN_PAREN
793 || token->type == CPP_OPEN_SQUARE)
795 else if (token->type == CPP_CLOSE_BRACE
796 || token->type == CPP_CLOSE_PAREN
797 || token->type == CPP_CLOSE_SQUARE)
799 if (nesting_depth-- == 0)
802 /* Consume this token. */
803 c_parser_consume_token (parser);
805 parser->error = false;
808 /* Expect to be at the end of the pragma directive and consume an
809 end of line marker. */
812 c_parser_skip_to_pragma_eol (c_parser *parser)
814 gcc_assert (parser->in_pragma);
815 parser->in_pragma = false;
817 if (!c_parser_require (parser, CPP_PRAGMA_EOL, "expected end of line"))
820 c_token *token = c_parser_peek_token (parser);
821 if (token->type == CPP_EOF)
823 if (token->type == CPP_PRAGMA_EOL)
825 c_parser_consume_token (parser);
828 c_parser_consume_token (parser);
831 parser->error = false;
834 /* Skip tokens until we have consumed an entire block, or until we
835 have consumed a non-nested ';'. */
838 c_parser_skip_to_end_of_block_or_statement (c_parser *parser)
840 unsigned nesting_depth = 0;
841 bool save_error = parser->error;
847 /* Peek at the next token. */
848 token = c_parser_peek_token (parser);
856 if (parser->in_pragma)
861 /* If the next token is a ';', we have reached the
862 end of the statement. */
865 /* Consume the ';'. */
866 c_parser_consume_token (parser);
871 case CPP_CLOSE_BRACE:
872 /* If the next token is a non-nested '}', then we have
873 reached the end of the current block. */
874 if (nesting_depth == 0 || --nesting_depth == 0)
876 c_parser_consume_token (parser);
882 /* If it the next token is a '{', then we are entering a new
883 block. Consume the entire block. */
888 /* If we see a pragma, consume the whole thing at once. We
889 have some safeguards against consuming pragmas willy-nilly.
890 Normally, we'd expect to be here with parser->error set,
891 which disables these safeguards. But it's possible to get
892 here for secondary error recovery, after parser->error has
894 c_parser_consume_pragma (parser);
895 c_parser_skip_to_pragma_eol (parser);
896 parser->error = save_error;
903 c_parser_consume_token (parser);
907 parser->error = false;
910 /* Save the warning flags which are controlled by __extension__. */
913 disable_extension_diagnostics (void)
916 | (warn_pointer_arith << 1)
917 | (warn_traditional << 2)
920 warn_pointer_arith = 0;
921 warn_traditional = 0;
926 /* Restore the warning flags which are controlled by __extension__.
927 FLAGS is the return value from disable_extension_diagnostics. */
930 restore_extension_diagnostics (int flags)
932 pedantic = flags & 1;
933 warn_pointer_arith = (flags >> 1) & 1;
934 warn_traditional = (flags >> 2) & 1;
935 flag_iso = (flags >> 3) & 1;
938 /* Possibly kinds of declarator to parse. */
939 typedef enum c_dtr_syn {
940 /* A normal declarator with an identifier. */
942 /* An abstract declarator (maybe empty). */
944 /* A parameter declarator: may be either, but after a type name does
945 not redeclare a typedef name as an identifier if it can
946 alternatively be interpreted as a typedef name; see DR#009,
947 applied in C90 TC1, omitted from C99 and reapplied in C99 TC2
948 following DR#249. For example, given a typedef T, "int T" and
949 "int *T" are valid parameter declarations redeclaring T, while
950 "int (T)" and "int * (T)" and "int (T[])" and "int (T (int))" are
951 abstract declarators rather than involving redundant parentheses;
952 the same applies with attributes inside the parentheses before
957 static void c_parser_external_declaration (c_parser *);
958 static void c_parser_asm_definition (c_parser *);
959 static void c_parser_declaration_or_fndef (c_parser *, bool, bool, bool, bool);
960 static void c_parser_declspecs (c_parser *, struct c_declspecs *, bool, bool,
962 static struct c_typespec c_parser_enum_specifier (c_parser *);
963 static struct c_typespec c_parser_struct_or_union_specifier (c_parser *);
964 static tree c_parser_struct_declaration (c_parser *);
965 static struct c_typespec c_parser_typeof_specifier (c_parser *);
966 static struct c_declarator *c_parser_declarator (c_parser *, bool, c_dtr_syn,
968 static struct c_declarator *c_parser_direct_declarator (c_parser *, bool,
970 static struct c_declarator *c_parser_direct_declarator_inner (c_parser *,
972 struct c_declarator *);
973 static struct c_arg_info *c_parser_parms_declarator (c_parser *, bool, tree);
974 static struct c_arg_info *c_parser_parms_list_declarator (c_parser *, tree);
975 static struct c_parm *c_parser_parameter_declaration (c_parser *, tree);
976 static tree c_parser_simple_asm_expr (c_parser *);
977 static tree c_parser_attributes (c_parser *);
978 static struct c_type_name *c_parser_type_name (c_parser *);
979 static struct c_expr c_parser_initializer (c_parser *);
980 static struct c_expr c_parser_braced_init (c_parser *, tree, bool);
981 static void c_parser_initelt (c_parser *);
982 static void c_parser_initval (c_parser *, struct c_expr *);
983 static tree c_parser_compound_statement (c_parser *);
984 static void c_parser_compound_statement_nostart (c_parser *);
985 static void c_parser_label (c_parser *);
986 static void c_parser_statement (c_parser *);
987 static void c_parser_statement_after_labels (c_parser *);
988 static void c_parser_if_statement (c_parser *);
989 static void c_parser_switch_statement (c_parser *);
990 static void c_parser_while_statement (c_parser *);
991 static void c_parser_do_statement (c_parser *);
992 static void c_parser_for_statement (c_parser *);
993 static tree c_parser_asm_statement (c_parser *);
994 static tree c_parser_asm_operands (c_parser *, bool);
995 static tree c_parser_asm_clobbers (c_parser *);
996 static struct c_expr c_parser_expr_no_commas (c_parser *, struct c_expr *);
997 static struct c_expr c_parser_conditional_expression (c_parser *,
999 static struct c_expr c_parser_binary_expression (c_parser *, struct c_expr *);
1000 static struct c_expr c_parser_cast_expression (c_parser *, struct c_expr *);
1001 static struct c_expr c_parser_unary_expression (c_parser *);
1002 static struct c_expr c_parser_sizeof_expression (c_parser *);
1003 static struct c_expr c_parser_alignof_expression (c_parser *);
1004 static struct c_expr c_parser_postfix_expression (c_parser *);
1005 static struct c_expr c_parser_postfix_expression_after_paren_type (c_parser *,
1006 struct c_type_name *);
1007 static struct c_expr c_parser_postfix_expression_after_primary (c_parser *,
1009 static struct c_expr c_parser_expression (c_parser *);
1010 static struct c_expr c_parser_expression_conv (c_parser *);
1011 static tree c_parser_expr_list (c_parser *, bool);
1012 static void c_parser_omp_construct (c_parser *);
1013 static void c_parser_omp_threadprivate (c_parser *);
1014 static void c_parser_omp_barrier (c_parser *);
1015 static void c_parser_omp_flush (c_parser *);
1017 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
1018 static bool c_parser_pragma (c_parser *, enum pragma_context);
1020 /* These Objective-C parser functions are only ever called when
1021 compiling Objective-C. */
1022 static void c_parser_objc_class_definition (c_parser *);
1023 static void c_parser_objc_class_instance_variables (c_parser *);
1024 static void c_parser_objc_class_declaration (c_parser *);
1025 static void c_parser_objc_alias_declaration (c_parser *);
1026 static void c_parser_objc_protocol_definition (c_parser *);
1027 static enum tree_code c_parser_objc_method_type (c_parser *);
1028 static void c_parser_objc_method_definition (c_parser *);
1029 static void c_parser_objc_methodprotolist (c_parser *);
1030 static void c_parser_objc_methodproto (c_parser *);
1031 static tree c_parser_objc_method_decl (c_parser *);
1032 static tree c_parser_objc_type_name (c_parser *);
1033 static tree c_parser_objc_protocol_refs (c_parser *);
1034 static void c_parser_objc_try_catch_statement (c_parser *);
1035 static void c_parser_objc_synchronized_statement (c_parser *);
1036 static tree c_parser_objc_selector (c_parser *);
1037 static tree c_parser_objc_selector_arg (c_parser *);
1038 static tree c_parser_objc_receiver (c_parser *);
1039 static tree c_parser_objc_message_args (c_parser *);
1040 static tree c_parser_objc_keywordexpr (c_parser *);
1042 /* Parse a translation unit (C90 6.7, C99 6.9).
1045 external-declarations
1047 external-declarations:
1048 external-declaration
1049 external-declarations external-declaration
1058 c_parser_translation_unit (c_parser *parser)
1060 if (c_parser_next_token_is (parser, CPP_EOF))
1063 pedwarn ("ISO C forbids an empty source file");
1067 void *obstack_position = obstack_alloc (&parser_obstack, 0);
1071 c_parser_external_declaration (parser);
1072 obstack_free (&parser_obstack, obstack_position);
1074 while (c_parser_next_token_is_not (parser, CPP_EOF));
1078 /* Parse an external declaration (C90 6.7, C99 6.9).
1080 external-declaration:
1086 external-declaration:
1089 __extension__ external-declaration
1093 external-declaration:
1094 objc-class-definition
1095 objc-class-declaration
1096 objc-alias-declaration
1097 objc-protocol-definition
1098 objc-method-definition
1103 c_parser_external_declaration (c_parser *parser)
1106 switch (c_parser_peek_token (parser)->type)
1109 switch (c_parser_peek_token (parser)->keyword)
1112 ext = disable_extension_diagnostics ();
1113 c_parser_consume_token (parser);
1114 c_parser_external_declaration (parser);
1115 restore_extension_diagnostics (ext);
1118 c_parser_asm_definition (parser);
1120 case RID_AT_INTERFACE:
1121 case RID_AT_IMPLEMENTATION:
1122 gcc_assert (c_dialect_objc ());
1123 c_parser_objc_class_definition (parser);
1126 gcc_assert (c_dialect_objc ());
1127 c_parser_objc_class_declaration (parser);
1130 gcc_assert (c_dialect_objc ());
1131 c_parser_objc_alias_declaration (parser);
1133 case RID_AT_PROTOCOL:
1134 gcc_assert (c_dialect_objc ());
1135 c_parser_objc_protocol_definition (parser);
1138 gcc_assert (c_dialect_objc ());
1139 c_parser_consume_token (parser);
1140 objc_finish_implementation ();
1148 pedwarn ("ISO C does not allow extra %<;%> outside of a function");
1149 c_parser_consume_token (parser);
1152 c_parser_pragma (parser, pragma_external);
1156 if (c_dialect_objc ())
1158 c_parser_objc_method_definition (parser);
1161 /* Else fall through, and yield a syntax error trying to parse
1162 as a declaration or function definition. */
1165 /* A declaration or a function definition. We can only tell
1166 which after parsing the declaration specifiers, if any, and
1167 the first declarator. */
1168 c_parser_declaration_or_fndef (parser, true, true, false, true);
1174 /* Parse a declaration or function definition (C90 6.5, 6.7.1, C99
1175 6.7, 6.9.1). If FNDEF_OK is true, a function definition is
1176 accepted; otherwise (old-style parameter declarations) only other
1177 declarations are accepted. If NESTED is true, we are inside a
1178 function or parsing old-style parameter declarations; any functions
1179 encountered are nested functions and declaration specifiers are
1180 required; otherwise we are at top level and functions are normal
1181 functions and declaration specifiers may be optional. If EMPTY_OK
1182 is true, empty declarations are OK (subject to all other
1183 constraints); otherwise (old-style parameter declarations) they are
1184 diagnosed. If START_ATTR_OK is true, the declaration specifiers
1185 may start with attributes; otherwise they may not.
1188 declaration-specifiers init-declarator-list[opt] ;
1190 function-definition:
1191 declaration-specifiers[opt] declarator declaration-list[opt]
1196 declaration-list declaration
1198 init-declarator-list:
1200 init-declarator-list , init-declarator
1203 declarator simple-asm-expr[opt] attributes[opt]
1204 declarator simple-asm-expr[opt] attributes[opt] = initializer
1208 nested-function-definition:
1209 declaration-specifiers declarator declaration-list[opt]
1212 The simple-asm-expr and attributes are GNU extensions.
1214 This function does not handle __extension__; that is handled in its
1215 callers. ??? Following the old parser, __extension__ may start
1216 external declarations, declarations in functions and declarations
1217 at the start of "for" loops, but not old-style parameter
1220 C99 requires declaration specifiers in a function definition; the
1221 absence is diagnosed through the diagnosis of implicit int. In GNU
1222 C we also allow but diagnose declarations without declaration
1223 specifiers, but only at top level (elsewhere they conflict with
1229 threadprivate-directive */
1232 c_parser_declaration_or_fndef (c_parser *parser, bool fndef_ok, bool empty_ok,
1233 bool nested, bool start_attr_ok)
1235 struct c_declspecs *specs;
1237 tree all_prefix_attrs;
1238 bool diagnosed_no_specs = false;
1240 specs = build_null_declspecs ();
1241 c_parser_declspecs (parser, specs, true, true, start_attr_ok);
1244 c_parser_skip_to_end_of_block_or_statement (parser);
1247 if (nested && !specs->declspecs_seen_p)
1249 c_parser_error (parser, "expected declaration specifiers");
1250 c_parser_skip_to_end_of_block_or_statement (parser);
1253 finish_declspecs (specs);
1254 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1260 shadow_tag_warned (specs, 1);
1261 pedwarn ("empty declaration");
1263 c_parser_consume_token (parser);
1266 pending_xref_error ();
1267 prefix_attrs = specs->attrs;
1268 all_prefix_attrs = prefix_attrs;
1269 specs->attrs = NULL_TREE;
1272 struct c_declarator *declarator;
1275 /* Declaring either one or more declarators (in which case we
1276 should diagnose if there were no declaration specifiers) or a
1277 function definition (in which case the diagnostic for
1278 implicit int suffices). */
1279 declarator = c_parser_declarator (parser, specs->type_seen_p,
1280 C_DTR_NORMAL, &dummy);
1281 if (declarator == NULL)
1283 c_parser_skip_to_end_of_block_or_statement (parser);
1286 if (c_parser_next_token_is (parser, CPP_EQ)
1287 || c_parser_next_token_is (parser, CPP_COMMA)
1288 || c_parser_next_token_is (parser, CPP_SEMICOLON)
1289 || c_parser_next_token_is_keyword (parser, RID_ASM)
1290 || c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
1292 tree asm_name = NULL_TREE;
1293 tree postfix_attrs = NULL_TREE;
1294 if (!diagnosed_no_specs && !specs->declspecs_seen_p)
1296 diagnosed_no_specs = true;
1297 pedwarn ("data definition has no type or storage class");
1299 /* Having seen a data definition, there cannot now be a
1300 function definition. */
1302 if (c_parser_next_token_is_keyword (parser, RID_ASM))
1303 asm_name = c_parser_simple_asm_expr (parser);
1304 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
1305 postfix_attrs = c_parser_attributes (parser);
1306 if (c_parser_next_token_is (parser, CPP_EQ))
1310 c_parser_consume_token (parser);
1311 /* The declaration of the variable is in effect while
1312 its initializer is parsed. */
1313 d = start_decl (declarator, specs, true,
1314 chainon (postfix_attrs, all_prefix_attrs));
1316 d = error_mark_node;
1317 start_init (d, asm_name, global_bindings_p ());
1318 init = c_parser_initializer (parser);
1320 if (d != error_mark_node)
1322 maybe_warn_string_init (TREE_TYPE (d), init);
1323 finish_decl (d, init.value, asm_name);
1328 tree d = start_decl (declarator, specs, false,
1329 chainon (postfix_attrs,
1332 finish_decl (d, NULL_TREE, asm_name);
1334 if (c_parser_next_token_is (parser, CPP_COMMA))
1336 c_parser_consume_token (parser);
1337 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
1338 all_prefix_attrs = chainon (c_parser_attributes (parser),
1341 all_prefix_attrs = prefix_attrs;
1344 else if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1346 c_parser_consume_token (parser);
1351 c_parser_error (parser, "expected %<,%> or %<;%>");
1352 c_parser_skip_to_end_of_block_or_statement (parser);
1358 c_parser_error (parser, "expected %<=%>, %<,%>, %<;%>, "
1359 "%<asm%> or %<__attribute__%>");
1360 c_parser_skip_to_end_of_block_or_statement (parser);
1363 /* Function definition (nested or otherwise). */
1367 pedwarn ("ISO C forbids nested functions");
1368 push_function_context ();
1370 if (!start_function (specs, declarator, all_prefix_attrs))
1372 /* This can appear in many cases looking nothing like a
1373 function definition, so we don't give a more specific
1374 error suggesting there was one. */
1375 c_parser_error (parser, "expected %<=%>, %<,%>, %<;%>, %<asm%> "
1376 "or %<__attribute__%>");
1378 pop_function_context ();
1381 /* Parse old-style parameter declarations. ??? Attributes are
1382 not allowed to start declaration specifiers here because of a
1383 syntax conflict between a function declaration with attribute
1384 suffix and a function definition with an attribute prefix on
1385 first old-style parameter declaration. Following the old
1386 parser, they are not accepted on subsequent old-style
1387 parameter declarations either. However, there is no
1388 ambiguity after the first declaration, nor indeed on the
1389 first as long as we don't allow postfix attributes after a
1390 declarator with a nonempty identifier list in a definition;
1391 and postfix attributes have never been accepted here in
1392 function definitions either. */
1393 while (c_parser_next_token_is_not (parser, CPP_EOF)
1394 && c_parser_next_token_is_not (parser, CPP_OPEN_BRACE))
1395 c_parser_declaration_or_fndef (parser, false, false, true, false);
1396 DECL_SOURCE_LOCATION (current_function_decl)
1397 = c_parser_peek_token (parser)->location;
1398 store_parm_decls ();
1399 fnbody = c_parser_compound_statement (parser);
1402 tree decl = current_function_decl;
1405 pop_function_context ();
1406 add_stmt (build_stmt (DECL_EXPR, decl));
1417 /* Parse an asm-definition (asm() outside a function body). This is a
1425 c_parser_asm_definition (c_parser *parser)
1427 tree asm_str = c_parser_simple_asm_expr (parser);
1429 cgraph_add_asm_node (asm_str);
1430 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
1433 /* Parse some declaration specifiers (possibly none) (C90 6.5, C99
1434 6.7), adding them to SPECS (which may already include some).
1435 Storage class specifiers are accepted iff SCSPEC_OK; type
1436 specifiers are accepted iff TYPESPEC_OK; attributes are accepted at
1437 the start iff START_ATTR_OK.
1439 declaration-specifiers:
1440 storage-class-specifier declaration-specifiers[opt]
1441 type-specifier declaration-specifiers[opt]
1442 type-qualifier declaration-specifiers[opt]
1443 function-specifier declaration-specifiers[opt]
1445 Function specifiers (inline) are from C99, and are currently
1446 handled as storage class specifiers, as is __thread.
1448 C90 6.5.1, C99 6.7.1:
1449 storage-class-specifier:
1460 C90 6.5.2, C99 6.7.2:
1473 [_Imaginary removed in C99 TC2]
1474 struct-or-union-specifier
1478 (_Bool and _Complex are new in C99.)
1480 C90 6.5.3, C99 6.7.3:
1487 (restrict is new in C99.)
1491 declaration-specifiers:
1492 attributes declaration-specifiers[opt]
1494 storage-class-specifier:
1506 class-name objc-protocol-refs[opt]
1507 typedef-name objc-protocol-refs
1512 c_parser_declspecs (c_parser *parser, struct c_declspecs *specs,
1513 bool scspec_ok, bool typespec_ok, bool start_attr_ok)
1515 bool attrs_ok = start_attr_ok;
1516 bool seen_type = specs->type_seen_p;
1517 while (c_parser_next_token_is (parser, CPP_NAME)
1518 || c_parser_next_token_is (parser, CPP_KEYWORD)
1519 || (c_dialect_objc () && c_parser_next_token_is (parser, CPP_LESS)))
1521 struct c_typespec t;
1523 if (c_parser_next_token_is (parser, CPP_NAME))
1525 tree value = c_parser_peek_token (parser)->value;
1526 c_id_kind kind = c_parser_peek_token (parser)->id_kind;
1527 /* This finishes the specifiers unless a type name is OK, it
1528 is declared as a type name and a type name hasn't yet
1530 if (!typespec_ok || seen_type
1531 || (kind != C_ID_TYPENAME && kind != C_ID_CLASSNAME))
1533 c_parser_consume_token (parser);
1536 if (kind == C_ID_TYPENAME
1537 && (!c_dialect_objc ()
1538 || c_parser_next_token_is_not (parser, CPP_LESS)))
1540 t.kind = ctsk_typedef;
1541 /* For a typedef name, record the meaning, not the name.
1542 In case of 'foo foo, bar;'. */
1543 t.spec = lookup_name (value);
1547 tree proto = NULL_TREE;
1548 gcc_assert (c_dialect_objc ());
1550 if (c_parser_next_token_is (parser, CPP_LESS))
1551 proto = c_parser_objc_protocol_refs (parser);
1552 t.spec = objc_get_protocol_qualified_type (value, proto);
1554 declspecs_add_type (specs, t);
1557 if (c_parser_next_token_is (parser, CPP_LESS))
1559 /* Make "<SomeProtocol>" equivalent to "id <SomeProtocol>" -
1560 nisse@lysator.liu.se. */
1562 gcc_assert (c_dialect_objc ());
1563 if (!typespec_ok || seen_type)
1565 proto = c_parser_objc_protocol_refs (parser);
1567 t.spec = objc_get_protocol_qualified_type (NULL_TREE, proto);
1568 declspecs_add_type (specs, t);
1571 gcc_assert (c_parser_next_token_is (parser, CPP_KEYWORD));
1572 switch (c_parser_peek_token (parser)->keyword)
1584 /* TODO: Distinguish between function specifiers (inline)
1585 and storage class specifiers, either here or in
1586 declspecs_add_scspec. */
1587 declspecs_add_scspec (specs, c_parser_peek_token (parser)->value);
1588 c_parser_consume_token (parser);
1608 OBJC_NEED_RAW_IDENTIFIER (1);
1609 t.kind = ctsk_resword;
1610 t.spec = c_parser_peek_token (parser)->value;
1611 declspecs_add_type (specs, t);
1612 c_parser_consume_token (parser);
1619 t = c_parser_enum_specifier (parser);
1620 declspecs_add_type (specs, t);
1628 t = c_parser_struct_or_union_specifier (parser);
1629 declspecs_add_type (specs, t);
1632 /* ??? The old parser rejected typeof after other type
1633 specifiers, but is a syntax error the best way of
1635 if (!typespec_ok || seen_type)
1639 t = c_parser_typeof_specifier (parser);
1640 declspecs_add_type (specs, t);
1646 declspecs_add_qual (specs, c_parser_peek_token (parser)->value);
1647 c_parser_consume_token (parser);
1652 attrs = c_parser_attributes (parser);
1653 declspecs_add_attrs (specs, attrs);
1662 /* Parse an enum specifier (C90 6.5.2.2, C99 6.7.2.2).
1665 enum attributes[opt] identifier[opt] { enumerator-list } attributes[opt]
1666 enum attributes[opt] identifier[opt] { enumerator-list , } attributes[opt]
1667 enum attributes[opt] identifier
1669 The form with trailing comma is new in C99. The forms with
1670 attributes are GNU extensions. In GNU C, we accept any expression
1671 without commas in the syntax (assignment expressions, not just
1672 conditional expressions); assignment expressions will be diagnosed
1677 enumerator-list , enumerator
1680 enumeration-constant
1681 enumeration-constant = constant-expression
1684 static struct c_typespec
1685 c_parser_enum_specifier (c_parser *parser)
1687 struct c_typespec ret;
1689 tree ident = NULL_TREE;
1690 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ENUM));
1691 c_parser_consume_token (parser);
1692 attrs = c_parser_attributes (parser);
1693 if (c_parser_next_token_is (parser, CPP_NAME))
1695 ident = c_parser_peek_token (parser)->value;
1696 c_parser_consume_token (parser);
1698 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
1700 /* Parse an enum definition. */
1701 struct c_enum_contents the_enum;
1702 tree type = start_enum (&the_enum, ident);
1704 /* We chain the enumerators in reverse order, then put them in
1705 forward order at the end. */
1706 tree values = NULL_TREE;
1707 c_parser_consume_token (parser);
1714 if (c_parser_next_token_is_not (parser, CPP_NAME))
1716 c_parser_error (parser, "expected identifier");
1717 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
1718 values = error_mark_node;
1721 enum_id = c_parser_peek_token (parser)->value;
1722 c_parser_consume_token (parser);
1723 if (c_parser_next_token_is (parser, CPP_EQ))
1725 c_parser_consume_token (parser);
1726 enum_value = c_parser_expr_no_commas (parser, NULL).value;
1729 enum_value = NULL_TREE;
1730 enum_decl = build_enumerator (&the_enum, enum_id, enum_value);
1731 TREE_CHAIN (enum_decl) = values;
1734 if (c_parser_next_token_is (parser, CPP_COMMA))
1737 c_parser_consume_token (parser);
1739 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
1741 if (seen_comma && pedantic && !flag_isoc99)
1742 pedwarn ("comma at end of enumerator list");
1743 c_parser_consume_token (parser);
1748 c_parser_error (parser, "expected %<,%> or %<}%>");
1749 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
1750 values = error_mark_node;
1754 postfix_attrs = c_parser_attributes (parser);
1755 ret.spec = finish_enum (type, nreverse (values),
1756 chainon (attrs, postfix_attrs));
1757 ret.kind = ctsk_tagdef;
1762 c_parser_error (parser, "expected %<{%>");
1763 ret.spec = error_mark_node;
1764 ret.kind = ctsk_tagref;
1767 ret = parser_xref_tag (ENUMERAL_TYPE, ident);
1768 /* In ISO C, enumerated types can be referred to only if already
1770 if (pedantic && !COMPLETE_TYPE_P (ret.spec))
1771 pedwarn ("ISO C forbids forward references to %<enum%> types");
1775 /* Parse a struct or union specifier (C90 6.5.2.1, C99 6.7.2.1).
1777 struct-or-union-specifier:
1778 struct-or-union attributes[opt] identifier[opt]
1779 { struct-contents } attributes[opt]
1780 struct-or-union attributes[opt] identifier
1783 struct-declaration-list
1785 struct-declaration-list:
1786 struct-declaration ;
1787 struct-declaration-list struct-declaration ;
1794 struct-declaration-list struct-declaration
1796 struct-declaration-list:
1797 struct-declaration-list ;
1800 (Note that in the syntax here, unlike that in ISO C, the semicolons
1801 are included here rather than in struct-declaration, in order to
1802 describe the syntax with extra semicolons and missing semicolon at
1807 struct-declaration-list:
1808 @defs ( class-name )
1810 (Note this does not include a trailing semicolon, but can be
1811 followed by further declarations, and gets a pedwarn-if-pedantic
1812 when followed by a semicolon.) */
1814 static struct c_typespec
1815 c_parser_struct_or_union_specifier (c_parser *parser)
1817 struct c_typespec ret;
1819 tree ident = NULL_TREE;
1820 enum tree_code code;
1821 switch (c_parser_peek_token (parser)->keyword)
1832 c_parser_consume_token (parser);
1833 attrs = c_parser_attributes (parser);
1834 if (c_parser_next_token_is (parser, CPP_NAME))
1836 ident = c_parser_peek_token (parser)->value;
1837 c_parser_consume_token (parser);
1839 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
1841 /* Parse a struct or union definition. Start the scope of the
1842 tag before parsing components. */
1843 tree type = start_struct (code, ident);
1845 /* We chain the components in reverse order, then put them in
1846 forward order at the end. Each struct-declaration may
1847 declare multiple components (comma-separated), so we must use
1848 chainon to join them, although when parsing each
1849 struct-declaration we can use TREE_CHAIN directly.
1851 The theory behind all this is that there will be more
1852 semicolon separated fields than comma separated fields, and
1853 so we'll be minimizing the number of node traversals required
1855 tree contents = NULL_TREE;
1856 c_parser_consume_token (parser);
1857 /* Handle the Objective-C @defs construct,
1858 e.g. foo(sizeof(struct{ @defs(ClassName) }));. */
1859 if (c_parser_next_token_is_keyword (parser, RID_AT_DEFS))
1862 gcc_assert (c_dialect_objc ());
1863 c_parser_consume_token (parser);
1864 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
1866 if (c_parser_next_token_is (parser, CPP_NAME)
1867 && c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME)
1869 name = c_parser_peek_token (parser)->value;
1870 c_parser_consume_token (parser);
1874 c_parser_error (parser, "expected class name");
1875 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
1878 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
1880 contents = nreverse (objc_get_class_ivars (name));
1883 /* Parse the struct-declarations and semicolons. Problems with
1884 semicolons are diagnosed here; empty structures are diagnosed
1889 /* Parse any stray semicolon. */
1890 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1893 pedwarn ("extra semicolon in struct or union specified");
1894 c_parser_consume_token (parser);
1897 /* Stop if at the end of the struct or union contents. */
1898 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
1900 c_parser_consume_token (parser);
1903 /* Accept #pragmas at struct scope. */
1904 if (c_parser_next_token_is (parser, CPP_PRAGMA))
1906 c_parser_pragma (parser, pragma_external);
1909 /* Parse some comma-separated declarations, but not the
1910 trailing semicolon if any. */
1911 decls = c_parser_struct_declaration (parser);
1912 contents = chainon (decls, contents);
1913 /* If no semicolon follows, either we have a parse error or
1914 are at the end of the struct or union and should
1916 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1917 c_parser_consume_token (parser);
1920 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
1921 pedwarn ("no semicolon at end of struct or union");
1924 c_parser_error (parser, "expected %<;%>");
1925 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
1930 postfix_attrs = c_parser_attributes (parser);
1931 ret.spec = finish_struct (type, nreverse (contents),
1932 chainon (attrs, postfix_attrs));
1933 ret.kind = ctsk_tagdef;
1938 c_parser_error (parser, "expected %<{%>");
1939 ret.spec = error_mark_node;
1940 ret.kind = ctsk_tagref;
1943 ret = parser_xref_tag (code, ident);
1947 /* Parse a struct-declaration (C90 6.5.2.1, C99 6.7.2.1), *without*
1948 the trailing semicolon.
1951 specifier-qualifier-list struct-declarator-list
1953 specifier-qualifier-list:
1954 type-specifier specifier-qualifier-list[opt]
1955 type-qualifier specifier-qualifier-list[opt]
1956 attributes specifier-qualifier-list[opt]
1958 struct-declarator-list:
1960 struct-declarator-list , attributes[opt] struct-declarator
1963 declarator attributes[opt]
1964 declarator[opt] : constant-expression attributes[opt]
1969 __extension__ struct-declaration
1970 specifier-qualifier-list
1972 Unlike the ISO C syntax, semicolons are handled elsewhere. The use
1973 of attributes where shown is a GNU extension. In GNU C, we accept
1974 any expression without commas in the syntax (assignment
1975 expressions, not just conditional expressions); assignment
1976 expressions will be diagnosed as non-constant. */
1979 c_parser_struct_declaration (c_parser *parser)
1981 struct c_declspecs *specs;
1983 tree all_prefix_attrs;
1985 if (c_parser_next_token_is_keyword (parser, RID_EXTENSION))
1989 ext = disable_extension_diagnostics ();
1990 c_parser_consume_token (parser);
1991 decl = c_parser_struct_declaration (parser);
1992 restore_extension_diagnostics (ext);
1995 specs = build_null_declspecs ();
1996 c_parser_declspecs (parser, specs, false, true, true);
1999 if (!specs->declspecs_seen_p)
2001 c_parser_error (parser, "expected specifier-qualifier-list");
2004 finish_declspecs (specs);
2005 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
2008 if (!specs->type_seen_p)
2011 pedwarn ("ISO C forbids member declarations with no members");
2012 shadow_tag_warned (specs, pedantic);
2017 /* Support for unnamed structs or unions as members of
2018 structs or unions (which is [a] useful and [b] supports
2020 ret = grokfield (build_id_declarator (NULL_TREE), specs, NULL_TREE);
2024 pending_xref_error ();
2025 prefix_attrs = specs->attrs;
2026 all_prefix_attrs = prefix_attrs;
2027 specs->attrs = NULL_TREE;
2031 /* Declaring one or more declarators or un-named bit-fields. */
2032 struct c_declarator *declarator;
2034 if (c_parser_next_token_is (parser, CPP_COLON))
2035 declarator = build_id_declarator (NULL_TREE);
2037 declarator = c_parser_declarator (parser, specs->type_seen_p,
2038 C_DTR_NORMAL, &dummy);
2039 if (declarator == NULL)
2041 c_parser_skip_to_end_of_block_or_statement (parser);
2044 if (c_parser_next_token_is (parser, CPP_COLON)
2045 || c_parser_next_token_is (parser, CPP_COMMA)
2046 || c_parser_next_token_is (parser, CPP_SEMICOLON)
2047 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE)
2048 || c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2050 tree postfix_attrs = NULL_TREE;
2051 tree width = NULL_TREE;
2053 if (c_parser_next_token_is (parser, CPP_COLON))
2055 c_parser_consume_token (parser);
2056 width = c_parser_expr_no_commas (parser, NULL).value;
2058 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2059 postfix_attrs = c_parser_attributes (parser);
2060 d = grokfield (declarator, specs, width);
2061 decl_attributes (&d, chainon (postfix_attrs,
2062 all_prefix_attrs), 0);
2063 TREE_CHAIN (d) = decls;
2065 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2066 all_prefix_attrs = chainon (c_parser_attributes (parser),
2069 all_prefix_attrs = prefix_attrs;
2070 if (c_parser_next_token_is (parser, CPP_COMMA))
2071 c_parser_consume_token (parser);
2072 else if (c_parser_next_token_is (parser, CPP_SEMICOLON)
2073 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2075 /* Semicolon consumed in caller. */
2080 c_parser_error (parser, "expected %<,%>, %<;%> or %<}%>");
2086 c_parser_error (parser,
2087 "expected %<:%>, %<,%>, %<;%>, %<}%> or "
2088 "%<__attribute__%>");
2095 /* Parse a typeof specifier (a GNU extension).
2098 typeof ( expression )
2099 typeof ( type-name )
2102 static struct c_typespec
2103 c_parser_typeof_specifier (c_parser *parser)
2105 struct c_typespec ret;
2106 ret.kind = ctsk_typeof;
2107 ret.spec = error_mark_node;
2108 gcc_assert (c_parser_next_token_is_keyword (parser, RID_TYPEOF));
2109 c_parser_consume_token (parser);
2112 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2118 if (c_parser_next_token_starts_typename (parser))
2120 struct c_type_name *type = c_parser_type_name (parser);
2125 ret.spec = groktypename (type);
2126 pop_maybe_used (variably_modified_type_p (ret.spec, NULL_TREE));
2132 struct c_expr expr = c_parser_expression (parser);
2135 if (TREE_CODE (expr.value) == COMPONENT_REF
2136 && DECL_C_BIT_FIELD (TREE_OPERAND (expr.value, 1)))
2137 error ("%<typeof%> applied to a bit-field");
2138 ret.spec = TREE_TYPE (expr.value);
2139 was_vm = variably_modified_type_p (ret.spec, NULL_TREE);
2140 /* This should be returned with the type so that when the type
2141 is evaluated, this can be evaluated. For now, we avoid
2142 evaluation when the context might. */
2143 if (!skip_evaluation && was_vm)
2145 tree e = expr.value;
2147 /* If the expression is not of a type to which we cannot assign a line
2148 number, wrap the thing in a no-op NOP_EXPR. */
2149 if (DECL_P (e) || CONSTANT_CLASS_P (e))
2150 e = build1 (NOP_EXPR, void_type_node, e);
2152 if (CAN_HAVE_LOCATION_P (e))
2153 SET_EXPR_LOCATION (e, input_location);
2157 pop_maybe_used (was_vm);
2159 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
2163 /* Parse a declarator, possibly an abstract declarator (C90 6.5.4,
2164 6.5.5, C99 6.7.5, 6.7.6). If TYPE_SEEN_P then a typedef name may
2165 be redeclared; otherwise it may not. KIND indicates which kind of
2166 declarator is wanted. Returns a valid declarator except in the
2167 case of a syntax error in which case NULL is returned. *SEEN_ID is
2168 set to true if an identifier being declared is seen; this is used
2169 to diagnose bad forms of abstract array declarators and to
2170 determine whether an identifier list is syntactically permitted.
2173 pointer[opt] direct-declarator
2177 ( attributes[opt] declarator )
2178 direct-declarator array-declarator
2179 direct-declarator ( parameter-type-list )
2180 direct-declarator ( identifier-list[opt] )
2183 * type-qualifier-list[opt]
2184 * type-qualifier-list[opt] pointer
2186 type-qualifier-list:
2189 type-qualifier-list type-qualifier
2190 type-qualifier-list attributes
2192 parameter-type-list:
2194 parameter-list , ...
2197 parameter-declaration
2198 parameter-list , parameter-declaration
2200 parameter-declaration:
2201 declaration-specifiers declarator attributes[opt]
2202 declaration-specifiers abstract-declarator[opt] attributes[opt]
2206 identifier-list , identifier
2208 abstract-declarator:
2210 pointer[opt] direct-abstract-declarator
2212 direct-abstract-declarator:
2213 ( attributes[opt] abstract-declarator )
2214 direct-abstract-declarator[opt] array-declarator
2215 direct-abstract-declarator[opt] ( parameter-type-list[opt] )
2220 direct-declarator ( parameter-forward-declarations
2221 parameter-type-list[opt] )
2223 direct-abstract-declarator:
2224 direct-abstract-declarator[opt] ( parameter-forward-declarations
2225 parameter-type-list[opt] )
2227 parameter-forward-declarations:
2229 parameter-forward-declarations parameter-list ;
2231 The uses of attributes shown above are GNU extensions.
2233 Some forms of array declarator are not included in C99 in the
2234 syntax for abstract declarators; these are disallowed elsewhere.
2235 This may be a defect (DR#289).
2237 This function also accepts an omitted abstract declarator as being
2238 an abstract declarator, although not part of the formal syntax. */
2240 static struct c_declarator *
2241 c_parser_declarator (c_parser *parser, bool type_seen_p, c_dtr_syn kind,
2244 /* Parse any initial pointer part. */
2245 if (c_parser_next_token_is (parser, CPP_MULT))
2247 struct c_declspecs *quals_attrs = build_null_declspecs ();
2248 struct c_declarator *inner;
2249 c_parser_consume_token (parser);
2250 c_parser_declspecs (parser, quals_attrs, false, false, true);
2251 inner = c_parser_declarator (parser, type_seen_p, kind, seen_id);
2255 return make_pointer_declarator (quals_attrs, inner);
2257 /* Now we have a direct declarator, direct abstract declarator or
2258 nothing (which counts as a direct abstract declarator here). */
2259 return c_parser_direct_declarator (parser, type_seen_p, kind, seen_id);
2262 /* Parse a direct declarator or direct abstract declarator; arguments
2263 as c_parser_declarator. */
2265 static struct c_declarator *
2266 c_parser_direct_declarator (c_parser *parser, bool type_seen_p, c_dtr_syn kind,
2269 /* The direct declarator must start with an identifier (possibly
2270 omitted) or a parenthesized declarator (possibly abstract). In
2271 an ordinary declarator, initial parentheses must start a
2272 parenthesized declarator. In an abstract declarator or parameter
2273 declarator, they could start a parenthesized declarator or a
2274 parameter list. To tell which, the open parenthesis and any
2275 following attributes must be read. If a declaration specifier
2276 follows, then it is a parameter list; if the specifier is a
2277 typedef name, there might be an ambiguity about redeclaring it,
2278 which is resolved in the direction of treating it as a typedef
2279 name. If a close parenthesis follows, it is also an empty
2280 parameter list, as the syntax does not permit empty abstract
2281 declarators. Otherwise, it is a parenthesized declarator (in
2282 which case the analysis may be repeated inside it, recursively).
2284 ??? There is an ambiguity in a parameter declaration "int
2285 (__attribute__((foo)) x)", where x is not a typedef name: it
2286 could be an abstract declarator for a function, or declare x with
2287 parentheses. The proper resolution of this ambiguity needs
2288 documenting. At present we follow an accident of the old
2289 parser's implementation, whereby the first parameter must have
2290 some declaration specifiers other than just attributes. Thus as
2291 a parameter declaration it is treated as a parenthesized
2292 parameter named x, and as an abstract declarator it is
2295 ??? Also following the old parser, attributes inside an empty
2296 parameter list are ignored, making it a list not yielding a
2297 prototype, rather than giving an error or making it have one
2298 parameter with implicit type int.
2300 ??? Also following the old parser, typedef names may be
2301 redeclared in declarators, but not Objective-C class names. */
2303 if (kind != C_DTR_ABSTRACT
2304 && c_parser_next_token_is (parser, CPP_NAME)
2306 && c_parser_peek_token (parser)->id_kind == C_ID_TYPENAME)
2307 || c_parser_peek_token (parser)->id_kind == C_ID_ID))
2309 struct c_declarator *inner
2310 = build_id_declarator (c_parser_peek_token (parser)->value);
2312 inner->id_loc = c_parser_peek_token (parser)->location;
2313 c_parser_consume_token (parser);
2314 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
2317 if (kind != C_DTR_NORMAL
2318 && c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
2320 struct c_declarator *inner = build_id_declarator (NULL_TREE);
2321 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
2324 /* Either we are at the end of an abstract declarator, or we have
2327 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
2330 struct c_declarator *inner;
2331 c_parser_consume_token (parser);
2332 attrs = c_parser_attributes (parser);
2333 if (kind != C_DTR_NORMAL
2334 && (c_parser_next_token_starts_declspecs (parser)
2335 || c_parser_next_token_is (parser, CPP_CLOSE_PAREN)))
2337 struct c_arg_info *args
2338 = c_parser_parms_declarator (parser, kind == C_DTR_NORMAL,
2345 = build_function_declarator (args,
2346 build_id_declarator (NULL_TREE));
2347 return c_parser_direct_declarator_inner (parser, *seen_id,
2351 /* A parenthesized declarator. */
2352 inner = c_parser_declarator (parser, type_seen_p, kind, seen_id);
2353 if (inner != NULL && attrs != NULL)
2354 inner = build_attrs_declarator (attrs, inner);
2355 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2357 c_parser_consume_token (parser);
2361 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
2365 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2372 if (kind == C_DTR_NORMAL)
2374 c_parser_error (parser, "expected identifier or %<(%>");
2378 return build_id_declarator (NULL_TREE);
2382 /* Parse part of a direct declarator or direct abstract declarator,
2383 given that some (in INNER) has already been parsed; ID_PRESENT is
2384 true if an identifier is present, false for an abstract
2387 static struct c_declarator *
2388 c_parser_direct_declarator_inner (c_parser *parser, bool id_present,
2389 struct c_declarator *inner)
2391 /* Parse a sequence of array declarators and parameter lists. */
2392 if (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
2394 struct c_declarator *declarator;
2395 struct c_declspecs *quals_attrs = build_null_declspecs ();
2399 c_parser_consume_token (parser);
2400 c_parser_declspecs (parser, quals_attrs, false, false, true);
2401 static_seen = c_parser_next_token_is_keyword (parser, RID_STATIC);
2403 c_parser_consume_token (parser);
2404 if (static_seen && !quals_attrs->declspecs_seen_p)
2405 c_parser_declspecs (parser, quals_attrs, false, false, true);
2406 if (!quals_attrs->declspecs_seen_p)
2408 /* If "static" is present, there must be an array dimension.
2409 Otherwise, there may be a dimension, "*", or no
2414 dimen = c_parser_expr_no_commas (parser, NULL).value;
2418 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
2423 else if (c_parser_next_token_is (parser, CPP_MULT))
2425 if (c_parser_peek_2nd_token (parser)->type == CPP_CLOSE_SQUARE)
2429 c_parser_consume_token (parser);
2434 dimen = c_parser_expr_no_commas (parser, NULL).value;
2440 dimen = c_parser_expr_no_commas (parser, NULL).value;
2443 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
2444 c_parser_consume_token (parser);
2447 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
2451 declarator = build_array_declarator (dimen, quals_attrs, static_seen,
2453 if (declarator == NULL)
2455 inner = set_array_declarator_inner (declarator, inner, !id_present);
2456 return c_parser_direct_declarator_inner (parser, id_present, inner);
2458 else if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
2461 struct c_arg_info *args;
2462 c_parser_consume_token (parser);
2463 attrs = c_parser_attributes (parser);
2464 args = c_parser_parms_declarator (parser, id_present, attrs);
2469 inner = build_function_declarator (args, inner);
2470 return c_parser_direct_declarator_inner (parser, id_present, inner);
2476 /* Parse a parameter list or identifier list, including the closing
2477 parenthesis but not the opening one. ATTRS are the attributes at
2478 the start of the list. ID_LIST_OK is true if an identifier list is
2479 acceptable; such a list must not have attributes at the start. */
2481 static struct c_arg_info *
2482 c_parser_parms_declarator (c_parser *parser, bool id_list_ok, tree attrs)
2485 declare_parm_level ();
2486 /* If the list starts with an identifier, it is an identifier list.
2487 Otherwise, it is either a prototype list or an empty list. */
2490 && c_parser_next_token_is (parser, CPP_NAME)
2491 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
2493 tree list = NULL_TREE, *nextp = &list;
2494 while (c_parser_next_token_is (parser, CPP_NAME)
2495 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
2497 *nextp = build_tree_list (NULL_TREE,
2498 c_parser_peek_token (parser)->value);
2499 nextp = & TREE_CHAIN (*nextp);
2500 c_parser_consume_token (parser);
2501 if (c_parser_next_token_is_not (parser, CPP_COMMA))
2503 c_parser_consume_token (parser);
2504 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2506 c_parser_error (parser, "expected identifier");
2510 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2512 struct c_arg_info *ret = XOBNEW (&parser_obstack, struct c_arg_info);
2517 ret->pending_sizes = 0;
2518 ret->had_vla_unspec = 0;
2519 c_parser_consume_token (parser);
2525 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2533 struct c_arg_info *ret = c_parser_parms_list_declarator (parser, attrs);
2539 /* Parse a parameter list (possibly empty), including the closing
2540 parenthesis but not the opening one. ATTRS are the attributes at
2541 the start of the list. */
2543 static struct c_arg_info *
2544 c_parser_parms_list_declarator (c_parser *parser, tree attrs)
2546 bool good_parm = false;
2547 /* ??? Following the old parser, forward parameter declarations may
2548 use abstract declarators, and if no real parameter declarations
2549 follow the forward declarations then this is not diagnosed. Also
2550 note as above that attributes are ignored as the only contents of
2551 the parentheses, or as the only contents after forward
2553 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2555 struct c_arg_info *ret = XOBNEW (&parser_obstack, struct c_arg_info);
2560 ret->pending_sizes = 0;
2561 ret->had_vla_unspec = 0;
2562 c_parser_consume_token (parser);
2565 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
2567 struct c_arg_info *ret = XOBNEW (&parser_obstack, struct c_arg_info);
2571 ret->pending_sizes = 0;
2572 ret->had_vla_unspec = 0;
2573 /* Suppress -Wold-style-definition for this case. */
2574 ret->types = error_mark_node;
2575 error ("ISO C requires a named argument before %<...%>");
2576 c_parser_consume_token (parser);
2577 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2579 c_parser_consume_token (parser);
2584 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2589 /* Nonempty list of parameters, either terminated with semicolon
2590 (forward declarations; recurse) or with close parenthesis (normal
2591 function) or with ", ... )" (variadic function). */
2594 /* Parse a parameter. */
2595 struct c_parm *parm = c_parser_parameter_declaration (parser, attrs);
2600 push_parm_decl (parm);
2602 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
2605 c_parser_consume_token (parser);
2606 mark_forward_parm_decls ();
2607 new_attrs = c_parser_attributes (parser);
2608 return c_parser_parms_list_declarator (parser, new_attrs);
2610 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2612 c_parser_consume_token (parser);
2614 return get_parm_info (false);
2617 struct c_arg_info *ret
2618 = XOBNEW (&parser_obstack, struct c_arg_info);
2623 ret->pending_sizes = 0;
2624 ret->had_vla_unspec = 0;
2628 if (!c_parser_require (parser, CPP_COMMA,
2629 "expected %<;%>, %<,%> or %<)%>"))
2631 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
2634 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
2636 c_parser_consume_token (parser);
2637 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2639 c_parser_consume_token (parser);
2641 return get_parm_info (true);
2644 struct c_arg_info *ret
2645 = XOBNEW (&parser_obstack, struct c_arg_info);
2650 ret->pending_sizes = 0;
2651 ret->had_vla_unspec = 0;
2657 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2665 /* Parse a parameter declaration. ATTRS are the attributes at the
2666 start of the declaration if it is the first parameter. */
2668 static struct c_parm *
2669 c_parser_parameter_declaration (c_parser *parser, tree attrs)
2671 struct c_declspecs *specs;
2672 struct c_declarator *declarator;
2674 tree postfix_attrs = NULL_TREE;
2676 if (!c_parser_next_token_starts_declspecs (parser))
2678 /* ??? In some Objective-C cases '...' isn't applicable so there
2679 should be a different message. */
2680 c_parser_error (parser,
2681 "expected declaration specifiers or %<...%>");
2682 c_parser_skip_to_end_of_parameter (parser);
2685 specs = build_null_declspecs ();
2688 declspecs_add_attrs (specs, attrs);
2691 c_parser_declspecs (parser, specs, true, true, true);
2692 finish_declspecs (specs);
2693 pending_xref_error ();
2694 prefix_attrs = specs->attrs;
2695 specs->attrs = NULL_TREE;
2696 declarator = c_parser_declarator (parser, specs->type_seen_p,
2697 C_DTR_PARM, &dummy);
2698 if (declarator == NULL)
2700 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
2703 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2704 postfix_attrs = c_parser_attributes (parser);
2705 return build_c_parm (specs, chainon (postfix_attrs, prefix_attrs),
2709 /* Parse a string literal in an asm expression. It should not be
2710 translated, and wide string literals are an error although
2711 permitted by the syntax. This is a GNU extension.
2716 ??? At present, following the old parser, the caller needs to have
2717 set c_lex_string_translate to 0. It would be better to follow the
2718 C++ parser rather than using the c_lex_string_translate kludge. */
2721 c_parser_asm_string_literal (c_parser *parser)
2724 if (c_parser_next_token_is (parser, CPP_STRING))
2726 str = c_parser_peek_token (parser)->value;
2727 c_parser_consume_token (parser);
2729 else if (c_parser_next_token_is (parser, CPP_WSTRING))
2731 error ("wide string literal in %<asm%>");
2732 str = build_string (1, "");
2733 c_parser_consume_token (parser);
2737 c_parser_error (parser, "expected string literal");
2743 /* Parse a simple asm expression. This is used in restricted
2744 contexts, where a full expression with inputs and outputs does not
2745 make sense. This is a GNU extension.
2748 asm ( asm-string-literal )
2752 c_parser_simple_asm_expr (c_parser *parser)
2755 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ASM));
2756 /* ??? Follow the C++ parser rather than using the
2757 c_lex_string_translate kludge. */
2758 c_lex_string_translate = 0;
2759 c_parser_consume_token (parser);
2760 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2762 c_lex_string_translate = 1;
2765 str = c_parser_asm_string_literal (parser);
2766 c_lex_string_translate = 1;
2767 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
2769 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
2775 /* Parse (possibly empty) attributes. This is a GNU extension.
2779 attributes attribute
2782 __attribute__ ( ( attribute-list ) )
2786 attribute_list , attrib
2791 any-word ( identifier )
2792 any-word ( identifier , nonempty-expr-list )
2793 any-word ( expr-list )
2795 where the "identifier" must not be declared as a type, and
2796 "any-word" may be any identifier (including one declared as a
2797 type), a reserved word storage class specifier, type specifier or
2798 type qualifier. ??? This still leaves out most reserved keywords
2799 (following the old parser), shouldn't we include them, and why not
2800 allow identifiers declared as types to start the arguments? */
2803 c_parser_attributes (c_parser *parser)
2805 tree attrs = NULL_TREE;
2806 while (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2808 /* ??? Follow the C++ parser rather than using the
2809 c_lex_string_translate kludge. */
2810 c_lex_string_translate = 0;
2811 c_parser_consume_token (parser);
2812 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2814 c_lex_string_translate = 1;
2817 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2819 c_lex_string_translate = 1;
2820 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
2823 /* Parse the attribute list. */
2824 while (c_parser_next_token_is (parser, CPP_COMMA)
2825 || c_parser_next_token_is (parser, CPP_NAME)
2826 || c_parser_next_token_is (parser, CPP_KEYWORD))
2828 tree attr, attr_name, attr_args;
2829 if (c_parser_next_token_is (parser, CPP_COMMA))
2831 c_parser_consume_token (parser);
2834 if (c_parser_next_token_is (parser, CPP_KEYWORD))
2836 /* ??? See comment above about what keywords are
2839 switch (c_parser_peek_token (parser)->keyword)
2874 attr_name = c_parser_peek_token (parser)->value;
2875 c_parser_consume_token (parser);
2876 if (c_parser_next_token_is_not (parser, CPP_OPEN_PAREN))
2878 attr = build_tree_list (attr_name, NULL_TREE);
2879 attrs = chainon (attrs, attr);
2882 c_parser_consume_token (parser);
2883 /* Parse the attribute contents. If they start with an
2884 identifier which is followed by a comma or close
2885 parenthesis, then the arguments start with that
2886 identifier; otherwise they are an expression list. */
2887 if (c_parser_next_token_is (parser, CPP_NAME)
2888 && c_parser_peek_token (parser)->id_kind == C_ID_ID
2889 && ((c_parser_peek_2nd_token (parser)->type == CPP_COMMA)
2890 || (c_parser_peek_2nd_token (parser)->type
2891 == CPP_CLOSE_PAREN)))
2893 tree arg1 = c_parser_peek_token (parser)->value;
2894 c_parser_consume_token (parser);
2895 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2896 attr_args = build_tree_list (NULL_TREE, arg1);
2899 c_parser_consume_token (parser);
2900 attr_args = tree_cons (NULL_TREE, arg1,
2901 c_parser_expr_list (parser, false));
2906 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2907 attr_args = NULL_TREE;
2909 attr_args = c_parser_expr_list (parser, false);
2911 attr = build_tree_list (attr_name, attr_args);
2912 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2913 c_parser_consume_token (parser);
2916 c_lex_string_translate = 1;
2917 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2921 attrs = chainon (attrs, attr);
2923 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2924 c_parser_consume_token (parser);
2927 c_lex_string_translate = 1;
2928 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2932 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2933 c_parser_consume_token (parser);
2936 c_lex_string_translate = 1;
2937 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2941 c_lex_string_translate = 1;
2946 /* Parse a type name (C90 6.5.5, C99 6.7.6).
2949 specifier-qualifier-list abstract-declarator[opt]
2952 static struct c_type_name *
2953 c_parser_type_name (c_parser *parser)
2955 struct c_declspecs *specs = build_null_declspecs ();
2956 struct c_declarator *declarator;
2957 struct c_type_name *ret;
2959 c_parser_declspecs (parser, specs, false, true, true);
2960 if (!specs->declspecs_seen_p)
2962 c_parser_error (parser, "expected specifier-qualifier-list");
2965 pending_xref_error ();
2966 finish_declspecs (specs);
2967 declarator = c_parser_declarator (parser, specs->type_seen_p,
2968 C_DTR_ABSTRACT, &dummy);
2969 if (declarator == NULL)
2971 ret = XOBNEW (&parser_obstack, struct c_type_name);
2973 ret->declarator = declarator;
2977 /* Parse an initializer (C90 6.5.7, C99 6.7.8).
2980 assignment-expression
2981 { initializer-list }
2982 { initializer-list , }
2985 designation[opt] initializer
2986 initializer-list , designation[opt] initializer
2993 designator-list designator
3000 [ constant-expression ]
3012 [ constant-expression ... constant-expression ]
3014 Any expression without commas is accepted in the syntax for the
3015 constant-expressions, with non-constant expressions rejected later.
3017 This function is only used for top-level initializers; for nested
3018 ones, see c_parser_initval. */
3020 static struct c_expr
3021 c_parser_initializer (c_parser *parser)
3023 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
3024 return c_parser_braced_init (parser, NULL_TREE, false);
3028 ret = c_parser_expr_no_commas (parser, NULL);
3029 if (TREE_CODE (ret.value) != STRING_CST
3030 && TREE_CODE (ret.value) != COMPOUND_LITERAL_EXPR)
3031 ret = default_function_array_conversion (ret);
3036 /* Parse a braced initializer list. TYPE is the type specified for a
3037 compound literal, and NULL_TREE for other initializers and for
3038 nested braced lists. NESTED_P is true for nested braced lists,
3039 false for the list of a compound literal or the list that is the
3040 top-level initializer in a declaration. */
3042 static struct c_expr
3043 c_parser_braced_init (c_parser *parser, tree type, bool nested_p)
3045 gcc_assert (c_parser_next_token_is (parser, CPP_OPEN_BRACE));
3046 c_parser_consume_token (parser);
3048 push_init_level (0);
3050 really_start_incremental_init (type);
3051 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3054 pedwarn ("ISO C forbids empty initializer braces");
3058 /* Parse a non-empty initializer list, possibly with a trailing
3062 c_parser_initelt (parser);
3065 if (c_parser_next_token_is (parser, CPP_COMMA))
3066 c_parser_consume_token (parser);
3069 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3073 if (c_parser_next_token_is_not (parser, CPP_CLOSE_BRACE))
3076 ret.value = error_mark_node;
3077 ret.original_code = ERROR_MARK;
3078 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, "expected %<}%>");
3081 c_parser_consume_token (parser);
3082 return pop_init_level (0);
3085 /* Parse a nested initializer, including designators. */
3088 c_parser_initelt (c_parser *parser)
3090 /* Parse any designator or designator list. A single array
3091 designator may have the subsequent "=" omitted in GNU C, but a
3092 longer list or a structure member designator may not. */
3093 if (c_parser_next_token_is (parser, CPP_NAME)
3094 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
3096 /* Old-style structure member designator. */
3097 set_init_label (c_parser_peek_token (parser)->value);
3099 pedwarn ("obsolete use of designated initializer with %<:%>");
3100 c_parser_consume_token (parser);
3101 c_parser_consume_token (parser);
3105 /* des_seen is 0 if there have been no designators, 1 if there
3106 has been a single array designator and 2 otherwise. */
3108 while (c_parser_next_token_is (parser, CPP_OPEN_SQUARE)
3109 || c_parser_next_token_is (parser, CPP_DOT))
3111 int des_prev = des_seen;
3114 if (c_parser_next_token_is (parser, CPP_DOT))
3117 c_parser_consume_token (parser);
3118 if (c_parser_next_token_is (parser, CPP_NAME))
3120 set_init_label (c_parser_peek_token (parser)->value);
3121 c_parser_consume_token (parser);
3126 init.value = error_mark_node;
3127 init.original_code = ERROR_MARK;
3128 c_parser_error (parser, "expected identifier");
3129 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
3130 process_init_element (init);
3137 /* ??? Following the old parser, [ objc-receiver
3138 objc-message-args ] is accepted as an initializer,
3139 being distinguished from a designator by what follows
3140 the first assignment expression inside the square
3141 brackets, but after a first array designator a
3142 subsequent square bracket is for Objective-C taken to
3143 start an expression, using the obsolete form of
3144 designated initializer without '=', rather than
3145 possibly being a second level of designation: in LALR
3146 terms, the '[' is shifted rather than reducing
3147 designator to designator-list. */
3148 if (des_prev == 1 && c_dialect_objc ())
3150 des_seen = des_prev;
3153 if (des_prev == 0 && c_dialect_objc ())
3155 /* This might be an array designator or an
3156 Objective-C message expression. If the former,
3157 continue parsing here; if the latter, parse the
3158 remainder of the initializer given the starting
3159 primary-expression. ??? It might make sense to
3160 distinguish when des_prev == 1 as well; see
3161 previous comment. */
3163 struct c_expr mexpr;
3164 c_parser_consume_token (parser);
3165 if (c_parser_peek_token (parser)->type == CPP_NAME
3166 && ((c_parser_peek_token (parser)->id_kind
3168 || (c_parser_peek_token (parser)->id_kind
3169 == C_ID_CLASSNAME)))
3171 /* Type name receiver. */
3172 tree id = c_parser_peek_token (parser)->value;
3173 c_parser_consume_token (parser);
3174 rec = objc_get_class_reference (id);
3175 goto parse_message_args;
3177 first = c_parser_expr_no_commas (parser, NULL).value;
3178 if (c_parser_next_token_is (parser, CPP_ELLIPSIS)
3179 || c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
3180 goto array_desig_after_first;
3181 /* Expression receiver. So far only one part
3182 without commas has been parsed; there might be
3183 more of the expression. */
3185 while (c_parser_next_token_is (parser, CPP_COMMA))
3188 c_parser_consume_token (parser);
3189 next = c_parser_expr_no_commas (parser, NULL);
3190 next = default_function_array_conversion (next);
3191 rec = build_compound_expr (rec, next.value);
3194 /* Now parse the objc-message-args. */
3195 args = c_parser_objc_message_args (parser);
3196 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
3199 = objc_build_message_expr (build_tree_list (rec, args));
3200 mexpr.original_code = ERROR_MARK;
3201 /* Now parse and process the remainder of the
3202 initializer, starting with this message
3203 expression as a primary-expression. */
3204 c_parser_initval (parser, &mexpr);
3207 c_parser_consume_token (parser);
3208 first = c_parser_expr_no_commas (parser, NULL).value;
3209 array_desig_after_first:
3210 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
3212 c_parser_consume_token (parser);
3213 second = c_parser_expr_no_commas (parser, NULL).value;
3217 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
3219 c_parser_consume_token (parser);
3220 set_init_index (first, second);
3221 if (pedantic && second)
3222 pedwarn ("ISO C forbids specifying range of "
3223 "elements to initialize");
3226 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
3232 if (c_parser_next_token_is (parser, CPP_EQ))
3234 if (pedantic && !flag_isoc99)
3235 pedwarn ("ISO C90 forbids specifying subobject to initialize");
3236 c_parser_consume_token (parser);
3243 pedwarn ("obsolete use of designated initializer "
3249 init.value = error_mark_node;
3250 init.original_code = ERROR_MARK;
3251 c_parser_error (parser, "expected %<=%>");
3252 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
3253 process_init_element (init);
3259 c_parser_initval (parser, NULL);
3262 /* Parse a nested initializer; as c_parser_initializer but parses
3263 initializers within braced lists, after any designators have been
3264 applied. If AFTER is not NULL then it is an Objective-C message
3265 expression which is the primary-expression starting the
3269 c_parser_initval (c_parser *parser, struct c_expr *after)
3272 gcc_assert (!after || c_dialect_objc ());
3273 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE) && !after)
3274 init = c_parser_braced_init (parser, NULL_TREE, true);
3277 init = c_parser_expr_no_commas (parser, after);
3278 if (init.value != NULL_TREE
3279 && TREE_CODE (init.value) != STRING_CST
3280 && TREE_CODE (init.value) != COMPOUND_LITERAL_EXPR)
3281 init = default_function_array_conversion (init);
3283 process_init_element (init);
3286 /* Parse a compound statement (possibly a function body) (C90 6.6.2,
3290 { block-item-list[opt] }
3291 { label-declarations block-item-list }
3295 block-item-list block-item
3307 { label-declarations block-item-list }
3310 __extension__ nested-declaration
3311 nested-function-definition
3315 label-declarations label-declaration
3318 __label__ identifier-list ;
3320 Allowing the mixing of declarations and code is new in C99. The
3321 GNU syntax also permits (not shown above) labels at the end of
3322 compound statements, which yield an error. We don't allow labels
3323 on declarations; this might seem like a natural extension, but
3324 there would be a conflict between attributes on the label and
3325 prefix attributes on the declaration. ??? The syntax follows the
3326 old parser in requiring something after label declarations.
3327 Although they are erroneous if the labels declared aren't defined,
3328 is it useful for the syntax to be this way?
3340 c_parser_compound_statement (c_parser *parser)
3343 if (!c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
3344 return error_mark_node;
3345 stmt = c_begin_compound_stmt (true);
3346 c_parser_compound_statement_nostart (parser);
3347 return c_end_compound_stmt (stmt, true);
3350 /* Parse a compound statement except for the opening brace. This is
3351 used for parsing both compound statements and statement expressions
3352 (which follow different paths to handling the opening). */
3355 c_parser_compound_statement_nostart (c_parser *parser)
3357 bool last_stmt = false;
3358 bool last_label = false;
3359 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3361 c_parser_consume_token (parser);
3364 if (c_parser_next_token_is_keyword (parser, RID_LABEL))
3366 /* Read zero or more forward-declarations for labels that nested
3367 functions can jump to. */
3368 while (c_parser_next_token_is_keyword (parser, RID_LABEL))
3370 c_parser_consume_token (parser);
3371 /* Any identifiers, including those declared as type names,
3376 if (c_parser_next_token_is_not (parser, CPP_NAME))
3378 c_parser_error (parser, "expected identifier");
3382 = declare_label (c_parser_peek_token (parser)->value);
3383 C_DECLARED_LABEL_FLAG (label) = 1;
3384 add_stmt (build_stmt (DECL_EXPR, label));
3385 c_parser_consume_token (parser);
3386 if (c_parser_next_token_is (parser, CPP_COMMA))
3387 c_parser_consume_token (parser);
3391 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
3393 /* ??? Locating this diagnostic on the token after the
3394 declarations end follows the old parser, but it might be
3395 better to locate it where the declarations start instead. */
3397 pedwarn ("ISO C forbids label declarations");
3399 /* We must now have at least one statement, label or declaration. */
3400 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3402 c_parser_error (parser, "expected declaration or statement");
3403 c_parser_consume_token (parser);
3406 while (c_parser_next_token_is_not (parser, CPP_CLOSE_BRACE))
3408 location_t loc = c_parser_peek_token (parser)->location;
3409 if (c_parser_next_token_is_keyword (parser, RID_CASE)
3410 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
3411 || (c_parser_next_token_is (parser, CPP_NAME)
3412 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
3416 c_parser_label (parser);
3418 else if (!last_label
3419 && c_parser_next_token_starts_declspecs (parser))
3422 c_parser_declaration_or_fndef (parser, true, true, true, true);
3424 && ((pedantic && !flag_isoc99)
3425 || warn_declaration_after_statement))
3426 pedwarn_c90 ("%HISO C90 forbids mixed declarations and code",
3430 else if (!last_label
3431 && c_parser_next_token_is_keyword (parser, RID_EXTENSION))
3433 /* __extension__ can start a declaration, but is also an
3434 unary operator that can start an expression. Consume all
3435 but the last of a possible series of __extension__ to
3437 while (c_parser_peek_2nd_token (parser)->type == CPP_KEYWORD
3438 && (c_parser_peek_2nd_token (parser)->keyword
3440 c_parser_consume_token (parser);
3441 if (c_token_starts_declspecs (c_parser_peek_2nd_token (parser)))
3444 ext = disable_extension_diagnostics ();
3445 c_parser_consume_token (parser);
3447 c_parser_declaration_or_fndef (parser, true, true, true, true);
3448 /* Following the old parser, __extension__ does not
3449 disable this diagnostic. */
3450 restore_extension_diagnostics (ext);
3452 && ((pedantic && !flag_isoc99)
3453 || warn_declaration_after_statement))
3454 pedwarn_c90 ("%HISO C90 forbids mixed declarations and code",
3461 else if (c_parser_next_token_is (parser, CPP_PRAGMA))
3463 /* External pragmas, and some omp pragmas, are not associated
3464 with regular c code, and so are not to be considered statements
3465 syntactically. This ensures that the user doesn't put them
3466 places that would turn into syntax errors if the directive