]> gcc.gnu.org Git - gcc.git/blame - gcc/c-parser.c
c-tree.h (enum c_typespec_kind): Add ctsk_none.
[gcc.git] / gcc / c-parser.c
CommitLineData
27bf414c
JM
1/* Parser for C and Objective-C.
2 Copyright (C) 1987, 1988, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
ebfbbdc5 3 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2007, 2008, 2009, 2010
5df27e4a 4 Free Software Foundation, Inc.
27bf414c
JM
5
6 Parser actions based on the old Bison parser; structure somewhat
7 influenced by and fragments based on the C++ parser.
8
9This file is part of GCC.
10
11GCC is free software; you can redistribute it and/or modify it under
12the terms of the GNU General Public License as published by the Free
9dcd6f09 13Software Foundation; either version 3, or (at your option) any later
27bf414c
JM
14version.
15
16GCC is distributed in the hope that it will be useful, but WITHOUT ANY
17WARRANTY; without even the implied warranty of MERCHANTABILITY or
18FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19for more details.
20
21You should have received a copy of the GNU General Public License
9dcd6f09
NC
22along with GCC; see the file COPYING3. If not see
23<http://www.gnu.org/licenses/>. */
27bf414c
JM
24
25/* TODO:
26
27 Make sure all relevant comments, and all relevant code from all
28 actions, brought over from old parser. Verify exact correspondence
29 of syntax accepted.
30
31 Add testcases covering every input symbol in every state in old and
32 new parsers.
33
34 Include full syntax for GNU C, including erroneous cases accepted
35 with error messages, in syntax productions in comments.
36
37 Make more diagnostics in the front end generally take an explicit
38 location rather than implicitly using input_location. */
39
40#include "config.h"
41#include "system.h"
42#include "coretypes.h"
f4ce02c5 43#include "tm.h" /* For rtl.h: needs enum reg_class. */
27bf414c
JM
44#include "tree.h"
45#include "langhooks.h"
46#include "input.h"
47#include "cpplib.h"
48#include "timevar.h"
39dabefd 49#include "c-family/c-pragma.h"
27bf414c
JM
50#include "c-tree.h"
51#include "flags.h"
52#include "output.h"
53#include "toplev.h"
54#include "ggc.h"
39dabefd 55#include "c-family/c-common.h"
bc4071dd
RH
56#include "vec.h"
57#include "target.h"
474eccc6 58#include "cgraph.h"
68a607d8 59#include "plugin.h"
27bf414c
JM
60
61\f
27bf414c
JM
62/* Initialization routine for this file. */
63
64void
65c_parse_init (void)
66{
67 /* The only initialization required is of the reserved word
68 identifiers. */
69 unsigned int i;
70 tree id;
eea1139b 71 int mask = 0;
27bf414c 72
36c5e70a
BE
73 /* Make sure RID_MAX hasn't grown past the 8 bits used to hold the keyword in
74 the c_token structure. */
75 gcc_assert (RID_MAX <= 255);
76
eea1139b
ILT
77 mask |= D_CXXONLY;
78 if (!flag_isoc99)
79 mask |= D_C99;
80 if (flag_no_asm)
81 {
82 mask |= D_ASM | D_EXT;
83 if (!flag_isoc99)
84 mask |= D_EXT89;
85 }
27bf414c 86 if (!c_dialect_objc ())
eea1139b 87 mask |= D_OBJC | D_CXX_OBJC;
27bf414c 88
a9429e29 89 ridpointers = ggc_alloc_cleared_vec_tree ((int) RID_MAX);
eea1139b 90 for (i = 0; i < num_c_common_reswords; i++)
27bf414c
JM
91 {
92 /* If a keyword is disabled, do not enter it into the table
93 and so create a canonical spelling that isn't a keyword. */
eea1139b
ILT
94 if (c_common_reswords[i].disable & mask)
95 {
96 if (warn_cxx_compat
97 && (c_common_reswords[i].disable & D_CXXWARN))
98 {
99 id = get_identifier (c_common_reswords[i].word);
100 C_SET_RID_CODE (id, RID_CXX_COMPAT_WARN);
101 C_IS_RESERVED_WORD (id) = 1;
102 }
103 continue;
104 }
27bf414c 105
eea1139b
ILT
106 id = get_identifier (c_common_reswords[i].word);
107 C_SET_RID_CODE (id, c_common_reswords[i].rid);
27bf414c 108 C_IS_RESERVED_WORD (id) = 1;
eea1139b 109 ridpointers [(int) c_common_reswords[i].rid] = id;
27bf414c
JM
110 }
111}
112\f
113/* The C lexer intermediates between the lexer in cpplib and c-lex.c
114 and the C parser. Unlike the C++ lexer, the parser structure
115 stores the lexer information instead of using a separate structure.
116 Identifiers are separated into ordinary identifiers, type names,
117 keywords and some other Objective-C types of identifiers, and some
118 look-ahead is maintained.
119
120 ??? It might be a good idea to lex the whole file up front (as for
121 C++). It would then be possible to share more of the C and C++
122 lexer code, if desired. */
123
124/* The following local token type is used. */
125
126/* A keyword. */
127#define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
128
27bf414c
JM
129/* More information about the type of a CPP_NAME token. */
130typedef enum c_id_kind {
131 /* An ordinary identifier. */
132 C_ID_ID,
133 /* An identifier declared as a typedef name. */
134 C_ID_TYPENAME,
135 /* An identifier declared as an Objective-C class name. */
136 C_ID_CLASSNAME,
36c5e70a
BE
137 /* An address space identifier. */
138 C_ID_ADDRSPACE,
27bf414c
JM
139 /* Not an identifier. */
140 C_ID_NONE
141} c_id_kind;
142
143/* A single C token after string literal concatenation and conversion
144 of preprocessing tokens to tokens. */
d1b38208 145typedef struct GTY (()) c_token {
27bf414c
JM
146 /* The kind of token. */
147 ENUM_BITFIELD (cpp_ttype) type : 8;
148 /* If this token is a CPP_NAME, this value indicates whether also
149 declared as some kind of type. Otherwise, it is C_ID_NONE. */
150 ENUM_BITFIELD (c_id_kind) id_kind : 8;
151 /* If this token is a keyword, this value indicates which keyword.
152 Otherwise, this value is RID_MAX. */
153 ENUM_BITFIELD (rid) keyword : 8;
bc4071dd
RH
154 /* If this token is a CPP_PRAGMA, this indicates the pragma that
155 was seen. Otherwise it is PRAGMA_NONE. */
13fa1171 156 ENUM_BITFIELD (pragma_kind) pragma_kind : 8;
27bf414c
JM
157 /* The location at which this token was found. */
158 location_t location;
8415f317
NF
159 /* The value associated with this token, if any. */
160 tree value;
27bf414c
JM
161} c_token;
162
163/* A parser structure recording information about the state and
164 context of parsing. Includes lexer information with up to two
165 tokens of look-ahead; more are not needed for C. */
d1b38208 166typedef struct GTY(()) c_parser {
27bf414c
JM
167 /* The look-ahead tokens. */
168 c_token tokens[2];
169 /* How many look-ahead tokens are available (0, 1 or 2). */
170 short tokens_avail;
171 /* True if a syntax error is being recovered from; false otherwise.
172 c_parser_error sets this flag. It should clear this flag when
173 enough tokens have been consumed to recover from the error. */
174 BOOL_BITFIELD error : 1;
bc4071dd
RH
175 /* True if we're processing a pragma, and shouldn't automatically
176 consume CPP_PRAGMA_EOL. */
177 BOOL_BITFIELD in_pragma : 1;
b4b56033
MLI
178 /* True if we're parsing the outermost block of an if statement. */
179 BOOL_BITFIELD in_if_block : 1;
46c2514e
TT
180 /* True if we want to lex an untranslated string. */
181 BOOL_BITFIELD lex_untranslated_string : 1;
1973201f 182
0bacb8c7 183 /* Objective-C specific parser/lexer information. */
1973201f
NP
184
185 /* True if we are in a context where the Objective-C "PQ" keywords
186 are considered keywords. */
0bacb8c7 187 BOOL_BITFIELD objc_pq_context : 1;
f05b9d93
NP
188 /* True if we are parsing a (potential) Objective-C foreach
189 statement. This is set to true after we parsed 'for (' and while
190 we wait for 'in' or ';' to decide if it's a standard C for loop or an
191 Objective-C foreach loop. */
192 BOOL_BITFIELD objc_could_be_foreach_context : 1;
0bacb8c7
TT
193 /* The following flag is needed to contextualize Objective-C lexical
194 analysis. In some cases (e.g., 'int NSObject;'), it is
195 undesirable to bind an identifier to an Objective-C class, even
196 if a class with that name exists. */
197 BOOL_BITFIELD objc_need_raw_identifier : 1;
668ea4b1
IS
198 /* True if we are in a context where the Objective-C "Property attribute"
199 keywords are valid. */
200 BOOL_BITFIELD objc_property_attr_context : 1;
27bf414c
JM
201} c_parser;
202
bc4071dd
RH
203
204/* The actual parser and external interface. ??? Does this need to be
205 garbage-collected? */
206
207static GTY (()) c_parser *the_parser;
208
27bf414c
JM
209/* Read in and lex a single token, storing it in *TOKEN. */
210
211static void
0bacb8c7 212c_lex_one_token (c_parser *parser, c_token *token)
27bf414c
JM
213{
214 timevar_push (TV_LEX);
bc4071dd 215
46c2514e
TT
216 token->type = c_lex_with_flags (&token->value, &token->location, NULL,
217 (parser->lex_untranslated_string
218 ? C_LEX_STRING_NO_TRANSLATE : 0));
bc4071dd
RH
219 token->id_kind = C_ID_NONE;
220 token->keyword = RID_MAX;
221 token->pragma_kind = PRAGMA_NONE;
bc4071dd 222
27bf414c
JM
223 switch (token->type)
224 {
225 case CPP_NAME:
27bf414c
JM
226 {
227 tree decl;
228
0bacb8c7
TT
229 bool objc_force_identifier = parser->objc_need_raw_identifier;
230 if (c_dialect_objc ())
231 parser->objc_need_raw_identifier = false;
27bf414c
JM
232
233 if (C_IS_RESERVED_WORD (token->value))
234 {
235 enum rid rid_code = C_RID_CODE (token->value);
236
eea1139b
ILT
237 if (rid_code == RID_CXX_COMPAT_WARN)
238 {
3ba09659
AH
239 warning_at (token->location,
240 OPT_Wc___compat,
88388a52
JM
241 "identifier %qE conflicts with C++ keyword",
242 token->value);
eea1139b 243 }
36c5e70a
BE
244 else if (rid_code >= RID_FIRST_ADDR_SPACE
245 && rid_code <= RID_LAST_ADDR_SPACE)
246 {
247 token->id_kind = C_ID_ADDRSPACE;
248 token->keyword = rid_code;
249 break;
250 }
1973201f 251 else if (c_dialect_objc () && OBJC_IS_PQ_KEYWORD (rid_code))
27bf414c 252 {
1973201f
NP
253 /* We found an Objective-C "pq" keyword (in, out,
254 inout, bycopy, byref, oneway). They need special
255 care because the interpretation depends on the
256 context.
257 */
258 if (parser->objc_pq_context)
27bf414c 259 {
27bf414c
JM
260 token->type = CPP_KEYWORD;
261 token->keyword = rid_code;
262 break;
263 }
f05b9d93
NP
264 else if (parser->objc_could_be_foreach_context
265 && rid_code == RID_IN)
266 {
267 /* We are in Objective-C, inside a (potential)
268 foreach context (which means after having
269 parsed 'for (', but before having parsed ';'),
270 and we found 'in'. We consider it the keyword
271 which terminates the declaration at the
272 beginning of a foreach-statement. Note that
273 this means you can't use 'in' for anything else
274 in that context; in particular, in Objective-C
275 you can't use 'in' as the name of the running
276 variable in a C for loop. We could potentially
277 try to add code here to disambiguate, but it
278 seems a reasonable limitation.
279 */
280 token->type = CPP_KEYWORD;
281 token->keyword = rid_code;
282 break;
283 }
1973201f
NP
284 /* Else, "pq" keywords outside of the "pq" context are
285 not keywords, and we fall through to the code for
286 normal tokens.
287 */
288 }
668ea4b1
IS
289 else if (c_dialect_objc () && OBJC_IS_PATTR_KEYWORD (rid_code))
290 {
291 /* We found an Objective-C "property attribute" keyword
292 (readonly, copies, getter, setter, ivar). These are
293 only valid in the property context. */
294 if (parser->objc_property_attr_context)
295 {
296 token->type = CPP_KEYWORD;
297 token->keyword = rid_code;
298 break;
299 }
300 /* Else they are not special keywords.
301 */
302 }
1973201f
NP
303 else if (c_dialect_objc ()
304 && (OBJC_IS_AT_KEYWORD (rid_code)
305 || OBJC_IS_CXX_KEYWORD (rid_code)))
306 {
307 /* We found one of the Objective-C "@" keywords (defs,
308 selector, synchronized, etc) or one of the
309 Objective-C "cxx" keywords (class, private,
310 protected, public, try, catch, throw) without a
311 preceding '@' sign. Do nothing and fall through to
312 the code for normal tokens (in C++ we would still
313 consider the CXX ones keywords, but not in C).
314 */
315 ;
27bf414c
JM
316 }
317 else
318 {
27bf414c
JM
319 token->type = CPP_KEYWORD;
320 token->keyword = rid_code;
321 break;
322 }
323 }
324
325 decl = lookup_name (token->value);
326 if (decl)
327 {
328 if (TREE_CODE (decl) == TYPE_DECL)
329 {
330 token->id_kind = C_ID_TYPENAME;
331 break;
332 }
333 }
334 else if (c_dialect_objc ())
335 {
336 tree objc_interface_decl = objc_is_class_name (token->value);
337 /* Objective-C class names are in the same namespace as
338 variables and typedefs, and hence are shadowed by local
339 declarations. */
340 if (objc_interface_decl
341 && (global_bindings_p ()
342 || (!objc_force_identifier && !decl)))
343 {
344 token->value = objc_interface_decl;
345 token->id_kind = C_ID_CLASSNAME;
346 break;
347 }
348 }
bc4071dd 349 token->id_kind = C_ID_ID;
27bf414c 350 }
27bf414c
JM
351 break;
352 case CPP_AT_NAME:
353 /* This only happens in Objective-C; it must be a keyword. */
354 token->type = CPP_KEYWORD;
49b91f05
NP
355 switch (C_RID_CODE (token->value))
356 {
357 /* Replace 'class' with '@class', 'private' with '@private',
358 etc. This prevents confusion with the C++ keyword
359 'class', and makes the tokens consistent with other
360 Objective-C 'AT' keywords. For example '@class' is
361 reported as RID_AT_CLASS which is consistent with
362 '@synchronized', which is reported as
363 RID_AT_SYNCHRONIZED.
364 */
365 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
366 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
367 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
368 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
369 case RID_THROW: token->keyword = RID_AT_THROW; break;
370 case RID_TRY: token->keyword = RID_AT_TRY; break;
371 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
372 default: token->keyword = C_RID_CODE (token->value);
373 }
27bf414c
JM
374 break;
375 case CPP_COLON:
376 case CPP_COMMA:
377 case CPP_CLOSE_PAREN:
378 case CPP_SEMICOLON:
379 /* These tokens may affect the interpretation of any identifiers
380 following, if doing Objective-C. */
0bacb8c7
TT
381 if (c_dialect_objc ())
382 parser->objc_need_raw_identifier = false;
bc4071dd
RH
383 break;
384 case CPP_PRAGMA:
385 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
d75d71e0 386 token->pragma_kind = (enum pragma_kind) TREE_INT_CST_LOW (token->value);
bc4071dd 387 token->value = NULL;
27bf414c
JM
388 break;
389 default:
27bf414c
JM
390 break;
391 }
392 timevar_pop (TV_LEX);
393}
394
395/* Return a pointer to the next token from PARSER, reading it in if
396 necessary. */
397
398static inline c_token *
399c_parser_peek_token (c_parser *parser)
400{
401 if (parser->tokens_avail == 0)
402 {
0bacb8c7 403 c_lex_one_token (parser, &parser->tokens[0]);
27bf414c
JM
404 parser->tokens_avail = 1;
405 }
406 return &parser->tokens[0];
407}
408
409/* Return true if the next token from PARSER has the indicated
410 TYPE. */
411
412static inline bool
413c_parser_next_token_is (c_parser *parser, enum cpp_ttype type)
414{
415 return c_parser_peek_token (parser)->type == type;
416}
417
418/* Return true if the next token from PARSER does not have the
419 indicated TYPE. */
420
421static inline bool
422c_parser_next_token_is_not (c_parser *parser, enum cpp_ttype type)
423{
424 return !c_parser_next_token_is (parser, type);
425}
426
427/* Return true if the next token from PARSER is the indicated
428 KEYWORD. */
429
430static inline bool
431c_parser_next_token_is_keyword (c_parser *parser, enum rid keyword)
432{
dbc518f0 433 return c_parser_peek_token (parser)->keyword == keyword;
27bf414c
JM
434}
435
436/* Return true if TOKEN can start a type name,
437 false otherwise. */
438static bool
439c_token_starts_typename (c_token *token)
440{
441 switch (token->type)
442 {
443 case CPP_NAME:
444 switch (token->id_kind)
445 {
446 case C_ID_ID:
447 return false;
36c5e70a
BE
448 case C_ID_ADDRSPACE:
449 return true;
27bf414c
JM
450 case C_ID_TYPENAME:
451 return true;
452 case C_ID_CLASSNAME:
453 gcc_assert (c_dialect_objc ());
454 return true;
455 default:
456 gcc_unreachable ();
457 }
458 case CPP_KEYWORD:
459 switch (token->keyword)
460 {
461 case RID_UNSIGNED:
462 case RID_LONG:
a6766312 463 case RID_INT128:
27bf414c
JM
464 case RID_SHORT:
465 case RID_SIGNED:
466 case RID_COMPLEX:
467 case RID_INT:
468 case RID_CHAR:
469 case RID_FLOAT:
470 case RID_DOUBLE:
471 case RID_VOID:
9a8ce21f
JG
472 case RID_DFLOAT32:
473 case RID_DFLOAT64:
474 case RID_DFLOAT128:
27bf414c
JM
475 case RID_BOOL:
476 case RID_ENUM:
477 case RID_STRUCT:
478 case RID_UNION:
479 case RID_TYPEOF:
480 case RID_CONST:
481 case RID_VOLATILE:
482 case RID_RESTRICT:
483 case RID_ATTRIBUTE:
ab22c1fa
CF
484 case RID_FRACT:
485 case RID_ACCUM:
486 case RID_SAT:
27bf414c
JM
487 return true;
488 default:
489 return false;
490 }
491 case CPP_LESS:
492 if (c_dialect_objc ())
493 return true;
494 return false;
495 default:
496 return false;
497 }
498}
499
500/* Return true if the next token from PARSER can start a type name,
501 false otherwise. */
502static inline bool
503c_parser_next_token_starts_typename (c_parser *parser)
504{
505 c_token *token = c_parser_peek_token (parser);
506 return c_token_starts_typename (token);
507}
508
509/* Return true if TOKEN can start declaration specifiers, false
510 otherwise. */
511static bool
512c_token_starts_declspecs (c_token *token)
513{
514 switch (token->type)
515 {
516 case CPP_NAME:
517 switch (token->id_kind)
518 {
519 case C_ID_ID:
520 return false;
36c5e70a
BE
521 case C_ID_ADDRSPACE:
522 return true;
27bf414c
JM
523 case C_ID_TYPENAME:
524 return true;
525 case C_ID_CLASSNAME:
526 gcc_assert (c_dialect_objc ());
527 return true;
528 default:
529 gcc_unreachable ();
530 }
531 case CPP_KEYWORD:
532 switch (token->keyword)
533 {
534 case RID_STATIC:
535 case RID_EXTERN:
536 case RID_REGISTER:
537 case RID_TYPEDEF:
538 case RID_INLINE:
539 case RID_AUTO:
540 case RID_THREAD:
541 case RID_UNSIGNED:
542 case RID_LONG:
a6766312 543 case RID_INT128:
27bf414c
JM
544 case RID_SHORT:
545 case RID_SIGNED:
546 case RID_COMPLEX:
547 case RID_INT:
548 case RID_CHAR:
549 case RID_FLOAT:
550 case RID_DOUBLE:
551 case RID_VOID:
9a8ce21f
JG
552 case RID_DFLOAT32:
553 case RID_DFLOAT64:
554 case RID_DFLOAT128:
27bf414c
JM
555 case RID_BOOL:
556 case RID_ENUM:
557 case RID_STRUCT:
558 case RID_UNION:
559 case RID_TYPEOF:
560 case RID_CONST:
561 case RID_VOLATILE:
562 case RID_RESTRICT:
563 case RID_ATTRIBUTE:
ab22c1fa
CF
564 case RID_FRACT:
565 case RID_ACCUM:
566 case RID_SAT:
27bf414c
JM
567 return true;
568 default:
569 return false;
570 }
571 case CPP_LESS:
572 if (c_dialect_objc ())
573 return true;
574 return false;
575 default:
576 return false;
577 }
578}
579
32912286
JM
580
581/* Return true if TOKEN can start declaration specifiers or a static
582 assertion, false otherwise. */
583static bool
584c_token_starts_declaration (c_token *token)
585{
586 if (c_token_starts_declspecs (token)
587 || token->keyword == RID_STATIC_ASSERT)
588 return true;
589 else
590 return false;
591}
592
668ea4b1
IS
593static c_token *c_parser_peek_2nd_token (c_parser *parser);
594
27bf414c
JM
595/* Return true if the next token from PARSER can start declaration
596 specifiers, false otherwise. */
597static inline bool
598c_parser_next_token_starts_declspecs (c_parser *parser)
599{
600 c_token *token = c_parser_peek_token (parser);
bede2adc
NP
601
602 /* In Objective-C, a classname normally starts a declspecs unless it
603 is immediately followed by a dot. In that case, it is the
604 Objective-C 2.0 "dot-syntax" for class objects, ie, calls the
605 setter/getter on the class. c_token_starts_declspecs() can't
606 differentiate between the two cases because it only checks the
607 current token, so we have a special check here. */
608 if (c_dialect_objc ()
609 && token->type == CPP_NAME
610 && token->id_kind == C_ID_CLASSNAME
611 && c_parser_peek_2nd_token (parser)->type == CPP_DOT)
612 return false;
613
27bf414c
JM
614 return c_token_starts_declspecs (token);
615}
616
32912286
JM
617/* Return true if the next token from PARSER can start declaration
618 specifiers or a static assertion, false otherwise. */
619static inline bool
620c_parser_next_token_starts_declaration (c_parser *parser)
621{
622 c_token *token = c_parser_peek_token (parser);
bede2adc
NP
623
624 /* Same as above. */
625 if (c_dialect_objc ()
626 && token->type == CPP_NAME
627 && token->id_kind == C_ID_CLASSNAME
628 && c_parser_peek_2nd_token (parser)->type == CPP_DOT)
629 return false;
630
32912286
JM
631 return c_token_starts_declaration (token);
632}
633
27bf414c
JM
634/* Return a pointer to the next-but-one token from PARSER, reading it
635 in if necessary. The next token is already read in. */
636
637static c_token *
638c_parser_peek_2nd_token (c_parser *parser)
639{
640 if (parser->tokens_avail >= 2)
641 return &parser->tokens[1];
642 gcc_assert (parser->tokens_avail == 1);
643 gcc_assert (parser->tokens[0].type != CPP_EOF);
bc4071dd 644 gcc_assert (parser->tokens[0].type != CPP_PRAGMA_EOL);
0bacb8c7 645 c_lex_one_token (parser, &parser->tokens[1]);
27bf414c
JM
646 parser->tokens_avail = 2;
647 return &parser->tokens[1];
648}
649
650/* Consume the next token from PARSER. */
651
652static void
653c_parser_consume_token (c_parser *parser)
654{
bc4071dd
RH
655 gcc_assert (parser->tokens_avail >= 1);
656 gcc_assert (parser->tokens[0].type != CPP_EOF);
657 gcc_assert (!parser->in_pragma || parser->tokens[0].type != CPP_PRAGMA_EOL);
658 gcc_assert (parser->error || parser->tokens[0].type != CPP_PRAGMA);
27bf414c
JM
659 if (parser->tokens_avail == 2)
660 parser->tokens[0] = parser->tokens[1];
27bf414c
JM
661 parser->tokens_avail--;
662}
663
bc4071dd
RH
664/* Expect the current token to be a #pragma. Consume it and remember
665 that we've begun parsing a pragma. */
666
667static void
668c_parser_consume_pragma (c_parser *parser)
669{
670 gcc_assert (!parser->in_pragma);
671 gcc_assert (parser->tokens_avail >= 1);
672 gcc_assert (parser->tokens[0].type == CPP_PRAGMA);
673 if (parser->tokens_avail == 2)
674 parser->tokens[0] = parser->tokens[1];
675 parser->tokens_avail--;
676 parser->in_pragma = true;
677}
678
27bf414c
JM
679/* Update the globals input_location and in_system_header from
680 TOKEN. */
681static inline void
682c_parser_set_source_position_from_token (c_token *token)
683{
684 if (token->type != CPP_EOF)
685 {
686 input_location = token->location;
27bf414c
JM
687 }
688}
689
27bf414c
JM
690/* Issue a diagnostic of the form
691 FILE:LINE: MESSAGE before TOKEN
692 where TOKEN is the next token in the input stream of PARSER.
693 MESSAGE (specified by the caller) is usually of the form "expected
694 OTHER-TOKEN".
695
696 Do not issue a diagnostic if still recovering from an error.
697
698 ??? This is taken from the C++ parser, but building up messages in
699 this way is not i18n-friendly and some other approach should be
700 used. */
701
702static void
4b794eaf 703c_parser_error (c_parser *parser, const char *gmsgid)
27bf414c
JM
704{
705 c_token *token = c_parser_peek_token (parser);
706 if (parser->error)
707 return;
708 parser->error = true;
4b794eaf 709 if (!gmsgid)
27bf414c
JM
710 return;
711 /* This diagnostic makes more sense if it is tagged to the line of
712 the token we just peeked at. */
713 c_parser_set_source_position_from_token (token);
4b794eaf 714 c_parse_error (gmsgid,
27bf414c
JM
715 /* Because c_parse_error does not understand
716 CPP_KEYWORD, keywords are treated like
717 identifiers. */
718 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
cfc93532
MLI
719 /* ??? The C parser does not save the cpp flags of a
720 token, we need to pass 0 here and we will not get
721 the source spelling of some tokens but rather the
722 canonical spelling. */
723 token->value, /*flags=*/0);
27bf414c
JM
724}
725
726/* If the next token is of the indicated TYPE, consume it. Otherwise,
727 issue the error MSGID. If MSGID is NULL then a message has already
728 been produced and no message will be produced this time. Returns
729 true if found, false otherwise. */
730
731static bool
732c_parser_require (c_parser *parser,
733 enum cpp_ttype type,
734 const char *msgid)
735{
736 if (c_parser_next_token_is (parser, type))
737 {
738 c_parser_consume_token (parser);
739 return true;
740 }
741 else
742 {
743 c_parser_error (parser, msgid);
744 return false;
745 }
746}
747
748/* If the next token is the indicated keyword, consume it. Otherwise,
749 issue the error MSGID. Returns true if found, false otherwise. */
750
751static bool
752c_parser_require_keyword (c_parser *parser,
753 enum rid keyword,
754 const char *msgid)
755{
756 if (c_parser_next_token_is_keyword (parser, keyword))
757 {
758 c_parser_consume_token (parser);
759 return true;
760 }
761 else
762 {
763 c_parser_error (parser, msgid);
764 return false;
765 }
766}
767
768/* Like c_parser_require, except that tokens will be skipped until the
769 desired token is found. An error message is still produced if the
770 next token is not as expected. If MSGID is NULL then a message has
771 already been produced and no message will be produced this
772 time. */
773
774static void
775c_parser_skip_until_found (c_parser *parser,
776 enum cpp_ttype type,
777 const char *msgid)
778{
779 unsigned nesting_depth = 0;
780
781 if (c_parser_require (parser, type, msgid))
782 return;
783
784 /* Skip tokens until the desired token is found. */
785 while (true)
786 {
787 /* Peek at the next token. */
788 c_token *token = c_parser_peek_token (parser);
789 /* If we've reached the token we want, consume it and stop. */
790 if (token->type == type && !nesting_depth)
791 {
792 c_parser_consume_token (parser);
793 break;
794 }
bc4071dd 795
27bf414c
JM
796 /* If we've run out of tokens, stop. */
797 if (token->type == CPP_EOF)
798 return;
bc4071dd
RH
799 if (token->type == CPP_PRAGMA_EOL && parser->in_pragma)
800 return;
27bf414c
JM
801 if (token->type == CPP_OPEN_BRACE
802 || token->type == CPP_OPEN_PAREN
803 || token->type == CPP_OPEN_SQUARE)
804 ++nesting_depth;
805 else if (token->type == CPP_CLOSE_BRACE
806 || token->type == CPP_CLOSE_PAREN
807 || token->type == CPP_CLOSE_SQUARE)
808 {
809 if (nesting_depth-- == 0)
810 break;
811 }
812 /* Consume this token. */
813 c_parser_consume_token (parser);
814 }
815 parser->error = false;
816}
817
818/* Skip tokens until the end of a parameter is found, but do not
819 consume the comma, semicolon or closing delimiter. */
820
821static void
822c_parser_skip_to_end_of_parameter (c_parser *parser)
823{
824 unsigned nesting_depth = 0;
825
826 while (true)
827 {
828 c_token *token = c_parser_peek_token (parser);
829 if ((token->type == CPP_COMMA || token->type == CPP_SEMICOLON)
830 && !nesting_depth)
831 break;
832 /* If we've run out of tokens, stop. */
833 if (token->type == CPP_EOF)
834 return;
bc4071dd
RH
835 if (token->type == CPP_PRAGMA_EOL && parser->in_pragma)
836 return;
27bf414c
JM
837 if (token->type == CPP_OPEN_BRACE
838 || token->type == CPP_OPEN_PAREN
839 || token->type == CPP_OPEN_SQUARE)
840 ++nesting_depth;
841 else if (token->type == CPP_CLOSE_BRACE
842 || token->type == CPP_CLOSE_PAREN
843 || token->type == CPP_CLOSE_SQUARE)
844 {
845 if (nesting_depth-- == 0)
846 break;
847 }
848 /* Consume this token. */
849 c_parser_consume_token (parser);
850 }
851 parser->error = false;
852}
853
bc4071dd
RH
854/* Expect to be at the end of the pragma directive and consume an
855 end of line marker. */
856
857static void
858c_parser_skip_to_pragma_eol (c_parser *parser)
859{
860 gcc_assert (parser->in_pragma);
861 parser->in_pragma = false;
862
863 if (!c_parser_require (parser, CPP_PRAGMA_EOL, "expected end of line"))
864 while (true)
865 {
866 c_token *token = c_parser_peek_token (parser);
867 if (token->type == CPP_EOF)
868 break;
869 if (token->type == CPP_PRAGMA_EOL)
870 {
871 c_parser_consume_token (parser);
872 break;
873 }
874 c_parser_consume_token (parser);
875 }
876
877 parser->error = false;
878}
27bf414c 879
2a83cc52
RH
880/* Skip tokens until we have consumed an entire block, or until we
881 have consumed a non-nested ';'. */
882
883static void
884c_parser_skip_to_end_of_block_or_statement (c_parser *parser)
885{
886 unsigned nesting_depth = 0;
887 bool save_error = parser->error;
888
889 while (true)
890 {
891 c_token *token;
892
893 /* Peek at the next token. */
894 token = c_parser_peek_token (parser);
895
896 switch (token->type)
897 {
898 case CPP_EOF:
899 return;
900
901 case CPP_PRAGMA_EOL:
902 if (parser->in_pragma)
903 return;
904 break;
905
906 case CPP_SEMICOLON:
907 /* If the next token is a ';', we have reached the
908 end of the statement. */
909 if (!nesting_depth)
910 {
911 /* Consume the ';'. */
912 c_parser_consume_token (parser);
913 goto finished;
914 }
915 break;
916
917 case CPP_CLOSE_BRACE:
918 /* If the next token is a non-nested '}', then we have
919 reached the end of the current block. */
920 if (nesting_depth == 0 || --nesting_depth == 0)
921 {
922 c_parser_consume_token (parser);
923 goto finished;
924 }
925 break;
926
927 case CPP_OPEN_BRACE:
928 /* If it the next token is a '{', then we are entering a new
929 block. Consume the entire block. */
930 ++nesting_depth;
931 break;
932
933 case CPP_PRAGMA:
934 /* If we see a pragma, consume the whole thing at once. We
935 have some safeguards against consuming pragmas willy-nilly.
936 Normally, we'd expect to be here with parser->error set,
937 which disables these safeguards. But it's possible to get
938 here for secondary error recovery, after parser->error has
939 been cleared. */
940 c_parser_consume_pragma (parser);
941 c_parser_skip_to_pragma_eol (parser);
942 parser->error = save_error;
943 continue;
9e33de05
RS
944
945 default:
946 break;
2a83cc52
RH
947 }
948
949 c_parser_consume_token (parser);
950 }
951
952 finished:
953 parser->error = false;
954}
955
d2e796ad
MLI
956/* CPP's options (initialized by c-opts.c). */
957extern cpp_options *cpp_opts;
958
27bf414c
JM
959/* Save the warning flags which are controlled by __extension__. */
960
961static inline int
962disable_extension_diagnostics (void)
963{
964 int ret = (pedantic
965 | (warn_pointer_arith << 1)
966 | (warn_traditional << 2)
d2e796ad 967 | (flag_iso << 3)
24b97832
ILT
968 | (warn_long_long << 4)
969 | (warn_cxx_compat << 5));
e3339d0f 970 cpp_opts->cpp_pedantic = pedantic = 0;
27bf414c 971 warn_pointer_arith = 0;
e3339d0f 972 cpp_opts->cpp_warn_traditional = warn_traditional = 0;
27bf414c 973 flag_iso = 0;
e3339d0f 974 cpp_opts->cpp_warn_long_long = warn_long_long = 0;
24b97832 975 warn_cxx_compat = 0;
27bf414c
JM
976 return ret;
977}
978
979/* Restore the warning flags which are controlled by __extension__.
980 FLAGS is the return value from disable_extension_diagnostics. */
981
982static inline void
983restore_extension_diagnostics (int flags)
984{
e3339d0f 985 cpp_opts->cpp_pedantic = pedantic = flags & 1;
27bf414c 986 warn_pointer_arith = (flags >> 1) & 1;
e3339d0f 987 cpp_opts->cpp_warn_traditional = warn_traditional = (flags >> 2) & 1;
27bf414c 988 flag_iso = (flags >> 3) & 1;
e3339d0f 989 cpp_opts->cpp_warn_long_long = warn_long_long = (flags >> 4) & 1;
24b97832 990 warn_cxx_compat = (flags >> 5) & 1;
27bf414c
JM
991}
992
993/* Possibly kinds of declarator to parse. */
994typedef enum c_dtr_syn {
995 /* A normal declarator with an identifier. */
996 C_DTR_NORMAL,
997 /* An abstract declarator (maybe empty). */
998 C_DTR_ABSTRACT,
999 /* A parameter declarator: may be either, but after a type name does
1000 not redeclare a typedef name as an identifier if it can
1001 alternatively be interpreted as a typedef name; see DR#009,
1002 applied in C90 TC1, omitted from C99 and reapplied in C99 TC2
1003 following DR#249. For example, given a typedef T, "int T" and
1004 "int *T" are valid parameter declarations redeclaring T, while
1005 "int (T)" and "int * (T)" and "int (T[])" and "int (T (int))" are
1006 abstract declarators rather than involving redundant parentheses;
1007 the same applies with attributes inside the parentheses before
1008 "T". */
1009 C_DTR_PARM
1010} c_dtr_syn;
1011
1012static void c_parser_external_declaration (c_parser *);
1013static void c_parser_asm_definition (c_parser *);
32912286 1014static void c_parser_declaration_or_fndef (c_parser *, bool, bool, bool,
f05b9d93 1015 bool, bool, tree *);
32912286
JM
1016static void c_parser_static_assert_declaration_no_semi (c_parser *);
1017static void c_parser_static_assert_declaration (c_parser *);
27bf414c
JM
1018static void c_parser_declspecs (c_parser *, struct c_declspecs *, bool, bool,
1019 bool);
1020static struct c_typespec c_parser_enum_specifier (c_parser *);
1021static struct c_typespec c_parser_struct_or_union_specifier (c_parser *);
1022static tree c_parser_struct_declaration (c_parser *);
1023static struct c_typespec c_parser_typeof_specifier (c_parser *);
1024static struct c_declarator *c_parser_declarator (c_parser *, bool, c_dtr_syn,
1025 bool *);
1026static struct c_declarator *c_parser_direct_declarator (c_parser *, bool,
1027 c_dtr_syn, bool *);
1028static struct c_declarator *c_parser_direct_declarator_inner (c_parser *,
1029 bool,
1030 struct c_declarator *);
1031static struct c_arg_info *c_parser_parms_declarator (c_parser *, bool, tree);
1032static struct c_arg_info *c_parser_parms_list_declarator (c_parser *, tree);
1033static struct c_parm *c_parser_parameter_declaration (c_parser *, tree);
1034static tree c_parser_simple_asm_expr (c_parser *);
1035static tree c_parser_attributes (c_parser *);
1036static struct c_type_name *c_parser_type_name (c_parser *);
1037static struct c_expr c_parser_initializer (c_parser *);
1038static struct c_expr c_parser_braced_init (c_parser *, tree, bool);
a1e3b3d9
LB
1039static void c_parser_initelt (c_parser *, struct obstack *);
1040static void c_parser_initval (c_parser *, struct c_expr *,
1041 struct obstack *);
27bf414c
JM
1042static tree c_parser_compound_statement (c_parser *);
1043static void c_parser_compound_statement_nostart (c_parser *);
1044static void c_parser_label (c_parser *);
1045static void c_parser_statement (c_parser *);
1046static void c_parser_statement_after_labels (c_parser *);
1047static void c_parser_if_statement (c_parser *);
1048static void c_parser_switch_statement (c_parser *);
1049static void c_parser_while_statement (c_parser *);
1050static void c_parser_do_statement (c_parser *);
1051static void c_parser_for_statement (c_parser *);
1052static tree c_parser_asm_statement (c_parser *);
46bdb9cf 1053static tree c_parser_asm_operands (c_parser *, bool);
1c384bf1 1054static tree c_parser_asm_goto_operands (c_parser *);
27bf414c
JM
1055static tree c_parser_asm_clobbers (c_parser *);
1056static struct c_expr c_parser_expr_no_commas (c_parser *, struct c_expr *);
1057static struct c_expr c_parser_conditional_expression (c_parser *,
1058 struct c_expr *);
1059static struct c_expr c_parser_binary_expression (c_parser *, struct c_expr *);
1060static struct c_expr c_parser_cast_expression (c_parser *, struct c_expr *);
1061static struct c_expr c_parser_unary_expression (c_parser *);
1062static struct c_expr c_parser_sizeof_expression (c_parser *);
1063static struct c_expr c_parser_alignof_expression (c_parser *);
1064static struct c_expr c_parser_postfix_expression (c_parser *);
1065static struct c_expr c_parser_postfix_expression_after_paren_type (c_parser *,
24b97832
ILT
1066 struct c_type_name *,
1067 location_t);
27bf414c 1068static struct c_expr c_parser_postfix_expression_after_primary (c_parser *,
c2255bc4 1069 location_t loc,
27bf414c
JM
1070 struct c_expr);
1071static struct c_expr c_parser_expression (c_parser *);
46bdb9cf 1072static struct c_expr c_parser_expression_conv (c_parser *);
bbbbb16a
ILT
1073static VEC(tree,gc) *c_parser_expr_list (c_parser *, bool, bool,
1074 VEC(tree,gc) **);
953ff289
DN
1075static void c_parser_omp_construct (c_parser *);
1076static void c_parser_omp_threadprivate (c_parser *);
1077static void c_parser_omp_barrier (c_parser *);
1078static void c_parser_omp_flush (c_parser *);
a68ab351 1079static void c_parser_omp_taskwait (c_parser *);
27bf414c 1080
bc4071dd
RH
1081enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
1082static bool c_parser_pragma (c_parser *, enum pragma_context);
1083
27bf414c
JM
1084/* These Objective-C parser functions are only ever called when
1085 compiling Objective-C. */
c165dca7 1086static void c_parser_objc_class_definition (c_parser *, tree);
27bf414c
JM
1087static void c_parser_objc_class_instance_variables (c_parser *);
1088static void c_parser_objc_class_declaration (c_parser *);
1089static void c_parser_objc_alias_declaration (c_parser *);
c165dca7 1090static void c_parser_objc_protocol_definition (c_parser *, tree);
249a82c4 1091static bool c_parser_objc_method_type (c_parser *);
27bf414c
JM
1092static void c_parser_objc_method_definition (c_parser *);
1093static void c_parser_objc_methodprotolist (c_parser *);
1094static void c_parser_objc_methodproto (c_parser *);
249a82c4 1095static tree c_parser_objc_method_decl (c_parser *, bool, tree *);
27bf414c
JM
1096static tree c_parser_objc_type_name (c_parser *);
1097static tree c_parser_objc_protocol_refs (c_parser *);
1098static void c_parser_objc_try_catch_statement (c_parser *);
1099static void c_parser_objc_synchronized_statement (c_parser *);
1100static tree c_parser_objc_selector (c_parser *);
1101static tree c_parser_objc_selector_arg (c_parser *);
1102static tree c_parser_objc_receiver (c_parser *);
1103static tree c_parser_objc_message_args (c_parser *);
1104static tree c_parser_objc_keywordexpr (c_parser *);
f614132b 1105static void c_parser_objc_at_property_declaration (c_parser *);
da57d1b9
NP
1106static void c_parser_objc_at_synthesize_declaration (c_parser *);
1107static void c_parser_objc_at_dynamic_declaration (c_parser *);
668ea4b1 1108static bool c_parser_objc_diagnose_bad_element_prefix
c165dca7 1109 (c_parser *, struct c_declspecs *);
27bf414c
JM
1110
1111/* Parse a translation unit (C90 6.7, C99 6.9).
1112
1113 translation-unit:
1114 external-declarations
1115
1116 external-declarations:
1117 external-declaration
1118 external-declarations external-declaration
1119
1120 GNU extensions:
1121
1122 translation-unit:
1123 empty
1124*/
1125
1126static void
1127c_parser_translation_unit (c_parser *parser)
1128{
1129 if (c_parser_next_token_is (parser, CPP_EOF))
1130 {
b8698a0f 1131 pedwarn (c_parser_peek_token (parser)->location, OPT_pedantic,
509c9d60 1132 "ISO C forbids an empty translation unit");
27bf414c
JM
1133 }
1134 else
1135 {
1136 void *obstack_position = obstack_alloc (&parser_obstack, 0);
6ec637a4 1137 mark_valid_location_for_stdc_pragma (false);
27bf414c
JM
1138 do
1139 {
1140 ggc_collect ();
1141 c_parser_external_declaration (parser);
1142 obstack_free (&parser_obstack, obstack_position);
1143 }
1144 while (c_parser_next_token_is_not (parser, CPP_EOF));
1145 }
1146}
1147
1148/* Parse an external declaration (C90 6.7, C99 6.9).
1149
1150 external-declaration:
1151 function-definition
1152 declaration
1153
1154 GNU extensions:
1155
1156 external-declaration:
1157 asm-definition
1158 ;
1159 __extension__ external-declaration
1160
1161 Objective-C:
1162
1163 external-declaration:
1164 objc-class-definition
1165 objc-class-declaration
1166 objc-alias-declaration
1167 objc-protocol-definition
1168 objc-method-definition
1169 @end
1170*/
1171
1172static void
1173c_parser_external_declaration (c_parser *parser)
1174{
1175 int ext;
1176 switch (c_parser_peek_token (parser)->type)
1177 {
1178 case CPP_KEYWORD:
1179 switch (c_parser_peek_token (parser)->keyword)
1180 {
1181 case RID_EXTENSION:
1182 ext = disable_extension_diagnostics ();
1183 c_parser_consume_token (parser);
1184 c_parser_external_declaration (parser);
1185 restore_extension_diagnostics (ext);
1186 break;
1187 case RID_ASM:
1188 c_parser_asm_definition (parser);
1189 break;
1190 case RID_AT_INTERFACE:
1191 case RID_AT_IMPLEMENTATION:
1192 gcc_assert (c_dialect_objc ());
c165dca7 1193 c_parser_objc_class_definition (parser, NULL_TREE);
27bf414c 1194 break;
49b91f05 1195 case RID_AT_CLASS:
27bf414c
JM
1196 gcc_assert (c_dialect_objc ());
1197 c_parser_objc_class_declaration (parser);
1198 break;
1199 case RID_AT_ALIAS:
1200 gcc_assert (c_dialect_objc ());
1201 c_parser_objc_alias_declaration (parser);
1202 break;
1203 case RID_AT_PROTOCOL:
1204 gcc_assert (c_dialect_objc ());
c165dca7 1205 c_parser_objc_protocol_definition (parser, NULL_TREE);
27bf414c 1206 break;
668ea4b1
IS
1207 case RID_AT_PROPERTY:
1208 gcc_assert (c_dialect_objc ());
f614132b 1209 c_parser_objc_at_property_declaration (parser);
668ea4b1 1210 break;
da57d1b9
NP
1211 case RID_AT_SYNTHESIZE:
1212 gcc_assert (c_dialect_objc ());
1213 c_parser_objc_at_synthesize_declaration (parser);
1214 break;
1215 case RID_AT_DYNAMIC:
1216 gcc_assert (c_dialect_objc ());
1217 c_parser_objc_at_dynamic_declaration (parser);
1218 break;
27bf414c
JM
1219 case RID_AT_END:
1220 gcc_assert (c_dialect_objc ());
1221 c_parser_consume_token (parser);
1222 objc_finish_implementation ();
1223 break;
1224 default:
1225 goto decl_or_fndef;
1226 }
1227 break;
1228 case CPP_SEMICOLON:
b8698a0f 1229 pedwarn (c_parser_peek_token (parser)->location, OPT_pedantic,
509c9d60 1230 "ISO C does not allow extra %<;%> outside of a function");
27bf414c
JM
1231 c_parser_consume_token (parser);
1232 break;
bc4071dd 1233 case CPP_PRAGMA:
6ec637a4 1234 mark_valid_location_for_stdc_pragma (true);
bc4071dd 1235 c_parser_pragma (parser, pragma_external);
6ec637a4 1236 mark_valid_location_for_stdc_pragma (false);
bc4071dd 1237 break;
27bf414c
JM
1238 case CPP_PLUS:
1239 case CPP_MINUS:
1240 if (c_dialect_objc ())
1241 {
1242 c_parser_objc_method_definition (parser);
1243 break;
1244 }
1245 /* Else fall through, and yield a syntax error trying to parse
1246 as a declaration or function definition. */
1247 default:
1248 decl_or_fndef:
c165dca7
IS
1249 /* A declaration or a function definition (or, in Objective-C,
1250 an @interface or @protocol with prefix attributes). We can
1251 only tell which after parsing the declaration specifiers, if
1252 any, and the first declarator. */
f05b9d93 1253 c_parser_declaration_or_fndef (parser, true, true, true, false, true, NULL);
27bf414c
JM
1254 break;
1255 }
1256}
1257
1258/* Parse a declaration or function definition (C90 6.5, 6.7.1, C99
1259 6.7, 6.9.1). If FNDEF_OK is true, a function definition is
1260 accepted; otherwise (old-style parameter declarations) only other
32912286
JM
1261 declarations are accepted. If STATIC_ASSERT_OK is true, a static
1262 assertion is accepted; otherwise (old-style parameter declarations)
1263 it is not. If NESTED is true, we are inside a function or parsing
1264 old-style parameter declarations; any functions encountered are
1265 nested functions and declaration specifiers are required; otherwise
1266 we are at top level and functions are normal functions and
1267 declaration specifiers may be optional. If EMPTY_OK is true, empty
1268 declarations are OK (subject to all other constraints); otherwise
1269 (old-style parameter declarations) they are diagnosed. If
1270 START_ATTR_OK is true, the declaration specifiers may start with
1271 attributes; otherwise they may not.
f05b9d93
NP
1272 OBJC_FOREACH_OBJECT_DECLARATION can be used to get back the parsed
1273 declaration when parsing an Objective-C foreach statement.
27bf414c
JM
1274
1275 declaration:
1276 declaration-specifiers init-declarator-list[opt] ;
32912286 1277 static_assert-declaration
27bf414c
JM
1278
1279 function-definition:
1280 declaration-specifiers[opt] declarator declaration-list[opt]
1281 compound-statement
1282
1283 declaration-list:
1284 declaration
1285 declaration-list declaration
1286
1287 init-declarator-list:
1288 init-declarator
1289 init-declarator-list , init-declarator
1290
1291 init-declarator:
1292 declarator simple-asm-expr[opt] attributes[opt]
1293 declarator simple-asm-expr[opt] attributes[opt] = initializer
1294
1295 GNU extensions:
1296
1297 nested-function-definition:
1298 declaration-specifiers declarator declaration-list[opt]
1299 compound-statement
1300
c165dca7
IS
1301 Objective-C:
1302 attributes objc-class-definition
1303 attributes objc-category-definition
1304 attributes objc-protocol-definition
1305
27bf414c
JM
1306 The simple-asm-expr and attributes are GNU extensions.
1307
1308 This function does not handle __extension__; that is handled in its
1309 callers. ??? Following the old parser, __extension__ may start
1310 external declarations, declarations in functions and declarations
1311 at the start of "for" loops, but not old-style parameter
1312 declarations.
1313
1314 C99 requires declaration specifiers in a function definition; the
1315 absence is diagnosed through the diagnosis of implicit int. In GNU
1316 C we also allow but diagnose declarations without declaration
1317 specifiers, but only at top level (elsewhere they conflict with
953ff289 1318 other syntax).
b8698a0f 1319
f05b9d93
NP
1320 In Objective-C, declarations of the looping variable in a foreach
1321 statement are exceptionally terminated by 'in' (for example, 'for
1322 (NSObject *object in array) { ... }').
1323
953ff289 1324 OpenMP:
b8698a0f 1325
953ff289
DN
1326 declaration:
1327 threadprivate-directive */
27bf414c
JM
1328
1329static void
32912286
JM
1330c_parser_declaration_or_fndef (c_parser *parser, bool fndef_ok,
1331 bool static_assert_ok, bool empty_ok,
f05b9d93
NP
1332 bool nested, bool start_attr_ok,
1333 tree *objc_foreach_object_declaration)
27bf414c
JM
1334{
1335 struct c_declspecs *specs;
1336 tree prefix_attrs;
1337 tree all_prefix_attrs;
1338 bool diagnosed_no_specs = false;
c7412148 1339 location_t here = c_parser_peek_token (parser)->location;
bc4071dd 1340
32912286
JM
1341 if (static_assert_ok
1342 && c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT))
1343 {
1344 c_parser_static_assert_declaration (parser);
1345 return;
1346 }
27bf414c
JM
1347 specs = build_null_declspecs ();
1348 c_parser_declspecs (parser, specs, true, true, start_attr_ok);
1349 if (parser->error)
1350 {
1351 c_parser_skip_to_end_of_block_or_statement (parser);
1352 return;
1353 }
1354 if (nested && !specs->declspecs_seen_p)
1355 {
1356 c_parser_error (parser, "expected declaration specifiers");
1357 c_parser_skip_to_end_of_block_or_statement (parser);
1358 return;
1359 }
1360 finish_declspecs (specs);
1361 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1362 {
1363 if (empty_ok)
1364 shadow_tag (specs);
1365 else
1366 {
1367 shadow_tag_warned (specs, 1);
509c9d60 1368 pedwarn (here, 0, "empty declaration");
27bf414c
JM
1369 }
1370 c_parser_consume_token (parser);
1371 return;
1372 }
c165dca7
IS
1373 else if (c_dialect_objc ())
1374 {
f7e71da5
IS
1375 /* Prefix attributes are an error on method decls. */
1376 switch (c_parser_peek_token (parser)->type)
1377 {
1378 case CPP_PLUS:
1379 case CPP_MINUS:
1380 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1381 return;
1382 if (specs->attrs)
1383 {
1384 warning_at (c_parser_peek_token (parser)->location,
1385 OPT_Wattributes,
1386 "prefix attributes are ignored for methods");
1387 specs->attrs = NULL_TREE;
1388 }
1389 if (fndef_ok)
1390 c_parser_objc_method_definition (parser);
1391 else
1392 c_parser_objc_methodproto (parser);
1393 return;
1394 break;
1395 default:
1396 break;
1397 }
c165dca7
IS
1398 /* This is where we parse 'attributes @interface ...',
1399 'attributes @implementation ...', 'attributes @protocol ...'
1400 (where attributes could be, for example, __attribute__
1401 ((deprecated)).
1402 */
1403 switch (c_parser_peek_token (parser)->keyword)
1404 {
1405 case RID_AT_INTERFACE:
1406 {
1407 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1408 return;
1409 c_parser_objc_class_definition (parser, specs->attrs);
1410 return;
1411 }
1412 break;
1413 case RID_AT_IMPLEMENTATION:
1414 {
1415 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1416 return;
1417 if (specs->attrs)
1418 {
1419 warning_at (c_parser_peek_token (parser)->location,
1420 OPT_Wattributes,
1421 "prefix attributes are ignored for implementations");
1422 specs->attrs = NULL_TREE;
1423 }
1424 c_parser_objc_class_definition (parser, NULL_TREE);
1425 return;
1426 }
1427 break;
1428 case RID_AT_PROTOCOL:
1429 {
1430 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1431 return;
1432 c_parser_objc_protocol_definition (parser, specs->attrs);
1433 return;
1434 }
1435 break;
668ea4b1
IS
1436 case RID_AT_ALIAS:
1437 case RID_AT_CLASS:
1438 case RID_AT_END:
1439 case RID_AT_PROPERTY:
1440 if (specs->attrs)
1441 {
1442 c_parser_error (parser,
1443 "attributes may not be specified before" );
1444 specs->attrs = NULL;
1445 }
1446 break;
c165dca7
IS
1447 default:
1448 break;
1449 }
1450 }
1451
27bf414c
JM
1452 pending_xref_error ();
1453 prefix_attrs = specs->attrs;
1454 all_prefix_attrs = prefix_attrs;
1455 specs->attrs = NULL_TREE;
1456 while (true)
1457 {
1458 struct c_declarator *declarator;
1459 bool dummy = false;
1460 tree fnbody;
1461 /* Declaring either one or more declarators (in which case we
1462 should diagnose if there were no declaration specifiers) or a
1463 function definition (in which case the diagnostic for
1464 implicit int suffices). */
9e5b2115
PB
1465 declarator = c_parser_declarator (parser,
1466 specs->typespec_kind != ctsk_none,
27bf414c
JM
1467 C_DTR_NORMAL, &dummy);
1468 if (declarator == NULL)
1469 {
1470 c_parser_skip_to_end_of_block_or_statement (parser);
1471 return;
1472 }
1473 if (c_parser_next_token_is (parser, CPP_EQ)
1474 || c_parser_next_token_is (parser, CPP_COMMA)
1475 || c_parser_next_token_is (parser, CPP_SEMICOLON)
1476 || c_parser_next_token_is_keyword (parser, RID_ASM)
f05b9d93
NP
1477 || c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE)
1478 || c_parser_next_token_is_keyword (parser, RID_IN))
27bf414c
JM
1479 {
1480 tree asm_name = NULL_TREE;
1481 tree postfix_attrs = NULL_TREE;
1482 if (!diagnosed_no_specs && !specs->declspecs_seen_p)
1483 {
1484 diagnosed_no_specs = true;
509c9d60 1485 pedwarn (here, 0, "data definition has no type or storage class");
27bf414c
JM
1486 }
1487 /* Having seen a data definition, there cannot now be a
1488 function definition. */
1489 fndef_ok = false;
1490 if (c_parser_next_token_is_keyword (parser, RID_ASM))
1491 asm_name = c_parser_simple_asm_expr (parser);
1492 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
1493 postfix_attrs = c_parser_attributes (parser);
1494 if (c_parser_next_token_is (parser, CPP_EQ))
1495 {
1496 tree d;
1497 struct c_expr init;
c2255bc4 1498 location_t init_loc;
27bf414c
JM
1499 c_parser_consume_token (parser);
1500 /* The declaration of the variable is in effect while
1501 its initializer is parsed. */
1502 d = start_decl (declarator, specs, true,
1503 chainon (postfix_attrs, all_prefix_attrs));
1504 if (!d)
1505 d = error_mark_node;
1506 start_init (d, asm_name, global_bindings_p ());
c2255bc4 1507 init_loc = c_parser_peek_token (parser)->location;
27bf414c
JM
1508 init = c_parser_initializer (parser);
1509 finish_init ();
1510 if (d != error_mark_node)
1511 {
1512 maybe_warn_string_init (TREE_TYPE (d), init);
c2255bc4
AH
1513 finish_decl (d, init_loc, init.value,
1514 init.original_type, asm_name);
27bf414c
JM
1515 }
1516 }
1517 else
1518 {
1519 tree d = start_decl (declarator, specs, false,
1520 chainon (postfix_attrs,
1521 all_prefix_attrs));
1522 if (d)
c2255bc4 1523 finish_decl (d, UNKNOWN_LOCATION, NULL_TREE,
f05b9d93
NP
1524 NULL_TREE, asm_name);
1525
1526 if (c_parser_next_token_is_keyword (parser, RID_IN))
1527 {
1528 if (d)
1529 *objc_foreach_object_declaration = d;
1530 else
1531 *objc_foreach_object_declaration = error_mark_node;
1532 }
27bf414c
JM
1533 }
1534 if (c_parser_next_token_is (parser, CPP_COMMA))
1535 {
1536 c_parser_consume_token (parser);
1537 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
1538 all_prefix_attrs = chainon (c_parser_attributes (parser),
1539 prefix_attrs);
1540 else
1541 all_prefix_attrs = prefix_attrs;
1542 continue;
1543 }
1544 else if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1545 {
1546 c_parser_consume_token (parser);
1547 return;
1548 }
f05b9d93
NP
1549 else if (c_parser_next_token_is_keyword (parser, RID_IN))
1550 {
1551 /* This can only happen in Objective-C: we found the
1552 'in' that terminates the declaration inside an
1553 Objective-C foreach statement. Do not consume the
1554 token, so that the caller can use it to determine
1555 that this indeed is a foreach context. */
1556 return;
1557 }
27bf414c
JM
1558 else
1559 {
1560 c_parser_error (parser, "expected %<,%> or %<;%>");
1561 c_parser_skip_to_end_of_block_or_statement (parser);
1562 return;
1563 }
1564 }
1565 else if (!fndef_ok)
1566 {
1567 c_parser_error (parser, "expected %<=%>, %<,%>, %<;%>, "
1568 "%<asm%> or %<__attribute__%>");
1569 c_parser_skip_to_end_of_block_or_statement (parser);
1570 return;
1571 }
1572 /* Function definition (nested or otherwise). */
1573 if (nested)
1574 {
509c9d60 1575 pedwarn (here, OPT_pedantic, "ISO C forbids nested functions");
d2784db4 1576 c_push_function_context ();
27bf414c
JM
1577 }
1578 if (!start_function (specs, declarator, all_prefix_attrs))
1579 {
1580 /* This can appear in many cases looking nothing like a
1581 function definition, so we don't give a more specific
1582 error suggesting there was one. */
1583 c_parser_error (parser, "expected %<=%>, %<,%>, %<;%>, %<asm%> "
1584 "or %<__attribute__%>");
1585 if (nested)
d2784db4 1586 c_pop_function_context ();
27bf414c
JM
1587 break;
1588 }
1589 /* Parse old-style parameter declarations. ??? Attributes are
1590 not allowed to start declaration specifiers here because of a
1591 syntax conflict between a function declaration with attribute
1592 suffix and a function definition with an attribute prefix on
1593 first old-style parameter declaration. Following the old
1594 parser, they are not accepted on subsequent old-style
1595 parameter declarations either. However, there is no
1596 ambiguity after the first declaration, nor indeed on the
1597 first as long as we don't allow postfix attributes after a
1598 declarator with a nonempty identifier list in a definition;
1599 and postfix attributes have never been accepted here in
1600 function definitions either. */
1601 while (c_parser_next_token_is_not (parser, CPP_EOF)
1602 && c_parser_next_token_is_not (parser, CPP_OPEN_BRACE))
32912286 1603 c_parser_declaration_or_fndef (parser, false, false, false,
f05b9d93 1604 true, false, NULL);
27bf414c 1605 store_parm_decls ();
1751ecd6
AH
1606 DECL_STRUCT_FUNCTION (current_function_decl)->function_start_locus
1607 = c_parser_peek_token (parser)->location;
27bf414c
JM
1608 fnbody = c_parser_compound_statement (parser);
1609 if (nested)
1610 {
1611 tree decl = current_function_decl;
9f62cb92
JJ
1612 /* Mark nested functions as needing static-chain initially.
1613 lower_nested_functions will recompute it but the
1614 DECL_STATIC_CHAIN flag is also used before that happens,
1615 by initializer_constant_valid_p. See gcc.dg/nested-fn-2.c. */
1616 DECL_STATIC_CHAIN (decl) = 1;
27bf414c
JM
1617 add_stmt (fnbody);
1618 finish_function ();
d2784db4 1619 c_pop_function_context ();
c2255bc4 1620 add_stmt (build_stmt (DECL_SOURCE_LOCATION (decl), DECL_EXPR, decl));
27bf414c
JM
1621 }
1622 else
1623 {
1624 add_stmt (fnbody);
1625 finish_function ();
1626 }
1627 break;
1628 }
1629}
1630
1631/* Parse an asm-definition (asm() outside a function body). This is a
1632 GNU extension.
1633
1634 asm-definition:
1635 simple-asm-expr ;
1636*/
1637
1638static void
1639c_parser_asm_definition (c_parser *parser)
1640{
1641 tree asm_str = c_parser_simple_asm_expr (parser);
27bf414c 1642 if (asm_str)
474eccc6 1643 cgraph_add_asm_node (asm_str);
27bf414c
JM
1644 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
1645}
1646
32912286
JM
1647/* Parse a static assertion (C1X N1425 6.7.10).
1648
1649 static_assert-declaration:
1650 static_assert-declaration-no-semi ;
1651*/
1652
1653static void
1654c_parser_static_assert_declaration (c_parser *parser)
1655{
1656 c_parser_static_assert_declaration_no_semi (parser);
1657 if (parser->error
1658 || !c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
1659 c_parser_skip_to_end_of_block_or_statement (parser);
1660}
1661
1662/* Parse a static assertion (C1X N1425 6.7.10), without the trailing
1663 semicolon.
1664
1665 static_assert-declaration-no-semi:
1666 _Static_assert ( constant-expression , string-literal )
1667*/
1668
1669static void
1670c_parser_static_assert_declaration_no_semi (c_parser *parser)
1671{
1672 location_t assert_loc, value_loc;
1673 tree value;
1674 tree string;
1675
1676 gcc_assert (c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT));
1677 assert_loc = c_parser_peek_token (parser)->location;
1678 if (!flag_isoc1x)
1679 {
1680 if (flag_isoc99)
1681 pedwarn (assert_loc, OPT_pedantic,
1682 "ISO C99 does not support %<_Static_assert%>");
1683 else
1684 pedwarn (assert_loc, OPT_pedantic,
1685 "ISO C90 does not support %<_Static_assert%>");
1686 }
1687 c_parser_consume_token (parser);
1688 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
1689 return;
1690 value_loc = c_parser_peek_token (parser)->location;
1691 value = c_parser_expr_no_commas (parser, NULL).value;
1692 parser->lex_untranslated_string = true;
1693 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
1694 {
1695 parser->lex_untranslated_string = false;
1696 return;
1697 }
1698 switch (c_parser_peek_token (parser)->type)
1699 {
1700 case CPP_STRING:
1701 case CPP_STRING16:
1702 case CPP_STRING32:
1703 case CPP_WSTRING:
1704 case CPP_UTF8STRING:
1705 string = c_parser_peek_token (parser)->value;
1706 c_parser_consume_token (parser);
1707 parser->lex_untranslated_string = false;
1708 break;
1709 default:
1710 c_parser_error (parser, "expected string literal");
1711 parser->lex_untranslated_string = false;
1712 return;
1713 }
1714 c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
1715
1716 if (!INTEGRAL_TYPE_P (TREE_TYPE (value)))
1717 {
1718 error_at (value_loc, "expression in static assertion is not an integer");
1719 return;
1720 }
1721 if (TREE_CODE (value) != INTEGER_CST)
1722 {
1723 value = c_fully_fold (value, false, NULL);
1724 if (TREE_CODE (value) == INTEGER_CST)
1725 pedwarn (value_loc, OPT_pedantic, "expression in static assertion "
1726 "is not an integer constant expression");
1727 }
1728 if (TREE_CODE (value) != INTEGER_CST)
1729 {
1730 error_at (value_loc, "expression in static assertion is not constant");
1731 return;
1732 }
1733 constant_expression_warning (value);
1734 if (integer_zerop (value))
1735 error_at (assert_loc, "static assertion failed: %E", string);
1736}
1737
27bf414c
JM
1738/* Parse some declaration specifiers (possibly none) (C90 6.5, C99
1739 6.7), adding them to SPECS (which may already include some).
1740 Storage class specifiers are accepted iff SCSPEC_OK; type
1741 specifiers are accepted iff TYPESPEC_OK; attributes are accepted at
1742 the start iff START_ATTR_OK.
1743
1744 declaration-specifiers:
1745 storage-class-specifier declaration-specifiers[opt]
1746 type-specifier declaration-specifiers[opt]
1747 type-qualifier declaration-specifiers[opt]
1748 function-specifier declaration-specifiers[opt]
1749
1750 Function specifiers (inline) are from C99, and are currently
1751 handled as storage class specifiers, as is __thread.
1752
1753 C90 6.5.1, C99 6.7.1:
1754 storage-class-specifier:
1755 typedef
1756 extern
1757 static
1758 auto
1759 register
1760
1761 C99 6.7.4:
1762 function-specifier:
1763 inline
1764
1765 C90 6.5.2, C99 6.7.2:
1766 type-specifier:
1767 void
1768 char
1769 short
1770 int
1771 long
1772 float
1773 double
1774 signed
1775 unsigned
1776 _Bool
1777 _Complex
1778 [_Imaginary removed in C99 TC2]
1779 struct-or-union-specifier
1780 enum-specifier
1781 typedef-name
1782
1783 (_Bool and _Complex are new in C99.)
1784
1785 C90 6.5.3, C99 6.7.3:
1786
1787 type-qualifier:
1788 const
1789 restrict
1790 volatile
36c5e70a 1791 address-space-qualifier
27bf414c
JM
1792
1793 (restrict is new in C99.)
1794
1795 GNU extensions:
1796
1797 declaration-specifiers:
1798 attributes declaration-specifiers[opt]
1799
36c5e70a
BE
1800 type-qualifier:
1801 address-space
1802
1803 address-space:
1804 identifier recognized by the target
1805
27bf414c
JM
1806 storage-class-specifier:
1807 __thread
1808
1809 type-specifier:
1810 typeof-specifier
a6766312 1811 __int128
9a8ce21f
JG
1812 _Decimal32
1813 _Decimal64
1814 _Decimal128
ab22c1fa
CF
1815 _Fract
1816 _Accum
1817 _Sat
1818
1819 (_Fract, _Accum, and _Sat are new from ISO/IEC DTR 18037:
1820 http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1169.pdf)
27bf414c
JM
1821
1822 Objective-C:
1823
1824 type-specifier:
1825 class-name objc-protocol-refs[opt]
1826 typedef-name objc-protocol-refs
1827 objc-protocol-refs
1828*/
1829
1830static void
1831c_parser_declspecs (c_parser *parser, struct c_declspecs *specs,
1832 bool scspec_ok, bool typespec_ok, bool start_attr_ok)
1833{
1834 bool attrs_ok = start_attr_ok;
9e5b2115 1835 bool seen_type = specs->typespec_kind != ctsk_none;
27bf414c
JM
1836 while (c_parser_next_token_is (parser, CPP_NAME)
1837 || c_parser_next_token_is (parser, CPP_KEYWORD)
1838 || (c_dialect_objc () && c_parser_next_token_is (parser, CPP_LESS)))
1839 {
1840 struct c_typespec t;
1841 tree attrs;
dc491a25 1842 location_t loc = c_parser_peek_token (parser)->location;
27bf414c
JM
1843 if (c_parser_next_token_is (parser, CPP_NAME))
1844 {
1845 tree value = c_parser_peek_token (parser)->value;
1846 c_id_kind kind = c_parser_peek_token (parser)->id_kind;
36c5e70a
BE
1847
1848 if (kind == C_ID_ADDRSPACE)
1849 {
1850 addr_space_t as
1851 = c_parser_peek_token (parser)->keyword - RID_FIRST_ADDR_SPACE;
1852 declspecs_add_addrspace (specs, as);
1853 c_parser_consume_token (parser);
1854 attrs_ok = true;
1855 continue;
1856 }
1857
27bf414c
JM
1858 /* This finishes the specifiers unless a type name is OK, it
1859 is declared as a type name and a type name hasn't yet
1860 been seen. */
1861 if (!typespec_ok || seen_type
1862 || (kind != C_ID_TYPENAME && kind != C_ID_CLASSNAME))
1863 break;
1864 c_parser_consume_token (parser);
1865 seen_type = true;
1866 attrs_ok = true;
1867 if (kind == C_ID_TYPENAME
1868 && (!c_dialect_objc ()
1869 || c_parser_next_token_is_not (parser, CPP_LESS)))
1870 {
1871 t.kind = ctsk_typedef;
1872 /* For a typedef name, record the meaning, not the name.
1873 In case of 'foo foo, bar;'. */
1874 t.spec = lookup_name (value);
928c19bb
JM
1875 t.expr = NULL_TREE;
1876 t.expr_const_operands = true;
27bf414c
JM
1877 }
1878 else
1879 {
1880 tree proto = NULL_TREE;
1881 gcc_assert (c_dialect_objc ());
1882 t.kind = ctsk_objc;
1883 if (c_parser_next_token_is (parser, CPP_LESS))
1884 proto = c_parser_objc_protocol_refs (parser);
1885 t.spec = objc_get_protocol_qualified_type (value, proto);
928c19bb
JM
1886 t.expr = NULL_TREE;
1887 t.expr_const_operands = true;
27bf414c 1888 }
dc491a25 1889 declspecs_add_type (loc, specs, t);
27bf414c
JM
1890 continue;
1891 }
1892 if (c_parser_next_token_is (parser, CPP_LESS))
1893 {
1894 /* Make "<SomeProtocol>" equivalent to "id <SomeProtocol>" -
1895 nisse@lysator.liu.se. */
1896 tree proto;
1897 gcc_assert (c_dialect_objc ());
1898 if (!typespec_ok || seen_type)
1899 break;
1900 proto = c_parser_objc_protocol_refs (parser);
1901 t.kind = ctsk_objc;
1902 t.spec = objc_get_protocol_qualified_type (NULL_TREE, proto);
928c19bb
JM
1903 t.expr = NULL_TREE;
1904 t.expr_const_operands = true;
dc491a25 1905 declspecs_add_type (loc, specs, t);
27bf414c
JM
1906 continue;
1907 }
1908 gcc_assert (c_parser_next_token_is (parser, CPP_KEYWORD));
1909 switch (c_parser_peek_token (parser)->keyword)
1910 {
1911 case RID_STATIC:
1912 case RID_EXTERN:
1913 case RID_REGISTER:
1914 case RID_TYPEDEF:
1915 case RID_INLINE:
1916 case RID_AUTO:
1917 case RID_THREAD:
1918 if (!scspec_ok)
1919 goto out;
1920 attrs_ok = true;
1921 /* TODO: Distinguish between function specifiers (inline)
1922 and storage class specifiers, either here or in
1923 declspecs_add_scspec. */
1924 declspecs_add_scspec (specs, c_parser_peek_token (parser)->value);
1925 c_parser_consume_token (parser);
1926 break;
1927 case RID_UNSIGNED:
1928 case RID_LONG:
a6766312 1929 case RID_INT128:
27bf414c
JM
1930 case RID_SHORT:
1931 case RID_SIGNED:
1932 case RID_COMPLEX:
1933 case RID_INT:
1934 case RID_CHAR:
1935 case RID_FLOAT:
1936 case RID_DOUBLE:
1937 case RID_VOID:
9a8ce21f
JG
1938 case RID_DFLOAT32:
1939 case RID_DFLOAT64:
1940 case RID_DFLOAT128:
27bf414c 1941 case RID_BOOL:
ab22c1fa
CF
1942 case RID_FRACT:
1943 case RID_ACCUM:
1944 case RID_SAT:
27bf414c
JM
1945 if (!typespec_ok)
1946 goto out;
1947 attrs_ok = true;
1948 seen_type = true;
0bacb8c7
TT
1949 if (c_dialect_objc ())
1950 parser->objc_need_raw_identifier = true;
27bf414c
JM
1951 t.kind = ctsk_resword;
1952 t.spec = c_parser_peek_token (parser)->value;
928c19bb
JM
1953 t.expr = NULL_TREE;
1954 t.expr_const_operands = true;
dc491a25 1955 declspecs_add_type (loc, specs, t);
27bf414c
JM
1956 c_parser_consume_token (parser);
1957 break;
1958 case RID_ENUM:
1959 if (!typespec_ok)
1960 goto out;
1961 attrs_ok = true;
1962 seen_type = true;
1963 t = c_parser_enum_specifier (parser);
dc491a25 1964 declspecs_add_type (loc, specs, t);
27bf414c
JM
1965 break;
1966 case RID_STRUCT:
1967 case RID_UNION:
1968 if (!typespec_ok)
1969 goto out;
1970 attrs_ok = true;
1971 seen_type = true;
1972 t = c_parser_struct_or_union_specifier (parser);
dab71827 1973 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, t.spec);
dc491a25 1974 declspecs_add_type (loc, specs, t);
27bf414c
JM
1975 break;
1976 case RID_TYPEOF:
1977 /* ??? The old parser rejected typeof after other type
1978 specifiers, but is a syntax error the best way of
1979 handling this? */
1980 if (!typespec_ok || seen_type)
1981 goto out;
1982 attrs_ok = true;
1983 seen_type = true;
1984 t = c_parser_typeof_specifier (parser);
dc491a25 1985 declspecs_add_type (loc, specs, t);
27bf414c
JM
1986 break;
1987 case RID_CONST:
1988 case RID_VOLATILE:
1989 case RID_RESTRICT:
1990 attrs_ok = true;
1991 declspecs_add_qual (specs, c_parser_peek_token (parser)->value);
1992 c_parser_consume_token (parser);
1993 break;
1994 case RID_ATTRIBUTE:
1995 if (!attrs_ok)
1996 goto out;
1997 attrs = c_parser_attributes (parser);
1998 declspecs_add_attrs (specs, attrs);
1999 break;
2000 default:
2001 goto out;
2002 }
2003 }
2004 out: ;
2005}
2006
2007/* Parse an enum specifier (C90 6.5.2.2, C99 6.7.2.2).
2008
2009 enum-specifier:
2010 enum attributes[opt] identifier[opt] { enumerator-list } attributes[opt]
2011 enum attributes[opt] identifier[opt] { enumerator-list , } attributes[opt]
2012 enum attributes[opt] identifier
2013
2014 The form with trailing comma is new in C99. The forms with
2015 attributes are GNU extensions. In GNU C, we accept any expression
2016 without commas in the syntax (assignment expressions, not just
2017 conditional expressions); assignment expressions will be diagnosed
2018 as non-constant.
2019
2020 enumerator-list:
2021 enumerator
2022 enumerator-list , enumerator
2023
2024 enumerator:
2025 enumeration-constant
2026 enumeration-constant = constant-expression
2027*/
2028
2029static struct c_typespec
2030c_parser_enum_specifier (c_parser *parser)
2031{
2032 struct c_typespec ret;
2033 tree attrs;
2034 tree ident = NULL_TREE;
24b97832 2035 location_t enum_loc;
922f2908 2036 location_t ident_loc = UNKNOWN_LOCATION; /* Quiet warning. */
27bf414c 2037 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ENUM));
24b97832 2038 enum_loc = c_parser_peek_token (parser)->location;
27bf414c
JM
2039 c_parser_consume_token (parser);
2040 attrs = c_parser_attributes (parser);
c2255bc4 2041 enum_loc = c_parser_peek_token (parser)->location;
5af28c74
TT
2042 /* Set the location in case we create a decl now. */
2043 c_parser_set_source_position_from_token (c_parser_peek_token (parser));
27bf414c
JM
2044 if (c_parser_next_token_is (parser, CPP_NAME))
2045 {
2046 ident = c_parser_peek_token (parser)->value;
c7412148 2047 ident_loc = c_parser_peek_token (parser)->location;
24b97832 2048 enum_loc = ident_loc;
27bf414c
JM
2049 c_parser_consume_token (parser);
2050 }
2051 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
2052 {
2053 /* Parse an enum definition. */
7114359f 2054 struct c_enum_contents the_enum;
c2255bc4 2055 tree type = start_enum (enum_loc, &the_enum, ident);
27bf414c
JM
2056 tree postfix_attrs;
2057 /* We chain the enumerators in reverse order, then put them in
2058 forward order at the end. */
2059 tree values = NULL_TREE;
2060 c_parser_consume_token (parser);
2061 while (true)
2062 {
2063 tree enum_id;
2064 tree enum_value;
2065 tree enum_decl;
2066 bool seen_comma;
5af28c74 2067 c_token *token;
922f2908 2068 location_t comma_loc = UNKNOWN_LOCATION; /* Quiet warning. */
7370e0da 2069 location_t decl_loc, value_loc;
27bf414c
JM
2070 if (c_parser_next_token_is_not (parser, CPP_NAME))
2071 {
2072 c_parser_error (parser, "expected identifier");
2073 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
2074 values = error_mark_node;
2075 break;
2076 }
5af28c74
TT
2077 token = c_parser_peek_token (parser);
2078 enum_id = token->value;
2079 /* Set the location in case we create a decl now. */
2080 c_parser_set_source_position_from_token (token);
7370e0da 2081 decl_loc = value_loc = token->location;
27bf414c
JM
2082 c_parser_consume_token (parser);
2083 if (c_parser_next_token_is (parser, CPP_EQ))
2084 {
2085 c_parser_consume_token (parser);
85790e66 2086 value_loc = c_parser_peek_token (parser)->location;
27bf414c
JM
2087 enum_value = c_parser_expr_no_commas (parser, NULL).value;
2088 }
2089 else
2090 enum_value = NULL_TREE;
7370e0da 2091 enum_decl = build_enumerator (decl_loc, value_loc,
c2255bc4 2092 &the_enum, enum_id, enum_value);
27bf414c
JM
2093 TREE_CHAIN (enum_decl) = values;
2094 values = enum_decl;
2095 seen_comma = false;
2096 if (c_parser_next_token_is (parser, CPP_COMMA))
2097 {
c7412148 2098 comma_loc = c_parser_peek_token (parser)->location;
27bf414c
JM
2099 seen_comma = true;
2100 c_parser_consume_token (parser);
2101 }
2102 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2103 {
fcf73884 2104 if (seen_comma && !flag_isoc99)
509c9d60 2105 pedwarn (comma_loc, OPT_pedantic, "comma at end of enumerator list");
27bf414c
JM
2106 c_parser_consume_token (parser);
2107 break;
2108 }
2109 if (!seen_comma)
2110 {
2111 c_parser_error (parser, "expected %<,%> or %<}%>");
2112 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
2113 values = error_mark_node;
2114 break;
2115 }
2116 }
2117 postfix_attrs = c_parser_attributes (parser);
2118 ret.spec = finish_enum (type, nreverse (values),
2119 chainon (attrs, postfix_attrs));
2120 ret.kind = ctsk_tagdef;
928c19bb
JM
2121 ret.expr = NULL_TREE;
2122 ret.expr_const_operands = true;
27bf414c
JM
2123 return ret;
2124 }
2125 else if (!ident)
2126 {
2127 c_parser_error (parser, "expected %<{%>");
2128 ret.spec = error_mark_node;
2129 ret.kind = ctsk_tagref;
928c19bb
JM
2130 ret.expr = NULL_TREE;
2131 ret.expr_const_operands = true;
27bf414c
JM
2132 return ret;
2133 }
c2255bc4 2134 ret = parser_xref_tag (ident_loc, ENUMERAL_TYPE, ident);
27bf414c
JM
2135 /* In ISO C, enumerated types can be referred to only if already
2136 defined. */
2137 if (pedantic && !COMPLETE_TYPE_P (ret.spec))
c7412148
TT
2138 {
2139 gcc_assert (ident);
c2255bc4 2140 pedwarn (enum_loc, OPT_pedantic,
509c9d60 2141 "ISO C forbids forward references to %<enum%> types");
c7412148 2142 }
27bf414c
JM
2143 return ret;
2144}
2145
2146/* Parse a struct or union specifier (C90 6.5.2.1, C99 6.7.2.1).
2147
2148 struct-or-union-specifier:
2149 struct-or-union attributes[opt] identifier[opt]
2150 { struct-contents } attributes[opt]
2151 struct-or-union attributes[opt] identifier
2152
2153 struct-contents:
2154 struct-declaration-list
2155
2156 struct-declaration-list:
2157 struct-declaration ;
2158 struct-declaration-list struct-declaration ;
2159
2160 GNU extensions:
2161
2162 struct-contents:
2163 empty
2164 struct-declaration
2165 struct-declaration-list struct-declaration
2166
2167 struct-declaration-list:
2168 struct-declaration-list ;
2169 ;
2170
2171 (Note that in the syntax here, unlike that in ISO C, the semicolons
2172 are included here rather than in struct-declaration, in order to
2173 describe the syntax with extra semicolons and missing semicolon at
2174 end.)
2175
2176 Objective-C:
2177
2178 struct-declaration-list:
2179 @defs ( class-name )
2180
2181 (Note this does not include a trailing semicolon, but can be
2182 followed by further declarations, and gets a pedwarn-if-pedantic
2183 when followed by a semicolon.) */
2184
2185static struct c_typespec
2186c_parser_struct_or_union_specifier (c_parser *parser)
2187{
2188 struct c_typespec ret;
2189 tree attrs;
2190 tree ident = NULL_TREE;
24b97832
ILT
2191 location_t struct_loc;
2192 location_t ident_loc = UNKNOWN_LOCATION;
27bf414c
JM
2193 enum tree_code code;
2194 switch (c_parser_peek_token (parser)->keyword)
2195 {
2196 case RID_STRUCT:
2197 code = RECORD_TYPE;
2198 break;
2199 case RID_UNION:
2200 code = UNION_TYPE;
2201 break;
2202 default:
2203 gcc_unreachable ();
2204 }
24b97832 2205 struct_loc = c_parser_peek_token (parser)->location;
27bf414c
JM
2206 c_parser_consume_token (parser);
2207 attrs = c_parser_attributes (parser);
c2255bc4 2208
5af28c74
TT
2209 /* Set the location in case we create a decl now. */
2210 c_parser_set_source_position_from_token (c_parser_peek_token (parser));
c2255bc4 2211
27bf414c
JM
2212 if (c_parser_next_token_is (parser, CPP_NAME))
2213 {
2214 ident = c_parser_peek_token (parser)->value;
24b97832
ILT
2215 ident_loc = c_parser_peek_token (parser)->location;
2216 struct_loc = ident_loc;
27bf414c
JM
2217 c_parser_consume_token (parser);
2218 }
2219 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
2220 {
2221 /* Parse a struct or union definition. Start the scope of the
2222 tag before parsing components. */
dc491a25
ILT
2223 struct c_struct_parse_info *struct_info;
2224 tree type = start_struct (struct_loc, code, ident, &struct_info);
27bf414c
JM
2225 tree postfix_attrs;
2226 /* We chain the components in reverse order, then put them in
2227 forward order at the end. Each struct-declaration may
2228 declare multiple components (comma-separated), so we must use
2229 chainon to join them, although when parsing each
2230 struct-declaration we can use TREE_CHAIN directly.
2231
2232 The theory behind all this is that there will be more
2233 semicolon separated fields than comma separated fields, and
2234 so we'll be minimizing the number of node traversals required
2235 by chainon. */
2236 tree contents = NULL_TREE;
2237 c_parser_consume_token (parser);
2238 /* Handle the Objective-C @defs construct,
2239 e.g. foo(sizeof(struct{ @defs(ClassName) }));. */
2240 if (c_parser_next_token_is_keyword (parser, RID_AT_DEFS))
2241 {
2242 tree name;
2243 gcc_assert (c_dialect_objc ());
2244 c_parser_consume_token (parser);
2245 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2246 goto end_at_defs;
2247 if (c_parser_next_token_is (parser, CPP_NAME)
2248 && c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME)
2249 {
2250 name = c_parser_peek_token (parser)->value;
2251 c_parser_consume_token (parser);
2252 }
2253 else
2254 {
2255 c_parser_error (parser, "expected class name");
2256 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
2257 goto end_at_defs;
2258 }
2259 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2260 "expected %<)%>");
2261 contents = nreverse (objc_get_class_ivars (name));
2262 }
2263 end_at_defs:
2264 /* Parse the struct-declarations and semicolons. Problems with
2265 semicolons are diagnosed here; empty structures are diagnosed
2266 elsewhere. */
2267 while (true)
2268 {
2269 tree decls;
2270 /* Parse any stray semicolon. */
2271 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
2272 {
b8698a0f 2273 pedwarn (c_parser_peek_token (parser)->location, OPT_pedantic,
509c9d60 2274 "extra semicolon in struct or union specified");
27bf414c
JM
2275 c_parser_consume_token (parser);
2276 continue;
2277 }
2278 /* Stop if at the end of the struct or union contents. */
2279 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2280 {
2281 c_parser_consume_token (parser);
2282 break;
2283 }
bc4071dd
RH
2284 /* Accept #pragmas at struct scope. */
2285 if (c_parser_next_token_is (parser, CPP_PRAGMA))
2286 {
2287 c_parser_pragma (parser, pragma_external);
2288 continue;
2289 }
27bf414c
JM
2290 /* Parse some comma-separated declarations, but not the
2291 trailing semicolon if any. */
2292 decls = c_parser_struct_declaration (parser);
2293 contents = chainon (decls, contents);
2294 /* If no semicolon follows, either we have a parse error or
2295 are at the end of the struct or union and should
2296 pedwarn. */
2297 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
2298 c_parser_consume_token (parser);
2299 else
2300 {
2301 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
b8698a0f 2302 pedwarn (c_parser_peek_token (parser)->location, 0,
509c9d60 2303 "no semicolon at end of struct or union");
27bf414c
JM
2304 else
2305 {
2306 c_parser_error (parser, "expected %<;%>");
2307 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
2308 break;
2309 }
2310 }
2311 }
2312 postfix_attrs = c_parser_attributes (parser);
c2255bc4 2313 ret.spec = finish_struct (struct_loc, type, nreverse (contents),
dc491a25 2314 chainon (attrs, postfix_attrs), struct_info);
27bf414c 2315 ret.kind = ctsk_tagdef;
928c19bb
JM
2316 ret.expr = NULL_TREE;
2317 ret.expr_const_operands = true;
27bf414c
JM
2318 return ret;
2319 }
2320 else if (!ident)
2321 {
2322 c_parser_error (parser, "expected %<{%>");
2323 ret.spec = error_mark_node;
2324 ret.kind = ctsk_tagref;
928c19bb
JM
2325 ret.expr = NULL_TREE;
2326 ret.expr_const_operands = true;
67c2939d 2327 return ret;
27bf414c 2328 }
c2255bc4 2329 ret = parser_xref_tag (ident_loc, code, ident);
27bf414c
JM
2330 return ret;
2331}
2332
2333/* Parse a struct-declaration (C90 6.5.2.1, C99 6.7.2.1), *without*
2334 the trailing semicolon.
2335
2336 struct-declaration:
2337 specifier-qualifier-list struct-declarator-list
32912286 2338 static_assert-declaration-no-semi
27bf414c
JM
2339
2340 specifier-qualifier-list:
2341 type-specifier specifier-qualifier-list[opt]
2342 type-qualifier specifier-qualifier-list[opt]
2343 attributes specifier-qualifier-list[opt]
2344
2345 struct-declarator-list:
2346 struct-declarator
2347 struct-declarator-list , attributes[opt] struct-declarator
2348
2349 struct-declarator:
2350 declarator attributes[opt]
2351 declarator[opt] : constant-expression attributes[opt]
2352
2353 GNU extensions:
2354
2355 struct-declaration:
2356 __extension__ struct-declaration
2357 specifier-qualifier-list
2358
2359 Unlike the ISO C syntax, semicolons are handled elsewhere. The use
2360 of attributes where shown is a GNU extension. In GNU C, we accept
2361 any expression without commas in the syntax (assignment
2362 expressions, not just conditional expressions); assignment
2363 expressions will be diagnosed as non-constant. */
2364
2365static tree
2366c_parser_struct_declaration (c_parser *parser)
2367{
2368 struct c_declspecs *specs;
2369 tree prefix_attrs;
2370 tree all_prefix_attrs;
2371 tree decls;
c7412148 2372 location_t decl_loc;
27bf414c
JM
2373 if (c_parser_next_token_is_keyword (parser, RID_EXTENSION))
2374 {
2375 int ext;
2376 tree decl;
2377 ext = disable_extension_diagnostics ();
2378 c_parser_consume_token (parser);
2379 decl = c_parser_struct_declaration (parser);
2380 restore_extension_diagnostics (ext);
2381 return decl;
2382 }
32912286
JM
2383 if (c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT))
2384 {
2385 c_parser_static_assert_declaration_no_semi (parser);
2386 return NULL_TREE;
2387 }
27bf414c 2388 specs = build_null_declspecs ();
c7412148 2389 decl_loc = c_parser_peek_token (parser)->location;
27bf414c
JM
2390 c_parser_declspecs (parser, specs, false, true, true);
2391 if (parser->error)
67c2939d 2392 return NULL_TREE;
27bf414c
JM
2393 if (!specs->declspecs_seen_p)
2394 {
2395 c_parser_error (parser, "expected specifier-qualifier-list");
2396 return NULL_TREE;
2397 }
2398 finish_declspecs (specs);
b8cbdff5
JM
2399 if (c_parser_next_token_is (parser, CPP_SEMICOLON)
2400 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
27bf414c
JM
2401 {
2402 tree ret;
9e5b2115 2403 if (specs->typespec_kind == ctsk_none)
27bf414c 2404 {
b8698a0f 2405 pedwarn (decl_loc, OPT_pedantic,
509c9d60 2406 "ISO C forbids member declarations with no members");
27bf414c
JM
2407 shadow_tag_warned (specs, pedantic);
2408 ret = NULL_TREE;
2409 }
2410 else
2411 {
2412 /* Support for unnamed structs or unions as members of
2413 structs or unions (which is [a] useful and [b] supports
2414 MS P-SDK). */
b9baeecd 2415 tree attrs = NULL;
3d10ed6c
AH
2416
2417 ret = grokfield (c_parser_peek_token (parser)->location,
2418 build_id_declarator (NULL_TREE), specs,
b9baeecd 2419 NULL_TREE, &attrs);
0ad7e054
RS
2420 if (ret)
2421 decl_attributes (&ret, attrs, 0);
27bf414c
JM
2422 }
2423 return ret;
2424 }
2425 pending_xref_error ();
2426 prefix_attrs = specs->attrs;
2427 all_prefix_attrs = prefix_attrs;
2428 specs->attrs = NULL_TREE;
2429 decls = NULL_TREE;
2430 while (true)
2431 {
2432 /* Declaring one or more declarators or un-named bit-fields. */
2433 struct c_declarator *declarator;
2434 bool dummy = false;
2435 if (c_parser_next_token_is (parser, CPP_COLON))
2436 declarator = build_id_declarator (NULL_TREE);
2437 else
9e5b2115
PB
2438 declarator = c_parser_declarator (parser,
2439 specs->typespec_kind != ctsk_none,
27bf414c
JM
2440 C_DTR_NORMAL, &dummy);
2441 if (declarator == NULL)
2442 {
2443 c_parser_skip_to_end_of_block_or_statement (parser);
2444 break;
2445 }
2446 if (c_parser_next_token_is (parser, CPP_COLON)
2447 || c_parser_next_token_is (parser, CPP_COMMA)
2448 || c_parser_next_token_is (parser, CPP_SEMICOLON)
2449 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE)
2450 || c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2451 {
2452 tree postfix_attrs = NULL_TREE;
2453 tree width = NULL_TREE;
2454 tree d;
2455 if (c_parser_next_token_is (parser, CPP_COLON))
2456 {
2457 c_parser_consume_token (parser);
2458 width = c_parser_expr_no_commas (parser, NULL).value;
2459 }
2460 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2461 postfix_attrs = c_parser_attributes (parser);
3d10ed6c
AH
2462 d = grokfield (c_parser_peek_token (parser)->location,
2463 declarator, specs, width, &all_prefix_attrs);
27bf414c
JM
2464 decl_attributes (&d, chainon (postfix_attrs,
2465 all_prefix_attrs), 0);
910ad8de 2466 DECL_CHAIN (d) = decls;
27bf414c
JM
2467 decls = d;
2468 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2469 all_prefix_attrs = chainon (c_parser_attributes (parser),
2470 prefix_attrs);
2471 else
2472 all_prefix_attrs = prefix_attrs;
2473 if (c_parser_next_token_is (parser, CPP_COMMA))
2474 c_parser_consume_token (parser);
2475 else if (c_parser_next_token_is (parser, CPP_SEMICOLON)
2476 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2477 {
2478 /* Semicolon consumed in caller. */
2479 break;
2480 }
2481 else
2482 {
2483 c_parser_error (parser, "expected %<,%>, %<;%> or %<}%>");
2484 break;
2485 }
2486 }
2487 else
2488 {
2489 c_parser_error (parser,
2490 "expected %<:%>, %<,%>, %<;%>, %<}%> or "
2491 "%<__attribute__%>");
2492 break;
2493 }
2494 }
2495 return decls;
2496}
2497
2498/* Parse a typeof specifier (a GNU extension).
2499
2500 typeof-specifier:
2501 typeof ( expression )
2502 typeof ( type-name )
2503*/
2504
2505static struct c_typespec
2506c_parser_typeof_specifier (c_parser *parser)
2507{
2508 struct c_typespec ret;
2509 ret.kind = ctsk_typeof;
2510 ret.spec = error_mark_node;
928c19bb
JM
2511 ret.expr = NULL_TREE;
2512 ret.expr_const_operands = true;
27bf414c
JM
2513 gcc_assert (c_parser_next_token_is_keyword (parser, RID_TYPEOF));
2514 c_parser_consume_token (parser);
7d882b83 2515 c_inhibit_evaluation_warnings++;
27bf414c
JM
2516 in_typeof++;
2517 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2518 {
7d882b83 2519 c_inhibit_evaluation_warnings--;
27bf414c
JM
2520 in_typeof--;
2521 return ret;
2522 }
2523 if (c_parser_next_token_starts_typename (parser))
2524 {
2525 struct c_type_name *type = c_parser_type_name (parser);
7d882b83 2526 c_inhibit_evaluation_warnings--;
27bf414c
JM
2527 in_typeof--;
2528 if (type != NULL)
2529 {
928c19bb 2530 ret.spec = groktypename (type, &ret.expr, &ret.expr_const_operands);
27bf414c
JM
2531 pop_maybe_used (variably_modified_type_p (ret.spec, NULL_TREE));
2532 }
2533 }
2534 else
2535 {
52ffd86e 2536 bool was_vm;
c7412148 2537 location_t here = c_parser_peek_token (parser)->location;
27bf414c 2538 struct c_expr expr = c_parser_expression (parser);
7d882b83 2539 c_inhibit_evaluation_warnings--;
27bf414c
JM
2540 in_typeof--;
2541 if (TREE_CODE (expr.value) == COMPONENT_REF
2542 && DECL_C_BIT_FIELD (TREE_OPERAND (expr.value, 1)))
3ba09659 2543 error_at (here, "%<typeof%> applied to a bit-field");
ebfbbdc5 2544 mark_exp_read (expr.value);
27bf414c 2545 ret.spec = TREE_TYPE (expr.value);
e426b47b
NP
2546 if (c_dialect_objc()
2547 && ret.spec != error_mark_node
2548 && lookup_attribute ("objc_volatilized", TYPE_ATTRIBUTES (ret.spec)))
2549 ret.spec = build_qualified_type
2550 (ret.spec, (TYPE_QUALS (ret.spec) & ~TYPE_QUAL_VOLATILE));
52ffd86e 2551 was_vm = variably_modified_type_p (ret.spec, NULL_TREE);
928c19bb
JM
2552 /* This is returned with the type so that when the type is
2553 evaluated, this can be evaluated. */
2554 if (was_vm)
2555 ret.expr = c_fully_fold (expr.value, false, &ret.expr_const_operands);
52ffd86e 2556 pop_maybe_used (was_vm);
27bf414c
JM
2557 }
2558 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
2559 return ret;
2560}
2561
2562/* Parse a declarator, possibly an abstract declarator (C90 6.5.4,
2563 6.5.5, C99 6.7.5, 6.7.6). If TYPE_SEEN_P then a typedef name may
2564 be redeclared; otherwise it may not. KIND indicates which kind of
2565 declarator is wanted. Returns a valid declarator except in the
2566 case of a syntax error in which case NULL is returned. *SEEN_ID is
2567 set to true if an identifier being declared is seen; this is used
2568 to diagnose bad forms of abstract array declarators and to
2569 determine whether an identifier list is syntactically permitted.
2570
2571 declarator:
2572 pointer[opt] direct-declarator
2573
2574 direct-declarator:
2575 identifier
2576 ( attributes[opt] declarator )
2577 direct-declarator array-declarator
2578 direct-declarator ( parameter-type-list )
2579 direct-declarator ( identifier-list[opt] )
2580
2581 pointer:
2582 * type-qualifier-list[opt]
2583 * type-qualifier-list[opt] pointer
2584
2585 type-qualifier-list:
2586 type-qualifier
2587 attributes
2588 type-qualifier-list type-qualifier
2589 type-qualifier-list attributes
2590
2591 parameter-type-list:
2592 parameter-list
2593 parameter-list , ...
2594
2595 parameter-list:
2596 parameter-declaration
2597 parameter-list , parameter-declaration
2598
2599 parameter-declaration:
2600 declaration-specifiers declarator attributes[opt]
2601 declaration-specifiers abstract-declarator[opt] attributes[opt]
2602
2603 identifier-list:
2604 identifier
2605 identifier-list , identifier
2606
2607 abstract-declarator:
2608 pointer
2609 pointer[opt] direct-abstract-declarator
2610
2611 direct-abstract-declarator:
2612 ( attributes[opt] abstract-declarator )
2613 direct-abstract-declarator[opt] array-declarator
2614 direct-abstract-declarator[opt] ( parameter-type-list[opt] )
2615
2616 GNU extensions:
2617
2618 direct-declarator:
2619 direct-declarator ( parameter-forward-declarations
2620 parameter-type-list[opt] )
2621
2622 direct-abstract-declarator:
c22cacf3 2623 direct-abstract-declarator[opt] ( parameter-forward-declarations
27bf414c
JM
2624 parameter-type-list[opt] )
2625
2626 parameter-forward-declarations:
2627 parameter-list ;
2628 parameter-forward-declarations parameter-list ;
2629
2630 The uses of attributes shown above are GNU extensions.
2631
2632 Some forms of array declarator are not included in C99 in the
2633 syntax for abstract declarators; these are disallowed elsewhere.
2634 This may be a defect (DR#289).
2635
2636 This function also accepts an omitted abstract declarator as being
2637 an abstract declarator, although not part of the formal syntax. */
2638
2639static struct c_declarator *
2640c_parser_declarator (c_parser *parser, bool type_seen_p, c_dtr_syn kind,
2641 bool *seen_id)
2642{
2643 /* Parse any initial pointer part. */
2644 if (c_parser_next_token_is (parser, CPP_MULT))
2645 {
2646 struct c_declspecs *quals_attrs = build_null_declspecs ();
2647 struct c_declarator *inner;
2648 c_parser_consume_token (parser);
2649 c_parser_declspecs (parser, quals_attrs, false, false, true);
2650 inner = c_parser_declarator (parser, type_seen_p, kind, seen_id);
2651 if (inner == NULL)
2652 return NULL;
2653 else
2654 return make_pointer_declarator (quals_attrs, inner);
2655 }
2656 /* Now we have a direct declarator, direct abstract declarator or
2657 nothing (which counts as a direct abstract declarator here). */
2658 return c_parser_direct_declarator (parser, type_seen_p, kind, seen_id);
2659}
2660
2661/* Parse a direct declarator or direct abstract declarator; arguments
2662 as c_parser_declarator. */
2663
2664static struct c_declarator *
2665c_parser_direct_declarator (c_parser *parser, bool type_seen_p, c_dtr_syn kind,
2666 bool *seen_id)
2667{
2668 /* The direct declarator must start with an identifier (possibly
2669 omitted) or a parenthesized declarator (possibly abstract). In
2670 an ordinary declarator, initial parentheses must start a
2671 parenthesized declarator. In an abstract declarator or parameter
2672 declarator, they could start a parenthesized declarator or a
2673 parameter list. To tell which, the open parenthesis and any
2674 following attributes must be read. If a declaration specifier
2675 follows, then it is a parameter list; if the specifier is a
2676 typedef name, there might be an ambiguity about redeclaring it,
2677 which is resolved in the direction of treating it as a typedef
2678 name. If a close parenthesis follows, it is also an empty
2679 parameter list, as the syntax does not permit empty abstract
0fa2e4df 2680 declarators. Otherwise, it is a parenthesized declarator (in
27bf414c
JM
2681 which case the analysis may be repeated inside it, recursively).
2682
2683 ??? There is an ambiguity in a parameter declaration "int
2684 (__attribute__((foo)) x)", where x is not a typedef name: it
2685 could be an abstract declarator for a function, or declare x with
2686 parentheses. The proper resolution of this ambiguity needs
2687 documenting. At present we follow an accident of the old
2688 parser's implementation, whereby the first parameter must have
2689 some declaration specifiers other than just attributes. Thus as
0fa2e4df 2690 a parameter declaration it is treated as a parenthesized
27bf414c
JM
2691 parameter named x, and as an abstract declarator it is
2692 rejected.
2693
2694 ??? Also following the old parser, attributes inside an empty
2695 parameter list are ignored, making it a list not yielding a
2696 prototype, rather than giving an error or making it have one
2697 parameter with implicit type int.
2698
2699 ??? Also following the old parser, typedef names may be
2700 redeclared in declarators, but not Objective-C class names. */
2701
2702 if (kind != C_DTR_ABSTRACT
2703 && c_parser_next_token_is (parser, CPP_NAME)
2704 && ((type_seen_p
a6341d57
NP
2705 && (c_parser_peek_token (parser)->id_kind == C_ID_TYPENAME
2706 || c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME))
27bf414c
JM
2707 || c_parser_peek_token (parser)->id_kind == C_ID_ID))
2708 {
2709 struct c_declarator *inner
2710 = build_id_declarator (c_parser_peek_token (parser)->value);
2711 *seen_id = true;
6037d88d 2712 inner->id_loc = c_parser_peek_token (parser)->location;
27bf414c
JM
2713 c_parser_consume_token (parser);
2714 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
2715 }
2716
2717 if (kind != C_DTR_NORMAL
2718 && c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
2719 {
2720 struct c_declarator *inner = build_id_declarator (NULL_TREE);
2721 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
2722 }
2723
2724 /* Either we are at the end of an abstract declarator, or we have
2725 parentheses. */
2726
2727 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
2728 {
2729 tree attrs;
2730 struct c_declarator *inner;
2731 c_parser_consume_token (parser);
2732 attrs = c_parser_attributes (parser);
2733 if (kind != C_DTR_NORMAL
2734 && (c_parser_next_token_starts_declspecs (parser)
2735 || c_parser_next_token_is (parser, CPP_CLOSE_PAREN)))
2736 {
2737 struct c_arg_info *args
2738 = c_parser_parms_declarator (parser, kind == C_DTR_NORMAL,
2739 attrs);
2740 if (args == NULL)
2741 return NULL;
2742 else
2743 {
2744 inner
2745 = build_function_declarator (args,
2746 build_id_declarator (NULL_TREE));
2747 return c_parser_direct_declarator_inner (parser, *seen_id,
2748 inner);
2749 }
2750 }
2751 /* A parenthesized declarator. */
2752 inner = c_parser_declarator (parser, type_seen_p, kind, seen_id);
2753 if (inner != NULL && attrs != NULL)
2754 inner = build_attrs_declarator (attrs, inner);
2755 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2756 {
2757 c_parser_consume_token (parser);
2758 if (inner == NULL)
2759 return NULL;
2760 else
2761 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
2762 }
2763 else
2764 {
2765 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2766 "expected %<)%>");
2767 return NULL;
2768 }
2769 }
2770 else
2771 {
2772 if (kind == C_DTR_NORMAL)
2773 {
2774 c_parser_error (parser, "expected identifier or %<(%>");
2775 return NULL;
2776 }
2777 else
2778 return build_id_declarator (NULL_TREE);
2779 }
2780}
2781
2782/* Parse part of a direct declarator or direct abstract declarator,
2783 given that some (in INNER) has already been parsed; ID_PRESENT is
2784 true if an identifier is present, false for an abstract
2785 declarator. */
2786
2787static struct c_declarator *
2788c_parser_direct_declarator_inner (c_parser *parser, bool id_present,
2789 struct c_declarator *inner)
2790{
2791 /* Parse a sequence of array declarators and parameter lists. */
2792 if (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
2793 {
b8698a0f 2794 location_t brace_loc = c_parser_peek_token (parser)->location;
27bf414c
JM
2795 struct c_declarator *declarator;
2796 struct c_declspecs *quals_attrs = build_null_declspecs ();
2797 bool static_seen;
2798 bool star_seen;
2799 tree dimen;
2800 c_parser_consume_token (parser);
2801 c_parser_declspecs (parser, quals_attrs, false, false, true);
2802 static_seen = c_parser_next_token_is_keyword (parser, RID_STATIC);
2803 if (static_seen)
2804 c_parser_consume_token (parser);
2805 if (static_seen && !quals_attrs->declspecs_seen_p)
2806 c_parser_declspecs (parser, quals_attrs, false, false, true);
2807 if (!quals_attrs->declspecs_seen_p)
2808 quals_attrs = NULL;
2809 /* If "static" is present, there must be an array dimension.
2810 Otherwise, there may be a dimension, "*", or no
2811 dimension. */
2812 if (static_seen)
2813 {
2814 star_seen = false;
2815 dimen = c_parser_expr_no_commas (parser, NULL).value;
2816 }
2817 else
2818 {
2819 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
2820 {
2821 dimen = NULL_TREE;
2822 star_seen = false;
2823 }
2824 else if (c_parser_next_token_is (parser, CPP_MULT))
2825 {
2826 if (c_parser_peek_2nd_token (parser)->type == CPP_CLOSE_SQUARE)
2827 {
2828 dimen = NULL_TREE;
2829 star_seen = true;
2830 c_parser_consume_token (parser);
2831 }
2832 else
2833 {
2834 star_seen = false;
2835 dimen = c_parser_expr_no_commas (parser, NULL).value;
2836 }
2837 }
2838 else
2839 {
2840 star_seen = false;
2841 dimen = c_parser_expr_no_commas (parser, NULL).value;
2842 }
2843 }
2844 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
2845 c_parser_consume_token (parser);
2846 else
2847 {
2848 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
2849 "expected %<]%>");
2850 return NULL;
2851 }
97e3c923
JJ
2852 if (dimen)
2853 mark_exp_read (dimen);
c2255bc4
AH
2854 declarator = build_array_declarator (brace_loc, dimen, quals_attrs,
2855 static_seen, star_seen);
52ffd86e
MS
2856 if (declarator == NULL)
2857 return NULL;
6ac0194d 2858 inner = set_array_declarator_inner (declarator, inner);
27bf414c
JM
2859 return c_parser_direct_declarator_inner (parser, id_present, inner);
2860 }
2861 else if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
2862 {
2863 tree attrs;
2864 struct c_arg_info *args;
2865 c_parser_consume_token (parser);
2866 attrs = c_parser_attributes (parser);
2867 args = c_parser_parms_declarator (parser, id_present, attrs);
2868 if (args == NULL)
2869 return NULL;
2870 else
2871 {
2872 inner = build_function_declarator (args, inner);
2873 return c_parser_direct_declarator_inner (parser, id_present, inner);
2874 }
2875 }
2876 return inner;
2877}
2878
2879/* Parse a parameter list or identifier list, including the closing
2880 parenthesis but not the opening one. ATTRS are the attributes at
2881 the start of the list. ID_LIST_OK is true if an identifier list is
2882 acceptable; such a list must not have attributes at the start. */
2883
2884static struct c_arg_info *
2885c_parser_parms_declarator (c_parser *parser, bool id_list_ok, tree attrs)
2886{
2887 push_scope ();
2888 declare_parm_level ();
2889 /* If the list starts with an identifier, it is an identifier list.
2890 Otherwise, it is either a prototype list or an empty list. */
2891 if (id_list_ok
2892 && !attrs
2893 && c_parser_next_token_is (parser, CPP_NAME)
2894 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
2895 {
7fa3585c 2896 tree list = NULL_TREE, *nextp = &list;
27bf414c
JM
2897 while (c_parser_next_token_is (parser, CPP_NAME)
2898 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
2899 {
7fa3585c
GK
2900 *nextp = build_tree_list (NULL_TREE,
2901 c_parser_peek_token (parser)->value);
2902 nextp = & TREE_CHAIN (*nextp);
27bf414c
JM
2903 c_parser_consume_token (parser);
2904 if (c_parser_next_token_is_not (parser, CPP_COMMA))
2905 break;
2906 c_parser_consume_token (parser);
2907 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2908 {
2909 c_parser_error (parser, "expected identifier");
2910 break;
2911 }
2912 }
2913 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2914 {
b3399d18 2915 struct c_arg_info *ret = build_arg_info ();
27bf414c 2916 ret->types = list;
27bf414c
JM
2917 c_parser_consume_token (parser);
2918 pop_scope ();
2919 return ret;
2920 }
2921 else
2922 {
2923 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2924 "expected %<)%>");
2925 pop_scope ();
2926 return NULL;
2927 }
2928 }
2929 else
2930 {
2931 struct c_arg_info *ret = c_parser_parms_list_declarator (parser, attrs);
2932 pop_scope ();
2933 return ret;
2934 }
2935}
2936
2937/* Parse a parameter list (possibly empty), including the closing
2938 parenthesis but not the opening one. ATTRS are the attributes at
2939 the start of the list. */
2940
2941static struct c_arg_info *
2942c_parser_parms_list_declarator (c_parser *parser, tree attrs)
2943{
09a1e889 2944 bool bad_parm = false;
27bf414c
JM
2945 /* ??? Following the old parser, forward parameter declarations may
2946 use abstract declarators, and if no real parameter declarations
2947 follow the forward declarations then this is not diagnosed. Also
2948 note as above that attributes are ignored as the only contents of
2949 the parentheses, or as the only contents after forward
2950 declarations. */
2951 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2952 {
b3399d18 2953 struct c_arg_info *ret = build_arg_info ();
27bf414c
JM
2954 c_parser_consume_token (parser);
2955 return ret;
2956 }
2957 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
2958 {
b3399d18 2959 struct c_arg_info *ret = build_arg_info ();
27bf414c
JM
2960 /* Suppress -Wold-style-definition for this case. */
2961 ret->types = error_mark_node;
3ba09659
AH
2962 error_at (c_parser_peek_token (parser)->location,
2963 "ISO C requires a named argument before %<...%>");
27bf414c
JM
2964 c_parser_consume_token (parser);
2965 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2966 {
2967 c_parser_consume_token (parser);
2968 return ret;
2969 }
2970 else
2971 {
2972 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2973 "expected %<)%>");
2974 return NULL;
2975 }
2976 }
2977 /* Nonempty list of parameters, either terminated with semicolon
2978 (forward declarations; recurse) or with close parenthesis (normal
2979 function) or with ", ... )" (variadic function). */
2980 while (true)
2981 {
2982 /* Parse a parameter. */
2983 struct c_parm *parm = c_parser_parameter_declaration (parser, attrs);
2984 attrs = NULL_TREE;
09a1e889
SZ
2985 if (parm == NULL)
2986 bad_parm = true;
2987 else
2988 push_parm_decl (parm);
27bf414c
JM
2989 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
2990 {
2991 tree new_attrs;
2992 c_parser_consume_token (parser);
91d975b8 2993 mark_forward_parm_decls ();
27bf414c
JM
2994 new_attrs = c_parser_attributes (parser);
2995 return c_parser_parms_list_declarator (parser, new_attrs);
2996 }
2997 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2998 {
2999 c_parser_consume_token (parser);
09a1e889 3000 if (bad_parm)
27bf414c 3001 {
09a1e889
SZ
3002 get_pending_sizes ();
3003 return NULL;
27bf414c 3004 }
09a1e889
SZ
3005 else
3006 return get_parm_info (false);
27bf414c
JM
3007 }
3008 if (!c_parser_require (parser, CPP_COMMA,
3009 "expected %<;%>, %<,%> or %<)%>"))
3010 {
3011 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
f007042e 3012 get_pending_sizes ();
27bf414c
JM
3013 return NULL;
3014 }
3015 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
3016 {
3017 c_parser_consume_token (parser);
3018 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3019 {
3020 c_parser_consume_token (parser);
09a1e889 3021 if (bad_parm)
27bf414c 3022 {
09a1e889
SZ
3023 get_pending_sizes ();
3024 return NULL;
27bf414c 3025 }
09a1e889
SZ
3026 else
3027 return get_parm_info (true);
27bf414c
JM
3028 }
3029 else
3030 {
3031 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3032 "expected %<)%>");
f007042e 3033 get_pending_sizes ();
27bf414c
JM
3034 return NULL;
3035 }
3036 }
3037 }
3038}
3039
3040/* Parse a parameter declaration. ATTRS are the attributes at the
3041 start of the declaration if it is the first parameter. */
3042
3043static struct c_parm *
3044c_parser_parameter_declaration (c_parser *parser, tree attrs)
3045{
3046 struct c_declspecs *specs;
3047 struct c_declarator *declarator;
3048 tree prefix_attrs;
3049 tree postfix_attrs = NULL_TREE;
3050 bool dummy = false;
3051 if (!c_parser_next_token_starts_declspecs (parser))
3052 {
09a1e889
SZ
3053 c_token *token = c_parser_peek_token (parser);
3054 if (parser->error)
3055 return NULL;
3056 c_parser_set_source_position_from_token (token);
3057 if (token->type == CPP_NAME
3058 && c_parser_peek_2nd_token (parser)->type != CPP_COMMA
3059 && c_parser_peek_2nd_token (parser)->type != CPP_CLOSE_PAREN)
3060 {
3061 error ("unknown type name %qE", token->value);
3062 parser->error = true;
3063 }
27bf414c
JM
3064 /* ??? In some Objective-C cases '...' isn't applicable so there
3065 should be a different message. */
09a1e889
SZ
3066 else
3067 c_parser_error (parser,
3068 "expected declaration specifiers or %<...%>");
27bf414c
JM
3069 c_parser_skip_to_end_of_parameter (parser);
3070 return NULL;
3071 }
3072 specs = build_null_declspecs ();
3073 if (attrs)
3074 {
3075 declspecs_add_attrs (specs, attrs);
3076 attrs = NULL_TREE;
3077 }
3078 c_parser_declspecs (parser, specs, true, true, true);
3079 finish_declspecs (specs);
3080 pending_xref_error ();
3081 prefix_attrs = specs->attrs;
3082 specs->attrs = NULL_TREE;
9e5b2115
PB
3083 declarator = c_parser_declarator (parser,
3084 specs->typespec_kind != ctsk_none,
27bf414c
JM
3085 C_DTR_PARM, &dummy);
3086 if (declarator == NULL)
3087 {
3088 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
3089 return NULL;
3090 }
3091 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
3092 postfix_attrs = c_parser_attributes (parser);
3093 return build_c_parm (specs, chainon (postfix_attrs, prefix_attrs),
3094 declarator);
3095}
3096
3097/* Parse a string literal in an asm expression. It should not be
3098 translated, and wide string literals are an error although
3099 permitted by the syntax. This is a GNU extension.
3100
3101 asm-string-literal:
3102 string-literal
3103
3104 ??? At present, following the old parser, the caller needs to have
46c2514e
TT
3105 set lex_untranslated_string to 1. It would be better to follow the
3106 C++ parser rather than using this kludge. */
27bf414c
JM
3107
3108static tree
3109c_parser_asm_string_literal (c_parser *parser)
3110{
3111 tree str;
3112 if (c_parser_next_token_is (parser, CPP_STRING))
3113 {
3114 str = c_parser_peek_token (parser)->value;
3115 c_parser_consume_token (parser);
3116 }
3117 else if (c_parser_next_token_is (parser, CPP_WSTRING))
3118 {
3ba09659
AH
3119 error_at (c_parser_peek_token (parser)->location,
3120 "wide string literal in %<asm%>");
27bf414c
JM
3121 str = build_string (1, "");
3122 c_parser_consume_token (parser);
3123 }
3124 else
3125 {
3126 c_parser_error (parser, "expected string literal");
3127 str = NULL_TREE;
3128 }
3129 return str;
3130}
3131
3132/* Parse a simple asm expression. This is used in restricted
3133 contexts, where a full expression with inputs and outputs does not
3134 make sense. This is a GNU extension.
3135
3136 simple-asm-expr:
3137 asm ( asm-string-literal )
3138*/
3139
3140static tree
3141c_parser_simple_asm_expr (c_parser *parser)
3142{
3143 tree str;
3144 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ASM));
3145 /* ??? Follow the C++ parser rather than using the
46c2514e
TT
3146 lex_untranslated_string kludge. */
3147 parser->lex_untranslated_string = true;
27bf414c
JM
3148 c_parser_consume_token (parser);
3149 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3150 {
46c2514e 3151 parser->lex_untranslated_string = false;
27bf414c
JM
3152 return NULL_TREE;
3153 }
3154 str = c_parser_asm_string_literal (parser);
46c2514e 3155 parser->lex_untranslated_string = false;
27bf414c
JM
3156 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
3157 {
3158 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
3159 return NULL_TREE;
3160 }
3161 return str;
3162}
3163
3164/* Parse (possibly empty) attributes. This is a GNU extension.
3165
3166 attributes:
3167 empty
3168 attributes attribute
3169
3170 attribute:
3171 __attribute__ ( ( attribute-list ) )
3172
3173 attribute-list:
3174 attrib
3175 attribute_list , attrib
3176
3177 attrib:
3178 empty
3179 any-word
3180 any-word ( identifier )
3181 any-word ( identifier , nonempty-expr-list )
3182 any-word ( expr-list )
3183
3184 where the "identifier" must not be declared as a type, and
3185 "any-word" may be any identifier (including one declared as a
3186 type), a reserved word storage class specifier, type specifier or
3187 type qualifier. ??? This still leaves out most reserved keywords
3188 (following the old parser), shouldn't we include them, and why not
3189 allow identifiers declared as types to start the arguments? */
3190
3191static tree
3192c_parser_attributes (c_parser *parser)
3193{
3194 tree attrs = NULL_TREE;
3195 while (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
3196 {
3197 /* ??? Follow the C++ parser rather than using the
46c2514e
TT
3198 lex_untranslated_string kludge. */
3199 parser->lex_untranslated_string = true;
27bf414c
JM
3200 c_parser_consume_token (parser);
3201 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3202 {
46c2514e 3203 parser->lex_untranslated_string = false;
27bf414c
JM
3204 return attrs;
3205 }
3206 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3207 {
46c2514e 3208 parser->lex_untranslated_string = false;
27bf414c
JM
3209 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
3210 return attrs;
3211 }
3212 /* Parse the attribute list. */
3213 while (c_parser_next_token_is (parser, CPP_COMMA)
3214 || c_parser_next_token_is (parser, CPP_NAME)
3215 || c_parser_next_token_is (parser, CPP_KEYWORD))
3216 {
3217 tree attr, attr_name, attr_args;
bbbbb16a 3218 VEC(tree,gc) *expr_list;
27bf414c
JM
3219 if (c_parser_next_token_is (parser, CPP_COMMA))
3220 {
3221 c_parser_consume_token (parser);
3222 continue;
3223 }
3224 if (c_parser_next_token_is (parser, CPP_KEYWORD))
3225 {
3226 /* ??? See comment above about what keywords are
3227 accepted here. */
3228 bool ok;
3229 switch (c_parser_peek_token (parser)->keyword)
3230 {
3231 case RID_STATIC:
3232 case RID_UNSIGNED:
3233 case RID_LONG:
a6766312 3234 case RID_INT128:
27bf414c
JM
3235 case RID_CONST:
3236 case RID_EXTERN:
3237 case RID_REGISTER:
3238 case RID_TYPEDEF:
3239 case RID_SHORT:
3240 case RID_INLINE:
3241 case RID_VOLATILE:
3242 case RID_SIGNED:
3243 case RID_AUTO:
3244 case RID_RESTRICT:
3245 case RID_COMPLEX:
3246 case RID_THREAD:
3247 case RID_INT:
3248 case RID_CHAR:
3249 case RID_FLOAT:
3250 case RID_DOUBLE:
3251 case RID_VOID:
9a8ce21f
JG
3252 case RID_DFLOAT32:
3253 case RID_DFLOAT64:
3254 case RID_DFLOAT128:
27bf414c 3255 case RID_BOOL:
ab22c1fa
CF
3256 case RID_FRACT:
3257 case RID_ACCUM:
3258 case RID_SAT:
27bf414c
JM
3259 ok = true;
3260 break;
3261 default:
3262 ok = false;
3263 break;
3264 }
3265 if (!ok)
3266 break;
5dc4a7f4
JJ
3267 /* Accept __attribute__((__const)) as __attribute__((const))
3268 etc. */
3269 attr_name
3270 = ridpointers[(int) c_parser_peek_token (parser)->keyword];
27bf414c 3271 }
5dc4a7f4
JJ
3272 else
3273 attr_name = c_parser_peek_token (parser)->value;
27bf414c
JM
3274 c_parser_consume_token (parser);
3275 if (c_parser_next_token_is_not (parser, CPP_OPEN_PAREN))
3276 {
3277 attr = build_tree_list (attr_name, NULL_TREE);
3278 attrs = chainon (attrs, attr);
3279 continue;
3280 }
3281 c_parser_consume_token (parser);
3282 /* Parse the attribute contents. If they start with an
3283 identifier which is followed by a comma or close
3284 parenthesis, then the arguments start with that
91ebb981
IS
3285 identifier; otherwise they are an expression list.
3286 In objective-c the identifier may be a classname. */
27bf414c 3287 if (c_parser_next_token_is (parser, CPP_NAME)
91ebb981
IS
3288 && (c_parser_peek_token (parser)->id_kind == C_ID_ID
3289 || (c_dialect_objc ()
3290 && c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME))
27bf414c
JM
3291 && ((c_parser_peek_2nd_token (parser)->type == CPP_COMMA)
3292 || (c_parser_peek_2nd_token (parser)->type
3293 == CPP_CLOSE_PAREN)))
3294 {
3295 tree arg1 = c_parser_peek_token (parser)->value;
3296 c_parser_consume_token (parser);
3297 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3298 attr_args = build_tree_list (NULL_TREE, arg1);
3299 else
3300 {
bbbbb16a 3301 tree tree_list;
27bf414c 3302 c_parser_consume_token (parser);
bbbbb16a 3303 expr_list = c_parser_expr_list (parser, false, true, NULL);
c166b898 3304 tree_list = build_tree_list_vec (expr_list);
bbbbb16a 3305 attr_args = tree_cons (NULL_TREE, arg1, tree_list);
c166b898 3306 release_tree_vector (expr_list);
27bf414c
JM
3307 }
3308 }
3309 else
3310 {
3311 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3312 attr_args = NULL_TREE;
3313 else
bbbbb16a
ILT
3314 {
3315 expr_list = c_parser_expr_list (parser, false, true, NULL);
c166b898
ILT
3316 attr_args = build_tree_list_vec (expr_list);
3317 release_tree_vector (expr_list);
bbbbb16a 3318 }
27bf414c
JM
3319 }
3320 attr = build_tree_list (attr_name, attr_args);
3321 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3322 c_parser_consume_token (parser);
3323 else
3324 {
46c2514e 3325 parser->lex_untranslated_string = false;
27bf414c
JM
3326 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3327 "expected %<)%>");
3328 return attrs;
3329 }
3330 attrs = chainon (attrs, attr);
3331 }
3332 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3333 c_parser_consume_token (parser);
3334 else
3335 {
46c2514e 3336 parser->lex_untranslated_string = false;
27bf414c
JM
3337 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3338 "expected %<)%>");
3339 return attrs;
3340 }
3341 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3342 c_parser_consume_token (parser);
3343 else
3344 {
46c2514e 3345 parser->lex_untranslated_string = false;
27bf414c
JM
3346 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3347 "expected %<)%>");
3348 return attrs;
3349 }
46c2514e 3350 parser->lex_untranslated_string = false;
27bf414c
JM
3351 }
3352 return attrs;
3353}
3354
3355/* Parse a type name (C90 6.5.5, C99 6.7.6).
3356
3357 type-name:
3358 specifier-qualifier-list abstract-declarator[opt]
3359*/
3360
3361static struct c_type_name *
3362c_parser_type_name (c_parser *parser)
3363{
3364 struct c_declspecs *specs = build_null_declspecs ();
3365 struct c_declarator *declarator;
3366 struct c_type_name *ret;
3367 bool dummy = false;
3368 c_parser_declspecs (parser, specs, false, true, true);
3369 if (!specs->declspecs_seen_p)
3370 {
3371 c_parser_error (parser, "expected specifier-qualifier-list");
3372 return NULL;
3373 }
3374 pending_xref_error ();
3375 finish_declspecs (specs);
9e5b2115
PB
3376 declarator = c_parser_declarator (parser,
3377 specs->typespec_kind != ctsk_none,
27bf414c
JM
3378 C_DTR_ABSTRACT, &dummy);
3379 if (declarator == NULL)
3380 return NULL;
3381 ret = XOBNEW (&parser_obstack, struct c_type_name);
3382 ret->specs = specs;
3383 ret->declarator = declarator;
3384 return ret;
3385}
3386
3387/* Parse an initializer (C90 6.5.7, C99 6.7.8).
3388
3389 initializer:
3390 assignment-expression
3391 { initializer-list }
3392 { initializer-list , }
3393
3394 initializer-list:
3395 designation[opt] initializer
3396 initializer-list , designation[opt] initializer
3397
3398 designation:
3399 designator-list =
3400
3401 designator-list:
3402 designator
3403 designator-list designator
3404
3405 designator:
3406 array-designator
3407 . identifier
3408
3409 array-designator:
3410 [ constant-expression ]
3411
3412 GNU extensions:
3413
3414 initializer:
3415 { }
3416
3417 designation:
3418 array-designator
3419 identifier :
3420
3421 array-designator:
3422 [ constant-expression ... constant-expression ]
3423
3424 Any expression without commas is accepted in the syntax for the
3425 constant-expressions, with non-constant expressions rejected later.
3426
3427 This function is only used for top-level initializers; for nested
3428 ones, see c_parser_initval. */
3429
3430static struct c_expr
3431c_parser_initializer (c_parser *parser)
3432{
3433 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
3434 return c_parser_braced_init (parser, NULL_TREE, false);
3435 else
46bdb9cf
JM
3436 {
3437 struct c_expr ret;
c2255bc4 3438 location_t loc = c_parser_peek_token (parser)->location;
46bdb9cf
JM
3439 ret = c_parser_expr_no_commas (parser, NULL);
3440 if (TREE_CODE (ret.value) != STRING_CST
3441 && TREE_CODE (ret.value) != COMPOUND_LITERAL_EXPR)
ebfbbdc5 3442 ret = default_function_array_read_conversion (loc, ret);
46bdb9cf
JM
3443 return ret;
3444 }
27bf414c
JM
3445}
3446
3447/* Parse a braced initializer list. TYPE is the type specified for a
3448 compound literal, and NULL_TREE for other initializers and for
3449 nested braced lists. NESTED_P is true for nested braced lists,
3450 false for the list of a compound literal or the list that is the
3451 top-level initializer in a declaration. */
3452
3453static struct c_expr
3454c_parser_braced_init (c_parser *parser, tree type, bool nested_p)
3455{
a1e3b3d9
LB
3456 struct c_expr ret;
3457 struct obstack braced_init_obstack;
c7412148 3458 location_t brace_loc = c_parser_peek_token (parser)->location;
a1e3b3d9 3459 gcc_obstack_init (&braced_init_obstack);
27bf414c
JM
3460 gcc_assert (c_parser_next_token_is (parser, CPP_OPEN_BRACE));
3461 c_parser_consume_token (parser);
3462 if (nested_p)
a1e3b3d9 3463 push_init_level (0, &braced_init_obstack);
27bf414c
JM
3464 else
3465 really_start_incremental_init (type);
3466 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3467 {
509c9d60 3468 pedwarn (brace_loc, OPT_pedantic, "ISO C forbids empty initializer braces");
27bf414c
JM
3469 }
3470 else
3471 {
3472 /* Parse a non-empty initializer list, possibly with a trailing
3473 comma. */
3474 while (true)
3475 {
a1e3b3d9 3476 c_parser_initelt (parser, &braced_init_obstack);
27bf414c
JM
3477 if (parser->error)
3478 break;
3479 if (c_parser_next_token_is (parser, CPP_COMMA))
3480 c_parser_consume_token (parser);
3481 else
3482 break;
3483 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3484 break;
3485 }
3486 }
3487 if (c_parser_next_token_is_not (parser, CPP_CLOSE_BRACE))
3488 {
27bf414c
JM
3489 ret.value = error_mark_node;
3490 ret.original_code = ERROR_MARK;
6866c6e8 3491 ret.original_type = NULL;
27bf414c 3492 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, "expected %<}%>");
a1e3b3d9
LB
3493 pop_init_level (0, &braced_init_obstack);
3494 obstack_free (&braced_init_obstack, NULL);
27bf414c
JM
3495 return ret;
3496 }
3497 c_parser_consume_token (parser);
a1e3b3d9
LB
3498 ret = pop_init_level (0, &braced_init_obstack);
3499 obstack_free (&braced_init_obstack, NULL);
3500 return ret;
27bf414c
JM
3501}
3502
3503/* Parse a nested initializer, including designators. */
3504
3505static void
a1e3b3d9 3506c_parser_initelt (c_parser *parser, struct obstack * braced_init_obstack)
27bf414c
JM
3507{
3508 /* Parse any designator or designator list. A single array
3509 designator may have the subsequent "=" omitted in GNU C, but a
3510 longer list or a structure member designator may not. */
3511 if (c_parser_next_token_is (parser, CPP_NAME)
3512 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
3513 {
3514 /* Old-style structure member designator. */
a1e3b3d9
LB
3515 set_init_label (c_parser_peek_token (parser)->value,
3516 braced_init_obstack);
fcf73884 3517 /* Use the colon as the error location. */
b8698a0f 3518 pedwarn (c_parser_peek_2nd_token (parser)->location, OPT_pedantic,
509c9d60 3519 "obsolete use of designated initializer with %<:%>");
27bf414c
JM
3520 c_parser_consume_token (parser);
3521 c_parser_consume_token (parser);
3522 }
3523 else
3524 {
3525 /* des_seen is 0 if there have been no designators, 1 if there
3526 has been a single array designator and 2 otherwise. */
3527 int des_seen = 0;
c7412148 3528 /* Location of a designator. */
922f2908 3529 location_t des_loc = UNKNOWN_LOCATION; /* Quiet warning. */
27bf414c
JM
3530 while (c_parser_next_token_is (parser, CPP_OPEN_SQUARE)
3531 || c_parser_next_token_is (parser, CPP_DOT))
3532 {
3533 int des_prev = des_seen;
c7412148
TT
3534 if (!des_seen)
3535 des_loc = c_parser_peek_token (parser)->location;
27bf414c
JM
3536 if (des_seen < 2)
3537 des_seen++;
3538 if (c_parser_next_token_is (parser, CPP_DOT))
3539 {
3540 des_seen = 2;
3541 c_parser_consume_token (parser);
3542 if (c_parser_next_token_is (parser, CPP_NAME))
3543 {
a1e3b3d9
LB
3544 set_init_label (c_parser_peek_token (parser)->value,
3545 braced_init_obstack);
27bf414c
JM
3546 c_parser_consume_token (parser);
3547 }
3548 else
3549 {
3550 struct c_expr init;
3551 init.value = error_mark_node;
3552 init.original_code = ERROR_MARK;
6866c6e8 3553 init.original_type = NULL;
27bf414c
JM
3554 c_parser_error (parser, "expected identifier");
3555 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
a1e3b3d9 3556 process_init_element (init, false, braced_init_obstack);
27bf414c
JM
3557 return;
3558 }
3559 }
3560 else
3561 {
3562 tree first, second;
922f2908 3563 location_t ellipsis_loc = UNKNOWN_LOCATION; /* Quiet warning. */
27bf414c
JM
3564 /* ??? Following the old parser, [ objc-receiver
3565 objc-message-args ] is accepted as an initializer,
3566 being distinguished from a designator by what follows
3567 the first assignment expression inside the square
3568 brackets, but after a first array designator a
3569 subsequent square bracket is for Objective-C taken to
3570 start an expression, using the obsolete form of
3571 designated initializer without '=', rather than
3572 possibly being a second level of designation: in LALR
3573 terms, the '[' is shifted rather than reducing
3574 designator to designator-list. */
3575 if (des_prev == 1 && c_dialect_objc ())
3576 {
3577 des_seen = des_prev;
3578 break;
3579 }
3580 if (des_prev == 0 && c_dialect_objc ())
3581 {
3582 /* This might be an array designator or an
3583 Objective-C message expression. If the former,
3584 continue parsing here; if the latter, parse the
3585 remainder of the initializer given the starting
3586 primary-expression. ??? It might make sense to
3587 distinguish when des_prev == 1 as well; see
3588 previous comment. */
3589 tree rec, args;
3590 struct c_expr mexpr;
3591 c_parser_consume_token (parser);
3592 if (c_parser_peek_token (parser)->type == CPP_NAME
3593 && ((c_parser_peek_token (parser)->id_kind
3594 == C_ID_TYPENAME)
3595 || (c_parser_peek_token (parser)->id_kind
3596 == C_ID_CLASSNAME)))
3597 {
3598 /* Type name receiver. */
3599 tree id = c_parser_peek_token (parser)->value;
3600 c_parser_consume_token (parser);
3601 rec = objc_get_class_reference (id);
3602 goto parse_message_args;
3603 }
3604 first = c_parser_expr_no_commas (parser, NULL).value;
ebfbbdc5 3605 mark_exp_read (first);
27bf414c
JM
3606 if (c_parser_next_token_is (parser, CPP_ELLIPSIS)
3607 || c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
3608 goto array_desig_after_first;
3609 /* Expression receiver. So far only one part
3610 without commas has been parsed; there might be
3611 more of the expression. */
3612 rec = first;
3613 while (c_parser_next_token_is (parser, CPP_COMMA))
3614 {
f2a71bbc 3615 struct c_expr next;
c2255bc4
AH
3616 location_t comma_loc, exp_loc;
3617 comma_loc = c_parser_peek_token (parser)->location;
27bf414c 3618 c_parser_consume_token (parser);
c2255bc4 3619 exp_loc = c_parser_peek_token (parser)->location;
f2a71bbc 3620 next = c_parser_expr_no_commas (parser, NULL);
ebfbbdc5
JJ
3621 next = default_function_array_read_conversion (exp_loc,
3622 next);
c2255bc4 3623 rec = build_compound_expr (comma_loc, rec, next.value);
27bf414c
JM
3624 }
3625 parse_message_args:
3626 /* Now parse the objc-message-args. */
3627 args = c_parser_objc_message_args (parser);
3628 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
3629 "expected %<]%>");
3630 mexpr.value
3631 = objc_build_message_expr (build_tree_list (rec, args));
3632 mexpr.original_code = ERROR_MARK;
6866c6e8 3633 mexpr.original_type = NULL;
27bf414c
JM
3634 /* Now parse and process the remainder of the
3635 initializer, starting with this message
3636 expression as a primary-expression. */
a1e3b3d9 3637 c_parser_initval (parser, &mexpr, braced_init_obstack);
27bf414c
JM
3638 return;
3639 }
3640 c_parser_consume_token (parser);
3641 first = c_parser_expr_no_commas (parser, NULL).value;
ebfbbdc5 3642 mark_exp_read (first);
27bf414c
JM
3643 array_desig_after_first:
3644 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
3645 {
c7412148 3646 ellipsis_loc = c_parser_peek_token (parser)->location;
27bf414c
JM
3647 c_parser_consume_token (parser);
3648 second = c_parser_expr_no_commas (parser, NULL).value;
ebfbbdc5 3649 mark_exp_read (second);
27bf414c
JM
3650 }
3651 else
3652 second = NULL_TREE;
3653 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
3654 {
3655 c_parser_consume_token (parser);
a1e3b3d9 3656 set_init_index (first, second, braced_init_obstack);
fcf73884 3657 if (second)
b8698a0f 3658 pedwarn (ellipsis_loc, OPT_pedantic,
509c9d60 3659 "ISO C forbids specifying range of elements to initialize");
27bf414c
JM
3660 }
3661 else
3662 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
3663 "expected %<]%>");
3664 }
3665 }
3666 if (des_seen >= 1)
3667 {
3668 if (c_parser_next_token_is (parser, CPP_EQ))
3669 {
fcf73884 3670 if (!flag_isoc99)
b8698a0f 3671 pedwarn (des_loc, OPT_pedantic,
509c9d60 3672 "ISO C90 forbids specifying subobject to initialize");
27bf414c
JM
3673 c_parser_consume_token (parser);
3674 }
3675 else
3676 {
3677 if (des_seen == 1)
b8698a0f 3678 pedwarn (c_parser_peek_token (parser)->location, OPT_pedantic,
509c9d60 3679 "obsolete use of designated initializer without %<=%>");
27bf414c
JM
3680 else
3681 {
3682 struct c_expr init;
3683 init.value = error_mark_node;
3684 init.original_code = ERROR_MARK;
6866c6e8 3685 init.original_type = NULL;
27bf414c
JM
3686 c_parser_error (parser, "expected %<=%>");
3687 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
a1e3b3d9 3688 process_init_element (init, false, braced_init_obstack);
27bf414c
JM
3689 return;
3690 }
3691 }
3692 }
3693 }
a1e3b3d9 3694 c_parser_initval (parser, NULL, braced_init_obstack);
27bf414c
JM
3695}
3696
3697/* Parse a nested initializer; as c_parser_initializer but parses
3698 initializers within braced lists, after any designators have been
3699 applied. If AFTER is not NULL then it is an Objective-C message
3700 expression which is the primary-expression starting the
3701 initializer. */
3702
3703static void
a1e3b3d9
LB
3704c_parser_initval (c_parser *parser, struct c_expr *after,
3705 struct obstack * braced_init_obstack)
27bf414c
JM
3706{
3707 struct c_expr init;
3708 gcc_assert (!after || c_dialect_objc ());
3709 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE) && !after)
3710 init = c_parser_braced_init (parser, NULL_TREE, true);
3711 else
46bdb9cf 3712 {
c2255bc4 3713 location_t loc = c_parser_peek_token (parser)->location;
46bdb9cf
JM
3714 init = c_parser_expr_no_commas (parser, after);
3715 if (init.value != NULL_TREE
3716 && TREE_CODE (init.value) != STRING_CST
3717 && TREE_CODE (init.value) != COMPOUND_LITERAL_EXPR)
ebfbbdc5 3718 init = default_function_array_read_conversion (loc, init);
46bdb9cf 3719 }
a1e3b3d9 3720 process_init_element (init, false, braced_init_obstack);
27bf414c
JM
3721}
3722
3723/* Parse a compound statement (possibly a function body) (C90 6.6.2,
3724 C99 6.8.2).
3725
3726 compound-statement:
3727 { block-item-list[opt] }
3728 { label-declarations block-item-list }
3729
3730 block-item-list:
3731 block-item
3732 block-item-list block-item
3733
3734 block-item:
3735 nested-declaration
3736 statement
3737
3738 nested-declaration:
3739 declaration
3740
3741 GNU extensions:
3742
3743 compound-statement:
3744 { label-declarations block-item-list }
3745
3746 nested-declaration:
3747 __extension__ nested-declaration
3748 nested-function-definition
3749
3750 label-declarations:
3751 label-declaration
3752 label-declarations label-declaration
3753
3754 label-declaration:
3755 __label__ identifier-list ;
3756
3757 Allowing the mixing of declarations and code is new in C99. The
3758 GNU syntax also permits (not shown above) labels at the end of
3759 compound statements, which yield an error. We don't allow labels
3760 on declarations; this might seem like a natural extension, but
3761 there would be a conflict between attributes on the label and
3762 prefix attributes on the declaration. ??? The syntax follows the
3763 old parser in requiring something after label declarations.
3764 Although they are erroneous if the labels declared aren't defined,
953ff289 3765 is it useful for the syntax to be this way?
b8698a0f 3766
953ff289 3767 OpenMP:
b8698a0f 3768
953ff289
DN
3769 block-item:
3770 openmp-directive
3771
3772 openmp-directive:
3773 barrier-directive
3774 flush-directive */
27bf414c
JM
3775
3776static tree
3777c_parser_compound_statement (c_parser *parser)
3778{
3779 tree stmt;
c2255bc4
AH
3780 location_t brace_loc;
3781 brace_loc = c_parser_peek_token (parser)->location;
27bf414c 3782 if (!c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
5600f233
JM
3783 {
3784 /* Ensure a scope is entered and left anyway to avoid confusion
3785 if we have just prepared to enter a function body. */
3786 stmt = c_begin_compound_stmt (true);
c2255bc4 3787 c_end_compound_stmt (brace_loc, stmt, true);
5600f233
JM
3788 return error_mark_node;
3789 }
27bf414c
JM
3790 stmt = c_begin_compound_stmt (true);
3791 c_parser_compound_statement_nostart (parser);
c2255bc4 3792 return c_end_compound_stmt (brace_loc, stmt, true);
27bf414c
JM
3793}
3794
3795/* Parse a compound statement except for the opening brace. This is
3796 used for parsing both compound statements and statement expressions
3797 (which follow different paths to handling the opening). */
3798
3799static void
3800c_parser_compound_statement_nostart (c_parser *parser)
3801{
3802 bool last_stmt = false;
3803 bool last_label = false;
6ec637a4 3804 bool save_valid_for_pragma = valid_location_for_stdc_pragma_p ();
3ba09659 3805 location_t label_loc = UNKNOWN_LOCATION; /* Quiet warning. */
27bf414c
JM
3806 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3807 {
3808 c_parser_consume_token (parser);
3809 return;
3810 }
6ec637a4 3811 mark_valid_location_for_stdc_pragma (true);
27bf414c
JM
3812 if (c_parser_next_token_is_keyword (parser, RID_LABEL))
3813 {
3814 /* Read zero or more forward-declarations for labels that nested
3815 functions can jump to. */
6ec637a4 3816 mark_valid_location_for_stdc_pragma (false);
27bf414c
JM
3817 while (c_parser_next_token_is_keyword (parser, RID_LABEL))
3818 {
c2255bc4 3819 label_loc = c_parser_peek_token (parser)->location;
27bf414c
JM
3820 c_parser_consume_token (parser);
3821 /* Any identifiers, including those declared as type names,
3822 are OK here. */
3823 while (true)
3824 {
3825 tree label;
3826 if (c_parser_next_token_is_not (parser, CPP_NAME))
3827 {
3828 c_parser_error (parser, "expected identifier");
3829 break;
3830 }
3831 label
3832 = declare_label (c_parser_peek_token (parser)->value);
3833 C_DECLARED_LABEL_FLAG (label) = 1;
c2255bc4 3834 add_stmt (build_stmt (label_loc, DECL_EXPR, label));
27bf414c
JM
3835 c_parser_consume_token (parser);
3836 if (c_parser_next_token_is (parser, CPP_COMMA))
3837 c_parser_consume_token (parser);
3838 else
3839 break;
3840 }
3841 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
3842 }
c2255bc4 3843 pedwarn (label_loc, OPT_pedantic, "ISO C forbids label declarations");
27bf414c
JM
3844 }
3845 /* We must now have at least one statement, label or declaration. */
3846 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3847 {
6ec637a4 3848 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
27bf414c
JM
3849 c_parser_error (parser, "expected declaration or statement");
3850 c_parser_consume_token (parser);
3851 return;
3852 }
3853 while (c_parser_next_token_is_not (parser, CPP_CLOSE_BRACE))
3854 {
3855 location_t loc = c_parser_peek_token (parser)->location;
27bf414c
JM
3856 if (c_parser_next_token_is_keyword (parser, RID_CASE)
3857 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
3858 || (c_parser_next_token_is (parser, CPP_NAME)
3859 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
3860 {
c7412148
TT
3861 if (c_parser_next_token_is_keyword (parser, RID_CASE))
3862 label_loc = c_parser_peek_2nd_token (parser)->location;
3863 else
3864 label_loc = c_parser_peek_token (parser)->location;
27bf414c
JM
3865 last_label = true;
3866 last_stmt = false;
6ec637a4 3867 mark_valid_location_for_stdc_pragma (false);
27bf414c
JM
3868 c_parser_label (parser);
3869 }
3870 else if (!last_label
32912286 3871 && c_parser_next_token_starts_declaration (parser))
27bf414c
JM
3872 {
3873 last_label = false;
6ec637a4 3874 mark_valid_location_for_stdc_pragma (false);
f05b9d93 3875 c_parser_declaration_or_fndef (parser, true, true, true, true, true, NULL);
fcf73884 3876 if (last_stmt)
b8698a0f 3877 pedwarn_c90 (loc,
509c9d60 3878 (pedantic && !flag_isoc99)
fcf73884
MLI
3879 ? OPT_pedantic
3880 : OPT_Wdeclaration_after_statement,
509c9d60 3881 "ISO C90 forbids mixed declarations and code");
27bf414c
JM
3882 last_stmt = false;
3883 }
3884 else if (!last_label
3885 && c_parser_next_token_is_keyword (parser, RID_EXTENSION))
3886 {
3887 /* __extension__ can start a declaration, but is also an
3888 unary operator that can start an expression. Consume all
3889 but the last of a possible series of __extension__ to
3890 determine which. */
3891 while (c_parser_peek_2nd_token (parser)->type == CPP_KEYWORD
3892 && (c_parser_peek_2nd_token (parser)->keyword
3893 == RID_EXTENSION))
3894 c_parser_consume_token (parser);
32912286 3895 if (c_token_starts_declaration (c_parser_peek_2nd_token (parser)))
27bf414c
JM
3896 {
3897 int ext;
3898 ext = disable_extension_diagnostics ();
3899 c_parser_consume_token (parser);
3900 last_label = false;
6ec637a4 3901 mark_valid_location_for_stdc_pragma (false);
32912286 3902 c_parser_declaration_or_fndef (parser, true, true, true, true,
f05b9d93 3903 true, NULL);
27bf414c
JM
3904 /* Following the old parser, __extension__ does not
3905 disable this diagnostic. */
3906 restore_extension_diagnostics (ext);
fcf73884 3907 if (last_stmt)
509c9d60 3908 pedwarn_c90 (loc, (pedantic && !flag_isoc99)
fcf73884
MLI
3909 ? OPT_pedantic
3910 : OPT_Wdeclaration_after_statement,
509c9d60 3911 "ISO C90 forbids mixed declarations and code");
27bf414c
JM
3912 last_stmt = false;
3913 }
3914 else
3915 goto statement;
3916 }
bc4071dd
RH
3917 else if (c_parser_next_token_is (parser, CPP_PRAGMA))
3918 {
3919 /* External pragmas, and some omp pragmas, are not associated
3920 with regular c code, and so are not to be considered statements
3921 syntactically. This ensures that the user doesn't put them
3922 places that would turn into syntax errors if the directive
3923 were ignored. */
3924 if (c_parser_pragma (parser, pragma_compound))
3925 last_label = false, last_stmt = true;
3926 }
3927 else if (c_parser_next_token_is (parser, CPP_EOF))
3928 {
6ec637a4 3929 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
bc4071dd
RH
3930 c_parser_error (parser, "expected declaration or statement");
3931 return;
3932 }
b4b56033
MLI
3933 else if (c_parser_next_token_is_keyword (parser, RID_ELSE))
3934 {
b8698a0f 3935 if (parser->in_if_block)
b4b56033 3936 {
6ec637a4 3937 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
3ba09659 3938 error_at (loc, """expected %<}%> before %<else%>");
b4b56033
MLI
3939 return;
3940 }
b8698a0f 3941 else
b4b56033 3942 {
3ba09659 3943 error_at (loc, "%<else%> without a previous %<if%>");
b4b56033
MLI
3944 c_parser_consume_token (parser);
3945 continue;
3946 }
3947 }
27bf414c
JM
3948 else
3949 {
3950 statement:
3951 last_label = false;
3952 last_stmt = true;
6ec637a4 3953 mark_valid_location_for_stdc_pragma (false);
27bf414c
JM
3954 c_parser_statement_after_labels (parser);
3955 }
2c14ae9a
VR
3956
3957 parser->error = false;
27bf414c
JM
3958 }
3959 if (last_label)
3ba09659 3960 error_at (label_loc, "label at end of compound statement");
27bf414c 3961 c_parser_consume_token (parser);
6ec637a4
JJ
3962 /* Restore the value we started with. */
3963 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
27bf414c
JM
3964}
3965
3966/* Parse a label (C90 6.6.1, C99 6.8.1).
3967
3968 label:
3969 identifier : attributes[opt]
3970 case constant-expression :
3971 default :
3972
3973 GNU extensions:
3974
3975 label:
3976 case constant-expression ... constant-expression :
3977
3978 The use of attributes on labels is a GNU extension. The syntax in
3979 GNU C accepts any expressions without commas, non-constant
3980 expressions being rejected later. */
3981
3982static void
3983c_parser_label (c_parser *parser)
3984{
3985 location_t loc1 = c_parser_peek_token (parser)->location;
3986 tree label = NULL_TREE;
3987 if (c_parser_next_token_is_keyword (parser, RID_CASE))
3988 {
3989 tree exp1, exp2;
3990 c_parser_consume_token (parser);
3991 exp1 = c_parser_expr_no_commas (parser, NULL).value;
3992 if (c_parser_next_token_is (parser, CPP_COLON))
3993 {
3994 c_parser_consume_token (parser);
c2255bc4 3995 label = do_case (loc1, exp1, NULL_TREE);
27bf414c
JM
3996 }
3997 else if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
3998 {
3999 c_parser_consume_token (parser);
4000 exp2 = c_parser_expr_no_commas (parser, NULL).value;
4001 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
c2255bc4 4002 label = do_case (loc1, exp1, exp2);
27bf414c
JM
4003 }
4004 else
4005 c_parser_error (parser, "expected %<:%> or %<...%>");
4006 }
4007 else if (c_parser_next_token_is_keyword (parser, RID_DEFAULT))
4008 {
4009 c_parser_consume_token (parser);
4010 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
c2255bc4 4011 label = do_case (loc1, NULL_TREE, NULL_TREE);
27bf414c
JM
4012 }
4013 else
4014 {
4015 tree name = c_parser_peek_token (parser)->value;
4016 tree tlab;
27bf414c 4017 tree attrs;
c7412148 4018 location_t loc2 = c_parser_peek_token (parser)->location;
27bf414c
JM
4019 gcc_assert (c_parser_next_token_is (parser, CPP_NAME));
4020 c_parser_consume_token (parser);
4021 gcc_assert (c_parser_next_token_is (parser, CPP_COLON));
27bf414c
JM
4022 c_parser_consume_token (parser);
4023 attrs = c_parser_attributes (parser);
4024 tlab = define_label (loc2, name);
4025 if (tlab)
4026 {
4027 decl_attributes (&tlab, attrs, 0);
c2255bc4 4028 label = add_stmt (build_stmt (loc1, LABEL_EXPR, tlab));
27bf414c
JM
4029 }
4030 }
4031 if (label)
3d57f0f0 4032 {
32912286 4033 if (c_parser_next_token_starts_declaration (parser)
3d57f0f0
MLI
4034 && !(c_parser_next_token_is (parser, CPP_NAME)
4035 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
4036 {
3ba09659
AH
4037 error_at (c_parser_peek_token (parser)->location,
4038 "a label can only be part of a statement and "
4039 "a declaration is not a statement");
b8698a0f 4040 c_parser_declaration_or_fndef (parser, /*fndef_ok*/ false,
32912286 4041 /*static_assert_ok*/ true,
3d57f0f0 4042 /*nested*/ true, /*empty_ok*/ false,
f05b9d93 4043 /*start_attr_ok*/ true, NULL);
3d57f0f0
MLI
4044 }
4045 }
27bf414c
JM
4046}
4047
4048/* Parse a statement (C90 6.6, C99 6.8).
4049
4050 statement:
4051 labeled-statement
4052 compound-statement
4053 expression-statement
4054 selection-statement
4055 iteration-statement
4056 jump-statement
4057
4058 labeled-statement:
4059 label statement
4060
4061 expression-statement:
4062 expression[opt] ;
4063
4064 selection-statement:
4065 if-statement
4066 switch-statement
4067
4068 iteration-statement:
4069 while-statement
4070 do-statement
4071 for-statement
4072
4073 jump-statement:
4074 goto identifier ;
4075 continue ;
4076 break ;
4077 return expression[opt] ;
4078
4079 GNU extensions:
4080
4081 statement:
4082 asm-statement
4083
4084 jump-statement:
4085 goto * expression ;
4086
4087 Objective-C:
4088
4089 statement:
4090 objc-throw-statement
4091 objc-try-catch-statement
4092 objc-synchronized-statement
4093
4094 objc-throw-statement:
4095 @throw expression ;
4096 @throw ;
953ff289
DN
4097
4098 OpenMP:
4099
4100 statement:
4101 openmp-construct
4102
4103 openmp-construct:
4104 parallel-construct
4105 for-construct
4106 sections-construct
4107 single-construct
4108 parallel-for-construct
4109 parallel-sections-construct
4110 master-construct
4111 critical-construct
4112 atomic-construct
4113 ordered-construct
4114
4115 parallel-construct:
4116 parallel-directive structured-block
4117
4118 for-construct:
4119 for-directive iteration-statement
4120
4121 sections-construct:
4122 sections-directive section-scope
4123
4124 single-construct:
4125 single-directive structured-block
4126
4127 parallel-for-construct:
4128 parallel-for-directive iteration-statement
4129
4130 parallel-sections-construct:
4131 parallel-sections-directive section-scope
4132
4133 master-construct:
4134 master-directive structured-block
4135
4136 critical-construct:
4137 critical-directive structured-block
4138
4139 atomic-construct:
4140 atomic-directive expression-statement
4141
4142 ordered-construct:
4143 ordered-directive structured-block */
27bf414c
JM
4144
4145static void
4146c_parser_statement (c_parser *parser)
4147{
4148 while (c_parser_next_token_is_keyword (parser, RID_CASE)
4149 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
4150 || (c_parser_next_token_is (parser, CPP_NAME)
4151 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
4152 c_parser_label (parser);
4153 c_parser_statement_after_labels (parser);
4154}
4155
4156/* Parse a statement, other than a labeled statement. */
4157
4158static void
4159c_parser_statement_after_labels (c_parser *parser)
4160{
4161 location_t loc = c_parser_peek_token (parser)->location;
4162 tree stmt = NULL_TREE;
b4b56033
MLI
4163 bool in_if_block = parser->in_if_block;
4164 parser->in_if_block = false;
27bf414c
JM
4165 switch (c_parser_peek_token (parser)->type)
4166 {
4167 case CPP_OPEN_BRACE:
4168 add_stmt (c_parser_compound_statement (parser));
4169 break;
4170 case CPP_KEYWORD:
4171 switch (c_parser_peek_token (parser)->keyword)
4172 {
4173 case RID_IF:
4174 c_parser_if_statement (parser);
4175 break;
4176 case RID_SWITCH:
4177 c_parser_switch_statement (parser);
4178 break;
4179 case RID_WHILE:
4180 c_parser_while_statement (parser);
4181 break;
4182 case RID_DO:
4183 c_parser_do_statement (parser);
4184 break;
4185 case RID_FOR:
4186 c_parser_for_statement (parser);
4187 break;
4188 case RID_GOTO:
4189 c_parser_consume_token (parser);
4190 if (c_parser_next_token_is (parser, CPP_NAME))
4191 {
c2255bc4
AH
4192 stmt = c_finish_goto_label (loc,
4193 c_parser_peek_token (parser)->value);
27bf414c
JM
4194 c_parser_consume_token (parser);
4195 }
4196 else if (c_parser_next_token_is (parser, CPP_MULT))
4197 {
84628aa8
JJ
4198 tree val;
4199
27bf414c 4200 c_parser_consume_token (parser);
84628aa8
JJ
4201 val = c_parser_expression (parser).value;
4202 mark_exp_read (val);
4203 stmt = c_finish_goto_ptr (loc, val);
27bf414c
JM
4204 }
4205 else
4206 c_parser_error (parser, "expected identifier or %<*%>");
4207 goto expect_semicolon;
4208 case RID_CONTINUE:
4209 c_parser_consume_token (parser);
c2255bc4 4210 stmt = c_finish_bc_stmt (loc, &c_cont_label, false);
27bf414c
JM
4211 goto expect_semicolon;
4212 case RID_BREAK:
4213 c_parser_consume_token (parser);
c2255bc4 4214 stmt = c_finish_bc_stmt (loc, &c_break_label, true);
27bf414c
JM
4215 goto expect_semicolon;
4216 case RID_RETURN:
4217 c_parser_consume_token (parser);
4218 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
4219 {
c2255bc4 4220 stmt = c_finish_return (loc, NULL_TREE, NULL_TREE);
27bf414c
JM
4221 c_parser_consume_token (parser);
4222 }
4223 else
4224 {
bbbbb16a 4225 struct c_expr expr = c_parser_expression_conv (parser);
ebfbbdc5 4226 mark_exp_read (expr.value);
c2255bc4 4227 stmt = c_finish_return (loc, expr.value, expr.original_type);
27bf414c
JM
4228 goto expect_semicolon;
4229 }
4230 break;
4231 case RID_ASM:
4232 stmt = c_parser_asm_statement (parser);
4233 break;
49b91f05 4234 case RID_AT_THROW:
27bf414c
JM
4235 gcc_assert (c_dialect_objc ());
4236 c_parser_consume_token (parser);
4237 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
4238 {
c2255bc4 4239 stmt = objc_build_throw_stmt (loc, NULL_TREE);
27bf414c
JM
4240 c_parser_consume_token (parser);
4241 }
4242 else
4243 {
928c19bb
JM
4244 tree expr = c_parser_expression (parser).value;
4245 expr = c_fully_fold (expr, false, NULL);
c2255bc4 4246 stmt = objc_build_throw_stmt (loc, expr);
27bf414c
JM
4247 goto expect_semicolon;
4248 }
4249 break;
49b91f05 4250 case RID_AT_TRY:
27bf414c
JM
4251 gcc_assert (c_dialect_objc ());
4252 c_parser_objc_try_catch_statement (parser);
4253 break;
4254 case RID_AT_SYNCHRONIZED:
4255 gcc_assert (c_dialect_objc ());
4256 c_parser_objc_synchronized_statement (parser);
4257 break;
4258 default:
4259 goto expr_stmt;
4260 }
4261 break;
4262 case CPP_SEMICOLON:
4263 c_parser_consume_token (parser);
4264 break;
4265 case CPP_CLOSE_PAREN:
4266 case CPP_CLOSE_SQUARE:
4267 /* Avoid infinite loop in error recovery:
4268 c_parser_skip_until_found stops at a closing nesting
4269 delimiter without consuming it, but here we need to consume
4270 it to proceed further. */
4271 c_parser_error (parser, "expected statement");
4272 c_parser_consume_token (parser);
4273 break;
bc4071dd
RH
4274 case CPP_PRAGMA:
4275 c_parser_pragma (parser, pragma_stmt);
4276 break;
27bf414c
JM
4277 default:
4278 expr_stmt:
c2255bc4 4279 stmt = c_finish_expr_stmt (loc, c_parser_expression_conv (parser).value);
27bf414c
JM
4280 expect_semicolon:
4281 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
4282 break;
4283 }
4284 /* Two cases cannot and do not have line numbers associated: If stmt
4285 is degenerate, such as "2;", then stmt is an INTEGER_CST, which
4286 cannot hold line numbers. But that's OK because the statement
4287 will either be changed to a MODIFY_EXPR during gimplification of
4288 the statement expr, or discarded. If stmt was compound, but
4289 without new variables, we will have skipped the creation of a
4290 BIND and will have a bare STATEMENT_LIST. But that's OK because
4291 (recursively) all of the component statements should already have
4292 line numbers assigned. ??? Can we discard no-op statements
4293 earlier? */
c2255bc4
AH
4294 if (CAN_HAVE_LOCATION_P (stmt)
4295 && EXPR_LOCATION (stmt) == UNKNOWN_LOCATION)
4296 SET_EXPR_LOCATION (stmt, loc);
b4b56033
MLI
4297
4298 parser->in_if_block = in_if_block;
27bf414c
JM
4299}
4300
ca085fd7
MLI
4301/* Parse the condition from an if, do, while or for statements. */
4302
4303static tree
4304c_parser_condition (c_parser *parser)
4305{
c2255bc4 4306 location_t loc = c_parser_peek_token (parser)->location;
ca085fd7 4307 tree cond;
928c19bb
JM
4308 cond = c_parser_expression_conv (parser).value;
4309 cond = c_objc_common_truthvalue_conversion (loc, cond);
4310 cond = c_fully_fold (cond, false, NULL);
ca085fd7
MLI
4311 if (warn_sequence_point)
4312 verify_sequence_points (cond);
4313 return cond;
4314}
4315
27bf414c
JM
4316/* Parse a parenthesized condition from an if, do or while statement.
4317
4318 condition:
4319 ( expression )
4320*/
4321static tree
4322c_parser_paren_condition (c_parser *parser)
4323{
27bf414c
JM
4324 tree cond;
4325 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4326 return error_mark_node;
ca085fd7 4327 cond = c_parser_condition (parser);
27bf414c
JM
4328 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
4329 return cond;
4330}
4331
4332/* Parse a statement which is a block in C99. */
4333
4334static tree
4335c_parser_c99_block_statement (c_parser *parser)
4336{
4337 tree block = c_begin_compound_stmt (flag_isoc99);
c2255bc4 4338 location_t loc = c_parser_peek_token (parser)->location;
27bf414c 4339 c_parser_statement (parser);
c2255bc4 4340 return c_end_compound_stmt (loc, block, flag_isoc99);
27bf414c
JM
4341}
4342
b4b56033
MLI
4343/* Parse the body of an if statement. This is just parsing a
4344 statement but (a) it is a block in C99, (b) we track whether the
4345 body is an if statement for the sake of -Wparentheses warnings, (c)
4346 we handle an empty body specially for the sake of -Wempty-body
4347 warnings, and (d) we call parser_compound_statement directly
4348 because c_parser_statement_after_labels resets
4349 parser->in_if_block. */
27bf414c
JM
4350
4351static tree
4352c_parser_if_body (c_parser *parser, bool *if_p)
4353{
4354 tree block = c_begin_compound_stmt (flag_isoc99);
c2255bc4 4355 location_t body_loc = c_parser_peek_token (parser)->location;
27bf414c
JM
4356 while (c_parser_next_token_is_keyword (parser, RID_CASE)
4357 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
4358 || (c_parser_next_token_is (parser, CPP_NAME)
4359 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
4360 c_parser_label (parser);
4361 *if_p = c_parser_next_token_is_keyword (parser, RID_IF);
62e00e94 4362 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
b4b56033 4363 {
626c34b5 4364 location_t loc = c_parser_peek_token (parser)->location;
c2255bc4 4365 add_stmt (build_empty_stmt (loc));
b4b56033 4366 c_parser_consume_token (parser);
626c34b5
PB
4367 if (!c_parser_next_token_is_keyword (parser, RID_ELSE))
4368 warning_at (loc, OPT_Wempty_body,
4369 "suggest braces around empty body in an %<if%> statement");
b4b56033
MLI
4370 }
4371 else if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
4372 add_stmt (c_parser_compound_statement (parser));
4373 else
4374 c_parser_statement_after_labels (parser);
c2255bc4 4375 return c_end_compound_stmt (body_loc, block, flag_isoc99);
b4b56033
MLI
4376}
4377
4378/* Parse the else body of an if statement. This is just parsing a
4379 statement but (a) it is a block in C99, (b) we handle an empty body
4380 specially for the sake of -Wempty-body warnings. */
4381
4382static tree
4383c_parser_else_body (c_parser *parser)
4384{
c2255bc4 4385 location_t else_loc = c_parser_peek_token (parser)->location;
b4b56033
MLI
4386 tree block = c_begin_compound_stmt (flag_isoc99);
4387 while (c_parser_next_token_is_keyword (parser, RID_CASE)
4388 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
4389 || (c_parser_next_token_is (parser, CPP_NAME)
4390 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
4391 c_parser_label (parser);
4392 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
4393 {
c2255bc4
AH
4394 location_t loc = c_parser_peek_token (parser)->location;
4395 warning_at (loc,
626c34b5
PB
4396 OPT_Wempty_body,
4397 "suggest braces around empty body in an %<else%> statement");
c2255bc4 4398 add_stmt (build_empty_stmt (loc));
b4b56033
MLI
4399 c_parser_consume_token (parser);
4400 }
b8698a0f 4401 else
b4b56033 4402 c_parser_statement_after_labels (parser);
c2255bc4 4403 return c_end_compound_stmt (else_loc, block, flag_isoc99);
27bf414c
JM
4404}
4405
4406/* Parse an if statement (C90 6.6.4, C99 6.8.4).
4407
4408 if-statement:
4409 if ( expression ) statement
4410 if ( expression ) statement else statement
4411*/
4412
4413static void
4414c_parser_if_statement (c_parser *parser)
4415{
4416 tree block;
4417 location_t loc;
4418 tree cond;
b4b56033 4419 bool first_if = false;
27bf414c 4420 tree first_body, second_body;
b4b56033
MLI
4421 bool in_if_block;
4422
27bf414c
JM
4423 gcc_assert (c_parser_next_token_is_keyword (parser, RID_IF));
4424 c_parser_consume_token (parser);
4425 block = c_begin_compound_stmt (flag_isoc99);
4426 loc = c_parser_peek_token (parser)->location;
4427 cond = c_parser_paren_condition (parser);
b4b56033
MLI
4428 in_if_block = parser->in_if_block;
4429 parser->in_if_block = true;
27bf414c 4430 first_body = c_parser_if_body (parser, &first_if);
b4b56033 4431 parser->in_if_block = in_if_block;
27bf414c
JM
4432 if (c_parser_next_token_is_keyword (parser, RID_ELSE))
4433 {
4434 c_parser_consume_token (parser);
b4b56033 4435 second_body = c_parser_else_body (parser);
27bf414c
JM
4436 }
4437 else
4438 second_body = NULL_TREE;
4439 c_finish_if_stmt (loc, cond, first_body, second_body, first_if);
c2255bc4 4440 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99));
27bf414c
JM
4441}
4442
4443/* Parse a switch statement (C90 6.6.4, C99 6.8.4).
4444
4445 switch-statement:
4446 switch (expression) statement
4447*/
4448
4449static void
4450c_parser_switch_statement (c_parser *parser)
4451{
4452 tree block, expr, body, save_break;
c2255bc4
AH
4453 location_t switch_loc = c_parser_peek_token (parser)->location;
4454 location_t switch_cond_loc;
27bf414c
JM
4455 gcc_assert (c_parser_next_token_is_keyword (parser, RID_SWITCH));
4456 c_parser_consume_token (parser);
4457 block = c_begin_compound_stmt (flag_isoc99);
4458 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4459 {
c2255bc4 4460 switch_cond_loc = c_parser_peek_token (parser)->location;
27bf414c
JM
4461 expr = c_parser_expression (parser).value;
4462 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
4463 }
4464 else
c2255bc4
AH
4465 {
4466 switch_cond_loc = UNKNOWN_LOCATION;
4467 expr = error_mark_node;
4468 }
4469 c_start_case (switch_loc, switch_cond_loc, expr);
27bf414c
JM
4470 save_break = c_break_label;
4471 c_break_label = NULL_TREE;
4472 body = c_parser_c99_block_statement (parser);
4473 c_finish_case (body);
4474 if (c_break_label)
c2255bc4
AH
4475 {
4476 location_t here = c_parser_peek_token (parser)->location;
4477 tree t = build1 (LABEL_EXPR, void_type_node, c_break_label);
4478 SET_EXPR_LOCATION (t, here);
4479 add_stmt (t);
4480 }
27bf414c 4481 c_break_label = save_break;
c2255bc4 4482 add_stmt (c_end_compound_stmt (switch_loc, block, flag_isoc99));
27bf414c
JM
4483}
4484
4485/* Parse a while statement (C90 6.6.5, C99 6.8.5).
4486
4487 while-statement:
4488 while (expression) statement
4489*/
4490
4491static void
4492c_parser_while_statement (c_parser *parser)
4493{
4494 tree block, cond, body, save_break, save_cont;
4495 location_t loc;
4496 gcc_assert (c_parser_next_token_is_keyword (parser, RID_WHILE));
4497 c_parser_consume_token (parser);
4498 block = c_begin_compound_stmt (flag_isoc99);
4499 loc = c_parser_peek_token (parser)->location;
4500 cond = c_parser_paren_condition (parser);
4501 save_break = c_break_label;
4502 c_break_label = NULL_TREE;
4503 save_cont = c_cont_label;
4504 c_cont_label = NULL_TREE;
4505 body = c_parser_c99_block_statement (parser);
4506 c_finish_loop (loc, cond, NULL, body, c_break_label, c_cont_label, true);
c2255bc4 4507 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99));
27bf414c
JM
4508 c_break_label = save_break;
4509 c_cont_label = save_cont;
4510}
4511
4512/* Parse a do statement (C90 6.6.5, C99 6.8.5).
4513
4514 do-statement:
4515 do statement while ( expression ) ;
4516*/
4517
4518static void
4519c_parser_do_statement (c_parser *parser)
4520{
4521 tree block, cond, body, save_break, save_cont, new_break, new_cont;
4522 location_t loc;
4523 gcc_assert (c_parser_next_token_is_keyword (parser, RID_DO));
4524 c_parser_consume_token (parser);
62e00e94 4525 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
3ba09659
AH
4526 warning_at (c_parser_peek_token (parser)->location,
4527 OPT_Wempty_body,
4528 "suggest braces around empty body in %<do%> statement");
27bf414c
JM
4529 block = c_begin_compound_stmt (flag_isoc99);
4530 loc = c_parser_peek_token (parser)->location;
4531 save_break = c_break_label;
4532 c_break_label = NULL_TREE;
4533 save_cont = c_cont_label;
4534 c_cont_label = NULL_TREE;
4535 body = c_parser_c99_block_statement (parser);
4536 c_parser_require_keyword (parser, RID_WHILE, "expected %<while%>");
4537 new_break = c_break_label;
4538 c_break_label = save_break;
4539 new_cont = c_cont_label;
4540 c_cont_label = save_cont;
4541 cond = c_parser_paren_condition (parser);
4542 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
4543 c_parser_skip_to_end_of_block_or_statement (parser);
4544 c_finish_loop (loc, cond, NULL, body, new_break, new_cont, false);
c2255bc4 4545 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99));
27bf414c
JM
4546}
4547
4548/* Parse a for statement (C90 6.6.5, C99 6.8.5).
4549
4550 for-statement:
4551 for ( expression[opt] ; expression[opt] ; expression[opt] ) statement
4552 for ( nested-declaration expression[opt] ; expression[opt] ) statement
4553
4554 The form with a declaration is new in C99.
4555
4556 ??? In accordance with the old parser, the declaration may be a
4557 nested function, which is then rejected in check_for_loop_decls,
4558 but does it make any sense for this to be included in the grammar?
4559 Note in particular that the nested function does not include a
4560 trailing ';', whereas the "declaration" production includes one.
4561 Also, can we reject bad declarations earlier and cheaper than
f05b9d93
NP
4562 check_for_loop_decls?
4563
4564 In Objective-C, there are two additional variants:
4565
4566 foreach-statement:
4567 for ( expression in expresssion ) statement
4568 for ( declaration in expression ) statement
4569
4570 This is inconsistent with C, because the second variant is allowed
4571 even if c99 is not enabled.
4572
4573 The rest of the comment documents these Objective-C foreach-statement.
4574
4575 Here is the canonical example of the first variant:
4576 for (object in array) { do something with object }
4577 we call the first expression ("object") the "object_expression" and
4578 the second expression ("array") the "collection_expression".
4579 object_expression must be an lvalue of type "id" (a generic Objective-C
4580 object) because the loop works by assigning to object_expression the
4581 various objects from the collection_expression. collection_expression
4582 must evaluate to something of type "id" which responds to the method
4583 countByEnumeratingWithState:objects:count:.
4584
4585 The canonical example of the second variant is:
4586 for (id object in array) { do something with object }
4587 which is completely equivalent to
4588 {
4589 id object;
4590 for (object in array) { do something with object }
4591 }
4592 Note that initizializing 'object' in some way (eg, "for ((object =
4593 xxx) in array) { do something with object }") is possibly
4594 technically valid, but completely pointless as 'object' will be
4595 assigned to something else as soon as the loop starts. We should
4596 most likely reject it (TODO).
4597
4598 The beginning of the Objective-C foreach-statement looks exactly
4599 like the beginning of the for-statement, and we can tell it is a
4600 foreach-statement only because the initial declaration or
4601 expression is terminated by 'in' instead of ';'.
4602*/
27bf414c
JM
4603
4604static void
4605c_parser_for_statement (c_parser *parser)
4606{
4607 tree block, cond, incr, save_break, save_cont, body;
f05b9d93
NP
4608 /* The following are only used when parsing an ObjC foreach statement. */
4609 tree object_expression, collection_expression;
c2255bc4
AH
4610 location_t loc = c_parser_peek_token (parser)->location;
4611 location_t for_loc = c_parser_peek_token (parser)->location;
f05b9d93 4612 bool is_foreach_statement = false;
27bf414c
JM
4613 gcc_assert (c_parser_next_token_is_keyword (parser, RID_FOR));
4614 c_parser_consume_token (parser);
f05b9d93
NP
4615 /* Open a compound statement in Objective-C as well, just in case this is
4616 as foreach expression. */
4617 block = c_begin_compound_stmt (flag_isoc99 || c_dialect_objc ());
91b90ead
UB
4618 cond = error_mark_node;
4619 incr = error_mark_node;
27bf414c
JM
4620 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4621 {
4622 /* Parse the initialization declaration or expression. */
f05b9d93 4623 object_expression = error_mark_node;
27bf414c
JM
4624 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
4625 {
4626 c_parser_consume_token (parser);
c2255bc4 4627 c_finish_expr_stmt (loc, NULL_TREE);
27bf414c 4628 }
32912286 4629 else if (c_parser_next_token_starts_declaration (parser))
27bf414c 4630 {
f05b9d93
NP
4631 parser->objc_could_be_foreach_context = true;
4632 c_parser_declaration_or_fndef (parser, true, true, true, true, true,
4633 &object_expression);
4634 parser->objc_could_be_foreach_context = false;
4635
4636 if (c_parser_next_token_is_keyword (parser, RID_IN))
4637 {
4638 c_parser_consume_token (parser);
4639 is_foreach_statement = true;
4640 if (check_for_loop_decls (for_loc, true) == NULL_TREE)
4641 c_parser_error (parser, "multiple iterating variables in fast enumeration");
4642 }
4643 else
4644 check_for_loop_decls (for_loc, flag_isoc99);
27bf414c
JM
4645 }
4646 else if (c_parser_next_token_is_keyword (parser, RID_EXTENSION))
4647 {
4648 /* __extension__ can start a declaration, but is also an
4649 unary operator that can start an expression. Consume all
4650 but the last of a possible series of __extension__ to
4651 determine which. */
4652 while (c_parser_peek_2nd_token (parser)->type == CPP_KEYWORD
4653 && (c_parser_peek_2nd_token (parser)->keyword
4654 == RID_EXTENSION))
4655 c_parser_consume_token (parser);
32912286 4656 if (c_token_starts_declaration (c_parser_peek_2nd_token (parser)))
27bf414c
JM
4657 {
4658 int ext;
4659 ext = disable_extension_diagnostics ();
4660 c_parser_consume_token (parser);
f05b9d93 4661 parser->objc_could_be_foreach_context = true;
32912286 4662 c_parser_declaration_or_fndef (parser, true, true, true, true,
f05b9d93
NP
4663 true, &object_expression);
4664 parser->objc_could_be_foreach_context = false;
4665
27bf414c 4666 restore_extension_diagnostics (ext);
f05b9d93
NP
4667 if (c_parser_next_token_is_keyword (parser, RID_IN))
4668 {
4669 c_parser_consume_token (parser);
4670 is_foreach_statement = true;
4671 if (check_for_loop_decls (for_loc, true) == NULL_TREE)
4672 c_parser_error (parser, "multiple iterating variables in fast enumeration");
4673 }
4674 else
4675 check_for_loop_decls (for_loc, flag_isoc99);
27bf414c
JM
4676 }
4677 else
4678 goto init_expr;
4679 }
4680 else
4681 {
4682 init_expr:
f05b9d93
NP
4683 {
4684 tree init_expression;
4685 parser->objc_could_be_foreach_context = true;
4686 init_expression = c_parser_expression (parser).value;
4687 parser->objc_could_be_foreach_context = false;
4688 if (c_parser_next_token_is_keyword (parser, RID_IN))
4689 {
4690 c_parser_consume_token (parser);
4691 is_foreach_statement = true;
4692 if (! lvalue_p (init_expression))
4693 c_parser_error (parser, "invalid iterating variable in fast enumeration");
4694 object_expression = c_process_expr_stmt (loc, init_expression);
4695
4696 }
4697 else
4698 {
4699 c_finish_expr_stmt (loc, init_expression);
4700 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
4701 }
4702 }
27bf414c 4703 }
f05b9d93
NP
4704 /* Parse the loop condition. In the case of a foreach
4705 statement, there is no loop condition. */
4706 if (!is_foreach_statement)
27bf414c 4707 {
f05b9d93
NP
4708 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
4709 {
4710 c_parser_consume_token (parser);
4711 cond = NULL_TREE;
4712 }
4713 else
4714 {
4715 cond = c_parser_condition (parser);
4716 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
4717 }
27bf414c 4718 }
f05b9d93
NP
4719 /* Parse the increment expression (the third expression in a
4720 for-statement). In the case of a foreach-statement, this is
4721 the expression that follows the 'in'. */
4722 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
27bf414c 4723 {
f05b9d93
NP
4724 if (is_foreach_statement)
4725 {
4726 c_parser_error (parser, "missing collection in fast enumeration");
4727 collection_expression = error_mark_node;
4728 }
4729 else
4730 incr = c_process_expr_stmt (loc, NULL_TREE);
27bf414c 4731 }
27bf414c 4732 else
f05b9d93
NP
4733 {
4734 if (is_foreach_statement)
4735 collection_expression = c_process_expr_stmt (loc, c_parser_expression (parser).value);
4736 else
4737 incr = c_process_expr_stmt (loc, c_parser_expression (parser).value);
4738 }
27bf414c
JM
4739 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
4740 }
27bf414c
JM
4741 save_break = c_break_label;
4742 c_break_label = NULL_TREE;
4743 save_cont = c_cont_label;
4744 c_cont_label = NULL_TREE;
4745 body = c_parser_c99_block_statement (parser);
f05b9d93
NP
4746 if (is_foreach_statement)
4747 objc_finish_foreach_loop (loc, object_expression, collection_expression, body, c_break_label, c_cont_label);
4748 else
4749 c_finish_loop (loc, cond, incr, body, c_break_label, c_cont_label, true);
4750 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99 || c_dialect_objc ()));
27bf414c
JM
4751 c_break_label = save_break;
4752 c_cont_label = save_cont;
4753}
4754
4755/* Parse an asm statement, a GNU extension. This is a full-blown asm
4756 statement with inputs, outputs, clobbers, and volatile tag
4757 allowed.
4758
4759 asm-statement:
4760 asm type-qualifier[opt] ( asm-argument ) ;
1c384bf1 4761 asm type-qualifier[opt] goto ( asm-goto-argument ) ;
27bf414c
JM
4762
4763 asm-argument:
4764 asm-string-literal
4765 asm-string-literal : asm-operands[opt]
4766 asm-string-literal : asm-operands[opt] : asm-operands[opt]
1c384bf1
RH
4767 asm-string-literal : asm-operands[opt] : asm-operands[opt] : asm-clobbers[opt]
4768
4769 asm-goto-argument:
4770 asm-string-literal : : asm-operands[opt] : asm-clobbers[opt] \
4771 : asm-goto-operands
27bf414c
JM
4772
4773 Qualifiers other than volatile are accepted in the syntax but
4774 warned for. */
4775
4776static tree
4777c_parser_asm_statement (c_parser *parser)
4778{
1c384bf1
RH
4779 tree quals, str, outputs, inputs, clobbers, labels, ret;
4780 bool simple, is_goto;
c2255bc4 4781 location_t asm_loc = c_parser_peek_token (parser)->location;
1c384bf1
RH
4782 int section, nsections;
4783
27bf414c
JM
4784 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ASM));
4785 c_parser_consume_token (parser);
4786 if (c_parser_next_token_is_keyword (parser, RID_VOLATILE))
4787 {
4788 quals = c_parser_peek_token (parser)->value;
4789 c_parser_consume_token (parser);
4790 }
4791 else if (c_parser_next_token_is_keyword (parser, RID_CONST)
4792 || c_parser_next_token_is_keyword (parser, RID_RESTRICT))
4793 {
3ba09659
AH
4794 warning_at (c_parser_peek_token (parser)->location,
4795 0,
4796 "%E qualifier ignored on asm",
4797 c_parser_peek_token (parser)->value);
27bf414c
JM
4798 quals = NULL_TREE;
4799 c_parser_consume_token (parser);
4800 }
4801 else
4802 quals = NULL_TREE;
1c384bf1
RH
4803
4804 is_goto = false;
4805 if (c_parser_next_token_is_keyword (parser, RID_GOTO))
4806 {
4807 c_parser_consume_token (parser);
4808 is_goto = true;
4809 }
4810
27bf414c 4811 /* ??? Follow the C++ parser rather than using the
46c2514e
TT
4812 lex_untranslated_string kludge. */
4813 parser->lex_untranslated_string = true;
1c384bf1
RH
4814 ret = NULL;
4815
27bf414c 4816 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
1c384bf1
RH
4817 goto error;
4818
27bf414c 4819 str = c_parser_asm_string_literal (parser);
b85eb797 4820 if (str == NULL_TREE)
1c384bf1
RH
4821 goto error_close_paren;
4822
4823 simple = true;
4824 outputs = NULL_TREE;
4825 inputs = NULL_TREE;
4826 clobbers = NULL_TREE;
4827 labels = NULL_TREE;
4828
4829 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN) && !is_goto)
4830 goto done_asm;
4831
4832 /* Parse each colon-delimited section of operands. */
4833 nsections = 3 + is_goto;
4834 for (section = 0; section < nsections; ++section)
b85eb797 4835 {
1c384bf1
RH
4836 if (!c_parser_require (parser, CPP_COLON,
4837 is_goto
4838 ? "expected %<:%>"
4839 : "expected %<:%> or %<)%>"))
4840 goto error_close_paren;
4841
4842 /* Once past any colon, we're no longer a simple asm. */
4843 simple = false;
4844
4845 if ((!c_parser_next_token_is (parser, CPP_COLON)
4846 && !c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4847 || section == 3)
4848 switch (section)
4849 {
4850 case 0:
4851 /* For asm goto, we don't allow output operands, but reserve
4852 the slot for a future extension that does allow them. */
4853 if (!is_goto)
4854 outputs = c_parser_asm_operands (parser, false);
4855 break;
4856 case 1:
4857 inputs = c_parser_asm_operands (parser, true);
4858 break;
4859 case 2:
4860 clobbers = c_parser_asm_clobbers (parser);
4861 break;
4862 case 3:
4863 labels = c_parser_asm_goto_operands (parser);
4864 break;
4865 default:
4866 gcc_unreachable ();
4867 }
4868
4869 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN) && !is_goto)
4870 goto done_asm;
27bf414c 4871 }
1c384bf1 4872
27bf414c 4873 done_asm:
27bf414c
JM
4874 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
4875 {
4876 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
1c384bf1 4877 goto error;
27bf414c 4878 }
1c384bf1 4879
27bf414c
JM
4880 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
4881 c_parser_skip_to_end_of_block_or_statement (parser);
1c384bf1 4882
c2255bc4 4883 ret = build_asm_stmt (quals, build_asm_expr (asm_loc, str, outputs, inputs,
1c384bf1
RH
4884 clobbers, labels, simple));
4885
4886 error:
4887 parser->lex_untranslated_string = false;
27bf414c 4888 return ret;
1c384bf1
RH
4889
4890 error_close_paren:
4891 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
4892 goto error;
27bf414c
JM
4893}
4894
46bdb9cf
JM
4895/* Parse asm operands, a GNU extension. If CONVERT_P (for inputs but
4896 not outputs), apply the default conversion of functions and arrays
4897 to pointers.
27bf414c
JM
4898
4899 asm-operands:
4900 asm-operand
4901 asm-operands , asm-operand
4902
4903 asm-operand:
4904 asm-string-literal ( expression )
4905 [ identifier ] asm-string-literal ( expression )
4906*/
4907
4908static tree
46bdb9cf 4909c_parser_asm_operands (c_parser *parser, bool convert_p)
27bf414c
JM
4910{
4911 tree list = NULL_TREE;
c2255bc4 4912 location_t loc;
27bf414c
JM
4913 while (true)
4914 {
f2a71bbc
JM
4915 tree name, str;
4916 struct c_expr expr;
27bf414c
JM
4917 if (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
4918 {
4919 c_parser_consume_token (parser);
4920 if (c_parser_next_token_is (parser, CPP_NAME))
4921 {
4922 tree id = c_parser_peek_token (parser)->value;
4923 c_parser_consume_token (parser);
4924 name = build_string (IDENTIFIER_LENGTH (id),
4925 IDENTIFIER_POINTER (id));
4926 }
4927 else
4928 {
4929 c_parser_error (parser, "expected identifier");
4930 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
4931 return NULL_TREE;
4932 }
4933 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
4934 "expected %<]%>");
4935 }
4936 else
4937 name = NULL_TREE;
4938 str = c_parser_asm_string_literal (parser);
4939 if (str == NULL_TREE)
4940 return NULL_TREE;
46c2514e 4941 parser->lex_untranslated_string = false;
27bf414c
JM
4942 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4943 {
46c2514e 4944 parser->lex_untranslated_string = true;
27bf414c
JM
4945 return NULL_TREE;
4946 }
c2255bc4 4947 loc = c_parser_peek_token (parser)->location;
f2a71bbc 4948 expr = c_parser_expression (parser);
ebfbbdc5 4949 mark_exp_read (expr.value);
46bdb9cf 4950 if (convert_p)
c2255bc4 4951 expr = default_function_array_conversion (loc, expr);
928c19bb 4952 expr.value = c_fully_fold (expr.value, false, NULL);
46c2514e 4953 parser->lex_untranslated_string = true;
27bf414c
JM
4954 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
4955 {
4956 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
4957 return NULL_TREE;
4958 }
4959 list = chainon (list, build_tree_list (build_tree_list (name, str),
f2a71bbc 4960 expr.value));
27bf414c
JM
4961 if (c_parser_next_token_is (parser, CPP_COMMA))
4962 c_parser_consume_token (parser);
4963 else
4964 break;
4965 }
4966 return list;
4967}
4968
4969/* Parse asm clobbers, a GNU extension.
4970
4971 asm-clobbers:
4972 asm-string-literal
4973 asm-clobbers , asm-string-literal
4974*/
4975
4976static tree
4977c_parser_asm_clobbers (c_parser *parser)
4978{
4979 tree list = NULL_TREE;
4980 while (true)
4981 {
4982 tree str = c_parser_asm_string_literal (parser);
4983 if (str)
4984 list = tree_cons (NULL_TREE, str, list);
4985 else
4986 return NULL_TREE;
4987 if (c_parser_next_token_is (parser, CPP_COMMA))
4988 c_parser_consume_token (parser);
4989 else
4990 break;
4991 }
4992 return list;
4993}
4994
1c384bf1 4995/* Parse asm goto labels, a GNU extension.
b8698a0f 4996
1c384bf1
RH
4997 asm-goto-operands:
4998 identifier
4999 asm-goto-operands , identifier
5000*/
5001
5002static tree
5003c_parser_asm_goto_operands (c_parser *parser)
5004{
5005 tree list = NULL_TREE;
5006 while (true)
5007 {
5008 tree name, label;
5009
5010 if (c_parser_next_token_is (parser, CPP_NAME))
5011 {
5012 c_token *tok = c_parser_peek_token (parser);
5013 name = tok->value;
5014 label = lookup_label_for_goto (tok->location, name);
5015 c_parser_consume_token (parser);
5016 TREE_USED (label) = 1;
5017 }
5018 else
5019 {
5020 c_parser_error (parser, "expected identifier");
5021 return NULL_TREE;
5022 }
5023
5024 name = build_string (IDENTIFIER_LENGTH (name),
5025 IDENTIFIER_POINTER (name));
5026 list = tree_cons (name, label, list);
5027 if (c_parser_next_token_is (parser, CPP_COMMA))
5028 c_parser_consume_token (parser);
5029 else
5030 return nreverse (list);
5031 }
5032}
5033
27bf414c
JM
5034/* Parse an expression other than a compound expression; that is, an
5035 assignment expression (C90 6.3.16, C99 6.5.16). If AFTER is not
5036 NULL then it is an Objective-C message expression which is the
5037 primary-expression starting the expression as an initializer.
5038
5039 assignment-expression:
5040 conditional-expression
5041 unary-expression assignment-operator assignment-expression
5042
5043 assignment-operator: one of
5044 = *= /= %= += -= <<= >>= &= ^= |=
5045
5046 In GNU C we accept any conditional expression on the LHS and
5047 diagnose the invalid lvalue rather than producing a syntax
5048 error. */
5049
5050static struct c_expr
5051c_parser_expr_no_commas (c_parser *parser, struct c_expr *after)
5052{
5053 struct c_expr lhs, rhs, ret;
5054 enum tree_code code;
c2255bc4 5055 location_t op_location, exp_location;
27bf414c
JM
5056 gcc_assert (!after || c_dialect_objc ());
5057 lhs = c_parser_conditional_expression (parser, after);
c9f9eb5d 5058 op_location = c_parser_peek_token (parser)->location;
27bf414c
JM
5059 switch (c_parser_peek_token (parser)->type)
5060 {
5061 case CPP_EQ:
5062 code = NOP_EXPR;
5063 break;
5064 case CPP_MULT_EQ:
5065 code = MULT_EXPR;
5066 break;
5067 case CPP_DIV_EQ:
5068 code = TRUNC_DIV_EXPR;
5069 break;
5070 case CPP_MOD_EQ:
5071 code = TRUNC_MOD_EXPR;
5072 break;
5073 case CPP_PLUS_EQ:
5074 code = PLUS_EXPR;
5075 break;
5076 case CPP_MINUS_EQ:
5077 code = MINUS_EXPR;
5078 break;
5079 case CPP_LSHIFT_EQ:
5080 code = LSHIFT_EXPR;
5081 break;
5082 case CPP_RSHIFT_EQ:
5083 code = RSHIFT_EXPR;
5084 break;
5085 case CPP_AND_EQ:
5086 code = BIT_AND_EXPR;
5087 break;
5088 case CPP_XOR_EQ:
5089 code = BIT_XOR_EXPR;
5090 break;
5091 case CPP_OR_EQ:
5092 code = BIT_IOR_EXPR;
5093 break;
5094 default:
5095 return lhs;
5096 }
5097 c_parser_consume_token (parser);
c2255bc4 5098 exp_location = c_parser_peek_token (parser)->location;
27bf414c 5099 rhs = c_parser_expr_no_commas (parser, NULL);
ebfbbdc5 5100 rhs = default_function_array_read_conversion (exp_location, rhs);
32e8bb8e 5101 ret.value = build_modify_expr (op_location, lhs.value, lhs.original_type,
c2255bc4
AH
5102 code, exp_location, rhs.value,
5103 rhs.original_type);
27bf414c
JM
5104 if (code == NOP_EXPR)
5105 ret.original_code = MODIFY_EXPR;
5106 else
5107 {
5108 TREE_NO_WARNING (ret.value) = 1;
5109 ret.original_code = ERROR_MARK;
5110 }
6866c6e8 5111 ret.original_type = NULL;
27bf414c
JM
5112 return ret;
5113}
5114
5115/* Parse a conditional expression (C90 6.3.15, C99 6.5.15). If AFTER
5116 is not NULL then it is an Objective-C message expression which is
5117 the primary-expression starting the expression as an initializer.
5118
5119 conditional-expression:
5120 logical-OR-expression
5121 logical-OR-expression ? expression : conditional-expression
5122
5123 GNU extensions:
5124
5125 conditional-expression:
5126 logical-OR-expression ? : conditional-expression
5127*/
5128
5129static struct c_expr
5130c_parser_conditional_expression (c_parser *parser, struct c_expr *after)
5131{
5132 struct c_expr cond, exp1, exp2, ret;
d166d4c3 5133 location_t cond_loc, colon_loc, middle_loc;
ba47d38d 5134
27bf414c 5135 gcc_assert (!after || c_dialect_objc ());
ba47d38d 5136
27bf414c 5137 cond = c_parser_binary_expression (parser, after);
ba47d38d 5138
27bf414c
JM
5139 if (c_parser_next_token_is_not (parser, CPP_QUERY))
5140 return cond;
c2255bc4 5141 cond_loc = c_parser_peek_token (parser)->location;
ebfbbdc5 5142 cond = default_function_array_read_conversion (cond_loc, cond);
27bf414c
JM
5143 c_parser_consume_token (parser);
5144 if (c_parser_next_token_is (parser, CPP_COLON))
5145 {
8ce94e44 5146 tree eptype = NULL_TREE;
d166d4c3
AK
5147
5148 middle_loc = c_parser_peek_token (parser)->location;
5149 pedwarn (middle_loc, OPT_pedantic,
509c9d60 5150 "ISO C forbids omitting the middle term of a ?: expression");
d166d4c3 5151 warn_for_omitted_condop (middle_loc, cond.value);
8ce94e44
JM
5152 if (TREE_CODE (cond.value) == EXCESS_PRECISION_EXPR)
5153 {
5154 eptype = TREE_TYPE (cond.value);
5155 cond.value = TREE_OPERAND (cond.value, 0);
5156 }
27bf414c 5157 /* Make sure first operand is calculated only once. */
928c19bb 5158 exp1.value = c_save_expr (default_conversion (cond.value));
8ce94e44
JM
5159 if (eptype)
5160 exp1.value = build1 (EXCESS_PRECISION_EXPR, eptype, exp1.value);
6866c6e8 5161 exp1.original_type = NULL;
ba47d38d 5162 cond.value = c_objc_common_truthvalue_conversion (cond_loc, exp1.value);
7d882b83 5163 c_inhibit_evaluation_warnings += cond.value == truthvalue_true_node;
27bf414c
JM
5164 }
5165 else
5166 {
5167 cond.value
85498824 5168 = c_objc_common_truthvalue_conversion
ba47d38d 5169 (cond_loc, default_conversion (cond.value));
7d882b83 5170 c_inhibit_evaluation_warnings += cond.value == truthvalue_false_node;
46bdb9cf 5171 exp1 = c_parser_expression_conv (parser);
ebfbbdc5 5172 mark_exp_read (exp1.value);
7d882b83
ILT
5173 c_inhibit_evaluation_warnings +=
5174 ((cond.value == truthvalue_true_node)
5175 - (cond.value == truthvalue_false_node));
27bf414c 5176 }
744aa42f
ILT
5177
5178 colon_loc = c_parser_peek_token (parser)->location;
27bf414c
JM
5179 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
5180 {
7d882b83 5181 c_inhibit_evaluation_warnings -= cond.value == truthvalue_true_node;
27bf414c
JM
5182 ret.value = error_mark_node;
5183 ret.original_code = ERROR_MARK;
6866c6e8 5184 ret.original_type = NULL;
27bf414c
JM
5185 return ret;
5186 }
c2255bc4
AH
5187 {
5188 location_t exp2_loc = c_parser_peek_token (parser)->location;
5189 exp2 = c_parser_conditional_expression (parser, NULL);
ebfbbdc5 5190 exp2 = default_function_array_read_conversion (exp2_loc, exp2);
c2255bc4 5191 }
7d882b83 5192 c_inhibit_evaluation_warnings -= cond.value == truthvalue_true_node;
744aa42f 5193 ret.value = build_conditional_expr (colon_loc, cond.value,
928c19bb 5194 cond.original_code == C_MAYBE_CONST_EXPR,
d130ae11
ILT
5195 exp1.value, exp1.original_type,
5196 exp2.value, exp2.original_type);
27bf414c 5197 ret.original_code = ERROR_MARK;
6866c6e8
ILT
5198 if (exp1.value == error_mark_node || exp2.value == error_mark_node)
5199 ret.original_type = NULL;
5200 else
5201 {
5202 tree t1, t2;
5203
5204 /* If both sides are enum type, the default conversion will have
5205 made the type of the result be an integer type. We want to
5206 remember the enum types we started with. */
5207 t1 = exp1.original_type ? exp1.original_type : TREE_TYPE (exp1.value);
5208 t2 = exp2.original_type ? exp2.original_type : TREE_TYPE (exp2.value);
5209 ret.original_type = ((t1 != error_mark_node
5210 && t2 != error_mark_node
5211 && (TYPE_MAIN_VARIANT (t1)
5212 == TYPE_MAIN_VARIANT (t2)))
5213 ? t1
5214 : NULL);
5215 }
27bf414c
JM
5216 return ret;
5217}
5218
5219/* Parse a binary expression; that is, a logical-OR-expression (C90
5220 6.3.5-6.3.14, C99 6.5.5-6.5.14). If AFTER is not NULL then it is
5221 an Objective-C message expression which is the primary-expression
5222 starting the expression as an initializer.
5223
5224 multiplicative-expression:
5225 cast-expression
5226 multiplicative-expression * cast-expression
5227 multiplicative-expression / cast-expression
5228 multiplicative-expression % cast-expression
5229
5230 additive-expression:
5231 multiplicative-expression
5232 additive-expression + multiplicative-expression
5233 additive-expression - multiplicative-expression
5234
5235 shift-expression:
5236 additive-expression
5237 shift-expression << additive-expression
5238 shift-expression >> additive-expression
5239
5240 relational-expression:
5241 shift-expression
5242 relational-expression < shift-expression
5243 relational-expression > shift-expression
5244 relational-expression <= shift-expression
5245 relational-expression >= shift-expression
5246
5247 equality-expression:
5248 relational-expression
5249 equality-expression == relational-expression
5250 equality-expression != relational-expression
5251
5252 AND-expression:
5253 equality-expression
5254 AND-expression & equality-expression
5255
5256 exclusive-OR-expression:
5257 AND-expression
5258 exclusive-OR-expression ^ AND-expression
5259
5260 inclusive-OR-expression:
5261 exclusive-OR-expression
5262 inclusive-OR-expression | exclusive-OR-expression
5263
5264 logical-AND-expression:
5265 inclusive-OR-expression
5266 logical-AND-expression && inclusive-OR-expression
5267
5268 logical-OR-expression:
5269 logical-AND-expression
5270 logical-OR-expression || logical-AND-expression
5271*/
5272
5273static struct c_expr
5274c_parser_binary_expression (c_parser *parser, struct c_expr *after)
5275{
5276 /* A binary expression is parsed using operator-precedence parsing,
5277 with the operands being cast expressions. All the binary
5278 operators are left-associative. Thus a binary expression is of
5279 form:
5280
5281 E0 op1 E1 op2 E2 ...
5282
5283 which we represent on a stack. On the stack, the precedence
5284 levels are strictly increasing. When a new operator is
5285 encountered of higher precedence than that at the top of the
5286 stack, it is pushed; its LHS is the top expression, and its RHS
5287 is everything parsed until it is popped. When a new operator is
5288 encountered with precedence less than or equal to that at the top
5289 of the stack, triples E[i-1] op[i] E[i] are popped and replaced
5290 by the result of the operation until the operator at the top of
5291 the stack has lower precedence than the new operator or there is
5292 only one element on the stack; then the top expression is the LHS
5293 of the new operator. In the case of logical AND and OR
7d882b83
ILT
5294 expressions, we also need to adjust c_inhibit_evaluation_warnings
5295 as appropriate when the operators are pushed and popped. */
27bf414c
JM
5296
5297 /* The precedence levels, where 0 is a dummy lowest level used for
5298 the bottom of the stack. */
5299 enum prec {
5300 PREC_NONE,
5301 PREC_LOGOR,
5302 PREC_LOGAND,
5303 PREC_BITOR,
5304 PREC_BITXOR,
5305 PREC_BITAND,
5306 PREC_EQ,
5307 PREC_REL,
5308 PREC_SHIFT,
5309 PREC_ADD,
5310 PREC_MULT,
5311 NUM_PRECS
5312 };
5313 struct {
5314 /* The expression at this stack level. */
5315 struct c_expr expr;
5316 /* The precedence of the operator on its left, PREC_NONE at the
5317 bottom of the stack. */
5318 enum prec prec;
5319 /* The operation on its left. */
5320 enum tree_code op;
ca80e52b
EB
5321 /* The source location of this operation. */
5322 location_t loc;
27bf414c
JM
5323 } stack[NUM_PRECS];
5324 int sp;
ba47d38d 5325 /* Location of the binary operator. */
1f6d0c60 5326 location_t binary_loc = UNKNOWN_LOCATION; /* Quiet warning. */
27bf414c
JM
5327#define POP \
5328 do { \
5329 switch (stack[sp].op) \
5330 { \
5331 case TRUTH_ANDIF_EXPR: \
7d882b83
ILT
5332 c_inhibit_evaluation_warnings -= (stack[sp - 1].expr.value \
5333 == truthvalue_false_node); \
27bf414c
JM
5334 break; \
5335 case TRUTH_ORIF_EXPR: \
7d882b83
ILT
5336 c_inhibit_evaluation_warnings -= (stack[sp - 1].expr.value \
5337 == truthvalue_true_node); \
27bf414c
JM
5338 break; \
5339 default: \
5340 break; \
5341 } \
f2a71bbc 5342 stack[sp - 1].expr \
ebfbbdc5
JJ
5343 = default_function_array_read_conversion (stack[sp - 1].loc, \
5344 stack[sp - 1].expr); \
f2a71bbc 5345 stack[sp].expr \
ebfbbdc5
JJ
5346 = default_function_array_read_conversion (stack[sp].loc, \
5347 stack[sp].expr); \
ca80e52b 5348 stack[sp - 1].expr = parser_build_binary_op (stack[sp].loc, \
ba47d38d 5349 stack[sp].op, \
27bf414c
JM
5350 stack[sp - 1].expr, \
5351 stack[sp].expr); \
5352 sp--; \
5353 } while (0)
5354 gcc_assert (!after || c_dialect_objc ());
ca80e52b 5355 stack[0].loc = c_parser_peek_token (parser)->location;
27bf414c
JM
5356 stack[0].expr = c_parser_cast_expression (parser, after);
5357 stack[0].prec = PREC_NONE;
5358 sp = 0;
5359 while (true)
5360 {
5361 enum prec oprec;
5362 enum tree_code ocode;
5363 if (parser->error)
5364 goto out;
5365 switch (c_parser_peek_token (parser)->type)
5366 {
5367 case CPP_MULT:
5368 oprec = PREC_MULT;
5369 ocode = MULT_EXPR;
5370 break;
5371 case CPP_DIV:
5372 oprec = PREC_MULT;
5373 ocode = TRUNC_DIV_EXPR;
5374 break;
5375 case CPP_MOD:
5376 oprec = PREC_MULT;
5377 ocode = TRUNC_MOD_EXPR;
5378 break;
5379 case CPP_PLUS:
5380 oprec = PREC_ADD;
5381 ocode = PLUS_EXPR;
5382 break;
5383 case CPP_MINUS:
5384 oprec = PREC_ADD;
5385 ocode = MINUS_EXPR;
5386 break;
5387 case CPP_LSHIFT:
5388 oprec = PREC_SHIFT;
5389 ocode = LSHIFT_EXPR;
5390 break;
5391 case CPP_RSHIFT:
5392 oprec = PREC_SHIFT;
5393 ocode = RSHIFT_EXPR;
5394 break;
5395 case CPP_LESS:
5396 oprec = PREC_REL;
5397 ocode = LT_EXPR;
5398 break;
5399 case CPP_GREATER:
5400 oprec = PREC_REL;
5401 ocode = GT_EXPR;
5402 break;
5403 case CPP_LESS_EQ:
5404 oprec = PREC_REL;
5405 ocode = LE_EXPR;
5406 break;
5407 case CPP_GREATER_EQ:
5408 oprec = PREC_REL;
5409 ocode = GE_EXPR;
5410 break;
5411 case CPP_EQ_EQ:
5412 oprec = PREC_EQ;
5413 ocode = EQ_EXPR;
5414 break;
5415 case CPP_NOT_EQ:
5416 oprec = PREC_EQ;
5417 ocode = NE_EXPR;
5418 break;
5419 case CPP_AND:
5420 oprec = PREC_BITAND;
5421 ocode = BIT_AND_EXPR;
5422 break;
5423 case CPP_XOR:
5424 oprec = PREC_BITXOR;
5425 ocode = BIT_XOR_EXPR;
5426 break;
5427 case CPP_OR:
5428 oprec = PREC_BITOR;
5429 ocode = BIT_IOR_EXPR;
5430 break;
5431 case CPP_AND_AND:
5432 oprec = PREC_LOGAND;
5433 ocode = TRUTH_ANDIF_EXPR;
5434 break;
5435 case CPP_OR_OR:
5436 oprec = PREC_LOGOR;
5437 ocode = TRUTH_ORIF_EXPR;
5438 break;
5439 default:
5440 /* Not a binary operator, so end of the binary
5441 expression. */
5442 goto out;
5443 }
ba47d38d 5444 binary_loc = c_parser_peek_token (parser)->location;
27bf414c
JM
5445 c_parser_consume_token (parser);
5446 while (oprec <= stack[sp].prec)
5447 POP;
5448 switch (ocode)
5449 {
5450 case TRUTH_ANDIF_EXPR:
f2a71bbc 5451 stack[sp].expr
ebfbbdc5
JJ
5452 = default_function_array_read_conversion (stack[sp].loc,
5453 stack[sp].expr);
85498824 5454 stack[sp].expr.value = c_objc_common_truthvalue_conversion
ca80e52b 5455 (stack[sp].loc, default_conversion (stack[sp].expr.value));
7d882b83
ILT
5456 c_inhibit_evaluation_warnings += (stack[sp].expr.value
5457 == truthvalue_false_node);
27bf414c
JM
5458 break;
5459 case TRUTH_ORIF_EXPR:
f2a71bbc 5460 stack[sp].expr
ebfbbdc5
JJ
5461 = default_function_array_read_conversion (stack[sp].loc,
5462 stack[sp].expr);
85498824 5463 stack[sp].expr.value = c_objc_common_truthvalue_conversion
ca80e52b 5464 (stack[sp].loc, default_conversion (stack[sp].expr.value));
7d882b83
ILT
5465 c_inhibit_evaluation_warnings += (stack[sp].expr.value
5466 == truthvalue_true_node);
27bf414c
JM
5467 break;
5468 default:
5469 break;
5470 }
5471 sp++;
ca80e52b 5472 stack[sp].loc = binary_loc;
27bf414c
JM
5473 stack[sp].expr = c_parser_cast_expression (parser, NULL);
5474 stack[sp].prec = oprec;
5475 stack[sp].op = ocode;
c2255bc4 5476 stack[sp].loc = binary_loc;
27bf414c
JM
5477 }
5478 out:
5479 while (sp > 0)
5480 POP;
5481 return stack[0].expr;
5482#undef POP
5483}
5484
5485/* Parse a cast expression (C90 6.3.4, C99 6.5.4). If AFTER is not
5486 NULL then it is an Objective-C message expression which is the
5487 primary-expression starting the expression as an initializer.
5488
5489 cast-expression:
5490 unary-expression
5491 ( type-name ) unary-expression
5492*/
5493
5494static struct c_expr
5495c_parser_cast_expression (c_parser *parser, struct c_expr *after)
5496{
c2255bc4 5497 location_t cast_loc = c_parser_peek_token (parser)->location;
27bf414c
JM
5498 gcc_assert (!after || c_dialect_objc ());
5499 if (after)
c2255bc4
AH
5500 return c_parser_postfix_expression_after_primary (parser,
5501 cast_loc, *after);
27bf414c
JM
5502 /* If the expression begins with a parenthesized type name, it may
5503 be either a cast or a compound literal; we need to see whether
5504 the next character is '{' to tell the difference. If not, it is
5505 an unary expression. */
5506 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
5507 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
5508 {
5509 struct c_type_name *type_name;
5510 struct c_expr ret;
f2a71bbc 5511 struct c_expr expr;
27bf414c
JM
5512 c_parser_consume_token (parser);
5513 type_name = c_parser_type_name (parser);
5514 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
5515 if (type_name == NULL)
5516 {
5517 ret.value = error_mark_node;
5518 ret.original_code = ERROR_MARK;
6866c6e8 5519 ret.original_type = NULL;
27bf414c
JM
5520 return ret;
5521 }
33c9159e
AH
5522
5523 /* Save casted types in the function's used types hash table. */
8d8d1a28 5524 used_types_insert (type_name->specs->type);
33c9159e 5525
27bf414c 5526 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
24b97832 5527 return c_parser_postfix_expression_after_paren_type (parser, type_name,
c2255bc4
AH
5528 cast_loc);
5529 {
5530 location_t expr_loc = c_parser_peek_token (parser)->location;
5531 expr = c_parser_cast_expression (parser, NULL);
ebfbbdc5 5532 expr = default_function_array_read_conversion (expr_loc, expr);
c2255bc4
AH
5533 }
5534 ret.value = c_cast_expr (cast_loc, type_name, expr.value);
27bf414c 5535 ret.original_code = ERROR_MARK;
6866c6e8 5536 ret.original_type = NULL;
27bf414c
JM
5537 return ret;
5538 }
5539 else
5540 return c_parser_unary_expression (parser);
5541}
5542
5543/* Parse an unary expression (C90 6.3.3, C99 6.5.3).
5544
5545 unary-expression:
5546 postfix-expression
5547 ++ unary-expression
5548 -- unary-expression
5549 unary-operator cast-expression
5550 sizeof unary-expression
5551 sizeof ( type-name )
5552
5553 unary-operator: one of
5554 & * + - ~ !
5555
5556 GNU extensions:
5557
5558 unary-expression:
5559 __alignof__ unary-expression
5560 __alignof__ ( type-name )
5561 && identifier
5562
5563 unary-operator: one of
5564 __extension__ __real__ __imag__
5565
5566 In addition, the GNU syntax treats ++ and -- as unary operators, so
5567 they may be applied to cast expressions with errors for non-lvalues
5568 given later. */
5569
5570static struct c_expr
5571c_parser_unary_expression (c_parser *parser)
5572{
5573 int ext;
46bdb9cf 5574 struct c_expr ret, op;
c2255bc4
AH
5575 location_t op_loc = c_parser_peek_token (parser)->location;
5576 location_t exp_loc;
6866c6e8
ILT
5577 ret.original_code = ERROR_MARK;
5578 ret.original_type = NULL;
27bf414c
JM
5579 switch (c_parser_peek_token (parser)->type)
5580 {
5581 case CPP_PLUS_PLUS:
5582 c_parser_consume_token (parser);
c2255bc4 5583 exp_loc = c_parser_peek_token (parser)->location;
46bdb9cf 5584 op = c_parser_cast_expression (parser, NULL);
ebfbbdc5 5585 op = default_function_array_read_conversion (exp_loc, op);
c2255bc4 5586 return parser_build_unary_op (op_loc, PREINCREMENT_EXPR, op);
27bf414c
JM
5587 case CPP_MINUS_MINUS:
5588 c_parser_consume_token (parser);
c2255bc4 5589 exp_loc = c_parser_peek_token (parser)->location;
46bdb9cf 5590 op = c_parser_cast_expression (parser, NULL);
ebfbbdc5 5591 op = default_function_array_read_conversion (exp_loc, op);
c2255bc4 5592 return parser_build_unary_op (op_loc, PREDECREMENT_EXPR, op);
27bf414c
JM
5593 case CPP_AND:
5594 c_parser_consume_token (parser);
ebfbbdc5
JJ
5595 op = c_parser_cast_expression (parser, NULL);
5596 mark_exp_read (op.value);
5597 return parser_build_unary_op (op_loc, ADDR_EXPR, op);
27bf414c
JM
5598 case CPP_MULT:
5599 c_parser_consume_token (parser);
c2255bc4 5600 exp_loc = c_parser_peek_token (parser)->location;
46bdb9cf 5601 op = c_parser_cast_expression (parser, NULL);
ebfbbdc5 5602 op = default_function_array_read_conversion (exp_loc, op);
dd865ef6 5603 ret.value = build_indirect_ref (op_loc, op.value, RO_UNARY_STAR);
27bf414c
JM
5604 return ret;
5605 case CPP_PLUS:
44c21c7f 5606 if (!c_dialect_objc () && !in_system_header)
c2255bc4 5607 warning_at (op_loc,
3ba09659
AH
5608 OPT_Wtraditional,
5609 "traditional C rejects the unary plus operator");
c7412148 5610 c_parser_consume_token (parser);
c2255bc4 5611 exp_loc = c_parser_peek_token (parser)->location;
46bdb9cf 5612 op = c_parser_cast_expression (parser, NULL);
ebfbbdc5 5613 op = default_function_array_read_conversion (exp_loc, op);
c2255bc4 5614 return parser_build_unary_op (op_loc, CONVERT_EXPR, op);
27bf414c
JM
5615 case CPP_MINUS:
5616 c_parser_consume_token (parser);
c2255bc4 5617 exp_loc = c_parser_peek_token (parser)->location;
46bdb9cf 5618 op = c_parser_cast_expression (parser, NULL);
ebfbbdc5 5619 op = default_function_array_read_conversion (exp_loc, op);
c2255bc4 5620 return parser_build_unary_op (op_loc, NEGATE_EXPR, op);
27bf414c
JM
5621 case CPP_COMPL:
5622 c_parser_consume_token (parser);
c2255bc4 5623 exp_loc = c_parser_peek_token (parser)->location;
46bdb9cf 5624 op = c_parser_cast_expression (parser, NULL);
ebfbbdc5 5625 op = default_function_array_read_conversion (exp_loc, op);
c2255bc4 5626 return parser_build_unary_op (op_loc, BIT_NOT_EXPR, op);
27bf414c
JM
5627 case CPP_NOT:
5628 c_parser_consume_token (parser);
c2255bc4 5629 exp_loc = c_parser_peek_token (parser)->location;
46bdb9cf 5630 op = c_parser_cast_expression (parser, NULL);
ebfbbdc5 5631 op = default_function_array_read_conversion (exp_loc, op);
c2255bc4 5632 return parser_build_unary_op (op_loc, TRUTH_NOT_EXPR, op);
27bf414c
JM
5633 case CPP_AND_AND:
5634 /* Refer to the address of a label as a pointer. */
5635 c_parser_consume_token (parser);
5636 if (c_parser_next_token_is (parser, CPP_NAME))
5637 {
5638 ret.value = finish_label_address_expr
c2255bc4 5639 (c_parser_peek_token (parser)->value, op_loc);
27bf414c 5640 c_parser_consume_token (parser);
27bf414c
JM
5641 }
5642 else
5643 {
5644 c_parser_error (parser, "expected identifier");
5645 ret.value = error_mark_node;
27bf414c 5646 }
43f6dfd3 5647 return ret;
27bf414c
JM
5648 case CPP_KEYWORD:
5649 switch (c_parser_peek_token (parser)->keyword)
5650 {
5651 case RID_SIZEOF:
5652 return c_parser_sizeof_expression (parser);
5653 case RID_ALIGNOF:
5654 return c_parser_alignof_expression (parser);
5655 case RID_EXTENSION:
5656 c_parser_consume_token (parser);
5657 ext = disable_extension_diagnostics ();
5658 ret = c_parser_cast_expression (parser, NULL);
5659 restore_extension_diagnostics (ext);
5660 return ret;
5661 case RID_REALPART:
5662 c_parser_consume_token (parser);
c2255bc4 5663 exp_loc = c_parser_peek_token (parser)->location;
46bdb9cf 5664 op = c_parser_cast_expression (parser, NULL);
c2255bc4
AH
5665 op = default_function_array_conversion (exp_loc, op);
5666 return parser_build_unary_op (op_loc, REALPART_EXPR, op);
27bf414c
JM
5667 case RID_IMAGPART:
5668 c_parser_consume_token (parser);
c2255bc4 5669 exp_loc = c_parser_peek_token (parser)->location;
46bdb9cf 5670 op = c_parser_cast_expression (parser, NULL);
c2255bc4
AH
5671 op = default_function_array_conversion (exp_loc, op);
5672 return parser_build_unary_op (op_loc, IMAGPART_EXPR, op);
27bf414c
JM
5673 default:
5674 return c_parser_postfix_expression (parser);
5675 }
5676 default:
5677 return c_parser_postfix_expression (parser);
5678 }
5679}
5680
5681/* Parse a sizeof expression. */
5682
5683static struct c_expr
5684c_parser_sizeof_expression (c_parser *parser)
5685{
5686 struct c_expr expr;
c7412148 5687 location_t expr_loc;
27bf414c
JM
5688 gcc_assert (c_parser_next_token_is_keyword (parser, RID_SIZEOF));
5689 c_parser_consume_token (parser);
7d882b83 5690 c_inhibit_evaluation_warnings++;
27bf414c
JM
5691 in_sizeof++;
5692 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
5693 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
5694 {
5695 /* Either sizeof ( type-name ) or sizeof unary-expression
5696 starting with a compound literal. */
5697 struct c_type_name *type_name;
5698 c_parser_consume_token (parser);
c7412148 5699 expr_loc = c_parser_peek_token (parser)->location;
27bf414c
JM
5700 type_name = c_parser_type_name (parser);
5701 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
5702 if (type_name == NULL)
5703 {
5704 struct c_expr ret;
7d882b83 5705 c_inhibit_evaluation_warnings--;
27bf414c
JM
5706 in_sizeof--;
5707 ret.value = error_mark_node;
5708 ret.original_code = ERROR_MARK;
6866c6e8 5709 ret.original_type = NULL;
27bf414c
JM
5710 return ret;
5711 }
5712 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
5713 {
5714 expr = c_parser_postfix_expression_after_paren_type (parser,
24b97832
ILT
5715 type_name,
5716 expr_loc);
27bf414c
JM
5717 goto sizeof_expr;
5718 }
5719 /* sizeof ( type-name ). */
7d882b83 5720 c_inhibit_evaluation_warnings--;
27bf414c 5721 in_sizeof--;
c2255bc4 5722 return c_expr_sizeof_type (expr_loc, type_name);
27bf414c
JM
5723 }
5724 else
5725 {
c7412148 5726 expr_loc = c_parser_peek_token (parser)->location;
27bf414c
JM
5727 expr = c_parser_unary_expression (parser);
5728 sizeof_expr:
7d882b83 5729 c_inhibit_evaluation_warnings--;
27bf414c 5730 in_sizeof--;
ebfbbdc5 5731 mark_exp_read (expr.value);
27bf414c
JM
5732 if (TREE_CODE (expr.value) == COMPONENT_REF
5733 && DECL_C_BIT_FIELD (TREE_OPERAND (expr.value, 1)))
3ba09659 5734 error_at (expr_loc, "%<sizeof%> applied to a bit-field");
c2255bc4 5735 return c_expr_sizeof_expr (expr_loc, expr);
27bf414c
JM
5736 }
5737}
5738
5739/* Parse an alignof expression. */
5740
5741static struct c_expr
5742c_parser_alignof_expression (c_parser *parser)
5743{
5744 struct c_expr expr;
c2255bc4 5745 location_t loc = c_parser_peek_token (parser)->location;
27bf414c
JM
5746 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ALIGNOF));
5747 c_parser_consume_token (parser);
7d882b83 5748 c_inhibit_evaluation_warnings++;
27bf414c
JM
5749 in_alignof++;
5750 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
5751 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
5752 {
5753 /* Either __alignof__ ( type-name ) or __alignof__
5754 unary-expression starting with a compound literal. */
24b97832 5755 location_t loc;
27bf414c
JM
5756 struct c_type_name *type_name;
5757 struct c_expr ret;
5758 c_parser_consume_token (parser);
24b97832 5759 loc = c_parser_peek_token (parser)->location;
27bf414c
JM
5760 type_name = c_parser_type_name (parser);
5761 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
5762 if (type_name == NULL)
5763 {
5764 struct c_expr ret;
7d882b83 5765 c_inhibit_evaluation_warnings--;
27bf414c
JM
5766 in_alignof--;
5767 ret.value = error_mark_node;
5768 ret.original_code = ERROR_MARK;
6866c6e8 5769 ret.original_type = NULL;
27bf414c
JM
5770 return ret;
5771 }
5772 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
5773 {
5774 expr = c_parser_postfix_expression_after_paren_type (parser,
24b97832
ILT
5775 type_name,
5776 loc);
27bf414c
JM
5777 goto alignof_expr;
5778 }
5779 /* alignof ( type-name ). */
7d882b83 5780 c_inhibit_evaluation_warnings--;
27bf414c 5781 in_alignof--;
c2255bc4 5782 ret.value = c_alignof (loc, groktypename (type_name, NULL, NULL));
27bf414c 5783 ret.original_code = ERROR_MARK;
6866c6e8 5784 ret.original_type = NULL;
27bf414c
JM
5785 return ret;
5786 }
5787 else
5788 {
5789 struct c_expr ret;
5790 expr = c_parser_unary_expression (parser);
5791 alignof_expr:
ebfbbdc5 5792 mark_exp_read (expr.value);
7d882b83 5793 c_inhibit_evaluation_warnings--;
27bf414c 5794 in_alignof--;
c2255bc4 5795 ret.value = c_alignof_expr (loc, expr.value);
27bf414c 5796 ret.original_code = ERROR_MARK;
6866c6e8 5797 ret.original_type = NULL;
27bf414c
JM
5798 return ret;
5799 }
5800}
5801
5802/* Parse a postfix expression (C90 6.3.1-6.3.2, C99 6.5.1-6.5.2).
5803
5804 postfix-expression:
5805 primary-expression
5806 postfix-expression [ expression ]
5807 postfix-expression ( argument-expression-list[opt] )
5808 postfix-expression . identifier
5809 postfix-expression -> identifier
5810 postfix-expression ++
5811 postfix-expression --
5812 ( type-name ) { initializer-list }
5813 ( type-name ) { initializer-list , }
5814
5815 argument-expression-list:
5816 argument-expression
5817 argument-expression-list , argument-expression
5818
5819 primary-expression:
5820 identifier
5821 constant
5822 string-literal
5823 ( expression )
5824
5825 GNU extensions:
5826
5827 primary-expression:
5828 __func__
5829 (treated as a keyword in GNU C)
5830 __FUNCTION__
5831 __PRETTY_FUNCTION__
5832 ( compound-statement )
5833 __builtin_va_arg ( assignment-expression , type-name )
5834 __builtin_offsetof ( type-name , offsetof-member-designator )
5835 __builtin_choose_expr ( assignment-expression ,
5836 assignment-expression ,
5837 assignment-expression )
5838 __builtin_types_compatible_p ( type-name , type-name )
5839
5840 offsetof-member-designator:
5841 identifier
5842 offsetof-member-designator . identifier
5843 offsetof-member-designator [ expression ]
5844
5845 Objective-C:
5846
5847 primary-expression:
5848 [ objc-receiver objc-message-args ]
5849 @selector ( objc-selector-arg )
5850 @protocol ( identifier )
5851 @encode ( type-name )
5852 objc-string-literal
bede2adc 5853 Classname . identifier
27bf414c
JM
5854*/
5855
5856static struct c_expr
5857c_parser_postfix_expression (c_parser *parser)
5858{
5859 struct c_expr expr, e1, e2, e3;
5860 struct c_type_name *t1, *t2;
c2255bc4 5861 location_t loc = c_parser_peek_token (parser)->location;;
6866c6e8
ILT
5862 expr.original_code = ERROR_MARK;
5863 expr.original_type = NULL;
27bf414c
JM
5864 switch (c_parser_peek_token (parser)->type)
5865 {
5866 case CPP_NUMBER:
754ccf7c 5867 expr.value = c_parser_peek_token (parser)->value;
754ccf7c
JJ
5868 loc = c_parser_peek_token (parser)->location;
5869 c_parser_consume_token (parser);
5870 if (TREE_CODE (expr.value) == FIXED_CST
5871 && !targetm.fixed_point_supported_p ())
5872 {
5873 error_at (loc, "fixed-point types not supported for this target");
5874 expr.value = error_mark_node;
5875 }
5876 break;
27bf414c 5877 case CPP_CHAR:
b6baa67d
KVH
5878 case CPP_CHAR16:
5879 case CPP_CHAR32:
27bf414c
JM
5880 case CPP_WCHAR:
5881 expr.value = c_parser_peek_token (parser)->value;
27bf414c
JM
5882 c_parser_consume_token (parser);
5883 break;
5884 case CPP_STRING:
b6baa67d
KVH
5885 case CPP_STRING16:
5886 case CPP_STRING32:
27bf414c 5887 case CPP_WSTRING:
2c6e3f55 5888 case CPP_UTF8STRING:
27bf414c
JM
5889 expr.value = c_parser_peek_token (parser)->value;
5890 expr.original_code = STRING_CST;
5891 c_parser_consume_token (parser);
5892 break;
5893 case CPP_OBJC_STRING:
5894 gcc_assert (c_dialect_objc ());
5895 expr.value
5896 = objc_build_string_object (c_parser_peek_token (parser)->value);
27bf414c
JM
5897 c_parser_consume_token (parser);
5898 break;
5899 case CPP_NAME:
bede2adc 5900 switch (c_parser_peek_token (parser)->id_kind)
27bf414c 5901 {
bede2adc
NP
5902 case C_ID_ID:
5903 {
5904 tree id = c_parser_peek_token (parser)->value;
5905 c_parser_consume_token (parser);
5906 expr.value = build_external_ref (loc, id,
5907 (c_parser_peek_token (parser)->type
5908 == CPP_OPEN_PAREN),
5909 &expr.original_type);
5910 break;
5911 }
5912 case C_ID_CLASSNAME:
5913 {
5914 /* Here we parse the Objective-C 2.0 Class.name dot
5915 syntax. */
5916 tree class_name = c_parser_peek_token (parser)->value;
5917 tree component;
5918 c_parser_consume_token (parser);
5919 gcc_assert (c_dialect_objc ());
5920 if (!c_parser_require (parser, CPP_DOT, "expected %<.%>"))
5921 {
5922 expr.value = error_mark_node;
5923 break;
5924 }
5925 if (c_parser_next_token_is_not (parser, CPP_NAME))
5926 {
5927 c_parser_error (parser, "expected identifier");
5928 expr.value = error_mark_node;
5929 break;
5930 }
5931 component = c_parser_peek_token (parser)->value;
5932 c_parser_consume_token (parser);
5933 expr.value = objc_build_class_component_ref (class_name,
5934 component);
5935 break;
5936 }
5937 default:
27bf414c
JM
5938 c_parser_error (parser, "expected expression");
5939 expr.value = error_mark_node;
27bf414c
JM
5940 break;
5941 }
27bf414c
JM
5942 break;
5943 case CPP_OPEN_PAREN:
5944 /* A parenthesized expression, statement expression or compound
5945 literal. */
5946 if (c_parser_peek_2nd_token (parser)->type == CPP_OPEN_BRACE)
5947 {
5948 /* A statement expression. */
5949 tree stmt;
c2255bc4 5950 location_t brace_loc;
27bf414c 5951 c_parser_consume_token (parser);
c2255bc4 5952 brace_loc = c_parser_peek_token (parser)->location;
27bf414c
JM
5953 c_parser_consume_token (parser);
5954 if (cur_stmt_list == NULL)
5955 {
c2255bc4 5956 error_at (loc, "braced-group within expression allowed "
3ba09659 5957 "only inside a function");
27bf414c
JM
5958 parser->error = true;
5959 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
5960 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5961 expr.value = error_mark_node;
27bf414c
JM
5962 break;
5963 }
5964 stmt = c_begin_stmt_expr ();
5965 c_parser_compound_statement_nostart (parser);
5966 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5967 "expected %<)%>");
b8698a0f 5968 pedwarn (loc, OPT_pedantic,
509c9d60 5969 "ISO C forbids braced-groups within expressions");
c2255bc4 5970 expr.value = c_finish_stmt_expr (brace_loc, stmt);
82c3c067 5971 mark_exp_read (expr.value);
27bf414c
JM
5972 }
5973 else if (c_token_starts_typename (c_parser_peek_2nd_token (parser)))
5974 {
5975 /* A compound literal. ??? Can we actually get here rather
5976 than going directly to
5977 c_parser_postfix_expression_after_paren_type from
5978 elsewhere? */
24b97832 5979 location_t loc;
27bf414c
JM
5980 struct c_type_name *type_name;
5981 c_parser_consume_token (parser);
24b97832 5982 loc = c_parser_peek_token (parser)->location;
27bf414c
JM
5983 type_name = c_parser_type_name (parser);
5984 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5985 "expected %<)%>");
5986 if (type_name == NULL)
5987 {
5988 expr.value = error_mark_node;
27bf414c
JM
5989 }
5990 else
5991 expr = c_parser_postfix_expression_after_paren_type (parser,
24b97832
ILT
5992 type_name,
5993 loc);
27bf414c
JM
5994 }
5995 else
5996 {
5997 /* A parenthesized expression. */
5998 c_parser_consume_token (parser);
5999 expr = c_parser_expression (parser);
6000 if (TREE_CODE (expr.value) == MODIFY_EXPR)
6001 TREE_NO_WARNING (expr.value) = 1;
928c19bb
JM
6002 if (expr.original_code != C_MAYBE_CONST_EXPR)
6003 expr.original_code = ERROR_MARK;
6866c6e8 6004 /* Don't change EXPR.ORIGINAL_TYPE. */
27bf414c
JM
6005 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
6006 "expected %<)%>");
6007 }
6008 break;
6009 case CPP_KEYWORD:
6010 switch (c_parser_peek_token (parser)->keyword)
6011 {
6012 case RID_FUNCTION_NAME:
6013 case RID_PRETTY_FUNCTION_NAME:
6014 case RID_C99_FUNCTION_NAME:
c2255bc4 6015 expr.value = fname_decl (loc,
3ba09659 6016 c_parser_peek_token (parser)->keyword,
27bf414c 6017 c_parser_peek_token (parser)->value);
27bf414c
JM
6018 c_parser_consume_token (parser);
6019 break;
6020 case RID_VA_ARG:
6021 c_parser_consume_token (parser);
6022 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6023 {
6024 expr.value = error_mark_node;
27bf414c
JM
6025 break;
6026 }
6027 e1 = c_parser_expr_no_commas (parser, NULL);
ebfbbdc5 6028 mark_exp_read (e1.value);
928c19bb 6029 e1.value = c_fully_fold (e1.value, false, NULL);
27bf414c
JM
6030 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
6031 {
6032 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6033 expr.value = error_mark_node;
27bf414c
JM
6034 break;
6035 }
72b5577d 6036 loc = c_parser_peek_token (parser)->location;
27bf414c
JM
6037 t1 = c_parser_type_name (parser);
6038 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
6039 "expected %<)%>");
6040 if (t1 == NULL)
6041 {
6042 expr.value = error_mark_node;
27bf414c
JM
6043 }
6044 else
6045 {
928c19bb 6046 tree type_expr = NULL_TREE;
c2255bc4
AH
6047 expr.value = c_build_va_arg (loc, e1.value,
6048 groktypename (t1, &type_expr, NULL));
928c19bb
JM
6049 if (type_expr)
6050 {
6051 expr.value = build2 (C_MAYBE_CONST_EXPR,
6052 TREE_TYPE (expr.value), type_expr,
6053 expr.value);
6054 C_MAYBE_CONST_EXPR_NON_CONST (expr.value) = true;
6055 }
27bf414c
JM
6056 }
6057 break;
6058 case RID_OFFSETOF:
6059 c_parser_consume_token (parser);
6060 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6061 {
6062 expr.value = error_mark_node;
27bf414c
JM
6063 break;
6064 }
6065 t1 = c_parser_type_name (parser);
6066 if (t1 == NULL)
6067 {
6068 expr.value = error_mark_node;
27bf414c
JM
6069 break;
6070 }
6071 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
6072 {
6073 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6074 expr.value = error_mark_node;
27bf414c
JM
6075 break;
6076 }
6077 {
928c19bb 6078 tree type = groktypename (t1, NULL, NULL);
27bf414c
JM
6079 tree offsetof_ref;
6080 if (type == error_mark_node)
6081 offsetof_ref = error_mark_node;
6082 else
c2255bc4
AH
6083 {
6084 offsetof_ref = build1 (INDIRECT_REF, type, null_pointer_node);
6085 SET_EXPR_LOCATION (offsetof_ref, loc);
6086 }
27bf414c
JM
6087 /* Parse the second argument to __builtin_offsetof. We
6088 must have one identifier, and beyond that we want to
6089 accept sub structure and sub array references. */
6090 if (c_parser_next_token_is (parser, CPP_NAME))
6091 {
6092 offsetof_ref = build_component_ref
c2255bc4 6093 (loc, offsetof_ref, c_parser_peek_token (parser)->value);
27bf414c
JM
6094 c_parser_consume_token (parser);
6095 while (c_parser_next_token_is (parser, CPP_DOT)
6096 || c_parser_next_token_is (parser,
634b5df5
JJ
6097 CPP_OPEN_SQUARE)
6098 || c_parser_next_token_is (parser,
6099 CPP_DEREF))
27bf414c 6100 {
634b5df5
JJ
6101 if (c_parser_next_token_is (parser, CPP_DEREF))
6102 {
6103 loc = c_parser_peek_token (parser)->location;
c2255bc4
AH
6104 offsetof_ref = build_array_ref (loc,
6105 offsetof_ref,
6106 integer_zero_node);
634b5df5
JJ
6107 goto do_dot;
6108 }
6109 else if (c_parser_next_token_is (parser, CPP_DOT))
27bf414c 6110 {
634b5df5 6111 do_dot:
27bf414c
JM
6112 c_parser_consume_token (parser);
6113 if (c_parser_next_token_is_not (parser,
6114 CPP_NAME))
6115 {
6116 c_parser_error (parser, "expected identifier");
6117 break;
6118 }
6119 offsetof_ref = build_component_ref
c2255bc4 6120 (loc, offsetof_ref,
27bf414c
JM
6121 c_parser_peek_token (parser)->value);
6122 c_parser_consume_token (parser);
6123 }
6124 else
6125 {
6126 tree idx;
6a3799eb 6127 loc = c_parser_peek_token (parser)->location;
27bf414c
JM
6128 c_parser_consume_token (parser);
6129 idx = c_parser_expression (parser).value;
928c19bb 6130 idx = c_fully_fold (idx, false, NULL);
27bf414c
JM
6131 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
6132 "expected %<]%>");
c2255bc4 6133 offsetof_ref = build_array_ref (loc, offsetof_ref, idx);
27bf414c
JM
6134 }
6135 }
6136 }
6137 else
6138 c_parser_error (parser, "expected identifier");
6139 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
6140 "expected %<)%>");
6d4d7b0e 6141 expr.value = fold_offsetof (offsetof_ref, NULL_TREE);
27bf414c
JM
6142 }
6143 break;
6144 case RID_CHOOSE_EXPR:
6145 c_parser_consume_token (parser);
6146 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6147 {
6148 expr.value = error_mark_node;
27bf414c
JM
6149 break;
6150 }
c7412148 6151 loc = c_parser_peek_token (parser)->location;
27bf414c
JM
6152 e1 = c_parser_expr_no_commas (parser, NULL);
6153 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
6154 {
6155 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6156 expr.value = error_mark_node;
27bf414c
JM
6157 break;
6158 }
6159 e2 = c_parser_expr_no_commas (parser, NULL);
6160 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
6161 {
6162 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6163 expr.value = error_mark_node;
27bf414c
JM
6164 break;
6165 }
6166 e3 = c_parser_expr_no_commas (parser, NULL);
6167 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
6168 "expected %<)%>");
6169 {
6170 tree c;
6171
4d84fe7c 6172 c = e1.value;
ebfbbdc5
JJ
6173 mark_exp_read (e2.value);
6174 mark_exp_read (e3.value);
928c19bb
JM
6175 if (TREE_CODE (c) != INTEGER_CST
6176 || !INTEGRAL_TYPE_P (TREE_TYPE (c)))
3ba09659
AH
6177 error_at (loc,
6178 "first argument to %<__builtin_choose_expr%> not"
6179 " a constant");
928c19bb 6180 constant_expression_warning (c);
27bf414c
JM
6181 expr = integer_zerop (c) ? e3 : e2;
6182 }
6183 break;
6184 case RID_TYPES_COMPATIBLE_P:
6185 c_parser_consume_token (parser);
6186 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6187 {
6188 expr.value = error_mark_node;
27bf414c
JM
6189 break;
6190 }
6191 t1 = c_parser_type_name (parser);
6192 if (t1 == NULL)
6193 {
6194 expr.value = error_mark_node;
27bf414c
JM
6195 break;
6196 }
6197 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
6198 {
6199 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6200 expr.value = error_mark_node;
27bf414c
JM
6201 break;
6202 }
6203 t2 = c_parser_type_name (parser);
6204 if (t2 == NULL)
6205 {
6206 expr.value = error_mark_node;
27bf414c
JM
6207 break;
6208 }
6209 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
6210 "expected %<)%>");
6211 {
6212 tree e1, e2;
6213
928c19bb
JM
6214 e1 = TYPE_MAIN_VARIANT (groktypename (t1, NULL, NULL));
6215 e2 = TYPE_MAIN_VARIANT (groktypename (t2, NULL, NULL));
27bf414c 6216
9a9d280e
AS
6217 expr.value
6218 = comptypes (e1, e2) ? integer_one_node : integer_zero_node;
27bf414c
JM
6219 }
6220 break;
6221 case RID_AT_SELECTOR:
6222 gcc_assert (c_dialect_objc ());
6223 c_parser_consume_token (parser);
6224 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6225 {
6226 expr.value = error_mark_node;
27bf414c
JM
6227 break;
6228 }
6229 {
6230 tree sel = c_parser_objc_selector_arg (parser);
6231 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
6232 "expected %<)%>");
c2255bc4 6233 expr.value = objc_build_selector_expr (loc, sel);
27bf414c
JM
6234 }
6235 break;
6236 case RID_AT_PROTOCOL:
6237 gcc_assert (c_dialect_objc ());
6238 c_parser_consume_token (parser);
6239 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6240 {
6241 expr.value = error_mark_node;
27bf414c
JM
6242 break;
6243 }
6244 if (c_parser_next_token_is_not (parser, CPP_NAME))
6245 {
6246 c_parser_error (parser, "expected identifier");
6247 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6248 expr.value = error_mark_node;
27bf414c
JM
6249 break;
6250 }
6251 {
6252 tree id = c_parser_peek_token (parser)->value;
6253 c_parser_consume_token (parser);
6254 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
6255 "expected %<)%>");
6256 expr.value = objc_build_protocol_expr (id);
27bf414c
JM
6257 }
6258 break;
6259 case RID_AT_ENCODE:
6260 /* Extension to support C-structures in the archiver. */
6261 gcc_assert (c_dialect_objc ());
6262 c_parser_consume_token (parser);
6263 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6264 {
6265 expr.value = error_mark_node;
27bf414c
JM
6266 break;
6267 }
6268 t1 = c_parser_type_name (parser);
6269 if (t1 == NULL)
6270 {
6271 expr.value = error_mark_node;
27bf414c
JM
6272 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6273 break;
6274 }
6275 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
6276 "expected %<)%>");
6277 {
928c19bb 6278 tree type = groktypename (t1, NULL, NULL);
27bf414c 6279 expr.value = objc_build_encode_expr (type);
27bf414c
JM
6280 }
6281 break;
6282 default:
6283 c_parser_error (parser, "expected expression");
6284 expr.value = error_mark_node;
27bf414c
JM
6285 break;
6286 }
6287 break;
6288 case CPP_OPEN_SQUARE:
6289 if (c_dialect_objc ())
6290 {
6291 tree receiver, args;
6292 c_parser_consume_token (parser);
6293 receiver = c_parser_objc_receiver (parser);
6294 args = c_parser_objc_message_args (parser);
6295 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
6296 "expected %<]%>");
6297 expr.value = objc_build_message_expr (build_tree_list (receiver,
6298 args));
27bf414c
JM
6299 break;
6300 }
6301 /* Else fall through to report error. */
6302 default:
6303 c_parser_error (parser, "expected expression");
6304 expr.value = error_mark_node;
27bf414c
JM
6305 break;
6306 }
c2255bc4 6307 return c_parser_postfix_expression_after_primary (parser, loc, expr);
27bf414c
JM
6308}
6309
6310/* Parse a postfix expression after a parenthesized type name: the
6311 brace-enclosed initializer of a compound literal, possibly followed
6312 by some postfix operators. This is separate because it is not
6313 possible to tell until after the type name whether a cast
6314 expression has a cast or a compound literal, or whether the operand
6315 of sizeof is a parenthesized type name or starts with a compound
24b97832
ILT
6316 literal. TYPE_LOC is the location where TYPE_NAME starts--the
6317 location of the first token after the parentheses around the type
6318 name. */
27bf414c
JM
6319
6320static struct c_expr
6321c_parser_postfix_expression_after_paren_type (c_parser *parser,
24b97832
ILT
6322 struct c_type_name *type_name,
6323 location_t type_loc)
27bf414c
JM
6324{
6325 tree type;
6326 struct c_expr init;
928c19bb 6327 bool non_const;
27bf414c 6328 struct c_expr expr;
c7412148 6329 location_t start_loc;
928c19bb
JM
6330 tree type_expr = NULL_TREE;
6331 bool type_expr_const = true;
c2255bc4 6332 check_compound_literal_type (type_loc, type_name);
27bf414c 6333 start_init (NULL_TREE, NULL, 0);
928c19bb 6334 type = groktypename (type_name, &type_expr, &type_expr_const);
c7412148 6335 start_loc = c_parser_peek_token (parser)->location;
85cad37c 6336 if (type != error_mark_node && C_TYPE_VARIABLE_SIZE (type))
27bf414c 6337 {
24b97832 6338 error_at (type_loc, "compound literal has variable size");
27bf414c
JM
6339 type = error_mark_node;
6340 }
6341 init = c_parser_braced_init (parser, type, false);
6342 finish_init ();
6343 maybe_warn_string_init (type, init);
6344
36c5e70a
BE
6345 if (type != error_mark_node
6346 && !ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (type))
6347 && current_function_decl)
6348 {
6349 error ("compound literal qualified by address-space qualifier");
6350 type = error_mark_node;
6351 }
6352
fcf73884 6353 if (!flag_isoc99)
509c9d60 6354 pedwarn (start_loc, OPT_pedantic, "ISO C90 forbids compound literals");
928c19bb
JM
6355 non_const = ((init.value && TREE_CODE (init.value) == CONSTRUCTOR)
6356 ? CONSTRUCTOR_NON_CONST (init.value)
6357 : init.original_code == C_MAYBE_CONST_EXPR);
6358 non_const |= !type_expr_const;
c2255bc4 6359 expr.value = build_compound_literal (start_loc, type, init.value, non_const);
27bf414c 6360 expr.original_code = ERROR_MARK;
6866c6e8 6361 expr.original_type = NULL;
928c19bb
JM
6362 if (type_expr)
6363 {
6364 if (TREE_CODE (expr.value) == C_MAYBE_CONST_EXPR)
6365 {
6366 gcc_assert (C_MAYBE_CONST_EXPR_PRE (expr.value) == NULL_TREE);
6367 C_MAYBE_CONST_EXPR_PRE (expr.value) = type_expr;
6368 }
6369 else
6370 {
6371 gcc_assert (!non_const);
6372 expr.value = build2 (C_MAYBE_CONST_EXPR, type,
6373 type_expr, expr.value);
6374 }
6375 }
c2255bc4 6376 return c_parser_postfix_expression_after_primary (parser, start_loc, expr);
27bf414c
JM
6377}
6378
6379/* Parse a postfix expression after the initial primary or compound
c2255bc4
AH
6380 literal; that is, parse a series of postfix operators.
6381
6382 EXPR_LOC is the location of the primary expression. */
27bf414c
JM
6383
6384static struct c_expr
6385c_parser_postfix_expression_after_primary (c_parser *parser,
c2255bc4 6386 location_t expr_loc,
27bf414c
JM
6387 struct c_expr expr)
6388{
928c19bb 6389 struct c_expr orig_expr;
bbbbb16a
ILT
6390 tree ident, idx;
6391 VEC(tree,gc) *exprlist;
6392 VEC(tree,gc) *origtypes;
27bf414c
JM
6393 while (true)
6394 {
c2255bc4 6395 location_t op_loc = c_parser_peek_token (parser)->location;
27bf414c
JM
6396 switch (c_parser_peek_token (parser)->type)
6397 {
6398 case CPP_OPEN_SQUARE:
6399 /* Array reference. */
6400 c_parser_consume_token (parser);
6401 idx = c_parser_expression (parser).value;
6402 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
6403 "expected %<]%>");
c2255bc4 6404 expr.value = build_array_ref (op_loc, expr.value, idx);
27bf414c 6405 expr.original_code = ERROR_MARK;
6866c6e8 6406 expr.original_type = NULL;
27bf414c
JM
6407 break;
6408 case CPP_OPEN_PAREN:
6409 /* Function call. */
6410 c_parser_consume_token (parser);
6411 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
bbbbb16a 6412 exprlist = NULL;
27bf414c 6413 else
bbbbb16a 6414 exprlist = c_parser_expr_list (parser, true, false, &origtypes);
27bf414c
JM
6415 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
6416 "expected %<)%>");
928c19bb 6417 orig_expr = expr;
ebfbbdc5 6418 mark_exp_read (expr.value);
c2255bc4
AH
6419 /* FIXME diagnostics: Ideally we want the FUNCNAME, not the
6420 "(" after the FUNCNAME, which is what we have now. */
6421 expr.value = build_function_call_vec (op_loc, expr.value, exprlist,
bbbbb16a 6422 origtypes);
27bf414c 6423 expr.original_code = ERROR_MARK;
928c19bb
JM
6424 if (TREE_CODE (expr.value) == INTEGER_CST
6425 && TREE_CODE (orig_expr.value) == FUNCTION_DECL
6426 && DECL_BUILT_IN_CLASS (orig_expr.value) == BUILT_IN_NORMAL
6427 && DECL_FUNCTION_CODE (orig_expr.value) == BUILT_IN_CONSTANT_P)
6428 expr.original_code = C_MAYBE_CONST_EXPR;
6866c6e8 6429 expr.original_type = NULL;
bbbbb16a
ILT
6430 if (exprlist != NULL)
6431 {
c166b898
ILT
6432 release_tree_vector (exprlist);
6433 release_tree_vector (origtypes);
bbbbb16a 6434 }
27bf414c
JM
6435 break;
6436 case CPP_DOT:
6437 /* Structure element reference. */
6438 c_parser_consume_token (parser);
c2255bc4 6439 expr = default_function_array_conversion (expr_loc, expr);
27bf414c
JM
6440 if (c_parser_next_token_is (parser, CPP_NAME))
6441 ident = c_parser_peek_token (parser)->value;
6442 else
6443 {
6444 c_parser_error (parser, "expected identifier");
6445 expr.value = error_mark_node;
6446 expr.original_code = ERROR_MARK;
6866c6e8 6447 expr.original_type = NULL;
27bf414c
JM
6448 return expr;
6449 }
6450 c_parser_consume_token (parser);
c2255bc4 6451 expr.value = build_component_ref (op_loc, expr.value, ident);
27bf414c 6452 expr.original_code = ERROR_MARK;
6866c6e8
ILT
6453 if (TREE_CODE (expr.value) != COMPONENT_REF)
6454 expr.original_type = NULL;
6455 else
6456 {
6457 /* Remember the original type of a bitfield. */
6458 tree field = TREE_OPERAND (expr.value, 1);
6459 if (TREE_CODE (field) != FIELD_DECL)
6460 expr.original_type = NULL;
6461 else
6462 expr.original_type = DECL_BIT_FIELD_TYPE (field);
6463 }
27bf414c
JM
6464 break;
6465 case CPP_DEREF:
6466 /* Structure element reference. */
6467 c_parser_consume_token (parser);
c2255bc4 6468 expr = default_function_array_conversion (expr_loc, expr);
27bf414c
JM
6469 if (c_parser_next_token_is (parser, CPP_NAME))
6470 ident = c_parser_peek_token (parser)->value;
6471 else
6472 {
6473 c_parser_error (parser, "expected identifier");
6474 expr.value = error_mark_node;
6475 expr.original_code = ERROR_MARK;
6866c6e8 6476 expr.original_type = NULL;
27bf414c
JM
6477 return expr;
6478 }
6479 c_parser_consume_token (parser);
c2255bc4
AH
6480 expr.value = build_component_ref (op_loc,
6481 build_indirect_ref (op_loc,
c9f9eb5d 6482 expr.value,
dd865ef6 6483 RO_ARROW),
6a3799eb 6484 ident);
27bf414c 6485 expr.original_code = ERROR_MARK;
6866c6e8
ILT
6486 if (TREE_CODE (expr.value) != COMPONENT_REF)
6487 expr.original_type = NULL;
6488 else
6489 {
6490 /* Remember the original type of a bitfield. */
6491 tree field = TREE_OPERAND (expr.value, 1);
6492 if (TREE_CODE (field) != FIELD_DECL)
6493 expr.original_type = NULL;
6494 else
6495 expr.original_type = DECL_BIT_FIELD_TYPE (field);
6496 }
27bf414c
JM
6497 break;
6498 case CPP_PLUS_PLUS:
6499 /* Postincrement. */
6500 c_parser_consume_token (parser);
ebfbbdc5 6501 expr = default_function_array_read_conversion (expr_loc, expr);
c2255bc4 6502 expr.value = build_unary_op (op_loc,
c9f9eb5d 6503 POSTINCREMENT_EXPR, expr.value, 0);
27bf414c 6504 expr.original_code = ERROR_MARK;
6866c6e8 6505 expr.original_type = NULL;
27bf414c
JM
6506 break;
6507 case CPP_MINUS_MINUS:
6508 /* Postdecrement. */
6509 c_parser_consume_token (parser);
ebfbbdc5 6510 expr = default_function_array_read_conversion (expr_loc, expr);
c2255bc4 6511 expr.value = build_unary_op (op_loc,
c9f9eb5d 6512 POSTDECREMENT_EXPR, expr.value, 0);
27bf414c 6513 expr.original_code = ERROR_MARK;
6866c6e8 6514 expr.original_type = NULL;
27bf414c
JM
6515 break;
6516 default:
6517 return expr;
6518 }
6519 }
6520}
6521
6522/* Parse an expression (C90 6.3.17, C99 6.5.17).
6523
6524 expression:
6525 assignment-expression
6526 expression , assignment-expression
6527*/
6528
6529static struct c_expr
6530c_parser_expression (c_parser *parser)
6531{
6532 struct c_expr expr;
6533 expr = c_parser_expr_no_commas (parser, NULL);
6534 while (c_parser_next_token_is (parser, CPP_COMMA))
6535 {
6536 struct c_expr next;
056928b2 6537 tree lhsval;
c2255bc4
AH
6538 location_t loc = c_parser_peek_token (parser)->location;
6539 location_t expr_loc;
27bf414c 6540 c_parser_consume_token (parser);
c2255bc4 6541 expr_loc = c_parser_peek_token (parser)->location;
056928b2
JJ
6542 lhsval = expr.value;
6543 while (TREE_CODE (lhsval) == COMPOUND_EXPR)
6544 lhsval = TREE_OPERAND (lhsval, 1);
6545 if (DECL_P (lhsval) || handled_component_p (lhsval))
6546 mark_exp_read (lhsval);
27bf414c 6547 next = c_parser_expr_no_commas (parser, NULL);
c2255bc4
AH
6548 next = default_function_array_conversion (expr_loc, next);
6549 expr.value = build_compound_expr (loc, expr.value, next.value);
27bf414c 6550 expr.original_code = COMPOUND_EXPR;
81f40b79 6551 expr.original_type = next.original_type;
27bf414c
JM
6552 }
6553 return expr;
6554}
6555
46bdb9cf
JM
6556/* Parse an expression and convert functions or arrays to
6557 pointers. */
6558
6559static struct c_expr
6560c_parser_expression_conv (c_parser *parser)
6561{
6562 struct c_expr expr;
c2255bc4 6563 location_t loc = c_parser_peek_token (parser)->location;
46bdb9cf 6564 expr = c_parser_expression (parser);
c2255bc4 6565 expr = default_function_array_conversion (loc, expr);
46bdb9cf
JM
6566 return expr;
6567}
6568
6569/* Parse a non-empty list of expressions. If CONVERT_P, convert
928c19bb 6570 functions and arrays to pointers. If FOLD_P, fold the expressions.
27bf414c
JM
6571
6572 nonempty-expr-list:
6573 assignment-expression
6574 nonempty-expr-list , assignment-expression
6575*/
6576
bbbbb16a
ILT
6577static VEC(tree,gc) *
6578c_parser_expr_list (c_parser *parser, bool convert_p, bool fold_p,
6579 VEC(tree,gc) **p_orig_types)
27bf414c 6580{
bbbbb16a
ILT
6581 VEC(tree,gc) *ret;
6582 VEC(tree,gc) *orig_types;
27bf414c 6583 struct c_expr expr;
c2255bc4 6584 location_t loc = c_parser_peek_token (parser)->location;
bbbbb16a 6585
c166b898 6586 ret = make_tree_vector ();
bbbbb16a
ILT
6587 if (p_orig_types == NULL)
6588 orig_types = NULL;
6589 else
c166b898 6590 orig_types = make_tree_vector ();
bbbbb16a 6591
27bf414c 6592 expr = c_parser_expr_no_commas (parser, NULL);
46bdb9cf 6593 if (convert_p)
ebfbbdc5 6594 expr = default_function_array_read_conversion (loc, expr);
928c19bb
JM
6595 if (fold_p)
6596 expr.value = c_fully_fold (expr.value, false, NULL);
bbbbb16a
ILT
6597 VEC_quick_push (tree, ret, expr.value);
6598 if (orig_types != NULL)
6599 VEC_quick_push (tree, orig_types, expr.original_type);
27bf414c
JM
6600 while (c_parser_next_token_is (parser, CPP_COMMA))
6601 {
6602 c_parser_consume_token (parser);
c2255bc4 6603 loc = c_parser_peek_token (parser)->location;
27bf414c 6604 expr = c_parser_expr_no_commas (parser, NULL);
46bdb9cf 6605 if (convert_p)
ebfbbdc5 6606 expr = default_function_array_read_conversion (loc, expr);
928c19bb
JM
6607 if (fold_p)
6608 expr.value = c_fully_fold (expr.value, false, NULL);
bbbbb16a
ILT
6609 VEC_safe_push (tree, gc, ret, expr.value);
6610 if (orig_types != NULL)
6611 VEC_safe_push (tree, gc, orig_types, expr.original_type);
27bf414c 6612 }
bbbbb16a
ILT
6613 if (orig_types != NULL)
6614 *p_orig_types = orig_types;
27bf414c
JM
6615 return ret;
6616}
27bf414c
JM
6617\f
6618/* Parse Objective-C-specific constructs. */
6619
6620/* Parse an objc-class-definition.
6621
6622 objc-class-definition:
6623 @interface identifier objc-superclass[opt] objc-protocol-refs[opt]
6624 objc-class-instance-variables[opt] objc-methodprotolist @end
6625 @implementation identifier objc-superclass[opt]
6626 objc-class-instance-variables[opt]
6627 @interface identifier ( identifier ) objc-protocol-refs[opt]
6628 objc-methodprotolist @end
6629 @implementation identifier ( identifier )
6630
6631 objc-superclass:
6632 : identifier
6633
6634 "@interface identifier (" must start "@interface identifier (
6635 identifier ) ...": objc-methodprotolist in the first production may
0fa2e4df 6636 not start with a parenthesized identifier as a declarator of a data
27bf414c
JM
6637 definition with no declaration specifiers if the objc-superclass,
6638 objc-protocol-refs and objc-class-instance-variables are omitted. */
6639
6640static void
c165dca7 6641c_parser_objc_class_definition (c_parser *parser, tree attributes)
27bf414c
JM
6642{
6643 bool iface_p;
6644 tree id1;
6645 tree superclass;
6646 if (c_parser_next_token_is_keyword (parser, RID_AT_INTERFACE))
6647 iface_p = true;
6648 else if (c_parser_next_token_is_keyword (parser, RID_AT_IMPLEMENTATION))
6649 iface_p = false;
6650 else
6651 gcc_unreachable ();
c165dca7 6652
27bf414c
JM
6653 c_parser_consume_token (parser);
6654 if (c_parser_next_token_is_not (parser, CPP_NAME))
6655 {
6656 c_parser_error (parser, "expected identifier");
6657 return;
6658 }
6659 id1 = c_parser_peek_token (parser)->value;
6660 c_parser_consume_token (parser);
6661 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
6662 {
6663 tree id2;
6664 tree proto = NULL_TREE;
6665 c_parser_consume_token (parser);
6666 if (c_parser_next_token_is_not (parser, CPP_NAME))
6667 {
6668 c_parser_error (parser, "expected identifier");
6669 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6670 return;
6671 }
6672 id2 = c_parser_peek_token (parser)->value;
6673 c_parser_consume_token (parser);
6674 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
6675 if (!iface_p)
6676 {
6677 objc_start_category_implementation (id1, id2);
6678 return;
6679 }
6680 if (c_parser_next_token_is (parser, CPP_LESS))
6681 proto = c_parser_objc_protocol_refs (parser);
c165dca7 6682 objc_start_category_interface (id1, id2, proto, attributes);
27bf414c
JM
6683 c_parser_objc_methodprotolist (parser);
6684 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
6685 objc_finish_interface ();
6686 return;
6687 }
6688 if (c_parser_next_token_is (parser, CPP_COLON))
6689 {
6690 c_parser_consume_token (parser);
6691 if (c_parser_next_token_is_not (parser, CPP_NAME))
6692 {
6693 c_parser_error (parser, "expected identifier");
6694 return;
6695 }
6696 superclass = c_parser_peek_token (parser)->value;
6697 c_parser_consume_token (parser);
6698 }
6699 else
6700 superclass = NULL_TREE;
6701 if (iface_p)
6702 {
6703 tree proto = NULL_TREE;
6704 if (c_parser_next_token_is (parser, CPP_LESS))
6705 proto = c_parser_objc_protocol_refs (parser);
c165dca7 6706 objc_start_class_interface (id1, superclass, proto, attributes);
27bf414c
JM
6707 }
6708 else
6709 objc_start_class_implementation (id1, superclass);
6710 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
6711 c_parser_objc_class_instance_variables (parser);
6712 if (iface_p)
6713 {
6714 objc_continue_interface ();
6715 c_parser_objc_methodprotolist (parser);
6716 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
6717 objc_finish_interface ();
6718 }
6719 else
6720 {
6721 objc_continue_implementation ();
6722 return;
6723 }
6724}
6725
6726/* Parse objc-class-instance-variables.
6727
6728 objc-class-instance-variables:
6729 { objc-instance-variable-decl-list[opt] }
6730
6731 objc-instance-variable-decl-list:
6732 objc-visibility-spec
6733 objc-instance-variable-decl ;
6734 ;
6735 objc-instance-variable-decl-list objc-visibility-spec
6736 objc-instance-variable-decl-list objc-instance-variable-decl ;
6737 objc-instance-variable-decl-list ;
6738
6739 objc-visibility-spec:
6740 @private
6741 @protected
6742 @public
6743
6744 objc-instance-variable-decl:
6745 struct-declaration
6746*/
6747
6748static void
6749c_parser_objc_class_instance_variables (c_parser *parser)
6750{
6751 gcc_assert (c_parser_next_token_is (parser, CPP_OPEN_BRACE));
6752 c_parser_consume_token (parser);
6753 while (c_parser_next_token_is_not (parser, CPP_EOF))
6754 {
6755 tree decls;
6756 /* Parse any stray semicolon. */
6757 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
6758 {
b8698a0f 6759 pedwarn (c_parser_peek_token (parser)->location, OPT_pedantic,
509c9d60 6760 "extra semicolon in struct or union specified");
27bf414c
JM
6761 c_parser_consume_token (parser);
6762 continue;
6763 }
6764 /* Stop if at the end of the instance variables. */
6765 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
6766 {
6767 c_parser_consume_token (parser);
6768 break;
6769 }
6770 /* Parse any objc-visibility-spec. */
49b91f05 6771 if (c_parser_next_token_is_keyword (parser, RID_AT_PRIVATE))
27bf414c
JM
6772 {
6773 c_parser_consume_token (parser);
c37d8c30 6774 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
27bf414c
JM
6775 continue;
6776 }
49b91f05 6777 else if (c_parser_next_token_is_keyword (parser, RID_AT_PROTECTED))
27bf414c
JM
6778 {
6779 c_parser_consume_token (parser);
c37d8c30 6780 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
27bf414c
JM
6781 continue;
6782 }
49b91f05 6783 else if (c_parser_next_token_is_keyword (parser, RID_AT_PUBLIC))
27bf414c
JM
6784 {
6785 c_parser_consume_token (parser);
c37d8c30
IS
6786 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
6787 continue;
6788 }
6789 else if (c_parser_next_token_is_keyword (parser, RID_AT_PACKAGE))
6790 {
6791 c_parser_consume_token (parser);
6792 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
27bf414c
JM
6793 continue;
6794 }
bc4071dd
RH
6795 else if (c_parser_next_token_is (parser, CPP_PRAGMA))
6796 {
6797 c_parser_pragma (parser, pragma_external);
6798 continue;
6799 }
6800
27bf414c
JM
6801 /* Parse some comma-separated declarations. */
6802 decls = c_parser_struct_declaration (parser);
6803 {
6804 /* Comma-separated instance variables are chained together in
6805 reverse order; add them one by one. */
6806 tree ivar = nreverse (decls);
910ad8de 6807 for (; ivar; ivar = DECL_CHAIN (ivar))
27bf414c
JM
6808 objc_add_instance_variable (copy_node (ivar));
6809 }
6810 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
6811 }
6812}
6813
6814/* Parse an objc-class-declaration.
6815
6816 objc-class-declaration:
6817 @class identifier-list ;
6818*/
6819
6820static void
6821c_parser_objc_class_declaration (c_parser *parser)
6822{
6823 tree list = NULL_TREE;
49b91f05 6824 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_CLASS));
27bf414c
JM
6825 c_parser_consume_token (parser);
6826 /* Any identifiers, including those declared as type names, are OK
6827 here. */
6828 while (true)
6829 {
6830 tree id;
6831 if (c_parser_next_token_is_not (parser, CPP_NAME))
6832 {
6833 c_parser_error (parser, "expected identifier");
da57d1b9
NP
6834 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
6835 parser->error = false;
6836 return;
27bf414c
JM
6837 }
6838 id = c_parser_peek_token (parser)->value;
6839 list = chainon (list, build_tree_list (NULL_TREE, id));
6840 c_parser_consume_token (parser);
6841 if (c_parser_next_token_is (parser, CPP_COMMA))
6842 c_parser_consume_token (parser);
6843 else
6844 break;
6845 }
6846 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
6847 objc_declare_class (list);
6848}
6849
6850/* Parse an objc-alias-declaration.
6851
6852 objc-alias-declaration:
6853 @compatibility_alias identifier identifier ;
6854*/
6855
6856static void
6857c_parser_objc_alias_declaration (c_parser *parser)
6858{
6859 tree id1, id2;
6860 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_ALIAS));
6861 c_parser_consume_token (parser);
6862 if (c_parser_next_token_is_not (parser, CPP_NAME))
6863 {
6864 c_parser_error (parser, "expected identifier");
6865 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
6866 return;
6867 }
6868 id1 = c_parser_peek_token (parser)->value;
6869 c_parser_consume_token (parser);
6870 if (c_parser_next_token_is_not (parser, CPP_NAME))
6871 {
6872 c_parser_error (parser, "expected identifier");
6873 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
6874 return;
6875 }
6876 id2 = c_parser_peek_token (parser)->value;
6877 c_parser_consume_token (parser);
6878 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
6879 objc_declare_alias (id1, id2);
6880}
6881
6882/* Parse an objc-protocol-definition.
6883
6884 objc-protocol-definition:
6885 @protocol identifier objc-protocol-refs[opt] objc-methodprotolist @end
6886 @protocol identifier-list ;
6887
6888 "@protocol identifier ;" should be resolved as "@protocol
6889 identifier-list ;": objc-methodprotolist may not start with a
6890 semicolon in the first alternative if objc-protocol-refs are
6891 omitted. */
6892
6893static void
c165dca7 6894c_parser_objc_protocol_definition (c_parser *parser, tree attributes)
27bf414c
JM
6895{
6896 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_PROTOCOL));
c165dca7 6897
27bf414c
JM
6898 c_parser_consume_token (parser);
6899 if (c_parser_next_token_is_not (parser, CPP_NAME))
6900 {
6901 c_parser_error (parser, "expected identifier");
6902 return;
6903 }
6904 if (c_parser_peek_2nd_token (parser)->type == CPP_COMMA
6905 || c_parser_peek_2nd_token (parser)->type == CPP_SEMICOLON)
6906 {
6907 tree list = NULL_TREE;
6908 /* Any identifiers, including those declared as type names, are
6909 OK here. */
6910 while (true)
6911 {
6912 tree id;
6913 if (c_parser_next_token_is_not (parser, CPP_NAME))
6914 {
6915 c_parser_error (parser, "expected identifier");
6916 break;
6917 }
6918 id = c_parser_peek_token (parser)->value;
6919 list = chainon (list, build_tree_list (NULL_TREE, id));
6920 c_parser_consume_token (parser);
6921 if (c_parser_next_token_is (parser, CPP_COMMA))
6922 c_parser_consume_token (parser);
6923 else
6924 break;
6925 }
6926 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
6927 objc_declare_protocols (list);
6928 }
6929 else
6930 {
6931 tree id = c_parser_peek_token (parser)->value;
6932 tree proto = NULL_TREE;
6933 c_parser_consume_token (parser);
6934 if (c_parser_next_token_is (parser, CPP_LESS))
6935 proto = c_parser_objc_protocol_refs (parser);
0bacb8c7 6936 parser->objc_pq_context = true;
c165dca7 6937 objc_start_protocol (id, proto, attributes);
27bf414c
JM
6938 c_parser_objc_methodprotolist (parser);
6939 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
0bacb8c7 6940 parser->objc_pq_context = false;
27bf414c
JM
6941 objc_finish_interface ();
6942 }
6943}
6944
6945/* Parse an objc-method-type.
6946
6947 objc-method-type:
6948 +
6949 -
27bf414c 6950
249a82c4
NP
6951 Return true if it is a class method (+) and false if it is
6952 an instance method (-).
6953*/
6954static inline bool
27bf414c
JM
6955c_parser_objc_method_type (c_parser *parser)
6956{
6957 switch (c_parser_peek_token (parser)->type)
6958 {
6959 case CPP_PLUS:
6960 c_parser_consume_token (parser);
249a82c4 6961 return true;
27bf414c
JM
6962 case CPP_MINUS:
6963 c_parser_consume_token (parser);
249a82c4 6964 return false;
27bf414c
JM
6965 default:
6966 gcc_unreachable ();
6967 }
6968}
6969
6970/* Parse an objc-method-definition.
6971
6972 objc-method-definition:
6973 objc-method-type objc-method-decl ;[opt] compound-statement
6974*/
6975
6976static void
6977c_parser_objc_method_definition (c_parser *parser)
6978{
249a82c4 6979 bool is_class_method = c_parser_objc_method_type (parser);
f7e71da5 6980 tree decl, attributes = NULL_TREE;
0bacb8c7 6981 parser->objc_pq_context = true;
249a82c4 6982 decl = c_parser_objc_method_decl (parser, is_class_method, &attributes);
f7e71da5
IS
6983 if (decl == error_mark_node)
6984 return; /* Bail here. */
6985
27bf414c
JM
6986 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
6987 {
6988 c_parser_consume_token (parser);
b8698a0f 6989 pedwarn (c_parser_peek_token (parser)->location, OPT_pedantic,
509c9d60 6990 "extra semicolon in method definition specified");
27bf414c 6991 }
f7e71da5 6992
8f078c08
AP
6993 if (!c_parser_next_token_is (parser, CPP_OPEN_BRACE))
6994 {
6995 c_parser_error (parser, "expected %<{%>");
6996 return;
6997 }
f7e71da5 6998
0bacb8c7 6999 parser->objc_pq_context = false;
249a82c4 7000 if (objc_start_method_definition (is_class_method, decl, attributes))
45547c7f
NP
7001 {
7002 add_stmt (c_parser_compound_statement (parser));
7003 objc_finish_method_definition (current_function_decl);
7004 }
7005 else
7006 {
7007 /* This code is executed when we find a method definition
a26d8862
NP
7008 outside of an @implementation context (or invalid for other
7009 reasons). Parse the method (to keep going) but do not emit
7010 any code.
45547c7f
NP
7011 */
7012 c_parser_compound_statement (parser);
7013 }
27bf414c
JM
7014}
7015
7016/* Parse an objc-methodprotolist.
7017
7018 objc-methodprotolist:
7019 empty
7020 objc-methodprotolist objc-methodproto
7021 objc-methodprotolist declaration
7022 objc-methodprotolist ;
92902b1b
IS
7023 @optional
7024 @required
27bf414c
JM
7025
7026 The declaration is a data definition, which may be missing
7027 declaration specifiers under the same rules and diagnostics as
7028 other data definitions outside functions, and the stray semicolon
7029 is diagnosed the same way as a stray semicolon outside a
7030 function. */
7031
7032static void
7033c_parser_objc_methodprotolist (c_parser *parser)
7034{
7035 while (true)
7036 {
7037 /* The list is terminated by @end. */
7038 switch (c_parser_peek_token (parser)->type)
7039 {
7040 case CPP_SEMICOLON:
b8698a0f 7041 pedwarn (c_parser_peek_token (parser)->location, OPT_pedantic,
509c9d60 7042 "ISO C does not allow extra %<;%> outside of a function");
27bf414c
JM
7043 c_parser_consume_token (parser);
7044 break;
7045 case CPP_PLUS:
7046 case CPP_MINUS:
7047 c_parser_objc_methodproto (parser);
7048 break;
b9b58168
RH
7049 case CPP_PRAGMA:
7050 c_parser_pragma (parser, pragma_external);
7051 break;
27bf414c
JM
7052 case CPP_EOF:
7053 return;
7054 default:
7055 if (c_parser_next_token_is_keyword (parser, RID_AT_END))
7056 return;
668ea4b1 7057 else if (c_parser_next_token_is_keyword (parser, RID_AT_PROPERTY))
f614132b 7058 c_parser_objc_at_property_declaration (parser);
92902b1b
IS
7059 else if (c_parser_next_token_is_keyword (parser, RID_AT_OPTIONAL))
7060 {
7061 objc_set_method_opt (true);
7062 c_parser_consume_token (parser);
7063 }
7064 else if (c_parser_next_token_is_keyword (parser, RID_AT_REQUIRED))
7065 {
7066 objc_set_method_opt (false);
7067 c_parser_consume_token (parser);
7068 }
7069 else
7070 c_parser_declaration_or_fndef (parser, false, false, true,
f05b9d93 7071 false, true, NULL);
27bf414c
JM
7072 break;
7073 }
7074 }
7075}
7076
7077/* Parse an objc-methodproto.
7078
7079 objc-methodproto:
7080 objc-method-type objc-method-decl ;
7081*/
7082
7083static void
7084c_parser_objc_methodproto (c_parser *parser)
7085{
249a82c4 7086 bool is_class_method = c_parser_objc_method_type (parser);
f7e71da5 7087 tree decl, attributes = NULL_TREE;
249a82c4 7088
27bf414c 7089 /* Remember protocol qualifiers in prototypes. */
0bacb8c7 7090 parser->objc_pq_context = true;
249a82c4 7091 decl = c_parser_objc_method_decl (parser, is_class_method, &attributes);
f7e71da5 7092 /* Forget protocol qualifiers now. */
0bacb8c7 7093 parser->objc_pq_context = false;
f7e71da5
IS
7094
7095 /* Do not allow the presence of attributes to hide an erroneous
7096 method implementation in the interface section. */
7097 if (!c_parser_next_token_is (parser, CPP_SEMICOLON))
7098 {
7099 c_parser_error (parser, "expected %<;%>");
7100 return;
7101 }
7102
7103 if (decl != error_mark_node)
249a82c4 7104 objc_add_method_declaration (is_class_method, decl, attributes);
f7e71da5 7105
27bf414c
JM
7106 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
7107}
7108
f7e71da5
IS
7109/* If we are at a position that method attributes may be present, check that
7110 there are not any parsed already (a syntax error) and then collect any
7111 specified at the current location. Finally, if new attributes were present,
7112 check that the next token is legal ( ';' for decls and '{' for defs). */
7113
7114static bool
7115c_parser_objc_maybe_method_attributes (c_parser* parser, tree* attributes)
7116{
7117 bool bad = false;
7118 if (*attributes)
7119 {
7120 c_parser_error (parser,
7121 "method attributes must be specified at the end only");
7122 *attributes = NULL_TREE;
7123 bad = true;
7124 }
7125
7126 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
7127 *attributes = c_parser_attributes (parser);
7128
7129 /* If there were no attributes here, just report any earlier error. */
7130 if (*attributes == NULL_TREE || bad)
7131 return bad;
7132
7133 /* If the attributes are followed by a ; or {, then just report any earlier
7134 error. */
7135 if (c_parser_next_token_is (parser, CPP_SEMICOLON)
7136 || c_parser_next_token_is (parser, CPP_OPEN_BRACE))
7137 return bad;
7138
7139 /* We've got attributes, but not at the end. */
7140 c_parser_error (parser,
7141 "expected %<;%> or %<{%> after method attribute definition");
7142 return true;
7143}
7144
27bf414c
JM
7145/* Parse an objc-method-decl.
7146
7147 objc-method-decl:
7148 ( objc-type-name ) objc-selector
7149 objc-selector
7150 ( objc-type-name ) objc-keyword-selector objc-optparmlist
7151 objc-keyword-selector objc-optparmlist
f7e71da5 7152 attributes
27bf414c
JM
7153
7154 objc-keyword-selector:
7155 objc-keyword-decl
7156 objc-keyword-selector objc-keyword-decl
7157
7158 objc-keyword-decl:
7159 objc-selector : ( objc-type-name ) identifier
7160 objc-selector : identifier
7161 : ( objc-type-name ) identifier
7162 : identifier
7163
7164 objc-optparmlist:
7165 objc-optparms objc-optellipsis
7166
7167 objc-optparms:
7168 empty
7169 objc-opt-parms , parameter-declaration
7170
7171 objc-optellipsis:
7172 empty
7173 , ...
7174*/
7175
7176static tree
249a82c4 7177c_parser_objc_method_decl (c_parser *parser, bool is_class_method, tree *attributes)
27bf414c
JM
7178{
7179 tree type = NULL_TREE;
7180 tree sel;
7181 tree parms = NULL_TREE;
dbb74365 7182 bool ellipsis = false;
f7e71da5 7183 bool attr_err = false;
dbb74365 7184
f7e71da5 7185 *attributes = NULL_TREE;
27bf414c
JM
7186 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
7187 {
7188 c_parser_consume_token (parser);
7189 type = c_parser_objc_type_name (parser);
7190 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
7191 }
7192 sel = c_parser_objc_selector (parser);
7193 /* If there is no selector, or a colon follows, we have an
7194 objc-keyword-selector. If there is a selector, and a colon does
7195 not follow, that selector ends the objc-method-decl. */
7196 if (!sel || c_parser_next_token_is (parser, CPP_COLON))
7197 {
7198 tree tsel = sel;
7199 tree list = NULL_TREE;
27bf414c
JM
7200 while (true)
7201 {
7202 tree atype = NULL_TREE, id, keyworddecl;
f7e71da5 7203 tree param_attr = NULL_TREE;
27bf414c
JM
7204 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
7205 break;
7206 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
7207 {
7208 c_parser_consume_token (parser);
7209 atype = c_parser_objc_type_name (parser);
7210 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7211 "expected %<)%>");
7212 }
f7e71da5
IS
7213 /* New ObjC allows attributes on method parameters. */
7214 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
7215 param_attr = c_parser_attributes (parser);
27bf414c
JM
7216 if (c_parser_next_token_is_not (parser, CPP_NAME))
7217 {
7218 c_parser_error (parser, "expected identifier");
7219 return error_mark_node;
7220 }
7221 id = c_parser_peek_token (parser)->value;
7222 c_parser_consume_token (parser);
f7e71da5 7223 keyworddecl = objc_build_keyword_decl (tsel, atype, id, param_attr);
27bf414c
JM
7224 list = chainon (list, keyworddecl);
7225 tsel = c_parser_objc_selector (parser);
7226 if (!tsel && c_parser_next_token_is_not (parser, CPP_COLON))
7227 break;
7228 }
f7e71da5
IS
7229
7230 attr_err |= c_parser_objc_maybe_method_attributes (parser, attributes) ;
7231
27bf414c
JM
7232 /* Parse the optional parameter list. Optional Objective-C
7233 method parameters follow the C syntax, and may include '...'
7234 to denote a variable number of arguments. */
7235 parms = make_node (TREE_LIST);
27bf414c
JM
7236 while (c_parser_next_token_is (parser, CPP_COMMA))
7237 {
7238 struct c_parm *parm;
7239 c_parser_consume_token (parser);
7240 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
7241 {
7242 ellipsis = true;
7243 c_parser_consume_token (parser);
f7e71da5
IS
7244 attr_err |= c_parser_objc_maybe_method_attributes
7245 (parser, attributes) ;
27bf414c
JM
7246 break;
7247 }
7248 parm = c_parser_parameter_declaration (parser, NULL_TREE);
7249 if (parm == NULL)
7250 break;
7251 parms = chainon (parms,
7252 build_tree_list (NULL_TREE, grokparm (parm)));
7253 }
27bf414c
JM
7254 sel = list;
7255 }
f7e71da5
IS
7256 else
7257 attr_err |= c_parser_objc_maybe_method_attributes (parser, attributes) ;
7258
7259 if (sel == NULL)
7260 {
7261 c_parser_error (parser, "objective-c method declaration is expected");
7262 return error_mark_node;
7263 }
7264
7265 if (attr_err)
7266 return error_mark_node;
7267
249a82c4 7268 return objc_build_method_signature (is_class_method, type, sel, parms, ellipsis);
27bf414c
JM
7269}
7270
7271/* Parse an objc-type-name.
7272
7273 objc-type-name:
7274 objc-type-qualifiers[opt] type-name
7275 objc-type-qualifiers[opt]
7276
7277 objc-type-qualifiers:
7278 objc-type-qualifier
7279 objc-type-qualifiers objc-type-qualifier
7280
7281 objc-type-qualifier: one of
7282 in out inout bycopy byref oneway
7283*/
7284
7285static tree
7286c_parser_objc_type_name (c_parser *parser)
7287{
7288 tree quals = NULL_TREE;
d75d71e0 7289 struct c_type_name *type_name = NULL;
27bf414c
JM
7290 tree type = NULL_TREE;
7291 while (true)
7292 {
7293 c_token *token = c_parser_peek_token (parser);
7294 if (token->type == CPP_KEYWORD
7295 && (token->keyword == RID_IN
7296 || token->keyword == RID_OUT
7297 || token->keyword == RID_INOUT
7298 || token->keyword == RID_BYCOPY
7299 || token->keyword == RID_BYREF
7300 || token->keyword == RID_ONEWAY))
7301 {
71fc71d8 7302 quals = chainon (build_tree_list (NULL_TREE, token->value), quals);
27bf414c
JM
7303 c_parser_consume_token (parser);
7304 }
7305 else
7306 break;
7307 }
7308 if (c_parser_next_token_starts_typename (parser))
d75d71e0
ILT
7309 type_name = c_parser_type_name (parser);
7310 if (type_name)
928c19bb 7311 type = groktypename (type_name, NULL, NULL);
27bf414c
JM
7312 return build_tree_list (quals, type);
7313}
7314
7315/* Parse objc-protocol-refs.
7316
7317 objc-protocol-refs:
7318 < identifier-list >
7319*/
7320
7321static tree
7322c_parser_objc_protocol_refs (c_parser *parser)
7323{
7324 tree list = NULL_TREE;
7325 gcc_assert (c_parser_next_token_is (parser, CPP_LESS));
7326 c_parser_consume_token (parser);
7327 /* Any identifiers, including those declared as type names, are OK
7328 here. */
7329 while (true)
7330 {
7331 tree id;
7332 if (c_parser_next_token_is_not (parser, CPP_NAME))
7333 {
7334 c_parser_error (parser, "expected identifier");
7335 break;
7336 }
7337 id = c_parser_peek_token (parser)->value;
7338 list = chainon (list, build_tree_list (NULL_TREE, id));
7339 c_parser_consume_token (parser);
7340 if (c_parser_next_token_is (parser, CPP_COMMA))
7341 c_parser_consume_token (parser);
7342 else
7343 break;
7344 }
7345 c_parser_require (parser, CPP_GREATER, "expected %<>%>");
7346 return list;
7347}
7348
7349/* Parse an objc-try-catch-statement.
7350
7351 objc-try-catch-statement:
7352 @try compound-statement objc-catch-list[opt]
7353 @try compound-statement objc-catch-list[opt] @finally compound-statement
7354
7355 objc-catch-list:
7356 @catch ( parameter-declaration ) compound-statement
7357 objc-catch-list @catch ( parameter-declaration ) compound-statement
7358*/
7359
7360static void
7361c_parser_objc_try_catch_statement (c_parser *parser)
7362{
7363 location_t loc;
7364 tree stmt;
49b91f05 7365 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_TRY));
27bf414c
JM
7366 c_parser_consume_token (parser);
7367 loc = c_parser_peek_token (parser)->location;
7368 stmt = c_parser_compound_statement (parser);
7369 objc_begin_try_stmt (loc, stmt);
49b91f05 7370 while (c_parser_next_token_is_keyword (parser, RID_AT_CATCH))
27bf414c
JM
7371 {
7372 struct c_parm *parm;
7373 c_parser_consume_token (parser);
7374 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7375 break;
7376 parm = c_parser_parameter_declaration (parser, NULL_TREE);
7377 if (parm == NULL)
7378 {
7379 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7380 break;
7381 }
7382 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
7383 objc_begin_catch_clause (grokparm (parm));
7384 if (c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
7385 c_parser_compound_statement_nostart (parser);
7386 objc_finish_catch_clause ();
7387 }
7388 if (c_parser_next_token_is_keyword (parser, RID_AT_FINALLY))
7389 {
7390 location_t finloc;
7391 tree finstmt;
7392 c_parser_consume_token (parser);
7393 finloc = c_parser_peek_token (parser)->location;
7394 finstmt = c_parser_compound_statement (parser);
7395 objc_build_finally_clause (finloc, finstmt);
7396 }
7397 objc_finish_try_stmt ();
7398}
7399
7400/* Parse an objc-synchronized-statement.
7401
7402 objc-synchronized-statement:
7403 @synchronized ( expression ) compound-statement
7404*/
7405
7406static void
7407c_parser_objc_synchronized_statement (c_parser *parser)
7408{
7409 location_t loc;
7410 tree expr, stmt;
7411 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_SYNCHRONIZED));
7412 c_parser_consume_token (parser);
7413 loc = c_parser_peek_token (parser)->location;
7414 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7415 {
7416 expr = c_parser_expression (parser).value;
928c19bb 7417 expr = c_fully_fold (expr, false, NULL);
27bf414c
JM
7418 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
7419 }
7420 else
7421 expr = error_mark_node;
7422 stmt = c_parser_compound_statement (parser);
7423 objc_build_synchronized (loc, expr, stmt);
7424}
7425
7426/* Parse an objc-selector; return NULL_TREE without an error if the
7427 next token is not an objc-selector.
7428
7429 objc-selector:
7430 identifier
7431 one of
7432 enum struct union if else while do for switch case default
7433 break continue return goto asm sizeof typeof __alignof
7434 unsigned long const short volatile signed restrict _Complex
7435 in out inout bycopy byref oneway int char float double void _Bool
7436
7437 ??? Why this selection of keywords but not, for example, storage
7438 class specifiers? */
7439
7440static tree
7441c_parser_objc_selector (c_parser *parser)
7442{
7443 c_token *token = c_parser_peek_token (parser);
7444 tree value = token->value;
7445 if (token->type == CPP_NAME)
7446 {
7447 c_parser_consume_token (parser);
7448 return value;
7449 }
7450 if (token->type != CPP_KEYWORD)
7451 return NULL_TREE;
7452 switch (token->keyword)
7453 {
7454 case RID_ENUM:
7455 case RID_STRUCT:
7456 case RID_UNION:
7457 case RID_IF:
7458 case RID_ELSE:
7459 case RID_WHILE:
7460 case RID_DO:
7461 case RID_FOR:
7462 case RID_SWITCH:
7463 case RID_CASE:
7464 case RID_DEFAULT:
7465 case RID_BREAK:
7466 case RID_CONTINUE:
7467 case RID_RETURN:
7468 case RID_GOTO:
7469 case RID_ASM:
7470 case RID_SIZEOF:
7471 case RID_TYPEOF:
7472 case RID_ALIGNOF:
7473 case RID_UNSIGNED:
7474 case RID_LONG:
a6766312 7475 case RID_INT128:
27bf414c
JM
7476 case RID_CONST:
7477 case RID_SHORT:
7478 case RID_VOLATILE:
7479 case RID_SIGNED:
7480 case RID_RESTRICT:
7481 case RID_COMPLEX:
7482 case RID_IN:
7483 case RID_OUT:
7484 case RID_INOUT:
7485 case RID_BYCOPY:
7486 case RID_BYREF:
7487 case RID_ONEWAY:
7488 case RID_INT:
7489 case RID_CHAR:
7490 case RID_FLOAT:
7491 case RID_DOUBLE:
7492 case RID_VOID:
7493 case RID_BOOL:
7494 c_parser_consume_token (parser);
7495 return value;
7496 default:
7497 return NULL_TREE;
7498 }
7499}
7500
7501/* Parse an objc-selector-arg.
7502
7503 objc-selector-arg:
7504 objc-selector
7505 objc-keywordname-list
7506
7507 objc-keywordname-list:
7508 objc-keywordname
7509 objc-keywordname-list objc-keywordname
7510
7511 objc-keywordname:
7512 objc-selector :
7513 :
7514*/
7515
7516static tree
7517c_parser_objc_selector_arg (c_parser *parser)
7518{
7519 tree sel = c_parser_objc_selector (parser);
7520 tree list = NULL_TREE;
7521 if (sel && c_parser_next_token_is_not (parser, CPP_COLON))
7522 return sel;
7523 while (true)
7524 {
7525 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
7526 return list;
7527 list = chainon (list, build_tree_list (sel, NULL_TREE));
7528 sel = c_parser_objc_selector (parser);
7529 if (!sel && c_parser_next_token_is_not (parser, CPP_COLON))
7530 break;
7531 }
7532 return list;
7533}
7534
7535/* Parse an objc-receiver.
7536
7537 objc-receiver:
7538 expression
7539 class-name
7540 type-name
7541*/
7542
7543static tree
7544c_parser_objc_receiver (c_parser *parser)
7545{
7546 if (c_parser_peek_token (parser)->type == CPP_NAME
7547 && (c_parser_peek_token (parser)->id_kind == C_ID_TYPENAME
7548 || c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME))
7549 {
7550 tree id = c_parser_peek_token (parser)->value;
7551 c_parser_consume_token (parser);
7552 return objc_get_class_reference (id);
7553 }
928c19bb 7554 return c_fully_fold (c_parser_expression (parser).value, false, NULL);
27bf414c
JM
7555}
7556
7557/* Parse objc-message-args.
7558
7559 objc-message-args:
7560 objc-selector
7561 objc-keywordarg-list
7562
7563 objc-keywordarg-list:
7564 objc-keywordarg
7565 objc-keywordarg-list objc-keywordarg
7566
7567 objc-keywordarg:
7568 objc-selector : objc-keywordexpr
7569 : objc-keywordexpr
7570*/
7571
7572static tree
7573c_parser_objc_message_args (c_parser *parser)
7574{
7575 tree sel = c_parser_objc_selector (parser);
7576 tree list = NULL_TREE;
7577 if (sel && c_parser_next_token_is_not (parser, CPP_COLON))
7578 return sel;
7579 while (true)
7580 {
7581 tree keywordexpr;
7582 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
0a7d7dea 7583 return error_mark_node;
27bf414c
JM
7584 keywordexpr = c_parser_objc_keywordexpr (parser);
7585 list = chainon (list, build_tree_list (sel, keywordexpr));
7586 sel = c_parser_objc_selector (parser);
7587 if (!sel && c_parser_next_token_is_not (parser, CPP_COLON))
7588 break;
7589 }
7590 return list;
7591}
7592
7593/* Parse an objc-keywordexpr.
7594
7595 objc-keywordexpr:
7596 nonempty-expr-list
7597*/
7598
7599static tree
7600c_parser_objc_keywordexpr (c_parser *parser)
7601{
bbbbb16a
ILT
7602 tree ret;
7603 VEC(tree,gc) *expr_list = c_parser_expr_list (parser, true, true, NULL);
7604 if (VEC_length (tree, expr_list) == 1)
27bf414c
JM
7605 {
7606 /* Just return the expression, remove a level of
7607 indirection. */
bbbbb16a 7608 ret = VEC_index (tree, expr_list, 0);
27bf414c
JM
7609 }
7610 else
7611 {
7612 /* We have a comma expression, we will collapse later. */
c166b898 7613 ret = build_tree_list_vec (expr_list);
27bf414c 7614 }
c166b898 7615 release_tree_vector (expr_list);
bbbbb16a 7616 return ret;
27bf414c
JM
7617}
7618
c165dca7
IS
7619/* A check, needed in several places, that ObjC interface, implementation or
7620 method definitions are not prefixed by incorrect items. */
7621static bool
7622c_parser_objc_diagnose_bad_element_prefix (c_parser *parser,
7623 struct c_declspecs *specs)
7624{
9e5b2115
PB
7625 if (!specs->declspecs_seen_p || specs->non_sc_seen_p
7626 || specs->typespec_kind != ctsk_none)
c165dca7
IS
7627 {
7628 c_parser_error (parser,
7629 "no type or storage class may be specified here,");
7630 c_parser_skip_to_end_of_block_or_statement (parser);
7631 return true;
7632 }
7633 return false;
7634}
668ea4b1 7635
f614132b 7636/* Parse an Objective-C @property declaration. The syntax is:
668ea4b1 7637
f614132b
NP
7638 objc-property-declaration:
7639 '@property' objc-property-attributes[opt] struct-declaration ;
668ea4b1 7640
f614132b
NP
7641 objc-property-attributes:
7642 '(' objc-property-attribute-list ')'
7643
7644 objc-property-attribute-list:
7645 objc-property-attribute
7646 objc-property-attribute-list, objc-property-attribute
7647
7648 objc-property-attribute
7649 'getter' = identifier
7650 'setter' = identifier
7651 'readonly'
7652 'readwrite'
7653 'assign'
7654 'retain'
7655 'copy'
7656 'nonatomic'
7657
7658 For example:
7659 @property NSString *name;
7660 @property (readonly) id object;
7661 @property (retain, nonatomic, getter=getTheName) id name;
7662 @property int a, b, c;
7663
7664 PS: This function is identical to cp_parser_objc_at_propery_declaration
200290f2 7665 for C++. Keep them in sync. */
668ea4b1 7666static void
f614132b 7667c_parser_objc_at_property_declaration (c_parser *parser)
668ea4b1 7668{
200290f2
NP
7669 /* The following variables hold the attributes of the properties as
7670 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
7671 seen. When we see an attribute, we set them to 'true' (if they
7672 are boolean properties) or to the identifier (if they have an
7673 argument, ie, for getter and setter). Note that here we only
7674 parse the list of attributes, check the syntax and accumulate the
7675 attributes that we find. objc_add_property_declaration() will
7676 then process the information. */
7677 bool property_assign = false;
7678 bool property_copy = false;
7679 tree property_getter_ident = NULL_TREE;
7680 bool property_nonatomic = false;
7681 bool property_readonly = false;
7682 bool property_readwrite = false;
7683 bool property_retain = false;
7684 tree property_setter_ident = NULL_TREE;
200290f2
NP
7685
7686 /* 'properties' is the list of properties that we read. Usually a
7687 single one, but maybe more (eg, in "@property int a, b, c;" there
7688 are three). */
f614132b
NP
7689 tree properties;
7690 location_t loc;
200290f2 7691
f614132b
NP
7692 loc = c_parser_peek_token (parser)->location;
7693 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_PROPERTY));
668ea4b1 7694
f614132b 7695 c_parser_consume_token (parser); /* Eat '@property'. */
668ea4b1 7696
f614132b
NP
7697 /* Parse the optional attribute list... */
7698 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
668ea4b1 7699 {
f614132b
NP
7700 /* Eat the '(' */
7701 c_parser_consume_token (parser);
7702
7703 /* Property attribute keywords are valid now. */
7704 parser->objc_property_attr_context = true;
7705
7706 while (true)
668ea4b1 7707 {
f614132b
NP
7708 bool syntax_error = false;
7709 c_token *token = c_parser_peek_token (parser);
7710 enum rid keyword;
7711
7712 if (token->type != CPP_KEYWORD)
7713 {
7714 if (token->type == CPP_CLOSE_PAREN)
7715 c_parser_error (parser, "expected identifier");
7716 else
7717 {
7718 c_parser_consume_token (parser);
7719 c_parser_error (parser, "unknown property attribute");
7720 }
7721 break;
7722 }
7723 keyword = token->keyword;
200290f2 7724 c_parser_consume_token (parser);
f614132b
NP
7725 switch (keyword)
7726 {
200290f2 7727 case RID_ASSIGN: property_assign = true; break;
200290f2
NP
7728 case RID_COPY: property_copy = true; break;
7729 case RID_NONATOMIC: property_nonatomic = true; break;
7730 case RID_READONLY: property_readonly = true; break;
7731 case RID_READWRITE: property_readwrite = true; break;
7732 case RID_RETAIN: property_retain = true; break;
7733
f614132b
NP
7734 case RID_GETTER:
7735 case RID_SETTER:
f614132b
NP
7736 if (c_parser_next_token_is_not (parser, CPP_EQ))
7737 {
7738 c_parser_error (parser,
46a88c12 7739 "getter/setter attribute must be followed by %<=%>");
f614132b
NP
7740 syntax_error = true;
7741 break;
7742 }
7743 c_parser_consume_token (parser); /* eat the = */
7744 if (c_parser_next_token_is_not (parser, CPP_NAME))
7745 {
7746 c_parser_error (parser, "expected identifier");
7747 syntax_error = true;
7748 break;
7749 }
f614132b
NP
7750 if (keyword == RID_SETTER)
7751 {
200290f2
NP
7752 if (property_setter_ident != NULL_TREE)
7753 c_parser_error (parser, "the %<setter%> attribute may only be specified once");
7754 else
7755 property_setter_ident = c_parser_peek_token (parser)->value;
f614132b 7756 c_parser_consume_token (parser);
200290f2
NP
7757 if (c_parser_next_token_is_not (parser, CPP_COLON))
7758 c_parser_error (parser, "setter name must terminate with %<:%>");
7759 else
7760 c_parser_consume_token (parser);
f614132b 7761 }
46a88c12 7762 else
f614132b 7763 {
200290f2
NP
7764 if (property_getter_ident != NULL_TREE)
7765 c_parser_error (parser, "the %<getter%> attribute may only be specified once");
7766 else
7767 property_getter_ident = c_parser_peek_token (parser)->value;
f614132b 7768 c_parser_consume_token (parser);
f614132b 7769 }
200290f2
NP
7770 break;
7771 default:
7772 c_parser_error (parser, "unknown property attribute");
f614132b
NP
7773 syntax_error = true;
7774 break;
7775 }
7776
7777 if (syntax_error)
668ea4b1 7778 break;
f614132b
NP
7779
7780 if (c_parser_next_token_is (parser, CPP_COMMA))
668ea4b1 7781 c_parser_consume_token (parser);
f614132b 7782 else
668ea4b1
IS
7783 break;
7784 }
f614132b
NP
7785 parser->objc_property_attr_context = false;
7786 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
7787 }
7788 /* ... and the property declaration(s). */
7789 properties = c_parser_struct_declaration (parser);
668ea4b1 7790
f614132b
NP
7791 if (properties == error_mark_node)
7792 {
7793 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
7794 parser->error = false;
7795 return;
7796 }
668ea4b1 7797
f614132b
NP
7798 if (properties == NULL_TREE)
7799 c_parser_error (parser, "expected identifier");
7800 else
7801 {
7802 /* Comma-separated properties are chained together in
7803 reverse order; add them one by one. */
7804 properties = nreverse (properties);
7805
7806 for (; properties; properties = TREE_CHAIN (properties))
200290f2
NP
7807 objc_add_property_declaration (loc, copy_node (properties),
7808 property_readonly, property_readwrite,
7809 property_assign, property_retain,
7810 property_copy, property_nonatomic,
46a88c12 7811 property_getter_ident, property_setter_ident);
f614132b 7812 }
668ea4b1
IS
7813
7814 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
f614132b 7815 parser->error = false;
668ea4b1
IS
7816}
7817
da57d1b9
NP
7818/* Parse an Objective-C @synthesize declaration. The syntax is:
7819
7820 objc-synthesize-declaration:
7821 @synthesize objc-synthesize-identifier-list ;
7822
7823 objc-synthesize-identifier-list:
7824 objc-synthesize-identifier
7825 objc-synthesize-identifier-list, objc-synthesize-identifier
7826
7827 objc-synthesize-identifier
7828 identifier
7829 identifier = identifier
7830
7831 For example:
7832 @synthesize MyProperty;
7833 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
7834
7835 PS: This function is identical to cp_parser_objc_at_synthesize_declaration
7836 for C++. Keep them in sync.
7837*/
7838static void
7839c_parser_objc_at_synthesize_declaration (c_parser *parser)
7840{
7841 tree list = NULL_TREE;
7842 location_t loc;
7843 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_SYNTHESIZE));
7844 loc = c_parser_peek_token (parser)->location;
7845
7846 c_parser_consume_token (parser);
7847 while (true)
7848 {
7849 tree property, ivar;
7850 if (c_parser_next_token_is_not (parser, CPP_NAME))
7851 {
7852 c_parser_error (parser, "expected identifier");
7853 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
7854 /* Once we find the semicolon, we can resume normal parsing.
7855 We have to reset parser->error manually because
7856 c_parser_skip_until_found() won't reset it for us if the
7857 next token is precisely a semicolon. */
7858 parser->error = false;
7859 return;
7860 }
7861 property = c_parser_peek_token (parser)->value;
7862 c_parser_consume_token (parser);
7863 if (c_parser_next_token_is (parser, CPP_EQ))
7864 {
7865 c_parser_consume_token (parser);
7866 if (c_parser_next_token_is_not (parser, CPP_NAME))
7867 {
7868 c_parser_error (parser, "expected identifier");
7869 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
7870 parser->error = false;
7871 return;
7872 }
7873 ivar = c_parser_peek_token (parser)->value;
7874 c_parser_consume_token (parser);
7875 }
7876 else
7877 ivar = NULL_TREE;
7878 list = chainon (list, build_tree_list (ivar, property));
7879 if (c_parser_next_token_is (parser, CPP_COMMA))
7880 c_parser_consume_token (parser);
7881 else
7882 break;
7883 }
7884 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
7885 objc_add_synthesize_declaration (loc, list);
7886}
7887
7888/* Parse an Objective-C @dynamic declaration. The syntax is:
7889
7890 objc-dynamic-declaration:
7891 @dynamic identifier-list ;
7892
7893 For example:
7894 @dynamic MyProperty;
7895 @dynamic MyProperty, AnotherProperty;
7896
7897 PS: This function is identical to cp_parser_objc_at_dynamic_declaration
7898 for C++. Keep them in sync.
7899*/
7900static void
7901c_parser_objc_at_dynamic_declaration (c_parser *parser)
7902{
7903 tree list = NULL_TREE;
7904 location_t loc;
7905 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_DYNAMIC));
7906 loc = c_parser_peek_token (parser)->location;
7907
7908 c_parser_consume_token (parser);
7909 while (true)
7910 {
7911 tree property;
7912 if (c_parser_next_token_is_not (parser, CPP_NAME))
7913 {
7914 c_parser_error (parser, "expected identifier");
7915 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
7916 parser->error = false;
7917 return;
7918 }
7919 property = c_parser_peek_token (parser)->value;
7920 list = chainon (list, build_tree_list (NULL_TREE, property));
7921 c_parser_consume_token (parser);
7922 if (c_parser_next_token_is (parser, CPP_COMMA))
7923 c_parser_consume_token (parser);
7924 else
7925 break;
7926 }
7927 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
7928 objc_add_dynamic_declaration (loc, list);
7929}
7930
27bf414c 7931\f
953ff289
DN
7932/* Handle pragmas. Some OpenMP pragmas are associated with, and therefore
7933 should be considered, statements. ALLOW_STMT is true if we're within
7934 the context of a function and such pragmas are to be allowed. Returns
7935 true if we actually parsed such a pragma. */
27bf414c 7936
bc4071dd 7937static bool
953ff289 7938c_parser_pragma (c_parser *parser, enum pragma_context context)
bc4071dd
RH
7939{
7940 unsigned int id;
7941
7942 id = c_parser_peek_token (parser)->pragma_kind;
7943 gcc_assert (id != PRAGMA_NONE);
7944
7945 switch (id)
7946 {
953ff289
DN
7947 case PRAGMA_OMP_BARRIER:
7948 if (context != pragma_compound)
7949 {
7950 if (context == pragma_stmt)
7951 c_parser_error (parser, "%<#pragma omp barrier%> may only be "
7952 "used in compound statements");
7953 goto bad_stmt;
7954 }
7955 c_parser_omp_barrier (parser);
7956 return false;
7957
7958 case PRAGMA_OMP_FLUSH:
7959 if (context != pragma_compound)
7960 {
7961 if (context == pragma_stmt)
7962 c_parser_error (parser, "%<#pragma omp flush%> may only be "
7963 "used in compound statements");
7964 goto bad_stmt;
7965 }
7966 c_parser_omp_flush (parser);
7967 return false;
7968
a68ab351
JJ
7969 case PRAGMA_OMP_TASKWAIT:
7970 if (context != pragma_compound)
7971 {
7972 if (context == pragma_stmt)
7973 c_parser_error (parser, "%<#pragma omp taskwait%> may only be "
7974 "used in compound statements");
7975 goto bad_stmt;
7976 }
7977 c_parser_omp_taskwait (parser);
7978 return false;
7979
953ff289
DN
7980 case PRAGMA_OMP_THREADPRIVATE:
7981 c_parser_omp_threadprivate (parser);
7982 return false;
7983
7984 case PRAGMA_OMP_SECTION:
3ba09659
AH
7985 error_at (c_parser_peek_token (parser)->location,
7986 "%<#pragma omp section%> may only be used in "
7987 "%<#pragma omp sections%> construct");
953ff289
DN
7988 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
7989 return false;
7990
bc4071dd
RH
7991 case PRAGMA_GCC_PCH_PREPROCESS:
7992 c_parser_error (parser, "%<#pragma GCC pch_preprocess%> must be first");
7993 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
7994 return false;
7995
7996 default:
953ff289
DN
7997 if (id < PRAGMA_FIRST_EXTERNAL)
7998 {
7999 if (context == pragma_external)
8000 {
8001 bad_stmt:
8002 c_parser_error (parser, "expected declaration specifiers");
8003 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
8004 return false;
8005 }
8006 c_parser_omp_construct (parser);
8007 return true;
8008 }
bc4071dd
RH
8009 break;
8010 }
8011
8012 c_parser_consume_pragma (parser);
8013 c_invoke_pragma_handler (id);
27bf414c 8014
b8698a0f 8015 /* Skip to EOL, but suppress any error message. Those will have been
bc4071dd
RH
8016 generated by the handler routine through calling error, as opposed
8017 to calling c_parser_error. */
8018 parser->error = true;
8019 c_parser_skip_to_pragma_eol (parser);
8020
8021 return false;
8022}
8023
8024/* The interface the pragma parsers have to the lexer. */
8025
8026enum cpp_ttype
8027pragma_lex (tree *value)
8028{
8029 c_token *tok = c_parser_peek_token (the_parser);
8030 enum cpp_ttype ret = tok->type;
8031
8032 *value = tok->value;
8033 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
8034 ret = CPP_EOF;
8035 else
8036 {
8037 if (ret == CPP_KEYWORD)
8038 ret = CPP_NAME;
8039 c_parser_consume_token (the_parser);
8040 }
8041
8042 return ret;
8043}
8044
8045static void
8046c_parser_pragma_pch_preprocess (c_parser *parser)
8047{
8048 tree name = NULL;
8049
8050 c_parser_consume_pragma (parser);
8051 if (c_parser_next_token_is (parser, CPP_STRING))
8052 {
8053 name = c_parser_peek_token (parser)->value;
8054 c_parser_consume_token (parser);
8055 }
8056 else
8057 c_parser_error (parser, "expected string literal");
8058 c_parser_skip_to_pragma_eol (parser);
8059
8060 if (name)
8061 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
8062}
953ff289
DN
8063\f
8064/* OpenMP 2.5 parsing routines. */
8065
8066/* Returns name of the next clause.
8067 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
8068 the token is not consumed. Otherwise appropriate pragma_omp_clause is
8069 returned and the token is consumed. */
8070
8071static pragma_omp_clause
8072c_parser_omp_clause_name (c_parser *parser)
8073{
8074 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
8075
8076 if (c_parser_next_token_is_keyword (parser, RID_IF))
8077 result = PRAGMA_OMP_CLAUSE_IF;
8078 else if (c_parser_next_token_is_keyword (parser, RID_DEFAULT))
8079 result = PRAGMA_OMP_CLAUSE_DEFAULT;
8080 else if (c_parser_next_token_is (parser, CPP_NAME))
8081 {
8082 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
8083
8084 switch (p[0])
8085 {
8086 case 'c':
a68ab351
JJ
8087 if (!strcmp ("collapse", p))
8088 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
8089 else if (!strcmp ("copyin", p))
953ff289
DN
8090 result = PRAGMA_OMP_CLAUSE_COPYIN;
8091 else if (!strcmp ("copyprivate", p))
8092 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
8093 break;
8094 case 'f':
8095 if (!strcmp ("firstprivate", p))
8096 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
8097 break;
8098 case 'l':
8099 if (!strcmp ("lastprivate", p))
8100 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
8101 break;
8102 case 'n':
8103 if (!strcmp ("nowait", p))
8104 result = PRAGMA_OMP_CLAUSE_NOWAIT;
8105 else if (!strcmp ("num_threads", p))
8106 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
8107 break;
8108 case 'o':
8109 if (!strcmp ("ordered", p))
8110 result = PRAGMA_OMP_CLAUSE_ORDERED;
8111 break;
8112 case 'p':
8113 if (!strcmp ("private", p))
8114 result = PRAGMA_OMP_CLAUSE_PRIVATE;
8115 break;
8116 case 'r':
8117 if (!strcmp ("reduction", p))
8118 result = PRAGMA_OMP_CLAUSE_REDUCTION;
8119 break;
8120 case 's':
8121 if (!strcmp ("schedule", p))
8122 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
8123 else if (!strcmp ("shared", p))
8124 result = PRAGMA_OMP_CLAUSE_SHARED;
8125 break;
a68ab351
JJ
8126 case 'u':
8127 if (!strcmp ("untied", p))
8128 result = PRAGMA_OMP_CLAUSE_UNTIED;
8129 break;
953ff289
DN
8130 }
8131 }
8132
8133 if (result != PRAGMA_OMP_CLAUSE_NONE)
8134 c_parser_consume_token (parser);
8135
8136 return result;
8137}
8138
8139/* Validate that a clause of the given type does not already exist. */
8140
8141static void
d75d71e0
ILT
8142check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
8143 const char *name)
953ff289
DN
8144{
8145 tree c;
8146
8147 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
aaf46ef9 8148 if (OMP_CLAUSE_CODE (c) == code)
953ff289 8149 {
c2255bc4
AH
8150 location_t loc = OMP_CLAUSE_LOCATION (c);
8151 error_at (loc, "too many %qs clauses", name);
953ff289
DN
8152 break;
8153 }
8154}
8155
8156/* OpenMP 2.5:
8157 variable-list:
8158 identifier
8159 variable-list , identifier
8160
c2255bc4
AH
8161 If KIND is nonzero, create the appropriate node and install the
8162 decl in OMP_CLAUSE_DECL and add the node to the head of the list.
8163 If KIND is nonzero, CLAUSE_LOC is the location of the clause.
953ff289
DN
8164
8165 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
8166 return the list created. */
8167
8168static tree
c2255bc4
AH
8169c_parser_omp_variable_list (c_parser *parser,
8170 location_t clause_loc,
8171 enum omp_clause_code kind,
aaf46ef9 8172 tree list)
953ff289
DN
8173{
8174 if (c_parser_next_token_is_not (parser, CPP_NAME)
8175 || c_parser_peek_token (parser)->id_kind != C_ID_ID)
8176 c_parser_error (parser, "expected identifier");
8177
8178 while (c_parser_next_token_is (parser, CPP_NAME)
8179 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
8180 {
8181 tree t = lookup_name (c_parser_peek_token (parser)->value);
8182
8183 if (t == NULL_TREE)
c2255bc4
AH
8184 undeclared_variable (c_parser_peek_token (parser)->location,
8185 c_parser_peek_token (parser)->value);
953ff289
DN
8186 else if (t == error_mark_node)
8187 ;
8188 else if (kind != 0)
8189 {
c2255bc4 8190 tree u = build_omp_clause (clause_loc, kind);
953ff289
DN
8191 OMP_CLAUSE_DECL (u) = t;
8192 OMP_CLAUSE_CHAIN (u) = list;
8193 list = u;
8194 }
8195 else
8196 list = tree_cons (t, NULL_TREE, list);
8197
8198 c_parser_consume_token (parser);
8199
8200 if (c_parser_next_token_is_not (parser, CPP_COMMA))
8201 break;
8202
8203 c_parser_consume_token (parser);
8204 }
8205
8206 return list;
8207}
8208
8209/* Similarly, but expect leading and trailing parenthesis. This is a very
8210 common case for omp clauses. */
8211
8212static tree
d75d71e0
ILT
8213c_parser_omp_var_list_parens (c_parser *parser, enum omp_clause_code kind,
8214 tree list)
953ff289 8215{
c2255bc4
AH
8216 /* The clauses location. */
8217 location_t loc = c_parser_peek_token (parser)->location;
8218
953ff289
DN
8219 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
8220 {
c2255bc4 8221 list = c_parser_omp_variable_list (parser, loc, kind, list);
953ff289
DN
8222 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
8223 }
8224 return list;
8225}
8226
a68ab351
JJ
8227/* OpenMP 3.0:
8228 collapse ( constant-expression ) */
8229
8230static tree
8231c_parser_omp_clause_collapse (c_parser *parser, tree list)
8232{
8233 tree c, num = error_mark_node;
8234 HOST_WIDE_INT n;
8235 location_t loc;
8236
8237 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse");
8238
8239 loc = c_parser_peek_token (parser)->location;
8240 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
8241 {
8242 num = c_parser_expr_no_commas (parser, NULL).value;
8243 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
8244 }
8245 if (num == error_mark_node)
8246 return list;
8247 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
8248 || !host_integerp (num, 0)
8249 || (n = tree_low_cst (num, 0)) <= 0
8250 || (int) n != n)
8251 {
3ba09659
AH
8252 error_at (loc,
8253 "collapse argument needs positive constant integer expression");
a68ab351
JJ
8254 return list;
8255 }
c2255bc4 8256 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
a68ab351
JJ
8257 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
8258 OMP_CLAUSE_CHAIN (c) = list;
8259 return c;
8260}
8261
953ff289
DN
8262/* OpenMP 2.5:
8263 copyin ( variable-list ) */
8264
8265static tree
8266c_parser_omp_clause_copyin (c_parser *parser, tree list)
8267{
8268 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_COPYIN, list);
8269}
8270
8271/* OpenMP 2.5:
8272 copyprivate ( variable-list ) */
8273
8274static tree
8275c_parser_omp_clause_copyprivate (c_parser *parser, tree list)
8276{
8277 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_COPYPRIVATE, list);
8278}
8279
8280/* OpenMP 2.5:
8281 default ( shared | none ) */
8282
8283static tree
8284c_parser_omp_clause_default (c_parser *parser, tree list)
8285{
8286 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
c2255bc4 8287 location_t loc = c_parser_peek_token (parser)->location;
953ff289
DN
8288 tree c;
8289
8290 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
8291 return list;
8292 if (c_parser_next_token_is (parser, CPP_NAME))
8293 {
8294 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
8295
8296 switch (p[0])
8297 {
8298 case 'n':
8299 if (strcmp ("none", p) != 0)
8300 goto invalid_kind;
8301 kind = OMP_CLAUSE_DEFAULT_NONE;
8302 break;
8303
8304 case 's':
8305 if (strcmp ("shared", p) != 0)
8306 goto invalid_kind;
8307 kind = OMP_CLAUSE_DEFAULT_SHARED;
8308 break;
8309
8310 default:
8311 goto invalid_kind;
8312 }
8313
8314 c_parser_consume_token (parser);
8315 }
8316 else
8317 {
8318 invalid_kind:
8319 c_parser_error (parser, "expected %<none%> or %<shared%>");
8320 }
8321 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
8322
8323 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
8324 return list;
8325
8326 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default");
c2255bc4 8327 c = build_omp_clause (loc, OMP_CLAUSE_DEFAULT);
953ff289
DN
8328 OMP_CLAUSE_CHAIN (c) = list;
8329 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
8330
8331 return c;
8332}
8333
8334/* OpenMP 2.5:
8335 firstprivate ( variable-list ) */
8336
8337static tree
8338c_parser_omp_clause_firstprivate (c_parser *parser, tree list)
8339{
8340 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_FIRSTPRIVATE, list);
8341}
8342
8343/* OpenMP 2.5:
8344 if ( expression ) */
8345
8346static tree
8347c_parser_omp_clause_if (c_parser *parser, tree list)
8348{
c2255bc4 8349 location_t loc = c_parser_peek_token (parser)->location;
953ff289
DN
8350 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
8351 {
8352 tree t = c_parser_paren_condition (parser);
8353 tree c;
8354
8355 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if");
8356
c2255bc4 8357 c = build_omp_clause (loc, OMP_CLAUSE_IF);
953ff289
DN
8358 OMP_CLAUSE_IF_EXPR (c) = t;
8359 OMP_CLAUSE_CHAIN (c) = list;
8360 list = c;
8361 }
8362 else
8363 c_parser_error (parser, "expected %<(%>");
8364
8365 return list;
8366}
8367
8368/* OpenMP 2.5:
8369 lastprivate ( variable-list ) */
8370
8371static tree
8372c_parser_omp_clause_lastprivate (c_parser *parser, tree list)
8373{
8374 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_LASTPRIVATE, list);
8375}
8376
8377/* OpenMP 2.5:
8378 nowait */
8379
8380static tree
8381c_parser_omp_clause_nowait (c_parser *parser ATTRIBUTE_UNUSED, tree list)
8382{
8383 tree c;
c2255bc4 8384 location_t loc = c_parser_peek_token (parser)->location;
953ff289
DN
8385
8386 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait");
8387
c2255bc4 8388 c = build_omp_clause (loc, OMP_CLAUSE_NOWAIT);
953ff289
DN
8389 OMP_CLAUSE_CHAIN (c) = list;
8390 return c;
8391}
8392
8393/* OpenMP 2.5:
8394 num_threads ( expression ) */
8395
8396static tree
8397c_parser_omp_clause_num_threads (c_parser *parser, tree list)
8398{
c2255bc4 8399 location_t num_threads_loc = c_parser_peek_token (parser)->location;
953ff289
DN
8400 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
8401 {
c7412148 8402 location_t expr_loc = c_parser_peek_token (parser)->location;
953ff289 8403 tree c, t = c_parser_expression (parser).value;
928c19bb 8404 t = c_fully_fold (t, false, NULL);
953ff289
DN
8405
8406 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
8407
8408 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
8409 {
8410 c_parser_error (parser, "expected integer expression");
8411 return list;
8412 }
8413
8414 /* Attempt to statically determine when the number isn't positive. */
db3927fb 8415 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
953ff289 8416 build_int_cst (TREE_TYPE (t), 0));
c2255bc4
AH
8417 if (CAN_HAVE_LOCATION_P (c))
8418 SET_EXPR_LOCATION (c, expr_loc);
953ff289
DN
8419 if (c == boolean_true_node)
8420 {
3ba09659
AH
8421 warning_at (expr_loc, 0,
8422 "%<num_threads%> value must be positive");
953ff289
DN
8423 t = integer_one_node;
8424 }
8425
8426 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS, "num_threads");
8427
c2255bc4 8428 c = build_omp_clause (num_threads_loc, OMP_CLAUSE_NUM_THREADS);
953ff289
DN
8429 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
8430 OMP_CLAUSE_CHAIN (c) = list;
8431 list = c;
8432 }
8433
8434 return list;
8435}
8436
8437/* OpenMP 2.5:
8438 ordered */
8439
8440static tree
c2255bc4 8441c_parser_omp_clause_ordered (c_parser *parser, tree list)
953ff289
DN
8442{
8443 tree c;
8444
8445 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED, "ordered");
8446
c2255bc4
AH
8447 c = build_omp_clause (c_parser_peek_token (parser)->location,
8448 OMP_CLAUSE_ORDERED);
953ff289 8449 OMP_CLAUSE_CHAIN (c) = list;
c2255bc4 8450
953ff289
DN
8451 return c;
8452}
8453
8454/* OpenMP 2.5:
8455 private ( variable-list ) */
8456
8457static tree
8458c_parser_omp_clause_private (c_parser *parser, tree list)
8459{
8460 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_PRIVATE, list);
8461}
8462
8463/* OpenMP 2.5:
8464 reduction ( reduction-operator : variable-list )
8465
8466 reduction-operator:
8467 One of: + * - & ^ | && || */
8468
8469static tree
8470c_parser_omp_clause_reduction (c_parser *parser, tree list)
8471{
c2255bc4 8472 location_t clause_loc = c_parser_peek_token (parser)->location;
953ff289
DN
8473 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
8474 {
8475 enum tree_code code;
8476
8477 switch (c_parser_peek_token (parser)->type)
8478 {
8479 case CPP_PLUS:
8480 code = PLUS_EXPR;
8481 break;
8482 case CPP_MULT:
8483 code = MULT_EXPR;
8484 break;
8485 case CPP_MINUS:
8486 code = MINUS_EXPR;
8487 break;
8488 case CPP_AND:
8489 code = BIT_AND_EXPR;
8490 break;
8491 case CPP_XOR:
8492 code = BIT_XOR_EXPR;
8493 break;
8494 case CPP_OR:
8495 code = BIT_IOR_EXPR;
8496 break;
8497 case CPP_AND_AND:
8498 code = TRUTH_ANDIF_EXPR;
8499 break;
8500 case CPP_OR_OR:
8501 code = TRUTH_ORIF_EXPR;
8502 break;
8503 default:
8504 c_parser_error (parser,
8505 "expected %<+%>, %<*%>, %<-%>, %<&%>, "
8506 "%<^%>, %<|%>, %<&&%>, or %<||%>");
8507 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 0);
8508 return list;
8509 }
8510 c_parser_consume_token (parser);
8511 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
8512 {
8513 tree nl, c;
8514
c2255bc4
AH
8515 nl = c_parser_omp_variable_list (parser, clause_loc,
8516 OMP_CLAUSE_REDUCTION, list);
953ff289
DN
8517 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
8518 OMP_CLAUSE_REDUCTION_CODE (c) = code;
8519
8520 list = nl;
8521 }
8522 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
8523 }
8524 return list;
8525}
8526
8527/* OpenMP 2.5:
8528 schedule ( schedule-kind )
8529 schedule ( schedule-kind , expression )
8530
8531 schedule-kind:
a68ab351 8532 static | dynamic | guided | runtime | auto
953ff289
DN
8533*/
8534
8535static tree
8536c_parser_omp_clause_schedule (c_parser *parser, tree list)
8537{
8538 tree c, t;
c2255bc4 8539 location_t loc = c_parser_peek_token (parser)->location;
953ff289
DN
8540
8541 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
8542 return list;
8543
c2255bc4 8544 c = build_omp_clause (loc, OMP_CLAUSE_SCHEDULE);
953ff289
DN
8545
8546 if (c_parser_next_token_is (parser, CPP_NAME))
8547 {
8548 tree kind = c_parser_peek_token (parser)->value;
8549 const char *p = IDENTIFIER_POINTER (kind);
8550
8551 switch (p[0])
8552 {
8553 case 'd':
8554 if (strcmp ("dynamic", p) != 0)
8555 goto invalid_kind;
8556 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
8557 break;
8558
8559 case 'g':
8560 if (strcmp ("guided", p) != 0)
8561 goto invalid_kind;
8562 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
8563 break;
8564
8565 case 'r':
8566 if (strcmp ("runtime", p) != 0)
8567 goto invalid_kind;
8568 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
8569 break;
8570
8571 default:
8572 goto invalid_kind;
8573 }
8574 }
8575 else if (c_parser_next_token_is_keyword (parser, RID_STATIC))
8576 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
a68ab351
JJ
8577 else if (c_parser_next_token_is_keyword (parser, RID_AUTO))
8578 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
953ff289
DN
8579 else
8580 goto invalid_kind;
8581
8582 c_parser_consume_token (parser);
8583 if (c_parser_next_token_is (parser, CPP_COMMA))
8584 {
c7412148 8585 location_t here;
953ff289
DN
8586 c_parser_consume_token (parser);
8587
c7412148 8588 here = c_parser_peek_token (parser)->location;
953ff289 8589 t = c_parser_expr_no_commas (parser, NULL).value;
928c19bb 8590 t = c_fully_fold (t, false, NULL);
953ff289
DN
8591
8592 if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
3ba09659
AH
8593 error_at (here, "schedule %<runtime%> does not take "
8594 "a %<chunk_size%> parameter");
a68ab351 8595 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
3ba09659
AH
8596 error_at (here,
8597 "schedule %<auto%> does not take "
8598 "a %<chunk_size%> parameter");
953ff289
DN
8599 else if (TREE_CODE (TREE_TYPE (t)) == INTEGER_TYPE)
8600 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
8601 else
8602 c_parser_error (parser, "expected integer expression");
8603
8604 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
8605 }
8606 else
8607 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
8608 "expected %<,%> or %<)%>");
8609
8610 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule");
8611 OMP_CLAUSE_CHAIN (c) = list;
8612 return c;
8613
8614 invalid_kind:
8615 c_parser_error (parser, "invalid schedule kind");
8616 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 0);
8617 return list;
8618}
8619
8620/* OpenMP 2.5:
8621 shared ( variable-list ) */
8622
8623static tree
8624c_parser_omp_clause_shared (c_parser *parser, tree list)
8625{
8626 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_SHARED, list);
8627}
8628
a68ab351
JJ
8629/* OpenMP 3.0:
8630 untied */
8631
8632static tree
8633c_parser_omp_clause_untied (c_parser *parser ATTRIBUTE_UNUSED, tree list)
8634{
8635 tree c;
8636
8637 /* FIXME: Should we allow duplicates? */
8638 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied");
8639
c2255bc4
AH
8640 c = build_omp_clause (c_parser_peek_token (parser)->location,
8641 OMP_CLAUSE_UNTIED);
a68ab351 8642 OMP_CLAUSE_CHAIN (c) = list;
c2255bc4 8643
a68ab351
JJ
8644 return c;
8645}
8646
953ff289
DN
8647/* Parse all OpenMP clauses. The set clauses allowed by the directive
8648 is a bitmask in MASK. Return the list of clauses found; the result
8649 of clause default goes in *pdefault. */
8650
8651static tree
8652c_parser_omp_all_clauses (c_parser *parser, unsigned int mask,
8653 const char *where)
8654{
8655 tree clauses = NULL;
8085ca15 8656 bool first = true;
953ff289
DN
8657
8658 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
8659 {
8085ca15
JJ
8660 location_t here;
8661 pragma_omp_clause c_kind;
953ff289
DN
8662 const char *c_name;
8663 tree prev = clauses;
8664
8085ca15
JJ
8665 if (!first && c_parser_next_token_is (parser, CPP_COMMA))
8666 c_parser_consume_token (parser);
8667
8668 first = false;
8669 here = c_parser_peek_token (parser)->location;
8670 c_kind = c_parser_omp_clause_name (parser);
8671
953ff289
DN
8672 switch (c_kind)
8673 {
a68ab351
JJ
8674 case PRAGMA_OMP_CLAUSE_COLLAPSE:
8675 clauses = c_parser_omp_clause_collapse (parser, clauses);
8676 c_name = "collapse";
8677 break;
953ff289
DN
8678 case PRAGMA_OMP_CLAUSE_COPYIN:
8679 clauses = c_parser_omp_clause_copyin (parser, clauses);
8680 c_name = "copyin";
8681 break;
8682 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
8683 clauses = c_parser_omp_clause_copyprivate (parser, clauses);
8684 c_name = "copyprivate";
8685 break;
8686 case PRAGMA_OMP_CLAUSE_DEFAULT:
8687 clauses = c_parser_omp_clause_default (parser, clauses);
8688 c_name = "default";
8689 break;
8690 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
8691 clauses = c_parser_omp_clause_firstprivate (parser, clauses);
8692 c_name = "firstprivate";
8693 break;
8694 case PRAGMA_OMP_CLAUSE_IF:
8695 clauses = c_parser_omp_clause_if (parser, clauses);
8696 c_name = "if";
8697 break;
8698 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
8699 clauses = c_parser_omp_clause_lastprivate (parser, clauses);
8700 c_name = "lastprivate";
8701 break;
8702 case PRAGMA_OMP_CLAUSE_NOWAIT:
8703 clauses = c_parser_omp_clause_nowait (parser, clauses);
8704 c_name = "nowait";
8705 break;
8706 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
8707 clauses = c_parser_omp_clause_num_threads (parser, clauses);
8708 c_name = "num_threads";
8709 break;
8710 case PRAGMA_OMP_CLAUSE_ORDERED:
8711 clauses = c_parser_omp_clause_ordered (parser, clauses);
8712 c_name = "ordered";
8713 break;
8714 case PRAGMA_OMP_CLAUSE_PRIVATE:
8715 clauses = c_parser_omp_clause_private (parser, clauses);
8716 c_name = "private";
8717 break;
8718 case PRAGMA_OMP_CLAUSE_REDUCTION:
8719 clauses = c_parser_omp_clause_reduction (parser, clauses);
8720 c_name = "reduction";
8721 break;
8722 case PRAGMA_OMP_CLAUSE_SCHEDULE:
8723 clauses = c_parser_omp_clause_schedule (parser, clauses);
8724 c_name = "schedule";
8725 break;
8726 case PRAGMA_OMP_CLAUSE_SHARED:
8727 clauses = c_parser_omp_clause_shared (parser, clauses);
8728 c_name = "shared";
8729 break;
a68ab351
JJ
8730 case PRAGMA_OMP_CLAUSE_UNTIED:
8731 clauses = c_parser_omp_clause_untied (parser, clauses);
8732 c_name = "untied";
8733 break;
953ff289
DN
8734 default:
8735 c_parser_error (parser, "expected %<#pragma omp%> clause");
8736 goto saw_error;
8737 }
8738
8739 if (((mask >> c_kind) & 1) == 0 && !parser->error)
8740 {
8741 /* Remove the invalid clause(s) from the list to avoid
8742 confusing the rest of the compiler. */
8743 clauses = prev;
3ba09659 8744 error_at (here, "%qs is not valid for %qs", c_name, where);
953ff289
DN
8745 }
8746 }
8747
8748 saw_error:
8749 c_parser_skip_to_pragma_eol (parser);
8750
8751 return c_finish_omp_clauses (clauses);
8752}
8753
8754/* OpenMP 2.5:
8755 structured-block:
8756 statement
8757
8758 In practice, we're also interested in adding the statement to an
8759 outer node. So it is convenient if we work around the fact that
8760 c_parser_statement calls add_stmt. */
8761
8762static tree
8763c_parser_omp_structured_block (c_parser *parser)
8764{
8765 tree stmt = push_stmt_list ();
8766 c_parser_statement (parser);
8767 return pop_stmt_list (stmt);
8768}
8769
8770/* OpenMP 2.5:
8771 # pragma omp atomic new-line
8772 expression-stmt
8773
8774 expression-stmt:
8775 x binop= expr | x++ | ++x | x-- | --x
8776 binop:
8777 +, *, -, /, &, ^, |, <<, >>
8778
b8698a0f 8779 where x is an lvalue expression with scalar type.
c2255bc4
AH
8780
8781 LOC is the location of the #pragma token. */
953ff289
DN
8782
8783static void
c2255bc4 8784c_parser_omp_atomic (location_t loc, c_parser *parser)
953ff289
DN
8785{
8786 tree lhs, rhs;
fe89d797 8787 tree stmt;
953ff289 8788 enum tree_code code;
79addd1f 8789 struct c_expr rhs_expr;
953ff289
DN
8790
8791 c_parser_skip_to_pragma_eol (parser);
8792
8793 lhs = c_parser_unary_expression (parser).value;
928c19bb 8794 lhs = c_fully_fold (lhs, false, NULL);
953ff289
DN
8795 switch (TREE_CODE (lhs))
8796 {
8797 case ERROR_MARK:
8798 saw_error:
8799 c_parser_skip_to_end_of_block_or_statement (parser);
8800 return;
8801
8802 case PREINCREMENT_EXPR:
8803 case POSTINCREMENT_EXPR:
8804 lhs = TREE_OPERAND (lhs, 0);
8805 code = PLUS_EXPR;
8806 rhs = integer_one_node;
8807 break;
8808
8809 case PREDECREMENT_EXPR:
8810 case POSTDECREMENT_EXPR:
8811 lhs = TREE_OPERAND (lhs, 0);
8812 code = MINUS_EXPR;
8813 rhs = integer_one_node;
8814 break;
8815
f2b11865
JJ
8816 case COMPOUND_EXPR:
8817 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
8818 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
8819 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
8820 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
8821 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
8822 (TREE_OPERAND (lhs, 1), 0), 0)))
8823 == BOOLEAN_TYPE)
8824 /* Undo effects of boolean_increment for post {in,de}crement. */
8825 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
8826 /* FALLTHRU */
8827 case MODIFY_EXPR:
8828 if (TREE_CODE (lhs) == MODIFY_EXPR
8829 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
8830 {
8831 /* Undo effects of boolean_increment. */
8832 if (integer_onep (TREE_OPERAND (lhs, 1)))
8833 {
8834 /* This is pre or post increment. */
8835 rhs = TREE_OPERAND (lhs, 1);
8836 lhs = TREE_OPERAND (lhs, 0);
8837 code = NOP_EXPR;
8838 break;
8839 }
8840 if (TREE_CODE (TREE_OPERAND (lhs, 1)) == TRUTH_NOT_EXPR
8841 && TREE_OPERAND (lhs, 0)
8842 == TREE_OPERAND (TREE_OPERAND (lhs, 1), 0))
8843 {
8844 /* This is pre or post decrement. */
8845 rhs = TREE_OPERAND (lhs, 1);
8846 lhs = TREE_OPERAND (lhs, 0);
8847 code = NOP_EXPR;
8848 break;
8849 }
8850 }
8851 /* FALLTHRU */
953ff289
DN
8852 default:
8853 switch (c_parser_peek_token (parser)->type)
8854 {
8855 case CPP_MULT_EQ:
8856 code = MULT_EXPR;
8857 break;
8858 case CPP_DIV_EQ:
8859 code = TRUNC_DIV_EXPR;
8860 break;
8861 case CPP_PLUS_EQ:
8862 code = PLUS_EXPR;
8863 break;
8864 case CPP_MINUS_EQ:
8865 code = MINUS_EXPR;
8866 break;
8867 case CPP_LSHIFT_EQ:
8868 code = LSHIFT_EXPR;
8869 break;
8870 case CPP_RSHIFT_EQ:
8871 code = RSHIFT_EXPR;
8872 break;
8873 case CPP_AND_EQ:
8874 code = BIT_AND_EXPR;
8875 break;
8876 case CPP_OR_EQ:
8877 code = BIT_IOR_EXPR;
8878 break;
8879 case CPP_XOR_EQ:
8880 code = BIT_XOR_EXPR;
8881 break;
8882 default:
8883 c_parser_error (parser,
8884 "invalid operator for %<#pragma omp atomic%>");
8885 goto saw_error;
8886 }
8887
8888 c_parser_consume_token (parser);
c2255bc4
AH
8889 {
8890 location_t rhs_loc = c_parser_peek_token (parser)->location;
8891 rhs_expr = c_parser_expression (parser);
ebfbbdc5 8892 rhs_expr = default_function_array_read_conversion (rhs_loc, rhs_expr);
c2255bc4 8893 }
79addd1f 8894 rhs = rhs_expr.value;
928c19bb 8895 rhs = c_fully_fold (rhs, false, NULL);
953ff289
DN
8896 break;
8897 }
c2255bc4 8898 stmt = c_finish_omp_atomic (loc, code, lhs, rhs);
fe89d797
MM
8899 if (stmt != error_mark_node)
8900 add_stmt (stmt);
953ff289
DN
8901 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
8902}
8903
8904
8905/* OpenMP 2.5:
8906 # pragma omp barrier new-line
8907*/
8908
8909static void
8910c_parser_omp_barrier (c_parser *parser)
8911{
c2255bc4 8912 location_t loc = c_parser_peek_token (parser)->location;
953ff289
DN
8913 c_parser_consume_pragma (parser);
8914 c_parser_skip_to_pragma_eol (parser);
8915
c2255bc4 8916 c_finish_omp_barrier (loc);
953ff289
DN
8917}
8918
8919/* OpenMP 2.5:
8920 # pragma omp critical [(name)] new-line
8921 structured-block
c2255bc4
AH
8922
8923 LOC is the location of the #pragma itself. */
953ff289
DN
8924
8925static tree
c2255bc4 8926c_parser_omp_critical (location_t loc, c_parser *parser)
953ff289
DN
8927{
8928 tree stmt, name = NULL;
8929
8930 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
8931 {
8932 c_parser_consume_token (parser);
8933 if (c_parser_next_token_is (parser, CPP_NAME))
8934 {
8935 name = c_parser_peek_token (parser)->value;
8936 c_parser_consume_token (parser);
8937 c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
8938 }
8939 else
8940 c_parser_error (parser, "expected identifier");
8941 }
8942 else if (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
8943 c_parser_error (parser, "expected %<(%> or end of line");
8944 c_parser_skip_to_pragma_eol (parser);
8945
8946 stmt = c_parser_omp_structured_block (parser);
c2255bc4 8947 return c_finish_omp_critical (loc, stmt, name);
953ff289
DN
8948}
8949
8950/* OpenMP 2.5:
8951 # pragma omp flush flush-vars[opt] new-line
8952
8953 flush-vars:
8954 ( variable-list ) */
8955
8956static void
8957c_parser_omp_flush (c_parser *parser)
8958{
c2255bc4 8959 location_t loc = c_parser_peek_token (parser)->location;
953ff289
DN
8960 c_parser_consume_pragma (parser);
8961 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
d75d71e0 8962 c_parser_omp_var_list_parens (parser, OMP_CLAUSE_ERROR, NULL);
953ff289
DN
8963 else if (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
8964 c_parser_error (parser, "expected %<(%> or end of line");
8965 c_parser_skip_to_pragma_eol (parser);
8966
c2255bc4 8967 c_finish_omp_flush (loc);
953ff289
DN
8968}
8969
fa10beec 8970/* Parse the restricted form of the for statement allowed by OpenMP.
953ff289 8971 The real trick here is to determine the loop control variable early
c2255bc4
AH
8972 so that we can push a new decl if necessary to make it private.
8973 LOC is the location of the OMP in "#pragma omp". */
953ff289
DN
8974
8975static tree
c2255bc4
AH
8976c_parser_omp_for_loop (location_t loc,
8977 c_parser *parser, tree clauses, tree *par_clauses)
953ff289 8978{
a68ab351 8979 tree decl, cond, incr, save_break, save_cont, body, init, stmt, cl;
1d468b06 8980 tree declv, condv, incrv, initv, ret = NULL;
a68ab351
JJ
8981 bool fail = false, open_brace_parsed = false;
8982 int i, collapse = 1, nbraces = 0;
c2255bc4 8983 location_t for_loc;
1d468b06 8984 VEC(tree,gc) *for_block = make_tree_vector ();
a68ab351
JJ
8985
8986 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
8987 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
8988 collapse = tree_low_cst (OMP_CLAUSE_COLLAPSE_EXPR (cl), 0);
8989
8990 gcc_assert (collapse >= 1);
8991
8992 declv = make_tree_vec (collapse);
8993 initv = make_tree_vec (collapse);
8994 condv = make_tree_vec (collapse);
8995 incrv = make_tree_vec (collapse);
953ff289
DN
8996
8997 if (!c_parser_next_token_is_keyword (parser, RID_FOR))
8998 {
8999 c_parser_error (parser, "for statement expected");
9000 return NULL;
9001 }
c2255bc4 9002 for_loc = c_parser_peek_token (parser)->location;
953ff289
DN
9003 c_parser_consume_token (parser);
9004
a68ab351 9005 for (i = 0; i < collapse; i++)
953ff289 9006 {
a68ab351 9007 int bracecount = 0;
953ff289 9008
a68ab351
JJ
9009 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
9010 goto pop_scopes;
953ff289 9011
a68ab351 9012 /* Parse the initialization declaration or expression. */
32912286 9013 if (c_parser_next_token_starts_declaration (parser))
a68ab351
JJ
9014 {
9015 if (i > 0)
1d468b06 9016 VEC_safe_push (tree, gc, for_block, c_begin_compound_stmt (true));
f05b9d93
NP
9017 c_parser_declaration_or_fndef (parser, true, true, true, true, true, NULL);
9018 decl = check_for_loop_decls (for_loc, flag_isoc99);
a68ab351
JJ
9019 if (decl == NULL)
9020 goto error_init;
9021 if (DECL_INITIAL (decl) == error_mark_node)
9022 decl = error_mark_node;
9023 init = decl;
9024 }
9025 else if (c_parser_next_token_is (parser, CPP_NAME)
9026 && c_parser_peek_2nd_token (parser)->type == CPP_EQ)
9027 {
32e8bb8e 9028 struct c_expr decl_exp;
a68ab351 9029 struct c_expr init_exp;
c9f9eb5d 9030 location_t init_loc;
a68ab351 9031
32e8bb8e
ILT
9032 decl_exp = c_parser_postfix_expression (parser);
9033 decl = decl_exp.value;
a68ab351
JJ
9034
9035 c_parser_require (parser, CPP_EQ, "expected %<=%>");
9036
c2255bc4 9037 init_loc = c_parser_peek_token (parser)->location;
a68ab351 9038 init_exp = c_parser_expr_no_commas (parser, NULL);
ebfbbdc5
JJ
9039 init_exp = default_function_array_read_conversion (init_loc,
9040 init_exp);
32e8bb8e 9041 init = build_modify_expr (init_loc, decl, decl_exp.original_type,
c2255bc4 9042 NOP_EXPR, init_loc, init_exp.value,
bbbbb16a 9043 init_exp.original_type);
c2255bc4 9044 init = c_process_expr_stmt (init_loc, init);
953ff289 9045
a68ab351
JJ
9046 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
9047 }
9048 else
9049 {
9050 error_init:
9051 c_parser_error (parser,
9052 "expected iteration declaration or initialization");
9053 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
9054 "expected %<)%>");
9055 fail = true;
9056 goto parse_next;
9057 }
9058
9059 /* Parse the loop condition. */
9060 cond = NULL_TREE;
9061 if (c_parser_next_token_is_not (parser, CPP_SEMICOLON))
9062 {
3ba09659 9063 location_t cond_loc = c_parser_peek_token (parser)->location;
c5cdb03f 9064 struct c_expr cond_expr = c_parser_binary_expression (parser, NULL);
3ba09659 9065
c5cdb03f 9066 cond = cond_expr.value;
3ba09659 9067 cond = c_objc_common_truthvalue_conversion (cond_loc, cond);
928c19bb 9068 cond = c_fully_fold (cond, false, NULL);
c5cdb03f
JJ
9069 switch (cond_expr.original_code)
9070 {
9071 case GT_EXPR:
9072 case GE_EXPR:
9073 case LT_EXPR:
9074 case LE_EXPR:
9075 break;
9076 default:
9077 /* Can't be cond = error_mark_node, because we want to preserve
9078 the location until c_finish_omp_for. */
9079 cond = build1 (NOP_EXPR, boolean_type_node, error_mark_node);
9080 break;
9081 }
c9f9eb5d 9082 protected_set_expr_location (cond, cond_loc);
a68ab351 9083 }
953ff289 9084 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
953ff289 9085
a68ab351
JJ
9086 /* Parse the increment expression. */
9087 incr = NULL_TREE;
9088 if (c_parser_next_token_is_not (parser, CPP_CLOSE_PAREN))
c9f9eb5d
AH
9089 {
9090 location_t incr_loc = c_parser_peek_token (parser)->location;
9091
c2255bc4
AH
9092 incr = c_process_expr_stmt (incr_loc,
9093 c_parser_expression (parser).value);
c9f9eb5d 9094 }
a68ab351 9095 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
953ff289 9096
a68ab351
JJ
9097 if (decl == NULL || decl == error_mark_node || init == error_mark_node)
9098 fail = true;
9099 else
9100 {
9101 TREE_VEC_ELT (declv, i) = decl;
9102 TREE_VEC_ELT (initv, i) = init;
9103 TREE_VEC_ELT (condv, i) = cond;
9104 TREE_VEC_ELT (incrv, i) = incr;
9105 }
9106
9107 parse_next:
9108 if (i == collapse - 1)
9109 break;
9110
9111 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
9112 in between the collapsed for loops to be still considered perfectly
9113 nested. Hopefully the final version clarifies this.
9114 For now handle (multiple) {'s and empty statements. */
9115 do
9116 {
9117 if (c_parser_next_token_is_keyword (parser, RID_FOR))
9118 {
9119 c_parser_consume_token (parser);
9120 break;
9121 }
9122 else if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
9123 {
9124 c_parser_consume_token (parser);
9125 bracecount++;
9126 }
9127 else if (bracecount
9128 && c_parser_next_token_is (parser, CPP_SEMICOLON))
9129 c_parser_consume_token (parser);
9130 else
9131 {
9132 c_parser_error (parser, "not enough perfectly nested loops");
9133 if (bracecount)
9134 {
9135 open_brace_parsed = true;
9136 bracecount--;
9137 }
9138 fail = true;
9139 collapse = 0;
9140 break;
9141 }
9142 }
9143 while (1);
9144
9145 nbraces += bracecount;
9146 }
953ff289 9147
953ff289
DN
9148 save_break = c_break_label;
9149 c_break_label = size_one_node;
9150 save_cont = c_cont_label;
9151 c_cont_label = NULL_TREE;
9152 body = push_stmt_list ();
9153
a68ab351
JJ
9154 if (open_brace_parsed)
9155 {
c2255bc4 9156 location_t here = c_parser_peek_token (parser)->location;
a68ab351
JJ
9157 stmt = c_begin_compound_stmt (true);
9158 c_parser_compound_statement_nostart (parser);
c2255bc4 9159 add_stmt (c_end_compound_stmt (here, stmt, true));
a68ab351
JJ
9160 }
9161 else
9162 add_stmt (c_parser_c99_block_statement (parser));
953ff289 9163 if (c_cont_label)
c2255bc4
AH
9164 {
9165 tree t = build1 (LABEL_EXPR, void_type_node, c_cont_label);
9166 SET_EXPR_LOCATION (t, loc);
9167 add_stmt (t);
9168 }
953ff289
DN
9169
9170 body = pop_stmt_list (body);
9171 c_break_label = save_break;
9172 c_cont_label = save_cont;
9173
a68ab351
JJ
9174 while (nbraces)
9175 {
9176 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
9177 {
9178 c_parser_consume_token (parser);
9179 nbraces--;
9180 }
9181 else if (c_parser_next_token_is (parser, CPP_SEMICOLON))
9182 c_parser_consume_token (parser);
9183 else
9184 {
9185 c_parser_error (parser, "collapsed loops not perfectly nested");
9186 while (nbraces)
9187 {
c2255bc4 9188 location_t here = c_parser_peek_token (parser)->location;
a68ab351
JJ
9189 stmt = c_begin_compound_stmt (true);
9190 add_stmt (body);
9191 c_parser_compound_statement_nostart (parser);
c2255bc4 9192 body = c_end_compound_stmt (here, stmt, true);
a68ab351
JJ
9193 nbraces--;
9194 }
9195 goto pop_scopes;
9196 }
9197 }
9198
61c3a446 9199 /* Only bother calling c_finish_omp_for if we haven't already generated
953ff289 9200 an error from the initialization parsing. */
a68ab351
JJ
9201 if (!fail)
9202 {
9203 stmt = c_finish_omp_for (loc, declv, initv, condv, incrv, body, NULL);
9204 if (stmt)
9205 {
9206 if (par_clauses != NULL)
9207 {
9208 tree *c;
9209 for (c = par_clauses; *c ; )
9210 if (OMP_CLAUSE_CODE (*c) != OMP_CLAUSE_FIRSTPRIVATE
9211 && OMP_CLAUSE_CODE (*c) != OMP_CLAUSE_LASTPRIVATE)
9212 c = &OMP_CLAUSE_CHAIN (*c);
9213 else
9214 {
9215 for (i = 0; i < collapse; i++)
9216 if (TREE_VEC_ELT (declv, i) == OMP_CLAUSE_DECL (*c))
9217 break;
9218 if (i == collapse)
9219 c = &OMP_CLAUSE_CHAIN (*c);
9220 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE)
9221 {
3ba09659
AH
9222 error_at (loc,
9223 "iteration variable %qD should not be firstprivate",
9224 OMP_CLAUSE_DECL (*c));
a68ab351
JJ
9225 *c = OMP_CLAUSE_CHAIN (*c);
9226 }
9227 else
9228 {
9229 /* Copy lastprivate (decl) clause to OMP_FOR_CLAUSES,
9230 change it to shared (decl) in
9231 OMP_PARALLEL_CLAUSES. */
c2255bc4
AH
9232 tree l = build_omp_clause (OMP_CLAUSE_LOCATION (*c),
9233 OMP_CLAUSE_LASTPRIVATE);
a68ab351
JJ
9234 OMP_CLAUSE_DECL (l) = OMP_CLAUSE_DECL (*c);
9235 OMP_CLAUSE_CHAIN (l) = clauses;
9236 clauses = l;
9237 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
9238 }
9239 }
9240 }
9241 OMP_FOR_CLAUSES (stmt) = clauses;
9242 }
9243 ret = stmt;
9244 }
9245pop_scopes:
1d468b06 9246 while (!VEC_empty (tree, for_block))
a68ab351 9247 {
c2255bc4
AH
9248 /* FIXME diagnostics: LOC below should be the actual location of
9249 this particular for block. We need to build a list of
9250 locations to go along with FOR_BLOCK. */
1d468b06 9251 stmt = c_end_compound_stmt (loc, VEC_pop (tree, for_block), true);
a68ab351 9252 add_stmt (stmt);
a68ab351 9253 }
1d468b06 9254 release_tree_vector (for_block);
a68ab351 9255 return ret;
953ff289
DN
9256}
9257
9258/* OpenMP 2.5:
9259 #pragma omp for for-clause[optseq] new-line
9260 for-loop
c2255bc4
AH
9261
9262 LOC is the location of the #pragma token.
953ff289
DN
9263*/
9264
9265#define OMP_FOR_CLAUSE_MASK \
9266 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
9267 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
9268 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
9269 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
9270 | (1u << PRAGMA_OMP_CLAUSE_ORDERED) \
9271 | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE) \
a68ab351 9272 | (1u << PRAGMA_OMP_CLAUSE_COLLAPSE) \
953ff289
DN
9273 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
9274
9275static tree
c2255bc4 9276c_parser_omp_for (location_t loc, c_parser *parser)
953ff289
DN
9277{
9278 tree block, clauses, ret;
9279
9280 clauses = c_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
9281 "#pragma omp for");
9282
9283 block = c_begin_compound_stmt (true);
c2255bc4
AH
9284 ret = c_parser_omp_for_loop (loc, parser, clauses, NULL);
9285 block = c_end_compound_stmt (loc, block, true);
953ff289
DN
9286 add_stmt (block);
9287
9288 return ret;
9289}
9290
9291/* OpenMP 2.5:
9292 # pragma omp master new-line
9293 structured-block
c2255bc4
AH
9294
9295 LOC is the location of the #pragma token.
953ff289
DN
9296*/
9297
9298static tree
c2255bc4 9299c_parser_omp_master (location_t loc, c_parser *parser)
953ff289
DN
9300{
9301 c_parser_skip_to_pragma_eol (parser);
c2255bc4 9302 return c_finish_omp_master (loc, c_parser_omp_structured_block (parser));
953ff289
DN
9303}
9304
9305/* OpenMP 2.5:
9306 # pragma omp ordered new-line
9307 structured-block
c2255bc4
AH
9308
9309 LOC is the location of the #pragma itself.
953ff289
DN
9310*/
9311
9312static tree
c2255bc4 9313c_parser_omp_ordered (location_t loc, c_parser *parser)
953ff289
DN
9314{
9315 c_parser_skip_to_pragma_eol (parser);
c2255bc4 9316 return c_finish_omp_ordered (loc, c_parser_omp_structured_block (parser));
953ff289
DN
9317}
9318
9319/* OpenMP 2.5:
9320
9321 section-scope:
9322 { section-sequence }
9323
9324 section-sequence:
9325 section-directive[opt] structured-block
b8698a0f 9326 section-sequence section-directive structured-block
c2255bc4
AH
9327
9328 SECTIONS_LOC is the location of the #pragma omp sections. */
953ff289
DN
9329
9330static tree
c2255bc4 9331c_parser_omp_sections_scope (location_t sections_loc, c_parser *parser)
953ff289
DN
9332{
9333 tree stmt, substmt;
9334 bool error_suppress = false;
9335 location_t loc;
9336
c2255bc4 9337 loc = c_parser_peek_token (parser)->location;
953ff289
DN
9338 if (!c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
9339 {
9340 /* Avoid skipping until the end of the block. */
9341 parser->error = false;
9342 return NULL_TREE;
9343 }
9344
9345 stmt = push_stmt_list ();
9346
953ff289
DN
9347 if (c_parser_peek_token (parser)->pragma_kind != PRAGMA_OMP_SECTION)
9348 {
9349 substmt = push_stmt_list ();
9350
9351 while (1)
9352 {
9353 c_parser_statement (parser);
9354
9355 if (c_parser_peek_token (parser)->pragma_kind == PRAGMA_OMP_SECTION)
9356 break;
9357 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
9358 break;
9359 if (c_parser_next_token_is (parser, CPP_EOF))
9360 break;
9361 }
9362
9363 substmt = pop_stmt_list (substmt);
9364 substmt = build1 (OMP_SECTION, void_type_node, substmt);
9365 SET_EXPR_LOCATION (substmt, loc);
9366 add_stmt (substmt);
9367 }
9368
9369 while (1)
9370 {
9371 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
9372 break;
9373 if (c_parser_next_token_is (parser, CPP_EOF))
9374 break;
9375
9376 loc = c_parser_peek_token (parser)->location;
9377 if (c_parser_peek_token (parser)->pragma_kind == PRAGMA_OMP_SECTION)
9378 {
9379 c_parser_consume_pragma (parser);
9380 c_parser_skip_to_pragma_eol (parser);
9381 error_suppress = false;
9382 }
9383 else if (!error_suppress)
9384 {
3ba09659 9385 error_at (loc, "expected %<#pragma omp section%> or %<}%>");
953ff289
DN
9386 error_suppress = true;
9387 }
9388
9389 substmt = c_parser_omp_structured_block (parser);
9390 substmt = build1 (OMP_SECTION, void_type_node, substmt);
9391 SET_EXPR_LOCATION (substmt, loc);
9392 add_stmt (substmt);
9393 }
9394 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE,
9395 "expected %<#pragma omp section%> or %<}%>");
9396
9397 substmt = pop_stmt_list (stmt);
9398
9399 stmt = make_node (OMP_SECTIONS);
c2255bc4 9400 SET_EXPR_LOCATION (stmt, sections_loc);
953ff289
DN
9401 TREE_TYPE (stmt) = void_type_node;
9402 OMP_SECTIONS_BODY (stmt) = substmt;
9403
9404 return add_stmt (stmt);
9405}
9406
9407/* OpenMP 2.5:
9408 # pragma omp sections sections-clause[optseq] newline
9409 sections-scope
c2255bc4
AH
9410
9411 LOC is the location of the #pragma token.
953ff289
DN
9412*/
9413
9414#define OMP_SECTIONS_CLAUSE_MASK \
9415 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
9416 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
9417 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
9418 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
9419 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
9420
9421static tree
c2255bc4 9422c_parser_omp_sections (location_t loc, c_parser *parser)
953ff289
DN
9423{
9424 tree block, clauses, ret;
9425
9426 clauses = c_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
9427 "#pragma omp sections");
9428
9429 block = c_begin_compound_stmt (true);
c2255bc4 9430 ret = c_parser_omp_sections_scope (loc, parser);
953ff289
DN
9431 if (ret)
9432 OMP_SECTIONS_CLAUSES (ret) = clauses;
c2255bc4 9433 block = c_end_compound_stmt (loc, block, true);
953ff289
DN
9434 add_stmt (block);
9435
9436 return ret;
9437}
9438
9439/* OpenMP 2.5:
9440 # pragma parallel parallel-clause new-line
9441 # pragma parallel for parallel-for-clause new-line
9442 # pragma parallel sections parallel-sections-clause new-line
c2255bc4
AH
9443
9444 LOC is the location of the #pragma token.
953ff289
DN
9445*/
9446
9447#define OMP_PARALLEL_CLAUSE_MASK \
9448 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
9449 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
9450 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
9451 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
9452 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
9453 | (1u << PRAGMA_OMP_CLAUSE_COPYIN) \
9454 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
9455 | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
9456
9457static tree
c2255bc4 9458c_parser_omp_parallel (location_t loc, c_parser *parser)
953ff289
DN
9459{
9460 enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
9461 const char *p_name = "#pragma omp parallel";
9462 tree stmt, clauses, par_clause, ws_clause, block;
9463 unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
9464
9465 if (c_parser_next_token_is_keyword (parser, RID_FOR))
9466 {
9467 c_parser_consume_token (parser);
9468 p_kind = PRAGMA_OMP_PARALLEL_FOR;
9469 p_name = "#pragma omp parallel for";
9470 mask |= OMP_FOR_CLAUSE_MASK;
9471 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
9472 }
9473 else if (c_parser_next_token_is (parser, CPP_NAME))
9474 {
9475 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
9476 if (strcmp (p, "sections") == 0)
9477 {
9478 c_parser_consume_token (parser);
9479 p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
9480 p_name = "#pragma omp parallel sections";
9481 mask |= OMP_SECTIONS_CLAUSE_MASK;
9482 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
9483 }
9484 }
9485
9486 clauses = c_parser_omp_all_clauses (parser, mask, p_name);
9487
9488 switch (p_kind)
9489 {
9490 case PRAGMA_OMP_PARALLEL:
9491 block = c_begin_omp_parallel ();
9492 c_parser_statement (parser);
c2255bc4 9493 stmt = c_finish_omp_parallel (loc, clauses, block);
953ff289
DN
9494 break;
9495
9496 case PRAGMA_OMP_PARALLEL_FOR:
9497 block = c_begin_omp_parallel ();
c2255bc4
AH
9498 c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
9499 c_parser_omp_for_loop (loc, parser, ws_clause, &par_clause);
9500 stmt = c_finish_omp_parallel (loc, par_clause, block);
761041be 9501 OMP_PARALLEL_COMBINED (stmt) = 1;
953ff289
DN
9502 break;
9503
9504 case PRAGMA_OMP_PARALLEL_SECTIONS:
9505 block = c_begin_omp_parallel ();
c2255bc4
AH
9506 c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
9507 stmt = c_parser_omp_sections_scope (loc, parser);
953ff289
DN
9508 if (stmt)
9509 OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
c2255bc4 9510 stmt = c_finish_omp_parallel (loc, par_clause, block);
761041be 9511 OMP_PARALLEL_COMBINED (stmt) = 1;
953ff289
DN
9512 break;
9513
9514 default:
9515 gcc_unreachable ();
9516 }
9517
9518 return stmt;
9519}
9520
9521/* OpenMP 2.5:
9522 # pragma omp single single-clause[optseq] new-line
9523 structured-block
c2255bc4
AH
9524
9525 LOC is the location of the #pragma.
953ff289
DN
9526*/
9527
9528#define OMP_SINGLE_CLAUSE_MASK \
9529 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
9530 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
9531 | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
9532 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
9533
9534static tree
c2255bc4 9535c_parser_omp_single (location_t loc, c_parser *parser)
953ff289
DN
9536{
9537 tree stmt = make_node (OMP_SINGLE);
c2255bc4 9538 SET_EXPR_LOCATION (stmt, loc);
953ff289
DN
9539 TREE_TYPE (stmt) = void_type_node;
9540
9541 OMP_SINGLE_CLAUSES (stmt)
9542 = c_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
9543 "#pragma omp single");
9544 OMP_SINGLE_BODY (stmt) = c_parser_omp_structured_block (parser);
9545
9546 return add_stmt (stmt);
9547}
9548
a68ab351
JJ
9549/* OpenMP 3.0:
9550 # pragma omp task task-clause[optseq] new-line
c2255bc4
AH
9551
9552 LOC is the location of the #pragma.
a68ab351
JJ
9553*/
9554
9555#define OMP_TASK_CLAUSE_MASK \
9556 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
9557 | (1u << PRAGMA_OMP_CLAUSE_UNTIED) \
9558 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
9559 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
9560 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
9561 | (1u << PRAGMA_OMP_CLAUSE_SHARED))
9562
9563static tree
c2255bc4 9564c_parser_omp_task (location_t loc, c_parser *parser)
a68ab351
JJ
9565{
9566 tree clauses, block;
9567
9568 clauses = c_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
9569 "#pragma omp task");
9570
9571 block = c_begin_omp_task ();
9572 c_parser_statement (parser);
c2255bc4 9573 return c_finish_omp_task (loc, clauses, block);
a68ab351
JJ
9574}
9575
9576/* OpenMP 3.0:
9577 # pragma omp taskwait new-line
9578*/
9579
9580static void
9581c_parser_omp_taskwait (c_parser *parser)
9582{
c2255bc4 9583 location_t loc = c_parser_peek_token (parser)->location;
a68ab351
JJ
9584 c_parser_consume_pragma (parser);
9585 c_parser_skip_to_pragma_eol (parser);
9586
c2255bc4 9587 c_finish_omp_taskwait (loc);
a68ab351 9588}
953ff289
DN
9589
9590/* Main entry point to parsing most OpenMP pragmas. */
9591
9592static void
9593c_parser_omp_construct (c_parser *parser)
9594{
9595 enum pragma_kind p_kind;
9596 location_t loc;
9597 tree stmt;
9598
9599 loc = c_parser_peek_token (parser)->location;
9600 p_kind = c_parser_peek_token (parser)->pragma_kind;
9601 c_parser_consume_pragma (parser);
9602
9603 switch (p_kind)
9604 {
9605 case PRAGMA_OMP_ATOMIC:
c2255bc4 9606 c_parser_omp_atomic (loc, parser);
953ff289
DN
9607 return;
9608 case PRAGMA_OMP_CRITICAL:
c2255bc4 9609 stmt = c_parser_omp_critical (loc, parser);
953ff289
DN
9610 break;
9611 case PRAGMA_OMP_FOR:
c2255bc4 9612 stmt = c_parser_omp_for (loc, parser);
953ff289
DN
9613 break;
9614 case PRAGMA_OMP_MASTER:
c2255bc4 9615 stmt = c_parser_omp_master (loc, parser);
953ff289
DN
9616 break;
9617 case PRAGMA_OMP_ORDERED:
c2255bc4 9618 stmt = c_parser_omp_ordered (loc, parser);
953ff289
DN
9619 break;
9620 case PRAGMA_OMP_PARALLEL:
c2255bc4 9621 stmt = c_parser_omp_parallel (loc, parser);
953ff289
DN
9622 break;
9623 case PRAGMA_OMP_SECTIONS:
c2255bc4 9624 stmt = c_parser_omp_sections (loc, parser);
953ff289
DN
9625 break;
9626 case PRAGMA_OMP_SINGLE:
c2255bc4 9627 stmt = c_parser_omp_single (loc, parser);
953ff289 9628 break;
a68ab351 9629 case PRAGMA_OMP_TASK:
c2255bc4 9630 stmt = c_parser_omp_task (loc, parser);
a68ab351 9631 break;
953ff289
DN
9632 default:
9633 gcc_unreachable ();
9634 }
9635
9636 if (stmt)
c2255bc4 9637 gcc_assert (EXPR_LOCATION (stmt) != UNKNOWN_LOCATION);
953ff289
DN
9638}
9639
9640
9641/* OpenMP 2.5:
9642 # pragma omp threadprivate (variable-list) */
9643
9644static void
9645c_parser_omp_threadprivate (c_parser *parser)
9646{
9647 tree vars, t;
c2255bc4 9648 location_t loc;
953ff289
DN
9649
9650 c_parser_consume_pragma (parser);
c2255bc4 9651 loc = c_parser_peek_token (parser)->location;
d75d71e0 9652 vars = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_ERROR, NULL);
953ff289 9653
953ff289
DN
9654 /* Mark every variable in VARS to be assigned thread local storage. */
9655 for (t = vars; t; t = TREE_CHAIN (t))
9656 {
9657 tree v = TREE_PURPOSE (t);
9658
c2255bc4
AH
9659 /* FIXME diagnostics: Ideally we should keep individual
9660 locations for all the variables in the var list to make the
9661 following errors more precise. Perhaps
9662 c_parser_omp_var_list_parens() should construct a list of
9663 locations to go along with the var list. */
9664
953ff289
DN
9665 /* If V had already been marked threadprivate, it doesn't matter
9666 whether it had been used prior to this point. */
5df27e4a 9667 if (TREE_CODE (v) != VAR_DECL)
c2255bc4 9668 error_at (loc, "%qD is not a variable", v);
5df27e4a 9669 else if (TREE_USED (v) && !C_DECL_THREADPRIVATE_P (v))
c2255bc4 9670 error_at (loc, "%qE declared %<threadprivate%> after first use", v);
953ff289 9671 else if (! TREE_STATIC (v) && ! DECL_EXTERNAL (v))
c2255bc4 9672 error_at (loc, "automatic variable %qE cannot be %<threadprivate%>", v);
5df27e4a
JJ
9673 else if (TREE_TYPE (v) == error_mark_node)
9674 ;
953ff289 9675 else if (! COMPLETE_TYPE_P (TREE_TYPE (v)))
c2255bc4 9676 error_at (loc, "%<threadprivate%> %qE has incomplete type", v);
953ff289
DN
9677 else
9678 {
9679 if (! DECL_THREAD_LOCAL_P (v))
9680 {
9681 DECL_TLS_MODEL (v) = decl_default_tls_model (v);
9682 /* If rtl has been already set for this var, call
9683 make_decl_rtl once again, so that encode_section_info
9684 has a chance to look at the new decl flags. */
9685 if (DECL_RTL_SET_P (v))
9686 make_decl_rtl (v);
9687 }
9688 C_DECL_THREADPRIVATE_P (v) = 1;
9689 }
9690 }
9691
9692 c_parser_skip_to_pragma_eol (parser);
9693}
9694
bc4071dd 9695\f
27bf414c
JM
9696/* Parse a single source file. */
9697
9698void
9699c_parse_file (void)
9700{
bc4071dd
RH
9701 /* Use local storage to begin. If the first token is a pragma, parse it.
9702 If it is #pragma GCC pch_preprocess, then this will load a PCH file
9703 which will cause garbage collection. */
9704 c_parser tparser;
9705
9706 memset (&tparser, 0, sizeof tparser);
9707 the_parser = &tparser;
9708
9709 if (c_parser_peek_token (&tparser)->pragma_kind == PRAGMA_GCC_PCH_PREPROCESS)
9710 c_parser_pragma_pch_preprocess (&tparser);
9711
a9429e29 9712 the_parser = ggc_alloc_c_parser ();
bc4071dd
RH
9713 *the_parser = tparser;
9714
f9417da1
RG
9715 /* Initialize EH, if we've been told to do so. */
9716 if (flag_exceptions)
1d65f45c 9717 using_eh_for_cleanups ();
f9417da1 9718
27bf414c
JM
9719 c_parser_translation_unit (the_parser);
9720 the_parser = NULL;
9721}
9722
9723#include "gt-c-parser.h"
This page took 2.615134 seconds and 5 git commands to generate.