]> gcc.gnu.org Git - gcc.git/blame - gcc/c/c-parser.c
[AArch64] Fix TLS for ILP32.
[gcc.git] / gcc / c / c-parser.c
CommitLineData
27bf414c 1/* Parser for C and Objective-C.
23a5b65a 2 Copyright (C) 1987-2014 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 42#include "tree.h"
d8a2d370
DN
43#include "stringpool.h"
44#include "attribs.h"
45#include "stor-layout.h"
46#include "varasm.h"
47#include "trans-mem.h"
27bf414c
JM
48#include "langhooks.h"
49#include "input.h"
50#include "cpplib.h"
51#include "timevar.h"
39dabefd 52#include "c-family/c-pragma.h"
27bf414c 53#include "c-tree.h"
acf0174b 54#include "c-lang.h"
27bf414c 55#include "flags.h"
27bf414c 56#include "ggc.h"
39dabefd 57#include "c-family/c-common.h"
61d3ce20 58#include "c-family/c-objc.h"
bc4071dd
RH
59#include "vec.h"
60#include "target.h"
474eccc6 61#include "cgraph.h"
68a607d8 62#include "plugin.h"
0645c1a2 63#include "omp-low.h"
27bf414c
JM
64
65\f
27bf414c
JM
66/* Initialization routine for this file. */
67
68void
69c_parse_init (void)
70{
71 /* The only initialization required is of the reserved word
72 identifiers. */
73 unsigned int i;
74 tree id;
eea1139b 75 int mask = 0;
27bf414c 76
36c5e70a
BE
77 /* Make sure RID_MAX hasn't grown past the 8 bits used to hold the keyword in
78 the c_token structure. */
79 gcc_assert (RID_MAX <= 255);
80
eea1139b
ILT
81 mask |= D_CXXONLY;
82 if (!flag_isoc99)
83 mask |= D_C99;
84 if (flag_no_asm)
85 {
86 mask |= D_ASM | D_EXT;
87 if (!flag_isoc99)
88 mask |= D_EXT89;
89 }
27bf414c 90 if (!c_dialect_objc ())
eea1139b 91 mask |= D_OBJC | D_CXX_OBJC;
27bf414c 92
a9429e29 93 ridpointers = ggc_alloc_cleared_vec_tree ((int) RID_MAX);
eea1139b 94 for (i = 0; i < num_c_common_reswords; i++)
27bf414c
JM
95 {
96 /* If a keyword is disabled, do not enter it into the table
97 and so create a canonical spelling that isn't a keyword. */
eea1139b
ILT
98 if (c_common_reswords[i].disable & mask)
99 {
100 if (warn_cxx_compat
101 && (c_common_reswords[i].disable & D_CXXWARN))
102 {
103 id = get_identifier (c_common_reswords[i].word);
104 C_SET_RID_CODE (id, RID_CXX_COMPAT_WARN);
105 C_IS_RESERVED_WORD (id) = 1;
106 }
107 continue;
108 }
27bf414c 109
eea1139b
ILT
110 id = get_identifier (c_common_reswords[i].word);
111 C_SET_RID_CODE (id, c_common_reswords[i].rid);
27bf414c 112 C_IS_RESERVED_WORD (id) = 1;
eea1139b 113 ridpointers [(int) c_common_reswords[i].rid] = id;
27bf414c
JM
114 }
115}
116\f
117/* The C lexer intermediates between the lexer in cpplib and c-lex.c
118 and the C parser. Unlike the C++ lexer, the parser structure
119 stores the lexer information instead of using a separate structure.
120 Identifiers are separated into ordinary identifiers, type names,
121 keywords and some other Objective-C types of identifiers, and some
122 look-ahead is maintained.
123
124 ??? It might be a good idea to lex the whole file up front (as for
125 C++). It would then be possible to share more of the C and C++
126 lexer code, if desired. */
127
128/* The following local token type is used. */
129
130/* A keyword. */
131#define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
132
27bf414c
JM
133/* More information about the type of a CPP_NAME token. */
134typedef enum c_id_kind {
135 /* An ordinary identifier. */
136 C_ID_ID,
137 /* An identifier declared as a typedef name. */
138 C_ID_TYPENAME,
139 /* An identifier declared as an Objective-C class name. */
140 C_ID_CLASSNAME,
36c5e70a
BE
141 /* An address space identifier. */
142 C_ID_ADDRSPACE,
27bf414c
JM
143 /* Not an identifier. */
144 C_ID_NONE
145} c_id_kind;
146
147/* A single C token after string literal concatenation and conversion
148 of preprocessing tokens to tokens. */
d1b38208 149typedef struct GTY (()) c_token {
27bf414c
JM
150 /* The kind of token. */
151 ENUM_BITFIELD (cpp_ttype) type : 8;
152 /* If this token is a CPP_NAME, this value indicates whether also
153 declared as some kind of type. Otherwise, it is C_ID_NONE. */
154 ENUM_BITFIELD (c_id_kind) id_kind : 8;
155 /* If this token is a keyword, this value indicates which keyword.
156 Otherwise, this value is RID_MAX. */
157 ENUM_BITFIELD (rid) keyword : 8;
bc4071dd
RH
158 /* If this token is a CPP_PRAGMA, this indicates the pragma that
159 was seen. Otherwise it is PRAGMA_NONE. */
13fa1171 160 ENUM_BITFIELD (pragma_kind) pragma_kind : 8;
27bf414c
JM
161 /* The location at which this token was found. */
162 location_t location;
8415f317
NF
163 /* The value associated with this token, if any. */
164 tree value;
27bf414c
JM
165} c_token;
166
167/* A parser structure recording information about the state and
168 context of parsing. Includes lexer information with up to two
169 tokens of look-ahead; more are not needed for C. */
d1b38208 170typedef struct GTY(()) c_parser {
27bf414c 171 /* The look-ahead tokens. */
acf0174b
JJ
172 c_token * GTY((skip)) tokens;
173 /* Buffer for look-ahead tokens. */
174 c_token tokens_buf[2];
175 /* How many look-ahead tokens are available (0, 1 or 2, or
176 more if parsing from pre-lexed tokens). */
177 unsigned int tokens_avail;
27bf414c
JM
178 /* True if a syntax error is being recovered from; false otherwise.
179 c_parser_error sets this flag. It should clear this flag when
180 enough tokens have been consumed to recover from the error. */
181 BOOL_BITFIELD error : 1;
bc4071dd
RH
182 /* True if we're processing a pragma, and shouldn't automatically
183 consume CPP_PRAGMA_EOL. */
184 BOOL_BITFIELD in_pragma : 1;
b4b56033
MLI
185 /* True if we're parsing the outermost block of an if statement. */
186 BOOL_BITFIELD in_if_block : 1;
46c2514e
TT
187 /* True if we want to lex an untranslated string. */
188 BOOL_BITFIELD lex_untranslated_string : 1;
1973201f 189
0bacb8c7 190 /* Objective-C specific parser/lexer information. */
1973201f
NP
191
192 /* True if we are in a context where the Objective-C "PQ" keywords
193 are considered keywords. */
0bacb8c7 194 BOOL_BITFIELD objc_pq_context : 1;
f05b9d93
NP
195 /* True if we are parsing a (potential) Objective-C foreach
196 statement. This is set to true after we parsed 'for (' and while
197 we wait for 'in' or ';' to decide if it's a standard C for loop or an
198 Objective-C foreach loop. */
199 BOOL_BITFIELD objc_could_be_foreach_context : 1;
0bacb8c7
TT
200 /* The following flag is needed to contextualize Objective-C lexical
201 analysis. In some cases (e.g., 'int NSObject;'), it is
202 undesirable to bind an identifier to an Objective-C class, even
203 if a class with that name exists. */
204 BOOL_BITFIELD objc_need_raw_identifier : 1;
0a35513e
AH
205 /* Nonzero if we're processing a __transaction statement. The value
206 is 1 | TM_STMT_ATTR_*. */
207 unsigned int in_transaction : 4;
668ea4b1
IS
208 /* True if we are in a context where the Objective-C "Property attribute"
209 keywords are valid. */
210 BOOL_BITFIELD objc_property_attr_context : 1;
41958c28
BI
211
212 /* Cilk Plus specific parser/lexer information. */
213
214 /* Buffer to hold all the tokens from parsing the vector attribute for the
215 SIMD-enabled functions (formerly known as elemental functions). */
216 vec <c_token, va_gc> *cilk_simd_fn_tokens;
27bf414c
JM
217} c_parser;
218
bc4071dd
RH
219
220/* The actual parser and external interface. ??? Does this need to be
221 garbage-collected? */
222
223static GTY (()) c_parser *the_parser;
224
27bf414c
JM
225/* Read in and lex a single token, storing it in *TOKEN. */
226
227static void
0bacb8c7 228c_lex_one_token (c_parser *parser, c_token *token)
27bf414c
JM
229{
230 timevar_push (TV_LEX);
bc4071dd 231
46c2514e
TT
232 token->type = c_lex_with_flags (&token->value, &token->location, NULL,
233 (parser->lex_untranslated_string
234 ? C_LEX_STRING_NO_TRANSLATE : 0));
bc4071dd
RH
235 token->id_kind = C_ID_NONE;
236 token->keyword = RID_MAX;
237 token->pragma_kind = PRAGMA_NONE;
bc4071dd 238
27bf414c
JM
239 switch (token->type)
240 {
241 case CPP_NAME:
27bf414c
JM
242 {
243 tree decl;
244
0bacb8c7
TT
245 bool objc_force_identifier = parser->objc_need_raw_identifier;
246 if (c_dialect_objc ())
247 parser->objc_need_raw_identifier = false;
27bf414c
JM
248
249 if (C_IS_RESERVED_WORD (token->value))
250 {
251 enum rid rid_code = C_RID_CODE (token->value);
252
eea1139b
ILT
253 if (rid_code == RID_CXX_COMPAT_WARN)
254 {
3ba09659
AH
255 warning_at (token->location,
256 OPT_Wc___compat,
88388a52
JM
257 "identifier %qE conflicts with C++ keyword",
258 token->value);
eea1139b 259 }
36c5e70a
BE
260 else if (rid_code >= RID_FIRST_ADDR_SPACE
261 && rid_code <= RID_LAST_ADDR_SPACE)
262 {
263 token->id_kind = C_ID_ADDRSPACE;
264 token->keyword = rid_code;
265 break;
266 }
1973201f 267 else if (c_dialect_objc () && OBJC_IS_PQ_KEYWORD (rid_code))
27bf414c 268 {
1973201f
NP
269 /* We found an Objective-C "pq" keyword (in, out,
270 inout, bycopy, byref, oneway). They need special
271 care because the interpretation depends on the
d853ee42 272 context. */
1973201f 273 if (parser->objc_pq_context)
27bf414c 274 {
27bf414c
JM
275 token->type = CPP_KEYWORD;
276 token->keyword = rid_code;
277 break;
278 }
f05b9d93
NP
279 else if (parser->objc_could_be_foreach_context
280 && rid_code == RID_IN)
281 {
282 /* We are in Objective-C, inside a (potential)
283 foreach context (which means after having
284 parsed 'for (', but before having parsed ';'),
285 and we found 'in'. We consider it the keyword
286 which terminates the declaration at the
287 beginning of a foreach-statement. Note that
288 this means you can't use 'in' for anything else
289 in that context; in particular, in Objective-C
290 you can't use 'in' as the name of the running
291 variable in a C for loop. We could potentially
292 try to add code here to disambiguate, but it
d853ee42 293 seems a reasonable limitation. */
f05b9d93
NP
294 token->type = CPP_KEYWORD;
295 token->keyword = rid_code;
296 break;
297 }
1973201f
NP
298 /* Else, "pq" keywords outside of the "pq" context are
299 not keywords, and we fall through to the code for
d853ee42 300 normal tokens. */
1973201f 301 }
668ea4b1
IS
302 else if (c_dialect_objc () && OBJC_IS_PATTR_KEYWORD (rid_code))
303 {
d853ee42
NP
304 /* We found an Objective-C "property attribute"
305 keyword (getter, setter, readonly, etc). These are
668ea4b1
IS
306 only valid in the property context. */
307 if (parser->objc_property_attr_context)
308 {
309 token->type = CPP_KEYWORD;
310 token->keyword = rid_code;
311 break;
312 }
313 /* Else they are not special keywords.
314 */
315 }
1973201f
NP
316 else if (c_dialect_objc ()
317 && (OBJC_IS_AT_KEYWORD (rid_code)
318 || OBJC_IS_CXX_KEYWORD (rid_code)))
319 {
320 /* We found one of the Objective-C "@" keywords (defs,
321 selector, synchronized, etc) or one of the
322 Objective-C "cxx" keywords (class, private,
323 protected, public, try, catch, throw) without a
324 preceding '@' sign. Do nothing and fall through to
325 the code for normal tokens (in C++ we would still
d853ee42 326 consider the CXX ones keywords, but not in C). */
1973201f 327 ;
27bf414c
JM
328 }
329 else
330 {
27bf414c
JM
331 token->type = CPP_KEYWORD;
332 token->keyword = rid_code;
333 break;
334 }
335 }
336
337 decl = lookup_name (token->value);
338 if (decl)
339 {
340 if (TREE_CODE (decl) == TYPE_DECL)
341 {
342 token->id_kind = C_ID_TYPENAME;
343 break;
344 }
345 }
346 else if (c_dialect_objc ())
347 {
348 tree objc_interface_decl = objc_is_class_name (token->value);
349 /* Objective-C class names are in the same namespace as
350 variables and typedefs, and hence are shadowed by local
351 declarations. */
352 if (objc_interface_decl
0d8a2528 353 && (!objc_force_identifier || global_bindings_p ()))
27bf414c
JM
354 {
355 token->value = objc_interface_decl;
356 token->id_kind = C_ID_CLASSNAME;
357 break;
358 }
359 }
bc4071dd 360 token->id_kind = C_ID_ID;
27bf414c 361 }
27bf414c
JM
362 break;
363 case CPP_AT_NAME:
364 /* This only happens in Objective-C; it must be a keyword. */
365 token->type = CPP_KEYWORD;
49b91f05
NP
366 switch (C_RID_CODE (token->value))
367 {
368 /* Replace 'class' with '@class', 'private' with '@private',
369 etc. This prevents confusion with the C++ keyword
370 'class', and makes the tokens consistent with other
371 Objective-C 'AT' keywords. For example '@class' is
372 reported as RID_AT_CLASS which is consistent with
373 '@synchronized', which is reported as
374 RID_AT_SYNCHRONIZED.
375 */
376 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
377 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
378 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
379 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
380 case RID_THROW: token->keyword = RID_AT_THROW; break;
381 case RID_TRY: token->keyword = RID_AT_TRY; break;
382 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
383 default: token->keyword = C_RID_CODE (token->value);
384 }
27bf414c
JM
385 break;
386 case CPP_COLON:
387 case CPP_COMMA:
388 case CPP_CLOSE_PAREN:
389 case CPP_SEMICOLON:
390 /* These tokens may affect the interpretation of any identifiers
391 following, if doing Objective-C. */
0bacb8c7
TT
392 if (c_dialect_objc ())
393 parser->objc_need_raw_identifier = false;
bc4071dd
RH
394 break;
395 case CPP_PRAGMA:
396 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
d75d71e0 397 token->pragma_kind = (enum pragma_kind) TREE_INT_CST_LOW (token->value);
bc4071dd 398 token->value = NULL;
27bf414c
JM
399 break;
400 default:
27bf414c
JM
401 break;
402 }
403 timevar_pop (TV_LEX);
404}
405
406/* Return a pointer to the next token from PARSER, reading it in if
407 necessary. */
408
409static inline c_token *
410c_parser_peek_token (c_parser *parser)
411{
412 if (parser->tokens_avail == 0)
413 {
0bacb8c7 414 c_lex_one_token (parser, &parser->tokens[0]);
27bf414c
JM
415 parser->tokens_avail = 1;
416 }
417 return &parser->tokens[0];
418}
419
420/* Return true if the next token from PARSER has the indicated
421 TYPE. */
422
423static inline bool
424c_parser_next_token_is (c_parser *parser, enum cpp_ttype type)
425{
426 return c_parser_peek_token (parser)->type == type;
427}
428
429/* Return true if the next token from PARSER does not have the
430 indicated TYPE. */
431
432static inline bool
433c_parser_next_token_is_not (c_parser *parser, enum cpp_ttype type)
434{
435 return !c_parser_next_token_is (parser, type);
436}
437
438/* Return true if the next token from PARSER is the indicated
439 KEYWORD. */
440
441static inline bool
442c_parser_next_token_is_keyword (c_parser *parser, enum rid keyword)
443{
dbc518f0 444 return c_parser_peek_token (parser)->keyword == keyword;
27bf414c
JM
445}
446
29ce73cb
PB
447/* Return a pointer to the next-but-one token from PARSER, reading it
448 in if necessary. The next token is already read in. */
449
450static c_token *
451c_parser_peek_2nd_token (c_parser *parser)
452{
453 if (parser->tokens_avail >= 2)
454 return &parser->tokens[1];
455 gcc_assert (parser->tokens_avail == 1);
456 gcc_assert (parser->tokens[0].type != CPP_EOF);
457 gcc_assert (parser->tokens[0].type != CPP_PRAGMA_EOL);
458 c_lex_one_token (parser, &parser->tokens[1]);
459 parser->tokens_avail = 2;
460 return &parser->tokens[1];
461}
462
27bf414c
JM
463/* Return true if TOKEN can start a type name,
464 false otherwise. */
465static bool
466c_token_starts_typename (c_token *token)
467{
468 switch (token->type)
469 {
470 case CPP_NAME:
471 switch (token->id_kind)
472 {
473 case C_ID_ID:
474 return false;
36c5e70a
BE
475 case C_ID_ADDRSPACE:
476 return true;
27bf414c
JM
477 case C_ID_TYPENAME:
478 return true;
479 case C_ID_CLASSNAME:
480 gcc_assert (c_dialect_objc ());
481 return true;
482 default:
483 gcc_unreachable ();
484 }
485 case CPP_KEYWORD:
486 switch (token->keyword)
487 {
488 case RID_UNSIGNED:
489 case RID_LONG:
a6766312 490 case RID_INT128:
27bf414c
JM
491 case RID_SHORT:
492 case RID_SIGNED:
493 case RID_COMPLEX:
494 case RID_INT:
495 case RID_CHAR:
496 case RID_FLOAT:
497 case RID_DOUBLE:
498 case RID_VOID:
9a8ce21f
JG
499 case RID_DFLOAT32:
500 case RID_DFLOAT64:
501 case RID_DFLOAT128:
27bf414c
JM
502 case RID_BOOL:
503 case RID_ENUM:
504 case RID_STRUCT:
505 case RID_UNION:
506 case RID_TYPEOF:
507 case RID_CONST:
267bac10 508 case RID_ATOMIC:
27bf414c
JM
509 case RID_VOLATILE:
510 case RID_RESTRICT:
511 case RID_ATTRIBUTE:
ab22c1fa
CF
512 case RID_FRACT:
513 case RID_ACCUM:
514 case RID_SAT:
38b7bc7f 515 case RID_AUTO_TYPE:
27bf414c
JM
516 return true;
517 default:
518 return false;
519 }
520 case CPP_LESS:
521 if (c_dialect_objc ())
522 return true;
523 return false;
524 default:
525 return false;
526 }
527}
528
29ce73cb
PB
529enum c_lookahead_kind {
530 /* Always treat unknown identifiers as typenames. */
531 cla_prefer_type,
532
533 /* Could be parsing a nonabstract declarator. Only treat an identifier
534 as a typename if followed by another identifier or a star. */
535 cla_nonabstract_decl,
536
537 /* Never treat identifiers as typenames. */
538 cla_prefer_id
539};
540
27bf414c 541/* Return true if the next token from PARSER can start a type name,
29ce73cb
PB
542 false otherwise. LA specifies how to do lookahead in order to
543 detect unknown type names. If unsure, pick CLA_PREFER_ID. */
544
27bf414c 545static inline bool
29ce73cb 546c_parser_next_tokens_start_typename (c_parser *parser, enum c_lookahead_kind la)
27bf414c
JM
547{
548 c_token *token = c_parser_peek_token (parser);
29ce73cb
PB
549 if (c_token_starts_typename (token))
550 return true;
551
552 /* Try a bit harder to detect an unknown typename. */
553 if (la != cla_prefer_id
554 && token->type == CPP_NAME
555 && token->id_kind == C_ID_ID
556
557 /* Do not try too hard when we could have "object in array". */
558 && !parser->objc_could_be_foreach_context
559
560 && (la == cla_prefer_type
561 || c_parser_peek_2nd_token (parser)->type == CPP_NAME
562 || c_parser_peek_2nd_token (parser)->type == CPP_MULT)
563
564 /* Only unknown identifiers. */
565 && !lookup_name (token->value))
566 return true;
567
568 return false;
27bf414c
JM
569}
570
f725e721
PB
571/* Return true if TOKEN is a type qualifier, false otherwise. */
572static bool
573c_token_is_qualifier (c_token *token)
574{
575 switch (token->type)
576 {
577 case CPP_NAME:
578 switch (token->id_kind)
579 {
580 case C_ID_ADDRSPACE:
581 return true;
582 default:
583 return false;
584 }
585 case CPP_KEYWORD:
586 switch (token->keyword)
587 {
588 case RID_CONST:
589 case RID_VOLATILE:
590 case RID_RESTRICT:
591 case RID_ATTRIBUTE:
267bac10 592 case RID_ATOMIC:
f725e721
PB
593 return true;
594 default:
595 return false;
596 }
597 case CPP_LESS:
598 return false;
599 default:
600 gcc_unreachable ();
601 }
602}
603
604/* Return true if the next token from PARSER is a type qualifier,
605 false otherwise. */
606static inline bool
607c_parser_next_token_is_qualifier (c_parser *parser)
608{
609 c_token *token = c_parser_peek_token (parser);
610 return c_token_is_qualifier (token);
611}
612
27bf414c
JM
613/* Return true if TOKEN can start declaration specifiers, false
614 otherwise. */
615static bool
616c_token_starts_declspecs (c_token *token)
617{
618 switch (token->type)
619 {
620 case CPP_NAME:
621 switch (token->id_kind)
622 {
623 case C_ID_ID:
624 return false;
36c5e70a
BE
625 case C_ID_ADDRSPACE:
626 return true;
27bf414c
JM
627 case C_ID_TYPENAME:
628 return true;
629 case C_ID_CLASSNAME:
630 gcc_assert (c_dialect_objc ());
631 return true;
632 default:
633 gcc_unreachable ();
634 }
635 case CPP_KEYWORD:
636 switch (token->keyword)
637 {
638 case RID_STATIC:
639 case RID_EXTERN:
640 case RID_REGISTER:
641 case RID_TYPEDEF:
642 case RID_INLINE:
bbceee64 643 case RID_NORETURN:
27bf414c
JM
644 case RID_AUTO:
645 case RID_THREAD:
646 case RID_UNSIGNED:
647 case RID_LONG:
a6766312 648 case RID_INT128:
27bf414c
JM
649 case RID_SHORT:
650 case RID_SIGNED:
651 case RID_COMPLEX:
652 case RID_INT:
653 case RID_CHAR:
654 case RID_FLOAT:
655 case RID_DOUBLE:
656 case RID_VOID:
9a8ce21f
JG
657 case RID_DFLOAT32:
658 case RID_DFLOAT64:
659 case RID_DFLOAT128:
27bf414c
JM
660 case RID_BOOL:
661 case RID_ENUM:
662 case RID_STRUCT:
663 case RID_UNION:
664 case RID_TYPEOF:
665 case RID_CONST:
666 case RID_VOLATILE:
667 case RID_RESTRICT:
668 case RID_ATTRIBUTE:
ab22c1fa
CF
669 case RID_FRACT:
670 case RID_ACCUM:
671 case RID_SAT:
d19fa6b5 672 case RID_ALIGNAS:
267bac10 673 case RID_ATOMIC:
38b7bc7f 674 case RID_AUTO_TYPE:
27bf414c
JM
675 return true;
676 default:
677 return false;
678 }
679 case CPP_LESS:
680 if (c_dialect_objc ())
681 return true;
682 return false;
683 default:
684 return false;
685 }
686}
687
32912286
JM
688
689/* Return true if TOKEN can start declaration specifiers or a static
690 assertion, false otherwise. */
691static bool
692c_token_starts_declaration (c_token *token)
693{
694 if (c_token_starts_declspecs (token)
695 || token->keyword == RID_STATIC_ASSERT)
696 return true;
697 else
698 return false;
699}
700
27bf414c
JM
701/* Return true if the next token from PARSER can start declaration
702 specifiers, false otherwise. */
703static inline bool
704c_parser_next_token_starts_declspecs (c_parser *parser)
705{
706 c_token *token = c_parser_peek_token (parser);
bede2adc
NP
707
708 /* In Objective-C, a classname normally starts a declspecs unless it
709 is immediately followed by a dot. In that case, it is the
710 Objective-C 2.0 "dot-syntax" for class objects, ie, calls the
711 setter/getter on the class. c_token_starts_declspecs() can't
712 differentiate between the two cases because it only checks the
713 current token, so we have a special check here. */
714 if (c_dialect_objc ()
715 && token->type == CPP_NAME
716 && token->id_kind == C_ID_CLASSNAME
717 && c_parser_peek_2nd_token (parser)->type == CPP_DOT)
718 return false;
719
27bf414c
JM
720 return c_token_starts_declspecs (token);
721}
722
2f413185 723/* Return true if the next tokens from PARSER can start declaration
32912286
JM
724 specifiers or a static assertion, false otherwise. */
725static inline bool
2f413185 726c_parser_next_tokens_start_declaration (c_parser *parser)
32912286
JM
727{
728 c_token *token = c_parser_peek_token (parser);
bede2adc
NP
729
730 /* Same as above. */
731 if (c_dialect_objc ()
732 && token->type == CPP_NAME
733 && token->id_kind == C_ID_CLASSNAME
734 && c_parser_peek_2nd_token (parser)->type == CPP_DOT)
735 return false;
736
2f413185
PB
737 /* Labels do not start declarations. */
738 if (token->type == CPP_NAME
739 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
740 return false;
741
742 if (c_token_starts_declaration (token))
743 return true;
744
29ce73cb 745 if (c_parser_next_tokens_start_typename (parser, cla_nonabstract_decl))
2f413185
PB
746 return true;
747
748 return false;
32912286
JM
749}
750
27bf414c
JM
751/* Consume the next token from PARSER. */
752
753static void
754c_parser_consume_token (c_parser *parser)
755{
bc4071dd
RH
756 gcc_assert (parser->tokens_avail >= 1);
757 gcc_assert (parser->tokens[0].type != CPP_EOF);
758 gcc_assert (!parser->in_pragma || parser->tokens[0].type != CPP_PRAGMA_EOL);
759 gcc_assert (parser->error || parser->tokens[0].type != CPP_PRAGMA);
acf0174b
JJ
760 if (parser->tokens != &parser->tokens_buf[0])
761 parser->tokens++;
762 else if (parser->tokens_avail == 2)
27bf414c 763 parser->tokens[0] = parser->tokens[1];
27bf414c
JM
764 parser->tokens_avail--;
765}
766
bc4071dd
RH
767/* Expect the current token to be a #pragma. Consume it and remember
768 that we've begun parsing a pragma. */
769
770static void
771c_parser_consume_pragma (c_parser *parser)
772{
773 gcc_assert (!parser->in_pragma);
774 gcc_assert (parser->tokens_avail >= 1);
775 gcc_assert (parser->tokens[0].type == CPP_PRAGMA);
acf0174b
JJ
776 if (parser->tokens != &parser->tokens_buf[0])
777 parser->tokens++;
778 else if (parser->tokens_avail == 2)
bc4071dd
RH
779 parser->tokens[0] = parser->tokens[1];
780 parser->tokens_avail--;
781 parser->in_pragma = true;
782}
783
8400e75e 784/* Update the global input_location from TOKEN. */
27bf414c
JM
785static inline void
786c_parser_set_source_position_from_token (c_token *token)
787{
788 if (token->type != CPP_EOF)
789 {
790 input_location = token->location;
27bf414c
JM
791 }
792}
793
27bf414c
JM
794/* Issue a diagnostic of the form
795 FILE:LINE: MESSAGE before TOKEN
796 where TOKEN is the next token in the input stream of PARSER.
797 MESSAGE (specified by the caller) is usually of the form "expected
798 OTHER-TOKEN".
799
800 Do not issue a diagnostic if still recovering from an error.
801
802 ??? This is taken from the C++ parser, but building up messages in
803 this way is not i18n-friendly and some other approach should be
804 used. */
805
806static void
4b794eaf 807c_parser_error (c_parser *parser, const char *gmsgid)
27bf414c
JM
808{
809 c_token *token = c_parser_peek_token (parser);
810 if (parser->error)
811 return;
812 parser->error = true;
4b794eaf 813 if (!gmsgid)
27bf414c
JM
814 return;
815 /* This diagnostic makes more sense if it is tagged to the line of
816 the token we just peeked at. */
817 c_parser_set_source_position_from_token (token);
4b794eaf 818 c_parse_error (gmsgid,
27bf414c
JM
819 /* Because c_parse_error does not understand
820 CPP_KEYWORD, keywords are treated like
821 identifiers. */
822 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
cfc93532
MLI
823 /* ??? The C parser does not save the cpp flags of a
824 token, we need to pass 0 here and we will not get
825 the source spelling of some tokens but rather the
826 canonical spelling. */
827 token->value, /*flags=*/0);
27bf414c
JM
828}
829
830/* If the next token is of the indicated TYPE, consume it. Otherwise,
831 issue the error MSGID. If MSGID is NULL then a message has already
832 been produced and no message will be produced this time. Returns
833 true if found, false otherwise. */
834
835static bool
836c_parser_require (c_parser *parser,
837 enum cpp_ttype type,
838 const char *msgid)
839{
840 if (c_parser_next_token_is (parser, type))
841 {
842 c_parser_consume_token (parser);
843 return true;
844 }
845 else
846 {
847 c_parser_error (parser, msgid);
848 return false;
849 }
850}
851
852/* If the next token is the indicated keyword, consume it. Otherwise,
853 issue the error MSGID. Returns true if found, false otherwise. */
854
855static bool
856c_parser_require_keyword (c_parser *parser,
857 enum rid keyword,
858 const char *msgid)
859{
860 if (c_parser_next_token_is_keyword (parser, keyword))
861 {
862 c_parser_consume_token (parser);
863 return true;
864 }
865 else
866 {
867 c_parser_error (parser, msgid);
868 return false;
869 }
870}
871
872/* Like c_parser_require, except that tokens will be skipped until the
873 desired token is found. An error message is still produced if the
874 next token is not as expected. If MSGID is NULL then a message has
875 already been produced and no message will be produced this
876 time. */
877
878static void
879c_parser_skip_until_found (c_parser *parser,
880 enum cpp_ttype type,
881 const char *msgid)
882{
883 unsigned nesting_depth = 0;
884
885 if (c_parser_require (parser, type, msgid))
886 return;
887
888 /* Skip tokens until the desired token is found. */
889 while (true)
890 {
891 /* Peek at the next token. */
892 c_token *token = c_parser_peek_token (parser);
893 /* If we've reached the token we want, consume it and stop. */
894 if (token->type == type && !nesting_depth)
895 {
896 c_parser_consume_token (parser);
897 break;
898 }
bc4071dd 899
27bf414c
JM
900 /* If we've run out of tokens, stop. */
901 if (token->type == CPP_EOF)
902 return;
bc4071dd
RH
903 if (token->type == CPP_PRAGMA_EOL && parser->in_pragma)
904 return;
27bf414c
JM
905 if (token->type == CPP_OPEN_BRACE
906 || token->type == CPP_OPEN_PAREN
907 || token->type == CPP_OPEN_SQUARE)
908 ++nesting_depth;
909 else if (token->type == CPP_CLOSE_BRACE
910 || token->type == CPP_CLOSE_PAREN
911 || token->type == CPP_CLOSE_SQUARE)
912 {
913 if (nesting_depth-- == 0)
914 break;
915 }
916 /* Consume this token. */
917 c_parser_consume_token (parser);
918 }
919 parser->error = false;
920}
921
922/* Skip tokens until the end of a parameter is found, but do not
923 consume the comma, semicolon or closing delimiter. */
924
925static void
926c_parser_skip_to_end_of_parameter (c_parser *parser)
927{
928 unsigned nesting_depth = 0;
929
930 while (true)
931 {
932 c_token *token = c_parser_peek_token (parser);
933 if ((token->type == CPP_COMMA || token->type == CPP_SEMICOLON)
934 && !nesting_depth)
935 break;
936 /* If we've run out of tokens, stop. */
937 if (token->type == CPP_EOF)
938 return;
bc4071dd
RH
939 if (token->type == CPP_PRAGMA_EOL && parser->in_pragma)
940 return;
27bf414c
JM
941 if (token->type == CPP_OPEN_BRACE
942 || token->type == CPP_OPEN_PAREN
943 || token->type == CPP_OPEN_SQUARE)
944 ++nesting_depth;
945 else if (token->type == CPP_CLOSE_BRACE
946 || token->type == CPP_CLOSE_PAREN
947 || token->type == CPP_CLOSE_SQUARE)
948 {
949 if (nesting_depth-- == 0)
950 break;
951 }
952 /* Consume this token. */
953 c_parser_consume_token (parser);
954 }
955 parser->error = false;
956}
957
bc4071dd
RH
958/* Expect to be at the end of the pragma directive and consume an
959 end of line marker. */
960
961static void
962c_parser_skip_to_pragma_eol (c_parser *parser)
963{
964 gcc_assert (parser->in_pragma);
965 parser->in_pragma = false;
966
967 if (!c_parser_require (parser, CPP_PRAGMA_EOL, "expected end of line"))
968 while (true)
969 {
970 c_token *token = c_parser_peek_token (parser);
971 if (token->type == CPP_EOF)
972 break;
973 if (token->type == CPP_PRAGMA_EOL)
974 {
975 c_parser_consume_token (parser);
976 break;
977 }
978 c_parser_consume_token (parser);
979 }
980
981 parser->error = false;
982}
27bf414c 983
2a83cc52
RH
984/* Skip tokens until we have consumed an entire block, or until we
985 have consumed a non-nested ';'. */
986
987static void
988c_parser_skip_to_end_of_block_or_statement (c_parser *parser)
989{
990 unsigned nesting_depth = 0;
991 bool save_error = parser->error;
992
993 while (true)
994 {
995 c_token *token;
996
997 /* Peek at the next token. */
998 token = c_parser_peek_token (parser);
999
1000 switch (token->type)
1001 {
1002 case CPP_EOF:
1003 return;
1004
1005 case CPP_PRAGMA_EOL:
1006 if (parser->in_pragma)
1007 return;
1008 break;
1009
1010 case CPP_SEMICOLON:
1011 /* If the next token is a ';', we have reached the
1012 end of the statement. */
1013 if (!nesting_depth)
1014 {
1015 /* Consume the ';'. */
1016 c_parser_consume_token (parser);
1017 goto finished;
1018 }
1019 break;
1020
1021 case CPP_CLOSE_BRACE:
1022 /* If the next token is a non-nested '}', then we have
1023 reached the end of the current block. */
1024 if (nesting_depth == 0 || --nesting_depth == 0)
1025 {
1026 c_parser_consume_token (parser);
1027 goto finished;
1028 }
1029 break;
1030
1031 case CPP_OPEN_BRACE:
1032 /* If it the next token is a '{', then we are entering a new
1033 block. Consume the entire block. */
1034 ++nesting_depth;
1035 break;
1036
1037 case CPP_PRAGMA:
1038 /* If we see a pragma, consume the whole thing at once. We
1039 have some safeguards against consuming pragmas willy-nilly.
1040 Normally, we'd expect to be here with parser->error set,
1041 which disables these safeguards. But it's possible to get
1042 here for secondary error recovery, after parser->error has
1043 been cleared. */
1044 c_parser_consume_pragma (parser);
1045 c_parser_skip_to_pragma_eol (parser);
1046 parser->error = save_error;
1047 continue;
9e33de05
RS
1048
1049 default:
1050 break;
2a83cc52
RH
1051 }
1052
1053 c_parser_consume_token (parser);
1054 }
1055
1056 finished:
1057 parser->error = false;
1058}
1059
d2e796ad
MLI
1060/* CPP's options (initialized by c-opts.c). */
1061extern cpp_options *cpp_opts;
1062
27bf414c
JM
1063/* Save the warning flags which are controlled by __extension__. */
1064
1065static inline int
1066disable_extension_diagnostics (void)
1067{
1068 int ret = (pedantic
1069 | (warn_pointer_arith << 1)
1070 | (warn_traditional << 2)
d2e796ad 1071 | (flag_iso << 3)
24b97832 1072 | (warn_long_long << 4)
b3ab9ea2
TT
1073 | (warn_cxx_compat << 5)
1074 | (warn_overlength_strings << 6));
e3339d0f 1075 cpp_opts->cpp_pedantic = pedantic = 0;
27bf414c 1076 warn_pointer_arith = 0;
e3339d0f 1077 cpp_opts->cpp_warn_traditional = warn_traditional = 0;
27bf414c 1078 flag_iso = 0;
e3339d0f 1079 cpp_opts->cpp_warn_long_long = warn_long_long = 0;
24b97832 1080 warn_cxx_compat = 0;
b3ab9ea2 1081 warn_overlength_strings = 0;
27bf414c
JM
1082 return ret;
1083}
1084
1085/* Restore the warning flags which are controlled by __extension__.
1086 FLAGS is the return value from disable_extension_diagnostics. */
1087
1088static inline void
1089restore_extension_diagnostics (int flags)
1090{
e3339d0f 1091 cpp_opts->cpp_pedantic = pedantic = flags & 1;
27bf414c 1092 warn_pointer_arith = (flags >> 1) & 1;
e3339d0f 1093 cpp_opts->cpp_warn_traditional = warn_traditional = (flags >> 2) & 1;
27bf414c 1094 flag_iso = (flags >> 3) & 1;
e3339d0f 1095 cpp_opts->cpp_warn_long_long = warn_long_long = (flags >> 4) & 1;
24b97832 1096 warn_cxx_compat = (flags >> 5) & 1;
b3ab9ea2 1097 warn_overlength_strings = (flags >> 6) & 1;
27bf414c
JM
1098}
1099
1100/* Possibly kinds of declarator to parse. */
1101typedef enum c_dtr_syn {
1102 /* A normal declarator with an identifier. */
1103 C_DTR_NORMAL,
1104 /* An abstract declarator (maybe empty). */
1105 C_DTR_ABSTRACT,
1106 /* A parameter declarator: may be either, but after a type name does
1107 not redeclare a typedef name as an identifier if it can
1108 alternatively be interpreted as a typedef name; see DR#009,
1109 applied in C90 TC1, omitted from C99 and reapplied in C99 TC2
1110 following DR#249. For example, given a typedef T, "int T" and
1111 "int *T" are valid parameter declarations redeclaring T, while
1112 "int (T)" and "int * (T)" and "int (T[])" and "int (T (int))" are
1113 abstract declarators rather than involving redundant parentheses;
1114 the same applies with attributes inside the parentheses before
1115 "T". */
1116 C_DTR_PARM
1117} c_dtr_syn;
1118
20906c66
JJ
1119/* The binary operation precedence levels, where 0 is a dummy lowest level
1120 used for the bottom of the stack. */
1121enum c_parser_prec {
1122 PREC_NONE,
1123 PREC_LOGOR,
1124 PREC_LOGAND,
1125 PREC_BITOR,
1126 PREC_BITXOR,
1127 PREC_BITAND,
1128 PREC_EQ,
1129 PREC_REL,
1130 PREC_SHIFT,
1131 PREC_ADD,
1132 PREC_MULT,
1133 NUM_PRECS
1134};
1135
27bf414c
JM
1136static void c_parser_external_declaration (c_parser *);
1137static void c_parser_asm_definition (c_parser *);
32912286 1138static void c_parser_declaration_or_fndef (c_parser *, bool, bool, bool,
acf0174b 1139 bool, bool, tree *, vec<c_token>);
32912286
JM
1140static void c_parser_static_assert_declaration_no_semi (c_parser *);
1141static void c_parser_static_assert_declaration (c_parser *);
27bf414c 1142static void c_parser_declspecs (c_parser *, struct c_declspecs *, bool, bool,
38b7bc7f 1143 bool, bool, bool, enum c_lookahead_kind);
27bf414c
JM
1144static struct c_typespec c_parser_enum_specifier (c_parser *);
1145static struct c_typespec c_parser_struct_or_union_specifier (c_parser *);
1146static tree c_parser_struct_declaration (c_parser *);
1147static struct c_typespec c_parser_typeof_specifier (c_parser *);
d19fa6b5 1148static tree c_parser_alignas_specifier (c_parser *);
27bf414c
JM
1149static struct c_declarator *c_parser_declarator (c_parser *, bool, c_dtr_syn,
1150 bool *);
1151static struct c_declarator *c_parser_direct_declarator (c_parser *, bool,
1152 c_dtr_syn, bool *);
1153static struct c_declarator *c_parser_direct_declarator_inner (c_parser *,
1154 bool,
1155 struct c_declarator *);
1156static struct c_arg_info *c_parser_parms_declarator (c_parser *, bool, tree);
a04a722b
JM
1157static struct c_arg_info *c_parser_parms_list_declarator (c_parser *, tree,
1158 tree);
27bf414c
JM
1159static struct c_parm *c_parser_parameter_declaration (c_parser *, tree);
1160static tree c_parser_simple_asm_expr (c_parser *);
1161static tree c_parser_attributes (c_parser *);
1162static struct c_type_name *c_parser_type_name (c_parser *);
1163static struct c_expr c_parser_initializer (c_parser *);
1164static struct c_expr c_parser_braced_init (c_parser *, tree, bool);
a1e3b3d9
LB
1165static void c_parser_initelt (c_parser *, struct obstack *);
1166static void c_parser_initval (c_parser *, struct c_expr *,
1167 struct obstack *);
27bf414c
JM
1168static tree c_parser_compound_statement (c_parser *);
1169static void c_parser_compound_statement_nostart (c_parser *);
1170static void c_parser_label (c_parser *);
1171static void c_parser_statement (c_parser *);
1172static void c_parser_statement_after_labels (c_parser *);
1173static void c_parser_if_statement (c_parser *);
1174static void c_parser_switch_statement (c_parser *);
d4af74d4
TB
1175static void c_parser_while_statement (c_parser *, bool);
1176static void c_parser_do_statement (c_parser *, bool);
8170608b 1177static void c_parser_for_statement (c_parser *, bool);
27bf414c 1178static tree c_parser_asm_statement (c_parser *);
eadd3d0d 1179static tree c_parser_asm_operands (c_parser *);
1c384bf1 1180static tree c_parser_asm_goto_operands (c_parser *);
27bf414c 1181static tree c_parser_asm_clobbers (c_parser *);
acf0174b
JJ
1182static struct c_expr c_parser_expr_no_commas (c_parser *, struct c_expr *,
1183 tree = NULL_TREE);
27bf414c 1184static struct c_expr c_parser_conditional_expression (c_parser *,
acf0174b 1185 struct c_expr *, tree);
20906c66 1186static struct c_expr c_parser_binary_expression (c_parser *, struct c_expr *,
acf0174b 1187 tree);
27bf414c
JM
1188static struct c_expr c_parser_cast_expression (c_parser *, struct c_expr *);
1189static struct c_expr c_parser_unary_expression (c_parser *);
1190static struct c_expr c_parser_sizeof_expression (c_parser *);
1191static struct c_expr c_parser_alignof_expression (c_parser *);
1192static struct c_expr c_parser_postfix_expression (c_parser *);
1193static struct c_expr c_parser_postfix_expression_after_paren_type (c_parser *,
24b97832
ILT
1194 struct c_type_name *,
1195 location_t);
27bf414c 1196static struct c_expr c_parser_postfix_expression_after_primary (c_parser *,
c2255bc4 1197 location_t loc,
27bf414c 1198 struct c_expr);
0a35513e
AH
1199static tree c_parser_transaction (c_parser *, enum rid);
1200static struct c_expr c_parser_transaction_expression (c_parser *, enum rid);
1201static tree c_parser_transaction_cancel (c_parser *);
27bf414c 1202static struct c_expr c_parser_expression (c_parser *);
46bdb9cf 1203static struct c_expr c_parser_expression_conv (c_parser *);
9771b263
DN
1204static vec<tree, va_gc> *c_parser_expr_list (c_parser *, bool, bool,
1205 vec<tree, va_gc> **, location_t *,
81e5eca8 1206 tree *, vec<location_t> *);
953ff289
DN
1207static void c_parser_omp_construct (c_parser *);
1208static void c_parser_omp_threadprivate (c_parser *);
1209static void c_parser_omp_barrier (c_parser *);
1210static void c_parser_omp_flush (c_parser *);
a68ab351 1211static void c_parser_omp_taskwait (c_parser *);
20906c66 1212static void c_parser_omp_taskyield (c_parser *);
acf0174b
JJ
1213static void c_parser_omp_cancel (c_parser *);
1214static void c_parser_omp_cancellation_point (c_parser *);
27bf414c 1215
acf0174b
JJ
1216enum pragma_context { pragma_external, pragma_struct, pragma_param,
1217 pragma_stmt, pragma_compound };
bc4071dd 1218static bool c_parser_pragma (c_parser *, enum pragma_context);
acf0174b
JJ
1219static bool c_parser_omp_target (c_parser *, enum pragma_context);
1220static void c_parser_omp_end_declare_target (c_parser *);
1221static void c_parser_omp_declare (c_parser *, enum pragma_context);
bc4071dd 1222
27bf414c
JM
1223/* These Objective-C parser functions are only ever called when
1224 compiling Objective-C. */
c165dca7 1225static void c_parser_objc_class_definition (c_parser *, tree);
27bf414c
JM
1226static void c_parser_objc_class_instance_variables (c_parser *);
1227static void c_parser_objc_class_declaration (c_parser *);
1228static void c_parser_objc_alias_declaration (c_parser *);
c165dca7 1229static void c_parser_objc_protocol_definition (c_parser *, tree);
249a82c4 1230static bool c_parser_objc_method_type (c_parser *);
27bf414c
JM
1231static void c_parser_objc_method_definition (c_parser *);
1232static void c_parser_objc_methodprotolist (c_parser *);
1233static void c_parser_objc_methodproto (c_parser *);
a04a722b 1234static tree c_parser_objc_method_decl (c_parser *, bool, tree *, tree *);
27bf414c
JM
1235static tree c_parser_objc_type_name (c_parser *);
1236static tree c_parser_objc_protocol_refs (c_parser *);
437c2322 1237static void c_parser_objc_try_catch_finally_statement (c_parser *);
27bf414c
JM
1238static void c_parser_objc_synchronized_statement (c_parser *);
1239static tree c_parser_objc_selector (c_parser *);
1240static tree c_parser_objc_selector_arg (c_parser *);
1241static tree c_parser_objc_receiver (c_parser *);
1242static tree c_parser_objc_message_args (c_parser *);
1243static tree c_parser_objc_keywordexpr (c_parser *);
f614132b 1244static void c_parser_objc_at_property_declaration (c_parser *);
da57d1b9
NP
1245static void c_parser_objc_at_synthesize_declaration (c_parser *);
1246static void c_parser_objc_at_dynamic_declaration (c_parser *);
668ea4b1 1247static bool c_parser_objc_diagnose_bad_element_prefix
c165dca7 1248 (c_parser *, struct c_declspecs *);
27bf414c 1249
c02065fc
AH
1250/* Cilk Plus supporting routines. */
1251static void c_parser_cilk_simd (c_parser *);
1252static bool c_parser_cilk_verify_simd (c_parser *, enum pragma_context);
36536d79 1253static tree c_parser_array_notation (location_t, c_parser *, tree, tree);
41958c28 1254static tree c_parser_cilk_clause_vectorlength (c_parser *, tree, bool);
36536d79 1255
27bf414c
JM
1256/* Parse a translation unit (C90 6.7, C99 6.9).
1257
1258 translation-unit:
1259 external-declarations
1260
1261 external-declarations:
1262 external-declaration
1263 external-declarations external-declaration
1264
1265 GNU extensions:
1266
1267 translation-unit:
1268 empty
1269*/
1270
1271static void
1272c_parser_translation_unit (c_parser *parser)
1273{
1274 if (c_parser_next_token_is (parser, CPP_EOF))
1275 {
c1771a20 1276 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
509c9d60 1277 "ISO C forbids an empty translation unit");
27bf414c
JM
1278 }
1279 else
1280 {
1281 void *obstack_position = obstack_alloc (&parser_obstack, 0);
6ec637a4 1282 mark_valid_location_for_stdc_pragma (false);
27bf414c
JM
1283 do
1284 {
1285 ggc_collect ();
1286 c_parser_external_declaration (parser);
1287 obstack_free (&parser_obstack, obstack_position);
1288 }
1289 while (c_parser_next_token_is_not (parser, CPP_EOF));
1290 }
1291}
1292
1293/* Parse an external declaration (C90 6.7, C99 6.9).
1294
1295 external-declaration:
1296 function-definition
1297 declaration
1298
1299 GNU extensions:
1300
1301 external-declaration:
1302 asm-definition
1303 ;
1304 __extension__ external-declaration
1305
1306 Objective-C:
1307
1308 external-declaration:
1309 objc-class-definition
1310 objc-class-declaration
1311 objc-alias-declaration
1312 objc-protocol-definition
1313 objc-method-definition
1314 @end
1315*/
1316
1317static void
1318c_parser_external_declaration (c_parser *parser)
1319{
1320 int ext;
1321 switch (c_parser_peek_token (parser)->type)
1322 {
1323 case CPP_KEYWORD:
1324 switch (c_parser_peek_token (parser)->keyword)
1325 {
1326 case RID_EXTENSION:
1327 ext = disable_extension_diagnostics ();
1328 c_parser_consume_token (parser);
1329 c_parser_external_declaration (parser);
1330 restore_extension_diagnostics (ext);
1331 break;
1332 case RID_ASM:
1333 c_parser_asm_definition (parser);
1334 break;
1335 case RID_AT_INTERFACE:
1336 case RID_AT_IMPLEMENTATION:
1337 gcc_assert (c_dialect_objc ());
c165dca7 1338 c_parser_objc_class_definition (parser, NULL_TREE);
27bf414c 1339 break;
49b91f05 1340 case RID_AT_CLASS:
27bf414c
JM
1341 gcc_assert (c_dialect_objc ());
1342 c_parser_objc_class_declaration (parser);
1343 break;
1344 case RID_AT_ALIAS:
1345 gcc_assert (c_dialect_objc ());
1346 c_parser_objc_alias_declaration (parser);
1347 break;
1348 case RID_AT_PROTOCOL:
1349 gcc_assert (c_dialect_objc ());
c165dca7 1350 c_parser_objc_protocol_definition (parser, NULL_TREE);
27bf414c 1351 break;
668ea4b1
IS
1352 case RID_AT_PROPERTY:
1353 gcc_assert (c_dialect_objc ());
f614132b 1354 c_parser_objc_at_property_declaration (parser);
668ea4b1 1355 break;
da57d1b9
NP
1356 case RID_AT_SYNTHESIZE:
1357 gcc_assert (c_dialect_objc ());
1358 c_parser_objc_at_synthesize_declaration (parser);
1359 break;
1360 case RID_AT_DYNAMIC:
1361 gcc_assert (c_dialect_objc ());
1362 c_parser_objc_at_dynamic_declaration (parser);
1363 break;
27bf414c
JM
1364 case RID_AT_END:
1365 gcc_assert (c_dialect_objc ());
1366 c_parser_consume_token (parser);
1367 objc_finish_implementation ();
1368 break;
1369 default:
1370 goto decl_or_fndef;
1371 }
1372 break;
1373 case CPP_SEMICOLON:
c1771a20 1374 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
509c9d60 1375 "ISO C does not allow extra %<;%> outside of a function");
27bf414c
JM
1376 c_parser_consume_token (parser);
1377 break;
bc4071dd 1378 case CPP_PRAGMA:
6ec637a4 1379 mark_valid_location_for_stdc_pragma (true);
bc4071dd 1380 c_parser_pragma (parser, pragma_external);
6ec637a4 1381 mark_valid_location_for_stdc_pragma (false);
bc4071dd 1382 break;
27bf414c
JM
1383 case CPP_PLUS:
1384 case CPP_MINUS:
1385 if (c_dialect_objc ())
1386 {
1387 c_parser_objc_method_definition (parser);
1388 break;
1389 }
1390 /* Else fall through, and yield a syntax error trying to parse
1391 as a declaration or function definition. */
1392 default:
1393 decl_or_fndef:
c165dca7
IS
1394 /* A declaration or a function definition (or, in Objective-C,
1395 an @interface or @protocol with prefix attributes). We can
1396 only tell which after parsing the declaration specifiers, if
1397 any, and the first declarator. */
acf0174b
JJ
1398 c_parser_declaration_or_fndef (parser, true, true, true, false, true,
1399 NULL, vNULL);
27bf414c
JM
1400 break;
1401 }
1402}
1403
acf0174b
JJ
1404static void c_finish_omp_declare_simd (c_parser *, tree, tree, vec<c_token>);
1405
27bf414c
JM
1406/* Parse a declaration or function definition (C90 6.5, 6.7.1, C99
1407 6.7, 6.9.1). If FNDEF_OK is true, a function definition is
1408 accepted; otherwise (old-style parameter declarations) only other
32912286
JM
1409 declarations are accepted. If STATIC_ASSERT_OK is true, a static
1410 assertion is accepted; otherwise (old-style parameter declarations)
1411 it is not. If NESTED is true, we are inside a function or parsing
1412 old-style parameter declarations; any functions encountered are
1413 nested functions and declaration specifiers are required; otherwise
1414 we are at top level and functions are normal functions and
1415 declaration specifiers may be optional. If EMPTY_OK is true, empty
1416 declarations are OK (subject to all other constraints); otherwise
1417 (old-style parameter declarations) they are diagnosed. If
1418 START_ATTR_OK is true, the declaration specifiers may start with
1419 attributes; otherwise they may not.
f05b9d93
NP
1420 OBJC_FOREACH_OBJECT_DECLARATION can be used to get back the parsed
1421 declaration when parsing an Objective-C foreach statement.
27bf414c
JM
1422
1423 declaration:
1424 declaration-specifiers init-declarator-list[opt] ;
32912286 1425 static_assert-declaration
27bf414c
JM
1426
1427 function-definition:
1428 declaration-specifiers[opt] declarator declaration-list[opt]
1429 compound-statement
1430
1431 declaration-list:
1432 declaration
1433 declaration-list declaration
1434
1435 init-declarator-list:
1436 init-declarator
1437 init-declarator-list , init-declarator
1438
1439 init-declarator:
1440 declarator simple-asm-expr[opt] attributes[opt]
1441 declarator simple-asm-expr[opt] attributes[opt] = initializer
1442
1443 GNU extensions:
1444
1445 nested-function-definition:
1446 declaration-specifiers declarator declaration-list[opt]
1447 compound-statement
1448
c165dca7
IS
1449 Objective-C:
1450 attributes objc-class-definition
1451 attributes objc-category-definition
1452 attributes objc-protocol-definition
1453
27bf414c
JM
1454 The simple-asm-expr and attributes are GNU extensions.
1455
1456 This function does not handle __extension__; that is handled in its
1457 callers. ??? Following the old parser, __extension__ may start
1458 external declarations, declarations in functions and declarations
1459 at the start of "for" loops, but not old-style parameter
1460 declarations.
1461
1462 C99 requires declaration specifiers in a function definition; the
1463 absence is diagnosed through the diagnosis of implicit int. In GNU
1464 C we also allow but diagnose declarations without declaration
1465 specifiers, but only at top level (elsewhere they conflict with
953ff289 1466 other syntax).
b8698a0f 1467
f05b9d93
NP
1468 In Objective-C, declarations of the looping variable in a foreach
1469 statement are exceptionally terminated by 'in' (for example, 'for
1470 (NSObject *object in array) { ... }').
1471
953ff289 1472 OpenMP:
b8698a0f 1473
953ff289
DN
1474 declaration:
1475 threadprivate-directive */
27bf414c
JM
1476
1477static void
32912286
JM
1478c_parser_declaration_or_fndef (c_parser *parser, bool fndef_ok,
1479 bool static_assert_ok, bool empty_ok,
f05b9d93 1480 bool nested, bool start_attr_ok,
acf0174b
JJ
1481 tree *objc_foreach_object_declaration,
1482 vec<c_token> omp_declare_simd_clauses)
27bf414c
JM
1483{
1484 struct c_declspecs *specs;
1485 tree prefix_attrs;
1486 tree all_prefix_attrs;
1487 bool diagnosed_no_specs = false;
c7412148 1488 location_t here = c_parser_peek_token (parser)->location;
bc4071dd 1489
32912286
JM
1490 if (static_assert_ok
1491 && c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT))
1492 {
1493 c_parser_static_assert_declaration (parser);
1494 return;
1495 }
27bf414c 1496 specs = build_null_declspecs ();
2f413185
PB
1497
1498 /* Try to detect an unknown type name when we have "A B" or "A *B". */
1499 if (c_parser_peek_token (parser)->type == CPP_NAME
1500 && c_parser_peek_token (parser)->id_kind == C_ID_ID
1501 && (c_parser_peek_2nd_token (parser)->type == CPP_NAME
1502 || c_parser_peek_2nd_token (parser)->type == CPP_MULT)
1503 && (!nested || !lookup_name (c_parser_peek_token (parser)->value)))
1504 {
1505 error_at (here, "unknown type name %qE",
1506 c_parser_peek_token (parser)->value);
1507
1508 /* Parse declspecs normally to get a correct pointer type, but avoid
a5812bdc
PB
1509 a further "fails to be a type name" error. Refuse nested functions
1510 since it is not how the user likely wants us to recover. */
2f413185
PB
1511 c_parser_peek_token (parser)->type = CPP_KEYWORD;
1512 c_parser_peek_token (parser)->keyword = RID_VOID;
1513 c_parser_peek_token (parser)->value = error_mark_node;
a5812bdc 1514 fndef_ok = !nested;
2f413185
PB
1515 }
1516
568a31f2 1517 c_parser_declspecs (parser, specs, true, true, start_attr_ok,
38b7bc7f 1518 true, true, cla_nonabstract_decl);
27bf414c
JM
1519 if (parser->error)
1520 {
1521 c_parser_skip_to_end_of_block_or_statement (parser);
1522 return;
1523 }
1524 if (nested && !specs->declspecs_seen_p)
1525 {
1526 c_parser_error (parser, "expected declaration specifiers");
1527 c_parser_skip_to_end_of_block_or_statement (parser);
1528 return;
1529 }
1530 finish_declspecs (specs);
38b7bc7f 1531 bool auto_type_p = specs->typespec_word == cts_auto_type;
27bf414c
JM
1532 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1533 {
38b7bc7f
JM
1534 if (auto_type_p)
1535 error_at (here, "%<__auto_type%> in empty declaration");
1536 else if (empty_ok)
27bf414c
JM
1537 shadow_tag (specs);
1538 else
1539 {
1540 shadow_tag_warned (specs, 1);
509c9d60 1541 pedwarn (here, 0, "empty declaration");
27bf414c
JM
1542 }
1543 c_parser_consume_token (parser);
1544 return;
1545 }
f725e721
PB
1546
1547 /* Provide better error recovery. Note that a type name here is usually
1548 better diagnosed as a redeclaration. */
1549 if (empty_ok
1550 && specs->typespec_kind == ctsk_tagdef
1551 && c_parser_next_token_starts_declspecs (parser)
1552 && !c_parser_next_token_is (parser, CPP_NAME))
1553 {
1554 c_parser_error (parser, "expected %<;%>, identifier or %<(%>");
1555 parser->error = false;
1556 shadow_tag_warned (specs, 1);
1557 return;
1558 }
38b7bc7f 1559 else if (c_dialect_objc () && !auto_type_p)
c165dca7 1560 {
f7e71da5
IS
1561 /* Prefix attributes are an error on method decls. */
1562 switch (c_parser_peek_token (parser)->type)
1563 {
1564 case CPP_PLUS:
1565 case CPP_MINUS:
1566 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1567 return;
1568 if (specs->attrs)
1569 {
1570 warning_at (c_parser_peek_token (parser)->location,
1571 OPT_Wattributes,
1572 "prefix attributes are ignored for methods");
1573 specs->attrs = NULL_TREE;
1574 }
1575 if (fndef_ok)
1576 c_parser_objc_method_definition (parser);
1577 else
1578 c_parser_objc_methodproto (parser);
1579 return;
1580 break;
1581 default:
1582 break;
1583 }
c165dca7
IS
1584 /* This is where we parse 'attributes @interface ...',
1585 'attributes @implementation ...', 'attributes @protocol ...'
1586 (where attributes could be, for example, __attribute__
1587 ((deprecated)).
1588 */
1589 switch (c_parser_peek_token (parser)->keyword)
1590 {
1591 case RID_AT_INTERFACE:
1592 {
1593 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1594 return;
1595 c_parser_objc_class_definition (parser, specs->attrs);
1596 return;
1597 }
1598 break;
1599 case RID_AT_IMPLEMENTATION:
1600 {
1601 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1602 return;
1603 if (specs->attrs)
1604 {
1605 warning_at (c_parser_peek_token (parser)->location,
1606 OPT_Wattributes,
1607 "prefix attributes are ignored for implementations");
1608 specs->attrs = NULL_TREE;
1609 }
1610 c_parser_objc_class_definition (parser, NULL_TREE);
1611 return;
1612 }
1613 break;
1614 case RID_AT_PROTOCOL:
1615 {
1616 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1617 return;
1618 c_parser_objc_protocol_definition (parser, specs->attrs);
1619 return;
1620 }
1621 break;
668ea4b1
IS
1622 case RID_AT_ALIAS:
1623 case RID_AT_CLASS:
1624 case RID_AT_END:
1625 case RID_AT_PROPERTY:
1626 if (specs->attrs)
1627 {
96bbfbac 1628 c_parser_error (parser, "unexpected attribute");
668ea4b1
IS
1629 specs->attrs = NULL;
1630 }
1631 break;
c165dca7
IS
1632 default:
1633 break;
1634 }
1635 }
1636
27bf414c
JM
1637 pending_xref_error ();
1638 prefix_attrs = specs->attrs;
1639 all_prefix_attrs = prefix_attrs;
1640 specs->attrs = NULL_TREE;
1641 while (true)
1642 {
1643 struct c_declarator *declarator;
1644 bool dummy = false;
575bfb00 1645 timevar_id_t tv;
27bf414c
JM
1646 tree fnbody;
1647 /* Declaring either one or more declarators (in which case we
1648 should diagnose if there were no declaration specifiers) or a
1649 function definition (in which case the diagnostic for
1650 implicit int suffices). */
9e5b2115
PB
1651 declarator = c_parser_declarator (parser,
1652 specs->typespec_kind != ctsk_none,
27bf414c
JM
1653 C_DTR_NORMAL, &dummy);
1654 if (declarator == NULL)
1655 {
41958c28
BI
1656 if (omp_declare_simd_clauses.exists ()
1657 || !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
acf0174b
JJ
1658 c_finish_omp_declare_simd (parser, NULL_TREE, NULL_TREE,
1659 omp_declare_simd_clauses);
27bf414c
JM
1660 c_parser_skip_to_end_of_block_or_statement (parser);
1661 return;
1662 }
38b7bc7f
JM
1663 if (auto_type_p && declarator->kind != cdk_id)
1664 {
1665 error_at (here,
1666 "%<__auto_type%> requires a plain identifier"
1667 " as declarator");
1668 c_parser_skip_to_end_of_block_or_statement (parser);
1669 return;
1670 }
27bf414c
JM
1671 if (c_parser_next_token_is (parser, CPP_EQ)
1672 || c_parser_next_token_is (parser, CPP_COMMA)
1673 || c_parser_next_token_is (parser, CPP_SEMICOLON)
1674 || c_parser_next_token_is_keyword (parser, RID_ASM)
f05b9d93
NP
1675 || c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE)
1676 || c_parser_next_token_is_keyword (parser, RID_IN))
27bf414c
JM
1677 {
1678 tree asm_name = NULL_TREE;
1679 tree postfix_attrs = NULL_TREE;
1680 if (!diagnosed_no_specs && !specs->declspecs_seen_p)
1681 {
1682 diagnosed_no_specs = true;
509c9d60 1683 pedwarn (here, 0, "data definition has no type or storage class");
27bf414c
JM
1684 }
1685 /* Having seen a data definition, there cannot now be a
1686 function definition. */
1687 fndef_ok = false;
1688 if (c_parser_next_token_is_keyword (parser, RID_ASM))
1689 asm_name = c_parser_simple_asm_expr (parser);
1690 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
1691 postfix_attrs = c_parser_attributes (parser);
1692 if (c_parser_next_token_is (parser, CPP_EQ))
1693 {
1694 tree d;
1695 struct c_expr init;
c2255bc4 1696 location_t init_loc;
27bf414c 1697 c_parser_consume_token (parser);
38b7bc7f
JM
1698 if (auto_type_p)
1699 {
1700 start_init (NULL_TREE, asm_name, global_bindings_p ());
1701 init_loc = c_parser_peek_token (parser)->location;
1702 init = c_parser_expr_no_commas (parser, NULL);
1703 if (TREE_CODE (init.value) == COMPONENT_REF
1704 && DECL_C_BIT_FIELD (TREE_OPERAND (init.value, 1)))
1705 error_at (here,
1706 "%<__auto_type%> used with a bit-field"
1707 " initializer");
1708 init = convert_lvalue_to_rvalue (init_loc, init, true, true);
1709 tree init_type = TREE_TYPE (init.value);
1710 /* As with typeof, remove _Atomic and const
1711 qualifiers from atomic types. */
1712 if (init_type != error_mark_node && TYPE_ATOMIC (init_type))
1713 init_type
1714 = c_build_qualified_type (init_type,
1715 (TYPE_QUALS (init_type)
1716 & ~(TYPE_QUAL_ATOMIC
1717 | TYPE_QUAL_CONST)));
1718 bool vm_type = variably_modified_type_p (init_type,
1719 NULL_TREE);
1720 if (vm_type)
1721 init.value = c_save_expr (init.value);
1722 finish_init ();
1723 specs->typespec_kind = ctsk_typeof;
1724 specs->locations[cdw_typedef] = init_loc;
1725 specs->typedef_p = true;
1726 specs->type = init_type;
1727 if (vm_type)
1728 {
1729 bool maybe_const = true;
1730 tree type_expr = c_fully_fold (init.value, false,
1731 &maybe_const);
1732 specs->expr_const_operands &= maybe_const;
1733 if (specs->expr)
1734 specs->expr = build2 (COMPOUND_EXPR,
1735 TREE_TYPE (type_expr),
1736 specs->expr, type_expr);
1737 else
1738 specs->expr = type_expr;
1739 }
1740 d = start_decl (declarator, specs, true,
1741 chainon (postfix_attrs, all_prefix_attrs));
1742 if (!d)
1743 d = error_mark_node;
41958c28
BI
1744 if (omp_declare_simd_clauses.exists ()
1745 || !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
38b7bc7f
JM
1746 c_finish_omp_declare_simd (parser, d, NULL_TREE,
1747 omp_declare_simd_clauses);
1748 }
1749 else
1750 {
1751 /* The declaration of the variable is in effect while
1752 its initializer is parsed. */
1753 d = start_decl (declarator, specs, true,
1754 chainon (postfix_attrs, all_prefix_attrs));
1755 if (!d)
1756 d = error_mark_node;
41958c28
BI
1757 if (omp_declare_simd_clauses.exists ()
1758 || !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
38b7bc7f
JM
1759 c_finish_omp_declare_simd (parser, d, NULL_TREE,
1760 omp_declare_simd_clauses);
1761 start_init (d, asm_name, global_bindings_p ());
1762 init_loc = c_parser_peek_token (parser)->location;
1763 init = c_parser_initializer (parser);
1764 finish_init ();
1765 }
27bf414c
JM
1766 if (d != error_mark_node)
1767 {
1768 maybe_warn_string_init (TREE_TYPE (d), init);
c2255bc4
AH
1769 finish_decl (d, init_loc, init.value,
1770 init.original_type, asm_name);
27bf414c
JM
1771 }
1772 }
1773 else
1774 {
38b7bc7f
JM
1775 if (auto_type_p)
1776 {
1777 error_at (here,
1778 "%<__auto_type%> requires an initialized "
1779 "data declaration");
1780 c_parser_skip_to_end_of_block_or_statement (parser);
1781 return;
1782 }
27bf414c
JM
1783 tree d = start_decl (declarator, specs, false,
1784 chainon (postfix_attrs,
1785 all_prefix_attrs));
41958c28
BI
1786 if (omp_declare_simd_clauses.exists ()
1787 || !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
acf0174b
JJ
1788 {
1789 tree parms = NULL_TREE;
1790 if (d && TREE_CODE (d) == FUNCTION_DECL)
1791 {
1792 struct c_declarator *ce = declarator;
1793 while (ce != NULL)
1794 if (ce->kind == cdk_function)
1795 {
1796 parms = ce->u.arg_info->parms;
1797 break;
1798 }
1799 else
1800 ce = ce->declarator;
1801 }
1802 if (parms)
1803 temp_store_parm_decls (d, parms);
1804 c_finish_omp_declare_simd (parser, d, parms,
1805 omp_declare_simd_clauses);
1806 if (parms)
1807 temp_pop_parm_decls ();
1808 }
27bf414c 1809 if (d)
c2255bc4 1810 finish_decl (d, UNKNOWN_LOCATION, NULL_TREE,
f05b9d93
NP
1811 NULL_TREE, asm_name);
1812
1813 if (c_parser_next_token_is_keyword (parser, RID_IN))
1814 {
1815 if (d)
1816 *objc_foreach_object_declaration = d;
1817 else
1818 *objc_foreach_object_declaration = error_mark_node;
1819 }
27bf414c
JM
1820 }
1821 if (c_parser_next_token_is (parser, CPP_COMMA))
1822 {
38b7bc7f
JM
1823 if (auto_type_p)
1824 {
1825 error_at (here,
1826 "%<__auto_type%> may only be used with"
1827 " a single declarator");
1828 c_parser_skip_to_end_of_block_or_statement (parser);
1829 return;
1830 }
27bf414c
JM
1831 c_parser_consume_token (parser);
1832 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
1833 all_prefix_attrs = chainon (c_parser_attributes (parser),
1834 prefix_attrs);
1835 else
1836 all_prefix_attrs = prefix_attrs;
1837 continue;
1838 }
1839 else if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1840 {
1841 c_parser_consume_token (parser);
1842 return;
1843 }
f05b9d93
NP
1844 else if (c_parser_next_token_is_keyword (parser, RID_IN))
1845 {
1846 /* This can only happen in Objective-C: we found the
1847 'in' that terminates the declaration inside an
1848 Objective-C foreach statement. Do not consume the
1849 token, so that the caller can use it to determine
1850 that this indeed is a foreach context. */
1851 return;
1852 }
27bf414c
JM
1853 else
1854 {
1855 c_parser_error (parser, "expected %<,%> or %<;%>");
1856 c_parser_skip_to_end_of_block_or_statement (parser);
1857 return;
1858 }
1859 }
38b7bc7f
JM
1860 else if (auto_type_p)
1861 {
1862 error_at (here,
1863 "%<__auto_type%> requires an initialized data declaration");
1864 c_parser_skip_to_end_of_block_or_statement (parser);
1865 return;
1866 }
27bf414c
JM
1867 else if (!fndef_ok)
1868 {
1869 c_parser_error (parser, "expected %<=%>, %<,%>, %<;%>, "
1870 "%<asm%> or %<__attribute__%>");
1871 c_parser_skip_to_end_of_block_or_statement (parser);
1872 return;
1873 }
1874 /* Function definition (nested or otherwise). */
1875 if (nested)
1876 {
c1771a20 1877 pedwarn (here, OPT_Wpedantic, "ISO C forbids nested functions");
d2784db4 1878 c_push_function_context ();
27bf414c
JM
1879 }
1880 if (!start_function (specs, declarator, all_prefix_attrs))
1881 {
1882 /* This can appear in many cases looking nothing like a
1883 function definition, so we don't give a more specific
1884 error suggesting there was one. */
1885 c_parser_error (parser, "expected %<=%>, %<,%>, %<;%>, %<asm%> "
1886 "or %<__attribute__%>");
1887 if (nested)
d2784db4 1888 c_pop_function_context ();
27bf414c
JM
1889 break;
1890 }
575bfb00
LC
1891
1892 if (DECL_DECLARED_INLINE_P (current_function_decl))
1893 tv = TV_PARSE_INLINE;
1894 else
1895 tv = TV_PARSE_FUNC;
1896 timevar_push (tv);
1897
27bf414c
JM
1898 /* Parse old-style parameter declarations. ??? Attributes are
1899 not allowed to start declaration specifiers here because of a
1900 syntax conflict between a function declaration with attribute
1901 suffix and a function definition with an attribute prefix on
1902 first old-style parameter declaration. Following the old
1903 parser, they are not accepted on subsequent old-style
1904 parameter declarations either. However, there is no
1905 ambiguity after the first declaration, nor indeed on the
1906 first as long as we don't allow postfix attributes after a
1907 declarator with a nonempty identifier list in a definition;
1908 and postfix attributes have never been accepted here in
1909 function definitions either. */
1910 while (c_parser_next_token_is_not (parser, CPP_EOF)
1911 && c_parser_next_token_is_not (parser, CPP_OPEN_BRACE))
32912286 1912 c_parser_declaration_or_fndef (parser, false, false, false,
acf0174b 1913 true, false, NULL, vNULL);
27bf414c 1914 store_parm_decls ();
41958c28
BI
1915 if (omp_declare_simd_clauses.exists ()
1916 || !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
acf0174b
JJ
1917 c_finish_omp_declare_simd (parser, current_function_decl, NULL_TREE,
1918 omp_declare_simd_clauses);
1751ecd6
AH
1919 DECL_STRUCT_FUNCTION (current_function_decl)->function_start_locus
1920 = c_parser_peek_token (parser)->location;
27bf414c 1921 fnbody = c_parser_compound_statement (parser);
b72271b9 1922 if (flag_cilkplus && contains_array_notation_expr (fnbody))
25c22937 1923 fnbody = expand_array_notation_exprs (fnbody);
27bf414c
JM
1924 if (nested)
1925 {
1926 tree decl = current_function_decl;
9f62cb92
JJ
1927 /* Mark nested functions as needing static-chain initially.
1928 lower_nested_functions will recompute it but the
1929 DECL_STATIC_CHAIN flag is also used before that happens,
1930 by initializer_constant_valid_p. See gcc.dg/nested-fn-2.c. */
1931 DECL_STATIC_CHAIN (decl) = 1;
27bf414c
JM
1932 add_stmt (fnbody);
1933 finish_function ();
d2784db4 1934 c_pop_function_context ();
c2255bc4 1935 add_stmt (build_stmt (DECL_SOURCE_LOCATION (decl), DECL_EXPR, decl));
27bf414c
JM
1936 }
1937 else
1938 {
1939 add_stmt (fnbody);
1940 finish_function ();
1941 }
575bfb00
LC
1942
1943 timevar_pop (tv);
27bf414c
JM
1944 break;
1945 }
1946}
1947
1948/* Parse an asm-definition (asm() outside a function body). This is a
1949 GNU extension.
1950
1951 asm-definition:
1952 simple-asm-expr ;
1953*/
1954
1955static void
1956c_parser_asm_definition (c_parser *parser)
1957{
1958 tree asm_str = c_parser_simple_asm_expr (parser);
27bf414c 1959 if (asm_str)
65d630d4 1960 add_asm_node (asm_str);
27bf414c
JM
1961 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
1962}
1963
48b0b196 1964/* Parse a static assertion (C11 6.7.10).
32912286
JM
1965
1966 static_assert-declaration:
1967 static_assert-declaration-no-semi ;
1968*/
1969
1970static void
1971c_parser_static_assert_declaration (c_parser *parser)
1972{
1973 c_parser_static_assert_declaration_no_semi (parser);
1974 if (parser->error
1975 || !c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
1976 c_parser_skip_to_end_of_block_or_statement (parser);
1977}
1978
48b0b196 1979/* Parse a static assertion (C11 6.7.10), without the trailing
32912286
JM
1980 semicolon.
1981
1982 static_assert-declaration-no-semi:
1983 _Static_assert ( constant-expression , string-literal )
1984*/
1985
1986static void
1987c_parser_static_assert_declaration_no_semi (c_parser *parser)
1988{
1989 location_t assert_loc, value_loc;
1990 tree value;
1991 tree string;
1992
1993 gcc_assert (c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT));
1994 assert_loc = c_parser_peek_token (parser)->location;
48b0b196 1995 if (!flag_isoc11)
32912286
JM
1996 {
1997 if (flag_isoc99)
c1771a20 1998 pedwarn (assert_loc, OPT_Wpedantic,
32912286
JM
1999 "ISO C99 does not support %<_Static_assert%>");
2000 else
c1771a20 2001 pedwarn (assert_loc, OPT_Wpedantic,
32912286
JM
2002 "ISO C90 does not support %<_Static_assert%>");
2003 }
2004 c_parser_consume_token (parser);
2005 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2006 return;
2007 value_loc = c_parser_peek_token (parser)->location;
2008 value = c_parser_expr_no_commas (parser, NULL).value;
2009 parser->lex_untranslated_string = true;
2010 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
2011 {
2012 parser->lex_untranslated_string = false;
2013 return;
2014 }
2015 switch (c_parser_peek_token (parser)->type)
2016 {
2017 case CPP_STRING:
2018 case CPP_STRING16:
2019 case CPP_STRING32:
2020 case CPP_WSTRING:
2021 case CPP_UTF8STRING:
2022 string = c_parser_peek_token (parser)->value;
2023 c_parser_consume_token (parser);
2024 parser->lex_untranslated_string = false;
2025 break;
2026 default:
2027 c_parser_error (parser, "expected string literal");
2028 parser->lex_untranslated_string = false;
2029 return;
2030 }
2031 c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
2032
2033 if (!INTEGRAL_TYPE_P (TREE_TYPE (value)))
2034 {
2035 error_at (value_loc, "expression in static assertion is not an integer");
2036 return;
2037 }
2038 if (TREE_CODE (value) != INTEGER_CST)
2039 {
2040 value = c_fully_fold (value, false, NULL);
2041 if (TREE_CODE (value) == INTEGER_CST)
c1771a20 2042 pedwarn (value_loc, OPT_Wpedantic, "expression in static assertion "
32912286
JM
2043 "is not an integer constant expression");
2044 }
2045 if (TREE_CODE (value) != INTEGER_CST)
2046 {
2047 error_at (value_loc, "expression in static assertion is not constant");
2048 return;
2049 }
2050 constant_expression_warning (value);
2051 if (integer_zerop (value))
2052 error_at (assert_loc, "static assertion failed: %E", string);
2053}
2054
27bf414c
JM
2055/* Parse some declaration specifiers (possibly none) (C90 6.5, C99
2056 6.7), adding them to SPECS (which may already include some).
2057 Storage class specifiers are accepted iff SCSPEC_OK; type
568a31f2
MP
2058 specifiers are accepted iff TYPESPEC_OK; alignment specifiers are
2059 accepted iff ALIGNSPEC_OK; attributes are accepted at the start
38b7bc7f 2060 iff START_ATTR_OK; __auto_type is accepted iff AUTO_TYPE_OK.
27bf414c
JM
2061
2062 declaration-specifiers:
2063 storage-class-specifier declaration-specifiers[opt]
2064 type-specifier declaration-specifiers[opt]
2065 type-qualifier declaration-specifiers[opt]
2066 function-specifier declaration-specifiers[opt]
d19fa6b5 2067 alignment-specifier declaration-specifiers[opt]
27bf414c
JM
2068
2069 Function specifiers (inline) are from C99, and are currently
d19fa6b5 2070 handled as storage class specifiers, as is __thread. Alignment
48b0b196 2071 specifiers are from C11.
27bf414c
JM
2072
2073 C90 6.5.1, C99 6.7.1:
2074 storage-class-specifier:
2075 typedef
2076 extern
2077 static
2078 auto
2079 register
582d9b50
JM
2080 _Thread_local
2081
2082 (_Thread_local is new in C11.)
27bf414c
JM
2083
2084 C99 6.7.4:
2085 function-specifier:
2086 inline
c4b3a0a0
JM
2087 _Noreturn
2088
48b0b196 2089 (_Noreturn is new in C11.)
27bf414c
JM
2090
2091 C90 6.5.2, C99 6.7.2:
2092 type-specifier:
2093 void
2094 char
2095 short
2096 int
2097 long
2098 float
2099 double
2100 signed
2101 unsigned
2102 _Bool
2103 _Complex
2104 [_Imaginary removed in C99 TC2]
2105 struct-or-union-specifier
2106 enum-specifier
2107 typedef-name
267bac10 2108 atomic-type-specifier
27bf414c
JM
2109
2110 (_Bool and _Complex are new in C99.)
267bac10 2111 (atomic-type-specifier is new in C11.)
27bf414c
JM
2112
2113 C90 6.5.3, C99 6.7.3:
2114
2115 type-qualifier:
2116 const
2117 restrict
2118 volatile
36c5e70a 2119 address-space-qualifier
267bac10 2120 _Atomic
27bf414c
JM
2121
2122 (restrict is new in C99.)
267bac10 2123 (_Atomic is new in C11.)
27bf414c
JM
2124
2125 GNU extensions:
2126
2127 declaration-specifiers:
2128 attributes declaration-specifiers[opt]
2129
36c5e70a
BE
2130 type-qualifier:
2131 address-space
2132
2133 address-space:
2134 identifier recognized by the target
2135
27bf414c
JM
2136 storage-class-specifier:
2137 __thread
2138
2139 type-specifier:
2140 typeof-specifier
38b7bc7f 2141 __auto_type
a6766312 2142 __int128
9a8ce21f
JG
2143 _Decimal32
2144 _Decimal64
2145 _Decimal128
ab22c1fa
CF
2146 _Fract
2147 _Accum
2148 _Sat
2149
2150 (_Fract, _Accum, and _Sat are new from ISO/IEC DTR 18037:
2151 http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1169.pdf)
27bf414c 2152
267bac10
JM
2153 atomic-type-specifier
2154 _Atomic ( type-name )
2155
27bf414c
JM
2156 Objective-C:
2157
2158 type-specifier:
2159 class-name objc-protocol-refs[opt]
2160 typedef-name objc-protocol-refs
2161 objc-protocol-refs
2162*/
2163
2164static void
2165c_parser_declspecs (c_parser *parser, struct c_declspecs *specs,
29ce73cb 2166 bool scspec_ok, bool typespec_ok, bool start_attr_ok,
38b7bc7f
JM
2167 bool alignspec_ok, bool auto_type_ok,
2168 enum c_lookahead_kind la)
27bf414c
JM
2169{
2170 bool attrs_ok = start_attr_ok;
9e5b2115 2171 bool seen_type = specs->typespec_kind != ctsk_none;
29ce73cb
PB
2172
2173 if (!typespec_ok)
2174 gcc_assert (la == cla_prefer_id);
2175
2176 while (c_parser_next_token_is (parser, CPP_NAME)
27bf414c
JM
2177 || c_parser_next_token_is (parser, CPP_KEYWORD)
2178 || (c_dialect_objc () && c_parser_next_token_is (parser, CPP_LESS)))
2179 {
2180 struct c_typespec t;
2181 tree attrs;
d19fa6b5 2182 tree align;
dc491a25 2183 location_t loc = c_parser_peek_token (parser)->location;
f725e721 2184
29ce73cb
PB
2185 /* If we cannot accept a type, exit if the next token must start
2186 one. Also, if we already have seen a tagged definition,
2187 a typename would be an error anyway and likely the user
2188 has simply forgotten a semicolon, so we exit. */
2189 if ((!typespec_ok || specs->typespec_kind == ctsk_tagdef)
2190 && c_parser_next_tokens_start_typename (parser, la)
2191 && !c_parser_next_token_is_qualifier (parser))
2192 break;
f725e721 2193
27bf414c
JM
2194 if (c_parser_next_token_is (parser, CPP_NAME))
2195 {
0b2c4be5
DS
2196 c_token *name_token = c_parser_peek_token (parser);
2197 tree value = name_token->value;
2198 c_id_kind kind = name_token->id_kind;
36c5e70a
BE
2199
2200 if (kind == C_ID_ADDRSPACE)
2201 {
2202 addr_space_t as
0b2c4be5
DS
2203 = name_token->keyword - RID_FIRST_ADDR_SPACE;
2204 declspecs_add_addrspace (name_token->location, specs, as);
36c5e70a
BE
2205 c_parser_consume_token (parser);
2206 attrs_ok = true;
2207 continue;
2208 }
2209
29ce73cb
PB
2210 gcc_assert (!c_parser_next_token_is_qualifier (parser));
2211
2212 /* If we cannot accept a type, and the next token must start one,
2213 exit. Do the same if we already have seen a tagged definition,
2214 since it would be an error anyway and likely the user has simply
2215 forgotten a semicolon. */
2216 if (seen_type || !c_parser_next_tokens_start_typename (parser, la))
2217 break;
2218
2219 /* Now at an unknown typename (C_ID_ID), a C_ID_TYPENAME or
2220 a C_ID_CLASSNAME. */
27bf414c
JM
2221 c_parser_consume_token (parser);
2222 seen_type = true;
2223 attrs_ok = true;
29ce73cb
PB
2224 if (kind == C_ID_ID)
2225 {
1c9f5f33 2226 error_at (loc, "unknown type name %qE", value);
29ce73cb
PB
2227 t.kind = ctsk_typedef;
2228 t.spec = error_mark_node;
2229 }
2230 else if (kind == C_ID_TYPENAME
2231 && (!c_dialect_objc ()
2232 || c_parser_next_token_is_not (parser, CPP_LESS)))
27bf414c
JM
2233 {
2234 t.kind = ctsk_typedef;
2235 /* For a typedef name, record the meaning, not the name.
2236 In case of 'foo foo, bar;'. */
2237 t.spec = lookup_name (value);
2238 }
2239 else
2240 {
2241 tree proto = NULL_TREE;
2242 gcc_assert (c_dialect_objc ());
2243 t.kind = ctsk_objc;
2244 if (c_parser_next_token_is (parser, CPP_LESS))
2245 proto = c_parser_objc_protocol_refs (parser);
2246 t.spec = objc_get_protocol_qualified_type (value, proto);
2247 }
29ce73cb
PB
2248 t.expr = NULL_TREE;
2249 t.expr_const_operands = true;
0b2c4be5 2250 declspecs_add_type (name_token->location, specs, t);
27bf414c
JM
2251 continue;
2252 }
2253 if (c_parser_next_token_is (parser, CPP_LESS))
2254 {
2255 /* Make "<SomeProtocol>" equivalent to "id <SomeProtocol>" -
2256 nisse@lysator.liu.se. */
2257 tree proto;
2258 gcc_assert (c_dialect_objc ());
2259 if (!typespec_ok || seen_type)
2260 break;
2261 proto = c_parser_objc_protocol_refs (parser);
2262 t.kind = ctsk_objc;
2263 t.spec = objc_get_protocol_qualified_type (NULL_TREE, proto);
928c19bb
JM
2264 t.expr = NULL_TREE;
2265 t.expr_const_operands = true;
dc491a25 2266 declspecs_add_type (loc, specs, t);
27bf414c
JM
2267 continue;
2268 }
2269 gcc_assert (c_parser_next_token_is (parser, CPP_KEYWORD));
2270 switch (c_parser_peek_token (parser)->keyword)
2271 {
2272 case RID_STATIC:
2273 case RID_EXTERN:
2274 case RID_REGISTER:
2275 case RID_TYPEDEF:
2276 case RID_INLINE:
bbceee64 2277 case RID_NORETURN:
27bf414c
JM
2278 case RID_AUTO:
2279 case RID_THREAD:
2280 if (!scspec_ok)
2281 goto out;
2282 attrs_ok = true;
bbceee64 2283 /* TODO: Distinguish between function specifiers (inline, noreturn)
27bf414c
JM
2284 and storage class specifiers, either here or in
2285 declspecs_add_scspec. */
0b2c4be5
DS
2286 declspecs_add_scspec (loc, specs,
2287 c_parser_peek_token (parser)->value);
27bf414c
JM
2288 c_parser_consume_token (parser);
2289 break;
38b7bc7f
JM
2290 case RID_AUTO_TYPE:
2291 if (!auto_type_ok)
2292 goto out;
2293 /* Fall through. */
27bf414c
JM
2294 case RID_UNSIGNED:
2295 case RID_LONG:
a6766312 2296 case RID_INT128:
27bf414c
JM
2297 case RID_SHORT:
2298 case RID_SIGNED:
2299 case RID_COMPLEX:
2300 case RID_INT:
2301 case RID_CHAR:
2302 case RID_FLOAT:
2303 case RID_DOUBLE:
2304 case RID_VOID:
9a8ce21f
JG
2305 case RID_DFLOAT32:
2306 case RID_DFLOAT64:
2307 case RID_DFLOAT128:
27bf414c 2308 case RID_BOOL:
ab22c1fa
CF
2309 case RID_FRACT:
2310 case RID_ACCUM:
2311 case RID_SAT:
27bf414c
JM
2312 if (!typespec_ok)
2313 goto out;
2314 attrs_ok = true;
2315 seen_type = true;
0bacb8c7
TT
2316 if (c_dialect_objc ())
2317 parser->objc_need_raw_identifier = true;
27bf414c
JM
2318 t.kind = ctsk_resword;
2319 t.spec = c_parser_peek_token (parser)->value;
928c19bb
JM
2320 t.expr = NULL_TREE;
2321 t.expr_const_operands = true;
dc491a25 2322 declspecs_add_type (loc, specs, t);
27bf414c
JM
2323 c_parser_consume_token (parser);
2324 break;
2325 case RID_ENUM:
2326 if (!typespec_ok)
2327 goto out;
2328 attrs_ok = true;
2329 seen_type = true;
2330 t = c_parser_enum_specifier (parser);
dc491a25 2331 declspecs_add_type (loc, specs, t);
27bf414c
JM
2332 break;
2333 case RID_STRUCT:
2334 case RID_UNION:
2335 if (!typespec_ok)
2336 goto out;
2337 attrs_ok = true;
2338 seen_type = true;
2339 t = c_parser_struct_or_union_specifier (parser);
dab71827 2340 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, t.spec);
dc491a25 2341 declspecs_add_type (loc, specs, t);
27bf414c
JM
2342 break;
2343 case RID_TYPEOF:
2344 /* ??? The old parser rejected typeof after other type
2345 specifiers, but is a syntax error the best way of
2346 handling this? */
2347 if (!typespec_ok || seen_type)
2348 goto out;
2349 attrs_ok = true;
2350 seen_type = true;
2351 t = c_parser_typeof_specifier (parser);
dc491a25 2352 declspecs_add_type (loc, specs, t);
27bf414c 2353 break;
267bac10
JM
2354 case RID_ATOMIC:
2355 /* C parser handling of Objective-C constructs needs
2356 checking for correct lvalue-to-rvalue conversions, and
2357 the code in build_modify_expr handling various
2358 Objective-C cases, and that in build_unary_op handling
2359 Objective-C cases for increment / decrement, also needs
2360 updating; uses of TYPE_MAIN_VARIANT in objc_compare_types
2361 and objc_types_are_equivalent may also need updates. */
2362 if (c_dialect_objc ())
2363 sorry ("%<_Atomic%> in Objective-C");
2364 /* C parser handling of OpenMP constructs needs checking for
2365 correct lvalue-to-rvalue conversions. */
2366 if (flag_openmp)
2367 sorry ("%<_Atomic%> with OpenMP");
2368 if (!flag_isoc11)
2369 {
2370 if (flag_isoc99)
2371 pedwarn (loc, OPT_Wpedantic,
2372 "ISO C99 does not support the %<_Atomic%> qualifier");
2373 else
2374 pedwarn (loc, OPT_Wpedantic,
2375 "ISO C90 does not support the %<_Atomic%> qualifier");
2376 }
2377 attrs_ok = true;
2378 tree value;
2379 value = c_parser_peek_token (parser)->value;
2380 c_parser_consume_token (parser);
2381 if (typespec_ok && c_parser_next_token_is (parser, CPP_OPEN_PAREN))
2382 {
2383 /* _Atomic ( type-name ). */
2384 seen_type = true;
2385 c_parser_consume_token (parser);
2386 struct c_type_name *type = c_parser_type_name (parser);
2387 t.kind = ctsk_typeof;
2388 t.spec = error_mark_node;
2389 t.expr = NULL_TREE;
2390 t.expr_const_operands = true;
2391 if (type != NULL)
2392 t.spec = groktypename (type, &t.expr,
2393 &t.expr_const_operands);
2394 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2395 "expected %<)%>");
2396 if (t.spec != error_mark_node)
2397 {
2398 if (TREE_CODE (t.spec) == ARRAY_TYPE)
2399 error_at (loc, "%<_Atomic%>-qualified array type");
2400 else if (TREE_CODE (t.spec) == FUNCTION_TYPE)
2401 error_at (loc, "%<_Atomic%>-qualified function type");
2402 else if (TYPE_QUALS (t.spec) != TYPE_UNQUALIFIED)
2403 error_at (loc, "%<_Atomic%> applied to a qualified type");
2404 else
2405 t.spec = c_build_qualified_type (t.spec, TYPE_QUAL_ATOMIC);
2406 }
2407 declspecs_add_type (loc, specs, t);
2408 }
2409 else
2410 declspecs_add_qual (loc, specs, value);
2411 break;
27bf414c
JM
2412 case RID_CONST:
2413 case RID_VOLATILE:
2414 case RID_RESTRICT:
2415 attrs_ok = true;
0b2c4be5 2416 declspecs_add_qual (loc, specs, c_parser_peek_token (parser)->value);
27bf414c
JM
2417 c_parser_consume_token (parser);
2418 break;
2419 case RID_ATTRIBUTE:
2420 if (!attrs_ok)
2421 goto out;
2422 attrs = c_parser_attributes (parser);
0b2c4be5 2423 declspecs_add_attrs (loc, specs, attrs);
27bf414c 2424 break;
d19fa6b5 2425 case RID_ALIGNAS:
568a31f2
MP
2426 if (!alignspec_ok)
2427 goto out;
d19fa6b5 2428 align = c_parser_alignas_specifier (parser);
0b2c4be5 2429 declspecs_add_alignas (loc, specs, align);
d19fa6b5 2430 break;
27bf414c
JM
2431 default:
2432 goto out;
2433 }
2434 }
2435 out: ;
2436}
2437
2438/* Parse an enum specifier (C90 6.5.2.2, C99 6.7.2.2).
2439
2440 enum-specifier:
2441 enum attributes[opt] identifier[opt] { enumerator-list } attributes[opt]
2442 enum attributes[opt] identifier[opt] { enumerator-list , } attributes[opt]
2443 enum attributes[opt] identifier
2444
2445 The form with trailing comma is new in C99. The forms with
2446 attributes are GNU extensions. In GNU C, we accept any expression
2447 without commas in the syntax (assignment expressions, not just
2448 conditional expressions); assignment expressions will be diagnosed
2449 as non-constant.
2450
2451 enumerator-list:
2452 enumerator
2453 enumerator-list , enumerator
2454
2455 enumerator:
2456 enumeration-constant
2457 enumeration-constant = constant-expression
2458*/
2459
2460static struct c_typespec
2461c_parser_enum_specifier (c_parser *parser)
2462{
2463 struct c_typespec ret;
2464 tree attrs;
2465 tree ident = NULL_TREE;
24b97832 2466 location_t enum_loc;
922f2908 2467 location_t ident_loc = UNKNOWN_LOCATION; /* Quiet warning. */
27bf414c 2468 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ENUM));
24b97832 2469 enum_loc = c_parser_peek_token (parser)->location;
27bf414c
JM
2470 c_parser_consume_token (parser);
2471 attrs = c_parser_attributes (parser);
c2255bc4 2472 enum_loc = c_parser_peek_token (parser)->location;
5af28c74
TT
2473 /* Set the location in case we create a decl now. */
2474 c_parser_set_source_position_from_token (c_parser_peek_token (parser));
27bf414c
JM
2475 if (c_parser_next_token_is (parser, CPP_NAME))
2476 {
2477 ident = c_parser_peek_token (parser)->value;
c7412148 2478 ident_loc = c_parser_peek_token (parser)->location;
24b97832 2479 enum_loc = ident_loc;
27bf414c
JM
2480 c_parser_consume_token (parser);
2481 }
2482 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
2483 {
2484 /* Parse an enum definition. */
7114359f 2485 struct c_enum_contents the_enum;
575bfb00 2486 tree type;
27bf414c
JM
2487 tree postfix_attrs;
2488 /* We chain the enumerators in reverse order, then put them in
2489 forward order at the end. */
575bfb00
LC
2490 tree values;
2491 timevar_push (TV_PARSE_ENUM);
2492 type = start_enum (enum_loc, &the_enum, ident);
2493 values = NULL_TREE;
27bf414c
JM
2494 c_parser_consume_token (parser);
2495 while (true)
2496 {
2497 tree enum_id;
2498 tree enum_value;
2499 tree enum_decl;
2500 bool seen_comma;
5af28c74 2501 c_token *token;
922f2908 2502 location_t comma_loc = UNKNOWN_LOCATION; /* Quiet warning. */
7370e0da 2503 location_t decl_loc, value_loc;
27bf414c
JM
2504 if (c_parser_next_token_is_not (parser, CPP_NAME))
2505 {
2506 c_parser_error (parser, "expected identifier");
2507 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
2508 values = error_mark_node;
2509 break;
2510 }
5af28c74
TT
2511 token = c_parser_peek_token (parser);
2512 enum_id = token->value;
2513 /* Set the location in case we create a decl now. */
2514 c_parser_set_source_position_from_token (token);
7370e0da 2515 decl_loc = value_loc = token->location;
27bf414c
JM
2516 c_parser_consume_token (parser);
2517 if (c_parser_next_token_is (parser, CPP_EQ))
2518 {
2519 c_parser_consume_token (parser);
85790e66 2520 value_loc = c_parser_peek_token (parser)->location;
27bf414c
JM
2521 enum_value = c_parser_expr_no_commas (parser, NULL).value;
2522 }
2523 else
2524 enum_value = NULL_TREE;
7370e0da 2525 enum_decl = build_enumerator (decl_loc, value_loc,
c2255bc4 2526 &the_enum, enum_id, enum_value);
27bf414c
JM
2527 TREE_CHAIN (enum_decl) = values;
2528 values = enum_decl;
2529 seen_comma = false;
2530 if (c_parser_next_token_is (parser, CPP_COMMA))
2531 {
c7412148 2532 comma_loc = c_parser_peek_token (parser)->location;
27bf414c
JM
2533 seen_comma = true;
2534 c_parser_consume_token (parser);
2535 }
2536 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2537 {
fcf73884 2538 if (seen_comma && !flag_isoc99)
c1771a20 2539 pedwarn (comma_loc, OPT_Wpedantic, "comma at end of enumerator list");
27bf414c
JM
2540 c_parser_consume_token (parser);
2541 break;
2542 }
2543 if (!seen_comma)
2544 {
2545 c_parser_error (parser, "expected %<,%> or %<}%>");
2546 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
2547 values = error_mark_node;
2548 break;
2549 }
2550 }
2551 postfix_attrs = c_parser_attributes (parser);
2552 ret.spec = finish_enum (type, nreverse (values),
2553 chainon (attrs, postfix_attrs));
2554 ret.kind = ctsk_tagdef;
928c19bb
JM
2555 ret.expr = NULL_TREE;
2556 ret.expr_const_operands = true;
575bfb00 2557 timevar_pop (TV_PARSE_ENUM);
27bf414c
JM
2558 return ret;
2559 }
2560 else if (!ident)
2561 {
2562 c_parser_error (parser, "expected %<{%>");
2563 ret.spec = error_mark_node;
2564 ret.kind = ctsk_tagref;
928c19bb
JM
2565 ret.expr = NULL_TREE;
2566 ret.expr_const_operands = true;
27bf414c
JM
2567 return ret;
2568 }
c2255bc4 2569 ret = parser_xref_tag (ident_loc, ENUMERAL_TYPE, ident);
27bf414c
JM
2570 /* In ISO C, enumerated types can be referred to only if already
2571 defined. */
2572 if (pedantic && !COMPLETE_TYPE_P (ret.spec))
c7412148
TT
2573 {
2574 gcc_assert (ident);
c1771a20 2575 pedwarn (enum_loc, OPT_Wpedantic,
509c9d60 2576 "ISO C forbids forward references to %<enum%> types");
c7412148 2577 }
27bf414c
JM
2578 return ret;
2579}
2580
2581/* Parse a struct or union specifier (C90 6.5.2.1, C99 6.7.2.1).
2582
2583 struct-or-union-specifier:
2584 struct-or-union attributes[opt] identifier[opt]
2585 { struct-contents } attributes[opt]
2586 struct-or-union attributes[opt] identifier
2587
2588 struct-contents:
2589 struct-declaration-list
2590
2591 struct-declaration-list:
2592 struct-declaration ;
2593 struct-declaration-list struct-declaration ;
2594
2595 GNU extensions:
2596
2597 struct-contents:
2598 empty
2599 struct-declaration
2600 struct-declaration-list struct-declaration
2601
2602 struct-declaration-list:
2603 struct-declaration-list ;
2604 ;
2605
2606 (Note that in the syntax here, unlike that in ISO C, the semicolons
2607 are included here rather than in struct-declaration, in order to
2608 describe the syntax with extra semicolons and missing semicolon at
2609 end.)
2610
2611 Objective-C:
2612
2613 struct-declaration-list:
2614 @defs ( class-name )
2615
2616 (Note this does not include a trailing semicolon, but can be
2617 followed by further declarations, and gets a pedwarn-if-pedantic
2618 when followed by a semicolon.) */
2619
2620static struct c_typespec
2621c_parser_struct_or_union_specifier (c_parser *parser)
2622{
2623 struct c_typespec ret;
2624 tree attrs;
2625 tree ident = NULL_TREE;
24b97832
ILT
2626 location_t struct_loc;
2627 location_t ident_loc = UNKNOWN_LOCATION;
27bf414c
JM
2628 enum tree_code code;
2629 switch (c_parser_peek_token (parser)->keyword)
2630 {
2631 case RID_STRUCT:
2632 code = RECORD_TYPE;
2633 break;
2634 case RID_UNION:
2635 code = UNION_TYPE;
2636 break;
2637 default:
2638 gcc_unreachable ();
2639 }
24b97832 2640 struct_loc = c_parser_peek_token (parser)->location;
27bf414c
JM
2641 c_parser_consume_token (parser);
2642 attrs = c_parser_attributes (parser);
c2255bc4 2643
5af28c74
TT
2644 /* Set the location in case we create a decl now. */
2645 c_parser_set_source_position_from_token (c_parser_peek_token (parser));
c2255bc4 2646
27bf414c
JM
2647 if (c_parser_next_token_is (parser, CPP_NAME))
2648 {
2649 ident = c_parser_peek_token (parser)->value;
24b97832
ILT
2650 ident_loc = c_parser_peek_token (parser)->location;
2651 struct_loc = ident_loc;
27bf414c
JM
2652 c_parser_consume_token (parser);
2653 }
2654 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
2655 {
2656 /* Parse a struct or union definition. Start the scope of the
2657 tag before parsing components. */
dc491a25
ILT
2658 struct c_struct_parse_info *struct_info;
2659 tree type = start_struct (struct_loc, code, ident, &struct_info);
27bf414c
JM
2660 tree postfix_attrs;
2661 /* We chain the components in reverse order, then put them in
2662 forward order at the end. Each struct-declaration may
2663 declare multiple components (comma-separated), so we must use
2664 chainon to join them, although when parsing each
2665 struct-declaration we can use TREE_CHAIN directly.
2666
2667 The theory behind all this is that there will be more
2668 semicolon separated fields than comma separated fields, and
2669 so we'll be minimizing the number of node traversals required
2670 by chainon. */
575bfb00
LC
2671 tree contents;
2672 timevar_push (TV_PARSE_STRUCT);
2673 contents = NULL_TREE;
27bf414c
JM
2674 c_parser_consume_token (parser);
2675 /* Handle the Objective-C @defs construct,
2676 e.g. foo(sizeof(struct{ @defs(ClassName) }));. */
2677 if (c_parser_next_token_is_keyword (parser, RID_AT_DEFS))
2678 {
2679 tree name;
2680 gcc_assert (c_dialect_objc ());
2681 c_parser_consume_token (parser);
2682 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2683 goto end_at_defs;
2684 if (c_parser_next_token_is (parser, CPP_NAME)
2685 && c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME)
2686 {
2687 name = c_parser_peek_token (parser)->value;
2688 c_parser_consume_token (parser);
2689 }
2690 else
2691 {
2692 c_parser_error (parser, "expected class name");
2693 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
2694 goto end_at_defs;
2695 }
2696 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2697 "expected %<)%>");
2698 contents = nreverse (objc_get_class_ivars (name));
2699 }
2700 end_at_defs:
2701 /* Parse the struct-declarations and semicolons. Problems with
2702 semicolons are diagnosed here; empty structures are diagnosed
2703 elsewhere. */
2704 while (true)
2705 {
2706 tree decls;
2707 /* Parse any stray semicolon. */
2708 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
2709 {
c1771a20 2710 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
509c9d60 2711 "extra semicolon in struct or union specified");
27bf414c
JM
2712 c_parser_consume_token (parser);
2713 continue;
2714 }
2715 /* Stop if at the end of the struct or union contents. */
2716 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2717 {
2718 c_parser_consume_token (parser);
2719 break;
2720 }
bc4071dd
RH
2721 /* Accept #pragmas at struct scope. */
2722 if (c_parser_next_token_is (parser, CPP_PRAGMA))
2723 {
acf0174b 2724 c_parser_pragma (parser, pragma_struct);
bc4071dd
RH
2725 continue;
2726 }
27bf414c
JM
2727 /* Parse some comma-separated declarations, but not the
2728 trailing semicolon if any. */
2729 decls = c_parser_struct_declaration (parser);
2730 contents = chainon (decls, contents);
2731 /* If no semicolon follows, either we have a parse error or
2732 are at the end of the struct or union and should
2733 pedwarn. */
2734 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
2735 c_parser_consume_token (parser);
2736 else
2737 {
2738 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
b8698a0f 2739 pedwarn (c_parser_peek_token (parser)->location, 0,
509c9d60 2740 "no semicolon at end of struct or union");
f725e721
PB
2741 else if (parser->error
2742 || !c_parser_next_token_starts_declspecs (parser))
27bf414c
JM
2743 {
2744 c_parser_error (parser, "expected %<;%>");
2745 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
2746 break;
2747 }
f725e721
PB
2748
2749 /* If we come here, we have already emitted an error
2750 for an expected `;', identifier or `(', and we also
2751 recovered already. Go on with the next field. */
27bf414c
JM
2752 }
2753 }
2754 postfix_attrs = c_parser_attributes (parser);
c2255bc4 2755 ret.spec = finish_struct (struct_loc, type, nreverse (contents),
dc491a25 2756 chainon (attrs, postfix_attrs), struct_info);
27bf414c 2757 ret.kind = ctsk_tagdef;
928c19bb
JM
2758 ret.expr = NULL_TREE;
2759 ret.expr_const_operands = true;
575bfb00 2760 timevar_pop (TV_PARSE_STRUCT);
27bf414c
JM
2761 return ret;
2762 }
2763 else if (!ident)
2764 {
2765 c_parser_error (parser, "expected %<{%>");
2766 ret.spec = error_mark_node;
2767 ret.kind = ctsk_tagref;
928c19bb
JM
2768 ret.expr = NULL_TREE;
2769 ret.expr_const_operands = true;
67c2939d 2770 return ret;
27bf414c 2771 }
c2255bc4 2772 ret = parser_xref_tag (ident_loc, code, ident);
27bf414c
JM
2773 return ret;
2774}
2775
2776/* Parse a struct-declaration (C90 6.5.2.1, C99 6.7.2.1), *without*
2777 the trailing semicolon.
2778
2779 struct-declaration:
2780 specifier-qualifier-list struct-declarator-list
32912286 2781 static_assert-declaration-no-semi
27bf414c
JM
2782
2783 specifier-qualifier-list:
2784 type-specifier specifier-qualifier-list[opt]
2785 type-qualifier specifier-qualifier-list[opt]
2786 attributes specifier-qualifier-list[opt]
2787
2788 struct-declarator-list:
2789 struct-declarator
2790 struct-declarator-list , attributes[opt] struct-declarator
2791
2792 struct-declarator:
2793 declarator attributes[opt]
2794 declarator[opt] : constant-expression attributes[opt]
2795
2796 GNU extensions:
2797
2798 struct-declaration:
2799 __extension__ struct-declaration
2800 specifier-qualifier-list
2801
2802 Unlike the ISO C syntax, semicolons are handled elsewhere. The use
2803 of attributes where shown is a GNU extension. In GNU C, we accept
2804 any expression without commas in the syntax (assignment
2805 expressions, not just conditional expressions); assignment
2806 expressions will be diagnosed as non-constant. */
2807
2808static tree
2809c_parser_struct_declaration (c_parser *parser)
2810{
2811 struct c_declspecs *specs;
2812 tree prefix_attrs;
2813 tree all_prefix_attrs;
2814 tree decls;
c7412148 2815 location_t decl_loc;
27bf414c
JM
2816 if (c_parser_next_token_is_keyword (parser, RID_EXTENSION))
2817 {
2818 int ext;
2819 tree decl;
2820 ext = disable_extension_diagnostics ();
2821 c_parser_consume_token (parser);
2822 decl = c_parser_struct_declaration (parser);
2823 restore_extension_diagnostics (ext);
2824 return decl;
2825 }
32912286
JM
2826 if (c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT))
2827 {
2828 c_parser_static_assert_declaration_no_semi (parser);
2829 return NULL_TREE;
2830 }
27bf414c 2831 specs = build_null_declspecs ();
c7412148 2832 decl_loc = c_parser_peek_token (parser)->location;
f28aa681
MP
2833 /* Strictly by the standard, we shouldn't allow _Alignas here,
2834 but it appears to have been intended to allow it there, so
2835 we're keeping it as it is until WG14 reaches a conclusion
2836 of N1731.
2837 <http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1731.pdf> */
568a31f2 2838 c_parser_declspecs (parser, specs, false, true, true,
38b7bc7f 2839 true, false, cla_nonabstract_decl);
27bf414c 2840 if (parser->error)
67c2939d 2841 return NULL_TREE;
27bf414c
JM
2842 if (!specs->declspecs_seen_p)
2843 {
2844 c_parser_error (parser, "expected specifier-qualifier-list");
2845 return NULL_TREE;
2846 }
2847 finish_declspecs (specs);
b8cbdff5
JM
2848 if (c_parser_next_token_is (parser, CPP_SEMICOLON)
2849 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
27bf414c
JM
2850 {
2851 tree ret;
9e5b2115 2852 if (specs->typespec_kind == ctsk_none)
27bf414c 2853 {
c1771a20 2854 pedwarn (decl_loc, OPT_Wpedantic,
509c9d60 2855 "ISO C forbids member declarations with no members");
27bf414c
JM
2856 shadow_tag_warned (specs, pedantic);
2857 ret = NULL_TREE;
2858 }
2859 else
2860 {
2861 /* Support for unnamed structs or unions as members of
2862 structs or unions (which is [a] useful and [b] supports
2863 MS P-SDK). */
b9baeecd 2864 tree attrs = NULL;
3d10ed6c
AH
2865
2866 ret = grokfield (c_parser_peek_token (parser)->location,
2867 build_id_declarator (NULL_TREE), specs,
b9baeecd 2868 NULL_TREE, &attrs);
0ad7e054
RS
2869 if (ret)
2870 decl_attributes (&ret, attrs, 0);
27bf414c
JM
2871 }
2872 return ret;
2873 }
f725e721
PB
2874
2875 /* Provide better error recovery. Note that a type name here is valid,
2876 and will be treated as a field name. */
2877 if (specs->typespec_kind == ctsk_tagdef
2878 && TREE_CODE (specs->type) != ENUMERAL_TYPE
2879 && c_parser_next_token_starts_declspecs (parser)
2880 && !c_parser_next_token_is (parser, CPP_NAME))
2881 {
2882 c_parser_error (parser, "expected %<;%>, identifier or %<(%>");
2883 parser->error = false;
2884 return NULL_TREE;
2885 }
2886
27bf414c
JM
2887 pending_xref_error ();
2888 prefix_attrs = specs->attrs;
2889 all_prefix_attrs = prefix_attrs;
2890 specs->attrs = NULL_TREE;
2891 decls = NULL_TREE;
2892 while (true)
2893 {
2894 /* Declaring one or more declarators or un-named bit-fields. */
2895 struct c_declarator *declarator;
2896 bool dummy = false;
2897 if (c_parser_next_token_is (parser, CPP_COLON))
2898 declarator = build_id_declarator (NULL_TREE);
2899 else
9e5b2115
PB
2900 declarator = c_parser_declarator (parser,
2901 specs->typespec_kind != ctsk_none,
27bf414c
JM
2902 C_DTR_NORMAL, &dummy);
2903 if (declarator == NULL)
2904 {
2905 c_parser_skip_to_end_of_block_or_statement (parser);
2906 break;
2907 }
2908 if (c_parser_next_token_is (parser, CPP_COLON)
2909 || c_parser_next_token_is (parser, CPP_COMMA)
2910 || c_parser_next_token_is (parser, CPP_SEMICOLON)
2911 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE)
2912 || c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2913 {
2914 tree postfix_attrs = NULL_TREE;
2915 tree width = NULL_TREE;
2916 tree d;
2917 if (c_parser_next_token_is (parser, CPP_COLON))
2918 {
2919 c_parser_consume_token (parser);
2920 width = c_parser_expr_no_commas (parser, NULL).value;
2921 }
2922 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2923 postfix_attrs = c_parser_attributes (parser);
3d10ed6c
AH
2924 d = grokfield (c_parser_peek_token (parser)->location,
2925 declarator, specs, width, &all_prefix_attrs);
27bf414c
JM
2926 decl_attributes (&d, chainon (postfix_attrs,
2927 all_prefix_attrs), 0);
910ad8de 2928 DECL_CHAIN (d) = decls;
27bf414c
JM
2929 decls = d;
2930 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2931 all_prefix_attrs = chainon (c_parser_attributes (parser),
2932 prefix_attrs);
2933 else
2934 all_prefix_attrs = prefix_attrs;
2935 if (c_parser_next_token_is (parser, CPP_COMMA))
2936 c_parser_consume_token (parser);
2937 else if (c_parser_next_token_is (parser, CPP_SEMICOLON)
2938 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2939 {
2940 /* Semicolon consumed in caller. */
2941 break;
2942 }
2943 else
2944 {
2945 c_parser_error (parser, "expected %<,%>, %<;%> or %<}%>");
2946 break;
2947 }
2948 }
2949 else
2950 {
2951 c_parser_error (parser,
2952 "expected %<:%>, %<,%>, %<;%>, %<}%> or "
2953 "%<__attribute__%>");
2954 break;
2955 }
2956 }
2957 return decls;
2958}
2959
2960/* Parse a typeof specifier (a GNU extension).
2961
2962 typeof-specifier:
2963 typeof ( expression )
2964 typeof ( type-name )
2965*/
2966
2967static struct c_typespec
2968c_parser_typeof_specifier (c_parser *parser)
2969{
2970 struct c_typespec ret;
2971 ret.kind = ctsk_typeof;
2972 ret.spec = error_mark_node;
928c19bb
JM
2973 ret.expr = NULL_TREE;
2974 ret.expr_const_operands = true;
27bf414c
JM
2975 gcc_assert (c_parser_next_token_is_keyword (parser, RID_TYPEOF));
2976 c_parser_consume_token (parser);
7d882b83 2977 c_inhibit_evaluation_warnings++;
27bf414c
JM
2978 in_typeof++;
2979 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2980 {
7d882b83 2981 c_inhibit_evaluation_warnings--;
27bf414c
JM
2982 in_typeof--;
2983 return ret;
2984 }
29ce73cb 2985 if (c_parser_next_tokens_start_typename (parser, cla_prefer_id))
27bf414c
JM
2986 {
2987 struct c_type_name *type = c_parser_type_name (parser);
7d882b83 2988 c_inhibit_evaluation_warnings--;
27bf414c
JM
2989 in_typeof--;
2990 if (type != NULL)
2991 {
928c19bb 2992 ret.spec = groktypename (type, &ret.expr, &ret.expr_const_operands);
27bf414c
JM
2993 pop_maybe_used (variably_modified_type_p (ret.spec, NULL_TREE));
2994 }
2995 }
2996 else
2997 {
52ffd86e 2998 bool was_vm;
c7412148 2999 location_t here = c_parser_peek_token (parser)->location;
27bf414c 3000 struct c_expr expr = c_parser_expression (parser);
7d882b83 3001 c_inhibit_evaluation_warnings--;
27bf414c
JM
3002 in_typeof--;
3003 if (TREE_CODE (expr.value) == COMPONENT_REF
3004 && DECL_C_BIT_FIELD (TREE_OPERAND (expr.value, 1)))
3ba09659 3005 error_at (here, "%<typeof%> applied to a bit-field");
ebfbbdc5 3006 mark_exp_read (expr.value);
27bf414c 3007 ret.spec = TREE_TYPE (expr.value);
52ffd86e 3008 was_vm = variably_modified_type_p (ret.spec, NULL_TREE);
928c19bb
JM
3009 /* This is returned with the type so that when the type is
3010 evaluated, this can be evaluated. */
3011 if (was_vm)
3012 ret.expr = c_fully_fold (expr.value, false, &ret.expr_const_operands);
52ffd86e 3013 pop_maybe_used (was_vm);
267bac10
JM
3014 /* For use in macros such as those in <stdatomic.h>, remove
3015 _Atomic and const qualifiers from atomic types. (Possibly
3016 all qualifiers should be removed; const can be an issue for
3017 more macros using typeof than just the <stdatomic.h>
3018 ones.) */
3019 if (ret.spec != error_mark_node && TYPE_ATOMIC (ret.spec))
3020 ret.spec = c_build_qualified_type (ret.spec,
3021 (TYPE_QUALS (ret.spec)
3022 & ~(TYPE_QUAL_ATOMIC
3023 | TYPE_QUAL_CONST)));
27bf414c
JM
3024 }
3025 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
3026 return ret;
3027}
3028
d19fa6b5
JM
3029/* Parse an alignment-specifier.
3030
48b0b196 3031 C11 6.7.5:
d19fa6b5
JM
3032
3033 alignment-specifier:
3034 _Alignas ( type-name )
3035 _Alignas ( constant-expression )
3036*/
3037
3038static tree
3039c_parser_alignas_specifier (c_parser * parser)
3040{
3041 tree ret = error_mark_node;
3042 location_t loc = c_parser_peek_token (parser)->location;
3043 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ALIGNAS));
3044 c_parser_consume_token (parser);
48b0b196 3045 if (!flag_isoc11)
d19fa6b5
JM
3046 {
3047 if (flag_isoc99)
c1771a20 3048 pedwarn (loc, OPT_Wpedantic,
d19fa6b5
JM
3049 "ISO C99 does not support %<_Alignas%>");
3050 else
c1771a20 3051 pedwarn (loc, OPT_Wpedantic,
d19fa6b5
JM
3052 "ISO C90 does not support %<_Alignas%>");
3053 }
3054 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3055 return ret;
3056 if (c_parser_next_tokens_start_typename (parser, cla_prefer_id))
3057 {
3058 struct c_type_name *type = c_parser_type_name (parser);
3059 if (type != NULL)
296674db
JM
3060 ret = c_sizeof_or_alignof_type (loc, groktypename (type, NULL, NULL),
3061 false, true, 1);
d19fa6b5
JM
3062 }
3063 else
3064 ret = c_parser_expr_no_commas (parser, NULL).value;
3065 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
3066 return ret;
3067}
3068
27bf414c
JM
3069/* Parse a declarator, possibly an abstract declarator (C90 6.5.4,
3070 6.5.5, C99 6.7.5, 6.7.6). If TYPE_SEEN_P then a typedef name may
3071 be redeclared; otherwise it may not. KIND indicates which kind of
3072 declarator is wanted. Returns a valid declarator except in the
3073 case of a syntax error in which case NULL is returned. *SEEN_ID is
3074 set to true if an identifier being declared is seen; this is used
3075 to diagnose bad forms of abstract array declarators and to
3076 determine whether an identifier list is syntactically permitted.
3077
3078 declarator:
3079 pointer[opt] direct-declarator
3080
3081 direct-declarator:
3082 identifier
3083 ( attributes[opt] declarator )
3084 direct-declarator array-declarator
3085 direct-declarator ( parameter-type-list )
3086 direct-declarator ( identifier-list[opt] )
3087
3088 pointer:
3089 * type-qualifier-list[opt]
3090 * type-qualifier-list[opt] pointer
3091
3092 type-qualifier-list:
3093 type-qualifier
3094 attributes
3095 type-qualifier-list type-qualifier
3096 type-qualifier-list attributes
3097
568a31f2
MP
3098 array-declarator:
3099 [ type-qualifier-list[opt] assignment-expression[opt] ]
3100 [ static type-qualifier-list[opt] assignment-expression ]
3101 [ type-qualifier-list static assignment-expression ]
3102 [ type-qualifier-list[opt] * ]
3103
27bf414c
JM
3104 parameter-type-list:
3105 parameter-list
3106 parameter-list , ...
3107
3108 parameter-list:
3109 parameter-declaration
3110 parameter-list , parameter-declaration
3111
3112 parameter-declaration:
3113 declaration-specifiers declarator attributes[opt]
3114 declaration-specifiers abstract-declarator[opt] attributes[opt]
3115
3116 identifier-list:
3117 identifier
3118 identifier-list , identifier
3119
3120 abstract-declarator:
3121 pointer
3122 pointer[opt] direct-abstract-declarator
3123
3124 direct-abstract-declarator:
3125 ( attributes[opt] abstract-declarator )
3126 direct-abstract-declarator[opt] array-declarator
3127 direct-abstract-declarator[opt] ( parameter-type-list[opt] )
3128
3129 GNU extensions:
3130
3131 direct-declarator:
3132 direct-declarator ( parameter-forward-declarations
3133 parameter-type-list[opt] )
3134
3135 direct-abstract-declarator:
c22cacf3 3136 direct-abstract-declarator[opt] ( parameter-forward-declarations
27bf414c
JM
3137 parameter-type-list[opt] )
3138
3139 parameter-forward-declarations:
3140 parameter-list ;
3141 parameter-forward-declarations parameter-list ;
3142
3143 The uses of attributes shown above are GNU extensions.
3144
3145 Some forms of array declarator are not included in C99 in the
3146 syntax for abstract declarators; these are disallowed elsewhere.
3147 This may be a defect (DR#289).
3148
3149 This function also accepts an omitted abstract declarator as being
3150 an abstract declarator, although not part of the formal syntax. */
3151
3152static struct c_declarator *
3153c_parser_declarator (c_parser *parser, bool type_seen_p, c_dtr_syn kind,
3154 bool *seen_id)
3155{
3156 /* Parse any initial pointer part. */
3157 if (c_parser_next_token_is (parser, CPP_MULT))
3158 {
3159 struct c_declspecs *quals_attrs = build_null_declspecs ();
3160 struct c_declarator *inner;
3161 c_parser_consume_token (parser);
568a31f2 3162 c_parser_declspecs (parser, quals_attrs, false, false, true,
38b7bc7f 3163 false, false, cla_prefer_id);
27bf414c
JM
3164 inner = c_parser_declarator (parser, type_seen_p, kind, seen_id);
3165 if (inner == NULL)
3166 return NULL;
3167 else
3168 return make_pointer_declarator (quals_attrs, inner);
3169 }
3170 /* Now we have a direct declarator, direct abstract declarator or
3171 nothing (which counts as a direct abstract declarator here). */
3172 return c_parser_direct_declarator (parser, type_seen_p, kind, seen_id);
3173}
3174
3175/* Parse a direct declarator or direct abstract declarator; arguments
3176 as c_parser_declarator. */
3177
3178static struct c_declarator *
3179c_parser_direct_declarator (c_parser *parser, bool type_seen_p, c_dtr_syn kind,
3180 bool *seen_id)
3181{
3182 /* The direct declarator must start with an identifier (possibly
3183 omitted) or a parenthesized declarator (possibly abstract). In
3184 an ordinary declarator, initial parentheses must start a
3185 parenthesized declarator. In an abstract declarator or parameter
3186 declarator, they could start a parenthesized declarator or a
3187 parameter list. To tell which, the open parenthesis and any
3188 following attributes must be read. If a declaration specifier
3189 follows, then it is a parameter list; if the specifier is a
3190 typedef name, there might be an ambiguity about redeclaring it,
3191 which is resolved in the direction of treating it as a typedef
3192 name. If a close parenthesis follows, it is also an empty
3193 parameter list, as the syntax does not permit empty abstract
0fa2e4df 3194 declarators. Otherwise, it is a parenthesized declarator (in
27bf414c
JM
3195 which case the analysis may be repeated inside it, recursively).
3196
3197 ??? There is an ambiguity in a parameter declaration "int
3198 (__attribute__((foo)) x)", where x is not a typedef name: it
3199 could be an abstract declarator for a function, or declare x with
3200 parentheses. The proper resolution of this ambiguity needs
3201 documenting. At present we follow an accident of the old
3202 parser's implementation, whereby the first parameter must have
3203 some declaration specifiers other than just attributes. Thus as
0fa2e4df 3204 a parameter declaration it is treated as a parenthesized
27bf414c
JM
3205 parameter named x, and as an abstract declarator it is
3206 rejected.
3207
3208 ??? Also following the old parser, attributes inside an empty
3209 parameter list are ignored, making it a list not yielding a
3210 prototype, rather than giving an error or making it have one
3211 parameter with implicit type int.
3212
3213 ??? Also following the old parser, typedef names may be
3214 redeclared in declarators, but not Objective-C class names. */
3215
3216 if (kind != C_DTR_ABSTRACT
3217 && c_parser_next_token_is (parser, CPP_NAME)
3218 && ((type_seen_p
a6341d57
NP
3219 && (c_parser_peek_token (parser)->id_kind == C_ID_TYPENAME
3220 || c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME))
27bf414c
JM
3221 || c_parser_peek_token (parser)->id_kind == C_ID_ID))
3222 {
3223 struct c_declarator *inner
3224 = build_id_declarator (c_parser_peek_token (parser)->value);
3225 *seen_id = true;
6037d88d 3226 inner->id_loc = c_parser_peek_token (parser)->location;
27bf414c
JM
3227 c_parser_consume_token (parser);
3228 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
3229 }
3230
3231 if (kind != C_DTR_NORMAL
3232 && c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
3233 {
3234 struct c_declarator *inner = build_id_declarator (NULL_TREE);
3235 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
3236 }
3237
3238 /* Either we are at the end of an abstract declarator, or we have
3239 parentheses. */
3240
3241 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
3242 {
3243 tree attrs;
3244 struct c_declarator *inner;
3245 c_parser_consume_token (parser);
3246 attrs = c_parser_attributes (parser);
3247 if (kind != C_DTR_NORMAL
3248 && (c_parser_next_token_starts_declspecs (parser)
3249 || c_parser_next_token_is (parser, CPP_CLOSE_PAREN)))
3250 {
3251 struct c_arg_info *args
3252 = c_parser_parms_declarator (parser, kind == C_DTR_NORMAL,
3253 attrs);
3254 if (args == NULL)
3255 return NULL;
3256 else
3257 {
3258 inner
3259 = build_function_declarator (args,
3260 build_id_declarator (NULL_TREE));
3261 return c_parser_direct_declarator_inner (parser, *seen_id,
3262 inner);
3263 }
3264 }
3265 /* A parenthesized declarator. */
3266 inner = c_parser_declarator (parser, type_seen_p, kind, seen_id);
3267 if (inner != NULL && attrs != NULL)
3268 inner = build_attrs_declarator (attrs, inner);
3269 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3270 {
3271 c_parser_consume_token (parser);
3272 if (inner == NULL)
3273 return NULL;
3274 else
3275 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
3276 }
3277 else
3278 {
3279 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3280 "expected %<)%>");
3281 return NULL;
3282 }
3283 }
3284 else
3285 {
3286 if (kind == C_DTR_NORMAL)
3287 {
3288 c_parser_error (parser, "expected identifier or %<(%>");
3289 return NULL;
3290 }
3291 else
3292 return build_id_declarator (NULL_TREE);
3293 }
3294}
3295
3296/* Parse part of a direct declarator or direct abstract declarator,
3297 given that some (in INNER) has already been parsed; ID_PRESENT is
3298 true if an identifier is present, false for an abstract
3299 declarator. */
3300
3301static struct c_declarator *
3302c_parser_direct_declarator_inner (c_parser *parser, bool id_present,
3303 struct c_declarator *inner)
3304{
3305 /* Parse a sequence of array declarators and parameter lists. */
3306 if (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
3307 {
b8698a0f 3308 location_t brace_loc = c_parser_peek_token (parser)->location;
27bf414c
JM
3309 struct c_declarator *declarator;
3310 struct c_declspecs *quals_attrs = build_null_declspecs ();
3311 bool static_seen;
3312 bool star_seen;
267bac10
JM
3313 struct c_expr dimen;
3314 dimen.value = NULL_TREE;
3315 dimen.original_code = ERROR_MARK;
3316 dimen.original_type = NULL_TREE;
27bf414c 3317 c_parser_consume_token (parser);
568a31f2 3318 c_parser_declspecs (parser, quals_attrs, false, false, true,
38b7bc7f 3319 false, false, cla_prefer_id);
27bf414c
JM
3320 static_seen = c_parser_next_token_is_keyword (parser, RID_STATIC);
3321 if (static_seen)
3322 c_parser_consume_token (parser);
3323 if (static_seen && !quals_attrs->declspecs_seen_p)
568a31f2 3324 c_parser_declspecs (parser, quals_attrs, false, false, true,
38b7bc7f 3325 false, false, cla_prefer_id);
27bf414c
JM
3326 if (!quals_attrs->declspecs_seen_p)
3327 quals_attrs = NULL;
3328 /* If "static" is present, there must be an array dimension.
3329 Otherwise, there may be a dimension, "*", or no
3330 dimension. */
3331 if (static_seen)
3332 {
3333 star_seen = false;
267bac10 3334 dimen = c_parser_expr_no_commas (parser, NULL);
27bf414c
JM
3335 }
3336 else
3337 {
3338 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
3339 {
267bac10 3340 dimen.value = NULL_TREE;
27bf414c
JM
3341 star_seen = false;
3342 }
b72271b9 3343 else if (flag_cilkplus
36536d79
BI
3344 && c_parser_next_token_is (parser, CPP_COLON))
3345 {
267bac10 3346 dimen.value = error_mark_node;
36536d79
BI
3347 star_seen = false;
3348 error_at (c_parser_peek_token (parser)->location,
3349 "array notations cannot be used in declaration");
3350 c_parser_consume_token (parser);
3351 }
27bf414c
JM
3352 else if (c_parser_next_token_is (parser, CPP_MULT))
3353 {
3354 if (c_parser_peek_2nd_token (parser)->type == CPP_CLOSE_SQUARE)
3355 {
267bac10 3356 dimen.value = NULL_TREE;
27bf414c
JM
3357 star_seen = true;
3358 c_parser_consume_token (parser);
3359 }
3360 else
3361 {
3362 star_seen = false;
267bac10 3363 dimen = c_parser_expr_no_commas (parser, NULL);
27bf414c
JM
3364 }
3365 }
3366 else
3367 {
3368 star_seen = false;
267bac10 3369 dimen = c_parser_expr_no_commas (parser, NULL);
27bf414c
JM
3370 }
3371 }
3372 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
3373 c_parser_consume_token (parser);
b72271b9 3374 else if (flag_cilkplus
36536d79
BI
3375 && c_parser_next_token_is (parser, CPP_COLON))
3376 {
3377 error_at (c_parser_peek_token (parser)->location,
3378 "array notations cannot be used in declaration");
3379 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
3380 return NULL;
3381 }
27bf414c
JM
3382 else
3383 {
3384 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
3385 "expected %<]%>");
3386 return NULL;
3387 }
267bac10
JM
3388 if (dimen.value)
3389 dimen = convert_lvalue_to_rvalue (brace_loc, dimen, true, true);
3390 declarator = build_array_declarator (brace_loc, dimen.value, quals_attrs,
c2255bc4 3391 static_seen, star_seen);
52ffd86e
MS
3392 if (declarator == NULL)
3393 return NULL;
6ac0194d 3394 inner = set_array_declarator_inner (declarator, inner);
27bf414c
JM
3395 return c_parser_direct_declarator_inner (parser, id_present, inner);
3396 }
3397 else if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
3398 {
3399 tree attrs;
3400 struct c_arg_info *args;
3401 c_parser_consume_token (parser);
3402 attrs = c_parser_attributes (parser);
3403 args = c_parser_parms_declarator (parser, id_present, attrs);
3404 if (args == NULL)
3405 return NULL;
3406 else
3407 {
3408 inner = build_function_declarator (args, inner);
3409 return c_parser_direct_declarator_inner (parser, id_present, inner);
3410 }
3411 }
3412 return inner;
3413}
3414
3415/* Parse a parameter list or identifier list, including the closing
3416 parenthesis but not the opening one. ATTRS are the attributes at
3417 the start of the list. ID_LIST_OK is true if an identifier list is
3418 acceptable; such a list must not have attributes at the start. */
3419
3420static struct c_arg_info *
3421c_parser_parms_declarator (c_parser *parser, bool id_list_ok, tree attrs)
3422{
3423 push_scope ();
3424 declare_parm_level ();
3425 /* If the list starts with an identifier, it is an identifier list.
3426 Otherwise, it is either a prototype list or an empty list. */
3427 if (id_list_ok
3428 && !attrs
3429 && c_parser_next_token_is (parser, CPP_NAME)
29ce73cb
PB
3430 && c_parser_peek_token (parser)->id_kind == C_ID_ID
3431
3432 /* Look ahead to detect typos in type names. */
3433 && c_parser_peek_2nd_token (parser)->type != CPP_NAME
3434 && c_parser_peek_2nd_token (parser)->type != CPP_MULT
3435 && c_parser_peek_2nd_token (parser)->type != CPP_OPEN_PAREN
3436 && c_parser_peek_2nd_token (parser)->type != CPP_OPEN_SQUARE)
27bf414c 3437 {
7fa3585c 3438 tree list = NULL_TREE, *nextp = &list;
27bf414c
JM
3439 while (c_parser_next_token_is (parser, CPP_NAME)
3440 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
3441 {
7fa3585c
GK
3442 *nextp = build_tree_list (NULL_TREE,
3443 c_parser_peek_token (parser)->value);
3444 nextp = & TREE_CHAIN (*nextp);
27bf414c
JM
3445 c_parser_consume_token (parser);
3446 if (c_parser_next_token_is_not (parser, CPP_COMMA))
3447 break;
3448 c_parser_consume_token (parser);
3449 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3450 {
3451 c_parser_error (parser, "expected identifier");
3452 break;
3453 }
3454 }
3455 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3456 {
b3399d18 3457 struct c_arg_info *ret = build_arg_info ();
27bf414c 3458 ret->types = list;
27bf414c
JM
3459 c_parser_consume_token (parser);
3460 pop_scope ();
3461 return ret;
3462 }
3463 else
3464 {
3465 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3466 "expected %<)%>");
3467 pop_scope ();
3468 return NULL;
3469 }
3470 }
3471 else
3472 {
a04a722b
JM
3473 struct c_arg_info *ret = c_parser_parms_list_declarator (parser, attrs,
3474 NULL);
27bf414c
JM
3475 pop_scope ();
3476 return ret;
3477 }
3478}
3479
3480/* Parse a parameter list (possibly empty), including the closing
3481 parenthesis but not the opening one. ATTRS are the attributes at
a04a722b
JM
3482 the start of the list. EXPR is NULL or an expression that needs to
3483 be evaluated for the side effects of array size expressions in the
3484 parameters. */
27bf414c
JM
3485
3486static struct c_arg_info *
a04a722b 3487c_parser_parms_list_declarator (c_parser *parser, tree attrs, tree expr)
27bf414c 3488{
09a1e889 3489 bool bad_parm = false;
a04a722b 3490
27bf414c
JM
3491 /* ??? Following the old parser, forward parameter declarations may
3492 use abstract declarators, and if no real parameter declarations
3493 follow the forward declarations then this is not diagnosed. Also
3494 note as above that attributes are ignored as the only contents of
3495 the parentheses, or as the only contents after forward
3496 declarations. */
3497 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3498 {
b3399d18 3499 struct c_arg_info *ret = build_arg_info ();
27bf414c
JM
3500 c_parser_consume_token (parser);
3501 return ret;
3502 }
3503 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
3504 {
b3399d18 3505 struct c_arg_info *ret = build_arg_info ();
6637388f
TG
3506
3507 if (flag_allow_parameterless_variadic_functions)
3508 {
3509 /* F (...) is allowed. */
3510 ret->types = NULL_TREE;
3511 }
3512 else
3513 {
3514 /* Suppress -Wold-style-definition for this case. */
3515 ret->types = error_mark_node;
3516 error_at (c_parser_peek_token (parser)->location,
3517 "ISO C requires a named argument before %<...%>");
3518 }
27bf414c
JM
3519 c_parser_consume_token (parser);
3520 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3521 {
3522 c_parser_consume_token (parser);
3523 return ret;
3524 }
3525 else
3526 {
3527 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3528 "expected %<)%>");
3529 return NULL;
3530 }
3531 }
3532 /* Nonempty list of parameters, either terminated with semicolon
3533 (forward declarations; recurse) or with close parenthesis (normal
3534 function) or with ", ... )" (variadic function). */
3535 while (true)
3536 {
3537 /* Parse a parameter. */
3538 struct c_parm *parm = c_parser_parameter_declaration (parser, attrs);
3539 attrs = NULL_TREE;
09a1e889
SZ
3540 if (parm == NULL)
3541 bad_parm = true;
3542 else
a04a722b 3543 push_parm_decl (parm, &expr);
27bf414c
JM
3544 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
3545 {
3546 tree new_attrs;
3547 c_parser_consume_token (parser);
91d975b8 3548 mark_forward_parm_decls ();
27bf414c 3549 new_attrs = c_parser_attributes (parser);
a04a722b 3550 return c_parser_parms_list_declarator (parser, new_attrs, expr);
27bf414c
JM
3551 }
3552 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3553 {
3554 c_parser_consume_token (parser);
09a1e889 3555 if (bad_parm)
a04a722b 3556 return NULL;
09a1e889 3557 else
a04a722b 3558 return get_parm_info (false, expr);
27bf414c
JM
3559 }
3560 if (!c_parser_require (parser, CPP_COMMA,
3561 "expected %<;%>, %<,%> or %<)%>"))
3562 {
3563 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
3564 return NULL;
3565 }
3566 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
3567 {
3568 c_parser_consume_token (parser);
3569 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3570 {
3571 c_parser_consume_token (parser);
09a1e889 3572 if (bad_parm)
a04a722b 3573 return NULL;
09a1e889 3574 else
a04a722b 3575 return get_parm_info (true, expr);
27bf414c
JM
3576 }
3577 else
3578 {
3579 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3580 "expected %<)%>");
3581 return NULL;
3582 }
3583 }
3584 }
3585}
3586
3587/* Parse a parameter declaration. ATTRS are the attributes at the
3588 start of the declaration if it is the first parameter. */
3589
3590static struct c_parm *
3591c_parser_parameter_declaration (c_parser *parser, tree attrs)
3592{
3593 struct c_declspecs *specs;
3594 struct c_declarator *declarator;
3595 tree prefix_attrs;
3596 tree postfix_attrs = NULL_TREE;
3597 bool dummy = false;
51a1aacf
TG
3598
3599 /* Accept #pragmas between parameter declarations. */
3600 while (c_parser_next_token_is (parser, CPP_PRAGMA))
acf0174b 3601 c_parser_pragma (parser, pragma_param);
51a1aacf 3602
27bf414c
JM
3603 if (!c_parser_next_token_starts_declspecs (parser))
3604 {
09a1e889
SZ
3605 c_token *token = c_parser_peek_token (parser);
3606 if (parser->error)
3607 return NULL;
3608 c_parser_set_source_position_from_token (token);
29ce73cb 3609 if (c_parser_next_tokens_start_typename (parser, cla_prefer_type))
09a1e889 3610 {
1c9f5f33 3611 error_at (token->location, "unknown type name %qE", token->value);
09a1e889
SZ
3612 parser->error = true;
3613 }
27bf414c
JM
3614 /* ??? In some Objective-C cases '...' isn't applicable so there
3615 should be a different message. */
09a1e889
SZ
3616 else
3617 c_parser_error (parser,
3618 "expected declaration specifiers or %<...%>");
27bf414c
JM
3619 c_parser_skip_to_end_of_parameter (parser);
3620 return NULL;
3621 }
3622 specs = build_null_declspecs ();
3623 if (attrs)
3624 {
0b2c4be5 3625 declspecs_add_attrs (input_location, specs, attrs);
27bf414c
JM
3626 attrs = NULL_TREE;
3627 }
38b7bc7f 3628 c_parser_declspecs (parser, specs, true, true, true, true, false,
568a31f2 3629 cla_nonabstract_decl);
27bf414c
JM
3630 finish_declspecs (specs);
3631 pending_xref_error ();
3632 prefix_attrs = specs->attrs;
3633 specs->attrs = NULL_TREE;
9e5b2115
PB
3634 declarator = c_parser_declarator (parser,
3635 specs->typespec_kind != ctsk_none,
27bf414c
JM
3636 C_DTR_PARM, &dummy);
3637 if (declarator == NULL)
3638 {
3639 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
3640 return NULL;
3641 }
3642 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
3643 postfix_attrs = c_parser_attributes (parser);
3644 return build_c_parm (specs, chainon (postfix_attrs, prefix_attrs),
3645 declarator);
3646}
3647
3648/* Parse a string literal in an asm expression. It should not be
3649 translated, and wide string literals are an error although
3650 permitted by the syntax. This is a GNU extension.
3651
3652 asm-string-literal:
3653 string-literal
3654
3655 ??? At present, following the old parser, the caller needs to have
46c2514e
TT
3656 set lex_untranslated_string to 1. It would be better to follow the
3657 C++ parser rather than using this kludge. */
27bf414c
JM
3658
3659static tree
3660c_parser_asm_string_literal (c_parser *parser)
3661{
3662 tree str;
87f9e23d
TT
3663 int save_flag = warn_overlength_strings;
3664 warn_overlength_strings = 0;
27bf414c
JM
3665 if (c_parser_next_token_is (parser, CPP_STRING))
3666 {
3667 str = c_parser_peek_token (parser)->value;
3668 c_parser_consume_token (parser);
3669 }
3670 else if (c_parser_next_token_is (parser, CPP_WSTRING))
3671 {
3ba09659
AH
3672 error_at (c_parser_peek_token (parser)->location,
3673 "wide string literal in %<asm%>");
27bf414c
JM
3674 str = build_string (1, "");
3675 c_parser_consume_token (parser);
3676 }
3677 else
3678 {
3679 c_parser_error (parser, "expected string literal");
3680 str = NULL_TREE;
3681 }
87f9e23d 3682 warn_overlength_strings = save_flag;
27bf414c
JM
3683 return str;
3684}
3685
3686/* Parse a simple asm expression. This is used in restricted
3687 contexts, where a full expression with inputs and outputs does not
3688 make sense. This is a GNU extension.
3689
3690 simple-asm-expr:
3691 asm ( asm-string-literal )
3692*/
3693
3694static tree
3695c_parser_simple_asm_expr (c_parser *parser)
3696{
3697 tree str;
3698 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ASM));
3699 /* ??? Follow the C++ parser rather than using the
46c2514e
TT
3700 lex_untranslated_string kludge. */
3701 parser->lex_untranslated_string = true;
27bf414c
JM
3702 c_parser_consume_token (parser);
3703 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3704 {
46c2514e 3705 parser->lex_untranslated_string = false;
27bf414c
JM
3706 return NULL_TREE;
3707 }
3708 str = c_parser_asm_string_literal (parser);
46c2514e 3709 parser->lex_untranslated_string = false;
27bf414c
JM
3710 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
3711 {
3712 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
3713 return NULL_TREE;
3714 }
3715 return str;
3716}
3717
0a35513e
AH
3718static tree
3719c_parser_attribute_any_word (c_parser *parser)
3720{
3721 tree attr_name = NULL_TREE;
3722
3723 if (c_parser_next_token_is (parser, CPP_KEYWORD))
3724 {
3725 /* ??? See comment above about what keywords are accepted here. */
3726 bool ok;
3727 switch (c_parser_peek_token (parser)->keyword)
3728 {
3729 case RID_STATIC:
3730 case RID_UNSIGNED:
3731 case RID_LONG:
3732 case RID_INT128:
3733 case RID_CONST:
3734 case RID_EXTERN:
3735 case RID_REGISTER:
3736 case RID_TYPEDEF:
3737 case RID_SHORT:
3738 case RID_INLINE:
3739 case RID_NORETURN:
3740 case RID_VOLATILE:
3741 case RID_SIGNED:
3742 case RID_AUTO:
3743 case RID_RESTRICT:
3744 case RID_COMPLEX:
3745 case RID_THREAD:
3746 case RID_INT:
3747 case RID_CHAR:
3748 case RID_FLOAT:
3749 case RID_DOUBLE:
3750 case RID_VOID:
3751 case RID_DFLOAT32:
3752 case RID_DFLOAT64:
3753 case RID_DFLOAT128:
3754 case RID_BOOL:
3755 case RID_FRACT:
3756 case RID_ACCUM:
3757 case RID_SAT:
3758 case RID_TRANSACTION_ATOMIC:
3759 case RID_TRANSACTION_CANCEL:
267bac10 3760 case RID_ATOMIC:
38b7bc7f 3761 case RID_AUTO_TYPE:
0a35513e
AH
3762 ok = true;
3763 break;
3764 default:
3765 ok = false;
3766 break;
3767 }
3768 if (!ok)
3769 return NULL_TREE;
3770
3771 /* Accept __attribute__((__const)) as __attribute__((const)) etc. */
3772 attr_name = ridpointers[(int) c_parser_peek_token (parser)->keyword];
3773 }
3774 else if (c_parser_next_token_is (parser, CPP_NAME))
3775 attr_name = c_parser_peek_token (parser)->value;
3776
3777 return attr_name;
3778}
3779
41958c28
BI
3780/* Returns true of NAME is an IDENTIFIER_NODE with identiifer "vector,"
3781 "__vector" or "__vector__." */
3782
3783static inline bool
3784is_cilkplus_vector_p (tree name)
3785{
b72271b9 3786 if (flag_cilkplus && is_attribute_p ("vector", name))
41958c28
BI
3787 return true;
3788 return false;
3789}
3790
3791#define CILK_SIMD_FN_CLAUSE_MASK \
3792 ((OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_VECTORLENGTH) \
3793 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_LINEAR) \
3794 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_UNIFORM) \
3795 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_MASK) \
3796 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_NOMASK))
3797
3798/* Parses the vector attribute of SIMD enabled functions in Cilk Plus.
3799 VEC_TOKEN is the "vector" token that is replaced with "simd" and
3800 pushed into the token list.
3801 Syntax:
3802 vector
3803 vector (<vector attributes>). */
3804
3805static void
3806c_parser_cilk_simd_fn_vector_attrs (c_parser *parser, c_token vec_token)
3807{
3808 gcc_assert (is_cilkplus_vector_p (vec_token.value));
3809
3810 int paren_scope = 0;
3811 vec_safe_push (parser->cilk_simd_fn_tokens, vec_token);
3812 /* Consume the "vector" token. */
3813 c_parser_consume_token (parser);
3814
3815 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
3816 {
3817 c_parser_consume_token (parser);
3818 paren_scope++;
3819 }
3820 while (paren_scope > 0)
3821 {
3822 c_token *token = c_parser_peek_token (parser);
3823 if (token->type == CPP_OPEN_PAREN)
3824 paren_scope++;
3825 else if (token->type == CPP_CLOSE_PAREN)
3826 paren_scope--;
3827 /* Do not push the last ')' since we are not pushing the '('. */
3828 if (!(token->type == CPP_CLOSE_PAREN && paren_scope == 0))
3829 vec_safe_push (parser->cilk_simd_fn_tokens, *token);
3830 c_parser_consume_token (parser);
3831 }
3832
3833 /* Since we are converting an attribute to a pragma, we need to end the
3834 attribute with PRAGMA_EOL. */
3835 c_token eol_token;
3836 memset (&eol_token, 0, sizeof (eol_token));
3837 eol_token.type = CPP_PRAGMA_EOL;
3838 vec_safe_push (parser->cilk_simd_fn_tokens, eol_token);
3839}
3840
3841/* Add 2 CPP_EOF at the end of PARSER->ELEM_FN_TOKENS vector. */
3842
3843static void
3844c_finish_cilk_simd_fn_tokens (c_parser *parser)
3845{
3846 c_token last_token = parser->cilk_simd_fn_tokens->last ();
3847
3848 /* c_parser_attributes is called in several places, so if these EOF
3849 tokens are already inserted, then don't do them again. */
3850 if (last_token.type == CPP_EOF)
3851 return;
3852
3853 /* Two CPP_EOF token are added as a safety net since the normal C
3854 front-end has two token look-ahead. */
3855 c_token eof_token;
3856 eof_token.type = CPP_EOF;
3857 vec_safe_push (parser->cilk_simd_fn_tokens, eof_token);
3858 vec_safe_push (parser->cilk_simd_fn_tokens, eof_token);
3859}
3860
27bf414c
JM
3861/* Parse (possibly empty) attributes. This is a GNU extension.
3862
3863 attributes:
3864 empty
3865 attributes attribute
3866
3867 attribute:
3868 __attribute__ ( ( attribute-list ) )
3869
3870 attribute-list:
3871 attrib
3872 attribute_list , attrib
3873
3874 attrib:
3875 empty
3876 any-word
3877 any-word ( identifier )
3878 any-word ( identifier , nonempty-expr-list )
3879 any-word ( expr-list )
3880
3881 where the "identifier" must not be declared as a type, and
3882 "any-word" may be any identifier (including one declared as a
3883 type), a reserved word storage class specifier, type specifier or
3884 type qualifier. ??? This still leaves out most reserved keywords
3885 (following the old parser), shouldn't we include them, and why not
3886 allow identifiers declared as types to start the arguments? */
3887
3888static tree
3889c_parser_attributes (c_parser *parser)
3890{
3891 tree attrs = NULL_TREE;
3892 while (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
3893 {
3894 /* ??? Follow the C++ parser rather than using the
46c2514e
TT
3895 lex_untranslated_string kludge. */
3896 parser->lex_untranslated_string = true;
27bf414c
JM
3897 c_parser_consume_token (parser);
3898 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3899 {
46c2514e 3900 parser->lex_untranslated_string = false;
27bf414c
JM
3901 return attrs;
3902 }
3903 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3904 {
46c2514e 3905 parser->lex_untranslated_string = false;
27bf414c
JM
3906 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
3907 return attrs;
3908 }
3909 /* Parse the attribute list. */
3910 while (c_parser_next_token_is (parser, CPP_COMMA)
3911 || c_parser_next_token_is (parser, CPP_NAME)
3912 || c_parser_next_token_is (parser, CPP_KEYWORD))
3913 {
3914 tree attr, attr_name, attr_args;
9771b263 3915 vec<tree, va_gc> *expr_list;
27bf414c
JM
3916 if (c_parser_next_token_is (parser, CPP_COMMA))
3917 {
3918 c_parser_consume_token (parser);
3919 continue;
3920 }
0a35513e
AH
3921
3922 attr_name = c_parser_attribute_any_word (parser);
3923 if (attr_name == NULL)
3924 break;
41958c28
BI
3925 if (is_cilkplus_vector_p (attr_name))
3926 {
3927 c_token *v_token = c_parser_peek_token (parser);
3928 c_parser_cilk_simd_fn_vector_attrs (parser, *v_token);
3929 continue;
3930 }
27bf414c
JM
3931 c_parser_consume_token (parser);
3932 if (c_parser_next_token_is_not (parser, CPP_OPEN_PAREN))
3933 {
3934 attr = build_tree_list (attr_name, NULL_TREE);
3935 attrs = chainon (attrs, attr);
3936 continue;
3937 }
3938 c_parser_consume_token (parser);
3939 /* Parse the attribute contents. If they start with an
3940 identifier which is followed by a comma or close
3941 parenthesis, then the arguments start with that
91ebb981
IS
3942 identifier; otherwise they are an expression list.
3943 In objective-c the identifier may be a classname. */
27bf414c 3944 if (c_parser_next_token_is (parser, CPP_NAME)
91ebb981
IS
3945 && (c_parser_peek_token (parser)->id_kind == C_ID_ID
3946 || (c_dialect_objc ()
3947 && c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME))
27bf414c
JM
3948 && ((c_parser_peek_2nd_token (parser)->type == CPP_COMMA)
3949 || (c_parser_peek_2nd_token (parser)->type
3950 == CPP_CLOSE_PAREN)))
3951 {
3952 tree arg1 = c_parser_peek_token (parser)->value;
3953 c_parser_consume_token (parser);
3954 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3955 attr_args = build_tree_list (NULL_TREE, arg1);
3956 else
3957 {
bbbbb16a 3958 tree tree_list;
27bf414c 3959 c_parser_consume_token (parser);
1a4049e7 3960 expr_list = c_parser_expr_list (parser, false, true,
81e5eca8 3961 NULL, NULL, NULL, NULL);
c166b898 3962 tree_list = build_tree_list_vec (expr_list);
bbbbb16a 3963 attr_args = tree_cons (NULL_TREE, arg1, tree_list);
c166b898 3964 release_tree_vector (expr_list);
27bf414c
JM
3965 }
3966 }
3967 else
3968 {
3969 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3970 attr_args = NULL_TREE;
3971 else
bbbbb16a 3972 {
1a4049e7 3973 expr_list = c_parser_expr_list (parser, false, true,
81e5eca8 3974 NULL, NULL, NULL, NULL);
c166b898
ILT
3975 attr_args = build_tree_list_vec (expr_list);
3976 release_tree_vector (expr_list);
bbbbb16a 3977 }
27bf414c
JM
3978 }
3979 attr = build_tree_list (attr_name, attr_args);
3980 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3981 c_parser_consume_token (parser);
3982 else
3983 {
46c2514e 3984 parser->lex_untranslated_string = false;
27bf414c
JM
3985 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3986 "expected %<)%>");
3987 return attrs;
3988 }
3989 attrs = chainon (attrs, attr);
3990 }
3991 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3992 c_parser_consume_token (parser);
3993 else
3994 {
46c2514e 3995 parser->lex_untranslated_string = false;
27bf414c
JM
3996 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3997 "expected %<)%>");
3998 return attrs;
3999 }
4000 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4001 c_parser_consume_token (parser);
4002 else
4003 {
46c2514e 4004 parser->lex_untranslated_string = false;
27bf414c
JM
4005 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
4006 "expected %<)%>");
4007 return attrs;
4008 }
46c2514e 4009 parser->lex_untranslated_string = false;
27bf414c 4010 }
41958c28 4011
b72271b9 4012 if (flag_cilkplus && !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
41958c28 4013 c_finish_cilk_simd_fn_tokens (parser);
27bf414c
JM
4014 return attrs;
4015}
4016
4017/* Parse a type name (C90 6.5.5, C99 6.7.6).
4018
4019 type-name:
4020 specifier-qualifier-list abstract-declarator[opt]
4021*/
4022
4023static struct c_type_name *
4024c_parser_type_name (c_parser *parser)
4025{
4026 struct c_declspecs *specs = build_null_declspecs ();
4027 struct c_declarator *declarator;
4028 struct c_type_name *ret;
4029 bool dummy = false;
38b7bc7f 4030 c_parser_declspecs (parser, specs, false, true, true, false, false,
568a31f2 4031 cla_prefer_type);
27bf414c
JM
4032 if (!specs->declspecs_seen_p)
4033 {
4034 c_parser_error (parser, "expected specifier-qualifier-list");
4035 return NULL;
4036 }
29ce73cb
PB
4037 if (specs->type != error_mark_node)
4038 {
4039 pending_xref_error ();
4040 finish_declspecs (specs);
4041 }
9e5b2115
PB
4042 declarator = c_parser_declarator (parser,
4043 specs->typespec_kind != ctsk_none,
27bf414c
JM
4044 C_DTR_ABSTRACT, &dummy);
4045 if (declarator == NULL)
4046 return NULL;
4047 ret = XOBNEW (&parser_obstack, struct c_type_name);
4048 ret->specs = specs;
4049 ret->declarator = declarator;
4050 return ret;
4051}
4052
4053/* Parse an initializer (C90 6.5.7, C99 6.7.8).
4054
4055 initializer:
4056 assignment-expression
4057 { initializer-list }
4058 { initializer-list , }
4059
4060 initializer-list:
4061 designation[opt] initializer
4062 initializer-list , designation[opt] initializer
4063
4064 designation:
4065 designator-list =
4066
4067 designator-list:
4068 designator
4069 designator-list designator
4070
4071 designator:
4072 array-designator
4073 . identifier
4074
4075 array-designator:
4076 [ constant-expression ]
4077
4078 GNU extensions:
4079
4080 initializer:
4081 { }
4082
4083 designation:
4084 array-designator
4085 identifier :
4086
4087 array-designator:
4088 [ constant-expression ... constant-expression ]
4089
4090 Any expression without commas is accepted in the syntax for the
4091 constant-expressions, with non-constant expressions rejected later.
4092
4093 This function is only used for top-level initializers; for nested
4094 ones, see c_parser_initval. */
4095
4096static struct c_expr
4097c_parser_initializer (c_parser *parser)
4098{
4099 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
4100 return c_parser_braced_init (parser, NULL_TREE, false);
4101 else
46bdb9cf
JM
4102 {
4103 struct c_expr ret;
c2255bc4 4104 location_t loc = c_parser_peek_token (parser)->location;
46bdb9cf
JM
4105 ret = c_parser_expr_no_commas (parser, NULL);
4106 if (TREE_CODE (ret.value) != STRING_CST
4107 && TREE_CODE (ret.value) != COMPOUND_LITERAL_EXPR)
267bac10 4108 ret = convert_lvalue_to_rvalue (loc, ret, true, true);
46bdb9cf
JM
4109 return ret;
4110 }
27bf414c
JM
4111}
4112
4113/* Parse a braced initializer list. TYPE is the type specified for a
4114 compound literal, and NULL_TREE for other initializers and for
4115 nested braced lists. NESTED_P is true for nested braced lists,
4116 false for the list of a compound literal or the list that is the
4117 top-level initializer in a declaration. */
4118
4119static struct c_expr
4120c_parser_braced_init (c_parser *parser, tree type, bool nested_p)
4121{
a1e3b3d9
LB
4122 struct c_expr ret;
4123 struct obstack braced_init_obstack;
c7412148 4124 location_t brace_loc = c_parser_peek_token (parser)->location;
a1e3b3d9 4125 gcc_obstack_init (&braced_init_obstack);
27bf414c
JM
4126 gcc_assert (c_parser_next_token_is (parser, CPP_OPEN_BRACE));
4127 c_parser_consume_token (parser);
4128 if (nested_p)
a1e3b3d9 4129 push_init_level (0, &braced_init_obstack);
27bf414c
JM
4130 else
4131 really_start_incremental_init (type);
4132 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
4133 {
c1771a20 4134 pedwarn (brace_loc, OPT_Wpedantic, "ISO C forbids empty initializer braces");
27bf414c
JM
4135 }
4136 else
4137 {
4138 /* Parse a non-empty initializer list, possibly with a trailing
4139 comma. */
4140 while (true)
4141 {
a1e3b3d9 4142 c_parser_initelt (parser, &braced_init_obstack);
27bf414c
JM
4143 if (parser->error)
4144 break;
4145 if (c_parser_next_token_is (parser, CPP_COMMA))
4146 c_parser_consume_token (parser);
4147 else
4148 break;
4149 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
4150 break;
4151 }
4152 }
4153 if (c_parser_next_token_is_not (parser, CPP_CLOSE_BRACE))
4154 {
27bf414c
JM
4155 ret.value = error_mark_node;
4156 ret.original_code = ERROR_MARK;
6866c6e8 4157 ret.original_type = NULL;
27bf414c 4158 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, "expected %<}%>");
a1e3b3d9
LB
4159 pop_init_level (0, &braced_init_obstack);
4160 obstack_free (&braced_init_obstack, NULL);
27bf414c
JM
4161 return ret;
4162 }
4163 c_parser_consume_token (parser);
a1e3b3d9
LB
4164 ret = pop_init_level (0, &braced_init_obstack);
4165 obstack_free (&braced_init_obstack, NULL);
4166 return ret;
27bf414c
JM
4167}
4168
4169/* Parse a nested initializer, including designators. */
4170
4171static void
a1e3b3d9 4172c_parser_initelt (c_parser *parser, struct obstack * braced_init_obstack)
27bf414c
JM
4173{
4174 /* Parse any designator or designator list. A single array
4175 designator may have the subsequent "=" omitted in GNU C, but a
4176 longer list or a structure member designator may not. */
4177 if (c_parser_next_token_is (parser, CPP_NAME)
4178 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
4179 {
4180 /* Old-style structure member designator. */
a1e3b3d9
LB
4181 set_init_label (c_parser_peek_token (parser)->value,
4182 braced_init_obstack);
fcf73884 4183 /* Use the colon as the error location. */
c1771a20 4184 pedwarn (c_parser_peek_2nd_token (parser)->location, OPT_Wpedantic,
509c9d60 4185 "obsolete use of designated initializer with %<:%>");
27bf414c
JM
4186 c_parser_consume_token (parser);
4187 c_parser_consume_token (parser);
4188 }
4189 else
4190 {
4191 /* des_seen is 0 if there have been no designators, 1 if there
4192 has been a single array designator and 2 otherwise. */
4193 int des_seen = 0;
c7412148 4194 /* Location of a designator. */
922f2908 4195 location_t des_loc = UNKNOWN_LOCATION; /* Quiet warning. */
27bf414c
JM
4196 while (c_parser_next_token_is (parser, CPP_OPEN_SQUARE)
4197 || c_parser_next_token_is (parser, CPP_DOT))
4198 {
4199 int des_prev = des_seen;
c7412148
TT
4200 if (!des_seen)
4201 des_loc = c_parser_peek_token (parser)->location;
27bf414c
JM
4202 if (des_seen < 2)
4203 des_seen++;
4204 if (c_parser_next_token_is (parser, CPP_DOT))
4205 {
4206 des_seen = 2;
4207 c_parser_consume_token (parser);
4208 if (c_parser_next_token_is (parser, CPP_NAME))
4209 {
a1e3b3d9
LB
4210 set_init_label (c_parser_peek_token (parser)->value,
4211 braced_init_obstack);
27bf414c
JM
4212 c_parser_consume_token (parser);
4213 }
4214 else
4215 {
4216 struct c_expr init;
4217 init.value = error_mark_node;
4218 init.original_code = ERROR_MARK;
6866c6e8 4219 init.original_type = NULL;
27bf414c
JM
4220 c_parser_error (parser, "expected identifier");
4221 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
a1e3b3d9 4222 process_init_element (init, false, braced_init_obstack);
27bf414c
JM
4223 return;
4224 }
4225 }
4226 else
4227 {
4228 tree first, second;
922f2908 4229 location_t ellipsis_loc = UNKNOWN_LOCATION; /* Quiet warning. */
27bf414c
JM
4230 /* ??? Following the old parser, [ objc-receiver
4231 objc-message-args ] is accepted as an initializer,
4232 being distinguished from a designator by what follows
4233 the first assignment expression inside the square
4234 brackets, but after a first array designator a
4235 subsequent square bracket is for Objective-C taken to
4236 start an expression, using the obsolete form of
4237 designated initializer without '=', rather than
4238 possibly being a second level of designation: in LALR
4239 terms, the '[' is shifted rather than reducing
4240 designator to designator-list. */
4241 if (des_prev == 1 && c_dialect_objc ())
4242 {
4243 des_seen = des_prev;
4244 break;
4245 }
4246 if (des_prev == 0 && c_dialect_objc ())
4247 {
4248 /* This might be an array designator or an
4249 Objective-C message expression. If the former,
4250 continue parsing here; if the latter, parse the
4251 remainder of the initializer given the starting
4252 primary-expression. ??? It might make sense to
4253 distinguish when des_prev == 1 as well; see
4254 previous comment. */
4255 tree rec, args;
4256 struct c_expr mexpr;
4257 c_parser_consume_token (parser);
4258 if (c_parser_peek_token (parser)->type == CPP_NAME
4259 && ((c_parser_peek_token (parser)->id_kind
4260 == C_ID_TYPENAME)
4261 || (c_parser_peek_token (parser)->id_kind
4262 == C_ID_CLASSNAME)))
4263 {
4264 /* Type name receiver. */
4265 tree id = c_parser_peek_token (parser)->value;
4266 c_parser_consume_token (parser);
4267 rec = objc_get_class_reference (id);
4268 goto parse_message_args;
4269 }
4270 first = c_parser_expr_no_commas (parser, NULL).value;
ebfbbdc5 4271 mark_exp_read (first);
27bf414c
JM
4272 if (c_parser_next_token_is (parser, CPP_ELLIPSIS)
4273 || c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
4274 goto array_desig_after_first;
4275 /* Expression receiver. So far only one part
4276 without commas has been parsed; there might be
4277 more of the expression. */
4278 rec = first;
4279 while (c_parser_next_token_is (parser, CPP_COMMA))
4280 {
f2a71bbc 4281 struct c_expr next;
c2255bc4
AH
4282 location_t comma_loc, exp_loc;
4283 comma_loc = c_parser_peek_token (parser)->location;
27bf414c 4284 c_parser_consume_token (parser);
c2255bc4 4285 exp_loc = c_parser_peek_token (parser)->location;
f2a71bbc 4286 next = c_parser_expr_no_commas (parser, NULL);
267bac10
JM
4287 next = convert_lvalue_to_rvalue (exp_loc, next,
4288 true, true);
c2255bc4 4289 rec = build_compound_expr (comma_loc, rec, next.value);
27bf414c
JM
4290 }
4291 parse_message_args:
4292 /* Now parse the objc-message-args. */
4293 args = c_parser_objc_message_args (parser);
4294 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
4295 "expected %<]%>");
4296 mexpr.value
eb345401 4297 = objc_build_message_expr (rec, args);
27bf414c 4298 mexpr.original_code = ERROR_MARK;
6866c6e8 4299 mexpr.original_type = NULL;
27bf414c
JM
4300 /* Now parse and process the remainder of the
4301 initializer, starting with this message
4302 expression as a primary-expression. */
a1e3b3d9 4303 c_parser_initval (parser, &mexpr, braced_init_obstack);
27bf414c
JM
4304 return;
4305 }
4306 c_parser_consume_token (parser);
4307 first = c_parser_expr_no_commas (parser, NULL).value;
ebfbbdc5 4308 mark_exp_read (first);
27bf414c
JM
4309 array_desig_after_first:
4310 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
4311 {
c7412148 4312 ellipsis_loc = c_parser_peek_token (parser)->location;
27bf414c
JM
4313 c_parser_consume_token (parser);
4314 second = c_parser_expr_no_commas (parser, NULL).value;
ebfbbdc5 4315 mark_exp_read (second);
27bf414c
JM
4316 }
4317 else
4318 second = NULL_TREE;
4319 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
4320 {
4321 c_parser_consume_token (parser);
a1e3b3d9 4322 set_init_index (first, second, braced_init_obstack);
fcf73884 4323 if (second)
c1771a20 4324 pedwarn (ellipsis_loc, OPT_Wpedantic,
509c9d60 4325 "ISO C forbids specifying range of elements to initialize");
27bf414c
JM
4326 }
4327 else
4328 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
4329 "expected %<]%>");
4330 }
4331 }
4332 if (des_seen >= 1)
4333 {
4334 if (c_parser_next_token_is (parser, CPP_EQ))
4335 {
fcf73884 4336 if (!flag_isoc99)
c1771a20 4337 pedwarn (des_loc, OPT_Wpedantic,
509c9d60 4338 "ISO C90 forbids specifying subobject to initialize");
27bf414c
JM
4339 c_parser_consume_token (parser);
4340 }
4341 else
4342 {
4343 if (des_seen == 1)
c1771a20 4344 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
509c9d60 4345 "obsolete use of designated initializer without %<=%>");
27bf414c
JM
4346 else
4347 {
4348 struct c_expr init;
4349 init.value = error_mark_node;
4350 init.original_code = ERROR_MARK;
6866c6e8 4351 init.original_type = NULL;
27bf414c
JM
4352 c_parser_error (parser, "expected %<=%>");
4353 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
a1e3b3d9 4354 process_init_element (init, false, braced_init_obstack);
27bf414c
JM
4355 return;
4356 }
4357 }
4358 }
4359 }
a1e3b3d9 4360 c_parser_initval (parser, NULL, braced_init_obstack);
27bf414c
JM
4361}
4362
4363/* Parse a nested initializer; as c_parser_initializer but parses
4364 initializers within braced lists, after any designators have been
4365 applied. If AFTER is not NULL then it is an Objective-C message
4366 expression which is the primary-expression starting the
4367 initializer. */
4368
4369static void
a1e3b3d9
LB
4370c_parser_initval (c_parser *parser, struct c_expr *after,
4371 struct obstack * braced_init_obstack)
27bf414c
JM
4372{
4373 struct c_expr init;
4374 gcc_assert (!after || c_dialect_objc ());
4375 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE) && !after)
4376 init = c_parser_braced_init (parser, NULL_TREE, true);
4377 else
46bdb9cf 4378 {
c2255bc4 4379 location_t loc = c_parser_peek_token (parser)->location;
46bdb9cf
JM
4380 init = c_parser_expr_no_commas (parser, after);
4381 if (init.value != NULL_TREE
4382 && TREE_CODE (init.value) != STRING_CST
4383 && TREE_CODE (init.value) != COMPOUND_LITERAL_EXPR)
267bac10 4384 init = convert_lvalue_to_rvalue (loc, init, true, true);
46bdb9cf 4385 }
a1e3b3d9 4386 process_init_element (init, false, braced_init_obstack);
27bf414c
JM
4387}
4388
4389/* Parse a compound statement (possibly a function body) (C90 6.6.2,
4390 C99 6.8.2).
4391
4392 compound-statement:
4393 { block-item-list[opt] }
4394 { label-declarations block-item-list }
4395
4396 block-item-list:
4397 block-item
4398 block-item-list block-item
4399
4400 block-item:
4401 nested-declaration
4402 statement
4403
4404 nested-declaration:
4405 declaration
4406
4407 GNU extensions:
4408
4409 compound-statement:
4410 { label-declarations block-item-list }
4411
4412 nested-declaration:
4413 __extension__ nested-declaration
4414 nested-function-definition
4415
4416 label-declarations:
4417 label-declaration
4418 label-declarations label-declaration
4419
4420 label-declaration:
4421 __label__ identifier-list ;
4422
4423 Allowing the mixing of declarations and code is new in C99. The
4424 GNU syntax also permits (not shown above) labels at the end of
4425 compound statements, which yield an error. We don't allow labels
4426 on declarations; this might seem like a natural extension, but
4427 there would be a conflict between attributes on the label and
4428 prefix attributes on the declaration. ??? The syntax follows the
4429 old parser in requiring something after label declarations.
4430 Although they are erroneous if the labels declared aren't defined,
953ff289 4431 is it useful for the syntax to be this way?
b8698a0f 4432
953ff289 4433 OpenMP:
b8698a0f 4434
953ff289
DN
4435 block-item:
4436 openmp-directive
4437
4438 openmp-directive:
4439 barrier-directive
acf0174b
JJ
4440 flush-directive
4441 taskwait-directive
4442 taskyield-directive
4443 cancel-directive
4444 cancellation-point-directive */
27bf414c
JM
4445
4446static tree
4447c_parser_compound_statement (c_parser *parser)
4448{
4449 tree stmt;
c2255bc4
AH
4450 location_t brace_loc;
4451 brace_loc = c_parser_peek_token (parser)->location;
27bf414c 4452 if (!c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
5600f233
JM
4453 {
4454 /* Ensure a scope is entered and left anyway to avoid confusion
4455 if we have just prepared to enter a function body. */
4456 stmt = c_begin_compound_stmt (true);
c2255bc4 4457 c_end_compound_stmt (brace_loc, stmt, true);
5600f233
JM
4458 return error_mark_node;
4459 }
27bf414c
JM
4460 stmt = c_begin_compound_stmt (true);
4461 c_parser_compound_statement_nostart (parser);
36536d79
BI
4462
4463 /* If the compound stmt contains array notations, then we expand them. */
b72271b9 4464 if (flag_cilkplus && contains_array_notation_expr (stmt))
36536d79 4465 stmt = expand_array_notation_exprs (stmt);
c2255bc4 4466 return c_end_compound_stmt (brace_loc, stmt, true);
27bf414c
JM
4467}
4468
4469/* Parse a compound statement except for the opening brace. This is
4470 used for parsing both compound statements and statement expressions
4471 (which follow different paths to handling the opening). */
4472
4473static void
4474c_parser_compound_statement_nostart (c_parser *parser)
4475{
4476 bool last_stmt = false;
4477 bool last_label = false;
6ec637a4 4478 bool save_valid_for_pragma = valid_location_for_stdc_pragma_p ();
3ba09659 4479 location_t label_loc = UNKNOWN_LOCATION; /* Quiet warning. */
27bf414c
JM
4480 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
4481 {
4482 c_parser_consume_token (parser);
4483 return;
4484 }
6ec637a4 4485 mark_valid_location_for_stdc_pragma (true);
27bf414c
JM
4486 if (c_parser_next_token_is_keyword (parser, RID_LABEL))
4487 {
4488 /* Read zero or more forward-declarations for labels that nested
4489 functions can jump to. */
6ec637a4 4490 mark_valid_location_for_stdc_pragma (false);
27bf414c
JM
4491 while (c_parser_next_token_is_keyword (parser, RID_LABEL))
4492 {
c2255bc4 4493 label_loc = c_parser_peek_token (parser)->location;
27bf414c
JM
4494 c_parser_consume_token (parser);
4495 /* Any identifiers, including those declared as type names,
4496 are OK here. */
4497 while (true)
4498 {
4499 tree label;
4500 if (c_parser_next_token_is_not (parser, CPP_NAME))
4501 {
4502 c_parser_error (parser, "expected identifier");
4503 break;
4504 }
4505 label
4506 = declare_label (c_parser_peek_token (parser)->value);
4507 C_DECLARED_LABEL_FLAG (label) = 1;
c2255bc4 4508 add_stmt (build_stmt (label_loc, DECL_EXPR, label));
27bf414c
JM
4509 c_parser_consume_token (parser);
4510 if (c_parser_next_token_is (parser, CPP_COMMA))
4511 c_parser_consume_token (parser);
4512 else
4513 break;
4514 }
4515 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
4516 }
c1771a20 4517 pedwarn (label_loc, OPT_Wpedantic, "ISO C forbids label declarations");
27bf414c
JM
4518 }
4519 /* We must now have at least one statement, label or declaration. */
4520 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
4521 {
6ec637a4 4522 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
27bf414c
JM
4523 c_parser_error (parser, "expected declaration or statement");
4524 c_parser_consume_token (parser);
4525 return;
4526 }
4527 while (c_parser_next_token_is_not (parser, CPP_CLOSE_BRACE))
4528 {
4529 location_t loc = c_parser_peek_token (parser)->location;
27bf414c
JM
4530 if (c_parser_next_token_is_keyword (parser, RID_CASE)
4531 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
4532 || (c_parser_next_token_is (parser, CPP_NAME)
4533 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
4534 {
c7412148
TT
4535 if (c_parser_next_token_is_keyword (parser, RID_CASE))
4536 label_loc = c_parser_peek_2nd_token (parser)->location;
4537 else
4538 label_loc = c_parser_peek_token (parser)->location;
27bf414c
JM
4539 last_label = true;
4540 last_stmt = false;
6ec637a4 4541 mark_valid_location_for_stdc_pragma (false);
27bf414c
JM
4542 c_parser_label (parser);
4543 }
4544 else if (!last_label
2f413185 4545 && c_parser_next_tokens_start_declaration (parser))
27bf414c
JM
4546 {
4547 last_label = false;
6ec637a4 4548 mark_valid_location_for_stdc_pragma (false);
acf0174b
JJ
4549 c_parser_declaration_or_fndef (parser, true, true, true, true,
4550 true, NULL, vNULL);
fcf73884 4551 if (last_stmt)
b8698a0f 4552 pedwarn_c90 (loc,
509c9d60 4553 (pedantic && !flag_isoc99)
c1771a20 4554 ? OPT_Wpedantic
fcf73884 4555 : OPT_Wdeclaration_after_statement,
509c9d60 4556 "ISO C90 forbids mixed declarations and code");
27bf414c
JM
4557 last_stmt = false;
4558 }
4559 else if (!last_label
4560 && c_parser_next_token_is_keyword (parser, RID_EXTENSION))
4561 {
4562 /* __extension__ can start a declaration, but is also an
4563 unary operator that can start an expression. Consume all
4564 but the last of a possible series of __extension__ to
4565 determine which. */
4566 while (c_parser_peek_2nd_token (parser)->type == CPP_KEYWORD
4567 && (c_parser_peek_2nd_token (parser)->keyword
4568 == RID_EXTENSION))
4569 c_parser_consume_token (parser);
32912286 4570 if (c_token_starts_declaration (c_parser_peek_2nd_token (parser)))
27bf414c
JM
4571 {
4572 int ext;
4573 ext = disable_extension_diagnostics ();
4574 c_parser_consume_token (parser);
4575 last_label = false;
6ec637a4 4576 mark_valid_location_for_stdc_pragma (false);
32912286 4577 c_parser_declaration_or_fndef (parser, true, true, true, true,
acf0174b 4578 true, NULL, vNULL);
27bf414c
JM
4579 /* Following the old parser, __extension__ does not
4580 disable this diagnostic. */
4581 restore_extension_diagnostics (ext);
fcf73884 4582 if (last_stmt)
509c9d60 4583 pedwarn_c90 (loc, (pedantic && !flag_isoc99)
c1771a20 4584 ? OPT_Wpedantic
fcf73884 4585 : OPT_Wdeclaration_after_statement,
509c9d60 4586 "ISO C90 forbids mixed declarations and code");
27bf414c
JM
4587 last_stmt = false;
4588 }
4589 else
4590 goto statement;
4591 }
bc4071dd
RH
4592 else if (c_parser_next_token_is (parser, CPP_PRAGMA))
4593 {
4594 /* External pragmas, and some omp pragmas, are not associated
4595 with regular c code, and so are not to be considered statements
4596 syntactically. This ensures that the user doesn't put them
4597 places that would turn into syntax errors if the directive
4598 were ignored. */
4599 if (c_parser_pragma (parser, pragma_compound))
4600 last_label = false, last_stmt = true;
4601 }
4602 else if (c_parser_next_token_is (parser, CPP_EOF))
4603 {
6ec637a4 4604 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
bc4071dd
RH
4605 c_parser_error (parser, "expected declaration or statement");
4606 return;
4607 }
b4b56033
MLI
4608 else if (c_parser_next_token_is_keyword (parser, RID_ELSE))
4609 {
b8698a0f 4610 if (parser->in_if_block)
b4b56033 4611 {
6ec637a4 4612 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
3ba09659 4613 error_at (loc, """expected %<}%> before %<else%>");
b4b56033
MLI
4614 return;
4615 }
b8698a0f 4616 else
b4b56033 4617 {
3ba09659 4618 error_at (loc, "%<else%> without a previous %<if%>");
b4b56033
MLI
4619 c_parser_consume_token (parser);
4620 continue;
4621 }
4622 }
27bf414c
JM
4623 else
4624 {
4625 statement:
4626 last_label = false;
4627 last_stmt = true;
6ec637a4 4628 mark_valid_location_for_stdc_pragma (false);
27bf414c
JM
4629 c_parser_statement_after_labels (parser);
4630 }
2c14ae9a
VR
4631
4632 parser->error = false;
27bf414c
JM
4633 }
4634 if (last_label)
3ba09659 4635 error_at (label_loc, "label at end of compound statement");
27bf414c 4636 c_parser_consume_token (parser);
6ec637a4
JJ
4637 /* Restore the value we started with. */
4638 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
27bf414c
JM
4639}
4640
4641/* Parse a label (C90 6.6.1, C99 6.8.1).
4642
4643 label:
4644 identifier : attributes[opt]
4645 case constant-expression :
4646 default :
4647
4648 GNU extensions:
4649
4650 label:
4651 case constant-expression ... constant-expression :
4652
4653 The use of attributes on labels is a GNU extension. The syntax in
4654 GNU C accepts any expressions without commas, non-constant
4655 expressions being rejected later. */
4656
4657static void
4658c_parser_label (c_parser *parser)
4659{
4660 location_t loc1 = c_parser_peek_token (parser)->location;
4661 tree label = NULL_TREE;
4662 if (c_parser_next_token_is_keyword (parser, RID_CASE))
4663 {
4664 tree exp1, exp2;
4665 c_parser_consume_token (parser);
4666 exp1 = c_parser_expr_no_commas (parser, NULL).value;
4667 if (c_parser_next_token_is (parser, CPP_COLON))
4668 {
4669 c_parser_consume_token (parser);
c2255bc4 4670 label = do_case (loc1, exp1, NULL_TREE);
27bf414c
JM
4671 }
4672 else if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
4673 {
4674 c_parser_consume_token (parser);
4675 exp2 = c_parser_expr_no_commas (parser, NULL).value;
4676 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
c2255bc4 4677 label = do_case (loc1, exp1, exp2);
27bf414c
JM
4678 }
4679 else
4680 c_parser_error (parser, "expected %<:%> or %<...%>");
4681 }
4682 else if (c_parser_next_token_is_keyword (parser, RID_DEFAULT))
4683 {
4684 c_parser_consume_token (parser);
4685 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
c2255bc4 4686 label = do_case (loc1, NULL_TREE, NULL_TREE);
27bf414c
JM
4687 }
4688 else
4689 {
4690 tree name = c_parser_peek_token (parser)->value;
4691 tree tlab;
27bf414c 4692 tree attrs;
c7412148 4693 location_t loc2 = c_parser_peek_token (parser)->location;
27bf414c
JM
4694 gcc_assert (c_parser_next_token_is (parser, CPP_NAME));
4695 c_parser_consume_token (parser);
4696 gcc_assert (c_parser_next_token_is (parser, CPP_COLON));
27bf414c
JM
4697 c_parser_consume_token (parser);
4698 attrs = c_parser_attributes (parser);
4699 tlab = define_label (loc2, name);
4700 if (tlab)
4701 {
4702 decl_attributes (&tlab, attrs, 0);
c2255bc4 4703 label = add_stmt (build_stmt (loc1, LABEL_EXPR, tlab));
27bf414c
JM
4704 }
4705 }
4706 if (label)
3d57f0f0 4707 {
2f413185 4708 if (c_parser_next_tokens_start_declaration (parser))
3d57f0f0 4709 {
3ba09659
AH
4710 error_at (c_parser_peek_token (parser)->location,
4711 "a label can only be part of a statement and "
4712 "a declaration is not a statement");
b8698a0f 4713 c_parser_declaration_or_fndef (parser, /*fndef_ok*/ false,
32912286 4714 /*static_assert_ok*/ true,
6265d07c 4715 /*empty_ok*/ true, /*nested*/ true,
acf0174b
JJ
4716 /*start_attr_ok*/ true, NULL,
4717 vNULL);
3d57f0f0
MLI
4718 }
4719 }
27bf414c
JM
4720}
4721
4722/* Parse a statement (C90 6.6, C99 6.8).
4723
4724 statement:
4725 labeled-statement
4726 compound-statement
4727 expression-statement
4728 selection-statement
4729 iteration-statement
4730 jump-statement
4731
4732 labeled-statement:
4733 label statement
4734
4735 expression-statement:
4736 expression[opt] ;
4737
4738 selection-statement:
4739 if-statement
4740 switch-statement
4741
4742 iteration-statement:
4743 while-statement
4744 do-statement
4745 for-statement
4746
4747 jump-statement:
4748 goto identifier ;
4749 continue ;
4750 break ;
4751 return expression[opt] ;
4752
4753 GNU extensions:
4754
4755 statement:
4756 asm-statement
4757
4758 jump-statement:
4759 goto * expression ;
4760
4761 Objective-C:
4762
4763 statement:
4764 objc-throw-statement
4765 objc-try-catch-statement
4766 objc-synchronized-statement
4767
4768 objc-throw-statement:
4769 @throw expression ;
4770 @throw ;
953ff289
DN
4771
4772 OpenMP:
4773
4774 statement:
4775 openmp-construct
4776
4777 openmp-construct:
4778 parallel-construct
4779 for-construct
acf0174b
JJ
4780 simd-construct
4781 for-simd-construct
953ff289
DN
4782 sections-construct
4783 single-construct
4784 parallel-for-construct
acf0174b 4785 parallel-for-simd-construct
953ff289
DN
4786 parallel-sections-construct
4787 master-construct
4788 critical-construct
4789 atomic-construct
4790 ordered-construct
4791
4792 parallel-construct:
4793 parallel-directive structured-block
4794
4795 for-construct:
4796 for-directive iteration-statement
4797
acf0174b
JJ
4798 simd-construct:
4799 simd-directive iteration-statements
4800
4801 for-simd-construct:
4802 for-simd-directive iteration-statements
4803
953ff289
DN
4804 sections-construct:
4805 sections-directive section-scope
4806
4807 single-construct:
4808 single-directive structured-block
4809
4810 parallel-for-construct:
4811 parallel-for-directive iteration-statement
4812
acf0174b
JJ
4813 parallel-for-simd-construct:
4814 parallel-for-simd-directive iteration-statement
4815
953ff289
DN
4816 parallel-sections-construct:
4817 parallel-sections-directive section-scope
4818
4819 master-construct:
4820 master-directive structured-block
4821
4822 critical-construct:
4823 critical-directive structured-block
4824
4825 atomic-construct:
4826 atomic-directive expression-statement
4827
4828 ordered-construct:
0a35513e
AH
4829 ordered-directive structured-block
4830
4831 Transactional Memory:
4832
4833 statement:
4834 transaction-statement
4835 transaction-cancel-statement
4836*/
27bf414c
JM
4837
4838static void
4839c_parser_statement (c_parser *parser)
4840{
4841 while (c_parser_next_token_is_keyword (parser, RID_CASE)
4842 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
4843 || (c_parser_next_token_is (parser, CPP_NAME)
4844 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
4845 c_parser_label (parser);
4846 c_parser_statement_after_labels (parser);
4847}
4848
4849/* Parse a statement, other than a labeled statement. */
4850
4851static void
4852c_parser_statement_after_labels (c_parser *parser)
4853{
4854 location_t loc = c_parser_peek_token (parser)->location;
4855 tree stmt = NULL_TREE;
b4b56033
MLI
4856 bool in_if_block = parser->in_if_block;
4857 parser->in_if_block = false;
27bf414c
JM
4858 switch (c_parser_peek_token (parser)->type)
4859 {
4860 case CPP_OPEN_BRACE:
4861 add_stmt (c_parser_compound_statement (parser));
4862 break;
4863 case CPP_KEYWORD:
4864 switch (c_parser_peek_token (parser)->keyword)
4865 {
4866 case RID_IF:
4867 c_parser_if_statement (parser);
4868 break;
4869 case RID_SWITCH:
4870 c_parser_switch_statement (parser);
4871 break;
4872 case RID_WHILE:
d4af74d4 4873 c_parser_while_statement (parser, false);
27bf414c
JM
4874 break;
4875 case RID_DO:
d4af74d4 4876 c_parser_do_statement (parser, false);
27bf414c
JM
4877 break;
4878 case RID_FOR:
8170608b 4879 c_parser_for_statement (parser, false);
27bf414c 4880 break;
939b37da
BI
4881 case RID_CILK_SYNC:
4882 c_parser_consume_token (parser);
4883 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
b72271b9 4884 if (!flag_cilkplus)
939b37da
BI
4885 error_at (loc, "-fcilkplus must be enabled to use %<_Cilk_sync%>");
4886 else
4887 add_stmt (build_cilk_sync ());
4888 break;
27bf414c
JM
4889 case RID_GOTO:
4890 c_parser_consume_token (parser);
4891 if (c_parser_next_token_is (parser, CPP_NAME))
4892 {
c2255bc4
AH
4893 stmt = c_finish_goto_label (loc,
4894 c_parser_peek_token (parser)->value);
27bf414c
JM
4895 c_parser_consume_token (parser);
4896 }
4897 else if (c_parser_next_token_is (parser, CPP_MULT))
4898 {
267bac10 4899 struct c_expr val;
84628aa8 4900
27bf414c 4901 c_parser_consume_token (parser);
267bac10
JM
4902 val = c_parser_expression (parser);
4903 val = convert_lvalue_to_rvalue (loc, val, false, true);
4904 stmt = c_finish_goto_ptr (loc, val.value);
27bf414c
JM
4905 }
4906 else
4907 c_parser_error (parser, "expected identifier or %<*%>");
4908 goto expect_semicolon;
4909 case RID_CONTINUE:
4910 c_parser_consume_token (parser);
c2255bc4 4911 stmt = c_finish_bc_stmt (loc, &c_cont_label, false);
27bf414c
JM
4912 goto expect_semicolon;
4913 case RID_BREAK:
4914 c_parser_consume_token (parser);
c2255bc4 4915 stmt = c_finish_bc_stmt (loc, &c_break_label, true);
27bf414c
JM
4916 goto expect_semicolon;
4917 case RID_RETURN:
4918 c_parser_consume_token (parser);
4919 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
4920 {
c2255bc4 4921 stmt = c_finish_return (loc, NULL_TREE, NULL_TREE);
27bf414c
JM
4922 c_parser_consume_token (parser);
4923 }
4924 else
4925 {
bbbbb16a 4926 struct c_expr expr = c_parser_expression_conv (parser);
ebfbbdc5 4927 mark_exp_read (expr.value);
c2255bc4 4928 stmt = c_finish_return (loc, expr.value, expr.original_type);
27bf414c
JM
4929 goto expect_semicolon;
4930 }
4931 break;
4932 case RID_ASM:
4933 stmt = c_parser_asm_statement (parser);
4934 break;
0a35513e
AH
4935 case RID_TRANSACTION_ATOMIC:
4936 case RID_TRANSACTION_RELAXED:
4937 stmt = c_parser_transaction (parser,
4938 c_parser_peek_token (parser)->keyword);
4939 break;
4940 case RID_TRANSACTION_CANCEL:
4941 stmt = c_parser_transaction_cancel (parser);
4942 goto expect_semicolon;
49b91f05 4943 case RID_AT_THROW:
27bf414c
JM
4944 gcc_assert (c_dialect_objc ());
4945 c_parser_consume_token (parser);
4946 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
4947 {
c2255bc4 4948 stmt = objc_build_throw_stmt (loc, NULL_TREE);
27bf414c
JM
4949 c_parser_consume_token (parser);
4950 }
4951 else
4952 {
267bac10
JM
4953 struct c_expr expr = c_parser_expression (parser);
4954 expr = convert_lvalue_to_rvalue (loc, expr, false, false);
4955 expr.value = c_fully_fold (expr.value, false, NULL);
4956 stmt = objc_build_throw_stmt (loc, expr.value);
27bf414c
JM
4957 goto expect_semicolon;
4958 }
4959 break;
49b91f05 4960 case RID_AT_TRY:
27bf414c 4961 gcc_assert (c_dialect_objc ());
437c2322 4962 c_parser_objc_try_catch_finally_statement (parser);
27bf414c
JM
4963 break;
4964 case RID_AT_SYNCHRONIZED:
4965 gcc_assert (c_dialect_objc ());
4966 c_parser_objc_synchronized_statement (parser);
4967 break;
4968 default:
4969 goto expr_stmt;
4970 }
4971 break;
4972 case CPP_SEMICOLON:
4973 c_parser_consume_token (parser);
4974 break;
4975 case CPP_CLOSE_PAREN:
4976 case CPP_CLOSE_SQUARE:
4977 /* Avoid infinite loop in error recovery:
4978 c_parser_skip_until_found stops at a closing nesting
4979 delimiter without consuming it, but here we need to consume
4980 it to proceed further. */
4981 c_parser_error (parser, "expected statement");
4982 c_parser_consume_token (parser);
4983 break;
bc4071dd
RH
4984 case CPP_PRAGMA:
4985 c_parser_pragma (parser, pragma_stmt);
4986 break;
27bf414c
JM
4987 default:
4988 expr_stmt:
c2255bc4 4989 stmt = c_finish_expr_stmt (loc, c_parser_expression_conv (parser).value);
27bf414c
JM
4990 expect_semicolon:
4991 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
4992 break;
4993 }
4994 /* Two cases cannot and do not have line numbers associated: If stmt
4995 is degenerate, such as "2;", then stmt is an INTEGER_CST, which
4996 cannot hold line numbers. But that's OK because the statement
4997 will either be changed to a MODIFY_EXPR during gimplification of
4998 the statement expr, or discarded. If stmt was compound, but
4999 without new variables, we will have skipped the creation of a
5000 BIND and will have a bare STATEMENT_LIST. But that's OK because
5001 (recursively) all of the component statements should already have
5002 line numbers assigned. ??? Can we discard no-op statements
5003 earlier? */
c2255bc4
AH
5004 if (CAN_HAVE_LOCATION_P (stmt)
5005 && EXPR_LOCATION (stmt) == UNKNOWN_LOCATION)
5006 SET_EXPR_LOCATION (stmt, loc);
b4b56033
MLI
5007
5008 parser->in_if_block = in_if_block;
27bf414c
JM
5009}
5010
ca085fd7
MLI
5011/* Parse the condition from an if, do, while or for statements. */
5012
5013static tree
5014c_parser_condition (c_parser *parser)
5015{
c2255bc4 5016 location_t loc = c_parser_peek_token (parser)->location;
ca085fd7 5017 tree cond;
928c19bb
JM
5018 cond = c_parser_expression_conv (parser).value;
5019 cond = c_objc_common_truthvalue_conversion (loc, cond);
5020 cond = c_fully_fold (cond, false, NULL);
ca085fd7
MLI
5021 if (warn_sequence_point)
5022 verify_sequence_points (cond);
5023 return cond;
5024}
5025
27bf414c
JM
5026/* Parse a parenthesized condition from an if, do or while statement.
5027
5028 condition:
5029 ( expression )
5030*/
5031static tree
5032c_parser_paren_condition (c_parser *parser)
5033{
27bf414c
JM
5034 tree cond;
5035 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5036 return error_mark_node;
ca085fd7 5037 cond = c_parser_condition (parser);
27bf414c
JM
5038 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
5039 return cond;
5040}
5041
5042/* Parse a statement which is a block in C99. */
5043
5044static tree
5045c_parser_c99_block_statement (c_parser *parser)
5046{
5047 tree block = c_begin_compound_stmt (flag_isoc99);
c2255bc4 5048 location_t loc = c_parser_peek_token (parser)->location;
27bf414c 5049 c_parser_statement (parser);
c2255bc4 5050 return c_end_compound_stmt (loc, block, flag_isoc99);
27bf414c
JM
5051}
5052
b4b56033
MLI
5053/* Parse the body of an if statement. This is just parsing a
5054 statement but (a) it is a block in C99, (b) we track whether the
5055 body is an if statement for the sake of -Wparentheses warnings, (c)
5056 we handle an empty body specially for the sake of -Wempty-body
5057 warnings, and (d) we call parser_compound_statement directly
5058 because c_parser_statement_after_labels resets
5059 parser->in_if_block. */
27bf414c
JM
5060
5061static tree
5062c_parser_if_body (c_parser *parser, bool *if_p)
5063{
5064 tree block = c_begin_compound_stmt (flag_isoc99);
c2255bc4 5065 location_t body_loc = c_parser_peek_token (parser)->location;
27bf414c
JM
5066 while (c_parser_next_token_is_keyword (parser, RID_CASE)
5067 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
5068 || (c_parser_next_token_is (parser, CPP_NAME)
5069 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
5070 c_parser_label (parser);
5071 *if_p = c_parser_next_token_is_keyword (parser, RID_IF);
62e00e94 5072 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
b4b56033 5073 {
626c34b5 5074 location_t loc = c_parser_peek_token (parser)->location;
c2255bc4 5075 add_stmt (build_empty_stmt (loc));
b4b56033 5076 c_parser_consume_token (parser);
626c34b5
PB
5077 if (!c_parser_next_token_is_keyword (parser, RID_ELSE))
5078 warning_at (loc, OPT_Wempty_body,
5079 "suggest braces around empty body in an %<if%> statement");
b4b56033
MLI
5080 }
5081 else if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
5082 add_stmt (c_parser_compound_statement (parser));
5083 else
5084 c_parser_statement_after_labels (parser);
c2255bc4 5085 return c_end_compound_stmt (body_loc, block, flag_isoc99);
b4b56033
MLI
5086}
5087
5088/* Parse the else body of an if statement. This is just parsing a
5089 statement but (a) it is a block in C99, (b) we handle an empty body
5090 specially for the sake of -Wempty-body warnings. */
5091
5092static tree
5093c_parser_else_body (c_parser *parser)
5094{
c2255bc4 5095 location_t else_loc = c_parser_peek_token (parser)->location;
b4b56033
MLI
5096 tree block = c_begin_compound_stmt (flag_isoc99);
5097 while (c_parser_next_token_is_keyword (parser, RID_CASE)
5098 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
5099 || (c_parser_next_token_is (parser, CPP_NAME)
5100 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
5101 c_parser_label (parser);
5102 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5103 {
c2255bc4
AH
5104 location_t loc = c_parser_peek_token (parser)->location;
5105 warning_at (loc,
626c34b5
PB
5106 OPT_Wempty_body,
5107 "suggest braces around empty body in an %<else%> statement");
c2255bc4 5108 add_stmt (build_empty_stmt (loc));
b4b56033
MLI
5109 c_parser_consume_token (parser);
5110 }
b8698a0f 5111 else
b4b56033 5112 c_parser_statement_after_labels (parser);
c2255bc4 5113 return c_end_compound_stmt (else_loc, block, flag_isoc99);
27bf414c
JM
5114}
5115
5116/* Parse an if statement (C90 6.6.4, C99 6.8.4).
5117
5118 if-statement:
5119 if ( expression ) statement
5120 if ( expression ) statement else statement
5121*/
5122
5123static void
5124c_parser_if_statement (c_parser *parser)
5125{
5126 tree block;
5127 location_t loc;
5128 tree cond;
b4b56033 5129 bool first_if = false;
27bf414c 5130 tree first_body, second_body;
b4b56033 5131 bool in_if_block;
36536d79 5132 tree if_stmt;
b4b56033 5133
27bf414c
JM
5134 gcc_assert (c_parser_next_token_is_keyword (parser, RID_IF));
5135 c_parser_consume_token (parser);
5136 block = c_begin_compound_stmt (flag_isoc99);
5137 loc = c_parser_peek_token (parser)->location;
5138 cond = c_parser_paren_condition (parser);
b4b56033
MLI
5139 in_if_block = parser->in_if_block;
5140 parser->in_if_block = true;
27bf414c 5141 first_body = c_parser_if_body (parser, &first_if);
b4b56033 5142 parser->in_if_block = in_if_block;
27bf414c
JM
5143 if (c_parser_next_token_is_keyword (parser, RID_ELSE))
5144 {
5145 c_parser_consume_token (parser);
b4b56033 5146 second_body = c_parser_else_body (parser);
27bf414c
JM
5147 }
5148 else
5149 second_body = NULL_TREE;
5150 c_finish_if_stmt (loc, cond, first_body, second_body, first_if);
36536d79
BI
5151 if_stmt = c_end_compound_stmt (loc, block, flag_isoc99);
5152
5153 /* If the if statement contains array notations, then we expand them. */
b72271b9 5154 if (flag_cilkplus && contains_array_notation_expr (if_stmt))
36536d79
BI
5155 if_stmt = fix_conditional_array_notations (if_stmt);
5156 add_stmt (if_stmt);
27bf414c
JM
5157}
5158
5159/* Parse a switch statement (C90 6.6.4, C99 6.8.4).
5160
5161 switch-statement:
5162 switch (expression) statement
5163*/
5164
5165static void
5166c_parser_switch_statement (c_parser *parser)
5167{
267bac10 5168 struct c_expr ce;
27bf414c 5169 tree block, expr, body, save_break;
c2255bc4
AH
5170 location_t switch_loc = c_parser_peek_token (parser)->location;
5171 location_t switch_cond_loc;
27bf414c
JM
5172 gcc_assert (c_parser_next_token_is_keyword (parser, RID_SWITCH));
5173 c_parser_consume_token (parser);
5174 block = c_begin_compound_stmt (flag_isoc99);
5175 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5176 {
c2255bc4 5177 switch_cond_loc = c_parser_peek_token (parser)->location;
267bac10
JM
5178 ce = c_parser_expression (parser);
5179 ce = convert_lvalue_to_rvalue (switch_cond_loc, ce, true, false);
5180 expr = ce.value;
b72271b9 5181 if (flag_cilkplus && contains_array_notation_expr (expr))
36536d79
BI
5182 {
5183 error_at (switch_cond_loc,
5184 "array notations cannot be used as a condition for switch "
5185 "statement");
5186 expr = error_mark_node;
5187 }
27bf414c
JM
5188 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
5189 }
5190 else
c2255bc4
AH
5191 {
5192 switch_cond_loc = UNKNOWN_LOCATION;
5193 expr = error_mark_node;
5194 }
5195 c_start_case (switch_loc, switch_cond_loc, expr);
27bf414c
JM
5196 save_break = c_break_label;
5197 c_break_label = NULL_TREE;
5198 body = c_parser_c99_block_statement (parser);
5199 c_finish_case (body);
5200 if (c_break_label)
c2255bc4
AH
5201 {
5202 location_t here = c_parser_peek_token (parser)->location;
5203 tree t = build1 (LABEL_EXPR, void_type_node, c_break_label);
5204 SET_EXPR_LOCATION (t, here);
5205 add_stmt (t);
5206 }
27bf414c 5207 c_break_label = save_break;
c2255bc4 5208 add_stmt (c_end_compound_stmt (switch_loc, block, flag_isoc99));
27bf414c
JM
5209}
5210
5211/* Parse a while statement (C90 6.6.5, C99 6.8.5).
5212
5213 while-statement:
5214 while (expression) statement
5215*/
5216
5217static void
d4af74d4 5218c_parser_while_statement (c_parser *parser, bool ivdep)
27bf414c
JM
5219{
5220 tree block, cond, body, save_break, save_cont;
5221 location_t loc;
5222 gcc_assert (c_parser_next_token_is_keyword (parser, RID_WHILE));
5223 c_parser_consume_token (parser);
5224 block = c_begin_compound_stmt (flag_isoc99);
5225 loc = c_parser_peek_token (parser)->location;
5226 cond = c_parser_paren_condition (parser);
b72271b9 5227 if (flag_cilkplus && contains_array_notation_expr (cond))
36536d79
BI
5228 {
5229 error_at (loc, "array notations cannot be used as a condition for while "
5230 "statement");
5231 cond = error_mark_node;
5232 }
d4af74d4
TB
5233
5234 if (ivdep && cond != error_mark_node)
5235 cond = build2 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
5236 build_int_cst (integer_type_node,
5237 annot_expr_ivdep_kind));
27bf414c
JM
5238 save_break = c_break_label;
5239 c_break_label = NULL_TREE;
5240 save_cont = c_cont_label;
5241 c_cont_label = NULL_TREE;
5242 body = c_parser_c99_block_statement (parser);
5243 c_finish_loop (loc, cond, NULL, body, c_break_label, c_cont_label, true);
c2255bc4 5244 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99));
27bf414c
JM
5245 c_break_label = save_break;
5246 c_cont_label = save_cont;
5247}
5248
5249/* Parse a do statement (C90 6.6.5, C99 6.8.5).
5250
5251 do-statement:
5252 do statement while ( expression ) ;
5253*/
5254
5255static void
d4af74d4 5256c_parser_do_statement (c_parser *parser, bool ivdep)
27bf414c
JM
5257{
5258 tree block, cond, body, save_break, save_cont, new_break, new_cont;
5259 location_t loc;
5260 gcc_assert (c_parser_next_token_is_keyword (parser, RID_DO));
5261 c_parser_consume_token (parser);
62e00e94 5262 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
3ba09659
AH
5263 warning_at (c_parser_peek_token (parser)->location,
5264 OPT_Wempty_body,
5265 "suggest braces around empty body in %<do%> statement");
27bf414c
JM
5266 block = c_begin_compound_stmt (flag_isoc99);
5267 loc = c_parser_peek_token (parser)->location;
5268 save_break = c_break_label;
5269 c_break_label = NULL_TREE;
5270 save_cont = c_cont_label;
5271 c_cont_label = NULL_TREE;
5272 body = c_parser_c99_block_statement (parser);
5273 c_parser_require_keyword (parser, RID_WHILE, "expected %<while%>");
5274 new_break = c_break_label;
5275 c_break_label = save_break;
5276 new_cont = c_cont_label;
5277 c_cont_label = save_cont;
5278 cond = c_parser_paren_condition (parser);
b72271b9 5279 if (flag_cilkplus && contains_array_notation_expr (cond))
36536d79
BI
5280 {
5281 error_at (loc, "array notations cannot be used as a condition for a "
5282 "do-while statement");
5283 cond = error_mark_node;
5284 }
d4af74d4
TB
5285 if (ivdep && cond != error_mark_node)
5286 cond = build2 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
5287 build_int_cst (integer_type_node,
5288 annot_expr_ivdep_kind));
27bf414c
JM
5289 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
5290 c_parser_skip_to_end_of_block_or_statement (parser);
5291 c_finish_loop (loc, cond, NULL, body, new_break, new_cont, false);
c2255bc4 5292 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99));
27bf414c
JM
5293}
5294
5295/* Parse a for statement (C90 6.6.5, C99 6.8.5).
5296
5297 for-statement:
5298 for ( expression[opt] ; expression[opt] ; expression[opt] ) statement
5299 for ( nested-declaration expression[opt] ; expression[opt] ) statement
5300
5301 The form with a declaration is new in C99.
5302
5303 ??? In accordance with the old parser, the declaration may be a
5304 nested function, which is then rejected in check_for_loop_decls,
5305 but does it make any sense for this to be included in the grammar?
5306 Note in particular that the nested function does not include a
5307 trailing ';', whereas the "declaration" production includes one.
5308 Also, can we reject bad declarations earlier and cheaper than
f05b9d93
NP
5309 check_for_loop_decls?
5310
5311 In Objective-C, there are two additional variants:
5312
5313 foreach-statement:
5314 for ( expression in expresssion ) statement
5315 for ( declaration in expression ) statement
5316
5317 This is inconsistent with C, because the second variant is allowed
5318 even if c99 is not enabled.
5319
5320 The rest of the comment documents these Objective-C foreach-statement.
5321
5322 Here is the canonical example of the first variant:
5323 for (object in array) { do something with object }
5324 we call the first expression ("object") the "object_expression" and
5325 the second expression ("array") the "collection_expression".
5326 object_expression must be an lvalue of type "id" (a generic Objective-C
5327 object) because the loop works by assigning to object_expression the
5328 various objects from the collection_expression. collection_expression
5329 must evaluate to something of type "id" which responds to the method
5330 countByEnumeratingWithState:objects:count:.
5331
5332 The canonical example of the second variant is:
5333 for (id object in array) { do something with object }
5334 which is completely equivalent to
5335 {
5336 id object;
5337 for (object in array) { do something with object }
5338 }
5339 Note that initizializing 'object' in some way (eg, "for ((object =
5340 xxx) in array) { do something with object }") is possibly
5341 technically valid, but completely pointless as 'object' will be
5342 assigned to something else as soon as the loop starts. We should
5343 most likely reject it (TODO).
5344
5345 The beginning of the Objective-C foreach-statement looks exactly
5346 like the beginning of the for-statement, and we can tell it is a
5347 foreach-statement only because the initial declaration or
5348 expression is terminated by 'in' instead of ';'.
5349*/
27bf414c
JM
5350
5351static void
8170608b 5352c_parser_for_statement (c_parser *parser, bool ivdep)
27bf414c
JM
5353{
5354 tree block, cond, incr, save_break, save_cont, body;
f05b9d93 5355 /* The following are only used when parsing an ObjC foreach statement. */
689f2c82
AO
5356 tree object_expression;
5357 /* Silence the bogus uninitialized warning. */
5358 tree collection_expression = NULL;
c2255bc4
AH
5359 location_t loc = c_parser_peek_token (parser)->location;
5360 location_t for_loc = c_parser_peek_token (parser)->location;
f05b9d93 5361 bool is_foreach_statement = false;
27bf414c
JM
5362 gcc_assert (c_parser_next_token_is_keyword (parser, RID_FOR));
5363 c_parser_consume_token (parser);
f05b9d93
NP
5364 /* Open a compound statement in Objective-C as well, just in case this is
5365 as foreach expression. */
5366 block = c_begin_compound_stmt (flag_isoc99 || c_dialect_objc ());
91b90ead
UB
5367 cond = error_mark_node;
5368 incr = error_mark_node;
27bf414c
JM
5369 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5370 {
5371 /* Parse the initialization declaration or expression. */
f05b9d93 5372 object_expression = error_mark_node;
a5812bdc 5373 parser->objc_could_be_foreach_context = c_dialect_objc ();
27bf414c
JM
5374 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5375 {
a5812bdc 5376 parser->objc_could_be_foreach_context = false;
27bf414c 5377 c_parser_consume_token (parser);
c2255bc4 5378 c_finish_expr_stmt (loc, NULL_TREE);
27bf414c 5379 }
2f413185 5380 else if (c_parser_next_tokens_start_declaration (parser))
27bf414c 5381 {
f05b9d93 5382 c_parser_declaration_or_fndef (parser, true, true, true, true, true,
acf0174b 5383 &object_expression, vNULL);
f05b9d93
NP
5384 parser->objc_could_be_foreach_context = false;
5385
5386 if (c_parser_next_token_is_keyword (parser, RID_IN))
5387 {
5388 c_parser_consume_token (parser);
5389 is_foreach_statement = true;
5390 if (check_for_loop_decls (for_loc, true) == NULL_TREE)
5391 c_parser_error (parser, "multiple iterating variables in fast enumeration");
5392 }
5393 else
5394 check_for_loop_decls (for_loc, flag_isoc99);
27bf414c
JM
5395 }
5396 else if (c_parser_next_token_is_keyword (parser, RID_EXTENSION))
5397 {
5398 /* __extension__ can start a declaration, but is also an
5399 unary operator that can start an expression. Consume all
5400 but the last of a possible series of __extension__ to
5401 determine which. */
5402 while (c_parser_peek_2nd_token (parser)->type == CPP_KEYWORD
5403 && (c_parser_peek_2nd_token (parser)->keyword
5404 == RID_EXTENSION))
5405 c_parser_consume_token (parser);
32912286 5406 if (c_token_starts_declaration (c_parser_peek_2nd_token (parser)))
27bf414c
JM
5407 {
5408 int ext;
5409 ext = disable_extension_diagnostics ();
5410 c_parser_consume_token (parser);
32912286 5411 c_parser_declaration_or_fndef (parser, true, true, true, true,
acf0174b 5412 true, &object_expression, vNULL);
f05b9d93
NP
5413 parser->objc_could_be_foreach_context = false;
5414
27bf414c 5415 restore_extension_diagnostics (ext);
f05b9d93
NP
5416 if (c_parser_next_token_is_keyword (parser, RID_IN))
5417 {
5418 c_parser_consume_token (parser);
5419 is_foreach_statement = true;
5420 if (check_for_loop_decls (for_loc, true) == NULL_TREE)
5421 c_parser_error (parser, "multiple iterating variables in fast enumeration");
5422 }
5423 else
5424 check_for_loop_decls (for_loc, flag_isoc99);
27bf414c
JM
5425 }
5426 else
5427 goto init_expr;
5428 }
5429 else
5430 {
5431 init_expr:
f05b9d93 5432 {
267bac10 5433 struct c_expr ce;
f05b9d93 5434 tree init_expression;
267bac10
JM
5435 ce = c_parser_expression (parser);
5436 init_expression = ce.value;
f05b9d93
NP
5437 parser->objc_could_be_foreach_context = false;
5438 if (c_parser_next_token_is_keyword (parser, RID_IN))
5439 {
5440 c_parser_consume_token (parser);
5441 is_foreach_statement = true;
5442 if (! lvalue_p (init_expression))
5443 c_parser_error (parser, "invalid iterating variable in fast enumeration");
69a97201 5444 object_expression = c_fully_fold (init_expression, false, NULL);
f05b9d93
NP
5445 }
5446 else
5447 {
267bac10
JM
5448 ce = convert_lvalue_to_rvalue (loc, ce, true, false);
5449 init_expression = ce.value;
f05b9d93
NP
5450 c_finish_expr_stmt (loc, init_expression);
5451 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
5452 }
5453 }
27bf414c 5454 }
f05b9d93
NP
5455 /* Parse the loop condition. In the case of a foreach
5456 statement, there is no loop condition. */
a5812bdc 5457 gcc_assert (!parser->objc_could_be_foreach_context);
f05b9d93 5458 if (!is_foreach_statement)
27bf414c 5459 {
f05b9d93
NP
5460 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5461 {
8170608b
TB
5462 if (ivdep)
5463 {
5464 c_parser_error (parser, "missing loop condition in loop with "
5465 "%<GCC ivdep%> pragma");
5466 cond = error_mark_node;
5467 }
5468 else
5469 {
5470 c_parser_consume_token (parser);
5471 cond = NULL_TREE;
5472 }
f05b9d93
NP
5473 }
5474 else
5475 {
5476 cond = c_parser_condition (parser);
b72271b9 5477 if (flag_cilkplus && contains_array_notation_expr (cond))
36536d79
BI
5478 {
5479 error_at (loc, "array notations cannot be used in a "
5480 "condition for a for-loop");
5481 cond = error_mark_node;
5482 }
5483 c_parser_skip_until_found (parser, CPP_SEMICOLON,
5484 "expected %<;%>");
f05b9d93 5485 }
8170608b
TB
5486 if (ivdep && cond != error_mark_node)
5487 cond = build2 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
5488 build_int_cst (integer_type_node,
5489 annot_expr_ivdep_kind));
27bf414c 5490 }
f05b9d93
NP
5491 /* Parse the increment expression (the third expression in a
5492 for-statement). In the case of a foreach-statement, this is
5493 the expression that follows the 'in'. */
5494 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
27bf414c 5495 {
f05b9d93
NP
5496 if (is_foreach_statement)
5497 {
5498 c_parser_error (parser, "missing collection in fast enumeration");
5499 collection_expression = error_mark_node;
5500 }
5501 else
5502 incr = c_process_expr_stmt (loc, NULL_TREE);
27bf414c 5503 }
27bf414c 5504 else
f05b9d93
NP
5505 {
5506 if (is_foreach_statement)
69a97201
NP
5507 collection_expression = c_fully_fold (c_parser_expression (parser).value,
5508 false, NULL);
f05b9d93 5509 else
267bac10
JM
5510 {
5511 struct c_expr ce = c_parser_expression (parser);
5512 ce = convert_lvalue_to_rvalue (loc, ce, true, false);
5513 incr = c_process_expr_stmt (loc, ce.value);
5514 }
f05b9d93 5515 }
27bf414c
JM
5516 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
5517 }
27bf414c
JM
5518 save_break = c_break_label;
5519 c_break_label = NULL_TREE;
5520 save_cont = c_cont_label;
5521 c_cont_label = NULL_TREE;
5522 body = c_parser_c99_block_statement (parser);
f05b9d93
NP
5523 if (is_foreach_statement)
5524 objc_finish_foreach_loop (loc, object_expression, collection_expression, body, c_break_label, c_cont_label);
5525 else
5526 c_finish_loop (loc, cond, incr, body, c_break_label, c_cont_label, true);
5527 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99 || c_dialect_objc ()));
27bf414c
JM
5528 c_break_label = save_break;
5529 c_cont_label = save_cont;
5530}
5531
5532/* Parse an asm statement, a GNU extension. This is a full-blown asm
5533 statement with inputs, outputs, clobbers, and volatile tag
5534 allowed.
5535
5536 asm-statement:
5537 asm type-qualifier[opt] ( asm-argument ) ;
1c384bf1 5538 asm type-qualifier[opt] goto ( asm-goto-argument ) ;
27bf414c
JM
5539
5540 asm-argument:
5541 asm-string-literal
5542 asm-string-literal : asm-operands[opt]
5543 asm-string-literal : asm-operands[opt] : asm-operands[opt]
1c384bf1
RH
5544 asm-string-literal : asm-operands[opt] : asm-operands[opt] : asm-clobbers[opt]
5545
5546 asm-goto-argument:
5547 asm-string-literal : : asm-operands[opt] : asm-clobbers[opt] \
5548 : asm-goto-operands
27bf414c
JM
5549
5550 Qualifiers other than volatile are accepted in the syntax but
5551 warned for. */
5552
5553static tree
5554c_parser_asm_statement (c_parser *parser)
5555{
1c384bf1
RH
5556 tree quals, str, outputs, inputs, clobbers, labels, ret;
5557 bool simple, is_goto;
c2255bc4 5558 location_t asm_loc = c_parser_peek_token (parser)->location;
1c384bf1
RH
5559 int section, nsections;
5560
27bf414c
JM
5561 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ASM));
5562 c_parser_consume_token (parser);
5563 if (c_parser_next_token_is_keyword (parser, RID_VOLATILE))
5564 {
5565 quals = c_parser_peek_token (parser)->value;
5566 c_parser_consume_token (parser);
5567 }
5568 else if (c_parser_next_token_is_keyword (parser, RID_CONST)
5569 || c_parser_next_token_is_keyword (parser, RID_RESTRICT))
5570 {
3ba09659
AH
5571 warning_at (c_parser_peek_token (parser)->location,
5572 0,
5573 "%E qualifier ignored on asm",
5574 c_parser_peek_token (parser)->value);
27bf414c
JM
5575 quals = NULL_TREE;
5576 c_parser_consume_token (parser);
5577 }
5578 else
5579 quals = NULL_TREE;
1c384bf1
RH
5580
5581 is_goto = false;
5582 if (c_parser_next_token_is_keyword (parser, RID_GOTO))
5583 {
5584 c_parser_consume_token (parser);
5585 is_goto = true;
5586 }
5587
27bf414c 5588 /* ??? Follow the C++ parser rather than using the
46c2514e
TT
5589 lex_untranslated_string kludge. */
5590 parser->lex_untranslated_string = true;
1c384bf1
RH
5591 ret = NULL;
5592
27bf414c 5593 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
1c384bf1
RH
5594 goto error;
5595
27bf414c 5596 str = c_parser_asm_string_literal (parser);
b85eb797 5597 if (str == NULL_TREE)
1c384bf1
RH
5598 goto error_close_paren;
5599
5600 simple = true;
5601 outputs = NULL_TREE;
5602 inputs = NULL_TREE;
5603 clobbers = NULL_TREE;
5604 labels = NULL_TREE;
5605
5606 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN) && !is_goto)
5607 goto done_asm;
5608
5609 /* Parse each colon-delimited section of operands. */
5610 nsections = 3 + is_goto;
5611 for (section = 0; section < nsections; ++section)
b85eb797 5612 {
1c384bf1
RH
5613 if (!c_parser_require (parser, CPP_COLON,
5614 is_goto
5615 ? "expected %<:%>"
5616 : "expected %<:%> or %<)%>"))
5617 goto error_close_paren;
5618
5619 /* Once past any colon, we're no longer a simple asm. */
5620 simple = false;
5621
5622 if ((!c_parser_next_token_is (parser, CPP_COLON)
5623 && !c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
5624 || section == 3)
5625 switch (section)
5626 {
5627 case 0:
5628 /* For asm goto, we don't allow output operands, but reserve
5629 the slot for a future extension that does allow them. */
5630 if (!is_goto)
eadd3d0d 5631 outputs = c_parser_asm_operands (parser);
1c384bf1
RH
5632 break;
5633 case 1:
eadd3d0d 5634 inputs = c_parser_asm_operands (parser);
1c384bf1
RH
5635 break;
5636 case 2:
5637 clobbers = c_parser_asm_clobbers (parser);
5638 break;
5639 case 3:
5640 labels = c_parser_asm_goto_operands (parser);
5641 break;
5642 default:
5643 gcc_unreachable ();
5644 }
5645
5646 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN) && !is_goto)
5647 goto done_asm;
27bf414c 5648 }
1c384bf1 5649
27bf414c 5650 done_asm:
27bf414c
JM
5651 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
5652 {
5653 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
1c384bf1 5654 goto error;
27bf414c 5655 }
1c384bf1 5656
27bf414c
JM
5657 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
5658 c_parser_skip_to_end_of_block_or_statement (parser);
1c384bf1 5659
c2255bc4 5660 ret = build_asm_stmt (quals, build_asm_expr (asm_loc, str, outputs, inputs,
1c384bf1
RH
5661 clobbers, labels, simple));
5662
5663 error:
5664 parser->lex_untranslated_string = false;
27bf414c 5665 return ret;
1c384bf1
RH
5666
5667 error_close_paren:
5668 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5669 goto error;
27bf414c
JM
5670}
5671
eadd3d0d 5672/* Parse asm operands, a GNU extension.
27bf414c
JM
5673
5674 asm-operands:
5675 asm-operand
5676 asm-operands , asm-operand
5677
5678 asm-operand:
5679 asm-string-literal ( expression )
5680 [ identifier ] asm-string-literal ( expression )
5681*/
5682
5683static tree
eadd3d0d 5684c_parser_asm_operands (c_parser *parser)
27bf414c
JM
5685{
5686 tree list = NULL_TREE;
5687 while (true)
5688 {
f2a71bbc
JM
5689 tree name, str;
5690 struct c_expr expr;
27bf414c
JM
5691 if (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
5692 {
5693 c_parser_consume_token (parser);
5694 if (c_parser_next_token_is (parser, CPP_NAME))
5695 {
5696 tree id = c_parser_peek_token (parser)->value;
5697 c_parser_consume_token (parser);
5698 name = build_string (IDENTIFIER_LENGTH (id),
5699 IDENTIFIER_POINTER (id));
5700 }
5701 else
5702 {
5703 c_parser_error (parser, "expected identifier");
5704 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
5705 return NULL_TREE;
5706 }
5707 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
5708 "expected %<]%>");
5709 }
5710 else
5711 name = NULL_TREE;
5712 str = c_parser_asm_string_literal (parser);
5713 if (str == NULL_TREE)
5714 return NULL_TREE;
46c2514e 5715 parser->lex_untranslated_string = false;
27bf414c
JM
5716 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5717 {
46c2514e 5718 parser->lex_untranslated_string = true;
27bf414c
JM
5719 return NULL_TREE;
5720 }
f2a71bbc 5721 expr = c_parser_expression (parser);
ebfbbdc5 5722 mark_exp_read (expr.value);
46c2514e 5723 parser->lex_untranslated_string = true;
27bf414c
JM
5724 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
5725 {
5726 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5727 return NULL_TREE;
5728 }
5729 list = chainon (list, build_tree_list (build_tree_list (name, str),
f2a71bbc 5730 expr.value));
27bf414c
JM
5731 if (c_parser_next_token_is (parser, CPP_COMMA))
5732 c_parser_consume_token (parser);
5733 else
5734 break;
5735 }
5736 return list;
5737}
5738
5739/* Parse asm clobbers, a GNU extension.
5740
5741 asm-clobbers:
5742 asm-string-literal
5743 asm-clobbers , asm-string-literal
5744*/
5745
5746static tree
5747c_parser_asm_clobbers (c_parser *parser)
5748{
5749 tree list = NULL_TREE;
5750 while (true)
5751 {
5752 tree str = c_parser_asm_string_literal (parser);
5753 if (str)
5754 list = tree_cons (NULL_TREE, str, list);
5755 else
5756 return NULL_TREE;
5757 if (c_parser_next_token_is (parser, CPP_COMMA))
5758 c_parser_consume_token (parser);
5759 else
5760 break;
5761 }
5762 return list;
5763}
5764
1c384bf1 5765/* Parse asm goto labels, a GNU extension.
b8698a0f 5766
1c384bf1
RH
5767 asm-goto-operands:
5768 identifier
5769 asm-goto-operands , identifier
5770*/
5771
5772static tree
5773c_parser_asm_goto_operands (c_parser *parser)
5774{
5775 tree list = NULL_TREE;
5776 while (true)
5777 {
5778 tree name, label;
5779
5780 if (c_parser_next_token_is (parser, CPP_NAME))
5781 {
5782 c_token *tok = c_parser_peek_token (parser);
5783 name = tok->value;
5784 label = lookup_label_for_goto (tok->location, name);
5785 c_parser_consume_token (parser);
5786 TREE_USED (label) = 1;
5787 }
5788 else
5789 {
5790 c_parser_error (parser, "expected identifier");
5791 return NULL_TREE;
5792 }
5793
5794 name = build_string (IDENTIFIER_LENGTH (name),
5795 IDENTIFIER_POINTER (name));
5796 list = tree_cons (name, label, list);
5797 if (c_parser_next_token_is (parser, CPP_COMMA))
5798 c_parser_consume_token (parser);
5799 else
5800 return nreverse (list);
5801 }
5802}
5803
27bf414c
JM
5804/* Parse an expression other than a compound expression; that is, an
5805 assignment expression (C90 6.3.16, C99 6.5.16). If AFTER is not
5806 NULL then it is an Objective-C message expression which is the
5807 primary-expression starting the expression as an initializer.
5808
5809 assignment-expression:
5810 conditional-expression
5811 unary-expression assignment-operator assignment-expression
5812
5813 assignment-operator: one of
5814 = *= /= %= += -= <<= >>= &= ^= |=
5815
5816 In GNU C we accept any conditional expression on the LHS and
5817 diagnose the invalid lvalue rather than producing a syntax
5818 error. */
5819
5820static struct c_expr
acf0174b
JJ
5821c_parser_expr_no_commas (c_parser *parser, struct c_expr *after,
5822 tree omp_atomic_lhs)
27bf414c
JM
5823{
5824 struct c_expr lhs, rhs, ret;
5825 enum tree_code code;
c2255bc4 5826 location_t op_location, exp_location;
27bf414c 5827 gcc_assert (!after || c_dialect_objc ());
acf0174b 5828 lhs = c_parser_conditional_expression (parser, after, omp_atomic_lhs);
c9f9eb5d 5829 op_location = c_parser_peek_token (parser)->location;
27bf414c
JM
5830 switch (c_parser_peek_token (parser)->type)
5831 {
5832 case CPP_EQ:
5833 code = NOP_EXPR;
5834 break;
5835 case CPP_MULT_EQ:
5836 code = MULT_EXPR;
5837 break;
5838 case CPP_DIV_EQ:
5839 code = TRUNC_DIV_EXPR;
5840 break;
5841 case CPP_MOD_EQ:
5842 code = TRUNC_MOD_EXPR;
5843 break;
5844 case CPP_PLUS_EQ:
5845 code = PLUS_EXPR;
5846 break;
5847 case CPP_MINUS_EQ:
5848 code = MINUS_EXPR;
5849 break;
5850 case CPP_LSHIFT_EQ:
5851 code = LSHIFT_EXPR;
5852 break;
5853 case CPP_RSHIFT_EQ:
5854 code = RSHIFT_EXPR;
5855 break;
5856 case CPP_AND_EQ:
5857 code = BIT_AND_EXPR;
5858 break;
5859 case CPP_XOR_EQ:
5860 code = BIT_XOR_EXPR;
5861 break;
5862 case CPP_OR_EQ:
5863 code = BIT_IOR_EXPR;
5864 break;
5865 default:
5866 return lhs;
5867 }
5868 c_parser_consume_token (parser);
c2255bc4 5869 exp_location = c_parser_peek_token (parser)->location;
27bf414c 5870 rhs = c_parser_expr_no_commas (parser, NULL);
267bac10 5871 rhs = convert_lvalue_to_rvalue (exp_location, rhs, true, true);
36536d79 5872
25c22937
BI
5873 ret.value = build_modify_expr (op_location, lhs.value, lhs.original_type,
5874 code, exp_location, rhs.value,
5875 rhs.original_type);
27bf414c
JM
5876 if (code == NOP_EXPR)
5877 ret.original_code = MODIFY_EXPR;
5878 else
5879 {
5880 TREE_NO_WARNING (ret.value) = 1;
5881 ret.original_code = ERROR_MARK;
5882 }
6866c6e8 5883 ret.original_type = NULL;
27bf414c
JM
5884 return ret;
5885}
5886
5887/* Parse a conditional expression (C90 6.3.15, C99 6.5.15). If AFTER
5888 is not NULL then it is an Objective-C message expression which is
5889 the primary-expression starting the expression as an initializer.
5890
5891 conditional-expression:
5892 logical-OR-expression
5893 logical-OR-expression ? expression : conditional-expression
5894
5895 GNU extensions:
5896
5897 conditional-expression:
5898 logical-OR-expression ? : conditional-expression
5899*/
5900
5901static struct c_expr
acf0174b
JJ
5902c_parser_conditional_expression (c_parser *parser, struct c_expr *after,
5903 tree omp_atomic_lhs)
27bf414c
JM
5904{
5905 struct c_expr cond, exp1, exp2, ret;
d166d4c3 5906 location_t cond_loc, colon_loc, middle_loc;
ba47d38d 5907
27bf414c 5908 gcc_assert (!after || c_dialect_objc ());
ba47d38d 5909
acf0174b 5910 cond = c_parser_binary_expression (parser, after, omp_atomic_lhs);
ba47d38d 5911
27bf414c
JM
5912 if (c_parser_next_token_is_not (parser, CPP_QUERY))
5913 return cond;
c2255bc4 5914 cond_loc = c_parser_peek_token (parser)->location;
267bac10 5915 cond = convert_lvalue_to_rvalue (cond_loc, cond, true, true);
27bf414c
JM
5916 c_parser_consume_token (parser);
5917 if (c_parser_next_token_is (parser, CPP_COLON))
5918 {
8ce94e44 5919 tree eptype = NULL_TREE;
d166d4c3
AK
5920
5921 middle_loc = c_parser_peek_token (parser)->location;
c1771a20 5922 pedwarn (middle_loc, OPT_Wpedantic,
509c9d60 5923 "ISO C forbids omitting the middle term of a ?: expression");
d166d4c3 5924 warn_for_omitted_condop (middle_loc, cond.value);
8ce94e44
JM
5925 if (TREE_CODE (cond.value) == EXCESS_PRECISION_EXPR)
5926 {
5927 eptype = TREE_TYPE (cond.value);
5928 cond.value = TREE_OPERAND (cond.value, 0);
5929 }
27bf414c 5930 /* Make sure first operand is calculated only once. */
928c19bb 5931 exp1.value = c_save_expr (default_conversion (cond.value));
8ce94e44
JM
5932 if (eptype)
5933 exp1.value = build1 (EXCESS_PRECISION_EXPR, eptype, exp1.value);
6866c6e8 5934 exp1.original_type = NULL;
ba47d38d 5935 cond.value = c_objc_common_truthvalue_conversion (cond_loc, exp1.value);
7d882b83 5936 c_inhibit_evaluation_warnings += cond.value == truthvalue_true_node;
27bf414c
JM
5937 }
5938 else
5939 {
5940 cond.value
85498824 5941 = c_objc_common_truthvalue_conversion
ba47d38d 5942 (cond_loc, default_conversion (cond.value));
7d882b83 5943 c_inhibit_evaluation_warnings += cond.value == truthvalue_false_node;
46bdb9cf 5944 exp1 = c_parser_expression_conv (parser);
ebfbbdc5 5945 mark_exp_read (exp1.value);
7d882b83
ILT
5946 c_inhibit_evaluation_warnings +=
5947 ((cond.value == truthvalue_true_node)
5948 - (cond.value == truthvalue_false_node));
27bf414c 5949 }
744aa42f
ILT
5950
5951 colon_loc = c_parser_peek_token (parser)->location;
27bf414c
JM
5952 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
5953 {
7d882b83 5954 c_inhibit_evaluation_warnings -= cond.value == truthvalue_true_node;
27bf414c
JM
5955 ret.value = error_mark_node;
5956 ret.original_code = ERROR_MARK;
6866c6e8 5957 ret.original_type = NULL;
27bf414c
JM
5958 return ret;
5959 }
c2255bc4
AH
5960 {
5961 location_t exp2_loc = c_parser_peek_token (parser)->location;
acf0174b 5962 exp2 = c_parser_conditional_expression (parser, NULL, NULL_TREE);
267bac10 5963 exp2 = convert_lvalue_to_rvalue (exp2_loc, exp2, true, true);
c2255bc4 5964 }
7d882b83 5965 c_inhibit_evaluation_warnings -= cond.value == truthvalue_true_node;
744aa42f 5966 ret.value = build_conditional_expr (colon_loc, cond.value,
928c19bb 5967 cond.original_code == C_MAYBE_CONST_EXPR,
d130ae11
ILT
5968 exp1.value, exp1.original_type,
5969 exp2.value, exp2.original_type);
27bf414c 5970 ret.original_code = ERROR_MARK;
6866c6e8
ILT
5971 if (exp1.value == error_mark_node || exp2.value == error_mark_node)
5972 ret.original_type = NULL;
5973 else
5974 {
5975 tree t1, t2;
5976
5977 /* If both sides are enum type, the default conversion will have
5978 made the type of the result be an integer type. We want to
5979 remember the enum types we started with. */
5980 t1 = exp1.original_type ? exp1.original_type : TREE_TYPE (exp1.value);
5981 t2 = exp2.original_type ? exp2.original_type : TREE_TYPE (exp2.value);
5982 ret.original_type = ((t1 != error_mark_node
5983 && t2 != error_mark_node
5984 && (TYPE_MAIN_VARIANT (t1)
5985 == TYPE_MAIN_VARIANT (t2)))
5986 ? t1
5987 : NULL);
5988 }
27bf414c
JM
5989 return ret;
5990}
5991
5992/* Parse a binary expression; that is, a logical-OR-expression (C90
5993 6.3.5-6.3.14, C99 6.5.5-6.5.14). If AFTER is not NULL then it is
5994 an Objective-C message expression which is the primary-expression
acf0174b
JJ
5995 starting the expression as an initializer.
5996
5997 OMP_ATOMIC_LHS is NULL, unless parsing OpenMP #pragma omp atomic,
5998 when it should be the unfolded lhs. In a valid OpenMP source,
5999 one of the operands of the toplevel binary expression must be equal
6000 to it. In that case, just return a build2 created binary operation
6001 rather than result of parser_build_binary_op.
27bf414c
JM
6002
6003 multiplicative-expression:
6004 cast-expression
6005 multiplicative-expression * cast-expression
6006 multiplicative-expression / cast-expression
6007 multiplicative-expression % cast-expression
6008
6009 additive-expression:
6010 multiplicative-expression
6011 additive-expression + multiplicative-expression
6012 additive-expression - multiplicative-expression
6013
6014 shift-expression:
6015 additive-expression
6016 shift-expression << additive-expression
6017 shift-expression >> additive-expression
6018
6019 relational-expression:
6020 shift-expression
6021 relational-expression < shift-expression
6022 relational-expression > shift-expression
6023 relational-expression <= shift-expression
6024 relational-expression >= shift-expression
6025
6026 equality-expression:
6027 relational-expression
6028 equality-expression == relational-expression
6029 equality-expression != relational-expression
6030
6031 AND-expression:
6032 equality-expression
6033 AND-expression & equality-expression
6034
6035 exclusive-OR-expression:
6036 AND-expression
6037 exclusive-OR-expression ^ AND-expression
6038
6039 inclusive-OR-expression:
6040 exclusive-OR-expression
6041 inclusive-OR-expression | exclusive-OR-expression
6042
6043 logical-AND-expression:
6044 inclusive-OR-expression
6045 logical-AND-expression && inclusive-OR-expression
6046
6047 logical-OR-expression:
6048 logical-AND-expression
6049 logical-OR-expression || logical-AND-expression
6050*/
6051
6052static struct c_expr
20906c66 6053c_parser_binary_expression (c_parser *parser, struct c_expr *after,
acf0174b 6054 tree omp_atomic_lhs)
27bf414c
JM
6055{
6056 /* A binary expression is parsed using operator-precedence parsing,
6057 with the operands being cast expressions. All the binary
6058 operators are left-associative. Thus a binary expression is of
6059 form:
6060
6061 E0 op1 E1 op2 E2 ...
6062
6063 which we represent on a stack. On the stack, the precedence
6064 levels are strictly increasing. When a new operator is
6065 encountered of higher precedence than that at the top of the
6066 stack, it is pushed; its LHS is the top expression, and its RHS
6067 is everything parsed until it is popped. When a new operator is
6068 encountered with precedence less than or equal to that at the top
6069 of the stack, triples E[i-1] op[i] E[i] are popped and replaced
6070 by the result of the operation until the operator at the top of
6071 the stack has lower precedence than the new operator or there is
6072 only one element on the stack; then the top expression is the LHS
6073 of the new operator. In the case of logical AND and OR
7d882b83
ILT
6074 expressions, we also need to adjust c_inhibit_evaluation_warnings
6075 as appropriate when the operators are pushed and popped. */
27bf414c 6076
27bf414c
JM
6077 struct {
6078 /* The expression at this stack level. */
6079 struct c_expr expr;
6080 /* The precedence of the operator on its left, PREC_NONE at the
6081 bottom of the stack. */
20906c66 6082 enum c_parser_prec prec;
27bf414c
JM
6083 /* The operation on its left. */
6084 enum tree_code op;
ca80e52b
EB
6085 /* The source location of this operation. */
6086 location_t loc;
27bf414c
JM
6087 } stack[NUM_PRECS];
6088 int sp;
ba47d38d 6089 /* Location of the binary operator. */
1f6d0c60 6090 location_t binary_loc = UNKNOWN_LOCATION; /* Quiet warning. */
27bf414c
JM
6091#define POP \
6092 do { \
6093 switch (stack[sp].op) \
6094 { \
6095 case TRUTH_ANDIF_EXPR: \
7d882b83
ILT
6096 c_inhibit_evaluation_warnings -= (stack[sp - 1].expr.value \
6097 == truthvalue_false_node); \
27bf414c
JM
6098 break; \
6099 case TRUTH_ORIF_EXPR: \
7d882b83
ILT
6100 c_inhibit_evaluation_warnings -= (stack[sp - 1].expr.value \
6101 == truthvalue_true_node); \
27bf414c
JM
6102 break; \
6103 default: \
6104 break; \
6105 } \
f2a71bbc 6106 stack[sp - 1].expr \
267bac10
JM
6107 = convert_lvalue_to_rvalue (stack[sp - 1].loc, \
6108 stack[sp - 1].expr, true, true); \
f2a71bbc 6109 stack[sp].expr \
267bac10
JM
6110 = convert_lvalue_to_rvalue (stack[sp].loc, \
6111 stack[sp].expr, true, true); \
acf0174b
JJ
6112 if (__builtin_expect (omp_atomic_lhs != NULL_TREE, 0) && sp == 1 \
6113 && c_parser_peek_token (parser)->type == CPP_SEMICOLON \
6114 && ((1 << stack[sp].prec) \
6115 & (1 << (PREC_BITOR | PREC_BITXOR | PREC_BITAND | PREC_SHIFT \
6116 | PREC_ADD | PREC_MULT))) \
6117 && stack[sp].op != TRUNC_MOD_EXPR \
6118 && stack[0].expr.value != error_mark_node \
6119 && stack[1].expr.value != error_mark_node \
6120 && (c_tree_equal (stack[0].expr.value, omp_atomic_lhs) \
6121 || c_tree_equal (stack[1].expr.value, omp_atomic_lhs))) \
6122 stack[0].expr.value \
6123 = build2 (stack[1].op, TREE_TYPE (stack[0].expr.value), \
6124 stack[0].expr.value, stack[1].expr.value); \
6125 else \
6126 stack[sp - 1].expr = parser_build_binary_op (stack[sp].loc, \
6127 stack[sp].op, \
6128 stack[sp - 1].expr, \
6129 stack[sp].expr); \
27bf414c
JM
6130 sp--; \
6131 } while (0)
6132 gcc_assert (!after || c_dialect_objc ());
ca80e52b 6133 stack[0].loc = c_parser_peek_token (parser)->location;
27bf414c 6134 stack[0].expr = c_parser_cast_expression (parser, after);
acf0174b 6135 stack[0].prec = PREC_NONE;
27bf414c
JM
6136 sp = 0;
6137 while (true)
6138 {
20906c66 6139 enum c_parser_prec oprec;
27bf414c
JM
6140 enum tree_code ocode;
6141 if (parser->error)
6142 goto out;
6143 switch (c_parser_peek_token (parser)->type)
6144 {
6145 case CPP_MULT:
6146 oprec = PREC_MULT;
6147 ocode = MULT_EXPR;
6148 break;
6149 case CPP_DIV:
6150 oprec = PREC_MULT;
6151 ocode = TRUNC_DIV_EXPR;
6152 break;
6153 case CPP_MOD:
6154 oprec = PREC_MULT;
6155 ocode = TRUNC_MOD_EXPR;
6156 break;
6157 case CPP_PLUS:
6158 oprec = PREC_ADD;
6159 ocode = PLUS_EXPR;
6160 break;
6161 case CPP_MINUS:
6162 oprec = PREC_ADD;
6163 ocode = MINUS_EXPR;
6164 break;
6165 case CPP_LSHIFT:
6166 oprec = PREC_SHIFT;
6167 ocode = LSHIFT_EXPR;
6168 break;
6169 case CPP_RSHIFT:
6170 oprec = PREC_SHIFT;
6171 ocode = RSHIFT_EXPR;
6172 break;
6173 case CPP_LESS:
6174 oprec = PREC_REL;
6175 ocode = LT_EXPR;
6176 break;
6177 case CPP_GREATER:
6178 oprec = PREC_REL;
6179 ocode = GT_EXPR;
6180 break;
6181 case CPP_LESS_EQ:
6182 oprec = PREC_REL;
6183 ocode = LE_EXPR;
6184 break;
6185 case CPP_GREATER_EQ:
6186 oprec = PREC_REL;
6187 ocode = GE_EXPR;
6188 break;
6189 case CPP_EQ_EQ:
6190 oprec = PREC_EQ;
6191 ocode = EQ_EXPR;
6192 break;
6193 case CPP_NOT_EQ:
6194 oprec = PREC_EQ;
6195 ocode = NE_EXPR;
6196 break;
6197 case CPP_AND:
6198 oprec = PREC_BITAND;
6199 ocode = BIT_AND_EXPR;
6200 break;
6201 case CPP_XOR:
6202 oprec = PREC_BITXOR;
6203 ocode = BIT_XOR_EXPR;
6204 break;
6205 case CPP_OR:
6206 oprec = PREC_BITOR;
6207 ocode = BIT_IOR_EXPR;
6208 break;
6209 case CPP_AND_AND:
6210 oprec = PREC_LOGAND;
6211 ocode = TRUTH_ANDIF_EXPR;
6212 break;
6213 case CPP_OR_OR:
6214 oprec = PREC_LOGOR;
6215 ocode = TRUTH_ORIF_EXPR;
6216 break;
6217 default:
6218 /* Not a binary operator, so end of the binary
6219 expression. */
6220 goto out;
6221 }
ba47d38d 6222 binary_loc = c_parser_peek_token (parser)->location;
27bf414c 6223 while (oprec <= stack[sp].prec)
acf0174b 6224 POP;
20906c66 6225 c_parser_consume_token (parser);
27bf414c
JM
6226 switch (ocode)
6227 {
6228 case TRUTH_ANDIF_EXPR:
f2a71bbc 6229 stack[sp].expr
267bac10
JM
6230 = convert_lvalue_to_rvalue (stack[sp].loc,
6231 stack[sp].expr, true, true);
85498824 6232 stack[sp].expr.value = c_objc_common_truthvalue_conversion
ca80e52b 6233 (stack[sp].loc, default_conversion (stack[sp].expr.value));
7d882b83
ILT
6234 c_inhibit_evaluation_warnings += (stack[sp].expr.value
6235 == truthvalue_false_node);
27bf414c
JM
6236 break;
6237 case TRUTH_ORIF_EXPR:
f2a71bbc 6238 stack[sp].expr
267bac10
JM
6239 = convert_lvalue_to_rvalue (stack[sp].loc,
6240 stack[sp].expr, true, true);
85498824 6241 stack[sp].expr.value = c_objc_common_truthvalue_conversion
ca80e52b 6242 (stack[sp].loc, default_conversion (stack[sp].expr.value));
7d882b83
ILT
6243 c_inhibit_evaluation_warnings += (stack[sp].expr.value
6244 == truthvalue_true_node);
27bf414c
JM
6245 break;
6246 default:
6247 break;
6248 }
6249 sp++;
ca80e52b 6250 stack[sp].loc = binary_loc;
27bf414c
JM
6251 stack[sp].expr = c_parser_cast_expression (parser, NULL);
6252 stack[sp].prec = oprec;
6253 stack[sp].op = ocode;
c2255bc4 6254 stack[sp].loc = binary_loc;
27bf414c
JM
6255 }
6256 out:
6257 while (sp > 0)
6258 POP;
6259 return stack[0].expr;
6260#undef POP
6261}
6262
6263/* Parse a cast expression (C90 6.3.4, C99 6.5.4). If AFTER is not
6264 NULL then it is an Objective-C message expression which is the
6265 primary-expression starting the expression as an initializer.
6266
6267 cast-expression:
6268 unary-expression
6269 ( type-name ) unary-expression
6270*/
6271
6272static struct c_expr
6273c_parser_cast_expression (c_parser *parser, struct c_expr *after)
6274{
c2255bc4 6275 location_t cast_loc = c_parser_peek_token (parser)->location;
27bf414c
JM
6276 gcc_assert (!after || c_dialect_objc ());
6277 if (after)
c2255bc4
AH
6278 return c_parser_postfix_expression_after_primary (parser,
6279 cast_loc, *after);
27bf414c
JM
6280 /* If the expression begins with a parenthesized type name, it may
6281 be either a cast or a compound literal; we need to see whether
6282 the next character is '{' to tell the difference. If not, it is
29ce73cb
PB
6283 an unary expression. Full detection of unknown typenames here
6284 would require a 3-token lookahead. */
27bf414c
JM
6285 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
6286 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
6287 {
6288 struct c_type_name *type_name;
6289 struct c_expr ret;
f2a71bbc 6290 struct c_expr expr;
27bf414c
JM
6291 c_parser_consume_token (parser);
6292 type_name = c_parser_type_name (parser);
6293 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
6294 if (type_name == NULL)
6295 {
6296 ret.value = error_mark_node;
6297 ret.original_code = ERROR_MARK;
6866c6e8 6298 ret.original_type = NULL;
27bf414c
JM
6299 return ret;
6300 }
33c9159e
AH
6301
6302 /* Save casted types in the function's used types hash table. */
8d8d1a28 6303 used_types_insert (type_name->specs->type);
33c9159e 6304
27bf414c 6305 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
24b97832 6306 return c_parser_postfix_expression_after_paren_type (parser, type_name,
c2255bc4
AH
6307 cast_loc);
6308 {
6309 location_t expr_loc = c_parser_peek_token (parser)->location;
6310 expr = c_parser_cast_expression (parser, NULL);
267bac10 6311 expr = convert_lvalue_to_rvalue (expr_loc, expr, true, true);
c2255bc4
AH
6312 }
6313 ret.value = c_cast_expr (cast_loc, type_name, expr.value);
27bf414c 6314 ret.original_code = ERROR_MARK;
6866c6e8 6315 ret.original_type = NULL;
27bf414c
JM
6316 return ret;
6317 }
6318 else
6319 return c_parser_unary_expression (parser);
6320}
6321
6322/* Parse an unary expression (C90 6.3.3, C99 6.5.3).
6323
6324 unary-expression:
6325 postfix-expression
6326 ++ unary-expression
6327 -- unary-expression
6328 unary-operator cast-expression
6329 sizeof unary-expression
6330 sizeof ( type-name )
6331
6332 unary-operator: one of
6333 & * + - ~ !
6334
6335 GNU extensions:
6336
6337 unary-expression:
6338 __alignof__ unary-expression
6339 __alignof__ ( type-name )
6340 && identifier
6341
48b0b196 6342 (C11 permits _Alignof with type names only.)
d19fa6b5 6343
27bf414c
JM
6344 unary-operator: one of
6345 __extension__ __real__ __imag__
6346
0a35513e
AH
6347 Transactional Memory:
6348
6349 unary-expression:
6350 transaction-expression
6351
27bf414c
JM
6352 In addition, the GNU syntax treats ++ and -- as unary operators, so
6353 they may be applied to cast expressions with errors for non-lvalues
6354 given later. */
6355
6356static struct c_expr
6357c_parser_unary_expression (c_parser *parser)
6358{
6359 int ext;
46bdb9cf 6360 struct c_expr ret, op;
c2255bc4
AH
6361 location_t op_loc = c_parser_peek_token (parser)->location;
6362 location_t exp_loc;
6866c6e8
ILT
6363 ret.original_code = ERROR_MARK;
6364 ret.original_type = NULL;
27bf414c
JM
6365 switch (c_parser_peek_token (parser)->type)
6366 {
6367 case CPP_PLUS_PLUS:
6368 c_parser_consume_token (parser);
c2255bc4 6369 exp_loc = c_parser_peek_token (parser)->location;
46bdb9cf 6370 op = c_parser_cast_expression (parser, NULL);
36536d79
BI
6371
6372 /* If there is array notations in op, we expand them. */
b72271b9 6373 if (flag_cilkplus && TREE_CODE (op.value) == ARRAY_NOTATION_REF)
36536d79
BI
6374 return fix_array_notation_expr (exp_loc, PREINCREMENT_EXPR, op);
6375 else
6376 {
6377 op = default_function_array_read_conversion (exp_loc, op);
6378 return parser_build_unary_op (op_loc, PREINCREMENT_EXPR, op);
6379 }
27bf414c
JM
6380 case CPP_MINUS_MINUS:
6381 c_parser_consume_token (parser);
c2255bc4 6382 exp_loc = c_parser_peek_token (parser)->location;
46bdb9cf 6383 op = c_parser_cast_expression (parser, NULL);
36536d79
BI
6384
6385 /* If there is array notations in op, we expand them. */
b72271b9 6386 if (flag_cilkplus && TREE_CODE (op.value) == ARRAY_NOTATION_REF)
36536d79
BI
6387 return fix_array_notation_expr (exp_loc, PREDECREMENT_EXPR, op);
6388 else
6389 {
6390 op = default_function_array_read_conversion (exp_loc, op);
6391 return parser_build_unary_op (op_loc, PREDECREMENT_EXPR, op);
6392 }
27bf414c
JM
6393 case CPP_AND:
6394 c_parser_consume_token (parser);
ebfbbdc5
JJ
6395 op = c_parser_cast_expression (parser, NULL);
6396 mark_exp_read (op.value);
6397 return parser_build_unary_op (op_loc, ADDR_EXPR, op);
27bf414c
JM
6398 case CPP_MULT:
6399 c_parser_consume_token (parser);
c2255bc4 6400 exp_loc = c_parser_peek_token (parser)->location;
46bdb9cf 6401 op = c_parser_cast_expression (parser, NULL);
267bac10 6402 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
dd865ef6 6403 ret.value = build_indirect_ref (op_loc, op.value, RO_UNARY_STAR);
27bf414c
JM
6404 return ret;
6405 case CPP_PLUS:
8400e75e 6406 if (!c_dialect_objc () && !in_system_header_at (input_location))
c2255bc4 6407 warning_at (op_loc,
3ba09659
AH
6408 OPT_Wtraditional,
6409 "traditional C rejects the unary plus operator");
c7412148 6410 c_parser_consume_token (parser);
c2255bc4 6411 exp_loc = c_parser_peek_token (parser)->location;
46bdb9cf 6412 op = c_parser_cast_expression (parser, NULL);
267bac10 6413 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
c2255bc4 6414 return parser_build_unary_op (op_loc, CONVERT_EXPR, op);
27bf414c
JM
6415 case CPP_MINUS:
6416 c_parser_consume_token (parser);
c2255bc4 6417 exp_loc = c_parser_peek_token (parser)->location;
46bdb9cf 6418 op = c_parser_cast_expression (parser, NULL);
267bac10 6419 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
c2255bc4 6420 return parser_build_unary_op (op_loc, NEGATE_EXPR, op);
27bf414c
JM
6421 case CPP_COMPL:
6422 c_parser_consume_token (parser);
c2255bc4 6423 exp_loc = c_parser_peek_token (parser)->location;
46bdb9cf 6424 op = c_parser_cast_expression (parser, NULL);
267bac10 6425 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
c2255bc4 6426 return parser_build_unary_op (op_loc, BIT_NOT_EXPR, op);
27bf414c
JM
6427 case CPP_NOT:
6428 c_parser_consume_token (parser);
c2255bc4 6429 exp_loc = c_parser_peek_token (parser)->location;
46bdb9cf 6430 op = c_parser_cast_expression (parser, NULL);
267bac10 6431 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
c2255bc4 6432 return parser_build_unary_op (op_loc, TRUTH_NOT_EXPR, op);
27bf414c
JM
6433 case CPP_AND_AND:
6434 /* Refer to the address of a label as a pointer. */
6435 c_parser_consume_token (parser);
6436 if (c_parser_next_token_is (parser, CPP_NAME))
6437 {
6438 ret.value = finish_label_address_expr
c2255bc4 6439 (c_parser_peek_token (parser)->value, op_loc);
27bf414c 6440 c_parser_consume_token (parser);
27bf414c
JM
6441 }
6442 else
6443 {
6444 c_parser_error (parser, "expected identifier");
6445 ret.value = error_mark_node;
27bf414c 6446 }
43f6dfd3 6447 return ret;
27bf414c
JM
6448 case CPP_KEYWORD:
6449 switch (c_parser_peek_token (parser)->keyword)
6450 {
6451 case RID_SIZEOF:
6452 return c_parser_sizeof_expression (parser);
6453 case RID_ALIGNOF:
6454 return c_parser_alignof_expression (parser);
6455 case RID_EXTENSION:
6456 c_parser_consume_token (parser);
6457 ext = disable_extension_diagnostics ();
6458 ret = c_parser_cast_expression (parser, NULL);
6459 restore_extension_diagnostics (ext);
6460 return ret;
6461 case RID_REALPART:
6462 c_parser_consume_token (parser);
c2255bc4 6463 exp_loc = c_parser_peek_token (parser)->location;
46bdb9cf 6464 op = c_parser_cast_expression (parser, NULL);
c2255bc4
AH
6465 op = default_function_array_conversion (exp_loc, op);
6466 return parser_build_unary_op (op_loc, REALPART_EXPR, op);
27bf414c
JM
6467 case RID_IMAGPART:
6468 c_parser_consume_token (parser);
c2255bc4 6469 exp_loc = c_parser_peek_token (parser)->location;
46bdb9cf 6470 op = c_parser_cast_expression (parser, NULL);
c2255bc4
AH
6471 op = default_function_array_conversion (exp_loc, op);
6472 return parser_build_unary_op (op_loc, IMAGPART_EXPR, op);
0a35513e
AH
6473 case RID_TRANSACTION_ATOMIC:
6474 case RID_TRANSACTION_RELAXED:
6475 return c_parser_transaction_expression (parser,
6476 c_parser_peek_token (parser)->keyword);
27bf414c
JM
6477 default:
6478 return c_parser_postfix_expression (parser);
6479 }
6480 default:
6481 return c_parser_postfix_expression (parser);
6482 }
6483}
6484
6485/* Parse a sizeof expression. */
6486
6487static struct c_expr
6488c_parser_sizeof_expression (c_parser *parser)
6489{
6490 struct c_expr expr;
c7412148 6491 location_t expr_loc;
27bf414c
JM
6492 gcc_assert (c_parser_next_token_is_keyword (parser, RID_SIZEOF));
6493 c_parser_consume_token (parser);
7d882b83 6494 c_inhibit_evaluation_warnings++;
27bf414c
JM
6495 in_sizeof++;
6496 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
6497 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
6498 {
6499 /* Either sizeof ( type-name ) or sizeof unary-expression
6500 starting with a compound literal. */
6501 struct c_type_name *type_name;
6502 c_parser_consume_token (parser);
c7412148 6503 expr_loc = c_parser_peek_token (parser)->location;
27bf414c
JM
6504 type_name = c_parser_type_name (parser);
6505 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
6506 if (type_name == NULL)
6507 {
6508 struct c_expr ret;
7d882b83 6509 c_inhibit_evaluation_warnings--;
27bf414c
JM
6510 in_sizeof--;
6511 ret.value = error_mark_node;
6512 ret.original_code = ERROR_MARK;
6866c6e8 6513 ret.original_type = NULL;
27bf414c
JM
6514 return ret;
6515 }
6516 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
6517 {
6518 expr = c_parser_postfix_expression_after_paren_type (parser,
24b97832
ILT
6519 type_name,
6520 expr_loc);
27bf414c
JM
6521 goto sizeof_expr;
6522 }
6523 /* sizeof ( type-name ). */
7d882b83 6524 c_inhibit_evaluation_warnings--;
27bf414c 6525 in_sizeof--;
c2255bc4 6526 return c_expr_sizeof_type (expr_loc, type_name);
27bf414c
JM
6527 }
6528 else
6529 {
c7412148 6530 expr_loc = c_parser_peek_token (parser)->location;
27bf414c
JM
6531 expr = c_parser_unary_expression (parser);
6532 sizeof_expr:
7d882b83 6533 c_inhibit_evaluation_warnings--;
27bf414c 6534 in_sizeof--;
ebfbbdc5 6535 mark_exp_read (expr.value);
27bf414c
JM
6536 if (TREE_CODE (expr.value) == COMPONENT_REF
6537 && DECL_C_BIT_FIELD (TREE_OPERAND (expr.value, 1)))
3ba09659 6538 error_at (expr_loc, "%<sizeof%> applied to a bit-field");
c2255bc4 6539 return c_expr_sizeof_expr (expr_loc, expr);
27bf414c
JM
6540 }
6541}
6542
6543/* Parse an alignof expression. */
6544
6545static struct c_expr
6546c_parser_alignof_expression (c_parser *parser)
6547{
6548 struct c_expr expr;
c2255bc4 6549 location_t loc = c_parser_peek_token (parser)->location;
d19fa6b5 6550 tree alignof_spelling = c_parser_peek_token (parser)->value;
27bf414c 6551 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ALIGNOF));
296674db
JM
6552 bool is_c11_alignof = strcmp (IDENTIFIER_POINTER (alignof_spelling),
6553 "_Alignof") == 0;
d19fa6b5 6554 /* A diagnostic is not required for the use of this identifier in
48b0b196 6555 the implementation namespace; only diagnose it for the C11
d19fa6b5 6556 spelling because of existing code using the other spellings. */
296674db 6557 if (!flag_isoc11 && is_c11_alignof)
d19fa6b5
JM
6558 {
6559 if (flag_isoc99)
c1771a20 6560 pedwarn (loc, OPT_Wpedantic, "ISO C99 does not support %qE",
d19fa6b5
JM
6561 alignof_spelling);
6562 else
c1771a20 6563 pedwarn (loc, OPT_Wpedantic, "ISO C90 does not support %qE",
d19fa6b5
JM
6564 alignof_spelling);
6565 }
27bf414c 6566 c_parser_consume_token (parser);
7d882b83 6567 c_inhibit_evaluation_warnings++;
27bf414c
JM
6568 in_alignof++;
6569 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
6570 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
6571 {
6572 /* Either __alignof__ ( type-name ) or __alignof__
6573 unary-expression starting with a compound literal. */
24b97832 6574 location_t loc;
27bf414c
JM
6575 struct c_type_name *type_name;
6576 struct c_expr ret;
6577 c_parser_consume_token (parser);
24b97832 6578 loc = c_parser_peek_token (parser)->location;
27bf414c
JM
6579 type_name = c_parser_type_name (parser);
6580 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
6581 if (type_name == NULL)
6582 {
6583 struct c_expr ret;
7d882b83 6584 c_inhibit_evaluation_warnings--;
27bf414c
JM
6585 in_alignof--;
6586 ret.value = error_mark_node;
6587 ret.original_code = ERROR_MARK;
6866c6e8 6588 ret.original_type = NULL;
27bf414c
JM
6589 return ret;
6590 }
6591 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
6592 {
6593 expr = c_parser_postfix_expression_after_paren_type (parser,
24b97832
ILT
6594 type_name,
6595 loc);
27bf414c
JM
6596 goto alignof_expr;
6597 }
6598 /* alignof ( type-name ). */
7d882b83 6599 c_inhibit_evaluation_warnings--;
27bf414c 6600 in_alignof--;
296674db
JM
6601 ret.value = c_sizeof_or_alignof_type (loc, groktypename (type_name,
6602 NULL, NULL),
6603 false, is_c11_alignof, 1);
27bf414c 6604 ret.original_code = ERROR_MARK;
6866c6e8 6605 ret.original_type = NULL;
27bf414c
JM
6606 return ret;
6607 }
6608 else
6609 {
6610 struct c_expr ret;
6611 expr = c_parser_unary_expression (parser);
6612 alignof_expr:
ebfbbdc5 6613 mark_exp_read (expr.value);
7d882b83 6614 c_inhibit_evaluation_warnings--;
27bf414c 6615 in_alignof--;
c1771a20 6616 pedwarn (loc, OPT_Wpedantic, "ISO C does not allow %<%E (expression)%>",
d19fa6b5 6617 alignof_spelling);
c2255bc4 6618 ret.value = c_alignof_expr (loc, expr.value);
27bf414c 6619 ret.original_code = ERROR_MARK;
6866c6e8 6620 ret.original_type = NULL;
27bf414c
JM
6621 return ret;
6622 }
6623}
6624
f90e8e2e 6625/* Helper function to read arguments of builtins which are interfaces
2205ed25 6626 for the middle-end nodes like COMPLEX_EXPR, VEC_PERM_EXPR and
f90e8e2e
AS
6627 others. The name of the builtin is passed using BNAME parameter.
6628 Function returns true if there were no errors while parsing and
86785830 6629 stores the arguments in CEXPR_LIST. */
f90e8e2e
AS
6630static bool
6631c_parser_get_builtin_args (c_parser *parser, const char *bname,
4e7d7b3d
JJ
6632 vec<c_expr_t, va_gc> **ret_cexpr_list,
6633 bool choose_expr_p)
f90e8e2e
AS
6634{
6635 location_t loc = c_parser_peek_token (parser)->location;
9771b263 6636 vec<c_expr_t, va_gc> *cexpr_list;
86785830 6637 c_expr_t expr;
4e7d7b3d 6638 bool saved_force_folding_builtin_constant_p;
f90e8e2e 6639
86785830 6640 *ret_cexpr_list = NULL;
f90e8e2e
AS
6641 if (c_parser_next_token_is_not (parser, CPP_OPEN_PAREN))
6642 {
6643 error_at (loc, "cannot take address of %qs", bname);
6644 return false;
6645 }
6646
6647 c_parser_consume_token (parser);
6648
6649 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
6650 {
6651 c_parser_consume_token (parser);
6652 return true;
6653 }
86785830 6654
4e7d7b3d
JJ
6655 saved_force_folding_builtin_constant_p
6656 = force_folding_builtin_constant_p;
6657 force_folding_builtin_constant_p |= choose_expr_p;
86785830 6658 expr = c_parser_expr_no_commas (parser, NULL);
4e7d7b3d
JJ
6659 force_folding_builtin_constant_p
6660 = saved_force_folding_builtin_constant_p;
9771b263 6661 vec_alloc (cexpr_list, 1);
b581c05c 6662 vec_safe_push (cexpr_list, expr);
86785830
AS
6663 while (c_parser_next_token_is (parser, CPP_COMMA))
6664 {
6665 c_parser_consume_token (parser);
6666 expr = c_parser_expr_no_commas (parser, NULL);
b581c05c 6667 vec_safe_push (cexpr_list, expr);
86785830 6668 }
f90e8e2e
AS
6669
6670 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
6671 return false;
6672
86785830 6673 *ret_cexpr_list = cexpr_list;
f90e8e2e
AS
6674 return true;
6675}
6676
433cc7b0
TT
6677/* This represents a single generic-association. */
6678
6679struct c_generic_association
6680{
6681 /* The location of the starting token of the type. */
6682 location_t type_location;
fb48aadc 6683 /* The association's type, or NULL_TREE for 'default'. */
433cc7b0
TT
6684 tree type;
6685 /* The association's expression. */
6686 struct c_expr expression;
6687};
6688
6689/* Parse a generic-selection. (C11 6.5.1.1).
6690
6691 generic-selection:
6692 _Generic ( assignment-expression , generic-assoc-list )
6693
6694 generic-assoc-list:
6695 generic-association
6696 generic-assoc-list , generic-association
6697
6698 generic-association:
6699 type-name : assignment-expression
6700 default : assignment-expression
6701*/
6702
6703static struct c_expr
6704c_parser_generic_selection (c_parser *parser)
6705{
6706 vec<c_generic_association> associations = vNULL;
6707 struct c_expr selector, error_expr;
6708 tree selector_type;
6709 struct c_generic_association matched_assoc;
6710 bool match_found = false;
6711 location_t generic_loc, selector_loc;
6712
6713 error_expr.original_code = ERROR_MARK;
6714 error_expr.original_type = NULL;
6715 error_expr.value = error_mark_node;
6716 matched_assoc.type_location = UNKNOWN_LOCATION;
6717 matched_assoc.type = NULL_TREE;
6718 matched_assoc.expression = error_expr;
6719
6720 gcc_assert (c_parser_next_token_is_keyword (parser, RID_GENERIC));
6721 generic_loc = c_parser_peek_token (parser)->location;
6722 c_parser_consume_token (parser);
6723 if (!flag_isoc11)
6724 {
6725 if (flag_isoc99)
6726 pedwarn (generic_loc, OPT_Wpedantic,
6727 "ISO C99 does not support %<_Generic%>");
6728 else
6729 pedwarn (generic_loc, OPT_Wpedantic,
6730 "ISO C90 does not support %<_Generic%>");
6731 }
6732
6733 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6734 return error_expr;
6735
6736 c_inhibit_evaluation_warnings++;
6737 selector_loc = c_parser_peek_token (parser)->location;
6738 selector = c_parser_expr_no_commas (parser, NULL);
6739 selector = default_function_array_conversion (selector_loc, selector);
6740 c_inhibit_evaluation_warnings--;
6741
6742 if (selector.value == error_mark_node)
6743 {
6744 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6745 return selector;
6746 }
6747 selector_type = TREE_TYPE (selector.value);
6748 /* In ISO C terms, rvalues (including the controlling expression of
6749 _Generic) do not have qualified types. */
6750 if (TREE_CODE (selector_type) != ARRAY_TYPE)
6751 selector_type = TYPE_MAIN_VARIANT (selector_type);
6752 /* In ISO C terms, _Noreturn is not part of the type of expressions
6753 such as &abort, but in GCC it is represented internally as a type
6754 qualifier. */
6755 if (FUNCTION_POINTER_TYPE_P (selector_type)
6756 && TYPE_QUALS (TREE_TYPE (selector_type)) != TYPE_UNQUALIFIED)
6757 selector_type
6758 = build_pointer_type (TYPE_MAIN_VARIANT (TREE_TYPE (selector_type)));
6759
6760 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
6761 {
6762 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6763 return error_expr;
6764 }
6765
6766 while (1)
6767 {
6768 struct c_generic_association assoc, *iter;
6769 unsigned int ix;
6770 c_token *token = c_parser_peek_token (parser);
6771
6772 assoc.type_location = token->location;
6773 if (token->type == CPP_KEYWORD && token->keyword == RID_DEFAULT)
6774 {
6775 c_parser_consume_token (parser);
6776 assoc.type = NULL_TREE;
6777 }
6778 else
6779 {
6780 struct c_type_name *type_name;
6781
6782 type_name = c_parser_type_name (parser);
6783 if (type_name == NULL)
6784 {
6785 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6786 goto error_exit;
6787 }
6788 assoc.type = groktypename (type_name, NULL, NULL);
6789 if (assoc.type == error_mark_node)
6790 {
6791 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6792 goto error_exit;
6793 }
6794
6795 if (TREE_CODE (assoc.type) == FUNCTION_TYPE)
6796 error_at (assoc.type_location,
6797 "%<_Generic%> association has function type");
6798 else if (!COMPLETE_TYPE_P (assoc.type))
6799 error_at (assoc.type_location,
6800 "%<_Generic%> association has incomplete type");
6801
6802 if (variably_modified_type_p (assoc.type, NULL_TREE))
6803 error_at (assoc.type_location,
6804 "%<_Generic%> association has "
6805 "variable length type");
6806 }
6807
6808 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
6809 {
6810 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6811 goto error_exit;
6812 }
6813
6814 assoc.expression = c_parser_expr_no_commas (parser, NULL);
6815 if (assoc.expression.value == error_mark_node)
6816 {
6817 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6818 goto error_exit;
6819 }
6820
6821 for (ix = 0; associations.iterate (ix, &iter); ++ix)
6822 {
6823 if (assoc.type == NULL_TREE)
6824 {
6825 if (iter->type == NULL_TREE)
6826 {
6827 error_at (assoc.type_location,
6828 "duplicate %<default%> case in %<_Generic%>");
6829 inform (iter->type_location, "original %<default%> is here");
6830 }
6831 }
6832 else if (iter->type != NULL_TREE)
6833 {
6834 if (comptypes (assoc.type, iter->type))
6835 {
6836 error_at (assoc.type_location,
6837 "%<_Generic%> specifies two compatible types");
6838 inform (iter->type_location, "compatible type is here");
6839 }
6840 }
6841 }
6842
6843 if (assoc.type == NULL_TREE)
6844 {
6845 if (!match_found)
6846 {
6847 matched_assoc = assoc;
6848 match_found = true;
6849 }
6850 }
6851 else if (comptypes (assoc.type, selector_type))
6852 {
6853 if (!match_found || matched_assoc.type == NULL_TREE)
6854 {
6855 matched_assoc = assoc;
6856 match_found = true;
6857 }
6858 else
6859 {
6860 error_at (assoc.type_location,
6861 "%<_Generic> selector matches multiple associations");
6862 inform (matched_assoc.type_location,
6863 "other match is here");
6864 }
6865 }
6866
6867 associations.safe_push (assoc);
6868
6869 if (c_parser_peek_token (parser)->type != CPP_COMMA)
6870 break;
6871 c_parser_consume_token (parser);
6872 }
6873
6874 associations.release ();
6875
6876 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
6877 {
6878 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6879 return error_expr;
6880 }
6881
6882 if (!match_found)
6883 {
6884 error_at (selector_loc, "%<_Generic%> selector of type %qT is not "
6885 "compatible with any association",
6886 selector_type);
6887 return error_expr;
6888 }
6889
6890 return matched_assoc.expression;
6891
6892 error_exit:
6893 associations.release ();
6894 return error_expr;
6895}
f90e8e2e 6896
27bf414c
JM
6897/* Parse a postfix expression (C90 6.3.1-6.3.2, C99 6.5.1-6.5.2).
6898
6899 postfix-expression:
6900 primary-expression
6901 postfix-expression [ expression ]
6902 postfix-expression ( argument-expression-list[opt] )
6903 postfix-expression . identifier
6904 postfix-expression -> identifier
6905 postfix-expression ++
6906 postfix-expression --
6907 ( type-name ) { initializer-list }
6908 ( type-name ) { initializer-list , }
6909
6910 argument-expression-list:
6911 argument-expression
6912 argument-expression-list , argument-expression
6913
6914 primary-expression:
6915 identifier
6916 constant
6917 string-literal
6918 ( expression )
433cc7b0 6919 generic-selection
27bf414c
JM
6920
6921 GNU extensions:
6922
6923 primary-expression:
6924 __func__
6925 (treated as a keyword in GNU C)
6926 __FUNCTION__
6927 __PRETTY_FUNCTION__
6928 ( compound-statement )
6929 __builtin_va_arg ( assignment-expression , type-name )
6930 __builtin_offsetof ( type-name , offsetof-member-designator )
6931 __builtin_choose_expr ( assignment-expression ,
6932 assignment-expression ,
6933 assignment-expression )
6934 __builtin_types_compatible_p ( type-name , type-name )
d4a83c10 6935 __builtin_complex ( assignment-expression , assignment-expression )
f90e8e2e
AS
6936 __builtin_shuffle ( assignment-expression , assignment-expression )
6937 __builtin_shuffle ( assignment-expression ,
6938 assignment-expression ,
6939 assignment-expression, )
27bf414c
JM
6940
6941 offsetof-member-designator:
6942 identifier
6943 offsetof-member-designator . identifier
6944 offsetof-member-designator [ expression ]
6945
6946 Objective-C:
6947
6948 primary-expression:
6949 [ objc-receiver objc-message-args ]
6950 @selector ( objc-selector-arg )
6951 @protocol ( identifier )
6952 @encode ( type-name )
6953 objc-string-literal
bede2adc 6954 Classname . identifier
27bf414c
JM
6955*/
6956
6957static struct c_expr
6958c_parser_postfix_expression (c_parser *parser)
6959{
f90e8e2e 6960 struct c_expr expr, e1;
27bf414c 6961 struct c_type_name *t1, *t2;
c2255bc4 6962 location_t loc = c_parser_peek_token (parser)->location;;
6866c6e8
ILT
6963 expr.original_code = ERROR_MARK;
6964 expr.original_type = NULL;
27bf414c
JM
6965 switch (c_parser_peek_token (parser)->type)
6966 {
6967 case CPP_NUMBER:
754ccf7c 6968 expr.value = c_parser_peek_token (parser)->value;
754ccf7c
JJ
6969 loc = c_parser_peek_token (parser)->location;
6970 c_parser_consume_token (parser);
6971 if (TREE_CODE (expr.value) == FIXED_CST
6972 && !targetm.fixed_point_supported_p ())
6973 {
6974 error_at (loc, "fixed-point types not supported for this target");
6975 expr.value = error_mark_node;
6976 }
6977 break;
27bf414c 6978 case CPP_CHAR:
b6baa67d
KVH
6979 case CPP_CHAR16:
6980 case CPP_CHAR32:
27bf414c
JM
6981 case CPP_WCHAR:
6982 expr.value = c_parser_peek_token (parser)->value;
27bf414c
JM
6983 c_parser_consume_token (parser);
6984 break;
6985 case CPP_STRING:
b6baa67d
KVH
6986 case CPP_STRING16:
6987 case CPP_STRING32:
27bf414c 6988 case CPP_WSTRING:
2c6e3f55 6989 case CPP_UTF8STRING:
27bf414c
JM
6990 expr.value = c_parser_peek_token (parser)->value;
6991 expr.original_code = STRING_CST;
6992 c_parser_consume_token (parser);
6993 break;
6994 case CPP_OBJC_STRING:
6995 gcc_assert (c_dialect_objc ());
6996 expr.value
6997 = objc_build_string_object (c_parser_peek_token (parser)->value);
27bf414c
JM
6998 c_parser_consume_token (parser);
6999 break;
7000 case CPP_NAME:
bede2adc 7001 switch (c_parser_peek_token (parser)->id_kind)
27bf414c 7002 {
bede2adc
NP
7003 case C_ID_ID:
7004 {
7005 tree id = c_parser_peek_token (parser)->value;
7006 c_parser_consume_token (parser);
7007 expr.value = build_external_ref (loc, id,
7008 (c_parser_peek_token (parser)->type
7009 == CPP_OPEN_PAREN),
7010 &expr.original_type);
7011 break;
7012 }
7013 case C_ID_CLASSNAME:
7014 {
7015 /* Here we parse the Objective-C 2.0 Class.name dot
7016 syntax. */
7017 tree class_name = c_parser_peek_token (parser)->value;
7018 tree component;
7019 c_parser_consume_token (parser);
7020 gcc_assert (c_dialect_objc ());
7021 if (!c_parser_require (parser, CPP_DOT, "expected %<.%>"))
7022 {
7023 expr.value = error_mark_node;
7024 break;
7025 }
7026 if (c_parser_next_token_is_not (parser, CPP_NAME))
7027 {
7028 c_parser_error (parser, "expected identifier");
7029 expr.value = error_mark_node;
7030 break;
7031 }
7032 component = c_parser_peek_token (parser)->value;
7033 c_parser_consume_token (parser);
7034 expr.value = objc_build_class_component_ref (class_name,
7035 component);
7036 break;
7037 }
7038 default:
27bf414c
JM
7039 c_parser_error (parser, "expected expression");
7040 expr.value = error_mark_node;
27bf414c
JM
7041 break;
7042 }
27bf414c
JM
7043 break;
7044 case CPP_OPEN_PAREN:
7045 /* A parenthesized expression, statement expression or compound
7046 literal. */
7047 if (c_parser_peek_2nd_token (parser)->type == CPP_OPEN_BRACE)
7048 {
7049 /* A statement expression. */
7050 tree stmt;
c2255bc4 7051 location_t brace_loc;
27bf414c 7052 c_parser_consume_token (parser);
c2255bc4 7053 brace_loc = c_parser_peek_token (parser)->location;
27bf414c 7054 c_parser_consume_token (parser);
38e01f9e 7055 if (!building_stmt_list_p ())
27bf414c 7056 {
c2255bc4 7057 error_at (loc, "braced-group within expression allowed "
3ba09659 7058 "only inside a function");
27bf414c
JM
7059 parser->error = true;
7060 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
7061 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7062 expr.value = error_mark_node;
27bf414c
JM
7063 break;
7064 }
7065 stmt = c_begin_stmt_expr ();
7066 c_parser_compound_statement_nostart (parser);
7067 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7068 "expected %<)%>");
c1771a20 7069 pedwarn (loc, OPT_Wpedantic,
509c9d60 7070 "ISO C forbids braced-groups within expressions");
c2255bc4 7071 expr.value = c_finish_stmt_expr (brace_loc, stmt);
82c3c067 7072 mark_exp_read (expr.value);
27bf414c
JM
7073 }
7074 else if (c_token_starts_typename (c_parser_peek_2nd_token (parser)))
7075 {
7076 /* A compound literal. ??? Can we actually get here rather
7077 than going directly to
7078 c_parser_postfix_expression_after_paren_type from
7079 elsewhere? */
24b97832 7080 location_t loc;
27bf414c
JM
7081 struct c_type_name *type_name;
7082 c_parser_consume_token (parser);
24b97832 7083 loc = c_parser_peek_token (parser)->location;
27bf414c
JM
7084 type_name = c_parser_type_name (parser);
7085 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7086 "expected %<)%>");
7087 if (type_name == NULL)
7088 {
7089 expr.value = error_mark_node;
27bf414c
JM
7090 }
7091 else
7092 expr = c_parser_postfix_expression_after_paren_type (parser,
24b97832
ILT
7093 type_name,
7094 loc);
27bf414c
JM
7095 }
7096 else
7097 {
7098 /* A parenthesized expression. */
7099 c_parser_consume_token (parser);
7100 expr = c_parser_expression (parser);
7101 if (TREE_CODE (expr.value) == MODIFY_EXPR)
7102 TREE_NO_WARNING (expr.value) = 1;
928c19bb
JM
7103 if (expr.original_code != C_MAYBE_CONST_EXPR)
7104 expr.original_code = ERROR_MARK;
6866c6e8 7105 /* Don't change EXPR.ORIGINAL_TYPE. */
27bf414c
JM
7106 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7107 "expected %<)%>");
7108 }
7109 break;
7110 case CPP_KEYWORD:
7111 switch (c_parser_peek_token (parser)->keyword)
7112 {
7113 case RID_FUNCTION_NAME:
7114 case RID_PRETTY_FUNCTION_NAME:
7115 case RID_C99_FUNCTION_NAME:
c2255bc4 7116 expr.value = fname_decl (loc,
3ba09659 7117 c_parser_peek_token (parser)->keyword,
27bf414c 7118 c_parser_peek_token (parser)->value);
27bf414c
JM
7119 c_parser_consume_token (parser);
7120 break;
7121 case RID_VA_ARG:
7122 c_parser_consume_token (parser);
7123 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7124 {
7125 expr.value = error_mark_node;
27bf414c
JM
7126 break;
7127 }
7128 e1 = c_parser_expr_no_commas (parser, NULL);
ebfbbdc5 7129 mark_exp_read (e1.value);
928c19bb 7130 e1.value = c_fully_fold (e1.value, false, NULL);
27bf414c
JM
7131 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
7132 {
7133 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7134 expr.value = error_mark_node;
27bf414c
JM
7135 break;
7136 }
72b5577d 7137 loc = c_parser_peek_token (parser)->location;
27bf414c
JM
7138 t1 = c_parser_type_name (parser);
7139 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7140 "expected %<)%>");
7141 if (t1 == NULL)
7142 {
7143 expr.value = error_mark_node;
27bf414c
JM
7144 }
7145 else
7146 {
928c19bb 7147 tree type_expr = NULL_TREE;
c2255bc4
AH
7148 expr.value = c_build_va_arg (loc, e1.value,
7149 groktypename (t1, &type_expr, NULL));
928c19bb
JM
7150 if (type_expr)
7151 {
7152 expr.value = build2 (C_MAYBE_CONST_EXPR,
7153 TREE_TYPE (expr.value), type_expr,
7154 expr.value);
7155 C_MAYBE_CONST_EXPR_NON_CONST (expr.value) = true;
7156 }
27bf414c
JM
7157 }
7158 break;
7159 case RID_OFFSETOF:
7160 c_parser_consume_token (parser);
7161 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7162 {
7163 expr.value = error_mark_node;
27bf414c
JM
7164 break;
7165 }
7166 t1 = c_parser_type_name (parser);
7167 if (t1 == NULL)
29ce73cb 7168 parser->error = true;
27bf414c 7169 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
29ce73cb
PB
7170 gcc_assert (parser->error);
7171 if (parser->error)
27bf414c
JM
7172 {
7173 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7174 expr.value = error_mark_node;
27bf414c
JM
7175 break;
7176 }
29ce73cb 7177
27bf414c 7178 {
928c19bb 7179 tree type = groktypename (t1, NULL, NULL);
27bf414c
JM
7180 tree offsetof_ref;
7181 if (type == error_mark_node)
7182 offsetof_ref = error_mark_node;
7183 else
c2255bc4
AH
7184 {
7185 offsetof_ref = build1 (INDIRECT_REF, type, null_pointer_node);
7186 SET_EXPR_LOCATION (offsetof_ref, loc);
7187 }
27bf414c
JM
7188 /* Parse the second argument to __builtin_offsetof. We
7189 must have one identifier, and beyond that we want to
7190 accept sub structure and sub array references. */
7191 if (c_parser_next_token_is (parser, CPP_NAME))
7192 {
7193 offsetof_ref = build_component_ref
c2255bc4 7194 (loc, offsetof_ref, c_parser_peek_token (parser)->value);
27bf414c
JM
7195 c_parser_consume_token (parser);
7196 while (c_parser_next_token_is (parser, CPP_DOT)
7197 || c_parser_next_token_is (parser,
634b5df5
JJ
7198 CPP_OPEN_SQUARE)
7199 || c_parser_next_token_is (parser,
7200 CPP_DEREF))
27bf414c 7201 {
634b5df5
JJ
7202 if (c_parser_next_token_is (parser, CPP_DEREF))
7203 {
7204 loc = c_parser_peek_token (parser)->location;
c2255bc4
AH
7205 offsetof_ref = build_array_ref (loc,
7206 offsetof_ref,
7207 integer_zero_node);
634b5df5
JJ
7208 goto do_dot;
7209 }
7210 else if (c_parser_next_token_is (parser, CPP_DOT))
27bf414c 7211 {
634b5df5 7212 do_dot:
27bf414c
JM
7213 c_parser_consume_token (parser);
7214 if (c_parser_next_token_is_not (parser,
7215 CPP_NAME))
7216 {
7217 c_parser_error (parser, "expected identifier");
7218 break;
7219 }
7220 offsetof_ref = build_component_ref
c2255bc4 7221 (loc, offsetof_ref,
27bf414c
JM
7222 c_parser_peek_token (parser)->value);
7223 c_parser_consume_token (parser);
7224 }
7225 else
7226 {
267bac10 7227 struct c_expr ce;
27bf414c 7228 tree idx;
6a3799eb 7229 loc = c_parser_peek_token (parser)->location;
27bf414c 7230 c_parser_consume_token (parser);
267bac10
JM
7231 ce = c_parser_expression (parser);
7232 ce = convert_lvalue_to_rvalue (loc, ce, false, false);
7233 idx = ce.value;
928c19bb 7234 idx = c_fully_fold (idx, false, NULL);
27bf414c
JM
7235 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
7236 "expected %<]%>");
c2255bc4 7237 offsetof_ref = build_array_ref (loc, offsetof_ref, idx);
27bf414c
JM
7238 }
7239 }
7240 }
7241 else
7242 c_parser_error (parser, "expected identifier");
7243 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7244 "expected %<)%>");
cf9e9959 7245 expr.value = fold_offsetof (offsetof_ref);
27bf414c
JM
7246 }
7247 break;
7248 case RID_CHOOSE_EXPR:
27bf414c 7249 {
9771b263 7250 vec<c_expr_t, va_gc> *cexpr_list;
86785830
AS
7251 c_expr_t *e1_p, *e2_p, *e3_p;
7252 tree c;
27bf414c 7253
f90e8e2e
AS
7254 c_parser_consume_token (parser);
7255 if (!c_parser_get_builtin_args (parser,
7256 "__builtin_choose_expr",
4e7d7b3d 7257 &cexpr_list, true))
f90e8e2e
AS
7258 {
7259 expr.value = error_mark_node;
7260 break;
7261 }
7262
9771b263 7263 if (vec_safe_length (cexpr_list) != 3)
f90e8e2e
AS
7264 {
7265 error_at (loc, "wrong number of arguments to "
7266 "%<__builtin_choose_expr%>");
7267 expr.value = error_mark_node;
7268 break;
7269 }
86785830 7270
9771b263
DN
7271 e1_p = &(*cexpr_list)[0];
7272 e2_p = &(*cexpr_list)[1];
7273 e3_p = &(*cexpr_list)[2];
86785830
AS
7274
7275 c = e1_p->value;
7276 mark_exp_read (e2_p->value);
7277 mark_exp_read (e3_p->value);
928c19bb
JM
7278 if (TREE_CODE (c) != INTEGER_CST
7279 || !INTEGRAL_TYPE_P (TREE_TYPE (c)))
3ba09659
AH
7280 error_at (loc,
7281 "first argument to %<__builtin_choose_expr%> not"
7282 " a constant");
928c19bb 7283 constant_expression_warning (c);
86785830 7284 expr = integer_zerop (c) ? *e3_p : *e2_p;
f90e8e2e 7285 break;
27bf414c 7286 }
27bf414c
JM
7287 case RID_TYPES_COMPATIBLE_P:
7288 c_parser_consume_token (parser);
7289 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7290 {
7291 expr.value = error_mark_node;
27bf414c
JM
7292 break;
7293 }
7294 t1 = c_parser_type_name (parser);
7295 if (t1 == NULL)
7296 {
7297 expr.value = error_mark_node;
27bf414c
JM
7298 break;
7299 }
7300 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
7301 {
7302 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7303 expr.value = error_mark_node;
27bf414c
JM
7304 break;
7305 }
7306 t2 = c_parser_type_name (parser);
7307 if (t2 == NULL)
7308 {
7309 expr.value = error_mark_node;
27bf414c
JM
7310 break;
7311 }
7312 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7313 "expected %<)%>");
7314 {
7315 tree e1, e2;
9abfe986
AP
7316 e1 = groktypename (t1, NULL, NULL);
7317 e2 = groktypename (t2, NULL, NULL);
7318 if (e1 == error_mark_node || e2 == error_mark_node)
7319 {
7320 expr.value = error_mark_node;
7321 break;
7322 }
27bf414c 7323
9abfe986
AP
7324 e1 = TYPE_MAIN_VARIANT (e1);
7325 e2 = TYPE_MAIN_VARIANT (e2);
27bf414c 7326
9a9d280e
AS
7327 expr.value
7328 = comptypes (e1, e2) ? integer_one_node : integer_zero_node;
27bf414c
JM
7329 }
7330 break;
d4a83c10 7331 case RID_BUILTIN_COMPLEX:
86785830 7332 {
9771b263 7333 vec<c_expr_t, va_gc> *cexpr_list;
86785830
AS
7334 c_expr_t *e1_p, *e2_p;
7335
f90e8e2e
AS
7336 c_parser_consume_token (parser);
7337 if (!c_parser_get_builtin_args (parser,
7338 "__builtin_complex",
4e7d7b3d 7339 &cexpr_list, false))
f90e8e2e
AS
7340 {
7341 expr.value = error_mark_node;
7342 break;
7343 }
7344
9771b263 7345 if (vec_safe_length (cexpr_list) != 2)
f90e8e2e
AS
7346 {
7347 error_at (loc, "wrong number of arguments to "
7348 "%<__builtin_complex%>");
7349 expr.value = error_mark_node;
7350 break;
7351 }
86785830 7352
9771b263
DN
7353 e1_p = &(*cexpr_list)[0];
7354 e2_p = &(*cexpr_list)[1];
86785830 7355
267bac10 7356 *e1_p = convert_lvalue_to_rvalue (loc, *e1_p, true, true);
86785830
AS
7357 if (TREE_CODE (e1_p->value) == EXCESS_PRECISION_EXPR)
7358 e1_p->value = convert (TREE_TYPE (e1_p->value),
7359 TREE_OPERAND (e1_p->value, 0));
267bac10 7360 *e2_p = convert_lvalue_to_rvalue (loc, *e2_p, true, true);
86785830
AS
7361 if (TREE_CODE (e2_p->value) == EXCESS_PRECISION_EXPR)
7362 e2_p->value = convert (TREE_TYPE (e2_p->value),
7363 TREE_OPERAND (e2_p->value, 0));
7364 if (!SCALAR_FLOAT_TYPE_P (TREE_TYPE (e1_p->value))
7365 || DECIMAL_FLOAT_TYPE_P (TREE_TYPE (e1_p->value))
7366 || !SCALAR_FLOAT_TYPE_P (TREE_TYPE (e2_p->value))
7367 || DECIMAL_FLOAT_TYPE_P (TREE_TYPE (e2_p->value)))
f90e8e2e
AS
7368 {
7369 error_at (loc, "%<__builtin_complex%> operand "
7370 "not of real binary floating-point type");
7371 expr.value = error_mark_node;
7372 break;
7373 }
86785830
AS
7374 if (TYPE_MAIN_VARIANT (TREE_TYPE (e1_p->value))
7375 != TYPE_MAIN_VARIANT (TREE_TYPE (e2_p->value)))
f90e8e2e
AS
7376 {
7377 error_at (loc,
7378 "%<__builtin_complex%> operands of different types");
7379 expr.value = error_mark_node;
7380 break;
7381 }
7382 if (!flag_isoc99)
c1771a20 7383 pedwarn (loc, OPT_Wpedantic,
f90e8e2e
AS
7384 "ISO C90 does not support complex types");
7385 expr.value = build2 (COMPLEX_EXPR,
86785830
AS
7386 build_complex_type
7387 (TYPE_MAIN_VARIANT
7388 (TREE_TYPE (e1_p->value))),
7389 e1_p->value, e2_p->value);
f90e8e2e
AS
7390 break;
7391 }
7392 case RID_BUILTIN_SHUFFLE:
7393 {
9771b263 7394 vec<c_expr_t, va_gc> *cexpr_list;
9243c51d
JJ
7395 unsigned int i;
7396 c_expr_t *p;
86785830 7397
f90e8e2e
AS
7398 c_parser_consume_token (parser);
7399 if (!c_parser_get_builtin_args (parser,
7400 "__builtin_shuffle",
4e7d7b3d 7401 &cexpr_list, false))
f90e8e2e
AS
7402 {
7403 expr.value = error_mark_node;
7404 break;
7405 }
7406
9771b263 7407 FOR_EACH_VEC_SAFE_ELT (cexpr_list, i, p)
267bac10 7408 *p = convert_lvalue_to_rvalue (loc, *p, true, true);
9243c51d 7409
9771b263 7410 if (vec_safe_length (cexpr_list) == 2)
86785830 7411 expr.value =
2205ed25 7412 c_build_vec_perm_expr
9771b263
DN
7413 (loc, (*cexpr_list)[0].value,
7414 NULL_TREE, (*cexpr_list)[1].value);
86785830 7415
9771b263 7416 else if (vec_safe_length (cexpr_list) == 3)
86785830 7417 expr.value =
2205ed25 7418 c_build_vec_perm_expr
9771b263
DN
7419 (loc, (*cexpr_list)[0].value,
7420 (*cexpr_list)[1].value,
7421 (*cexpr_list)[2].value);
f90e8e2e
AS
7422 else
7423 {
7424 error_at (loc, "wrong number of arguments to "
7425 "%<__builtin_shuffle%>");
7426 expr.value = error_mark_node;
7427 }
7428 break;
7429 }
27bf414c
JM
7430 case RID_AT_SELECTOR:
7431 gcc_assert (c_dialect_objc ());
7432 c_parser_consume_token (parser);
7433 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7434 {
7435 expr.value = error_mark_node;
27bf414c
JM
7436 break;
7437 }
7438 {
7439 tree sel = c_parser_objc_selector_arg (parser);
7440 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7441 "expected %<)%>");
c2255bc4 7442 expr.value = objc_build_selector_expr (loc, sel);
27bf414c
JM
7443 }
7444 break;
7445 case RID_AT_PROTOCOL:
7446 gcc_assert (c_dialect_objc ());
7447 c_parser_consume_token (parser);
7448 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7449 {
7450 expr.value = error_mark_node;
27bf414c
JM
7451 break;
7452 }
7453 if (c_parser_next_token_is_not (parser, CPP_NAME))
7454 {
7455 c_parser_error (parser, "expected identifier");
7456 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7457 expr.value = error_mark_node;
27bf414c
JM
7458 break;
7459 }
7460 {
7461 tree id = c_parser_peek_token (parser)->value;
7462 c_parser_consume_token (parser);
7463 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7464 "expected %<)%>");
7465 expr.value = objc_build_protocol_expr (id);
27bf414c
JM
7466 }
7467 break;
7468 case RID_AT_ENCODE:
7469 /* Extension to support C-structures in the archiver. */
7470 gcc_assert (c_dialect_objc ());
7471 c_parser_consume_token (parser);
7472 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7473 {
7474 expr.value = error_mark_node;
27bf414c
JM
7475 break;
7476 }
7477 t1 = c_parser_type_name (parser);
7478 if (t1 == NULL)
7479 {
7480 expr.value = error_mark_node;
27bf414c
JM
7481 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7482 break;
7483 }
7484 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7485 "expected %<)%>");
7486 {
928c19bb 7487 tree type = groktypename (t1, NULL, NULL);
27bf414c 7488 expr.value = objc_build_encode_expr (type);
27bf414c
JM
7489 }
7490 break;
433cc7b0
TT
7491 case RID_GENERIC:
7492 expr = c_parser_generic_selection (parser);
7493 break;
939b37da
BI
7494 case RID_CILK_SPAWN:
7495 c_parser_consume_token (parser);
b72271b9 7496 if (!flag_cilkplus)
939b37da
BI
7497 {
7498 error_at (loc, "-fcilkplus must be enabled to use "
7499 "%<_Cilk_spawn%>");
7500 expr = c_parser_postfix_expression (parser);
7501 expr.value = error_mark_node;
7502 }
9a74f20c 7503 else if (c_parser_peek_token (parser)->keyword == RID_CILK_SPAWN)
939b37da
BI
7504 {
7505 error_at (loc, "consecutive %<_Cilk_spawn%> keywords "
7506 "are not permitted");
7507 /* Now flush out all the _Cilk_spawns. */
7508 while (c_parser_peek_token (parser)->keyword == RID_CILK_SPAWN)
7509 c_parser_consume_token (parser);
7510 expr = c_parser_postfix_expression (parser);
7511 }
7512 else
7513 {
7514 expr = c_parser_postfix_expression (parser);
7515 expr.value = build_cilk_spawn (loc, expr.value);
7516 }
7517 break;
27bf414c
JM
7518 default:
7519 c_parser_error (parser, "expected expression");
7520 expr.value = error_mark_node;
27bf414c
JM
7521 break;
7522 }
7523 break;
7524 case CPP_OPEN_SQUARE:
7525 if (c_dialect_objc ())
7526 {
7527 tree receiver, args;
7528 c_parser_consume_token (parser);
7529 receiver = c_parser_objc_receiver (parser);
7530 args = c_parser_objc_message_args (parser);
7531 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
7532 "expected %<]%>");
eb345401 7533 expr.value = objc_build_message_expr (receiver, args);
27bf414c
JM
7534 break;
7535 }
7536 /* Else fall through to report error. */
7537 default:
7538 c_parser_error (parser, "expected expression");
7539 expr.value = error_mark_node;
27bf414c
JM
7540 break;
7541 }
c2255bc4 7542 return c_parser_postfix_expression_after_primary (parser, loc, expr);
27bf414c
JM
7543}
7544
7545/* Parse a postfix expression after a parenthesized type name: the
7546 brace-enclosed initializer of a compound literal, possibly followed
7547 by some postfix operators. This is separate because it is not
7548 possible to tell until after the type name whether a cast
7549 expression has a cast or a compound literal, or whether the operand
7550 of sizeof is a parenthesized type name or starts with a compound
24b97832
ILT
7551 literal. TYPE_LOC is the location where TYPE_NAME starts--the
7552 location of the first token after the parentheses around the type
7553 name. */
27bf414c
JM
7554
7555static struct c_expr
7556c_parser_postfix_expression_after_paren_type (c_parser *parser,
24b97832
ILT
7557 struct c_type_name *type_name,
7558 location_t type_loc)
27bf414c
JM
7559{
7560 tree type;
7561 struct c_expr init;
928c19bb 7562 bool non_const;
27bf414c 7563 struct c_expr expr;
c7412148 7564 location_t start_loc;
928c19bb
JM
7565 tree type_expr = NULL_TREE;
7566 bool type_expr_const = true;
c2255bc4 7567 check_compound_literal_type (type_loc, type_name);
27bf414c 7568 start_init (NULL_TREE, NULL, 0);
928c19bb 7569 type = groktypename (type_name, &type_expr, &type_expr_const);
c7412148 7570 start_loc = c_parser_peek_token (parser)->location;
85cad37c 7571 if (type != error_mark_node && C_TYPE_VARIABLE_SIZE (type))
27bf414c 7572 {
24b97832 7573 error_at (type_loc, "compound literal has variable size");
27bf414c
JM
7574 type = error_mark_node;
7575 }
7576 init = c_parser_braced_init (parser, type, false);
7577 finish_init ();
7578 maybe_warn_string_init (type, init);
7579
36c5e70a
BE
7580 if (type != error_mark_node
7581 && !ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (type))
7582 && current_function_decl)
7583 {
7584 error ("compound literal qualified by address-space qualifier");
7585 type = error_mark_node;
7586 }
7587
fcf73884 7588 if (!flag_isoc99)
c1771a20 7589 pedwarn (start_loc, OPT_Wpedantic, "ISO C90 forbids compound literals");
928c19bb
JM
7590 non_const = ((init.value && TREE_CODE (init.value) == CONSTRUCTOR)
7591 ? CONSTRUCTOR_NON_CONST (init.value)
7592 : init.original_code == C_MAYBE_CONST_EXPR);
7593 non_const |= !type_expr_const;
c2255bc4 7594 expr.value = build_compound_literal (start_loc, type, init.value, non_const);
27bf414c 7595 expr.original_code = ERROR_MARK;
6866c6e8 7596 expr.original_type = NULL;
928c19bb
JM
7597 if (type_expr)
7598 {
7599 if (TREE_CODE (expr.value) == C_MAYBE_CONST_EXPR)
7600 {
7601 gcc_assert (C_MAYBE_CONST_EXPR_PRE (expr.value) == NULL_TREE);
7602 C_MAYBE_CONST_EXPR_PRE (expr.value) = type_expr;
7603 }
7604 else
7605 {
7606 gcc_assert (!non_const);
7607 expr.value = build2 (C_MAYBE_CONST_EXPR, type,
7608 type_expr, expr.value);
7609 }
7610 }
c2255bc4 7611 return c_parser_postfix_expression_after_primary (parser, start_loc, expr);
27bf414c
JM
7612}
7613
1a4049e7
JJ
7614/* Callback function for sizeof_pointer_memaccess_warning to compare
7615 types. */
7616
7617static bool
7618sizeof_ptr_memacc_comptypes (tree type1, tree type2)
7619{
7620 return comptypes (type1, type2) == 1;
7621}
7622
27bf414c 7623/* Parse a postfix expression after the initial primary or compound
c2255bc4
AH
7624 literal; that is, parse a series of postfix operators.
7625
7626 EXPR_LOC is the location of the primary expression. */
27bf414c
JM
7627
7628static struct c_expr
7629c_parser_postfix_expression_after_primary (c_parser *parser,
c2255bc4 7630 location_t expr_loc,
27bf414c
JM
7631 struct c_expr expr)
7632{
928c19bb 7633 struct c_expr orig_expr;
bbbbb16a 7634 tree ident, idx;
3a785c97
JJ
7635 location_t sizeof_arg_loc[3];
7636 tree sizeof_arg[3];
7637 unsigned int i;
9771b263 7638 vec<tree, va_gc> *exprlist;
2ee028f1 7639 vec<tree, va_gc> *origtypes = NULL;
81e5eca8
MP
7640 vec<location_t> arg_loc = vNULL;
7641
27bf414c
JM
7642 while (true)
7643 {
c2255bc4 7644 location_t op_loc = c_parser_peek_token (parser)->location;
27bf414c
JM
7645 switch (c_parser_peek_token (parser)->type)
7646 {
7647 case CPP_OPEN_SQUARE:
7648 /* Array reference. */
7649 c_parser_consume_token (parser);
b72271b9 7650 if (flag_cilkplus
36536d79
BI
7651 && c_parser_peek_token (parser)->type == CPP_COLON)
7652 /* If we are here, then we have something like this:
7653 Array [ : ]
7654 */
7655 expr.value = c_parser_array_notation (expr_loc, parser, NULL_TREE,
7656 expr.value);
7657 else
7658 {
7659 idx = c_parser_expression (parser).value;
7660 /* Here we have 3 options:
7661 1. Array [EXPR] -- Normal Array call.
7662 2. Array [EXPR : EXPR] -- Array notation without stride.
7663 3. Array [EXPR : EXPR : EXPR] -- Array notation with stride.
7664
7665 For 1, we just handle it just like a normal array expression.
7666 For 2 and 3 we handle it like we handle array notations. The
7667 idx value we have above becomes the initial/start index.
7668 */
b72271b9 7669 if (flag_cilkplus
36536d79
BI
7670 && c_parser_peek_token (parser)->type == CPP_COLON)
7671 expr.value = c_parser_array_notation (expr_loc, parser, idx,
7672 expr.value);
7673 else
7674 {
7675 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
7676 "expected %<]%>");
7677 expr.value = build_array_ref (op_loc, expr.value, idx);
7678 }
7679 }
27bf414c 7680 expr.original_code = ERROR_MARK;
6866c6e8 7681 expr.original_type = NULL;
27bf414c
JM
7682 break;
7683 case CPP_OPEN_PAREN:
7684 /* Function call. */
7685 c_parser_consume_token (parser);
3a785c97
JJ
7686 for (i = 0; i < 3; i++)
7687 {
7688 sizeof_arg[i] = NULL_TREE;
7689 sizeof_arg_loc[i] = UNKNOWN_LOCATION;
7690 }
27bf414c 7691 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
bbbbb16a 7692 exprlist = NULL;
27bf414c 7693 else
1a4049e7 7694 exprlist = c_parser_expr_list (parser, true, false, &origtypes,
81e5eca8
MP
7695 sizeof_arg_loc, sizeof_arg,
7696 &arg_loc);
27bf414c
JM
7697 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7698 "expected %<)%>");
928c19bb 7699 orig_expr = expr;
ebfbbdc5 7700 mark_exp_read (expr.value);
3a785c97
JJ
7701 if (warn_sizeof_pointer_memaccess)
7702 sizeof_pointer_memaccess_warning (sizeof_arg_loc,
1a4049e7 7703 expr.value, exprlist,
3a785c97 7704 sizeof_arg,
1a4049e7 7705 sizeof_ptr_memacc_comptypes);
8edbfaa6
JJ
7706 expr.value
7707 = c_build_function_call_vec (expr_loc, arg_loc, expr.value,
7708 exprlist, origtypes);
27bf414c 7709 expr.original_code = ERROR_MARK;
928c19bb
JM
7710 if (TREE_CODE (expr.value) == INTEGER_CST
7711 && TREE_CODE (orig_expr.value) == FUNCTION_DECL
7712 && DECL_BUILT_IN_CLASS (orig_expr.value) == BUILT_IN_NORMAL
7713 && DECL_FUNCTION_CODE (orig_expr.value) == BUILT_IN_CONSTANT_P)
7714 expr.original_code = C_MAYBE_CONST_EXPR;
6866c6e8 7715 expr.original_type = NULL;
9771b263 7716 if (exprlist)
bbbbb16a 7717 {
c166b898
ILT
7718 release_tree_vector (exprlist);
7719 release_tree_vector (origtypes);
bbbbb16a 7720 }
81e5eca8 7721 arg_loc.release ();
27bf414c
JM
7722 break;
7723 case CPP_DOT:
7724 /* Structure element reference. */
7725 c_parser_consume_token (parser);
c2255bc4 7726 expr = default_function_array_conversion (expr_loc, expr);
27bf414c
JM
7727 if (c_parser_next_token_is (parser, CPP_NAME))
7728 ident = c_parser_peek_token (parser)->value;
7729 else
7730 {
7731 c_parser_error (parser, "expected identifier");
7732 expr.value = error_mark_node;
7733 expr.original_code = ERROR_MARK;
6866c6e8 7734 expr.original_type = NULL;
27bf414c
JM
7735 return expr;
7736 }
7737 c_parser_consume_token (parser);
c2255bc4 7738 expr.value = build_component_ref (op_loc, expr.value, ident);
27bf414c 7739 expr.original_code = ERROR_MARK;
6866c6e8
ILT
7740 if (TREE_CODE (expr.value) != COMPONENT_REF)
7741 expr.original_type = NULL;
7742 else
7743 {
7744 /* Remember the original type of a bitfield. */
7745 tree field = TREE_OPERAND (expr.value, 1);
7746 if (TREE_CODE (field) != FIELD_DECL)
7747 expr.original_type = NULL;
7748 else
7749 expr.original_type = DECL_BIT_FIELD_TYPE (field);
7750 }
27bf414c
JM
7751 break;
7752 case CPP_DEREF:
7753 /* Structure element reference. */
7754 c_parser_consume_token (parser);
267bac10 7755 expr = convert_lvalue_to_rvalue (expr_loc, expr, true, false);
27bf414c
JM
7756 if (c_parser_next_token_is (parser, CPP_NAME))
7757 ident = c_parser_peek_token (parser)->value;
7758 else
7759 {
7760 c_parser_error (parser, "expected identifier");
7761 expr.value = error_mark_node;
7762 expr.original_code = ERROR_MARK;
6866c6e8 7763 expr.original_type = NULL;
27bf414c
JM
7764 return expr;
7765 }
7766 c_parser_consume_token (parser);
c2255bc4
AH
7767 expr.value = build_component_ref (op_loc,
7768 build_indirect_ref (op_loc,
c9f9eb5d 7769 expr.value,
dd865ef6 7770 RO_ARROW),
6a3799eb 7771 ident);
27bf414c 7772 expr.original_code = ERROR_MARK;
6866c6e8
ILT
7773 if (TREE_CODE (expr.value) != COMPONENT_REF)
7774 expr.original_type = NULL;
7775 else
7776 {
7777 /* Remember the original type of a bitfield. */
7778 tree field = TREE_OPERAND (expr.value, 1);
7779 if (TREE_CODE (field) != FIELD_DECL)
7780 expr.original_type = NULL;
7781 else
7782 expr.original_type = DECL_BIT_FIELD_TYPE (field);
7783 }
27bf414c
JM
7784 break;
7785 case CPP_PLUS_PLUS:
7786 /* Postincrement. */
7787 c_parser_consume_token (parser);
36536d79 7788 /* If the expressions have array notations, we expand them. */
b72271b9 7789 if (flag_cilkplus
36536d79
BI
7790 && TREE_CODE (expr.value) == ARRAY_NOTATION_REF)
7791 expr = fix_array_notation_expr (expr_loc, POSTINCREMENT_EXPR, expr);
7792 else
7793 {
7794 expr = default_function_array_read_conversion (expr_loc, expr);
7795 expr.value = build_unary_op (op_loc,
7796 POSTINCREMENT_EXPR, expr.value, 0);
7797 }
27bf414c 7798 expr.original_code = ERROR_MARK;
6866c6e8 7799 expr.original_type = NULL;
27bf414c
JM
7800 break;
7801 case CPP_MINUS_MINUS:
7802 /* Postdecrement. */
7803 c_parser_consume_token (parser);
36536d79 7804 /* If the expressions have array notations, we expand them. */
b72271b9 7805 if (flag_cilkplus
36536d79
BI
7806 && TREE_CODE (expr.value) == ARRAY_NOTATION_REF)
7807 expr = fix_array_notation_expr (expr_loc, POSTDECREMENT_EXPR, expr);
7808 else
7809 {
7810 expr = default_function_array_read_conversion (expr_loc, expr);
7811 expr.value = build_unary_op (op_loc,
7812 POSTDECREMENT_EXPR, expr.value, 0);
7813 }
27bf414c 7814 expr.original_code = ERROR_MARK;
6866c6e8 7815 expr.original_type = NULL;
27bf414c
JM
7816 break;
7817 default:
7818 return expr;
7819 }
7820 }
7821}
7822
7823/* Parse an expression (C90 6.3.17, C99 6.5.17).
7824
7825 expression:
7826 assignment-expression
7827 expression , assignment-expression
7828*/
7829
7830static struct c_expr
7831c_parser_expression (c_parser *parser)
7832{
267bac10 7833 location_t tloc = c_parser_peek_token (parser)->location;
27bf414c
JM
7834 struct c_expr expr;
7835 expr = c_parser_expr_no_commas (parser, NULL);
267bac10
JM
7836 if (c_parser_next_token_is (parser, CPP_COMMA))
7837 expr = convert_lvalue_to_rvalue (tloc, expr, true, false);
27bf414c
JM
7838 while (c_parser_next_token_is (parser, CPP_COMMA))
7839 {
7840 struct c_expr next;
056928b2 7841 tree lhsval;
c2255bc4
AH
7842 location_t loc = c_parser_peek_token (parser)->location;
7843 location_t expr_loc;
27bf414c 7844 c_parser_consume_token (parser);
c2255bc4 7845 expr_loc = c_parser_peek_token (parser)->location;
056928b2
JJ
7846 lhsval = expr.value;
7847 while (TREE_CODE (lhsval) == COMPOUND_EXPR)
7848 lhsval = TREE_OPERAND (lhsval, 1);
7849 if (DECL_P (lhsval) || handled_component_p (lhsval))
7850 mark_exp_read (lhsval);
27bf414c 7851 next = c_parser_expr_no_commas (parser, NULL);
267bac10 7852 next = convert_lvalue_to_rvalue (expr_loc, next, true, false);
c2255bc4 7853 expr.value = build_compound_expr (loc, expr.value, next.value);
27bf414c 7854 expr.original_code = COMPOUND_EXPR;
81f40b79 7855 expr.original_type = next.original_type;
27bf414c
JM
7856 }
7857 return expr;
7858}
7859
267bac10
JM
7860/* Parse an expression and convert functions or arrays to pointers and
7861 lvalues to rvalues. */
46bdb9cf
JM
7862
7863static struct c_expr
7864c_parser_expression_conv (c_parser *parser)
7865{
7866 struct c_expr expr;
c2255bc4 7867 location_t loc = c_parser_peek_token (parser)->location;
46bdb9cf 7868 expr = c_parser_expression (parser);
267bac10 7869 expr = convert_lvalue_to_rvalue (loc, expr, true, false);
46bdb9cf
JM
7870 return expr;
7871}
7872
7873/* Parse a non-empty list of expressions. If CONVERT_P, convert
267bac10 7874 functions and arrays to pointers and lvalues to rvalues. If
81e5eca8
MP
7875 FOLD_P, fold the expressions. If LOCATIONS is non-NULL, save the
7876 locations of function arguments into this vector.
27bf414c
JM
7877
7878 nonempty-expr-list:
7879 assignment-expression
7880 nonempty-expr-list , assignment-expression
7881*/
7882
9771b263 7883static vec<tree, va_gc> *
bbbbb16a 7884c_parser_expr_list (c_parser *parser, bool convert_p, bool fold_p,
9771b263 7885 vec<tree, va_gc> **p_orig_types,
81e5eca8
MP
7886 location_t *sizeof_arg_loc, tree *sizeof_arg,
7887 vec<location_t> *locations)
27bf414c 7888{
9771b263
DN
7889 vec<tree, va_gc> *ret;
7890 vec<tree, va_gc> *orig_types;
27bf414c 7891 struct c_expr expr;
c2255bc4 7892 location_t loc = c_parser_peek_token (parser)->location;
3a785c97
JJ
7893 location_t cur_sizeof_arg_loc = UNKNOWN_LOCATION;
7894 unsigned int idx = 0;
bbbbb16a 7895
c166b898 7896 ret = make_tree_vector ();
bbbbb16a
ILT
7897 if (p_orig_types == NULL)
7898 orig_types = NULL;
7899 else
c166b898 7900 orig_types = make_tree_vector ();
bbbbb16a 7901
1a4049e7
JJ
7902 if (sizeof_arg != NULL
7903 && c_parser_next_token_is_keyword (parser, RID_SIZEOF))
3a785c97 7904 cur_sizeof_arg_loc = c_parser_peek_2nd_token (parser)->location;
27bf414c 7905 expr = c_parser_expr_no_commas (parser, NULL);
46bdb9cf 7906 if (convert_p)
267bac10 7907 expr = convert_lvalue_to_rvalue (loc, expr, true, true);
928c19bb
JM
7908 if (fold_p)
7909 expr.value = c_fully_fold (expr.value, false, NULL);
9771b263
DN
7910 ret->quick_push (expr.value);
7911 if (orig_types)
7912 orig_types->quick_push (expr.original_type);
81e5eca8
MP
7913 if (locations)
7914 locations->safe_push (loc);
3a785c97
JJ
7915 if (sizeof_arg != NULL
7916 && cur_sizeof_arg_loc != UNKNOWN_LOCATION
7917 && expr.original_code == SIZEOF_EXPR)
7918 {
7919 sizeof_arg[0] = c_last_sizeof_arg;
7920 sizeof_arg_loc[0] = cur_sizeof_arg_loc;
7921 }
27bf414c
JM
7922 while (c_parser_next_token_is (parser, CPP_COMMA))
7923 {
7924 c_parser_consume_token (parser);
c2255bc4 7925 loc = c_parser_peek_token (parser)->location;
1a4049e7
JJ
7926 if (sizeof_arg != NULL
7927 && c_parser_next_token_is_keyword (parser, RID_SIZEOF))
3a785c97 7928 cur_sizeof_arg_loc = c_parser_peek_2nd_token (parser)->location;
1a4049e7 7929 else
3a785c97 7930 cur_sizeof_arg_loc = UNKNOWN_LOCATION;
27bf414c 7931 expr = c_parser_expr_no_commas (parser, NULL);
46bdb9cf 7932 if (convert_p)
267bac10 7933 expr = convert_lvalue_to_rvalue (loc, expr, true, true);
928c19bb
JM
7934 if (fold_p)
7935 expr.value = c_fully_fold (expr.value, false, NULL);
9771b263
DN
7936 vec_safe_push (ret, expr.value);
7937 if (orig_types)
7938 vec_safe_push (orig_types, expr.original_type);
81e5eca8
MP
7939 if (locations)
7940 locations->safe_push (loc);
3a785c97
JJ
7941 if (++idx < 3
7942 && sizeof_arg != NULL
7943 && cur_sizeof_arg_loc != UNKNOWN_LOCATION
1a4049e7
JJ
7944 && expr.original_code == SIZEOF_EXPR)
7945 {
3a785c97
JJ
7946 sizeof_arg[idx] = c_last_sizeof_arg;
7947 sizeof_arg_loc[idx] = cur_sizeof_arg_loc;
1a4049e7
JJ
7948 }
7949 }
9771b263 7950 if (orig_types)
bbbbb16a 7951 *p_orig_types = orig_types;
27bf414c
JM
7952 return ret;
7953}
27bf414c
JM
7954\f
7955/* Parse Objective-C-specific constructs. */
7956
7957/* Parse an objc-class-definition.
7958
7959 objc-class-definition:
7960 @interface identifier objc-superclass[opt] objc-protocol-refs[opt]
7961 objc-class-instance-variables[opt] objc-methodprotolist @end
7962 @implementation identifier objc-superclass[opt]
7963 objc-class-instance-variables[opt]
7964 @interface identifier ( identifier ) objc-protocol-refs[opt]
7965 objc-methodprotolist @end
ec3e9f82
NP
7966 @interface identifier ( ) objc-protocol-refs[opt]
7967 objc-methodprotolist @end
27bf414c
JM
7968 @implementation identifier ( identifier )
7969
7970 objc-superclass:
7971 : identifier
7972
7973 "@interface identifier (" must start "@interface identifier (
7974 identifier ) ...": objc-methodprotolist in the first production may
0fa2e4df 7975 not start with a parenthesized identifier as a declarator of a data
27bf414c
JM
7976 definition with no declaration specifiers if the objc-superclass,
7977 objc-protocol-refs and objc-class-instance-variables are omitted. */
7978
7979static void
c165dca7 7980c_parser_objc_class_definition (c_parser *parser, tree attributes)
27bf414c
JM
7981{
7982 bool iface_p;
7983 tree id1;
7984 tree superclass;
7985 if (c_parser_next_token_is_keyword (parser, RID_AT_INTERFACE))
7986 iface_p = true;
7987 else if (c_parser_next_token_is_keyword (parser, RID_AT_IMPLEMENTATION))
7988 iface_p = false;
7989 else
7990 gcc_unreachable ();
c165dca7 7991
27bf414c
JM
7992 c_parser_consume_token (parser);
7993 if (c_parser_next_token_is_not (parser, CPP_NAME))
7994 {
7995 c_parser_error (parser, "expected identifier");
7996 return;
7997 }
7998 id1 = c_parser_peek_token (parser)->value;
7999 c_parser_consume_token (parser);
8000 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
8001 {
ec3e9f82 8002 /* We have a category or class extension. */
27bf414c
JM
8003 tree id2;
8004 tree proto = NULL_TREE;
8005 c_parser_consume_token (parser);
8006 if (c_parser_next_token_is_not (parser, CPP_NAME))
8007 {
ec3e9f82
NP
8008 if (iface_p && c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
8009 {
8010 /* We have a class extension. */
8011 id2 = NULL_TREE;
8012 }
8013 else
8014 {
8015 c_parser_error (parser, "expected identifier or %<)%>");
8016 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
8017 return;
8018 }
8019 }
8020 else
8021 {
8022 id2 = c_parser_peek_token (parser)->value;
8023 c_parser_consume_token (parser);
27bf414c 8024 }
27bf414c
JM
8025 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
8026 if (!iface_p)
8027 {
8028 objc_start_category_implementation (id1, id2);
8029 return;
8030 }
8031 if (c_parser_next_token_is (parser, CPP_LESS))
8032 proto = c_parser_objc_protocol_refs (parser);
c165dca7 8033 objc_start_category_interface (id1, id2, proto, attributes);
27bf414c
JM
8034 c_parser_objc_methodprotolist (parser);
8035 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
8036 objc_finish_interface ();
8037 return;
8038 }
8039 if (c_parser_next_token_is (parser, CPP_COLON))
8040 {
8041 c_parser_consume_token (parser);
8042 if (c_parser_next_token_is_not (parser, CPP_NAME))
8043 {
8044 c_parser_error (parser, "expected identifier");
8045 return;
8046 }
8047 superclass = c_parser_peek_token (parser)->value;
8048 c_parser_consume_token (parser);
8049 }
8050 else
8051 superclass = NULL_TREE;
8052 if (iface_p)
8053 {
8054 tree proto = NULL_TREE;
8055 if (c_parser_next_token_is (parser, CPP_LESS))
8056 proto = c_parser_objc_protocol_refs (parser);
c165dca7 8057 objc_start_class_interface (id1, superclass, proto, attributes);
27bf414c
JM
8058 }
8059 else
8060 objc_start_class_implementation (id1, superclass);
8061 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
8062 c_parser_objc_class_instance_variables (parser);
8063 if (iface_p)
8064 {
8065 objc_continue_interface ();
8066 c_parser_objc_methodprotolist (parser);
8067 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
8068 objc_finish_interface ();
8069 }
8070 else
8071 {
8072 objc_continue_implementation ();
8073 return;
8074 }
8075}
8076
8077/* Parse objc-class-instance-variables.
8078
8079 objc-class-instance-variables:
8080 { objc-instance-variable-decl-list[opt] }
8081
8082 objc-instance-variable-decl-list:
8083 objc-visibility-spec
8084 objc-instance-variable-decl ;
8085 ;
8086 objc-instance-variable-decl-list objc-visibility-spec
8087 objc-instance-variable-decl-list objc-instance-variable-decl ;
8088 objc-instance-variable-decl-list ;
8089
8090 objc-visibility-spec:
8091 @private
8092 @protected
8093 @public
8094
8095 objc-instance-variable-decl:
8096 struct-declaration
8097*/
8098
8099static void
8100c_parser_objc_class_instance_variables (c_parser *parser)
8101{
8102 gcc_assert (c_parser_next_token_is (parser, CPP_OPEN_BRACE));
8103 c_parser_consume_token (parser);
8104 while (c_parser_next_token_is_not (parser, CPP_EOF))
8105 {
8106 tree decls;
8107 /* Parse any stray semicolon. */
8108 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
8109 {
c1771a20 8110 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
4e26ba90 8111 "extra semicolon");
27bf414c
JM
8112 c_parser_consume_token (parser);
8113 continue;
8114 }
8115 /* Stop if at the end of the instance variables. */
8116 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
8117 {
8118 c_parser_consume_token (parser);
8119 break;
8120 }
8121 /* Parse any objc-visibility-spec. */
49b91f05 8122 if (c_parser_next_token_is_keyword (parser, RID_AT_PRIVATE))
27bf414c
JM
8123 {
8124 c_parser_consume_token (parser);
c37d8c30 8125 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
27bf414c
JM
8126 continue;
8127 }
49b91f05 8128 else if (c_parser_next_token_is_keyword (parser, RID_AT_PROTECTED))
27bf414c
JM
8129 {
8130 c_parser_consume_token (parser);
c37d8c30 8131 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
27bf414c
JM
8132 continue;
8133 }
49b91f05 8134 else if (c_parser_next_token_is_keyword (parser, RID_AT_PUBLIC))
27bf414c
JM
8135 {
8136 c_parser_consume_token (parser);
c37d8c30
IS
8137 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
8138 continue;
8139 }
8140 else if (c_parser_next_token_is_keyword (parser, RID_AT_PACKAGE))
8141 {
8142 c_parser_consume_token (parser);
8143 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
27bf414c
JM
8144 continue;
8145 }
bc4071dd
RH
8146 else if (c_parser_next_token_is (parser, CPP_PRAGMA))
8147 {
8148 c_parser_pragma (parser, pragma_external);
8149 continue;
8150 }
8151
27bf414c
JM
8152 /* Parse some comma-separated declarations. */
8153 decls = c_parser_struct_declaration (parser);
4e26ba90
NP
8154 if (decls == NULL)
8155 {
8156 /* There is a syntax error. We want to skip the offending
8157 tokens up to the next ';' (included) or '}'
8158 (excluded). */
8159
8160 /* First, skip manually a ')' or ']'. This is because they
8161 reduce the nesting level, so c_parser_skip_until_found()
8162 wouldn't be able to skip past them. */
8163 c_token *token = c_parser_peek_token (parser);
8164 if (token->type == CPP_CLOSE_PAREN || token->type == CPP_CLOSE_SQUARE)
8165 c_parser_consume_token (parser);
8166
8167 /* Then, do the standard skipping. */
8168 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
8169
8170 /* We hopefully recovered. Start normal parsing again. */
8171 parser->error = false;
8172 continue;
8173 }
8174 else
8175 {
8176 /* Comma-separated instance variables are chained together
8177 in reverse order; add them one by one. */
8178 tree ivar = nreverse (decls);
8179 for (; ivar; ivar = DECL_CHAIN (ivar))
8180 objc_add_instance_variable (copy_node (ivar));
8181 }
27bf414c
JM
8182 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
8183 }
8184}
8185
8186/* Parse an objc-class-declaration.
8187
8188 objc-class-declaration:
8189 @class identifier-list ;
8190*/
8191
8192static void
8193c_parser_objc_class_declaration (c_parser *parser)
8194{
49b91f05 8195 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_CLASS));
27bf414c
JM
8196 c_parser_consume_token (parser);
8197 /* Any identifiers, including those declared as type names, are OK
8198 here. */
8199 while (true)
8200 {
8201 tree id;
8202 if (c_parser_next_token_is_not (parser, CPP_NAME))
8203 {
8204 c_parser_error (parser, "expected identifier");
da57d1b9
NP
8205 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
8206 parser->error = false;
8207 return;
27bf414c
JM
8208 }
8209 id = c_parser_peek_token (parser)->value;
32dabdaf 8210 objc_declare_class (id);
27bf414c
JM
8211 c_parser_consume_token (parser);
8212 if (c_parser_next_token_is (parser, CPP_COMMA))
8213 c_parser_consume_token (parser);
8214 else
8215 break;
8216 }
8217 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
27bf414c
JM
8218}
8219
8220/* Parse an objc-alias-declaration.
8221
8222 objc-alias-declaration:
8223 @compatibility_alias identifier identifier ;
8224*/
8225
8226static void
8227c_parser_objc_alias_declaration (c_parser *parser)
8228{
8229 tree id1, id2;
8230 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_ALIAS));
8231 c_parser_consume_token (parser);
8232 if (c_parser_next_token_is_not (parser, CPP_NAME))
8233 {
8234 c_parser_error (parser, "expected identifier");
8235 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
8236 return;
8237 }
8238 id1 = c_parser_peek_token (parser)->value;
8239 c_parser_consume_token (parser);
8240 if (c_parser_next_token_is_not (parser, CPP_NAME))
8241 {
8242 c_parser_error (parser, "expected identifier");
8243 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
8244 return;
8245 }
8246 id2 = c_parser_peek_token (parser)->value;
8247 c_parser_consume_token (parser);
8248 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
8249 objc_declare_alias (id1, id2);
8250}
8251
8252/* Parse an objc-protocol-definition.
8253
8254 objc-protocol-definition:
8255 @protocol identifier objc-protocol-refs[opt] objc-methodprotolist @end
8256 @protocol identifier-list ;
8257
8258 "@protocol identifier ;" should be resolved as "@protocol
8259 identifier-list ;": objc-methodprotolist may not start with a
8260 semicolon in the first alternative if objc-protocol-refs are
8261 omitted. */
8262
8263static void
c165dca7 8264c_parser_objc_protocol_definition (c_parser *parser, tree attributes)
27bf414c
JM
8265{
8266 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_PROTOCOL));
c165dca7 8267
27bf414c
JM
8268 c_parser_consume_token (parser);
8269 if (c_parser_next_token_is_not (parser, CPP_NAME))
8270 {
8271 c_parser_error (parser, "expected identifier");
8272 return;
8273 }
8274 if (c_parser_peek_2nd_token (parser)->type == CPP_COMMA
8275 || c_parser_peek_2nd_token (parser)->type == CPP_SEMICOLON)
8276 {
27bf414c
JM
8277 /* Any identifiers, including those declared as type names, are
8278 OK here. */
8279 while (true)
8280 {
8281 tree id;
8282 if (c_parser_next_token_is_not (parser, CPP_NAME))
8283 {
8284 c_parser_error (parser, "expected identifier");
8285 break;
8286 }
8287 id = c_parser_peek_token (parser)->value;
c59633d9 8288 objc_declare_protocol (id, attributes);
27bf414c
JM
8289 c_parser_consume_token (parser);
8290 if (c_parser_next_token_is (parser, CPP_COMMA))
8291 c_parser_consume_token (parser);
8292 else
8293 break;
8294 }
8295 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
27bf414c
JM
8296 }
8297 else
8298 {
8299 tree id = c_parser_peek_token (parser)->value;
8300 tree proto = NULL_TREE;
8301 c_parser_consume_token (parser);
8302 if (c_parser_next_token_is (parser, CPP_LESS))
8303 proto = c_parser_objc_protocol_refs (parser);
0bacb8c7 8304 parser->objc_pq_context = true;
c165dca7 8305 objc_start_protocol (id, proto, attributes);
27bf414c
JM
8306 c_parser_objc_methodprotolist (parser);
8307 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
0bacb8c7 8308 parser->objc_pq_context = false;
27bf414c
JM
8309 objc_finish_interface ();
8310 }
8311}
8312
8313/* Parse an objc-method-type.
8314
8315 objc-method-type:
8316 +
8317 -
27bf414c 8318
249a82c4
NP
8319 Return true if it is a class method (+) and false if it is
8320 an instance method (-).
8321*/
8322static inline bool
27bf414c
JM
8323c_parser_objc_method_type (c_parser *parser)
8324{
8325 switch (c_parser_peek_token (parser)->type)
8326 {
8327 case CPP_PLUS:
8328 c_parser_consume_token (parser);
249a82c4 8329 return true;
27bf414c
JM
8330 case CPP_MINUS:
8331 c_parser_consume_token (parser);
249a82c4 8332 return false;
27bf414c
JM
8333 default:
8334 gcc_unreachable ();
8335 }
8336}
8337
8338/* Parse an objc-method-definition.
8339
8340 objc-method-definition:
8341 objc-method-type objc-method-decl ;[opt] compound-statement
8342*/
8343
8344static void
8345c_parser_objc_method_definition (c_parser *parser)
8346{
249a82c4 8347 bool is_class_method = c_parser_objc_method_type (parser);
a04a722b 8348 tree decl, attributes = NULL_TREE, expr = NULL_TREE;
0bacb8c7 8349 parser->objc_pq_context = true;
a04a722b
JM
8350 decl = c_parser_objc_method_decl (parser, is_class_method, &attributes,
8351 &expr);
f7e71da5
IS
8352 if (decl == error_mark_node)
8353 return; /* Bail here. */
8354
27bf414c
JM
8355 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
8356 {
8357 c_parser_consume_token (parser);
c1771a20 8358 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
509c9d60 8359 "extra semicolon in method definition specified");
27bf414c 8360 }
f7e71da5 8361
8f078c08
AP
8362 if (!c_parser_next_token_is (parser, CPP_OPEN_BRACE))
8363 {
8364 c_parser_error (parser, "expected %<{%>");
8365 return;
8366 }
f7e71da5 8367
0bacb8c7 8368 parser->objc_pq_context = false;
a04a722b 8369 if (objc_start_method_definition (is_class_method, decl, attributes, expr))
45547c7f
NP
8370 {
8371 add_stmt (c_parser_compound_statement (parser));
8372 objc_finish_method_definition (current_function_decl);
8373 }
8374 else
8375 {
8376 /* This code is executed when we find a method definition
a26d8862
NP
8377 outside of an @implementation context (or invalid for other
8378 reasons). Parse the method (to keep going) but do not emit
8379 any code.
45547c7f
NP
8380 */
8381 c_parser_compound_statement (parser);
8382 }
27bf414c
JM
8383}
8384
8385/* Parse an objc-methodprotolist.
8386
8387 objc-methodprotolist:
8388 empty
8389 objc-methodprotolist objc-methodproto
8390 objc-methodprotolist declaration
8391 objc-methodprotolist ;
92902b1b
IS
8392 @optional
8393 @required
27bf414c
JM
8394
8395 The declaration is a data definition, which may be missing
8396 declaration specifiers under the same rules and diagnostics as
8397 other data definitions outside functions, and the stray semicolon
8398 is diagnosed the same way as a stray semicolon outside a
8399 function. */
8400
8401static void
8402c_parser_objc_methodprotolist (c_parser *parser)
8403{
8404 while (true)
8405 {
8406 /* The list is terminated by @end. */
8407 switch (c_parser_peek_token (parser)->type)
8408 {
8409 case CPP_SEMICOLON:
c1771a20 8410 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
509c9d60 8411 "ISO C does not allow extra %<;%> outside of a function");
27bf414c
JM
8412 c_parser_consume_token (parser);
8413 break;
8414 case CPP_PLUS:
8415 case CPP_MINUS:
8416 c_parser_objc_methodproto (parser);
8417 break;
b9b58168
RH
8418 case CPP_PRAGMA:
8419 c_parser_pragma (parser, pragma_external);
8420 break;
27bf414c
JM
8421 case CPP_EOF:
8422 return;
8423 default:
8424 if (c_parser_next_token_is_keyword (parser, RID_AT_END))
8425 return;
668ea4b1 8426 else if (c_parser_next_token_is_keyword (parser, RID_AT_PROPERTY))
f614132b 8427 c_parser_objc_at_property_declaration (parser);
92902b1b
IS
8428 else if (c_parser_next_token_is_keyword (parser, RID_AT_OPTIONAL))
8429 {
8430 objc_set_method_opt (true);
8431 c_parser_consume_token (parser);
8432 }
8433 else if (c_parser_next_token_is_keyword (parser, RID_AT_REQUIRED))
8434 {
8435 objc_set_method_opt (false);
8436 c_parser_consume_token (parser);
8437 }
8438 else
8439 c_parser_declaration_or_fndef (parser, false, false, true,
acf0174b 8440 false, true, NULL, vNULL);
27bf414c
JM
8441 break;
8442 }
8443 }
8444}
8445
8446/* Parse an objc-methodproto.
8447
8448 objc-methodproto:
8449 objc-method-type objc-method-decl ;
8450*/
8451
8452static void
8453c_parser_objc_methodproto (c_parser *parser)
8454{
249a82c4 8455 bool is_class_method = c_parser_objc_method_type (parser);
f7e71da5 8456 tree decl, attributes = NULL_TREE;
249a82c4 8457
27bf414c 8458 /* Remember protocol qualifiers in prototypes. */
0bacb8c7 8459 parser->objc_pq_context = true;
a04a722b
JM
8460 decl = c_parser_objc_method_decl (parser, is_class_method, &attributes,
8461 NULL);
f7e71da5 8462 /* Forget protocol qualifiers now. */
0bacb8c7 8463 parser->objc_pq_context = false;
f7e71da5
IS
8464
8465 /* Do not allow the presence of attributes to hide an erroneous
8466 method implementation in the interface section. */
8467 if (!c_parser_next_token_is (parser, CPP_SEMICOLON))
8468 {
8469 c_parser_error (parser, "expected %<;%>");
8470 return;
8471 }
8472
8473 if (decl != error_mark_node)
249a82c4 8474 objc_add_method_declaration (is_class_method, decl, attributes);
f7e71da5 8475
27bf414c
JM
8476 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
8477}
8478
f7e71da5
IS
8479/* If we are at a position that method attributes may be present, check that
8480 there are not any parsed already (a syntax error) and then collect any
8481 specified at the current location. Finally, if new attributes were present,
8482 check that the next token is legal ( ';' for decls and '{' for defs). */
8483
8484static bool
8485c_parser_objc_maybe_method_attributes (c_parser* parser, tree* attributes)
8486{
8487 bool bad = false;
8488 if (*attributes)
8489 {
8490 c_parser_error (parser,
8491 "method attributes must be specified at the end only");
8492 *attributes = NULL_TREE;
8493 bad = true;
8494 }
8495
8496 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
8497 *attributes = c_parser_attributes (parser);
8498
8499 /* If there were no attributes here, just report any earlier error. */
8500 if (*attributes == NULL_TREE || bad)
8501 return bad;
8502
8503 /* If the attributes are followed by a ; or {, then just report any earlier
8504 error. */
8505 if (c_parser_next_token_is (parser, CPP_SEMICOLON)
8506 || c_parser_next_token_is (parser, CPP_OPEN_BRACE))
8507 return bad;
8508
8509 /* We've got attributes, but not at the end. */
8510 c_parser_error (parser,
8511 "expected %<;%> or %<{%> after method attribute definition");
8512 return true;
8513}
8514
27bf414c
JM
8515/* Parse an objc-method-decl.
8516
8517 objc-method-decl:
8518 ( objc-type-name ) objc-selector
8519 objc-selector
8520 ( objc-type-name ) objc-keyword-selector objc-optparmlist
8521 objc-keyword-selector objc-optparmlist
f7e71da5 8522 attributes
27bf414c
JM
8523
8524 objc-keyword-selector:
8525 objc-keyword-decl
8526 objc-keyword-selector objc-keyword-decl
8527
8528 objc-keyword-decl:
8529 objc-selector : ( objc-type-name ) identifier
8530 objc-selector : identifier
8531 : ( objc-type-name ) identifier
8532 : identifier
8533
8534 objc-optparmlist:
8535 objc-optparms objc-optellipsis
8536
8537 objc-optparms:
8538 empty
8539 objc-opt-parms , parameter-declaration
8540
8541 objc-optellipsis:
8542 empty
8543 , ...
8544*/
8545
8546static tree
a04a722b
JM
8547c_parser_objc_method_decl (c_parser *parser, bool is_class_method,
8548 tree *attributes, tree *expr)
27bf414c
JM
8549{
8550 tree type = NULL_TREE;
8551 tree sel;
8552 tree parms = NULL_TREE;
dbb74365 8553 bool ellipsis = false;
f7e71da5 8554 bool attr_err = false;
dbb74365 8555
f7e71da5 8556 *attributes = NULL_TREE;
27bf414c
JM
8557 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
8558 {
8559 c_parser_consume_token (parser);
8560 type = c_parser_objc_type_name (parser);
8561 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
8562 }
8563 sel = c_parser_objc_selector (parser);
8564 /* If there is no selector, or a colon follows, we have an
8565 objc-keyword-selector. If there is a selector, and a colon does
8566 not follow, that selector ends the objc-method-decl. */
8567 if (!sel || c_parser_next_token_is (parser, CPP_COLON))
8568 {
8569 tree tsel = sel;
8570 tree list = NULL_TREE;
27bf414c
JM
8571 while (true)
8572 {
8573 tree atype = NULL_TREE, id, keyworddecl;
f7e71da5 8574 tree param_attr = NULL_TREE;
27bf414c
JM
8575 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
8576 break;
8577 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
8578 {
8579 c_parser_consume_token (parser);
8580 atype = c_parser_objc_type_name (parser);
8581 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
8582 "expected %<)%>");
8583 }
f7e71da5
IS
8584 /* New ObjC allows attributes on method parameters. */
8585 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
8586 param_attr = c_parser_attributes (parser);
27bf414c
JM
8587 if (c_parser_next_token_is_not (parser, CPP_NAME))
8588 {
8589 c_parser_error (parser, "expected identifier");
8590 return error_mark_node;
8591 }
8592 id = c_parser_peek_token (parser)->value;
8593 c_parser_consume_token (parser);
f7e71da5 8594 keyworddecl = objc_build_keyword_decl (tsel, atype, id, param_attr);
27bf414c
JM
8595 list = chainon (list, keyworddecl);
8596 tsel = c_parser_objc_selector (parser);
8597 if (!tsel && c_parser_next_token_is_not (parser, CPP_COLON))
8598 break;
8599 }
f7e71da5
IS
8600
8601 attr_err |= c_parser_objc_maybe_method_attributes (parser, attributes) ;
8602
27bf414c
JM
8603 /* Parse the optional parameter list. Optional Objective-C
8604 method parameters follow the C syntax, and may include '...'
8605 to denote a variable number of arguments. */
8606 parms = make_node (TREE_LIST);
27bf414c
JM
8607 while (c_parser_next_token_is (parser, CPP_COMMA))
8608 {
8609 struct c_parm *parm;
8610 c_parser_consume_token (parser);
8611 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
8612 {
8613 ellipsis = true;
8614 c_parser_consume_token (parser);
f7e71da5
IS
8615 attr_err |= c_parser_objc_maybe_method_attributes
8616 (parser, attributes) ;
27bf414c
JM
8617 break;
8618 }
8619 parm = c_parser_parameter_declaration (parser, NULL_TREE);
8620 if (parm == NULL)
8621 break;
8622 parms = chainon (parms,
a04a722b 8623 build_tree_list (NULL_TREE, grokparm (parm, expr)));
27bf414c 8624 }
27bf414c
JM
8625 sel = list;
8626 }
f7e71da5
IS
8627 else
8628 attr_err |= c_parser_objc_maybe_method_attributes (parser, attributes) ;
8629
8630 if (sel == NULL)
8631 {
8632 c_parser_error (parser, "objective-c method declaration is expected");
8633 return error_mark_node;
8634 }
8635
8636 if (attr_err)
8637 return error_mark_node;
8638
249a82c4 8639 return objc_build_method_signature (is_class_method, type, sel, parms, ellipsis);
27bf414c
JM
8640}
8641
8642/* Parse an objc-type-name.
8643
8644 objc-type-name:
8645 objc-type-qualifiers[opt] type-name
8646 objc-type-qualifiers[opt]
8647
8648 objc-type-qualifiers:
8649 objc-type-qualifier
8650 objc-type-qualifiers objc-type-qualifier
8651
8652 objc-type-qualifier: one of
8653 in out inout bycopy byref oneway
8654*/
8655
8656static tree
8657c_parser_objc_type_name (c_parser *parser)
8658{
8659 tree quals = NULL_TREE;
d75d71e0 8660 struct c_type_name *type_name = NULL;
27bf414c
JM
8661 tree type = NULL_TREE;
8662 while (true)
8663 {
8664 c_token *token = c_parser_peek_token (parser);
8665 if (token->type == CPP_KEYWORD
8666 && (token->keyword == RID_IN
8667 || token->keyword == RID_OUT
8668 || token->keyword == RID_INOUT
8669 || token->keyword == RID_BYCOPY
8670 || token->keyword == RID_BYREF
8671 || token->keyword == RID_ONEWAY))
8672 {
71fc71d8 8673 quals = chainon (build_tree_list (NULL_TREE, token->value), quals);
27bf414c
JM
8674 c_parser_consume_token (parser);
8675 }
8676 else
8677 break;
8678 }
29ce73cb 8679 if (c_parser_next_tokens_start_typename (parser, cla_prefer_type))
d75d71e0
ILT
8680 type_name = c_parser_type_name (parser);
8681 if (type_name)
928c19bb 8682 type = groktypename (type_name, NULL, NULL);
046608a3
NP
8683
8684 /* If the type is unknown, and error has already been produced and
8685 we need to recover from the error. In that case, use NULL_TREE
8686 for the type, as if no type had been specified; this will use the
8687 default type ('id') which is good for error recovery. */
8688 if (type == error_mark_node)
8689 type = NULL_TREE;
8690
27bf414c
JM
8691 return build_tree_list (quals, type);
8692}
8693
8694/* Parse objc-protocol-refs.
8695
8696 objc-protocol-refs:
8697 < identifier-list >
8698*/
8699
8700static tree
8701c_parser_objc_protocol_refs (c_parser *parser)
8702{
8703 tree list = NULL_TREE;
8704 gcc_assert (c_parser_next_token_is (parser, CPP_LESS));
8705 c_parser_consume_token (parser);
8706 /* Any identifiers, including those declared as type names, are OK
8707 here. */
8708 while (true)
8709 {
8710 tree id;
8711 if (c_parser_next_token_is_not (parser, CPP_NAME))
8712 {
8713 c_parser_error (parser, "expected identifier");
8714 break;
8715 }
8716 id = c_parser_peek_token (parser)->value;
8717 list = chainon (list, build_tree_list (NULL_TREE, id));
8718 c_parser_consume_token (parser);
8719 if (c_parser_next_token_is (parser, CPP_COMMA))
8720 c_parser_consume_token (parser);
8721 else
8722 break;
8723 }
8724 c_parser_require (parser, CPP_GREATER, "expected %<>%>");
8725 return list;
8726}
8727
437c2322 8728/* Parse an objc-try-catch-finally-statement.
27bf414c 8729
437c2322 8730 objc-try-catch-finally-statement:
27bf414c
JM
8731 @try compound-statement objc-catch-list[opt]
8732 @try compound-statement objc-catch-list[opt] @finally compound-statement
8733
8734 objc-catch-list:
437c2322
NP
8735 @catch ( objc-catch-parameter-declaration ) compound-statement
8736 objc-catch-list @catch ( objc-catch-parameter-declaration ) compound-statement
8737
8738 objc-catch-parameter-declaration:
8739 parameter-declaration
8740 '...'
8741
8742 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
8743
8744 PS: This function is identical to cp_parser_objc_try_catch_finally_statement
8745 for C++. Keep them in sync. */
27bf414c
JM
8746
8747static void
437c2322 8748c_parser_objc_try_catch_finally_statement (c_parser *parser)
27bf414c 8749{
437c2322 8750 location_t location;
27bf414c 8751 tree stmt;
437c2322 8752
49b91f05 8753 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_TRY));
27bf414c 8754 c_parser_consume_token (parser);
437c2322 8755 location = c_parser_peek_token (parser)->location;
46270f14 8756 objc_maybe_warn_exceptions (location);
27bf414c 8757 stmt = c_parser_compound_statement (parser);
437c2322
NP
8758 objc_begin_try_stmt (location, stmt);
8759
49b91f05 8760 while (c_parser_next_token_is_keyword (parser, RID_AT_CATCH))
27bf414c
JM
8761 {
8762 struct c_parm *parm;
437c2322
NP
8763 tree parameter_declaration = error_mark_node;
8764 bool seen_open_paren = false;
8765
27bf414c
JM
8766 c_parser_consume_token (parser);
8767 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
437c2322
NP
8768 seen_open_paren = true;
8769 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
27bf414c 8770 {
437c2322
NP
8771 /* We have "@catch (...)" (where the '...' are literally
8772 what is in the code). Skip the '...'.
8773 parameter_declaration is set to NULL_TREE, and
8774 objc_being_catch_clauses() knows that that means
8775 '...'. */
8776 c_parser_consume_token (parser);
8777 parameter_declaration = NULL_TREE;
27bf414c 8778 }
437c2322
NP
8779 else
8780 {
8781 /* We have "@catch (NSException *exception)" or something
8782 like that. Parse the parameter declaration. */
8783 parm = c_parser_parameter_declaration (parser, NULL_TREE);
8784 if (parm == NULL)
8785 parameter_declaration = error_mark_node;
8786 else
a04a722b 8787 parameter_declaration = grokparm (parm, NULL);
437c2322
NP
8788 }
8789 if (seen_open_paren)
8790 c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
8791 else
8792 {
8793 /* If there was no open parenthesis, we are recovering from
8794 an error, and we are trying to figure out what mistake
8795 the user has made. */
8796
8797 /* If there is an immediate closing parenthesis, the user
8798 probably forgot the opening one (ie, they typed "@catch
8799 NSException *e)". Parse the closing parenthesis and keep
8800 going. */
8801 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
8802 c_parser_consume_token (parser);
8803
8804 /* If these is no immediate closing parenthesis, the user
8805 probably doesn't know that parenthesis are required at
8806 all (ie, they typed "@catch NSException *e"). So, just
8807 forget about the closing parenthesis and keep going. */
8808 }
8809 objc_begin_catch_clause (parameter_declaration);
27bf414c
JM
8810 if (c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
8811 c_parser_compound_statement_nostart (parser);
8812 objc_finish_catch_clause ();
8813 }
8814 if (c_parser_next_token_is_keyword (parser, RID_AT_FINALLY))
8815 {
27bf414c 8816 c_parser_consume_token (parser);
437c2322
NP
8817 location = c_parser_peek_token (parser)->location;
8818 stmt = c_parser_compound_statement (parser);
8819 objc_build_finally_clause (location, stmt);
27bf414c
JM
8820 }
8821 objc_finish_try_stmt ();
8822}
8823
8824/* Parse an objc-synchronized-statement.
8825
8826 objc-synchronized-statement:
8827 @synchronized ( expression ) compound-statement
8828*/
8829
8830static void
8831c_parser_objc_synchronized_statement (c_parser *parser)
8832{
8833 location_t loc;
8834 tree expr, stmt;
8835 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_SYNCHRONIZED));
8836 c_parser_consume_token (parser);
8837 loc = c_parser_peek_token (parser)->location;
46270f14 8838 objc_maybe_warn_exceptions (loc);
27bf414c
JM
8839 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
8840 {
267bac10
JM
8841 struct c_expr ce = c_parser_expression (parser);
8842 ce = convert_lvalue_to_rvalue (loc, ce, false, false);
8843 expr = ce.value;
928c19bb 8844 expr = c_fully_fold (expr, false, NULL);
27bf414c
JM
8845 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
8846 }
8847 else
8848 expr = error_mark_node;
8849 stmt = c_parser_compound_statement (parser);
8850 objc_build_synchronized (loc, expr, stmt);
8851}
8852
8853/* Parse an objc-selector; return NULL_TREE without an error if the
8854 next token is not an objc-selector.
8855
8856 objc-selector:
8857 identifier
8858 one of
8859 enum struct union if else while do for switch case default
8860 break continue return goto asm sizeof typeof __alignof
8861 unsigned long const short volatile signed restrict _Complex
8862 in out inout bycopy byref oneway int char float double void _Bool
267bac10 8863 _Atomic
27bf414c
JM
8864
8865 ??? Why this selection of keywords but not, for example, storage
8866 class specifiers? */
8867
8868static tree
8869c_parser_objc_selector (c_parser *parser)
8870{
8871 c_token *token = c_parser_peek_token (parser);
8872 tree value = token->value;
8873 if (token->type == CPP_NAME)
8874 {
8875 c_parser_consume_token (parser);
8876 return value;
8877 }
8878 if (token->type != CPP_KEYWORD)
8879 return NULL_TREE;
8880 switch (token->keyword)
8881 {
8882 case RID_ENUM:
8883 case RID_STRUCT:
8884 case RID_UNION:
8885 case RID_IF:
8886 case RID_ELSE:
8887 case RID_WHILE:
8888 case RID_DO:
8889 case RID_FOR:
8890 case RID_SWITCH:
8891 case RID_CASE:
8892 case RID_DEFAULT:
8893 case RID_BREAK:
8894 case RID_CONTINUE:
8895 case RID_RETURN:
8896 case RID_GOTO:
8897 case RID_ASM:
8898 case RID_SIZEOF:
8899 case RID_TYPEOF:
8900 case RID_ALIGNOF:
8901 case RID_UNSIGNED:
8902 case RID_LONG:
a6766312 8903 case RID_INT128:
27bf414c
JM
8904 case RID_CONST:
8905 case RID_SHORT:
8906 case RID_VOLATILE:
8907 case RID_SIGNED:
8908 case RID_RESTRICT:
8909 case RID_COMPLEX:
8910 case RID_IN:
8911 case RID_OUT:
8912 case RID_INOUT:
8913 case RID_BYCOPY:
8914 case RID_BYREF:
8915 case RID_ONEWAY:
8916 case RID_INT:
8917 case RID_CHAR:
8918 case RID_FLOAT:
8919 case RID_DOUBLE:
8920 case RID_VOID:
8921 case RID_BOOL:
267bac10 8922 case RID_ATOMIC:
38b7bc7f 8923 case RID_AUTO_TYPE:
27bf414c
JM
8924 c_parser_consume_token (parser);
8925 return value;
8926 default:
8927 return NULL_TREE;
8928 }
8929}
8930
8931/* Parse an objc-selector-arg.
8932
8933 objc-selector-arg:
8934 objc-selector
8935 objc-keywordname-list
8936
8937 objc-keywordname-list:
8938 objc-keywordname
8939 objc-keywordname-list objc-keywordname
8940
8941 objc-keywordname:
8942 objc-selector :
8943 :
8944*/
8945
8946static tree
8947c_parser_objc_selector_arg (c_parser *parser)
8948{
8949 tree sel = c_parser_objc_selector (parser);
8950 tree list = NULL_TREE;
8951 if (sel && c_parser_next_token_is_not (parser, CPP_COLON))
8952 return sel;
8953 while (true)
8954 {
8955 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
8956 return list;
8957 list = chainon (list, build_tree_list (sel, NULL_TREE));
8958 sel = c_parser_objc_selector (parser);
8959 if (!sel && c_parser_next_token_is_not (parser, CPP_COLON))
8960 break;
8961 }
8962 return list;
8963}
8964
8965/* Parse an objc-receiver.
8966
8967 objc-receiver:
8968 expression
8969 class-name
8970 type-name
8971*/
8972
8973static tree
8974c_parser_objc_receiver (c_parser *parser)
8975{
267bac10
JM
8976 location_t loc = c_parser_peek_token (parser)->location;
8977
27bf414c
JM
8978 if (c_parser_peek_token (parser)->type == CPP_NAME
8979 && (c_parser_peek_token (parser)->id_kind == C_ID_TYPENAME
8980 || c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME))
8981 {
8982 tree id = c_parser_peek_token (parser)->value;
8983 c_parser_consume_token (parser);
8984 return objc_get_class_reference (id);
8985 }
267bac10
JM
8986 struct c_expr ce = c_parser_expression (parser);
8987 ce = convert_lvalue_to_rvalue (loc, ce, false, false);
8988 return c_fully_fold (ce.value, false, NULL);
27bf414c
JM
8989}
8990
8991/* Parse objc-message-args.
8992
8993 objc-message-args:
8994 objc-selector
8995 objc-keywordarg-list
8996
8997 objc-keywordarg-list:
8998 objc-keywordarg
8999 objc-keywordarg-list objc-keywordarg
9000
9001 objc-keywordarg:
9002 objc-selector : objc-keywordexpr
9003 : objc-keywordexpr
9004*/
9005
9006static tree
9007c_parser_objc_message_args (c_parser *parser)
9008{
9009 tree sel = c_parser_objc_selector (parser);
9010 tree list = NULL_TREE;
9011 if (sel && c_parser_next_token_is_not (parser, CPP_COLON))
9012 return sel;
9013 while (true)
9014 {
9015 tree keywordexpr;
9016 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
0a7d7dea 9017 return error_mark_node;
27bf414c
JM
9018 keywordexpr = c_parser_objc_keywordexpr (parser);
9019 list = chainon (list, build_tree_list (sel, keywordexpr));
9020 sel = c_parser_objc_selector (parser);
9021 if (!sel && c_parser_next_token_is_not (parser, CPP_COLON))
9022 break;
9023 }
9024 return list;
9025}
9026
9027/* Parse an objc-keywordexpr.
9028
9029 objc-keywordexpr:
9030 nonempty-expr-list
9031*/
9032
9033static tree
9034c_parser_objc_keywordexpr (c_parser *parser)
9035{
bbbbb16a 9036 tree ret;
9771b263 9037 vec<tree, va_gc> *expr_list = c_parser_expr_list (parser, true, true,
81e5eca8 9038 NULL, NULL, NULL, NULL);
9771b263 9039 if (vec_safe_length (expr_list) == 1)
27bf414c
JM
9040 {
9041 /* Just return the expression, remove a level of
9042 indirection. */
9771b263 9043 ret = (*expr_list)[0];
27bf414c
JM
9044 }
9045 else
9046 {
9047 /* We have a comma expression, we will collapse later. */
c166b898 9048 ret = build_tree_list_vec (expr_list);
27bf414c 9049 }
c166b898 9050 release_tree_vector (expr_list);
bbbbb16a 9051 return ret;
27bf414c
JM
9052}
9053
c165dca7
IS
9054/* A check, needed in several places, that ObjC interface, implementation or
9055 method definitions are not prefixed by incorrect items. */
9056static bool
9057c_parser_objc_diagnose_bad_element_prefix (c_parser *parser,
9058 struct c_declspecs *specs)
9059{
9e5b2115
PB
9060 if (!specs->declspecs_seen_p || specs->non_sc_seen_p
9061 || specs->typespec_kind != ctsk_none)
c165dca7
IS
9062 {
9063 c_parser_error (parser,
9064 "no type or storage class may be specified here,");
9065 c_parser_skip_to_end_of_block_or_statement (parser);
9066 return true;
9067 }
9068 return false;
9069}
668ea4b1 9070
f614132b 9071/* Parse an Objective-C @property declaration. The syntax is:
668ea4b1 9072
f614132b
NP
9073 objc-property-declaration:
9074 '@property' objc-property-attributes[opt] struct-declaration ;
668ea4b1 9075
f614132b
NP
9076 objc-property-attributes:
9077 '(' objc-property-attribute-list ')'
9078
9079 objc-property-attribute-list:
9080 objc-property-attribute
9081 objc-property-attribute-list, objc-property-attribute
9082
9083 objc-property-attribute
9084 'getter' = identifier
9085 'setter' = identifier
9086 'readonly'
9087 'readwrite'
9088 'assign'
9089 'retain'
9090 'copy'
9091 'nonatomic'
9092
9093 For example:
9094 @property NSString *name;
9095 @property (readonly) id object;
9096 @property (retain, nonatomic, getter=getTheName) id name;
9097 @property int a, b, c;
9098
9099 PS: This function is identical to cp_parser_objc_at_propery_declaration
200290f2 9100 for C++. Keep them in sync. */
668ea4b1 9101static void
f614132b 9102c_parser_objc_at_property_declaration (c_parser *parser)
668ea4b1 9103{
200290f2
NP
9104 /* The following variables hold the attributes of the properties as
9105 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
9106 seen. When we see an attribute, we set them to 'true' (if they
9107 are boolean properties) or to the identifier (if they have an
9108 argument, ie, for getter and setter). Note that here we only
9109 parse the list of attributes, check the syntax and accumulate the
9110 attributes that we find. objc_add_property_declaration() will
9111 then process the information. */
9112 bool property_assign = false;
9113 bool property_copy = false;
9114 tree property_getter_ident = NULL_TREE;
9115 bool property_nonatomic = false;
9116 bool property_readonly = false;
9117 bool property_readwrite = false;
9118 bool property_retain = false;
9119 tree property_setter_ident = NULL_TREE;
200290f2
NP
9120
9121 /* 'properties' is the list of properties that we read. Usually a
9122 single one, but maybe more (eg, in "@property int a, b, c;" there
9123 are three). */
f614132b
NP
9124 tree properties;
9125 location_t loc;
200290f2 9126
f614132b
NP
9127 loc = c_parser_peek_token (parser)->location;
9128 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_PROPERTY));
668ea4b1 9129
f614132b 9130 c_parser_consume_token (parser); /* Eat '@property'. */
668ea4b1 9131
f614132b
NP
9132 /* Parse the optional attribute list... */
9133 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
668ea4b1 9134 {
f614132b
NP
9135 /* Eat the '(' */
9136 c_parser_consume_token (parser);
9137
9138 /* Property attribute keywords are valid now. */
9139 parser->objc_property_attr_context = true;
9140
9141 while (true)
668ea4b1 9142 {
f614132b
NP
9143 bool syntax_error = false;
9144 c_token *token = c_parser_peek_token (parser);
9145 enum rid keyword;
9146
9147 if (token->type != CPP_KEYWORD)
9148 {
9149 if (token->type == CPP_CLOSE_PAREN)
9150 c_parser_error (parser, "expected identifier");
9151 else
9152 {
9153 c_parser_consume_token (parser);
9154 c_parser_error (parser, "unknown property attribute");
9155 }
9156 break;
9157 }
9158 keyword = token->keyword;
200290f2 9159 c_parser_consume_token (parser);
f614132b
NP
9160 switch (keyword)
9161 {
200290f2 9162 case RID_ASSIGN: property_assign = true; break;
200290f2
NP
9163 case RID_COPY: property_copy = true; break;
9164 case RID_NONATOMIC: property_nonatomic = true; break;
9165 case RID_READONLY: property_readonly = true; break;
9166 case RID_READWRITE: property_readwrite = true; break;
9167 case RID_RETAIN: property_retain = true; break;
9168
f614132b
NP
9169 case RID_GETTER:
9170 case RID_SETTER:
f614132b
NP
9171 if (c_parser_next_token_is_not (parser, CPP_EQ))
9172 {
d853ee42
NP
9173 if (keyword == RID_GETTER)
9174 c_parser_error (parser,
9175 "missing %<=%> (after %<getter%> attribute)");
9176 else
9177 c_parser_error (parser,
9178 "missing %<=%> (after %<setter%> attribute)");
f614132b
NP
9179 syntax_error = true;
9180 break;
9181 }
9182 c_parser_consume_token (parser); /* eat the = */
9183 if (c_parser_next_token_is_not (parser, CPP_NAME))
9184 {
9185 c_parser_error (parser, "expected identifier");
9186 syntax_error = true;
9187 break;
9188 }
f614132b
NP
9189 if (keyword == RID_SETTER)
9190 {
200290f2
NP
9191 if (property_setter_ident != NULL_TREE)
9192 c_parser_error (parser, "the %<setter%> attribute may only be specified once");
9193 else
9194 property_setter_ident = c_parser_peek_token (parser)->value;
f614132b 9195 c_parser_consume_token (parser);
200290f2
NP
9196 if (c_parser_next_token_is_not (parser, CPP_COLON))
9197 c_parser_error (parser, "setter name must terminate with %<:%>");
9198 else
9199 c_parser_consume_token (parser);
f614132b 9200 }
46a88c12 9201 else
f614132b 9202 {
200290f2
NP
9203 if (property_getter_ident != NULL_TREE)
9204 c_parser_error (parser, "the %<getter%> attribute may only be specified once");
9205 else
9206 property_getter_ident = c_parser_peek_token (parser)->value;
f614132b 9207 c_parser_consume_token (parser);
f614132b 9208 }
200290f2
NP
9209 break;
9210 default:
9211 c_parser_error (parser, "unknown property attribute");
f614132b
NP
9212 syntax_error = true;
9213 break;
9214 }
9215
9216 if (syntax_error)
668ea4b1 9217 break;
f614132b
NP
9218
9219 if (c_parser_next_token_is (parser, CPP_COMMA))
668ea4b1 9220 c_parser_consume_token (parser);
f614132b 9221 else
668ea4b1
IS
9222 break;
9223 }
f614132b
NP
9224 parser->objc_property_attr_context = false;
9225 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
9226 }
9227 /* ... and the property declaration(s). */
9228 properties = c_parser_struct_declaration (parser);
668ea4b1 9229
f614132b
NP
9230 if (properties == error_mark_node)
9231 {
9232 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
9233 parser->error = false;
9234 return;
9235 }
668ea4b1 9236
f614132b
NP
9237 if (properties == NULL_TREE)
9238 c_parser_error (parser, "expected identifier");
9239 else
9240 {
9241 /* Comma-separated properties are chained together in
9242 reverse order; add them one by one. */
9243 properties = nreverse (properties);
9244
9245 for (; properties; properties = TREE_CHAIN (properties))
200290f2
NP
9246 objc_add_property_declaration (loc, copy_node (properties),
9247 property_readonly, property_readwrite,
9248 property_assign, property_retain,
9249 property_copy, property_nonatomic,
46a88c12 9250 property_getter_ident, property_setter_ident);
f614132b 9251 }
668ea4b1
IS
9252
9253 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
f614132b 9254 parser->error = false;
668ea4b1
IS
9255}
9256
da57d1b9
NP
9257/* Parse an Objective-C @synthesize declaration. The syntax is:
9258
9259 objc-synthesize-declaration:
9260 @synthesize objc-synthesize-identifier-list ;
9261
9262 objc-synthesize-identifier-list:
9263 objc-synthesize-identifier
9264 objc-synthesize-identifier-list, objc-synthesize-identifier
9265
9266 objc-synthesize-identifier
9267 identifier
9268 identifier = identifier
9269
9270 For example:
9271 @synthesize MyProperty;
9272 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
9273
9274 PS: This function is identical to cp_parser_objc_at_synthesize_declaration
9275 for C++. Keep them in sync.
9276*/
9277static void
9278c_parser_objc_at_synthesize_declaration (c_parser *parser)
9279{
9280 tree list = NULL_TREE;
9281 location_t loc;
9282 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_SYNTHESIZE));
9283 loc = c_parser_peek_token (parser)->location;
9284
9285 c_parser_consume_token (parser);
9286 while (true)
9287 {
9288 tree property, ivar;
9289 if (c_parser_next_token_is_not (parser, CPP_NAME))
9290 {
9291 c_parser_error (parser, "expected identifier");
9292 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
9293 /* Once we find the semicolon, we can resume normal parsing.
9294 We have to reset parser->error manually because
9295 c_parser_skip_until_found() won't reset it for us if the
9296 next token is precisely a semicolon. */
9297 parser->error = false;
9298 return;
9299 }
9300 property = c_parser_peek_token (parser)->value;
9301 c_parser_consume_token (parser);
9302 if (c_parser_next_token_is (parser, CPP_EQ))
9303 {
9304 c_parser_consume_token (parser);
9305 if (c_parser_next_token_is_not (parser, CPP_NAME))
9306 {
9307 c_parser_error (parser, "expected identifier");
9308 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
9309 parser->error = false;
9310 return;
9311 }
9312 ivar = c_parser_peek_token (parser)->value;
9313 c_parser_consume_token (parser);
9314 }
9315 else
9316 ivar = NULL_TREE;
9317 list = chainon (list, build_tree_list (ivar, property));
9318 if (c_parser_next_token_is (parser, CPP_COMMA))
9319 c_parser_consume_token (parser);
9320 else
9321 break;
9322 }
9323 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
9324 objc_add_synthesize_declaration (loc, list);
9325}
9326
9327/* Parse an Objective-C @dynamic declaration. The syntax is:
9328
9329 objc-dynamic-declaration:
9330 @dynamic identifier-list ;
9331
9332 For example:
9333 @dynamic MyProperty;
9334 @dynamic MyProperty, AnotherProperty;
9335
9336 PS: This function is identical to cp_parser_objc_at_dynamic_declaration
9337 for C++. Keep them in sync.
9338*/
9339static void
9340c_parser_objc_at_dynamic_declaration (c_parser *parser)
9341{
9342 tree list = NULL_TREE;
9343 location_t loc;
9344 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_DYNAMIC));
9345 loc = c_parser_peek_token (parser)->location;
9346
9347 c_parser_consume_token (parser);
9348 while (true)
9349 {
9350 tree property;
9351 if (c_parser_next_token_is_not (parser, CPP_NAME))
9352 {
9353 c_parser_error (parser, "expected identifier");
9354 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
9355 parser->error = false;
9356 return;
9357 }
9358 property = c_parser_peek_token (parser)->value;
9359 list = chainon (list, build_tree_list (NULL_TREE, property));
9360 c_parser_consume_token (parser);
9361 if (c_parser_next_token_is (parser, CPP_COMMA))
9362 c_parser_consume_token (parser);
9363 else
9364 break;
9365 }
9366 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
9367 objc_add_dynamic_declaration (loc, list);
9368}
9369
27bf414c 9370\f
953ff289
DN
9371/* Handle pragmas. Some OpenMP pragmas are associated with, and therefore
9372 should be considered, statements. ALLOW_STMT is true if we're within
9373 the context of a function and such pragmas are to be allowed. Returns
9374 true if we actually parsed such a pragma. */
27bf414c 9375
bc4071dd 9376static bool
953ff289 9377c_parser_pragma (c_parser *parser, enum pragma_context context)
bc4071dd
RH
9378{
9379 unsigned int id;
9380
9381 id = c_parser_peek_token (parser)->pragma_kind;
9382 gcc_assert (id != PRAGMA_NONE);
9383
9384 switch (id)
9385 {
953ff289
DN
9386 case PRAGMA_OMP_BARRIER:
9387 if (context != pragma_compound)
9388 {
9389 if (context == pragma_stmt)
9390 c_parser_error (parser, "%<#pragma omp barrier%> may only be "
9391 "used in compound statements");
9392 goto bad_stmt;
9393 }
9394 c_parser_omp_barrier (parser);
9395 return false;
9396
9397 case PRAGMA_OMP_FLUSH:
9398 if (context != pragma_compound)
9399 {
9400 if (context == pragma_stmt)
9401 c_parser_error (parser, "%<#pragma omp flush%> may only be "
9402 "used in compound statements");
9403 goto bad_stmt;
9404 }
9405 c_parser_omp_flush (parser);
9406 return false;
9407
a68ab351
JJ
9408 case PRAGMA_OMP_TASKWAIT:
9409 if (context != pragma_compound)
9410 {
9411 if (context == pragma_stmt)
9412 c_parser_error (parser, "%<#pragma omp taskwait%> may only be "
9413 "used in compound statements");
9414 goto bad_stmt;
9415 }
9416 c_parser_omp_taskwait (parser);
9417 return false;
9418
20906c66
JJ
9419 case PRAGMA_OMP_TASKYIELD:
9420 if (context != pragma_compound)
9421 {
9422 if (context == pragma_stmt)
9423 c_parser_error (parser, "%<#pragma omp taskyield%> may only be "
9424 "used in compound statements");
9425 goto bad_stmt;
9426 }
9427 c_parser_omp_taskyield (parser);
9428 return false;
9429
acf0174b
JJ
9430 case PRAGMA_OMP_CANCEL:
9431 if (context != pragma_compound)
9432 {
9433 if (context == pragma_stmt)
9434 c_parser_error (parser, "%<#pragma omp cancel%> may only be "
9435 "used in compound statements");
9436 goto bad_stmt;
9437 }
9438 c_parser_omp_cancel (parser);
9439 return false;
9440
9441 case PRAGMA_OMP_CANCELLATION_POINT:
9442 if (context != pragma_compound)
9443 {
9444 if (context == pragma_stmt)
9445 c_parser_error (parser, "%<#pragma omp cancellation point%> may "
9446 "only be used in compound statements");
9447 goto bad_stmt;
9448 }
9449 c_parser_omp_cancellation_point (parser);
9450 return false;
9451
953ff289
DN
9452 case PRAGMA_OMP_THREADPRIVATE:
9453 c_parser_omp_threadprivate (parser);
9454 return false;
9455
acf0174b
JJ
9456 case PRAGMA_OMP_TARGET:
9457 return c_parser_omp_target (parser, context);
9458
9459 case PRAGMA_OMP_END_DECLARE_TARGET:
9460 c_parser_omp_end_declare_target (parser);
9461 return false;
9462
953ff289 9463 case PRAGMA_OMP_SECTION:
3ba09659
AH
9464 error_at (c_parser_peek_token (parser)->location,
9465 "%<#pragma omp section%> may only be used in "
9466 "%<#pragma omp sections%> construct");
953ff289
DN
9467 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
9468 return false;
9469
acf0174b
JJ
9470 case PRAGMA_OMP_DECLARE_REDUCTION:
9471 c_parser_omp_declare (parser, context);
9472 return false;
8170608b
TB
9473 case PRAGMA_IVDEP:
9474 c_parser_consume_pragma (parser);
9475 c_parser_skip_to_pragma_eol (parser);
d4af74d4
TB
9476 if (!c_parser_next_token_is_keyword (parser, RID_FOR)
9477 && !c_parser_next_token_is_keyword (parser, RID_WHILE)
9478 && !c_parser_next_token_is_keyword (parser, RID_DO))
8170608b 9479 {
d4af74d4 9480 c_parser_error (parser, "for, while or do statement expected");
8170608b
TB
9481 return false;
9482 }
d4af74d4
TB
9483 if (c_parser_next_token_is_keyword (parser, RID_FOR))
9484 c_parser_for_statement (parser, true);
9485 else if (c_parser_next_token_is_keyword (parser, RID_WHILE))
9486 c_parser_while_statement (parser, true);
9487 else
9488 c_parser_do_statement (parser, true);
8170608b 9489 return false;
acf0174b 9490
bc4071dd
RH
9491 case PRAGMA_GCC_PCH_PREPROCESS:
9492 c_parser_error (parser, "%<#pragma GCC pch_preprocess%> must be first");
9493 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
9494 return false;
9495
c02065fc
AH
9496 case PRAGMA_CILK_SIMD:
9497 if (!c_parser_cilk_verify_simd (parser, context))
9498 return false;
9499 c_parser_consume_pragma (parser);
9500 c_parser_cilk_simd (parser);
9501 return false;
9502
bc4071dd 9503 default:
953ff289
DN
9504 if (id < PRAGMA_FIRST_EXTERNAL)
9505 {
acf0174b 9506 if (context != pragma_stmt && context != pragma_compound)
953ff289
DN
9507 {
9508 bad_stmt:
9509 c_parser_error (parser, "expected declaration specifiers");
9510 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
9511 return false;
9512 }
9513 c_parser_omp_construct (parser);
9514 return true;
9515 }
bc4071dd
RH
9516 break;
9517 }
9518
9519 c_parser_consume_pragma (parser);
9520 c_invoke_pragma_handler (id);
27bf414c 9521
b8698a0f 9522 /* Skip to EOL, but suppress any error message. Those will have been
bc4071dd
RH
9523 generated by the handler routine through calling error, as opposed
9524 to calling c_parser_error. */
9525 parser->error = true;
9526 c_parser_skip_to_pragma_eol (parser);
9527
9528 return false;
9529}
9530
9531/* The interface the pragma parsers have to the lexer. */
9532
9533enum cpp_ttype
9534pragma_lex (tree *value)
9535{
9536 c_token *tok = c_parser_peek_token (the_parser);
9537 enum cpp_ttype ret = tok->type;
9538
9539 *value = tok->value;
9540 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
9541 ret = CPP_EOF;
9542 else
9543 {
9544 if (ret == CPP_KEYWORD)
9545 ret = CPP_NAME;
9546 c_parser_consume_token (the_parser);
9547 }
9548
9549 return ret;
9550}
9551
9552static void
9553c_parser_pragma_pch_preprocess (c_parser *parser)
9554{
9555 tree name = NULL;
9556
9557 c_parser_consume_pragma (parser);
9558 if (c_parser_next_token_is (parser, CPP_STRING))
9559 {
9560 name = c_parser_peek_token (parser)->value;
9561 c_parser_consume_token (parser);
9562 }
9563 else
9564 c_parser_error (parser, "expected string literal");
9565 c_parser_skip_to_pragma_eol (parser);
9566
9567 if (name)
9568 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
9569}
953ff289 9570\f
acf0174b 9571/* OpenMP 2.5 / 3.0 / 3.1 / 4.0 parsing routines. */
953ff289
DN
9572
9573/* Returns name of the next clause.
9574 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
9575 the token is not consumed. Otherwise appropriate pragma_omp_clause is
9576 returned and the token is consumed. */
9577
9578static pragma_omp_clause
9579c_parser_omp_clause_name (c_parser *parser)
9580{
9581 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
9582
9583 if (c_parser_next_token_is_keyword (parser, RID_IF))
9584 result = PRAGMA_OMP_CLAUSE_IF;
9585 else if (c_parser_next_token_is_keyword (parser, RID_DEFAULT))
9586 result = PRAGMA_OMP_CLAUSE_DEFAULT;
acf0174b
JJ
9587 else if (c_parser_next_token_is_keyword (parser, RID_FOR))
9588 result = PRAGMA_OMP_CLAUSE_FOR;
953ff289
DN
9589 else if (c_parser_next_token_is (parser, CPP_NAME))
9590 {
9591 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
9592
9593 switch (p[0])
9594 {
acf0174b
JJ
9595 case 'a':
9596 if (!strcmp ("aligned", p))
9597 result = PRAGMA_OMP_CLAUSE_ALIGNED;
9598 break;
953ff289 9599 case 'c':
a68ab351
JJ
9600 if (!strcmp ("collapse", p))
9601 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
9602 else if (!strcmp ("copyin", p))
953ff289
DN
9603 result = PRAGMA_OMP_CLAUSE_COPYIN;
9604 else if (!strcmp ("copyprivate", p))
9605 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
9606 break;
acf0174b
JJ
9607 case 'd':
9608 if (!strcmp ("depend", p))
9609 result = PRAGMA_OMP_CLAUSE_DEPEND;
9610 else if (!strcmp ("device", p))
9611 result = PRAGMA_OMP_CLAUSE_DEVICE;
9612 else if (!strcmp ("dist_schedule", p))
9613 result = PRAGMA_OMP_CLAUSE_DIST_SCHEDULE;
9614 break;
953ff289 9615 case 'f':
20906c66
JJ
9616 if (!strcmp ("final", p))
9617 result = PRAGMA_OMP_CLAUSE_FINAL;
9618 else if (!strcmp ("firstprivate", p))
953ff289 9619 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
acf0174b
JJ
9620 else if (!strcmp ("from", p))
9621 result = PRAGMA_OMP_CLAUSE_FROM;
9622 break;
9623 case 'i':
9624 if (!strcmp ("inbranch", p))
9625 result = PRAGMA_OMP_CLAUSE_INBRANCH;
953ff289
DN
9626 break;
9627 case 'l':
9628 if (!strcmp ("lastprivate", p))
9629 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
acf0174b
JJ
9630 else if (!strcmp ("linear", p))
9631 result = PRAGMA_OMP_CLAUSE_LINEAR;
953ff289 9632 break;
20906c66 9633 case 'm':
acf0174b
JJ
9634 if (!strcmp ("map", p))
9635 result = PRAGMA_OMP_CLAUSE_MAP;
9636 else if (!strcmp ("mergeable", p))
20906c66 9637 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
b72271b9 9638 else if (flag_cilkplus && !strcmp ("mask", p))
41958c28 9639 result = PRAGMA_CILK_CLAUSE_MASK;
20906c66 9640 break;
953ff289 9641 case 'n':
acf0174b
JJ
9642 if (!strcmp ("notinbranch", p))
9643 result = PRAGMA_OMP_CLAUSE_NOTINBRANCH;
9644 else if (!strcmp ("nowait", p))
953ff289 9645 result = PRAGMA_OMP_CLAUSE_NOWAIT;
acf0174b
JJ
9646 else if (!strcmp ("num_teams", p))
9647 result = PRAGMA_OMP_CLAUSE_NUM_TEAMS;
953ff289
DN
9648 else if (!strcmp ("num_threads", p))
9649 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
b72271b9 9650 else if (flag_cilkplus && !strcmp ("nomask", p))
41958c28 9651 result = PRAGMA_CILK_CLAUSE_NOMASK;
953ff289
DN
9652 break;
9653 case 'o':
9654 if (!strcmp ("ordered", p))
9655 result = PRAGMA_OMP_CLAUSE_ORDERED;
9656 break;
9657 case 'p':
acf0174b
JJ
9658 if (!strcmp ("parallel", p))
9659 result = PRAGMA_OMP_CLAUSE_PARALLEL;
9660 else if (!strcmp ("private", p))
953ff289 9661 result = PRAGMA_OMP_CLAUSE_PRIVATE;
acf0174b
JJ
9662 else if (!strcmp ("proc_bind", p))
9663 result = PRAGMA_OMP_CLAUSE_PROC_BIND;
953ff289
DN
9664 break;
9665 case 'r':
9666 if (!strcmp ("reduction", p))
9667 result = PRAGMA_OMP_CLAUSE_REDUCTION;
9668 break;
9669 case 's':
acf0174b
JJ
9670 if (!strcmp ("safelen", p))
9671 result = PRAGMA_OMP_CLAUSE_SAFELEN;
9672 else if (!strcmp ("schedule", p))
953ff289 9673 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
acf0174b
JJ
9674 else if (!strcmp ("sections", p))
9675 result = PRAGMA_OMP_CLAUSE_SECTIONS;
953ff289
DN
9676 else if (!strcmp ("shared", p))
9677 result = PRAGMA_OMP_CLAUSE_SHARED;
acf0174b
JJ
9678 else if (!strcmp ("simdlen", p))
9679 result = PRAGMA_OMP_CLAUSE_SIMDLEN;
9680 break;
9681 case 't':
9682 if (!strcmp ("taskgroup", p))
9683 result = PRAGMA_OMP_CLAUSE_TASKGROUP;
9684 else if (!strcmp ("thread_limit", p))
9685 result = PRAGMA_OMP_CLAUSE_THREAD_LIMIT;
9686 else if (!strcmp ("to", p))
9687 result = PRAGMA_OMP_CLAUSE_TO;
953ff289 9688 break;
a68ab351 9689 case 'u':
acf0174b
JJ
9690 if (!strcmp ("uniform", p))
9691 result = PRAGMA_OMP_CLAUSE_UNIFORM;
9692 else if (!strcmp ("untied", p))
a68ab351
JJ
9693 result = PRAGMA_OMP_CLAUSE_UNTIED;
9694 break;
41958c28 9695 case 'v':
b72271b9 9696 if (flag_cilkplus && !strcmp ("vectorlength", p))
41958c28
BI
9697 result = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
9698 break;
953ff289
DN
9699 }
9700 }
9701
9702 if (result != PRAGMA_OMP_CLAUSE_NONE)
9703 c_parser_consume_token (parser);
9704
9705 return result;
9706}
9707
9708/* Validate that a clause of the given type does not already exist. */
9709
9710static void
d75d71e0
ILT
9711check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
9712 const char *name)
953ff289
DN
9713{
9714 tree c;
9715
9716 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
aaf46ef9 9717 if (OMP_CLAUSE_CODE (c) == code)
953ff289 9718 {
c2255bc4
AH
9719 location_t loc = OMP_CLAUSE_LOCATION (c);
9720 error_at (loc, "too many %qs clauses", name);
953ff289
DN
9721 break;
9722 }
9723}
9724
9725/* OpenMP 2.5:
9726 variable-list:
9727 identifier
9728 variable-list , identifier
9729
c2255bc4
AH
9730 If KIND is nonzero, create the appropriate node and install the
9731 decl in OMP_CLAUSE_DECL and add the node to the head of the list.
9732 If KIND is nonzero, CLAUSE_LOC is the location of the clause.
953ff289
DN
9733
9734 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
9735 return the list created. */
9736
9737static tree
c2255bc4
AH
9738c_parser_omp_variable_list (c_parser *parser,
9739 location_t clause_loc,
acf0174b 9740 enum omp_clause_code kind, tree list)
953ff289
DN
9741{
9742 if (c_parser_next_token_is_not (parser, CPP_NAME)
9743 || c_parser_peek_token (parser)->id_kind != C_ID_ID)
9744 c_parser_error (parser, "expected identifier");
9745
9746 while (c_parser_next_token_is (parser, CPP_NAME)
9747 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
9748 {
9749 tree t = lookup_name (c_parser_peek_token (parser)->value);
9750
9751 if (t == NULL_TREE)
acf0174b
JJ
9752 {
9753 undeclared_variable (c_parser_peek_token (parser)->location,
9754 c_parser_peek_token (parser)->value);
9755 t = error_mark_node;
9756 }
9757
9758 c_parser_consume_token (parser);
9759
9760 if (t == error_mark_node)
953ff289
DN
9761 ;
9762 else if (kind != 0)
9763 {
acf0174b
JJ
9764 switch (kind)
9765 {
9766 case OMP_CLAUSE_MAP:
9767 case OMP_CLAUSE_FROM:
9768 case OMP_CLAUSE_TO:
9769 case OMP_CLAUSE_DEPEND:
9770 while (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
9771 {
9772 tree low_bound = NULL_TREE, length = NULL_TREE;
9773
9774 c_parser_consume_token (parser);
9775 if (!c_parser_next_token_is (parser, CPP_COLON))
9776 low_bound = c_parser_expression (parser).value;
9777 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
9778 length = integer_one_node;
9779 else
9780 {
9781 /* Look for `:'. */
9782 if (!c_parser_require (parser, CPP_COLON,
9783 "expected %<:%>"))
9784 {
9785 t = error_mark_node;
9786 break;
9787 }
9788 if (!c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
9789 length = c_parser_expression (parser).value;
9790 }
9791 /* Look for the closing `]'. */
9792 if (!c_parser_require (parser, CPP_CLOSE_SQUARE,
9793 "expected %<]%>"))
9794 {
9795 t = error_mark_node;
9796 break;
9797 }
9798 t = tree_cons (low_bound, length, t);
9799 }
9800 break;
9801 default:
9802 break;
9803 }
9804
9805 if (t != error_mark_node)
9806 {
9807 tree u = build_omp_clause (clause_loc, kind);
9808 OMP_CLAUSE_DECL (u) = t;
9809 OMP_CLAUSE_CHAIN (u) = list;
9810 list = u;
9811 }
953ff289
DN
9812 }
9813 else
9814 list = tree_cons (t, NULL_TREE, list);
9815
953ff289
DN
9816 if (c_parser_next_token_is_not (parser, CPP_COMMA))
9817 break;
9818
9819 c_parser_consume_token (parser);
9820 }
9821
9822 return list;
9823}
9824
9825/* Similarly, but expect leading and trailing parenthesis. This is a very
9826 common case for omp clauses. */
9827
9828static tree
d75d71e0
ILT
9829c_parser_omp_var_list_parens (c_parser *parser, enum omp_clause_code kind,
9830 tree list)
953ff289 9831{
c2255bc4
AH
9832 /* The clauses location. */
9833 location_t loc = c_parser_peek_token (parser)->location;
9834
953ff289
DN
9835 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
9836 {
c2255bc4 9837 list = c_parser_omp_variable_list (parser, loc, kind, list);
953ff289
DN
9838 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
9839 }
9840 return list;
9841}
9842
a68ab351
JJ
9843/* OpenMP 3.0:
9844 collapse ( constant-expression ) */
9845
9846static tree
9847c_parser_omp_clause_collapse (c_parser *parser, tree list)
9848{
9849 tree c, num = error_mark_node;
9850 HOST_WIDE_INT n;
9851 location_t loc;
9852
9853 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse");
9854
9855 loc = c_parser_peek_token (parser)->location;
9856 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
9857 {
9858 num = c_parser_expr_no_commas (parser, NULL).value;
9859 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
9860 }
9861 if (num == error_mark_node)
9862 return list;
acf0174b
JJ
9863 mark_exp_read (num);
9864 num = c_fully_fold (num, false, NULL);
a68ab351 9865 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
9541ffee 9866 || !tree_fits_shwi_p (num)
9439e9a1 9867 || (n = tree_to_shwi (num)) <= 0
a68ab351
JJ
9868 || (int) n != n)
9869 {
3ba09659
AH
9870 error_at (loc,
9871 "collapse argument needs positive constant integer expression");
a68ab351
JJ
9872 return list;
9873 }
c2255bc4 9874 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
a68ab351
JJ
9875 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
9876 OMP_CLAUSE_CHAIN (c) = list;
9877 return c;
9878}
9879
953ff289
DN
9880/* OpenMP 2.5:
9881 copyin ( variable-list ) */
9882
9883static tree
9884c_parser_omp_clause_copyin (c_parser *parser, tree list)
9885{
9886 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_COPYIN, list);
9887}
9888
9889/* OpenMP 2.5:
9890 copyprivate ( variable-list ) */
9891
9892static tree
9893c_parser_omp_clause_copyprivate (c_parser *parser, tree list)
9894{
9895 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_COPYPRIVATE, list);
9896}
9897
9898/* OpenMP 2.5:
9899 default ( shared | none ) */
9900
9901static tree
9902c_parser_omp_clause_default (c_parser *parser, tree list)
9903{
9904 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
c2255bc4 9905 location_t loc = c_parser_peek_token (parser)->location;
953ff289
DN
9906 tree c;
9907
9908 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
9909 return list;
9910 if (c_parser_next_token_is (parser, CPP_NAME))
9911 {
9912 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
9913
9914 switch (p[0])
9915 {
9916 case 'n':
9917 if (strcmp ("none", p) != 0)
9918 goto invalid_kind;
9919 kind = OMP_CLAUSE_DEFAULT_NONE;
9920 break;
9921
9922 case 's':
9923 if (strcmp ("shared", p) != 0)
9924 goto invalid_kind;
9925 kind = OMP_CLAUSE_DEFAULT_SHARED;
9926 break;
9927
9928 default:
9929 goto invalid_kind;
9930 }
9931
9932 c_parser_consume_token (parser);
9933 }
9934 else
9935 {
9936 invalid_kind:
9937 c_parser_error (parser, "expected %<none%> or %<shared%>");
9938 }
9939 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
9940
9941 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
9942 return list;
9943
9944 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default");
c2255bc4 9945 c = build_omp_clause (loc, OMP_CLAUSE_DEFAULT);
953ff289
DN
9946 OMP_CLAUSE_CHAIN (c) = list;
9947 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
9948
9949 return c;
9950}
9951
9952/* OpenMP 2.5:
9953 firstprivate ( variable-list ) */
9954
9955static tree
9956c_parser_omp_clause_firstprivate (c_parser *parser, tree list)
9957{
9958 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_FIRSTPRIVATE, list);
9959}
9960
20906c66
JJ
9961/* OpenMP 3.1:
9962 final ( expression ) */
9963
9964static tree
9965c_parser_omp_clause_final (c_parser *parser, tree list)
9966{
9967 location_t loc = c_parser_peek_token (parser)->location;
9968 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
9969 {
9970 tree t = c_parser_paren_condition (parser);
9971 tree c;
9972
9973 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final");
9974
9975 c = build_omp_clause (loc, OMP_CLAUSE_FINAL);
9976 OMP_CLAUSE_FINAL_EXPR (c) = t;
9977 OMP_CLAUSE_CHAIN (c) = list;
9978 list = c;
9979 }
9980 else
9981 c_parser_error (parser, "expected %<(%>");
9982
9983 return list;
9984}
9985
953ff289
DN
9986/* OpenMP 2.5:
9987 if ( expression ) */
9988
9989static tree
9990c_parser_omp_clause_if (c_parser *parser, tree list)
9991{
c2255bc4 9992 location_t loc = c_parser_peek_token (parser)->location;
953ff289
DN
9993 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
9994 {
9995 tree t = c_parser_paren_condition (parser);
9996 tree c;
9997
9998 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if");
9999
c2255bc4 10000 c = build_omp_clause (loc, OMP_CLAUSE_IF);
953ff289
DN
10001 OMP_CLAUSE_IF_EXPR (c) = t;
10002 OMP_CLAUSE_CHAIN (c) = list;
10003 list = c;
10004 }
10005 else
10006 c_parser_error (parser, "expected %<(%>");
10007
10008 return list;
10009}
10010
10011/* OpenMP 2.5:
10012 lastprivate ( variable-list ) */
10013
10014static tree
10015c_parser_omp_clause_lastprivate (c_parser *parser, tree list)
10016{
10017 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_LASTPRIVATE, list);
10018}
10019
20906c66
JJ
10020/* OpenMP 3.1:
10021 mergeable */
10022
10023static tree
10024c_parser_omp_clause_mergeable (c_parser *parser ATTRIBUTE_UNUSED, tree list)
10025{
10026 tree c;
10027
10028 /* FIXME: Should we allow duplicates? */
10029 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable");
10030
10031 c = build_omp_clause (c_parser_peek_token (parser)->location,
10032 OMP_CLAUSE_MERGEABLE);
10033 OMP_CLAUSE_CHAIN (c) = list;
10034
10035 return c;
10036}
10037
953ff289
DN
10038/* OpenMP 2.5:
10039 nowait */
10040
10041static tree
10042c_parser_omp_clause_nowait (c_parser *parser ATTRIBUTE_UNUSED, tree list)
10043{
10044 tree c;
c2255bc4 10045 location_t loc = c_parser_peek_token (parser)->location;
953ff289
DN
10046
10047 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait");
10048
c2255bc4 10049 c = build_omp_clause (loc, OMP_CLAUSE_NOWAIT);
953ff289
DN
10050 OMP_CLAUSE_CHAIN (c) = list;
10051 return c;
10052}
10053
10054/* OpenMP 2.5:
10055 num_threads ( expression ) */
10056
10057static tree
10058c_parser_omp_clause_num_threads (c_parser *parser, tree list)
10059{
c2255bc4 10060 location_t num_threads_loc = c_parser_peek_token (parser)->location;
953ff289
DN
10061 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10062 {
c7412148 10063 location_t expr_loc = c_parser_peek_token (parser)->location;
953ff289 10064 tree c, t = c_parser_expression (parser).value;
7d1362bc 10065 mark_exp_read (t);
928c19bb 10066 t = c_fully_fold (t, false, NULL);
953ff289
DN
10067
10068 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10069
10070 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
10071 {
10072 c_parser_error (parser, "expected integer expression");
10073 return list;
10074 }
10075
10076 /* Attempt to statically determine when the number isn't positive. */
db3927fb 10077 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
953ff289 10078 build_int_cst (TREE_TYPE (t), 0));
c2255bc4
AH
10079 if (CAN_HAVE_LOCATION_P (c))
10080 SET_EXPR_LOCATION (c, expr_loc);
953ff289
DN
10081 if (c == boolean_true_node)
10082 {
3ba09659
AH
10083 warning_at (expr_loc, 0,
10084 "%<num_threads%> value must be positive");
953ff289
DN
10085 t = integer_one_node;
10086 }
10087
10088 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS, "num_threads");
10089
c2255bc4 10090 c = build_omp_clause (num_threads_loc, OMP_CLAUSE_NUM_THREADS);
953ff289
DN
10091 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
10092 OMP_CLAUSE_CHAIN (c) = list;
10093 list = c;
10094 }
10095
10096 return list;
10097}
10098
10099/* OpenMP 2.5:
10100 ordered */
10101
10102static tree
c2255bc4 10103c_parser_omp_clause_ordered (c_parser *parser, tree list)
953ff289
DN
10104{
10105 tree c;
10106
10107 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED, "ordered");
10108
c2255bc4
AH
10109 c = build_omp_clause (c_parser_peek_token (parser)->location,
10110 OMP_CLAUSE_ORDERED);
953ff289 10111 OMP_CLAUSE_CHAIN (c) = list;
c2255bc4 10112
953ff289
DN
10113 return c;
10114}
10115
10116/* OpenMP 2.5:
10117 private ( variable-list ) */
10118
10119static tree
10120c_parser_omp_clause_private (c_parser *parser, tree list)
10121{
10122 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_PRIVATE, list);
10123}
10124
10125/* OpenMP 2.5:
10126 reduction ( reduction-operator : variable-list )
10127
10128 reduction-operator:
20906c66 10129 One of: + * - & ^ | && ||
acf0174b 10130
20906c66
JJ
10131 OpenMP 3.1:
10132
10133 reduction-operator:
acf0174b
JJ
10134 One of: + * - & ^ | && || max min
10135
10136 OpenMP 4.0:
10137
10138 reduction-operator:
10139 One of: + * - & ^ | && ||
10140 identifier */
953ff289
DN
10141
10142static tree
10143c_parser_omp_clause_reduction (c_parser *parser, tree list)
10144{
c2255bc4 10145 location_t clause_loc = c_parser_peek_token (parser)->location;
953ff289
DN
10146 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10147 {
acf0174b
JJ
10148 enum tree_code code = ERROR_MARK;
10149 tree reduc_id = NULL_TREE;
953ff289
DN
10150
10151 switch (c_parser_peek_token (parser)->type)
10152 {
10153 case CPP_PLUS:
10154 code = PLUS_EXPR;
10155 break;
10156 case CPP_MULT:
10157 code = MULT_EXPR;
10158 break;
10159 case CPP_MINUS:
10160 code = MINUS_EXPR;
10161 break;
10162 case CPP_AND:
10163 code = BIT_AND_EXPR;
10164 break;
10165 case CPP_XOR:
10166 code = BIT_XOR_EXPR;
10167 break;
10168 case CPP_OR:
10169 code = BIT_IOR_EXPR;
10170 break;
10171 case CPP_AND_AND:
10172 code = TRUTH_ANDIF_EXPR;
10173 break;
10174 case CPP_OR_OR:
10175 code = TRUTH_ORIF_EXPR;
10176 break;
20906c66
JJ
10177 case CPP_NAME:
10178 {
10179 const char *p
10180 = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
10181 if (strcmp (p, "min") == 0)
10182 {
10183 code = MIN_EXPR;
10184 break;
10185 }
10186 if (strcmp (p, "max") == 0)
10187 {
10188 code = MAX_EXPR;
10189 break;
10190 }
acf0174b
JJ
10191 reduc_id = c_parser_peek_token (parser)->value;
10192 break;
20906c66 10193 }
953ff289
DN
10194 default:
10195 c_parser_error (parser,
10196 "expected %<+%>, %<*%>, %<-%>, %<&%>, "
20906c66 10197 "%<^%>, %<|%>, %<&&%>, %<||%>, %<min%> or %<max%>");
953ff289
DN
10198 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 0);
10199 return list;
10200 }
10201 c_parser_consume_token (parser);
acf0174b 10202 reduc_id = c_omp_reduction_id (code, reduc_id);
953ff289
DN
10203 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
10204 {
10205 tree nl, c;
10206
c2255bc4
AH
10207 nl = c_parser_omp_variable_list (parser, clause_loc,
10208 OMP_CLAUSE_REDUCTION, list);
953ff289 10209 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
acf0174b
JJ
10210 {
10211 tree type = TREE_TYPE (OMP_CLAUSE_DECL (c));
10212 OMP_CLAUSE_REDUCTION_CODE (c) = code;
10213 if (code == ERROR_MARK
10214 || !(INTEGRAL_TYPE_P (type)
10215 || TREE_CODE (type) == REAL_TYPE
10216 || TREE_CODE (type) == COMPLEX_TYPE))
10217 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c)
10218 = c_omp_reduction_lookup (reduc_id,
10219 TYPE_MAIN_VARIANT (type));
10220 }
953ff289
DN
10221
10222 list = nl;
10223 }
10224 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10225 }
10226 return list;
10227}
10228
10229/* OpenMP 2.5:
10230 schedule ( schedule-kind )
10231 schedule ( schedule-kind , expression )
10232
10233 schedule-kind:
a68ab351 10234 static | dynamic | guided | runtime | auto
953ff289
DN
10235*/
10236
10237static tree
10238c_parser_omp_clause_schedule (c_parser *parser, tree list)
10239{
10240 tree c, t;
c2255bc4 10241 location_t loc = c_parser_peek_token (parser)->location;
953ff289
DN
10242
10243 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10244 return list;
10245
c2255bc4 10246 c = build_omp_clause (loc, OMP_CLAUSE_SCHEDULE);
953ff289
DN
10247
10248 if (c_parser_next_token_is (parser, CPP_NAME))
10249 {
10250 tree kind = c_parser_peek_token (parser)->value;
10251 const char *p = IDENTIFIER_POINTER (kind);
10252
10253 switch (p[0])
10254 {
10255 case 'd':
10256 if (strcmp ("dynamic", p) != 0)
10257 goto invalid_kind;
10258 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
10259 break;
10260
10261 case 'g':
10262 if (strcmp ("guided", p) != 0)
10263 goto invalid_kind;
10264 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
10265 break;
10266
10267 case 'r':
10268 if (strcmp ("runtime", p) != 0)
10269 goto invalid_kind;
10270 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
10271 break;
10272
10273 default:
10274 goto invalid_kind;
10275 }
10276 }
10277 else if (c_parser_next_token_is_keyword (parser, RID_STATIC))
10278 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
a68ab351
JJ
10279 else if (c_parser_next_token_is_keyword (parser, RID_AUTO))
10280 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
953ff289
DN
10281 else
10282 goto invalid_kind;
10283
10284 c_parser_consume_token (parser);
10285 if (c_parser_next_token_is (parser, CPP_COMMA))
10286 {
c7412148 10287 location_t here;
953ff289
DN
10288 c_parser_consume_token (parser);
10289
c7412148 10290 here = c_parser_peek_token (parser)->location;
953ff289 10291 t = c_parser_expr_no_commas (parser, NULL).value;
7d1362bc 10292 mark_exp_read (t);
928c19bb 10293 t = c_fully_fold (t, false, NULL);
953ff289
DN
10294
10295 if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
3ba09659
AH
10296 error_at (here, "schedule %<runtime%> does not take "
10297 "a %<chunk_size%> parameter");
a68ab351 10298 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
3ba09659
AH
10299 error_at (here,
10300 "schedule %<auto%> does not take "
10301 "a %<chunk_size%> parameter");
953ff289
DN
10302 else if (TREE_CODE (TREE_TYPE (t)) == INTEGER_TYPE)
10303 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
10304 else
10305 c_parser_error (parser, "expected integer expression");
10306
10307 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10308 }
10309 else
10310 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
10311 "expected %<,%> or %<)%>");
10312
10313 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule");
10314 OMP_CLAUSE_CHAIN (c) = list;
10315 return c;
10316
10317 invalid_kind:
10318 c_parser_error (parser, "invalid schedule kind");
10319 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 0);
10320 return list;
10321}
10322
10323/* OpenMP 2.5:
10324 shared ( variable-list ) */
10325
10326static tree
10327c_parser_omp_clause_shared (c_parser *parser, tree list)
10328{
10329 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_SHARED, list);
10330}
10331
a68ab351
JJ
10332/* OpenMP 3.0:
10333 untied */
10334
10335static tree
10336c_parser_omp_clause_untied (c_parser *parser ATTRIBUTE_UNUSED, tree list)
10337{
10338 tree c;
10339
10340 /* FIXME: Should we allow duplicates? */
10341 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied");
10342
c2255bc4
AH
10343 c = build_omp_clause (c_parser_peek_token (parser)->location,
10344 OMP_CLAUSE_UNTIED);
a68ab351 10345 OMP_CLAUSE_CHAIN (c) = list;
c2255bc4 10346
a68ab351
JJ
10347 return c;
10348}
10349
acf0174b
JJ
10350/* OpenMP 4.0:
10351 inbranch
10352 notinbranch */
953ff289
DN
10353
10354static tree
acf0174b
JJ
10355c_parser_omp_clause_branch (c_parser *parser ATTRIBUTE_UNUSED,
10356 enum omp_clause_code code, tree list)
953ff289 10357{
acf0174b 10358 check_no_duplicate_clause (list, code, omp_clause_code_name[code]);
953ff289 10359
acf0174b
JJ
10360 tree c = build_omp_clause (c_parser_peek_token (parser)->location, code);
10361 OMP_CLAUSE_CHAIN (c) = list;
953ff289 10362
acf0174b
JJ
10363 return c;
10364}
8085ca15 10365
acf0174b
JJ
10366/* OpenMP 4.0:
10367 parallel
10368 for
10369 sections
10370 taskgroup */
8085ca15 10371
acf0174b
JJ
10372static tree
10373c_parser_omp_clause_cancelkind (c_parser *parser ATTRIBUTE_UNUSED,
10374 enum omp_clause_code code, tree list)
10375{
10376 tree c = build_omp_clause (c_parser_peek_token (parser)->location, code);
10377 OMP_CLAUSE_CHAIN (c) = list;
10378
10379 return c;
10380}
10381
10382/* OpenMP 4.0:
10383 num_teams ( expression ) */
10384
10385static tree
10386c_parser_omp_clause_num_teams (c_parser *parser, tree list)
10387{
10388 location_t num_teams_loc = c_parser_peek_token (parser)->location;
10389 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10390 {
10391 location_t expr_loc = c_parser_peek_token (parser)->location;
10392 tree c, t = c_parser_expression (parser).value;
10393 mark_exp_read (t);
10394 t = c_fully_fold (t, false, NULL);
10395
10396 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10397
10398 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
953ff289 10399 {
acf0174b
JJ
10400 c_parser_error (parser, "expected integer expression");
10401 return list;
953ff289
DN
10402 }
10403
acf0174b
JJ
10404 /* Attempt to statically determine when the number isn't positive. */
10405 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
10406 build_int_cst (TREE_TYPE (t), 0));
10407 if (CAN_HAVE_LOCATION_P (c))
10408 SET_EXPR_LOCATION (c, expr_loc);
10409 if (c == boolean_true_node)
953ff289 10410 {
acf0174b
JJ
10411 warning_at (expr_loc, 0, "%<num_teams%> value must be positive");
10412 t = integer_one_node;
953ff289 10413 }
953ff289 10414
acf0174b 10415 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TEAMS, "num_teams");
953ff289 10416
acf0174b
JJ
10417 c = build_omp_clause (num_teams_loc, OMP_CLAUSE_NUM_TEAMS);
10418 OMP_CLAUSE_NUM_TEAMS_EXPR (c) = t;
10419 OMP_CLAUSE_CHAIN (c) = list;
10420 list = c;
10421 }
953ff289 10422
acf0174b
JJ
10423 return list;
10424}
953ff289 10425
acf0174b
JJ
10426/* OpenMP 4.0:
10427 thread_limit ( expression ) */
953ff289
DN
10428
10429static tree
acf0174b 10430c_parser_omp_clause_thread_limit (c_parser *parser, tree list)
953ff289 10431{
acf0174b
JJ
10432 location_t num_teams_loc = c_parser_peek_token (parser)->location;
10433 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10434 {
10435 location_t expr_loc = c_parser_peek_token (parser)->location;
10436 tree c, t = c_parser_expression (parser).value;
10437 mark_exp_read (t);
10438 t = c_fully_fold (t, false, NULL);
953ff289 10439
acf0174b 10440 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
953ff289 10441
acf0174b
JJ
10442 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
10443 {
10444 c_parser_error (parser, "expected integer expression");
10445 return list;
10446 }
c2255bc4 10447
acf0174b
JJ
10448 /* Attempt to statically determine when the number isn't positive. */
10449 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
10450 build_int_cst (TREE_TYPE (t), 0));
10451 if (CAN_HAVE_LOCATION_P (c))
10452 SET_EXPR_LOCATION (c, expr_loc);
10453 if (c == boolean_true_node)
10454 {
10455 warning_at (expr_loc, 0, "%<thread_limit%> value must be positive");
10456 t = integer_one_node;
10457 }
20906c66 10458
acf0174b
JJ
10459 check_no_duplicate_clause (list, OMP_CLAUSE_THREAD_LIMIT,
10460 "thread_limit");
20906c66 10461
acf0174b
JJ
10462 c = build_omp_clause (num_teams_loc, OMP_CLAUSE_THREAD_LIMIT);
10463 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
10464 OMP_CLAUSE_CHAIN (c) = list;
10465 list = c;
10466 }
20906c66 10467
acf0174b
JJ
10468 return list;
10469}
20906c66 10470
acf0174b
JJ
10471/* OpenMP 4.0:
10472 aligned ( variable-list )
10473 aligned ( variable-list : constant-expression ) */
20906c66 10474
acf0174b
JJ
10475static tree
10476c_parser_omp_clause_aligned (c_parser *parser, tree list)
10477{
10478 location_t clause_loc = c_parser_peek_token (parser)->location;
10479 tree nl, c;
20906c66 10480
acf0174b
JJ
10481 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10482 return list;
20906c66 10483
acf0174b
JJ
10484 nl = c_parser_omp_variable_list (parser, clause_loc,
10485 OMP_CLAUSE_ALIGNED, list);
20906c66 10486
acf0174b
JJ
10487 if (c_parser_next_token_is (parser, CPP_COLON))
10488 {
10489 c_parser_consume_token (parser);
10490 tree alignment = c_parser_expr_no_commas (parser, NULL).value;
10491 mark_exp_read (alignment);
10492 alignment = c_fully_fold (alignment, false, NULL);
10493 if (!INTEGRAL_TYPE_P (TREE_TYPE (alignment))
10494 && TREE_CODE (alignment) != INTEGER_CST
10495 && tree_int_cst_sgn (alignment) != 1)
10496 {
10497 error_at (clause_loc, "%<aligned%> clause alignment expression must "
10498 "be positive constant integer expression");
10499 alignment = NULL_TREE;
10500 }
953ff289 10501
acf0174b
JJ
10502 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
10503 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = alignment;
10504 }
10505
10506 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10507 return nl;
10508}
10509
10510/* OpenMP 4.0:
10511 linear ( variable-list )
10512 linear ( variable-list : expression ) */
10513
10514static tree
41958c28 10515c_parser_omp_clause_linear (c_parser *parser, tree list, bool is_cilk_simd_fn)
953ff289 10516{
acf0174b
JJ
10517 location_t clause_loc = c_parser_peek_token (parser)->location;
10518 tree nl, c, step;
10519
10520 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10521 return list;
10522
10523 nl = c_parser_omp_variable_list (parser, clause_loc,
10524 OMP_CLAUSE_LINEAR, list);
10525
10526 if (c_parser_next_token_is (parser, CPP_COLON))
10527 {
10528 c_parser_consume_token (parser);
10529 step = c_parser_expression (parser).value;
10530 mark_exp_read (step);
10531 step = c_fully_fold (step, false, NULL);
41958c28
BI
10532 if (is_cilk_simd_fn && TREE_CODE (step) == PARM_DECL)
10533 {
10534 sorry ("using parameters for %<linear%> step is not supported yet");
10535 step = integer_one_node;
10536 }
acf0174b
JJ
10537 if (!INTEGRAL_TYPE_P (TREE_TYPE (step)))
10538 {
10539 error_at (clause_loc, "%<linear%> clause step expression must "
10540 "be integral");
10541 step = integer_one_node;
10542 }
10543
10544 }
10545 else
10546 step = integer_one_node;
10547
10548 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
10549 {
10550 OMP_CLAUSE_LINEAR_STEP (c) = step;
10551 }
10552
10553 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10554 return nl;
10555}
10556
10557/* OpenMP 4.0:
10558 safelen ( constant-expression ) */
10559
10560static tree
10561c_parser_omp_clause_safelen (c_parser *parser, tree list)
10562{
10563 location_t clause_loc = c_parser_peek_token (parser)->location;
10564 tree c, t;
10565
10566 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10567 return list;
10568
10569 t = c_parser_expr_no_commas (parser, NULL).value;
10570 mark_exp_read (t);
10571 t = c_fully_fold (t, false, NULL);
10572 if (!INTEGRAL_TYPE_P (TREE_TYPE (t))
10573 && TREE_CODE (t) != INTEGER_CST
10574 && tree_int_cst_sgn (t) != 1)
10575 {
10576 error_at (clause_loc, "%<safelen%> clause expression must "
10577 "be positive constant integer expression");
10578 t = NULL_TREE;
10579 }
10580
10581 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10582 if (t == NULL_TREE || t == error_mark_node)
10583 return list;
10584
10585 check_no_duplicate_clause (list, OMP_CLAUSE_SAFELEN, "safelen");
10586
10587 c = build_omp_clause (clause_loc, OMP_CLAUSE_SAFELEN);
10588 OMP_CLAUSE_SAFELEN_EXPR (c) = t;
10589 OMP_CLAUSE_CHAIN (c) = list;
10590 return c;
10591}
10592
10593/* OpenMP 4.0:
10594 simdlen ( constant-expression ) */
10595
10596static tree
10597c_parser_omp_clause_simdlen (c_parser *parser, tree list)
10598{
10599 location_t clause_loc = c_parser_peek_token (parser)->location;
10600 tree c, t;
10601
10602 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10603 return list;
10604
10605 t = c_parser_expr_no_commas (parser, NULL).value;
10606 mark_exp_read (t);
10607 t = c_fully_fold (t, false, NULL);
10608 if (!INTEGRAL_TYPE_P (TREE_TYPE (t))
10609 && TREE_CODE (t) != INTEGER_CST
10610 && tree_int_cst_sgn (t) != 1)
10611 {
10612 error_at (clause_loc, "%<simdlen%> clause expression must "
10613 "be positive constant integer expression");
10614 t = NULL_TREE;
10615 }
10616
10617 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10618 if (t == NULL_TREE || t == error_mark_node)
10619 return list;
10620
10621 check_no_duplicate_clause (list, OMP_CLAUSE_SIMDLEN, "simdlen");
10622
10623 c = build_omp_clause (clause_loc, OMP_CLAUSE_SIMDLEN);
10624 OMP_CLAUSE_SIMDLEN_EXPR (c) = t;
10625 OMP_CLAUSE_CHAIN (c) = list;
10626 return c;
10627}
10628
10629/* OpenMP 4.0:
10630 depend ( depend-kind: variable-list )
10631
10632 depend-kind:
10633 in | out | inout */
10634
10635static tree
10636c_parser_omp_clause_depend (c_parser *parser, tree list)
10637{
10638 location_t clause_loc = c_parser_peek_token (parser)->location;
10639 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_INOUT;
10640 tree nl, c;
10641
10642 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10643 return list;
953ff289 10644
20906c66
JJ
10645 if (c_parser_next_token_is (parser, CPP_NAME))
10646 {
10647 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
acf0174b
JJ
10648 if (strcmp ("in", p) == 0)
10649 kind = OMP_CLAUSE_DEPEND_IN;
10650 else if (strcmp ("inout", p) == 0)
10651 kind = OMP_CLAUSE_DEPEND_INOUT;
10652 else if (strcmp ("out", p) == 0)
10653 kind = OMP_CLAUSE_DEPEND_OUT;
20906c66 10654 else
acf0174b 10655 goto invalid_kind;
20906c66 10656 }
acf0174b
JJ
10657 else
10658 goto invalid_kind;
953ff289 10659
acf0174b
JJ
10660 c_parser_consume_token (parser);
10661 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
10662 goto resync_fail;
10663
10664 nl = c_parser_omp_variable_list (parser, clause_loc,
10665 OMP_CLAUSE_DEPEND, list);
10666
10667 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
10668 OMP_CLAUSE_DEPEND_KIND (c) = kind;
10669
10670 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10671 return nl;
10672
10673 invalid_kind:
10674 c_parser_error (parser, "invalid depend kind");
10675 resync_fail:
10676 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10677 return list;
10678}
10679
10680/* OpenMP 4.0:
10681 map ( map-kind: variable-list )
10682 map ( variable-list )
10683
10684 map-kind:
10685 alloc | to | from | tofrom */
10686
10687static tree
10688c_parser_omp_clause_map (c_parser *parser, tree list)
10689{
10690 location_t clause_loc = c_parser_peek_token (parser)->location;
10691 enum omp_clause_map_kind kind = OMP_CLAUSE_MAP_TOFROM;
10692 tree nl, c;
10693
10694 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10695 return list;
10696
10697 if (c_parser_next_token_is (parser, CPP_NAME)
10698 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
20906c66 10699 {
acf0174b
JJ
10700 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
10701 if (strcmp ("alloc", p) == 0)
10702 kind = OMP_CLAUSE_MAP_ALLOC;
10703 else if (strcmp ("to", p) == 0)
10704 kind = OMP_CLAUSE_MAP_TO;
10705 else if (strcmp ("from", p) == 0)
10706 kind = OMP_CLAUSE_MAP_FROM;
10707 else if (strcmp ("tofrom", p) == 0)
10708 kind = OMP_CLAUSE_MAP_TOFROM;
20906c66
JJ
10709 else
10710 {
acf0174b
JJ
10711 c_parser_error (parser, "invalid map kind");
10712 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
10713 "expected %<)%>");
10714 return list;
20906c66 10715 }
acf0174b
JJ
10716 c_parser_consume_token (parser);
10717 c_parser_consume_token (parser);
20906c66
JJ
10718 }
10719
acf0174b
JJ
10720 nl = c_parser_omp_variable_list (parser, clause_loc, OMP_CLAUSE_MAP, list);
10721
10722 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
10723 OMP_CLAUSE_MAP_KIND (c) = kind;
10724
10725 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10726 return nl;
10727}
10728
10729/* OpenMP 4.0:
10730 device ( expression ) */
10731
10732static tree
10733c_parser_omp_clause_device (c_parser *parser, tree list)
10734{
10735 location_t clause_loc = c_parser_peek_token (parser)->location;
10736 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
953ff289 10737 {
acf0174b
JJ
10738 tree c, t = c_parser_expr_no_commas (parser, NULL).value;
10739 mark_exp_read (t);
10740 t = c_fully_fold (t, false, NULL);
10741
10742 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10743
10744 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
20906c66 10745 {
acf0174b
JJ
10746 c_parser_error (parser, "expected integer expression");
10747 return list;
20906c66 10748 }
953ff289 10749
acf0174b 10750 check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE, "device");
953ff289 10751
acf0174b
JJ
10752 c = build_omp_clause (clause_loc, OMP_CLAUSE_DEVICE);
10753 OMP_CLAUSE_DEVICE_ID (c) = t;
10754 OMP_CLAUSE_CHAIN (c) = list;
10755 list = c;
10756 }
953ff289 10757
acf0174b
JJ
10758 return list;
10759}
10760
10761/* OpenMP 4.0:
10762 dist_schedule ( static )
10763 dist_schedule ( static , expression ) */
10764
10765static tree
10766c_parser_omp_clause_dist_schedule (c_parser *parser, tree list)
10767{
10768 tree c, t = NULL_TREE;
10769 location_t loc = c_parser_peek_token (parser)->location;
10770
10771 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10772 return list;
10773
10774 if (!c_parser_next_token_is_keyword (parser, RID_STATIC))
10775 {
10776 c_parser_error (parser, "invalid dist_schedule kind");
10777 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
10778 "expected %<)%>");
10779 return list;
10780 }
10781
10782 c_parser_consume_token (parser);
10783 if (c_parser_next_token_is (parser, CPP_COMMA))
10784 {
10785 c_parser_consume_token (parser);
10786
10787 t = c_parser_expr_no_commas (parser, NULL).value;
10788 mark_exp_read (t);
10789 t = c_fully_fold (t, false, NULL);
10790 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10791 }
10792 else
10793 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
10794 "expected %<,%> or %<)%>");
10795
10796 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule");
10797 if (t == error_mark_node)
10798 return list;
10799
10800 c = build_omp_clause (loc, OMP_CLAUSE_DIST_SCHEDULE);
10801 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
10802 OMP_CLAUSE_CHAIN (c) = list;
10803 return c;
10804}
10805
10806/* OpenMP 4.0:
10807 proc_bind ( proc-bind-kind )
10808
10809 proc-bind-kind:
10810 master | close | spread */
10811
10812static tree
10813c_parser_omp_clause_proc_bind (c_parser *parser, tree list)
10814{
10815 location_t clause_loc = c_parser_peek_token (parser)->location;
10816 enum omp_clause_proc_bind_kind kind;
10817 tree c;
10818
10819 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10820 return list;
10821
10822 if (c_parser_next_token_is (parser, CPP_NAME))
10823 {
10824 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
10825 if (strcmp ("master", p) == 0)
10826 kind = OMP_CLAUSE_PROC_BIND_MASTER;
10827 else if (strcmp ("close", p) == 0)
10828 kind = OMP_CLAUSE_PROC_BIND_CLOSE;
10829 else if (strcmp ("spread", p) == 0)
10830 kind = OMP_CLAUSE_PROC_BIND_SPREAD;
10831 else
10832 goto invalid_kind;
10833 }
10834 else
10835 goto invalid_kind;
10836
10837 c_parser_consume_token (parser);
10838 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10839 c = build_omp_clause (clause_loc, OMP_CLAUSE_PROC_BIND);
10840 OMP_CLAUSE_PROC_BIND_KIND (c) = kind;
10841 OMP_CLAUSE_CHAIN (c) = list;
10842 return c;
10843
10844 invalid_kind:
10845 c_parser_error (parser, "invalid proc_bind kind");
10846 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10847 return list;
10848}
10849
10850/* OpenMP 4.0:
10851 to ( variable-list ) */
10852
10853static tree
10854c_parser_omp_clause_to (c_parser *parser, tree list)
10855{
10856 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_TO, list);
10857}
10858
10859/* OpenMP 4.0:
10860 from ( variable-list ) */
10861
10862static tree
10863c_parser_omp_clause_from (c_parser *parser, tree list)
10864{
10865 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_FROM, list);
10866}
10867
10868/* OpenMP 4.0:
10869 uniform ( variable-list ) */
10870
10871static tree
10872c_parser_omp_clause_uniform (c_parser *parser, tree list)
10873{
10874 /* The clauses location. */
10875 location_t loc = c_parser_peek_token (parser)->location;
10876
10877 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10878 {
10879 list = c_parser_omp_variable_list (parser, loc, OMP_CLAUSE_UNIFORM,
10880 list);
10881 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10882 }
10883 return list;
10884}
10885
10886/* Parse all OpenMP clauses. The set clauses allowed by the directive
10887 is a bitmask in MASK. Return the list of clauses found; the result
10888 of clause default goes in *pdefault. */
10889
10890static tree
10891c_parser_omp_all_clauses (c_parser *parser, omp_clause_mask mask,
10892 const char *where, bool finish_p = true)
10893{
10894 tree clauses = NULL;
41958c28 10895 bool first = true, cilk_simd_fn = false;
acf0174b
JJ
10896
10897 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
10898 {
10899 location_t here;
10900 pragma_omp_clause c_kind;
10901 const char *c_name;
10902 tree prev = clauses;
10903
10904 if (!first && c_parser_next_token_is (parser, CPP_COMMA))
10905 c_parser_consume_token (parser);
10906
10907 here = c_parser_peek_token (parser)->location;
10908 c_kind = c_parser_omp_clause_name (parser);
10909
10910 switch (c_kind)
953ff289 10911 {
acf0174b
JJ
10912 case PRAGMA_OMP_CLAUSE_COLLAPSE:
10913 clauses = c_parser_omp_clause_collapse (parser, clauses);
10914 c_name = "collapse";
953ff289 10915 break;
acf0174b
JJ
10916 case PRAGMA_OMP_CLAUSE_COPYIN:
10917 clauses = c_parser_omp_clause_copyin (parser, clauses);
10918 c_name = "copyin";
953ff289 10919 break;
acf0174b
JJ
10920 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
10921 clauses = c_parser_omp_clause_copyprivate (parser, clauses);
10922 c_name = "copyprivate";
953ff289 10923 break;
acf0174b
JJ
10924 case PRAGMA_OMP_CLAUSE_DEFAULT:
10925 clauses = c_parser_omp_clause_default (parser, clauses);
10926 c_name = "default";
953ff289 10927 break;
acf0174b
JJ
10928 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
10929 clauses = c_parser_omp_clause_firstprivate (parser, clauses);
10930 c_name = "firstprivate";
953ff289 10931 break;
acf0174b
JJ
10932 case PRAGMA_OMP_CLAUSE_FINAL:
10933 clauses = c_parser_omp_clause_final (parser, clauses);
10934 c_name = "final";
953ff289 10935 break;
acf0174b
JJ
10936 case PRAGMA_OMP_CLAUSE_IF:
10937 clauses = c_parser_omp_clause_if (parser, clauses);
10938 c_name = "if";
953ff289 10939 break;
acf0174b
JJ
10940 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
10941 clauses = c_parser_omp_clause_lastprivate (parser, clauses);
10942 c_name = "lastprivate";
953ff289 10943 break;
acf0174b
JJ
10944 case PRAGMA_OMP_CLAUSE_MERGEABLE:
10945 clauses = c_parser_omp_clause_mergeable (parser, clauses);
10946 c_name = "mergeable";
953ff289 10947 break;
acf0174b
JJ
10948 case PRAGMA_OMP_CLAUSE_NOWAIT:
10949 clauses = c_parser_omp_clause_nowait (parser, clauses);
10950 c_name = "nowait";
10951 break;
10952 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
10953 clauses = c_parser_omp_clause_num_threads (parser, clauses);
10954 c_name = "num_threads";
10955 break;
10956 case PRAGMA_OMP_CLAUSE_ORDERED:
10957 clauses = c_parser_omp_clause_ordered (parser, clauses);
10958 c_name = "ordered";
10959 break;
10960 case PRAGMA_OMP_CLAUSE_PRIVATE:
10961 clauses = c_parser_omp_clause_private (parser, clauses);
10962 c_name = "private";
10963 break;
10964 case PRAGMA_OMP_CLAUSE_REDUCTION:
10965 clauses = c_parser_omp_clause_reduction (parser, clauses);
10966 c_name = "reduction";
10967 break;
10968 case PRAGMA_OMP_CLAUSE_SCHEDULE:
10969 clauses = c_parser_omp_clause_schedule (parser, clauses);
10970 c_name = "schedule";
10971 break;
10972 case PRAGMA_OMP_CLAUSE_SHARED:
10973 clauses = c_parser_omp_clause_shared (parser, clauses);
10974 c_name = "shared";
10975 break;
10976 case PRAGMA_OMP_CLAUSE_UNTIED:
10977 clauses = c_parser_omp_clause_untied (parser, clauses);
10978 c_name = "untied";
10979 break;
10980 case PRAGMA_OMP_CLAUSE_INBRANCH:
41958c28 10981 case PRAGMA_CILK_CLAUSE_MASK:
acf0174b
JJ
10982 clauses = c_parser_omp_clause_branch (parser, OMP_CLAUSE_INBRANCH,
10983 clauses);
10984 c_name = "inbranch";
10985 break;
10986 case PRAGMA_OMP_CLAUSE_NOTINBRANCH:
41958c28 10987 case PRAGMA_CILK_CLAUSE_NOMASK:
acf0174b
JJ
10988 clauses = c_parser_omp_clause_branch (parser, OMP_CLAUSE_NOTINBRANCH,
10989 clauses);
10990 c_name = "notinbranch";
10991 break;
10992 case PRAGMA_OMP_CLAUSE_PARALLEL:
10993 clauses
10994 = c_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_PARALLEL,
10995 clauses);
10996 c_name = "parallel";
10997 if (!first)
20906c66 10998 {
acf0174b
JJ
10999 clause_not_first:
11000 error_at (here, "%qs must be the first clause of %qs",
11001 c_name, where);
11002 clauses = prev;
11003 }
11004 break;
11005 case PRAGMA_OMP_CLAUSE_FOR:
11006 clauses
11007 = c_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_FOR,
11008 clauses);
11009 c_name = "for";
11010 if (!first)
11011 goto clause_not_first;
11012 break;
11013 case PRAGMA_OMP_CLAUSE_SECTIONS:
11014 clauses
11015 = c_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_SECTIONS,
11016 clauses);
11017 c_name = "sections";
11018 if (!first)
11019 goto clause_not_first;
11020 break;
11021 case PRAGMA_OMP_CLAUSE_TASKGROUP:
11022 clauses
11023 = c_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_TASKGROUP,
11024 clauses);
11025 c_name = "taskgroup";
11026 if (!first)
11027 goto clause_not_first;
11028 break;
11029 case PRAGMA_OMP_CLAUSE_TO:
11030 clauses = c_parser_omp_clause_to (parser, clauses);
11031 c_name = "to";
11032 break;
11033 case PRAGMA_OMP_CLAUSE_FROM:
11034 clauses = c_parser_omp_clause_from (parser, clauses);
11035 c_name = "from";
11036 break;
11037 case PRAGMA_OMP_CLAUSE_UNIFORM:
11038 clauses = c_parser_omp_clause_uniform (parser, clauses);
11039 c_name = "uniform";
11040 break;
11041 case PRAGMA_OMP_CLAUSE_NUM_TEAMS:
11042 clauses = c_parser_omp_clause_num_teams (parser, clauses);
11043 c_name = "num_teams";
11044 break;
11045 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT:
11046 clauses = c_parser_omp_clause_thread_limit (parser, clauses);
11047 c_name = "thread_limit";
11048 break;
11049 case PRAGMA_OMP_CLAUSE_ALIGNED:
11050 clauses = c_parser_omp_clause_aligned (parser, clauses);
11051 c_name = "aligned";
11052 break;
41958c28
BI
11053 case PRAGMA_OMP_CLAUSE_LINEAR:
11054 if (((mask >> PRAGMA_CILK_CLAUSE_VECTORLENGTH) & 1) != 0)
11055 cilk_simd_fn = true;
11056 clauses = c_parser_omp_clause_linear (parser, clauses, cilk_simd_fn);
acf0174b
JJ
11057 c_name = "linear";
11058 break;
11059 case PRAGMA_OMP_CLAUSE_DEPEND:
11060 clauses = c_parser_omp_clause_depend (parser, clauses);
11061 c_name = "depend";
11062 break;
11063 case PRAGMA_OMP_CLAUSE_MAP:
11064 clauses = c_parser_omp_clause_map (parser, clauses);
11065 c_name = "map";
11066 break;
11067 case PRAGMA_OMP_CLAUSE_DEVICE:
11068 clauses = c_parser_omp_clause_device (parser, clauses);
11069 c_name = "device";
11070 break;
11071 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE:
11072 clauses = c_parser_omp_clause_dist_schedule (parser, clauses);
11073 c_name = "dist_schedule";
11074 break;
11075 case PRAGMA_OMP_CLAUSE_PROC_BIND:
11076 clauses = c_parser_omp_clause_proc_bind (parser, clauses);
11077 c_name = "proc_bind";
11078 break;
11079 case PRAGMA_OMP_CLAUSE_SAFELEN:
11080 clauses = c_parser_omp_clause_safelen (parser, clauses);
11081 c_name = "safelen";
11082 break;
41958c28
BI
11083 case PRAGMA_CILK_CLAUSE_VECTORLENGTH:
11084 clauses = c_parser_cilk_clause_vectorlength (parser, clauses, true);
11085 c_name = "simdlen";
11086 break;
acf0174b
JJ
11087 case PRAGMA_OMP_CLAUSE_SIMDLEN:
11088 clauses = c_parser_omp_clause_simdlen (parser, clauses);
11089 c_name = "simdlen";
11090 break;
953ff289 11091 default:
acf0174b 11092 c_parser_error (parser, "expected %<#pragma omp%> clause");
953ff289
DN
11093 goto saw_error;
11094 }
11095
acf0174b
JJ
11096 first = false;
11097
11098 if (((mask >> c_kind) & 1) == 0 && !parser->error)
11099 {
11100 /* Remove the invalid clause(s) from the list to avoid
11101 confusing the rest of the compiler. */
11102 clauses = prev;
11103 error_at (here, "%qs is not valid for %qs", c_name, where);
11104 }
11105 }
11106
11107 saw_error:
11108 c_parser_skip_to_pragma_eol (parser);
11109
11110 if (finish_p)
11111 return c_finish_omp_clauses (clauses);
11112
11113 return clauses;
11114}
11115
11116/* OpenMP 2.5:
11117 structured-block:
11118 statement
11119
11120 In practice, we're also interested in adding the statement to an
11121 outer node. So it is convenient if we work around the fact that
11122 c_parser_statement calls add_stmt. */
11123
11124static tree
11125c_parser_omp_structured_block (c_parser *parser)
11126{
11127 tree stmt = push_stmt_list ();
11128 c_parser_statement (parser);
11129 return pop_stmt_list (stmt);
11130}
11131
11132/* OpenMP 2.5:
11133 # pragma omp atomic new-line
11134 expression-stmt
11135
11136 expression-stmt:
11137 x binop= expr | x++ | ++x | x-- | --x
11138 binop:
11139 +, *, -, /, &, ^, |, <<, >>
11140
11141 where x is an lvalue expression with scalar type.
11142
11143 OpenMP 3.1:
11144 # pragma omp atomic new-line
11145 update-stmt
11146
11147 # pragma omp atomic read new-line
11148 read-stmt
11149
11150 # pragma omp atomic write new-line
11151 write-stmt
11152
11153 # pragma omp atomic update new-line
11154 update-stmt
11155
11156 # pragma omp atomic capture new-line
11157 capture-stmt
11158
11159 # pragma omp atomic capture new-line
11160 capture-block
11161
11162 read-stmt:
11163 v = x
11164 write-stmt:
11165 x = expr
11166 update-stmt:
11167 expression-stmt | x = x binop expr
11168 capture-stmt:
11169 v = expression-stmt
11170 capture-block:
11171 { v = x; update-stmt; } | { update-stmt; v = x; }
11172
11173 OpenMP 4.0:
11174 update-stmt:
11175 expression-stmt | x = x binop expr | x = expr binop x
11176 capture-stmt:
11177 v = update-stmt
11178 capture-block:
11179 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
11180
11181 where x and v are lvalue expressions with scalar type.
11182
11183 LOC is the location of the #pragma token. */
11184
11185static void
11186c_parser_omp_atomic (location_t loc, c_parser *parser)
11187{
11188 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE;
11189 tree lhs1 = NULL_TREE, rhs1 = NULL_TREE;
11190 tree stmt, orig_lhs, unfolded_lhs = NULL_TREE, unfolded_lhs1 = NULL_TREE;
11191 enum tree_code code = OMP_ATOMIC, opcode = NOP_EXPR;
11192 struct c_expr expr;
11193 location_t eloc;
11194 bool structured_block = false;
11195 bool swapped = false;
11196 bool seq_cst = false;
11197
11198 if (c_parser_next_token_is (parser, CPP_NAME))
11199 {
11200 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
11201
11202 if (!strcmp (p, "read"))
11203 code = OMP_ATOMIC_READ;
11204 else if (!strcmp (p, "write"))
11205 code = NOP_EXPR;
11206 else if (!strcmp (p, "update"))
11207 code = OMP_ATOMIC;
11208 else if (!strcmp (p, "capture"))
11209 code = OMP_ATOMIC_CAPTURE_NEW;
11210 else
11211 p = NULL;
11212 if (p)
11213 c_parser_consume_token (parser);
11214 }
11215 if (c_parser_next_token_is (parser, CPP_NAME))
11216 {
11217 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
11218 if (!strcmp (p, "seq_cst"))
11219 {
11220 seq_cst = true;
11221 c_parser_consume_token (parser);
11222 }
11223 }
11224 c_parser_skip_to_pragma_eol (parser);
11225
11226 switch (code)
11227 {
11228 case OMP_ATOMIC_READ:
11229 case NOP_EXPR: /* atomic write */
11230 v = c_parser_unary_expression (parser).value;
11231 v = c_fully_fold (v, false, NULL);
11232 if (v == error_mark_node)
11233 goto saw_error;
11234 loc = c_parser_peek_token (parser)->location;
11235 if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
11236 goto saw_error;
11237 if (code == NOP_EXPR)
11238 lhs = c_parser_expression (parser).value;
11239 else
11240 lhs = c_parser_unary_expression (parser).value;
11241 lhs = c_fully_fold (lhs, false, NULL);
11242 if (lhs == error_mark_node)
11243 goto saw_error;
11244 if (code == NOP_EXPR)
11245 {
11246 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
11247 opcode. */
11248 code = OMP_ATOMIC;
11249 rhs = lhs;
11250 lhs = v;
11251 v = NULL_TREE;
11252 }
11253 goto done;
11254 case OMP_ATOMIC_CAPTURE_NEW:
11255 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
11256 {
11257 c_parser_consume_token (parser);
11258 structured_block = true;
11259 }
11260 else
11261 {
11262 v = c_parser_unary_expression (parser).value;
11263 v = c_fully_fold (v, false, NULL);
11264 if (v == error_mark_node)
11265 goto saw_error;
11266 if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
11267 goto saw_error;
11268 }
11269 break;
11270 default:
11271 break;
11272 }
11273
11274 /* For structured_block case we don't know yet whether
11275 old or new x should be captured. */
11276restart:
11277 eloc = c_parser_peek_token (parser)->location;
11278 expr = c_parser_unary_expression (parser);
11279 lhs = expr.value;
11280 expr = default_function_array_conversion (eloc, expr);
11281 unfolded_lhs = expr.value;
11282 lhs = c_fully_fold (lhs, false, NULL);
11283 orig_lhs = lhs;
11284 switch (TREE_CODE (lhs))
11285 {
11286 case ERROR_MARK:
11287 saw_error:
11288 c_parser_skip_to_end_of_block_or_statement (parser);
11289 if (structured_block)
11290 {
11291 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
11292 c_parser_consume_token (parser);
11293 else if (code == OMP_ATOMIC_CAPTURE_NEW)
11294 {
11295 c_parser_skip_to_end_of_block_or_statement (parser);
11296 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
11297 c_parser_consume_token (parser);
11298 }
11299 }
11300 return;
11301
11302 case POSTINCREMENT_EXPR:
11303 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
11304 code = OMP_ATOMIC_CAPTURE_OLD;
11305 /* FALLTHROUGH */
11306 case PREINCREMENT_EXPR:
11307 lhs = TREE_OPERAND (lhs, 0);
11308 unfolded_lhs = NULL_TREE;
11309 opcode = PLUS_EXPR;
11310 rhs = integer_one_node;
11311 break;
11312
11313 case POSTDECREMENT_EXPR:
11314 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
11315 code = OMP_ATOMIC_CAPTURE_OLD;
11316 /* FALLTHROUGH */
11317 case PREDECREMENT_EXPR:
11318 lhs = TREE_OPERAND (lhs, 0);
11319 unfolded_lhs = NULL_TREE;
11320 opcode = MINUS_EXPR;
11321 rhs = integer_one_node;
11322 break;
11323
11324 case COMPOUND_EXPR:
11325 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
11326 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
11327 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
11328 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
11329 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
11330 (TREE_OPERAND (lhs, 1), 0), 0)))
11331 == BOOLEAN_TYPE)
11332 /* Undo effects of boolean_increment for post {in,de}crement. */
11333 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
11334 /* FALLTHRU */
11335 case MODIFY_EXPR:
11336 if (TREE_CODE (lhs) == MODIFY_EXPR
11337 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
11338 {
11339 /* Undo effects of boolean_increment. */
11340 if (integer_onep (TREE_OPERAND (lhs, 1)))
11341 {
11342 /* This is pre or post increment. */
11343 rhs = TREE_OPERAND (lhs, 1);
11344 lhs = TREE_OPERAND (lhs, 0);
11345 unfolded_lhs = NULL_TREE;
11346 opcode = NOP_EXPR;
11347 if (code == OMP_ATOMIC_CAPTURE_NEW
11348 && !structured_block
11349 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
11350 code = OMP_ATOMIC_CAPTURE_OLD;
11351 break;
11352 }
11353 if (TREE_CODE (TREE_OPERAND (lhs, 1)) == TRUTH_NOT_EXPR
11354 && TREE_OPERAND (lhs, 0)
11355 == TREE_OPERAND (TREE_OPERAND (lhs, 1), 0))
11356 {
11357 /* This is pre or post decrement. */
11358 rhs = TREE_OPERAND (lhs, 1);
11359 lhs = TREE_OPERAND (lhs, 0);
11360 unfolded_lhs = NULL_TREE;
11361 opcode = NOP_EXPR;
11362 if (code == OMP_ATOMIC_CAPTURE_NEW
11363 && !structured_block
11364 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
11365 code = OMP_ATOMIC_CAPTURE_OLD;
11366 break;
11367 }
11368 }
11369 /* FALLTHRU */
11370 default:
11371 switch (c_parser_peek_token (parser)->type)
11372 {
11373 case CPP_MULT_EQ:
11374 opcode = MULT_EXPR;
11375 break;
11376 case CPP_DIV_EQ:
11377 opcode = TRUNC_DIV_EXPR;
11378 break;
11379 case CPP_PLUS_EQ:
11380 opcode = PLUS_EXPR;
11381 break;
11382 case CPP_MINUS_EQ:
11383 opcode = MINUS_EXPR;
11384 break;
11385 case CPP_LSHIFT_EQ:
11386 opcode = LSHIFT_EXPR;
11387 break;
11388 case CPP_RSHIFT_EQ:
11389 opcode = RSHIFT_EXPR;
11390 break;
11391 case CPP_AND_EQ:
11392 opcode = BIT_AND_EXPR;
11393 break;
11394 case CPP_OR_EQ:
11395 opcode = BIT_IOR_EXPR;
11396 break;
11397 case CPP_XOR_EQ:
11398 opcode = BIT_XOR_EXPR;
11399 break;
11400 case CPP_EQ:
11401 c_parser_consume_token (parser);
11402 eloc = c_parser_peek_token (parser)->location;
11403 expr = c_parser_expr_no_commas (parser, NULL, unfolded_lhs);
11404 rhs1 = expr.value;
11405 switch (TREE_CODE (rhs1))
11406 {
11407 case MULT_EXPR:
11408 case TRUNC_DIV_EXPR:
11409 case PLUS_EXPR:
11410 case MINUS_EXPR:
11411 case LSHIFT_EXPR:
11412 case RSHIFT_EXPR:
11413 case BIT_AND_EXPR:
11414 case BIT_IOR_EXPR:
11415 case BIT_XOR_EXPR:
11416 if (c_tree_equal (TREE_OPERAND (rhs1, 0), unfolded_lhs))
11417 {
11418 opcode = TREE_CODE (rhs1);
11419 rhs = c_fully_fold (TREE_OPERAND (rhs1, 1), false, NULL);
11420 rhs1 = c_fully_fold (TREE_OPERAND (rhs1, 0), false, NULL);
11421 goto stmt_done;
11422 }
11423 if (c_tree_equal (TREE_OPERAND (rhs1, 1), unfolded_lhs))
11424 {
11425 opcode = TREE_CODE (rhs1);
11426 rhs = c_fully_fold (TREE_OPERAND (rhs1, 0), false, NULL);
11427 rhs1 = c_fully_fold (TREE_OPERAND (rhs1, 1), false, NULL);
11428 swapped = !commutative_tree_code (opcode);
11429 goto stmt_done;
11430 }
11431 break;
11432 case ERROR_MARK:
11433 goto saw_error;
11434 default:
11435 break;
11436 }
11437 if (c_parser_peek_token (parser)->type == CPP_SEMICOLON)
11438 {
11439 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
11440 {
11441 code = OMP_ATOMIC_CAPTURE_OLD;
11442 v = lhs;
11443 lhs = NULL_TREE;
11444 expr = default_function_array_read_conversion (eloc, expr);
11445 unfolded_lhs1 = expr.value;
11446 lhs1 = c_fully_fold (unfolded_lhs1, false, NULL);
11447 rhs1 = NULL_TREE;
11448 c_parser_consume_token (parser);
11449 goto restart;
11450 }
11451 if (structured_block)
11452 {
11453 opcode = NOP_EXPR;
11454 expr = default_function_array_read_conversion (eloc, expr);
11455 rhs = c_fully_fold (expr.value, false, NULL);
11456 rhs1 = NULL_TREE;
11457 goto stmt_done;
11458 }
11459 }
11460 c_parser_error (parser, "invalid form of %<#pragma omp atomic%>");
11461 goto saw_error;
11462 default:
11463 c_parser_error (parser,
11464 "invalid operator for %<#pragma omp atomic%>");
11465 goto saw_error;
11466 }
11467
11468 /* Arrange to pass the location of the assignment operator to
11469 c_finish_omp_atomic. */
11470 loc = c_parser_peek_token (parser)->location;
11471 c_parser_consume_token (parser);
11472 eloc = c_parser_peek_token (parser)->location;
11473 expr = c_parser_expression (parser);
11474 expr = default_function_array_read_conversion (eloc, expr);
11475 rhs = expr.value;
11476 rhs = c_fully_fold (rhs, false, NULL);
11477 break;
11478 }
11479stmt_done:
11480 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
11481 {
11482 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
11483 goto saw_error;
11484 v = c_parser_unary_expression (parser).value;
11485 v = c_fully_fold (v, false, NULL);
11486 if (v == error_mark_node)
11487 goto saw_error;
11488 if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
11489 goto saw_error;
11490 eloc = c_parser_peek_token (parser)->location;
11491 expr = c_parser_unary_expression (parser);
11492 lhs1 = expr.value;
11493 expr = default_function_array_read_conversion (eloc, expr);
11494 unfolded_lhs1 = expr.value;
11495 lhs1 = c_fully_fold (lhs1, false, NULL);
11496 if (lhs1 == error_mark_node)
11497 goto saw_error;
11498 }
11499 if (structured_block)
11500 {
11501 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
11502 c_parser_require (parser, CPP_CLOSE_BRACE, "expected %<}%>");
11503 }
11504done:
11505 if (unfolded_lhs && unfolded_lhs1
11506 && !c_tree_equal (unfolded_lhs, unfolded_lhs1))
11507 {
11508 error ("%<#pragma omp atomic capture%> uses two different "
11509 "expressions for memory");
11510 stmt = error_mark_node;
11511 }
11512 else
11513 stmt = c_finish_omp_atomic (loc, code, opcode, lhs, rhs, v, lhs1, rhs1,
11514 swapped, seq_cst);
11515 if (stmt != error_mark_node)
11516 add_stmt (stmt);
11517
11518 if (!structured_block)
11519 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
11520}
11521
11522
11523/* OpenMP 2.5:
11524 # pragma omp barrier new-line
11525*/
11526
11527static void
11528c_parser_omp_barrier (c_parser *parser)
11529{
11530 location_t loc = c_parser_peek_token (parser)->location;
11531 c_parser_consume_pragma (parser);
11532 c_parser_skip_to_pragma_eol (parser);
11533
11534 c_finish_omp_barrier (loc);
11535}
11536
11537/* OpenMP 2.5:
11538 # pragma omp critical [(name)] new-line
11539 structured-block
11540
11541 LOC is the location of the #pragma itself. */
11542
11543static tree
11544c_parser_omp_critical (location_t loc, c_parser *parser)
11545{
11546 tree stmt, name = NULL;
11547
11548 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
11549 {
11550 c_parser_consume_token (parser);
11551 if (c_parser_next_token_is (parser, CPP_NAME))
11552 {
11553 name = c_parser_peek_token (parser)->value;
11554 c_parser_consume_token (parser);
11555 c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11556 }
11557 else
11558 c_parser_error (parser, "expected identifier");
11559 }
11560 else if (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
11561 c_parser_error (parser, "expected %<(%> or end of line");
11562 c_parser_skip_to_pragma_eol (parser);
11563
11564 stmt = c_parser_omp_structured_block (parser);
11565 return c_finish_omp_critical (loc, stmt, name);
11566}
11567
11568/* OpenMP 2.5:
11569 # pragma omp flush flush-vars[opt] new-line
11570
11571 flush-vars:
11572 ( variable-list ) */
11573
11574static void
11575c_parser_omp_flush (c_parser *parser)
11576{
11577 location_t loc = c_parser_peek_token (parser)->location;
11578 c_parser_consume_pragma (parser);
11579 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
11580 c_parser_omp_var_list_parens (parser, OMP_CLAUSE_ERROR, NULL);
11581 else if (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
11582 c_parser_error (parser, "expected %<(%> or end of line");
11583 c_parser_skip_to_pragma_eol (parser);
11584
11585 c_finish_omp_flush (loc);
11586}
11587
11588/* Parse the restricted form of the for statement allowed by OpenMP.
11589 The real trick here is to determine the loop control variable early
11590 so that we can push a new decl if necessary to make it private.
11591 LOC is the location of the OMP in "#pragma omp". */
11592
11593static tree
11594c_parser_omp_for_loop (location_t loc, c_parser *parser, enum tree_code code,
11595 tree clauses, tree *cclauses)
11596{
11597 tree decl, cond, incr, save_break, save_cont, body, init, stmt, cl;
11598 tree declv, condv, incrv, initv, ret = NULL;
11599 bool fail = false, open_brace_parsed = false;
11600 int i, collapse = 1, nbraces = 0;
11601 location_t for_loc;
11602 vec<tree, va_gc> *for_block = make_tree_vector ();
11603
11604 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
11605 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
9439e9a1 11606 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl));
acf0174b
JJ
11607
11608 gcc_assert (collapse >= 1);
11609
11610 declv = make_tree_vec (collapse);
11611 initv = make_tree_vec (collapse);
11612 condv = make_tree_vec (collapse);
11613 incrv = make_tree_vec (collapse);
11614
11615 if (!c_parser_next_token_is_keyword (parser, RID_FOR))
11616 {
11617 c_parser_error (parser, "for statement expected");
11618 return NULL;
11619 }
11620 for_loc = c_parser_peek_token (parser)->location;
11621 c_parser_consume_token (parser);
11622
11623 for (i = 0; i < collapse; i++)
11624 {
11625 int bracecount = 0;
11626
11627 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11628 goto pop_scopes;
11629
11630 /* Parse the initialization declaration or expression. */
11631 if (c_parser_next_tokens_start_declaration (parser))
11632 {
11633 if (i > 0)
11634 vec_safe_push (for_block, c_begin_compound_stmt (true));
11635 c_parser_declaration_or_fndef (parser, true, true, true, true, true,
11636 NULL, vNULL);
11637 decl = check_for_loop_decls (for_loc, flag_isoc99);
11638 if (decl == NULL)
11639 goto error_init;
11640 if (DECL_INITIAL (decl) == error_mark_node)
11641 decl = error_mark_node;
11642 init = decl;
11643 }
11644 else if (c_parser_next_token_is (parser, CPP_NAME)
11645 && c_parser_peek_2nd_token (parser)->type == CPP_EQ)
11646 {
11647 struct c_expr decl_exp;
11648 struct c_expr init_exp;
11649 location_t init_loc;
11650
11651 decl_exp = c_parser_postfix_expression (parser);
11652 decl = decl_exp.value;
11653
11654 c_parser_require (parser, CPP_EQ, "expected %<=%>");
11655
11656 init_loc = c_parser_peek_token (parser)->location;
11657 init_exp = c_parser_expr_no_commas (parser, NULL);
11658 init_exp = default_function_array_read_conversion (init_loc,
11659 init_exp);
11660 init = build_modify_expr (init_loc, decl, decl_exp.original_type,
11661 NOP_EXPR, init_loc, init_exp.value,
11662 init_exp.original_type);
11663 init = c_process_expr_stmt (init_loc, init);
11664
11665 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
11666 }
11667 else
11668 {
11669 error_init:
11670 c_parser_error (parser,
11671 "expected iteration declaration or initialization");
11672 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
11673 "expected %<)%>");
11674 fail = true;
11675 goto parse_next;
11676 }
11677
11678 /* Parse the loop condition. */
11679 cond = NULL_TREE;
11680 if (c_parser_next_token_is_not (parser, CPP_SEMICOLON))
11681 {
11682 location_t cond_loc = c_parser_peek_token (parser)->location;
11683 struct c_expr cond_expr
11684 = c_parser_binary_expression (parser, NULL, NULL_TREE);
11685
11686 cond = cond_expr.value;
11687 cond = c_objc_common_truthvalue_conversion (cond_loc, cond);
11688 cond = c_fully_fold (cond, false, NULL);
11689 switch (cond_expr.original_code)
11690 {
11691 case GT_EXPR:
11692 case GE_EXPR:
11693 case LT_EXPR:
11694 case LE_EXPR:
11695 break;
c02065fc
AH
11696 case NE_EXPR:
11697 if (code == CILK_SIMD)
11698 break;
11699 /* FALLTHRU. */
acf0174b
JJ
11700 default:
11701 /* Can't be cond = error_mark_node, because we want to preserve
11702 the location until c_finish_omp_for. */
11703 cond = build1 (NOP_EXPR, boolean_type_node, error_mark_node);
11704 break;
11705 }
11706 protected_set_expr_location (cond, cond_loc);
11707 }
11708 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
11709
11710 /* Parse the increment expression. */
11711 incr = NULL_TREE;
11712 if (c_parser_next_token_is_not (parser, CPP_CLOSE_PAREN))
11713 {
11714 location_t incr_loc = c_parser_peek_token (parser)->location;
11715
11716 incr = c_process_expr_stmt (incr_loc,
11717 c_parser_expression (parser).value);
11718 }
11719 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11720
11721 if (decl == NULL || decl == error_mark_node || init == error_mark_node)
11722 fail = true;
11723 else
11724 {
11725 TREE_VEC_ELT (declv, i) = decl;
11726 TREE_VEC_ELT (initv, i) = init;
11727 TREE_VEC_ELT (condv, i) = cond;
11728 TREE_VEC_ELT (incrv, i) = incr;
11729 }
11730
11731 parse_next:
11732 if (i == collapse - 1)
11733 break;
11734
11735 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
11736 in between the collapsed for loops to be still considered perfectly
11737 nested. Hopefully the final version clarifies this.
11738 For now handle (multiple) {'s and empty statements. */
11739 do
11740 {
11741 if (c_parser_next_token_is_keyword (parser, RID_FOR))
11742 {
11743 c_parser_consume_token (parser);
11744 break;
11745 }
11746 else if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
11747 {
11748 c_parser_consume_token (parser);
11749 bracecount++;
11750 }
11751 else if (bracecount
11752 && c_parser_next_token_is (parser, CPP_SEMICOLON))
11753 c_parser_consume_token (parser);
11754 else
11755 {
11756 c_parser_error (parser, "not enough perfectly nested loops");
11757 if (bracecount)
11758 {
11759 open_brace_parsed = true;
11760 bracecount--;
11761 }
11762 fail = true;
11763 collapse = 0;
11764 break;
11765 }
11766 }
11767 while (1);
11768
11769 nbraces += bracecount;
11770 }
11771
11772 save_break = c_break_label;
c02065fc
AH
11773 if (code == CILK_SIMD)
11774 c_break_label = build_int_cst (size_type_node, 2);
11775 else
11776 c_break_label = size_one_node;
acf0174b
JJ
11777 save_cont = c_cont_label;
11778 c_cont_label = NULL_TREE;
11779 body = push_stmt_list ();
11780
11781 if (open_brace_parsed)
11782 {
11783 location_t here = c_parser_peek_token (parser)->location;
11784 stmt = c_begin_compound_stmt (true);
11785 c_parser_compound_statement_nostart (parser);
11786 add_stmt (c_end_compound_stmt (here, stmt, true));
11787 }
11788 else
11789 add_stmt (c_parser_c99_block_statement (parser));
11790 if (c_cont_label)
11791 {
11792 tree t = build1 (LABEL_EXPR, void_type_node, c_cont_label);
11793 SET_EXPR_LOCATION (t, loc);
11794 add_stmt (t);
11795 }
11796
11797 body = pop_stmt_list (body);
11798 c_break_label = save_break;
11799 c_cont_label = save_cont;
11800
11801 while (nbraces)
11802 {
11803 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
11804 {
11805 c_parser_consume_token (parser);
11806 nbraces--;
11807 }
11808 else if (c_parser_next_token_is (parser, CPP_SEMICOLON))
11809 c_parser_consume_token (parser);
11810 else
11811 {
11812 c_parser_error (parser, "collapsed loops not perfectly nested");
11813 while (nbraces)
11814 {
11815 location_t here = c_parser_peek_token (parser)->location;
11816 stmt = c_begin_compound_stmt (true);
11817 add_stmt (body);
11818 c_parser_compound_statement_nostart (parser);
11819 body = c_end_compound_stmt (here, stmt, true);
11820 nbraces--;
11821 }
11822 goto pop_scopes;
11823 }
11824 }
11825
11826 /* Only bother calling c_finish_omp_for if we haven't already generated
11827 an error from the initialization parsing. */
11828 if (!fail)
11829 {
11830 stmt = c_finish_omp_for (loc, code, declv, initv, condv,
11831 incrv, body, NULL);
11832 if (stmt)
11833 {
11834 if (cclauses != NULL
11835 && cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL] != NULL)
11836 {
11837 tree *c;
11838 for (c = &cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL]; *c ; )
11839 if (OMP_CLAUSE_CODE (*c) != OMP_CLAUSE_FIRSTPRIVATE
11840 && OMP_CLAUSE_CODE (*c) != OMP_CLAUSE_LASTPRIVATE)
11841 c = &OMP_CLAUSE_CHAIN (*c);
11842 else
11843 {
11844 for (i = 0; i < collapse; i++)
11845 if (TREE_VEC_ELT (declv, i) == OMP_CLAUSE_DECL (*c))
11846 break;
11847 if (i == collapse)
11848 c = &OMP_CLAUSE_CHAIN (*c);
11849 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE)
11850 {
11851 error_at (loc,
11852 "iteration variable %qD should not be firstprivate",
11853 OMP_CLAUSE_DECL (*c));
11854 *c = OMP_CLAUSE_CHAIN (*c);
11855 }
11856 else
11857 {
11858 /* Copy lastprivate (decl) clause to OMP_FOR_CLAUSES,
11859 change it to shared (decl) in
11860 OMP_PARALLEL_CLAUSES. */
11861 tree l = build_omp_clause (OMP_CLAUSE_LOCATION (*c),
11862 OMP_CLAUSE_LASTPRIVATE);
11863 OMP_CLAUSE_DECL (l) = OMP_CLAUSE_DECL (*c);
11864 OMP_CLAUSE_CHAIN (l) = clauses;
11865 clauses = l;
11866 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
11867 }
11868 }
11869 }
11870 OMP_FOR_CLAUSES (stmt) = clauses;
11871 }
11872 ret = stmt;
11873 }
11874pop_scopes:
11875 while (!for_block->is_empty ())
11876 {
11877 /* FIXME diagnostics: LOC below should be the actual location of
11878 this particular for block. We need to build a list of
11879 locations to go along with FOR_BLOCK. */
11880 stmt = c_end_compound_stmt (loc, for_block->pop (), true);
11881 add_stmt (stmt);
11882 }
11883 release_tree_vector (for_block);
11884 return ret;
11885}
11886
11887/* Helper function for OpenMP parsing, split clauses and call
11888 finish_omp_clauses on each of the set of clauses afterwards. */
11889
11890static void
11891omp_split_clauses (location_t loc, enum tree_code code,
11892 omp_clause_mask mask, tree clauses, tree *cclauses)
11893{
11894 int i;
11895 c_omp_split_clauses (loc, code, mask, clauses, cclauses);
11896 for (i = 0; i < C_OMP_CLAUSE_SPLIT_COUNT; i++)
11897 if (cclauses[i])
11898 cclauses[i] = c_finish_omp_clauses (cclauses[i]);
11899}
11900
11901/* OpenMP 4.0:
11902 #pragma omp simd simd-clause[optseq] new-line
11903 for-loop
11904
11905 LOC is the location of the #pragma token.
11906*/
11907
11908#define OMP_SIMD_CLAUSE_MASK \
11909 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
11910 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
11911 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
11912 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
11913 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
11914 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
11915 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
11916
11917static tree
11918c_parser_omp_simd (location_t loc, c_parser *parser,
11919 char *p_name, omp_clause_mask mask, tree *cclauses)
11920{
11921 tree block, clauses, ret;
11922
11923 strcat (p_name, " simd");
11924 mask |= OMP_SIMD_CLAUSE_MASK;
11925 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED);
11926
11927 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
11928 if (cclauses)
11929 {
11930 omp_split_clauses (loc, OMP_SIMD, mask, clauses, cclauses);
11931 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SIMD];
11932 }
11933
11934 block = c_begin_compound_stmt (true);
11935 ret = c_parser_omp_for_loop (loc, parser, OMP_SIMD, clauses, cclauses);
11936 block = c_end_compound_stmt (loc, block, true);
11937 add_stmt (block);
11938
11939 return ret;
11940}
11941
11942/* OpenMP 2.5:
11943 #pragma omp for for-clause[optseq] new-line
11944 for-loop
11945
11946 OpenMP 4.0:
11947 #pragma omp for simd for-simd-clause[optseq] new-line
11948 for-loop
11949
11950 LOC is the location of the #pragma token.
11951*/
11952
11953#define OMP_FOR_CLAUSE_MASK \
11954 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
11955 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
11956 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
11957 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
11958 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
11959 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
11960 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
11961 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
11962
11963static tree
11964c_parser_omp_for (location_t loc, c_parser *parser,
11965 char *p_name, omp_clause_mask mask, tree *cclauses)
11966{
11967 tree block, clauses, ret;
11968
11969 strcat (p_name, " for");
11970 mask |= OMP_FOR_CLAUSE_MASK;
11971 if (cclauses)
11972 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
11973
11974 if (c_parser_next_token_is (parser, CPP_NAME))
20906c66 11975 {
acf0174b
JJ
11976 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
11977
11978 if (strcmp (p, "simd") == 0)
11979 {
11980 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
11981 if (cclauses == NULL)
11982 cclauses = cclauses_buf;
11983
11984 c_parser_consume_token (parser);
6d7f7e0a
TB
11985 if (!flag_openmp) /* flag_openmp_simd */
11986 return c_parser_omp_simd (loc, parser, p_name, mask, cclauses);
acf0174b
JJ
11987 block = c_begin_compound_stmt (true);
11988 ret = c_parser_omp_simd (loc, parser, p_name, mask, cclauses);
11989 block = c_end_compound_stmt (loc, block, true);
11990 if (ret == NULL_TREE)
11991 return ret;
11992 ret = make_node (OMP_FOR);
11993 TREE_TYPE (ret) = void_type_node;
11994 OMP_FOR_BODY (ret) = block;
11995 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
11996 SET_EXPR_LOCATION (ret, loc);
11997 add_stmt (ret);
11998 return ret;
11999 }
20906c66 12000 }
6d7f7e0a
TB
12001 if (!flag_openmp) /* flag_openmp_simd */
12002 {
12003 c_parser_skip_to_pragma_eol (parser);
12004 return NULL_TREE;
12005 }
acf0174b
JJ
12006
12007 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
12008 if (cclauses)
20906c66 12009 {
acf0174b
JJ
12010 omp_split_clauses (loc, OMP_FOR, mask, clauses, cclauses);
12011 clauses = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
20906c66 12012 }
20906c66 12013
acf0174b
JJ
12014 block = c_begin_compound_stmt (true);
12015 ret = c_parser_omp_for_loop (loc, parser, OMP_FOR, clauses, cclauses);
12016 block = c_end_compound_stmt (loc, block, true);
12017 add_stmt (block);
12018
12019 return ret;
953ff289
DN
12020}
12021
acf0174b
JJ
12022/* OpenMP 2.5:
12023 # pragma omp master new-line
12024 structured-block
12025
12026 LOC is the location of the #pragma token.
12027*/
12028
12029static tree
12030c_parser_omp_master (location_t loc, c_parser *parser)
12031{
12032 c_parser_skip_to_pragma_eol (parser);
12033 return c_finish_omp_master (loc, c_parser_omp_structured_block (parser));
12034}
953ff289
DN
12035
12036/* OpenMP 2.5:
acf0174b
JJ
12037 # pragma omp ordered new-line
12038 structured-block
12039
12040 LOC is the location of the #pragma itself.
953ff289
DN
12041*/
12042
acf0174b
JJ
12043static tree
12044c_parser_omp_ordered (location_t loc, c_parser *parser)
953ff289 12045{
953ff289 12046 c_parser_skip_to_pragma_eol (parser);
acf0174b
JJ
12047 return c_finish_omp_ordered (loc, c_parser_omp_structured_block (parser));
12048}
953ff289 12049
acf0174b
JJ
12050/* OpenMP 2.5:
12051
12052 section-scope:
12053 { section-sequence }
12054
12055 section-sequence:
12056 section-directive[opt] structured-block
12057 section-sequence section-directive structured-block
12058
12059 SECTIONS_LOC is the location of the #pragma omp sections. */
12060
12061static tree
12062c_parser_omp_sections_scope (location_t sections_loc, c_parser *parser)
12063{
12064 tree stmt, substmt;
12065 bool error_suppress = false;
12066 location_t loc;
12067
12068 loc = c_parser_peek_token (parser)->location;
12069 if (!c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
12070 {
12071 /* Avoid skipping until the end of the block. */
12072 parser->error = false;
12073 return NULL_TREE;
12074 }
12075
12076 stmt = push_stmt_list ();
12077
12078 if (c_parser_peek_token (parser)->pragma_kind != PRAGMA_OMP_SECTION)
12079 {
12080 substmt = c_parser_omp_structured_block (parser);
12081 substmt = build1 (OMP_SECTION, void_type_node, substmt);
12082 SET_EXPR_LOCATION (substmt, loc);
12083 add_stmt (substmt);
12084 }
12085
12086 while (1)
12087 {
12088 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
12089 break;
12090 if (c_parser_next_token_is (parser, CPP_EOF))
12091 break;
12092
12093 loc = c_parser_peek_token (parser)->location;
12094 if (c_parser_peek_token (parser)->pragma_kind == PRAGMA_OMP_SECTION)
12095 {
12096 c_parser_consume_pragma (parser);
12097 c_parser_skip_to_pragma_eol (parser);
12098 error_suppress = false;
12099 }
12100 else if (!error_suppress)
12101 {
12102 error_at (loc, "expected %<#pragma omp section%> or %<}%>");
12103 error_suppress = true;
12104 }
12105
12106 substmt = c_parser_omp_structured_block (parser);
12107 substmt = build1 (OMP_SECTION, void_type_node, substmt);
12108 SET_EXPR_LOCATION (substmt, loc);
12109 add_stmt (substmt);
12110 }
12111 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE,
12112 "expected %<#pragma omp section%> or %<}%>");
12113
12114 substmt = pop_stmt_list (stmt);
12115
12116 stmt = make_node (OMP_SECTIONS);
12117 SET_EXPR_LOCATION (stmt, sections_loc);
12118 TREE_TYPE (stmt) = void_type_node;
12119 OMP_SECTIONS_BODY (stmt) = substmt;
12120
12121 return add_stmt (stmt);
953ff289
DN
12122}
12123
12124/* OpenMP 2.5:
acf0174b
JJ
12125 # pragma omp sections sections-clause[optseq] newline
12126 sections-scope
c2255bc4 12127
acf0174b
JJ
12128 LOC is the location of the #pragma token.
12129*/
12130
12131#define OMP_SECTIONS_CLAUSE_MASK \
12132 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
12133 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
12134 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
12135 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
12136 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
953ff289
DN
12137
12138static tree
acf0174b
JJ
12139c_parser_omp_sections (location_t loc, c_parser *parser,
12140 char *p_name, omp_clause_mask mask, tree *cclauses)
953ff289 12141{
acf0174b 12142 tree block, clauses, ret;
953ff289 12143
acf0174b
JJ
12144 strcat (p_name, " sections");
12145 mask |= OMP_SECTIONS_CLAUSE_MASK;
12146 if (cclauses)
12147 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
12148
12149 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
12150 if (cclauses)
12151 {
12152 omp_split_clauses (loc, OMP_SECTIONS, mask, clauses, cclauses);
12153 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SECTIONS];
12154 }
12155
12156 block = c_begin_compound_stmt (true);
12157 ret = c_parser_omp_sections_scope (loc, parser);
12158 if (ret)
12159 OMP_SECTIONS_CLAUSES (ret) = clauses;
12160 block = c_end_compound_stmt (loc, block, true);
12161 add_stmt (block);
12162
12163 return ret;
12164}
12165
12166/* OpenMP 2.5:
cef0fd0e
TS
12167 # pragma omp parallel parallel-clause[optseq] new-line
12168 structured-block
12169 # pragma omp parallel for parallel-for-clause[optseq] new-line
12170 structured-block
12171 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
12172 structured-block
12173
12174 OpenMP 4.0:
12175 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
12176 structured-block
acf0174b
JJ
12177
12178 LOC is the location of the #pragma token.
12179*/
12180
12181#define OMP_PARALLEL_CLAUSE_MASK \
12182 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
12183 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
12184 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
12185 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
12186 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
12187 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
12188 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
12189 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
12190 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
12191
12192static tree
12193c_parser_omp_parallel (location_t loc, c_parser *parser,
12194 char *p_name, omp_clause_mask mask, tree *cclauses)
12195{
12196 tree stmt, clauses, block;
12197
12198 strcat (p_name, " parallel");
12199 mask |= OMP_PARALLEL_CLAUSE_MASK;
12200
12201 if (c_parser_next_token_is_keyword (parser, RID_FOR))
953ff289 12202 {
acf0174b
JJ
12203 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
12204 if (cclauses == NULL)
12205 cclauses = cclauses_buf;
12206
953ff289 12207 c_parser_consume_token (parser);
6d7f7e0a
TB
12208 if (!flag_openmp) /* flag_openmp_simd */
12209 return c_parser_omp_for (loc, parser, p_name, mask, cclauses);
acf0174b
JJ
12210 block = c_begin_omp_parallel ();
12211 c_parser_omp_for (loc, parser, p_name, mask, cclauses);
12212 stmt
12213 = c_finish_omp_parallel (loc, cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
12214 block);
12215 OMP_PARALLEL_COMBINED (stmt) = 1;
12216 return stmt;
12217 }
12218 else if (cclauses)
12219 {
12220 error_at (loc, "expected %<for%> after %qs", p_name);
12221 c_parser_skip_to_pragma_eol (parser);
12222 return NULL_TREE;
12223 }
6d7f7e0a
TB
12224 else if (!flag_openmp) /* flag_openmp_simd */
12225 {
12226 c_parser_skip_to_pragma_eol (parser);
12227 return NULL_TREE;
12228 }
acf0174b
JJ
12229 else if (c_parser_next_token_is (parser, CPP_NAME))
12230 {
12231 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
12232 if (strcmp (p, "sections") == 0)
953ff289 12233 {
acf0174b
JJ
12234 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
12235 if (cclauses == NULL)
12236 cclauses = cclauses_buf;
12237
953ff289 12238 c_parser_consume_token (parser);
acf0174b
JJ
12239 block = c_begin_omp_parallel ();
12240 c_parser_omp_sections (loc, parser, p_name, mask, cclauses);
12241 stmt = c_finish_omp_parallel (loc,
12242 cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
12243 block);
12244 OMP_PARALLEL_COMBINED (stmt) = 1;
12245 return stmt;
953ff289 12246 }
953ff289 12247 }
953ff289 12248
acf0174b
JJ
12249 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
12250
12251 block = c_begin_omp_parallel ();
12252 c_parser_statement (parser);
12253 stmt = c_finish_omp_parallel (loc, clauses, block);
12254
12255 return stmt;
12256}
12257
12258/* OpenMP 2.5:
12259 # pragma omp single single-clause[optseq] new-line
12260 structured-block
12261
12262 LOC is the location of the #pragma.
12263*/
12264
12265#define OMP_SINGLE_CLAUSE_MASK \
12266 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
12267 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
12268 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
12269 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
12270
12271static tree
12272c_parser_omp_single (location_t loc, c_parser *parser)
12273{
12274 tree stmt = make_node (OMP_SINGLE);
12275 SET_EXPR_LOCATION (stmt, loc);
12276 TREE_TYPE (stmt) = void_type_node;
12277
12278 OMP_SINGLE_CLAUSES (stmt)
12279 = c_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
12280 "#pragma omp single");
12281 OMP_SINGLE_BODY (stmt) = c_parser_omp_structured_block (parser);
12282
12283 return add_stmt (stmt);
12284}
12285
12286/* OpenMP 3.0:
12287 # pragma omp task task-clause[optseq] new-line
12288
12289 LOC is the location of the #pragma.
12290*/
12291
12292#define OMP_TASK_CLAUSE_MASK \
12293 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
12294 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
12295 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
12296 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
12297 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
12298 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
12299 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
12300 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
12301 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND))
12302
12303static tree
12304c_parser_omp_task (location_t loc, c_parser *parser)
12305{
12306 tree clauses, block;
12307
12308 clauses = c_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
12309 "#pragma omp task");
12310
12311 block = c_begin_omp_task ();
12312 c_parser_statement (parser);
12313 return c_finish_omp_task (loc, clauses, block);
953ff289
DN
12314}
12315
acf0174b
JJ
12316/* OpenMP 3.0:
12317 # pragma omp taskwait new-line
12318*/
953ff289
DN
12319
12320static void
acf0174b 12321c_parser_omp_taskwait (c_parser *parser)
953ff289 12322{
c2255bc4 12323 location_t loc = c_parser_peek_token (parser)->location;
953ff289 12324 c_parser_consume_pragma (parser);
953ff289
DN
12325 c_parser_skip_to_pragma_eol (parser);
12326
acf0174b 12327 c_finish_omp_taskwait (loc);
953ff289
DN
12328}
12329
acf0174b
JJ
12330/* OpenMP 3.1:
12331 # pragma omp taskyield new-line
12332*/
953ff289 12333
acf0174b
JJ
12334static void
12335c_parser_omp_taskyield (c_parser *parser)
953ff289 12336{
acf0174b
JJ
12337 location_t loc = c_parser_peek_token (parser)->location;
12338 c_parser_consume_pragma (parser);
12339 c_parser_skip_to_pragma_eol (parser);
a68ab351 12340
acf0174b
JJ
12341 c_finish_omp_taskyield (loc);
12342}
3ba09659 12343
acf0174b
JJ
12344/* OpenMP 4.0:
12345 # pragma omp taskgroup new-line
12346*/
953ff289 12347
acf0174b
JJ
12348static tree
12349c_parser_omp_taskgroup (c_parser *parser)
12350{
12351 location_t loc = c_parser_peek_token (parser)->location;
12352 c_parser_skip_to_pragma_eol (parser);
12353 return c_finish_omp_taskgroup (loc, c_parser_omp_structured_block (parser));
12354}
c9f9eb5d 12355
acf0174b
JJ
12356/* OpenMP 4.0:
12357 # pragma omp cancel cancel-clause[optseq] new-line
953ff289 12358
acf0174b
JJ
12359 LOC is the location of the #pragma.
12360*/
a68ab351 12361
acf0174b
JJ
12362#define OMP_CANCEL_CLAUSE_MASK \
12363 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
12364 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
12365 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
12366 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
12367 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
a68ab351 12368
acf0174b
JJ
12369static void
12370c_parser_omp_cancel (c_parser *parser)
12371{
12372 location_t loc = c_parser_peek_token (parser)->location;
a68ab351 12373
acf0174b
JJ
12374 c_parser_consume_pragma (parser);
12375 tree clauses = c_parser_omp_all_clauses (parser, OMP_CANCEL_CLAUSE_MASK,
12376 "#pragma omp cancel");
953ff289 12377
acf0174b
JJ
12378 c_finish_omp_cancel (loc, clauses);
12379}
953ff289 12380
acf0174b
JJ
12381/* OpenMP 4.0:
12382 # pragma omp cancellation point cancelpt-clause[optseq] new-line
953ff289 12383
acf0174b
JJ
12384 LOC is the location of the #pragma.
12385*/
953ff289 12386
acf0174b
JJ
12387#define OMP_CANCELLATION_POINT_CLAUSE_MASK \
12388 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
12389 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
12390 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
12391 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
a68ab351 12392
acf0174b
JJ
12393static void
12394c_parser_omp_cancellation_point (c_parser *parser)
12395{
12396 location_t loc = c_parser_peek_token (parser)->location;
12397 tree clauses;
12398 bool point_seen = false;
12399
12400 c_parser_consume_pragma (parser);
12401 if (c_parser_next_token_is (parser, CPP_NAME))
a68ab351 12402 {
acf0174b
JJ
12403 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
12404 if (strcmp (p, "point") == 0)
a68ab351 12405 {
acf0174b
JJ
12406 c_parser_consume_token (parser);
12407 point_seen = true;
a68ab351 12408 }
a68ab351 12409 }
acf0174b 12410 if (!point_seen)
a68ab351 12411 {
acf0174b
JJ
12412 c_parser_error (parser, "expected %<point%>");
12413 c_parser_skip_to_pragma_eol (parser);
12414 return;
a68ab351 12415 }
953ff289 12416
acf0174b
JJ
12417 clauses
12418 = c_parser_omp_all_clauses (parser, OMP_CANCELLATION_POINT_CLAUSE_MASK,
12419 "#pragma omp cancellation point");
c2255bc4 12420
acf0174b
JJ
12421 c_finish_omp_cancellation_point (loc, clauses);
12422}
953ff289 12423
acf0174b
JJ
12424/* OpenMP 4.0:
12425 #pragma omp distribute distribute-clause[optseq] new-line
12426 for-loop */
12427
12428#define OMP_DISTRIBUTE_CLAUSE_MASK \
12429 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
12430 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
12431 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
12432 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
953ff289
DN
12433
12434static tree
acf0174b
JJ
12435c_parser_omp_distribute (location_t loc, c_parser *parser,
12436 char *p_name, omp_clause_mask mask, tree *cclauses)
953ff289 12437{
acf0174b
JJ
12438 tree clauses, block, ret;
12439
12440 strcat (p_name, " distribute");
12441 mask |= OMP_DISTRIBUTE_CLAUSE_MASK;
12442
12443 if (c_parser_next_token_is (parser, CPP_NAME))
12444 {
12445 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
12446 bool simd = false;
12447 bool parallel = false;
12448
12449 if (strcmp (p, "simd") == 0)
12450 simd = true;
12451 else
12452 parallel = strcmp (p, "parallel") == 0;
12453 if (parallel || simd)
12454 {
12455 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
12456 if (cclauses == NULL)
12457 cclauses = cclauses_buf;
12458 c_parser_consume_token (parser);
6d7f7e0a
TB
12459 if (!flag_openmp) /* flag_openmp_simd */
12460 {
12461 if (simd)
12462 return c_parser_omp_simd (loc, parser, p_name, mask, cclauses);
12463 else
12464 return c_parser_omp_parallel (loc, parser, p_name, mask,
12465 cclauses);
12466 }
acf0174b
JJ
12467 block = c_begin_compound_stmt (true);
12468 if (simd)
12469 ret = c_parser_omp_simd (loc, parser, p_name, mask, cclauses);
12470 else
12471 ret = c_parser_omp_parallel (loc, parser, p_name, mask, cclauses);
12472 block = c_end_compound_stmt (loc, block, true);
12473 if (ret == NULL)
12474 return ret;
12475 ret = make_node (OMP_DISTRIBUTE);
12476 TREE_TYPE (ret) = void_type_node;
12477 OMP_FOR_BODY (ret) = block;
12478 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
12479 SET_EXPR_LOCATION (ret, loc);
12480 add_stmt (ret);
12481 return ret;
12482 }
12483 }
6d7f7e0a
TB
12484 if (!flag_openmp) /* flag_openmp_simd */
12485 {
12486 c_parser_skip_to_pragma_eol (parser);
12487 return NULL_TREE;
12488 }
953ff289 12489
acf0174b
JJ
12490 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
12491 if (cclauses)
12492 {
12493 omp_split_clauses (loc, OMP_DISTRIBUTE, mask, clauses, cclauses);
12494 clauses = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
12495 }
953ff289
DN
12496
12497 block = c_begin_compound_stmt (true);
acf0174b 12498 ret = c_parser_omp_for_loop (loc, parser, OMP_DISTRIBUTE, clauses, NULL);
c2255bc4 12499 block = c_end_compound_stmt (loc, block, true);
953ff289
DN
12500 add_stmt (block);
12501
12502 return ret;
12503}
12504
acf0174b
JJ
12505/* OpenMP 4.0:
12506 # pragma omp teams teams-clause[optseq] new-line
12507 structured-block */
c2255bc4 12508
acf0174b
JJ
12509#define OMP_TEAMS_CLAUSE_MASK \
12510 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
12511 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
12512 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
12513 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
12514 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
12515 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
12516 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
953ff289
DN
12517
12518static tree
acf0174b
JJ
12519c_parser_omp_teams (location_t loc, c_parser *parser,
12520 char *p_name, omp_clause_mask mask, tree *cclauses)
953ff289 12521{
acf0174b
JJ
12522 tree clauses, block, ret;
12523
12524 strcat (p_name, " teams");
12525 mask |= OMP_TEAMS_CLAUSE_MASK;
12526
12527 if (c_parser_next_token_is (parser, CPP_NAME))
12528 {
12529 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
12530 if (strcmp (p, "distribute") == 0)
12531 {
12532 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
12533 if (cclauses == NULL)
12534 cclauses = cclauses_buf;
12535
12536 c_parser_consume_token (parser);
6d7f7e0a
TB
12537 if (!flag_openmp) /* flag_openmp_simd */
12538 return c_parser_omp_distribute (loc, parser, p_name, mask, cclauses);
acf0174b
JJ
12539 block = c_begin_compound_stmt (true);
12540 ret = c_parser_omp_distribute (loc, parser, p_name, mask, cclauses);
12541 block = c_end_compound_stmt (loc, block, true);
12542 if (ret == NULL)
12543 return ret;
12544 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
12545 ret = make_node (OMP_TEAMS);
12546 TREE_TYPE (ret) = void_type_node;
12547 OMP_TEAMS_CLAUSES (ret) = clauses;
12548 OMP_TEAMS_BODY (ret) = block;
12549 return add_stmt (ret);
12550 }
12551 }
6d7f7e0a
TB
12552 if (!flag_openmp) /* flag_openmp_simd */
12553 {
12554 c_parser_skip_to_pragma_eol (parser);
12555 return NULL_TREE;
12556 }
acf0174b
JJ
12557
12558 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
12559 if (cclauses)
12560 {
12561 omp_split_clauses (loc, OMP_TEAMS, mask, clauses, cclauses);
12562 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
12563 }
12564
12565 tree stmt = make_node (OMP_TEAMS);
12566 TREE_TYPE (stmt) = void_type_node;
12567 OMP_TEAMS_CLAUSES (stmt) = clauses;
12568 OMP_TEAMS_BODY (stmt) = c_parser_omp_structured_block (parser);
12569
12570 return add_stmt (stmt);
953ff289
DN
12571}
12572
acf0174b
JJ
12573/* OpenMP 4.0:
12574 # pragma omp target data target-data-clause[optseq] new-line
12575 structured-block */
c2255bc4 12576
acf0174b
JJ
12577#define OMP_TARGET_DATA_CLAUSE_MASK \
12578 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
12579 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
12580 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
953ff289
DN
12581
12582static tree
acf0174b 12583c_parser_omp_target_data (location_t loc, c_parser *parser)
953ff289 12584{
acf0174b
JJ
12585 tree stmt = make_node (OMP_TARGET_DATA);
12586 TREE_TYPE (stmt) = void_type_node;
953ff289 12587
acf0174b
JJ
12588 OMP_TARGET_DATA_CLAUSES (stmt)
12589 = c_parser_omp_all_clauses (parser, OMP_TARGET_DATA_CLAUSE_MASK,
12590 "#pragma omp target data");
12591 keep_next_level ();
12592 tree block = c_begin_compound_stmt (true);
12593 add_stmt (c_parser_omp_structured_block (parser));
12594 OMP_TARGET_DATA_BODY (stmt) = c_end_compound_stmt (loc, block, true);
953ff289 12595
acf0174b
JJ
12596 SET_EXPR_LOCATION (stmt, loc);
12597 return add_stmt (stmt);
12598}
953ff289 12599
acf0174b
JJ
12600/* OpenMP 4.0:
12601 # pragma omp target update target-update-clause[optseq] new-line */
c2255bc4 12602
acf0174b
JJ
12603#define OMP_TARGET_UPDATE_CLAUSE_MASK \
12604 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
12605 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
12606 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
12607 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
953ff289 12608
acf0174b
JJ
12609static bool
12610c_parser_omp_target_update (location_t loc, c_parser *parser,
12611 enum pragma_context context)
953ff289 12612{
acf0174b
JJ
12613 if (context == pragma_stmt)
12614 {
12615 error_at (loc,
12616 "%<#pragma omp target update%> may only be "
12617 "used in compound statements");
12618 c_parser_skip_to_pragma_eol (parser);
12619 return false;
12620 }
953ff289 12621
acf0174b
JJ
12622 tree clauses
12623 = c_parser_omp_all_clauses (parser, OMP_TARGET_UPDATE_CLAUSE_MASK,
12624 "#pragma omp target update");
12625 if (find_omp_clause (clauses, OMP_CLAUSE_TO) == NULL_TREE
12626 && find_omp_clause (clauses, OMP_CLAUSE_FROM) == NULL_TREE)
953ff289 12627 {
acf0174b
JJ
12628 error_at (loc,
12629 "%<#pragma omp target update must contain at least one "
12630 "%<from%> or %<to%> clauses");
12631 return false;
953ff289
DN
12632 }
12633
acf0174b
JJ
12634 tree stmt = make_node (OMP_TARGET_UPDATE);
12635 TREE_TYPE (stmt) = void_type_node;
12636 OMP_TARGET_UPDATE_CLAUSES (stmt) = clauses;
12637 SET_EXPR_LOCATION (stmt, loc);
12638 add_stmt (stmt);
12639 return false;
12640}
953ff289 12641
acf0174b
JJ
12642/* OpenMP 4.0:
12643 # pragma omp target target-clause[optseq] new-line
12644 structured-block */
953ff289 12645
acf0174b
JJ
12646#define OMP_TARGET_CLAUSE_MASK \
12647 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
12648 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
12649 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
953ff289 12650
acf0174b
JJ
12651static bool
12652c_parser_omp_target (c_parser *parser, enum pragma_context context)
12653{
12654 location_t loc = c_parser_peek_token (parser)->location;
12655 c_parser_consume_pragma (parser);
953ff289 12656
acf0174b
JJ
12657 if (context != pragma_stmt && context != pragma_compound)
12658 {
12659 c_parser_error (parser, "expected declaration specifiers");
12660 c_parser_skip_to_pragma_eol (parser);
12661 return false;
953ff289
DN
12662 }
12663
acf0174b 12664 if (c_parser_next_token_is (parser, CPP_NAME))
953ff289 12665 {
acf0174b 12666 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
953ff289 12667
6d7f7e0a 12668 if (strcmp (p, "teams") == 0)
acf0174b
JJ
12669 {
12670 tree cclauses[C_OMP_CLAUSE_SPLIT_COUNT];
12671 char p_name[sizeof ("#pragma omp target teams distribute "
12672 "parallel for simd")];
953ff289 12673
acf0174b 12674 c_parser_consume_token (parser);
e7bd1de1 12675 strcpy (p_name, "#pragma omp target");
6d7f7e0a
TB
12676 if (!flag_openmp) /* flag_openmp_simd */
12677 return c_parser_omp_teams (loc, parser, p_name,
12678 OMP_TARGET_CLAUSE_MASK, cclauses);
acf0174b
JJ
12679 keep_next_level ();
12680 tree block = c_begin_compound_stmt (true);
12681 tree ret = c_parser_omp_teams (loc, parser, p_name,
12682 OMP_TARGET_CLAUSE_MASK, cclauses);
12683 block = c_end_compound_stmt (loc, block, true);
12684 if (ret == NULL)
12685 return ret;
12686 tree stmt = make_node (OMP_TARGET);
12687 TREE_TYPE (stmt) = void_type_node;
12688 OMP_TARGET_CLAUSES (stmt) = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
12689 OMP_TARGET_BODY (stmt) = block;
12690 add_stmt (stmt);
12691 return true;
12692 }
6d7f7e0a
TB
12693 else if (!flag_openmp) /* flag_openmp_simd */
12694 {
12695 c_parser_skip_to_pragma_eol (parser);
12696 return NULL_TREE;
12697 }
12698 else if (strcmp (p, "data") == 0)
12699 {
12700 c_parser_consume_token (parser);
12701 c_parser_omp_target_data (loc, parser);
12702 return true;
12703 }
12704 else if (strcmp (p, "update") == 0)
12705 {
12706 c_parser_consume_token (parser);
12707 return c_parser_omp_target_update (loc, parser, context);
12708 }
953ff289 12709 }
953ff289 12710
acf0174b 12711 tree stmt = make_node (OMP_TARGET);
953ff289 12712 TREE_TYPE (stmt) = void_type_node;
953ff289 12713
acf0174b
JJ
12714 OMP_TARGET_CLAUSES (stmt)
12715 = c_parser_omp_all_clauses (parser, OMP_TARGET_CLAUSE_MASK,
12716 "#pragma omp target");
12717 keep_next_level ();
12718 tree block = c_begin_compound_stmt (true);
12719 add_stmt (c_parser_omp_structured_block (parser));
12720 OMP_TARGET_BODY (stmt) = c_end_compound_stmt (loc, block, true);
12721
12722 SET_EXPR_LOCATION (stmt, loc);
12723 add_stmt (stmt);
12724 return true;
12725}
12726
12727/* OpenMP 4.0:
12728 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
12729
12730#define OMP_DECLARE_SIMD_CLAUSE_MASK \
12731 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
12732 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
12733 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
12734 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
12735 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
12736 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
12737
12738static void
12739c_parser_omp_declare_simd (c_parser *parser, enum pragma_context context)
12740{
12741 vec<c_token> clauses = vNULL;
12742 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
12743 {
12744 c_token *token = c_parser_peek_token (parser);
12745 if (token->type == CPP_EOF)
12746 {
12747 c_parser_skip_to_pragma_eol (parser);
12748 clauses.release ();
12749 return;
12750 }
12751 clauses.safe_push (*token);
12752 c_parser_consume_token (parser);
12753 }
12754 clauses.safe_push (*c_parser_peek_token (parser));
12755 c_parser_skip_to_pragma_eol (parser);
12756
12757 while (c_parser_next_token_is (parser, CPP_PRAGMA))
12758 {
12759 if (c_parser_peek_token (parser)->pragma_kind
12760 != PRAGMA_OMP_DECLARE_REDUCTION
12761 || c_parser_peek_2nd_token (parser)->type != CPP_NAME
12762 || strcmp (IDENTIFIER_POINTER
12763 (c_parser_peek_2nd_token (parser)->value),
12764 "simd") != 0)
12765 {
12766 c_parser_error (parser,
12767 "%<#pragma omp declare simd%> must be followed by "
12768 "function declaration or definition or another "
12769 "%<#pragma omp declare simd%>");
12770 clauses.release ();
12771 return;
12772 }
12773 c_parser_consume_pragma (parser);
12774 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
12775 {
12776 c_token *token = c_parser_peek_token (parser);
12777 if (token->type == CPP_EOF)
12778 {
12779 c_parser_skip_to_pragma_eol (parser);
12780 clauses.release ();
12781 return;
12782 }
12783 clauses.safe_push (*token);
12784 c_parser_consume_token (parser);
12785 }
12786 clauses.safe_push (*c_parser_peek_token (parser));
12787 c_parser_skip_to_pragma_eol (parser);
12788 }
12789
12790 /* Make sure nothing tries to read past the end of the tokens. */
12791 c_token eof_token;
12792 memset (&eof_token, 0, sizeof (eof_token));
12793 eof_token.type = CPP_EOF;
12794 clauses.safe_push (eof_token);
12795 clauses.safe_push (eof_token);
12796
12797 switch (context)
12798 {
12799 case pragma_external:
12800 if (c_parser_next_token_is (parser, CPP_KEYWORD)
12801 && c_parser_peek_token (parser)->keyword == RID_EXTENSION)
12802 {
12803 int ext = disable_extension_diagnostics ();
12804 do
12805 c_parser_consume_token (parser);
12806 while (c_parser_next_token_is (parser, CPP_KEYWORD)
12807 && c_parser_peek_token (parser)->keyword == RID_EXTENSION);
12808 c_parser_declaration_or_fndef (parser, true, true, true, false, true,
12809 NULL, clauses);
12810 restore_extension_diagnostics (ext);
12811 }
12812 else
12813 c_parser_declaration_or_fndef (parser, true, true, true, false, true,
12814 NULL, clauses);
12815 break;
12816 case pragma_struct:
12817 case pragma_param:
12818 c_parser_error (parser, "%<#pragma omp declare simd%> must be followed by "
12819 "function declaration or definition");
12820 break;
12821 case pragma_compound:
12822 case pragma_stmt:
12823 if (c_parser_next_token_is (parser, CPP_KEYWORD)
12824 && c_parser_peek_token (parser)->keyword == RID_EXTENSION)
12825 {
12826 int ext = disable_extension_diagnostics ();
12827 do
12828 c_parser_consume_token (parser);
12829 while (c_parser_next_token_is (parser, CPP_KEYWORD)
12830 && c_parser_peek_token (parser)->keyword == RID_EXTENSION);
12831 if (c_parser_next_tokens_start_declaration (parser))
12832 {
12833 c_parser_declaration_or_fndef (parser, true, true, true, true,
12834 true, NULL, clauses);
12835 restore_extension_diagnostics (ext);
12836 break;
12837 }
12838 restore_extension_diagnostics (ext);
12839 }
12840 else if (c_parser_next_tokens_start_declaration (parser))
12841 {
12842 c_parser_declaration_or_fndef (parser, true, true, true, true, true,
12843 NULL, clauses);
12844 break;
12845 }
12846 c_parser_error (parser, "%<#pragma omp declare simd%> must be followed by "
12847 "function declaration or definition");
12848 break;
12849 default:
12850 gcc_unreachable ();
12851 }
12852 clauses.release ();
953ff289
DN
12853}
12854
acf0174b
JJ
12855/* Finalize #pragma omp declare simd clauses after FNDECL has been parsed,
12856 and put that into "omp declare simd" attribute. */
c2255bc4 12857
acf0174b
JJ
12858static void
12859c_finish_omp_declare_simd (c_parser *parser, tree fndecl, tree parms,
12860 vec<c_token> clauses)
12861{
b72271b9 12862 if (flag_cilkplus
41958c28
BI
12863 && clauses.exists () && !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
12864 {
12865 error ("%<#pragma omp declare simd%> cannot be used in the same "
12866 "function marked as a Cilk Plus SIMD-enabled function");
12867 vec_free (parser->cilk_simd_fn_tokens);
12868 return;
12869 }
12870
acf0174b
JJ
12871 /* Normally first token is CPP_NAME "simd". CPP_EOF there indicates
12872 error has been reported and CPP_PRAGMA that c_finish_omp_declare_simd
12873 has already processed the tokens. */
41958c28 12874 if (clauses.exists () && clauses[0].type == CPP_EOF)
acf0174b
JJ
12875 return;
12876 if (fndecl == NULL_TREE || TREE_CODE (fndecl) != FUNCTION_DECL)
12877 {
12878 error ("%<#pragma omp declare simd%> not immediately followed by "
12879 "a function declaration or definition");
12880 clauses[0].type = CPP_EOF;
12881 return;
12882 }
41958c28 12883 if (clauses.exists () && clauses[0].type != CPP_NAME)
acf0174b
JJ
12884 {
12885 error_at (DECL_SOURCE_LOCATION (fndecl),
12886 "%<#pragma omp declare simd%> not immediately followed by "
12887 "a single function declaration or definition");
12888 clauses[0].type = CPP_EOF;
12889 return;
12890 }
953ff289 12891
acf0174b
JJ
12892 if (parms == NULL_TREE)
12893 parms = DECL_ARGUMENTS (fndecl);
953ff289 12894
acf0174b
JJ
12895 unsigned int tokens_avail = parser->tokens_avail;
12896 gcc_assert (parser->tokens == &parser->tokens_buf[0]);
41958c28
BI
12897 bool is_cilkplus_cilk_simd_fn = false;
12898
b72271b9 12899 if (flag_cilkplus && !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
41958c28
BI
12900 {
12901 parser->tokens = parser->cilk_simd_fn_tokens->address ();
12902 parser->tokens_avail = vec_safe_length (parser->cilk_simd_fn_tokens);
12903 is_cilkplus_cilk_simd_fn = true;
12904 }
12905 else
12906 {
12907 parser->tokens = clauses.address ();
12908 parser->tokens_avail = clauses.length ();
12909 }
12910
acf0174b
JJ
12911 /* c_parser_omp_declare_simd pushed 2 extra CPP_EOF tokens at the end. */
12912 while (parser->tokens_avail > 3)
12913 {
12914 c_token *token = c_parser_peek_token (parser);
41958c28
BI
12915 if (!is_cilkplus_cilk_simd_fn)
12916 gcc_assert (token->type == CPP_NAME
12917 && strcmp (IDENTIFIER_POINTER (token->value), "simd") == 0);
12918 else
12919 gcc_assert (token->type == CPP_NAME
12920 && is_cilkplus_vector_p (token->value));
acf0174b
JJ
12921 c_parser_consume_token (parser);
12922 parser->in_pragma = true;
953ff289 12923
41958c28
BI
12924 tree c = NULL_TREE;
12925 if (is_cilkplus_cilk_simd_fn)
12926 c = c_parser_omp_all_clauses (parser, CILK_SIMD_FN_CLAUSE_MASK,
12927 "SIMD-enabled functions attribute");
12928 else
12929 c = c_parser_omp_all_clauses (parser, OMP_DECLARE_SIMD_CLAUSE_MASK,
12930 "#pragma omp declare simd");
acf0174b
JJ
12931 c = c_omp_declare_simd_clauses_to_numbers (parms, c);
12932 if (c != NULL_TREE)
12933 c = tree_cons (NULL_TREE, c, NULL_TREE);
41958c28
BI
12934 if (is_cilkplus_cilk_simd_fn)
12935 {
74558dd9
BI
12936 tree k = build_tree_list (get_identifier ("cilk simd function"),
12937 NULL_TREE);
41958c28
BI
12938 TREE_CHAIN (k) = DECL_ATTRIBUTES (fndecl);
12939 DECL_ATTRIBUTES (fndecl) = k;
12940 }
acf0174b
JJ
12941 c = build_tree_list (get_identifier ("omp declare simd"), c);
12942 TREE_CHAIN (c) = DECL_ATTRIBUTES (fndecl);
12943 DECL_ATTRIBUTES (fndecl) = c;
12944 }
953ff289 12945
acf0174b
JJ
12946 parser->tokens = &parser->tokens_buf[0];
12947 parser->tokens_avail = tokens_avail;
41958c28
BI
12948 if (clauses.exists ())
12949 clauses[0].type = CPP_PRAGMA;
12950
12951 if (!vec_safe_is_empty (parser->cilk_simd_fn_tokens))
12952 vec_free (parser->cilk_simd_fn_tokens);
953ff289
DN
12953}
12954
953ff289 12955
acf0174b
JJ
12956/* OpenMP 4.0:
12957 # pragma omp declare target new-line
12958 declarations and definitions
12959 # pragma omp end declare target new-line */
953ff289 12960
acf0174b
JJ
12961static void
12962c_parser_omp_declare_target (c_parser *parser)
953ff289 12963{
acf0174b
JJ
12964 c_parser_skip_to_pragma_eol (parser);
12965 current_omp_declare_target_attribute++;
12966}
953ff289 12967
acf0174b
JJ
12968static void
12969c_parser_omp_end_declare_target (c_parser *parser)
12970{
12971 location_t loc = c_parser_peek_token (parser)->location;
12972 c_parser_consume_pragma (parser);
12973 if (c_parser_next_token_is (parser, CPP_NAME)
12974 && strcmp (IDENTIFIER_POINTER (c_parser_peek_token (parser)->value),
12975 "declare") == 0)
953ff289
DN
12976 {
12977 c_parser_consume_token (parser);
acf0174b
JJ
12978 if (c_parser_next_token_is (parser, CPP_NAME)
12979 && strcmp (IDENTIFIER_POINTER (c_parser_peek_token (parser)->value),
12980 "target") == 0)
12981 c_parser_consume_token (parser);
12982 else
953ff289 12983 {
acf0174b
JJ
12984 c_parser_error (parser, "expected %<target%>");
12985 c_parser_skip_to_pragma_eol (parser);
12986 return;
953ff289
DN
12987 }
12988 }
acf0174b
JJ
12989 else
12990 {
12991 c_parser_error (parser, "expected %<declare%>");
12992 c_parser_skip_to_pragma_eol (parser);
12993 return;
12994 }
12995 c_parser_skip_to_pragma_eol (parser);
12996 if (!current_omp_declare_target_attribute)
12997 error_at (loc, "%<#pragma omp end declare target%> without corresponding "
12998 "%<#pragma omp declare target%>");
12999 else
13000 current_omp_declare_target_attribute--;
13001}
953ff289 13002
953ff289 13003
acf0174b
JJ
13004/* OpenMP 4.0
13005 #pragma omp declare reduction (reduction-id : typename-list : expression) \
13006 initializer-clause[opt] new-line
13007
13008 initializer-clause:
13009 initializer (omp_priv = initializer)
13010 initializer (function-name (argument-list)) */
13011
13012static void
13013c_parser_omp_declare_reduction (c_parser *parser, enum pragma_context context)
13014{
13015 unsigned int tokens_avail = 0, i;
13016 vec<tree> types = vNULL;
13017 vec<c_token> clauses = vNULL;
13018 enum tree_code reduc_code = ERROR_MARK;
13019 tree reduc_id = NULL_TREE;
13020 tree type;
13021 location_t rloc = c_parser_peek_token (parser)->location;
13022
13023 if (context == pragma_struct || context == pragma_param)
953ff289 13024 {
acf0174b
JJ
13025 error ("%<#pragma omp declare reduction%> not at file or block scope");
13026 goto fail;
13027 }
953ff289 13028
acf0174b
JJ
13029 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
13030 goto fail;
953ff289 13031
acf0174b
JJ
13032 switch (c_parser_peek_token (parser)->type)
13033 {
13034 case CPP_PLUS:
13035 reduc_code = PLUS_EXPR;
13036 break;
13037 case CPP_MULT:
13038 reduc_code = MULT_EXPR;
13039 break;
13040 case CPP_MINUS:
13041 reduc_code = MINUS_EXPR;
13042 break;
13043 case CPP_AND:
13044 reduc_code = BIT_AND_EXPR;
13045 break;
13046 case CPP_XOR:
13047 reduc_code = BIT_XOR_EXPR;
13048 break;
13049 case CPP_OR:
13050 reduc_code = BIT_IOR_EXPR;
13051 break;
13052 case CPP_AND_AND:
13053 reduc_code = TRUTH_ANDIF_EXPR;
13054 break;
13055 case CPP_OR_OR:
13056 reduc_code = TRUTH_ORIF_EXPR;
13057 break;
13058 case CPP_NAME:
13059 const char *p;
13060 p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
13061 if (strcmp (p, "min") == 0)
13062 {
13063 reduc_code = MIN_EXPR;
13064 break;
13065 }
13066 if (strcmp (p, "max") == 0)
13067 {
13068 reduc_code = MAX_EXPR;
13069 break;
13070 }
13071 reduc_id = c_parser_peek_token (parser)->value;
953ff289 13072 break;
953ff289 13073 default:
acf0174b
JJ
13074 c_parser_error (parser,
13075 "expected %<+%>, %<*%>, %<-%>, %<&%>, "
13076 "%<^%>, %<|%>, %<&&%>, %<||%>, %<min%> or identifier");
13077 goto fail;
953ff289
DN
13078 }
13079
acf0174b
JJ
13080 tree orig_reduc_id, reduc_decl;
13081 orig_reduc_id = reduc_id;
13082 reduc_id = c_omp_reduction_id (reduc_code, reduc_id);
13083 reduc_decl = c_omp_reduction_decl (reduc_id);
13084 c_parser_consume_token (parser);
953ff289 13085
acf0174b
JJ
13086 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
13087 goto fail;
c2255bc4 13088
acf0174b
JJ
13089 while (true)
13090 {
13091 location_t loc = c_parser_peek_token (parser)->location;
13092 struct c_type_name *ctype = c_parser_type_name (parser);
13093 if (ctype != NULL)
13094 {
13095 type = groktypename (ctype, NULL, NULL);
13096 if (type == error_mark_node)
13097 ;
13098 else if ((INTEGRAL_TYPE_P (type)
13099 || TREE_CODE (type) == REAL_TYPE
13100 || TREE_CODE (type) == COMPLEX_TYPE)
13101 && orig_reduc_id == NULL_TREE)
13102 error_at (loc, "predeclared arithmetic type in "
13103 "%<#pragma omp declare reduction%>");
13104 else if (TREE_CODE (type) == FUNCTION_TYPE
13105 || TREE_CODE (type) == ARRAY_TYPE)
13106 error_at (loc, "function or array type in "
13107 "%<#pragma omp declare reduction%>");
13108 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
13109 error_at (loc, "const, volatile or restrict qualified type in "
13110 "%<#pragma omp declare reduction%>");
13111 else
13112 {
13113 tree t;
13114 for (t = DECL_INITIAL (reduc_decl); t; t = TREE_CHAIN (t))
13115 if (comptypes (TREE_PURPOSE (t), type))
13116 {
13117 error_at (loc, "redeclaration of %qs "
13118 "%<#pragma omp declare reduction%> for "
13119 "type %qT",
13120 IDENTIFIER_POINTER (reduc_id)
13121 + sizeof ("omp declare reduction ") - 1,
13122 type);
13123 location_t ploc
13124 = DECL_SOURCE_LOCATION (TREE_VEC_ELT (TREE_VALUE (t),
13125 0));
13126 error_at (ploc, "previous %<#pragma omp declare "
13127 "reduction%>");
13128 break;
13129 }
13130 if (t == NULL_TREE)
13131 types.safe_push (type);
13132 }
13133 if (c_parser_next_token_is (parser, CPP_COMMA))
13134 c_parser_consume_token (parser);
13135 else
13136 break;
13137 }
13138 else
13139 break;
13140 }
953ff289 13141
acf0174b
JJ
13142 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>")
13143 || types.is_empty ())
13144 {
13145 fail:
13146 clauses.release ();
13147 types.release ();
13148 while (true)
13149 {
13150 c_token *token = c_parser_peek_token (parser);
13151 if (token->type == CPP_EOF || token->type == CPP_PRAGMA_EOL)
13152 break;
13153 c_parser_consume_token (parser);
13154 }
13155 c_parser_skip_to_pragma_eol (parser);
13156 return;
13157 }
953ff289 13158
acf0174b
JJ
13159 if (types.length () > 1)
13160 {
13161 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
13162 {
13163 c_token *token = c_parser_peek_token (parser);
13164 if (token->type == CPP_EOF)
13165 goto fail;
13166 clauses.safe_push (*token);
13167 c_parser_consume_token (parser);
13168 }
13169 clauses.safe_push (*c_parser_peek_token (parser));
13170 c_parser_skip_to_pragma_eol (parser);
953ff289 13171
acf0174b
JJ
13172 /* Make sure nothing tries to read past the end of the tokens. */
13173 c_token eof_token;
13174 memset (&eof_token, 0, sizeof (eof_token));
13175 eof_token.type = CPP_EOF;
13176 clauses.safe_push (eof_token);
13177 clauses.safe_push (eof_token);
13178 }
953ff289 13179
acf0174b
JJ
13180 int errs = errorcount;
13181 FOR_EACH_VEC_ELT (types, i, type)
13182 {
13183 tokens_avail = parser->tokens_avail;
13184 gcc_assert (parser->tokens == &parser->tokens_buf[0]);
13185 if (!clauses.is_empty ())
13186 {
13187 parser->tokens = clauses.address ();
13188 parser->tokens_avail = clauses.length ();
13189 parser->in_pragma = true;
13190 }
953ff289 13191
acf0174b
JJ
13192 bool nested = current_function_decl != NULL_TREE;
13193 if (nested)
13194 c_push_function_context ();
13195 tree fndecl = build_decl (BUILTINS_LOCATION, FUNCTION_DECL,
13196 reduc_id, default_function_type);
13197 current_function_decl = fndecl;
13198 allocate_struct_function (fndecl, true);
13199 push_scope ();
13200 tree stmt = push_stmt_list ();
13201 /* Intentionally BUILTINS_LOCATION, so that -Wshadow doesn't
13202 warn about these. */
13203 tree omp_out = build_decl (BUILTINS_LOCATION, VAR_DECL,
13204 get_identifier ("omp_out"), type);
13205 DECL_ARTIFICIAL (omp_out) = 1;
13206 DECL_CONTEXT (omp_out) = fndecl;
13207 pushdecl (omp_out);
13208 tree omp_in = build_decl (BUILTINS_LOCATION, VAR_DECL,
13209 get_identifier ("omp_in"), type);
13210 DECL_ARTIFICIAL (omp_in) = 1;
13211 DECL_CONTEXT (omp_in) = fndecl;
13212 pushdecl (omp_in);
13213 struct c_expr combiner = c_parser_expression (parser);
13214 struct c_expr initializer;
13215 tree omp_priv = NULL_TREE, omp_orig = NULL_TREE;
13216 bool bad = false;
13217 initializer.value = error_mark_node;
13218 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
13219 bad = true;
13220 else if (c_parser_next_token_is (parser, CPP_NAME)
13221 && strcmp (IDENTIFIER_POINTER
13222 (c_parser_peek_token (parser)->value),
13223 "initializer") == 0)
13224 {
13225 c_parser_consume_token (parser);
13226 pop_scope ();
13227 push_scope ();
13228 omp_priv = build_decl (BUILTINS_LOCATION, VAR_DECL,
13229 get_identifier ("omp_priv"), type);
13230 DECL_ARTIFICIAL (omp_priv) = 1;
13231 DECL_INITIAL (omp_priv) = error_mark_node;
13232 DECL_CONTEXT (omp_priv) = fndecl;
13233 pushdecl (omp_priv);
13234 omp_orig = build_decl (BUILTINS_LOCATION, VAR_DECL,
13235 get_identifier ("omp_orig"), type);
13236 DECL_ARTIFICIAL (omp_orig) = 1;
13237 DECL_CONTEXT (omp_orig) = fndecl;
13238 pushdecl (omp_orig);
13239 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
13240 bad = true;
13241 else if (!c_parser_next_token_is (parser, CPP_NAME))
13242 {
13243 c_parser_error (parser, "expected %<omp_priv%> or "
13244 "function-name");
13245 bad = true;
13246 }
13247 else if (strcmp (IDENTIFIER_POINTER
13248 (c_parser_peek_token (parser)->value),
13249 "omp_priv") != 0)
13250 {
13251 if (c_parser_peek_2nd_token (parser)->type != CPP_OPEN_PAREN
13252 || c_parser_peek_token (parser)->id_kind != C_ID_ID)
13253 {
13254 c_parser_error (parser, "expected function-name %<(%>");
13255 bad = true;
13256 }
13257 else
13258 initializer = c_parser_postfix_expression (parser);
13259 if (initializer.value
13260 && TREE_CODE (initializer.value) == CALL_EXPR)
13261 {
13262 int j;
13263 tree c = initializer.value;
13264 for (j = 0; j < call_expr_nargs (c); j++)
13265 if (TREE_CODE (CALL_EXPR_ARG (c, j)) == ADDR_EXPR
13266 && TREE_OPERAND (CALL_EXPR_ARG (c, j), 0) == omp_priv)
13267 break;
13268 if (j == call_expr_nargs (c))
13269 error ("one of the initializer call arguments should be "
13270 "%<&omp_priv%>");
13271 }
13272 }
13273 else
13274 {
13275 c_parser_consume_token (parser);
13276 if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
13277 bad = true;
13278 else
13279 {
13280 tree st = push_stmt_list ();
13281 start_init (omp_priv, NULL_TREE, 0);
13282 location_t loc = c_parser_peek_token (parser)->location;
13283 struct c_expr init = c_parser_initializer (parser);
13284 finish_init ();
13285 finish_decl (omp_priv, loc, init.value,
13286 init.original_type, NULL_TREE);
13287 pop_stmt_list (st);
13288 }
13289 }
13290 if (!bad
13291 && !c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
13292 bad = true;
13293 }
c2255bc4 13294
acf0174b
JJ
13295 if (!bad)
13296 {
13297 c_parser_skip_to_pragma_eol (parser);
a68ab351 13298
acf0174b
JJ
13299 tree t = tree_cons (type, make_tree_vec (omp_priv ? 6 : 3),
13300 DECL_INITIAL (reduc_decl));
13301 DECL_INITIAL (reduc_decl) = t;
13302 DECL_SOURCE_LOCATION (omp_out) = rloc;
13303 TREE_VEC_ELT (TREE_VALUE (t), 0) = omp_out;
13304 TREE_VEC_ELT (TREE_VALUE (t), 1) = omp_in;
13305 TREE_VEC_ELT (TREE_VALUE (t), 2) = combiner.value;
13306 walk_tree (&combiner.value, c_check_omp_declare_reduction_r,
13307 &TREE_VEC_ELT (TREE_VALUE (t), 0), NULL);
13308 if (omp_priv)
13309 {
13310 DECL_SOURCE_LOCATION (omp_priv) = rloc;
13311 TREE_VEC_ELT (TREE_VALUE (t), 3) = omp_priv;
13312 TREE_VEC_ELT (TREE_VALUE (t), 4) = omp_orig;
13313 TREE_VEC_ELT (TREE_VALUE (t), 5) = initializer.value;
13314 walk_tree (&initializer.value, c_check_omp_declare_reduction_r,
13315 &TREE_VEC_ELT (TREE_VALUE (t), 3), NULL);
13316 walk_tree (&DECL_INITIAL (omp_priv),
13317 c_check_omp_declare_reduction_r,
13318 &TREE_VEC_ELT (TREE_VALUE (t), 3), NULL);
13319 }
13320 }
a68ab351 13321
acf0174b
JJ
13322 pop_stmt_list (stmt);
13323 pop_scope ();
13324 if (cfun->language != NULL)
13325 {
13326 ggc_free (cfun->language);
13327 cfun->language = NULL;
13328 }
13329 set_cfun (NULL);
13330 current_function_decl = NULL_TREE;
13331 if (nested)
13332 c_pop_function_context ();
a68ab351 13333
acf0174b
JJ
13334 if (!clauses.is_empty ())
13335 {
13336 parser->tokens = &parser->tokens_buf[0];
13337 parser->tokens_avail = tokens_avail;
13338 }
13339 if (bad)
13340 goto fail;
13341 if (errs != errorcount)
13342 break;
13343 }
a68ab351 13344
acf0174b
JJ
13345 clauses.release ();
13346 types.release ();
a68ab351
JJ
13347}
13348
953ff289 13349
acf0174b
JJ
13350/* OpenMP 4.0
13351 #pragma omp declare simd declare-simd-clauses[optseq] new-line
13352 #pragma omp declare reduction (reduction-id : typename-list : expression) \
13353 initializer-clause[opt] new-line
13354 #pragma omp declare target new-line */
20906c66
JJ
13355
13356static void
acf0174b 13357c_parser_omp_declare (c_parser *parser, enum pragma_context context)
20906c66 13358{
20906c66 13359 c_parser_consume_pragma (parser);
acf0174b
JJ
13360 if (c_parser_next_token_is (parser, CPP_NAME))
13361 {
13362 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
13363 if (strcmp (p, "simd") == 0)
13364 {
13365 /* c_parser_consume_token (parser); done in
13366 c_parser_omp_declare_simd. */
13367 c_parser_omp_declare_simd (parser, context);
13368 return;
13369 }
13370 if (strcmp (p, "reduction") == 0)
13371 {
13372 c_parser_consume_token (parser);
13373 c_parser_omp_declare_reduction (parser, context);
13374 return;
13375 }
6d7f7e0a
TB
13376 if (!flag_openmp) /* flag_openmp_simd */
13377 {
13378 c_parser_skip_to_pragma_eol (parser);
13379 return;
13380 }
acf0174b
JJ
13381 if (strcmp (p, "target") == 0)
13382 {
13383 c_parser_consume_token (parser);
13384 c_parser_omp_declare_target (parser);
13385 return;
13386 }
13387 }
20906c66 13388
acf0174b
JJ
13389 c_parser_error (parser, "expected %<simd%> or %<reduction%> "
13390 "or %<target%>");
13391 c_parser_skip_to_pragma_eol (parser);
20906c66
JJ
13392}
13393
953ff289
DN
13394/* Main entry point to parsing most OpenMP pragmas. */
13395
13396static void
13397c_parser_omp_construct (c_parser *parser)
13398{
13399 enum pragma_kind p_kind;
13400 location_t loc;
13401 tree stmt;
acf0174b
JJ
13402 char p_name[sizeof "#pragma omp teams distribute parallel for simd"];
13403 omp_clause_mask mask (0);
953ff289
DN
13404
13405 loc = c_parser_peek_token (parser)->location;
13406 p_kind = c_parser_peek_token (parser)->pragma_kind;
13407 c_parser_consume_pragma (parser);
13408
13409 switch (p_kind)
13410 {
13411 case PRAGMA_OMP_ATOMIC:
c2255bc4 13412 c_parser_omp_atomic (loc, parser);
953ff289
DN
13413 return;
13414 case PRAGMA_OMP_CRITICAL:
c2255bc4 13415 stmt = c_parser_omp_critical (loc, parser);
953ff289 13416 break;
acf0174b
JJ
13417 case PRAGMA_OMP_DISTRIBUTE:
13418 strcpy (p_name, "#pragma omp");
13419 stmt = c_parser_omp_distribute (loc, parser, p_name, mask, NULL);
13420 break;
953ff289 13421 case PRAGMA_OMP_FOR:
acf0174b
JJ
13422 strcpy (p_name, "#pragma omp");
13423 stmt = c_parser_omp_for (loc, parser, p_name, mask, NULL);
953ff289
DN
13424 break;
13425 case PRAGMA_OMP_MASTER:
c2255bc4 13426 stmt = c_parser_omp_master (loc, parser);
953ff289
DN
13427 break;
13428 case PRAGMA_OMP_ORDERED:
c2255bc4 13429 stmt = c_parser_omp_ordered (loc, parser);
953ff289
DN
13430 break;
13431 case PRAGMA_OMP_PARALLEL:
acf0174b
JJ
13432 strcpy (p_name, "#pragma omp");
13433 stmt = c_parser_omp_parallel (loc, parser, p_name, mask, NULL);
953ff289
DN
13434 break;
13435 case PRAGMA_OMP_SECTIONS:
acf0174b
JJ
13436 strcpy (p_name, "#pragma omp");
13437 stmt = c_parser_omp_sections (loc, parser, p_name, mask, NULL);
13438 break;
13439 case PRAGMA_OMP_SIMD:
13440 strcpy (p_name, "#pragma omp");
13441 stmt = c_parser_omp_simd (loc, parser, p_name, mask, NULL);
953ff289
DN
13442 break;
13443 case PRAGMA_OMP_SINGLE:
c2255bc4 13444 stmt = c_parser_omp_single (loc, parser);
953ff289 13445 break;
a68ab351 13446 case PRAGMA_OMP_TASK:
c2255bc4 13447 stmt = c_parser_omp_task (loc, parser);
a68ab351 13448 break;
acf0174b
JJ
13449 case PRAGMA_OMP_TASKGROUP:
13450 stmt = c_parser_omp_taskgroup (parser);
13451 break;
13452 case PRAGMA_OMP_TEAMS:
13453 strcpy (p_name, "#pragma omp");
13454 stmt = c_parser_omp_teams (loc, parser, p_name, mask, NULL);
13455 break;
953ff289
DN
13456 default:
13457 gcc_unreachable ();
13458 }
13459
13460 if (stmt)
c2255bc4 13461 gcc_assert (EXPR_LOCATION (stmt) != UNKNOWN_LOCATION);
953ff289
DN
13462}
13463
13464
13465/* OpenMP 2.5:
13466 # pragma omp threadprivate (variable-list) */
13467
13468static void
13469c_parser_omp_threadprivate (c_parser *parser)
13470{
13471 tree vars, t;
c2255bc4 13472 location_t loc;
953ff289
DN
13473
13474 c_parser_consume_pragma (parser);
c2255bc4 13475 loc = c_parser_peek_token (parser)->location;
d75d71e0 13476 vars = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_ERROR, NULL);
953ff289 13477
953ff289
DN
13478 /* Mark every variable in VARS to be assigned thread local storage. */
13479 for (t = vars; t; t = TREE_CHAIN (t))
13480 {
13481 tree v = TREE_PURPOSE (t);
13482
c2255bc4
AH
13483 /* FIXME diagnostics: Ideally we should keep individual
13484 locations for all the variables in the var list to make the
13485 following errors more precise. Perhaps
13486 c_parser_omp_var_list_parens() should construct a list of
13487 locations to go along with the var list. */
13488
953ff289
DN
13489 /* If V had already been marked threadprivate, it doesn't matter
13490 whether it had been used prior to this point. */
5df27e4a 13491 if (TREE_CODE (v) != VAR_DECL)
c2255bc4 13492 error_at (loc, "%qD is not a variable", v);
5df27e4a 13493 else if (TREE_USED (v) && !C_DECL_THREADPRIVATE_P (v))
c2255bc4 13494 error_at (loc, "%qE declared %<threadprivate%> after first use", v);
953ff289 13495 else if (! TREE_STATIC (v) && ! DECL_EXTERNAL (v))
c2255bc4 13496 error_at (loc, "automatic variable %qE cannot be %<threadprivate%>", v);
5df27e4a
JJ
13497 else if (TREE_TYPE (v) == error_mark_node)
13498 ;
953ff289 13499 else if (! COMPLETE_TYPE_P (TREE_TYPE (v)))
c2255bc4 13500 error_at (loc, "%<threadprivate%> %qE has incomplete type", v);
953ff289
DN
13501 else
13502 {
13503 if (! DECL_THREAD_LOCAL_P (v))
13504 {
13505 DECL_TLS_MODEL (v) = decl_default_tls_model (v);
13506 /* If rtl has been already set for this var, call
13507 make_decl_rtl once again, so that encode_section_info
13508 has a chance to look at the new decl flags. */
13509 if (DECL_RTL_SET_P (v))
13510 make_decl_rtl (v);
13511 }
13512 C_DECL_THREADPRIVATE_P (v) = 1;
13513 }
13514 }
13515
13516 c_parser_skip_to_pragma_eol (parser);
13517}
c02065fc
AH
13518\f
13519/* Cilk Plus <#pragma simd> parsing routines. */
13520
13521/* Helper function for c_parser_pragma. Perform some sanity checking
13522 for <#pragma simd> constructs. Returns FALSE if there was a
13523 problem. */
13524
13525static bool
13526c_parser_cilk_verify_simd (c_parser *parser,
13527 enum pragma_context context)
13528{
b72271b9 13529 if (!flag_cilkplus)
c02065fc
AH
13530 {
13531 warning (0, "pragma simd ignored because -fcilkplus is not enabled");
13532 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
13533 return false;
13534 }
13535 if (context == pragma_external)
13536 {
13537 c_parser_error (parser,"pragma simd must be inside a function");
13538 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
13539 return false;
13540 }
13541 return true;
13542}
13543
13544/* Cilk Plus:
41958c28
BI
13545 This function is shared by SIMD-enabled functions and #pragma simd.
13546 If IS_SIMD_FN is true then it is parsing a SIMD-enabled function and
13547 CLAUSES is unused. The main purpose of this function is to parse a
13548 vectorlength attribute or clause and check for parse errors.
13549 When IS_SIMD_FN is true then the function is merely caching the tokens
13550 in PARSER->CILK_SIMD_FN_TOKENS. If errors are found then the token
13551 cache is cleared since there is no reason to continue.
13552 Syntax:
13553 vectorlength ( constant-expression ) */
c02065fc
AH
13554
13555static tree
41958c28
BI
13556c_parser_cilk_clause_vectorlength (c_parser *parser, tree clauses,
13557 bool is_simd_fn)
c02065fc 13558{
41958c28
BI
13559 if (is_simd_fn)
13560 check_no_duplicate_clause (clauses, OMP_CLAUSE_SIMDLEN, "vectorlength");
13561 else
c02065fc
AH
13562 /* The vectorlength clause behaves exactly like OpenMP's safelen
13563 clause. Represent it in OpenMP terms. */
41958c28 13564 check_no_duplicate_clause (clauses, OMP_CLAUSE_SAFELEN, "vectorlength");
c02065fc
AH
13565
13566 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
13567 return clauses;
13568
13569 location_t loc = c_parser_peek_token (parser)->location;
13570 tree expr = c_parser_expr_no_commas (parser, NULL).value;
13571 expr = c_fully_fold (expr, false, NULL);
13572
41958c28
BI
13573 /* If expr is an error_mark_node then the above function would have
13574 emitted an error. No reason to do it twice. */
13575 if (expr == error_mark_node)
13576 ;
13577 else if (!TREE_TYPE (expr)
13578 || !TREE_CONSTANT (expr)
13579 || !INTEGRAL_TYPE_P (TREE_TYPE (expr)))
13580
13581 error_at (loc, "vectorlength must be an integer constant");
c02065fc
AH
13582 else if (exact_log2 (TREE_INT_CST_LOW (expr)) == -1)
13583 error_at (loc, "vectorlength must be a power of 2");
13584 else
13585 {
41958c28
BI
13586 if (is_simd_fn)
13587 {
13588 tree u = build_omp_clause (loc, OMP_CLAUSE_SIMDLEN);
13589 OMP_CLAUSE_SIMDLEN_EXPR (u) = expr;
13590 OMP_CLAUSE_CHAIN (u) = clauses;
13591 clauses = u;
13592 }
13593 else
13594 {
13595 tree u = build_omp_clause (loc, OMP_CLAUSE_SAFELEN);
13596 OMP_CLAUSE_SAFELEN_EXPR (u) = expr;
13597 OMP_CLAUSE_CHAIN (u) = clauses;
13598 clauses = u;
13599 }
c02065fc
AH
13600 }
13601
13602 c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
13603
13604 return clauses;
13605}
13606
13607/* Cilk Plus:
13608 linear ( simd-linear-variable-list )
13609
13610 simd-linear-variable-list:
13611 simd-linear-variable
13612 simd-linear-variable-list , simd-linear-variable
13613
13614 simd-linear-variable:
13615 id-expression
13616 id-expression : simd-linear-step
13617
13618 simd-linear-step:
13619 conditional-expression */
13620
13621static tree
13622c_parser_cilk_clause_linear (c_parser *parser, tree clauses)
13623{
13624 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
13625 return clauses;
13626
13627 location_t loc = c_parser_peek_token (parser)->location;
13628
13629 if (c_parser_next_token_is_not (parser, CPP_NAME)
13630 || c_parser_peek_token (parser)->id_kind != C_ID_ID)
13631 c_parser_error (parser, "expected identifier");
13632
13633 while (c_parser_next_token_is (parser, CPP_NAME)
13634 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
13635 {
13636 tree var = lookup_name (c_parser_peek_token (parser)->value);
13637
13638 if (var == NULL)
13639 {
13640 undeclared_variable (c_parser_peek_token (parser)->location,
13641 c_parser_peek_token (parser)->value);
13642 c_parser_consume_token (parser);
13643 }
13644 else if (var == error_mark_node)
13645 c_parser_consume_token (parser);
13646 else
13647 {
13648 tree step = integer_one_node;
13649
13650 /* Parse the linear step if present. */
13651 if (c_parser_peek_2nd_token (parser)->type == CPP_COLON)
13652 {
13653 c_parser_consume_token (parser);
13654 c_parser_consume_token (parser);
13655
13656 tree expr = c_parser_expr_no_commas (parser, NULL).value;
13657 expr = c_fully_fold (expr, false, NULL);
13658
13659 if (TREE_TYPE (expr)
13660 && INTEGRAL_TYPE_P (TREE_TYPE (expr))
13661 && (TREE_CONSTANT (expr)
13662 || DECL_P (expr)))
13663 step = expr;
13664 else
13665 c_parser_error (parser,
13666 "step size must be an integer constant "
13667 "expression or an integer variable");
13668 }
13669 else
13670 c_parser_consume_token (parser);
13671
13672 /* Use OMP_CLAUSE_LINEAR, which has the same semantics. */
13673 tree u = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
13674 OMP_CLAUSE_DECL (u) = var;
13675 OMP_CLAUSE_LINEAR_STEP (u) = step;
13676 OMP_CLAUSE_CHAIN (u) = clauses;
13677 clauses = u;
13678 }
13679
13680 if (c_parser_next_token_is_not (parser, CPP_COMMA))
13681 break;
13682
13683 c_parser_consume_token (parser);
13684 }
13685
13686 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
13687
13688 return clauses;
13689}
13690
13691/* Returns the name of the next clause. If the clause is not
13692 recognized SIMD_OMP_CLAUSE_NONE is returned and the next token is
13693 not consumed. Otherwise, the appropriate pragma_simd_clause is
13694 returned and the token is consumed. */
13695
41958c28 13696static pragma_omp_clause
c02065fc
AH
13697c_parser_cilk_clause_name (c_parser *parser)
13698{
41958c28 13699 pragma_omp_clause result;
c02065fc
AH
13700 c_token *token = c_parser_peek_token (parser);
13701
13702 if (!token->value || token->type != CPP_NAME)
13703 return PRAGMA_CILK_CLAUSE_NONE;
13704
13705 const char *p = IDENTIFIER_POINTER (token->value);
13706
13707 if (!strcmp (p, "vectorlength"))
13708 result = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
13709 else if (!strcmp (p, "linear"))
13710 result = PRAGMA_CILK_CLAUSE_LINEAR;
13711 else if (!strcmp (p, "private"))
13712 result = PRAGMA_CILK_CLAUSE_PRIVATE;
13713 else if (!strcmp (p, "firstprivate"))
13714 result = PRAGMA_CILK_CLAUSE_FIRSTPRIVATE;
13715 else if (!strcmp (p, "lastprivate"))
13716 result = PRAGMA_CILK_CLAUSE_LASTPRIVATE;
13717 else if (!strcmp (p, "reduction"))
13718 result = PRAGMA_CILK_CLAUSE_REDUCTION;
13719 else
13720 return PRAGMA_CILK_CLAUSE_NONE;
13721
13722 c_parser_consume_token (parser);
13723 return result;
13724}
13725
13726/* Parse all #<pragma simd> clauses. Return the list of clauses
13727 found. */
13728
13729static tree
13730c_parser_cilk_all_clauses (c_parser *parser)
13731{
13732 tree clauses = NULL;
13733
13734 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
13735 {
41958c28 13736 pragma_omp_clause c_kind;
c02065fc
AH
13737
13738 c_kind = c_parser_cilk_clause_name (parser);
13739
13740 switch (c_kind)
13741 {
13742 case PRAGMA_CILK_CLAUSE_VECTORLENGTH:
41958c28 13743 clauses = c_parser_cilk_clause_vectorlength (parser, clauses, false);
c02065fc
AH
13744 break;
13745 case PRAGMA_CILK_CLAUSE_LINEAR:
13746 clauses = c_parser_cilk_clause_linear (parser, clauses);
13747 break;
13748 case PRAGMA_CILK_CLAUSE_PRIVATE:
13749 /* Use the OpenMP counterpart. */
13750 clauses = c_parser_omp_clause_private (parser, clauses);
13751 break;
13752 case PRAGMA_CILK_CLAUSE_FIRSTPRIVATE:
13753 /* Use the OpenMP counterpart. */
13754 clauses = c_parser_omp_clause_firstprivate (parser, clauses);
13755 break;
13756 case PRAGMA_CILK_CLAUSE_LASTPRIVATE:
13757 /* Use the OpenMP counterpart. */
13758 clauses = c_parser_omp_clause_lastprivate (parser, clauses);
13759 break;
13760 case PRAGMA_CILK_CLAUSE_REDUCTION:
13761 /* Use the OpenMP counterpart. */
13762 clauses = c_parser_omp_clause_reduction (parser, clauses);
13763 break;
13764 default:
13765 c_parser_error (parser, "expected %<#pragma simd%> clause");
13766 goto saw_error;
13767 }
13768 }
13769
13770 saw_error:
13771 c_parser_skip_to_pragma_eol (parser);
13772 return c_finish_cilk_clauses (clauses);
13773}
13774
13775/* Main entry point for parsing Cilk Plus <#pragma simd> for
13776 loops. */
953ff289 13777
c02065fc 13778static void
e7bd1de1 13779c_parser_cilk_simd (c_parser *parser)
c02065fc 13780{
c02065fc
AH
13781 tree clauses = c_parser_cilk_all_clauses (parser);
13782 tree block = c_begin_compound_stmt (true);
13783 location_t loc = c_parser_peek_token (parser)->location;
13784 c_parser_omp_for_loop (loc, parser, CILK_SIMD, clauses, NULL);
13785 block = c_end_compound_stmt (loc, block, true);
13786 add_stmt (block);
13787}
13788\f
0a35513e
AH
13789/* Parse a transaction attribute (GCC Extension).
13790
13791 transaction-attribute:
13792 attributes
13793 [ [ any-word ] ]
13794
13795 The transactional memory language description is written for C++,
13796 and uses the C++0x attribute syntax. For compatibility, allow the
13797 bracket style for transactions in C as well. */
13798
13799static tree
13800c_parser_transaction_attributes (c_parser *parser)
13801{
13802 tree attr_name, attr = NULL;
13803
13804 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
13805 return c_parser_attributes (parser);
13806
13807 if (!c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
13808 return NULL_TREE;
13809 c_parser_consume_token (parser);
13810 if (!c_parser_require (parser, CPP_OPEN_SQUARE, "expected %<[%>"))
13811 goto error1;
13812
13813 attr_name = c_parser_attribute_any_word (parser);
13814 if (attr_name)
13815 {
13816 c_parser_consume_token (parser);
13817 attr = build_tree_list (attr_name, NULL_TREE);
13818 }
13819 else
13820 c_parser_error (parser, "expected identifier");
13821
13822 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, "expected %<]%>");
13823 error1:
13824 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, "expected %<]%>");
13825 return attr;
13826}
13827
13828/* Parse a __transaction_atomic or __transaction_relaxed statement
13829 (GCC Extension).
13830
13831 transaction-statement:
13832 __transaction_atomic transaction-attribute[opt] compound-statement
13833 __transaction_relaxed compound-statement
13834
13835 Note that the only valid attribute is: "outer".
13836*/
13837
13838static tree
13839c_parser_transaction (c_parser *parser, enum rid keyword)
13840{
13841 unsigned int old_in = parser->in_transaction;
13842 unsigned int this_in = 1, new_in;
13843 location_t loc = c_parser_peek_token (parser)->location;
13844 tree stmt, attrs;
13845
13846 gcc_assert ((keyword == RID_TRANSACTION_ATOMIC
13847 || keyword == RID_TRANSACTION_RELAXED)
13848 && c_parser_next_token_is_keyword (parser, keyword));
13849 c_parser_consume_token (parser);
13850
13851 if (keyword == RID_TRANSACTION_RELAXED)
13852 this_in |= TM_STMT_ATTR_RELAXED;
13853 else
13854 {
13855 attrs = c_parser_transaction_attributes (parser);
13856 if (attrs)
13857 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
13858 }
13859
13860 /* Keep track if we're in the lexical scope of an outer transaction. */
13861 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
13862
13863 parser->in_transaction = new_in;
13864 stmt = c_parser_compound_statement (parser);
13865 parser->in_transaction = old_in;
13866
13867 if (flag_tm)
13868 stmt = c_finish_transaction (loc, stmt, this_in);
13869 else
13870 error_at (loc, (keyword == RID_TRANSACTION_ATOMIC ?
13871 "%<__transaction_atomic%> without transactional memory support enabled"
13872 : "%<__transaction_relaxed %> "
13873 "without transactional memory support enabled"));
13874
13875 return stmt;
13876}
13877
13878/* Parse a __transaction_atomic or __transaction_relaxed expression
13879 (GCC Extension).
13880
13881 transaction-expression:
13882 __transaction_atomic ( expression )
13883 __transaction_relaxed ( expression )
13884*/
13885
13886static struct c_expr
13887c_parser_transaction_expression (c_parser *parser, enum rid keyword)
13888{
13889 struct c_expr ret;
13890 unsigned int old_in = parser->in_transaction;
13891 unsigned int this_in = 1;
13892 location_t loc = c_parser_peek_token (parser)->location;
13893 tree attrs;
13894
13895 gcc_assert ((keyword == RID_TRANSACTION_ATOMIC
13896 || keyword == RID_TRANSACTION_RELAXED)
13897 && c_parser_next_token_is_keyword (parser, keyword));
13898 c_parser_consume_token (parser);
13899
13900 if (keyword == RID_TRANSACTION_RELAXED)
13901 this_in |= TM_STMT_ATTR_RELAXED;
13902 else
13903 {
13904 attrs = c_parser_transaction_attributes (parser);
13905 if (attrs)
13906 this_in |= parse_tm_stmt_attr (attrs, 0);
13907 }
13908
13909 parser->in_transaction = this_in;
69b76518 13910 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
0a35513e
AH
13911 {
13912 tree expr = c_parser_expression (parser).value;
13913 ret.original_type = TREE_TYPE (expr);
13914 ret.value = build1 (TRANSACTION_EXPR, ret.original_type, expr);
13915 if (this_in & TM_STMT_ATTR_RELAXED)
13916 TRANSACTION_EXPR_RELAXED (ret.value) = 1;
13917 SET_EXPR_LOCATION (ret.value, loc);
13918 ret.original_code = TRANSACTION_EXPR;
69b76518
TR
13919 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
13920 {
13921 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
13922 goto error;
13923 }
0a35513e
AH
13924 }
13925 else
13926 {
69b76518 13927 error:
0a35513e
AH
13928 ret.value = error_mark_node;
13929 ret.original_code = ERROR_MARK;
13930 ret.original_type = NULL;
13931 }
13932 parser->in_transaction = old_in;
13933
13934 if (!flag_tm)
13935 error_at (loc, (keyword == RID_TRANSACTION_ATOMIC ?
13936 "%<__transaction_atomic%> without transactional memory support enabled"
13937 : "%<__transaction_relaxed %> "
13938 "without transactional memory support enabled"));
13939
13940 return ret;
13941}
13942
13943/* Parse a __transaction_cancel statement (GCC Extension).
13944
13945 transaction-cancel-statement:
13946 __transaction_cancel transaction-attribute[opt] ;
13947
13948 Note that the only valid attribute is "outer".
13949*/
13950
13951static tree
acf0174b 13952c_parser_transaction_cancel (c_parser *parser)
0a35513e
AH
13953{
13954 location_t loc = c_parser_peek_token (parser)->location;
13955 tree attrs;
13956 bool is_outer = false;
13957
13958 gcc_assert (c_parser_next_token_is_keyword (parser, RID_TRANSACTION_CANCEL));
13959 c_parser_consume_token (parser);
13960
13961 attrs = c_parser_transaction_attributes (parser);
13962 if (attrs)
13963 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
13964
13965 if (!flag_tm)
13966 {
13967 error_at (loc, "%<__transaction_cancel%> without "
13968 "transactional memory support enabled");
13969 goto ret_error;
13970 }
13971 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
13972 {
13973 error_at (loc, "%<__transaction_cancel%> within a "
13974 "%<__transaction_relaxed%>");
13975 goto ret_error;
13976 }
13977 else if (is_outer)
13978 {
13979 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
13980 && !is_tm_may_cancel_outer (current_function_decl))
13981 {
13982 error_at (loc, "outer %<__transaction_cancel%> not "
13983 "within outer %<__transaction_atomic%>");
13984 error_at (loc, " or a %<transaction_may_cancel_outer%> function");
13985 goto ret_error;
13986 }
13987 }
13988 else if (parser->in_transaction == 0)
13989 {
13990 error_at (loc, "%<__transaction_cancel%> not within "
13991 "%<__transaction_atomic%>");
13992 goto ret_error;
13993 }
13994
13995 return add_stmt (build_tm_abort_call (loc, is_outer));
13996
13997 ret_error:
13998 return build1 (NOP_EXPR, void_type_node, error_mark_node);
13999}
bc4071dd 14000\f
27bf414c
JM
14001/* Parse a single source file. */
14002
14003void
14004c_parse_file (void)
14005{
bc4071dd
RH
14006 /* Use local storage to begin. If the first token is a pragma, parse it.
14007 If it is #pragma GCC pch_preprocess, then this will load a PCH file
14008 which will cause garbage collection. */
14009 c_parser tparser;
14010
14011 memset (&tparser, 0, sizeof tparser);
acf0174b 14012 tparser.tokens = &tparser.tokens_buf[0];
bc4071dd
RH
14013 the_parser = &tparser;
14014
14015 if (c_parser_peek_token (&tparser)->pragma_kind == PRAGMA_GCC_PCH_PREPROCESS)
14016 c_parser_pragma_pch_preprocess (&tparser);
14017
a9429e29 14018 the_parser = ggc_alloc_c_parser ();
bc4071dd 14019 *the_parser = tparser;
acf0174b
JJ
14020 if (tparser.tokens == &tparser.tokens_buf[0])
14021 the_parser->tokens = &the_parser->tokens_buf[0];
bc4071dd 14022
f9417da1
RG
14023 /* Initialize EH, if we've been told to do so. */
14024 if (flag_exceptions)
1d65f45c 14025 using_eh_for_cleanups ();
f9417da1 14026
27bf414c
JM
14027 c_parser_translation_unit (the_parser);
14028 the_parser = NULL;
14029}
14030
36536d79
BI
14031/* This function parses Cilk Plus array notation. The starting index is
14032 passed in INITIAL_INDEX and the array name is passes in ARRAY_VALUE. The
14033 return value of this function is a tree_node called VALUE_TREE of type
14034 ARRAY_NOTATION_REF. */
14035
14036static tree
14037c_parser_array_notation (location_t loc, c_parser *parser, tree initial_index,
14038 tree array_value)
14039{
14040 c_token *token = NULL;
14041 tree start_index = NULL_TREE, end_index = NULL_TREE, stride = NULL_TREE;
14042 tree value_tree = NULL_TREE, type = NULL_TREE, array_type = NULL_TREE;
14043 tree array_type_domain = NULL_TREE;
14044
14045 if (array_value == error_mark_node)
14046 {
14047 /* No need to continue. If either of these 2 were true, then an error
14048 must be emitted already. Thus, no need to emit them twice. */
14049 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
14050 return error_mark_node;
14051 }
14052
14053 array_type = TREE_TYPE (array_value);
14054 gcc_assert (array_type);
14055 type = TREE_TYPE (array_type);
14056 token = c_parser_peek_token (parser);
14057
14058 if (token->type == CPP_EOF)
14059 {
14060 c_parser_error (parser, "expected %<:%> or numeral");
14061 return value_tree;
14062 }
14063 else if (token->type == CPP_COLON)
14064 {
14065 if (!initial_index)
14066 {
14067 /* If we are here, then we have a case like this A[:]. */
14068 c_parser_consume_token (parser);
14069 if (TREE_CODE (array_type) == POINTER_TYPE)
14070 {
14071 error_at (loc, "start-index and length fields necessary for "
14072 "using array notations in pointers");
14073 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
14074 return error_mark_node;
14075 }
14076 if (TREE_CODE (array_type) == FUNCTION_TYPE)
14077 {
14078 error_at (loc, "array notations cannot be used with function "
14079 "type");
14080 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
14081 return error_mark_node;
14082 }
36536d79
BI
14083 array_type_domain = TYPE_DOMAIN (array_type);
14084
14085 if (!array_type_domain)
14086 {
14087 error_at (loc, "start-index and length fields necessary for "
14088 "using array notations in dimensionless arrays");
14089 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
14090 return error_mark_node;
14091 }
14092
14093 start_index = TYPE_MINVAL (array_type_domain);
14094 start_index = fold_build1 (CONVERT_EXPR, ptrdiff_type_node,
14095 start_index);
14096 if (!TYPE_MAXVAL (array_type_domain)
14097 || !TREE_CONSTANT (TYPE_MAXVAL (array_type_domain)))
14098 {
14099 error_at (loc, "start-index and length fields necessary for "
14100 "using array notations in variable-length arrays");
14101 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
14102 return error_mark_node;
14103 }
14104 end_index = TYPE_MAXVAL (array_type_domain);
14105 end_index = fold_build2 (PLUS_EXPR, TREE_TYPE (end_index),
14106 end_index, integer_one_node);
14107 end_index = fold_build1 (CONVERT_EXPR, ptrdiff_type_node, end_index);
14108 stride = build_int_cst (integer_type_node, 1);
14109 stride = fold_build1 (CONVERT_EXPR, ptrdiff_type_node, stride);
14110 }
14111 else if (initial_index != error_mark_node)
14112 {
14113 /* If we are here, then there should be 2 possibilities:
14114 1. Array [EXPR : EXPR]
14115 2. Array [EXPR : EXPR : EXPR]
14116 */
14117 start_index = initial_index;
14118
14119 if (TREE_CODE (array_type) == FUNCTION_TYPE)
14120 {
14121 error_at (loc, "array notations cannot be used with function "
14122 "type");
14123 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
14124 return error_mark_node;
14125 }
36536d79 14126 c_parser_consume_token (parser); /* consume the ':' */
267bac10
JM
14127 struct c_expr ce = c_parser_expression (parser);
14128 ce = convert_lvalue_to_rvalue (loc, ce, false, false);
14129 end_index = ce.value;
36536d79
BI
14130 if (!end_index || end_index == error_mark_node)
14131 {
14132 c_parser_skip_to_end_of_block_or_statement (parser);
14133 return error_mark_node;
14134 }
14135 if (c_parser_peek_token (parser)->type == CPP_COLON)
14136 {
14137 c_parser_consume_token (parser);
267bac10
JM
14138 ce = c_parser_expression (parser);
14139 ce = convert_lvalue_to_rvalue (loc, ce, false, false);
14140 stride = ce.value;
36536d79
BI
14141 if (!stride || stride == error_mark_node)
14142 {
14143 c_parser_skip_to_end_of_block_or_statement (parser);
14144 return error_mark_node;
14145 }
14146 }
14147 }
14148 else
14149 c_parser_error (parser, "expected array notation expression");
14150 }
14151 else
14152 c_parser_error (parser, "expected array notation expression");
14153
14154 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, "expected %<]%>");
14155
14156 value_tree = build_array_notation_ref (loc, array_value, start_index,
14157 end_index, stride, type);
14158 if (value_tree != error_mark_node)
14159 SET_EXPR_LOCATION (value_tree, loc);
14160 return value_tree;
14161}
14162
d4a10d0a 14163#include "gt-c-c-parser.h"
This page took 4.337983 seconds and 5 git commands to generate.