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
4 Free Software Foundation, Inc.
6 Parser actions based on the old Bison parser; structure somewhat
7 influenced by and fragments based on the C++ parser.
9 This file is part of GCC.
11 GCC is free software; you can redistribute it and/or modify it under
12 the terms of the GNU General Public License as published by the Free
13 Software Foundation; either version 3, or (at your option) any later
16 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
17 WARRANTY; without even the implied warranty of MERCHANTABILITY or
18 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21 You should have received a copy of the GNU General Public License
22 along with GCC; see the file COPYING3. If not see
23 <http://www.gnu.org/licenses/>. */
27 Make sure all relevant comments, and all relevant code from all
28 actions, brought over from old parser. Verify exact correspondence
31 Add testcases covering every input symbol in every state in old and
34 Include full syntax for GNU C, including erroneous cases accepted
35 with error messages, in syntax productions in comments.
37 Make more diagnostics in the front end generally take an explicit
38 location rather than implicitly using input_location. */
42 #include "coretypes.h"
46 #include "langhooks.h"
63 /* Initialization routine for this file. */
68 /* The only initialization required is of the reserved word
79 mask |= D_ASM | D_EXT;
83 if (!c_dialect_objc ())
84 mask |= D_OBJC | D_CXX_OBJC;
86 ridpointers = GGC_CNEWVEC (tree, (int) RID_MAX);
87 for (i = 0; i < num_c_common_reswords; i++)
89 /* If a keyword is disabled, do not enter it into the table
90 and so create a canonical spelling that isn't a keyword. */
91 if (c_common_reswords[i].disable & mask)
94 && (c_common_reswords[i].disable & D_CXXWARN))
96 id = get_identifier (c_common_reswords[i].word);
97 C_SET_RID_CODE (id, RID_CXX_COMPAT_WARN);
98 C_IS_RESERVED_WORD (id) = 1;
103 id = get_identifier (c_common_reswords[i].word);
104 C_SET_RID_CODE (id, c_common_reswords[i].rid);
105 C_IS_RESERVED_WORD (id) = 1;
106 ridpointers [(int) c_common_reswords[i].rid] = id;
110 /* The C lexer intermediates between the lexer in cpplib and c-lex.c
111 and the C parser. Unlike the C++ lexer, the parser structure
112 stores the lexer information instead of using a separate structure.
113 Identifiers are separated into ordinary identifiers, type names,
114 keywords and some other Objective-C types of identifiers, and some
115 look-ahead is maintained.
117 ??? It might be a good idea to lex the whole file up front (as for
118 C++). It would then be possible to share more of the C and C++
119 lexer code, if desired. */
121 /* The following local token type is used. */
124 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
126 /* More information about the type of a CPP_NAME token. */
127 typedef enum c_id_kind {
128 /* An ordinary identifier. */
130 /* An identifier declared as a typedef name. */
132 /* An identifier declared as an Objective-C class name. */
134 /* Not an identifier. */
138 /* A single C token after string literal concatenation and conversion
139 of preprocessing tokens to tokens. */
140 typedef struct GTY (()) c_token {
141 /* The kind of token. */
142 ENUM_BITFIELD (cpp_ttype) type : 8;
143 /* If this token is a CPP_NAME, this value indicates whether also
144 declared as some kind of type. Otherwise, it is C_ID_NONE. */
145 ENUM_BITFIELD (c_id_kind) id_kind : 8;
146 /* If this token is a keyword, this value indicates which keyword.
147 Otherwise, this value is RID_MAX. */
148 ENUM_BITFIELD (rid) keyword : 8;
149 /* If this token is a CPP_PRAGMA, this indicates the pragma that
150 was seen. Otherwise it is PRAGMA_NONE. */
151 ENUM_BITFIELD (pragma_kind) pragma_kind : 8;
152 /* The value associated with this token, if any. */
154 /* The location at which this token was found. */
158 /* A parser structure recording information about the state and
159 context of parsing. Includes lexer information with up to two
160 tokens of look-ahead; more are not needed for C. */
161 typedef struct GTY(()) c_parser {
162 /* The look-ahead tokens. */
164 /* How many look-ahead tokens are available (0, 1 or 2). */
166 /* True if a syntax error is being recovered from; false otherwise.
167 c_parser_error sets this flag. It should clear this flag when
168 enough tokens have been consumed to recover from the error. */
169 BOOL_BITFIELD error : 1;
170 /* True if we're processing a pragma, and shouldn't automatically
171 consume CPP_PRAGMA_EOL. */
172 BOOL_BITFIELD in_pragma : 1;
173 /* True if we're parsing the outermost block of an if statement. */
174 BOOL_BITFIELD in_if_block : 1;
175 /* True if we want to lex an untranslated string. */
176 BOOL_BITFIELD lex_untranslated_string : 1;
177 /* Objective-C specific parser/lexer information. */
178 BOOL_BITFIELD objc_pq_context : 1;
179 /* The following flag is needed to contextualize Objective-C lexical
180 analysis. In some cases (e.g., 'int NSObject;'), it is
181 undesirable to bind an identifier to an Objective-C class, even
182 if a class with that name exists. */
183 BOOL_BITFIELD objc_need_raw_identifier : 1;
187 /* The actual parser and external interface. ??? Does this need to be
188 garbage-collected? */
190 static GTY (()) c_parser *the_parser;
193 /* Read in and lex a single token, storing it in *TOKEN. */
196 c_lex_one_token (c_parser *parser, c_token *token)
198 timevar_push (TV_LEX);
200 token->type = c_lex_with_flags (&token->value, &token->location, NULL,
201 (parser->lex_untranslated_string
202 ? C_LEX_STRING_NO_TRANSLATE : 0));
203 token->id_kind = C_ID_NONE;
204 token->keyword = RID_MAX;
205 token->pragma_kind = PRAGMA_NONE;
213 bool objc_force_identifier = parser->objc_need_raw_identifier;
214 if (c_dialect_objc ())
215 parser->objc_need_raw_identifier = false;
217 if (C_IS_RESERVED_WORD (token->value))
219 enum rid rid_code = C_RID_CODE (token->value);
221 if (rid_code == RID_CXX_COMPAT_WARN)
223 warning_at (token->location,
225 "identifier %qE conflicts with C++ keyword",
228 else if (c_dialect_objc ())
230 if (!objc_is_reserved_word (token->value)
231 && (!OBJC_IS_PQ_KEYWORD (rid_code)
232 || parser->objc_pq_context))
234 /* Return the canonical spelling for this keyword. */
235 token->value = ridpointers[(int) rid_code];
236 token->type = CPP_KEYWORD;
237 token->keyword = rid_code;
243 token->type = CPP_KEYWORD;
244 token->keyword = rid_code;
249 decl = lookup_name (token->value);
252 if (TREE_CODE (decl) == TYPE_DECL)
254 token->id_kind = C_ID_TYPENAME;
258 else if (c_dialect_objc ())
260 tree objc_interface_decl = objc_is_class_name (token->value);
261 /* Objective-C class names are in the same namespace as
262 variables and typedefs, and hence are shadowed by local
264 if (objc_interface_decl
265 && (global_bindings_p ()
266 || (!objc_force_identifier && !decl)))
268 token->value = objc_interface_decl;
269 token->id_kind = C_ID_CLASSNAME;
273 token->id_kind = C_ID_ID;
277 /* This only happens in Objective-C; it must be a keyword. */
278 token->type = CPP_KEYWORD;
279 token->keyword = C_RID_CODE (token->value);
283 case CPP_CLOSE_PAREN:
285 /* These tokens may affect the interpretation of any identifiers
286 following, if doing Objective-C. */
287 if (c_dialect_objc ())
288 parser->objc_need_raw_identifier = false;
291 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
292 token->pragma_kind = (enum pragma_kind) TREE_INT_CST_LOW (token->value);
298 timevar_pop (TV_LEX);
301 /* Return a pointer to the next token from PARSER, reading it in if
304 static inline c_token *
305 c_parser_peek_token (c_parser *parser)
307 if (parser->tokens_avail == 0)
309 c_lex_one_token (parser, &parser->tokens[0]);
310 parser->tokens_avail = 1;
312 return &parser->tokens[0];
315 /* Return true if the next token from PARSER has the indicated
319 c_parser_next_token_is (c_parser *parser, enum cpp_ttype type)
321 return c_parser_peek_token (parser)->type == type;
324 /* Return true if the next token from PARSER does not have the
328 c_parser_next_token_is_not (c_parser *parser, enum cpp_ttype type)
330 return !c_parser_next_token_is (parser, type);
333 /* Return true if the next token from PARSER is the indicated
337 c_parser_next_token_is_keyword (c_parser *parser, enum rid keyword)
339 return c_parser_peek_token (parser)->keyword == keyword;
342 /* Return true if TOKEN can start a type name,
345 c_token_starts_typename (c_token *token)
350 switch (token->id_kind)
357 gcc_assert (c_dialect_objc ());
363 switch (token->keyword)
395 if (c_dialect_objc ())
403 /* Return true if the next token from PARSER can start a type name,
406 c_parser_next_token_starts_typename (c_parser *parser)
408 c_token *token = c_parser_peek_token (parser);
409 return c_token_starts_typename (token);
412 /* Return true if TOKEN can start declaration specifiers, false
415 c_token_starts_declspecs (c_token *token)
420 switch (token->id_kind)
427 gcc_assert (c_dialect_objc ());
433 switch (token->keyword)
472 if (c_dialect_objc ())
480 /* Return true if the next token from PARSER can start declaration
481 specifiers, false otherwise. */
483 c_parser_next_token_starts_declspecs (c_parser *parser)
485 c_token *token = c_parser_peek_token (parser);
486 return c_token_starts_declspecs (token);
489 /* Return a pointer to the next-but-one token from PARSER, reading it
490 in if necessary. The next token is already read in. */
493 c_parser_peek_2nd_token (c_parser *parser)
495 if (parser->tokens_avail >= 2)
496 return &parser->tokens[1];
497 gcc_assert (parser->tokens_avail == 1);
498 gcc_assert (parser->tokens[0].type != CPP_EOF);
499 gcc_assert (parser->tokens[0].type != CPP_PRAGMA_EOL);
500 c_lex_one_token (parser, &parser->tokens[1]);
501 parser->tokens_avail = 2;
502 return &parser->tokens[1];
505 /* Consume the next token from PARSER. */
508 c_parser_consume_token (c_parser *parser)
510 gcc_assert (parser->tokens_avail >= 1);
511 gcc_assert (parser->tokens[0].type != CPP_EOF);
512 gcc_assert (!parser->in_pragma || parser->tokens[0].type != CPP_PRAGMA_EOL);
513 gcc_assert (parser->error || parser->tokens[0].type != CPP_PRAGMA);
514 if (parser->tokens_avail == 2)
515 parser->tokens[0] = parser->tokens[1];
516 parser->tokens_avail--;
519 /* Expect the current token to be a #pragma. Consume it and remember
520 that we've begun parsing a pragma. */
523 c_parser_consume_pragma (c_parser *parser)
525 gcc_assert (!parser->in_pragma);
526 gcc_assert (parser->tokens_avail >= 1);
527 gcc_assert (parser->tokens[0].type == CPP_PRAGMA);
528 if (parser->tokens_avail == 2)
529 parser->tokens[0] = parser->tokens[1];
530 parser->tokens_avail--;
531 parser->in_pragma = true;
534 /* Update the globals input_location and in_system_header from
537 c_parser_set_source_position_from_token (c_token *token)
539 if (token->type != CPP_EOF)
541 input_location = token->location;
545 /* Issue a diagnostic of the form
546 FILE:LINE: MESSAGE before TOKEN
547 where TOKEN is the next token in the input stream of PARSER.
548 MESSAGE (specified by the caller) is usually of the form "expected
551 Do not issue a diagnostic if still recovering from an error.
553 ??? This is taken from the C++ parser, but building up messages in
554 this way is not i18n-friendly and some other approach should be
558 c_parser_error (c_parser *parser, const char *gmsgid)
560 c_token *token = c_parser_peek_token (parser);
563 parser->error = true;
566 /* This diagnostic makes more sense if it is tagged to the line of
567 the token we just peeked at. */
568 c_parser_set_source_position_from_token (token);
569 c_parse_error (gmsgid,
570 /* Because c_parse_error does not understand
571 CPP_KEYWORD, keywords are treated like
573 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
574 /* ??? The C parser does not save the cpp flags of a
575 token, we need to pass 0 here and we will not get
576 the source spelling of some tokens but rather the
577 canonical spelling. */
578 token->value, /*flags=*/0);
581 /* If the next token is of the indicated TYPE, consume it. Otherwise,
582 issue the error MSGID. If MSGID is NULL then a message has already
583 been produced and no message will be produced this time. Returns
584 true if found, false otherwise. */
587 c_parser_require (c_parser *parser,
591 if (c_parser_next_token_is (parser, type))
593 c_parser_consume_token (parser);
598 c_parser_error (parser, msgid);
603 /* If the next token is the indicated keyword, consume it. Otherwise,
604 issue the error MSGID. Returns true if found, false otherwise. */
607 c_parser_require_keyword (c_parser *parser,
611 if (c_parser_next_token_is_keyword (parser, keyword))
613 c_parser_consume_token (parser);
618 c_parser_error (parser, msgid);
623 /* Like c_parser_require, except that tokens will be skipped until the
624 desired token is found. An error message is still produced if the
625 next token is not as expected. If MSGID is NULL then a message has
626 already been produced and no message will be produced this
630 c_parser_skip_until_found (c_parser *parser,
634 unsigned nesting_depth = 0;
636 if (c_parser_require (parser, type, msgid))
639 /* Skip tokens until the desired token is found. */
642 /* Peek at the next token. */
643 c_token *token = c_parser_peek_token (parser);
644 /* If we've reached the token we want, consume it and stop. */
645 if (token->type == type && !nesting_depth)
647 c_parser_consume_token (parser);
651 /* If we've run out of tokens, stop. */
652 if (token->type == CPP_EOF)
654 if (token->type == CPP_PRAGMA_EOL && parser->in_pragma)
656 if (token->type == CPP_OPEN_BRACE
657 || token->type == CPP_OPEN_PAREN
658 || token->type == CPP_OPEN_SQUARE)
660 else if (token->type == CPP_CLOSE_BRACE
661 || token->type == CPP_CLOSE_PAREN
662 || token->type == CPP_CLOSE_SQUARE)
664 if (nesting_depth-- == 0)
667 /* Consume this token. */
668 c_parser_consume_token (parser);
670 parser->error = false;
673 /* Skip tokens until the end of a parameter is found, but do not
674 consume the comma, semicolon or closing delimiter. */
677 c_parser_skip_to_end_of_parameter (c_parser *parser)
679 unsigned nesting_depth = 0;
683 c_token *token = c_parser_peek_token (parser);
684 if ((token->type == CPP_COMMA || token->type == CPP_SEMICOLON)
687 /* If we've run out of tokens, stop. */
688 if (token->type == CPP_EOF)
690 if (token->type == CPP_PRAGMA_EOL && parser->in_pragma)
692 if (token->type == CPP_OPEN_BRACE
693 || token->type == CPP_OPEN_PAREN
694 || token->type == CPP_OPEN_SQUARE)
696 else if (token->type == CPP_CLOSE_BRACE
697 || token->type == CPP_CLOSE_PAREN
698 || token->type == CPP_CLOSE_SQUARE)
700 if (nesting_depth-- == 0)
703 /* Consume this token. */
704 c_parser_consume_token (parser);
706 parser->error = false;
709 /* Expect to be at the end of the pragma directive and consume an
710 end of line marker. */
713 c_parser_skip_to_pragma_eol (c_parser *parser)
715 gcc_assert (parser->in_pragma);
716 parser->in_pragma = false;
718 if (!c_parser_require (parser, CPP_PRAGMA_EOL, "expected end of line"))
721 c_token *token = c_parser_peek_token (parser);
722 if (token->type == CPP_EOF)
724 if (token->type == CPP_PRAGMA_EOL)
726 c_parser_consume_token (parser);
729 c_parser_consume_token (parser);
732 parser->error = false;
735 /* Skip tokens until we have consumed an entire block, or until we
736 have consumed a non-nested ';'. */
739 c_parser_skip_to_end_of_block_or_statement (c_parser *parser)
741 unsigned nesting_depth = 0;
742 bool save_error = parser->error;
748 /* Peek at the next token. */
749 token = c_parser_peek_token (parser);
757 if (parser->in_pragma)
762 /* If the next token is a ';', we have reached the
763 end of the statement. */
766 /* Consume the ';'. */
767 c_parser_consume_token (parser);
772 case CPP_CLOSE_BRACE:
773 /* If the next token is a non-nested '}', then we have
774 reached the end of the current block. */
775 if (nesting_depth == 0 || --nesting_depth == 0)
777 c_parser_consume_token (parser);
783 /* If it the next token is a '{', then we are entering a new
784 block. Consume the entire block. */
789 /* If we see a pragma, consume the whole thing at once. We
790 have some safeguards against consuming pragmas willy-nilly.
791 Normally, we'd expect to be here with parser->error set,
792 which disables these safeguards. But it's possible to get
793 here for secondary error recovery, after parser->error has
795 c_parser_consume_pragma (parser);
796 c_parser_skip_to_pragma_eol (parser);
797 parser->error = save_error;
804 c_parser_consume_token (parser);
808 parser->error = false;
811 /* CPP's options (initialized by c-opts.c). */
812 extern cpp_options *cpp_opts;
814 /* Save the warning flags which are controlled by __extension__. */
817 disable_extension_diagnostics (void)
820 | (warn_pointer_arith << 1)
821 | (warn_traditional << 2)
823 | (warn_long_long << 4));
824 cpp_opts->pedantic = pedantic = 0;
825 warn_pointer_arith = 0;
826 cpp_opts->warn_traditional = warn_traditional = 0;
828 cpp_opts->warn_long_long = warn_long_long = 0;
832 /* Restore the warning flags which are controlled by __extension__.
833 FLAGS is the return value from disable_extension_diagnostics. */
836 restore_extension_diagnostics (int flags)
838 cpp_opts->pedantic = pedantic = flags & 1;
839 warn_pointer_arith = (flags >> 1) & 1;
840 cpp_opts->warn_traditional = warn_traditional = (flags >> 2) & 1;
841 flag_iso = (flags >> 3) & 1;
842 cpp_opts->warn_long_long = warn_long_long = (flags >> 4) & 1;
845 /* Possibly kinds of declarator to parse. */
846 typedef enum c_dtr_syn {
847 /* A normal declarator with an identifier. */
849 /* An abstract declarator (maybe empty). */
851 /* A parameter declarator: may be either, but after a type name does
852 not redeclare a typedef name as an identifier if it can
853 alternatively be interpreted as a typedef name; see DR#009,
854 applied in C90 TC1, omitted from C99 and reapplied in C99 TC2
855 following DR#249. For example, given a typedef T, "int T" and
856 "int *T" are valid parameter declarations redeclaring T, while
857 "int (T)" and "int * (T)" and "int (T[])" and "int (T (int))" are
858 abstract declarators rather than involving redundant parentheses;
859 the same applies with attributes inside the parentheses before
864 static void c_parser_external_declaration (c_parser *);
865 static void c_parser_asm_definition (c_parser *);
866 static void c_parser_declaration_or_fndef (c_parser *, bool, bool, bool, bool);
867 static void c_parser_declspecs (c_parser *, struct c_declspecs *, bool, bool,
869 static struct c_typespec c_parser_enum_specifier (c_parser *);
870 static struct c_typespec c_parser_struct_or_union_specifier (c_parser *);
871 static tree c_parser_struct_declaration (c_parser *);
872 static struct c_typespec c_parser_typeof_specifier (c_parser *);
873 static struct c_declarator *c_parser_declarator (c_parser *, bool, c_dtr_syn,
875 static struct c_declarator *c_parser_direct_declarator (c_parser *, bool,
877 static struct c_declarator *c_parser_direct_declarator_inner (c_parser *,
879 struct c_declarator *);
880 static struct c_arg_info *c_parser_parms_declarator (c_parser *, bool, tree);
881 static struct c_arg_info *c_parser_parms_list_declarator (c_parser *, tree);
882 static struct c_parm *c_parser_parameter_declaration (c_parser *, tree);
883 static tree c_parser_simple_asm_expr (c_parser *);
884 static tree c_parser_attributes (c_parser *);
885 static struct c_type_name *c_parser_type_name (c_parser *);
886 static struct c_expr c_parser_initializer (c_parser *);
887 static struct c_expr c_parser_braced_init (c_parser *, tree, bool);
888 static void c_parser_initelt (c_parser *);
889 static void c_parser_initval (c_parser *, struct c_expr *);
890 static tree c_parser_compound_statement (c_parser *);
891 static void c_parser_compound_statement_nostart (c_parser *);
892 static void c_parser_label (c_parser *);
893 static void c_parser_statement (c_parser *);
894 static void c_parser_statement_after_labels (c_parser *);
895 static void c_parser_if_statement (c_parser *);
896 static void c_parser_switch_statement (c_parser *);
897 static void c_parser_while_statement (c_parser *);
898 static void c_parser_do_statement (c_parser *);
899 static void c_parser_for_statement (c_parser *);
900 static tree c_parser_asm_statement (c_parser *);
901 static tree c_parser_asm_operands (c_parser *, bool);
902 static tree c_parser_asm_clobbers (c_parser *);
903 static struct c_expr c_parser_expr_no_commas (c_parser *, struct c_expr *);
904 static struct c_expr c_parser_conditional_expression (c_parser *,
906 static struct c_expr c_parser_binary_expression (c_parser *, struct c_expr *);
907 static struct c_expr c_parser_cast_expression (c_parser *, struct c_expr *);
908 static struct c_expr c_parser_unary_expression (c_parser *);
909 static struct c_expr c_parser_sizeof_expression (c_parser *);
910 static struct c_expr c_parser_alignof_expression (c_parser *);
911 static struct c_expr c_parser_postfix_expression (c_parser *);
912 static struct c_expr c_parser_postfix_expression_after_paren_type (c_parser *,
913 struct c_type_name *);
914 static struct c_expr c_parser_postfix_expression_after_primary (c_parser *,
916 static struct c_expr c_parser_expression (c_parser *);
917 static struct c_expr c_parser_expression_conv (c_parser *);
918 static VEC(tree,gc) *c_parser_expr_list (c_parser *, bool, bool,
920 static void c_parser_release_expr_list (VEC(tree,gc) *);
921 static tree c_parser_vec_to_tree_list (VEC(tree,gc) *);
922 static void c_parser_omp_construct (c_parser *);
923 static void c_parser_omp_threadprivate (c_parser *);
924 static void c_parser_omp_barrier (c_parser *);
925 static void c_parser_omp_flush (c_parser *);
926 static void c_parser_omp_taskwait (c_parser *);
928 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
929 static bool c_parser_pragma (c_parser *, enum pragma_context);
931 /* These Objective-C parser functions are only ever called when
932 compiling Objective-C. */
933 static void c_parser_objc_class_definition (c_parser *);
934 static void c_parser_objc_class_instance_variables (c_parser *);
935 static void c_parser_objc_class_declaration (c_parser *);
936 static void c_parser_objc_alias_declaration (c_parser *);
937 static void c_parser_objc_protocol_definition (c_parser *);
938 static enum tree_code c_parser_objc_method_type (c_parser *);
939 static void c_parser_objc_method_definition (c_parser *);
940 static void c_parser_objc_methodprotolist (c_parser *);
941 static void c_parser_objc_methodproto (c_parser *);
942 static tree c_parser_objc_method_decl (c_parser *);
943 static tree c_parser_objc_type_name (c_parser *);
944 static tree c_parser_objc_protocol_refs (c_parser *);
945 static void c_parser_objc_try_catch_statement (c_parser *);
946 static void c_parser_objc_synchronized_statement (c_parser *);
947 static tree c_parser_objc_selector (c_parser *);
948 static tree c_parser_objc_selector_arg (c_parser *);
949 static tree c_parser_objc_receiver (c_parser *);
950 static tree c_parser_objc_message_args (c_parser *);
951 static tree c_parser_objc_keywordexpr (c_parser *);
953 /* Parse a translation unit (C90 6.7, C99 6.9).
956 external-declarations
958 external-declarations:
960 external-declarations external-declaration
969 c_parser_translation_unit (c_parser *parser)
971 if (c_parser_next_token_is (parser, CPP_EOF))
973 pedwarn (c_parser_peek_token (parser)->location, OPT_pedantic,
974 "ISO C forbids an empty translation unit");
978 void *obstack_position = obstack_alloc (&parser_obstack, 0);
979 mark_valid_location_for_stdc_pragma (false);
983 c_parser_external_declaration (parser);
984 obstack_free (&parser_obstack, obstack_position);
986 while (c_parser_next_token_is_not (parser, CPP_EOF));
990 /* Parse an external declaration (C90 6.7, C99 6.9).
992 external-declaration:
998 external-declaration:
1001 __extension__ external-declaration
1005 external-declaration:
1006 objc-class-definition
1007 objc-class-declaration
1008 objc-alias-declaration
1009 objc-protocol-definition
1010 objc-method-definition
1015 c_parser_external_declaration (c_parser *parser)
1018 switch (c_parser_peek_token (parser)->type)
1021 switch (c_parser_peek_token (parser)->keyword)
1024 ext = disable_extension_diagnostics ();
1025 c_parser_consume_token (parser);
1026 c_parser_external_declaration (parser);
1027 restore_extension_diagnostics (ext);
1030 c_parser_asm_definition (parser);
1032 case RID_AT_INTERFACE:
1033 case RID_AT_IMPLEMENTATION:
1034 gcc_assert (c_dialect_objc ());
1035 c_parser_objc_class_definition (parser);
1038 gcc_assert (c_dialect_objc ());
1039 c_parser_objc_class_declaration (parser);
1042 gcc_assert (c_dialect_objc ());
1043 c_parser_objc_alias_declaration (parser);
1045 case RID_AT_PROTOCOL:
1046 gcc_assert (c_dialect_objc ());
1047 c_parser_objc_protocol_definition (parser);
1050 gcc_assert (c_dialect_objc ());
1051 c_parser_consume_token (parser);
1052 objc_finish_implementation ();
1059 pedwarn (c_parser_peek_token (parser)->location, OPT_pedantic,
1060 "ISO C does not allow extra %<;%> outside of a function");
1061 c_parser_consume_token (parser);
1064 mark_valid_location_for_stdc_pragma (true);
1065 c_parser_pragma (parser, pragma_external);
1066 mark_valid_location_for_stdc_pragma (false);
1070 if (c_dialect_objc ())
1072 c_parser_objc_method_definition (parser);
1075 /* Else fall through, and yield a syntax error trying to parse
1076 as a declaration or function definition. */
1079 /* A declaration or a function definition. We can only tell
1080 which after parsing the declaration specifiers, if any, and
1081 the first declarator. */
1082 c_parser_declaration_or_fndef (parser, true, true, false, true);
1088 /* Parse a declaration or function definition (C90 6.5, 6.7.1, C99
1089 6.7, 6.9.1). If FNDEF_OK is true, a function definition is
1090 accepted; otherwise (old-style parameter declarations) only other
1091 declarations are accepted. If NESTED is true, we are inside a
1092 function or parsing old-style parameter declarations; any functions
1093 encountered are nested functions and declaration specifiers are
1094 required; otherwise we are at top level and functions are normal
1095 functions and declaration specifiers may be optional. If EMPTY_OK
1096 is true, empty declarations are OK (subject to all other
1097 constraints); otherwise (old-style parameter declarations) they are
1098 diagnosed. If START_ATTR_OK is true, the declaration specifiers
1099 may start with attributes; otherwise they may not.
1102 declaration-specifiers init-declarator-list[opt] ;
1104 function-definition:
1105 declaration-specifiers[opt] declarator declaration-list[opt]
1110 declaration-list declaration
1112 init-declarator-list:
1114 init-declarator-list , init-declarator
1117 declarator simple-asm-expr[opt] attributes[opt]
1118 declarator simple-asm-expr[opt] attributes[opt] = initializer
1122 nested-function-definition:
1123 declaration-specifiers declarator declaration-list[opt]
1126 The simple-asm-expr and attributes are GNU extensions.
1128 This function does not handle __extension__; that is handled in its
1129 callers. ??? Following the old parser, __extension__ may start
1130 external declarations, declarations in functions and declarations
1131 at the start of "for" loops, but not old-style parameter
1134 C99 requires declaration specifiers in a function definition; the
1135 absence is diagnosed through the diagnosis of implicit int. In GNU
1136 C we also allow but diagnose declarations without declaration
1137 specifiers, but only at top level (elsewhere they conflict with
1143 threadprivate-directive */
1146 c_parser_declaration_or_fndef (c_parser *parser, bool fndef_ok, bool empty_ok,
1147 bool nested, bool start_attr_ok)
1149 struct c_declspecs *specs;
1151 tree all_prefix_attrs;
1152 bool diagnosed_no_specs = false;
1153 location_t here = c_parser_peek_token (parser)->location;
1155 specs = build_null_declspecs ();
1156 c_parser_declspecs (parser, specs, true, true, start_attr_ok);
1159 c_parser_skip_to_end_of_block_or_statement (parser);
1162 if (nested && !specs->declspecs_seen_p)
1164 c_parser_error (parser, "expected declaration specifiers");
1165 c_parser_skip_to_end_of_block_or_statement (parser);
1168 finish_declspecs (specs);
1169 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1175 shadow_tag_warned (specs, 1);
1176 pedwarn (here, 0, "empty declaration");
1178 c_parser_consume_token (parser);
1181 pending_xref_error ();
1182 prefix_attrs = specs->attrs;
1183 all_prefix_attrs = prefix_attrs;
1184 specs->attrs = NULL_TREE;
1187 struct c_declarator *declarator;
1190 /* Declaring either one or more declarators (in which case we
1191 should diagnose if there were no declaration specifiers) or a
1192 function definition (in which case the diagnostic for
1193 implicit int suffices). */
1194 declarator = c_parser_declarator (parser, specs->type_seen_p,
1195 C_DTR_NORMAL, &dummy);
1196 if (declarator == NULL)
1198 c_parser_skip_to_end_of_block_or_statement (parser);
1201 if (c_parser_next_token_is (parser, CPP_EQ)
1202 || c_parser_next_token_is (parser, CPP_COMMA)
1203 || c_parser_next_token_is (parser, CPP_SEMICOLON)
1204 || c_parser_next_token_is_keyword (parser, RID_ASM)
1205 || c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
1207 tree asm_name = NULL_TREE;
1208 tree postfix_attrs = NULL_TREE;
1209 if (!diagnosed_no_specs && !specs->declspecs_seen_p)
1211 diagnosed_no_specs = true;
1212 pedwarn (here, 0, "data definition has no type or storage class");
1214 /* Having seen a data definition, there cannot now be a
1215 function definition. */
1217 if (c_parser_next_token_is_keyword (parser, RID_ASM))
1218 asm_name = c_parser_simple_asm_expr (parser);
1219 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
1220 postfix_attrs = c_parser_attributes (parser);
1221 if (c_parser_next_token_is (parser, CPP_EQ))
1225 c_parser_consume_token (parser);
1226 /* The declaration of the variable is in effect while
1227 its initializer is parsed. */
1228 d = start_decl (declarator, specs, true,
1229 chainon (postfix_attrs, all_prefix_attrs));
1231 d = error_mark_node;
1232 start_init (d, asm_name, global_bindings_p ());
1233 init = c_parser_initializer (parser);
1235 if (d != error_mark_node)
1237 maybe_warn_string_init (TREE_TYPE (d), init);
1238 finish_decl (d, init.value, init.original_type, asm_name);
1243 tree d = start_decl (declarator, specs, false,
1244 chainon (postfix_attrs,
1247 finish_decl (d, NULL_TREE, NULL_TREE, asm_name);
1249 if (c_parser_next_token_is (parser, CPP_COMMA))
1251 c_parser_consume_token (parser);
1252 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
1253 all_prefix_attrs = chainon (c_parser_attributes (parser),
1256 all_prefix_attrs = prefix_attrs;
1259 else if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1261 c_parser_consume_token (parser);
1266 c_parser_error (parser, "expected %<,%> or %<;%>");
1267 c_parser_skip_to_end_of_block_or_statement (parser);
1273 c_parser_error (parser, "expected %<=%>, %<,%>, %<;%>, "
1274 "%<asm%> or %<__attribute__%>");
1275 c_parser_skip_to_end_of_block_or_statement (parser);
1278 /* Function definition (nested or otherwise). */
1281 pedwarn (here, OPT_pedantic, "ISO C forbids nested functions");
1282 c_push_function_context ();
1284 if (!start_function (specs, declarator, all_prefix_attrs))
1286 /* This can appear in many cases looking nothing like a
1287 function definition, so we don't give a more specific
1288 error suggesting there was one. */
1289 c_parser_error (parser, "expected %<=%>, %<,%>, %<;%>, %<asm%> "
1290 "or %<__attribute__%>");
1292 c_pop_function_context ();
1295 /* Parse old-style parameter declarations. ??? Attributes are
1296 not allowed to start declaration specifiers here because of a
1297 syntax conflict between a function declaration with attribute
1298 suffix and a function definition with an attribute prefix on
1299 first old-style parameter declaration. Following the old
1300 parser, they are not accepted on subsequent old-style
1301 parameter declarations either. However, there is no
1302 ambiguity after the first declaration, nor indeed on the
1303 first as long as we don't allow postfix attributes after a
1304 declarator with a nonempty identifier list in a definition;
1305 and postfix attributes have never been accepted here in
1306 function definitions either. */
1307 while (c_parser_next_token_is_not (parser, CPP_EOF)
1308 && c_parser_next_token_is_not (parser, CPP_OPEN_BRACE))
1309 c_parser_declaration_or_fndef (parser, false, false, true, false);
1310 store_parm_decls ();
1311 DECL_STRUCT_FUNCTION (current_function_decl)->function_start_locus
1312 = c_parser_peek_token (parser)->location;
1313 fnbody = c_parser_compound_statement (parser);
1316 tree decl = current_function_decl;
1319 c_pop_function_context ();
1320 add_stmt (build_stmt (DECL_EXPR, decl));
1331 /* Parse an asm-definition (asm() outside a function body). This is a
1339 c_parser_asm_definition (c_parser *parser)
1341 tree asm_str = c_parser_simple_asm_expr (parser);
1343 cgraph_add_asm_node (asm_str);
1344 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
1347 /* Parse some declaration specifiers (possibly none) (C90 6.5, C99
1348 6.7), adding them to SPECS (which may already include some).
1349 Storage class specifiers are accepted iff SCSPEC_OK; type
1350 specifiers are accepted iff TYPESPEC_OK; attributes are accepted at
1351 the start iff START_ATTR_OK.
1353 declaration-specifiers:
1354 storage-class-specifier declaration-specifiers[opt]
1355 type-specifier declaration-specifiers[opt]
1356 type-qualifier declaration-specifiers[opt]
1357 function-specifier declaration-specifiers[opt]
1359 Function specifiers (inline) are from C99, and are currently
1360 handled as storage class specifiers, as is __thread.
1362 C90 6.5.1, C99 6.7.1:
1363 storage-class-specifier:
1374 C90 6.5.2, C99 6.7.2:
1387 [_Imaginary removed in C99 TC2]
1388 struct-or-union-specifier
1392 (_Bool and _Complex are new in C99.)
1394 C90 6.5.3, C99 6.7.3:
1401 (restrict is new in C99.)
1405 declaration-specifiers:
1406 attributes declaration-specifiers[opt]
1408 storage-class-specifier:
1420 (_Fract, _Accum, and _Sat are new from ISO/IEC DTR 18037:
1421 http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1169.pdf)
1426 class-name objc-protocol-refs[opt]
1427 typedef-name objc-protocol-refs
1432 c_parser_declspecs (c_parser *parser, struct c_declspecs *specs,
1433 bool scspec_ok, bool typespec_ok, bool start_attr_ok)
1435 bool attrs_ok = start_attr_ok;
1436 bool seen_type = specs->type_seen_p;
1437 while (c_parser_next_token_is (parser, CPP_NAME)
1438 || c_parser_next_token_is (parser, CPP_KEYWORD)
1439 || (c_dialect_objc () && c_parser_next_token_is (parser, CPP_LESS)))
1441 struct c_typespec t;
1443 if (c_parser_next_token_is (parser, CPP_NAME))
1445 tree value = c_parser_peek_token (parser)->value;
1446 c_id_kind kind = c_parser_peek_token (parser)->id_kind;
1447 /* This finishes the specifiers unless a type name is OK, it
1448 is declared as a type name and a type name hasn't yet
1450 if (!typespec_ok || seen_type
1451 || (kind != C_ID_TYPENAME && kind != C_ID_CLASSNAME))
1453 c_parser_consume_token (parser);
1456 if (kind == C_ID_TYPENAME
1457 && (!c_dialect_objc ()
1458 || c_parser_next_token_is_not (parser, CPP_LESS)))
1460 t.kind = ctsk_typedef;
1461 /* For a typedef name, record the meaning, not the name.
1462 In case of 'foo foo, bar;'. */
1463 t.spec = lookup_name (value);
1465 t.expr_const_operands = true;
1469 tree proto = NULL_TREE;
1470 gcc_assert (c_dialect_objc ());
1472 if (c_parser_next_token_is (parser, CPP_LESS))
1473 proto = c_parser_objc_protocol_refs (parser);
1474 t.spec = objc_get_protocol_qualified_type (value, proto);
1476 t.expr_const_operands = true;
1478 declspecs_add_type (specs, t);
1481 if (c_parser_next_token_is (parser, CPP_LESS))
1483 /* Make "<SomeProtocol>" equivalent to "id <SomeProtocol>" -
1484 nisse@lysator.liu.se. */
1486 gcc_assert (c_dialect_objc ());
1487 if (!typespec_ok || seen_type)
1489 proto = c_parser_objc_protocol_refs (parser);
1491 t.spec = objc_get_protocol_qualified_type (NULL_TREE, proto);
1493 t.expr_const_operands = true;
1494 declspecs_add_type (specs, t);
1497 gcc_assert (c_parser_next_token_is (parser, CPP_KEYWORD));
1498 switch (c_parser_peek_token (parser)->keyword)
1510 /* TODO: Distinguish between function specifiers (inline)
1511 and storage class specifiers, either here or in
1512 declspecs_add_scspec. */
1513 declspecs_add_scspec (specs, c_parser_peek_token (parser)->value);
1514 c_parser_consume_token (parser);
1537 if (c_dialect_objc ())
1538 parser->objc_need_raw_identifier = true;
1539 t.kind = ctsk_resword;
1540 t.spec = c_parser_peek_token (parser)->value;
1542 t.expr_const_operands = true;
1543 declspecs_add_type (specs, t);
1544 c_parser_consume_token (parser);
1551 t = c_parser_enum_specifier (parser);
1552 declspecs_add_type (specs, t);
1560 t = c_parser_struct_or_union_specifier (parser);
1561 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, t.spec);
1562 declspecs_add_type (specs, t);
1565 /* ??? The old parser rejected typeof after other type
1566 specifiers, but is a syntax error the best way of
1568 if (!typespec_ok || seen_type)
1572 t = c_parser_typeof_specifier (parser);
1573 declspecs_add_type (specs, t);
1579 declspecs_add_qual (specs, c_parser_peek_token (parser)->value);
1580 c_parser_consume_token (parser);
1585 attrs = c_parser_attributes (parser);
1586 declspecs_add_attrs (specs, attrs);
1595 /* Parse an enum specifier (C90 6.5.2.2, C99 6.7.2.2).
1598 enum attributes[opt] identifier[opt] { enumerator-list } attributes[opt]
1599 enum attributes[opt] identifier[opt] { enumerator-list , } attributes[opt]
1600 enum attributes[opt] identifier
1602 The form with trailing comma is new in C99. The forms with
1603 attributes are GNU extensions. In GNU C, we accept any expression
1604 without commas in the syntax (assignment expressions, not just
1605 conditional expressions); assignment expressions will be diagnosed
1610 enumerator-list , enumerator
1613 enumeration-constant
1614 enumeration-constant = constant-expression
1617 static struct c_typespec
1618 c_parser_enum_specifier (c_parser *parser)
1620 struct c_typespec ret;
1622 tree ident = NULL_TREE;
1623 location_t ident_loc = UNKNOWN_LOCATION; /* Quiet warning. */
1624 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ENUM));
1625 c_parser_consume_token (parser);
1626 attrs = c_parser_attributes (parser);
1627 /* Set the location in case we create a decl now. */
1628 c_parser_set_source_position_from_token (c_parser_peek_token (parser));
1629 if (c_parser_next_token_is (parser, CPP_NAME))
1631 ident = c_parser_peek_token (parser)->value;
1632 ident_loc = c_parser_peek_token (parser)->location;
1633 c_parser_consume_token (parser);
1635 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
1637 /* Parse an enum definition. */
1638 struct c_enum_contents the_enum;
1639 tree type = start_enum (&the_enum, ident);
1641 /* We chain the enumerators in reverse order, then put them in
1642 forward order at the end. */
1643 tree values = NULL_TREE;
1644 c_parser_consume_token (parser);
1652 location_t comma_loc = UNKNOWN_LOCATION; /* Quiet warning. */
1653 location_t value_loc;
1654 if (c_parser_next_token_is_not (parser, CPP_NAME))
1656 c_parser_error (parser, "expected identifier");
1657 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
1658 values = error_mark_node;
1661 token = c_parser_peek_token (parser);
1662 enum_id = token->value;
1663 /* Set the location in case we create a decl now. */
1664 c_parser_set_source_position_from_token (token);
1665 value_loc = token->location;
1666 c_parser_consume_token (parser);
1667 if (c_parser_next_token_is (parser, CPP_EQ))
1669 c_parser_consume_token (parser);
1670 value_loc = c_parser_peek_token (parser)->location;
1671 enum_value = c_parser_expr_no_commas (parser, NULL).value;
1674 enum_value = NULL_TREE;
1675 enum_decl = build_enumerator (&the_enum, enum_id, enum_value,
1677 TREE_CHAIN (enum_decl) = values;
1680 if (c_parser_next_token_is (parser, CPP_COMMA))
1682 comma_loc = c_parser_peek_token (parser)->location;
1684 c_parser_consume_token (parser);
1686 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
1688 if (seen_comma && !flag_isoc99)
1689 pedwarn (comma_loc, OPT_pedantic, "comma at end of enumerator list");
1690 c_parser_consume_token (parser);
1695 c_parser_error (parser, "expected %<,%> or %<}%>");
1696 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
1697 values = error_mark_node;
1701 postfix_attrs = c_parser_attributes (parser);
1702 ret.spec = finish_enum (type, nreverse (values),
1703 chainon (attrs, postfix_attrs));
1704 ret.kind = ctsk_tagdef;
1705 ret.expr = NULL_TREE;
1706 ret.expr_const_operands = true;
1711 c_parser_error (parser, "expected %<{%>");
1712 ret.spec = error_mark_node;
1713 ret.kind = ctsk_tagref;
1714 ret.expr = NULL_TREE;
1715 ret.expr_const_operands = true;
1718 ret = parser_xref_tag (ENUMERAL_TYPE, ident);
1719 /* In ISO C, enumerated types can be referred to only if already
1721 if (pedantic && !COMPLETE_TYPE_P (ret.spec))
1724 pedwarn (ident_loc, OPT_pedantic,
1725 "ISO C forbids forward references to %<enum%> types");
1730 /* Parse a struct or union specifier (C90 6.5.2.1, C99 6.7.2.1).
1732 struct-or-union-specifier:
1733 struct-or-union attributes[opt] identifier[opt]
1734 { struct-contents } attributes[opt]
1735 struct-or-union attributes[opt] identifier
1738 struct-declaration-list
1740 struct-declaration-list:
1741 struct-declaration ;
1742 struct-declaration-list struct-declaration ;
1749 struct-declaration-list struct-declaration
1751 struct-declaration-list:
1752 struct-declaration-list ;
1755 (Note that in the syntax here, unlike that in ISO C, the semicolons
1756 are included here rather than in struct-declaration, in order to
1757 describe the syntax with extra semicolons and missing semicolon at
1762 struct-declaration-list:
1763 @defs ( class-name )
1765 (Note this does not include a trailing semicolon, but can be
1766 followed by further declarations, and gets a pedwarn-if-pedantic
1767 when followed by a semicolon.) */
1769 static struct c_typespec
1770 c_parser_struct_or_union_specifier (c_parser *parser)
1772 struct c_typespec ret;
1774 tree ident = NULL_TREE;
1775 enum tree_code code;
1776 switch (c_parser_peek_token (parser)->keyword)
1787 c_parser_consume_token (parser);
1788 attrs = c_parser_attributes (parser);
1789 /* Set the location in case we create a decl now. */
1790 c_parser_set_source_position_from_token (c_parser_peek_token (parser));
1791 if (c_parser_next_token_is (parser, CPP_NAME))
1793 ident = c_parser_peek_token (parser)->value;
1794 c_parser_consume_token (parser);
1796 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
1798 /* Parse a struct or union definition. Start the scope of the
1799 tag before parsing components. */
1800 tree type = start_struct (code, ident);
1802 /* We chain the components in reverse order, then put them in
1803 forward order at the end. Each struct-declaration may
1804 declare multiple components (comma-separated), so we must use
1805 chainon to join them, although when parsing each
1806 struct-declaration we can use TREE_CHAIN directly.
1808 The theory behind all this is that there will be more
1809 semicolon separated fields than comma separated fields, and
1810 so we'll be minimizing the number of node traversals required
1812 tree contents = NULL_TREE;
1813 c_parser_consume_token (parser);
1814 /* Handle the Objective-C @defs construct,
1815 e.g. foo(sizeof(struct{ @defs(ClassName) }));. */
1816 if (c_parser_next_token_is_keyword (parser, RID_AT_DEFS))
1819 gcc_assert (c_dialect_objc ());
1820 c_parser_consume_token (parser);
1821 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
1823 if (c_parser_next_token_is (parser, CPP_NAME)
1824 && c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME)
1826 name = c_parser_peek_token (parser)->value;
1827 c_parser_consume_token (parser);
1831 c_parser_error (parser, "expected class name");
1832 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
1835 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
1837 contents = nreverse (objc_get_class_ivars (name));
1840 /* Parse the struct-declarations and semicolons. Problems with
1841 semicolons are diagnosed here; empty structures are diagnosed
1846 /* Parse any stray semicolon. */
1847 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1849 pedwarn (c_parser_peek_token (parser)->location, OPT_pedantic,
1850 "extra semicolon in struct or union specified");
1851 c_parser_consume_token (parser);
1854 /* Stop if at the end of the struct or union contents. */
1855 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
1857 c_parser_consume_token (parser);
1860 /* Accept #pragmas at struct scope. */
1861 if (c_parser_next_token_is (parser, CPP_PRAGMA))
1863 c_parser_pragma (parser, pragma_external);
1866 /* Parse some comma-separated declarations, but not the
1867 trailing semicolon if any. */
1868 decls = c_parser_struct_declaration (parser);
1869 contents = chainon (decls, contents);
1870 /* If no semicolon follows, either we have a parse error or
1871 are at the end of the struct or union and should
1873 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1874 c_parser_consume_token (parser);
1877 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
1878 pedwarn (c_parser_peek_token (parser)->location, 0,
1879 "no semicolon at end of struct or union");
1882 c_parser_error (parser, "expected %<;%>");
1883 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
1888 postfix_attrs = c_parser_attributes (parser);
1889 ret.spec = finish_struct (type, nreverse (contents),
1890 chainon (attrs, postfix_attrs));
1891 ret.kind = ctsk_tagdef;
1892 ret.expr = NULL_TREE;
1893 ret.expr_const_operands = true;
1898 c_parser_error (parser, "expected %<{%>");
1899 ret.spec = error_mark_node;
1900 ret.kind = ctsk_tagref;
1901 ret.expr = NULL_TREE;
1902 ret.expr_const_operands = true;
1905 ret = parser_xref_tag (code, ident);
1909 /* Parse a struct-declaration (C90 6.5.2.1, C99 6.7.2.1), *without*
1910 the trailing semicolon.
1913 specifier-qualifier-list struct-declarator-list
1915 specifier-qualifier-list:
1916 type-specifier specifier-qualifier-list[opt]
1917 type-qualifier specifier-qualifier-list[opt]
1918 attributes specifier-qualifier-list[opt]
1920 struct-declarator-list:
1922 struct-declarator-list , attributes[opt] struct-declarator
1925 declarator attributes[opt]
1926 declarator[opt] : constant-expression attributes[opt]
1931 __extension__ struct-declaration
1932 specifier-qualifier-list
1934 Unlike the ISO C syntax, semicolons are handled elsewhere. The use
1935 of attributes where shown is a GNU extension. In GNU C, we accept
1936 any expression without commas in the syntax (assignment
1937 expressions, not just conditional expressions); assignment
1938 expressions will be diagnosed as non-constant. */
1941 c_parser_struct_declaration (c_parser *parser)
1943 struct c_declspecs *specs;
1945 tree all_prefix_attrs;
1947 location_t decl_loc;
1948 if (c_parser_next_token_is_keyword (parser, RID_EXTENSION))
1952 ext = disable_extension_diagnostics ();
1953 c_parser_consume_token (parser);
1954 decl = c_parser_struct_declaration (parser);
1955 restore_extension_diagnostics (ext);
1958 specs = build_null_declspecs ();
1959 decl_loc = c_parser_peek_token (parser)->location;
1960 c_parser_declspecs (parser, specs, false, true, true);
1963 if (!specs->declspecs_seen_p)
1965 c_parser_error (parser, "expected specifier-qualifier-list");
1968 finish_declspecs (specs);
1969 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1972 if (!specs->type_seen_p)
1974 pedwarn (decl_loc, OPT_pedantic,
1975 "ISO C forbids member declarations with no members");
1976 shadow_tag_warned (specs, pedantic);
1981 /* Support for unnamed structs or unions as members of
1982 structs or unions (which is [a] useful and [b] supports
1986 ret = grokfield (c_parser_peek_token (parser)->location,
1987 build_id_declarator (NULL_TREE), specs,
1990 decl_attributes (&ret, attrs, 0);
1994 pending_xref_error ();
1995 prefix_attrs = specs->attrs;
1996 all_prefix_attrs = prefix_attrs;
1997 specs->attrs = NULL_TREE;
2001 /* Declaring one or more declarators or un-named bit-fields. */
2002 struct c_declarator *declarator;
2004 if (c_parser_next_token_is (parser, CPP_COLON))
2005 declarator = build_id_declarator (NULL_TREE);
2007 declarator = c_parser_declarator (parser, specs->type_seen_p,
2008 C_DTR_NORMAL, &dummy);
2009 if (declarator == NULL)
2011 c_parser_skip_to_end_of_block_or_statement (parser);
2014 if (c_parser_next_token_is (parser, CPP_COLON)
2015 || c_parser_next_token_is (parser, CPP_COMMA)
2016 || c_parser_next_token_is (parser, CPP_SEMICOLON)
2017 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE)
2018 || c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2020 tree postfix_attrs = NULL_TREE;
2021 tree width = NULL_TREE;
2023 if (c_parser_next_token_is (parser, CPP_COLON))
2025 c_parser_consume_token (parser);
2026 width = c_parser_expr_no_commas (parser, NULL).value;
2028 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2029 postfix_attrs = c_parser_attributes (parser);
2030 d = grokfield (c_parser_peek_token (parser)->location,
2031 declarator, specs, width, &all_prefix_attrs);
2032 decl_attributes (&d, chainon (postfix_attrs,
2033 all_prefix_attrs), 0);
2034 TREE_CHAIN (d) = decls;
2036 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2037 all_prefix_attrs = chainon (c_parser_attributes (parser),
2040 all_prefix_attrs = prefix_attrs;
2041 if (c_parser_next_token_is (parser, CPP_COMMA))
2042 c_parser_consume_token (parser);
2043 else if (c_parser_next_token_is (parser, CPP_SEMICOLON)
2044 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2046 /* Semicolon consumed in caller. */
2051 c_parser_error (parser, "expected %<,%>, %<;%> or %<}%>");
2057 c_parser_error (parser,
2058 "expected %<:%>, %<,%>, %<;%>, %<}%> or "
2059 "%<__attribute__%>");
2066 /* Parse a typeof specifier (a GNU extension).
2069 typeof ( expression )
2070 typeof ( type-name )
2073 static struct c_typespec
2074 c_parser_typeof_specifier (c_parser *parser)
2076 struct c_typespec ret;
2077 ret.kind = ctsk_typeof;
2078 ret.spec = error_mark_node;
2079 ret.expr = NULL_TREE;
2080 ret.expr_const_operands = true;
2081 gcc_assert (c_parser_next_token_is_keyword (parser, RID_TYPEOF));
2082 c_parser_consume_token (parser);
2085 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2091 if (c_parser_next_token_starts_typename (parser))
2093 struct c_type_name *type = c_parser_type_name (parser);
2098 ret.spec = groktypename (type, &ret.expr, &ret.expr_const_operands);
2099 pop_maybe_used (variably_modified_type_p (ret.spec, NULL_TREE));
2105 location_t here = c_parser_peek_token (parser)->location;
2106 struct c_expr expr = c_parser_expression (parser);
2109 if (TREE_CODE (expr.value) == COMPONENT_REF
2110 && DECL_C_BIT_FIELD (TREE_OPERAND (expr.value, 1)))
2111 error_at (here, "%<typeof%> applied to a bit-field");
2112 ret.spec = TREE_TYPE (expr.value);
2113 was_vm = variably_modified_type_p (ret.spec, NULL_TREE);
2114 /* This is returned with the type so that when the type is
2115 evaluated, this can be evaluated. */
2117 ret.expr = c_fully_fold (expr.value, false, &ret.expr_const_operands);
2118 pop_maybe_used (was_vm);
2120 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
2124 /* Parse a declarator, possibly an abstract declarator (C90 6.5.4,
2125 6.5.5, C99 6.7.5, 6.7.6). If TYPE_SEEN_P then a typedef name may
2126 be redeclared; otherwise it may not. KIND indicates which kind of
2127 declarator is wanted. Returns a valid declarator except in the
2128 case of a syntax error in which case NULL is returned. *SEEN_ID is
2129 set to true if an identifier being declared is seen; this is used
2130 to diagnose bad forms of abstract array declarators and to
2131 determine whether an identifier list is syntactically permitted.
2134 pointer[opt] direct-declarator
2138 ( attributes[opt] declarator )
2139 direct-declarator array-declarator
2140 direct-declarator ( parameter-type-list )
2141 direct-declarator ( identifier-list[opt] )
2144 * type-qualifier-list[opt]
2145 * type-qualifier-list[opt] pointer
2147 type-qualifier-list:
2150 type-qualifier-list type-qualifier
2151 type-qualifier-list attributes
2153 parameter-type-list:
2155 parameter-list , ...
2158 parameter-declaration
2159 parameter-list , parameter-declaration
2161 parameter-declaration:
2162 declaration-specifiers declarator attributes[opt]
2163 declaration-specifiers abstract-declarator[opt] attributes[opt]
2167 identifier-list , identifier
2169 abstract-declarator:
2171 pointer[opt] direct-abstract-declarator
2173 direct-abstract-declarator:
2174 ( attributes[opt] abstract-declarator )
2175 direct-abstract-declarator[opt] array-declarator
2176 direct-abstract-declarator[opt] ( parameter-type-list[opt] )
2181 direct-declarator ( parameter-forward-declarations
2182 parameter-type-list[opt] )
2184 direct-abstract-declarator:
2185 direct-abstract-declarator[opt] ( parameter-forward-declarations
2186 parameter-type-list[opt] )
2188 parameter-forward-declarations:
2190 parameter-forward-declarations parameter-list ;
2192 The uses of attributes shown above are GNU extensions.
2194 Some forms of array declarator are not included in C99 in the
2195 syntax for abstract declarators; these are disallowed elsewhere.
2196 This may be a defect (DR#289).
2198 This function also accepts an omitted abstract declarator as being
2199 an abstract declarator, although not part of the formal syntax. */
2201 static struct c_declarator *
2202 c_parser_declarator (c_parser *parser, bool type_seen_p, c_dtr_syn kind,
2205 /* Parse any initial pointer part. */
2206 if (c_parser_next_token_is (parser, CPP_MULT))
2208 struct c_declspecs *quals_attrs = build_null_declspecs ();
2209 struct c_declarator *inner;
2210 c_parser_consume_token (parser);
2211 c_parser_declspecs (parser, quals_attrs, false, false, true);
2212 inner = c_parser_declarator (parser, type_seen_p, kind, seen_id);
2216 return make_pointer_declarator (quals_attrs, inner);
2218 /* Now we have a direct declarator, direct abstract declarator or
2219 nothing (which counts as a direct abstract declarator here). */
2220 return c_parser_direct_declarator (parser, type_seen_p, kind, seen_id);
2223 /* Parse a direct declarator or direct abstract declarator; arguments
2224 as c_parser_declarator. */
2226 static struct c_declarator *
2227 c_parser_direct_declarator (c_parser *parser, bool type_seen_p, c_dtr_syn kind,
2230 /* The direct declarator must start with an identifier (possibly
2231 omitted) or a parenthesized declarator (possibly abstract). In
2232 an ordinary declarator, initial parentheses must start a
2233 parenthesized declarator. In an abstract declarator or parameter
2234 declarator, they could start a parenthesized declarator or a
2235 parameter list. To tell which, the open parenthesis and any
2236 following attributes must be read. If a declaration specifier
2237 follows, then it is a parameter list; if the specifier is a
2238 typedef name, there might be an ambiguity about redeclaring it,
2239 which is resolved in the direction of treating it as a typedef
2240 name. If a close parenthesis follows, it is also an empty
2241 parameter list, as the syntax does not permit empty abstract
2242 declarators. Otherwise, it is a parenthesized declarator (in
2243 which case the analysis may be repeated inside it, recursively).
2245 ??? There is an ambiguity in a parameter declaration "int
2246 (__attribute__((foo)) x)", where x is not a typedef name: it
2247 could be an abstract declarator for a function, or declare x with
2248 parentheses. The proper resolution of this ambiguity needs
2249 documenting. At present we follow an accident of the old
2250 parser's implementation, whereby the first parameter must have
2251 some declaration specifiers other than just attributes. Thus as
2252 a parameter declaration it is treated as a parenthesized
2253 parameter named x, and as an abstract declarator it is
2256 ??? Also following the old parser, attributes inside an empty
2257 parameter list are ignored, making it a list not yielding a
2258 prototype, rather than giving an error or making it have one
2259 parameter with implicit type int.
2261 ??? Also following the old parser, typedef names may be
2262 redeclared in declarators, but not Objective-C class names. */
2264 if (kind != C_DTR_ABSTRACT
2265 && c_parser_next_token_is (parser, CPP_NAME)
2267 && c_parser_peek_token (parser)->id_kind == C_ID_TYPENAME)
2268 || c_parser_peek_token (parser)->id_kind == C_ID_ID))
2270 struct c_declarator *inner
2271 = build_id_declarator (c_parser_peek_token (parser)->value);
2273 inner->id_loc = c_parser_peek_token (parser)->location;
2274 c_parser_consume_token (parser);
2275 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
2278 if (kind != C_DTR_NORMAL
2279 && c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
2281 struct c_declarator *inner = build_id_declarator (NULL_TREE);
2282 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
2285 /* Either we are at the end of an abstract declarator, or we have
2288 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
2291 struct c_declarator *inner;
2292 c_parser_consume_token (parser);
2293 attrs = c_parser_attributes (parser);
2294 if (kind != C_DTR_NORMAL
2295 && (c_parser_next_token_starts_declspecs (parser)
2296 || c_parser_next_token_is (parser, CPP_CLOSE_PAREN)))
2298 struct c_arg_info *args
2299 = c_parser_parms_declarator (parser, kind == C_DTR_NORMAL,
2306 = build_function_declarator (args,
2307 build_id_declarator (NULL_TREE));
2308 return c_parser_direct_declarator_inner (parser, *seen_id,
2312 /* A parenthesized declarator. */
2313 inner = c_parser_declarator (parser, type_seen_p, kind, seen_id);
2314 if (inner != NULL && attrs != NULL)
2315 inner = build_attrs_declarator (attrs, inner);
2316 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2318 c_parser_consume_token (parser);
2322 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
2326 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2333 if (kind == C_DTR_NORMAL)
2335 c_parser_error (parser, "expected identifier or %<(%>");
2339 return build_id_declarator (NULL_TREE);
2343 /* Parse part of a direct declarator or direct abstract declarator,
2344 given that some (in INNER) has already been parsed; ID_PRESENT is
2345 true if an identifier is present, false for an abstract
2348 static struct c_declarator *
2349 c_parser_direct_declarator_inner (c_parser *parser, bool id_present,
2350 struct c_declarator *inner)
2352 /* Parse a sequence of array declarators and parameter lists. */
2353 if (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
2355 struct c_declarator *declarator;
2356 struct c_declspecs *quals_attrs = build_null_declspecs ();
2360 c_parser_consume_token (parser);
2361 c_parser_declspecs (parser, quals_attrs, false, false, true);
2362 static_seen = c_parser_next_token_is_keyword (parser, RID_STATIC);
2364 c_parser_consume_token (parser);
2365 if (static_seen && !quals_attrs->declspecs_seen_p)
2366 c_parser_declspecs (parser, quals_attrs, false, false, true);
2367 if (!quals_attrs->declspecs_seen_p)
2369 /* If "static" is present, there must be an array dimension.
2370 Otherwise, there may be a dimension, "*", or no
2375 dimen = c_parser_expr_no_commas (parser, NULL).value;
2379 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
2384 else if (c_parser_next_token_is (parser, CPP_MULT))
2386 if (c_parser_peek_2nd_token (parser)->type == CPP_CLOSE_SQUARE)
2390 c_parser_consume_token (parser);
2395 dimen = c_parser_expr_no_commas (parser, NULL).value;
2401 dimen = c_parser_expr_no_commas (parser, NULL).value;
2404 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
2405 c_parser_consume_token (parser);
2408 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
2412 declarator = build_array_declarator (dimen, quals_attrs, static_seen,
2414 if (declarator == NULL)
2416 inner = set_array_declarator_inner (declarator, inner);
2417 return c_parser_direct_declarator_inner (parser, id_present, inner);
2419 else if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
2422 struct c_arg_info *args;
2423 c_parser_consume_token (parser);
2424 attrs = c_parser_attributes (parser);
2425 args = c_parser_parms_declarator (parser, id_present, attrs);
2430 inner = build_function_declarator (args, inner);
2431 return c_parser_direct_declarator_inner (parser, id_present, inner);
2437 /* Parse a parameter list or identifier list, including the closing
2438 parenthesis but not the opening one. ATTRS are the attributes at
2439 the start of the list. ID_LIST_OK is true if an identifier list is
2440 acceptable; such a list must not have attributes at the start. */
2442 static struct c_arg_info *
2443 c_parser_parms_declarator (c_parser *parser, bool id_list_ok, tree attrs)
2446 declare_parm_level ();
2447 /* If the list starts with an identifier, it is an identifier list.
2448 Otherwise, it is either a prototype list or an empty list. */
2451 && c_parser_next_token_is (parser, CPP_NAME)
2452 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
2454 tree list = NULL_TREE, *nextp = &list;
2455 while (c_parser_next_token_is (parser, CPP_NAME)
2456 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
2458 *nextp = build_tree_list (NULL_TREE,
2459 c_parser_peek_token (parser)->value);
2460 nextp = & TREE_CHAIN (*nextp);
2461 c_parser_consume_token (parser);
2462 if (c_parser_next_token_is_not (parser, CPP_COMMA))
2464 c_parser_consume_token (parser);
2465 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2467 c_parser_error (parser, "expected identifier");
2471 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2473 struct c_arg_info *ret = XOBNEW (&parser_obstack, struct c_arg_info);
2478 ret->pending_sizes = 0;
2479 ret->had_vla_unspec = 0;
2480 c_parser_consume_token (parser);
2486 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2494 struct c_arg_info *ret = c_parser_parms_list_declarator (parser, attrs);
2500 /* Parse a parameter list (possibly empty), including the closing
2501 parenthesis but not the opening one. ATTRS are the attributes at
2502 the start of the list. */
2504 static struct c_arg_info *
2505 c_parser_parms_list_declarator (c_parser *parser, tree attrs)
2507 bool good_parm = false;
2508 /* ??? Following the old parser, forward parameter declarations may
2509 use abstract declarators, and if no real parameter declarations
2510 follow the forward declarations then this is not diagnosed. Also
2511 note as above that attributes are ignored as the only contents of
2512 the parentheses, or as the only contents after forward
2514 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2516 struct c_arg_info *ret = XOBNEW (&parser_obstack, struct c_arg_info);
2521 ret->pending_sizes = 0;
2522 ret->had_vla_unspec = 0;
2523 c_parser_consume_token (parser);
2526 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
2528 struct c_arg_info *ret = XOBNEW (&parser_obstack, struct c_arg_info);
2532 ret->pending_sizes = 0;
2533 ret->had_vla_unspec = 0;
2534 /* Suppress -Wold-style-definition for this case. */
2535 ret->types = error_mark_node;
2536 error_at (c_parser_peek_token (parser)->location,
2537 "ISO C requires a named argument before %<...%>");
2538 c_parser_consume_token (parser);
2539 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2541 c_parser_consume_token (parser);
2546 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2551 /* Nonempty list of parameters, either terminated with semicolon
2552 (forward declarations; recurse) or with close parenthesis (normal
2553 function) or with ", ... )" (variadic function). */
2556 /* Parse a parameter. */
2557 struct c_parm *parm = c_parser_parameter_declaration (parser, attrs);
2562 push_parm_decl (parm);
2564 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
2567 c_parser_consume_token (parser);
2568 mark_forward_parm_decls ();
2569 new_attrs = c_parser_attributes (parser);
2570 return c_parser_parms_list_declarator (parser, new_attrs);
2572 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2574 c_parser_consume_token (parser);
2576 return get_parm_info (false);
2579 struct c_arg_info *ret
2580 = XOBNEW (&parser_obstack, struct c_arg_info);
2585 ret->pending_sizes = 0;
2586 ret->had_vla_unspec = 0;
2590 if (!c_parser_require (parser, CPP_COMMA,
2591 "expected %<;%>, %<,%> or %<)%>"))
2593 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
2594 get_pending_sizes ();
2597 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
2599 c_parser_consume_token (parser);
2600 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2602 c_parser_consume_token (parser);
2604 return get_parm_info (true);
2607 struct c_arg_info *ret
2608 = XOBNEW (&parser_obstack, struct c_arg_info);
2613 ret->pending_sizes = 0;
2614 ret->had_vla_unspec = 0;
2620 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2622 get_pending_sizes ();
2629 /* Parse a parameter declaration. ATTRS are the attributes at the
2630 start of the declaration if it is the first parameter. */
2632 static struct c_parm *
2633 c_parser_parameter_declaration (c_parser *parser, tree attrs)
2635 struct c_declspecs *specs;
2636 struct c_declarator *declarator;
2638 tree postfix_attrs = NULL_TREE;
2640 if (!c_parser_next_token_starts_declspecs (parser))
2642 /* ??? In some Objective-C cases '...' isn't applicable so there
2643 should be a different message. */
2644 c_parser_error (parser,
2645 "expected declaration specifiers or %<...%>");
2646 c_parser_skip_to_end_of_parameter (parser);
2649 specs = build_null_declspecs ();
2652 declspecs_add_attrs (specs, attrs);
2655 c_parser_declspecs (parser, specs, true, true, true);
2656 finish_declspecs (specs);
2657 pending_xref_error ();
2658 prefix_attrs = specs->attrs;
2659 specs->attrs = NULL_TREE;
2660 declarator = c_parser_declarator (parser, specs->type_seen_p,
2661 C_DTR_PARM, &dummy);
2662 if (declarator == NULL)
2664 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
2667 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2668 postfix_attrs = c_parser_attributes (parser);
2669 return build_c_parm (specs, chainon (postfix_attrs, prefix_attrs),
2673 /* Parse a string literal in an asm expression. It should not be
2674 translated, and wide string literals are an error although
2675 permitted by the syntax. This is a GNU extension.
2680 ??? At present, following the old parser, the caller needs to have
2681 set lex_untranslated_string to 1. It would be better to follow the
2682 C++ parser rather than using this kludge. */
2685 c_parser_asm_string_literal (c_parser *parser)
2688 if (c_parser_next_token_is (parser, CPP_STRING))
2690 str = c_parser_peek_token (parser)->value;
2691 c_parser_consume_token (parser);
2693 else if (c_parser_next_token_is (parser, CPP_WSTRING))
2695 error_at (c_parser_peek_token (parser)->location,
2696 "wide string literal in %<asm%>");
2697 str = build_string (1, "");
2698 c_parser_consume_token (parser);
2702 c_parser_error (parser, "expected string literal");
2708 /* Parse a simple asm expression. This is used in restricted
2709 contexts, where a full expression with inputs and outputs does not
2710 make sense. This is a GNU extension.
2713 asm ( asm-string-literal )
2717 c_parser_simple_asm_expr (c_parser *parser)
2720 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ASM));
2721 /* ??? Follow the C++ parser rather than using the
2722 lex_untranslated_string kludge. */
2723 parser->lex_untranslated_string = true;
2724 c_parser_consume_token (parser);
2725 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2727 parser->lex_untranslated_string = false;
2730 str = c_parser_asm_string_literal (parser);
2731 parser->lex_untranslated_string = false;
2732 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
2734 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
2740 /* Parse (possibly empty) attributes. This is a GNU extension.
2744 attributes attribute
2747 __attribute__ ( ( attribute-list ) )
2751 attribute_list , attrib
2756 any-word ( identifier )
2757 any-word ( identifier , nonempty-expr-list )
2758 any-word ( expr-list )
2760 where the "identifier" must not be declared as a type, and
2761 "any-word" may be any identifier (including one declared as a
2762 type), a reserved word storage class specifier, type specifier or
2763 type qualifier. ??? This still leaves out most reserved keywords
2764 (following the old parser), shouldn't we include them, and why not
2765 allow identifiers declared as types to start the arguments? */
2768 c_parser_attributes (c_parser *parser)
2770 tree attrs = NULL_TREE;
2771 while (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2773 /* ??? Follow the C++ parser rather than using the
2774 lex_untranslated_string kludge. */
2775 parser->lex_untranslated_string = true;
2776 c_parser_consume_token (parser);
2777 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2779 parser->lex_untranslated_string = false;
2782 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2784 parser->lex_untranslated_string = false;
2785 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
2788 /* Parse the attribute list. */
2789 while (c_parser_next_token_is (parser, CPP_COMMA)
2790 || c_parser_next_token_is (parser, CPP_NAME)
2791 || c_parser_next_token_is (parser, CPP_KEYWORD))
2793 tree attr, attr_name, attr_args;
2794 VEC(tree,gc) *expr_list;
2795 if (c_parser_next_token_is (parser, CPP_COMMA))
2797 c_parser_consume_token (parser);
2800 if (c_parser_next_token_is (parser, CPP_KEYWORD))
2802 /* ??? See comment above about what keywords are
2805 switch (c_parser_peek_token (parser)->keyword)
2842 /* Accept __attribute__((__const)) as __attribute__((const))
2845 = ridpointers[(int) c_parser_peek_token (parser)->keyword];
2848 attr_name = c_parser_peek_token (parser)->value;
2849 c_parser_consume_token (parser);
2850 if (c_parser_next_token_is_not (parser, CPP_OPEN_PAREN))
2852 attr = build_tree_list (attr_name, NULL_TREE);
2853 attrs = chainon (attrs, attr);
2856 c_parser_consume_token (parser);
2857 /* Parse the attribute contents. If they start with an
2858 identifier which is followed by a comma or close
2859 parenthesis, then the arguments start with that
2860 identifier; otherwise they are an expression list. */
2861 if (c_parser_next_token_is (parser, CPP_NAME)
2862 && c_parser_peek_token (parser)->id_kind == C_ID_ID
2863 && ((c_parser_peek_2nd_token (parser)->type == CPP_COMMA)
2864 || (c_parser_peek_2nd_token (parser)->type
2865 == CPP_CLOSE_PAREN)))
2867 tree arg1 = c_parser_peek_token (parser)->value;
2868 c_parser_consume_token (parser);
2869 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2870 attr_args = build_tree_list (NULL_TREE, arg1);
2874 c_parser_consume_token (parser);
2875 expr_list = c_parser_expr_list (parser, false, true, NULL);
2876 tree_list = c_parser_vec_to_tree_list (expr_list);
2877 attr_args = tree_cons (NULL_TREE, arg1, tree_list);
2878 c_parser_release_expr_list (expr_list);
2883 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2884 attr_args = NULL_TREE;
2887 expr_list = c_parser_expr_list (parser, false, true, NULL);
2888 attr_args = c_parser_vec_to_tree_list (expr_list);
2889 c_parser_release_expr_list (expr_list);
2892 attr = build_tree_list (attr_name, attr_args);
2893 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2894 c_parser_consume_token (parser);
2897 parser->lex_untranslated_string = false;
2898 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2902 attrs = chainon (attrs, attr);
2904 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2905 c_parser_consume_token (parser);
2908 parser->lex_untranslated_string = false;
2909 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2913 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2914 c_parser_consume_token (parser);
2917 parser->lex_untranslated_string = false;
2918 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2922 parser->lex_untranslated_string = false;
2927 /* Parse a type name (C90 6.5.5, C99 6.7.6).
2930 specifier-qualifier-list abstract-declarator[opt]
2933 static struct c_type_name *
2934 c_parser_type_name (c_parser *parser)
2936 struct c_declspecs *specs = build_null_declspecs ();
2937 struct c_declarator *declarator;
2938 struct c_type_name *ret;
2940 c_parser_declspecs (parser, specs, false, true, true);
2941 if (!specs->declspecs_seen_p)
2943 c_parser_error (parser, "expected specifier-qualifier-list");
2946 pending_xref_error ();
2947 finish_declspecs (specs);
2948 declarator = c_parser_declarator (parser, specs->type_seen_p,
2949 C_DTR_ABSTRACT, &dummy);
2950 if (declarator == NULL)
2952 ret = XOBNEW (&parser_obstack, struct c_type_name);
2954 ret->declarator = declarator;
2958 /* Parse an initializer (C90 6.5.7, C99 6.7.8).
2961 assignment-expression
2962 { initializer-list }
2963 { initializer-list , }
2966 designation[opt] initializer
2967 initializer-list , designation[opt] initializer
2974 designator-list designator
2981 [ constant-expression ]
2993 [ constant-expression ... constant-expression ]
2995 Any expression without commas is accepted in the syntax for the
2996 constant-expressions, with non-constant expressions rejected later.
2998 This function is only used for top-level initializers; for nested
2999 ones, see c_parser_initval. */
3001 static struct c_expr
3002 c_parser_initializer (c_parser *parser)
3004 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
3005 return c_parser_braced_init (parser, NULL_TREE, false);
3009 ret = c_parser_expr_no_commas (parser, NULL);
3010 if (TREE_CODE (ret.value) != STRING_CST
3011 && TREE_CODE (ret.value) != COMPOUND_LITERAL_EXPR)
3012 ret = default_function_array_conversion (ret);
3017 /* Parse a braced initializer list. TYPE is the type specified for a
3018 compound literal, and NULL_TREE for other initializers and for
3019 nested braced lists. NESTED_P is true for nested braced lists,
3020 false for the list of a compound literal or the list that is the
3021 top-level initializer in a declaration. */
3023 static struct c_expr
3024 c_parser_braced_init (c_parser *parser, tree type, bool nested_p)
3026 location_t brace_loc = c_parser_peek_token (parser)->location;
3027 gcc_assert (c_parser_next_token_is (parser, CPP_OPEN_BRACE));
3028 c_parser_consume_token (parser);
3030 push_init_level (0);
3032 really_start_incremental_init (type);
3033 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3035 pedwarn (brace_loc, OPT_pedantic, "ISO C forbids empty initializer braces");
3039 /* Parse a non-empty initializer list, possibly with a trailing
3043 c_parser_initelt (parser);
3046 if (c_parser_next_token_is (parser, CPP_COMMA))
3047 c_parser_consume_token (parser);
3050 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3054 if (c_parser_next_token_is_not (parser, CPP_CLOSE_BRACE))
3057 ret.value = error_mark_node;
3058 ret.original_code = ERROR_MARK;
3059 ret.original_type = NULL;
3060 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, "expected %<}%>");
3064 c_parser_consume_token (parser);
3065 return pop_init_level (0);
3068 /* Parse a nested initializer, including designators. */
3071 c_parser_initelt (c_parser *parser)
3073 /* Parse any designator or designator list. A single array
3074 designator may have the subsequent "=" omitted in GNU C, but a
3075 longer list or a structure member designator may not. */
3076 if (c_parser_next_token_is (parser, CPP_NAME)
3077 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
3079 /* Old-style structure member designator. */
3080 set_init_label (c_parser_peek_token (parser)->value);
3081 /* Use the colon as the error location. */
3082 pedwarn (c_parser_peek_2nd_token (parser)->location, OPT_pedantic,
3083 "obsolete use of designated initializer with %<:%>");
3084 c_parser_consume_token (parser);
3085 c_parser_consume_token (parser);
3089 /* des_seen is 0 if there have been no designators, 1 if there
3090 has been a single array designator and 2 otherwise. */
3092 /* Location of a designator. */
3093 location_t des_loc = UNKNOWN_LOCATION; /* Quiet warning. */
3094 while (c_parser_next_token_is (parser, CPP_OPEN_SQUARE)
3095 || c_parser_next_token_is (parser, CPP_DOT))
3097 int des_prev = des_seen;
3099 des_loc = c_parser_peek_token (parser)->location;
3102 if (c_parser_next_token_is (parser, CPP_DOT))
3105 c_parser_consume_token (parser);
3106 if (c_parser_next_token_is (parser, CPP_NAME))
3108 set_init_label (c_parser_peek_token (parser)->value);
3109 c_parser_consume_token (parser);
3114 init.value = error_mark_node;
3115 init.original_code = ERROR_MARK;
3116 init.original_type = NULL;
3117 c_parser_error (parser, "expected identifier");
3118 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
3119 process_init_element (init, false);
3126 location_t ellipsis_loc = UNKNOWN_LOCATION; /* Quiet warning. */
3127 /* ??? Following the old parser, [ objc-receiver
3128 objc-message-args ] is accepted as an initializer,
3129 being distinguished from a designator by what follows
3130 the first assignment expression inside the square
3131 brackets, but after a first array designator a
3132 subsequent square bracket is for Objective-C taken to
3133 start an expression, using the obsolete form of
3134 designated initializer without '=', rather than
3135 possibly being a second level of designation: in LALR
3136 terms, the '[' is shifted rather than reducing
3137 designator to designator-list. */
3138 if (des_prev == 1 && c_dialect_objc ())
3140 des_seen = des_prev;
3143 if (des_prev == 0 && c_dialect_objc ())
3145 /* This might be an array designator or an
3146 Objective-C message expression. If the former,
3147 continue parsing here; if the latter, parse the
3148 remainder of the initializer given the starting
3149 primary-expression. ??? It might make sense to
3150 distinguish when des_prev == 1 as well; see
3151 previous comment. */
3153 struct c_expr mexpr;
3154 c_parser_consume_token (parser);
3155 if (c_parser_peek_token (parser)->type == CPP_NAME
3156 && ((c_parser_peek_token (parser)->id_kind
3158 || (c_parser_peek_token (parser)->id_kind
3159 == C_ID_CLASSNAME)))
3161 /* Type name receiver. */
3162 tree id = c_parser_peek_token (parser)->value;
3163 c_parser_consume_token (parser);
3164 rec = objc_get_class_reference (id);
3165 goto parse_message_args;
3167 first = c_parser_expr_no_commas (parser, NULL).value;
3168 if (c_parser_next_token_is (parser, CPP_ELLIPSIS)
3169 || c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
3170 goto array_desig_after_first;
3171 /* Expression receiver. So far only one part
3172 without commas has been parsed; there might be
3173 more of the expression. */
3175 while (c_parser_next_token_is (parser, CPP_COMMA))
3178 c_parser_consume_token (parser);
3179 next = c_parser_expr_no_commas (parser, NULL);
3180 next = default_function_array_conversion (next);
3181 rec = build_compound_expr (rec, next.value);
3184 /* Now parse the objc-message-args. */
3185 args = c_parser_objc_message_args (parser);
3186 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
3189 = objc_build_message_expr (build_tree_list (rec, args));
3190 mexpr.original_code = ERROR_MARK;
3191 mexpr.original_type = NULL;
3192 /* Now parse and process the remainder of the
3193 initializer, starting with this message
3194 expression as a primary-expression. */
3195 c_parser_initval (parser, &mexpr);
3198 c_parser_consume_token (parser);
3199 first = c_parser_expr_no_commas (parser, NULL).value;
3200 array_desig_after_first:
3201 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
3203 ellipsis_loc = c_parser_peek_token (parser)->location;
3204 c_parser_consume_token (parser);
3205 second = c_parser_expr_no_commas (parser, NULL).value;
3209 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
3211 c_parser_consume_token (parser);
3212 set_init_index (first, second);
3214 pedwarn (ellipsis_loc, OPT_pedantic,
3215 "ISO C forbids specifying range of elements to initialize");
3218 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
3224 if (c_parser_next_token_is (parser, CPP_EQ))
3227 pedwarn (des_loc, OPT_pedantic,
3228 "ISO C90 forbids specifying subobject to initialize");
3229 c_parser_consume_token (parser);
3234 pedwarn (c_parser_peek_token (parser)->location, OPT_pedantic,
3235 "obsolete use of designated initializer without %<=%>");
3239 init.value = error_mark_node;
3240 init.original_code = ERROR_MARK;
3241 init.original_type = NULL;
3242 c_parser_error (parser, "expected %<=%>");
3243 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
3244 process_init_element (init, false);
3250 c_parser_initval (parser, NULL);
3253 /* Parse a nested initializer; as c_parser_initializer but parses
3254 initializers within braced lists, after any designators have been
3255 applied. If AFTER is not NULL then it is an Objective-C message
3256 expression which is the primary-expression starting the
3260 c_parser_initval (c_parser *parser, struct c_expr *after)
3263 gcc_assert (!after || c_dialect_objc ());
3264 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE) && !after)
3265 init = c_parser_braced_init (parser, NULL_TREE, true);
3268 init = c_parser_expr_no_commas (parser, after);
3269 if (init.value != NULL_TREE
3270 && TREE_CODE (init.value) != STRING_CST
3271 && TREE_CODE (init.value) != COMPOUND_LITERAL_EXPR)
3272 init = default_function_array_conversion (init);
3274 process_init_element (init, false);
3277 /* Parse a compound statement (possibly a function body) (C90 6.6.2,
3281 { block-item-list[opt] }
3282 { label-declarations block-item-list }
3286 block-item-list block-item
3298 { label-declarations block-item-list }
3301 __extension__ nested-declaration
3302 nested-function-definition
3306 label-declarations label-declaration
3309 __label__ identifier-list ;
3311 Allowing the mixing of declarations and code is new in C99. The
3312 GNU syntax also permits (not shown above) labels at the end of
3313 compound statements, which yield an error. We don't allow labels
3314 on declarations; this might seem like a natural extension, but
3315 there would be a conflict between attributes on the label and
3316 prefix attributes on the declaration. ??? The syntax follows the
3317 old parser in requiring something after label declarations.
3318 Although they are erroneous if the labels declared aren't defined,
3319 is it useful for the syntax to be this way?
3331 c_parser_compound_statement (c_parser *parser)
3334 if (!c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
3336 /* Ensure a scope is entered and left anyway to avoid confusion
3337 if we have just prepared to enter a function body. */
3338 stmt = c_begin_compound_stmt (true);
3339 c_end_compound_stmt (stmt, true);
3340 return error_mark_node;
3342 stmt = c_begin_compound_stmt (true);
3343 c_parser_compound_statement_nostart (parser);
3344 return c_end_compound_stmt (stmt, true);
3347 /* Parse a compound statement except for the opening brace. This is
3348 used for parsing both compound statements and statement expressions
3349 (which follow different paths to handling the opening). */
3352 c_parser_compound_statement_nostart (c_parser *parser)
3354 bool last_stmt = false;
3355 bool last_label = false;
3356 bool save_valid_for_pragma = valid_location_for_stdc_pragma_p ();
3357 location_t label_loc = UNKNOWN_LOCATION; /* Quiet warning. */
3358 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3360 c_parser_consume_token (parser);
3363 mark_valid_location_for_stdc_pragma (true);
3364 if (c_parser_next_token_is_keyword (parser, RID_LABEL))
3366 location_t err_loc = c_parser_peek_token (parser)->location;
3367 /* Read zero or more forward-declarations for labels that nested
3368 functions can jump to. */
3369 mark_valid_location_for_stdc_pragma (false);
3370 while (c_parser_next_token_is_keyword (parser, RID_LABEL))
3372 c_parser_consume_token (parser);
3373 /* Any identifiers, including those declared as type names,
3378 if (c_parser_next_token_is_not (parser, CPP_NAME))
3380 c_parser_error (parser, "expected identifier");
3384 = declare_label (c_parser_peek_token (parser)->value);
3385 C_DECLARED_LABEL_FLAG (label) = 1;
3386 add_stmt (build_stmt (DECL_EXPR, label));
3387 c_parser_consume_token (parser);
3388 if (c_parser_next_token_is (parser, CPP_COMMA))
3389 c_parser_consume_token (parser);
3393 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
3395 pedwarn (err_loc, OPT_pedantic, "ISO C forbids label declarations");
3397 /* We must now have at least one statement, label or declaration. */
3398 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3400 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
3401 c_parser_error (parser, "expected declaration or statement");
3402 c_parser_consume_token (parser);
3405 while (c_parser_next_token_is_not (parser, CPP_CLOSE_BRACE))
3407 location_t loc = c_parser_peek_token (parser)->location;
3408 if (c_parser_next_token_is_keyword (parser, RID_CASE)
3409 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
3410 || (c_parser_next_token_is (parser, CPP_NAME)
3411 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
3413 if (c_parser_next_token_is_keyword (parser, RID_CASE))
3414 label_loc = c_parser_peek_2nd_token (parser)->location;
3416 label_loc = c_parser_peek_token (parser)->location;
3419 mark_valid_location_for_stdc_pragma (false);
3420 c_parser_label (parser);
3422 else if (!last_label
3423 && c_parser_next_token_starts_declspecs (parser))
3426 mark_valid_location_for_stdc_pragma (false);
3427 c_parser_declaration_or_fndef (parser, true, true, true, true);
3430 (pedantic && !flag_isoc99)
3432 : OPT_Wdeclaration_after_statement,
3433 "ISO C90 forbids mixed declarations and code");
3436 else if (!last_label
3437 && c_parser_next_token_is_keyword (parser, RID_EXTENSION))
3439 /* __extension__ can start a declaration, but is also an
3440 unary operator that can start an expression. Consume all
3441 but the last of a possible series of __extension__ to
3443 while (c_parser_peek_2nd_token (parser)->type == CPP_KEYWORD
3444 && (c_parser_peek_2nd_token (parser)->keyword
3446 c_parser_consume_token (parser);
3447 if (c_token_starts_declspecs (c_parser_peek_2nd_token (parser)))
3450 ext = disable_extension_diagnostics ();
3451 c_parser_consume_token (parser);
3453 mark_valid_location_for_stdc_pragma (false);
3454 c_parser_declaration_or_fndef (parser, true, true, true, true);
3455 /* Following the old parser, __extension__ does not
3456 disable this diagnostic. */
3457 restore_extension_diagnostics (ext);
3459 pedwarn_c90 (loc, (pedantic && !flag_isoc99)
3461 : OPT_Wdeclaration_after_statement,
3462 "ISO C90 forbids mixed declarations and code");
3468 else if (c_parser_next_token_is (parser, CPP_PRAGMA))
3470 /* External pragmas, and some omp pragmas, are not associated
3471 with regular c code, and so are not to be considered statements
3472 syntactically. This ensures that the user doesn't put them
3473 places that would turn into syntax errors if the directive
3475 if (c_parser_pragma (parser, pragma_compound))
3476 last_label = false, last_stmt = true;
3478 else if (c_parser_next_token_is (parser, CPP_EOF))
3480 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
3481 c_parser_error (parser, "expected declaration or statement");
3484 else if (c_parser_next_token_is_keyword (parser, RID_ELSE))
3486 if (parser->in_if_block)
3488 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
3489 error_at (loc, """expected %<}%> before %<else%>");
3494 error_at (loc, "%<else%> without a previous %<if%>");
3495 c_parser_consume_token (parser);
3504 mark_valid_location_for_stdc_pragma (false);
3505 c_parser_statement_after_labels (parser);
3508 parser->error = false;
3511 error_at (label_loc, "label at end of compound statement");
3512 c_parser_consume_token (parser);
3513 /* Restore the value we started with. */
3514 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
3517 /* Parse a label (C90 6.6.1, C99 6.8.1).
3520 identifier : attributes[opt]
3521 case constant-expression :
3527 case constant-expression ... constant-expression :
3529 The use of attributes on labels is a GNU extension. The syntax in
3530 GNU C accepts any expressions without commas, non-constant
3531 expressions being rejected later. */
3534 c_parser_label (c_parser *parser)
3536 location_t loc1 = c_parser_peek_token (parser)->location;
3537 tree label = NULL_TREE;
3538 if (c_parser_next_token_is_keyword (parser, RID_CASE))
3541 c_parser_consume_token (parser);
3542 exp1 = c_parser_expr_no_commas (parser, NULL).value;
3543 if (c_parser_next_token_is (parser, CPP_COLON))
3545 c_parser_consume_token (parser);
3546 label = do_case (exp1, NULL_TREE);
3548 else if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
3550 c_parser_consume_token (parser);
3551 exp2 = c_parser_expr_no_commas (parser, NULL).value;
3552 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
3553 label = do_case (exp1, exp2);
3556 c_parser_error (parser, "expected %<:%> or %<...%>");
3558 else if (c_parser_next_token_is_keyword (parser, RID_DEFAULT))
3560 c_parser_consume_token (parser);
3561 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
3562 label = do_case (NULL_TREE, NULL_TREE);
3566 tree name = c_parser_peek_token (parser)->value;
3569 location_t loc2 = c_parser_peek_token (parser)->location;
3570 gcc_assert (c_parser_next_token_is (parser, CPP_NAME));
3571 c_parser_consume_token (parser);
3572 gcc_assert (c_parser_next_token_is (parser, CPP_COLON));
3573 c_parser_consume_token (parser);
3574 attrs = c_parser_attributes (parser);
3575 tlab = define_label (loc2, name);
3578 decl_attributes (&tlab, attrs, 0);
3579 label = add_stmt (build_stmt (LABEL_EXPR, tlab));
3584 SET_EXPR_LOCATION (label, loc1);
3585 if (c_parser_next_token_starts_declspecs (parser)
3586 && !(c_parser_next_token_is (parser, CPP_NAME)
3587 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
3589 error_at (c_parser_peek_token (parser)->location,
3590 "a label can only be part of a statement and "
3591 "a declaration is not a statement");
3592 c_parser_declaration_or_fndef (parser, /*fndef_ok*/ false,
3593 /*nested*/ true, /*empty_ok*/ false,
3594 /*start_attr_ok*/ true);
3599 /* Parse a statement (C90 6.6, C99 6.8).
3604 expression-statement
3612 expression-statement:
3615 selection-statement:
3619 iteration-statement:
3628 return expression[opt] ;
3641 objc-throw-statement
3642 objc-try-catch-statement
3643 objc-synchronized-statement
3645 objc-throw-statement:
3659 parallel-for-construct
3660 parallel-sections-construct
3667 parallel-directive structured-block
3670 for-directive iteration-statement
3673 sections-directive section-scope
3676 single-directive structured-block
3678 parallel-for-construct:
3679 parallel-for-directive iteration-statement
3681 parallel-sections-construct:
3682 parallel-sections-directive section-scope
3685 master-directive structured-block
3688 critical-directive structured-block
3691 atomic-directive expression-statement
3694 ordered-directive structured-block */
3697 c_parser_statement (c_parser *parser)
3699 while (c_parser_next_token_is_keyword (parser, RID_CASE)
3700 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
3701 || (c_parser_next_token_is (parser, CPP_NAME)
3702 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
3703 c_parser_label (parser);
3704 c_parser_statement_after_labels (parser);
3707 /* Parse a statement, other than a labeled statement. */
3710 c_parser_statement_after_labels (c_parser *parser)
3712 location_t loc = c_parser_peek_token (parser)->location;
3713 tree stmt = NULL_TREE;
3714 bool in_if_block = parser->in_if_block;
3715 parser->in_if_block = false;
3716 switch (c_parser_peek_token (parser)->type)
3718 case CPP_OPEN_BRACE:
3719 add_stmt (c_parser_compound_statement (parser));
3722 switch (c_parser_peek_token (parser)->keyword)
3725 c_parser_if_statement (parser);
3728 c_parser_switch_statement (parser);
3731 c_parser_while_statement (parser);
3734 c_parser_do_statement (parser);
3737 c_parser_for_statement (parser);
3740 c_parser_consume_token (parser);
3741 if (c_parser_next_token_is (parser, CPP_NAME))
3743 stmt = c_finish_goto_label (c_parser_peek_token (parser)->value);
3744 c_parser_consume_token (parser);
3746 else if (c_parser_next_token_is (parser, CPP_MULT))
3748 c_parser_consume_token (parser);
3749 stmt = c_finish_goto_ptr (c_parser_expression (parser).value);
3752 c_parser_error (parser, "expected identifier or %<*%>");
3753 goto expect_semicolon;
3755 c_parser_consume_token (parser);
3756 stmt = c_finish_bc_stmt (&c_cont_label, false);
3757 goto expect_semicolon;
3759 c_parser_consume_token (parser);
3760 stmt = c_finish_bc_stmt (&c_break_label, true);
3761 goto expect_semicolon;
3763 c_parser_consume_token (parser);
3764 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
3766 stmt = c_finish_return (NULL_TREE, NULL_TREE);
3767 c_parser_consume_token (parser);
3771 struct c_expr expr = c_parser_expression_conv (parser);
3772 stmt = c_finish_return (expr.value, expr.original_type);
3773 goto expect_semicolon;
3777 stmt = c_parser_asm_statement (parser);
3780 gcc_assert (c_dialect_objc ());
3781 c_parser_consume_token (parser);
3782 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
3784 stmt = objc_build_throw_stmt (NULL_TREE);
3785 c_parser_consume_token (parser);
3789 tree expr = c_parser_expression (parser).value;
3790 expr = c_fully_fold (expr, false, NULL);
3791 stmt = objc_build_throw_stmt (expr);
3792 goto expect_semicolon;
3796 gcc_assert (c_dialect_objc ());
3797 c_parser_objc_try_catch_statement (parser);
3799 case RID_AT_SYNCHRONIZED:
3800 gcc_assert (c_dialect_objc ());
3801 c_parser_objc_synchronized_statement (parser);
3808 c_parser_consume_token (parser);
3810 case CPP_CLOSE_PAREN:
3811 case CPP_CLOSE_SQUARE:
3812 /* Avoid infinite loop in error recovery:
3813 c_parser_skip_until_found stops at a closing nesting
3814 delimiter without consuming it, but here we need to consume
3815 it to proceed further. */
3816 c_parser_error (parser, "expected statement");
3817 c_parser_consume_token (parser);
3820 c_parser_pragma (parser, pragma_stmt);
3824 stmt = c_finish_expr_stmt (c_parser_expression_conv (parser).value);
3826 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
3829 /* Two cases cannot and do not have line numbers associated: If stmt
3830 is degenerate, such as "2;", then stmt is an INTEGER_CST, which
3831 cannot hold line numbers. But that's OK because the statement
3832 will either be changed to a MODIFY_EXPR during gimplification of
3833 the statement expr, or discarded. If stmt was compound, but
3834 without new variables, we will have skipped the creation of a
3835 BIND and will have a bare STATEMENT_LIST. But that's OK because
3836 (recursively) all of the component statements should already have
3837 line numbers assigned. ??? Can we discard no-op statements
3839 protected_set_expr_location (stmt, loc);
3841 parser->in_if_block = in_if_block;
3844 /* Parse the condition from an if, do, while or for statements. */
3847 c_parser_condition (c_parser *parser)
3851 loc = c_parser_peek_token (parser)->location;
3852 cond = c_parser_expression_conv (parser).value;
3853 cond = c_objc_common_truthvalue_conversion (loc, cond);
3854 cond = c_fully_fold (cond, false, NULL);
3855 if (warn_sequence_point)
3856 verify_sequence_points (cond);
3860 /* Parse a parenthesized condition from an if, do or while statement.
3866 c_parser_paren_condition (c_parser *parser)
3869 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3870 return error_mark_node;
3871 cond = c_parser_condition (parser);
3872 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
3876 /* Parse a statement which is a block in C99. */
3879 c_parser_c99_block_statement (c_parser *parser)
3881 tree block = c_begin_compound_stmt (flag_isoc99);
3882 c_parser_statement (parser);
3883 return c_end_compound_stmt (block, flag_isoc99);
3886 /* Parse the body of an if statement. This is just parsing a
3887 statement but (a) it is a block in C99, (b) we track whether the
3888 body is an if statement for the sake of -Wparentheses warnings, (c)
3889 we handle an empty body specially for the sake of -Wempty-body
3890 warnings, and (d) we call parser_compound_statement directly
3891 because c_parser_statement_after_labels resets
3892 parser->in_if_block. */
3895 c_parser_if_body (c_parser *parser, bool *if_p)
3897 tree block = c_begin_compound_stmt (flag_isoc99);
3898 while (c_parser_next_token_is_keyword (parser, RID_CASE)
3899 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
3900 || (c_parser_next_token_is (parser, CPP_NAME)
3901 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
3902 c_parser_label (parser);
3903 *if_p = c_parser_next_token_is_keyword (parser, RID_IF);
3904 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
3906 location_t loc = c_parser_peek_token (parser)->location;
3907 add_stmt (build_empty_stmt ());
3908 c_parser_consume_token (parser);
3909 if (!c_parser_next_token_is_keyword (parser, RID_ELSE))
3910 warning_at (loc, OPT_Wempty_body,
3911 "suggest braces around empty body in an %<if%> statement");
3913 else if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
3914 add_stmt (c_parser_compound_statement (parser));
3916 c_parser_statement_after_labels (parser);
3917 return c_end_compound_stmt (block, flag_isoc99);
3920 /* Parse the else body of an if statement. This is just parsing a
3921 statement but (a) it is a block in C99, (b) we handle an empty body
3922 specially for the sake of -Wempty-body warnings. */
3925 c_parser_else_body (c_parser *parser)
3927 tree block = c_begin_compound_stmt (flag_isoc99);
3928 while (c_parser_next_token_is_keyword (parser, RID_CASE)
3929 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
3930 || (c_parser_next_token_is (parser, CPP_NAME)
3931 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
3932 c_parser_label (parser);
3933 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
3935 warning_at (c_parser_peek_token (parser)->location,
3937 "suggest braces around empty body in an %<else%> statement");
3938 add_stmt (build_empty_stmt ());
3939 c_parser_consume_token (parser);
3942 c_parser_statement_after_labels (parser);
3943 return c_end_compound_stmt (block, flag_isoc99);
3946 /* Parse an if statement (C90 6.6.4, C99 6.8.4).
3949 if ( expression ) statement
3950 if ( expression ) statement else statement
3954 c_parser_if_statement (c_parser *parser)
3959 bool first_if = false;
3960 tree first_body, second_body;
3963 gcc_assert (c_parser_next_token_is_keyword (parser, RID_IF));
3964 c_parser_consume_token (parser);
3965 block = c_begin_compound_stmt (flag_isoc99);
3966 loc = c_parser_peek_token (parser)->location;
3967 cond = c_parser_paren_condition (parser);
3968 in_if_block = parser->in_if_block;
3969 parser->in_if_block = true;
3970 first_body = c_parser_if_body (parser, &first_if);
3971 parser->in_if_block = in_if_block;
3972 if (c_parser_next_token_is_keyword (parser, RID_ELSE))
3974 c_parser_consume_token (parser);
3975 second_body = c_parser_else_body (parser);
3978 second_body = NULL_TREE;
3979 c_finish_if_stmt (loc, cond, first_body, second_body, first_if);
3980 add_stmt (c_end_compound_stmt (block, flag_isoc99));
3983 /* Parse a switch statement (C90 6.6.4, C99 6.8.4).
3986 switch (expression) statement
3990 c_parser_switch_statement (c_parser *parser)
3992 tree block, expr, body, save_break;
3993 gcc_assert (c_parser_next_token_is_keyword (parser, RID_SWITCH));
3994 c_parser_consume_token (parser);
3995 block = c_begin_compound_stmt (flag_isoc99);
3996 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3998 expr = c_parser_expression (parser).value;
3999 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
4002 expr = error_mark_node;
4003 c_start_case (expr);
4004 save_break = c_break_label;
4005 c_break_label = NULL_TREE;
4006 body = c_parser_c99_block_statement (parser);
4007 c_finish_case (body);
4009 add_stmt (build1 (LABEL_EXPR, void_type_node, c_break_label));
4010 c_break_label = save_break;
4011 add_stmt (c_end_compound_stmt (block, flag_isoc99));
4014 /* Parse a while statement (C90 6.6.5, C99 6.8.5).
4017 while (expression) statement
4021 c_parser_while_statement (c_parser *parser)
4023 tree block, cond, body, save_break, save_cont;
4025 gcc_assert (c_parser_next_token_is_keyword (parser, RID_WHILE));
4026 c_parser_consume_token (parser);
4027 block = c_begin_compound_stmt (flag_isoc99);
4028 loc = c_parser_peek_token (parser)->location;
4029 cond = c_parser_paren_condition (parser);
4030 save_break = c_break_label;
4031 c_break_label = NULL_TREE;
4032 save_cont = c_cont_label;
4033 c_cont_label = NULL_TREE;
4034 body = c_parser_c99_block_statement (parser);
4035 c_finish_loop (loc, cond, NULL, body, c_break_label, c_cont_label, true);
4036 add_stmt (c_end_compound_stmt (block, flag_isoc99));
4037 c_break_label = save_break;
4038 c_cont_label = save_cont;
4041 /* Parse a do statement (C90 6.6.5, C99 6.8.5).
4044 do statement while ( expression ) ;
4048 c_parser_do_statement (c_parser *parser)
4050 tree block, cond, body, save_break, save_cont, new_break, new_cont;
4052 gcc_assert (c_parser_next_token_is_keyword (parser, RID_DO));
4053 c_parser_consume_token (parser);
4054 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
4055 warning_at (c_parser_peek_token (parser)->location,
4057 "suggest braces around empty body in %<do%> statement");
4058 block = c_begin_compound_stmt (flag_isoc99);
4059 loc = c_parser_peek_token (parser)->location;
4060 save_break = c_break_label;
4061 c_break_label = NULL_TREE;
4062 save_cont = c_cont_label;
4063 c_cont_label = NULL_TREE;
4064 body = c_parser_c99_block_statement (parser);
4065 c_parser_require_keyword (parser, RID_WHILE, "expected %<while%>");
4066 new_break = c_break_label;
4067 c_break_label = save_break;
4068 new_cont = c_cont_label;
4069 c_cont_label = save_cont;
4070 cond = c_parser_paren_condition (parser);
4071 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
4072 c_parser_skip_to_end_of_block_or_statement (parser);
4073 c_finish_loop (loc, cond, NULL, body, new_break, new_cont, false);
4074 add_stmt (c_end_compound_stmt (block, flag_isoc99));
4077 /* Parse a for statement (C90 6.6.5, C99 6.8.5).
4080 for ( expression[opt] ; expression[opt] ; expression[opt] ) statement
4081 for ( nested-declaration expression[opt] ; expression[opt] ) statement
4083 The form with a declaration is new in C99.
4085 ??? In accordance with the old parser, the declaration may be a
4086 nested function, which is then rejected in check_for_loop_decls,
4087 but does it make any sense for this to be included in the grammar?
4088 Note in particular that the nested function does not include a
4089 trailing ';', whereas the "declaration" production includes one.
4090 Also, can we reject bad declarations earlier and cheaper than
4091 check_for_loop_decls? */
4094 c_parser_for_statement (c_parser *parser)
4096 tree block, cond, incr, save_break, save_cont, body;
4098 gcc_assert (c_parser_next_token_is_keyword (parser, RID_FOR));
4099 loc = c_parser_peek_token (parser)->location;
4100 c_parser_consume_token (parser);
4101 block = c_begin_compound_stmt (flag_isoc99);
4102 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4104 /* Parse the initialization declaration or expression. */
4105 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
4107 c_parser_consume_token (parser);
4108 c_finish_expr_stmt (NULL_TREE);
4110 else if (c_parser_next_token_starts_declspecs (parser))
4112 c_parser_declaration_or_fndef (parser, true, true, true, true);
4113 check_for_loop_decls ();
4115 else if (c_parser_next_token_is_keyword (parser, RID_EXTENSION))
4117 /* __extension__ can start a declaration, but is also an
4118 unary operator that can start an expression. Consume all
4119 but the last of a possible series of __extension__ to
4121 while (c_parser_peek_2nd_token (parser)->type == CPP_KEYWORD
4122 && (c_parser_peek_2nd_token (parser)->keyword
4124 c_parser_consume_token (parser);
4125 if (c_token_starts_declspecs (c_parser_peek_2nd_token (parser)))
4128 ext = disable_extension_diagnostics ();
4129 c_parser_consume_token (parser);
4130 c_parser_declaration_or_fndef (parser, true, true, true, true);
4131 restore_extension_diagnostics (ext);
4132 check_for_loop_decls ();
4140 c_finish_expr_stmt (c_parser_expression (parser).value);
4141 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
4143 /* Parse the loop condition. */
4144 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
4146 c_parser_consume_token (parser);
4151 cond = c_parser_condition (parser);
4152 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
4154 /* Parse the increment expression. */
4155 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4156 incr = c_process_expr_stmt (NULL_TREE);
4158 incr = c_process_expr_stmt (c_parser_expression (parser).value);
4159 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
4163 cond = error_mark_node;
4164 incr = error_mark_node;
4166 save_break = c_break_label;
4167 c_break_label = NULL_TREE;
4168 save_cont = c_cont_label;
4169 c_cont_label = NULL_TREE;
4170 body = c_parser_c99_block_statement (parser);
4171 c_finish_loop (loc, cond, incr, body, c_break_label, c_cont_label, true);
4172 add_stmt (c_end_compound_stmt (block, flag_isoc99));
4173 c_break_label = save_break;
4174 c_cont_label = save_cont;
4177 /* Parse an asm statement, a GNU extension. This is a full-blown asm
4178 statement with inputs, outputs, clobbers, and volatile tag
4182 asm type-qualifier[opt] ( asm-argument ) ;
4186 asm-string-literal : asm-operands[opt]
4187 asm-string-literal : asm-operands[opt] : asm-operands[opt]
4188 asm-string-literal : asm-operands[opt] : asm-operands[opt] : asm-clobbers
4190 Qualifiers other than volatile are accepted in the syntax but
4194 c_parser_asm_statement (c_parser *parser)
4196 tree quals, str, outputs, inputs, clobbers, ret;
4198 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ASM));
4199 c_parser_consume_token (parser);
4200 if (c_parser_next_token_is_keyword (parser, RID_VOLATILE))
4202 quals = c_parser_peek_token (parser)->value;
4203 c_parser_consume_token (parser);
4205 else if (c_parser_next_token_is_keyword (parser, RID_CONST)
4206 || c_parser_next_token_is_keyword (parser, RID_RESTRICT))
4208 warning_at (c_parser_peek_token (parser)->location,
4210 "%E qualifier ignored on asm",
4211 c_parser_peek_token (parser)->value);
4213 c_parser_consume_token (parser);
4217 /* ??? Follow the C++ parser rather than using the
4218 lex_untranslated_string kludge. */
4219 parser->lex_untranslated_string = true;
4220 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4222 parser->lex_untranslated_string = false;
4225 str = c_parser_asm_string_literal (parser);
4226 if (str == NULL_TREE)
4228 parser->lex_untranslated_string = false;
4229 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
4232 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4235 outputs = NULL_TREE;
4237 clobbers = NULL_TREE;
4240 if (!c_parser_require (parser, CPP_COLON, "expected %<:%> or %<)%>"))
4242 parser->lex_untranslated_string = false;
4243 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
4247 /* Parse outputs. */
4248 if (c_parser_next_token_is (parser, CPP_COLON)
4249 || c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4250 outputs = NULL_TREE;
4252 outputs = c_parser_asm_operands (parser, false);
4253 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4256 clobbers = NULL_TREE;
4259 if (!c_parser_require (parser, CPP_COLON, "expected %<:%> or %<)%>"))
4261 parser->lex_untranslated_string = false;
4262 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
4266 if (c_parser_next_token_is (parser, CPP_COLON)
4267 || c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4270 inputs = c_parser_asm_operands (parser, true);
4271 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4273 clobbers = NULL_TREE;
4276 if (!c_parser_require (parser, CPP_COLON, "expected %<:%> or %<)%>"))
4278 parser->lex_untranslated_string = false;
4279 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
4282 /* Parse clobbers. */
4283 clobbers = c_parser_asm_clobbers (parser);
4285 parser->lex_untranslated_string = false;
4286 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
4288 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
4291 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
4292 c_parser_skip_to_end_of_block_or_statement (parser);
4293 ret = build_asm_stmt (quals, build_asm_expr (str, outputs, inputs,
4298 /* Parse asm operands, a GNU extension. If CONVERT_P (for inputs but
4299 not outputs), apply the default conversion of functions and arrays
4304 asm-operands , asm-operand
4307 asm-string-literal ( expression )
4308 [ identifier ] asm-string-literal ( expression )
4312 c_parser_asm_operands (c_parser *parser, bool convert_p)
4314 tree list = NULL_TREE;
4319 if (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
4321 c_parser_consume_token (parser);
4322 if (c_parser_next_token_is (parser, CPP_NAME))
4324 tree id = c_parser_peek_token (parser)->value;
4325 c_parser_consume_token (parser);
4326 name = build_string (IDENTIFIER_LENGTH (id),
4327 IDENTIFIER_POINTER (id));
4331 c_parser_error (parser, "expected identifier");
4332 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
4335 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
4340 str = c_parser_asm_string_literal (parser);
4341 if (str == NULL_TREE)
4343 parser->lex_untranslated_string = false;
4344 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4346 parser->lex_untranslated_string = true;
4349 expr = c_parser_expression (parser);
4351 expr = default_function_array_conversion (expr);
4352 expr.value = c_fully_fold (expr.value, false, NULL);
4353 parser->lex_untranslated_string = true;
4354 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
4356 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
4359 list = chainon (list, build_tree_list (build_tree_list (name, str),
4361 if (c_parser_next_token_is (parser, CPP_COMMA))
4362 c_parser_consume_token (parser);
4369 /* Parse asm clobbers, a GNU extension.
4373 asm-clobbers , asm-string-literal
4377 c_parser_asm_clobbers (c_parser *parser)
4379 tree list = NULL_TREE;
4382 tree str = c_parser_asm_string_literal (parser);
4384 list = tree_cons (NULL_TREE, str, list);
4387 if (c_parser_next_token_is (parser, CPP_COMMA))
4388 c_parser_consume_token (parser);
4395 /* Parse an expression other than a compound expression; that is, an
4396 assignment expression (C90 6.3.16, C99 6.5.16). If AFTER is not
4397 NULL then it is an Objective-C message expression which is the
4398 primary-expression starting the expression as an initializer.
4400 assignment-expression:
4401 conditional-expression
4402 unary-expression assignment-operator assignment-expression
4404 assignment-operator: one of
4405 = *= /= %= += -= <<= >>= &= ^= |=
4407 In GNU C we accept any conditional expression on the LHS and
4408 diagnose the invalid lvalue rather than producing a syntax
4411 static struct c_expr
4412 c_parser_expr_no_commas (c_parser *parser, struct c_expr *after)
4414 struct c_expr lhs, rhs, ret;
4415 enum tree_code code;
4416 location_t op_location;
4417 gcc_assert (!after || c_dialect_objc ());
4418 lhs = c_parser_conditional_expression (parser, after);
4419 op_location = c_parser_peek_token (parser)->location;
4420 switch (c_parser_peek_token (parser)->type)
4429 code = TRUNC_DIV_EXPR;
4432 code = TRUNC_MOD_EXPR;
4447 code = BIT_AND_EXPR;
4450 code = BIT_XOR_EXPR;
4453 code = BIT_IOR_EXPR;
4458 c_parser_consume_token (parser);
4459 rhs = c_parser_expr_no_commas (parser, NULL);
4460 rhs = default_function_array_conversion (rhs);
4461 ret.value = build_modify_expr (op_location, lhs.value, lhs.original_type,
4462 code, rhs.value, rhs.original_type);
4463 if (code == NOP_EXPR)
4464 ret.original_code = MODIFY_EXPR;
4467 TREE_NO_WARNING (ret.value) = 1;
4468 ret.original_code = ERROR_MARK;
4470 ret.original_type = NULL;
4474 /* Parse a conditional expression (C90 6.3.15, C99 6.5.15). If AFTER
4475 is not NULL then it is an Objective-C message expression which is
4476 the primary-expression starting the expression as an initializer.
4478 conditional-expression:
4479 logical-OR-expression
4480 logical-OR-expression ? expression : conditional-expression
4484 conditional-expression:
4485 logical-OR-expression ? : conditional-expression
4488 static struct c_expr
4489 c_parser_conditional_expression (c_parser *parser, struct c_expr *after)
4491 struct c_expr cond, exp1, exp2, ret;
4492 location_t cond_loc;
4494 gcc_assert (!after || c_dialect_objc ());
4496 cond_loc = c_parser_peek_token (parser)->location;
4497 cond = c_parser_binary_expression (parser, after);
4499 if (c_parser_next_token_is_not (parser, CPP_QUERY))
4501 cond = default_function_array_conversion (cond);
4502 c_parser_consume_token (parser);
4503 if (c_parser_next_token_is (parser, CPP_COLON))
4505 tree eptype = NULL_TREE;
4506 pedwarn (c_parser_peek_token (parser)->location, OPT_pedantic,
4507 "ISO C forbids omitting the middle term of a ?: expression");
4508 if (TREE_CODE (cond.value) == EXCESS_PRECISION_EXPR)
4510 eptype = TREE_TYPE (cond.value);
4511 cond.value = TREE_OPERAND (cond.value, 0);
4513 /* Make sure first operand is calculated only once. */
4514 exp1.value = c_save_expr (default_conversion (cond.value));
4516 exp1.value = build1 (EXCESS_PRECISION_EXPR, eptype, exp1.value);
4517 exp1.original_type = NULL;
4518 cond.value = c_objc_common_truthvalue_conversion (cond_loc, exp1.value);
4519 skip_evaluation += cond.value == truthvalue_true_node;
4524 = c_objc_common_truthvalue_conversion
4525 (cond_loc, default_conversion (cond.value));
4526 skip_evaluation += cond.value == truthvalue_false_node;
4527 exp1 = c_parser_expression_conv (parser);
4528 skip_evaluation += ((cond.value == truthvalue_true_node)
4529 - (cond.value == truthvalue_false_node));
4531 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
4533 skip_evaluation -= cond.value == truthvalue_true_node;
4534 ret.value = error_mark_node;
4535 ret.original_code = ERROR_MARK;
4536 ret.original_type = NULL;
4539 exp2 = c_parser_conditional_expression (parser, NULL);
4540 exp2 = default_function_array_conversion (exp2);
4541 skip_evaluation -= cond.value == truthvalue_true_node;
4542 ret.value = build_conditional_expr (cond.value,
4543 cond.original_code == C_MAYBE_CONST_EXPR,
4544 exp1.value, exp2.value);
4545 ret.original_code = ERROR_MARK;
4546 if (exp1.value == error_mark_node || exp2.value == error_mark_node)
4547 ret.original_type = NULL;
4552 /* If both sides are enum type, the default conversion will have
4553 made the type of the result be an integer type. We want to
4554 remember the enum types we started with. */
4555 t1 = exp1.original_type ? exp1.original_type : TREE_TYPE (exp1.value);
4556 t2 = exp2.original_type ? exp2.original_type : TREE_TYPE (exp2.value);
4557 ret.original_type = ((t1 != error_mark_node
4558 && t2 != error_mark_node
4559 && (TYPE_MAIN_VARIANT (t1)
4560 == TYPE_MAIN_VARIANT (t2)))
4567 /* Parse a binary expression; that is, a logical-OR-expression (C90
4568 6.3.5-6.3.14, C99 6.5.5-6.5.14). If AFTER is not NULL then it is
4569 an Objective-C message expression which is the primary-expression
4570 starting the expression as an initializer.
4572 multiplicative-expression:
4574 multiplicative-expression * cast-expression
4575 multiplicative-expression / cast-expression
4576 multiplicative-expression % cast-expression
4578 additive-expression:
4579 multiplicative-expression
4580 additive-expression + multiplicative-expression
4581 additive-expression - multiplicative-expression
4585 shift-expression << additive-expression
4586 shift-expression >> additive-expression
4588 relational-expression:
4590 relational-expression < shift-expression
4591 relational-expression > shift-expression
4592 relational-expression <= shift-expression
4593 relational-expression >= shift-expression
4595 equality-expression:
4596 relational-expression
4597 equality-expression == relational-expression
4598 equality-expression != relational-expression
4602 AND-expression & equality-expression
4604 exclusive-OR-expression:
4606 exclusive-OR-expression ^ AND-expression
4608 inclusive-OR-expression:
4609 exclusive-OR-expression
4610 inclusive-OR-expression | exclusive-OR-expression
4612 logical-AND-expression:
4613 inclusive-OR-expression
4614 logical-AND-expression && inclusive-OR-expression
4616 logical-OR-expression:
4617 logical-AND-expression
4618 logical-OR-expression || logical-AND-expression
4621 static struct c_expr
4622 c_parser_binary_expression (c_parser *parser, struct c_expr *after)
4624 /* A binary expression is parsed using operator-precedence parsing,
4625 with the operands being cast expressions. All the binary
4626 operators are left-associative. Thus a binary expression is of
4629 E0 op1 E1 op2 E2 ...
4631 which we represent on a stack. On the stack, the precedence
4632 levels are strictly increasing. When a new operator is
4633 encountered of higher precedence than that at the top of the
4634 stack, it is pushed; its LHS is the top expression, and its RHS
4635 is everything parsed until it is popped. When a new operator is
4636 encountered with precedence less than or equal to that at the top
4637 of the stack, triples E[i-1] op[i] E[i] are popped and replaced
4638 by the result of the operation until the operator at the top of
4639 the stack has lower precedence than the new operator or there is
4640 only one element on the stack; then the top expression is the LHS
4641 of the new operator. In the case of logical AND and OR
4642 expressions, we also need to adjust skip_evaluation as
4643 appropriate when the operators are pushed and popped. */
4645 /* The precedence levels, where 0 is a dummy lowest level used for
4646 the bottom of the stack. */
4662 /* The expression at this stack level. */
4664 /* The precedence of the operator on its left, PREC_NONE at the
4665 bottom of the stack. */
4667 /* The operation on its left. */
4669 /* The source location of this operation. */
4673 /* Location of the binary operator. */
4674 location_t binary_loc = UNKNOWN_LOCATION; /* Quiet warning. */
4677 switch (stack[sp].op) \
4679 case TRUTH_ANDIF_EXPR: \
4680 skip_evaluation -= stack[sp - 1].expr.value == truthvalue_false_node; \
4682 case TRUTH_ORIF_EXPR: \
4683 skip_evaluation -= stack[sp - 1].expr.value == truthvalue_true_node; \
4688 stack[sp - 1].expr \
4689 = default_function_array_conversion (stack[sp - 1].expr); \
4691 = default_function_array_conversion (stack[sp].expr); \
4692 stack[sp - 1].expr = parser_build_binary_op (stack[sp].loc, \
4694 stack[sp - 1].expr, \
4698 gcc_assert (!after || c_dialect_objc ());
4699 stack[0].loc = c_parser_peek_token (parser)->location;
4700 stack[0].expr = c_parser_cast_expression (parser, after);
4701 stack[0].prec = PREC_NONE;
4706 enum tree_code ocode;
4709 switch (c_parser_peek_token (parser)->type)
4717 ocode = TRUNC_DIV_EXPR;
4721 ocode = TRUNC_MOD_EXPR;
4733 ocode = LSHIFT_EXPR;
4737 ocode = RSHIFT_EXPR;
4751 case CPP_GREATER_EQ:
4764 oprec = PREC_BITAND;
4765 ocode = BIT_AND_EXPR;
4768 oprec = PREC_BITXOR;
4769 ocode = BIT_XOR_EXPR;
4773 ocode = BIT_IOR_EXPR;
4776 oprec = PREC_LOGAND;
4777 ocode = TRUTH_ANDIF_EXPR;
4781 ocode = TRUTH_ORIF_EXPR;
4784 /* Not a binary operator, so end of the binary
4788 binary_loc = c_parser_peek_token (parser)->location;
4789 c_parser_consume_token (parser);
4790 while (oprec <= stack[sp].prec)
4794 case TRUTH_ANDIF_EXPR:
4796 = default_function_array_conversion (stack[sp].expr);
4797 stack[sp].expr.value = c_objc_common_truthvalue_conversion
4798 (stack[sp].loc, default_conversion (stack[sp].expr.value));
4799 skip_evaluation += stack[sp].expr.value == truthvalue_false_node;
4801 case TRUTH_ORIF_EXPR:
4803 = default_function_array_conversion (stack[sp].expr);
4804 stack[sp].expr.value = c_objc_common_truthvalue_conversion
4805 (stack[sp].loc, default_conversion (stack[sp].expr.value));
4806 skip_evaluation += stack[sp].expr.value == truthvalue_true_node;
4812 stack[sp].loc = binary_loc;
4813 stack[sp].expr = c_parser_cast_expression (parser, NULL);
4814 stack[sp].prec = oprec;
4815 stack[sp].op = ocode;
4820 return stack[0].expr;
4824 /* Parse a cast expression (C90 6.3.4, C99 6.5.4). If AFTER is not
4825 NULL then it is an Objective-C message expression which is the
4826 primary-expression starting the expression as an initializer.
4830 ( type-name ) unary-expression
4833 static struct c_expr
4834 c_parser_cast_expression (c_parser *parser, struct c_expr *after)
4836 gcc_assert (!after || c_dialect_objc ());
4838 return c_parser_postfix_expression_after_primary (parser, *after);
4839 /* If the expression begins with a parenthesized type name, it may
4840 be either a cast or a compound literal; we need to see whether
4841 the next character is '{' to tell the difference. If not, it is
4842 an unary expression. */
4843 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
4844 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
4846 struct c_type_name *type_name;
4849 c_parser_consume_token (parser);
4850 type_name = c_parser_type_name (parser);
4851 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
4852 if (type_name == NULL)
4854 ret.value = error_mark_node;
4855 ret.original_code = ERROR_MARK;
4856 ret.original_type = NULL;
4860 /* Save casted types in the function's used types hash table. */
4861 used_types_insert (type_name->specs->type);
4863 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
4864 return c_parser_postfix_expression_after_paren_type (parser,
4866 expr = c_parser_cast_expression (parser, NULL);
4867 expr = default_function_array_conversion (expr);
4868 ret.value = c_cast_expr (type_name, expr.value);
4869 ret.original_code = ERROR_MARK;
4870 ret.original_type = NULL;
4874 return c_parser_unary_expression (parser);
4877 /* Parse an unary expression (C90 6.3.3, C99 6.5.3).
4883 unary-operator cast-expression
4884 sizeof unary-expression
4885 sizeof ( type-name )
4887 unary-operator: one of
4893 __alignof__ unary-expression
4894 __alignof__ ( type-name )
4897 unary-operator: one of
4898 __extension__ __real__ __imag__
4900 In addition, the GNU syntax treats ++ and -- as unary operators, so
4901 they may be applied to cast expressions with errors for non-lvalues
4904 static struct c_expr
4905 c_parser_unary_expression (c_parser *parser)
4908 struct c_expr ret, op;
4909 location_t loc = c_parser_peek_token (parser)->location;
4910 ret.original_code = ERROR_MARK;
4911 ret.original_type = NULL;
4912 switch (c_parser_peek_token (parser)->type)
4915 c_parser_consume_token (parser);
4916 op = c_parser_cast_expression (parser, NULL);
4917 op = default_function_array_conversion (op);
4918 return parser_build_unary_op (PREINCREMENT_EXPR, op, loc);
4919 case CPP_MINUS_MINUS:
4920 c_parser_consume_token (parser);
4921 op = c_parser_cast_expression (parser, NULL);
4922 op = default_function_array_conversion (op);
4923 return parser_build_unary_op (PREDECREMENT_EXPR, op, loc);
4925 c_parser_consume_token (parser);
4926 return parser_build_unary_op (ADDR_EXPR,
4927 c_parser_cast_expression (parser, NULL),
4930 c_parser_consume_token (parser);
4931 op = c_parser_cast_expression (parser, NULL);
4932 op = default_function_array_conversion (op);
4933 ret.value = build_indirect_ref (loc, op.value, "unary *");
4936 if (!c_dialect_objc () && !in_system_header)
4937 warning_at (c_parser_peek_token (parser)->location,
4939 "traditional C rejects the unary plus operator");
4940 c_parser_consume_token (parser);
4941 op = c_parser_cast_expression (parser, NULL);
4942 op = default_function_array_conversion (op);
4943 return parser_build_unary_op (CONVERT_EXPR, op, loc);
4945 c_parser_consume_token (parser);
4946 op = c_parser_cast_expression (parser, NULL);
4947 op = default_function_array_conversion (op);
4948 return parser_build_unary_op (NEGATE_EXPR, op, loc);
4950 c_parser_consume_token (parser);
4951 op = c_parser_cast_expression (parser, NULL);
4952 op = default_function_array_conversion (op);
4953 return parser_build_unary_op (BIT_NOT_EXPR, op, loc);
4955 c_parser_consume_token (parser);
4956 op = c_parser_cast_expression (parser, NULL);
4957 op = default_function_array_conversion (op);
4958 return parser_build_unary_op (TRUTH_NOT_EXPR, op, loc);
4960 /* Refer to the address of a label as a pointer. */
4961 c_parser_consume_token (parser);
4962 if (c_parser_next_token_is (parser, CPP_NAME))
4964 ret.value = finish_label_address_expr
4965 (c_parser_peek_token (parser)->value, loc);
4966 c_parser_consume_token (parser);
4970 c_parser_error (parser, "expected identifier");
4971 ret.value = error_mark_node;
4975 switch (c_parser_peek_token (parser)->keyword)
4978 return c_parser_sizeof_expression (parser);
4980 return c_parser_alignof_expression (parser);
4982 c_parser_consume_token (parser);
4983 ext = disable_extension_diagnostics ();
4984 ret = c_parser_cast_expression (parser, NULL);
4985 restore_extension_diagnostics (ext);
4988 c_parser_consume_token (parser);
4989 op = c_parser_cast_expression (parser, NULL);
4990 op = default_function_array_conversion (op);
4991 return parser_build_unary_op (REALPART_EXPR, op, loc);
4993 c_parser_consume_token (parser);
4994 op = c_parser_cast_expression (parser, NULL);
4995 op = default_function_array_conversion (op);
4996 return parser_build_unary_op (IMAGPART_EXPR, op, loc);
4998 return c_parser_postfix_expression (parser);
5001 return c_parser_postfix_expression (parser);
5005 /* Parse a sizeof expression. */
5007 static struct c_expr
5008 c_parser_sizeof_expression (c_parser *parser)
5011 location_t expr_loc;
5012 gcc_assert (c_parser_next_token_is_keyword (parser, RID_SIZEOF));
5013 c_parser_consume_token (parser);
5016 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
5017 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
5019 /* Either sizeof ( type-name ) or sizeof unary-expression
5020 starting with a compound literal. */
5021 struct c_type_name *type_name;
5022 c_parser_consume_token (parser);
5023 expr_loc = c_parser_peek_token (parser)->location;
5024 type_name = c_parser_type_name (parser);
5025 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
5026 if (type_name == NULL)
5031 ret.value = error_mark_node;
5032 ret.original_code = ERROR_MARK;
5033 ret.original_type = NULL;
5036 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
5038 expr = c_parser_postfix_expression_after_paren_type (parser,
5042 /* sizeof ( type-name ). */
5045 return c_expr_sizeof_type (type_name);
5049 expr_loc = c_parser_peek_token (parser)->location;
5050 expr = c_parser_unary_expression (parser);
5054 if (TREE_CODE (expr.value) == COMPONENT_REF
5055 && DECL_C_BIT_FIELD (TREE_OPERAND (expr.value, 1)))
5056 error_at (expr_loc, "%<sizeof%> applied to a bit-field");
5057 return c_expr_sizeof_expr (expr);
5061 /* Parse an alignof expression. */
5063 static struct c_expr
5064 c_parser_alignof_expression (c_parser *parser)
5067 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ALIGNOF));
5068 c_parser_consume_token (parser);
5071 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
5072 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
5074 /* Either __alignof__ ( type-name ) or __alignof__
5075 unary-expression starting with a compound literal. */
5076 struct c_type_name *type_name;
5078 c_parser_consume_token (parser);
5079 type_name = c_parser_type_name (parser);
5080 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
5081 if (type_name == NULL)
5086 ret.value = error_mark_node;
5087 ret.original_code = ERROR_MARK;
5088 ret.original_type = NULL;
5091 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
5093 expr = c_parser_postfix_expression_after_paren_type (parser,
5097 /* alignof ( type-name ). */
5100 ret.value = c_alignof (groktypename (type_name, NULL, NULL));
5101 ret.original_code = ERROR_MARK;
5102 ret.original_type = NULL;
5108 expr = c_parser_unary_expression (parser);
5112 ret.value = c_alignof_expr (expr.value);
5113 ret.original_code = ERROR_MARK;
5114 ret.original_type = NULL;
5119 /* Parse a postfix expression (C90 6.3.1-6.3.2, C99 6.5.1-6.5.2).
5123 postfix-expression [ expression ]
5124 postfix-expression ( argument-expression-list[opt] )
5125 postfix-expression . identifier
5126 postfix-expression -> identifier
5127 postfix-expression ++
5128 postfix-expression --
5129 ( type-name ) { initializer-list }
5130 ( type-name ) { initializer-list , }
5132 argument-expression-list:
5134 argument-expression-list , argument-expression
5146 (treated as a keyword in GNU C)
5149 ( compound-statement )
5150 __builtin_va_arg ( assignment-expression , type-name )
5151 __builtin_offsetof ( type-name , offsetof-member-designator )
5152 __builtin_choose_expr ( assignment-expression ,
5153 assignment-expression ,
5154 assignment-expression )
5155 __builtin_types_compatible_p ( type-name , type-name )
5157 offsetof-member-designator:
5159 offsetof-member-designator . identifier
5160 offsetof-member-designator [ expression ]
5165 [ objc-receiver objc-message-args ]
5166 @selector ( objc-selector-arg )
5167 @protocol ( identifier )
5168 @encode ( type-name )
5172 static struct c_expr
5173 c_parser_postfix_expression (c_parser *parser)
5175 struct c_expr expr, e1, e2, e3;
5176 struct c_type_name *t1, *t2;
5178 expr.original_code = ERROR_MARK;
5179 expr.original_type = NULL;
5180 switch (c_parser_peek_token (parser)->type)
5183 expr.value = c_parser_peek_token (parser)->value;
5184 loc = c_parser_peek_token (parser)->location;
5185 c_parser_consume_token (parser);
5186 if (TREE_CODE (expr.value) == FIXED_CST
5187 && !targetm.fixed_point_supported_p ())
5189 error_at (loc, "fixed-point types not supported for this target");
5190 expr.value = error_mark_node;
5197 expr.value = c_parser_peek_token (parser)->value;
5198 c_parser_consume_token (parser);
5204 expr.value = c_parser_peek_token (parser)->value;
5205 expr.original_code = STRING_CST;
5206 c_parser_consume_token (parser);
5208 case CPP_OBJC_STRING:
5209 gcc_assert (c_dialect_objc ());
5211 = objc_build_string_object (c_parser_peek_token (parser)->value);
5212 c_parser_consume_token (parser);
5215 if (c_parser_peek_token (parser)->id_kind != C_ID_ID)
5217 c_parser_error (parser, "expected expression");
5218 expr.value = error_mark_node;
5222 tree id = c_parser_peek_token (parser)->value;
5223 location_t loc = c_parser_peek_token (parser)->location;
5224 c_parser_consume_token (parser);
5225 expr.value = build_external_ref (id,
5226 (c_parser_peek_token (parser)->type
5227 == CPP_OPEN_PAREN), loc,
5228 &expr.original_type);
5231 case CPP_OPEN_PAREN:
5232 /* A parenthesized expression, statement expression or compound
5234 if (c_parser_peek_2nd_token (parser)->type == CPP_OPEN_BRACE)
5236 /* A statement expression. */
5238 location_t here = c_parser_peek_token (parser)->location;
5239 c_parser_consume_token (parser);
5240 c_parser_consume_token (parser);
5241 if (cur_stmt_list == NULL)
5243 error_at (here, "braced-group within expression allowed "
5244 "only inside a function");
5245 parser->error = true;
5246 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
5247 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5248 expr.value = error_mark_node;
5251 stmt = c_begin_stmt_expr ();
5252 c_parser_compound_statement_nostart (parser);
5253 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5255 pedwarn (here, OPT_pedantic,
5256 "ISO C forbids braced-groups within expressions");
5257 expr.value = c_finish_stmt_expr (stmt);
5259 else if (c_token_starts_typename (c_parser_peek_2nd_token (parser)))
5261 /* A compound literal. ??? Can we actually get here rather
5262 than going directly to
5263 c_parser_postfix_expression_after_paren_type from
5265 struct c_type_name *type_name;
5266 c_parser_consume_token (parser);
5267 type_name = c_parser_type_name (parser);
5268 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5270 if (type_name == NULL)
5272 expr.value = error_mark_node;
5275 expr = c_parser_postfix_expression_after_paren_type (parser,
5280 /* A parenthesized expression. */
5281 c_parser_consume_token (parser);
5282 expr = c_parser_expression (parser);
5283 if (TREE_CODE (expr.value) == MODIFY_EXPR)
5284 TREE_NO_WARNING (expr.value) = 1;
5285 if (expr.original_code != C_MAYBE_CONST_EXPR)
5286 expr.original_code = ERROR_MARK;
5287 /* Don't change EXPR.ORIGINAL_TYPE. */
5288 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5293 switch (c_parser_peek_token (parser)->keyword)
5295 case RID_FUNCTION_NAME:
5296 case RID_PRETTY_FUNCTION_NAME:
5297 case RID_C99_FUNCTION_NAME:
5298 expr.value = fname_decl (c_parser_peek_token (parser)->location,
5299 c_parser_peek_token (parser)->keyword,
5300 c_parser_peek_token (parser)->value);
5301 c_parser_consume_token (parser);
5304 c_parser_consume_token (parser);
5305 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5307 expr.value = error_mark_node;
5310 e1 = c_parser_expr_no_commas (parser, NULL);
5311 e1.value = c_fully_fold (e1.value, false, NULL);
5312 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
5314 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5315 expr.value = error_mark_node;
5318 t1 = c_parser_type_name (parser);
5319 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5323 expr.value = error_mark_node;
5327 tree type_expr = NULL_TREE;
5328 expr.value = build_va_arg (e1.value, groktypename (t1,
5333 expr.value = build2 (C_MAYBE_CONST_EXPR,
5334 TREE_TYPE (expr.value), type_expr,
5336 C_MAYBE_CONST_EXPR_NON_CONST (expr.value) = true;
5341 c_parser_consume_token (parser);
5342 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5344 expr.value = error_mark_node;
5347 t1 = c_parser_type_name (parser);
5350 expr.value = error_mark_node;
5353 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
5355 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5356 expr.value = error_mark_node;
5360 tree type = groktypename (t1, NULL, NULL);
5362 if (type == error_mark_node)
5363 offsetof_ref = error_mark_node;
5365 offsetof_ref = build1 (INDIRECT_REF, type, null_pointer_node);
5366 /* Parse the second argument to __builtin_offsetof. We
5367 must have one identifier, and beyond that we want to
5368 accept sub structure and sub array references. */
5369 if (c_parser_next_token_is (parser, CPP_NAME))
5371 offsetof_ref = build_component_ref
5372 (offsetof_ref, c_parser_peek_token (parser)->value);
5373 c_parser_consume_token (parser);
5374 while (c_parser_next_token_is (parser, CPP_DOT)
5375 || c_parser_next_token_is (parser,
5377 || c_parser_next_token_is (parser,
5380 if (c_parser_next_token_is (parser, CPP_DEREF))
5382 loc = c_parser_peek_token (parser)->location;
5383 offsetof_ref = build_array_ref (offsetof_ref,
5388 else if (c_parser_next_token_is (parser, CPP_DOT))
5391 c_parser_consume_token (parser);
5392 if (c_parser_next_token_is_not (parser,
5395 c_parser_error (parser, "expected identifier");
5398 offsetof_ref = build_component_ref
5400 c_parser_peek_token (parser)->value);
5401 c_parser_consume_token (parser);
5406 loc = c_parser_peek_token (parser)->location;
5407 c_parser_consume_token (parser);
5408 idx = c_parser_expression (parser).value;
5409 idx = c_fully_fold (idx, false, NULL);
5410 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
5412 offsetof_ref = build_array_ref (offsetof_ref, idx, loc);
5417 c_parser_error (parser, "expected identifier");
5418 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5420 expr.value = fold_offsetof (offsetof_ref, NULL_TREE);
5423 case RID_CHOOSE_EXPR:
5424 c_parser_consume_token (parser);
5425 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5427 expr.value = error_mark_node;
5430 loc = c_parser_peek_token (parser)->location;
5431 e1 = c_parser_expr_no_commas (parser, NULL);
5432 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
5434 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5435 expr.value = error_mark_node;
5438 e2 = c_parser_expr_no_commas (parser, NULL);
5439 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
5441 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5442 expr.value = error_mark_node;
5445 e3 = c_parser_expr_no_commas (parser, NULL);
5446 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5452 if (TREE_CODE (c) != INTEGER_CST
5453 || !INTEGRAL_TYPE_P (TREE_TYPE (c)))
5455 "first argument to %<__builtin_choose_expr%> not"
5457 constant_expression_warning (c);
5458 expr = integer_zerop (c) ? e3 : e2;
5461 case RID_TYPES_COMPATIBLE_P:
5462 c_parser_consume_token (parser);
5463 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5465 expr.value = error_mark_node;
5468 t1 = c_parser_type_name (parser);
5471 expr.value = error_mark_node;
5474 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
5476 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5477 expr.value = error_mark_node;
5480 t2 = c_parser_type_name (parser);
5483 expr.value = error_mark_node;
5486 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5491 e1 = TYPE_MAIN_VARIANT (groktypename (t1, NULL, NULL));
5492 e2 = TYPE_MAIN_VARIANT (groktypename (t2, NULL, NULL));
5494 expr.value = comptypes (e1, e2)
5495 ? build_int_cst (NULL_TREE, 1)
5496 : build_int_cst (NULL_TREE, 0);
5499 case RID_AT_SELECTOR:
5500 gcc_assert (c_dialect_objc ());
5501 c_parser_consume_token (parser);
5502 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5504 expr.value = error_mark_node;
5508 tree sel = c_parser_objc_selector_arg (parser);
5509 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5511 expr.value = objc_build_selector_expr (sel);
5514 case RID_AT_PROTOCOL:
5515 gcc_assert (c_dialect_objc ());
5516 c_parser_consume_token (parser);
5517 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5519 expr.value = error_mark_node;
5522 if (c_parser_next_token_is_not (parser, CPP_NAME))
5524 c_parser_error (parser, "expected identifier");
5525 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5526 expr.value = error_mark_node;
5530 tree id = c_parser_peek_token (parser)->value;
5531 c_parser_consume_token (parser);
5532 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5534 expr.value = objc_build_protocol_expr (id);
5538 /* Extension to support C-structures in the archiver. */
5539 gcc_assert (c_dialect_objc ());
5540 c_parser_consume_token (parser);
5541 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5543 expr.value = error_mark_node;
5546 t1 = c_parser_type_name (parser);
5549 expr.value = error_mark_node;
5550 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5553 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5556 tree type = groktypename (t1, NULL, NULL);
5557 expr.value = objc_build_encode_expr (type);
5561 c_parser_error (parser, "expected expression");
5562 expr.value = error_mark_node;
5566 case CPP_OPEN_SQUARE:
5567 if (c_dialect_objc ())
5569 tree receiver, args;
5570 c_parser_consume_token (parser);
5571 receiver = c_parser_objc_receiver (parser);
5572 args = c_parser_objc_message_args (parser);
5573 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
5575 expr.value = objc_build_message_expr (build_tree_list (receiver,
5579 /* Else fall through to report error. */
5581 c_parser_error (parser, "expected expression");
5582 expr.value = error_mark_node;
5585 return c_parser_postfix_expression_after_primary (parser, expr);
5588 /* Parse a postfix expression after a parenthesized type name: the
5589 brace-enclosed initializer of a compound literal, possibly followed
5590 by some postfix operators. This is separate because it is not
5591 possible to tell until after the type name whether a cast
5592 expression has a cast or a compound literal, or whether the operand
5593 of sizeof is a parenthesized type name or starts with a compound
5596 static struct c_expr
5597 c_parser_postfix_expression_after_paren_type (c_parser *parser,
5598 struct c_type_name *type_name)
5604 location_t start_loc;
5605 tree type_expr = NULL_TREE;
5606 bool type_expr_const = true;
5607 start_init (NULL_TREE, NULL, 0);
5608 type = groktypename (type_name, &type_expr, &type_expr_const);
5609 start_loc = c_parser_peek_token (parser)->location;
5610 if (type != error_mark_node && C_TYPE_VARIABLE_SIZE (type))
5612 error_at (start_loc, "compound literal has variable size");
5613 type = error_mark_node;
5615 init = c_parser_braced_init (parser, type, false);
5617 maybe_warn_string_init (type, init);
5620 pedwarn (start_loc, OPT_pedantic, "ISO C90 forbids compound literals");
5621 non_const = ((init.value && TREE_CODE (init.value) == CONSTRUCTOR)
5622 ? CONSTRUCTOR_NON_CONST (init.value)
5623 : init.original_code == C_MAYBE_CONST_EXPR);
5624 non_const |= !type_expr_const;
5625 expr.value = build_compound_literal (type, init.value, non_const);
5626 expr.original_code = ERROR_MARK;
5627 expr.original_type = NULL;
5630 if (TREE_CODE (expr.value) == C_MAYBE_CONST_EXPR)
5632 gcc_assert (C_MAYBE_CONST_EXPR_PRE (expr.value) == NULL_TREE);
5633 C_MAYBE_CONST_EXPR_PRE (expr.value) = type_expr;
5637 gcc_assert (!non_const);
5638 expr.value = build2 (C_MAYBE_CONST_EXPR, type,
5639 type_expr, expr.value);
5642 return c_parser_postfix_expression_after_primary (parser, expr);
5645 /* Parse a postfix expression after the initial primary or compound
5646 literal; that is, parse a series of postfix operators. */
5648 static struct c_expr
5649 c_parser_postfix_expression_after_primary (c_parser *parser,
5652 struct c_expr orig_expr;
5654 VEC(tree,gc) *exprlist;
5655 VEC(tree,gc) *origtypes;
5656 location_t loc = c_parser_peek_token (parser)->location;
5659 switch (c_parser_peek_token (parser)->type)
5661 case CPP_OPEN_SQUARE:
5662 /* Array reference. */
5663 loc = c_parser_peek_token (parser)->location;
5664 c_parser_consume_token (parser);
5665 idx = c_parser_expression (parser).value;
5666 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
5668 expr.value = build_array_ref (expr.value, idx, loc);
5669 expr.original_code = ERROR_MARK;
5670 expr.original_type = NULL;
5672 case CPP_OPEN_PAREN:
5673 /* Function call. */
5674 c_parser_consume_token (parser);
5675 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
5678 exprlist = c_parser_expr_list (parser, true, false, &origtypes);
5679 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5682 expr.value = build_function_call_vec (expr.value, exprlist,
5684 expr.original_code = ERROR_MARK;
5685 if (TREE_CODE (expr.value) == INTEGER_CST
5686 && TREE_CODE (orig_expr.value) == FUNCTION_DECL
5687 && DECL_BUILT_IN_CLASS (orig_expr.value) == BUILT_IN_NORMAL
5688 && DECL_FUNCTION_CODE (orig_expr.value) == BUILT_IN_CONSTANT_P)
5689 expr.original_code = C_MAYBE_CONST_EXPR;
5690 expr.original_type = NULL;
5691 if (exprlist != NULL)
5693 c_parser_release_expr_list (exprlist);
5694 c_parser_release_expr_list (origtypes);
5698 /* Structure element reference. */
5699 c_parser_consume_token (parser);
5700 expr = default_function_array_conversion (expr);
5701 if (c_parser_next_token_is (parser, CPP_NAME))
5702 ident = c_parser_peek_token (parser)->value;
5705 c_parser_error (parser, "expected identifier");
5706 expr.value = error_mark_node;
5707 expr.original_code = ERROR_MARK;
5708 expr.original_type = NULL;
5711 c_parser_consume_token (parser);
5712 expr.value = build_component_ref (expr.value, ident);
5713 expr.original_code = ERROR_MARK;
5714 if (TREE_CODE (expr.value) != COMPONENT_REF)
5715 expr.original_type = NULL;
5718 /* Remember the original type of a bitfield. */
5719 tree field = TREE_OPERAND (expr.value, 1);
5720 if (TREE_CODE (field) != FIELD_DECL)
5721 expr.original_type = NULL;
5723 expr.original_type = DECL_BIT_FIELD_TYPE (field);
5727 /* Structure element reference. */
5728 c_parser_consume_token (parser);
5729 expr = default_function_array_conversion (expr);
5730 if (c_parser_next_token_is (parser, CPP_NAME))
5731 ident = c_parser_peek_token (parser)->value;
5734 c_parser_error (parser, "expected identifier");
5735 expr.value = error_mark_node;
5736 expr.original_code = ERROR_MARK;
5737 expr.original_type = NULL;
5740 c_parser_consume_token (parser);
5741 expr.value = build_component_ref (build_indirect_ref (loc,
5745 expr.original_code = ERROR_MARK;
5746 if (TREE_CODE (expr.value) != COMPONENT_REF)
5747 expr.original_type = NULL;
5750 /* Remember the original type of a bitfield. */
5751 tree field = TREE_OPERAND (expr.value, 1);
5752 if (TREE_CODE (field) != FIELD_DECL)
5753 expr.original_type = NULL;
5755 expr.original_type = DECL_BIT_FIELD_TYPE (field);
5759 /* Postincrement. */
5760 c_parser_consume_token (parser);
5761 expr = default_function_array_conversion (expr);
5762 expr.value = build_unary_op (loc,
5763 POSTINCREMENT_EXPR, expr.value, 0);
5764 expr.original_code = ERROR_MARK;
5765 expr.original_type = NULL;
5767 case CPP_MINUS_MINUS:
5768 /* Postdecrement. */
5769 c_parser_consume_token (parser);
5770 expr = default_function_array_conversion (expr);
5771 expr.value = build_unary_op (loc,
5772 POSTDECREMENT_EXPR, expr.value, 0);
5773 expr.original_code = ERROR_MARK;
5774 expr.original_type = NULL;
5782 /* Parse an expression (C90 6.3.17, C99 6.5.17).
5785 assignment-expression
5786 expression , assignment-expression
5789 static struct c_expr
5790 c_parser_expression (c_parser *parser)
5793 expr = c_parser_expr_no_commas (parser, NULL);
5794 while (c_parser_next_token_is (parser, CPP_COMMA))
5797 c_parser_consume_token (parser);
5798 next = c_parser_expr_no_commas (parser, NULL);
5799 next = default_function_array_conversion (next);
5800 expr.value = build_compound_expr (expr.value, next.value);
5801 expr.original_code = COMPOUND_EXPR;
5802 expr.original_type = next.original_type;
5807 /* Parse an expression and convert functions or arrays to
5810 static struct c_expr
5811 c_parser_expression_conv (c_parser *parser)
5814 expr = c_parser_expression (parser);
5815 expr = default_function_array_conversion (expr);
5819 /* Parse a non-empty list of expressions. If CONVERT_P, convert
5820 functions and arrays to pointers. If FOLD_P, fold the expressions.
5823 assignment-expression
5824 nonempty-expr-list , assignment-expression
5827 /* We cache two vectors, to save most allocation and deallocation. */
5828 static GTY((deletable)) VEC(tree,gc) *cached_expr_list_1;
5829 static GTY((deletable)) VEC(tree,gc) *cached_expr_list_2;
5831 static VEC(tree,gc) *
5832 c_parser_expr_list (c_parser *parser, bool convert_p, bool fold_p,
5833 VEC(tree,gc) **p_orig_types)
5836 VEC(tree,gc) *orig_types;
5839 if (cached_expr_list_1 != NULL)
5841 ret = cached_expr_list_1;
5842 cached_expr_list_1 = NULL;
5843 VEC_truncate (tree, ret, 0);
5845 else if (cached_expr_list_2 != NULL)
5847 ret = cached_expr_list_2;
5848 cached_expr_list_2 = NULL;
5849 VEC_truncate (tree, ret, 0);
5852 ret = VEC_alloc (tree, gc, 16);
5854 if (p_orig_types == NULL)
5858 if (cached_expr_list_2 != NULL)
5860 orig_types = cached_expr_list_2;
5861 cached_expr_list_2 = NULL;
5862 VEC_truncate (tree, orig_types, 0);
5865 orig_types = VEC_alloc (tree, gc, 16);
5868 expr = c_parser_expr_no_commas (parser, NULL);
5870 expr = default_function_array_conversion (expr);
5872 expr.value = c_fully_fold (expr.value, false, NULL);
5873 VEC_quick_push (tree, ret, expr.value);
5874 if (orig_types != NULL)
5875 VEC_quick_push (tree, orig_types, expr.original_type);
5876 while (c_parser_next_token_is (parser, CPP_COMMA))
5878 c_parser_consume_token (parser);
5879 expr = c_parser_expr_no_commas (parser, NULL);
5881 expr = default_function_array_conversion (expr);
5883 expr.value = c_fully_fold (expr.value, false, NULL);
5884 VEC_safe_push (tree, gc, ret, expr.value);
5885 if (orig_types != NULL)
5886 VEC_safe_push (tree, gc, orig_types, expr.original_type);
5888 if (orig_types != NULL)
5889 *p_orig_types = orig_types;
5893 /* Release a vector returned by c_parser_expr_list. */
5896 c_parser_release_expr_list (VEC(tree,gc) *vec)
5898 if (cached_expr_list_1 == NULL)
5899 cached_expr_list_1 = vec;
5900 else if (cached_expr_list_2 == NULL)
5901 cached_expr_list_2 = vec;
5903 VEC_free (tree, gc, vec);
5906 /* Convert a vector, as returned by c_parser_expr_list, to a
5910 c_parser_vec_to_tree_list (VEC(tree,gc) *vec)
5912 tree ret = NULL_TREE;
5916 for (i = 0; VEC_iterate (tree, vec, i, t); ++i)
5918 *pp = build_tree_list (NULL, t);
5919 pp = &TREE_CHAIN (*pp);
5924 /* Parse Objective-C-specific constructs. */
5926 /* Parse an objc-class-definition.
5928 objc-class-definition:
5929 @interface identifier objc-superclass[opt] objc-protocol-refs[opt]
5930 objc-class-instance-variables[opt] objc-methodprotolist @end
5931 @implementation identifier objc-superclass[opt]
5932 objc-class-instance-variables[opt]
5933 @interface identifier ( identifier ) objc-protocol-refs[opt]
5934 objc-methodprotolist @end
5935 @implementation identifier ( identifier )
5940 "@interface identifier (" must start "@interface identifier (
5941 identifier ) ...": objc-methodprotolist in the first production may
5942 not start with a parenthesized identifier as a declarator of a data
5943 definition with no declaration specifiers if the objc-superclass,
5944 objc-protocol-refs and objc-class-instance-variables are omitted. */
5947 c_parser_objc_class_definition (c_parser *parser)
5952 if (c_parser_next_token_is_keyword (parser, RID_AT_INTERFACE))
5954 else if (c_parser_next_token_is_keyword (parser, RID_AT_IMPLEMENTATION))
5958 c_parser_consume_token (parser);
5959 if (c_parser_next_token_is_not (parser, CPP_NAME))
5961 c_parser_error (parser, "expected identifier");
5964 id1 = c_parser_peek_token (parser)->value;
5965 c_parser_consume_token (parser);
5966 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
5969 tree proto = NULL_TREE;
5970 c_parser_consume_token (parser);
5971 if (c_parser_next_token_is_not (parser, CPP_NAME))
5973 c_parser_error (parser, "expected identifier");
5974 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5977 id2 = c_parser_peek_token (parser)->value;
5978 c_parser_consume_token (parser);
5979 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
5982 objc_start_category_implementation (id1, id2);
5985 if (c_parser_next_token_is (parser, CPP_LESS))
5986 proto = c_parser_objc_protocol_refs (parser);
5987 objc_start_category_interface (id1, id2, proto);
5988 c_parser_objc_methodprotolist (parser);
5989 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
5990 objc_finish_interface ();
5993 if (c_parser_next_token_is (parser, CPP_COLON))
5995 c_parser_consume_token (parser);
5996 if (c_parser_next_token_is_not (parser, CPP_NAME))
5998 c_parser_error (parser, "expected identifier");
6001 superclass = c_parser_peek_token (parser)->value;
6002 c_parser_consume_token (parser);
6005 superclass = NULL_TREE;
6008 tree proto = NULL_TREE;
6009 if (c_parser_next_token_is (parser, CPP_LESS))
6010 proto = c_parser_objc_protocol_refs (parser);
6011 objc_start_class_interface (id1, superclass, proto);
6014 objc_start_class_implementation (id1, superclass);
6015 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
6016 c_parser_objc_class_instance_variables (parser);
6019 objc_continue_interface ();
6020 c_parser_objc_methodprotolist (parser);
6021 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
6022 objc_finish_interface ();
6026 objc_continue_implementation ();
6031 /* Parse objc-class-instance-variables.
6033 objc-class-instance-variables:
6034 { objc-instance-variable-decl-list[opt] }
6036 objc-instance-variable-decl-list:
6037 objc-visibility-spec
6038 objc-instance-variable-decl ;
6040 objc-instance-variable-decl-list objc-visibility-spec
6041 objc-instance-variable-decl-list objc-instance-variable-decl ;
6042 objc-instance-variable-decl-list ;
6044 objc-visibility-spec:
6049 objc-instance-variable-decl:
6054 c_parser_objc_class_instance_variables (c_parser *parser)
6056 gcc_assert (c_parser_next_token_is (parser, CPP_OPEN_BRACE));
6057 c_parser_consume_token (parser);
6058 while (c_parser_next_token_is_not (parser, CPP_EOF))
6061 /* Parse any stray semicolon. */
6062 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
6064 pedwarn (c_parser_peek_token (parser)->location, OPT_pedantic,
6065 "extra semicolon in struct or union specified");
6066 c_parser_consume_token (parser);
6069 /* Stop if at the end of the instance variables. */
6070 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
6072 c_parser_consume_token (parser);
6075 /* Parse any objc-visibility-spec. */
6076 if (c_parser_next_token_is_keyword (parser, RID_PRIVATE))
6078 c_parser_consume_token (parser);
6079 objc_set_visibility (2);
6082 else if (c_parser_next_token_is_keyword (parser, RID_PROTECTED))
6084 c_parser_consume_token (parser);
6085 objc_set_visibility (0);
6088 else if (c_parser_next_token_is_keyword (parser, RID_PUBLIC))
6090 c_parser_consume_token (parser);
6091 objc_set_visibility (1);
6094 else if (c_parser_next_token_is (parser, CPP_PRAGMA))
6096 c_parser_pragma (parser, pragma_external);
6100 /* Parse some comma-separated declarations. */
6101 decls = c_parser_struct_declaration (parser);
6103 /* Comma-separated instance variables are chained together in
6104 reverse order; add them one by one. */
6105 tree ivar = nreverse (decls);
6106 for (; ivar; ivar = TREE_CHAIN (ivar))
6107 objc_add_instance_variable (copy_node (ivar));
6109 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
6113 /* Parse an objc-class-declaration.
6115 objc-class-declaration:
6116 @class identifier-list ;
6120 c_parser_objc_class_declaration (c_parser *parser)
6122 tree list = NULL_TREE;
6123 gcc_assert (c_parser_next_token_is_keyword (parser, RID_CLASS));
6124 c_parser_consume_token (parser);
6125 /* Any identifiers, including those declared as type names, are OK
6130 if (c_parser_next_token_is_not (parser, CPP_NAME))
6132 c_parser_error (parser, "expected identifier");
6135 id = c_parser_peek_token (parser)->value;
6136 list = chainon (list, build_tree_list (NULL_TREE, id));
6137 c_parser_consume_token (parser);
6138 if (c_parser_next_token_is (parser, CPP_COMMA))
6139 c_parser_consume_token (parser);
6143 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
6144 objc_declare_class (list);
6147 /* Parse an objc-alias-declaration.
6149 objc-alias-declaration:
6150 @compatibility_alias identifier identifier ;
6154 c_parser_objc_alias_declaration (c_parser *parser)
6157 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_ALIAS));
6158 c_parser_consume_token (parser);
6159 if (c_parser_next_token_is_not (parser, CPP_NAME))
6161 c_parser_error (parser, "expected identifier");
6162 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
6165 id1 = c_parser_peek_token (parser)->value;
6166 c_parser_consume_token (parser);
6167 if (c_parser_next_token_is_not (parser, CPP_NAME))
6169 c_parser_error (parser, "expected identifier");
6170 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
6173 id2 = c_parser_peek_token (parser)->value;
6174 c_parser_consume_token (parser);
6175 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
6176 objc_declare_alias (id1, id2);
6179 /* Parse an objc-protocol-definition.
6181 objc-protocol-definition:
6182 @protocol identifier objc-protocol-refs[opt] objc-methodprotolist @end
6183 @protocol identifier-list ;
6185 "@protocol identifier ;" should be resolved as "@protocol
6186 identifier-list ;": objc-methodprotolist may not start with a
6187 semicolon in the first alternative if objc-protocol-refs are
6191 c_parser_objc_protocol_definition (c_parser *parser)
6193 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_PROTOCOL));
6194 c_parser_consume_token (parser);
6195 if (c_parser_next_token_is_not (parser, CPP_NAME))
6197 c_parser_error (parser, "expected identifier");
6200 if (c_parser_peek_2nd_token (parser)->type == CPP_COMMA
6201 || c_parser_peek_2nd_token (parser)->type == CPP_SEMICOLON)
6203 tree list = NULL_TREE;
6204 /* Any identifiers, including those declared as type names, are
6209 if (c_parser_next_token_is_not (parser, CPP_NAME))
6211 c_parser_error (parser, "expected identifier");
6214 id = c_parser_peek_token (parser)->value;
6215 list = chainon (list, build_tree_list (NULL_TREE, id));
6216 c_parser_consume_token (parser);
6217 if (c_parser_next_token_is (parser, CPP_COMMA))
6218 c_parser_consume_token (parser);
6222 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
6223 objc_declare_protocols (list);
6227 tree id = c_parser_peek_token (parser)->value;
6228 tree proto = NULL_TREE;
6229 c_parser_consume_token (parser);
6230 if (c_parser_next_token_is (parser, CPP_LESS))
6231 proto = c_parser_objc_protocol_refs (parser);
6232 parser->objc_pq_context = true;
6233 objc_start_protocol (id, proto);
6234 c_parser_objc_methodprotolist (parser);
6235 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
6236 parser->objc_pq_context = false;
6237 objc_finish_interface ();
6241 /* Parse an objc-method-type.
6248 static enum tree_code
6249 c_parser_objc_method_type (c_parser *parser)
6251 switch (c_parser_peek_token (parser)->type)
6254 c_parser_consume_token (parser);
6257 c_parser_consume_token (parser);
6264 /* Parse an objc-method-definition.
6266 objc-method-definition:
6267 objc-method-type objc-method-decl ;[opt] compound-statement
6271 c_parser_objc_method_definition (c_parser *parser)
6273 enum tree_code type = c_parser_objc_method_type (parser);
6275 objc_set_method_type (type);
6276 parser->objc_pq_context = true;
6277 decl = c_parser_objc_method_decl (parser);
6278 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
6280 c_parser_consume_token (parser);
6281 pedwarn (c_parser_peek_token (parser)->location, OPT_pedantic,
6282 "extra semicolon in method definition specified");
6284 if (!c_parser_next_token_is (parser, CPP_OPEN_BRACE))
6286 c_parser_error (parser, "expected %<{%>");
6289 parser->objc_pq_context = false;
6290 objc_start_method_definition (decl);
6291 add_stmt (c_parser_compound_statement (parser));
6292 objc_finish_method_definition (current_function_decl);
6295 /* Parse an objc-methodprotolist.
6297 objc-methodprotolist:
6299 objc-methodprotolist objc-methodproto
6300 objc-methodprotolist declaration
6301 objc-methodprotolist ;
6303 The declaration is a data definition, which may be missing
6304 declaration specifiers under the same rules and diagnostics as
6305 other data definitions outside functions, and the stray semicolon
6306 is diagnosed the same way as a stray semicolon outside a
6310 c_parser_objc_methodprotolist (c_parser *parser)
6314 /* The list is terminated by @end. */
6315 switch (c_parser_peek_token (parser)->type)
6318 pedwarn (c_parser_peek_token (parser)->location, OPT_pedantic,
6319 "ISO C does not allow extra %<;%> outside of a function");
6320 c_parser_consume_token (parser);
6324 c_parser_objc_methodproto (parser);
6327 c_parser_pragma (parser, pragma_external);
6332 if (c_parser_next_token_is_keyword (parser, RID_AT_END))
6334 c_parser_declaration_or_fndef (parser, false, true, false, true);
6340 /* Parse an objc-methodproto.
6343 objc-method-type objc-method-decl ;
6347 c_parser_objc_methodproto (c_parser *parser)
6349 enum tree_code type = c_parser_objc_method_type (parser);
6351 objc_set_method_type (type);
6352 /* Remember protocol qualifiers in prototypes. */
6353 parser->objc_pq_context = true;
6354 decl = c_parser_objc_method_decl (parser);
6355 /* Forget protocol qualifiers here. */
6356 parser->objc_pq_context = false;
6357 objc_add_method_declaration (decl);
6358 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
6361 /* Parse an objc-method-decl.
6364 ( objc-type-name ) objc-selector
6366 ( objc-type-name ) objc-keyword-selector objc-optparmlist
6367 objc-keyword-selector objc-optparmlist
6369 objc-keyword-selector:
6371 objc-keyword-selector objc-keyword-decl
6374 objc-selector : ( objc-type-name ) identifier
6375 objc-selector : identifier
6376 : ( objc-type-name ) identifier
6380 objc-optparms objc-optellipsis
6384 objc-opt-parms , parameter-declaration
6392 c_parser_objc_method_decl (c_parser *parser)
6394 tree type = NULL_TREE;
6396 tree parms = NULL_TREE;
6397 bool ellipsis = false;
6399 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
6401 c_parser_consume_token (parser);
6402 type = c_parser_objc_type_name (parser);
6403 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
6405 sel = c_parser_objc_selector (parser);
6406 /* If there is no selector, or a colon follows, we have an
6407 objc-keyword-selector. If there is a selector, and a colon does
6408 not follow, that selector ends the objc-method-decl. */
6409 if (!sel || c_parser_next_token_is (parser, CPP_COLON))
6412 tree list = NULL_TREE;
6415 tree atype = NULL_TREE, id, keyworddecl;
6416 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
6418 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
6420 c_parser_consume_token (parser);
6421 atype = c_parser_objc_type_name (parser);
6422 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
6425 if (c_parser_next_token_is_not (parser, CPP_NAME))
6427 c_parser_error (parser, "expected identifier");
6428 return error_mark_node;
6430 id = c_parser_peek_token (parser)->value;
6431 c_parser_consume_token (parser);
6432 keyworddecl = objc_build_keyword_decl (tsel, atype, id);
6433 list = chainon (list, keyworddecl);
6434 tsel = c_parser_objc_selector (parser);
6435 if (!tsel && c_parser_next_token_is_not (parser, CPP_COLON))
6438 /* Parse the optional parameter list. Optional Objective-C
6439 method parameters follow the C syntax, and may include '...'
6440 to denote a variable number of arguments. */
6441 parms = make_node (TREE_LIST);
6442 while (c_parser_next_token_is (parser, CPP_COMMA))
6444 struct c_parm *parm;
6445 c_parser_consume_token (parser);
6446 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
6449 c_parser_consume_token (parser);
6452 parm = c_parser_parameter_declaration (parser, NULL_TREE);
6455 parms = chainon (parms,
6456 build_tree_list (NULL_TREE, grokparm (parm)));
6460 return objc_build_method_signature (type, sel, parms, ellipsis);
6463 /* Parse an objc-type-name.
6466 objc-type-qualifiers[opt] type-name
6467 objc-type-qualifiers[opt]
6469 objc-type-qualifiers:
6471 objc-type-qualifiers objc-type-qualifier
6473 objc-type-qualifier: one of
6474 in out inout bycopy byref oneway
6478 c_parser_objc_type_name (c_parser *parser)
6480 tree quals = NULL_TREE;
6481 struct c_type_name *type_name = NULL;
6482 tree type = NULL_TREE;
6485 c_token *token = c_parser_peek_token (parser);
6486 if (token->type == CPP_KEYWORD
6487 && (token->keyword == RID_IN
6488 || token->keyword == RID_OUT
6489 || token->keyword == RID_INOUT
6490 || token->keyword == RID_BYCOPY
6491 || token->keyword == RID_BYREF
6492 || token->keyword == RID_ONEWAY))
6494 quals = chainon (quals, build_tree_list (NULL_TREE, token->value));
6495 c_parser_consume_token (parser);
6500 if (c_parser_next_token_starts_typename (parser))
6501 type_name = c_parser_type_name (parser);
6503 type = groktypename (type_name, NULL, NULL);
6504 return build_tree_list (quals, type);
6507 /* Parse objc-protocol-refs.
6514 c_parser_objc_protocol_refs (c_parser *parser)
6516 tree list = NULL_TREE;
6517 gcc_assert (c_parser_next_token_is (parser, CPP_LESS));
6518 c_parser_consume_token (parser);
6519 /* Any identifiers, including those declared as type names, are OK
6524 if (c_parser_next_token_is_not (parser, CPP_NAME))
6526 c_parser_error (parser, "expected identifier");
6529 id = c_parser_peek_token (parser)->value;
6530 list = chainon (list, build_tree_list (NULL_TREE, id));
6531 c_parser_consume_token (parser);
6532 if (c_parser_next_token_is (parser, CPP_COMMA))
6533 c_parser_consume_token (parser);
6537 c_parser_require (parser, CPP_GREATER, "expected %<>%>");
6541 /* Parse an objc-try-catch-statement.
6543 objc-try-catch-statement:
6544 @try compound-statement objc-catch-list[opt]
6545 @try compound-statement objc-catch-list[opt] @finally compound-statement
6548 @catch ( parameter-declaration ) compound-statement
6549 objc-catch-list @catch ( parameter-declaration ) compound-statement
6553 c_parser_objc_try_catch_statement (c_parser *parser)
6557 gcc_assert (c_parser_next_token_is_keyword (parser, RID_TRY));
6558 c_parser_consume_token (parser);
6559 loc = c_parser_peek_token (parser)->location;
6560 stmt = c_parser_compound_statement (parser);
6561 objc_begin_try_stmt (loc, stmt);
6562 while (c_parser_next_token_is_keyword (parser, RID_CATCH))
6564 struct c_parm *parm;
6565 c_parser_consume_token (parser);
6566 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6568 parm = c_parser_parameter_declaration (parser, NULL_TREE);
6571 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6574 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
6575 objc_begin_catch_clause (grokparm (parm));
6576 if (c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
6577 c_parser_compound_statement_nostart (parser);
6578 objc_finish_catch_clause ();
6580 if (c_parser_next_token_is_keyword (parser, RID_AT_FINALLY))
6584 c_parser_consume_token (parser);
6585 finloc = c_parser_peek_token (parser)->location;
6586 finstmt = c_parser_compound_statement (parser);
6587 objc_build_finally_clause (finloc, finstmt);
6589 objc_finish_try_stmt ();
6592 /* Parse an objc-synchronized-statement.
6594 objc-synchronized-statement:
6595 @synchronized ( expression ) compound-statement
6599 c_parser_objc_synchronized_statement (c_parser *parser)
6603 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_SYNCHRONIZED));
6604 c_parser_consume_token (parser);
6605 loc = c_parser_peek_token (parser)->location;
6606 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6608 expr = c_parser_expression (parser).value;
6609 expr = c_fully_fold (expr, false, NULL);
6610 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
6613 expr = error_mark_node;
6614 stmt = c_parser_compound_statement (parser);
6615 objc_build_synchronized (loc, expr, stmt);
6618 /* Parse an objc-selector; return NULL_TREE without an error if the
6619 next token is not an objc-selector.
6624 enum struct union if else while do for switch case default
6625 break continue return goto asm sizeof typeof __alignof
6626 unsigned long const short volatile signed restrict _Complex
6627 in out inout bycopy byref oneway int char float double void _Bool
6629 ??? Why this selection of keywords but not, for example, storage
6630 class specifiers? */
6633 c_parser_objc_selector (c_parser *parser)
6635 c_token *token = c_parser_peek_token (parser);
6636 tree value = token->value;
6637 if (token->type == CPP_NAME)
6639 c_parser_consume_token (parser);
6642 if (token->type != CPP_KEYWORD)
6644 switch (token->keyword)
6685 c_parser_consume_token (parser);
6692 /* Parse an objc-selector-arg.
6696 objc-keywordname-list
6698 objc-keywordname-list:
6700 objc-keywordname-list objc-keywordname
6708 c_parser_objc_selector_arg (c_parser *parser)
6710 tree sel = c_parser_objc_selector (parser);
6711 tree list = NULL_TREE;
6712 if (sel && c_parser_next_token_is_not (parser, CPP_COLON))
6716 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
6718 list = chainon (list, build_tree_list (sel, NULL_TREE));
6719 sel = c_parser_objc_selector (parser);
6720 if (!sel && c_parser_next_token_is_not (parser, CPP_COLON))
6726 /* Parse an objc-receiver.
6735 c_parser_objc_receiver (c_parser *parser)
6737 if (c_parser_peek_token (parser)->type == CPP_NAME
6738 && (c_parser_peek_token (parser)->id_kind == C_ID_TYPENAME
6739 || c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME))
6741 tree id = c_parser_peek_token (parser)->value;
6742 c_parser_consume_token (parser);
6743 return objc_get_class_reference (id);
6745 return c_fully_fold (c_parser_expression (parser).value, false, NULL);
6748 /* Parse objc-message-args.
6752 objc-keywordarg-list
6754 objc-keywordarg-list:
6756 objc-keywordarg-list objc-keywordarg
6759 objc-selector : objc-keywordexpr
6764 c_parser_objc_message_args (c_parser *parser)
6766 tree sel = c_parser_objc_selector (parser);
6767 tree list = NULL_TREE;
6768 if (sel && c_parser_next_token_is_not (parser, CPP_COLON))
6773 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
6775 keywordexpr = c_parser_objc_keywordexpr (parser);
6776 list = chainon (list, build_tree_list (sel, keywordexpr));
6777 sel = c_parser_objc_selector (parser);
6778 if (!sel && c_parser_next_token_is_not (parser, CPP_COLON))
6784 /* Parse an objc-keywordexpr.
6791 c_parser_objc_keywordexpr (c_parser *parser)
6794 VEC(tree,gc) *expr_list = c_parser_expr_list (parser, true, true, NULL);
6795 if (VEC_length (tree, expr_list) == 1)
6797 /* Just return the expression, remove a level of
6799 ret = VEC_index (tree, expr_list, 0);
6803 /* We have a comma expression, we will collapse later. */
6804 ret = c_parser_vec_to_tree_list (expr_list);
6806 c_parser_release_expr_list (expr_list);
6811 /* Handle pragmas. Some OpenMP pragmas are associated with, and therefore
6812 should be considered, statements. ALLOW_STMT is true if we're within
6813 the context of a function and such pragmas are to be allowed. Returns
6814 true if we actually parsed such a pragma. */
6817 c_parser_pragma (c_parser *parser, enum pragma_context context)
6821 id = c_parser_peek_token (parser)->pragma_kind;
6822 gcc_assert (id != PRAGMA_NONE);
6826 case PRAGMA_OMP_BARRIER:
6827 if (context != pragma_compound)
6829 if (context == pragma_stmt)
6830 c_parser_error (parser, "%<#pragma omp barrier%> may only be "
6831 "used in compound statements");
6834 c_parser_omp_barrier (parser);
6837 case PRAGMA_OMP_FLUSH:
6838 if (context != pragma_compound)
6840 if (context == pragma_stmt)
6841 c_parser_error (parser, "%<#pragma omp flush%> may only be "
6842 "used in compound statements");
6845 c_parser_omp_flush (parser);
6848 case PRAGMA_OMP_TASKWAIT:
6849 if (context != pragma_compound)
6851 if (context == pragma_stmt)
6852 c_parser_error (parser, "%<#pragma omp taskwait%> may only be "
6853 "used in compound statements");
6856 c_parser_omp_taskwait (parser);
6859 case PRAGMA_OMP_THREADPRIVATE:
6860 c_parser_omp_threadprivate (parser);
6863 case PRAGMA_OMP_SECTION:
6864 error_at (c_parser_peek_token (parser)->location,
6865 "%<#pragma omp section%> may only be used in "
6866 "%<#pragma omp sections%> construct");
6867 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
6870 case PRAGMA_GCC_PCH_PREPROCESS:
6871 c_parser_error (parser, "%<#pragma GCC pch_preprocess%> must be first");
6872 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
6876 if (id < PRAGMA_FIRST_EXTERNAL)
6878 if (context == pragma_external)
6881 c_parser_error (parser, "expected declaration specifiers");
6882 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
6885 c_parser_omp_construct (parser);
6891 c_parser_consume_pragma (parser);
6892 c_invoke_pragma_handler (id);
6894 /* Skip to EOL, but suppress any error message. Those will have been
6895 generated by the handler routine through calling error, as opposed
6896 to calling c_parser_error. */
6897 parser->error = true;
6898 c_parser_skip_to_pragma_eol (parser);
6903 /* The interface the pragma parsers have to the lexer. */
6906 pragma_lex (tree *value)
6908 c_token *tok = c_parser_peek_token (the_parser);
6909 enum cpp_ttype ret = tok->type;
6911 *value = tok->value;
6912 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
6916 if (ret == CPP_KEYWORD)
6918 c_parser_consume_token (the_parser);
6925 c_parser_pragma_pch_preprocess (c_parser *parser)
6929 c_parser_consume_pragma (parser);
6930 if (c_parser_next_token_is (parser, CPP_STRING))
6932 name = c_parser_peek_token (parser)->value;
6933 c_parser_consume_token (parser);
6936 c_parser_error (parser, "expected string literal");
6937 c_parser_skip_to_pragma_eol (parser);
6940 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
6943 /* OpenMP 2.5 parsing routines. */
6945 /* Returns name of the next clause.
6946 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
6947 the token is not consumed. Otherwise appropriate pragma_omp_clause is
6948 returned and the token is consumed. */
6950 static pragma_omp_clause
6951 c_parser_omp_clause_name (c_parser *parser)
6953 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
6955 if (c_parser_next_token_is_keyword (parser, RID_IF))
6956 result = PRAGMA_OMP_CLAUSE_IF;
6957 else if (c_parser_next_token_is_keyword (parser, RID_DEFAULT))
6958 result = PRAGMA_OMP_CLAUSE_DEFAULT;
6959 else if (c_parser_next_token_is (parser, CPP_NAME))
6961 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
6966 if (!strcmp ("collapse", p))
6967 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
6968 else if (!strcmp ("copyin", p))
6969 result = PRAGMA_OMP_CLAUSE_COPYIN;
6970 else if (!strcmp ("copyprivate", p))
6971 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
6974 if (!strcmp ("firstprivate", p))
6975 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
6978 if (!strcmp ("lastprivate", p))
6979 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
6982 if (!strcmp ("nowait", p))
6983 result = PRAGMA_OMP_CLAUSE_NOWAIT;
6984 else if (!strcmp ("num_threads", p))
6985 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
6988 if (!strcmp ("ordered", p))
6989 result = PRAGMA_OMP_CLAUSE_ORDERED;
6992 if (!strcmp ("private", p))
6993 result = PRAGMA_OMP_CLAUSE_PRIVATE;
6996 if (!strcmp ("reduction", p))
6997 result = PRAGMA_OMP_CLAUSE_REDUCTION;
7000 if (!strcmp ("schedule", p))
7001 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
7002 else if (!strcmp ("shared", p))
7003 result = PRAGMA_OMP_CLAUSE_SHARED;
7006 if (!strcmp ("untied", p))
7007 result = PRAGMA_OMP_CLAUSE_UNTIED;
7012 if (result != PRAGMA_OMP_CLAUSE_NONE)
7013 c_parser_consume_token (parser);
7018 /* Validate that a clause of the given type does not already exist. */
7021 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
7026 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
7027 if (OMP_CLAUSE_CODE (c) == code)
7029 error ("too many %qs clauses", name);
7037 variable-list , identifier
7039 If KIND is nonzero, create the appropriate node and install the decl
7040 in OMP_CLAUSE_DECL and add the node to the head of the list.
7042 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
7043 return the list created. */
7046 c_parser_omp_variable_list (c_parser *parser, enum omp_clause_code kind,
7049 if (c_parser_next_token_is_not (parser, CPP_NAME)
7050 || c_parser_peek_token (parser)->id_kind != C_ID_ID)
7051 c_parser_error (parser, "expected identifier");
7053 while (c_parser_next_token_is (parser, CPP_NAME)
7054 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
7056 tree t = lookup_name (c_parser_peek_token (parser)->value);
7059 undeclared_variable (c_parser_peek_token (parser)->value,
7060 c_parser_peek_token (parser)->location);
7061 else if (t == error_mark_node)
7065 tree u = build_omp_clause (kind);
7066 OMP_CLAUSE_DECL (u) = t;
7067 OMP_CLAUSE_CHAIN (u) = list;
7071 list = tree_cons (t, NULL_TREE, list);
7073 c_parser_consume_token (parser);
7075 if (c_parser_next_token_is_not (parser, CPP_COMMA))
7078 c_parser_consume_token (parser);
7084 /* Similarly, but expect leading and trailing parenthesis. This is a very
7085 common case for omp clauses. */
7088 c_parser_omp_var_list_parens (c_parser *parser, enum omp_clause_code kind,
7091 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7093 list = c_parser_omp_variable_list (parser, kind, list);
7094 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
7100 collapse ( constant-expression ) */
7103 c_parser_omp_clause_collapse (c_parser *parser, tree list)
7105 tree c, num = error_mark_node;
7109 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse");
7111 loc = c_parser_peek_token (parser)->location;
7112 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7114 num = c_parser_expr_no_commas (parser, NULL).value;
7115 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
7117 if (num == error_mark_node)
7119 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
7120 || !host_integerp (num, 0)
7121 || (n = tree_low_cst (num, 0)) <= 0
7125 "collapse argument needs positive constant integer expression");
7128 c = build_omp_clause (OMP_CLAUSE_COLLAPSE);
7129 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
7130 OMP_CLAUSE_CHAIN (c) = list;
7135 copyin ( variable-list ) */
7138 c_parser_omp_clause_copyin (c_parser *parser, tree list)
7140 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_COPYIN, list);
7144 copyprivate ( variable-list ) */
7147 c_parser_omp_clause_copyprivate (c_parser *parser, tree list)
7149 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_COPYPRIVATE, list);
7153 default ( shared | none ) */
7156 c_parser_omp_clause_default (c_parser *parser, tree list)
7158 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
7161 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7163 if (c_parser_next_token_is (parser, CPP_NAME))
7165 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
7170 if (strcmp ("none", p) != 0)
7172 kind = OMP_CLAUSE_DEFAULT_NONE;
7176 if (strcmp ("shared", p) != 0)
7178 kind = OMP_CLAUSE_DEFAULT_SHARED;
7185 c_parser_consume_token (parser);
7190 c_parser_error (parser, "expected %<none%> or %<shared%>");
7192 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
7194 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
7197 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default");
7198 c = build_omp_clause (OMP_CLAUSE_DEFAULT);
7199 OMP_CLAUSE_CHAIN (c) = list;
7200 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
7206 firstprivate ( variable-list ) */
7209 c_parser_omp_clause_firstprivate (c_parser *parser, tree list)
7211 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_FIRSTPRIVATE, list);
7215 if ( expression ) */
7218 c_parser_omp_clause_if (c_parser *parser, tree list)
7220 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
7222 tree t = c_parser_paren_condition (parser);
7225 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if");
7227 c = build_omp_clause (OMP_CLAUSE_IF);
7228 OMP_CLAUSE_IF_EXPR (c) = t;
7229 OMP_CLAUSE_CHAIN (c) = list;
7233 c_parser_error (parser, "expected %<(%>");
7239 lastprivate ( variable-list ) */
7242 c_parser_omp_clause_lastprivate (c_parser *parser, tree list)
7244 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_LASTPRIVATE, list);
7251 c_parser_omp_clause_nowait (c_parser *parser ATTRIBUTE_UNUSED, tree list)
7255 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait");
7257 c = build_omp_clause (OMP_CLAUSE_NOWAIT);
7258 OMP_CLAUSE_CHAIN (c) = list;
7263 num_threads ( expression ) */
7266 c_parser_omp_clause_num_threads (c_parser *parser, tree list)
7268 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7270 location_t expr_loc = c_parser_peek_token (parser)->location;
7271 tree c, t = c_parser_expression (parser).value;
7272 t = c_fully_fold (t, false, NULL);
7274 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
7276 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
7278 c_parser_error (parser, "expected integer expression");
7282 /* Attempt to statically determine when the number isn't positive. */
7283 c = fold_build2 (LE_EXPR, boolean_type_node, t,
7284 build_int_cst (TREE_TYPE (t), 0));
7285 if (c == boolean_true_node)
7287 warning_at (expr_loc, 0,
7288 "%<num_threads%> value must be positive");
7289 t = integer_one_node;
7292 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS, "num_threads");
7294 c = build_omp_clause (OMP_CLAUSE_NUM_THREADS);
7295 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
7296 OMP_CLAUSE_CHAIN (c) = list;
7307 c_parser_omp_clause_ordered (c_parser *parser ATTRIBUTE_UNUSED, tree list)
7311 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED, "ordered");
7313 c = build_omp_clause (OMP_CLAUSE_ORDERED);
7314 OMP_CLAUSE_CHAIN (c) = list;
7319 private ( variable-list ) */
7322 c_parser_omp_clause_private (c_parser *parser, tree list)
7324 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_PRIVATE, list);
7328 reduction ( reduction-operator : variable-list )
7331 One of: + * - & ^ | && || */
7334 c_parser_omp_clause_reduction (c_parser *parser, tree list)
7336 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7338 enum tree_code code;
7340 switch (c_parser_peek_token (parser)->type)
7352 code = BIT_AND_EXPR;
7355 code = BIT_XOR_EXPR;
7358 code = BIT_IOR_EXPR;
7361 code = TRUTH_ANDIF_EXPR;
7364 code = TRUTH_ORIF_EXPR;
7367 c_parser_error (parser,
7368 "expected %<+%>, %<*%>, %<-%>, %<&%>, "
7369 "%<^%>, %<|%>, %<&&%>, or %<||%>");
7370 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 0);
7373 c_parser_consume_token (parser);
7374 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
7378 nl = c_parser_omp_variable_list (parser, OMP_CLAUSE_REDUCTION, list);
7379 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
7380 OMP_CLAUSE_REDUCTION_CODE (c) = code;
7384 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
7390 schedule ( schedule-kind )
7391 schedule ( schedule-kind , expression )
7394 static | dynamic | guided | runtime | auto
7398 c_parser_omp_clause_schedule (c_parser *parser, tree list)
7402 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7405 c = build_omp_clause (OMP_CLAUSE_SCHEDULE);
7407 if (c_parser_next_token_is (parser, CPP_NAME))
7409 tree kind = c_parser_peek_token (parser)->value;
7410 const char *p = IDENTIFIER_POINTER (kind);
7415 if (strcmp ("dynamic", p) != 0)
7417 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
7421 if (strcmp ("guided", p) != 0)
7423 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
7427 if (strcmp ("runtime", p) != 0)
7429 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
7436 else if (c_parser_next_token_is_keyword (parser, RID_STATIC))
7437 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
7438 else if (c_parser_next_token_is_keyword (parser, RID_AUTO))
7439 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
7443 c_parser_consume_token (parser);
7444 if (c_parser_next_token_is (parser, CPP_COMMA))
7447 c_parser_consume_token (parser);
7449 here = c_parser_peek_token (parser)->location;
7450 t = c_parser_expr_no_commas (parser, NULL).value;
7451 t = c_fully_fold (t, false, NULL);
7453 if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
7454 error_at (here, "schedule %<runtime%> does not take "
7455 "a %<chunk_size%> parameter");
7456 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
7458 "schedule %<auto%> does not take "
7459 "a %<chunk_size%> parameter");
7460 else if (TREE_CODE (TREE_TYPE (t)) == INTEGER_TYPE)
7461 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
7463 c_parser_error (parser, "expected integer expression");
7465 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
7468 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7469 "expected %<,%> or %<)%>");
7471 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule");
7472 OMP_CLAUSE_CHAIN (c) = list;
7476 c_parser_error (parser, "invalid schedule kind");
7477 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 0);
7482 shared ( variable-list ) */
7485 c_parser_omp_clause_shared (c_parser *parser, tree list)
7487 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_SHARED, list);
7494 c_parser_omp_clause_untied (c_parser *parser ATTRIBUTE_UNUSED, tree list)
7498 /* FIXME: Should we allow duplicates? */
7499 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied");
7501 c = build_omp_clause (OMP_CLAUSE_UNTIED);
7502 OMP_CLAUSE_CHAIN (c) = list;
7506 /* Parse all OpenMP clauses. The set clauses allowed by the directive
7507 is a bitmask in MASK. Return the list of clauses found; the result
7508 of clause default goes in *pdefault. */
7511 c_parser_omp_all_clauses (c_parser *parser, unsigned int mask,
7514 tree clauses = NULL;
7517 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
7520 pragma_omp_clause c_kind;
7522 tree prev = clauses;
7524 if (!first && c_parser_next_token_is (parser, CPP_COMMA))
7525 c_parser_consume_token (parser);
7528 here = c_parser_peek_token (parser)->location;
7529 c_kind = c_parser_omp_clause_name (parser);
7533 case PRAGMA_OMP_CLAUSE_COLLAPSE:
7534 clauses = c_parser_omp_clause_collapse (parser, clauses);
7535 c_name = "collapse";
7537 case PRAGMA_OMP_CLAUSE_COPYIN:
7538 clauses = c_parser_omp_clause_copyin (parser, clauses);
7541 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
7542 clauses = c_parser_omp_clause_copyprivate (parser, clauses);
7543 c_name = "copyprivate";
7545 case PRAGMA_OMP_CLAUSE_DEFAULT:
7546 clauses = c_parser_omp_clause_default (parser, clauses);
7549 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
7550 clauses = c_parser_omp_clause_firstprivate (parser, clauses);
7551 c_name = "firstprivate";
7553 case PRAGMA_OMP_CLAUSE_IF:
7554 clauses = c_parser_omp_clause_if (parser, clauses);
7557 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
7558 clauses = c_parser_omp_clause_lastprivate (parser, clauses);
7559 c_name = "lastprivate";
7561 case PRAGMA_OMP_CLAUSE_NOWAIT:
7562 clauses = c_parser_omp_clause_nowait (parser, clauses);
7565 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
7566 clauses = c_parser_omp_clause_num_threads (parser, clauses);
7567 c_name = "num_threads";
7569 case PRAGMA_OMP_CLAUSE_ORDERED:
7570 clauses = c_parser_omp_clause_ordered (parser, clauses);
7573 case PRAGMA_OMP_CLAUSE_PRIVATE:
7574 clauses = c_parser_omp_clause_private (parser, clauses);
7577 case PRAGMA_OMP_CLAUSE_REDUCTION:
7578 clauses = c_parser_omp_clause_reduction (parser, clauses);
7579 c_name = "reduction";
7581 case PRAGMA_OMP_CLAUSE_SCHEDULE:
7582 clauses = c_parser_omp_clause_schedule (parser, clauses);
7583 c_name = "schedule";
7585 case PRAGMA_OMP_CLAUSE_SHARED:
7586 clauses = c_parser_omp_clause_shared (parser, clauses);
7589 case PRAGMA_OMP_CLAUSE_UNTIED:
7590 clauses = c_parser_omp_clause_untied (parser, clauses);
7594 c_parser_error (parser, "expected %<#pragma omp%> clause");
7598 if (((mask >> c_kind) & 1) == 0 && !parser->error)
7600 /* Remove the invalid clause(s) from the list to avoid
7601 confusing the rest of the compiler. */
7603 error_at (here, "%qs is not valid for %qs", c_name, where);
7608 c_parser_skip_to_pragma_eol (parser);
7610 return c_finish_omp_clauses (clauses);
7617 In practice, we're also interested in adding the statement to an
7618 outer node. So it is convenient if we work around the fact that
7619 c_parser_statement calls add_stmt. */
7622 c_parser_omp_structured_block (c_parser *parser)
7624 tree stmt = push_stmt_list ();
7625 c_parser_statement (parser);
7626 return pop_stmt_list (stmt);
7630 # pragma omp atomic new-line
7634 x binop= expr | x++ | ++x | x-- | --x
7636 +, *, -, /, &, ^, |, <<, >>
7638 where x is an lvalue expression with scalar type. */
7641 c_parser_omp_atomic (c_parser *parser)
7645 enum tree_code code;
7646 struct c_expr rhs_expr;
7648 c_parser_skip_to_pragma_eol (parser);
7650 lhs = c_parser_unary_expression (parser).value;
7651 lhs = c_fully_fold (lhs, false, NULL);
7652 switch (TREE_CODE (lhs))
7656 c_parser_skip_to_end_of_block_or_statement (parser);
7659 case PREINCREMENT_EXPR:
7660 case POSTINCREMENT_EXPR:
7661 lhs = TREE_OPERAND (lhs, 0);
7663 rhs = integer_one_node;
7666 case PREDECREMENT_EXPR:
7667 case POSTDECREMENT_EXPR:
7668 lhs = TREE_OPERAND (lhs, 0);
7670 rhs = integer_one_node;
7674 switch (c_parser_peek_token (parser)->type)
7680 code = TRUNC_DIV_EXPR;
7695 code = BIT_AND_EXPR;
7698 code = BIT_IOR_EXPR;
7701 code = BIT_XOR_EXPR;
7704 c_parser_error (parser,
7705 "invalid operator for %<#pragma omp atomic%>");
7709 c_parser_consume_token (parser);
7710 rhs_expr = c_parser_expression (parser);
7711 rhs_expr = default_function_array_conversion (rhs_expr);
7712 rhs = rhs_expr.value;
7713 rhs = c_fully_fold (rhs, false, NULL);
7716 stmt = c_finish_omp_atomic (code, lhs, rhs);
7717 if (stmt != error_mark_node)
7719 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
7724 # pragma omp barrier new-line
7728 c_parser_omp_barrier (c_parser *parser)
7730 c_parser_consume_pragma (parser);
7731 c_parser_skip_to_pragma_eol (parser);
7733 c_finish_omp_barrier ();
7737 # pragma omp critical [(name)] new-line
7742 c_parser_omp_critical (c_parser *parser)
7744 tree stmt, name = NULL;
7746 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
7748 c_parser_consume_token (parser);
7749 if (c_parser_next_token_is (parser, CPP_NAME))
7751 name = c_parser_peek_token (parser)->value;
7752 c_parser_consume_token (parser);
7753 c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
7756 c_parser_error (parser, "expected identifier");
7758 else if (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
7759 c_parser_error (parser, "expected %<(%> or end of line");
7760 c_parser_skip_to_pragma_eol (parser);
7762 stmt = c_parser_omp_structured_block (parser);
7763 return c_finish_omp_critical (stmt, name);
7767 # pragma omp flush flush-vars[opt] new-line
7770 ( variable-list ) */
7773 c_parser_omp_flush (c_parser *parser)
7775 c_parser_consume_pragma (parser);
7776 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
7777 c_parser_omp_var_list_parens (parser, OMP_CLAUSE_ERROR, NULL);
7778 else if (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
7779 c_parser_error (parser, "expected %<(%> or end of line");
7780 c_parser_skip_to_pragma_eol (parser);
7782 c_finish_omp_flush ();
7785 /* Parse the restricted form of the for statement allowed by OpenMP.
7786 The real trick here is to determine the loop control variable early
7787 so that we can push a new decl if necessary to make it private. */
7790 c_parser_omp_for_loop (c_parser *parser, tree clauses, tree *par_clauses)
7792 tree decl, cond, incr, save_break, save_cont, body, init, stmt, cl;
7793 tree declv, condv, incrv, initv, for_block = NULL, ret = NULL;
7795 bool fail = false, open_brace_parsed = false;
7796 int i, collapse = 1, nbraces = 0;
7798 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
7799 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
7800 collapse = tree_low_cst (OMP_CLAUSE_COLLAPSE_EXPR (cl), 0);
7802 gcc_assert (collapse >= 1);
7804 declv = make_tree_vec (collapse);
7805 initv = make_tree_vec (collapse);
7806 condv = make_tree_vec (collapse);
7807 incrv = make_tree_vec (collapse);
7809 if (!c_parser_next_token_is_keyword (parser, RID_FOR))
7811 c_parser_error (parser, "for statement expected");
7814 loc = c_parser_peek_token (parser)->location;
7815 c_parser_consume_token (parser);
7817 for (i = 0; i < collapse; i++)
7821 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7824 /* Parse the initialization declaration or expression. */
7825 if (c_parser_next_token_starts_declspecs (parser))
7829 = tree_cons (NULL, c_begin_compound_stmt (true), for_block);
7830 c_parser_declaration_or_fndef (parser, true, true, true, true);
7831 decl = check_for_loop_decls ();
7834 if (DECL_INITIAL (decl) == error_mark_node)
7835 decl = error_mark_node;
7838 else if (c_parser_next_token_is (parser, CPP_NAME)
7839 && c_parser_peek_2nd_token (parser)->type == CPP_EQ)
7841 struct c_expr decl_exp;
7842 struct c_expr init_exp;
7843 location_t init_loc;
7845 decl_exp = c_parser_postfix_expression (parser);
7846 decl = decl_exp.value;
7848 c_parser_require (parser, CPP_EQ, "expected %<=%>");
7849 init_loc = c_parser_peek_token (parser)->location;
7851 init_exp = c_parser_expr_no_commas (parser, NULL);
7852 init_exp = default_function_array_conversion (init_exp);
7853 init = build_modify_expr (init_loc, decl, decl_exp.original_type,
7854 NOP_EXPR, init_exp.value,
7855 init_exp.original_type);
7856 init = c_process_expr_stmt (init);
7858 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
7863 c_parser_error (parser,
7864 "expected iteration declaration or initialization");
7865 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7871 /* Parse the loop condition. */
7873 if (c_parser_next_token_is_not (parser, CPP_SEMICOLON))
7875 location_t cond_loc = c_parser_peek_token (parser)->location;
7876 struct c_expr cond_expr = c_parser_binary_expression (parser, NULL);
7878 cond = cond_expr.value;
7879 cond = c_objc_common_truthvalue_conversion (cond_loc, cond);
7880 cond = c_fully_fold (cond, false, NULL);
7881 switch (cond_expr.original_code)
7889 /* Can't be cond = error_mark_node, because we want to preserve
7890 the location until c_finish_omp_for. */
7891 cond = build1 (NOP_EXPR, boolean_type_node, error_mark_node);
7894 protected_set_expr_location (cond, cond_loc);
7896 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
7898 /* Parse the increment expression. */
7900 if (c_parser_next_token_is_not (parser, CPP_CLOSE_PAREN))
7902 location_t incr_loc = c_parser_peek_token (parser)->location;
7904 incr = c_process_expr_stmt (c_parser_expression (parser).value);
7905 protected_set_expr_location (incr, incr_loc);
7907 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
7909 if (decl == NULL || decl == error_mark_node || init == error_mark_node)
7913 TREE_VEC_ELT (declv, i) = decl;
7914 TREE_VEC_ELT (initv, i) = init;
7915 TREE_VEC_ELT (condv, i) = cond;
7916 TREE_VEC_ELT (incrv, i) = incr;
7920 if (i == collapse - 1)
7923 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
7924 in between the collapsed for loops to be still considered perfectly
7925 nested. Hopefully the final version clarifies this.
7926 For now handle (multiple) {'s and empty statements. */
7929 if (c_parser_next_token_is_keyword (parser, RID_FOR))
7931 c_parser_consume_token (parser);
7934 else if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
7936 c_parser_consume_token (parser);
7940 && c_parser_next_token_is (parser, CPP_SEMICOLON))
7941 c_parser_consume_token (parser);
7944 c_parser_error (parser, "not enough perfectly nested loops");
7947 open_brace_parsed = true;
7957 nbraces += bracecount;
7960 save_break = c_break_label;
7961 c_break_label = size_one_node;
7962 save_cont = c_cont_label;
7963 c_cont_label = NULL_TREE;
7964 body = push_stmt_list ();
7966 if (open_brace_parsed)
7968 stmt = c_begin_compound_stmt (true);
7969 c_parser_compound_statement_nostart (parser);
7970 add_stmt (c_end_compound_stmt (stmt, true));
7973 add_stmt (c_parser_c99_block_statement (parser));
7975 add_stmt (build1 (LABEL_EXPR, void_type_node, c_cont_label));
7977 body = pop_stmt_list (body);
7978 c_break_label = save_break;
7979 c_cont_label = save_cont;
7983 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
7985 c_parser_consume_token (parser);
7988 else if (c_parser_next_token_is (parser, CPP_SEMICOLON))
7989 c_parser_consume_token (parser);
7992 c_parser_error (parser, "collapsed loops not perfectly nested");
7995 stmt = c_begin_compound_stmt (true);
7997 c_parser_compound_statement_nostart (parser);
7998 body = c_end_compound_stmt (stmt, true);
8005 /* Only bother calling c_finish_omp_for if we haven't already generated
8006 an error from the initialization parsing. */
8009 stmt = c_finish_omp_for (loc, declv, initv, condv, incrv, body, NULL);
8012 if (par_clauses != NULL)
8015 for (c = par_clauses; *c ; )
8016 if (OMP_CLAUSE_CODE (*c) != OMP_CLAUSE_FIRSTPRIVATE
8017 && OMP_CLAUSE_CODE (*c) != OMP_CLAUSE_LASTPRIVATE)
8018 c = &OMP_CLAUSE_CHAIN (*c);
8021 for (i = 0; i < collapse; i++)
8022 if (TREE_VEC_ELT (declv, i) == OMP_CLAUSE_DECL (*c))
8025 c = &OMP_CLAUSE_CHAIN (*c);
8026 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE)
8029 "iteration variable %qD should not be firstprivate",
8030 OMP_CLAUSE_DECL (*c));
8031 *c = OMP_CLAUSE_CHAIN (*c);
8035 /* Copy lastprivate (decl) clause to OMP_FOR_CLAUSES,
8036 change it to shared (decl) in
8037 OMP_PARALLEL_CLAUSES. */
8038 tree l = build_omp_clause (OMP_CLAUSE_LASTPRIVATE);
8039 OMP_CLAUSE_DECL (l) = OMP_CLAUSE_DECL (*c);
8040 OMP_CLAUSE_CHAIN (l) = clauses;
8042 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
8046 OMP_FOR_CLAUSES (stmt) = clauses;
8053 stmt = c_end_compound_stmt (TREE_VALUE (for_block), true);
8055 for_block = TREE_CHAIN (for_block);
8061 #pragma omp for for-clause[optseq] new-line
8065 #define OMP_FOR_CLAUSE_MASK \
8066 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
8067 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
8068 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
8069 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
8070 | (1u << PRAGMA_OMP_CLAUSE_ORDERED) \
8071 | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE) \
8072 | (1u << PRAGMA_OMP_CLAUSE_COLLAPSE) \
8073 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
8076 c_parser_omp_for (c_parser *parser)
8078 tree block, clauses, ret;
8080 clauses = c_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
8083 block = c_begin_compound_stmt (true);
8084 ret = c_parser_omp_for_loop (parser, clauses, NULL);
8085 block = c_end_compound_stmt (block, true);
8092 # pragma omp master new-line
8097 c_parser_omp_master (c_parser *parser)
8099 c_parser_skip_to_pragma_eol (parser);
8100 return c_finish_omp_master (c_parser_omp_structured_block (parser));
8104 # pragma omp ordered new-line
8109 c_parser_omp_ordered (c_parser *parser)
8111 c_parser_skip_to_pragma_eol (parser);
8112 return c_finish_omp_ordered (c_parser_omp_structured_block (parser));
8118 { section-sequence }
8121 section-directive[opt] structured-block
8122 section-sequence section-directive structured-block */
8125 c_parser_omp_sections_scope (c_parser *parser)
8128 bool error_suppress = false;
8131 if (!c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
8133 /* Avoid skipping until the end of the block. */
8134 parser->error = false;
8138 stmt = push_stmt_list ();
8140 loc = c_parser_peek_token (parser)->location;
8141 if (c_parser_peek_token (parser)->pragma_kind != PRAGMA_OMP_SECTION)
8143 substmt = push_stmt_list ();
8147 c_parser_statement (parser);
8149 if (c_parser_peek_token (parser)->pragma_kind == PRAGMA_OMP_SECTION)
8151 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
8153 if (c_parser_next_token_is (parser, CPP_EOF))
8157 substmt = pop_stmt_list (substmt);
8158 substmt = build1 (OMP_SECTION, void_type_node, substmt);
8159 SET_EXPR_LOCATION (substmt, loc);
8165 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
8167 if (c_parser_next_token_is (parser, CPP_EOF))
8170 loc = c_parser_peek_token (parser)->location;
8171 if (c_parser_peek_token (parser)->pragma_kind == PRAGMA_OMP_SECTION)
8173 c_parser_consume_pragma (parser);
8174 c_parser_skip_to_pragma_eol (parser);
8175 error_suppress = false;
8177 else if (!error_suppress)
8179 error_at (loc, "expected %<#pragma omp section%> or %<}%>");
8180 error_suppress = true;
8183 substmt = c_parser_omp_structured_block (parser);
8184 substmt = build1 (OMP_SECTION, void_type_node, substmt);
8185 SET_EXPR_LOCATION (substmt, loc);
8188 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE,
8189 "expected %<#pragma omp section%> or %<}%>");
8191 substmt = pop_stmt_list (stmt);
8193 stmt = make_node (OMP_SECTIONS);
8194 TREE_TYPE (stmt) = void_type_node;
8195 OMP_SECTIONS_BODY (stmt) = substmt;
8197 return add_stmt (stmt);
8201 # pragma omp sections sections-clause[optseq] newline
8205 #define OMP_SECTIONS_CLAUSE_MASK \
8206 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
8207 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
8208 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
8209 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
8210 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
8213 c_parser_omp_sections (c_parser *parser)
8215 tree block, clauses, ret;
8217 clauses = c_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
8218 "#pragma omp sections");
8220 block = c_begin_compound_stmt (true);
8221 ret = c_parser_omp_sections_scope (parser);
8223 OMP_SECTIONS_CLAUSES (ret) = clauses;
8224 block = c_end_compound_stmt (block, true);
8231 # pragma parallel parallel-clause new-line
8232 # pragma parallel for parallel-for-clause new-line
8233 # pragma parallel sections parallel-sections-clause new-line
8236 #define OMP_PARALLEL_CLAUSE_MASK \
8237 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
8238 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
8239 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
8240 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
8241 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
8242 | (1u << PRAGMA_OMP_CLAUSE_COPYIN) \
8243 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
8244 | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
8247 c_parser_omp_parallel (c_parser *parser)
8249 enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
8250 const char *p_name = "#pragma omp parallel";
8251 tree stmt, clauses, par_clause, ws_clause, block;
8252 unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
8254 if (c_parser_next_token_is_keyword (parser, RID_FOR))
8256 c_parser_consume_token (parser);
8257 p_kind = PRAGMA_OMP_PARALLEL_FOR;
8258 p_name = "#pragma omp parallel for";
8259 mask |= OMP_FOR_CLAUSE_MASK;
8260 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
8262 else if (c_parser_next_token_is (parser, CPP_NAME))
8264 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
8265 if (strcmp (p, "sections") == 0)
8267 c_parser_consume_token (parser);
8268 p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
8269 p_name = "#pragma omp parallel sections";
8270 mask |= OMP_SECTIONS_CLAUSE_MASK;
8271 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
8275 clauses = c_parser_omp_all_clauses (parser, mask, p_name);
8279 case PRAGMA_OMP_PARALLEL:
8280 block = c_begin_omp_parallel ();
8281 c_parser_statement (parser);
8282 stmt = c_finish_omp_parallel (clauses, block);
8285 case PRAGMA_OMP_PARALLEL_FOR:
8286 block = c_begin_omp_parallel ();
8287 c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
8288 c_parser_omp_for_loop (parser, ws_clause, &par_clause);
8289 stmt = c_finish_omp_parallel (par_clause, block);
8290 OMP_PARALLEL_COMBINED (stmt) = 1;
8293 case PRAGMA_OMP_PARALLEL_SECTIONS:
8294 block = c_begin_omp_parallel ();
8295 c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
8296 stmt = c_parser_omp_sections_scope (parser);
8298 OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
8299 stmt = c_finish_omp_parallel (par_clause, block);
8300 OMP_PARALLEL_COMBINED (stmt) = 1;
8311 # pragma omp single single-clause[optseq] new-line
8315 #define OMP_SINGLE_CLAUSE_MASK \
8316 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
8317 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
8318 | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
8319 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
8322 c_parser_omp_single (c_parser *parser)
8324 tree stmt = make_node (OMP_SINGLE);
8325 TREE_TYPE (stmt) = void_type_node;
8327 OMP_SINGLE_CLAUSES (stmt)
8328 = c_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
8329 "#pragma omp single");
8330 OMP_SINGLE_BODY (stmt) = c_parser_omp_structured_block (parser);
8332 return add_stmt (stmt);
8336 # pragma omp task task-clause[optseq] new-line
8339 #define OMP_TASK_CLAUSE_MASK \
8340 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
8341 | (1u << PRAGMA_OMP_CLAUSE_UNTIED) \
8342 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
8343 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
8344 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
8345 | (1u << PRAGMA_OMP_CLAUSE_SHARED))
8348 c_parser_omp_task (c_parser *parser)
8350 tree clauses, block;
8352 clauses = c_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
8353 "#pragma omp task");
8355 block = c_begin_omp_task ();
8356 c_parser_statement (parser);
8357 return c_finish_omp_task (clauses, block);
8361 # pragma omp taskwait new-line
8365 c_parser_omp_taskwait (c_parser *parser)
8367 c_parser_consume_pragma (parser);
8368 c_parser_skip_to_pragma_eol (parser);
8370 c_finish_omp_taskwait ();
8373 /* Main entry point to parsing most OpenMP pragmas. */
8376 c_parser_omp_construct (c_parser *parser)
8378 enum pragma_kind p_kind;
8382 loc = c_parser_peek_token (parser)->location;
8383 p_kind = c_parser_peek_token (parser)->pragma_kind;
8384 c_parser_consume_pragma (parser);
8386 /* For all constructs below except #pragma omp atomic
8387 MUST_NOT_THROW catch handlers are needed when exceptions
8389 if (p_kind != PRAGMA_OMP_ATOMIC)
8390 c_maybe_initialize_eh ();
8394 case PRAGMA_OMP_ATOMIC:
8395 c_parser_omp_atomic (parser);
8397 case PRAGMA_OMP_CRITICAL:
8398 stmt = c_parser_omp_critical (parser);
8400 case PRAGMA_OMP_FOR:
8401 stmt = c_parser_omp_for (parser);
8403 case PRAGMA_OMP_MASTER:
8404 stmt = c_parser_omp_master (parser);
8406 case PRAGMA_OMP_ORDERED:
8407 stmt = c_parser_omp_ordered (parser);
8409 case PRAGMA_OMP_PARALLEL:
8410 stmt = c_parser_omp_parallel (parser);
8412 case PRAGMA_OMP_SECTIONS:
8413 stmt = c_parser_omp_sections (parser);
8415 case PRAGMA_OMP_SINGLE:
8416 stmt = c_parser_omp_single (parser);
8418 case PRAGMA_OMP_TASK:
8419 stmt = c_parser_omp_task (parser);
8426 SET_EXPR_LOCATION (stmt, loc);
8431 # pragma omp threadprivate (variable-list) */
8434 c_parser_omp_threadprivate (c_parser *parser)
8438 c_parser_consume_pragma (parser);
8439 vars = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_ERROR, NULL);
8441 /* Mark every variable in VARS to be assigned thread local storage. */
8442 for (t = vars; t; t = TREE_CHAIN (t))
8444 tree v = TREE_PURPOSE (t);
8446 /* If V had already been marked threadprivate, it doesn't matter
8447 whether it had been used prior to this point. */
8448 if (TREE_CODE (v) != VAR_DECL)
8449 error ("%qD is not a variable", v);
8450 else if (TREE_USED (v) && !C_DECL_THREADPRIVATE_P (v))
8451 error ("%qE declared %<threadprivate%> after first use", v);
8452 else if (! TREE_STATIC (v) && ! DECL_EXTERNAL (v))
8453 error ("automatic variable %qE cannot be %<threadprivate%>", v);
8454 else if (TREE_TYPE (v) == error_mark_node)
8456 else if (! COMPLETE_TYPE_P (TREE_TYPE (v)))
8457 error ("%<threadprivate%> %qE has incomplete type", v);
8460 if (! DECL_THREAD_LOCAL_P (v))
8462 DECL_TLS_MODEL (v) = decl_default_tls_model (v);
8463 /* If rtl has been already set for this var, call
8464 make_decl_rtl once again, so that encode_section_info
8465 has a chance to look at the new decl flags. */
8466 if (DECL_RTL_SET_P (v))
8469 C_DECL_THREADPRIVATE_P (v) = 1;
8473 c_parser_skip_to_pragma_eol (parser);
8477 /* Parse a single source file. */
8482 /* Use local storage to begin. If the first token is a pragma, parse it.
8483 If it is #pragma GCC pch_preprocess, then this will load a PCH file
8484 which will cause garbage collection. */
8487 memset (&tparser, 0, sizeof tparser);
8488 the_parser = &tparser;
8490 if (c_parser_peek_token (&tparser)->pragma_kind == PRAGMA_GCC_PCH_PREPROCESS)
8491 c_parser_pragma_pch_preprocess (&tparser);
8493 the_parser = GGC_NEW (c_parser);
8494 *the_parser = tparser;
8496 c_parser_translation_unit (the_parser);
8500 #include "gt-c-parser.h"