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