1 /* Parser for C and Objective-C.
2 Copyright (C) 1987, 1988, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2007, 2008, 2009, 2010
4 Free Software Foundation, Inc.
6 Parser actions based on the old Bison parser; structure somewhat
7 influenced by and fragments based on the C++ parser.
9 This file is part of GCC.
11 GCC is free software; you can redistribute it and/or modify it under
12 the terms of the GNU General Public License as published by the Free
13 Software Foundation; either version 3, or (at your option) any later
16 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
17 WARRANTY; without even the implied warranty of MERCHANTABILITY or
18 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21 You should have received a copy of the GNU General Public License
22 along with GCC; see the file COPYING3. If not see
23 <http://www.gnu.org/licenses/>. */
27 Make sure all relevant comments, and all relevant code from all
28 actions, brought over from old parser. Verify exact correspondence
31 Add testcases covering every input symbol in every state in old and
34 Include full syntax for GNU C, including erroneous cases accepted
35 with error messages, in syntax productions in comments.
37 Make more diagnostics in the front end generally take an explicit
38 location rather than implicitly using input_location. */
42 #include "coretypes.h"
43 #include "tm.h" /* For rtl.h: needs enum reg_class. */
45 #include "langhooks.h"
49 #include "c-family/c-pragma.h"
55 #include "c-family/c-common.h"
62 /* Initialization routine for this file. */
67 /* The only initialization required is of the reserved word
73 /* Make sure RID_MAX hasn't grown past the 8 bits used to hold the keyword in
74 the c_token structure. */
75 gcc_assert (RID_MAX <= 255);
82 mask |= D_ASM | D_EXT;
86 if (!c_dialect_objc ())
87 mask |= D_OBJC | D_CXX_OBJC;
89 ridpointers = ggc_alloc_cleared_vec_tree ((int) RID_MAX);
90 for (i = 0; i < num_c_common_reswords; i++)
92 /* If a keyword is disabled, do not enter it into the table
93 and so create a canonical spelling that isn't a keyword. */
94 if (c_common_reswords[i].disable & mask)
97 && (c_common_reswords[i].disable & D_CXXWARN))
99 id = get_identifier (c_common_reswords[i].word);
100 C_SET_RID_CODE (id, RID_CXX_COMPAT_WARN);
101 C_IS_RESERVED_WORD (id) = 1;
106 id = get_identifier (c_common_reswords[i].word);
107 C_SET_RID_CODE (id, c_common_reswords[i].rid);
108 C_IS_RESERVED_WORD (id) = 1;
109 ridpointers [(int) c_common_reswords[i].rid] = id;
113 /* The C lexer intermediates between the lexer in cpplib and c-lex.c
114 and the C parser. Unlike the C++ lexer, the parser structure
115 stores the lexer information instead of using a separate structure.
116 Identifiers are separated into ordinary identifiers, type names,
117 keywords and some other Objective-C types of identifiers, and some
118 look-ahead is maintained.
120 ??? It might be a good idea to lex the whole file up front (as for
121 C++). It would then be possible to share more of the C and C++
122 lexer code, if desired. */
124 /* The following local token type is used. */
127 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
129 /* More information about the type of a CPP_NAME token. */
130 typedef enum c_id_kind {
131 /* An ordinary identifier. */
133 /* An identifier declared as a typedef name. */
135 /* An identifier declared as an Objective-C class name. */
137 /* An address space identifier. */
139 /* Not an identifier. */
143 /* A single C token after string literal concatenation and conversion
144 of preprocessing tokens to tokens. */
145 typedef struct GTY (()) c_token {
146 /* The kind of token. */
147 ENUM_BITFIELD (cpp_ttype) type : 8;
148 /* If this token is a CPP_NAME, this value indicates whether also
149 declared as some kind of type. Otherwise, it is C_ID_NONE. */
150 ENUM_BITFIELD (c_id_kind) id_kind : 8;
151 /* If this token is a keyword, this value indicates which keyword.
152 Otherwise, this value is RID_MAX. */
153 ENUM_BITFIELD (rid) keyword : 8;
154 /* If this token is a CPP_PRAGMA, this indicates the pragma that
155 was seen. Otherwise it is PRAGMA_NONE. */
156 ENUM_BITFIELD (pragma_kind) pragma_kind : 8;
157 /* The location at which this token was found. */
159 /* The value associated with this token, if any. */
163 /* A parser structure recording information about the state and
164 context of parsing. Includes lexer information with up to two
165 tokens of look-ahead; more are not needed for C. */
166 typedef struct GTY(()) c_parser {
167 /* The look-ahead tokens. */
169 /* How many look-ahead tokens are available (0, 1 or 2). */
171 /* True if a syntax error is being recovered from; false otherwise.
172 c_parser_error sets this flag. It should clear this flag when
173 enough tokens have been consumed to recover from the error. */
174 BOOL_BITFIELD error : 1;
175 /* True if we're processing a pragma, and shouldn't automatically
176 consume CPP_PRAGMA_EOL. */
177 BOOL_BITFIELD in_pragma : 1;
178 /* True if we're parsing the outermost block of an if statement. */
179 BOOL_BITFIELD in_if_block : 1;
180 /* True if we want to lex an untranslated string. */
181 BOOL_BITFIELD lex_untranslated_string : 1;
183 /* Objective-C specific parser/lexer information. */
185 /* True if we are in a context where the Objective-C "PQ" keywords
186 are considered keywords. */
187 BOOL_BITFIELD objc_pq_context : 1;
188 /* True if we are parsing a (potential) Objective-C foreach
189 statement. This is set to true after we parsed 'for (' and while
190 we wait for 'in' or ';' to decide if it's a standard C for loop or an
191 Objective-C foreach loop. */
192 BOOL_BITFIELD objc_could_be_foreach_context : 1;
193 /* The following flag is needed to contextualize Objective-C lexical
194 analysis. In some cases (e.g., 'int NSObject;'), it is
195 undesirable to bind an identifier to an Objective-C class, even
196 if a class with that name exists. */
197 BOOL_BITFIELD objc_need_raw_identifier : 1;
198 /* True if we are in a context where the Objective-C "Property attribute"
199 keywords are valid. */
200 BOOL_BITFIELD objc_property_attr_context : 1;
204 /* The actual parser and external interface. ??? Does this need to be
205 garbage-collected? */
207 static GTY (()) c_parser *the_parser;
209 /* Read in and lex a single token, storing it in *TOKEN. */
212 c_lex_one_token (c_parser *parser, c_token *token)
214 timevar_push (TV_LEX);
216 token->type = c_lex_with_flags (&token->value, &token->location, NULL,
217 (parser->lex_untranslated_string
218 ? C_LEX_STRING_NO_TRANSLATE : 0));
219 token->id_kind = C_ID_NONE;
220 token->keyword = RID_MAX;
221 token->pragma_kind = PRAGMA_NONE;
229 bool objc_force_identifier = parser->objc_need_raw_identifier;
230 if (c_dialect_objc ())
231 parser->objc_need_raw_identifier = false;
233 if (C_IS_RESERVED_WORD (token->value))
235 enum rid rid_code = C_RID_CODE (token->value);
237 if (rid_code == RID_CXX_COMPAT_WARN)
239 warning_at (token->location,
241 "identifier %qE conflicts with C++ keyword",
244 else if (rid_code >= RID_FIRST_ADDR_SPACE
245 && rid_code <= RID_LAST_ADDR_SPACE)
247 token->id_kind = C_ID_ADDRSPACE;
248 token->keyword = rid_code;
251 else if (c_dialect_objc () && OBJC_IS_PQ_KEYWORD (rid_code))
253 /* We found an Objective-C "pq" keyword (in, out,
254 inout, bycopy, byref, oneway). They need special
255 care because the interpretation depends on the
258 if (parser->objc_pq_context)
260 token->type = CPP_KEYWORD;
261 token->keyword = rid_code;
264 else if (parser->objc_could_be_foreach_context
265 && rid_code == RID_IN)
267 /* We are in Objective-C, inside a (potential)
268 foreach context (which means after having
269 parsed 'for (', but before having parsed ';'),
270 and we found 'in'. We consider it the keyword
271 which terminates the declaration at the
272 beginning of a foreach-statement. Note that
273 this means you can't use 'in' for anything else
274 in that context; in particular, in Objective-C
275 you can't use 'in' as the name of the running
276 variable in a C for loop. We could potentially
277 try to add code here to disambiguate, but it
278 seems a reasonable limitation.
280 token->type = CPP_KEYWORD;
281 token->keyword = rid_code;
284 /* Else, "pq" keywords outside of the "pq" context are
285 not keywords, and we fall through to the code for
289 else if (c_dialect_objc () && OBJC_IS_PATTR_KEYWORD (rid_code))
291 /* We found an Objective-C "property attribute" keyword
292 (readonly, copies, getter, setter, ivar). These are
293 only valid in the property context. */
294 if (parser->objc_property_attr_context)
296 token->type = CPP_KEYWORD;
297 token->keyword = rid_code;
300 /* Else they are not special keywords.
303 else if (c_dialect_objc ()
304 && (OBJC_IS_AT_KEYWORD (rid_code)
305 || OBJC_IS_CXX_KEYWORD (rid_code)))
307 /* We found one of the Objective-C "@" keywords (defs,
308 selector, synchronized, etc) or one of the
309 Objective-C "cxx" keywords (class, private,
310 protected, public, try, catch, throw) without a
311 preceding '@' sign. Do nothing and fall through to
312 the code for normal tokens (in C++ we would still
313 consider the CXX ones keywords, but not in C).
319 token->type = CPP_KEYWORD;
320 token->keyword = rid_code;
325 decl = lookup_name (token->value);
328 if (TREE_CODE (decl) == TYPE_DECL)
330 token->id_kind = C_ID_TYPENAME;
334 else if (c_dialect_objc ())
336 tree objc_interface_decl = objc_is_class_name (token->value);
337 /* Objective-C class names are in the same namespace as
338 variables and typedefs, and hence are shadowed by local
340 if (objc_interface_decl
341 && (global_bindings_p ()
342 || (!objc_force_identifier && !decl)))
344 token->value = objc_interface_decl;
345 token->id_kind = C_ID_CLASSNAME;
349 token->id_kind = C_ID_ID;
353 /* This only happens in Objective-C; it must be a keyword. */
354 token->type = CPP_KEYWORD;
355 switch (C_RID_CODE (token->value))
357 /* Replace 'class' with '@class', 'private' with '@private',
358 etc. This prevents confusion with the C++ keyword
359 'class', and makes the tokens consistent with other
360 Objective-C 'AT' keywords. For example '@class' is
361 reported as RID_AT_CLASS which is consistent with
362 '@synchronized', which is reported as
365 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
366 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
367 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
368 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
369 case RID_THROW: token->keyword = RID_AT_THROW; break;
370 case RID_TRY: token->keyword = RID_AT_TRY; break;
371 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
372 default: token->keyword = C_RID_CODE (token->value);
377 case CPP_CLOSE_PAREN:
379 /* These tokens may affect the interpretation of any identifiers
380 following, if doing Objective-C. */
381 if (c_dialect_objc ())
382 parser->objc_need_raw_identifier = false;
385 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
386 token->pragma_kind = (enum pragma_kind) TREE_INT_CST_LOW (token->value);
392 timevar_pop (TV_LEX);
395 /* Return a pointer to the next token from PARSER, reading it in if
398 static inline c_token *
399 c_parser_peek_token (c_parser *parser)
401 if (parser->tokens_avail == 0)
403 c_lex_one_token (parser, &parser->tokens[0]);
404 parser->tokens_avail = 1;
406 return &parser->tokens[0];
409 /* Return true if the next token from PARSER has the indicated
413 c_parser_next_token_is (c_parser *parser, enum cpp_ttype type)
415 return c_parser_peek_token (parser)->type == type;
418 /* Return true if the next token from PARSER does not have the
422 c_parser_next_token_is_not (c_parser *parser, enum cpp_ttype type)
424 return !c_parser_next_token_is (parser, type);
427 /* Return true if the next token from PARSER is the indicated
431 c_parser_next_token_is_keyword (c_parser *parser, enum rid keyword)
433 return c_parser_peek_token (parser)->keyword == keyword;
436 /* Return true if TOKEN can start a type name,
439 c_token_starts_typename (c_token *token)
444 switch (token->id_kind)
453 gcc_assert (c_dialect_objc ());
459 switch (token->keyword)
492 if (c_dialect_objc ())
500 /* Return true if the next token from PARSER can start a type name,
503 c_parser_next_token_starts_typename (c_parser *parser)
505 c_token *token = c_parser_peek_token (parser);
506 return c_token_starts_typename (token);
509 /* Return true if TOKEN can start declaration specifiers, false
512 c_token_starts_declspecs (c_token *token)
517 switch (token->id_kind)
526 gcc_assert (c_dialect_objc ());
532 switch (token->keyword)
572 if (c_dialect_objc ())
581 /* Return true if TOKEN can start declaration specifiers or a static
582 assertion, false otherwise. */
584 c_token_starts_declaration (c_token *token)
586 if (c_token_starts_declspecs (token)
587 || token->keyword == RID_STATIC_ASSERT)
593 static c_token *c_parser_peek_2nd_token (c_parser *parser);
595 /* Return true if the next token from PARSER can start declaration
596 specifiers, false otherwise. */
598 c_parser_next_token_starts_declspecs (c_parser *parser)
600 c_token *token = c_parser_peek_token (parser);
602 /* In Objective-C, a classname normally starts a declspecs unless it
603 is immediately followed by a dot. In that case, it is the
604 Objective-C 2.0 "dot-syntax" for class objects, ie, calls the
605 setter/getter on the class. c_token_starts_declspecs() can't
606 differentiate between the two cases because it only checks the
607 current token, so we have a special check here. */
608 if (c_dialect_objc ()
609 && token->type == CPP_NAME
610 && token->id_kind == C_ID_CLASSNAME
611 && c_parser_peek_2nd_token (parser)->type == CPP_DOT)
614 return c_token_starts_declspecs (token);
617 /* Return true if the next tokens from PARSER can start declaration
618 specifiers or a static assertion, false otherwise. */
620 c_parser_next_tokens_start_declaration (c_parser *parser)
622 c_token *token = c_parser_peek_token (parser);
625 if (c_dialect_objc ()
626 && token->type == CPP_NAME
627 && token->id_kind == C_ID_CLASSNAME
628 && c_parser_peek_2nd_token (parser)->type == CPP_DOT)
631 /* Labels do not start declarations. */
632 if (token->type == CPP_NAME
633 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
636 if (c_token_starts_declaration (token))
639 /* Try a bit harder to detect an unknown typename. */
640 if (token->type == CPP_NAME
641 && token->id_kind == C_ID_ID
642 && (c_parser_peek_2nd_token (parser)->type == CPP_NAME
643 || c_parser_peek_2nd_token (parser)->type == CPP_MULT)
644 && !lookup_name (token->value))
650 /* Return a pointer to the next-but-one token from PARSER, reading it
651 in if necessary. The next token is already read in. */
654 c_parser_peek_2nd_token (c_parser *parser)
656 if (parser->tokens_avail >= 2)
657 return &parser->tokens[1];
658 gcc_assert (parser->tokens_avail == 1);
659 gcc_assert (parser->tokens[0].type != CPP_EOF);
660 gcc_assert (parser->tokens[0].type != CPP_PRAGMA_EOL);
661 c_lex_one_token (parser, &parser->tokens[1]);
662 parser->tokens_avail = 2;
663 return &parser->tokens[1];
666 /* Consume the next token from PARSER. */
669 c_parser_consume_token (c_parser *parser)
671 gcc_assert (parser->tokens_avail >= 1);
672 gcc_assert (parser->tokens[0].type != CPP_EOF);
673 gcc_assert (!parser->in_pragma || parser->tokens[0].type != CPP_PRAGMA_EOL);
674 gcc_assert (parser->error || parser->tokens[0].type != CPP_PRAGMA);
675 if (parser->tokens_avail == 2)
676 parser->tokens[0] = parser->tokens[1];
677 parser->tokens_avail--;
680 /* Expect the current token to be a #pragma. Consume it and remember
681 that we've begun parsing a pragma. */
684 c_parser_consume_pragma (c_parser *parser)
686 gcc_assert (!parser->in_pragma);
687 gcc_assert (parser->tokens_avail >= 1);
688 gcc_assert (parser->tokens[0].type == CPP_PRAGMA);
689 if (parser->tokens_avail == 2)
690 parser->tokens[0] = parser->tokens[1];
691 parser->tokens_avail--;
692 parser->in_pragma = true;
695 /* Update the globals input_location and in_system_header from
698 c_parser_set_source_position_from_token (c_token *token)
700 if (token->type != CPP_EOF)
702 input_location = token->location;
706 /* Issue a diagnostic of the form
707 FILE:LINE: MESSAGE before TOKEN
708 where TOKEN is the next token in the input stream of PARSER.
709 MESSAGE (specified by the caller) is usually of the form "expected
712 Do not issue a diagnostic if still recovering from an error.
714 ??? This is taken from the C++ parser, but building up messages in
715 this way is not i18n-friendly and some other approach should be
719 c_parser_error (c_parser *parser, const char *gmsgid)
721 c_token *token = c_parser_peek_token (parser);
724 parser->error = true;
727 /* This diagnostic makes more sense if it is tagged to the line of
728 the token we just peeked at. */
729 c_parser_set_source_position_from_token (token);
730 c_parse_error (gmsgid,
731 /* Because c_parse_error does not understand
732 CPP_KEYWORD, keywords are treated like
734 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
735 /* ??? The C parser does not save the cpp flags of a
736 token, we need to pass 0 here and we will not get
737 the source spelling of some tokens but rather the
738 canonical spelling. */
739 token->value, /*flags=*/0);
742 /* If the next token is of the indicated TYPE, consume it. Otherwise,
743 issue the error MSGID. If MSGID is NULL then a message has already
744 been produced and no message will be produced this time. Returns
745 true if found, false otherwise. */
748 c_parser_require (c_parser *parser,
752 if (c_parser_next_token_is (parser, type))
754 c_parser_consume_token (parser);
759 c_parser_error (parser, msgid);
764 /* If the next token is the indicated keyword, consume it. Otherwise,
765 issue the error MSGID. Returns true if found, false otherwise. */
768 c_parser_require_keyword (c_parser *parser,
772 if (c_parser_next_token_is_keyword (parser, keyword))
774 c_parser_consume_token (parser);
779 c_parser_error (parser, msgid);
784 /* Like c_parser_require, except that tokens will be skipped until the
785 desired token is found. An error message is still produced if the
786 next token is not as expected. If MSGID is NULL then a message has
787 already been produced and no message will be produced this
791 c_parser_skip_until_found (c_parser *parser,
795 unsigned nesting_depth = 0;
797 if (c_parser_require (parser, type, msgid))
800 /* Skip tokens until the desired token is found. */
803 /* Peek at the next token. */
804 c_token *token = c_parser_peek_token (parser);
805 /* If we've reached the token we want, consume it and stop. */
806 if (token->type == type && !nesting_depth)
808 c_parser_consume_token (parser);
812 /* If we've run out of tokens, stop. */
813 if (token->type == CPP_EOF)
815 if (token->type == CPP_PRAGMA_EOL && parser->in_pragma)
817 if (token->type == CPP_OPEN_BRACE
818 || token->type == CPP_OPEN_PAREN
819 || token->type == CPP_OPEN_SQUARE)
821 else if (token->type == CPP_CLOSE_BRACE
822 || token->type == CPP_CLOSE_PAREN
823 || token->type == CPP_CLOSE_SQUARE)
825 if (nesting_depth-- == 0)
828 /* Consume this token. */
829 c_parser_consume_token (parser);
831 parser->error = false;
834 /* Skip tokens until the end of a parameter is found, but do not
835 consume the comma, semicolon or closing delimiter. */
838 c_parser_skip_to_end_of_parameter (c_parser *parser)
840 unsigned nesting_depth = 0;
844 c_token *token = c_parser_peek_token (parser);
845 if ((token->type == CPP_COMMA || token->type == CPP_SEMICOLON)
848 /* If we've run out of tokens, stop. */
849 if (token->type == CPP_EOF)
851 if (token->type == CPP_PRAGMA_EOL && parser->in_pragma)
853 if (token->type == CPP_OPEN_BRACE
854 || token->type == CPP_OPEN_PAREN
855 || token->type == CPP_OPEN_SQUARE)
857 else if (token->type == CPP_CLOSE_BRACE
858 || token->type == CPP_CLOSE_PAREN
859 || token->type == CPP_CLOSE_SQUARE)
861 if (nesting_depth-- == 0)
864 /* Consume this token. */
865 c_parser_consume_token (parser);
867 parser->error = false;
870 /* Expect to be at the end of the pragma directive and consume an
871 end of line marker. */
874 c_parser_skip_to_pragma_eol (c_parser *parser)
876 gcc_assert (parser->in_pragma);
877 parser->in_pragma = false;
879 if (!c_parser_require (parser, CPP_PRAGMA_EOL, "expected end of line"))
882 c_token *token = c_parser_peek_token (parser);
883 if (token->type == CPP_EOF)
885 if (token->type == CPP_PRAGMA_EOL)
887 c_parser_consume_token (parser);
890 c_parser_consume_token (parser);
893 parser->error = false;
896 /* Skip tokens until we have consumed an entire block, or until we
897 have consumed a non-nested ';'. */
900 c_parser_skip_to_end_of_block_or_statement (c_parser *parser)
902 unsigned nesting_depth = 0;
903 bool save_error = parser->error;
909 /* Peek at the next token. */
910 token = c_parser_peek_token (parser);
918 if (parser->in_pragma)
923 /* If the next token is a ';', we have reached the
924 end of the statement. */
927 /* Consume the ';'. */
928 c_parser_consume_token (parser);
933 case CPP_CLOSE_BRACE:
934 /* If the next token is a non-nested '}', then we have
935 reached the end of the current block. */
936 if (nesting_depth == 0 || --nesting_depth == 0)
938 c_parser_consume_token (parser);
944 /* If it the next token is a '{', then we are entering a new
945 block. Consume the entire block. */
950 /* If we see a pragma, consume the whole thing at once. We
951 have some safeguards against consuming pragmas willy-nilly.
952 Normally, we'd expect to be here with parser->error set,
953 which disables these safeguards. But it's possible to get
954 here for secondary error recovery, after parser->error has
956 c_parser_consume_pragma (parser);
957 c_parser_skip_to_pragma_eol (parser);
958 parser->error = save_error;
965 c_parser_consume_token (parser);
969 parser->error = false;
972 /* CPP's options (initialized by c-opts.c). */
973 extern cpp_options *cpp_opts;
975 /* Save the warning flags which are controlled by __extension__. */
978 disable_extension_diagnostics (void)
981 | (warn_pointer_arith << 1)
982 | (warn_traditional << 2)
984 | (warn_long_long << 4)
985 | (warn_cxx_compat << 5));
986 cpp_opts->cpp_pedantic = pedantic = 0;
987 warn_pointer_arith = 0;
988 cpp_opts->cpp_warn_traditional = warn_traditional = 0;
990 cpp_opts->cpp_warn_long_long = warn_long_long = 0;
995 /* Restore the warning flags which are controlled by __extension__.
996 FLAGS is the return value from disable_extension_diagnostics. */
999 restore_extension_diagnostics (int flags)
1001 cpp_opts->cpp_pedantic = pedantic = flags & 1;
1002 warn_pointer_arith = (flags >> 1) & 1;
1003 cpp_opts->cpp_warn_traditional = warn_traditional = (flags >> 2) & 1;
1004 flag_iso = (flags >> 3) & 1;
1005 cpp_opts->cpp_warn_long_long = warn_long_long = (flags >> 4) & 1;
1006 warn_cxx_compat = (flags >> 5) & 1;
1009 /* Possibly kinds of declarator to parse. */
1010 typedef enum c_dtr_syn {
1011 /* A normal declarator with an identifier. */
1013 /* An abstract declarator (maybe empty). */
1015 /* A parameter declarator: may be either, but after a type name does
1016 not redeclare a typedef name as an identifier if it can
1017 alternatively be interpreted as a typedef name; see DR#009,
1018 applied in C90 TC1, omitted from C99 and reapplied in C99 TC2
1019 following DR#249. For example, given a typedef T, "int T" and
1020 "int *T" are valid parameter declarations redeclaring T, while
1021 "int (T)" and "int * (T)" and "int (T[])" and "int (T (int))" are
1022 abstract declarators rather than involving redundant parentheses;
1023 the same applies with attributes inside the parentheses before
1028 static void c_parser_external_declaration (c_parser *);
1029 static void c_parser_asm_definition (c_parser *);
1030 static void c_parser_declaration_or_fndef (c_parser *, bool, bool, bool,
1031 bool, bool, tree *);
1032 static void c_parser_static_assert_declaration_no_semi (c_parser *);
1033 static void c_parser_static_assert_declaration (c_parser *);
1034 static void c_parser_declspecs (c_parser *, struct c_declspecs *, bool, bool,
1036 static struct c_typespec c_parser_enum_specifier (c_parser *);
1037 static struct c_typespec c_parser_struct_or_union_specifier (c_parser *);
1038 static tree c_parser_struct_declaration (c_parser *);
1039 static struct c_typespec c_parser_typeof_specifier (c_parser *);
1040 static struct c_declarator *c_parser_declarator (c_parser *, bool, c_dtr_syn,
1042 static struct c_declarator *c_parser_direct_declarator (c_parser *, bool,
1044 static struct c_declarator *c_parser_direct_declarator_inner (c_parser *,
1046 struct c_declarator *);
1047 static struct c_arg_info *c_parser_parms_declarator (c_parser *, bool, tree);
1048 static struct c_arg_info *c_parser_parms_list_declarator (c_parser *, tree);
1049 static struct c_parm *c_parser_parameter_declaration (c_parser *, tree);
1050 static tree c_parser_simple_asm_expr (c_parser *);
1051 static tree c_parser_attributes (c_parser *);
1052 static struct c_type_name *c_parser_type_name (c_parser *);
1053 static struct c_expr c_parser_initializer (c_parser *);
1054 static struct c_expr c_parser_braced_init (c_parser *, tree, bool);
1055 static void c_parser_initelt (c_parser *, struct obstack *);
1056 static void c_parser_initval (c_parser *, struct c_expr *,
1058 static tree c_parser_compound_statement (c_parser *);
1059 static void c_parser_compound_statement_nostart (c_parser *);
1060 static void c_parser_label (c_parser *);
1061 static void c_parser_statement (c_parser *);
1062 static void c_parser_statement_after_labels (c_parser *);
1063 static void c_parser_if_statement (c_parser *);
1064 static void c_parser_switch_statement (c_parser *);
1065 static void c_parser_while_statement (c_parser *);
1066 static void c_parser_do_statement (c_parser *);
1067 static void c_parser_for_statement (c_parser *);
1068 static tree c_parser_asm_statement (c_parser *);
1069 static tree c_parser_asm_operands (c_parser *, bool);
1070 static tree c_parser_asm_goto_operands (c_parser *);
1071 static tree c_parser_asm_clobbers (c_parser *);
1072 static struct c_expr c_parser_expr_no_commas (c_parser *, struct c_expr *);
1073 static struct c_expr c_parser_conditional_expression (c_parser *,
1075 static struct c_expr c_parser_binary_expression (c_parser *, struct c_expr *);
1076 static struct c_expr c_parser_cast_expression (c_parser *, struct c_expr *);
1077 static struct c_expr c_parser_unary_expression (c_parser *);
1078 static struct c_expr c_parser_sizeof_expression (c_parser *);
1079 static struct c_expr c_parser_alignof_expression (c_parser *);
1080 static struct c_expr c_parser_postfix_expression (c_parser *);
1081 static struct c_expr c_parser_postfix_expression_after_paren_type (c_parser *,
1082 struct c_type_name *,
1084 static struct c_expr c_parser_postfix_expression_after_primary (c_parser *,
1087 static struct c_expr c_parser_expression (c_parser *);
1088 static struct c_expr c_parser_expression_conv (c_parser *);
1089 static VEC(tree,gc) *c_parser_expr_list (c_parser *, bool, bool,
1091 static void c_parser_omp_construct (c_parser *);
1092 static void c_parser_omp_threadprivate (c_parser *);
1093 static void c_parser_omp_barrier (c_parser *);
1094 static void c_parser_omp_flush (c_parser *);
1095 static void c_parser_omp_taskwait (c_parser *);
1097 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
1098 static bool c_parser_pragma (c_parser *, enum pragma_context);
1100 /* These Objective-C parser functions are only ever called when
1101 compiling Objective-C. */
1102 static void c_parser_objc_class_definition (c_parser *, tree);
1103 static void c_parser_objc_class_instance_variables (c_parser *);
1104 static void c_parser_objc_class_declaration (c_parser *);
1105 static void c_parser_objc_alias_declaration (c_parser *);
1106 static void c_parser_objc_protocol_definition (c_parser *, tree);
1107 static bool c_parser_objc_method_type (c_parser *);
1108 static void c_parser_objc_method_definition (c_parser *);
1109 static void c_parser_objc_methodprotolist (c_parser *);
1110 static void c_parser_objc_methodproto (c_parser *);
1111 static tree c_parser_objc_method_decl (c_parser *, bool, tree *);
1112 static tree c_parser_objc_type_name (c_parser *);
1113 static tree c_parser_objc_protocol_refs (c_parser *);
1114 static void c_parser_objc_try_catch_statement (c_parser *);
1115 static void c_parser_objc_synchronized_statement (c_parser *);
1116 static tree c_parser_objc_selector (c_parser *);
1117 static tree c_parser_objc_selector_arg (c_parser *);
1118 static tree c_parser_objc_receiver (c_parser *);
1119 static tree c_parser_objc_message_args (c_parser *);
1120 static tree c_parser_objc_keywordexpr (c_parser *);
1121 static void c_parser_objc_at_property_declaration (c_parser *);
1122 static void c_parser_objc_at_synthesize_declaration (c_parser *);
1123 static void c_parser_objc_at_dynamic_declaration (c_parser *);
1124 static bool c_parser_objc_diagnose_bad_element_prefix
1125 (c_parser *, struct c_declspecs *);
1127 /* Parse a translation unit (C90 6.7, C99 6.9).
1130 external-declarations
1132 external-declarations:
1133 external-declaration
1134 external-declarations external-declaration
1143 c_parser_translation_unit (c_parser *parser)
1145 if (c_parser_next_token_is (parser, CPP_EOF))
1147 pedwarn (c_parser_peek_token (parser)->location, OPT_pedantic,
1148 "ISO C forbids an empty translation unit");
1152 void *obstack_position = obstack_alloc (&parser_obstack, 0);
1153 mark_valid_location_for_stdc_pragma (false);
1157 c_parser_external_declaration (parser);
1158 obstack_free (&parser_obstack, obstack_position);
1160 while (c_parser_next_token_is_not (parser, CPP_EOF));
1164 /* Parse an external declaration (C90 6.7, C99 6.9).
1166 external-declaration:
1172 external-declaration:
1175 __extension__ external-declaration
1179 external-declaration:
1180 objc-class-definition
1181 objc-class-declaration
1182 objc-alias-declaration
1183 objc-protocol-definition
1184 objc-method-definition
1189 c_parser_external_declaration (c_parser *parser)
1192 switch (c_parser_peek_token (parser)->type)
1195 switch (c_parser_peek_token (parser)->keyword)
1198 ext = disable_extension_diagnostics ();
1199 c_parser_consume_token (parser);
1200 c_parser_external_declaration (parser);
1201 restore_extension_diagnostics (ext);
1204 c_parser_asm_definition (parser);
1206 case RID_AT_INTERFACE:
1207 case RID_AT_IMPLEMENTATION:
1208 gcc_assert (c_dialect_objc ());
1209 c_parser_objc_class_definition (parser, NULL_TREE);
1212 gcc_assert (c_dialect_objc ());
1213 c_parser_objc_class_declaration (parser);
1216 gcc_assert (c_dialect_objc ());
1217 c_parser_objc_alias_declaration (parser);
1219 case RID_AT_PROTOCOL:
1220 gcc_assert (c_dialect_objc ());
1221 c_parser_objc_protocol_definition (parser, NULL_TREE);
1223 case RID_AT_PROPERTY:
1224 gcc_assert (c_dialect_objc ());
1225 c_parser_objc_at_property_declaration (parser);
1227 case RID_AT_SYNTHESIZE:
1228 gcc_assert (c_dialect_objc ());
1229 c_parser_objc_at_synthesize_declaration (parser);
1231 case RID_AT_DYNAMIC:
1232 gcc_assert (c_dialect_objc ());
1233 c_parser_objc_at_dynamic_declaration (parser);
1236 gcc_assert (c_dialect_objc ());
1237 c_parser_consume_token (parser);
1238 objc_finish_implementation ();
1245 pedwarn (c_parser_peek_token (parser)->location, OPT_pedantic,
1246 "ISO C does not allow extra %<;%> outside of a function");
1247 c_parser_consume_token (parser);
1250 mark_valid_location_for_stdc_pragma (true);
1251 c_parser_pragma (parser, pragma_external);
1252 mark_valid_location_for_stdc_pragma (false);
1256 if (c_dialect_objc ())
1258 c_parser_objc_method_definition (parser);
1261 /* Else fall through, and yield a syntax error trying to parse
1262 as a declaration or function definition. */
1265 /* A declaration or a function definition (or, in Objective-C,
1266 an @interface or @protocol with prefix attributes). We can
1267 only tell which after parsing the declaration specifiers, if
1268 any, and the first declarator. */
1269 c_parser_declaration_or_fndef (parser, true, true, true, false, true, NULL);
1274 /* Parse a declaration or function definition (C90 6.5, 6.7.1, C99
1275 6.7, 6.9.1). If FNDEF_OK is true, a function definition is
1276 accepted; otherwise (old-style parameter declarations) only other
1277 declarations are accepted. If STATIC_ASSERT_OK is true, a static
1278 assertion is accepted; otherwise (old-style parameter declarations)
1279 it is not. If NESTED is true, we are inside a function or parsing
1280 old-style parameter declarations; any functions encountered are
1281 nested functions and declaration specifiers are required; otherwise
1282 we are at top level and functions are normal functions and
1283 declaration specifiers may be optional. If EMPTY_OK is true, empty
1284 declarations are OK (subject to all other constraints); otherwise
1285 (old-style parameter declarations) they are diagnosed. If
1286 START_ATTR_OK is true, the declaration specifiers may start with
1287 attributes; otherwise they may not.
1288 OBJC_FOREACH_OBJECT_DECLARATION can be used to get back the parsed
1289 declaration when parsing an Objective-C foreach statement.
1292 declaration-specifiers init-declarator-list[opt] ;
1293 static_assert-declaration
1295 function-definition:
1296 declaration-specifiers[opt] declarator declaration-list[opt]
1301 declaration-list declaration
1303 init-declarator-list:
1305 init-declarator-list , init-declarator
1308 declarator simple-asm-expr[opt] attributes[opt]
1309 declarator simple-asm-expr[opt] attributes[opt] = initializer
1313 nested-function-definition:
1314 declaration-specifiers declarator declaration-list[opt]
1318 attributes objc-class-definition
1319 attributes objc-category-definition
1320 attributes objc-protocol-definition
1322 The simple-asm-expr and attributes are GNU extensions.
1324 This function does not handle __extension__; that is handled in its
1325 callers. ??? Following the old parser, __extension__ may start
1326 external declarations, declarations in functions and declarations
1327 at the start of "for" loops, but not old-style parameter
1330 C99 requires declaration specifiers in a function definition; the
1331 absence is diagnosed through the diagnosis of implicit int. In GNU
1332 C we also allow but diagnose declarations without declaration
1333 specifiers, but only at top level (elsewhere they conflict with
1336 In Objective-C, declarations of the looping variable in a foreach
1337 statement are exceptionally terminated by 'in' (for example, 'for
1338 (NSObject *object in array) { ... }').
1343 threadprivate-directive */
1346 c_parser_declaration_or_fndef (c_parser *parser, bool fndef_ok,
1347 bool static_assert_ok, bool empty_ok,
1348 bool nested, bool start_attr_ok,
1349 tree *objc_foreach_object_declaration)
1351 struct c_declspecs *specs;
1353 tree all_prefix_attrs;
1354 bool diagnosed_no_specs = false;
1355 location_t here = c_parser_peek_token (parser)->location;
1357 if (static_assert_ok
1358 && c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT))
1360 c_parser_static_assert_declaration (parser);
1363 specs = build_null_declspecs ();
1365 /* Try to detect an unknown type name when we have "A B" or "A *B". */
1366 if (c_parser_peek_token (parser)->type == CPP_NAME
1367 && c_parser_peek_token (parser)->id_kind == C_ID_ID
1368 && (c_parser_peek_2nd_token (parser)->type == CPP_NAME
1369 || c_parser_peek_2nd_token (parser)->type == CPP_MULT)
1370 && (!nested || !lookup_name (c_parser_peek_token (parser)->value)))
1372 error_at (here, "unknown type name %qE",
1373 c_parser_peek_token (parser)->value);
1375 /* Parse declspecs normally to get a correct pointer type, but avoid
1376 a further "fails to be a type name" error. */
1377 c_parser_peek_token (parser)->type = CPP_KEYWORD;
1378 c_parser_peek_token (parser)->keyword = RID_VOID;
1379 c_parser_peek_token (parser)->value = error_mark_node;
1382 c_parser_declspecs (parser, specs, true, true, start_attr_ok);
1385 c_parser_skip_to_end_of_block_or_statement (parser);
1388 if (nested && !specs->declspecs_seen_p)
1390 c_parser_error (parser, "expected declaration specifiers");
1391 c_parser_skip_to_end_of_block_or_statement (parser);
1394 finish_declspecs (specs);
1395 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1401 shadow_tag_warned (specs, 1);
1402 pedwarn (here, 0, "empty declaration");
1404 c_parser_consume_token (parser);
1407 else if (c_dialect_objc ())
1409 /* Prefix attributes are an error on method decls. */
1410 switch (c_parser_peek_token (parser)->type)
1414 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1418 warning_at (c_parser_peek_token (parser)->location,
1420 "prefix attributes are ignored for methods");
1421 specs->attrs = NULL_TREE;
1424 c_parser_objc_method_definition (parser);
1426 c_parser_objc_methodproto (parser);
1432 /* This is where we parse 'attributes @interface ...',
1433 'attributes @implementation ...', 'attributes @protocol ...'
1434 (where attributes could be, for example, __attribute__
1437 switch (c_parser_peek_token (parser)->keyword)
1439 case RID_AT_INTERFACE:
1441 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1443 c_parser_objc_class_definition (parser, specs->attrs);
1447 case RID_AT_IMPLEMENTATION:
1449 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1453 warning_at (c_parser_peek_token (parser)->location,
1455 "prefix attributes are ignored for implementations");
1456 specs->attrs = NULL_TREE;
1458 c_parser_objc_class_definition (parser, NULL_TREE);
1462 case RID_AT_PROTOCOL:
1464 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1466 c_parser_objc_protocol_definition (parser, specs->attrs);
1473 case RID_AT_PROPERTY:
1476 c_parser_error (parser,
1477 "attributes may not be specified before" );
1478 specs->attrs = NULL;
1486 pending_xref_error ();
1487 prefix_attrs = specs->attrs;
1488 all_prefix_attrs = prefix_attrs;
1489 specs->attrs = NULL_TREE;
1492 struct c_declarator *declarator;
1495 /* Declaring either one or more declarators (in which case we
1496 should diagnose if there were no declaration specifiers) or a
1497 function definition (in which case the diagnostic for
1498 implicit int suffices). */
1499 declarator = c_parser_declarator (parser,
1500 specs->typespec_kind != ctsk_none,
1501 C_DTR_NORMAL, &dummy);
1502 if (declarator == NULL)
1504 c_parser_skip_to_end_of_block_or_statement (parser);
1507 if (c_parser_next_token_is (parser, CPP_EQ)
1508 || c_parser_next_token_is (parser, CPP_COMMA)
1509 || c_parser_next_token_is (parser, CPP_SEMICOLON)
1510 || c_parser_next_token_is_keyword (parser, RID_ASM)
1511 || c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE)
1512 || c_parser_next_token_is_keyword (parser, RID_IN))
1514 tree asm_name = NULL_TREE;
1515 tree postfix_attrs = NULL_TREE;
1516 if (!diagnosed_no_specs && !specs->declspecs_seen_p)
1518 diagnosed_no_specs = true;
1519 pedwarn (here, 0, "data definition has no type or storage class");
1521 /* Having seen a data definition, there cannot now be a
1522 function definition. */
1524 if (c_parser_next_token_is_keyword (parser, RID_ASM))
1525 asm_name = c_parser_simple_asm_expr (parser);
1526 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
1527 postfix_attrs = c_parser_attributes (parser);
1528 if (c_parser_next_token_is (parser, CPP_EQ))
1532 location_t init_loc;
1533 c_parser_consume_token (parser);
1534 /* The declaration of the variable is in effect while
1535 its initializer is parsed. */
1536 d = start_decl (declarator, specs, true,
1537 chainon (postfix_attrs, all_prefix_attrs));
1539 d = error_mark_node;
1540 start_init (d, asm_name, global_bindings_p ());
1541 init_loc = c_parser_peek_token (parser)->location;
1542 init = c_parser_initializer (parser);
1544 if (d != error_mark_node)
1546 maybe_warn_string_init (TREE_TYPE (d), init);
1547 finish_decl (d, init_loc, init.value,
1548 init.original_type, asm_name);
1553 tree d = start_decl (declarator, specs, false,
1554 chainon (postfix_attrs,
1557 finish_decl (d, UNKNOWN_LOCATION, NULL_TREE,
1558 NULL_TREE, asm_name);
1560 if (c_parser_next_token_is_keyword (parser, RID_IN))
1563 *objc_foreach_object_declaration = d;
1565 *objc_foreach_object_declaration = error_mark_node;
1568 if (c_parser_next_token_is (parser, CPP_COMMA))
1570 c_parser_consume_token (parser);
1571 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
1572 all_prefix_attrs = chainon (c_parser_attributes (parser),
1575 all_prefix_attrs = prefix_attrs;
1578 else if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1580 c_parser_consume_token (parser);
1583 else if (c_parser_next_token_is_keyword (parser, RID_IN))
1585 /* This can only happen in Objective-C: we found the
1586 'in' that terminates the declaration inside an
1587 Objective-C foreach statement. Do not consume the
1588 token, so that the caller can use it to determine
1589 that this indeed is a foreach context. */
1594 c_parser_error (parser, "expected %<,%> or %<;%>");
1595 c_parser_skip_to_end_of_block_or_statement (parser);
1601 c_parser_error (parser, "expected %<=%>, %<,%>, %<;%>, "
1602 "%<asm%> or %<__attribute__%>");
1603 c_parser_skip_to_end_of_block_or_statement (parser);
1606 /* Function definition (nested or otherwise). */
1609 pedwarn (here, OPT_pedantic, "ISO C forbids nested functions");
1610 c_push_function_context ();
1612 if (!start_function (specs, declarator, all_prefix_attrs))
1614 /* This can appear in many cases looking nothing like a
1615 function definition, so we don't give a more specific
1616 error suggesting there was one. */
1617 c_parser_error (parser, "expected %<=%>, %<,%>, %<;%>, %<asm%> "
1618 "or %<__attribute__%>");
1620 c_pop_function_context ();
1623 /* Parse old-style parameter declarations. ??? Attributes are
1624 not allowed to start declaration specifiers here because of a
1625 syntax conflict between a function declaration with attribute
1626 suffix and a function definition with an attribute prefix on
1627 first old-style parameter declaration. Following the old
1628 parser, they are not accepted on subsequent old-style
1629 parameter declarations either. However, there is no
1630 ambiguity after the first declaration, nor indeed on the
1631 first as long as we don't allow postfix attributes after a
1632 declarator with a nonempty identifier list in a definition;
1633 and postfix attributes have never been accepted here in
1634 function definitions either. */
1635 while (c_parser_next_token_is_not (parser, CPP_EOF)
1636 && c_parser_next_token_is_not (parser, CPP_OPEN_BRACE))
1637 c_parser_declaration_or_fndef (parser, false, false, false,
1639 store_parm_decls ();
1640 DECL_STRUCT_FUNCTION (current_function_decl)->function_start_locus
1641 = c_parser_peek_token (parser)->location;
1642 fnbody = c_parser_compound_statement (parser);
1645 tree decl = current_function_decl;
1646 /* Mark nested functions as needing static-chain initially.
1647 lower_nested_functions will recompute it but the
1648 DECL_STATIC_CHAIN flag is also used before that happens,
1649 by initializer_constant_valid_p. See gcc.dg/nested-fn-2.c. */
1650 DECL_STATIC_CHAIN (decl) = 1;
1653 c_pop_function_context ();
1654 add_stmt (build_stmt (DECL_SOURCE_LOCATION (decl), DECL_EXPR, decl));
1665 /* Parse an asm-definition (asm() outside a function body). This is a
1673 c_parser_asm_definition (c_parser *parser)
1675 tree asm_str = c_parser_simple_asm_expr (parser);
1677 cgraph_add_asm_node (asm_str);
1678 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
1681 /* Parse a static assertion (C1X N1425 6.7.10).
1683 static_assert-declaration:
1684 static_assert-declaration-no-semi ;
1688 c_parser_static_assert_declaration (c_parser *parser)
1690 c_parser_static_assert_declaration_no_semi (parser);
1692 || !c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
1693 c_parser_skip_to_end_of_block_or_statement (parser);
1696 /* Parse a static assertion (C1X N1425 6.7.10), without the trailing
1699 static_assert-declaration-no-semi:
1700 _Static_assert ( constant-expression , string-literal )
1704 c_parser_static_assert_declaration_no_semi (c_parser *parser)
1706 location_t assert_loc, value_loc;
1710 gcc_assert (c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT));
1711 assert_loc = c_parser_peek_token (parser)->location;
1715 pedwarn (assert_loc, OPT_pedantic,
1716 "ISO C99 does not support %<_Static_assert%>");
1718 pedwarn (assert_loc, OPT_pedantic,
1719 "ISO C90 does not support %<_Static_assert%>");
1721 c_parser_consume_token (parser);
1722 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
1724 value_loc = c_parser_peek_token (parser)->location;
1725 value = c_parser_expr_no_commas (parser, NULL).value;
1726 parser->lex_untranslated_string = true;
1727 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
1729 parser->lex_untranslated_string = false;
1732 switch (c_parser_peek_token (parser)->type)
1738 case CPP_UTF8STRING:
1739 string = c_parser_peek_token (parser)->value;
1740 c_parser_consume_token (parser);
1741 parser->lex_untranslated_string = false;
1744 c_parser_error (parser, "expected string literal");
1745 parser->lex_untranslated_string = false;
1748 c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
1750 if (!INTEGRAL_TYPE_P (TREE_TYPE (value)))
1752 error_at (value_loc, "expression in static assertion is not an integer");
1755 if (TREE_CODE (value) != INTEGER_CST)
1757 value = c_fully_fold (value, false, NULL);
1758 if (TREE_CODE (value) == INTEGER_CST)
1759 pedwarn (value_loc, OPT_pedantic, "expression in static assertion "
1760 "is not an integer constant expression");
1762 if (TREE_CODE (value) != INTEGER_CST)
1764 error_at (value_loc, "expression in static assertion is not constant");
1767 constant_expression_warning (value);
1768 if (integer_zerop (value))
1769 error_at (assert_loc, "static assertion failed: %E", string);
1772 /* Parse some declaration specifiers (possibly none) (C90 6.5, C99
1773 6.7), adding them to SPECS (which may already include some).
1774 Storage class specifiers are accepted iff SCSPEC_OK; type
1775 specifiers are accepted iff TYPESPEC_OK; attributes are accepted at
1776 the start iff START_ATTR_OK.
1778 declaration-specifiers:
1779 storage-class-specifier declaration-specifiers[opt]
1780 type-specifier declaration-specifiers[opt]
1781 type-qualifier declaration-specifiers[opt]
1782 function-specifier declaration-specifiers[opt]
1784 Function specifiers (inline) are from C99, and are currently
1785 handled as storage class specifiers, as is __thread.
1787 C90 6.5.1, C99 6.7.1:
1788 storage-class-specifier:
1799 C90 6.5.2, C99 6.7.2:
1812 [_Imaginary removed in C99 TC2]
1813 struct-or-union-specifier
1817 (_Bool and _Complex are new in C99.)
1819 C90 6.5.3, C99 6.7.3:
1825 address-space-qualifier
1827 (restrict is new in C99.)
1831 declaration-specifiers:
1832 attributes declaration-specifiers[opt]
1838 identifier recognized by the target
1840 storage-class-specifier:
1853 (_Fract, _Accum, and _Sat are new from ISO/IEC DTR 18037:
1854 http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1169.pdf)
1859 class-name objc-protocol-refs[opt]
1860 typedef-name objc-protocol-refs
1865 c_parser_declspecs (c_parser *parser, struct c_declspecs *specs,
1866 bool scspec_ok, bool typespec_ok, bool start_attr_ok)
1868 bool attrs_ok = start_attr_ok;
1869 bool seen_type = specs->typespec_kind != ctsk_none;
1870 while (c_parser_next_token_is (parser, CPP_NAME)
1871 || c_parser_next_token_is (parser, CPP_KEYWORD)
1872 || (c_dialect_objc () && c_parser_next_token_is (parser, CPP_LESS)))
1874 struct c_typespec t;
1876 location_t loc = c_parser_peek_token (parser)->location;
1877 if (c_parser_next_token_is (parser, CPP_NAME))
1879 tree value = c_parser_peek_token (parser)->value;
1880 c_id_kind kind = c_parser_peek_token (parser)->id_kind;
1882 if (kind == C_ID_ADDRSPACE)
1885 = c_parser_peek_token (parser)->keyword - RID_FIRST_ADDR_SPACE;
1886 declspecs_add_addrspace (specs, as);
1887 c_parser_consume_token (parser);
1892 /* This finishes the specifiers unless a type name is OK, it
1893 is declared as a type name and a type name hasn't yet
1895 if (!typespec_ok || seen_type
1896 || (kind != C_ID_TYPENAME && kind != C_ID_CLASSNAME))
1898 c_parser_consume_token (parser);
1901 if (kind == C_ID_TYPENAME
1902 && (!c_dialect_objc ()
1903 || c_parser_next_token_is_not (parser, CPP_LESS)))
1905 t.kind = ctsk_typedef;
1906 /* For a typedef name, record the meaning, not the name.
1907 In case of 'foo foo, bar;'. */
1908 t.spec = lookup_name (value);
1910 t.expr_const_operands = true;
1914 tree proto = NULL_TREE;
1915 gcc_assert (c_dialect_objc ());
1917 if (c_parser_next_token_is (parser, CPP_LESS))
1918 proto = c_parser_objc_protocol_refs (parser);
1919 t.spec = objc_get_protocol_qualified_type (value, proto);
1921 t.expr_const_operands = true;
1923 declspecs_add_type (loc, specs, t);
1926 if (c_parser_next_token_is (parser, CPP_LESS))
1928 /* Make "<SomeProtocol>" equivalent to "id <SomeProtocol>" -
1929 nisse@lysator.liu.se. */
1931 gcc_assert (c_dialect_objc ());
1932 if (!typespec_ok || seen_type)
1934 proto = c_parser_objc_protocol_refs (parser);
1936 t.spec = objc_get_protocol_qualified_type (NULL_TREE, proto);
1938 t.expr_const_operands = true;
1939 declspecs_add_type (loc, specs, t);
1942 gcc_assert (c_parser_next_token_is (parser, CPP_KEYWORD));
1943 switch (c_parser_peek_token (parser)->keyword)
1955 /* TODO: Distinguish between function specifiers (inline)
1956 and storage class specifiers, either here or in
1957 declspecs_add_scspec. */
1958 declspecs_add_scspec (specs, c_parser_peek_token (parser)->value);
1959 c_parser_consume_token (parser);
1983 if (c_dialect_objc ())
1984 parser->objc_need_raw_identifier = true;
1985 t.kind = ctsk_resword;
1986 t.spec = c_parser_peek_token (parser)->value;
1988 t.expr_const_operands = true;
1989 declspecs_add_type (loc, specs, t);
1990 c_parser_consume_token (parser);
1997 t = c_parser_enum_specifier (parser);
1998 declspecs_add_type (loc, specs, t);
2006 t = c_parser_struct_or_union_specifier (parser);
2007 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, t.spec);
2008 declspecs_add_type (loc, specs, t);
2011 /* ??? The old parser rejected typeof after other type
2012 specifiers, but is a syntax error the best way of
2014 if (!typespec_ok || seen_type)
2018 t = c_parser_typeof_specifier (parser);
2019 declspecs_add_type (loc, specs, t);
2025 declspecs_add_qual (specs, c_parser_peek_token (parser)->value);
2026 c_parser_consume_token (parser);
2031 attrs = c_parser_attributes (parser);
2032 declspecs_add_attrs (specs, attrs);
2041 /* Parse an enum specifier (C90 6.5.2.2, C99 6.7.2.2).
2044 enum attributes[opt] identifier[opt] { enumerator-list } attributes[opt]
2045 enum attributes[opt] identifier[opt] { enumerator-list , } attributes[opt]
2046 enum attributes[opt] identifier
2048 The form with trailing comma is new in C99. The forms with
2049 attributes are GNU extensions. In GNU C, we accept any expression
2050 without commas in the syntax (assignment expressions, not just
2051 conditional expressions); assignment expressions will be diagnosed
2056 enumerator-list , enumerator
2059 enumeration-constant
2060 enumeration-constant = constant-expression
2063 static struct c_typespec
2064 c_parser_enum_specifier (c_parser *parser)
2066 struct c_typespec ret;
2068 tree ident = NULL_TREE;
2069 location_t enum_loc;
2070 location_t ident_loc = UNKNOWN_LOCATION; /* Quiet warning. */
2071 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ENUM));
2072 enum_loc = c_parser_peek_token (parser)->location;
2073 c_parser_consume_token (parser);
2074 attrs = c_parser_attributes (parser);
2075 enum_loc = c_parser_peek_token (parser)->location;
2076 /* Set the location in case we create a decl now. */
2077 c_parser_set_source_position_from_token (c_parser_peek_token (parser));
2078 if (c_parser_next_token_is (parser, CPP_NAME))
2080 ident = c_parser_peek_token (parser)->value;
2081 ident_loc = c_parser_peek_token (parser)->location;
2082 enum_loc = ident_loc;
2083 c_parser_consume_token (parser);
2085 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
2087 /* Parse an enum definition. */
2088 struct c_enum_contents the_enum;
2089 tree type = start_enum (enum_loc, &the_enum, ident);
2091 /* We chain the enumerators in reverse order, then put them in
2092 forward order at the end. */
2093 tree values = NULL_TREE;
2094 c_parser_consume_token (parser);
2102 location_t comma_loc = UNKNOWN_LOCATION; /* Quiet warning. */
2103 location_t decl_loc, value_loc;
2104 if (c_parser_next_token_is_not (parser, CPP_NAME))
2106 c_parser_error (parser, "expected identifier");
2107 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
2108 values = error_mark_node;
2111 token = c_parser_peek_token (parser);
2112 enum_id = token->value;
2113 /* Set the location in case we create a decl now. */
2114 c_parser_set_source_position_from_token (token);
2115 decl_loc = value_loc = token->location;
2116 c_parser_consume_token (parser);
2117 if (c_parser_next_token_is (parser, CPP_EQ))
2119 c_parser_consume_token (parser);
2120 value_loc = c_parser_peek_token (parser)->location;
2121 enum_value = c_parser_expr_no_commas (parser, NULL).value;
2124 enum_value = NULL_TREE;
2125 enum_decl = build_enumerator (decl_loc, value_loc,
2126 &the_enum, enum_id, enum_value);
2127 TREE_CHAIN (enum_decl) = values;
2130 if (c_parser_next_token_is (parser, CPP_COMMA))
2132 comma_loc = c_parser_peek_token (parser)->location;
2134 c_parser_consume_token (parser);
2136 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2138 if (seen_comma && !flag_isoc99)
2139 pedwarn (comma_loc, OPT_pedantic, "comma at end of enumerator list");
2140 c_parser_consume_token (parser);
2145 c_parser_error (parser, "expected %<,%> or %<}%>");
2146 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
2147 values = error_mark_node;
2151 postfix_attrs = c_parser_attributes (parser);
2152 ret.spec = finish_enum (type, nreverse (values),
2153 chainon (attrs, postfix_attrs));
2154 ret.kind = ctsk_tagdef;
2155 ret.expr = NULL_TREE;
2156 ret.expr_const_operands = true;
2161 c_parser_error (parser, "expected %<{%>");
2162 ret.spec = error_mark_node;
2163 ret.kind = ctsk_tagref;
2164 ret.expr = NULL_TREE;
2165 ret.expr_const_operands = true;
2168 ret = parser_xref_tag (ident_loc, ENUMERAL_TYPE, ident);
2169 /* In ISO C, enumerated types can be referred to only if already
2171 if (pedantic && !COMPLETE_TYPE_P (ret.spec))
2174 pedwarn (enum_loc, OPT_pedantic,
2175 "ISO C forbids forward references to %<enum%> types");
2180 /* Parse a struct or union specifier (C90 6.5.2.1, C99 6.7.2.1).
2182 struct-or-union-specifier:
2183 struct-or-union attributes[opt] identifier[opt]
2184 { struct-contents } attributes[opt]
2185 struct-or-union attributes[opt] identifier
2188 struct-declaration-list
2190 struct-declaration-list:
2191 struct-declaration ;
2192 struct-declaration-list struct-declaration ;
2199 struct-declaration-list struct-declaration
2201 struct-declaration-list:
2202 struct-declaration-list ;
2205 (Note that in the syntax here, unlike that in ISO C, the semicolons
2206 are included here rather than in struct-declaration, in order to
2207 describe the syntax with extra semicolons and missing semicolon at
2212 struct-declaration-list:
2213 @defs ( class-name )
2215 (Note this does not include a trailing semicolon, but can be
2216 followed by further declarations, and gets a pedwarn-if-pedantic
2217 when followed by a semicolon.) */
2219 static struct c_typespec
2220 c_parser_struct_or_union_specifier (c_parser *parser)
2222 struct c_typespec ret;
2224 tree ident = NULL_TREE;
2225 location_t struct_loc;
2226 location_t ident_loc = UNKNOWN_LOCATION;
2227 enum tree_code code;
2228 switch (c_parser_peek_token (parser)->keyword)
2239 struct_loc = c_parser_peek_token (parser)->location;
2240 c_parser_consume_token (parser);
2241 attrs = c_parser_attributes (parser);
2243 /* Set the location in case we create a decl now. */
2244 c_parser_set_source_position_from_token (c_parser_peek_token (parser));
2246 if (c_parser_next_token_is (parser, CPP_NAME))
2248 ident = c_parser_peek_token (parser)->value;
2249 ident_loc = c_parser_peek_token (parser)->location;
2250 struct_loc = ident_loc;
2251 c_parser_consume_token (parser);
2253 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
2255 /* Parse a struct or union definition. Start the scope of the
2256 tag before parsing components. */
2257 struct c_struct_parse_info *struct_info;
2258 tree type = start_struct (struct_loc, code, ident, &struct_info);
2260 /* We chain the components in reverse order, then put them in
2261 forward order at the end. Each struct-declaration may
2262 declare multiple components (comma-separated), so we must use
2263 chainon to join them, although when parsing each
2264 struct-declaration we can use TREE_CHAIN directly.
2266 The theory behind all this is that there will be more
2267 semicolon separated fields than comma separated fields, and
2268 so we'll be minimizing the number of node traversals required
2270 tree contents = NULL_TREE;
2271 c_parser_consume_token (parser);
2272 /* Handle the Objective-C @defs construct,
2273 e.g. foo(sizeof(struct{ @defs(ClassName) }));. */
2274 if (c_parser_next_token_is_keyword (parser, RID_AT_DEFS))
2277 gcc_assert (c_dialect_objc ());
2278 c_parser_consume_token (parser);
2279 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2281 if (c_parser_next_token_is (parser, CPP_NAME)
2282 && c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME)
2284 name = c_parser_peek_token (parser)->value;
2285 c_parser_consume_token (parser);
2289 c_parser_error (parser, "expected class name");
2290 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
2293 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2295 contents = nreverse (objc_get_class_ivars (name));
2298 /* Parse the struct-declarations and semicolons. Problems with
2299 semicolons are diagnosed here; empty structures are diagnosed
2304 /* Parse any stray semicolon. */
2305 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
2307 pedwarn (c_parser_peek_token (parser)->location, OPT_pedantic,
2308 "extra semicolon in struct or union specified");
2309 c_parser_consume_token (parser);
2312 /* Stop if at the end of the struct or union contents. */
2313 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2315 c_parser_consume_token (parser);
2318 /* Accept #pragmas at struct scope. */
2319 if (c_parser_next_token_is (parser, CPP_PRAGMA))
2321 c_parser_pragma (parser, pragma_external);
2324 /* Parse some comma-separated declarations, but not the
2325 trailing semicolon if any. */
2326 decls = c_parser_struct_declaration (parser);
2327 contents = chainon (decls, contents);
2328 /* If no semicolon follows, either we have a parse error or
2329 are at the end of the struct or union and should
2331 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
2332 c_parser_consume_token (parser);
2335 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2336 pedwarn (c_parser_peek_token (parser)->location, 0,
2337 "no semicolon at end of struct or union");
2340 c_parser_error (parser, "expected %<;%>");
2341 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
2346 postfix_attrs = c_parser_attributes (parser);
2347 ret.spec = finish_struct (struct_loc, type, nreverse (contents),
2348 chainon (attrs, postfix_attrs), struct_info);
2349 ret.kind = ctsk_tagdef;
2350 ret.expr = NULL_TREE;
2351 ret.expr_const_operands = true;
2356 c_parser_error (parser, "expected %<{%>");
2357 ret.spec = error_mark_node;
2358 ret.kind = ctsk_tagref;
2359 ret.expr = NULL_TREE;
2360 ret.expr_const_operands = true;
2363 ret = parser_xref_tag (ident_loc, code, ident);
2367 /* Parse a struct-declaration (C90 6.5.2.1, C99 6.7.2.1), *without*
2368 the trailing semicolon.
2371 specifier-qualifier-list struct-declarator-list
2372 static_assert-declaration-no-semi
2374 specifier-qualifier-list:
2375 type-specifier specifier-qualifier-list[opt]
2376 type-qualifier specifier-qualifier-list[opt]
2377 attributes specifier-qualifier-list[opt]
2379 struct-declarator-list:
2381 struct-declarator-list , attributes[opt] struct-declarator
2384 declarator attributes[opt]
2385 declarator[opt] : constant-expression attributes[opt]
2390 __extension__ struct-declaration
2391 specifier-qualifier-list
2393 Unlike the ISO C syntax, semicolons are handled elsewhere. The use
2394 of attributes where shown is a GNU extension. In GNU C, we accept
2395 any expression without commas in the syntax (assignment
2396 expressions, not just conditional expressions); assignment
2397 expressions will be diagnosed as non-constant. */
2400 c_parser_struct_declaration (c_parser *parser)
2402 struct c_declspecs *specs;
2404 tree all_prefix_attrs;
2406 location_t decl_loc;
2407 if (c_parser_next_token_is_keyword (parser, RID_EXTENSION))
2411 ext = disable_extension_diagnostics ();
2412 c_parser_consume_token (parser);
2413 decl = c_parser_struct_declaration (parser);
2414 restore_extension_diagnostics (ext);
2417 if (c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT))
2419 c_parser_static_assert_declaration_no_semi (parser);
2422 specs = build_null_declspecs ();
2423 decl_loc = c_parser_peek_token (parser)->location;
2424 c_parser_declspecs (parser, specs, false, true, true);
2427 if (!specs->declspecs_seen_p)
2429 c_parser_error (parser, "expected specifier-qualifier-list");
2432 finish_declspecs (specs);
2433 if (c_parser_next_token_is (parser, CPP_SEMICOLON)
2434 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2437 if (specs->typespec_kind == ctsk_none)
2439 pedwarn (decl_loc, OPT_pedantic,
2440 "ISO C forbids member declarations with no members");
2441 shadow_tag_warned (specs, pedantic);
2446 /* Support for unnamed structs or unions as members of
2447 structs or unions (which is [a] useful and [b] supports
2451 ret = grokfield (c_parser_peek_token (parser)->location,
2452 build_id_declarator (NULL_TREE), specs,
2455 decl_attributes (&ret, attrs, 0);
2459 pending_xref_error ();
2460 prefix_attrs = specs->attrs;
2461 all_prefix_attrs = prefix_attrs;
2462 specs->attrs = NULL_TREE;
2466 /* Declaring one or more declarators or un-named bit-fields. */
2467 struct c_declarator *declarator;
2469 if (c_parser_next_token_is (parser, CPP_COLON))
2470 declarator = build_id_declarator (NULL_TREE);
2472 declarator = c_parser_declarator (parser,
2473 specs->typespec_kind != ctsk_none,
2474 C_DTR_NORMAL, &dummy);
2475 if (declarator == NULL)
2477 c_parser_skip_to_end_of_block_or_statement (parser);
2480 if (c_parser_next_token_is (parser, CPP_COLON)
2481 || c_parser_next_token_is (parser, CPP_COMMA)
2482 || c_parser_next_token_is (parser, CPP_SEMICOLON)
2483 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE)
2484 || c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2486 tree postfix_attrs = NULL_TREE;
2487 tree width = NULL_TREE;
2489 if (c_parser_next_token_is (parser, CPP_COLON))
2491 c_parser_consume_token (parser);
2492 width = c_parser_expr_no_commas (parser, NULL).value;
2494 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2495 postfix_attrs = c_parser_attributes (parser);
2496 d = grokfield (c_parser_peek_token (parser)->location,
2497 declarator, specs, width, &all_prefix_attrs);
2498 decl_attributes (&d, chainon (postfix_attrs,
2499 all_prefix_attrs), 0);
2500 DECL_CHAIN (d) = decls;
2502 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2503 all_prefix_attrs = chainon (c_parser_attributes (parser),
2506 all_prefix_attrs = prefix_attrs;
2507 if (c_parser_next_token_is (parser, CPP_COMMA))
2508 c_parser_consume_token (parser);
2509 else if (c_parser_next_token_is (parser, CPP_SEMICOLON)
2510 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2512 /* Semicolon consumed in caller. */
2517 c_parser_error (parser, "expected %<,%>, %<;%> or %<}%>");
2523 c_parser_error (parser,
2524 "expected %<:%>, %<,%>, %<;%>, %<}%> or "
2525 "%<__attribute__%>");
2532 /* Parse a typeof specifier (a GNU extension).
2535 typeof ( expression )
2536 typeof ( type-name )
2539 static struct c_typespec
2540 c_parser_typeof_specifier (c_parser *parser)
2542 struct c_typespec ret;
2543 ret.kind = ctsk_typeof;
2544 ret.spec = error_mark_node;
2545 ret.expr = NULL_TREE;
2546 ret.expr_const_operands = true;
2547 gcc_assert (c_parser_next_token_is_keyword (parser, RID_TYPEOF));
2548 c_parser_consume_token (parser);
2549 c_inhibit_evaluation_warnings++;
2551 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2553 c_inhibit_evaluation_warnings--;
2557 if (c_parser_next_token_starts_typename (parser))
2559 struct c_type_name *type = c_parser_type_name (parser);
2560 c_inhibit_evaluation_warnings--;
2564 ret.spec = groktypename (type, &ret.expr, &ret.expr_const_operands);
2565 pop_maybe_used (variably_modified_type_p (ret.spec, NULL_TREE));
2571 location_t here = c_parser_peek_token (parser)->location;
2572 struct c_expr expr = c_parser_expression (parser);
2573 c_inhibit_evaluation_warnings--;
2575 if (TREE_CODE (expr.value) == COMPONENT_REF
2576 && DECL_C_BIT_FIELD (TREE_OPERAND (expr.value, 1)))
2577 error_at (here, "%<typeof%> applied to a bit-field");
2578 mark_exp_read (expr.value);
2579 ret.spec = TREE_TYPE (expr.value);
2580 if (c_dialect_objc()
2581 && ret.spec != error_mark_node
2582 && lookup_attribute ("objc_volatilized", TYPE_ATTRIBUTES (ret.spec)))
2583 ret.spec = build_qualified_type
2584 (ret.spec, (TYPE_QUALS (ret.spec) & ~TYPE_QUAL_VOLATILE));
2585 was_vm = variably_modified_type_p (ret.spec, NULL_TREE);
2586 /* This is returned with the type so that when the type is
2587 evaluated, this can be evaluated. */
2589 ret.expr = c_fully_fold (expr.value, false, &ret.expr_const_operands);
2590 pop_maybe_used (was_vm);
2592 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
2596 /* Parse a declarator, possibly an abstract declarator (C90 6.5.4,
2597 6.5.5, C99 6.7.5, 6.7.6). If TYPE_SEEN_P then a typedef name may
2598 be redeclared; otherwise it may not. KIND indicates which kind of
2599 declarator is wanted. Returns a valid declarator except in the
2600 case of a syntax error in which case NULL is returned. *SEEN_ID is
2601 set to true if an identifier being declared is seen; this is used
2602 to diagnose bad forms of abstract array declarators and to
2603 determine whether an identifier list is syntactically permitted.
2606 pointer[opt] direct-declarator
2610 ( attributes[opt] declarator )
2611 direct-declarator array-declarator
2612 direct-declarator ( parameter-type-list )
2613 direct-declarator ( identifier-list[opt] )
2616 * type-qualifier-list[opt]
2617 * type-qualifier-list[opt] pointer
2619 type-qualifier-list:
2622 type-qualifier-list type-qualifier
2623 type-qualifier-list attributes
2625 parameter-type-list:
2627 parameter-list , ...
2630 parameter-declaration
2631 parameter-list , parameter-declaration
2633 parameter-declaration:
2634 declaration-specifiers declarator attributes[opt]
2635 declaration-specifiers abstract-declarator[opt] attributes[opt]
2639 identifier-list , identifier
2641 abstract-declarator:
2643 pointer[opt] direct-abstract-declarator
2645 direct-abstract-declarator:
2646 ( attributes[opt] abstract-declarator )
2647 direct-abstract-declarator[opt] array-declarator
2648 direct-abstract-declarator[opt] ( parameter-type-list[opt] )
2653 direct-declarator ( parameter-forward-declarations
2654 parameter-type-list[opt] )
2656 direct-abstract-declarator:
2657 direct-abstract-declarator[opt] ( parameter-forward-declarations
2658 parameter-type-list[opt] )
2660 parameter-forward-declarations:
2662 parameter-forward-declarations parameter-list ;
2664 The uses of attributes shown above are GNU extensions.
2666 Some forms of array declarator are not included in C99 in the
2667 syntax for abstract declarators; these are disallowed elsewhere.
2668 This may be a defect (DR#289).
2670 This function also accepts an omitted abstract declarator as being
2671 an abstract declarator, although not part of the formal syntax. */
2673 static struct c_declarator *
2674 c_parser_declarator (c_parser *parser, bool type_seen_p, c_dtr_syn kind,
2677 /* Parse any initial pointer part. */
2678 if (c_parser_next_token_is (parser, CPP_MULT))
2680 struct c_declspecs *quals_attrs = build_null_declspecs ();
2681 struct c_declarator *inner;
2682 c_parser_consume_token (parser);
2683 c_parser_declspecs (parser, quals_attrs, false, false, true);
2684 inner = c_parser_declarator (parser, type_seen_p, kind, seen_id);
2688 return make_pointer_declarator (quals_attrs, inner);
2690 /* Now we have a direct declarator, direct abstract declarator or
2691 nothing (which counts as a direct abstract declarator here). */
2692 return c_parser_direct_declarator (parser, type_seen_p, kind, seen_id);
2695 /* Parse a direct declarator or direct abstract declarator; arguments
2696 as c_parser_declarator. */
2698 static struct c_declarator *
2699 c_parser_direct_declarator (c_parser *parser, bool type_seen_p, c_dtr_syn kind,
2702 /* The direct declarator must start with an identifier (possibly
2703 omitted) or a parenthesized declarator (possibly abstract). In
2704 an ordinary declarator, initial parentheses must start a
2705 parenthesized declarator. In an abstract declarator or parameter
2706 declarator, they could start a parenthesized declarator or a
2707 parameter list. To tell which, the open parenthesis and any
2708 following attributes must be read. If a declaration specifier
2709 follows, then it is a parameter list; if the specifier is a
2710 typedef name, there might be an ambiguity about redeclaring it,
2711 which is resolved in the direction of treating it as a typedef
2712 name. If a close parenthesis follows, it is also an empty
2713 parameter list, as the syntax does not permit empty abstract
2714 declarators. Otherwise, it is a parenthesized declarator (in
2715 which case the analysis may be repeated inside it, recursively).
2717 ??? There is an ambiguity in a parameter declaration "int
2718 (__attribute__((foo)) x)", where x is not a typedef name: it
2719 could be an abstract declarator for a function, or declare x with
2720 parentheses. The proper resolution of this ambiguity needs
2721 documenting. At present we follow an accident of the old
2722 parser's implementation, whereby the first parameter must have
2723 some declaration specifiers other than just attributes. Thus as
2724 a parameter declaration it is treated as a parenthesized
2725 parameter named x, and as an abstract declarator it is
2728 ??? Also following the old parser, attributes inside an empty
2729 parameter list are ignored, making it a list not yielding a
2730 prototype, rather than giving an error or making it have one
2731 parameter with implicit type int.
2733 ??? Also following the old parser, typedef names may be
2734 redeclared in declarators, but not Objective-C class names. */
2736 if (kind != C_DTR_ABSTRACT
2737 && c_parser_next_token_is (parser, CPP_NAME)
2739 && (c_parser_peek_token (parser)->id_kind == C_ID_TYPENAME
2740 || c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME))
2741 || c_parser_peek_token (parser)->id_kind == C_ID_ID))
2743 struct c_declarator *inner
2744 = build_id_declarator (c_parser_peek_token (parser)->value);
2746 inner->id_loc = c_parser_peek_token (parser)->location;
2747 c_parser_consume_token (parser);
2748 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
2751 if (kind != C_DTR_NORMAL
2752 && c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
2754 struct c_declarator *inner = build_id_declarator (NULL_TREE);
2755 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
2758 /* Either we are at the end of an abstract declarator, or we have
2761 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
2764 struct c_declarator *inner;
2765 c_parser_consume_token (parser);
2766 attrs = c_parser_attributes (parser);
2767 if (kind != C_DTR_NORMAL
2768 && (c_parser_next_token_starts_declspecs (parser)
2769 || c_parser_next_token_is (parser, CPP_CLOSE_PAREN)))
2771 struct c_arg_info *args
2772 = c_parser_parms_declarator (parser, kind == C_DTR_NORMAL,
2779 = build_function_declarator (args,
2780 build_id_declarator (NULL_TREE));
2781 return c_parser_direct_declarator_inner (parser, *seen_id,
2785 /* A parenthesized declarator. */
2786 inner = c_parser_declarator (parser, type_seen_p, kind, seen_id);
2787 if (inner != NULL && attrs != NULL)
2788 inner = build_attrs_declarator (attrs, inner);
2789 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2791 c_parser_consume_token (parser);
2795 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
2799 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2806 if (kind == C_DTR_NORMAL)
2808 c_parser_error (parser, "expected identifier or %<(%>");
2812 return build_id_declarator (NULL_TREE);
2816 /* Parse part of a direct declarator or direct abstract declarator,
2817 given that some (in INNER) has already been parsed; ID_PRESENT is
2818 true if an identifier is present, false for an abstract
2821 static struct c_declarator *
2822 c_parser_direct_declarator_inner (c_parser *parser, bool id_present,
2823 struct c_declarator *inner)
2825 /* Parse a sequence of array declarators and parameter lists. */
2826 if (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
2828 location_t brace_loc = c_parser_peek_token (parser)->location;
2829 struct c_declarator *declarator;
2830 struct c_declspecs *quals_attrs = build_null_declspecs ();
2834 c_parser_consume_token (parser);
2835 c_parser_declspecs (parser, quals_attrs, false, false, true);
2836 static_seen = c_parser_next_token_is_keyword (parser, RID_STATIC);
2838 c_parser_consume_token (parser);
2839 if (static_seen && !quals_attrs->declspecs_seen_p)
2840 c_parser_declspecs (parser, quals_attrs, false, false, true);
2841 if (!quals_attrs->declspecs_seen_p)
2843 /* If "static" is present, there must be an array dimension.
2844 Otherwise, there may be a dimension, "*", or no
2849 dimen = c_parser_expr_no_commas (parser, NULL).value;
2853 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
2858 else if (c_parser_next_token_is (parser, CPP_MULT))
2860 if (c_parser_peek_2nd_token (parser)->type == CPP_CLOSE_SQUARE)
2864 c_parser_consume_token (parser);
2869 dimen = c_parser_expr_no_commas (parser, NULL).value;
2875 dimen = c_parser_expr_no_commas (parser, NULL).value;
2878 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
2879 c_parser_consume_token (parser);
2882 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
2887 mark_exp_read (dimen);
2888 declarator = build_array_declarator (brace_loc, dimen, quals_attrs,
2889 static_seen, star_seen);
2890 if (declarator == NULL)
2892 inner = set_array_declarator_inner (declarator, inner);
2893 return c_parser_direct_declarator_inner (parser, id_present, inner);
2895 else if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
2898 struct c_arg_info *args;
2899 c_parser_consume_token (parser);
2900 attrs = c_parser_attributes (parser);
2901 args = c_parser_parms_declarator (parser, id_present, attrs);
2906 inner = build_function_declarator (args, inner);
2907 return c_parser_direct_declarator_inner (parser, id_present, inner);
2913 /* Parse a parameter list or identifier list, including the closing
2914 parenthesis but not the opening one. ATTRS are the attributes at
2915 the start of the list. ID_LIST_OK is true if an identifier list is
2916 acceptable; such a list must not have attributes at the start. */
2918 static struct c_arg_info *
2919 c_parser_parms_declarator (c_parser *parser, bool id_list_ok, tree attrs)
2922 declare_parm_level ();
2923 /* If the list starts with an identifier, it is an identifier list.
2924 Otherwise, it is either a prototype list or an empty list. */
2927 && c_parser_next_token_is (parser, CPP_NAME)
2928 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
2930 tree list = NULL_TREE, *nextp = &list;
2931 while (c_parser_next_token_is (parser, CPP_NAME)
2932 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
2934 *nextp = build_tree_list (NULL_TREE,
2935 c_parser_peek_token (parser)->value);
2936 nextp = & TREE_CHAIN (*nextp);
2937 c_parser_consume_token (parser);
2938 if (c_parser_next_token_is_not (parser, CPP_COMMA))
2940 c_parser_consume_token (parser);
2941 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2943 c_parser_error (parser, "expected identifier");
2947 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2949 struct c_arg_info *ret = build_arg_info ();
2951 c_parser_consume_token (parser);
2957 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2965 struct c_arg_info *ret = c_parser_parms_list_declarator (parser, attrs);
2971 /* Parse a parameter list (possibly empty), including the closing
2972 parenthesis but not the opening one. ATTRS are the attributes at
2973 the start of the list. */
2975 static struct c_arg_info *
2976 c_parser_parms_list_declarator (c_parser *parser, tree attrs)
2978 bool bad_parm = false;
2979 /* ??? Following the old parser, forward parameter declarations may
2980 use abstract declarators, and if no real parameter declarations
2981 follow the forward declarations then this is not diagnosed. Also
2982 note as above that attributes are ignored as the only contents of
2983 the parentheses, or as the only contents after forward
2985 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2987 struct c_arg_info *ret = build_arg_info ();
2988 c_parser_consume_token (parser);
2991 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
2993 struct c_arg_info *ret = build_arg_info ();
2994 /* Suppress -Wold-style-definition for this case. */
2995 ret->types = error_mark_node;
2996 error_at (c_parser_peek_token (parser)->location,
2997 "ISO C requires a named argument before %<...%>");
2998 c_parser_consume_token (parser);
2999 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3001 c_parser_consume_token (parser);
3006 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3011 /* Nonempty list of parameters, either terminated with semicolon
3012 (forward declarations; recurse) or with close parenthesis (normal
3013 function) or with ", ... )" (variadic function). */
3016 /* Parse a parameter. */
3017 struct c_parm *parm = c_parser_parameter_declaration (parser, attrs);
3022 push_parm_decl (parm);
3023 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
3026 c_parser_consume_token (parser);
3027 mark_forward_parm_decls ();
3028 new_attrs = c_parser_attributes (parser);
3029 return c_parser_parms_list_declarator (parser, new_attrs);
3031 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3033 c_parser_consume_token (parser);
3036 get_pending_sizes ();
3040 return get_parm_info (false);
3042 if (!c_parser_require (parser, CPP_COMMA,
3043 "expected %<;%>, %<,%> or %<)%>"))
3045 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
3046 get_pending_sizes ();
3049 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
3051 c_parser_consume_token (parser);
3052 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3054 c_parser_consume_token (parser);
3057 get_pending_sizes ();
3061 return get_parm_info (true);
3065 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3067 get_pending_sizes ();
3074 /* Parse a parameter declaration. ATTRS are the attributes at the
3075 start of the declaration if it is the first parameter. */
3077 static struct c_parm *
3078 c_parser_parameter_declaration (c_parser *parser, tree attrs)
3080 struct c_declspecs *specs;
3081 struct c_declarator *declarator;
3083 tree postfix_attrs = NULL_TREE;
3085 if (!c_parser_next_token_starts_declspecs (parser))
3087 c_token *token = c_parser_peek_token (parser);
3090 c_parser_set_source_position_from_token (token);
3091 if (token->type == CPP_NAME
3092 && c_parser_peek_2nd_token (parser)->type != CPP_COMMA
3093 && c_parser_peek_2nd_token (parser)->type != CPP_CLOSE_PAREN)
3095 error ("unknown type name %qE", token->value);
3096 parser->error = true;
3098 /* ??? In some Objective-C cases '...' isn't applicable so there
3099 should be a different message. */
3101 c_parser_error (parser,
3102 "expected declaration specifiers or %<...%>");
3103 c_parser_skip_to_end_of_parameter (parser);
3106 specs = build_null_declspecs ();
3109 declspecs_add_attrs (specs, attrs);
3112 c_parser_declspecs (parser, specs, true, true, true);
3113 finish_declspecs (specs);
3114 pending_xref_error ();
3115 prefix_attrs = specs->attrs;
3116 specs->attrs = NULL_TREE;
3117 declarator = c_parser_declarator (parser,
3118 specs->typespec_kind != ctsk_none,
3119 C_DTR_PARM, &dummy);
3120 if (declarator == NULL)
3122 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
3125 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
3126 postfix_attrs = c_parser_attributes (parser);
3127 return build_c_parm (specs, chainon (postfix_attrs, prefix_attrs),
3131 /* Parse a string literal in an asm expression. It should not be
3132 translated, and wide string literals are an error although
3133 permitted by the syntax. This is a GNU extension.
3138 ??? At present, following the old parser, the caller needs to have
3139 set lex_untranslated_string to 1. It would be better to follow the
3140 C++ parser rather than using this kludge. */
3143 c_parser_asm_string_literal (c_parser *parser)
3146 if (c_parser_next_token_is (parser, CPP_STRING))
3148 str = c_parser_peek_token (parser)->value;
3149 c_parser_consume_token (parser);
3151 else if (c_parser_next_token_is (parser, CPP_WSTRING))
3153 error_at (c_parser_peek_token (parser)->location,
3154 "wide string literal in %<asm%>");
3155 str = build_string (1, "");
3156 c_parser_consume_token (parser);
3160 c_parser_error (parser, "expected string literal");
3166 /* Parse a simple asm expression. This is used in restricted
3167 contexts, where a full expression with inputs and outputs does not
3168 make sense. This is a GNU extension.
3171 asm ( asm-string-literal )
3175 c_parser_simple_asm_expr (c_parser *parser)
3178 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ASM));
3179 /* ??? Follow the C++ parser rather than using the
3180 lex_untranslated_string kludge. */
3181 parser->lex_untranslated_string = true;
3182 c_parser_consume_token (parser);
3183 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3185 parser->lex_untranslated_string = false;
3188 str = c_parser_asm_string_literal (parser);
3189 parser->lex_untranslated_string = false;
3190 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
3192 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
3198 /* Parse (possibly empty) attributes. This is a GNU extension.
3202 attributes attribute
3205 __attribute__ ( ( attribute-list ) )
3209 attribute_list , attrib
3214 any-word ( identifier )
3215 any-word ( identifier , nonempty-expr-list )
3216 any-word ( expr-list )
3218 where the "identifier" must not be declared as a type, and
3219 "any-word" may be any identifier (including one declared as a
3220 type), a reserved word storage class specifier, type specifier or
3221 type qualifier. ??? This still leaves out most reserved keywords
3222 (following the old parser), shouldn't we include them, and why not
3223 allow identifiers declared as types to start the arguments? */
3226 c_parser_attributes (c_parser *parser)
3228 tree attrs = NULL_TREE;
3229 while (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
3231 /* ??? Follow the C++ parser rather than using the
3232 lex_untranslated_string kludge. */
3233 parser->lex_untranslated_string = true;
3234 c_parser_consume_token (parser);
3235 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3237 parser->lex_untranslated_string = false;
3240 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3242 parser->lex_untranslated_string = false;
3243 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
3246 /* Parse the attribute list. */
3247 while (c_parser_next_token_is (parser, CPP_COMMA)
3248 || c_parser_next_token_is (parser, CPP_NAME)
3249 || c_parser_next_token_is (parser, CPP_KEYWORD))
3251 tree attr, attr_name, attr_args;
3252 VEC(tree,gc) *expr_list;
3253 if (c_parser_next_token_is (parser, CPP_COMMA))
3255 c_parser_consume_token (parser);
3258 if (c_parser_next_token_is (parser, CPP_KEYWORD))
3260 /* ??? See comment above about what keywords are
3263 switch (c_parser_peek_token (parser)->keyword)
3301 /* Accept __attribute__((__const)) as __attribute__((const))
3304 = ridpointers[(int) c_parser_peek_token (parser)->keyword];
3307 attr_name = c_parser_peek_token (parser)->value;
3308 c_parser_consume_token (parser);
3309 if (c_parser_next_token_is_not (parser, CPP_OPEN_PAREN))
3311 attr = build_tree_list (attr_name, NULL_TREE);
3312 attrs = chainon (attrs, attr);
3315 c_parser_consume_token (parser);
3316 /* Parse the attribute contents. If they start with an
3317 identifier which is followed by a comma or close
3318 parenthesis, then the arguments start with that
3319 identifier; otherwise they are an expression list.
3320 In objective-c the identifier may be a classname. */
3321 if (c_parser_next_token_is (parser, CPP_NAME)
3322 && (c_parser_peek_token (parser)->id_kind == C_ID_ID
3323 || (c_dialect_objc ()
3324 && c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME))
3325 && ((c_parser_peek_2nd_token (parser)->type == CPP_COMMA)
3326 || (c_parser_peek_2nd_token (parser)->type
3327 == CPP_CLOSE_PAREN)))
3329 tree arg1 = c_parser_peek_token (parser)->value;
3330 c_parser_consume_token (parser);
3331 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3332 attr_args = build_tree_list (NULL_TREE, arg1);
3336 c_parser_consume_token (parser);
3337 expr_list = c_parser_expr_list (parser, false, true, NULL);
3338 tree_list = build_tree_list_vec (expr_list);
3339 attr_args = tree_cons (NULL_TREE, arg1, tree_list);
3340 release_tree_vector (expr_list);
3345 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3346 attr_args = NULL_TREE;
3349 expr_list = c_parser_expr_list (parser, false, true, NULL);
3350 attr_args = build_tree_list_vec (expr_list);
3351 release_tree_vector (expr_list);
3354 attr = build_tree_list (attr_name, attr_args);
3355 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3356 c_parser_consume_token (parser);
3359 parser->lex_untranslated_string = false;
3360 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3364 attrs = chainon (attrs, attr);
3366 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3367 c_parser_consume_token (parser);
3370 parser->lex_untranslated_string = false;
3371 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3375 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3376 c_parser_consume_token (parser);
3379 parser->lex_untranslated_string = false;
3380 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3384 parser->lex_untranslated_string = false;
3389 /* Parse a type name (C90 6.5.5, C99 6.7.6).
3392 specifier-qualifier-list abstract-declarator[opt]
3395 static struct c_type_name *
3396 c_parser_type_name (c_parser *parser)
3398 struct c_declspecs *specs = build_null_declspecs ();
3399 struct c_declarator *declarator;
3400 struct c_type_name *ret;
3402 c_parser_declspecs (parser, specs, false, true, true);
3403 if (!specs->declspecs_seen_p)
3405 c_parser_error (parser, "expected specifier-qualifier-list");
3408 pending_xref_error ();
3409 finish_declspecs (specs);
3410 declarator = c_parser_declarator (parser,
3411 specs->typespec_kind != ctsk_none,
3412 C_DTR_ABSTRACT, &dummy);
3413 if (declarator == NULL)
3415 ret = XOBNEW (&parser_obstack, struct c_type_name);
3417 ret->declarator = declarator;
3421 /* Parse an initializer (C90 6.5.7, C99 6.7.8).
3424 assignment-expression
3425 { initializer-list }
3426 { initializer-list , }
3429 designation[opt] initializer
3430 initializer-list , designation[opt] initializer
3437 designator-list designator
3444 [ constant-expression ]
3456 [ constant-expression ... constant-expression ]
3458 Any expression without commas is accepted in the syntax for the
3459 constant-expressions, with non-constant expressions rejected later.
3461 This function is only used for top-level initializers; for nested
3462 ones, see c_parser_initval. */
3464 static struct c_expr
3465 c_parser_initializer (c_parser *parser)
3467 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
3468 return c_parser_braced_init (parser, NULL_TREE, false);
3472 location_t loc = c_parser_peek_token (parser)->location;
3473 ret = c_parser_expr_no_commas (parser, NULL);
3474 if (TREE_CODE (ret.value) != STRING_CST
3475 && TREE_CODE (ret.value) != COMPOUND_LITERAL_EXPR)
3476 ret = default_function_array_read_conversion (loc, ret);
3481 /* Parse a braced initializer list. TYPE is the type specified for a
3482 compound literal, and NULL_TREE for other initializers and for
3483 nested braced lists. NESTED_P is true for nested braced lists,
3484 false for the list of a compound literal or the list that is the
3485 top-level initializer in a declaration. */
3487 static struct c_expr
3488 c_parser_braced_init (c_parser *parser, tree type, bool nested_p)
3491 struct obstack braced_init_obstack;
3492 location_t brace_loc = c_parser_peek_token (parser)->location;
3493 gcc_obstack_init (&braced_init_obstack);
3494 gcc_assert (c_parser_next_token_is (parser, CPP_OPEN_BRACE));
3495 c_parser_consume_token (parser);
3497 push_init_level (0, &braced_init_obstack);
3499 really_start_incremental_init (type);
3500 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3502 pedwarn (brace_loc, OPT_pedantic, "ISO C forbids empty initializer braces");
3506 /* Parse a non-empty initializer list, possibly with a trailing
3510 c_parser_initelt (parser, &braced_init_obstack);
3513 if (c_parser_next_token_is (parser, CPP_COMMA))
3514 c_parser_consume_token (parser);
3517 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3521 if (c_parser_next_token_is_not (parser, CPP_CLOSE_BRACE))
3523 ret.value = error_mark_node;
3524 ret.original_code = ERROR_MARK;
3525 ret.original_type = NULL;
3526 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, "expected %<}%>");
3527 pop_init_level (0, &braced_init_obstack);
3528 obstack_free (&braced_init_obstack, NULL);
3531 c_parser_consume_token (parser);
3532 ret = pop_init_level (0, &braced_init_obstack);
3533 obstack_free (&braced_init_obstack, NULL);
3537 /* Parse a nested initializer, including designators. */
3540 c_parser_initelt (c_parser *parser, struct obstack * braced_init_obstack)
3542 /* Parse any designator or designator list. A single array
3543 designator may have the subsequent "=" omitted in GNU C, but a
3544 longer list or a structure member designator may not. */
3545 if (c_parser_next_token_is (parser, CPP_NAME)
3546 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
3548 /* Old-style structure member designator. */
3549 set_init_label (c_parser_peek_token (parser)->value,
3550 braced_init_obstack);
3551 /* Use the colon as the error location. */
3552 pedwarn (c_parser_peek_2nd_token (parser)->location, OPT_pedantic,
3553 "obsolete use of designated initializer with %<:%>");
3554 c_parser_consume_token (parser);
3555 c_parser_consume_token (parser);
3559 /* des_seen is 0 if there have been no designators, 1 if there
3560 has been a single array designator and 2 otherwise. */
3562 /* Location of a designator. */
3563 location_t des_loc = UNKNOWN_LOCATION; /* Quiet warning. */
3564 while (c_parser_next_token_is (parser, CPP_OPEN_SQUARE)
3565 || c_parser_next_token_is (parser, CPP_DOT))
3567 int des_prev = des_seen;
3569 des_loc = c_parser_peek_token (parser)->location;
3572 if (c_parser_next_token_is (parser, CPP_DOT))
3575 c_parser_consume_token (parser);
3576 if (c_parser_next_token_is (parser, CPP_NAME))
3578 set_init_label (c_parser_peek_token (parser)->value,
3579 braced_init_obstack);
3580 c_parser_consume_token (parser);
3585 init.value = error_mark_node;
3586 init.original_code = ERROR_MARK;
3587 init.original_type = NULL;
3588 c_parser_error (parser, "expected identifier");
3589 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
3590 process_init_element (init, false, braced_init_obstack);
3597 location_t ellipsis_loc = UNKNOWN_LOCATION; /* Quiet warning. */
3598 /* ??? Following the old parser, [ objc-receiver
3599 objc-message-args ] is accepted as an initializer,
3600 being distinguished from a designator by what follows
3601 the first assignment expression inside the square
3602 brackets, but after a first array designator a
3603 subsequent square bracket is for Objective-C taken to
3604 start an expression, using the obsolete form of
3605 designated initializer without '=', rather than
3606 possibly being a second level of designation: in LALR
3607 terms, the '[' is shifted rather than reducing
3608 designator to designator-list. */
3609 if (des_prev == 1 && c_dialect_objc ())
3611 des_seen = des_prev;
3614 if (des_prev == 0 && c_dialect_objc ())
3616 /* This might be an array designator or an
3617 Objective-C message expression. If the former,
3618 continue parsing here; if the latter, parse the
3619 remainder of the initializer given the starting
3620 primary-expression. ??? It might make sense to
3621 distinguish when des_prev == 1 as well; see
3622 previous comment. */
3624 struct c_expr mexpr;
3625 c_parser_consume_token (parser);
3626 if (c_parser_peek_token (parser)->type == CPP_NAME
3627 && ((c_parser_peek_token (parser)->id_kind
3629 || (c_parser_peek_token (parser)->id_kind
3630 == C_ID_CLASSNAME)))
3632 /* Type name receiver. */
3633 tree id = c_parser_peek_token (parser)->value;
3634 c_parser_consume_token (parser);
3635 rec = objc_get_class_reference (id);
3636 goto parse_message_args;
3638 first = c_parser_expr_no_commas (parser, NULL).value;
3639 mark_exp_read (first);
3640 if (c_parser_next_token_is (parser, CPP_ELLIPSIS)
3641 || c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
3642 goto array_desig_after_first;
3643 /* Expression receiver. So far only one part
3644 without commas has been parsed; there might be
3645 more of the expression. */
3647 while (c_parser_next_token_is (parser, CPP_COMMA))
3650 location_t comma_loc, exp_loc;
3651 comma_loc = c_parser_peek_token (parser)->location;
3652 c_parser_consume_token (parser);
3653 exp_loc = c_parser_peek_token (parser)->location;
3654 next = c_parser_expr_no_commas (parser, NULL);
3655 next = default_function_array_read_conversion (exp_loc,
3657 rec = build_compound_expr (comma_loc, rec, next.value);
3660 /* Now parse the objc-message-args. */
3661 args = c_parser_objc_message_args (parser);
3662 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
3665 = objc_build_message_expr (build_tree_list (rec, args));
3666 mexpr.original_code = ERROR_MARK;
3667 mexpr.original_type = NULL;
3668 /* Now parse and process the remainder of the
3669 initializer, starting with this message
3670 expression as a primary-expression. */
3671 c_parser_initval (parser, &mexpr, braced_init_obstack);
3674 c_parser_consume_token (parser);
3675 first = c_parser_expr_no_commas (parser, NULL).value;
3676 mark_exp_read (first);
3677 array_desig_after_first:
3678 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
3680 ellipsis_loc = c_parser_peek_token (parser)->location;
3681 c_parser_consume_token (parser);
3682 second = c_parser_expr_no_commas (parser, NULL).value;
3683 mark_exp_read (second);
3687 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
3689 c_parser_consume_token (parser);
3690 set_init_index (first, second, braced_init_obstack);
3692 pedwarn (ellipsis_loc, OPT_pedantic,
3693 "ISO C forbids specifying range of elements to initialize");
3696 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
3702 if (c_parser_next_token_is (parser, CPP_EQ))
3705 pedwarn (des_loc, OPT_pedantic,
3706 "ISO C90 forbids specifying subobject to initialize");
3707 c_parser_consume_token (parser);
3712 pedwarn (c_parser_peek_token (parser)->location, OPT_pedantic,
3713 "obsolete use of designated initializer without %<=%>");
3717 init.value = error_mark_node;
3718 init.original_code = ERROR_MARK;
3719 init.original_type = NULL;
3720 c_parser_error (parser, "expected %<=%>");
3721 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
3722 process_init_element (init, false, braced_init_obstack);
3728 c_parser_initval (parser, NULL, braced_init_obstack);
3731 /* Parse a nested initializer; as c_parser_initializer but parses
3732 initializers within braced lists, after any designators have been
3733 applied. If AFTER is not NULL then it is an Objective-C message
3734 expression which is the primary-expression starting the
3738 c_parser_initval (c_parser *parser, struct c_expr *after,
3739 struct obstack * braced_init_obstack)
3742 gcc_assert (!after || c_dialect_objc ());
3743 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE) && !after)
3744 init = c_parser_braced_init (parser, NULL_TREE, true);
3747 location_t loc = c_parser_peek_token (parser)->location;
3748 init = c_parser_expr_no_commas (parser, after);
3749 if (init.value != NULL_TREE
3750 && TREE_CODE (init.value) != STRING_CST
3751 && TREE_CODE (init.value) != COMPOUND_LITERAL_EXPR)
3752 init = default_function_array_read_conversion (loc, init);
3754 process_init_element (init, false, braced_init_obstack);
3757 /* Parse a compound statement (possibly a function body) (C90 6.6.2,
3761 { block-item-list[opt] }
3762 { label-declarations block-item-list }
3766 block-item-list block-item
3778 { label-declarations block-item-list }
3781 __extension__ nested-declaration
3782 nested-function-definition
3786 label-declarations label-declaration
3789 __label__ identifier-list ;
3791 Allowing the mixing of declarations and code is new in C99. The
3792 GNU syntax also permits (not shown above) labels at the end of
3793 compound statements, which yield an error. We don't allow labels
3794 on declarations; this might seem like a natural extension, but
3795 there would be a conflict between attributes on the label and
3796 prefix attributes on the declaration. ??? The syntax follows the
3797 old parser in requiring something after label declarations.
3798 Although they are erroneous if the labels declared aren't defined,
3799 is it useful for the syntax to be this way?
3811 c_parser_compound_statement (c_parser *parser)
3814 location_t brace_loc;
3815 brace_loc = c_parser_peek_token (parser)->location;
3816 if (!c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
3818 /* Ensure a scope is entered and left anyway to avoid confusion
3819 if we have just prepared to enter a function body. */
3820 stmt = c_begin_compound_stmt (true);
3821 c_end_compound_stmt (brace_loc, stmt, true);
3822 return error_mark_node;
3824 stmt = c_begin_compound_stmt (true);
3825 c_parser_compound_statement_nostart (parser);
3826 return c_end_compound_stmt (brace_loc, stmt, true);
3829 /* Parse a compound statement except for the opening brace. This is
3830 used for parsing both compound statements and statement expressions
3831 (which follow different paths to handling the opening). */
3834 c_parser_compound_statement_nostart (c_parser *parser)
3836 bool last_stmt = false;
3837 bool last_label = false;
3838 bool save_valid_for_pragma = valid_location_for_stdc_pragma_p ();
3839 location_t label_loc = UNKNOWN_LOCATION; /* Quiet warning. */
3840 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3842 c_parser_consume_token (parser);
3845 mark_valid_location_for_stdc_pragma (true);
3846 if (c_parser_next_token_is_keyword (parser, RID_LABEL))
3848 /* Read zero or more forward-declarations for labels that nested
3849 functions can jump to. */
3850 mark_valid_location_for_stdc_pragma (false);
3851 while (c_parser_next_token_is_keyword (parser, RID_LABEL))
3853 label_loc = c_parser_peek_token (parser)->location;
3854 c_parser_consume_token (parser);
3855 /* Any identifiers, including those declared as type names,
3860 if (c_parser_next_token_is_not (parser, CPP_NAME))
3862 c_parser_error (parser, "expected identifier");
3866 = declare_label (c_parser_peek_token (parser)->value);
3867 C_DECLARED_LABEL_FLAG (label) = 1;
3868 add_stmt (build_stmt (label_loc, DECL_EXPR, label));
3869 c_parser_consume_token (parser);
3870 if (c_parser_next_token_is (parser, CPP_COMMA))
3871 c_parser_consume_token (parser);
3875 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
3877 pedwarn (label_loc, OPT_pedantic, "ISO C forbids label declarations");
3879 /* We must now have at least one statement, label or declaration. */
3880 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3882 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
3883 c_parser_error (parser, "expected declaration or statement");
3884 c_parser_consume_token (parser);
3887 while (c_parser_next_token_is_not (parser, CPP_CLOSE_BRACE))
3889 location_t loc = c_parser_peek_token (parser)->location;
3890 if (c_parser_next_token_is_keyword (parser, RID_CASE)
3891 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
3892 || (c_parser_next_token_is (parser, CPP_NAME)
3893 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
3895 if (c_parser_next_token_is_keyword (parser, RID_CASE))
3896 label_loc = c_parser_peek_2nd_token (parser)->location;
3898 label_loc = c_parser_peek_token (parser)->location;
3901 mark_valid_location_for_stdc_pragma (false);
3902 c_parser_label (parser);
3904 else if (!last_label
3905 && c_parser_next_tokens_start_declaration (parser))
3908 mark_valid_location_for_stdc_pragma (false);
3909 c_parser_declaration_or_fndef (parser, true, true, true, true, true, NULL);
3912 (pedantic && !flag_isoc99)
3914 : OPT_Wdeclaration_after_statement,
3915 "ISO C90 forbids mixed declarations and code");
3918 else if (!last_label
3919 && c_parser_next_token_is_keyword (parser, RID_EXTENSION))
3921 /* __extension__ can start a declaration, but is also an
3922 unary operator that can start an expression. Consume all
3923 but the last of a possible series of __extension__ to
3925 while (c_parser_peek_2nd_token (parser)->type == CPP_KEYWORD
3926 && (c_parser_peek_2nd_token (parser)->keyword
3928 c_parser_consume_token (parser);
3929 if (c_token_starts_declaration (c_parser_peek_2nd_token (parser)))
3932 ext = disable_extension_diagnostics ();
3933 c_parser_consume_token (parser);
3935 mark_valid_location_for_stdc_pragma (false);
3936 c_parser_declaration_or_fndef (parser, true, true, true, true,
3938 /* Following the old parser, __extension__ does not
3939 disable this diagnostic. */
3940 restore_extension_diagnostics (ext);
3942 pedwarn_c90 (loc, (pedantic && !flag_isoc99)
3944 : OPT_Wdeclaration_after_statement,
3945 "ISO C90 forbids mixed declarations and code");
3951 else if (c_parser_next_token_is (parser, CPP_PRAGMA))
3953 /* External pragmas, and some omp pragmas, are not associated
3954 with regular c code, and so are not to be considered statements
3955 syntactically. This ensures that the user doesn't put them
3956 places that would turn into syntax errors if the directive
3958 if (c_parser_pragma (parser, pragma_compound))
3959 last_label = false, last_stmt = true;
3961 else if (c_parser_next_token_is (parser, CPP_EOF))
3963 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
3964 c_parser_error (parser, "expected declaration or statement");
3967 else if (c_parser_next_token_is_keyword (parser, RID_ELSE))
3969 if (parser->in_if_block)
3971 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
3972 error_at (loc, """expected %<}%> before %<else%>");
3977 error_at (loc, "%<else%> without a previous %<if%>");
3978 c_parser_consume_token (parser);
3987 mark_valid_location_for_stdc_pragma (false);
3988 c_parser_statement_after_labels (parser);
3991 parser->error = false;
3994 error_at (label_loc, "label at end of compound statement");
3995 c_parser_consume_token (parser);
3996 /* Restore the value we started with. */
3997 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
4000 /* Parse a label (C90 6.6.1, C99 6.8.1).
4003 identifier : attributes[opt]
4004 case constant-expression :
4010 case constant-expression ... constant-expression :
4012 The use of attributes on labels is a GNU extension. The syntax in
4013 GNU C accepts any expressions without commas, non-constant
4014 expressions being rejected later. */
4017 c_parser_label (c_parser *parser)
4019 location_t loc1 = c_parser_peek_token (parser)->location;
4020 tree label = NULL_TREE;
4021 if (c_parser_next_token_is_keyword (parser, RID_CASE))
4024 c_parser_consume_token (parser);
4025 exp1 = c_parser_expr_no_commas (parser, NULL).value;
4026 if (c_parser_next_token_is (parser, CPP_COLON))
4028 c_parser_consume_token (parser);
4029 label = do_case (loc1, exp1, NULL_TREE);
4031 else if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
4033 c_parser_consume_token (parser);
4034 exp2 = c_parser_expr_no_commas (parser, NULL).value;
4035 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
4036 label = do_case (loc1, exp1, exp2);
4039 c_parser_error (parser, "expected %<:%> or %<...%>");
4041 else if (c_parser_next_token_is_keyword (parser, RID_DEFAULT))
4043 c_parser_consume_token (parser);
4044 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
4045 label = do_case (loc1, NULL_TREE, NULL_TREE);
4049 tree name = c_parser_peek_token (parser)->value;
4052 location_t loc2 = c_parser_peek_token (parser)->location;
4053 gcc_assert (c_parser_next_token_is (parser, CPP_NAME));
4054 c_parser_consume_token (parser);
4055 gcc_assert (c_parser_next_token_is (parser, CPP_COLON));
4056 c_parser_consume_token (parser);
4057 attrs = c_parser_attributes (parser);
4058 tlab = define_label (loc2, name);
4061 decl_attributes (&tlab, attrs, 0);
4062 label = add_stmt (build_stmt (loc1, LABEL_EXPR, tlab));
4067 if (c_parser_next_tokens_start_declaration (parser))
4069 error_at (c_parser_peek_token (parser)->location,
4070 "a label can only be part of a statement and "
4071 "a declaration is not a statement");
4072 c_parser_declaration_or_fndef (parser, /*fndef_ok*/ false,
4073 /*static_assert_ok*/ true,
4074 /*nested*/ true, /*empty_ok*/ false,
4075 /*start_attr_ok*/ true, NULL);
4080 /* Parse a statement (C90 6.6, C99 6.8).
4085 expression-statement
4093 expression-statement:
4096 selection-statement:
4100 iteration-statement:
4109 return expression[opt] ;
4122 objc-throw-statement
4123 objc-try-catch-statement
4124 objc-synchronized-statement
4126 objc-throw-statement:
4140 parallel-for-construct
4141 parallel-sections-construct
4148 parallel-directive structured-block
4151 for-directive iteration-statement
4154 sections-directive section-scope
4157 single-directive structured-block
4159 parallel-for-construct:
4160 parallel-for-directive iteration-statement
4162 parallel-sections-construct:
4163 parallel-sections-directive section-scope
4166 master-directive structured-block
4169 critical-directive structured-block
4172 atomic-directive expression-statement
4175 ordered-directive structured-block */
4178 c_parser_statement (c_parser *parser)
4180 while (c_parser_next_token_is_keyword (parser, RID_CASE)
4181 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
4182 || (c_parser_next_token_is (parser, CPP_NAME)
4183 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
4184 c_parser_label (parser);
4185 c_parser_statement_after_labels (parser);
4188 /* Parse a statement, other than a labeled statement. */
4191 c_parser_statement_after_labels (c_parser *parser)
4193 location_t loc = c_parser_peek_token (parser)->location;
4194 tree stmt = NULL_TREE;
4195 bool in_if_block = parser->in_if_block;
4196 parser->in_if_block = false;
4197 switch (c_parser_peek_token (parser)->type)
4199 case CPP_OPEN_BRACE:
4200 add_stmt (c_parser_compound_statement (parser));
4203 switch (c_parser_peek_token (parser)->keyword)
4206 c_parser_if_statement (parser);
4209 c_parser_switch_statement (parser);
4212 c_parser_while_statement (parser);
4215 c_parser_do_statement (parser);
4218 c_parser_for_statement (parser);
4221 c_parser_consume_token (parser);
4222 if (c_parser_next_token_is (parser, CPP_NAME))
4224 stmt = c_finish_goto_label (loc,
4225 c_parser_peek_token (parser)->value);
4226 c_parser_consume_token (parser);
4228 else if (c_parser_next_token_is (parser, CPP_MULT))
4232 c_parser_consume_token (parser);
4233 val = c_parser_expression (parser).value;
4234 mark_exp_read (val);
4235 stmt = c_finish_goto_ptr (loc, val);
4238 c_parser_error (parser, "expected identifier or %<*%>");
4239 goto expect_semicolon;
4241 c_parser_consume_token (parser);
4242 stmt = c_finish_bc_stmt (loc, &c_cont_label, false);
4243 goto expect_semicolon;
4245 c_parser_consume_token (parser);
4246 stmt = c_finish_bc_stmt (loc, &c_break_label, true);
4247 goto expect_semicolon;
4249 c_parser_consume_token (parser);
4250 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
4252 stmt = c_finish_return (loc, NULL_TREE, NULL_TREE);
4253 c_parser_consume_token (parser);
4257 struct c_expr expr = c_parser_expression_conv (parser);
4258 mark_exp_read (expr.value);
4259 stmt = c_finish_return (loc, expr.value, expr.original_type);
4260 goto expect_semicolon;
4264 stmt = c_parser_asm_statement (parser);
4267 gcc_assert (c_dialect_objc ());
4268 c_parser_consume_token (parser);
4269 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
4271 stmt = objc_build_throw_stmt (loc, NULL_TREE);
4272 c_parser_consume_token (parser);
4276 tree expr = c_parser_expression (parser).value;
4277 expr = c_fully_fold (expr, false, NULL);
4278 stmt = objc_build_throw_stmt (loc, expr);
4279 goto expect_semicolon;
4283 gcc_assert (c_dialect_objc ());
4284 c_parser_objc_try_catch_statement (parser);
4286 case RID_AT_SYNCHRONIZED:
4287 gcc_assert (c_dialect_objc ());
4288 c_parser_objc_synchronized_statement (parser);
4295 c_parser_consume_token (parser);
4297 case CPP_CLOSE_PAREN:
4298 case CPP_CLOSE_SQUARE:
4299 /* Avoid infinite loop in error recovery:
4300 c_parser_skip_until_found stops at a closing nesting
4301 delimiter without consuming it, but here we need to consume
4302 it to proceed further. */
4303 c_parser_error (parser, "expected statement");
4304 c_parser_consume_token (parser);
4307 c_parser_pragma (parser, pragma_stmt);
4311 stmt = c_finish_expr_stmt (loc, c_parser_expression_conv (parser).value);
4313 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
4316 /* Two cases cannot and do not have line numbers associated: If stmt
4317 is degenerate, such as "2;", then stmt is an INTEGER_CST, which
4318 cannot hold line numbers. But that's OK because the statement
4319 will either be changed to a MODIFY_EXPR during gimplification of
4320 the statement expr, or discarded. If stmt was compound, but
4321 without new variables, we will have skipped the creation of a
4322 BIND and will have a bare STATEMENT_LIST. But that's OK because
4323 (recursively) all of the component statements should already have
4324 line numbers assigned. ??? Can we discard no-op statements
4326 if (CAN_HAVE_LOCATION_P (stmt)
4327 && EXPR_LOCATION (stmt) == UNKNOWN_LOCATION)
4328 SET_EXPR_LOCATION (stmt, loc);
4330 parser->in_if_block = in_if_block;
4333 /* Parse the condition from an if, do, while or for statements. */
4336 c_parser_condition (c_parser *parser)
4338 location_t loc = c_parser_peek_token (parser)->location;
4340 cond = c_parser_expression_conv (parser).value;
4341 cond = c_objc_common_truthvalue_conversion (loc, cond);
4342 cond = c_fully_fold (cond, false, NULL);
4343 if (warn_sequence_point)
4344 verify_sequence_points (cond);
4348 /* Parse a parenthesized condition from an if, do or while statement.
4354 c_parser_paren_condition (c_parser *parser)
4357 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4358 return error_mark_node;
4359 cond = c_parser_condition (parser);
4360 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
4364 /* Parse a statement which is a block in C99. */
4367 c_parser_c99_block_statement (c_parser *parser)
4369 tree block = c_begin_compound_stmt (flag_isoc99);
4370 location_t loc = c_parser_peek_token (parser)->location;
4371 c_parser_statement (parser);
4372 return c_end_compound_stmt (loc, block, flag_isoc99);
4375 /* Parse the body of an if statement. This is just parsing a
4376 statement but (a) it is a block in C99, (b) we track whether the
4377 body is an if statement for the sake of -Wparentheses warnings, (c)
4378 we handle an empty body specially for the sake of -Wempty-body
4379 warnings, and (d) we call parser_compound_statement directly
4380 because c_parser_statement_after_labels resets
4381 parser->in_if_block. */
4384 c_parser_if_body (c_parser *parser, bool *if_p)
4386 tree block = c_begin_compound_stmt (flag_isoc99);
4387 location_t body_loc = c_parser_peek_token (parser)->location;
4388 while (c_parser_next_token_is_keyword (parser, RID_CASE)
4389 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
4390 || (c_parser_next_token_is (parser, CPP_NAME)
4391 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
4392 c_parser_label (parser);
4393 *if_p = c_parser_next_token_is_keyword (parser, RID_IF);
4394 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
4396 location_t loc = c_parser_peek_token (parser)->location;
4397 add_stmt (build_empty_stmt (loc));
4398 c_parser_consume_token (parser);
4399 if (!c_parser_next_token_is_keyword (parser, RID_ELSE))
4400 warning_at (loc, OPT_Wempty_body,
4401 "suggest braces around empty body in an %<if%> statement");
4403 else if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
4404 add_stmt (c_parser_compound_statement (parser));
4406 c_parser_statement_after_labels (parser);
4407 return c_end_compound_stmt (body_loc, block, flag_isoc99);
4410 /* Parse the else body of an if statement. This is just parsing a
4411 statement but (a) it is a block in C99, (b) we handle an empty body
4412 specially for the sake of -Wempty-body warnings. */
4415 c_parser_else_body (c_parser *parser)
4417 location_t else_loc = c_parser_peek_token (parser)->location;
4418 tree block = c_begin_compound_stmt (flag_isoc99);
4419 while (c_parser_next_token_is_keyword (parser, RID_CASE)
4420 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
4421 || (c_parser_next_token_is (parser, CPP_NAME)
4422 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
4423 c_parser_label (parser);
4424 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
4426 location_t loc = c_parser_peek_token (parser)->location;
4429 "suggest braces around empty body in an %<else%> statement");
4430 add_stmt (build_empty_stmt (loc));
4431 c_parser_consume_token (parser);
4434 c_parser_statement_after_labels (parser);
4435 return c_end_compound_stmt (else_loc, block, flag_isoc99);
4438 /* Parse an if statement (C90 6.6.4, C99 6.8.4).
4441 if ( expression ) statement
4442 if ( expression ) statement else statement
4446 c_parser_if_statement (c_parser *parser)
4451 bool first_if = false;
4452 tree first_body, second_body;
4455 gcc_assert (c_parser_next_token_is_keyword (parser, RID_IF));
4456 c_parser_consume_token (parser);
4457 block = c_begin_compound_stmt (flag_isoc99);
4458 loc = c_parser_peek_token (parser)->location;
4459 cond = c_parser_paren_condition (parser);
4460 in_if_block = parser->in_if_block;
4461 parser->in_if_block = true;
4462 first_body = c_parser_if_body (parser, &first_if);
4463 parser->in_if_block = in_if_block;
4464 if (c_parser_next_token_is_keyword (parser, RID_ELSE))
4466 c_parser_consume_token (parser);
4467 second_body = c_parser_else_body (parser);
4470 second_body = NULL_TREE;
4471 c_finish_if_stmt (loc, cond, first_body, second_body, first_if);
4472 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99));
4475 /* Parse a switch statement (C90 6.6.4, C99 6.8.4).
4478 switch (expression) statement
4482 c_parser_switch_statement (c_parser *parser)
4484 tree block, expr, body, save_break;
4485 location_t switch_loc = c_parser_peek_token (parser)->location;
4486 location_t switch_cond_loc;
4487 gcc_assert (c_parser_next_token_is_keyword (parser, RID_SWITCH));
4488 c_parser_consume_token (parser);
4489 block = c_begin_compound_stmt (flag_isoc99);
4490 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4492 switch_cond_loc = c_parser_peek_token (parser)->location;
4493 expr = c_parser_expression (parser).value;
4494 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
4498 switch_cond_loc = UNKNOWN_LOCATION;
4499 expr = error_mark_node;
4501 c_start_case (switch_loc, switch_cond_loc, expr);
4502 save_break = c_break_label;
4503 c_break_label = NULL_TREE;
4504 body = c_parser_c99_block_statement (parser);
4505 c_finish_case (body);
4508 location_t here = c_parser_peek_token (parser)->location;
4509 tree t = build1 (LABEL_EXPR, void_type_node, c_break_label);
4510 SET_EXPR_LOCATION (t, here);
4513 c_break_label = save_break;
4514 add_stmt (c_end_compound_stmt (switch_loc, block, flag_isoc99));
4517 /* Parse a while statement (C90 6.6.5, C99 6.8.5).
4520 while (expression) statement
4524 c_parser_while_statement (c_parser *parser)
4526 tree block, cond, body, save_break, save_cont;
4528 gcc_assert (c_parser_next_token_is_keyword (parser, RID_WHILE));
4529 c_parser_consume_token (parser);
4530 block = c_begin_compound_stmt (flag_isoc99);
4531 loc = c_parser_peek_token (parser)->location;
4532 cond = c_parser_paren_condition (parser);
4533 save_break = c_break_label;
4534 c_break_label = NULL_TREE;
4535 save_cont = c_cont_label;
4536 c_cont_label = NULL_TREE;
4537 body = c_parser_c99_block_statement (parser);
4538 c_finish_loop (loc, cond, NULL, body, c_break_label, c_cont_label, true);
4539 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99));
4540 c_break_label = save_break;
4541 c_cont_label = save_cont;
4544 /* Parse a do statement (C90 6.6.5, C99 6.8.5).
4547 do statement while ( expression ) ;
4551 c_parser_do_statement (c_parser *parser)
4553 tree block, cond, body, save_break, save_cont, new_break, new_cont;
4555 gcc_assert (c_parser_next_token_is_keyword (parser, RID_DO));
4556 c_parser_consume_token (parser);
4557 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
4558 warning_at (c_parser_peek_token (parser)->location,
4560 "suggest braces around empty body in %<do%> statement");
4561 block = c_begin_compound_stmt (flag_isoc99);
4562 loc = c_parser_peek_token (parser)->location;
4563 save_break = c_break_label;
4564 c_break_label = NULL_TREE;
4565 save_cont = c_cont_label;
4566 c_cont_label = NULL_TREE;
4567 body = c_parser_c99_block_statement (parser);
4568 c_parser_require_keyword (parser, RID_WHILE, "expected %<while%>");
4569 new_break = c_break_label;
4570 c_break_label = save_break;
4571 new_cont = c_cont_label;
4572 c_cont_label = save_cont;
4573 cond = c_parser_paren_condition (parser);
4574 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
4575 c_parser_skip_to_end_of_block_or_statement (parser);
4576 c_finish_loop (loc, cond, NULL, body, new_break, new_cont, false);
4577 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99));
4580 /* Parse a for statement (C90 6.6.5, C99 6.8.5).
4583 for ( expression[opt] ; expression[opt] ; expression[opt] ) statement
4584 for ( nested-declaration expression[opt] ; expression[opt] ) statement
4586 The form with a declaration is new in C99.
4588 ??? In accordance with the old parser, the declaration may be a
4589 nested function, which is then rejected in check_for_loop_decls,
4590 but does it make any sense for this to be included in the grammar?
4591 Note in particular that the nested function does not include a
4592 trailing ';', whereas the "declaration" production includes one.
4593 Also, can we reject bad declarations earlier and cheaper than
4594 check_for_loop_decls?
4596 In Objective-C, there are two additional variants:
4599 for ( expression in expresssion ) statement
4600 for ( declaration in expression ) statement
4602 This is inconsistent with C, because the second variant is allowed
4603 even if c99 is not enabled.
4605 The rest of the comment documents these Objective-C foreach-statement.
4607 Here is the canonical example of the first variant:
4608 for (object in array) { do something with object }
4609 we call the first expression ("object") the "object_expression" and
4610 the second expression ("array") the "collection_expression".
4611 object_expression must be an lvalue of type "id" (a generic Objective-C
4612 object) because the loop works by assigning to object_expression the
4613 various objects from the collection_expression. collection_expression
4614 must evaluate to something of type "id" which responds to the method
4615 countByEnumeratingWithState:objects:count:.
4617 The canonical example of the second variant is:
4618 for (id object in array) { do something with object }
4619 which is completely equivalent to
4622 for (object in array) { do something with object }
4624 Note that initizializing 'object' in some way (eg, "for ((object =
4625 xxx) in array) { do something with object }") is possibly
4626 technically valid, but completely pointless as 'object' will be
4627 assigned to something else as soon as the loop starts. We should
4628 most likely reject it (TODO).
4630 The beginning of the Objective-C foreach-statement looks exactly
4631 like the beginning of the for-statement, and we can tell it is a
4632 foreach-statement only because the initial declaration or
4633 expression is terminated by 'in' instead of ';'.
4637 c_parser_for_statement (c_parser *parser)
4639 tree block, cond, incr, save_break, save_cont, body;
4640 /* The following are only used when parsing an ObjC foreach statement. */
4641 tree object_expression, collection_expression;
4642 location_t loc = c_parser_peek_token (parser)->location;
4643 location_t for_loc = c_parser_peek_token (parser)->location;
4644 bool is_foreach_statement = false;
4645 gcc_assert (c_parser_next_token_is_keyword (parser, RID_FOR));
4646 c_parser_consume_token (parser);
4647 /* Open a compound statement in Objective-C as well, just in case this is
4648 as foreach expression. */
4649 block = c_begin_compound_stmt (flag_isoc99 || c_dialect_objc ());
4650 cond = error_mark_node;
4651 incr = error_mark_node;
4652 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4654 /* Parse the initialization declaration or expression. */
4655 object_expression = error_mark_node;
4656 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
4658 c_parser_consume_token (parser);
4659 c_finish_expr_stmt (loc, NULL_TREE);
4661 else if (c_parser_next_tokens_start_declaration (parser))
4663 parser->objc_could_be_foreach_context = true;
4664 c_parser_declaration_or_fndef (parser, true, true, true, true, true,
4665 &object_expression);
4666 parser->objc_could_be_foreach_context = false;
4668 if (c_parser_next_token_is_keyword (parser, RID_IN))
4670 c_parser_consume_token (parser);
4671 is_foreach_statement = true;
4672 if (check_for_loop_decls (for_loc, true) == NULL_TREE)
4673 c_parser_error (parser, "multiple iterating variables in fast enumeration");
4676 check_for_loop_decls (for_loc, flag_isoc99);
4678 else if (c_parser_next_token_is_keyword (parser, RID_EXTENSION))
4680 /* __extension__ can start a declaration, but is also an
4681 unary operator that can start an expression. Consume all
4682 but the last of a possible series of __extension__ to
4684 while (c_parser_peek_2nd_token (parser)->type == CPP_KEYWORD
4685 && (c_parser_peek_2nd_token (parser)->keyword
4687 c_parser_consume_token (parser);
4688 if (c_token_starts_declaration (c_parser_peek_2nd_token (parser)))
4691 ext = disable_extension_diagnostics ();
4692 c_parser_consume_token (parser);
4693 parser->objc_could_be_foreach_context = true;
4694 c_parser_declaration_or_fndef (parser, true, true, true, true,
4695 true, &object_expression);
4696 parser->objc_could_be_foreach_context = false;
4698 restore_extension_diagnostics (ext);
4699 if (c_parser_next_token_is_keyword (parser, RID_IN))
4701 c_parser_consume_token (parser);
4702 is_foreach_statement = true;
4703 if (check_for_loop_decls (for_loc, true) == NULL_TREE)
4704 c_parser_error (parser, "multiple iterating variables in fast enumeration");
4707 check_for_loop_decls (for_loc, flag_isoc99);
4716 tree init_expression;
4717 parser->objc_could_be_foreach_context = true;
4718 init_expression = c_parser_expression (parser).value;
4719 parser->objc_could_be_foreach_context = false;
4720 if (c_parser_next_token_is_keyword (parser, RID_IN))
4722 c_parser_consume_token (parser);
4723 is_foreach_statement = true;
4724 if (! lvalue_p (init_expression))
4725 c_parser_error (parser, "invalid iterating variable in fast enumeration");
4726 object_expression = c_process_expr_stmt (loc, init_expression);
4731 c_finish_expr_stmt (loc, init_expression);
4732 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
4736 /* Parse the loop condition. In the case of a foreach
4737 statement, there is no loop condition. */
4738 if (!is_foreach_statement)
4740 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
4742 c_parser_consume_token (parser);
4747 cond = c_parser_condition (parser);
4748 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
4751 /* Parse the increment expression (the third expression in a
4752 for-statement). In the case of a foreach-statement, this is
4753 the expression that follows the 'in'. */
4754 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4756 if (is_foreach_statement)
4758 c_parser_error (parser, "missing collection in fast enumeration");
4759 collection_expression = error_mark_node;
4762 incr = c_process_expr_stmt (loc, NULL_TREE);
4766 if (is_foreach_statement)
4767 collection_expression = c_process_expr_stmt (loc, c_parser_expression (parser).value);
4769 incr = c_process_expr_stmt (loc, c_parser_expression (parser).value);
4771 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
4773 save_break = c_break_label;
4774 c_break_label = NULL_TREE;
4775 save_cont = c_cont_label;
4776 c_cont_label = NULL_TREE;
4777 body = c_parser_c99_block_statement (parser);
4778 if (is_foreach_statement)
4779 objc_finish_foreach_loop (loc, object_expression, collection_expression, body, c_break_label, c_cont_label);
4781 c_finish_loop (loc, cond, incr, body, c_break_label, c_cont_label, true);
4782 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99 || c_dialect_objc ()));
4783 c_break_label = save_break;
4784 c_cont_label = save_cont;
4787 /* Parse an asm statement, a GNU extension. This is a full-blown asm
4788 statement with inputs, outputs, clobbers, and volatile tag
4792 asm type-qualifier[opt] ( asm-argument ) ;
4793 asm type-qualifier[opt] goto ( asm-goto-argument ) ;
4797 asm-string-literal : asm-operands[opt]
4798 asm-string-literal : asm-operands[opt] : asm-operands[opt]
4799 asm-string-literal : asm-operands[opt] : asm-operands[opt] : asm-clobbers[opt]
4802 asm-string-literal : : asm-operands[opt] : asm-clobbers[opt] \
4805 Qualifiers other than volatile are accepted in the syntax but
4809 c_parser_asm_statement (c_parser *parser)
4811 tree quals, str, outputs, inputs, clobbers, labels, ret;
4812 bool simple, is_goto;
4813 location_t asm_loc = c_parser_peek_token (parser)->location;
4814 int section, nsections;
4816 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ASM));
4817 c_parser_consume_token (parser);
4818 if (c_parser_next_token_is_keyword (parser, RID_VOLATILE))
4820 quals = c_parser_peek_token (parser)->value;
4821 c_parser_consume_token (parser);
4823 else if (c_parser_next_token_is_keyword (parser, RID_CONST)
4824 || c_parser_next_token_is_keyword (parser, RID_RESTRICT))
4826 warning_at (c_parser_peek_token (parser)->location,
4828 "%E qualifier ignored on asm",
4829 c_parser_peek_token (parser)->value);
4831 c_parser_consume_token (parser);
4837 if (c_parser_next_token_is_keyword (parser, RID_GOTO))
4839 c_parser_consume_token (parser);
4843 /* ??? Follow the C++ parser rather than using the
4844 lex_untranslated_string kludge. */
4845 parser->lex_untranslated_string = true;
4848 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4851 str = c_parser_asm_string_literal (parser);
4852 if (str == NULL_TREE)
4853 goto error_close_paren;
4856 outputs = NULL_TREE;
4858 clobbers = NULL_TREE;
4861 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN) && !is_goto)
4864 /* Parse each colon-delimited section of operands. */
4865 nsections = 3 + is_goto;
4866 for (section = 0; section < nsections; ++section)
4868 if (!c_parser_require (parser, CPP_COLON,
4871 : "expected %<:%> or %<)%>"))
4872 goto error_close_paren;
4874 /* Once past any colon, we're no longer a simple asm. */
4877 if ((!c_parser_next_token_is (parser, CPP_COLON)
4878 && !c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4883 /* For asm goto, we don't allow output operands, but reserve
4884 the slot for a future extension that does allow them. */
4886 outputs = c_parser_asm_operands (parser, false);
4889 inputs = c_parser_asm_operands (parser, true);
4892 clobbers = c_parser_asm_clobbers (parser);
4895 labels = c_parser_asm_goto_operands (parser);
4901 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN) && !is_goto)
4906 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
4908 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
4912 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
4913 c_parser_skip_to_end_of_block_or_statement (parser);
4915 ret = build_asm_stmt (quals, build_asm_expr (asm_loc, str, outputs, inputs,
4916 clobbers, labels, simple));
4919 parser->lex_untranslated_string = false;
4923 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
4927 /* Parse asm operands, a GNU extension. If CONVERT_P (for inputs but
4928 not outputs), apply the default conversion of functions and arrays
4933 asm-operands , asm-operand
4936 asm-string-literal ( expression )
4937 [ identifier ] asm-string-literal ( expression )
4941 c_parser_asm_operands (c_parser *parser, bool convert_p)
4943 tree list = NULL_TREE;
4949 if (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
4951 c_parser_consume_token (parser);
4952 if (c_parser_next_token_is (parser, CPP_NAME))
4954 tree id = c_parser_peek_token (parser)->value;
4955 c_parser_consume_token (parser);
4956 name = build_string (IDENTIFIER_LENGTH (id),
4957 IDENTIFIER_POINTER (id));
4961 c_parser_error (parser, "expected identifier");
4962 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
4965 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
4970 str = c_parser_asm_string_literal (parser);
4971 if (str == NULL_TREE)
4973 parser->lex_untranslated_string = false;
4974 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4976 parser->lex_untranslated_string = true;
4979 loc = c_parser_peek_token (parser)->location;
4980 expr = c_parser_expression (parser);
4981 mark_exp_read (expr.value);
4983 expr = default_function_array_conversion (loc, expr);
4984 expr.value = c_fully_fold (expr.value, false, NULL);
4985 parser->lex_untranslated_string = true;
4986 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
4988 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
4991 list = chainon (list, build_tree_list (build_tree_list (name, str),
4993 if (c_parser_next_token_is (parser, CPP_COMMA))
4994 c_parser_consume_token (parser);
5001 /* Parse asm clobbers, a GNU extension.
5005 asm-clobbers , asm-string-literal
5009 c_parser_asm_clobbers (c_parser *parser)
5011 tree list = NULL_TREE;
5014 tree str = c_parser_asm_string_literal (parser);
5016 list = tree_cons (NULL_TREE, str, list);
5019 if (c_parser_next_token_is (parser, CPP_COMMA))
5020 c_parser_consume_token (parser);
5027 /* Parse asm goto labels, a GNU extension.
5031 asm-goto-operands , identifier
5035 c_parser_asm_goto_operands (c_parser *parser)
5037 tree list = NULL_TREE;
5042 if (c_parser_next_token_is (parser, CPP_NAME))
5044 c_token *tok = c_parser_peek_token (parser);
5046 label = lookup_label_for_goto (tok->location, name);
5047 c_parser_consume_token (parser);
5048 TREE_USED (label) = 1;
5052 c_parser_error (parser, "expected identifier");
5056 name = build_string (IDENTIFIER_LENGTH (name),
5057 IDENTIFIER_POINTER (name));
5058 list = tree_cons (name, label, list);
5059 if (c_parser_next_token_is (parser, CPP_COMMA))
5060 c_parser_consume_token (parser);
5062 return nreverse (list);
5066 /* Parse an expression other than a compound expression; that is, an
5067 assignment expression (C90 6.3.16, C99 6.5.16). If AFTER is not
5068 NULL then it is an Objective-C message expression which is the
5069 primary-expression starting the expression as an initializer.
5071 assignment-expression:
5072 conditional-expression
5073 unary-expression assignment-operator assignment-expression
5075 assignment-operator: one of
5076 = *= /= %= += -= <<= >>= &= ^= |=
5078 In GNU C we accept any conditional expression on the LHS and
5079 diagnose the invalid lvalue rather than producing a syntax
5082 static struct c_expr
5083 c_parser_expr_no_commas (c_parser *parser, struct c_expr *after)
5085 struct c_expr lhs, rhs, ret;
5086 enum tree_code code;
5087 location_t op_location, exp_location;
5088 gcc_assert (!after || c_dialect_objc ());
5089 lhs = c_parser_conditional_expression (parser, after);
5090 op_location = c_parser_peek_token (parser)->location;
5091 switch (c_parser_peek_token (parser)->type)
5100 code = TRUNC_DIV_EXPR;
5103 code = TRUNC_MOD_EXPR;
5118 code = BIT_AND_EXPR;
5121 code = BIT_XOR_EXPR;
5124 code = BIT_IOR_EXPR;
5129 c_parser_consume_token (parser);
5130 exp_location = c_parser_peek_token (parser)->location;
5131 rhs = c_parser_expr_no_commas (parser, NULL);
5132 rhs = default_function_array_read_conversion (exp_location, rhs);
5133 ret.value = build_modify_expr (op_location, lhs.value, lhs.original_type,
5134 code, exp_location, rhs.value,
5136 if (code == NOP_EXPR)
5137 ret.original_code = MODIFY_EXPR;
5140 TREE_NO_WARNING (ret.value) = 1;
5141 ret.original_code = ERROR_MARK;
5143 ret.original_type = NULL;
5147 /* Parse a conditional expression (C90 6.3.15, C99 6.5.15). If AFTER
5148 is not NULL then it is an Objective-C message expression which is
5149 the primary-expression starting the expression as an initializer.
5151 conditional-expression:
5152 logical-OR-expression
5153 logical-OR-expression ? expression : conditional-expression
5157 conditional-expression:
5158 logical-OR-expression ? : conditional-expression
5161 static struct c_expr
5162 c_parser_conditional_expression (c_parser *parser, struct c_expr *after)
5164 struct c_expr cond, exp1, exp2, ret;
5165 location_t cond_loc, colon_loc, middle_loc;
5167 gcc_assert (!after || c_dialect_objc ());
5169 cond = c_parser_binary_expression (parser, after);
5171 if (c_parser_next_token_is_not (parser, CPP_QUERY))
5173 cond_loc = c_parser_peek_token (parser)->location;
5174 cond = default_function_array_read_conversion (cond_loc, cond);
5175 c_parser_consume_token (parser);
5176 if (c_parser_next_token_is (parser, CPP_COLON))
5178 tree eptype = NULL_TREE;
5180 middle_loc = c_parser_peek_token (parser)->location;
5181 pedwarn (middle_loc, OPT_pedantic,
5182 "ISO C forbids omitting the middle term of a ?: expression");
5183 warn_for_omitted_condop (middle_loc, cond.value);
5184 if (TREE_CODE (cond.value) == EXCESS_PRECISION_EXPR)
5186 eptype = TREE_TYPE (cond.value);
5187 cond.value = TREE_OPERAND (cond.value, 0);
5189 /* Make sure first operand is calculated only once. */
5190 exp1.value = c_save_expr (default_conversion (cond.value));
5192 exp1.value = build1 (EXCESS_PRECISION_EXPR, eptype, exp1.value);
5193 exp1.original_type = NULL;
5194 cond.value = c_objc_common_truthvalue_conversion (cond_loc, exp1.value);
5195 c_inhibit_evaluation_warnings += cond.value == truthvalue_true_node;
5200 = c_objc_common_truthvalue_conversion
5201 (cond_loc, default_conversion (cond.value));
5202 c_inhibit_evaluation_warnings += cond.value == truthvalue_false_node;
5203 exp1 = c_parser_expression_conv (parser);
5204 mark_exp_read (exp1.value);
5205 c_inhibit_evaluation_warnings +=
5206 ((cond.value == truthvalue_true_node)
5207 - (cond.value == truthvalue_false_node));
5210 colon_loc = c_parser_peek_token (parser)->location;
5211 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
5213 c_inhibit_evaluation_warnings -= cond.value == truthvalue_true_node;
5214 ret.value = error_mark_node;
5215 ret.original_code = ERROR_MARK;
5216 ret.original_type = NULL;
5220 location_t exp2_loc = c_parser_peek_token (parser)->location;
5221 exp2 = c_parser_conditional_expression (parser, NULL);
5222 exp2 = default_function_array_read_conversion (exp2_loc, exp2);
5224 c_inhibit_evaluation_warnings -= cond.value == truthvalue_true_node;
5225 ret.value = build_conditional_expr (colon_loc, cond.value,
5226 cond.original_code == C_MAYBE_CONST_EXPR,
5227 exp1.value, exp1.original_type,
5228 exp2.value, exp2.original_type);
5229 ret.original_code = ERROR_MARK;
5230 if (exp1.value == error_mark_node || exp2.value == error_mark_node)
5231 ret.original_type = NULL;
5236 /* If both sides are enum type, the default conversion will have
5237 made the type of the result be an integer type. We want to
5238 remember the enum types we started with. */
5239 t1 = exp1.original_type ? exp1.original_type : TREE_TYPE (exp1.value);
5240 t2 = exp2.original_type ? exp2.original_type : TREE_TYPE (exp2.value);
5241 ret.original_type = ((t1 != error_mark_node
5242 && t2 != error_mark_node
5243 && (TYPE_MAIN_VARIANT (t1)
5244 == TYPE_MAIN_VARIANT (t2)))
5251 /* Parse a binary expression; that is, a logical-OR-expression (C90
5252 6.3.5-6.3.14, C99 6.5.5-6.5.14). If AFTER is not NULL then it is
5253 an Objective-C message expression which is the primary-expression
5254 starting the expression as an initializer.
5256 multiplicative-expression:
5258 multiplicative-expression * cast-expression
5259 multiplicative-expression / cast-expression
5260 multiplicative-expression % cast-expression
5262 additive-expression:
5263 multiplicative-expression
5264 additive-expression + multiplicative-expression
5265 additive-expression - multiplicative-expression
5269 shift-expression << additive-expression
5270 shift-expression >> additive-expression
5272 relational-expression:
5274 relational-expression < shift-expression
5275 relational-expression > shift-expression
5276 relational-expression <= shift-expression
5277 relational-expression >= shift-expression
5279 equality-expression:
5280 relational-expression
5281 equality-expression == relational-expression
5282 equality-expression != relational-expression
5286 AND-expression & equality-expression
5288 exclusive-OR-expression:
5290 exclusive-OR-expression ^ AND-expression
5292 inclusive-OR-expression:
5293 exclusive-OR-expression
5294 inclusive-OR-expression | exclusive-OR-expression
5296 logical-AND-expression:
5297 inclusive-OR-expression
5298 logical-AND-expression && inclusive-OR-expression
5300 logical-OR-expression:
5301 logical-AND-expression
5302 logical-OR-expression || logical-AND-expression
5305 static struct c_expr
5306 c_parser_binary_expression (c_parser *parser, struct c_expr *after)
5308 /* A binary expression is parsed using operator-precedence parsing,
5309 with the operands being cast expressions. All the binary
5310 operators are left-associative. Thus a binary expression is of
5313 E0 op1 E1 op2 E2 ...
5315 which we represent on a stack. On the stack, the precedence
5316 levels are strictly increasing. When a new operator is
5317 encountered of higher precedence than that at the top of the
5318 stack, it is pushed; its LHS is the top expression, and its RHS
5319 is everything parsed until it is popped. When a new operator is
5320 encountered with precedence less than or equal to that at the top
5321 of the stack, triples E[i-1] op[i] E[i] are popped and replaced
5322 by the result of the operation until the operator at the top of
5323 the stack has lower precedence than the new operator or there is
5324 only one element on the stack; then the top expression is the LHS
5325 of the new operator. In the case of logical AND and OR
5326 expressions, we also need to adjust c_inhibit_evaluation_warnings
5327 as appropriate when the operators are pushed and popped. */
5329 /* The precedence levels, where 0 is a dummy lowest level used for
5330 the bottom of the stack. */
5346 /* The expression at this stack level. */
5348 /* The precedence of the operator on its left, PREC_NONE at the
5349 bottom of the stack. */
5351 /* The operation on its left. */
5353 /* The source location of this operation. */
5357 /* Location of the binary operator. */
5358 location_t binary_loc = UNKNOWN_LOCATION; /* Quiet warning. */
5361 switch (stack[sp].op) \
5363 case TRUTH_ANDIF_EXPR: \
5364 c_inhibit_evaluation_warnings -= (stack[sp - 1].expr.value \
5365 == truthvalue_false_node); \
5367 case TRUTH_ORIF_EXPR: \
5368 c_inhibit_evaluation_warnings -= (stack[sp - 1].expr.value \
5369 == truthvalue_true_node); \
5374 stack[sp - 1].expr \
5375 = default_function_array_read_conversion (stack[sp - 1].loc, \
5376 stack[sp - 1].expr); \
5378 = default_function_array_read_conversion (stack[sp].loc, \
5380 stack[sp - 1].expr = parser_build_binary_op (stack[sp].loc, \
5382 stack[sp - 1].expr, \
5386 gcc_assert (!after || c_dialect_objc ());
5387 stack[0].loc = c_parser_peek_token (parser)->location;
5388 stack[0].expr = c_parser_cast_expression (parser, after);
5389 stack[0].prec = PREC_NONE;
5394 enum tree_code ocode;
5397 switch (c_parser_peek_token (parser)->type)
5405 ocode = TRUNC_DIV_EXPR;
5409 ocode = TRUNC_MOD_EXPR;
5421 ocode = LSHIFT_EXPR;
5425 ocode = RSHIFT_EXPR;
5439 case CPP_GREATER_EQ:
5452 oprec = PREC_BITAND;
5453 ocode = BIT_AND_EXPR;
5456 oprec = PREC_BITXOR;
5457 ocode = BIT_XOR_EXPR;
5461 ocode = BIT_IOR_EXPR;
5464 oprec = PREC_LOGAND;
5465 ocode = TRUTH_ANDIF_EXPR;
5469 ocode = TRUTH_ORIF_EXPR;
5472 /* Not a binary operator, so end of the binary
5476 binary_loc = c_parser_peek_token (parser)->location;
5477 c_parser_consume_token (parser);
5478 while (oprec <= stack[sp].prec)
5482 case TRUTH_ANDIF_EXPR:
5484 = default_function_array_read_conversion (stack[sp].loc,
5486 stack[sp].expr.value = c_objc_common_truthvalue_conversion
5487 (stack[sp].loc, default_conversion (stack[sp].expr.value));
5488 c_inhibit_evaluation_warnings += (stack[sp].expr.value
5489 == truthvalue_false_node);
5491 case TRUTH_ORIF_EXPR:
5493 = default_function_array_read_conversion (stack[sp].loc,
5495 stack[sp].expr.value = c_objc_common_truthvalue_conversion
5496 (stack[sp].loc, default_conversion (stack[sp].expr.value));
5497 c_inhibit_evaluation_warnings += (stack[sp].expr.value
5498 == truthvalue_true_node);
5504 stack[sp].loc = binary_loc;
5505 stack[sp].expr = c_parser_cast_expression (parser, NULL);
5506 stack[sp].prec = oprec;
5507 stack[sp].op = ocode;
5508 stack[sp].loc = binary_loc;
5513 return stack[0].expr;
5517 /* Parse a cast expression (C90 6.3.4, C99 6.5.4). If AFTER is not
5518 NULL then it is an Objective-C message expression which is the
5519 primary-expression starting the expression as an initializer.
5523 ( type-name ) unary-expression
5526 static struct c_expr
5527 c_parser_cast_expression (c_parser *parser, struct c_expr *after)
5529 location_t cast_loc = c_parser_peek_token (parser)->location;
5530 gcc_assert (!after || c_dialect_objc ());
5532 return c_parser_postfix_expression_after_primary (parser,
5534 /* If the expression begins with a parenthesized type name, it may
5535 be either a cast or a compound literal; we need to see whether
5536 the next character is '{' to tell the difference. If not, it is
5537 an unary expression. */
5538 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
5539 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
5541 struct c_type_name *type_name;
5544 c_parser_consume_token (parser);
5545 type_name = c_parser_type_name (parser);
5546 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
5547 if (type_name == NULL)
5549 ret.value = error_mark_node;
5550 ret.original_code = ERROR_MARK;
5551 ret.original_type = NULL;
5555 /* Save casted types in the function's used types hash table. */
5556 used_types_insert (type_name->specs->type);
5558 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
5559 return c_parser_postfix_expression_after_paren_type (parser, type_name,
5562 location_t expr_loc = c_parser_peek_token (parser)->location;
5563 expr = c_parser_cast_expression (parser, NULL);
5564 expr = default_function_array_read_conversion (expr_loc, expr);
5566 ret.value = c_cast_expr (cast_loc, type_name, expr.value);
5567 ret.original_code = ERROR_MARK;
5568 ret.original_type = NULL;
5572 return c_parser_unary_expression (parser);
5575 /* Parse an unary expression (C90 6.3.3, C99 6.5.3).
5581 unary-operator cast-expression
5582 sizeof unary-expression
5583 sizeof ( type-name )
5585 unary-operator: one of
5591 __alignof__ unary-expression
5592 __alignof__ ( type-name )
5595 unary-operator: one of
5596 __extension__ __real__ __imag__
5598 In addition, the GNU syntax treats ++ and -- as unary operators, so
5599 they may be applied to cast expressions with errors for non-lvalues
5602 static struct c_expr
5603 c_parser_unary_expression (c_parser *parser)
5606 struct c_expr ret, op;
5607 location_t op_loc = c_parser_peek_token (parser)->location;
5609 ret.original_code = ERROR_MARK;
5610 ret.original_type = NULL;
5611 switch (c_parser_peek_token (parser)->type)
5614 c_parser_consume_token (parser);
5615 exp_loc = c_parser_peek_token (parser)->location;
5616 op = c_parser_cast_expression (parser, NULL);
5617 op = default_function_array_read_conversion (exp_loc, op);
5618 return parser_build_unary_op (op_loc, PREINCREMENT_EXPR, op);
5619 case CPP_MINUS_MINUS:
5620 c_parser_consume_token (parser);
5621 exp_loc = c_parser_peek_token (parser)->location;
5622 op = c_parser_cast_expression (parser, NULL);
5623 op = default_function_array_read_conversion (exp_loc, op);
5624 return parser_build_unary_op (op_loc, PREDECREMENT_EXPR, op);
5626 c_parser_consume_token (parser);
5627 op = c_parser_cast_expression (parser, NULL);
5628 mark_exp_read (op.value);
5629 return parser_build_unary_op (op_loc, ADDR_EXPR, op);
5631 c_parser_consume_token (parser);
5632 exp_loc = c_parser_peek_token (parser)->location;
5633 op = c_parser_cast_expression (parser, NULL);
5634 op = default_function_array_read_conversion (exp_loc, op);
5635 ret.value = build_indirect_ref (op_loc, op.value, RO_UNARY_STAR);
5638 if (!c_dialect_objc () && !in_system_header)
5641 "traditional C rejects the unary plus operator");
5642 c_parser_consume_token (parser);
5643 exp_loc = c_parser_peek_token (parser)->location;
5644 op = c_parser_cast_expression (parser, NULL);
5645 op = default_function_array_read_conversion (exp_loc, op);
5646 return parser_build_unary_op (op_loc, CONVERT_EXPR, op);
5648 c_parser_consume_token (parser);
5649 exp_loc = c_parser_peek_token (parser)->location;
5650 op = c_parser_cast_expression (parser, NULL);
5651 op = default_function_array_read_conversion (exp_loc, op);
5652 return parser_build_unary_op (op_loc, NEGATE_EXPR, op);
5654 c_parser_consume_token (parser);
5655 exp_loc = c_parser_peek_token (parser)->location;
5656 op = c_parser_cast_expression (parser, NULL);
5657 op = default_function_array_read_conversion (exp_loc, op);
5658 return parser_build_unary_op (op_loc, BIT_NOT_EXPR, op);
5660 c_parser_consume_token (parser);
5661 exp_loc = c_parser_peek_token (parser)->location;
5662 op = c_parser_cast_expression (parser, NULL);
5663 op = default_function_array_read_conversion (exp_loc, op);
5664 return parser_build_unary_op (op_loc, TRUTH_NOT_EXPR, op);
5666 /* Refer to the address of a label as a pointer. */
5667 c_parser_consume_token (parser);
5668 if (c_parser_next_token_is (parser, CPP_NAME))
5670 ret.value = finish_label_address_expr
5671 (c_parser_peek_token (parser)->value, op_loc);
5672 c_parser_consume_token (parser);
5676 c_parser_error (parser, "expected identifier");
5677 ret.value = error_mark_node;
5681 switch (c_parser_peek_token (parser)->keyword)
5684 return c_parser_sizeof_expression (parser);
5686 return c_parser_alignof_expression (parser);
5688 c_parser_consume_token (parser);
5689 ext = disable_extension_diagnostics ();
5690 ret = c_parser_cast_expression (parser, NULL);
5691 restore_extension_diagnostics (ext);
5694 c_parser_consume_token (parser);
5695 exp_loc = c_parser_peek_token (parser)->location;
5696 op = c_parser_cast_expression (parser, NULL);
5697 op = default_function_array_conversion (exp_loc, op);
5698 return parser_build_unary_op (op_loc, REALPART_EXPR, op);
5700 c_parser_consume_token (parser);
5701 exp_loc = c_parser_peek_token (parser)->location;
5702 op = c_parser_cast_expression (parser, NULL);
5703 op = default_function_array_conversion (exp_loc, op);
5704 return parser_build_unary_op (op_loc, IMAGPART_EXPR, op);
5706 return c_parser_postfix_expression (parser);
5709 return c_parser_postfix_expression (parser);
5713 /* Parse a sizeof expression. */
5715 static struct c_expr
5716 c_parser_sizeof_expression (c_parser *parser)
5719 location_t expr_loc;
5720 gcc_assert (c_parser_next_token_is_keyword (parser, RID_SIZEOF));
5721 c_parser_consume_token (parser);
5722 c_inhibit_evaluation_warnings++;
5724 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
5725 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
5727 /* Either sizeof ( type-name ) or sizeof unary-expression
5728 starting with a compound literal. */
5729 struct c_type_name *type_name;
5730 c_parser_consume_token (parser);
5731 expr_loc = c_parser_peek_token (parser)->location;
5732 type_name = c_parser_type_name (parser);
5733 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
5734 if (type_name == NULL)
5737 c_inhibit_evaluation_warnings--;
5739 ret.value = error_mark_node;
5740 ret.original_code = ERROR_MARK;
5741 ret.original_type = NULL;
5744 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
5746 expr = c_parser_postfix_expression_after_paren_type (parser,
5751 /* sizeof ( type-name ). */
5752 c_inhibit_evaluation_warnings--;
5754 return c_expr_sizeof_type (expr_loc, type_name);
5758 expr_loc = c_parser_peek_token (parser)->location;
5759 expr = c_parser_unary_expression (parser);
5761 c_inhibit_evaluation_warnings--;
5763 mark_exp_read (expr.value);
5764 if (TREE_CODE (expr.value) == COMPONENT_REF
5765 && DECL_C_BIT_FIELD (TREE_OPERAND (expr.value, 1)))
5766 error_at (expr_loc, "%<sizeof%> applied to a bit-field");
5767 return c_expr_sizeof_expr (expr_loc, expr);
5771 /* Parse an alignof expression. */
5773 static struct c_expr
5774 c_parser_alignof_expression (c_parser *parser)
5777 location_t loc = c_parser_peek_token (parser)->location;
5778 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ALIGNOF));
5779 c_parser_consume_token (parser);
5780 c_inhibit_evaluation_warnings++;
5782 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
5783 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
5785 /* Either __alignof__ ( type-name ) or __alignof__
5786 unary-expression starting with a compound literal. */
5788 struct c_type_name *type_name;
5790 c_parser_consume_token (parser);
5791 loc = c_parser_peek_token (parser)->location;
5792 type_name = c_parser_type_name (parser);
5793 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
5794 if (type_name == NULL)
5797 c_inhibit_evaluation_warnings--;
5799 ret.value = error_mark_node;
5800 ret.original_code = ERROR_MARK;
5801 ret.original_type = NULL;
5804 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
5806 expr = c_parser_postfix_expression_after_paren_type (parser,
5811 /* alignof ( type-name ). */
5812 c_inhibit_evaluation_warnings--;
5814 ret.value = c_alignof (loc, groktypename (type_name, NULL, NULL));
5815 ret.original_code = ERROR_MARK;
5816 ret.original_type = NULL;
5822 expr = c_parser_unary_expression (parser);
5824 mark_exp_read (expr.value);
5825 c_inhibit_evaluation_warnings--;
5827 ret.value = c_alignof_expr (loc, expr.value);
5828 ret.original_code = ERROR_MARK;
5829 ret.original_type = NULL;
5834 /* Parse a postfix expression (C90 6.3.1-6.3.2, C99 6.5.1-6.5.2).
5838 postfix-expression [ expression ]
5839 postfix-expression ( argument-expression-list[opt] )
5840 postfix-expression . identifier
5841 postfix-expression -> identifier
5842 postfix-expression ++
5843 postfix-expression --
5844 ( type-name ) { initializer-list }
5845 ( type-name ) { initializer-list , }
5847 argument-expression-list:
5849 argument-expression-list , argument-expression
5861 (treated as a keyword in GNU C)
5864 ( compound-statement )
5865 __builtin_va_arg ( assignment-expression , type-name )
5866 __builtin_offsetof ( type-name , offsetof-member-designator )
5867 __builtin_choose_expr ( assignment-expression ,
5868 assignment-expression ,
5869 assignment-expression )
5870 __builtin_types_compatible_p ( type-name , type-name )
5872 offsetof-member-designator:
5874 offsetof-member-designator . identifier
5875 offsetof-member-designator [ expression ]
5880 [ objc-receiver objc-message-args ]
5881 @selector ( objc-selector-arg )
5882 @protocol ( identifier )
5883 @encode ( type-name )
5885 Classname . identifier
5888 static struct c_expr
5889 c_parser_postfix_expression (c_parser *parser)
5891 struct c_expr expr, e1, e2, e3;
5892 struct c_type_name *t1, *t2;
5893 location_t loc = c_parser_peek_token (parser)->location;;
5894 expr.original_code = ERROR_MARK;
5895 expr.original_type = NULL;
5896 switch (c_parser_peek_token (parser)->type)
5899 expr.value = c_parser_peek_token (parser)->value;
5900 loc = c_parser_peek_token (parser)->location;
5901 c_parser_consume_token (parser);
5902 if (TREE_CODE (expr.value) == FIXED_CST
5903 && !targetm.fixed_point_supported_p ())
5905 error_at (loc, "fixed-point types not supported for this target");
5906 expr.value = error_mark_node;
5913 expr.value = c_parser_peek_token (parser)->value;
5914 c_parser_consume_token (parser);
5920 case CPP_UTF8STRING:
5921 expr.value = c_parser_peek_token (parser)->value;
5922 expr.original_code = STRING_CST;
5923 c_parser_consume_token (parser);
5925 case CPP_OBJC_STRING:
5926 gcc_assert (c_dialect_objc ());
5928 = objc_build_string_object (c_parser_peek_token (parser)->value);
5929 c_parser_consume_token (parser);
5932 switch (c_parser_peek_token (parser)->id_kind)
5936 tree id = c_parser_peek_token (parser)->value;
5937 c_parser_consume_token (parser);
5938 expr.value = build_external_ref (loc, id,
5939 (c_parser_peek_token (parser)->type
5941 &expr.original_type);
5944 case C_ID_CLASSNAME:
5946 /* Here we parse the Objective-C 2.0 Class.name dot
5948 tree class_name = c_parser_peek_token (parser)->value;
5950 c_parser_consume_token (parser);
5951 gcc_assert (c_dialect_objc ());
5952 if (!c_parser_require (parser, CPP_DOT, "expected %<.%>"))
5954 expr.value = error_mark_node;
5957 if (c_parser_next_token_is_not (parser, CPP_NAME))
5959 c_parser_error (parser, "expected identifier");
5960 expr.value = error_mark_node;
5963 component = c_parser_peek_token (parser)->value;
5964 c_parser_consume_token (parser);
5965 expr.value = objc_build_class_component_ref (class_name,
5970 c_parser_error (parser, "expected expression");
5971 expr.value = error_mark_node;
5975 case CPP_OPEN_PAREN:
5976 /* A parenthesized expression, statement expression or compound
5978 if (c_parser_peek_2nd_token (parser)->type == CPP_OPEN_BRACE)
5980 /* A statement expression. */
5982 location_t brace_loc;
5983 c_parser_consume_token (parser);
5984 brace_loc = c_parser_peek_token (parser)->location;
5985 c_parser_consume_token (parser);
5986 if (cur_stmt_list == NULL)
5988 error_at (loc, "braced-group within expression allowed "
5989 "only inside a function");
5990 parser->error = true;
5991 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
5992 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5993 expr.value = error_mark_node;
5996 stmt = c_begin_stmt_expr ();
5997 c_parser_compound_statement_nostart (parser);
5998 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
6000 pedwarn (loc, OPT_pedantic,
6001 "ISO C forbids braced-groups within expressions");
6002 expr.value = c_finish_stmt_expr (brace_loc, stmt);
6003 mark_exp_read (expr.value);
6005 else if (c_token_starts_typename (c_parser_peek_2nd_token (parser)))
6007 /* A compound literal. ??? Can we actually get here rather
6008 than going directly to
6009 c_parser_postfix_expression_after_paren_type from
6012 struct c_type_name *type_name;
6013 c_parser_consume_token (parser);
6014 loc = c_parser_peek_token (parser)->location;
6015 type_name = c_parser_type_name (parser);
6016 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
6018 if (type_name == NULL)
6020 expr.value = error_mark_node;
6023 expr = c_parser_postfix_expression_after_paren_type (parser,
6029 /* A parenthesized expression. */
6030 c_parser_consume_token (parser);
6031 expr = c_parser_expression (parser);
6032 if (TREE_CODE (expr.value) == MODIFY_EXPR)
6033 TREE_NO_WARNING (expr.value) = 1;
6034 if (expr.original_code != C_MAYBE_CONST_EXPR)
6035 expr.original_code = ERROR_MARK;
6036 /* Don't change EXPR.ORIGINAL_TYPE. */
6037 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
6042 switch (c_parser_peek_token (parser)->keyword)
6044 case RID_FUNCTION_NAME:
6045 case RID_PRETTY_FUNCTION_NAME:
6046 case RID_C99_FUNCTION_NAME:
6047 expr.value = fname_decl (loc,
6048 c_parser_peek_token (parser)->keyword,
6049 c_parser_peek_token (parser)->value);
6050 c_parser_consume_token (parser);
6053 c_parser_consume_token (parser);
6054 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6056 expr.value = error_mark_node;
6059 e1 = c_parser_expr_no_commas (parser, NULL);
6060 mark_exp_read (e1.value);
6061 e1.value = c_fully_fold (e1.value, false, NULL);
6062 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
6064 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6065 expr.value = error_mark_node;
6068 loc = c_parser_peek_token (parser)->location;
6069 t1 = c_parser_type_name (parser);
6070 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
6074 expr.value = error_mark_node;
6078 tree type_expr = NULL_TREE;
6079 expr.value = c_build_va_arg (loc, e1.value,
6080 groktypename (t1, &type_expr, NULL));
6083 expr.value = build2 (C_MAYBE_CONST_EXPR,
6084 TREE_TYPE (expr.value), type_expr,
6086 C_MAYBE_CONST_EXPR_NON_CONST (expr.value) = true;
6091 c_parser_consume_token (parser);
6092 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6094 expr.value = error_mark_node;
6097 t1 = c_parser_type_name (parser);
6100 expr.value = error_mark_node;
6103 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
6105 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6106 expr.value = error_mark_node;
6110 tree type = groktypename (t1, NULL, NULL);
6112 if (type == error_mark_node)
6113 offsetof_ref = error_mark_node;
6116 offsetof_ref = build1 (INDIRECT_REF, type, null_pointer_node);
6117 SET_EXPR_LOCATION (offsetof_ref, loc);
6119 /* Parse the second argument to __builtin_offsetof. We
6120 must have one identifier, and beyond that we want to
6121 accept sub structure and sub array references. */
6122 if (c_parser_next_token_is (parser, CPP_NAME))
6124 offsetof_ref = build_component_ref
6125 (loc, offsetof_ref, c_parser_peek_token (parser)->value);
6126 c_parser_consume_token (parser);
6127 while (c_parser_next_token_is (parser, CPP_DOT)
6128 || c_parser_next_token_is (parser,
6130 || c_parser_next_token_is (parser,
6133 if (c_parser_next_token_is (parser, CPP_DEREF))
6135 loc = c_parser_peek_token (parser)->location;
6136 offsetof_ref = build_array_ref (loc,
6141 else if (c_parser_next_token_is (parser, CPP_DOT))
6144 c_parser_consume_token (parser);
6145 if (c_parser_next_token_is_not (parser,
6148 c_parser_error (parser, "expected identifier");
6151 offsetof_ref = build_component_ref
6153 c_parser_peek_token (parser)->value);
6154 c_parser_consume_token (parser);
6159 loc = c_parser_peek_token (parser)->location;
6160 c_parser_consume_token (parser);
6161 idx = c_parser_expression (parser).value;
6162 idx = c_fully_fold (idx, false, NULL);
6163 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
6165 offsetof_ref = build_array_ref (loc, offsetof_ref, idx);
6170 c_parser_error (parser, "expected identifier");
6171 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
6173 expr.value = fold_offsetof (offsetof_ref, NULL_TREE);
6176 case RID_CHOOSE_EXPR:
6177 c_parser_consume_token (parser);
6178 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6180 expr.value = error_mark_node;
6183 loc = c_parser_peek_token (parser)->location;
6184 e1 = c_parser_expr_no_commas (parser, NULL);
6185 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
6187 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6188 expr.value = error_mark_node;
6191 e2 = c_parser_expr_no_commas (parser, NULL);
6192 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
6194 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6195 expr.value = error_mark_node;
6198 e3 = c_parser_expr_no_commas (parser, NULL);
6199 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
6205 mark_exp_read (e2.value);
6206 mark_exp_read (e3.value);
6207 if (TREE_CODE (c) != INTEGER_CST
6208 || !INTEGRAL_TYPE_P (TREE_TYPE (c)))
6210 "first argument to %<__builtin_choose_expr%> not"
6212 constant_expression_warning (c);
6213 expr = integer_zerop (c) ? e3 : e2;
6216 case RID_TYPES_COMPATIBLE_P:
6217 c_parser_consume_token (parser);
6218 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6220 expr.value = error_mark_node;
6223 t1 = c_parser_type_name (parser);
6226 expr.value = error_mark_node;
6229 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
6231 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6232 expr.value = error_mark_node;
6235 t2 = c_parser_type_name (parser);
6238 expr.value = error_mark_node;
6241 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
6246 e1 = TYPE_MAIN_VARIANT (groktypename (t1, NULL, NULL));
6247 e2 = TYPE_MAIN_VARIANT (groktypename (t2, NULL, NULL));
6250 = comptypes (e1, e2) ? integer_one_node : integer_zero_node;
6253 case RID_AT_SELECTOR:
6254 gcc_assert (c_dialect_objc ());
6255 c_parser_consume_token (parser);
6256 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6258 expr.value = error_mark_node;
6262 tree sel = c_parser_objc_selector_arg (parser);
6263 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
6265 expr.value = objc_build_selector_expr (loc, sel);
6268 case RID_AT_PROTOCOL:
6269 gcc_assert (c_dialect_objc ());
6270 c_parser_consume_token (parser);
6271 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6273 expr.value = error_mark_node;
6276 if (c_parser_next_token_is_not (parser, CPP_NAME))
6278 c_parser_error (parser, "expected identifier");
6279 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6280 expr.value = error_mark_node;
6284 tree id = c_parser_peek_token (parser)->value;
6285 c_parser_consume_token (parser);
6286 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
6288 expr.value = objc_build_protocol_expr (id);
6292 /* Extension to support C-structures in the archiver. */
6293 gcc_assert (c_dialect_objc ());
6294 c_parser_consume_token (parser);
6295 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6297 expr.value = error_mark_node;
6300 t1 = c_parser_type_name (parser);
6303 expr.value = error_mark_node;
6304 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6307 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
6310 tree type = groktypename (t1, NULL, NULL);
6311 expr.value = objc_build_encode_expr (type);
6315 c_parser_error (parser, "expected expression");
6316 expr.value = error_mark_node;
6320 case CPP_OPEN_SQUARE:
6321 if (c_dialect_objc ())
6323 tree receiver, args;
6324 c_parser_consume_token (parser);
6325 receiver = c_parser_objc_receiver (parser);
6326 args = c_parser_objc_message_args (parser);
6327 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
6329 expr.value = objc_build_message_expr (build_tree_list (receiver,
6333 /* Else fall through to report error. */
6335 c_parser_error (parser, "expected expression");
6336 expr.value = error_mark_node;
6339 return c_parser_postfix_expression_after_primary (parser, loc, expr);
6342 /* Parse a postfix expression after a parenthesized type name: the
6343 brace-enclosed initializer of a compound literal, possibly followed
6344 by some postfix operators. This is separate because it is not
6345 possible to tell until after the type name whether a cast
6346 expression has a cast or a compound literal, or whether the operand
6347 of sizeof is a parenthesized type name or starts with a compound
6348 literal. TYPE_LOC is the location where TYPE_NAME starts--the
6349 location of the first token after the parentheses around the type
6352 static struct c_expr
6353 c_parser_postfix_expression_after_paren_type (c_parser *parser,
6354 struct c_type_name *type_name,
6355 location_t type_loc)
6361 location_t start_loc;
6362 tree type_expr = NULL_TREE;
6363 bool type_expr_const = true;
6364 check_compound_literal_type (type_loc, type_name);
6365 start_init (NULL_TREE, NULL, 0);
6366 type = groktypename (type_name, &type_expr, &type_expr_const);
6367 start_loc = c_parser_peek_token (parser)->location;
6368 if (type != error_mark_node && C_TYPE_VARIABLE_SIZE (type))
6370 error_at (type_loc, "compound literal has variable size");
6371 type = error_mark_node;
6373 init = c_parser_braced_init (parser, type, false);
6375 maybe_warn_string_init (type, init);
6377 if (type != error_mark_node
6378 && !ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (type))
6379 && current_function_decl)
6381 error ("compound literal qualified by address-space qualifier");
6382 type = error_mark_node;
6386 pedwarn (start_loc, OPT_pedantic, "ISO C90 forbids compound literals");
6387 non_const = ((init.value && TREE_CODE (init.value) == CONSTRUCTOR)
6388 ? CONSTRUCTOR_NON_CONST (init.value)
6389 : init.original_code == C_MAYBE_CONST_EXPR);
6390 non_const |= !type_expr_const;
6391 expr.value = build_compound_literal (start_loc, type, init.value, non_const);
6392 expr.original_code = ERROR_MARK;
6393 expr.original_type = NULL;
6396 if (TREE_CODE (expr.value) == C_MAYBE_CONST_EXPR)
6398 gcc_assert (C_MAYBE_CONST_EXPR_PRE (expr.value) == NULL_TREE);
6399 C_MAYBE_CONST_EXPR_PRE (expr.value) = type_expr;
6403 gcc_assert (!non_const);
6404 expr.value = build2 (C_MAYBE_CONST_EXPR, type,
6405 type_expr, expr.value);
6408 return c_parser_postfix_expression_after_primary (parser, start_loc, expr);
6411 /* Parse a postfix expression after the initial primary or compound
6412 literal; that is, parse a series of postfix operators.
6414 EXPR_LOC is the location of the primary expression. */
6416 static struct c_expr
6417 c_parser_postfix_expression_after_primary (c_parser *parser,
6418 location_t expr_loc,
6421 struct c_expr orig_expr;
6423 VEC(tree,gc) *exprlist;
6424 VEC(tree,gc) *origtypes;
6427 location_t op_loc = c_parser_peek_token (parser)->location;
6428 switch (c_parser_peek_token (parser)->type)
6430 case CPP_OPEN_SQUARE:
6431 /* Array reference. */
6432 c_parser_consume_token (parser);
6433 idx = c_parser_expression (parser).value;
6434 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
6436 expr.value = build_array_ref (op_loc, expr.value, idx);
6437 expr.original_code = ERROR_MARK;
6438 expr.original_type = NULL;
6440 case CPP_OPEN_PAREN:
6441 /* Function call. */
6442 c_parser_consume_token (parser);
6443 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
6446 exprlist = c_parser_expr_list (parser, true, false, &origtypes);
6447 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
6450 mark_exp_read (expr.value);
6451 /* FIXME diagnostics: Ideally we want the FUNCNAME, not the
6452 "(" after the FUNCNAME, which is what we have now. */
6453 expr.value = build_function_call_vec (op_loc, expr.value, exprlist,
6455 expr.original_code = ERROR_MARK;
6456 if (TREE_CODE (expr.value) == INTEGER_CST
6457 && TREE_CODE (orig_expr.value) == FUNCTION_DECL
6458 && DECL_BUILT_IN_CLASS (orig_expr.value) == BUILT_IN_NORMAL
6459 && DECL_FUNCTION_CODE (orig_expr.value) == BUILT_IN_CONSTANT_P)
6460 expr.original_code = C_MAYBE_CONST_EXPR;
6461 expr.original_type = NULL;
6462 if (exprlist != NULL)
6464 release_tree_vector (exprlist);
6465 release_tree_vector (origtypes);
6469 /* Structure element reference. */
6470 c_parser_consume_token (parser);
6471 expr = default_function_array_conversion (expr_loc, expr);
6472 if (c_parser_next_token_is (parser, CPP_NAME))
6473 ident = c_parser_peek_token (parser)->value;
6476 c_parser_error (parser, "expected identifier");
6477 expr.value = error_mark_node;
6478 expr.original_code = ERROR_MARK;
6479 expr.original_type = NULL;
6482 c_parser_consume_token (parser);
6483 expr.value = build_component_ref (op_loc, expr.value, ident);
6484 expr.original_code = ERROR_MARK;
6485 if (TREE_CODE (expr.value) != COMPONENT_REF)
6486 expr.original_type = NULL;
6489 /* Remember the original type of a bitfield. */
6490 tree field = TREE_OPERAND (expr.value, 1);
6491 if (TREE_CODE (field) != FIELD_DECL)
6492 expr.original_type = NULL;
6494 expr.original_type = DECL_BIT_FIELD_TYPE (field);
6498 /* Structure element reference. */
6499 c_parser_consume_token (parser);
6500 expr = default_function_array_conversion (expr_loc, expr);
6501 if (c_parser_next_token_is (parser, CPP_NAME))
6502 ident = c_parser_peek_token (parser)->value;
6505 c_parser_error (parser, "expected identifier");
6506 expr.value = error_mark_node;
6507 expr.original_code = ERROR_MARK;
6508 expr.original_type = NULL;
6511 c_parser_consume_token (parser);
6512 expr.value = build_component_ref (op_loc,
6513 build_indirect_ref (op_loc,
6517 expr.original_code = ERROR_MARK;
6518 if (TREE_CODE (expr.value) != COMPONENT_REF)
6519 expr.original_type = NULL;
6522 /* Remember the original type of a bitfield. */
6523 tree field = TREE_OPERAND (expr.value, 1);
6524 if (TREE_CODE (field) != FIELD_DECL)
6525 expr.original_type = NULL;
6527 expr.original_type = DECL_BIT_FIELD_TYPE (field);
6531 /* Postincrement. */
6532 c_parser_consume_token (parser);
6533 expr = default_function_array_read_conversion (expr_loc, expr);
6534 expr.value = build_unary_op (op_loc,
6535 POSTINCREMENT_EXPR, expr.value, 0);
6536 expr.original_code = ERROR_MARK;
6537 expr.original_type = NULL;
6539 case CPP_MINUS_MINUS:
6540 /* Postdecrement. */
6541 c_parser_consume_token (parser);
6542 expr = default_function_array_read_conversion (expr_loc, expr);
6543 expr.value = build_unary_op (op_loc,
6544 POSTDECREMENT_EXPR, expr.value, 0);
6545 expr.original_code = ERROR_MARK;
6546 expr.original_type = NULL;
6554 /* Parse an expression (C90 6.3.17, C99 6.5.17).
6557 assignment-expression
6558 expression , assignment-expression
6561 static struct c_expr
6562 c_parser_expression (c_parser *parser)
6565 expr = c_parser_expr_no_commas (parser, NULL);
6566 while (c_parser_next_token_is (parser, CPP_COMMA))
6570 location_t loc = c_parser_peek_token (parser)->location;
6571 location_t expr_loc;
6572 c_parser_consume_token (parser);
6573 expr_loc = c_parser_peek_token (parser)->location;
6574 lhsval = expr.value;
6575 while (TREE_CODE (lhsval) == COMPOUND_EXPR)
6576 lhsval = TREE_OPERAND (lhsval, 1);
6577 if (DECL_P (lhsval) || handled_component_p (lhsval))
6578 mark_exp_read (lhsval);
6579 next = c_parser_expr_no_commas (parser, NULL);
6580 next = default_function_array_conversion (expr_loc, next);
6581 expr.value = build_compound_expr (loc, expr.value, next.value);
6582 expr.original_code = COMPOUND_EXPR;
6583 expr.original_type = next.original_type;
6588 /* Parse an expression and convert functions or arrays to
6591 static struct c_expr
6592 c_parser_expression_conv (c_parser *parser)
6595 location_t loc = c_parser_peek_token (parser)->location;
6596 expr = c_parser_expression (parser);
6597 expr = default_function_array_conversion (loc, expr);
6601 /* Parse a non-empty list of expressions. If CONVERT_P, convert
6602 functions and arrays to pointers. If FOLD_P, fold the expressions.
6605 assignment-expression
6606 nonempty-expr-list , assignment-expression
6609 static VEC(tree,gc) *
6610 c_parser_expr_list (c_parser *parser, bool convert_p, bool fold_p,
6611 VEC(tree,gc) **p_orig_types)
6614 VEC(tree,gc) *orig_types;
6616 location_t loc = c_parser_peek_token (parser)->location;
6618 ret = make_tree_vector ();
6619 if (p_orig_types == NULL)
6622 orig_types = make_tree_vector ();
6624 expr = c_parser_expr_no_commas (parser, NULL);
6626 expr = default_function_array_read_conversion (loc, expr);
6628 expr.value = c_fully_fold (expr.value, false, NULL);
6629 VEC_quick_push (tree, ret, expr.value);
6630 if (orig_types != NULL)
6631 VEC_quick_push (tree, orig_types, expr.original_type);
6632 while (c_parser_next_token_is (parser, CPP_COMMA))
6634 c_parser_consume_token (parser);
6635 loc = c_parser_peek_token (parser)->location;
6636 expr = c_parser_expr_no_commas (parser, NULL);
6638 expr = default_function_array_read_conversion (loc, expr);
6640 expr.value = c_fully_fold (expr.value, false, NULL);
6641 VEC_safe_push (tree, gc, ret, expr.value);
6642 if (orig_types != NULL)
6643 VEC_safe_push (tree, gc, orig_types, expr.original_type);
6645 if (orig_types != NULL)
6646 *p_orig_types = orig_types;
6650 /* Parse Objective-C-specific constructs. */
6652 /* Parse an objc-class-definition.
6654 objc-class-definition:
6655 @interface identifier objc-superclass[opt] objc-protocol-refs[opt]
6656 objc-class-instance-variables[opt] objc-methodprotolist @end
6657 @implementation identifier objc-superclass[opt]
6658 objc-class-instance-variables[opt]
6659 @interface identifier ( identifier ) objc-protocol-refs[opt]
6660 objc-methodprotolist @end
6661 @implementation identifier ( identifier )
6666 "@interface identifier (" must start "@interface identifier (
6667 identifier ) ...": objc-methodprotolist in the first production may
6668 not start with a parenthesized identifier as a declarator of a data
6669 definition with no declaration specifiers if the objc-superclass,
6670 objc-protocol-refs and objc-class-instance-variables are omitted. */
6673 c_parser_objc_class_definition (c_parser *parser, tree attributes)
6678 if (c_parser_next_token_is_keyword (parser, RID_AT_INTERFACE))
6680 else if (c_parser_next_token_is_keyword (parser, RID_AT_IMPLEMENTATION))
6685 c_parser_consume_token (parser);
6686 if (c_parser_next_token_is_not (parser, CPP_NAME))
6688 c_parser_error (parser, "expected identifier");
6691 id1 = c_parser_peek_token (parser)->value;
6692 c_parser_consume_token (parser);
6693 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
6696 tree proto = NULL_TREE;
6697 c_parser_consume_token (parser);
6698 if (c_parser_next_token_is_not (parser, CPP_NAME))
6700 c_parser_error (parser, "expected identifier");
6701 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6704 id2 = c_parser_peek_token (parser)->value;
6705 c_parser_consume_token (parser);
6706 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
6709 objc_start_category_implementation (id1, id2);
6712 if (c_parser_next_token_is (parser, CPP_LESS))
6713 proto = c_parser_objc_protocol_refs (parser);
6714 objc_start_category_interface (id1, id2, proto, attributes);
6715 c_parser_objc_methodprotolist (parser);
6716 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
6717 objc_finish_interface ();
6720 if (c_parser_next_token_is (parser, CPP_COLON))
6722 c_parser_consume_token (parser);
6723 if (c_parser_next_token_is_not (parser, CPP_NAME))
6725 c_parser_error (parser, "expected identifier");
6728 superclass = c_parser_peek_token (parser)->value;
6729 c_parser_consume_token (parser);
6732 superclass = NULL_TREE;
6735 tree proto = NULL_TREE;
6736 if (c_parser_next_token_is (parser, CPP_LESS))
6737 proto = c_parser_objc_protocol_refs (parser);
6738 objc_start_class_interface (id1, superclass, proto, attributes);
6741 objc_start_class_implementation (id1, superclass);
6742 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
6743 c_parser_objc_class_instance_variables (parser);
6746 objc_continue_interface ();
6747 c_parser_objc_methodprotolist (parser);
6748 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
6749 objc_finish_interface ();
6753 objc_continue_implementation ();
6758 /* Parse objc-class-instance-variables.
6760 objc-class-instance-variables:
6761 { objc-instance-variable-decl-list[opt] }
6763 objc-instance-variable-decl-list:
6764 objc-visibility-spec
6765 objc-instance-variable-decl ;
6767 objc-instance-variable-decl-list objc-visibility-spec
6768 objc-instance-variable-decl-list objc-instance-variable-decl ;
6769 objc-instance-variable-decl-list ;
6771 objc-visibility-spec:
6776 objc-instance-variable-decl:
6781 c_parser_objc_class_instance_variables (c_parser *parser)
6783 gcc_assert (c_parser_next_token_is (parser, CPP_OPEN_BRACE));
6784 c_parser_consume_token (parser);
6785 while (c_parser_next_token_is_not (parser, CPP_EOF))
6788 /* Parse any stray semicolon. */
6789 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
6791 pedwarn (c_parser_peek_token (parser)->location, OPT_pedantic,
6792 "extra semicolon in struct or union specified");
6793 c_parser_consume_token (parser);
6796 /* Stop if at the end of the instance variables. */
6797 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
6799 c_parser_consume_token (parser);
6802 /* Parse any objc-visibility-spec. */
6803 if (c_parser_next_token_is_keyword (parser, RID_AT_PRIVATE))
6805 c_parser_consume_token (parser);
6806 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
6809 else if (c_parser_next_token_is_keyword (parser, RID_AT_PROTECTED))
6811 c_parser_consume_token (parser);
6812 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
6815 else if (c_parser_next_token_is_keyword (parser, RID_AT_PUBLIC))
6817 c_parser_consume_token (parser);
6818 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
6821 else if (c_parser_next_token_is_keyword (parser, RID_AT_PACKAGE))
6823 c_parser_consume_token (parser);
6824 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
6827 else if (c_parser_next_token_is (parser, CPP_PRAGMA))
6829 c_parser_pragma (parser, pragma_external);
6833 /* Parse some comma-separated declarations. */
6834 decls = c_parser_struct_declaration (parser);
6836 /* Comma-separated instance variables are chained together in
6837 reverse order; add them one by one. */
6838 tree ivar = nreverse (decls);
6839 for (; ivar; ivar = DECL_CHAIN (ivar))
6840 objc_add_instance_variable (copy_node (ivar));
6842 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
6846 /* Parse an objc-class-declaration.
6848 objc-class-declaration:
6849 @class identifier-list ;
6853 c_parser_objc_class_declaration (c_parser *parser)
6855 tree list = NULL_TREE;
6856 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_CLASS));
6857 c_parser_consume_token (parser);
6858 /* Any identifiers, including those declared as type names, are OK
6863 if (c_parser_next_token_is_not (parser, CPP_NAME))
6865 c_parser_error (parser, "expected identifier");
6866 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
6867 parser->error = false;
6870 id = c_parser_peek_token (parser)->value;
6871 list = chainon (list, build_tree_list (NULL_TREE, id));
6872 c_parser_consume_token (parser);
6873 if (c_parser_next_token_is (parser, CPP_COMMA))
6874 c_parser_consume_token (parser);
6878 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
6879 objc_declare_class (list);
6882 /* Parse an objc-alias-declaration.
6884 objc-alias-declaration:
6885 @compatibility_alias identifier identifier ;
6889 c_parser_objc_alias_declaration (c_parser *parser)
6892 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_ALIAS));
6893 c_parser_consume_token (parser);
6894 if (c_parser_next_token_is_not (parser, CPP_NAME))
6896 c_parser_error (parser, "expected identifier");
6897 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
6900 id1 = c_parser_peek_token (parser)->value;
6901 c_parser_consume_token (parser);
6902 if (c_parser_next_token_is_not (parser, CPP_NAME))
6904 c_parser_error (parser, "expected identifier");
6905 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
6908 id2 = c_parser_peek_token (parser)->value;
6909 c_parser_consume_token (parser);
6910 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
6911 objc_declare_alias (id1, id2);
6914 /* Parse an objc-protocol-definition.
6916 objc-protocol-definition:
6917 @protocol identifier objc-protocol-refs[opt] objc-methodprotolist @end
6918 @protocol identifier-list ;
6920 "@protocol identifier ;" should be resolved as "@protocol
6921 identifier-list ;": objc-methodprotolist may not start with a
6922 semicolon in the first alternative if objc-protocol-refs are
6926 c_parser_objc_protocol_definition (c_parser *parser, tree attributes)
6928 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_PROTOCOL));
6930 c_parser_consume_token (parser);
6931 if (c_parser_next_token_is_not (parser, CPP_NAME))
6933 c_parser_error (parser, "expected identifier");
6936 if (c_parser_peek_2nd_token (parser)->type == CPP_COMMA
6937 || c_parser_peek_2nd_token (parser)->type == CPP_SEMICOLON)
6939 tree list = NULL_TREE;
6940 /* Any identifiers, including those declared as type names, are
6945 if (c_parser_next_token_is_not (parser, CPP_NAME))
6947 c_parser_error (parser, "expected identifier");
6950 id = c_parser_peek_token (parser)->value;
6951 list = chainon (list, build_tree_list (NULL_TREE, id));
6952 c_parser_consume_token (parser);
6953 if (c_parser_next_token_is (parser, CPP_COMMA))
6954 c_parser_consume_token (parser);
6958 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
6959 objc_declare_protocols (list);
6963 tree id = c_parser_peek_token (parser)->value;
6964 tree proto = NULL_TREE;
6965 c_parser_consume_token (parser);
6966 if (c_parser_next_token_is (parser, CPP_LESS))
6967 proto = c_parser_objc_protocol_refs (parser);
6968 parser->objc_pq_context = true;
6969 objc_start_protocol (id, proto, attributes);
6970 c_parser_objc_methodprotolist (parser);
6971 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
6972 parser->objc_pq_context = false;
6973 objc_finish_interface ();
6977 /* Parse an objc-method-type.
6983 Return true if it is a class method (+) and false if it is
6984 an instance method (-).
6987 c_parser_objc_method_type (c_parser *parser)
6989 switch (c_parser_peek_token (parser)->type)
6992 c_parser_consume_token (parser);
6995 c_parser_consume_token (parser);
7002 /* Parse an objc-method-definition.
7004 objc-method-definition:
7005 objc-method-type objc-method-decl ;[opt] compound-statement
7009 c_parser_objc_method_definition (c_parser *parser)
7011 bool is_class_method = c_parser_objc_method_type (parser);
7012 tree decl, attributes = NULL_TREE;
7013 parser->objc_pq_context = true;
7014 decl = c_parser_objc_method_decl (parser, is_class_method, &attributes);
7015 if (decl == error_mark_node)
7016 return; /* Bail here. */
7018 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
7020 c_parser_consume_token (parser);
7021 pedwarn (c_parser_peek_token (parser)->location, OPT_pedantic,
7022 "extra semicolon in method definition specified");
7025 if (!c_parser_next_token_is (parser, CPP_OPEN_BRACE))
7027 c_parser_error (parser, "expected %<{%>");
7031 parser->objc_pq_context = false;
7032 if (objc_start_method_definition (is_class_method, decl, attributes))
7034 add_stmt (c_parser_compound_statement (parser));
7035 objc_finish_method_definition (current_function_decl);
7039 /* This code is executed when we find a method definition
7040 outside of an @implementation context (or invalid for other
7041 reasons). Parse the method (to keep going) but do not emit
7044 c_parser_compound_statement (parser);
7048 /* Parse an objc-methodprotolist.
7050 objc-methodprotolist:
7052 objc-methodprotolist objc-methodproto
7053 objc-methodprotolist declaration
7054 objc-methodprotolist ;
7058 The declaration is a data definition, which may be missing
7059 declaration specifiers under the same rules and diagnostics as
7060 other data definitions outside functions, and the stray semicolon
7061 is diagnosed the same way as a stray semicolon outside a
7065 c_parser_objc_methodprotolist (c_parser *parser)
7069 /* The list is terminated by @end. */
7070 switch (c_parser_peek_token (parser)->type)
7073 pedwarn (c_parser_peek_token (parser)->location, OPT_pedantic,
7074 "ISO C does not allow extra %<;%> outside of a function");
7075 c_parser_consume_token (parser);
7079 c_parser_objc_methodproto (parser);
7082 c_parser_pragma (parser, pragma_external);
7087 if (c_parser_next_token_is_keyword (parser, RID_AT_END))
7089 else if (c_parser_next_token_is_keyword (parser, RID_AT_PROPERTY))
7090 c_parser_objc_at_property_declaration (parser);
7091 else if (c_parser_next_token_is_keyword (parser, RID_AT_OPTIONAL))
7093 objc_set_method_opt (true);
7094 c_parser_consume_token (parser);
7096 else if (c_parser_next_token_is_keyword (parser, RID_AT_REQUIRED))
7098 objc_set_method_opt (false);
7099 c_parser_consume_token (parser);
7102 c_parser_declaration_or_fndef (parser, false, false, true,
7109 /* Parse an objc-methodproto.
7112 objc-method-type objc-method-decl ;
7116 c_parser_objc_methodproto (c_parser *parser)
7118 bool is_class_method = c_parser_objc_method_type (parser);
7119 tree decl, attributes = NULL_TREE;
7121 /* Remember protocol qualifiers in prototypes. */
7122 parser->objc_pq_context = true;
7123 decl = c_parser_objc_method_decl (parser, is_class_method, &attributes);
7124 /* Forget protocol qualifiers now. */
7125 parser->objc_pq_context = false;
7127 /* Do not allow the presence of attributes to hide an erroneous
7128 method implementation in the interface section. */
7129 if (!c_parser_next_token_is (parser, CPP_SEMICOLON))
7131 c_parser_error (parser, "expected %<;%>");
7135 if (decl != error_mark_node)
7136 objc_add_method_declaration (is_class_method, decl, attributes);
7138 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
7141 /* If we are at a position that method attributes may be present, check that
7142 there are not any parsed already (a syntax error) and then collect any
7143 specified at the current location. Finally, if new attributes were present,
7144 check that the next token is legal ( ';' for decls and '{' for defs). */
7147 c_parser_objc_maybe_method_attributes (c_parser* parser, tree* attributes)
7152 c_parser_error (parser,
7153 "method attributes must be specified at the end only");
7154 *attributes = NULL_TREE;
7158 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
7159 *attributes = c_parser_attributes (parser);
7161 /* If there were no attributes here, just report any earlier error. */
7162 if (*attributes == NULL_TREE || bad)
7165 /* If the attributes are followed by a ; or {, then just report any earlier
7167 if (c_parser_next_token_is (parser, CPP_SEMICOLON)
7168 || c_parser_next_token_is (parser, CPP_OPEN_BRACE))
7171 /* We've got attributes, but not at the end. */
7172 c_parser_error (parser,
7173 "expected %<;%> or %<{%> after method attribute definition");
7177 /* Parse an objc-method-decl.
7180 ( objc-type-name ) objc-selector
7182 ( objc-type-name ) objc-keyword-selector objc-optparmlist
7183 objc-keyword-selector objc-optparmlist
7186 objc-keyword-selector:
7188 objc-keyword-selector objc-keyword-decl
7191 objc-selector : ( objc-type-name ) identifier
7192 objc-selector : identifier
7193 : ( objc-type-name ) identifier
7197 objc-optparms objc-optellipsis
7201 objc-opt-parms , parameter-declaration
7209 c_parser_objc_method_decl (c_parser *parser, bool is_class_method, tree *attributes)
7211 tree type = NULL_TREE;
7213 tree parms = NULL_TREE;
7214 bool ellipsis = false;
7215 bool attr_err = false;
7217 *attributes = NULL_TREE;
7218 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
7220 c_parser_consume_token (parser);
7221 type = c_parser_objc_type_name (parser);
7222 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
7224 sel = c_parser_objc_selector (parser);
7225 /* If there is no selector, or a colon follows, we have an
7226 objc-keyword-selector. If there is a selector, and a colon does
7227 not follow, that selector ends the objc-method-decl. */
7228 if (!sel || c_parser_next_token_is (parser, CPP_COLON))
7231 tree list = NULL_TREE;
7234 tree atype = NULL_TREE, id, keyworddecl;
7235 tree param_attr = NULL_TREE;
7236 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
7238 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
7240 c_parser_consume_token (parser);
7241 atype = c_parser_objc_type_name (parser);
7242 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7245 /* New ObjC allows attributes on method parameters. */
7246 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
7247 param_attr = c_parser_attributes (parser);
7248 if (c_parser_next_token_is_not (parser, CPP_NAME))
7250 c_parser_error (parser, "expected identifier");
7251 return error_mark_node;
7253 id = c_parser_peek_token (parser)->value;
7254 c_parser_consume_token (parser);
7255 keyworddecl = objc_build_keyword_decl (tsel, atype, id, param_attr);
7256 list = chainon (list, keyworddecl);
7257 tsel = c_parser_objc_selector (parser);
7258 if (!tsel && c_parser_next_token_is_not (parser, CPP_COLON))
7262 attr_err |= c_parser_objc_maybe_method_attributes (parser, attributes) ;
7264 /* Parse the optional parameter list. Optional Objective-C
7265 method parameters follow the C syntax, and may include '...'
7266 to denote a variable number of arguments. */
7267 parms = make_node (TREE_LIST);
7268 while (c_parser_next_token_is (parser, CPP_COMMA))
7270 struct c_parm *parm;
7271 c_parser_consume_token (parser);
7272 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
7275 c_parser_consume_token (parser);
7276 attr_err |= c_parser_objc_maybe_method_attributes
7277 (parser, attributes) ;
7280 parm = c_parser_parameter_declaration (parser, NULL_TREE);
7283 parms = chainon (parms,
7284 build_tree_list (NULL_TREE, grokparm (parm)));
7289 attr_err |= c_parser_objc_maybe_method_attributes (parser, attributes) ;
7293 c_parser_error (parser, "objective-c method declaration is expected");
7294 return error_mark_node;
7298 return error_mark_node;
7300 return objc_build_method_signature (is_class_method, type, sel, parms, ellipsis);
7303 /* Parse an objc-type-name.
7306 objc-type-qualifiers[opt] type-name
7307 objc-type-qualifiers[opt]
7309 objc-type-qualifiers:
7311 objc-type-qualifiers objc-type-qualifier
7313 objc-type-qualifier: one of
7314 in out inout bycopy byref oneway
7318 c_parser_objc_type_name (c_parser *parser)
7320 tree quals = NULL_TREE;
7321 struct c_type_name *type_name = NULL;
7322 tree type = NULL_TREE;
7325 c_token *token = c_parser_peek_token (parser);
7326 if (token->type == CPP_KEYWORD
7327 && (token->keyword == RID_IN
7328 || token->keyword == RID_OUT
7329 || token->keyword == RID_INOUT
7330 || token->keyword == RID_BYCOPY
7331 || token->keyword == RID_BYREF
7332 || token->keyword == RID_ONEWAY))
7334 quals = chainon (build_tree_list (NULL_TREE, token->value), quals);
7335 c_parser_consume_token (parser);
7340 if (c_parser_next_token_starts_typename (parser))
7341 type_name = c_parser_type_name (parser);
7343 type = groktypename (type_name, NULL, NULL);
7344 return build_tree_list (quals, type);
7347 /* Parse objc-protocol-refs.
7354 c_parser_objc_protocol_refs (c_parser *parser)
7356 tree list = NULL_TREE;
7357 gcc_assert (c_parser_next_token_is (parser, CPP_LESS));
7358 c_parser_consume_token (parser);
7359 /* Any identifiers, including those declared as type names, are OK
7364 if (c_parser_next_token_is_not (parser, CPP_NAME))
7366 c_parser_error (parser, "expected identifier");
7369 id = c_parser_peek_token (parser)->value;
7370 list = chainon (list, build_tree_list (NULL_TREE, id));
7371 c_parser_consume_token (parser);
7372 if (c_parser_next_token_is (parser, CPP_COMMA))
7373 c_parser_consume_token (parser);
7377 c_parser_require (parser, CPP_GREATER, "expected %<>%>");
7381 /* Parse an objc-try-catch-statement.
7383 objc-try-catch-statement:
7384 @try compound-statement objc-catch-list[opt]
7385 @try compound-statement objc-catch-list[opt] @finally compound-statement
7388 @catch ( parameter-declaration ) compound-statement
7389 objc-catch-list @catch ( parameter-declaration ) compound-statement
7393 c_parser_objc_try_catch_statement (c_parser *parser)
7397 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_TRY));
7398 c_parser_consume_token (parser);
7399 loc = c_parser_peek_token (parser)->location;
7400 stmt = c_parser_compound_statement (parser);
7401 objc_begin_try_stmt (loc, stmt);
7402 while (c_parser_next_token_is_keyword (parser, RID_AT_CATCH))
7404 struct c_parm *parm;
7405 c_parser_consume_token (parser);
7406 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7408 parm = c_parser_parameter_declaration (parser, NULL_TREE);
7411 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7414 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
7415 objc_begin_catch_clause (grokparm (parm));
7416 if (c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
7417 c_parser_compound_statement_nostart (parser);
7418 objc_finish_catch_clause ();
7420 if (c_parser_next_token_is_keyword (parser, RID_AT_FINALLY))
7424 c_parser_consume_token (parser);
7425 finloc = c_parser_peek_token (parser)->location;
7426 finstmt = c_parser_compound_statement (parser);
7427 objc_build_finally_clause (finloc, finstmt);
7429 objc_finish_try_stmt ();
7432 /* Parse an objc-synchronized-statement.
7434 objc-synchronized-statement:
7435 @synchronized ( expression ) compound-statement
7439 c_parser_objc_synchronized_statement (c_parser *parser)
7443 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_SYNCHRONIZED));
7444 c_parser_consume_token (parser);
7445 loc = c_parser_peek_token (parser)->location;
7446 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7448 expr = c_parser_expression (parser).value;
7449 expr = c_fully_fold (expr, false, NULL);
7450 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
7453 expr = error_mark_node;
7454 stmt = c_parser_compound_statement (parser);
7455 objc_build_synchronized (loc, expr, stmt);
7458 /* Parse an objc-selector; return NULL_TREE without an error if the
7459 next token is not an objc-selector.
7464 enum struct union if else while do for switch case default
7465 break continue return goto asm sizeof typeof __alignof
7466 unsigned long const short volatile signed restrict _Complex
7467 in out inout bycopy byref oneway int char float double void _Bool
7469 ??? Why this selection of keywords but not, for example, storage
7470 class specifiers? */
7473 c_parser_objc_selector (c_parser *parser)
7475 c_token *token = c_parser_peek_token (parser);
7476 tree value = token->value;
7477 if (token->type == CPP_NAME)
7479 c_parser_consume_token (parser);
7482 if (token->type != CPP_KEYWORD)
7484 switch (token->keyword)
7526 c_parser_consume_token (parser);
7533 /* Parse an objc-selector-arg.
7537 objc-keywordname-list
7539 objc-keywordname-list:
7541 objc-keywordname-list objc-keywordname
7549 c_parser_objc_selector_arg (c_parser *parser)
7551 tree sel = c_parser_objc_selector (parser);
7552 tree list = NULL_TREE;
7553 if (sel && c_parser_next_token_is_not (parser, CPP_COLON))
7557 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
7559 list = chainon (list, build_tree_list (sel, NULL_TREE));
7560 sel = c_parser_objc_selector (parser);
7561 if (!sel && c_parser_next_token_is_not (parser, CPP_COLON))
7567 /* Parse an objc-receiver.
7576 c_parser_objc_receiver (c_parser *parser)
7578 if (c_parser_peek_token (parser)->type == CPP_NAME
7579 && (c_parser_peek_token (parser)->id_kind == C_ID_TYPENAME
7580 || c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME))
7582 tree id = c_parser_peek_token (parser)->value;
7583 c_parser_consume_token (parser);
7584 return objc_get_class_reference (id);
7586 return c_fully_fold (c_parser_expression (parser).value, false, NULL);
7589 /* Parse objc-message-args.
7593 objc-keywordarg-list
7595 objc-keywordarg-list:
7597 objc-keywordarg-list objc-keywordarg
7600 objc-selector : objc-keywordexpr
7605 c_parser_objc_message_args (c_parser *parser)
7607 tree sel = c_parser_objc_selector (parser);
7608 tree list = NULL_TREE;
7609 if (sel && c_parser_next_token_is_not (parser, CPP_COLON))
7614 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
7615 return error_mark_node;
7616 keywordexpr = c_parser_objc_keywordexpr (parser);
7617 list = chainon (list, build_tree_list (sel, keywordexpr));
7618 sel = c_parser_objc_selector (parser);
7619 if (!sel && c_parser_next_token_is_not (parser, CPP_COLON))
7625 /* Parse an objc-keywordexpr.
7632 c_parser_objc_keywordexpr (c_parser *parser)
7635 VEC(tree,gc) *expr_list = c_parser_expr_list (parser, true, true, NULL);
7636 if (VEC_length (tree, expr_list) == 1)
7638 /* Just return the expression, remove a level of
7640 ret = VEC_index (tree, expr_list, 0);
7644 /* We have a comma expression, we will collapse later. */
7645 ret = build_tree_list_vec (expr_list);
7647 release_tree_vector (expr_list);
7651 /* A check, needed in several places, that ObjC interface, implementation or
7652 method definitions are not prefixed by incorrect items. */
7654 c_parser_objc_diagnose_bad_element_prefix (c_parser *parser,
7655 struct c_declspecs *specs)
7657 if (!specs->declspecs_seen_p || specs->non_sc_seen_p
7658 || specs->typespec_kind != ctsk_none)
7660 c_parser_error (parser,
7661 "no type or storage class may be specified here,");
7662 c_parser_skip_to_end_of_block_or_statement (parser);
7668 /* Parse an Objective-C @property declaration. The syntax is:
7670 objc-property-declaration:
7671 '@property' objc-property-attributes[opt] struct-declaration ;
7673 objc-property-attributes:
7674 '(' objc-property-attribute-list ')'
7676 objc-property-attribute-list:
7677 objc-property-attribute
7678 objc-property-attribute-list, objc-property-attribute
7680 objc-property-attribute
7681 'getter' = identifier
7682 'setter' = identifier
7691 @property NSString *name;
7692 @property (readonly) id object;
7693 @property (retain, nonatomic, getter=getTheName) id name;
7694 @property int a, b, c;
7696 PS: This function is identical to cp_parser_objc_at_propery_declaration
7697 for C++. Keep them in sync. */
7699 c_parser_objc_at_property_declaration (c_parser *parser)
7701 /* The following variables hold the attributes of the properties as
7702 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
7703 seen. When we see an attribute, we set them to 'true' (if they
7704 are boolean properties) or to the identifier (if they have an
7705 argument, ie, for getter and setter). Note that here we only
7706 parse the list of attributes, check the syntax and accumulate the
7707 attributes that we find. objc_add_property_declaration() will
7708 then process the information. */
7709 bool property_assign = false;
7710 bool property_copy = false;
7711 tree property_getter_ident = NULL_TREE;
7712 bool property_nonatomic = false;
7713 bool property_readonly = false;
7714 bool property_readwrite = false;
7715 bool property_retain = false;
7716 tree property_setter_ident = NULL_TREE;
7718 /* 'properties' is the list of properties that we read. Usually a
7719 single one, but maybe more (eg, in "@property int a, b, c;" there
7724 loc = c_parser_peek_token (parser)->location;
7725 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_PROPERTY));
7727 c_parser_consume_token (parser); /* Eat '@property'. */
7729 /* Parse the optional attribute list... */
7730 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
7733 c_parser_consume_token (parser);
7735 /* Property attribute keywords are valid now. */
7736 parser->objc_property_attr_context = true;
7740 bool syntax_error = false;
7741 c_token *token = c_parser_peek_token (parser);
7744 if (token->type != CPP_KEYWORD)
7746 if (token->type == CPP_CLOSE_PAREN)
7747 c_parser_error (parser, "expected identifier");
7750 c_parser_consume_token (parser);
7751 c_parser_error (parser, "unknown property attribute");
7755 keyword = token->keyword;
7756 c_parser_consume_token (parser);
7759 case RID_ASSIGN: property_assign = true; break;
7760 case RID_COPY: property_copy = true; break;
7761 case RID_NONATOMIC: property_nonatomic = true; break;
7762 case RID_READONLY: property_readonly = true; break;
7763 case RID_READWRITE: property_readwrite = true; break;
7764 case RID_RETAIN: property_retain = true; break;
7768 if (c_parser_next_token_is_not (parser, CPP_EQ))
7770 c_parser_error (parser,
7771 "getter/setter attribute must be followed by %<=%>");
7772 syntax_error = true;
7775 c_parser_consume_token (parser); /* eat the = */
7776 if (c_parser_next_token_is_not (parser, CPP_NAME))
7778 c_parser_error (parser, "expected identifier");
7779 syntax_error = true;
7782 if (keyword == RID_SETTER)
7784 if (property_setter_ident != NULL_TREE)
7785 c_parser_error (parser, "the %<setter%> attribute may only be specified once");
7787 property_setter_ident = c_parser_peek_token (parser)->value;
7788 c_parser_consume_token (parser);
7789 if (c_parser_next_token_is_not (parser, CPP_COLON))
7790 c_parser_error (parser, "setter name must terminate with %<:%>");
7792 c_parser_consume_token (parser);
7796 if (property_getter_ident != NULL_TREE)
7797 c_parser_error (parser, "the %<getter%> attribute may only be specified once");
7799 property_getter_ident = c_parser_peek_token (parser)->value;
7800 c_parser_consume_token (parser);
7804 c_parser_error (parser, "unknown property attribute");
7805 syntax_error = true;
7812 if (c_parser_next_token_is (parser, CPP_COMMA))
7813 c_parser_consume_token (parser);
7817 parser->objc_property_attr_context = false;
7818 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
7820 /* ... and the property declaration(s). */
7821 properties = c_parser_struct_declaration (parser);
7823 if (properties == error_mark_node)
7825 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
7826 parser->error = false;
7830 if (properties == NULL_TREE)
7831 c_parser_error (parser, "expected identifier");
7834 /* Comma-separated properties are chained together in
7835 reverse order; add them one by one. */
7836 properties = nreverse (properties);
7838 for (; properties; properties = TREE_CHAIN (properties))
7839 objc_add_property_declaration (loc, copy_node (properties),
7840 property_readonly, property_readwrite,
7841 property_assign, property_retain,
7842 property_copy, property_nonatomic,
7843 property_getter_ident, property_setter_ident);
7846 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
7847 parser->error = false;
7850 /* Parse an Objective-C @synthesize declaration. The syntax is:
7852 objc-synthesize-declaration:
7853 @synthesize objc-synthesize-identifier-list ;
7855 objc-synthesize-identifier-list:
7856 objc-synthesize-identifier
7857 objc-synthesize-identifier-list, objc-synthesize-identifier
7859 objc-synthesize-identifier
7861 identifier = identifier
7864 @synthesize MyProperty;
7865 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
7867 PS: This function is identical to cp_parser_objc_at_synthesize_declaration
7868 for C++. Keep them in sync.
7871 c_parser_objc_at_synthesize_declaration (c_parser *parser)
7873 tree list = NULL_TREE;
7875 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_SYNTHESIZE));
7876 loc = c_parser_peek_token (parser)->location;
7878 c_parser_consume_token (parser);
7881 tree property, ivar;
7882 if (c_parser_next_token_is_not (parser, CPP_NAME))
7884 c_parser_error (parser, "expected identifier");
7885 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
7886 /* Once we find the semicolon, we can resume normal parsing.
7887 We have to reset parser->error manually because
7888 c_parser_skip_until_found() won't reset it for us if the
7889 next token is precisely a semicolon. */
7890 parser->error = false;
7893 property = c_parser_peek_token (parser)->value;
7894 c_parser_consume_token (parser);
7895 if (c_parser_next_token_is (parser, CPP_EQ))
7897 c_parser_consume_token (parser);
7898 if (c_parser_next_token_is_not (parser, CPP_NAME))
7900 c_parser_error (parser, "expected identifier");
7901 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
7902 parser->error = false;
7905 ivar = c_parser_peek_token (parser)->value;
7906 c_parser_consume_token (parser);
7910 list = chainon (list, build_tree_list (ivar, property));
7911 if (c_parser_next_token_is (parser, CPP_COMMA))
7912 c_parser_consume_token (parser);
7916 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
7917 objc_add_synthesize_declaration (loc, list);
7920 /* Parse an Objective-C @dynamic declaration. The syntax is:
7922 objc-dynamic-declaration:
7923 @dynamic identifier-list ;
7926 @dynamic MyProperty;
7927 @dynamic MyProperty, AnotherProperty;
7929 PS: This function is identical to cp_parser_objc_at_dynamic_declaration
7930 for C++. Keep them in sync.
7933 c_parser_objc_at_dynamic_declaration (c_parser *parser)
7935 tree list = NULL_TREE;
7937 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_DYNAMIC));
7938 loc = c_parser_peek_token (parser)->location;
7940 c_parser_consume_token (parser);
7944 if (c_parser_next_token_is_not (parser, CPP_NAME))
7946 c_parser_error (parser, "expected identifier");
7947 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
7948 parser->error = false;
7951 property = c_parser_peek_token (parser)->value;
7952 list = chainon (list, build_tree_list (NULL_TREE, property));
7953 c_parser_consume_token (parser);
7954 if (c_parser_next_token_is (parser, CPP_COMMA))
7955 c_parser_consume_token (parser);
7959 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
7960 objc_add_dynamic_declaration (loc, list);
7964 /* Handle pragmas. Some OpenMP pragmas are associated with, and therefore
7965 should be considered, statements. ALLOW_STMT is true if we're within
7966 the context of a function and such pragmas are to be allowed. Returns
7967 true if we actually parsed such a pragma. */
7970 c_parser_pragma (c_parser *parser, enum pragma_context context)
7974 id = c_parser_peek_token (parser)->pragma_kind;
7975 gcc_assert (id != PRAGMA_NONE);
7979 case PRAGMA_OMP_BARRIER:
7980 if (context != pragma_compound)
7982 if (context == pragma_stmt)
7983 c_parser_error (parser, "%<#pragma omp barrier%> may only be "
7984 "used in compound statements");
7987 c_parser_omp_barrier (parser);
7990 case PRAGMA_OMP_FLUSH:
7991 if (context != pragma_compound)
7993 if (context == pragma_stmt)
7994 c_parser_error (parser, "%<#pragma omp flush%> may only be "
7995 "used in compound statements");
7998 c_parser_omp_flush (parser);
8001 case PRAGMA_OMP_TASKWAIT:
8002 if (context != pragma_compound)
8004 if (context == pragma_stmt)
8005 c_parser_error (parser, "%<#pragma omp taskwait%> may only be "
8006 "used in compound statements");
8009 c_parser_omp_taskwait (parser);
8012 case PRAGMA_OMP_THREADPRIVATE:
8013 c_parser_omp_threadprivate (parser);
8016 case PRAGMA_OMP_SECTION:
8017 error_at (c_parser_peek_token (parser)->location,
8018 "%<#pragma omp section%> may only be used in "
8019 "%<#pragma omp sections%> construct");
8020 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
8023 case PRAGMA_GCC_PCH_PREPROCESS:
8024 c_parser_error (parser, "%<#pragma GCC pch_preprocess%> must be first");
8025 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
8029 if (id < PRAGMA_FIRST_EXTERNAL)
8031 if (context == pragma_external)
8034 c_parser_error (parser, "expected declaration specifiers");
8035 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
8038 c_parser_omp_construct (parser);
8044 c_parser_consume_pragma (parser);
8045 c_invoke_pragma_handler (id);
8047 /* Skip to EOL, but suppress any error message. Those will have been
8048 generated by the handler routine through calling error, as opposed
8049 to calling c_parser_error. */
8050 parser->error = true;
8051 c_parser_skip_to_pragma_eol (parser);
8056 /* The interface the pragma parsers have to the lexer. */
8059 pragma_lex (tree *value)
8061 c_token *tok = c_parser_peek_token (the_parser);
8062 enum cpp_ttype ret = tok->type;
8064 *value = tok->value;
8065 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
8069 if (ret == CPP_KEYWORD)
8071 c_parser_consume_token (the_parser);
8078 c_parser_pragma_pch_preprocess (c_parser *parser)
8082 c_parser_consume_pragma (parser);
8083 if (c_parser_next_token_is (parser, CPP_STRING))
8085 name = c_parser_peek_token (parser)->value;
8086 c_parser_consume_token (parser);
8089 c_parser_error (parser, "expected string literal");
8090 c_parser_skip_to_pragma_eol (parser);
8093 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
8096 /* OpenMP 2.5 parsing routines. */
8098 /* Returns name of the next clause.
8099 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
8100 the token is not consumed. Otherwise appropriate pragma_omp_clause is
8101 returned and the token is consumed. */
8103 static pragma_omp_clause
8104 c_parser_omp_clause_name (c_parser *parser)
8106 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
8108 if (c_parser_next_token_is_keyword (parser, RID_IF))
8109 result = PRAGMA_OMP_CLAUSE_IF;
8110 else if (c_parser_next_token_is_keyword (parser, RID_DEFAULT))
8111 result = PRAGMA_OMP_CLAUSE_DEFAULT;
8112 else if (c_parser_next_token_is (parser, CPP_NAME))
8114 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
8119 if (!strcmp ("collapse", p))
8120 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
8121 else if (!strcmp ("copyin", p))
8122 result = PRAGMA_OMP_CLAUSE_COPYIN;
8123 else if (!strcmp ("copyprivate", p))
8124 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
8127 if (!strcmp ("firstprivate", p))
8128 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
8131 if (!strcmp ("lastprivate", p))
8132 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
8135 if (!strcmp ("nowait", p))
8136 result = PRAGMA_OMP_CLAUSE_NOWAIT;
8137 else if (!strcmp ("num_threads", p))
8138 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
8141 if (!strcmp ("ordered", p))
8142 result = PRAGMA_OMP_CLAUSE_ORDERED;
8145 if (!strcmp ("private", p))
8146 result = PRAGMA_OMP_CLAUSE_PRIVATE;
8149 if (!strcmp ("reduction", p))
8150 result = PRAGMA_OMP_CLAUSE_REDUCTION;
8153 if (!strcmp ("schedule", p))
8154 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
8155 else if (!strcmp ("shared", p))
8156 result = PRAGMA_OMP_CLAUSE_SHARED;
8159 if (!strcmp ("untied", p))
8160 result = PRAGMA_OMP_CLAUSE_UNTIED;
8165 if (result != PRAGMA_OMP_CLAUSE_NONE)
8166 c_parser_consume_token (parser);
8171 /* Validate that a clause of the given type does not already exist. */
8174 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
8179 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
8180 if (OMP_CLAUSE_CODE (c) == code)
8182 location_t loc = OMP_CLAUSE_LOCATION (c);
8183 error_at (loc, "too many %qs clauses", name);
8191 variable-list , identifier
8193 If KIND is nonzero, create the appropriate node and install the
8194 decl in OMP_CLAUSE_DECL and add the node to the head of the list.
8195 If KIND is nonzero, CLAUSE_LOC is the location of the clause.
8197 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
8198 return the list created. */
8201 c_parser_omp_variable_list (c_parser *parser,
8202 location_t clause_loc,
8203 enum omp_clause_code kind,
8206 if (c_parser_next_token_is_not (parser, CPP_NAME)
8207 || c_parser_peek_token (parser)->id_kind != C_ID_ID)
8208 c_parser_error (parser, "expected identifier");
8210 while (c_parser_next_token_is (parser, CPP_NAME)
8211 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
8213 tree t = lookup_name (c_parser_peek_token (parser)->value);
8216 undeclared_variable (c_parser_peek_token (parser)->location,
8217 c_parser_peek_token (parser)->value);
8218 else if (t == error_mark_node)
8222 tree u = build_omp_clause (clause_loc, kind);
8223 OMP_CLAUSE_DECL (u) = t;
8224 OMP_CLAUSE_CHAIN (u) = list;
8228 list = tree_cons (t, NULL_TREE, list);
8230 c_parser_consume_token (parser);
8232 if (c_parser_next_token_is_not (parser, CPP_COMMA))
8235 c_parser_consume_token (parser);
8241 /* Similarly, but expect leading and trailing parenthesis. This is a very
8242 common case for omp clauses. */
8245 c_parser_omp_var_list_parens (c_parser *parser, enum omp_clause_code kind,
8248 /* The clauses location. */
8249 location_t loc = c_parser_peek_token (parser)->location;
8251 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
8253 list = c_parser_omp_variable_list (parser, loc, kind, list);
8254 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
8260 collapse ( constant-expression ) */
8263 c_parser_omp_clause_collapse (c_parser *parser, tree list)
8265 tree c, num = error_mark_node;
8269 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse");
8271 loc = c_parser_peek_token (parser)->location;
8272 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
8274 num = c_parser_expr_no_commas (parser, NULL).value;
8275 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
8277 if (num == error_mark_node)
8279 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
8280 || !host_integerp (num, 0)
8281 || (n = tree_low_cst (num, 0)) <= 0
8285 "collapse argument needs positive constant integer expression");
8288 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
8289 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
8290 OMP_CLAUSE_CHAIN (c) = list;
8295 copyin ( variable-list ) */
8298 c_parser_omp_clause_copyin (c_parser *parser, tree list)
8300 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_COPYIN, list);
8304 copyprivate ( variable-list ) */
8307 c_parser_omp_clause_copyprivate (c_parser *parser, tree list)
8309 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_COPYPRIVATE, list);
8313 default ( shared | none ) */
8316 c_parser_omp_clause_default (c_parser *parser, tree list)
8318 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
8319 location_t loc = c_parser_peek_token (parser)->location;
8322 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
8324 if (c_parser_next_token_is (parser, CPP_NAME))
8326 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
8331 if (strcmp ("none", p) != 0)
8333 kind = OMP_CLAUSE_DEFAULT_NONE;
8337 if (strcmp ("shared", p) != 0)
8339 kind = OMP_CLAUSE_DEFAULT_SHARED;
8346 c_parser_consume_token (parser);
8351 c_parser_error (parser, "expected %<none%> or %<shared%>");
8353 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
8355 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
8358 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default");
8359 c = build_omp_clause (loc, OMP_CLAUSE_DEFAULT);
8360 OMP_CLAUSE_CHAIN (c) = list;
8361 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
8367 firstprivate ( variable-list ) */
8370 c_parser_omp_clause_firstprivate (c_parser *parser, tree list)
8372 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_FIRSTPRIVATE, list);
8376 if ( expression ) */
8379 c_parser_omp_clause_if (c_parser *parser, tree list)
8381 location_t loc = c_parser_peek_token (parser)->location;
8382 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
8384 tree t = c_parser_paren_condition (parser);
8387 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if");
8389 c = build_omp_clause (loc, OMP_CLAUSE_IF);
8390 OMP_CLAUSE_IF_EXPR (c) = t;
8391 OMP_CLAUSE_CHAIN (c) = list;
8395 c_parser_error (parser, "expected %<(%>");
8401 lastprivate ( variable-list ) */
8404 c_parser_omp_clause_lastprivate (c_parser *parser, tree list)
8406 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_LASTPRIVATE, list);
8413 c_parser_omp_clause_nowait (c_parser *parser ATTRIBUTE_UNUSED, tree list)
8416 location_t loc = c_parser_peek_token (parser)->location;
8418 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait");
8420 c = build_omp_clause (loc, OMP_CLAUSE_NOWAIT);
8421 OMP_CLAUSE_CHAIN (c) = list;
8426 num_threads ( expression ) */
8429 c_parser_omp_clause_num_threads (c_parser *parser, tree list)
8431 location_t num_threads_loc = c_parser_peek_token (parser)->location;
8432 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
8434 location_t expr_loc = c_parser_peek_token (parser)->location;
8435 tree c, t = c_parser_expression (parser).value;
8436 t = c_fully_fold (t, false, NULL);
8438 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
8440 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
8442 c_parser_error (parser, "expected integer expression");
8446 /* Attempt to statically determine when the number isn't positive. */
8447 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
8448 build_int_cst (TREE_TYPE (t), 0));
8449 if (CAN_HAVE_LOCATION_P (c))
8450 SET_EXPR_LOCATION (c, expr_loc);
8451 if (c == boolean_true_node)
8453 warning_at (expr_loc, 0,
8454 "%<num_threads%> value must be positive");
8455 t = integer_one_node;
8458 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS, "num_threads");
8460 c = build_omp_clause (num_threads_loc, OMP_CLAUSE_NUM_THREADS);
8461 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
8462 OMP_CLAUSE_CHAIN (c) = list;
8473 c_parser_omp_clause_ordered (c_parser *parser, tree list)
8477 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED, "ordered");
8479 c = build_omp_clause (c_parser_peek_token (parser)->location,
8480 OMP_CLAUSE_ORDERED);
8481 OMP_CLAUSE_CHAIN (c) = list;
8487 private ( variable-list ) */
8490 c_parser_omp_clause_private (c_parser *parser, tree list)
8492 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_PRIVATE, list);
8496 reduction ( reduction-operator : variable-list )
8499 One of: + * - & ^ | && || */
8502 c_parser_omp_clause_reduction (c_parser *parser, tree list)
8504 location_t clause_loc = c_parser_peek_token (parser)->location;
8505 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
8507 enum tree_code code;
8509 switch (c_parser_peek_token (parser)->type)
8521 code = BIT_AND_EXPR;
8524 code = BIT_XOR_EXPR;
8527 code = BIT_IOR_EXPR;
8530 code = TRUTH_ANDIF_EXPR;
8533 code = TRUTH_ORIF_EXPR;
8536 c_parser_error (parser,
8537 "expected %<+%>, %<*%>, %<-%>, %<&%>, "
8538 "%<^%>, %<|%>, %<&&%>, or %<||%>");
8539 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 0);
8542 c_parser_consume_token (parser);
8543 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
8547 nl = c_parser_omp_variable_list (parser, clause_loc,
8548 OMP_CLAUSE_REDUCTION, list);
8549 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
8550 OMP_CLAUSE_REDUCTION_CODE (c) = code;
8554 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
8560 schedule ( schedule-kind )
8561 schedule ( schedule-kind , expression )
8564 static | dynamic | guided | runtime | auto
8568 c_parser_omp_clause_schedule (c_parser *parser, tree list)
8571 location_t loc = c_parser_peek_token (parser)->location;
8573 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
8576 c = build_omp_clause (loc, OMP_CLAUSE_SCHEDULE);
8578 if (c_parser_next_token_is (parser, CPP_NAME))
8580 tree kind = c_parser_peek_token (parser)->value;
8581 const char *p = IDENTIFIER_POINTER (kind);
8586 if (strcmp ("dynamic", p) != 0)
8588 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
8592 if (strcmp ("guided", p) != 0)
8594 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
8598 if (strcmp ("runtime", p) != 0)
8600 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
8607 else if (c_parser_next_token_is_keyword (parser, RID_STATIC))
8608 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
8609 else if (c_parser_next_token_is_keyword (parser, RID_AUTO))
8610 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
8614 c_parser_consume_token (parser);
8615 if (c_parser_next_token_is (parser, CPP_COMMA))
8618 c_parser_consume_token (parser);
8620 here = c_parser_peek_token (parser)->location;
8621 t = c_parser_expr_no_commas (parser, NULL).value;
8622 t = c_fully_fold (t, false, NULL);
8624 if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
8625 error_at (here, "schedule %<runtime%> does not take "
8626 "a %<chunk_size%> parameter");
8627 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
8629 "schedule %<auto%> does not take "
8630 "a %<chunk_size%> parameter");
8631 else if (TREE_CODE (TREE_TYPE (t)) == INTEGER_TYPE)
8632 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
8634 c_parser_error (parser, "expected integer expression");
8636 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
8639 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
8640 "expected %<,%> or %<)%>");
8642 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule");
8643 OMP_CLAUSE_CHAIN (c) = list;
8647 c_parser_error (parser, "invalid schedule kind");
8648 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 0);
8653 shared ( variable-list ) */
8656 c_parser_omp_clause_shared (c_parser *parser, tree list)
8658 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_SHARED, list);
8665 c_parser_omp_clause_untied (c_parser *parser ATTRIBUTE_UNUSED, tree list)
8669 /* FIXME: Should we allow duplicates? */
8670 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied");
8672 c = build_omp_clause (c_parser_peek_token (parser)->location,
8674 OMP_CLAUSE_CHAIN (c) = list;
8679 /* Parse all OpenMP clauses. The set clauses allowed by the directive
8680 is a bitmask in MASK. Return the list of clauses found; the result
8681 of clause default goes in *pdefault. */
8684 c_parser_omp_all_clauses (c_parser *parser, unsigned int mask,
8687 tree clauses = NULL;
8690 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
8693 pragma_omp_clause c_kind;
8695 tree prev = clauses;
8697 if (!first && c_parser_next_token_is (parser, CPP_COMMA))
8698 c_parser_consume_token (parser);
8701 here = c_parser_peek_token (parser)->location;
8702 c_kind = c_parser_omp_clause_name (parser);
8706 case PRAGMA_OMP_CLAUSE_COLLAPSE:
8707 clauses = c_parser_omp_clause_collapse (parser, clauses);
8708 c_name = "collapse";
8710 case PRAGMA_OMP_CLAUSE_COPYIN:
8711 clauses = c_parser_omp_clause_copyin (parser, clauses);
8714 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
8715 clauses = c_parser_omp_clause_copyprivate (parser, clauses);
8716 c_name = "copyprivate";
8718 case PRAGMA_OMP_CLAUSE_DEFAULT:
8719 clauses = c_parser_omp_clause_default (parser, clauses);
8722 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
8723 clauses = c_parser_omp_clause_firstprivate (parser, clauses);
8724 c_name = "firstprivate";
8726 case PRAGMA_OMP_CLAUSE_IF:
8727 clauses = c_parser_omp_clause_if (parser, clauses);
8730 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
8731 clauses = c_parser_omp_clause_lastprivate (parser, clauses);
8732 c_name = "lastprivate";
8734 case PRAGMA_OMP_CLAUSE_NOWAIT:
8735 clauses = c_parser_omp_clause_nowait (parser, clauses);
8738 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
8739 clauses = c_parser_omp_clause_num_threads (parser, clauses);
8740 c_name = "num_threads";
8742 case PRAGMA_OMP_CLAUSE_ORDERED:
8743 clauses = c_parser_omp_clause_ordered (parser, clauses);
8746 case PRAGMA_OMP_CLAUSE_PRIVATE:
8747 clauses = c_parser_omp_clause_private (parser, clauses);
8750 case PRAGMA_OMP_CLAUSE_REDUCTION:
8751 clauses = c_parser_omp_clause_reduction (parser, clauses);
8752 c_name = "reduction";
8754 case PRAGMA_OMP_CLAUSE_SCHEDULE:
8755 clauses = c_parser_omp_clause_schedule (parser, clauses);
8756 c_name = "schedule";
8758 case PRAGMA_OMP_CLAUSE_SHARED:
8759 clauses = c_parser_omp_clause_shared (parser, clauses);
8762 case PRAGMA_OMP_CLAUSE_UNTIED:
8763 clauses = c_parser_omp_clause_untied (parser, clauses);
8767 c_parser_error (parser, "expected %<#pragma omp%> clause");
8771 if (((mask >> c_kind) & 1) == 0 && !parser->error)
8773 /* Remove the invalid clause(s) from the list to avoid
8774 confusing the rest of the compiler. */
8776 error_at (here, "%qs is not valid for %qs", c_name, where);
8781 c_parser_skip_to_pragma_eol (parser);
8783 return c_finish_omp_clauses (clauses);
8790 In practice, we're also interested in adding the statement to an
8791 outer node. So it is convenient if we work around the fact that
8792 c_parser_statement calls add_stmt. */
8795 c_parser_omp_structured_block (c_parser *parser)
8797 tree stmt = push_stmt_list ();
8798 c_parser_statement (parser);
8799 return pop_stmt_list (stmt);
8803 # pragma omp atomic new-line
8807 x binop= expr | x++ | ++x | x-- | --x
8809 +, *, -, /, &, ^, |, <<, >>
8811 where x is an lvalue expression with scalar type.
8813 LOC is the location of the #pragma token. */
8816 c_parser_omp_atomic (location_t loc, c_parser *parser)
8820 enum tree_code code;
8821 struct c_expr rhs_expr;
8823 c_parser_skip_to_pragma_eol (parser);
8825 lhs = c_parser_unary_expression (parser).value;
8826 lhs = c_fully_fold (lhs, false, NULL);
8827 switch (TREE_CODE (lhs))
8831 c_parser_skip_to_end_of_block_or_statement (parser);
8834 case PREINCREMENT_EXPR:
8835 case POSTINCREMENT_EXPR:
8836 lhs = TREE_OPERAND (lhs, 0);
8838 rhs = integer_one_node;
8841 case PREDECREMENT_EXPR:
8842 case POSTDECREMENT_EXPR:
8843 lhs = TREE_OPERAND (lhs, 0);
8845 rhs = integer_one_node;
8849 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
8850 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
8851 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
8852 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
8853 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
8854 (TREE_OPERAND (lhs, 1), 0), 0)))
8856 /* Undo effects of boolean_increment for post {in,de}crement. */
8857 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
8860 if (TREE_CODE (lhs) == MODIFY_EXPR
8861 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
8863 /* Undo effects of boolean_increment. */
8864 if (integer_onep (TREE_OPERAND (lhs, 1)))
8866 /* This is pre or post increment. */
8867 rhs = TREE_OPERAND (lhs, 1);
8868 lhs = TREE_OPERAND (lhs, 0);
8872 if (TREE_CODE (TREE_OPERAND (lhs, 1)) == TRUTH_NOT_EXPR
8873 && TREE_OPERAND (lhs, 0)
8874 == TREE_OPERAND (TREE_OPERAND (lhs, 1), 0))
8876 /* This is pre or post decrement. */
8877 rhs = TREE_OPERAND (lhs, 1);
8878 lhs = TREE_OPERAND (lhs, 0);
8885 switch (c_parser_peek_token (parser)->type)
8891 code = TRUNC_DIV_EXPR;
8906 code = BIT_AND_EXPR;
8909 code = BIT_IOR_EXPR;
8912 code = BIT_XOR_EXPR;
8915 c_parser_error (parser,
8916 "invalid operator for %<#pragma omp atomic%>");
8920 c_parser_consume_token (parser);
8922 location_t rhs_loc = c_parser_peek_token (parser)->location;
8923 rhs_expr = c_parser_expression (parser);
8924 rhs_expr = default_function_array_read_conversion (rhs_loc, rhs_expr);
8926 rhs = rhs_expr.value;
8927 rhs = c_fully_fold (rhs, false, NULL);
8930 stmt = c_finish_omp_atomic (loc, code, lhs, rhs);
8931 if (stmt != error_mark_node)
8933 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
8938 # pragma omp barrier new-line
8942 c_parser_omp_barrier (c_parser *parser)
8944 location_t loc = c_parser_peek_token (parser)->location;
8945 c_parser_consume_pragma (parser);
8946 c_parser_skip_to_pragma_eol (parser);
8948 c_finish_omp_barrier (loc);
8952 # pragma omp critical [(name)] new-line
8955 LOC is the location of the #pragma itself. */
8958 c_parser_omp_critical (location_t loc, c_parser *parser)
8960 tree stmt, name = NULL;
8962 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
8964 c_parser_consume_token (parser);
8965 if (c_parser_next_token_is (parser, CPP_NAME))
8967 name = c_parser_peek_token (parser)->value;
8968 c_parser_consume_token (parser);
8969 c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
8972 c_parser_error (parser, "expected identifier");
8974 else if (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
8975 c_parser_error (parser, "expected %<(%> or end of line");
8976 c_parser_skip_to_pragma_eol (parser);
8978 stmt = c_parser_omp_structured_block (parser);
8979 return c_finish_omp_critical (loc, stmt, name);
8983 # pragma omp flush flush-vars[opt] new-line
8986 ( variable-list ) */
8989 c_parser_omp_flush (c_parser *parser)
8991 location_t loc = c_parser_peek_token (parser)->location;
8992 c_parser_consume_pragma (parser);
8993 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
8994 c_parser_omp_var_list_parens (parser, OMP_CLAUSE_ERROR, NULL);
8995 else if (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
8996 c_parser_error (parser, "expected %<(%> or end of line");
8997 c_parser_skip_to_pragma_eol (parser);
8999 c_finish_omp_flush (loc);
9002 /* Parse the restricted form of the for statement allowed by OpenMP.
9003 The real trick here is to determine the loop control variable early
9004 so that we can push a new decl if necessary to make it private.
9005 LOC is the location of the OMP in "#pragma omp". */
9008 c_parser_omp_for_loop (location_t loc,
9009 c_parser *parser, tree clauses, tree *par_clauses)
9011 tree decl, cond, incr, save_break, save_cont, body, init, stmt, cl;
9012 tree declv, condv, incrv, initv, ret = NULL;
9013 bool fail = false, open_brace_parsed = false;
9014 int i, collapse = 1, nbraces = 0;
9016 VEC(tree,gc) *for_block = make_tree_vector ();
9018 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
9019 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
9020 collapse = tree_low_cst (OMP_CLAUSE_COLLAPSE_EXPR (cl), 0);
9022 gcc_assert (collapse >= 1);
9024 declv = make_tree_vec (collapse);
9025 initv = make_tree_vec (collapse);
9026 condv = make_tree_vec (collapse);
9027 incrv = make_tree_vec (collapse);
9029 if (!c_parser_next_token_is_keyword (parser, RID_FOR))
9031 c_parser_error (parser, "for statement expected");
9034 for_loc = c_parser_peek_token (parser)->location;
9035 c_parser_consume_token (parser);
9037 for (i = 0; i < collapse; i++)
9041 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
9044 /* Parse the initialization declaration or expression. */
9045 if (c_parser_next_tokens_start_declaration (parser))
9048 VEC_safe_push (tree, gc, for_block, c_begin_compound_stmt (true));
9049 c_parser_declaration_or_fndef (parser, true, true, true, true, true, NULL);
9050 decl = check_for_loop_decls (for_loc, flag_isoc99);
9053 if (DECL_INITIAL (decl) == error_mark_node)
9054 decl = error_mark_node;
9057 else if (c_parser_next_token_is (parser, CPP_NAME)
9058 && c_parser_peek_2nd_token (parser)->type == CPP_EQ)
9060 struct c_expr decl_exp;
9061 struct c_expr init_exp;
9062 location_t init_loc;
9064 decl_exp = c_parser_postfix_expression (parser);
9065 decl = decl_exp.value;
9067 c_parser_require (parser, CPP_EQ, "expected %<=%>");
9069 init_loc = c_parser_peek_token (parser)->location;
9070 init_exp = c_parser_expr_no_commas (parser, NULL);
9071 init_exp = default_function_array_read_conversion (init_loc,
9073 init = build_modify_expr (init_loc, decl, decl_exp.original_type,
9074 NOP_EXPR, init_loc, init_exp.value,
9075 init_exp.original_type);
9076 init = c_process_expr_stmt (init_loc, init);
9078 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
9083 c_parser_error (parser,
9084 "expected iteration declaration or initialization");
9085 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
9091 /* Parse the loop condition. */
9093 if (c_parser_next_token_is_not (parser, CPP_SEMICOLON))
9095 location_t cond_loc = c_parser_peek_token (parser)->location;
9096 struct c_expr cond_expr = c_parser_binary_expression (parser, NULL);
9098 cond = cond_expr.value;
9099 cond = c_objc_common_truthvalue_conversion (cond_loc, cond);
9100 cond = c_fully_fold (cond, false, NULL);
9101 switch (cond_expr.original_code)
9109 /* Can't be cond = error_mark_node, because we want to preserve
9110 the location until c_finish_omp_for. */
9111 cond = build1 (NOP_EXPR, boolean_type_node, error_mark_node);
9114 protected_set_expr_location (cond, cond_loc);
9116 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
9118 /* Parse the increment expression. */
9120 if (c_parser_next_token_is_not (parser, CPP_CLOSE_PAREN))
9122 location_t incr_loc = c_parser_peek_token (parser)->location;
9124 incr = c_process_expr_stmt (incr_loc,
9125 c_parser_expression (parser).value);
9127 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
9129 if (decl == NULL || decl == error_mark_node || init == error_mark_node)
9133 TREE_VEC_ELT (declv, i) = decl;
9134 TREE_VEC_ELT (initv, i) = init;
9135 TREE_VEC_ELT (condv, i) = cond;
9136 TREE_VEC_ELT (incrv, i) = incr;
9140 if (i == collapse - 1)
9143 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
9144 in between the collapsed for loops to be still considered perfectly
9145 nested. Hopefully the final version clarifies this.
9146 For now handle (multiple) {'s and empty statements. */
9149 if (c_parser_next_token_is_keyword (parser, RID_FOR))
9151 c_parser_consume_token (parser);
9154 else if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
9156 c_parser_consume_token (parser);
9160 && c_parser_next_token_is (parser, CPP_SEMICOLON))
9161 c_parser_consume_token (parser);
9164 c_parser_error (parser, "not enough perfectly nested loops");
9167 open_brace_parsed = true;
9177 nbraces += bracecount;
9180 save_break = c_break_label;
9181 c_break_label = size_one_node;
9182 save_cont = c_cont_label;
9183 c_cont_label = NULL_TREE;
9184 body = push_stmt_list ();
9186 if (open_brace_parsed)
9188 location_t here = c_parser_peek_token (parser)->location;
9189 stmt = c_begin_compound_stmt (true);
9190 c_parser_compound_statement_nostart (parser);
9191 add_stmt (c_end_compound_stmt (here, stmt, true));
9194 add_stmt (c_parser_c99_block_statement (parser));
9197 tree t = build1 (LABEL_EXPR, void_type_node, c_cont_label);
9198 SET_EXPR_LOCATION (t, loc);
9202 body = pop_stmt_list (body);
9203 c_break_label = save_break;
9204 c_cont_label = save_cont;
9208 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
9210 c_parser_consume_token (parser);
9213 else if (c_parser_next_token_is (parser, CPP_SEMICOLON))
9214 c_parser_consume_token (parser);
9217 c_parser_error (parser, "collapsed loops not perfectly nested");
9220 location_t here = c_parser_peek_token (parser)->location;
9221 stmt = c_begin_compound_stmt (true);
9223 c_parser_compound_statement_nostart (parser);
9224 body = c_end_compound_stmt (here, stmt, true);
9231 /* Only bother calling c_finish_omp_for if we haven't already generated
9232 an error from the initialization parsing. */
9235 stmt = c_finish_omp_for (loc, declv, initv, condv, incrv, body, NULL);
9238 if (par_clauses != NULL)
9241 for (c = par_clauses; *c ; )
9242 if (OMP_CLAUSE_CODE (*c) != OMP_CLAUSE_FIRSTPRIVATE
9243 && OMP_CLAUSE_CODE (*c) != OMP_CLAUSE_LASTPRIVATE)
9244 c = &OMP_CLAUSE_CHAIN (*c);
9247 for (i = 0; i < collapse; i++)
9248 if (TREE_VEC_ELT (declv, i) == OMP_CLAUSE_DECL (*c))
9251 c = &OMP_CLAUSE_CHAIN (*c);
9252 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE)
9255 "iteration variable %qD should not be firstprivate",
9256 OMP_CLAUSE_DECL (*c));
9257 *c = OMP_CLAUSE_CHAIN (*c);
9261 /* Copy lastprivate (decl) clause to OMP_FOR_CLAUSES,
9262 change it to shared (decl) in
9263 OMP_PARALLEL_CLAUSES. */
9264 tree l = build_omp_clause (OMP_CLAUSE_LOCATION (*c),
9265 OMP_CLAUSE_LASTPRIVATE);
9266 OMP_CLAUSE_DECL (l) = OMP_CLAUSE_DECL (*c);
9267 OMP_CLAUSE_CHAIN (l) = clauses;
9269 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
9273 OMP_FOR_CLAUSES (stmt) = clauses;
9278 while (!VEC_empty (tree, for_block))
9280 /* FIXME diagnostics: LOC below should be the actual location of
9281 this particular for block. We need to build a list of
9282 locations to go along with FOR_BLOCK. */
9283 stmt = c_end_compound_stmt (loc, VEC_pop (tree, for_block), true);
9286 release_tree_vector (for_block);
9291 #pragma omp for for-clause[optseq] new-line
9294 LOC is the location of the #pragma token.
9297 #define OMP_FOR_CLAUSE_MASK \
9298 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
9299 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
9300 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
9301 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
9302 | (1u << PRAGMA_OMP_CLAUSE_ORDERED) \
9303 | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE) \
9304 | (1u << PRAGMA_OMP_CLAUSE_COLLAPSE) \
9305 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
9308 c_parser_omp_for (location_t loc, c_parser *parser)
9310 tree block, clauses, ret;
9312 clauses = c_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
9315 block = c_begin_compound_stmt (true);
9316 ret = c_parser_omp_for_loop (loc, parser, clauses, NULL);
9317 block = c_end_compound_stmt (loc, block, true);
9324 # pragma omp master new-line
9327 LOC is the location of the #pragma token.
9331 c_parser_omp_master (location_t loc, c_parser *parser)
9333 c_parser_skip_to_pragma_eol (parser);
9334 return c_finish_omp_master (loc, c_parser_omp_structured_block (parser));
9338 # pragma omp ordered new-line
9341 LOC is the location of the #pragma itself.
9345 c_parser_omp_ordered (location_t loc, c_parser *parser)
9347 c_parser_skip_to_pragma_eol (parser);
9348 return c_finish_omp_ordered (loc, c_parser_omp_structured_block (parser));
9354 { section-sequence }
9357 section-directive[opt] structured-block
9358 section-sequence section-directive structured-block
9360 SECTIONS_LOC is the location of the #pragma omp sections. */
9363 c_parser_omp_sections_scope (location_t sections_loc, c_parser *parser)
9366 bool error_suppress = false;
9369 loc = c_parser_peek_token (parser)->location;
9370 if (!c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
9372 /* Avoid skipping until the end of the block. */
9373 parser->error = false;
9377 stmt = push_stmt_list ();
9379 if (c_parser_peek_token (parser)->pragma_kind != PRAGMA_OMP_SECTION)
9381 substmt = push_stmt_list ();
9385 c_parser_statement (parser);
9387 if (c_parser_peek_token (parser)->pragma_kind == PRAGMA_OMP_SECTION)
9389 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
9391 if (c_parser_next_token_is (parser, CPP_EOF))
9395 substmt = pop_stmt_list (substmt);
9396 substmt = build1 (OMP_SECTION, void_type_node, substmt);
9397 SET_EXPR_LOCATION (substmt, loc);
9403 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
9405 if (c_parser_next_token_is (parser, CPP_EOF))
9408 loc = c_parser_peek_token (parser)->location;
9409 if (c_parser_peek_token (parser)->pragma_kind == PRAGMA_OMP_SECTION)
9411 c_parser_consume_pragma (parser);
9412 c_parser_skip_to_pragma_eol (parser);
9413 error_suppress = false;
9415 else if (!error_suppress)
9417 error_at (loc, "expected %<#pragma omp section%> or %<}%>");
9418 error_suppress = true;
9421 substmt = c_parser_omp_structured_block (parser);
9422 substmt = build1 (OMP_SECTION, void_type_node, substmt);
9423 SET_EXPR_LOCATION (substmt, loc);
9426 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE,
9427 "expected %<#pragma omp section%> or %<}%>");
9429 substmt = pop_stmt_list (stmt);
9431 stmt = make_node (OMP_SECTIONS);
9432 SET_EXPR_LOCATION (stmt, sections_loc);
9433 TREE_TYPE (stmt) = void_type_node;
9434 OMP_SECTIONS_BODY (stmt) = substmt;
9436 return add_stmt (stmt);
9440 # pragma omp sections sections-clause[optseq] newline
9443 LOC is the location of the #pragma token.
9446 #define OMP_SECTIONS_CLAUSE_MASK \
9447 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
9448 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
9449 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
9450 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
9451 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
9454 c_parser_omp_sections (location_t loc, c_parser *parser)
9456 tree block, clauses, ret;
9458 clauses = c_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
9459 "#pragma omp sections");
9461 block = c_begin_compound_stmt (true);
9462 ret = c_parser_omp_sections_scope (loc, parser);
9464 OMP_SECTIONS_CLAUSES (ret) = clauses;
9465 block = c_end_compound_stmt (loc, block, true);
9472 # pragma parallel parallel-clause new-line
9473 # pragma parallel for parallel-for-clause new-line
9474 # pragma parallel sections parallel-sections-clause new-line
9476 LOC is the location of the #pragma token.
9479 #define OMP_PARALLEL_CLAUSE_MASK \
9480 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
9481 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
9482 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
9483 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
9484 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
9485 | (1u << PRAGMA_OMP_CLAUSE_COPYIN) \
9486 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
9487 | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
9490 c_parser_omp_parallel (location_t loc, c_parser *parser)
9492 enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
9493 const char *p_name = "#pragma omp parallel";
9494 tree stmt, clauses, par_clause, ws_clause, block;
9495 unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
9497 if (c_parser_next_token_is_keyword (parser, RID_FOR))
9499 c_parser_consume_token (parser);
9500 p_kind = PRAGMA_OMP_PARALLEL_FOR;
9501 p_name = "#pragma omp parallel for";
9502 mask |= OMP_FOR_CLAUSE_MASK;
9503 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
9505 else if (c_parser_next_token_is (parser, CPP_NAME))
9507 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
9508 if (strcmp (p, "sections") == 0)
9510 c_parser_consume_token (parser);
9511 p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
9512 p_name = "#pragma omp parallel sections";
9513 mask |= OMP_SECTIONS_CLAUSE_MASK;
9514 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
9518 clauses = c_parser_omp_all_clauses (parser, mask, p_name);
9522 case PRAGMA_OMP_PARALLEL:
9523 block = c_begin_omp_parallel ();
9524 c_parser_statement (parser);
9525 stmt = c_finish_omp_parallel (loc, clauses, block);
9528 case PRAGMA_OMP_PARALLEL_FOR:
9529 block = c_begin_omp_parallel ();
9530 c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
9531 c_parser_omp_for_loop (loc, parser, ws_clause, &par_clause);
9532 stmt = c_finish_omp_parallel (loc, par_clause, block);
9533 OMP_PARALLEL_COMBINED (stmt) = 1;
9536 case PRAGMA_OMP_PARALLEL_SECTIONS:
9537 block = c_begin_omp_parallel ();
9538 c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
9539 stmt = c_parser_omp_sections_scope (loc, parser);
9541 OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
9542 stmt = c_finish_omp_parallel (loc, par_clause, block);
9543 OMP_PARALLEL_COMBINED (stmt) = 1;
9554 # pragma omp single single-clause[optseq] new-line
9557 LOC is the location of the #pragma.
9560 #define OMP_SINGLE_CLAUSE_MASK \
9561 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
9562 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
9563 | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
9564 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
9567 c_parser_omp_single (location_t loc, c_parser *parser)
9569 tree stmt = make_node (OMP_SINGLE);
9570 SET_EXPR_LOCATION (stmt, loc);
9571 TREE_TYPE (stmt) = void_type_node;
9573 OMP_SINGLE_CLAUSES (stmt)
9574 = c_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
9575 "#pragma omp single");
9576 OMP_SINGLE_BODY (stmt) = c_parser_omp_structured_block (parser);
9578 return add_stmt (stmt);
9582 # pragma omp task task-clause[optseq] new-line
9584 LOC is the location of the #pragma.
9587 #define OMP_TASK_CLAUSE_MASK \
9588 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
9589 | (1u << PRAGMA_OMP_CLAUSE_UNTIED) \
9590 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
9591 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
9592 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
9593 | (1u << PRAGMA_OMP_CLAUSE_SHARED))
9596 c_parser_omp_task (location_t loc, c_parser *parser)
9598 tree clauses, block;
9600 clauses = c_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
9601 "#pragma omp task");
9603 block = c_begin_omp_task ();
9604 c_parser_statement (parser);
9605 return c_finish_omp_task (loc, clauses, block);
9609 # pragma omp taskwait new-line
9613 c_parser_omp_taskwait (c_parser *parser)
9615 location_t loc = c_parser_peek_token (parser)->location;
9616 c_parser_consume_pragma (parser);
9617 c_parser_skip_to_pragma_eol (parser);
9619 c_finish_omp_taskwait (loc);
9622 /* Main entry point to parsing most OpenMP pragmas. */
9625 c_parser_omp_construct (c_parser *parser)
9627 enum pragma_kind p_kind;
9631 loc = c_parser_peek_token (parser)->location;
9632 p_kind = c_parser_peek_token (parser)->pragma_kind;
9633 c_parser_consume_pragma (parser);
9637 case PRAGMA_OMP_ATOMIC:
9638 c_parser_omp_atomic (loc, parser);
9640 case PRAGMA_OMP_CRITICAL:
9641 stmt = c_parser_omp_critical (loc, parser);
9643 case PRAGMA_OMP_FOR:
9644 stmt = c_parser_omp_for (loc, parser);
9646 case PRAGMA_OMP_MASTER:
9647 stmt = c_parser_omp_master (loc, parser);
9649 case PRAGMA_OMP_ORDERED:
9650 stmt = c_parser_omp_ordered (loc, parser);
9652 case PRAGMA_OMP_PARALLEL:
9653 stmt = c_parser_omp_parallel (loc, parser);
9655 case PRAGMA_OMP_SECTIONS:
9656 stmt = c_parser_omp_sections (loc, parser);
9658 case PRAGMA_OMP_SINGLE:
9659 stmt = c_parser_omp_single (loc, parser);
9661 case PRAGMA_OMP_TASK:
9662 stmt = c_parser_omp_task (loc, parser);
9669 gcc_assert (EXPR_LOCATION (stmt) != UNKNOWN_LOCATION);
9674 # pragma omp threadprivate (variable-list) */
9677 c_parser_omp_threadprivate (c_parser *parser)
9682 c_parser_consume_pragma (parser);
9683 loc = c_parser_peek_token (parser)->location;
9684 vars = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_ERROR, NULL);
9686 /* Mark every variable in VARS to be assigned thread local storage. */
9687 for (t = vars; t; t = TREE_CHAIN (t))
9689 tree v = TREE_PURPOSE (t);
9691 /* FIXME diagnostics: Ideally we should keep individual
9692 locations for all the variables in the var list to make the
9693 following errors more precise. Perhaps
9694 c_parser_omp_var_list_parens() should construct a list of
9695 locations to go along with the var list. */
9697 /* If V had already been marked threadprivate, it doesn't matter
9698 whether it had been used prior to this point. */
9699 if (TREE_CODE (v) != VAR_DECL)
9700 error_at (loc, "%qD is not a variable", v);
9701 else if (TREE_USED (v) && !C_DECL_THREADPRIVATE_P (v))
9702 error_at (loc, "%qE declared %<threadprivate%> after first use", v);
9703 else if (! TREE_STATIC (v) && ! DECL_EXTERNAL (v))
9704 error_at (loc, "automatic variable %qE cannot be %<threadprivate%>", v);
9705 else if (TREE_TYPE (v) == error_mark_node)
9707 else if (! COMPLETE_TYPE_P (TREE_TYPE (v)))
9708 error_at (loc, "%<threadprivate%> %qE has incomplete type", v);
9711 if (! DECL_THREAD_LOCAL_P (v))
9713 DECL_TLS_MODEL (v) = decl_default_tls_model (v);
9714 /* If rtl has been already set for this var, call
9715 make_decl_rtl once again, so that encode_section_info
9716 has a chance to look at the new decl flags. */
9717 if (DECL_RTL_SET_P (v))
9720 C_DECL_THREADPRIVATE_P (v) = 1;
9724 c_parser_skip_to_pragma_eol (parser);
9728 /* Parse a single source file. */
9733 /* Use local storage to begin. If the first token is a pragma, parse it.
9734 If it is #pragma GCC pch_preprocess, then this will load a PCH file
9735 which will cause garbage collection. */
9738 memset (&tparser, 0, sizeof tparser);
9739 the_parser = &tparser;
9741 if (c_parser_peek_token (&tparser)->pragma_kind == PRAGMA_GCC_PCH_PREPROCESS)
9742 c_parser_pragma_pch_preprocess (&tparser);
9744 the_parser = ggc_alloc_c_parser ();
9745 *the_parser = tparser;
9747 /* Initialize EH, if we've been told to do so. */
9748 if (flag_exceptions)
9749 using_eh_for_cleanups ();
9751 c_parser_translation_unit (the_parser);
9755 #include "gt-c-parser.h"