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