1 /* Parser for C and Objective-C.
2 Copyright (C) 1987, 1988, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
5 Parser actions based on the old Bison parser; structure somewhat
6 influenced by and fragments based on the C++ parser.
8 This file is part of GCC.
10 GCC is free software; you can redistribute it and/or modify it under
11 the terms of the GNU General Public License as published by the Free
12 Software Foundation; either version 2, or (at your option) any later
15 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
16 WARRANTY; without even the implied warranty of MERCHANTABILITY or
17 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
20 You should have received a copy of the GNU General Public License
21 along with GCC; see the file COPYING. If not, write to the Free
22 Software Foundation, 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 tree type = start_enum (ident);
1703 /* We chain the enumerators in reverse order, then put them in
1704 forward order at the end. */
1705 tree values = NULL_TREE;
1706 c_parser_consume_token (parser);
1713 if (c_parser_next_token_is_not (parser, CPP_NAME))
1715 c_parser_error (parser, "expected identifier");
1716 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
1717 values = error_mark_node;
1720 enum_id = c_parser_peek_token (parser)->value;
1721 c_parser_consume_token (parser);
1722 if (c_parser_next_token_is (parser, CPP_EQ))
1724 c_parser_consume_token (parser);
1725 enum_value = c_parser_expr_no_commas (parser, NULL).value;
1728 enum_value = NULL_TREE;
1729 enum_decl = build_enumerator (enum_id, enum_value);
1730 TREE_CHAIN (enum_decl) = values;
1733 if (c_parser_next_token_is (parser, CPP_COMMA))
1736 c_parser_consume_token (parser);
1738 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
1740 if (seen_comma && pedantic && !flag_isoc99)
1741 pedwarn ("comma at end of enumerator list");
1742 c_parser_consume_token (parser);
1747 c_parser_error (parser, "expected %<,%> or %<}%>");
1748 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
1749 values = error_mark_node;
1753 postfix_attrs = c_parser_attributes (parser);
1754 ret.spec = finish_enum (type, nreverse (values),
1755 chainon (attrs, postfix_attrs));
1756 ret.kind = ctsk_tagdef;
1761 c_parser_error (parser, "expected %<{%>");
1762 ret.spec = error_mark_node;
1763 ret.kind = ctsk_tagref;
1766 ret = parser_xref_tag (ENUMERAL_TYPE, ident);
1767 /* In ISO C, enumerated types can be referred to only if already
1769 if (pedantic && !COMPLETE_TYPE_P (ret.spec))
1770 pedwarn ("ISO C forbids forward references to %<enum%> types");
1774 /* Parse a struct or union specifier (C90 6.5.2.1, C99 6.7.2.1).
1776 struct-or-union-specifier:
1777 struct-or-union attributes[opt] identifier[opt]
1778 { struct-contents } attributes[opt]
1779 struct-or-union attributes[opt] identifier
1782 struct-declaration-list
1784 struct-declaration-list:
1785 struct-declaration ;
1786 struct-declaration-list struct-declaration ;
1793 struct-declaration-list struct-declaration
1795 struct-declaration-list:
1796 struct-declaration-list ;
1799 (Note that in the syntax here, unlike that in ISO C, the semicolons
1800 are included here rather than in struct-declaration, in order to
1801 describe the syntax with extra semicolons and missing semicolon at
1806 struct-declaration-list:
1807 @defs ( class-name )
1809 (Note this does not include a trailing semicolon, but can be
1810 followed by further declarations, and gets a pedwarn-if-pedantic
1811 when followed by a semicolon.) */
1813 static struct c_typespec
1814 c_parser_struct_or_union_specifier (c_parser *parser)
1816 struct c_typespec ret;
1818 tree ident = NULL_TREE;
1819 enum tree_code code;
1820 switch (c_parser_peek_token (parser)->keyword)
1831 c_parser_consume_token (parser);
1832 attrs = c_parser_attributes (parser);
1833 if (c_parser_next_token_is (parser, CPP_NAME))
1835 ident = c_parser_peek_token (parser)->value;
1836 c_parser_consume_token (parser);
1838 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
1840 /* Parse a struct or union definition. Start the scope of the
1841 tag before parsing components. */
1842 tree type = start_struct (code, ident);
1844 /* We chain the components in reverse order, then put them in
1845 forward order at the end. Each struct-declaration may
1846 declare multiple components (comma-separated), so we must use
1847 chainon to join them, although when parsing each
1848 struct-declaration we can use TREE_CHAIN directly.
1850 The theory behind all this is that there will be more
1851 semicolon separated fields than comma separated fields, and
1852 so we'll be minimizing the number of node traversals required
1854 tree contents = NULL_TREE;
1855 c_parser_consume_token (parser);
1856 /* Handle the Objective-C @defs construct,
1857 e.g. foo(sizeof(struct{ @defs(ClassName) }));. */
1858 if (c_parser_next_token_is_keyword (parser, RID_AT_DEFS))
1861 gcc_assert (c_dialect_objc ());
1862 c_parser_consume_token (parser);
1863 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
1865 if (c_parser_next_token_is (parser, CPP_NAME)
1866 && c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME)
1868 name = c_parser_peek_token (parser)->value;
1869 c_parser_consume_token (parser);
1873 c_parser_error (parser, "expected class name");
1874 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
1877 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
1879 contents = nreverse (objc_get_class_ivars (name));
1882 /* Parse the struct-declarations and semicolons. Problems with
1883 semicolons are diagnosed here; empty structures are diagnosed
1888 /* Parse any stray semicolon. */
1889 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1892 pedwarn ("extra semicolon in struct or union specified");
1893 c_parser_consume_token (parser);
1896 /* Stop if at the end of the struct or union contents. */
1897 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
1899 c_parser_consume_token (parser);
1902 /* Accept #pragmas at struct scope. */
1903 if (c_parser_next_token_is (parser, CPP_PRAGMA))
1905 c_parser_pragma (parser, pragma_external);
1908 /* Parse some comma-separated declarations, but not the
1909 trailing semicolon if any. */
1910 decls = c_parser_struct_declaration (parser);
1911 contents = chainon (decls, contents);
1912 /* If no semicolon follows, either we have a parse error or
1913 are at the end of the struct or union and should
1915 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1916 c_parser_consume_token (parser);
1919 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
1920 pedwarn ("no semicolon at end of struct or union");
1923 c_parser_error (parser, "expected %<;%>");
1924 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
1929 postfix_attrs = c_parser_attributes (parser);
1930 ret.spec = finish_struct (type, nreverse (contents),
1931 chainon (attrs, postfix_attrs));
1932 ret.kind = ctsk_tagdef;
1937 c_parser_error (parser, "expected %<{%>");
1938 ret.spec = error_mark_node;
1939 ret.kind = ctsk_tagref;
1942 ret = parser_xref_tag (code, ident);
1946 /* Parse a struct-declaration (C90 6.5.2.1, C99 6.7.2.1), *without*
1947 the trailing semicolon.
1950 specifier-qualifier-list struct-declarator-list
1952 specifier-qualifier-list:
1953 type-specifier specifier-qualifier-list[opt]
1954 type-qualifier specifier-qualifier-list[opt]
1955 attributes specifier-qualifier-list[opt]
1957 struct-declarator-list:
1959 struct-declarator-list , attributes[opt] struct-declarator
1962 declarator attributes[opt]
1963 declarator[opt] : constant-expression attributes[opt]
1968 __extension__ struct-declaration
1969 specifier-qualifier-list
1971 Unlike the ISO C syntax, semicolons are handled elsewhere. The use
1972 of attributes where shown is a GNU extension. In GNU C, we accept
1973 any expression without commas in the syntax (assignment
1974 expressions, not just conditional expressions); assignment
1975 expressions will be diagnosed as non-constant. */
1978 c_parser_struct_declaration (c_parser *parser)
1980 struct c_declspecs *specs;
1982 tree all_prefix_attrs;
1984 if (c_parser_next_token_is_keyword (parser, RID_EXTENSION))
1988 ext = disable_extension_diagnostics ();
1989 c_parser_consume_token (parser);
1990 decl = c_parser_struct_declaration (parser);
1991 restore_extension_diagnostics (ext);
1994 specs = build_null_declspecs ();
1995 c_parser_declspecs (parser, specs, false, true, true);
1998 if (!specs->declspecs_seen_p)
2000 c_parser_error (parser, "expected specifier-qualifier-list");
2003 finish_declspecs (specs);
2004 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
2007 if (!specs->type_seen_p)
2010 pedwarn ("ISO C forbids member declarations with no members");
2011 shadow_tag_warned (specs, pedantic);
2016 /* Support for unnamed structs or unions as members of
2017 structs or unions (which is [a] useful and [b] supports
2019 ret = grokfield (build_id_declarator (NULL_TREE), specs, NULL_TREE);
2023 pending_xref_error ();
2024 prefix_attrs = specs->attrs;
2025 all_prefix_attrs = prefix_attrs;
2026 specs->attrs = NULL_TREE;
2030 /* Declaring one or more declarators or un-named bit-fields. */
2031 struct c_declarator *declarator;
2033 if (c_parser_next_token_is (parser, CPP_COLON))
2034 declarator = build_id_declarator (NULL_TREE);
2036 declarator = c_parser_declarator (parser, specs->type_seen_p,
2037 C_DTR_NORMAL, &dummy);
2038 if (declarator == NULL)
2040 c_parser_skip_to_end_of_block_or_statement (parser);
2043 if (c_parser_next_token_is (parser, CPP_COLON)
2044 || c_parser_next_token_is (parser, CPP_COMMA)
2045 || c_parser_next_token_is (parser, CPP_SEMICOLON)
2046 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE)
2047 || c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2049 tree postfix_attrs = NULL_TREE;
2050 tree width = NULL_TREE;
2052 if (c_parser_next_token_is (parser, CPP_COLON))
2054 c_parser_consume_token (parser);
2055 width = c_parser_expr_no_commas (parser, NULL).value;
2057 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2058 postfix_attrs = c_parser_attributes (parser);
2059 d = grokfield (declarator, specs, width);
2060 decl_attributes (&d, chainon (postfix_attrs,
2061 all_prefix_attrs), 0);
2062 TREE_CHAIN (d) = decls;
2064 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2065 all_prefix_attrs = chainon (c_parser_attributes (parser),
2068 all_prefix_attrs = prefix_attrs;
2069 if (c_parser_next_token_is (parser, CPP_COMMA))
2070 c_parser_consume_token (parser);
2071 else if (c_parser_next_token_is (parser, CPP_SEMICOLON)
2072 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2074 /* Semicolon consumed in caller. */
2079 c_parser_error (parser, "expected %<,%>, %<;%> or %<}%>");
2085 c_parser_error (parser,
2086 "expected %<:%>, %<,%>, %<;%>, %<}%> or "
2087 "%<__attribute__%>");
2094 /* Parse a typeof specifier (a GNU extension).
2097 typeof ( expression )
2098 typeof ( type-name )
2101 static struct c_typespec
2102 c_parser_typeof_specifier (c_parser *parser)
2104 struct c_typespec ret;
2105 ret.kind = ctsk_typeof;
2106 ret.spec = error_mark_node;
2107 gcc_assert (c_parser_next_token_is_keyword (parser, RID_TYPEOF));
2108 c_parser_consume_token (parser);
2111 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2117 if (c_parser_next_token_starts_typename (parser))
2119 struct c_type_name *type = c_parser_type_name (parser);
2124 ret.spec = groktypename (type);
2125 pop_maybe_used (variably_modified_type_p (ret.spec, NULL_TREE));
2131 struct c_expr expr = c_parser_expression (parser);
2134 if (TREE_CODE (expr.value) == COMPONENT_REF
2135 && DECL_C_BIT_FIELD (TREE_OPERAND (expr.value, 1)))
2136 error ("%<typeof%> applied to a bit-field");
2137 ret.spec = TREE_TYPE (expr.value);
2138 was_vm = variably_modified_type_p (ret.spec, NULL_TREE);
2139 /* This should be returned with the type so that when the type
2140 is evaluated, this can be evaluated. For now, we avoid
2141 evaluation when the context might. */
2142 if (!skip_evaluation && was_vm)
2144 tree e = expr.value;
2146 /* If the expression is not of a type to which we cannot assign a line
2147 number, wrap the thing in a no-op NOP_EXPR. */
2148 if (DECL_P (e) || CONSTANT_CLASS_P (e))
2149 e = build1 (NOP_EXPR, void_type_node, e);
2151 if (CAN_HAVE_LOCATION_P (e))
2152 SET_EXPR_LOCATION (e, input_location);
2156 pop_maybe_used (was_vm);
2158 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
2162 /* Parse a declarator, possibly an abstract declarator (C90 6.5.4,
2163 6.5.5, C99 6.7.5, 6.7.6). If TYPE_SEEN_P then a typedef name may
2164 be redeclared; otherwise it may not. KIND indicates which kind of
2165 declarator is wanted. Returns a valid declarator except in the
2166 case of a syntax error in which case NULL is returned. *SEEN_ID is
2167 set to true if an identifier being declared is seen; this is used
2168 to diagnose bad forms of abstract array declarators and to
2169 determine whether an identifier list is syntactically permitted.
2172 pointer[opt] direct-declarator
2176 ( attributes[opt] declarator )
2177 direct-declarator array-declarator
2178 direct-declarator ( parameter-type-list )
2179 direct-declarator ( identifier-list[opt] )
2182 * type-qualifier-list[opt]
2183 * type-qualifier-list[opt] pointer
2185 type-qualifier-list:
2188 type-qualifier-list type-qualifier
2189 type-qualifier-list attributes
2191 parameter-type-list:
2193 parameter-list , ...
2196 parameter-declaration
2197 parameter-list , parameter-declaration
2199 parameter-declaration:
2200 declaration-specifiers declarator attributes[opt]
2201 declaration-specifiers abstract-declarator[opt] attributes[opt]
2205 identifier-list , identifier
2207 abstract-declarator:
2209 pointer[opt] direct-abstract-declarator
2211 direct-abstract-declarator:
2212 ( attributes[opt] abstract-declarator )
2213 direct-abstract-declarator[opt] array-declarator
2214 direct-abstract-declarator[opt] ( parameter-type-list[opt] )
2219 direct-declarator ( parameter-forward-declarations
2220 parameter-type-list[opt] )
2222 direct-abstract-declarator:
2223 direct-abstract-declarator[opt] ( parameter-forward-declarations
2224 parameter-type-list[opt] )
2226 parameter-forward-declarations:
2228 parameter-forward-declarations parameter-list ;
2230 The uses of attributes shown above are GNU extensions.
2232 Some forms of array declarator are not included in C99 in the
2233 syntax for abstract declarators; these are disallowed elsewhere.
2234 This may be a defect (DR#289).
2236 This function also accepts an omitted abstract declarator as being
2237 an abstract declarator, although not part of the formal syntax. */
2239 static struct c_declarator *
2240 c_parser_declarator (c_parser *parser, bool type_seen_p, c_dtr_syn kind,
2243 /* Parse any initial pointer part. */
2244 if (c_parser_next_token_is (parser, CPP_MULT))
2246 struct c_declspecs *quals_attrs = build_null_declspecs ();
2247 struct c_declarator *inner;
2248 c_parser_consume_token (parser);
2249 c_parser_declspecs (parser, quals_attrs, false, false, true);
2250 inner = c_parser_declarator (parser, type_seen_p, kind, seen_id);
2254 return make_pointer_declarator (quals_attrs, inner);
2256 /* Now we have a direct declarator, direct abstract declarator or
2257 nothing (which counts as a direct abstract declarator here). */
2258 return c_parser_direct_declarator (parser, type_seen_p, kind, seen_id);
2261 /* Parse a direct declarator or direct abstract declarator; arguments
2262 as c_parser_declarator. */
2264 static struct c_declarator *
2265 c_parser_direct_declarator (c_parser *parser, bool type_seen_p, c_dtr_syn kind,
2268 /* The direct declarator must start with an identifier (possibly
2269 omitted) or a parenthesized declarator (possibly abstract). In
2270 an ordinary declarator, initial parentheses must start a
2271 parenthesized declarator. In an abstract declarator or parameter
2272 declarator, they could start a parenthesized declarator or a
2273 parameter list. To tell which, the open parenthesis and any
2274 following attributes must be read. If a declaration specifier
2275 follows, then it is a parameter list; if the specifier is a
2276 typedef name, there might be an ambiguity about redeclaring it,
2277 which is resolved in the direction of treating it as a typedef
2278 name. If a close parenthesis follows, it is also an empty
2279 parameter list, as the syntax does not permit empty abstract
2280 declarators. Otherwise, it is a parenthesized declarator (in
2281 which case the analysis may be repeated inside it, recursively).
2283 ??? There is an ambiguity in a parameter declaration "int
2284 (__attribute__((foo)) x)", where x is not a typedef name: it
2285 could be an abstract declarator for a function, or declare x with
2286 parentheses. The proper resolution of this ambiguity needs
2287 documenting. At present we follow an accident of the old
2288 parser's implementation, whereby the first parameter must have
2289 some declaration specifiers other than just attributes. Thus as
2290 a parameter declaration it is treated as a parenthesized
2291 parameter named x, and as an abstract declarator it is
2294 ??? Also following the old parser, attributes inside an empty
2295 parameter list are ignored, making it a list not yielding a
2296 prototype, rather than giving an error or making it have one
2297 parameter with implicit type int.
2299 ??? Also following the old parser, typedef names may be
2300 redeclared in declarators, but not Objective-C class names. */
2302 if (kind != C_DTR_ABSTRACT
2303 && c_parser_next_token_is (parser, CPP_NAME)
2305 && c_parser_peek_token (parser)->id_kind == C_ID_TYPENAME)
2306 || c_parser_peek_token (parser)->id_kind == C_ID_ID))
2308 struct c_declarator *inner
2309 = build_id_declarator (c_parser_peek_token (parser)->value);
2311 inner->id_loc = c_parser_peek_token (parser)->location;
2312 c_parser_consume_token (parser);
2313 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
2316 if (kind != C_DTR_NORMAL
2317 && c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
2319 struct c_declarator *inner = build_id_declarator (NULL_TREE);
2320 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
2323 /* Either we are at the end of an abstract declarator, or we have
2326 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
2329 struct c_declarator *inner;
2330 c_parser_consume_token (parser);
2331 attrs = c_parser_attributes (parser);
2332 if (kind != C_DTR_NORMAL
2333 && (c_parser_next_token_starts_declspecs (parser)
2334 || c_parser_next_token_is (parser, CPP_CLOSE_PAREN)))
2336 struct c_arg_info *args
2337 = c_parser_parms_declarator (parser, kind == C_DTR_NORMAL,
2344 = build_function_declarator (args,
2345 build_id_declarator (NULL_TREE));
2346 return c_parser_direct_declarator_inner (parser, *seen_id,
2350 /* A parenthesized declarator. */
2351 inner = c_parser_declarator (parser, type_seen_p, kind, seen_id);
2352 if (inner != NULL && attrs != NULL)
2353 inner = build_attrs_declarator (attrs, inner);
2354 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2356 c_parser_consume_token (parser);
2360 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
2364 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2371 if (kind == C_DTR_NORMAL)
2373 c_parser_error (parser, "expected identifier or %<(%>");
2377 return build_id_declarator (NULL_TREE);
2381 /* Parse part of a direct declarator or direct abstract declarator,
2382 given that some (in INNER) has already been parsed; ID_PRESENT is
2383 true if an identifier is present, false for an abstract
2386 static struct c_declarator *
2387 c_parser_direct_declarator_inner (c_parser *parser, bool id_present,
2388 struct c_declarator *inner)
2390 /* Parse a sequence of array declarators and parameter lists. */
2391 if (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
2393 struct c_declarator *declarator;
2394 struct c_declspecs *quals_attrs = build_null_declspecs ();
2398 c_parser_consume_token (parser);
2399 c_parser_declspecs (parser, quals_attrs, false, false, true);
2400 static_seen = c_parser_next_token_is_keyword (parser, RID_STATIC);
2402 c_parser_consume_token (parser);
2403 if (static_seen && !quals_attrs->declspecs_seen_p)
2404 c_parser_declspecs (parser, quals_attrs, false, false, true);
2405 if (!quals_attrs->declspecs_seen_p)
2407 /* If "static" is present, there must be an array dimension.
2408 Otherwise, there may be a dimension, "*", or no
2413 dimen = c_parser_expr_no_commas (parser, NULL).value;
2417 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
2422 else if (c_parser_next_token_is (parser, CPP_MULT))
2424 if (c_parser_peek_2nd_token (parser)->type == CPP_CLOSE_SQUARE)
2428 c_parser_consume_token (parser);
2433 dimen = c_parser_expr_no_commas (parser, NULL).value;
2439 dimen = c_parser_expr_no_commas (parser, NULL).value;
2442 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
2443 c_parser_consume_token (parser);
2446 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
2450 declarator = build_array_declarator (dimen, quals_attrs, static_seen,
2452 if (declarator == NULL)
2454 inner = set_array_declarator_inner (declarator, inner, !id_present);
2455 return c_parser_direct_declarator_inner (parser, id_present, inner);
2457 else if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
2460 struct c_arg_info *args;
2461 c_parser_consume_token (parser);
2462 attrs = c_parser_attributes (parser);
2463 args = c_parser_parms_declarator (parser, id_present, attrs);
2468 inner = build_function_declarator (args, inner);
2469 return c_parser_direct_declarator_inner (parser, id_present, inner);
2475 /* Parse a parameter list or identifier list, including the closing
2476 parenthesis but not the opening one. ATTRS are the attributes at
2477 the start of the list. ID_LIST_OK is true if an identifier list is
2478 acceptable; such a list must not have attributes at the start. */
2480 static struct c_arg_info *
2481 c_parser_parms_declarator (c_parser *parser, bool id_list_ok, tree attrs)
2484 declare_parm_level ();
2485 /* If the list starts with an identifier, it is an identifier list.
2486 Otherwise, it is either a prototype list or an empty list. */
2489 && c_parser_next_token_is (parser, CPP_NAME)
2490 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
2492 tree list = NULL_TREE, *nextp = &list;
2493 while (c_parser_next_token_is (parser, CPP_NAME)
2494 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
2496 *nextp = build_tree_list (NULL_TREE,
2497 c_parser_peek_token (parser)->value);
2498 nextp = & TREE_CHAIN (*nextp);
2499 c_parser_consume_token (parser);
2500 if (c_parser_next_token_is_not (parser, CPP_COMMA))
2502 c_parser_consume_token (parser);
2503 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2505 c_parser_error (parser, "expected identifier");
2509 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2511 struct c_arg_info *ret = XOBNEW (&parser_obstack, struct c_arg_info);
2516 ret->pending_sizes = 0;
2517 ret->had_vla_unspec = 0;
2518 c_parser_consume_token (parser);
2524 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2532 struct c_arg_info *ret = c_parser_parms_list_declarator (parser, attrs);
2538 /* Parse a parameter list (possibly empty), including the closing
2539 parenthesis but not the opening one. ATTRS are the attributes at
2540 the start of the list. */
2542 static struct c_arg_info *
2543 c_parser_parms_list_declarator (c_parser *parser, tree attrs)
2545 bool good_parm = false;
2546 /* ??? Following the old parser, forward parameter declarations may
2547 use abstract declarators, and if no real parameter declarations
2548 follow the forward declarations then this is not diagnosed. Also
2549 note as above that attributes are ignored as the only contents of
2550 the parentheses, or as the only contents after forward
2552 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2554 struct c_arg_info *ret = XOBNEW (&parser_obstack, struct c_arg_info);
2559 ret->pending_sizes = 0;
2560 ret->had_vla_unspec = 0;
2561 c_parser_consume_token (parser);
2564 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
2566 struct c_arg_info *ret = XOBNEW (&parser_obstack, struct c_arg_info);
2570 ret->pending_sizes = 0;
2571 ret->had_vla_unspec = 0;
2572 /* Suppress -Wold-style-definition for this case. */
2573 ret->types = error_mark_node;
2574 error ("ISO C requires a named argument before %<...%>");
2575 c_parser_consume_token (parser);
2576 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2578 c_parser_consume_token (parser);
2583 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2588 /* Nonempty list of parameters, either terminated with semicolon
2589 (forward declarations; recurse) or with close parenthesis (normal
2590 function) or with ", ... )" (variadic function). */
2593 /* Parse a parameter. */
2594 struct c_parm *parm = c_parser_parameter_declaration (parser, attrs);
2599 push_parm_decl (parm);
2601 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
2604 c_parser_consume_token (parser);
2605 mark_forward_parm_decls ();
2606 new_attrs = c_parser_attributes (parser);
2607 return c_parser_parms_list_declarator (parser, new_attrs);
2609 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2611 c_parser_consume_token (parser);
2613 return get_parm_info (false);
2616 struct c_arg_info *ret
2617 = XOBNEW (&parser_obstack, struct c_arg_info);
2622 ret->pending_sizes = 0;
2623 ret->had_vla_unspec = 0;
2627 if (!c_parser_require (parser, CPP_COMMA,
2628 "expected %<;%>, %<,%> or %<)%>"))
2630 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
2633 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
2635 c_parser_consume_token (parser);
2636 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2638 c_parser_consume_token (parser);
2640 return get_parm_info (true);
2643 struct c_arg_info *ret
2644 = XOBNEW (&parser_obstack, struct c_arg_info);
2649 ret->pending_sizes = 0;
2650 ret->had_vla_unspec = 0;
2656 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2664 /* Parse a parameter declaration. ATTRS are the attributes at the
2665 start of the declaration if it is the first parameter. */
2667 static struct c_parm *
2668 c_parser_parameter_declaration (c_parser *parser, tree attrs)
2670 struct c_declspecs *specs;
2671 struct c_declarator *declarator;
2673 tree postfix_attrs = NULL_TREE;
2675 if (!c_parser_next_token_starts_declspecs (parser))
2677 /* ??? In some Objective-C cases '...' isn't applicable so there
2678 should be a different message. */
2679 c_parser_error (parser,
2680 "expected declaration specifiers or %<...%>");
2681 c_parser_skip_to_end_of_parameter (parser);
2684 specs = build_null_declspecs ();
2687 declspecs_add_attrs (specs, attrs);
2690 c_parser_declspecs (parser, specs, true, true, true);
2691 finish_declspecs (specs);
2692 pending_xref_error ();
2693 prefix_attrs = specs->attrs;
2694 specs->attrs = NULL_TREE;
2695 declarator = c_parser_declarator (parser, specs->type_seen_p,
2696 C_DTR_PARM, &dummy);
2697 if (declarator == NULL)
2699 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
2702 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2703 postfix_attrs = c_parser_attributes (parser);
2704 return build_c_parm (specs, chainon (postfix_attrs, prefix_attrs),
2708 /* Parse a string literal in an asm expression. It should not be
2709 translated, and wide string literals are an error although
2710 permitted by the syntax. This is a GNU extension.
2715 ??? At present, following the old parser, the caller needs to have
2716 set c_lex_string_translate to 0. It would be better to follow the
2717 C++ parser rather than using the c_lex_string_translate kludge. */
2720 c_parser_asm_string_literal (c_parser *parser)
2723 if (c_parser_next_token_is (parser, CPP_STRING))
2725 str = c_parser_peek_token (parser)->value;
2726 c_parser_consume_token (parser);
2728 else if (c_parser_next_token_is (parser, CPP_WSTRING))
2730 error ("wide string literal in %<asm%>");
2731 str = build_string (1, "");
2732 c_parser_consume_token (parser);
2736 c_parser_error (parser, "expected string literal");
2742 /* Parse a simple asm expression. This is used in restricted
2743 contexts, where a full expression with inputs and outputs does not
2744 make sense. This is a GNU extension.
2747 asm ( asm-string-literal )
2751 c_parser_simple_asm_expr (c_parser *parser)
2754 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ASM));
2755 /* ??? Follow the C++ parser rather than using the
2756 c_lex_string_translate kludge. */
2757 c_lex_string_translate = 0;
2758 c_parser_consume_token (parser);
2759 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2761 c_lex_string_translate = 1;
2764 str = c_parser_asm_string_literal (parser);
2765 c_lex_string_translate = 1;
2766 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
2768 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
2774 /* Parse (possibly empty) attributes. This is a GNU extension.
2778 attributes attribute
2781 __attribute__ ( ( attribute-list ) )
2785 attribute_list , attrib
2790 any-word ( identifier )
2791 any-word ( identifier , nonempty-expr-list )
2792 any-word ( expr-list )
2794 where the "identifier" must not be declared as a type, and
2795 "any-word" may be any identifier (including one declared as a
2796 type), a reserved word storage class specifier, type specifier or
2797 type qualifier. ??? This still leaves out most reserved keywords
2798 (following the old parser), shouldn't we include them, and why not
2799 allow identifiers declared as types to start the arguments? */
2802 c_parser_attributes (c_parser *parser)
2804 tree attrs = NULL_TREE;
2805 while (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2807 /* ??? Follow the C++ parser rather than using the
2808 c_lex_string_translate kludge. */
2809 c_lex_string_translate = 0;
2810 c_parser_consume_token (parser);
2811 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2813 c_lex_string_translate = 1;
2816 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2818 c_lex_string_translate = 1;
2819 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
2822 /* Parse the attribute list. */
2823 while (c_parser_next_token_is (parser, CPP_COMMA)
2824 || c_parser_next_token_is (parser, CPP_NAME)
2825 || c_parser_next_token_is (parser, CPP_KEYWORD))
2827 tree attr, attr_name, attr_args;
2828 if (c_parser_next_token_is (parser, CPP_COMMA))
2830 c_parser_consume_token (parser);
2833 if (c_parser_next_token_is (parser, CPP_KEYWORD))
2835 /* ??? See comment above about what keywords are
2838 switch (c_parser_peek_token (parser)->keyword)
2873 attr_name = c_parser_peek_token (parser)->value;
2874 c_parser_consume_token (parser);
2875 if (c_parser_next_token_is_not (parser, CPP_OPEN_PAREN))
2877 attr = build_tree_list (attr_name, NULL_TREE);
2878 attrs = chainon (attrs, attr);
2881 c_parser_consume_token (parser);
2882 /* Parse the attribute contents. If they start with an
2883 identifier which is followed by a comma or close
2884 parenthesis, then the arguments start with that
2885 identifier; otherwise they are an expression list. */
2886 if (c_parser_next_token_is (parser, CPP_NAME)
2887 && c_parser_peek_token (parser)->id_kind == C_ID_ID
2888 && ((c_parser_peek_2nd_token (parser)->type == CPP_COMMA)
2889 || (c_parser_peek_2nd_token (parser)->type
2890 == CPP_CLOSE_PAREN)))
2892 tree arg1 = c_parser_peek_token (parser)->value;
2893 c_parser_consume_token (parser);
2894 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2895 attr_args = build_tree_list (NULL_TREE, arg1);
2898 c_parser_consume_token (parser);
2899 attr_args = tree_cons (NULL_TREE, arg1,
2900 c_parser_expr_list (parser, false));
2905 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2906 attr_args = NULL_TREE;
2908 attr_args = c_parser_expr_list (parser, false);
2910 attr = build_tree_list (attr_name, attr_args);
2911 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2912 c_parser_consume_token (parser);
2915 c_lex_string_translate = 1;
2916 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2920 attrs = chainon (attrs, attr);
2922 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2923 c_parser_consume_token (parser);
2926 c_lex_string_translate = 1;
2927 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2931 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2932 c_parser_consume_token (parser);
2935 c_lex_string_translate = 1;
2936 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2940 c_lex_string_translate = 1;
2945 /* Parse a type name (C90 6.5.5, C99 6.7.6).
2948 specifier-qualifier-list abstract-declarator[opt]
2951 static struct c_type_name *
2952 c_parser_type_name (c_parser *parser)
2954 struct c_declspecs *specs = build_null_declspecs ();
2955 struct c_declarator *declarator;
2956 struct c_type_name *ret;
2958 c_parser_declspecs (parser, specs, false, true, true);
2959 if (!specs->declspecs_seen_p)
2961 c_parser_error (parser, "expected specifier-qualifier-list");
2964 pending_xref_error ();
2965 finish_declspecs (specs);
2966 declarator = c_parser_declarator (parser, specs->type_seen_p,
2967 C_DTR_ABSTRACT, &dummy);
2968 if (declarator == NULL)
2970 ret = XOBNEW (&parser_obstack, struct c_type_name);
2972 ret->declarator = declarator;
2976 /* Parse an initializer (C90 6.5.7, C99 6.7.8).
2979 assignment-expression
2980 { initializer-list }
2981 { initializer-list , }
2984 designation[opt] initializer
2985 initializer-list , designation[opt] initializer
2992 designator-list designator
2999 [ constant-expression ]
3011 [ constant-expression ... constant-expression ]
3013 Any expression without commas is accepted in the syntax for the
3014 constant-expressions, with non-constant expressions rejected later.
3016 This function is only used for top-level initializers; for nested
3017 ones, see c_parser_initval. */
3019 static struct c_expr
3020 c_parser_initializer (c_parser *parser)
3022 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
3023 return c_parser_braced_init (parser, NULL_TREE, false);
3027 ret = c_parser_expr_no_commas (parser, NULL);
3028 if (TREE_CODE (ret.value) != STRING_CST
3029 && TREE_CODE (ret.value) != COMPOUND_LITERAL_EXPR)
3030 ret = default_function_array_conversion (ret);
3035 /* Parse a braced initializer list. TYPE is the type specified for a
3036 compound literal, and NULL_TREE for other initializers and for
3037 nested braced lists. NESTED_P is true for nested braced lists,
3038 false for the list of a compound literal or the list that is the
3039 top-level initializer in a declaration. */
3041 static struct c_expr
3042 c_parser_braced_init (c_parser *parser, tree type, bool nested_p)
3044 gcc_assert (c_parser_next_token_is (parser, CPP_OPEN_BRACE));
3045 c_parser_consume_token (parser);
3047 push_init_level (0);
3049 really_start_incremental_init (type);
3050 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3053 pedwarn ("ISO C forbids empty initializer braces");
3057 /* Parse a non-empty initializer list, possibly with a trailing
3061 c_parser_initelt (parser);
3064 if (c_parser_next_token_is (parser, CPP_COMMA))
3065 c_parser_consume_token (parser);
3068 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3072 if (c_parser_next_token_is_not (parser, CPP_CLOSE_BRACE))
3075 ret.value = error_mark_node;
3076 ret.original_code = ERROR_MARK;
3077 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, "expected %<}%>");
3080 c_parser_consume_token (parser);
3081 return pop_init_level (0);
3084 /* Parse a nested initializer, including designators. */
3087 c_parser_initelt (c_parser *parser)
3089 /* Parse any designator or designator list. A single array
3090 designator may have the subsequent "=" omitted in GNU C, but a
3091 longer list or a structure member designator may not. */
3092 if (c_parser_next_token_is (parser, CPP_NAME)
3093 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
3095 /* Old-style structure member designator. */
3096 set_init_label (c_parser_peek_token (parser)->value);
3098 pedwarn ("obsolete use of designated initializer with %<:%>");
3099 c_parser_consume_token (parser);
3100 c_parser_consume_token (parser);
3104 /* des_seen is 0 if there have been no designators, 1 if there
3105 has been a single array designator and 2 otherwise. */
3107 while (c_parser_next_token_is (parser, CPP_OPEN_SQUARE)
3108 || c_parser_next_token_is (parser, CPP_DOT))
3110 int des_prev = des_seen;
3113 if (c_parser_next_token_is (parser, CPP_DOT))
3116 c_parser_consume_token (parser);
3117 if (c_parser_next_token_is (parser, CPP_NAME))
3119 set_init_label (c_parser_peek_token (parser)->value);
3120 c_parser_consume_token (parser);
3125 init.value = error_mark_node;
3126 init.original_code = ERROR_MARK;
3127 c_parser_error (parser, "expected identifier");
3128 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
3129 process_init_element (init);
3136 /* ??? Following the old parser, [ objc-receiver
3137 objc-message-args ] is accepted as an initializer,
3138 being distinguished from a designator by what follows
3139 the first assignment expression inside the square
3140 brackets, but after a first array designator a
3141 subsequent square bracket is for Objective-C taken to
3142 start an expression, using the obsolete form of
3143 designated initializer without '=', rather than
3144 possibly being a second level of designation: in LALR
3145 terms, the '[' is shifted rather than reducing
3146 designator to designator-list. */
3147 if (des_prev == 1 && c_dialect_objc ())
3149 des_seen = des_prev;
3152 if (des_prev == 0 && c_dialect_objc ())
3154 /* This might be an array designator or an
3155 Objective-C message expression. If the former,
3156 continue parsing here; if the latter, parse the
3157 remainder of the initializer given the starting
3158 primary-expression. ??? It might make sense to
3159 distinguish when des_prev == 1 as well; see
3160 previous comment. */
3162 struct c_expr mexpr;
3163 c_parser_consume_token (parser);
3164 if (c_parser_peek_token (parser)->type == CPP_NAME
3165 && ((c_parser_peek_token (parser)->id_kind
3167 || (c_parser_peek_token (parser)->id_kind
3168 == C_ID_CLASSNAME)))
3170 /* Type name receiver. */
3171 tree id = c_parser_peek_token (parser)->value;
3172 c_parser_consume_token (parser);
3173 rec = objc_get_class_reference (id);
3174 goto parse_message_args;
3176 first = c_parser_expr_no_commas (parser, NULL).value;
3177 if (c_parser_next_token_is (parser, CPP_ELLIPSIS)
3178 || c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
3179 goto array_desig_after_first;
3180 /* Expression receiver. So far only one part
3181 without commas has been parsed; there might be
3182 more of the expression. */
3184 while (c_parser_next_token_is (parser, CPP_COMMA))
3187 c_parser_consume_token (parser);
3188 next = c_parser_expr_no_commas (parser, NULL);
3189 next = default_function_array_conversion (next);
3190 rec = build_compound_expr (rec, next.value);
3193 /* Now parse the objc-message-args. */
3194 args = c_parser_objc_message_args (parser);
3195 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
3198 = objc_build_message_expr (build_tree_list (rec, args));
3199 mexpr.original_code = ERROR_MARK;
3200 /* Now parse and process the remainder of the
3201 initializer, starting with this message
3202 expression as a primary-expression. */
3203 c_parser_initval (parser, &mexpr);
3206 c_parser_consume_token (parser);
3207 first = c_parser_expr_no_commas (parser, NULL).value;
3208 array_desig_after_first:
3209 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
3211 c_parser_consume_token (parser);
3212 second = c_parser_expr_no_commas (parser, NULL).value;
3216 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
3218 c_parser_consume_token (parser);
3219 set_init_index (first, second);
3220 if (pedantic && second)
3221 pedwarn ("ISO C forbids specifying range of "
3222 "elements to initialize");
3225 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
3231 if (c_parser_next_token_is (parser, CPP_EQ))
3233 if (pedantic && !flag_isoc99)
3234 pedwarn ("ISO C90 forbids specifying subobject to initialize");
3235 c_parser_consume_token (parser);
3242 pedwarn ("obsolete use of designated initializer "
3248 init.value = error_mark_node;
3249 init.original_code = ERROR_MARK;
3250 c_parser_error (parser, "expected %<=%>");
3251 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
3252 process_init_element (init);
3258 c_parser_initval (parser, NULL);
3261 /* Parse a nested initializer; as c_parser_initializer but parses
3262 initializers within braced lists, after any designators have been
3263 applied. If AFTER is not NULL then it is an Objective-C message
3264 expression which is the primary-expression starting the
3268 c_parser_initval (c_parser *parser, struct c_expr *after)
3271 gcc_assert (!after || c_dialect_objc ());
3272 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE) && !after)
3273 init = c_parser_braced_init (parser, NULL_TREE, true);
3276 init = c_parser_expr_no_commas (parser, after);
3277 if (init.value != NULL_TREE
3278 && TREE_CODE (init.value) != STRING_CST
3279 && TREE_CODE (init.value) != COMPOUND_LITERAL_EXPR)
3280 init = default_function_array_conversion (init);
3282 process_init_element (init);
3285 /* Parse a compound statement (possibly a function body) (C90 6.6.2,
3289 { block-item-list[opt] }
3290 { label-declarations block-item-list }
3294 block-item-list block-item
3306 { label-declarations block-item-list }
3309 __extension__ nested-declaration
3310 nested-function-definition
3314 label-declarations label-declaration
3317 __label__ identifier-list ;
3319 Allowing the mixing of declarations and code is new in C99. The
3320 GNU syntax also permits (not shown above) labels at the end of
3321 compound statements, which yield an error. We don't allow labels
3322 on declarations; this might seem like a natural extension, but
3323 there would be a conflict between attributes on the label and
3324 prefix attributes on the declaration. ??? The syntax follows the
3325 old parser in requiring something after label declarations.
3326 Although they are erroneous if the labels declared aren't defined,
3327 is it useful for the syntax to be this way?
3339 c_parser_compound_statement (c_parser *parser)
3342 if (!c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
3343 return error_mark_node;
3344 stmt = c_begin_compound_stmt (true);
3345 c_parser_compound_statement_nostart (parser);
3346 return c_end_compound_stmt (stmt, true);
3349 /* Parse a compound statement except for the opening brace. This is
3350 used for parsing both compound statements and statement expressions
3351 (which follow different paths to handling the opening). */
3354 c_parser_compound_statement_nostart (c_parser *parser)
3356 bool last_stmt = false;
3357 bool last_label = false;
3358 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3360 c_parser_consume_token (parser);
3363 if (c_parser_next_token_is_keyword (parser, RID_LABEL))
3365 /* Read zero or more forward-declarations for labels that nested
3366 functions can jump to. */
3367 while (c_parser_next_token_is_keyword (parser, RID_LABEL))
3369 c_parser_consume_token (parser);
3370 /* Any identifiers, including those declared as type names,
3375 if (c_parser_next_token_is_not (parser, CPP_NAME))
3377 c_parser_error (parser, "expected identifier");
3381 = declare_label (c_parser_peek_token (parser)->value);
3382 C_DECLARED_LABEL_FLAG (label) = 1;
3383 add_stmt (build_stmt (DECL_EXPR, label));
3384 c_parser_consume_token (parser);
3385 if (c_parser_next_token_is (parser, CPP_COMMA))
3386 c_parser_consume_token (parser);
3390 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
3392 /* ??? Locating this diagnostic on the token after the
3393 declarations end follows the old parser, but it might be
3394 better to locate it where the declarations start instead. */
3396 pedwarn ("ISO C forbids label declarations");
3398 /* We must now have at least one statement, label or declaration. */
3399 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3401 c_parser_error (parser, "expected declaration or statement");
3402 c_parser_consume_token (parser);
3405 while (c_parser_next_token_is_not (parser, CPP_CLOSE_BRACE))
3407 location_t loc = c_parser_peek_token (parser)->location;
3408 if (c_parser_next_token_is_keyword (parser, RID_CASE)
3409 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
3410 || (c_parser_next_token_is (parser, CPP_NAME)
3411 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
3415 c_parser_label (parser);
3417 else if (!last_label
3418 && c_parser_next_token_starts_declspecs (parser))
3421 c_parser_declaration_or_fndef (parser, true, true, true, true);
3423 && ((pedantic && !flag_isoc99)
3424 || warn_declaration_after_statement))
3425 pedwarn_c90 ("%HISO C90 forbids mixed declarations and code",
3429 else if (!last_label
3430 && c_parser_next_token_is_keyword (parser, RID_EXTENSION))
3432 /* __extension__ can start a declaration, but is also an
3433 unary operator that can start an expression. Consume all
3434 but the last of a possible series of __extension__ to
3436 while (c_parser_peek_2nd_token (parser)->type == CPP_KEYWORD
3437 && (c_parser_peek_2nd_token (parser)->keyword
3439 c_parser_consume_token (parser);
3440 if (c_token_starts_declspecs (c_parser_peek_2nd_token (parser)))
3443 ext = disable_extension_diagnostics ();
3444 c_parser_consume_token (parser);
3446 c_parser_declaration_or_fndef (parser, true, true, true, true);
3447 /* Following the old parser, __extension__ does not
3448 disable this diagnostic. */
3449 restore_extension_diagnostics (ext);
3451 && ((pedantic && !flag_isoc99)
3452 || warn_declaration_after_statement))
3453 pedwarn_c90 ("%HISO C90 forbids mixed declarations and code",
3460 else if (c_parser_next_token_is (parser, CPP_PRAGMA))
3462 /* External pragmas, and some omp pragmas, are not associated
3463 with regular c code, and so are not to be considered statements
3464 syntactically. This ensures that the user doesn't put them
3465 places that would turn into syntax errors if the directive
3467 if (c_parser_pragma (parser, pragma_compound))
3468 last_label = false, last_stmt = true;
3470 else if (c_parser_next_token_is (parser, CPP_EOF))
3472 c_parser_error (parser, "expected declaration or statement");
3480 c_parser_statement_after_labels (parser);
3483 parser->error = false;
3486 error ("label at end of compound statement");
3487 c_parser_consume_token (parser);
3490 /* Parse a label (C90 6.6.1, C99 6.8.1).
3493 identifier : attributes[opt]
3494 case constant-expression :
3500 case constant-expression ... constant-expression :
3502 The use of attributes on labels is a GNU extension. The syntax in
3503 GNU C accepts any expressions without commas, non-constant
3504 expressions being rejected later. */
3507 c_parser_label (c_parser *parser)
3509 location_t loc1 = c_parser_peek_token (parser)->location;
3510 tree label = NULL_TREE;
3511 if (c_parser_next_token_is_keyword (parser, RID_CASE))
3514 c_parser_consume_token (parser);
3515 exp1 = c_parser_expr_no_commas (parser, NULL).value;
3516 if (c_parser_next_token_is (parser, CPP_COLON))
3518 c_parser_consume_token (parser);
3519 label = do_case (exp1, NULL_TREE);
3521 else if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
3523 c_parser_consume_token (parser);
3524 exp2 = c_parser_expr_no_commas (parser, NULL).value;
3525 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
3526 label = do_case (exp1, exp2);
3529 c_parser_error (parser, "expected %<:%> or %<...%>");
3531 else if (c_parser_next_token_is_keyword (parser, RID_DEFAULT))
3533 c_parser_consume_token (parser);
3534 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
3535 label = do_case (NULL_TREE, NULL_TREE);
3539 tree name = c_parser_peek_token (parser)->value;
3543 gcc_assert (c_parser_next_token_is (parser, CPP_NAME));
3544 c_parser_consume_token (parser);
3545 gcc_assert (c_parser_next_token_is (parser, CPP_COLON));
3546 loc2 = c_parser_peek_token (parser)->location;
3547 c_parser_consume_token (parser);
3548 attrs = c_parser_attributes (parser);
3549 tlab = define_label (loc2, name);
3552 decl_attributes (&tlab, attrs, 0);
3553 label = add_stmt (build_stmt (LABEL_EXPR, tlab));
3557 SET_EXPR_LOCATION (label, loc1);
3560 /* Parse a statement (C90 6.6, C99 6.8).
3565 expression-statement
3573 expression-statement:
3576 selection-statement:
3580 iteration-statement:
3589 return expression[opt] ;
3602 objc-throw-statement
3603 objc-try-catch-statement
3604 objc-synchronized-statement
3606 objc-throw-statement:
3620 parallel-for-construct
3621 parallel-sections-construct
3628 parallel-directive structured-block
3631 for-directive iteration-statement
3634 sections-directive section-scope
3637 single-directive structured-block
3639 parallel-for-construct:
3640 parallel-for-directive iteration-statement
3642 parallel-sections-construct:
3643 parallel-sections-directive section-scope
3646 master-directive structured-block
3649 critical-directive structured-block
3652 atomic-directive expression-statement
3655 ordered-directive structured-block */
3658 c_parser_statement (c_parser *parser)
3660 while (c_parser_next_token_is_keyword (parser, RID_CASE)
3661 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
3662 || (c_parser_next_token_is (parser, CPP_NAME)
3663 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
3664 c_parser_label (parser);
3665 c_parser_statement_after_labels (parser);
3668 /* Parse a statement, other than a labeled statement. */
3671 c_parser_statement_after_labels (c_parser *parser)
3673 location_t loc = c_parser_peek_token (parser)->location;
3674 tree stmt = NULL_TREE;
3675 switch (c_parser_peek_token (parser)->type)
3677 case CPP_OPEN_BRACE:
3678 add_stmt (c_parser_compound_statement (parser));
3681 switch (c_parser_peek_token (parser)->keyword)
3684 c_parser_if_statement (parser);
3687 c_parser_switch_statement (parser);
3690 c_parser_while_statement (parser);
3693 c_parser_do_statement (parser);
3696 c_parser_for_statement (parser);
3699 c_parser_consume_token (parser);
3700 if (c_parser_next_token_is (parser, CPP_NAME))
3702 stmt = c_finish_goto_label (c_parser_peek_token (parser)->value);
3703 c_parser_consume_token (parser);
3705 else if (c_parser_next_token_is (parser, CPP_MULT))
3707 c_parser_consume_token (parser);
3708 stmt = c_finish_goto_ptr (c_parser_expression (parser).value);
3711 c_parser_error (parser, "expected identifier or %<*%>");
3712 goto expect_semicolon;
3714 c_parser_consume_token (parser);
3715 stmt = c_finish_bc_stmt (&c_cont_label, false);
3716 goto expect_semicolon;
3718 c_parser_consume_token (parser);
3719 stmt = c_finish_bc_stmt (&c_break_label, true);
3720 goto expect_semicolon;
3722 c_parser_consume_token (parser);
3723 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
3725 stmt = c_finish_return (NULL_TREE);
3726 c_parser_consume_token (parser);
3730 stmt = c_finish_return (c_parser_expression_conv (parser).value);
3731 goto expect_semicolon;
3735 stmt = c_parser_asm_statement (parser);
3738 gcc_assert (c_dialect_objc ());
3739 c_parser_consume_token (parser);
3740 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
3742 stmt = objc_build_throw_stmt (NULL_TREE);
3743 c_parser_consume_token (parser);
3748 = objc_build_throw_stmt (c_parser_expression (parser).value);
3749 goto expect_semicolon;
3753 gcc_assert (c_dialect_objc ());
3754 c_parser_objc_try_catch_statement (parser);
3756 case RID_AT_SYNCHRONIZED:
3757 gcc_assert (c_dialect_objc ());
3758 c_parser_objc_synchronized_statement (parser);
3765 c_parser_consume_token (parser);
3767 case CPP_CLOSE_PAREN:
3768 case CPP_CLOSE_SQUARE:
3769 /* Avoid infinite loop in error recovery:
3770 c_parser_skip_until_found stops at a closing nesting
3771 delimiter without consuming it, but here we need to consume
3772 it to proceed further. */
3773 c_parser_error (parser, "expected statement");
3774 c_parser_consume_token (parser);
3777 c_parser_pragma (parser, pragma_stmt);
3781 stmt = c_finish_expr_stmt (c_parser_expression_conv (parser).value);
3783 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
3786 /* Two cases cannot and do not have line numbers associated: If stmt
3787 is degenerate, such as "2;", then stmt is an INTEGER_CST, which
3788 cannot hold line numbers. But that's OK because the statement
3789 will either be changed to a MODIFY_EXPR during gimplification of
3790 the statement expr, or discarded. If stmt was compound, but
3791 without new variables, we will have skipped the creation of a
3792 BIND and will have a bare STATEMENT_LIST. But that's OK because
3793 (recursively) all of the component statements should already have
3794 line numbers assigned. ??? Can we discard no-op statements
3796 if (stmt && CAN_HAVE_LOCATION_P (stmt))
3797 SET_EXPR_LOCATION (stmt, loc);
3800 /* Parse a parenthesized condition from an if, do or while statement.
3806 c_parser_paren_condition (c_parser *parser)
3810 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3811 return error_mark_node;
3812 loc = c_parser_peek_token (parser)->location;
3813 cond = c_objc_common_truthvalue_conversion
3814 (c_parser_expression_conv (parser).value);
3815 if (CAN_HAVE_LOCATION_P (cond))
3816 SET_EXPR_LOCATION (cond, loc);
3817 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
3821 /* Parse a statement which is a block in C99. */
3824 c_parser_c99_block_statement (c_parser *parser)
3826 tree block = c_begin_compound_stmt (flag_isoc99);
3827 c_parser_statement (parser);
3828 return c_end_compound_stmt (block, flag_isoc99);
3831 /* Parse the body of an if statement or the else half thereof. This
3832 is just parsing a statement but (a) it is a block in C99, (b) we
3833 track whether the body is an if statement for the sake of
3834 -Wparentheses warnings, (c) we handle an empty body specially for
3835 the sake of -Wextra warnings. */
3838 c_parser_if_body (c_parser *parser, bool *if_p)
3840 tree block = c_begin_compound_stmt (flag_isoc99);
3841 while (c_parser_next_token_is_keyword (parser, RID_CASE)
3842 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
3843 || (c_parser_next_token_is (parser, CPP_NAME)
3844 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
3845 c_parser_label (parser);
3846 *if_p = c_parser_next_token_is_keyword (parser, RID_IF);
3847 if (extra_warnings && c_parser_next_token_is (parser, CPP_SEMICOLON))
3848 add_stmt (build_empty_stmt ());
3849 c_parser_statement_after_labels (parser);
3850 return c_end_compound_stmt (block, flag_isoc99);
3853 /* Parse an if statement (C90 6.6.4, C99 6.8.4).
3856 if ( expression ) statement
3857 if ( expression ) statement else statement
3861 c_parser_if_statement (c_parser *parser)
3866 bool first_if = false, second_if = false;
3867 tree first_body, second_body;
3868 gcc_assert (c_parser_next_token_is_keyword (parser, RID_IF));
3869 c_parser_consume_token (parser);
3870 block = c_begin_compound_stmt (flag_isoc99);
3871 loc = c_parser_peek_token (parser)->location;
3872 cond = c_parser_paren_condition (parser);
3873 first_body = c_parser_if_body (parser, &first_if);
3874 if (c_parser_next_token_is_keyword (parser, RID_ELSE))
3876 c_parser_consume_token (parser);
3877 second_body = c_parser_if_body (parser, &second_if);
3880 second_body = NULL_TREE;
3881 c_finish_if_stmt (loc, cond, first_body, second_body, first_if);
3882 add_stmt (c_end_compound_stmt (block, flag_isoc99));
3885 /* Parse a switch statement (C90 6.6.4, C99 6.8.4).
3888 switch (expression) statement
3892 c_parser_switch_statement (c_parser *parser)
3894 tree block, expr, body, save_break;
3895 gcc_assert (c_parser_next_token_is_keyword (parser, RID_SWITCH));
3896 c_parser_consume_token (parser);
3897 block = c_begin_compound_stmt (flag_isoc99);
3898 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3900 expr = c_parser_expression (parser).value;
3901 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
3904 expr = error_mark_node;
3905 c_start_case (expr);
3906 save_break = c_break_label;
3907 c_break_label = NULL_TREE;
3908 body = c_parser_c99_block_statement (parser);
3909 c_finish_case (body);
3911 add_stmt (build1 (LABEL_EXPR, void_type_node, c_break_label));
3912 c_break_label = save_break;
3913 add_stmt (c_end_compound_stmt (block, flag_isoc99));
3916 /* Parse a while statement (C90 6.6.5, C99 6.8.5).
3919 while (expression) statement
3923 c_parser_while_statement (c_parser *parser)
3925 tree block, cond, body, save_break, save_cont;
3927 gcc_assert (c_parser_next_token_is_keyword (parser, RID_WHILE));
3928 c_parser_consume_token (parser);
3929 block = c_begin_compound_stmt (flag_isoc99);
3930 loc = c_parser_peek_token (parser)->location;
3931 cond = c_parser_paren_condition (parser);
3932 save_break = c_break_label;
3933 c_break_label = NULL_TREE;
3934 save_cont = c_cont_label;
3935 c_cont_label = NULL_TREE;
3936 body = c_parser_c99_block_statement (parser);
3937 c_finish_loop (loc, cond, NULL, body, c_break_label, c_cont_label, true);
3938 add_stmt (c_end_compound_stmt (block, flag_isoc99));
3939 c_break_label = save_break;
3940 c_cont_label = save_cont;
3943 /* Parse a do statement (C90 6.6.5, C99 6.8.5).
3946 do statement while ( expression ) ;
3950 c_parser_do_statement (c_parser *parser)
3952 tree block, cond, body, save_break, save_cont, new_break, new_cont;
3954 gcc_assert (c_parser_next_token_is_keyword (parser, RID_DO));
3955 c_parser_consume_token (parser);
3956 block = c_begin_compound_stmt (flag_isoc99);
3957 loc = c_parser_peek_token (parser)->location;
3958 save_break = c_break_label;
3959 c_break_label = NULL_TREE;
3960 save_cont = c_cont_label;
3961 c_cont_label = NULL_TREE;
3962 body = c_parser_c99_block_statement (parser);
3963 c_parser_require_keyword (parser, RID_WHILE, "expected %<while%>");
3964 new_break = c_break_label;
3965 c_break_label = save_break;
3966 new_cont = c_cont_label;
3967 c_cont_label = save_cont;
3968 cond = c_parser_paren_condition (parser);
3969 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
3970 c_parser_skip_to_end_of_block_or_statement (parser);
3971 c_finish_loop (loc, cond, NULL, body, new_break, new_cont, false);
3972 add_stmt (c_end_compound_stmt (block, flag_isoc99));
3975 /* Parse a for statement (C90 6.6.5, C99 6.8.5).
3978 for ( expression[opt] ; expression[opt] ; expression[opt] ) statement
3979 for ( nested-declaration expression[opt] ; expression[opt] ) statement
3981 The form with a declaration is new in C99.
3983 ??? In accordance with the old parser, the declaration may be a
3984 nested function, which is then rejected in check_for_loop_decls,
3985 but does it make any sense for this to be included in the grammar?
3986 Note in particular that the nested function does not include a
3987 trailing ';', whereas the "declaration" production includes one.
3988 Also, can we reject bad declarations earlier and cheaper than
3989 check_for_loop_decls? */
3992 c_parser_for_statement (c_parser *parser)
3994 tree block, cond, incr, save_break, save_cont, body;
3996 gcc_assert (c_parser_next_token_is_keyword (parser, RID_FOR));
3997 loc = c_parser_peek_token (parser)->location;
3998 c_parser_consume_token (parser);
3999 block = c_begin_compound_stmt (flag_isoc99);
4000 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4002 /* Parse the initialization declaration or expression. */
4003 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
4005 c_parser_consume_token (parser);
4006 c_finish_expr_stmt (NULL_TREE);
4008 else if (c_parser_next_token_starts_declspecs (parser))
4010 c_parser_declaration_or_fndef (parser, true, true, true, true);
4011 check_for_loop_decls ();
4013 else if (c_parser_next_token_is_keyword (parser, RID_EXTENSION))
4015 /* __extension__ can start a declaration, but is also an
4016 unary operator that can start an expression. Consume all
4017 but the last of a possible series of __extension__ to
4019 while (c_parser_peek_2nd_token (parser)->type == CPP_KEYWORD
4020 && (c_parser_peek_2nd_token (parser)->keyword
4022 c_parser_consume_token (parser);
4023 if (c_token_starts_declspecs (c_parser_peek_2nd_token (parser)))
4026 ext = disable_extension_diagnostics ();
4027 c_parser_consume_token (parser);
4028 c_parser_declaration_or_fndef (parser, true, true, true, true);
4029 restore_extension_diagnostics (ext);
4030 check_for_loop_decls ();
4038 c_finish_expr_stmt (c_parser_expression (parser).value);
4039 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
4041 /* Parse the loop condition. */
4042 loc = c_parser_peek_token (parser)->location;
4043 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
4045 c_parser_consume_token (parser);
4050 tree ocond = c_parser_expression_conv (parser).value;
4051 cond = c_objc_common_truthvalue_conversion (ocond);
4052 if (CAN_HAVE_LOCATION_P (cond))
4053 SET_EXPR_LOCATION (cond, loc);
4054 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
4056 /* Parse the increment expression. */
4057 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4058 incr = c_process_expr_stmt (NULL_TREE);
4060 incr = c_process_expr_stmt (c_parser_expression (parser).value);
4061 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
4065 cond = error_mark_node;
4066 incr = error_mark_node;
4068 save_break = c_break_label;
4069 c_break_label = NULL_TREE;
4070 save_cont = c_cont_label;
4071 c_cont_label = NULL_TREE;
4072 body = c_parser_c99_block_statement (parser);
4073 c_finish_loop (loc, cond, incr, body, c_break_label, c_cont_label, true);
4074 add_stmt (c_end_compound_stmt (block, flag_isoc99));
4075 c_break_label = save_break;
4076 c_cont_label = save_cont;
4079 /* Parse an asm statement, a GNU extension. This is a full-blown asm
4080 statement with inputs, outputs, clobbers, and volatile tag
4084 asm type-qualifier[opt] ( asm-argument ) ;
4088 asm-string-literal : asm-operands[opt]
4089 asm-string-literal : asm-operands[opt] : asm-operands[opt]
4090 asm-string-literal : asm-operands[opt] : asm-operands[opt] : asm-clobbers
4092 Qualifiers other than volatile are accepted in the syntax but
4096 c_parser_asm_statement (c_parser *parser)
4098 tree quals, str, outputs, inputs, clobbers, ret;
4100 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ASM));
4101 c_parser_consume_token (parser);
4102 if (c_parser_next_token_is_keyword (parser, RID_VOLATILE))
4104 quals = c_parser_peek_token (parser)->value;
4105 c_parser_consume_token (parser);
4107 else if (c_parser_next_token_is_keyword (parser, RID_CONST)
4108 || c_parser_next_token_is_keyword (parser, RID_RESTRICT))