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"
54 #include "c-family/c-common.h"
55 #include "c-family/c-objc.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 is a type qualifier, false otherwise. */
511 c_token_is_qualifier (c_token *token)
516 switch (token->id_kind)
524 switch (token->keyword)
541 /* Return true if the next token from PARSER is a type qualifier,
544 c_parser_next_token_is_qualifier (c_parser *parser)
546 c_token *token = c_parser_peek_token (parser);
547 return c_token_is_qualifier (token);
550 /* Return true if TOKEN can start declaration specifiers, false
553 c_token_starts_declspecs (c_token *token)
558 switch (token->id_kind)
567 gcc_assert (c_dialect_objc ());
573 switch (token->keyword)
613 if (c_dialect_objc ())
622 /* Return true if TOKEN can start declaration specifiers or a static
623 assertion, false otherwise. */
625 c_token_starts_declaration (c_token *token)
627 if (c_token_starts_declspecs (token)
628 || token->keyword == RID_STATIC_ASSERT)
634 static c_token *c_parser_peek_2nd_token (c_parser *parser);
636 /* Return true if the next token from PARSER can start declaration
637 specifiers, false otherwise. */
639 c_parser_next_token_starts_declspecs (c_parser *parser)
641 c_token *token = c_parser_peek_token (parser);
643 /* In Objective-C, a classname normally starts a declspecs unless it
644 is immediately followed by a dot. In that case, it is the
645 Objective-C 2.0 "dot-syntax" for class objects, ie, calls the
646 setter/getter on the class. c_token_starts_declspecs() can't
647 differentiate between the two cases because it only checks the
648 current token, so we have a special check here. */
649 if (c_dialect_objc ()
650 && token->type == CPP_NAME
651 && token->id_kind == C_ID_CLASSNAME
652 && c_parser_peek_2nd_token (parser)->type == CPP_DOT)
655 return c_token_starts_declspecs (token);
658 /* Return true if the next tokens from PARSER can start declaration
659 specifiers or a static assertion, false otherwise. */
661 c_parser_next_tokens_start_declaration (c_parser *parser)
663 c_token *token = c_parser_peek_token (parser);
666 if (c_dialect_objc ()
667 && token->type == CPP_NAME
668 && token->id_kind == C_ID_CLASSNAME
669 && c_parser_peek_2nd_token (parser)->type == CPP_DOT)
672 /* Labels do not start declarations. */
673 if (token->type == CPP_NAME
674 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
677 if (c_token_starts_declaration (token))
680 /* Try a bit harder to detect an unknown typename. */
681 if (token->type == CPP_NAME
682 && token->id_kind == C_ID_ID
683 && (c_parser_peek_2nd_token (parser)->type == CPP_NAME
684 || c_parser_peek_2nd_token (parser)->type == CPP_MULT)
685 && !lookup_name (token->value)
687 /* Do not try too hard when we could have "object in array". */
688 && !parser->objc_could_be_foreach_context)
694 /* Return a pointer to the next-but-one token from PARSER, reading it
695 in if necessary. The next token is already read in. */
698 c_parser_peek_2nd_token (c_parser *parser)
700 if (parser->tokens_avail >= 2)
701 return &parser->tokens[1];
702 gcc_assert (parser->tokens_avail == 1);
703 gcc_assert (parser->tokens[0].type != CPP_EOF);
704 gcc_assert (parser->tokens[0].type != CPP_PRAGMA_EOL);
705 c_lex_one_token (parser, &parser->tokens[1]);
706 parser->tokens_avail = 2;
707 return &parser->tokens[1];
710 /* Consume the next token from PARSER. */
713 c_parser_consume_token (c_parser *parser)
715 gcc_assert (parser->tokens_avail >= 1);
716 gcc_assert (parser->tokens[0].type != CPP_EOF);
717 gcc_assert (!parser->in_pragma || parser->tokens[0].type != CPP_PRAGMA_EOL);
718 gcc_assert (parser->error || parser->tokens[0].type != CPP_PRAGMA);
719 if (parser->tokens_avail == 2)
720 parser->tokens[0] = parser->tokens[1];
721 parser->tokens_avail--;
724 /* Expect the current token to be a #pragma. Consume it and remember
725 that we've begun parsing a pragma. */
728 c_parser_consume_pragma (c_parser *parser)
730 gcc_assert (!parser->in_pragma);
731 gcc_assert (parser->tokens_avail >= 1);
732 gcc_assert (parser->tokens[0].type == CPP_PRAGMA);
733 if (parser->tokens_avail == 2)
734 parser->tokens[0] = parser->tokens[1];
735 parser->tokens_avail--;
736 parser->in_pragma = true;
739 /* Update the globals input_location and in_system_header from
742 c_parser_set_source_position_from_token (c_token *token)
744 if (token->type != CPP_EOF)
746 input_location = token->location;
750 /* Issue a diagnostic of the form
751 FILE:LINE: MESSAGE before TOKEN
752 where TOKEN is the next token in the input stream of PARSER.
753 MESSAGE (specified by the caller) is usually of the form "expected
756 Do not issue a diagnostic if still recovering from an error.
758 ??? This is taken from the C++ parser, but building up messages in
759 this way is not i18n-friendly and some other approach should be
763 c_parser_error (c_parser *parser, const char *gmsgid)
765 c_token *token = c_parser_peek_token (parser);
768 parser->error = true;
771 /* This diagnostic makes more sense if it is tagged to the line of
772 the token we just peeked at. */
773 c_parser_set_source_position_from_token (token);
774 c_parse_error (gmsgid,
775 /* Because c_parse_error does not understand
776 CPP_KEYWORD, keywords are treated like
778 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
779 /* ??? The C parser does not save the cpp flags of a
780 token, we need to pass 0 here and we will not get
781 the source spelling of some tokens but rather the
782 canonical spelling. */
783 token->value, /*flags=*/0);
786 /* If the next token is of the indicated TYPE, consume it. Otherwise,
787 issue the error MSGID. If MSGID is NULL then a message has already
788 been produced and no message will be produced this time. Returns
789 true if found, false otherwise. */
792 c_parser_require (c_parser *parser,
796 if (c_parser_next_token_is (parser, type))
798 c_parser_consume_token (parser);
803 c_parser_error (parser, msgid);
808 /* If the next token is the indicated keyword, consume it. Otherwise,
809 issue the error MSGID. Returns true if found, false otherwise. */
812 c_parser_require_keyword (c_parser *parser,
816 if (c_parser_next_token_is_keyword (parser, keyword))
818 c_parser_consume_token (parser);
823 c_parser_error (parser, msgid);
828 /* Like c_parser_require, except that tokens will be skipped until the
829 desired token is found. An error message is still produced if the
830 next token is not as expected. If MSGID is NULL then a message has
831 already been produced and no message will be produced this
835 c_parser_skip_until_found (c_parser *parser,
839 unsigned nesting_depth = 0;
841 if (c_parser_require (parser, type, msgid))
844 /* Skip tokens until the desired token is found. */
847 /* Peek at the next token. */
848 c_token *token = c_parser_peek_token (parser);
849 /* If we've reached the token we want, consume it and stop. */
850 if (token->type == type && !nesting_depth)
852 c_parser_consume_token (parser);
856 /* If we've run out of tokens, stop. */
857 if (token->type == CPP_EOF)
859 if (token->type == CPP_PRAGMA_EOL && parser->in_pragma)
861 if (token->type == CPP_OPEN_BRACE
862 || token->type == CPP_OPEN_PAREN
863 || token->type == CPP_OPEN_SQUARE)
865 else if (token->type == CPP_CLOSE_BRACE
866 || token->type == CPP_CLOSE_PAREN
867 || token->type == CPP_CLOSE_SQUARE)
869 if (nesting_depth-- == 0)
872 /* Consume this token. */
873 c_parser_consume_token (parser);
875 parser->error = false;
878 /* Skip tokens until the end of a parameter is found, but do not
879 consume the comma, semicolon or closing delimiter. */
882 c_parser_skip_to_end_of_parameter (c_parser *parser)
884 unsigned nesting_depth = 0;
888 c_token *token = c_parser_peek_token (parser);
889 if ((token->type == CPP_COMMA || token->type == CPP_SEMICOLON)
892 /* If we've run out of tokens, stop. */
893 if (token->type == CPP_EOF)
895 if (token->type == CPP_PRAGMA_EOL && parser->in_pragma)
897 if (token->type == CPP_OPEN_BRACE
898 || token->type == CPP_OPEN_PAREN
899 || token->type == CPP_OPEN_SQUARE)
901 else if (token->type == CPP_CLOSE_BRACE
902 || token->type == CPP_CLOSE_PAREN
903 || token->type == CPP_CLOSE_SQUARE)
905 if (nesting_depth-- == 0)
908 /* Consume this token. */
909 c_parser_consume_token (parser);
911 parser->error = false;
914 /* Expect to be at the end of the pragma directive and consume an
915 end of line marker. */
918 c_parser_skip_to_pragma_eol (c_parser *parser)
920 gcc_assert (parser->in_pragma);
921 parser->in_pragma = false;
923 if (!c_parser_require (parser, CPP_PRAGMA_EOL, "expected end of line"))
926 c_token *token = c_parser_peek_token (parser);
927 if (token->type == CPP_EOF)
929 if (token->type == CPP_PRAGMA_EOL)
931 c_parser_consume_token (parser);
934 c_parser_consume_token (parser);
937 parser->error = false;
940 /* Skip tokens until we have consumed an entire block, or until we
941 have consumed a non-nested ';'. */
944 c_parser_skip_to_end_of_block_or_statement (c_parser *parser)
946 unsigned nesting_depth = 0;
947 bool save_error = parser->error;
953 /* Peek at the next token. */
954 token = c_parser_peek_token (parser);
962 if (parser->in_pragma)
967 /* If the next token is a ';', we have reached the
968 end of the statement. */
971 /* Consume the ';'. */
972 c_parser_consume_token (parser);
977 case CPP_CLOSE_BRACE:
978 /* If the next token is a non-nested '}', then we have
979 reached the end of the current block. */
980 if (nesting_depth == 0 || --nesting_depth == 0)
982 c_parser_consume_token (parser);
988 /* If it the next token is a '{', then we are entering a new
989 block. Consume the entire block. */
994 /* If we see a pragma, consume the whole thing at once. We
995 have some safeguards against consuming pragmas willy-nilly.
996 Normally, we'd expect to be here with parser->error set,
997 which disables these safeguards. But it's possible to get
998 here for secondary error recovery, after parser->error has
1000 c_parser_consume_pragma (parser);
1001 c_parser_skip_to_pragma_eol (parser);
1002 parser->error = save_error;
1009 c_parser_consume_token (parser);
1013 parser->error = false;
1016 /* CPP's options (initialized by c-opts.c). */
1017 extern cpp_options *cpp_opts;
1019 /* Save the warning flags which are controlled by __extension__. */
1022 disable_extension_diagnostics (void)
1025 | (warn_pointer_arith << 1)
1026 | (warn_traditional << 2)
1028 | (warn_long_long << 4)
1029 | (warn_cxx_compat << 5));
1030 cpp_opts->cpp_pedantic = pedantic = 0;
1031 warn_pointer_arith = 0;
1032 cpp_opts->cpp_warn_traditional = warn_traditional = 0;
1034 cpp_opts->cpp_warn_long_long = warn_long_long = 0;
1035 warn_cxx_compat = 0;
1039 /* Restore the warning flags which are controlled by __extension__.
1040 FLAGS is the return value from disable_extension_diagnostics. */
1043 restore_extension_diagnostics (int flags)
1045 cpp_opts->cpp_pedantic = pedantic = flags & 1;
1046 warn_pointer_arith = (flags >> 1) & 1;
1047 cpp_opts->cpp_warn_traditional = warn_traditional = (flags >> 2) & 1;
1048 flag_iso = (flags >> 3) & 1;
1049 cpp_opts->cpp_warn_long_long = warn_long_long = (flags >> 4) & 1;
1050 warn_cxx_compat = (flags >> 5) & 1;
1053 /* Possibly kinds of declarator to parse. */
1054 typedef enum c_dtr_syn {
1055 /* A normal declarator with an identifier. */
1057 /* An abstract declarator (maybe empty). */
1059 /* A parameter declarator: may be either, but after a type name does
1060 not redeclare a typedef name as an identifier if it can
1061 alternatively be interpreted as a typedef name; see DR#009,
1062 applied in C90 TC1, omitted from C99 and reapplied in C99 TC2
1063 following DR#249. For example, given a typedef T, "int T" and
1064 "int *T" are valid parameter declarations redeclaring T, while
1065 "int (T)" and "int * (T)" and "int (T[])" and "int (T (int))" are
1066 abstract declarators rather than involving redundant parentheses;
1067 the same applies with attributes inside the parentheses before
1072 static void c_parser_external_declaration (c_parser *);
1073 static void c_parser_asm_definition (c_parser *);
1074 static void c_parser_declaration_or_fndef (c_parser *, bool, bool, bool,
1075 bool, bool, tree *);
1076 static void c_parser_static_assert_declaration_no_semi (c_parser *);
1077 static void c_parser_static_assert_declaration (c_parser *);
1078 static void c_parser_declspecs (c_parser *, struct c_declspecs *, bool, bool,
1080 static struct c_typespec c_parser_enum_specifier (c_parser *);
1081 static struct c_typespec c_parser_struct_or_union_specifier (c_parser *);
1082 static tree c_parser_struct_declaration (c_parser *);
1083 static struct c_typespec c_parser_typeof_specifier (c_parser *);
1084 static struct c_declarator *c_parser_declarator (c_parser *, bool, c_dtr_syn,
1086 static struct c_declarator *c_parser_direct_declarator (c_parser *, bool,
1088 static struct c_declarator *c_parser_direct_declarator_inner (c_parser *,
1090 struct c_declarator *);
1091 static struct c_arg_info *c_parser_parms_declarator (c_parser *, bool, tree);
1092 static struct c_arg_info *c_parser_parms_list_declarator (c_parser *, tree);
1093 static struct c_parm *c_parser_parameter_declaration (c_parser *, tree);
1094 static tree c_parser_simple_asm_expr (c_parser *);
1095 static tree c_parser_attributes (c_parser *);
1096 static struct c_type_name *c_parser_type_name (c_parser *);
1097 static struct c_expr c_parser_initializer (c_parser *);
1098 static struct c_expr c_parser_braced_init (c_parser *, tree, bool);
1099 static void c_parser_initelt (c_parser *, struct obstack *);
1100 static void c_parser_initval (c_parser *, struct c_expr *,
1102 static tree c_parser_compound_statement (c_parser *);
1103 static void c_parser_compound_statement_nostart (c_parser *);
1104 static void c_parser_label (c_parser *);
1105 static void c_parser_statement (c_parser *);
1106 static void c_parser_statement_after_labels (c_parser *);
1107 static void c_parser_if_statement (c_parser *);
1108 static void c_parser_switch_statement (c_parser *);
1109 static void c_parser_while_statement (c_parser *);
1110 static void c_parser_do_statement (c_parser *);
1111 static void c_parser_for_statement (c_parser *);
1112 static tree c_parser_asm_statement (c_parser *);
1113 static tree c_parser_asm_operands (c_parser *, bool);
1114 static tree c_parser_asm_goto_operands (c_parser *);
1115 static tree c_parser_asm_clobbers (c_parser *);
1116 static struct c_expr c_parser_expr_no_commas (c_parser *, struct c_expr *);
1117 static struct c_expr c_parser_conditional_expression (c_parser *,
1119 static struct c_expr c_parser_binary_expression (c_parser *, struct c_expr *);
1120 static struct c_expr c_parser_cast_expression (c_parser *, struct c_expr *);
1121 static struct c_expr c_parser_unary_expression (c_parser *);
1122 static struct c_expr c_parser_sizeof_expression (c_parser *);
1123 static struct c_expr c_parser_alignof_expression (c_parser *);
1124 static struct c_expr c_parser_postfix_expression (c_parser *);
1125 static struct c_expr c_parser_postfix_expression_after_paren_type (c_parser *,
1126 struct c_type_name *,
1128 static struct c_expr c_parser_postfix_expression_after_primary (c_parser *,
1131 static struct c_expr c_parser_expression (c_parser *);
1132 static struct c_expr c_parser_expression_conv (c_parser *);
1133 static VEC(tree,gc) *c_parser_expr_list (c_parser *, bool, bool,
1135 static void c_parser_omp_construct (c_parser *);
1136 static void c_parser_omp_threadprivate (c_parser *);
1137 static void c_parser_omp_barrier (c_parser *);
1138 static void c_parser_omp_flush (c_parser *);
1139 static void c_parser_omp_taskwait (c_parser *);
1141 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
1142 static bool c_parser_pragma (c_parser *, enum pragma_context);
1144 /* These Objective-C parser functions are only ever called when
1145 compiling Objective-C. */
1146 static void c_parser_objc_class_definition (c_parser *, tree);
1147 static void c_parser_objc_class_instance_variables (c_parser *);
1148 static void c_parser_objc_class_declaration (c_parser *);
1149 static void c_parser_objc_alias_declaration (c_parser *);
1150 static void c_parser_objc_protocol_definition (c_parser *, tree);
1151 static bool c_parser_objc_method_type (c_parser *);
1152 static void c_parser_objc_method_definition (c_parser *);
1153 static void c_parser_objc_methodprotolist (c_parser *);
1154 static void c_parser_objc_methodproto (c_parser *);
1155 static tree c_parser_objc_method_decl (c_parser *, bool, tree *);
1156 static tree c_parser_objc_type_name (c_parser *);
1157 static tree c_parser_objc_protocol_refs (c_parser *);
1158 static void c_parser_objc_try_catch_finally_statement (c_parser *);
1159 static void c_parser_objc_synchronized_statement (c_parser *);
1160 static tree c_parser_objc_selector (c_parser *);
1161 static tree c_parser_objc_selector_arg (c_parser *);
1162 static tree c_parser_objc_receiver (c_parser *);
1163 static tree c_parser_objc_message_args (c_parser *);
1164 static tree c_parser_objc_keywordexpr (c_parser *);
1165 static void c_parser_objc_at_property_declaration (c_parser *);
1166 static void c_parser_objc_at_synthesize_declaration (c_parser *);
1167 static void c_parser_objc_at_dynamic_declaration (c_parser *);
1168 static bool c_parser_objc_diagnose_bad_element_prefix
1169 (c_parser *, struct c_declspecs *);
1171 /* Parse a translation unit (C90 6.7, C99 6.9).
1174 external-declarations
1176 external-declarations:
1177 external-declaration
1178 external-declarations external-declaration
1187 c_parser_translation_unit (c_parser *parser)
1189 if (c_parser_next_token_is (parser, CPP_EOF))
1191 pedwarn (c_parser_peek_token (parser)->location, OPT_pedantic,
1192 "ISO C forbids an empty translation unit");
1196 void *obstack_position = obstack_alloc (&parser_obstack, 0);
1197 mark_valid_location_for_stdc_pragma (false);
1201 c_parser_external_declaration (parser);
1202 obstack_free (&parser_obstack, obstack_position);
1204 while (c_parser_next_token_is_not (parser, CPP_EOF));
1208 /* Parse an external declaration (C90 6.7, C99 6.9).
1210 external-declaration:
1216 external-declaration:
1219 __extension__ external-declaration
1223 external-declaration:
1224 objc-class-definition
1225 objc-class-declaration
1226 objc-alias-declaration
1227 objc-protocol-definition
1228 objc-method-definition
1233 c_parser_external_declaration (c_parser *parser)
1236 switch (c_parser_peek_token (parser)->type)
1239 switch (c_parser_peek_token (parser)->keyword)
1242 ext = disable_extension_diagnostics ();
1243 c_parser_consume_token (parser);
1244 c_parser_external_declaration (parser);
1245 restore_extension_diagnostics (ext);
1248 c_parser_asm_definition (parser);
1250 case RID_AT_INTERFACE:
1251 case RID_AT_IMPLEMENTATION:
1252 gcc_assert (c_dialect_objc ());
1253 c_parser_objc_class_definition (parser, NULL_TREE);
1256 gcc_assert (c_dialect_objc ());
1257 c_parser_objc_class_declaration (parser);
1260 gcc_assert (c_dialect_objc ());
1261 c_parser_objc_alias_declaration (parser);
1263 case RID_AT_PROTOCOL:
1264 gcc_assert (c_dialect_objc ());
1265 c_parser_objc_protocol_definition (parser, NULL_TREE);
1267 case RID_AT_PROPERTY:
1268 gcc_assert (c_dialect_objc ());
1269 c_parser_objc_at_property_declaration (parser);
1271 case RID_AT_SYNTHESIZE:
1272 gcc_assert (c_dialect_objc ());
1273 c_parser_objc_at_synthesize_declaration (parser);
1275 case RID_AT_DYNAMIC:
1276 gcc_assert (c_dialect_objc ());
1277 c_parser_objc_at_dynamic_declaration (parser);
1280 gcc_assert (c_dialect_objc ());
1281 c_parser_consume_token (parser);
1282 objc_finish_implementation ();
1289 pedwarn (c_parser_peek_token (parser)->location, OPT_pedantic,
1290 "ISO C does not allow extra %<;%> outside of a function");
1291 c_parser_consume_token (parser);
1294 mark_valid_location_for_stdc_pragma (true);
1295 c_parser_pragma (parser, pragma_external);
1296 mark_valid_location_for_stdc_pragma (false);
1300 if (c_dialect_objc ())
1302 c_parser_objc_method_definition (parser);
1305 /* Else fall through, and yield a syntax error trying to parse
1306 as a declaration or function definition. */
1309 /* A declaration or a function definition (or, in Objective-C,
1310 an @interface or @protocol with prefix attributes). We can
1311 only tell which after parsing the declaration specifiers, if
1312 any, and the first declarator. */
1313 c_parser_declaration_or_fndef (parser, true, true, true, false, true, NULL);
1318 /* Parse a declaration or function definition (C90 6.5, 6.7.1, C99
1319 6.7, 6.9.1). If FNDEF_OK is true, a function definition is
1320 accepted; otherwise (old-style parameter declarations) only other
1321 declarations are accepted. If STATIC_ASSERT_OK is true, a static
1322 assertion is accepted; otherwise (old-style parameter declarations)
1323 it is not. If NESTED is true, we are inside a function or parsing
1324 old-style parameter declarations; any functions encountered are
1325 nested functions and declaration specifiers are required; otherwise
1326 we are at top level and functions are normal functions and
1327 declaration specifiers may be optional. If EMPTY_OK is true, empty
1328 declarations are OK (subject to all other constraints); otherwise
1329 (old-style parameter declarations) they are diagnosed. If
1330 START_ATTR_OK is true, the declaration specifiers may start with
1331 attributes; otherwise they may not.
1332 OBJC_FOREACH_OBJECT_DECLARATION can be used to get back the parsed
1333 declaration when parsing an Objective-C foreach statement.
1336 declaration-specifiers init-declarator-list[opt] ;
1337 static_assert-declaration
1339 function-definition:
1340 declaration-specifiers[opt] declarator declaration-list[opt]
1345 declaration-list declaration
1347 init-declarator-list:
1349 init-declarator-list , init-declarator
1352 declarator simple-asm-expr[opt] attributes[opt]
1353 declarator simple-asm-expr[opt] attributes[opt] = initializer
1357 nested-function-definition:
1358 declaration-specifiers declarator declaration-list[opt]
1362 attributes objc-class-definition
1363 attributes objc-category-definition
1364 attributes objc-protocol-definition
1366 The simple-asm-expr and attributes are GNU extensions.
1368 This function does not handle __extension__; that is handled in its
1369 callers. ??? Following the old parser, __extension__ may start
1370 external declarations, declarations in functions and declarations
1371 at the start of "for" loops, but not old-style parameter
1374 C99 requires declaration specifiers in a function definition; the
1375 absence is diagnosed through the diagnosis of implicit int. In GNU
1376 C we also allow but diagnose declarations without declaration
1377 specifiers, but only at top level (elsewhere they conflict with
1380 In Objective-C, declarations of the looping variable in a foreach
1381 statement are exceptionally terminated by 'in' (for example, 'for
1382 (NSObject *object in array) { ... }').
1387 threadprivate-directive */
1390 c_parser_declaration_or_fndef (c_parser *parser, bool fndef_ok,
1391 bool static_assert_ok, bool empty_ok,
1392 bool nested, bool start_attr_ok,
1393 tree *objc_foreach_object_declaration)
1395 struct c_declspecs *specs;
1397 tree all_prefix_attrs;
1398 bool diagnosed_no_specs = false;
1399 location_t here = c_parser_peek_token (parser)->location;
1401 if (static_assert_ok
1402 && c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT))
1404 c_parser_static_assert_declaration (parser);
1407 specs = build_null_declspecs ();
1409 /* Try to detect an unknown type name when we have "A B" or "A *B". */
1410 if (c_parser_peek_token (parser)->type == CPP_NAME
1411 && c_parser_peek_token (parser)->id_kind == C_ID_ID
1412 && (c_parser_peek_2nd_token (parser)->type == CPP_NAME
1413 || c_parser_peek_2nd_token (parser)->type == CPP_MULT)
1414 && (!nested || !lookup_name (c_parser_peek_token (parser)->value)))
1416 error_at (here, "unknown type name %qE",
1417 c_parser_peek_token (parser)->value);
1419 /* Parse declspecs normally to get a correct pointer type, but avoid
1420 a further "fails to be a type name" error. Refuse nested functions
1421 since it is not how the user likely wants us to recover. */
1422 c_parser_peek_token (parser)->type = CPP_KEYWORD;
1423 c_parser_peek_token (parser)->keyword = RID_VOID;
1424 c_parser_peek_token (parser)->value = error_mark_node;
1428 c_parser_declspecs (parser, specs, true, true, start_attr_ok);
1431 c_parser_skip_to_end_of_block_or_statement (parser);
1434 if (nested && !specs->declspecs_seen_p)
1436 c_parser_error (parser, "expected declaration specifiers");
1437 c_parser_skip_to_end_of_block_or_statement (parser);
1440 finish_declspecs (specs);
1441 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1447 shadow_tag_warned (specs, 1);
1448 pedwarn (here, 0, "empty declaration");
1450 c_parser_consume_token (parser);
1454 /* Provide better error recovery. Note that a type name here is usually
1455 better diagnosed as a redeclaration. */
1457 && specs->typespec_kind == ctsk_tagdef
1458 && c_parser_next_token_starts_declspecs (parser)
1459 && !c_parser_next_token_is (parser, CPP_NAME))
1461 c_parser_error (parser, "expected %<;%>, identifier or %<(%>");
1462 parser->error = false;
1463 shadow_tag_warned (specs, 1);
1466 else if (c_dialect_objc ())
1468 /* Prefix attributes are an error on method decls. */
1469 switch (c_parser_peek_token (parser)->type)
1473 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1477 warning_at (c_parser_peek_token (parser)->location,
1479 "prefix attributes are ignored for methods");
1480 specs->attrs = NULL_TREE;
1483 c_parser_objc_method_definition (parser);
1485 c_parser_objc_methodproto (parser);
1491 /* This is where we parse 'attributes @interface ...',
1492 'attributes @implementation ...', 'attributes @protocol ...'
1493 (where attributes could be, for example, __attribute__
1496 switch (c_parser_peek_token (parser)->keyword)
1498 case RID_AT_INTERFACE:
1500 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1502 c_parser_objc_class_definition (parser, specs->attrs);
1506 case RID_AT_IMPLEMENTATION:
1508 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1512 warning_at (c_parser_peek_token (parser)->location,
1514 "prefix attributes are ignored for implementations");
1515 specs->attrs = NULL_TREE;
1517 c_parser_objc_class_definition (parser, NULL_TREE);
1521 case RID_AT_PROTOCOL:
1523 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1525 c_parser_objc_protocol_definition (parser, specs->attrs);
1532 case RID_AT_PROPERTY:
1535 c_parser_error (parser,
1536 "attributes may not be specified before" );
1537 specs->attrs = NULL;
1545 pending_xref_error ();
1546 prefix_attrs = specs->attrs;
1547 all_prefix_attrs = prefix_attrs;
1548 specs->attrs = NULL_TREE;
1551 struct c_declarator *declarator;
1554 /* Declaring either one or more declarators (in which case we
1555 should diagnose if there were no declaration specifiers) or a
1556 function definition (in which case the diagnostic for
1557 implicit int suffices). */
1558 declarator = c_parser_declarator (parser,
1559 specs->typespec_kind != ctsk_none,
1560 C_DTR_NORMAL, &dummy);
1561 if (declarator == NULL)
1563 c_parser_skip_to_end_of_block_or_statement (parser);
1566 if (c_parser_next_token_is (parser, CPP_EQ)
1567 || c_parser_next_token_is (parser, CPP_COMMA)
1568 || c_parser_next_token_is (parser, CPP_SEMICOLON)
1569 || c_parser_next_token_is_keyword (parser, RID_ASM)
1570 || c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE)
1571 || c_parser_next_token_is_keyword (parser, RID_IN))
1573 tree asm_name = NULL_TREE;
1574 tree postfix_attrs = NULL_TREE;
1575 if (!diagnosed_no_specs && !specs->declspecs_seen_p)
1577 diagnosed_no_specs = true;
1578 pedwarn (here, 0, "data definition has no type or storage class");
1580 /* Having seen a data definition, there cannot now be a
1581 function definition. */
1583 if (c_parser_next_token_is_keyword (parser, RID_ASM))
1584 asm_name = c_parser_simple_asm_expr (parser);
1585 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
1586 postfix_attrs = c_parser_attributes (parser);
1587 if (c_parser_next_token_is (parser, CPP_EQ))
1591 location_t init_loc;
1592 c_parser_consume_token (parser);
1593 /* The declaration of the variable is in effect while
1594 its initializer is parsed. */
1595 d = start_decl (declarator, specs, true,
1596 chainon (postfix_attrs, all_prefix_attrs));
1598 d = error_mark_node;
1599 start_init (d, asm_name, global_bindings_p ());
1600 init_loc = c_parser_peek_token (parser)->location;
1601 init = c_parser_initializer (parser);
1603 if (d != error_mark_node)
1605 maybe_warn_string_init (TREE_TYPE (d), init);
1606 finish_decl (d, init_loc, init.value,
1607 init.original_type, asm_name);
1612 tree d = start_decl (declarator, specs, false,
1613 chainon (postfix_attrs,
1616 finish_decl (d, UNKNOWN_LOCATION, NULL_TREE,
1617 NULL_TREE, asm_name);
1619 if (c_parser_next_token_is_keyword (parser, RID_IN))
1622 *objc_foreach_object_declaration = d;
1624 *objc_foreach_object_declaration = error_mark_node;
1627 if (c_parser_next_token_is (parser, CPP_COMMA))
1629 c_parser_consume_token (parser);
1630 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
1631 all_prefix_attrs = chainon (c_parser_attributes (parser),
1634 all_prefix_attrs = prefix_attrs;
1637 else if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1639 c_parser_consume_token (parser);
1642 else if (c_parser_next_token_is_keyword (parser, RID_IN))
1644 /* This can only happen in Objective-C: we found the
1645 'in' that terminates the declaration inside an
1646 Objective-C foreach statement. Do not consume the
1647 token, so that the caller can use it to determine
1648 that this indeed is a foreach context. */
1653 c_parser_error (parser, "expected %<,%> or %<;%>");
1654 c_parser_skip_to_end_of_block_or_statement (parser);
1660 c_parser_error (parser, "expected %<=%>, %<,%>, %<;%>, "
1661 "%<asm%> or %<__attribute__%>");
1662 c_parser_skip_to_end_of_block_or_statement (parser);
1665 /* Function definition (nested or otherwise). */
1668 pedwarn (here, OPT_pedantic, "ISO C forbids nested functions");
1669 c_push_function_context ();
1671 if (!start_function (specs, declarator, all_prefix_attrs))
1673 /* This can appear in many cases looking nothing like a
1674 function definition, so we don't give a more specific
1675 error suggesting there was one. */
1676 c_parser_error (parser, "expected %<=%>, %<,%>, %<;%>, %<asm%> "
1677 "or %<__attribute__%>");
1679 c_pop_function_context ();
1682 /* Parse old-style parameter declarations. ??? Attributes are
1683 not allowed to start declaration specifiers here because of a
1684 syntax conflict between a function declaration with attribute
1685 suffix and a function definition with an attribute prefix on
1686 first old-style parameter declaration. Following the old
1687 parser, they are not accepted on subsequent old-style
1688 parameter declarations either. However, there is no
1689 ambiguity after the first declaration, nor indeed on the
1690 first as long as we don't allow postfix attributes after a
1691 declarator with a nonempty identifier list in a definition;
1692 and postfix attributes have never been accepted here in
1693 function definitions either. */
1694 while (c_parser_next_token_is_not (parser, CPP_EOF)
1695 && c_parser_next_token_is_not (parser, CPP_OPEN_BRACE))
1696 c_parser_declaration_or_fndef (parser, false, false, false,
1698 store_parm_decls ();
1699 DECL_STRUCT_FUNCTION (current_function_decl)->function_start_locus
1700 = c_parser_peek_token (parser)->location;
1701 fnbody = c_parser_compound_statement (parser);
1704 tree decl = current_function_decl;
1705 /* Mark nested functions as needing static-chain initially.
1706 lower_nested_functions will recompute it but the
1707 DECL_STATIC_CHAIN flag is also used before that happens,
1708 by initializer_constant_valid_p. See gcc.dg/nested-fn-2.c. */
1709 DECL_STATIC_CHAIN (decl) = 1;
1712 c_pop_function_context ();
1713 add_stmt (build_stmt (DECL_SOURCE_LOCATION (decl), DECL_EXPR, decl));
1724 /* Parse an asm-definition (asm() outside a function body). This is a
1732 c_parser_asm_definition (c_parser *parser)
1734 tree asm_str = c_parser_simple_asm_expr (parser);
1736 cgraph_add_asm_node (asm_str);
1737 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
1740 /* Parse a static assertion (C1X N1425 6.7.10).
1742 static_assert-declaration:
1743 static_assert-declaration-no-semi ;
1747 c_parser_static_assert_declaration (c_parser *parser)
1749 c_parser_static_assert_declaration_no_semi (parser);
1751 || !c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
1752 c_parser_skip_to_end_of_block_or_statement (parser);
1755 /* Parse a static assertion (C1X N1425 6.7.10), without the trailing
1758 static_assert-declaration-no-semi:
1759 _Static_assert ( constant-expression , string-literal )
1763 c_parser_static_assert_declaration_no_semi (c_parser *parser)
1765 location_t assert_loc, value_loc;
1769 gcc_assert (c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT));
1770 assert_loc = c_parser_peek_token (parser)->location;
1774 pedwarn (assert_loc, OPT_pedantic,
1775 "ISO C99 does not support %<_Static_assert%>");
1777 pedwarn (assert_loc, OPT_pedantic,
1778 "ISO C90 does not support %<_Static_assert%>");
1780 c_parser_consume_token (parser);
1781 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
1783 value_loc = c_parser_peek_token (parser)->location;
1784 value = c_parser_expr_no_commas (parser, NULL).value;
1785 parser->lex_untranslated_string = true;
1786 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
1788 parser->lex_untranslated_string = false;
1791 switch (c_parser_peek_token (parser)->type)
1797 case CPP_UTF8STRING:
1798 string = c_parser_peek_token (parser)->value;
1799 c_parser_consume_token (parser);
1800 parser->lex_untranslated_string = false;
1803 c_parser_error (parser, "expected string literal");
1804 parser->lex_untranslated_string = false;
1807 c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
1809 if (!INTEGRAL_TYPE_P (TREE_TYPE (value)))
1811 error_at (value_loc, "expression in static assertion is not an integer");
1814 if (TREE_CODE (value) != INTEGER_CST)
1816 value = c_fully_fold (value, false, NULL);
1817 if (TREE_CODE (value) == INTEGER_CST)
1818 pedwarn (value_loc, OPT_pedantic, "expression in static assertion "
1819 "is not an integer constant expression");
1821 if (TREE_CODE (value) != INTEGER_CST)
1823 error_at (value_loc, "expression in static assertion is not constant");
1826 constant_expression_warning (value);
1827 if (integer_zerop (value))
1828 error_at (assert_loc, "static assertion failed: %E", string);
1831 /* Parse some declaration specifiers (possibly none) (C90 6.5, C99
1832 6.7), adding them to SPECS (which may already include some).
1833 Storage class specifiers are accepted iff SCSPEC_OK; type
1834 specifiers are accepted iff TYPESPEC_OK; attributes are accepted at
1835 the start iff START_ATTR_OK.
1837 declaration-specifiers:
1838 storage-class-specifier declaration-specifiers[opt]
1839 type-specifier declaration-specifiers[opt]
1840 type-qualifier declaration-specifiers[opt]
1841 function-specifier declaration-specifiers[opt]
1843 Function specifiers (inline) are from C99, and are currently
1844 handled as storage class specifiers, as is __thread.
1846 C90 6.5.1, C99 6.7.1:
1847 storage-class-specifier:
1858 C90 6.5.2, C99 6.7.2:
1871 [_Imaginary removed in C99 TC2]
1872 struct-or-union-specifier
1876 (_Bool and _Complex are new in C99.)
1878 C90 6.5.3, C99 6.7.3:
1884 address-space-qualifier
1886 (restrict is new in C99.)
1890 declaration-specifiers:
1891 attributes declaration-specifiers[opt]
1897 identifier recognized by the target
1899 storage-class-specifier:
1912 (_Fract, _Accum, and _Sat are new from ISO/IEC DTR 18037:
1913 http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1169.pdf)
1918 class-name objc-protocol-refs[opt]
1919 typedef-name objc-protocol-refs
1924 c_parser_declspecs (c_parser *parser, struct c_declspecs *specs,
1925 bool scspec_ok, bool typespec_ok, bool start_attr_ok)
1927 bool attrs_ok = start_attr_ok;
1928 bool seen_type = specs->typespec_kind != ctsk_none;
1929 while ((c_parser_next_token_is (parser, CPP_NAME)
1930 && c_parser_peek_token (parser)->id_kind != C_ID_ID)
1931 || c_parser_next_token_is (parser, CPP_KEYWORD)
1932 || (c_dialect_objc () && c_parser_next_token_is (parser, CPP_LESS)))
1934 struct c_typespec t;
1936 location_t loc = c_parser_peek_token (parser)->location;
1938 if (!c_parser_next_token_is_qualifier (parser))
1940 /* Exit for TYPENAMEs after any type because they can appear as a
1942 if (seen_type && c_parser_next_token_is (parser, CPP_NAME))
1945 /* If we cannot accept a type, and the next token must start one,
1946 exit. Do the same if we already have seen a tagged definition,
1947 since it would be an error anyway and likely the user has simply
1948 forgotten a semicolon. */
1949 if ((!typespec_ok || specs->typespec_kind == ctsk_tagdef)
1950 && c_parser_next_token_starts_typename (parser))
1954 if (c_parser_next_token_is (parser, CPP_NAME))
1956 tree value = c_parser_peek_token (parser)->value;
1957 c_id_kind kind = c_parser_peek_token (parser)->id_kind;
1959 if (kind == C_ID_ADDRSPACE)
1962 = c_parser_peek_token (parser)->keyword - RID_FIRST_ADDR_SPACE;
1963 declspecs_add_addrspace (specs, as);
1964 c_parser_consume_token (parser);
1969 /* Now at a C_ID_TYPENAME or C_ID_CLASSNAME. */
1970 c_parser_consume_token (parser);
1973 if (kind == C_ID_TYPENAME
1974 && (!c_dialect_objc ()
1975 || c_parser_next_token_is_not (parser, CPP_LESS)))
1977 t.kind = ctsk_typedef;
1978 /* For a typedef name, record the meaning, not the name.
1979 In case of 'foo foo, bar;'. */
1980 t.spec = lookup_name (value);
1982 t.expr_const_operands = true;
1986 tree proto = NULL_TREE;
1987 gcc_assert (c_dialect_objc ());
1989 if (c_parser_next_token_is (parser, CPP_LESS))
1990 proto = c_parser_objc_protocol_refs (parser);
1991 t.spec = objc_get_protocol_qualified_type (value, proto);
1993 t.expr_const_operands = true;
1995 declspecs_add_type (loc, specs, t);
1998 if (c_parser_next_token_is (parser, CPP_LESS))
2000 /* Make "<SomeProtocol>" equivalent to "id <SomeProtocol>" -
2001 nisse@lysator.liu.se. */
2003 gcc_assert (c_dialect_objc ());
2004 if (!typespec_ok || seen_type)
2006 proto = c_parser_objc_protocol_refs (parser);
2008 t.spec = objc_get_protocol_qualified_type (NULL_TREE, proto);
2010 t.expr_const_operands = true;
2011 declspecs_add_type (loc, specs, t);
2014 gcc_assert (c_parser_next_token_is (parser, CPP_KEYWORD));
2015 switch (c_parser_peek_token (parser)->keyword)
2027 /* TODO: Distinguish between function specifiers (inline)
2028 and storage class specifiers, either here or in
2029 declspecs_add_scspec. */
2030 declspecs_add_scspec (specs, c_parser_peek_token (parser)->value);
2031 c_parser_consume_token (parser);
2055 if (c_dialect_objc ())
2056 parser->objc_need_raw_identifier = true;
2057 t.kind = ctsk_resword;
2058 t.spec = c_parser_peek_token (parser)->value;
2060 t.expr_const_operands = true;
2061 declspecs_add_type (loc, specs, t);
2062 c_parser_consume_token (parser);
2069 t = c_parser_enum_specifier (parser);
2070 declspecs_add_type (loc, specs, t);
2078 t = c_parser_struct_or_union_specifier (parser);
2079 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, t.spec);
2080 declspecs_add_type (loc, specs, t);
2083 /* ??? The old parser rejected typeof after other type
2084 specifiers, but is a syntax error the best way of
2086 if (!typespec_ok || seen_type)
2090 t = c_parser_typeof_specifier (parser);
2091 declspecs_add_type (loc, specs, t);
2097 declspecs_add_qual (specs, c_parser_peek_token (parser)->value);
2098 c_parser_consume_token (parser);
2103 attrs = c_parser_attributes (parser);
2104 declspecs_add_attrs (specs, attrs);
2113 /* Parse an enum specifier (C90 6.5.2.2, C99 6.7.2.2).
2116 enum attributes[opt] identifier[opt] { enumerator-list } attributes[opt]
2117 enum attributes[opt] identifier[opt] { enumerator-list , } attributes[opt]
2118 enum attributes[opt] identifier
2120 The form with trailing comma is new in C99. The forms with
2121 attributes are GNU extensions. In GNU C, we accept any expression
2122 without commas in the syntax (assignment expressions, not just
2123 conditional expressions); assignment expressions will be diagnosed
2128 enumerator-list , enumerator
2131 enumeration-constant
2132 enumeration-constant = constant-expression
2135 static struct c_typespec
2136 c_parser_enum_specifier (c_parser *parser)
2138 struct c_typespec ret;
2140 tree ident = NULL_TREE;
2141 location_t enum_loc;
2142 location_t ident_loc = UNKNOWN_LOCATION; /* Quiet warning. */
2143 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ENUM));
2144 enum_loc = c_parser_peek_token (parser)->location;
2145 c_parser_consume_token (parser);
2146 attrs = c_parser_attributes (parser);
2147 enum_loc = c_parser_peek_token (parser)->location;
2148 /* Set the location in case we create a decl now. */
2149 c_parser_set_source_position_from_token (c_parser_peek_token (parser));
2150 if (c_parser_next_token_is (parser, CPP_NAME))
2152 ident = c_parser_peek_token (parser)->value;
2153 ident_loc = c_parser_peek_token (parser)->location;
2154 enum_loc = ident_loc;
2155 c_parser_consume_token (parser);
2157 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
2159 /* Parse an enum definition. */
2160 struct c_enum_contents the_enum;
2161 tree type = start_enum (enum_loc, &the_enum, ident);
2163 /* We chain the enumerators in reverse order, then put them in
2164 forward order at the end. */
2165 tree values = NULL_TREE;
2166 c_parser_consume_token (parser);
2174 location_t comma_loc = UNKNOWN_LOCATION; /* Quiet warning. */
2175 location_t decl_loc, value_loc;
2176 if (c_parser_next_token_is_not (parser, CPP_NAME))
2178 c_parser_error (parser, "expected identifier");
2179 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
2180 values = error_mark_node;
2183 token = c_parser_peek_token (parser);
2184 enum_id = token->value;
2185 /* Set the location in case we create a decl now. */
2186 c_parser_set_source_position_from_token (token);
2187 decl_loc = value_loc = token->location;
2188 c_parser_consume_token (parser);
2189 if (c_parser_next_token_is (parser, CPP_EQ))
2191 c_parser_consume_token (parser);
2192 value_loc = c_parser_peek_token (parser)->location;
2193 enum_value = c_parser_expr_no_commas (parser, NULL).value;
2196 enum_value = NULL_TREE;
2197 enum_decl = build_enumerator (decl_loc, value_loc,
2198 &the_enum, enum_id, enum_value);
2199 TREE_CHAIN (enum_decl) = values;
2202 if (c_parser_next_token_is (parser, CPP_COMMA))
2204 comma_loc = c_parser_peek_token (parser)->location;
2206 c_parser_consume_token (parser);
2208 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2210 if (seen_comma && !flag_isoc99)
2211 pedwarn (comma_loc, OPT_pedantic, "comma at end of enumerator list");
2212 c_parser_consume_token (parser);
2217 c_parser_error (parser, "expected %<,%> or %<}%>");
2218 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
2219 values = error_mark_node;
2223 postfix_attrs = c_parser_attributes (parser);
2224 ret.spec = finish_enum (type, nreverse (values),
2225 chainon (attrs, postfix_attrs));
2226 ret.kind = ctsk_tagdef;
2227 ret.expr = NULL_TREE;
2228 ret.expr_const_operands = true;
2233 c_parser_error (parser, "expected %<{%>");
2234 ret.spec = error_mark_node;
2235 ret.kind = ctsk_tagref;
2236 ret.expr = NULL_TREE;
2237 ret.expr_const_operands = true;
2240 ret = parser_xref_tag (ident_loc, ENUMERAL_TYPE, ident);
2241 /* In ISO C, enumerated types can be referred to only if already
2243 if (pedantic && !COMPLETE_TYPE_P (ret.spec))
2246 pedwarn (enum_loc, OPT_pedantic,
2247 "ISO C forbids forward references to %<enum%> types");
2252 /* Parse a struct or union specifier (C90 6.5.2.1, C99 6.7.2.1).
2254 struct-or-union-specifier:
2255 struct-or-union attributes[opt] identifier[opt]
2256 { struct-contents } attributes[opt]
2257 struct-or-union attributes[opt] identifier
2260 struct-declaration-list
2262 struct-declaration-list:
2263 struct-declaration ;
2264 struct-declaration-list struct-declaration ;
2271 struct-declaration-list struct-declaration
2273 struct-declaration-list:
2274 struct-declaration-list ;
2277 (Note that in the syntax here, unlike that in ISO C, the semicolons
2278 are included here rather than in struct-declaration, in order to
2279 describe the syntax with extra semicolons and missing semicolon at
2284 struct-declaration-list:
2285 @defs ( class-name )
2287 (Note this does not include a trailing semicolon, but can be
2288 followed by further declarations, and gets a pedwarn-if-pedantic
2289 when followed by a semicolon.) */
2291 static struct c_typespec
2292 c_parser_struct_or_union_specifier (c_parser *parser)
2294 struct c_typespec ret;
2296 tree ident = NULL_TREE;
2297 location_t struct_loc;
2298 location_t ident_loc = UNKNOWN_LOCATION;
2299 enum tree_code code;
2300 switch (c_parser_peek_token (parser)->keyword)
2311 struct_loc = c_parser_peek_token (parser)->location;
2312 c_parser_consume_token (parser);
2313 attrs = c_parser_attributes (parser);
2315 /* Set the location in case we create a decl now. */
2316 c_parser_set_source_position_from_token (c_parser_peek_token (parser));
2318 if (c_parser_next_token_is (parser, CPP_NAME))
2320 ident = c_parser_peek_token (parser)->value;
2321 ident_loc = c_parser_peek_token (parser)->location;
2322 struct_loc = ident_loc;
2323 c_parser_consume_token (parser);
2325 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
2327 /* Parse a struct or union definition. Start the scope of the
2328 tag before parsing components. */
2329 struct c_struct_parse_info *struct_info;
2330 tree type = start_struct (struct_loc, code, ident, &struct_info);
2332 /* We chain the components in reverse order, then put them in
2333 forward order at the end. Each struct-declaration may
2334 declare multiple components (comma-separated), so we must use
2335 chainon to join them, although when parsing each
2336 struct-declaration we can use TREE_CHAIN directly.
2338 The theory behind all this is that there will be more
2339 semicolon separated fields than comma separated fields, and
2340 so we'll be minimizing the number of node traversals required
2342 tree contents = NULL_TREE;
2343 c_parser_consume_token (parser);
2344 /* Handle the Objective-C @defs construct,
2345 e.g. foo(sizeof(struct{ @defs(ClassName) }));. */
2346 if (c_parser_next_token_is_keyword (parser, RID_AT_DEFS))
2349 gcc_assert (c_dialect_objc ());
2350 c_parser_consume_token (parser);
2351 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2353 if (c_parser_next_token_is (parser, CPP_NAME)
2354 && c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME)
2356 name = c_parser_peek_token (parser)->value;
2357 c_parser_consume_token (parser);
2361 c_parser_error (parser, "expected class name");
2362 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
2365 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2367 contents = nreverse (objc_get_class_ivars (name));
2370 /* Parse the struct-declarations and semicolons. Problems with
2371 semicolons are diagnosed here; empty structures are diagnosed
2376 /* Parse any stray semicolon. */
2377 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
2379 pedwarn (c_parser_peek_token (parser)->location, OPT_pedantic,
2380 "extra semicolon in struct or union specified");
2381 c_parser_consume_token (parser);
2384 /* Stop if at the end of the struct or union contents. */
2385 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2387 c_parser_consume_token (parser);
2390 /* Accept #pragmas at struct scope. */
2391 if (c_parser_next_token_is (parser, CPP_PRAGMA))
2393 c_parser_pragma (parser, pragma_external);
2396 /* Parse some comma-separated declarations, but not the
2397 trailing semicolon if any. */
2398 decls = c_parser_struct_declaration (parser);
2399 contents = chainon (decls, contents);
2400 /* If no semicolon follows, either we have a parse error or
2401 are at the end of the struct or union and should
2403 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
2404 c_parser_consume_token (parser);
2407 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2408 pedwarn (c_parser_peek_token (parser)->location, 0,
2409 "no semicolon at end of struct or union");
2410 else if (parser->error
2411 || !c_parser_next_token_starts_declspecs (parser))
2413 c_parser_error (parser, "expected %<;%>");
2414 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
2418 /* If we come here, we have already emitted an error
2419 for an expected `;', identifier or `(', and we also
2420 recovered already. Go on with the next field. */
2423 postfix_attrs = c_parser_attributes (parser);
2424 ret.spec = finish_struct (struct_loc, type, nreverse (contents),
2425 chainon (attrs, postfix_attrs), struct_info);
2426 ret.kind = ctsk_tagdef;
2427 ret.expr = NULL_TREE;
2428 ret.expr_const_operands = true;
2433 c_parser_error (parser, "expected %<{%>");
2434 ret.spec = error_mark_node;
2435 ret.kind = ctsk_tagref;
2436 ret.expr = NULL_TREE;
2437 ret.expr_const_operands = true;
2440 ret = parser_xref_tag (ident_loc, code, ident);
2444 /* Parse a struct-declaration (C90 6.5.2.1, C99 6.7.2.1), *without*
2445 the trailing semicolon.
2448 specifier-qualifier-list struct-declarator-list
2449 static_assert-declaration-no-semi
2451 specifier-qualifier-list:
2452 type-specifier specifier-qualifier-list[opt]
2453 type-qualifier specifier-qualifier-list[opt]
2454 attributes specifier-qualifier-list[opt]
2456 struct-declarator-list:
2458 struct-declarator-list , attributes[opt] struct-declarator
2461 declarator attributes[opt]
2462 declarator[opt] : constant-expression attributes[opt]
2467 __extension__ struct-declaration
2468 specifier-qualifier-list
2470 Unlike the ISO C syntax, semicolons are handled elsewhere. The use
2471 of attributes where shown is a GNU extension. In GNU C, we accept
2472 any expression without commas in the syntax (assignment
2473 expressions, not just conditional expressions); assignment
2474 expressions will be diagnosed as non-constant. */
2477 c_parser_struct_declaration (c_parser *parser)
2479 struct c_declspecs *specs;
2481 tree all_prefix_attrs;
2483 location_t decl_loc;
2484 if (c_parser_next_token_is_keyword (parser, RID_EXTENSION))
2488 ext = disable_extension_diagnostics ();
2489 c_parser_consume_token (parser);
2490 decl = c_parser_struct_declaration (parser);
2491 restore_extension_diagnostics (ext);
2494 if (c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT))
2496 c_parser_static_assert_declaration_no_semi (parser);
2499 specs = build_null_declspecs ();
2500 decl_loc = c_parser_peek_token (parser)->location;
2501 c_parser_declspecs (parser, specs, false, true, true);
2504 if (!specs->declspecs_seen_p)
2506 c_parser_error (parser, "expected specifier-qualifier-list");
2509 finish_declspecs (specs);
2510 if (c_parser_next_token_is (parser, CPP_SEMICOLON)
2511 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2514 if (specs->typespec_kind == ctsk_none)
2516 pedwarn (decl_loc, OPT_pedantic,
2517 "ISO C forbids member declarations with no members");
2518 shadow_tag_warned (specs, pedantic);
2523 /* Support for unnamed structs or unions as members of
2524 structs or unions (which is [a] useful and [b] supports
2528 ret = grokfield (c_parser_peek_token (parser)->location,
2529 build_id_declarator (NULL_TREE), specs,
2532 decl_attributes (&ret, attrs, 0);
2537 /* Provide better error recovery. Note that a type name here is valid,
2538 and will be treated as a field name. */
2539 if (specs->typespec_kind == ctsk_tagdef
2540 && TREE_CODE (specs->type) != ENUMERAL_TYPE
2541 && c_parser_next_token_starts_declspecs (parser)
2542 && !c_parser_next_token_is (parser, CPP_NAME))
2544 c_parser_error (parser, "expected %<;%>, identifier or %<(%>");
2545 parser->error = false;
2549 pending_xref_error ();
2550 prefix_attrs = specs->attrs;
2551 all_prefix_attrs = prefix_attrs;
2552 specs->attrs = NULL_TREE;
2556 /* Declaring one or more declarators or un-named bit-fields. */
2557 struct c_declarator *declarator;
2559 if (c_parser_next_token_is (parser, CPP_COLON))
2560 declarator = build_id_declarator (NULL_TREE);
2562 declarator = c_parser_declarator (parser,
2563 specs->typespec_kind != ctsk_none,
2564 C_DTR_NORMAL, &dummy);
2565 if (declarator == NULL)
2567 c_parser_skip_to_end_of_block_or_statement (parser);
2570 if (c_parser_next_token_is (parser, CPP_COLON)
2571 || c_parser_next_token_is (parser, CPP_COMMA)
2572 || c_parser_next_token_is (parser, CPP_SEMICOLON)
2573 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE)
2574 || c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2576 tree postfix_attrs = NULL_TREE;
2577 tree width = NULL_TREE;
2579 if (c_parser_next_token_is (parser, CPP_COLON))
2581 c_parser_consume_token (parser);
2582 width = c_parser_expr_no_commas (parser, NULL).value;
2584 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2585 postfix_attrs = c_parser_attributes (parser);
2586 d = grokfield (c_parser_peek_token (parser)->location,
2587 declarator, specs, width, &all_prefix_attrs);
2588 decl_attributes (&d, chainon (postfix_attrs,
2589 all_prefix_attrs), 0);
2590 DECL_CHAIN (d) = decls;
2592 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2593 all_prefix_attrs = chainon (c_parser_attributes (parser),
2596 all_prefix_attrs = prefix_attrs;
2597 if (c_parser_next_token_is (parser, CPP_COMMA))
2598 c_parser_consume_token (parser);
2599 else if (c_parser_next_token_is (parser, CPP_SEMICOLON)
2600 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2602 /* Semicolon consumed in caller. */
2607 c_parser_error (parser, "expected %<,%>, %<;%> or %<}%>");
2613 c_parser_error (parser,
2614 "expected %<:%>, %<,%>, %<;%>, %<}%> or "
2615 "%<__attribute__%>");
2622 /* Parse a typeof specifier (a GNU extension).
2625 typeof ( expression )
2626 typeof ( type-name )
2629 static struct c_typespec
2630 c_parser_typeof_specifier (c_parser *parser)
2632 struct c_typespec ret;
2633 ret.kind = ctsk_typeof;
2634 ret.spec = error_mark_node;
2635 ret.expr = NULL_TREE;
2636 ret.expr_const_operands = true;
2637 gcc_assert (c_parser_next_token_is_keyword (parser, RID_TYPEOF));
2638 c_parser_consume_token (parser);
2639 c_inhibit_evaluation_warnings++;
2641 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2643 c_inhibit_evaluation_warnings--;
2647 if (c_parser_next_token_starts_typename (parser))
2649 struct c_type_name *type = c_parser_type_name (parser);
2650 c_inhibit_evaluation_warnings--;
2654 ret.spec = groktypename (type, &ret.expr, &ret.expr_const_operands);
2655 pop_maybe_used (variably_modified_type_p (ret.spec, NULL_TREE));
2661 location_t here = c_parser_peek_token (parser)->location;
2662 struct c_expr expr = c_parser_expression (parser);
2663 c_inhibit_evaluation_warnings--;
2665 if (TREE_CODE (expr.value) == COMPONENT_REF
2666 && DECL_C_BIT_FIELD (TREE_OPERAND (expr.value, 1)))
2667 error_at (here, "%<typeof%> applied to a bit-field");
2668 mark_exp_read (expr.value);
2669 ret.spec = TREE_TYPE (expr.value);
2670 was_vm = variably_modified_type_p (ret.spec, NULL_TREE);
2671 /* This is returned with the type so that when the type is
2672 evaluated, this can be evaluated. */
2674 ret.expr = c_fully_fold (expr.value, false, &ret.expr_const_operands);
2675 pop_maybe_used (was_vm);
2677 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
2681 /* Parse a declarator, possibly an abstract declarator (C90 6.5.4,
2682 6.5.5, C99 6.7.5, 6.7.6). If TYPE_SEEN_P then a typedef name may
2683 be redeclared; otherwise it may not. KIND indicates which kind of
2684 declarator is wanted. Returns a valid declarator except in the
2685 case of a syntax error in which case NULL is returned. *SEEN_ID is
2686 set to true if an identifier being declared is seen; this is used
2687 to diagnose bad forms of abstract array declarators and to
2688 determine whether an identifier list is syntactically permitted.
2691 pointer[opt] direct-declarator
2695 ( attributes[opt] declarator )
2696 direct-declarator array-declarator
2697 direct-declarator ( parameter-type-list )
2698 direct-declarator ( identifier-list[opt] )
2701 * type-qualifier-list[opt]
2702 * type-qualifier-list[opt] pointer
2704 type-qualifier-list:
2707 type-qualifier-list type-qualifier
2708 type-qualifier-list attributes
2710 parameter-type-list:
2712 parameter-list , ...
2715 parameter-declaration
2716 parameter-list , parameter-declaration
2718 parameter-declaration:
2719 declaration-specifiers declarator attributes[opt]
2720 declaration-specifiers abstract-declarator[opt] attributes[opt]
2724 identifier-list , identifier
2726 abstract-declarator:
2728 pointer[opt] direct-abstract-declarator
2730 direct-abstract-declarator:
2731 ( attributes[opt] abstract-declarator )
2732 direct-abstract-declarator[opt] array-declarator
2733 direct-abstract-declarator[opt] ( parameter-type-list[opt] )
2738 direct-declarator ( parameter-forward-declarations
2739 parameter-type-list[opt] )
2741 direct-abstract-declarator:
2742 direct-abstract-declarator[opt] ( parameter-forward-declarations
2743 parameter-type-list[opt] )
2745 parameter-forward-declarations:
2747 parameter-forward-declarations parameter-list ;
2749 The uses of attributes shown above are GNU extensions.
2751 Some forms of array declarator are not included in C99 in the
2752 syntax for abstract declarators; these are disallowed elsewhere.
2753 This may be a defect (DR#289).
2755 This function also accepts an omitted abstract declarator as being
2756 an abstract declarator, although not part of the formal syntax. */
2758 static struct c_declarator *
2759 c_parser_declarator (c_parser *parser, bool type_seen_p, c_dtr_syn kind,
2762 /* Parse any initial pointer part. */
2763 if (c_parser_next_token_is (parser, CPP_MULT))
2765 struct c_declspecs *quals_attrs = build_null_declspecs ();
2766 struct c_declarator *inner;
2767 c_parser_consume_token (parser);
2768 c_parser_declspecs (parser, quals_attrs, false, false, true);
2769 inner = c_parser_declarator (parser, type_seen_p, kind, seen_id);
2773 return make_pointer_declarator (quals_attrs, inner);
2775 /* Now we have a direct declarator, direct abstract declarator or
2776 nothing (which counts as a direct abstract declarator here). */
2777 return c_parser_direct_declarator (parser, type_seen_p, kind, seen_id);
2780 /* Parse a direct declarator or direct abstract declarator; arguments
2781 as c_parser_declarator. */
2783 static struct c_declarator *
2784 c_parser_direct_declarator (c_parser *parser, bool type_seen_p, c_dtr_syn kind,
2787 /* The direct declarator must start with an identifier (possibly
2788 omitted) or a parenthesized declarator (possibly abstract). In
2789 an ordinary declarator, initial parentheses must start a
2790 parenthesized declarator. In an abstract declarator or parameter
2791 declarator, they could start a parenthesized declarator or a
2792 parameter list. To tell which, the open parenthesis and any
2793 following attributes must be read. If a declaration specifier
2794 follows, then it is a parameter list; if the specifier is a
2795 typedef name, there might be an ambiguity about redeclaring it,
2796 which is resolved in the direction of treating it as a typedef
2797 name. If a close parenthesis follows, it is also an empty
2798 parameter list, as the syntax does not permit empty abstract
2799 declarators. Otherwise, it is a parenthesized declarator (in
2800 which case the analysis may be repeated inside it, recursively).
2802 ??? There is an ambiguity in a parameter declaration "int
2803 (__attribute__((foo)) x)", where x is not a typedef name: it
2804 could be an abstract declarator for a function, or declare x with
2805 parentheses. The proper resolution of this ambiguity needs
2806 documenting. At present we follow an accident of the old
2807 parser's implementation, whereby the first parameter must have
2808 some declaration specifiers other than just attributes. Thus as
2809 a parameter declaration it is treated as a parenthesized
2810 parameter named x, and as an abstract declarator it is
2813 ??? Also following the old parser, attributes inside an empty
2814 parameter list are ignored, making it a list not yielding a
2815 prototype, rather than giving an error or making it have one
2816 parameter with implicit type int.
2818 ??? Also following the old parser, typedef names may be
2819 redeclared in declarators, but not Objective-C class names. */
2821 if (kind != C_DTR_ABSTRACT
2822 && c_parser_next_token_is (parser, CPP_NAME)
2824 && (c_parser_peek_token (parser)->id_kind == C_ID_TYPENAME
2825 || c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME))
2826 || c_parser_peek_token (parser)->id_kind == C_ID_ID))
2828 struct c_declarator *inner
2829 = build_id_declarator (c_parser_peek_token (parser)->value);
2831 inner->id_loc = c_parser_peek_token (parser)->location;
2832 c_parser_consume_token (parser);
2833 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
2836 if (kind != C_DTR_NORMAL
2837 && c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
2839 struct c_declarator *inner = build_id_declarator (NULL_TREE);
2840 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
2843 /* Either we are at the end of an abstract declarator, or we have
2846 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
2849 struct c_declarator *inner;
2850 c_parser_consume_token (parser);
2851 attrs = c_parser_attributes (parser);
2852 if (kind != C_DTR_NORMAL
2853 && (c_parser_next_token_starts_declspecs (parser)
2854 || c_parser_next_token_is (parser, CPP_CLOSE_PAREN)))
2856 struct c_arg_info *args
2857 = c_parser_parms_declarator (parser, kind == C_DTR_NORMAL,
2864 = build_function_declarator (args,
2865 build_id_declarator (NULL_TREE));
2866 return c_parser_direct_declarator_inner (parser, *seen_id,
2870 /* A parenthesized declarator. */
2871 inner = c_parser_declarator (parser, type_seen_p, kind, seen_id);
2872 if (inner != NULL && attrs != NULL)
2873 inner = build_attrs_declarator (attrs, inner);
2874 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2876 c_parser_consume_token (parser);
2880 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
2884 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2891 if (kind == C_DTR_NORMAL)
2893 c_parser_error (parser, "expected identifier or %<(%>");
2897 return build_id_declarator (NULL_TREE);
2901 /* Parse part of a direct declarator or direct abstract declarator,
2902 given that some (in INNER) has already been parsed; ID_PRESENT is
2903 true if an identifier is present, false for an abstract
2906 static struct c_declarator *
2907 c_parser_direct_declarator_inner (c_parser *parser, bool id_present,
2908 struct c_declarator *inner)
2910 /* Parse a sequence of array declarators and parameter lists. */
2911 if (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
2913 location_t brace_loc = c_parser_peek_token (parser)->location;
2914 struct c_declarator *declarator;
2915 struct c_declspecs *quals_attrs = build_null_declspecs ();
2919 c_parser_consume_token (parser);
2920 c_parser_declspecs (parser, quals_attrs, false, false, true);
2921 static_seen = c_parser_next_token_is_keyword (parser, RID_STATIC);
2923 c_parser_consume_token (parser);
2924 if (static_seen && !quals_attrs->declspecs_seen_p)
2925 c_parser_declspecs (parser, quals_attrs, false, false, true);
2926 if (!quals_attrs->declspecs_seen_p)
2928 /* If "static" is present, there must be an array dimension.
2929 Otherwise, there may be a dimension, "*", or no
2934 dimen = c_parser_expr_no_commas (parser, NULL).value;
2938 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
2943 else if (c_parser_next_token_is (parser, CPP_MULT))
2945 if (c_parser_peek_2nd_token (parser)->type == CPP_CLOSE_SQUARE)
2949 c_parser_consume_token (parser);
2954 dimen = c_parser_expr_no_commas (parser, NULL).value;
2960 dimen = c_parser_expr_no_commas (parser, NULL).value;
2963 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
2964 c_parser_consume_token (parser);
2967 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
2972 mark_exp_read (dimen);
2973 declarator = build_array_declarator (brace_loc, dimen, quals_attrs,
2974 static_seen, star_seen);
2975 if (declarator == NULL)
2977 inner = set_array_declarator_inner (declarator, inner);
2978 return c_parser_direct_declarator_inner (parser, id_present, inner);
2980 else if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
2983 struct c_arg_info *args;
2984 c_parser_consume_token (parser);
2985 attrs = c_parser_attributes (parser);
2986 args = c_parser_parms_declarator (parser, id_present, attrs);
2991 inner = build_function_declarator (args, inner);
2992 return c_parser_direct_declarator_inner (parser, id_present, inner);
2998 /* Parse a parameter list or identifier list, including the closing
2999 parenthesis but not the opening one. ATTRS are the attributes at
3000 the start of the list. ID_LIST_OK is true if an identifier list is
3001 acceptable; such a list must not have attributes at the start. */
3003 static struct c_arg_info *
3004 c_parser_parms_declarator (c_parser *parser, bool id_list_ok, tree attrs)
3007 declare_parm_level ();
3008 /* If the list starts with an identifier, it is an identifier list.
3009 Otherwise, it is either a prototype list or an empty list. */
3012 && c_parser_next_token_is (parser, CPP_NAME)
3013 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
3015 tree list = NULL_TREE, *nextp = &list;
3016 while (c_parser_next_token_is (parser, CPP_NAME)
3017 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
3019 *nextp = build_tree_list (NULL_TREE,
3020 c_parser_peek_token (parser)->value);
3021 nextp = & TREE_CHAIN (*nextp);
3022 c_parser_consume_token (parser);
3023 if (c_parser_next_token_is_not (parser, CPP_COMMA))
3025 c_parser_consume_token (parser);
3026 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3028 c_parser_error (parser, "expected identifier");
3032 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3034 struct c_arg_info *ret = build_arg_info ();
3036 c_parser_consume_token (parser);
3042 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3050 struct c_arg_info *ret = c_parser_parms_list_declarator (parser, attrs);
3056 /* Parse a parameter list (possibly empty), including the closing
3057 parenthesis but not the opening one. ATTRS are the attributes at
3058 the start of the list. */
3060 static struct c_arg_info *
3061 c_parser_parms_list_declarator (c_parser *parser, tree attrs)
3063 bool bad_parm = false;
3064 /* ??? Following the old parser, forward parameter declarations may
3065 use abstract declarators, and if no real parameter declarations
3066 follow the forward declarations then this is not diagnosed. Also
3067 note as above that attributes are ignored as the only contents of
3068 the parentheses, or as the only contents after forward
3070 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3072 struct c_arg_info *ret = build_arg_info ();
3073 c_parser_consume_token (parser);
3076 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
3078 struct c_arg_info *ret = build_arg_info ();
3079 /* Suppress -Wold-style-definition for this case. */
3080 ret->types = error_mark_node;
3081 error_at (c_parser_peek_token (parser)->location,
3082 "ISO C requires a named argument before %<...%>");
3083 c_parser_consume_token (parser);
3084 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3086 c_parser_consume_token (parser);
3091 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3096 /* Nonempty list of parameters, either terminated with semicolon
3097 (forward declarations; recurse) or with close parenthesis (normal
3098 function) or with ", ... )" (variadic function). */
3101 /* Parse a parameter. */
3102 struct c_parm *parm = c_parser_parameter_declaration (parser, attrs);
3107 push_parm_decl (parm);
3108 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
3111 c_parser_consume_token (parser);
3112 mark_forward_parm_decls ();
3113 new_attrs = c_parser_attributes (parser);
3114 return c_parser_parms_list_declarator (parser, new_attrs);
3116 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3118 c_parser_consume_token (parser);
3121 get_pending_sizes ();
3125 return get_parm_info (false);
3127 if (!c_parser_require (parser, CPP_COMMA,
3128 "expected %<;%>, %<,%> or %<)%>"))
3130 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
3131 get_pending_sizes ();
3134 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
3136 c_parser_consume_token (parser);
3137 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3139 c_parser_consume_token (parser);
3142 get_pending_sizes ();
3146 return get_parm_info (true);
3150 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3152 get_pending_sizes ();
3159 /* Parse a parameter declaration. ATTRS are the attributes at the
3160 start of the declaration if it is the first parameter. */
3162 static struct c_parm *
3163 c_parser_parameter_declaration (c_parser *parser, tree attrs)
3165 struct c_declspecs *specs;
3166 struct c_declarator *declarator;
3168 tree postfix_attrs = NULL_TREE;
3170 if (!c_parser_next_token_starts_declspecs (parser))
3172 c_token *token = c_parser_peek_token (parser);
3175 c_parser_set_source_position_from_token (token);
3176 if (token->type == CPP_NAME
3177 && c_parser_peek_2nd_token (parser)->type != CPP_COMMA
3178 && c_parser_peek_2nd_token (parser)->type != CPP_CLOSE_PAREN)
3180 error ("unknown type name %qE", token->value);
3181 parser->error = true;
3183 /* ??? In some Objective-C cases '...' isn't applicable so there
3184 should be a different message. */
3186 c_parser_error (parser,
3187 "expected declaration specifiers or %<...%>");
3188 c_parser_skip_to_end_of_parameter (parser);
3191 specs = build_null_declspecs ();
3194 declspecs_add_attrs (specs, attrs);
3197 c_parser_declspecs (parser, specs, true, true, true);
3198 finish_declspecs (specs);
3199 pending_xref_error ();
3200 prefix_attrs = specs->attrs;
3201 specs->attrs = NULL_TREE;
3202 declarator = c_parser_declarator (parser,
3203 specs->typespec_kind != ctsk_none,
3204 C_DTR_PARM, &dummy);
3205 if (declarator == NULL)
3207 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
3210 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
3211 postfix_attrs = c_parser_attributes (parser);
3212 return build_c_parm (specs, chainon (postfix_attrs, prefix_attrs),
3216 /* Parse a string literal in an asm expression. It should not be
3217 translated, and wide string literals are an error although
3218 permitted by the syntax. This is a GNU extension.
3223 ??? At present, following the old parser, the caller needs to have
3224 set lex_untranslated_string to 1. It would be better to follow the
3225 C++ parser rather than using this kludge. */
3228 c_parser_asm_string_literal (c_parser *parser)
3231 if (c_parser_next_token_is (parser, CPP_STRING))
3233 str = c_parser_peek_token (parser)->value;
3234 c_parser_consume_token (parser);
3236 else if (c_parser_next_token_is (parser, CPP_WSTRING))
3238 error_at (c_parser_peek_token (parser)->location,
3239 "wide string literal in %<asm%>");
3240 str = build_string (1, "");
3241 c_parser_consume_token (parser);
3245 c_parser_error (parser, "expected string literal");
3251 /* Parse a simple asm expression. This is used in restricted
3252 contexts, where a full expression with inputs and outputs does not
3253 make sense. This is a GNU extension.
3256 asm ( asm-string-literal )
3260 c_parser_simple_asm_expr (c_parser *parser)
3263 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ASM));
3264 /* ??? Follow the C++ parser rather than using the
3265 lex_untranslated_string kludge. */
3266 parser->lex_untranslated_string = true;
3267 c_parser_consume_token (parser);
3268 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3270 parser->lex_untranslated_string = false;
3273 str = c_parser_asm_string_literal (parser);
3274 parser->lex_untranslated_string = false;
3275 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
3277 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
3283 /* Parse (possibly empty) attributes. This is a GNU extension.
3287 attributes attribute
3290 __attribute__ ( ( attribute-list ) )
3294 attribute_list , attrib
3299 any-word ( identifier )
3300 any-word ( identifier , nonempty-expr-list )
3301 any-word ( expr-list )
3303 where the "identifier" must not be declared as a type, and
3304 "any-word" may be any identifier (including one declared as a
3305 type), a reserved word storage class specifier, type specifier or
3306 type qualifier. ??? This still leaves out most reserved keywords
3307 (following the old parser), shouldn't we include them, and why not
3308 allow identifiers declared as types to start the arguments? */
3311 c_parser_attributes (c_parser *parser)
3313 tree attrs = NULL_TREE;
3314 while (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
3316 /* ??? Follow the C++ parser rather than using the
3317 lex_untranslated_string kludge. */
3318 parser->lex_untranslated_string = true;
3319 c_parser_consume_token (parser);
3320 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3322 parser->lex_untranslated_string = false;
3325 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3327 parser->lex_untranslated_string = false;
3328 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
3331 /* Parse the attribute list. */
3332 while (c_parser_next_token_is (parser, CPP_COMMA)
3333 || c_parser_next_token_is (parser, CPP_NAME)
3334 || c_parser_next_token_is (parser, CPP_KEYWORD))
3336 tree attr, attr_name, attr_args;
3337 VEC(tree,gc) *expr_list;
3338 if (c_parser_next_token_is (parser, CPP_COMMA))
3340 c_parser_consume_token (parser);
3343 if (c_parser_next_token_is (parser, CPP_KEYWORD))
3345 /* ??? See comment above about what keywords are
3348 switch (c_parser_peek_token (parser)->keyword)
3386 /* Accept __attribute__((__const)) as __attribute__((const))
3389 = ridpointers[(int) c_parser_peek_token (parser)->keyword];
3392 attr_name = c_parser_peek_token (parser)->value;
3393 c_parser_consume_token (parser);
3394 if (c_parser_next_token_is_not (parser, CPP_OPEN_PAREN))
3396 attr = build_tree_list (attr_name, NULL_TREE);
3397 attrs = chainon (attrs, attr);
3400 c_parser_consume_token (parser);
3401 /* Parse the attribute contents. If they start with an
3402 identifier which is followed by a comma or close
3403 parenthesis, then the arguments start with that
3404 identifier; otherwise they are an expression list.
3405 In objective-c the identifier may be a classname. */
3406 if (c_parser_next_token_is (parser, CPP_NAME)
3407 && (c_parser_peek_token (parser)->id_kind == C_ID_ID
3408 || (c_dialect_objc ()
3409 && c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME))
3410 && ((c_parser_peek_2nd_token (parser)->type == CPP_COMMA)
3411 || (c_parser_peek_2nd_token (parser)->type
3412 == CPP_CLOSE_PAREN)))
3414 tree arg1 = c_parser_peek_token (parser)->value;
3415 c_parser_consume_token (parser);
3416 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3417 attr_args = build_tree_list (NULL_TREE, arg1);
3421 c_parser_consume_token (parser);
3422 expr_list = c_parser_expr_list (parser, false, true, NULL);
3423 tree_list = build_tree_list_vec (expr_list);
3424 attr_args = tree_cons (NULL_TREE, arg1, tree_list);
3425 release_tree_vector (expr_list);
3430 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3431 attr_args = NULL_TREE;
3434 expr_list = c_parser_expr_list (parser, false, true, NULL);
3435 attr_args = build_tree_list_vec (expr_list);
3436 release_tree_vector (expr_list);
3439 attr = build_tree_list (attr_name, attr_args);
3440 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3441 c_parser_consume_token (parser);
3444 parser->lex_untranslated_string = false;
3445 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3449 attrs = chainon (attrs, attr);
3451 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3452 c_parser_consume_token (parser);
3455 parser->lex_untranslated_string = false;
3456 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3460 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3461 c_parser_consume_token (parser);
3464 parser->lex_untranslated_string = false;
3465 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3469 parser->lex_untranslated_string = false;
3474 /* Parse a type name (C90 6.5.5, C99 6.7.6).
3477 specifier-qualifier-list abstract-declarator[opt]
3480 static struct c_type_name *
3481 c_parser_type_name (c_parser *parser)
3483 struct c_declspecs *specs = build_null_declspecs ();
3484 struct c_declarator *declarator;
3485 struct c_type_name *ret;
3487 c_parser_declspecs (parser, specs, false, true, true);
3488 if (!specs->declspecs_seen_p)
3490 c_parser_error (parser, "expected specifier-qualifier-list");
3493 pending_xref_error ();
3494 finish_declspecs (specs);
3495 declarator = c_parser_declarator (parser,
3496 specs->typespec_kind != ctsk_none,
3497 C_DTR_ABSTRACT, &dummy);
3498 if (declarator == NULL)
3500 ret = XOBNEW (&parser_obstack, struct c_type_name);
3502 ret->declarator = declarator;
3506 /* Parse an initializer (C90 6.5.7, C99 6.7.8).
3509 assignment-expression
3510 { initializer-list }
3511 { initializer-list , }
3514 designation[opt] initializer
3515 initializer-list , designation[opt] initializer
3522 designator-list designator
3529 [ constant-expression ]
3541 [ constant-expression ... constant-expression ]
3543 Any expression without commas is accepted in the syntax for the
3544 constant-expressions, with non-constant expressions rejected later.
3546 This function is only used for top-level initializers; for nested
3547 ones, see c_parser_initval. */
3549 static struct c_expr
3550 c_parser_initializer (c_parser *parser)
3552 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
3553 return c_parser_braced_init (parser, NULL_TREE, false);
3557 location_t loc = c_parser_peek_token (parser)->location;
3558 ret = c_parser_expr_no_commas (parser, NULL);
3559 if (TREE_CODE (ret.value) != STRING_CST
3560 && TREE_CODE (ret.value) != COMPOUND_LITERAL_EXPR)
3561 ret = default_function_array_read_conversion (loc, ret);
3566 /* Parse a braced initializer list. TYPE is the type specified for a
3567 compound literal, and NULL_TREE for other initializers and for
3568 nested braced lists. NESTED_P is true for nested braced lists,
3569 false for the list of a compound literal or the list that is the
3570 top-level initializer in a declaration. */
3572 static struct c_expr
3573 c_parser_braced_init (c_parser *parser, tree type, bool nested_p)
3576 struct obstack braced_init_obstack;
3577 location_t brace_loc = c_parser_peek_token (parser)->location;
3578 gcc_obstack_init (&braced_init_obstack);
3579 gcc_assert (c_parser_next_token_is (parser, CPP_OPEN_BRACE));
3580 c_parser_consume_token (parser);
3582 push_init_level (0, &braced_init_obstack);
3584 really_start_incremental_init (type);
3585 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3587 pedwarn (brace_loc, OPT_pedantic, "ISO C forbids empty initializer braces");
3591 /* Parse a non-empty initializer list, possibly with a trailing
3595 c_parser_initelt (parser, &braced_init_obstack);
3598 if (c_parser_next_token_is (parser, CPP_COMMA))
3599 c_parser_consume_token (parser);
3602 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3606 if (c_parser_next_token_is_not (parser, CPP_CLOSE_BRACE))
3608 ret.value = error_mark_node;
3609 ret.original_code = ERROR_MARK;
3610 ret.original_type = NULL;
3611 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, "expected %<}%>");
3612 pop_init_level (0, &braced_init_obstack);
3613 obstack_free (&braced_init_obstack, NULL);
3616 c_parser_consume_token (parser);
3617 ret = pop_init_level (0, &braced_init_obstack);
3618 obstack_free (&braced_init_obstack, NULL);
3622 /* Parse a nested initializer, including designators. */
3625 c_parser_initelt (c_parser *parser, struct obstack * braced_init_obstack)
3627 /* Parse any designator or designator list. A single array
3628 designator may have the subsequent "=" omitted in GNU C, but a
3629 longer list or a structure member designator may not. */
3630 if (c_parser_next_token_is (parser, CPP_NAME)
3631 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
3633 /* Old-style structure member designator. */
3634 set_init_label (c_parser_peek_token (parser)->value,
3635 braced_init_obstack);
3636 /* Use the colon as the error location. */
3637 pedwarn (c_parser_peek_2nd_token (parser)->location, OPT_pedantic,
3638 "obsolete use of designated initializer with %<:%>");
3639 c_parser_consume_token (parser);
3640 c_parser_consume_token (parser);
3644 /* des_seen is 0 if there have been no designators, 1 if there
3645 has been a single array designator and 2 otherwise. */
3647 /* Location of a designator. */
3648 location_t des_loc = UNKNOWN_LOCATION; /* Quiet warning. */
3649 while (c_parser_next_token_is (parser, CPP_OPEN_SQUARE)
3650 || c_parser_next_token_is (parser, CPP_DOT))
3652 int des_prev = des_seen;
3654 des_loc = c_parser_peek_token (parser)->location;
3657 if (c_parser_next_token_is (parser, CPP_DOT))
3660 c_parser_consume_token (parser);
3661 if (c_parser_next_token_is (parser, CPP_NAME))
3663 set_init_label (c_parser_peek_token (parser)->value,
3664 braced_init_obstack);
3665 c_parser_consume_token (parser);
3670 init.value = error_mark_node;
3671 init.original_code = ERROR_MARK;
3672 init.original_type = NULL;
3673 c_parser_error (parser, "expected identifier");
3674 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
3675 process_init_element (init, false, braced_init_obstack);
3682 location_t ellipsis_loc = UNKNOWN_LOCATION; /* Quiet warning. */
3683 /* ??? Following the old parser, [ objc-receiver
3684 objc-message-args ] is accepted as an initializer,
3685 being distinguished from a designator by what follows
3686 the first assignment expression inside the square
3687 brackets, but after a first array designator a
3688 subsequent square bracket is for Objective-C taken to
3689 start an expression, using the obsolete form of
3690 designated initializer without '=', rather than
3691 possibly being a second level of designation: in LALR
3692 terms, the '[' is shifted rather than reducing
3693 designator to designator-list. */
3694 if (des_prev == 1 && c_dialect_objc ())
3696 des_seen = des_prev;
3699 if (des_prev == 0 && c_dialect_objc ())
3701 /* This might be an array designator or an
3702 Objective-C message expression. If the former,
3703 continue parsing here; if the latter, parse the
3704 remainder of the initializer given the starting
3705 primary-expression. ??? It might make sense to
3706 distinguish when des_prev == 1 as well; see
3707 previous comment. */
3709 struct c_expr mexpr;
3710 c_parser_consume_token (parser);
3711 if (c_parser_peek_token (parser)->type == CPP_NAME
3712 && ((c_parser_peek_token (parser)->id_kind
3714 || (c_parser_peek_token (parser)->id_kind
3715 == C_ID_CLASSNAME)))
3717 /* Type name receiver. */
3718 tree id = c_parser_peek_token (parser)->value;
3719 c_parser_consume_token (parser);
3720 rec = objc_get_class_reference (id);
3721 goto parse_message_args;
3723 first = c_parser_expr_no_commas (parser, NULL).value;
3724 mark_exp_read (first);
3725 if (c_parser_next_token_is (parser, CPP_ELLIPSIS)
3726 || c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
3727 goto array_desig_after_first;
3728 /* Expression receiver. So far only one part
3729 without commas has been parsed; there might be
3730 more of the expression. */
3732 while (c_parser_next_token_is (parser, CPP_COMMA))
3735 location_t comma_loc, exp_loc;
3736 comma_loc = c_parser_peek_token (parser)->location;
3737 c_parser_consume_token (parser);
3738 exp_loc = c_parser_peek_token (parser)->location;
3739 next = c_parser_expr_no_commas (parser, NULL);
3740 next = default_function_array_read_conversion (exp_loc,
3742 rec = build_compound_expr (comma_loc, rec, next.value);
3745 /* Now parse the objc-message-args. */
3746 args = c_parser_objc_message_args (parser);
3747 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
3750 = objc_build_message_expr (build_tree_list (rec, args));
3751 mexpr.original_code = ERROR_MARK;
3752 mexpr.original_type = NULL;
3753 /* Now parse and process the remainder of the
3754 initializer, starting with this message
3755 expression as a primary-expression. */
3756 c_parser_initval (parser, &mexpr, braced_init_obstack);
3759 c_parser_consume_token (parser);
3760 first = c_parser_expr_no_commas (parser, NULL).value;
3761 mark_exp_read (first);
3762 array_desig_after_first:
3763 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
3765 ellipsis_loc = c_parser_peek_token (parser)->location;
3766 c_parser_consume_token (parser);
3767 second = c_parser_expr_no_commas (parser, NULL).value;
3768 mark_exp_read (second);
3772 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
3774 c_parser_consume_token (parser);
3775 set_init_index (first, second, braced_init_obstack);
3777 pedwarn (ellipsis_loc, OPT_pedantic,
3778 "ISO C forbids specifying range of elements to initialize");
3781 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
3787 if (c_parser_next_token_is (parser, CPP_EQ))
3790 pedwarn (des_loc, OPT_pedantic,
3791 "ISO C90 forbids specifying subobject to initialize");
3792 c_parser_consume_token (parser);
3797 pedwarn (c_parser_peek_token (parser)->location, OPT_pedantic,
3798 "obsolete use of designated initializer without %<=%>");
3802 init.value = error_mark_node;
3803 init.original_code = ERROR_MARK;
3804 init.original_type = NULL;
3805 c_parser_error (parser, "expected %<=%>");
3806 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
3807 process_init_element (init, false, braced_init_obstack);
3813 c_parser_initval (parser, NULL, braced_init_obstack);
3816 /* Parse a nested initializer; as c_parser_initializer but parses
3817 initializers within braced lists, after any designators have been
3818 applied. If AFTER is not NULL then it is an Objective-C message
3819 expression which is the primary-expression starting the
3823 c_parser_initval (c_parser *parser, struct c_expr *after,
3824 struct obstack * braced_init_obstack)
3827 gcc_assert (!after || c_dialect_objc ());
3828 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE) && !after)
3829 init = c_parser_braced_init (parser, NULL_TREE, true);
3832 location_t loc = c_parser_peek_token (parser)->location;
3833 init = c_parser_expr_no_commas (parser, after);
3834 if (init.value != NULL_TREE
3835 && TREE_CODE (init.value) != STRING_CST
3836 && TREE_CODE (init.value) != COMPOUND_LITERAL_EXPR)
3837 init = default_function_array_read_conversion (loc, init);
3839 process_init_element (init, false, braced_init_obstack);
3842 /* Parse a compound statement (possibly a function body) (C90 6.6.2,
3846 { block-item-list[opt] }
3847 { label-declarations block-item-list }
3851 block-item-list block-item
3863 { label-declarations block-item-list }
3866 __extension__ nested-declaration
3867 nested-function-definition
3871 label-declarations label-declaration
3874 __label__ identifier-list ;
3876 Allowing the mixing of declarations and code is new in C99. The
3877 GNU syntax also permits (not shown above) labels at the end of
3878 compound statements, which yield an error. We don't allow labels
3879 on declarations; this might seem like a natural extension, but
3880 there would be a conflict between attributes on the label and
3881 prefix attributes on the declaration. ??? The syntax follows the
3882 old parser in requiring something after label declarations.
3883 Although they are erroneous if the labels declared aren't defined,
3884 is it useful for the syntax to be this way?
3896 c_parser_compound_statement (c_parser *parser)
3899 location_t brace_loc;
3900 brace_loc = c_parser_peek_token (parser)->location;
3901 if (!c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
3903 /* Ensure a scope is entered and left anyway to avoid confusion
3904 if we have just prepared to enter a function body. */
3905 stmt = c_begin_compound_stmt (true);
3906 c_end_compound_stmt (brace_loc, stmt, true);
3907 return error_mark_node;
3909 stmt = c_begin_compound_stmt (true);
3910 c_parser_compound_statement_nostart (parser);
3911 return c_end_compound_stmt (brace_loc, stmt, true);
3914 /* Parse a compound statement except for the opening brace. This is
3915 used for parsing both compound statements and statement expressions
3916 (which follow different paths to handling the opening). */
3919 c_parser_compound_statement_nostart (c_parser *parser)
3921 bool last_stmt = false;
3922 bool last_label = false;
3923 bool save_valid_for_pragma = valid_location_for_stdc_pragma_p ();
3924 location_t label_loc = UNKNOWN_LOCATION; /* Quiet warning. */
3925 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3927 c_parser_consume_token (parser);
3930 mark_valid_location_for_stdc_pragma (true);
3931 if (c_parser_next_token_is_keyword (parser, RID_LABEL))
3933 /* Read zero or more forward-declarations for labels that nested
3934 functions can jump to. */
3935 mark_valid_location_for_stdc_pragma (false);
3936 while (c_parser_next_token_is_keyword (parser, RID_LABEL))
3938 label_loc = c_parser_peek_token (parser)->location;
3939 c_parser_consume_token (parser);
3940 /* Any identifiers, including those declared as type names,
3945 if (c_parser_next_token_is_not (parser, CPP_NAME))
3947 c_parser_error (parser, "expected identifier");
3951 = declare_label (c_parser_peek_token (parser)->value);
3952 C_DECLARED_LABEL_FLAG (label) = 1;
3953 add_stmt (build_stmt (label_loc, DECL_EXPR, label));
3954 c_parser_consume_token (parser);
3955 if (c_parser_next_token_is (parser, CPP_COMMA))
3956 c_parser_consume_token (parser);
3960 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
3962 pedwarn (label_loc, OPT_pedantic, "ISO C forbids label declarations");
3964 /* We must now have at least one statement, label or declaration. */
3965 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3967 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
3968 c_parser_error (parser, "expected declaration or statement");
3969 c_parser_consume_token (parser);
3972 while (c_parser_next_token_is_not (parser, CPP_CLOSE_BRACE))
3974 location_t loc = c_parser_peek_token (parser)->location;
3975 if (c_parser_next_token_is_keyword (parser, RID_CASE)
3976 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
3977 || (c_parser_next_token_is (parser, CPP_NAME)
3978 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
3980 if (c_parser_next_token_is_keyword (parser, RID_CASE))
3981 label_loc = c_parser_peek_2nd_token (parser)->location;
3983 label_loc = c_parser_peek_token (parser)->location;
3986 mark_valid_location_for_stdc_pragma (false);
3987 c_parser_label (parser);
3989 else if (!last_label
3990 && c_parser_next_tokens_start_declaration (parser))
3993 mark_valid_location_for_stdc_pragma (false);
3994 c_parser_declaration_or_fndef (parser, true, true, true, true, true, NULL);
3997 (pedantic && !flag_isoc99)
3999 : OPT_Wdeclaration_after_statement,
4000 "ISO C90 forbids mixed declarations and code");
4003 else if (!last_label
4004 && c_parser_next_token_is_keyword (parser, RID_EXTENSION))
4006 /* __extension__ can start a declaration, but is also an
4007 unary operator that can start an expression. Consume all
4008 but the last of a possible series of __extension__ to
4010 while (c_parser_peek_2nd_token (parser)->type == CPP_KEYWORD
4011 && (c_parser_peek_2nd_token (parser)->keyword
4013 c_parser_consume_token (parser);
4014 if (c_token_starts_declaration (c_parser_peek_2nd_token (parser)))
4017 ext = disable_extension_diagnostics ();
4018 c_parser_consume_token (parser);
4020 mark_valid_location_for_stdc_pragma (false);
4021 c_parser_declaration_or_fndef (parser, true, true, true, true,
4023 /* Following the old parser, __extension__ does not
4024 disable this diagnostic. */
4025 restore_extension_diagnostics (ext);
4027 pedwarn_c90 (loc, (pedantic && !flag_isoc99)
4029 : OPT_Wdeclaration_after_statement,
4030 "ISO C90 forbids mixed declarations and code");
4036 else if (c_parser_next_token_is (parser, CPP_PRAGMA))
4038 /* External pragmas, and some omp pragmas, are not associated
4039 with regular c code, and so are not to be considered statements
4040 syntactically. This ensures that the user doesn't put them
4041 places that would turn into syntax errors if the directive
4043 if (c_parser_pragma (parser, pragma_compound))
4044 last_label = false, last_stmt = true;
4046 else if (c_parser_next_token_is (parser, CPP_EOF))
4048 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
4049 c_parser_error (parser, "expected declaration or statement");
4052 else if (c_parser_next_token_is_keyword (parser, RID_ELSE))
4054 if (parser->in_if_block)
4056 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
4057 error_at (loc, """expected %<}%> before %<else%>");
4062 error_at (loc, "%<else%> without a previous %<if%>");
4063 c_parser_consume_token (parser);
4072 mark_valid_location_for_stdc_pragma (false);
4073 c_parser_statement_after_labels (parser);
4076 parser->error = false;
4079 error_at (label_loc, "label at end of compound statement");
4080 c_parser_consume_token (parser);
4081 /* Restore the value we started with. */
4082 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
4085 /* Parse a label (C90 6.6.1, C99 6.8.1).
4088 identifier : attributes[opt]
4089 case constant-expression :
4095 case constant-expression ... constant-expression :
4097 The use of attributes on labels is a GNU extension. The syntax in
4098 GNU C accepts any expressions without commas, non-constant
4099 expressions being rejected later. */
4102 c_parser_label (c_parser *parser)
4104 location_t loc1 = c_parser_peek_token (parser)->location;
4105 tree label = NULL_TREE;
4106 if (c_parser_next_token_is_keyword (parser, RID_CASE))
4109 c_parser_consume_token (parser);
4110 exp1 = c_parser_expr_no_commas (parser, NULL).value;
4111 if (c_parser_next_token_is (parser, CPP_COLON))
4113 c_parser_consume_token (parser);
4114 label = do_case (loc1, exp1, NULL_TREE);
4116 else if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
4118 c_parser_consume_token (parser);
4119 exp2 = c_parser_expr_no_commas (parser, NULL).value;
4120 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
4121 label = do_case (loc1, exp1, exp2);
4124 c_parser_error (parser, "expected %<:%> or %<...%>");
4126 else if (c_parser_next_token_is_keyword (parser, RID_DEFAULT))
4128 c_parser_consume_token (parser);
4129 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
4130 label = do_case (loc1, NULL_TREE, NULL_TREE);
4134 tree name = c_parser_peek_token (parser)->value;
4137 location_t loc2 = c_parser_peek_token (parser)->location;
4138 gcc_assert (c_parser_next_token_is (parser, CPP_NAME));
4139 c_parser_consume_token (parser);
4140 gcc_assert (c_parser_next_token_is (parser, CPP_COLON));
4141 c_parser_consume_token (parser);
4142 attrs = c_parser_attributes (parser);
4143 tlab = define_label (loc2, name);
4146 decl_attributes (&tlab, attrs, 0);
4147 label = add_stmt (build_stmt (loc1, LABEL_EXPR, tlab));
4152 if (c_parser_next_tokens_start_declaration (parser))
4154 error_at (c_parser_peek_token (parser)->location,
4155 "a label can only be part of a statement and "
4156 "a declaration is not a statement");
4157 c_parser_declaration_or_fndef (parser, /*fndef_ok*/ false,
4158 /*static_assert_ok*/ true,
4159 /*nested*/ true, /*empty_ok*/ false,
4160 /*start_attr_ok*/ true, NULL);
4165 /* Parse a statement (C90 6.6, C99 6.8).
4170 expression-statement
4178 expression-statement:
4181 selection-statement:
4185 iteration-statement:
4194 return expression[opt] ;
4207 objc-throw-statement
4208 objc-try-catch-statement
4209 objc-synchronized-statement
4211 objc-throw-statement:
4225 parallel-for-construct
4226 parallel-sections-construct
4233 parallel-directive structured-block
4236 for-directive iteration-statement
4239 sections-directive section-scope
4242 single-directive structured-block
4244 parallel-for-construct:
4245 parallel-for-directive iteration-statement
4247 parallel-sections-construct:
4248 parallel-sections-directive section-scope
4251 master-directive structured-block
4254 critical-directive structured-block
4257 atomic-directive expression-statement
4260 ordered-directive structured-block */
4263 c_parser_statement (c_parser *parser)
4265 while (c_parser_next_token_is_keyword (parser, RID_CASE)
4266 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
4267 || (c_parser_next_token_is (parser, CPP_NAME)
4268 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
4269 c_parser_label (parser);
4270 c_parser_statement_after_labels (parser);
4273 /* Parse a statement, other than a labeled statement. */
4276 c_parser_statement_after_labels (c_parser *parser)
4278 location_t loc = c_parser_peek_token (parser)->location;
4279 tree stmt = NULL_TREE;
4280 bool in_if_block = parser->in_if_block;
4281 parser->in_if_block = false;
4282 switch (c_parser_peek_token (parser)->type)
4284 case CPP_OPEN_BRACE:
4285 add_stmt (c_parser_compound_statement (parser));
4288 switch (c_parser_peek_token (parser)->keyword)
4291 c_parser_if_statement (parser);
4294 c_parser_switch_statement (parser);
4297 c_parser_while_statement (parser);
4300 c_parser_do_statement (parser);
4303 c_parser_for_statement (parser);
4306 c_parser_consume_token (parser);
4307 if (c_parser_next_token_is (parser, CPP_NAME))
4309 stmt = c_finish_goto_label (loc,
4310 c_parser_peek_token (parser)->value);
4311 c_parser_consume_token (parser);
4313 else if (c_parser_next_token_is (parser, CPP_MULT))
4317 c_parser_consume_token (parser);
4318 val = c_parser_expression (parser).value;
4319 mark_exp_read (val);
4320 stmt = c_finish_goto_ptr (loc, val);
4323 c_parser_error (parser, "expected identifier or %<*%>");
4324 goto expect_semicolon;
4326 c_parser_consume_token (parser);
4327 stmt = c_finish_bc_stmt (loc, &c_cont_label, false);
4328 goto expect_semicolon;
4330 c_parser_consume_token (parser);
4331 stmt = c_finish_bc_stmt (loc, &c_break_label, true);
4332 goto expect_semicolon;
4334 c_parser_consume_token (parser);
4335 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
4337 stmt = c_finish_return (loc, NULL_TREE, NULL_TREE);
4338 c_parser_consume_token (parser);
4342 struct c_expr expr = c_parser_expression_conv (parser);
4343 mark_exp_read (expr.value);
4344 stmt = c_finish_return (loc, expr.value, expr.original_type);
4345 goto expect_semicolon;
4349 stmt = c_parser_asm_statement (parser);
4352 gcc_assert (c_dialect_objc ());
4353 c_parser_consume_token (parser);
4354 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
4356 stmt = objc_build_throw_stmt (loc, NULL_TREE);
4357 c_parser_consume_token (parser);
4361 tree expr = c_parser_expression (parser).value;
4362 expr = c_fully_fold (expr, false, NULL);
4363 stmt = objc_build_throw_stmt (loc, expr);
4364 goto expect_semicolon;
4368 gcc_assert (c_dialect_objc ());
4369 c_parser_objc_try_catch_finally_statement (parser);
4371 case RID_AT_SYNCHRONIZED:
4372 gcc_assert (c_dialect_objc ());
4373 c_parser_objc_synchronized_statement (parser);
4380 c_parser_consume_token (parser);
4382 case CPP_CLOSE_PAREN:
4383 case CPP_CLOSE_SQUARE:
4384 /* Avoid infinite loop in error recovery:
4385 c_parser_skip_until_found stops at a closing nesting
4386 delimiter without consuming it, but here we need to consume
4387 it to proceed further. */
4388 c_parser_error (parser, "expected statement");
4389 c_parser_consume_token (parser);
4392 c_parser_pragma (parser, pragma_stmt);
4396 stmt = c_finish_expr_stmt (loc, c_parser_expression_conv (parser).value);
4398 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
4401 /* Two cases cannot and do not have line numbers associated: If stmt
4402 is degenerate, such as "2;", then stmt is an INTEGER_CST, which
4403 cannot hold line numbers. But that's OK because the statement
4404 will either be changed to a MODIFY_EXPR during gimplification of
4405 the statement expr, or discarded. If stmt was compound, but
4406 without new variables, we will have skipped the creation of a
4407 BIND and will have a bare STATEMENT_LIST. But that's OK because
4408 (recursively) all of the component statements should already have
4409 line numbers assigned. ??? Can we discard no-op statements
4411 if (CAN_HAVE_LOCATION_P (stmt)
4412 && EXPR_LOCATION (stmt) == UNKNOWN_LOCATION)
4413 SET_EXPR_LOCATION (stmt, loc);
4415 parser->in_if_block = in_if_block;
4418 /* Parse the condition from an if, do, while or for statements. */
4421 c_parser_condition (c_parser *parser)
4423 location_t loc = c_parser_peek_token (parser)->location;
4425 cond = c_parser_expression_conv (parser).value;
4426 cond = c_objc_common_truthvalue_conversion (loc, cond);
4427 cond = c_fully_fold (cond, false, NULL);
4428 if (warn_sequence_point)
4429 verify_sequence_points (cond);
4433 /* Parse a parenthesized condition from an if, do or while statement.
4439 c_parser_paren_condition (c_parser *parser)
4442 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4443 return error_mark_node;
4444 cond = c_parser_condition (parser);
4445 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
4449 /* Parse a statement which is a block in C99. */
4452 c_parser_c99_block_statement (c_parser *parser)
4454 tree block = c_begin_compound_stmt (flag_isoc99);
4455 location_t loc = c_parser_peek_token (parser)->location;
4456 c_parser_statement (parser);
4457 return c_end_compound_stmt (loc, block, flag_isoc99);
4460 /* Parse the body of an if statement. This is just parsing a
4461 statement but (a) it is a block in C99, (b) we track whether the
4462 body is an if statement for the sake of -Wparentheses warnings, (c)
4463 we handle an empty body specially for the sake of -Wempty-body
4464 warnings, and (d) we call parser_compound_statement directly
4465 because c_parser_statement_after_labels resets
4466 parser->in_if_block. */
4469 c_parser_if_body (c_parser *parser, bool *if_p)
4471 tree block = c_begin_compound_stmt (flag_isoc99);
4472 location_t body_loc = c_parser_peek_token (parser)->location;
4473 while (c_parser_next_token_is_keyword (parser, RID_CASE)
4474 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
4475 || (c_parser_next_token_is (parser, CPP_NAME)
4476 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
4477 c_parser_label (parser);
4478 *if_p = c_parser_next_token_is_keyword (parser, RID_IF);
4479 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
4481 location_t loc = c_parser_peek_token (parser)->location;
4482 add_stmt (build_empty_stmt (loc));
4483 c_parser_consume_token (parser);
4484 if (!c_parser_next_token_is_keyword (parser, RID_ELSE))
4485 warning_at (loc, OPT_Wempty_body,
4486 "suggest braces around empty body in an %<if%> statement");
4488 else if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
4489 add_stmt (c_parser_compound_statement (parser));
4491 c_parser_statement_after_labels (parser);
4492 return c_end_compound_stmt (body_loc, block, flag_isoc99);
4495 /* Parse the else body of an if statement. This is just parsing a
4496 statement but (a) it is a block in C99, (b) we handle an empty body
4497 specially for the sake of -Wempty-body warnings. */
4500 c_parser_else_body (c_parser *parser)
4502 location_t else_loc = c_parser_peek_token (parser)->location;
4503 tree block = c_begin_compound_stmt (flag_isoc99);
4504 while (c_parser_next_token_is_keyword (parser, RID_CASE)
4505 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
4506 || (c_parser_next_token_is (parser, CPP_NAME)
4507 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
4508 c_parser_label (parser);
4509 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
4511 location_t loc = c_parser_peek_token (parser)->location;
4514 "suggest braces around empty body in an %<else%> statement");
4515 add_stmt (build_empty_stmt (loc));
4516 c_parser_consume_token (parser);
4519 c_parser_statement_after_labels (parser);
4520 return c_end_compound_stmt (else_loc, block, flag_isoc99);
4523 /* Parse an if statement (C90 6.6.4, C99 6.8.4).
4526 if ( expression ) statement
4527 if ( expression ) statement else statement
4531 c_parser_if_statement (c_parser *parser)
4536 bool first_if = false;
4537 tree first_body, second_body;
4540 gcc_assert (c_parser_next_token_is_keyword (parser, RID_IF));
4541 c_parser_consume_token (parser);
4542 block = c_begin_compound_stmt (flag_isoc99);
4543 loc = c_parser_peek_token (parser)->location;
4544 cond = c_parser_paren_condition (parser);
4545 in_if_block = parser->in_if_block;
4546 parser->in_if_block = true;
4547 first_body = c_parser_if_body (parser, &first_if);
4548 parser->in_if_block = in_if_block;
4549 if (c_parser_next_token_is_keyword (parser, RID_ELSE))
4551 c_parser_consume_token (parser);
4552 second_body = c_parser_else_body (parser);
4555 second_body = NULL_TREE;
4556 c_finish_if_stmt (loc, cond, first_body, second_body, first_if);
4557 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99));
4560 /* Parse a switch statement (C90 6.6.4, C99 6.8.4).
4563 switch (expression) statement
4567 c_parser_switch_statement (c_parser *parser)
4569 tree block, expr, body, save_break;
4570 location_t switch_loc = c_parser_peek_token (parser)->location;
4571 location_t switch_cond_loc;
4572 gcc_assert (c_parser_next_token_is_keyword (parser, RID_SWITCH));
4573 c_parser_consume_token (parser);
4574 block = c_begin_compound_stmt (flag_isoc99);
4575 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4577 switch_cond_loc = c_parser_peek_token (parser)->location;
4578 expr = c_parser_expression (parser).value;
4579 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
4583 switch_cond_loc = UNKNOWN_LOCATION;
4584 expr = error_mark_node;
4586 c_start_case (switch_loc, switch_cond_loc, expr);
4587 save_break = c_break_label;
4588 c_break_label = NULL_TREE;
4589 body = c_parser_c99_block_statement (parser);
4590 c_finish_case (body);
4593 location_t here = c_parser_peek_token (parser)->location;
4594 tree t = build1 (LABEL_EXPR, void_type_node, c_break_label);
4595 SET_EXPR_LOCATION (t, here);
4598 c_break_label = save_break;
4599 add_stmt (c_end_compound_stmt (switch_loc, block, flag_isoc99));
4602 /* Parse a while statement (C90 6.6.5, C99 6.8.5).
4605 while (expression) statement
4609 c_parser_while_statement (c_parser *parser)
4611 tree block, cond, body, save_break, save_cont;
4613 gcc_assert (c_parser_next_token_is_keyword (parser, RID_WHILE));
4614 c_parser_consume_token (parser);
4615 block = c_begin_compound_stmt (flag_isoc99);
4616 loc = c_parser_peek_token (parser)->location;
4617 cond = c_parser_paren_condition (parser);
4618 save_break = c_break_label;
4619 c_break_label = NULL_TREE;
4620 save_cont = c_cont_label;
4621 c_cont_label = NULL_TREE;
4622 body = c_parser_c99_block_statement (parser);
4623 c_finish_loop (loc, cond, NULL, body, c_break_label, c_cont_label, true);
4624 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99));
4625 c_break_label = save_break;
4626 c_cont_label = save_cont;
4629 /* Parse a do statement (C90 6.6.5, C99 6.8.5).
4632 do statement while ( expression ) ;
4636 c_parser_do_statement (c_parser *parser)
4638 tree block, cond, body, save_break, save_cont, new_break, new_cont;
4640 gcc_assert (c_parser_next_token_is_keyword (parser, RID_DO));
4641 c_parser_consume_token (parser);
4642 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
4643 warning_at (c_parser_peek_token (parser)->location,
4645 "suggest braces around empty body in %<do%> statement");
4646 block = c_begin_compound_stmt (flag_isoc99);
4647 loc = c_parser_peek_token (parser)->location;
4648 save_break = c_break_label;
4649 c_break_label = NULL_TREE;
4650 save_cont = c_cont_label;
4651 c_cont_label = NULL_TREE;
4652 body = c_parser_c99_block_statement (parser);
4653 c_parser_require_keyword (parser, RID_WHILE, "expected %<while%>");
4654 new_break = c_break_label;
4655 c_break_label = save_break;
4656 new_cont = c_cont_label;
4657 c_cont_label = save_cont;
4658 cond = c_parser_paren_condition (parser);
4659 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
4660 c_parser_skip_to_end_of_block_or_statement (parser);
4661 c_finish_loop (loc, cond, NULL, body, new_break, new_cont, false);
4662 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99));
4665 /* Parse a for statement (C90 6.6.5, C99 6.8.5).
4668 for ( expression[opt] ; expression[opt] ; expression[opt] ) statement
4669 for ( nested-declaration expression[opt] ; expression[opt] ) statement
4671 The form with a declaration is new in C99.
4673 ??? In accordance with the old parser, the declaration may be a
4674 nested function, which is then rejected in check_for_loop_decls,
4675 but does it make any sense for this to be included in the grammar?
4676 Note in particular that the nested function does not include a
4677 trailing ';', whereas the "declaration" production includes one.
4678 Also, can we reject bad declarations earlier and cheaper than
4679 check_for_loop_decls?
4681 In Objective-C, there are two additional variants:
4684 for ( expression in expresssion ) statement
4685 for ( declaration in expression ) statement
4687 This is inconsistent with C, because the second variant is allowed
4688 even if c99 is not enabled.
4690 The rest of the comment documents these Objective-C foreach-statement.
4692 Here is the canonical example of the first variant:
4693 for (object in array) { do something with object }
4694 we call the first expression ("object") the "object_expression" and
4695 the second expression ("array") the "collection_expression".
4696 object_expression must be an lvalue of type "id" (a generic Objective-C
4697 object) because the loop works by assigning to object_expression the
4698 various objects from the collection_expression. collection_expression
4699 must evaluate to something of type "id" which responds to the method
4700 countByEnumeratingWithState:objects:count:.
4702 The canonical example of the second variant is:
4703 for (id object in array) { do something with object }
4704 which is completely equivalent to
4707 for (object in array) { do something with object }
4709 Note that initizializing 'object' in some way (eg, "for ((object =
4710 xxx) in array) { do something with object }") is possibly
4711 technically valid, but completely pointless as 'object' will be
4712 assigned to something else as soon as the loop starts. We should
4713 most likely reject it (TODO).
4715 The beginning of the Objective-C foreach-statement looks exactly
4716 like the beginning of the for-statement, and we can tell it is a
4717 foreach-statement only because the initial declaration or
4718 expression is terminated by 'in' instead of ';'.
4722 c_parser_for_statement (c_parser *parser)
4724 tree block, cond, incr, save_break, save_cont, body;
4725 /* The following are only used when parsing an ObjC foreach statement. */
4726 tree object_expression, collection_expression;
4727 location_t loc = c_parser_peek_token (parser)->location;
4728 location_t for_loc = c_parser_peek_token (parser)->location;
4729 bool is_foreach_statement = false;
4730 gcc_assert (c_parser_next_token_is_keyword (parser, RID_FOR));
4731 c_parser_consume_token (parser);
4732 /* Open a compound statement in Objective-C as well, just in case this is
4733 as foreach expression. */
4734 block = c_begin_compound_stmt (flag_isoc99 || c_dialect_objc ());
4735 cond = error_mark_node;
4736 incr = error_mark_node;
4737 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4739 /* Parse the initialization declaration or expression. */
4740 object_expression = error_mark_node;
4741 parser->objc_could_be_foreach_context = c_dialect_objc ();
4742 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
4744 parser->objc_could_be_foreach_context = false;
4745 c_parser_consume_token (parser);
4746 c_finish_expr_stmt (loc, NULL_TREE);
4748 else if (c_parser_next_tokens_start_declaration (parser))
4750 c_parser_declaration_or_fndef (parser, true, true, true, true, true,
4751 &object_expression);
4752 parser->objc_could_be_foreach_context = false;
4754 if (c_parser_next_token_is_keyword (parser, RID_IN))
4756 c_parser_consume_token (parser);
4757 is_foreach_statement = true;
4758 if (check_for_loop_decls (for_loc, true) == NULL_TREE)
4759 c_parser_error (parser, "multiple iterating variables in fast enumeration");
4762 check_for_loop_decls (for_loc, flag_isoc99);
4764 else if (c_parser_next_token_is_keyword (parser, RID_EXTENSION))
4766 /* __extension__ can start a declaration, but is also an
4767 unary operator that can start an expression. Consume all
4768 but the last of a possible series of __extension__ to
4770 while (c_parser_peek_2nd_token (parser)->type == CPP_KEYWORD
4771 && (c_parser_peek_2nd_token (parser)->keyword
4773 c_parser_consume_token (parser);
4774 if (c_token_starts_declaration (c_parser_peek_2nd_token (parser)))
4777 ext = disable_extension_diagnostics ();
4778 c_parser_consume_token (parser);
4779 c_parser_declaration_or_fndef (parser, true, true, true, true,
4780 true, &object_expression);
4781 parser->objc_could_be_foreach_context = false;
4783 restore_extension_diagnostics (ext);
4784 if (c_parser_next_token_is_keyword (parser, RID_IN))
4786 c_parser_consume_token (parser);
4787 is_foreach_statement = true;
4788 if (check_for_loop_decls (for_loc, true) == NULL_TREE)
4789 c_parser_error (parser, "multiple iterating variables in fast enumeration");
4792 check_for_loop_decls (for_loc, flag_isoc99);
4801 tree init_expression;
4802 init_expression = c_parser_expression (parser).value;
4803 parser->objc_could_be_foreach_context = false;
4804 if (c_parser_next_token_is_keyword (parser, RID_IN))
4806 c_parser_consume_token (parser);
4807 is_foreach_statement = true;
4808 if (! lvalue_p (init_expression))
4809 c_parser_error (parser, "invalid iterating variable in fast enumeration");
4810 object_expression = c_fully_fold (init_expression, false, NULL);
4814 c_finish_expr_stmt (loc, init_expression);
4815 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
4819 /* Parse the loop condition. In the case of a foreach
4820 statement, there is no loop condition. */
4821 gcc_assert (!parser->objc_could_be_foreach_context);
4822 if (!is_foreach_statement)
4824 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
4826 c_parser_consume_token (parser);
4831 cond = c_parser_condition (parser);
4832 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
4835 /* Parse the increment expression (the third expression in a
4836 for-statement). In the case of a foreach-statement, this is
4837 the expression that follows the 'in'. */
4838 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4840 if (is_foreach_statement)
4842 c_parser_error (parser, "missing collection in fast enumeration");
4843 collection_expression = error_mark_node;
4846 incr = c_process_expr_stmt (loc, NULL_TREE);
4850 if (is_foreach_statement)
4851 collection_expression = c_fully_fold (c_parser_expression (parser).value,
4854 incr = c_process_expr_stmt (loc, c_parser_expression (parser).value);
4856 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
4858 save_break = c_break_label;
4859 c_break_label = NULL_TREE;
4860 save_cont = c_cont_label;
4861 c_cont_label = NULL_TREE;
4862 body = c_parser_c99_block_statement (parser);
4863 if (is_foreach_statement)
4864 objc_finish_foreach_loop (loc, object_expression, collection_expression, body, c_break_label, c_cont_label);
4866 c_finish_loop (loc, cond, incr, body, c_break_label, c_cont_label, true);
4867 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99 || c_dialect_objc ()));
4868 c_break_label = save_break;
4869 c_cont_label = save_cont;
4872 /* Parse an asm statement, a GNU extension. This is a full-blown asm
4873 statement with inputs, outputs, clobbers, and volatile tag
4877 asm type-qualifier[opt] ( asm-argument ) ;
4878 asm type-qualifier[opt] goto ( asm-goto-argument ) ;
4882 asm-string-literal : asm-operands[opt]
4883 asm-string-literal : asm-operands[opt] : asm-operands[opt]
4884 asm-string-literal : asm-operands[opt] : asm-operands[opt] : asm-clobbers[opt]
4887 asm-string-literal : : asm-operands[opt] : asm-clobbers[opt] \
4890 Qualifiers other than volatile are accepted in the syntax but
4894 c_parser_asm_statement (c_parser *parser)
4896 tree quals, str, outputs, inputs, clobbers, labels, ret;
4897 bool simple, is_goto;
4898 location_t asm_loc = c_parser_peek_token (parser)->location;
4899 int section, nsections;
4901 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ASM));
4902 c_parser_consume_token (parser);
4903 if (c_parser_next_token_is_keyword (parser, RID_VOLATILE))
4905 quals = c_parser_peek_token (parser)->value;
4906 c_parser_consume_token (parser);
4908 else if (c_parser_next_token_is_keyword (parser, RID_CONST)
4909 || c_parser_next_token_is_keyword (parser, RID_RESTRICT))
4911 warning_at (c_parser_peek_token (parser)->location,
4913 "%E qualifier ignored on asm",
4914 c_parser_peek_token (parser)->value);
4916 c_parser_consume_token (parser);
4922 if (c_parser_next_token_is_keyword (parser, RID_GOTO))
4924 c_parser_consume_token (parser);
4928 /* ??? Follow the C++ parser rather than using the
4929 lex_untranslated_string kludge. */
4930 parser->lex_untranslated_string = true;
4933 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4936 str = c_parser_asm_string_literal (parser);
4937 if (str == NULL_TREE)
4938 goto error_close_paren;
4941 outputs = NULL_TREE;
4943 clobbers = NULL_TREE;
4946 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN) && !is_goto)
4949 /* Parse each colon-delimited section of operands. */
4950 nsections = 3 + is_goto;
4951 for (section = 0; section < nsections; ++section)
4953 if (!c_parser_require (parser, CPP_COLON,
4956 : "expected %<:%> or %<)%>"))
4957 goto error_close_paren;
4959 /* Once past any colon, we're no longer a simple asm. */
4962 if ((!c_parser_next_token_is (parser, CPP_COLON)
4963 && !c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4968 /* For asm goto, we don't allow output operands, but reserve
4969 the slot for a future extension that does allow them. */
4971 outputs = c_parser_asm_operands (parser, false);
4974 inputs = c_parser_asm_operands (parser, true);
4977 clobbers = c_parser_asm_clobbers (parser);
4980 labels = c_parser_asm_goto_operands (parser);
4986 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN) && !is_goto)
4991 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
4993 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
4997 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
4998 c_parser_skip_to_end_of_block_or_statement (parser);
5000 ret = build_asm_stmt (quals, build_asm_expr (asm_loc, str, outputs, inputs,
5001 clobbers, labels, simple));
5004 parser->lex_untranslated_string = false;
5008 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5012 /* Parse asm operands, a GNU extension. If CONVERT_P (for inputs but
5013 not outputs), apply the default conversion of functions and arrays
5018 asm-operands , asm-operand
5021 asm-string-literal ( expression )
5022 [ identifier ] asm-string-literal ( expression )
5026 c_parser_asm_operands (c_parser *parser, bool convert_p)
5028 tree list = NULL_TREE;
5034 if (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
5036 c_parser_consume_token (parser);
5037 if (c_parser_next_token_is (parser, CPP_NAME))
5039 tree id = c_parser_peek_token (parser)->value;
5040 c_parser_consume_token (parser);
5041 name = build_string (IDENTIFIER_LENGTH (id),
5042 IDENTIFIER_POINTER (id));
5046 c_parser_error (parser, "expected identifier");
5047 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
5050 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
5055 str = c_parser_asm_string_literal (parser);
5056 if (str == NULL_TREE)
5058 parser->lex_untranslated_string = false;
5059 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5061 parser->lex_untranslated_string = true;
5064 loc = c_parser_peek_token (parser)->location;
5065 expr = c_parser_expression (parser);
5066 mark_exp_read (expr.value);
5068 expr = default_function_array_conversion (loc, expr);
5069 expr.value = c_fully_fold (expr.value, false, NULL);
5070 parser->lex_untranslated_string = true;
5071 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
5073 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5076 list = chainon (list, build_tree_list (build_tree_list (name, str),
5078 if (c_parser_next_token_is (parser, CPP_COMMA))
5079 c_parser_consume_token (parser);
5086 /* Parse asm clobbers, a GNU extension.
5090 asm-clobbers , asm-string-literal
5094 c_parser_asm_clobbers (c_parser *parser)
5096 tree list = NULL_TREE;
5099 tree str = c_parser_asm_string_literal (parser);
5101 list = tree_cons (NULL_TREE, str, list);
5104 if (c_parser_next_token_is (parser, CPP_COMMA))
5105 c_parser_consume_token (parser);
5112 /* Parse asm goto labels, a GNU extension.
5116 asm-goto-operands , identifier
5120 c_parser_asm_goto_operands (c_parser *parser)
5122 tree list = NULL_TREE;
5127 if (c_parser_next_token_is (parser, CPP_NAME))
5129 c_token *tok = c_parser_peek_token (parser);
5131 label = lookup_label_for_goto (tok->location, name);
5132 c_parser_consume_token (parser);
5133 TREE_USED (label) = 1;
5137 c_parser_error (parser, "expected identifier");
5141 name = build_string (IDENTIFIER_LENGTH (name),
5142 IDENTIFIER_POINTER (name));
5143 list = tree_cons (name, label, list);
5144 if (c_parser_next_token_is (parser, CPP_COMMA))
5145 c_parser_consume_token (parser);
5147 return nreverse (list);
5151 /* Parse an expression other than a compound expression; that is, an
5152 assignment expression (C90 6.3.16, C99 6.5.16). If AFTER is not
5153 NULL then it is an Objective-C message expression which is the
5154 primary-expression starting the expression as an initializer.
5156 assignment-expression:
5157 conditional-expression
5158 unary-expression assignment-operator assignment-expression
5160 assignment-operator: one of
5161 = *= /= %= += -= <<= >>= &= ^= |=
5163 In GNU C we accept any conditional expression on the LHS and
5164 diagnose the invalid lvalue rather than producing a syntax
5167 static struct c_expr
5168 c_parser_expr_no_commas (c_parser *parser, struct c_expr *after)
5170 struct c_expr lhs, rhs, ret;
5171 enum tree_code code;
5172 location_t op_location, exp_location;
5173 gcc_assert (!after || c_dialect_objc ());
5174 lhs = c_parser_conditional_expression (parser, after);
5175 op_location = c_parser_peek_token (parser)->location;
5176 switch (c_parser_peek_token (parser)->type)
5185 code = TRUNC_DIV_EXPR;
5188 code = TRUNC_MOD_EXPR;
5203 code = BIT_AND_EXPR;
5206 code = BIT_XOR_EXPR;
5209 code = BIT_IOR_EXPR;
5214 c_parser_consume_token (parser);
5215 exp_location = c_parser_peek_token (parser)->location;
5216 rhs = c_parser_expr_no_commas (parser, NULL);
5217 rhs = default_function_array_read_conversion (exp_location, rhs);
5218 ret.value = build_modify_expr (op_location, lhs.value, lhs.original_type,
5219 code, exp_location, rhs.value,
5221 if (code == NOP_EXPR)
5222 ret.original_code = MODIFY_EXPR;
5225 TREE_NO_WARNING (ret.value) = 1;
5226 ret.original_code = ERROR_MARK;
5228 ret.original_type = NULL;
5232 /* Parse a conditional expression (C90 6.3.15, C99 6.5.15). If AFTER
5233 is not NULL then it is an Objective-C message expression which is
5234 the primary-expression starting the expression as an initializer.
5236 conditional-expression:
5237 logical-OR-expression
5238 logical-OR-expression ? expression : conditional-expression
5242 conditional-expression:
5243 logical-OR-expression ? : conditional-expression
5246 static struct c_expr
5247 c_parser_conditional_expression (c_parser *parser, struct c_expr *after)
5249 struct c_expr cond, exp1, exp2, ret;
5250 location_t cond_loc, colon_loc, middle_loc;
5252 gcc_assert (!after || c_dialect_objc ());
5254 cond = c_parser_binary_expression (parser, after);
5256 if (c_parser_next_token_is_not (parser, CPP_QUERY))
5258 cond_loc = c_parser_peek_token (parser)->location;
5259 cond = default_function_array_read_conversion (cond_loc, cond);
5260 c_parser_consume_token (parser);
5261 if (c_parser_next_token_is (parser, CPP_COLON))
5263 tree eptype = NULL_TREE;
5265 middle_loc = c_parser_peek_token (parser)->location;
5266 pedwarn (middle_loc, OPT_pedantic,
5267 "ISO C forbids omitting the middle term of a ?: expression");
5268 warn_for_omitted_condop (middle_loc, cond.value);
5269 if (TREE_CODE (cond.value) == EXCESS_PRECISION_EXPR)
5271 eptype = TREE_TYPE (cond.value);
5272 cond.value = TREE_OPERAND (cond.value, 0);
5274 /* Make sure first operand is calculated only once. */
5275 exp1.value = c_save_expr (default_conversion (cond.value));
5277 exp1.value = build1 (EXCESS_PRECISION_EXPR, eptype, exp1.value);
5278 exp1.original_type = NULL;
5279 cond.value = c_objc_common_truthvalue_conversion (cond_loc, exp1.value);
5280 c_inhibit_evaluation_warnings += cond.value == truthvalue_true_node;
5285 = c_objc_common_truthvalue_conversion
5286 (cond_loc, default_conversion (cond.value));
5287 c_inhibit_evaluation_warnings += cond.value == truthvalue_false_node;
5288 exp1 = c_parser_expression_conv (parser);
5289 mark_exp_read (exp1.value);
5290 c_inhibit_evaluation_warnings +=
5291 ((cond.value == truthvalue_true_node)
5292 - (cond.value == truthvalue_false_node));
5295 colon_loc = c_parser_peek_token (parser)->location;
5296 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
5298 c_inhibit_evaluation_warnings -= cond.value == truthvalue_true_node;
5299 ret.value = error_mark_node;
5300 ret.original_code = ERROR_MARK;
5301 ret.original_type = NULL;
5305 location_t exp2_loc = c_parser_peek_token (parser)->location;
5306 exp2 = c_parser_conditional_expression (parser, NULL);
5307 exp2 = default_function_array_read_conversion (exp2_loc, exp2);
5309 c_inhibit_evaluation_warnings -= cond.value == truthvalue_true_node;
5310 ret.value = build_conditional_expr (colon_loc, cond.value,
5311 cond.original_code == C_MAYBE_CONST_EXPR,
5312 exp1.value, exp1.original_type,
5313 exp2.value, exp2.original_type);
5314 ret.original_code = ERROR_MARK;
5315 if (exp1.value == error_mark_node || exp2.value == error_mark_node)
5316 ret.original_type = NULL;
5321 /* If both sides are enum type, the default conversion will have
5322 made the type of the result be an integer type. We want to
5323 remember the enum types we started with. */
5324 t1 = exp1.original_type ? exp1.original_type : TREE_TYPE (exp1.value);
5325 t2 = exp2.original_type ? exp2.original_type : TREE_TYPE (exp2.value);
5326 ret.original_type = ((t1 != error_mark_node
5327 && t2 != error_mark_node
5328 && (TYPE_MAIN_VARIANT (t1)
5329 == TYPE_MAIN_VARIANT (t2)))
5336 /* Parse a binary expression; that is, a logical-OR-expression (C90
5337 6.3.5-6.3.14, C99 6.5.5-6.5.14). If AFTER is not NULL then it is
5338 an Objective-C message expression which is the primary-expression
5339 starting the expression as an initializer.
5341 multiplicative-expression:
5343 multiplicative-expression * cast-expression
5344 multiplicative-expression / cast-expression
5345 multiplicative-expression % cast-expression
5347 additive-expression:
5348 multiplicative-expression
5349 additive-expression + multiplicative-expression
5350 additive-expression - multiplicative-expression
5354 shift-expression << additive-expression
5355 shift-expression >> additive-expression
5357 relational-expression:
5359 relational-expression < shift-expression
5360 relational-expression > shift-expression
5361 relational-expression <= shift-expression
5362 relational-expression >= shift-expression
5364 equality-expression:
5365 relational-expression
5366 equality-expression == relational-expression
5367 equality-expression != relational-expression
5371 AND-expression & equality-expression
5373 exclusive-OR-expression:
5375 exclusive-OR-expression ^ AND-expression
5377 inclusive-OR-expression:
5378 exclusive-OR-expression
5379 inclusive-OR-expression | exclusive-OR-expression
5381 logical-AND-expression:
5382 inclusive-OR-expression
5383 logical-AND-expression && inclusive-OR-expression
5385 logical-OR-expression:
5386 logical-AND-expression
5387 logical-OR-expression || logical-AND-expression
5390 static struct c_expr
5391 c_parser_binary_expression (c_parser *parser, struct c_expr *after)
5393 /* A binary expression is parsed using operator-precedence parsing,
5394 with the operands being cast expressions. All the binary
5395 operators are left-associative. Thus a binary expression is of
5398 E0 op1 E1 op2 E2 ...
5400 which we represent on a stack. On the stack, the precedence
5401 levels are strictly increasing. When a new operator is
5402 encountered of higher precedence than that at the top of the
5403 stack, it is pushed; its LHS is the top expression, and its RHS
5404 is everything parsed until it is popped. When a new operator is
5405 encountered with precedence less than or equal to that at the top
5406 of the stack, triples E[i-1] op[i] E[i] are popped and replaced
5407 by the result of the operation until the operator at the top of
5408 the stack has lower precedence than the new operator or there is
5409 only one element on the stack; then the top expression is the LHS
5410 of the new operator. In the case of logical AND and OR
5411 expressions, we also need to adjust c_inhibit_evaluation_warnings
5412 as appropriate when the operators are pushed and popped. */
5414 /* The precedence levels, where 0 is a dummy lowest level used for
5415 the bottom of the stack. */
5431 /* The expression at this stack level. */
5433 /* The precedence of the operator on its left, PREC_NONE at the
5434 bottom of the stack. */
5436 /* The operation on its left. */
5438 /* The source location of this operation. */
5442 /* Location of the binary operator. */
5443 location_t binary_loc = UNKNOWN_LOCATION; /* Quiet warning. */
5446 switch (stack[sp].op) \
5448 case TRUTH_ANDIF_EXPR: \
5449 c_inhibit_evaluation_warnings -= (stack[sp - 1].expr.value \
5450 == truthvalue_false_node); \
5452 case TRUTH_ORIF_EXPR: \
5453 c_inhibit_evaluation_warnings -= (stack[sp - 1].expr.value \
5454 == truthvalue_true_node); \
5459 stack[sp - 1].expr \
5460 = default_function_array_read_conversion (stack[sp - 1].loc, \
5461 stack[sp - 1].expr); \
5463 = default_function_array_read_conversion (stack[sp].loc, \
5465 stack[sp - 1].expr = parser_build_binary_op (stack[sp].loc, \
5467 stack[sp - 1].expr, \
5471 gcc_assert (!after || c_dialect_objc ());
5472 stack[0].loc = c_parser_peek_token (parser)->location;
5473 stack[0].expr = c_parser_cast_expression (parser, after);
5474 stack[0].prec = PREC_NONE;
5479 enum tree_code ocode;
5482 switch (c_parser_peek_token (parser)->type)
5490 ocode = TRUNC_DIV_EXPR;
5494 ocode = TRUNC_MOD_EXPR;
5506 ocode = LSHIFT_EXPR;
5510 ocode = RSHIFT_EXPR;
5524 case CPP_GREATER_EQ:
5537 oprec = PREC_BITAND;
5538 ocode = BIT_AND_EXPR;
5541 oprec = PREC_BITXOR;
5542 ocode = BIT_XOR_EXPR;
5546 ocode = BIT_IOR_EXPR;
5549 oprec = PREC_LOGAND;
5550 ocode = TRUTH_ANDIF_EXPR;
5554 ocode = TRUTH_ORIF_EXPR;
5557 /* Not a binary operator, so end of the binary
5561 binary_loc = c_parser_peek_token (parser)->location;
5562 c_parser_consume_token (parser);
5563 while (oprec <= stack[sp].prec)
5567 case TRUTH_ANDIF_EXPR:
5569 = default_function_array_read_conversion (stack[sp].loc,
5571 stack[sp].expr.value = c_objc_common_truthvalue_conversion
5572 (stack[sp].loc, default_conversion (stack[sp].expr.value));
5573 c_inhibit_evaluation_warnings += (stack[sp].expr.value
5574 == truthvalue_false_node);
5576 case TRUTH_ORIF_EXPR:
5578 = default_function_array_read_conversion (stack[sp].loc,
5580 stack[sp].expr.value = c_objc_common_truthvalue_conversion
5581 (stack[sp].loc, default_conversion (stack[sp].expr.value));
5582 c_inhibit_evaluation_warnings += (stack[sp].expr.value
5583 == truthvalue_true_node);
5589 stack[sp].loc = binary_loc;
5590 stack[sp].expr = c_parser_cast_expression (parser, NULL);
5591 stack[sp].prec = oprec;
5592 stack[sp].op = ocode;
5593 stack[sp].loc = binary_loc;
5598 return stack[0].expr;
5602 /* Parse a cast expression (C90 6.3.4, C99 6.5.4). If AFTER is not
5603 NULL then it is an Objective-C message expression which is the
5604 primary-expression starting the expression as an initializer.
5608 ( type-name ) unary-expression
5611 static struct c_expr
5612 c_parser_cast_expression (c_parser *parser, struct c_expr *after)
5614 location_t cast_loc = c_parser_peek_token (parser)->location;
5615 gcc_assert (!after || c_dialect_objc ());
5617 return c_parser_postfix_expression_after_primary (parser,
5619 /* If the expression begins with a parenthesized type name, it may
5620 be either a cast or a compound literal; we need to see whether
5621 the next character is '{' to tell the difference. If not, it is
5622 an unary expression. */
5623 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
5624 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
5626 struct c_type_name *type_name;
5629 c_parser_consume_token (parser);
5630 type_name = c_parser_type_name (parser);
5631 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
5632 if (type_name == NULL)
5634 ret.value = error_mark_node;
5635 ret.original_code = ERROR_MARK;
5636 ret.original_type = NULL;
5640 /* Save casted types in the function's used types hash table. */
5641 used_types_insert (type_name->specs->type);
5643 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
5644 return c_parser_postfix_expression_after_paren_type (parser, type_name,
5647 location_t expr_loc = c_parser_peek_token (parser)->location;
5648 expr = c_parser_cast_expression (parser, NULL);
5649 expr = default_function_array_read_conversion (expr_loc, expr);
5651 ret.value = c_cast_expr (cast_loc, type_name, expr.value);
5652 ret.original_code = ERROR_MARK;
5653 ret.original_type = NULL;
5657 return c_parser_unary_expression (parser);
5660 /* Parse an unary expression (C90 6.3.3, C99 6.5.3).
5666 unary-operator cast-expression
5667 sizeof unary-expression
5668 sizeof ( type-name )
5670 unary-operator: one of
5676 __alignof__ unary-expression
5677 __alignof__ ( type-name )
5680 unary-operator: one of
5681 __extension__ __real__ __imag__
5683 In addition, the GNU syntax treats ++ and -- as unary operators, so
5684 they may be applied to cast expressions with errors for non-lvalues
5687 static struct c_expr
5688 c_parser_unary_expression (c_parser *parser)
5691 struct c_expr ret, op;
5692 location_t op_loc = c_parser_peek_token (parser)->location;
5694 ret.original_code = ERROR_MARK;
5695 ret.original_type = NULL;
5696 switch (c_parser_peek_token (parser)->type)
5699 c_parser_consume_token (parser);
5700 exp_loc = c_parser_peek_token (parser)->location;
5701 op = c_parser_cast_expression (parser, NULL);
5702 op = default_function_array_read_conversion (exp_loc, op);
5703 return parser_build_unary_op (op_loc, PREINCREMENT_EXPR, op);
5704 case CPP_MINUS_MINUS:
5705 c_parser_consume_token (parser);
5706 exp_loc = c_parser_peek_token (parser)->location;
5707 op = c_parser_cast_expression (parser, NULL);
5708 op = default_function_array_read_conversion (exp_loc, op);
5709 return parser_build_unary_op (op_loc, PREDECREMENT_EXPR, op);
5711 c_parser_consume_token (parser);
5712 op = c_parser_cast_expression (parser, NULL);
5713 mark_exp_read (op.value);
5714 return parser_build_unary_op (op_loc, ADDR_EXPR, op);
5716 c_parser_consume_token (parser);
5717 exp_loc = c_parser_peek_token (parser)->location;
5718 op = c_parser_cast_expression (parser, NULL);
5719 op = default_function_array_read_conversion (exp_loc, op);
5720 ret.value = build_indirect_ref (op_loc, op.value, RO_UNARY_STAR);
5723 if (!c_dialect_objc () && !in_system_header)
5726 "traditional C rejects the unary plus operator");
5727 c_parser_consume_token (parser);
5728 exp_loc = c_parser_peek_token (parser)->location;
5729 op = c_parser_cast_expression (parser, NULL);
5730 op = default_function_array_read_conversion (exp_loc, op);
5731 return parser_build_unary_op (op_loc, CONVERT_EXPR, op);
5733 c_parser_consume_token (parser);
5734 exp_loc = c_parser_peek_token (parser)->location;
5735 op = c_parser_cast_expression (parser, NULL);
5736 op = default_function_array_read_conversion (exp_loc, op);
5737 return parser_build_unary_op (op_loc, NEGATE_EXPR, op);
5739 c_parser_consume_token (parser);
5740 exp_loc = c_parser_peek_token (parser)->location;
5741 op = c_parser_cast_expression (parser, NULL);
5742 op = default_function_array_read_conversion (exp_loc, op);
5743 return parser_build_unary_op (op_loc, BIT_NOT_EXPR, op);
5745 c_parser_consume_token (parser);
5746 exp_loc = c_parser_peek_token (parser)->location;
5747 op = c_parser_cast_expression (parser, NULL);
5748 op = default_function_array_read_conversion (exp_loc, op);
5749 return parser_build_unary_op (op_loc, TRUTH_NOT_EXPR, op);
5751 /* Refer to the address of a label as a pointer. */
5752 c_parser_consume_token (parser);
5753 if (c_parser_next_token_is (parser, CPP_NAME))
5755 ret.value = finish_label_address_expr
5756 (c_parser_peek_token (parser)->value, op_loc);
5757 c_parser_consume_token (parser);
5761 c_parser_error (parser, "expected identifier");
5762 ret.value = error_mark_node;
5766 switch (c_parser_peek_token (parser)->keyword)
5769 return c_parser_sizeof_expression (parser);
5771 return c_parser_alignof_expression (parser);
5773 c_parser_consume_token (parser);
5774 ext = disable_extension_diagnostics ();
5775 ret = c_parser_cast_expression (parser, NULL);
5776 restore_extension_diagnostics (ext);
5779 c_parser_consume_token (parser);
5780 exp_loc = c_parser_peek_token (parser)->location;
5781 op = c_parser_cast_expression (parser, NULL);
5782 op = default_function_array_conversion (exp_loc, op);
5783 return parser_build_unary_op (op_loc, REALPART_EXPR, op);
5785 c_parser_consume_token (parser);
5786 exp_loc = c_parser_peek_token (parser)->location;
5787 op = c_parser_cast_expression (parser, NULL);
5788 op = default_function_array_conversion (exp_loc, op);
5789 return parser_build_unary_op (op_loc, IMAGPART_EXPR, op);
5791 return c_parser_postfix_expression (parser);
5794 return c_parser_postfix_expression (parser);
5798 /* Parse a sizeof expression. */
5800 static struct c_expr
5801 c_parser_sizeof_expression (c_parser *parser)
5804 location_t expr_loc;
5805 gcc_assert (c_parser_next_token_is_keyword (parser, RID_SIZEOF));
5806 c_parser_consume_token (parser);
5807 c_inhibit_evaluation_warnings++;
5809 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
5810 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
5812 /* Either sizeof ( type-name ) or sizeof unary-expression
5813 starting with a compound literal. */
5814 struct c_type_name *type_name;
5815 c_parser_consume_token (parser);
5816 expr_loc = c_parser_peek_token (parser)->location;
5817 type_name = c_parser_type_name (parser);
5818 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
5819 if (type_name == NULL)
5822 c_inhibit_evaluation_warnings--;
5824 ret.value = error_mark_node;
5825 ret.original_code = ERROR_MARK;
5826 ret.original_type = NULL;
5829 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
5831 expr = c_parser_postfix_expression_after_paren_type (parser,
5836 /* sizeof ( type-name ). */
5837 c_inhibit_evaluation_warnings--;
5839 return c_expr_sizeof_type (expr_loc, type_name);
5843 expr_loc = c_parser_peek_token (parser)->location;
5844 expr = c_parser_unary_expression (parser);
5846 c_inhibit_evaluation_warnings--;
5848 mark_exp_read (expr.value);
5849 if (TREE_CODE (expr.value) == COMPONENT_REF
5850 && DECL_C_BIT_FIELD (TREE_OPERAND (expr.value, 1)))
5851 error_at (expr_loc, "%<sizeof%> applied to a bit-field");
5852 return c_expr_sizeof_expr (expr_loc, expr);
5856 /* Parse an alignof expression. */
5858 static struct c_expr
5859 c_parser_alignof_expression (c_parser *parser)
5862 location_t loc = c_parser_peek_token (parser)->location;
5863 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ALIGNOF));
5864 c_parser_consume_token (parser);
5865 c_inhibit_evaluation_warnings++;
5867 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
5868 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
5870 /* Either __alignof__ ( type-name ) or __alignof__
5871 unary-expression starting with a compound literal. */
5873 struct c_type_name *type_name;
5875 c_parser_consume_token (parser);
5876 loc = c_parser_peek_token (parser)->location;
5877 type_name = c_parser_type_name (parser);
5878 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
5879 if (type_name == NULL)
5882 c_inhibit_evaluation_warnings--;
5884 ret.value = error_mark_node;
5885 ret.original_code = ERROR_MARK;
5886 ret.original_type = NULL;
5889 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
5891 expr = c_parser_postfix_expression_after_paren_type (parser,
5896 /* alignof ( type-name ). */
5897 c_inhibit_evaluation_warnings--;
5899 ret.value = c_alignof (loc, groktypename (type_name, NULL, NULL));
5900 ret.original_code = ERROR_MARK;
5901 ret.original_type = NULL;
5907 expr = c_parser_unary_expression (parser);
5909 mark_exp_read (expr.value);
5910 c_inhibit_evaluation_warnings--;
5912 ret.value = c_alignof_expr (loc, expr.value);
5913 ret.original_code = ERROR_MARK;
5914 ret.original_type = NULL;
5919 /* Parse a postfix expression (C90 6.3.1-6.3.2, C99 6.5.1-6.5.2).
5923 postfix-expression [ expression ]
5924 postfix-expression ( argument-expression-list[opt] )
5925 postfix-expression . identifier
5926 postfix-expression -> identifier
5927 postfix-expression ++
5928 postfix-expression --
5929 ( type-name ) { initializer-list }
5930 ( type-name ) { initializer-list , }
5932 argument-expression-list:
5934 argument-expression-list , argument-expression
5946 (treated as a keyword in GNU C)
5949 ( compound-statement )
5950 __builtin_va_arg ( assignment-expression , type-name )
5951 __builtin_offsetof ( type-name , offsetof-member-designator )
5952 __builtin_choose_expr ( assignment-expression ,
5953 assignment-expression ,
5954 assignment-expression )
5955 __builtin_types_compatible_p ( type-name , type-name )
5957 offsetof-member-designator:
5959 offsetof-member-designator . identifier
5960 offsetof-member-designator [ expression ]
5965 [ objc-receiver objc-message-args ]
5966 @selector ( objc-selector-arg )
5967 @protocol ( identifier )
5968 @encode ( type-name )
5970 Classname . identifier
5973 static struct c_expr
5974 c_parser_postfix_expression (c_parser *parser)
5976 struct c_expr expr, e1, e2, e3;
5977 struct c_type_name *t1, *t2;
5978 location_t loc = c_parser_peek_token (parser)->location;;
5979 expr.original_code = ERROR_MARK;
5980 expr.original_type = NULL;
5981 switch (c_parser_peek_token (parser)->type)
5984 expr.value = c_parser_peek_token (parser)->value;
5985 loc = c_parser_peek_token (parser)->location;
5986 c_parser_consume_token (parser);
5987 if (TREE_CODE (expr.value) == FIXED_CST
5988 && !targetm.fixed_point_supported_p ())
5990 error_at (loc, "fixed-point types not supported for this target");
5991 expr.value = error_mark_node;
5998 expr.value = c_parser_peek_token (parser)->value;
5999 c_parser_consume_token (parser);
6005 case CPP_UTF8STRING:
6006 expr.value = c_parser_peek_token (parser)->value;
6007 expr.original_code = STRING_CST;
6008 c_parser_consume_token (parser);
6010 case CPP_OBJC_STRING:
6011 gcc_assert (c_dialect_objc ());
6013 = objc_build_string_object (c_parser_peek_token (parser)->value);
6014 c_parser_consume_token (parser);
6017 switch (c_parser_peek_token (parser)->id_kind)
6021 tree id = c_parser_peek_token (parser)->value;
6022 c_parser_consume_token (parser);
6023 expr.value = build_external_ref (loc, id,
6024 (c_parser_peek_token (parser)->type
6026 &expr.original_type);
6029 case C_ID_CLASSNAME:
6031 /* Here we parse the Objective-C 2.0 Class.name dot
6033 tree class_name = c_parser_peek_token (parser)->value;
6035 c_parser_consume_token (parser);
6036 gcc_assert (c_dialect_objc ());
6037 if (!c_parser_require (parser, CPP_DOT, "expected %<.%>"))
6039 expr.value = error_mark_node;
6042 if (c_parser_next_token_is_not (parser, CPP_NAME))
6044 c_parser_error (parser, "expected identifier");
6045 expr.value = error_mark_node;
6048 component = c_parser_peek_token (parser)->value;
6049 c_parser_consume_token (parser);
6050 expr.value = objc_build_class_component_ref (class_name,
6055 c_parser_error (parser, "expected expression");
6056 expr.value = error_mark_node;
6060 case CPP_OPEN_PAREN:
6061 /* A parenthesized expression, statement expression or compound
6063 if (c_parser_peek_2nd_token (parser)->type == CPP_OPEN_BRACE)
6065 /* A statement expression. */
6067 location_t brace_loc;
6068 c_parser_consume_token (parser);
6069 brace_loc = c_parser_peek_token (parser)->location;
6070 c_parser_consume_token (parser);
6071 if (cur_stmt_list == NULL)
6073 error_at (loc, "braced-group within expression allowed "
6074 "only inside a function");
6075 parser->error = true;
6076 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
6077 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6078 expr.value = error_mark_node;
6081 stmt = c_begin_stmt_expr ();
6082 c_parser_compound_statement_nostart (parser);
6083 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
6085 pedwarn (loc, OPT_pedantic,
6086 "ISO C forbids braced-groups within expressions");
6087 expr.value = c_finish_stmt_expr (brace_loc, stmt);
6088 mark_exp_read (expr.value);
6090 else if (c_token_starts_typename (c_parser_peek_2nd_token (parser)))
6092 /* A compound literal. ??? Can we actually get here rather
6093 than going directly to
6094 c_parser_postfix_expression_after_paren_type from
6097 struct c_type_name *type_name;
6098 c_parser_consume_token (parser);
6099 loc = c_parser_peek_token (parser)->location;
6100 type_name = c_parser_type_name (parser);
6101 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
6103 if (type_name == NULL)
6105 expr.value = error_mark_node;
6108 expr = c_parser_postfix_expression_after_paren_type (parser,
6114 /* A parenthesized expression. */
6115 c_parser_consume_token (parser);
6116 expr = c_parser_expression (parser);
6117 if (TREE_CODE (expr.value) == MODIFY_EXPR)
6118 TREE_NO_WARNING (expr.value) = 1;
6119 if (expr.original_code != C_MAYBE_CONST_EXPR)
6120 expr.original_code = ERROR_MARK;
6121 /* Don't change EXPR.ORIGINAL_TYPE. */
6122 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
6127 switch (c_parser_peek_token (parser)->keyword)
6129 case RID_FUNCTION_NAME:
6130 case RID_PRETTY_FUNCTION_NAME:
6131 case RID_C99_FUNCTION_NAME:
6132 expr.value = fname_decl (loc,
6133 c_parser_peek_token (parser)->keyword,
6134 c_parser_peek_token (parser)->value);
6135 c_parser_consume_token (parser);
6138 c_parser_consume_token (parser);
6139 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6141 expr.value = error_mark_node;
6144 e1 = c_parser_expr_no_commas (parser, NULL);
6145 mark_exp_read (e1.value);
6146 e1.value = c_fully_fold (e1.value, false, NULL);
6147 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
6149 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6150 expr.value = error_mark_node;
6153 loc = c_parser_peek_token (parser)->location;
6154 t1 = c_parser_type_name (parser);
6155 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
6159 expr.value = error_mark_node;
6163 tree type_expr = NULL_TREE;
6164 expr.value = c_build_va_arg (loc, e1.value,
6165 groktypename (t1, &type_expr, NULL));
6168 expr.value = build2 (C_MAYBE_CONST_EXPR,
6169 TREE_TYPE (expr.value), type_expr,
6171 C_MAYBE_CONST_EXPR_NON_CONST (expr.value) = true;
6176 c_parser_consume_token (parser);
6177 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6179 expr.value = error_mark_node;
6182 t1 = c_parser_type_name (parser);
6185 expr.value = error_mark_node;
6188 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
6190 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6191 expr.value = error_mark_node;
6195 tree type = groktypename (t1, NULL, NULL);
6197 if (type == error_mark_node)
6198 offsetof_ref = error_mark_node;
6201 offsetof_ref = build1 (INDIRECT_REF, type, null_pointer_node);
6202 SET_EXPR_LOCATION (offsetof_ref, loc);
6204 /* Parse the second argument to __builtin_offsetof. We
6205 must have one identifier, and beyond that we want to
6206 accept sub structure and sub array references. */
6207 if (c_parser_next_token_is (parser, CPP_NAME))
6209 offsetof_ref = build_component_ref
6210 (loc, offsetof_ref, c_parser_peek_token (parser)->value);
6211 c_parser_consume_token (parser);
6212 while (c_parser_next_token_is (parser, CPP_DOT)
6213 || c_parser_next_token_is (parser,
6215 || c_parser_next_token_is (parser,
6218 if (c_parser_next_token_is (parser, CPP_DEREF))
6220 loc = c_parser_peek_token (parser)->location;
6221 offsetof_ref = build_array_ref (loc,
6226 else if (c_parser_next_token_is (parser, CPP_DOT))
6229 c_parser_consume_token (parser);
6230 if (c_parser_next_token_is_not (parser,
6233 c_parser_error (parser, "expected identifier");
6236 offsetof_ref = build_component_ref
6238 c_parser_peek_token (parser)->value);
6239 c_parser_consume_token (parser);
6244 loc = c_parser_peek_token (parser)->location;
6245 c_parser_consume_token (parser);
6246 idx = c_parser_expression (parser).value;
6247 idx = c_fully_fold (idx, false, NULL);
6248 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
6250 offsetof_ref = build_array_ref (loc, offsetof_ref, idx);
6255 c_parser_error (parser, "expected identifier");
6256 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
6258 expr.value = fold_offsetof (offsetof_ref, NULL_TREE);
6261 case RID_CHOOSE_EXPR:
6262 c_parser_consume_token (parser);
6263 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6265 expr.value = error_mark_node;
6268 loc = c_parser_peek_token (parser)->location;
6269 e1 = c_parser_expr_no_commas (parser, NULL);
6270 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
6272 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6273 expr.value = error_mark_node;
6276 e2 = c_parser_expr_no_commas (parser, NULL);
6277 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
6279 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6280 expr.value = error_mark_node;
6283 e3 = c_parser_expr_no_commas (parser, NULL);
6284 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
6290 mark_exp_read (e2.value);
6291 mark_exp_read (e3.value);
6292 if (TREE_CODE (c) != INTEGER_CST
6293 || !INTEGRAL_TYPE_P (TREE_TYPE (c)))
6295 "first argument to %<__builtin_choose_expr%> not"
6297 constant_expression_warning (c);
6298 expr = integer_zerop (c) ? e3 : e2;
6301 case RID_TYPES_COMPATIBLE_P:
6302 c_parser_consume_token (parser);
6303 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6305 expr.value = error_mark_node;
6308 t1 = c_parser_type_name (parser);
6311 expr.value = error_mark_node;
6314 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
6316 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6317 expr.value = error_mark_node;
6320 t2 = c_parser_type_name (parser);
6323 expr.value = error_mark_node;
6326 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
6331 e1 = TYPE_MAIN_VARIANT (groktypename (t1, NULL, NULL));
6332 e2 = TYPE_MAIN_VARIANT (groktypename (t2, NULL, NULL));
6335 = comptypes (e1, e2) ? integer_one_node : integer_zero_node;
6338 case RID_AT_SELECTOR:
6339 gcc_assert (c_dialect_objc ());
6340 c_parser_consume_token (parser);
6341 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6343 expr.value = error_mark_node;
6347 tree sel = c_parser_objc_selector_arg (parser);
6348 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
6350 expr.value = objc_build_selector_expr (loc, sel);
6353 case RID_AT_PROTOCOL:
6354 gcc_assert (c_dialect_objc ());
6355 c_parser_consume_token (parser);
6356 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6358 expr.value = error_mark_node;
6361 if (c_parser_next_token_is_not (parser, CPP_NAME))
6363 c_parser_error (parser, "expected identifier");
6364 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6365 expr.value = error_mark_node;
6369 tree id = c_parser_peek_token (parser)->value;
6370 c_parser_consume_token (parser);
6371 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
6373 expr.value = objc_build_protocol_expr (id);
6377 /* Extension to support C-structures in the archiver. */
6378 gcc_assert (c_dialect_objc ());
6379 c_parser_consume_token (parser);
6380 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6382 expr.value = error_mark_node;
6385 t1 = c_parser_type_name (parser);
6388 expr.value = error_mark_node;
6389 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6392 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
6395 tree type = groktypename (t1, NULL, NULL);
6396 expr.value = objc_build_encode_expr (type);
6400 c_parser_error (parser, "expected expression");
6401 expr.value = error_mark_node;
6405 case CPP_OPEN_SQUARE:
6406 if (c_dialect_objc ())
6408 tree receiver, args;
6409 c_parser_consume_token (parser);
6410 receiver = c_parser_objc_receiver (parser);
6411 args = c_parser_objc_message_args (parser);
6412 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
6414 expr.value = objc_build_message_expr (build_tree_list (receiver,
6418 /* Else fall through to report error. */
6420 c_parser_error (parser, "expected expression");
6421 expr.value = error_mark_node;
6424 return c_parser_postfix_expression_after_primary (parser, loc, expr);
6427 /* Parse a postfix expression after a parenthesized type name: the
6428 brace-enclosed initializer of a compound literal, possibly followed
6429 by some postfix operators. This is separate because it is not
6430 possible to tell until after the type name whether a cast
6431 expression has a cast or a compound literal, or whether the operand
6432 of sizeof is a parenthesized type name or starts with a compound
6433 literal. TYPE_LOC is the location where TYPE_NAME starts--the
6434 location of the first token after the parentheses around the type
6437 static struct c_expr
6438 c_parser_postfix_expression_after_paren_type (c_parser *parser,
6439 struct c_type_name *type_name,
6440 location_t type_loc)
6446 location_t start_loc;
6447 tree type_expr = NULL_TREE;
6448 bool type_expr_const = true;
6449 check_compound_literal_type (type_loc, type_name);
6450 start_init (NULL_TREE, NULL, 0);
6451 type = groktypename (type_name, &type_expr, &type_expr_const);
6452 start_loc = c_parser_peek_token (parser)->location;
6453 if (type != error_mark_node && C_TYPE_VARIABLE_SIZE (type))
6455 error_at (type_loc, "compound literal has variable size");
6456 type = error_mark_node;
6458 init = c_parser_braced_init (parser, type, false);
6460 maybe_warn_string_init (type, init);
6462 if (type != error_mark_node
6463 && !ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (type))
6464 && current_function_decl)
6466 error ("compound literal qualified by address-space qualifier");
6467 type = error_mark_node;
6471 pedwarn (start_loc, OPT_pedantic, "ISO C90 forbids compound literals");
6472 non_const = ((init.value && TREE_CODE (init.value) == CONSTRUCTOR)
6473 ? CONSTRUCTOR_NON_CONST (init.value)
6474 : init.original_code == C_MAYBE_CONST_EXPR);
6475 non_const |= !type_expr_const;
6476 expr.value = build_compound_literal (start_loc, type, init.value, non_const);
6477 expr.original_code = ERROR_MARK;
6478 expr.original_type = NULL;
6481 if (TREE_CODE (expr.value) == C_MAYBE_CONST_EXPR)
6483 gcc_assert (C_MAYBE_CONST_EXPR_PRE (expr.value) == NULL_TREE);
6484 C_MAYBE_CONST_EXPR_PRE (expr.value) = type_expr;
6488 gcc_assert (!non_const);
6489 expr.value = build2 (C_MAYBE_CONST_EXPR, type,
6490 type_expr, expr.value);
6493 return c_parser_postfix_expression_after_primary (parser, start_loc, expr);
6496 /* Parse a postfix expression after the initial primary or compound
6497 literal; that is, parse a series of postfix operators.
6499 EXPR_LOC is the location of the primary expression. */
6501 static struct c_expr
6502 c_parser_postfix_expression_after_primary (c_parser *parser,
6503 location_t expr_loc,
6506 struct c_expr orig_expr;
6508 VEC(tree,gc) *exprlist;
6509 VEC(tree,gc) *origtypes;
6512 location_t op_loc = c_parser_peek_token (parser)->location;
6513 switch (c_parser_peek_token (parser)->type)
6515 case CPP_OPEN_SQUARE:
6516 /* Array reference. */
6517 c_parser_consume_token (parser);
6518 idx = c_parser_expression (parser).value;
6519 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
6521 expr.value = build_array_ref (op_loc, expr.value, idx);
6522 expr.original_code = ERROR_MARK;
6523 expr.original_type = NULL;
6525 case CPP_OPEN_PAREN:
6526 /* Function call. */
6527 c_parser_consume_token (parser);
6528 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
6531 exprlist = c_parser_expr_list (parser, true, false, &origtypes);
6532 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
6535 mark_exp_read (expr.value);
6536 /* FIXME diagnostics: Ideally we want the FUNCNAME, not the
6537 "(" after the FUNCNAME, which is what we have now. */
6538 expr.value = build_function_call_vec (op_loc, expr.value, exprlist,
6540 expr.original_code = ERROR_MARK;
6541 if (TREE_CODE (expr.value) == INTEGER_CST
6542 && TREE_CODE (orig_expr.value) == FUNCTION_DECL
6543 && DECL_BUILT_IN_CLASS (orig_expr.value) == BUILT_IN_NORMAL
6544 && DECL_FUNCTION_CODE (orig_expr.value) == BUILT_IN_CONSTANT_P)
6545 expr.original_code = C_MAYBE_CONST_EXPR;
6546 expr.original_type = NULL;
6547 if (exprlist != NULL)
6549 release_tree_vector (exprlist);
6550 release_tree_vector (origtypes);
6554 /* Structure element reference. */
6555 c_parser_consume_token (parser);
6556 expr = default_function_array_conversion (expr_loc, expr);
6557 if (c_parser_next_token_is (parser, CPP_NAME))
6558 ident = c_parser_peek_token (parser)->value;
6561 c_parser_error (parser, "expected identifier");
6562 expr.value = error_mark_node;
6563 expr.original_code = ERROR_MARK;
6564 expr.original_type = NULL;
6567 c_parser_consume_token (parser);
6568 expr.value = build_component_ref (op_loc, expr.value, ident);
6569 expr.original_code = ERROR_MARK;
6570 if (TREE_CODE (expr.value) != COMPONENT_REF)
6571 expr.original_type = NULL;
6574 /* Remember the original type of a bitfield. */
6575 tree field = TREE_OPERAND (expr.value, 1);
6576 if (TREE_CODE (field) != FIELD_DECL)
6577 expr.original_type = NULL;
6579 expr.original_type = DECL_BIT_FIELD_TYPE (field);
6583 /* Structure element reference. */
6584 c_parser_consume_token (parser);
6585 expr = default_function_array_conversion (expr_loc, expr);
6586 if (c_parser_next_token_is (parser, CPP_NAME))
6587 ident = c_parser_peek_token (parser)->value;
6590 c_parser_error (parser, "expected identifier");
6591 expr.value = error_mark_node;
6592 expr.original_code = ERROR_MARK;
6593 expr.original_type = NULL;
6596 c_parser_consume_token (parser);
6597 expr.value = build_component_ref (op_loc,
6598 build_indirect_ref (op_loc,
6602 expr.original_code = ERROR_MARK;
6603 if (TREE_CODE (expr.value) != COMPONENT_REF)
6604 expr.original_type = NULL;
6607 /* Remember the original type of a bitfield. */
6608 tree field = TREE_OPERAND (expr.value, 1);
6609 if (TREE_CODE (field) != FIELD_DECL)
6610 expr.original_type = NULL;
6612 expr.original_type = DECL_BIT_FIELD_TYPE (field);
6616 /* Postincrement. */
6617 c_parser_consume_token (parser);
6618 expr = default_function_array_read_conversion (expr_loc, expr);
6619 expr.value = build_unary_op (op_loc,
6620 POSTINCREMENT_EXPR, expr.value, 0);
6621 expr.original_code = ERROR_MARK;
6622 expr.original_type = NULL;
6624 case CPP_MINUS_MINUS:
6625 /* Postdecrement. */
6626 c_parser_consume_token (parser);
6627 expr = default_function_array_read_conversion (expr_loc, expr);
6628 expr.value = build_unary_op (op_loc,
6629 POSTDECREMENT_EXPR, expr.value, 0);
6630 expr.original_code = ERROR_MARK;
6631 expr.original_type = NULL;
6639 /* Parse an expression (C90 6.3.17, C99 6.5.17).
6642 assignment-expression
6643 expression , assignment-expression
6646 static struct c_expr
6647 c_parser_expression (c_parser *parser)
6650 expr = c_parser_expr_no_commas (parser, NULL);
6651 while (c_parser_next_token_is (parser, CPP_COMMA))
6655 location_t loc = c_parser_peek_token (parser)->location;
6656 location_t expr_loc;
6657 c_parser_consume_token (parser);
6658 expr_loc = c_parser_peek_token (parser)->location;
6659 lhsval = expr.value;
6660 while (TREE_CODE (lhsval) == COMPOUND_EXPR)
6661 lhsval = TREE_OPERAND (lhsval, 1);
6662 if (DECL_P (lhsval) || handled_component_p (lhsval))
6663 mark_exp_read (lhsval);
6664 next = c_parser_expr_no_commas (parser, NULL);
6665 next = default_function_array_conversion (expr_loc, next);
6666 expr.value = build_compound_expr (loc, expr.value, next.value);
6667 expr.original_code = COMPOUND_EXPR;
6668 expr.original_type = next.original_type;
6673 /* Parse an expression and convert functions or arrays to
6676 static struct c_expr
6677 c_parser_expression_conv (c_parser *parser)
6680 location_t loc = c_parser_peek_token (parser)->location;
6681 expr = c_parser_expression (parser);
6682 expr = default_function_array_conversion (loc, expr);
6686 /* Parse a non-empty list of expressions. If CONVERT_P, convert
6687 functions and arrays to pointers. If FOLD_P, fold the expressions.
6690 assignment-expression
6691 nonempty-expr-list , assignment-expression
6694 static VEC(tree,gc) *
6695 c_parser_expr_list (c_parser *parser, bool convert_p, bool fold_p,
6696 VEC(tree,gc) **p_orig_types)
6699 VEC(tree,gc) *orig_types;
6701 location_t loc = c_parser_peek_token (parser)->location;
6703 ret = make_tree_vector ();
6704 if (p_orig_types == NULL)
6707 orig_types = make_tree_vector ();
6709 expr = c_parser_expr_no_commas (parser, NULL);
6711 expr = default_function_array_read_conversion (loc, expr);
6713 expr.value = c_fully_fold (expr.value, false, NULL);
6714 VEC_quick_push (tree, ret, expr.value);
6715 if (orig_types != NULL)
6716 VEC_quick_push (tree, orig_types, expr.original_type);
6717 while (c_parser_next_token_is (parser, CPP_COMMA))
6719 c_parser_consume_token (parser);
6720 loc = c_parser_peek_token (parser)->location;
6721 expr = c_parser_expr_no_commas (parser, NULL);
6723 expr = default_function_array_read_conversion (loc, expr);
6725 expr.value = c_fully_fold (expr.value, false, NULL);
6726 VEC_safe_push (tree, gc, ret, expr.value);
6727 if (orig_types != NULL)
6728 VEC_safe_push (tree, gc, orig_types, expr.original_type);
6730 if (orig_types != NULL)
6731 *p_orig_types = orig_types;
6735 /* Parse Objective-C-specific constructs. */
6737 /* Parse an objc-class-definition.
6739 objc-class-definition:
6740 @interface identifier objc-superclass[opt] objc-protocol-refs[opt]
6741 objc-class-instance-variables[opt] objc-methodprotolist @end
6742 @implementation identifier objc-superclass[opt]
6743 objc-class-instance-variables[opt]
6744 @interface identifier ( identifier ) objc-protocol-refs[opt]
6745 objc-methodprotolist @end
6746 @interface identifier ( ) objc-protocol-refs[opt]
6747 objc-methodprotolist @end
6748 @implementation identifier ( identifier )
6753 "@interface identifier (" must start "@interface identifier (
6754 identifier ) ...": objc-methodprotolist in the first production may
6755 not start with a parenthesized identifier as a declarator of a data
6756 definition with no declaration specifiers if the objc-superclass,
6757 objc-protocol-refs and objc-class-instance-variables are omitted. */
6760 c_parser_objc_class_definition (c_parser *parser, tree attributes)
6765 if (c_parser_next_token_is_keyword (parser, RID_AT_INTERFACE))
6767 else if (c_parser_next_token_is_keyword (parser, RID_AT_IMPLEMENTATION))
6772 c_parser_consume_token (parser);
6773 if (c_parser_next_token_is_not (parser, CPP_NAME))
6775 c_parser_error (parser, "expected identifier");
6778 id1 = c_parser_peek_token (parser)->value;
6779 c_parser_consume_token (parser);
6780 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
6782 /* We have a category or class extension. */
6784 tree proto = NULL_TREE;
6785 c_parser_consume_token (parser);
6786 if (c_parser_next_token_is_not (parser, CPP_NAME))
6788 if (iface_p && c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
6790 /* We have a class extension. */
6795 c_parser_error (parser, "expected identifier or %<)%>");
6796 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6802 id2 = c_parser_peek_token (parser)->value;
6803 c_parser_consume_token (parser);
6805 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
6808 objc_start_category_implementation (id1, id2);
6811 if (c_parser_next_token_is (parser, CPP_LESS))
6812 proto = c_parser_objc_protocol_refs (parser);
6813 objc_start_category_interface (id1, id2, proto, attributes);
6814 c_parser_objc_methodprotolist (parser);
6815 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
6816 objc_finish_interface ();
6819 if (c_parser_next_token_is (parser, CPP_COLON))
6821 c_parser_consume_token (parser);
6822 if (c_parser_next_token_is_not (parser, CPP_NAME))
6824 c_parser_error (parser, "expected identifier");
6827 superclass = c_parser_peek_token (parser)->value;
6828 c_parser_consume_token (parser);
6831 superclass = NULL_TREE;
6834 tree proto = NULL_TREE;
6835 if (c_parser_next_token_is (parser, CPP_LESS))
6836 proto = c_parser_objc_protocol_refs (parser);
6837 objc_start_class_interface (id1, superclass, proto, attributes);
6840 objc_start_class_implementation (id1, superclass);
6841 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
6842 c_parser_objc_class_instance_variables (parser);
6845 objc_continue_interface ();
6846 c_parser_objc_methodprotolist (parser);
6847 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
6848 objc_finish_interface ();
6852 objc_continue_implementation ();
6857 /* Parse objc-class-instance-variables.
6859 objc-class-instance-variables:
6860 { objc-instance-variable-decl-list[opt] }
6862 objc-instance-variable-decl-list:
6863 objc-visibility-spec
6864 objc-instance-variable-decl ;
6866 objc-instance-variable-decl-list objc-visibility-spec
6867 objc-instance-variable-decl-list objc-instance-variable-decl ;
6868 objc-instance-variable-decl-list ;
6870 objc-visibility-spec:
6875 objc-instance-variable-decl:
6880 c_parser_objc_class_instance_variables (c_parser *parser)
6882 gcc_assert (c_parser_next_token_is (parser, CPP_OPEN_BRACE));
6883 c_parser_consume_token (parser);
6884 while (c_parser_next_token_is_not (parser, CPP_EOF))
6887 /* Parse any stray semicolon. */
6888 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
6890 pedwarn (c_parser_peek_token (parser)->location, OPT_pedantic,
6891 "extra semicolon in struct or union specified");
6892 c_parser_consume_token (parser);
6895 /* Stop if at the end of the instance variables. */
6896 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
6898 c_parser_consume_token (parser);
6901 /* Parse any objc-visibility-spec. */
6902 if (c_parser_next_token_is_keyword (parser, RID_AT_PRIVATE))
6904 c_parser_consume_token (parser);
6905 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
6908 else if (c_parser_next_token_is_keyword (parser, RID_AT_PROTECTED))
6910 c_parser_consume_token (parser);
6911 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
6914 else if (c_parser_next_token_is_keyword (parser, RID_AT_PUBLIC))
6916 c_parser_consume_token (parser);
6917 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
6920 else if (c_parser_next_token_is_keyword (parser, RID_AT_PACKAGE))
6922 c_parser_consume_token (parser);
6923 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
6926 else if (c_parser_next_token_is (parser, CPP_PRAGMA))
6928 c_parser_pragma (parser, pragma_external);
6932 /* Parse some comma-separated declarations. */
6933 decls = c_parser_struct_declaration (parser);
6935 /* Comma-separated instance variables are chained together in
6936 reverse order; add them one by one. */
6937 tree ivar = nreverse (decls);
6938 for (; ivar; ivar = DECL_CHAIN (ivar))
6939 objc_add_instance_variable (copy_node (ivar));
6941 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
6945 /* Parse an objc-class-declaration.
6947 objc-class-declaration:
6948 @class identifier-list ;
6952 c_parser_objc_class_declaration (c_parser *parser)
6954 tree list = NULL_TREE;
6955 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_CLASS));
6956 c_parser_consume_token (parser);
6957 /* Any identifiers, including those declared as type names, are OK
6962 if (c_parser_next_token_is_not (parser, CPP_NAME))
6964 c_parser_error (parser, "expected identifier");
6965 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
6966 parser->error = false;
6969 id = c_parser_peek_token (parser)->value;
6970 list = chainon (list, build_tree_list (NULL_TREE, id));
6971 c_parser_consume_token (parser);
6972 if (c_parser_next_token_is (parser, CPP_COMMA))
6973 c_parser_consume_token (parser);
6977 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
6978 objc_declare_class (list);
6981 /* Parse an objc-alias-declaration.
6983 objc-alias-declaration:
6984 @compatibility_alias identifier identifier ;
6988 c_parser_objc_alias_declaration (c_parser *parser)
6991 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_ALIAS));
6992 c_parser_consume_token (parser);
6993 if (c_parser_next_token_is_not (parser, CPP_NAME))
6995 c_parser_error (parser, "expected identifier");
6996 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
6999 id1 = c_parser_peek_token (parser)->value;
7000 c_parser_consume_token (parser);
7001 if (c_parser_next_token_is_not (parser, CPP_NAME))
7003 c_parser_error (parser, "expected identifier");
7004 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
7007 id2 = c_parser_peek_token (parser)->value;
7008 c_parser_consume_token (parser);
7009 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
7010 objc_declare_alias (id1, id2);
7013 /* Parse an objc-protocol-definition.
7015 objc-protocol-definition:
7016 @protocol identifier objc-protocol-refs[opt] objc-methodprotolist @end
7017 @protocol identifier-list ;
7019 "@protocol identifier ;" should be resolved as "@protocol
7020 identifier-list ;": objc-methodprotolist may not start with a
7021 semicolon in the first alternative if objc-protocol-refs are
7025 c_parser_objc_protocol_definition (c_parser *parser, tree attributes)
7027 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_PROTOCOL));
7029 c_parser_consume_token (parser);
7030 if (c_parser_next_token_is_not (parser, CPP_NAME))
7032 c_parser_error (parser, "expected identifier");
7035 if (c_parser_peek_2nd_token (parser)->type == CPP_COMMA
7036 || c_parser_peek_2nd_token (parser)->type == CPP_SEMICOLON)
7038 tree list = NULL_TREE;
7039 /* Any identifiers, including those declared as type names, are
7044 if (c_parser_next_token_is_not (parser, CPP_NAME))
7046 c_parser_error (parser, "expected identifier");
7049 id = c_parser_peek_token (parser)->value;
7050 list = chainon (list, build_tree_list (NULL_TREE, id));
7051 c_parser_consume_token (parser);
7052 if (c_parser_next_token_is (parser, CPP_COMMA))
7053 c_parser_consume_token (parser);
7057 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
7058 objc_declare_protocols (list, attributes);
7062 tree id = c_parser_peek_token (parser)->value;
7063 tree proto = NULL_TREE;
7064 c_parser_consume_token (parser);
7065 if (c_parser_next_token_is (parser, CPP_LESS))
7066 proto = c_parser_objc_protocol_refs (parser);
7067 parser->objc_pq_context = true;
7068 objc_start_protocol (id, proto, attributes);
7069 c_parser_objc_methodprotolist (parser);
7070 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
7071 parser->objc_pq_context = false;
7072 objc_finish_interface ();
7076 /* Parse an objc-method-type.
7082 Return true if it is a class method (+) and false if it is
7083 an instance method (-).
7086 c_parser_objc_method_type (c_parser *parser)
7088 switch (c_parser_peek_token (parser)->type)
7091 c_parser_consume_token (parser);
7094 c_parser_consume_token (parser);
7101 /* Parse an objc-method-definition.
7103 objc-method-definition:
7104 objc-method-type objc-method-decl ;[opt] compound-statement
7108 c_parser_objc_method_definition (c_parser *parser)
7110 bool is_class_method = c_parser_objc_method_type (parser);
7111 tree decl, attributes = NULL_TREE;
7112 parser->objc_pq_context = true;
7113 decl = c_parser_objc_method_decl (parser, is_class_method, &attributes);
7114 if (decl == error_mark_node)
7115 return; /* Bail here. */
7117 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
7119 c_parser_consume_token (parser);
7120 pedwarn (c_parser_peek_token (parser)->location, OPT_pedantic,
7121 "extra semicolon in method definition specified");
7124 if (!c_parser_next_token_is (parser, CPP_OPEN_BRACE))
7126 c_parser_error (parser, "expected %<{%>");
7130 parser->objc_pq_context = false;
7131 if (objc_start_method_definition (is_class_method, decl, attributes))
7133 add_stmt (c_parser_compound_statement (parser));
7134 objc_finish_method_definition (current_function_decl);
7138 /* This code is executed when we find a method definition
7139 outside of an @implementation context (or invalid for other
7140 reasons). Parse the method (to keep going) but do not emit
7143 c_parser_compound_statement (parser);
7147 /* Parse an objc-methodprotolist.
7149 objc-methodprotolist:
7151 objc-methodprotolist objc-methodproto
7152 objc-methodprotolist declaration
7153 objc-methodprotolist ;
7157 The declaration is a data definition, which may be missing
7158 declaration specifiers under the same rules and diagnostics as
7159 other data definitions outside functions, and the stray semicolon
7160 is diagnosed the same way as a stray semicolon outside a
7164 c_parser_objc_methodprotolist (c_parser *parser)
7168 /* The list is terminated by @end. */
7169 switch (c_parser_peek_token (parser)->type)
7172 pedwarn (c_parser_peek_token (parser)->location, OPT_pedantic,
7173 "ISO C does not allow extra %<;%> outside of a function");
7174 c_parser_consume_token (parser);
7178 c_parser_objc_methodproto (parser);
7181 c_parser_pragma (parser, pragma_external);
7186 if (c_parser_next_token_is_keyword (parser, RID_AT_END))
7188 else if (c_parser_next_token_is_keyword (parser, RID_AT_PROPERTY))
7189 c_parser_objc_at_property_declaration (parser);
7190 else if (c_parser_next_token_is_keyword (parser, RID_AT_OPTIONAL))
7192 objc_set_method_opt (true);
7193 c_parser_consume_token (parser);
7195 else if (c_parser_next_token_is_keyword (parser, RID_AT_REQUIRED))
7197 objc_set_method_opt (false);
7198 c_parser_consume_token (parser);
7201 c_parser_declaration_or_fndef (parser, false, false, true,
7208 /* Parse an objc-methodproto.
7211 objc-method-type objc-method-decl ;
7215 c_parser_objc_methodproto (c_parser *parser)
7217 bool is_class_method = c_parser_objc_method_type (parser);
7218 tree decl, attributes = NULL_TREE;
7220 /* Remember protocol qualifiers in prototypes. */
7221 parser->objc_pq_context = true;
7222 decl = c_parser_objc_method_decl (parser, is_class_method, &attributes);
7223 /* Forget protocol qualifiers now. */
7224 parser->objc_pq_context = false;
7226 /* Do not allow the presence of attributes to hide an erroneous
7227 method implementation in the interface section. */
7228 if (!c_parser_next_token_is (parser, CPP_SEMICOLON))
7230 c_parser_error (parser, "expected %<;%>");
7234 if (decl != error_mark_node)
7235 objc_add_method_declaration (is_class_method, decl, attributes);
7237 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
7240 /* If we are at a position that method attributes may be present, check that
7241 there are not any parsed already (a syntax error) and then collect any
7242 specified at the current location. Finally, if new attributes were present,
7243 check that the next token is legal ( ';' for decls and '{' for defs). */
7246 c_parser_objc_maybe_method_attributes (c_parser* parser, tree* attributes)
7251 c_parser_error (parser,
7252 "method attributes must be specified at the end only");
7253 *attributes = NULL_TREE;
7257 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
7258 *attributes = c_parser_attributes (parser);
7260 /* If there were no attributes here, just report any earlier error. */
7261 if (*attributes == NULL_TREE || bad)
7264 /* If the attributes are followed by a ; or {, then just report any earlier
7266 if (c_parser_next_token_is (parser, CPP_SEMICOLON)
7267 || c_parser_next_token_is (parser, CPP_OPEN_BRACE))
7270 /* We've got attributes, but not at the end. */
7271 c_parser_error (parser,
7272 "expected %<;%> or %<{%> after method attribute definition");
7276 /* Parse an objc-method-decl.
7279 ( objc-type-name ) objc-selector
7281 ( objc-type-name ) objc-keyword-selector objc-optparmlist
7282 objc-keyword-selector objc-optparmlist
7285 objc-keyword-selector:
7287 objc-keyword-selector objc-keyword-decl
7290 objc-selector : ( objc-type-name ) identifier
7291 objc-selector : identifier
7292 : ( objc-type-name ) identifier
7296 objc-optparms objc-optellipsis
7300 objc-opt-parms , parameter-declaration
7308 c_parser_objc_method_decl (c_parser *parser, bool is_class_method, tree *attributes)
7310 tree type = NULL_TREE;
7312 tree parms = NULL_TREE;
7313 bool ellipsis = false;
7314 bool attr_err = false;
7316 *attributes = NULL_TREE;
7317 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
7319 c_parser_consume_token (parser);
7320 type = c_parser_objc_type_name (parser);
7321 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
7323 sel = c_parser_objc_selector (parser);
7324 /* If there is no selector, or a colon follows, we have an
7325 objc-keyword-selector. If there is a selector, and a colon does
7326 not follow, that selector ends the objc-method-decl. */
7327 if (!sel || c_parser_next_token_is (parser, CPP_COLON))
7330 tree list = NULL_TREE;
7333 tree atype = NULL_TREE, id, keyworddecl;
7334 tree param_attr = NULL_TREE;
7335 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
7337 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
7339 c_parser_consume_token (parser);
7340 atype = c_parser_objc_type_name (parser);
7341 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7344 /* New ObjC allows attributes on method parameters. */
7345 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
7346 param_attr = c_parser_attributes (parser);
7347 if (c_parser_next_token_is_not (parser, CPP_NAME))
7349 c_parser_error (parser, "expected identifier");
7350 return error_mark_node;
7352 id = c_parser_peek_token (parser)->value;
7353 c_parser_consume_token (parser);
7354 keyworddecl = objc_build_keyword_decl (tsel, atype, id, param_attr);
7355 list = chainon (list, keyworddecl);
7356 tsel = c_parser_objc_selector (parser);
7357 if (!tsel && c_parser_next_token_is_not (parser, CPP_COLON))
7361 attr_err |= c_parser_objc_maybe_method_attributes (parser, attributes) ;
7363 /* Parse the optional parameter list. Optional Objective-C
7364 method parameters follow the C syntax, and may include '...'
7365 to denote a variable number of arguments. */
7366 parms = make_node (TREE_LIST);
7367 while (c_parser_next_token_is (parser, CPP_COMMA))
7369 struct c_parm *parm;
7370 c_parser_consume_token (parser);
7371 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
7374 c_parser_consume_token (parser);
7375 attr_err |= c_parser_objc_maybe_method_attributes
7376 (parser, attributes) ;
7379 parm = c_parser_parameter_declaration (parser, NULL_TREE);
7382 parms = chainon (parms,
7383 build_tree_list (NULL_TREE, grokparm (parm)));
7388 attr_err |= c_parser_objc_maybe_method_attributes (parser, attributes) ;
7392 c_parser_error (parser, "objective-c method declaration is expected");
7393 return error_mark_node;
7397 return error_mark_node;
7399 return objc_build_method_signature (is_class_method, type, sel, parms, ellipsis);
7402 /* Parse an objc-type-name.
7405 objc-type-qualifiers[opt] type-name
7406 objc-type-qualifiers[opt]
7408 objc-type-qualifiers:
7410 objc-type-qualifiers objc-type-qualifier
7412 objc-type-qualifier: one of
7413 in out inout bycopy byref oneway
7417 c_parser_objc_type_name (c_parser *parser)
7419 tree quals = NULL_TREE;
7420 struct c_type_name *type_name = NULL;
7421 tree type = NULL_TREE;
7424 c_token *token = c_parser_peek_token (parser);
7425 if (token->type == CPP_KEYWORD
7426 && (token->keyword == RID_IN
7427 || token->keyword == RID_OUT
7428 || token->keyword == RID_INOUT
7429 || token->keyword == RID_BYCOPY
7430 || token->keyword == RID_BYREF
7431 || token->keyword == RID_ONEWAY))
7433 quals = chainon (build_tree_list (NULL_TREE, token->value), quals);
7434 c_parser_consume_token (parser);
7439 if (c_parser_next_token_starts_typename (parser))
7440 type_name = c_parser_type_name (parser);
7442 type = groktypename (type_name, NULL, NULL);
7443 return build_tree_list (quals, type);
7446 /* Parse objc-protocol-refs.
7453 c_parser_objc_protocol_refs (c_parser *parser)
7455 tree list = NULL_TREE;
7456 gcc_assert (c_parser_next_token_is (parser, CPP_LESS));
7457 c_parser_consume_token (parser);
7458 /* Any identifiers, including those declared as type names, are OK
7463 if (c_parser_next_token_is_not (parser, CPP_NAME))
7465 c_parser_error (parser, "expected identifier");
7468 id = c_parser_peek_token (parser)->value;
7469 list = chainon (list, build_tree_list (NULL_TREE, id));
7470 c_parser_consume_token (parser);
7471 if (c_parser_next_token_is (parser, CPP_COMMA))
7472 c_parser_consume_token (parser);
7476 c_parser_require (parser, CPP_GREATER, "expected %<>%>");
7480 /* Parse an objc-try-catch-finally-statement.
7482 objc-try-catch-finally-statement:
7483 @try compound-statement objc-catch-list[opt]
7484 @try compound-statement objc-catch-list[opt] @finally compound-statement
7487 @catch ( objc-catch-parameter-declaration ) compound-statement
7488 objc-catch-list @catch ( objc-catch-parameter-declaration ) compound-statement
7490 objc-catch-parameter-declaration:
7491 parameter-declaration
7494 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
7496 PS: This function is identical to cp_parser_objc_try_catch_finally_statement
7497 for C++. Keep them in sync. */
7500 c_parser_objc_try_catch_finally_statement (c_parser *parser)
7502 location_t location;
7505 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_TRY));
7506 c_parser_consume_token (parser);
7507 location = c_parser_peek_token (parser)->location;
7508 stmt = c_parser_compound_statement (parser);
7509 objc_begin_try_stmt (location, stmt);
7511 while (c_parser_next_token_is_keyword (parser, RID_AT_CATCH))
7513 struct c_parm *parm;
7514 tree parameter_declaration = error_mark_node;
7515 bool seen_open_paren = false;
7517 c_parser_consume_token (parser);
7518 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7519 seen_open_paren = true;
7520 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
7522 /* We have "@catch (...)" (where the '...' are literally
7523 what is in the code). Skip the '...'.
7524 parameter_declaration is set to NULL_TREE, and
7525 objc_being_catch_clauses() knows that that means
7527 c_parser_consume_token (parser);
7528 parameter_declaration = NULL_TREE;
7532 /* We have "@catch (NSException *exception)" or something
7533 like that. Parse the parameter declaration. */
7534 parm = c_parser_parameter_declaration (parser, NULL_TREE);
7536 parameter_declaration = error_mark_node;
7538 parameter_declaration = grokparm (parm);
7540 if (seen_open_paren)
7541 c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
7544 /* If there was no open parenthesis, we are recovering from
7545 an error, and we are trying to figure out what mistake
7546 the user has made. */
7548 /* If there is an immediate closing parenthesis, the user
7549 probably forgot the opening one (ie, they typed "@catch
7550 NSException *e)". Parse the closing parenthesis and keep
7552 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
7553 c_parser_consume_token (parser);
7555 /* If these is no immediate closing parenthesis, the user
7556 probably doesn't know that parenthesis are required at
7557 all (ie, they typed "@catch NSException *e"). So, just
7558 forget about the closing parenthesis and keep going. */
7560 objc_begin_catch_clause (parameter_declaration);
7561 if (c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
7562 c_parser_compound_statement_nostart (parser);
7563 objc_finish_catch_clause ();
7565 if (c_parser_next_token_is_keyword (parser, RID_AT_FINALLY))
7567 c_parser_consume_token (parser);
7568 location = c_parser_peek_token (parser)->location;
7569 stmt = c_parser_compound_statement (parser);
7570 objc_build_finally_clause (location, stmt);
7572 objc_finish_try_stmt ();
7575 /* Parse an objc-synchronized-statement.
7577 objc-synchronized-statement:
7578 @synchronized ( expression ) compound-statement
7582 c_parser_objc_synchronized_statement (c_parser *parser)
7586 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_SYNCHRONIZED));
7587 c_parser_consume_token (parser);
7588 loc = c_parser_peek_token (parser)->location;
7589 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7591 expr = c_parser_expression (parser).value;
7592 expr = c_fully_fold (expr, false, NULL);
7593 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
7596 expr = error_mark_node;
7597 stmt = c_parser_compound_statement (parser);
7598 objc_build_synchronized (loc, expr, stmt);
7601 /* Parse an objc-selector; return NULL_TREE without an error if the
7602 next token is not an objc-selector.
7607 enum struct union if else while do for switch case default
7608 break continue return goto asm sizeof typeof __alignof
7609 unsigned long const short volatile signed restrict _Complex
7610 in out inout bycopy byref oneway int char float double void _Bool
7612 ??? Why this selection of keywords but not, for example, storage
7613 class specifiers? */
7616 c_parser_objc_selector (c_parser *parser)
7618 c_token *token = c_parser_peek_token (parser);
7619 tree value = token->value;
7620 if (token->type == CPP_NAME)
7622 c_parser_consume_token (parser);
7625 if (token->type != CPP_KEYWORD)
7627 switch (token->keyword)
7669 c_parser_consume_token (parser);
7676 /* Parse an objc-selector-arg.
7680 objc-keywordname-list
7682 objc-keywordname-list:
7684 objc-keywordname-list objc-keywordname
7692 c_parser_objc_selector_arg (c_parser *parser)
7694 tree sel = c_parser_objc_selector (parser);
7695 tree list = NULL_TREE;
7696 if (sel && c_parser_next_token_is_not (parser, CPP_COLON))
7700 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
7702 list = chainon (list, build_tree_list (sel, NULL_TREE));
7703 sel = c_parser_objc_selector (parser);
7704 if (!sel && c_parser_next_token_is_not (parser, CPP_COLON))
7710 /* Parse an objc-receiver.
7719 c_parser_objc_receiver (c_parser *parser)
7721 if (c_parser_peek_token (parser)->type == CPP_NAME
7722 && (c_parser_peek_token (parser)->id_kind == C_ID_TYPENAME
7723 || c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME))
7725 tree id = c_parser_peek_token (parser)->value;
7726 c_parser_consume_token (parser);
7727 return objc_get_class_reference (id);
7729 return c_fully_fold (c_parser_expression (parser).value, false, NULL);
7732 /* Parse objc-message-args.
7736 objc-keywordarg-list
7738 objc-keywordarg-list:
7740 objc-keywordarg-list objc-keywordarg
7743 objc-selector : objc-keywordexpr
7748 c_parser_objc_message_args (c_parser *parser)
7750 tree sel = c_parser_objc_selector (parser);
7751 tree list = NULL_TREE;
7752 if (sel && c_parser_next_token_is_not (parser, CPP_COLON))
7757 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
7758 return error_mark_node;
7759 keywordexpr = c_parser_objc_keywordexpr (parser);
7760 list = chainon (list, build_tree_list (sel, keywordexpr));
7761 sel = c_parser_objc_selector (parser);
7762 if (!sel && c_parser_next_token_is_not (parser, CPP_COLON))
7768 /* Parse an objc-keywordexpr.
7775 c_parser_objc_keywordexpr (c_parser *parser)
7778 VEC(tree,gc) *expr_list = c_parser_expr_list (parser, true, true, NULL);
7779 if (VEC_length (tree, expr_list) == 1)
7781 /* Just return the expression, remove a level of
7783 ret = VEC_index (tree, expr_list, 0);
7787 /* We have a comma expression, we will collapse later. */
7788 ret = build_tree_list_vec (expr_list);
7790 release_tree_vector (expr_list);
7794 /* A check, needed in several places, that ObjC interface, implementation or
7795 method definitions are not prefixed by incorrect items. */
7797 c_parser_objc_diagnose_bad_element_prefix (c_parser *parser,
7798 struct c_declspecs *specs)
7800 if (!specs->declspecs_seen_p || specs->non_sc_seen_p
7801 || specs->typespec_kind != ctsk_none)
7803 c_parser_error (parser,
7804 "no type or storage class may be specified here,");
7805 c_parser_skip_to_end_of_block_or_statement (parser);
7811 /* Parse an Objective-C @property declaration. The syntax is:
7813 objc-property-declaration:
7814 '@property' objc-property-attributes[opt] struct-declaration ;
7816 objc-property-attributes:
7817 '(' objc-property-attribute-list ')'
7819 objc-property-attribute-list:
7820 objc-property-attribute
7821 objc-property-attribute-list, objc-property-attribute
7823 objc-property-attribute
7824 'getter' = identifier
7825 'setter' = identifier
7834 @property NSString *name;
7835 @property (readonly) id object;
7836 @property (retain, nonatomic, getter=getTheName) id name;
7837 @property int a, b, c;
7839 PS: This function is identical to cp_parser_objc_at_propery_declaration
7840 for C++. Keep them in sync. */
7842 c_parser_objc_at_property_declaration (c_parser *parser)
7844 /* The following variables hold the attributes of the properties as
7845 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
7846 seen. When we see an attribute, we set them to 'true' (if they
7847 are boolean properties) or to the identifier (if they have an
7848 argument, ie, for getter and setter). Note that here we only
7849 parse the list of attributes, check the syntax and accumulate the
7850 attributes that we find. objc_add_property_declaration() will
7851 then process the information. */
7852 bool property_assign = false;
7853 bool property_copy = false;
7854 tree property_getter_ident = NULL_TREE;
7855 bool property_nonatomic = false;
7856 bool property_readonly = false;
7857 bool property_readwrite = false;
7858 bool property_retain = false;
7859 tree property_setter_ident = NULL_TREE;
7861 /* 'properties' is the list of properties that we read. Usually a
7862 single one, but maybe more (eg, in "@property int a, b, c;" there
7867 loc = c_parser_peek_token (parser)->location;
7868 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_PROPERTY));
7870 c_parser_consume_token (parser); /* Eat '@property'. */
7872 /* Parse the optional attribute list... */
7873 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
7876 c_parser_consume_token (parser);
7878 /* Property attribute keywords are valid now. */
7879 parser->objc_property_attr_context = true;
7883 bool syntax_error = false;
7884 c_token *token = c_parser_peek_token (parser);
7887 if (token->type != CPP_KEYWORD)
7889 if (token->type == CPP_CLOSE_PAREN)
7890 c_parser_error (parser, "expected identifier");
7893 c_parser_consume_token (parser);
7894 c_parser_error (parser, "unknown property attribute");
7898 keyword = token->keyword;
7899 c_parser_consume_token (parser);
7902 case RID_ASSIGN: property_assign = true; break;
7903 case RID_COPY: property_copy = true; break;
7904 case RID_NONATOMIC: property_nonatomic = true; break;
7905 case RID_READONLY: property_readonly = true; break;
7906 case RID_READWRITE: property_readwrite = true; break;
7907 case RID_RETAIN: property_retain = true; break;
7911 if (c_parser_next_token_is_not (parser, CPP_EQ))
7913 c_parser_error (parser,
7914 "getter/setter attribute must be followed by %<=%>");
7915 syntax_error = true;
7918 c_parser_consume_token (parser); /* eat the = */
7919 if (c_parser_next_token_is_not (parser, CPP_NAME))
7921 c_parser_error (parser, "expected identifier");
7922 syntax_error = true;
7925 if (keyword == RID_SETTER)
7927 if (property_setter_ident != NULL_TREE)
7928 c_parser_error (parser, "the %<setter%> attribute may only be specified once");
7930 property_setter_ident = c_parser_peek_token (parser)->value;
7931 c_parser_consume_token (parser);
7932 if (c_parser_next_token_is_not (parser, CPP_COLON))
7933 c_parser_error (parser, "setter name must terminate with %<:%>");
7935 c_parser_consume_token (parser);
7939 if (property_getter_ident != NULL_TREE)
7940 c_parser_error (parser, "the %<getter%> attribute may only be specified once");
7942 property_getter_ident = c_parser_peek_token (parser)->value;
7943 c_parser_consume_token (parser);
7947 c_parser_error (parser, "unknown property attribute");
7948 syntax_error = true;
7955 if (c_parser_next_token_is (parser, CPP_COMMA))
7956 c_parser_consume_token (parser);
7960 parser->objc_property_attr_context = false;
7961 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
7963 /* ... and the property declaration(s). */
7964 properties = c_parser_struct_declaration (parser);
7966 if (properties == error_mark_node)
7968 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
7969 parser->error = false;
7973 if (properties == NULL_TREE)
7974 c_parser_error (parser, "expected identifier");
7977 /* Comma-separated properties are chained together in
7978 reverse order; add them one by one. */
7979 properties = nreverse (properties);
7981 for (; properties; properties = TREE_CHAIN (properties))
7982 objc_add_property_declaration (loc, copy_node (properties),
7983 property_readonly, property_readwrite,
7984 property_assign, property_retain,
7985 property_copy, property_nonatomic,
7986 property_getter_ident, property_setter_ident);
7989 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
7990 parser->error = false;
7993 /* Parse an Objective-C @synthesize declaration. The syntax is:
7995 objc-synthesize-declaration:
7996 @synthesize objc-synthesize-identifier-list ;
7998 objc-synthesize-identifier-list:
7999 objc-synthesize-identifier
8000 objc-synthesize-identifier-list, objc-synthesize-identifier
8002 objc-synthesize-identifier
8004 identifier = identifier
8007 @synthesize MyProperty;
8008 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
8010 PS: This function is identical to cp_parser_objc_at_synthesize_declaration
8011 for C++. Keep them in sync.
8014 c_parser_objc_at_synthesize_declaration (c_parser *parser)
8016 tree list = NULL_TREE;
8018 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_SYNTHESIZE));
8019 loc = c_parser_peek_token (parser)->location;
8021 c_parser_consume_token (parser);
8024 tree property, ivar;
8025 if (c_parser_next_token_is_not (parser, CPP_NAME))
8027 c_parser_error (parser, "expected identifier");
8028 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
8029 /* Once we find the semicolon, we can resume normal parsing.
8030 We have to reset parser->error manually because
8031 c_parser_skip_until_found() won't reset it for us if the
8032 next token is precisely a semicolon. */
8033 parser->error = false;
8036 property = c_parser_peek_token (parser)->value;
8037 c_parser_consume_token (parser);
8038 if (c_parser_next_token_is (parser, CPP_EQ))
8040 c_parser_consume_token (parser);
8041 if (c_parser_next_token_is_not (parser, CPP_NAME))
8043 c_parser_error (parser, "expected identifier");
8044 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
8045 parser->error = false;
8048 ivar = c_parser_peek_token (parser)->value;
8049 c_parser_consume_token (parser);
8053 list = chainon (list, build_tree_list (ivar, property));
8054 if (c_parser_next_token_is (parser, CPP_COMMA))
8055 c_parser_consume_token (parser);
8059 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
8060 objc_add_synthesize_declaration (loc, list);
8063 /* Parse an Objective-C @dynamic declaration. The syntax is:
8065 objc-dynamic-declaration:
8066 @dynamic identifier-list ;
8069 @dynamic MyProperty;
8070 @dynamic MyProperty, AnotherProperty;
8072 PS: This function is identical to cp_parser_objc_at_dynamic_declaration
8073 for C++. Keep them in sync.
8076 c_parser_objc_at_dynamic_declaration (c_parser *parser)
8078 tree list = NULL_TREE;
8080 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_DYNAMIC));
8081 loc = c_parser_peek_token (parser)->location;
8083 c_parser_consume_token (parser);
8087 if (c_parser_next_token_is_not (parser, CPP_NAME))
8089 c_parser_error (parser, "expected identifier");
8090 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
8091 parser->error = false;
8094 property = c_parser_peek_token (parser)->value;
8095 list = chainon (list, build_tree_list (NULL_TREE, property));
8096 c_parser_consume_token (parser);
8097 if (c_parser_next_token_is (parser, CPP_COMMA))
8098 c_parser_consume_token (parser);
8102 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
8103 objc_add_dynamic_declaration (loc, list);
8107 /* Handle pragmas. Some OpenMP pragmas are associated with, and therefore
8108 should be considered, statements. ALLOW_STMT is true if we're within
8109 the context of a function and such pragmas are to be allowed. Returns
8110 true if we actually parsed such a pragma. */
8113 c_parser_pragma (c_parser *parser, enum pragma_context context)
8117 id = c_parser_peek_token (parser)->pragma_kind;
8118 gcc_assert (id != PRAGMA_NONE);
8122 case PRAGMA_OMP_BARRIER:
8123 if (context != pragma_compound)
8125 if (context == pragma_stmt)
8126 c_parser_error (parser, "%<#pragma omp barrier%> may only be "
8127 "used in compound statements");
8130 c_parser_omp_barrier (parser);
8133 case PRAGMA_OMP_FLUSH:
8134 if (context != pragma_compound)
8136 if (context == pragma_stmt)
8137 c_parser_error (parser, "%<#pragma omp flush%> may only be "
8138 "used in compound statements");
8141 c_parser_omp_flush (parser);
8144 case PRAGMA_OMP_TASKWAIT:
8145 if (context != pragma_compound)
8147 if (context == pragma_stmt)
8148 c_parser_error (parser, "%<#pragma omp taskwait%> may only be "
8149 "used in compound statements");
8152 c_parser_omp_taskwait (parser);
8155 case PRAGMA_OMP_THREADPRIVATE:
8156 c_parser_omp_threadprivate (parser);
8159 case PRAGMA_OMP_SECTION:
8160 error_at (c_parser_peek_token (parser)->location,
8161 "%<#pragma omp section%> may only be used in "
8162 "%<#pragma omp sections%> construct");
8163 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
8166 case PRAGMA_GCC_PCH_PREPROCESS:
8167 c_parser_error (parser, "%<#pragma GCC pch_preprocess%> must be first");
8168 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
8172 if (id < PRAGMA_FIRST_EXTERNAL)
8174 if (context == pragma_external)
8177 c_parser_error (parser, "expected declaration specifiers");
8178 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
8181 c_parser_omp_construct (parser);
8187 c_parser_consume_pragma (parser);
8188 c_invoke_pragma_handler (id);
8190 /* Skip to EOL, but suppress any error message. Those will have been
8191 generated by the handler routine through calling error, as opposed
8192 to calling c_parser_error. */
8193 parser->error = true;
8194 c_parser_skip_to_pragma_eol (parser);
8199 /* The interface the pragma parsers have to the lexer. */
8202 pragma_lex (tree *value)
8204 c_token *tok = c_parser_peek_token (the_parser);
8205 enum cpp_ttype ret = tok->type;
8207 *value = tok->value;
8208 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
8212 if (ret == CPP_KEYWORD)
8214 c_parser_consume_token (the_parser);
8221 c_parser_pragma_pch_preprocess (c_parser *parser)
8225 c_parser_consume_pragma (parser);
8226 if (c_parser_next_token_is (parser, CPP_STRING))
8228 name = c_parser_peek_token (parser)->value;
8229 c_parser_consume_token (parser);
8232 c_parser_error (parser, "expected string literal");
8233 c_parser_skip_to_pragma_eol (parser);
8236 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
8239 /* OpenMP 2.5 parsing routines. */
8241 /* Returns name of the next clause.
8242 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
8243 the token is not consumed. Otherwise appropriate pragma_omp_clause is
8244 returned and the token is consumed. */
8246 static pragma_omp_clause
8247 c_parser_omp_clause_name (c_parser *parser)
8249 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
8251 if (c_parser_next_token_is_keyword (parser, RID_IF))
8252 result = PRAGMA_OMP_CLAUSE_IF;
8253 else if (c_parser_next_token_is_keyword (parser, RID_DEFAULT))
8254 result = PRAGMA_OMP_CLAUSE_DEFAULT;
8255 else if (c_parser_next_token_is (parser, CPP_NAME))
8257 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
8262 if (!strcmp ("collapse", p))
8263 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
8264 else if (!strcmp ("copyin", p))
8265 result = PRAGMA_OMP_CLAUSE_COPYIN;
8266 else if (!strcmp ("copyprivate", p))
8267 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
8270 if (!strcmp ("firstprivate", p))
8271 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
8274 if (!strcmp ("lastprivate", p))
8275 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
8278 if (!strcmp ("nowait", p))
8279 result = PRAGMA_OMP_CLAUSE_NOWAIT;
8280 else if (!strcmp ("num_threads", p))
8281 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
8284 if (!strcmp ("ordered", p))
8285 result = PRAGMA_OMP_CLAUSE_ORDERED;
8288 if (!strcmp ("private", p))
8289 result = PRAGMA_OMP_CLAUSE_PRIVATE;
8292 if (!strcmp ("reduction", p))
8293 result = PRAGMA_OMP_CLAUSE_REDUCTION;
8296 if (!strcmp ("schedule", p))
8297 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
8298 else if (!strcmp ("shared", p))
8299 result = PRAGMA_OMP_CLAUSE_SHARED;
8302 if (!strcmp ("untied", p))
8303 result = PRAGMA_OMP_CLAUSE_UNTIED;
8308 if (result != PRAGMA_OMP_CLAUSE_NONE)
8309 c_parser_consume_token (parser);
8314 /* Validate that a clause of the given type does not already exist. */
8317 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
8322 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
8323 if (OMP_CLAUSE_CODE (c) == code)
8325 location_t loc = OMP_CLAUSE_LOCATION (c);
8326 error_at (loc, "too many %qs clauses", name);
8334 variable-list , identifier
8336 If KIND is nonzero, create the appropriate node and install the
8337 decl in OMP_CLAUSE_DECL and add the node to the head of the list.
8338 If KIND is nonzero, CLAUSE_LOC is the location of the clause.
8340 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
8341 return the list created. */
8344 c_parser_omp_variable_list (c_parser *parser,
8345 location_t clause_loc,
8346 enum omp_clause_code kind,
8349 if (c_parser_next_token_is_not (parser, CPP_NAME)
8350 || c_parser_peek_token (parser)->id_kind != C_ID_ID)
8351 c_parser_error (parser, "expected identifier");
8353 while (c_parser_next_token_is (parser, CPP_NAME)
8354 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
8356 tree t = lookup_name (c_parser_peek_token (parser)->value);
8359 undeclared_variable (c_parser_peek_token (parser)->location,
8360 c_parser_peek_token (parser)->value);
8361 else if (t == error_mark_node)
8365 tree u = build_omp_clause (clause_loc, kind);
8366 OMP_CLAUSE_DECL (u) = t;
8367 OMP_CLAUSE_CHAIN (u) = list;
8371 list = tree_cons (t, NULL_TREE, list);
8373 c_parser_consume_token (parser);
8375 if (c_parser_next_token_is_not (parser, CPP_COMMA))
8378 c_parser_consume_token (parser);
8384 /* Similarly, but expect leading and trailing parenthesis. This is a very
8385 common case for omp clauses. */
8388 c_parser_omp_var_list_parens (c_parser *parser, enum omp_clause_code kind,
8391 /* The clauses location. */
8392 location_t loc = c_parser_peek_token (parser)->location;
8394 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
8396 list = c_parser_omp_variable_list (parser, loc, kind, list);
8397 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
8403 collapse ( constant-expression ) */
8406 c_parser_omp_clause_collapse (c_parser *parser, tree list)
8408 tree c, num = error_mark_node;
8412 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse");
8414 loc = c_parser_peek_token (parser)->location;
8415 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
8417 num = c_parser_expr_no_commas (parser, NULL).value;
8418 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
8420 if (num == error_mark_node)
8422 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
8423 || !host_integerp (num, 0)
8424 || (n = tree_low_cst (num, 0)) <= 0
8428 "collapse argument needs positive constant integer expression");
8431 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
8432 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
8433 OMP_CLAUSE_CHAIN (c) = list;
8438 copyin ( variable-list ) */
8441 c_parser_omp_clause_copyin (c_parser *parser, tree list)
8443 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_COPYIN, list);
8447 copyprivate ( variable-list ) */
8450 c_parser_omp_clause_copyprivate (c_parser *parser, tree list)
8452 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_COPYPRIVATE, list);
8456 default ( shared | none ) */
8459 c_parser_omp_clause_default (c_parser *parser, tree list)
8461 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
8462 location_t loc = c_parser_peek_token (parser)->location;
8465 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
8467 if (c_parser_next_token_is (parser, CPP_NAME))
8469 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
8474 if (strcmp ("none", p) != 0)
8476 kind = OMP_CLAUSE_DEFAULT_NONE;
8480 if (strcmp ("shared", p) != 0)
8482 kind = OMP_CLAUSE_DEFAULT_SHARED;
8489 c_parser_consume_token (parser);
8494 c_parser_error (parser, "expected %<none%> or %<shared%>");
8496 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
8498 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
8501 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default");
8502 c = build_omp_clause (loc, OMP_CLAUSE_DEFAULT);
8503 OMP_CLAUSE_CHAIN (c) = list;
8504 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
8510 firstprivate ( variable-list ) */
8513 c_parser_omp_clause_firstprivate (c_parser *parser, tree list)
8515 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_FIRSTPRIVATE, list);
8519 if ( expression ) */
8522 c_parser_omp_clause_if (c_parser *parser, tree list)
8524 location_t loc = c_parser_peek_token (parser)->location;
8525 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
8527 tree t = c_parser_paren_condition (parser);
8530 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if");
8532 c = build_omp_clause (loc, OMP_CLAUSE_IF);
8533 OMP_CLAUSE_IF_EXPR (c) = t;
8534 OMP_CLAUSE_CHAIN (c) = list;
8538 c_parser_error (parser, "expected %<(%>");
8544 lastprivate ( variable-list ) */
8547 c_parser_omp_clause_lastprivate (c_parser *parser, tree list)
8549 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_LASTPRIVATE, list);
8556 c_parser_omp_clause_nowait (c_parser *parser ATTRIBUTE_UNUSED, tree list)
8559 location_t loc = c_parser_peek_token (parser)->location;
8561 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait");
8563 c = build_omp_clause (loc, OMP_CLAUSE_NOWAIT);
8564 OMP_CLAUSE_CHAIN (c) = list;
8569 num_threads ( expression ) */
8572 c_parser_omp_clause_num_threads (c_parser *parser, tree list)
8574 location_t num_threads_loc = c_parser_peek_token (parser)->location;
8575 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
8577 location_t expr_loc = c_parser_peek_token (parser)->location;
8578 tree c, t = c_parser_expression (parser).value;
8579 t = c_fully_fold (t, false, NULL);
8581 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
8583 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
8585 c_parser_error (parser, "expected integer expression");
8589 /* Attempt to statically determine when the number isn't positive. */
8590 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
8591 build_int_cst (TREE_TYPE (t), 0));
8592 if (CAN_HAVE_LOCATION_P (c))
8593 SET_EXPR_LOCATION (c, expr_loc);
8594 if (c == boolean_true_node)
8596 warning_at (expr_loc, 0,
8597 "%<num_threads%> value must be positive");
8598 t = integer_one_node;
8601 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS, "num_threads");
8603 c = build_omp_clause (num_threads_loc, OMP_CLAUSE_NUM_THREADS);
8604 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
8605 OMP_CLAUSE_CHAIN (c) = list;
8616 c_parser_omp_clause_ordered (c_parser *parser, tree list)
8620 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED, "ordered");
8622 c = build_omp_clause (c_parser_peek_token (parser)->location,
8623 OMP_CLAUSE_ORDERED);
8624 OMP_CLAUSE_CHAIN (c) = list;
8630 private ( variable-list ) */
8633 c_parser_omp_clause_private (c_parser *parser, tree list)
8635 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_PRIVATE, list);
8639 reduction ( reduction-operator : variable-list )
8642 One of: + * - & ^ | && || */
8645 c_parser_omp_clause_reduction (c_parser *parser, tree list)
8647 location_t clause_loc = c_parser_peek_token (parser)->location;
8648 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
8650 enum tree_code code;
8652 switch (c_parser_peek_token (parser)->type)
8664 code = BIT_AND_EXPR;
8667 code = BIT_XOR_EXPR;
8670 code = BIT_IOR_EXPR;
8673 code = TRUTH_ANDIF_EXPR;
8676 code = TRUTH_ORIF_EXPR;
8679 c_parser_error (parser,
8680 "expected %<+%>, %<*%>, %<-%>, %<&%>, "
8681 "%<^%>, %<|%>, %<&&%>, or %<||%>");
8682 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 0);
8685 c_parser_consume_token (parser);
8686 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
8690 nl = c_parser_omp_variable_list (parser, clause_loc,
8691 OMP_CLAUSE_REDUCTION, list);
8692 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
8693 OMP_CLAUSE_REDUCTION_CODE (c) = code;
8697 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
8703 schedule ( schedule-kind )
8704 schedule ( schedule-kind , expression )
8707 static | dynamic | guided | runtime | auto
8711 c_parser_omp_clause_schedule (c_parser *parser, tree list)
8714 location_t loc = c_parser_peek_token (parser)->location;
8716 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
8719 c = build_omp_clause (loc, OMP_CLAUSE_SCHEDULE);
8721 if (c_parser_next_token_is (parser, CPP_NAME))
8723 tree kind = c_parser_peek_token (parser)->value;
8724 const char *p = IDENTIFIER_POINTER (kind);
8729 if (strcmp ("dynamic", p) != 0)
8731 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
8735 if (strcmp ("guided", p) != 0)
8737 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
8741 if (strcmp ("runtime", p) != 0)
8743 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
8750 else if (c_parser_next_token_is_keyword (parser, RID_STATIC))
8751 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
8752 else if (c_parser_next_token_is_keyword (parser, RID_AUTO))
8753 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
8757 c_parser_consume_token (parser);
8758 if (c_parser_next_token_is (parser, CPP_COMMA))
8761 c_parser_consume_token (parser);
8763 here = c_parser_peek_token (parser)->location;
8764 t = c_parser_expr_no_commas (parser, NULL).value;
8765 t = c_fully_fold (t, false, NULL);
8767 if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
8768 error_at (here, "schedule %<runtime%> does not take "
8769 "a %<chunk_size%> parameter");
8770 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
8772 "schedule %<auto%> does not take "
8773 "a %<chunk_size%> parameter");
8774 else if (TREE_CODE (TREE_TYPE (t)) == INTEGER_TYPE)
8775 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
8777 c_parser_error (parser, "expected integer expression");
8779 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
8782 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
8783 "expected %<,%> or %<)%>");
8785 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule");
8786 OMP_CLAUSE_CHAIN (c) = list;
8790 c_parser_error (parser, "invalid schedule kind");
8791 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 0);
8796 shared ( variable-list ) */
8799 c_parser_omp_clause_shared (c_parser *parser, tree list)
8801 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_SHARED, list);
8808 c_parser_omp_clause_untied (c_parser *parser ATTRIBUTE_UNUSED, tree list)
8812 /* FIXME: Should we allow duplicates? */
8813 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied");
8815 c = build_omp_clause (c_parser_peek_token (parser)->location,
8817 OMP_CLAUSE_CHAIN (c) = list;
8822 /* Parse all OpenMP clauses. The set clauses allowed by the directive
8823 is a bitmask in MASK. Return the list of clauses found; the result
8824 of clause default goes in *pdefault. */
8827 c_parser_omp_all_clauses (c_parser *parser, unsigned int mask,
8830 tree clauses = NULL;
8833 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
8836 pragma_omp_clause c_kind;
8838 tree prev = clauses;
8840 if (!first && c_parser_next_token_is (parser, CPP_COMMA))
8841 c_parser_consume_token (parser);
8844 here = c_parser_peek_token (parser)->location;
8845 c_kind = c_parser_omp_clause_name (parser);
8849 case PRAGMA_OMP_CLAUSE_COLLAPSE:
8850 clauses = c_parser_omp_clause_collapse (parser, clauses);
8851 c_name = "collapse";
8853 case PRAGMA_OMP_CLAUSE_COPYIN:
8854 clauses = c_parser_omp_clause_copyin (parser, clauses);
8857 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
8858 clauses = c_parser_omp_clause_copyprivate (parser, clauses);
8859 c_name = "copyprivate";
8861 case PRAGMA_OMP_CLAUSE_DEFAULT:
8862 clauses = c_parser_omp_clause_default (parser, clauses);
8865 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
8866 clauses = c_parser_omp_clause_firstprivate (parser, clauses);
8867 c_name = "firstprivate";
8869 case PRAGMA_OMP_CLAUSE_IF:
8870 clauses = c_parser_omp_clause_if (parser, clauses);
8873 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
8874 clauses = c_parser_omp_clause_lastprivate (parser, clauses);
8875 c_name = "lastprivate";
8877 case PRAGMA_OMP_CLAUSE_NOWAIT:
8878 clauses = c_parser_omp_clause_nowait (parser, clauses);
8881 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
8882 clauses = c_parser_omp_clause_num_threads (parser, clauses);
8883 c_name = "num_threads";
8885 case PRAGMA_OMP_CLAUSE_ORDERED:
8886 clauses = c_parser_omp_clause_ordered (parser, clauses);
8889 case PRAGMA_OMP_CLAUSE_PRIVATE:
8890 clauses = c_parser_omp_clause_private (parser, clauses);
8893 case PRAGMA_OMP_CLAUSE_REDUCTION:
8894 clauses = c_parser_omp_clause_reduction (parser, clauses);
8895 c_name = "reduction";
8897 case PRAGMA_OMP_CLAUSE_SCHEDULE:
8898 clauses = c_parser_omp_clause_schedule (parser, clauses);
8899 c_name = "schedule";
8901 case PRAGMA_OMP_CLAUSE_SHARED:
8902 clauses = c_parser_omp_clause_shared (parser, clauses);
8905 case PRAGMA_OMP_CLAUSE_UNTIED:
8906 clauses = c_parser_omp_clause_untied (parser, clauses);
8910 c_parser_error (parser, "expected %<#pragma omp%> clause");
8914 if (((mask >> c_kind) & 1) == 0 && !parser->error)
8916 /* Remove the invalid clause(s) from the list to avoid
8917 confusing the rest of the compiler. */
8919 error_at (here, "%qs is not valid for %qs", c_name, where);
8924 c_parser_skip_to_pragma_eol (parser);
8926 return c_finish_omp_clauses (clauses);
8933 In practice, we're also interested in adding the statement to an
8934 outer node. So it is convenient if we work around the fact that
8935 c_parser_statement calls add_stmt. */
8938 c_parser_omp_structured_block (c_parser *parser)
8940 tree stmt = push_stmt_list ();
8941 c_parser_statement (parser);
8942 return pop_stmt_list (stmt);
8946 # pragma omp atomic new-line
8950 x binop= expr | x++ | ++x | x-- | --x
8952 +, *, -, /, &, ^, |, <<, >>
8954 where x is an lvalue expression with scalar type.
8956 LOC is the location of the #pragma token. */
8959 c_parser_omp_atomic (location_t loc, c_parser *parser)
8963 enum tree_code code;
8964 struct c_expr rhs_expr;
8966 c_parser_skip_to_pragma_eol (parser);
8968 lhs = c_parser_unary_expression (parser).value;
8969 lhs = c_fully_fold (lhs, false, NULL);
8970 switch (TREE_CODE (lhs))
8974 c_parser_skip_to_end_of_block_or_statement (parser);
8977 case PREINCREMENT_EXPR:
8978 case POSTINCREMENT_EXPR:
8979 lhs = TREE_OPERAND (lhs, 0);
8981 rhs = integer_one_node;
8984 case PREDECREMENT_EXPR:
8985 case POSTDECREMENT_EXPR:
8986 lhs = TREE_OPERAND (lhs, 0);
8988 rhs = integer_one_node;
8992 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
8993 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
8994 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
8995 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
8996 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
8997 (TREE_OPERAND (lhs, 1), 0), 0)))
8999 /* Undo effects of boolean_increment for post {in,de}crement. */
9000 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
9003 if (TREE_CODE (lhs) == MODIFY_EXPR
9004 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
9006 /* Undo effects of boolean_increment. */
9007 if (integer_onep (TREE_OPERAND (lhs, 1)))
9009 /* This is pre or post increment. */
9010 rhs = TREE_OPERAND (lhs, 1);
9011 lhs = TREE_OPERAND (lhs, 0);
9015 if (TREE_CODE (TREE_OPERAND (lhs, 1)) == TRUTH_NOT_EXPR
9016 && TREE_OPERAND (lhs, 0)
9017 == TREE_OPERAND (TREE_OPERAND (lhs, 1), 0))
9019 /* This is pre or post decrement. */
9020 rhs = TREE_OPERAND (lhs, 1);
9021 lhs = TREE_OPERAND (lhs, 0);
9028 switch (c_parser_peek_token (parser)->type)
9034 code = TRUNC_DIV_EXPR;
9049 code = BIT_AND_EXPR;
9052 code = BIT_IOR_EXPR;
9055 code = BIT_XOR_EXPR;
9058 c_parser_error (parser,
9059 "invalid operator for %<#pragma omp atomic%>");
9063 c_parser_consume_token (parser);
9065 location_t rhs_loc = c_parser_peek_token (parser)->location;
9066 rhs_expr = c_parser_expression (parser);
9067 rhs_expr = default_function_array_read_conversion (rhs_loc, rhs_expr);
9069 rhs = rhs_expr.value;
9070 rhs = c_fully_fold (rhs, false, NULL);
9073 stmt = c_finish_omp_atomic (loc, code, lhs, rhs);
9074 if (stmt != error_mark_node)
9076 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
9081 # pragma omp barrier new-line
9085 c_parser_omp_barrier (c_parser *parser)
9087 location_t loc = c_parser_peek_token (parser)->location;
9088 c_parser_consume_pragma (parser);
9089 c_parser_skip_to_pragma_eol (parser);
9091 c_finish_omp_barrier (loc);
9095 # pragma omp critical [(name)] new-line
9098 LOC is the location of the #pragma itself. */
9101 c_parser_omp_critical (location_t loc, c_parser *parser)
9103 tree stmt, name = NULL;
9105 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
9107 c_parser_consume_token (parser);
9108 if (c_parser_next_token_is (parser, CPP_NAME))
9110 name = c_parser_peek_token (parser)->value;
9111 c_parser_consume_token (parser);
9112 c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
9115 c_parser_error (parser, "expected identifier");
9117 else if (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
9118 c_parser_error (parser, "expected %<(%> or end of line");
9119 c_parser_skip_to_pragma_eol (parser);
9121 stmt = c_parser_omp_structured_block (parser);
9122 return c_finish_omp_critical (loc, stmt, name);
9126 # pragma omp flush flush-vars[opt] new-line
9129 ( variable-list ) */
9132 c_parser_omp_flush (c_parser *parser)
9134 location_t loc = c_parser_peek_token (parser)->location;
9135 c_parser_consume_pragma (parser);
9136 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
9137 c_parser_omp_var_list_parens (parser, OMP_CLAUSE_ERROR, NULL);
9138 else if (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
9139 c_parser_error (parser, "expected %<(%> or end of line");
9140 c_parser_skip_to_pragma_eol (parser);
9142 c_finish_omp_flush (loc);
9145 /* Parse the restricted form of the for statement allowed by OpenMP.
9146 The real trick here is to determine the loop control variable early
9147 so that we can push a new decl if necessary to make it private.
9148 LOC is the location of the OMP in "#pragma omp". */
9151 c_parser_omp_for_loop (location_t loc,
9152 c_parser *parser, tree clauses, tree *par_clauses)
9154 tree decl, cond, incr, save_break, save_cont, body, init, stmt, cl;
9155 tree declv, condv, incrv, initv, ret = NULL;
9156 bool fail = false, open_brace_parsed = false;
9157 int i, collapse = 1, nbraces = 0;
9159 VEC(tree,gc) *for_block = make_tree_vector ();
9161 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
9162 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
9163 collapse = tree_low_cst (OMP_CLAUSE_COLLAPSE_EXPR (cl), 0);
9165 gcc_assert (collapse >= 1);
9167 declv = make_tree_vec (collapse);
9168 initv = make_tree_vec (collapse);
9169 condv = make_tree_vec (collapse);
9170 incrv = make_tree_vec (collapse);
9172 if (!c_parser_next_token_is_keyword (parser, RID_FOR))
9174 c_parser_error (parser, "for statement expected");
9177 for_loc = c_parser_peek_token (parser)->location;
9178 c_parser_consume_token (parser);
9180 for (i = 0; i < collapse; i++)
9184 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
9187 /* Parse the initialization declaration or expression. */
9188 if (c_parser_next_tokens_start_declaration (parser))
9191 VEC_safe_push (tree, gc, for_block, c_begin_compound_stmt (true));
9192 c_parser_declaration_or_fndef (parser, true, true, true, true, true, NULL);
9193 decl = check_for_loop_decls (for_loc, flag_isoc99);
9196 if (DECL_INITIAL (decl) == error_mark_node)
9197 decl = error_mark_node;
9200 else if (c_parser_next_token_is (parser, CPP_NAME)
9201 && c_parser_peek_2nd_token (parser)->type == CPP_EQ)
9203 struct c_expr decl_exp;
9204 struct c_expr init_exp;
9205 location_t init_loc;
9207 decl_exp = c_parser_postfix_expression (parser);
9208 decl = decl_exp.value;
9210 c_parser_require (parser, CPP_EQ, "expected %<=%>");
9212 init_loc = c_parser_peek_token (parser)->location;
9213 init_exp = c_parser_expr_no_commas (parser, NULL);
9214 init_exp = default_function_array_read_conversion (init_loc,
9216 init = build_modify_expr (init_loc, decl, decl_exp.original_type,
9217 NOP_EXPR, init_loc, init_exp.value,
9218 init_exp.original_type);
9219 init = c_process_expr_stmt (init_loc, init);
9221 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
9226 c_parser_error (parser,
9227 "expected iteration declaration or initialization");
9228 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
9234 /* Parse the loop condition. */
9236 if (c_parser_next_token_is_not (parser, CPP_SEMICOLON))
9238 location_t cond_loc = c_parser_peek_token (parser)->location;
9239 struct c_expr cond_expr = c_parser_binary_expression (parser, NULL);
9241 cond = cond_expr.value;
9242 cond = c_objc_common_truthvalue_conversion (cond_loc, cond);
9243 cond = c_fully_fold (cond, false, NULL);
9244 switch (cond_expr.original_code)
9252 /* Can't be cond = error_mark_node, because we want to preserve
9253 the location until c_finish_omp_for. */
9254 cond = build1 (NOP_EXPR, boolean_type_node, error_mark_node);
9257 protected_set_expr_location (cond, cond_loc);
9259 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
9261 /* Parse the increment expression. */
9263 if (c_parser_next_token_is_not (parser, CPP_CLOSE_PAREN))
9265 location_t incr_loc = c_parser_peek_token (parser)->location;
9267 incr = c_process_expr_stmt (incr_loc,
9268 c_parser_expression (parser).value);
9270 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
9272 if (decl == NULL || decl == error_mark_node || init == error_mark_node)
9276 TREE_VEC_ELT (declv, i) = decl;
9277 TREE_VEC_ELT (initv, i) = init;
9278 TREE_VEC_ELT (condv, i) = cond;
9279 TREE_VEC_ELT (incrv, i) = incr;
9283 if (i == collapse - 1)
9286 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
9287 in between the collapsed for loops to be still considered perfectly
9288 nested. Hopefully the final version clarifies this.
9289 For now handle (multiple) {'s and empty statements. */
9292 if (c_parser_next_token_is_keyword (parser, RID_FOR))
9294 c_parser_consume_token (parser);
9297 else if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
9299 c_parser_consume_token (parser);
9303 && c_parser_next_token_is (parser, CPP_SEMICOLON))
9304 c_parser_consume_token (parser);
9307 c_parser_error (parser, "not enough perfectly nested loops");
9310 open_brace_parsed = true;
9320 nbraces += bracecount;
9323 save_break = c_break_label;
9324 c_break_label = size_one_node;
9325 save_cont = c_cont_label;
9326 c_cont_label = NULL_TREE;
9327 body = push_stmt_list ();
9329 if (open_brace_parsed)
9331 location_t here = c_parser_peek_token (parser)->location;
9332 stmt = c_begin_compound_stmt (true);
9333 c_parser_compound_statement_nostart (parser);
9334 add_stmt (c_end_compound_stmt (here, stmt, true));
9337 add_stmt (c_parser_c99_block_statement (parser));
9340 tree t = build1 (LABEL_EXPR, void_type_node, c_cont_label);
9341 SET_EXPR_LOCATION (t, loc);
9345 body = pop_stmt_list (body);
9346 c_break_label = save_break;
9347 c_cont_label = save_cont;
9351 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
9353 c_parser_consume_token (parser);
9356 else if (c_parser_next_token_is (parser, CPP_SEMICOLON))
9357 c_parser_consume_token (parser);
9360 c_parser_error (parser, "collapsed loops not perfectly nested");
9363 location_t here = c_parser_peek_token (parser)->location;
9364 stmt = c_begin_compound_stmt (true);
9366 c_parser_compound_statement_nostart (parser);
9367 body = c_end_compound_stmt (here, stmt, true);
9374 /* Only bother calling c_finish_omp_for if we haven't already generated
9375 an error from the initialization parsing. */
9378 stmt = c_finish_omp_for (loc, declv, initv, condv, incrv, body, NULL);
9381 if (par_clauses != NULL)
9384 for (c = par_clauses; *c ; )
9385 if (OMP_CLAUSE_CODE (*c) != OMP_CLAUSE_FIRSTPRIVATE
9386 && OMP_CLAUSE_CODE (*c) != OMP_CLAUSE_LASTPRIVATE)
9387 c = &OMP_CLAUSE_CHAIN (*c);
9390 for (i = 0; i < collapse; i++)
9391 if (TREE_VEC_ELT (declv, i) == OMP_CLAUSE_DECL (*c))
9394 c = &OMP_CLAUSE_CHAIN (*c);
9395 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE)
9398 "iteration variable %qD should not be firstprivate",
9399 OMP_CLAUSE_DECL (*c));
9400 *c = OMP_CLAUSE_CHAIN (*c);
9404 /* Copy lastprivate (decl) clause to OMP_FOR_CLAUSES,
9405 change it to shared (decl) in
9406 OMP_PARALLEL_CLAUSES. */
9407 tree l = build_omp_clause (OMP_CLAUSE_LOCATION (*c),
9408 OMP_CLAUSE_LASTPRIVATE);
9409 OMP_CLAUSE_DECL (l) = OMP_CLAUSE_DECL (*c);
9410 OMP_CLAUSE_CHAIN (l) = clauses;
9412 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
9416 OMP_FOR_CLAUSES (stmt) = clauses;
9421 while (!VEC_empty (tree, for_block))
9423 /* FIXME diagnostics: LOC below should be the actual location of
9424 this particular for block. We need to build a list of
9425 locations to go along with FOR_BLOCK. */
9426 stmt = c_end_compound_stmt (loc, VEC_pop (tree, for_block), true);
9429 release_tree_vector (for_block);
9434 #pragma omp for for-clause[optseq] new-line
9437 LOC is the location of the #pragma token.
9440 #define OMP_FOR_CLAUSE_MASK \
9441 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
9442 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
9443 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
9444 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
9445 | (1u << PRAGMA_OMP_CLAUSE_ORDERED) \
9446 | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE) \
9447 | (1u << PRAGMA_OMP_CLAUSE_COLLAPSE) \
9448 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
9451 c_parser_omp_for (location_t loc, c_parser *parser)
9453 tree block, clauses, ret;
9455 clauses = c_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
9458 block = c_begin_compound_stmt (true);
9459 ret = c_parser_omp_for_loop (loc, parser, clauses, NULL);
9460 block = c_end_compound_stmt (loc, block, true);
9467 # pragma omp master new-line
9470 LOC is the location of the #pragma token.
9474 c_parser_omp_master (location_t loc, c_parser *parser)
9476 c_parser_skip_to_pragma_eol (parser);
9477 return c_finish_omp_master (loc, c_parser_omp_structured_block (parser));
9481 # pragma omp ordered new-line
9484 LOC is the location of the #pragma itself.
9488 c_parser_omp_ordered (location_t loc, c_parser *parser)
9490 c_parser_skip_to_pragma_eol (parser);
9491 return c_finish_omp_ordered (loc, c_parser_omp_structured_block (parser));
9497 { section-sequence }
9500 section-directive[opt] structured-block
9501 section-sequence section-directive structured-block
9503 SECTIONS_LOC is the location of the #pragma omp sections. */
9506 c_parser_omp_sections_scope (location_t sections_loc, c_parser *parser)
9509 bool error_suppress = false;
9512 loc = c_parser_peek_token (parser)->location;
9513 if (!c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
9515 /* Avoid skipping until the end of the block. */
9516 parser->error = false;
9520 stmt = push_stmt_list ();
9522 if (c_parser_peek_token (parser)->pragma_kind != PRAGMA_OMP_SECTION)
9524 substmt = push_stmt_list ();
9528 c_parser_statement (parser);
9530 if (c_parser_peek_token (parser)->pragma_kind == PRAGMA_OMP_SECTION)
9532 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
9534 if (c_parser_next_token_is (parser, CPP_EOF))
9538 substmt = pop_stmt_list (substmt);
9539 substmt = build1 (OMP_SECTION, void_type_node, substmt);
9540 SET_EXPR_LOCATION (substmt, loc);
9546 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
9548 if (c_parser_next_token_is (parser, CPP_EOF))
9551 loc = c_parser_peek_token (parser)->location;
9552 if (c_parser_peek_token (parser)->pragma_kind == PRAGMA_OMP_SECTION)
9554 c_parser_consume_pragma (parser);
9555 c_parser_skip_to_pragma_eol (parser);
9556 error_suppress = false;
9558 else if (!error_suppress)
9560 error_at (loc, "expected %<#pragma omp section%> or %<}%>");
9561 error_suppress = true;
9564 substmt = c_parser_omp_structured_block (parser);
9565 substmt = build1 (OMP_SECTION, void_type_node, substmt);
9566 SET_EXPR_LOCATION (substmt, loc);
9569 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE,
9570 "expected %<#pragma omp section%> or %<}%>");
9572 substmt = pop_stmt_list (stmt);
9574 stmt = make_node (OMP_SECTIONS);
9575 SET_EXPR_LOCATION (stmt, sections_loc);
9576 TREE_TYPE (stmt) = void_type_node;
9577 OMP_SECTIONS_BODY (stmt) = substmt;
9579 return add_stmt (stmt);
9583 # pragma omp sections sections-clause[optseq] newline
9586 LOC is the location of the #pragma token.
9589 #define OMP_SECTIONS_CLAUSE_MASK \
9590 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
9591 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
9592 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
9593 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
9594 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
9597 c_parser_omp_sections (location_t loc, c_parser *parser)
9599 tree block, clauses, ret;
9601 clauses = c_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
9602 "#pragma omp sections");
9604 block = c_begin_compound_stmt (true);
9605 ret = c_parser_omp_sections_scope (loc, parser);
9607 OMP_SECTIONS_CLAUSES (ret) = clauses;
9608 block = c_end_compound_stmt (loc, block, true);
9615 # pragma parallel parallel-clause new-line
9616 # pragma parallel for parallel-for-clause new-line
9617 # pragma parallel sections parallel-sections-clause new-line
9619 LOC is the location of the #pragma token.
9622 #define OMP_PARALLEL_CLAUSE_MASK \
9623 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
9624 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
9625 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
9626 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
9627 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
9628 | (1u << PRAGMA_OMP_CLAUSE_COPYIN) \
9629 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
9630 | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
9633 c_parser_omp_parallel (location_t loc, c_parser *parser)
9635 enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
9636 const char *p_name = "#pragma omp parallel";
9637 tree stmt, clauses, par_clause, ws_clause, block;
9638 unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
9640 if (c_parser_next_token_is_keyword (parser, RID_FOR))
9642 c_parser_consume_token (parser);
9643 p_kind = PRAGMA_OMP_PARALLEL_FOR;
9644 p_name = "#pragma omp parallel for";
9645 mask |= OMP_FOR_CLAUSE_MASK;
9646 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
9648 else if (c_parser_next_token_is (parser, CPP_NAME))
9650 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
9651 if (strcmp (p, "sections") == 0)
9653 c_parser_consume_token (parser);
9654 p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
9655 p_name = "#pragma omp parallel sections";
9656 mask |= OMP_SECTIONS_CLAUSE_MASK;
9657 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
9661 clauses = c_parser_omp_all_clauses (parser, mask, p_name);
9665 case PRAGMA_OMP_PARALLEL:
9666 block = c_begin_omp_parallel ();
9667 c_parser_statement (parser);
9668 stmt = c_finish_omp_parallel (loc, clauses, block);
9671 case PRAGMA_OMP_PARALLEL_FOR:
9672 block = c_begin_omp_parallel ();
9673 c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
9674 c_parser_omp_for_loop (loc, parser, ws_clause, &par_clause);
9675 stmt = c_finish_omp_parallel (loc, par_clause, block);
9676 OMP_PARALLEL_COMBINED (stmt) = 1;
9679 case PRAGMA_OMP_PARALLEL_SECTIONS:
9680 block = c_begin_omp_parallel ();
9681 c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
9682 stmt = c_parser_omp_sections_scope (loc, parser);
9684 OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
9685 stmt = c_finish_omp_parallel (loc, par_clause, block);
9686 OMP_PARALLEL_COMBINED (stmt) = 1;
9697 # pragma omp single single-clause[optseq] new-line
9700 LOC is the location of the #pragma.
9703 #define OMP_SINGLE_CLAUSE_MASK \
9704 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
9705 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
9706 | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
9707 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
9710 c_parser_omp_single (location_t loc, c_parser *parser)
9712 tree stmt = make_node (OMP_SINGLE);
9713 SET_EXPR_LOCATION (stmt, loc);
9714 TREE_TYPE (stmt) = void_type_node;
9716 OMP_SINGLE_CLAUSES (stmt)
9717 = c_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
9718 "#pragma omp single");
9719 OMP_SINGLE_BODY (stmt) = c_parser_omp_structured_block (parser);
9721 return add_stmt (stmt);
9725 # pragma omp task task-clause[optseq] new-line
9727 LOC is the location of the #pragma.
9730 #define OMP_TASK_CLAUSE_MASK \
9731 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
9732 | (1u << PRAGMA_OMP_CLAUSE_UNTIED) \
9733 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
9734 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
9735 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
9736 | (1u << PRAGMA_OMP_CLAUSE_SHARED))
9739 c_parser_omp_task (location_t loc, c_parser *parser)
9741 tree clauses, block;
9743 clauses = c_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
9744 "#pragma omp task");
9746 block = c_begin_omp_task ();
9747 c_parser_statement (parser);
9748 return c_finish_omp_task (loc, clauses, block);
9752 # pragma omp taskwait new-line
9756 c_parser_omp_taskwait (c_parser *parser)
9758 location_t loc = c_parser_peek_token (parser)->location;
9759 c_parser_consume_pragma (parser);
9760 c_parser_skip_to_pragma_eol (parser);
9762 c_finish_omp_taskwait (loc);
9765 /* Main entry point to parsing most OpenMP pragmas. */
9768 c_parser_omp_construct (c_parser *parser)
9770 enum pragma_kind p_kind;
9774 loc = c_parser_peek_token (parser)->location;
9775 p_kind = c_parser_peek_token (parser)->pragma_kind;
9776 c_parser_consume_pragma (parser);
9780 case PRAGMA_OMP_ATOMIC:
9781 c_parser_omp_atomic (loc, parser);
9783 case PRAGMA_OMP_CRITICAL:
9784 stmt = c_parser_omp_critical (loc, parser);
9786 case PRAGMA_OMP_FOR:
9787 stmt = c_parser_omp_for (loc, parser);
9789 case PRAGMA_OMP_MASTER:
9790 stmt = c_parser_omp_master (loc, parser);
9792 case PRAGMA_OMP_ORDERED:
9793 stmt = c_parser_omp_ordered (loc, parser);
9795 case PRAGMA_OMP_PARALLEL:
9796 stmt = c_parser_omp_parallel (loc, parser);
9798 case PRAGMA_OMP_SECTIONS:
9799 stmt = c_parser_omp_sections (loc, parser);
9801 case PRAGMA_OMP_SINGLE:
9802 stmt = c_parser_omp_single (loc, parser);
9804 case PRAGMA_OMP_TASK:
9805 stmt = c_parser_omp_task (loc, parser);
9812 gcc_assert (EXPR_LOCATION (stmt) != UNKNOWN_LOCATION);
9817 # pragma omp threadprivate (variable-list) */
9820 c_parser_omp_threadprivate (c_parser *parser)
9825 c_parser_consume_pragma (parser);
9826 loc = c_parser_peek_token (parser)->location;
9827 vars = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_ERROR, NULL);
9829 /* Mark every variable in VARS to be assigned thread local storage. */
9830 for (t = vars; t; t = TREE_CHAIN (t))
9832 tree v = TREE_PURPOSE (t);
9834 /* FIXME diagnostics: Ideally we should keep individual
9835 locations for all the variables in the var list to make the
9836 following errors more precise. Perhaps
9837 c_parser_omp_var_list_parens() should construct a list of
9838 locations to go along with the var list. */
9840 /* If V had already been marked threadprivate, it doesn't matter
9841 whether it had been used prior to this point. */
9842 if (TREE_CODE (v) != VAR_DECL)
9843 error_at (loc, "%qD is not a variable", v);
9844 else if (TREE_USED (v) && !C_DECL_THREADPRIVATE_P (v))
9845 error_at (loc, "%qE declared %<threadprivate%> after first use", v);
9846 else if (! TREE_STATIC (v) && ! DECL_EXTERNAL (v))
9847 error_at (loc, "automatic variable %qE cannot be %<threadprivate%>", v);
9848 else if (TREE_TYPE (v) == error_mark_node)
9850 else if (! COMPLETE_TYPE_P (TREE_TYPE (v)))
9851 error_at (loc, "%<threadprivate%> %qE has incomplete type", v);
9854 if (! DECL_THREAD_LOCAL_P (v))
9856 DECL_TLS_MODEL (v) = decl_default_tls_model (v);
9857 /* If rtl has been already set for this var, call
9858 make_decl_rtl once again, so that encode_section_info
9859 has a chance to look at the new decl flags. */
9860 if (DECL_RTL_SET_P (v))
9863 C_DECL_THREADPRIVATE_P (v) = 1;
9867 c_parser_skip_to_pragma_eol (parser);
9871 /* Parse a single source file. */
9876 /* Use local storage to begin. If the first token is a pragma, parse it.
9877 If it is #pragma GCC pch_preprocess, then this will load a PCH file
9878 which will cause garbage collection. */
9881 memset (&tparser, 0, sizeof tparser);
9882 the_parser = &tparser;
9884 if (c_parser_peek_token (&tparser)->pragma_kind == PRAGMA_GCC_PCH_PREPROCESS)
9885 c_parser_pragma_pch_preprocess (&tparser);
9887 the_parser = ggc_alloc_c_parser ();
9888 *the_parser = tparser;
9890 /* Initialize EH, if we've been told to do so. */
9891 if (flag_exceptions)
9892 using_eh_for_cleanups ();
9894 c_parser_translation_unit (the_parser);
9898 #include "gt-c-parser.h"