1 /* Parser for C and Objective-C.
2 Copyright (C) 1987, 1988, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2007 Free Software Foundation, Inc.
5 Parser actions based on the old Bison parser; structure somewhat
6 influenced by and fragments based on the C++ parser.
8 This file is part of GCC.
10 GCC is free software; you can redistribute it and/or modify it under
11 the terms of the GNU General Public License as published by the Free
12 Software Foundation; either version 3, or (at your option) any later
15 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
16 WARRANTY; without even the implied warranty of MERCHANTABILITY or
17 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
20 You should have received a copy of the GNU General Public License
21 along with GCC; see the file COPYING3. If not see
22 <http://www.gnu.org/licenses/>. */
26 Make sure all relevant comments, and all relevant code from all
27 actions, brought over from old parser. Verify exact correspondence
30 Add testcases covering every input symbol in every state in old and
33 Include full syntax for GNU C, including erroneous cases accepted
34 with error messages, in syntax productions in comments.
36 Make more diagnostics in the front end generally take an explicit
37 location rather than implicitly using input_location. */
41 #include "coretypes.h"
45 #include "langhooks.h"
61 /* The reserved keyword table. */
65 ENUM_BITFIELD(rid
) rid
: 16;
66 unsigned int disable
: 16;
69 /* Disable mask. Keywords are disabled if (reswords[i].disable &
71 #define D_C89 0x01 /* not in C89 */
72 #define D_EXT 0x02 /* GCC extension */
73 #define D_EXT89 0x04 /* GCC extension incorporated in C99 */
74 #define D_OBJC 0x08 /* Objective C only */
76 static const struct resword reswords
[] =
78 { "_Bool", RID_BOOL
, 0 },
79 { "_Complex", RID_COMPLEX
, 0 },
80 { "_Decimal32", RID_DFLOAT32
, D_EXT
},
81 { "_Decimal64", RID_DFLOAT64
, D_EXT
},
82 { "_Decimal128", RID_DFLOAT128
, D_EXT
},
83 { "_Fract", RID_FRACT
, D_EXT
},
84 { "_Accum", RID_ACCUM
, D_EXT
},
85 { "_Sat", RID_SAT
, D_EXT
},
86 { "__FUNCTION__", RID_FUNCTION_NAME
, 0 },
87 { "__PRETTY_FUNCTION__", RID_PRETTY_FUNCTION_NAME
, 0 },
88 { "__alignof", RID_ALIGNOF
, 0 },
89 { "__alignof__", RID_ALIGNOF
, 0 },
90 { "__asm", RID_ASM
, 0 },
91 { "__asm__", RID_ASM
, 0 },
92 { "__attribute", RID_ATTRIBUTE
, 0 },
93 { "__attribute__", RID_ATTRIBUTE
, 0 },
94 { "__builtin_choose_expr", RID_CHOOSE_EXPR
, 0 },
95 { "__builtin_offsetof", RID_OFFSETOF
, 0 },
96 { "__builtin_types_compatible_p", RID_TYPES_COMPATIBLE_P
, 0 },
97 { "__builtin_va_arg", RID_VA_ARG
, 0 },
98 { "__complex", RID_COMPLEX
, 0 },
99 { "__complex__", RID_COMPLEX
, 0 },
100 { "__const", RID_CONST
, 0 },
101 { "__const__", RID_CONST
, 0 },
102 { "__extension__", RID_EXTENSION
, 0 },
103 { "__func__", RID_C99_FUNCTION_NAME
, 0 },
104 { "__imag", RID_IMAGPART
, 0 },
105 { "__imag__", RID_IMAGPART
, 0 },
106 { "__inline", RID_INLINE
, 0 },
107 { "__inline__", RID_INLINE
, 0 },
108 { "__label__", RID_LABEL
, 0 },
109 { "__real", RID_REALPART
, 0 },
110 { "__real__", RID_REALPART
, 0 },
111 { "__restrict", RID_RESTRICT
, 0 },
112 { "__restrict__", RID_RESTRICT
, 0 },
113 { "__signed", RID_SIGNED
, 0 },
114 { "__signed__", RID_SIGNED
, 0 },
115 { "__thread", RID_THREAD
, 0 },
116 { "__typeof", RID_TYPEOF
, 0 },
117 { "__typeof__", RID_TYPEOF
, 0 },
118 { "__volatile", RID_VOLATILE
, 0 },
119 { "__volatile__", RID_VOLATILE
, 0 },
120 { "asm", RID_ASM
, D_EXT
},
121 { "auto", RID_AUTO
, 0 },
122 { "break", RID_BREAK
, 0 },
123 { "case", RID_CASE
, 0 },
124 { "char", RID_CHAR
, 0 },
125 { "const", RID_CONST
, 0 },
126 { "continue", RID_CONTINUE
, 0 },
127 { "default", RID_DEFAULT
, 0 },
129 { "double", RID_DOUBLE
, 0 },
130 { "else", RID_ELSE
, 0 },
131 { "enum", RID_ENUM
, 0 },
132 { "extern", RID_EXTERN
, 0 },
133 { "float", RID_FLOAT
, 0 },
134 { "for", RID_FOR
, 0 },
135 { "goto", RID_GOTO
, 0 },
137 { "inline", RID_INLINE
, D_EXT89
},
138 { "int", RID_INT
, 0 },
139 { "long", RID_LONG
, 0 },
140 { "register", RID_REGISTER
, 0 },
141 { "restrict", RID_RESTRICT
, D_C89
},
142 { "return", RID_RETURN
, 0 },
143 { "short", RID_SHORT
, 0 },
144 { "signed", RID_SIGNED
, 0 },
145 { "sizeof", RID_SIZEOF
, 0 },
146 { "static", RID_STATIC
, 0 },
147 { "struct", RID_STRUCT
, 0 },
148 { "switch", RID_SWITCH
, 0 },
149 { "typedef", RID_TYPEDEF
, 0 },
150 { "typeof", RID_TYPEOF
, D_EXT
},
151 { "union", RID_UNION
, 0 },
152 { "unsigned", RID_UNSIGNED
, 0 },
153 { "void", RID_VOID
, 0 },
154 { "volatile", RID_VOLATILE
, 0 },
155 { "while", RID_WHILE
, 0 },
156 /* These Objective-C keywords are recognized only immediately after
158 { "class", RID_AT_CLASS
, D_OBJC
},
159 { "compatibility_alias", RID_AT_ALIAS
, D_OBJC
},
160 { "defs", RID_AT_DEFS
, D_OBJC
},
161 { "encode", RID_AT_ENCODE
, D_OBJC
},
162 { "end", RID_AT_END
, D_OBJC
},
163 { "implementation", RID_AT_IMPLEMENTATION
, D_OBJC
},
164 { "interface", RID_AT_INTERFACE
, D_OBJC
},
165 { "private", RID_AT_PRIVATE
, D_OBJC
},
166 { "protected", RID_AT_PROTECTED
, D_OBJC
},
167 { "protocol", RID_AT_PROTOCOL
, D_OBJC
},
168 { "public", RID_AT_PUBLIC
, D_OBJC
},
169 { "selector", RID_AT_SELECTOR
, D_OBJC
},
170 { "throw", RID_AT_THROW
, D_OBJC
},
171 { "try", RID_AT_TRY
, D_OBJC
},
172 { "catch", RID_AT_CATCH
, D_OBJC
},
173 { "finally", RID_AT_FINALLY
, D_OBJC
},
174 { "synchronized", RID_AT_SYNCHRONIZED
, D_OBJC
},
175 /* These are recognized only in protocol-qualifier context
177 { "bycopy", RID_BYCOPY
, D_OBJC
},
178 { "byref", RID_BYREF
, D_OBJC
},
179 { "in", RID_IN
, D_OBJC
},
180 { "inout", RID_INOUT
, D_OBJC
},
181 { "oneway", RID_ONEWAY
, D_OBJC
},
182 { "out", RID_OUT
, D_OBJC
},
184 #define N_reswords (sizeof reswords / sizeof (struct resword))
186 /* Initialization routine for this file. */
191 /* The only initialization required is of the reserved word
195 int mask
= (flag_isoc99
? 0 : D_C89
)
196 | (flag_no_asm
? (flag_isoc99
? D_EXT
: D_EXT
|D_EXT89
) : 0);
198 if (!c_dialect_objc ())
201 ridpointers
= GGC_CNEWVEC (tree
, (int) RID_MAX
);
202 for (i
= 0; i
< N_reswords
; i
++)
204 /* If a keyword is disabled, do not enter it into the table
205 and so create a canonical spelling that isn't a keyword. */
206 if (reswords
[i
].disable
& mask
)
209 id
= get_identifier (reswords
[i
].word
);
210 C_RID_CODE (id
) = reswords
[i
].rid
;
211 C_IS_RESERVED_WORD (id
) = 1;
212 ridpointers
[(int) reswords
[i
].rid
] = id
;
216 /* The C lexer intermediates between the lexer in cpplib and c-lex.c
217 and the C parser. Unlike the C++ lexer, the parser structure
218 stores the lexer information instead of using a separate structure.
219 Identifiers are separated into ordinary identifiers, type names,
220 keywords and some other Objective-C types of identifiers, and some
221 look-ahead is maintained.
223 ??? It might be a good idea to lex the whole file up front (as for
224 C++). It would then be possible to share more of the C and C++
225 lexer code, if desired. */
227 /* The following local token type is used. */
230 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
232 /* More information about the type of a CPP_NAME token. */
233 typedef enum c_id_kind
{
234 /* An ordinary identifier. */
236 /* An identifier declared as a typedef name. */
238 /* An identifier declared as an Objective-C class name. */
240 /* Not an identifier. */
244 /* A single C token after string literal concatenation and conversion
245 of preprocessing tokens to tokens. */
246 typedef struct c_token
GTY (())
248 /* The kind of token. */
249 ENUM_BITFIELD (cpp_ttype
) type
: 8;
250 /* If this token is a CPP_NAME, this value indicates whether also
251 declared as some kind of type. Otherwise, it is C_ID_NONE. */
252 ENUM_BITFIELD (c_id_kind
) id_kind
: 8;
253 /* If this token is a keyword, this value indicates which keyword.
254 Otherwise, this value is RID_MAX. */
255 ENUM_BITFIELD (rid
) keyword
: 8;
256 /* If this token is a CPP_PRAGMA, this indicates the pragma that
257 was seen. Otherwise it is PRAGMA_NONE. */
258 ENUM_BITFIELD (pragma_kind
) pragma_kind
: 7;
259 /* True if this token is from a system header. */
260 BOOL_BITFIELD in_system_header
: 1;
261 /* The value associated with this token, if any. */
263 /* The location at which this token was found. */
267 /* A parser structure recording information about the state and
268 context of parsing. Includes lexer information with up to two
269 tokens of look-ahead; more are not needed for C. */
270 typedef struct c_parser
GTY(())
272 /* The look-ahead tokens. */
274 /* How many look-ahead tokens are available (0, 1 or 2). */
276 /* True if a syntax error is being recovered from; false otherwise.
277 c_parser_error sets this flag. It should clear this flag when
278 enough tokens have been consumed to recover from the error. */
279 BOOL_BITFIELD error
: 1;
280 /* True if we're processing a pragma, and shouldn't automatically
281 consume CPP_PRAGMA_EOL. */
282 BOOL_BITFIELD in_pragma
: 1;
283 /* True if we're parsing the outermost block of an if statement. */
284 BOOL_BITFIELD in_if_block
: 1;
285 /* True if we want to lex an untranslated string. */
286 BOOL_BITFIELD lex_untranslated_string
: 1;
287 /* Objective-C specific parser/lexer information. */
288 BOOL_BITFIELD objc_pq_context
: 1;
289 /* The following flag is needed to contextualize Objective-C lexical
290 analysis. In some cases (e.g., 'int NSObject;'), it is
291 undesirable to bind an identifier to an Objective-C class, even
292 if a class with that name exists. */
293 BOOL_BITFIELD objc_need_raw_identifier
: 1;
297 /* The actual parser and external interface. ??? Does this need to be
298 garbage-collected? */
300 static GTY (()) c_parser
*the_parser
;
303 /* Read in and lex a single token, storing it in *TOKEN. */
306 c_lex_one_token (c_parser
*parser
, c_token
*token
)
308 timevar_push (TV_LEX
);
310 token
->type
= c_lex_with_flags (&token
->value
, &token
->location
, NULL
,
311 (parser
->lex_untranslated_string
312 ? C_LEX_STRING_NO_TRANSLATE
: 0));
313 token
->id_kind
= C_ID_NONE
;
314 token
->keyword
= RID_MAX
;
315 token
->pragma_kind
= PRAGMA_NONE
;
316 token
->in_system_header
= in_system_header
;
324 bool objc_force_identifier
= parser
->objc_need_raw_identifier
;
325 if (c_dialect_objc ())
326 parser
->objc_need_raw_identifier
= false;
328 if (C_IS_RESERVED_WORD (token
->value
))
330 enum rid rid_code
= C_RID_CODE (token
->value
);
332 if (c_dialect_objc ())
334 if (!OBJC_IS_AT_KEYWORD (rid_code
)
335 && (!OBJC_IS_PQ_KEYWORD (rid_code
)
336 || parser
->objc_pq_context
))
338 /* Return the canonical spelling for this keyword. */
339 token
->value
= ridpointers
[(int) rid_code
];
340 token
->type
= CPP_KEYWORD
;
341 token
->keyword
= rid_code
;
347 /* Return the canonical spelling for this keyword. */
348 token
->value
= ridpointers
[(int) rid_code
];
349 token
->type
= CPP_KEYWORD
;
350 token
->keyword
= rid_code
;
355 decl
= lookup_name (token
->value
);
358 if (TREE_CODE (decl
) == TYPE_DECL
)
360 token
->id_kind
= C_ID_TYPENAME
;
364 else if (c_dialect_objc ())
366 tree objc_interface_decl
= objc_is_class_name (token
->value
);
367 /* Objective-C class names are in the same namespace as
368 variables and typedefs, and hence are shadowed by local
370 if (objc_interface_decl
371 && (global_bindings_p ()
372 || (!objc_force_identifier
&& !decl
)))
374 token
->value
= objc_interface_decl
;
375 token
->id_kind
= C_ID_CLASSNAME
;
379 token
->id_kind
= C_ID_ID
;
383 /* This only happens in Objective-C; it must be a keyword. */
384 token
->type
= CPP_KEYWORD
;
385 token
->keyword
= C_RID_CODE (token
->value
);
389 case CPP_CLOSE_PAREN
:
391 /* These tokens may affect the interpretation of any identifiers
392 following, if doing Objective-C. */
393 if (c_dialect_objc ())
394 parser
->objc_need_raw_identifier
= false;
397 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
398 token
->pragma_kind
= TREE_INT_CST_LOW (token
->value
);
404 timevar_pop (TV_LEX
);
407 /* Return a pointer to the next token from PARSER, reading it in if
410 static inline c_token
*
411 c_parser_peek_token (c_parser
*parser
)
413 if (parser
->tokens_avail
== 0)
415 c_lex_one_token (parser
, &parser
->tokens
[0]);
416 parser
->tokens_avail
= 1;
418 return &parser
->tokens
[0];
421 /* Return true if the next token from PARSER has the indicated
425 c_parser_next_token_is (c_parser
*parser
, enum cpp_ttype type
)
427 return c_parser_peek_token (parser
)->type
== type
;
430 /* Return true if the next token from PARSER does not have the
434 c_parser_next_token_is_not (c_parser
*parser
, enum cpp_ttype type
)
436 return !c_parser_next_token_is (parser
, type
);
439 /* Return true if the next token from PARSER is the indicated
443 c_parser_next_token_is_keyword (c_parser
*parser
, enum rid keyword
)
447 /* Peek at the next token. */
448 token
= c_parser_peek_token (parser
);
449 /* Check to see if it is the indicated keyword. */
450 return token
->keyword
== keyword
;
453 /* Return true if TOKEN can start a type name,
456 c_token_starts_typename (c_token
*token
)
461 switch (token
->id_kind
)
468 gcc_assert (c_dialect_objc ());
474 switch (token
->keyword
)
506 if (c_dialect_objc ())
514 /* Return true if the next token from PARSER can start a type name,
517 c_parser_next_token_starts_typename (c_parser
*parser
)
519 c_token
*token
= c_parser_peek_token (parser
);
520 return c_token_starts_typename (token
);
523 /* Return true if TOKEN can start declaration specifiers, false
526 c_token_starts_declspecs (c_token
*token
)
531 switch (token
->id_kind
)
538 gcc_assert (c_dialect_objc ());
544 switch (token
->keyword
)
583 if (c_dialect_objc ())
591 /* Return true if the next token from PARSER can start declaration
592 specifiers, false otherwise. */
594 c_parser_next_token_starts_declspecs (c_parser
*parser
)
596 c_token
*token
= c_parser_peek_token (parser
);
597 return c_token_starts_declspecs (token
);
600 /* Return a pointer to the next-but-one token from PARSER, reading it
601 in if necessary. The next token is already read in. */
604 c_parser_peek_2nd_token (c_parser
*parser
)
606 if (parser
->tokens_avail
>= 2)
607 return &parser
->tokens
[1];
608 gcc_assert (parser
->tokens_avail
== 1);
609 gcc_assert (parser
->tokens
[0].type
!= CPP_EOF
);
610 gcc_assert (parser
->tokens
[0].type
!= CPP_PRAGMA_EOL
);
611 c_lex_one_token (parser
, &parser
->tokens
[1]);
612 parser
->tokens_avail
= 2;
613 return &parser
->tokens
[1];
616 /* Consume the next token from PARSER. */
619 c_parser_consume_token (c_parser
*parser
)
621 gcc_assert (parser
->tokens_avail
>= 1);
622 gcc_assert (parser
->tokens
[0].type
!= CPP_EOF
);
623 gcc_assert (!parser
->in_pragma
|| parser
->tokens
[0].type
!= CPP_PRAGMA_EOL
);
624 gcc_assert (parser
->error
|| parser
->tokens
[0].type
!= CPP_PRAGMA
);
625 if (parser
->tokens_avail
== 2)
626 parser
->tokens
[0] = parser
->tokens
[1];
627 parser
->tokens_avail
--;
630 /* Expect the current token to be a #pragma. Consume it and remember
631 that we've begun parsing a pragma. */
634 c_parser_consume_pragma (c_parser
*parser
)
636 gcc_assert (!parser
->in_pragma
);
637 gcc_assert (parser
->tokens_avail
>= 1);
638 gcc_assert (parser
->tokens
[0].type
== CPP_PRAGMA
);
639 if (parser
->tokens_avail
== 2)
640 parser
->tokens
[0] = parser
->tokens
[1];
641 parser
->tokens_avail
--;
642 parser
->in_pragma
= true;
645 /* Update the globals input_location and in_system_header from
648 c_parser_set_source_position_from_token (c_token
*token
)
650 if (token
->type
!= CPP_EOF
)
652 input_location
= token
->location
;
653 in_system_header
= token
->in_system_header
;
657 /* Issue a diagnostic of the form
658 FILE:LINE: MESSAGE before TOKEN
659 where TOKEN is the next token in the input stream of PARSER.
660 MESSAGE (specified by the caller) is usually of the form "expected
663 Do not issue a diagnostic if still recovering from an error.
665 ??? This is taken from the C++ parser, but building up messages in
666 this way is not i18n-friendly and some other approach should be
670 c_parser_error (c_parser
*parser
, const char *gmsgid
)
672 c_token
*token
= c_parser_peek_token (parser
);
675 parser
->error
= true;
678 /* This diagnostic makes more sense if it is tagged to the line of
679 the token we just peeked at. */
680 c_parser_set_source_position_from_token (token
);
681 c_parse_error (gmsgid
,
682 /* Because c_parse_error does not understand
683 CPP_KEYWORD, keywords are treated like
685 (token
->type
== CPP_KEYWORD
? CPP_NAME
: token
->type
),
689 /* If the next token is of the indicated TYPE, consume it. Otherwise,
690 issue the error MSGID. If MSGID is NULL then a message has already
691 been produced and no message will be produced this time. Returns
692 true if found, false otherwise. */
695 c_parser_require (c_parser
*parser
,
699 if (c_parser_next_token_is (parser
, type
))
701 c_parser_consume_token (parser
);
706 c_parser_error (parser
, msgid
);
711 /* If the next token is the indicated keyword, consume it. Otherwise,
712 issue the error MSGID. Returns true if found, false otherwise. */
715 c_parser_require_keyword (c_parser
*parser
,
719 if (c_parser_next_token_is_keyword (parser
, keyword
))
721 c_parser_consume_token (parser
);
726 c_parser_error (parser
, msgid
);
731 /* Like c_parser_require, except that tokens will be skipped until the
732 desired token is found. An error message is still produced if the
733 next token is not as expected. If MSGID is NULL then a message has
734 already been produced and no message will be produced this
738 c_parser_skip_until_found (c_parser
*parser
,
742 unsigned nesting_depth
= 0;
744 if (c_parser_require (parser
, type
, msgid
))
747 /* Skip tokens until the desired token is found. */
750 /* Peek at the next token. */
751 c_token
*token
= c_parser_peek_token (parser
);
752 /* If we've reached the token we want, consume it and stop. */
753 if (token
->type
== type
&& !nesting_depth
)
755 c_parser_consume_token (parser
);
759 /* If we've run out of tokens, stop. */
760 if (token
->type
== CPP_EOF
)
762 if (token
->type
== CPP_PRAGMA_EOL
&& parser
->in_pragma
)
764 if (token
->type
== CPP_OPEN_BRACE
765 || token
->type
== CPP_OPEN_PAREN
766 || token
->type
== CPP_OPEN_SQUARE
)
768 else if (token
->type
== CPP_CLOSE_BRACE
769 || token
->type
== CPP_CLOSE_PAREN
770 || token
->type
== CPP_CLOSE_SQUARE
)
772 if (nesting_depth
-- == 0)
775 /* Consume this token. */
776 c_parser_consume_token (parser
);
778 parser
->error
= false;
781 /* Skip tokens until the end of a parameter is found, but do not
782 consume the comma, semicolon or closing delimiter. */
785 c_parser_skip_to_end_of_parameter (c_parser
*parser
)
787 unsigned nesting_depth
= 0;
791 c_token
*token
= c_parser_peek_token (parser
);
792 if ((token
->type
== CPP_COMMA
|| token
->type
== CPP_SEMICOLON
)
795 /* If we've run out of tokens, stop. */
796 if (token
->type
== CPP_EOF
)
798 if (token
->type
== CPP_PRAGMA_EOL
&& parser
->in_pragma
)
800 if (token
->type
== CPP_OPEN_BRACE
801 || token
->type
== CPP_OPEN_PAREN
802 || token
->type
== CPP_OPEN_SQUARE
)
804 else if (token
->type
== CPP_CLOSE_BRACE
805 || token
->type
== CPP_CLOSE_PAREN
806 || token
->type
== CPP_CLOSE_SQUARE
)
808 if (nesting_depth
-- == 0)
811 /* Consume this token. */
812 c_parser_consume_token (parser
);
814 parser
->error
= false;
817 /* Expect to be at the end of the pragma directive and consume an
818 end of line marker. */
821 c_parser_skip_to_pragma_eol (c_parser
*parser
)
823 gcc_assert (parser
->in_pragma
);
824 parser
->in_pragma
= false;
826 if (!c_parser_require (parser
, CPP_PRAGMA_EOL
, "expected end of line"))
829 c_token
*token
= c_parser_peek_token (parser
);
830 if (token
->type
== CPP_EOF
)
832 if (token
->type
== CPP_PRAGMA_EOL
)
834 c_parser_consume_token (parser
);
837 c_parser_consume_token (parser
);
840 parser
->error
= false;
843 /* Skip tokens until we have consumed an entire block, or until we
844 have consumed a non-nested ';'. */
847 c_parser_skip_to_end_of_block_or_statement (c_parser
*parser
)
849 unsigned nesting_depth
= 0;
850 bool save_error
= parser
->error
;
856 /* Peek at the next token. */
857 token
= c_parser_peek_token (parser
);
865 if (parser
->in_pragma
)
870 /* If the next token is a ';', we have reached the
871 end of the statement. */
874 /* Consume the ';'. */
875 c_parser_consume_token (parser
);
880 case CPP_CLOSE_BRACE
:
881 /* If the next token is a non-nested '}', then we have
882 reached the end of the current block. */
883 if (nesting_depth
== 0 || --nesting_depth
== 0)
885 c_parser_consume_token (parser
);
891 /* If it the next token is a '{', then we are entering a new
892 block. Consume the entire block. */
897 /* If we see a pragma, consume the whole thing at once. We
898 have some safeguards against consuming pragmas willy-nilly.
899 Normally, we'd expect to be here with parser->error set,
900 which disables these safeguards. But it's possible to get
901 here for secondary error recovery, after parser->error has
903 c_parser_consume_pragma (parser
);
904 c_parser_skip_to_pragma_eol (parser
);
905 parser
->error
= save_error
;
912 c_parser_consume_token (parser
);
916 parser
->error
= false;
919 /* Save the warning flags which are controlled by __extension__. */
922 disable_extension_diagnostics (void)
925 | (warn_pointer_arith
<< 1)
926 | (warn_traditional
<< 2)
929 warn_pointer_arith
= 0;
930 warn_traditional
= 0;
935 /* Restore the warning flags which are controlled by __extension__.
936 FLAGS is the return value from disable_extension_diagnostics. */
939 restore_extension_diagnostics (int flags
)
941 pedantic
= flags
& 1;
942 warn_pointer_arith
= (flags
>> 1) & 1;
943 warn_traditional
= (flags
>> 2) & 1;
944 flag_iso
= (flags
>> 3) & 1;
947 /* Possibly kinds of declarator to parse. */
948 typedef enum c_dtr_syn
{
949 /* A normal declarator with an identifier. */
951 /* An abstract declarator (maybe empty). */
953 /* A parameter declarator: may be either, but after a type name does
954 not redeclare a typedef name as an identifier if it can
955 alternatively be interpreted as a typedef name; see DR#009,
956 applied in C90 TC1, omitted from C99 and reapplied in C99 TC2
957 following DR#249. For example, given a typedef T, "int T" and
958 "int *T" are valid parameter declarations redeclaring T, while
959 "int (T)" and "int * (T)" and "int (T[])" and "int (T (int))" are
960 abstract declarators rather than involving redundant parentheses;
961 the same applies with attributes inside the parentheses before
966 static void c_parser_external_declaration (c_parser
*);
967 static void c_parser_asm_definition (c_parser
*);
968 static void c_parser_declaration_or_fndef (c_parser
*, bool, bool, bool, bool);
969 static void c_parser_declspecs (c_parser
*, struct c_declspecs
*, bool, bool,
971 static struct c_typespec
c_parser_enum_specifier (c_parser
*);
972 static struct c_typespec
c_parser_struct_or_union_specifier (c_parser
*);
973 static tree
c_parser_struct_declaration (c_parser
*);
974 static struct c_typespec
c_parser_typeof_specifier (c_parser
*);
975 static struct c_declarator
*c_parser_declarator (c_parser
*, bool, c_dtr_syn
,
977 static struct c_declarator
*c_parser_direct_declarator (c_parser
*, bool,
979 static struct c_declarator
*c_parser_direct_declarator_inner (c_parser
*,
981 struct c_declarator
*);
982 static struct c_arg_info
*c_parser_parms_declarator (c_parser
*, bool, tree
);
983 static struct c_arg_info
*c_parser_parms_list_declarator (c_parser
*, tree
);
984 static struct c_parm
*c_parser_parameter_declaration (c_parser
*, tree
);
985 static tree
c_parser_simple_asm_expr (c_parser
*);
986 static tree
c_parser_attributes (c_parser
*);
987 static struct c_type_name
*c_parser_type_name (c_parser
*);
988 static struct c_expr
c_parser_initializer (c_parser
*);
989 static struct c_expr
c_parser_braced_init (c_parser
*, tree
, bool);
990 static void c_parser_initelt (c_parser
*);
991 static void c_parser_initval (c_parser
*, struct c_expr
*);
992 static tree
c_parser_compound_statement (c_parser
*);
993 static void c_parser_compound_statement_nostart (c_parser
*);
994 static void c_parser_label (c_parser
*);
995 static void c_parser_statement (c_parser
*);
996 static void c_parser_statement_after_labels (c_parser
*);
997 static void c_parser_if_statement (c_parser
*);
998 static void c_parser_switch_statement (c_parser
*);
999 static void c_parser_while_statement (c_parser
*);
1000 static void c_parser_do_statement (c_parser
*);
1001 static void c_parser_for_statement (c_parser
*);
1002 static tree
c_parser_asm_statement (c_parser
*);
1003 static tree
c_parser_asm_operands (c_parser
*, bool);
1004 static tree
c_parser_asm_clobbers (c_parser
*);
1005 static struct c_expr
c_parser_expr_no_commas (c_parser
*, struct c_expr
*);
1006 static struct c_expr
c_parser_conditional_expression (c_parser
*,
1008 static struct c_expr
c_parser_binary_expression (c_parser
*, struct c_expr
*);
1009 static struct c_expr
c_parser_cast_expression (c_parser
*, struct c_expr
*);
1010 static struct c_expr
c_parser_unary_expression (c_parser
*);
1011 static struct c_expr
c_parser_sizeof_expression (c_parser
*);
1012 static struct c_expr
c_parser_alignof_expression (c_parser
*);
1013 static struct c_expr
c_parser_postfix_expression (c_parser
*);
1014 static struct c_expr
c_parser_postfix_expression_after_paren_type (c_parser
*,
1015 struct c_type_name
*);
1016 static struct c_expr
c_parser_postfix_expression_after_primary (c_parser
*,
1018 static struct c_expr
c_parser_expression (c_parser
*);
1019 static struct c_expr
c_parser_expression_conv (c_parser
*);
1020 static tree
c_parser_expr_list (c_parser
*, bool);
1021 static void c_parser_omp_construct (c_parser
*);
1022 static void c_parser_omp_threadprivate (c_parser
*);
1023 static void c_parser_omp_barrier (c_parser
*);
1024 static void c_parser_omp_flush (c_parser
*);
1026 enum pragma_context
{ pragma_external
, pragma_stmt
, pragma_compound
};
1027 static bool c_parser_pragma (c_parser
*, enum pragma_context
);
1029 /* These Objective-C parser functions are only ever called when
1030 compiling Objective-C. */
1031 static void c_parser_objc_class_definition (c_parser
*);
1032 static void c_parser_objc_class_instance_variables (c_parser
*);
1033 static void c_parser_objc_class_declaration (c_parser
*);
1034 static void c_parser_objc_alias_declaration (c_parser
*);
1035 static void c_parser_objc_protocol_definition (c_parser
*);
1036 static enum tree_code
c_parser_objc_method_type (c_parser
*);
1037 static void c_parser_objc_method_definition (c_parser
*);
1038 static void c_parser_objc_methodprotolist (c_parser
*);
1039 static void c_parser_objc_methodproto (c_parser
*);
1040 static tree
c_parser_objc_method_decl (c_parser
*);
1041 static tree
c_parser_objc_type_name (c_parser
*);
1042 static tree
c_parser_objc_protocol_refs (c_parser
*);
1043 static void c_parser_objc_try_catch_statement (c_parser
*);
1044 static void c_parser_objc_synchronized_statement (c_parser
*);
1045 static tree
c_parser_objc_selector (c_parser
*);
1046 static tree
c_parser_objc_selector_arg (c_parser
*);
1047 static tree
c_parser_objc_receiver (c_parser
*);
1048 static tree
c_parser_objc_message_args (c_parser
*);
1049 static tree
c_parser_objc_keywordexpr (c_parser
*);
1051 /* Parse a translation unit (C90 6.7, C99 6.9).
1054 external-declarations
1056 external-declarations:
1057 external-declaration
1058 external-declarations external-declaration
1067 c_parser_translation_unit (c_parser
*parser
)
1069 if (c_parser_next_token_is (parser
, CPP_EOF
))
1072 pedwarn ("%HISO C forbids an empty source file",
1073 &c_parser_peek_token (parser
)->location
);
1077 void *obstack_position
= obstack_alloc (&parser_obstack
, 0);
1081 c_parser_external_declaration (parser
);
1082 obstack_free (&parser_obstack
, obstack_position
);
1084 while (c_parser_next_token_is_not (parser
, CPP_EOF
));
1088 /* Parse an external declaration (C90 6.7, C99 6.9).
1090 external-declaration:
1096 external-declaration:
1099 __extension__ external-declaration
1103 external-declaration:
1104 objc-class-definition
1105 objc-class-declaration
1106 objc-alias-declaration
1107 objc-protocol-definition
1108 objc-method-definition
1113 c_parser_external_declaration (c_parser
*parser
)
1116 switch (c_parser_peek_token (parser
)->type
)
1119 switch (c_parser_peek_token (parser
)->keyword
)
1122 ext
= disable_extension_diagnostics ();
1123 c_parser_consume_token (parser
);
1124 c_parser_external_declaration (parser
);
1125 restore_extension_diagnostics (ext
);
1128 c_parser_asm_definition (parser
);
1130 case RID_AT_INTERFACE
:
1131 case RID_AT_IMPLEMENTATION
:
1132 gcc_assert (c_dialect_objc ());
1133 c_parser_objc_class_definition (parser
);
1136 gcc_assert (c_dialect_objc ());
1137 c_parser_objc_class_declaration (parser
);
1140 gcc_assert (c_dialect_objc ());
1141 c_parser_objc_alias_declaration (parser
);
1143 case RID_AT_PROTOCOL
:
1144 gcc_assert (c_dialect_objc ());
1145 c_parser_objc_protocol_definition (parser
);
1148 gcc_assert (c_dialect_objc ());
1149 c_parser_consume_token (parser
);
1150 objc_finish_implementation ();
1158 pedwarn ("%HISO C does not allow extra %<;%> outside of a function",
1159 &c_parser_peek_token (parser
)->location
);
1160 c_parser_consume_token (parser
);
1163 c_parser_pragma (parser
, pragma_external
);
1167 if (c_dialect_objc ())
1169 c_parser_objc_method_definition (parser
);
1172 /* Else fall through, and yield a syntax error trying to parse
1173 as a declaration or function definition. */
1176 /* A declaration or a function definition. We can only tell
1177 which after parsing the declaration specifiers, if any, and
1178 the first declarator. */
1179 c_parser_declaration_or_fndef (parser
, true, true, false, true);
1185 /* Parse a declaration or function definition (C90 6.5, 6.7.1, C99
1186 6.7, 6.9.1). If FNDEF_OK is true, a function definition is
1187 accepted; otherwise (old-style parameter declarations) only other
1188 declarations are accepted. If NESTED is true, we are inside a
1189 function or parsing old-style parameter declarations; any functions
1190 encountered are nested functions and declaration specifiers are
1191 required; otherwise we are at top level and functions are normal
1192 functions and declaration specifiers may be optional. If EMPTY_OK
1193 is true, empty declarations are OK (subject to all other
1194 constraints); otherwise (old-style parameter declarations) they are
1195 diagnosed. If START_ATTR_OK is true, the declaration specifiers
1196 may start with attributes; otherwise they may not.
1199 declaration-specifiers init-declarator-list[opt] ;
1201 function-definition:
1202 declaration-specifiers[opt] declarator declaration-list[opt]
1207 declaration-list declaration
1209 init-declarator-list:
1211 init-declarator-list , init-declarator
1214 declarator simple-asm-expr[opt] attributes[opt]
1215 declarator simple-asm-expr[opt] attributes[opt] = initializer
1219 nested-function-definition:
1220 declaration-specifiers declarator declaration-list[opt]
1223 The simple-asm-expr and attributes are GNU extensions.
1225 This function does not handle __extension__; that is handled in its
1226 callers. ??? Following the old parser, __extension__ may start
1227 external declarations, declarations in functions and declarations
1228 at the start of "for" loops, but not old-style parameter
1231 C99 requires declaration specifiers in a function definition; the
1232 absence is diagnosed through the diagnosis of implicit int. In GNU
1233 C we also allow but diagnose declarations without declaration
1234 specifiers, but only at top level (elsewhere they conflict with
1240 threadprivate-directive */
1243 c_parser_declaration_or_fndef (c_parser
*parser
, bool fndef_ok
, bool empty_ok
,
1244 bool nested
, bool start_attr_ok
)
1246 struct c_declspecs
*specs
;
1248 tree all_prefix_attrs
;
1249 bool diagnosed_no_specs
= false;
1250 location_t here
= c_parser_peek_token (parser
)->location
;
1252 specs
= build_null_declspecs ();
1253 c_parser_declspecs (parser
, specs
, true, true, start_attr_ok
);
1256 c_parser_skip_to_end_of_block_or_statement (parser
);
1259 if (nested
&& !specs
->declspecs_seen_p
)
1261 c_parser_error (parser
, "expected declaration specifiers");
1262 c_parser_skip_to_end_of_block_or_statement (parser
);
1265 finish_declspecs (specs
);
1266 if (c_parser_next_token_is (parser
, CPP_SEMICOLON
))
1272 shadow_tag_warned (specs
, 1);
1273 pedwarn ("%Hempty declaration", &here
);
1275 c_parser_consume_token (parser
);
1278 pending_xref_error ();
1279 prefix_attrs
= specs
->attrs
;
1280 all_prefix_attrs
= prefix_attrs
;
1281 specs
->attrs
= NULL_TREE
;
1284 struct c_declarator
*declarator
;
1287 /* Declaring either one or more declarators (in which case we
1288 should diagnose if there were no declaration specifiers) or a
1289 function definition (in which case the diagnostic for
1290 implicit int suffices). */
1291 declarator
= c_parser_declarator (parser
, specs
->type_seen_p
,
1292 C_DTR_NORMAL
, &dummy
);
1293 if (declarator
== NULL
)
1295 c_parser_skip_to_end_of_block_or_statement (parser
);
1298 if (c_parser_next_token_is (parser
, CPP_EQ
)
1299 || c_parser_next_token_is (parser
, CPP_COMMA
)
1300 || c_parser_next_token_is (parser
, CPP_SEMICOLON
)
1301 || c_parser_next_token_is_keyword (parser
, RID_ASM
)
1302 || c_parser_next_token_is_keyword (parser
, RID_ATTRIBUTE
))
1304 tree asm_name
= NULL_TREE
;
1305 tree postfix_attrs
= NULL_TREE
;
1306 if (!diagnosed_no_specs
&& !specs
->declspecs_seen_p
)
1308 diagnosed_no_specs
= true;
1309 pedwarn ("%Hdata definition has no type or storage class",
1312 /* Having seen a data definition, there cannot now be a
1313 function definition. */
1315 if (c_parser_next_token_is_keyword (parser
, RID_ASM
))
1316 asm_name
= c_parser_simple_asm_expr (parser
);
1317 if (c_parser_next_token_is_keyword (parser
, RID_ATTRIBUTE
))
1318 postfix_attrs
= c_parser_attributes (parser
);
1319 if (c_parser_next_token_is (parser
, CPP_EQ
))
1323 c_parser_consume_token (parser
);
1324 /* The declaration of the variable is in effect while
1325 its initializer is parsed. */
1326 d
= start_decl (declarator
, specs
, true,
1327 chainon (postfix_attrs
, all_prefix_attrs
));
1329 d
= error_mark_node
;
1330 start_init (d
, asm_name
, global_bindings_p ());
1331 init
= c_parser_initializer (parser
);
1333 if (d
!= error_mark_node
)
1335 maybe_warn_string_init (TREE_TYPE (d
), init
);
1336 finish_decl (d
, init
.value
, asm_name
);
1341 tree d
= start_decl (declarator
, specs
, false,
1342 chainon (postfix_attrs
,
1345 finish_decl (d
, NULL_TREE
, asm_name
);
1347 if (c_parser_next_token_is (parser
, CPP_COMMA
))
1349 c_parser_consume_token (parser
);
1350 if (c_parser_next_token_is_keyword (parser
, RID_ATTRIBUTE
))
1351 all_prefix_attrs
= chainon (c_parser_attributes (parser
),
1354 all_prefix_attrs
= prefix_attrs
;
1357 else if (c_parser_next_token_is (parser
, CPP_SEMICOLON
))
1359 c_parser_consume_token (parser
);
1364 c_parser_error (parser
, "expected %<,%> or %<;%>");
1365 c_parser_skip_to_end_of_block_or_statement (parser
);
1371 c_parser_error (parser
, "expected %<=%>, %<,%>, %<;%>, "
1372 "%<asm%> or %<__attribute__%>");
1373 c_parser_skip_to_end_of_block_or_statement (parser
);
1376 /* Function definition (nested or otherwise). */
1380 pedwarn ("%HISO C forbids nested functions", &here
);
1381 push_function_context ();
1383 if (!start_function (specs
, declarator
, all_prefix_attrs
))
1385 /* This can appear in many cases looking nothing like a
1386 function definition, so we don't give a more specific
1387 error suggesting there was one. */
1388 c_parser_error (parser
, "expected %<=%>, %<,%>, %<;%>, %<asm%> "
1389 "or %<__attribute__%>");
1391 pop_function_context ();
1394 /* Parse old-style parameter declarations. ??? Attributes are
1395 not allowed to start declaration specifiers here because of a
1396 syntax conflict between a function declaration with attribute
1397 suffix and a function definition with an attribute prefix on
1398 first old-style parameter declaration. Following the old
1399 parser, they are not accepted on subsequent old-style
1400 parameter declarations either. However, there is no
1401 ambiguity after the first declaration, nor indeed on the
1402 first as long as we don't allow postfix attributes after a
1403 declarator with a nonempty identifier list in a definition;
1404 and postfix attributes have never been accepted here in
1405 function definitions either. */
1406 while (c_parser_next_token_is_not (parser
, CPP_EOF
)
1407 && c_parser_next_token_is_not (parser
, CPP_OPEN_BRACE
))
1408 c_parser_declaration_or_fndef (parser
, false, false, true, false);
1409 DECL_SOURCE_LOCATION (current_function_decl
)
1410 = c_parser_peek_token (parser
)->location
;
1411 store_parm_decls ();
1412 fnbody
= c_parser_compound_statement (parser
);
1415 tree decl
= current_function_decl
;
1418 pop_function_context ();
1419 add_stmt (build_stmt (DECL_EXPR
, decl
));
1430 /* Parse an asm-definition (asm() outside a function body). This is a
1438 c_parser_asm_definition (c_parser
*parser
)
1440 tree asm_str
= c_parser_simple_asm_expr (parser
);
1442 cgraph_add_asm_node (asm_str
);
1443 c_parser_skip_until_found (parser
, CPP_SEMICOLON
, "expected %<;%>");
1446 /* Parse some declaration specifiers (possibly none) (C90 6.5, C99
1447 6.7), adding them to SPECS (which may already include some).
1448 Storage class specifiers are accepted iff SCSPEC_OK; type
1449 specifiers are accepted iff TYPESPEC_OK; attributes are accepted at
1450 the start iff START_ATTR_OK.
1452 declaration-specifiers:
1453 storage-class-specifier declaration-specifiers[opt]
1454 type-specifier declaration-specifiers[opt]
1455 type-qualifier declaration-specifiers[opt]
1456 function-specifier declaration-specifiers[opt]
1458 Function specifiers (inline) are from C99, and are currently
1459 handled as storage class specifiers, as is __thread.
1461 C90 6.5.1, C99 6.7.1:
1462 storage-class-specifier:
1473 C90 6.5.2, C99 6.7.2:
1486 [_Imaginary removed in C99 TC2]
1487 struct-or-union-specifier
1491 (_Bool and _Complex are new in C99.)
1493 C90 6.5.3, C99 6.7.3:
1500 (restrict is new in C99.)
1504 declaration-specifiers:
1505 attributes declaration-specifiers[opt]
1507 storage-class-specifier:
1519 (_Fract, _Accum, and _Sat are new from ISO/IEC DTR 18037:
1520 http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1169.pdf)
1525 class-name objc-protocol-refs[opt]
1526 typedef-name objc-protocol-refs
1531 c_parser_declspecs (c_parser
*parser
, struct c_declspecs
*specs
,
1532 bool scspec_ok
, bool typespec_ok
, bool start_attr_ok
)
1534 bool attrs_ok
= start_attr_ok
;
1535 bool seen_type
= specs
->type_seen_p
;
1536 while (c_parser_next_token_is (parser
, CPP_NAME
)
1537 || c_parser_next_token_is (parser
, CPP_KEYWORD
)
1538 || (c_dialect_objc () && c_parser_next_token_is (parser
, CPP_LESS
)))
1540 struct c_typespec t
;
1542 if (c_parser_next_token_is (parser
, CPP_NAME
))
1544 tree value
= c_parser_peek_token (parser
)->value
;
1545 c_id_kind kind
= c_parser_peek_token (parser
)->id_kind
;
1546 /* This finishes the specifiers unless a type name is OK, it
1547 is declared as a type name and a type name hasn't yet
1549 if (!typespec_ok
|| seen_type
1550 || (kind
!= C_ID_TYPENAME
&& kind
!= C_ID_CLASSNAME
))
1552 c_parser_consume_token (parser
);
1555 if (kind
== C_ID_TYPENAME
1556 && (!c_dialect_objc ()
1557 || c_parser_next_token_is_not (parser
, CPP_LESS
)))
1559 t
.kind
= ctsk_typedef
;
1560 /* For a typedef name, record the meaning, not the name.
1561 In case of 'foo foo, bar;'. */
1562 t
.spec
= lookup_name (value
);
1566 tree proto
= NULL_TREE
;
1567 gcc_assert (c_dialect_objc ());
1569 if (c_parser_next_token_is (parser
, CPP_LESS
))
1570 proto
= c_parser_objc_protocol_refs (parser
);
1571 t
.spec
= objc_get_protocol_qualified_type (value
, proto
);
1573 declspecs_add_type (specs
, t
);
1576 if (c_parser_next_token_is (parser
, CPP_LESS
))
1578 /* Make "<SomeProtocol>" equivalent to "id <SomeProtocol>" -
1579 nisse@lysator.liu.se. */
1581 gcc_assert (c_dialect_objc ());
1582 if (!typespec_ok
|| seen_type
)
1584 proto
= c_parser_objc_protocol_refs (parser
);
1586 t
.spec
= objc_get_protocol_qualified_type (NULL_TREE
, proto
);
1587 declspecs_add_type (specs
, t
);
1590 gcc_assert (c_parser_next_token_is (parser
, CPP_KEYWORD
));
1591 switch (c_parser_peek_token (parser
)->keyword
)
1603 /* TODO: Distinguish between function specifiers (inline)
1604 and storage class specifiers, either here or in
1605 declspecs_add_scspec. */
1606 declspecs_add_scspec (specs
, c_parser_peek_token (parser
)->value
);
1607 c_parser_consume_token (parser
);
1630 if (c_dialect_objc ())
1631 parser
->objc_need_raw_identifier
= true;
1632 t
.kind
= ctsk_resword
;
1633 t
.spec
= c_parser_peek_token (parser
)->value
;
1634 declspecs_add_type (specs
, t
);
1635 c_parser_consume_token (parser
);
1642 t
= c_parser_enum_specifier (parser
);
1643 declspecs_add_type (specs
, t
);
1651 t
= c_parser_struct_or_union_specifier (parser
);
1652 declspecs_add_type (specs
, t
);
1655 /* ??? The old parser rejected typeof after other type
1656 specifiers, but is a syntax error the best way of
1658 if (!typespec_ok
|| seen_type
)
1662 t
= c_parser_typeof_specifier (parser
);
1663 declspecs_add_type (specs
, t
);
1669 declspecs_add_qual (specs
, c_parser_peek_token (parser
)->value
);
1670 c_parser_consume_token (parser
);
1675 attrs
= c_parser_attributes (parser
);
1676 declspecs_add_attrs (specs
, attrs
);
1685 /* Parse an enum specifier (C90 6.5.2.2, C99 6.7.2.2).
1688 enum attributes[opt] identifier[opt] { enumerator-list } attributes[opt]
1689 enum attributes[opt] identifier[opt] { enumerator-list , } attributes[opt]
1690 enum attributes[opt] identifier
1692 The form with trailing comma is new in C99. The forms with
1693 attributes are GNU extensions. In GNU C, we accept any expression
1694 without commas in the syntax (assignment expressions, not just
1695 conditional expressions); assignment expressions will be diagnosed
1700 enumerator-list , enumerator
1703 enumeration-constant
1704 enumeration-constant = constant-expression
1707 static struct c_typespec
1708 c_parser_enum_specifier (c_parser
*parser
)
1710 struct c_typespec ret
;
1712 tree ident
= NULL_TREE
;
1713 location_t ident_loc
;
1714 gcc_assert (c_parser_next_token_is_keyword (parser
, RID_ENUM
));
1715 c_parser_consume_token (parser
);
1716 attrs
= c_parser_attributes (parser
);
1717 /* Set the location in case we create a decl now. */
1718 c_parser_set_source_position_from_token (c_parser_peek_token (parser
));
1719 if (c_parser_next_token_is (parser
, CPP_NAME
))
1721 ident
= c_parser_peek_token (parser
)->value
;
1722 ident_loc
= c_parser_peek_token (parser
)->location
;
1723 c_parser_consume_token (parser
);
1725 if (c_parser_next_token_is (parser
, CPP_OPEN_BRACE
))
1727 /* Parse an enum definition. */
1728 struct c_enum_contents the_enum
;
1729 tree type
= start_enum (&the_enum
, ident
);
1731 /* We chain the enumerators in reverse order, then put them in
1732 forward order at the end. */
1733 tree values
= NULL_TREE
;
1734 c_parser_consume_token (parser
);
1742 location_t comma_loc
;
1743 if (c_parser_next_token_is_not (parser
, CPP_NAME
))
1745 c_parser_error (parser
, "expected identifier");
1746 c_parser_skip_until_found (parser
, CPP_CLOSE_BRACE
, NULL
);
1747 values
= error_mark_node
;
1750 token
= c_parser_peek_token (parser
);
1751 enum_id
= token
->value
;
1752 /* Set the location in case we create a decl now. */
1753 c_parser_set_source_position_from_token (token
);
1754 c_parser_consume_token (parser
);
1755 if (c_parser_next_token_is (parser
, CPP_EQ
))
1757 c_parser_consume_token (parser
);
1758 enum_value
= c_parser_expr_no_commas (parser
, NULL
).value
;
1761 enum_value
= NULL_TREE
;
1762 enum_decl
= build_enumerator (&the_enum
, enum_id
, enum_value
);
1763 TREE_CHAIN (enum_decl
) = values
;
1766 if (c_parser_next_token_is (parser
, CPP_COMMA
))
1768 comma_loc
= c_parser_peek_token (parser
)->location
;
1770 c_parser_consume_token (parser
);
1772 if (c_parser_next_token_is (parser
, CPP_CLOSE_BRACE
))
1774 if (seen_comma
&& pedantic
&& !flag_isoc99
)
1775 pedwarn ("%Hcomma at end of enumerator list", &comma_loc
);
1776 c_parser_consume_token (parser
);
1781 c_parser_error (parser
, "expected %<,%> or %<}%>");
1782 c_parser_skip_until_found (parser
, CPP_CLOSE_BRACE
, NULL
);
1783 values
= error_mark_node
;
1787 postfix_attrs
= c_parser_attributes (parser
);
1788 ret
.spec
= finish_enum (type
, nreverse (values
),
1789 chainon (attrs
, postfix_attrs
));
1790 ret
.kind
= ctsk_tagdef
;
1795 c_parser_error (parser
, "expected %<{%>");
1796 ret
.spec
= error_mark_node
;
1797 ret
.kind
= ctsk_tagref
;
1800 ret
= parser_xref_tag (ENUMERAL_TYPE
, ident
);
1801 /* In ISO C, enumerated types can be referred to only if already
1803 if (pedantic
&& !COMPLETE_TYPE_P (ret
.spec
))
1806 pedwarn ("%HISO C forbids forward references to %<enum%> types",
1812 /* Parse a struct or union specifier (C90 6.5.2.1, C99 6.7.2.1).
1814 struct-or-union-specifier:
1815 struct-or-union attributes[opt] identifier[opt]
1816 { struct-contents } attributes[opt]
1817 struct-or-union attributes[opt] identifier
1820 struct-declaration-list
1822 struct-declaration-list:
1823 struct-declaration ;
1824 struct-declaration-list struct-declaration ;
1831 struct-declaration-list struct-declaration
1833 struct-declaration-list:
1834 struct-declaration-list ;
1837 (Note that in the syntax here, unlike that in ISO C, the semicolons
1838 are included here rather than in struct-declaration, in order to
1839 describe the syntax with extra semicolons and missing semicolon at
1844 struct-declaration-list:
1845 @defs ( class-name )
1847 (Note this does not include a trailing semicolon, but can be
1848 followed by further declarations, and gets a pedwarn-if-pedantic
1849 when followed by a semicolon.) */
1851 static struct c_typespec
1852 c_parser_struct_or_union_specifier (c_parser
*parser
)
1854 struct c_typespec ret
;
1856 tree ident
= NULL_TREE
;
1857 enum tree_code code
;
1858 switch (c_parser_peek_token (parser
)->keyword
)
1869 c_parser_consume_token (parser
);
1870 attrs
= c_parser_attributes (parser
);
1871 /* Set the location in case we create a decl now. */
1872 c_parser_set_source_position_from_token (c_parser_peek_token (parser
));
1873 if (c_parser_next_token_is (parser
, CPP_NAME
))
1875 ident
= c_parser_peek_token (parser
)->value
;
1876 c_parser_consume_token (parser
);
1878 if (c_parser_next_token_is (parser
, CPP_OPEN_BRACE
))
1880 /* Parse a struct or union definition. Start the scope of the
1881 tag before parsing components. */
1882 tree type
= start_struct (code
, ident
);
1884 /* We chain the components in reverse order, then put them in
1885 forward order at the end. Each struct-declaration may
1886 declare multiple components (comma-separated), so we must use
1887 chainon to join them, although when parsing each
1888 struct-declaration we can use TREE_CHAIN directly.
1890 The theory behind all this is that there will be more
1891 semicolon separated fields than comma separated fields, and
1892 so we'll be minimizing the number of node traversals required
1894 tree contents
= NULL_TREE
;
1895 c_parser_consume_token (parser
);
1896 /* Handle the Objective-C @defs construct,
1897 e.g. foo(sizeof(struct{ @defs(ClassName) }));. */
1898 if (c_parser_next_token_is_keyword (parser
, RID_AT_DEFS
))
1901 gcc_assert (c_dialect_objc ());
1902 c_parser_consume_token (parser
);
1903 if (!c_parser_require (parser
, CPP_OPEN_PAREN
, "expected %<(%>"))
1905 if (c_parser_next_token_is (parser
, CPP_NAME
)
1906 && c_parser_peek_token (parser
)->id_kind
== C_ID_CLASSNAME
)
1908 name
= c_parser_peek_token (parser
)->value
;
1909 c_parser_consume_token (parser
);
1913 c_parser_error (parser
, "expected class name");
1914 c_parser_skip_until_found (parser
, CPP_CLOSE_PAREN
, NULL
);
1917 c_parser_skip_until_found (parser
, CPP_CLOSE_PAREN
,
1919 contents
= nreverse (objc_get_class_ivars (name
));
1922 /* Parse the struct-declarations and semicolons. Problems with
1923 semicolons are diagnosed here; empty structures are diagnosed
1928 /* Parse any stray semicolon. */
1929 if (c_parser_next_token_is (parser
, CPP_SEMICOLON
))
1932 pedwarn ("%Hextra semicolon in struct or union specified",
1933 &c_parser_peek_token (parser
)->location
);
1934 c_parser_consume_token (parser
);
1937 /* Stop if at the end of the struct or union contents. */
1938 if (c_parser_next_token_is (parser
, CPP_CLOSE_BRACE
))
1940 c_parser_consume_token (parser
);
1943 /* Accept #pragmas at struct scope. */
1944 if (c_parser_next_token_is (parser
, CPP_PRAGMA
))
1946 c_parser_pragma (parser
, pragma_external
);
1949 /* Parse some comma-separated declarations, but not the
1950 trailing semicolon if any. */
1951 decls
= c_parser_struct_declaration (parser
);
1952 contents
= chainon (decls
, contents
);
1953 /* If no semicolon follows, either we have a parse error or
1954 are at the end of the struct or union and should
1956 if (c_parser_next_token_is (parser
, CPP_SEMICOLON
))
1957 c_parser_consume_token (parser
);
1960 if (c_parser_next_token_is (parser
, CPP_CLOSE_BRACE
))
1961 pedwarn ("%Hno semicolon at end of struct or union",
1962 &c_parser_peek_token (parser
)->location
);
1965 c_parser_error (parser
, "expected %<;%>");
1966 c_parser_skip_until_found (parser
, CPP_CLOSE_BRACE
, NULL
);
1971 postfix_attrs
= c_parser_attributes (parser
);
1972 ret
.spec
= finish_struct (type
, nreverse (contents
),
1973 chainon (attrs
, postfix_attrs
));
1974 ret
.kind
= ctsk_tagdef
;
1979 c_parser_error (parser
, "expected %<{%>");
1980 ret
.spec
= error_mark_node
;
1981 ret
.kind
= ctsk_tagref
;
1984 ret
= parser_xref_tag (code
, ident
);
1988 /* Parse a struct-declaration (C90 6.5.2.1, C99 6.7.2.1), *without*
1989 the trailing semicolon.
1992 specifier-qualifier-list struct-declarator-list
1994 specifier-qualifier-list:
1995 type-specifier specifier-qualifier-list[opt]
1996 type-qualifier specifier-qualifier-list[opt]
1997 attributes specifier-qualifier-list[opt]
1999 struct-declarator-list:
2001 struct-declarator-list , attributes[opt] struct-declarator
2004 declarator attributes[opt]
2005 declarator[opt] : constant-expression attributes[opt]
2010 __extension__ struct-declaration
2011 specifier-qualifier-list
2013 Unlike the ISO C syntax, semicolons are handled elsewhere. The use
2014 of attributes where shown is a GNU extension. In GNU C, we accept
2015 any expression without commas in the syntax (assignment
2016 expressions, not just conditional expressions); assignment
2017 expressions will be diagnosed as non-constant. */
2020 c_parser_struct_declaration (c_parser
*parser
)
2022 struct c_declspecs
*specs
;
2024 tree all_prefix_attrs
;
2026 location_t decl_loc
;
2027 if (c_parser_next_token_is_keyword (parser
, RID_EXTENSION
))
2031 ext
= disable_extension_diagnostics ();
2032 c_parser_consume_token (parser
);
2033 decl
= c_parser_struct_declaration (parser
);
2034 restore_extension_diagnostics (ext
);
2037 specs
= build_null_declspecs ();
2038 decl_loc
= c_parser_peek_token (parser
)->location
;
2039 c_parser_declspecs (parser
, specs
, false, true, true);
2042 if (!specs
->declspecs_seen_p
)
2044 c_parser_error (parser
, "expected specifier-qualifier-list");
2047 finish_declspecs (specs
);
2048 if (c_parser_next_token_is (parser
, CPP_SEMICOLON
))
2051 if (!specs
->type_seen_p
)
2054 pedwarn ("%HISO C forbids member declarations with no members",
2056 shadow_tag_warned (specs
, pedantic
);
2061 /* Support for unnamed structs or unions as members of
2062 structs or unions (which is [a] useful and [b] supports
2065 ret
= grokfield (build_id_declarator (NULL_TREE
), specs
,
2068 decl_attributes (&ret
, attrs
, 0);
2072 pending_xref_error ();
2073 prefix_attrs
= specs
->attrs
;
2074 all_prefix_attrs
= prefix_attrs
;
2075 specs
->attrs
= NULL_TREE
;
2079 /* Declaring one or more declarators or un-named bit-fields. */
2080 struct c_declarator
*declarator
;
2082 if (c_parser_next_token_is (parser
, CPP_COLON
))
2083 declarator
= build_id_declarator (NULL_TREE
);
2085 declarator
= c_parser_declarator (parser
, specs
->type_seen_p
,
2086 C_DTR_NORMAL
, &dummy
);
2087 if (declarator
== NULL
)
2089 c_parser_skip_to_end_of_block_or_statement (parser
);
2092 if (c_parser_next_token_is (parser
, CPP_COLON
)
2093 || c_parser_next_token_is (parser
, CPP_COMMA
)
2094 || c_parser_next_token_is (parser
, CPP_SEMICOLON
)
2095 || c_parser_next_token_is (parser
, CPP_CLOSE_BRACE
)
2096 || c_parser_next_token_is_keyword (parser
, RID_ATTRIBUTE
))
2098 tree postfix_attrs
= NULL_TREE
;
2099 tree width
= NULL_TREE
;
2101 if (c_parser_next_token_is (parser
, CPP_COLON
))
2103 c_parser_consume_token (parser
);
2104 width
= c_parser_expr_no_commas (parser
, NULL
).value
;
2106 if (c_parser_next_token_is_keyword (parser
, RID_ATTRIBUTE
))
2107 postfix_attrs
= c_parser_attributes (parser
);
2108 d
= grokfield (declarator
, specs
, width
, &all_prefix_attrs
);
2109 decl_attributes (&d
, chainon (postfix_attrs
,
2110 all_prefix_attrs
), 0);
2111 TREE_CHAIN (d
) = decls
;
2113 if (c_parser_next_token_is_keyword (parser
, RID_ATTRIBUTE
))
2114 all_prefix_attrs
= chainon (c_parser_attributes (parser
),
2117 all_prefix_attrs
= prefix_attrs
;
2118 if (c_parser_next_token_is (parser
, CPP_COMMA
))
2119 c_parser_consume_token (parser
);
2120 else if (c_parser_next_token_is (parser
, CPP_SEMICOLON
)
2121 || c_parser_next_token_is (parser
, CPP_CLOSE_BRACE
))
2123 /* Semicolon consumed in caller. */
2128 c_parser_error (parser
, "expected %<,%>, %<;%> or %<}%>");
2134 c_parser_error (parser
,
2135 "expected %<:%>, %<,%>, %<;%>, %<}%> or "
2136 "%<__attribute__%>");
2143 /* Parse a typeof specifier (a GNU extension).
2146 typeof ( expression )
2147 typeof ( type-name )
2150 static struct c_typespec
2151 c_parser_typeof_specifier (c_parser
*parser
)
2153 struct c_typespec ret
;
2154 ret
.kind
= ctsk_typeof
;
2155 ret
.spec
= error_mark_node
;
2156 gcc_assert (c_parser_next_token_is_keyword (parser
, RID_TYPEOF
));
2157 c_parser_consume_token (parser
);
2160 if (!c_parser_require (parser
, CPP_OPEN_PAREN
, "expected %<(%>"))
2166 if (c_parser_next_token_starts_typename (parser
))
2168 struct c_type_name
*type
= c_parser_type_name (parser
);
2173 ret
.spec
= groktypename (type
);
2174 pop_maybe_used (variably_modified_type_p (ret
.spec
, NULL_TREE
));
2180 location_t here
= c_parser_peek_token (parser
)->location
;
2181 struct c_expr expr
= c_parser_expression (parser
);
2184 if (TREE_CODE (expr
.value
) == COMPONENT_REF
2185 && DECL_C_BIT_FIELD (TREE_OPERAND (expr
.value
, 1)))
2186 error ("%H%<typeof%> applied to a bit-field", &here
);
2187 ret
.spec
= TREE_TYPE (expr
.value
);
2188 was_vm
= variably_modified_type_p (ret
.spec
, NULL_TREE
);
2189 /* This should be returned with the type so that when the type
2190 is evaluated, this can be evaluated. For now, we avoid
2191 evaluation when the context might. */
2192 if (!skip_evaluation
&& was_vm
)
2194 tree e
= expr
.value
;
2196 /* If the expression is not of a type to which we cannot assign a line
2197 number, wrap the thing in a no-op NOP_EXPR. */
2198 if (DECL_P (e
) || CONSTANT_CLASS_P (e
))
2199 e
= build1 (NOP_EXPR
, void_type_node
, e
);
2201 if (CAN_HAVE_LOCATION_P (e
))
2202 SET_EXPR_LOCATION (e
, input_location
);
2206 pop_maybe_used (was_vm
);
2208 c_parser_skip_until_found (parser
, CPP_CLOSE_PAREN
, "expected %<)%>");
2212 /* Parse a declarator, possibly an abstract declarator (C90 6.5.4,
2213 6.5.5, C99 6.7.5, 6.7.6). If TYPE_SEEN_P then a typedef name may
2214 be redeclared; otherwise it may not. KIND indicates which kind of
2215 declarator is wanted. Returns a valid declarator except in the
2216 case of a syntax error in which case NULL is returned. *SEEN_ID is
2217 set to true if an identifier being declared is seen; this is used
2218 to diagnose bad forms of abstract array declarators and to
2219 determine whether an identifier list is syntactically permitted.
2222 pointer[opt] direct-declarator
2226 ( attributes[opt] declarator )
2227 direct-declarator array-declarator
2228 direct-declarator ( parameter-type-list )
2229 direct-declarator ( identifier-list[opt] )
2232 * type-qualifier-list[opt]
2233 * type-qualifier-list[opt] pointer
2235 type-qualifier-list:
2238 type-qualifier-list type-qualifier
2239 type-qualifier-list attributes
2241 parameter-type-list:
2243 parameter-list , ...
2246 parameter-declaration
2247 parameter-list , parameter-declaration
2249 parameter-declaration:
2250 declaration-specifiers declarator attributes[opt]
2251 declaration-specifiers abstract-declarator[opt] attributes[opt]
2255 identifier-list , identifier
2257 abstract-declarator:
2259 pointer[opt] direct-abstract-declarator
2261 direct-abstract-declarator:
2262 ( attributes[opt] abstract-declarator )
2263 direct-abstract-declarator[opt] array-declarator
2264 direct-abstract-declarator[opt] ( parameter-type-list[opt] )
2269 direct-declarator ( parameter-forward-declarations
2270 parameter-type-list[opt] )
2272 direct-abstract-declarator:
2273 direct-abstract-declarator[opt] ( parameter-forward-declarations
2274 parameter-type-list[opt] )
2276 parameter-forward-declarations:
2278 parameter-forward-declarations parameter-list ;
2280 The uses of attributes shown above are GNU extensions.
2282 Some forms of array declarator are not included in C99 in the
2283 syntax for abstract declarators; these are disallowed elsewhere.
2284 This may be a defect (DR#289).
2286 This function also accepts an omitted abstract declarator as being
2287 an abstract declarator, although not part of the formal syntax. */
2289 static struct c_declarator
*
2290 c_parser_declarator (c_parser
*parser
, bool type_seen_p
, c_dtr_syn kind
,
2293 /* Parse any initial pointer part. */
2294 if (c_parser_next_token_is (parser
, CPP_MULT
))
2296 struct c_declspecs
*quals_attrs
= build_null_declspecs ();
2297 struct c_declarator
*inner
;
2298 c_parser_consume_token (parser
);
2299 c_parser_declspecs (parser
, quals_attrs
, false, false, true);
2300 inner
= c_parser_declarator (parser
, type_seen_p
, kind
, seen_id
);
2304 return make_pointer_declarator (quals_attrs
, inner
);
2306 /* Now we have a direct declarator, direct abstract declarator or
2307 nothing (which counts as a direct abstract declarator here). */
2308 return c_parser_direct_declarator (parser
, type_seen_p
, kind
, seen_id
);
2311 /* Parse a direct declarator or direct abstract declarator; arguments
2312 as c_parser_declarator. */
2314 static struct c_declarator
*
2315 c_parser_direct_declarator (c_parser
*parser
, bool type_seen_p
, c_dtr_syn kind
,
2318 /* The direct declarator must start with an identifier (possibly
2319 omitted) or a parenthesized declarator (possibly abstract). In
2320 an ordinary declarator, initial parentheses must start a
2321 parenthesized declarator. In an abstract declarator or parameter
2322 declarator, they could start a parenthesized declarator or a
2323 parameter list. To tell which, the open parenthesis and any
2324 following attributes must be read. If a declaration specifier
2325 follows, then it is a parameter list; if the specifier is a
2326 typedef name, there might be an ambiguity about redeclaring it,
2327 which is resolved in the direction of treating it as a typedef
2328 name. If a close parenthesis follows, it is also an empty
2329 parameter list, as the syntax does not permit empty abstract
2330 declarators. Otherwise, it is a parenthesized declarator (in
2331 which case the analysis may be repeated inside it, recursively).
2333 ??? There is an ambiguity in a parameter declaration "int
2334 (__attribute__((foo)) x)", where x is not a typedef name: it
2335 could be an abstract declarator for a function, or declare x with
2336 parentheses. The proper resolution of this ambiguity needs
2337 documenting. At present we follow an accident of the old
2338 parser's implementation, whereby the first parameter must have
2339 some declaration specifiers other than just attributes. Thus as
2340 a parameter declaration it is treated as a parenthesized
2341 parameter named x, and as an abstract declarator it is
2344 ??? Also following the old parser, attributes inside an empty
2345 parameter list are ignored, making it a list not yielding a
2346 prototype, rather than giving an error or making it have one
2347 parameter with implicit type int.
2349 ??? Also following the old parser, typedef names may be
2350 redeclared in declarators, but not Objective-C class names. */
2352 if (kind
!= C_DTR_ABSTRACT
2353 && c_parser_next_token_is (parser
, CPP_NAME
)
2355 && c_parser_peek_token (parser
)->id_kind
== C_ID_TYPENAME
)
2356 || c_parser_peek_token (parser
)->id_kind
== C_ID_ID
))
2358 struct c_declarator
*inner
2359 = build_id_declarator (c_parser_peek_token (parser
)->value
);
2361 inner
->id_loc
= c_parser_peek_token (parser
)->location
;
2362 c_parser_consume_token (parser
);
2363 return c_parser_direct_declarator_inner (parser
, *seen_id
, inner
);
2366 if (kind
!= C_DTR_NORMAL
2367 && c_parser_next_token_is (parser
, CPP_OPEN_SQUARE
))
2369 struct c_declarator
*inner
= build_id_declarator (NULL_TREE
);
2370 return c_parser_direct_declarator_inner (parser
, *seen_id
, inner
);
2373 /* Either we are at the end of an abstract declarator, or we have
2376 if (c_parser_next_token_is (parser
, CPP_OPEN_PAREN
))
2379 struct c_declarator
*inner
;
2380 c_parser_consume_token (parser
);
2381 attrs
= c_parser_attributes (parser
);
2382 if (kind
!= C_DTR_NORMAL
2383 && (c_parser_next_token_starts_declspecs (parser
)
2384 || c_parser_next_token_is (parser
, CPP_CLOSE_PAREN
)))
2386 struct c_arg_info
*args
2387 = c_parser_parms_declarator (parser
, kind
== C_DTR_NORMAL
,
2394 = build_function_declarator (args
,
2395 build_id_declarator (NULL_TREE
));
2396 return c_parser_direct_declarator_inner (parser
, *seen_id
,
2400 /* A parenthesized declarator. */
2401 inner
= c_parser_declarator (parser
, type_seen_p
, kind
, seen_id
);
2402 if (inner
!= NULL
&& attrs
!= NULL
)
2403 inner
= build_attrs_declarator (attrs
, inner
);
2404 if (c_parser_next_token_is (parser
, CPP_CLOSE_PAREN
))
2406 c_parser_consume_token (parser
);
2410 return c_parser_direct_declarator_inner (parser
, *seen_id
, inner
);
2414 c_parser_skip_until_found (parser
, CPP_CLOSE_PAREN
,
2421 if (kind
== C_DTR_NORMAL
)
2423 c_parser_error (parser
, "expected identifier or %<(%>");
2427 return build_id_declarator (NULL_TREE
);
2431 /* Parse part of a direct declarator or direct abstract declarator,
2432 given that some (in INNER) has already been parsed; ID_PRESENT is
2433 true if an identifier is present, false for an abstract
2436 static struct c_declarator
*
2437 c_parser_direct_declarator_inner (c_parser
*parser
, bool id_present
,
2438 struct c_declarator
*inner
)
2440 /* Parse a sequence of array declarators and parameter lists. */
2441 if (c_parser_next_token_is (parser
, CPP_OPEN_SQUARE
))
2443 struct c_declarator
*declarator
;
2444 struct c_declspecs
*quals_attrs
= build_null_declspecs ();
2448 c_parser_consume_token (parser
);
2449 c_parser_declspecs (parser
, quals_attrs
, false, false, true);
2450 static_seen
= c_parser_next_token_is_keyword (parser
, RID_STATIC
);
2452 c_parser_consume_token (parser
);
2453 if (static_seen
&& !quals_attrs
->declspecs_seen_p
)
2454 c_parser_declspecs (parser
, quals_attrs
, false, false, true);
2455 if (!quals_attrs
->declspecs_seen_p
)
2457 /* If "static" is present, there must be an array dimension.
2458 Otherwise, there may be a dimension, "*", or no
2463 dimen
= c_parser_expr_no_commas (parser
, NULL
).value
;
2467 if (c_parser_next_token_is (parser
, CPP_CLOSE_SQUARE
))
2472 else if (c_parser_next_token_is (parser
, CPP_MULT
))
2474 if (c_parser_peek_2nd_token (parser
)->type
== CPP_CLOSE_SQUARE
)
2478 c_parser_consume_token (parser
);
2483 dimen
= c_parser_expr_no_commas (parser
, NULL
).value
;
2489 dimen
= c_parser_expr_no_commas (parser
, NULL
).value
;
2492 if (c_parser_next_token_is (parser
, CPP_CLOSE_SQUARE
))
2493 c_parser_consume_token (parser
);
2496 c_parser_skip_until_found (parser
, CPP_CLOSE_SQUARE
,
2500 declarator
= build_array_declarator (dimen
, quals_attrs
, static_seen
,
2502 if (declarator
== NULL
)
2504 inner
= set_array_declarator_inner (declarator
, inner
);
2505 return c_parser_direct_declarator_inner (parser
, id_present
, inner
);
2507 else if (c_parser_next_token_is (parser
, CPP_OPEN_PAREN
))
2510 struct c_arg_info
*args
;
2511 c_parser_consume_token (parser
);
2512 attrs
= c_parser_attributes (parser
);
2513 args
= c_parser_parms_declarator (parser
, id_present
, attrs
);
2518 inner
= build_function_declarator (args
, inner
);
2519 return c_parser_direct_declarator_inner (parser
, id_present
, inner
);
2525 /* Parse a parameter list or identifier list, including the closing
2526 parenthesis but not the opening one. ATTRS are the attributes at
2527 the start of the list. ID_LIST_OK is true if an identifier list is
2528 acceptable; such a list must not have attributes at the start. */
2530 static struct c_arg_info
*
2531 c_parser_parms_declarator (c_parser
*parser
, bool id_list_ok
, tree attrs
)
2534 declare_parm_level ();
2535 /* If the list starts with an identifier, it is an identifier list.
2536 Otherwise, it is either a prototype list or an empty list. */
2539 && c_parser_next_token_is (parser
, CPP_NAME
)
2540 && c_parser_peek_token (parser
)->id_kind
== C_ID_ID
)
2542 tree list
= NULL_TREE
, *nextp
= &list
;
2543 while (c_parser_next_token_is (parser
, CPP_NAME
)
2544 && c_parser_peek_token (parser
)->id_kind
== C_ID_ID
)
2546 *nextp
= build_tree_list (NULL_TREE
,
2547 c_parser_peek_token (parser
)->value
);
2548 nextp
= & TREE_CHAIN (*nextp
);
2549 c_parser_consume_token (parser
);
2550 if (c_parser_next_token_is_not (parser
, CPP_COMMA
))
2552 c_parser_consume_token (parser
);
2553 if (c_parser_next_token_is (parser
, CPP_CLOSE_PAREN
))
2555 c_parser_error (parser
, "expected identifier");
2559 if (c_parser_next_token_is (parser
, CPP_CLOSE_PAREN
))
2561 struct c_arg_info
*ret
= XOBNEW (&parser_obstack
, struct c_arg_info
);
2566 ret
->pending_sizes
= 0;
2567 ret
->had_vla_unspec
= 0;
2568 c_parser_consume_token (parser
);
2574 c_parser_skip_until_found (parser
, CPP_CLOSE_PAREN
,
2582 struct c_arg_info
*ret
= c_parser_parms_list_declarator (parser
, attrs
);
2588 /* Parse a parameter list (possibly empty), including the closing
2589 parenthesis but not the opening one. ATTRS are the attributes at
2590 the start of the list. */
2592 static struct c_arg_info
*
2593 c_parser_parms_list_declarator (c_parser
*parser
, tree attrs
)
2595 bool good_parm
= false;
2596 /* ??? Following the old parser, forward parameter declarations may
2597 use abstract declarators, and if no real parameter declarations
2598 follow the forward declarations then this is not diagnosed. Also
2599 note as above that attributes are ignored as the only contents of
2600 the parentheses, or as the only contents after forward
2602 if (c_parser_next_token_is (parser
, CPP_CLOSE_PAREN
))
2604 struct c_arg_info
*ret
= XOBNEW (&parser_obstack
, struct c_arg_info
);
2609 ret
->pending_sizes
= 0;
2610 ret
->had_vla_unspec
= 0;
2611 c_parser_consume_token (parser
);
2614 if (c_parser_next_token_is (parser
, CPP_ELLIPSIS
))
2616 struct c_arg_info
*ret
= XOBNEW (&parser_obstack
, struct c_arg_info
);
2620 ret
->pending_sizes
= 0;
2621 ret
->had_vla_unspec
= 0;
2622 /* Suppress -Wold-style-definition for this case. */
2623 ret
->types
= error_mark_node
;
2624 error ("%HISO C requires a named argument before %<...%>",
2625 &c_parser_peek_token (parser
)->location
);
2626 c_parser_consume_token (parser
);
2627 if (c_parser_next_token_is (parser
, CPP_CLOSE_PAREN
))
2629 c_parser_consume_token (parser
);
2634 c_parser_skip_until_found (parser
, CPP_CLOSE_PAREN
,
2639 /* Nonempty list of parameters, either terminated with semicolon
2640 (forward declarations; recurse) or with close parenthesis (normal
2641 function) or with ", ... )" (variadic function). */
2644 /* Parse a parameter. */
2645 struct c_parm
*parm
= c_parser_parameter_declaration (parser
, attrs
);
2650 push_parm_decl (parm
);
2652 if (c_parser_next_token_is (parser
, CPP_SEMICOLON
))
2655 c_parser_consume_token (parser
);
2656 mark_forward_parm_decls ();
2657 new_attrs
= c_parser_attributes (parser
);
2658 return c_parser_parms_list_declarator (parser
, new_attrs
);
2660 if (c_parser_next_token_is (parser
, CPP_CLOSE_PAREN
))
2662 c_parser_consume_token (parser
);
2664 return get_parm_info (false);
2667 struct c_arg_info
*ret
2668 = XOBNEW (&parser_obstack
, struct c_arg_info
);
2673 ret
->pending_sizes
= 0;
2674 ret
->had_vla_unspec
= 0;
2678 if (!c_parser_require (parser
, CPP_COMMA
,
2679 "expected %<;%>, %<,%> or %<)%>"))
2681 c_parser_skip_until_found (parser
, CPP_CLOSE_PAREN
, NULL
);
2684 if (c_parser_next_token_is (parser
, CPP_ELLIPSIS
))
2686 c_parser_consume_token (parser
);
2687 if (c_parser_next_token_is (parser
, CPP_CLOSE_PAREN
))
2689 c_parser_consume_token (parser
);
2691 return get_parm_info (true);
2694 struct c_arg_info
*ret
2695 = XOBNEW (&parser_obstack
, struct c_arg_info
);
2700 ret
->pending_sizes
= 0;
2701 ret
->had_vla_unspec
= 0;
2707 c_parser_skip_until_found (parser
, CPP_CLOSE_PAREN
,
2715 /* Parse a parameter declaration. ATTRS are the attributes at the
2716 start of the declaration if it is the first parameter. */
2718 static struct c_parm
*
2719 c_parser_parameter_declaration (c_parser
*parser
, tree attrs
)
2721 struct c_declspecs
*specs
;
2722 struct c_declarator
*declarator
;
2724 tree postfix_attrs
= NULL_TREE
;
2726 if (!c_parser_next_token_starts_declspecs (parser
))
2728 /* ??? In some Objective-C cases '...' isn't applicable so there
2729 should be a different message. */
2730 c_parser_error (parser
,
2731 "expected declaration specifiers or %<...%>");
2732 c_parser_skip_to_end_of_parameter (parser
);
2735 specs
= build_null_declspecs ();
2738 declspecs_add_attrs (specs
, attrs
);
2741 c_parser_declspecs (parser
, specs
, true, true, true);
2742 finish_declspecs (specs
);
2743 pending_xref_error ();
2744 prefix_attrs
= specs
->attrs
;
2745 specs
->attrs
= NULL_TREE
;
2746 declarator
= c_parser_declarator (parser
, specs
->type_seen_p
,
2747 C_DTR_PARM
, &dummy
);
2748 if (declarator
== NULL
)
2750 c_parser_skip_until_found (parser
, CPP_COMMA
, NULL
);
2753 if (c_parser_next_token_is_keyword (parser
, RID_ATTRIBUTE
))
2754 postfix_attrs
= c_parser_attributes (parser
);
2755 return build_c_parm (specs
, chainon (postfix_attrs
, prefix_attrs
),
2759 /* Parse a string literal in an asm expression. It should not be
2760 translated, and wide string literals are an error although
2761 permitted by the syntax. This is a GNU extension.
2766 ??? At present, following the old parser, the caller needs to have
2767 set lex_untranslated_string to 1. It would be better to follow the
2768 C++ parser rather than using this kludge. */
2771 c_parser_asm_string_literal (c_parser
*parser
)
2774 if (c_parser_next_token_is (parser
, CPP_STRING
))
2776 str
= c_parser_peek_token (parser
)->value
;
2777 c_parser_consume_token (parser
);
2779 else if (c_parser_next_token_is (parser
, CPP_WSTRING
))
2781 error ("%Hwide string literal in %<asm%>",
2782 &c_parser_peek_token (parser
)->location
);
2783 str
= build_string (1, "");
2784 c_parser_consume_token (parser
);
2788 c_parser_error (parser
, "expected string literal");
2794 /* Parse a simple asm expression. This is used in restricted
2795 contexts, where a full expression with inputs and outputs does not
2796 make sense. This is a GNU extension.
2799 asm ( asm-string-literal )
2803 c_parser_simple_asm_expr (c_parser
*parser
)
2806 gcc_assert (c_parser_next_token_is_keyword (parser
, RID_ASM
));
2807 /* ??? Follow the C++ parser rather than using the
2808 lex_untranslated_string kludge. */
2809 parser
->lex_untranslated_string
= true;
2810 c_parser_consume_token (parser
);
2811 if (!c_parser_require (parser
, CPP_OPEN_PAREN
, "expected %<(%>"))
2813 parser
->lex_untranslated_string
= false;
2816 str
= c_parser_asm_string_literal (parser
);
2817 parser
->lex_untranslated_string
= false;
2818 if (!c_parser_require (parser
, CPP_CLOSE_PAREN
, "expected %<)%>"))
2820 c_parser_skip_until_found (parser
, CPP_CLOSE_PAREN
, NULL
);
2826 /* Parse (possibly empty) attributes. This is a GNU extension.
2830 attributes attribute
2833 __attribute__ ( ( attribute-list ) )
2837 attribute_list , attrib
2842 any-word ( identifier )
2843 any-word ( identifier , nonempty-expr-list )
2844 any-word ( expr-list )
2846 where the "identifier" must not be declared as a type, and
2847 "any-word" may be any identifier (including one declared as a
2848 type), a reserved word storage class specifier, type specifier or
2849 type qualifier. ??? This still leaves out most reserved keywords
2850 (following the old parser), shouldn't we include them, and why not
2851 allow identifiers declared as types to start the arguments? */
2854 c_parser_attributes (c_parser
*parser
)
2856 tree attrs
= NULL_TREE
;
2857 while (c_parser_next_token_is_keyword (parser
, RID_ATTRIBUTE
))
2859 /* ??? Follow the C++ parser rather than using the
2860 lex_untranslated_string kludge. */
2861 parser
->lex_untranslated_string
= true;
2862 c_parser_consume_token (parser
);
2863 if (!c_parser_require (parser
, CPP_OPEN_PAREN
, "expected %<(%>"))
2865 parser
->lex_untranslated_string
= false;
2868 if (!c_parser_require (parser
, CPP_OPEN_PAREN
, "expected %<(%>"))
2870 parser
->lex_untranslated_string
= false;
2871 c_parser_skip_until_found (parser
, CPP_CLOSE_PAREN
, NULL
);
2874 /* Parse the attribute list. */
2875 while (c_parser_next_token_is (parser
, CPP_COMMA
)
2876 || c_parser_next_token_is (parser
, CPP_NAME
)
2877 || c_parser_next_token_is (parser
, CPP_KEYWORD
))
2879 tree attr
, attr_name
, attr_args
;
2880 if (c_parser_next_token_is (parser
, CPP_COMMA
))
2882 c_parser_consume_token (parser
);
2885 if (c_parser_next_token_is (parser
, CPP_KEYWORD
))
2887 /* ??? See comment above about what keywords are
2890 switch (c_parser_peek_token (parser
)->keyword
)
2928 attr_name
= c_parser_peek_token (parser
)->value
;
2929 c_parser_consume_token (parser
);
2930 if (c_parser_next_token_is_not (parser
, CPP_OPEN_PAREN
))
2932 attr
= build_tree_list (attr_name
, NULL_TREE
);
2933 attrs
= chainon (attrs
, attr
);
2936 c_parser_consume_token (parser
);
2937 /* Parse the attribute contents. If they start with an
2938 identifier which is followed by a comma or close
2939 parenthesis, then the arguments start with that
2940 identifier; otherwise they are an expression list. */
2941 if (c_parser_next_token_is (parser
, CPP_NAME
)
2942 && c_parser_peek_token (parser
)->id_kind
== C_ID_ID
2943 && ((c_parser_peek_2nd_token (parser
)->type
== CPP_COMMA
)
2944 || (c_parser_peek_2nd_token (parser
)->type
2945 == CPP_CLOSE_PAREN
)))
2947 tree arg1
= c_parser_peek_token (parser
)->value
;
2948 c_parser_consume_token (parser
);
2949 if (c_parser_next_token_is (parser
, CPP_CLOSE_PAREN
))
2950 attr_args
= build_tree_list (NULL_TREE
, arg1
);
2953 c_parser_consume_token (parser
);
2954 attr_args
= tree_cons (NULL_TREE
, arg1
,
2955 c_parser_expr_list (parser
, false));
2960 if (c_parser_next_token_is (parser
, CPP_CLOSE_PAREN
))
2961 attr_args
= NULL_TREE
;
2963 attr_args
= c_parser_expr_list (parser
, false);
2965 attr
= build_tree_list (attr_name
, attr_args
);
2966 if (c_parser_next_token_is (parser
, CPP_CLOSE_PAREN
))
2967 c_parser_consume_token (parser
);
2970 parser
->lex_untranslated_string
= false;
2971 c_parser_skip_until_found (parser
, CPP_CLOSE_PAREN
,
2975 attrs
= chainon (attrs
, attr
);
2977 if (c_parser_next_token_is (parser
, CPP_CLOSE_PAREN
))
2978 c_parser_consume_token (parser
);
2981 parser
->lex_untranslated_string
= false;
2982 c_parser_skip_until_found (parser
, CPP_CLOSE_PAREN
,
2986 if (c_parser_next_token_is (parser
, CPP_CLOSE_PAREN
))
2987 c_parser_consume_token (parser
);
2990 parser
->lex_untranslated_string
= false;
2991 c_parser_skip_until_found (parser
, CPP_CLOSE_PAREN
,
2995 parser
->lex_untranslated_string
= false;
3000 /* Parse a type name (C90 6.5.5, C99 6.7.6).
3003 specifier-qualifier-list abstract-declarator[opt]
3006 static struct c_type_name
*
3007 c_parser_type_name (c_parser
*parser
)
3009 struct c_declspecs
*specs
= build_null_declspecs ();
3010 struct c_declarator
*declarator
;
3011 struct c_type_name
*ret
;
3013 c_parser_declspecs (parser
, specs
, false, true, true);
3014 if (!specs
->declspecs_seen_p
)
3016 c_parser_error (parser
, "expected specifier-qualifier-list");
3019 pending_xref_error ();
3020 finish_declspecs (specs
);
3021 declarator
= c_parser_declarator (parser
, specs
->type_seen_p
,
3022 C_DTR_ABSTRACT
, &dummy
);
3023 if (declarator
== NULL
)
3025 ret
= XOBNEW (&parser_obstack
, struct c_type_name
);
3027 ret
->declarator
= declarator
;
3031 /* Parse an initializer (C90 6.5.7, C99 6.7.8).
3034 assignment-expression
3035 { initializer-list }
3036 { initializer-list , }
3039 designation[opt] initializer
3040 initializer-list , designation[opt] initializer
3047 designator-list designator
3054 [ constant-expression ]
3066 [ constant-expression ... constant-expression ]
3068 Any expression without commas is accepted in the syntax for the
3069 constant-expressions, with non-constant expressions rejected later.
3071 This function is only used for top-level initializers; for nested
3072 ones, see c_parser_initval. */
3074 static struct c_expr
3075 c_parser_initializer (c_parser
*parser
)
3077 if (c_parser_next_token_is (parser
, CPP_OPEN_BRACE
))
3078 return c_parser_braced_init (parser
, NULL_TREE
, false);
3082 ret
= c_parser_expr_no_commas (parser
, NULL
);
3083 if (TREE_CODE (ret
.value
) != STRING_CST
3084 && TREE_CODE (ret
.value
) != COMPOUND_LITERAL_EXPR
)
3085 ret
= default_function_array_conversion (ret
);
3090 /* Parse a braced initializer list. TYPE is the type specified for a
3091 compound literal, and NULL_TREE for other initializers and for
3092 nested braced lists. NESTED_P is true for nested braced lists,
3093 false for the list of a compound literal or the list that is the
3094 top-level initializer in a declaration. */
3096 static struct c_expr
3097 c_parser_braced_init (c_parser
*parser
, tree type
, bool nested_p
)
3099 location_t brace_loc
= c_parser_peek_token (parser
)->location
;
3100 gcc_assert (c_parser_next_token_is (parser
, CPP_OPEN_BRACE
));
3101 c_parser_consume_token (parser
);
3103 push_init_level (0);
3105 really_start_incremental_init (type
);
3106 if (c_parser_next_token_is (parser
, CPP_CLOSE_BRACE
))
3109 pedwarn ("%HISO C forbids empty initializer braces", &brace_loc
);
3113 /* Parse a non-empty initializer list, possibly with a trailing
3117 c_parser_initelt (parser
);
3120 if (c_parser_next_token_is (parser
, CPP_COMMA
))
3121 c_parser_consume_token (parser
);
3124 if (c_parser_next_token_is (parser
, CPP_CLOSE_BRACE
))
3128 if (c_parser_next_token_is_not (parser
, CPP_CLOSE_BRACE
))
3131 ret
.value
= error_mark_node
;
3132 ret
.original_code
= ERROR_MARK
;
3133 c_parser_skip_until_found (parser
, CPP_CLOSE_BRACE
, "expected %<}%>");
3136 c_parser_consume_token (parser
);
3137 return pop_init_level (0);
3140 /* Parse a nested initializer, including designators. */
3143 c_parser_initelt (c_parser
*parser
)
3145 /* Parse any designator or designator list. A single array
3146 designator may have the subsequent "=" omitted in GNU C, but a
3147 longer list or a structure member designator may not. */
3148 if (c_parser_next_token_is (parser
, CPP_NAME
)
3149 && c_parser_peek_2nd_token (parser
)->type
== CPP_COLON
)
3151 /* Old-style structure member designator. */
3152 set_init_label (c_parser_peek_token (parser
)->value
);
3155 /* Use the colon as the error location. */
3156 pedwarn ("%Hobsolete use of designated initializer with %<:%>",
3157 &c_parser_peek_2nd_token (parser
)->location
);
3159 c_parser_consume_token (parser
);
3160 c_parser_consume_token (parser
);
3164 /* des_seen is 0 if there have been no designators, 1 if there
3165 has been a single array designator and 2 otherwise. */
3167 /* Location of a designator. */
3169 while (c_parser_next_token_is (parser
, CPP_OPEN_SQUARE
)
3170 || c_parser_next_token_is (parser
, CPP_DOT
))
3172 int des_prev
= des_seen
;
3174 des_loc
= c_parser_peek_token (parser
)->location
;
3177 if (c_parser_next_token_is (parser
, CPP_DOT
))
3180 c_parser_consume_token (parser
);
3181 if (c_parser_next_token_is (parser
, CPP_NAME
))
3183 set_init_label (c_parser_peek_token (parser
)->value
);
3184 c_parser_consume_token (parser
);
3189 init
.value
= error_mark_node
;
3190 init
.original_code
= ERROR_MARK
;
3191 c_parser_error (parser
, "expected identifier");
3192 c_parser_skip_until_found (parser
, CPP_COMMA
, NULL
);
3193 process_init_element (init
);
3200 location_t ellipsis_loc
;
3201 /* ??? Following the old parser, [ objc-receiver
3202 objc-message-args ] is accepted as an initializer,
3203 being distinguished from a designator by what follows
3204 the first assignment expression inside the square
3205 brackets, but after a first array designator a
3206 subsequent square bracket is for Objective-C taken to
3207 start an expression, using the obsolete form of
3208 designated initializer without '=', rather than
3209 possibly being a second level of designation: in LALR
3210 terms, the '[' is shifted rather than reducing
3211 designator to designator-list. */
3212 if (des_prev
== 1 && c_dialect_objc ())
3214 des_seen
= des_prev
;
3217 if (des_prev
== 0 && c_dialect_objc ())
3219 /* This might be an array designator or an
3220 Objective-C message expression. If the former,
3221 continue parsing here; if the latter, parse the
3222 remainder of the initializer given the starting
3223 primary-expression. ??? It might make sense to
3224 distinguish when des_prev == 1 as well; see
3225 previous comment. */
3227 struct c_expr mexpr
;
3228 c_parser_consume_token (parser
);
3229 if (c_parser_peek_token (parser
)->type
== CPP_NAME
3230 && ((c_parser_peek_token (parser
)->id_kind
3232 || (c_parser_peek_token (parser
)->id_kind
3233 == C_ID_CLASSNAME
)))
3235 /* Type name receiver. */
3236 tree id
= c_parser_peek_token (parser
)->value
;
3237 c_parser_consume_token (parser
);
3238 rec
= objc_get_class_reference (id
);
3239 goto parse_message_args
;
3241 first
= c_parser_expr_no_commas (parser
, NULL
).value
;
3242 if (c_parser_next_token_is (parser
, CPP_ELLIPSIS
)
3243 || c_parser_next_token_is (parser
, CPP_CLOSE_SQUARE
))
3244 goto array_desig_after_first
;
3245 /* Expression receiver. So far only one part
3246 without commas has been parsed; there might be
3247 more of the expression. */
3249 while (c_parser_next_token_is (parser
, CPP_COMMA
))
3252 c_parser_consume_token (parser
);
3253 next
= c_parser_expr_no_commas (parser
, NULL
);
3254 next
= default_function_array_conversion (next
);
3255 rec
= build_compound_expr (rec
, next
.value
);
3258 /* Now parse the objc-message-args. */
3259 args
= c_parser_objc_message_args (parser
);
3260 c_parser_skip_until_found (parser
, CPP_CLOSE_SQUARE
,
3263 = objc_build_message_expr (build_tree_list (rec
, args
));
3264 mexpr
.original_code
= ERROR_MARK
;
3265 /* Now parse and process the remainder of the
3266 initializer, starting with this message
3267 expression as a primary-expression. */
3268 c_parser_initval (parser
, &mexpr
);
3271 c_parser_consume_token (parser
);
3272 first
= c_parser_expr_no_commas (parser
, NULL
).value
;
3273 array_desig_after_first
:
3274 if (c_parser_next_token_is (parser
, CPP_ELLIPSIS
))
3276 ellipsis_loc
= c_parser_peek_token (parser
)->location
;
3277 c_parser_consume_token (parser
);
3278 second
= c_parser_expr_no_commas (parser
, NULL
).value
;
3282 if (c_parser_next_token_is (parser
, CPP_CLOSE_SQUARE
))
3284 c_parser_consume_token (parser
);
3285 set_init_index (first
, second
);
3286 if (pedantic
&& second
)
3287 pedwarn ("%HISO C forbids specifying range of "
3288 "elements to initialize", &ellipsis_loc
);
3291 c_parser_skip_until_found (parser
, CPP_CLOSE_SQUARE
,
3297 if (c_parser_next_token_is (parser
, CPP_EQ
))
3299 if (pedantic
&& !flag_isoc99
)
3300 pedwarn ("%HISO C90 forbids specifying subobject "
3301 "to initialize", &des_loc
);
3302 c_parser_consume_token (parser
);
3309 pedwarn ("%Hobsolete use of designated initializer "
3311 &c_parser_peek_token (parser
)->location
);
3316 init
.value
= error_mark_node
;
3317 init
.original_code
= ERROR_MARK
;
3318 c_parser_error (parser
, "expected %<=%>");
3319 c_parser_skip_until_found (parser
, CPP_COMMA
, NULL
);
3320 process_init_element (init
);
3326 c_parser_initval (parser
, NULL
);
3329 /* Parse a nested initializer; as c_parser_initializer but parses
3330 initializers within braced lists, after any designators have been
3331 applied. If AFTER is not NULL then it is an Objective-C message
3332 expression which is the primary-expression starting the
3336 c_parser_initval (c_parser
*parser
, struct c_expr
*after
)
3339 gcc_assert (!after
|| c_dialect_objc ());
3340 if (c_parser_next_token_is (parser
, CPP_OPEN_BRACE
) && !after
)
3341 init
= c_parser_braced_init (parser
, NULL_TREE
, true);
3344 init
= c_parser_expr_no_commas (parser
, after
);
3345 if (init
.value
!= NULL_TREE
3346 && TREE_CODE (init
.value
) != STRING_CST
3347 && TREE_CODE (init
.value
) != COMPOUND_LITERAL_EXPR
)
3348 init
= default_function_array_conversion (init
);
3350 process_init_element (init
);
3353 /* Parse a compound statement (possibly a function body) (C90 6.6.2,
3357 { block-item-list[opt] }
3358 { label-declarations block-item-list }
3362 block-item-list block-item
3374 { label-declarations block-item-list }
3377 __extension__ nested-declaration
3378 nested-function-definition
3382 label-declarations label-declaration
3385 __label__ identifier-list ;
3387 Allowing the mixing of declarations and code is new in C99. The
3388 GNU syntax also permits (not shown above) labels at the end of
3389 compound statements, which yield an error. We don't allow labels
3390 on declarations; this might seem like a natural extension, but
3391 there would be a conflict between attributes on the label and
3392 prefix attributes on the declaration. ??? The syntax follows the
3393 old parser in requiring something after label declarations.
3394 Although they are erroneous if the labels declared aren't defined,
3395 is it useful for the syntax to be this way?
3407 c_parser_compound_statement (c_parser
*parser
)
3410 if (!c_parser_require (parser
, CPP_OPEN_BRACE
, "expected %<{%>"))
3411 return error_mark_node
;
3412 stmt
= c_begin_compound_stmt (true);
3413 c_parser_compound_statement_nostart (parser
);
3414 return c_end_compound_stmt (stmt
, true);
3417 /* Parse a compound statement except for the opening brace. This is
3418 used for parsing both compound statements and statement expressions
3419 (which follow different paths to handling the opening). */
3422 c_parser_compound_statement_nostart (c_parser
*parser
)
3424 bool last_stmt
= false;
3425 bool last_label
= false;
3426 location_t label_loc
;
3427 if (c_parser_next_token_is (parser
, CPP_CLOSE_BRACE
))
3429 c_parser_consume_token (parser
);
3432 if (c_parser_next_token_is_keyword (parser
, RID_LABEL
))
3434 location_t err_loc
= c_parser_peek_token (parser
)->location
;
3435 /* Read zero or more forward-declarations for labels that nested
3436 functions can jump to. */
3437 while (c_parser_next_token_is_keyword (parser
, RID_LABEL
))
3439 c_parser_consume_token (parser
);
3440 /* Any identifiers, including those declared as type names,
3445 if (c_parser_next_token_is_not (parser
, CPP_NAME
))
3447 c_parser_error (parser
, "expected identifier");
3451 = declare_label (c_parser_peek_token (parser
)->value
);
3452 C_DECLARED_LABEL_FLAG (label
) = 1;
3453 add_stmt (build_stmt (DECL_EXPR
, label
));
3454 c_parser_consume_token (parser
);
3455 if (c_parser_next_token_is (parser
, CPP_COMMA
))
3456 c_parser_consume_token (parser
);
3460 c_parser_skip_until_found (parser
, CPP_SEMICOLON
, "expected %<;%>");
3463 pedwarn ("%HISO C forbids label declarations", &err_loc
);
3465 /* We must now have at least one statement, label or declaration. */
3466 if (c_parser_next_token_is (parser
, CPP_CLOSE_BRACE
))
3468 c_parser_error (parser
, "expected declaration or statement");
3469 c_parser_consume_token (parser
);
3472 while (c_parser_next_token_is_not (parser
, CPP_CLOSE_BRACE
))
3474 location_t loc
= c_parser_peek_token (parser
)->location
;
3475 if (c_parser_next_token_is_keyword (parser
, RID_CASE
)
3476 || c_parser_next_token_is_keyword (parser
, RID_DEFAULT
)
3477 || (c_parser_next_token_is (parser
, CPP_NAME
)
3478 && c_parser_peek_2nd_token (parser
)->type
== CPP_COLON
))
3480 if (c_parser_next_token_is_keyword (parser
, RID_CASE
))
3481 label_loc
= c_parser_peek_2nd_token (parser
)->location
;
3483 label_loc
= c_parser_peek_token (parser
)->location
;
3486 c_parser_label (parser
);
3488 else if (!last_label
3489 && c_parser_next_token_starts_declspecs (parser
))
3492 c_parser_declaration_or_fndef (parser
, true, true, true, true);
3494 && ((pedantic
&& !flag_isoc99
)
3495 || warn_declaration_after_statement
))
3496 pedwarn_c90 ("%HISO C90 forbids mixed declarations and code",
3500 else if (!last_label
3501 && c_parser_next_token_is_keyword (parser
, RID_EXTENSION
))
3503 /* __extension__ can start a declaration, but is also an
3504 unary operator that can start an expression. Consume all
3505 but the last of a possible series of __extension__ to
3507 while (c_parser_peek_2nd_token (parser
)->type
== CPP_KEYWORD
3508 && (c_parser_peek_2nd_token (parser
)->keyword
3510 c_parser_consume_token (parser
);
3511 if (c_token_starts_declspecs (c_parser_peek_2nd_token (parser
)))
3514 ext
= disable_extension_diagnostics ();
3515 c_parser_consume_token (parser
);
3517 c_parser_declaration_or_fndef (parser
, true, true, true, true);
3518 /* Following the old parser, __extension__ does not
3519 disable this diagnostic. */
3520 restore_extension_diagnostics (ext
);
3522 && ((pedantic
&& !flag_isoc99
)
3523 || warn_declaration_after_statement
))
3524 pedwarn_c90 ("%HISO C90 forbids mixed declarations and code",
3531 else if (c_parser_next_token_is (parser
, CPP_PRAGMA
))
3533 /* External pragmas, and some omp pragmas, are not associated
3534 with regular c code, and so are not to be considered statements
3535 syntactically. This ensures that the user doesn't put them
3536 places that would turn into syntax errors if the directive
3538 if (c_parser_pragma (parser
, pragma_compound
))
3539 last_label
= false, last_stmt
= true;
3541 else if (c_parser_next_token_is (parser
, CPP_EOF
))
3543 c_parser_error (parser
, "expected declaration or statement");
3546 else if (c_parser_next_token_is_keyword (parser
, RID_ELSE
))
3548 if (parser
->in_if_block
)
3550 error ("%H""expected %<}%> before %<else%>", &loc
);
3555 error ("%H%<else%> without a previous %<if%>", &loc
);
3556 c_parser_consume_token (parser
);
3565 c_parser_statement_after_labels (parser
);
3568 parser
->error
= false;
3571 error ("%Hlabel at end of compound statement", &label_loc
);
3572 c_parser_consume_token (parser
);
3575 /* Parse a label (C90 6.6.1, C99 6.8.1).
3578 identifier : attributes[opt]
3579 case constant-expression :
3585 case constant-expression ... constant-expression :
3587 The use of attributes on labels is a GNU extension. The syntax in
3588 GNU C accepts any expressions without commas, non-constant
3589 expressions being rejected later. */
3592 c_parser_label (c_parser
*parser
)
3594 location_t loc1
= c_parser_peek_token (parser
)->location
;
3595 tree label
= NULL_TREE
;
3596 if (c_parser_next_token_is_keyword (parser
, RID_CASE
))
3599 c_parser_consume_token (parser
);
3600 exp1
= c_parser_expr_no_commas (parser
, NULL
).value
;
3601 if (c_parser_next_token_is (parser
, CPP_COLON
))
3603 c_parser_consume_token (parser
);
3604 label
= do_case (exp1
, NULL_TREE
);
3606 else if (c_parser_next_token_is (parser
, CPP_ELLIPSIS
))
3608 c_parser_consume_token (parser
);
3609 exp2
= c_parser_expr_no_commas (parser
, NULL
).value
;
3610 if (c_parser_require (parser
, CPP_COLON
, "expected %<:%>"))
3611 label
= do_case (exp1
, exp2
);
3614 c_parser_error (parser
, "expected %<:%> or %<...%>");
3616 else if (c_parser_next_token_is_keyword (parser
, RID_DEFAULT
))
3618 c_parser_consume_token (parser
);
3619 if (c_parser_require (parser
, CPP_COLON
, "expected %<:%>"))
3620 label
= do_case (NULL_TREE
, NULL_TREE
);
3624 tree name
= c_parser_peek_token (parser
)->value
;
3627 location_t loc2
= c_parser_peek_token (parser
)->location
;
3628 gcc_assert (c_parser_next_token_is (parser
, CPP_NAME
));
3629 c_parser_consume_token (parser
);
3630 gcc_assert (c_parser_next_token_is (parser
, CPP_COLON
));
3631 c_parser_consume_token (parser
);
3632 attrs
= c_parser_attributes (parser
);
3633 tlab
= define_label (loc2
, name
);
3636 decl_attributes (&tlab
, attrs
, 0);
3637 label
= add_stmt (build_stmt (LABEL_EXPR
, tlab
));
3642 SET_EXPR_LOCATION (label
, loc1
);
3643 if (c_parser_next_token_starts_declspecs (parser
)
3644 && !(c_parser_next_token_is (parser
, CPP_NAME
)
3645 && c_parser_peek_2nd_token (parser
)->type
== CPP_COLON
))
3647 error ("%Ha label can only be part of a statement and "
3648 "a declaration is not a statement",
3649 &c_parser_peek_token (parser
)->location
);
3650 c_parser_declaration_or_fndef (parser
, /*fndef_ok*/ false,
3651 /*nested*/ true, /*empty_ok*/ false,
3652 /*start_attr_ok*/ true);
3657 /* Parse a statement (C90 6.6, C99 6.8).
3662 expression-statement
3670 expression-statement:
3673 selection-statement:
3677 iteration-statement:
3686 return expression[opt] ;
3699 objc-throw-statement
3700 objc-try-catch-statement
3701 objc-synchronized-statement
3703 objc-throw-statement:
3717 parallel-for-construct
3718 parallel-sections-construct
3725 parallel-directive structured-block
3728 for-directive iteration-statement
3731 sections-directive section-scope
3734 single-directive structured-block
3736 parallel-for-construct:
3737 parallel-for-directive iteration-statement
3739 parallel-sections-construct:
3740 parallel-sections-directive section-scope
3743 master-directive structured-block
3746 critical-directive structured-block
3749 atomic-directive expression-statement
3752 ordered-directive structured-block */
3755 c_parser_statement (c_parser
*parser
)
3757 while (c_parser_next_token_is_keyword (parser
, RID_CASE
)
3758 || c_parser_next_token_is_keyword (parser
, RID_DEFAULT
)
3759 || (c_parser_next_token_is (parser
, CPP_NAME
)
3760 && c_parser_peek_2nd_token (parser
)->type
== CPP_COLON
))
3761 c_parser_label (parser
);
3762 c_parser_statement_after_labels (parser
);
3765 /* Parse a statement, other than a labeled statement. */
3768 c_parser_statement_after_labels (c_parser
*parser
)
3770 location_t loc
= c_parser_peek_token (parser
)->location
;
3771 tree stmt
= NULL_TREE
;
3772 bool in_if_block
= parser
->in_if_block
;
3773 parser
->in_if_block
= false;
3774 switch (c_parser_peek_token (parser
)->type
)
3776 case CPP_OPEN_BRACE
:
3777 add_stmt (c_parser_compound_statement (parser
));
3780 switch (c_parser_peek_token (parser
)->keyword
)
3783 c_parser_if_statement (parser
);
3786 c_parser_switch_statement (parser
);
3789 c_parser_while_statement (parser
);
3792 c_parser_do_statement (parser
);
3795 c_parser_for_statement (parser
);
3798 c_parser_consume_token (parser
);
3799 if (c_parser_next_token_is (parser
, CPP_NAME
))
3801 stmt
= c_finish_goto_label (c_parser_peek_token (parser
)->value
);
3802 c_parser_consume_token (parser
);
3804 else if (c_parser_next_token_is (parser
, CPP_MULT
))
3806 c_parser_consume_token (parser
);
3807 stmt
= c_finish_goto_ptr (c_parser_expression (parser
).value
);
3810 c_parser_error (parser
, "expected identifier or %<*%>");
3811 goto expect_semicolon
;
3813 c_parser_consume_token (parser
);
3814 stmt
= c_finish_bc_stmt (&c_cont_label
, false);
3815 goto expect_semicolon
;
3817 c_parser_consume_token (parser
);
3818 stmt
= c_finish_bc_stmt (&c_break_label
, true);
3819 goto expect_semicolon
;
3821 c_parser_consume_token (parser
);
3822 if (c_parser_next_token_is (parser
, CPP_SEMICOLON
))
3824 stmt
= c_finish_return (NULL_TREE
);
3825 c_parser_consume_token (parser
);
3829 stmt
= c_finish_return (c_parser_expression_conv (parser
).value
);
3830 goto expect_semicolon
;
3834 stmt
= c_parser_asm_statement (parser
);
3837 gcc_assert (c_dialect_objc ());
3838 c_parser_consume_token (parser
);
3839 if (c_parser_next_token_is (parser
, CPP_SEMICOLON
))
3841 stmt
= objc_build_throw_stmt (NULL_TREE
);
3842 c_parser_consume_token (parser
);
3847 = objc_build_throw_stmt (c_parser_expression (parser
).value
);
3848 goto expect_semicolon
;
3852 gcc_assert (c_dialect_objc ());
3853 c_parser_objc_try_catch_statement (parser
);
3855 case RID_AT_SYNCHRONIZED
:
3856 gcc_assert (c_dialect_objc ());
3857 c_parser_objc_synchronized_statement (parser
);
3864 c_parser_consume_token (parser
);
3866 case CPP_CLOSE_PAREN
:
3867 case CPP_CLOSE_SQUARE
:
3868 /* Avoid infinite loop in error recovery:
3869 c_parser_skip_until_found stops at a closing nesting
3870 delimiter without consuming it, but here we need to consume
3871 it to proceed further. */
3872 c_parser_error (parser
, "expected statement");
3873 c_parser_consume_token (parser
);
3876 c_parser_pragma (parser
, pragma_stmt
);
3880 stmt
= c_finish_expr_stmt (c_parser_expression_conv (parser
).value
);
3882 c_parser_skip_until_found (parser
, CPP_SEMICOLON
, "expected %<;%>");
3885 /* Two cases cannot and do not have line numbers associated: If stmt
3886 is degenerate, such as "2;", then stmt is an INTEGER_CST, which
3887 cannot hold line numbers. But that's OK because the statement
3888 will either be changed to a MODIFY_EXPR during gimplification of
3889 the statement expr, or discarded. If stmt was compound, but
3890 without new variables, we will have skipped the creation of a
3891 BIND and will have a bare STATEMENT_LIST. But that's OK because
3892 (recursively) all of the component statements should already have
3893 line numbers assigned. ??? Can we discard no-op statements
3895 if (stmt
&& CAN_HAVE_LOCATION_P (stmt
))
3896 SET_EXPR_LOCATION (stmt
, loc
);
3898 parser
->in_if_block
= in_if_block
;
3901 /* Parse a parenthesized condition from an if, do or while statement.
3907 c_parser_paren_condition (c_parser
*parser
)
3911 if (!c_parser_require (parser
, CPP_OPEN_PAREN
, "expected %<(%>"))
3912 return error_mark_node
;
3913 loc
= c_parser_peek_token (parser
)->location
;
3914 cond
= c_objc_common_truthvalue_conversion
3915 (c_parser_expression_conv (parser
).value
);
3916 if (CAN_HAVE_LOCATION_P (cond
))
3917 SET_EXPR_LOCATION (cond
, loc
);
3918 c_parser_skip_until_found (parser
, CPP_CLOSE_PAREN
, "expected %<)%>");
3922 /* Parse a statement which is a block in C99. */
3925 c_parser_c99_block_statement (c_parser
*parser
)
3927 tree block
= c_begin_compound_stmt (flag_isoc99
);
3928 c_parser_statement (parser
);
3929 return c_end_compound_stmt (block
, flag_isoc99
);
3932 /* Parse the body of an if statement. This is just parsing a
3933 statement but (a) it is a block in C99, (b) we track whether the
3934 body is an if statement for the sake of -Wparentheses warnings, (c)
3935 we handle an empty body specially for the sake of -Wempty-body
3936 warnings, and (d) we call parser_compound_statement directly
3937 because c_parser_statement_after_labels resets
3938 parser->in_if_block. */
3941 c_parser_if_body (c_parser
*parser
, bool *if_p
)
3943 tree block
= c_begin_compound_stmt (flag_isoc99
);
3944 while (c_parser_next_token_is_keyword (parser
, RID_CASE
)
3945 || c_parser_next_token_is_keyword (parser
, RID_DEFAULT
)
3946 || (c_parser_next_token_is (parser
, CPP_NAME
)
3947 && c_parser_peek_2nd_token (parser
)->type
== CPP_COLON
))
3948 c_parser_label (parser
);
3949 *if_p
= c_parser_next_token_is_keyword (parser
, RID_IF
);
3950 if (c_parser_next_token_is (parser
, CPP_SEMICOLON
))
3952 add_stmt (build_empty_stmt ());
3953 c_parser_consume_token (parser
);
3955 else if (c_parser_next_token_is (parser
, CPP_OPEN_BRACE
))
3956 add_stmt (c_parser_compound_statement (parser
));
3958 c_parser_statement_after_labels (parser
);
3959 return c_end_compound_stmt (block
, flag_isoc99
);
3962 /* Parse the else body of an if statement. This is just parsing a
3963 statement but (a) it is a block in C99, (b) we handle an empty body
3964 specially for the sake of -Wempty-body warnings. */
3967 c_parser_else_body (c_parser
*parser
)
3969 tree block
= c_begin_compound_stmt (flag_isoc99
);
3970 while (c_parser_next_token_is_keyword (parser
, RID_CASE
)
3971 || c_parser_next_token_is_keyword (parser
, RID_DEFAULT
)
3972 || (c_parser_next_token_is (parser
, CPP_NAME
)
3973 && c_parser_peek_2nd_token (parser
)->type
== CPP_COLON
))
3974 c_parser_label (parser
);
3975 if (c_parser_next_token_is (parser
, CPP_SEMICOLON
))
3977 add_stmt (build_empty_stmt ());
3978 c_parser_consume_token (parser
);
3981 c_parser_statement_after_labels (parser
);
3982 return c_end_compound_stmt (block
, flag_isoc99
);
3985 /* Parse an if statement (C90 6.6.4, C99 6.8.4).
3988 if ( expression ) statement
3989 if ( expression ) statement else statement
3993 c_parser_if_statement (c_parser
*parser
)
3998 bool first_if
= false;
3999 tree first_body
, second_body
;
4002 gcc_assert (c_parser_next_token_is_keyword (parser
, RID_IF
));
4003 c_parser_consume_token (parser
);
4004 block
= c_begin_compound_stmt (flag_isoc99
);
4005 loc
= c_parser_peek_token (parser
)->location
;
4006 cond
= c_parser_paren_condition (parser
);
4007 in_if_block
= parser
->in_if_block
;
4008 parser
->in_if_block
= true;
4009 first_body
= c_parser_if_body (parser
, &first_if
);
4010 parser
->in_if_block
= in_if_block
;
4011 if (c_parser_next_token_is_keyword (parser
, RID_ELSE
))
4013 c_parser_consume_token (parser
);
4014 second_body
= c_parser_else_body (parser
);
4017 second_body
= NULL_TREE
;
4018 c_finish_if_stmt (loc
, cond
, first_body
, second_body
, first_if
);
4019 add_stmt (c_end_compound_stmt (block
, flag_isoc99
));
4022 /* Parse a switch statement (C90 6.6.4, C99 6.8.4).
4025 switch (expression) statement
4029 c_parser_switch_statement (c_parser
*parser
)
4031 tree block
, expr
, body
, save_break
;
4032 gcc_assert (c_parser_next_token_is_keyword (parser
, RID_SWITCH
));
4033 c_parser_consume_token (parser
);
4034 block
= c_begin_compound_stmt (flag_isoc99
);
4035 if (c_parser_require (parser
, CPP_OPEN_PAREN
, "expected %<(%>"))
4037 expr
= c_parser_expression (parser
).value
;
4038 c_parser_skip_until_found (parser
, CPP_CLOSE_PAREN
, "expected %<)%>");
4041 expr
= error_mark_node
;
4042 c_start_case (expr
);
4043 save_break
= c_break_label
;
4044 c_break_label
= NULL_TREE
;
4045 body
= c_parser_c99_block_statement (parser
);
4046 c_finish_case (body
);
4048 add_stmt (build1 (LABEL_EXPR
, void_type_node
, c_break_label
));
4049 c_break_label
= save_break
;
4050 add_stmt (c_end_compound_stmt (block
, flag_isoc99
));
4053 /* Parse a while statement (C90 6.6.5, C99 6.8.5).
4056 while (expression) statement
4060 c_parser_while_statement (c_parser
*parser
)
4062 tree block
, cond
, body
, save_break
, save_cont
;
4064 gcc_assert (c_parser_next_token_is_keyword (parser
, RID_WHILE
));
4065 c_parser_consume_token (parser
);
4066 block
= c_begin_compound_stmt (flag_isoc99
);
4067 loc
= c_parser_peek_token (parser
)->location
;
4068 cond
= c_parser_paren_condition (parser
);
4069 save_break
= c_break_label
;
4070 c_break_label
= NULL_TREE
;
4071 save_cont
= c_cont_label
;
4072 c_cont_label
= NULL_TREE
;
4073 body
= c_parser_c99_block_statement (parser
);
4074 c_finish_loop (loc
, cond
, NULL
, body
, c_break_label
, c_cont_label
, true);
4075 add_stmt (c_end_compound_stmt (block
, flag_isoc99
));
4076 c_break_label
= save_break
;
4077 c_cont_label
= save_cont
;
4080 /* Parse a do statement (C90 6.6.5, C99 6.8.5).
4083 do statement while ( expression ) ;
4087 c_parser_do_statement (c_parser
*parser
)
4089 tree block
, cond
, body
, save_break
, save_cont
, new_break
, new_cont
;
4091 gcc_assert (c_parser_next_token_is_keyword (parser
, RID_DO
));
4092 c_parser_consume_token (parser
);
4093 if (c_parser_next_token_is (parser
, CPP_SEMICOLON
))
4094 warning (OPT_Wempty_body
,
4095 "%Hsuggest braces around empty body in %<do%> statement",
4096 &c_parser_peek_token (parser
)->location
);
4097 block
= c_begin_compound_stmt (flag_isoc99
);
4098 loc
= c_parser_peek_token (parser
)->location
;
4099 save_break
= c_break_label
;
4100 c_break_label
= NULL_TREE
;
4101 save_cont
= c_cont_label
;
4102 c_cont_label
= NULL_TREE
;
4103 body
= c_parser_c99_block_statement (parser
);
4104 c_parser_require_keyword (parser
, RID_WHILE
, "expected %<while%>");
4105 new_break
= c_break_label
;
4106 c_break_label
= save_break
;
4107 new_cont
= c_cont_label
;
4108 c_cont_label
= save_cont
;
4109 cond
= c_parser_paren_condition (parser
);
4110 if (!c_parser_require (parser
, CPP_SEMICOLON
, "expected %<;%>"))
4111 c_parser_skip_to_end_of_block_or_statement (parser
);
4112 c_finish_loop (loc
, cond
, NULL
, body
, new_break
, new_cont
, false);
4113 add_stmt (c_end_compound_stmt (block
, flag_isoc99
));
4116 /* Parse a for statement (C90 6.6.5, C99 6.8.5).
4119 for ( expression[opt] ; expression[opt] ; expression[opt] ) statement
4120 for ( nested-declaration expression[opt] ; expression[opt] ) statement
4122 The form with a declaration is new in C99.
4124 ??? In accordance with the old parser, the declaration may be a
4125 nested function, which is then rejected in check_for_loop_decls,
4126 but does it make any sense for this to be included in the grammar?
4127 Note in particular that the nested function does not include a
4128 trailing ';', whereas the "declaration" production includes one.
4129 Also, can we reject bad declarations earlier and cheaper than
4130 check_for_loop_decls? */
4133 c_parser_for_statement (c_parser
*parser
)
4135 tree block
, cond
, incr
, save_break
, save_cont
, body
;
4137 gcc_assert (c_parser_next_token_is_keyword (parser
, RID_FOR
));
4138 loc
= c_parser_peek_token (parser
)->location
;
4139 c_parser_consume_token (parser
);
4140 block
= c_begin_compound_stmt (flag_isoc99
);
4141 if (c_parser_require (parser
, CPP_OPEN_PAREN
, "expected %<(%>"))
4143 /* Parse the initialization declaration or expression. */
4144 if (c_parser_next_token_is (parser
, CPP_SEMICOLON
))
4146 c_parser_consume_token (parser
);
4147 c_finish_expr_stmt (NULL_TREE
);
4149 else if (c_parser_next_token_starts_declspecs (parser
))
4151 c_parser_declaration_or_fndef (parser
, true, true, true, true);
4152 check_for_loop_decls ();
4154 else if (c_parser_next_token_is_keyword (parser
, RID_EXTENSION
))
4156 /* __extension__ can start a declaration, but is also an
4157 unary operator that can start an expression. Consume all
4158 but the last of a possible series of __extension__ to
4160 while (c_parser_peek_2nd_token (parser
)->type
== CPP_KEYWORD
4161 && (c_parser_peek_2nd_token (parser
)->keyword
4163 c_parser_consume_token (parser
);
4164 if (c_token_starts_declspecs (c_parser_peek_2nd_token (parser
)))
4167 ext
= disable_extension_diagnostics ();
4168 c_parser_consume_token (parser
);
4169 c_parser_declaration_or_fndef (parser
, true, true, true, true);
4170 restore_extension_diagnostics (ext
);
4171 check_for_loop_decls ();
4179 c_finish_expr_stmt (c_parser_expression (parser
).value
);
4180 c_parser_skip_until_found (parser
, CPP_SEMICOLON
, "expected %<;%>");
4182 /* Parse the loop condition. */
4183 loc
= c_parser_peek_token (parser
)->location
;
4184 if (c_parser_next_token_is (parser
, CPP_SEMICOLON
))
4186 c_parser_consume_token (parser
);
4191 tree ocond
= c_parser_expression_conv (parser
).value
;
4192 cond
= c_objc_common_truthvalue_conversion (ocond
);
4193 if (CAN_HAVE_LOCATION_P (cond
))
4194 SET_EXPR_LOCATION (cond
, loc
);
4195 c_parser_skip_until_found (parser
, CPP_SEMICOLON
, "expected %<;%>");
4197 /* Parse the increment expression. */
4198 if (c_parser_next_token_is (parser
, CPP_CLOSE_PAREN
))
4199 incr
= c_process_expr_stmt (NULL_TREE
);
4201 incr
= c_process_expr_stmt (c_parser_expression (parser
).value
);
4202 c_parser_skip_until_found (parser
, CPP_CLOSE_PAREN
, "expected %<)%>");
4206 cond
= error_mark_node
;
4207 incr
= error_mark_node
;
4209 save_break
= c_break_label
;
4210 c_break_label
= NULL_TREE
;
4211 save_cont
= c_cont_label
;
4212 c_cont_label
= NULL_TREE
;
4213 body
= c_parser_c99_block_statement (parser
);
4214 c_finish_loop (loc
, cond
, incr
, body
, c_break_label
, c_cont_label
, true);
4215 add_stmt (c_end_compound_stmt (block
, flag_isoc99
));
4216 c_break_label
= save_break
;
4217 c_cont_label
= save_cont
;
4220 /* Parse an asm statement, a GNU extension. This is a full-blown asm
4221 statement with inputs, outputs, clobbers, and volatile tag
4225 asm type-qualifier[opt] ( asm-argument ) ;
4229 asm-string-literal : asm-operands[opt]
4230 asm-string-literal : asm-operands[opt] : asm-operands[opt]
4231 asm-string-literal : asm-operands[opt] : asm-operands[opt] : asm-clobbers
4233 Qualifiers other than volatile are accepted in the syntax but
4237 c_parser_asm_statement (c_parser
*parser
)
4239 tree quals
, str
, outputs
, inputs
, clobbers
, ret
;
4241 gcc_assert (c_parser_next_token_is_keyword (parser
, RID_ASM
));
4242 c_parser_consume_token (parser
);
4243 if (c_parser_next_token_is_keyword (parser
, RID_VOLATILE
))
4245 quals
= c_parser_peek_token (parser
)->value
;
4246 c_parser_consume_token (parser
);
4248 else if (c_parser_next_token_is_keyword (parser
, RID_CONST
)
4249 || c_parser_next_token_is_keyword (parser
, RID_RESTRICT
))
4251 warning (0, "%H%E qualifier ignored on asm",
4252 &c_parser_peek_token (parser
)->location
,
4253 c_parser_peek_token (parser
)->value
);
4255 c_parser_consume_token (parser
);
4259 /* ??? Follow the C++ parser rather than using the
4260 lex_untranslated_string kludge. */
4261 parser
->lex_untranslated_string
= true;
4262 if (!c_parser_require (parser
, CPP_OPEN_PAREN
, "expected %<(%>"))
4264 parser
->lex_untranslated_string
= false;
4267 str
= c_parser_asm_string_literal (parser
);
4268 if (c_parser_next_token_is (parser
, CPP_CLOSE_PAREN
))
4271 outputs
= NULL_TREE
;
4273 clobbers
= NULL_TREE
;
4276 if (!c_parser_require (parser
, CPP_COLON
, "expected %<:%> or %<)%>"))
4278 parser
->lex_untranslated_string
= false;
4279 c_parser_skip_until_found (parser
, CPP_CLOSE_PAREN
, NULL
);
4283 /* Parse outputs. */
4284 if (c_parser_next_token_is (parser
, CPP_COLON
)
4285 || c_parser_next_token_is (parser
, CPP_CLOSE_PAREN
))
4286 outputs
= NULL_TREE
;
4288 outputs
= c_parser_asm_operands (parser
, false);
4289 if (c_parser_next_token_is (parser
, CPP_CLOSE_PAREN
))
4292 clobbers
= NULL_TREE
;
4295 if (!c_parser_require (parser
, CPP_COLON
, "expected %<:%> or %<)%>"))
4297 parser
->lex_untranslated_string
= false;
4298 c_parser_skip_until_found (parser
, CPP_CLOSE_PAREN
, NULL
);
4302 if (c_parser_next_token_is (parser
, CPP_COLON
)
4303 || c_parser_next_token_is (parser
, CPP_CLOSE_PAREN
))
4306 inputs
= c_parser_asm_operands (parser
, true);
4307 if (c_parser_next_token_is (parser
, CPP_CLOSE_PAREN
))
4309 clobbers
= NULL_TREE
;
4312 if (!c_parser_require (parser
, CPP_COLON
, "expected %<:%> or %<)%>"))
4314 parser
->lex_untranslated_string
= false;
4315 c_parser_skip_until_found (parser
, CPP_CLOSE_PAREN
, NULL
);
4318 /* Parse clobbers. */
4319 clobbers
= c_parser_asm_clobbers (parser
);
4321 parser
->lex_untranslated_string
= false;
4322 if (!c_parser_require (parser
, CPP_CLOSE_PAREN
, "expected %<)%>"))
4324 c_parser_skip_until_found (parser
, CPP_CLOSE_PAREN
, NULL
);
4327 if (!c_parser_require (parser
, CPP_SEMICOLON
, "expected %<;%>"))
4328 c_parser_skip_to_end_of_block_or_statement (parser
);
4329 ret
= build_asm_stmt (quals
, build_asm_expr (str
, outputs
, inputs
,
4334 /* Parse asm operands, a GNU extension. If CONVERT_P (for inputs but
4335 not outputs), apply the default conversion of functions and arrays
4340 asm-operands , asm-operand
4343 asm-string-literal ( expression )
4344 [ identifier ] asm-string-literal ( expression )
4348 c_parser_asm_operands (c_parser
*parser
, bool convert_p
)
4350 tree list
= NULL_TREE
;
4355 if (c_parser_next_token_is (parser
, CPP_OPEN_SQUARE
))
4357 c_parser_consume_token (parser
);
4358 if (c_parser_next_token_is (parser
, CPP_NAME
))
4360 tree id
= c_parser_peek_token (parser
)->value
;
4361 c_parser_consume_token (parser
);
4362 name
= build_string (IDENTIFIER_LENGTH (id
),
4363 IDENTIFIER_POINTER (id
));
4367 c_parser_error (parser
, "expected identifier");
4368 c_parser_skip_until_found (parser
, CPP_CLOSE_SQUARE
, NULL
);
4371 c_parser_skip_until_found (parser
, CPP_CLOSE_SQUARE
,
4376 str
= c_parser_asm_string_literal (parser
);
4377 if (str
== NULL_TREE
)
4379 parser
->lex_untranslated_string
= false;
4380 if (!c_parser_require (parser
, CPP_OPEN_PAREN
, "expected %<(%>"))
4382 parser
->lex_untranslated_string
= true;
4385 expr
= c_parser_expression (parser
);
4387 expr
= default_function_array_conversion (expr
);
4388 parser
->lex_untranslated_string
= true;
4389 if (!c_parser_require (parser
, CPP_CLOSE_PAREN
, "expected %<)%>"))
4391 c_parser_skip_until_found (parser
, CPP_CLOSE_PAREN
, NULL
);
4394 list
= chainon (list
, build_tree_list (build_tree_list (name
, str
),
4396 if (c_parser_next_token_is (parser
, CPP_COMMA
))
4397 c_parser_consume_token (parser
);
4404 /* Parse asm clobbers, a GNU extension.
4408 asm-clobbers , asm-string-literal
4412 c_parser_asm_clobbers (c_parser
*parser
)
4414 tree list
= NULL_TREE
;
4417 tree str
= c_parser_asm_string_literal (parser
);
4419 list
= tree_cons (NULL_TREE
, str
, list
);
4422 if (c_parser_next_token_is (parser
, CPP_COMMA
))
4423 c_parser_consume_token (parser
);
4430 /* Parse an expression other than a compound expression; that is, an
4431 assignment expression (C90 6.3.16, C99 6.5.16). If AFTER is not
4432 NULL then it is an Objective-C message expression which is the
4433 primary-expression starting the expression as an initializer.
4435 assignment-expression:
4436 conditional-expression
4437 unary-expression assignment-operator assignment-expression
4439 assignment-operator: one of
4440 = *= /= %= += -= <<= >>= &= ^= |=
4442 In GNU C we accept any conditional expression on the LHS and
4443 diagnose the invalid lvalue rather than producing a syntax
4446 static struct c_expr
4447 c_parser_expr_no_commas (c_parser
*parser
, struct c_expr
*after
)
4449 struct c_expr lhs
, rhs
, ret
;
4450 enum tree_code code
;
4451 gcc_assert (!after
|| c_dialect_objc ());
4452 lhs
= c_parser_conditional_expression (parser
, after
);
4453 switch (c_parser_peek_token (parser
)->type
)
4462 code
= TRUNC_DIV_EXPR
;
4465 code
= TRUNC_MOD_EXPR
;
4480 code
= BIT_AND_EXPR
;
4483 code
= BIT_XOR_EXPR
;
4486 code
= BIT_IOR_EXPR
;
4491 c_parser_consume_token (parser
);
4492 rhs
= c_parser_expr_no_commas (parser
, NULL
);
4493 rhs
= default_function_array_conversion (rhs
);
4494 ret
.value
= build_modify_expr (lhs
.value
, code
, rhs
.value
);
4495 if (code
== NOP_EXPR
)
4496 ret
.original_code
= MODIFY_EXPR
;
4499 TREE_NO_WARNING (ret
.value
) = 1;
4500 ret
.original_code
= ERROR_MARK
;
4505 /* Parse a conditional expression (C90 6.3.15, C99 6.5.15). If AFTER
4506 is not NULL then it is an Objective-C message expression which is
4507 the primary-expression starting the expression as an initializer.
4509 conditional-expression:
4510 logical-OR-expression
4511 logical-OR-expression ? expression : conditional-expression
4515 conditional-expression:
4516 logical-OR-expression ? : conditional-expression
4519 static struct c_expr
4520 c_parser_conditional_expression (c_parser
*parser
, struct c_expr
*after
)
4522 struct c_expr cond
, exp1
, exp2
, ret
;
4523 gcc_assert (!after
|| c_dialect_objc ());
4524 cond
= c_parser_binary_expression (parser
, after
);
4525 if (c_parser_next_token_is_not (parser
, CPP_QUERY
))
4527 cond
= default_function_array_conversion (cond
);
4528 c_parser_consume_token (parser
);
4529 if (c_parser_next_token_is (parser
, CPP_COLON
))
4532 pedwarn ("%HISO C forbids omitting the middle term of a ?: expression",
4533 &c_parser_peek_token (parser
)->location
);
4534 /* Make sure first operand is calculated only once. */
4535 exp1
.value
= save_expr (default_conversion (cond
.value
));
4536 cond
.value
= c_objc_common_truthvalue_conversion (exp1
.value
);
4537 skip_evaluation
+= cond
.value
== truthvalue_true_node
;
4542 = c_objc_common_truthvalue_conversion
4543 (default_conversion (cond
.value
));
4544 skip_evaluation
+= cond
.value
== truthvalue_false_node
;
4545 exp1
= c_parser_expression_conv (parser
);
4546 skip_evaluation
+= ((cond
.value
== truthvalue_true_node
)
4547 - (cond
.value
== truthvalue_false_node
));
4549 if (!c_parser_require (parser
, CPP_COLON
, "expected %<:%>"))
4551 skip_evaluation
-= cond
.value
== truthvalue_true_node
;
4552 ret
.value
= error_mark_node
;
4553 ret
.original_code
= ERROR_MARK
;
4556 exp2
= c_parser_conditional_expression (parser
, NULL
);
4557 exp2
= default_function_array_conversion (exp2
);
4558 skip_evaluation
-= cond
.value
== truthvalue_true_node
;
4559 ret
.value
= build_conditional_expr (cond
.value
, exp1
.value
, exp2
.value
);
4560 ret
.original_code
= ERROR_MARK
;
4564 /* Parse a binary expression; that is, a logical-OR-expression (C90
4565 6.3.5-6.3.14, C99 6.5.5-6.5.14). If AFTER is not NULL then it is
4566 an Objective-C message expression which is the primary-expression
4567 starting the expression as an initializer.
4569 multiplicative-expression:
4571 multiplicative-expression * cast-expression
4572 multiplicative-expression / cast-expression
4573 multiplicative-expression % cast-expression
4575 additive-expression:
4576 multiplicative-expression
4577 additive-expression + multiplicative-expression
4578 additive-expression - multiplicative-expression
4582 shift-expression << additive-expression
4583 shift-expression >> additive-expression
4585 relational-expression:
4587 relational-expression < shift-expression
4588 relational-expression > shift-expression
4589 relational-expression <= shift-expression
4590 relational-expression >= shift-expression
4592 equality-expression:
4593 relational-expression
4594 equality-expression == relational-expression
4595 equality-expression != relational-expression
4599 AND-expression & equality-expression
4601 exclusive-OR-expression:
4603 exclusive-OR-expression ^ AND-expression
4605 inclusive-OR-expression:
4606 exclusive-OR-expression
4607 inclusive-OR-expression | exclusive-OR-expression
4609 logical-AND-expression:
4610 inclusive-OR-expression
4611 logical-AND-expression && inclusive-OR-expression
4613 logical-OR-expression:
4614 logical-AND-expression
4615 logical-OR-expression || logical-AND-expression
4618 static struct c_expr
4619 c_parser_binary_expression (c_parser
*parser
, struct c_expr
*after
)
4621 /* A binary expression is parsed using operator-precedence parsing,
4622 with the operands being cast expressions. All the binary
4623 operators are left-associative. Thus a binary expression is of
4626 E0 op1 E1 op2 E2 ...
4628 which we represent on a stack. On the stack, the precedence
4629 levels are strictly increasing. When a new operator is
4630 encountered of higher precedence than that at the top of the
4631 stack, it is pushed; its LHS is the top expression, and its RHS
4632 is everything parsed until it is popped. When a new operator is
4633 encountered with precedence less than or equal to that at the top
4634 of the stack, triples E[i-1] op[i] E[i] are popped and replaced
4635 by the result of the operation until the operator at the top of
4636 the stack has lower precedence than the new operator or there is
4637 only one element on the stack; then the top expression is the LHS
4638 of the new operator. In the case of logical AND and OR
4639 expressions, we also need to adjust skip_evaluation as
4640 appropriate when the operators are pushed and popped. */
4642 /* The precedence levels, where 0 is a dummy lowest level used for
4643 the bottom of the stack. */
4659 /* The expression at this stack level. */
4661 /* The precedence of the operator on its left, PREC_NONE at the
4662 bottom of the stack. */
4664 /* The operation on its left. */
4670 switch (stack[sp].op) \
4672 case TRUTH_ANDIF_EXPR: \
4673 skip_evaluation -= stack[sp - 1].expr.value == truthvalue_false_node; \
4675 case TRUTH_ORIF_EXPR: \
4676 skip_evaluation -= stack[sp - 1].expr.value == truthvalue_true_node; \
4681 stack[sp - 1].expr \
4682 = default_function_array_conversion (stack[sp - 1].expr); \
4684 = default_function_array_conversion (stack[sp].expr); \
4685 stack[sp - 1].expr = parser_build_binary_op (stack[sp].op, \
4686 stack[sp - 1].expr, \
4690 gcc_assert (!after
|| c_dialect_objc ());
4691 stack
[0].expr
= c_parser_cast_expression (parser
, after
);
4692 stack
[0].prec
= PREC_NONE
;
4697 enum tree_code ocode
;
4700 switch (c_parser_peek_token (parser
)->type
)
4708 ocode
= TRUNC_DIV_EXPR
;
4712 ocode
= TRUNC_MOD_EXPR
;
4724 ocode
= LSHIFT_EXPR
;
4728 ocode
= RSHIFT_EXPR
;
4742 case CPP_GREATER_EQ
:
4755 oprec
= PREC_BITAND
;
4756 ocode
= BIT_AND_EXPR
;
4759 oprec
= PREC_BITXOR
;
4760 ocode
= BIT_XOR_EXPR
;
4764 ocode
= BIT_IOR_EXPR
;
4767 oprec
= PREC_LOGAND
;
4768 ocode
= TRUTH_ANDIF_EXPR
;
4772 ocode
= TRUTH_ORIF_EXPR
;
4775 /* Not a binary operator, so end of the binary
4779 c_parser_consume_token (parser
);
4780 while (oprec
<= stack
[sp
].prec
)
4784 case TRUTH_ANDIF_EXPR
:
4786 = default_function_array_conversion (stack
[sp
].expr
);
4787 stack
[sp
].expr
.value
= c_objc_common_truthvalue_conversion
4788 (default_conversion (stack
[sp
].expr
.value
));
4789 skip_evaluation
+= stack
[sp
].expr
.value
== truthvalue_false_node
;
4791 case TRUTH_ORIF_EXPR
:
4793 = default_function_array_conversion (stack
[sp
].expr
);
4794 stack
[sp
].expr
.value
= c_objc_common_truthvalue_conversion
4795 (default_conversion (stack
[sp
].expr
.value
));
4796 skip_evaluation
+= stack
[sp
].expr
.value
== truthvalue_true_node
;
4802 stack
[sp
].expr
= c_parser_cast_expression (parser
, NULL
);
4803 stack
[sp
].prec
= oprec
;
4804 stack
[sp
].op
= ocode
;
4809 return stack
[0].expr
;
4813 /* Parse a cast expression (C90 6.3.4, C99 6.5.4). If AFTER is not
4814 NULL then it is an Objective-C message expression which is the
4815 primary-expression starting the expression as an initializer.
4819 ( type-name ) unary-expression
4822 static struct c_expr
4823 c_parser_cast_expression (c_parser
*parser
, struct c_expr
*after
)
4825 gcc_assert (!after
|| c_dialect_objc ());
4827 return c_parser_postfix_expression_after_primary (parser
, *after
);
4828 /* If the expression begins with a parenthesized type name, it may
4829 be either a cast or a compound literal; we need to see whether
4830 the next character is '{' to tell the difference. If not, it is
4831 an unary expression. */
4832 if (c_parser_next_token_is (parser
, CPP_OPEN_PAREN
)
4833 && c_token_starts_typename (c_parser_peek_2nd_token (parser
)))
4835 struct c_type_name
*type_name
;
4838 c_parser_consume_token (parser
);
4839 type_name
= c_parser_type_name (parser
);
4840 c_parser_skip_until_found (parser
, CPP_CLOSE_PAREN
, "expected %<)%>");
4841 if (type_name
== NULL
)
4843 ret
.value
= error_mark_node
;
4844 ret
.original_code
= ERROR_MARK
;
4848 /* Save casted types in the function's used types hash table. */
4849 used_types_insert (type_name
->specs
->type
);
4851 if (c_parser_next_token_is (parser
, CPP_OPEN_BRACE
))
4852 return c_parser_postfix_expression_after_paren_type (parser
,
4854 expr
= c_parser_cast_expression (parser
, NULL
);
4855 expr
= default_function_array_conversion (expr
);
4856 ret
.value
= c_cast_expr (type_name
, expr
.value
);
4857 ret
.original_code
= ERROR_MARK
;
4861 return c_parser_unary_expression (parser
);
4864 /* Parse an unary expression (C90 6.3.3, C99 6.5.3).
4870 unary-operator cast-expression
4871 sizeof unary-expression
4872 sizeof ( type-name )
4874 unary-operator: one of
4880 __alignof__ unary-expression
4881 __alignof__ ( type-name )
4884 unary-operator: one of
4885 __extension__ __real__ __imag__
4887 In addition, the GNU syntax treats ++ and -- as unary operators, so
4888 they may be applied to cast expressions with errors for non-lvalues
4891 static struct c_expr
4892 c_parser_unary_expression (c_parser
*parser
)
4895 struct c_expr ret
, op
;
4896 switch (c_parser_peek_token (parser
)->type
)
4899 c_parser_consume_token (parser
);
4900 op
= c_parser_cast_expression (parser
, NULL
);
4901 op
= default_function_array_conversion (op
);
4902 return parser_build_unary_op (PREINCREMENT_EXPR
, op
);
4903 case CPP_MINUS_MINUS
:
4904 c_parser_consume_token (parser
);
4905 op
= c_parser_cast_expression (parser
, NULL
);
4906 op
= default_function_array_conversion (op
);
4907 return parser_build_unary_op (PREDECREMENT_EXPR
, op
);
4909 c_parser_consume_token (parser
);
4910 return parser_build_unary_op (ADDR_EXPR
,
4911 c_parser_cast_expression (parser
, NULL
));
4913 c_parser_consume_token (parser
);
4914 op
= c_parser_cast_expression (parser
, NULL
);
4915 op
= default_function_array_conversion (op
);
4916 ret
.value
= build_indirect_ref (op
.value
, "unary *");
4917 ret
.original_code
= ERROR_MARK
;
4920 if (!c_dialect_objc () && !in_system_header
)
4921 warning (OPT_Wtraditional
,
4922 "%Htraditional C rejects the unary plus operator",
4923 &c_parser_peek_token (parser
)->location
);
4924 c_parser_consume_token (parser
);
4925 op
= c_parser_cast_expression (parser
, NULL
);
4926 op
= default_function_array_conversion (op
);
4927 return parser_build_unary_op (CONVERT_EXPR
, op
);
4929 c_parser_consume_token (parser
);
4930 op
= c_parser_cast_expression (parser
, NULL
);
4931 op
= default_function_array_conversion (op
);
4932 return parser_build_unary_op (NEGATE_EXPR
, op
);
4934 c_parser_consume_token (parser
);
4935 op
= c_parser_cast_expression (parser
, NULL
);
4936 op
= default_function_array_conversion (op
);
4937 return parser_build_unary_op (BIT_NOT_EXPR
, op
);
4939 c_parser_consume_token (parser
);
4940 op
= c_parser_cast_expression (parser
, NULL
);
4941 op
= default_function_array_conversion (op
);
4942 return parser_build_unary_op (TRUTH_NOT_EXPR
, op
);
4944 /* Refer to the address of a label as a pointer. */
4945 c_parser_consume_token (parser
);
4946 if (c_parser_next_token_is (parser
, CPP_NAME
))
4948 ret
.value
= finish_label_address_expr
4949 (c_parser_peek_token (parser
)->value
);
4950 c_parser_consume_token (parser
);
4954 c_parser_error (parser
, "expected identifier");
4955 ret
.value
= error_mark_node
;
4957 ret
.original_code
= ERROR_MARK
;
4960 switch (c_parser_peek_token (parser
)->keyword
)
4963 return c_parser_sizeof_expression (parser
);
4965 return c_parser_alignof_expression (parser
);
4967 c_parser_consume_token (parser
);
4968 ext
= disable_extension_diagnostics ();
4969 ret
= c_parser_cast_expression (parser
, NULL
);
4970 restore_extension_diagnostics (ext
);
4973 c_parser_consume_token (parser
);
4974 op
= c_parser_cast_expression (parser
, NULL
);
4975 op
= default_function_array_conversion (op
);
4976 return parser_build_unary_op (REALPART_EXPR
, op
);
4978 c_parser_consume_token (parser
);
4979 op
= c_parser_cast_expression (parser
, NULL
);
4980 op
= default_function_array_conversion (op
);
4981 return parser_build_unary_op (IMAGPART_EXPR
, op
);
4983 return c_parser_postfix_expression (parser
);
4986 return c_parser_postfix_expression (parser
);
4990 /* Parse a sizeof expression. */
4992 static struct c_expr
4993 c_parser_sizeof_expression (c_parser
*parser
)
4996 location_t expr_loc
;
4997 gcc_assert (c_parser_next_token_is_keyword (parser
, RID_SIZEOF
));
4998 c_parser_consume_token (parser
);
5001 if (c_parser_next_token_is (parser
, CPP_OPEN_PAREN
)
5002 && c_token_starts_typename (c_parser_peek_2nd_token (parser
)))
5004 /* Either sizeof ( type-name ) or sizeof unary-expression
5005 starting with a compound literal. */
5006 struct c_type_name
*type_name
;
5007 c_parser_consume_token (parser
);
5008 expr_loc
= c_parser_peek_token (parser
)->location
;
5009 type_name
= c_parser_type_name (parser
);
5010 c_parser_skip_until_found (parser
, CPP_CLOSE_PAREN
, "expected %<)%>");
5011 if (type_name
== NULL
)
5016 ret
.value
= error_mark_node
;
5017 ret
.original_code
= ERROR_MARK
;
5020 if (c_parser_next_token_is (parser
, CPP_OPEN_BRACE
))
5022 expr
= c_parser_postfix_expression_after_paren_type (parser
,
5026 /* sizeof ( type-name ). */
5029 if (type_name
->declarator
->kind
== cdk_array
5030 && type_name
->declarator
->u
.array
.vla_unspec_p
)
5033 error ("%H%<[*]%> not allowed in other than a declaration",
5036 return c_expr_sizeof_type (type_name
);
5040 expr_loc
= c_parser_peek_token (parser
)->location
;
5041 expr
= c_parser_unary_expression (parser
);
5045 if (TREE_CODE (expr
.value
) == COMPONENT_REF
5046 && DECL_C_BIT_FIELD (TREE_OPERAND (expr
.value
, 1)))
5047 error ("%H%<sizeof%> applied to a bit-field", &expr_loc
);
5048 return c_expr_sizeof_expr (expr
);
5052 /* Parse an alignof expression. */
5054 static struct c_expr
5055 c_parser_alignof_expression (c_parser
*parser
)
5058 gcc_assert (c_parser_next_token_is_keyword (parser
, RID_ALIGNOF
));
5059 c_parser_consume_token (parser
);
5062 if (c_parser_next_token_is (parser
, CPP_OPEN_PAREN
)
5063 && c_token_starts_typename (c_parser_peek_2nd_token (parser
)))
5065 /* Either __alignof__ ( type-name ) or __alignof__
5066 unary-expression starting with a compound literal. */
5067 struct c_type_name
*type_name
;
5069 c_parser_consume_token (parser
);
5070 type_name
= c_parser_type_name (parser
);
5071 c_parser_skip_until_found (parser
, CPP_CLOSE_PAREN
, "expected %<)%>");
5072 if (type_name
== NULL
)
5077 ret
.value
= error_mark_node
;
5078 ret
.original_code
= ERROR_MARK
;
5081 if (c_parser_next_token_is (parser
, CPP_OPEN_BRACE
))
5083 expr
= c_parser_postfix_expression_after_paren_type (parser
,
5087 /* alignof ( type-name ). */
5090 ret
.value
= c_alignof (groktypename (type_name
));
5091 ret
.original_code
= ERROR_MARK
;
5097 expr
= c_parser_unary_expression (parser
);
5101 ret
.value
= c_alignof_expr (expr
.value
);
5102 ret
.original_code
= ERROR_MARK
;
5107 /* Parse a postfix expression (C90 6.3.1-6.3.2, C99 6.5.1-6.5.2).
5111 postfix-expression [ expression ]
5112 postfix-expression ( argument-expression-list[opt] )
5113 postfix-expression . identifier
5114 postfix-expression -> identifier
5115 postfix-expression ++
5116 postfix-expression --
5117 ( type-name ) { initializer-list }
5118 ( type-name ) { initializer-list , }
5120 argument-expression-list:
5122 argument-expression-list , argument-expression
5134 (treated as a keyword in GNU C)
5137 ( compound-statement )
5138 __builtin_va_arg ( assignment-expression , type-name )
5139 __builtin_offsetof ( type-name , offsetof-member-designator )
5140 __builtin_choose_expr ( assignment-expression ,
5141 assignment-expression ,
5142 assignment-expression )
5143 __builtin_types_compatible_p ( type-name , type-name )
5145 offsetof-member-designator:
5147 offsetof-member-designator . identifier
5148 offsetof-member-designator [ expression ]
5153 [ objc-receiver objc-message-args ]
5154 @selector ( objc-selector-arg )
5155 @protocol ( identifier )
5156 @encode ( type-name )
5160 static struct c_expr
5161 c_parser_postfix_expression (c_parser
*parser
)
5163 struct c_expr expr
, e1
, e2
, e3
;
5164 struct c_type_name
*t1
, *t2
;
5166 switch (c_parser_peek_token (parser
)->type
)
5171 expr
.value
= c_parser_peek_token (parser
)->value
;
5172 expr
.original_code
= ERROR_MARK
;
5173 c_parser_consume_token (parser
);
5177 expr
.value
= c_parser_peek_token (parser
)->value
;
5178 expr
.original_code
= STRING_CST
;
5179 c_parser_consume_token (parser
);
5181 case CPP_OBJC_STRING
:
5182 gcc_assert (c_dialect_objc ());
5184 = objc_build_string_object (c_parser_peek_token (parser
)->value
);
5185 expr
.original_code
= ERROR_MARK
;
5186 c_parser_consume_token (parser
);
5189 if (c_parser_peek_token (parser
)->id_kind
!= C_ID_ID
)
5191 c_parser_error (parser
, "expected expression");
5192 expr
.value
= error_mark_node
;
5193 expr
.original_code
= ERROR_MARK
;
5197 tree id
= c_parser_peek_token (parser
)->value
;
5198 location_t loc
= c_parser_peek_token (parser
)->location
;
5199 c_parser_consume_token (parser
);
5200 expr
.value
= build_external_ref (id
,
5201 (c_parser_peek_token (parser
)->type
5202 == CPP_OPEN_PAREN
), loc
);
5203 expr
.original_code
= ERROR_MARK
;
5206 case CPP_OPEN_PAREN
:
5207 /* A parenthesized expression, statement expression or compound
5209 if (c_parser_peek_2nd_token (parser
)->type
== CPP_OPEN_BRACE
)
5211 /* A statement expression. */
5213 location_t here
= c_parser_peek_token (parser
)->location
;
5214 c_parser_consume_token (parser
);
5215 c_parser_consume_token (parser
);
5216 if (cur_stmt_list
== NULL
)
5218 error ("%Hbraced-group within expression allowed "
5219 "only inside a function", &here
);
5220 parser
->error
= true;
5221 c_parser_skip_until_found (parser
, CPP_CLOSE_BRACE
, NULL
);
5222 c_parser_skip_until_found (parser
, CPP_CLOSE_PAREN
, NULL
);
5223 expr
.value
= error_mark_node
;
5224 expr
.original_code
= ERROR_MARK
;
5227 stmt
= c_begin_stmt_expr ();
5228 c_parser_compound_statement_nostart (parser
);
5229 c_parser_skip_until_found (parser
, CPP_CLOSE_PAREN
,
5232 pedwarn ("%HISO C forbids braced-groups within expressions",
5234 expr
.value
= c_finish_stmt_expr (stmt
);
5235 expr
.original_code
= ERROR_MARK
;
5237 else if (c_token_starts_typename (c_parser_peek_2nd_token (parser
)))
5239 /* A compound literal. ??? Can we actually get here rather
5240 than going directly to
5241 c_parser_postfix_expression_after_paren_type from
5243 struct c_type_name
*type_name
;
5244 c_parser_consume_token (parser
);
5245 type_name
= c_parser_type_name (parser
);
5246 c_parser_skip_until_found (parser
, CPP_CLOSE_PAREN
,
5248 if (type_name
== NULL
)
5250 expr
.value
= error_mark_node
;
5251 expr
.original_code
= ERROR_MARK
;
5254 expr
= c_parser_postfix_expression_after_paren_type (parser
,
5259 /* A parenthesized expression. */
5260 c_parser_consume_token (parser
);
5261 expr
= c_parser_expression (parser
);
5262 if (TREE_CODE (expr
.value
) == MODIFY_EXPR
)
5263 TREE_NO_WARNING (expr
.value
) = 1;
5264 expr
.original_code
= ERROR_MARK
;
5265 c_parser_skip_until_found (parser
, CPP_CLOSE_PAREN
,
5270 switch (c_parser_peek_token (parser
)->keyword
)
5272 case RID_FUNCTION_NAME
:
5273 case RID_PRETTY_FUNCTION_NAME
:
5274 case RID_C99_FUNCTION_NAME
:
5275 expr
.value
= fname_decl (c_parser_peek_token (parser
)->keyword
,
5276 c_parser_peek_token (parser
)->value
);
5277 expr
.original_code
= ERROR_MARK
;
5278 c_parser_consume_token (parser
);
5281 c_parser_consume_token (parser
);
5282 if (!c_parser_require (parser
, CPP_OPEN_PAREN
, "expected %<(%>"))
5284 expr
.value
= error_mark_node
;
5285 expr
.original_code
= ERROR_MARK
;
5288 e1
= c_parser_expr_no_commas (parser
, NULL
);
5289 if (!c_parser_require (parser
, CPP_COMMA
, "expected %<,%>"))
5291 c_parser_skip_until_found (parser
, CPP_CLOSE_PAREN
, NULL
);
5292 expr
.value
= error_mark_node
;
5293 expr
.original_code
= ERROR_MARK
;
5296 t1
= c_parser_type_name (parser
);
5297 c_parser_skip_until_found (parser
, CPP_CLOSE_PAREN
,
5301 expr
.value
= error_mark_node
;
5302 expr
.original_code
= ERROR_MARK
;
5306 expr
.value
= build_va_arg (e1
.value
, groktypename (t1
));
5307 expr
.original_code
= ERROR_MARK
;
5311 c_parser_consume_token (parser
);
5312 if (!c_parser_require (parser
, CPP_OPEN_PAREN
, "expected %<(%>"))
5314 expr
.value
= error_mark_node
;
5315 expr
.original_code
= ERROR_MARK
;
5318 t1
= c_parser_type_name (parser
);
5321 expr
.value
= error_mark_node
;
5322 expr
.original_code
= ERROR_MARK
;
5325 if (!c_parser_require (parser
, CPP_COMMA
, "expected %<,%>"))
5327 c_parser_skip_until_found (parser
, CPP_CLOSE_PAREN
, NULL
);
5328 expr
.value
= error_mark_node
;
5329 expr
.original_code
= ERROR_MARK
;
5333 tree type
= groktypename (t1
);
5335 if (type
== error_mark_node
)
5336 offsetof_ref
= error_mark_node
;
5338 offsetof_ref
= build1 (INDIRECT_REF
, type
, null_pointer_node
);
5339 /* Parse the second argument to __builtin_offsetof. We
5340 must have one identifier, and beyond that we want to
5341 accept sub structure and sub array references. */
5342 if (c_parser_next_token_is (parser
, CPP_NAME
))
5344 offsetof_ref
= build_component_ref
5345 (offsetof_ref
, c_parser_peek_token (parser
)->value
);
5346 c_parser_consume_token (parser
);
5347 while (c_parser_next_token_is (parser
, CPP_DOT
)
5348 || c_parser_next_token_is (parser
,
5351 if (c_parser_next_token_is (parser
, CPP_DOT
))
5353 c_parser_consume_token (parser
);
5354 if (c_parser_next_token_is_not (parser
,
5357 c_parser_error (parser
, "expected identifier");
5360 offsetof_ref
= build_component_ref
5362 c_parser_peek_token (parser
)->value
);
5363 c_parser_consume_token (parser
);
5368 c_parser_consume_token (parser
);
5369 idx
= c_parser_expression (parser
).value
;
5370 c_parser_skip_until_found (parser
, CPP_CLOSE_SQUARE
,
5372 offsetof_ref
= build_array_ref (offsetof_ref
, idx
);
5377 c_parser_error (parser
, "expected identifier");
5378 c_parser_skip_until_found (parser
, CPP_CLOSE_PAREN
,
5380 expr
.value
= fold_offsetof (offsetof_ref
, NULL_TREE
);
5381 expr
.original_code
= ERROR_MARK
;
5384 case RID_CHOOSE_EXPR
:
5385 c_parser_consume_token (parser
);
5386 if (!c_parser_require (parser
, CPP_OPEN_PAREN
, "expected %<(%>"))
5388 expr
.value
= error_mark_node
;
5389 expr
.original_code
= ERROR_MARK
;
5392 loc
= c_parser_peek_token (parser
)->location
;
5393 e1
= c_parser_expr_no_commas (parser
, NULL
);
5394 if (!c_parser_require (parser
, CPP_COMMA
, "expected %<,%>"))
5396 c_parser_skip_until_found (parser
, CPP_CLOSE_PAREN
, NULL
);
5397 expr
.value
= error_mark_node
;
5398 expr
.original_code
= ERROR_MARK
;
5401 e2
= c_parser_expr_no_commas (parser
, NULL
);
5402 if (!c_parser_require (parser
, CPP_COMMA
, "expected %<,%>"))
5404 c_parser_skip_until_found (parser
, CPP_CLOSE_PAREN
, NULL
);
5405 expr
.value
= error_mark_node
;
5406 expr
.original_code
= ERROR_MARK
;
5409 e3
= c_parser_expr_no_commas (parser
, NULL
);
5410 c_parser_skip_until_found (parser
, CPP_CLOSE_PAREN
,
5415 c
= fold (e1
.value
);
5416 if (TREE_CODE (c
) != INTEGER_CST
)
5417 error ("%Hfirst argument to %<__builtin_choose_expr%> not"
5418 " a constant", &loc
);
5419 expr
= integer_zerop (c
) ? e3
: e2
;
5422 case RID_TYPES_COMPATIBLE_P
:
5423 c_parser_consume_token (parser
);
5424 if (!c_parser_require (parser
, CPP_OPEN_PAREN
, "expected %<(%>"))
5426 expr
.value
= error_mark_node
;
5427 expr
.original_code
= ERROR_MARK
;
5430 t1
= c_parser_type_name (parser
);
5433 expr
.value
= error_mark_node
;
5434 expr
.original_code
= ERROR_MARK
;
5437 if (!c_parser_require (parser
, CPP_COMMA
, "expected %<,%>"))
5439 c_parser_skip_until_found (parser
, CPP_CLOSE_PAREN
, NULL
);
5440 expr
.value
= error_mark_node
;
5441 expr
.original_code
= ERROR_MARK
;
5444 t2
= c_parser_type_name (parser
);
5447 expr
.value
= error_mark_node
;
5448 expr
.original_code
= ERROR_MARK
;
5451 c_parser_skip_until_found (parser
, CPP_CLOSE_PAREN
,
5456 e1
= TYPE_MAIN_VARIANT (groktypename (t1
));
5457 e2
= TYPE_MAIN_VARIANT (groktypename (t2
));
5459 expr
.value
= comptypes (e1
, e2
)
5460 ? build_int_cst (NULL_TREE
, 1)
5461 : build_int_cst (NULL_TREE
, 0);
5462 expr
.original_code
= ERROR_MARK
;
5465 case RID_AT_SELECTOR
:
5466 gcc_assert (c_dialect_objc ());
5467 c_parser_consume_token (parser
);
5468 if (!c_parser_require (parser
, CPP_OPEN_PAREN
, "expected %<(%>"))
5470 expr
.value
= error_mark_node
;
5471 expr
.original_code
= ERROR_MARK
;
5475 tree sel
= c_parser_objc_selector_arg (parser
);
5476 c_parser_skip_until_found (parser
, CPP_CLOSE_PAREN
,
5478 expr
.value
= objc_build_selector_expr (sel
);
5479 expr
.original_code
= ERROR_MARK
;
5482 case RID_AT_PROTOCOL
:
5483 gcc_assert (c_dialect_objc ());
5484 c_parser_consume_token (parser
);
5485 if (!c_parser_require (parser
, CPP_OPEN_PAREN
, "expected %<(%>"))
5487 expr
.value
= error_mark_node
;
5488 expr
.original_code
= ERROR_MARK
;
5491 if (c_parser_next_token_is_not (parser
, CPP_NAME
))
5493 c_parser_error (parser
, "expected identifier");
5494 c_parser_skip_until_found (parser
, CPP_CLOSE_PAREN
, NULL
);
5495 expr
.value
= error_mark_node
;
5496 expr
.original_code
= ERROR_MARK
;
5500 tree id
= c_parser_peek_token (parser
)->value
;
5501 c_parser_consume_token (parser
);
5502 c_parser_skip_until_found (parser
, CPP_CLOSE_PAREN
,
5504 expr
.value
= objc_build_protocol_expr (id
);
5505 expr
.original_code
= ERROR_MARK
;
5509 /* Extension to support C-structures in the archiver. */
5510 gcc_assert (c_dialect_objc ());
5511 c_parser_consume_token (parser
);
5512 if (!c_parser_require (parser
, CPP_OPEN_PAREN
, "expected %<(%>"))
5514 expr
.value
= error_mark_node
;
5515 expr
.original_code
= ERROR_MARK
;
5518 t1
= c_parser_type_name (parser
);
5521 expr
.value
= error_mark_node
;
5522 expr
.original_code
= ERROR_MARK
;
5523 c_parser_skip_until_found (parser
, CPP_CLOSE_PAREN
, NULL
);
5526 c_parser_skip_until_found (parser
, CPP_CLOSE_PAREN
,
5529 tree type
= groktypename (t1
);
5530 expr
.value
= objc_build_encode_expr (type
);
5531 expr
.original_code
= ERROR_MARK
;
5535 c_parser_error (parser
, "expected expression");
5536 expr
.value
= error_mark_node
;
5537 expr
.original_code
= ERROR_MARK
;
5541 case CPP_OPEN_SQUARE
:
5542 if (c_dialect_objc ())
5544 tree receiver
, args
;
5545 c_parser_consume_token (parser
);
5546 receiver
= c_parser_objc_receiver (parser
);
5547 args
= c_parser_objc_message_args (parser
);
5548 c_parser_skip_until_found (parser
, CPP_CLOSE_SQUARE
,
5550 expr
.value
= objc_build_message_expr (build_tree_list (receiver
,
5552 expr
.original_code
= ERROR_MARK
;
5555 /* Else fall through to report error. */
5557 c_parser_error (parser
, "expected expression");
5558 expr
.value
= error_mark_node
;
5559 expr
.original_code
= ERROR_MARK
;
5562 return c_parser_postfix_expression_after_primary (parser
, expr
);
5565 /* Parse a postfix expression after a parenthesized type name: the
5566 brace-enclosed initializer of a compound literal, possibly followed
5567 by some postfix operators. This is separate because it is not
5568 possible to tell until after the type name whether a cast
5569 expression has a cast or a compound literal, or whether the operand
5570 of sizeof is a parenthesized type name or starts with a compound
5573 static struct c_expr
5574 c_parser_postfix_expression_after_paren_type (c_parser
*parser
,
5575 struct c_type_name
*type_name
)
5580 location_t start_loc
;
5581 start_init (NULL_TREE
, NULL
, 0);
5582 type
= groktypename (type_name
);
5583 start_loc
= c_parser_peek_token (parser
)->location
;
5584 if (type
!= error_mark_node
&& C_TYPE_VARIABLE_SIZE (type
))
5586 error ("%Hcompound literal has variable size", &start_loc
);
5587 type
= error_mark_node
;
5589 init
= c_parser_braced_init (parser
, type
, false);
5591 maybe_warn_string_init (type
, init
);
5593 if (pedantic
&& !flag_isoc99
)
5594 pedwarn ("%HISO C90 forbids compound literals", &start_loc
);
5595 expr
.value
= build_compound_literal (type
, init
.value
);
5596 expr
.original_code
= ERROR_MARK
;
5597 return c_parser_postfix_expression_after_primary (parser
, expr
);
5600 /* Parse a postfix expression after the initial primary or compound
5601 literal; that is, parse a series of postfix operators. */
5603 static struct c_expr
5604 c_parser_postfix_expression_after_primary (c_parser
*parser
,
5607 tree ident
, idx
, exprlist
;
5610 switch (c_parser_peek_token (parser
)->type
)
5612 case CPP_OPEN_SQUARE
:
5613 /* Array reference. */
5614 c_parser_consume_token (parser
);
5615 idx
= c_parser_expression (parser
).value
;
5616 c_parser_skip_until_found (parser
, CPP_CLOSE_SQUARE
,
5618 expr
.value
= build_array_ref (expr
.value
, idx
);
5619 expr
.original_code
= ERROR_MARK
;
5621 case CPP_OPEN_PAREN
:
5622 /* Function call. */
5623 c_parser_consume_token (parser
);
5624 if (c_parser_next_token_is (parser
, CPP_CLOSE_PAREN
))
5625 exprlist
= NULL_TREE
;
5627 exprlist
= c_parser_expr_list (parser
, true);
5628 c_parser_skip_until_found (parser
, CPP_CLOSE_PAREN
,
5630 expr
.value
= build_function_call (expr
.value
, exprlist
);
5631 expr
.original_code
= ERROR_MARK
;
5634 /* Structure element reference. */
5635 c_parser_consume_token (parser
);
5636 expr
= default_function_array_conversion (expr
);
5637 if (c_parser_next_token_is (parser
, CPP_NAME
))
5638 ident
= c_parser_peek_token (parser
)->value
;
5641 c_parser_error (parser
, "expected identifier");
5642 expr
.value
= error_mark_node
;
5643 expr
.original_code
= ERROR_MARK
;
5646 c_parser_consume_token (parser
);
5647 expr
.value
= build_component_ref (expr
.value
, ident
);
5648 expr
.original_code
= ERROR_MARK
;
5651 /* Structure element reference. */
5652 c_parser_consume_token (parser
);
5653 expr
= default_function_array_conversion (expr
);
5654 if (c_parser_next_token_is (parser
, CPP_NAME
))
5655 ident
= c_parser_peek_token (parser
)->value
;
5658 c_parser_error (parser
, "expected identifier");
5659 expr
.value
= error_mark_node
;
5660 expr
.original_code
= ERROR_MARK
;
5663 c_parser_consume_token (parser
);
5664 expr
.value
= build_component_ref (build_indirect_ref (expr
.value
,
5666 expr
.original_code
= ERROR_MARK
;
5669 /* Postincrement. */
5670 c_parser_consume_token (parser
);
5671 expr
= default_function_array_conversion (expr
);
5672 expr
.value
= build_unary_op (POSTINCREMENT_EXPR
, expr
.value
, 0);
5673 expr
.original_code
= ERROR_MARK
;
5675 case CPP_MINUS_MINUS
:
5676 /* Postdecrement. */
5677 c_parser_consume_token (parser
);
5678 expr
= default_function_array_conversion (expr
);
5679 expr
.value
= build_unary_op (POSTDECREMENT_EXPR
, expr
.value
, 0);
5680 expr
.original_code
= ERROR_MARK
;
5688 /* Parse an expression (C90 6.3.17, C99 6.5.17).
5691 assignment-expression
5692 expression , assignment-expression
5695 static struct c_expr
5696 c_parser_expression (c_parser
*parser
)
5699 expr
= c_parser_expr_no_commas (parser
, NULL
);
5700 while (c_parser_next_token_is (parser
, CPP_COMMA
))
5703 c_parser_consume_token (parser
);
5704 next
= c_parser_expr_no_commas (parser
, NULL
);
5705 next
= default_function_array_conversion (next
);
5706 expr
.value
= build_compound_expr (expr
.value
, next
.value
);
5707 expr
.original_code
= COMPOUND_EXPR
;
5712 /* Parse an expression and convert functions or arrays to
5715 static struct c_expr
5716 c_parser_expression_conv (c_parser
*parser
)
5719 expr
= c_parser_expression (parser
);
5720 expr
= default_function_array_conversion (expr
);
5724 /* Parse a non-empty list of expressions. If CONVERT_P, convert
5725 functions and arrays to pointers.
5728 assignment-expression
5729 nonempty-expr-list , assignment-expression
5733 c_parser_expr_list (c_parser
*parser
, bool convert_p
)
5737 expr
= c_parser_expr_no_commas (parser
, NULL
);
5739 expr
= default_function_array_conversion (expr
);
5740 ret
= cur
= build_tree_list (NULL_TREE
, expr
.value
);
5741 while (c_parser_next_token_is (parser
, CPP_COMMA
))
5743 c_parser_consume_token (parser
);
5744 expr
= c_parser_expr_no_commas (parser
, NULL
);
5746 expr
= default_function_array_conversion (expr
);
5747 cur
= TREE_CHAIN (cur
) = build_tree_list (NULL_TREE
, expr
.value
);
5753 /* Parse Objective-C-specific constructs. */
5755 /* Parse an objc-class-definition.
5757 objc-class-definition:
5758 @interface identifier objc-superclass[opt] objc-protocol-refs[opt]
5759 objc-class-instance-variables[opt] objc-methodprotolist @end
5760 @implementation identifier objc-superclass[opt]
5761 objc-class-instance-variables[opt]
5762 @interface identifier ( identifier ) objc-protocol-refs[opt]
5763 objc-methodprotolist @end
5764 @implementation identifier ( identifier )
5769 "@interface identifier (" must start "@interface identifier (
5770 identifier ) ...": objc-methodprotolist in the first production may
5771 not start with a parenthesized identifier as a declarator of a data
5772 definition with no declaration specifiers if the objc-superclass,
5773 objc-protocol-refs and objc-class-instance-variables are omitted. */
5776 c_parser_objc_class_definition (c_parser
*parser
)
5781 if (c_parser_next_token_is_keyword (parser
, RID_AT_INTERFACE
))
5783 else if (c_parser_next_token_is_keyword (parser
, RID_AT_IMPLEMENTATION
))
5787 c_parser_consume_token (parser
);
5788 if (c_parser_next_token_is_not (parser
, CPP_NAME
))
5790 c_parser_error (parser
, "expected identifier");
5793 id1
= c_parser_peek_token (parser
)->value
;
5794 c_parser_consume_token (parser
);
5795 if (c_parser_next_token_is (parser
, CPP_OPEN_PAREN
))
5798 tree proto
= NULL_TREE
;
5799 c_parser_consume_token (parser
);
5800 if (c_parser_next_token_is_not (parser
, CPP_NAME
))
5802 c_parser_error (parser
, "expected identifier");
5803 c_parser_skip_until_found (parser
, CPP_CLOSE_PAREN
, NULL
);
5806 id2
= c_parser_peek_token (parser
)->value
;
5807 c_parser_consume_token (parser
);
5808 c_parser_skip_until_found (parser
, CPP_CLOSE_PAREN
, "expected %<)%>");
5811 objc_start_category_implementation (id1
, id2
);
5814 if (c_parser_next_token_is (parser
, CPP_LESS
))
5815 proto
= c_parser_objc_protocol_refs (parser
);
5816 objc_start_category_interface (id1
, id2
, proto
);
5817 c_parser_objc_methodprotolist (parser
);
5818 c_parser_require_keyword (parser
, RID_AT_END
, "expected %<@end%>");
5819 objc_finish_interface ();
5822 if (c_parser_next_token_is (parser
, CPP_COLON
))
5824 c_parser_consume_token (parser
);
5825 if (c_parser_next_token_is_not (parser
, CPP_NAME
))
5827 c_parser_error (parser
, "expected identifier");
5830 superclass
= c_parser_peek_token (parser
)->value
;
5831 c_parser_consume_token (parser
);
5834 superclass
= NULL_TREE
;
5837 tree proto
= NULL_TREE
;
5838 if (c_parser_next_token_is (parser
, CPP_LESS
))
5839 proto
= c_parser_objc_protocol_refs (parser
);
5840 objc_start_class_interface (id1
, superclass
, proto
);
5843 objc_start_class_implementation (id1
, superclass
);
5844 if (c_parser_next_token_is (parser
, CPP_OPEN_BRACE
))
5845 c_parser_objc_class_instance_variables (parser
);
5848 objc_continue_interface ();
5849 c_parser_objc_methodprotolist (parser
);
5850 c_parser_require_keyword (parser
, RID_AT_END
, "expected %<@end%>");
5851 objc_finish_interface ();
5855 objc_continue_implementation ();
5860 /* Parse objc-class-instance-variables.
5862 objc-class-instance-variables:
5863 { objc-instance-variable-decl-list[opt] }
5865 objc-instance-variable-decl-list:
5866 objc-visibility-spec
5867 objc-instance-variable-decl ;
5869 objc-instance-variable-decl-list objc-visibility-spec
5870 objc-instance-variable-decl-list objc-instance-variable-decl ;
5871 objc-instance-variable-decl-list ;
5873 objc-visibility-spec:
5878 objc-instance-variable-decl:
5883 c_parser_objc_class_instance_variables (c_parser
*parser
)
5885 gcc_assert (c_parser_next_token_is (parser
, CPP_OPEN_BRACE
));
5886 c_parser_consume_token (parser
);
5887 while (c_parser_next_token_is_not (parser
, CPP_EOF
))
5890 /* Parse any stray semicolon. */
5891 if (c_parser_next_token_is (parser
, CPP_SEMICOLON
))
5894 pedwarn ("%Hextra semicolon in struct or union specified",
5895 &c_parser_peek_token (parser
)->location
);
5896 c_parser_consume_token (parser
);
5899 /* Stop if at the end of the instance variables. */
5900 if (c_parser_next_token_is (parser
, CPP_CLOSE_BRACE
))
5902 c_parser_consume_token (parser
);
5905 /* Parse any objc-visibility-spec. */
5906 if (c_parser_next_token_is_keyword (parser
, RID_AT_PRIVATE
))
5908 c_parser_consume_token (parser
);
5909 objc_set_visibility (2);
5912 else if (c_parser_next_token_is_keyword (parser
, RID_AT_PROTECTED
))
5914 c_parser_consume_token (parser
);
5915 objc_set_visibility (0);
5918 else if (c_parser_next_token_is_keyword (parser
, RID_AT_PUBLIC
))
5920 c_parser_consume_token (parser
);
5921 objc_set_visibility (1);
5924 else if (c_parser_next_token_is (parser
, CPP_PRAGMA
))
5926 c_parser_pragma (parser
, pragma_external
);
5930 /* Parse some comma-separated declarations. */
5931 decls
= c_parser_struct_declaration (parser
);
5933 /* Comma-separated instance variables are chained together in
5934 reverse order; add them one by one. */
5935 tree ivar
= nreverse (decls
);
5936 for (; ivar
; ivar
= TREE_CHAIN (ivar
))
5937 objc_add_instance_variable (copy_node (ivar
));
5939 c_parser_skip_until_found (parser
, CPP_SEMICOLON
, "expected %<;%>");
5943 /* Parse an objc-class-declaration.
5945 objc-class-declaration:
5946 @class identifier-list ;
5950 c_parser_objc_class_declaration (c_parser
*parser
)
5952 tree list
= NULL_TREE
;
5953 gcc_assert (c_parser_next_token_is_keyword (parser
, RID_AT_CLASS
));
5954 c_parser_consume_token (parser
);
5955 /* Any identifiers, including those declared as type names, are OK
5960 if (c_parser_next_token_is_not (parser
, CPP_NAME
))
5962 c_parser_error (parser
, "expected identifier");
5965 id
= c_parser_peek_token (parser
)->value
;
5966 list
= chainon (list
, build_tree_list (NULL_TREE
, id
));
5967 c_parser_consume_token (parser
);
5968 if (c_parser_next_token_is (parser
, CPP_COMMA
))
5969 c_parser_consume_token (parser
);
5973 c_parser_skip_until_found (parser
, CPP_SEMICOLON
, "expected %<;%>");
5974 objc_declare_class (list
);
5977 /* Parse an objc-alias-declaration.
5979 objc-alias-declaration:
5980 @compatibility_alias identifier identifier ;
5984 c_parser_objc_alias_declaration (c_parser
*parser
)
5987 gcc_assert (c_parser_next_token_is_keyword (parser
, RID_AT_ALIAS
));
5988 c_parser_consume_token (parser
);
5989 if (c_parser_next_token_is_not (parser
, CPP_NAME
))
5991 c_parser_error (parser
, "expected identifier");
5992 c_parser_skip_until_found (parser
, CPP_SEMICOLON
, NULL
);
5995 id1
= c_parser_peek_token (parser
)->value
;
5996 c_parser_consume_token (parser
);
5997 if (c_parser_next_token_is_not (parser
, CPP_NAME
))
5999 c_parser_error (parser
, "expected identifier");
6000 c_parser_skip_until_found (parser
, CPP_SEMICOLON
, NULL
);
6003 id2
= c_parser_peek_token (parser
)->value
;
6004 c_parser_consume_token (parser
);
6005 c_parser_skip_until_found (parser
, CPP_SEMICOLON
, "expected %<;%>");
6006 objc_declare_alias (id1
, id2
);
6009 /* Parse an objc-protocol-definition.
6011 objc-protocol-definition:
6012 @protocol identifier objc-protocol-refs[opt] objc-methodprotolist @end
6013 @protocol identifier-list ;
6015 "@protocol identifier ;" should be resolved as "@protocol
6016 identifier-list ;": objc-methodprotolist may not start with a
6017 semicolon in the first alternative if objc-protocol-refs are
6021 c_parser_objc_protocol_definition (c_parser
*parser
)
6023 gcc_assert (c_parser_next_token_is_keyword (parser
, RID_AT_PROTOCOL
));
6024 c_parser_consume_token (parser
);
6025 if (c_parser_next_token_is_not (parser
, CPP_NAME
))
6027 c_parser_error (parser
, "expected identifier");
6030 if (c_parser_peek_2nd_token (parser
)->type
== CPP_COMMA
6031 || c_parser_peek_2nd_token (parser
)->type
== CPP_SEMICOLON
)
6033 tree list
= NULL_TREE
;
6034 /* Any identifiers, including those declared as type names, are
6039 if (c_parser_next_token_is_not (parser
, CPP_NAME
))
6041 c_parser_error (parser
, "expected identifier");
6044 id
= c_parser_peek_token (parser
)->value
;
6045 list
= chainon (list
, build_tree_list (NULL_TREE
, id
));
6046 c_parser_consume_token (parser
);
6047 if (c_parser_next_token_is (parser
, CPP_COMMA
))
6048 c_parser_consume_token (parser
);
6052 c_parser_skip_until_found (parser
, CPP_SEMICOLON
, "expected %<;%>");
6053 objc_declare_protocols (list
);
6057 tree id
= c_parser_peek_token (parser
)->value
;
6058 tree proto
= NULL_TREE
;
6059 c_parser_consume_token (parser
);
6060 if (c_parser_next_token_is (parser
, CPP_LESS
))
6061 proto
= c_parser_objc_protocol_refs (parser
);
6062 parser
->objc_pq_context
= true;
6063 objc_start_protocol (id
, proto
);
6064 c_parser_objc_methodprotolist (parser
);
6065 c_parser_require_keyword (parser
, RID_AT_END
, "expected %<@end%>");
6066 parser
->objc_pq_context
= false;
6067 objc_finish_interface ();
6071 /* Parse an objc-method-type.
6078 static enum tree_code
6079 c_parser_objc_method_type (c_parser
*parser
)
6081 switch (c_parser_peek_token (parser
)->type
)
6084 c_parser_consume_token (parser
);
6087 c_parser_consume_token (parser
);
6094 /* Parse an objc-method-definition.
6096 objc-method-definition:
6097 objc-method-type objc-method-decl ;[opt] compound-statement
6101 c_parser_objc_method_definition (c_parser
*parser
)
6103 enum tree_code type
= c_parser_objc_method_type (parser
);
6105 objc_set_method_type (type
);
6106 parser
->objc_pq_context
= true;
6107 decl
= c_parser_objc_method_decl (parser
);
6108 if (c_parser_next_token_is (parser
, CPP_SEMICOLON
))
6110 c_parser_consume_token (parser
);
6112 pedwarn ("%Hextra semicolon in method definition specified",
6113 &c_parser_peek_token (parser
)->location
);
6115 if (!c_parser_next_token_is (parser
, CPP_OPEN_BRACE
))
6117 c_parser_error (parser
, "expected %<{%>");
6120 parser
->objc_pq_context
= false;
6121 objc_start_method_definition (decl
);
6122 add_stmt (c_parser_compound_statement (parser
));
6123 objc_finish_method_definition (current_function_decl
);
6126 /* Parse an objc-methodprotolist.
6128 objc-methodprotolist:
6130 objc-methodprotolist objc-methodproto
6131 objc-methodprotolist declaration
6132 objc-methodprotolist ;
6134 The declaration is a data definition, which may be missing
6135 declaration specifiers under the same rules and diagnostics as
6136 other data definitions outside functions, and the stray semicolon
6137 is diagnosed the same way as a stray semicolon outside a
6141 c_parser_objc_methodprotolist (c_parser
*parser
)
6145 /* The list is terminated by @end. */
6146 switch (c_parser_peek_token (parser
)->type
)
6150 pedwarn ("%HISO C does not allow extra %<;%> "
6151 "outside of a function",
6152 &c_parser_peek_token (parser
)->location
);
6153 c_parser_consume_token (parser
);
6157 c_parser_objc_methodproto (parser
);
6160 c_parser_pragma (parser
, pragma_external
);
6165 if (c_parser_next_token_is_keyword (parser
, RID_AT_END
))
6167 c_parser_declaration_or_fndef (parser
, false, true, false, true);
6173 /* Parse an objc-methodproto.
6176 objc-method-type objc-method-decl ;
6180 c_parser_objc_methodproto (c_parser
*parser
)
6182 enum tree_code type
= c_parser_objc_method_type (parser
);
6184 objc_set_method_type (type
);
6185 /* Remember protocol qualifiers in prototypes. */
6186 parser
->objc_pq_context
= true;
6187 decl
= c_parser_objc_method_decl (parser
);
6188 /* Forget protocol qualifiers here. */
6189 parser
->objc_pq_context
= false;
6190 objc_add_method_declaration (decl
);
6191 c_parser_skip_until_found (parser
, CPP_SEMICOLON
, "expected %<;%>");
6194 /* Parse an objc-method-decl.
6197 ( objc-type-name ) objc-selector
6199 ( objc-type-name ) objc-keyword-selector objc-optparmlist
6200 objc-keyword-selector objc-optparmlist
6202 objc-keyword-selector:
6204 objc-keyword-selector objc-keyword-decl
6207 objc-selector : ( objc-type-name ) identifier
6208 objc-selector : identifier
6209 : ( objc-type-name ) identifier
6213 objc-optparms objc-optellipsis
6217 objc-opt-parms , parameter-declaration
6225 c_parser_objc_method_decl (c_parser
*parser
)
6227 tree type
= NULL_TREE
;
6229 tree parms
= NULL_TREE
;
6230 bool ellipsis
= false;
6232 if (c_parser_next_token_is (parser
, CPP_OPEN_PAREN
))
6234 c_parser_consume_token (parser
);
6235 type
= c_parser_objc_type_name (parser
);
6236 c_parser_skip_until_found (parser
, CPP_CLOSE_PAREN
, "expected %<)%>");
6238 sel
= c_parser_objc_selector (parser
);
6239 /* If there is no selector, or a colon follows, we have an
6240 objc-keyword-selector. If there is a selector, and a colon does
6241 not follow, that selector ends the objc-method-decl. */
6242 if (!sel
|| c_parser_next_token_is (parser
, CPP_COLON
))
6245 tree list
= NULL_TREE
;
6248 tree atype
= NULL_TREE
, id
, keyworddecl
;
6249 if (!c_parser_require (parser
, CPP_COLON
, "expected %<:%>"))
6251 if (c_parser_next_token_is (parser
, CPP_OPEN_PAREN
))
6253 c_parser_consume_token (parser
);
6254 atype
= c_parser_objc_type_name (parser
);
6255 c_parser_skip_until_found (parser
, CPP_CLOSE_PAREN
,
6258 if (c_parser_next_token_is_not (parser
, CPP_NAME
))
6260 c_parser_error (parser
, "expected identifier");
6261 return error_mark_node
;
6263 id
= c_parser_peek_token (parser
)->value
;
6264 c_parser_consume_token (parser
);
6265 keyworddecl
= objc_build_keyword_decl (tsel
, atype
, id
);
6266 list
= chainon (list
, keyworddecl
);
6267 tsel
= c_parser_objc_selector (parser
);
6268 if (!tsel
&& c_parser_next_token_is_not (parser
, CPP_COLON
))
6271 /* Parse the optional parameter list. Optional Objective-C
6272 method parameters follow the C syntax, and may include '...'
6273 to denote a variable number of arguments. */
6274 parms
= make_node (TREE_LIST
);
6275 while (c_parser_next_token_is (parser
, CPP_COMMA
))
6277 struct c_parm
*parm
;
6278 c_parser_consume_token (parser
);
6279 if (c_parser_next_token_is (parser
, CPP_ELLIPSIS
))
6282 c_parser_consume_token (parser
);
6285 parm
= c_parser_parameter_declaration (parser
, NULL_TREE
);
6288 parms
= chainon (parms
,
6289 build_tree_list (NULL_TREE
, grokparm (parm
)));
6293 return objc_build_method_signature (type
, sel
, parms
, ellipsis
);
6296 /* Parse an objc-type-name.
6299 objc-type-qualifiers[opt] type-name
6300 objc-type-qualifiers[opt]
6302 objc-type-qualifiers:
6304 objc-type-qualifiers objc-type-qualifier
6306 objc-type-qualifier: one of
6307 in out inout bycopy byref oneway
6311 c_parser_objc_type_name (c_parser
*parser
)
6313 tree quals
= NULL_TREE
;
6314 struct c_type_name
*typename
= NULL
;
6315 tree type
= NULL_TREE
;
6318 c_token
*token
= c_parser_peek_token (parser
);
6319 if (token
->type
== CPP_KEYWORD
6320 && (token
->keyword
== RID_IN
6321 || token
->keyword
== RID_OUT
6322 || token
->keyword
== RID_INOUT
6323 || token
->keyword
== RID_BYCOPY
6324 || token
->keyword
== RID_BYREF
6325 || token
->keyword
== RID_ONEWAY
))
6327 quals
= chainon (quals
, build_tree_list (NULL_TREE
, token
->value
));
6328 c_parser_consume_token (parser
);
6333 if (c_parser_next_token_starts_typename (parser
))
6334 typename
= c_parser_type_name (parser
);
6336 type
= groktypename (typename
);
6337 return build_tree_list (quals
, type
);
6340 /* Parse objc-protocol-refs.
6347 c_parser_objc_protocol_refs (c_parser
*parser
)
6349 tree list
= NULL_TREE
;
6350 gcc_assert (c_parser_next_token_is (parser
, CPP_LESS
));
6351 c_parser_consume_token (parser
);
6352 /* Any identifiers, including those declared as type names, are OK
6357 if (c_parser_next_token_is_not (parser
, CPP_NAME
))
6359 c_parser_error (parser
, "expected identifier");
6362 id
= c_parser_peek_token (parser
)->value
;
6363 list
= chainon (list
, build_tree_list (NULL_TREE
, id
));
6364 c_parser_consume_token (parser
);
6365 if (c_parser_next_token_is (parser
, CPP_COMMA
))
6366 c_parser_consume_token (parser
);
6370 c_parser_require (parser
, CPP_GREATER
, "expected %<>%>");
6374 /* Parse an objc-try-catch-statement.
6376 objc-try-catch-statement:
6377 @try compound-statement objc-catch-list[opt]
6378 @try compound-statement objc-catch-list[opt] @finally compound-statement
6381 @catch ( parameter-declaration ) compound-statement
6382 objc-catch-list @catch ( parameter-declaration ) compound-statement
6386 c_parser_objc_try_catch_statement (c_parser
*parser
)
6390 gcc_assert (c_parser_next_token_is_keyword (parser
, RID_AT_TRY
));
6391 c_parser_consume_token (parser
);
6392 loc
= c_parser_peek_token (parser
)->location
;
6393 stmt
= c_parser_compound_statement (parser
);
6394 objc_begin_try_stmt (loc
, stmt
);
6395 while (c_parser_next_token_is_keyword (parser
, RID_AT_CATCH
))
6397 struct c_parm
*parm
;
6398 c_parser_consume_token (parser
);
6399 if (!c_parser_require (parser
, CPP_OPEN_PAREN
, "expected %<(%>"))
6401 parm
= c_parser_parameter_declaration (parser
, NULL_TREE
);
6404 c_parser_skip_until_found (parser
, CPP_CLOSE_PAREN
, NULL
);
6407 c_parser_skip_until_found (parser
, CPP_CLOSE_PAREN
, "expected %<)%>");
6408 objc_begin_catch_clause (grokparm (parm
));
6409 if (c_parser_require (parser
, CPP_OPEN_BRACE
, "expected %<{%>"))
6410 c_parser_compound_statement_nostart (parser
);
6411 objc_finish_catch_clause ();
6413 if (c_parser_next_token_is_keyword (parser
, RID_AT_FINALLY
))
6417 c_parser_consume_token (parser
);
6418 finloc
= c_parser_peek_token (parser
)->location
;
6419 finstmt
= c_parser_compound_statement (parser
);
6420 objc_build_finally_clause (finloc
, finstmt
);
6422 objc_finish_try_stmt ();
6425 /* Parse an objc-synchronized-statement.
6427 objc-synchronized-statement:
6428 @synchronized ( expression ) compound-statement
6432 c_parser_objc_synchronized_statement (c_parser
*parser
)
6436 gcc_assert (c_parser_next_token_is_keyword (parser
, RID_AT_SYNCHRONIZED
));
6437 c_parser_consume_token (parser
);
6438 loc
= c_parser_peek_token (parser
)->location
;
6439 if (c_parser_require (parser
, CPP_OPEN_PAREN
, "expected %<(%>"))
6441 expr
= c_parser_expression (parser
).value
;
6442 c_parser_skip_until_found (parser
, CPP_CLOSE_PAREN
, "expected %<)%>");
6445 expr
= error_mark_node
;
6446 stmt
= c_parser_compound_statement (parser
);
6447 objc_build_synchronized (loc
, expr
, stmt
);
6450 /* Parse an objc-selector; return NULL_TREE without an error if the
6451 next token is not an objc-selector.
6456 enum struct union if else while do for switch case default
6457 break continue return goto asm sizeof typeof __alignof
6458 unsigned long const short volatile signed restrict _Complex
6459 in out inout bycopy byref oneway int char float double void _Bool
6461 ??? Why this selection of keywords but not, for example, storage
6462 class specifiers? */
6465 c_parser_objc_selector (c_parser
*parser
)
6467 c_token
*token
= c_parser_peek_token (parser
);
6468 tree value
= token
->value
;
6469 if (token
->type
== CPP_NAME
)
6471 c_parser_consume_token (parser
);
6474 if (token
->type
!= CPP_KEYWORD
)
6476 switch (token
->keyword
)
6517 c_parser_consume_token (parser
);
6524 /* Parse an objc-selector-arg.
6528 objc-keywordname-list
6530 objc-keywordname-list:
6532 objc-keywordname-list objc-keywordname
6540 c_parser_objc_selector_arg (c_parser
*parser
)
6542 tree sel
= c_parser_objc_selector (parser
);
6543 tree list
= NULL_TREE
;
6544 if (sel
&& c_parser_next_token_is_not (parser
, CPP_COLON
))
6548 if (!c_parser_require (parser
, CPP_COLON
, "expected %<:%>"))
6550 list
= chainon (list
, build_tree_list (sel
, NULL_TREE
));
6551 sel
= c_parser_objc_selector (parser
);
6552 if (!sel
&& c_parser_next_token_is_not (parser
, CPP_COLON
))
6558 /* Parse an objc-receiver.
6567 c_parser_objc_receiver (c_parser
*parser
)
6569 if (c_parser_peek_token (parser
)->type
== CPP_NAME
6570 && (c_parser_peek_token (parser
)->id_kind
== C_ID_TYPENAME
6571 || c_parser_peek_token (parser
)->id_kind
== C_ID_CLASSNAME
))
6573 tree id
= c_parser_peek_token (parser
)->value
;
6574 c_parser_consume_token (parser
);
6575 return objc_get_class_reference (id
);
6577 return c_parser_expression (parser
).value
;
6580 /* Parse objc-message-args.
6584 objc-keywordarg-list
6586 objc-keywordarg-list:
6588 objc-keywordarg-list objc-keywordarg
6591 objc-selector : objc-keywordexpr
6596 c_parser_objc_message_args (c_parser
*parser
)
6598 tree sel
= c_parser_objc_selector (parser
);
6599 tree list
= NULL_TREE
;
6600 if (sel
&& c_parser_next_token_is_not (parser
, CPP_COLON
))
6605 if (!c_parser_require (parser
, CPP_COLON
, "expected %<:%>"))
6607 keywordexpr
= c_parser_objc_keywordexpr (parser
);
6608 list
= chainon (list
, build_tree_list (sel
, keywordexpr
));
6609 sel
= c_parser_objc_selector (parser
);
6610 if (!sel
&& c_parser_next_token_is_not (parser
, CPP_COLON
))
6616 /* Parse an objc-keywordexpr.
6623 c_parser_objc_keywordexpr (c_parser
*parser
)
6625 tree list
= c_parser_expr_list (parser
, true);
6626 if (TREE_CHAIN (list
) == NULL_TREE
)
6628 /* Just return the expression, remove a level of
6630 return TREE_VALUE (list
);
6634 /* We have a comma expression, we will collapse later. */
6640 /* Handle pragmas. Some OpenMP pragmas are associated with, and therefore
6641 should be considered, statements. ALLOW_STMT is true if we're within
6642 the context of a function and such pragmas are to be allowed. Returns
6643 true if we actually parsed such a pragma. */
6646 c_parser_pragma (c_parser
*parser
, enum pragma_context context
)
6650 id
= c_parser_peek_token (parser
)->pragma_kind
;
6651 gcc_assert (id
!= PRAGMA_NONE
);
6655 case PRAGMA_OMP_BARRIER
:
6656 if (context
!= pragma_compound
)
6658 if (context
== pragma_stmt
)
6659 c_parser_error (parser
, "%<#pragma omp barrier%> may only be "
6660 "used in compound statements");
6663 c_parser_omp_barrier (parser
);
6666 case PRAGMA_OMP_FLUSH
:
6667 if (context
!= pragma_compound
)
6669 if (context
== pragma_stmt
)
6670 c_parser_error (parser
, "%<#pragma omp flush%> may only be "
6671 "used in compound statements");
6674 c_parser_omp_flush (parser
);
6677 case PRAGMA_OMP_THREADPRIVATE
:
6678 c_parser_omp_threadprivate (parser
);
6681 case PRAGMA_OMP_SECTION
:
6682 error ("%H%<#pragma omp section%> may only be used in "
6683 "%<#pragma omp sections%> construct",
6684 &c_parser_peek_token (parser
)->location
);
6685 c_parser_skip_until_found (parser
, CPP_PRAGMA_EOL
, NULL
);
6688 case PRAGMA_GCC_PCH_PREPROCESS
:
6689 c_parser_error (parser
, "%<#pragma GCC pch_preprocess%> must be first");
6690 c_parser_skip_until_found (parser
, CPP_PRAGMA_EOL
, NULL
);
6694 if (id
< PRAGMA_FIRST_EXTERNAL
)
6696 if (context
== pragma_external
)
6699 c_parser_error (parser
, "expected declaration specifiers");
6700 c_parser_skip_until_found (parser
, CPP_PRAGMA_EOL
, NULL
);
6703 c_parser_omp_construct (parser
);
6709 c_parser_consume_pragma (parser
);
6710 c_invoke_pragma_handler (id
);
6712 /* Skip to EOL, but suppress any error message. Those will have been
6713 generated by the handler routine through calling error, as opposed
6714 to calling c_parser_error. */
6715 parser
->error
= true;
6716 c_parser_skip_to_pragma_eol (parser
);
6721 /* The interface the pragma parsers have to the lexer. */
6724 pragma_lex (tree
*value
)
6726 c_token
*tok
= c_parser_peek_token (the_parser
);
6727 enum cpp_ttype ret
= tok
->type
;
6729 *value
= tok
->value
;
6730 if (ret
== CPP_PRAGMA_EOL
|| ret
== CPP_EOF
)
6734 if (ret
== CPP_KEYWORD
)
6736 c_parser_consume_token (the_parser
);
6743 c_parser_pragma_pch_preprocess (c_parser
*parser
)
6747 c_parser_consume_pragma (parser
);
6748 if (c_parser_next_token_is (parser
, CPP_STRING
))
6750 name
= c_parser_peek_token (parser
)->value
;
6751 c_parser_consume_token (parser
);
6754 c_parser_error (parser
, "expected string literal");
6755 c_parser_skip_to_pragma_eol (parser
);
6758 c_common_pch_pragma (parse_in
, TREE_STRING_POINTER (name
));
6761 /* OpenMP 2.5 parsing routines. */
6763 /* Returns name of the next clause.
6764 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
6765 the token is not consumed. Otherwise appropriate pragma_omp_clause is
6766 returned and the token is consumed. */
6768 static pragma_omp_clause
6769 c_parser_omp_clause_name (c_parser
*parser
)
6771 pragma_omp_clause result
= PRAGMA_OMP_CLAUSE_NONE
;
6773 if (c_parser_next_token_is_keyword (parser
, RID_IF
))
6774 result
= PRAGMA_OMP_CLAUSE_IF
;
6775 else if (c_parser_next_token_is_keyword (parser
, RID_DEFAULT
))
6776 result
= PRAGMA_OMP_CLAUSE_DEFAULT
;
6777 else if (c_parser_next_token_is (parser
, CPP_NAME
))
6779 const char *p
= IDENTIFIER_POINTER (c_parser_peek_token (parser
)->value
);
6784 if (!strcmp ("copyin", p
))
6785 result
= PRAGMA_OMP_CLAUSE_COPYIN
;
6786 else if (!strcmp ("copyprivate", p
))
6787 result
= PRAGMA_OMP_CLAUSE_COPYPRIVATE
;
6790 if (!strcmp ("firstprivate", p
))
6791 result
= PRAGMA_OMP_CLAUSE_FIRSTPRIVATE
;
6794 if (!strcmp ("lastprivate", p
))
6795 result
= PRAGMA_OMP_CLAUSE_LASTPRIVATE
;
6798 if (!strcmp ("nowait", p
))
6799 result
= PRAGMA_OMP_CLAUSE_NOWAIT
;
6800 else if (!strcmp ("num_threads", p
))
6801 result
= PRAGMA_OMP_CLAUSE_NUM_THREADS
;
6804 if (!strcmp ("ordered", p
))
6805 result
= PRAGMA_OMP_CLAUSE_ORDERED
;
6808 if (!strcmp ("private", p
))
6809 result
= PRAGMA_OMP_CLAUSE_PRIVATE
;
6812 if (!strcmp ("reduction", p
))
6813 result
= PRAGMA_OMP_CLAUSE_REDUCTION
;
6816 if (!strcmp ("schedule", p
))
6817 result
= PRAGMA_OMP_CLAUSE_SCHEDULE
;
6818 else if (!strcmp ("shared", p
))
6819 result
= PRAGMA_OMP_CLAUSE_SHARED
;
6824 if (result
!= PRAGMA_OMP_CLAUSE_NONE
)
6825 c_parser_consume_token (parser
);
6830 /* Validate that a clause of the given type does not already exist. */
6833 check_no_duplicate_clause (tree clauses
, enum tree_code code
, const char *name
)
6837 for (c
= clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
6838 if (OMP_CLAUSE_CODE (c
) == code
)
6840 error ("too many %qs clauses", name
);
6848 variable-list , identifier
6850 If KIND is nonzero, create the appropriate node and install the decl
6851 in OMP_CLAUSE_DECL and add the node to the head of the list.
6853 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
6854 return the list created. */
6857 c_parser_omp_variable_list (c_parser
*parser
, enum omp_clause_code kind
,
6860 if (c_parser_next_token_is_not (parser
, CPP_NAME
)
6861 || c_parser_peek_token (parser
)->id_kind
!= C_ID_ID
)
6862 c_parser_error (parser
, "expected identifier");
6864 while (c_parser_next_token_is (parser
, CPP_NAME
)
6865 && c_parser_peek_token (parser
)->id_kind
== C_ID_ID
)
6867 tree t
= lookup_name (c_parser_peek_token (parser
)->value
);
6870 undeclared_variable (c_parser_peek_token (parser
)->value
,
6871 c_parser_peek_token (parser
)->location
);
6872 else if (t
== error_mark_node
)
6876 tree u
= build_omp_clause (kind
);
6877 OMP_CLAUSE_DECL (u
) = t
;
6878 OMP_CLAUSE_CHAIN (u
) = list
;
6882 list
= tree_cons (t
, NULL_TREE
, list
);
6884 c_parser_consume_token (parser
);
6886 if (c_parser_next_token_is_not (parser
, CPP_COMMA
))
6889 c_parser_consume_token (parser
);
6895 /* Similarly, but expect leading and trailing parenthesis. This is a very
6896 common case for omp clauses. */
6899 c_parser_omp_var_list_parens (c_parser
*parser
, enum tree_code kind
, tree list
)
6901 if (c_parser_require (parser
, CPP_OPEN_PAREN
, "expected %<(%>"))
6903 list
= c_parser_omp_variable_list (parser
, kind
, list
);
6904 c_parser_skip_until_found (parser
, CPP_CLOSE_PAREN
, "expected %<)%>");
6910 copyin ( variable-list ) */
6913 c_parser_omp_clause_copyin (c_parser
*parser
, tree list
)
6915 return c_parser_omp_var_list_parens (parser
, OMP_CLAUSE_COPYIN
, list
);
6919 copyprivate ( variable-list ) */
6922 c_parser_omp_clause_copyprivate (c_parser
*parser
, tree list
)
6924 return c_parser_omp_var_list_parens (parser
, OMP_CLAUSE_COPYPRIVATE
, list
);
6928 default ( shared | none ) */
6931 c_parser_omp_clause_default (c_parser
*parser
, tree list
)
6933 enum omp_clause_default_kind kind
= OMP_CLAUSE_DEFAULT_UNSPECIFIED
;
6936 if (!c_parser_require (parser
, CPP_OPEN_PAREN
, "expected %<(%>"))
6938 if (c_parser_next_token_is (parser
, CPP_NAME
))
6940 const char *p
= IDENTIFIER_POINTER (c_parser_peek_token (parser
)->value
);
6945 if (strcmp ("none", p
) != 0)
6947 kind
= OMP_CLAUSE_DEFAULT_NONE
;
6951 if (strcmp ("shared", p
) != 0)
6953 kind
= OMP_CLAUSE_DEFAULT_SHARED
;
6960 c_parser_consume_token (parser
);
6965 c_parser_error (parser
, "expected %<none%> or %<shared%>");
6967 c_parser_skip_until_found (parser
, CPP_CLOSE_PAREN
, "expected %<)%>");
6969 if (kind
== OMP_CLAUSE_DEFAULT_UNSPECIFIED
)
6972 check_no_duplicate_clause (list
, OMP_CLAUSE_DEFAULT
, "default");
6973 c
= build_omp_clause (OMP_CLAUSE_DEFAULT
);
6974 OMP_CLAUSE_CHAIN (c
) = list
;
6975 OMP_CLAUSE_DEFAULT_KIND (c
) = kind
;
6981 firstprivate ( variable-list ) */
6984 c_parser_omp_clause_firstprivate (c_parser
*parser
, tree list
)
6986 return c_parser_omp_var_list_parens (parser
, OMP_CLAUSE_FIRSTPRIVATE
, list
);
6990 if ( expression ) */
6993 c_parser_omp_clause_if (c_parser
*parser
, tree list
)
6995 if (c_parser_next_token_is (parser
, CPP_OPEN_PAREN
))
6997 tree t
= c_parser_paren_condition (parser
);
7000 check_no_duplicate_clause (list
, OMP_CLAUSE_IF
, "if");
7002 c
= build_omp_clause (OMP_CLAUSE_IF
);
7003 OMP_CLAUSE_IF_EXPR (c
) = t
;
7004 OMP_CLAUSE_CHAIN (c
) = list
;
7008 c_parser_error (parser
, "expected %<(%>");
7014 lastprivate ( variable-list ) */
7017 c_parser_omp_clause_lastprivate (c_parser
*parser
, tree list
)
7019 return c_parser_omp_var_list_parens (parser
, OMP_CLAUSE_LASTPRIVATE
, list
);
7026 c_parser_omp_clause_nowait (c_parser
*parser ATTRIBUTE_UNUSED
, tree list
)
7030 check_no_duplicate_clause (list
, OMP_CLAUSE_NOWAIT
, "nowait");
7032 c
= build_omp_clause (OMP_CLAUSE_NOWAIT
);
7033 OMP_CLAUSE_CHAIN (c
) = list
;
7038 num_threads ( expression ) */
7041 c_parser_omp_clause_num_threads (c_parser
*parser
, tree list
)
7043 if (c_parser_require (parser
, CPP_OPEN_PAREN
, "expected %<(%>"))
7045 location_t expr_loc
= c_parser_peek_token (parser
)->location
;
7046 tree c
, t
= c_parser_expression (parser
).value
;
7048 c_parser_skip_until_found (parser
, CPP_CLOSE_PAREN
, "expected %<)%>");
7050 if (!INTEGRAL_TYPE_P (TREE_TYPE (t
)))
7052 c_parser_error (parser
, "expected integer expression");
7056 /* Attempt to statically determine when the number isn't positive. */
7057 c
= fold_build2 (LE_EXPR
, boolean_type_node
, t
,
7058 build_int_cst (TREE_TYPE (t
), 0));
7059 if (c
== boolean_true_node
)
7061 warning (0, "%H%<num_threads%> value must be positive", &expr_loc
);
7062 t
= integer_one_node
;
7065 check_no_duplicate_clause (list
, OMP_CLAUSE_NUM_THREADS
, "num_threads");
7067 c
= build_omp_clause (OMP_CLAUSE_NUM_THREADS
);
7068 OMP_CLAUSE_NUM_THREADS_EXPR (c
) = t
;
7069 OMP_CLAUSE_CHAIN (c
) = list
;
7080 c_parser_omp_clause_ordered (c_parser
*parser ATTRIBUTE_UNUSED
, tree list
)
7084 check_no_duplicate_clause (list
, OMP_CLAUSE_ORDERED
, "ordered");
7086 c
= build_omp_clause (OMP_CLAUSE_ORDERED
);
7087 OMP_CLAUSE_CHAIN (c
) = list
;
7092 private ( variable-list ) */
7095 c_parser_omp_clause_private (c_parser
*parser
, tree list
)
7097 return c_parser_omp_var_list_parens (parser
, OMP_CLAUSE_PRIVATE
, list
);
7101 reduction ( reduction-operator : variable-list )
7104 One of: + * - & ^ | && || */
7107 c_parser_omp_clause_reduction (c_parser
*parser
, tree list
)
7109 if (c_parser_require (parser
, CPP_OPEN_PAREN
, "expected %<(%>"))
7111 enum tree_code code
;
7113 switch (c_parser_peek_token (parser
)->type
)
7125 code
= BIT_AND_EXPR
;
7128 code
= BIT_XOR_EXPR
;
7131 code
= BIT_IOR_EXPR
;
7134 code
= TRUTH_ANDIF_EXPR
;
7137 code
= TRUTH_ORIF_EXPR
;
7140 c_parser_error (parser
,
7141 "expected %<+%>, %<*%>, %<-%>, %<&%>, "
7142 "%<^%>, %<|%>, %<&&%>, or %<||%>");
7143 c_parser_skip_until_found (parser
, CPP_CLOSE_PAREN
, 0);
7146 c_parser_consume_token (parser
);
7147 if (c_parser_require (parser
, CPP_COLON
, "expected %<:%>"))
7151 nl
= c_parser_omp_variable_list (parser
, OMP_CLAUSE_REDUCTION
, list
);
7152 for (c
= nl
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
7153 OMP_CLAUSE_REDUCTION_CODE (c
) = code
;
7157 c_parser_skip_until_found (parser
, CPP_CLOSE_PAREN
, "expected %<)%>");
7163 schedule ( schedule-kind )
7164 schedule ( schedule-kind , expression )
7167 static | dynamic | guided | runtime
7171 c_parser_omp_clause_schedule (c_parser
*parser
, tree list
)
7175 if (!c_parser_require (parser
, CPP_OPEN_PAREN
, "expected %<(%>"))
7178 c
= build_omp_clause (OMP_CLAUSE_SCHEDULE
);
7180 if (c_parser_next_token_is (parser
, CPP_NAME
))
7182 tree kind
= c_parser_peek_token (parser
)->value
;
7183 const char *p
= IDENTIFIER_POINTER (kind
);
7188 if (strcmp ("dynamic", p
) != 0)
7190 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_DYNAMIC
;
7194 if (strcmp ("guided", p
) != 0)
7196 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_GUIDED
;
7200 if (strcmp ("runtime", p
) != 0)
7202 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_RUNTIME
;
7209 else if (c_parser_next_token_is_keyword (parser
, RID_STATIC
))
7210 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_STATIC
;
7214 c_parser_consume_token (parser
);
7215 if (c_parser_next_token_is (parser
, CPP_COMMA
))
7218 c_parser_consume_token (parser
);
7220 here
= c_parser_peek_token (parser
)->location
;
7221 t
= c_parser_expr_no_commas (parser
, NULL
).value
;
7223 if (OMP_CLAUSE_SCHEDULE_KIND (c
) == OMP_CLAUSE_SCHEDULE_RUNTIME
)
7224 error ("%Hschedule %<runtime%> does not take "
7225 "a %<chunk_size%> parameter", &here
);
7226 else if (TREE_CODE (TREE_TYPE (t
)) == INTEGER_TYPE
)
7227 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c
) = t
;
7229 c_parser_error (parser
, "expected integer expression");
7231 c_parser_skip_until_found (parser
, CPP_CLOSE_PAREN
, "expected %<)%>");
7234 c_parser_skip_until_found (parser
, CPP_CLOSE_PAREN
,
7235 "expected %<,%> or %<)%>");
7237 check_no_duplicate_clause (list
, OMP_CLAUSE_SCHEDULE
, "schedule");
7238 OMP_CLAUSE_CHAIN (c
) = list
;
7242 c_parser_error (parser
, "invalid schedule kind");
7243 c_parser_skip_until_found (parser
, CPP_CLOSE_PAREN
, 0);
7248 shared ( variable-list ) */
7251 c_parser_omp_clause_shared (c_parser
*parser
, tree list
)
7253 return c_parser_omp_var_list_parens (parser
, OMP_CLAUSE_SHARED
, list
);
7256 /* Parse all OpenMP clauses. The set clauses allowed by the directive
7257 is a bitmask in MASK. Return the list of clauses found; the result
7258 of clause default goes in *pdefault. */
7261 c_parser_omp_all_clauses (c_parser
*parser
, unsigned int mask
,
7264 tree clauses
= NULL
;
7267 while (c_parser_next_token_is_not (parser
, CPP_PRAGMA_EOL
))
7270 pragma_omp_clause c_kind
;
7272 tree prev
= clauses
;
7274 if (!first
&& c_parser_next_token_is (parser
, CPP_COMMA
))
7275 c_parser_consume_token (parser
);
7278 here
= c_parser_peek_token (parser
)->location
;
7279 c_kind
= c_parser_omp_clause_name (parser
);
7283 case PRAGMA_OMP_CLAUSE_COPYIN
:
7284 clauses
= c_parser_omp_clause_copyin (parser
, clauses
);
7287 case PRAGMA_OMP_CLAUSE_COPYPRIVATE
:
7288 clauses
= c_parser_omp_clause_copyprivate (parser
, clauses
);
7289 c_name
= "copyprivate";
7291 case PRAGMA_OMP_CLAUSE_DEFAULT
:
7292 clauses
= c_parser_omp_clause_default (parser
, clauses
);
7295 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE
:
7296 clauses
= c_parser_omp_clause_firstprivate (parser
, clauses
);
7297 c_name
= "firstprivate";
7299 case PRAGMA_OMP_CLAUSE_IF
:
7300 clauses
= c_parser_omp_clause_if (parser
, clauses
);
7303 case PRAGMA_OMP_CLAUSE_LASTPRIVATE
:
7304 clauses
= c_parser_omp_clause_lastprivate (parser
, clauses
);
7305 c_name
= "lastprivate";
7307 case PRAGMA_OMP_CLAUSE_NOWAIT
:
7308 clauses
= c_parser_omp_clause_nowait (parser
, clauses
);
7311 case PRAGMA_OMP_CLAUSE_NUM_THREADS
:
7312 clauses
= c_parser_omp_clause_num_threads (parser
, clauses
);
7313 c_name
= "num_threads";
7315 case PRAGMA_OMP_CLAUSE_ORDERED
:
7316 clauses
= c_parser_omp_clause_ordered (parser
, clauses
);
7319 case PRAGMA_OMP_CLAUSE_PRIVATE
:
7320 clauses
= c_parser_omp_clause_private (parser
, clauses
);
7323 case PRAGMA_OMP_CLAUSE_REDUCTION
:
7324 clauses
= c_parser_omp_clause_reduction (parser
, clauses
);
7325 c_name
= "reduction";
7327 case PRAGMA_OMP_CLAUSE_SCHEDULE
:
7328 clauses
= c_parser_omp_clause_schedule (parser
, clauses
);
7329 c_name
= "schedule";
7331 case PRAGMA_OMP_CLAUSE_SHARED
:
7332 clauses
= c_parser_omp_clause_shared (parser
, clauses
);
7336 c_parser_error (parser
, "expected %<#pragma omp%> clause");
7340 if (((mask
>> c_kind
) & 1) == 0 && !parser
->error
)
7342 /* Remove the invalid clause(s) from the list to avoid
7343 confusing the rest of the compiler. */
7345 error ("%H%qs is not valid for %qs", &here
, c_name
, where
);
7350 c_parser_skip_to_pragma_eol (parser
);
7352 return c_finish_omp_clauses (clauses
);
7359 In practice, we're also interested in adding the statement to an
7360 outer node. So it is convenient if we work around the fact that
7361 c_parser_statement calls add_stmt. */
7364 c_parser_omp_structured_block (c_parser
*parser
)
7366 tree stmt
= push_stmt_list ();
7367 c_parser_statement (parser
);
7368 return pop_stmt_list (stmt
);
7372 # pragma omp atomic new-line
7376 x binop= expr | x++ | ++x | x-- | --x
7378 +, *, -, /, &, ^, |, <<, >>
7380 where x is an lvalue expression with scalar type. */
7383 c_parser_omp_atomic (c_parser
*parser
)
7387 enum tree_code code
;
7389 c_parser_skip_to_pragma_eol (parser
);
7391 lhs
= c_parser_unary_expression (parser
).value
;
7392 switch (TREE_CODE (lhs
))
7396 c_parser_skip_to_end_of_block_or_statement (parser
);
7399 case PREINCREMENT_EXPR
:
7400 case POSTINCREMENT_EXPR
:
7401 lhs
= TREE_OPERAND (lhs
, 0);
7403 rhs
= integer_one_node
;
7406 case PREDECREMENT_EXPR
:
7407 case POSTDECREMENT_EXPR
:
7408 lhs
= TREE_OPERAND (lhs
, 0);
7410 rhs
= integer_one_node
;
7414 switch (c_parser_peek_token (parser
)->type
)
7420 code
= TRUNC_DIV_EXPR
;
7435 code
= BIT_AND_EXPR
;
7438 code
= BIT_IOR_EXPR
;
7441 code
= BIT_XOR_EXPR
;
7444 c_parser_error (parser
,
7445 "invalid operator for %<#pragma omp atomic%>");
7449 c_parser_consume_token (parser
);
7450 rhs
= c_parser_expression (parser
).value
;
7453 stmt
= c_finish_omp_atomic (code
, lhs
, rhs
);
7454 if (stmt
!= error_mark_node
)
7456 c_parser_skip_until_found (parser
, CPP_SEMICOLON
, "expected %<;%>");
7461 # pragma omp barrier new-line
7465 c_parser_omp_barrier (c_parser
*parser
)
7467 c_parser_consume_pragma (parser
);
7468 c_parser_skip_to_pragma_eol (parser
);
7470 c_finish_omp_barrier ();
7474 # pragma omp critical [(name)] new-line
7479 c_parser_omp_critical (c_parser
*parser
)
7481 tree stmt
, name
= NULL
;
7483 if (c_parser_next_token_is (parser
, CPP_OPEN_PAREN
))
7485 c_parser_consume_token (parser
);
7486 if (c_parser_next_token_is (parser
, CPP_NAME
))
7488 name
= c_parser_peek_token (parser
)->value
;
7489 c_parser_consume_token (parser
);
7490 c_parser_require (parser
, CPP_CLOSE_PAREN
, "expected %<)%>");
7493 c_parser_error (parser
, "expected identifier");
7495 else if (c_parser_next_token_is_not (parser
, CPP_PRAGMA_EOL
))
7496 c_parser_error (parser
, "expected %<(%> or end of line");
7497 c_parser_skip_to_pragma_eol (parser
);
7499 stmt
= c_parser_omp_structured_block (parser
);
7500 return c_finish_omp_critical (stmt
, name
);
7504 # pragma omp flush flush-vars[opt] new-line
7507 ( variable-list ) */
7510 c_parser_omp_flush (c_parser
*parser
)
7512 c_parser_consume_pragma (parser
);
7513 if (c_parser_next_token_is (parser
, CPP_OPEN_PAREN
))
7514 c_parser_omp_var_list_parens (parser
, 0, NULL
);
7515 else if (c_parser_next_token_is_not (parser
, CPP_PRAGMA_EOL
))
7516 c_parser_error (parser
, "expected %<(%> or end of line");
7517 c_parser_skip_to_pragma_eol (parser
);
7519 c_finish_omp_flush ();
7522 /* Parse the restricted form of the for statment allowed by OpenMP.
7523 The real trick here is to determine the loop control variable early
7524 so that we can push a new decl if necessary to make it private. */
7527 c_parser_omp_for_loop (c_parser
*parser
)
7529 tree decl
, cond
, incr
, save_break
, save_cont
, body
, init
;
7532 if (!c_parser_next_token_is_keyword (parser
, RID_FOR
))
7534 c_parser_error (parser
, "for statement expected");
7537 loc
= c_parser_peek_token (parser
)->location
;
7538 c_parser_consume_token (parser
);
7540 if (!c_parser_require (parser
, CPP_OPEN_PAREN
, "expected %<(%>"))
7543 /* Parse the initialization declaration or expression. */
7544 if (c_parser_next_token_starts_declspecs (parser
))
7546 c_parser_declaration_or_fndef (parser
, true, true, true, true);
7547 decl
= check_for_loop_decls ();
7552 else if (c_parser_next_token_is (parser
, CPP_NAME
)
7553 && c_parser_peek_2nd_token (parser
)->type
== CPP_EQ
)
7555 decl
= c_parser_postfix_expression (parser
).value
;
7557 c_parser_require (parser
, CPP_EQ
, "expected %<=%>");
7559 init
= c_parser_expr_no_commas (parser
, NULL
).value
;
7560 init
= build_modify_expr (decl
, NOP_EXPR
, init
);
7561 init
= c_process_expr_stmt (init
);
7563 c_parser_skip_until_found (parser
, CPP_SEMICOLON
, "expected %<;%>");
7568 /* Parse the loop condition. */
7570 if (c_parser_next_token_is_not (parser
, CPP_SEMICOLON
))
7572 cond
= c_parser_expression_conv (parser
).value
;
7573 cond
= c_objc_common_truthvalue_conversion (cond
);
7574 if (CAN_HAVE_LOCATION_P (cond
))
7575 SET_EXPR_LOCATION (cond
, input_location
);
7577 c_parser_skip_until_found (parser
, CPP_SEMICOLON
, "expected %<;%>");
7579 /* Parse the increment expression. */
7581 if (c_parser_next_token_is_not (parser
, CPP_CLOSE_PAREN
))
7582 incr
= c_process_expr_stmt (c_parser_expression (parser
).value
);
7583 c_parser_skip_until_found (parser
, CPP_CLOSE_PAREN
, "expected %<)%>");
7586 save_break
= c_break_label
;
7587 c_break_label
= size_one_node
;
7588 save_cont
= c_cont_label
;
7589 c_cont_label
= NULL_TREE
;
7590 body
= push_stmt_list ();
7592 add_stmt (c_parser_c99_block_statement (parser
));
7594 add_stmt (build1 (LABEL_EXPR
, void_type_node
, c_cont_label
));
7596 body
= pop_stmt_list (body
);
7597 c_break_label
= save_break
;
7598 c_cont_label
= save_cont
;
7600 /* Only bother calling c_finish_omp_for if we havn't already generated
7601 an error from the initialization parsing. */
7602 if (decl
!= NULL
&& decl
!= error_mark_node
&& init
!= error_mark_node
)
7603 return c_finish_omp_for (loc
, decl
, init
, cond
, incr
, body
, NULL
);
7607 c_parser_error (parser
, "expected iteration declaration or initialization");
7608 c_parser_skip_until_found (parser
, CPP_CLOSE_PAREN
, "expected %<)%>");
7609 decl
= init
= cond
= incr
= NULL_TREE
;
7614 #pragma omp for for-clause[optseq] new-line
7618 #define OMP_FOR_CLAUSE_MASK \
7619 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
7620 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
7621 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
7622 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
7623 | (1u << PRAGMA_OMP_CLAUSE_ORDERED) \
7624 | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE) \
7625 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
7628 c_parser_omp_for (c_parser
*parser
)
7630 tree block
, clauses
, ret
;
7632 clauses
= c_parser_omp_all_clauses (parser
, OMP_FOR_CLAUSE_MASK
,
7635 block
= c_begin_compound_stmt (true);
7636 ret
= c_parser_omp_for_loop (parser
);
7638 OMP_FOR_CLAUSES (ret
) = clauses
;
7639 block
= c_end_compound_stmt (block
, true);
7646 # pragma omp master new-line
7651 c_parser_omp_master (c_parser
*parser
)
7653 c_parser_skip_to_pragma_eol (parser
);
7654 return c_finish_omp_master (c_parser_omp_structured_block (parser
));
7658 # pragma omp ordered new-line
7663 c_parser_omp_ordered (c_parser
*parser
)
7665 c_parser_skip_to_pragma_eol (parser
);
7666 return c_finish_omp_ordered (c_parser_omp_structured_block (parser
));
7672 { section-sequence }
7675 section-directive[opt] structured-block
7676 section-sequence section-directive structured-block */
7679 c_parser_omp_sections_scope (c_parser
*parser
)
7682 bool error_suppress
= false;
7685 if (!c_parser_require (parser
, CPP_OPEN_BRACE
, "expected %<{%>"))
7687 /* Avoid skipping until the end of the block. */
7688 parser
->error
= false;
7692 stmt
= push_stmt_list ();
7694 loc
= c_parser_peek_token (parser
)->location
;
7695 if (c_parser_peek_token (parser
)->pragma_kind
!= PRAGMA_OMP_SECTION
)
7697 substmt
= push_stmt_list ();
7701 c_parser_statement (parser
);
7703 if (c_parser_peek_token (parser
)->pragma_kind
== PRAGMA_OMP_SECTION
)
7705 if (c_parser_next_token_is (parser
, CPP_CLOSE_BRACE
))
7707 if (c_parser_next_token_is (parser
, CPP_EOF
))
7711 substmt
= pop_stmt_list (substmt
);
7712 substmt
= build1 (OMP_SECTION
, void_type_node
, substmt
);
7713 SET_EXPR_LOCATION (substmt
, loc
);
7719 if (c_parser_next_token_is (parser
, CPP_CLOSE_BRACE
))
7721 if (c_parser_next_token_is (parser
, CPP_EOF
))
7724 loc
= c_parser_peek_token (parser
)->location
;
7725 if (c_parser_peek_token (parser
)->pragma_kind
== PRAGMA_OMP_SECTION
)
7727 c_parser_consume_pragma (parser
);
7728 c_parser_skip_to_pragma_eol (parser
);
7729 error_suppress
= false;
7731 else if (!error_suppress
)
7733 error ("%Hexpected %<#pragma omp section%> or %<}%>",
7735 error_suppress
= true;
7738 substmt
= c_parser_omp_structured_block (parser
);
7739 substmt
= build1 (OMP_SECTION
, void_type_node
, substmt
);
7740 SET_EXPR_LOCATION (substmt
, loc
);
7743 c_parser_skip_until_found (parser
, CPP_CLOSE_BRACE
,
7744 "expected %<#pragma omp section%> or %<}%>");
7746 substmt
= pop_stmt_list (stmt
);
7748 stmt
= make_node (OMP_SECTIONS
);
7749 TREE_TYPE (stmt
) = void_type_node
;
7750 OMP_SECTIONS_BODY (stmt
) = substmt
;
7752 return add_stmt (stmt
);
7756 # pragma omp sections sections-clause[optseq] newline
7760 #define OMP_SECTIONS_CLAUSE_MASK \
7761 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
7762 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
7763 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
7764 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
7765 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
7768 c_parser_omp_sections (c_parser
*parser
)
7770 tree block
, clauses
, ret
;
7772 clauses
= c_parser_omp_all_clauses (parser
, OMP_SECTIONS_CLAUSE_MASK
,
7773 "#pragma omp sections");
7775 block
= c_begin_compound_stmt (true);
7776 ret
= c_parser_omp_sections_scope (parser
);
7778 OMP_SECTIONS_CLAUSES (ret
) = clauses
;
7779 block
= c_end_compound_stmt (block
, true);
7786 # pragma parallel parallel-clause new-line
7787 # pragma parallel for parallel-for-clause new-line
7788 # pragma parallel sections parallel-sections-clause new-line
7791 #define OMP_PARALLEL_CLAUSE_MASK \
7792 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
7793 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
7794 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
7795 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
7796 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
7797 | (1u << PRAGMA_OMP_CLAUSE_COPYIN) \
7798 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
7799 | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
7802 c_parser_omp_parallel (c_parser
*parser
)
7804 enum pragma_kind p_kind
= PRAGMA_OMP_PARALLEL
;
7805 const char *p_name
= "#pragma omp parallel";
7806 tree stmt
, clauses
, par_clause
, ws_clause
, block
;
7807 unsigned int mask
= OMP_PARALLEL_CLAUSE_MASK
;
7809 if (c_parser_next_token_is_keyword (parser
, RID_FOR
))
7811 c_parser_consume_token (parser
);
7812 p_kind
= PRAGMA_OMP_PARALLEL_FOR
;
7813 p_name
= "#pragma omp parallel for";
7814 mask
|= OMP_FOR_CLAUSE_MASK
;
7815 mask
&= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT
);
7817 else if (c_parser_next_token_is (parser
, CPP_NAME
))
7819 const char *p
= IDENTIFIER_POINTER (c_parser_peek_token (parser
)->value
);
7820 if (strcmp (p
, "sections") == 0)
7822 c_parser_consume_token (parser
);
7823 p_kind
= PRAGMA_OMP_PARALLEL_SECTIONS
;
7824 p_name
= "#pragma omp parallel sections";
7825 mask
|= OMP_SECTIONS_CLAUSE_MASK
;
7826 mask
&= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT
);
7830 clauses
= c_parser_omp_all_clauses (parser
, mask
, p_name
);
7834 case PRAGMA_OMP_PARALLEL
:
7835 block
= c_begin_omp_parallel ();
7836 c_parser_statement (parser
);
7837 stmt
= c_finish_omp_parallel (clauses
, block
);
7840 case PRAGMA_OMP_PARALLEL_FOR
:
7841 block
= c_begin_omp_parallel ();
7842 c_split_parallel_clauses (clauses
, &par_clause
, &ws_clause
);
7843 stmt
= c_parser_omp_for_loop (parser
);
7845 OMP_FOR_CLAUSES (stmt
) = ws_clause
;
7846 stmt
= c_finish_omp_parallel (par_clause
, block
);
7847 OMP_PARALLEL_COMBINED (stmt
) = 1;
7850 case PRAGMA_OMP_PARALLEL_SECTIONS
:
7851 block
= c_begin_omp_parallel ();
7852 c_split_parallel_clauses (clauses
, &par_clause
, &ws_clause
);
7853 stmt
= c_parser_omp_sections_scope (parser
);
7855 OMP_SECTIONS_CLAUSES (stmt
) = ws_clause
;
7856 stmt
= c_finish_omp_parallel (par_clause
, block
);
7857 OMP_PARALLEL_COMBINED (stmt
) = 1;
7868 # pragma omp single single-clause[optseq] new-line
7872 #define OMP_SINGLE_CLAUSE_MASK \
7873 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
7874 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
7875 | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
7876 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
7879 c_parser_omp_single (c_parser
*parser
)
7881 tree stmt
= make_node (OMP_SINGLE
);
7882 TREE_TYPE (stmt
) = void_type_node
;
7884 OMP_SINGLE_CLAUSES (stmt
)
7885 = c_parser_omp_all_clauses (parser
, OMP_SINGLE_CLAUSE_MASK
,
7886 "#pragma omp single");
7887 OMP_SINGLE_BODY (stmt
) = c_parser_omp_structured_block (parser
);
7889 return add_stmt (stmt
);
7893 /* Main entry point to parsing most OpenMP pragmas. */
7896 c_parser_omp_construct (c_parser
*parser
)
7898 enum pragma_kind p_kind
;
7902 loc
= c_parser_peek_token (parser
)->location
;
7903 p_kind
= c_parser_peek_token (parser
)->pragma_kind
;
7904 c_parser_consume_pragma (parser
);
7906 /* For all constructs below except #pragma omp atomic
7907 MUST_NOT_THROW catch handlers are needed when exceptions
7909 if (p_kind
!= PRAGMA_OMP_ATOMIC
)
7910 c_maybe_initialize_eh ();
7914 case PRAGMA_OMP_ATOMIC
:
7915 c_parser_omp_atomic (parser
);
7917 case PRAGMA_OMP_CRITICAL
:
7918 stmt
= c_parser_omp_critical (parser
);
7920 case PRAGMA_OMP_FOR
:
7921 stmt
= c_parser_omp_for (parser
);
7923 case PRAGMA_OMP_MASTER
:
7924 stmt
= c_parser_omp_master (parser
);
7926 case PRAGMA_OMP_ORDERED
:
7927 stmt
= c_parser_omp_ordered (parser
);
7929 case PRAGMA_OMP_PARALLEL
:
7930 stmt
= c_parser_omp_parallel (parser
);
7932 case PRAGMA_OMP_SECTIONS
:
7933 stmt
= c_parser_omp_sections (parser
);
7935 case PRAGMA_OMP_SINGLE
:
7936 stmt
= c_parser_omp_single (parser
);
7943 SET_EXPR_LOCATION (stmt
, loc
);
7948 # pragma omp threadprivate (variable-list) */
7951 c_parser_omp_threadprivate (c_parser
*parser
)
7955 c_parser_consume_pragma (parser
);
7956 vars
= c_parser_omp_var_list_parens (parser
, 0, NULL
);
7958 /* Mark every variable in VARS to be assigned thread local storage. */
7959 for (t
= vars
; t
; t
= TREE_CHAIN (t
))
7961 tree v
= TREE_PURPOSE (t
);
7963 /* If V had already been marked threadprivate, it doesn't matter
7964 whether it had been used prior to this point. */
7965 if (TREE_USED (v
) && !C_DECL_THREADPRIVATE_P (v
))
7966 error ("%qE declared %<threadprivate%> after first use", v
);
7967 else if (! TREE_STATIC (v
) && ! DECL_EXTERNAL (v
))
7968 error ("automatic variable %qE cannot be %<threadprivate%>", v
);
7969 else if (! COMPLETE_TYPE_P (TREE_TYPE (v
)))
7970 error ("%<threadprivate%> %qE has incomplete type", v
);
7973 if (! DECL_THREAD_LOCAL_P (v
))
7975 DECL_TLS_MODEL (v
) = decl_default_tls_model (v
);
7976 /* If rtl has been already set for this var, call
7977 make_decl_rtl once again, so that encode_section_info
7978 has a chance to look at the new decl flags. */
7979 if (DECL_RTL_SET_P (v
))
7982 C_DECL_THREADPRIVATE_P (v
) = 1;
7986 c_parser_skip_to_pragma_eol (parser
);
7990 /* Parse a single source file. */
7995 /* Use local storage to begin. If the first token is a pragma, parse it.
7996 If it is #pragma GCC pch_preprocess, then this will load a PCH file
7997 which will cause garbage collection. */
8000 memset (&tparser
, 0, sizeof tparser
);
8001 the_parser
= &tparser
;
8003 if (c_parser_peek_token (&tparser
)->pragma_kind
== PRAGMA_GCC_PCH_PREPROCESS
)
8004 c_parser_pragma_pch_preprocess (&tparser
);
8006 the_parser
= GGC_NEW (c_parser
);
8007 *the_parser
= tparser
;
8009 c_parser_translation_unit (the_parser
);
8013 #include "gt-c-parser.h"