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"
43 #include "tm.h" /* For rtl.h: needs enum reg_class. */
45 #include "langhooks.h"
49 #include "c-family/c-pragma.h"
55 #include "c-family/c-common.h"
62 /* Initialization routine for this file. */
67 /* The only initialization required is of the reserved word
73 /* Make sure RID_MAX hasn't grown past the 8 bits used to hold the keyword in
74 the c_token structure. */
75 gcc_assert (RID_MAX <= 255);
82 mask |= D_ASM | D_EXT;
86 if (!c_dialect_objc ())
87 mask |= D_OBJC | D_CXX_OBJC;
89 ridpointers = ggc_alloc_cleared_vec_tree ((int) RID_MAX);
90 for (i = 0; i < num_c_common_reswords; i++)
92 /* If a keyword is disabled, do not enter it into the table
93 and so create a canonical spelling that isn't a keyword. */
94 if (c_common_reswords[i].disable & mask)
97 && (c_common_reswords[i].disable & D_CXXWARN))
99 id = get_identifier (c_common_reswords[i].word);
100 C_SET_RID_CODE (id, RID_CXX_COMPAT_WARN);
101 C_IS_RESERVED_WORD (id) = 1;
106 id = get_identifier (c_common_reswords[i].word);
107 C_SET_RID_CODE (id, c_common_reswords[i].rid);
108 C_IS_RESERVED_WORD (id) = 1;
109 ridpointers [(int) c_common_reswords[i].rid] = id;
113 /* The C lexer intermediates between the lexer in cpplib and c-lex.c
114 and the C parser. Unlike the C++ lexer, the parser structure
115 stores the lexer information instead of using a separate structure.
116 Identifiers are separated into ordinary identifiers, type names,
117 keywords and some other Objective-C types of identifiers, and some
118 look-ahead is maintained.
120 ??? It might be a good idea to lex the whole file up front (as for
121 C++). It would then be possible to share more of the C and C++
122 lexer code, if desired. */
124 /* The following local token type is used. */
127 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
129 /* More information about the type of a CPP_NAME token. */
130 typedef enum c_id_kind {
131 /* An ordinary identifier. */
133 /* An identifier declared as a typedef name. */
135 /* An identifier declared as an Objective-C class name. */
137 /* An address space identifier. */
139 /* Not an identifier. */
143 /* A single C token after string literal concatenation and conversion
144 of preprocessing tokens to tokens. */
145 typedef struct GTY (()) c_token {
146 /* The kind of token. */
147 ENUM_BITFIELD (cpp_ttype) type : 8;
148 /* If this token is a CPP_NAME, this value indicates whether also
149 declared as some kind of type. Otherwise, it is C_ID_NONE. */
150 ENUM_BITFIELD (c_id_kind) id_kind : 8;
151 /* If this token is a keyword, this value indicates which keyword.
152 Otherwise, this value is RID_MAX. */
153 ENUM_BITFIELD (rid) keyword : 8;
154 /* If this token is a CPP_PRAGMA, this indicates the pragma that
155 was seen. Otherwise it is PRAGMA_NONE. */
156 ENUM_BITFIELD (pragma_kind) pragma_kind : 8;
157 /* The location at which this token was found. */
159 /* The value associated with this token, if any. */
163 /* A parser structure recording information about the state and
164 context of parsing. Includes lexer information with up to two
165 tokens of look-ahead; more are not needed for C. */
166 typedef struct GTY(()) c_parser {
167 /* The look-ahead tokens. */
169 /* How many look-ahead tokens are available (0, 1 or 2). */
171 /* True if a syntax error is being recovered from; false otherwise.
172 c_parser_error sets this flag. It should clear this flag when
173 enough tokens have been consumed to recover from the error. */
174 BOOL_BITFIELD error : 1;
175 /* True if we're processing a pragma, and shouldn't automatically
176 consume CPP_PRAGMA_EOL. */
177 BOOL_BITFIELD in_pragma : 1;
178 /* True if we're parsing the outermost block of an if statement. */
179 BOOL_BITFIELD in_if_block : 1;
180 /* True if we want to lex an untranslated string. */
181 BOOL_BITFIELD lex_untranslated_string : 1;
183 /* Objective-C specific parser/lexer information. */
185 /* True if we are in a context where the Objective-C "PQ" keywords
186 are considered keywords. */
187 BOOL_BITFIELD objc_pq_context : 1;
188 /* True if we are parsing a (potential) Objective-C foreach
189 statement. This is set to true after we parsed 'for (' and while
190 we wait for 'in' or ';' to decide if it's a standard C for loop or an
191 Objective-C foreach loop. */
192 BOOL_BITFIELD objc_could_be_foreach_context : 1;
193 /* The following flag is needed to contextualize Objective-C lexical
194 analysis. In some cases (e.g., 'int NSObject;'), it is
195 undesirable to bind an identifier to an Objective-C class, even
196 if a class with that name exists. */
197 BOOL_BITFIELD objc_need_raw_identifier : 1;
201 /* The actual parser and external interface. ??? Does this need to be
202 garbage-collected? */
204 static GTY (()) c_parser *the_parser;
206 /* Read in and lex a single token, storing it in *TOKEN. */
209 c_lex_one_token (c_parser *parser, c_token *token)
211 timevar_push (TV_LEX);
213 token->type = c_lex_with_flags (&token->value, &token->location, NULL,
214 (parser->lex_untranslated_string
215 ? C_LEX_STRING_NO_TRANSLATE : 0));
216 token->id_kind = C_ID_NONE;
217 token->keyword = RID_MAX;
218 token->pragma_kind = PRAGMA_NONE;
226 bool objc_force_identifier = parser->objc_need_raw_identifier;
227 if (c_dialect_objc ())
228 parser->objc_need_raw_identifier = false;
230 if (C_IS_RESERVED_WORD (token->value))
232 enum rid rid_code = C_RID_CODE (token->value);
234 if (rid_code == RID_CXX_COMPAT_WARN)
236 warning_at (token->location,
238 "identifier %qE conflicts with C++ keyword",
241 else if (rid_code >= RID_FIRST_ADDR_SPACE
242 && rid_code <= RID_LAST_ADDR_SPACE)
244 token->id_kind = C_ID_ADDRSPACE;
245 token->keyword = rid_code;
248 else if (c_dialect_objc () && OBJC_IS_PQ_KEYWORD (rid_code))
250 /* We found an Objective-C "pq" keyword (in, out,
251 inout, bycopy, byref, oneway). They need special
252 care because the interpretation depends on the
255 if (parser->objc_pq_context)
257 token->type = CPP_KEYWORD;
258 token->keyword = rid_code;
261 else if (parser->objc_could_be_foreach_context
262 && rid_code == RID_IN)
264 /* We are in Objective-C, inside a (potential)
265 foreach context (which means after having
266 parsed 'for (', but before having parsed ';'),
267 and we found 'in'. We consider it the keyword
268 which terminates the declaration at the
269 beginning of a foreach-statement. Note that
270 this means you can't use 'in' for anything else
271 in that context; in particular, in Objective-C
272 you can't use 'in' as the name of the running
273 variable in a C for loop. We could potentially
274 try to add code here to disambiguate, but it
275 seems a reasonable limitation.
277 token->type = CPP_KEYWORD;
278 token->keyword = rid_code;
281 /* Else, "pq" keywords outside of the "pq" context are
282 not keywords, and we fall through to the code for
286 else if (c_dialect_objc ()
287 && (OBJC_IS_AT_KEYWORD (rid_code)
288 || OBJC_IS_CXX_KEYWORD (rid_code)))
290 /* We found one of the Objective-C "@" keywords (defs,
291 selector, synchronized, etc) or one of the
292 Objective-C "cxx" keywords (class, private,
293 protected, public, try, catch, throw) without a
294 preceding '@' sign. Do nothing and fall through to
295 the code for normal tokens (in C++ we would still
296 consider the CXX ones keywords, but not in C).
302 token->type = CPP_KEYWORD;
303 token->keyword = rid_code;
308 decl = lookup_name (token->value);
311 if (TREE_CODE (decl) == TYPE_DECL)
313 token->id_kind = C_ID_TYPENAME;
317 else if (c_dialect_objc ())
319 tree objc_interface_decl = objc_is_class_name (token->value);
320 /* Objective-C class names are in the same namespace as
321 variables and typedefs, and hence are shadowed by local
323 if (objc_interface_decl
324 && (global_bindings_p ()
325 || (!objc_force_identifier && !decl)))
327 token->value = objc_interface_decl;
328 token->id_kind = C_ID_CLASSNAME;
332 token->id_kind = C_ID_ID;
336 /* This only happens in Objective-C; it must be a keyword. */
337 token->type = CPP_KEYWORD;
338 switch (C_RID_CODE (token->value))
340 /* Replace 'class' with '@class', 'private' with '@private',
341 etc. This prevents confusion with the C++ keyword
342 'class', and makes the tokens consistent with other
343 Objective-C 'AT' keywords. For example '@class' is
344 reported as RID_AT_CLASS which is consistent with
345 '@synchronized', which is reported as
348 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
349 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
350 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
351 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
352 case RID_THROW: token->keyword = RID_AT_THROW; break;
353 case RID_TRY: token->keyword = RID_AT_TRY; break;
354 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
355 default: token->keyword = C_RID_CODE (token->value);
360 case CPP_CLOSE_PAREN:
362 /* These tokens may affect the interpretation of any identifiers
363 following, if doing Objective-C. */
364 if (c_dialect_objc ())
365 parser->objc_need_raw_identifier = false;
368 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
369 token->pragma_kind = (enum pragma_kind) TREE_INT_CST_LOW (token->value);
375 timevar_pop (TV_LEX);
378 /* Return a pointer to the next token from PARSER, reading it in if
381 static inline c_token *
382 c_parser_peek_token (c_parser *parser)
384 if (parser->tokens_avail == 0)
386 c_lex_one_token (parser, &parser->tokens[0]);
387 parser->tokens_avail = 1;
389 return &parser->tokens[0];
392 /* Return true if the next token from PARSER has the indicated
396 c_parser_next_token_is (c_parser *parser, enum cpp_ttype type)
398 return c_parser_peek_token (parser)->type == type;
401 /* Return true if the next token from PARSER does not have the
405 c_parser_next_token_is_not (c_parser *parser, enum cpp_ttype type)
407 return !c_parser_next_token_is (parser, type);
410 /* Return true if the next token from PARSER is the indicated
414 c_parser_next_token_is_keyword (c_parser *parser, enum rid keyword)
416 return c_parser_peek_token (parser)->keyword == keyword;
419 /* Return true if TOKEN can start a type name,
422 c_token_starts_typename (c_token *token)
427 switch (token->id_kind)
436 gcc_assert (c_dialect_objc ());
442 switch (token->keyword)
475 if (c_dialect_objc ())
483 /* Return true if the next token from PARSER can start a type name,
486 c_parser_next_token_starts_typename (c_parser *parser)
488 c_token *token = c_parser_peek_token (parser);
489 return c_token_starts_typename (token);
492 /* Return true if TOKEN can start declaration specifiers, false
495 c_token_starts_declspecs (c_token *token)
500 switch (token->id_kind)
509 gcc_assert (c_dialect_objc ());
515 switch (token->keyword)
555 if (c_dialect_objc ())
564 /* Return true if TOKEN can start declaration specifiers or a static
565 assertion, false otherwise. */
567 c_token_starts_declaration (c_token *token)
569 if (c_token_starts_declspecs (token)
570 || token->keyword == RID_STATIC_ASSERT)
576 /* Return true if the next token from PARSER can start declaration
577 specifiers, false otherwise. */
579 c_parser_next_token_starts_declspecs (c_parser *parser)
581 c_token *token = c_parser_peek_token (parser);
582 return c_token_starts_declspecs (token);
585 /* Return true if the next token from PARSER can start declaration
586 specifiers or a static assertion, false otherwise. */
588 c_parser_next_token_starts_declaration (c_parser *parser)
590 c_token *token = c_parser_peek_token (parser);
591 return c_token_starts_declaration (token);
594 /* Return a pointer to the next-but-one token from PARSER, reading it
595 in if necessary. The next token is already read in. */
598 c_parser_peek_2nd_token (c_parser *parser)
600 if (parser->tokens_avail >= 2)
601 return &parser->tokens[1];
602 gcc_assert (parser->tokens_avail == 1);
603 gcc_assert (parser->tokens[0].type != CPP_EOF);
604 gcc_assert (parser->tokens[0].type != CPP_PRAGMA_EOL);
605 c_lex_one_token (parser, &parser->tokens[1]);
606 parser->tokens_avail = 2;
607 return &parser->tokens[1];
610 /* Consume the next token from PARSER. */
613 c_parser_consume_token (c_parser *parser)
615 gcc_assert (parser->tokens_avail >= 1);
616 gcc_assert (parser->tokens[0].type != CPP_EOF);
617 gcc_assert (!parser->in_pragma || parser->tokens[0].type != CPP_PRAGMA_EOL);
618 gcc_assert (parser->error || parser->tokens[0].type != CPP_PRAGMA);
619 if (parser->tokens_avail == 2)
620 parser->tokens[0] = parser->tokens[1];
621 parser->tokens_avail--;
624 /* Expect the current token to be a #pragma. Consume it and remember
625 that we've begun parsing a pragma. */
628 c_parser_consume_pragma (c_parser *parser)
630 gcc_assert (!parser->in_pragma);
631 gcc_assert (parser->tokens_avail >= 1);
632 gcc_assert (parser->tokens[0].type == CPP_PRAGMA);
633 if (parser->tokens_avail == 2)
634 parser->tokens[0] = parser->tokens[1];
635 parser->tokens_avail--;
636 parser->in_pragma = true;
639 /* Update the globals input_location and in_system_header from
642 c_parser_set_source_position_from_token (c_token *token)
644 if (token->type != CPP_EOF)
646 input_location = token->location;
650 /* Issue a diagnostic of the form
651 FILE:LINE: MESSAGE before TOKEN
652 where TOKEN is the next token in the input stream of PARSER.
653 MESSAGE (specified by the caller) is usually of the form "expected
656 Do not issue a diagnostic if still recovering from an error.
658 ??? This is taken from the C++ parser, but building up messages in
659 this way is not i18n-friendly and some other approach should be
663 c_parser_error (c_parser *parser, const char *gmsgid)
665 c_token *token = c_parser_peek_token (parser);
668 parser->error = true;
671 /* This diagnostic makes more sense if it is tagged to the line of
672 the token we just peeked at. */
673 c_parser_set_source_position_from_token (token);
674 c_parse_error (gmsgid,
675 /* Because c_parse_error does not understand
676 CPP_KEYWORD, keywords are treated like
678 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
679 /* ??? The C parser does not save the cpp flags of a
680 token, we need to pass 0 here and we will not get
681 the source spelling of some tokens but rather the
682 canonical spelling. */
683 token->value, /*flags=*/0);
686 /* If the next token is of the indicated TYPE, consume it. Otherwise,
687 issue the error MSGID. If MSGID is NULL then a message has already
688 been produced and no message will be produced this time. Returns
689 true if found, false otherwise. */
692 c_parser_require (c_parser *parser,
696 if (c_parser_next_token_is (parser, type))
698 c_parser_consume_token (parser);
703 c_parser_error (parser, msgid);
708 /* If the next token is the indicated keyword, consume it. Otherwise,
709 issue the error MSGID. Returns true if found, false otherwise. */
712 c_parser_require_keyword (c_parser *parser,
716 if (c_parser_next_token_is_keyword (parser, keyword))
718 c_parser_consume_token (parser);
723 c_parser_error (parser, msgid);
728 /* Like c_parser_require, except that tokens will be skipped until the
729 desired token is found. An error message is still produced if the
730 next token is not as expected. If MSGID is NULL then a message has
731 already been produced and no message will be produced this
735 c_parser_skip_until_found (c_parser *parser,
739 unsigned nesting_depth = 0;
741 if (c_parser_require (parser, type, msgid))
744 /* Skip tokens until the desired token is found. */
747 /* Peek at the next token. */
748 c_token *token = c_parser_peek_token (parser);
749 /* If we've reached the token we want, consume it and stop. */
750 if (token->type == type && !nesting_depth)
752 c_parser_consume_token (parser);
756 /* If we've run out of tokens, stop. */
757 if (token->type == CPP_EOF)
759 if (token->type == CPP_PRAGMA_EOL && parser->in_pragma)
761 if (token->type == CPP_OPEN_BRACE
762 || token->type == CPP_OPEN_PAREN
763 || token->type == CPP_OPEN_SQUARE)
765 else if (token->type == CPP_CLOSE_BRACE
766 || token->type == CPP_CLOSE_PAREN
767 || token->type == CPP_CLOSE_SQUARE)
769 if (nesting_depth-- == 0)
772 /* Consume this token. */
773 c_parser_consume_token (parser);
775 parser->error = false;
778 /* Skip tokens until the end of a parameter is found, but do not
779 consume the comma, semicolon or closing delimiter. */
782 c_parser_skip_to_end_of_parameter (c_parser *parser)
784 unsigned nesting_depth = 0;
788 c_token *token = c_parser_peek_token (parser);
789 if ((token->type == CPP_COMMA || token->type == CPP_SEMICOLON)
792 /* If we've run out of tokens, stop. */
793 if (token->type == CPP_EOF)
795 if (token->type == CPP_PRAGMA_EOL && parser->in_pragma)
797 if (token->type == CPP_OPEN_BRACE
798 || token->type == CPP_OPEN_PAREN
799 || token->type == CPP_OPEN_SQUARE)
801 else if (token->type == CPP_CLOSE_BRACE
802 || token->type == CPP_CLOSE_PAREN
803 || token->type == CPP_CLOSE_SQUARE)
805 if (nesting_depth-- == 0)
808 /* Consume this token. */
809 c_parser_consume_token (parser);
811 parser->error = false;
814 /* Expect to be at the end of the pragma directive and consume an
815 end of line marker. */
818 c_parser_skip_to_pragma_eol (c_parser *parser)
820 gcc_assert (parser->in_pragma);
821 parser->in_pragma = false;
823 if (!c_parser_require (parser, CPP_PRAGMA_EOL, "expected end of line"))
826 c_token *token = c_parser_peek_token (parser);
827 if (token->type == CPP_EOF)
829 if (token->type == CPP_PRAGMA_EOL)
831 c_parser_consume_token (parser);
834 c_parser_consume_token (parser);
837 parser->error = false;
840 /* Skip tokens until we have consumed an entire block, or until we
841 have consumed a non-nested ';'. */
844 c_parser_skip_to_end_of_block_or_statement (c_parser *parser)
846 unsigned nesting_depth = 0;
847 bool save_error = parser->error;
853 /* Peek at the next token. */
854 token = c_parser_peek_token (parser);
862 if (parser->in_pragma)
867 /* If the next token is a ';', we have reached the
868 end of the statement. */
871 /* Consume the ';'. */
872 c_parser_consume_token (parser);
877 case CPP_CLOSE_BRACE:
878 /* If the next token is a non-nested '}', then we have
879 reached the end of the current block. */
880 if (nesting_depth == 0 || --nesting_depth == 0)
882 c_parser_consume_token (parser);
888 /* If it the next token is a '{', then we are entering a new
889 block. Consume the entire block. */
894 /* If we see a pragma, consume the whole thing at once. We
895 have some safeguards against consuming pragmas willy-nilly.
896 Normally, we'd expect to be here with parser->error set,
897 which disables these safeguards. But it's possible to get
898 here for secondary error recovery, after parser->error has
900 c_parser_consume_pragma (parser);
901 c_parser_skip_to_pragma_eol (parser);
902 parser->error = save_error;
909 c_parser_consume_token (parser);
913 parser->error = false;
916 /* CPP's options (initialized by c-opts.c). */
917 extern cpp_options *cpp_opts;
919 /* Save the warning flags which are controlled by __extension__. */
922 disable_extension_diagnostics (void)
925 | (warn_pointer_arith << 1)
926 | (warn_traditional << 2)
928 | (warn_long_long << 4)
929 | (warn_cxx_compat << 5));
930 cpp_opts->cpp_pedantic = pedantic = 0;
931 warn_pointer_arith = 0;
932 cpp_opts->cpp_warn_traditional = warn_traditional = 0;
934 cpp_opts->cpp_warn_long_long = warn_long_long = 0;
939 /* Restore the warning flags which are controlled by __extension__.
940 FLAGS is the return value from disable_extension_diagnostics. */
943 restore_extension_diagnostics (int flags)
945 cpp_opts->cpp_pedantic = pedantic = flags & 1;
946 warn_pointer_arith = (flags >> 1) & 1;
947 cpp_opts->cpp_warn_traditional = warn_traditional = (flags >> 2) & 1;
948 flag_iso = (flags >> 3) & 1;
949 cpp_opts->cpp_warn_long_long = warn_long_long = (flags >> 4) & 1;
950 warn_cxx_compat = (flags >> 5) & 1;
953 /* Possibly kinds of declarator to parse. */
954 typedef enum c_dtr_syn {
955 /* A normal declarator with an identifier. */
957 /* An abstract declarator (maybe empty). */
959 /* A parameter declarator: may be either, but after a type name does
960 not redeclare a typedef name as an identifier if it can
961 alternatively be interpreted as a typedef name; see DR#009,
962 applied in C90 TC1, omitted from C99 and reapplied in C99 TC2
963 following DR#249. For example, given a typedef T, "int T" and
964 "int *T" are valid parameter declarations redeclaring T, while
965 "int (T)" and "int * (T)" and "int (T[])" and "int (T (int))" are
966 abstract declarators rather than involving redundant parentheses;
967 the same applies with attributes inside the parentheses before
972 static void c_parser_external_declaration (c_parser *);
973 static void c_parser_asm_definition (c_parser *);
974 static void c_parser_declaration_or_fndef (c_parser *, bool, bool, bool,
976 static void c_parser_static_assert_declaration_no_semi (c_parser *);
977 static void c_parser_static_assert_declaration (c_parser *);
978 static void c_parser_declspecs (c_parser *, struct c_declspecs *, bool, bool,
980 static struct c_typespec c_parser_enum_specifier (c_parser *);
981 static struct c_typespec c_parser_struct_or_union_specifier (c_parser *);
982 static tree c_parser_struct_declaration (c_parser *);
983 static struct c_typespec c_parser_typeof_specifier (c_parser *);
984 static struct c_declarator *c_parser_declarator (c_parser *, bool, c_dtr_syn,
986 static struct c_declarator *c_parser_direct_declarator (c_parser *, bool,
988 static struct c_declarator *c_parser_direct_declarator_inner (c_parser *,
990 struct c_declarator *);
991 static struct c_arg_info *c_parser_parms_declarator (c_parser *, bool, tree);
992 static struct c_arg_info *c_parser_parms_list_declarator (c_parser *, tree);
993 static struct c_parm *c_parser_parameter_declaration (c_parser *, tree);
994 static tree c_parser_simple_asm_expr (c_parser *);
995 static tree c_parser_attributes (c_parser *);
996 static struct c_type_name *c_parser_type_name (c_parser *);
997 static struct c_expr c_parser_initializer (c_parser *);
998 static struct c_expr c_parser_braced_init (c_parser *, tree, bool);
999 static void c_parser_initelt (c_parser *, struct obstack *);
1000 static void c_parser_initval (c_parser *, struct c_expr *,
1002 static tree c_parser_compound_statement (c_parser *);
1003 static void c_parser_compound_statement_nostart (c_parser *);
1004 static void c_parser_label (c_parser *);
1005 static void c_parser_statement (c_parser *);
1006 static void c_parser_statement_after_labels (c_parser *);
1007 static void c_parser_if_statement (c_parser *);
1008 static void c_parser_switch_statement (c_parser *);
1009 static void c_parser_while_statement (c_parser *);
1010 static void c_parser_do_statement (c_parser *);
1011 static void c_parser_for_statement (c_parser *);
1012 static tree c_parser_asm_statement (c_parser *);
1013 static tree c_parser_asm_operands (c_parser *, bool);
1014 static tree c_parser_asm_goto_operands (c_parser *);
1015 static tree c_parser_asm_clobbers (c_parser *);
1016 static struct c_expr c_parser_expr_no_commas (c_parser *, struct c_expr *);
1017 static struct c_expr c_parser_conditional_expression (c_parser *,
1019 static struct c_expr c_parser_binary_expression (c_parser *, struct c_expr *);
1020 static struct c_expr c_parser_cast_expression (c_parser *, struct c_expr *);
1021 static struct c_expr c_parser_unary_expression (c_parser *);
1022 static struct c_expr c_parser_sizeof_expression (c_parser *);
1023 static struct c_expr c_parser_alignof_expression (c_parser *);
1024 static struct c_expr c_parser_postfix_expression (c_parser *);
1025 static struct c_expr c_parser_postfix_expression_after_paren_type (c_parser *,
1026 struct c_type_name *,
1028 static struct c_expr c_parser_postfix_expression_after_primary (c_parser *,
1031 static struct c_expr c_parser_expression (c_parser *);
1032 static struct c_expr c_parser_expression_conv (c_parser *);
1033 static VEC(tree,gc) *c_parser_expr_list (c_parser *, bool, bool,
1035 static void c_parser_omp_construct (c_parser *);
1036 static void c_parser_omp_threadprivate (c_parser *);
1037 static void c_parser_omp_barrier (c_parser *);
1038 static void c_parser_omp_flush (c_parser *);
1039 static void c_parser_omp_taskwait (c_parser *);
1041 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
1042 static bool c_parser_pragma (c_parser *, enum pragma_context);
1044 /* These Objective-C parser functions are only ever called when
1045 compiling Objective-C. */
1046 static void c_parser_objc_class_definition (c_parser *, tree);
1047 static void c_parser_objc_class_instance_variables (c_parser *);
1048 static void c_parser_objc_class_declaration (c_parser *);
1049 static void c_parser_objc_alias_declaration (c_parser *);
1050 static void c_parser_objc_protocol_definition (c_parser *, tree);
1051 static enum tree_code c_parser_objc_method_type (c_parser *);
1052 static void c_parser_objc_method_definition (c_parser *);
1053 static void c_parser_objc_methodprotolist (c_parser *);
1054 static void c_parser_objc_methodproto (c_parser *);
1055 static tree c_parser_objc_method_decl (c_parser *, tree *);
1056 static tree c_parser_objc_type_name (c_parser *);
1057 static tree c_parser_objc_protocol_refs (c_parser *);
1058 static void c_parser_objc_try_catch_statement (c_parser *);
1059 static void c_parser_objc_synchronized_statement (c_parser *);
1060 static tree c_parser_objc_selector (c_parser *);
1061 static tree c_parser_objc_selector_arg (c_parser *);
1062 static tree c_parser_objc_receiver (c_parser *);
1063 static tree c_parser_objc_message_args (c_parser *);
1064 static tree c_parser_objc_keywordexpr (c_parser *);
1065 static bool c_parser_objc_diagnose_bad_element_prefix
1066 (c_parser *, struct c_declspecs *);
1068 /* Parse a translation unit (C90 6.7, C99 6.9).
1071 external-declarations
1073 external-declarations:
1074 external-declaration
1075 external-declarations external-declaration
1084 c_parser_translation_unit (c_parser *parser)
1086 if (c_parser_next_token_is (parser, CPP_EOF))
1088 pedwarn (c_parser_peek_token (parser)->location, OPT_pedantic,
1089 "ISO C forbids an empty translation unit");
1093 void *obstack_position = obstack_alloc (&parser_obstack, 0);
1094 mark_valid_location_for_stdc_pragma (false);
1098 c_parser_external_declaration (parser);
1099 obstack_free (&parser_obstack, obstack_position);
1101 while (c_parser_next_token_is_not (parser, CPP_EOF));
1105 /* Parse an external declaration (C90 6.7, C99 6.9).
1107 external-declaration:
1113 external-declaration:
1116 __extension__ external-declaration
1120 external-declaration:
1121 objc-class-definition
1122 objc-class-declaration
1123 objc-alias-declaration
1124 objc-protocol-definition
1125 objc-method-definition
1130 c_parser_external_declaration (c_parser *parser)
1133 switch (c_parser_peek_token (parser)->type)
1136 switch (c_parser_peek_token (parser)->keyword)
1139 ext = disable_extension_diagnostics ();
1140 c_parser_consume_token (parser);
1141 c_parser_external_declaration (parser);
1142 restore_extension_diagnostics (ext);
1145 c_parser_asm_definition (parser);
1147 case RID_AT_INTERFACE:
1148 case RID_AT_IMPLEMENTATION:
1149 gcc_assert (c_dialect_objc ());
1150 c_parser_objc_class_definition (parser, NULL_TREE);
1153 gcc_assert (c_dialect_objc ());
1154 c_parser_objc_class_declaration (parser);
1157 gcc_assert (c_dialect_objc ());
1158 c_parser_objc_alias_declaration (parser);
1160 case RID_AT_PROTOCOL:
1161 gcc_assert (c_dialect_objc ());
1162 c_parser_objc_protocol_definition (parser, NULL_TREE);
1165 gcc_assert (c_dialect_objc ());
1166 c_parser_consume_token (parser);
1167 objc_finish_implementation ();
1174 pedwarn (c_parser_peek_token (parser)->location, OPT_pedantic,
1175 "ISO C does not allow extra %<;%> outside of a function");
1176 c_parser_consume_token (parser);
1179 mark_valid_location_for_stdc_pragma (true);
1180 c_parser_pragma (parser, pragma_external);
1181 mark_valid_location_for_stdc_pragma (false);
1185 if (c_dialect_objc ())
1187 c_parser_objc_method_definition (parser);
1190 /* Else fall through, and yield a syntax error trying to parse
1191 as a declaration or function definition. */
1194 /* A declaration or a function definition (or, in Objective-C,
1195 an @interface or @protocol with prefix attributes). We can
1196 only tell which after parsing the declaration specifiers, if
1197 any, and the first declarator. */
1198 c_parser_declaration_or_fndef (parser, true, true, true, false, true, NULL);
1203 /* Parse a declaration or function definition (C90 6.5, 6.7.1, C99
1204 6.7, 6.9.1). If FNDEF_OK is true, a function definition is
1205 accepted; otherwise (old-style parameter declarations) only other
1206 declarations are accepted. If STATIC_ASSERT_OK is true, a static
1207 assertion is accepted; otherwise (old-style parameter declarations)
1208 it is not. If NESTED is true, we are inside a function or parsing
1209 old-style parameter declarations; any functions encountered are
1210 nested functions and declaration specifiers are required; otherwise
1211 we are at top level and functions are normal functions and
1212 declaration specifiers may be optional. If EMPTY_OK is true, empty
1213 declarations are OK (subject to all other constraints); otherwise
1214 (old-style parameter declarations) they are diagnosed. If
1215 START_ATTR_OK is true, the declaration specifiers may start with
1216 attributes; otherwise they may not.
1217 OBJC_FOREACH_OBJECT_DECLARATION can be used to get back the parsed
1218 declaration when parsing an Objective-C foreach statement.
1221 declaration-specifiers init-declarator-list[opt] ;
1222 static_assert-declaration
1224 function-definition:
1225 declaration-specifiers[opt] declarator declaration-list[opt]
1230 declaration-list declaration
1232 init-declarator-list:
1234 init-declarator-list , init-declarator
1237 declarator simple-asm-expr[opt] attributes[opt]
1238 declarator simple-asm-expr[opt] attributes[opt] = initializer
1242 nested-function-definition:
1243 declaration-specifiers declarator declaration-list[opt]
1247 attributes objc-class-definition
1248 attributes objc-category-definition
1249 attributes objc-protocol-definition
1251 The simple-asm-expr and attributes are GNU extensions.
1253 This function does not handle __extension__; that is handled in its
1254 callers. ??? Following the old parser, __extension__ may start
1255 external declarations, declarations in functions and declarations
1256 at the start of "for" loops, but not old-style parameter
1259 C99 requires declaration specifiers in a function definition; the
1260 absence is diagnosed through the diagnosis of implicit int. In GNU
1261 C we also allow but diagnose declarations without declaration
1262 specifiers, but only at top level (elsewhere they conflict with
1265 In Objective-C, declarations of the looping variable in a foreach
1266 statement are exceptionally terminated by 'in' (for example, 'for
1267 (NSObject *object in array) { ... }').
1272 threadprivate-directive */
1275 c_parser_declaration_or_fndef (c_parser *parser, bool fndef_ok,
1276 bool static_assert_ok, bool empty_ok,
1277 bool nested, bool start_attr_ok,
1278 tree *objc_foreach_object_declaration)
1280 struct c_declspecs *specs;
1282 tree all_prefix_attrs;
1283 bool diagnosed_no_specs = false;
1284 location_t here = c_parser_peek_token (parser)->location;
1286 if (static_assert_ok
1287 && c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT))
1289 c_parser_static_assert_declaration (parser);
1292 specs = build_null_declspecs ();
1293 c_parser_declspecs (parser, specs, true, true, start_attr_ok);
1296 c_parser_skip_to_end_of_block_or_statement (parser);
1299 if (nested && !specs->declspecs_seen_p)
1301 c_parser_error (parser, "expected declaration specifiers");
1302 c_parser_skip_to_end_of_block_or_statement (parser);
1305 finish_declspecs (specs);
1306 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1312 shadow_tag_warned (specs, 1);
1313 pedwarn (here, 0, "empty declaration");
1315 c_parser_consume_token (parser);
1318 else if (c_dialect_objc ())
1320 /* Prefix attributes are an error on method decls. */
1321 switch (c_parser_peek_token (parser)->type)
1325 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1329 warning_at (c_parser_peek_token (parser)->location,
1331 "prefix attributes are ignored for methods");
1332 specs->attrs = NULL_TREE;
1335 c_parser_objc_method_definition (parser);
1337 c_parser_objc_methodproto (parser);
1343 /* This is where we parse 'attributes @interface ...',
1344 'attributes @implementation ...', 'attributes @protocol ...'
1345 (where attributes could be, for example, __attribute__
1348 switch (c_parser_peek_token (parser)->keyword)
1350 case RID_AT_INTERFACE:
1352 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1354 c_parser_objc_class_definition (parser, specs->attrs);
1358 case RID_AT_IMPLEMENTATION:
1360 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1364 warning_at (c_parser_peek_token (parser)->location,
1366 "prefix attributes are ignored for implementations");
1367 specs->attrs = NULL_TREE;
1369 c_parser_objc_class_definition (parser, NULL_TREE);
1373 case RID_AT_PROTOCOL:
1375 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1377 c_parser_objc_protocol_definition (parser, specs->attrs);
1386 pending_xref_error ();
1387 prefix_attrs = specs->attrs;
1388 all_prefix_attrs = prefix_attrs;
1389 specs->attrs = NULL_TREE;
1392 struct c_declarator *declarator;
1395 /* Declaring either one or more declarators (in which case we
1396 should diagnose if there were no declaration specifiers) or a
1397 function definition (in which case the diagnostic for
1398 implicit int suffices). */
1399 declarator = c_parser_declarator (parser, specs->type_seen_p,
1400 C_DTR_NORMAL, &dummy);
1401 if (declarator == NULL)
1403 c_parser_skip_to_end_of_block_or_statement (parser);
1406 if (c_parser_next_token_is (parser, CPP_EQ)
1407 || c_parser_next_token_is (parser, CPP_COMMA)
1408 || c_parser_next_token_is (parser, CPP_SEMICOLON)
1409 || c_parser_next_token_is_keyword (parser, RID_ASM)
1410 || c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE)
1411 || c_parser_next_token_is_keyword (parser, RID_IN))
1413 tree asm_name = NULL_TREE;
1414 tree postfix_attrs = NULL_TREE;
1415 if (!diagnosed_no_specs && !specs->declspecs_seen_p)
1417 diagnosed_no_specs = true;
1418 pedwarn (here, 0, "data definition has no type or storage class");
1420 /* Having seen a data definition, there cannot now be a
1421 function definition. */
1423 if (c_parser_next_token_is_keyword (parser, RID_ASM))
1424 asm_name = c_parser_simple_asm_expr (parser);
1425 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
1426 postfix_attrs = c_parser_attributes (parser);
1427 if (c_parser_next_token_is (parser, CPP_EQ))
1431 location_t init_loc;
1432 c_parser_consume_token (parser);
1433 /* The declaration of the variable is in effect while
1434 its initializer is parsed. */
1435 d = start_decl (declarator, specs, true,
1436 chainon (postfix_attrs, all_prefix_attrs));
1438 d = error_mark_node;
1439 start_init (d, asm_name, global_bindings_p ());
1440 init_loc = c_parser_peek_token (parser)->location;
1441 init = c_parser_initializer (parser);
1443 if (d != error_mark_node)
1445 maybe_warn_string_init (TREE_TYPE (d), init);
1446 finish_decl (d, init_loc, init.value,
1447 init.original_type, asm_name);
1452 tree d = start_decl (declarator, specs, false,
1453 chainon (postfix_attrs,
1456 finish_decl (d, UNKNOWN_LOCATION, NULL_TREE,
1457 NULL_TREE, asm_name);
1459 if (c_parser_next_token_is_keyword (parser, RID_IN))
1462 *objc_foreach_object_declaration = d;
1464 *objc_foreach_object_declaration = error_mark_node;
1467 if (c_parser_next_token_is (parser, CPP_COMMA))
1469 c_parser_consume_token (parser);
1470 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
1471 all_prefix_attrs = chainon (c_parser_attributes (parser),
1474 all_prefix_attrs = prefix_attrs;
1477 else if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1479 c_parser_consume_token (parser);
1482 else if (c_parser_next_token_is_keyword (parser, RID_IN))
1484 /* This can only happen in Objective-C: we found the
1485 'in' that terminates the declaration inside an
1486 Objective-C foreach statement. Do not consume the
1487 token, so that the caller can use it to determine
1488 that this indeed is a foreach context. */
1493 c_parser_error (parser, "expected %<,%> or %<;%>");
1494 c_parser_skip_to_end_of_block_or_statement (parser);
1500 c_parser_error (parser, "expected %<=%>, %<,%>, %<;%>, "
1501 "%<asm%> or %<__attribute__%>");
1502 c_parser_skip_to_end_of_block_or_statement (parser);
1505 /* Function definition (nested or otherwise). */
1508 pedwarn (here, OPT_pedantic, "ISO C forbids nested functions");
1509 c_push_function_context ();
1511 if (!start_function (specs, declarator, all_prefix_attrs))
1513 /* This can appear in many cases looking nothing like a
1514 function definition, so we don't give a more specific
1515 error suggesting there was one. */
1516 c_parser_error (parser, "expected %<=%>, %<,%>, %<;%>, %<asm%> "
1517 "or %<__attribute__%>");
1519 c_pop_function_context ();
1522 /* Parse old-style parameter declarations. ??? Attributes are
1523 not allowed to start declaration specifiers here because of a
1524 syntax conflict between a function declaration with attribute
1525 suffix and a function definition with an attribute prefix on
1526 first old-style parameter declaration. Following the old
1527 parser, they are not accepted on subsequent old-style
1528 parameter declarations either. However, there is no
1529 ambiguity after the first declaration, nor indeed on the
1530 first as long as we don't allow postfix attributes after a
1531 declarator with a nonempty identifier list in a definition;
1532 and postfix attributes have never been accepted here in
1533 function definitions either. */
1534 while (c_parser_next_token_is_not (parser, CPP_EOF)
1535 && c_parser_next_token_is_not (parser, CPP_OPEN_BRACE))
1536 c_parser_declaration_or_fndef (parser, false, false, false,
1538 store_parm_decls ();
1539 DECL_STRUCT_FUNCTION (current_function_decl)->function_start_locus
1540 = c_parser_peek_token (parser)->location;
1541 fnbody = c_parser_compound_statement (parser);
1544 tree decl = current_function_decl;
1545 /* Mark nested functions as needing static-chain initially.
1546 lower_nested_functions will recompute it but the
1547 DECL_STATIC_CHAIN flag is also used before that happens,
1548 by initializer_constant_valid_p. See gcc.dg/nested-fn-2.c. */
1549 DECL_STATIC_CHAIN (decl) = 1;
1552 c_pop_function_context ();
1553 add_stmt (build_stmt (DECL_SOURCE_LOCATION (decl), DECL_EXPR, decl));
1564 /* Parse an asm-definition (asm() outside a function body). This is a
1572 c_parser_asm_definition (c_parser *parser)
1574 tree asm_str = c_parser_simple_asm_expr (parser);
1576 cgraph_add_asm_node (asm_str);
1577 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
1580 /* Parse a static assertion (C1X N1425 6.7.10).
1582 static_assert-declaration:
1583 static_assert-declaration-no-semi ;
1587 c_parser_static_assert_declaration (c_parser *parser)
1589 c_parser_static_assert_declaration_no_semi (parser);
1591 || !c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
1592 c_parser_skip_to_end_of_block_or_statement (parser);
1595 /* Parse a static assertion (C1X N1425 6.7.10), without the trailing
1598 static_assert-declaration-no-semi:
1599 _Static_assert ( constant-expression , string-literal )
1603 c_parser_static_assert_declaration_no_semi (c_parser *parser)
1605 location_t assert_loc, value_loc;
1609 gcc_assert (c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT));
1610 assert_loc = c_parser_peek_token (parser)->location;
1614 pedwarn (assert_loc, OPT_pedantic,
1615 "ISO C99 does not support %<_Static_assert%>");
1617 pedwarn (assert_loc, OPT_pedantic,
1618 "ISO C90 does not support %<_Static_assert%>");
1620 c_parser_consume_token (parser);
1621 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
1623 value_loc = c_parser_peek_token (parser)->location;
1624 value = c_parser_expr_no_commas (parser, NULL).value;
1625 parser->lex_untranslated_string = true;
1626 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
1628 parser->lex_untranslated_string = false;
1631 switch (c_parser_peek_token (parser)->type)
1637 case CPP_UTF8STRING:
1638 string = c_parser_peek_token (parser)->value;
1639 c_parser_consume_token (parser);
1640 parser->lex_untranslated_string = false;
1643 c_parser_error (parser, "expected string literal");
1644 parser->lex_untranslated_string = false;
1647 c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
1649 if (!INTEGRAL_TYPE_P (TREE_TYPE (value)))
1651 error_at (value_loc, "expression in static assertion is not an integer");
1654 if (TREE_CODE (value) != INTEGER_CST)
1656 value = c_fully_fold (value, false, NULL);
1657 if (TREE_CODE (value) == INTEGER_CST)
1658 pedwarn (value_loc, OPT_pedantic, "expression in static assertion "
1659 "is not an integer constant expression");
1661 if (TREE_CODE (value) != INTEGER_CST)
1663 error_at (value_loc, "expression in static assertion is not constant");
1666 constant_expression_warning (value);
1667 if (integer_zerop (value))
1668 error_at (assert_loc, "static assertion failed: %E", string);
1671 /* Parse some declaration specifiers (possibly none) (C90 6.5, C99
1672 6.7), adding them to SPECS (which may already include some).
1673 Storage class specifiers are accepted iff SCSPEC_OK; type
1674 specifiers are accepted iff TYPESPEC_OK; attributes are accepted at
1675 the start iff START_ATTR_OK.
1677 declaration-specifiers:
1678 storage-class-specifier declaration-specifiers[opt]
1679 type-specifier declaration-specifiers[opt]
1680 type-qualifier declaration-specifiers[opt]
1681 function-specifier declaration-specifiers[opt]
1683 Function specifiers (inline) are from C99, and are currently
1684 handled as storage class specifiers, as is __thread.
1686 C90 6.5.1, C99 6.7.1:
1687 storage-class-specifier:
1698 C90 6.5.2, C99 6.7.2:
1711 [_Imaginary removed in C99 TC2]
1712 struct-or-union-specifier
1716 (_Bool and _Complex are new in C99.)
1718 C90 6.5.3, C99 6.7.3:
1724 address-space-qualifier
1726 (restrict is new in C99.)
1730 declaration-specifiers:
1731 attributes declaration-specifiers[opt]
1737 identifier recognized by the target
1739 storage-class-specifier:
1752 (_Fract, _Accum, and _Sat are new from ISO/IEC DTR 18037:
1753 http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1169.pdf)
1758 class-name objc-protocol-refs[opt]
1759 typedef-name objc-protocol-refs
1764 c_parser_declspecs (c_parser *parser, struct c_declspecs *specs,
1765 bool scspec_ok, bool typespec_ok, bool start_attr_ok)
1767 bool attrs_ok = start_attr_ok;
1768 bool seen_type = specs->type_seen_p;
1769 while (c_parser_next_token_is (parser, CPP_NAME)
1770 || c_parser_next_token_is (parser, CPP_KEYWORD)
1771 || (c_dialect_objc () && c_parser_next_token_is (parser, CPP_LESS)))
1773 struct c_typespec t;
1775 location_t loc = c_parser_peek_token (parser)->location;
1776 if (c_parser_next_token_is (parser, CPP_NAME))
1778 tree value = c_parser_peek_token (parser)->value;
1779 c_id_kind kind = c_parser_peek_token (parser)->id_kind;
1781 if (kind == C_ID_ADDRSPACE)
1784 = c_parser_peek_token (parser)->keyword - RID_FIRST_ADDR_SPACE;
1785 declspecs_add_addrspace (specs, as);
1786 c_parser_consume_token (parser);
1791 /* This finishes the specifiers unless a type name is OK, it
1792 is declared as a type name and a type name hasn't yet
1794 if (!typespec_ok || seen_type
1795 || (kind != C_ID_TYPENAME && kind != C_ID_CLASSNAME))
1797 c_parser_consume_token (parser);
1800 if (kind == C_ID_TYPENAME
1801 && (!c_dialect_objc ()
1802 || c_parser_next_token_is_not (parser, CPP_LESS)))
1804 t.kind = ctsk_typedef;
1805 /* For a typedef name, record the meaning, not the name.
1806 In case of 'foo foo, bar;'. */
1807 t.spec = lookup_name (value);
1809 t.expr_const_operands = true;
1813 tree proto = NULL_TREE;
1814 gcc_assert (c_dialect_objc ());
1816 if (c_parser_next_token_is (parser, CPP_LESS))
1817 proto = c_parser_objc_protocol_refs (parser);
1818 t.spec = objc_get_protocol_qualified_type (value, proto);
1820 t.expr_const_operands = true;
1822 declspecs_add_type (loc, specs, t);
1825 if (c_parser_next_token_is (parser, CPP_LESS))
1827 /* Make "<SomeProtocol>" equivalent to "id <SomeProtocol>" -
1828 nisse@lysator.liu.se. */
1830 gcc_assert (c_dialect_objc ());
1831 if (!typespec_ok || seen_type)
1833 proto = c_parser_objc_protocol_refs (parser);
1835 t.spec = objc_get_protocol_qualified_type (NULL_TREE, proto);
1837 t.expr_const_operands = true;
1838 declspecs_add_type (loc, specs, t);
1841 gcc_assert (c_parser_next_token_is (parser, CPP_KEYWORD));
1842 switch (c_parser_peek_token (parser)->keyword)
1854 /* TODO: Distinguish between function specifiers (inline)
1855 and storage class specifiers, either here or in
1856 declspecs_add_scspec. */
1857 declspecs_add_scspec (specs, c_parser_peek_token (parser)->value);
1858 c_parser_consume_token (parser);
1882 if (c_dialect_objc ())
1883 parser->objc_need_raw_identifier = true;
1884 t.kind = ctsk_resword;
1885 t.spec = c_parser_peek_token (parser)->value;
1887 t.expr_const_operands = true;
1888 declspecs_add_type (loc, specs, t);
1889 c_parser_consume_token (parser);
1896 t = c_parser_enum_specifier (parser);
1897 declspecs_add_type (loc, specs, t);
1905 t = c_parser_struct_or_union_specifier (parser);
1906 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, t.spec);
1907 declspecs_add_type (loc, specs, t);
1910 /* ??? The old parser rejected typeof after other type
1911 specifiers, but is a syntax error the best way of
1913 if (!typespec_ok || seen_type)
1917 t = c_parser_typeof_specifier (parser);
1918 declspecs_add_type (loc, specs, t);
1924 declspecs_add_qual (specs, c_parser_peek_token (parser)->value);
1925 c_parser_consume_token (parser);
1930 attrs = c_parser_attributes (parser);
1931 declspecs_add_attrs (specs, attrs);
1940 /* Parse an enum specifier (C90 6.5.2.2, C99 6.7.2.2).
1943 enum attributes[opt] identifier[opt] { enumerator-list } attributes[opt]
1944 enum attributes[opt] identifier[opt] { enumerator-list , } attributes[opt]
1945 enum attributes[opt] identifier
1947 The form with trailing comma is new in C99. The forms with
1948 attributes are GNU extensions. In GNU C, we accept any expression
1949 without commas in the syntax (assignment expressions, not just
1950 conditional expressions); assignment expressions will be diagnosed
1955 enumerator-list , enumerator
1958 enumeration-constant
1959 enumeration-constant = constant-expression
1962 static struct c_typespec
1963 c_parser_enum_specifier (c_parser *parser)
1965 struct c_typespec ret;
1967 tree ident = NULL_TREE;
1968 location_t enum_loc;
1969 location_t ident_loc = UNKNOWN_LOCATION; /* Quiet warning. */
1970 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ENUM));
1971 enum_loc = c_parser_peek_token (parser)->location;
1972 c_parser_consume_token (parser);
1973 attrs = c_parser_attributes (parser);
1974 enum_loc = c_parser_peek_token (parser)->location;
1975 /* Set the location in case we create a decl now. */
1976 c_parser_set_source_position_from_token (c_parser_peek_token (parser));
1977 if (c_parser_next_token_is (parser, CPP_NAME))
1979 ident = c_parser_peek_token (parser)->value;
1980 ident_loc = c_parser_peek_token (parser)->location;
1981 enum_loc = ident_loc;
1982 c_parser_consume_token (parser);
1984 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
1986 /* Parse an enum definition. */
1987 struct c_enum_contents the_enum;
1988 tree type = start_enum (enum_loc, &the_enum, ident);
1990 /* We chain the enumerators in reverse order, then put them in
1991 forward order at the end. */
1992 tree values = NULL_TREE;
1993 c_parser_consume_token (parser);
2001 location_t comma_loc = UNKNOWN_LOCATION; /* Quiet warning. */
2002 location_t decl_loc, value_loc;
2003 if (c_parser_next_token_is_not (parser, CPP_NAME))
2005 c_parser_error (parser, "expected identifier");
2006 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
2007 values = error_mark_node;
2010 token = c_parser_peek_token (parser);
2011 enum_id = token->value;
2012 /* Set the location in case we create a decl now. */
2013 c_parser_set_source_position_from_token (token);
2014 decl_loc = value_loc = token->location;
2015 c_parser_consume_token (parser);
2016 if (c_parser_next_token_is (parser, CPP_EQ))
2018 c_parser_consume_token (parser);
2019 value_loc = c_parser_peek_token (parser)->location;
2020 enum_value = c_parser_expr_no_commas (parser, NULL).value;
2023 enum_value = NULL_TREE;
2024 enum_decl = build_enumerator (decl_loc, value_loc,
2025 &the_enum, enum_id, enum_value);
2026 TREE_CHAIN (enum_decl) = values;
2029 if (c_parser_next_token_is (parser, CPP_COMMA))
2031 comma_loc = c_parser_peek_token (parser)->location;
2033 c_parser_consume_token (parser);
2035 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2037 if (seen_comma && !flag_isoc99)
2038 pedwarn (comma_loc, OPT_pedantic, "comma at end of enumerator list");
2039 c_parser_consume_token (parser);
2044 c_parser_error (parser, "expected %<,%> or %<}%>");
2045 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
2046 values = error_mark_node;
2050 postfix_attrs = c_parser_attributes (parser);
2051 ret.spec = finish_enum (type, nreverse (values),
2052 chainon (attrs, postfix_attrs));
2053 ret.kind = ctsk_tagdef;
2054 ret.expr = NULL_TREE;
2055 ret.expr_const_operands = true;
2060 c_parser_error (parser, "expected %<{%>");
2061 ret.spec = error_mark_node;
2062 ret.kind = ctsk_tagref;
2063 ret.expr = NULL_TREE;
2064 ret.expr_const_operands = true;
2067 ret = parser_xref_tag (ident_loc, ENUMERAL_TYPE, ident);
2068 /* In ISO C, enumerated types can be referred to only if already
2070 if (pedantic && !COMPLETE_TYPE_P (ret.spec))
2073 pedwarn (enum_loc, OPT_pedantic,
2074 "ISO C forbids forward references to %<enum%> types");
2079 /* Parse a struct or union specifier (C90 6.5.2.1, C99 6.7.2.1).
2081 struct-or-union-specifier:
2082 struct-or-union attributes[opt] identifier[opt]
2083 { struct-contents } attributes[opt]
2084 struct-or-union attributes[opt] identifier
2087 struct-declaration-list
2089 struct-declaration-list:
2090 struct-declaration ;
2091 struct-declaration-list struct-declaration ;
2098 struct-declaration-list struct-declaration
2100 struct-declaration-list:
2101 struct-declaration-list ;
2104 (Note that in the syntax here, unlike that in ISO C, the semicolons
2105 are included here rather than in struct-declaration, in order to
2106 describe the syntax with extra semicolons and missing semicolon at
2111 struct-declaration-list:
2112 @defs ( class-name )
2114 (Note this does not include a trailing semicolon, but can be
2115 followed by further declarations, and gets a pedwarn-if-pedantic
2116 when followed by a semicolon.) */
2118 static struct c_typespec
2119 c_parser_struct_or_union_specifier (c_parser *parser)
2121 struct c_typespec ret;
2123 tree ident = NULL_TREE;
2124 location_t struct_loc;
2125 location_t ident_loc = UNKNOWN_LOCATION;
2126 enum tree_code code;
2127 switch (c_parser_peek_token (parser)->keyword)
2138 struct_loc = c_parser_peek_token (parser)->location;
2139 c_parser_consume_token (parser);
2140 attrs = c_parser_attributes (parser);
2142 /* Set the location in case we create a decl now. */
2143 c_parser_set_source_position_from_token (c_parser_peek_token (parser));
2145 if (c_parser_next_token_is (parser, CPP_NAME))
2147 ident = c_parser_peek_token (parser)->value;
2148 ident_loc = c_parser_peek_token (parser)->location;
2149 struct_loc = ident_loc;
2150 c_parser_consume_token (parser);
2152 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
2154 /* Parse a struct or union definition. Start the scope of the
2155 tag before parsing components. */
2156 struct c_struct_parse_info *struct_info;
2157 tree type = start_struct (struct_loc, code, ident, &struct_info);
2159 /* We chain the components in reverse order, then put them in
2160 forward order at the end. Each struct-declaration may
2161 declare multiple components (comma-separated), so we must use
2162 chainon to join them, although when parsing each
2163 struct-declaration we can use TREE_CHAIN directly.
2165 The theory behind all this is that there will be more
2166 semicolon separated fields than comma separated fields, and
2167 so we'll be minimizing the number of node traversals required
2169 tree contents = NULL_TREE;
2170 c_parser_consume_token (parser);
2171 /* Handle the Objective-C @defs construct,
2172 e.g. foo(sizeof(struct{ @defs(ClassName) }));. */
2173 if (c_parser_next_token_is_keyword (parser, RID_AT_DEFS))
2176 gcc_assert (c_dialect_objc ());
2177 c_parser_consume_token (parser);
2178 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2180 if (c_parser_next_token_is (parser, CPP_NAME)
2181 && c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME)
2183 name = c_parser_peek_token (parser)->value;
2184 c_parser_consume_token (parser);
2188 c_parser_error (parser, "expected class name");
2189 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
2192 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2194 contents = nreverse (objc_get_class_ivars (name));
2197 /* Parse the struct-declarations and semicolons. Problems with
2198 semicolons are diagnosed here; empty structures are diagnosed
2203 /* Parse any stray semicolon. */
2204 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
2206 pedwarn (c_parser_peek_token (parser)->location, OPT_pedantic,
2207 "extra semicolon in struct or union specified");
2208 c_parser_consume_token (parser);
2211 /* Stop if at the end of the struct or union contents. */
2212 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2214 c_parser_consume_token (parser);
2217 /* Accept #pragmas at struct scope. */
2218 if (c_parser_next_token_is (parser, CPP_PRAGMA))
2220 c_parser_pragma (parser, pragma_external);
2223 /* Parse some comma-separated declarations, but not the
2224 trailing semicolon if any. */
2225 decls = c_parser_struct_declaration (parser);
2226 contents = chainon (decls, contents);
2227 /* If no semicolon follows, either we have a parse error or
2228 are at the end of the struct or union and should
2230 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
2231 c_parser_consume_token (parser);
2234 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2235 pedwarn (c_parser_peek_token (parser)->location, 0,
2236 "no semicolon at end of struct or union");
2239 c_parser_error (parser, "expected %<;%>");
2240 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
2245 postfix_attrs = c_parser_attributes (parser);
2246 ret.spec = finish_struct (struct_loc, type, nreverse (contents),
2247 chainon (attrs, postfix_attrs), struct_info);
2248 ret.kind = ctsk_tagdef;
2249 ret.expr = NULL_TREE;
2250 ret.expr_const_operands = true;
2255 c_parser_error (parser, "expected %<{%>");
2256 ret.spec = error_mark_node;
2257 ret.kind = ctsk_tagref;
2258 ret.expr = NULL_TREE;
2259 ret.expr_const_operands = true;
2262 ret = parser_xref_tag (ident_loc, code, ident);
2266 /* Parse a struct-declaration (C90 6.5.2.1, C99 6.7.2.1), *without*
2267 the trailing semicolon.
2270 specifier-qualifier-list struct-declarator-list
2271 static_assert-declaration-no-semi
2273 specifier-qualifier-list:
2274 type-specifier specifier-qualifier-list[opt]
2275 type-qualifier specifier-qualifier-list[opt]
2276 attributes specifier-qualifier-list[opt]
2278 struct-declarator-list:
2280 struct-declarator-list , attributes[opt] struct-declarator
2283 declarator attributes[opt]
2284 declarator[opt] : constant-expression attributes[opt]
2289 __extension__ struct-declaration
2290 specifier-qualifier-list
2292 Unlike the ISO C syntax, semicolons are handled elsewhere. The use
2293 of attributes where shown is a GNU extension. In GNU C, we accept
2294 any expression without commas in the syntax (assignment
2295 expressions, not just conditional expressions); assignment
2296 expressions will be diagnosed as non-constant. */
2299 c_parser_struct_declaration (c_parser *parser)
2301 struct c_declspecs *specs;
2303 tree all_prefix_attrs;
2305 location_t decl_loc;
2306 if (c_parser_next_token_is_keyword (parser, RID_EXTENSION))
2310 ext = disable_extension_diagnostics ();
2311 c_parser_consume_token (parser);
2312 decl = c_parser_struct_declaration (parser);
2313 restore_extension_diagnostics (ext);
2316 if (c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT))
2318 c_parser_static_assert_declaration_no_semi (parser);
2321 specs = build_null_declspecs ();
2322 decl_loc = c_parser_peek_token (parser)->location;
2323 c_parser_declspecs (parser, specs, false, true, true);
2326 if (!specs->declspecs_seen_p)
2328 c_parser_error (parser, "expected specifier-qualifier-list");
2331 finish_declspecs (specs);
2332 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
2335 if (!specs->type_seen_p)
2337 pedwarn (decl_loc, OPT_pedantic,
2338 "ISO C forbids member declarations with no members");
2339 shadow_tag_warned (specs, pedantic);
2344 /* Support for unnamed structs or unions as members of
2345 structs or unions (which is [a] useful and [b] supports
2349 ret = grokfield (c_parser_peek_token (parser)->location,
2350 build_id_declarator (NULL_TREE), specs,
2353 decl_attributes (&ret, attrs, 0);
2357 pending_xref_error ();
2358 prefix_attrs = specs->attrs;
2359 all_prefix_attrs = prefix_attrs;
2360 specs->attrs = NULL_TREE;
2364 /* Declaring one or more declarators or un-named bit-fields. */
2365 struct c_declarator *declarator;
2367 if (c_parser_next_token_is (parser, CPP_COLON))
2368 declarator = build_id_declarator (NULL_TREE);
2370 declarator = c_parser_declarator (parser, specs->type_seen_p,
2371 C_DTR_NORMAL, &dummy);
2372 if (declarator == NULL)
2374 c_parser_skip_to_end_of_block_or_statement (parser);
2377 if (c_parser_next_token_is (parser, CPP_COLON)
2378 || c_parser_next_token_is (parser, CPP_COMMA)
2379 || c_parser_next_token_is (parser, CPP_SEMICOLON)
2380 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE)
2381 || c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2383 tree postfix_attrs = NULL_TREE;
2384 tree width = NULL_TREE;
2386 if (c_parser_next_token_is (parser, CPP_COLON))
2388 c_parser_consume_token (parser);
2389 width = c_parser_expr_no_commas (parser, NULL).value;
2391 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2392 postfix_attrs = c_parser_attributes (parser);
2393 d = grokfield (c_parser_peek_token (parser)->location,
2394 declarator, specs, width, &all_prefix_attrs);
2395 decl_attributes (&d, chainon (postfix_attrs,
2396 all_prefix_attrs), 0);
2397 DECL_CHAIN (d) = decls;
2399 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2400 all_prefix_attrs = chainon (c_parser_attributes (parser),
2403 all_prefix_attrs = prefix_attrs;
2404 if (c_parser_next_token_is (parser, CPP_COMMA))
2405 c_parser_consume_token (parser);
2406 else if (c_parser_next_token_is (parser, CPP_SEMICOLON)
2407 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2409 /* Semicolon consumed in caller. */
2414 c_parser_error (parser, "expected %<,%>, %<;%> or %<}%>");
2420 c_parser_error (parser,
2421 "expected %<:%>, %<,%>, %<;%>, %<}%> or "
2422 "%<__attribute__%>");
2429 /* Parse a typeof specifier (a GNU extension).
2432 typeof ( expression )
2433 typeof ( type-name )
2436 static struct c_typespec
2437 c_parser_typeof_specifier (c_parser *parser)
2439 struct c_typespec ret;
2440 ret.kind = ctsk_typeof;
2441 ret.spec = error_mark_node;
2442 ret.expr = NULL_TREE;
2443 ret.expr_const_operands = true;
2444 gcc_assert (c_parser_next_token_is_keyword (parser, RID_TYPEOF));
2445 c_parser_consume_token (parser);
2446 c_inhibit_evaluation_warnings++;
2448 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2450 c_inhibit_evaluation_warnings--;
2454 if (c_parser_next_token_starts_typename (parser))
2456 struct c_type_name *type = c_parser_type_name (parser);
2457 c_inhibit_evaluation_warnings--;
2461 ret.spec = groktypename (type, &ret.expr, &ret.expr_const_operands);
2462 pop_maybe_used (variably_modified_type_p (ret.spec, NULL_TREE));
2468 location_t here = c_parser_peek_token (parser)->location;
2469 struct c_expr expr = c_parser_expression (parser);
2470 c_inhibit_evaluation_warnings--;
2472 if (TREE_CODE (expr.value) == COMPONENT_REF
2473 && DECL_C_BIT_FIELD (TREE_OPERAND (expr.value, 1)))
2474 error_at (here, "%<typeof%> applied to a bit-field");
2475 mark_exp_read (expr.value);
2476 ret.spec = TREE_TYPE (expr.value);
2477 was_vm = variably_modified_type_p (ret.spec, NULL_TREE);
2478 /* This is returned with the type so that when the type is
2479 evaluated, this can be evaluated. */
2481 ret.expr = c_fully_fold (expr.value, false, &ret.expr_const_operands);
2482 pop_maybe_used (was_vm);
2484 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
2488 /* Parse a declarator, possibly an abstract declarator (C90 6.5.4,
2489 6.5.5, C99 6.7.5, 6.7.6). If TYPE_SEEN_P then a typedef name may
2490 be redeclared; otherwise it may not. KIND indicates which kind of
2491 declarator is wanted. Returns a valid declarator except in the
2492 case of a syntax error in which case NULL is returned. *SEEN_ID is
2493 set to true if an identifier being declared is seen; this is used
2494 to diagnose bad forms of abstract array declarators and to
2495 determine whether an identifier list is syntactically permitted.
2498 pointer[opt] direct-declarator
2502 ( attributes[opt] declarator )
2503 direct-declarator array-declarator
2504 direct-declarator ( parameter-type-list )
2505 direct-declarator ( identifier-list[opt] )
2508 * type-qualifier-list[opt]
2509 * type-qualifier-list[opt] pointer
2511 type-qualifier-list:
2514 type-qualifier-list type-qualifier
2515 type-qualifier-list attributes
2517 parameter-type-list:
2519 parameter-list , ...
2522 parameter-declaration
2523 parameter-list , parameter-declaration
2525 parameter-declaration:
2526 declaration-specifiers declarator attributes[opt]
2527 declaration-specifiers abstract-declarator[opt] attributes[opt]
2531 identifier-list , identifier
2533 abstract-declarator:
2535 pointer[opt] direct-abstract-declarator
2537 direct-abstract-declarator:
2538 ( attributes[opt] abstract-declarator )
2539 direct-abstract-declarator[opt] array-declarator
2540 direct-abstract-declarator[opt] ( parameter-type-list[opt] )
2545 direct-declarator ( parameter-forward-declarations
2546 parameter-type-list[opt] )
2548 direct-abstract-declarator:
2549 direct-abstract-declarator[opt] ( parameter-forward-declarations
2550 parameter-type-list[opt] )
2552 parameter-forward-declarations:
2554 parameter-forward-declarations parameter-list ;
2556 The uses of attributes shown above are GNU extensions.
2558 Some forms of array declarator are not included in C99 in the
2559 syntax for abstract declarators; these are disallowed elsewhere.
2560 This may be a defect (DR#289).
2562 This function also accepts an omitted abstract declarator as being
2563 an abstract declarator, although not part of the formal syntax. */
2565 static struct c_declarator *
2566 c_parser_declarator (c_parser *parser, bool type_seen_p, c_dtr_syn kind,
2569 /* Parse any initial pointer part. */
2570 if (c_parser_next_token_is (parser, CPP_MULT))
2572 struct c_declspecs *quals_attrs = build_null_declspecs ();
2573 struct c_declarator *inner;
2574 c_parser_consume_token (parser);
2575 c_parser_declspecs (parser, quals_attrs, false, false, true);
2576 inner = c_parser_declarator (parser, type_seen_p, kind, seen_id);
2580 return make_pointer_declarator (quals_attrs, inner);
2582 /* Now we have a direct declarator, direct abstract declarator or
2583 nothing (which counts as a direct abstract declarator here). */
2584 return c_parser_direct_declarator (parser, type_seen_p, kind, seen_id);
2587 /* Parse a direct declarator or direct abstract declarator; arguments
2588 as c_parser_declarator. */
2590 static struct c_declarator *
2591 c_parser_direct_declarator (c_parser *parser, bool type_seen_p, c_dtr_syn kind,
2594 /* The direct declarator must start with an identifier (possibly
2595 omitted) or a parenthesized declarator (possibly abstract). In
2596 an ordinary declarator, initial parentheses must start a
2597 parenthesized declarator. In an abstract declarator or parameter
2598 declarator, they could start a parenthesized declarator or a
2599 parameter list. To tell which, the open parenthesis and any
2600 following attributes must be read. If a declaration specifier
2601 follows, then it is a parameter list; if the specifier is a
2602 typedef name, there might be an ambiguity about redeclaring it,
2603 which is resolved in the direction of treating it as a typedef
2604 name. If a close parenthesis follows, it is also an empty
2605 parameter list, as the syntax does not permit empty abstract
2606 declarators. Otherwise, it is a parenthesized declarator (in
2607 which case the analysis may be repeated inside it, recursively).
2609 ??? There is an ambiguity in a parameter declaration "int
2610 (__attribute__((foo)) x)", where x is not a typedef name: it
2611 could be an abstract declarator for a function, or declare x with
2612 parentheses. The proper resolution of this ambiguity needs
2613 documenting. At present we follow an accident of the old
2614 parser's implementation, whereby the first parameter must have
2615 some declaration specifiers other than just attributes. Thus as
2616 a parameter declaration it is treated as a parenthesized
2617 parameter named x, and as an abstract declarator it is
2620 ??? Also following the old parser, attributes inside an empty
2621 parameter list are ignored, making it a list not yielding a
2622 prototype, rather than giving an error or making it have one
2623 parameter with implicit type int.
2625 ??? Also following the old parser, typedef names may be
2626 redeclared in declarators, but not Objective-C class names. */
2628 if (kind != C_DTR_ABSTRACT
2629 && c_parser_next_token_is (parser, CPP_NAME)
2631 && (c_parser_peek_token (parser)->id_kind == C_ID_TYPENAME
2632 || c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME))
2633 || c_parser_peek_token (parser)->id_kind == C_ID_ID))
2635 struct c_declarator *inner
2636 = build_id_declarator (c_parser_peek_token (parser)->value);
2638 inner->id_loc = c_parser_peek_token (parser)->location;
2639 c_parser_consume_token (parser);
2640 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
2643 if (kind != C_DTR_NORMAL
2644 && c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
2646 struct c_declarator *inner = build_id_declarator (NULL_TREE);
2647 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
2650 /* Either we are at the end of an abstract declarator, or we have
2653 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
2656 struct c_declarator *inner;
2657 c_parser_consume_token (parser);
2658 attrs = c_parser_attributes (parser);
2659 if (kind != C_DTR_NORMAL
2660 && (c_parser_next_token_starts_declspecs (parser)
2661 || c_parser_next_token_is (parser, CPP_CLOSE_PAREN)))
2663 struct c_arg_info *args
2664 = c_parser_parms_declarator (parser, kind == C_DTR_NORMAL,
2671 = build_function_declarator (args,
2672 build_id_declarator (NULL_TREE));
2673 return c_parser_direct_declarator_inner (parser, *seen_id,
2677 /* A parenthesized declarator. */
2678 inner = c_parser_declarator (parser, type_seen_p, kind, seen_id);
2679 if (inner != NULL && attrs != NULL)
2680 inner = build_attrs_declarator (attrs, inner);
2681 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2683 c_parser_consume_token (parser);
2687 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
2691 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2698 if (kind == C_DTR_NORMAL)
2700 c_parser_error (parser, "expected identifier or %<(%>");
2704 return build_id_declarator (NULL_TREE);
2708 /* Parse part of a direct declarator or direct abstract declarator,
2709 given that some (in INNER) has already been parsed; ID_PRESENT is
2710 true if an identifier is present, false for an abstract
2713 static struct c_declarator *
2714 c_parser_direct_declarator_inner (c_parser *parser, bool id_present,
2715 struct c_declarator *inner)
2717 /* Parse a sequence of array declarators and parameter lists. */
2718 if (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
2720 location_t brace_loc = c_parser_peek_token (parser)->location;
2721 struct c_declarator *declarator;
2722 struct c_declspecs *quals_attrs = build_null_declspecs ();
2726 c_parser_consume_token (parser);
2727 c_parser_declspecs (parser, quals_attrs, false, false, true);
2728 static_seen = c_parser_next_token_is_keyword (parser, RID_STATIC);
2730 c_parser_consume_token (parser);
2731 if (static_seen && !quals_attrs->declspecs_seen_p)
2732 c_parser_declspecs (parser, quals_attrs, false, false, true);
2733 if (!quals_attrs->declspecs_seen_p)
2735 /* If "static" is present, there must be an array dimension.
2736 Otherwise, there may be a dimension, "*", or no
2741 dimen = c_parser_expr_no_commas (parser, NULL).value;
2745 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
2750 else if (c_parser_next_token_is (parser, CPP_MULT))
2752 if (c_parser_peek_2nd_token (parser)->type == CPP_CLOSE_SQUARE)
2756 c_parser_consume_token (parser);
2761 dimen = c_parser_expr_no_commas (parser, NULL).value;
2767 dimen = c_parser_expr_no_commas (parser, NULL).value;
2770 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
2771 c_parser_consume_token (parser);
2774 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
2779 mark_exp_read (dimen);
2780 declarator = build_array_declarator (brace_loc, dimen, quals_attrs,
2781 static_seen, star_seen);
2782 if (declarator == NULL)
2784 inner = set_array_declarator_inner (declarator, inner);
2785 return c_parser_direct_declarator_inner (parser, id_present, inner);
2787 else if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
2790 struct c_arg_info *args;
2791 c_parser_consume_token (parser);
2792 attrs = c_parser_attributes (parser);
2793 args = c_parser_parms_declarator (parser, id_present, attrs);
2798 inner = build_function_declarator (args, inner);
2799 return c_parser_direct_declarator_inner (parser, id_present, inner);
2805 /* Parse a parameter list or identifier list, including the closing
2806 parenthesis but not the opening one. ATTRS are the attributes at
2807 the start of the list. ID_LIST_OK is true if an identifier list is
2808 acceptable; such a list must not have attributes at the start. */
2810 static struct c_arg_info *
2811 c_parser_parms_declarator (c_parser *parser, bool id_list_ok, tree attrs)
2814 declare_parm_level ();
2815 /* If the list starts with an identifier, it is an identifier list.
2816 Otherwise, it is either a prototype list or an empty list. */
2819 && c_parser_next_token_is (parser, CPP_NAME)
2820 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
2822 tree list = NULL_TREE, *nextp = &list;
2823 while (c_parser_next_token_is (parser, CPP_NAME)
2824 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
2826 *nextp = build_tree_list (NULL_TREE,
2827 c_parser_peek_token (parser)->value);
2828 nextp = & TREE_CHAIN (*nextp);
2829 c_parser_consume_token (parser);
2830 if (c_parser_next_token_is_not (parser, CPP_COMMA))
2832 c_parser_consume_token (parser);
2833 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2835 c_parser_error (parser, "expected identifier");
2839 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2841 struct c_arg_info *ret = build_arg_info ();
2843 c_parser_consume_token (parser);
2849 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2857 struct c_arg_info *ret = c_parser_parms_list_declarator (parser, attrs);
2863 /* Parse a parameter list (possibly empty), including the closing
2864 parenthesis but not the opening one. ATTRS are the attributes at
2865 the start of the list. */
2867 static struct c_arg_info *
2868 c_parser_parms_list_declarator (c_parser *parser, tree attrs)
2870 bool bad_parm = false;
2871 /* ??? Following the old parser, forward parameter declarations may
2872 use abstract declarators, and if no real parameter declarations
2873 follow the forward declarations then this is not diagnosed. Also
2874 note as above that attributes are ignored as the only contents of
2875 the parentheses, or as the only contents after forward
2877 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2879 struct c_arg_info *ret = build_arg_info ();
2880 c_parser_consume_token (parser);
2883 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
2885 struct c_arg_info *ret = build_arg_info ();
2886 /* Suppress -Wold-style-definition for this case. */
2887 ret->types = error_mark_node;
2888 error_at (c_parser_peek_token (parser)->location,
2889 "ISO C requires a named argument before %<...%>");
2890 c_parser_consume_token (parser);
2891 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2893 c_parser_consume_token (parser);
2898 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2903 /* Nonempty list of parameters, either terminated with semicolon
2904 (forward declarations; recurse) or with close parenthesis (normal
2905 function) or with ", ... )" (variadic function). */
2908 /* Parse a parameter. */
2909 struct c_parm *parm = c_parser_parameter_declaration (parser, attrs);
2914 push_parm_decl (parm);
2915 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
2918 c_parser_consume_token (parser);
2919 mark_forward_parm_decls ();
2920 new_attrs = c_parser_attributes (parser);
2921 return c_parser_parms_list_declarator (parser, new_attrs);
2923 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2925 c_parser_consume_token (parser);
2928 get_pending_sizes ();
2932 return get_parm_info (false);
2934 if (!c_parser_require (parser, CPP_COMMA,
2935 "expected %<;%>, %<,%> or %<)%>"))
2937 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
2938 get_pending_sizes ();
2941 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
2943 c_parser_consume_token (parser);
2944 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2946 c_parser_consume_token (parser);
2949 get_pending_sizes ();
2953 return get_parm_info (true);
2957 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2959 get_pending_sizes ();
2966 /* Parse a parameter declaration. ATTRS are the attributes at the
2967 start of the declaration if it is the first parameter. */
2969 static struct c_parm *
2970 c_parser_parameter_declaration (c_parser *parser, tree attrs)
2972 struct c_declspecs *specs;
2973 struct c_declarator *declarator;
2975 tree postfix_attrs = NULL_TREE;
2977 if (!c_parser_next_token_starts_declspecs (parser))
2979 c_token *token = c_parser_peek_token (parser);
2982 c_parser_set_source_position_from_token (token);
2983 if (token->type == CPP_NAME
2984 && c_parser_peek_2nd_token (parser)->type != CPP_COMMA
2985 && c_parser_peek_2nd_token (parser)->type != CPP_CLOSE_PAREN)
2987 error ("unknown type name %qE", token->value);
2988 parser->error = true;
2990 /* ??? In some Objective-C cases '...' isn't applicable so there
2991 should be a different message. */
2993 c_parser_error (parser,
2994 "expected declaration specifiers or %<...%>");
2995 c_parser_skip_to_end_of_parameter (parser);
2998 specs = build_null_declspecs ();
3001 declspecs_add_attrs (specs, attrs);
3004 c_parser_declspecs (parser, specs, true, true, true);
3005 finish_declspecs (specs);
3006 pending_xref_error ();
3007 prefix_attrs = specs->attrs;
3008 specs->attrs = NULL_TREE;
3009 declarator = c_parser_declarator (parser, specs->type_seen_p,
3010 C_DTR_PARM, &dummy);
3011 if (declarator == NULL)
3013 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
3016 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
3017 postfix_attrs = c_parser_attributes (parser);
3018 return build_c_parm (specs, chainon (postfix_attrs, prefix_attrs),
3022 /* Parse a string literal in an asm expression. It should not be
3023 translated, and wide string literals are an error although
3024 permitted by the syntax. This is a GNU extension.
3029 ??? At present, following the old parser, the caller needs to have
3030 set lex_untranslated_string to 1. It would be better to follow the
3031 C++ parser rather than using this kludge. */
3034 c_parser_asm_string_literal (c_parser *parser)
3037 if (c_parser_next_token_is (parser, CPP_STRING))
3039 str = c_parser_peek_token (parser)->value;
3040 c_parser_consume_token (parser);
3042 else if (c_parser_next_token_is (parser, CPP_WSTRING))
3044 error_at (c_parser_peek_token (parser)->location,
3045 "wide string literal in %<asm%>");
3046 str = build_string (1, "");
3047 c_parser_consume_token (parser);
3051 c_parser_error (parser, "expected string literal");
3057 /* Parse a simple asm expression. This is used in restricted
3058 contexts, where a full expression with inputs and outputs does not
3059 make sense. This is a GNU extension.
3062 asm ( asm-string-literal )
3066 c_parser_simple_asm_expr (c_parser *parser)
3069 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ASM));
3070 /* ??? Follow the C++ parser rather than using the
3071 lex_untranslated_string kludge. */
3072 parser->lex_untranslated_string = true;
3073 c_parser_consume_token (parser);
3074 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3076 parser->lex_untranslated_string = false;
3079 str = c_parser_asm_string_literal (parser);
3080 parser->lex_untranslated_string = false;
3081 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
3083 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
3089 /* Parse (possibly empty) attributes. This is a GNU extension.
3093 attributes attribute
3096 __attribute__ ( ( attribute-list ) )
3100 attribute_list , attrib
3105 any-word ( identifier )
3106 any-word ( identifier , nonempty-expr-list )
3107 any-word ( expr-list )
3109 where the "identifier" must not be declared as a type, and
3110 "any-word" may be any identifier (including one declared as a
3111 type), a reserved word storage class specifier, type specifier or
3112 type qualifier. ??? This still leaves out most reserved keywords
3113 (following the old parser), shouldn't we include them, and why not
3114 allow identifiers declared as types to start the arguments? */
3117 c_parser_attributes (c_parser *parser)
3119 tree attrs = NULL_TREE;
3120 while (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
3122 /* ??? Follow the C++ parser rather than using the
3123 lex_untranslated_string kludge. */
3124 parser->lex_untranslated_string = true;
3125 c_parser_consume_token (parser);
3126 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3128 parser->lex_untranslated_string = false;
3131 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3133 parser->lex_untranslated_string = false;
3134 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
3137 /* Parse the attribute list. */
3138 while (c_parser_next_token_is (parser, CPP_COMMA)
3139 || c_parser_next_token_is (parser, CPP_NAME)
3140 || c_parser_next_token_is (parser, CPP_KEYWORD))
3142 tree attr, attr_name, attr_args;
3143 VEC(tree,gc) *expr_list;
3144 if (c_parser_next_token_is (parser, CPP_COMMA))
3146 c_parser_consume_token (parser);
3149 if (c_parser_next_token_is (parser, CPP_KEYWORD))
3151 /* ??? See comment above about what keywords are
3154 switch (c_parser_peek_token (parser)->keyword)
3192 /* Accept __attribute__((__const)) as __attribute__((const))
3195 = ridpointers[(int) c_parser_peek_token (parser)->keyword];
3198 attr_name = c_parser_peek_token (parser)->value;
3199 c_parser_consume_token (parser);
3200 if (c_parser_next_token_is_not (parser, CPP_OPEN_PAREN))
3202 attr = build_tree_list (attr_name, NULL_TREE);
3203 attrs = chainon (attrs, attr);
3206 c_parser_consume_token (parser);
3207 /* Parse the attribute contents. If they start with an
3208 identifier which is followed by a comma or close
3209 parenthesis, then the arguments start with that
3210 identifier; otherwise they are an expression list. */
3211 if (c_parser_next_token_is (parser, CPP_NAME)
3212 && c_parser_peek_token (parser)->id_kind == C_ID_ID
3213 && ((c_parser_peek_2nd_token (parser)->type == CPP_COMMA)
3214 || (c_parser_peek_2nd_token (parser)->type
3215 == CPP_CLOSE_PAREN)))
3217 tree arg1 = c_parser_peek_token (parser)->value;
3218 c_parser_consume_token (parser);
3219 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3220 attr_args = build_tree_list (NULL_TREE, arg1);
3224 c_parser_consume_token (parser);
3225 expr_list = c_parser_expr_list (parser, false, true, NULL);
3226 tree_list = build_tree_list_vec (expr_list);
3227 attr_args = tree_cons (NULL_TREE, arg1, tree_list);
3228 release_tree_vector (expr_list);
3233 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3234 attr_args = NULL_TREE;
3237 expr_list = c_parser_expr_list (parser, false, true, NULL);
3238 attr_args = build_tree_list_vec (expr_list);
3239 release_tree_vector (expr_list);
3242 attr = build_tree_list (attr_name, attr_args);
3243 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3244 c_parser_consume_token (parser);
3247 parser->lex_untranslated_string = false;
3248 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3252 attrs = chainon (attrs, attr);
3254 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3255 c_parser_consume_token (parser);
3258 parser->lex_untranslated_string = false;
3259 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3263 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3264 c_parser_consume_token (parser);
3267 parser->lex_untranslated_string = false;
3268 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3272 parser->lex_untranslated_string = false;
3277 /* Parse a type name (C90 6.5.5, C99 6.7.6).
3280 specifier-qualifier-list abstract-declarator[opt]
3283 static struct c_type_name *
3284 c_parser_type_name (c_parser *parser)
3286 struct c_declspecs *specs = build_null_declspecs ();
3287 struct c_declarator *declarator;
3288 struct c_type_name *ret;
3290 c_parser_declspecs (parser, specs, false, true, true);
3291 if (!specs->declspecs_seen_p)
3293 c_parser_error (parser, "expected specifier-qualifier-list");
3296 pending_xref_error ();
3297 finish_declspecs (specs);
3298 declarator = c_parser_declarator (parser, specs->type_seen_p,
3299 C_DTR_ABSTRACT, &dummy);
3300 if (declarator == NULL)
3302 ret = XOBNEW (&parser_obstack, struct c_type_name);
3304 ret->declarator = declarator;
3308 /* Parse an initializer (C90 6.5.7, C99 6.7.8).
3311 assignment-expression
3312 { initializer-list }
3313 { initializer-list , }
3316 designation[opt] initializer
3317 initializer-list , designation[opt] initializer
3324 designator-list designator
3331 [ constant-expression ]
3343 [ constant-expression ... constant-expression ]
3345 Any expression without commas is accepted in the syntax for the
3346 constant-expressions, with non-constant expressions rejected later.
3348 This function is only used for top-level initializers; for nested
3349 ones, see c_parser_initval. */
3351 static struct c_expr
3352 c_parser_initializer (c_parser *parser)
3354 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
3355 return c_parser_braced_init (parser, NULL_TREE, false);
3359 location_t loc = c_parser_peek_token (parser)->location;
3360 ret = c_parser_expr_no_commas (parser, NULL);
3361 if (TREE_CODE (ret.value) != STRING_CST
3362 && TREE_CODE (ret.value) != COMPOUND_LITERAL_EXPR)
3363 ret = default_function_array_read_conversion (loc, ret);
3368 /* Parse a braced initializer list. TYPE is the type specified for a
3369 compound literal, and NULL_TREE for other initializers and for
3370 nested braced lists. NESTED_P is true for nested braced lists,
3371 false for the list of a compound literal or the list that is the
3372 top-level initializer in a declaration. */
3374 static struct c_expr
3375 c_parser_braced_init (c_parser *parser, tree type, bool nested_p)
3378 struct obstack braced_init_obstack;
3379 location_t brace_loc = c_parser_peek_token (parser)->location;
3380 gcc_obstack_init (&braced_init_obstack);
3381 gcc_assert (c_parser_next_token_is (parser, CPP_OPEN_BRACE));
3382 c_parser_consume_token (parser);
3384 push_init_level (0, &braced_init_obstack);
3386 really_start_incremental_init (type);
3387 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3389 pedwarn (brace_loc, OPT_pedantic, "ISO C forbids empty initializer braces");
3393 /* Parse a non-empty initializer list, possibly with a trailing
3397 c_parser_initelt (parser, &braced_init_obstack);
3400 if (c_parser_next_token_is (parser, CPP_COMMA))
3401 c_parser_consume_token (parser);
3404 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3408 if (c_parser_next_token_is_not (parser, CPP_CLOSE_BRACE))
3410 ret.value = error_mark_node;
3411 ret.original_code = ERROR_MARK;
3412 ret.original_type = NULL;
3413 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, "expected %<}%>");
3414 pop_init_level (0, &braced_init_obstack);
3415 obstack_free (&braced_init_obstack, NULL);
3418 c_parser_consume_token (parser);
3419 ret = pop_init_level (0, &braced_init_obstack);
3420 obstack_free (&braced_init_obstack, NULL);
3424 /* Parse a nested initializer, including designators. */
3427 c_parser_initelt (c_parser *parser, struct obstack * braced_init_obstack)
3429 /* Parse any designator or designator list. A single array
3430 designator may have the subsequent "=" omitted in GNU C, but a
3431 longer list or a structure member designator may not. */
3432 if (c_parser_next_token_is (parser, CPP_NAME)
3433 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
3435 /* Old-style structure member designator. */
3436 set_init_label (c_parser_peek_token (parser)->value,
3437 braced_init_obstack);
3438 /* Use the colon as the error location. */
3439 pedwarn (c_parser_peek_2nd_token (parser)->location, OPT_pedantic,
3440 "obsolete use of designated initializer with %<:%>");
3441 c_parser_consume_token (parser);
3442 c_parser_consume_token (parser);
3446 /* des_seen is 0 if there have been no designators, 1 if there
3447 has been a single array designator and 2 otherwise. */
3449 /* Location of a designator. */
3450 location_t des_loc = UNKNOWN_LOCATION; /* Quiet warning. */
3451 while (c_parser_next_token_is (parser, CPP_OPEN_SQUARE)
3452 || c_parser_next_token_is (parser, CPP_DOT))
3454 int des_prev = des_seen;
3456 des_loc = c_parser_peek_token (parser)->location;
3459 if (c_parser_next_token_is (parser, CPP_DOT))
3462 c_parser_consume_token (parser);
3463 if (c_parser_next_token_is (parser, CPP_NAME))
3465 set_init_label (c_parser_peek_token (parser)->value,
3466 braced_init_obstack);
3467 c_parser_consume_token (parser);
3472 init.value = error_mark_node;
3473 init.original_code = ERROR_MARK;
3474 init.original_type = NULL;
3475 c_parser_error (parser, "expected identifier");
3476 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
3477 process_init_element (init, false, braced_init_obstack);
3484 location_t ellipsis_loc = UNKNOWN_LOCATION; /* Quiet warning. */
3485 /* ??? Following the old parser, [ objc-receiver
3486 objc-message-args ] is accepted as an initializer,
3487 being distinguished from a designator by what follows
3488 the first assignment expression inside the square
3489 brackets, but after a first array designator a
3490 subsequent square bracket is for Objective-C taken to
3491 start an expression, using the obsolete form of
3492 designated initializer without '=', rather than
3493 possibly being a second level of designation: in LALR
3494 terms, the '[' is shifted rather than reducing
3495 designator to designator-list. */
3496 if (des_prev == 1 && c_dialect_objc ())
3498 des_seen = des_prev;
3501 if (des_prev == 0 && c_dialect_objc ())
3503 /* This might be an array designator or an
3504 Objective-C message expression. If the former,
3505 continue parsing here; if the latter, parse the
3506 remainder of the initializer given the starting
3507 primary-expression. ??? It might make sense to
3508 distinguish when des_prev == 1 as well; see
3509 previous comment. */
3511 struct c_expr mexpr;
3512 c_parser_consume_token (parser);
3513 if (c_parser_peek_token (parser)->type == CPP_NAME
3514 && ((c_parser_peek_token (parser)->id_kind
3516 || (c_parser_peek_token (parser)->id_kind
3517 == C_ID_CLASSNAME)))
3519 /* Type name receiver. */
3520 tree id = c_parser_peek_token (parser)->value;
3521 c_parser_consume_token (parser);
3522 rec = objc_get_class_reference (id);
3523 goto parse_message_args;
3525 first = c_parser_expr_no_commas (parser, NULL).value;
3526 mark_exp_read (first);
3527 if (c_parser_next_token_is (parser, CPP_ELLIPSIS)
3528 || c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
3529 goto array_desig_after_first;
3530 /* Expression receiver. So far only one part
3531 without commas has been parsed; there might be
3532 more of the expression. */
3534 while (c_parser_next_token_is (parser, CPP_COMMA))
3537 location_t comma_loc, exp_loc;
3538 comma_loc = c_parser_peek_token (parser)->location;
3539 c_parser_consume_token (parser);
3540 exp_loc = c_parser_peek_token (parser)->location;
3541 next = c_parser_expr_no_commas (parser, NULL);
3542 next = default_function_array_read_conversion (exp_loc,
3544 rec = build_compound_expr (comma_loc, rec, next.value);
3547 /* Now parse the objc-message-args. */
3548 args = c_parser_objc_message_args (parser);
3549 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
3552 = objc_build_message_expr (build_tree_list (rec, args));
3553 mexpr.original_code = ERROR_MARK;
3554 mexpr.original_type = NULL;
3555 /* Now parse and process the remainder of the
3556 initializer, starting with this message
3557 expression as a primary-expression. */
3558 c_parser_initval (parser, &mexpr, braced_init_obstack);
3561 c_parser_consume_token (parser);
3562 first = c_parser_expr_no_commas (parser, NULL).value;
3563 mark_exp_read (first);
3564 array_desig_after_first:
3565 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
3567 ellipsis_loc = c_parser_peek_token (parser)->location;
3568 c_parser_consume_token (parser);
3569 second = c_parser_expr_no_commas (parser, NULL).value;
3570 mark_exp_read (second);
3574 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
3576 c_parser_consume_token (parser);
3577 set_init_index (first, second, braced_init_obstack);
3579 pedwarn (ellipsis_loc, OPT_pedantic,
3580 "ISO C forbids specifying range of elements to initialize");
3583 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
3589 if (c_parser_next_token_is (parser, CPP_EQ))
3592 pedwarn (des_loc, OPT_pedantic,
3593 "ISO C90 forbids specifying subobject to initialize");
3594 c_parser_consume_token (parser);
3599 pedwarn (c_parser_peek_token (parser)->location, OPT_pedantic,
3600 "obsolete use of designated initializer without %<=%>");
3604 init.value = error_mark_node;
3605 init.original_code = ERROR_MARK;
3606 init.original_type = NULL;
3607 c_parser_error (parser, "expected %<=%>");
3608 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
3609 process_init_element (init, false, braced_init_obstack);
3615 c_parser_initval (parser, NULL, braced_init_obstack);
3618 /* Parse a nested initializer; as c_parser_initializer but parses
3619 initializers within braced lists, after any designators have been
3620 applied. If AFTER is not NULL then it is an Objective-C message
3621 expression which is the primary-expression starting the
3625 c_parser_initval (c_parser *parser, struct c_expr *after,
3626 struct obstack * braced_init_obstack)
3629 gcc_assert (!after || c_dialect_objc ());
3630 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE) && !after)
3631 init = c_parser_braced_init (parser, NULL_TREE, true);
3634 location_t loc = c_parser_peek_token (parser)->location;
3635 init = c_parser_expr_no_commas (parser, after);
3636 if (init.value != NULL_TREE
3637 && TREE_CODE (init.value) != STRING_CST
3638 && TREE_CODE (init.value) != COMPOUND_LITERAL_EXPR)
3639 init = default_function_array_read_conversion (loc, init);
3641 process_init_element (init, false, braced_init_obstack);
3644 /* Parse a compound statement (possibly a function body) (C90 6.6.2,
3648 { block-item-list[opt] }
3649 { label-declarations block-item-list }
3653 block-item-list block-item
3665 { label-declarations block-item-list }
3668 __extension__ nested-declaration
3669 nested-function-definition
3673 label-declarations label-declaration
3676 __label__ identifier-list ;
3678 Allowing the mixing of declarations and code is new in C99. The
3679 GNU syntax also permits (not shown above) labels at the end of
3680 compound statements, which yield an error. We don't allow labels
3681 on declarations; this might seem like a natural extension, but
3682 there would be a conflict between attributes on the label and
3683 prefix attributes on the declaration. ??? The syntax follows the
3684 old parser in requiring something after label declarations.
3685 Although they are erroneous if the labels declared aren't defined,
3686 is it useful for the syntax to be this way?
3698 c_parser_compound_statement (c_parser *parser)
3701 location_t brace_loc;
3702 brace_loc = c_parser_peek_token (parser)->location;
3703 if (!c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
3705 /* Ensure a scope is entered and left anyway to avoid confusion
3706 if we have just prepared to enter a function body. */
3707 stmt = c_begin_compound_stmt (true);
3708 c_end_compound_stmt (brace_loc, stmt, true);
3709 return error_mark_node;
3711 stmt = c_begin_compound_stmt (true);
3712 c_parser_compound_statement_nostart (parser);
3713 return c_end_compound_stmt (brace_loc, stmt, true);
3716 /* Parse a compound statement except for the opening brace. This is
3717 used for parsing both compound statements and statement expressions
3718 (which follow different paths to handling the opening). */
3721 c_parser_compound_statement_nostart (c_parser *parser)
3723 bool last_stmt = false;
3724 bool last_label = false;
3725 bool save_valid_for_pragma = valid_location_for_stdc_pragma_p ();
3726 location_t label_loc = UNKNOWN_LOCATION; /* Quiet warning. */
3727 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3729 c_parser_consume_token (parser);
3732 mark_valid_location_for_stdc_pragma (true);
3733 if (c_parser_next_token_is_keyword (parser, RID_LABEL))
3735 /* Read zero or more forward-declarations for labels that nested
3736 functions can jump to. */
3737 mark_valid_location_for_stdc_pragma (false);
3738 while (c_parser_next_token_is_keyword (parser, RID_LABEL))
3740 label_loc = c_parser_peek_token (parser)->location;
3741 c_parser_consume_token (parser);
3742 /* Any identifiers, including those declared as type names,
3747 if (c_parser_next_token_is_not (parser, CPP_NAME))
3749 c_parser_error (parser, "expected identifier");
3753 = declare_label (c_parser_peek_token (parser)->value);
3754 C_DECLARED_LABEL_FLAG (label) = 1;
3755 add_stmt (build_stmt (label_loc, DECL_EXPR, label));
3756 c_parser_consume_token (parser);
3757 if (c_parser_next_token_is (parser, CPP_COMMA))
3758 c_parser_consume_token (parser);
3762 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
3764 pedwarn (label_loc, OPT_pedantic, "ISO C forbids label declarations");
3766 /* We must now have at least one statement, label or declaration. */
3767 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3769 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
3770 c_parser_error (parser, "expected declaration or statement");
3771 c_parser_consume_token (parser);
3774 while (c_parser_next_token_is_not (parser, CPP_CLOSE_BRACE))
3776 location_t loc = c_parser_peek_token (parser)->location;
3777 if (c_parser_next_token_is_keyword (parser, RID_CASE)
3778 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
3779 || (c_parser_next_token_is (parser, CPP_NAME)
3780 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
3782 if (c_parser_next_token_is_keyword (parser, RID_CASE))
3783 label_loc = c_parser_peek_2nd_token (parser)->location;
3785 label_loc = c_parser_peek_token (parser)->location;
3788 mark_valid_location_for_stdc_pragma (false);
3789 c_parser_label (parser);
3791 else if (!last_label
3792 && c_parser_next_token_starts_declaration (parser))
3795 mark_valid_location_for_stdc_pragma (false);
3796 c_parser_declaration_or_fndef (parser, true, true, true, true, true, NULL);
3799 (pedantic && !flag_isoc99)
3801 : OPT_Wdeclaration_after_statement,
3802 "ISO C90 forbids mixed declarations and code");
3805 else if (!last_label
3806 && c_parser_next_token_is_keyword (parser, RID_EXTENSION))
3808 /* __extension__ can start a declaration, but is also an
3809 unary operator that can start an expression. Consume all
3810 but the last of a possible series of __extension__ to
3812 while (c_parser_peek_2nd_token (parser)->type == CPP_KEYWORD
3813 && (c_parser_peek_2nd_token (parser)->keyword
3815 c_parser_consume_token (parser);
3816 if (c_token_starts_declaration (c_parser_peek_2nd_token (parser)))
3819 ext = disable_extension_diagnostics ();
3820 c_parser_consume_token (parser);
3822 mark_valid_location_for_stdc_pragma (false);
3823 c_parser_declaration_or_fndef (parser, true, true, true, true,
3825 /* Following the old parser, __extension__ does not
3826 disable this diagnostic. */
3827 restore_extension_diagnostics (ext);
3829 pedwarn_c90 (loc, (pedantic && !flag_isoc99)
3831 : OPT_Wdeclaration_after_statement,
3832 "ISO C90 forbids mixed declarations and code");
3838 else if (c_parser_next_token_is (parser, CPP_PRAGMA))
3840 /* External pragmas, and some omp pragmas, are not associated
3841 with regular c code, and so are not to be considered statements
3842 syntactically. This ensures that the user doesn't put them
3843 places that would turn into syntax errors if the directive
3845 if (c_parser_pragma (parser, pragma_compound))
3846 last_label = false, last_stmt = true;
3848 else if (c_parser_next_token_is (parser, CPP_EOF))
3850 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
3851 c_parser_error (parser, "expected declaration or statement");
3854 else if (c_parser_next_token_is_keyword (parser, RID_ELSE))
3856 if (parser->in_if_block)
3858 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
3859 error_at (loc, """expected %<}%> before %<else%>");
3864 error_at (loc, "%<else%> without a previous %<if%>");
3865 c_parser_consume_token (parser);
3874 mark_valid_location_for_stdc_pragma (false);
3875 c_parser_statement_after_labels (parser);
3878 parser->error = false;
3881 error_at (label_loc, "label at end of compound statement");
3882 c_parser_consume_token (parser);
3883 /* Restore the value we started with. */
3884 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
3887 /* Parse a label (C90 6.6.1, C99 6.8.1).
3890 identifier : attributes[opt]
3891 case constant-expression :
3897 case constant-expression ... constant-expression :
3899 The use of attributes on labels is a GNU extension. The syntax in
3900 GNU C accepts any expressions without commas, non-constant
3901 expressions being rejected later. */
3904 c_parser_label (c_parser *parser)
3906 location_t loc1 = c_parser_peek_token (parser)->location;
3907 tree label = NULL_TREE;
3908 if (c_parser_next_token_is_keyword (parser, RID_CASE))
3911 c_parser_consume_token (parser);
3912 exp1 = c_parser_expr_no_commas (parser, NULL).value;
3913 if (c_parser_next_token_is (parser, CPP_COLON))
3915 c_parser_consume_token (parser);
3916 label = do_case (loc1, exp1, NULL_TREE);
3918 else if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
3920 c_parser_consume_token (parser);
3921 exp2 = c_parser_expr_no_commas (parser, NULL).value;
3922 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
3923 label = do_case (loc1, exp1, exp2);
3926 c_parser_error (parser, "expected %<:%> or %<...%>");
3928 else if (c_parser_next_token_is_keyword (parser, RID_DEFAULT))
3930 c_parser_consume_token (parser);
3931 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
3932 label = do_case (loc1, NULL_TREE, NULL_TREE);
3936 tree name = c_parser_peek_token (parser)->value;
3939 location_t loc2 = c_parser_peek_token (parser)->location;
3940 gcc_assert (c_parser_next_token_is (parser, CPP_NAME));
3941 c_parser_consume_token (parser);
3942 gcc_assert (c_parser_next_token_is (parser, CPP_COLON));
3943 c_parser_consume_token (parser);
3944 attrs = c_parser_attributes (parser);
3945 tlab = define_label (loc2, name);
3948 decl_attributes (&tlab, attrs, 0);
3949 label = add_stmt (build_stmt (loc1, LABEL_EXPR, tlab));
3954 if (c_parser_next_token_starts_declaration (parser)
3955 && !(c_parser_next_token_is (parser, CPP_NAME)
3956 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
3958 error_at (c_parser_peek_token (parser)->location,
3959 "a label can only be part of a statement and "
3960 "a declaration is not a statement");
3961 c_parser_declaration_or_fndef (parser, /*fndef_ok*/ false,
3962 /*static_assert_ok*/ true,
3963 /*nested*/ true, /*empty_ok*/ false,
3964 /*start_attr_ok*/ true, NULL);
3969 /* Parse a statement (C90 6.6, C99 6.8).
3974 expression-statement
3982 expression-statement:
3985 selection-statement:
3989 iteration-statement:
3998 return expression[opt] ;
4011 objc-throw-statement
4012 objc-try-catch-statement
4013 objc-synchronized-statement
4015 objc-throw-statement:
4029 parallel-for-construct
4030 parallel-sections-construct
4037 parallel-directive structured-block
4040 for-directive iteration-statement
4043 sections-directive section-scope
4046 single-directive structured-block
4048 parallel-for-construct:
4049 parallel-for-directive iteration-statement
4051 parallel-sections-construct:
4052 parallel-sections-directive section-scope
4055 master-directive structured-block
4058 critical-directive structured-block
4061 atomic-directive expression-statement
4064 ordered-directive structured-block */
4067 c_parser_statement (c_parser *parser)
4069 while (c_parser_next_token_is_keyword (parser, RID_CASE)
4070 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
4071 || (c_parser_next_token_is (parser, CPP_NAME)
4072 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
4073 c_parser_label (parser);
4074 c_parser_statement_after_labels (parser);
4077 /* Parse a statement, other than a labeled statement. */
4080 c_parser_statement_after_labels (c_parser *parser)
4082 location_t loc = c_parser_peek_token (parser)->location;
4083 tree stmt = NULL_TREE;
4084 bool in_if_block = parser->in_if_block;
4085 parser->in_if_block = false;
4086 switch (c_parser_peek_token (parser)->type)
4088 case CPP_OPEN_BRACE:
4089 add_stmt (c_parser_compound_statement (parser));
4092 switch (c_parser_peek_token (parser)->keyword)
4095 c_parser_if_statement (parser);
4098 c_parser_switch_statement (parser);
4101 c_parser_while_statement (parser);
4104 c_parser_do_statement (parser);
4107 c_parser_for_statement (parser);
4110 c_parser_consume_token (parser);
4111 if (c_parser_next_token_is (parser, CPP_NAME))
4113 stmt = c_finish_goto_label (loc,
4114 c_parser_peek_token (parser)->value);
4115 c_parser_consume_token (parser);
4117 else if (c_parser_next_token_is (parser, CPP_MULT))
4119 c_parser_consume_token (parser);
4120 stmt = c_finish_goto_ptr (loc,
4121 c_parser_expression (parser).value);
4124 c_parser_error (parser, "expected identifier or %<*%>");
4125 goto expect_semicolon;
4127 c_parser_consume_token (parser);
4128 stmt = c_finish_bc_stmt (loc, &c_cont_label, false);
4129 goto expect_semicolon;
4131 c_parser_consume_token (parser);
4132 stmt = c_finish_bc_stmt (loc, &c_break_label, true);
4133 goto expect_semicolon;
4135 c_parser_consume_token (parser);
4136 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
4138 stmt = c_finish_return (loc, NULL_TREE, NULL_TREE);
4139 c_parser_consume_token (parser);
4143 struct c_expr expr = c_parser_expression_conv (parser);
4144 mark_exp_read (expr.value);
4145 stmt = c_finish_return (loc, expr.value, expr.original_type);
4146 goto expect_semicolon;
4150 stmt = c_parser_asm_statement (parser);
4153 gcc_assert (c_dialect_objc ());
4154 c_parser_consume_token (parser);
4155 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
4157 stmt = objc_build_throw_stmt (loc, NULL_TREE);
4158 c_parser_consume_token (parser);
4162 tree expr = c_parser_expression (parser).value;
4163 expr = c_fully_fold (expr, false, NULL);
4164 stmt = objc_build_throw_stmt (loc, expr);
4165 goto expect_semicolon;
4169 gcc_assert (c_dialect_objc ());
4170 c_parser_objc_try_catch_statement (parser);
4172 case RID_AT_SYNCHRONIZED:
4173 gcc_assert (c_dialect_objc ());
4174 c_parser_objc_synchronized_statement (parser);
4181 c_parser_consume_token (parser);
4183 case CPP_CLOSE_PAREN:
4184 case CPP_CLOSE_SQUARE:
4185 /* Avoid infinite loop in error recovery:
4186 c_parser_skip_until_found stops at a closing nesting
4187 delimiter without consuming it, but here we need to consume
4188 it to proceed further. */
4189 c_parser_error (parser, "expected statement");
4190 c_parser_consume_token (parser);
4193 c_parser_pragma (parser, pragma_stmt);
4197 stmt = c_finish_expr_stmt (loc, c_parser_expression_conv (parser).value);
4199 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
4202 /* Two cases cannot and do not have line numbers associated: If stmt
4203 is degenerate, such as "2;", then stmt is an INTEGER_CST, which
4204 cannot hold line numbers. But that's OK because the statement
4205 will either be changed to a MODIFY_EXPR during gimplification of
4206 the statement expr, or discarded. If stmt was compound, but
4207 without new variables, we will have skipped the creation of a
4208 BIND and will have a bare STATEMENT_LIST. But that's OK because
4209 (recursively) all of the component statements should already have
4210 line numbers assigned. ??? Can we discard no-op statements
4212 if (CAN_HAVE_LOCATION_P (stmt)
4213 && EXPR_LOCATION (stmt) == UNKNOWN_LOCATION)
4214 SET_EXPR_LOCATION (stmt, loc);
4216 parser->in_if_block = in_if_block;
4219 /* Parse the condition from an if, do, while or for statements. */
4222 c_parser_condition (c_parser *parser)
4224 location_t loc = c_parser_peek_token (parser)->location;
4226 cond = c_parser_expression_conv (parser).value;
4227 cond = c_objc_common_truthvalue_conversion (loc, cond);
4228 cond = c_fully_fold (cond, false, NULL);
4229 if (warn_sequence_point)
4230 verify_sequence_points (cond);
4234 /* Parse a parenthesized condition from an if, do or while statement.
4240 c_parser_paren_condition (c_parser *parser)
4243 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4244 return error_mark_node;
4245 cond = c_parser_condition (parser);
4246 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
4250 /* Parse a statement which is a block in C99. */
4253 c_parser_c99_block_statement (c_parser *parser)
4255 tree block = c_begin_compound_stmt (flag_isoc99);
4256 location_t loc = c_parser_peek_token (parser)->location;
4257 c_parser_statement (parser);
4258 return c_end_compound_stmt (loc, block, flag_isoc99);
4261 /* Parse the body of an if statement. This is just parsing a
4262 statement but (a) it is a block in C99, (b) we track whether the
4263 body is an if statement for the sake of -Wparentheses warnings, (c)
4264 we handle an empty body specially for the sake of -Wempty-body
4265 warnings, and (d) we call parser_compound_statement directly
4266 because c_parser_statement_after_labels resets
4267 parser->in_if_block. */
4270 c_parser_if_body (c_parser *parser, bool *if_p)
4272 tree block = c_begin_compound_stmt (flag_isoc99);
4273 location_t body_loc = c_parser_peek_token (parser)->location;
4274 while (c_parser_next_token_is_keyword (parser, RID_CASE)
4275 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
4276 || (c_parser_next_token_is (parser, CPP_NAME)
4277 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
4278 c_parser_label (parser);
4279 *if_p = c_parser_next_token_is_keyword (parser, RID_IF);
4280 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
4282 location_t loc = c_parser_peek_token (parser)->location;
4283 add_stmt (build_empty_stmt (loc));
4284 c_parser_consume_token (parser);
4285 if (!c_parser_next_token_is_keyword (parser, RID_ELSE))
4286 warning_at (loc, OPT_Wempty_body,
4287 "suggest braces around empty body in an %<if%> statement");
4289 else if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
4290 add_stmt (c_parser_compound_statement (parser));
4292 c_parser_statement_after_labels (parser);
4293 return c_end_compound_stmt (body_loc, block, flag_isoc99);
4296 /* Parse the else body of an if statement. This is just parsing a
4297 statement but (a) it is a block in C99, (b) we handle an empty body
4298 specially for the sake of -Wempty-body warnings. */
4301 c_parser_else_body (c_parser *parser)
4303 location_t else_loc = c_parser_peek_token (parser)->location;
4304 tree block = c_begin_compound_stmt (flag_isoc99);
4305 while (c_parser_next_token_is_keyword (parser, RID_CASE)
4306 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
4307 || (c_parser_next_token_is (parser, CPP_NAME)
4308 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
4309 c_parser_label (parser);
4310 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
4312 location_t loc = c_parser_peek_token (parser)->location;
4315 "suggest braces around empty body in an %<else%> statement");
4316 add_stmt (build_empty_stmt (loc));
4317 c_parser_consume_token (parser);
4320 c_parser_statement_after_labels (parser);
4321 return c_end_compound_stmt (else_loc, block, flag_isoc99);
4324 /* Parse an if statement (C90 6.6.4, C99 6.8.4).
4327 if ( expression ) statement
4328 if ( expression ) statement else statement
4332 c_parser_if_statement (c_parser *parser)
4337 bool first_if = false;
4338 tree first_body, second_body;
4341 gcc_assert (c_parser_next_token_is_keyword (parser, RID_IF));
4342 c_parser_consume_token (parser);
4343 block = c_begin_compound_stmt (flag_isoc99);
4344 loc = c_parser_peek_token (parser)->location;
4345 cond = c_parser_paren_condition (parser);
4346 in_if_block = parser->in_if_block;
4347 parser->in_if_block = true;
4348 first_body = c_parser_if_body (parser, &first_if);
4349 parser->in_if_block = in_if_block;
4350 if (c_parser_next_token_is_keyword (parser, RID_ELSE))
4352 c_parser_consume_token (parser);
4353 second_body = c_parser_else_body (parser);
4356 second_body = NULL_TREE;
4357 c_finish_if_stmt (loc, cond, first_body, second_body, first_if);
4358 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99));
4361 /* Parse a switch statement (C90 6.6.4, C99 6.8.4).
4364 switch (expression) statement
4368 c_parser_switch_statement (c_parser *parser)
4370 tree block, expr, body, save_break;
4371 location_t switch_loc = c_parser_peek_token (parser)->location;
4372 location_t switch_cond_loc;
4373 gcc_assert (c_parser_next_token_is_keyword (parser, RID_SWITCH));
4374 c_parser_consume_token (parser);
4375 block = c_begin_compound_stmt (flag_isoc99);
4376 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4378 switch_cond_loc = c_parser_peek_token (parser)->location;
4379 expr = c_parser_expression (parser).value;
4380 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
4384 switch_cond_loc = UNKNOWN_LOCATION;
4385 expr = error_mark_node;
4387 c_start_case (switch_loc, switch_cond_loc, expr);
4388 save_break = c_break_label;
4389 c_break_label = NULL_TREE;
4390 body = c_parser_c99_block_statement (parser);
4391 c_finish_case (body);
4394 location_t here = c_parser_peek_token (parser)->location;
4395 tree t = build1 (LABEL_EXPR, void_type_node, c_break_label);
4396 SET_EXPR_LOCATION (t, here);
4399 c_break_label = save_break;
4400 add_stmt (c_end_compound_stmt (switch_loc, block, flag_isoc99));
4403 /* Parse a while statement (C90 6.6.5, C99 6.8.5).
4406 while (expression) statement
4410 c_parser_while_statement (c_parser *parser)
4412 tree block, cond, body, save_break, save_cont;
4414 gcc_assert (c_parser_next_token_is_keyword (parser, RID_WHILE));
4415 c_parser_consume_token (parser);
4416 block = c_begin_compound_stmt (flag_isoc99);
4417 loc = c_parser_peek_token (parser)->location;
4418 cond = c_parser_paren_condition (parser);
4419 save_break = c_break_label;
4420 c_break_label = NULL_TREE;
4421 save_cont = c_cont_label;
4422 c_cont_label = NULL_TREE;
4423 body = c_parser_c99_block_statement (parser);
4424 c_finish_loop (loc, cond, NULL, body, c_break_label, c_cont_label, true);
4425 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99));
4426 c_break_label = save_break;
4427 c_cont_label = save_cont;
4430 /* Parse a do statement (C90 6.6.5, C99 6.8.5).
4433 do statement while ( expression ) ;
4437 c_parser_do_statement (c_parser *parser)
4439 tree block, cond, body, save_break, save_cont, new_break, new_cont;
4441 gcc_assert (c_parser_next_token_is_keyword (parser, RID_DO));
4442 c_parser_consume_token (parser);
4443 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
4444 warning_at (c_parser_peek_token (parser)->location,
4446 "suggest braces around empty body in %<do%> statement");
4447 block = c_begin_compound_stmt (flag_isoc99);
4448 loc = c_parser_peek_token (parser)->location;
4449 save_break = c_break_label;
4450 c_break_label = NULL_TREE;
4451 save_cont = c_cont_label;
4452 c_cont_label = NULL_TREE;
4453 body = c_parser_c99_block_statement (parser);
4454 c_parser_require_keyword (parser, RID_WHILE, "expected %<while%>");
4455 new_break = c_break_label;
4456 c_break_label = save_break;
4457 new_cont = c_cont_label;
4458 c_cont_label = save_cont;
4459 cond = c_parser_paren_condition (parser);
4460 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
4461 c_parser_skip_to_end_of_block_or_statement (parser);
4462 c_finish_loop (loc, cond, NULL, body, new_break, new_cont, false);
4463 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99));
4466 /* Parse a for statement (C90 6.6.5, C99 6.8.5).
4469 for ( expression[opt] ; expression[opt] ; expression[opt] ) statement
4470 for ( nested-declaration expression[opt] ; expression[opt] ) statement
4472 The form with a declaration is new in C99.
4474 ??? In accordance with the old parser, the declaration may be a
4475 nested function, which is then rejected in check_for_loop_decls,
4476 but does it make any sense for this to be included in the grammar?
4477 Note in particular that the nested function does not include a
4478 trailing ';', whereas the "declaration" production includes one.
4479 Also, can we reject bad declarations earlier and cheaper than
4480 check_for_loop_decls?
4482 In Objective-C, there are two additional variants:
4485 for ( expression in expresssion ) statement
4486 for ( declaration in expression ) statement
4488 This is inconsistent with C, because the second variant is allowed
4489 even if c99 is not enabled.
4491 The rest of the comment documents these Objective-C foreach-statement.
4493 Here is the canonical example of the first variant:
4494 for (object in array) { do something with object }
4495 we call the first expression ("object") the "object_expression" and
4496 the second expression ("array") the "collection_expression".
4497 object_expression must be an lvalue of type "id" (a generic Objective-C
4498 object) because the loop works by assigning to object_expression the
4499 various objects from the collection_expression. collection_expression
4500 must evaluate to something of type "id" which responds to the method
4501 countByEnumeratingWithState:objects:count:.
4503 The canonical example of the second variant is:
4504 for (id object in array) { do something with object }
4505 which is completely equivalent to
4508 for (object in array) { do something with object }
4510 Note that initizializing 'object' in some way (eg, "for ((object =
4511 xxx) in array) { do something with object }") is possibly
4512 technically valid, but completely pointless as 'object' will be
4513 assigned to something else as soon as the loop starts. We should
4514 most likely reject it (TODO).
4516 The beginning of the Objective-C foreach-statement looks exactly
4517 like the beginning of the for-statement, and we can tell it is a
4518 foreach-statement only because the initial declaration or
4519 expression is terminated by 'in' instead of ';'.
4523 c_parser_for_statement (c_parser *parser)
4525 tree block, cond, incr, save_break, save_cont, body;
4526 /* The following are only used when parsing an ObjC foreach statement. */
4527 tree object_expression, collection_expression;
4528 location_t loc = c_parser_peek_token (parser)->location;
4529 location_t for_loc = c_parser_peek_token (parser)->location;
4530 bool is_foreach_statement = false;
4531 gcc_assert (c_parser_next_token_is_keyword (parser, RID_FOR));
4532 c_parser_consume_token (parser);
4533 /* Open a compound statement in Objective-C as well, just in case this is
4534 as foreach expression. */
4535 block = c_begin_compound_stmt (flag_isoc99 || c_dialect_objc ());
4536 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4538 /* Parse the initialization declaration or expression. */
4539 cond = error_mark_node;
4540 object_expression = error_mark_node;
4541 /* Initializing incr should not be necessary, but it avoids
4542 bogus warnings of uninitialized uses. */
4543 incr = error_mark_node;
4545 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
4547 c_parser_consume_token (parser);
4548 c_finish_expr_stmt (loc, NULL_TREE);
4550 else if (c_parser_next_token_starts_declaration (parser))
4552 parser->objc_could_be_foreach_context = true;
4553 c_parser_declaration_or_fndef (parser, true, true, true, true, true,
4554 &object_expression);
4555 parser->objc_could_be_foreach_context = false;
4557 if (c_parser_next_token_is_keyword (parser, RID_IN))
4559 c_parser_consume_token (parser);
4560 is_foreach_statement = true;
4561 if (check_for_loop_decls (for_loc, true) == NULL_TREE)
4562 c_parser_error (parser, "multiple iterating variables in fast enumeration");
4565 check_for_loop_decls (for_loc, flag_isoc99);
4567 else if (c_parser_next_token_is_keyword (parser, RID_EXTENSION))
4569 /* __extension__ can start a declaration, but is also an
4570 unary operator that can start an expression. Consume all
4571 but the last of a possible series of __extension__ to
4573 while (c_parser_peek_2nd_token (parser)->type == CPP_KEYWORD
4574 && (c_parser_peek_2nd_token (parser)->keyword
4576 c_parser_consume_token (parser);
4577 if (c_token_starts_declaration (c_parser_peek_2nd_token (parser)))
4580 ext = disable_extension_diagnostics ();
4581 c_parser_consume_token (parser);
4582 parser->objc_could_be_foreach_context = true;
4583 c_parser_declaration_or_fndef (parser, true, true, true, true,
4584 true, &object_expression);
4585 parser->objc_could_be_foreach_context = false;
4587 restore_extension_diagnostics (ext);
4588 if (c_parser_next_token_is_keyword (parser, RID_IN))
4590 c_parser_consume_token (parser);
4591 is_foreach_statement = true;
4592 if (check_for_loop_decls (for_loc, true) == NULL_TREE)
4593 c_parser_error (parser, "multiple iterating variables in fast enumeration");
4596 check_for_loop_decls (for_loc, flag_isoc99);
4605 tree init_expression;
4606 parser->objc_could_be_foreach_context = true;
4607 init_expression = c_parser_expression (parser).value;
4608 parser->objc_could_be_foreach_context = false;
4609 if (c_parser_next_token_is_keyword (parser, RID_IN))
4611 c_parser_consume_token (parser);
4612 is_foreach_statement = true;
4613 if (! lvalue_p (init_expression))
4614 c_parser_error (parser, "invalid iterating variable in fast enumeration");
4615 object_expression = c_process_expr_stmt (loc, init_expression);
4620 c_finish_expr_stmt (loc, init_expression);
4621 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
4625 /* Parse the loop condition. In the case of a foreach
4626 statement, there is no loop condition. */
4627 if (!is_foreach_statement)
4629 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
4631 c_parser_consume_token (parser);
4636 cond = c_parser_condition (parser);
4637 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
4640 /* Parse the increment expression (the third expression in a
4641 for-statement). In the case of a foreach-statement, this is
4642 the expression that follows the 'in'. */
4643 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4645 if (is_foreach_statement)
4647 c_parser_error (parser, "missing collection in fast enumeration");
4648 collection_expression = error_mark_node;
4651 incr = c_process_expr_stmt (loc, NULL_TREE);
4655 if (is_foreach_statement)
4656 collection_expression = c_process_expr_stmt (loc, c_parser_expression (parser).value);
4658 incr = c_process_expr_stmt (loc, c_parser_expression (parser).value);
4660 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
4664 cond = error_mark_node;
4665 incr = error_mark_node;
4667 save_break = c_break_label;
4668 c_break_label = NULL_TREE;
4669 save_cont = c_cont_label;
4670 c_cont_label = NULL_TREE;
4671 body = c_parser_c99_block_statement (parser);
4672 if (is_foreach_statement)
4673 objc_finish_foreach_loop (loc, object_expression, collection_expression, body, c_break_label, c_cont_label);
4675 c_finish_loop (loc, cond, incr, body, c_break_label, c_cont_label, true);
4676 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99 || c_dialect_objc ()));
4677 c_break_label = save_break;
4678 c_cont_label = save_cont;
4681 /* Parse an asm statement, a GNU extension. This is a full-blown asm
4682 statement with inputs, outputs, clobbers, and volatile tag
4686 asm type-qualifier[opt] ( asm-argument ) ;
4687 asm type-qualifier[opt] goto ( asm-goto-argument ) ;
4691 asm-string-literal : asm-operands[opt]
4692 asm-string-literal : asm-operands[opt] : asm-operands[opt]
4693 asm-string-literal : asm-operands[opt] : asm-operands[opt] : asm-clobbers[opt]
4696 asm-string-literal : : asm-operands[opt] : asm-clobbers[opt] \
4699 Qualifiers other than volatile are accepted in the syntax but
4703 c_parser_asm_statement (c_parser *parser)
4705 tree quals, str, outputs, inputs, clobbers, labels, ret;
4706 bool simple, is_goto;
4707 location_t asm_loc = c_parser_peek_token (parser)->location;
4708 int section, nsections;
4710 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ASM));
4711 c_parser_consume_token (parser);
4712 if (c_parser_next_token_is_keyword (parser, RID_VOLATILE))
4714 quals = c_parser_peek_token (parser)->value;
4715 c_parser_consume_token (parser);
4717 else if (c_parser_next_token_is_keyword (parser, RID_CONST)
4718 || c_parser_next_token_is_keyword (parser, RID_RESTRICT))
4720 warning_at (c_parser_peek_token (parser)->location,
4722 "%E qualifier ignored on asm",
4723 c_parser_peek_token (parser)->value);
4725 c_parser_consume_token (parser);
4731 if (c_parser_next_token_is_keyword (parser, RID_GOTO))
4733 c_parser_consume_token (parser);
4737 /* ??? Follow the C++ parser rather than using the
4738 lex_untranslated_string kludge. */
4739 parser->lex_untranslated_string = true;
4742 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4745 str = c_parser_asm_string_literal (parser);
4746 if (str == NULL_TREE)
4747 goto error_close_paren;
4750 outputs = NULL_TREE;
4752 clobbers = NULL_TREE;
4755 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN) && !is_goto)
4758 /* Parse each colon-delimited section of operands. */
4759 nsections = 3 + is_goto;
4760 for (section = 0; section < nsections; ++section)
4762 if (!c_parser_require (parser, CPP_COLON,
4765 : "expected %<:%> or %<)%>"))
4766 goto error_close_paren;
4768 /* Once past any colon, we're no longer a simple asm. */
4771 if ((!c_parser_next_token_is (parser, CPP_COLON)
4772 && !c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4777 /* For asm goto, we don't allow output operands, but reserve
4778 the slot for a future extension that does allow them. */
4780 outputs = c_parser_asm_operands (parser, false);
4783 inputs = c_parser_asm_operands (parser, true);
4786 clobbers = c_parser_asm_clobbers (parser);
4789 labels = c_parser_asm_goto_operands (parser);
4795 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN) && !is_goto)
4800 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
4802 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
4806 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
4807 c_parser_skip_to_end_of_block_or_statement (parser);
4809 ret = build_asm_stmt (quals, build_asm_expr (asm_loc, str, outputs, inputs,
4810 clobbers, labels, simple));
4813 parser->lex_untranslated_string = false;
4817 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
4821 /* Parse asm operands, a GNU extension. If CONVERT_P (for inputs but
4822 not outputs), apply the default conversion of functions and arrays
4827 asm-operands , asm-operand
4830 asm-string-literal ( expression )
4831 [ identifier ] asm-string-literal ( expression )
4835 c_parser_asm_operands (c_parser *parser, bool convert_p)
4837 tree list = NULL_TREE;
4843 if (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
4845 c_parser_consume_token (parser);
4846 if (c_parser_next_token_is (parser, CPP_NAME))
4848 tree id = c_parser_peek_token (parser)->value;
4849 c_parser_consume_token (parser);
4850 name = build_string (IDENTIFIER_LENGTH (id),
4851 IDENTIFIER_POINTER (id));
4855 c_parser_error (parser, "expected identifier");
4856 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
4859 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
4864 str = c_parser_asm_string_literal (parser);
4865 if (str == NULL_TREE)
4867 parser->lex_untranslated_string = false;
4868 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4870 parser->lex_untranslated_string = true;
4873 loc = c_parser_peek_token (parser)->location;
4874 expr = c_parser_expression (parser);
4875 mark_exp_read (expr.value);
4877 expr = default_function_array_conversion (loc, expr);
4878 expr.value = c_fully_fold (expr.value, false, NULL);
4879 parser->lex_untranslated_string = true;
4880 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
4882 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
4885 list = chainon (list, build_tree_list (build_tree_list (name, str),
4887 if (c_parser_next_token_is (parser, CPP_COMMA))
4888 c_parser_consume_token (parser);
4895 /* Parse asm clobbers, a GNU extension.
4899 asm-clobbers , asm-string-literal
4903 c_parser_asm_clobbers (c_parser *parser)
4905 tree list = NULL_TREE;
4908 tree str = c_parser_asm_string_literal (parser);
4910 list = tree_cons (NULL_TREE, str, list);
4913 if (c_parser_next_token_is (parser, CPP_COMMA))
4914 c_parser_consume_token (parser);
4921 /* Parse asm goto labels, a GNU extension.
4925 asm-goto-operands , identifier
4929 c_parser_asm_goto_operands (c_parser *parser)
4931 tree list = NULL_TREE;
4936 if (c_parser_next_token_is (parser, CPP_NAME))
4938 c_token *tok = c_parser_peek_token (parser);
4940 label = lookup_label_for_goto (tok->location, name);
4941 c_parser_consume_token (parser);
4942 TREE_USED (label) = 1;
4946 c_parser_error (parser, "expected identifier");
4950 name = build_string (IDENTIFIER_LENGTH (name),
4951 IDENTIFIER_POINTER (name));
4952 list = tree_cons (name, label, list);
4953 if (c_parser_next_token_is (parser, CPP_COMMA))
4954 c_parser_consume_token (parser);
4956 return nreverse (list);
4960 /* Parse an expression other than a compound expression; that is, an
4961 assignment expression (C90 6.3.16, C99 6.5.16). If AFTER is not
4962 NULL then it is an Objective-C message expression which is the
4963 primary-expression starting the expression as an initializer.
4965 assignment-expression:
4966 conditional-expression
4967 unary-expression assignment-operator assignment-expression
4969 assignment-operator: one of
4970 = *= /= %= += -= <<= >>= &= ^= |=
4972 In GNU C we accept any conditional expression on the LHS and
4973 diagnose the invalid lvalue rather than producing a syntax
4976 static struct c_expr
4977 c_parser_expr_no_commas (c_parser *parser, struct c_expr *after)
4979 struct c_expr lhs, rhs, ret;
4980 enum tree_code code;
4981 location_t op_location, exp_location;
4982 gcc_assert (!after || c_dialect_objc ());
4983 lhs = c_parser_conditional_expression (parser, after);
4984 op_location = c_parser_peek_token (parser)->location;
4985 switch (c_parser_peek_token (parser)->type)
4994 code = TRUNC_DIV_EXPR;
4997 code = TRUNC_MOD_EXPR;
5012 code = BIT_AND_EXPR;
5015 code = BIT_XOR_EXPR;
5018 code = BIT_IOR_EXPR;
5023 c_parser_consume_token (parser);
5024 exp_location = c_parser_peek_token (parser)->location;
5025 rhs = c_parser_expr_no_commas (parser, NULL);
5026 rhs = default_function_array_read_conversion (exp_location, rhs);
5027 ret.value = build_modify_expr (op_location, lhs.value, lhs.original_type,
5028 code, exp_location, rhs.value,
5030 if (code == NOP_EXPR)
5031 ret.original_code = MODIFY_EXPR;
5034 TREE_NO_WARNING (ret.value) = 1;
5035 ret.original_code = ERROR_MARK;
5037 ret.original_type = NULL;
5041 /* Parse a conditional expression (C90 6.3.15, C99 6.5.15). If AFTER
5042 is not NULL then it is an Objective-C message expression which is
5043 the primary-expression starting the expression as an initializer.
5045 conditional-expression:
5046 logical-OR-expression
5047 logical-OR-expression ? expression : conditional-expression
5051 conditional-expression:
5052 logical-OR-expression ? : conditional-expression
5055 static struct c_expr
5056 c_parser_conditional_expression (c_parser *parser, struct c_expr *after)
5058 struct c_expr cond, exp1, exp2, ret;
5059 location_t cond_loc, colon_loc, middle_loc;
5061 gcc_assert (!after || c_dialect_objc ());
5063 cond = c_parser_binary_expression (parser, after);
5065 if (c_parser_next_token_is_not (parser, CPP_QUERY))
5067 cond_loc = c_parser_peek_token (parser)->location;
5068 cond = default_function_array_read_conversion (cond_loc, cond);
5069 c_parser_consume_token (parser);
5070 if (c_parser_next_token_is (parser, CPP_COLON))
5072 tree eptype = NULL_TREE;
5074 middle_loc = c_parser_peek_token (parser)->location;
5075 pedwarn (middle_loc, OPT_pedantic,
5076 "ISO C forbids omitting the middle term of a ?: expression");
5077 warn_for_omitted_condop (middle_loc, cond.value);
5078 if (TREE_CODE (cond.value) == EXCESS_PRECISION_EXPR)
5080 eptype = TREE_TYPE (cond.value);
5081 cond.value = TREE_OPERAND (cond.value, 0);
5083 /* Make sure first operand is calculated only once. */
5084 exp1.value = c_save_expr (default_conversion (cond.value));
5086 exp1.value = build1 (EXCESS_PRECISION_EXPR, eptype, exp1.value);
5087 exp1.original_type = NULL;
5088 cond.value = c_objc_common_truthvalue_conversion (cond_loc, exp1.value);
5089 c_inhibit_evaluation_warnings += cond.value == truthvalue_true_node;
5094 = c_objc_common_truthvalue_conversion
5095 (cond_loc, default_conversion (cond.value));
5096 c_inhibit_evaluation_warnings += cond.value == truthvalue_false_node;
5097 exp1 = c_parser_expression_conv (parser);
5098 mark_exp_read (exp1.value);
5099 c_inhibit_evaluation_warnings +=
5100 ((cond.value == truthvalue_true_node)
5101 - (cond.value == truthvalue_false_node));
5104 colon_loc = c_parser_peek_token (parser)->location;
5105 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
5107 c_inhibit_evaluation_warnings -= cond.value == truthvalue_true_node;
5108 ret.value = error_mark_node;
5109 ret.original_code = ERROR_MARK;
5110 ret.original_type = NULL;
5114 location_t exp2_loc = c_parser_peek_token (parser)->location;
5115 exp2 = c_parser_conditional_expression (parser, NULL);
5116 exp2 = default_function_array_read_conversion (exp2_loc, exp2);
5118 c_inhibit_evaluation_warnings -= cond.value == truthvalue_true_node;
5119 ret.value = build_conditional_expr (colon_loc, cond.value,
5120 cond.original_code == C_MAYBE_CONST_EXPR,
5121 exp1.value, exp1.original_type,
5122 exp2.value, exp2.original_type);
5123 ret.original_code = ERROR_MARK;
5124 if (exp1.value == error_mark_node || exp2.value == error_mark_node)
5125 ret.original_type = NULL;
5130 /* If both sides are enum type, the default conversion will have
5131 made the type of the result be an integer type. We want to
5132 remember the enum types we started with. */
5133 t1 = exp1.original_type ? exp1.original_type : TREE_TYPE (exp1.value);
5134 t2 = exp2.original_type ? exp2.original_type : TREE_TYPE (exp2.value);
5135 ret.original_type = ((t1 != error_mark_node
5136 && t2 != error_mark_node
5137 && (TYPE_MAIN_VARIANT (t1)
5138 == TYPE_MAIN_VARIANT (t2)))
5145 /* Parse a binary expression; that is, a logical-OR-expression (C90
5146 6.3.5-6.3.14, C99 6.5.5-6.5.14). If AFTER is not NULL then it is
5147 an Objective-C message expression which is the primary-expression
5148 starting the expression as an initializer.
5150 multiplicative-expression:
5152 multiplicative-expression * cast-expression
5153 multiplicative-expression / cast-expression
5154 multiplicative-expression % cast-expression
5156 additive-expression:
5157 multiplicative-expression
5158 additive-expression + multiplicative-expression
5159 additive-expression - multiplicative-expression
5163 shift-expression << additive-expression
5164 shift-expression >> additive-expression
5166 relational-expression:
5168 relational-expression < shift-expression
5169 relational-expression > shift-expression
5170 relational-expression <= shift-expression
5171 relational-expression >= shift-expression
5173 equality-expression:
5174 relational-expression
5175 equality-expression == relational-expression
5176 equality-expression != relational-expression
5180 AND-expression & equality-expression
5182 exclusive-OR-expression:
5184 exclusive-OR-expression ^ AND-expression
5186 inclusive-OR-expression:
5187 exclusive-OR-expression
5188 inclusive-OR-expression | exclusive-OR-expression
5190 logical-AND-expression:
5191 inclusive-OR-expression
5192 logical-AND-expression && inclusive-OR-expression
5194 logical-OR-expression:
5195 logical-AND-expression
5196 logical-OR-expression || logical-AND-expression
5199 static struct c_expr
5200 c_parser_binary_expression (c_parser *parser, struct c_expr *after)
5202 /* A binary expression is parsed using operator-precedence parsing,
5203 with the operands being cast expressions. All the binary
5204 operators are left-associative. Thus a binary expression is of
5207 E0 op1 E1 op2 E2 ...
5209 which we represent on a stack. On the stack, the precedence
5210 levels are strictly increasing. When a new operator is
5211 encountered of higher precedence than that at the top of the
5212 stack, it is pushed; its LHS is the top expression, and its RHS
5213 is everything parsed until it is popped. When a new operator is
5214 encountered with precedence less than or equal to that at the top
5215 of the stack, triples E[i-1] op[i] E[i] are popped and replaced
5216 by the result of the operation until the operator at the top of
5217 the stack has lower precedence than the new operator or there is
5218 only one element on the stack; then the top expression is the LHS
5219 of the new operator. In the case of logical AND and OR
5220 expressions, we also need to adjust c_inhibit_evaluation_warnings
5221 as appropriate when the operators are pushed and popped. */
5223 /* The precedence levels, where 0 is a dummy lowest level used for
5224 the bottom of the stack. */
5240 /* The expression at this stack level. */
5242 /* The precedence of the operator on its left, PREC_NONE at the
5243 bottom of the stack. */
5245 /* The operation on its left. */
5247 /* The source location of this operation. */
5251 /* Location of the binary operator. */
5252 location_t binary_loc = UNKNOWN_LOCATION; /* Quiet warning. */
5255 switch (stack[sp].op) \
5257 case TRUTH_ANDIF_EXPR: \
5258 c_inhibit_evaluation_warnings -= (stack[sp - 1].expr.value \
5259 == truthvalue_false_node); \
5261 case TRUTH_ORIF_EXPR: \
5262 c_inhibit_evaluation_warnings -= (stack[sp - 1].expr.value \
5263 == truthvalue_true_node); \
5268 stack[sp - 1].expr \
5269 = default_function_array_read_conversion (stack[sp - 1].loc, \
5270 stack[sp - 1].expr); \
5272 = default_function_array_read_conversion (stack[sp].loc, \
5274 stack[sp - 1].expr = parser_build_binary_op (stack[sp].loc, \
5276 stack[sp - 1].expr, \
5280 gcc_assert (!after || c_dialect_objc ());
5281 stack[0].loc = c_parser_peek_token (parser)->location;
5282 stack[0].expr = c_parser_cast_expression (parser, after);
5283 stack[0].prec = PREC_NONE;
5288 enum tree_code ocode;
5291 switch (c_parser_peek_token (parser)->type)
5299 ocode = TRUNC_DIV_EXPR;
5303 ocode = TRUNC_MOD_EXPR;
5315 ocode = LSHIFT_EXPR;
5319 ocode = RSHIFT_EXPR;
5333 case CPP_GREATER_EQ:
5346 oprec = PREC_BITAND;
5347 ocode = BIT_AND_EXPR;
5350 oprec = PREC_BITXOR;
5351 ocode = BIT_XOR_EXPR;
5355 ocode = BIT_IOR_EXPR;
5358 oprec = PREC_LOGAND;
5359 ocode = TRUTH_ANDIF_EXPR;
5363 ocode = TRUTH_ORIF_EXPR;
5366 /* Not a binary operator, so end of the binary
5370 binary_loc = c_parser_peek_token (parser)->location;
5371 c_parser_consume_token (parser);
5372 while (oprec <= stack[sp].prec)
5376 case TRUTH_ANDIF_EXPR:
5378 = default_function_array_read_conversion (stack[sp].loc,
5380 stack[sp].expr.value = c_objc_common_truthvalue_conversion
5381 (stack[sp].loc, default_conversion (stack[sp].expr.value));
5382 c_inhibit_evaluation_warnings += (stack[sp].expr.value
5383 == truthvalue_false_node);
5385 case TRUTH_ORIF_EXPR:
5387 = default_function_array_read_conversion (stack[sp].loc,
5389 stack[sp].expr.value = c_objc_common_truthvalue_conversion
5390 (stack[sp].loc, default_conversion (stack[sp].expr.value));
5391 c_inhibit_evaluation_warnings += (stack[sp].expr.value
5392 == truthvalue_true_node);
5398 stack[sp].loc = binary_loc;
5399 stack[sp].expr = c_parser_cast_expression (parser, NULL);
5400 stack[sp].prec = oprec;
5401 stack[sp].op = ocode;
5402 stack[sp].loc = binary_loc;
5407 return stack[0].expr;
5411 /* Parse a cast expression (C90 6.3.4, C99 6.5.4). If AFTER is not
5412 NULL then it is an Objective-C message expression which is the
5413 primary-expression starting the expression as an initializer.
5417 ( type-name ) unary-expression
5420 static struct c_expr
5421 c_parser_cast_expression (c_parser *parser, struct c_expr *after)
5423 location_t cast_loc = c_parser_peek_token (parser)->location;
5424 gcc_assert (!after || c_dialect_objc ());
5426 return c_parser_postfix_expression_after_primary (parser,
5428 /* If the expression begins with a parenthesized type name, it may
5429 be either a cast or a compound literal; we need to see whether
5430 the next character is '{' to tell the difference. If not, it is
5431 an unary expression. */
5432 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
5433 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
5435 struct c_type_name *type_name;
5438 c_parser_consume_token (parser);
5439 type_name = c_parser_type_name (parser);
5440 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
5441 if (type_name == NULL)
5443 ret.value = error_mark_node;
5444 ret.original_code = ERROR_MARK;
5445 ret.original_type = NULL;
5449 /* Save casted types in the function's used types hash table. */
5450 used_types_insert (type_name->specs->type);
5452 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
5453 return c_parser_postfix_expression_after_paren_type (parser, type_name,
5456 location_t expr_loc = c_parser_peek_token (parser)->location;
5457 expr = c_parser_cast_expression (parser, NULL);
5458 expr = default_function_array_read_conversion (expr_loc, expr);
5460 ret.value = c_cast_expr (cast_loc, type_name, expr.value);
5461 ret.original_code = ERROR_MARK;
5462 ret.original_type = NULL;
5466 return c_parser_unary_expression (parser);
5469 /* Parse an unary expression (C90 6.3.3, C99 6.5.3).
5475 unary-operator cast-expression
5476 sizeof unary-expression
5477 sizeof ( type-name )
5479 unary-operator: one of
5485 __alignof__ unary-expression
5486 __alignof__ ( type-name )
5489 unary-operator: one of
5490 __extension__ __real__ __imag__
5492 In addition, the GNU syntax treats ++ and -- as unary operators, so
5493 they may be applied to cast expressions with errors for non-lvalues
5496 static struct c_expr
5497 c_parser_unary_expression (c_parser *parser)
5500 struct c_expr ret, op;
5501 location_t op_loc = c_parser_peek_token (parser)->location;
5503 ret.original_code = ERROR_MARK;
5504 ret.original_type = NULL;
5505 switch (c_parser_peek_token (parser)->type)
5508 c_parser_consume_token (parser);
5509 exp_loc = c_parser_peek_token (parser)->location;
5510 op = c_parser_cast_expression (parser, NULL);
5511 op = default_function_array_read_conversion (exp_loc, op);
5512 return parser_build_unary_op (op_loc, PREINCREMENT_EXPR, op);
5513 case CPP_MINUS_MINUS:
5514 c_parser_consume_token (parser);
5515 exp_loc = c_parser_peek_token (parser)->location;
5516 op = c_parser_cast_expression (parser, NULL);
5517 op = default_function_array_read_conversion (exp_loc, op);
5518 return parser_build_unary_op (op_loc, PREDECREMENT_EXPR, op);
5520 c_parser_consume_token (parser);
5521 op = c_parser_cast_expression (parser, NULL);
5522 mark_exp_read (op.value);
5523 return parser_build_unary_op (op_loc, ADDR_EXPR, op);
5525 c_parser_consume_token (parser);
5526 exp_loc = c_parser_peek_token (parser)->location;
5527 op = c_parser_cast_expression (parser, NULL);
5528 op = default_function_array_read_conversion (exp_loc, op);
5529 ret.value = build_indirect_ref (op_loc, op.value, RO_UNARY_STAR);
5532 if (!c_dialect_objc () && !in_system_header)
5535 "traditional C rejects the unary plus operator");
5536 c_parser_consume_token (parser);
5537 exp_loc = c_parser_peek_token (parser)->location;
5538 op = c_parser_cast_expression (parser, NULL);
5539 op = default_function_array_read_conversion (exp_loc, op);
5540 return parser_build_unary_op (op_loc, CONVERT_EXPR, op);
5542 c_parser_consume_token (parser);
5543 exp_loc = c_parser_peek_token (parser)->location;
5544 op = c_parser_cast_expression (parser, NULL);
5545 op = default_function_array_read_conversion (exp_loc, op);
5546 return parser_build_unary_op (op_loc, NEGATE_EXPR, op);
5548 c_parser_consume_token (parser);
5549 exp_loc = c_parser_peek_token (parser)->location;
5550 op = c_parser_cast_expression (parser, NULL);
5551 op = default_function_array_read_conversion (exp_loc, op);
5552 return parser_build_unary_op (op_loc, BIT_NOT_EXPR, op);
5554 c_parser_consume_token (parser);
5555 exp_loc = c_parser_peek_token (parser)->location;
5556 op = c_parser_cast_expression (parser, NULL);
5557 op = default_function_array_read_conversion (exp_loc, op);
5558 return parser_build_unary_op (op_loc, TRUTH_NOT_EXPR, op);
5560 /* Refer to the address of a label as a pointer. */
5561 c_parser_consume_token (parser);
5562 if (c_parser_next_token_is (parser, CPP_NAME))
5564 ret.value = finish_label_address_expr
5565 (c_parser_peek_token (parser)->value, op_loc);
5566 c_parser_consume_token (parser);
5570 c_parser_error (parser, "expected identifier");
5571 ret.value = error_mark_node;
5575 switch (c_parser_peek_token (parser)->keyword)
5578 return c_parser_sizeof_expression (parser);
5580 return c_parser_alignof_expression (parser);
5582 c_parser_consume_token (parser);
5583 ext = disable_extension_diagnostics ();
5584 ret = c_parser_cast_expression (parser, NULL);
5585 restore_extension_diagnostics (ext);
5588 c_parser_consume_token (parser);
5589 exp_loc = c_parser_peek_token (parser)->location;
5590 op = c_parser_cast_expression (parser, NULL);
5591 op = default_function_array_conversion (exp_loc, op);
5592 return parser_build_unary_op (op_loc, REALPART_EXPR, op);
5594 c_parser_consume_token (parser);
5595 exp_loc = c_parser_peek_token (parser)->location;
5596 op = c_parser_cast_expression (parser, NULL);
5597 op = default_function_array_conversion (exp_loc, op);
5598 return parser_build_unary_op (op_loc, IMAGPART_EXPR, op);
5600 return c_parser_postfix_expression (parser);
5603 return c_parser_postfix_expression (parser);
5607 /* Parse a sizeof expression. */
5609 static struct c_expr
5610 c_parser_sizeof_expression (c_parser *parser)
5613 location_t expr_loc;
5614 gcc_assert (c_parser_next_token_is_keyword (parser, RID_SIZEOF));
5615 c_parser_consume_token (parser);
5616 c_inhibit_evaluation_warnings++;
5618 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
5619 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
5621 /* Either sizeof ( type-name ) or sizeof unary-expression
5622 starting with a compound literal. */
5623 struct c_type_name *type_name;
5624 c_parser_consume_token (parser);
5625 expr_loc = c_parser_peek_token (parser)->location;
5626 type_name = c_parser_type_name (parser);
5627 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
5628 if (type_name == NULL)
5631 c_inhibit_evaluation_warnings--;
5633 ret.value = error_mark_node;
5634 ret.original_code = ERROR_MARK;
5635 ret.original_type = NULL;
5638 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
5640 expr = c_parser_postfix_expression_after_paren_type (parser,
5645 /* sizeof ( type-name ). */
5646 c_inhibit_evaluation_warnings--;
5648 return c_expr_sizeof_type (expr_loc, type_name);
5652 expr_loc = c_parser_peek_token (parser)->location;
5653 expr = c_parser_unary_expression (parser);
5655 c_inhibit_evaluation_warnings--;
5657 mark_exp_read (expr.value);
5658 if (TREE_CODE (expr.value) == COMPONENT_REF
5659 && DECL_C_BIT_FIELD (TREE_OPERAND (expr.value, 1)))
5660 error_at (expr_loc, "%<sizeof%> applied to a bit-field");
5661 return c_expr_sizeof_expr (expr_loc, expr);
5665 /* Parse an alignof expression. */
5667 static struct c_expr
5668 c_parser_alignof_expression (c_parser *parser)
5671 location_t loc = c_parser_peek_token (parser)->location;
5672 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ALIGNOF));
5673 c_parser_consume_token (parser);
5674 c_inhibit_evaluation_warnings++;
5676 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
5677 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
5679 /* Either __alignof__ ( type-name ) or __alignof__
5680 unary-expression starting with a compound literal. */
5682 struct c_type_name *type_name;
5684 c_parser_consume_token (parser);
5685 loc = c_parser_peek_token (parser)->location;
5686 type_name = c_parser_type_name (parser);
5687 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
5688 if (type_name == NULL)
5691 c_inhibit_evaluation_warnings--;
5693 ret.value = error_mark_node;
5694 ret.original_code = ERROR_MARK;
5695 ret.original_type = NULL;
5698 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
5700 expr = c_parser_postfix_expression_after_paren_type (parser,
5705 /* alignof ( type-name ). */
5706 c_inhibit_evaluation_warnings--;
5708 ret.value = c_alignof (loc, groktypename (type_name, NULL, NULL));
5709 ret.original_code = ERROR_MARK;
5710 ret.original_type = NULL;
5716 expr = c_parser_unary_expression (parser);
5718 mark_exp_read (expr.value);
5719 c_inhibit_evaluation_warnings--;
5721 ret.value = c_alignof_expr (loc, expr.value);
5722 ret.original_code = ERROR_MARK;
5723 ret.original_type = NULL;
5728 /* Parse a postfix expression (C90 6.3.1-6.3.2, C99 6.5.1-6.5.2).
5732 postfix-expression [ expression ]
5733 postfix-expression ( argument-expression-list[opt] )
5734 postfix-expression . identifier
5735 postfix-expression -> identifier
5736 postfix-expression ++
5737 postfix-expression --
5738 ( type-name ) { initializer-list }
5739 ( type-name ) { initializer-list , }
5741 argument-expression-list:
5743 argument-expression-list , argument-expression
5755 (treated as a keyword in GNU C)
5758 ( compound-statement )
5759 __builtin_va_arg ( assignment-expression , type-name )
5760 __builtin_offsetof ( type-name , offsetof-member-designator )
5761 __builtin_choose_expr ( assignment-expression ,
5762 assignment-expression ,
5763 assignment-expression )
5764 __builtin_types_compatible_p ( type-name , type-name )
5766 offsetof-member-designator:
5768 offsetof-member-designator . identifier
5769 offsetof-member-designator [ expression ]
5774 [ objc-receiver objc-message-args ]
5775 @selector ( objc-selector-arg )
5776 @protocol ( identifier )
5777 @encode ( type-name )
5781 static struct c_expr
5782 c_parser_postfix_expression (c_parser *parser)
5784 struct c_expr expr, e1, e2, e3;
5785 struct c_type_name *t1, *t2;
5786 location_t loc = c_parser_peek_token (parser)->location;;
5787 expr.original_code = ERROR_MARK;
5788 expr.original_type = NULL;
5789 switch (c_parser_peek_token (parser)->type)
5792 expr.value = c_parser_peek_token (parser)->value;
5793 loc = c_parser_peek_token (parser)->location;
5794 c_parser_consume_token (parser);
5795 if (TREE_CODE (expr.value) == FIXED_CST
5796 && !targetm.fixed_point_supported_p ())
5798 error_at (loc, "fixed-point types not supported for this target");
5799 expr.value = error_mark_node;
5806 expr.value = c_parser_peek_token (parser)->value;
5807 c_parser_consume_token (parser);
5813 case CPP_UTF8STRING:
5814 expr.value = c_parser_peek_token (parser)->value;
5815 expr.original_code = STRING_CST;
5816 c_parser_consume_token (parser);
5818 case CPP_OBJC_STRING:
5819 gcc_assert (c_dialect_objc ());
5821 = objc_build_string_object (c_parser_peek_token (parser)->value);
5822 c_parser_consume_token (parser);
5825 if (c_parser_peek_token (parser)->id_kind != C_ID_ID)
5827 c_parser_error (parser, "expected expression");
5828 expr.value = error_mark_node;
5832 tree id = c_parser_peek_token (parser)->value;
5833 c_parser_consume_token (parser);
5834 expr.value = build_external_ref (loc, id,
5835 (c_parser_peek_token (parser)->type
5837 &expr.original_type);
5840 case CPP_OPEN_PAREN:
5841 /* A parenthesized expression, statement expression or compound
5843 if (c_parser_peek_2nd_token (parser)->type == CPP_OPEN_BRACE)
5845 /* A statement expression. */
5847 location_t brace_loc;
5848 c_parser_consume_token (parser);
5849 brace_loc = c_parser_peek_token (parser)->location;
5850 c_parser_consume_token (parser);
5851 if (cur_stmt_list == NULL)
5853 error_at (loc, "braced-group within expression allowed "
5854 "only inside a function");
5855 parser->error = true;
5856 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
5857 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5858 expr.value = error_mark_node;
5861 stmt = c_begin_stmt_expr ();
5862 c_parser_compound_statement_nostart (parser);
5863 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5865 pedwarn (loc, OPT_pedantic,
5866 "ISO C forbids braced-groups within expressions");
5867 expr.value = c_finish_stmt_expr (brace_loc, stmt);
5868 mark_exp_read (expr.value);
5870 else if (c_token_starts_typename (c_parser_peek_2nd_token (parser)))
5872 /* A compound literal. ??? Can we actually get here rather
5873 than going directly to
5874 c_parser_postfix_expression_after_paren_type from
5877 struct c_type_name *type_name;
5878 c_parser_consume_token (parser);
5879 loc = c_parser_peek_token (parser)->location;
5880 type_name = c_parser_type_name (parser);
5881 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5883 if (type_name == NULL)
5885 expr.value = error_mark_node;
5888 expr = c_parser_postfix_expression_after_paren_type (parser,
5894 /* A parenthesized expression. */
5895 c_parser_consume_token (parser);
5896 expr = c_parser_expression (parser);
5897 if (TREE_CODE (expr.value) == MODIFY_EXPR)
5898 TREE_NO_WARNING (expr.value) = 1;
5899 if (expr.original_code != C_MAYBE_CONST_EXPR)
5900 expr.original_code = ERROR_MARK;
5901 /* Don't change EXPR.ORIGINAL_TYPE. */
5902 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5907 switch (c_parser_peek_token (parser)->keyword)
5909 case RID_FUNCTION_NAME:
5910 case RID_PRETTY_FUNCTION_NAME:
5911 case RID_C99_FUNCTION_NAME:
5912 expr.value = fname_decl (loc,
5913 c_parser_peek_token (parser)->keyword,
5914 c_parser_peek_token (parser)->value);
5915 c_parser_consume_token (parser);
5918 c_parser_consume_token (parser);
5919 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5921 expr.value = error_mark_node;
5924 e1 = c_parser_expr_no_commas (parser, NULL);
5925 mark_exp_read (e1.value);
5926 e1.value = c_fully_fold (e1.value, false, NULL);
5927 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
5929 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5930 expr.value = error_mark_node;
5933 loc = c_parser_peek_token (parser)->location;
5934 t1 = c_parser_type_name (parser);
5935 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5939 expr.value = error_mark_node;
5943 tree type_expr = NULL_TREE;
5944 expr.value = c_build_va_arg (loc, e1.value,
5945 groktypename (t1, &type_expr, NULL));
5948 expr.value = build2 (C_MAYBE_CONST_EXPR,
5949 TREE_TYPE (expr.value), type_expr,
5951 C_MAYBE_CONST_EXPR_NON_CONST (expr.value) = true;
5956 c_parser_consume_token (parser);
5957 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5959 expr.value = error_mark_node;
5962 t1 = c_parser_type_name (parser);
5965 expr.value = error_mark_node;
5968 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
5970 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5971 expr.value = error_mark_node;
5975 tree type = groktypename (t1, NULL, NULL);
5977 if (type == error_mark_node)
5978 offsetof_ref = error_mark_node;
5981 offsetof_ref = build1 (INDIRECT_REF, type, null_pointer_node);
5982 SET_EXPR_LOCATION (offsetof_ref, loc);
5984 /* Parse the second argument to __builtin_offsetof. We
5985 must have one identifier, and beyond that we want to
5986 accept sub structure and sub array references. */
5987 if (c_parser_next_token_is (parser, CPP_NAME))
5989 offsetof_ref = build_component_ref
5990 (loc, offsetof_ref, c_parser_peek_token (parser)->value);
5991 c_parser_consume_token (parser);
5992 while (c_parser_next_token_is (parser, CPP_DOT)
5993 || c_parser_next_token_is (parser,
5995 || c_parser_next_token_is (parser,
5998 if (c_parser_next_token_is (parser, CPP_DEREF))
6000 loc = c_parser_peek_token (parser)->location;
6001 offsetof_ref = build_array_ref (loc,
6006 else if (c_parser_next_token_is (parser, CPP_DOT))
6009 c_parser_consume_token (parser);
6010 if (c_parser_next_token_is_not (parser,
6013 c_parser_error (parser, "expected identifier");
6016 offsetof_ref = build_component_ref
6018 c_parser_peek_token (parser)->value);
6019 c_parser_consume_token (parser);
6024 loc = c_parser_peek_token (parser)->location;
6025 c_parser_consume_token (parser);
6026 idx = c_parser_expression (parser).value;
6027 idx = c_fully_fold (idx, false, NULL);
6028 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
6030 offsetof_ref = build_array_ref (loc, offsetof_ref, idx);
6035 c_parser_error (parser, "expected identifier");
6036 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
6038 expr.value = fold_offsetof (offsetof_ref, NULL_TREE);
6041 case RID_CHOOSE_EXPR:
6042 c_parser_consume_token (parser);
6043 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6045 expr.value = error_mark_node;
6048 loc = c_parser_peek_token (parser)->location;
6049 e1 = c_parser_expr_no_commas (parser, NULL);
6050 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
6052 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6053 expr.value = error_mark_node;
6056 e2 = c_parser_expr_no_commas (parser, NULL);
6057 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
6059 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6060 expr.value = error_mark_node;
6063 e3 = c_parser_expr_no_commas (parser, NULL);
6064 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
6070 mark_exp_read (e2.value);
6071 mark_exp_read (e3.value);
6072 if (TREE_CODE (c) != INTEGER_CST
6073 || !INTEGRAL_TYPE_P (TREE_TYPE (c)))
6075 "first argument to %<__builtin_choose_expr%> not"
6077 constant_expression_warning (c);
6078 expr = integer_zerop (c) ? e3 : e2;
6081 case RID_TYPES_COMPATIBLE_P:
6082 c_parser_consume_token (parser);
6083 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6085 expr.value = error_mark_node;
6088 t1 = c_parser_type_name (parser);
6091 expr.value = error_mark_node;
6094 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
6096 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6097 expr.value = error_mark_node;
6100 t2 = c_parser_type_name (parser);
6103 expr.value = error_mark_node;
6106 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
6111 e1 = TYPE_MAIN_VARIANT (groktypename (t1, NULL, NULL));
6112 e2 = TYPE_MAIN_VARIANT (groktypename (t2, NULL, NULL));
6115 = comptypes (e1, e2) ? integer_one_node : integer_zero_node;
6118 case RID_AT_SELECTOR:
6119 gcc_assert (c_dialect_objc ());
6120 c_parser_consume_token (parser);
6121 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6123 expr.value = error_mark_node;
6127 tree sel = c_parser_objc_selector_arg (parser);
6128 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
6130 expr.value = objc_build_selector_expr (loc, sel);
6133 case RID_AT_PROTOCOL:
6134 gcc_assert (c_dialect_objc ());
6135 c_parser_consume_token (parser);
6136 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6138 expr.value = error_mark_node;
6141 if (c_parser_next_token_is_not (parser, CPP_NAME))
6143 c_parser_error (parser, "expected identifier");
6144 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6145 expr.value = error_mark_node;
6149 tree id = c_parser_peek_token (parser)->value;
6150 c_parser_consume_token (parser);
6151 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
6153 expr.value = objc_build_protocol_expr (id);
6157 /* Extension to support C-structures in the archiver. */
6158 gcc_assert (c_dialect_objc ());
6159 c_parser_consume_token (parser);
6160 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6162 expr.value = error_mark_node;
6165 t1 = c_parser_type_name (parser);
6168 expr.value = error_mark_node;
6169 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6172 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
6175 tree type = groktypename (t1, NULL, NULL);
6176 expr.value = objc_build_encode_expr (type);
6180 c_parser_error (parser, "expected expression");
6181 expr.value = error_mark_node;
6185 case CPP_OPEN_SQUARE:
6186 if (c_dialect_objc ())
6188 tree receiver, args;
6189 c_parser_consume_token (parser);
6190 receiver = c_parser_objc_receiver (parser);
6191 args = c_parser_objc_message_args (parser);
6192 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
6194 expr.value = objc_build_message_expr (build_tree_list (receiver,
6198 /* Else fall through to report error. */
6200 c_parser_error (parser, "expected expression");
6201 expr.value = error_mark_node;
6204 return c_parser_postfix_expression_after_primary (parser, loc, expr);
6207 /* Parse a postfix expression after a parenthesized type name: the
6208 brace-enclosed initializer of a compound literal, possibly followed
6209 by some postfix operators. This is separate because it is not
6210 possible to tell until after the type name whether a cast
6211 expression has a cast or a compound literal, or whether the operand
6212 of sizeof is a parenthesized type name or starts with a compound
6213 literal. TYPE_LOC is the location where TYPE_NAME starts--the
6214 location of the first token after the parentheses around the type
6217 static struct c_expr
6218 c_parser_postfix_expression_after_paren_type (c_parser *parser,
6219 struct c_type_name *type_name,
6220 location_t type_loc)
6226 location_t start_loc;
6227 tree type_expr = NULL_TREE;
6228 bool type_expr_const = true;
6229 check_compound_literal_type (type_loc, type_name);
6230 start_init (NULL_TREE, NULL, 0);
6231 type = groktypename (type_name, &type_expr, &type_expr_const);
6232 start_loc = c_parser_peek_token (parser)->location;
6233 if (type != error_mark_node && C_TYPE_VARIABLE_SIZE (type))
6235 error_at (type_loc, "compound literal has variable size");
6236 type = error_mark_node;
6238 init = c_parser_braced_init (parser, type, false);
6240 maybe_warn_string_init (type, init);
6242 if (type != error_mark_node
6243 && !ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (type))
6244 && current_function_decl)
6246 error ("compound literal qualified by address-space qualifier");
6247 type = error_mark_node;
6251 pedwarn (start_loc, OPT_pedantic, "ISO C90 forbids compound literals");
6252 non_const = ((init.value && TREE_CODE (init.value) == CONSTRUCTOR)
6253 ? CONSTRUCTOR_NON_CONST (init.value)
6254 : init.original_code == C_MAYBE_CONST_EXPR);
6255 non_const |= !type_expr_const;
6256 expr.value = build_compound_literal (start_loc, type, init.value, non_const);
6257 expr.original_code = ERROR_MARK;
6258 expr.original_type = NULL;
6261 if (TREE_CODE (expr.value) == C_MAYBE_CONST_EXPR)
6263 gcc_assert (C_MAYBE_CONST_EXPR_PRE (expr.value) == NULL_TREE);
6264 C_MAYBE_CONST_EXPR_PRE (expr.value) = type_expr;
6268 gcc_assert (!non_const);
6269 expr.value = build2 (C_MAYBE_CONST_EXPR, type,
6270 type_expr, expr.value);
6273 return c_parser_postfix_expression_after_primary (parser, start_loc, expr);
6276 /* Parse a postfix expression after the initial primary or compound
6277 literal; that is, parse a series of postfix operators.
6279 EXPR_LOC is the location of the primary expression. */
6281 static struct c_expr
6282 c_parser_postfix_expression_after_primary (c_parser *parser,
6283 location_t expr_loc,
6286 struct c_expr orig_expr;
6288 VEC(tree,gc) *exprlist;
6289 VEC(tree,gc) *origtypes;
6292 location_t op_loc = c_parser_peek_token (parser)->location;
6293 switch (c_parser_peek_token (parser)->type)
6295 case CPP_OPEN_SQUARE:
6296 /* Array reference. */
6297 c_parser_consume_token (parser);
6298 idx = c_parser_expression (parser).value;
6299 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
6301 expr.value = build_array_ref (op_loc, expr.value, idx);
6302 expr.original_code = ERROR_MARK;
6303 expr.original_type = NULL;
6305 case CPP_OPEN_PAREN:
6306 /* Function call. */
6307 c_parser_consume_token (parser);
6308 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
6311 exprlist = c_parser_expr_list (parser, true, false, &origtypes);
6312 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
6315 mark_exp_read (expr.value);
6316 /* FIXME diagnostics: Ideally we want the FUNCNAME, not the
6317 "(" after the FUNCNAME, which is what we have now. */
6318 expr.value = build_function_call_vec (op_loc, expr.value, exprlist,
6320 expr.original_code = ERROR_MARK;
6321 if (TREE_CODE (expr.value) == INTEGER_CST
6322 && TREE_CODE (orig_expr.value) == FUNCTION_DECL
6323 && DECL_BUILT_IN_CLASS (orig_expr.value) == BUILT_IN_NORMAL
6324 && DECL_FUNCTION_CODE (orig_expr.value) == BUILT_IN_CONSTANT_P)
6325 expr.original_code = C_MAYBE_CONST_EXPR;
6326 expr.original_type = NULL;
6327 if (exprlist != NULL)
6329 release_tree_vector (exprlist);
6330 release_tree_vector (origtypes);
6334 /* Structure element reference. */
6335 c_parser_consume_token (parser);
6336 expr = default_function_array_conversion (expr_loc, expr);
6337 if (c_parser_next_token_is (parser, CPP_NAME))
6338 ident = c_parser_peek_token (parser)->value;
6341 c_parser_error (parser, "expected identifier");
6342 expr.value = error_mark_node;
6343 expr.original_code = ERROR_MARK;
6344 expr.original_type = NULL;
6347 c_parser_consume_token (parser);
6348 expr.value = build_component_ref (op_loc, expr.value, ident);
6349 expr.original_code = ERROR_MARK;
6350 if (TREE_CODE (expr.value) != COMPONENT_REF)
6351 expr.original_type = NULL;
6354 /* Remember the original type of a bitfield. */
6355 tree field = TREE_OPERAND (expr.value, 1);
6356 if (TREE_CODE (field) != FIELD_DECL)
6357 expr.original_type = NULL;
6359 expr.original_type = DECL_BIT_FIELD_TYPE (field);
6363 /* Structure element reference. */
6364 c_parser_consume_token (parser);
6365 expr = default_function_array_conversion (expr_loc, expr);
6366 if (c_parser_next_token_is (parser, CPP_NAME))
6367 ident = c_parser_peek_token (parser)->value;
6370 c_parser_error (parser, "expected identifier");
6371 expr.value = error_mark_node;
6372 expr.original_code = ERROR_MARK;
6373 expr.original_type = NULL;
6376 c_parser_consume_token (parser);
6377 expr.value = build_component_ref (op_loc,
6378 build_indirect_ref (op_loc,
6382 expr.original_code = ERROR_MARK;
6383 if (TREE_CODE (expr.value) != COMPONENT_REF)
6384 expr.original_type = NULL;
6387 /* Remember the original type of a bitfield. */
6388 tree field = TREE_OPERAND (expr.value, 1);
6389 if (TREE_CODE (field) != FIELD_DECL)
6390 expr.original_type = NULL;
6392 expr.original_type = DECL_BIT_FIELD_TYPE (field);
6396 /* Postincrement. */
6397 c_parser_consume_token (parser);
6398 expr = default_function_array_read_conversion (expr_loc, expr);
6399 expr.value = build_unary_op (op_loc,
6400 POSTINCREMENT_EXPR, expr.value, 0);
6401 expr.original_code = ERROR_MARK;
6402 expr.original_type = NULL;
6404 case CPP_MINUS_MINUS:
6405 /* Postdecrement. */
6406 c_parser_consume_token (parser);
6407 expr = default_function_array_read_conversion (expr_loc, expr);
6408 expr.value = build_unary_op (op_loc,
6409 POSTDECREMENT_EXPR, expr.value, 0);
6410 expr.original_code = ERROR_MARK;
6411 expr.original_type = NULL;
6419 /* Parse an expression (C90 6.3.17, C99 6.5.17).
6422 assignment-expression
6423 expression , assignment-expression
6426 static struct c_expr
6427 c_parser_expression (c_parser *parser)
6430 expr = c_parser_expr_no_commas (parser, NULL);
6431 while (c_parser_next_token_is (parser, CPP_COMMA))
6435 location_t loc = c_parser_peek_token (parser)->location;
6436 location_t expr_loc;
6437 c_parser_consume_token (parser);
6438 expr_loc = c_parser_peek_token (parser)->location;
6439 lhsval = expr.value;
6440 while (TREE_CODE (lhsval) == COMPOUND_EXPR)
6441 lhsval = TREE_OPERAND (lhsval, 1);
6442 if (DECL_P (lhsval) || handled_component_p (lhsval))
6443 mark_exp_read (lhsval);
6444 next = c_parser_expr_no_commas (parser, NULL);
6445 next = default_function_array_conversion (expr_loc, next);
6446 expr.value = build_compound_expr (loc, expr.value, next.value);
6447 expr.original_code = COMPOUND_EXPR;
6448 expr.original_type = next.original_type;
6453 /* Parse an expression and convert functions or arrays to
6456 static struct c_expr
6457 c_parser_expression_conv (c_parser *parser)
6460 location_t loc = c_parser_peek_token (parser)->location;
6461 expr = c_parser_expression (parser);
6462 expr = default_function_array_conversion (loc, expr);
6466 /* Parse a non-empty list of expressions. If CONVERT_P, convert
6467 functions and arrays to pointers. If FOLD_P, fold the expressions.
6470 assignment-expression
6471 nonempty-expr-list , assignment-expression
6474 static VEC(tree,gc) *
6475 c_parser_expr_list (c_parser *parser, bool convert_p, bool fold_p,
6476 VEC(tree,gc) **p_orig_types)
6479 VEC(tree,gc) *orig_types;
6481 location_t loc = c_parser_peek_token (parser)->location;
6483 ret = make_tree_vector ();
6484 if (p_orig_types == NULL)
6487 orig_types = make_tree_vector ();
6489 expr = c_parser_expr_no_commas (parser, NULL);
6491 expr = default_function_array_read_conversion (loc, expr);
6493 expr.value = c_fully_fold (expr.value, false, NULL);
6494 VEC_quick_push (tree, ret, expr.value);
6495 if (orig_types != NULL)
6496 VEC_quick_push (tree, orig_types, expr.original_type);
6497 while (c_parser_next_token_is (parser, CPP_COMMA))
6499 c_parser_consume_token (parser);
6500 loc = c_parser_peek_token (parser)->location;
6501 expr = c_parser_expr_no_commas (parser, NULL);
6503 expr = default_function_array_read_conversion (loc, expr);
6505 expr.value = c_fully_fold (expr.value, false, NULL);
6506 VEC_safe_push (tree, gc, ret, expr.value);
6507 if (orig_types != NULL)
6508 VEC_safe_push (tree, gc, orig_types, expr.original_type);
6510 if (orig_types != NULL)
6511 *p_orig_types = orig_types;
6515 /* Parse Objective-C-specific constructs. */
6517 /* Parse an objc-class-definition.
6519 objc-class-definition:
6520 @interface identifier objc-superclass[opt] objc-protocol-refs[opt]
6521 objc-class-instance-variables[opt] objc-methodprotolist @end
6522 @implementation identifier objc-superclass[opt]
6523 objc-class-instance-variables[opt]
6524 @interface identifier ( identifier ) objc-protocol-refs[opt]
6525 objc-methodprotolist @end
6526 @implementation identifier ( identifier )
6531 "@interface identifier (" must start "@interface identifier (
6532 identifier ) ...": objc-methodprotolist in the first production may
6533 not start with a parenthesized identifier as a declarator of a data
6534 definition with no declaration specifiers if the objc-superclass,
6535 objc-protocol-refs and objc-class-instance-variables are omitted. */
6538 c_parser_objc_class_definition (c_parser *parser, tree attributes)
6543 if (c_parser_next_token_is_keyword (parser, RID_AT_INTERFACE))
6545 else if (c_parser_next_token_is_keyword (parser, RID_AT_IMPLEMENTATION))
6550 c_parser_consume_token (parser);
6551 if (c_parser_next_token_is_not (parser, CPP_NAME))
6553 c_parser_error (parser, "expected identifier");
6556 id1 = c_parser_peek_token (parser)->value;
6557 c_parser_consume_token (parser);
6558 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
6561 tree proto = NULL_TREE;
6562 c_parser_consume_token (parser);
6563 if (c_parser_next_token_is_not (parser, CPP_NAME))
6565 c_parser_error (parser, "expected identifier");
6566 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6569 id2 = c_parser_peek_token (parser)->value;
6570 c_parser_consume_token (parser);
6571 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
6574 objc_start_category_implementation (id1, id2);
6577 if (c_parser_next_token_is (parser, CPP_LESS))
6578 proto = c_parser_objc_protocol_refs (parser);
6579 objc_start_category_interface (id1, id2, proto, attributes);
6580 c_parser_objc_methodprotolist (parser);
6581 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
6582 objc_finish_interface ();
6585 if (c_parser_next_token_is (parser, CPP_COLON))
6587 c_parser_consume_token (parser);
6588 if (c_parser_next_token_is_not (parser, CPP_NAME))
6590 c_parser_error (parser, "expected identifier");
6593 superclass = c_parser_peek_token (parser)->value;
6594 c_parser_consume_token (parser);
6597 superclass = NULL_TREE;
6600 tree proto = NULL_TREE;
6601 if (c_parser_next_token_is (parser, CPP_LESS))
6602 proto = c_parser_objc_protocol_refs (parser);
6603 objc_start_class_interface (id1, superclass, proto, attributes);
6606 objc_start_class_implementation (id1, superclass);
6607 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
6608 c_parser_objc_class_instance_variables (parser);
6611 objc_continue_interface ();
6612 c_parser_objc_methodprotolist (parser);
6613 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
6614 objc_finish_interface ();
6618 objc_continue_implementation ();
6623 /* Parse objc-class-instance-variables.
6625 objc-class-instance-variables:
6626 { objc-instance-variable-decl-list[opt] }
6628 objc-instance-variable-decl-list:
6629 objc-visibility-spec
6630 objc-instance-variable-decl ;
6632 objc-instance-variable-decl-list objc-visibility-spec
6633 objc-instance-variable-decl-list objc-instance-variable-decl ;
6634 objc-instance-variable-decl-list ;
6636 objc-visibility-spec:
6641 objc-instance-variable-decl:
6646 c_parser_objc_class_instance_variables (c_parser *parser)
6648 gcc_assert (c_parser_next_token_is (parser, CPP_OPEN_BRACE));
6649 c_parser_consume_token (parser);
6650 while (c_parser_next_token_is_not (parser, CPP_EOF))
6653 /* Parse any stray semicolon. */
6654 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
6656 pedwarn (c_parser_peek_token (parser)->location, OPT_pedantic,
6657 "extra semicolon in struct or union specified");
6658 c_parser_consume_token (parser);
6661 /* Stop if at the end of the instance variables. */
6662 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
6664 c_parser_consume_token (parser);
6667 /* Parse any objc-visibility-spec. */
6668 if (c_parser_next_token_is_keyword (parser, RID_AT_PRIVATE))
6670 c_parser_consume_token (parser);
6671 objc_set_visibility (2);
6674 else if (c_parser_next_token_is_keyword (parser, RID_AT_PROTECTED))
6676 c_parser_consume_token (parser);
6677 objc_set_visibility (0);
6680 else if (c_parser_next_token_is_keyword (parser, RID_AT_PUBLIC))
6682 c_parser_consume_token (parser);
6683 objc_set_visibility (1);
6686 else if (c_parser_next_token_is (parser, CPP_PRAGMA))
6688 c_parser_pragma (parser, pragma_external);
6692 /* Parse some comma-separated declarations. */
6693 decls = c_parser_struct_declaration (parser);
6695 /* Comma-separated instance variables are chained together in
6696 reverse order; add them one by one. */
6697 tree ivar = nreverse (decls);
6698 for (; ivar; ivar = DECL_CHAIN (ivar))
6699 objc_add_instance_variable (copy_node (ivar));
6701 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
6705 /* Parse an objc-class-declaration.
6707 objc-class-declaration:
6708 @class identifier-list ;
6712 c_parser_objc_class_declaration (c_parser *parser)
6714 tree list = NULL_TREE;
6715 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_CLASS));
6716 c_parser_consume_token (parser);
6717 /* Any identifiers, including those declared as type names, are OK
6722 if (c_parser_next_token_is_not (parser, CPP_NAME))
6724 c_parser_error (parser, "expected identifier");
6727 id = c_parser_peek_token (parser)->value;
6728 list = chainon (list, build_tree_list (NULL_TREE, id));
6729 c_parser_consume_token (parser);
6730 if (c_parser_next_token_is (parser, CPP_COMMA))
6731 c_parser_consume_token (parser);
6735 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
6736 objc_declare_class (list);
6739 /* Parse an objc-alias-declaration.
6741 objc-alias-declaration:
6742 @compatibility_alias identifier identifier ;
6746 c_parser_objc_alias_declaration (c_parser *parser)
6749 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_ALIAS));
6750 c_parser_consume_token (parser);
6751 if (c_parser_next_token_is_not (parser, CPP_NAME))
6753 c_parser_error (parser, "expected identifier");
6754 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
6757 id1 = c_parser_peek_token (parser)->value;
6758 c_parser_consume_token (parser);
6759 if (c_parser_next_token_is_not (parser, CPP_NAME))
6761 c_parser_error (parser, "expected identifier");
6762 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
6765 id2 = c_parser_peek_token (parser)->value;
6766 c_parser_consume_token (parser);
6767 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
6768 objc_declare_alias (id1, id2);
6771 /* Parse an objc-protocol-definition.
6773 objc-protocol-definition:
6774 @protocol identifier objc-protocol-refs[opt] objc-methodprotolist @end
6775 @protocol identifier-list ;
6777 "@protocol identifier ;" should be resolved as "@protocol
6778 identifier-list ;": objc-methodprotolist may not start with a
6779 semicolon in the first alternative if objc-protocol-refs are
6783 c_parser_objc_protocol_definition (c_parser *parser, tree attributes)
6785 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_PROTOCOL));
6787 c_parser_consume_token (parser);
6788 if (c_parser_next_token_is_not (parser, CPP_NAME))
6790 c_parser_error (parser, "expected identifier");
6793 if (c_parser_peek_2nd_token (parser)->type == CPP_COMMA
6794 || c_parser_peek_2nd_token (parser)->type == CPP_SEMICOLON)
6796 tree list = NULL_TREE;
6797 /* Any identifiers, including those declared as type names, are
6802 if (c_parser_next_token_is_not (parser, CPP_NAME))
6804 c_parser_error (parser, "expected identifier");
6807 id = c_parser_peek_token (parser)->value;
6808 list = chainon (list, build_tree_list (NULL_TREE, id));
6809 c_parser_consume_token (parser);
6810 if (c_parser_next_token_is (parser, CPP_COMMA))
6811 c_parser_consume_token (parser);
6815 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
6816 objc_declare_protocols (list);
6820 tree id = c_parser_peek_token (parser)->value;
6821 tree proto = NULL_TREE;
6822 c_parser_consume_token (parser);
6823 if (c_parser_next_token_is (parser, CPP_LESS))
6824 proto = c_parser_objc_protocol_refs (parser);
6825 parser->objc_pq_context = true;
6826 objc_start_protocol (id, proto, attributes);
6827 c_parser_objc_methodprotolist (parser);
6828 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
6829 parser->objc_pq_context = false;
6830 objc_finish_interface ();
6834 /* Parse an objc-method-type.
6841 static enum tree_code
6842 c_parser_objc_method_type (c_parser *parser)
6844 switch (c_parser_peek_token (parser)->type)
6847 c_parser_consume_token (parser);
6850 c_parser_consume_token (parser);
6857 /* Parse an objc-method-definition.
6859 objc-method-definition:
6860 objc-method-type objc-method-decl ;[opt] compound-statement
6864 c_parser_objc_method_definition (c_parser *parser)
6866 enum tree_code type = c_parser_objc_method_type (parser);
6867 tree decl, attributes = NULL_TREE;
6868 objc_set_method_type (type);
6869 parser->objc_pq_context = true;
6870 decl = c_parser_objc_method_decl (parser, &attributes);
6871 if (decl == error_mark_node)
6872 return; /* Bail here. */
6874 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
6876 c_parser_consume_token (parser);
6877 pedwarn (c_parser_peek_token (parser)->location, OPT_pedantic,
6878 "extra semicolon in method definition specified");
6881 if (!c_parser_next_token_is (parser, CPP_OPEN_BRACE))
6883 c_parser_error (parser, "expected %<{%>");
6887 parser->objc_pq_context = false;
6888 if (objc_start_method_definition (decl, attributes))
6890 add_stmt (c_parser_compound_statement (parser));
6891 objc_finish_method_definition (current_function_decl);
6895 /* This code is executed when we find a method definition
6896 outside of an @implementation context (or invalid for other
6897 reasons). Parse the method (to keep going) but do not emit
6900 c_parser_compound_statement (parser);
6904 /* Parse an objc-methodprotolist.
6906 objc-methodprotolist:
6908 objc-methodprotolist objc-methodproto
6909 objc-methodprotolist declaration
6910 objc-methodprotolist ;
6914 The declaration is a data definition, which may be missing
6915 declaration specifiers under the same rules and diagnostics as
6916 other data definitions outside functions, and the stray semicolon
6917 is diagnosed the same way as a stray semicolon outside a
6921 c_parser_objc_methodprotolist (c_parser *parser)
6925 /* The list is terminated by @end. */
6926 switch (c_parser_peek_token (parser)->type)
6929 pedwarn (c_parser_peek_token (parser)->location, OPT_pedantic,
6930 "ISO C does not allow extra %<;%> outside of a function");
6931 c_parser_consume_token (parser);
6935 c_parser_objc_methodproto (parser);
6938 c_parser_pragma (parser, pragma_external);
6943 if (c_parser_next_token_is_keyword (parser, RID_AT_END))
6945 else if (c_parser_next_token_is_keyword (parser, RID_AT_OPTIONAL))
6947 objc_set_method_opt (true);
6948 c_parser_consume_token (parser);
6950 else if (c_parser_next_token_is_keyword (parser, RID_AT_REQUIRED))
6952 objc_set_method_opt (false);
6953 c_parser_consume_token (parser);
6956 c_parser_declaration_or_fndef (parser, false, false, true,
6963 /* Parse an objc-methodproto.
6966 objc-method-type objc-method-decl ;
6970 c_parser_objc_methodproto (c_parser *parser)
6972 enum tree_code type = c_parser_objc_method_type (parser);
6973 tree decl, attributes = NULL_TREE;
6974 objc_set_method_type (type);
6975 /* Remember protocol qualifiers in prototypes. */
6976 parser->objc_pq_context = true;
6977 decl = c_parser_objc_method_decl (parser, &attributes);
6978 /* Forget protocol qualifiers now. */
6979 parser->objc_pq_context = false;
6981 /* Do not allow the presence of attributes to hide an erroneous
6982 method implementation in the interface section. */
6983 if (!c_parser_next_token_is (parser, CPP_SEMICOLON))
6985 c_parser_error (parser, "expected %<;%>");
6989 if (decl != error_mark_node)
6990 objc_add_method_declaration (decl, attributes);
6992 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
6995 /* If we are at a position that method attributes may be present, check that
6996 there are not any parsed already (a syntax error) and then collect any
6997 specified at the current location. Finally, if new attributes were present,
6998 check that the next token is legal ( ';' for decls and '{' for defs). */
7001 c_parser_objc_maybe_method_attributes (c_parser* parser, tree* attributes)
7006 c_parser_error (parser,
7007 "method attributes must be specified at the end only");
7008 *attributes = NULL_TREE;
7012 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
7013 *attributes = c_parser_attributes (parser);
7015 /* If there were no attributes here, just report any earlier error. */
7016 if (*attributes == NULL_TREE || bad)
7019 /* If the attributes are followed by a ; or {, then just report any earlier
7021 if (c_parser_next_token_is (parser, CPP_SEMICOLON)
7022 || c_parser_next_token_is (parser, CPP_OPEN_BRACE))
7025 /* We've got attributes, but not at the end. */
7026 c_parser_error (parser,
7027 "expected %<;%> or %<{%> after method attribute definition");
7031 /* Parse an objc-method-decl.
7034 ( objc-type-name ) objc-selector
7036 ( objc-type-name ) objc-keyword-selector objc-optparmlist
7037 objc-keyword-selector objc-optparmlist
7040 objc-keyword-selector:
7042 objc-keyword-selector objc-keyword-decl
7045 objc-selector : ( objc-type-name ) identifier
7046 objc-selector : identifier
7047 : ( objc-type-name ) identifier
7051 objc-optparms objc-optellipsis
7055 objc-opt-parms , parameter-declaration
7063 c_parser_objc_method_decl (c_parser *parser, tree *attributes)
7065 tree type = NULL_TREE;
7067 tree parms = NULL_TREE;
7068 bool ellipsis = false;
7069 bool attr_err = false;
7071 *attributes = NULL_TREE;
7072 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
7074 c_parser_consume_token (parser);
7075 type = c_parser_objc_type_name (parser);
7076 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
7078 sel = c_parser_objc_selector (parser);
7079 /* If there is no selector, or a colon follows, we have an
7080 objc-keyword-selector. If there is a selector, and a colon does
7081 not follow, that selector ends the objc-method-decl. */
7082 if (!sel || c_parser_next_token_is (parser, CPP_COLON))
7085 tree list = NULL_TREE;
7088 tree atype = NULL_TREE, id, keyworddecl;
7089 tree param_attr = NULL_TREE;
7090 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
7092 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
7094 c_parser_consume_token (parser);
7095 atype = c_parser_objc_type_name (parser);
7096 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7099 /* New ObjC allows attributes on method parameters. */
7100 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
7101 param_attr = c_parser_attributes (parser);
7102 if (c_parser_next_token_is_not (parser, CPP_NAME))
7104 c_parser_error (parser, "expected identifier");
7105 return error_mark_node;
7107 id = c_parser_peek_token (parser)->value;
7108 c_parser_consume_token (parser);
7109 keyworddecl = objc_build_keyword_decl (tsel, atype, id, param_attr);
7110 list = chainon (list, keyworddecl);
7111 tsel = c_parser_objc_selector (parser);
7112 if (!tsel && c_parser_next_token_is_not (parser, CPP_COLON))
7116 attr_err |= c_parser_objc_maybe_method_attributes (parser, attributes) ;
7118 /* Parse the optional parameter list. Optional Objective-C
7119 method parameters follow the C syntax, and may include '...'
7120 to denote a variable number of arguments. */
7121 parms = make_node (TREE_LIST);
7122 while (c_parser_next_token_is (parser, CPP_COMMA))
7124 struct c_parm *parm;
7125 c_parser_consume_token (parser);
7126 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
7129 c_parser_consume_token (parser);
7130 attr_err |= c_parser_objc_maybe_method_attributes
7131 (parser, attributes) ;
7134 parm = c_parser_parameter_declaration (parser, NULL_TREE);
7137 parms = chainon (parms,
7138 build_tree_list (NULL_TREE, grokparm (parm)));
7143 attr_err |= c_parser_objc_maybe_method_attributes (parser, attributes) ;
7147 c_parser_error (parser, "objective-c method declaration is expected");
7148 return error_mark_node;
7152 return error_mark_node;
7154 return objc_build_method_signature (type, sel, parms, ellipsis);
7157 /* Parse an objc-type-name.
7160 objc-type-qualifiers[opt] type-name
7161 objc-type-qualifiers[opt]
7163 objc-type-qualifiers:
7165 objc-type-qualifiers objc-type-qualifier
7167 objc-type-qualifier: one of
7168 in out inout bycopy byref oneway
7172 c_parser_objc_type_name (c_parser *parser)
7174 tree quals = NULL_TREE;
7175 struct c_type_name *type_name = NULL;
7176 tree type = NULL_TREE;
7179 c_token *token = c_parser_peek_token (parser);
7180 if (token->type == CPP_KEYWORD
7181 && (token->keyword == RID_IN
7182 || token->keyword == RID_OUT
7183 || token->keyword == RID_INOUT
7184 || token->keyword == RID_BYCOPY
7185 || token->keyword == RID_BYREF
7186 || token->keyword == RID_ONEWAY))
7188 quals = chainon (quals, build_tree_list (NULL_TREE, token->value));
7189 c_parser_consume_token (parser);
7194 if (c_parser_next_token_starts_typename (parser))
7195 type_name = c_parser_type_name (parser);
7197 type = groktypename (type_name, NULL, NULL);
7198 return build_tree_list (quals, type);
7201 /* Parse objc-protocol-refs.
7208 c_parser_objc_protocol_refs (c_parser *parser)
7210 tree list = NULL_TREE;
7211 gcc_assert (c_parser_next_token_is (parser, CPP_LESS));
7212 c_parser_consume_token (parser);
7213 /* Any identifiers, including those declared as type names, are OK
7218 if (c_parser_next_token_is_not (parser, CPP_NAME))
7220 c_parser_error (parser, "expected identifier");
7223 id = c_parser_peek_token (parser)->value;
7224 list = chainon (list, build_tree_list (NULL_TREE, id));
7225 c_parser_consume_token (parser);
7226 if (c_parser_next_token_is (parser, CPP_COMMA))
7227 c_parser_consume_token (parser);
7231 c_parser_require (parser, CPP_GREATER, "expected %<>%>");
7235 /* Parse an objc-try-catch-statement.
7237 objc-try-catch-statement:
7238 @try compound-statement objc-catch-list[opt]
7239 @try compound-statement objc-catch-list[opt] @finally compound-statement
7242 @catch ( parameter-declaration ) compound-statement
7243 objc-catch-list @catch ( parameter-declaration ) compound-statement
7247 c_parser_objc_try_catch_statement (c_parser *parser)
7251 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_TRY));
7252 c_parser_consume_token (parser);
7253 loc = c_parser_peek_token (parser)->location;
7254 stmt = c_parser_compound_statement (parser);
7255 objc_begin_try_stmt (loc, stmt);
7256 while (c_parser_next_token_is_keyword (parser, RID_AT_CATCH))
7258 struct c_parm *parm;
7259 c_parser_consume_token (parser);
7260 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7262 parm = c_parser_parameter_declaration (parser, NULL_TREE);
7265 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7268 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
7269 objc_begin_catch_clause (grokparm (parm));
7270 if (c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
7271 c_parser_compound_statement_nostart (parser);
7272 objc_finish_catch_clause ();
7274 if (c_parser_next_token_is_keyword (parser, RID_AT_FINALLY))
7278 c_parser_consume_token (parser);
7279 finloc = c_parser_peek_token (parser)->location;
7280 finstmt = c_parser_compound_statement (parser);
7281 objc_build_finally_clause (finloc, finstmt);
7283 objc_finish_try_stmt ();
7286 /* Parse an objc-synchronized-statement.
7288 objc-synchronized-statement:
7289 @synchronized ( expression ) compound-statement
7293 c_parser_objc_synchronized_statement (c_parser *parser)
7297 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_SYNCHRONIZED));
7298 c_parser_consume_token (parser);
7299 loc = c_parser_peek_token (parser)->location;
7300 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7302 expr = c_parser_expression (parser).value;
7303 expr = c_fully_fold (expr, false, NULL);
7304 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
7307 expr = error_mark_node;
7308 stmt = c_parser_compound_statement (parser);
7309 objc_build_synchronized (loc, expr, stmt);
7312 /* Parse an objc-selector; return NULL_TREE without an error if the
7313 next token is not an objc-selector.
7318 enum struct union if else while do for switch case default
7319 break continue return goto asm sizeof typeof __alignof
7320 unsigned long const short volatile signed restrict _Complex
7321 in out inout bycopy byref oneway int char float double void _Bool
7323 ??? Why this selection of keywords but not, for example, storage
7324 class specifiers? */
7327 c_parser_objc_selector (c_parser *parser)
7329 c_token *token = c_parser_peek_token (parser);
7330 tree value = token->value;
7331 if (token->type == CPP_NAME)
7333 c_parser_consume_token (parser);
7336 if (token->type != CPP_KEYWORD)
7338 switch (token->keyword)
7380 c_parser_consume_token (parser);
7387 /* Parse an objc-selector-arg.
7391 objc-keywordname-list
7393 objc-keywordname-list:
7395 objc-keywordname-list objc-keywordname
7403 c_parser_objc_selector_arg (c_parser *parser)
7405 tree sel = c_parser_objc_selector (parser);
7406 tree list = NULL_TREE;
7407 if (sel && c_parser_next_token_is_not (parser, CPP_COLON))
7411 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
7413 list = chainon (list, build_tree_list (sel, NULL_TREE));
7414 sel = c_parser_objc_selector (parser);
7415 if (!sel && c_parser_next_token_is_not (parser, CPP_COLON))
7421 /* Parse an objc-receiver.
7430 c_parser_objc_receiver (c_parser *parser)
7432 if (c_parser_peek_token (parser)->type == CPP_NAME
7433 && (c_parser_peek_token (parser)->id_kind == C_ID_TYPENAME
7434 || c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME))
7436 tree id = c_parser_peek_token (parser)->value;
7437 c_parser_consume_token (parser);
7438 return objc_get_class_reference (id);
7440 return c_fully_fold (c_parser_expression (parser).value, false, NULL);
7443 /* Parse objc-message-args.
7447 objc-keywordarg-list
7449 objc-keywordarg-list:
7451 objc-keywordarg-list objc-keywordarg
7454 objc-selector : objc-keywordexpr
7459 c_parser_objc_message_args (c_parser *parser)
7461 tree sel = c_parser_objc_selector (parser);
7462 tree list = NULL_TREE;
7463 if (sel && c_parser_next_token_is_not (parser, CPP_COLON))
7468 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
7469 return error_mark_node;
7470 keywordexpr = c_parser_objc_keywordexpr (parser);
7471 list = chainon (list, build_tree_list (sel, keywordexpr));
7472 sel = c_parser_objc_selector (parser);
7473 if (!sel && c_parser_next_token_is_not (parser, CPP_COLON))
7479 /* Parse an objc-keywordexpr.
7486 c_parser_objc_keywordexpr (c_parser *parser)
7489 VEC(tree,gc) *expr_list = c_parser_expr_list (parser, true, true, NULL);
7490 if (VEC_length (tree, expr_list) == 1)
7492 /* Just return the expression, remove a level of
7494 ret = VEC_index (tree, expr_list, 0);
7498 /* We have a comma expression, we will collapse later. */
7499 ret = build_tree_list_vec (expr_list);
7501 release_tree_vector (expr_list);
7505 /* A check, needed in several places, that ObjC interface, implementation or
7506 method definitions are not prefixed by incorrect items. */
7508 c_parser_objc_diagnose_bad_element_prefix (c_parser *parser,
7509 struct c_declspecs *specs)
7511 if (!specs->declspecs_seen_p || specs->type_seen_p || specs->non_sc_seen_p)
7513 c_parser_error (parser,
7514 "no type or storage class may be specified here,");
7515 c_parser_skip_to_end_of_block_or_statement (parser);
7521 /* Handle pragmas. Some OpenMP pragmas are associated with, and therefore
7522 should be considered, statements. ALLOW_STMT is true if we're within
7523 the context of a function and such pragmas are to be allowed. Returns
7524 true if we actually parsed such a pragma. */
7527 c_parser_pragma (c_parser *parser, enum pragma_context context)
7531 id = c_parser_peek_token (parser)->pragma_kind;
7532 gcc_assert (id != PRAGMA_NONE);
7536 case PRAGMA_OMP_BARRIER:
7537 if (context != pragma_compound)
7539 if (context == pragma_stmt)
7540 c_parser_error (parser, "%<#pragma omp barrier%> may only be "
7541 "used in compound statements");
7544 c_parser_omp_barrier (parser);
7547 case PRAGMA_OMP_FLUSH:
7548 if (context != pragma_compound)
7550 if (context == pragma_stmt)
7551 c_parser_error (parser, "%<#pragma omp flush%> may only be "
7552 "used in compound statements");
7555 c_parser_omp_flush (parser);
7558 case PRAGMA_OMP_TASKWAIT:
7559 if (context != pragma_compound)
7561 if (context == pragma_stmt)
7562 c_parser_error (parser, "%<#pragma omp taskwait%> may only be "
7563 "used in compound statements");
7566 c_parser_omp_taskwait (parser);
7569 case PRAGMA_OMP_THREADPRIVATE:
7570 c_parser_omp_threadprivate (parser);
7573 case PRAGMA_OMP_SECTION:
7574 error_at (c_parser_peek_token (parser)->location,
7575 "%<#pragma omp section%> may only be used in "
7576 "%<#pragma omp sections%> construct");
7577 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
7580 case PRAGMA_GCC_PCH_PREPROCESS:
7581 c_parser_error (parser, "%<#pragma GCC pch_preprocess%> must be first");
7582 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
7586 if (id < PRAGMA_FIRST_EXTERNAL)
7588 if (context == pragma_external)
7591 c_parser_error (parser, "expected declaration specifiers");
7592 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
7595 c_parser_omp_construct (parser);
7601 c_parser_consume_pragma (parser);
7602 c_invoke_pragma_handler (id);
7604 /* Skip to EOL, but suppress any error message. Those will have been
7605 generated by the handler routine through calling error, as opposed
7606 to calling c_parser_error. */
7607 parser->error = true;
7608 c_parser_skip_to_pragma_eol (parser);
7613 /* The interface the pragma parsers have to the lexer. */
7616 pragma_lex (tree *value)
7618 c_token *tok = c_parser_peek_token (the_parser);
7619 enum cpp_ttype ret = tok->type;
7621 *value = tok->value;
7622 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
7626 if (ret == CPP_KEYWORD)
7628 c_parser_consume_token (the_parser);
7635 c_parser_pragma_pch_preprocess (c_parser *parser)
7639 c_parser_consume_pragma (parser);
7640 if (c_parser_next_token_is (parser, CPP_STRING))
7642 name = c_parser_peek_token (parser)->value;
7643 c_parser_consume_token (parser);
7646 c_parser_error (parser, "expected string literal");
7647 c_parser_skip_to_pragma_eol (parser);
7650 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
7653 /* OpenMP 2.5 parsing routines. */
7655 /* Returns name of the next clause.
7656 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
7657 the token is not consumed. Otherwise appropriate pragma_omp_clause is
7658 returned and the token is consumed. */
7660 static pragma_omp_clause
7661 c_parser_omp_clause_name (c_parser *parser)
7663 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
7665 if (c_parser_next_token_is_keyword (parser, RID_IF))
7666 result = PRAGMA_OMP_CLAUSE_IF;
7667 else if (c_parser_next_token_is_keyword (parser, RID_DEFAULT))
7668 result = PRAGMA_OMP_CLAUSE_DEFAULT;
7669 else if (c_parser_next_token_is (parser, CPP_NAME))
7671 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
7676 if (!strcmp ("collapse", p))
7677 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
7678 else if (!strcmp ("copyin", p))
7679 result = PRAGMA_OMP_CLAUSE_COPYIN;
7680 else if (!strcmp ("copyprivate", p))
7681 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
7684 if (!strcmp ("firstprivate", p))
7685 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
7688 if (!strcmp ("lastprivate", p))
7689 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
7692 if (!strcmp ("nowait", p))
7693 result = PRAGMA_OMP_CLAUSE_NOWAIT;
7694 else if (!strcmp ("num_threads", p))
7695 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
7698 if (!strcmp ("ordered", p))
7699 result = PRAGMA_OMP_CLAUSE_ORDERED;
7702 if (!strcmp ("private", p))
7703 result = PRAGMA_OMP_CLAUSE_PRIVATE;
7706 if (!strcmp ("reduction", p))
7707 result = PRAGMA_OMP_CLAUSE_REDUCTION;
7710 if (!strcmp ("schedule", p))
7711 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
7712 else if (!strcmp ("shared", p))
7713 result = PRAGMA_OMP_CLAUSE_SHARED;
7716 if (!strcmp ("untied", p))
7717 result = PRAGMA_OMP_CLAUSE_UNTIED;
7722 if (result != PRAGMA_OMP_CLAUSE_NONE)
7723 c_parser_consume_token (parser);
7728 /* Validate that a clause of the given type does not already exist. */
7731 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
7736 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
7737 if (OMP_CLAUSE_CODE (c) == code)
7739 location_t loc = OMP_CLAUSE_LOCATION (c);
7740 error_at (loc, "too many %qs clauses", name);
7748 variable-list , identifier
7750 If KIND is nonzero, create the appropriate node and install the
7751 decl in OMP_CLAUSE_DECL and add the node to the head of the list.
7752 If KIND is nonzero, CLAUSE_LOC is the location of the clause.
7754 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
7755 return the list created. */
7758 c_parser_omp_variable_list (c_parser *parser,
7759 location_t clause_loc,
7760 enum omp_clause_code kind,
7763 if (c_parser_next_token_is_not (parser, CPP_NAME)
7764 || c_parser_peek_token (parser)->id_kind != C_ID_ID)
7765 c_parser_error (parser, "expected identifier");
7767 while (c_parser_next_token_is (parser, CPP_NAME)
7768 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
7770 tree t = lookup_name (c_parser_peek_token (parser)->value);
7773 undeclared_variable (c_parser_peek_token (parser)->location,
7774 c_parser_peek_token (parser)->value);
7775 else if (t == error_mark_node)
7779 tree u = build_omp_clause (clause_loc, kind);
7780 OMP_CLAUSE_DECL (u) = t;
7781 OMP_CLAUSE_CHAIN (u) = list;
7785 list = tree_cons (t, NULL_TREE, list);
7787 c_parser_consume_token (parser);
7789 if (c_parser_next_token_is_not (parser, CPP_COMMA))
7792 c_parser_consume_token (parser);
7798 /* Similarly, but expect leading and trailing parenthesis. This is a very
7799 common case for omp clauses. */
7802 c_parser_omp_var_list_parens (c_parser *parser, enum omp_clause_code kind,
7805 /* The clauses location. */
7806 location_t loc = c_parser_peek_token (parser)->location;
7808 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7810 list = c_parser_omp_variable_list (parser, loc, kind, list);
7811 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
7817 collapse ( constant-expression ) */
7820 c_parser_omp_clause_collapse (c_parser *parser, tree list)
7822 tree c, num = error_mark_node;
7826 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse");
7828 loc = c_parser_peek_token (parser)->location;
7829 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7831 num = c_parser_expr_no_commas (parser, NULL).value;
7832 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
7834 if (num == error_mark_node)
7836 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
7837 || !host_integerp (num, 0)
7838 || (n = tree_low_cst (num, 0)) <= 0
7842 "collapse argument needs positive constant integer expression");
7845 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
7846 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
7847 OMP_CLAUSE_CHAIN (c) = list;
7852 copyin ( variable-list ) */
7855 c_parser_omp_clause_copyin (c_parser *parser, tree list)
7857 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_COPYIN, list);
7861 copyprivate ( variable-list ) */
7864 c_parser_omp_clause_copyprivate (c_parser *parser, tree list)
7866 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_COPYPRIVATE, list);
7870 default ( shared | none ) */
7873 c_parser_omp_clause_default (c_parser *parser, tree list)
7875 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
7876 location_t loc = c_parser_peek_token (parser)->location;
7879 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7881 if (c_parser_next_token_is (parser, CPP_NAME))
7883 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
7888 if (strcmp ("none", p) != 0)
7890 kind = OMP_CLAUSE_DEFAULT_NONE;
7894 if (strcmp ("shared", p) != 0)
7896 kind = OMP_CLAUSE_DEFAULT_SHARED;
7903 c_parser_consume_token (parser);
7908 c_parser_error (parser, "expected %<none%> or %<shared%>");
7910 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
7912 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
7915 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default");
7916 c = build_omp_clause (loc, OMP_CLAUSE_DEFAULT);
7917 OMP_CLAUSE_CHAIN (c) = list;
7918 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
7924 firstprivate ( variable-list ) */
7927 c_parser_omp_clause_firstprivate (c_parser *parser, tree list)
7929 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_FIRSTPRIVATE, list);
7933 if ( expression ) */
7936 c_parser_omp_clause_if (c_parser *parser, tree list)
7938 location_t loc = c_parser_peek_token (parser)->location;
7939 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
7941 tree t = c_parser_paren_condition (parser);
7944 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if");
7946 c = build_omp_clause (loc, OMP_CLAUSE_IF);
7947 OMP_CLAUSE_IF_EXPR (c) = t;
7948 OMP_CLAUSE_CHAIN (c) = list;
7952 c_parser_error (parser, "expected %<(%>");
7958 lastprivate ( variable-list ) */
7961 c_parser_omp_clause_lastprivate (c_parser *parser, tree list)
7963 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_LASTPRIVATE, list);
7970 c_parser_omp_clause_nowait (c_parser *parser ATTRIBUTE_UNUSED, tree list)
7973 location_t loc = c_parser_peek_token (parser)->location;
7975 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait");
7977 c = build_omp_clause (loc, OMP_CLAUSE_NOWAIT);
7978 OMP_CLAUSE_CHAIN (c) = list;
7983 num_threads ( expression ) */
7986 c_parser_omp_clause_num_threads (c_parser *parser, tree list)
7988 location_t num_threads_loc = c_parser_peek_token (parser)->location;
7989 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7991 location_t expr_loc = c_parser_peek_token (parser)->location;
7992 tree c, t = c_parser_expression (parser).value;
7993 t = c_fully_fold (t, false, NULL);
7995 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
7997 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
7999 c_parser_error (parser, "expected integer expression");
8003 /* Attempt to statically determine when the number isn't positive. */
8004 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
8005 build_int_cst (TREE_TYPE (t), 0));
8006 if (CAN_HAVE_LOCATION_P (c))
8007 SET_EXPR_LOCATION (c, expr_loc);
8008 if (c == boolean_true_node)
8010 warning_at (expr_loc, 0,
8011 "%<num_threads%> value must be positive");
8012 t = integer_one_node;
8015 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS, "num_threads");
8017 c = build_omp_clause (num_threads_loc, OMP_CLAUSE_NUM_THREADS);
8018 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
8019 OMP_CLAUSE_CHAIN (c) = list;
8030 c_parser_omp_clause_ordered (c_parser *parser, tree list)
8034 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED, "ordered");
8036 c = build_omp_clause (c_parser_peek_token (parser)->location,
8037 OMP_CLAUSE_ORDERED);
8038 OMP_CLAUSE_CHAIN (c) = list;
8044 private ( variable-list ) */
8047 c_parser_omp_clause_private (c_parser *parser, tree list)
8049 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_PRIVATE, list);
8053 reduction ( reduction-operator : variable-list )
8056 One of: + * - & ^ | && || */
8059 c_parser_omp_clause_reduction (c_parser *parser, tree list)
8061 location_t clause_loc = c_parser_peek_token (parser)->location;
8062 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
8064 enum tree_code code;
8066 switch (c_parser_peek_token (parser)->type)
8078 code = BIT_AND_EXPR;
8081 code = BIT_XOR_EXPR;
8084 code = BIT_IOR_EXPR;
8087 code = TRUTH_ANDIF_EXPR;
8090 code = TRUTH_ORIF_EXPR;
8093 c_parser_error (parser,
8094 "expected %<+%>, %<*%>, %<-%>, %<&%>, "
8095 "%<^%>, %<|%>, %<&&%>, or %<||%>");
8096 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 0);
8099 c_parser_consume_token (parser);
8100 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
8104 nl = c_parser_omp_variable_list (parser, clause_loc,
8105 OMP_CLAUSE_REDUCTION, list);
8106 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
8107 OMP_CLAUSE_REDUCTION_CODE (c) = code;
8111 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
8117 schedule ( schedule-kind )
8118 schedule ( schedule-kind , expression )
8121 static | dynamic | guided | runtime | auto
8125 c_parser_omp_clause_schedule (c_parser *parser, tree list)
8128 location_t loc = c_parser_peek_token (parser)->location;
8130 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
8133 c = build_omp_clause (loc, OMP_CLAUSE_SCHEDULE);
8135 if (c_parser_next_token_is (parser, CPP_NAME))
8137 tree kind = c_parser_peek_token (parser)->value;
8138 const char *p = IDENTIFIER_POINTER (kind);
8143 if (strcmp ("dynamic", p) != 0)
8145 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
8149 if (strcmp ("guided", p) != 0)
8151 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
8155 if (strcmp ("runtime", p) != 0)
8157 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
8164 else if (c_parser_next_token_is_keyword (parser, RID_STATIC))
8165 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
8166 else if (c_parser_next_token_is_keyword (parser, RID_AUTO))
8167 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
8171 c_parser_consume_token (parser);
8172 if (c_parser_next_token_is (parser, CPP_COMMA))
8175 c_parser_consume_token (parser);
8177 here = c_parser_peek_token (parser)->location;
8178 t = c_parser_expr_no_commas (parser, NULL).value;
8179 t = c_fully_fold (t, false, NULL);
8181 if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
8182 error_at (here, "schedule %<runtime%> does not take "
8183 "a %<chunk_size%> parameter");
8184 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
8186 "schedule %<auto%> does not take "
8187 "a %<chunk_size%> parameter");
8188 else if (TREE_CODE (TREE_TYPE (t)) == INTEGER_TYPE)
8189 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
8191 c_parser_error (parser, "expected integer expression");
8193 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
8196 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
8197 "expected %<,%> or %<)%>");
8199 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule");
8200 OMP_CLAUSE_CHAIN (c) = list;
8204 c_parser_error (parser, "invalid schedule kind");
8205 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 0);
8210 shared ( variable-list ) */
8213 c_parser_omp_clause_shared (c_parser *parser, tree list)
8215 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_SHARED, list);
8222 c_parser_omp_clause_untied (c_parser *parser ATTRIBUTE_UNUSED, tree list)
8226 /* FIXME: Should we allow duplicates? */
8227 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied");
8229 c = build_omp_clause (c_parser_peek_token (parser)->location,
8231 OMP_CLAUSE_CHAIN (c) = list;
8236 /* Parse all OpenMP clauses. The set clauses allowed by the directive
8237 is a bitmask in MASK. Return the list of clauses found; the result
8238 of clause default goes in *pdefault. */
8241 c_parser_omp_all_clauses (c_parser *parser, unsigned int mask,
8244 tree clauses = NULL;
8247 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
8250 pragma_omp_clause c_kind;
8252 tree prev = clauses;
8254 if (!first && c_parser_next_token_is (parser, CPP_COMMA))
8255 c_parser_consume_token (parser);
8258 here = c_parser_peek_token (parser)->location;
8259 c_kind = c_parser_omp_clause_name (parser);
8263 case PRAGMA_OMP_CLAUSE_COLLAPSE:
8264 clauses = c_parser_omp_clause_collapse (parser, clauses);
8265 c_name = "collapse";
8267 case PRAGMA_OMP_CLAUSE_COPYIN:
8268 clauses = c_parser_omp_clause_copyin (parser, clauses);
8271 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
8272 clauses = c_parser_omp_clause_copyprivate (parser, clauses);
8273 c_name = "copyprivate";
8275 case PRAGMA_OMP_CLAUSE_DEFAULT:
8276 clauses = c_parser_omp_clause_default (parser, clauses);
8279 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
8280 clauses = c_parser_omp_clause_firstprivate (parser, clauses);
8281 c_name = "firstprivate";
8283 case PRAGMA_OMP_CLAUSE_IF:
8284 clauses = c_parser_omp_clause_if (parser, clauses);
8287 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
8288 clauses = c_parser_omp_clause_lastprivate (parser, clauses);
8289 c_name = "lastprivate";
8291 case PRAGMA_OMP_CLAUSE_NOWAIT:
8292 clauses = c_parser_omp_clause_nowait (parser, clauses);
8295 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
8296 clauses = c_parser_omp_clause_num_threads (parser, clauses);
8297 c_name = "num_threads";
8299 case PRAGMA_OMP_CLAUSE_ORDERED:
8300 clauses = c_parser_omp_clause_ordered (parser, clauses);
8303 case PRAGMA_OMP_CLAUSE_PRIVATE:
8304 clauses = c_parser_omp_clause_private (parser, clauses);
8307 case PRAGMA_OMP_CLAUSE_REDUCTION:
8308 clauses = c_parser_omp_clause_reduction (parser, clauses);
8309 c_name = "reduction";
8311 case PRAGMA_OMP_CLAUSE_SCHEDULE:
8312 clauses = c_parser_omp_clause_schedule (parser, clauses);
8313 c_name = "schedule";
8315 case PRAGMA_OMP_CLAUSE_SHARED:
8316 clauses = c_parser_omp_clause_shared (parser, clauses);
8319 case PRAGMA_OMP_CLAUSE_UNTIED:
8320 clauses = c_parser_omp_clause_untied (parser, clauses);
8324 c_parser_error (parser, "expected %<#pragma omp%> clause");
8328 if (((mask >> c_kind) & 1) == 0 && !parser->error)
8330 /* Remove the invalid clause(s) from the list to avoid
8331 confusing the rest of the compiler. */
8333 error_at (here, "%qs is not valid for %qs", c_name, where);
8338 c_parser_skip_to_pragma_eol (parser);
8340 return c_finish_omp_clauses (clauses);
8347 In practice, we're also interested in adding the statement to an
8348 outer node. So it is convenient if we work around the fact that
8349 c_parser_statement calls add_stmt. */
8352 c_parser_omp_structured_block (c_parser *parser)
8354 tree stmt = push_stmt_list ();
8355 c_parser_statement (parser);
8356 return pop_stmt_list (stmt);
8360 # pragma omp atomic new-line
8364 x binop= expr | x++ | ++x | x-- | --x
8366 +, *, -, /, &, ^, |, <<, >>
8368 where x is an lvalue expression with scalar type.
8370 LOC is the location of the #pragma token. */
8373 c_parser_omp_atomic (location_t loc, c_parser *parser)
8377 enum tree_code code;
8378 struct c_expr rhs_expr;
8380 c_parser_skip_to_pragma_eol (parser);
8382 lhs = c_parser_unary_expression (parser).value;
8383 lhs = c_fully_fold (lhs, false, NULL);
8384 switch (TREE_CODE (lhs))
8388 c_parser_skip_to_end_of_block_or_statement (parser);
8391 case PREINCREMENT_EXPR:
8392 case POSTINCREMENT_EXPR:
8393 lhs = TREE_OPERAND (lhs, 0);
8395 rhs = integer_one_node;
8398 case PREDECREMENT_EXPR:
8399 case POSTDECREMENT_EXPR:
8400 lhs = TREE_OPERAND (lhs, 0);
8402 rhs = integer_one_node;
8406 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
8407 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
8408 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
8409 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
8410 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
8411 (TREE_OPERAND (lhs, 1), 0), 0)))
8413 /* Undo effects of boolean_increment for post {in,de}crement. */
8414 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
8417 if (TREE_CODE (lhs) == MODIFY_EXPR
8418 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
8420 /* Undo effects of boolean_increment. */
8421 if (integer_onep (TREE_OPERAND (lhs, 1)))
8423 /* This is pre or post increment. */
8424 rhs = TREE_OPERAND (lhs, 1);
8425 lhs = TREE_OPERAND (lhs, 0);
8429 if (TREE_CODE (TREE_OPERAND (lhs, 1)) == TRUTH_NOT_EXPR
8430 && TREE_OPERAND (lhs, 0)
8431 == TREE_OPERAND (TREE_OPERAND (lhs, 1), 0))
8433 /* This is pre or post decrement. */
8434 rhs = TREE_OPERAND (lhs, 1);
8435 lhs = TREE_OPERAND (lhs, 0);
8442 switch (c_parser_peek_token (parser)->type)
8448 code = TRUNC_DIV_EXPR;
8463 code = BIT_AND_EXPR;
8466 code = BIT_IOR_EXPR;
8469 code = BIT_XOR_EXPR;
8472 c_parser_error (parser,
8473 "invalid operator for %<#pragma omp atomic%>");
8477 c_parser_consume_token (parser);
8479 location_t rhs_loc = c_parser_peek_token (parser)->location;
8480 rhs_expr = c_parser_expression (parser);
8481 rhs_expr = default_function_array_read_conversion (rhs_loc, rhs_expr);
8483 rhs = rhs_expr.value;
8484 rhs = c_fully_fold (rhs, false, NULL);
8487 stmt = c_finish_omp_atomic (loc, code, lhs, rhs);
8488 if (stmt != error_mark_node)
8490 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
8495 # pragma omp barrier new-line
8499 c_parser_omp_barrier (c_parser *parser)
8501 location_t loc = c_parser_peek_token (parser)->location;
8502 c_parser_consume_pragma (parser);
8503 c_parser_skip_to_pragma_eol (parser);
8505 c_finish_omp_barrier (loc);
8509 # pragma omp critical [(name)] new-line
8512 LOC is the location of the #pragma itself. */
8515 c_parser_omp_critical (location_t loc, c_parser *parser)
8517 tree stmt, name = NULL;
8519 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
8521 c_parser_consume_token (parser);
8522 if (c_parser_next_token_is (parser, CPP_NAME))
8524 name = c_parser_peek_token (parser)->value;
8525 c_parser_consume_token (parser);
8526 c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
8529 c_parser_error (parser, "expected identifier");
8531 else if (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
8532 c_parser_error (parser, "expected %<(%> or end of line");
8533 c_parser_skip_to_pragma_eol (parser);
8535 stmt = c_parser_omp_structured_block (parser);
8536 return c_finish_omp_critical (loc, stmt, name);
8540 # pragma omp flush flush-vars[opt] new-line
8543 ( variable-list ) */
8546 c_parser_omp_flush (c_parser *parser)
8548 location_t loc = c_parser_peek_token (parser)->location;
8549 c_parser_consume_pragma (parser);
8550 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
8551 c_parser_omp_var_list_parens (parser, OMP_CLAUSE_ERROR, NULL);
8552 else if (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
8553 c_parser_error (parser, "expected %<(%> or end of line");
8554 c_parser_skip_to_pragma_eol (parser);
8556 c_finish_omp_flush (loc);
8559 /* Parse the restricted form of the for statement allowed by OpenMP.
8560 The real trick here is to determine the loop control variable early
8561 so that we can push a new decl if necessary to make it private.
8562 LOC is the location of the OMP in "#pragma omp". */
8565 c_parser_omp_for_loop (location_t loc,
8566 c_parser *parser, tree clauses, tree *par_clauses)
8568 tree decl, cond, incr, save_break, save_cont, body, init, stmt, cl;
8569 tree declv, condv, incrv, initv, ret = NULL;
8570 bool fail = false, open_brace_parsed = false;
8571 int i, collapse = 1, nbraces = 0;
8573 VEC(tree,gc) *for_block = make_tree_vector ();
8575 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
8576 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
8577 collapse = tree_low_cst (OMP_CLAUSE_COLLAPSE_EXPR (cl), 0);
8579 gcc_assert (collapse >= 1);
8581 declv = make_tree_vec (collapse);
8582 initv = make_tree_vec (collapse);
8583 condv = make_tree_vec (collapse);
8584 incrv = make_tree_vec (collapse);
8586 if (!c_parser_next_token_is_keyword (parser, RID_FOR))
8588 c_parser_error (parser, "for statement expected");
8591 for_loc = c_parser_peek_token (parser)->location;
8592 c_parser_consume_token (parser);
8594 for (i = 0; i < collapse; i++)
8598 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
8601 /* Parse the initialization declaration or expression. */
8602 if (c_parser_next_token_starts_declaration (parser))
8605 VEC_safe_push (tree, gc, for_block, c_begin_compound_stmt (true));
8606 c_parser_declaration_or_fndef (parser, true, true, true, true, true, NULL);
8607 decl = check_for_loop_decls (for_loc, flag_isoc99);
8610 if (DECL_INITIAL (decl) == error_mark_node)
8611 decl = error_mark_node;
8614 else if (c_parser_next_token_is (parser, CPP_NAME)
8615 && c_parser_peek_2nd_token (parser)->type == CPP_EQ)
8617 struct c_expr decl_exp;
8618 struct c_expr init_exp;
8619 location_t init_loc;
8621 decl_exp = c_parser_postfix_expression (parser);
8622 decl = decl_exp.value;
8624 c_parser_require (parser, CPP_EQ, "expected %<=%>");
8626 init_loc = c_parser_peek_token (parser)->location;
8627 init_exp = c_parser_expr_no_commas (parser, NULL);
8628 init_exp = default_function_array_read_conversion (init_loc,
8630 init = build_modify_expr (init_loc, decl, decl_exp.original_type,
8631 NOP_EXPR, init_loc, init_exp.value,
8632 init_exp.original_type);
8633 init = c_process_expr_stmt (init_loc, init);
8635 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
8640 c_parser_error (parser,
8641 "expected iteration declaration or initialization");
8642 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
8648 /* Parse the loop condition. */
8650 if (c_parser_next_token_is_not (parser, CPP_SEMICOLON))
8652 location_t cond_loc = c_parser_peek_token (parser)->location;
8653 struct c_expr cond_expr = c_parser_binary_expression (parser, NULL);
8655 cond = cond_expr.value;
8656 cond = c_objc_common_truthvalue_conversion (cond_loc, cond);
8657 cond = c_fully_fold (cond, false, NULL);
8658 switch (cond_expr.original_code)
8666 /* Can't be cond = error_mark_node, because we want to preserve
8667 the location until c_finish_omp_for. */
8668 cond = build1 (NOP_EXPR, boolean_type_node, error_mark_node);
8671 protected_set_expr_location (cond, cond_loc);
8673 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
8675 /* Parse the increment expression. */
8677 if (c_parser_next_token_is_not (parser, CPP_CLOSE_PAREN))
8679 location_t incr_loc = c_parser_peek_token (parser)->location;
8681 incr = c_process_expr_stmt (incr_loc,
8682 c_parser_expression (parser).value);
8684 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
8686 if (decl == NULL || decl == error_mark_node || init == error_mark_node)
8690 TREE_VEC_ELT (declv, i) = decl;
8691 TREE_VEC_ELT (initv, i) = init;
8692 TREE_VEC_ELT (condv, i) = cond;
8693 TREE_VEC_ELT (incrv, i) = incr;
8697 if (i == collapse - 1)
8700 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
8701 in between the collapsed for loops to be still considered perfectly
8702 nested. Hopefully the final version clarifies this.
8703 For now handle (multiple) {'s and empty statements. */
8706 if (c_parser_next_token_is_keyword (parser, RID_FOR))
8708 c_parser_consume_token (parser);
8711 else if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
8713 c_parser_consume_token (parser);
8717 && c_parser_next_token_is (parser, CPP_SEMICOLON))
8718 c_parser_consume_token (parser);
8721 c_parser_error (parser, "not enough perfectly nested loops");
8724 open_brace_parsed = true;
8734 nbraces += bracecount;
8737 save_break = c_break_label;
8738 c_break_label = size_one_node;
8739 save_cont = c_cont_label;
8740 c_cont_label = NULL_TREE;
8741 body = push_stmt_list ();
8743 if (open_brace_parsed)
8745 location_t here = c_parser_peek_token (parser)->location;
8746 stmt = c_begin_compound_stmt (true);
8747 c_parser_compound_statement_nostart (parser);
8748 add_stmt (c_end_compound_stmt (here, stmt, true));
8751 add_stmt (c_parser_c99_block_statement (parser));
8754 tree t = build1 (LABEL_EXPR, void_type_node, c_cont_label);
8755 SET_EXPR_LOCATION (t, loc);
8759 body = pop_stmt_list (body);
8760 c_break_label = save_break;
8761 c_cont_label = save_cont;
8765 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
8767 c_parser_consume_token (parser);
8770 else if (c_parser_next_token_is (parser, CPP_SEMICOLON))
8771 c_parser_consume_token (parser);
8774 c_parser_error (parser, "collapsed loops not perfectly nested");
8777 location_t here = c_parser_peek_token (parser)->location;
8778 stmt = c_begin_compound_stmt (true);
8780 c_parser_compound_statement_nostart (parser);
8781 body = c_end_compound_stmt (here, stmt, true);
8788 /* Only bother calling c_finish_omp_for if we haven't already generated
8789 an error from the initialization parsing. */
8792 stmt = c_finish_omp_for (loc, declv, initv, condv, incrv, body, NULL);
8795 if (par_clauses != NULL)
8798 for (c = par_clauses; *c ; )
8799 if (OMP_CLAUSE_CODE (*c) != OMP_CLAUSE_FIRSTPRIVATE
8800 && OMP_CLAUSE_CODE (*c) != OMP_CLAUSE_LASTPRIVATE)
8801 c = &OMP_CLAUSE_CHAIN (*c);
8804 for (i = 0; i < collapse; i++)
8805 if (TREE_VEC_ELT (declv, i) == OMP_CLAUSE_DECL (*c))
8808 c = &OMP_CLAUSE_CHAIN (*c);
8809 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE)
8812 "iteration variable %qD should not be firstprivate",
8813 OMP_CLAUSE_DECL (*c));
8814 *c = OMP_CLAUSE_CHAIN (*c);
8818 /* Copy lastprivate (decl) clause to OMP_FOR_CLAUSES,
8819 change it to shared (decl) in
8820 OMP_PARALLEL_CLAUSES. */
8821 tree l = build_omp_clause (OMP_CLAUSE_LOCATION (*c),
8822 OMP_CLAUSE_LASTPRIVATE);
8823 OMP_CLAUSE_DECL (l) = OMP_CLAUSE_DECL (*c);
8824 OMP_CLAUSE_CHAIN (l) = clauses;
8826 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
8830 OMP_FOR_CLAUSES (stmt) = clauses;
8835 while (!VEC_empty (tree, for_block))
8837 /* FIXME diagnostics: LOC below should be the actual location of
8838 this particular for block. We need to build a list of
8839 locations to go along with FOR_BLOCK. */
8840 stmt = c_end_compound_stmt (loc, VEC_pop (tree, for_block), true);
8843 release_tree_vector (for_block);
8848 #pragma omp for for-clause[optseq] new-line
8851 LOC is the location of the #pragma token.
8854 #define OMP_FOR_CLAUSE_MASK \
8855 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
8856 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
8857 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
8858 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
8859 | (1u << PRAGMA_OMP_CLAUSE_ORDERED) \
8860 | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE) \
8861 | (1u << PRAGMA_OMP_CLAUSE_COLLAPSE) \
8862 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
8865 c_parser_omp_for (location_t loc, c_parser *parser)
8867 tree block, clauses, ret;
8869 clauses = c_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
8872 block = c_begin_compound_stmt (true);
8873 ret = c_parser_omp_for_loop (loc, parser, clauses, NULL);
8874 block = c_end_compound_stmt (loc, block, true);
8881 # pragma omp master new-line
8884 LOC is the location of the #pragma token.
8888 c_parser_omp_master (location_t loc, c_parser *parser)
8890 c_parser_skip_to_pragma_eol (parser);
8891 return c_finish_omp_master (loc, c_parser_omp_structured_block (parser));
8895 # pragma omp ordered new-line
8898 LOC is the location of the #pragma itself.
8902 c_parser_omp_ordered (location_t loc, c_parser *parser)
8904 c_parser_skip_to_pragma_eol (parser);
8905 return c_finish_omp_ordered (loc, c_parser_omp_structured_block (parser));
8911 { section-sequence }
8914 section-directive[opt] structured-block
8915 section-sequence section-directive structured-block
8917 SECTIONS_LOC is the location of the #pragma omp sections. */
8920 c_parser_omp_sections_scope (location_t sections_loc, c_parser *parser)
8923 bool error_suppress = false;
8926 loc = c_parser_peek_token (parser)->location;
8927 if (!c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
8929 /* Avoid skipping until the end of the block. */
8930 parser->error = false;
8934 stmt = push_stmt_list ();
8936 if (c_parser_peek_token (parser)->pragma_kind != PRAGMA_OMP_SECTION)
8938 substmt = push_stmt_list ();
8942 c_parser_statement (parser);
8944 if (c_parser_peek_token (parser)->pragma_kind == PRAGMA_OMP_SECTION)
8946 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
8948 if (c_parser_next_token_is (parser, CPP_EOF))
8952 substmt = pop_stmt_list (substmt);
8953 substmt = build1 (OMP_SECTION, void_type_node, substmt);
8954 SET_EXPR_LOCATION (substmt, loc);
8960 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
8962 if (c_parser_next_token_is (parser, CPP_EOF))
8965 loc = c_parser_peek_token (parser)->location;
8966 if (c_parser_peek_token (parser)->pragma_kind == PRAGMA_OMP_SECTION)
8968 c_parser_consume_pragma (parser);
8969 c_parser_skip_to_pragma_eol (parser);
8970 error_suppress = false;
8972 else if (!error_suppress)
8974 error_at (loc, "expected %<#pragma omp section%> or %<}%>");
8975 error_suppress = true;
8978 substmt = c_parser_omp_structured_block (parser);
8979 substmt = build1 (OMP_SECTION, void_type_node, substmt);
8980 SET_EXPR_LOCATION (substmt, loc);
8983 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE,
8984 "expected %<#pragma omp section%> or %<}%>");
8986 substmt = pop_stmt_list (stmt);
8988 stmt = make_node (OMP_SECTIONS);
8989 SET_EXPR_LOCATION (stmt, sections_loc);
8990 TREE_TYPE (stmt) = void_type_node;
8991 OMP_SECTIONS_BODY (stmt) = substmt;
8993 return add_stmt (stmt);
8997 # pragma omp sections sections-clause[optseq] newline
9000 LOC is the location of the #pragma token.
9003 #define OMP_SECTIONS_CLAUSE_MASK \
9004 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
9005 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
9006 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
9007 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
9008 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
9011 c_parser_omp_sections (location_t loc, c_parser *parser)
9013 tree block, clauses, ret;
9015 clauses = c_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
9016 "#pragma omp sections");
9018 block = c_begin_compound_stmt (true);
9019 ret = c_parser_omp_sections_scope (loc, parser);
9021 OMP_SECTIONS_CLAUSES (ret) = clauses;
9022 block = c_end_compound_stmt (loc, block, true);
9029 # pragma parallel parallel-clause new-line
9030 # pragma parallel for parallel-for-clause new-line
9031 # pragma parallel sections parallel-sections-clause new-line
9033 LOC is the location of the #pragma token.
9036 #define OMP_PARALLEL_CLAUSE_MASK \
9037 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
9038 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
9039 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
9040 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
9041 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
9042 | (1u << PRAGMA_OMP_CLAUSE_COPYIN) \
9043 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
9044 | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
9047 c_parser_omp_parallel (location_t loc, c_parser *parser)
9049 enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
9050 const char *p_name = "#pragma omp parallel";
9051 tree stmt, clauses, par_clause, ws_clause, block;
9052 unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
9054 if (c_parser_next_token_is_keyword (parser, RID_FOR))
9056 c_parser_consume_token (parser);
9057 p_kind = PRAGMA_OMP_PARALLEL_FOR;
9058 p_name = "#pragma omp parallel for";
9059 mask |= OMP_FOR_CLAUSE_MASK;
9060 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
9062 else if (c_parser_next_token_is (parser, CPP_NAME))
9064 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
9065 if (strcmp (p, "sections") == 0)
9067 c_parser_consume_token (parser);
9068 p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
9069 p_name = "#pragma omp parallel sections";
9070 mask |= OMP_SECTIONS_CLAUSE_MASK;
9071 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
9075 clauses = c_parser_omp_all_clauses (parser, mask, p_name);
9079 case PRAGMA_OMP_PARALLEL:
9080 block = c_begin_omp_parallel ();
9081 c_parser_statement (parser);
9082 stmt = c_finish_omp_parallel (loc, clauses, block);
9085 case PRAGMA_OMP_PARALLEL_FOR:
9086 block = c_begin_omp_parallel ();
9087 c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
9088 c_parser_omp_for_loop (loc, parser, ws_clause, &par_clause);
9089 stmt = c_finish_omp_parallel (loc, par_clause, block);
9090 OMP_PARALLEL_COMBINED (stmt) = 1;
9093 case PRAGMA_OMP_PARALLEL_SECTIONS:
9094 block = c_begin_omp_parallel ();
9095 c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
9096 stmt = c_parser_omp_sections_scope (loc, parser);
9098 OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
9099 stmt = c_finish_omp_parallel (loc, par_clause, block);
9100 OMP_PARALLEL_COMBINED (stmt) = 1;
9111 # pragma omp single single-clause[optseq] new-line
9114 LOC is the location of the #pragma.
9117 #define OMP_SINGLE_CLAUSE_MASK \
9118 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
9119 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
9120 | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
9121 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
9124 c_parser_omp_single (location_t loc, c_parser *parser)
9126 tree stmt = make_node (OMP_SINGLE);
9127 SET_EXPR_LOCATION (stmt, loc);
9128 TREE_TYPE (stmt) = void_type_node;
9130 OMP_SINGLE_CLAUSES (stmt)
9131 = c_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
9132 "#pragma omp single");
9133 OMP_SINGLE_BODY (stmt) = c_parser_omp_structured_block (parser);
9135 return add_stmt (stmt);
9139 # pragma omp task task-clause[optseq] new-line
9141 LOC is the location of the #pragma.
9144 #define OMP_TASK_CLAUSE_MASK \
9145 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
9146 | (1u << PRAGMA_OMP_CLAUSE_UNTIED) \
9147 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
9148 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
9149 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
9150 | (1u << PRAGMA_OMP_CLAUSE_SHARED))
9153 c_parser_omp_task (location_t loc, c_parser *parser)
9155 tree clauses, block;
9157 clauses = c_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
9158 "#pragma omp task");
9160 block = c_begin_omp_task ();
9161 c_parser_statement (parser);
9162 return c_finish_omp_task (loc, clauses, block);
9166 # pragma omp taskwait new-line
9170 c_parser_omp_taskwait (c_parser *parser)
9172 location_t loc = c_parser_peek_token (parser)->location;
9173 c_parser_consume_pragma (parser);
9174 c_parser_skip_to_pragma_eol (parser);
9176 c_finish_omp_taskwait (loc);
9179 /* Main entry point to parsing most OpenMP pragmas. */
9182 c_parser_omp_construct (c_parser *parser)
9184 enum pragma_kind p_kind;
9188 loc = c_parser_peek_token (parser)->location;
9189 p_kind = c_parser_peek_token (parser)->pragma_kind;
9190 c_parser_consume_pragma (parser);
9194 case PRAGMA_OMP_ATOMIC:
9195 c_parser_omp_atomic (loc, parser);
9197 case PRAGMA_OMP_CRITICAL:
9198 stmt = c_parser_omp_critical (loc, parser);
9200 case PRAGMA_OMP_FOR:
9201 stmt = c_parser_omp_for (loc, parser);
9203 case PRAGMA_OMP_MASTER:
9204 stmt = c_parser_omp_master (loc, parser);
9206 case PRAGMA_OMP_ORDERED:
9207 stmt = c_parser_omp_ordered (loc, parser);
9209 case PRAGMA_OMP_PARALLEL:
9210 stmt = c_parser_omp_parallel (loc, parser);
9212 case PRAGMA_OMP_SECTIONS:
9213 stmt = c_parser_omp_sections (loc, parser);
9215 case PRAGMA_OMP_SINGLE:
9216 stmt = c_parser_omp_single (loc, parser);
9218 case PRAGMA_OMP_TASK:
9219 stmt = c_parser_omp_task (loc, parser);
9226 gcc_assert (EXPR_LOCATION (stmt) != UNKNOWN_LOCATION);
9231 # pragma omp threadprivate (variable-list) */
9234 c_parser_omp_threadprivate (c_parser *parser)
9239 c_parser_consume_pragma (parser);
9240 loc = c_parser_peek_token (parser)->location;
9241 vars = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_ERROR, NULL);
9243 /* Mark every variable in VARS to be assigned thread local storage. */
9244 for (t = vars; t; t = TREE_CHAIN (t))
9246 tree v = TREE_PURPOSE (t);
9248 /* FIXME diagnostics: Ideally we should keep individual
9249 locations for all the variables in the var list to make the
9250 following errors more precise. Perhaps
9251 c_parser_omp_var_list_parens() should construct a list of
9252 locations to go along with the var list. */
9254 /* If V had already been marked threadprivate, it doesn't matter
9255 whether it had been used prior to this point. */
9256 if (TREE_CODE (v) != VAR_DECL)
9257 error_at (loc, "%qD is not a variable", v);
9258 else if (TREE_USED (v) && !C_DECL_THREADPRIVATE_P (v))
9259 error_at (loc, "%qE declared %<threadprivate%> after first use", v);
9260 else if (! TREE_STATIC (v) && ! DECL_EXTERNAL (v))
9261 error_at (loc, "automatic variable %qE cannot be %<threadprivate%>", v);
9262 else if (TREE_TYPE (v) == error_mark_node)
9264 else if (! COMPLETE_TYPE_P (TREE_TYPE (v)))
9265 error_at (loc, "%<threadprivate%> %qE has incomplete type", v);
9268 if (! DECL_THREAD_LOCAL_P (v))
9270 DECL_TLS_MODEL (v) = decl_default_tls_model (v);
9271 /* If rtl has been already set for this var, call
9272 make_decl_rtl once again, so that encode_section_info
9273 has a chance to look at the new decl flags. */
9274 if (DECL_RTL_SET_P (v))
9277 C_DECL_THREADPRIVATE_P (v) = 1;
9281 c_parser_skip_to_pragma_eol (parser);
9285 /* Parse a single source file. */
9290 /* Use local storage to begin. If the first token is a pragma, parse it.
9291 If it is #pragma GCC pch_preprocess, then this will load a PCH file
9292 which will cause garbage collection. */
9295 memset (&tparser, 0, sizeof tparser);
9296 the_parser = &tparser;
9298 if (c_parser_peek_token (&tparser)->pragma_kind == PRAGMA_GCC_PCH_PREPROCESS)
9299 c_parser_pragma_pch_preprocess (&tparser);
9301 the_parser = ggc_alloc_c_parser ();
9302 *the_parser = tparser;
9304 /* Initialize EH, if we've been told to do so. */
9305 if (flag_exceptions)
9306 using_eh_for_cleanups ();
9308 c_parser_translation_unit (the_parser);
9312 #include "gt-c-parser.h"