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 Free Software Foundation, Inc.
5 Parser actions based on the old Bison parser; structure somewhat
6 influenced by and fragments based on the C++ parser.
8 This file is part of GCC.
10 GCC is free software; you can redistribute it and/or modify it under
11 the terms of the GNU General Public License as published by the Free
12 Software Foundation; either version 3, or (at your option) any later
15 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
16 WARRANTY; without even the implied warranty of MERCHANTABILITY or
17 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
20 You should have received a copy of the GNU General Public License
21 along with GCC; see the file COPYING3. If not see
22 <http://www.gnu.org/licenses/>. */
26 Make sure all relevant comments, and all relevant code from all
27 actions, brought over from old parser. Verify exact correspondence
30 Add testcases covering every input symbol in every state in old and
33 Include full syntax for GNU C, including erroneous cases accepted
34 with error messages, in syntax productions in comments.
36 Make more diagnostics in the front end generally take an explicit
37 location rather than implicitly using input_location. */
41 #include "coretypes.h"
45 #include "langhooks.h"
61 /* The reserved keyword table. */
65 ENUM_BITFIELD(rid) rid : 16;
66 unsigned int disable : 16;
69 /* Disable mask. Keywords are disabled if (reswords[i].disable &
71 #define D_C89 0x01 /* not in C89 */
72 #define D_EXT 0x02 /* GCC extension */
73 #define D_EXT89 0x04 /* GCC extension incorporated in C99 */
74 #define D_OBJC 0x08 /* Objective C only */
76 static const struct resword reswords[] =
78 { "_Bool", RID_BOOL, 0 },
79 { "_Complex", RID_COMPLEX, 0 },
80 { "_Decimal32", RID_DFLOAT32, D_EXT },
81 { "_Decimal64", RID_DFLOAT64, D_EXT },
82 { "_Decimal128", RID_DFLOAT128, D_EXT },
83 { "_Fract", RID_FRACT, D_EXT },
84 { "_Accum", RID_ACCUM, D_EXT },
85 { "_Sat", RID_SAT, D_EXT },
86 { "__FUNCTION__", RID_FUNCTION_NAME, 0 },
87 { "__PRETTY_FUNCTION__", RID_PRETTY_FUNCTION_NAME, 0 },
88 { "__alignof", RID_ALIGNOF, 0 },
89 { "__alignof__", RID_ALIGNOF, 0 },
90 { "__asm", RID_ASM, 0 },
91 { "__asm__", RID_ASM, 0 },
92 { "__attribute", RID_ATTRIBUTE, 0 },
93 { "__attribute__", RID_ATTRIBUTE, 0 },
94 { "__builtin_choose_expr", RID_CHOOSE_EXPR, 0 },
95 { "__builtin_offsetof", RID_OFFSETOF, 0 },
96 { "__builtin_types_compatible_p", RID_TYPES_COMPATIBLE_P, 0 },
97 { "__builtin_va_arg", RID_VA_ARG, 0 },
98 { "__complex", RID_COMPLEX, 0 },
99 { "__complex__", RID_COMPLEX, 0 },
100 { "__const", RID_CONST, 0 },
101 { "__const__", RID_CONST, 0 },
102 { "__extension__", RID_EXTENSION, 0 },
103 { "__func__", RID_C99_FUNCTION_NAME, 0 },
104 { "__imag", RID_IMAGPART, 0 },
105 { "__imag__", RID_IMAGPART, 0 },
106 { "__inline", RID_INLINE, 0 },
107 { "__inline__", RID_INLINE, 0 },
108 { "__label__", RID_LABEL, 0 },
109 { "__real", RID_REALPART, 0 },
110 { "__real__", RID_REALPART, 0 },
111 { "__restrict", RID_RESTRICT, 0 },
112 { "__restrict__", RID_RESTRICT, 0 },
113 { "__signed", RID_SIGNED, 0 },
114 { "__signed__", RID_SIGNED, 0 },
115 { "__thread", RID_THREAD, 0 },
116 { "__typeof", RID_TYPEOF, 0 },
117 { "__typeof__", RID_TYPEOF, 0 },
118 { "__volatile", RID_VOLATILE, 0 },
119 { "__volatile__", RID_VOLATILE, 0 },
120 { "asm", RID_ASM, D_EXT },
121 { "auto", RID_AUTO, 0 },
122 { "break", RID_BREAK, 0 },
123 { "case", RID_CASE, 0 },
124 { "char", RID_CHAR, 0 },
125 { "const", RID_CONST, 0 },
126 { "continue", RID_CONTINUE, 0 },
127 { "default", RID_DEFAULT, 0 },
129 { "double", RID_DOUBLE, 0 },
130 { "else", RID_ELSE, 0 },
131 { "enum", RID_ENUM, 0 },
132 { "extern", RID_EXTERN, 0 },
133 { "float", RID_FLOAT, 0 },
134 { "for", RID_FOR, 0 },
135 { "goto", RID_GOTO, 0 },
137 { "inline", RID_INLINE, D_EXT89 },
138 { "int", RID_INT, 0 },
139 { "long", RID_LONG, 0 },
140 { "register", RID_REGISTER, 0 },
141 { "restrict", RID_RESTRICT, D_C89 },
142 { "return", RID_RETURN, 0 },
143 { "short", RID_SHORT, 0 },
144 { "signed", RID_SIGNED, 0 },
145 { "sizeof", RID_SIZEOF, 0 },
146 { "static", RID_STATIC, 0 },
147 { "struct", RID_STRUCT, 0 },
148 { "switch", RID_SWITCH, 0 },
149 { "typedef", RID_TYPEDEF, 0 },
150 { "typeof", RID_TYPEOF, D_EXT },
151 { "union", RID_UNION, 0 },
152 { "unsigned", RID_UNSIGNED, 0 },
153 { "void", RID_VOID, 0 },
154 { "volatile", RID_VOLATILE, 0 },
155 { "while", RID_WHILE, 0 },
156 /* These Objective-C keywords are recognized only immediately after
158 { "class", RID_AT_CLASS, D_OBJC },
159 { "compatibility_alias", RID_AT_ALIAS, D_OBJC },
160 { "defs", RID_AT_DEFS, D_OBJC },
161 { "encode", RID_AT_ENCODE, D_OBJC },
162 { "end", RID_AT_END, D_OBJC },
163 { "implementation", RID_AT_IMPLEMENTATION, D_OBJC },
164 { "interface", RID_AT_INTERFACE, D_OBJC },
165 { "private", RID_AT_PRIVATE, D_OBJC },
166 { "protected", RID_AT_PROTECTED, D_OBJC },
167 { "protocol", RID_AT_PROTOCOL, D_OBJC },
168 { "public", RID_AT_PUBLIC, D_OBJC },
169 { "selector", RID_AT_SELECTOR, D_OBJC },
170 { "throw", RID_AT_THROW, D_OBJC },
171 { "try", RID_AT_TRY, D_OBJC },
172 { "catch", RID_AT_CATCH, D_OBJC },
173 { "finally", RID_AT_FINALLY, D_OBJC },
174 { "synchronized", RID_AT_SYNCHRONIZED, D_OBJC },
175 /* These are recognized only in protocol-qualifier context
177 { "bycopy", RID_BYCOPY, D_OBJC },
178 { "byref", RID_BYREF, D_OBJC },
179 { "in", RID_IN, D_OBJC },
180 { "inout", RID_INOUT, D_OBJC },
181 { "oneway", RID_ONEWAY, D_OBJC },
182 { "out", RID_OUT, D_OBJC },
184 #define N_reswords (sizeof reswords / sizeof (struct resword))
186 /* Initialization routine for this file. */
191 /* The only initialization required is of the reserved word
195 int mask = (flag_isoc99 ? 0 : D_C89)
196 | (flag_no_asm ? (flag_isoc99 ? D_EXT : D_EXT|D_EXT89) : 0);
198 if (!c_dialect_objc ())
201 ridpointers = GGC_CNEWVEC (tree, (int) RID_MAX);
202 for (i = 0; i < N_reswords; i++)
204 /* If a keyword is disabled, do not enter it into the table
205 and so create a canonical spelling that isn't a keyword. */
206 if (reswords[i].disable & mask)
209 id = get_identifier (reswords[i].word);
210 C_RID_CODE (id) = reswords[i].rid;
211 C_IS_RESERVED_WORD (id) = 1;
212 ridpointers [(int) reswords[i].rid] = id;
216 /* The C lexer intermediates between the lexer in cpplib and c-lex.c
217 and the C parser. Unlike the C++ lexer, the parser structure
218 stores the lexer information instead of using a separate structure.
219 Identifiers are separated into ordinary identifiers, type names,
220 keywords and some other Objective-C types of identifiers, and some
221 look-ahead is maintained.
223 ??? It might be a good idea to lex the whole file up front (as for
224 C++). It would then be possible to share more of the C and C++
225 lexer code, if desired. */
227 /* The following local token type is used. */
230 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
232 /* More information about the type of a CPP_NAME token. */
233 typedef enum c_id_kind {
234 /* An ordinary identifier. */
236 /* An identifier declared as a typedef name. */
238 /* An identifier declared as an Objective-C class name. */
240 /* Not an identifier. */
244 /* A single C token after string literal concatenation and conversion
245 of preprocessing tokens to tokens. */
246 typedef struct c_token GTY (())
248 /* The kind of token. */
249 ENUM_BITFIELD (cpp_ttype) type : 8;
250 /* If this token is a CPP_NAME, this value indicates whether also
251 declared as some kind of type. Otherwise, it is C_ID_NONE. */
252 ENUM_BITFIELD (c_id_kind) id_kind : 8;
253 /* If this token is a keyword, this value indicates which keyword.
254 Otherwise, this value is RID_MAX. */
255 ENUM_BITFIELD (rid) keyword : 8;
256 /* If this token is a CPP_PRAGMA, this indicates the pragma that
257 was seen. Otherwise it is PRAGMA_NONE. */
258 ENUM_BITFIELD (pragma_kind) pragma_kind : 7;
259 /* True if this token is from a system header. */
260 BOOL_BITFIELD in_system_header : 1;
261 /* The value associated with this token, if any. */
263 /* The location at which this token was found. */
267 /* A parser structure recording information about the state and
268 context of parsing. Includes lexer information with up to two
269 tokens of look-ahead; more are not needed for C. */
270 typedef struct c_parser GTY(())
272 /* The look-ahead tokens. */
274 /* How many look-ahead tokens are available (0, 1 or 2). */
276 /* True if a syntax error is being recovered from; false otherwise.
277 c_parser_error sets this flag. It should clear this flag when
278 enough tokens have been consumed to recover from the error. */
279 BOOL_BITFIELD error : 1;
280 /* True if we're processing a pragma, and shouldn't automatically
281 consume CPP_PRAGMA_EOL. */
282 BOOL_BITFIELD in_pragma : 1;
283 /* True if we want to lex an untranslated string. */
284 BOOL_BITFIELD lex_untranslated_string : 1;
285 /* Objective-C specific parser/lexer information. */
286 BOOL_BITFIELD objc_pq_context : 1;
287 /* The following flag is needed to contextualize Objective-C lexical
288 analysis. In some cases (e.g., 'int NSObject;'), it is
289 undesirable to bind an identifier to an Objective-C class, even
290 if a class with that name exists. */
291 BOOL_BITFIELD objc_need_raw_identifier : 1;
295 /* The actual parser and external interface. ??? Does this need to be
296 garbage-collected? */
298 static GTY (()) c_parser *the_parser;
301 /* Read in and lex a single token, storing it in *TOKEN. */
304 c_lex_one_token (c_parser *parser, c_token *token)
306 timevar_push (TV_LEX);
308 token->type = c_lex_with_flags (&token->value, &token->location, NULL,
309 (parser->lex_untranslated_string
310 ? C_LEX_STRING_NO_TRANSLATE : 0));
311 token->id_kind = C_ID_NONE;
312 token->keyword = RID_MAX;
313 token->pragma_kind = PRAGMA_NONE;
314 token->in_system_header = in_system_header;
322 bool objc_force_identifier = parser->objc_need_raw_identifier;
323 if (c_dialect_objc ())
324 parser->objc_need_raw_identifier = false;
326 if (C_IS_RESERVED_WORD (token->value))
328 enum rid rid_code = C_RID_CODE (token->value);
330 if (c_dialect_objc ())
332 if (!OBJC_IS_AT_KEYWORD (rid_code)
333 && (!OBJC_IS_PQ_KEYWORD (rid_code)
334 || parser->objc_pq_context))
336 /* Return the canonical spelling for this keyword. */
337 token->value = ridpointers[(int) rid_code];
338 token->type = CPP_KEYWORD;
339 token->keyword = rid_code;
345 /* Return the canonical spelling for this keyword. */
346 token->value = ridpointers[(int) rid_code];
347 token->type = CPP_KEYWORD;
348 token->keyword = rid_code;
353 decl = lookup_name (token->value);
356 if (TREE_CODE (decl) == TYPE_DECL)
358 token->id_kind = C_ID_TYPENAME;
362 else if (c_dialect_objc ())
364 tree objc_interface_decl = objc_is_class_name (token->value);
365 /* Objective-C class names are in the same namespace as
366 variables and typedefs, and hence are shadowed by local
368 if (objc_interface_decl
369 && (global_bindings_p ()
370 || (!objc_force_identifier && !decl)))
372 token->value = objc_interface_decl;
373 token->id_kind = C_ID_CLASSNAME;
377 token->id_kind = C_ID_ID;
381 /* This only happens in Objective-C; it must be a keyword. */
382 token->type = CPP_KEYWORD;
383 token->keyword = C_RID_CODE (token->value);
387 case CPP_CLOSE_PAREN:
389 /* These tokens may affect the interpretation of any identifiers
390 following, if doing Objective-C. */
391 if (c_dialect_objc ())
392 parser->objc_need_raw_identifier = false;
395 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
396 token->pragma_kind = TREE_INT_CST_LOW (token->value);
402 timevar_pop (TV_LEX);
405 /* Return a pointer to the next token from PARSER, reading it in if
408 static inline c_token *
409 c_parser_peek_token (c_parser *parser)
411 if (parser->tokens_avail == 0)
413 c_lex_one_token (parser, &parser->tokens[0]);
414 parser->tokens_avail = 1;
416 return &parser->tokens[0];
419 /* Return true if the next token from PARSER has the indicated
423 c_parser_next_token_is (c_parser *parser, enum cpp_ttype type)
425 return c_parser_peek_token (parser)->type == type;
428 /* Return true if the next token from PARSER does not have the
432 c_parser_next_token_is_not (c_parser *parser, enum cpp_ttype type)
434 return !c_parser_next_token_is (parser, type);
437 /* Return true if the next token from PARSER is the indicated
441 c_parser_next_token_is_keyword (c_parser *parser, enum rid keyword)
445 /* Peek at the next token. */
446 token = c_parser_peek_token (parser);
447 /* Check to see if it is the indicated keyword. */
448 return token->keyword == keyword;
451 /* Return true if TOKEN can start a type name,
454 c_token_starts_typename (c_token *token)
459 switch (token->id_kind)
466 gcc_assert (c_dialect_objc ());
472 switch (token->keyword)
504 if (c_dialect_objc ())
512 /* Return true if the next token from PARSER can start a type name,
515 c_parser_next_token_starts_typename (c_parser *parser)
517 c_token *token = c_parser_peek_token (parser);
518 return c_token_starts_typename (token);
521 /* Return true if TOKEN can start declaration specifiers, false
524 c_token_starts_declspecs (c_token *token)
529 switch (token->id_kind)
536 gcc_assert (c_dialect_objc ());
542 switch (token->keyword)
581 if (c_dialect_objc ())
589 /* Return true if the next token from PARSER can start declaration
590 specifiers, false otherwise. */
592 c_parser_next_token_starts_declspecs (c_parser *parser)
594 c_token *token = c_parser_peek_token (parser);
595 return c_token_starts_declspecs (token);
598 /* Return a pointer to the next-but-one token from PARSER, reading it
599 in if necessary. The next token is already read in. */
602 c_parser_peek_2nd_token (c_parser *parser)
604 if (parser->tokens_avail >= 2)
605 return &parser->tokens[1];
606 gcc_assert (parser->tokens_avail == 1);
607 gcc_assert (parser->tokens[0].type != CPP_EOF);
608 gcc_assert (parser->tokens[0].type != CPP_PRAGMA_EOL);
609 c_lex_one_token (parser, &parser->tokens[1]);
610 parser->tokens_avail = 2;
611 return &parser->tokens[1];
614 /* Consume the next token from PARSER. */
617 c_parser_consume_token (c_parser *parser)
619 gcc_assert (parser->tokens_avail >= 1);
620 gcc_assert (parser->tokens[0].type != CPP_EOF);
621 gcc_assert (!parser->in_pragma || parser->tokens[0].type != CPP_PRAGMA_EOL);
622 gcc_assert (parser->error || parser->tokens[0].type != CPP_PRAGMA);
623 if (parser->tokens_avail == 2)
624 parser->tokens[0] = parser->tokens[1];
625 parser->tokens_avail--;
628 /* Expect the current token to be a #pragma. Consume it and remember
629 that we've begun parsing a pragma. */
632 c_parser_consume_pragma (c_parser *parser)
634 gcc_assert (!parser->in_pragma);
635 gcc_assert (parser->tokens_avail >= 1);
636 gcc_assert (parser->tokens[0].type == CPP_PRAGMA);
637 if (parser->tokens_avail == 2)
638 parser->tokens[0] = parser->tokens[1];
639 parser->tokens_avail--;
640 parser->in_pragma = true;
643 /* Update the globals input_location and in_system_header from
646 c_parser_set_source_position_from_token (c_token *token)
648 if (token->type != CPP_EOF)
650 input_location = token->location;
651 in_system_header = token->in_system_header;
655 /* Issue a diagnostic of the form
656 FILE:LINE: MESSAGE before TOKEN
657 where TOKEN is the next token in the input stream of PARSER.
658 MESSAGE (specified by the caller) is usually of the form "expected
661 Do not issue a diagnostic if still recovering from an error.
663 ??? This is taken from the C++ parser, but building up messages in
664 this way is not i18n-friendly and some other approach should be
668 c_parser_error (c_parser *parser, const char *gmsgid)
670 c_token *token = c_parser_peek_token (parser);
673 parser->error = true;
676 /* This diagnostic makes more sense if it is tagged to the line of
677 the token we just peeked at. */
678 c_parser_set_source_position_from_token (token);
679 c_parse_error (gmsgid,
680 /* Because c_parse_error does not understand
681 CPP_KEYWORD, keywords are treated like
683 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
687 /* If the next token is of the indicated TYPE, consume it. Otherwise,
688 issue the error MSGID. If MSGID is NULL then a message has already
689 been produced and no message will be produced this time. Returns
690 true if found, false otherwise. */
693 c_parser_require (c_parser *parser,
697 if (c_parser_next_token_is (parser, type))
699 c_parser_consume_token (parser);
704 c_parser_error (parser, msgid);
709 /* If the next token is the indicated keyword, consume it. Otherwise,
710 issue the error MSGID. Returns true if found, false otherwise. */
713 c_parser_require_keyword (c_parser *parser,
717 if (c_parser_next_token_is_keyword (parser, keyword))
719 c_parser_consume_token (parser);
724 c_parser_error (parser, msgid);
729 /* Like c_parser_require, except that tokens will be skipped until the
730 desired token is found. An error message is still produced if the
731 next token is not as expected. If MSGID is NULL then a message has
732 already been produced and no message will be produced this
736 c_parser_skip_until_found (c_parser *parser,
740 unsigned nesting_depth = 0;
742 if (c_parser_require (parser, type, msgid))
745 /* Skip tokens until the desired token is found. */
748 /* Peek at the next token. */
749 c_token *token = c_parser_peek_token (parser);
750 /* If we've reached the token we want, consume it and stop. */
751 if (token->type == type && !nesting_depth)
753 c_parser_consume_token (parser);
757 /* If we've run out of tokens, stop. */
758 if (token->type == CPP_EOF)
760 if (token->type == CPP_PRAGMA_EOL && parser->in_pragma)
762 if (token->type == CPP_OPEN_BRACE
763 || token->type == CPP_OPEN_PAREN
764 || token->type == CPP_OPEN_SQUARE)
766 else if (token->type == CPP_CLOSE_BRACE
767 || token->type == CPP_CLOSE_PAREN
768 || token->type == CPP_CLOSE_SQUARE)
770 if (nesting_depth-- == 0)
773 /* Consume this token. */
774 c_parser_consume_token (parser);
776 parser->error = false;
779 /* Skip tokens until the end of a parameter is found, but do not
780 consume the comma, semicolon or closing delimiter. */
783 c_parser_skip_to_end_of_parameter (c_parser *parser)
785 unsigned nesting_depth = 0;
789 c_token *token = c_parser_peek_token (parser);
790 if ((token->type == CPP_COMMA || token->type == CPP_SEMICOLON)
793 /* If we've run out of tokens, stop. */
794 if (token->type == CPP_EOF)
796 if (token->type == CPP_PRAGMA_EOL && parser->in_pragma)
798 if (token->type == CPP_OPEN_BRACE
799 || token->type == CPP_OPEN_PAREN
800 || token->type == CPP_OPEN_SQUARE)
802 else if (token->type == CPP_CLOSE_BRACE
803 || token->type == CPP_CLOSE_PAREN
804 || token->type == CPP_CLOSE_SQUARE)
806 if (nesting_depth-- == 0)
809 /* Consume this token. */
810 c_parser_consume_token (parser);
812 parser->error = false;
815 /* Expect to be at the end of the pragma directive and consume an
816 end of line marker. */
819 c_parser_skip_to_pragma_eol (c_parser *parser)
821 gcc_assert (parser->in_pragma);
822 parser->in_pragma = false;
824 if (!c_parser_require (parser, CPP_PRAGMA_EOL, "expected end of line"))
827 c_token *token = c_parser_peek_token (parser);
828 if (token->type == CPP_EOF)
830 if (token->type == CPP_PRAGMA_EOL)
832 c_parser_consume_token (parser);
835 c_parser_consume_token (parser);
838 parser->error = false;
841 /* Skip tokens until we have consumed an entire block, or until we
842 have consumed a non-nested ';'. */
845 c_parser_skip_to_end_of_block_or_statement (c_parser *parser)
847 unsigned nesting_depth = 0;
848 bool save_error = parser->error;
854 /* Peek at the next token. */
855 token = c_parser_peek_token (parser);
863 if (parser->in_pragma)
868 /* If the next token is a ';', we have reached the
869 end of the statement. */
872 /* Consume the ';'. */
873 c_parser_consume_token (parser);
878 case CPP_CLOSE_BRACE:
879 /* If the next token is a non-nested '}', then we have
880 reached the end of the current block. */
881 if (nesting_depth == 0 || --nesting_depth == 0)
883 c_parser_consume_token (parser);
889 /* If it the next token is a '{', then we are entering a new
890 block. Consume the entire block. */
895 /* If we see a pragma, consume the whole thing at once. We
896 have some safeguards against consuming pragmas willy-nilly.
897 Normally, we'd expect to be here with parser->error set,
898 which disables these safeguards. But it's possible to get
899 here for secondary error recovery, after parser->error has
901 c_parser_consume_pragma (parser);
902 c_parser_skip_to_pragma_eol (parser);
903 parser->error = save_error;
910 c_parser_consume_token (parser);
914 parser->error = false;
917 /* Save the warning flags which are controlled by __extension__. */
920 disable_extension_diagnostics (void)
923 | (warn_pointer_arith << 1)
924 | (warn_traditional << 2)
927 warn_pointer_arith = 0;
928 warn_traditional = 0;
933 /* Restore the warning flags which are controlled by __extension__.
934 FLAGS is the return value from disable_extension_diagnostics. */
937 restore_extension_diagnostics (int flags)
939 pedantic = flags & 1;
940 warn_pointer_arith = (flags >> 1) & 1;
941 warn_traditional = (flags >> 2) & 1;
942 flag_iso = (flags >> 3) & 1;
945 /* Possibly kinds of declarator to parse. */
946 typedef enum c_dtr_syn {
947 /* A normal declarator with an identifier. */
949 /* An abstract declarator (maybe empty). */
951 /* A parameter declarator: may be either, but after a type name does
952 not redeclare a typedef name as an identifier if it can
953 alternatively be interpreted as a typedef name; see DR#009,
954 applied in C90 TC1, omitted from C99 and reapplied in C99 TC2
955 following DR#249. For example, given a typedef T, "int T" and
956 "int *T" are valid parameter declarations redeclaring T, while
957 "int (T)" and "int * (T)" and "int (T[])" and "int (T (int))" are
958 abstract declarators rather than involving redundant parentheses;
959 the same applies with attributes inside the parentheses before
964 static void c_parser_external_declaration (c_parser *);
965 static void c_parser_asm_definition (c_parser *);
966 static void c_parser_declaration_or_fndef (c_parser *, bool, bool, bool, bool);
967 static void c_parser_declspecs (c_parser *, struct c_declspecs *, bool, bool,
969 static struct c_typespec c_parser_enum_specifier (c_parser *);
970 static struct c_typespec c_parser_struct_or_union_specifier (c_parser *);
971 static tree c_parser_struct_declaration (c_parser *);
972 static struct c_typespec c_parser_typeof_specifier (c_parser *);
973 static struct c_declarator *c_parser_declarator (c_parser *, bool, c_dtr_syn,
975 static struct c_declarator *c_parser_direct_declarator (c_parser *, bool,
977 static struct c_declarator *c_parser_direct_declarator_inner (c_parser *,
979 struct c_declarator *);
980 static struct c_arg_info *c_parser_parms_declarator (c_parser *, bool, tree);
981 static struct c_arg_info *c_parser_parms_list_declarator (c_parser *, tree);
982 static struct c_parm *c_parser_parameter_declaration (c_parser *, tree);
983 static tree c_parser_simple_asm_expr (c_parser *);
984 static tree c_parser_attributes (c_parser *);
985 static struct c_type_name *c_parser_type_name (c_parser *);
986 static struct c_expr c_parser_initializer (c_parser *);
987 static struct c_expr c_parser_braced_init (c_parser *, tree, bool);
988 static void c_parser_initelt (c_parser *);
989 static void c_parser_initval (c_parser *, struct c_expr *);
990 static tree c_parser_compound_statement (c_parser *);
991 static void c_parser_compound_statement_nostart (c_parser *);
992 static void c_parser_label (c_parser *);
993 static void c_parser_statement (c_parser *);
994 static void c_parser_statement_after_labels (c_parser *);
995 static void c_parser_if_statement (c_parser *);
996 static void c_parser_switch_statement (c_parser *);
997 static void c_parser_while_statement (c_parser *);
998 static void c_parser_do_statement (c_parser *);
999 static void c_parser_for_statement (c_parser *);
1000 static tree c_parser_asm_statement (c_parser *);
1001 static tree c_parser_asm_operands (c_parser *, bool);
1002 static tree c_parser_asm_clobbers (c_parser *);
1003 static struct c_expr c_parser_expr_no_commas (c_parser *, struct c_expr *);
1004 static struct c_expr c_parser_conditional_expression (c_parser *,
1006 static struct c_expr c_parser_binary_expression (c_parser *, struct c_expr *);
1007 static struct c_expr c_parser_cast_expression (c_parser *, struct c_expr *);
1008 static struct c_expr c_parser_unary_expression (c_parser *);
1009 static struct c_expr c_parser_sizeof_expression (c_parser *);
1010 static struct c_expr c_parser_alignof_expression (c_parser *);
1011 static struct c_expr c_parser_postfix_expression (c_parser *);
1012 static struct c_expr c_parser_postfix_expression_after_paren_type (c_parser *,
1013 struct c_type_name *);
1014 static struct c_expr c_parser_postfix_expression_after_primary (c_parser *,
1016 static struct c_expr c_parser_expression (c_parser *);
1017 static struct c_expr c_parser_expression_conv (c_parser *);
1018 static tree c_parser_expr_list (c_parser *, bool);
1019 static void c_parser_omp_construct (c_parser *);
1020 static void c_parser_omp_threadprivate (c_parser *);
1021 static void c_parser_omp_barrier (c_parser *);
1022 static void c_parser_omp_flush (c_parser *);
1024 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
1025 static bool c_parser_pragma (c_parser *, enum pragma_context);
1027 /* These Objective-C parser functions are only ever called when
1028 compiling Objective-C. */
1029 static void c_parser_objc_class_definition (c_parser *);
1030 static void c_parser_objc_class_instance_variables (c_parser *);
1031 static void c_parser_objc_class_declaration (c_parser *);
1032 static void c_parser_objc_alias_declaration (c_parser *);
1033 static void c_parser_objc_protocol_definition (c_parser *);
1034 static enum tree_code c_parser_objc_method_type (c_parser *);
1035 static void c_parser_objc_method_definition (c_parser *);
1036 static void c_parser_objc_methodprotolist (c_parser *);
1037 static void c_parser_objc_methodproto (c_parser *);
1038 static tree c_parser_objc_method_decl (c_parser *);
1039 static tree c_parser_objc_type_name (c_parser *);
1040 static tree c_parser_objc_protocol_refs (c_parser *);
1041 static void c_parser_objc_try_catch_statement (c_parser *);
1042 static void c_parser_objc_synchronized_statement (c_parser *);
1043 static tree c_parser_objc_selector (c_parser *);
1044 static tree c_parser_objc_selector_arg (c_parser *);
1045 static tree c_parser_objc_receiver (c_parser *);
1046 static tree c_parser_objc_message_args (c_parser *);
1047 static tree c_parser_objc_keywordexpr (c_parser *);
1049 /* Parse a translation unit (C90 6.7, C99 6.9).
1052 external-declarations
1054 external-declarations:
1055 external-declaration
1056 external-declarations external-declaration
1065 c_parser_translation_unit (c_parser *parser)
1067 if (c_parser_next_token_is (parser, CPP_EOF))
1070 pedwarn ("ISO C forbids an empty source file");
1074 void *obstack_position = obstack_alloc (&parser_obstack, 0);
1078 c_parser_external_declaration (parser);
1079 obstack_free (&parser_obstack, obstack_position);
1081 while (c_parser_next_token_is_not (parser, CPP_EOF));
1085 /* Parse an external declaration (C90 6.7, C99 6.9).
1087 external-declaration:
1093 external-declaration:
1096 __extension__ external-declaration
1100 external-declaration:
1101 objc-class-definition
1102 objc-class-declaration
1103 objc-alias-declaration
1104 objc-protocol-definition
1105 objc-method-definition
1110 c_parser_external_declaration (c_parser *parser)
1113 switch (c_parser_peek_token (parser)->type)
1116 switch (c_parser_peek_token (parser)->keyword)
1119 ext = disable_extension_diagnostics ();
1120 c_parser_consume_token (parser);
1121 c_parser_external_declaration (parser);
1122 restore_extension_diagnostics (ext);
1125 c_parser_asm_definition (parser);
1127 case RID_AT_INTERFACE:
1128 case RID_AT_IMPLEMENTATION:
1129 gcc_assert (c_dialect_objc ());
1130 c_parser_objc_class_definition (parser);
1133 gcc_assert (c_dialect_objc ());
1134 c_parser_objc_class_declaration (parser);
1137 gcc_assert (c_dialect_objc ());
1138 c_parser_objc_alias_declaration (parser);
1140 case RID_AT_PROTOCOL:
1141 gcc_assert (c_dialect_objc ());
1142 c_parser_objc_protocol_definition (parser);
1145 gcc_assert (c_dialect_objc ());
1146 c_parser_consume_token (parser);
1147 objc_finish_implementation ();
1155 pedwarn ("ISO C does not allow extra %<;%> outside of a function");
1156 c_parser_consume_token (parser);
1159 c_parser_pragma (parser, pragma_external);
1163 if (c_dialect_objc ())
1165 c_parser_objc_method_definition (parser);
1168 /* Else fall through, and yield a syntax error trying to parse
1169 as a declaration or function definition. */
1172 /* A declaration or a function definition. We can only tell
1173 which after parsing the declaration specifiers, if any, and
1174 the first declarator. */
1175 c_parser_declaration_or_fndef (parser, true, true, false, true);
1181 /* Parse a declaration or function definition (C90 6.5, 6.7.1, C99
1182 6.7, 6.9.1). If FNDEF_OK is true, a function definition is
1183 accepted; otherwise (old-style parameter declarations) only other
1184 declarations are accepted. If NESTED is true, we are inside a
1185 function or parsing old-style parameter declarations; any functions
1186 encountered are nested functions and declaration specifiers are
1187 required; otherwise we are at top level and functions are normal
1188 functions and declaration specifiers may be optional. If EMPTY_OK
1189 is true, empty declarations are OK (subject to all other
1190 constraints); otherwise (old-style parameter declarations) they are
1191 diagnosed. If START_ATTR_OK is true, the declaration specifiers
1192 may start with attributes; otherwise they may not.
1195 declaration-specifiers init-declarator-list[opt] ;
1197 function-definition:
1198 declaration-specifiers[opt] declarator declaration-list[opt]
1203 declaration-list declaration
1205 init-declarator-list:
1207 init-declarator-list , init-declarator
1210 declarator simple-asm-expr[opt] attributes[opt]
1211 declarator simple-asm-expr[opt] attributes[opt] = initializer
1215 nested-function-definition:
1216 declaration-specifiers declarator declaration-list[opt]
1219 The simple-asm-expr and attributes are GNU extensions.
1221 This function does not handle __extension__; that is handled in its
1222 callers. ??? Following the old parser, __extension__ may start
1223 external declarations, declarations in functions and declarations
1224 at the start of "for" loops, but not old-style parameter
1227 C99 requires declaration specifiers in a function definition; the
1228 absence is diagnosed through the diagnosis of implicit int. In GNU
1229 C we also allow but diagnose declarations without declaration
1230 specifiers, but only at top level (elsewhere they conflict with
1236 threadprivate-directive */
1239 c_parser_declaration_or_fndef (c_parser *parser, bool fndef_ok, bool empty_ok,
1240 bool nested, bool start_attr_ok)
1242 struct c_declspecs *specs;
1244 tree all_prefix_attrs;
1245 bool diagnosed_no_specs = false;
1247 specs = build_null_declspecs ();
1248 c_parser_declspecs (parser, specs, true, true, start_attr_ok);
1251 c_parser_skip_to_end_of_block_or_statement (parser);
1254 if (nested && !specs->declspecs_seen_p)
1256 c_parser_error (parser, "expected declaration specifiers");
1257 c_parser_skip_to_end_of_block_or_statement (parser);
1260 finish_declspecs (specs);
1261 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1267 shadow_tag_warned (specs, 1);
1268 pedwarn ("empty declaration");
1270 c_parser_consume_token (parser);
1273 pending_xref_error ();
1274 prefix_attrs = specs->attrs;
1275 all_prefix_attrs = prefix_attrs;
1276 specs->attrs = NULL_TREE;
1279 struct c_declarator *declarator;
1282 /* Declaring either one or more declarators (in which case we
1283 should diagnose if there were no declaration specifiers) or a
1284 function definition (in which case the diagnostic for
1285 implicit int suffices). */
1286 declarator = c_parser_declarator (parser, specs->type_seen_p,
1287 C_DTR_NORMAL, &dummy);
1288 if (declarator == NULL)
1290 c_parser_skip_to_end_of_block_or_statement (parser);
1293 if (c_parser_next_token_is (parser, CPP_EQ)
1294 || c_parser_next_token_is (parser, CPP_COMMA)
1295 || c_parser_next_token_is (parser, CPP_SEMICOLON)
1296 || c_parser_next_token_is_keyword (parser, RID_ASM)
1297 || c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
1299 tree asm_name = NULL_TREE;
1300 tree postfix_attrs = NULL_TREE;
1301 if (!diagnosed_no_specs && !specs->declspecs_seen_p)
1303 diagnosed_no_specs = true;
1304 pedwarn ("data definition has no type or storage class");
1306 /* Having seen a data definition, there cannot now be a
1307 function definition. */
1309 if (c_parser_next_token_is_keyword (parser, RID_ASM))
1310 asm_name = c_parser_simple_asm_expr (parser);
1311 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
1312 postfix_attrs = c_parser_attributes (parser);
1313 if (c_parser_next_token_is (parser, CPP_EQ))
1317 c_parser_consume_token (parser);
1318 /* The declaration of the variable is in effect while
1319 its initializer is parsed. */
1320 d = start_decl (declarator, specs, true,
1321 chainon (postfix_attrs, all_prefix_attrs));
1323 d = error_mark_node;
1324 start_init (d, asm_name, global_bindings_p ());
1325 init = c_parser_initializer (parser);
1327 if (d != error_mark_node)
1329 maybe_warn_string_init (TREE_TYPE (d), init);
1330 finish_decl (d, init.value, asm_name);
1335 tree d = start_decl (declarator, specs, false,
1336 chainon (postfix_attrs,
1339 finish_decl (d, NULL_TREE, asm_name);
1341 if (c_parser_next_token_is (parser, CPP_COMMA))
1343 c_parser_consume_token (parser);
1344 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
1345 all_prefix_attrs = chainon (c_parser_attributes (parser),
1348 all_prefix_attrs = prefix_attrs;
1351 else if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1353 c_parser_consume_token (parser);
1358 c_parser_error (parser, "expected %<,%> or %<;%>");
1359 c_parser_skip_to_end_of_block_or_statement (parser);
1365 c_parser_error (parser, "expected %<=%>, %<,%>, %<;%>, "
1366 "%<asm%> or %<__attribute__%>");
1367 c_parser_skip_to_end_of_block_or_statement (parser);
1370 /* Function definition (nested or otherwise). */
1374 pedwarn ("ISO C forbids nested functions");
1375 push_function_context ();
1377 if (!start_function (specs, declarator, all_prefix_attrs))
1379 /* This can appear in many cases looking nothing like a
1380 function definition, so we don't give a more specific
1381 error suggesting there was one. */
1382 c_parser_error (parser, "expected %<=%>, %<,%>, %<;%>, %<asm%> "
1383 "or %<__attribute__%>");
1385 pop_function_context ();
1388 /* Parse old-style parameter declarations. ??? Attributes are
1389 not allowed to start declaration specifiers here because of a
1390 syntax conflict between a function declaration with attribute
1391 suffix and a function definition with an attribute prefix on
1392 first old-style parameter declaration. Following the old
1393 parser, they are not accepted on subsequent old-style
1394 parameter declarations either. However, there is no
1395 ambiguity after the first declaration, nor indeed on the
1396 first as long as we don't allow postfix attributes after a
1397 declarator with a nonempty identifier list in a definition;
1398 and postfix attributes have never been accepted here in
1399 function definitions either. */
1400 while (c_parser_next_token_is_not (parser, CPP_EOF)
1401 && c_parser_next_token_is_not (parser, CPP_OPEN_BRACE))
1402 c_parser_declaration_or_fndef (parser, false, false, true, false);
1403 DECL_SOURCE_LOCATION (current_function_decl)
1404 = c_parser_peek_token (parser)->location;
1405 store_parm_decls ();
1406 fnbody = c_parser_compound_statement (parser);
1409 tree decl = current_function_decl;
1412 pop_function_context ();
1413 add_stmt (build_stmt (DECL_EXPR, decl));
1424 /* Parse an asm-definition (asm() outside a function body). This is a
1432 c_parser_asm_definition (c_parser *parser)
1434 tree asm_str = c_parser_simple_asm_expr (parser);
1436 cgraph_add_asm_node (asm_str);
1437 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
1440 /* Parse some declaration specifiers (possibly none) (C90 6.5, C99
1441 6.7), adding them to SPECS (which may already include some).
1442 Storage class specifiers are accepted iff SCSPEC_OK; type
1443 specifiers are accepted iff TYPESPEC_OK; attributes are accepted at
1444 the start iff START_ATTR_OK.
1446 declaration-specifiers:
1447 storage-class-specifier declaration-specifiers[opt]
1448 type-specifier declaration-specifiers[opt]
1449 type-qualifier declaration-specifiers[opt]
1450 function-specifier declaration-specifiers[opt]
1452 Function specifiers (inline) are from C99, and are currently
1453 handled as storage class specifiers, as is __thread.
1455 C90 6.5.1, C99 6.7.1:
1456 storage-class-specifier:
1467 C90 6.5.2, C99 6.7.2:
1480 [_Imaginary removed in C99 TC2]
1481 struct-or-union-specifier
1485 (_Bool and _Complex are new in C99.)
1487 C90 6.5.3, C99 6.7.3:
1494 (restrict is new in C99.)
1498 declaration-specifiers:
1499 attributes declaration-specifiers[opt]
1501 storage-class-specifier:
1513 (_Fract, _Accum, and _Sat are new from ISO/IEC DTR 18037:
1514 http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1169.pdf)
1519 class-name objc-protocol-refs[opt]
1520 typedef-name objc-protocol-refs
1525 c_parser_declspecs (c_parser *parser, struct c_declspecs *specs,
1526 bool scspec_ok, bool typespec_ok, bool start_attr_ok)
1528 bool attrs_ok = start_attr_ok;
1529 bool seen_type = specs->type_seen_p;
1530 while (c_parser_next_token_is (parser, CPP_NAME)
1531 || c_parser_next_token_is (parser, CPP_KEYWORD)
1532 || (c_dialect_objc () && c_parser_next_token_is (parser, CPP_LESS)))
1534 struct c_typespec t;
1536 if (c_parser_next_token_is (parser, CPP_NAME))
1538 tree value = c_parser_peek_token (parser)->value;
1539 c_id_kind kind = c_parser_peek_token (parser)->id_kind;
1540 /* This finishes the specifiers unless a type name is OK, it
1541 is declared as a type name and a type name hasn't yet
1543 if (!typespec_ok || seen_type
1544 || (kind != C_ID_TYPENAME && kind != C_ID_CLASSNAME))
1546 c_parser_consume_token (parser);
1549 if (kind == C_ID_TYPENAME
1550 && (!c_dialect_objc ()
1551 || c_parser_next_token_is_not (parser, CPP_LESS)))
1553 t.kind = ctsk_typedef;
1554 /* For a typedef name, record the meaning, not the name.
1555 In case of 'foo foo, bar;'. */
1556 t.spec = lookup_name (value);
1560 tree proto = NULL_TREE;
1561 gcc_assert (c_dialect_objc ());
1563 if (c_parser_next_token_is (parser, CPP_LESS))
1564 proto = c_parser_objc_protocol_refs (parser);
1565 t.spec = objc_get_protocol_qualified_type (value, proto);
1567 declspecs_add_type (specs, t);
1570 if (c_parser_next_token_is (parser, CPP_LESS))
1572 /* Make "<SomeProtocol>" equivalent to "id <SomeProtocol>" -
1573 nisse@lysator.liu.se. */
1575 gcc_assert (c_dialect_objc ());
1576 if (!typespec_ok || seen_type)
1578 proto = c_parser_objc_protocol_refs (parser);
1580 t.spec = objc_get_protocol_qualified_type (NULL_TREE, proto);
1581 declspecs_add_type (specs, t);
1584 gcc_assert (c_parser_next_token_is (parser, CPP_KEYWORD));
1585 switch (c_parser_peek_token (parser)->keyword)
1597 /* TODO: Distinguish between function specifiers (inline)
1598 and storage class specifiers, either here or in
1599 declspecs_add_scspec. */
1600 declspecs_add_scspec (specs, c_parser_peek_token (parser)->value);
1601 c_parser_consume_token (parser);
1624 if (c_dialect_objc ())
1625 parser->objc_need_raw_identifier = true;
1626 t.kind = ctsk_resword;
1627 t.spec = c_parser_peek_token (parser)->value;
1628 declspecs_add_type (specs, t);
1629 c_parser_consume_token (parser);
1636 t = c_parser_enum_specifier (parser);
1637 declspecs_add_type (specs, t);
1645 t = c_parser_struct_or_union_specifier (parser);
1646 declspecs_add_type (specs, t);
1649 /* ??? The old parser rejected typeof after other type
1650 specifiers, but is a syntax error the best way of
1652 if (!typespec_ok || seen_type)
1656 t = c_parser_typeof_specifier (parser);
1657 declspecs_add_type (specs, t);
1663 declspecs_add_qual (specs, c_parser_peek_token (parser)->value);
1664 c_parser_consume_token (parser);
1669 attrs = c_parser_attributes (parser);
1670 declspecs_add_attrs (specs, attrs);
1679 /* Parse an enum specifier (C90 6.5.2.2, C99 6.7.2.2).
1682 enum attributes[opt] identifier[opt] { enumerator-list } attributes[opt]
1683 enum attributes[opt] identifier[opt] { enumerator-list , } attributes[opt]
1684 enum attributes[opt] identifier
1686 The form with trailing comma is new in C99. The forms with
1687 attributes are GNU extensions. In GNU C, we accept any expression
1688 without commas in the syntax (assignment expressions, not just
1689 conditional expressions); assignment expressions will be diagnosed
1694 enumerator-list , enumerator
1697 enumeration-constant
1698 enumeration-constant = constant-expression
1701 static struct c_typespec
1702 c_parser_enum_specifier (c_parser *parser)
1704 struct c_typespec ret;
1706 tree ident = NULL_TREE;
1707 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ENUM));
1708 c_parser_consume_token (parser);
1709 attrs = c_parser_attributes (parser);
1710 /* Set the location in case we create a decl now. */
1711 c_parser_set_source_position_from_token (c_parser_peek_token (parser));
1712 if (c_parser_next_token_is (parser, CPP_NAME))
1714 ident = c_parser_peek_token (parser)->value;
1715 c_parser_consume_token (parser);
1717 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
1719 /* Parse an enum definition. */
1720 struct c_enum_contents the_enum;
1721 tree type = start_enum (&the_enum, ident);
1723 /* We chain the enumerators in reverse order, then put them in
1724 forward order at the end. */
1725 tree values = NULL_TREE;
1726 c_parser_consume_token (parser);
1734 if (c_parser_next_token_is_not (parser, CPP_NAME))
1736 c_parser_error (parser, "expected identifier");
1737 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
1738 values = error_mark_node;
1741 token = c_parser_peek_token (parser);
1742 enum_id = token->value;
1743 /* Set the location in case we create a decl now. */
1744 c_parser_set_source_position_from_token (token);
1745 c_parser_consume_token (parser);
1746 if (c_parser_next_token_is (parser, CPP_EQ))
1748 c_parser_consume_token (parser);
1749 enum_value = c_parser_expr_no_commas (parser, NULL).value;
1752 enum_value = NULL_TREE;
1753 enum_decl = build_enumerator (&the_enum, enum_id, enum_value);
1754 TREE_CHAIN (enum_decl) = values;
1757 if (c_parser_next_token_is (parser, CPP_COMMA))
1760 c_parser_consume_token (parser);
1762 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
1764 if (seen_comma && pedantic && !flag_isoc99)
1765 pedwarn ("comma at end of enumerator list");
1766 c_parser_consume_token (parser);
1771 c_parser_error (parser, "expected %<,%> or %<}%>");
1772 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
1773 values = error_mark_node;
1777 postfix_attrs = c_parser_attributes (parser);
1778 ret.spec = finish_enum (type, nreverse (values),
1779 chainon (attrs, postfix_attrs));
1780 ret.kind = ctsk_tagdef;
1785 c_parser_error (parser, "expected %<{%>");
1786 ret.spec = error_mark_node;
1787 ret.kind = ctsk_tagref;
1790 ret = parser_xref_tag (ENUMERAL_TYPE, ident);
1791 /* In ISO C, enumerated types can be referred to only if already
1793 if (pedantic && !COMPLETE_TYPE_P (ret.spec))
1794 pedwarn ("ISO C forbids forward references to %<enum%> types");
1798 /* Parse a struct or union specifier (C90 6.5.2.1, C99 6.7.2.1).
1800 struct-or-union-specifier:
1801 struct-or-union attributes[opt] identifier[opt]
1802 { struct-contents } attributes[opt]
1803 struct-or-union attributes[opt] identifier
1806 struct-declaration-list
1808 struct-declaration-list:
1809 struct-declaration ;
1810 struct-declaration-list struct-declaration ;
1817 struct-declaration-list struct-declaration
1819 struct-declaration-list:
1820 struct-declaration-list ;
1823 (Note that in the syntax here, unlike that in ISO C, the semicolons
1824 are included here rather than in struct-declaration, in order to
1825 describe the syntax with extra semicolons and missing semicolon at
1830 struct-declaration-list:
1831 @defs ( class-name )
1833 (Note this does not include a trailing semicolon, but can be
1834 followed by further declarations, and gets a pedwarn-if-pedantic
1835 when followed by a semicolon.) */
1837 static struct c_typespec
1838 c_parser_struct_or_union_specifier (c_parser *parser)
1840 struct c_typespec ret;
1842 tree ident = NULL_TREE;
1843 enum tree_code code;
1844 switch (c_parser_peek_token (parser)->keyword)
1855 c_parser_consume_token (parser);
1856 attrs = c_parser_attributes (parser);
1857 /* Set the location in case we create a decl now. */
1858 c_parser_set_source_position_from_token (c_parser_peek_token (parser));
1859 if (c_parser_next_token_is (parser, CPP_NAME))
1861 ident = c_parser_peek_token (parser)->value;
1862 c_parser_consume_token (parser);
1864 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
1866 /* Parse a struct or union definition. Start the scope of the
1867 tag before parsing components. */
1868 tree type = start_struct (code, ident);
1870 /* We chain the components in reverse order, then put them in
1871 forward order at the end. Each struct-declaration may
1872 declare multiple components (comma-separated), so we must use
1873 chainon to join them, although when parsing each
1874 struct-declaration we can use TREE_CHAIN directly.
1876 The theory behind all this is that there will be more
1877 semicolon separated fields than comma separated fields, and
1878 so we'll be minimizing the number of node traversals required
1880 tree contents = NULL_TREE;
1881 c_parser_consume_token (parser);
1882 /* Handle the Objective-C @defs construct,
1883 e.g. foo(sizeof(struct{ @defs(ClassName) }));. */
1884 if (c_parser_next_token_is_keyword (parser, RID_AT_DEFS))
1887 gcc_assert (c_dialect_objc ());
1888 c_parser_consume_token (parser);
1889 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
1891 if (c_parser_next_token_is (parser, CPP_NAME)
1892 && c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME)
1894 name = c_parser_peek_token (parser)->value;
1895 c_parser_consume_token (parser);
1899 c_parser_error (parser, "expected class name");
1900 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
1903 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
1905 contents = nreverse (objc_get_class_ivars (name));
1908 /* Parse the struct-declarations and semicolons. Problems with
1909 semicolons are diagnosed here; empty structures are diagnosed
1914 /* Parse any stray semicolon. */
1915 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1918 pedwarn ("extra semicolon in struct or union specified");
1919 c_parser_consume_token (parser);
1922 /* Stop if at the end of the struct or union contents. */
1923 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
1925 c_parser_consume_token (parser);
1928 /* Accept #pragmas at struct scope. */
1929 if (c_parser_next_token_is (parser, CPP_PRAGMA))
1931 c_parser_pragma (parser, pragma_external);
1934 /* Parse some comma-separated declarations, but not the
1935 trailing semicolon if any. */
1936 decls = c_parser_struct_declaration (parser);
1937 contents = chainon (decls, contents);
1938 /* If no semicolon follows, either we have a parse error or
1939 are at the end of the struct or union and should
1941 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1942 c_parser_consume_token (parser);
1945 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
1946 pedwarn ("no semicolon at end of struct or union");
1949 c_parser_error (parser, "expected %<;%>");
1950 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
1955 postfix_attrs = c_parser_attributes (parser);
1956 ret.spec = finish_struct (type, nreverse (contents),
1957 chainon (attrs, postfix_attrs));
1958 ret.kind = ctsk_tagdef;
1963 c_parser_error (parser, "expected %<{%>");
1964 ret.spec = error_mark_node;
1965 ret.kind = ctsk_tagref;
1968 ret = parser_xref_tag (code, ident);
1972 /* Parse a struct-declaration (C90 6.5.2.1, C99 6.7.2.1), *without*
1973 the trailing semicolon.
1976 specifier-qualifier-list struct-declarator-list
1978 specifier-qualifier-list:
1979 type-specifier specifier-qualifier-list[opt]
1980 type-qualifier specifier-qualifier-list[opt]
1981 attributes specifier-qualifier-list[opt]
1983 struct-declarator-list:
1985 struct-declarator-list , attributes[opt] struct-declarator
1988 declarator attributes[opt]
1989 declarator[opt] : constant-expression attributes[opt]
1994 __extension__ struct-declaration
1995 specifier-qualifier-list
1997 Unlike the ISO C syntax, semicolons are handled elsewhere. The use
1998 of attributes where shown is a GNU extension. In GNU C, we accept
1999 any expression without commas in the syntax (assignment
2000 expressions, not just conditional expressions); assignment
2001 expressions will be diagnosed as non-constant. */
2004 c_parser_struct_declaration (c_parser *parser)
2006 struct c_declspecs *specs;
2008 tree all_prefix_attrs;
2010 if (c_parser_next_token_is_keyword (parser, RID_EXTENSION))
2014 ext = disable_extension_diagnostics ();
2015 c_parser_consume_token (parser);
2016 decl = c_parser_struct_declaration (parser);
2017 restore_extension_diagnostics (ext);
2020 specs = build_null_declspecs ();
2021 c_parser_declspecs (parser, specs, false, true, true);
2024 if (!specs->declspecs_seen_p)
2026 c_parser_error (parser, "expected specifier-qualifier-list");
2029 finish_declspecs (specs);
2030 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
2033 if (!specs->type_seen_p)
2036 pedwarn ("ISO C forbids member declarations with no members");
2037 shadow_tag_warned (specs, pedantic);
2042 /* Support for unnamed structs or unions as members of
2043 structs or unions (which is [a] useful and [b] supports
2046 ret = grokfield (build_id_declarator (NULL_TREE), specs,
2049 decl_attributes (&ret, attrs, 0);
2053 pending_xref_error ();
2054 prefix_attrs = specs->attrs;
2055 all_prefix_attrs = prefix_attrs;
2056 specs->attrs = NULL_TREE;
2060 /* Declaring one or more declarators or un-named bit-fields. */
2061 struct c_declarator *declarator;
2063 if (c_parser_next_token_is (parser, CPP_COLON))
2064 declarator = build_id_declarator (NULL_TREE);
2066 declarator = c_parser_declarator (parser, specs->type_seen_p,
2067 C_DTR_NORMAL, &dummy);
2068 if (declarator == NULL)
2070 c_parser_skip_to_end_of_block_or_statement (parser);
2073 if (c_parser_next_token_is (parser, CPP_COLON)
2074 || c_parser_next_token_is (parser, CPP_COMMA)
2075 || c_parser_next_token_is (parser, CPP_SEMICOLON)
2076 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE)
2077 || c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2079 tree postfix_attrs = NULL_TREE;
2080 tree width = NULL_TREE;
2082 if (c_parser_next_token_is (parser, CPP_COLON))
2084 c_parser_consume_token (parser);
2085 width = c_parser_expr_no_commas (parser, NULL).value;
2087 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2088 postfix_attrs = c_parser_attributes (parser);
2089 d = grokfield (declarator, specs, width, &all_prefix_attrs);
2090 decl_attributes (&d, chainon (postfix_attrs,
2091 all_prefix_attrs), 0);
2092 TREE_CHAIN (d) = decls;
2094 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2095 all_prefix_attrs = chainon (c_parser_attributes (parser),
2098 all_prefix_attrs = prefix_attrs;
2099 if (c_parser_next_token_is (parser, CPP_COMMA))
2100 c_parser_consume_token (parser);
2101 else if (c_parser_next_token_is (parser, CPP_SEMICOLON)
2102 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2104 /* Semicolon consumed in caller. */
2109 c_parser_error (parser, "expected %<,%>, %<;%> or %<}%>");
2115 c_parser_error (parser,
2116 "expected %<:%>, %<,%>, %<;%>, %<}%> or "
2117 "%<__attribute__%>");
2124 /* Parse a typeof specifier (a GNU extension).
2127 typeof ( expression )
2128 typeof ( type-name )
2131 static struct c_typespec
2132 c_parser_typeof_specifier (c_parser *parser)
2134 struct c_typespec ret;
2135 ret.kind = ctsk_typeof;
2136 ret.spec = error_mark_node;
2137 gcc_assert (c_parser_next_token_is_keyword (parser, RID_TYPEOF));
2138 c_parser_consume_token (parser);
2141 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2147 if (c_parser_next_token_starts_typename (parser))
2149 struct c_type_name *type = c_parser_type_name (parser);
2154 ret.spec = groktypename (type);
2155 pop_maybe_used (variably_modified_type_p (ret.spec, NULL_TREE));
2161 struct c_expr expr = c_parser_expression (parser);
2164 if (TREE_CODE (expr.value) == COMPONENT_REF
2165 && DECL_C_BIT_FIELD (TREE_OPERAND (expr.value, 1)))
2166 error ("%<typeof%> applied to a bit-field");
2167 ret.spec = TREE_TYPE (expr.value);
2168 was_vm = variably_modified_type_p (ret.spec, NULL_TREE);
2169 /* This should be returned with the type so that when the type
2170 is evaluated, this can be evaluated. For now, we avoid
2171 evaluation when the context might. */
2172 if (!skip_evaluation && was_vm)
2174 tree e = expr.value;
2176 /* If the expression is not of a type to which we cannot assign a line
2177 number, wrap the thing in a no-op NOP_EXPR. */
2178 if (DECL_P (e) || CONSTANT_CLASS_P (e))
2179 e = build1 (NOP_EXPR, void_type_node, e);
2181 if (CAN_HAVE_LOCATION_P (e))
2182 SET_EXPR_LOCATION (e, input_location);
2186 pop_maybe_used (was_vm);
2188 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
2192 /* Parse a declarator, possibly an abstract declarator (C90 6.5.4,
2193 6.5.5, C99 6.7.5, 6.7.6). If TYPE_SEEN_P then a typedef name may
2194 be redeclared; otherwise it may not. KIND indicates which kind of
2195 declarator is wanted. Returns a valid declarator except in the
2196 case of a syntax error in which case NULL is returned. *SEEN_ID is
2197 set to true if an identifier being declared is seen; this is used
2198 to diagnose bad forms of abstract array declarators and to
2199 determine whether an identifier list is syntactically permitted.
2202 pointer[opt] direct-declarator
2206 ( attributes[opt] declarator )
2207 direct-declarator array-declarator
2208 direct-declarator ( parameter-type-list )
2209 direct-declarator ( identifier-list[opt] )
2212 * type-qualifier-list[opt]
2213 * type-qualifier-list[opt] pointer
2215 type-qualifier-list:
2218 type-qualifier-list type-qualifier
2219 type-qualifier-list attributes
2221 parameter-type-list:
2223 parameter-list , ...
2226 parameter-declaration
2227 parameter-list , parameter-declaration
2229 parameter-declaration:
2230 declaration-specifiers declarator attributes[opt]
2231 declaration-specifiers abstract-declarator[opt] attributes[opt]
2235 identifier-list , identifier
2237 abstract-declarator:
2239 pointer[opt] direct-abstract-declarator
2241 direct-abstract-declarator:
2242 ( attributes[opt] abstract-declarator )
2243 direct-abstract-declarator[opt] array-declarator
2244 direct-abstract-declarator[opt] ( parameter-type-list[opt] )
2249 direct-declarator ( parameter-forward-declarations
2250 parameter-type-list[opt] )
2252 direct-abstract-declarator:
2253 direct-abstract-declarator[opt] ( parameter-forward-declarations
2254 parameter-type-list[opt] )
2256 parameter-forward-declarations:
2258 parameter-forward-declarations parameter-list ;
2260 The uses of attributes shown above are GNU extensions.
2262 Some forms of array declarator are not included in C99 in the
2263 syntax for abstract declarators; these are disallowed elsewhere.
2264 This may be a defect (DR#289).
2266 This function also accepts an omitted abstract declarator as being
2267 an abstract declarator, although not part of the formal syntax. */
2269 static struct c_declarator *
2270 c_parser_declarator (c_parser *parser, bool type_seen_p, c_dtr_syn kind,
2273 /* Parse any initial pointer part. */
2274 if (c_parser_next_token_is (parser, CPP_MULT))
2276 struct c_declspecs *quals_attrs = build_null_declspecs ();
2277 struct c_declarator *inner;
2278 c_parser_consume_token (parser);
2279 c_parser_declspecs (parser, quals_attrs, false, false, true);
2280 inner = c_parser_declarator (parser, type_seen_p, kind, seen_id);
2284 return make_pointer_declarator (quals_attrs, inner);
2286 /* Now we have a direct declarator, direct abstract declarator or
2287 nothing (which counts as a direct abstract declarator here). */
2288 return c_parser_direct_declarator (parser, type_seen_p, kind, seen_id);
2291 /* Parse a direct declarator or direct abstract declarator; arguments
2292 as c_parser_declarator. */
2294 static struct c_declarator *
2295 c_parser_direct_declarator (c_parser *parser, bool type_seen_p, c_dtr_syn kind,
2298 /* The direct declarator must start with an identifier (possibly
2299 omitted) or a parenthesized declarator (possibly abstract). In
2300 an ordinary declarator, initial parentheses must start a
2301 parenthesized declarator. In an abstract declarator or parameter
2302 declarator, they could start a parenthesized declarator or a
2303 parameter list. To tell which, the open parenthesis and any
2304 following attributes must be read. If a declaration specifier
2305 follows, then it is a parameter list; if the specifier is a
2306 typedef name, there might be an ambiguity about redeclaring it,
2307 which is resolved in the direction of treating it as a typedef
2308 name. If a close parenthesis follows, it is also an empty
2309 parameter list, as the syntax does not permit empty abstract
2310 declarators. Otherwise, it is a parenthesized declarator (in
2311 which case the analysis may be repeated inside it, recursively).
2313 ??? There is an ambiguity in a parameter declaration "int
2314 (__attribute__((foo)) x)", where x is not a typedef name: it
2315 could be an abstract declarator for a function, or declare x with
2316 parentheses. The proper resolution of this ambiguity needs
2317 documenting. At present we follow an accident of the old
2318 parser's implementation, whereby the first parameter must have
2319 some declaration specifiers other than just attributes. Thus as
2320 a parameter declaration it is treated as a parenthesized
2321 parameter named x, and as an abstract declarator it is
2324 ??? Also following the old parser, attributes inside an empty
2325 parameter list are ignored, making it a list not yielding a
2326 prototype, rather than giving an error or making it have one
2327 parameter with implicit type int.
2329 ??? Also following the old parser, typedef names may be
2330 redeclared in declarators, but not Objective-C class names. */
2332 if (kind != C_DTR_ABSTRACT
2333 && c_parser_next_token_is (parser, CPP_NAME)
2335 && c_parser_peek_token (parser)->id_kind == C_ID_TYPENAME)
2336 || c_parser_peek_token (parser)->id_kind == C_ID_ID))
2338 struct c_declarator *inner
2339 = build_id_declarator (c_parser_peek_token (parser)->value);
2341 inner->id_loc = c_parser_peek_token (parser)->location;
2342 c_parser_consume_token (parser);
2343 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
2346 if (kind != C_DTR_NORMAL
2347 && c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
2349 struct c_declarator *inner = build_id_declarator (NULL_TREE);
2350 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
2353 /* Either we are at the end of an abstract declarator, or we have
2356 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
2359 struct c_declarator *inner;
2360 c_parser_consume_token (parser);
2361 attrs = c_parser_attributes (parser);
2362 if (kind != C_DTR_NORMAL
2363 && (c_parser_next_token_starts_declspecs (parser)
2364 || c_parser_next_token_is (parser, CPP_CLOSE_PAREN)))
2366 struct c_arg_info *args
2367 = c_parser_parms_declarator (parser, kind == C_DTR_NORMAL,
2374 = build_function_declarator (args,
2375 build_id_declarator (NULL_TREE));
2376 return c_parser_direct_declarator_inner (parser, *seen_id,
2380 /* A parenthesized declarator. */
2381 inner = c_parser_declarator (parser, type_seen_p, kind, seen_id);
2382 if (inner != NULL && attrs != NULL)
2383 inner = build_attrs_declarator (attrs, inner);
2384 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2386 c_parser_consume_token (parser);
2390 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
2394 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2401 if (kind == C_DTR_NORMAL)
2403 c_parser_error (parser, "expected identifier or %<(%>");
2407 return build_id_declarator (NULL_TREE);
2411 /* Parse part of a direct declarator or direct abstract declarator,
2412 given that some (in INNER) has already been parsed; ID_PRESENT is
2413 true if an identifier is present, false for an abstract
2416 static struct c_declarator *
2417 c_parser_direct_declarator_inner (c_parser *parser, bool id_present,
2418 struct c_declarator *inner)
2420 /* Parse a sequence of array declarators and parameter lists. */
2421 if (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
2423 struct c_declarator *declarator;
2424 struct c_declspecs *quals_attrs = build_null_declspecs ();
2428 c_parser_consume_token (parser);
2429 c_parser_declspecs (parser, quals_attrs, false, false, true);
2430 static_seen = c_parser_next_token_is_keyword (parser, RID_STATIC);
2432 c_parser_consume_token (parser);
2433 if (static_seen && !quals_attrs->declspecs_seen_p)
2434 c_parser_declspecs (parser, quals_attrs, false, false, true);
2435 if (!quals_attrs->declspecs_seen_p)
2437 /* If "static" is present, there must be an array dimension.
2438 Otherwise, there may be a dimension, "*", or no
2443 dimen = c_parser_expr_no_commas (parser, NULL).value;
2447 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
2452 else if (c_parser_next_token_is (parser, CPP_MULT))
2454 if (c_parser_peek_2nd_token (parser)->type == CPP_CLOSE_SQUARE)
2458 c_parser_consume_token (parser);
2463 dimen = c_parser_expr_no_commas (parser, NULL).value;
2469 dimen = c_parser_expr_no_commas (parser, NULL).value;
2472 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
2473 c_parser_consume_token (parser);
2476 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
2480 declarator = build_array_declarator (dimen, quals_attrs, static_seen,
2482 if (declarator == NULL)
2484 inner = set_array_declarator_inner (declarator, inner, !id_present);
2485 return c_parser_direct_declarator_inner (parser, id_present, inner);
2487 else if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
2490 struct c_arg_info *args;
2491 c_parser_consume_token (parser);
2492 attrs = c_parser_attributes (parser);
2493 args = c_parser_parms_declarator (parser, id_present, attrs);
2498 inner = build_function_declarator (args, inner);
2499 return c_parser_direct_declarator_inner (parser, id_present, inner);
2505 /* Parse a parameter list or identifier list, including the closing
2506 parenthesis but not the opening one. ATTRS are the attributes at
2507 the start of the list. ID_LIST_OK is true if an identifier list is
2508 acceptable; such a list must not have attributes at the start. */
2510 static struct c_arg_info *
2511 c_parser_parms_declarator (c_parser *parser, bool id_list_ok, tree attrs)
2514 declare_parm_level ();
2515 /* If the list starts with an identifier, it is an identifier list.
2516 Otherwise, it is either a prototype list or an empty list. */
2519 && c_parser_next_token_is (parser, CPP_NAME)
2520 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
2522 tree list = NULL_TREE, *nextp = &list;
2523 while (c_parser_next_token_is (parser, CPP_NAME)
2524 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
2526 *nextp = build_tree_list (NULL_TREE,
2527 c_parser_peek_token (parser)->value);
2528 nextp = & TREE_CHAIN (*nextp);
2529 c_parser_consume_token (parser);
2530 if (c_parser_next_token_is_not (parser, CPP_COMMA))
2532 c_parser_consume_token (parser);
2533 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2535 c_parser_error (parser, "expected identifier");
2539 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2541 struct c_arg_info *ret = XOBNEW (&parser_obstack, struct c_arg_info);
2546 ret->pending_sizes = 0;
2547 ret->had_vla_unspec = 0;
2548 c_parser_consume_token (parser);
2554 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2562 struct c_arg_info *ret = c_parser_parms_list_declarator (parser, attrs);
2568 /* Parse a parameter list (possibly empty), including the closing
2569 parenthesis but not the opening one. ATTRS are the attributes at
2570 the start of the list. */
2572 static struct c_arg_info *
2573 c_parser_parms_list_declarator (c_parser *parser, tree attrs)
2575 bool good_parm = false;
2576 /* ??? Following the old parser, forward parameter declarations may
2577 use abstract declarators, and if no real parameter declarations
2578 follow the forward declarations then this is not diagnosed. Also
2579 note as above that attributes are ignored as the only contents of
2580 the parentheses, or as the only contents after forward
2582 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2584 struct c_arg_info *ret = XOBNEW (&parser_obstack, struct c_arg_info);
2589 ret->pending_sizes = 0;
2590 ret->had_vla_unspec = 0;
2591 c_parser_consume_token (parser);
2594 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
2596 struct c_arg_info *ret = XOBNEW (&parser_obstack, struct c_arg_info);
2600 ret->pending_sizes = 0;
2601 ret->had_vla_unspec = 0;
2602 /* Suppress -Wold-style-definition for this case. */
2603 ret->types = error_mark_node;
2604 error ("ISO C requires a named argument before %<...%>");
2605 c_parser_consume_token (parser);
2606 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2608 c_parser_consume_token (parser);
2613 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2618 /* Nonempty list of parameters, either terminated with semicolon
2619 (forward declarations; recurse) or with close parenthesis (normal
2620 function) or with ", ... )" (variadic function). */
2623 /* Parse a parameter. */
2624 struct c_parm *parm = c_parser_parameter_declaration (parser, attrs);
2629 push_parm_decl (parm);
2631 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
2634 c_parser_consume_token (parser);
2635 mark_forward_parm_decls ();
2636 new_attrs = c_parser_attributes (parser);
2637 return c_parser_parms_list_declarator (parser, new_attrs);
2639 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2641 c_parser_consume_token (parser);
2643 return get_parm_info (false);
2646 struct c_arg_info *ret
2647 = XOBNEW (&parser_obstack, struct c_arg_info);
2652 ret->pending_sizes = 0;
2653 ret->had_vla_unspec = 0;
2657 if (!c_parser_require (parser, CPP_COMMA,
2658 "expected %<;%>, %<,%> or %<)%>"))
2660 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
2663 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
2665 c_parser_consume_token (parser);
2666 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2668 c_parser_consume_token (parser);
2670 return get_parm_info (true);
2673 struct c_arg_info *ret
2674 = XOBNEW (&parser_obstack, struct c_arg_info);
2679 ret->pending_sizes = 0;
2680 ret->had_vla_unspec = 0;
2686 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2694 /* Parse a parameter declaration. ATTRS are the attributes at the
2695 start of the declaration if it is the first parameter. */
2697 static struct c_parm *
2698 c_parser_parameter_declaration (c_parser *parser, tree attrs)
2700 struct c_declspecs *specs;
2701 struct c_declarator *declarator;
2703 tree postfix_attrs = NULL_TREE;
2705 if (!c_parser_next_token_starts_declspecs (parser))
2707 /* ??? In some Objective-C cases '...' isn't applicable so there
2708 should be a different message. */
2709 c_parser_error (parser,
2710 "expected declaration specifiers or %<...%>");
2711 c_parser_skip_to_end_of_parameter (parser);
2714 specs = build_null_declspecs ();
2717 declspecs_add_attrs (specs, attrs);
2720 c_parser_declspecs (parser, specs, true, true, true);
2721 finish_declspecs (specs);
2722 pending_xref_error ();
2723 prefix_attrs = specs->attrs;
2724 specs->attrs = NULL_TREE;
2725 declarator = c_parser_declarator (parser, specs->type_seen_p,
2726 C_DTR_PARM, &dummy);
2727 if (declarator == NULL)
2729 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
2732 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2733 postfix_attrs = c_parser_attributes (parser);
2734 return build_c_parm (specs, chainon (postfix_attrs, prefix_attrs),
2738 /* Parse a string literal in an asm expression. It should not be
2739 translated, and wide string literals are an error although
2740 permitted by the syntax. This is a GNU extension.
2745 ??? At present, following the old parser, the caller needs to have
2746 set lex_untranslated_string to 1. It would be better to follow the
2747 C++ parser rather than using this kludge. */
2750 c_parser_asm_string_literal (c_parser *parser)
2753 if (c_parser_next_token_is (parser, CPP_STRING))
2755 str = c_parser_peek_token (parser)->value;
2756 c_parser_consume_token (parser);
2758 else if (c_parser_next_token_is (parser, CPP_WSTRING))
2760 error ("wide string literal in %<asm%>");
2761 str = build_string (1, "");
2762 c_parser_consume_token (parser);
2766 c_parser_error (parser, "expected string literal");
2772 /* Parse a simple asm expression. This is used in restricted
2773 contexts, where a full expression with inputs and outputs does not
2774 make sense. This is a GNU extension.
2777 asm ( asm-string-literal )
2781 c_parser_simple_asm_expr (c_parser *parser)
2784 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ASM));
2785 /* ??? Follow the C++ parser rather than using the
2786 lex_untranslated_string kludge. */
2787 parser->lex_untranslated_string = true;
2788 c_parser_consume_token (parser);
2789 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2791 parser->lex_untranslated_string = false;
2794 str = c_parser_asm_string_literal (parser);
2795 parser->lex_untranslated_string = false;
2796 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
2798 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
2804 /* Parse (possibly empty) attributes. This is a GNU extension.
2808 attributes attribute
2811 __attribute__ ( ( attribute-list ) )
2815 attribute_list , attrib
2820 any-word ( identifier )
2821 any-word ( identifier , nonempty-expr-list )
2822 any-word ( expr-list )
2824 where the "identifier" must not be declared as a type, and
2825 "any-word" may be any identifier (including one declared as a
2826 type), a reserved word storage class specifier, type specifier or
2827 type qualifier. ??? This still leaves out most reserved keywords
2828 (following the old parser), shouldn't we include them, and why not
2829 allow identifiers declared as types to start the arguments? */
2832 c_parser_attributes (c_parser *parser)
2834 tree attrs = NULL_TREE;
2835 while (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2837 /* ??? Follow the C++ parser rather than using the
2838 lex_untranslated_string kludge. */
2839 parser->lex_untranslated_string = true;
2840 c_parser_consume_token (parser);
2841 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2843 parser->lex_untranslated_string = false;
2846 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2848 parser->lex_untranslated_string = false;
2849 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
2852 /* Parse the attribute list. */
2853 while (c_parser_next_token_is (parser, CPP_COMMA)
2854 || c_parser_next_token_is (parser, CPP_NAME)
2855 || c_parser_next_token_is (parser, CPP_KEYWORD))
2857 tree attr, attr_name, attr_args;
2858 if (c_parser_next_token_is (parser, CPP_COMMA))
2860 c_parser_consume_token (parser);
2863 if (c_parser_next_token_is (parser, CPP_KEYWORD))
2865 /* ??? See comment above about what keywords are
2868 switch (c_parser_peek_token (parser)->keyword)
2906 attr_name = c_parser_peek_token (parser)->value;
2907 c_parser_consume_token (parser);
2908 if (c_parser_next_token_is_not (parser, CPP_OPEN_PAREN))
2910 attr = build_tree_list (attr_name, NULL_TREE);
2911 attrs = chainon (attrs, attr);
2914 c_parser_consume_token (parser);
2915 /* Parse the attribute contents. If they start with an
2916 identifier which is followed by a comma or close
2917 parenthesis, then the arguments start with that
2918 identifier; otherwise they are an expression list. */
2919 if (c_parser_next_token_is (parser, CPP_NAME)
2920 && c_parser_peek_token (parser)->id_kind == C_ID_ID
2921 && ((c_parser_peek_2nd_token (parser)->type == CPP_COMMA)
2922 || (c_parser_peek_2nd_token (parser)->type
2923 == CPP_CLOSE_PAREN)))
2925 tree arg1 = c_parser_peek_token (parser)->value;
2926 c_parser_consume_token (parser);
2927 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2928 attr_args = build_tree_list (NULL_TREE, arg1);
2931 c_parser_consume_token (parser);
2932 attr_args = tree_cons (NULL_TREE, arg1,
2933 c_parser_expr_list (parser, false));
2938 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2939 attr_args = NULL_TREE;
2941 attr_args = c_parser_expr_list (parser, false);
2943 attr = build_tree_list (attr_name, attr_args);
2944 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2945 c_parser_consume_token (parser);
2948 parser->lex_untranslated_string = false;
2949 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2953 attrs = chainon (attrs, attr);
2955 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2956 c_parser_consume_token (parser);
2959 parser->lex_untranslated_string = false;
2960 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2964 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2965 c_parser_consume_token (parser);
2968 parser->lex_untranslated_string = false;
2969 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2973 parser->lex_untranslated_string = false;
2978 /* Parse a type name (C90 6.5.5, C99 6.7.6).
2981 specifier-qualifier-list abstract-declarator[opt]
2984 static struct c_type_name *
2985 c_parser_type_name (c_parser *parser)
2987 struct c_declspecs *specs = build_null_declspecs ();
2988 struct c_declarator *declarator;
2989 struct c_type_name *ret;
2991 c_parser_declspecs (parser, specs, false, true, true);
2992 if (!specs->declspecs_seen_p)
2994 c_parser_error (parser, "expected specifier-qualifier-list");
2997 pending_xref_error ();
2998 finish_declspecs (specs);
2999 declarator = c_parser_declarator (parser, specs->type_seen_p,
3000 C_DTR_ABSTRACT, &dummy);
3001 if (declarator == NULL)
3003 ret = XOBNEW (&parser_obstack, struct c_type_name);
3005 ret->declarator = declarator;
3009 /* Parse an initializer (C90 6.5.7, C99 6.7.8).
3012 assignment-expression
3013 { initializer-list }
3014 { initializer-list , }
3017 designation[opt] initializer
3018 initializer-list , designation[opt] initializer
3025 designator-list designator
3032 [ constant-expression ]
3044 [ constant-expression ... constant-expression ]
3046 Any expression without commas is accepted in the syntax for the
3047 constant-expressions, with non-constant expressions rejected later.
3049 This function is only used for top-level initializers; for nested
3050 ones, see c_parser_initval. */
3052 static struct c_expr
3053 c_parser_initializer (c_parser *parser)
3055 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
3056 return c_parser_braced_init (parser, NULL_TREE, false);
3060 ret = c_parser_expr_no_commas (parser, NULL);
3061 if (TREE_CODE (ret.value) != STRING_CST
3062 && TREE_CODE (ret.value) != COMPOUND_LITERAL_EXPR)
3063 ret = default_function_array_conversion (ret);
3068 /* Parse a braced initializer list. TYPE is the type specified for a
3069 compound literal, and NULL_TREE for other initializers and for
3070 nested braced lists. NESTED_P is true for nested braced lists,
3071 false for the list of a compound literal or the list that is the
3072 top-level initializer in a declaration. */
3074 static struct c_expr
3075 c_parser_braced_init (c_parser *parser, tree type, bool nested_p)
3077 gcc_assert (c_parser_next_token_is (parser, CPP_OPEN_BRACE));
3078 c_parser_consume_token (parser);
3080 push_init_level (0);
3082 really_start_incremental_init (type);
3083 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3086 pedwarn ("ISO C forbids empty initializer braces");
3090 /* Parse a non-empty initializer list, possibly with a trailing
3094 c_parser_initelt (parser);
3097 if (c_parser_next_token_is (parser, CPP_COMMA))
3098 c_parser_consume_token (parser);
3101 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3105 if (c_parser_next_token_is_not (parser, CPP_CLOSE_BRACE))
3108 ret.value = error_mark_node;
3109 ret.original_code = ERROR_MARK;
3110 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, "expected %<}%>");
3113 c_parser_consume_token (parser);
3114 return pop_init_level (0);
3117 /* Parse a nested initializer, including designators. */
3120 c_parser_initelt (c_parser *parser)
3122 /* Parse any designator or designator list. A single array
3123 designator may have the subsequent "=" omitted in GNU C, but a
3124 longer list or a structure member designator may not. */
3125 if (c_parser_next_token_is (parser, CPP_NAME)
3126 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
3128 /* Old-style structure member designator. */
3129 set_init_label (c_parser_peek_token (parser)->value);
3131 pedwarn ("obsolete use of designated initializer with %<:%>");
3132 c_parser_consume_token (parser);
3133 c_parser_consume_token (parser);
3137 /* des_seen is 0 if there have been no designators, 1 if there
3138 has been a single array designator and 2 otherwise. */
3140 while (c_parser_next_token_is (parser, CPP_OPEN_SQUARE)
3141 || c_parser_next_token_is (parser, CPP_DOT))
3143 int des_prev = des_seen;
3146 if (c_parser_next_token_is (parser, CPP_DOT))
3149 c_parser_consume_token (parser);
3150 if (c_parser_next_token_is (parser, CPP_NAME))
3152 set_init_label (c_parser_peek_token (parser)->value);
3153 c_parser_consume_token (parser);
3158 init.value = error_mark_node;
3159 init.original_code = ERROR_MARK;
3160 c_parser_error (parser, "expected identifier");
3161 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
3162 process_init_element (init);
3169 /* ??? Following the old parser, [ objc-receiver
3170 objc-message-args ] is accepted as an initializer,
3171 being distinguished from a designator by what follows
3172 the first assignment expression inside the square
3173 brackets, but after a first array designator a
3174 subsequent square bracket is for Objective-C taken to
3175 start an expression, using the obsolete form of
3176 designated initializer without '=', rather than
3177 possibly being a second level of designation: in LALR
3178 terms, the '[' is shifted rather than reducing
3179 designator to designator-list. */
3180 if (des_prev == 1 && c_dialect_objc ())
3182 des_seen = des_prev;
3185 if (des_prev == 0 && c_dialect_objc ())
3187 /* This might be an array designator or an
3188 Objective-C message expression. If the former,
3189 continue parsing here; if the latter, parse the
3190 remainder of the initializer given the starting
3191 primary-expression. ??? It might make sense to
3192 distinguish when des_prev == 1 as well; see
3193 previous comment. */
3195 struct c_expr mexpr;
3196 c_parser_consume_token (parser);
3197 if (c_parser_peek_token (parser)->type == CPP_NAME
3198 && ((c_parser_peek_token (parser)->id_kind
3200 || (c_parser_peek_token (parser)->id_kind
3201 == C_ID_CLASSNAME)))
3203 /* Type name receiver. */
3204 tree id = c_parser_peek_token (parser)->value;
3205 c_parser_consume_token (parser);
3206 rec = objc_get_class_reference (id);
3207 goto parse_message_args;
3209 first = c_parser_expr_no_commas (parser, NULL).value;
3210 if (c_parser_next_token_is (parser, CPP_ELLIPSIS)
3211 || c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
3212 goto array_desig_after_first;
3213 /* Expression receiver. So far only one part
3214 without commas has been parsed; there might be
3215 more of the expression. */
3217 while (c_parser_next_token_is (parser, CPP_COMMA))
3220 c_parser_consume_token (parser);
3221 next = c_parser_expr_no_commas (parser, NULL);
3222 next = default_function_array_conversion (next);
3223 rec = build_compound_expr (rec, next.value);
3226 /* Now parse the objc-message-args. */
3227 args = c_parser_objc_message_args (parser);
3228 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
3231 = objc_build_message_expr (build_tree_list (rec, args));
3232 mexpr.original_code = ERROR_MARK;
3233 /* Now parse and process the remainder of the
3234 initializer, starting with this message
3235 expression as a primary-expression. */
3236 c_parser_initval (parser, &mexpr);
3239 c_parser_consume_token (parser);
3240 first = c_parser_expr_no_commas (parser, NULL).value;
3241 array_desig_after_first:
3242 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
3244 c_parser_consume_token (parser);
3245 second = c_parser_expr_no_commas (parser, NULL).value;
3249 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
3251 c_parser_consume_token (parser);
3252 set_init_index (first, second);
3253 if (pedantic && second)
3254 pedwarn ("ISO C forbids specifying range of "
3255 "elements to initialize");
3258 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
3264 if (c_parser_next_token_is (parser, CPP_EQ))
3266 if (pedantic && !flag_isoc99)
3267 pedwarn ("ISO C90 forbids specifying subobject to initialize");
3268 c_parser_consume_token (parser);
3275 pedwarn ("obsolete use of designated initializer "
3281 init.value = error_mark_node;
3282 init.original_code = ERROR_MARK;
3283 c_parser_error (parser, "expected %<=%>");
3284 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
3285 process_init_element (init);
3291 c_parser_initval (parser, NULL);
3294 /* Parse a nested initializer; as c_parser_initializer but parses
3295 initializers within braced lists, after any designators have been
3296 applied. If AFTER is not NULL then it is an Objective-C message
3297 expression which is the primary-expression starting the
3301 c_parser_initval (c_parser *parser, struct c_expr *after)
3304 gcc_assert (!after || c_dialect_objc ());
3305 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE) && !after)
3306 init = c_parser_braced_init (parser, NULL_TREE, true);
3309 init = c_parser_expr_no_commas (parser, after);
3310 if (init.value != NULL_TREE
3311 && TREE_CODE (init.value) != STRING_CST
3312 && TREE_CODE (init.value) != COMPOUND_LITERAL_EXPR)
3313 init = default_function_array_conversion (init);
3315 process_init_element (init);
3318 /* Parse a compound statement (possibly a function body) (C90 6.6.2,
3322 { block-item-list[opt] }
3323 { label-declarations block-item-list }
3327 block-item-list block-item
3339 { label-declarations block-item-list }
3342 __extension__ nested-declaration
3343 nested-function-definition
3347 label-declarations label-declaration
3350 __label__ identifier-list ;
3352 Allowing the mixing of declarations and code is new in C99. The
3353 GNU syntax also permits (not shown above) labels at the end of
3354 compound statements, which yield an error. We don't allow labels
3355 on declarations; this might seem like a natural extension, but
3356 there would be a conflict between attributes on the label and
3357 prefix attributes on the declaration. ??? The syntax follows the
3358 old parser in requiring something after label declarations.
3359 Although they are erroneous if the labels declared aren't defined,
3360 is it useful for the syntax to be this way?
3372 c_parser_compound_statement (c_parser *parser)
3375 if (!c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
3376 return error_mark_node;
3377 stmt = c_begin_compound_stmt (true);
3378 c_parser_compound_statement_nostart (parser);
3379 return c_end_compound_stmt (stmt, true);
3382 /* Parse a compound statement except for the opening brace. This is
3383 used for parsing both compound statements and statement expressions
3384 (which follow different paths to handling the opening). */
3387 c_parser_compound_statement_nostart (c_parser *parser)
3389 bool last_stmt = false;
3390 bool last_label = false;
3391 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3393 c_parser_consume_token (parser);
3396 if (c_parser_next_token_is_keyword (parser, RID_LABEL))
3398 /* Read zero or more forward-declarations for labels that nested
3399 functions can jump to. */
3400 while (c_parser_next_token_is_keyword (parser, RID_LABEL))
3402 c_parser_consume_token (parser);
3403 /* Any identifiers, including those declared as type names,
3408 if (c_parser_next_token_is_not (parser, CPP_NAME))
3410 c_parser_error (parser, "expected identifier");
3414 = declare_label (c_parser_peek_token (parser)->value);
3415 C_DECLARED_LABEL_FLAG (label) = 1;
3416 add_stmt (build_stmt (DECL_EXPR, label));
3417 c_parser_consume_token (parser);
3418 if (c_parser_next_token_is (parser, CPP_COMMA))
3419 c_parser_consume_token (parser);
3423 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
3425 /* ??? Locating this diagnostic on the token after the
3426 declarations end follows the old parser, but it might be
3427 better to locate it where the declarations start instead. */
3429 pedwarn ("ISO C forbids label declarations");
3431 /* We must now have at least one statement, label or declaration. */
3432 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3434 c_parser_error (parser, "expected declaration or statement");
3435 c_parser_consume_token (parser);
3438 while (c_parser_next_token_is_not (parser, CPP_CLOSE_BRACE))
3440 location_t loc = c_parser_peek_token (parser)->location;
3441 if (c_parser_next_token_is_keyword (parser, RID_CASE)
3442 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
3443 || (c_parser_next_token_is (parser, CPP_NAME)
3444 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
3448 c_parser_label (parser);
3450 else if (!last_label
3451 && c_parser_next_token_starts_declspecs (parser))
3454 c_parser_declaration_or_fndef (parser, true, true, true, true);
3456 && ((pedantic && !flag_isoc99)
3457 || warn_declaration_after_statement))
3458 pedwarn_c90 ("%HISO C90 forbids mixed declarations and code",
3462 else if (!last_label
3463 && c_parser_next_token_is_keyword (parser, RID_EXTENSION))
3465 /* __extension__ can start a declaration, but is also an
3466 unary operator that can start an expression. Consume all
3467 but the last of a possible series of __extension__ to
3469 while (c_parser_peek_2nd_token (parser)->type == CPP_KEYWORD
3470 && (c_parser_peek_2nd_token (parser)->keyword
3472 c_parser_consume_token (parser);
3473 if (c_token_starts_declspecs (c_parser_peek_2nd_token (parser)))
3476 ext = disable_extension_diagnostics ();
3477 c_parser_consume_token (parser);
3479 c_parser_declaration_or_fndef (parser, true, true, true, true);
3480 /* Following the old parser, __extension__ does not
3481 disable this diagnostic. */
3482 restore_extension_diagnostics (ext);
3484 && ((pedantic && !flag_isoc99)
3485 || warn_declaration_after_statement))
3486 pedwarn_c90 ("%HISO C90 forbids mixed declarations and code",
3493 else if (c_parser_next_token_is (parser, CPP_PRAGMA))
3495 /* External pragmas, and some omp pragmas, are not associated
3496 with regular c code, and so are not to be considered statements
3497 syntactically. This ensures that the user doesn't put them
3498 places that would turn into syntax errors if the directive
3500 if (c_parser_pragma (parser, pragma_compound))
3501 last_label = false, last_stmt = true;
3503 else if (c_parser_next_token_is (parser, CPP_EOF))
3505 c_parser_error (parser, "expected declaration or statement");
3513 c_parser_statement_after_labels (parser);
3516 parser->error = false;
3519 error ("label at end of compound statement");
3520 c_parser_consume_token (parser);
3523 /* Parse a label (C90 6.6.1, C99 6.8.1).
3526 identifier : attributes[opt]
3527 case constant-expression :
3533 case constant-expression ... constant-expression :
3535 The use of attributes on labels is a GNU extension. The syntax in
3536 GNU C accepts any expressions without commas, non-constant
3537 expressions being rejected later. */
3540 c_parser_label (c_parser *parser)
3542 location_t loc1 = c_parser_peek_token (parser)->location;
3543 tree label = NULL_TREE;
3544 if (c_parser_next_token_is_keyword (parser, RID_CASE))
3547 c_parser_consume_token (parser);
3548 exp1 = c_parser_expr_no_commas (parser, NULL).value;
3549 if (c_parser_next_token_is (parser, CPP_COLON))
3551 c_parser_consume_token (parser);
3552 label = do_case (exp1, NULL_TREE);
3554 else if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
3556 c_parser_consume_token (parser);
3557 exp2 = c_parser_expr_no_commas (parser, NULL).value;
3558 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
3559 label = do_case (exp1, exp2);
3562 c_parser_error (parser, "expected %<:%> or %<...%>");
3564 else if (c_parser_next_token_is_keyword (parser, RID_DEFAULT))
3566 c_parser_consume_token (parser);
3567 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
3568 label = do_case (NULL_TREE, NULL_TREE);
3572 tree name = c_parser_peek_token (parser)->value;
3576 gcc_assert (c_parser_next_token_is (parser, CPP_NAME));
3577 c_parser_consume_token (parser);
3578 gcc_assert (c_parser_next_token_is (parser, CPP_COLON));
3579 loc2 = c_parser_peek_token (parser)->location;
3580 c_parser_consume_token (parser);
3581 attrs = c_parser_attributes (parser);
3582 tlab = define_label (loc2, name);
3585 decl_attributes (&tlab, attrs, 0);
3586 label = add_stmt (build_stmt (LABEL_EXPR, tlab));
3590 SET_EXPR_LOCATION (label, loc1);
3593 /* Parse a statement (C90 6.6, C99 6.8).
3598 expression-statement
3606 expression-statement:
3609 selection-statement:
3613 iteration-statement:
3622 return expression[opt] ;
3635 objc-throw-statement
3636 objc-try-catch-statement
3637 objc-synchronized-statement
3639 objc-throw-statement:
3653 parallel-for-construct
3654 parallel-sections-construct
3661 parallel-directive structured-block
3664 for-directive iteration-statement
3667 sections-directive section-scope
3670 single-directive structured-block
3672 parallel-for-construct:
3673 parallel-for-directive iteration-statement
3675 parallel-sections-construct:
3676 parallel-sections-directive section-scope
3679 master-directive structured-block
3682 critical-directive structured-block
3685 atomic-directive expression-statement
3688 ordered-directive structured-block */
3691 c_parser_statement (c_parser *parser)
3693 while (c_parser_next_token_is_keyword (parser, RID_CASE)
3694 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
3695 || (c_parser_next_token_is (parser, CPP_NAME)
3696 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
3697 c_parser_label (parser);
3698 c_parser_statement_after_labels (parser);
3701 /* Parse a statement, other than a labeled statement. */
3704 c_parser_statement_after_labels (c_parser *parser)
3706 location_t loc = c_parser_peek_token (parser)->location;
3707 tree stmt = NULL_TREE;
3708 switch (c_parser_peek_token (parser)->type)
3710 case CPP_OPEN_BRACE:
3711 add_stmt (c_parser_compound_statement (parser));
3714 switch (c_parser_peek_token (parser)->keyword)
3717 c_parser_if_statement (parser);
3720 c_parser_switch_statement (parser);
3723 c_parser_while_statement (parser);
3726 c_parser_do_statement (parser);
3729 c_parser_for_statement (parser);
3732 c_parser_consume_token (parser);
3733 if (c_parser_next_token_is (parser, CPP_NAME))
3735 stmt = c_finish_goto_label (c_parser_peek_token (parser)->value);
3736 c_parser_consume_token (parser);
3738 else if (c_parser_next_token_is (parser, CPP_MULT))
3740 c_parser_consume_token (parser);
3741 stmt = c_finish_goto_ptr (c_parser_expression (parser).value);
3744 c_parser_error (parser, "expected identifier or %<*%>");
3745 goto expect_semicolon;
3747 c_parser_consume_token (parser);
3748 stmt = c_finish_bc_stmt (&c_cont_label, false);
3749 goto expect_semicolon;
3751 c_parser_consume_token (parser);
3752 stmt = c_finish_bc_stmt (&c_break_label, true);
3753 goto expect_semicolon;
3755 c_parser_consume_token (parser);
3756 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
3758 stmt = c_finish_return (NULL_TREE);
3759 c_parser_consume_token (parser);
3763 stmt = c_finish_return (c_parser_expression_conv (parser).value);
3764 goto expect_semicolon;
3768 stmt = c_parser_asm_statement (parser);
3771 gcc_assert (c_dialect_objc ());
3772 c_parser_consume_token (parser);
3773 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
3775 stmt = objc_build_throw_stmt (NULL_TREE);
3776 c_parser_consume_token (parser);
3781 = objc_build_throw_stmt (c_parser_expression (parser).value);
3782 goto expect_semicolon;
3786 gcc_assert (c_dialect_objc ());
3787 c_parser_objc_try_catch_statement (parser);
3789 case RID_AT_SYNCHRONIZED:
3790 gcc_assert (c_dialect_objc ());
3791 c_parser_objc_synchronized_statement (parser);
3798 c_parser_consume_token (parser);
3800 case CPP_CLOSE_PAREN:
3801 case CPP_CLOSE_SQUARE:
3802 /* Avoid infinite loop in error recovery:
3803 c_parser_skip_until_found stops at a closing nesting
3804 delimiter without consuming it, but here we need to consume
3805 it to proceed further. */
3806 c_parser_error (parser, "expected statement");
3807 c_parser_consume_token (parser);
3810 c_parser_pragma (parser, pragma_stmt);
3814 if (c_parser_next_token_starts_declspecs (parser))
3816 error ("a label can only be part of a statement and a declaration is not a statement");
3817 c_parser_declaration_or_fndef (parser, /*fndef_ok*/ false,
3818 /*nested*/ true, /*empty_ok*/ false,
3819 /*start_attr_ok*/ true);
3822 stmt = c_finish_expr_stmt (c_parser_expression_conv (parser).value);
3824 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
3827 /* Two cases cannot and do not have line numbers associated: If stmt
3828 is degenerate, such as "2;", then stmt is an INTEGER_CST, which
3829 cannot hold line numbers. But that's OK because the statement
3830 will either be changed to a MODIFY_EXPR during gimplification of
3831 the statement expr, or discarded. If stmt was compound, but
3832 without new variables, we will have skipped the creation of a
3833 BIND and will have a bare STATEMENT_LIST. But that's OK because
3834 (recursively) all of the component statements should already have
3835 line numbers assigned. ??? Can we discard no-op statements
3837 if (stmt && CAN_HAVE_LOCATION_P (stmt))
3838 SET_EXPR_LOCATION (stmt, loc);
3841 /* Parse a parenthesized condition from an if, do or while statement.
3847 c_parser_paren_condition (c_parser *parser)
3851 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3852 return error_mark_node;
3853 loc = c_parser_peek_token (parser)->location;
3854 cond = c_objc_common_truthvalue_conversion
3855 (c_parser_expression_conv (parser).value);
3856 if (CAN_HAVE_LOCATION_P (cond))
3857 SET_EXPR_LOCATION (cond, loc);
3858 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
3862 /* Parse a statement which is a block in C99. */
3865 c_parser_c99_block_statement (c_parser *parser)
3867 tree block = c_begin_compound_stmt (flag_isoc99);
3868 c_parser_statement (parser);
3869 return c_end_compound_stmt (block, flag_isoc99);
3872 /* Parse the body of an if statement or the else half thereof. This
3873 is just parsing a statement but (a) it is a block in C99, (b) we
3874 track whether the body is an if statement for the sake of
3875 -Wparentheses warnings, (c) we handle an empty body specially for
3876 the sake of -Wempty-body warnings. */
3879 c_parser_if_body (c_parser *parser, bool *if_p)
3881 tree block = c_begin_compound_stmt (flag_isoc99);
3882 while (c_parser_next_token_is_keyword (parser, RID_CASE)
3883 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
3884 || (c_parser_next_token_is (parser, CPP_NAME)
3885 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
3886 c_parser_label (parser);
3887 *if_p = c_parser_next_token_is_keyword (parser, RID_IF);
3888 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
3889 add_stmt (build_empty_stmt ());
3890 c_parser_statement_after_labels (parser);
3891 return c_end_compound_stmt (block, flag_isoc99);
3894 /* Parse an if statement (C90 6.6.4, C99 6.8.4).
3897 if ( expression ) statement
3898 if ( expression ) statement else statement
3902 c_parser_if_statement (c_parser *parser)
3907 bool first_if = false, second_if = false;
3908 tree first_body, second_body;
3909 gcc_assert (c_parser_next_token_is_keyword (parser, RID_IF));
3910 c_parser_consume_token (parser);
3911 block = c_begin_compound_stmt (flag_isoc99);
3912 loc = c_parser_peek_token (parser)->location;
3913 cond = c_parser_paren_condition (parser);
3914 first_body = c_parser_if_body (parser, &first_if);
3915 if (c_parser_next_token_is_keyword (parser, RID_ELSE))
3917 c_parser_consume_token (parser);
3918 second_body = c_parser_if_body (parser, &second_if);
3921 second_body = NULL_TREE;
3922 c_finish_if_stmt (loc, cond, first_body, second_body, first_if);
3923 add_stmt (c_end_compound_stmt (block, flag_isoc99));
3926 /* Parse a switch statement (C90 6.6.4, C99 6.8.4).
3929 switch (expression) statement
3933 c_parser_switch_statement (c_parser *parser)
3935 tree block, expr, body, save_break;
3936 gcc_assert (c_parser_next_token_is_keyword (parser, RID_SWITCH));
3937 c_parser_consume_token (parser);
3938 block = c_begin_compound_stmt (flag_isoc99);
3939 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3941 expr = c_parser_expression (parser).value;
3942 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
3945 expr = error_mark_node;
3946 c_start_case (expr);
3947 save_break = c_break_label;
3948 c_break_label = NULL_TREE;
3949 body = c_parser_c99_block_statement (parser);
3950 c_finish_case (body);
3952 add_stmt (build1 (LABEL_EXPR, void_type_node, c_break_label));
3953 c_break_label = save_break;
3954 add_stmt (c_end_compound_stmt (block, flag_isoc99));
3957 /* Parse a while statement (C90 6.6.5, C99 6.8.5).
3960 while (expression) statement
3964 c_parser_while_statement (c_parser *parser)
3966 tree block, cond, body, save_break, save_cont;
3968 gcc_assert (c_parser_next_token_is_keyword (parser, RID_WHILE));
3969 c_parser_consume_token (parser);
3970 block = c_begin_compound_stmt (flag_isoc99);
3971 loc = c_parser_peek_token (parser)->location;
3972 cond = c_parser_paren_condition (parser);
3973 save_break = c_break_label;
3974 c_break_label = NULL_TREE;
3975 save_cont = c_cont_label;
3976 c_cont_label = NULL_TREE;
3977 body = c_parser_c99_block_statement (parser);
3978 c_finish_loop (loc, cond, NULL, body, c_break_label, c_cont_label, true);
3979 add_stmt (c_end_compound_stmt (block, flag_isoc99));
3980 c_break_label = save_break;
3981 c_cont_label = save_cont;
3984 /* Parse a do statement (C90 6.6.5, C99 6.8.5).
3987 do statement while ( expression ) ;
3991 c_parser_do_statement (c_parser *parser)
3993 tree block, cond, body, save_break, save_cont, new_break, new_cont;
3995 gcc_assert (c_parser_next_token_is_keyword (parser, RID_DO));
3996 c_parser_consume_token (parser);
3997 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
3998 warning (OPT_Wempty_body,
3999 "suggest braces around empty body in %<do%> statement");
4000 block = c_begin_compound_stmt (flag_isoc99);
4001 loc = c_parser_peek_token (parser)->location;
4002 save_break = c_break_label;
4003 c_break_label = NULL_TREE;
4004 save_cont = c_cont_label;
4005 c_cont_label = NULL_TREE;
4006 body = c_parser_c99_block_statement (parser);
4007 c_parser_require_keyword (parser, RID_WHILE, "expected %<while%>");
4008 new_break = c_break_label;
4009 c_break_label = save_break;
4010 new_cont = c_cont_label;
4011 c_cont_label = save_cont;
4012 cond = c_parser_paren_condition (parser);
4013 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
4014 c_parser_skip_to_end_of_block_or_statement (parser);
4015 c_finish_loop (loc, cond, NULL, body, new_break, new_cont, false);
4016 add_stmt (c_end_compound_stmt (block, flag_isoc99));
4019 /* Parse a for statement (C90 6.6.5, C99 6.8.5).
4022 for ( expression[opt] ; expression[opt] ; expression[opt] ) statement
4023 for ( nested-declaration expression[opt] ; expression[opt] ) statement
4025 The form with a declaration is new in C99.
4027 ??? In accordance with the old parser, the declaration may be a
4028 nested function, which is then rejected in check_for_loop_decls,
4029 but does it make any sense for this to be included in the grammar?
4030 Note in particular that the nested function does not include a
4031 trailing ';', whereas the "declaration" production includes one.
4032 Also, can we reject bad declarations earlier and cheaper than
4033 check_for_loop_decls? */
4036 c_parser_for_statement (c_parser *parser)
4038 tree block, cond, incr, save_break, save_cont, body;
4040 gcc_assert (c_parser_next_token_is_keyword (parser, RID_FOR));
4041 loc = c_parser_peek_token (parser)->location;
4042 c_parser_consume_token (parser);
4043 block = c_begin_compound_stmt (flag_isoc99);
4044 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4046 /* Parse the initialization declaration or expression. */
4047 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
4049 c_parser_consume_token (parser);
4050 c_finish_expr_stmt (NULL_TREE);
4052 else if (c_parser_next_token_starts_declspecs (parser))
4054 c_parser_declaration_or_fndef (parser, true, true, true, true);
4055 check_for_loop_decls ();
4057 else if (c_parser_next_token_is_keyword (parser, RID_EXTENSION))
4059 /* __extension__ can start a declaration, but is also an
4060 unary operator that can start an expression. Consume all
4061 but the last of a possible series of __extension__ to
4063 while (c_parser_peek_2nd_token (parser)->type == CPP_KEYWORD
4064 && (c_parser_peek_2nd_token (parser)->keyword
4066 c_parser_consume_token (parser);
4067 if (c_token_starts_declspecs (c_parser_peek_2nd_token (parser)))
4070 ext = disable_extension_diagnostics ();
4071 c_parser_consume_token (parser);
4072 c_parser_declaration_or_fndef (parser, true, true, true, true);
4073 restore_extension_diagnostics (ext);
4074 check_for_loop_decls ();
4082 c_finish_expr_stmt (c_parser_expression (parser).value);
4083 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
4085 /* Parse the loop condition. */
4086 loc = c_parser_peek_token (parser)->location;
4087 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
4089 c_parser_consume_token (parser);
4094 tree ocond = c_parser_expression_conv (parser).value;
4095 cond = c_objc_common_truthvalue_conversion (ocond);
4096 if (CAN_HAVE_LOCATION_P (cond))
4097 SET_EXPR_LOCATION (cond, loc);
4098 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
4100 /* Parse the increment expression. */
4101 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4102 incr = c_process_expr_stmt (NULL_TREE);
4104 incr = c_process_expr_stmt (c_parser_expression (parser).value);
4105 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
4109 cond = error_mark_node;
4110 incr = error_mark_node;
4112 save_break = c_break_label;
4113 c_break_label = NULL_TREE;
4114 save_cont = c_cont_label;
4115 c_cont_label = NULL_TREE;
4116 body = c_parser_c99_block_statement (parser);
4117 c_finish_loop (loc, cond, incr, body, c_break_label, c_cont_label, true);
4118 add_stmt (c_end_compound_stmt (block, flag_isoc99));
4119 c_break_label = save_break;
4120 c_cont_label = save_cont;
4123 /* Parse an asm statement, a GNU extension. This is a full-blown asm
4124 statement with inputs, outputs, clobbers, and volatile tag
4128 asm type-qualifier[opt] ( asm-argument ) ;
4132 asm-string-literal : asm-operands[opt]
4133 asm-string-literal : asm-operands[opt] : asm-operands[opt]
4134 asm-string-literal : asm-operands[opt] : asm-operands[opt] : asm-clobbers
4136 Qualifiers other than volatile are accepted in the syntax but
4140 c_parser_asm_statement (c_parser *parser)
4142 tree quals, str, outputs, inputs, clobbers, ret;
4144 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ASM));
4145 c_parser_consume_token (parser);
4146 if (c_parser_next_token_is_keyword (parser, RID_VOLATILE))
4148 quals = c_parser_peek_token (parser)->value;
4149 c_parser_consume_token (parser);
4151 else if (c_parser_next_token_is_keyword (parser, RID_CONST)
4152 || c_parser_next_token_is_keyword (parser, RID_RESTRICT))
4154 warning (0, "%E qualifier ignored on asm",
4155 c_parser_peek_token (parser)->value);
4157 c_parser_consume_token (parser);
4161 /* ??? Follow the C++ parser rather than using the
4162 lex_untranslated_string kludge. */
4163 parser->lex_untranslated_string = true;
4164 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4166 parser->lex_untranslated_string = false;
4169 str = c_parser_asm_string_literal (parser);
4170 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4173 outputs = NULL_TREE;
4175 clobbers = NULL_TREE;
4178 if (!c_parser_require (parser, CPP_COLON, "expected %<:%> or %<)%>"))
4180 parser->lex_untranslated_string = false;
4181 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
4185 /* Parse outputs. */
4186 if (c_parser_next_token_is (parser, CPP_COLON)
4187 || c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4188 outputs = NULL_TREE;
4190 outputs = c_parser_asm_operands (parser, false);
4191 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4194 clobbers = NULL_TREE;
4197 if (!c_parser_require (parser, CPP_COLON, "expected %<:%> or %<)%>"))
4199 parser->lex_untranslated_string = false;
4200 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
4204 if (c_parser_next_token_is (parser, CPP_COLON)
4205 || c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4208 inputs = c_parser_asm_operands (parser, true);
4209 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4211 clobbers = NULL_TREE;
4214 if (!c_parser_require (parser, CPP_COLON, "expected %<:%> or %<)%>"))
4216 parser->lex_untranslated_string = false;
4217 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
4220 /* Parse clobbers. */
4221 clobbers = c_parser_asm_clobbers (parser);
4223 parser->lex_untranslated_string = false;
4224 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
4226 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
4229 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
4230 c_parser_skip_to_end_of_block_or_statement (parser);
4231 ret = build_asm_stmt (quals, build_asm_expr (str, outputs, inputs,
4236 /* Parse asm operands, a GNU extension. If CONVERT_P (for inputs but
4237 not outputs), apply the default conversion of functions and arrays
4242 asm-operands , asm-operand
4245 asm-string-literal ( expression )
4246 [ identifier ] asm-string-literal ( expression )
4250 c_parser_asm_operands (c_parser *parser, bool convert_p)
4252 tree list = NULL_TREE;
4257 if (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
4259 c_parser_consume_token (parser);
4260 if (c_parser_next_token_is (parser, CPP_NAME))
4262 tree id = c_parser_peek_token (parser)->value;
4263 c_parser_consume_token (parser);
4264 name = build_string (IDENTIFIER_LENGTH (id),
4265 IDENTIFIER_POINTER (id));
4269 c_parser_error (parser, "expected identifier");
4270 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
4273 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
4278 str = c_parser_asm_string_literal (parser);
4279 if (str == NULL_TREE)
4281 parser->lex_untranslated_string = false;
4282 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4284 parser->lex_untranslated_string = true;
4287 expr = c_parser_expression (parser);
4289 expr = default_function_array_conversion (expr);
4290 parser->lex_untranslated_string = true;
4291 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
4293 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
4296 list = chainon (list, build_tree_list (build_tree_list (name, str),
4298 if (c_parser_next_token_is (parser, CPP_COMMA))
4299 c_parser_consume_token (parser);
4306 /* Parse asm clobbers, a GNU extension.
4310 asm-clobbers , asm-string-literal
4314 c_parser_asm_clobbers (c_parser *parser)
4316 tree list = NULL_TREE;
4319 tree str = c_parser_asm_string_literal (parser);
4321 list = tree_cons (NULL_TREE, str, list);
4324 if (c_parser_next_token_is (parser, CPP_COMMA))
4325 c_parser_consume_token (parser);
4332 /* Parse an expression other than a compound expression; that is, an
4333 assignment expression (C90 6.3.16, C99 6.5.16). If AFTER is not
4334 NULL then it is an Objective-C message expression which is the
4335 primary-expression starting the expression as an initializer.
4337 assignment-expression:
4338 conditional-expression
4339 unary-expression assignment-operator assignment-expression
4341 assignment-operator: one of
4342 = *= /= %= += -= <<= >>= &= ^= |=
4344 In GNU C we accept any conditional expression on the LHS and
4345 diagnose the invalid lvalue rather than producing a syntax
4348 static struct c_expr
4349 c_parser_expr_no_commas (c_parser *parser, struct c_expr *after)
4351 struct c_expr lhs, rhs, ret;
4352 enum tree_code code;
4353 gcc_assert (!after || c_dialect_objc ());
4354 lhs = c_parser_conditional_expression (parser, after);
4355 switch (c_parser_peek_token (parser)->type)
4364 code = TRUNC_DIV_EXPR;
4367 code = TRUNC_MOD_EXPR;
4382 code = BIT_AND_EXPR;
4385 code = BIT_XOR_EXPR;
4388 code = BIT_IOR_EXPR;
4393 c_parser_consume_token (parser);
4394 rhs = c_parser_expr_no_commas (parser, NULL);
4395 rhs = default_function_array_conversion (rhs);
4396 ret.value = build_modify_expr (lhs.value, code, rhs.value);
4397 if (code == NOP_EXPR)
4398 ret.original_code = MODIFY_EXPR;
4401 TREE_NO_WARNING (ret.value) = 1;
4402 ret.original_code = ERROR_MARK;
4407 /* Parse a conditional expression (C90 6.3.15, C99 6.5.15). If AFTER
4408 is not NULL then it is an Objective-C message expression which is
4409 the primary-expression starting the expression as an initializer.
4411 conditional-expression:
4412 logical-OR-expression
4413 logical-OR-expression ? expression : conditional-expression
4417 conditional-expression:
4418 logical-OR-expression ? : conditional-expression
4421 static struct c_expr
4422 c_parser_conditional_expression (c_parser *parser, struct c_expr *after)
4424 struct c_expr cond, exp1, exp2, ret;
4425 gcc_assert (!after || c_dialect_objc ());
4426 cond = c_parser_binary_expression (parser, after);
4427 if (c_parser_next_token_is_not (parser, CPP_QUERY))
4429 cond = default_function_array_conversion (cond);
4430 c_parser_consume_token (parser);
4431 if (c_parser_next_token_is (parser, CPP_COLON))
4434 pedwarn ("ISO C forbids omitting the middle term of a ?: expression");
4435 /* Make sure first operand is calculated only once. */
4436 exp1.value = save_expr (default_conversion (cond.value));
4437 cond.value = c_objc_common_truthvalue_conversion (exp1.value);
4438 skip_evaluation += cond.value == truthvalue_true_node;
4443 = c_objc_common_truthvalue_conversion
4444 (default_conversion (cond.value));
4445 skip_evaluation += cond.value == truthvalue_false_node;
4446 exp1 = c_parser_expression_conv (parser);
4447 skip_evaluation += ((cond.value == truthvalue_true_node)
4448 - (cond.value == truthvalue_false_node));
4450 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
4452 skip_evaluation -= cond.value == truthvalue_true_node;
4453 ret.value = error_mark_node;
4454 ret.original_code = ERROR_MARK;
4457 exp2 = c_parser_conditional_expression (parser, NULL);
4458 exp2 = default_function_array_conversion (exp2);
4459 skip_evaluation -= cond.value == truthvalue_true_node;
4460 ret.value = build_conditional_expr (cond.value, exp1.value, exp2.value);
4461 ret.original_code = ERROR_MARK;
4465 /* Parse a binary expression; that is, a logical-OR-expression (C90
4466 6.3.5-6.3.14, C99 6.5.5-6.5.14). If AFTER is not NULL then it is
4467 an Objective-C message expression which is the primary-expression
4468 starting the expression as an initializer.
4470 multiplicative-expression:
4472 multiplicative-expression * cast-expression
4473 multiplicative-expression / cast-expression
4474 multiplicative-expression % cast-expression
4476 additive-expression:
4477 multiplicative-expression
4478 additive-expression + multiplicative-expression
4479 additive-expression - multiplicative-expression
4483 shift-expression << additive-expression
4484 shift-expression >> additive-expression
4486 relational-expression:
4488 relational-expression < shift-expression
4489 relational-expression > shift-expression
4490 relational-expression <= shift-expression
4491 relational-expression >= shift-expression
4493 equality-expression:
4494 relational-expression
4495 equality-expression == relational-expression
4496 equality-expression != relational-expression
4500 AND-expression & equality-expression
4502 exclusive-OR-expression:
4504 exclusive-OR-expression ^ AND-expression
4506 inclusive-OR-expression:
4507 exclusive-OR-expression
4508 inclusive-OR-expression | exclusive-OR-expression
4510 logical-AND-expression:
4511 inclusive-OR-expression
4512 logical-AND-expression && inclusive-OR-expression
4514 logical-OR-expression:
4515 logical-AND-expression
4516 logical-OR-expression || logical-AND-expression
4519 static struct c_expr
4520 c_parser_binary_expression (c_parser *parser, struct c_expr *after)
4522 /* A binary expression is parsed using operator-precedence parsing,
4523 with the operands being cast expressions. All the binary
4524 operators are left-associative. Thus a binary expression is of
4527 E0 op1 E1 op2 E2 ...
4529 which we represent on a stack. On the stack, the precedence
4530 levels are strictly increasing. When a new operator is
4531 encountered of higher precedence than that at the top of the
4532 stack, it is pushed; its LHS is the top expression, and its RHS
4533 is everything parsed until it is popped. When a new operator is
4534 encountered with precedence less than or equal to that at the top
4535 of the stack, triples E[i-1] op[i] E[i] are popped and replaced
4536 by the result of the operation until the operator at the top of
4537 the stack has lower precedence than the new operator or there is
4538 only one element on the stack; then the top expression is the LHS
4539 of the new operator. In the case of logical AND and OR
4540 expressions, we also need to adjust skip_evaluation as
4541 appropriate when the operators are pushed and popped. */
4543 /* The precedence levels, where 0 is a dummy lowest level used for
4544 the bottom of the stack. */
4560 /* The expression at this stack level. */
4562 /* The precedence of the operator on its left, PREC_NONE at the
4563 bottom of the stack. */
4565 /* The operation on its left. */
4571 switch (stack[sp].op) \
4573 case TRUTH_ANDIF_EXPR: \
4574 skip_evaluation -= stack[sp - 1].expr.value == truthvalue_false_node; \
4576 case TRUTH_ORIF_EXPR: \
4577 skip_evaluation -= stack[sp - 1].expr.value == truthvalue_true_node; \
4582 stack[sp - 1].expr \
4583 = default_function_array_conversion (stack[sp - 1].expr); \
4585 = default_function_array_conversion (stack[sp].expr); \
4586 stack[sp - 1].expr = parser_build_binary_op (stack[sp].op, \
4587 stack[sp - 1].expr, \
4591 gcc_assert (!after || c_dialect_objc ());
4592 stack[0].expr = c_parser_cast_expression (parser, after);
4593 stack[0].prec = PREC_NONE;
4598 enum tree_code ocode;
4601 switch (c_parser_peek_token (parser)->type)
4609 ocode = TRUNC_DIV_EXPR;
4613 ocode = TRUNC_MOD_EXPR;
4625 ocode = LSHIFT_EXPR;
4629 ocode = RSHIFT_EXPR;
4643 case CPP_GREATER_EQ:
4656 oprec = PREC_BITAND;
4657 ocode = BIT_AND_EXPR;
4660 oprec = PREC_BITXOR;
4661 ocode = BIT_XOR_EXPR;
4665 ocode = BIT_IOR_EXPR;
4668 oprec = PREC_LOGAND;
4669 ocode = TRUTH_ANDIF_EXPR;
4673 ocode = TRUTH_ORIF_EXPR;
4676 /* Not a binary operator, so end of the binary
4680 c_parser_consume_token (parser);
4681 while (oprec <= stack[sp].prec)
4685 case TRUTH_ANDIF_EXPR:
4687 = default_function_array_conversion (stack[sp].expr);
4688 stack[sp].expr.value = c_objc_common_truthvalue_conversion
4689 (default_conversion (stack[sp].expr.value));
4690 skip_evaluation += stack[sp].expr.value == truthvalue_false_node;
4692 case TRUTH_ORIF_EXPR:
4694 = default_function_array_conversion (stack[sp].expr);
4695 stack[sp].expr.value = c_objc_common_truthvalue_conversion
4696 (default_conversion (stack[sp].expr.value));
4697 skip_evaluation += stack[sp].expr.value == truthvalue_true_node;
4703 stack[sp].expr = c_parser_cast_expression (parser, NULL);
4704 stack[sp].prec = oprec;
4705 stack[sp].op = ocode;
4710 return stack[0].expr;
4714 /* Parse a cast expression (C90 6.3.4, C99 6.5.4). If AFTER is not
4715 NULL then it is an Objective-C message expression which is the
4716 primary-expression starting the expression as an initializer.
4720 ( type-name ) unary-expression
4723 static struct c_expr
4724 c_parser_cast_expression (c_parser *parser, struct c_expr *after)
4726 gcc_assert (!after || c_dialect_objc ());
4728 return c_parser_postfix_expression_after_primary (parser, *after);
4729 /* If the expression begins with a parenthesized type name, it may
4730 be either a cast or a compound literal; we need to see whether
4731 the next character is '{' to tell the difference. If not, it is
4732 an unary expression. */
4733 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
4734 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
4736 struct c_type_name *type_name;
4739 c_parser_consume_token (parser);
4740 type_name = c_parser_type_name (parser);
4741 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
4742 if (type_name == NULL)
4744 ret.value = error_mark_node;
4745 ret.original_code = ERROR_MARK;
4749 /* Save casted types in the function's used types hash table. */
4750 used_types_insert (type_name->specs->type);
4752 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
4753 return c_parser_postfix_expression_after_paren_type (parser,
4755 expr = c_parser_cast_expression (parser, NULL);
4756 expr = default_function_array_conversion (expr);
4757 ret.value = c_cast_expr (type_name, expr.value);
4758 ret.original_code = ERROR_MARK;
4762 return c_parser_unary_expression (parser);
4765 /* Parse an unary expression (C90 6.3.3, C99 6.5.3).
4771 unary-operator cast-expression
4772 sizeof unary-expression
4773 sizeof ( type-name )
4775 unary-operator: one of
4781 __alignof__ unary-expression
4782 __alignof__ ( type-name )
4785 unary-operator: one of
4786 __extension__ __real__ __imag__
4788 In addition, the GNU syntax treats ++ and -- as unary operators, so
4789 they may be applied to cast expressions with errors for non-lvalues
4792 static struct c_expr
4793 c_parser_unary_expression (c_parser *parser)
4796 struct c_expr ret, op;
4797 switch (c_parser_peek_token (parser)->type)
4800 c_parser_consume_token (parser);
4801 op = c_parser_cast_expression (parser, NULL);
4802 op = default_function_array_conversion (op);
4803 return parser_build_unary_op (PREINCREMENT_EXPR, op);
4804 case CPP_MINUS_MINUS:
4805 c_parser_consume_token (parser);
4806 op = c_parser_cast_expression (parser, NULL);
4807 op = default_function_array_conversion (op);
4808 return parser_build_unary_op (PREDECREMENT_EXPR, op);
4810 c_parser_consume_token (parser);
4811 return parser_build_unary_op (ADDR_EXPR,
4812 c_parser_cast_expression (parser, NULL));
4814 c_parser_consume_token (parser);
4815 op = c_parser_cast_expression (parser, NULL);
4816 op = default_function_array_conversion (op);
4817 ret.value = build_indirect_ref (op.value, "unary *");
4818 ret.original_code = ERROR_MARK;
4821 c_parser_consume_token (parser);
4822 if (!c_dialect_objc () && !in_system_header)
4823 warning (OPT_Wtraditional,
4824 "traditional C rejects the unary plus operator");
4825 op = c_parser_cast_expression (parser, NULL);
4826 op = default_function_array_conversion (op);
4827 return parser_build_unary_op (CONVERT_EXPR, op);
4829 c_parser_consume_token (parser);
4830 op = c_parser_cast_expression (parser, NULL);
4831 op = default_function_array_conversion (op);
4832 return parser_build_unary_op (NEGATE_EXPR, op);
4834 c_parser_consume_token (parser);
4835 op = c_parser_cast_expression (parser, NULL);
4836 op = default_function_array_conversion (op);
4837 return parser_build_unary_op (BIT_NOT_EXPR, op);
4839 c_parser_consume_token (parser);
4840 op = c_parser_cast_expression (parser, NULL);
4841 op = default_function_array_conversion (op);
4842 return parser_build_unary_op (TRUTH_NOT_EXPR, op);
4844 /* Refer to the address of a label as a pointer. */
4845 c_parser_consume_token (parser);
4846 if (c_parser_next_token_is (parser, CPP_NAME))
4848 ret.value = finish_label_address_expr
4849 (c_parser_peek_token (parser)->value);
4850 c_parser_consume_token (parser);
4854 c_parser_error (parser, "expected identifier");
4855 ret.value = error_mark_node;
4857 ret.original_code = ERROR_MARK;
4860 switch (c_parser_peek_token (parser)->keyword)
4863 return c_parser_sizeof_expression (parser);
4865 return c_parser_alignof_expression (parser);
4867 c_parser_consume_token (parser);
4868 ext = disable_extension_diagnostics ();
4869 ret = c_parser_cast_expression (parser, NULL);
4870 restore_extension_diagnostics (ext);
4873 c_parser_consume_token (parser);
4874 op = c_parser_cast_expression (parser, NULL);
4875 op = default_function_array_conversion (op);
4876 return parser_build_unary_op (REALPART_EXPR, op);
4878 c_parser_consume_token (parser);
4879 op = c_parser_cast_expression (parser, NULL);
4880 op = default_function_array_conversion (op);
4881 return parser_build_unary_op (IMAGPART_EXPR, op);
4883 return c_parser_postfix_expression (parser);
4886 return c_parser_postfix_expression (parser);
4890 /* Parse a sizeof expression. */
4892 static struct c_expr
4893 c_parser_sizeof_expression (c_parser *parser)
4896 gcc_assert (c_parser_next_token_is_keyword (parser, RID_SIZEOF));
4897 c_parser_consume_token (parser);
4900 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
4901 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
4903 /* Either sizeof ( type-name ) or sizeof unary-expression
4904 starting with a compound literal. */
4905 struct c_type_name *type_name;
4906 c_parser_consume_token (parser);
4907 type_name = c_parser_type_name (parser);
4908 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
4909 if (type_name == NULL)
4914 ret.value = error_mark_node;
4915 ret.original_code = ERROR_MARK;
4918 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
4920 expr = c_parser_postfix_expression_after_paren_type (parser,
4924 /* sizeof ( type-name ). */
4927 if (type_name->declarator->kind == cdk_array
4928 && type_name->declarator->u.array.vla_unspec_p)
4931 error ("%<[*]%> not allowed in other than a declaration");
4933 return c_expr_sizeof_type (type_name);
4937 expr = c_parser_unary_expression (parser);
4941 if (TREE_CODE (expr.value) == COMPONENT_REF
4942 && DECL_C_BIT_FIELD (TREE_OPERAND (expr.value, 1)))
4943 error ("%<sizeof%> applied to a bit-field");
4944 return c_expr_sizeof_expr (expr);
4948 /* Parse an alignof expression. */
4950 static struct c_expr
4951 c_parser_alignof_expression (c_parser *parser)
4954 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ALIGNOF));
4955 c_parser_consume_token (parser);
4958 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
4959 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
4961 /* Either __alignof__ ( type-name ) or __alignof__
4962 unary-expression starting with a compound literal. */
4963 struct c_type_name *type_name;
4965 c_parser_consume_token (parser);
4966 type_name = c_parser_type_name (parser);
4967 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
4968 if (type_name == NULL)
4973 ret.value = error_mark_node;
4974 ret.original_code = ERROR_MARK;
4977 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
4979 expr = c_parser_postfix_expression_after_paren_type (parser,
4983 /* alignof ( type-name ). */
4986 ret.value = c_alignof (groktypename (type_name));
4987 ret.original_code = ERROR_MARK;
4993 expr = c_parser_unary_expression (parser);
4997 ret.value = c_alignof_expr (expr.value);
4998 ret.original_code = ERROR_MARK;
5003 /* Parse a postfix expression (C90 6.3.1-6.3.2, C99 6.5.1-6.5.2).
5007 postfix-expression [ expression ]
5008 postfix-expression ( argument-expression-list[opt] )
5009 postfix-expression . identifier
5010 postfix-expression -> identifier
5011 postfix-expression ++
5012 postfix-expression --
5013 ( type-name ) { initializer-list }
5014 ( type-name ) { initializer-list , }
5016 argument-expression-list:
5018 argument-expression-list , argument-expression
5030 (treated as a keyword in GNU C)
5033 ( compound-statement )
5034 __builtin_va_arg ( assignment-expression , type-name )
5035 __builtin_offsetof ( type-name , offsetof-member-designator )
5036 __builtin_choose_expr ( assignment-expression ,
5037 assignment-expression ,
5038 assignment-expression )
5039 __builtin_types_compatible_p ( type-name , type-name )
5041 offsetof-member-designator:
5043 offsetof-member-designator . identifier
5044 offsetof-member-designator [ expression ]
5049 [ objc-receiver objc-message-args ]
5050 @selector ( objc-selector-arg )
5051 @protocol ( identifier )
5052 @encode ( type-name )
5056 static struct c_expr
5057 c_parser_postfix_expression (c_parser *parser)
5059 struct c_expr expr, e1, e2, e3;
5060 struct c_type_name *t1, *t2;
5061 switch (c_parser_peek_token (parser)->type)
5066 expr.value = c_parser_peek_token (parser)->value;
5067 expr.original_code = ERROR_MARK;
5068 c_parser_consume_token (parser);
5072 expr.value = c_parser_peek_token (parser)->value;
5073 expr.original_code = STRING_CST;
5074 c_parser_consume_token (parser);
5076 case CPP_OBJC_STRING:
5077 gcc_assert (c_dialect_objc ());
5079 = objc_build_string_object (c_parser_peek_token (parser)->value);
5080 expr.original_code = ERROR_MARK;
5081 c_parser_consume_token (parser);
5084 if (c_parser_peek_token (parser)->id_kind != C_ID_ID)
5086 c_parser_error (parser, "expected expression");
5087 expr.value = error_mark_node;
5088 expr.original_code = ERROR_MARK;
5092 tree id = c_parser_peek_token (parser)->value;
5093 location_t loc = c_parser_peek_token (parser)->location;
5094 c_parser_consume_token (parser);
5095 expr.value = build_external_ref (id,
5096 (c_parser_peek_token (parser)->type
5097 == CPP_OPEN_PAREN), loc);
5098 expr.original_code = ERROR_MARK;
5101 case CPP_OPEN_PAREN:
5102 /* A parenthesized expression, statement expression or compound
5104 if (c_parser_peek_2nd_token (parser)->type == CPP_OPEN_BRACE)
5106 /* A statement expression. */
5108 c_parser_consume_token (parser);
5109 c_parser_consume_token (parser);
5110 if (cur_stmt_list == NULL)
5112 error ("braced-group within expression allowed "
5113 "only inside a function");
5114 parser->error = true;
5115 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
5116 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5117 expr.value = error_mark_node;
5118 expr.original_code = ERROR_MARK;
5121 stmt = c_begin_stmt_expr ();
5122 c_parser_compound_statement_nostart (parser);
5123 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5126 pedwarn ("ISO C forbids braced-groups within expressions");
5127 expr.value = c_finish_stmt_expr (stmt);
5128 expr.original_code = ERROR_MARK;
5130 else if (c_token_starts_typename (c_parser_peek_2nd_token (parser)))
5132 /* A compound literal. ??? Can we actually get here rather
5133 than going directly to
5134 c_parser_postfix_expression_after_paren_type from
5136 struct c_type_name *type_name;
5137 c_parser_consume_token (parser);
5138 type_name = c_parser_type_name (parser);
5139 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5141 if (type_name == NULL)
5143 expr.value = error_mark_node;
5144 expr.original_code = ERROR_MARK;
5147 expr = c_parser_postfix_expression_after_paren_type (parser,
5152 /* A parenthesized expression. */
5153 c_parser_consume_token (parser);
5154 expr = c_parser_expression (parser);
5155 if (TREE_CODE (expr.value) == MODIFY_EXPR)
5156 TREE_NO_WARNING (expr.value) = 1;
5157 expr.original_code = ERROR_MARK;
5158 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5163 switch (c_parser_peek_token (parser)->keyword)
5165 case RID_FUNCTION_NAME:
5166 case RID_PRETTY_FUNCTION_NAME:
5167 case RID_C99_FUNCTION_NAME:
5168 expr.value = fname_decl (c_parser_peek_token (parser)->keyword,
5169 c_parser_peek_token (parser)->value);
5170 expr.original_code = ERROR_MARK;
5171 c_parser_consume_token (parser);
5174 c_parser_consume_token (parser);
5175 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5177 expr.value = error_mark_node;
5178 expr.original_code = ERROR_MARK;
5181 e1 = c_parser_expr_no_commas (parser, NULL);
5182 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
5184 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5185 expr.value = error_mark_node;
5186 expr.original_code = ERROR_MARK;
5189 t1 = c_parser_type_name (parser);
5190 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5194 expr.value = error_mark_node;
5195 expr.original_code = ERROR_MARK;
5199 expr.value = build_va_arg (e1.value, groktypename (t1));
5200 expr.original_code = ERROR_MARK;
5204 c_parser_consume_token (parser);
5205 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5207 expr.value = error_mark_node;
5208 expr.original_code = ERROR_MARK;
5211 t1 = c_parser_type_name (parser);
5214 expr.value = error_mark_node;
5215 expr.original_code = ERROR_MARK;
5218 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
5220 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5221 expr.value = error_mark_node;
5222 expr.original_code = ERROR_MARK;
5226 tree type = groktypename (t1);
5228 if (type == error_mark_node)
5229 offsetof_ref = error_mark_node;
5231 offsetof_ref = build1 (INDIRECT_REF, type, null_pointer_node);
5232 /* Parse the second argument to __builtin_offsetof. We
5233 must have one identifier, and beyond that we want to
5234 accept sub structure and sub array references. */
5235 if (c_parser_next_token_is (parser, CPP_NAME))
5237 offsetof_ref = build_component_ref
5238 (offsetof_ref, c_parser_peek_token (parser)->value);
5239 c_parser_consume_token (parser);
5240 while (c_parser_next_token_is (parser, CPP_DOT)
5241 || c_parser_next_token_is (parser,
5244 if (c_parser_next_token_is (parser, CPP_DOT))
5246 c_parser_consume_token (parser);
5247 if (c_parser_next_token_is_not (parser,
5250 c_parser_error (parser, "expected identifier");
5253 offsetof_ref = build_component_ref
5255 c_parser_peek_token (parser)->value);
5256 c_parser_consume_token (parser);
5261 c_parser_consume_token (parser);
5262 idx = c_parser_expression (parser).value;
5263 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
5265 offsetof_ref = build_array_ref (offsetof_ref, idx);
5270 c_parser_error (parser, "expected identifier");
5271 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5273 expr.value = fold_offsetof (offsetof_ref, NULL_TREE);
5274 expr.original_code = ERROR_MARK;
5277 case RID_CHOOSE_EXPR:
5278 c_parser_consume_token (parser);
5279 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5281 expr.value = error_mark_node;
5282 expr.original_code = ERROR_MARK;
5285 e1 = c_parser_expr_no_commas (parser, NULL);
5286 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
5288 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5289 expr.value = error_mark_node;
5290 expr.original_code = ERROR_MARK;
5293 e2 = c_parser_expr_no_commas (parser, NULL);
5294 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
5296 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5297 expr.value = error_mark_node;
5298 expr.original_code = ERROR_MARK;
5301 e3 = c_parser_expr_no_commas (parser, NULL);
5302 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5307 c = fold (e1.value);
5308 if (TREE_CODE (c) != INTEGER_CST)
5309 error ("first argument to %<__builtin_choose_expr%> not"
5311 expr = integer_zerop (c) ? e3 : e2;
5314 case RID_TYPES_COMPATIBLE_P:
5315 c_parser_consume_token (parser);
5316 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5318 expr.value = error_mark_node;
5319 expr.original_code = ERROR_MARK;
5322 t1 = c_parser_type_name (parser);
5325 expr.value = error_mark_node;
5326 expr.original_code = ERROR_MARK;
5329 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
5331 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5332 expr.value = error_mark_node;
5333 expr.original_code = ERROR_MARK;
5336 t2 = c_parser_type_name (parser);
5339 expr.value = error_mark_node;
5340 expr.original_code = ERROR_MARK;
5343 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5348 e1 = TYPE_MAIN_VARIANT (groktypename (t1));
5349 e2 = TYPE_MAIN_VARIANT (groktypename (t2));
5351 expr.value = comptypes (e1, e2)
5352 ? build_int_cst (NULL_TREE, 1)
5353 : build_int_cst (NULL_TREE, 0);
5354 expr.original_code = ERROR_MARK;
5357 case RID_AT_SELECTOR:
5358 gcc_assert (c_dialect_objc ());
5359 c_parser_consume_token (parser);
5360 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5362 expr.value = error_mark_node;
5363 expr.original_code = ERROR_MARK;
5367 tree sel = c_parser_objc_selector_arg (parser);
5368 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5370 expr.value = objc_build_selector_expr (sel);
5371 expr.original_code = ERROR_MARK;
5374 case RID_AT_PROTOCOL:
5375 gcc_assert (c_dialect_objc ());
5376 c_parser_consume_token (parser);
5377 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5379 expr.value = error_mark_node;
5380 expr.original_code = ERROR_MARK;
5383 if (c_parser_next_token_is_not (parser, CPP_NAME))
5385 c_parser_error (parser, "expected identifier");
5386 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5387 expr.value = error_mark_node;
5388 expr.original_code = ERROR_MARK;
5392 tree id = c_parser_peek_token (parser)->value;
5393 c_parser_consume_token (parser);
5394 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5396 expr.value = objc_build_protocol_expr (id);
5397 expr.original_code = ERROR_MARK;
5401 /* Extension to support C-structures in the archiver. */
5402 gcc_assert (c_dialect_objc ());
5403 c_parser_consume_token (parser);
5404 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5406 expr.value = error_mark_node;
5407 expr.original_code = ERROR_MARK;
5410 t1 = c_parser_type_name (parser);
5413 expr.value = error_mark_node;
5414 expr.original_code = ERROR_MARK;
5415 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5418 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5421 tree type = groktypename (t1);
5422 expr.value = objc_build_encode_expr (type);
5423 expr.original_code = ERROR_MARK;
5427 c_parser_error (parser, "expected expression");
5428 expr.value = error_mark_node;
5429 expr.original_code = ERROR_MARK;
5433 case CPP_OPEN_SQUARE:
5434 if (c_dialect_objc ())
5436 tree receiver, args;
5437 c_parser_consume_token (parser);
5438 receiver = c_parser_objc_receiver (parser);
5439 args = c_parser_objc_message_args (parser);
5440 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
5442 expr.value = objc_build_message_expr (build_tree_list (receiver,
5444 expr.original_code = ERROR_MARK;
5447 /* Else fall through to report error. */
5449 c_parser_error (parser, "expected expression");
5450 expr.value = error_mark_node;
5451 expr.original_code = ERROR_MARK;
5454 return c_parser_postfix_expression_after_primary (parser, expr);
5457 /* Parse a postfix expression after a parenthesized type name: the
5458 brace-enclosed initializer of a compound literal, possibly followed
5459 by some postfix operators. This is separate because it is not
5460 possible to tell until after the type name whether a cast
5461 expression has a cast or a compound literal, or whether the operand
5462 of sizeof is a parenthesized type name or starts with a compound
5465 static struct c_expr
5466 c_parser_postfix_expression_after_paren_type (c_parser *parser,
5467 struct c_type_name *type_name)
5472 start_init (NULL_TREE, NULL, 0);
5473 type = groktypename (type_name);
5474 if (type != error_mark_node && C_TYPE_VARIABLE_SIZE (type))
5476 error ("compound literal has variable size");
5477 type = error_mark_node;
5479 init = c_parser_braced_init (parser, type, false);
5481 maybe_warn_string_init (type, init);
5483 if (pedantic && !flag_isoc99)
5484 pedwarn ("ISO C90 forbids compound literals");
5485 expr.value = build_compound_literal (type, init.value);
5486 expr.original_code = ERROR_MARK;
5487 return c_parser_postfix_expression_after_primary (parser, expr);
5490 /* Parse a postfix expression after the initial primary or compound
5491 literal; that is, parse a series of postfix operators. */
5493 static struct c_expr
5494 c_parser_postfix_expression_after_primary (c_parser *parser,
5497 tree ident, idx, exprlist;
5500 switch (c_parser_peek_token (parser)->type)
5502 case CPP_OPEN_SQUARE:
5503 /* Array reference. */
5504 c_parser_consume_token (parser);
5505 idx = c_parser_expression (parser).value;
5506 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
5508 expr.value = build_array_ref (expr.value, idx);
5509 expr.original_code = ERROR_MARK;
5511 case CPP_OPEN_PAREN:
5512 /* Function call. */
5513 c_parser_consume_token (parser);
5514 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
5515 exprlist = NULL_TREE;
5517 exprlist = c_parser_expr_list (parser, true);
5518 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5520 expr.value = build_function_call (expr.value, exprlist);
5521 expr.original_code = ERROR_MARK;
5524 /* Structure element reference. */
5525 c_parser_consume_token (parser);
5526 expr = default_function_array_conversion (expr);
5527 if (c_parser_next_token_is (parser, CPP_NAME))
5528 ident = c_parser_peek_token (parser)->value;
5531 c_parser_error (parser, "expected identifier");
5532 expr.value = error_mark_node;
5533 expr.original_code = ERROR_MARK;
5536 c_parser_consume_token (parser);
5537 expr.value = build_component_ref (expr.value, ident);
5538 expr.original_code = ERROR_MARK;
5541 /* Structure element reference. */
5542 c_parser_consume_token (parser);
5543 expr = default_function_array_conversion (expr);
5544 if (c_parser_next_token_is (parser, CPP_NAME))
5545 ident = c_parser_peek_token (parser)->value;
5548 c_parser_error (parser, "expected identifier");
5549 expr.value = error_mark_node;
5550 expr.original_code = ERROR_MARK;
5553 c_parser_consume_token (parser);
5554 expr.value = build_component_ref (build_indirect_ref (expr.value,
5556 expr.original_code = ERROR_MARK;
5559 /* Postincrement. */
5560 c_parser_consume_token (parser);
5561 expr = default_function_array_conversion (expr);
5562 expr.value = build_unary_op (POSTINCREMENT_EXPR, expr.value, 0);
5563 expr.original_code = ERROR_MARK;
5565 case CPP_MINUS_MINUS:
5566 /* Postdecrement. */
5567 c_parser_consume_token (parser);
5568 expr = default_function_array_conversion (expr);
5569 expr.value = build_unary_op (POSTDECREMENT_EXPR, expr.value, 0);
5570 expr.original_code = ERROR_MARK;
5578 /* Parse an expression (C90 6.3.17, C99 6.5.17).
5581 assignment-expression
5582 expression , assignment-expression
5585 static struct c_expr
5586 c_parser_expression (c_parser *parser)
5589 expr = c_parser_expr_no_commas (parser, NULL);
5590 while (c_parser_next_token_is (parser, CPP_COMMA))
5593 c_parser_consume_token (parser);
5594 next = c_parser_expr_no_commas (parser, NULL);
5595 next = default_function_array_conversion (next);
5596 expr.value = build_compound_expr (expr.value, next.value);
5597 expr.original_code = COMPOUND_EXPR;
5602 /* Parse an expression and convert functions or arrays to
5605 static struct c_expr
5606 c_parser_expression_conv (c_parser *parser)
5609 expr = c_parser_expression (parser);
5610 expr = default_function_array_conversion (expr);
5614 /* Parse a non-empty list of expressions. If CONVERT_P, convert
5615 functions and arrays to pointers.
5618 assignment-expression
5619 nonempty-expr-list , assignment-expression
5623 c_parser_expr_list (c_parser *parser, bool convert_p)
5627 expr = c_parser_expr_no_commas (parser, NULL);
5629 expr = default_function_array_conversion (expr);
5630 ret = cur = build_tree_list (NULL_TREE, expr.value);
5631 while (c_parser_next_token_is (parser, CPP_COMMA))
5633 c_parser_consume_token (parser);
5634 expr = c_parser_expr_no_commas (parser, NULL);
5636 expr = default_function_array_conversion (expr);
5637 cur = TREE_CHAIN (cur) = build_tree_list (NULL_TREE, expr.value);
5643 /* Parse Objective-C-specific constructs. */
5645 /* Parse an objc-class-definition.
5647 objc-class-definition:
5648 @interface identifier objc-superclass[opt] objc-protocol-refs[opt]
5649 objc-class-instance-variables[opt] objc-methodprotolist @end
5650 @implementation identifier objc-superclass[opt]
5651 objc-class-instance-variables[opt]
5652 @interface identifier ( identifier ) objc-protocol-refs[opt]
5653 objc-methodprotolist @end
5654 @implementation identifier ( identifier )
5659 "@interface identifier (" must start "@interface identifier (
5660 identifier ) ...": objc-methodprotolist in the first production may
5661 not start with a parenthesized identifier as a declarator of a data
5662 definition with no declaration specifiers if the objc-superclass,
5663 objc-protocol-refs and objc-class-instance-variables are omitted. */
5666 c_parser_objc_class_definition (c_parser *parser)
5671 if (c_parser_next_token_is_keyword (parser, RID_AT_INTERFACE))
5673 else if (c_parser_next_token_is_keyword (parser, RID_AT_IMPLEMENTATION))
5677 c_parser_consume_token (parser);
5678 if (c_parser_next_token_is_not (parser, CPP_NAME))
5680 c_parser_error (parser, "expected identifier");
5683 id1 = c_parser_peek_token (parser)->value;
5684 c_parser_consume_token (parser);
5685 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
5688 tree proto = NULL_TREE;
5689 c_parser_consume_token (parser);
5690 if (c_parser_next_token_is_not (parser, CPP_NAME))
5692 c_parser_error (parser, "expected identifier");
5693 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5696 id2 = c_parser_peek_token (parser)->value;
5697 c_parser_consume_token (parser);
5698 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
5701 objc_start_category_implementation (id1, id2);
5704 if (c_parser_next_token_is (parser, CPP_LESS))
5705 proto = c_parser_objc_protocol_refs (parser);
5706 objc_start_category_interface (id1, id2, proto);
5707 c_parser_objc_methodprotolist (parser);
5708 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
5709 objc_finish_interface ();
5712 if (c_parser_next_token_is (parser, CPP_COLON))
5714 c_parser_consume_token (parser);
5715 if (c_parser_next_token_is_not (parser, CPP_NAME))
5717 c_parser_error (parser, "expected identifier");
5720 superclass = c_parser_peek_token (parser)->value;
5721 c_parser_consume_token (parser);
5724 superclass = NULL_TREE;
5727 tree proto = NULL_TREE;
5728 if (c_parser_next_token_is (parser, CPP_LESS))
5729 proto = c_parser_objc_protocol_refs (parser);
5730 objc_start_class_interface (id1, superclass, proto);
5733 objc_start_class_implementation (id1, superclass);
5734 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
5735 c_parser_objc_class_instance_variables (parser);
5738 objc_continue_interface ();
5739 c_parser_objc_methodprotolist (parser);
5740 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
5741 objc_finish_interface ();
5745 objc_continue_implementation ();
5750 /* Parse objc-class-instance-variables.
5752 objc-class-instance-variables:
5753 { objc-instance-variable-decl-list[opt] }
5755 objc-instance-variable-decl-list:
5756 objc-visibility-spec
5757 objc-instance-variable-decl ;
5759 objc-instance-variable-decl-list objc-visibility-spec
5760 objc-instance-variable-decl-list objc-instance-variable-decl ;
5761 objc-instance-variable-decl-list ;
5763 objc-visibility-spec:
5768 objc-instance-variable-decl:
5773 c_parser_objc_class_instance_variables (c_parser *parser)
5775 gcc_assert (c_parser_next_token_is (parser, CPP_OPEN_BRACE));
5776 c_parser_consume_token (parser);
5777 while (c_parser_next_token_is_not (parser, CPP_EOF))
5780 /* Parse any stray semicolon. */
5781 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5784 pedwarn ("extra semicolon in struct or union specified");
5785 c_parser_consume_token (parser);
5788 /* Stop if at the end of the instance variables. */
5789 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
5791 c_parser_consume_token (parser);
5794 /* Parse any objc-visibility-spec. */
5795 if (c_parser_next_token_is_keyword (parser, RID_AT_PRIVATE))
5797 c_parser_consume_token (parser);
5798 objc_set_visibility (2);
5801 else if (c_parser_next_token_is_keyword (parser, RID_AT_PROTECTED))
5803 c_parser_consume_token (parser);
5804 objc_set_visibility (0);
5807 else if (c_parser_next_token_is_keyword (parser, RID_AT_PUBLIC))
5809 c_parser_consume_token (parser);
5810 objc_set_visibility (1);
5813 else if (c_parser_next_token_is (parser, CPP_PRAGMA))
5815 c_parser_pragma (parser, pragma_external);
5819 /* Parse some comma-separated declarations. */
5820 decls = c_parser_struct_declaration (parser);
5822 /* Comma-separated instance variables are chained together in
5823 reverse order; add them one by one. */
5824 tree ivar = nreverse (decls);
5825 for (; ivar; ivar = TREE_CHAIN (ivar))
5826 objc_add_instance_variable (copy_node (ivar));
5828 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
5832 /* Parse an objc-class-declaration.
5834 objc-class-declaration:
5835 @class identifier-list ;
5839 c_parser_objc_class_declaration (c_parser *parser)
5841 tree list = NULL_TREE;
5842 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_CLASS));
5843 c_parser_consume_token (parser);
5844 /* Any identifiers, including those declared as type names, are OK
5849 if (c_parser_next_token_is_not (parser, CPP_NAME))
5851 c_parser_error (parser, "expected identifier");
5854 id = c_parser_peek_token (parser)->value;
5855 list = chainon (list, build_tree_list (NULL_TREE, id));
5856 c_parser_consume_token (parser);
5857 if (c_parser_next_token_is (parser, CPP_COMMA))
5858 c_parser_consume_token (parser);
5862 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
5863 objc_declare_class (list);
5866 /* Parse an objc-alias-declaration.
5868 objc-alias-declaration:
5869 @compatibility_alias identifier identifier ;
5873 c_parser_objc_alias_declaration (c_parser *parser)
5876 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_ALIAS));
5877 c_parser_consume_token (parser);
5878 if (c_parser_next_token_is_not (parser, CPP_NAME))
5880 c_parser_error (parser, "expected identifier");
5881 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
5884 id1 = c_parser_peek_token (parser)->value;
5885 c_parser_consume_token (parser);
5886 if (c_parser_next_token_is_not (parser, CPP_NAME))
5888 c_parser_error (parser, "expected identifier");
5889 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
5892 id2 = c_parser_peek_token (parser)->value;
5893 c_parser_consume_token (parser);
5894 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
5895 objc_declare_alias (id1, id2);
5898 /* Parse an objc-protocol-definition.
5900 objc-protocol-definition:
5901 @protocol identifier objc-protocol-refs[opt] objc-methodprotolist @end
5902 @protocol identifier-list ;
5904 "@protocol identifier ;" should be resolved as "@protocol
5905 identifier-list ;": objc-methodprotolist may not start with a
5906 semicolon in the first alternative if objc-protocol-refs are
5910 c_parser_objc_protocol_definition (c_parser *parser)
5912 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_PROTOCOL));
5913 c_parser_consume_token (parser);
5914 if (c_parser_next_token_is_not (parser, CPP_NAME))
5916 c_parser_error (parser, "expected identifier");
5919 if (c_parser_peek_2nd_token (parser)->type == CPP_COMMA
5920 || c_parser_peek_2nd_token (parser)->type == CPP_SEMICOLON)
5922 tree list = NULL_TREE;
5923 /* Any identifiers, including those declared as type names, are
5928 if (c_parser_next_token_is_not (parser, CPP_NAME))
5930 c_parser_error (parser, "expected identifier");
5933 id = c_parser_peek_token (parser)->value;
5934 list = chainon (list, build_tree_list (NULL_TREE, id));
5935 c_parser_consume_token (parser);
5936 if (c_parser_next_token_is (parser, CPP_COMMA))
5937 c_parser_consume_token (parser);
5941 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
5942 objc_declare_protocols (list);
5946 tree id = c_parser_peek_token (parser)->value;
5947 tree proto = NULL_TREE;
5948 c_parser_consume_token (parser);
5949 if (c_parser_next_token_is (parser, CPP_LESS))
5950 proto = c_parser_objc_protocol_refs (parser);
5951 parser->objc_pq_context = true;
5952 objc_start_protocol (id, proto);
5953 c_parser_objc_methodprotolist (parser);
5954 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
5955 parser->objc_pq_context = false;
5956 objc_finish_interface ();
5960 /* Parse an objc-method-type.
5967 static enum tree_code
5968 c_parser_objc_method_type (c_parser *parser)
5970 switch (c_parser_peek_token (parser)->type)
5973 c_parser_consume_token (parser);
5976 c_parser_consume_token (parser);
5983 /* Parse an objc-method-definition.
5985 objc-method-definition:
5986 objc-method-type objc-method-decl ;[opt] compound-statement
5990 c_parser_objc_method_definition (c_parser *parser)
5992 enum tree_code type = c_parser_objc_method_type (parser);
5994 objc_set_method_type (type);
5995 parser->objc_pq_context = true;
5996 decl = c_parser_objc_method_decl (parser);
5997 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5999 c_parser_consume_token (parser);
6001 pedwarn ("extra semicolon in method definition specified");
6003 if (!c_parser_next_token_is (parser, CPP_OPEN_BRACE))
6005 c_parser_error (parser, "expected %<{%>");
6008 parser->objc_pq_context = false;
6009 objc_start_method_definition (decl);
6010 add_stmt (c_parser_compound_statement (parser));
6011 objc_finish_method_definition (current_function_decl);
6014 /* Parse an objc-methodprotolist.
6016 objc-methodprotolist:
6018 objc-methodprotolist objc-methodproto
6019 objc-methodprotolist declaration
6020 objc-methodprotolist ;
6022 The declaration is a data definition, which may be missing
6023 declaration specifiers under the same rules and diagnostics as
6024 other data definitions outside functions, and the stray semicolon
6025 is diagnosed the same way as a stray semicolon outside a
6029 c_parser_objc_methodprotolist (c_parser *parser)
6033 /* The list is terminated by @end. */
6034 switch (c_parser_peek_token (parser)->type)
6038 pedwarn ("ISO C does not allow extra %<;%> outside of a function");
6039 c_parser_consume_token (parser);
6043 c_parser_objc_methodproto (parser);
6046 c_parser_pragma (parser, pragma_external);
6051 if (c_parser_next_token_is_keyword (parser, RID_AT_END))
6053 c_parser_declaration_or_fndef (parser, false, true, false, true);
6059 /* Parse an objc-methodproto.
6062 objc-method-type objc-method-decl ;
6066 c_parser_objc_methodproto (c_parser *parser)
6068 enum tree_code type = c_parser_objc_method_type (parser);
6070 objc_set_method_type (type);
6071 /* Remember protocol qualifiers in prototypes. */
6072 parser->objc_pq_context = true;
6073 decl = c_parser_objc_method_decl (parser);
6074 /* Forget protocol qualifiers here. */
6075 parser->objc_pq_context = false;
6076 objc_add_method_declaration (decl);
6077 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
6080 /* Parse an objc-method-decl.
6083 ( objc-type-name ) objc-selector
6085 ( objc-type-name ) objc-keyword-selector objc-optparmlist
6086 objc-keyword-selector objc-optparmlist
6088 objc-keyword-selector:
6090 objc-keyword-selector objc-keyword-decl
6093 objc-selector : ( objc-type-name ) identifier
6094 objc-selector : identifier
6095 : ( objc-type-name ) identifier
6099 objc-optparms objc-optellipsis
6103 objc-opt-parms , parameter-declaration
6111 c_parser_objc_method_decl (c_parser *parser)
6113 tree type = NULL_TREE;
6115 tree parms = NULL_TREE;
6116 bool ellipsis = false;
6118 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
6120 c_parser_consume_token (parser);
6121 type = c_parser_objc_type_name (parser);
6122 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
6124 sel = c_parser_objc_selector (parser);
6125 /* If there is no selector, or a colon follows, we have an
6126 objc-keyword-selector. If there is a selector, and a colon does
6127 not follow, that selector ends the objc-method-decl. */
6128 if (!sel || c_parser_next_token_is (parser, CPP_COLON))
6131 tree list = NULL_TREE;
6134 tree atype = NULL_TREE, id, keyworddecl;
6135 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
6137 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
6139 c_parser_consume_token (parser);
6140 atype = c_parser_objc_type_name (parser);
6141 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
6144 if (c_parser_next_token_is_not (parser, CPP_NAME))
6146 c_parser_error (parser, "expected identifier");
6147 return error_mark_node;
6149 id = c_parser_peek_token (parser)->value;
6150 c_parser_consume_token (parser);
6151 keyworddecl = objc_build_keyword_decl (tsel, atype, id);
6152 list = chainon (list, keyworddecl);
6153 tsel = c_parser_objc_selector (parser);
6154 if (!tsel && c_parser_next_token_is_not (parser, CPP_COLON))
6157 /* Parse the optional parameter list. Optional Objective-C
6158 method parameters follow the C syntax, and may include '...'
6159 to denote a variable number of arguments. */
6160 parms = make_node (TREE_LIST);
6161 while (c_parser_next_token_is (parser, CPP_COMMA))
6163 struct c_parm *parm;
6164 c_parser_consume_token (parser);
6165 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
6168 c_parser_consume_token (parser);
6171 parm = c_parser_parameter_declaration (parser, NULL_TREE);
6174 parms = chainon (parms,
6175 build_tree_list (NULL_TREE, grokparm (parm)));
6179 return objc_build_method_signature (type, sel, parms, ellipsis);
6182 /* Parse an objc-type-name.
6185 objc-type-qualifiers[opt] type-name
6186 objc-type-qualifiers[opt]
6188 objc-type-qualifiers:
6190 objc-type-qualifiers objc-type-qualifier
6192 objc-type-qualifier: one of
6193 in out inout bycopy byref oneway
6197 c_parser_objc_type_name (c_parser *parser)
6199 tree quals = NULL_TREE;
6200 struct c_type_name *typename = NULL;
6201 tree type = NULL_TREE;
6204 c_token *token = c_parser_peek_token (parser);
6205 if (token->type == CPP_KEYWORD
6206 && (token->keyword == RID_IN
6207 || token->keyword == RID_OUT
6208 || token->keyword == RID_INOUT
6209 || token->keyword == RID_BYCOPY
6210 || token->keyword == RID_BYREF
6211 || token->keyword == RID_ONEWAY))
6213 quals = chainon (quals, build_tree_list (NULL_TREE, token->value));
6214 c_parser_consume_token (parser);
6219 if (c_parser_next_token_starts_typename (parser))
6220 typename = c_parser_type_name (parser);
6222 type = groktypename (typename);
6223 return build_tree_list (quals, type);
6226 /* Parse objc-protocol-refs.
6233 c_parser_objc_protocol_refs (c_parser *parser)
6235 tree list = NULL_TREE;
6236 gcc_assert (c_parser_next_token_is (parser, CPP_LESS));
6237 c_parser_consume_token (parser);
6238 /* Any identifiers, including those declared as type names, are OK
6243 if (c_parser_next_token_is_not (parser, CPP_NAME))
6245 c_parser_error (parser, "expected identifier");
6248 id = c_parser_peek_token (parser)->value;
6249 list = chainon (list, build_tree_list (NULL_TREE, id));
6250 c_parser_consume_token (parser);
6251 if (c_parser_next_token_is (parser, CPP_COMMA))
6252 c_parser_consume_token (parser);
6256 c_parser_require (parser, CPP_GREATER, "expected %<>%>");
6260 /* Parse an objc-try-catch-statement.
6262 objc-try-catch-statement:
6263 @try compound-statement objc-catch-list[opt]
6264 @try compound-statement objc-catch-list[opt] @finally compound-statement
6267 @catch ( parameter-declaration ) compound-statement
6268 objc-catch-list @catch ( parameter-declaration ) compound-statement
6272 c_parser_objc_try_catch_statement (c_parser *parser)
6276 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_TRY));
6277 c_parser_consume_token (parser);
6278 loc = c_parser_peek_token (parser)->location;
6279 stmt = c_parser_compound_statement (parser);
6280 objc_begin_try_stmt (loc, stmt);
6281 while (c_parser_next_token_is_keyword (parser, RID_AT_CATCH))
6283 struct c_parm *parm;
6284 c_parser_consume_token (parser);
6285 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6287 parm = c_parser_parameter_declaration (parser, NULL_TREE);
6290 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6293 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
6294 objc_begin_catch_clause (grokparm (parm));
6295 if (c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
6296 c_parser_compound_statement_nostart (parser);
6297 objc_finish_catch_clause ();
6299 if (c_parser_next_token_is_keyword (parser, RID_AT_FINALLY))
6303 c_parser_consume_token (parser);
6304 finloc = c_parser_peek_token (parser)->location;
6305 finstmt = c_parser_compound_statement (parser);
6306 objc_build_finally_clause (finloc, finstmt);
6308 objc_finish_try_stmt ();
6311 /* Parse an objc-synchronized-statement.
6313 objc-synchronized-statement:
6314 @synchronized ( expression ) compound-statement
6318 c_parser_objc_synchronized_statement (c_parser *parser)
6322 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_SYNCHRONIZED));
6323 c_parser_consume_token (parser);
6324 loc = c_parser_peek_token (parser)->location;
6325 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6327 expr = c_parser_expression (parser).value;
6328 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
6331 expr = error_mark_node;
6332 stmt = c_parser_compound_statement (parser);
6333 objc_build_synchronized (loc, expr, stmt);
6336 /* Parse an objc-selector; return NULL_TREE without an error if the
6337 next token is not an objc-selector.
6342 enum struct union if else while do for switch case default
6343 break continue return goto asm sizeof typeof __alignof
6344 unsigned long const short volatile signed restrict _Complex
6345 in out inout bycopy byref oneway int char float double void _Bool
6347 ??? Why this selection of keywords but not, for example, storage
6348 class specifiers? */
6351 c_parser_objc_selector (c_parser *parser)
6353 c_token *token = c_parser_peek_token (parser);
6354 tree value = token->value;
6355 if (token->type == CPP_NAME)
6357 c_parser_consume_token (parser);
6360 if (token->type != CPP_KEYWORD)
6362 switch (token->keyword)
6403 c_parser_consume_token (parser);
6410 /* Parse an objc-selector-arg.
6414 objc-keywordname-list
6416 objc-keywordname-list:
6418 objc-keywordname-list objc-keywordname
6426 c_parser_objc_selector_arg (c_parser *parser)
6428 tree sel = c_parser_objc_selector (parser);
6429 tree list = NULL_TREE;
6430 if (sel && c_parser_next_token_is_not (parser, CPP_COLON))
6434 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
6436 list = chainon (list, build_tree_list (sel, NULL_TREE));
6437 sel = c_parser_objc_selector (parser);
6438 if (!sel && c_parser_next_token_is_not (parser, CPP_COLON))
6444 /* Parse an objc-receiver.
6453 c_parser_objc_receiver (c_parser *parser)
6455 if (c_parser_peek_token (parser)->type == CPP_NAME
6456 && (c_parser_peek_token (parser)->id_kind == C_ID_TYPENAME
6457 || c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME))
6459 tree id = c_parser_peek_token (parser)->value;
6460 c_parser_consume_token (parser);
6461 return objc_get_class_reference (id);
6463 return c_parser_expression (parser).value;
6466 /* Parse objc-message-args.
6470 objc-keywordarg-list
6472 objc-keywordarg-list:
6474 objc-keywordarg-list objc-keywordarg
6477 objc-selector : objc-keywordexpr
6482 c_parser_objc_message_args (c_parser *parser)
6484 tree sel = c_parser_objc_selector (parser);
6485 tree list = NULL_TREE;
6486 if (sel && c_parser_next_token_is_not (parser, CPP_COLON))
6491 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
6493 keywordexpr = c_parser_objc_keywordexpr (parser);
6494 list = chainon (list, build_tree_list (sel, keywordexpr));
6495 sel = c_parser_objc_selector (parser);
6496 if (!sel && c_parser_next_token_is_not (parser, CPP_COLON))
6502 /* Parse an objc-keywordexpr.
6509 c_parser_objc_keywordexpr (c_parser *parser)
6511 tree list = c_parser_expr_list (parser, true);
6512 if (TREE_CHAIN (list) == NULL_TREE)
6514 /* Just return the expression, remove a level of
6516 return TREE_VALUE (list);
6520 /* We have a comma expression, we will collapse later. */
6526 /* Handle pragmas. Some OpenMP pragmas are associated with, and therefore
6527 should be considered, statements. ALLOW_STMT is true if we're within
6528 the context of a function and such pragmas are to be allowed. Returns
6529 true if we actually parsed such a pragma. */
6532 c_parser_pragma (c_parser *parser, enum pragma_context context)
6536 id = c_parser_peek_token (parser)->pragma_kind;
6537 gcc_assert (id != PRAGMA_NONE);
6541 case PRAGMA_OMP_BARRIER:
6542 if (context != pragma_compound)
6544 if (context == pragma_stmt)
6545 c_parser_error (parser, "%<#pragma omp barrier%> may only be "
6546 "used in compound statements");
6549 c_parser_omp_barrier (parser);
6552 case PRAGMA_OMP_FLUSH:
6553 if (context != pragma_compound)
6555 if (context == pragma_stmt)
6556 c_parser_error (parser, "%<#pragma omp flush%> may only be "
6557 "used in compound statements");
6560 c_parser_omp_flush (parser);
6563 case PRAGMA_OMP_THREADPRIVATE:
6564 c_parser_omp_threadprivate (parser);
6567 case PRAGMA_OMP_SECTION:
6568 error ("%<#pragma omp section%> may only be used in "
6569 "%<#pragma omp sections%> construct");
6570 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
6573 case PRAGMA_GCC_PCH_PREPROCESS:
6574 c_parser_error (parser, "%<#pragma GCC pch_preprocess%> must be first");
6575 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
6579 if (id < PRAGMA_FIRST_EXTERNAL)
6581 if (context == pragma_external)
6584 c_parser_error (parser, "expected declaration specifiers");
6585 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
6588 c_parser_omp_construct (parser);
6594 c_parser_consume_pragma (parser);
6595 c_invoke_pragma_handler (id);
6597 /* Skip to EOL, but suppress any error message. Those will have been
6598 generated by the handler routine through calling error, as opposed
6599 to calling c_parser_error. */
6600 parser->error = true;
6601 c_parser_skip_to_pragma_eol (parser);
6606 /* The interface the pragma parsers have to the lexer. */
6609 pragma_lex (tree *value)
6611 c_token *tok = c_parser_peek_token (the_parser);
6612 enum cpp_ttype ret = tok->type;
6614 *value = tok->value;
6615 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
6619 if (ret == CPP_KEYWORD)
6621 c_parser_consume_token (the_parser);
6628 c_parser_pragma_pch_preprocess (c_parser *parser)
6632 c_parser_consume_pragma (parser);
6633 if (c_parser_next_token_is (parser, CPP_STRING))
6635 name = c_parser_peek_token (parser)->value;
6636 c_parser_consume_token (parser);
6639 c_parser_error (parser, "expected string literal");
6640 c_parser_skip_to_pragma_eol (parser);
6643 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
6646 /* OpenMP 2.5 parsing routines. */
6648 /* Returns name of the next clause.
6649 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
6650 the token is not consumed. Otherwise appropriate pragma_omp_clause is
6651 returned and the token is consumed. */
6653 static pragma_omp_clause
6654 c_parser_omp_clause_name (c_parser *parser)
6656 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
6658 if (c_parser_next_token_is_keyword (parser, RID_IF))
6659 result = PRAGMA_OMP_CLAUSE_IF;
6660 else if (c_parser_next_token_is_keyword (parser, RID_DEFAULT))
6661 result = PRAGMA_OMP_CLAUSE_DEFAULT;
6662 else if (c_parser_next_token_is (parser, CPP_NAME))
6664 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
6669 if (!strcmp ("copyin", p))
6670 result = PRAGMA_OMP_CLAUSE_COPYIN;
6671 else if (!strcmp ("copyprivate", p))
6672 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
6675 if (!strcmp ("firstprivate", p))
6676 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
6679 if (!strcmp ("lastprivate", p))
6680 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
6683 if (!strcmp ("nowait", p))
6684 result = PRAGMA_OMP_CLAUSE_NOWAIT;
6685 else if (!strcmp ("num_threads", p))
6686 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
6689 if (!strcmp ("ordered", p))
6690 result = PRAGMA_OMP_CLAUSE_ORDERED;
6693 if (!strcmp ("private", p))
6694 result = PRAGMA_OMP_CLAUSE_PRIVATE;
6697 if (!strcmp ("reduction", p))
6698 result = PRAGMA_OMP_CLAUSE_REDUCTION;
6701 if (!strcmp ("schedule", p))
6702 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
6703 else if (!strcmp ("shared", p))
6704 result = PRAGMA_OMP_CLAUSE_SHARED;
6709 if (result != PRAGMA_OMP_CLAUSE_NONE)
6710 c_parser_consume_token (parser);
6715 /* Validate that a clause of the given type does not already exist. */
6718 check_no_duplicate_clause (tree clauses, enum tree_code code, const char *name)
6722 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
6723 if (OMP_CLAUSE_CODE (c) == code)
6725 error ("too many %qs clauses", name);
6733 variable-list , identifier
6735 If KIND is nonzero, create the appropriate node and install the decl
6736 in OMP_CLAUSE_DECL and add the node to the head of the list.
6738 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
6739 return the list created. */
6742 c_parser_omp_variable_list (c_parser *parser, enum omp_clause_code kind,
6745 if (c_parser_next_token_is_not (parser, CPP_NAME)
6746 || c_parser_peek_token (parser)->id_kind != C_ID_ID)
6747 c_parser_error (parser, "expected identifier");
6749 while (c_parser_next_token_is (parser, CPP_NAME)
6750 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
6752 tree t = lookup_name (c_parser_peek_token (parser)->value);
6755 undeclared_variable (c_parser_peek_token (parser)->value,
6756 c_parser_peek_token (parser)->location);
6757 else if (t == error_mark_node)
6761 tree u = build_omp_clause (kind);
6762 OMP_CLAUSE_DECL (u) = t;
6763 OMP_CLAUSE_CHAIN (u) = list;
6767 list = tree_cons (t, NULL_TREE, list);
6769 c_parser_consume_token (parser);
6771 if (c_parser_next_token_is_not (parser, CPP_COMMA))
6774 c_parser_consume_token (parser);
6780 /* Similarly, but expect leading and trailing parenthesis. This is a very
6781 common case for omp clauses. */
6784 c_parser_omp_var_list_parens (c_parser *parser, enum tree_code kind, tree list)
6786 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6788 list = c_parser_omp_variable_list (parser, kind, list);
6789 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
6795 copyin ( variable-list ) */
6798 c_parser_omp_clause_copyin (c_parser *parser, tree list)
6800 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_COPYIN, list);
6804 copyprivate ( variable-list ) */
6807 c_parser_omp_clause_copyprivate (c_parser *parser, tree list)
6809 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_COPYPRIVATE, list);
6813 default ( shared | none ) */
6816 c_parser_omp_clause_default (c_parser *parser, tree list)
6818 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
6821 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6823 if (c_parser_next_token_is (parser, CPP_NAME))
6825 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
6830 if (strcmp ("none", p) != 0)
6832 kind = OMP_CLAUSE_DEFAULT_NONE;
6836 if (strcmp ("shared", p) != 0)
6838 kind = OMP_CLAUSE_DEFAULT_SHARED;
6845 c_parser_consume_token (parser);
6850 c_parser_error (parser, "expected %<none%> or %<shared%>");
6852 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
6854 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
6857 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default");
6858 c = build_omp_clause (OMP_CLAUSE_DEFAULT);
6859 OMP_CLAUSE_CHAIN (c) = list;
6860 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
6866 firstprivate ( variable-list ) */
6869 c_parser_omp_clause_firstprivate (c_parser *parser, tree list)
6871 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_FIRSTPRIVATE, list);
6875 if ( expression ) */
6878 c_parser_omp_clause_if (c_parser *parser, tree list)
6880 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
6882 tree t = c_parser_paren_condition (parser);
6885 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if");
6887 c = build_omp_clause (OMP_CLAUSE_IF);
6888 OMP_CLAUSE_IF_EXPR (c) = t;
6889 OMP_CLAUSE_CHAIN (c) = list;
6893 c_parser_error (parser, "expected %<(%>");
6899 lastprivate ( variable-list ) */
6902 c_parser_omp_clause_lastprivate (c_parser *parser, tree list)
6904 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_LASTPRIVATE, list);
6911 c_parser_omp_clause_nowait (c_parser *parser ATTRIBUTE_UNUSED, tree list)
6915 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait");
6917 c = build_omp_clause (OMP_CLAUSE_NOWAIT);
6918 OMP_CLAUSE_CHAIN (c) = list;
6923 num_threads ( expression ) */
6926 c_parser_omp_clause_num_threads (c_parser *parser, tree list)
6928 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6930 tree c, t = c_parser_expression (parser).value;
6932 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
6934 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
6936 c_parser_error (parser, "expected integer expression");
6940 /* Attempt to statically determine when the number isn't positive. */
6941 c = fold_build2 (LE_EXPR, boolean_type_node, t,
6942 build_int_cst (TREE_TYPE (t), 0));
6943 if (c == boolean_true_node)
6945 warning (0, "%<num_threads%> value must be positive");
6946 t = integer_one_node;
6949 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS, "num_threads");
6951 c = build_omp_clause (OMP_CLAUSE_NUM_THREADS);
6952 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
6953 OMP_CLAUSE_CHAIN (c) = list;
6964 c_parser_omp_clause_ordered (c_parser *parser ATTRIBUTE_UNUSED, tree list)
6968 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED, "ordered");
6970 c = build_omp_clause (OMP_CLAUSE_ORDERED);
6971 OMP_CLAUSE_CHAIN (c) = list;
6976 private ( variable-list ) */
6979 c_parser_omp_clause_private (c_parser *parser, tree list)
6981 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_PRIVATE, list);
6985 reduction ( reduction-operator : variable-list )
6988 One of: + * - & ^ | && || */
6991 c_parser_omp_clause_reduction (c_parser *parser, tree list)
6993 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6995 enum tree_code code;
6997 switch (c_parser_peek_token (parser)->type)
7009 code = BIT_AND_EXPR;
7012 code = BIT_XOR_EXPR;
7015 code = BIT_IOR_EXPR;
7018 code = TRUTH_ANDIF_EXPR;
7021 code = TRUTH_ORIF_EXPR;
7024 c_parser_error (parser,
7025 "expected %<+%>, %<*%>, %<-%>, %<&%>, "
7026 "%<^%>, %<|%>, %<&&%>, or %<||%>");
7027 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 0);
7030 c_parser_consume_token (parser);
7031 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
7035 nl = c_parser_omp_variable_list (parser, OMP_CLAUSE_REDUCTION, list);
7036 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
7037 OMP_CLAUSE_REDUCTION_CODE (c) = code;
7041 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
7047 schedule ( schedule-kind )
7048 schedule ( schedule-kind , expression )
7051 static | dynamic | guided | runtime
7055 c_parser_omp_clause_schedule (c_parser *parser, tree list)
7059 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7062 c = build_omp_clause (OMP_CLAUSE_SCHEDULE);
7064 if (c_parser_next_token_is (parser, CPP_NAME))
7066 tree kind = c_parser_peek_token (parser)->value;
7067 const char *p = IDENTIFIER_POINTER (kind);
7072 if (strcmp ("dynamic", p) != 0)
7074 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
7078 if (strcmp ("guided", p) != 0)
7080 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
7084 if (strcmp ("runtime", p) != 0)
7086 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
7093 else if (c_parser_next_token_is_keyword (parser, RID_STATIC))
7094 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
7098 c_parser_consume_token (parser);
7099 if (c_parser_next_token_is (parser, CPP_COMMA))
7101 c_parser_consume_token (parser);
7103 t = c_parser_expr_no_commas (parser, NULL).value;
7105 if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
7106 error ("schedule %<runtime%> does not take "
7107 "a %<chunk_size%> parameter");
7108 else if (TREE_CODE (TREE_TYPE (t)) == INTEGER_TYPE)
7109 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
7111 c_parser_error (parser, "expected integer expression");
7113 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
7116 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7117 "expected %<,%> or %<)%>");
7119 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule");
7120 OMP_CLAUSE_CHAIN (c) = list;
7124 c_parser_error (parser, "invalid schedule kind");
7125 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 0);
7130 shared ( variable-list ) */
7133 c_parser_omp_clause_shared (c_parser *parser, tree list)
7135 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_SHARED, list);
7138 /* Parse all OpenMP clauses. The set clauses allowed by the directive
7139 is a bitmask in MASK. Return the list of clauses found; the result
7140 of clause default goes in *pdefault. */
7143 c_parser_omp_all_clauses (c_parser *parser, unsigned int mask,
7146 tree clauses = NULL;
7148 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
7150 const pragma_omp_clause c_kind = c_parser_omp_clause_name (parser);
7152 tree prev = clauses;
7156 case PRAGMA_OMP_CLAUSE_COPYIN:
7157 clauses = c_parser_omp_clause_copyin (parser, clauses);
7160 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
7161 clauses = c_parser_omp_clause_copyprivate (parser, clauses);
7162 c_name = "copyprivate";
7164 case PRAGMA_OMP_CLAUSE_DEFAULT:
7165 clauses = c_parser_omp_clause_default (parser, clauses);
7168 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
7169 clauses = c_parser_omp_clause_firstprivate (parser, clauses);
7170 c_name = "firstprivate";
7172 case PRAGMA_OMP_CLAUSE_IF:
7173 clauses = c_parser_omp_clause_if (parser, clauses);
7176 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
7177 clauses = c_parser_omp_clause_lastprivate (parser, clauses);
7178 c_name = "lastprivate";
7180 case PRAGMA_OMP_CLAUSE_NOWAIT:
7181 clauses = c_parser_omp_clause_nowait (parser, clauses);
7184 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
7185 clauses = c_parser_omp_clause_num_threads (parser, clauses);
7186 c_name = "num_threads";
7188 case PRAGMA_OMP_CLAUSE_ORDERED:
7189 clauses = c_parser_omp_clause_ordered (parser, clauses);
7192 case PRAGMA_OMP_CLAUSE_PRIVATE:
7193 clauses = c_parser_omp_clause_private (parser, clauses);
7196 case PRAGMA_OMP_CLAUSE_REDUCTION:
7197 clauses = c_parser_omp_clause_reduction (parser, clauses);
7198 c_name = "reduction";
7200 case PRAGMA_OMP_CLAUSE_SCHEDULE:
7201 clauses = c_parser_omp_clause_schedule (parser, clauses);
7202 c_name = "schedule";
7204 case PRAGMA_OMP_CLAUSE_SHARED:
7205 clauses = c_parser_omp_clause_shared (parser, clauses);
7209 c_parser_error (parser, "expected %<#pragma omp%> clause");
7213 if (((mask >> c_kind) & 1) == 0 && !parser->error)
7215 /* Remove the invalid clause(s) from the list to avoid
7216 confusing the rest of the compiler. */
7218 error ("%qs is not valid for %qs", c_name, where);
7223 c_parser_skip_to_pragma_eol (parser);
7225 return c_finish_omp_clauses (clauses);
7232 In practice, we're also interested in adding the statement to an
7233 outer node. So it is convenient if we work around the fact that
7234 c_parser_statement calls add_stmt. */
7237 c_parser_omp_structured_block (c_parser *parser)
7239 tree stmt = push_stmt_list ();
7240 c_parser_statement (parser);
7241 return pop_stmt_list (stmt);
7245 # pragma omp atomic new-line
7249 x binop= expr | x++ | ++x | x-- | --x
7251 +, *, -, /, &, ^, |, <<, >>
7253 where x is an lvalue expression with scalar type. */
7256 c_parser_omp_atomic (c_parser *parser)
7260 enum tree_code code;
7262 c_parser_skip_to_pragma_eol (parser);
7264 lhs = c_parser_unary_expression (parser).value;
7265 switch (TREE_CODE (lhs))
7269 c_parser_skip_to_end_of_block_or_statement (parser);
7272 case PREINCREMENT_EXPR:
7273 case POSTINCREMENT_EXPR:
7274 lhs = TREE_OPERAND (lhs, 0);
7276 rhs = integer_one_node;
7279 case PREDECREMENT_EXPR:
7280 case POSTDECREMENT_EXPR:
7281 lhs = TREE_OPERAND (lhs, 0);
7283 rhs = integer_one_node;
7287 switch (c_parser_peek_token (parser)->type)
7293 code = TRUNC_DIV_EXPR;
7308 code = BIT_AND_EXPR;
7311 code = BIT_IOR_EXPR;
7314 code = BIT_XOR_EXPR;
7317 c_parser_error (parser,
7318 "invalid operator for %<#pragma omp atomic%>");
7322 c_parser_consume_token (parser);
7323 rhs = c_parser_expression (parser).value;
7326 stmt = c_finish_omp_atomic (code, lhs, rhs);
7327 if (stmt != error_mark_node)
7329 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
7334 # pragma omp barrier new-line
7338 c_parser_omp_barrier (c_parser *parser)
7340 c_parser_consume_pragma (parser);
7341 c_parser_skip_to_pragma_eol (parser);
7343 c_finish_omp_barrier ();
7347 # pragma omp critical [(name)] new-line
7352 c_parser_omp_critical (c_parser *parser)
7354 tree stmt, name = NULL;
7356 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
7358 c_parser_consume_token (parser);
7359 if (c_parser_next_token_is (parser, CPP_NAME))
7361 name = c_parser_peek_token (parser)->value;
7362 c_parser_consume_token (parser);
7363 c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
7366 c_parser_error (parser, "expected identifier");
7368 else if (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
7369 c_parser_error (parser, "expected %<(%> or end of line");
7370 c_parser_skip_to_pragma_eol (parser);
7372 stmt = c_parser_omp_structured_block (parser);
7373 return c_finish_omp_critical (stmt, name);
7377 # pragma omp flush flush-vars[opt] new-line
7380 ( variable-list ) */
7383 c_parser_omp_flush (c_parser *parser)
7385 c_parser_consume_pragma (parser);
7386 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
7387 c_parser_omp_var_list_parens (parser, 0, NULL);
7388 else if (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
7389 c_parser_error (parser, "expected %<(%> or end of line");
7390 c_parser_skip_to_pragma_eol (parser);
7392 c_finish_omp_flush ();
7395 /* Parse the restricted form of the for statment allowed by OpenMP.
7396 The real trick here is to determine the loop control variable early
7397 so that we can push a new decl if necessary to make it private. */
7400 c_parser_omp_for_loop (c_parser *parser)
7402 tree decl, cond, incr, save_break, save_cont, body, init;
7405 if (!c_parser_next_token_is_keyword (parser, RID_FOR))
7407 c_parser_error (parser, "for statement expected");
7410 loc = c_parser_peek_token (parser)->location;
7411 c_parser_consume_token (parser);
7413 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7416 /* Parse the initialization declaration or expression. */
7417 if (c_parser_next_token_starts_declspecs (parser))
7419 c_parser_declaration_or_fndef (parser, true, true, true, true);
7420 decl = check_for_loop_decls ();
7425 else if (c_parser_next_token_is (parser, CPP_NAME)
7426 && c_parser_peek_2nd_token (parser)->type == CPP_EQ)
7428 decl = c_parser_postfix_expression (parser).value;
7430 c_parser_require (parser, CPP_EQ, "expected %<=%>");
7432 init = c_parser_expr_no_commas (parser, NULL).value;
7433 init = build_modify_expr (decl, NOP_EXPR, init);
7434 init = c_process_expr_stmt (init);
7436 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
7441 /* Parse the loop condition. */
7443 if (c_parser_next_token_is_not (parser, CPP_SEMICOLON))
7445 cond = c_parser_expression_conv (parser).value;
7446 cond = c_objc_common_truthvalue_conversion (cond);
7447 if (CAN_HAVE_LOCATION_P (cond))
7448 SET_EXPR_LOCATION (cond, input_location);
7450 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
7452 /* Parse the increment expression. */
7454 if (c_parser_next_token_is_not (parser, CPP_CLOSE_PAREN))
7455 incr = c_process_expr_stmt (c_parser_expression (parser).value);
7456 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
7459 save_break = c_break_label;
7460 c_break_label = size_one_node;
7461 save_cont = c_cont_label;
7462 c_cont_label = NULL_TREE;
7463 body = push_stmt_list ();
7465 add_stmt (c_parser_c99_block_statement (parser));
7467 add_stmt (build1 (LABEL_EXPR, void_type_node, c_cont_label));
7469 body = pop_stmt_list (body);
7470 c_break_label = save_break;
7471 c_cont_label = save_cont;
7473 /* Only bother calling c_finish_omp_for if we havn't already generated
7474 an error from the initialization parsing. */
7475 if (decl != NULL && decl != error_mark_node && init != error_mark_node)
7476 return c_finish_omp_for (loc, decl, init, cond, incr, body, NULL);
7480 c_parser_error (parser, "expected iteration declaration or initialization");
7481 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
7482 decl = init = cond = incr = NULL_TREE;
7487 #pragma omp for for-clause[optseq] new-line
7491 #define OMP_FOR_CLAUSE_MASK \
7492 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
7493 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
7494 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
7495 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
7496 | (1u << PRAGMA_OMP_CLAUSE_ORDERED) \
7497 | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE) \
7498 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
7501 c_parser_omp_for (c_parser *parser)
7503 tree block, clauses, ret;
7505 clauses = c_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
7508 block = c_begin_compound_stmt (true);
7509 ret = c_parser_omp_for_loop (parser);
7511 OMP_FOR_CLAUSES (ret) = clauses;
7512 block = c_end_compound_stmt (block, true);
7519 # pragma omp master new-line
7524 c_parser_omp_master (c_parser *parser)
7526 c_parser_skip_to_pragma_eol (parser);
7527 return c_finish_omp_master (c_parser_omp_structured_block (parser));
7531 # pragma omp ordered new-line
7536 c_parser_omp_ordered (c_parser *parser)
7538 c_parser_skip_to_pragma_eol (parser);
7539 return c_finish_omp_ordered (c_parser_omp_structured_block (parser));
7545 { section-sequence }
7548 section-directive[opt] structured-block
7549 section-sequence section-directive structured-block */
7552 c_parser_omp_sections_scope (c_parser *parser)
7555 bool error_suppress = false;
7558 if (!c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
7560 /* Avoid skipping until the end of the block. */
7561 parser->error = false;
7565 stmt = push_stmt_list ();
7567 loc = c_parser_peek_token (parser)->location;
7568 if (c_parser_peek_token (parser)->pragma_kind != PRAGMA_OMP_SECTION)
7570 substmt = push_stmt_list ();
7574 c_parser_statement (parser);
7576 if (c_parser_peek_token (parser)->pragma_kind == PRAGMA_OMP_SECTION)
7578 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
7580 if (c_parser_next_token_is (parser, CPP_EOF))
7584 substmt = pop_stmt_list (substmt);
7585 substmt = build1 (OMP_SECTION, void_type_node, substmt);
7586 SET_EXPR_LOCATION (substmt, loc);
7592 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
7594 if (c_parser_next_token_is (parser, CPP_EOF))
7597 loc = c_parser_peek_token (parser)->location;
7598 if (c_parser_peek_token (parser)->pragma_kind == PRAGMA_OMP_SECTION)
7600 c_parser_consume_pragma (parser);
7601 c_parser_skip_to_pragma_eol (parser);
7602 error_suppress = false;
7604 else if (!error_suppress)
7606 error ("expected %<#pragma omp section%> or %<}%>");
7607 error_suppress = true;
7610 substmt = c_parser_omp_structured_block (parser);
7611 substmt = build1 (OMP_SECTION, void_type_node, substmt);
7612 SET_EXPR_LOCATION (substmt, loc);
7615 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE,
7616 "expected %<#pragma omp section%> or %<}%>");
7618 substmt = pop_stmt_list (stmt);
7620 stmt = make_node (OMP_SECTIONS);
7621 TREE_TYPE (stmt) = void_type_node;
7622 OMP_SECTIONS_BODY (stmt) = substmt;
7624 return add_stmt (stmt);
7628 # pragma omp sections sections-clause[optseq] newline
7632 #define OMP_SECTIONS_CLAUSE_MASK \
7633 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
7634 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
7635 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
7636 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
7637 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
7640 c_parser_omp_sections (c_parser *parser)
7642 tree block, clauses, ret;
7644 clauses = c_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
7645 "#pragma omp sections");
7647 block = c_begin_compound_stmt (true);
7648 ret = c_parser_omp_sections_scope (parser);
7650 OMP_SECTIONS_CLAUSES (ret) = clauses;
7651 block = c_end_compound_stmt (block, true);
7658 # pragma parallel parallel-clause new-line
7659 # pragma parallel for parallel-for-clause new-line
7660 # pragma parallel sections parallel-sections-clause new-line
7663 #define OMP_PARALLEL_CLAUSE_MASK \
7664 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
7665 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
7666 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
7667 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
7668 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
7669 | (1u << PRAGMA_OMP_CLAUSE_COPYIN) \
7670 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
7671 | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
7674 c_parser_omp_parallel (c_parser *parser)
7676 enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
7677 const char *p_name = "#pragma omp parallel";
7678 tree stmt, clauses, par_clause, ws_clause, block;
7679 unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
7681 if (c_parser_next_token_is_keyword (parser, RID_FOR))
7683 c_parser_consume_token (parser);
7684 p_kind = PRAGMA_OMP_PARALLEL_FOR;
7685 p_name = "#pragma omp parallel for";
7686 mask |= OMP_FOR_CLAUSE_MASK;
7687 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
7689 else if (c_parser_next_token_is (parser, CPP_NAME))
7691 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
7692 if (strcmp (p, "sections") == 0)
7694 c_parser_consume_token (parser);
7695 p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
7696 p_name = "#pragma omp parallel sections";
7697 mask |= OMP_SECTIONS_CLAUSE_MASK;
7698 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
7702 clauses = c_parser_omp_all_clauses (parser, mask, p_name);
7706 case PRAGMA_OMP_PARALLEL:
7707 block = c_begin_omp_parallel ();
7708 c_parser_statement (parser);
7709 stmt = c_finish_omp_parallel (clauses, block);
7712 case PRAGMA_OMP_PARALLEL_FOR:
7713 block = c_begin_omp_parallel ();
7714 c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
7715 stmt = c_parser_omp_for_loop (parser);
7717 OMP_FOR_CLAUSES (stmt) = ws_clause;
7718 stmt = c_finish_omp_parallel (par_clause, block);
7719 OMP_PARALLEL_COMBINED (stmt) = 1;
7722 case PRAGMA_OMP_PARALLEL_SECTIONS:
7723 block = c_begin_omp_parallel ();
7724 c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
7725 stmt = c_parser_omp_sections_scope (parser);
7727 OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
7728 stmt = c_finish_omp_parallel (par_clause, block);
7729 OMP_PARALLEL_COMBINED (stmt) = 1;
7740 # pragma omp single single-clause[optseq] new-line
7744 #define OMP_SINGLE_CLAUSE_MASK \
7745 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
7746 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
7747 | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
7748 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
7751 c_parser_omp_single (c_parser *parser)
7753 tree stmt = make_node (OMP_SINGLE);
7754 TREE_TYPE (stmt) = void_type_node;
7756 OMP_SINGLE_CLAUSES (stmt)
7757 = c_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
7758 "#pragma omp single");
7759 OMP_SINGLE_BODY (stmt) = c_parser_omp_structured_block (parser);
7761 return add_stmt (stmt);
7765 /* Main entry point to parsing most OpenMP pragmas. */
7768 c_parser_omp_construct (c_parser *parser)
7770 enum pragma_kind p_kind;
7774 loc = c_parser_peek_token (parser)->location;
7775 p_kind = c_parser_peek_token (parser)->pragma_kind;
7776 c_parser_consume_pragma (parser);
7778 /* For all constructs below except #pragma omp atomic
7779 MUST_NOT_THROW catch handlers are needed when exceptions
7781 if (p_kind != PRAGMA_OMP_ATOMIC)
7782 c_maybe_initialize_eh ();
7786 case PRAGMA_OMP_ATOMIC:
7787 c_parser_omp_atomic (parser);
7789 case PRAGMA_OMP_CRITICAL:
7790 stmt = c_parser_omp_critical (parser);
7792 case PRAGMA_OMP_FOR:
7793 stmt = c_parser_omp_for (parser);
7795 case PRAGMA_OMP_MASTER:
7796 stmt = c_parser_omp_master (parser);
7798 case PRAGMA_OMP_ORDERED:
7799 stmt = c_parser_omp_ordered (parser);
7801 case PRAGMA_OMP_PARALLEL:
7802 stmt = c_parser_omp_parallel (parser);
7804 case PRAGMA_OMP_SECTIONS:
7805 stmt = c_parser_omp_sections (parser);
7807 case PRAGMA_OMP_SINGLE:
7808 stmt = c_parser_omp_single (parser);
7815 SET_EXPR_LOCATION (stmt, loc);
7820 # pragma omp threadprivate (variable-list) */
7823 c_parser_omp_threadprivate (c_parser *parser)
7827 c_parser_consume_pragma (parser);
7828 vars = c_parser_omp_var_list_parens (parser, 0, NULL);
7830 /* Mark every variable in VARS to be assigned thread local storage. */
7831 for (t = vars; t; t = TREE_CHAIN (t))
7833 tree v = TREE_PURPOSE (t);
7835 /* If V had already been marked threadprivate, it doesn't matter
7836 whether it had been used prior to this point. */
7837 if (TREE_USED (v) && !C_DECL_THREADPRIVATE_P (v))
7838 error ("%qE declared %<threadprivate%> after first use", v);
7839 else if (! TREE_STATIC (v) && ! DECL_EXTERNAL (v))
7840 error ("automatic variable %qE cannot be %<threadprivate%>", v);
7841 else if (! COMPLETE_TYPE_P (TREE_TYPE (v)))
7842 error ("%<threadprivate%> %qE has incomplete type", v);
7845 if (! DECL_THREAD_LOCAL_P (v))
7847 DECL_TLS_MODEL (v) = decl_default_tls_model (v);
7848 /* If rtl has been already set for this var, call
7849 make_decl_rtl once again, so that encode_section_info
7850 has a chance to look at the new decl flags. */
7851 if (DECL_RTL_SET_P (v))
7854 C_DECL_THREADPRIVATE_P (v) = 1;
7858 c_parser_skip_to_pragma_eol (parser);
7862 /* Parse a single source file. */
7867 /* Use local storage to begin. If the first token is a pragma, parse it.
7868 If it is #pragma GCC pch_preprocess, then this will load a PCH file
7869 which will cause garbage collection. */
7872 memset (&tparser, 0, sizeof tparser);
7873 the_parser = &tparser;
7875 if (c_parser_peek_token (&tparser)->pragma_kind == PRAGMA_GCC_PCH_PREPROCESS)
7876 c_parser_pragma_pch_preprocess (&tparser);
7878 the_parser = GGC_NEW (c_parser);
7879 *the_parser = tparser;
7881 c_parser_translation_unit (the_parser);
7885 #include "gt-c-parser.h"