1 /* Parser for C and Objective-C.
2 Copyright (C) 1987, 1988, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2007, 2008, 2009, 2010
4 Free Software Foundation, Inc.
6 Parser actions based on the old Bison parser; structure somewhat
7 influenced by and fragments based on the C++ parser.
9 This file is part of GCC.
11 GCC is free software; you can redistribute it and/or modify it under
12 the terms of the GNU General Public License as published by the Free
13 Software Foundation; either version 3, or (at your option) any later
16 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
17 WARRANTY; without even the implied warranty of MERCHANTABILITY or
18 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21 You should have received a copy of the GNU General Public License
22 along with GCC; see the file COPYING3. If not see
23 <http://www.gnu.org/licenses/>. */
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"
64 /* Initialization routine for this file. */
69 /* The only initialization required is of the reserved word
75 /* Make sure RID_MAX hasn't grown past the 8 bits used to hold the keyword in
76 the c_token structure. */
77 gcc_assert (RID_MAX <= 255);
84 mask |= D_ASM | D_EXT;
88 if (!c_dialect_objc ())
89 mask |= D_OBJC | D_CXX_OBJC;
91 ridpointers = GGC_CNEWVEC (tree, (int) RID_MAX);
92 for (i = 0; i < num_c_common_reswords; i++)
94 /* If a keyword is disabled, do not enter it into the table
95 and so create a canonical spelling that isn't a keyword. */
96 if (c_common_reswords[i].disable & mask)
99 && (c_common_reswords[i].disable & D_CXXWARN))
101 id = get_identifier (c_common_reswords[i].word);
102 C_SET_RID_CODE (id, RID_CXX_COMPAT_WARN);
103 C_IS_RESERVED_WORD (id) = 1;
108 id = get_identifier (c_common_reswords[i].word);
109 C_SET_RID_CODE (id, c_common_reswords[i].rid);
110 C_IS_RESERVED_WORD (id) = 1;
111 ridpointers [(int) c_common_reswords[i].rid] = id;
115 /* The C lexer intermediates between the lexer in cpplib and c-lex.c
116 and the C parser. Unlike the C++ lexer, the parser structure
117 stores the lexer information instead of using a separate structure.
118 Identifiers are separated into ordinary identifiers, type names,
119 keywords and some other Objective-C types of identifiers, and some
120 look-ahead is maintained.
122 ??? It might be a good idea to lex the whole file up front (as for
123 C++). It would then be possible to share more of the C and C++
124 lexer code, if desired. */
126 /* The following local token type is used. */
129 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
131 /* More information about the type of a CPP_NAME token. */
132 typedef enum c_id_kind {
133 /* An ordinary identifier. */
135 /* An identifier declared as a typedef name. */
137 /* An identifier declared as an Objective-C class name. */
139 /* An address space identifier. */
141 /* Not an identifier. */
145 /* A single C token after string literal concatenation and conversion
146 of preprocessing tokens to tokens. */
147 typedef struct GTY (()) c_token {
148 /* The kind of token. */
149 ENUM_BITFIELD (cpp_ttype) type : 8;
150 /* If this token is a CPP_NAME, this value indicates whether also
151 declared as some kind of type. Otherwise, it is C_ID_NONE. */
152 ENUM_BITFIELD (c_id_kind) id_kind : 8;
153 /* If this token is a keyword, this value indicates which keyword.
154 Otherwise, this value is RID_MAX. */
155 ENUM_BITFIELD (rid) keyword : 8;
156 /* If this token is a CPP_PRAGMA, this indicates the pragma that
157 was seen. Otherwise it is PRAGMA_NONE. */
158 ENUM_BITFIELD (pragma_kind) pragma_kind : 8;
159 /* The location at which this token was found. */
161 /* The value associated with this token, if any. */
165 /* A parser structure recording information about the state and
166 context of parsing. Includes lexer information with up to two
167 tokens of look-ahead; more are not needed for C. */
168 typedef struct GTY(()) c_parser {
169 /* The look-ahead tokens. */
171 /* How many look-ahead tokens are available (0, 1 or 2). */
173 /* True if a syntax error is being recovered from; false otherwise.
174 c_parser_error sets this flag. It should clear this flag when
175 enough tokens have been consumed to recover from the error. */
176 BOOL_BITFIELD error : 1;
177 /* True if we're processing a pragma, and shouldn't automatically
178 consume CPP_PRAGMA_EOL. */
179 BOOL_BITFIELD in_pragma : 1;
180 /* True if we're parsing the outermost block of an if statement. */
181 BOOL_BITFIELD in_if_block : 1;
182 /* True if we want to lex an untranslated string. */
183 BOOL_BITFIELD lex_untranslated_string : 1;
184 /* Objective-C specific parser/lexer information. */
185 BOOL_BITFIELD objc_pq_context : 1;
186 /* The following flag is needed to contextualize Objective-C lexical
187 analysis. In some cases (e.g., 'int NSObject;'), it is
188 undesirable to bind an identifier to an Objective-C class, even
189 if a class with that name exists. */
190 BOOL_BITFIELD objc_need_raw_identifier : 1;
194 /* The actual parser and external interface. ??? Does this need to be
195 garbage-collected? */
197 static GTY (()) c_parser *the_parser;
200 /* Read in and lex a single token, storing it in *TOKEN. */
203 c_lex_one_token (c_parser *parser, c_token *token)
205 timevar_push (TV_LEX);
207 token->type = c_lex_with_flags (&token->value, &token->location, NULL,
208 (parser->lex_untranslated_string
209 ? C_LEX_STRING_NO_TRANSLATE : 0));
210 token->id_kind = C_ID_NONE;
211 token->keyword = RID_MAX;
212 token->pragma_kind = PRAGMA_NONE;
220 bool objc_force_identifier = parser->objc_need_raw_identifier;
221 if (c_dialect_objc ())
222 parser->objc_need_raw_identifier = false;
224 if (C_IS_RESERVED_WORD (token->value))
226 enum rid rid_code = C_RID_CODE (token->value);
228 if (rid_code == RID_CXX_COMPAT_WARN)
230 warning_at (token->location,
232 "identifier %qE conflicts with C++ keyword",
235 else if (rid_code >= RID_FIRST_ADDR_SPACE
236 && rid_code <= RID_LAST_ADDR_SPACE)
238 token->id_kind = C_ID_ADDRSPACE;
239 token->keyword = rid_code;
242 else if (c_dialect_objc ())
244 if (!objc_is_reserved_word (token->value)
245 && (!OBJC_IS_PQ_KEYWORD (rid_code)
246 || parser->objc_pq_context))
248 /* Return the canonical spelling for this keyword. */
249 token->value = ridpointers[(int) rid_code];
250 token->type = CPP_KEYWORD;
251 token->keyword = rid_code;
257 token->type = CPP_KEYWORD;
258 token->keyword = rid_code;
263 decl = lookup_name (token->value);
266 if (TREE_CODE (decl) == TYPE_DECL)
268 token->id_kind = C_ID_TYPENAME;
272 else if (c_dialect_objc ())
274 tree objc_interface_decl = objc_is_class_name (token->value);
275 /* Objective-C class names are in the same namespace as
276 variables and typedefs, and hence are shadowed by local
278 if (objc_interface_decl
279 && (global_bindings_p ()
280 || (!objc_force_identifier && !decl)))
282 token->value = objc_interface_decl;
283 token->id_kind = C_ID_CLASSNAME;
287 token->id_kind = C_ID_ID;
291 /* This only happens in Objective-C; it must be a keyword. */
292 token->type = CPP_KEYWORD;
293 token->keyword = C_RID_CODE (token->value);
297 case CPP_CLOSE_PAREN:
299 /* These tokens may affect the interpretation of any identifiers
300 following, if doing Objective-C. */
301 if (c_dialect_objc ())
302 parser->objc_need_raw_identifier = false;
305 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
306 token->pragma_kind = (enum pragma_kind) TREE_INT_CST_LOW (token->value);
312 timevar_pop (TV_LEX);
315 /* Return a pointer to the next token from PARSER, reading it in if
318 static inline c_token *
319 c_parser_peek_token (c_parser *parser)
321 if (parser->tokens_avail == 0)
323 c_lex_one_token (parser, &parser->tokens[0]);
324 parser->tokens_avail = 1;
326 return &parser->tokens[0];
329 /* Return true if the next token from PARSER has the indicated
333 c_parser_next_token_is (c_parser *parser, enum cpp_ttype type)
335 return c_parser_peek_token (parser)->type == type;
338 /* Return true if the next token from PARSER does not have the
342 c_parser_next_token_is_not (c_parser *parser, enum cpp_ttype type)
344 return !c_parser_next_token_is (parser, type);
347 /* Return true if the next token from PARSER is the indicated
351 c_parser_next_token_is_keyword (c_parser *parser, enum rid keyword)
353 return c_parser_peek_token (parser)->keyword == keyword;
356 /* Return true if TOKEN can start a type name,
359 c_token_starts_typename (c_token *token)
364 switch (token->id_kind)
373 gcc_assert (c_dialect_objc ());
379 switch (token->keyword)
411 if (c_dialect_objc ())
419 /* Return true if the next token from PARSER can start a type name,
422 c_parser_next_token_starts_typename (c_parser *parser)
424 c_token *token = c_parser_peek_token (parser);
425 return c_token_starts_typename (token);
428 /* Return true if TOKEN can start declaration specifiers, false
431 c_token_starts_declspecs (c_token *token)
436 switch (token->id_kind)
445 gcc_assert (c_dialect_objc ());
451 switch (token->keyword)
490 if (c_dialect_objc ())
498 /* Return true if the next token from PARSER can start declaration
499 specifiers, false otherwise. */
501 c_parser_next_token_starts_declspecs (c_parser *parser)
503 c_token *token = c_parser_peek_token (parser);
504 return c_token_starts_declspecs (token);
507 /* Return a pointer to the next-but-one token from PARSER, reading it
508 in if necessary. The next token is already read in. */
511 c_parser_peek_2nd_token (c_parser *parser)
513 if (parser->tokens_avail >= 2)
514 return &parser->tokens[1];
515 gcc_assert (parser->tokens_avail == 1);
516 gcc_assert (parser->tokens[0].type != CPP_EOF);
517 gcc_assert (parser->tokens[0].type != CPP_PRAGMA_EOL);
518 c_lex_one_token (parser, &parser->tokens[1]);
519 parser->tokens_avail = 2;
520 return &parser->tokens[1];
523 /* Consume the next token from PARSER. */
526 c_parser_consume_token (c_parser *parser)
528 gcc_assert (parser->tokens_avail >= 1);
529 gcc_assert (parser->tokens[0].type != CPP_EOF);
530 gcc_assert (!parser->in_pragma || parser->tokens[0].type != CPP_PRAGMA_EOL);
531 gcc_assert (parser->error || parser->tokens[0].type != CPP_PRAGMA);
532 if (parser->tokens_avail == 2)
533 parser->tokens[0] = parser->tokens[1];
534 parser->tokens_avail--;
537 /* Expect the current token to be a #pragma. Consume it and remember
538 that we've begun parsing a pragma. */
541 c_parser_consume_pragma (c_parser *parser)
543 gcc_assert (!parser->in_pragma);
544 gcc_assert (parser->tokens_avail >= 1);
545 gcc_assert (parser->tokens[0].type == CPP_PRAGMA);
546 if (parser->tokens_avail == 2)
547 parser->tokens[0] = parser->tokens[1];
548 parser->tokens_avail--;
549 parser->in_pragma = true;
552 /* Update the globals input_location and in_system_header from
555 c_parser_set_source_position_from_token (c_token *token)
557 if (token->type != CPP_EOF)
559 input_location = token->location;
563 /* Issue a diagnostic of the form
564 FILE:LINE: MESSAGE before TOKEN
565 where TOKEN is the next token in the input stream of PARSER.
566 MESSAGE (specified by the caller) is usually of the form "expected
569 Do not issue a diagnostic if still recovering from an error.
571 ??? This is taken from the C++ parser, but building up messages in
572 this way is not i18n-friendly and some other approach should be
576 c_parser_error (c_parser *parser, const char *gmsgid)
578 c_token *token = c_parser_peek_token (parser);
581 parser->error = true;
584 /* This diagnostic makes more sense if it is tagged to the line of
585 the token we just peeked at. */
586 c_parser_set_source_position_from_token (token);
587 c_parse_error (gmsgid,
588 /* Because c_parse_error does not understand
589 CPP_KEYWORD, keywords are treated like
591 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
592 /* ??? The C parser does not save the cpp flags of a
593 token, we need to pass 0 here and we will not get
594 the source spelling of some tokens but rather the
595 canonical spelling. */
596 token->value, /*flags=*/0);
599 /* If the next token is of the indicated TYPE, consume it. Otherwise,
600 issue the error MSGID. If MSGID is NULL then a message has already
601 been produced and no message will be produced this time. Returns
602 true if found, false otherwise. */
605 c_parser_require (c_parser *parser,
609 if (c_parser_next_token_is (parser, type))
611 c_parser_consume_token (parser);
616 c_parser_error (parser, msgid);
621 /* If the next token is the indicated keyword, consume it. Otherwise,
622 issue the error MSGID. Returns true if found, false otherwise. */
625 c_parser_require_keyword (c_parser *parser,
629 if (c_parser_next_token_is_keyword (parser, keyword))
631 c_parser_consume_token (parser);
636 c_parser_error (parser, msgid);
641 /* Like c_parser_require, except that tokens will be skipped until the
642 desired token is found. An error message is still produced if the
643 next token is not as expected. If MSGID is NULL then a message has
644 already been produced and no message will be produced this
648 c_parser_skip_until_found (c_parser *parser,
652 unsigned nesting_depth = 0;
654 if (c_parser_require (parser, type, msgid))
657 /* Skip tokens until the desired token is found. */
660 /* Peek at the next token. */
661 c_token *token = c_parser_peek_token (parser);
662 /* If we've reached the token we want, consume it and stop. */
663 if (token->type == type && !nesting_depth)
665 c_parser_consume_token (parser);
669 /* If we've run out of tokens, stop. */
670 if (token->type == CPP_EOF)
672 if (token->type == CPP_PRAGMA_EOL && parser->in_pragma)
674 if (token->type == CPP_OPEN_BRACE
675 || token->type == CPP_OPEN_PAREN
676 || token->type == CPP_OPEN_SQUARE)
678 else if (token->type == CPP_CLOSE_BRACE
679 || token->type == CPP_CLOSE_PAREN
680 || token->type == CPP_CLOSE_SQUARE)
682 if (nesting_depth-- == 0)
685 /* Consume this token. */
686 c_parser_consume_token (parser);
688 parser->error = false;
691 /* Skip tokens until the end of a parameter is found, but do not
692 consume the comma, semicolon or closing delimiter. */
695 c_parser_skip_to_end_of_parameter (c_parser *parser)
697 unsigned nesting_depth = 0;
701 c_token *token = c_parser_peek_token (parser);
702 if ((token->type == CPP_COMMA || token->type == CPP_SEMICOLON)
705 /* If we've run out of tokens, stop. */
706 if (token->type == CPP_EOF)
708 if (token->type == CPP_PRAGMA_EOL && parser->in_pragma)
710 if (token->type == CPP_OPEN_BRACE
711 || token->type == CPP_OPEN_PAREN
712 || token->type == CPP_OPEN_SQUARE)
714 else if (token->type == CPP_CLOSE_BRACE
715 || token->type == CPP_CLOSE_PAREN
716 || token->type == CPP_CLOSE_SQUARE)
718 if (nesting_depth-- == 0)
721 /* Consume this token. */
722 c_parser_consume_token (parser);
724 parser->error = false;
727 /* Expect to be at the end of the pragma directive and consume an
728 end of line marker. */
731 c_parser_skip_to_pragma_eol (c_parser *parser)
733 gcc_assert (parser->in_pragma);
734 parser->in_pragma = false;
736 if (!c_parser_require (parser, CPP_PRAGMA_EOL, "expected end of line"))
739 c_token *token = c_parser_peek_token (parser);
740 if (token->type == CPP_EOF)
742 if (token->type == CPP_PRAGMA_EOL)
744 c_parser_consume_token (parser);
747 c_parser_consume_token (parser);
750 parser->error = false;
753 /* Skip tokens until we have consumed an entire block, or until we
754 have consumed a non-nested ';'. */
757 c_parser_skip_to_end_of_block_or_statement (c_parser *parser)
759 unsigned nesting_depth = 0;
760 bool save_error = parser->error;
766 /* Peek at the next token. */
767 token = c_parser_peek_token (parser);
775 if (parser->in_pragma)
780 /* If the next token is a ';', we have reached the
781 end of the statement. */
784 /* Consume the ';'. */
785 c_parser_consume_token (parser);
790 case CPP_CLOSE_BRACE:
791 /* If the next token is a non-nested '}', then we have
792 reached the end of the current block. */
793 if (nesting_depth == 0 || --nesting_depth == 0)
795 c_parser_consume_token (parser);
801 /* If it the next token is a '{', then we are entering a new
802 block. Consume the entire block. */
807 /* If we see a pragma, consume the whole thing at once. We
808 have some safeguards against consuming pragmas willy-nilly.
809 Normally, we'd expect to be here with parser->error set,
810 which disables these safeguards. But it's possible to get
811 here for secondary error recovery, after parser->error has
813 c_parser_consume_pragma (parser);
814 c_parser_skip_to_pragma_eol (parser);
815 parser->error = save_error;
822 c_parser_consume_token (parser);
826 parser->error = false;
829 /* CPP's options (initialized by c-opts.c). */
830 extern cpp_options *cpp_opts;
832 /* Save the warning flags which are controlled by __extension__. */
835 disable_extension_diagnostics (void)
838 | (warn_pointer_arith << 1)
839 | (warn_traditional << 2)
841 | (warn_long_long << 4)
842 | (warn_cxx_compat << 5));
843 cpp_opts->pedantic = pedantic = 0;
844 warn_pointer_arith = 0;
845 cpp_opts->warn_traditional = warn_traditional = 0;
847 cpp_opts->warn_long_long = warn_long_long = 0;
852 /* Restore the warning flags which are controlled by __extension__.
853 FLAGS is the return value from disable_extension_diagnostics. */
856 restore_extension_diagnostics (int flags)
858 cpp_opts->pedantic = pedantic = flags & 1;
859 warn_pointer_arith = (flags >> 1) & 1;
860 cpp_opts->warn_traditional = warn_traditional = (flags >> 2) & 1;
861 flag_iso = (flags >> 3) & 1;
862 cpp_opts->warn_long_long = warn_long_long = (flags >> 4) & 1;
863 warn_cxx_compat = (flags >> 5) & 1;
866 /* Possibly kinds of declarator to parse. */
867 typedef enum c_dtr_syn {
868 /* A normal declarator with an identifier. */
870 /* An abstract declarator (maybe empty). */
872 /* A parameter declarator: may be either, but after a type name does
873 not redeclare a typedef name as an identifier if it can
874 alternatively be interpreted as a typedef name; see DR#009,
875 applied in C90 TC1, omitted from C99 and reapplied in C99 TC2
876 following DR#249. For example, given a typedef T, "int T" and
877 "int *T" are valid parameter declarations redeclaring T, while
878 "int (T)" and "int * (T)" and "int (T[])" and "int (T (int))" are
879 abstract declarators rather than involving redundant parentheses;
880 the same applies with attributes inside the parentheses before
885 static void c_parser_external_declaration (c_parser *);
886 static void c_parser_asm_definition (c_parser *);
887 static void c_parser_declaration_or_fndef (c_parser *, bool, bool, bool, bool);
888 static void c_parser_declspecs (c_parser *, struct c_declspecs *, bool, bool,
890 static struct c_typespec c_parser_enum_specifier (c_parser *);
891 static struct c_typespec c_parser_struct_or_union_specifier (c_parser *);
892 static tree c_parser_struct_declaration (c_parser *);
893 static struct c_typespec c_parser_typeof_specifier (c_parser *);
894 static struct c_declarator *c_parser_declarator (c_parser *, bool, c_dtr_syn,
896 static struct c_declarator *c_parser_direct_declarator (c_parser *, bool,
898 static struct c_declarator *c_parser_direct_declarator_inner (c_parser *,
900 struct c_declarator *);
901 static struct c_arg_info *c_parser_parms_declarator (c_parser *, bool, tree);
902 static struct c_arg_info *c_parser_parms_list_declarator (c_parser *, tree);
903 static struct c_parm *c_parser_parameter_declaration (c_parser *, tree);
904 static tree c_parser_simple_asm_expr (c_parser *);
905 static tree c_parser_attributes (c_parser *);
906 static struct c_type_name *c_parser_type_name (c_parser *);
907 static struct c_expr c_parser_initializer (c_parser *);
908 static struct c_expr c_parser_braced_init (c_parser *, tree, bool);
909 static void c_parser_initelt (c_parser *, struct obstack *);
910 static void c_parser_initval (c_parser *, struct c_expr *,
912 static tree c_parser_compound_statement (c_parser *);
913 static void c_parser_compound_statement_nostart (c_parser *);
914 static void c_parser_label (c_parser *);
915 static void c_parser_statement (c_parser *);
916 static void c_parser_statement_after_labels (c_parser *);
917 static void c_parser_if_statement (c_parser *);
918 static void c_parser_switch_statement (c_parser *);
919 static void c_parser_while_statement (c_parser *);
920 static void c_parser_do_statement (c_parser *);
921 static void c_parser_for_statement (c_parser *);
922 static tree c_parser_asm_statement (c_parser *);
923 static tree c_parser_asm_operands (c_parser *, bool);
924 static tree c_parser_asm_goto_operands (c_parser *);
925 static tree c_parser_asm_clobbers (c_parser *);
926 static struct c_expr c_parser_expr_no_commas (c_parser *, struct c_expr *);
927 static struct c_expr c_parser_conditional_expression (c_parser *,
929 static struct c_expr c_parser_binary_expression (c_parser *, struct c_expr *);
930 static struct c_expr c_parser_cast_expression (c_parser *, struct c_expr *);
931 static struct c_expr c_parser_unary_expression (c_parser *);
932 static struct c_expr c_parser_sizeof_expression (c_parser *);
933 static struct c_expr c_parser_alignof_expression (c_parser *);
934 static struct c_expr c_parser_postfix_expression (c_parser *);
935 static struct c_expr c_parser_postfix_expression_after_paren_type (c_parser *,
936 struct c_type_name *,
938 static struct c_expr c_parser_postfix_expression_after_primary (c_parser *,
941 static struct c_expr c_parser_expression (c_parser *);
942 static struct c_expr c_parser_expression_conv (c_parser *);
943 static VEC(tree,gc) *c_parser_expr_list (c_parser *, bool, bool,
945 static void c_parser_omp_construct (c_parser *);
946 static void c_parser_omp_threadprivate (c_parser *);
947 static void c_parser_omp_barrier (c_parser *);
948 static void c_parser_omp_flush (c_parser *);
949 static void c_parser_omp_taskwait (c_parser *);
951 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
952 static bool c_parser_pragma (c_parser *, enum pragma_context);
954 /* These Objective-C parser functions are only ever called when
955 compiling Objective-C. */
956 static void c_parser_objc_class_definition (c_parser *);
957 static void c_parser_objc_class_instance_variables (c_parser *);
958 static void c_parser_objc_class_declaration (c_parser *);
959 static void c_parser_objc_alias_declaration (c_parser *);
960 static void c_parser_objc_protocol_definition (c_parser *);
961 static enum tree_code c_parser_objc_method_type (c_parser *);
962 static void c_parser_objc_method_definition (c_parser *);
963 static void c_parser_objc_methodprotolist (c_parser *);
964 static void c_parser_objc_methodproto (c_parser *);
965 static tree c_parser_objc_method_decl (c_parser *);
966 static tree c_parser_objc_type_name (c_parser *);
967 static tree c_parser_objc_protocol_refs (c_parser *);
968 static void c_parser_objc_try_catch_statement (c_parser *);
969 static void c_parser_objc_synchronized_statement (c_parser *);
970 static tree c_parser_objc_selector (c_parser *);
971 static tree c_parser_objc_selector_arg (c_parser *);
972 static tree c_parser_objc_receiver (c_parser *);
973 static tree c_parser_objc_message_args (c_parser *);
974 static tree c_parser_objc_keywordexpr (c_parser *);
976 /* Parse a translation unit (C90 6.7, C99 6.9).
979 external-declarations
981 external-declarations:
983 external-declarations external-declaration
992 c_parser_translation_unit (c_parser *parser)
994 if (c_parser_next_token_is (parser, CPP_EOF))
996 pedwarn (c_parser_peek_token (parser)->location, OPT_pedantic,
997 "ISO C forbids an empty translation unit");
1001 void *obstack_position = obstack_alloc (&parser_obstack, 0);
1002 mark_valid_location_for_stdc_pragma (false);
1006 c_parser_external_declaration (parser);
1007 obstack_free (&parser_obstack, obstack_position);
1009 while (c_parser_next_token_is_not (parser, CPP_EOF));
1013 /* Parse an external declaration (C90 6.7, C99 6.9).
1015 external-declaration:
1021 external-declaration:
1024 __extension__ external-declaration
1028 external-declaration:
1029 objc-class-definition
1030 objc-class-declaration
1031 objc-alias-declaration
1032 objc-protocol-definition
1033 objc-method-definition
1038 c_parser_external_declaration (c_parser *parser)
1041 switch (c_parser_peek_token (parser)->type)
1044 switch (c_parser_peek_token (parser)->keyword)
1047 ext = disable_extension_diagnostics ();
1048 c_parser_consume_token (parser);
1049 c_parser_external_declaration (parser);
1050 restore_extension_diagnostics (ext);
1053 c_parser_asm_definition (parser);
1055 case RID_AT_INTERFACE:
1056 case RID_AT_IMPLEMENTATION:
1057 gcc_assert (c_dialect_objc ());
1058 c_parser_objc_class_definition (parser);
1061 gcc_assert (c_dialect_objc ());
1062 c_parser_objc_class_declaration (parser);
1065 gcc_assert (c_dialect_objc ());
1066 c_parser_objc_alias_declaration (parser);
1068 case RID_AT_PROTOCOL:
1069 gcc_assert (c_dialect_objc ());
1070 c_parser_objc_protocol_definition (parser);
1073 gcc_assert (c_dialect_objc ());
1074 c_parser_consume_token (parser);
1075 objc_finish_implementation ();
1082 pedwarn (c_parser_peek_token (parser)->location, OPT_pedantic,
1083 "ISO C does not allow extra %<;%> outside of a function");
1084 c_parser_consume_token (parser);
1087 mark_valid_location_for_stdc_pragma (true);
1088 c_parser_pragma (parser, pragma_external);
1089 mark_valid_location_for_stdc_pragma (false);
1093 if (c_dialect_objc ())
1095 c_parser_objc_method_definition (parser);
1098 /* Else fall through, and yield a syntax error trying to parse
1099 as a declaration or function definition. */
1102 /* A declaration or a function definition. We can only tell
1103 which after parsing the declaration specifiers, if any, and
1104 the first declarator. */
1105 c_parser_declaration_or_fndef (parser, true, true, false, true);
1111 /* Parse a declaration or function definition (C90 6.5, 6.7.1, C99
1112 6.7, 6.9.1). If FNDEF_OK is true, a function definition is
1113 accepted; otherwise (old-style parameter declarations) only other
1114 declarations are accepted. If NESTED is true, we are inside a
1115 function or parsing old-style parameter declarations; any functions
1116 encountered are nested functions and declaration specifiers are
1117 required; otherwise we are at top level and functions are normal
1118 functions and declaration specifiers may be optional. If EMPTY_OK
1119 is true, empty declarations are OK (subject to all other
1120 constraints); otherwise (old-style parameter declarations) they are
1121 diagnosed. If START_ATTR_OK is true, the declaration specifiers
1122 may start with attributes; otherwise they may not.
1125 declaration-specifiers init-declarator-list[opt] ;
1127 function-definition:
1128 declaration-specifiers[opt] declarator declaration-list[opt]
1133 declaration-list declaration
1135 init-declarator-list:
1137 init-declarator-list , init-declarator
1140 declarator simple-asm-expr[opt] attributes[opt]
1141 declarator simple-asm-expr[opt] attributes[opt] = initializer
1145 nested-function-definition:
1146 declaration-specifiers declarator declaration-list[opt]
1149 The simple-asm-expr and attributes are GNU extensions.
1151 This function does not handle __extension__; that is handled in its
1152 callers. ??? Following the old parser, __extension__ may start
1153 external declarations, declarations in functions and declarations
1154 at the start of "for" loops, but not old-style parameter
1157 C99 requires declaration specifiers in a function definition; the
1158 absence is diagnosed through the diagnosis of implicit int. In GNU
1159 C we also allow but diagnose declarations without declaration
1160 specifiers, but only at top level (elsewhere they conflict with
1166 threadprivate-directive */
1169 c_parser_declaration_or_fndef (c_parser *parser, bool fndef_ok, bool empty_ok,
1170 bool nested, bool start_attr_ok)
1172 struct c_declspecs *specs;
1174 tree all_prefix_attrs;
1175 bool diagnosed_no_specs = false;
1176 location_t here = c_parser_peek_token (parser)->location;
1178 specs = build_null_declspecs ();
1179 c_parser_declspecs (parser, specs, true, true, start_attr_ok);
1182 c_parser_skip_to_end_of_block_or_statement (parser);
1185 if (nested && !specs->declspecs_seen_p)
1187 c_parser_error (parser, "expected declaration specifiers");
1188 c_parser_skip_to_end_of_block_or_statement (parser);
1191 finish_declspecs (specs);
1192 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1198 shadow_tag_warned (specs, 1);
1199 pedwarn (here, 0, "empty declaration");
1201 c_parser_consume_token (parser);
1204 pending_xref_error ();
1205 prefix_attrs = specs->attrs;
1206 all_prefix_attrs = prefix_attrs;
1207 specs->attrs = NULL_TREE;
1210 struct c_declarator *declarator;
1213 /* Declaring either one or more declarators (in which case we
1214 should diagnose if there were no declaration specifiers) or a
1215 function definition (in which case the diagnostic for
1216 implicit int suffices). */
1217 declarator = c_parser_declarator (parser, specs->type_seen_p,
1218 C_DTR_NORMAL, &dummy);
1219 if (declarator == NULL)
1221 c_parser_skip_to_end_of_block_or_statement (parser);
1224 if (c_parser_next_token_is (parser, CPP_EQ)
1225 || c_parser_next_token_is (parser, CPP_COMMA)
1226 || c_parser_next_token_is (parser, CPP_SEMICOLON)
1227 || c_parser_next_token_is_keyword (parser, RID_ASM)
1228 || c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
1230 tree asm_name = NULL_TREE;
1231 tree postfix_attrs = NULL_TREE;
1232 if (!diagnosed_no_specs && !specs->declspecs_seen_p)
1234 diagnosed_no_specs = true;
1235 pedwarn (here, 0, "data definition has no type or storage class");
1237 /* Having seen a data definition, there cannot now be a
1238 function definition. */
1240 if (c_parser_next_token_is_keyword (parser, RID_ASM))
1241 asm_name = c_parser_simple_asm_expr (parser);
1242 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
1243 postfix_attrs = c_parser_attributes (parser);
1244 if (c_parser_next_token_is (parser, CPP_EQ))
1248 location_t init_loc;
1249 c_parser_consume_token (parser);
1250 /* The declaration of the variable is in effect while
1251 its initializer is parsed. */
1252 d = start_decl (declarator, specs, true,
1253 chainon (postfix_attrs, all_prefix_attrs));
1255 d = error_mark_node;
1256 start_init (d, asm_name, global_bindings_p ());
1257 init_loc = c_parser_peek_token (parser)->location;
1258 init = c_parser_initializer (parser);
1260 if (d != error_mark_node)
1262 maybe_warn_string_init (TREE_TYPE (d), init);
1263 finish_decl (d, init_loc, init.value,
1264 init.original_type, asm_name);
1269 tree d = start_decl (declarator, specs, false,
1270 chainon (postfix_attrs,
1273 finish_decl (d, UNKNOWN_LOCATION, NULL_TREE,
1274 NULL_TREE, asm_name);
1276 if (c_parser_next_token_is (parser, CPP_COMMA))
1278 c_parser_consume_token (parser);
1279 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
1280 all_prefix_attrs = chainon (c_parser_attributes (parser),
1283 all_prefix_attrs = prefix_attrs;
1286 else if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1288 c_parser_consume_token (parser);
1293 c_parser_error (parser, "expected %<,%> or %<;%>");
1294 c_parser_skip_to_end_of_block_or_statement (parser);
1300 c_parser_error (parser, "expected %<=%>, %<,%>, %<;%>, "
1301 "%<asm%> or %<__attribute__%>");
1302 c_parser_skip_to_end_of_block_or_statement (parser);
1305 /* Function definition (nested or otherwise). */
1308 pedwarn (here, OPT_pedantic, "ISO C forbids nested functions");
1309 c_push_function_context ();
1311 if (!start_function (specs, declarator, all_prefix_attrs))
1313 /* This can appear in many cases looking nothing like a
1314 function definition, so we don't give a more specific
1315 error suggesting there was one. */
1316 c_parser_error (parser, "expected %<=%>, %<,%>, %<;%>, %<asm%> "
1317 "or %<__attribute__%>");
1319 c_pop_function_context ();
1322 /* Parse old-style parameter declarations. ??? Attributes are
1323 not allowed to start declaration specifiers here because of a
1324 syntax conflict between a function declaration with attribute
1325 suffix and a function definition with an attribute prefix on
1326 first old-style parameter declaration. Following the old
1327 parser, they are not accepted on subsequent old-style
1328 parameter declarations either. However, there is no
1329 ambiguity after the first declaration, nor indeed on the
1330 first as long as we don't allow postfix attributes after a
1331 declarator with a nonempty identifier list in a definition;
1332 and postfix attributes have never been accepted here in
1333 function definitions either. */
1334 while (c_parser_next_token_is_not (parser, CPP_EOF)
1335 && c_parser_next_token_is_not (parser, CPP_OPEN_BRACE))
1336 c_parser_declaration_or_fndef (parser, false, false, true, false);
1337 store_parm_decls ();
1338 DECL_STRUCT_FUNCTION (current_function_decl)->function_start_locus
1339 = c_parser_peek_token (parser)->location;
1340 fnbody = c_parser_compound_statement (parser);
1343 tree decl = current_function_decl;
1344 /* Mark nested functions as needing static-chain initially.
1345 lower_nested_functions will recompute it but the
1346 DECL_STATIC_CHAIN flag is also used before that happens,
1347 by initializer_constant_valid_p. See gcc.dg/nested-fn-2.c. */
1348 DECL_STATIC_CHAIN (decl) = 1;
1351 c_pop_function_context ();
1352 add_stmt (build_stmt (DECL_SOURCE_LOCATION (decl), DECL_EXPR, decl));
1363 /* Parse an asm-definition (asm() outside a function body). This is a
1371 c_parser_asm_definition (c_parser *parser)
1373 tree asm_str = c_parser_simple_asm_expr (parser);
1375 cgraph_add_asm_node (asm_str);
1376 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
1379 /* Parse some declaration specifiers (possibly none) (C90 6.5, C99
1380 6.7), adding them to SPECS (which may already include some).
1381 Storage class specifiers are accepted iff SCSPEC_OK; type
1382 specifiers are accepted iff TYPESPEC_OK; attributes are accepted at
1383 the start iff START_ATTR_OK.
1385 declaration-specifiers:
1386 storage-class-specifier declaration-specifiers[opt]
1387 type-specifier declaration-specifiers[opt]
1388 type-qualifier declaration-specifiers[opt]
1389 function-specifier declaration-specifiers[opt]
1391 Function specifiers (inline) are from C99, and are currently
1392 handled as storage class specifiers, as is __thread.
1394 C90 6.5.1, C99 6.7.1:
1395 storage-class-specifier:
1406 C90 6.5.2, C99 6.7.2:
1419 [_Imaginary removed in C99 TC2]
1420 struct-or-union-specifier
1424 (_Bool and _Complex are new in C99.)
1426 C90 6.5.3, C99 6.7.3:
1432 address-space-qualifier
1434 (restrict is new in C99.)
1438 declaration-specifiers:
1439 attributes declaration-specifiers[opt]
1445 identifier recognized by the target
1447 storage-class-specifier:
1459 (_Fract, _Accum, and _Sat are new from ISO/IEC DTR 18037:
1460 http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1169.pdf)
1465 class-name objc-protocol-refs[opt]
1466 typedef-name objc-protocol-refs
1471 c_parser_declspecs (c_parser *parser, struct c_declspecs *specs,
1472 bool scspec_ok, bool typespec_ok, bool start_attr_ok)
1474 bool attrs_ok = start_attr_ok;
1475 bool seen_type = specs->type_seen_p;
1476 while (c_parser_next_token_is (parser, CPP_NAME)
1477 || c_parser_next_token_is (parser, CPP_KEYWORD)
1478 || (c_dialect_objc () && c_parser_next_token_is (parser, CPP_LESS)))
1480 struct c_typespec t;
1482 location_t loc = c_parser_peek_token (parser)->location;
1483 if (c_parser_next_token_is (parser, CPP_NAME))
1485 tree value = c_parser_peek_token (parser)->value;
1486 c_id_kind kind = c_parser_peek_token (parser)->id_kind;
1488 if (kind == C_ID_ADDRSPACE)
1491 = c_parser_peek_token (parser)->keyword - RID_FIRST_ADDR_SPACE;
1492 declspecs_add_addrspace (specs, as);
1493 c_parser_consume_token (parser);
1498 /* This finishes the specifiers unless a type name is OK, it
1499 is declared as a type name and a type name hasn't yet
1501 if (!typespec_ok || seen_type
1502 || (kind != C_ID_TYPENAME && kind != C_ID_CLASSNAME))
1504 c_parser_consume_token (parser);
1507 if (kind == C_ID_TYPENAME
1508 && (!c_dialect_objc ()
1509 || c_parser_next_token_is_not (parser, CPP_LESS)))
1511 t.kind = ctsk_typedef;
1512 /* For a typedef name, record the meaning, not the name.
1513 In case of 'foo foo, bar;'. */
1514 t.spec = lookup_name (value);
1516 t.expr_const_operands = true;
1520 tree proto = NULL_TREE;
1521 gcc_assert (c_dialect_objc ());
1523 if (c_parser_next_token_is (parser, CPP_LESS))
1524 proto = c_parser_objc_protocol_refs (parser);
1525 t.spec = objc_get_protocol_qualified_type (value, proto);
1527 t.expr_const_operands = true;
1529 declspecs_add_type (loc, specs, t);
1532 if (c_parser_next_token_is (parser, CPP_LESS))
1534 /* Make "<SomeProtocol>" equivalent to "id <SomeProtocol>" -
1535 nisse@lysator.liu.se. */
1537 gcc_assert (c_dialect_objc ());
1538 if (!typespec_ok || seen_type)
1540 proto = c_parser_objc_protocol_refs (parser);
1542 t.spec = objc_get_protocol_qualified_type (NULL_TREE, proto);
1544 t.expr_const_operands = true;
1545 declspecs_add_type (loc, specs, t);
1548 gcc_assert (c_parser_next_token_is (parser, CPP_KEYWORD));
1549 switch (c_parser_peek_token (parser)->keyword)
1561 /* TODO: Distinguish between function specifiers (inline)
1562 and storage class specifiers, either here or in
1563 declspecs_add_scspec. */
1564 declspecs_add_scspec (specs, c_parser_peek_token (parser)->value);
1565 c_parser_consume_token (parser);
1588 if (c_dialect_objc ())
1589 parser->objc_need_raw_identifier = true;
1590 t.kind = ctsk_resword;
1591 t.spec = c_parser_peek_token (parser)->value;
1593 t.expr_const_operands = true;
1594 declspecs_add_type (loc, specs, t);
1595 c_parser_consume_token (parser);
1602 t = c_parser_enum_specifier (parser);
1603 declspecs_add_type (loc, specs, t);
1611 t = c_parser_struct_or_union_specifier (parser);
1612 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, t.spec);
1613 declspecs_add_type (loc, specs, t);
1616 /* ??? The old parser rejected typeof after other type
1617 specifiers, but is a syntax error the best way of
1619 if (!typespec_ok || seen_type)
1623 t = c_parser_typeof_specifier (parser);
1624 declspecs_add_type (loc, specs, t);
1630 declspecs_add_qual (specs, c_parser_peek_token (parser)->value);
1631 c_parser_consume_token (parser);
1636 attrs = c_parser_attributes (parser);
1637 declspecs_add_attrs (specs, attrs);
1646 /* Parse an enum specifier (C90 6.5.2.2, C99 6.7.2.2).
1649 enum attributes[opt] identifier[opt] { enumerator-list } attributes[opt]
1650 enum attributes[opt] identifier[opt] { enumerator-list , } attributes[opt]
1651 enum attributes[opt] identifier
1653 The form with trailing comma is new in C99. The forms with
1654 attributes are GNU extensions. In GNU C, we accept any expression
1655 without commas in the syntax (assignment expressions, not just
1656 conditional expressions); assignment expressions will be diagnosed
1661 enumerator-list , enumerator
1664 enumeration-constant
1665 enumeration-constant = constant-expression
1668 static struct c_typespec
1669 c_parser_enum_specifier (c_parser *parser)
1671 struct c_typespec ret;
1673 tree ident = NULL_TREE;
1674 location_t enum_loc;
1675 location_t ident_loc = UNKNOWN_LOCATION; /* Quiet warning. */
1676 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ENUM));
1677 enum_loc = c_parser_peek_token (parser)->location;
1678 c_parser_consume_token (parser);
1679 attrs = c_parser_attributes (parser);
1680 enum_loc = c_parser_peek_token (parser)->location;
1681 /* Set the location in case we create a decl now. */
1682 c_parser_set_source_position_from_token (c_parser_peek_token (parser));
1683 if (c_parser_next_token_is (parser, CPP_NAME))
1685 ident = c_parser_peek_token (parser)->value;
1686 ident_loc = c_parser_peek_token (parser)->location;
1687 enum_loc = ident_loc;
1688 c_parser_consume_token (parser);
1690 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
1692 /* Parse an enum definition. */
1693 struct c_enum_contents the_enum;
1694 tree type = start_enum (enum_loc, &the_enum, ident);
1696 /* We chain the enumerators in reverse order, then put them in
1697 forward order at the end. */
1698 tree values = NULL_TREE;
1699 c_parser_consume_token (parser);
1707 location_t comma_loc = UNKNOWN_LOCATION; /* Quiet warning. */
1708 location_t value_loc;
1709 if (c_parser_next_token_is_not (parser, CPP_NAME))
1711 c_parser_error (parser, "expected identifier");
1712 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
1713 values = error_mark_node;
1716 token = c_parser_peek_token (parser);
1717 enum_id = token->value;
1718 /* Set the location in case we create a decl now. */
1719 c_parser_set_source_position_from_token (token);
1720 value_loc = token->location;
1721 c_parser_consume_token (parser);
1722 if (c_parser_next_token_is (parser, CPP_EQ))
1724 c_parser_consume_token (parser);
1725 value_loc = c_parser_peek_token (parser)->location;
1726 enum_value = c_parser_expr_no_commas (parser, NULL).value;
1729 enum_value = NULL_TREE;
1730 enum_decl = build_enumerator (value_loc,
1731 &the_enum, enum_id, enum_value);
1732 TREE_CHAIN (enum_decl) = values;
1735 if (c_parser_next_token_is (parser, CPP_COMMA))
1737 comma_loc = c_parser_peek_token (parser)->location;
1739 c_parser_consume_token (parser);
1741 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
1743 if (seen_comma && !flag_isoc99)
1744 pedwarn (comma_loc, OPT_pedantic, "comma at end of enumerator list");
1745 c_parser_consume_token (parser);
1750 c_parser_error (parser, "expected %<,%> or %<}%>");
1751 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
1752 values = error_mark_node;
1756 postfix_attrs = c_parser_attributes (parser);
1757 ret.spec = finish_enum (type, nreverse (values),
1758 chainon (attrs, postfix_attrs));
1759 ret.kind = ctsk_tagdef;
1760 ret.expr = NULL_TREE;
1761 ret.expr_const_operands = true;
1766 c_parser_error (parser, "expected %<{%>");
1767 ret.spec = error_mark_node;
1768 ret.kind = ctsk_tagref;
1769 ret.expr = NULL_TREE;
1770 ret.expr_const_operands = true;
1773 ret = parser_xref_tag (ident_loc, ENUMERAL_TYPE, ident);
1774 /* In ISO C, enumerated types can be referred to only if already
1776 if (pedantic && !COMPLETE_TYPE_P (ret.spec))
1779 pedwarn (enum_loc, OPT_pedantic,
1780 "ISO C forbids forward references to %<enum%> types");
1785 /* Parse a struct or union specifier (C90 6.5.2.1, C99 6.7.2.1).
1787 struct-or-union-specifier:
1788 struct-or-union attributes[opt] identifier[opt]
1789 { struct-contents } attributes[opt]
1790 struct-or-union attributes[opt] identifier
1793 struct-declaration-list
1795 struct-declaration-list:
1796 struct-declaration ;
1797 struct-declaration-list struct-declaration ;
1804 struct-declaration-list struct-declaration
1806 struct-declaration-list:
1807 struct-declaration-list ;
1810 (Note that in the syntax here, unlike that in ISO C, the semicolons
1811 are included here rather than in struct-declaration, in order to
1812 describe the syntax with extra semicolons and missing semicolon at
1817 struct-declaration-list:
1818 @defs ( class-name )
1820 (Note this does not include a trailing semicolon, but can be
1821 followed by further declarations, and gets a pedwarn-if-pedantic
1822 when followed by a semicolon.) */
1824 static struct c_typespec
1825 c_parser_struct_or_union_specifier (c_parser *parser)
1827 struct c_typespec ret;
1829 tree ident = NULL_TREE;
1830 location_t struct_loc;
1831 location_t ident_loc = UNKNOWN_LOCATION;
1832 enum tree_code code;
1833 switch (c_parser_peek_token (parser)->keyword)
1844 struct_loc = c_parser_peek_token (parser)->location;
1845 c_parser_consume_token (parser);
1846 attrs = c_parser_attributes (parser);
1848 /* Set the location in case we create a decl now. */
1849 c_parser_set_source_position_from_token (c_parser_peek_token (parser));
1851 if (c_parser_next_token_is (parser, CPP_NAME))
1853 ident = c_parser_peek_token (parser)->value;
1854 ident_loc = c_parser_peek_token (parser)->location;
1855 struct_loc = ident_loc;
1856 c_parser_consume_token (parser);
1858 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
1860 /* Parse a struct or union definition. Start the scope of the
1861 tag before parsing components. */
1862 struct c_struct_parse_info *struct_info;
1863 tree type = start_struct (struct_loc, code, ident, &struct_info);
1865 /* We chain the components in reverse order, then put them in
1866 forward order at the end. Each struct-declaration may
1867 declare multiple components (comma-separated), so we must use
1868 chainon to join them, although when parsing each
1869 struct-declaration we can use TREE_CHAIN directly.
1871 The theory behind all this is that there will be more
1872 semicolon separated fields than comma separated fields, and
1873 so we'll be minimizing the number of node traversals required
1875 tree contents = NULL_TREE;
1876 c_parser_consume_token (parser);
1877 /* Handle the Objective-C @defs construct,
1878 e.g. foo(sizeof(struct{ @defs(ClassName) }));. */
1879 if (c_parser_next_token_is_keyword (parser, RID_AT_DEFS))
1882 gcc_assert (c_dialect_objc ());
1883 c_parser_consume_token (parser);
1884 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
1886 if (c_parser_next_token_is (parser, CPP_NAME)
1887 && c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME)
1889 name = c_parser_peek_token (parser)->value;
1890 c_parser_consume_token (parser);
1894 c_parser_error (parser, "expected class name");
1895 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
1898 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
1900 contents = nreverse (objc_get_class_ivars (name));
1903 /* Parse the struct-declarations and semicolons. Problems with
1904 semicolons are diagnosed here; empty structures are diagnosed
1909 /* Parse any stray semicolon. */
1910 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1912 pedwarn (c_parser_peek_token (parser)->location, OPT_pedantic,
1913 "extra semicolon in struct or union specified");
1914 c_parser_consume_token (parser);
1917 /* Stop if at the end of the struct or union contents. */
1918 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
1920 c_parser_consume_token (parser);
1923 /* Accept #pragmas at struct scope. */
1924 if (c_parser_next_token_is (parser, CPP_PRAGMA))
1926 c_parser_pragma (parser, pragma_external);
1929 /* Parse some comma-separated declarations, but not the
1930 trailing semicolon if any. */
1931 decls = c_parser_struct_declaration (parser);
1932 contents = chainon (decls, contents);
1933 /* If no semicolon follows, either we have a parse error or
1934 are at the end of the struct or union and should
1936 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1937 c_parser_consume_token (parser);
1940 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
1941 pedwarn (c_parser_peek_token (parser)->location, 0,
1942 "no semicolon at end of struct or union");
1945 c_parser_error (parser, "expected %<;%>");
1946 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
1951 postfix_attrs = c_parser_attributes (parser);
1952 ret.spec = finish_struct (struct_loc, type, nreverse (contents),
1953 chainon (attrs, postfix_attrs), struct_info);
1954 ret.kind = ctsk_tagdef;
1955 ret.expr = NULL_TREE;
1956 ret.expr_const_operands = true;
1961 c_parser_error (parser, "expected %<{%>");
1962 ret.spec = error_mark_node;
1963 ret.kind = ctsk_tagref;
1964 ret.expr = NULL_TREE;
1965 ret.expr_const_operands = true;
1968 ret = parser_xref_tag (ident_loc, code, ident);
1972 /* Parse a struct-declaration (C90 6.5.2.1, C99 6.7.2.1), *without*
1973 the trailing semicolon.
1976 specifier-qualifier-list struct-declarator-list
1978 specifier-qualifier-list:
1979 type-specifier specifier-qualifier-list[opt]
1980 type-qualifier specifier-qualifier-list[opt]
1981 attributes specifier-qualifier-list[opt]
1983 struct-declarator-list:
1985 struct-declarator-list , attributes[opt] struct-declarator
1988 declarator attributes[opt]
1989 declarator[opt] : constant-expression attributes[opt]
1994 __extension__ struct-declaration
1995 specifier-qualifier-list
1997 Unlike the ISO C syntax, semicolons are handled elsewhere. The use
1998 of attributes where shown is a GNU extension. In GNU C, we accept
1999 any expression without commas in the syntax (assignment
2000 expressions, not just conditional expressions); assignment
2001 expressions will be diagnosed as non-constant. */
2004 c_parser_struct_declaration (c_parser *parser)
2006 struct c_declspecs *specs;
2008 tree all_prefix_attrs;
2010 location_t decl_loc;
2011 if (c_parser_next_token_is_keyword (parser, RID_EXTENSION))
2015 ext = disable_extension_diagnostics ();
2016 c_parser_consume_token (parser);
2017 decl = c_parser_struct_declaration (parser);
2018 restore_extension_diagnostics (ext);
2021 specs = build_null_declspecs ();
2022 decl_loc = c_parser_peek_token (parser)->location;
2023 c_parser_declspecs (parser, specs, false, true, true);
2026 if (!specs->declspecs_seen_p)
2028 c_parser_error (parser, "expected specifier-qualifier-list");
2031 finish_declspecs (specs);
2032 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
2035 if (!specs->type_seen_p)
2037 pedwarn (decl_loc, OPT_pedantic,
2038 "ISO C forbids member declarations with no members");
2039 shadow_tag_warned (specs, pedantic);
2044 /* Support for unnamed structs or unions as members of
2045 structs or unions (which is [a] useful and [b] supports
2049 ret = grokfield (c_parser_peek_token (parser)->location,
2050 build_id_declarator (NULL_TREE), specs,
2053 decl_attributes (&ret, attrs, 0);
2057 pending_xref_error ();
2058 prefix_attrs = specs->attrs;
2059 all_prefix_attrs = prefix_attrs;
2060 specs->attrs = NULL_TREE;
2064 /* Declaring one or more declarators or un-named bit-fields. */
2065 struct c_declarator *declarator;
2067 if (c_parser_next_token_is (parser, CPP_COLON))
2068 declarator = build_id_declarator (NULL_TREE);
2070 declarator = c_parser_declarator (parser, specs->type_seen_p,
2071 C_DTR_NORMAL, &dummy);
2072 if (declarator == NULL)
2074 c_parser_skip_to_end_of_block_or_statement (parser);
2077 if (c_parser_next_token_is (parser, CPP_COLON)
2078 || c_parser_next_token_is (parser, CPP_COMMA)
2079 || c_parser_next_token_is (parser, CPP_SEMICOLON)
2080 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE)
2081 || c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2083 tree postfix_attrs = NULL_TREE;
2084 tree width = NULL_TREE;
2086 if (c_parser_next_token_is (parser, CPP_COLON))
2088 c_parser_consume_token (parser);
2089 width = c_parser_expr_no_commas (parser, NULL).value;
2091 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2092 postfix_attrs = c_parser_attributes (parser);
2093 d = grokfield (c_parser_peek_token (parser)->location,
2094 declarator, specs, width, &all_prefix_attrs);
2095 decl_attributes (&d, chainon (postfix_attrs,
2096 all_prefix_attrs), 0);
2097 TREE_CHAIN (d) = decls;
2099 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2100 all_prefix_attrs = chainon (c_parser_attributes (parser),
2103 all_prefix_attrs = prefix_attrs;
2104 if (c_parser_next_token_is (parser, CPP_COMMA))
2105 c_parser_consume_token (parser);
2106 else if (c_parser_next_token_is (parser, CPP_SEMICOLON)
2107 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2109 /* Semicolon consumed in caller. */
2114 c_parser_error (parser, "expected %<,%>, %<;%> or %<}%>");
2120 c_parser_error (parser,
2121 "expected %<:%>, %<,%>, %<;%>, %<}%> or "
2122 "%<__attribute__%>");
2129 /* Parse a typeof specifier (a GNU extension).
2132 typeof ( expression )
2133 typeof ( type-name )
2136 static struct c_typespec
2137 c_parser_typeof_specifier (c_parser *parser)
2139 struct c_typespec ret;
2140 ret.kind = ctsk_typeof;
2141 ret.spec = error_mark_node;
2142 ret.expr = NULL_TREE;
2143 ret.expr_const_operands = true;
2144 gcc_assert (c_parser_next_token_is_keyword (parser, RID_TYPEOF));
2145 c_parser_consume_token (parser);
2146 c_inhibit_evaluation_warnings++;
2148 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2150 c_inhibit_evaluation_warnings--;
2154 if (c_parser_next_token_starts_typename (parser))
2156 struct c_type_name *type = c_parser_type_name (parser);
2157 c_inhibit_evaluation_warnings--;
2161 ret.spec = groktypename (type, &ret.expr, &ret.expr_const_operands);
2162 pop_maybe_used (variably_modified_type_p (ret.spec, NULL_TREE));
2168 location_t here = c_parser_peek_token (parser)->location;
2169 struct c_expr expr = c_parser_expression (parser);
2170 c_inhibit_evaluation_warnings--;
2172 if (TREE_CODE (expr.value) == COMPONENT_REF
2173 && DECL_C_BIT_FIELD (TREE_OPERAND (expr.value, 1)))
2174 error_at (here, "%<typeof%> applied to a bit-field");
2175 mark_exp_read (expr.value);
2176 ret.spec = TREE_TYPE (expr.value);
2177 was_vm = variably_modified_type_p (ret.spec, NULL_TREE);
2178 /* This is returned with the type so that when the type is
2179 evaluated, this can be evaluated. */
2181 ret.expr = c_fully_fold (expr.value, false, &ret.expr_const_operands);
2182 pop_maybe_used (was_vm);
2184 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
2188 /* Parse a declarator, possibly an abstract declarator (C90 6.5.4,
2189 6.5.5, C99 6.7.5, 6.7.6). If TYPE_SEEN_P then a typedef name may
2190 be redeclared; otherwise it may not. KIND indicates which kind of
2191 declarator is wanted. Returns a valid declarator except in the
2192 case of a syntax error in which case NULL is returned. *SEEN_ID is
2193 set to true if an identifier being declared is seen; this is used
2194 to diagnose bad forms of abstract array declarators and to
2195 determine whether an identifier list is syntactically permitted.
2198 pointer[opt] direct-declarator
2202 ( attributes[opt] declarator )
2203 direct-declarator array-declarator
2204 direct-declarator ( parameter-type-list )
2205 direct-declarator ( identifier-list[opt] )
2208 * type-qualifier-list[opt]
2209 * type-qualifier-list[opt] pointer
2211 type-qualifier-list:
2214 type-qualifier-list type-qualifier
2215 type-qualifier-list attributes
2217 parameter-type-list:
2219 parameter-list , ...
2222 parameter-declaration
2223 parameter-list , parameter-declaration
2225 parameter-declaration:
2226 declaration-specifiers declarator attributes[opt]
2227 declaration-specifiers abstract-declarator[opt] attributes[opt]
2231 identifier-list , identifier
2233 abstract-declarator:
2235 pointer[opt] direct-abstract-declarator
2237 direct-abstract-declarator:
2238 ( attributes[opt] abstract-declarator )
2239 direct-abstract-declarator[opt] array-declarator
2240 direct-abstract-declarator[opt] ( parameter-type-list[opt] )
2245 direct-declarator ( parameter-forward-declarations
2246 parameter-type-list[opt] )
2248 direct-abstract-declarator:
2249 direct-abstract-declarator[opt] ( parameter-forward-declarations
2250 parameter-type-list[opt] )
2252 parameter-forward-declarations:
2254 parameter-forward-declarations parameter-list ;
2256 The uses of attributes shown above are GNU extensions.
2258 Some forms of array declarator are not included in C99 in the
2259 syntax for abstract declarators; these are disallowed elsewhere.
2260 This may be a defect (DR#289).
2262 This function also accepts an omitted abstract declarator as being
2263 an abstract declarator, although not part of the formal syntax. */
2265 static struct c_declarator *
2266 c_parser_declarator (c_parser *parser, bool type_seen_p, c_dtr_syn kind,
2269 /* Parse any initial pointer part. */
2270 if (c_parser_next_token_is (parser, CPP_MULT))
2272 struct c_declspecs *quals_attrs = build_null_declspecs ();
2273 struct c_declarator *inner;
2274 c_parser_consume_token (parser);
2275 c_parser_declspecs (parser, quals_attrs, false, false, true);
2276 inner = c_parser_declarator (parser, type_seen_p, kind, seen_id);
2280 return make_pointer_declarator (quals_attrs, inner);
2282 /* Now we have a direct declarator, direct abstract declarator or
2283 nothing (which counts as a direct abstract declarator here). */
2284 return c_parser_direct_declarator (parser, type_seen_p, kind, seen_id);
2287 /* Parse a direct declarator or direct abstract declarator; arguments
2288 as c_parser_declarator. */
2290 static struct c_declarator *
2291 c_parser_direct_declarator (c_parser *parser, bool type_seen_p, c_dtr_syn kind,
2294 /* The direct declarator must start with an identifier (possibly
2295 omitted) or a parenthesized declarator (possibly abstract). In
2296 an ordinary declarator, initial parentheses must start a
2297 parenthesized declarator. In an abstract declarator or parameter
2298 declarator, they could start a parenthesized declarator or a
2299 parameter list. To tell which, the open parenthesis and any
2300 following attributes must be read. If a declaration specifier
2301 follows, then it is a parameter list; if the specifier is a
2302 typedef name, there might be an ambiguity about redeclaring it,
2303 which is resolved in the direction of treating it as a typedef
2304 name. If a close parenthesis follows, it is also an empty
2305 parameter list, as the syntax does not permit empty abstract
2306 declarators. Otherwise, it is a parenthesized declarator (in
2307 which case the analysis may be repeated inside it, recursively).
2309 ??? There is an ambiguity in a parameter declaration "int
2310 (__attribute__((foo)) x)", where x is not a typedef name: it
2311 could be an abstract declarator for a function, or declare x with
2312 parentheses. The proper resolution of this ambiguity needs
2313 documenting. At present we follow an accident of the old
2314 parser's implementation, whereby the first parameter must have
2315 some declaration specifiers other than just attributes. Thus as
2316 a parameter declaration it is treated as a parenthesized
2317 parameter named x, and as an abstract declarator it is
2320 ??? Also following the old parser, attributes inside an empty
2321 parameter list are ignored, making it a list not yielding a
2322 prototype, rather than giving an error or making it have one
2323 parameter with implicit type int.
2325 ??? Also following the old parser, typedef names may be
2326 redeclared in declarators, but not Objective-C class names. */
2328 if (kind != C_DTR_ABSTRACT
2329 && c_parser_next_token_is (parser, CPP_NAME)
2331 && c_parser_peek_token (parser)->id_kind == C_ID_TYPENAME)
2332 || c_parser_peek_token (parser)->id_kind == C_ID_ID))
2334 struct c_declarator *inner
2335 = build_id_declarator (c_parser_peek_token (parser)->value);
2337 inner->id_loc = c_parser_peek_token (parser)->location;
2338 c_parser_consume_token (parser);
2339 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
2342 if (kind != C_DTR_NORMAL
2343 && c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
2345 struct c_declarator *inner = build_id_declarator (NULL_TREE);
2346 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
2349 /* Either we are at the end of an abstract declarator, or we have
2352 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
2355 struct c_declarator *inner;
2356 c_parser_consume_token (parser);
2357 attrs = c_parser_attributes (parser);
2358 if (kind != C_DTR_NORMAL
2359 && (c_parser_next_token_starts_declspecs (parser)
2360 || c_parser_next_token_is (parser, CPP_CLOSE_PAREN)))
2362 struct c_arg_info *args
2363 = c_parser_parms_declarator (parser, kind == C_DTR_NORMAL,
2370 = build_function_declarator (args,
2371 build_id_declarator (NULL_TREE));
2372 return c_parser_direct_declarator_inner (parser, *seen_id,
2376 /* A parenthesized declarator. */
2377 inner = c_parser_declarator (parser, type_seen_p, kind, seen_id);
2378 if (inner != NULL && attrs != NULL)
2379 inner = build_attrs_declarator (attrs, inner);
2380 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2382 c_parser_consume_token (parser);
2386 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
2390 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2397 if (kind == C_DTR_NORMAL)
2399 c_parser_error (parser, "expected identifier or %<(%>");
2403 return build_id_declarator (NULL_TREE);
2407 /* Parse part of a direct declarator or direct abstract declarator,
2408 given that some (in INNER) has already been parsed; ID_PRESENT is
2409 true if an identifier is present, false for an abstract
2412 static struct c_declarator *
2413 c_parser_direct_declarator_inner (c_parser *parser, bool id_present,
2414 struct c_declarator *inner)
2416 /* Parse a sequence of array declarators and parameter lists. */
2417 if (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
2419 location_t brace_loc = c_parser_peek_token (parser)->location;
2420 struct c_declarator *declarator;
2421 struct c_declspecs *quals_attrs = build_null_declspecs ();
2425 c_parser_consume_token (parser);
2426 c_parser_declspecs (parser, quals_attrs, false, false, true);
2427 static_seen = c_parser_next_token_is_keyword (parser, RID_STATIC);
2429 c_parser_consume_token (parser);
2430 if (static_seen && !quals_attrs->declspecs_seen_p)
2431 c_parser_declspecs (parser, quals_attrs, false, false, true);
2432 if (!quals_attrs->declspecs_seen_p)
2434 /* If "static" is present, there must be an array dimension.
2435 Otherwise, there may be a dimension, "*", or no
2440 dimen = c_parser_expr_no_commas (parser, NULL).value;
2444 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
2449 else if (c_parser_next_token_is (parser, CPP_MULT))
2451 if (c_parser_peek_2nd_token (parser)->type == CPP_CLOSE_SQUARE)
2455 c_parser_consume_token (parser);
2460 dimen = c_parser_expr_no_commas (parser, NULL).value;
2466 dimen = c_parser_expr_no_commas (parser, NULL).value;
2469 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
2470 c_parser_consume_token (parser);
2473 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
2478 mark_exp_read (dimen);
2479 declarator = build_array_declarator (brace_loc, dimen, quals_attrs,
2480 static_seen, star_seen);
2481 if (declarator == NULL)
2483 inner = set_array_declarator_inner (declarator, inner);
2484 return c_parser_direct_declarator_inner (parser, id_present, inner);
2486 else if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
2489 struct c_arg_info *args;
2490 c_parser_consume_token (parser);
2491 attrs = c_parser_attributes (parser);
2492 args = c_parser_parms_declarator (parser, id_present, attrs);
2497 inner = build_function_declarator (args, inner);
2498 return c_parser_direct_declarator_inner (parser, id_present, inner);
2504 /* Parse a parameter list or identifier list, including the closing
2505 parenthesis but not the opening one. ATTRS are the attributes at
2506 the start of the list. ID_LIST_OK is true if an identifier list is
2507 acceptable; such a list must not have attributes at the start. */
2509 static struct c_arg_info *
2510 c_parser_parms_declarator (c_parser *parser, bool id_list_ok, tree attrs)
2513 declare_parm_level ();
2514 /* If the list starts with an identifier, it is an identifier list.
2515 Otherwise, it is either a prototype list or an empty list. */
2518 && c_parser_next_token_is (parser, CPP_NAME)
2519 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
2521 tree list = NULL_TREE, *nextp = &list;
2522 while (c_parser_next_token_is (parser, CPP_NAME)
2523 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
2525 *nextp = build_tree_list (NULL_TREE,
2526 c_parser_peek_token (parser)->value);
2527 nextp = & TREE_CHAIN (*nextp);
2528 c_parser_consume_token (parser);
2529 if (c_parser_next_token_is_not (parser, CPP_COMMA))
2531 c_parser_consume_token (parser);
2532 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2534 c_parser_error (parser, "expected identifier");
2538 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2540 struct c_arg_info *ret = XOBNEW (&parser_obstack, struct c_arg_info);
2545 ret->pending_sizes = 0;
2546 ret->had_vla_unspec = 0;
2547 c_parser_consume_token (parser);
2553 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2561 struct c_arg_info *ret = c_parser_parms_list_declarator (parser, attrs);
2567 /* Parse a parameter list (possibly empty), including the closing
2568 parenthesis but not the opening one. ATTRS are the attributes at
2569 the start of the list. */
2571 static struct c_arg_info *
2572 c_parser_parms_list_declarator (c_parser *parser, tree attrs)
2574 bool good_parm = false;
2575 /* ??? Following the old parser, forward parameter declarations may
2576 use abstract declarators, and if no real parameter declarations
2577 follow the forward declarations then this is not diagnosed. Also
2578 note as above that attributes are ignored as the only contents of
2579 the parentheses, or as the only contents after forward
2581 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2583 struct c_arg_info *ret = XOBNEW (&parser_obstack, struct c_arg_info);
2588 ret->pending_sizes = 0;
2589 ret->had_vla_unspec = 0;
2590 c_parser_consume_token (parser);
2593 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
2595 struct c_arg_info *ret = XOBNEW (&parser_obstack, struct c_arg_info);
2599 ret->pending_sizes = 0;
2600 ret->had_vla_unspec = 0;
2601 /* Suppress -Wold-style-definition for this case. */
2602 ret->types = error_mark_node;
2603 error_at (c_parser_peek_token (parser)->location,
2604 "ISO C requires a named argument before %<...%>");
2605 c_parser_consume_token (parser);
2606 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2608 c_parser_consume_token (parser);
2613 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2618 /* Nonempty list of parameters, either terminated with semicolon
2619 (forward declarations; recurse) or with close parenthesis (normal
2620 function) or with ", ... )" (variadic function). */
2623 /* Parse a parameter. */
2624 struct c_parm *parm = c_parser_parameter_declaration (parser, attrs);
2629 push_parm_decl (parm);
2631 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
2634 c_parser_consume_token (parser);
2635 mark_forward_parm_decls ();
2636 new_attrs = c_parser_attributes (parser);
2637 return c_parser_parms_list_declarator (parser, new_attrs);
2639 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2641 c_parser_consume_token (parser);
2643 return get_parm_info (false);
2646 struct c_arg_info *ret
2647 = XOBNEW (&parser_obstack, struct c_arg_info);
2652 ret->pending_sizes = 0;
2653 ret->had_vla_unspec = 0;
2657 if (!c_parser_require (parser, CPP_COMMA,
2658 "expected %<;%>, %<,%> or %<)%>"))
2660 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
2661 get_pending_sizes ();
2664 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
2666 c_parser_consume_token (parser);
2667 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2669 c_parser_consume_token (parser);
2671 return get_parm_info (true);
2674 struct c_arg_info *ret
2675 = XOBNEW (&parser_obstack, struct c_arg_info);
2680 ret->pending_sizes = 0;
2681 ret->had_vla_unspec = 0;
2687 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2689 get_pending_sizes ();
2696 /* Parse a parameter declaration. ATTRS are the attributes at the
2697 start of the declaration if it is the first parameter. */
2699 static struct c_parm *
2700 c_parser_parameter_declaration (c_parser *parser, tree attrs)
2702 struct c_declspecs *specs;
2703 struct c_declarator *declarator;
2705 tree postfix_attrs = NULL_TREE;
2707 if (!c_parser_next_token_starts_declspecs (parser))
2709 /* ??? In some Objective-C cases '...' isn't applicable so there
2710 should be a different message. */
2711 c_parser_error (parser,
2712 "expected declaration specifiers or %<...%>");
2713 c_parser_skip_to_end_of_parameter (parser);
2716 specs = build_null_declspecs ();
2719 declspecs_add_attrs (specs, attrs);
2722 c_parser_declspecs (parser, specs, true, true, true);
2723 finish_declspecs (specs);
2724 pending_xref_error ();
2725 prefix_attrs = specs->attrs;
2726 specs->attrs = NULL_TREE;
2727 declarator = c_parser_declarator (parser, specs->type_seen_p,
2728 C_DTR_PARM, &dummy);
2729 if (declarator == NULL)
2731 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
2734 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2735 postfix_attrs = c_parser_attributes (parser);
2736 return build_c_parm (specs, chainon (postfix_attrs, prefix_attrs),
2740 /* Parse a string literal in an asm expression. It should not be
2741 translated, and wide string literals are an error although
2742 permitted by the syntax. This is a GNU extension.
2747 ??? At present, following the old parser, the caller needs to have
2748 set lex_untranslated_string to 1. It would be better to follow the
2749 C++ parser rather than using this kludge. */
2752 c_parser_asm_string_literal (c_parser *parser)
2755 if (c_parser_next_token_is (parser, CPP_STRING))
2757 str = c_parser_peek_token (parser)->value;
2758 c_parser_consume_token (parser);
2760 else if (c_parser_next_token_is (parser, CPP_WSTRING))
2762 error_at (c_parser_peek_token (parser)->location,
2763 "wide string literal in %<asm%>");
2764 str = build_string (1, "");
2765 c_parser_consume_token (parser);
2769 c_parser_error (parser, "expected string literal");
2775 /* Parse a simple asm expression. This is used in restricted
2776 contexts, where a full expression with inputs and outputs does not
2777 make sense. This is a GNU extension.
2780 asm ( asm-string-literal )
2784 c_parser_simple_asm_expr (c_parser *parser)
2787 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ASM));
2788 /* ??? Follow the C++ parser rather than using the
2789 lex_untranslated_string kludge. */
2790 parser->lex_untranslated_string = true;
2791 c_parser_consume_token (parser);
2792 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2794 parser->lex_untranslated_string = false;
2797 str = c_parser_asm_string_literal (parser);
2798 parser->lex_untranslated_string = false;
2799 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
2801 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
2807 /* Parse (possibly empty) attributes. This is a GNU extension.
2811 attributes attribute
2814 __attribute__ ( ( attribute-list ) )
2818 attribute_list , attrib
2823 any-word ( identifier )
2824 any-word ( identifier , nonempty-expr-list )
2825 any-word ( expr-list )
2827 where the "identifier" must not be declared as a type, and
2828 "any-word" may be any identifier (including one declared as a
2829 type), a reserved word storage class specifier, type specifier or
2830 type qualifier. ??? This still leaves out most reserved keywords
2831 (following the old parser), shouldn't we include them, and why not
2832 allow identifiers declared as types to start the arguments? */
2835 c_parser_attributes (c_parser *parser)
2837 tree attrs = NULL_TREE;
2838 while (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2840 /* ??? Follow the C++ parser rather than using the
2841 lex_untranslated_string kludge. */
2842 parser->lex_untranslated_string = true;
2843 c_parser_consume_token (parser);
2844 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2846 parser->lex_untranslated_string = false;
2849 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2851 parser->lex_untranslated_string = false;
2852 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
2855 /* Parse the attribute list. */
2856 while (c_parser_next_token_is (parser, CPP_COMMA)
2857 || c_parser_next_token_is (parser, CPP_NAME)
2858 || c_parser_next_token_is (parser, CPP_KEYWORD))
2860 tree attr, attr_name, attr_args;
2861 VEC(tree,gc) *expr_list;
2862 if (c_parser_next_token_is (parser, CPP_COMMA))
2864 c_parser_consume_token (parser);
2867 if (c_parser_next_token_is (parser, CPP_KEYWORD))
2869 /* ??? See comment above about what keywords are
2872 switch (c_parser_peek_token (parser)->keyword)
2909 /* Accept __attribute__((__const)) as __attribute__((const))
2912 = ridpointers[(int) c_parser_peek_token (parser)->keyword];
2915 attr_name = c_parser_peek_token (parser)->value;
2916 c_parser_consume_token (parser);
2917 if (c_parser_next_token_is_not (parser, CPP_OPEN_PAREN))
2919 attr = build_tree_list (attr_name, NULL_TREE);
2920 attrs = chainon (attrs, attr);
2923 c_parser_consume_token (parser);
2924 /* Parse the attribute contents. If they start with an
2925 identifier which is followed by a comma or close
2926 parenthesis, then the arguments start with that
2927 identifier; otherwise they are an expression list. */
2928 if (c_parser_next_token_is (parser, CPP_NAME)
2929 && c_parser_peek_token (parser)->id_kind == C_ID_ID
2930 && ((c_parser_peek_2nd_token (parser)->type == CPP_COMMA)
2931 || (c_parser_peek_2nd_token (parser)->type
2932 == CPP_CLOSE_PAREN)))
2934 tree arg1 = c_parser_peek_token (parser)->value;
2935 c_parser_consume_token (parser);
2936 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2937 attr_args = build_tree_list (NULL_TREE, arg1);
2941 c_parser_consume_token (parser);
2942 expr_list = c_parser_expr_list (parser, false, true, NULL);
2943 tree_list = build_tree_list_vec (expr_list);
2944 attr_args = tree_cons (NULL_TREE, arg1, tree_list);
2945 release_tree_vector (expr_list);
2950 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2951 attr_args = NULL_TREE;
2954 expr_list = c_parser_expr_list (parser, false, true, NULL);
2955 attr_args = build_tree_list_vec (expr_list);
2956 release_tree_vector (expr_list);
2959 attr = build_tree_list (attr_name, attr_args);
2960 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2961 c_parser_consume_token (parser);
2964 parser->lex_untranslated_string = false;
2965 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2969 attrs = chainon (attrs, attr);
2971 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2972 c_parser_consume_token (parser);
2975 parser->lex_untranslated_string = false;
2976 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2980 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2981 c_parser_consume_token (parser);
2984 parser->lex_untranslated_string = false;
2985 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2989 parser->lex_untranslated_string = false;
2994 /* Parse a type name (C90 6.5.5, C99 6.7.6).
2997 specifier-qualifier-list abstract-declarator[opt]
3000 static struct c_type_name *
3001 c_parser_type_name (c_parser *parser)
3003 struct c_declspecs *specs = build_null_declspecs ();
3004 struct c_declarator *declarator;
3005 struct c_type_name *ret;
3007 c_parser_declspecs (parser, specs, false, true, true);
3008 if (!specs->declspecs_seen_p)
3010 c_parser_error (parser, "expected specifier-qualifier-list");
3013 pending_xref_error ();
3014 finish_declspecs (specs);
3015 declarator = c_parser_declarator (parser, specs->type_seen_p,
3016 C_DTR_ABSTRACT, &dummy);
3017 if (declarator == NULL)
3019 ret = XOBNEW (&parser_obstack, struct c_type_name);
3021 ret->declarator = declarator;
3025 /* Parse an initializer (C90 6.5.7, C99 6.7.8).
3028 assignment-expression
3029 { initializer-list }
3030 { initializer-list , }
3033 designation[opt] initializer
3034 initializer-list , designation[opt] initializer
3041 designator-list designator
3048 [ constant-expression ]
3060 [ constant-expression ... constant-expression ]
3062 Any expression without commas is accepted in the syntax for the
3063 constant-expressions, with non-constant expressions rejected later.
3065 This function is only used for top-level initializers; for nested
3066 ones, see c_parser_initval. */
3068 static struct c_expr
3069 c_parser_initializer (c_parser *parser)
3071 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
3072 return c_parser_braced_init (parser, NULL_TREE, false);
3076 location_t loc = c_parser_peek_token (parser)->location;
3077 ret = c_parser_expr_no_commas (parser, NULL);
3078 if (TREE_CODE (ret.value) != STRING_CST
3079 && TREE_CODE (ret.value) != COMPOUND_LITERAL_EXPR)
3080 ret = default_function_array_read_conversion (loc, ret);
3085 /* Parse a braced initializer list. TYPE is the type specified for a
3086 compound literal, and NULL_TREE for other initializers and for
3087 nested braced lists. NESTED_P is true for nested braced lists,
3088 false for the list of a compound literal or the list that is the
3089 top-level initializer in a declaration. */
3091 static struct c_expr
3092 c_parser_braced_init (c_parser *parser, tree type, bool nested_p)
3095 struct obstack braced_init_obstack;
3096 location_t brace_loc = c_parser_peek_token (parser)->location;
3097 gcc_obstack_init (&braced_init_obstack);
3098 gcc_assert (c_parser_next_token_is (parser, CPP_OPEN_BRACE));
3099 c_parser_consume_token (parser);
3101 push_init_level (0, &braced_init_obstack);
3103 really_start_incremental_init (type);
3104 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3106 pedwarn (brace_loc, OPT_pedantic, "ISO C forbids empty initializer braces");
3110 /* Parse a non-empty initializer list, possibly with a trailing
3114 c_parser_initelt (parser, &braced_init_obstack);
3117 if (c_parser_next_token_is (parser, CPP_COMMA))
3118 c_parser_consume_token (parser);
3121 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3125 if (c_parser_next_token_is_not (parser, CPP_CLOSE_BRACE))
3127 ret.value = error_mark_node;
3128 ret.original_code = ERROR_MARK;
3129 ret.original_type = NULL;
3130 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, "expected %<}%>");
3131 pop_init_level (0, &braced_init_obstack);
3132 obstack_free (&braced_init_obstack, NULL);
3135 c_parser_consume_token (parser);
3136 ret = pop_init_level (0, &braced_init_obstack);
3137 obstack_free (&braced_init_obstack, NULL);
3141 /* Parse a nested initializer, including designators. */
3144 c_parser_initelt (c_parser *parser, struct obstack * braced_init_obstack)
3146 /* Parse any designator or designator list. A single array
3147 designator may have the subsequent "=" omitted in GNU C, but a
3148 longer list or a structure member designator may not. */
3149 if (c_parser_next_token_is (parser, CPP_NAME)
3150 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
3152 /* Old-style structure member designator. */
3153 set_init_label (c_parser_peek_token (parser)->value,
3154 braced_init_obstack);
3155 /* Use the colon as the error location. */
3156 pedwarn (c_parser_peek_2nd_token (parser)->location, OPT_pedantic,
3157 "obsolete use of designated initializer with %<:%>");
3158 c_parser_consume_token (parser);
3159 c_parser_consume_token (parser);
3163 /* des_seen is 0 if there have been no designators, 1 if there
3164 has been a single array designator and 2 otherwise. */
3166 /* Location of a designator. */
3167 location_t des_loc = UNKNOWN_LOCATION; /* Quiet warning. */
3168 while (c_parser_next_token_is (parser, CPP_OPEN_SQUARE)
3169 || c_parser_next_token_is (parser, CPP_DOT))
3171 int des_prev = des_seen;
3173 des_loc = c_parser_peek_token (parser)->location;
3176 if (c_parser_next_token_is (parser, CPP_DOT))
3179 c_parser_consume_token (parser);
3180 if (c_parser_next_token_is (parser, CPP_NAME))
3182 set_init_label (c_parser_peek_token (parser)->value,
3183 braced_init_obstack);
3184 c_parser_consume_token (parser);
3189 init.value = error_mark_node;
3190 init.original_code = ERROR_MARK;
3191 init.original_type = NULL;
3192 c_parser_error (parser, "expected identifier");
3193 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
3194 process_init_element (init, false, braced_init_obstack);
3201 location_t ellipsis_loc = UNKNOWN_LOCATION; /* Quiet warning. */
3202 /* ??? Following the old parser, [ objc-receiver
3203 objc-message-args ] is accepted as an initializer,
3204 being distinguished from a designator by what follows
3205 the first assignment expression inside the square
3206 brackets, but after a first array designator a
3207 subsequent square bracket is for Objective-C taken to
3208 start an expression, using the obsolete form of
3209 designated initializer without '=', rather than
3210 possibly being a second level of designation: in LALR
3211 terms, the '[' is shifted rather than reducing
3212 designator to designator-list. */
3213 if (des_prev == 1 && c_dialect_objc ())
3215 des_seen = des_prev;
3218 if (des_prev == 0 && c_dialect_objc ())
3220 /* This might be an array designator or an
3221 Objective-C message expression. If the former,
3222 continue parsing here; if the latter, parse the
3223 remainder of the initializer given the starting
3224 primary-expression. ??? It might make sense to
3225 distinguish when des_prev == 1 as well; see
3226 previous comment. */
3228 struct c_expr mexpr;
3229 c_parser_consume_token (parser);
3230 if (c_parser_peek_token (parser)->type == CPP_NAME
3231 && ((c_parser_peek_token (parser)->id_kind
3233 || (c_parser_peek_token (parser)->id_kind
3234 == C_ID_CLASSNAME)))
3236 /* Type name receiver. */
3237 tree id = c_parser_peek_token (parser)->value;
3238 c_parser_consume_token (parser);
3239 rec = objc_get_class_reference (id);
3240 goto parse_message_args;
3242 first = c_parser_expr_no_commas (parser, NULL).value;
3243 mark_exp_read (first);
3244 if (c_parser_next_token_is (parser, CPP_ELLIPSIS)
3245 || c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
3246 goto array_desig_after_first;
3247 /* Expression receiver. So far only one part
3248 without commas has been parsed; there might be
3249 more of the expression. */
3251 while (c_parser_next_token_is (parser, CPP_COMMA))
3254 location_t comma_loc, exp_loc;
3255 comma_loc = c_parser_peek_token (parser)->location;
3256 c_parser_consume_token (parser);
3257 exp_loc = c_parser_peek_token (parser)->location;
3258 next = c_parser_expr_no_commas (parser, NULL);
3259 next = default_function_array_read_conversion (exp_loc,
3261 rec = build_compound_expr (comma_loc, rec, next.value);
3264 /* Now parse the objc-message-args. */
3265 args = c_parser_objc_message_args (parser);
3266 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
3269 = objc_build_message_expr (build_tree_list (rec, args));
3270 mexpr.original_code = ERROR_MARK;
3271 mexpr.original_type = NULL;
3272 /* Now parse and process the remainder of the
3273 initializer, starting with this message
3274 expression as a primary-expression. */
3275 c_parser_initval (parser, &mexpr, braced_init_obstack);
3278 c_parser_consume_token (parser);
3279 first = c_parser_expr_no_commas (parser, NULL).value;
3280 mark_exp_read (first);
3281 array_desig_after_first:
3282 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
3284 ellipsis_loc = c_parser_peek_token (parser)->location;
3285 c_parser_consume_token (parser);
3286 second = c_parser_expr_no_commas (parser, NULL).value;
3287 mark_exp_read (second);
3291 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
3293 c_parser_consume_token (parser);
3294 set_init_index (first, second, braced_init_obstack);
3296 pedwarn (ellipsis_loc, OPT_pedantic,
3297 "ISO C forbids specifying range of elements to initialize");
3300 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
3306 if (c_parser_next_token_is (parser, CPP_EQ))
3309 pedwarn (des_loc, OPT_pedantic,
3310 "ISO C90 forbids specifying subobject to initialize");
3311 c_parser_consume_token (parser);
3316 pedwarn (c_parser_peek_token (parser)->location, OPT_pedantic,
3317 "obsolete use of designated initializer without %<=%>");
3321 init.value = error_mark_node;
3322 init.original_code = ERROR_MARK;
3323 init.original_type = NULL;
3324 c_parser_error (parser, "expected %<=%>");
3325 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
3326 process_init_element (init, false, braced_init_obstack);
3332 c_parser_initval (parser, NULL, braced_init_obstack);
3335 /* Parse a nested initializer; as c_parser_initializer but parses
3336 initializers within braced lists, after any designators have been
3337 applied. If AFTER is not NULL then it is an Objective-C message
3338 expression which is the primary-expression starting the
3342 c_parser_initval (c_parser *parser, struct c_expr *after,
3343 struct obstack * braced_init_obstack)
3346 gcc_assert (!after || c_dialect_objc ());
3347 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE) && !after)
3348 init = c_parser_braced_init (parser, NULL_TREE, true);
3351 location_t loc = c_parser_peek_token (parser)->location;
3352 init = c_parser_expr_no_commas (parser, after);
3353 if (init.value != NULL_TREE
3354 && TREE_CODE (init.value) != STRING_CST
3355 && TREE_CODE (init.value) != COMPOUND_LITERAL_EXPR)
3356 init = default_function_array_read_conversion (loc, init);
3358 process_init_element (init, false, braced_init_obstack);
3361 /* Parse a compound statement (possibly a function body) (C90 6.6.2,
3365 { block-item-list[opt] }
3366 { label-declarations block-item-list }
3370 block-item-list block-item
3382 { label-declarations block-item-list }
3385 __extension__ nested-declaration
3386 nested-function-definition
3390 label-declarations label-declaration
3393 __label__ identifier-list ;
3395 Allowing the mixing of declarations and code is new in C99. The
3396 GNU syntax also permits (not shown above) labels at the end of
3397 compound statements, which yield an error. We don't allow labels
3398 on declarations; this might seem like a natural extension, but
3399 there would be a conflict between attributes on the label and
3400 prefix attributes on the declaration. ??? The syntax follows the
3401 old parser in requiring something after label declarations.
3402 Although they are erroneous if the labels declared aren't defined,
3403 is it useful for the syntax to be this way?
3415 c_parser_compound_statement (c_parser *parser)
3418 location_t brace_loc;
3419 brace_loc = c_parser_peek_token (parser)->location;
3420 if (!c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
3422 /* Ensure a scope is entered and left anyway to avoid confusion
3423 if we have just prepared to enter a function body. */
3424 stmt = c_begin_compound_stmt (true);
3425 c_end_compound_stmt (brace_loc, stmt, true);
3426 return error_mark_node;
3428 stmt = c_begin_compound_stmt (true);
3429 c_parser_compound_statement_nostart (parser);
3430 return c_end_compound_stmt (brace_loc, stmt, true);
3433 /* Parse a compound statement except for the opening brace. This is
3434 used for parsing both compound statements and statement expressions
3435 (which follow different paths to handling the opening). */
3438 c_parser_compound_statement_nostart (c_parser *parser)
3440 bool last_stmt = false;
3441 bool last_label = false;
3442 bool save_valid_for_pragma = valid_location_for_stdc_pragma_p ();
3443 location_t label_loc = UNKNOWN_LOCATION; /* Quiet warning. */
3444 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3446 c_parser_consume_token (parser);
3449 mark_valid_location_for_stdc_pragma (true);
3450 if (c_parser_next_token_is_keyword (parser, RID_LABEL))
3452 /* Read zero or more forward-declarations for labels that nested
3453 functions can jump to. */
3454 mark_valid_location_for_stdc_pragma (false);
3455 while (c_parser_next_token_is_keyword (parser, RID_LABEL))
3457 label_loc = c_parser_peek_token (parser)->location;
3458 c_parser_consume_token (parser);
3459 /* Any identifiers, including those declared as type names,
3464 if (c_parser_next_token_is_not (parser, CPP_NAME))
3466 c_parser_error (parser, "expected identifier");
3470 = declare_label (c_parser_peek_token (parser)->value);
3471 C_DECLARED_LABEL_FLAG (label) = 1;
3472 add_stmt (build_stmt (label_loc, DECL_EXPR, label));
3473 c_parser_consume_token (parser);