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 3, 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 COPYING3. If not see
22 <http://www.gnu.org/licenses/>. */
26 Make sure all relevant comments, and all relevant code from all
27 actions, brought over from old parser. Verify exact correspondence
30 Add testcases covering every input symbol in every state in old and
33 Include full syntax for GNU C, including erroneous cases accepted
34 with error messages, in syntax productions in comments.
36 Make more diagnostics in the front end generally take an explicit
37 location rather than implicitly using input_location. */
41 #include "coretypes.h"
45 #include "langhooks.h"
61 /* The reserved keyword table. */
65 ENUM_BITFIELD(rid) rid : 16;
66 unsigned int disable : 16;
69 /* Disable mask. Keywords are disabled if (reswords[i].disable &
71 #define D_C89 0x01 /* not in C89 */
72 #define D_EXT 0x02 /* GCC extension */
73 #define D_EXT89 0x04 /* GCC extension incorporated in C99 */
74 #define D_OBJC 0x08 /* Objective C only */
76 static const struct resword reswords[] =
78 { "_Bool", RID_BOOL, 0 },
79 { "_Complex", RID_COMPLEX, 0 },
80 { "_Decimal32", RID_DFLOAT32, D_EXT },
81 { "_Decimal64", RID_DFLOAT64, D_EXT },
82 { "_Decimal128", RID_DFLOAT128, D_EXT },
83 { "_Fract", RID_FRACT, D_EXT },
84 { "_Accum", RID_ACCUM, D_EXT },
85 { "_Sat", RID_SAT, D_EXT },
86 { "__FUNCTION__", RID_FUNCTION_NAME, 0 },
87 { "__PRETTY_FUNCTION__", RID_PRETTY_FUNCTION_NAME, 0 },
88 { "__alignof", RID_ALIGNOF, 0 },
89 { "__alignof__", RID_ALIGNOF, 0 },
90 { "__asm", RID_ASM, 0 },
91 { "__asm__", RID_ASM, 0 },
92 { "__attribute", RID_ATTRIBUTE, 0 },
93 { "__attribute__", RID_ATTRIBUTE, 0 },
94 { "__builtin_choose_expr", RID_CHOOSE_EXPR, 0 },
95 { "__builtin_offsetof", RID_OFFSETOF, 0 },
96 { "__builtin_types_compatible_p", RID_TYPES_COMPATIBLE_P, 0 },
97 { "__builtin_va_arg", RID_VA_ARG, 0 },
98 { "__complex", RID_COMPLEX, 0 },
99 { "__complex__", RID_COMPLEX, 0 },
100 { "__const", RID_CONST, 0 },
101 { "__const__", RID_CONST, 0 },
102 { "__extension__", RID_EXTENSION, 0 },
103 { "__func__", RID_C99_FUNCTION_NAME, 0 },
104 { "__imag", RID_IMAGPART, 0 },
105 { "__imag__", RID_IMAGPART, 0 },
106 { "__inline", RID_INLINE, 0 },
107 { "__inline__", RID_INLINE, 0 },
108 { "__label__", RID_LABEL, 0 },
109 { "__real", RID_REALPART, 0 },
110 { "__real__", RID_REALPART, 0 },
111 { "__restrict", RID_RESTRICT, 0 },
112 { "__restrict__", RID_RESTRICT, 0 },
113 { "__signed", RID_SIGNED, 0 },
114 { "__signed__", RID_SIGNED, 0 },
115 { "__thread", RID_THREAD, 0 },
116 { "__typeof", RID_TYPEOF, 0 },
117 { "__typeof__", RID_TYPEOF, 0 },
118 { "__volatile", RID_VOLATILE, 0 },
119 { "__volatile__", RID_VOLATILE, 0 },
120 { "asm", RID_ASM, D_EXT },
121 { "auto", RID_AUTO, 0 },
122 { "break", RID_BREAK, 0 },
123 { "case", RID_CASE, 0 },
124 { "char", RID_CHAR, 0 },
125 { "const", RID_CONST, 0 },
126 { "continue", RID_CONTINUE, 0 },
127 { "default", RID_DEFAULT, 0 },
129 { "double", RID_DOUBLE, 0 },
130 { "else", RID_ELSE, 0 },
131 { "enum", RID_ENUM, 0 },
132 { "extern", RID_EXTERN, 0 },
133 { "float", RID_FLOAT, 0 },
134 { "for", RID_FOR, 0 },
135 { "goto", RID_GOTO, 0 },
137 { "inline", RID_INLINE, D_EXT89 },
138 { "int", RID_INT, 0 },
139 { "long", RID_LONG, 0 },
140 { "register", RID_REGISTER, 0 },
141 { "restrict", RID_RESTRICT, D_C89 },
142 { "return", RID_RETURN, 0 },
143 { "short", RID_SHORT, 0 },
144 { "signed", RID_SIGNED, 0 },
145 { "sizeof", RID_SIZEOF, 0 },
146 { "static", RID_STATIC, 0 },
147 { "struct", RID_STRUCT, 0 },
148 { "switch", RID_SWITCH, 0 },
149 { "typedef", RID_TYPEDEF, 0 },
150 { "typeof", RID_TYPEOF, D_EXT },
151 { "union", RID_UNION, 0 },
152 { "unsigned", RID_UNSIGNED, 0 },
153 { "void", RID_VOID, 0 },
154 { "volatile", RID_VOLATILE, 0 },
155 { "while", RID_WHILE, 0 },
156 /* These Objective-C keywords are recognized only immediately after
158 { "class", RID_AT_CLASS, D_OBJC },
159 { "compatibility_alias", RID_AT_ALIAS, D_OBJC },
160 { "defs", RID_AT_DEFS, D_OBJC },
161 { "encode", RID_AT_ENCODE, D_OBJC },
162 { "end", RID_AT_END, D_OBJC },
163 { "implementation", RID_AT_IMPLEMENTATION, D_OBJC },
164 { "interface", RID_AT_INTERFACE, D_OBJC },
165 { "private", RID_AT_PRIVATE, D_OBJC },
166 { "protected", RID_AT_PROTECTED, D_OBJC },
167 { "protocol", RID_AT_PROTOCOL, D_OBJC },
168 { "public", RID_AT_PUBLIC, D_OBJC },
169 { "selector", RID_AT_SELECTOR, D_OBJC },
170 { "throw", RID_AT_THROW, D_OBJC },
171 { "try", RID_AT_TRY, D_OBJC },
172 { "catch", RID_AT_CATCH, D_OBJC },
173 { "finally", RID_AT_FINALLY, D_OBJC },
174 { "synchronized", RID_AT_SYNCHRONIZED, D_OBJC },
175 /* These are recognized only in protocol-qualifier context
177 { "bycopy", RID_BYCOPY, D_OBJC },
178 { "byref", RID_BYREF, D_OBJC },
179 { "in", RID_IN, D_OBJC },
180 { "inout", RID_INOUT, D_OBJC },
181 { "oneway", RID_ONEWAY, D_OBJC },
182 { "out", RID_OUT, D_OBJC },
184 #define N_reswords (sizeof reswords / sizeof (struct resword))
186 /* Initialization routine for this file. */
191 /* The only initialization required is of the reserved word
195 int mask = (flag_isoc99 ? 0 : D_C89)
196 | (flag_no_asm ? (flag_isoc99 ? D_EXT : D_EXT|D_EXT89) : 0);
198 if (!c_dialect_objc ())
201 ridpointers = GGC_CNEWVEC (tree, (int) RID_MAX);
202 for (i = 0; i < N_reswords; i++)
204 /* If a keyword is disabled, do not enter it into the table
205 and so create a canonical spelling that isn't a keyword. */
206 if (reswords[i].disable & mask)
209 id = get_identifier (reswords[i].word);
210 C_RID_CODE (id) = reswords[i].rid;
211 C_IS_RESERVED_WORD (id) = 1;
212 ridpointers [(int) reswords[i].rid] = id;
216 /* The C lexer intermediates between the lexer in cpplib and c-lex.c
217 and the C parser. Unlike the C++ lexer, the parser structure
218 stores the lexer information instead of using a separate structure.
219 Identifiers are separated into ordinary identifiers, type names,
220 keywords and some other Objective-C types of identifiers, and some
221 look-ahead is maintained.
223 ??? It might be a good idea to lex the whole file up front (as for
224 C++). It would then be possible to share more of the C and C++
225 lexer code, if desired. */
227 /* The following local token type is used. */
230 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
232 /* More information about the type of a CPP_NAME token. */
233 typedef enum c_id_kind {
234 /* An ordinary identifier. */
236 /* An identifier declared as a typedef name. */
238 /* An identifier declared as an Objective-C class name. */
240 /* Not an identifier. */
244 /* A single C token after string literal concatenation and conversion
245 of preprocessing tokens to tokens. */
246 typedef struct c_token GTY (())
248 /* The kind of token. */
249 ENUM_BITFIELD (cpp_ttype) type : 8;
250 /* If this token is a CPP_NAME, this value indicates whether also
251 declared as some kind of type. Otherwise, it is C_ID_NONE. */
252 ENUM_BITFIELD (c_id_kind) id_kind : 8;
253 /* If this token is a keyword, this value indicates which keyword.
254 Otherwise, this value is RID_MAX. */
255 ENUM_BITFIELD (rid) keyword : 8;
256 /* If this token is a CPP_PRAGMA, this indicates the pragma that
257 was seen. Otherwise it is PRAGMA_NONE. */
258 ENUM_BITFIELD (pragma_kind) pragma_kind : 7;
259 /* True if this token is from a system header. */
260 BOOL_BITFIELD in_system_header : 1;
261 /* The value associated with this token, if any. */
263 /* The location at which this token was found. */
267 /* A parser structure recording information about the state and
268 context of parsing. Includes lexer information with up to two
269 tokens of look-ahead; more are not needed for C. */
270 typedef struct c_parser GTY(())
272 /* The look-ahead tokens. */
274 /* How many look-ahead tokens are available (0, 1 or 2). */
276 /* True if a syntax error is being recovered from; false otherwise.
277 c_parser_error sets this flag. It should clear this flag when
278 enough tokens have been consumed to recover from the error. */
279 BOOL_BITFIELD error : 1;
280 /* True if we're processing a pragma, and shouldn't automatically
281 consume CPP_PRAGMA_EOL. */
282 BOOL_BITFIELD in_pragma : 1;
283 /* True if we want to lex an untranslated string. */
284 BOOL_BITFIELD lex_untranslated_string : 1;
285 /* Objective-C specific parser/lexer information. */
286 BOOL_BITFIELD objc_pq_context : 1;
287 /* The following flag is needed to contextualize Objective-C lexical
288 analysis. In some cases (e.g., 'int NSObject;'), it is
289 undesirable to bind an identifier to an Objective-C class, even
290 if a class with that name exists. */
291 BOOL_BITFIELD objc_need_raw_identifier : 1;
295 /* The actual parser and external interface. ??? Does this need to be
296 garbage-collected? */
298 static GTY (()) c_parser *the_parser;
301 /* Read in and lex a single token, storing it in *TOKEN. */
304 c_lex_one_token (c_parser *parser, c_token *token)
306 timevar_push (TV_LEX);
308 token->type = c_lex_with_flags (&token->value, &token->location, NULL,
309 (parser->lex_untranslated_string
310 ? C_LEX_STRING_NO_TRANSLATE : 0));
311 token->id_kind = C_ID_NONE;
312 token->keyword = RID_MAX;
313 token->pragma_kind = PRAGMA_NONE;
314 token->in_system_header = in_system_header;
322 bool objc_force_identifier = parser->objc_need_raw_identifier;
323 if (c_dialect_objc ())
324 parser->objc_need_raw_identifier = false;
326 if (C_IS_RESERVED_WORD (token->value))
328 enum rid rid_code = C_RID_CODE (token->value);
330 if (c_dialect_objc ())
332 if (!OBJC_IS_AT_KEYWORD (rid_code)
333 && (!OBJC_IS_PQ_KEYWORD (rid_code)
334 || parser->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 if (c_dialect_objc ())
392 parser->objc_need_raw_identifier = false;
395 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
396 token->pragma_kind = TREE_INT_CST_LOW (token->value);
402 timevar_pop (TV_LEX);
405 /* Return a pointer to the next token from PARSER, reading it in if
408 static inline c_token *
409 c_parser_peek_token (c_parser *parser)
411 if (parser->tokens_avail == 0)
413 c_lex_one_token (parser, &parser->tokens[0]);
414 parser->tokens_avail = 1;
416 return &parser->tokens[0];
419 /* Return true if the next token from PARSER has the indicated
423 c_parser_next_token_is (c_parser *parser, enum cpp_ttype type)
425 return c_parser_peek_token (parser)->type == type;
428 /* Return true if the next token from PARSER does not have the
432 c_parser_next_token_is_not (c_parser *parser, enum cpp_ttype type)
434 return !c_parser_next_token_is (parser, type);
437 /* Return true if the next token from PARSER is the indicated
441 c_parser_next_token_is_keyword (c_parser *parser, enum rid keyword)
445 /* Peek at the next token. */
446 token = c_parser_peek_token (parser);
447 /* Check to see if it is the indicated keyword. */
448 return token->keyword == keyword;
451 /* Return true if TOKEN can start a type name,
454 c_token_starts_typename (c_token *token)
459 switch (token->id_kind)
466 gcc_assert (c_dialect_objc ());
472 switch (token->keyword)
504 if (c_dialect_objc ())
512 /* Return true if the next token from PARSER can start a type name,
515 c_parser_next_token_starts_typename (c_parser *parser)
517 c_token *token = c_parser_peek_token (parser);
518 return c_token_starts_typename (token);
521 /* Return true if TOKEN can start declaration specifiers, false
524 c_token_starts_declspecs (c_token *token)
529 switch (token->id_kind)
536 gcc_assert (c_dialect_objc ());
542 switch (token->keyword)
581 if (c_dialect_objc ())
589 /* Return true if the next token from PARSER can start declaration
590 specifiers, false otherwise. */
592 c_parser_next_token_starts_declspecs (c_parser *parser)
594 c_token *token = c_parser_peek_token (parser);
595 return c_token_starts_declspecs (token);
598 /* Return a pointer to the next-but-one token from PARSER, reading it
599 in if necessary. The next token is already read in. */
602 c_parser_peek_2nd_token (c_parser *parser)
604 if (parser->tokens_avail >= 2)
605 return &parser->tokens[1];
606 gcc_assert (parser->tokens_avail == 1);
607 gcc_assert (parser->tokens[0].type != CPP_EOF);
608 gcc_assert (parser->tokens[0].type != CPP_PRAGMA_EOL);
609 c_lex_one_token (parser, &parser->tokens[1]);
610 parser->tokens_avail = 2;
611 return &parser->tokens[1];
614 /* Consume the next token from PARSER. */
617 c_parser_consume_token (c_parser *parser)
619 gcc_assert (parser->tokens_avail >= 1);
620 gcc_assert (parser->tokens[0].type != CPP_EOF);
621 gcc_assert (!parser->in_pragma || parser->tokens[0].type != CPP_PRAGMA_EOL);
622 gcc_assert (parser->error || parser->tokens[0].type != CPP_PRAGMA);
623 if (parser->tokens_avail == 2)
624 parser->tokens[0] = parser->tokens[1];
625 parser->tokens_avail--;
628 /* Expect the current token to be a #pragma. Consume it and remember
629 that we've begun parsing a pragma. */
632 c_parser_consume_pragma (c_parser *parser)
634 gcc_assert (!parser->in_pragma);
635 gcc_assert (parser->tokens_avail >= 1);
636 gcc_assert (parser->tokens[0].type == CPP_PRAGMA);
637 if (parser->tokens_avail == 2)
638 parser->tokens[0] = parser->tokens[1];
639 parser->tokens_avail--;
640 parser->in_pragma = true;
643 /* Update the globals input_location and in_system_header from
646 c_parser_set_source_position_from_token (c_token *token)
648 if (token->type != CPP_EOF)
650 input_location = token->location;
651 in_system_header = token->in_system_header;
655 /* Issue a diagnostic of the form
656 FILE:LINE: MESSAGE before TOKEN
657 where TOKEN is the next token in the input stream of PARSER.
658 MESSAGE (specified by the caller) is usually of the form "expected
661 Do not issue a diagnostic if still recovering from an error.
663 ??? This is taken from the C++ parser, but building up messages in
664 this way is not i18n-friendly and some other approach should be
668 c_parser_error (c_parser *parser, const char *gmsgid)
670 c_token *token = c_parser_peek_token (parser);
673 parser->error = true;
676 /* This diagnostic makes more sense if it is tagged to the line of
677 the token we just peeked at. */
678 c_parser_set_source_position_from_token (token);
679 c_parse_error (gmsgid,
680 /* Because c_parse_error does not understand
681 CPP_KEYWORD, keywords are treated like
683 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
687 /* If the next token is of the indicated TYPE, consume it. Otherwise,
688 issue the error MSGID. If MSGID is NULL then a message has already
689 been produced and no message will be produced this time. Returns
690 true if found, false otherwise. */
693 c_parser_require (c_parser *parser,
697 if (c_parser_next_token_is (parser, type))
699 c_parser_consume_token (parser);
704 c_parser_error (parser, msgid);
709 /* If the next token is the indicated keyword, consume it. Otherwise,
710 issue the error MSGID. Returns true if found, false otherwise. */
713 c_parser_require_keyword (c_parser *parser,
717 if (c_parser_next_token_is_keyword (parser, keyword))
719 c_parser_consume_token (parser);
724 c_parser_error (parser, msgid);
729 /* Like c_parser_require, except that tokens will be skipped until the
730 desired token is found. An error message is still produced if the
731 next token is not as expected. If MSGID is NULL then a message has
732 already been produced and no message will be produced this
736 c_parser_skip_until_found (c_parser *parser,
740 unsigned nesting_depth = 0;
742 if (c_parser_require (parser, type, msgid))
745 /* Skip tokens until the desired token is found. */
748 /* Peek at the next token. */
749 c_token *token = c_parser_peek_token (parser);
750 /* If we've reached the token we want, consume it and stop. */
751 if (token->type == type && !nesting_depth)
753 c_parser_consume_token (parser);
757 /* If we've run out of tokens, stop. */
758 if (token->type == CPP_EOF)
760 if (token->type == CPP_PRAGMA_EOL && parser->in_pragma)
762 if (token->type == CPP_OPEN_BRACE
763 || token->type == CPP_OPEN_PAREN
764 || token->type == CPP_OPEN_SQUARE)
766 else if (token->type == CPP_CLOSE_BRACE
767 || token->type == CPP_CLOSE_PAREN
768 || token->type == CPP_CLOSE_SQUARE)
770 if (nesting_depth-- == 0)
773 /* Consume this token. */
774 c_parser_consume_token (parser);
776 parser->error = false;
779 /* Skip tokens until the end of a parameter is found, but do not
780 consume the comma, semicolon or closing delimiter. */
783 c_parser_skip_to_end_of_parameter (c_parser *parser)
785 unsigned nesting_depth = 0;
789 c_token *token = c_parser_peek_token (parser);
790 if ((token->type == CPP_COMMA || token->type == CPP_SEMICOLON)
793 /* If we've run out of tokens, stop. */
794 if (token->type == CPP_EOF)
796 if (token->type == CPP_PRAGMA_EOL && parser->in_pragma)
798 if (token->type == CPP_OPEN_BRACE
799 || token->type == CPP_OPEN_PAREN
800 || token->type == CPP_OPEN_SQUARE)
802 else if (token->type == CPP_CLOSE_BRACE
803 || token->type == CPP_CLOSE_PAREN
804 || token->type == CPP_CLOSE_SQUARE)
806 if (nesting_depth-- == 0)
809 /* Consume this token. */
810 c_parser_consume_token (parser);
812 parser->error = false;
815 /* Expect to be at the end of the pragma directive and consume an
816 end of line marker. */
819 c_parser_skip_to_pragma_eol (c_parser *parser)
821 gcc_assert (parser->in_pragma);
822 parser->in_pragma = false;
824 if (!c_parser_require (parser, CPP_PRAGMA_EOL, "expected end of line"))
827 c_token *token = c_parser_peek_token (parser);
828 if (token->type == CPP_EOF)
830 if (token->type == CPP_PRAGMA_EOL)
832 c_parser_consume_token (parser);
835 c_parser_consume_token (parser);
838 parser->error = false;
841 /* Skip tokens until we have consumed an entire block, or until we
842 have consumed a non-nested ';'. */
845 c_parser_skip_to_end_of_block_or_statement (c_parser *parser)
847 unsigned nesting_depth = 0;
848 bool save_error = parser->error;
854 /* Peek at the next token. */
855 token = c_parser_peek_token (parser);
863 if (parser->in_pragma)
868 /* If the next token is a ';', we have reached the
869 end of the statement. */
872 /* Consume the ';'. */
873 c_parser_consume_token (parser);
878 case CPP_CLOSE_BRACE:
879 /* If the next token is a non-nested '}', then we have
880 reached the end of the current block. */
881 if (nesting_depth == 0 || --nesting_depth == 0)
883 c_parser_consume_token (parser);
889 /* If it the next token is a '{', then we are entering a new
890 block. Consume the entire block. */
895 /* If we see a pragma, consume the whole thing at once. We
896 have some safeguards against consuming pragmas willy-nilly.
897 Normally, we'd expect to be here with parser->error set,
898 which disables these safeguards. But it's possible to get
899 here for secondary error recovery, after parser->error has
901 c_parser_consume_pragma (parser);
902 c_parser_skip_to_pragma_eol (parser);
903 parser->error = save_error;
910 c_parser_consume_token (parser);
914 parser->error = false;
917 /* Save the warning flags which are controlled by __extension__. */
920 disable_extension_diagnostics (void)
923 | (warn_pointer_arith << 1)
924 | (warn_traditional << 2)
927 warn_pointer_arith = 0;
928 warn_traditional = 0;
933 /* Restore the warning flags which are controlled by __extension__.
934 FLAGS is the return value from disable_extension_diagnostics. */
937 restore_extension_diagnostics (int flags)
939 pedantic = flags & 1;
940 warn_pointer_arith = (flags >> 1) & 1;
941 warn_traditional = (flags >> 2) & 1;
942 flag_iso = (flags >> 3) & 1;
945 /* Possibly kinds of declarator to parse. */
946 typedef enum c_dtr_syn {
947 /* A normal declarator with an identifier. */
949 /* An abstract declarator (maybe empty). */
951 /* A parameter declarator: may be either, but after a type name does
952 not redeclare a typedef name as an identifier if it can
953 alternatively be interpreted as a typedef name; see DR#009,
954 applied in C90 TC1, omitted from C99 and reapplied in C99 TC2
955 following DR#249. For example, given a typedef T, "int T" and
956 "int *T" are valid parameter declarations redeclaring T, while
957 "int (T)" and "int * (T)" and "int (T[])" and "int (T (int))" are
958 abstract declarators rather than involving redundant parentheses;
959 the same applies with attributes inside the parentheses before
964 static void c_parser_external_declaration (c_parser *);
965 static void c_parser_asm_definition (c_parser *);
966 static void c_parser_declaration_or_fndef (c_parser *, bool, bool, bool, bool);
967 static void c_parser_declspecs (c_parser *, struct c_declspecs *, bool, bool,
969 static struct c_typespec c_parser_enum_specifier (c_parser *);
970 static struct c_typespec c_parser_struct_or_union_specifier (c_parser *);
971 static tree c_parser_struct_declaration (c_parser *);
972 static struct c_typespec c_parser_typeof_specifier (c_parser *);
973 static struct c_declarator *c_parser_declarator (c_parser *, bool, c_dtr_syn,
975 static struct c_declarator *c_parser_direct_declarator (c_parser *, bool,
977 static struct c_declarator *c_parser_direct_declarator_inner (c_parser *,
979 struct c_declarator *);
980 static struct c_arg_info *c_parser_parms_declarator (c_parser *, bool, tree);
981 static struct c_arg_info *c_parser_parms_list_declarator (c_parser *, tree);
982 static struct c_parm *c_parser_parameter_declaration (c_parser *, tree);
983 static tree c_parser_simple_asm_expr (c_parser *);
984 static tree c_parser_attributes (c_parser *);
985 static struct c_type_name *c_parser_type_name (c_parser *);
986 static struct c_expr c_parser_initializer (c_parser *);
987 static struct c_expr c_parser_braced_init (c_parser *, tree, bool);
988 static void c_parser_initelt (c_parser *);
989 static void c_parser_initval (c_parser *, struct c_expr *);
990 static tree c_parser_compound_statement (c_parser *);
991 static void c_parser_compound_statement_nostart (c_parser *);
992 static void c_parser_label (c_parser *);
993 static void c_parser_statement (c_parser *);
994 static void c_parser_statement_after_labels (c_parser *);
995 static void c_parser_if_statement (c_parser *);
996 static void c_parser_switch_statement (c_parser *);
997 static void c_parser_while_statement (c_parser *);
998 static void c_parser_do_statement (c_parser *);
999 static void c_parser_for_statement (c_parser *);
1000 static tree c_parser_asm_statement (c_parser *);
1001 static tree c_parser_asm_operands (c_parser *, bool);
1002 static tree c_parser_asm_clobbers (c_parser *);
1003 static struct c_expr c_parser_expr_no_commas (c_parser *, struct c_expr *);
1004 static struct c_expr c_parser_conditional_expression (c_parser *,
1006 static struct c_expr c_parser_binary_expression (c_parser *, struct c_expr *);
1007 static struct c_expr c_parser_cast_expression (c_parser *, struct c_expr *);
1008 static struct c_expr c_parser_unary_expression (c_parser *);
1009 static struct c_expr c_parser_sizeof_expression (c_parser *);
1010 static struct c_expr c_parser_alignof_expression (c_parser *);
1011 static struct c_expr c_parser_postfix_expression (c_parser *);
1012 static struct c_expr c_parser_postfix_expression_after_paren_type (c_parser *,
1013 struct c_type_name *);
1014 static struct c_expr c_parser_postfix_expression_after_primary (c_parser *,
1016 static struct c_expr c_parser_expression (c_parser *);
1017 static struct c_expr c_parser_expression_conv (c_parser *);
1018 static tree c_parser_expr_list (c_parser *, bool);
1019 static void c_parser_omp_construct (c_parser *);
1020 static void c_parser_omp_threadprivate (c_parser *);
1021 static void c_parser_omp_barrier (c_parser *);
1022 static void c_parser_omp_flush (c_parser *);
1024 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
1025 static bool c_parser_pragma (c_parser *, enum pragma_context);
1027 /* These Objective-C parser functions are only ever called when
1028 compiling Objective-C. */
1029 static void c_parser_objc_class_definition (c_parser *);
1030 static void c_parser_objc_class_instance_variables (c_parser *);
1031 static void c_parser_objc_class_declaration (c_parser *);
1032 static void c_parser_objc_alias_declaration (c_parser *);
1033 static void c_parser_objc_protocol_definition (c_parser *);
1034 static enum tree_code c_parser_objc_method_type (c_parser *);
1035 static void c_parser_objc_method_definition (c_parser *);
1036 static void c_parser_objc_methodprotolist (c_parser *);
1037 static void c_parser_objc_methodproto (c_parser *);
1038 static tree c_parser_objc_method_decl (c_parser *);
1039 static tree c_parser_objc_type_name (c_parser *);
1040 static tree c_parser_objc_protocol_refs (c_parser *);
1041 static void c_parser_objc_try_catch_statement (c_parser *);
1042 static void c_parser_objc_synchronized_statement (c_parser *);
1043 static tree c_parser_objc_selector (c_parser *);
1044 static tree c_parser_objc_selector_arg (c_parser *);
1045 static tree c_parser_objc_receiver (c_parser *);
1046 static tree c_parser_objc_message_args (c_parser *);
1047 static tree c_parser_objc_keywordexpr (c_parser *);
1049 /* Parse a translation unit (C90 6.7, C99 6.9).
1052 external-declarations
1054 external-declarations:
1055 external-declaration
1056 external-declarations external-declaration
1065 c_parser_translation_unit (c_parser *parser)
1067 if (c_parser_next_token_is (parser, CPP_EOF))
1070 pedwarn ("ISO C forbids an empty source file");
1074 void *obstack_position = obstack_alloc (&parser_obstack, 0);
1078 c_parser_external_declaration (parser);
1079 obstack_free (&parser_obstack, obstack_position);
1081 while (c_parser_next_token_is_not (parser, CPP_EOF));
1085 /* Parse an external declaration (C90 6.7, C99 6.9).
1087 external-declaration:
1093 external-declaration:
1096 __extension__ external-declaration
1100 external-declaration:
1101 objc-class-definition
1102 objc-class-declaration
1103 objc-alias-declaration
1104 objc-protocol-definition
1105 objc-method-definition
1110 c_parser_external_declaration (c_parser *parser)
1113 switch (c_parser_peek_token (parser)->type)
1116 switch (c_parser_peek_token (parser)->keyword)
1119 ext = disable_extension_diagnostics ();
1120 c_parser_consume_token (parser);
1121 c_parser_external_declaration (parser);
1122 restore_extension_diagnostics (ext);
1125 c_parser_asm_definition (parser);
1127 case RID_AT_INTERFACE:
1128 case RID_AT_IMPLEMENTATION:
1129 gcc_assert (c_dialect_objc ());
1130 c_parser_objc_class_definition (parser);
1133 gcc_assert (c_dialect_objc ());
1134 c_parser_objc_class_declaration (parser);
1137 gcc_assert (c_dialect_objc ());
1138 c_parser_objc_alias_declaration (parser);
1140 case RID_AT_PROTOCOL:
1141 gcc_assert (c_dialect_objc ());
1142 c_parser_objc_protocol_definition (parser);
1145 gcc_assert (c_dialect_objc ());
1146 c_parser_consume_token (parser);
1147 objc_finish_implementation ();
1155 pedwarn ("ISO C does not allow extra %<;%> outside of a function");
1156 c_parser_consume_token (parser);
1159 c_parser_pragma (parser, pragma_external);
1163 if (c_dialect_objc ())
1165 c_parser_objc_method_definition (parser);
1168 /* Else fall through, and yield a syntax error trying to parse
1169 as a declaration or function definition. */
1172 /* A declaration or a function definition. We can only tell
1173 which after parsing the declaration specifiers, if any, and
1174 the first declarator. */
1175 c_parser_declaration_or_fndef (parser, true, true, false, true);
1181 /* Parse a declaration or function definition (C90 6.5, 6.7.1, C99
1182 6.7, 6.9.1). If FNDEF_OK is true, a function definition is
1183 accepted; otherwise (old-style parameter declarations) only other
1184 declarations are accepted. If NESTED is true, we are inside a
1185 function or parsing old-style parameter declarations; any functions
1186 encountered are nested functions and declaration specifiers are
1187 required; otherwise we are at top level and functions are normal
1188 functions and declaration specifiers may be optional. If EMPTY_OK
1189 is true, empty declarations are OK (subject to all other
1190 constraints); otherwise (old-style parameter declarations) they are
1191 diagnosed. If START_ATTR_OK is true, the declaration specifiers
1192 may start with attributes; otherwise they may not.
1195 declaration-specifiers init-declarator-list[opt] ;
1197 function-definition:
1198 declaration-specifiers[opt] declarator declaration-list[opt]
1203 declaration-list declaration
1205 init-declarator-list:
1207 init-declarator-list , init-declarator
1210 declarator simple-asm-expr[opt] attributes[opt]
1211 declarator simple-asm-expr[opt] attributes[opt] = initializer
1215 nested-function-definition:
1216 declaration-specifiers declarator declaration-list[opt]
1219 The simple-asm-expr and attributes are GNU extensions.
1221 This function does not handle __extension__; that is handled in its
1222 callers. ??? Following the old parser, __extension__ may start
1223 external declarations, declarations in functions and declarations
1224 at the start of "for" loops, but not old-style parameter
1227 C99 requires declaration specifiers in a function definition; the
1228 absence is diagnosed through the diagnosis of implicit int. In GNU
1229 C we also allow but diagnose declarations without declaration
1230 specifiers, but only at top level (elsewhere they conflict with
1236 threadprivate-directive */
1239 c_parser_declaration_or_fndef (c_parser *parser, bool fndef_ok, bool empty_ok,
1240 bool nested, bool start_attr_ok)
1242 struct c_declspecs *specs;
1244 tree all_prefix_attrs;
1245 bool diagnosed_no_specs = false;
1247 specs = build_null_declspecs ();
1248 c_parser_declspecs (parser, specs, true, true, start_attr_ok);
1251 c_parser_skip_to_end_of_block_or_statement (parser);
1254 if (nested && !specs->declspecs_seen_p)
1256 c_parser_error (parser, "expected declaration specifiers");
1257 c_parser_skip_to_end_of_block_or_statement (parser);
1260 finish_declspecs (specs);
1261 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1267 shadow_tag_warned (specs, 1);
1268 pedwarn ("empty declaration");
1270 c_parser_consume_token (parser);
1273 pending_xref_error ();
1274 prefix_attrs = specs->attrs;
1275 all_prefix_attrs = prefix_attrs;
1276 specs->attrs = NULL_TREE;
1279 struct c_declarator *declarator;
1282 /* Declaring either one or more declarators (in which case we
1283 should diagnose if there were no declaration specifiers) or a
1284 function definition (in which case the diagnostic for
1285 implicit int suffices). */
1286 declarator = c_parser_declarator (parser, specs->type_seen_p,
1287 C_DTR_NORMAL, &dummy);
1288 if (declarator == NULL)
1290 c_parser_skip_to_end_of_block_or_statement (parser);
1293 if (c_parser_next_token_is (parser, CPP_EQ)
1294 || c_parser_next_token_is (parser, CPP_COMMA)
1295 || c_parser_next_token_is (parser, CPP_SEMICOLON)
1296 || c_parser_next_token_is_keyword (parser, RID_ASM)
1297 || c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
1299 tree asm_name = NULL_TREE;
1300 tree postfix_attrs = NULL_TREE;
1301 if (!diagnosed_no_specs && !specs->declspecs_seen_p)
1303 diagnosed_no_specs = true;
1304 pedwarn ("data definition has no type or storage class");
1306 /* Having seen a data definition, there cannot now be a
1307 function definition. */
1309 if (c_parser_next_token_is_keyword (parser, RID_ASM))
1310 asm_name = c_parser_simple_asm_expr (parser);
1311 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
1312 postfix_attrs = c_parser_attributes (parser);
1313 if (c_parser_next_token_is (parser, CPP_EQ))
1317 c_parser_consume_token (parser);
1318 /* The declaration of the variable is in effect while
1319 its initializer is parsed. */
1320 d = start_decl (declarator, specs, true,
1321 chainon (postfix_attrs, all_prefix_attrs));
1323 d = error_mark_node;
1324 start_init (d, asm_name, global_bindings_p ());
1325 init = c_parser_initializer (parser);
1327 if (d != error_mark_node)
1329 maybe_warn_string_init (TREE_TYPE (d), init);
1330 finish_decl (d, init.value, asm_name);
1335 tree d = start_decl (declarator, specs, false,
1336 chainon (postfix_attrs,
1339 finish_decl (d, NULL_TREE, asm_name);
1341 if (c_parser_next_token_is (parser, CPP_COMMA))
1343 c_parser_consume_token (parser);
1344 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
1345 all_prefix_attrs = chainon (c_parser_attributes (parser),
1348 all_prefix_attrs = prefix_attrs;
1351 else if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1353 c_parser_consume_token (parser);
1358 c_parser_error (parser, "expected %<,%> or %<;%>");
1359 c_parser_skip_to_end_of_block_or_statement (parser);
1365 c_parser_error (parser, "expected %<=%>, %<,%>, %<;%>, "
1366 "%<asm%> or %<__attribute__%>");
1367 c_parser_skip_to_end_of_block_or_statement (parser);
1370 /* Function definition (nested or otherwise). */
1374 pedwarn ("ISO C forbids nested functions");
1375 push_function_context ();
1377 if (!start_function (specs, declarator, all_prefix_attrs))
1379 /* This can appear in many cases looking nothing like a
1380 function definition, so we don't give a more specific
1381 error suggesting there was one. */
1382 c_parser_error (parser, "expected %<=%>, %<,%>, %<;%>, %<asm%> "
1383 "or %<__attribute__%>");
1385 pop_function_context ();
1388 /* Parse old-style parameter declarations. ??? Attributes are
1389 not allowed to start declaration specifiers here because of a
1390 syntax conflict between a function declaration with attribute
1391 suffix and a function definition with an attribute prefix on
1392 first old-style parameter declaration. Following the old
1393 parser, they are not accepted on subsequent old-style
1394 parameter declarations either. However, there is no
1395 ambiguity after the first declaration, nor indeed on the
1396 first as long as we don't allow postfix attributes after a
1397 declarator with a nonempty identifier list in a definition;
1398 and postfix attributes have never been accepted here in
1399 function definitions either. */
1400 while (c_parser_next_token_is_not (parser, CPP_EOF)
1401 && c_parser_next_token_is_not (parser, CPP_OPEN_BRACE))
1402 c_parser_declaration_or_fndef (parser, false, false, true, false);
1403 DECL_SOURCE_LOCATION (current_function_decl)
1404 = c_parser_peek_token (parser)->location;
1405 store_parm_decls ();
1406 fnbody = c_parser_compound_statement (parser);
1409 tree decl = current_function_decl;
1412 pop_function_context ();
1413 add_stmt (build_stmt (DECL_EXPR, decl));
1424 /* Parse an asm-definition (asm() outside a function body). This is a
1432 c_parser_asm_definition (c_parser *parser)
1434 tree asm_str = c_parser_simple_asm_expr (parser);
1436 cgraph_add_asm_node (asm_str);
1437 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
1440 /* Parse some declaration specifiers (possibly none) (C90 6.5, C99
1441 6.7), adding them to SPECS (which may already include some).
1442 Storage class specifiers are accepted iff SCSPEC_OK; type
1443 specifiers are accepted iff TYPESPEC_OK; attributes are accepted at
1444 the start iff START_ATTR_OK.
1446 declaration-specifiers:
1447 storage-class-specifier declaration-specifiers[opt]
1448 type-specifier declaration-specifiers[opt]
1449 type-qualifier declaration-specifiers[opt]
1450 function-specifier declaration-specifiers[opt]
1452 Function specifiers (inline) are from C99, and are currently
1453 handled as storage class specifiers, as is __thread.
1455 C90 6.5.1, C99 6.7.1:
1456 storage-class-specifier:
1467 C90 6.5.2, C99 6.7.2:
1480 [_Imaginary removed in C99 TC2]
1481 struct-or-union-specifier
1485 (_Bool and _Complex are new in C99.)
1487 C90 6.5.3, C99 6.7.3:
1494 (restrict is new in C99.)
1498 declaration-specifiers:
1499 attributes declaration-specifiers[opt]
1501 storage-class-specifier:
1513 (_Fract, _Accum, and _Sat are new from ISO/IEC DTR 18037:
1514 http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1169.pdf)
1519 class-name objc-protocol-refs[opt]
1520 typedef-name objc-protocol-refs
1525 c_parser_declspecs (c_parser *parser, struct c_declspecs *specs,
1526 bool scspec_ok, bool typespec_ok, bool start_attr_ok)
1528 bool attrs_ok = start_attr_ok;
1529 bool seen_type = specs->type_seen_p;
1530 while (c_parser_next_token_is (parser, CPP_NAME)
1531 || c_parser_next_token_is (parser, CPP_KEYWORD)
1532 || (c_dialect_objc () && c_parser_next_token_is (parser, CPP_LESS)))
1534 struct c_typespec t;
1536 if (c_parser_next_token_is (parser, CPP_NAME))
1538 tree value = c_parser_peek_token (parser)->value;
1539 c_id_kind kind = c_parser_peek_token (parser)->id_kind;
1540 /* This finishes the specifiers unless a type name is OK, it
1541 is declared as a type name and a type name hasn't yet
1543 if (!typespec_ok || seen_type
1544 || (kind != C_ID_TYPENAME && kind != C_ID_CLASSNAME))
1546 c_parser_consume_token (parser);
1549 if (kind == C_ID_TYPENAME
1550 && (!c_dialect_objc ()
1551 || c_parser_next_token_is_not (parser, CPP_LESS)))
1553 t.kind = ctsk_typedef;
1554 /* For a typedef name, record the meaning, not the name.
1555 In case of 'foo foo, bar;'. */
1556 t.spec = lookup_name (value);
1560 tree proto = NULL_TREE;
1561 gcc_assert (c_dialect_objc ());
1563 if (c_parser_next_token_is (parser, CPP_LESS))
1564 proto = c_parser_objc_protocol_refs (parser);
1565 t.spec = objc_get_protocol_qualified_type (value, proto);
1567 declspecs_add_type (specs, t);
1570 if (c_parser_next_token_is (parser, CPP_LESS))
1572 /* Make "<SomeProtocol>" equivalent to "id <SomeProtocol>" -
1573 nisse@lysator.liu.se. */
1575 gcc_assert (c_dialect_objc ());
1576 if (!typespec_ok || seen_type)
1578 proto = c_parser_objc_protocol_refs (parser);
1580 t.spec = objc_get_protocol_qualified_type (NULL_TREE, proto);
1581 declspecs_add_type (specs, t);
1584 gcc_assert (c_parser_next_token_is (parser, CPP_KEYWORD));
1585 switch (c_parser_peek_token (parser)->keyword)
1597 /* TODO: Distinguish between function specifiers (inline)
1598 and storage class specifiers, either here or in
1599 declspecs_add_scspec. */
1600 declspecs_add_scspec (specs, c_parser_peek_token (parser)->value);
1601 c_parser_consume_token (parser);
1624 if (c_dialect_objc ())
1625 parser->objc_need_raw_identifier = true;
1626 t.kind = ctsk_resword;
1627 t.spec = c_parser_peek_token (parser)->value;
1628 declspecs_add_type (specs, t);
1629 c_parser_consume_token (parser);
1636 t = c_parser_enum_specifier (parser);
1637 declspecs_add_type (specs, t);
1645 t = c_parser_struct_or_union_specifier (parser);
1646 declspecs_add_type (specs, t);
1649 /* ??? The old parser rejected typeof after other type
1650 specifiers, but is a syntax error the best way of
1652 if (!typespec_ok || seen_type)
1656 t = c_parser_typeof_specifier (parser);
1657 declspecs_add_type (specs, t);
1663 declspecs_add_qual (specs, c_parser_peek_token (parser)->value);
1664 c_parser_consume_token (parser);
1669 attrs = c_parser_attributes (parser);
1670 declspecs_add_attrs (specs, attrs);
1679 /* Parse an enum specifier (C90 6.5.2.2, C99 6.7.2.2).
1682 enum attributes[opt] identifier[opt] { enumerator-list } attributes[opt]
1683 enum attributes[opt] identifier[opt] { enumerator-list , } attributes[opt]
1684 enum attributes[opt] identifier
1686 The form with trailing comma is new in C99. The forms with
1687 attributes are GNU extensions. In GNU C, we accept any expression
1688 without commas in the syntax (assignment expressions, not just
1689 conditional expressions); assignment expressions will be diagnosed
1694 enumerator-list , enumerator
1697 enumeration-constant
1698 enumeration-constant = constant-expression
1701 static struct c_typespec
1702 c_parser_enum_specifier (c_parser *parser)
1704 struct c_typespec ret;
1706 tree ident = NULL_TREE;
1707 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ENUM));
1708 c_parser_consume_token (parser);
1709 attrs = c_parser_attributes (parser);
1710 /* Set the location in case we create a decl now. */
1711 c_parser_set_source_position_from_token (c_parser_peek_token (parser));
1712 if (c_parser_next_token_is (parser, CPP_NAME))
1714 ident = c_parser_peek_token (parser)->value;
1715 c_parser_consume_token (parser);
1717 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
1719 /* Parse an enum definition. */
1720 struct c_enum_contents the_enum;
1721 tree type = start_enum (&the_enum, ident);
1723 /* We chain the enumerators in reverse order, then put them in
1724 forward order at the end. */
1725 tree values = NULL_TREE;
1726 c_parser_consume_token (parser);
1734 if (c_parser_next_token_is_not (parser, CPP_NAME))
1736 c_parser_error (parser, "expected identifier");
1737 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
1738 values = error_mark_node;
1741 token = c_parser_peek_token (parser);
1742 enum_id = token->value;
1743 /* Set the location in case we create a decl now. */
1744 c_parser_set_source_position_from_token (token);
1745 c_parser_consume_token (parser);
1746 if (c_parser_next_token_is (parser, CPP_EQ))
1748 c_parser_consume_token (parser);
1749 enum_value = c_parser_expr_no_commas (parser, NULL).value;
1752 enum_value = NULL_TREE;
1753 enum_decl = build_enumerator (&the_enum, enum_id, enum_value);
1754 TREE_CHAIN (enum_decl) = values;
1757 if (c_parser_next_token_is (parser, CPP_COMMA))
1760 c_parser_consume_token (parser);
1762 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
1764 if (seen_comma && pedantic && !flag_isoc99)
1765 pedwarn ("comma at end of enumerator list");
1766 c_parser_consume_token (parser);
1771 c_parser_error (parser, "expected %<,%> or %<}%>");
1772 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
1773 values = error_mark_node;
1777 postfix_attrs = c_parser_attributes (parser);
1778 ret.spec = finish_enum (type, nreverse (values),
1779 chainon (attrs, postfix_attrs));
1780 ret.kind = ctsk_tagdef;
1785 c_parser_error (parser, "expected %<{%>");
1786 ret.spec = error_mark_node;
1787 ret.kind = ctsk_tagref;
1790 ret = parser_xref_tag (ENUMERAL_TYPE, ident);
1791 /* In ISO C, enumerated types can be referred to only if already
1793 if (pedantic && !COMPLETE_TYPE_P (ret.spec))
1794 pedwarn ("ISO C forbids forward references to %<enum%> types");
1798 /* Parse a struct or union specifier (C90 6.5.2.1, C99 6.7.2.1).
1800 struct-or-union-specifier:
1801 struct-or-union attributes[opt] identifier[opt]
1802 { struct-contents } attributes[opt]
1803 struct-or-union attributes[opt] identifier
1806 struct-declaration-list
1808 struct-declaration-list:
1809 struct-declaration ;
1810 struct-declaration-list struct-declaration ;
1817 struct-declaration-list struct-declaration
1819 struct-declaration-list:
1820 struct-declaration-list ;
1823 (Note that in the syntax here, unlike that in ISO C, the semicolons
1824 are included here rather than in struct-declaration, in order to
1825 describe the syntax with extra semicolons and missing semicolon at
1830 struct-declaration-list:
1831 @defs ( class-name )
1833 (Note this does not include a trailing semicolon, but can be
1834 followed by further declarations, and gets a pedwarn-if-pedantic
1835 when followed by a semicolon.) */
1837 static struct c_typespec
1838 c_parser_struct_or_union_specifier (c_parser *parser)
1840 struct c_typespec ret;
1842 tree ident = NULL_TREE;
1843 enum tree_code code;
1844 switch (c_parser_peek_token (parser)->keyword)
1855 c_parser_consume_token (parser);
1856 attrs = c_parser_attributes (parser);
1857 /* Set the location in case we create a decl now. */
1858 c_parser_set_source_position_from_token (c_parser_peek_token (parser));
1859 if (c_parser_next_token_is (parser, CPP_NAME))
1861 ident = c_parser_peek_token (parser)->value;
1862 c_parser_consume_token (parser);
1864 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
1866 /* Parse a struct or union definition. Start the scope of the
1867 tag before parsing components. */
1868 tree type = start_struct (code, ident);
1870 /* We chain the components in reverse order, then put them in
1871 forward order at the end. Each struct-declaration may
1872 declare multiple components (comma-separated), so we must use
1873 chainon to join them, although when parsing each
1874 struct-declaration we can use TREE_CHAIN directly.
1876 The theory behind all this is that there will be more
1877 semicolon separated fields than comma separated fields, and
1878 so we'll be minimizing the number of node traversals required
1880 tree contents = NULL_TREE;
1881 c_parser_consume_token (parser);
1882 /* Handle the Objective-C @defs construct,
1883 e.g. foo(sizeof(struct{ @defs(ClassName) }));. */
1884 if (c_parser_next_token_is_keyword (parser, RID_AT_DEFS))
1887 gcc_assert (c_dialect_objc ());
1888 c_parser_consume_token (parser);
1889 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
1891 if (c_parser_next_token_is (parser, CPP_NAME)
1892 && c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME)
1894 name = c_parser_peek_token (parser)->value;
1895 c_parser_consume_token (parser);
1899 c_parser_error (parser, "expected class name");
1900 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
1903 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
1905 contents = nreverse (objc_get_class_ivars (name));
1908 /* Parse the struct-declarations and semicolons. Problems with
1909 semicolons are diagnosed here; empty structures are diagnosed
1914 /* Parse any stray semicolon. */
1915 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1918 pedwarn ("extra semicolon in struct or union specified");
1919 c_parser_consume_token (parser);
1922 /* Stop if at the end of the struct or union contents. */
1923 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
1925 c_parser_consume_token (parser);
1928 /* Accept #pragmas at struct scope. */
1929 if (c_parser_next_token_is (parser, CPP_PRAGMA))
1931 c_parser_pragma (parser, pragma_external);
1934 /* Parse some comma-separated declarations, but not the
1935 trailing semicolon if any. */
1936 decls = c_parser_struct_declaration (parser);
1937 contents = chainon (decls, contents);
1938 /* If no semicolon follows, either we have a parse error or
1939 are at the end of the struct or union and should
1941 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1942 c_parser_consume_token (parser);
1945 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
1946 pedwarn ("no semicolon at end of struct or union");
1949 c_parser_error (parser, "expected %<;%>");
1950 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
1955 postfix_attrs = c_parser_attributes (parser);
1956 ret.spec = finish_struct (type, nreverse (contents),
1957 chainon (attrs, postfix_attrs));
1958 ret.kind = ctsk_tagdef;
1963 c_parser_error (parser, "expected %<{%>");
1964 ret.spec = error_mark_node;
1965 ret.kind = ctsk_tagref;
1968 ret = parser_xref_tag (code, ident);
1972 /* Parse a struct-declaration (C90 6.5.2.1, C99 6.7.2.1), *without*
1973 the trailing semicolon.
1976 specifier-qualifier-list struct-declarator-list
1978 specifier-qualifier-list:
1979 type-specifier specifier-qualifier-list[opt]
1980 type-qualifier specifier-qualifier-list[opt]
1981 attributes specifier-qualifier-list[opt]
1983 struct-declarator-list:
1985 struct-declarator-list , attributes[opt] struct-declarator
1988 declarator attributes[opt]
1989 declarator[opt] : constant-expression attributes[opt]
1994 __extension__ struct-declaration
1995 specifier-qualifier-list
1997 Unlike the ISO C syntax, semicolons are handled elsewhere. The use
1998 of attributes where shown is a GNU extension. In GNU C, we accept
1999 any expression without commas in the syntax (assignment
2000 expressions, not just conditional expressions); assignment
2001 expressions will be diagnosed as non-constant. */
2004 c_parser_struct_declaration (c_parser *parser)
2006 struct c_declspecs *specs;
2008 tree all_prefix_attrs;
2010 if (c_parser_next_token_is_keyword (parser, RID_EXTENSION))
2014 ext = disable_extension_diagnostics ();
2015 c_parser_consume_token (parser);
2016 decl = c_parser_struct_declaration (parser);
2017 restore_extension_diagnostics (ext);
2020 specs = build_null_declspecs ();
2021 c_parser_declspecs (parser, specs, false, true, true);
2024 if (!specs->declspecs_seen_p)
2026 c_parser_error (parser, "expected specifier-qualifier-list");
2029 finish_declspecs (specs);
2030 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
2033 if (!specs->type_seen_p)
2036 pedwarn ("ISO C forbids member declarations with no members");
2037 shadow_tag_warned (specs, pedantic);
2042 /* Support for unnamed structs or unions as members of
2043 structs or unions (which is [a] useful and [b] supports
2046 ret = grokfield (build_id_declarator (NULL_TREE), specs,
2049 decl_attributes (&ret, attrs, 0);
2053 pending_xref_error ();
2054 prefix_attrs = specs->attrs;
2055 all_prefix_attrs = prefix_attrs;
2056 specs->attrs = NULL_TREE;
2060 /* Declaring one or more declarators or un-named bit-fields. */
2061 struct c_declarator *declarator;
2063 if (c_parser_next_token_is (parser, CPP_COLON))
2064 declarator = build_id_declarator (NULL_TREE);
2066 declarator = c_parser_declarator (parser, specs->type_seen_p,
2067 C_DTR_NORMAL, &dummy);
2068 if (declarator == NULL)
2070 c_parser_skip_to_end_of_block_or_statement (parser);
2073 if (c_parser_next_token_is (parser, CPP_COLON)
2074 || c_parser_next_token_is (parser, CPP_COMMA)
2075 || c_parser_next_token_is (parser, CPP_SEMICOLON)
2076 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE)
2077 || c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2079 tree postfix_attrs = NULL_TREE;
2080 tree width = NULL_TREE;
2082 if (c_parser_next_token_is (parser, CPP_COLON))
2084 c_parser_consume_token (parser);
2085 width = c_parser_expr_no_commas (parser, NULL).value;
2087 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2088 postfix_attrs = c_parser_attributes (parser);
2089 d = grokfield (declarator, specs, width, &all_prefix_attrs);
2090 decl_attributes (&d, chainon (postfix_attrs,
2091 all_prefix_attrs), 0);
2092 TREE_CHAIN (d) = decls;
2094 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2095 all_prefix_attrs = chainon (c_parser_attributes (parser),
2098 all_prefix_attrs = prefix_attrs;
2099 if (c_parser_next_token_is (parser, CPP_COMMA))
2100 c_parser_consume_token (parser);
2101 else if (c_parser_next_token_is (parser, CPP_SEMICOLON)
2102 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2104 /* Semicolon consumed in caller. */
2109 c_parser_error (parser, "expected %<,%>, %<;%> or %<}%>");
2115 c_parser_error (parser,
2116 "expected %<:%>, %<,%>, %<;%>, %<}%> or "
2117 "%<__attribute__%>");
2124 /* Parse a typeof specifier (a GNU extension).
2127 typeof ( expression )
2128 typeof ( type-name )
2131 static struct c_typespec
2132 c_parser_typeof_specifier (c_parser *parser)
2134 struct c_typespec ret;
2135 ret.kind = ctsk_typeof;
2136 ret.spec = error_mark_node;
2137 gcc_assert (c_parser_next_token_is_keyword (parser, RID_TYPEOF));
2138 c_parser_consume_token (parser);
2141 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2147 if (c_parser_next_token_starts_typename (parser))
2149 struct c_type_name *type = c_parser_type_name (parser);
2154 ret.spec = groktypename (type);
2155 pop_maybe_used (variably_modified_type_p (ret.spec, NULL_TREE));
2161 struct c_expr expr = c_parser_expression (parser);
2164 if (TREE_CODE (expr.value) == COMPONENT_REF
2165 && DECL_C_BIT_FIELD (TREE_OPERAND (expr.value, 1)))
2166 error ("%<typeof%> applied to a bit-field");
2167 ret.spec = TREE_TYPE (expr.value);
2168 was_vm = variably_modified_type_p (ret.spec, NULL_TREE);
2169 /* This should be returned with the type so that when the type
2170 is evaluated, this can be evaluated. For now, we avoid
2171 evaluation when the context might. */
2172 if (!skip_evaluation && was_vm)
2174 tree e = expr.value;
2176 /* If the expression is not of a type to which we cannot assign a line
2177 number, wrap the thing in a no-op NOP_EXPR. */
2178 if (DECL_P (e) || CONSTANT_CLASS_P (e))
2179 e = build1 (NOP_EXPR, void_type_node, e);
2181 if (CAN_HAVE_LOCATION_P (e))
2182 SET_EXPR_LOCATION (e, input_location);
2186 pop_maybe_used (was_vm);
2188 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
2192 /* Parse a declarator, possibly an abstract declarator (C90 6.5.4,
2193 6.5.5, C99 6.7.5, 6.7.6). If TYPE_SEEN_P then a typedef name may
2194 be redeclared; otherwise it may not. KIND indicates which kind of
2195 declarator is wanted. Returns a valid declarator except in the
2196 case of a syntax error in which case NULL is returned. *SEEN_ID is
2197 set to true if an identifier being declared is seen; this is used
2198 to diagnose bad forms of abstract array declarators and to
2199 determine whether an identifier list is syntactically permitted.
2202 pointer[opt] direct-declarator
2206 ( attributes[opt] declarator )
2207 direct-declarator array-declarator
2208 direct-declarator ( parameter-type-list )
2209 direct-declarator ( identifier-list[opt] )
2212 * type-qualifier-list[opt]
2213 * type-qualifier-list[opt] pointer
2215 type-qualifier-list:
2218 type-qualifier-list type-qualifier
2219 type-qualifier-list attributes
2221 parameter-type-list:
2223 parameter-list , ...
2226 parameter-declaration
2227 parameter-list , parameter-declaration
2229 parameter-declaration:
2230 declaration-specifiers declarator attributes[opt]
2231 declaration-specifiers abstract-declarator[opt] attributes[opt]
2235 identifier-list , identifier
2237 abstract-declarator:
2239 pointer[opt] direct-abstract-declarator
2241 direct-abstract-declarator:
2242 ( attributes[opt] abstract-declarator )
2243 direct-abstract-declarator[opt] array-declarator
2244 direct-abstract-declarator[opt] ( parameter-type-list[opt] )
2249 direct-declarator ( parameter-forward-declarations
2250 parameter-type-list[opt] )
2252 direct-abstract-declarator:
2253 direct-abstract-declarator[opt] ( parameter-forward-declarations
2254 parameter-type-list[opt] )
2256 parameter-forward-declarations:
2258 parameter-forward-declarations parameter-list ;
2260 The uses of attributes shown above are GNU extensions.
2262 Some forms of array declarator are not included in C99 in the
2263 syntax for abstract declarators; these are disallowed elsewhere.
2264 This may be a defect (DR#289).
2266 This function also accepts an omitted abstract declarator as being
2267 an abstract declarator, although not part of the formal syntax. */
2269 static struct c_declarator *
2270 c_parser_declarator (c_parser *parser, bool type_seen_p, c_dtr_syn kind,
2273 /* Parse any initial pointer part. */
2274 if (c_parser_next_token_is (parser, CPP_MULT))
2276 struct c_declspecs *quals_attrs = build_null_declspecs ();
2277 struct c_declarator *inner;
2278 c_parser_consume_token (parser);
2279 c_parser_declspecs (parser, quals_attrs, false, false, true);
2280 inner = c_parser_declarator (parser, type_seen_p, kind, seen_id);
2284 return make_pointer_declarator (quals_attrs, inner);
2286 /* Now we have a direct declarator, direct abstract declarator or
2287 nothing (which counts as a direct abstract declarator here). */
2288 return c_parser_direct_declarator (parser, type_seen_p, kind, seen_id);
2291 /* Parse a direct declarator or direct abstract declarator; arguments
2292 as c_parser_declarator. */
2294 static struct c_declarator *
2295 c_parser_direct_declarator (c_parser *parser, bool type_seen_p, c_dtr_syn kind,
2298 /* The direct declarator must start with an identifier (possibly
2299 omitted) or a parenthesized declarator (possibly abstract). In
2300 an ordinary declarator, initial parentheses must start a
2301 parenthesized declarator. In an abstract declarator or parameter
2302 declarator, they could start a parenthesized declarator or a
2303 parameter list. To tell which, the open parenthesis and any
2304 following attributes must be read. If a declaration specifier
2305 follows, then it is a parameter list; if the specifier is a
2306 typedef name, there might be an ambiguity about redeclaring it,
2307 which is resolved in the direction of treating it as a typedef
2308 name. If a close parenthesis follows, it is also an empty
2309 parameter list, as the syntax does not permit empty abstract
2310 declarators. Otherwise, it is a parenthesized declarator (in
2311 which case the analysis may be repeated inside it, recursively).
2313 ??? There is an ambiguity in a parameter declaration "int
2314 (__attribute__((foo)) x)", where x is not a typedef name: it
2315 could be an abstract declarator for a function, or declare x with
2316 parentheses. The proper resolution of this ambiguity needs
2317 documenting. At present we follow an accident of the old
2318 parser's implementation, whereby the first parameter must have
2319 some declaration specifiers other than just attributes. Thus as
2320 a parameter declaration it is treated as a parenthesized
2321 parameter named x, and as an abstract declarator it is
2324 ??? Also following the old parser, attributes inside an empty
2325 parameter list are ignored, making it a list not yielding a
2326 prototype, rather than giving an error or making it have one
2327 parameter with implicit type int.
2329 ??? Also following the old parser, typedef names may be
2330 redeclared in declarators, but not Objective-C class names. */
2332 if (kind != C_DTR_ABSTRACT
2333 && c_parser_next_token_is (parser, CPP_NAME)
2335 && c_parser_peek_token (parser)->id_kind == C_ID_TYPENAME)
2336 || c_parser_peek_token (parser)->id_kind == C_ID_ID))
2338 struct c_declarator *inner
2339 = build_id_declarator (c_parser_peek_token (parser)->value);
2341 inner->id_loc = c_parser_peek_token (parser)->location;
2342 c_parser_consume_token (parser);
2343 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
2346 if (kind != C_DTR_NORMAL
2347 && c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
2349 struct c_declarator *inner = build_id_declarator (NULL_TREE);
2350 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
2353 /* Either we are at the end of an abstract declarator, or we have
2356 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
2359 struct c_declarator *inner;
2360 c_parser_consume_token (parser);
2361 attrs = c_parser_attributes (parser);
2362 if (kind != C_DTR_NORMAL
2363 && (c_parser_next_token_starts_declspecs (parser)
2364 || c_parser_next_token_is (parser, CPP_CLOSE_PAREN)))
2366 struct c_arg_info *args
2367 = c_parser_parms_declarator (parser, kind == C_DTR_NORMAL,
2374 = build_function_declarator (args,
2375 build_id_declarator (NULL_TREE));
2376 return c_parser_direct_declarator_inner (parser, *seen_id,
2380 /* A parenthesized declarator. */
2381 inner = c_parser_declarator (parser, type_seen_p, kind, seen_id);
2382 if (inner != NULL && attrs != NULL)
2383 inner = build_attrs_declarator (attrs, inner);
2384 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2386 c_parser_consume_token (parser);
2390 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
2394 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2401 if (kind == C_DTR_NORMAL)
2403 c_parser_error (parser, "expected identifier or %<(%>");
2407 return build_id_declarator (NULL_TREE);
2411 /* Parse part of a direct declarator or direct abstract declarator,
2412 given that some (in INNER) has already been parsed; ID_PRESENT is
2413 true if an identifier is present, false for an abstract
2416 static struct c_declarator *
2417 c_parser_direct_declarator_inner (c_parser *parser, bool id_present,
2418 struct c_declarator *inner)
2420 /* Parse a sequence of array declarators and parameter lists. */
2421 if (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
2423 struct c_declarator *declarator;
2424 struct c_declspecs *quals_attrs = build_null_declspecs ();
2428 c_parser_consume_token (parser);
2429 c_parser_declspecs (parser, quals_attrs, false, false, true);
2430 static_seen = c_parser_next_token_is_keyword (parser, RID_STATIC);
2432 c_parser_consume_token (parser);
2433 if (static_seen && !quals_attrs->declspecs_seen_p)
2434 c_parser_declspecs (parser, quals_attrs, false, false, true);
2435 if (!quals_attrs->declspecs_seen_p)
2437 /* If "static" is present, there must be an array dimension.
2438 Otherwise, there may be a dimension, "*", or no
2443 dimen = c_parser_expr_no_commas (parser, NULL).value;
2447 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
2452 else if (c_parser_next_token_is (parser, CPP_MULT))
2454 if (c_parser_peek_2nd_token (parser)->type == CPP_CLOSE_SQUARE)
2458 c_parser_consume_token (parser);
2463 dimen = c_parser_expr_no_commas (parser, NULL).value;
2469 dimen = c_parser_expr_no_commas (parser, NULL).value;
2472 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
2473 c_parser_consume_token (parser);
2476 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
2480 declarator = build_array_declarator (dimen, quals_attrs, static_seen,
2482 if (declarator == NULL)
2484 inner = set_array_declarator_inner (declarator, inner, !id_present);
2485 return c_parser_direct_declarator_inner (parser, id_present, inner);
2487 else if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
2490 struct c_arg_info *args;
2491 c_parser_consume_token (parser);
2492 attrs = c_parser_attributes (parser);
2493 args = c_parser_parms_declarator (parser, id_present, attrs);
2498 inner = build_function_declarator (args, inner);
2499 return c_parser_direct_declarator_inner (parser, id_present, inner);
2505 /* Parse a parameter list or identifier list, including the closing
2506 parenthesis but not the opening one. ATTRS are the attributes at
2507 the start of the list. ID_LIST_OK is true if an identifier list is
2508 acceptable; such a list must not have attributes at the start. */
2510 static struct c_arg_info *
2511 c_parser_parms_declarator (c_parser *parser, bool id_list_ok, tree attrs)
2514 declare_parm_level ();
2515 /* If the list starts with an identifier, it is an identifier list.
2516 Otherwise, it is either a prototype list or an empty list. */
2519 && c_parser_next_token_is (parser, CPP_NAME)
2520 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
2522 tree list = NULL_TREE, *nextp = &list;
2523 while (c_parser_next_token_is (parser, CPP_NAME)
2524 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
2526 *nextp = build_tree_list (NULL_TREE,
2527 c_parser_peek_token (parser)->value);
2528 nextp = & TREE_CHAIN (*nextp);
2529 c_parser_consume_token (parser);
2530 if (c_parser_next_token_is_not (parser, CPP_COMMA))
2532 c_parser_consume_token (parser);
2533 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2535 c_parser_error (parser, "expected identifier");
2539 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2541 struct c_arg_info *ret = XOBNEW (&parser_obstack, struct c_arg_info);
2546 ret->pending_sizes = 0;
2547 ret->had_vla_unspec = 0;
2548 c_parser_consume_token (parser);
2554 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2562 struct c_arg_info *ret = c_parser_parms_list_declarator (parser, attrs);
2568 /* Parse a parameter list (possibly empty), including the closing
2569 parenthesis but not the opening one. ATTRS are the attributes at
2570 the start of the list. */
2572 static struct c_arg_info *
2573 c_parser_parms_list_declarator (c_parser *parser, tree attrs)
2575 bool good_parm = false;
2576 /* ??? Following the old parser, forward parameter declarations may
2577 use abstract declarators, and if no real parameter declarations
2578 follow the forward declarations then this is not diagnosed. Also
2579 note as above that attributes are ignored as the only contents of
2580 the parentheses, or as the only contents after forward
2582 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2584 struct c_arg_info *ret = XOBNEW (&parser_obstack, struct c_arg_info);
2589 ret->pending_sizes = 0;
2590 ret->had_vla_unspec = 0;
2591 c_parser_consume_token (parser);
2594 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
2596 struct c_arg_info *ret = XOBNEW (&parser_obstack, struct c_arg_info);
2600 ret->pending_sizes = 0;
2601 ret->had_vla_unspec = 0;
2602 /* Suppress -Wold-style-definition for this case. */
2603 ret->types = error_mark_node;
2604 error ("ISO C requires a named argument before %<...%>");
2605 c_parser_consume_token (parser);
2606 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2608 c_parser_consume_token (parser);
2613 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2618 /* Nonempty list of parameters, either terminated with semicolon
2619 (forward declarations; recurse) or with close parenthesis (normal
2620 function) or with ", ... )" (variadic function). */
2623 /* Parse a parameter. */
2624 struct c_parm *parm = c_parser_parameter_declaration (parser, attrs);
2629 push_parm_decl (parm);
2631 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
2634 c_parser_consume_token (parser);
2635 mark_forward_parm_decls ();
2636 new_attrs = c_parser_attributes (parser);
2637 return c_parser_parms_list_declarator (parser, new_attrs);
2639 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2641 c_parser_consume_token (parser);
2643 return get_parm_info (false);
2646 struct c_arg_info *ret
2647 = XOBNEW (&parser_obstack, struct c_arg_info);
2652 ret->pending_sizes = 0;
2653 ret->had_vla_unspec = 0;
2657 if (!c_parser_require (parser, CPP_COMMA,
2658 "expected %<;%>, %<,%> or %<)%>"))
2660 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
2663 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
2665 c_parser_consume_token (parser);
2666 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2668 c_parser_consume_token (parser);
2670 return get_parm_info (true);
2673 struct c_arg_info *ret
2674 = XOBNEW (&parser_obstack, struct c_arg_info);
2679 ret->pending_sizes = 0;
2680 ret->had_vla_unspec = 0;
2686 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2694 /* Parse a parameter declaration. ATTRS are the attributes at the
2695 start of the declaration if it is the first parameter. */
2697 static struct c_parm *
2698 c_parser_parameter_declaration (c_parser *parser, tree attrs)
2700 struct c_declspecs *specs;
2701 struct c_declarator *declarator;
2703 tree postfix_attrs = NULL_TREE;
2705 if (!c_parser_next_token_starts_declspecs (parser))
2707 /* ??? In some Objective-C cases '...' isn't applicable so there
2708 should be a different message. */
2709 c_parser_error (parser,
2710 "expected declaration specifiers or %<...%>");
2711 c_parser_skip_to_end_of_parameter (parser);
2714 specs = build_null_declspecs ();
2717 declspecs_add_attrs (specs, attrs);
2720 c_parser_declspecs (parser, specs, true, true, true);
2721 finish_declspecs (specs);
2722 pending_xref_error ();
2723 prefix_attrs = specs->attrs;
2724 specs->attrs = NULL_TREE;
2725 declarator = c_parser_declarator (parser, specs->type_seen_p,
2726 C_DTR_PARM, &dummy);
2727 if (declarator == NULL)
2729 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
2732 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2733 postfix_attrs = c_parser_attributes (parser);
2734 return build_c_parm (specs, chainon (postfix_attrs, prefix_attrs),
2738 /* Parse a string literal in an asm expression. It should not be
2739 translated, and wide string literals are an error although
2740 permitted by the syntax. This is a GNU extension.
2745 ??? At present, following the old parser, the caller needs to have
2746 set lex_untranslated_string to 1. It would be better to follow the
2747 C++ parser rather than using this kludge. */
2750 c_parser_asm_string_literal (c_parser *parser)
2753 if (c_parser_next_token_is (parser, CPP_STRING))
2755 str = c_parser_peek_token (parser)->value;
2756 c_parser_consume_token (parser);
2758 else if (c_parser_next_token_is (parser, CPP_WSTRING))
2760 error ("wide string literal in %<asm%>");
2761 str = build_string (1, "");
2762 c_parser_consume_token (parser);
2766 c_parser_error (parser, "expected string literal");
2772 /* Parse a simple asm expression. This is used in restricted
2773 contexts, where a full expression with inputs and outputs does not
2774 make sense. This is a GNU extension.
2777 asm ( asm-string-literal )
2781 c_parser_simple_asm_expr (c_parser *parser)
2784 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ASM));
2785 /* ??? Follow the C++ parser rather than using the
2786 lex_untranslated_string kludge. */
2787 parser->lex_untranslated_string = true;
2788 c_parser_consume_token (parser);
2789 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2791 parser->lex_untranslated_string = false;
2794 str = c_parser_asm_string_literal (parser);
2795 parser->lex_untranslated_string = false;
2796 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
2798 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
2804 /* Parse (possibly empty) attributes. This is a GNU extension.
2808 attributes attribute
2811 __attribute__ ( ( attribute-list ) )
2815 attribute_list , attrib
2820 any-word ( identifier )
2821 any-word ( identifier , nonempty-expr-list )
2822 any-word ( expr-list )
2824 where the "identifier" must not be declared as a type, and
2825 "any-word" may be any identifier (including one declared as a
2826 type), a reserved word storage class specifier, type specifier or
2827 type qualifier. ??? This still leaves out most reserved keywords
2828 (following the old parser), shouldn't we include them, and why not
2829 allow identifiers declared as types to start the arguments? */
2832 c_parser_attributes (c_parser *parser)
2834 tree attrs = NULL_TREE;
2835 while (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2837 /* ??? Follow the C++ parser rather than using the
2838 lex_untranslated_string kludge. */
2839 parser->lex_untranslated_string = true;
2840 c_parser_consume_token (parser);
2841 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2843 parser->lex_untranslated_string = false;
2846 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2848 parser->lex_untranslated_string = false;
2849 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
2852 /* Parse the attribute list. */
2853 while (c_parser_next_token_is (parser, CPP_COMMA)
2854 || c_parser_next_token_is (parser, CPP_NAME)
2855 || c_parser_next_token_is (parser, CPP_KEYWORD))
2857 tree attr, attr_name, attr_args;
2858 if (c_parser_next_token_is (parser, CPP_COMMA))
2860 c_parser_consume_token (parser);
2863 if (c_parser_next_token_is (parser, CPP_KEYWORD))
2865 /* ??? See comment above about what keywords are
2868 switch (c_parser_peek_token (parser)->keyword)
2906 attr_name = c_parser_peek_token (parser)->value;
2907 c_parser_consume_token (parser);
2908 if (c_parser_next_token_is_not (parser, CPP_OPEN_PAREN))
2910 attr = build_tree_list (attr_name, NULL_TREE);
2911 attrs = chainon (attrs, attr);
2914 c_parser_consume_token (parser);
2915 /* Parse the attribute contents. If they start with an
2916 identifier which is followed by a comma or close
2917 parenthesis, then the arguments start with that
2918 identifier; otherwise they are an expression list. */
2919 if (c_parser_next_token_is (parser, CPP_NAME)
2920 && c_parser_peek_token (parser)->id_kind == C_ID_ID
2921 && ((c_parser_peek_2nd_token (parser)->type == CPP_COMMA)
2922 || (c_parser_peek_2nd_token (parser)->type
2923 == CPP_CLOSE_PAREN)))
2925 tree arg1 = c_parser_peek_token (parser)->value;
2926 c_parser_consume_token (parser);
2927 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2928 attr_args = build_tree_list (NULL_TREE, arg1);
2931 c_parser_consume_token (parser);
2932 attr_args = tree_cons (NULL_TREE, arg1,
2933 c_parser_expr_list (parser, false));
2938 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2939 attr_args = NULL_TREE;
2941 attr_args = c_parser_expr_list (parser, false);
2943 attr = build_tree_list (attr_name, attr_args);
2944 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2945 c_parser_consume_token (parser);
2948 parser->lex_untranslated_string = false;
2949 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2953 attrs = chainon (attrs, attr);
2955 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2956 c_parser_consume_token (parser);
2959 parser->lex_untranslated_string = false;
2960 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2964 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2965 c_parser_consume_token (parser);
2968 parser->lex_untranslated_string = false;
2969 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2973 parser->lex_untranslated_string = false;
2978 /* Parse a type name (C90 6.5.5, C99 6.7.6).
2981 specifier-qualifier-list abstract-declarator[opt]
2984 static struct c_type_name *
2985 c_parser_type_name (c_parser *parser)
2987 struct c_declspecs *specs = build_null_declspecs ();
2988 struct c_declarator *declarator;
2989 struct c_type_name *ret;
2991 c_parser_declspecs (parser, specs, false, true, true);
2992 if (!specs->declspecs_seen_p)
2994 c_parser_error (parser, "expected specifier-qualifier-list");
2997 pending_xref_error ();
2998 finish_declspecs (specs);
2999 declarator = c_parser_declarator (parser, specs->type_seen_p,
3000 C_DTR_ABSTRACT, &dummy);
3001 if (declarator == NULL)
3003 ret = XOBNEW (&parser_obstack, struct c_type_name);
3005 ret->declarator = declarator;
3009 /* Parse an initializer (C90 6.5.7, C99 6.7.8).
3012 assignment-expression
3013 { initializer-list }
3014 { initializer-list , }
3017 designation[opt] initializer
3018 initializer-list , designation[opt] initializer
3025 designator-list designator
3032 [ constant-expression ]
3044 [ constant-expression ... constant-expression ]
3046 Any expression without commas is accepted in the syntax for the
3047 constant-expressions, with non-constant expressions rejected later.
3049 This function is only used for top-level initializers; for nested
3050 ones, see c_parser_initval. */
3052 static struct c_expr
3053 c_parser_initializer (c_parser *parser)
3055 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
3056 return c_parser_braced_init (parser, NULL_TREE, false);
3060 ret = c_parser_expr_no_commas (parser, NULL);
3061 if (TREE_CODE (ret.value) != STRING_CST
3062 && TREE_CODE (ret.value) != COMPOUND_LITERAL_EXPR)
3063 ret = default_function_array_conversion (ret);
3068 /* Parse a braced initializer list. TYPE is the type specified for a
3069 compound literal, and NULL_TREE for other initializers and for
3070 nested braced lists. NESTED_P is true for nested braced lists,
3071 false for the list of a compound literal or the list that is the
3072 top-level initializer in a declaration. */
3074 static struct c_expr
3075 c_parser_braced_init (c_parser *parser, tree type, bool nested_p)
3077 gcc_assert (c_parser_next_token_is (parser, CPP_OPEN_BRACE));
3078 c_parser_consume_token (parser);
3080 push_init_level (0);
3082 really_start_incremental_init (type);
3083 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3086 pedwarn ("ISO C forbids empty initializer braces");
3090 /* Parse a non-empty initializer list, possibly with a trailing
3094 c_parser_initelt (parser);
3097 if (c_parser_next_token_is (parser, CPP_COMMA))
3098 c_parser_consume_token (parser);
3101 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3105 if (c_parser_next_token_is_not (parser, CPP_CLOSE_BRACE))
3108 ret.value = error_mark_node;
3109 ret.original_code = ERROR_MARK;
3110 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, "expected %<}%>");
3113 c_parser_consume_token (parser);
3114 return pop_init_level (0);
3117 /* Parse a nested initializer, including designators. */
3120 c_parser_initelt (c_parser *parser)
3122 /* Parse any designator or designator list. A single array
3123 designator may have the subsequent "=" omitted in GNU C, but a
3124 longer list or a structure member designator may not. */
3125 if (c_parser_next_token_is (parser, CPP_NAME)
3126 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
3128 /* Old-style structure member designator. */
3129 set_init_label (c_parser_peek_token (parser)->value);
3131 pedwarn ("obsolete use of designated initializer with %<:%>");
3132 c_parser_consume_token (parser);
3133 c_parser_consume_token (parser);
3137 /* des_seen is 0 if there have been no designators, 1 if there
3138 has been a single array designator and 2 otherwise. */
3140 while (c_parser_next_token_is (parser, CPP_OPEN_SQUARE)
3141 || c_parser_next_token_is (parser, CPP_DOT))
3143 int des_prev = des_seen;
3146 if (c_parser_next_token_is (parser, CPP_DOT))
3149 c_parser_consume_token (parser);
3150 if (c_parser_next_token_is (parser, CPP_NAME))
3152 set_init_label (c_parser_peek_token (parser)->value);
3153 c_parser_consume_token (parser);
3158 init.value = error_mark_node;
3159 init.original_code = ERROR_MARK;
3160 c_parser_error (parser, "expected identifier");
3161 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
3162 process_init_element (init);
3169 /* ??? Following the old parser, [ objc-receiver
3170 objc-message-args ] is accepted as an initializer,
3171 being distinguished from a designator by what follows
3172 the first assignment expression inside the square
3173 brackets, but after a first array designator a
3174 subsequent square bracket is for Objective-C taken to
3175 start an expression, using the obsolete form of
3176 designated initializer without '=', rather than
3177 possibly being a second level of designation: in LALR
3178 terms, the '[' is shifted rather than reducing
3179 designator to designator-list. */
3180 if (des_prev == 1 && c_dialect_objc ())
3182 des_seen = des_prev;
3185 if (des_prev == 0 && c_dialect_objc ())
3187 /* This might be an array designator or an
3188 Objective-C message expression. If the former,
3189 continue parsing here; if the latter, parse the
3190 remainder of the initializer given the starting
3191 primary-expression. ??? It might make sense to
3192 distinguish when des_prev == 1 as well; see
3193 previous comment. */
3195 struct c_expr mexpr;
3196 c_parser_consume_token (parser);
3197 if (c_parser_peek_token (parser)->type == CPP_NAME
3198 && ((c_parser_peek_token (parser)->id_kind
3200 || (c_parser_peek_token (parser)->id_kind
3201 == C_ID_CLASSNAME)))
3203 /* Type name receiver. */
3204 tree id = c_parser_peek_token (parser)->value;
3205 c_parser_consume_token (parser);
3206 rec = objc_get_class_reference (id);
3207 goto parse_message_args;
3209 first = c_parser_expr_no_commas (parser, NULL).value;
3210 if (c_parser_next_token_is (parser, CPP_ELLIPSIS)
3211 || c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
3212 goto array_desig_after_first;
3213 /* Expression receiver. So far only one part
3214 without commas has been parsed; there might be
3215 more of the expression. */
3217 while (c_parser_next_token_is (parser, CPP_COMMA))
3220 c_parser_consume_token (parser);
3221 next = c_parser_expr_no_commas (parser, NULL);
3222 next = default_function_array_conversion (next);
3223 rec = build_compound_expr (rec, next.value);
3226 /* Now parse the objc-message-args. */
3227 args = c_parser_objc_message_args (parser);
3228 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
3231 = objc_build_message_expr (build_tree_list (rec, args));
3232 mexpr.original_code = ERROR_MARK;
3233 /* Now parse and process the remainder of the
3234 initializer, starting with this message
3235 expression as a primary-expression. */
3236 c_parser_initval (parser, &mexpr);
3239 c_parser_consume_token (parser);
3240 first = c_parser_expr_no_commas (parser, NULL).value;
3241 array_desig_after_first:
3242 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
3244 c_parser_consume_token (parser);
3245 second = c_parser_expr_no_commas (parser, NULL).value;
3249 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
3251 c_parser_consume_token (parser);
3252 set_init_index (first, second);
3253 if (pedantic && second)
3254 pedwarn ("ISO C forbids specifying range of "
3255 "elements to initialize");
3258 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
3264 if (c_parser_next_token_is (parser, CPP_EQ))
3266 if (pedantic && !flag_isoc99)
3267 pedwarn ("ISO C90 forbids specifying subobject to initialize");
3268 c_parser_consume_token (parser);
3275 pedwarn ("obsolete use of designated initializer "
3281 init.value = error_mark_node;
3282 init.original_code = ERROR_MARK;
3283 c_parser_error (parser, "expected %<=%>");
3284 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
3285 process_init_element (init);
3291 c_parser_initval (parser, NULL);
3294 /* Parse a nested initializer; as c_parser_initializer but parses
3295 initializers within braced lists, after any designators have been
3296 applied. If AFTER is not NULL then it is an Objective-C message
3297 expression which is the primary-expression starting the
3301 c_parser_initval (c_parser *parser, struct c_expr *after)
3304 gcc_assert (!after || c_dialect_objc ());
3305 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE) && !after)
3306 init = c_parser_braced_init (parser, NULL_TREE, true);
3309 init = c_parser_expr_no_commas (parser, after);
3310 if (init.value != NULL_TREE
3311 && TREE_CODE (init.value) != STRING_CST
3312 && TREE_CODE (init.value) != COMPOUND_LITERAL_EXPR)
3313 init = default_function_array_conversion (init);
3315 process_init_element (init);
3318 /* Parse a compound statement (possibly a function body) (C90 6.6.2,
3322 { block-item-list[opt] }
3323 { label-declarations block-item-list }
3327 block-item-list block-item
3339 { label-declarations block-item-list }
3342 __extension__ nested-declaration
3343 nested-function-definition
3347 label-declarations label-declaration
3350 __label__ identifier-list ;
3352 Allowing the mixing of declarations and code is new in C99. The
3353 GNU syntax also permits (not shown above) labels at the end of
3354 compound statements, which yield an error. We don't allow labels
3355 on declarations; this might seem like a natural extension, but
3356 there would be a conflict between attributes on the label and
3357 prefix attributes on the declaration. ??? The syntax follows the
3358 old parser in requiring something after label declarations.
3359 Although they are erroneous if the labels declared aren't defined,
3360 is it useful for the syntax to be this way?
3372 c_parser_compound_statement (c_parser *parser)
3375 if (!c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
3376 return error_mark_node;
3377 stmt = c_begin_compound_stmt (true);
3378 c_parser_compound_statement_nostart (parser);
3379 return c_end_compound_stmt (stmt, true);
3382 /* Parse a compound statement except for the opening brace. This is
3383 used for parsing both compound statements and statement expressions
3384 (which follow different paths to handling the opening). */
3387 c_parser_compound_statement_nostart (c_parser *parser)
3389 bool last_stmt = false;
3390 bool last_label = false;
3391 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3393 c_parser_consume_token (parser);
3396 if (c_parser_next_token_is_keyword (parser, RID_LABEL))
3398 /* Read zero or more forward-declarations for labels that nested
3399 functions can jump to. */
3400 while (c_parser_next_token_is_keyword (parser, RID_LABEL))
3402 c_parser_consume_token (parser);
3403 /* Any identifiers, including those declared as type names,
3408 if (c_parser_next_token_is_not (parser, CPP_NAME))
3410 c_parser_error (parser, "expected identifier");
3414 = declare_label (c_parser_peek_token (parser)->value);
3415 C_DECLARED_LABEL_FLAG (label) = 1;
3416 add_stmt (build_stmt (DECL_EXPR, label));
3417 c_parser_consume_token (parser);
3418 if (c_parser_next_token_is (parser, CPP_COMMA))
3419 c_parser_consume_token (parser);
3423 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
3425 /* ??? Locating this diagnostic on the token after the
3426 declarations end follows the old parser, but it might be
3427 better to locate it where the declarations start instead. */
3429 pedwarn ("ISO C forbids label declarations");
3431 /* We must now have at least one statement, label or declaration. */
3432 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3434 c_parser_error (parser, "expected declaration or statement");
3435 c_parser_consume_token (parser);
3438 while (c_parser_next_token_is_not (parser, CPP_CLOSE_BRACE))
3440 location_t loc = c_parser_peek_token (parser)->location;
3441 if (c_parser_next_token_is_keyword (parser, RID_CASE)
3442 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
3443 || (c_parser_next_token_is (parser, CPP_NAME)
3444 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
3448 c_parser_label (parser);
3450 else if (!last_label
3451 && c_parser_next_token_starts_declspecs (parser))
3454 c_parser_declaration_or_fndef (parser, true, true, true, true);
3456 && ((pedantic && !flag_isoc99)
3457 || warn_declaration_after_statement))
3458 pedwarn_c90 ("%HISO C90 forbids mixed declarations and code",
3462 else if (!last_label
3463 && c_parser_next_token_is_keyword (parser, RID_EXTENSION))
3465 /* __extension__ can start a declaration, but is also an
3466 unary operator that can start an expression. Consume all
3467 but the last of a possible series of __extension__ to
3469 while (c_parser_peek_2nd_token (parser)->type == CPP_KEYWORD