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 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 2, 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 COPYING. If not, write to the Free
22 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
27 Make sure all relevant comments, and all relevant code from all
28 actions, brought over from old parser. Verify exact correspondence
31 Add testcases covering every input symbol in every state in old and
34 Include full syntax for GNU C, including erroneous cases accepted
35 with error messages, in syntax productions in comments.
37 Make more diagnostics in the front end generally take an explicit
38 location rather than implicitly using input_location. */
42 #include "coretypes.h"
46 #include "langhooks.h"
62 /* Miscellaneous data and functions needed for the parser. */
66 /* Objective-C specific parser/lexer information. */
68 static int objc_pq_context = 0;
70 /* The following flag is needed to contextualize Objective-C lexical
71 analysis. In some cases (e.g., 'int NSObject;'), it is undesirable
72 to bind an identifier to an Objective-C class, even if a class with
74 static int objc_need_raw_identifier = 0;
75 #define OBJC_NEED_RAW_IDENTIFIER(VAL) \
77 if (c_dialect_objc ()) \
78 objc_need_raw_identifier = VAL; \
81 /* The reserved keyword table. */
85 ENUM_BITFIELD(rid) rid : 16;
86 unsigned int disable : 16;
89 /* Disable mask. Keywords are disabled if (reswords[i].disable &
91 #define D_C89 0x01 /* not in C89 */
92 #define D_EXT 0x02 /* GCC extension */
93 #define D_EXT89 0x04 /* GCC extension incorporated in C99 */
94 #define D_OBJC 0x08 /* Objective C only */
96 static const struct resword reswords[] =
98 { "_Bool", RID_BOOL, 0 },
99 { "_Complex", RID_COMPLEX, 0 },
100 { "_Decimal32", RID_DFLOAT32, D_EXT },
101 { "_Decimal64", RID_DFLOAT64, D_EXT },
102 { "_Decimal128", RID_DFLOAT128, D_EXT },
103 { "__FUNCTION__", RID_FUNCTION_NAME, 0 },
104 { "__PRETTY_FUNCTION__", RID_PRETTY_FUNCTION_NAME, 0 },
105 { "__alignof", RID_ALIGNOF, 0 },
106 { "__alignof__", RID_ALIGNOF, 0 },
107 { "__asm", RID_ASM, 0 },
108 { "__asm__", RID_ASM, 0 },
109 { "__attribute", RID_ATTRIBUTE, 0 },
110 { "__attribute__", RID_ATTRIBUTE, 0 },
111 { "__builtin_choose_expr", RID_CHOOSE_EXPR, 0 },
112 { "__builtin_offsetof", RID_OFFSETOF, 0 },
113 { "__builtin_types_compatible_p", RID_TYPES_COMPATIBLE_P, 0 },
114 { "__builtin_va_arg", RID_VA_ARG, 0 },
115 { "__complex", RID_COMPLEX, 0 },
116 { "__complex__", RID_COMPLEX, 0 },
117 { "__const", RID_CONST, 0 },
118 { "__const__", RID_CONST, 0 },
119 { "__extension__", RID_EXTENSION, 0 },
120 { "__func__", RID_C99_FUNCTION_NAME, 0 },
121 { "__imag", RID_IMAGPART, 0 },
122 { "__imag__", RID_IMAGPART, 0 },
123 { "__inline", RID_INLINE, 0 },
124 { "__inline__", RID_INLINE, 0 },
125 { "__label__", RID_LABEL, 0 },
126 { "__real", RID_REALPART, 0 },
127 { "__real__", RID_REALPART, 0 },
128 { "__restrict", RID_RESTRICT, 0 },
129 { "__restrict__", RID_RESTRICT, 0 },
130 { "__signed", RID_SIGNED, 0 },
131 { "__signed__", RID_SIGNED, 0 },
132 { "__thread", RID_THREAD, 0 },
133 { "__typeof", RID_TYPEOF, 0 },
134 { "__typeof__", RID_TYPEOF, 0 },
135 { "__volatile", RID_VOLATILE, 0 },
136 { "__volatile__", RID_VOLATILE, 0 },
137 { "asm", RID_ASM, D_EXT },
138 { "auto", RID_AUTO, 0 },
139 { "break", RID_BREAK, 0 },
140 { "case", RID_CASE, 0 },
141 { "char", RID_CHAR, 0 },
142 { "const", RID_CONST, 0 },
143 { "continue", RID_CONTINUE, 0 },
144 { "default", RID_DEFAULT, 0 },
146 { "double", RID_DOUBLE, 0 },
147 { "else", RID_ELSE, 0 },
148 { "enum", RID_ENUM, 0 },
149 { "extern", RID_EXTERN, 0 },
150 { "float", RID_FLOAT, 0 },
151 { "for", RID_FOR, 0 },
152 { "goto", RID_GOTO, 0 },
154 { "inline", RID_INLINE, D_EXT89 },
155 { "int", RID_INT, 0 },
156 { "long", RID_LONG, 0 },
157 { "register", RID_REGISTER, 0 },
158 { "restrict", RID_RESTRICT, D_C89 },
159 { "return", RID_RETURN, 0 },
160 { "short", RID_SHORT, 0 },
161 { "signed", RID_SIGNED, 0 },
162 { "sizeof", RID_SIZEOF, 0 },
163 { "static", RID_STATIC, 0 },
164 { "struct", RID_STRUCT, 0 },
165 { "switch", RID_SWITCH, 0 },
166 { "typedef", RID_TYPEDEF, 0 },
167 { "typeof", RID_TYPEOF, D_EXT },
168 { "union", RID_UNION, 0 },
169 { "unsigned", RID_UNSIGNED, 0 },
170 { "void", RID_VOID, 0 },
171 { "volatile", RID_VOLATILE, 0 },
172 { "while", RID_WHILE, 0 },
173 /* These Objective-C keywords are recognized only immediately after
175 { "class", RID_AT_CLASS, D_OBJC },
176 { "compatibility_alias", RID_AT_ALIAS, D_OBJC },
177 { "defs", RID_AT_DEFS, D_OBJC },
178 { "encode", RID_AT_ENCODE, D_OBJC },
179 { "end", RID_AT_END, D_OBJC },
180 { "implementation", RID_AT_IMPLEMENTATION, D_OBJC },
181 { "interface", RID_AT_INTERFACE, D_OBJC },
182 { "private", RID_AT_PRIVATE, D_OBJC },
183 { "protected", RID_AT_PROTECTED, D_OBJC },
184 { "protocol", RID_AT_PROTOCOL, D_OBJC },
185 { "public", RID_AT_PUBLIC, D_OBJC },
186 { "selector", RID_AT_SELECTOR, D_OBJC },
187 { "throw", RID_AT_THROW, D_OBJC },
188 { "try", RID_AT_TRY, D_OBJC },
189 { "catch", RID_AT_CATCH, D_OBJC },
190 { "finally", RID_AT_FINALLY, D_OBJC },
191 { "synchronized", RID_AT_SYNCHRONIZED, D_OBJC },
192 /* These are recognized only in protocol-qualifier context
194 { "bycopy", RID_BYCOPY, D_OBJC },
195 { "byref", RID_BYREF, D_OBJC },
196 { "in", RID_IN, D_OBJC },
197 { "inout", RID_INOUT, D_OBJC },
198 { "oneway", RID_ONEWAY, D_OBJC },
199 { "out", RID_OUT, D_OBJC },
201 #define N_reswords (sizeof reswords / sizeof (struct resword))
203 /* All OpenMP clauses. OpenMP 2.5. */
204 typedef enum pragma_omp_clause {
205 PRAGMA_OMP_CLAUSE_NONE = 0,
207 PRAGMA_OMP_CLAUSE_COPYIN,
208 PRAGMA_OMP_CLAUSE_COPYPRIVATE,
209 PRAGMA_OMP_CLAUSE_DEFAULT,
210 PRAGMA_OMP_CLAUSE_FIRSTPRIVATE,
211 PRAGMA_OMP_CLAUSE_IF,
212 PRAGMA_OMP_CLAUSE_LASTPRIVATE,
213 PRAGMA_OMP_CLAUSE_NOWAIT,
214 PRAGMA_OMP_CLAUSE_NUM_THREADS,
215 PRAGMA_OMP_CLAUSE_ORDERED,
216 PRAGMA_OMP_CLAUSE_PRIVATE,
217 PRAGMA_OMP_CLAUSE_REDUCTION,
218 PRAGMA_OMP_CLAUSE_SCHEDULE,
219 PRAGMA_OMP_CLAUSE_SHARED
223 /* Initialization routine for this file. */
228 /* The only initialization required is of the reserved word
232 int mask = (flag_isoc99 ? 0 : D_C89)
233 | (flag_no_asm ? (flag_isoc99 ? D_EXT : D_EXT|D_EXT89) : 0);
235 if (!c_dialect_objc ())
238 ridpointers = GGC_CNEWVEC (tree, (int) RID_MAX);
239 for (i = 0; i < N_reswords; i++)
241 /* If a keyword is disabled, do not enter it into the table
242 and so create a canonical spelling that isn't a keyword. */
243 if (reswords[i].disable & mask)
246 id = get_identifier (reswords[i].word);
247 C_RID_CODE (id) = reswords[i].rid;
248 C_IS_RESERVED_WORD (id) = 1;
249 ridpointers [(int) reswords[i].rid] = id;
253 /* The C lexer intermediates between the lexer in cpplib and c-lex.c
254 and the C parser. Unlike the C++ lexer, the parser structure
255 stores the lexer information instead of using a separate structure.
256 Identifiers are separated into ordinary identifiers, type names,
257 keywords and some other Objective-C types of identifiers, and some
258 look-ahead is maintained.
260 ??? It might be a good idea to lex the whole file up front (as for
261 C++). It would then be possible to share more of the C and C++
262 lexer code, if desired. */
264 /* The following local token type is used. */
267 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
269 /* More information about the type of a CPP_NAME token. */
270 typedef enum c_id_kind {
271 /* An ordinary identifier. */
273 /* An identifier declared as a typedef name. */
275 /* An identifier declared as an Objective-C class name. */
277 /* Not an identifier. */
281 /* A single C token after string literal concatenation and conversion
282 of preprocessing tokens to tokens. */
283 typedef struct c_token GTY (())
285 /* The kind of token. */
286 ENUM_BITFIELD (cpp_ttype) type : 8;
287 /* If this token is a CPP_NAME, this value indicates whether also
288 declared as some kind of type. Otherwise, it is C_ID_NONE. */
289 ENUM_BITFIELD (c_id_kind) id_kind : 8;
290 /* If this token is a keyword, this value indicates which keyword.
291 Otherwise, this value is RID_MAX. */
292 ENUM_BITFIELD (rid) keyword : 8;
293 /* If this token is a CPP_PRAGMA, this indicates the pragma that
294 was seen. Otherwise it is PRAGMA_NONE. */
295 ENUM_BITFIELD (pragma_kind) pragma_kind : 7;
296 /* True if this token is from a system header. */
297 BOOL_BITFIELD in_system_header : 1;
298 /* The value associated with this token, if any. */
300 /* The location at which this token was found. */
304 /* A parser structure recording information about the state and
305 context of parsing. Includes lexer information with up to two
306 tokens of look-ahead; more are not needed for C. */
307 typedef struct c_parser GTY(())
309 /* The look-ahead tokens. */
311 /* How many look-ahead tokens are available (0, 1 or 2). */
313 /* True if a syntax error is being recovered from; false otherwise.
314 c_parser_error sets this flag. It should clear this flag when
315 enough tokens have been consumed to recover from the error. */
316 BOOL_BITFIELD error : 1;
317 /* True if we're processing a pragma, and shouldn't automatically
318 consume CPP_PRAGMA_EOL. */
319 BOOL_BITFIELD in_pragma : 1;
323 /* The actual parser and external interface. ??? Does this need to be
324 garbage-collected? */
326 static GTY (()) c_parser *the_parser;
329 /* Read in and lex a single token, storing it in *TOKEN. */
332 c_lex_one_token (c_token *token)
334 timevar_push (TV_LEX);
336 token->type = c_lex_with_flags (&token->value, &token->location, NULL);
337 token->id_kind = C_ID_NONE;
338 token->keyword = RID_MAX;
339 token->pragma_kind = PRAGMA_NONE;
340 token->in_system_header = in_system_header;
348 int objc_force_identifier = objc_need_raw_identifier;
349 OBJC_NEED_RAW_IDENTIFIER (0);
351 if (C_IS_RESERVED_WORD (token->value))
353 enum rid rid_code = C_RID_CODE (token->value);
355 if (c_dialect_objc ())
357 if (!OBJC_IS_AT_KEYWORD (rid_code)
358 && (!OBJC_IS_PQ_KEYWORD (rid_code) || objc_pq_context))
360 /* Return the canonical spelling for this keyword. */
361 token->value = ridpointers[(int) rid_code];
362 token->type = CPP_KEYWORD;
363 token->keyword = rid_code;
369 /* Return the canonical spelling for this keyword. */
370 token->value = ridpointers[(int) rid_code];
371 token->type = CPP_KEYWORD;
372 token->keyword = rid_code;
377 decl = lookup_name (token->value);
380 if (TREE_CODE (decl) == TYPE_DECL)
382 token->id_kind = C_ID_TYPENAME;
386 else if (c_dialect_objc ())
388 tree objc_interface_decl = objc_is_class_name (token->value);
389 /* Objective-C class names are in the same namespace as
390 variables and typedefs, and hence are shadowed by local
392 if (objc_interface_decl
393 && (global_bindings_p ()
394 || (!objc_force_identifier && !decl)))
396 token->value = objc_interface_decl;
397 token->id_kind = C_ID_CLASSNAME;
401 token->id_kind = C_ID_ID;
405 /* This only happens in Objective-C; it must be a keyword. */
406 token->type = CPP_KEYWORD;
407 token->keyword = C_RID_CODE (token->value);
411 case CPP_CLOSE_PAREN:
413 /* These tokens may affect the interpretation of any identifiers
414 following, if doing Objective-C. */
415 OBJC_NEED_RAW_IDENTIFIER (0);
418 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
419 token->pragma_kind = TREE_INT_CST_LOW (token->value);
425 timevar_pop (TV_LEX);
428 /* Return a pointer to the next token from PARSER, reading it in if
431 static inline c_token *
432 c_parser_peek_token (c_parser *parser)
434 if (parser->tokens_avail == 0)
436 c_lex_one_token (&parser->tokens[0]);
437 parser->tokens_avail = 1;
439 return &parser->tokens[0];
442 /* Return true if the next token from PARSER has the indicated
446 c_parser_next_token_is (c_parser *parser, enum cpp_ttype type)
448 return c_parser_peek_token (parser)->type == type;
451 /* Return true if the next token from PARSER does not have the
455 c_parser_next_token_is_not (c_parser *parser, enum cpp_ttype type)
457 return !c_parser_next_token_is (parser, type);
460 /* Return true if the next token from PARSER is the indicated
464 c_parser_next_token_is_keyword (c_parser *parser, enum rid keyword)
468 /* Peek at the next token. */
469 token = c_parser_peek_token (parser);
470 /* Check to see if it is the indicated keyword. */
471 return token->keyword == keyword;
474 /* Return true if TOKEN can start a type name,
477 c_token_starts_typename (c_token *token)
482 switch (token->id_kind)
489 gcc_assert (c_dialect_objc ());
495 switch (token->keyword)
524 if (c_dialect_objc ())
532 /* Return true if the next token from PARSER can start a type name,
535 c_parser_next_token_starts_typename (c_parser *parser)
537 c_token *token = c_parser_peek_token (parser);
538 return c_token_starts_typename (token);
541 /* Return true if TOKEN can start declaration specifiers, false
544 c_token_starts_declspecs (c_token *token)
549 switch (token->id_kind)
556 gcc_assert (c_dialect_objc ());
562 switch (token->keyword)
598 if (c_dialect_objc ())
606 /* Return true if the next token from PARSER can start declaration
607 specifiers, false otherwise. */
609 c_parser_next_token_starts_declspecs (c_parser *parser)
611 c_token *token = c_parser_peek_token (parser);
612 return c_token_starts_declspecs (token);
615 /* Return a pointer to the next-but-one token from PARSER, reading it
616 in if necessary. The next token is already read in. */
619 c_parser_peek_2nd_token (c_parser *parser)
621 if (parser->tokens_avail >= 2)
622 return &parser->tokens[1];
623 gcc_assert (parser->tokens_avail == 1);
624 gcc_assert (parser->tokens[0].type != CPP_EOF);
625 gcc_assert (parser->tokens[0].type != CPP_PRAGMA_EOL);
626 c_lex_one_token (&parser->tokens[1]);
627 parser->tokens_avail = 2;
628 return &parser->tokens[1];
631 /* Consume the next token from PARSER. */
634 c_parser_consume_token (c_parser *parser)
636 gcc_assert (parser->tokens_avail >= 1);
637 gcc_assert (parser->tokens[0].type != CPP_EOF);
638 gcc_assert (!parser->in_pragma || parser->tokens[0].type != CPP_PRAGMA_EOL);
639 gcc_assert (parser->error || parser->tokens[0].type != CPP_PRAGMA);
640 if (parser->tokens_avail == 2)
641 parser->tokens[0] = parser->tokens[1];
642 parser->tokens_avail--;
645 /* Expect the current token to be a #pragma. Consume it and remember
646 that we've begun parsing a pragma. */
649 c_parser_consume_pragma (c_parser *parser)
651 gcc_assert (!parser->in_pragma);
652 gcc_assert (parser->tokens_avail >= 1);
653 gcc_assert (parser->tokens[0].type == CPP_PRAGMA);
654 if (parser->tokens_avail == 2)
655 parser->tokens[0] = parser->tokens[1];
656 parser->tokens_avail--;
657 parser->in_pragma = true;
660 /* Update the globals input_location and in_system_header from
663 c_parser_set_source_position_from_token (c_token *token)
665 if (token->type != CPP_EOF)
667 input_location = token->location;
668 in_system_header = token->in_system_header;
672 /* Issue a diagnostic of the form
673 FILE:LINE: MESSAGE before TOKEN
674 where TOKEN is the next token in the input stream of PARSER.
675 MESSAGE (specified by the caller) is usually of the form "expected
678 Do not issue a diagnostic if still recovering from an error.
680 ??? This is taken from the C++ parser, but building up messages in
681 this way is not i18n-friendly and some other approach should be
685 c_parser_error (c_parser *parser, const char *gmsgid)
687 c_token *token = c_parser_peek_token (parser);
690 parser->error = true;
693 /* This diagnostic makes more sense if it is tagged to the line of
694 the token we just peeked at. */
695 c_parser_set_source_position_from_token (token);
696 c_parse_error (gmsgid,
697 /* Because c_parse_error does not understand
698 CPP_KEYWORD, keywords are treated like
700 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
704 /* If the next token is of the indicated TYPE, consume it. Otherwise,
705 issue the error MSGID. If MSGID is NULL then a message has already
706 been produced and no message will be produced this time. Returns
707 true if found, false otherwise. */
710 c_parser_require (c_parser *parser,
714 if (c_parser_next_token_is (parser, type))
716 c_parser_consume_token (parser);
721 c_parser_error (parser, msgid);
726 /* If the next token is the indicated keyword, consume it. Otherwise,
727 issue the error MSGID. Returns true if found, false otherwise. */
730 c_parser_require_keyword (c_parser *parser,
734 if (c_parser_next_token_is_keyword (parser, keyword))
736 c_parser_consume_token (parser);
741 c_parser_error (parser, msgid);
746 /* Like c_parser_require, except that tokens will be skipped until the
747 desired token is found. An error message is still produced if the
748 next token is not as expected. If MSGID is NULL then a message has
749 already been produced and no message will be produced this
753 c_parser_skip_until_found (c_parser *parser,
757 unsigned nesting_depth = 0;
759 if (c_parser_require (parser, type, msgid))
762 /* Skip tokens until the desired token is found. */
765 /* Peek at the next token. */
766 c_token *token = c_parser_peek_token (parser);
767 /* If we've reached the token we want, consume it and stop. */
768 if (token->type == type && !nesting_depth)
770 c_parser_consume_token (parser);
774 /* If we've run out of tokens, stop. */
775 if (token->type == CPP_EOF)
777 if (token->type == CPP_PRAGMA_EOL && parser->in_pragma)
779 if (token->type == CPP_OPEN_BRACE
780 || token->type == CPP_OPEN_PAREN
781 || token->type == CPP_OPEN_SQUARE)
783 else if (token->type == CPP_CLOSE_BRACE
784 || token->type == CPP_CLOSE_PAREN
785 || token->type == CPP_CLOSE_SQUARE)
787 if (nesting_depth-- == 0)
790 /* Consume this token. */
791 c_parser_consume_token (parser);
793 parser->error = false;
796 /* Skip tokens until the end of a parameter is found, but do not
797 consume the comma, semicolon or closing delimiter. */
800 c_parser_skip_to_end_of_parameter (c_parser *parser)
802 unsigned nesting_depth = 0;
806 c_token *token = c_parser_peek_token (parser);
807 if ((token->type == CPP_COMMA || token->type == CPP_SEMICOLON)
810 /* If we've run out of tokens, stop. */
811 if (token->type == CPP_EOF)
813 if (token->type == CPP_PRAGMA_EOL && parser->in_pragma)
815 if (token->type == CPP_OPEN_BRACE
816 || token->type == CPP_OPEN_PAREN
817 || token->type == CPP_OPEN_SQUARE)
819 else if (token->type == CPP_CLOSE_BRACE
820 || token->type == CPP_CLOSE_PAREN
821 || token->type == CPP_CLOSE_SQUARE)
823 if (nesting_depth-- == 0)
826 /* Consume this token. */
827 c_parser_consume_token (parser);
829 parser->error = false;
832 /* Skip tokens until we have consumed an entire block, or until we
833 have consumed a non-nested ';'. */
836 c_parser_skip_to_end_of_block_or_statement (c_parser *parser)
838 unsigned nesting_depth = 0;
844 /* Peek at the next token. */
845 token = c_parser_peek_token (parser);
846 /* If we've run out of tokens, stop. */
847 if (token->type == CPP_EOF)
849 if (token->type == CPP_PRAGMA_EOL && parser->in_pragma)
851 /* If the next token is a ';', we have reached the end of the
853 if (token->type == CPP_SEMICOLON && !nesting_depth)
855 /* Consume the ';'. */
856 c_parser_consume_token (parser);
859 /* If the next token is a non-nested '}', then we have reached
860 the end of the current block. */
861 if (token->type == CPP_CLOSE_BRACE
862 && (nesting_depth == 0 || --nesting_depth == 0))
864 c_parser_consume_token (parser);
867 /* If it the next token is a '{', then we are entering a new
868 block. Consume the entire block. */
869 if (token->type == CPP_OPEN_BRACE)
871 c_parser_consume_token (parser);
873 parser->error = false;
876 /* Expect to be at the end of the pragma directive and consume an
877 end of line marker. */
880 c_parser_skip_to_pragma_eol (c_parser *parser)
882 gcc_assert (parser->in_pragma);
883 parser->in_pragma = false;
885 if (!c_parser_require (parser, CPP_PRAGMA_EOL, "expected end of line"))
888 c_token *token = c_parser_peek_token (parser);
889 if (token->type == CPP_EOF)
891 if (token->type == CPP_PRAGMA_EOL)
893 c_parser_consume_token (parser);
896 c_parser_consume_token (parser);
899 parser->error = false;
902 /* Save the warning flags which are controlled by __extension__. */
905 disable_extension_diagnostics (void)
908 | (warn_pointer_arith << 1)
909 | (warn_traditional << 2)
912 warn_pointer_arith = 0;
913 warn_traditional = 0;
918 /* Restore the warning flags which are controlled by __extension__.
919 FLAGS is the return value from disable_extension_diagnostics. */
922 restore_extension_diagnostics (int flags)
924 pedantic = flags & 1;
925 warn_pointer_arith = (flags >> 1) & 1;
926 warn_traditional = (flags >> 2) & 1;
927 flag_iso = (flags >> 3) & 1;
930 /* Possibly kinds of declarator to parse. */
931 typedef enum c_dtr_syn {
932 /* A normal declarator with an identifier. */
934 /* An abstract declarator (maybe empty). */
936 /* A parameter declarator: may be either, but after a type name does
937 not redeclare a typedef name as an identifier if it can
938 alternatively be interpreted as a typedef name; see DR#009,
939 applied in C90 TC1, omitted from C99 and reapplied in C99 TC2
940 following DR#249. For example, given a typedef T, "int T" and
941 "int *T" are valid parameter declarations redeclaring T, while
942 "int (T)" and "int * (T)" and "int (T[])" and "int (T (int))" are
943 abstract declarators rather than involving redundant parentheses;
944 the same applies with attributes inside the parentheses before
949 static void c_parser_external_declaration (c_parser *);
950 static void c_parser_asm_definition (c_parser *);
951 static void c_parser_declaration_or_fndef (c_parser *, bool, bool, bool, bool);
952 static void c_parser_declspecs (c_parser *, struct c_declspecs *, bool, bool,
954 static struct c_typespec c_parser_enum_specifier (c_parser *);
955 static struct c_typespec c_parser_struct_or_union_specifier (c_parser *);
956 static tree c_parser_struct_declaration (c_parser *);
957 static struct c_typespec c_parser_typeof_specifier (c_parser *);
958 static struct c_declarator *c_parser_declarator (c_parser *, bool, c_dtr_syn,
960 static struct c_declarator *c_parser_direct_declarator (c_parser *, bool,
962 static struct c_declarator *c_parser_direct_declarator_inner (c_parser *,
964 struct c_declarator *);
965 static struct c_arg_info *c_parser_parms_declarator (c_parser *, bool, tree);
966 static struct c_arg_info *c_parser_parms_list_declarator (c_parser *, tree);
967 static struct c_parm *c_parser_parameter_declaration (c_parser *, tree);
968 static tree c_parser_simple_asm_expr (c_parser *);
969 static tree c_parser_attributes (c_parser *);
970 static struct c_type_name *c_parser_type_name (c_parser *);
971 static struct c_expr c_parser_initializer (c_parser *);
972 static struct c_expr c_parser_braced_init (c_parser *, tree, bool);
973 static void c_parser_initelt (c_parser *);
974 static void c_parser_initval (c_parser *, struct c_expr *);
975 static tree c_parser_compound_statement (c_parser *);
976 static void c_parser_compound_statement_nostart (c_parser *);
977 static void c_parser_label (c_parser *);
978 static void c_parser_statement (c_parser *);
979 static void c_parser_statement_after_labels (c_parser *);
980 static void c_parser_if_statement (c_parser *);
981 static void c_parser_switch_statement (c_parser *);
982 static void c_parser_while_statement (c_parser *);
983 static void c_parser_do_statement (c_parser *);
984 static void c_parser_for_statement (c_parser *);
985 static tree c_parser_asm_statement (c_parser *);
986 static tree c_parser_asm_operands (c_parser *, bool);
987 static tree c_parser_asm_clobbers (c_parser *);
988 static struct c_expr c_parser_expr_no_commas (c_parser *, struct c_expr *);
989 static struct c_expr c_parser_conditional_expression (c_parser *,
991 static struct c_expr c_parser_binary_expression (c_parser *, struct c_expr *);
992 static struct c_expr c_parser_cast_expression (c_parser *, struct c_expr *);
993 static struct c_expr c_parser_unary_expression (c_parser *);
994 static struct c_expr c_parser_sizeof_expression (c_parser *);
995 static struct c_expr c_parser_alignof_expression (c_parser *);
996 static struct c_expr c_parser_postfix_expression (c_parser *);
997 static struct c_expr c_parser_postfix_expression_after_paren_type (c_parser *,
998 struct c_type_name *);
999 static struct c_expr c_parser_postfix_expression_after_primary (c_parser *,
1001 static struct c_expr c_parser_expression (c_parser *);
1002 static struct c_expr c_parser_expression_conv (c_parser *);
1003 static tree c_parser_expr_list (c_parser *, bool);
1004 static void c_parser_omp_construct (c_parser *);
1005 static void c_parser_omp_threadprivate (c_parser *);
1006 static void c_parser_omp_barrier (c_parser *);
1007 static void c_parser_omp_flush (c_parser *);
1009 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
1010 static bool c_parser_pragma (c_parser *, enum pragma_context);
1012 /* These Objective-C parser functions are only ever called when
1013 compiling Objective-C. */
1014 static void c_parser_objc_class_definition (c_parser *);
1015 static void c_parser_objc_class_instance_variables (c_parser *);
1016 static void c_parser_objc_class_declaration (c_parser *);
1017 static void c_parser_objc_alias_declaration (c_parser *);
1018 static void c_parser_objc_protocol_definition (c_parser *);
1019 static enum tree_code c_parser_objc_method_type (c_parser *);
1020 static void c_parser_objc_method_definition (c_parser *);
1021 static void c_parser_objc_methodprotolist (c_parser *);
1022 static void c_parser_objc_methodproto (c_parser *);
1023 static tree c_parser_objc_method_decl (c_parser *);
1024 static tree c_parser_objc_type_name (c_parser *);
1025 static tree c_parser_objc_protocol_refs (c_parser *);
1026 static void c_parser_objc_try_catch_statement (c_parser *);
1027 static void c_parser_objc_synchronized_statement (c_parser *);
1028 static tree c_parser_objc_selector (c_parser *);
1029 static tree c_parser_objc_selector_arg (c_parser *);
1030 static tree c_parser_objc_receiver (c_parser *);
1031 static tree c_parser_objc_message_args (c_parser *);
1032 static tree c_parser_objc_keywordexpr (c_parser *);
1034 /* Parse a translation unit (C90 6.7, C99 6.9).
1037 external-declarations
1039 external-declarations:
1040 external-declaration
1041 external-declarations external-declaration
1050 c_parser_translation_unit (c_parser *parser)
1052 if (c_parser_next_token_is (parser, CPP_EOF))
1055 pedwarn ("ISO C forbids an empty source file");
1059 void *obstack_position = obstack_alloc (&parser_obstack, 0);
1063 c_parser_external_declaration (parser);
1064 obstack_free (&parser_obstack, obstack_position);
1066 while (c_parser_next_token_is_not (parser, CPP_EOF));
1070 /* Parse an external declaration (C90 6.7, C99 6.9).
1072 external-declaration:
1078 external-declaration:
1081 __extension__ external-declaration
1085 external-declaration:
1086 objc-class-definition
1087 objc-class-declaration
1088 objc-alias-declaration
1089 objc-protocol-definition
1090 objc-method-definition
1095 c_parser_external_declaration (c_parser *parser)
1098 switch (c_parser_peek_token (parser)->type)
1101 switch (c_parser_peek_token (parser)->keyword)
1104 ext = disable_extension_diagnostics ();
1105 c_parser_consume_token (parser);
1106 c_parser_external_declaration (parser);
1107 restore_extension_diagnostics (ext);
1110 c_parser_asm_definition (parser);
1112 case RID_AT_INTERFACE:
1113 case RID_AT_IMPLEMENTATION:
1114 gcc_assert (c_dialect_objc ());
1115 c_parser_objc_class_definition (parser);
1118 gcc_assert (c_dialect_objc ());
1119 c_parser_objc_class_declaration (parser);
1122 gcc_assert (c_dialect_objc ());
1123 c_parser_objc_alias_declaration (parser);
1125 case RID_AT_PROTOCOL:
1126 gcc_assert (c_dialect_objc ());
1127 c_parser_objc_protocol_definition (parser);
1130 gcc_assert (c_dialect_objc ());
1131 c_parser_consume_token (parser);
1132 objc_finish_implementation ();
1140 pedwarn ("ISO C does not allow extra %<;%> outside of a function");
1141 c_parser_consume_token (parser);
1144 c_parser_pragma (parser, pragma_external);
1148 if (c_dialect_objc ())
1150 c_parser_objc_method_definition (parser);
1153 /* Else fall through, and yield a syntax error trying to parse
1154 as a declaration or function definition. */
1157 /* A declaration or a function definition. We can only tell
1158 which after parsing the declaration specifiers, if any, and
1159 the first declarator. */
1160 c_parser_declaration_or_fndef (parser, true, true, false, true);
1166 /* Parse a declaration or function definition (C90 6.5, 6.7.1, C99
1167 6.7, 6.9.1). If FNDEF_OK is true, a function definition is
1168 accepted; otherwise (old-style parameter declarations) only other
1169 declarations are accepted. If NESTED is true, we are inside a
1170 function or parsing old-style parameter declarations; any functions
1171 encountered are nested functions and declaration specifiers are
1172 required; otherwise we are at top level and functions are normal
1173 functions and declaration specifiers may be optional. If EMPTY_OK
1174 is true, empty declarations are OK (subject to all other
1175 constraints); otherwise (old-style parameter declarations) they are
1176 diagnosed. If START_ATTR_OK is true, the declaration specifiers
1177 may start with attributes; otherwise they may not.
1180 declaration-specifiers init-declarator-list[opt] ;
1182 function-definition:
1183 declaration-specifiers[opt] declarator declaration-list[opt]
1188 declaration-list declaration
1190 init-declarator-list:
1192 init-declarator-list , init-declarator
1195 declarator simple-asm-expr[opt] attributes[opt]
1196 declarator simple-asm-expr[opt] attributes[opt] = initializer
1200 nested-function-definition:
1201 declaration-specifiers declarator declaration-list[opt]
1204 The simple-asm-expr and attributes are GNU extensions.
1206 This function does not handle __extension__; that is handled in its
1207 callers. ??? Following the old parser, __extension__ may start
1208 external declarations, declarations in functions and declarations
1209 at the start of "for" loops, but not old-style parameter
1212 C99 requires declaration specifiers in a function definition; the
1213 absence is diagnosed through the diagnosis of implicit int. In GNU
1214 C we also allow but diagnose declarations without declaration
1215 specifiers, but only at top level (elsewhere they conflict with
1221 threadprivate-directive */
1224 c_parser_declaration_or_fndef (c_parser *parser, bool fndef_ok, bool empty_ok,
1225 bool nested, bool start_attr_ok)
1227 struct c_declspecs *specs;
1229 tree all_prefix_attrs;
1230 bool diagnosed_no_specs = false;
1232 specs = build_null_declspecs ();
1233 c_parser_declspecs (parser, specs, true, true, start_attr_ok);
1236 c_parser_skip_to_end_of_block_or_statement (parser);
1239 if (nested && !specs->declspecs_seen_p)
1241 c_parser_error (parser, "expected declaration specifiers");
1242 c_parser_skip_to_end_of_block_or_statement (parser);
1245 finish_declspecs (specs);
1246 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1252 shadow_tag_warned (specs, 1);
1253 pedwarn ("empty declaration");
1255 c_parser_consume_token (parser);
1258 pending_xref_error ();
1259 prefix_attrs = specs->attrs;
1260 all_prefix_attrs = prefix_attrs;
1261 specs->attrs = NULL_TREE;
1264 struct c_declarator *declarator;
1267 /* Declaring either one or more declarators (in which case we
1268 should diagnose if there were no declaration specifiers) or a
1269 function definition (in which case the diagnostic for
1270 implicit int suffices). */
1271 declarator = c_parser_declarator (parser, specs->type_seen_p,
1272 C_DTR_NORMAL, &dummy);
1273 if (declarator == NULL)
1275 c_parser_skip_to_end_of_block_or_statement (parser);
1278 if (c_parser_next_token_is (parser, CPP_EQ)
1279 || c_parser_next_token_is (parser, CPP_COMMA)
1280 || c_parser_next_token_is (parser, CPP_SEMICOLON)
1281 || c_parser_next_token_is_keyword (parser, RID_ASM)
1282 || c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
1284 tree asm_name = NULL_TREE;
1285 tree postfix_attrs = NULL_TREE;
1286 if (!diagnosed_no_specs && !specs->declspecs_seen_p)
1288 diagnosed_no_specs = true;
1289 pedwarn ("data definition has no type or storage class");
1291 /* Having seen a data definition, there cannot now be a
1292 function definition. */
1294 if (c_parser_next_token_is_keyword (parser, RID_ASM))
1295 asm_name = c_parser_simple_asm_expr (parser);
1296 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
1297 postfix_attrs = c_parser_attributes (parser);
1298 if (c_parser_next_token_is (parser, CPP_EQ))
1302 c_parser_consume_token (parser);
1303 /* The declaration of the variable is in effect while
1304 its initializer is parsed. */
1305 d = start_decl (declarator, specs, true,
1306 chainon (postfix_attrs, all_prefix_attrs));
1308 d = error_mark_node;
1309 start_init (d, asm_name, global_bindings_p ());
1310 init = c_parser_initializer (parser);
1312 if (d != error_mark_node)
1314 maybe_warn_string_init (TREE_TYPE (d), init);
1315 finish_decl (d, init.value, asm_name);
1320 tree d = start_decl (declarator, specs, false,
1321 chainon (postfix_attrs,
1324 finish_decl (d, NULL_TREE, asm_name);
1326 if (c_parser_next_token_is (parser, CPP_COMMA))
1328 c_parser_consume_token (parser);
1329 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
1330 all_prefix_attrs = chainon (c_parser_attributes (parser),
1333 all_prefix_attrs = prefix_attrs;
1336 else if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1338 c_parser_consume_token (parser);
1343 c_parser_error (parser, "expected %<,%> or %<;%>");
1344 c_parser_skip_to_end_of_block_or_statement (parser);
1350 c_parser_error (parser, "expected %<=%>, %<,%>, %<;%>, "
1351 "%<asm%> or %<__attribute__%>");
1352 c_parser_skip_to_end_of_block_or_statement (parser);
1355 /* Function definition (nested or otherwise). */
1359 pedwarn ("ISO C forbids nested functions");
1360 push_function_context ();
1362 if (!start_function (specs, declarator, all_prefix_attrs))
1364 /* This can appear in many cases looking nothing like a
1365 function definition, so we don't give a more specific
1366 error suggesting there was one. */
1367 c_parser_error (parser, "expected %<=%>, %<,%>, %<;%>, %<asm%> "
1368 "or %<__attribute__%>");
1370 pop_function_context ();
1373 /* Parse old-style parameter declarations. ??? Attributes are
1374 not allowed to start declaration specifiers here because of a
1375 syntax conflict between a function declaration with attribute
1376 suffix and a function definition with an attribute prefix on
1377 first old-style parameter declaration. Following the old
1378 parser, they are not accepted on subsequent old-style
1379 parameter declarations either. However, there is no
1380 ambiguity after the first declaration, nor indeed on the
1381 first as long as we don't allow postfix attributes after a
1382 declarator with a nonempty identifier list in a definition;
1383 and postfix attributes have never been accepted here in
1384 function definitions either. */
1385 while (c_parser_next_token_is_not (parser, CPP_EOF)
1386 && c_parser_next_token_is_not (parser, CPP_OPEN_BRACE))
1387 c_parser_declaration_or_fndef (parser, false, false, true, false);
1388 DECL_SOURCE_LOCATION (current_function_decl)
1389 = c_parser_peek_token (parser)->location;
1390 store_parm_decls ();
1391 fnbody = c_parser_compound_statement (parser);
1394 tree decl = current_function_decl;
1397 pop_function_context ();
1398 add_stmt (build_stmt (DECL_EXPR, decl));
1409 /* Parse an asm-definition (asm() outside a function body). This is a
1417 c_parser_asm_definition (c_parser *parser)
1419 tree asm_str = c_parser_simple_asm_expr (parser);
1421 cgraph_add_asm_node (asm_str);
1422 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
1425 /* Parse some declaration specifiers (possibly none) (C90 6.5, C99
1426 6.7), adding them to SPECS (which may already include some).
1427 Storage class specifiers are accepted iff SCSPEC_OK; type
1428 specifiers are accepted iff TYPESPEC_OK; attributes are accepted at
1429 the start iff START_ATTR_OK.
1431 declaration-specifiers:
1432 storage-class-specifier declaration-specifiers[opt]
1433 type-specifier declaration-specifiers[opt]
1434 type-qualifier declaration-specifiers[opt]
1435 function-specifier declaration-specifiers[opt]
1437 Function specifiers (inline) are from C99, and are currently
1438 handled as storage class specifiers, as is __thread.
1440 C90 6.5.1, C99 6.7.1:
1441 storage-class-specifier:
1452 C90 6.5.2, C99 6.7.2:
1465 [_Imaginary removed in C99 TC2]
1466 struct-or-union-specifier
1470 (_Bool and _Complex are new in C99.)
1472 C90 6.5.3, C99 6.7.3:
1479 (restrict is new in C99.)
1483 declaration-specifiers:
1484 attributes declaration-specifiers[opt]
1486 storage-class-specifier:
1498 class-name objc-protocol-refs[opt]
1499 typedef-name objc-protocol-refs
1504 c_parser_declspecs (c_parser *parser, struct c_declspecs *specs,
1505 bool scspec_ok, bool typespec_ok, bool start_attr_ok)
1507 bool attrs_ok = start_attr_ok;
1508 bool seen_type = specs->type_seen_p;
1509 while (c_parser_next_token_is (parser, CPP_NAME)
1510 || c_parser_next_token_is (parser, CPP_KEYWORD)
1511 || (c_dialect_objc () && c_parser_next_token_is (parser, CPP_LESS)))
1513 struct c_typespec t;
1515 if (c_parser_next_token_is (parser, CPP_NAME))
1517 tree value = c_parser_peek_token (parser)->value;
1518 c_id_kind kind = c_parser_peek_token (parser)->id_kind;
1519 /* This finishes the specifiers unless a type name is OK, it
1520 is declared as a type name and a type name hasn't yet
1522 if (!typespec_ok || seen_type
1523 || (kind != C_ID_TYPENAME && kind != C_ID_CLASSNAME))
1525 c_parser_consume_token (parser);
1528 if (kind == C_ID_TYPENAME
1529 && (!c_dialect_objc ()
1530 || c_parser_next_token_is_not (parser, CPP_LESS)))
1532 t.kind = ctsk_typedef;
1533 /* For a typedef name, record the meaning, not the name.
1534 In case of 'foo foo, bar;'. */
1535 t.spec = lookup_name (value);
1539 tree proto = NULL_TREE;
1540 gcc_assert (c_dialect_objc ());
1542 if (c_parser_next_token_is (parser, CPP_LESS))
1543 proto = c_parser_objc_protocol_refs (parser);
1544 t.spec = objc_get_protocol_qualified_type (value, proto);
1546 declspecs_add_type (specs, t);
1549 if (c_parser_next_token_is (parser, CPP_LESS))
1551 /* Make "<SomeProtocol>" equivalent to "id <SomeProtocol>" -
1552 nisse@lysator.liu.se. */
1554 gcc_assert (c_dialect_objc ());
1555 if (!typespec_ok || seen_type)
1557 proto = c_parser_objc_protocol_refs (parser);
1559 t.spec = objc_get_protocol_qualified_type (NULL_TREE, proto);
1560 declspecs_add_type (specs, t);
1563 gcc_assert (c_parser_next_token_is (parser, CPP_KEYWORD));
1564 switch (c_parser_peek_token (parser)->keyword)
1576 /* TODO: Distinguish between function specifiers (inline)
1577 and storage class specifiers, either here or in
1578 declspecs_add_scspec. */
1579 declspecs_add_scspec (specs, c_parser_peek_token (parser)->value);
1580 c_parser_consume_token (parser);
1600 OBJC_NEED_RAW_IDENTIFIER (1);
1601 t.kind = ctsk_resword;
1602 t.spec = c_parser_peek_token (parser)->value;
1603 declspecs_add_type (specs, t);
1604 c_parser_consume_token (parser);
1611 t = c_parser_enum_specifier (parser);
1612 declspecs_add_type (specs, t);
1620 t = c_parser_struct_or_union_specifier (parser);
1621 declspecs_add_type (specs, t);
1624 /* ??? The old parser rejected typeof after other type
1625 specifiers, but is a syntax error the best way of
1627 if (!typespec_ok || seen_type)
1631 t = c_parser_typeof_specifier (parser);
1632 declspecs_add_type (specs, t);
1638 declspecs_add_qual (specs, c_parser_peek_token (parser)->value);
1639 c_parser_consume_token (parser);
1644 attrs = c_parser_attributes (parser);
1645 declspecs_add_attrs (specs, attrs);
1654 /* Parse an enum specifier (C90 6.5.2.2, C99 6.7.2.2).
1657 enum attributes[opt] identifier[opt] { enumerator-list } attributes[opt]
1658 enum attributes[opt] identifier[opt] { enumerator-list , } attributes[opt]
1659 enum attributes[opt] identifier
1661 The form with trailing comma is new in C99. The forms with
1662 attributes are GNU extensions. In GNU C, we accept any expression
1663 without commas in the syntax (assignment expressions, not just
1664 conditional expressions); assignment expressions will be diagnosed
1669 enumerator-list , enumerator
1672 enumeration-constant
1673 enumeration-constant = constant-expression
1676 static struct c_typespec
1677 c_parser_enum_specifier (c_parser *parser)
1679 struct c_typespec ret;
1681 tree ident = NULL_TREE;
1682 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ENUM));
1683 c_parser_consume_token (parser);
1684 attrs = c_parser_attributes (parser);
1685 if (c_parser_next_token_is (parser, CPP_NAME))
1687 ident = c_parser_peek_token (parser)->value;
1688 c_parser_consume_token (parser);
1690 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
1692 /* Parse an enum definition. */
1693 tree type = start_enum (ident);
1695 /* We chain the enumerators in reverse order, then put them in
1696 forward order at the end. */
1697 tree values = NULL_TREE;
1698 c_parser_consume_token (parser);
1705 if (c_parser_next_token_is_not (parser, CPP_NAME))
1707 c_parser_error (parser, "expected identifier");
1708 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
1709 values = error_mark_node;
1712 enum_id = c_parser_peek_token (parser)->value;
1713 c_parser_consume_token (parser);
1714 if (c_parser_next_token_is (parser, CPP_EQ))
1716 c_parser_consume_token (parser);
1717 enum_value = c_parser_expr_no_commas (parser, NULL).value;
1720 enum_value = NULL_TREE;
1721 enum_decl = build_enumerator (enum_id, enum_value);
1722 TREE_CHAIN (enum_decl) = values;
1725 if (c_parser_next_token_is (parser, CPP_COMMA))
1728 c_parser_consume_token (parser);
1730 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
1732 if (seen_comma && pedantic && !flag_isoc99)
1733 pedwarn ("comma at end of enumerator list");
1734 c_parser_consume_token (parser);
1739 c_parser_error (parser, "expected %<,%> or %<}%>");
1740 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
1741 values = error_mark_node;
1745 postfix_attrs = c_parser_attributes (parser);
1746 ret.spec = finish_enum (type, nreverse (values),
1747 chainon (attrs, postfix_attrs));
1748 ret.kind = ctsk_tagdef;
1753 c_parser_error (parser, "expected %<{%>");
1754 ret.spec = error_mark_node;
1755 ret.kind = ctsk_tagref;
1758 ret = parser_xref_tag (ENUMERAL_TYPE, ident);
1759 /* In ISO C, enumerated types can be referred to only if already
1761 if (pedantic && !COMPLETE_TYPE_P (ret.spec))
1762 pedwarn ("ISO C forbids forward references to %<enum%> types");
1766 /* Parse a struct or union specifier (C90 6.5.2.1, C99 6.7.2.1).
1768 struct-or-union-specifier:
1769 struct-or-union attributes[opt] identifier[opt]
1770 { struct-contents } attributes[opt]
1771 struct-or-union attributes[opt] identifier
1774 struct-declaration-list
1776 struct-declaration-list:
1777 struct-declaration ;
1778 struct-declaration-list struct-declaration ;
1785 struct-declaration-list struct-declaration
1787 struct-declaration-list:
1788 struct-declaration-list ;
1791 (Note that in the syntax here, unlike that in ISO C, the semicolons
1792 are included here rather than in struct-declaration, in order to
1793 describe the syntax with extra semicolons and missing semicolon at
1798 struct-declaration-list:
1799 @defs ( class-name )
1801 (Note this does not include a trailing semicolon, but can be
1802 followed by further declarations, and gets a pedwarn-if-pedantic
1803 when followed by a semicolon.) */
1805 static struct c_typespec
1806 c_parser_struct_or_union_specifier (c_parser *parser)
1808 struct c_typespec ret;
1810 tree ident = NULL_TREE;
1811 enum tree_code code;
1812 switch (c_parser_peek_token (parser)->keyword)
1823 c_parser_consume_token (parser);
1824 attrs = c_parser_attributes (parser);
1825 if (c_parser_next_token_is (parser, CPP_NAME))
1827 ident = c_parser_peek_token (parser)->value;
1828 c_parser_consume_token (parser);
1830 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
1832 /* Parse a struct or union definition. Start the scope of the
1833 tag before parsing components. */
1834 tree type = start_struct (code, ident);
1836 /* We chain the components in reverse order, then put them in
1837 forward order at the end. Each struct-declaration may
1838 declare multiple components (comma-separated), so we must use
1839 chainon to join them, although when parsing each
1840 struct-declaration we can use TREE_CHAIN directly.
1842 The theory behind all this is that there will be more
1843 semicolon separated fields than comma separated fields, and
1844 so we'll be minimizing the number of node traversals required
1846 tree contents = NULL_TREE;
1847 c_parser_consume_token (parser);
1848 /* Handle the Objective-C @defs construct,
1849 e.g. foo(sizeof(struct{ @defs(ClassName) }));. */
1850 if (c_parser_next_token_is_keyword (parser, RID_AT_DEFS))
1853 gcc_assert (c_dialect_objc ());
1854 c_parser_consume_token (parser);
1855 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
1857 if (c_parser_next_token_is (parser, CPP_NAME)
1858 && c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME)
1860 name = c_parser_peek_token (parser)->value;
1861 c_parser_consume_token (parser);
1865 c_parser_error (parser, "expected class name");
1866 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
1869 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
1871 contents = nreverse (objc_get_class_ivars (name));
1874 /* Parse the struct-declarations and semicolons. Problems with
1875 semicolons are diagnosed here; empty structures are diagnosed
1880 /* Parse any stray semicolon. */
1881 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1884 pedwarn ("extra semicolon in struct or union specified");
1885 c_parser_consume_token (parser);
1888 /* Stop if at the end of the struct or union contents. */
1889 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
1891 c_parser_consume_token (parser);
1894 /* Accept #pragmas at struct scope. */
1895 if (c_parser_next_token_is (parser, CPP_PRAGMA))
1897 c_parser_pragma (parser, pragma_external);
1900 /* Parse some comma-separated declarations, but not the
1901 trailing semicolon if any. */
1902 decls = c_parser_struct_declaration (parser);
1903 contents = chainon (decls, contents);
1904 /* If no semicolon follows, either we have a parse error or
1905 are at the end of the struct or union and should
1907 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1908 c_parser_consume_token (parser);
1911 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
1912 pedwarn ("no semicolon at end of struct or union");
1915 c_parser_error (parser, "expected %<;%>");
1916 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
1921 postfix_attrs = c_parser_attributes (parser);
1922 ret.spec = finish_struct (type, nreverse (contents),
1923 chainon (attrs, postfix_attrs));
1924 ret.kind = ctsk_tagdef;
1929 c_parser_error (parser, "expected %<{%>");
1930 ret.spec = error_mark_node;
1931 ret.kind = ctsk_tagref;
1934 ret = parser_xref_tag (code, ident);
1938 /* Parse a struct-declaration (C90 6.5.2.1, C99 6.7.2.1), *without*
1939 the trailing semicolon.
1942 specifier-qualifier-list struct-declarator-list
1944 specifier-qualifier-list:
1945 type-specifier specifier-qualifier-list[opt]
1946 type-qualifier specifier-qualifier-list[opt]
1947 attributes specifier-qualifier-list[opt]
1949 struct-declarator-list:
1951 struct-declarator-list , attributes[opt] struct-declarator
1954 declarator attributes[opt]
1955 declarator[opt] : constant-expression attributes[opt]
1960 __extension__ struct-declaration
1961 specifier-qualifier-list
1963 Unlike the ISO C syntax, semicolons are handled elsewhere. The use
1964 of attributes where shown is a GNU extension. In GNU C, we accept
1965 any expression without commas in the syntax (assignment
1966 expressions, not just conditional expressions); assignment
1967 expressions will be diagnosed as non-constant. */
1970 c_parser_struct_declaration (c_parser *parser)
1972 struct c_declspecs *specs;
1974 tree all_prefix_attrs;
1976 if (c_parser_next_token_is_keyword (parser, RID_EXTENSION))
1980 ext = disable_extension_diagnostics ();
1981 c_parser_consume_token (parser);
1982 decl = c_parser_struct_declaration (parser);
1983 restore_extension_diagnostics (ext);
1986 specs = build_null_declspecs ();
1987 c_parser_declspecs (parser, specs, false, true, true);
1990 if (!specs->declspecs_seen_p)
1992 c_parser_error (parser, "expected specifier-qualifier-list");
1995 finish_declspecs (specs);
1996 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1999 if (!specs->type_seen_p)
2002 pedwarn ("ISO C forbids member declarations with no members");
2003 shadow_tag_warned (specs, pedantic);
2008 /* Support for unnamed structs or unions as members of
2009 structs or unions (which is [a] useful and [b] supports
2011 ret = grokfield (build_id_declarator (NULL_TREE), specs, NULL_TREE);
2015 pending_xref_error ();
2016 prefix_attrs = specs->attrs;
2017 all_prefix_attrs = prefix_attrs;
2018 specs->attrs = NULL_TREE;
2022 /* Declaring one or more declarators or un-named bit-fields. */
2023 struct c_declarator *declarator;
2025 if (c_parser_next_token_is (parser, CPP_COLON))
2026 declarator = build_id_declarator (NULL_TREE);
2028 declarator = c_parser_declarator (parser, specs->type_seen_p,
2029 C_DTR_NORMAL, &dummy);
2030 if (declarator == NULL)
2032 c_parser_skip_to_end_of_block_or_statement (parser);
2035 if (c_parser_next_token_is (parser, CPP_COLON)
2036 || c_parser_next_token_is (parser, CPP_COMMA)
2037 || c_parser_next_token_is (parser, CPP_SEMICOLON)
2038 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE)
2039 || c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2041 tree postfix_attrs = NULL_TREE;
2042 tree width = NULL_TREE;
2044 if (c_parser_next_token_is (parser, CPP_COLON))
2046 c_parser_consume_token (parser);
2047 width = c_parser_expr_no_commas (parser, NULL).value;
2049 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2050 postfix_attrs = c_parser_attributes (parser);
2051 d = grokfield (declarator, specs, width);
2052 decl_attributes (&d, chainon (postfix_attrs,
2053 all_prefix_attrs), 0);
2054 TREE_CHAIN (d) = decls;
2056 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2057 all_prefix_attrs = chainon (c_parser_attributes (parser),
2060 all_prefix_attrs = prefix_attrs;
2061 if (c_parser_next_token_is (parser, CPP_COMMA))
2062 c_parser_consume_token (parser);
2063 else if (c_parser_next_token_is (parser, CPP_SEMICOLON)
2064 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2066 /* Semicolon consumed in caller. */
2071 c_parser_error (parser, "expected %<,%>, %<;%> or %<}%>");
2077 c_parser_error (parser,
2078 "expected %<:%>, %<,%>, %<;%>, %<}%> or "
2079 "%<__attribute__%>");
2086 /* Parse a typeof specifier (a GNU extension).
2089 typeof ( expression )
2090 typeof ( type-name )
2093 static struct c_typespec
2094 c_parser_typeof_specifier (c_parser *parser)
2096 struct c_typespec ret;
2097 ret.kind = ctsk_typeof;
2098 ret.spec = error_mark_node;
2099 gcc_assert (c_parser_next_token_is_keyword (parser, RID_TYPEOF));
2100 c_parser_consume_token (parser);
2103 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2109 if (c_parser_next_token_starts_typename (parser))
2111 struct c_type_name *type = c_parser_type_name (parser);
2116 ret.spec = groktypename (type);
2117 pop_maybe_used (variably_modified_type_p (ret.spec, NULL_TREE));
2122 struct c_expr expr = c_parser_expression (parser);
2125 if (TREE_CODE (expr.value) == COMPONENT_REF
2126 && DECL_C_BIT_FIELD (TREE_OPERAND (expr.value, 1)))
2127 error ("%<typeof%> applied to a bit-field");
2128 ret.spec = TREE_TYPE (expr.value);
2129 pop_maybe_used (variably_modified_type_p (ret.spec, NULL_TREE));
2131 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
2135 /* Parse a declarator, possibly an abstract declarator (C90 6.5.4,
2136 6.5.5, C99 6.7.5, 6.7.6). If TYPE_SEEN_P then a typedef name may
2137 be redeclared; otherwise it may not. KIND indicates which kind of
2138 declarator is wanted. Returns a valid declarator except in the
2139 case of a syntax error in which case NULL is returned. *SEEN_ID is
2140 set to true if an identifier being declared is seen; this is used
2141 to diagnose bad forms of abstract array declarators and to
2142 determine whether an identifier list is syntactically permitted.
2145 pointer[opt] direct-declarator
2149 ( attributes[opt] declarator )
2150 direct-declarator array-declarator
2151 direct-declarator ( parameter-type-list )
2152 direct-declarator ( identifier-list[opt] )
2155 * type-qualifier-list[opt]
2156 * type-qualifier-list[opt] pointer
2158 type-qualifier-list:
2161 type-qualifier-list type-qualifier
2162 type-qualifier-list attributes
2164 parameter-type-list:
2166 parameter-list , ...
2169 parameter-declaration
2170 parameter-list , parameter-declaration
2172 parameter-declaration:
2173 declaration-specifiers declarator attributes[opt]
2174 declaration-specifiers abstract-declarator[opt] attributes[opt]
2178 identifier-list , identifier
2180 abstract-declarator:
2182 pointer[opt] direct-abstract-declarator
2184 direct-abstract-declarator:
2185 ( attributes[opt] abstract-declarator )
2186 direct-abstract-declarator[opt] array-declarator
2187 direct-abstract-declarator[opt] ( parameter-type-list[opt] )
2192 direct-declarator ( parameter-forward-declarations
2193 parameter-type-list[opt] )
2195 direct-abstract-declarator:
2196 direct-abstract-declarator[opt] ( parameter-forward-declarations
2197 parameter-type-list[opt] )
2199 parameter-forward-declarations:
2201 parameter-forward-declarations parameter-list ;
2203 The uses of attributes shown above are GNU extensions.
2205 Some forms of array declarator are not included in C99 in the
2206 syntax for abstract declarators; these are disallowed elsewhere.
2207 This may be a defect (DR#289).
2209 This function also accepts an omitted abstract declarator as being
2210 an abstract declarator, although not part of the formal syntax. */
2212 static struct c_declarator *
2213 c_parser_declarator (c_parser *parser, bool type_seen_p, c_dtr_syn kind,
2216 /* Parse any initial pointer part. */
2217 if (c_parser_next_token_is (parser, CPP_MULT))
2219 struct c_declspecs *quals_attrs = build_null_declspecs ();
2220 struct c_declarator *inner;
2221 c_parser_consume_token (parser);
2222 c_parser_declspecs (parser, quals_attrs, false, false, true);
2223 inner = c_parser_declarator (parser, type_seen_p, kind, seen_id);
2227 return make_pointer_declarator (quals_attrs, inner);
2229 /* Now we have a direct declarator, direct abstract declarator or
2230 nothing (which counts as a direct abstract declarator here). */
2231 return c_parser_direct_declarator (parser, type_seen_p, kind, seen_id);
2234 /* Parse a direct declarator or direct abstract declarator; arguments
2235 as c_parser_declarator. */
2237 static struct c_declarator *
2238 c_parser_direct_declarator (c_parser *parser, bool type_seen_p, c_dtr_syn kind,
2241 /* The direct declarator must start with an identifier (possibly
2242 omitted) or a parenthesized declarator (possibly abstract). In
2243 an ordinary declarator, initial parentheses must start a
2244 parenthesized declarator. In an abstract declarator or parameter
2245 declarator, they could start a parenthesized declarator or a
2246 parameter list. To tell which, the open parenthesis and any
2247 following attributes must be read. If a declaration specifier
2248 follows, then it is a parameter list; if the specifier is a
2249 typedef name, there might be an ambiguity about redeclaring it,
2250 which is resolved in the direction of treating it as a typedef
2251 name. If a close parenthesis follows, it is also an empty
2252 parameter list, as the syntax does not permit empty abstract
2253 declarators. Otherwise, it is a parenthesized declarator (in
2254 which case the analysis may be repeated inside it, recursively).
2256 ??? There is an ambiguity in a parameter declaration "int
2257 (__attribute__((foo)) x)", where x is not a typedef name: it
2258 could be an abstract declarator for a function, or declare x with
2259 parentheses. The proper resolution of this ambiguity needs
2260 documenting. At present we follow an accident of the old
2261 parser's implementation, whereby the first parameter must have
2262 some declaration specifiers other than just attributes. Thus as
2263 a parameter declaration it is treated as a parenthesized
2264 parameter named x, and as an abstract declarator it is
2267 ??? Also following the old parser, attributes inside an empty
2268 parameter list are ignored, making it a list not yielding a
2269 prototype, rather than giving an error or making it have one
2270 parameter with implicit type int.
2272 ??? Also following the old parser, typedef names may be
2273 redeclared in declarators, but not Objective-C class names. */
2275 if (kind != C_DTR_ABSTRACT
2276 && c_parser_next_token_is (parser, CPP_NAME)
2278 && c_parser_peek_token (parser)->id_kind == C_ID_TYPENAME)
2279 || c_parser_peek_token (parser)->id_kind == C_ID_ID))
2281 struct c_declarator *inner
2282 = build_id_declarator (c_parser_peek_token (parser)->value);
2284 inner->id_loc = c_parser_peek_token (parser)->location;
2285 c_parser_consume_token (parser);
2286 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
2289 if (kind != C_DTR_NORMAL
2290 && c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
2292 struct c_declarator *inner = build_id_declarator (NULL_TREE);
2293 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
2296 /* Either we are at the end of an abstract declarator, or we have
2299 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
2302 struct c_declarator *inner;
2303 c_parser_consume_token (parser);
2304 attrs = c_parser_attributes (parser);
2305 if (kind != C_DTR_NORMAL
2306 && (c_parser_next_token_starts_declspecs (parser)
2307 || c_parser_next_token_is (parser, CPP_CLOSE_PAREN)))
2309 struct c_arg_info *args
2310 = c_parser_parms_declarator (parser, kind == C_DTR_NORMAL,
2317 = build_function_declarator (args,
2318 build_id_declarator (NULL_TREE));
2319 return c_parser_direct_declarator_inner (parser, *seen_id,
2323 /* A parenthesized declarator. */
2324 inner = c_parser_declarator (parser, type_seen_p, kind, seen_id);
2325 if (inner != NULL && attrs != NULL)
2326 inner = build_attrs_declarator (attrs, inner);
2327 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2329 c_parser_consume_token (parser);
2333 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
2337 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2344 if (kind == C_DTR_NORMAL)
2346 c_parser_error (parser, "expected identifier or %<(%>");
2350 return build_id_declarator (NULL_TREE);
2354 /* Parse part of a direct declarator or direct abstract declarator,
2355 given that some (in INNER) has already been parsed; ID_PRESENT is
2356 true if an identifier is present, false for an abstract
2359 static struct c_declarator *
2360 c_parser_direct_declarator_inner (c_parser *parser, bool id_present,
2361 struct c_declarator *inner)
2363 /* Parse a sequence of array declarators and parameter lists. */
2364 if (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
2366 struct c_declarator *declarator;
2367 struct c_declspecs *quals_attrs = build_null_declspecs ();
2371 c_parser_consume_token (parser);
2372 c_parser_declspecs (parser, quals_attrs, false, false, true);
2373 static_seen = c_parser_next_token_is_keyword (parser, RID_STATIC);
2375 c_parser_consume_token (parser);
2376 if (static_seen && !quals_attrs->declspecs_seen_p)
2377 c_parser_declspecs (parser, quals_attrs, false, false, true);
2378 if (!quals_attrs->declspecs_seen_p)
2380 /* If "static" is present, there must be an array dimension.
2381 Otherwise, there may be a dimension, "*", or no
2386 dimen = c_parser_expr_no_commas (parser, NULL).value;
2390 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
2395 else if (c_parser_next_token_is (parser, CPP_MULT))
2397 if (c_parser_peek_2nd_token (parser)->type == CPP_CLOSE_SQUARE)
2401 c_parser_consume_token (parser);
2406 dimen = c_parser_expr_no_commas (parser, NULL).value;
2412 dimen = c_parser_expr_no_commas (parser, NULL).value;
2415 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
2416 c_parser_consume_token (parser);
2419 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
2423 declarator = build_array_declarator (dimen, quals_attrs, static_seen,
2425 inner = set_array_declarator_inner (declarator, inner, !id_present);
2426 return c_parser_direct_declarator_inner (parser, id_present, inner);
2428 else if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
2431 struct c_arg_info *args;
2432 c_parser_consume_token (parser);
2433 attrs = c_parser_attributes (parser);
2434 args = c_parser_parms_declarator (parser, id_present, attrs);
2439 inner = build_function_declarator (args, inner);
2440 return c_parser_direct_declarator_inner (parser, id_present, inner);
2446 /* Parse a parameter list or identifier list, including the closing
2447 parenthesis but not the opening one. ATTRS are the attributes at
2448 the start of the list. ID_LIST_OK is true if an identifier list is
2449 acceptable; such a list must not have attributes at the start. */
2451 static struct c_arg_info *
2452 c_parser_parms_declarator (c_parser *parser, bool id_list_ok, tree attrs)
2455 declare_parm_level ();
2456 /* If the list starts with an identifier, it is an identifier list.
2457 Otherwise, it is either a prototype list or an empty list. */
2460 && c_parser_next_token_is (parser, CPP_NAME)
2461 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
2463 tree list = NULL_TREE, *nextp = &list;
2464 while (c_parser_next_token_is (parser, CPP_NAME)
2465 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
2467 *nextp = build_tree_list (NULL_TREE,
2468 c_parser_peek_token (parser)->value);
2469 nextp = & TREE_CHAIN (*nextp);
2470 c_parser_consume_token (parser);
2471 if (c_parser_next_token_is_not (parser, CPP_COMMA))
2473 c_parser_consume_token (parser);
2474 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2476 c_parser_error (parser, "expected identifier");
2480 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2482 struct c_arg_info *ret = XOBNEW (&parser_obstack, struct c_arg_info);
2487 c_parser_consume_token (parser);
2493 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2501 struct c_arg_info *ret = c_parser_parms_list_declarator (parser, attrs);
2507 /* Parse a parameter list (possibly empty), including the closing
2508 parenthesis but not the opening one. ATTRS are the attributes at
2509 the start of the list. */
2511 static struct c_arg_info *
2512 c_parser_parms_list_declarator (c_parser *parser, tree attrs)
2514 bool good_parm = false;
2515 /* ??? Following the old parser, forward parameter declarations may
2516 use abstract declarators, and if no real parameter declarations
2517 follow the forward declarations then this is not diagnosed. Also
2518 note as above that attributes are ignored as the only contents of
2519 the parentheses, or as the only contents after forward
2521 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2523 struct c_arg_info *ret = XOBNEW (&parser_obstack, struct c_arg_info);
2528 c_parser_consume_token (parser);
2531 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
2533 struct c_arg_info *ret = XOBNEW (&parser_obstack, struct c_arg_info);
2537 /* Suppress -Wold-style-definition for this case. */
2538 ret->types = error_mark_node;
2539 error ("ISO C requires a named argument before %<...%>");
2540 c_parser_consume_token (parser);
2541 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2543 c_parser_consume_token (parser);
2548 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2553 /* Nonempty list of parameters, either terminated with semicolon
2554 (forward declarations; recurse) or with close parenthesis (normal
2555 function) or with ", ... )" (variadic function). */
2558 /* Parse a parameter. */
2559 struct c_parm *parm = c_parser_parameter_declaration (parser, attrs);
2564 push_parm_decl (parm);
2566 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
2569 c_parser_consume_token (parser);
2570 mark_forward_parm_decls ();
2571 new_attrs = c_parser_attributes (parser);
2572 return c_parser_parms_list_declarator (parser, new_attrs);
2574 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2576 c_parser_consume_token (parser);
2578 return get_parm_info (false);
2581 struct c_arg_info *ret
2582 = XOBNEW (&parser_obstack, struct c_arg_info);
2590 if (!c_parser_require (parser, CPP_COMMA,
2591 "expected %<;%>, %<,%> or %<)%>"))
2593 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
2596 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
2598 c_parser_consume_token (parser);
2599 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2601 c_parser_consume_token (parser);
2603 return get_parm_info (true);
2606 struct c_arg_info *ret
2607 = XOBNEW (&parser_obstack, struct c_arg_info);
2617 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2625 /* Parse a parameter declaration. ATTRS are the attributes at the
2626 start of the declaration if it is the first parameter. */
2628 static struct c_parm *
2629 c_parser_parameter_declaration (c_parser *parser, tree attrs)
2631 struct c_declspecs *specs;
2632 struct c_declarator *declarator;
2634 tree postfix_attrs = NULL_TREE;
2636 if (!c_parser_next_token_starts_declspecs (parser))
2638 /* ??? In some Objective-C cases '...' isn't applicable so there
2639 should be a different message. */
2640 c_parser_error (parser,
2641 "expected declaration specifiers or %<...%>");
2642 c_parser_skip_to_end_of_parameter (parser);
2645 specs = build_null_declspecs ();
2648 declspecs_add_attrs (specs, attrs);
2651 c_parser_declspecs (parser, specs, true, true, true);
2652 finish_declspecs (specs);
2653 pending_xref_error ();
2654 prefix_attrs = specs->attrs;
2655 specs->attrs = NULL_TREE;
2656 declarator = c_parser_declarator (parser, specs->type_seen_p,
2657 C_DTR_PARM, &dummy);
2658 if (declarator == NULL)
2660 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
2663 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2664 postfix_attrs = c_parser_attributes (parser);
2665 return build_c_parm (specs, chainon (postfix_attrs, prefix_attrs),
2669 /* Parse a string literal in an asm expression. It should not be
2670 translated, and wide string literals are an error although
2671 permitted by the syntax. This is a GNU extension.
2676 ??? At present, following the old parser, the caller needs to have
2677 set c_lex_string_translate to 0. It would be better to follow the
2678 C++ parser rather than using the c_lex_string_translate kludge. */
2681 c_parser_asm_string_literal (c_parser *parser)
2684 if (c_parser_next_token_is (parser, CPP_STRING))
2686 str = c_parser_peek_token (parser)->value;
2687 c_parser_consume_token (parser);
2689 else if (c_parser_next_token_is (parser, CPP_WSTRING))
2691 error ("wide string literal in %<asm%>");
2692 str = build_string (1, "");
2693 c_parser_consume_token (parser);
2697 c_parser_error (parser, "expected string literal");
2703 /* Parse a simple asm expression. This is used in restricted
2704 contexts, where a full expression with inputs and outputs does not
2705 make sense. This is a GNU extension.
2708 asm ( asm-string-literal )
2712 c_parser_simple_asm_expr (c_parser *parser)
2715 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ASM));
2716 /* ??? Follow the C++ parser rather than using the
2717 c_lex_string_translate kludge. */
2718 c_lex_string_translate = 0;
2719 c_parser_consume_token (parser);
2720 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2722 c_lex_string_translate = 1;
2725 str = c_parser_asm_string_literal (parser);
2726 c_lex_string_translate = 1;
2727 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
2729 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
2735 /* Parse (possibly empty) attributes. This is a GNU extension.
2739 attributes attribute
2742 __attribute__ ( ( attribute-list ) )
2746 attribute_list , attrib
2751 any-word ( identifier )
2752 any-word ( identifier , nonempty-expr-list )
2753 any-word ( expr-list )
2755 where the "identifier" must not be declared as a type, and
2756 "any-word" may be any identifier (including one declared as a
2757 type), a reserved word storage class specifier, type specifier or
2758 type qualifier. ??? This still leaves out most reserved keywords
2759 (following the old parser), shouldn't we include them, and why not
2760 allow identifiers declared as types to start the arguments? */
2763 c_parser_attributes (c_parser *parser)
2765 tree attrs = NULL_TREE;
2766 while (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2768 /* ??? Follow the C++ parser rather than using the
2769 c_lex_string_translate kludge. */
2770 c_lex_string_translate = 0;
2771 c_parser_consume_token (parser);
2772 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2774 c_lex_string_translate = 1;
2777 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2779 c_lex_string_translate = 1;
2780 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
2783 /* Parse the attribute list. */
2784 while (c_parser_next_token_is (parser, CPP_COMMA)
2785 || c_parser_next_token_is (parser, CPP_NAME)
2786 || c_parser_next_token_is (parser, CPP_KEYWORD))
2788 tree attr, attr_name, attr_args;
2789 if (c_parser_next_token_is (parser, CPP_COMMA))
2791 c_parser_consume_token (parser);
2794 if (c_parser_next_token_is (parser, CPP_KEYWORD))
2796 /* ??? See comment above about what keywords are
2799 switch (c_parser_peek_token (parser)->keyword)
2834 attr_name = c_parser_peek_token (parser)->value;
2835 c_parser_consume_token (parser);
2836 if (c_parser_next_token_is_not (parser, CPP_OPEN_PAREN))
2838 attr = build_tree_list (attr_name, NULL_TREE);
2839 attrs = chainon (attrs, attr);
2842 c_parser_consume_token (parser);
2843 /* Parse the attribute contents. If they start with an
2844 identifier which is followed by a comma or close
2845 parenthesis, then the arguments start with that
2846 identifier; otherwise they are an expression list. */
2847 if (c_parser_next_token_is (parser, CPP_NAME)
2848 && c_parser_peek_token (parser)->id_kind == C_ID_ID
2849 && ((c_parser_peek_2nd_token (parser)->type == CPP_COMMA)
2850 || (c_parser_peek_2nd_token (parser)->type
2851 == CPP_CLOSE_PAREN)))
2853 tree arg1 = c_parser_peek_token (parser)->value;
2854 c_parser_consume_token (parser);
2855 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2856 attr_args = build_tree_list (NULL_TREE, arg1);
2859 c_parser_consume_token (parser);
2860 attr_args = tree_cons (NULL_TREE, arg1,
2861 c_parser_expr_list (parser, false));
2866 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2867 attr_args = NULL_TREE;
2869 attr_args = c_parser_expr_list (parser, false);
2871 attr = build_tree_list (attr_name, attr_args);
2872 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2873 c_parser_consume_token (parser);
2876 c_lex_string_translate = 1;
2877 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2881 attrs = chainon (attrs, attr);
2883 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2884 c_parser_consume_token (parser);
2887 c_lex_string_translate = 1;
2888 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2892 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2893 c_parser_consume_token (parser);
2896 c_lex_string_translate = 1;
2897 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2901 c_lex_string_translate = 1;
2906 /* Parse a type name (C90 6.5.5, C99 6.7.6).
2909 specifier-qualifier-list abstract-declarator[opt]
2912 static struct c_type_name *
2913 c_parser_type_name (c_parser *parser)
2915 struct c_declspecs *specs = build_null_declspecs ();
2916 struct c_declarator *declarator;
2917 struct c_type_name *ret;
2919 c_parser_declspecs (parser, specs, false, true, true);
2920 if (!specs->declspecs_seen_p)
2922 c_parser_error (parser, "expected specifier-qualifier-list");
2925 pending_xref_error ();
2926 finish_declspecs (specs);
2927 declarator = c_parser_declarator (parser, specs->type_seen_p,
2928 C_DTR_ABSTRACT, &dummy);
2929 if (declarator == NULL)
2931 ret = XOBNEW (&parser_obstack, struct c_type_name);
2933 ret->declarator = declarator;
2937 /* Parse an initializer (C90 6.5.7, C99 6.7.8).
2940 assignment-expression
2941 { initializer-list }
2942 { initializer-list , }
2945 designation[opt] initializer
2946 initializer-list , designation[opt] initializer
2953 designator-list designator
2960 [ constant-expression ]
2972 [ constant-expression ... constant-expression ]
2974 Any expression without commas is accepted in the syntax for the
2975 constant-expressions, with non-constant expressions rejected later.
2977 This function is only used for top-level initializers; for nested
2978 ones, see c_parser_initval. */
2980 static struct c_expr
2981 c_parser_initializer (c_parser *parser)
2983 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
2984 return c_parser_braced_init (parser, NULL_TREE, false);
2988 ret = c_parser_expr_no_commas (parser, NULL);
2989 if (TREE_CODE (ret.value) != STRING_CST
2990 && TREE_CODE (ret.value) != COMPOUND_LITERAL_EXPR)
2991 ret = default_function_array_conversion (ret);
2996 /* Parse a braced initializer list. TYPE is the type specified for a
2997 compound literal, and NULL_TREE for other initializers and for
2998 nested braced lists. NESTED_P is true for nested braced lists,
2999 false for the list of a compound literal or the list that is the
3000 top-level initializer in a declaration. */
3002 static struct c_expr
3003 c_parser_braced_init (c_parser *parser, tree type, bool nested_p)
3005 gcc_assert (c_parser_next_token_is (parser, CPP_OPEN_BRACE));
3006 c_parser_consume_token (parser);
3008 push_init_level (0);
3010 really_start_incremental_init (type);
3011 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3014 pedwarn ("ISO C forbids empty initializer braces");
3018 /* Parse a non-empty initializer list, possibly with a trailing
3022 c_parser_initelt (parser);
3025 if (c_parser_next_token_is (parser, CPP_COMMA))
3026 c_parser_consume_token (parser);
3029 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3033 if (c_parser_next_token_is_not (parser, CPP_CLOSE_BRACE))
3036 ret.value = error_mark_node;
3037 ret.original_code = ERROR_MARK;
3038 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, "expected %<}%>");
3041 c_parser_consume_token (parser);
3042 return pop_init_level (0);
3045 /* Parse a nested initializer, including designators. */
3048 c_parser_initelt (c_parser *parser)
3050 /* Parse any designator or designator list. A single array
3051 designator may have the subsequent "=" omitted in GNU C, but a
3052 longer list or a structure member designator may not. */
3053 if (c_parser_next_token_is (parser, CPP_NAME)
3054 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
3056 /* Old-style structure member designator. */
3057 set_init_label (c_parser_peek_token (parser)->value);
3059 pedwarn ("obsolete use of designated initializer with %<:%>");
3060 c_parser_consume_token (parser);
3061 c_parser_consume_token (parser);
3065 /* des_seen is 0 if there have been no designators, 1 if there
3066 has been a single array designator and 2 otherwise. */
3068 while (c_parser_next_token_is (parser, CPP_OPEN_SQUARE)
3069 || c_parser_next_token_is (parser, CPP_DOT))
3071 int des_prev = des_seen;
3074 if (c_parser_next_token_is (parser, CPP_DOT))
3077 c_parser_consume_token (parser);
3078 if (c_parser_next_token_is (parser, CPP_NAME))
3080 set_init_label (c_parser_peek_token (parser)->value);
3081 c_parser_consume_token (parser);
3086 init.value = error_mark_node;
3087 init.original_code = ERROR_MARK;
3088 c_parser_error (parser, "expected identifier");
3089 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
3090 process_init_element (init);
3097 /* ??? Following the old parser, [ objc-receiver
3098 objc-message-args ] is accepted as an initializer,
3099 being distinguished from a designator by what follows
3100 the first assignment expression inside the square
3101 brackets, but after a first array designator a
3102 subsequent square bracket is for Objective-C taken to
3103 start an expression, using the obsolete form of
3104 designated initializer without '=', rather than
3105 possibly being a second level of designation: in LALR
3106 terms, the '[' is shifted rather than reducing
3107 designator to designator-list. */
3108 if (des_prev == 1 && c_dialect_objc ())
3110 des_seen = des_prev;
3113 if (des_prev == 0 && c_dialect_objc ())
3115 /* This might be an array designator or an
3116 Objective-C message expression. If the former,
3117 continue parsing here; if the latter, parse the
3118 remainder of the initializer given the starting
3119 primary-expression. ??? It might make sense to
3120 distinguish when des_prev == 1 as well; see
3121 previous comment. */
3123 struct c_expr mexpr;
3124 c_parser_consume_token (parser);
3125 if (c_parser_peek_token (parser)->type == CPP_NAME
3126 && ((c_parser_peek_token (parser)->id_kind
3128 || (c_parser_peek_token (parser)->id_kind
3129 == C_ID_CLASSNAME)))
3131 /* Type name receiver. */
3132 tree id = c_parser_peek_token (parser)->value;
3133 c_parser_consume_token (parser);
3134 rec = objc_get_class_reference (id);
3135 goto parse_message_args;
3137 first = c_parser_expr_no_commas (parser, NULL).value;
3138 if (c_parser_next_token_is (parser, CPP_ELLIPSIS)
3139 || c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
3140 goto array_desig_after_first;
3141 /* Expression receiver. So far only one part
3142 without commas has been parsed; there might be
3143 more of the expression. */
3145 while (c_parser_next_token_is (parser, CPP_COMMA))
3148 c_parser_consume_token (parser);
3149 next = c_parser_expr_no_commas (parser, NULL);
3150 next = default_function_array_conversion (next);
3151 rec = build_compound_expr (rec, next.value);
3154 /* Now parse the objc-message-args. */
3155 args = c_parser_objc_message_args (parser);
3156 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
3159 = objc_build_message_expr (build_tree_list (rec, args));
3160 mexpr.original_code = ERROR_MARK;
3161 /* Now parse and process the remainder of the
3162 initializer, starting with this message
3163 expression as a primary-expression. */
3164 c_parser_initval (parser, &mexpr);
3167 c_parser_consume_token (parser);
3168 first = c_parser_expr_no_commas (parser, NULL).value;
3169 array_desig_after_first:
3170 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
3172 c_parser_consume_token (parser);
3173 second = c_parser_expr_no_commas (parser, NULL).value;
3177 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
3179 c_parser_consume_token (parser);
3180 set_init_index (first, second);
3181 if (pedantic && second)
3182 pedwarn ("ISO C forbids specifying range of "
3183 "elements to initialize");
3186 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
3192 if (c_parser_next_token_is (parser, CPP_EQ))
3194 if (pedantic && !flag_isoc99)
3195 pedwarn ("ISO C90 forbids specifying subobject to initialize");
3196 c_parser_consume_token (parser);
3203 pedwarn ("obsolete use of designated initializer "
3209 init.value = error_mark_node;
3210 init.original_code = ERROR_MARK;
3211 c_parser_error (parser, "expected %<=%>");
3212 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
3213 process_init_element (init);
3219 c_parser_initval (parser, NULL);
3222 /* Parse a nested initializer; as c_parser_initializer but parses
3223 initializers within braced lists, after any designators have been
3224 applied. If AFTER is not NULL then it is an Objective-C message
3225 expression which is the primary-expression starting the
3229 c_parser_initval (c_parser *parser, struct c_expr *after)
3232 gcc_assert (!after || c_dialect_objc ());
3233 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE) && !after)
3234 init = c_parser_braced_init (parser, NULL_TREE, true);
3237 init = c_parser_expr_no_commas (parser, after);
3238 if (init.value != NULL_TREE
3239 && TREE_CODE (init.value) != STRING_CST
3240 && TREE_CODE (init.value) != COMPOUND_LITERAL_EXPR)
3241 init = default_function_array_conversion (init);
3243 process_init_element (init);
3246 /* Parse a compound statement (possibly a function body) (C90 6.6.2,
3250 { block-item-list[opt] }
3251 { label-declarations block-item-list }
3255 block-item-list block-item
3267 { label-declarations block-item-list }
3270 __extension__ nested-declaration
3271 nested-function-definition
3275 label-declarations label-declaration
3278 __label__ identifier-list ;
3280 Allowing the mixing of declarations and code is new in C99. The
3281 GNU syntax also permits (not shown above) labels at the end of
3282 compound statements, which yield an error. We don't allow labels
3283 on declarations; this might seem like a natural extension, but
3284 there would be a conflict between attributes on the label and
3285 prefix attributes on the declaration. ??? The syntax follows the
3286 old parser in requiring something after label declarations.
3287 Although they are erroneous if the labels declared aren't defined,
3288 is it useful for the syntax to be this way?
3300 c_parser_compound_statement (c_parser *parser)
3303 if (!c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
3304 return error_mark_node;
3305 stmt = c_begin_compound_stmt (true);
3306 c_parser_compound_statement_nostart (parser);
3307 return c_end_compound_stmt (stmt, true);
3310 /* Parse a compound statement except for the opening brace. This is
3311 used for parsing both compound statements and statement expressions
3312 (which follow different paths to handling the opening). */
3315 c_parser_compound_statement_nostart (c_parser *parser)
3317 bool last_stmt = false;
3318 bool last_label = false;
3319 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3321 c_parser_consume_token (parser);
3324 if (c_parser_next_token_is_keyword (parser, RID_LABEL))
3326 /* Read zero or more forward-declarations for labels that nested
3327 functions can jump to. */
3328 while (c_parser_next_token_is_keyword (parser, RID_LABEL))
3330 c_parser_consume_token (parser);
3331 /* Any identifiers, including those declared as type names,
3336 if (c_parser_next_token_is_not (parser, CPP_NAME))
3338 c_parser_error (parser, "expected identifier");
3342 = declare_label (c_parser_peek_token (parser)->value);
3343 C_DECLARED_LABEL_FLAG (label) = 1;
3344 add_stmt (build_stmt (DECL_EXPR, label));
3345 c_parser_consume_token (parser);
3346 if (c_parser_next_token_is (parser, CPP_COMMA))
3347 c_parser_consume_token (parser);
3351 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
3353 /* ??? Locating this diagnostic on the token after the
3354 declarations end follows the old parser, but it might be
3355 better to locate it where the declarations start instead. */
3357 pedwarn ("ISO C forbids label declarations");
3359 /* We must now have at least one statement, label or declaration. */
3360 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3362 c_parser_error (parser, "expected declaration or statement");
3363 c_parser_consume_token (parser);
3366 while (c_parser_next_token_is_not (parser, CPP_CLOSE_BRACE))
3368 location_t loc = c_parser_peek_token (parser)->location;
3369 if (c_parser_next_token_is_keyword (parser, RID_CASE)
3370 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
3371 || (c_parser_next_token_is (parser, CPP_NAME)
3372 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
3376 c_parser_label (parser);
3378 else if (!last_label
3379 && c_parser_next_token_starts_declspecs (parser))
3382 c_parser_declaration_or_fndef (parser, true, true, true, true);
3384 && ((pedantic && !flag_isoc99)
3385 || warn_declaration_after_statement))
3386 pedwarn_c90 ("%HISO C90 forbids mixed declarations and code",
3390 else if (!last_label
3391 && c_parser_next_token_is_keyword (parser, RID_EXTENSION))
3393 /* __extension__ can start a declaration, but is also an
3394 unary operator that can start an expression. Consume all
3395 but the last of a possible series of __extension__ to
3397 while (c_parser_peek_2nd_token (parser)->type == CPP_KEYWORD
3398 && (c_parser_peek_2nd_token (parser)->keyword
3400 c_parser_consume_token (parser);
3401 if (c_token_starts_declspecs (c_parser_peek_2nd_token (parser)))
3404 ext = disable_extension_diagnostics ();
3405 c_parser_consume_token (parser);
3407 c_parser_declaration_or_fndef (parser, true, true, true, true);
3408 /* Following the old parser, __extension__ does not
3409 disable this diagnostic. */
3410 restore_extension_diagnostics (ext);
3412 && ((pedantic && !flag_isoc99)
3413 || warn_declaration_after_statement))
3414 pedwarn_c90 ("%HISO C90 forbids mixed declarations and code",
3421 else if (c_parser_next_token_is (parser, CPP_PRAGMA))
3423 /* External pragmas, and some omp pragmas, are not associated
3424 with regular c code, and so are not to be considered statements
3425 syntactically. This ensures that the user doesn't put them
3426 places that would turn into syntax errors if the directive
3428 if (c_parser_pragma (parser, pragma_compound))
3429 last_label = false, last_stmt = true;
3431 else if (c_parser_next_token_is (parser, CPP_EOF))
3433 c_parser_error (parser, "expected declaration or statement");
3441 c_parser_statement_after_labels (parser);
3445 error ("label at end of compound statement");
3446 c_parser_consume_token (parser);
3449 /* Parse a label (C90 6.6.1, C99 6.8.1).
3452 identifier : attributes[opt]
3453 case constant-expression :
3459 case constant-expression ... constant-expression :
3461 The use of attributes on labels is a GNU extension. The syntax in
3462 GNU C accepts any expressions without commas, non-constant
3463 expressions being rejected later. */
3466 c_parser_label (c_parser *parser)
3468 location_t loc1 = c_parser_peek_token (parser)->location;
3469 tree label = NULL_TREE;
3470 if (c_parser_next_token_is_keyword (parser, RID_CASE))
3473 c_parser_consume_token (parser);
3474 exp1 = c_parser_expr_no_commas (parser, NULL).value;
3475 if (c_parser_next_token_is (parser, CPP_COLON))
3477 c_parser_consume_token (parser);
3478 label = do_case (exp1, NULL_TREE);
3480 else if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
3482 c_parser_consume_token (parser);
3483 exp2 = c_parser_expr_no_commas (parser, NULL).value;
3484 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
3485 label = do_case (exp1, exp2);
3488 c_parser_error (parser, "expected %<:%> or %<...%>");
3490 else if (c_parser_next_token_is_keyword (parser, RID_DEFAULT))
3492 c_parser_consume_token (parser);
3493 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
3494 label = do_case (NULL_TREE, NULL_TREE);
3498 tree name = c_parser_peek_token (parser)->value;
3502 gcc_assert (c_parser_next_token_is (parser, CPP_NAME));
3503 c_parser_consume_token (parser);
3504 gcc_assert (c_parser_next_token_is (parser, CPP_COLON));
3505 loc2 = c_parser_peek_token (parser)->location;
3506 c_parser_consume_token (parser);
3507 attrs = c_parser_attributes (parser);
3508 tlab = define_label (loc2, name);
3511 decl_attributes (&tlab, attrs, 0);
3512 label = add_stmt (build_stmt (LABEL_EXPR, tlab));
3516 SET_EXPR_LOCATION (label, loc1);
3519 /* Parse a statement (C90 6.6, C99 6.8).
3524 expression-statement
3532 expression-statement:
3535 selection-statement:
3539 iteration-statement:
3548 return expression[opt] ;
3561 objc-throw-statement
3562 objc-try-catch-statement
3563 objc-synchronized-statement
3565 objc-throw-statement:
3579 parallel-for-construct
3580 parallel-sections-construct
3587 parallel-directive structured-block
3590 for-directive iteration-statement
3593 sections-directive section-scope
3596 single-directive structured-block
3598 parallel-for-construct:
3599 parallel-for-directive iteration-statement
3601 parallel-sections-construct:
3602 parallel-sections-directive section-scope
3605 master-directive structured-block
3608 critical-directive structured-block
3611 atomic-directive expression-statement
3614 ordered-directive structured-block */
3617 c_parser_statement (c_parser *parser)
3619 while (c_parser_next_token_is_keyword (parser, RID_CASE)
3620 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
3621 || (c_parser_next_token_is (parser, CPP_NAME)
3622 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
3623 c_parser_label (parser);
3624 c_parser_statement_after_labels (parser);
3627 /* Parse a statement, other than a labeled statement. */
3630 c_parser_statement_after_labels (c_parser *parser)
3632 location_t loc = c_parser_peek_token (parser)->location;
3633 tree stmt = NULL_TREE;
3634 switch (c_parser_peek_token (parser)->type)
3636 case CPP_OPEN_BRACE:
3637 add_stmt (c_parser_compound_statement (parser));
3640 switch (c_parser_peek_token (parser)->keyword)
3643 c_parser_if_statement (parser);
3646 c_parser_switch_statement (parser);
3649 c_parser_while_statement (parser);
3652 c_parser_do_statement (parser);
3655 c_parser_for_statement (parser);
3658 c_parser_consume_token (parser);
3659 if (c_parser_next_token_is (parser, CPP_NAME))
3661 stmt = c_finish_goto_label (c_parser_peek_token (parser)->value);
3662 c_parser_consume_token (parser);
3664 else if (c_parser_next_token_is (parser, CPP_MULT))
3666 c_parser_consume_token (parser);
3667 stmt = c_finish_goto_ptr (c_parser_expression (parser).value);
3670 c_parser_error (parser, "expected identifier or %<*%>");
3671 goto expect_semicolon;
3673 c_parser_consume_token (parser);
3674 stmt = c_finish_bc_stmt (&c_cont_label, false);
3675 goto expect_semicolon;
3677 c_parser_consume_token (parser);
3678 stmt = c_finish_bc_stmt (&c_break_label, true);
3679 goto expect_semicolon;
3681 c_parser_consume_token (parser);
3682 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
3684 stmt = c_finish_return (NULL_TREE);
3685 c_parser_consume_token (parser);
3689 stmt = c_finish_return (c_parser_expression_conv (parser).value);
3690 goto expect_semicolon;
3694 stmt = c_parser_asm_statement (parser);
3697 gcc_assert (c_dialect_objc ());
3698 c_parser_consume_token (parser);
3699 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
3701 stmt = objc_build_throw_stmt (NULL_TREE);
3702 c_parser_consume_token (parser);
3707 = objc_build_throw_stmt (c_parser_expression (parser).value);
3708 goto expect_semicolon;
3712 gcc_assert (c_dialect_objc ());
3713 c_parser_objc_try_catch_statement (parser);
3715 case RID_AT_SYNCHRONIZED:
3716 gcc_assert (c_dialect_objc ());
3717 c_parser_objc_synchronized_statement (parser);
3724 c_parser_consume_token (parser);
3726 case CPP_CLOSE_PAREN:
3727 case CPP_CLOSE_SQUARE:
3728 /* Avoid infinite loop in error recovery:
3729 c_parser_skip_until_found stops at a closing nesting
3730 delimiter without consuming it, but here we need to consume
3731 it to proceed further. */
3732 c_parser_error (parser, "expected statement");
3733 c_parser_consume_token (parser);
3736 c_parser_pragma (parser, pragma_stmt);
3740 stmt = c_finish_expr_stmt (c_parser_expression_conv (parser).value);
3742 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
3745 /* Two cases cannot and do not have line numbers associated: If stmt
3746 is degenerate, such as "2;", then stmt is an INTEGER_CST, which
3747 cannot hold line numbers. But that's OK because the statement
3748 will either be changed to a MODIFY_EXPR during gimplification of
3749 the statement expr, or discarded. If stmt was compound, but
3750 without new variables, we will have skipped the creation of a
3751 BIND and will have a bare STATEMENT_LIST. But that's OK because
3752 (recursively) all of the component statements should already have
3753 line numbers assigned. ??? Can we discard no-op statements
3755 if (stmt && EXPR_P (stmt))
3756 SET_EXPR_LOCATION (stmt, loc);
3759 /* Parse a parenthesized condition from an if, do or while statement.
3765 c_parser_paren_condition (c_parser *parser)
3769 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3770 return error_mark_node;
3771 loc = c_parser_peek_token (parser)->location;
3772 cond = c_objc_common_truthvalue_conversion
3773 (c_parser_expression_conv (parser).value);
3775 SET_EXPR_LOCATION (cond, loc);
3776 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
3780 /* Parse a statement which is a block in C99. */
3783 c_parser_c99_block_statement (c_parser *parser)
3785 tree block = c_begin_compound_stmt (flag_isoc99);
3786 c_parser_statement (parser);
3787 return c_end_compound_stmt (block, flag_isoc99);
3790 /* Parse the body of an if statement or the else half thereof. This
3791 is just parsing a statement but (a) it is a block in C99, (b) we
3792 track whether the body is an if statement for the sake of
3793 -Wparentheses warnings, (c) we handle an empty body specially for
3794 the sake of -Wextra warnings. */
3797 c_parser_if_body (c_parser *parser, bool *if_p)
3799 tree block = c_begin_compound_stmt (flag_isoc99);
3800 while (c_parser_next_token_is_keyword (parser, RID_CASE)
3801 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
3802 || (c_parser_next_token_is (parser, CPP_NAME)
3803 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
3804 c_parser_label (parser);
3805 *if_p = c_parser_next_token_is_keyword (parser, RID_IF);
3806 if (extra_warnings && c_parser_next_token_is (parser, CPP_SEMICOLON))
3807 add_stmt (build_empty_stmt ());
3808 c_parser_statement_after_labels (parser);
3809 return c_end_compound_stmt (block, flag_isoc99);
3812 /* Parse an if statement (C90 6.6.4, C99 6.8.4).
3815 if ( expression ) statement
3816 if ( expression ) statement else statement
3820 c_parser_if_statement (c_parser *parser)
3825 bool first_if = false, second_if = false;
3826 tree first_body, second_body;
3827 gcc_assert (c_parser_next_token_is_keyword (parser, RID_IF));
3828 c_parser_consume_token (parser);
3829 block = c_begin_compound_stmt (flag_isoc99);
3830 loc = c_parser_peek_token (parser)->location;
3831 cond = c_parser_paren_condition (parser);
3832 first_body = c_parser_if_body (parser, &first_if);
3833 if (c_parser_next_token_is_keyword (parser, RID_ELSE))
3835 c_parser_consume_token (parser);
3836 second_body = c_parser_if_body (parser, &second_if);
3839 second_body = NULL_TREE;
3840 c_finish_if_stmt (loc, cond, first_body, second_body, first_if);
3841 add_stmt (c_end_compound_stmt (block, flag_isoc99));
3844 /* Parse a switch statement (C90 6.6.4, C99 6.8.4).
3847 switch (expression) statement
3851 c_parser_switch_statement (c_parser *parser)
3853 tree block, expr, body, save_break;
3854 gcc_assert (c_parser_next_token_is_keyword (parser, RID_SWITCH));
3855 c_parser_consume_token (parser);
3856 block = c_begin_compound_stmt (flag_isoc99);
3857 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3859 expr = c_parser_expression (parser).value;
3860 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
3863 expr = error_mark_node;
3864 c_start_case (expr);
3865 save_break = c_break_label;
3866 c_break_label = NULL_TREE;
3867 body = c_parser_c99_block_statement (parser);
3868 c_finish_case (body);
3870 add_stmt (build1 (LABEL_EXPR, void_type_node, c_break_label));
3871 c_break_label = save_break;
3872 add_stmt (c_end_compound_stmt (block, flag_isoc99));
3875 /* Parse a while statement (C90 6.6.5, C99 6.8.5).
3878 while (expression) statement
3882 c_parser_while_statement (c_parser *parser)
3884 tree block, cond, body, save_break, save_cont;
3886 gcc_assert (c_parser_next_token_is_keyword (parser, RID_WHILE));
3887 c_parser_consume_token (parser);
3888 block = c_begin_compound_stmt (flag_isoc99);
3889 loc = c_parser_peek_token (parser)->location;
3890 cond = c_parser_paren_condition (parser);
3891 save_break = c_break_label;
3892 c_break_label = NULL_TREE;
3893 save_cont = c_cont_label;
3894 c_cont_label = NULL_TREE;
3895 body = c_parser_c99_block_statement (parser);
3896 c_finish_loop (loc, cond, NULL, body, c_break_label, c_cont_label, true);
3897 add_stmt (c_end_compound_stmt (block, flag_isoc99));
3898 c_break_label = save_break;
3899 c_cont_label = save_cont;
3902 /* Parse a do statement (C90 6.6.5, C99 6.8.5).
3905 do statement while ( expression ) ;
3909 c_parser_do_statement (c_parser *parser)
3911 tree block, cond, body, save_break, save_cont, new_break, new_cont;
3913 gcc_assert (c_parser_next_token_is_keyword (parser, RID_DO));
3914 c_parser_consume_token (parser);
3915 block = c_begin_compound_stmt (flag_isoc99);
3916 loc = c_parser_peek_token (parser)->location;
3917 save_break = c_break_label;
3918 c_break_label = NULL_TREE;
3919 save_cont = c_cont_label;
3920 c_cont_label = NULL_TREE;
3921 body = c_parser_c99_block_statement (parser);
3922 c_parser_require_keyword (parser, RID_WHILE, "expected %<while%>");
3923 new_break = c_break_label;
3924 c_break_label = save_break;
3925 new_cont = c_cont_label;
3926 c_cont_label = save_cont;
3927 cond = c_parser_paren_condition (parser);
3928 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
3929 c_parser_skip_to_end_of_block_or_statement (parser);
3930 c_finish_loop (loc, cond, NULL, body, new_break, new_cont, false);
3931 add_stmt (c_end_compound_stmt (block, flag_isoc99));
3934 /* Parse a for statement (C90 6.6.5, C99 6.8.5).
3937 for ( expression[opt] ; expression[opt] ; expression[opt] ) statement
3938 for ( nested-declaration expression[opt] ; expression[opt] ) statement
3940 The form with a declaration is new in C99.
3942 ??? In accordance with the old parser, the declaration may be a
3943 nested function, which is then rejected in check_for_loop_decls,
3944 but does it make any sense for this to be included in the grammar?
3945 Note in particular that the nested function does not include a
3946 trailing ';', whereas the "declaration" production includes one.
3947 Also, can we reject bad declarations earlier and cheaper than
3948 check_for_loop_decls? */
3951 c_parser_for_statement (c_parser *parser)
3953 tree block, cond, incr, save_break, save_cont, body;
3955 gcc_assert (c_parser_next_token_is_keyword (parser, RID_FOR));
3956 loc = c_parser_peek_token (parser)->location;
3957 c_parser_consume_token (parser);
3958 block = c_begin_compound_stmt (flag_isoc99);
3959 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3961 /* Parse the initialization declaration or expression. */
3962 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
3964 c_parser_consume_token (parser);
3965 c_finish_expr_stmt (NULL_TREE);
3967 else if (c_parser_next_token_starts_declspecs (parser))
3969 c_parser_declaration_or_fndef (parser, true, true, true, true);
3970 check_for_loop_decls ();
3972 else if (c_parser_next_token_is_keyword (parser, RID_EXTENSION))
3974 /* __extension__ can start a declaration, but is also an
3975 unary operator that can start an expression. Consume all
3976 but the last of a possible series of __extension__ to
3978 while (c_parser_peek_2nd_token (parser)->type == CPP_KEYWORD
3979 && (c_parser_peek_2nd_token (parser)->keyword
3981 c_parser_consume_token (parser);
3982 if (c_token_starts_declspecs (c_parser_peek_2nd_token (parser)))
3985 ext = disable_extension_diagnostics ();
3986 c_parser_consume_token (parser);
3987 c_parser_declaration_or_fndef (parser, true, true, true, true);
3988 restore_extension_diagnostics (ext);
3989 check_for_loop_decls ();
3997 c_finish_expr_stmt (c_parser_expression (parser).value);
3998 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
4000 /* Parse the loop condition. */
4001 loc = c_parser_peek_token (parser)->location;
4002 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
4004 c_parser_consume_token (parser);
4009 tree ocond = c_parser_expression_conv (parser).value;
4010 cond = c_objc_common_truthvalue_conversion (ocond);
4012 SET_EXPR_LOCATION (cond, loc);
4013 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
4015 /* Parse the increment expression. */
4016 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4017 incr = c_process_expr_stmt (NULL_TREE);
4019 incr = c_process_expr_stmt (c_parser_expression (parser).value);
4020 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
4024 cond = error_mark_node;
4025 incr = error_mark_node;
4027 save_break = c_break_label;
4028 c_break_label = NULL_TREE;
4029 save_cont = c_cont_label;
4030 c_cont_label = NULL_TREE;
4031 body = c_parser_c99_block_statement (parser);
4032 c_finish_loop (loc, cond, incr, body, c_break_label, c_cont_label, true);
4033 add_stmt (c_end_compound_stmt (block, flag_isoc99));
4034 c_break_label = save_break;
4035 c_cont_label = save_cont;
4038 /* Parse an asm statement, a GNU extension. This is a full-blown asm
4039 statement with inputs, outputs, clobbers, and volatile tag
4043 asm type-qualifier[opt] ( asm-argument ) ;
4047 asm-string-literal : asm-operands[opt]
4048 asm-string-literal : asm-operands[opt] : asm-operands[opt]
4049 asm-string-literal : asm-operands[opt] : asm-operands[opt] : asm-clobbers
4051 Qualifiers other than volatile are accepted in the syntax but
4055 c_parser_asm_statement (c_parser *parser)
4057 tree quals, str, outputs, inputs, clobbers, ret;
4059 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ASM));
4060 c_parser_consume_token (parser);
4061 if (c_parser_next_token_is_keyword (parser, RID_VOLATILE))
4063 quals = c_parser_peek_token (parser)->value;
4064 c_parser_consume_token (parser);
4066 else if (c_parser_next_token_is_keyword (parser, RID_CONST)
4067 || c_parser_next_token_is_keyword (parser, RID_RESTRICT))
4069 warning (0, "%E qualifier ignored on asm",
4070 c_parser_peek_token (parser)->value);
4072 c_parser_consume_token (parser);
4076 /* ??? Follow the C++ parser rather than using the
4077 c_lex_string_translate kludge. */
4078 c_lex_string_translate = 0;
4079 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4081 c_lex_string_translate = 1;
4084 str = c_parser_asm_string_literal (parser);
4085 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4088 outputs = NULL_TREE;
4090 clobbers = NULL_TREE;
4093 if (!c_parser_require (parser, CPP_COLON, "expected %<:%> or %<)%>"))
4095 c_lex_string_translate = 1;
4096 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
4100 /* Parse outputs. */
4101 if (c_parser_next_token_is (parser, CPP_COLON)
4102 || c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4103 outputs = NULL_TREE;
4105 outputs = c_parser_asm_operands (parser, false);
4106 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4109 clobbers = NULL_TREE;
4112 if (!c_parser_require (parser, CPP_COLON, "expected %<:%> or %<)%>"))
4114 c_lex_string_translate = 1;
4115 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
4119 if (c_parser_next_token_is (parser, CPP_COLON)
4120 || c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4123 inputs = c_parser_asm_operands (parser, true);
4124 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4126 clobbers = NULL_TREE;
4129 if (!c_parser_require (parser, CPP_COLON, "expected %<:%> or %<)%>"))
4131 c_lex_string_translate = 1;
4132 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
4135 /* Parse clobbers. */
4136 clobbers = c_parser_asm_clobbers (parser);
4138 c_lex_string_translate = 1;
4139 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
4141 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
4144 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
4145 c_parser_skip_to_end_of_block_or_statement (parser);
4146 ret = build_asm_stmt (quals, build_asm_expr (str, outputs, inputs,
4151 /* Parse asm operands, a GNU extension. If CONVERT_P (for inputs but
4152 not outputs), apply the default conversion of functions and arrays
4157 asm-operands , asm-operand
4160 asm-string-literal ( expression )
4161 [ identifier ] asm-string-literal ( expression )
4165 c_parser_asm_operands (c_parser *parser, bool convert_p)
4167 tree list = NULL_TREE;
4172 if (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
4174 c_parser_consume_token (parser);
4175 if (c_parser_next_token_is (parser, CPP_NAME))
4177 tree id = c_parser_peek_token (parser)->value;
4178 c_parser_consume_token (parser);
4179 name = build_string (IDENTIFIER_LENGTH (id),
4180 IDENTIFIER_POINTER (id));
4184 c_parser_error (parser, "expected identifier");
4185 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
4188 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
4193 str = c_parser_asm_string_literal (parser);
4194 if (str == NULL_TREE)
4196 c_lex_string_translate = 1;
4197 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4199 c_lex_string_translate = 0;
4202 expr = c_parser_expression (parser);
4204 expr = default_function_array_conversion (expr);
4205 c_lex_string_translate = 0;
4206 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
4208 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
4211 list = chainon (list, build_tree_list (build_tree_list (name, str),
4213 if (c_parser_next_token_is (parser, CPP_COMMA))
4214 c_parser_consume_token (parser);
4221 /* Parse asm clobbers, a GNU extension.
4225 asm-clobbers , asm-string-literal
4229 c_parser_asm_clobbers (c_parser *parser)
4231 tree list = NULL_TREE;
4234 tree str = c_parser_asm_string_literal (parser);
4236 list = tree_cons (NULL_TREE, str, list);
4239 if (c_parser_next_token_is (parser, CPP_COMMA))
4240 c_parser_consume_token (parser);
4247 /* Parse an expression other than a compound expression; that is, an
4248 assignment expression (C90 6.3.16, C99 6.5.16). If AFTER is not
4249 NULL then it is an Objective-C message expression which is the
4250 primary-expression starting the expression as an initializer.
4252 assignment-expression:
4253 conditional-expression
4254 unary-expression assignment-operator assignment-expression
4256 assignment-operator: one of
4257 = *= /= %= += -= <<= >>= &= ^= |=
4259 In GNU C we accept any conditional expression on the LHS and
4260 diagnose the invalid lvalue rather than producing a syntax
4263 static struct c_expr
4264 c_parser_expr_no_commas (c_parser *parser, struct c_expr *after)
4266 struct c_expr lhs, rhs, ret;
4267 enum tree_code code;
4268 gcc_assert (!after || c_dialect_objc ());
4269 lhs = c_parser_conditional_expression (parser, after);
4270 switch (c_parser_peek_token (parser)->type)
4279 code = TRUNC_DIV_EXPR;
4282 code = TRUNC_MOD_EXPR;
4297 code = BIT_AND_EXPR;
4300 code = BIT_XOR_EXPR;
4303 code = BIT_IOR_EXPR;
4308 c_parser_consume_token (parser);
4309 rhs = c_parser_expr_no_commas (parser, NULL);
4310 rhs = default_function_array_conversion (rhs);
4311 ret.value = build_modify_expr (lhs.value, code, rhs.value);
4312 if (code == NOP_EXPR)
4313 ret.original_code = MODIFY_EXPR;
4316 TREE_NO_WARNING (ret.value) = 1;
4317 ret.original_code = ERROR_MARK;
4322 /* Parse a conditional expression (C90 6.3.15, C99 6.5.15). If AFTER
4323 is not NULL then it is an Objective-C message expression which is
4324 the primary-expression starting the expression as an initializer.
4326 conditional-expression:
4327 logical-OR-expression
4328 logical-OR-expression ? expression : conditional-expression
4332 conditional-expression:
4333 logical-OR-expression ? : conditional-expression
4336 static struct c_expr
4337 c_parser_conditional_expression (c_parser *parser, struct c_expr *after)
4339 struct c_expr cond, exp1, exp2, ret;
4340 gcc_assert (!after || c_dialect_objc ());
4341 cond = c_parser_binary_expression (parser, after);
4342 if (c_parser_next_token_is_not (parser, CPP_QUERY))
4344 cond = default_function_array_conversion (cond);
4345 c_parser_consume_token (parser);
4346 if (c_parser_next_token_is (parser, CPP_COLON))
4349 pedwarn ("ISO C forbids omitting the middle term of a ?: expression");
4350 /* Make sure first operand is calculated only once. */
4351 exp1.value = save_expr (default_conversion (cond.value));
4352 cond.value = c_objc_common_truthvalue_conversion (exp1.value);
4353 skip_evaluation += cond.value == truthvalue_true_node;
4358 = c_objc_common_truthvalue_conversion
4359 (default_conversion (cond.value));
4360 skip_evaluation += cond.value == truthvalue_false_node;
4361 exp1 = c_parser_expression_conv (parser);
4362 skip_evaluation += ((cond.value == truthvalue_true_node)
4363 - (cond.value == truthvalue_false_node));
4365 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
4367 skip_evaluation -= cond.value == truthvalue_true_node;
4368 ret.value = error_mark_node;
4369 ret.original_code = ERROR_MARK;
4372 exp2 = c_parser_conditional_expression (parser, NULL);
4373 exp2 = default_function_array_conversion (exp2);
4374 skip_evaluation -= cond.value == truthvalue_true_node;
4375 ret.value = build_conditional_expr (cond.value, exp1.value, exp2.value);
4376 ret.original_code = ERROR_MARK;
4380 /* Parse a binary expression; that is, a logical-OR-expression (C90
4381 6.3.5-6.3.14, C99 6.5.5-6.5.14). If AFTER is not NULL then it is
4382 an Objective-C message expression which is the primary-expression
4383 starting the expression as an initializer.
4385 multiplicative-expression:
4387 multiplicative-expression * cast-expression
4388 multiplicative-expression / cast-expression
4389 multiplicative-expression % cast-expression
4391 additive-expression:
4392 multiplicative-expression
4393 additive-expression + multiplicative-expression
4394 additive-expression - multiplicative-expression
4398 shift-expression << additive-expression
4399 shift-expression >> additive-expression
4401 relational-expression:
4403 relational-expression < shift-expression
4404 relational-expression > shift-expression
4405 relational-expression <= shift-expression
4406 relational-expression >= shift-expression
4408 equality-expression:
4409 relational-expression
4410 equality-expression == relational-expression
4411 equality-expression != relational-expression
4415 AND-expression & equality-expression
4417 exclusive-OR-expression:
4419 exclusive-OR-expression ^ AND-expression
4421 inclusive-OR-expression:
4422 exclusive-OR-expression
4423 inclusive-OR-expression | exclusive-OR-expression
4425 logical-AND-expression:
4426 inclusive-OR-expression
4427 logical-AND-expression && inclusive-OR-expression
4429 logical-OR-expression:
4430 logical-AND-expression
4431 logical-OR-expression || logical-AND-expression
4434 static struct c_expr
4435 c_parser_binary_expression (c_parser *parser, struct c_expr *after)
4437 /* A binary expression is parsed using operator-precedence parsing,
4438 with the operands being cast expressions. All the binary
4439 operators are left-associative. Thus a binary expression is of
4442 E0 op1 E1 op2 E2 ...
4444 which we represent on a stack. On the stack, the precedence
4445 levels are strictly increasing. When a new operator is
4446 encountered of higher precedence than that at the top of the
4447 stack, it is pushed; its LHS is the top expression, and its RHS
4448 is everything parsed until it is popped. When a new operator is
4449 encountered with precedence less than or equal to that at the top
4450 of the stack, triples E[i-1] op[i] E[i] are popped and replaced
4451 by the result of the operation until the operator at the top of
4452 the stack has lower precedence than the new operator or there is
4453 only one element on the stack; then the top expression is the LHS
4454 of the new operator. In the case of logical AND and OR
4455 expressions, we also need to adjust skip_evaluation as
4456 appropriate when the operators are pushed and popped. */
4458 /* The precedence levels, where 0 is a dummy lowest level used for
4459 the bottom of the stack. */
4475 /* The expression at this stack level. */
4477 /* The precedence of the operator on its left, PREC_NONE at the
4478 bottom of the stack. */
4480 /* The operation on its left. */
4486 switch (stack[sp].op) \
4488 case TRUTH_ANDIF_EXPR: \
4489 skip_evaluation -= stack[sp - 1].expr.value == truthvalue_false_node; \
4491 case TRUTH_ORIF_EXPR: \
4492 skip_evaluation -= stack[sp - 1].expr.value == truthvalue_true_node; \
4497 stack[sp - 1].expr \
4498 = default_function_array_conversion (stack[sp - 1].expr); \
4500 = default_function_array_conversion (stack[sp].expr); \
4501 stack[sp - 1].expr = parser_build_binary_op (stack[sp].op, \
4502 stack[sp - 1].expr, \
4506 gcc_assert (!after || c_dialect_objc ());
4507 stack[0].expr = c_parser_cast_expression (parser, after);
4508 stack[0].prec = PREC_NONE;
4513 enum tree_code ocode;
4516 switch (c_parser_peek_token (parser)->type)
4524 ocode = TRUNC_DIV_EXPR;
4528 ocode = TRUNC_MOD_EXPR;
4540 ocode = LSHIFT_EXPR;
4544 ocode = RSHIFT_EXPR;
4558 case CPP_GREATER_EQ:
4571 oprec = PREC_BITAND;
4572 ocode = BIT_AND_EXPR;
4575 oprec = PREC_BITXOR;
4576 ocode = BIT_XOR_EXPR;
4580 ocode = BIT_IOR_EXPR;
4583 oprec = PREC_LOGAND;
4584 ocode = TRUTH_ANDIF_EXPR;
4588 ocode = TRUTH_ORIF_EXPR;
4591 /* Not a binary operator, so end of the binary
4595 c_parser_consume_token (parser);
4596 while (oprec <= stack[sp].prec)
4600 case TRUTH_ANDIF_EXPR:
4602 = default_function_array_conversion (stack[sp].expr);
4603 stack[sp].expr.value = c_objc_common_truthvalue_conversion
4604 (default_conversion (stack[sp].expr.value));
4605 skip_evaluation += stack[sp].expr.value == truthvalue_false_node;
4607 case TRUTH_ORIF_EXPR:
4609 = default_function_array_conversion (stack[sp].expr);
4610 stack[sp].expr.value = c_objc_common_truthvalue_conversion
4611 (default_conversion (stack[sp].expr.value));
4612 skip_evaluation += stack[sp].expr.value == truthvalue_true_node;
4618 stack[sp].expr = c_parser_cast_expression (parser, NULL);
4619 stack[sp].prec = oprec;
4620 stack[sp].op = ocode;
4625 return stack[0].expr;
4629 /* Parse a cast expression (C90 6.3.4, C99 6.5.4). If AFTER is not
4630 NULL then it is an Objective-C message expression which is the
4631 primary-expression starting the expression as an initializer.
4635 ( type-name ) unary-expression
4638 static struct c_expr
4639 c_parser_cast_expression (c_parser *parser, struct c_expr *after)
4641 gcc_assert (!after || c_dialect_objc ());
4643 return c_parser_postfix_expression_after_primary (parser, *after);
4644 /* If the expression begins with a parenthesized type name, it may
4645 be either a cast or a compound literal; we need to see whether
4646 the next character is '{' to tell the difference. If not, it is
4647 an unary expression. */
4648 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
4649 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
4651 struct c_type_name *type_name;
4654 c_parser_consume_token (parser);
4655 type_name = c_parser_type_name (parser);
4656 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
4657 if (type_name == NULL)
4659 ret.value = error_mark_node;
4660 ret.original_code = ERROR_MARK;
4664 /* Save casted types in the function's used types hash table. */
4665 if (debug_info_level > DINFO_LEVEL_NONE)
4666 used_types_insert (type_name->specs->type, cfun);
4668 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
4669 return c_parser_postfix_expression_after_paren_type (parser,
4671 expr = c_parser_cast_expression (parser, NULL);
4672 expr = default_function_array_conversion (expr);
4673 ret.value = c_cast_expr (type_name, expr.value);
4674 ret.original_code = ERROR_MARK;
4678 return c_parser_unary_expression (parser);
4681 /* Parse an unary expression (C90 6.3.3, C99 6.5.3).
4687 unary-operator cast-expression
4688 sizeof unary-expression
4689 sizeof ( type-name )
4691 unary-operator: one of
4697 __alignof__ unary-expression
4698 __alignof__ ( type-name )
4701 unary-operator: one of
4702 __extension__ __real__ __imag__
4704 In addition, the GNU syntax treats ++ and -- as unary operators, so
4705 they may be applied to cast expressions with errors for non-lvalues
4708 static struct c_expr
4709 c_parser_unary_expression (c_parser *parser)
4712 struct c_expr ret, op;
4713 switch (c_parser_peek_token (parser)->type)
4716 c_parser_consume_token (parser);
4717 op = c_parser_cast_expression (parser, NULL);
4718 op = default_function_array_conversion (op);
4719 return parser_build_unary_op (PREINCREMENT_EXPR, op);
4720 case CPP_MINUS_MINUS:
4721 c_parser_consume_token (parser);
4722 op = c_parser_cast_expression (parser, NULL);
4723 op = default_function_array_conversion (op);
4724 return parser_build_unary_op (PREDECREMENT_EXPR, op);
4726 c_parser_consume_token (parser);
4727 return parser_build_unary_op (ADDR_EXPR,
4728 c_parser_cast_expression (parser, NULL));
4730 c_parser_consume_token (parser);
4731 op = c_parser_cast_expression (parser, NULL);
4732 op = default_function_array_conversion (op);
4733 ret.value = build_indirect_ref (op.value, "unary *");
4734 ret.original_code = ERROR_MARK;
4737 c_parser_consume_token (parser);
4738 if (!c_dialect_objc () && !in_system_header)
4739 warning (OPT_Wtraditional,
4740 "traditional C rejects the unary plus operator");
4741 op = c_parser_cast_expression (parser, NULL);
4742 op = default_function_array_conversion (op);
4743 return parser_build_unary_op (CONVERT_EXPR, op);
4745 c_parser_consume_token (parser);
4746 op = c_parser_cast_expression (parser, NULL);
4747 op = default_function_array_conversion (op);
4748 return parser_build_unary_op (NEGATE_EXPR, op);
4750 c_parser_consume_token (parser);
4751 op = c_parser_cast_expression (parser, NULL);
4752 op = default_function_array_conversion (op);
4753 return parser_build_unary_op (BIT_NOT_EXPR, op);
4755 c_parser_consume_token (parser);
4756 op = c_parser_cast_expression (parser, NULL);
4757 op = default_function_array_conversion (op);
4758 return parser_build_unary_op (TRUTH_NOT_EXPR, op);
4760 /* Refer to the address of a label as a pointer. */
4761 c_parser_consume_token (parser);
4762 if (c_parser_next_token_is (parser, CPP_NAME))
4764 ret.value = finish_label_address_expr
4765 (c_parser_peek_token (parser)->value);
4766 c_parser_consume_token (parser);
4770 c_parser_error (parser, "expected identifier");
4771 ret.value = error_mark_node;
4773 ret.original_code = ERROR_MARK;
4776 switch (c_parser_peek_token (parser)->keyword)
4779 return c_parser_sizeof_expression (parser);
4781 return c_parser_alignof_expression (parser);
4783 c_parser_consume_token (parser);
4784 ext = disable_extension_diagnostics ();
4785 ret = c_parser_cast_expression (parser, NULL);
4786 restore_extension_diagnostics (ext);
4789 c_parser_consume_token (parser);
4790 op = c_parser_cast_expression (parser, NULL);
4791 op = default_function_array_conversion (op);
4792 return parser_build_unary_op (REALPART_EXPR, op);
4794 c_parser_consume_token (parser);
4795 op = c_parser_cast_expression (parser, NULL);
4796 op = default_function_array_conversion (op);
4797 return parser_build_unary_op (IMAGPART_EXPR, op);
4799 return c_parser_postfix_expression (parser);
4802 return c_parser_postfix_expression (parser);
4806 /* Parse a sizeof expression. */
4808 static struct c_expr
4809 c_parser_sizeof_expression (c_parser *parser)
4812 gcc_assert (c_parser_next_token_is_keyword (parser, RID_SIZEOF));
4813 c_parser_consume_token (parser);
4816 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
4817 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
4819 /* Either sizeof ( type-name ) or sizeof unary-expression
4820 starting with a compound literal. */
4821 struct c_type_name *type_name;
4822 c_parser_consume_token (parser);
4823 type_name = c_parser_type_name (parser);
4824 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
4825 if (type_name == NULL)
4830 ret.value = error_mark_node;
4831 ret.original_code = ERROR_MARK;
4834 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
4836 expr = c_parser_postfix_expression_after_paren_type (parser,
4840 /* sizeof ( type-name ). */
4843 return c_expr_sizeof_type (type_name);
4847 expr = c_parser_unary_expression (parser);
4851 if (TREE_CODE (expr.value) == COMPONENT_REF
4852 && DECL_C_BIT_FIELD (TREE_OPERAND (expr.value, 1)))
4853 error ("%<sizeof%> applied to a bit-field");
4854 return c_expr_sizeof_expr (expr);
4858 /* Parse an alignof expression. */
4860 static struct c_expr
4861 c_parser_alignof_expression (c_parser *parser)
4864 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ALIGNOF));
4865 c_parser_consume_token (parser);
4868 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
4869 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
4871 /* Either __alignof__ ( type-name ) or __alignof__
4872 unary-expression starting with a compound literal. */
4873 struct c_type_name *type_name;
4875 c_parser_consume_token (parser);
4876 type_name = c_parser_type_name (parser);
4877 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
4878 if (type_name == NULL)
4883 ret.value = error_mark_node;
4884 ret.original_code = ERROR_MARK;
4887 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
4889 expr = c_parser_postfix_expression_after_paren_type (parser,
4893 /* alignof ( type-name ). */
4896 ret.value = c_alignof (groktypename (type_name));
4897 ret.original_code = ERROR_MARK;
4903 expr = c_parser_unary_expression (parser);
4907 ret.value = c_alignof_expr (expr.value);
4908 ret.original_code = ERROR_MARK;
4913 /* Parse a postfix expression (C90 6.3.1-6.3.2, C99 6.5.1-6.5.2).
4917 postfix-expression [ expression ]
4918 postfix-expression ( argument-expression-list[opt] )
4919 postfix-expression . identifier
4920 postfix-expression -> identifier
4921 postfix-expression ++
4922 postfix-expression --
4923 ( type-name ) { initializer-list }
4924 ( type-name ) { initializer-list , }
4926 argument-expression-list:
4928 argument-expression-list , argument-expression
4940 (treated as a keyword in GNU C)
4943 ( compound-statement )
4944 __builtin_va_arg ( assignment-expression , type-name )
4945 __builtin_offsetof ( type-name , offsetof-member-designator )
4946 __builtin_choose_expr ( assignment-expression ,
4947 assignment-expression ,
4948 assignment-expression )
4949 __builtin_types_compatible_p ( type-name , type-name )
4951 offsetof-member-designator:
4953 offsetof-member-designator . identifier
4954 offsetof-member-designator [ expression ]
4959 [ objc-receiver objc-message-args ]
4960 @selector ( objc-selector-arg )
4961 @protocol ( identifier )
4962 @encode ( type-name )
4966 static struct c_expr
4967 c_parser_postfix_expression (c_parser *parser)
4969 struct c_expr expr, e1, e2, e3;
4970 struct c_type_name *t1, *t2;
4971 switch (c_parser_peek_token (parser)->type)
4976 expr.value = c_parser_peek_token (parser)->value;
4977 expr.original_code = ERROR_MARK;
4978 c_parser_consume_token (parser);
4982 expr.value = c_parser_peek_token (parser)->value;
4983 expr.original_code = STRING_CST;
4984 c_parser_consume_token (parser);
4986 case CPP_OBJC_STRING:
4987 gcc_assert (c_dialect_objc ());
4989 = objc_build_string_object (c_parser_peek_token (parser)->value);
4990 expr.original_code = ERROR_MARK;
4991 c_parser_consume_token (parser);
4994 if (c_parser_peek_token (parser)->id_kind != C_ID_ID)
4996 c_parser_error (parser, "expected expression");
4997 expr.value = error_mark_node;
4998 expr.original_code = ERROR_MARK;
5002 tree id = c_parser_peek_token (parser)->value;
5003 location_t loc = c_parser_peek_token (parser)->location;
5004 c_parser_consume_token (parser);
5005 expr.value = build_external_ref (id,
5006 (c_parser_peek_token (parser)->type
5007 == CPP_OPEN_PAREN), loc);
5008 expr.original_code = ERROR_MARK;
5011 case CPP_OPEN_PAREN:
5012 /* A parenthesized expression, statement expression or compound
5014 if (c_parser_peek_2nd_token (parser)->type == CPP_OPEN_BRACE)
5016 /* A statement expression. */
5018 c_parser_consume_token (parser);
5019 c_parser_consume_token (parser);
5020 if (cur_stmt_list == NULL)
5022 error ("braced-group within expression allowed "
5023 "only inside a function");
5024 parser->error = true;
5025 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
5026 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5027 expr.value = error_mark_node;
5028 expr.original_code = ERROR_MARK;
5031 stmt = c_begin_stmt_expr ();
5032 c_parser_compound_statement_nostart (parser);
5033 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5036 pedwarn ("ISO C forbids braced-groups within expressions");
5037 expr.value = c_finish_stmt_expr (stmt);
5038 expr.original_code = ERROR_MARK;
5040 else if (c_token_starts_typename (c_parser_peek_2nd_token (parser)))
5042 /* A compound literal. ??? Can we actually get here rather
5043 than going directly to
5044 c_parser_postfix_expression_after_paren_type from
5046 struct c_type_name *type_name;
5047 c_parser_consume_token (parser);
5048 type_name = c_parser_type_name (parser);
5049 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5051 if (type_name == NULL)
5053 expr.value = error_mark_node;
5054 expr.original_code = ERROR_MARK;
5057 expr = c_parser_postfix_expression_after_paren_type (parser,
5062 /* A parenthesized expression. */
5063 c_parser_consume_token (parser);
5064 expr = c_parser_expression (parser);
5065 if (TREE_CODE (expr.value) == MODIFY_EXPR)
5066 TREE_NO_WARNING (expr.value) = 1;
5067 expr.original_code = ERROR_MARK;
5068 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5073 switch (c_parser_peek_token (parser)->keyword)
5075 case RID_FUNCTION_NAME:
5076 case RID_PRETTY_FUNCTION_NAME:
5077 case RID_C99_FUNCTION_NAME:
5078 expr.value = fname_decl (c_parser_peek_token (parser)->keyword,
5079 c_parser_peek_token (parser)->value);
5080 expr.original_code = ERROR_MARK;
5081 c_parser_consume_token (parser);
5084 c_parser_consume_token (parser);
5085 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5087 expr.value = error_mark_node;
5088 expr.original_code = ERROR_MARK;
5091 e1 = c_parser_expr_no_commas (parser, NULL);
5092 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
5094 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5095 expr.value = error_mark_node;
5096 expr.original_code = ERROR_MARK;
5099 t1 = c_parser_type_name (parser);
5100 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5104 expr.value = error_mark_node;
5105 expr.original_code = ERROR_MARK;
5109 expr.value = build_va_arg (e1.value, groktypename (t1));
5110 expr.original_code = ERROR_MARK;
5114 c_parser_consume_token (parser);
5115 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5117 expr.value = error_mark_node;
5118 expr.original_code = ERROR_MARK;
5121 t1 = c_parser_type_name (parser);
5124 expr.value = error_mark_node;
5125 expr.original_code = ERROR_MARK;
5128 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
5130 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5131 expr.value = error_mark_node;
5132 expr.original_code = ERROR_MARK;
5136 tree type = groktypename (t1);
5138 if (type == error_mark_node)
5139 offsetof_ref = error_mark_node;
5141 offsetof_ref = build1 (INDIRECT_REF, type, NULL);
5142 /* Parse the second argument to __builtin_offsetof. We
5143 must have one identifier, and beyond that we want to
5144 accept sub structure and sub array references. */
5145 if (c_parser_next_token_is (parser, CPP_NAME))
5147 offsetof_ref = build_component_ref
5148 (offsetof_ref, c_parser_peek_token (parser)->value);
5149 c_parser_consume_token (parser);
5150 while (c_parser_next_token_is (parser, CPP_DOT)
5151 || c_parser_next_token_is (parser,
5154 if (c_parser_next_token_is (parser, CPP_DOT))
5156 c_parser_consume_token (parser);
5157 if (c_parser_next_token_is_not (parser,
5160 c_parser_error (parser, "expected identifier");
5163 offsetof_ref = build_component_ref
5165 c_parser_peek_token (parser)->value);
5166 c_parser_consume_token (parser);
5171 c_parser_consume_token (parser);
5172 idx = c_parser_expression (parser).value;
5173 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
5175 offsetof_ref = build_array_ref (offsetof_ref, idx);
5180 c_parser_error (parser, "expected identifier");
5181 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5183 expr.value = fold_offsetof (offsetof_ref);
5184 expr.original_code = ERROR_MARK;
5187 case RID_CHOOSE_EXPR:
5188 c_parser_consume_token (parser);
5189 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5191 expr.value = error_mark_node;
5192 expr.original_code = ERROR_MARK;
5195 e1 = c_parser_expr_no_commas (parser, NULL);
5196 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
5198 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5199 expr.value = error_mark_node;
5200 expr.original_code = ERROR_MARK;
5203 e2 = c_parser_expr_no_commas (parser, NULL);
5204 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
5206 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5207 expr.value = error_mark_node;
5208 expr.original_code = ERROR_MARK;
5211 e3 = c_parser_expr_no_commas (parser, NULL);
5212 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5217 c = fold (e1.value);
5218 if (TREE_CODE (c) != INTEGER_CST)
5219 error ("first argument to %<__builtin_choose_expr%> not"
5221 expr = integer_zerop (c) ? e3 : e2;
5224 case RID_TYPES_COMPATIBLE_P:
5225 c_parser_consume_token (parser);
5226 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5228 expr.value = error_mark_node;
5229 expr.original_code = ERROR_MARK;
5232 t1 = c_parser_type_name (parser);
5235 expr.value = error_mark_node;
5236 expr.original_code = ERROR_MARK;
5239 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
5241 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5242 expr.value = error_mark_node;
5243 expr.original_code = ERROR_MARK;
5246 t2 = c_parser_type_name (parser);
5249 expr.value = error_mark_node;
5250 expr.original_code = ERROR_MARK;
5253 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5258 e1 = TYPE_MAIN_VARIANT (groktypename (t1));
5259 e2 = TYPE_MAIN_VARIANT (groktypename (t2));
5261 expr.value = comptypes (e1, e2)
5262 ? build_int_cst (NULL_TREE, 1)
5263 : build_int_cst (NULL_TREE, 0);
5264 expr.original_code = ERROR_MARK;
5267 case RID_AT_SELECTOR:
5268 gcc_assert (c_dialect_objc ());
5269 c_parser_consume_token (parser);
5270 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5272 expr.value = error_mark_node;
5273 expr.original_code = ERROR_MARK;
5277 tree sel = c_parser_objc_selector_arg (parser);
5278 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5280 expr.value = objc_build_selector_expr (sel);
5281 expr.original_code = ERROR_MARK;
5284 case RID_AT_PROTOCOL:
5285 gcc_assert (c_dialect_objc ());
5286 c_parser_consume_token (parser);
5287 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5289 expr.value = error_mark_node;
5290 expr.original_code = ERROR_MARK;
5293 if (c_parser_next_token_is_not (parser, CPP_NAME))
5295 c_parser_error (parser, "expected identifier");
5296 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5297 expr.value = error_mark_node;
5298 expr.original_code = ERROR_MARK;
5302 tree id = c_parser_peek_token (parser)->value;
5303 c_parser_consume_token (parser);
5304 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5306 expr.value = objc_build_protocol_expr (id);
5307 expr.original_code = ERROR_MARK;
5311 /* Extension to support C-structures in the archiver. */
5312 gcc_assert (c_dialect_objc ());
5313 c_parser_consume_token (parser);
5314 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5316 expr.value = error_mark_node;
5317 expr.original_code = ERROR_MARK;
5320 t1 = c_parser_type_name (parser);
5323 expr.value = error_mark_node;
5324 expr.original_code = ERROR_MARK;
5325 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5328 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5331 tree type = groktypename (t1);
5332 expr.value = objc_build_encode_expr (type);
5333 expr.original_code = ERROR_MARK;
5337 c_parser_error (parser, "expected expression");
5338 expr.value = error_mark_node;
5339 expr.original_code = ERROR_MARK;
5343 case CPP_OPEN_SQUARE:
5344 if (c_dialect_objc ())
5346 tree receiver, args;
5347 c_parser_consume_token (parser);
5348 receiver = c_parser_objc_receiver (parser);
5349 args = c_parser_objc_message_args (parser);
5350 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
5352 expr.value = objc_build_message_expr (build_tree_list (receiver,
5354 expr.original_code = ERROR_MARK;
5357 /* Else fall through to report error. */
5359 c_parser_error (parser, "expected expression");
5360 expr.value = error_mark_node;
5361 expr.original_code = ERROR_MARK;
5364 return c_parser_postfix_expression_after_primary (parser, expr);
5367 /* Parse a postfix expression after a parenthesized type name: the
5368 brace-enclosed initializer of a compound literal, possibly followed
5369 by some postfix operators. This is separate because it is not
5370 possible to tell until after the type name whether a cast
5371 expression has a cast or a compound literal, or whether the operand
5372 of sizeof is a parenthesized type name or starts with a compound
5375 static struct c_expr
5376 c_parser_postfix_expression_after_paren_type (c_parser *parser,
5377 struct c_type_name *type_name)
5382 start_init (NULL_TREE, NULL, 0);
5383 type = groktypename (type_name);
5384 if (C_TYPE_VARIABLE_SIZE (type))
5386 error ("compound literal has variable size");
5387 type = error_mark_node;
5389 init = c_parser_braced_init (parser, type, false);
5391 maybe_warn_string_init (type, init);
5393 if (pedantic && !flag_isoc99)
5394 pedwarn ("ISO C90 forbids compound literals");
5395 expr.value = build_compound_literal (type, init.value);
5396 expr.original_code = ERROR_MARK;
5397 return c_parser_postfix_expression_after_primary (parser, expr);
5400 /* Parse a postfix expression after the initial primary or compound
5401 literal; that is, parse a series of postfix operators. */
5403 static struct c_expr
5404 c_parser_postfix_expression_after_primary (c_parser *parser,
5407 tree ident, idx, exprlist;
5410 switch (c_parser_peek_token (parser)->type)
5412 case CPP_OPEN_SQUARE:
5413 /* Array reference. */
5414 c_parser_consume_token (parser);
5415 idx = c_parser_expression (parser).value;
5416 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
5418 expr.value = build_array_ref (expr.value, idx);
5419 expr.original_code = ERROR_MARK;
5421 case CPP_OPEN_PAREN:
5422 /* Function call. */
5423 c_parser_consume_token (parser);
5424 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
5425 exprlist = NULL_TREE;
5427 exprlist = c_parser_expr_list (parser, true);
5428 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5430 expr.value = build_function_call (expr.value, exprlist);
5431 expr.original_code = ERROR_MARK;
5434 /* Structure element reference. */
5435 c_parser_consume_token (parser);
5436 expr = default_function_array_conversion (expr);
5437 if (c_parser_next_token_is (parser, CPP_NAME))
5438 ident = c_parser_peek_token (parser)->value;
5441 c_parser_error (parser, "expected identifier");
5442 expr.value = error_mark_node;
5443 expr.original_code = ERROR_MARK;
5446 c_parser_consume_token (parser);
5447 expr.value = build_component_ref (expr.value, ident);
5448 expr.original_code = ERROR_MARK;
5451 /* Structure element reference. */
5452 c_parser_consume_token (parser);
5453 expr = default_function_array_conversion (expr);
5454 if (c_parser_next_token_is (parser, CPP_NAME))
5455 ident = c_parser_peek_token (parser)->value;
5458 c_parser_error (parser, "expected identifier");
5459 expr.value = error_mark_node;
5460 expr.original_code = ERROR_MARK;
5463 c_parser_consume_token (parser);
5464 expr.value = build_component_ref (build_indirect_ref (expr.value,
5466 expr.original_code = ERROR_MARK;
5469 /* Postincrement. */
5470 c_parser_consume_token (parser);
5471 expr = default_function_array_conversion (expr);
5472 expr.value = build_unary_op (POSTINCREMENT_EXPR, expr.value, 0);
5473 expr.original_code = ERROR_MARK;
5475 case CPP_MINUS_MINUS:
5476 /* Postdecrement. */
5477 c_parser_consume_token (parser);
5478 expr = default_function_array_conversion (expr);
5479 expr.value = build_unary_op (POSTDECREMENT_EXPR, expr.value, 0);
5480 expr.original_code = ERROR_MARK;
5488 /* Parse an expression (C90 6.3.17, C99 6.5.17).
5491 assignment-expression
5492 expression , assignment-expression
5495 static struct c_expr
5496 c_parser_expression (c_parser *parser)
5499 expr = c_parser_expr_no_commas (parser, NULL);
5500 while (c_parser_next_token_is (parser, CPP_COMMA))
5503 c_parser_consume_token (parser);
5504 next = c_parser_expr_no_commas (parser, NULL);
5505 next = default_function_array_conversion (next);
5506 expr.value = build_compound_expr (expr.value, next.value);
5507 expr.original_code = COMPOUND_EXPR;
5512 /* Parse an expression and convert functions or arrays to
5515 static struct c_expr
5516 c_parser_expression_conv (c_parser *parser)
5519 expr = c_parser_expression (parser);
5520 expr = default_function_array_conversion (expr);
5524 /* Parse a non-empty list of expressions. If CONVERT_P, convert
5525 functions and arrays to pointers.
5528 assignment-expression
5529 nonempty-expr-list , assignment-expression
5533 c_parser_expr_list (c_parser *parser, bool convert_p)
5537 expr = c_parser_expr_no_commas (parser, NULL);
5539 expr = default_function_array_conversion (expr);
5540 ret = cur = build_tree_list (NULL_TREE, expr.value);
5541 while (c_parser_next_token_is (parser, CPP_COMMA))
5543 c_parser_consume_token (parser);
5544 expr = c_parser_expr_no_commas (parser, NULL);
5546 expr = default_function_array_conversion (expr);
5547 cur = TREE_CHAIN (cur) = build_tree_list (NULL_TREE, expr.value);
5553 /* Parse Objective-C-specific constructs. */
5555 /* Parse an objc-class-definition.
5557 objc-class-definition:
5558 @interface identifier objc-superclass[opt] objc-protocol-refs[opt]
5559 objc-class-instance-variables[opt] objc-methodprotolist @end
5560 @implementation identifier objc-superclass[opt]
5561 objc-class-instance-variables[opt]
5562 @interface identifier ( identifier ) objc-protocol-refs[opt]
5563 objc-methodprotolist @end
5564 @implementation identifier ( identifier )
5569 "@interface identifier (" must start "@interface identifier (
5570 identifier ) ...": objc-methodprotolist in the first production may
5571 not start with a parenthesized identifier as a declarator of a data
5572 definition with no declaration specifiers if the objc-superclass,
5573 objc-protocol-refs and objc-class-instance-variables are omitted. */
5576 c_parser_objc_class_definition (c_parser *parser)
5581 if (c_parser_next_token_is_keyword (parser, RID_AT_INTERFACE))
5583 else if (c_parser_next_token_is_keyword (parser, RID_AT_IMPLEMENTATION))
5587 c_parser_consume_token (parser);
5588 if (c_parser_next_token_is_not (parser, CPP_NAME))
5590 c_parser_error (parser, "expected identifier");
5593 id1 = c_parser_peek_token (parser)->value;
5594 c_parser_consume_token (parser);
5595 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
5598 tree proto = NULL_TREE;
5599 c_parser_consume_token (parser);
5600 if (c_parser_next_token_is_not (parser, CPP_NAME))
5602 c_parser_error (parser, "expected identifier");
5603 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5606 id2 = c_parser_peek_token (parser)->value;
5607 c_parser_consume_token (parser);
5608 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
5611 objc_start_category_implementation (id1, id2);
5614 if (c_parser_next_token_is (parser, CPP_LESS))
5615 proto = c_parser_objc_protocol_refs (parser);
5616 objc_start_category_interface (id1, id2, proto);
5617 c_parser_objc_methodprotolist (parser);
5618 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
5619 objc_finish_interface ();
5622 if (c_parser_next_token_is (parser, CPP_COLON))
5624 c_parser_consume_token (parser);
5625 if (c_parser_next_token_is_not (parser, CPP_NAME))
5627 c_parser_error (parser, "expected identifier");
5630 superclass = c_parser_peek_token (parser)->value;
5631 c_parser_consume_token (parser);
5634 superclass = NULL_TREE;
5637 tree proto = NULL_TREE;
5638 if (c_parser_next_token_is (parser, CPP_LESS))
5639 proto = c_parser_objc_protocol_refs (parser);
5640 objc_start_class_interface (id1, superclass, proto);
5643 objc_start_class_implementation (id1, superclass);
5644 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
5645 c_parser_objc_class_instance_variables (parser);
5648 objc_continue_interface ();
5649 c_parser_objc_methodprotolist (parser);
5650 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
5651 objc_finish_interface ();
5655 objc_continue_implementation ();
5660 /* Parse objc-class-instance-variables.
5662 objc-class-instance-variables:
5663 { objc-instance-variable-decl-list[opt] }
5665 objc-instance-variable-decl-list:
5666 objc-visibility-spec
5667 objc-instance-variable-decl ;
5669 objc-instance-variable-decl-list objc-visibility-spec
5670 objc-instance-variable-decl-list objc-instance-variable-decl ;
5671 objc-instance-variable-decl-list ;
5673 objc-visibility-spec:
5678 objc-instance-variable-decl:
5683 c_parser_objc_class_instance_variables (c_parser *parser)
5685 gcc_assert (c_parser_next_token_is (parser, CPP_OPEN_BRACE));
5686 c_parser_consume_token (parser);
5687 while (c_parser_next_token_is_not (parser, CPP_EOF))
5690 /* Parse any stray semicolon. */
5691 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5694 pedwarn ("extra semicolon in struct or union specified");
5695 c_parser_consume_token (parser);
5698 /* Stop if at the end of the instance variables. */
5699 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
5701 c_parser_consume_token (parser);
5704 /* Parse any objc-visibility-spec. */
5705 if (c_parser_next_token_is_keyword (parser, RID_AT_PRIVATE))
5707 c_parser_consume_token (parser);
5708 objc_set_visibility (2);
5711 else if (c_parser_next_token_is_keyword (parser, RID_AT_PROTECTED))
5713 c_parser_consume_token (parser);
5714 objc_set_visibility (0);
5717 else if (c_parser_next_token_is_keyword (parser, RID_AT_PUBLIC))
5719 c_parser_consume_token (parser);
5720 objc_set_visibility (1);
5723 else if (c_parser_next_token_is (parser, CPP_PRAGMA))
5725 c_parser_pragma (parser, pragma_external);
5729 /* Parse some comma-separated declarations. */
5730 decls = c_parser_struct_declaration (parser);
5732 /* Comma-separated instance variables are chained together in
5733 reverse order; add them one by one. */
5734 tree ivar = nreverse (decls);
5735 for (; ivar; ivar = TREE_CHAIN (ivar))
5736 objc_add_instance_variable (copy_node (ivar));
5738 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
5742 /* Parse an objc-class-declaration.
5744 objc-class-declaration:
5745 @class identifier-list ;
5749 c_parser_objc_class_declaration (c_parser *parser)
5751 tree list = NULL_TREE;
5752 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_CLASS));
5753 c_parser_consume_token (parser);
5754 /* Any identifiers, including those declared as type names, are OK
5759 if (c_parser_next_token_is_not (parser, CPP_NAME))
5761 c_parser_error (parser, "expected identifier");
5764 id = c_parser_peek_token (parser)->value;
5765 list = chainon (list, build_tree_list (NULL_TREE, id));
5766 c_parser_consume_token (parser);
5767 if (c_parser_next_token_is (parser, CPP_COMMA))
5768 c_parser_consume_token (parser);
5772 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
5773 objc_declare_class (list);
5776 /* Parse an objc-alias-declaration.
5778 objc-alias-declaration:
5779 @compatibility_alias identifier identifier ;
5783 c_parser_objc_alias_declaration (c_parser *parser)
5786 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_ALIAS));
5787 c_parser_consume_token (parser);
5788 if (c_parser_next_token_is_not (parser, CPP_NAME))
5790 c_parser_error (parser, "expected identifier");
5791 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
5794 id1 = c_parser_peek_token (parser)->value;
5795 c_parser_consume_token (parser);
5796 if (c_parser_next_token_is_not (parser, CPP_NAME))
5798 c_parser_error (parser, "expected identifier");
5799 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
5802 id2 = c_parser_peek_token (parser)->value;
5803 c_parser_consume_token (parser);
5804 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
5805 objc_declare_alias (id1, id2);
5808 /* Parse an objc-protocol-definition.
5810 objc-protocol-definition:
5811 @protocol identifier objc-protocol-refs[opt] objc-methodprotolist @end
5812 @protocol identifier-list ;
5814 "@protocol identifier ;" should be resolved as "@protocol
5815 identifier-list ;": objc-methodprotolist may not start with a
5816 semicolon in the first alternative if objc-protocol-refs are
5820 c_parser_objc_protocol_definition (c_parser *parser)
5822 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_PROTOCOL));
5823 c_parser_consume_token (parser);
5824 if (c_parser_next_token_is_not (parser, CPP_NAME))
5826 c_parser_error (parser, "expected identifier");
5829 if (c_parser_peek_2nd_token (parser)->type == CPP_COMMA
5830 || c_parser_peek_2nd_token (parser)->type == CPP_SEMICOLON)
5832 tree list = NULL_TREE;
5833 /* Any identifiers, including those declared as type names, are
5838 if (c_parser_next_token_is_not (parser, CPP_NAME))
5840 c_parser_error (parser, "expected identifier");
5843 id = c_parser_peek_token (parser)->value;
5844 list = chainon (list, build_tree_list (NULL_TREE, id));
5845 c_parser_consume_token (parser);
5846 if (c_parser_next_token_is (parser, CPP_COMMA))
5847 c_parser_consume_token (parser);
5851 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
5852 objc_declare_protocols (list);
5856 tree id = c_parser_peek_token (parser)->value;
5857 tree proto = NULL_TREE;
5858 c_parser_consume_token (parser);
5859 if (c_parser_next_token_is (parser, CPP_LESS))
5860 proto = c_parser_objc_protocol_refs (parser);
5861 objc_pq_context = 1;
5862 objc_start_protocol (id, proto);
5863 c_parser_objc_methodprotolist (parser);
5864 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
5865 objc_pq_context = 0;
5866 objc_finish_interface ();
5870 /* Parse an objc-method-type.
5877 static enum tree_code
5878 c_parser_objc_method_type (c_parser *parser)
5880 switch (c_parser_peek_token (parser)->type)
5883 c_parser_consume_token (parser);
5886 c_parser_consume_token (parser);
5893 /* Parse an objc-method-definition.
5895 objc-method-definition:
5896 objc-method-type objc-method-decl ;[opt] compound-statement
5900 c_parser_objc_method_definition (c_parser *parser)
5902 enum tree_code type = c_parser_objc_method_type (parser);
5904 objc_set_method_type (type);
5905 objc_pq_context = 1;
5906 decl = c_parser_objc_method_decl (parser);
5907 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5909 c_parser_consume_token (parser);
5911 pedwarn ("extra semicolon in method definition specified");
5913 if (!c_parser_next_token_is (parser, CPP_OPEN_BRACE))
5915 c_parser_error (parser, "expected %<{%>");
5918 objc_pq_context = 0;
5919 objc_start_method_definition (decl);
5920 add_stmt (c_parser_compound_statement (parser));
5921 objc_finish_method_definition (current_function_decl);
5924 /* Parse an objc-methodprotolist.
5926 objc-methodprotolist:
5928 objc-methodprotolist objc-methodproto
5929 objc-methodprotolist declaration
5930 objc-methodprotolist ;
5932 The declaration is a data definition, which may be missing
5933 declaration specifiers under the same rules and diagnostics as
5934 other data definitions outside functions, and the stray semicolon
5935 is diagnosed the same way as a stray semicolon outside a
5939 c_parser_objc_methodprotolist (c_parser *parser)
5943 /* The list is terminated by @end. */
5944 switch (c_parser_peek_token (parser)->type)
5948 pedwarn ("ISO C does not allow extra %<;%> outside of a function");
5949 c_parser_consume_token (parser);
5953 c_parser_objc_methodproto (parser);
5956 c_parser_pragma (parser, pragma_external);
5961 if (c_parser_next_token_is_keyword (parser, RID_AT_END))
5963 c_parser_declaration_or_fndef (parser, false, true, false, true);
5969 /* Parse an objc-methodproto.
5972 objc-method-type objc-method-decl ;
5976 c_parser_objc_methodproto (c_parser *parser)
5978 enum tree_code type = c_parser_objc_method_type (parser);
5980 objc_set_method_type (type);
5981 /* Remember protocol qualifiers in prototypes. */
5982 objc_pq_context = 1;
5983 decl = c_parser_objc_method_decl (parser);
5984 /* Forget protocol qualifiers here. */
5985 objc_pq_context = 0;
5986 objc_add_method_declaration (decl);
5987 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
5990 /* Parse an objc-method-decl.
5993 ( objc-type-name ) objc-selector
5995 ( objc-type-name ) objc-keyword-selector objc-optparmlist
5996 objc-keyword-selector objc-optparmlist
5998 objc-keyword-selector:
6000 objc-keyword-selector objc-keyword-decl
6003 objc-selector : ( objc-type-name ) identifier
6004 objc-selector : identifier
6005 : ( objc-type-name ) identifier
6009 objc-optparms objc-optellipsis
6013 objc-opt-parms , parameter-declaration
6021 c_parser_objc_method_decl (c_parser *parser)
6023 tree type = NULL_TREE;
6025 tree parms = NULL_TREE;
6026 bool ellipsis = false;
6028 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
6030 c_parser_consume_token (parser);
6031 type = c_parser_objc_type_name (parser);
6032 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
6034 sel = c_parser_objc_selector (parser);
6035 /* If there is no selector, or a colon follows, we have an
6036 objc-keyword-selector. If there is a selector, and a colon does
6037 not follow, that selector ends the objc-method-decl. */
6038 if (!sel || c_parser_next_token_is (parser, CPP_COLON))
6041 tree list = NULL_TREE;
6044 tree atype = NULL_TREE, id, keyworddecl;
6045 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
6047 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
6049 c_parser_consume_token (parser);
6050 atype = c_parser_objc_type_name (parser);
6051 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
6054 if (c_parser_next_token_is_not (parser, CPP_NAME))
6056 c_parser_error (parser, "expected identifier");
6057 return error_mark_node;
6059 id = c_parser_peek_token (parser)->value;
6060 c_parser_consume_token (parser);
6061 keyworddecl = objc_build_keyword_decl (tsel, atype, id);
6062 list = chainon (list, keyworddecl);
6063 tsel = c_parser_objc_selector (parser);
6064 if (!tsel && c_parser_next_token_is_not (parser, CPP_COLON))
6067 /* Parse the optional parameter list. Optional Objective-C
6068 method parameters follow the C syntax, and may include '...'
6069 to denote a variable number of arguments. */
6070 parms = make_node (TREE_LIST);
6071 while (c_parser_next_token_is (parser, CPP_COMMA))
6073 struct c_parm *parm;
6074 c_parser_consume_token (parser);
6075 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
6078 c_parser_consume_token (parser);
6081 parm = c_parser_parameter_declaration (parser, NULL_TREE);
6084 parms = chainon (parms,
6085 build_tree_list (NULL_TREE, grokparm (parm)));
6089 return objc_build_method_signature (type, sel, parms, ellipsis);
6092 /* Parse an objc-type-name.
6095 objc-type-qualifiers[opt] type-name
6096 objc-type-qualifiers[opt]
6098 objc-type-qualifiers:
6100 objc-type-qualifiers objc-type-qualifier
6102 objc-type-qualifier: one of
6103 in out inout bycopy byref oneway
6107 c_parser_objc_type_name (c_parser *parser)
6109 tree quals = NULL_TREE;
6110 struct c_type_name *typename = NULL;
6111 tree type = NULL_TREE;
6114 c_token *token = c_parser_peek_token (parser);
6115 if (token->type == CPP_KEYWORD
6116 && (token->keyword == RID_IN
6117 || token->keyword == RID_OUT
6118 || token->keyword == RID_INOUT
6119 || token->keyword == RID_BYCOPY
6120 || token->keyword == RID_BYREF
6121 || token->keyword == RID_ONEWAY))
6123 quals = chainon (quals, build_tree_list (NULL_TREE, token->value));
6124 c_parser_consume_token (parser);
6129 if (c_parser_next_token_starts_typename (parser))
6130 typename = c_parser_type_name (parser);
6132 type = groktypename (typename);
6133 return build_tree_list (quals, type);
6136 /* Parse objc-protocol-refs.
6143 c_parser_objc_protocol_refs (c_parser *parser)
6145 tree list = NULL_TREE;
6146 gcc_assert (c_parser_next_token_is (parser, CPP_LESS));
6147 c_parser_consume_token (parser);
6148 /* Any identifiers, including those declared as type names, are OK
6153 if (c_parser_next_token_is_not (parser, CPP_NAME))
6155 c_parser_error (parser, "expected identifier");
6158 id = c_parser_peek_token (parser)->value;
6159 list = chainon (list, build_tree_list (NULL_TREE, id));
6160 c_parser_consume_token (parser);
6161 if (c_parser_next_token_is (parser, CPP_COMMA))
6162 c_parser_consume_token (parser);
6166 c_parser_require (parser, CPP_GREATER, "expected %<>%>");
6170 /* Parse an objc-try-catch-statement.
6172 objc-try-catch-statement:
6173 @try compound-statement objc-catch-list[opt]
6174 @try compound-statement objc-catch-list[opt] @finally compound-statement
6177 @catch ( parameter-declaration ) compound-statement
6178 objc-catch-list @catch ( parameter-declaration ) compound-statement
6182 c_parser_objc_try_catch_statement (c_parser *parser)
6186 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_TRY));
6187 c_parser_consume_token (parser);
6188 loc = c_parser_peek_token (parser)->location;
6189 stmt = c_parser_compound_statement (parser);
6190 objc_begin_try_stmt (loc, stmt);
6191 while (c_parser_next_token_is_keyword (parser, RID_AT_CATCH))
6193 struct c_parm *parm;
6194 c_parser_consume_token (parser);
6195 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6197 parm = c_parser_parameter_declaration (parser, NULL_TREE);
6200 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6203 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
6204 objc_begin_catch_clause (grokparm (parm));
6205 if (c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
6206 c_parser_compound_statement_nostart (parser);
6207 objc_finish_catch_clause ();
6209 if (c_parser_next_token_is_keyword (parser, RID_AT_FINALLY))
6213 c_parser_consume_token (parser);
6214 finloc = c_parser_peek_token (parser)->location;
6215 finstmt = c_parser_compound_statement (parser);
6216 objc_build_finally_clause (finloc, finstmt);
6218 objc_finish_try_stmt ();
6221 /* Parse an objc-synchronized-statement.
6223 objc-synchronized-statement:
6224 @synchronized ( expression ) compound-statement
6228 c_parser_objc_synchronized_statement (c_parser *parser)
6232 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_SYNCHRONIZED));
6233 c_parser_consume_token (parser);
6234 loc = c_parser_peek_token (parser)->location;
6235 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6237 expr = c_parser_expression (parser).value;
6238 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
6241 expr = error_mark_node;
6242 stmt = c_parser_compound_statement (parser);
6243 objc_build_synchronized (loc, expr, stmt);
6246 /* Parse an objc-selector; return NULL_TREE without an error if the
6247 next token is not an objc-selector.
6252 enum struct union if else while do for switch case default
6253 break continue return goto asm sizeof typeof __alignof
6254 unsigned long const short volatile signed restrict _Complex
6255 in out inout bycopy byref oneway int char float double void _Bool
6257 ??? Why this selection of keywords but not, for example, storage
6258 class specifiers? */
6261 c_parser_objc_selector (c_parser *parser)
6263 c_token *token = c_parser_peek_token (parser);
6264 tree value = token->value;
6265 if (token->type == CPP_NAME)
6267 c_parser_consume_token (parser);
6270 if (token->type != CPP_KEYWORD)
6272 switch (token->keyword)
6313 c_parser_consume_token (parser);
6320 /* Parse an objc-selector-arg.
6324 objc-keywordname-list
6326 objc-keywordname-list:
6328 objc-keywordname-list objc-keywordname
6336 c_parser_objc_selector_arg (c_parser *parser)
6338 tree sel = c_parser_objc_selector (parser);
6339 tree list = NULL_TREE;
6340 if (sel && c_parser_next_token_is_not (parser, CPP_COLON))
6344 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
6346 list = chainon (list, build_tree_list (sel, NULL_TREE));
6347 sel = c_parser_objc_selector (parser);
6348 if (!sel && c_parser_next_token_is_not (parser, CPP_COLON))
6354 /* Parse an objc-receiver.
6363 c_parser_objc_receiver (c_parser *parser)
6365 if (c_parser_peek_token (parser)->type == CPP_NAME
6366 && (c_parser_peek_token (parser)->id_kind == C_ID_TYPENAME
6367 || c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME))
6369 tree id = c_parser_peek_token (parser)->value;
6370 c_parser_consume_token (parser);
6371 return objc_get_class_reference (id);
6373 return c_parser_expression (parser).value;
6376 /* Parse objc-message-args.
6380 objc-keywordarg-list
6382 objc-keywordarg-list:
6384 objc-keywordarg-list objc-keywordarg
6387 objc-selector : objc-keywordexpr
6392 c_parser_objc_message_args (c_parser *parser)
6394 tree sel = c_parser_objc_selector (parser);
6395 tree list = NULL_TREE;
6396 if (sel && c_parser_next_token_is_not (parser, CPP_COLON))
6401 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
6403 keywordexpr = c_parser_objc_keywordexpr (parser);
6404 list = chainon (list, build_tree_list (sel, keywordexpr));
6405 sel = c_parser_objc_selector (parser);
6406 if (!sel && c_parser_next_token_is_not (parser, CPP_COLON))
6412 /* Parse an objc-keywordexpr.
6419 c_parser_objc_keywordexpr (c_parser *parser)
6421 tree list = c_parser_expr_list (parser, true);
6422 if (TREE_CHAIN (list) == NULL_TREE)
6424 /* Just return the expression, remove a level of
6426 return TREE_VALUE (list);
6430 /* We have a comma expression, we will collapse later. */
6436 /* Handle pragmas. Some OpenMP pragmas are associated with, and therefore
6437 should be considered, statements. ALLOW_STMT is true if we're within
6438 the context of a function and such pragmas are to be allowed. Returns
6439 true if we actually parsed such a pragma. */
6442 c_parser_pragma (c_parser *parser, enum pragma_context context)
6446 id = c_parser_peek_token (parser)->pragma_kind;
6447 gcc_assert (id != PRAGMA_NONE);
6451 case PRAGMA_OMP_BARRIER:
6452 if (context != pragma_compound)
6454 if (context == pragma_stmt)
6455 c_parser_error (parser, "%<#pragma omp barrier%> may only be "
6456 "used in compound statements");
6459 c_parser_omp_barrier (parser);
6462 case PRAGMA_OMP_FLUSH:
6463 if (context != pragma_compound)
6465 if (context == pragma_stmt)
6466 c_parser_error (parser, "%<#pragma omp flush%> may only be "
6467 "used in compound statements");
6470 c_parser_omp_flush (parser);
6473 case PRAGMA_OMP_THREADPRIVATE:
6474 c_parser_omp_threadprivate (parser);
6477 case PRAGMA_OMP_SECTION:
6478 error ("%<#pragma omp section%> may only be used in "
6479 "%<#pragma omp sections%> construct");
6480 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
6483 case PRAGMA_GCC_PCH_PREPROCESS:
6484 c_parser_error (parser, "%<#pragma GCC pch_preprocess%> must be first");
6485 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
6489 if (id < PRAGMA_FIRST_EXTERNAL)
6491 if (context == pragma_external)
6494 c_parser_error (parser, "expected declaration specifiers");
6495 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
6498 c_parser_omp_construct (parser);
6504 c_parser_consume_pragma (parser);
6505 c_invoke_pragma_handler (id);
6507 /* Skip to EOL, but suppress any error message. Those will have been
6508 generated by the handler routine through calling error, as opposed
6509 to calling c_parser_error. */
6510 parser->error = true;
6511 c_parser_skip_to_pragma_eol (parser);
6516 /* The interface the pragma parsers have to the lexer. */
6519 pragma_lex (tree *value)
6521 c_token *tok = c_parser_peek_token (the_parser);
6522 enum cpp_ttype ret = tok->type;
6524 *value = tok->value;
6525 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
6529 if (ret == CPP_KEYWORD)
6531 c_parser_consume_token (the_parser);
6538 c_parser_pragma_pch_preprocess (c_parser *parser)
6542 c_parser_consume_pragma (parser);
6543 if (c_parser_next_token_is (parser, CPP_STRING))
6545 name = c_parser_peek_token (parser)->value;
6546 c_parser_consume_token (parser);
6549 c_parser_error (parser, "expected string literal");
6550 c_parser_skip_to_pragma_eol (parser);
6553 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
6556 /* OpenMP 2.5 parsing routines. */
6558 /* Returns name of the next clause.
6559 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
6560 the token is not consumed. Otherwise appropriate pragma_omp_clause is
6561 returned and the token is consumed. */
6563 static pragma_omp_clause
6564 c_parser_omp_clause_name (c_parser *parser)
6566 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
6568 if (c_parser_next_token_is_keyword (parser, RID_IF))
6569 result = PRAGMA_OMP_CLAUSE_IF;
6570 else if (c_parser_next_token_is_keyword (parser, RID_DEFAULT))
6571 result = PRAGMA_OMP_CLAUSE_DEFAULT;
6572 else if (c_parser_next_token_is (parser, CPP_NAME))
6574 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
6579 if (!strcmp ("copyin", p))
6580 result = PRAGMA_OMP_CLAUSE_COPYIN;
6581 else if (!strcmp ("copyprivate", p))
6582 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
6585 if (!strcmp ("firstprivate", p))
6586 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
6589 if (!strcmp ("lastprivate", p))
6590 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
6593 if (!strcmp ("nowait", p))
6594 result = PRAGMA_OMP_CLAUSE_NOWAIT;
6595 else if (!strcmp ("num_threads", p))
6596 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
6599 if (!strcmp ("ordered", p))
6600 result = PRAGMA_OMP_CLAUSE_ORDERED;
6603 if (!strcmp ("private", p))
6604 result = PRAGMA_OMP_CLAUSE_PRIVATE;
6607 if (!strcmp ("reduction", p))
6608 result = PRAGMA_OMP_CLAUSE_REDUCTION;
6611 if (!strcmp ("schedule", p))
6612 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
6613 else if (!strcmp ("shared", p))
6614 result = PRAGMA_OMP_CLAUSE_SHARED;
6619 if (result != PRAGMA_OMP_CLAUSE_NONE)
6620 c_parser_consume_token (parser);
6625 /* Validate that a clause of the given type does not already exist. */
6628 check_no_duplicate_clause (tree clauses, enum tree_code code, const char *name)
6632 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
6633 if (OMP_CLAUSE_CODE (c) == code)
6635 error ("too many %qs clauses", name);
6643 variable-list , identifier
6645 If KIND is nonzero, create the appropriate node and install the decl
6646 in OMP_CLAUSE_DECL and add the node to the head of the list.
6648 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
6649 return the list created. */
6652 c_parser_omp_variable_list (c_parser *parser, enum omp_clause_code kind,
6655 if (c_parser_next_token_is_not (parser, CPP_NAME)
6656 || c_parser_peek_token (parser)->id_kind != C_ID_ID)
6657 c_parser_error (parser, "expected identifier");
6659 while (c_parser_next_token_is (parser, CPP_NAME)
6660 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
6662 tree t = lookup_name (c_parser_peek_token (parser)->value);
6665 undeclared_variable (c_parser_peek_token (parser)->value,
6666 c_parser_peek_token (parser)->location);
6667 else if (t == error_mark_node)
6671 tree u = build_omp_clause (kind);
6672 OMP_CLAUSE_DECL (u) = t;
6673 OMP_CLAUSE_CHAIN (u) = list;
6677 list = tree_cons (t, NULL_TREE, list);
6679 c_parser_consume_token (parser);
6681 if (c_parser_next_token_is_not (parser, CPP_COMMA))
6684 c_parser_consume_token (parser);
6690 /* Similarly, but expect leading and trailing parenthesis. This is a very
6691 common case for omp clauses. */
6694 c_parser_omp_var_list_parens (c_parser *parser, enum tree_code kind, tree list)
6696 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6698 list = c_parser_omp_variable_list (parser, kind, list);
6699 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
6705 copyin ( variable-list ) */
6708 c_parser_omp_clause_copyin (c_parser *parser, tree list)
6710 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_COPYIN, list);
6714 copyprivate ( variable-list ) */
6717 c_parser_omp_clause_copyprivate (c_parser *parser, tree list)
6719 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_COPYPRIVATE, list);
6723 default ( shared | none ) */
6726 c_parser_omp_clause_default (c_parser *parser, tree list)
6728 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
6731 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6733 if (c_parser_next_token_is (parser, CPP_NAME))
6735 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
6740 if (strcmp ("none", p) != 0)
6742 kind = OMP_CLAUSE_DEFAULT_NONE;
6746 if (strcmp ("shared", p) != 0)
6748 kind = OMP_CLAUSE_DEFAULT_SHARED;
6755 c_parser_consume_token (parser);
6760 c_parser_error (parser, "expected %<none%> or %<shared%>");
6762 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
6764 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
6767 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default");
6768 c = build_omp_clause (OMP_CLAUSE_DEFAULT);
6769 OMP_CLAUSE_CHAIN (c) = list;
6770 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
6776 firstprivate ( variable-list ) */
6779 c_parser_omp_clause_firstprivate (c_parser *parser, tree list)
6781 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_FIRSTPRIVATE, list);
6785 if ( expression ) */
6788 c_parser_omp_clause_if (c_parser *parser, tree list)
6790 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
6792 tree t = c_parser_paren_condition (parser);
6795 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if");
6797 c = build_omp_clause (OMP_CLAUSE_IF);
6798 OMP_CLAUSE_IF_EXPR (c) = t;
6799 OMP_CLAUSE_CHAIN (c) = list;
6803 c_parser_error (parser, "expected %<(%>");
6809 lastprivate ( variable-list ) */
6812 c_parser_omp_clause_lastprivate (c_parser *parser, tree list)
6814 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_LASTPRIVATE, list);
6821 c_parser_omp_clause_nowait (c_parser *parser ATTRIBUTE_UNUSED, tree list)
6825 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait");
6827 c = build_omp_clause (OMP_CLAUSE_NOWAIT);
6828 OMP_CLAUSE_CHAIN (c) = list;
6833 num_threads ( expression ) */
6836 c_parser_omp_clause_num_threads (c_parser *parser, tree list)
6838 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6840 tree c, t = c_parser_expression (parser).value;
6842 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
6844 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
6846 c_parser_error (parser, "expected integer expression");
6850 /* Attempt to statically determine when the number isn't positive. */
6851 c = fold_build2 (LE_EXPR, boolean_type_node, t,
6852 build_int_cst (TREE_TYPE (t), 0));
6853 if (c == boolean_true_node)
6855 warning (0, "%<num_threads%> value must be positive");
6856 t = integer_one_node;
6859 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS, "num_threads");
6861 c = build_omp_clause (OMP_CLAUSE_NUM_THREADS);
6862 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
6863 OMP_CLAUSE_CHAIN (c) = list;
6874 c_parser_omp_clause_ordered (c_parser *parser ATTRIBUTE_UNUSED, tree list)
6878 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED, "ordered");
6880 c = build_omp_clause (OMP_CLAUSE_ORDERED);
6881 OMP_CLAUSE_CHAIN (c) = list;
6886 private ( variable-list ) */
6889 c_parser_omp_clause_private (c_parser *parser, tree list)
6891 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_PRIVATE, list);
6895 reduction ( reduction-operator : variable-list )
6898 One of: + * - & ^ | && || */
6901 c_parser_omp_clause_reduction (c_parser *parser, tree list)
6903 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6905 enum tree_code code;
6907 switch (c_parser_peek_token (parser)->type)
6919 code = BIT_AND_EXPR;
6922 code = BIT_XOR_EXPR;
6925 code = BIT_IOR_EXPR;
6928 code = TRUTH_ANDIF_EXPR;
6931 code = TRUTH_ORIF_EXPR;
6934 c_parser_error (parser,
6935 "expected %<+%>, %<*%>, %<-%>, %<&%>, "
6936 "%<^%>, %<|%>, %<&&%>, or %<||%>");
6937 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 0);
6940 c_parser_consume_token (parser);
6941 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
6945 nl = c_parser_omp_variable_list (parser, OMP_CLAUSE_REDUCTION, list);
6946 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
6947 OMP_CLAUSE_REDUCTION_CODE (c) = code;
6951 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
6957 schedule ( schedule-kind )
6958 schedule ( schedule-kind , expression )
6961 static | dynamic | guided | runtime
6965 c_parser_omp_clause_schedule (c_parser *parser, tree list)
6969 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6972 c = build_omp_clause (OMP_CLAUSE_SCHEDULE);
6974 if (c_parser_next_token_is (parser, CPP_NAME))
6976 tree kind = c_parser_peek_token (parser)->value;
6977 const char *p = IDENTIFIER_POINTER (kind);
6982 if (strcmp ("dynamic", p) != 0)
6984 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
6988 if (strcmp ("guided", p) != 0)
6990 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
6994 if (strcmp ("runtime", p) != 0)
6996 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
7003 else if (c_parser_next_token_is_keyword (parser, RID_STATIC))
7004 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
7008 c_parser_consume_token (parser);
7009 if (c_parser_next_token_is (parser, CPP_COMMA))
7011 c_parser_consume_token (parser);
7013 t = c_parser_expr_no_commas (parser, NULL).value;
7015 if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
7016 error ("schedule %<runtime%> does not take "
7017 "a %<chunk_size%> parameter");
7018 else if (TREE_CODE (TREE_TYPE (t)) == INTEGER_TYPE)
7019 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
7021 c_parser_error (parser, "expected integer expression");
7023 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
7026 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7027 "expected %<,%> or %<)%>");
7029 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule");
7030 OMP_CLAUSE_CHAIN (c) = list;
7034 c_parser_error (parser, "invalid schedule kind");
7035 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 0);
7040 shared ( variable-list ) */
7043 c_parser_omp_clause_shared (c_parser *parser, tree list)
7045 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_SHARED, list);
7048 /* Parse all OpenMP clauses. The set clauses allowed by the directive
7049 is a bitmask in MASK. Return the list of clauses found; the result
7050 of clause default goes in *pdefault. */
7053 c_parser_omp_all_clauses (c_parser *parser, unsigned int mask,
7056 tree clauses = NULL;
7058 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
7060 const pragma_omp_clause c_kind = c_parser_omp_clause_name (parser);
7062 tree prev = clauses;
7066 case PRAGMA_OMP_CLAUSE_COPYIN:
7067 clauses = c_parser_omp_clause_copyin (parser, clauses);
7070 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
7071 clauses = c_parser_omp_clause_copyprivate (parser, clauses);
7072 c_name = "copyprivate";
7074 case PRAGMA_OMP_CLAUSE_DEFAULT:
7075 clauses = c_parser_omp_clause_default (parser, clauses);
7078 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
7079 clauses = c_parser_omp_clause_firstprivate (parser, clauses);
7080 c_name = "firstprivate";
7082 case PRAGMA_OMP_CLAUSE_IF:
7083 clauses = c_parser_omp_clause_if (parser, clauses);
7086 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
7087 clauses = c_parser_omp_clause_lastprivate (parser, clauses);
7088 c_name = "lastprivate";
7090 case PRAGMA_OMP_CLAUSE_NOWAIT:
7091 clauses = c_parser_omp_clause_nowait (parser, clauses);
7094 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
7095 clauses = c_parser_omp_clause_num_threads (parser, clauses);
7096 c_name = "num_threads";
7098 case PRAGMA_OMP_CLAUSE_ORDERED:
7099 clauses = c_parser_omp_clause_ordered (parser, clauses);
7102 case PRAGMA_OMP_CLAUSE_PRIVATE:
7103 clauses = c_parser_omp_clause_private (parser, clauses);
7106 case PRAGMA_OMP_CLAUSE_REDUCTION:
7107 clauses = c_parser_omp_clause_reduction (parser, clauses);
7108 c_name = "reduction";
7110 case PRAGMA_OMP_CLAUSE_SCHEDULE:
7111 clauses = c_parser_omp_clause_schedule (parser, clauses);
7112 c_name = "schedule";
7114 case PRAGMA_OMP_CLAUSE_SHARED:
7115 clauses = c_parser_omp_clause_shared (parser, clauses);
7119 c_parser_error (parser, "expected %<#pragma omp%> clause");
7123 if (((mask >> c_kind) & 1) == 0 && !parser->error)
7125 /* Remove the invalid clause(s) from the list to avoid
7126 confusing the rest of the compiler. */
7128 error ("%qs is not valid for %qs", c_name, where);
7133 c_parser_skip_to_pragma_eol (parser);
7135 return c_finish_omp_clauses (clauses);
7142 In practice, we're also interested in adding the statement to an
7143 outer node. So it is convenient if we work around the fact that
7144 c_parser_statement calls add_stmt. */
7147 c_parser_omp_structured_block (c_parser *parser)
7149 tree stmt = push_stmt_list ();
7150 c_parser_statement (parser);
7151 return pop_stmt_list (stmt);
7155 # pragma omp atomic new-line
7159 x binop= expr | x++ | ++x | x-- | --x
7161 +, *, -, /, &, ^, |, <<, >>
7163 where x is an lvalue expression with scalar type. */
7166 c_parser_omp_atomic (c_parser *parser)
7169 enum tree_code code;
7171 c_parser_skip_to_pragma_eol (parser);
7173 lhs = c_parser_unary_expression (parser).value;
7174 switch (TREE_CODE (lhs))
7178 c_parser_skip_to_end_of_block_or_statement (parser);
7181 case PREINCREMENT_EXPR:
7182 case POSTINCREMENT_EXPR:
7183 lhs = TREE_OPERAND (lhs, 0);
7185 rhs = integer_one_node;
7188 case PREDECREMENT_EXPR:
7189 case POSTDECREMENT_EXPR:
7190 lhs = TREE_OPERAND (lhs, 0);
7192 rhs = integer_one_node;
7196 switch (c_parser_peek_token (parser)->type)
7202 code = TRUNC_DIV_EXPR;
7217 code = BIT_AND_EXPR;
7220 code = BIT_IOR_EXPR;
7223 code = BIT_XOR_EXPR;
7226 c_parser_error (parser,
7227 "invalid operator for %<#pragma omp atomic%>");
7231 c_parser_consume_token (parser);
7232 rhs = c_parser_expression (parser).value;
7235 c_finish_omp_atomic (code, lhs, rhs);
7236 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
7241 # pragma omp barrier new-line
7245 c_parser_omp_barrier (c_parser *parser)
7247 c_parser_consume_pragma (parser);
7248 c_parser_skip_to_pragma_eol (parser);
7250 c_finish_omp_barrier ();
7254 # pragma omp critical [(name)] new-line
7259 c_parser_omp_critical (c_parser *parser)
7261 tree stmt, name = NULL;
7263 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
7265 c_parser_consume_token (parser);
7266 if (c_parser_next_token_is (parser, CPP_NAME))
7268 name = c_parser_peek_token (parser)->value;
7269 c_parser_consume_token (parser);
7270 c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
7273 c_parser_error (parser, "expected identifier");
7275 else if (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
7276 c_parser_error (parser, "expected %<(%> or end of line");
7277 c_parser_skip_to_pragma_eol (parser);
7279 stmt = c_parser_omp_structured_block (parser);
7280 return c_finish_omp_critical (stmt, name);
7284 # pragma omp flush flush-vars[opt] new-line
7287 ( variable-list ) */
7290 c_parser_omp_flush (c_parser *parser)
7292 c_parser_consume_pragma (parser);
7293 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
7294 c_parser_omp_var_list_parens (parser, 0, NULL);
7295 else if (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
7296 c_parser_error (parser, "expected %<(%> or end of line");
7297 c_parser_skip_to_pragma_eol (parser);
7299 c_finish_omp_flush ();
7302 /* Parse the restricted form of the for statment allowed by OpenMP.
7303 The real trick here is to determine the loop control variable early
7304 so that we can push a new decl if necessary to make it private. */
7307 c_parser_omp_for_loop (c_parser *parser)
7309 tree decl, cond, incr, save_break, save_cont, body, init;
7312 if (!c_parser_next_token_is_keyword (parser, RID_FOR))
7314 c_parser_error (parser, "for statement expected");
7317 loc = c_parser_peek_token (parser)->location;
7318 c_parser_consume_token (parser);
7320 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7323 /* Parse the initialization declaration or expression. */
7324 if (c_parser_next_token_starts_declspecs (parser))
7326 c_parser_declaration_or_fndef (parser, true, true, true, true);
7327 decl = check_for_loop_decls ();
7332 else if (c_parser_next_token_is (parser, CPP_NAME)
7333 && c_parser_peek_2nd_token (parser)->type == CPP_EQ)
7335 decl = c_parser_postfix_expression (parser).value;
7337 c_parser_require (parser, CPP_EQ, "expected %<=%>");
7339 init = c_parser_expr_no_commas (parser, NULL).value;
7340 init = build_modify_expr (decl, NOP_EXPR, init);
7341 init = c_process_expr_stmt (init);
7343 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
7348 /* Parse the loop condition. */
7350 if (c_parser_next_token_is_not (parser, CPP_SEMICOLON))
7352 cond = c_parser_expression_conv (parser).value;
7353 cond = c_objc_common_truthvalue_conversion (cond);
7355 SET_EXPR_LOCATION (cond, input_location);
7357 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
7359 /* Parse the increment expression. */
7361 if (c_parser_next_token_is_not (parser, CPP_CLOSE_PAREN))
7362 incr = c_process_expr_stmt (c_parser_expression (parser).value);
7363 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
7366 save_break = c_break_label;
7367 c_break_label = size_one_node;
7368 save_cont = c_cont_label;
7369 c_cont_label = NULL_TREE;
7370 body = push_stmt_list ();
7372 add_stmt (c_parser_c99_block_statement (parser));
7374 add_stmt (build1 (LABEL_EXPR, void_type_node, c_cont_label));
7376 body = pop_stmt_list (body);
7377 c_break_label = save_break;
7378 c_cont_label = save_cont;
7380 /* Only bother calling c_finish_omp_for if we havn't already generated
7381 an error from the initialization parsing. */
7383 return c_finish_omp_for (loc, decl, init, cond, incr, body, NULL);
7387 c_parser_error (parser, "expected iteration declaration or initialization");
7388 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
7389 decl = init = cond = incr = NULL_TREE;
7394 #pragma omp for for-clause[optseq] new-line
7398 #define OMP_FOR_CLAUSE_MASK \
7399 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
7400 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
7401 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
7402 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
7403 | (1u << PRAGMA_OMP_CLAUSE_ORDERED) \
7404 | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE) \
7405 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
7408 c_parser_omp_for (c_parser *parser)
7410 tree block, clauses, ret;
7412 clauses = c_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
7415 block = c_begin_compound_stmt (true);
7416 ret = c_parser_omp_for_loop (parser);
7418 OMP_FOR_CLAUSES (ret) = clauses;
7419 block = c_end_compound_stmt (block, true);
7426 # pragma omp master new-line
7431 c_parser_omp_master (c_parser *parser)
7433 c_parser_skip_to_pragma_eol (parser);
7434 return c_finish_omp_master (c_parser_omp_structured_block (parser));
7438 # pragma omp ordered new-line
7443 c_parser_omp_ordered (c_parser *parser)
7445 c_parser_skip_to_pragma_eol (parser);
7446 return c_finish_omp_ordered (c_parser_omp_structured_block (parser));
7452 { section-sequence }
7455 section-directive[opt] structured-block
7456 section-sequence section-directive structured-block */
7459 c_parser_omp_sections_scope (c_parser *parser)
7462 bool error_suppress = false;
7465 if (!c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
7467 /* Avoid skipping until the end of the block. */
7468 parser->error = false;
7472 stmt = push_stmt_list ();
7474 loc = c_parser_peek_token (parser)->location;
7475 if (c_parser_peek_token (parser)->pragma_kind != PRAGMA_OMP_SECTION)
7477 substmt = push_stmt_list ();
7481 c_parser_statement (parser);
7483 if (c_parser_peek_token (parser)->pragma_kind == PRAGMA_OMP_SECTION)
7485 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
7487 if (c_parser_next_token_is (parser, CPP_EOF))
7491 substmt = pop_stmt_list (substmt);
7492 substmt = build1 (OMP_SECTION, void_type_node, substmt);
7493 SET_EXPR_LOCATION (substmt, loc);
7499 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
7501 if (c_parser_next_token_is (parser, CPP_EOF))
7504 loc = c_parser_peek_token (parser)->location;
7505 if (c_parser_peek_token (parser)->pragma_kind == PRAGMA_OMP_SECTION)
7507 c_parser_consume_pragma (parser);
7508 c_parser_skip_to_pragma_eol (parser);
7509 error_suppress = false;
7511 else if (!error_suppress)
7513 error ("expected %<#pragma omp section%> or %<}%>");
7514 error_suppress = true;
7517 substmt = c_parser_omp_structured_block (parser);
7518 substmt = build1 (OMP_SECTION, void_type_node, substmt);
7519 SET_EXPR_LOCATION (substmt, loc);
7522 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE,
7523 "expected %<#pragma omp section%> or %<}%>");
7525 substmt = pop_stmt_list (stmt);
7527 stmt = make_node (OMP_SECTIONS);
7528 TREE_TYPE (stmt) = void_type_node;
7529 OMP_SECTIONS_BODY (stmt) = substmt;
7531 return add_stmt (stmt);
7535 # pragma omp sections sections-clause[optseq] newline
7539 #define OMP_SECTIONS_CLAUSE_MASK \
7540 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
7541 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
7542 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
7543 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
7544 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
7547 c_parser_omp_sections (c_parser *parser)
7549 tree block, clauses, ret;
7551 clauses = c_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
7552 "#pragma omp sections");
7554 block = c_begin_compound_stmt (true);
7555 ret = c_parser_omp_sections_scope (parser);
7557 OMP_SECTIONS_CLAUSES (ret) = clauses;
7558 block = c_end_compound_stmt (block, true);
7565 # pragma parallel parallel-clause new-line
7566 # pragma parallel for parallel-for-clause new-line
7567 # pragma parallel sections parallel-sections-clause new-line
7570 #define OMP_PARALLEL_CLAUSE_MASK \
7571 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
7572 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
7573 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
7574 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
7575 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
7576 | (1u << PRAGMA_OMP_CLAUSE_COPYIN) \
7577 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
7578 | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
7581 c_parser_omp_parallel (c_parser *parser)
7583 enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
7584 const char *p_name = "#pragma omp parallel";
7585 tree stmt, clauses, par_clause, ws_clause, block;
7586 unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
7588 if (c_parser_next_token_is_keyword (parser, RID_FOR))
7590 c_parser_consume_token (parser);
7591 p_kind = PRAGMA_OMP_PARALLEL_FOR;
7592 p_name = "#pragma omp parallel for";
7593 mask |= OMP_FOR_CLAUSE_MASK;
7594 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
7596 else if (c_parser_next_token_is (parser, CPP_NAME))
7598 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
7599 if (strcmp (p, "sections") == 0)
7601 c_parser_consume_token (parser);
7602 p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
7603 p_name = "#pragma omp parallel sections";
7604 mask |= OMP_SECTIONS_CLAUSE_MASK;
7605 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
7609 clauses = c_parser_omp_all_clauses (parser, mask, p_name);
7613 case PRAGMA_OMP_PARALLEL:
7614 block = c_begin_omp_parallel ();
7615 c_parser_statement (parser);
7616 stmt = c_finish_omp_parallel (clauses, block);
7619 case PRAGMA_OMP_PARALLEL_FOR:
7620 block = c_begin_omp_parallel ();
7621 c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
7622 stmt = c_parser_omp_for_loop (parser);
7624 OMP_FOR_CLAUSES (stmt) = ws_clause;
7625 stmt = c_finish_omp_parallel (par_clause, block);
7628 case PRAGMA_OMP_PARALLEL_SECTIONS:
7629 block = c_begin_omp_parallel ();
7630 c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
7631 stmt = c_parser_omp_sections_scope (parser);
7633 OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
7634 stmt = c_finish_omp_parallel (par_clause, block);
7645 # pragma omp single single-clause[optseq] new-line
7649 #define OMP_SINGLE_CLAUSE_MASK \
7650 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
7651 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
7652 | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
7653 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
7656 c_parser_omp_single (c_parser *parser)
7658 tree stmt = make_node (OMP_SINGLE);
7659 TREE_TYPE (stmt) = void_type_node;
7661 OMP_SINGLE_CLAUSES (stmt)
7662 = c_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
7663 "#pragma omp single");
7664 OMP_SINGLE_BODY (stmt) = c_parser_omp_structured_block (parser);
7666 return add_stmt (stmt);
7670 /* Main entry point to parsing most OpenMP pragmas. */
7673 c_parser_omp_construct (c_parser *parser)
7675 enum pragma_kind p_kind;
7679 loc = c_parser_peek_token (parser)->location;
7680 p_kind = c_parser_peek_token (parser)->pragma_kind;
7681 c_parser_consume_pragma (parser);
7685 case PRAGMA_OMP_ATOMIC:
7686 c_parser_omp_atomic (parser);
7688 case PRAGMA_OMP_CRITICAL:
7689 stmt = c_parser_omp_critical (parser);
7691 case PRAGMA_OMP_FOR:
7692 stmt = c_parser_omp_for (parser);
7694 case PRAGMA_OMP_MASTER:
7695 stmt = c_parser_omp_master (parser);
7697 case PRAGMA_OMP_ORDERED:
7698 stmt = c_parser_omp_ordered (parser);
7700 case PRAGMA_OMP_PARALLEL:
7701 stmt = c_parser_omp_parallel (parser);
7703 case PRAGMA_OMP_SECTIONS:
7704 stmt = c_parser_omp_sections (parser);
7706 case PRAGMA_OMP_SINGLE:
7707 stmt = c_parser_omp_single (parser);
7714 SET_EXPR_LOCATION (stmt, loc);
7719 # pragma omp threadprivate (variable-list) */
7722 c_parser_omp_threadprivate (c_parser *parser)
7726 c_parser_consume_pragma (parser);
7727 vars = c_parser_omp_var_list_parens (parser, 0, NULL);
7729 if (!targetm.have_tls)
7730 sorry ("threadprivate variables not supported in this target");
7732 /* Mark every variable in VARS to be assigned thread local storage. */
7733 for (t = vars; t; t = TREE_CHAIN (t))
7735 tree v = TREE_PURPOSE (t);
7737 /* If V had already been marked threadprivate, it doesn't matter
7738 whether it had been used prior to this point. */
7739 if (TREE_USED (v) && !C_DECL_THREADPRIVATE_P (v))
7740 error ("%qE declared %<threadprivate%> after first use", v);
7741 else if (! TREE_STATIC (v) && ! DECL_EXTERNAL (v))
7742 error ("automatic variable %qE cannot be %<threadprivate%>", v);
7743 else if (! COMPLETE_TYPE_P (TREE_TYPE (v)))
7744 error ("%<threadprivate%> %qE has incomplete type", v);
7747 if (! DECL_THREAD_LOCAL_P (v))
7749 DECL_TLS_MODEL (v) = decl_default_tls_model (v);
7750 /* If rtl has been already set for this var, call
7751 make_decl_rtl once again, so that encode_section_info
7752 has a chance to look at the new decl flags. */
7753 if (DECL_RTL_SET_P (v))
7756 C_DECL_THREADPRIVATE_P (v) = 1;
7760 c_parser_skip_to_pragma_eol (parser);
7764 /* Parse a single source file. */
7769 /* Use local storage to begin. If the first token is a pragma, parse it.
7770 If it is #pragma GCC pch_preprocess, then this will load a PCH file
7771 which will cause garbage collection. */
7774 memset (&tparser, 0, sizeof tparser);
7775 the_parser = &tparser;
7777 if (c_parser_peek_token (&tparser)->pragma_kind == PRAGMA_GCC_PCH_PREPROCESS)
7778 c_parser_pragma_pch_preprocess (&tparser);
7780 the_parser = GGC_NEW (c_parser);
7781 *the_parser = tparser;
7783 c_parser_translation_unit (the_parser);
7787 #include "gt-c-parser.h"