]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/c/c-parser.c
Update copyright years.
[thirdparty/gcc.git] / gcc / c / c-parser.c
CommitLineData
27bf414c 1/* Parser for C and Objective-C.
5624e564 2 Copyright (C) 1987-2015 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"
c582198b
AM
61#include "hash-map.h"
62#include "is-a.h"
63#include "plugin-api.h"
64#include "hashtab.h"
65#include "hash-set.h"
66#include "machmode.h"
67#include "hard-reg-set.h"
68#include "function.h"
69#include "ipa-ref.h"
474eccc6 70#include "cgraph.h"
68a607d8 71#include "plugin.h"
0645c1a2 72#include "omp-low.h"
9b2b7279 73#include "builtins.h"
27bf414c
JM
74
75\f
27bf414c
JM
76/* Initialization routine for this file. */
77
78void
79c_parse_init (void)
80{
81 /* The only initialization required is of the reserved word
82 identifiers. */
83 unsigned int i;
84 tree id;
eea1139b 85 int mask = 0;
27bf414c 86
36c5e70a
BE
87 /* Make sure RID_MAX hasn't grown past the 8 bits used to hold the keyword in
88 the c_token structure. */
89 gcc_assert (RID_MAX <= 255);
90
eea1139b
ILT
91 mask |= D_CXXONLY;
92 if (!flag_isoc99)
93 mask |= D_C99;
94 if (flag_no_asm)
95 {
96 mask |= D_ASM | D_EXT;
97 if (!flag_isoc99)
98 mask |= D_EXT89;
99 }
27bf414c 100 if (!c_dialect_objc ())
eea1139b 101 mask |= D_OBJC | D_CXX_OBJC;
27bf414c 102
766090c2 103 ridpointers = ggc_cleared_vec_alloc<tree> ((int) RID_MAX);
eea1139b 104 for (i = 0; i < num_c_common_reswords; i++)
27bf414c
JM
105 {
106 /* If a keyword is disabled, do not enter it into the table
107 and so create a canonical spelling that isn't a keyword. */
eea1139b
ILT
108 if (c_common_reswords[i].disable & mask)
109 {
110 if (warn_cxx_compat
111 && (c_common_reswords[i].disable & D_CXXWARN))
112 {
113 id = get_identifier (c_common_reswords[i].word);
114 C_SET_RID_CODE (id, RID_CXX_COMPAT_WARN);
115 C_IS_RESERVED_WORD (id) = 1;
116 }
117 continue;
118 }
27bf414c 119
eea1139b
ILT
120 id = get_identifier (c_common_reswords[i].word);
121 C_SET_RID_CODE (id, c_common_reswords[i].rid);
27bf414c 122 C_IS_RESERVED_WORD (id) = 1;
eea1139b 123 ridpointers [(int) c_common_reswords[i].rid] = id;
27bf414c 124 }
78a7c317
DD
125
126 for (i = 0; i < NUM_INT_N_ENTS; i++)
127 {
128 /* We always create the symbols but they aren't always supported. */
129 char name[50];
130 sprintf (name, "__int%d", int_n_data[i].bitsize);
131 id = get_identifier (xstrdup (name));
132 C_SET_RID_CODE (id, RID_FIRST_INT_N + i);
133 C_IS_RESERVED_WORD (id) = 1;
134 }
27bf414c
JM
135}
136\f
137/* The C lexer intermediates between the lexer in cpplib and c-lex.c
138 and the C parser. Unlike the C++ lexer, the parser structure
139 stores the lexer information instead of using a separate structure.
140 Identifiers are separated into ordinary identifiers, type names,
141 keywords and some other Objective-C types of identifiers, and some
142 look-ahead is maintained.
143
144 ??? It might be a good idea to lex the whole file up front (as for
145 C++). It would then be possible to share more of the C and C++
146 lexer code, if desired. */
147
27bf414c
JM
148/* More information about the type of a CPP_NAME token. */
149typedef enum c_id_kind {
150 /* An ordinary identifier. */
151 C_ID_ID,
152 /* An identifier declared as a typedef name. */
153 C_ID_TYPENAME,
154 /* An identifier declared as an Objective-C class name. */
155 C_ID_CLASSNAME,
36c5e70a
BE
156 /* An address space identifier. */
157 C_ID_ADDRSPACE,
27bf414c
JM
158 /* Not an identifier. */
159 C_ID_NONE
160} c_id_kind;
161
162/* A single C token after string literal concatenation and conversion
163 of preprocessing tokens to tokens. */
d1b38208 164typedef struct GTY (()) c_token {
27bf414c
JM
165 /* The kind of token. */
166 ENUM_BITFIELD (cpp_ttype) type : 8;
167 /* If this token is a CPP_NAME, this value indicates whether also
168 declared as some kind of type. Otherwise, it is C_ID_NONE. */
169 ENUM_BITFIELD (c_id_kind) id_kind : 8;
170 /* If this token is a keyword, this value indicates which keyword.
171 Otherwise, this value is RID_MAX. */
172 ENUM_BITFIELD (rid) keyword : 8;
bc4071dd
RH
173 /* If this token is a CPP_PRAGMA, this indicates the pragma that
174 was seen. Otherwise it is PRAGMA_NONE. */
13fa1171 175 ENUM_BITFIELD (pragma_kind) pragma_kind : 8;
27bf414c
JM
176 /* The location at which this token was found. */
177 location_t location;
8415f317
NF
178 /* The value associated with this token, if any. */
179 tree value;
27bf414c
JM
180} c_token;
181
182/* A parser structure recording information about the state and
183 context of parsing. Includes lexer information with up to two
184 tokens of look-ahead; more are not needed for C. */
d1b38208 185typedef struct GTY(()) c_parser {
27bf414c 186 /* The look-ahead tokens. */
acf0174b
JJ
187 c_token * GTY((skip)) tokens;
188 /* Buffer for look-ahead tokens. */
189 c_token tokens_buf[2];
190 /* How many look-ahead tokens are available (0, 1 or 2, or
191 more if parsing from pre-lexed tokens). */
192 unsigned int tokens_avail;
27bf414c
JM
193 /* True if a syntax error is being recovered from; false otherwise.
194 c_parser_error sets this flag. It should clear this flag when
195 enough tokens have been consumed to recover from the error. */
196 BOOL_BITFIELD error : 1;
bc4071dd
RH
197 /* True if we're processing a pragma, and shouldn't automatically
198 consume CPP_PRAGMA_EOL. */
199 BOOL_BITFIELD in_pragma : 1;
b4b56033
MLI
200 /* True if we're parsing the outermost block of an if statement. */
201 BOOL_BITFIELD in_if_block : 1;
46c2514e
TT
202 /* True if we want to lex an untranslated string. */
203 BOOL_BITFIELD lex_untranslated_string : 1;
1973201f 204
0bacb8c7 205 /* Objective-C specific parser/lexer information. */
1973201f
NP
206
207 /* True if we are in a context where the Objective-C "PQ" keywords
208 are considered keywords. */
0bacb8c7 209 BOOL_BITFIELD objc_pq_context : 1;
f05b9d93
NP
210 /* True if we are parsing a (potential) Objective-C foreach
211 statement. This is set to true after we parsed 'for (' and while
212 we wait for 'in' or ';' to decide if it's a standard C for loop or an
213 Objective-C foreach loop. */
214 BOOL_BITFIELD objc_could_be_foreach_context : 1;
0bacb8c7
TT
215 /* The following flag is needed to contextualize Objective-C lexical
216 analysis. In some cases (e.g., 'int NSObject;'), it is
217 undesirable to bind an identifier to an Objective-C class, even
218 if a class with that name exists. */
219 BOOL_BITFIELD objc_need_raw_identifier : 1;
0a35513e
AH
220 /* Nonzero if we're processing a __transaction statement. The value
221 is 1 | TM_STMT_ATTR_*. */
222 unsigned int in_transaction : 4;
668ea4b1
IS
223 /* True if we are in a context where the Objective-C "Property attribute"
224 keywords are valid. */
225 BOOL_BITFIELD objc_property_attr_context : 1;
41958c28
BI
226
227 /* Cilk Plus specific parser/lexer information. */
228
229 /* Buffer to hold all the tokens from parsing the vector attribute for the
230 SIMD-enabled functions (formerly known as elemental functions). */
231 vec <c_token, va_gc> *cilk_simd_fn_tokens;
27bf414c
JM
232} c_parser;
233
bc4071dd
RH
234
235/* The actual parser and external interface. ??? Does this need to be
236 garbage-collected? */
237
238static GTY (()) c_parser *the_parser;
239
27bf414c
JM
240/* Read in and lex a single token, storing it in *TOKEN. */
241
242static void
0bacb8c7 243c_lex_one_token (c_parser *parser, c_token *token)
27bf414c
JM
244{
245 timevar_push (TV_LEX);
bc4071dd 246
46c2514e
TT
247 token->type = c_lex_with_flags (&token->value, &token->location, NULL,
248 (parser->lex_untranslated_string
249 ? C_LEX_STRING_NO_TRANSLATE : 0));
bc4071dd
RH
250 token->id_kind = C_ID_NONE;
251 token->keyword = RID_MAX;
252 token->pragma_kind = PRAGMA_NONE;
bc4071dd 253
27bf414c
JM
254 switch (token->type)
255 {
256 case CPP_NAME:
27bf414c
JM
257 {
258 tree decl;
259
0bacb8c7
TT
260 bool objc_force_identifier = parser->objc_need_raw_identifier;
261 if (c_dialect_objc ())
262 parser->objc_need_raw_identifier = false;
27bf414c
JM
263
264 if (C_IS_RESERVED_WORD (token->value))
265 {
266 enum rid rid_code = C_RID_CODE (token->value);
267
eea1139b
ILT
268 if (rid_code == RID_CXX_COMPAT_WARN)
269 {
3ba09659
AH
270 warning_at (token->location,
271 OPT_Wc___compat,
88388a52
JM
272 "identifier %qE conflicts with C++ keyword",
273 token->value);
eea1139b 274 }
36c5e70a
BE
275 else if (rid_code >= RID_FIRST_ADDR_SPACE
276 && rid_code <= RID_LAST_ADDR_SPACE)
277 {
278 token->id_kind = C_ID_ADDRSPACE;
279 token->keyword = rid_code;
280 break;
281 }
1973201f 282 else if (c_dialect_objc () && OBJC_IS_PQ_KEYWORD (rid_code))
27bf414c 283 {
1973201f
NP
284 /* We found an Objective-C "pq" keyword (in, out,
285 inout, bycopy, byref, oneway). They need special
286 care because the interpretation depends on the
d853ee42 287 context. */
1973201f 288 if (parser->objc_pq_context)
27bf414c 289 {
27bf414c
JM
290 token->type = CPP_KEYWORD;
291 token->keyword = rid_code;
292 break;
293 }
f05b9d93
NP
294 else if (parser->objc_could_be_foreach_context
295 && rid_code == RID_IN)
296 {
297 /* We are in Objective-C, inside a (potential)
298 foreach context (which means after having
299 parsed 'for (', but before having parsed ';'),
300 and we found 'in'. We consider it the keyword
301 which terminates the declaration at the
302 beginning of a foreach-statement. Note that
303 this means you can't use 'in' for anything else
304 in that context; in particular, in Objective-C
305 you can't use 'in' as the name of the running
306 variable in a C for loop. We could potentially
307 try to add code here to disambiguate, but it
d853ee42 308 seems a reasonable limitation. */
f05b9d93
NP
309 token->type = CPP_KEYWORD;
310 token->keyword = rid_code;
311 break;
312 }
1973201f
NP
313 /* Else, "pq" keywords outside of the "pq" context are
314 not keywords, and we fall through to the code for
d853ee42 315 normal tokens. */
1973201f 316 }
668ea4b1
IS
317 else if (c_dialect_objc () && OBJC_IS_PATTR_KEYWORD (rid_code))
318 {
d853ee42
NP
319 /* We found an Objective-C "property attribute"
320 keyword (getter, setter, readonly, etc). These are
668ea4b1
IS
321 only valid in the property context. */
322 if (parser->objc_property_attr_context)
323 {
324 token->type = CPP_KEYWORD;
325 token->keyword = rid_code;
326 break;
327 }
328 /* Else they are not special keywords.
329 */
330 }
1973201f
NP
331 else if (c_dialect_objc ()
332 && (OBJC_IS_AT_KEYWORD (rid_code)
333 || OBJC_IS_CXX_KEYWORD (rid_code)))
334 {
335 /* We found one of the Objective-C "@" keywords (defs,
336 selector, synchronized, etc) or one of the
337 Objective-C "cxx" keywords (class, private,
338 protected, public, try, catch, throw) without a
339 preceding '@' sign. Do nothing and fall through to
340 the code for normal tokens (in C++ we would still
d853ee42 341 consider the CXX ones keywords, but not in C). */
1973201f 342 ;
27bf414c
JM
343 }
344 else
345 {
27bf414c
JM
346 token->type = CPP_KEYWORD;
347 token->keyword = rid_code;
348 break;
349 }
350 }
351
352 decl = lookup_name (token->value);
353 if (decl)
354 {
355 if (TREE_CODE (decl) == TYPE_DECL)
356 {
357 token->id_kind = C_ID_TYPENAME;
358 break;
359 }
360 }
361 else if (c_dialect_objc ())
362 {
363 tree objc_interface_decl = objc_is_class_name (token->value);
364 /* Objective-C class names are in the same namespace as
365 variables and typedefs, and hence are shadowed by local
366 declarations. */
367 if (objc_interface_decl
0d8a2528 368 && (!objc_force_identifier || global_bindings_p ()))
27bf414c
JM
369 {
370 token->value = objc_interface_decl;
371 token->id_kind = C_ID_CLASSNAME;
372 break;
373 }
374 }
bc4071dd 375 token->id_kind = C_ID_ID;
27bf414c 376 }
27bf414c
JM
377 break;
378 case CPP_AT_NAME:
379 /* This only happens in Objective-C; it must be a keyword. */
380 token->type = CPP_KEYWORD;
49b91f05
NP
381 switch (C_RID_CODE (token->value))
382 {
383 /* Replace 'class' with '@class', 'private' with '@private',
384 etc. This prevents confusion with the C++ keyword
385 'class', and makes the tokens consistent with other
386 Objective-C 'AT' keywords. For example '@class' is
387 reported as RID_AT_CLASS which is consistent with
388 '@synchronized', which is reported as
389 RID_AT_SYNCHRONIZED.
390 */
391 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
392 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
393 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
394 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
395 case RID_THROW: token->keyword = RID_AT_THROW; break;
396 case RID_TRY: token->keyword = RID_AT_TRY; break;
397 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
398 default: token->keyword = C_RID_CODE (token->value);
399 }
27bf414c
JM
400 break;
401 case CPP_COLON:
402 case CPP_COMMA:
403 case CPP_CLOSE_PAREN:
404 case CPP_SEMICOLON:
405 /* These tokens may affect the interpretation of any identifiers
406 following, if doing Objective-C. */
0bacb8c7
TT
407 if (c_dialect_objc ())
408 parser->objc_need_raw_identifier = false;
bc4071dd
RH
409 break;
410 case CPP_PRAGMA:
411 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
d75d71e0 412 token->pragma_kind = (enum pragma_kind) TREE_INT_CST_LOW (token->value);
bc4071dd 413 token->value = NULL;
27bf414c
JM
414 break;
415 default:
27bf414c
JM
416 break;
417 }
418 timevar_pop (TV_LEX);
419}
420
421/* Return a pointer to the next token from PARSER, reading it in if
422 necessary. */
423
424static inline c_token *
425c_parser_peek_token (c_parser *parser)
426{
427 if (parser->tokens_avail == 0)
428 {
0bacb8c7 429 c_lex_one_token (parser, &parser->tokens[0]);
27bf414c
JM
430 parser->tokens_avail = 1;
431 }
432 return &parser->tokens[0];
433}
434
435/* Return true if the next token from PARSER has the indicated
436 TYPE. */
437
438static inline bool
439c_parser_next_token_is (c_parser *parser, enum cpp_ttype type)
440{
441 return c_parser_peek_token (parser)->type == type;
442}
443
444/* Return true if the next token from PARSER does not have the
445 indicated TYPE. */
446
447static inline bool
448c_parser_next_token_is_not (c_parser *parser, enum cpp_ttype type)
449{
450 return !c_parser_next_token_is (parser, type);
451}
452
453/* Return true if the next token from PARSER is the indicated
454 KEYWORD. */
455
456static inline bool
457c_parser_next_token_is_keyword (c_parser *parser, enum rid keyword)
458{
dbc518f0 459 return c_parser_peek_token (parser)->keyword == keyword;
27bf414c
JM
460}
461
29ce73cb
PB
462/* Return a pointer to the next-but-one token from PARSER, reading it
463 in if necessary. The next token is already read in. */
464
465static c_token *
466c_parser_peek_2nd_token (c_parser *parser)
467{
468 if (parser->tokens_avail >= 2)
469 return &parser->tokens[1];
470 gcc_assert (parser->tokens_avail == 1);
471 gcc_assert (parser->tokens[0].type != CPP_EOF);
472 gcc_assert (parser->tokens[0].type != CPP_PRAGMA_EOL);
473 c_lex_one_token (parser, &parser->tokens[1]);
474 parser->tokens_avail = 2;
475 return &parser->tokens[1];
476}
477
27bf414c
JM
478/* Return true if TOKEN can start a type name,
479 false otherwise. */
480static bool
481c_token_starts_typename (c_token *token)
482{
483 switch (token->type)
484 {
485 case CPP_NAME:
486 switch (token->id_kind)
487 {
488 case C_ID_ID:
489 return false;
36c5e70a
BE
490 case C_ID_ADDRSPACE:
491 return true;
27bf414c
JM
492 case C_ID_TYPENAME:
493 return true;
494 case C_ID_CLASSNAME:
495 gcc_assert (c_dialect_objc ());
496 return true;
497 default:
498 gcc_unreachable ();
499 }
500 case CPP_KEYWORD:
501 switch (token->keyword)
502 {
503 case RID_UNSIGNED:
504 case RID_LONG:
505 case RID_SHORT:
506 case RID_SIGNED:
507 case RID_COMPLEX:
508 case RID_INT:
509 case RID_CHAR:
510 case RID_FLOAT:
511 case RID_DOUBLE:
512 case RID_VOID:
9a8ce21f
JG
513 case RID_DFLOAT32:
514 case RID_DFLOAT64:
515 case RID_DFLOAT128:
27bf414c
JM
516 case RID_BOOL:
517 case RID_ENUM:
518 case RID_STRUCT:
519 case RID_UNION:
520 case RID_TYPEOF:
521 case RID_CONST:
267bac10 522 case RID_ATOMIC:
27bf414c
JM
523 case RID_VOLATILE:
524 case RID_RESTRICT:
525 case RID_ATTRIBUTE:
ab22c1fa
CF
526 case RID_FRACT:
527 case RID_ACCUM:
528 case RID_SAT:
38b7bc7f 529 case RID_AUTO_TYPE:
27bf414c
JM
530 return true;
531 default:
78a7c317
DD
532 if (token->keyword >= RID_FIRST_INT_N
533 && token->keyword < RID_FIRST_INT_N + NUM_INT_N_ENTS
534 && int_n_enabled_p[token->keyword - RID_FIRST_INT_N])
535 return true;
27bf414c
JM
536 return false;
537 }
538 case CPP_LESS:
539 if (c_dialect_objc ())
540 return true;
541 return false;
542 default:
543 return false;
544 }
545}
546
29ce73cb
PB
547enum c_lookahead_kind {
548 /* Always treat unknown identifiers as typenames. */
549 cla_prefer_type,
550
551 /* Could be parsing a nonabstract declarator. Only treat an identifier
552 as a typename if followed by another identifier or a star. */
553 cla_nonabstract_decl,
554
555 /* Never treat identifiers as typenames. */
556 cla_prefer_id
557};
558
27bf414c 559/* Return true if the next token from PARSER can start a type name,
29ce73cb
PB
560 false otherwise. LA specifies how to do lookahead in order to
561 detect unknown type names. If unsure, pick CLA_PREFER_ID. */
562
27bf414c 563static inline bool
29ce73cb 564c_parser_next_tokens_start_typename (c_parser *parser, enum c_lookahead_kind la)
27bf414c
JM
565{
566 c_token *token = c_parser_peek_token (parser);
29ce73cb
PB
567 if (c_token_starts_typename (token))
568 return true;
569
570 /* Try a bit harder to detect an unknown typename. */
571 if (la != cla_prefer_id
572 && token->type == CPP_NAME
573 && token->id_kind == C_ID_ID
574
575 /* Do not try too hard when we could have "object in array". */
576 && !parser->objc_could_be_foreach_context
577
578 && (la == cla_prefer_type
579 || c_parser_peek_2nd_token (parser)->type == CPP_NAME
580 || c_parser_peek_2nd_token (parser)->type == CPP_MULT)
581
582 /* Only unknown identifiers. */
583 && !lookup_name (token->value))
584 return true;
585
586 return false;
27bf414c
JM
587}
588
f725e721
PB
589/* Return true if TOKEN is a type qualifier, false otherwise. */
590static bool
591c_token_is_qualifier (c_token *token)
592{
593 switch (token->type)
594 {
595 case CPP_NAME:
596 switch (token->id_kind)
597 {
598 case C_ID_ADDRSPACE:
599 return true;
600 default:
601 return false;
602 }
603 case CPP_KEYWORD:
604 switch (token->keyword)
605 {
606 case RID_CONST:
607 case RID_VOLATILE:
608 case RID_RESTRICT:
609 case RID_ATTRIBUTE:
267bac10 610 case RID_ATOMIC:
f725e721
PB
611 return true;
612 default:
613 return false;
614 }
615 case CPP_LESS:
616 return false;
617 default:
618 gcc_unreachable ();
619 }
620}
621
622/* Return true if the next token from PARSER is a type qualifier,
623 false otherwise. */
624static inline bool
625c_parser_next_token_is_qualifier (c_parser *parser)
626{
627 c_token *token = c_parser_peek_token (parser);
628 return c_token_is_qualifier (token);
629}
630
27bf414c
JM
631/* Return true if TOKEN can start declaration specifiers, false
632 otherwise. */
633static bool
634c_token_starts_declspecs (c_token *token)
635{
636 switch (token->type)
637 {
638 case CPP_NAME:
639 switch (token->id_kind)
640 {
641 case C_ID_ID:
642 return false;
36c5e70a
BE
643 case C_ID_ADDRSPACE:
644 return true;
27bf414c
JM
645 case C_ID_TYPENAME:
646 return true;
647 case C_ID_CLASSNAME:
648 gcc_assert (c_dialect_objc ());
649 return true;
650 default:
651 gcc_unreachable ();
652 }
653 case CPP_KEYWORD:
654 switch (token->keyword)
655 {
656 case RID_STATIC:
657 case RID_EXTERN:
658 case RID_REGISTER:
659 case RID_TYPEDEF:
660 case RID_INLINE:
bbceee64 661 case RID_NORETURN:
27bf414c
JM
662 case RID_AUTO:
663 case RID_THREAD:
664 case RID_UNSIGNED:
665 case RID_LONG:
666 case RID_SHORT:
667 case RID_SIGNED:
668 case RID_COMPLEX:
669 case RID_INT:
670 case RID_CHAR:
671 case RID_FLOAT:
672 case RID_DOUBLE:
673 case RID_VOID:
9a8ce21f
JG
674 case RID_DFLOAT32:
675 case RID_DFLOAT64:
676 case RID_DFLOAT128:
27bf414c
JM
677 case RID_BOOL:
678 case RID_ENUM:
679 case RID_STRUCT:
680 case RID_UNION:
681 case RID_TYPEOF:
682 case RID_CONST:
683 case RID_VOLATILE:
684 case RID_RESTRICT:
685 case RID_ATTRIBUTE:
ab22c1fa
CF
686 case RID_FRACT:
687 case RID_ACCUM:
688 case RID_SAT:
d19fa6b5 689 case RID_ALIGNAS:
267bac10 690 case RID_ATOMIC:
38b7bc7f 691 case RID_AUTO_TYPE:
27bf414c
JM
692 return true;
693 default:
78a7c317
DD
694 if (token->keyword >= RID_FIRST_INT_N
695 && token->keyword < RID_FIRST_INT_N + NUM_INT_N_ENTS
696 && int_n_enabled_p[token->keyword - RID_FIRST_INT_N])
697 return true;
27bf414c
JM
698 return false;
699 }
700 case CPP_LESS:
701 if (c_dialect_objc ())
702 return true;
703 return false;
704 default:
705 return false;
706 }
707}
708
32912286
JM
709
710/* Return true if TOKEN can start declaration specifiers or a static
711 assertion, false otherwise. */
712static bool
713c_token_starts_declaration (c_token *token)
714{
715 if (c_token_starts_declspecs (token)
716 || token->keyword == RID_STATIC_ASSERT)
717 return true;
718 else
719 return false;
720}
721
27bf414c
JM
722/* Return true if the next token from PARSER can start declaration
723 specifiers, false otherwise. */
724static inline bool
725c_parser_next_token_starts_declspecs (c_parser *parser)
726{
727 c_token *token = c_parser_peek_token (parser);
bede2adc
NP
728
729 /* In Objective-C, a classname normally starts a declspecs unless it
730 is immediately followed by a dot. In that case, it is the
731 Objective-C 2.0 "dot-syntax" for class objects, ie, calls the
732 setter/getter on the class. c_token_starts_declspecs() can't
733 differentiate between the two cases because it only checks the
734 current token, so we have a special check here. */
735 if (c_dialect_objc ()
736 && token->type == CPP_NAME
737 && token->id_kind == C_ID_CLASSNAME
738 && c_parser_peek_2nd_token (parser)->type == CPP_DOT)
739 return false;
740
27bf414c
JM
741 return c_token_starts_declspecs (token);
742}
743
2f413185 744/* Return true if the next tokens from PARSER can start declaration
32912286
JM
745 specifiers or a static assertion, false otherwise. */
746static inline bool
2f413185 747c_parser_next_tokens_start_declaration (c_parser *parser)
32912286
JM
748{
749 c_token *token = c_parser_peek_token (parser);
bede2adc
NP
750
751 /* Same as above. */
752 if (c_dialect_objc ()
753 && token->type == CPP_NAME
754 && token->id_kind == C_ID_CLASSNAME
755 && c_parser_peek_2nd_token (parser)->type == CPP_DOT)
756 return false;
757
2f413185
PB
758 /* Labels do not start declarations. */
759 if (token->type == CPP_NAME
760 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
761 return false;
762
763 if (c_token_starts_declaration (token))
764 return true;
765
29ce73cb 766 if (c_parser_next_tokens_start_typename (parser, cla_nonabstract_decl))
2f413185
PB
767 return true;
768
769 return false;
32912286
JM
770}
771
27bf414c
JM
772/* Consume the next token from PARSER. */
773
774static void
775c_parser_consume_token (c_parser *parser)
776{
bc4071dd
RH
777 gcc_assert (parser->tokens_avail >= 1);
778 gcc_assert (parser->tokens[0].type != CPP_EOF);
779 gcc_assert (!parser->in_pragma || parser->tokens[0].type != CPP_PRAGMA_EOL);
780 gcc_assert (parser->error || parser->tokens[0].type != CPP_PRAGMA);
acf0174b
JJ
781 if (parser->tokens != &parser->tokens_buf[0])
782 parser->tokens++;
783 else if (parser->tokens_avail == 2)
27bf414c 784 parser->tokens[0] = parser->tokens[1];
27bf414c
JM
785 parser->tokens_avail--;
786}
787
bc4071dd
RH
788/* Expect the current token to be a #pragma. Consume it and remember
789 that we've begun parsing a pragma. */
790
791static void
792c_parser_consume_pragma (c_parser *parser)
793{
794 gcc_assert (!parser->in_pragma);
795 gcc_assert (parser->tokens_avail >= 1);
796 gcc_assert (parser->tokens[0].type == CPP_PRAGMA);
acf0174b
JJ
797 if (parser->tokens != &parser->tokens_buf[0])
798 parser->tokens++;
799 else if (parser->tokens_avail == 2)
bc4071dd
RH
800 parser->tokens[0] = parser->tokens[1];
801 parser->tokens_avail--;
802 parser->in_pragma = true;
803}
804
8400e75e 805/* Update the global input_location from TOKEN. */
27bf414c
JM
806static inline void
807c_parser_set_source_position_from_token (c_token *token)
808{
809 if (token->type != CPP_EOF)
810 {
811 input_location = token->location;
27bf414c
JM
812 }
813}
814
27bf414c
JM
815/* Issue a diagnostic of the form
816 FILE:LINE: MESSAGE before TOKEN
817 where TOKEN is the next token in the input stream of PARSER.
818 MESSAGE (specified by the caller) is usually of the form "expected
819 OTHER-TOKEN".
820
821 Do not issue a diagnostic if still recovering from an error.
822
823 ??? This is taken from the C++ parser, but building up messages in
824 this way is not i18n-friendly and some other approach should be
825 used. */
826
827static void
4b794eaf 828c_parser_error (c_parser *parser, const char *gmsgid)
27bf414c
JM
829{
830 c_token *token = c_parser_peek_token (parser);
831 if (parser->error)
832 return;
833 parser->error = true;
4b794eaf 834 if (!gmsgid)
27bf414c
JM
835 return;
836 /* This diagnostic makes more sense if it is tagged to the line of
837 the token we just peeked at. */
838 c_parser_set_source_position_from_token (token);
4b794eaf 839 c_parse_error (gmsgid,
27bf414c
JM
840 /* Because c_parse_error does not understand
841 CPP_KEYWORD, keywords are treated like
842 identifiers. */
843 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
cfc93532
MLI
844 /* ??? The C parser does not save the cpp flags of a
845 token, we need to pass 0 here and we will not get
846 the source spelling of some tokens but rather the
847 canonical spelling. */
848 token->value, /*flags=*/0);
27bf414c
JM
849}
850
851/* If the next token is of the indicated TYPE, consume it. Otherwise,
852 issue the error MSGID. If MSGID is NULL then a message has already
853 been produced and no message will be produced this time. Returns
854 true if found, false otherwise. */
855
856static bool
857c_parser_require (c_parser *parser,
858 enum cpp_ttype type,
859 const char *msgid)
860{
861 if (c_parser_next_token_is (parser, type))
862 {
863 c_parser_consume_token (parser);
864 return true;
865 }
866 else
867 {
868 c_parser_error (parser, msgid);
869 return false;
870 }
871}
872
873/* If the next token is the indicated keyword, consume it. Otherwise,
874 issue the error MSGID. Returns true if found, false otherwise. */
875
876static bool
877c_parser_require_keyword (c_parser *parser,
878 enum rid keyword,
879 const char *msgid)
880{
881 if (c_parser_next_token_is_keyword (parser, keyword))
882 {
883 c_parser_consume_token (parser);
884 return true;
885 }
886 else
887 {
888 c_parser_error (parser, msgid);
889 return false;
890 }
891}
892
893/* Like c_parser_require, except that tokens will be skipped until the
894 desired token is found. An error message is still produced if the
895 next token is not as expected. If MSGID is NULL then a message has
896 already been produced and no message will be produced this
897 time. */
898
899static void
900c_parser_skip_until_found (c_parser *parser,
901 enum cpp_ttype type,
902 const char *msgid)
903{
904 unsigned nesting_depth = 0;
905
906 if (c_parser_require (parser, type, msgid))
907 return;
908
909 /* Skip tokens until the desired token is found. */
910 while (true)
911 {
912 /* Peek at the next token. */
913 c_token *token = c_parser_peek_token (parser);
914 /* If we've reached the token we want, consume it and stop. */
915 if (token->type == type && !nesting_depth)
916 {
917 c_parser_consume_token (parser);
918 break;
919 }
bc4071dd 920
27bf414c
JM
921 /* If we've run out of tokens, stop. */
922 if (token->type == CPP_EOF)
923 return;
bc4071dd
RH
924 if (token->type == CPP_PRAGMA_EOL && parser->in_pragma)
925 return;
27bf414c
JM
926 if (token->type == CPP_OPEN_BRACE
927 || token->type == CPP_OPEN_PAREN
928 || token->type == CPP_OPEN_SQUARE)
929 ++nesting_depth;
930 else if (token->type == CPP_CLOSE_BRACE
931 || token->type == CPP_CLOSE_PAREN
932 || token->type == CPP_CLOSE_SQUARE)
933 {
934 if (nesting_depth-- == 0)
935 break;
936 }
937 /* Consume this token. */
938 c_parser_consume_token (parser);
939 }
940 parser->error = false;
941}
942
943/* Skip tokens until the end of a parameter is found, but do not
944 consume the comma, semicolon or closing delimiter. */
945
946static void
947c_parser_skip_to_end_of_parameter (c_parser *parser)
948{
949 unsigned nesting_depth = 0;
950
951 while (true)
952 {
953 c_token *token = c_parser_peek_token (parser);
954 if ((token->type == CPP_COMMA || token->type == CPP_SEMICOLON)
955 && !nesting_depth)
956 break;
957 /* If we've run out of tokens, stop. */
958 if (token->type == CPP_EOF)
959 return;
bc4071dd
RH
960 if (token->type == CPP_PRAGMA_EOL && parser->in_pragma)
961 return;
27bf414c
JM
962 if (token->type == CPP_OPEN_BRACE
963 || token->type == CPP_OPEN_PAREN
964 || token->type == CPP_OPEN_SQUARE)
965 ++nesting_depth;
966 else if (token->type == CPP_CLOSE_BRACE
967 || token->type == CPP_CLOSE_PAREN
968 || token->type == CPP_CLOSE_SQUARE)
969 {
970 if (nesting_depth-- == 0)
971 break;
972 }
973 /* Consume this token. */
974 c_parser_consume_token (parser);
975 }
976 parser->error = false;
977}
978
bc4071dd
RH
979/* Expect to be at the end of the pragma directive and consume an
980 end of line marker. */
981
982static void
983c_parser_skip_to_pragma_eol (c_parser *parser)
984{
985 gcc_assert (parser->in_pragma);
986 parser->in_pragma = false;
987
988 if (!c_parser_require (parser, CPP_PRAGMA_EOL, "expected end of line"))
989 while (true)
990 {
991 c_token *token = c_parser_peek_token (parser);
992 if (token->type == CPP_EOF)
993 break;
994 if (token->type == CPP_PRAGMA_EOL)
995 {
996 c_parser_consume_token (parser);
997 break;
998 }
999 c_parser_consume_token (parser);
1000 }
1001
1002 parser->error = false;
1003}
27bf414c 1004
2a83cc52
RH
1005/* Skip tokens until we have consumed an entire block, or until we
1006 have consumed a non-nested ';'. */
1007
1008static void
1009c_parser_skip_to_end_of_block_or_statement (c_parser *parser)
1010{
1011 unsigned nesting_depth = 0;
1012 bool save_error = parser->error;
1013
1014 while (true)
1015 {
1016 c_token *token;
1017
1018 /* Peek at the next token. */
1019 token = c_parser_peek_token (parser);
1020
1021 switch (token->type)
1022 {
1023 case CPP_EOF:
1024 return;
1025
1026 case CPP_PRAGMA_EOL:
1027 if (parser->in_pragma)
1028 return;
1029 break;
1030
1031 case CPP_SEMICOLON:
1032 /* If the next token is a ';', we have reached the
1033 end of the statement. */
1034 if (!nesting_depth)
1035 {
1036 /* Consume the ';'. */
1037 c_parser_consume_token (parser);
1038 goto finished;
1039 }
1040 break;
1041
1042 case CPP_CLOSE_BRACE:
1043 /* If the next token is a non-nested '}', then we have
1044 reached the end of the current block. */
1045 if (nesting_depth == 0 || --nesting_depth == 0)
1046 {
1047 c_parser_consume_token (parser);
1048 goto finished;
1049 }
1050 break;
1051
1052 case CPP_OPEN_BRACE:
1053 /* If it the next token is a '{', then we are entering a new
1054 block. Consume the entire block. */
1055 ++nesting_depth;
1056 break;
1057
1058 case CPP_PRAGMA:
1059 /* If we see a pragma, consume the whole thing at once. We
1060 have some safeguards against consuming pragmas willy-nilly.
1061 Normally, we'd expect to be here with parser->error set,
1062 which disables these safeguards. But it's possible to get
1063 here for secondary error recovery, after parser->error has
1064 been cleared. */
1065 c_parser_consume_pragma (parser);
1066 c_parser_skip_to_pragma_eol (parser);
1067 parser->error = save_error;
1068 continue;
9e33de05
RS
1069
1070 default:
1071 break;
2a83cc52
RH
1072 }
1073
1074 c_parser_consume_token (parser);
1075 }
1076
1077 finished:
1078 parser->error = false;
1079}
1080
d2e796ad
MLI
1081/* CPP's options (initialized by c-opts.c). */
1082extern cpp_options *cpp_opts;
1083
27bf414c
JM
1084/* Save the warning flags which are controlled by __extension__. */
1085
1086static inline int
1087disable_extension_diagnostics (void)
1088{
1089 int ret = (pedantic
1090 | (warn_pointer_arith << 1)
1091 | (warn_traditional << 2)
d2e796ad 1092 | (flag_iso << 3)
24b97832 1093 | (warn_long_long << 4)
b3ab9ea2 1094 | (warn_cxx_compat << 5)
f3bede71 1095 | (warn_overlength_strings << 6)
177cce46
MP
1096 /* warn_c90_c99_compat has three states: -1/0/1, so we must
1097 play tricks to properly restore it. */
1098 | ((warn_c90_c99_compat == 1) << 7)
35aff4fb
MP
1099 | ((warn_c90_c99_compat == -1) << 8)
1100 /* Similarly for warn_c99_c11_compat. */
1101 | ((warn_c99_c11_compat == 1) << 9)
1102 | ((warn_c99_c11_compat == -1) << 10)
1103 );
e3339d0f 1104 cpp_opts->cpp_pedantic = pedantic = 0;
27bf414c 1105 warn_pointer_arith = 0;
e3339d0f 1106 cpp_opts->cpp_warn_traditional = warn_traditional = 0;
27bf414c 1107 flag_iso = 0;
e3339d0f 1108 cpp_opts->cpp_warn_long_long = warn_long_long = 0;
24b97832 1109 warn_cxx_compat = 0;
b3ab9ea2 1110 warn_overlength_strings = 0;
f3bede71 1111 warn_c90_c99_compat = 0;
35aff4fb 1112 warn_c99_c11_compat = 0;
27bf414c
JM
1113 return ret;
1114}
1115
1116/* Restore the warning flags which are controlled by __extension__.
1117 FLAGS is the return value from disable_extension_diagnostics. */
1118
1119static inline void
1120restore_extension_diagnostics (int flags)
1121{
e3339d0f 1122 cpp_opts->cpp_pedantic = pedantic = flags & 1;
27bf414c 1123 warn_pointer_arith = (flags >> 1) & 1;
e3339d0f 1124 cpp_opts->cpp_warn_traditional = warn_traditional = (flags >> 2) & 1;
27bf414c 1125 flag_iso = (flags >> 3) & 1;
e3339d0f 1126 cpp_opts->cpp_warn_long_long = warn_long_long = (flags >> 4) & 1;
24b97832 1127 warn_cxx_compat = (flags >> 5) & 1;
b3ab9ea2 1128 warn_overlength_strings = (flags >> 6) & 1;
177cce46
MP
1129 /* See above for why is this needed. */
1130 warn_c90_c99_compat = (flags >> 7) & 1 ? 1 : ((flags >> 8) & 1 ? -1 : 0);
35aff4fb 1131 warn_c99_c11_compat = (flags >> 9) & 1 ? 1 : ((flags >> 10) & 1 ? -1 : 0);
27bf414c
JM
1132}
1133
1134/* Possibly kinds of declarator to parse. */
1135typedef enum c_dtr_syn {
1136 /* A normal declarator with an identifier. */
1137 C_DTR_NORMAL,
1138 /* An abstract declarator (maybe empty). */
1139 C_DTR_ABSTRACT,
1140 /* A parameter declarator: may be either, but after a type name does
1141 not redeclare a typedef name as an identifier if it can
1142 alternatively be interpreted as a typedef name; see DR#009,
1143 applied in C90 TC1, omitted from C99 and reapplied in C99 TC2
1144 following DR#249. For example, given a typedef T, "int T" and
1145 "int *T" are valid parameter declarations redeclaring T, while
1146 "int (T)" and "int * (T)" and "int (T[])" and "int (T (int))" are
1147 abstract declarators rather than involving redundant parentheses;
1148 the same applies with attributes inside the parentheses before
1149 "T". */
1150 C_DTR_PARM
1151} c_dtr_syn;
1152
20906c66
JJ
1153/* The binary operation precedence levels, where 0 is a dummy lowest level
1154 used for the bottom of the stack. */
1155enum c_parser_prec {
1156 PREC_NONE,
1157 PREC_LOGOR,
1158 PREC_LOGAND,
1159 PREC_BITOR,
1160 PREC_BITXOR,
1161 PREC_BITAND,
1162 PREC_EQ,
1163 PREC_REL,
1164 PREC_SHIFT,
1165 PREC_ADD,
1166 PREC_MULT,
1167 NUM_PRECS
1168};
1169
27bf414c
JM
1170static void c_parser_external_declaration (c_parser *);
1171static void c_parser_asm_definition (c_parser *);
32912286 1172static void c_parser_declaration_or_fndef (c_parser *, bool, bool, bool,
acf0174b 1173 bool, bool, tree *, vec<c_token>);
32912286
JM
1174static void c_parser_static_assert_declaration_no_semi (c_parser *);
1175static void c_parser_static_assert_declaration (c_parser *);
27bf414c 1176static void c_parser_declspecs (c_parser *, struct c_declspecs *, bool, bool,
38b7bc7f 1177 bool, bool, bool, enum c_lookahead_kind);
27bf414c
JM
1178static struct c_typespec c_parser_enum_specifier (c_parser *);
1179static struct c_typespec c_parser_struct_or_union_specifier (c_parser *);
1180static tree c_parser_struct_declaration (c_parser *);
1181static struct c_typespec c_parser_typeof_specifier (c_parser *);
d19fa6b5 1182static tree c_parser_alignas_specifier (c_parser *);
27bf414c
JM
1183static struct c_declarator *c_parser_declarator (c_parser *, bool, c_dtr_syn,
1184 bool *);
1185static struct c_declarator *c_parser_direct_declarator (c_parser *, bool,
1186 c_dtr_syn, bool *);
1187static struct c_declarator *c_parser_direct_declarator_inner (c_parser *,
1188 bool,
1189 struct c_declarator *);
1190static struct c_arg_info *c_parser_parms_declarator (c_parser *, bool, tree);
a04a722b
JM
1191static struct c_arg_info *c_parser_parms_list_declarator (c_parser *, tree,
1192 tree);
27bf414c
JM
1193static struct c_parm *c_parser_parameter_declaration (c_parser *, tree);
1194static tree c_parser_simple_asm_expr (c_parser *);
1195static tree c_parser_attributes (c_parser *);
1196static struct c_type_name *c_parser_type_name (c_parser *);
1197static struct c_expr c_parser_initializer (c_parser *);
1198static struct c_expr c_parser_braced_init (c_parser *, tree, bool);
a1e3b3d9
LB
1199static void c_parser_initelt (c_parser *, struct obstack *);
1200static void c_parser_initval (c_parser *, struct c_expr *,
1201 struct obstack *);
27bf414c
JM
1202static tree c_parser_compound_statement (c_parser *);
1203static void c_parser_compound_statement_nostart (c_parser *);
1204static void c_parser_label (c_parser *);
1205static void c_parser_statement (c_parser *);
1206static void c_parser_statement_after_labels (c_parser *);
1207static void c_parser_if_statement (c_parser *);
1208static void c_parser_switch_statement (c_parser *);
d4af74d4
TB
1209static void c_parser_while_statement (c_parser *, bool);
1210static void c_parser_do_statement (c_parser *, bool);
8170608b 1211static void c_parser_for_statement (c_parser *, bool);
27bf414c 1212static tree c_parser_asm_statement (c_parser *);
eadd3d0d 1213static tree c_parser_asm_operands (c_parser *);
1c384bf1 1214static tree c_parser_asm_goto_operands (c_parser *);
27bf414c 1215static tree c_parser_asm_clobbers (c_parser *);
acf0174b
JJ
1216static struct c_expr c_parser_expr_no_commas (c_parser *, struct c_expr *,
1217 tree = NULL_TREE);
27bf414c 1218static struct c_expr c_parser_conditional_expression (c_parser *,
acf0174b 1219 struct c_expr *, tree);
20906c66 1220static struct c_expr c_parser_binary_expression (c_parser *, struct c_expr *,
acf0174b 1221 tree);
27bf414c
JM
1222static struct c_expr c_parser_cast_expression (c_parser *, struct c_expr *);
1223static struct c_expr c_parser_unary_expression (c_parser *);
1224static struct c_expr c_parser_sizeof_expression (c_parser *);
1225static struct c_expr c_parser_alignof_expression (c_parser *);
1226static struct c_expr c_parser_postfix_expression (c_parser *);
1227static struct c_expr c_parser_postfix_expression_after_paren_type (c_parser *,
24b97832
ILT
1228 struct c_type_name *,
1229 location_t);
27bf414c 1230static struct c_expr c_parser_postfix_expression_after_primary (c_parser *,
c2255bc4 1231 location_t loc,
27bf414c 1232 struct c_expr);
0a35513e
AH
1233static tree c_parser_transaction (c_parser *, enum rid);
1234static struct c_expr c_parser_transaction_expression (c_parser *, enum rid);
1235static tree c_parser_transaction_cancel (c_parser *);
27bf414c 1236static struct c_expr c_parser_expression (c_parser *);
46bdb9cf 1237static struct c_expr c_parser_expression_conv (c_parser *);
9771b263
DN
1238static vec<tree, va_gc> *c_parser_expr_list (c_parser *, bool, bool,
1239 vec<tree, va_gc> **, location_t *,
b108f48f
JJ
1240 tree *, vec<location_t> *,
1241 unsigned int * = NULL);
953ff289
DN
1242static void c_parser_omp_construct (c_parser *);
1243static void c_parser_omp_threadprivate (c_parser *);
1244static void c_parser_omp_barrier (c_parser *);
1245static void c_parser_omp_flush (c_parser *);
a68ab351 1246static void c_parser_omp_taskwait (c_parser *);
20906c66 1247static void c_parser_omp_taskyield (c_parser *);
acf0174b
JJ
1248static void c_parser_omp_cancel (c_parser *);
1249static void c_parser_omp_cancellation_point (c_parser *);
27bf414c 1250
acf0174b
JJ
1251enum pragma_context { pragma_external, pragma_struct, pragma_param,
1252 pragma_stmt, pragma_compound };
bc4071dd 1253static bool c_parser_pragma (c_parser *, enum pragma_context);
acf0174b
JJ
1254static bool c_parser_omp_target (c_parser *, enum pragma_context);
1255static void c_parser_omp_end_declare_target (c_parser *);
1256static void c_parser_omp_declare (c_parser *, enum pragma_context);
bc4071dd 1257
27bf414c
JM
1258/* These Objective-C parser functions are only ever called when
1259 compiling Objective-C. */
c165dca7 1260static void c_parser_objc_class_definition (c_parser *, tree);
27bf414c
JM
1261static void c_parser_objc_class_instance_variables (c_parser *);
1262static void c_parser_objc_class_declaration (c_parser *);
1263static void c_parser_objc_alias_declaration (c_parser *);
c165dca7 1264static void c_parser_objc_protocol_definition (c_parser *, tree);
249a82c4 1265static bool c_parser_objc_method_type (c_parser *);
27bf414c
JM
1266static void c_parser_objc_method_definition (c_parser *);
1267static void c_parser_objc_methodprotolist (c_parser *);
1268static void c_parser_objc_methodproto (c_parser *);
a04a722b 1269static tree c_parser_objc_method_decl (c_parser *, bool, tree *, tree *);
27bf414c
JM
1270static tree c_parser_objc_type_name (c_parser *);
1271static tree c_parser_objc_protocol_refs (c_parser *);
437c2322 1272static void c_parser_objc_try_catch_finally_statement (c_parser *);
27bf414c
JM
1273static void c_parser_objc_synchronized_statement (c_parser *);
1274static tree c_parser_objc_selector (c_parser *);
1275static tree c_parser_objc_selector_arg (c_parser *);
1276static tree c_parser_objc_receiver (c_parser *);
1277static tree c_parser_objc_message_args (c_parser *);
1278static tree c_parser_objc_keywordexpr (c_parser *);
f614132b 1279static void c_parser_objc_at_property_declaration (c_parser *);
da57d1b9
NP
1280static void c_parser_objc_at_synthesize_declaration (c_parser *);
1281static void c_parser_objc_at_dynamic_declaration (c_parser *);
668ea4b1 1282static bool c_parser_objc_diagnose_bad_element_prefix
c165dca7 1283 (c_parser *, struct c_declspecs *);
27bf414c 1284
c02065fc
AH
1285/* Cilk Plus supporting routines. */
1286static void c_parser_cilk_simd (c_parser *);
9a771876 1287static void c_parser_cilk_for (c_parser *, tree);
c02065fc 1288static bool c_parser_cilk_verify_simd (c_parser *, enum pragma_context);
36536d79 1289static tree c_parser_array_notation (location_t, c_parser *, tree, tree);
41958c28 1290static tree c_parser_cilk_clause_vectorlength (c_parser *, tree, bool);
9a771876 1291static void c_parser_cilk_grainsize (c_parser *);
36536d79 1292
27bf414c
JM
1293/* Parse a translation unit (C90 6.7, C99 6.9).
1294
1295 translation-unit:
1296 external-declarations
1297
1298 external-declarations:
1299 external-declaration
1300 external-declarations external-declaration
1301
1302 GNU extensions:
1303
1304 translation-unit:
1305 empty
1306*/
1307
1308static void
1309c_parser_translation_unit (c_parser *parser)
1310{
1311 if (c_parser_next_token_is (parser, CPP_EOF))
1312 {
c1771a20 1313 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
509c9d60 1314 "ISO C forbids an empty translation unit");
27bf414c
JM
1315 }
1316 else
1317 {
1318 void *obstack_position = obstack_alloc (&parser_obstack, 0);
6ec637a4 1319 mark_valid_location_for_stdc_pragma (false);
27bf414c
JM
1320 do
1321 {
1322 ggc_collect ();
1323 c_parser_external_declaration (parser);
1324 obstack_free (&parser_obstack, obstack_position);
1325 }
1326 while (c_parser_next_token_is_not (parser, CPP_EOF));
1327 }
1328}
1329
1330/* Parse an external declaration (C90 6.7, C99 6.9).
1331
1332 external-declaration:
1333 function-definition
1334 declaration
1335
1336 GNU extensions:
1337
1338 external-declaration:
1339 asm-definition
1340 ;
1341 __extension__ external-declaration
1342
1343 Objective-C:
1344
1345 external-declaration:
1346 objc-class-definition
1347 objc-class-declaration
1348 objc-alias-declaration
1349 objc-protocol-definition
1350 objc-method-definition
1351 @end
1352*/
1353
1354static void
1355c_parser_external_declaration (c_parser *parser)
1356{
1357 int ext;
1358 switch (c_parser_peek_token (parser)->type)
1359 {
1360 case CPP_KEYWORD:
1361 switch (c_parser_peek_token (parser)->keyword)
1362 {
1363 case RID_EXTENSION:
1364 ext = disable_extension_diagnostics ();
1365 c_parser_consume_token (parser);
1366 c_parser_external_declaration (parser);
1367 restore_extension_diagnostics (ext);
1368 break;
1369 case RID_ASM:
1370 c_parser_asm_definition (parser);
1371 break;
1372 case RID_AT_INTERFACE:
1373 case RID_AT_IMPLEMENTATION:
1374 gcc_assert (c_dialect_objc ());
c165dca7 1375 c_parser_objc_class_definition (parser, NULL_TREE);
27bf414c 1376 break;
49b91f05 1377 case RID_AT_CLASS:
27bf414c
JM
1378 gcc_assert (c_dialect_objc ());
1379 c_parser_objc_class_declaration (parser);
1380 break;
1381 case RID_AT_ALIAS:
1382 gcc_assert (c_dialect_objc ());
1383 c_parser_objc_alias_declaration (parser);
1384 break;
1385 case RID_AT_PROTOCOL:
1386 gcc_assert (c_dialect_objc ());
c165dca7 1387 c_parser_objc_protocol_definition (parser, NULL_TREE);
27bf414c 1388 break;
668ea4b1
IS
1389 case RID_AT_PROPERTY:
1390 gcc_assert (c_dialect_objc ());
f614132b 1391 c_parser_objc_at_property_declaration (parser);
668ea4b1 1392 break;
da57d1b9
NP
1393 case RID_AT_SYNTHESIZE:
1394 gcc_assert (c_dialect_objc ());
1395 c_parser_objc_at_synthesize_declaration (parser);
1396 break;
1397 case RID_AT_DYNAMIC:
1398 gcc_assert (c_dialect_objc ());
1399 c_parser_objc_at_dynamic_declaration (parser);
1400 break;
27bf414c
JM
1401 case RID_AT_END:
1402 gcc_assert (c_dialect_objc ());
1403 c_parser_consume_token (parser);
1404 objc_finish_implementation ();
1405 break;
1406 default:
1407 goto decl_or_fndef;
1408 }
1409 break;
1410 case CPP_SEMICOLON:
c1771a20 1411 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
509c9d60 1412 "ISO C does not allow extra %<;%> outside of a function");
27bf414c
JM
1413 c_parser_consume_token (parser);
1414 break;
bc4071dd 1415 case CPP_PRAGMA:
6ec637a4 1416 mark_valid_location_for_stdc_pragma (true);
bc4071dd 1417 c_parser_pragma (parser, pragma_external);
6ec637a4 1418 mark_valid_location_for_stdc_pragma (false);
bc4071dd 1419 break;
27bf414c
JM
1420 case CPP_PLUS:
1421 case CPP_MINUS:
1422 if (c_dialect_objc ())
1423 {
1424 c_parser_objc_method_definition (parser);
1425 break;
1426 }
1427 /* Else fall through, and yield a syntax error trying to parse
1428 as a declaration or function definition. */
1429 default:
1430 decl_or_fndef:
c165dca7
IS
1431 /* A declaration or a function definition (or, in Objective-C,
1432 an @interface or @protocol with prefix attributes). We can
1433 only tell which after parsing the declaration specifiers, if
1434 any, and the first declarator. */
acf0174b
JJ
1435 c_parser_declaration_or_fndef (parser, true, true, true, false, true,
1436 NULL, vNULL);
27bf414c
JM
1437 break;
1438 }
1439}
1440
acf0174b
JJ
1441static void c_finish_omp_declare_simd (c_parser *, tree, tree, vec<c_token>);
1442
27bf414c
JM
1443/* Parse a declaration or function definition (C90 6.5, 6.7.1, C99
1444 6.7, 6.9.1). If FNDEF_OK is true, a function definition is
1445 accepted; otherwise (old-style parameter declarations) only other
32912286
JM
1446 declarations are accepted. If STATIC_ASSERT_OK is true, a static
1447 assertion is accepted; otherwise (old-style parameter declarations)
1448 it is not. If NESTED is true, we are inside a function or parsing
1449 old-style parameter declarations; any functions encountered are
1450 nested functions and declaration specifiers are required; otherwise
1451 we are at top level and functions are normal functions and
1452 declaration specifiers may be optional. If EMPTY_OK is true, empty
1453 declarations are OK (subject to all other constraints); otherwise
1454 (old-style parameter declarations) they are diagnosed. If
1455 START_ATTR_OK is true, the declaration specifiers may start with
1456 attributes; otherwise they may not.
f05b9d93
NP
1457 OBJC_FOREACH_OBJECT_DECLARATION can be used to get back the parsed
1458 declaration when parsing an Objective-C foreach statement.
27bf414c
JM
1459
1460 declaration:
1461 declaration-specifiers init-declarator-list[opt] ;
32912286 1462 static_assert-declaration
27bf414c
JM
1463
1464 function-definition:
1465 declaration-specifiers[opt] declarator declaration-list[opt]
1466 compound-statement
1467
1468 declaration-list:
1469 declaration
1470 declaration-list declaration
1471
1472 init-declarator-list:
1473 init-declarator
1474 init-declarator-list , init-declarator
1475
1476 init-declarator:
1477 declarator simple-asm-expr[opt] attributes[opt]
1478 declarator simple-asm-expr[opt] attributes[opt] = initializer
1479
1480 GNU extensions:
1481
1482 nested-function-definition:
1483 declaration-specifiers declarator declaration-list[opt]
1484 compound-statement
1485
c165dca7
IS
1486 Objective-C:
1487 attributes objc-class-definition
1488 attributes objc-category-definition
1489 attributes objc-protocol-definition
1490
27bf414c
JM
1491 The simple-asm-expr and attributes are GNU extensions.
1492
1493 This function does not handle __extension__; that is handled in its
1494 callers. ??? Following the old parser, __extension__ may start
1495 external declarations, declarations in functions and declarations
1496 at the start of "for" loops, but not old-style parameter
1497 declarations.
1498
1499 C99 requires declaration specifiers in a function definition; the
1500 absence is diagnosed through the diagnosis of implicit int. In GNU
1501 C we also allow but diagnose declarations without declaration
1502 specifiers, but only at top level (elsewhere they conflict with
953ff289 1503 other syntax).
b8698a0f 1504
f05b9d93
NP
1505 In Objective-C, declarations of the looping variable in a foreach
1506 statement are exceptionally terminated by 'in' (for example, 'for
1507 (NSObject *object in array) { ... }').
1508
953ff289 1509 OpenMP:
b8698a0f 1510
953ff289
DN
1511 declaration:
1512 threadprivate-directive */
27bf414c
JM
1513
1514static void
32912286
JM
1515c_parser_declaration_or_fndef (c_parser *parser, bool fndef_ok,
1516 bool static_assert_ok, bool empty_ok,
f05b9d93 1517 bool nested, bool start_attr_ok,
acf0174b
JJ
1518 tree *objc_foreach_object_declaration,
1519 vec<c_token> omp_declare_simd_clauses)
27bf414c
JM
1520{
1521 struct c_declspecs *specs;
1522 tree prefix_attrs;
1523 tree all_prefix_attrs;
1524 bool diagnosed_no_specs = false;
c7412148 1525 location_t here = c_parser_peek_token (parser)->location;
bc4071dd 1526
32912286
JM
1527 if (static_assert_ok
1528 && c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT))
1529 {
1530 c_parser_static_assert_declaration (parser);
1531 return;
1532 }
27bf414c 1533 specs = build_null_declspecs ();
2f413185
PB
1534
1535 /* Try to detect an unknown type name when we have "A B" or "A *B". */
1536 if (c_parser_peek_token (parser)->type == CPP_NAME
1537 && c_parser_peek_token (parser)->id_kind == C_ID_ID
1538 && (c_parser_peek_2nd_token (parser)->type == CPP_NAME
1539 || c_parser_peek_2nd_token (parser)->type == CPP_MULT)
1540 && (!nested || !lookup_name (c_parser_peek_token (parser)->value)))
1541 {
1542 error_at (here, "unknown type name %qE",
1543 c_parser_peek_token (parser)->value);
1544
1545 /* Parse declspecs normally to get a correct pointer type, but avoid
a5812bdc
PB
1546 a further "fails to be a type name" error. Refuse nested functions
1547 since it is not how the user likely wants us to recover. */
2f413185
PB
1548 c_parser_peek_token (parser)->type = CPP_KEYWORD;
1549 c_parser_peek_token (parser)->keyword = RID_VOID;
1550 c_parser_peek_token (parser)->value = error_mark_node;
a5812bdc 1551 fndef_ok = !nested;
2f413185
PB
1552 }
1553
568a31f2 1554 c_parser_declspecs (parser, specs, true, true, start_attr_ok,
38b7bc7f 1555 true, true, cla_nonabstract_decl);
27bf414c
JM
1556 if (parser->error)
1557 {
1558 c_parser_skip_to_end_of_block_or_statement (parser);
1559 return;
1560 }
1561 if (nested && !specs->declspecs_seen_p)
1562 {
1563 c_parser_error (parser, "expected declaration specifiers");
1564 c_parser_skip_to_end_of_block_or_statement (parser);
1565 return;
1566 }
1567 finish_declspecs (specs);
38b7bc7f 1568 bool auto_type_p = specs->typespec_word == cts_auto_type;
27bf414c
JM
1569 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1570 {
38b7bc7f
JM
1571 if (auto_type_p)
1572 error_at (here, "%<__auto_type%> in empty declaration");
1573 else if (empty_ok)
27bf414c
JM
1574 shadow_tag (specs);
1575 else
1576 {
1577 shadow_tag_warned (specs, 1);
509c9d60 1578 pedwarn (here, 0, "empty declaration");
27bf414c
JM
1579 }
1580 c_parser_consume_token (parser);
1581 return;
1582 }
f725e721
PB
1583
1584 /* Provide better error recovery. Note that a type name here is usually
1585 better diagnosed as a redeclaration. */
1586 if (empty_ok
1587 && specs->typespec_kind == ctsk_tagdef
1588 && c_parser_next_token_starts_declspecs (parser)
1589 && !c_parser_next_token_is (parser, CPP_NAME))
1590 {
1591 c_parser_error (parser, "expected %<;%>, identifier or %<(%>");
1592 parser->error = false;
1593 shadow_tag_warned (specs, 1);
1594 return;
1595 }
38b7bc7f 1596 else if (c_dialect_objc () && !auto_type_p)
c165dca7 1597 {
f7e71da5
IS
1598 /* Prefix attributes are an error on method decls. */
1599 switch (c_parser_peek_token (parser)->type)
1600 {
1601 case CPP_PLUS:
1602 case CPP_MINUS:
1603 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1604 return;
1605 if (specs->attrs)
1606 {
1607 warning_at (c_parser_peek_token (parser)->location,
1608 OPT_Wattributes,
1609 "prefix attributes are ignored for methods");
1610 specs->attrs = NULL_TREE;
1611 }
1612 if (fndef_ok)
1613 c_parser_objc_method_definition (parser);
1614 else
1615 c_parser_objc_methodproto (parser);
1616 return;
1617 break;
1618 default:
1619 break;
1620 }
c165dca7
IS
1621 /* This is where we parse 'attributes @interface ...',
1622 'attributes @implementation ...', 'attributes @protocol ...'
1623 (where attributes could be, for example, __attribute__
1624 ((deprecated)).
1625 */
1626 switch (c_parser_peek_token (parser)->keyword)
1627 {
1628 case RID_AT_INTERFACE:
1629 {
1630 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1631 return;
1632 c_parser_objc_class_definition (parser, specs->attrs);
1633 return;
1634 }
1635 break;
1636 case RID_AT_IMPLEMENTATION:
1637 {
1638 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1639 return;
1640 if (specs->attrs)
1641 {
1642 warning_at (c_parser_peek_token (parser)->location,
1643 OPT_Wattributes,
1644 "prefix attributes are ignored for implementations");
1645 specs->attrs = NULL_TREE;
1646 }
1647 c_parser_objc_class_definition (parser, NULL_TREE);
1648 return;
1649 }
1650 break;
1651 case RID_AT_PROTOCOL:
1652 {
1653 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1654 return;
1655 c_parser_objc_protocol_definition (parser, specs->attrs);
1656 return;
1657 }
1658 break;
668ea4b1
IS
1659 case RID_AT_ALIAS:
1660 case RID_AT_CLASS:
1661 case RID_AT_END:
1662 case RID_AT_PROPERTY:
1663 if (specs->attrs)
1664 {
96bbfbac 1665 c_parser_error (parser, "unexpected attribute");
668ea4b1
IS
1666 specs->attrs = NULL;
1667 }
1668 break;
c165dca7
IS
1669 default:
1670 break;
1671 }
1672 }
1673
27bf414c
JM
1674 pending_xref_error ();
1675 prefix_attrs = specs->attrs;
1676 all_prefix_attrs = prefix_attrs;
1677 specs->attrs = NULL_TREE;
1678 while (true)
1679 {
1680 struct c_declarator *declarator;
1681 bool dummy = false;
575bfb00 1682 timevar_id_t tv;
27bf414c
JM
1683 tree fnbody;
1684 /* Declaring either one or more declarators (in which case we
1685 should diagnose if there were no declaration specifiers) or a
1686 function definition (in which case the diagnostic for
1687 implicit int suffices). */
9e5b2115
PB
1688 declarator = c_parser_declarator (parser,
1689 specs->typespec_kind != ctsk_none,
27bf414c
JM
1690 C_DTR_NORMAL, &dummy);
1691 if (declarator == NULL)
1692 {
41958c28
BI
1693 if (omp_declare_simd_clauses.exists ()
1694 || !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
acf0174b
JJ
1695 c_finish_omp_declare_simd (parser, NULL_TREE, NULL_TREE,
1696 omp_declare_simd_clauses);
27bf414c
JM
1697 c_parser_skip_to_end_of_block_or_statement (parser);
1698 return;
1699 }
38b7bc7f
JM
1700 if (auto_type_p && declarator->kind != cdk_id)
1701 {
1702 error_at (here,
1703 "%<__auto_type%> requires a plain identifier"
1704 " as declarator");
1705 c_parser_skip_to_end_of_block_or_statement (parser);
1706 return;
1707 }
27bf414c
JM
1708 if (c_parser_next_token_is (parser, CPP_EQ)
1709 || c_parser_next_token_is (parser, CPP_COMMA)
1710 || c_parser_next_token_is (parser, CPP_SEMICOLON)
1711 || c_parser_next_token_is_keyword (parser, RID_ASM)
f05b9d93
NP
1712 || c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE)
1713 || c_parser_next_token_is_keyword (parser, RID_IN))
27bf414c
JM
1714 {
1715 tree asm_name = NULL_TREE;
1716 tree postfix_attrs = NULL_TREE;
1717 if (!diagnosed_no_specs && !specs->declspecs_seen_p)
1718 {
1719 diagnosed_no_specs = true;
509c9d60 1720 pedwarn (here, 0, "data definition has no type or storage class");
27bf414c
JM
1721 }
1722 /* Having seen a data definition, there cannot now be a
1723 function definition. */
1724 fndef_ok = false;
1725 if (c_parser_next_token_is_keyword (parser, RID_ASM))
1726 asm_name = c_parser_simple_asm_expr (parser);
1727 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
ae5ebda4
MP
1728 {
1729 postfix_attrs = c_parser_attributes (parser);
1730 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
1731 {
1732 /* This means there is an attribute specifier after
1733 the declarator in a function definition. Provide
1734 some more information for the user. */
1735 error_at (here, "attributes should be specified before the "
1736 "declarator in a function definition");
1737 c_parser_skip_to_end_of_block_or_statement (parser);
1738 return;
1739 }
1740 }
27bf414c
JM
1741 if (c_parser_next_token_is (parser, CPP_EQ))
1742 {
1743 tree d;
1744 struct c_expr init;
c2255bc4 1745 location_t init_loc;
27bf414c 1746 c_parser_consume_token (parser);
38b7bc7f
JM
1747 if (auto_type_p)
1748 {
1749 start_init (NULL_TREE, asm_name, global_bindings_p ());
1750 init_loc = c_parser_peek_token (parser)->location;
1751 init = c_parser_expr_no_commas (parser, NULL);
1752 if (TREE_CODE (init.value) == COMPONENT_REF
1753 && DECL_C_BIT_FIELD (TREE_OPERAND (init.value, 1)))
1754 error_at (here,
1755 "%<__auto_type%> used with a bit-field"
1756 " initializer");
1757 init = convert_lvalue_to_rvalue (init_loc, init, true, true);
1758 tree init_type = TREE_TYPE (init.value);
9698b078 1759 /* As with typeof, remove all qualifiers from atomic types. */
38b7bc7f
JM
1760 if (init_type != error_mark_node && TYPE_ATOMIC (init_type))
1761 init_type
9698b078 1762 = c_build_qualified_type (init_type, TYPE_UNQUALIFIED);
38b7bc7f
JM
1763 bool vm_type = variably_modified_type_p (init_type,
1764 NULL_TREE);
1765 if (vm_type)
1766 init.value = c_save_expr (init.value);
1767 finish_init ();
1768 specs->typespec_kind = ctsk_typeof;
1769 specs->locations[cdw_typedef] = init_loc;
1770 specs->typedef_p = true;
1771 specs->type = init_type;
1772 if (vm_type)
1773 {
1774 bool maybe_const = true;
1775 tree type_expr = c_fully_fold (init.value, false,
1776 &maybe_const);
1777 specs->expr_const_operands &= maybe_const;
1778 if (specs->expr)
1779 specs->expr = build2 (COMPOUND_EXPR,
1780 TREE_TYPE (type_expr),
1781 specs->expr, type_expr);
1782 else
1783 specs->expr = type_expr;
1784 }
1785 d = start_decl (declarator, specs, true,
1786 chainon (postfix_attrs, all_prefix_attrs));
1787 if (!d)
1788 d = error_mark_node;
41958c28
BI
1789 if (omp_declare_simd_clauses.exists ()
1790 || !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
38b7bc7f
JM
1791 c_finish_omp_declare_simd (parser, d, NULL_TREE,
1792 omp_declare_simd_clauses);
1793 }
1794 else
1795 {
1796 /* The declaration of the variable is in effect while
1797 its initializer is parsed. */
1798 d = start_decl (declarator, specs, true,
1799 chainon (postfix_attrs, all_prefix_attrs));
1800 if (!d)
1801 d = error_mark_node;
41958c28
BI
1802 if (omp_declare_simd_clauses.exists ()
1803 || !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
38b7bc7f
JM
1804 c_finish_omp_declare_simd (parser, d, NULL_TREE,
1805 omp_declare_simd_clauses);
1806 start_init (d, asm_name, global_bindings_p ());
1807 init_loc = c_parser_peek_token (parser)->location;
1808 init = c_parser_initializer (parser);
1809 finish_init ();
1810 }
27bf414c
JM
1811 if (d != error_mark_node)
1812 {
d033409e 1813 maybe_warn_string_init (init_loc, TREE_TYPE (d), init);
c2255bc4 1814 finish_decl (d, init_loc, init.value,
d033409e 1815 init.original_type, asm_name);
27bf414c
JM
1816 }
1817 }
1818 else
1819 {
38b7bc7f
JM
1820 if (auto_type_p)
1821 {
1822 error_at (here,
1823 "%<__auto_type%> requires an initialized "
1824 "data declaration");
1825 c_parser_skip_to_end_of_block_or_statement (parser);
1826 return;
1827 }
27bf414c
JM
1828 tree d = start_decl (declarator, specs, false,
1829 chainon (postfix_attrs,
1830 all_prefix_attrs));
41958c28
BI
1831 if (omp_declare_simd_clauses.exists ()
1832 || !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
acf0174b
JJ
1833 {
1834 tree parms = NULL_TREE;
1835 if (d && TREE_CODE (d) == FUNCTION_DECL)
1836 {
1837 struct c_declarator *ce = declarator;
1838 while (ce != NULL)
1839 if (ce->kind == cdk_function)
1840 {
1841 parms = ce->u.arg_info->parms;
1842 break;
1843 }
1844 else
1845 ce = ce->declarator;
1846 }
1847 if (parms)
1848 temp_store_parm_decls (d, parms);
1849 c_finish_omp_declare_simd (parser, d, parms,
1850 omp_declare_simd_clauses);
1851 if (parms)
1852 temp_pop_parm_decls ();
1853 }
27bf414c 1854 if (d)
c2255bc4 1855 finish_decl (d, UNKNOWN_LOCATION, NULL_TREE,
f05b9d93
NP
1856 NULL_TREE, asm_name);
1857
1858 if (c_parser_next_token_is_keyword (parser, RID_IN))
1859 {
1860 if (d)
1861 *objc_foreach_object_declaration = d;
1862 else
1863 *objc_foreach_object_declaration = error_mark_node;
1864 }
27bf414c
JM
1865 }
1866 if (c_parser_next_token_is (parser, CPP_COMMA))
1867 {
38b7bc7f
JM
1868 if (auto_type_p)
1869 {
1870 error_at (here,
1871 "%<__auto_type%> may only be used with"
1872 " a single declarator");
1873 c_parser_skip_to_end_of_block_or_statement (parser);
1874 return;
1875 }
27bf414c
JM
1876 c_parser_consume_token (parser);
1877 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
1878 all_prefix_attrs = chainon (c_parser_attributes (parser),
1879 prefix_attrs);
1880 else
1881 all_prefix_attrs = prefix_attrs;
1882 continue;
1883 }
1884 else if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1885 {
1886 c_parser_consume_token (parser);
1887 return;
1888 }
f05b9d93
NP
1889 else if (c_parser_next_token_is_keyword (parser, RID_IN))
1890 {
1891 /* This can only happen in Objective-C: we found the
1892 'in' that terminates the declaration inside an
1893 Objective-C foreach statement. Do not consume the
1894 token, so that the caller can use it to determine
1895 that this indeed is a foreach context. */
1896 return;
1897 }
27bf414c
JM
1898 else
1899 {
1900 c_parser_error (parser, "expected %<,%> or %<;%>");
1901 c_parser_skip_to_end_of_block_or_statement (parser);
1902 return;
1903 }
1904 }
38b7bc7f
JM
1905 else if (auto_type_p)
1906 {
1907 error_at (here,
1908 "%<__auto_type%> requires an initialized data declaration");
1909 c_parser_skip_to_end_of_block_or_statement (parser);
1910 return;
1911 }
27bf414c
JM
1912 else if (!fndef_ok)
1913 {
1914 c_parser_error (parser, "expected %<=%>, %<,%>, %<;%>, "
1915 "%<asm%> or %<__attribute__%>");
1916 c_parser_skip_to_end_of_block_or_statement (parser);
1917 return;
1918 }
1919 /* Function definition (nested or otherwise). */
1920 if (nested)
1921 {
c1771a20 1922 pedwarn (here, OPT_Wpedantic, "ISO C forbids nested functions");
d2784db4 1923 c_push_function_context ();
27bf414c
JM
1924 }
1925 if (!start_function (specs, declarator, all_prefix_attrs))
1926 {
1927 /* This can appear in many cases looking nothing like a
1928 function definition, so we don't give a more specific
1929 error suggesting there was one. */
1930 c_parser_error (parser, "expected %<=%>, %<,%>, %<;%>, %<asm%> "
1931 "or %<__attribute__%>");
1932 if (nested)
d2784db4 1933 c_pop_function_context ();
27bf414c
JM
1934 break;
1935 }
575bfb00
LC
1936
1937 if (DECL_DECLARED_INLINE_P (current_function_decl))
1938 tv = TV_PARSE_INLINE;
1939 else
1940 tv = TV_PARSE_FUNC;
1941 timevar_push (tv);
1942
27bf414c
JM
1943 /* Parse old-style parameter declarations. ??? Attributes are
1944 not allowed to start declaration specifiers here because of a
1945 syntax conflict between a function declaration with attribute
1946 suffix and a function definition with an attribute prefix on
1947 first old-style parameter declaration. Following the old
1948 parser, they are not accepted on subsequent old-style
1949 parameter declarations either. However, there is no
1950 ambiguity after the first declaration, nor indeed on the
1951 first as long as we don't allow postfix attributes after a
1952 declarator with a nonempty identifier list in a definition;
1953 and postfix attributes have never been accepted here in
1954 function definitions either. */
1955 while (c_parser_next_token_is_not (parser, CPP_EOF)
1956 && c_parser_next_token_is_not (parser, CPP_OPEN_BRACE))
32912286 1957 c_parser_declaration_or_fndef (parser, false, false, false,
acf0174b 1958 true, false, NULL, vNULL);
27bf414c 1959 store_parm_decls ();
41958c28
BI
1960 if (omp_declare_simd_clauses.exists ()
1961 || !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
acf0174b
JJ
1962 c_finish_omp_declare_simd (parser, current_function_decl, NULL_TREE,
1963 omp_declare_simd_clauses);
1751ecd6
AH
1964 DECL_STRUCT_FUNCTION (current_function_decl)->function_start_locus
1965 = c_parser_peek_token (parser)->location;
27bf414c 1966 fnbody = c_parser_compound_statement (parser);
b72271b9 1967 if (flag_cilkplus && contains_array_notation_expr (fnbody))
25c22937 1968 fnbody = expand_array_notation_exprs (fnbody);
27bf414c
JM
1969 if (nested)
1970 {
1971 tree decl = current_function_decl;
9f62cb92
JJ
1972 /* Mark nested functions as needing static-chain initially.
1973 lower_nested_functions will recompute it but the
1974 DECL_STATIC_CHAIN flag is also used before that happens,
1975 by initializer_constant_valid_p. See gcc.dg/nested-fn-2.c. */
1976 DECL_STATIC_CHAIN (decl) = 1;
27bf414c
JM
1977 add_stmt (fnbody);
1978 finish_function ();
d2784db4 1979 c_pop_function_context ();
c2255bc4 1980 add_stmt (build_stmt (DECL_SOURCE_LOCATION (decl), DECL_EXPR, decl));
27bf414c
JM
1981 }
1982 else
1983 {
1984 add_stmt (fnbody);
1985 finish_function ();
1986 }
575bfb00
LC
1987
1988 timevar_pop (tv);
27bf414c
JM
1989 break;
1990 }
1991}
1992
1993/* Parse an asm-definition (asm() outside a function body). This is a
1994 GNU extension.
1995
1996 asm-definition:
1997 simple-asm-expr ;
1998*/
1999
2000static void
2001c_parser_asm_definition (c_parser *parser)
2002{
2003 tree asm_str = c_parser_simple_asm_expr (parser);
27bf414c 2004 if (asm_str)
3dafb85c 2005 symtab->finalize_toplevel_asm (asm_str);
27bf414c
JM
2006 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
2007}
2008
48b0b196 2009/* Parse a static assertion (C11 6.7.10).
32912286
JM
2010
2011 static_assert-declaration:
2012 static_assert-declaration-no-semi ;
2013*/
2014
2015static void
2016c_parser_static_assert_declaration (c_parser *parser)
2017{
2018 c_parser_static_assert_declaration_no_semi (parser);
2019 if (parser->error
2020 || !c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
2021 c_parser_skip_to_end_of_block_or_statement (parser);
2022}
2023
48b0b196 2024/* Parse a static assertion (C11 6.7.10), without the trailing
32912286
JM
2025 semicolon.
2026
2027 static_assert-declaration-no-semi:
2028 _Static_assert ( constant-expression , string-literal )
2029*/
2030
2031static void
2032c_parser_static_assert_declaration_no_semi (c_parser *parser)
2033{
2034 location_t assert_loc, value_loc;
2035 tree value;
2036 tree string;
2037
2038 gcc_assert (c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT));
2039 assert_loc = c_parser_peek_token (parser)->location;
35aff4fb
MP
2040 if (flag_isoc99)
2041 pedwarn_c99 (assert_loc, OPT_Wpedantic,
32912286 2042 "ISO C99 does not support %<_Static_assert%>");
35aff4fb
MP
2043 else
2044 pedwarn_c99 (assert_loc, OPT_Wpedantic,
32912286 2045 "ISO C90 does not support %<_Static_assert%>");
32912286
JM
2046 c_parser_consume_token (parser);
2047 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2048 return;
2049 value_loc = c_parser_peek_token (parser)->location;
2050 value = c_parser_expr_no_commas (parser, NULL).value;
2051 parser->lex_untranslated_string = true;
2052 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
2053 {
2054 parser->lex_untranslated_string = false;
2055 return;
2056 }
2057 switch (c_parser_peek_token (parser)->type)
2058 {
2059 case CPP_STRING:
2060 case CPP_STRING16:
2061 case CPP_STRING32:
2062 case CPP_WSTRING:
2063 case CPP_UTF8STRING:
2064 string = c_parser_peek_token (parser)->value;
2065 c_parser_consume_token (parser);
2066 parser->lex_untranslated_string = false;
2067 break;
2068 default:
2069 c_parser_error (parser, "expected string literal");
2070 parser->lex_untranslated_string = false;
2071 return;
2072 }
2073 c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
2074
2075 if (!INTEGRAL_TYPE_P (TREE_TYPE (value)))
2076 {
2077 error_at (value_loc, "expression in static assertion is not an integer");
2078 return;
2079 }
2080 if (TREE_CODE (value) != INTEGER_CST)
2081 {
2082 value = c_fully_fold (value, false, NULL);
8d95fe25
MP
2083 /* Strip no-op conversions. */
2084 STRIP_TYPE_NOPS (value);
32912286 2085 if (TREE_CODE (value) == INTEGER_CST)
c1771a20 2086 pedwarn (value_loc, OPT_Wpedantic, "expression in static assertion "
32912286
JM
2087 "is not an integer constant expression");
2088 }
2089 if (TREE_CODE (value) != INTEGER_CST)
2090 {
2091 error_at (value_loc, "expression in static assertion is not constant");
2092 return;
2093 }
2094 constant_expression_warning (value);
2095 if (integer_zerop (value))
2096 error_at (assert_loc, "static assertion failed: %E", string);
2097}
2098
27bf414c
JM
2099/* Parse some declaration specifiers (possibly none) (C90 6.5, C99
2100 6.7), adding them to SPECS (which may already include some).
2101 Storage class specifiers are accepted iff SCSPEC_OK; type
568a31f2
MP
2102 specifiers are accepted iff TYPESPEC_OK; alignment specifiers are
2103 accepted iff ALIGNSPEC_OK; attributes are accepted at the start
38b7bc7f 2104 iff START_ATTR_OK; __auto_type is accepted iff AUTO_TYPE_OK.
27bf414c
JM
2105
2106 declaration-specifiers:
2107 storage-class-specifier declaration-specifiers[opt]
2108 type-specifier declaration-specifiers[opt]
2109 type-qualifier declaration-specifiers[opt]
2110 function-specifier declaration-specifiers[opt]
d19fa6b5 2111 alignment-specifier declaration-specifiers[opt]
27bf414c
JM
2112
2113 Function specifiers (inline) are from C99, and are currently
d19fa6b5 2114 handled as storage class specifiers, as is __thread. Alignment
48b0b196 2115 specifiers are from C11.
27bf414c
JM
2116
2117 C90 6.5.1, C99 6.7.1:
2118 storage-class-specifier:
2119 typedef
2120 extern
2121 static
2122 auto
2123 register
582d9b50
JM
2124 _Thread_local
2125
2126 (_Thread_local is new in C11.)
27bf414c
JM
2127
2128 C99 6.7.4:
2129 function-specifier:
2130 inline
c4b3a0a0
JM
2131 _Noreturn
2132
48b0b196 2133 (_Noreturn is new in C11.)
27bf414c
JM
2134
2135 C90 6.5.2, C99 6.7.2:
2136 type-specifier:
2137 void
2138 char
2139 short
2140 int
2141 long
2142 float
2143 double
2144 signed
2145 unsigned
2146 _Bool
2147 _Complex
2148 [_Imaginary removed in C99 TC2]
2149 struct-or-union-specifier
2150 enum-specifier
2151 typedef-name
267bac10 2152 atomic-type-specifier
27bf414c
JM
2153
2154 (_Bool and _Complex are new in C99.)
267bac10 2155 (atomic-type-specifier is new in C11.)
27bf414c
JM
2156
2157 C90 6.5.3, C99 6.7.3:
2158
2159 type-qualifier:
2160 const
2161 restrict
2162 volatile
36c5e70a 2163 address-space-qualifier
267bac10 2164 _Atomic
27bf414c
JM
2165
2166 (restrict is new in C99.)
267bac10 2167 (_Atomic is new in C11.)
27bf414c
JM
2168
2169 GNU extensions:
2170
2171 declaration-specifiers:
2172 attributes declaration-specifiers[opt]
2173
36c5e70a
BE
2174 type-qualifier:
2175 address-space
2176
2177 address-space:
2178 identifier recognized by the target
2179
27bf414c
JM
2180 storage-class-specifier:
2181 __thread
2182
2183 type-specifier:
2184 typeof-specifier
38b7bc7f 2185 __auto_type
78a7c317 2186 __intN
9a8ce21f
JG
2187 _Decimal32
2188 _Decimal64
2189 _Decimal128
ab22c1fa
CF
2190 _Fract
2191 _Accum
2192 _Sat
2193
2194 (_Fract, _Accum, and _Sat are new from ISO/IEC DTR 18037:
2195 http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1169.pdf)
27bf414c 2196
267bac10
JM
2197 atomic-type-specifier
2198 _Atomic ( type-name )
2199
27bf414c
JM
2200 Objective-C:
2201
2202 type-specifier:
2203 class-name objc-protocol-refs[opt]
2204 typedef-name objc-protocol-refs
2205 objc-protocol-refs
2206*/
2207
2208static void
2209c_parser_declspecs (c_parser *parser, struct c_declspecs *specs,
29ce73cb 2210 bool scspec_ok, bool typespec_ok, bool start_attr_ok,
38b7bc7f
JM
2211 bool alignspec_ok, bool auto_type_ok,
2212 enum c_lookahead_kind la)
27bf414c
JM
2213{
2214 bool attrs_ok = start_attr_ok;
9e5b2115 2215 bool seen_type = specs->typespec_kind != ctsk_none;
29ce73cb
PB
2216
2217 if (!typespec_ok)
2218 gcc_assert (la == cla_prefer_id);
2219
2220 while (c_parser_next_token_is (parser, CPP_NAME)
27bf414c
JM
2221 || c_parser_next_token_is (parser, CPP_KEYWORD)
2222 || (c_dialect_objc () && c_parser_next_token_is (parser, CPP_LESS)))
2223 {
2224 struct c_typespec t;
2225 tree attrs;
d19fa6b5 2226 tree align;
dc491a25 2227 location_t loc = c_parser_peek_token (parser)->location;
f725e721 2228
29ce73cb
PB
2229 /* If we cannot accept a type, exit if the next token must start
2230 one. Also, if we already have seen a tagged definition,
2231 a typename would be an error anyway and likely the user
2232 has simply forgotten a semicolon, so we exit. */
2233 if ((!typespec_ok || specs->typespec_kind == ctsk_tagdef)
2234 && c_parser_next_tokens_start_typename (parser, la)
2235 && !c_parser_next_token_is_qualifier (parser))
2236 break;
f725e721 2237
27bf414c
JM
2238 if (c_parser_next_token_is (parser, CPP_NAME))
2239 {
0b2c4be5
DS
2240 c_token *name_token = c_parser_peek_token (parser);
2241 tree value = name_token->value;
2242 c_id_kind kind = name_token->id_kind;
36c5e70a
BE
2243
2244 if (kind == C_ID_ADDRSPACE)
2245 {
2246 addr_space_t as
0b2c4be5
DS
2247 = name_token->keyword - RID_FIRST_ADDR_SPACE;
2248 declspecs_add_addrspace (name_token->location, specs, as);
36c5e70a
BE
2249 c_parser_consume_token (parser);
2250 attrs_ok = true;
2251 continue;
2252 }
2253
29ce73cb
PB
2254 gcc_assert (!c_parser_next_token_is_qualifier (parser));
2255
2256 /* If we cannot accept a type, and the next token must start one,
2257 exit. Do the same if we already have seen a tagged definition,
2258 since it would be an error anyway and likely the user has simply
2259 forgotten a semicolon. */
2260 if (seen_type || !c_parser_next_tokens_start_typename (parser, la))
2261 break;
2262
2263 /* Now at an unknown typename (C_ID_ID), a C_ID_TYPENAME or
2264 a C_ID_CLASSNAME. */
27bf414c
JM
2265 c_parser_consume_token (parser);
2266 seen_type = true;
2267 attrs_ok = true;
29ce73cb
PB
2268 if (kind == C_ID_ID)
2269 {
1c9f5f33 2270 error_at (loc, "unknown type name %qE", value);
29ce73cb
PB
2271 t.kind = ctsk_typedef;
2272 t.spec = error_mark_node;
2273 }
2274 else if (kind == C_ID_TYPENAME
2275 && (!c_dialect_objc ()
2276 || c_parser_next_token_is_not (parser, CPP_LESS)))
27bf414c
JM
2277 {
2278 t.kind = ctsk_typedef;
2279 /* For a typedef name, record the meaning, not the name.
2280 In case of 'foo foo, bar;'. */
2281 t.spec = lookup_name (value);
2282 }
2283 else
2284 {
2285 tree proto = NULL_TREE;
2286 gcc_assert (c_dialect_objc ());
2287 t.kind = ctsk_objc;
2288 if (c_parser_next_token_is (parser, CPP_LESS))
2289 proto = c_parser_objc_protocol_refs (parser);
2290 t.spec = objc_get_protocol_qualified_type (value, proto);
2291 }
29ce73cb
PB
2292 t.expr = NULL_TREE;
2293 t.expr_const_operands = true;
0b2c4be5 2294 declspecs_add_type (name_token->location, specs, t);
27bf414c
JM
2295 continue;
2296 }
2297 if (c_parser_next_token_is (parser, CPP_LESS))
2298 {
2299 /* Make "<SomeProtocol>" equivalent to "id <SomeProtocol>" -
2300 nisse@lysator.liu.se. */
2301 tree proto;
2302 gcc_assert (c_dialect_objc ());
2303 if (!typespec_ok || seen_type)
2304 break;
2305 proto = c_parser_objc_protocol_refs (parser);
2306 t.kind = ctsk_objc;
2307 t.spec = objc_get_protocol_qualified_type (NULL_TREE, proto);
928c19bb
JM
2308 t.expr = NULL_TREE;
2309 t.expr_const_operands = true;
dc491a25 2310 declspecs_add_type (loc, specs, t);
27bf414c
JM
2311 continue;
2312 }
2313 gcc_assert (c_parser_next_token_is (parser, CPP_KEYWORD));
2314 switch (c_parser_peek_token (parser)->keyword)
2315 {
2316 case RID_STATIC:
2317 case RID_EXTERN:
2318 case RID_REGISTER:
2319 case RID_TYPEDEF:
2320 case RID_INLINE:
bbceee64 2321 case RID_NORETURN:
27bf414c
JM
2322 case RID_AUTO:
2323 case RID_THREAD:
2324 if (!scspec_ok)
2325 goto out;
2326 attrs_ok = true;
bbceee64 2327 /* TODO: Distinguish between function specifiers (inline, noreturn)
27bf414c
JM
2328 and storage class specifiers, either here or in
2329 declspecs_add_scspec. */
0b2c4be5
DS
2330 declspecs_add_scspec (loc, specs,
2331 c_parser_peek_token (parser)->value);
27bf414c
JM
2332 c_parser_consume_token (parser);
2333 break;
38b7bc7f
JM
2334 case RID_AUTO_TYPE:
2335 if (!auto_type_ok)
2336 goto out;
2337 /* Fall through. */
27bf414c
JM
2338 case RID_UNSIGNED:
2339 case RID_LONG:
2340 case RID_SHORT:
2341 case RID_SIGNED:
2342 case RID_COMPLEX:
2343 case RID_INT:
2344 case RID_CHAR:
2345 case RID_FLOAT:
2346 case RID_DOUBLE:
2347 case RID_VOID:
9a8ce21f
JG
2348 case RID_DFLOAT32:
2349 case RID_DFLOAT64:
2350 case RID_DFLOAT128:
27bf414c 2351 case RID_BOOL:
ab22c1fa
CF
2352 case RID_FRACT:
2353 case RID_ACCUM:
2354 case RID_SAT:
78a7c317
DD
2355 case RID_INT_N_0:
2356 case RID_INT_N_1:
2357 case RID_INT_N_2:
2358 case RID_INT_N_3:
27bf414c
JM
2359 if (!typespec_ok)
2360 goto out;
2361 attrs_ok = true;
2362 seen_type = true;
0bacb8c7
TT
2363 if (c_dialect_objc ())
2364 parser->objc_need_raw_identifier = true;
27bf414c
JM
2365 t.kind = ctsk_resword;
2366 t.spec = c_parser_peek_token (parser)->value;
928c19bb
JM
2367 t.expr = NULL_TREE;
2368 t.expr_const_operands = true;
dc491a25 2369 declspecs_add_type (loc, specs, t);
27bf414c
JM
2370 c_parser_consume_token (parser);
2371 break;
2372 case RID_ENUM:
2373 if (!typespec_ok)
2374 goto out;
2375 attrs_ok = true;
2376 seen_type = true;
2377 t = c_parser_enum_specifier (parser);
dc491a25 2378 declspecs_add_type (loc, specs, t);
27bf414c
JM
2379 break;
2380 case RID_STRUCT:
2381 case RID_UNION:
2382 if (!typespec_ok)
2383 goto out;
2384 attrs_ok = true;
2385 seen_type = true;
2386 t = c_parser_struct_or_union_specifier (parser);
dab71827 2387 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, t.spec);
dc491a25 2388 declspecs_add_type (loc, specs, t);
27bf414c
JM
2389 break;
2390 case RID_TYPEOF:
2391 /* ??? The old parser rejected typeof after other type
2392 specifiers, but is a syntax error the best way of
2393 handling this? */
2394 if (!typespec_ok || seen_type)
2395 goto out;
2396 attrs_ok = true;
2397 seen_type = true;
2398 t = c_parser_typeof_specifier (parser);
dc491a25 2399 declspecs_add_type (loc, specs, t);
27bf414c 2400 break;
267bac10
JM
2401 case RID_ATOMIC:
2402 /* C parser handling of Objective-C constructs needs
2403 checking for correct lvalue-to-rvalue conversions, and
2404 the code in build_modify_expr handling various
2405 Objective-C cases, and that in build_unary_op handling
2406 Objective-C cases for increment / decrement, also needs
2407 updating; uses of TYPE_MAIN_VARIANT in objc_compare_types
2408 and objc_types_are_equivalent may also need updates. */
2409 if (c_dialect_objc ())
2410 sorry ("%<_Atomic%> in Objective-C");
2411 /* C parser handling of OpenMP constructs needs checking for
2412 correct lvalue-to-rvalue conversions. */
2413 if (flag_openmp)
2414 sorry ("%<_Atomic%> with OpenMP");
35aff4fb
MP
2415 if (flag_isoc99)
2416 pedwarn_c99 (loc, OPT_Wpedantic,
267bac10 2417 "ISO C99 does not support the %<_Atomic%> qualifier");
35aff4fb
MP
2418 else
2419 pedwarn_c99 (loc, OPT_Wpedantic,
267bac10 2420 "ISO C90 does not support the %<_Atomic%> qualifier");
267bac10
JM
2421 attrs_ok = true;
2422 tree value;
2423 value = c_parser_peek_token (parser)->value;
2424 c_parser_consume_token (parser);
2425 if (typespec_ok && c_parser_next_token_is (parser, CPP_OPEN_PAREN))
2426 {
2427 /* _Atomic ( type-name ). */
2428 seen_type = true;
2429 c_parser_consume_token (parser);
2430 struct c_type_name *type = c_parser_type_name (parser);
2431 t.kind = ctsk_typeof;
2432 t.spec = error_mark_node;
2433 t.expr = NULL_TREE;
2434 t.expr_const_operands = true;
2435 if (type != NULL)
2436 t.spec = groktypename (type, &t.expr,
2437 &t.expr_const_operands);
2438 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2439 "expected %<)%>");
2440 if (t.spec != error_mark_node)
2441 {
2442 if (TREE_CODE (t.spec) == ARRAY_TYPE)
2443 error_at (loc, "%<_Atomic%>-qualified array type");
2444 else if (TREE_CODE (t.spec) == FUNCTION_TYPE)
2445 error_at (loc, "%<_Atomic%>-qualified function type");
2446 else if (TYPE_QUALS (t.spec) != TYPE_UNQUALIFIED)
2447 error_at (loc, "%<_Atomic%> applied to a qualified type");
2448 else
2449 t.spec = c_build_qualified_type (t.spec, TYPE_QUAL_ATOMIC);
2450 }
2451 declspecs_add_type (loc, specs, t);
2452 }
2453 else
2454 declspecs_add_qual (loc, specs, value);
2455 break;
27bf414c
JM
2456 case RID_CONST:
2457 case RID_VOLATILE:
2458 case RID_RESTRICT:
2459 attrs_ok = true;
0b2c4be5 2460 declspecs_add_qual (loc, specs, c_parser_peek_token (parser)->value);
27bf414c
JM
2461 c_parser_consume_token (parser);
2462 break;
2463 case RID_ATTRIBUTE:
2464 if (!attrs_ok)
2465 goto out;
2466 attrs = c_parser_attributes (parser);
0b2c4be5 2467 declspecs_add_attrs (loc, specs, attrs);
27bf414c 2468 break;
d19fa6b5 2469 case RID_ALIGNAS:
568a31f2
MP
2470 if (!alignspec_ok)
2471 goto out;
d19fa6b5 2472 align = c_parser_alignas_specifier (parser);
0b2c4be5 2473 declspecs_add_alignas (loc, specs, align);
d19fa6b5 2474 break;
27bf414c
JM
2475 default:
2476 goto out;
2477 }
2478 }
2479 out: ;
2480}
2481
2482/* Parse an enum specifier (C90 6.5.2.2, C99 6.7.2.2).
2483
2484 enum-specifier:
2485 enum attributes[opt] identifier[opt] { enumerator-list } attributes[opt]
2486 enum attributes[opt] identifier[opt] { enumerator-list , } attributes[opt]
2487 enum attributes[opt] identifier
2488
2489 The form with trailing comma is new in C99. The forms with
2490 attributes are GNU extensions. In GNU C, we accept any expression
2491 without commas in the syntax (assignment expressions, not just
2492 conditional expressions); assignment expressions will be diagnosed
2493 as non-constant.
2494
2495 enumerator-list:
2496 enumerator
2497 enumerator-list , enumerator
2498
2499 enumerator:
2500 enumeration-constant
2501 enumeration-constant = constant-expression
2502*/
2503
2504static struct c_typespec
2505c_parser_enum_specifier (c_parser *parser)
2506{
2507 struct c_typespec ret;
2508 tree attrs;
2509 tree ident = NULL_TREE;
24b97832 2510 location_t enum_loc;
922f2908 2511 location_t ident_loc = UNKNOWN_LOCATION; /* Quiet warning. */
27bf414c 2512 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ENUM));
24b97832 2513 enum_loc = c_parser_peek_token (parser)->location;
27bf414c
JM
2514 c_parser_consume_token (parser);
2515 attrs = c_parser_attributes (parser);
c2255bc4 2516 enum_loc = c_parser_peek_token (parser)->location;
5af28c74
TT
2517 /* Set the location in case we create a decl now. */
2518 c_parser_set_source_position_from_token (c_parser_peek_token (parser));
27bf414c
JM
2519 if (c_parser_next_token_is (parser, CPP_NAME))
2520 {
2521 ident = c_parser_peek_token (parser)->value;
c7412148 2522 ident_loc = c_parser_peek_token (parser)->location;
24b97832 2523 enum_loc = ident_loc;
27bf414c
JM
2524 c_parser_consume_token (parser);
2525 }
2526 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
2527 {
2528 /* Parse an enum definition. */
7114359f 2529 struct c_enum_contents the_enum;
575bfb00 2530 tree type;
27bf414c
JM
2531 tree postfix_attrs;
2532 /* We chain the enumerators in reverse order, then put them in
2533 forward order at the end. */
575bfb00
LC
2534 tree values;
2535 timevar_push (TV_PARSE_ENUM);
2536 type = start_enum (enum_loc, &the_enum, ident);
2537 values = NULL_TREE;
27bf414c
JM
2538 c_parser_consume_token (parser);
2539 while (true)
2540 {
2541 tree enum_id;
2542 tree enum_value;
2543 tree enum_decl;
2544 bool seen_comma;
5af28c74 2545 c_token *token;
922f2908 2546 location_t comma_loc = UNKNOWN_LOCATION; /* Quiet warning. */
7370e0da 2547 location_t decl_loc, value_loc;
27bf414c
JM
2548 if (c_parser_next_token_is_not (parser, CPP_NAME))
2549 {
2550 c_parser_error (parser, "expected identifier");
2551 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
2552 values = error_mark_node;
2553 break;
2554 }
5af28c74
TT
2555 token = c_parser_peek_token (parser);
2556 enum_id = token->value;
2557 /* Set the location in case we create a decl now. */
2558 c_parser_set_source_position_from_token (token);
7370e0da 2559 decl_loc = value_loc = token->location;
27bf414c
JM
2560 c_parser_consume_token (parser);
2561 if (c_parser_next_token_is (parser, CPP_EQ))
2562 {
2563 c_parser_consume_token (parser);
85790e66 2564 value_loc = c_parser_peek_token (parser)->location;
27bf414c
JM
2565 enum_value = c_parser_expr_no_commas (parser, NULL).value;
2566 }
2567 else
2568 enum_value = NULL_TREE;
7370e0da 2569 enum_decl = build_enumerator (decl_loc, value_loc,
c2255bc4 2570 &the_enum, enum_id, enum_value);
27bf414c
JM
2571 TREE_CHAIN (enum_decl) = values;
2572 values = enum_decl;
2573 seen_comma = false;
2574 if (c_parser_next_token_is (parser, CPP_COMMA))
2575 {
c7412148 2576 comma_loc = c_parser_peek_token (parser)->location;
27bf414c
JM
2577 seen_comma = true;
2578 c_parser_consume_token (parser);
2579 }
2580 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2581 {
f3bede71
MP
2582 if (seen_comma)
2583 pedwarn_c90 (comma_loc, OPT_Wpedantic,
2584 "comma at end of enumerator list");
27bf414c
JM
2585 c_parser_consume_token (parser);
2586 break;
2587 }
2588 if (!seen_comma)
2589 {
2590 c_parser_error (parser, "expected %<,%> or %<}%>");
2591 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
2592 values = error_mark_node;
2593 break;
2594 }
2595 }
2596 postfix_attrs = c_parser_attributes (parser);
2597 ret.spec = finish_enum (type, nreverse (values),
2598 chainon (attrs, postfix_attrs));
2599 ret.kind = ctsk_tagdef;
928c19bb
JM
2600 ret.expr = NULL_TREE;
2601 ret.expr_const_operands = true;
575bfb00 2602 timevar_pop (TV_PARSE_ENUM);
27bf414c
JM
2603 return ret;
2604 }
2605 else if (!ident)
2606 {
2607 c_parser_error (parser, "expected %<{%>");
2608 ret.spec = error_mark_node;
2609 ret.kind = ctsk_tagref;
928c19bb
JM
2610 ret.expr = NULL_TREE;
2611 ret.expr_const_operands = true;
27bf414c
JM
2612 return ret;
2613 }
c2255bc4 2614 ret = parser_xref_tag (ident_loc, ENUMERAL_TYPE, ident);
27bf414c
JM
2615 /* In ISO C, enumerated types can be referred to only if already
2616 defined. */
2617 if (pedantic && !COMPLETE_TYPE_P (ret.spec))
c7412148
TT
2618 {
2619 gcc_assert (ident);
c1771a20 2620 pedwarn (enum_loc, OPT_Wpedantic,
509c9d60 2621 "ISO C forbids forward references to %<enum%> types");
c7412148 2622 }
27bf414c
JM
2623 return ret;
2624}
2625
2626/* Parse a struct or union specifier (C90 6.5.2.1, C99 6.7.2.1).
2627
2628 struct-or-union-specifier:
2629 struct-or-union attributes[opt] identifier[opt]
2630 { struct-contents } attributes[opt]
2631 struct-or-union attributes[opt] identifier
2632
2633 struct-contents:
2634 struct-declaration-list
2635
2636 struct-declaration-list:
2637 struct-declaration ;
2638 struct-declaration-list struct-declaration ;
2639
2640 GNU extensions:
2641
2642 struct-contents:
2643 empty
2644 struct-declaration
2645 struct-declaration-list struct-declaration
2646
2647 struct-declaration-list:
2648 struct-declaration-list ;
2649 ;
2650
2651 (Note that in the syntax here, unlike that in ISO C, the semicolons
2652 are included here rather than in struct-declaration, in order to
2653 describe the syntax with extra semicolons and missing semicolon at
2654 end.)
2655
2656 Objective-C:
2657
2658 struct-declaration-list:
2659 @defs ( class-name )
2660
2661 (Note this does not include a trailing semicolon, but can be
2662 followed by further declarations, and gets a pedwarn-if-pedantic
2663 when followed by a semicolon.) */
2664
2665static struct c_typespec
2666c_parser_struct_or_union_specifier (c_parser *parser)
2667{
2668 struct c_typespec ret;
2669 tree attrs;
2670 tree ident = NULL_TREE;
24b97832
ILT
2671 location_t struct_loc;
2672 location_t ident_loc = UNKNOWN_LOCATION;
27bf414c
JM
2673 enum tree_code code;
2674 switch (c_parser_peek_token (parser)->keyword)
2675 {
2676 case RID_STRUCT:
2677 code = RECORD_TYPE;
2678 break;
2679 case RID_UNION:
2680 code = UNION_TYPE;
2681 break;
2682 default:
2683 gcc_unreachable ();
2684 }
24b97832 2685 struct_loc = c_parser_peek_token (parser)->location;
27bf414c
JM
2686 c_parser_consume_token (parser);
2687 attrs = c_parser_attributes (parser);
c2255bc4 2688
5af28c74
TT
2689 /* Set the location in case we create a decl now. */
2690 c_parser_set_source_position_from_token (c_parser_peek_token (parser));
c2255bc4 2691
27bf414c
JM
2692 if (c_parser_next_token_is (parser, CPP_NAME))
2693 {
2694 ident = c_parser_peek_token (parser)->value;
24b97832
ILT
2695 ident_loc = c_parser_peek_token (parser)->location;
2696 struct_loc = ident_loc;
27bf414c
JM
2697 c_parser_consume_token (parser);
2698 }
2699 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
2700 {
2701 /* Parse a struct or union definition. Start the scope of the
2702 tag before parsing components. */
dc491a25
ILT
2703 struct c_struct_parse_info *struct_info;
2704 tree type = start_struct (struct_loc, code, ident, &struct_info);
27bf414c
JM
2705 tree postfix_attrs;
2706 /* We chain the components in reverse order, then put them in
2707 forward order at the end. Each struct-declaration may
2708 declare multiple components (comma-separated), so we must use
2709 chainon to join them, although when parsing each
2710 struct-declaration we can use TREE_CHAIN directly.
2711
2712 The theory behind all this is that there will be more
2713 semicolon separated fields than comma separated fields, and
2714 so we'll be minimizing the number of node traversals required
2715 by chainon. */
575bfb00
LC
2716 tree contents;
2717 timevar_push (TV_PARSE_STRUCT);
2718 contents = NULL_TREE;
27bf414c
JM
2719 c_parser_consume_token (parser);
2720 /* Handle the Objective-C @defs construct,
2721 e.g. foo(sizeof(struct{ @defs(ClassName) }));. */
2722 if (c_parser_next_token_is_keyword (parser, RID_AT_DEFS))
2723 {
2724 tree name;
2725 gcc_assert (c_dialect_objc ());
2726 c_parser_consume_token (parser);
2727 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2728 goto end_at_defs;
2729 if (c_parser_next_token_is (parser, CPP_NAME)
2730 && c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME)
2731 {
2732 name = c_parser_peek_token (parser)->value;
2733 c_parser_consume_token (parser);
2734 }
2735 else
2736 {
2737 c_parser_error (parser, "expected class name");
2738 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
2739 goto end_at_defs;
2740 }
2741 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2742 "expected %<)%>");
2743 contents = nreverse (objc_get_class_ivars (name));
2744 }
2745 end_at_defs:
2746 /* Parse the struct-declarations and semicolons. Problems with
2747 semicolons are diagnosed here; empty structures are diagnosed
2748 elsewhere. */
2749 while (true)
2750 {
2751 tree decls;
2752 /* Parse any stray semicolon. */
2753 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
2754 {
c1771a20 2755 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
509c9d60 2756 "extra semicolon in struct or union specified");
27bf414c
JM
2757 c_parser_consume_token (parser);
2758 continue;
2759 }
2760 /* Stop if at the end of the struct or union contents. */
2761 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2762 {
2763 c_parser_consume_token (parser);
2764 break;
2765 }
bc4071dd
RH
2766 /* Accept #pragmas at struct scope. */
2767 if (c_parser_next_token_is (parser, CPP_PRAGMA))
2768 {
acf0174b 2769 c_parser_pragma (parser, pragma_struct);
bc4071dd
RH
2770 continue;
2771 }
27bf414c
JM
2772 /* Parse some comma-separated declarations, but not the
2773 trailing semicolon if any. */
2774 decls = c_parser_struct_declaration (parser);
2775 contents = chainon (decls, contents);
2776 /* If no semicolon follows, either we have a parse error or
2777 are at the end of the struct or union and should
2778 pedwarn. */
2779 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
2780 c_parser_consume_token (parser);
2781 else
2782 {
2783 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
b8698a0f 2784 pedwarn (c_parser_peek_token (parser)->location, 0,
509c9d60 2785 "no semicolon at end of struct or union");
f725e721
PB
2786 else if (parser->error
2787 || !c_parser_next_token_starts_declspecs (parser))
27bf414c
JM
2788 {
2789 c_parser_error (parser, "expected %<;%>");
2790 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
2791 break;
2792 }
f725e721
PB
2793
2794 /* If we come here, we have already emitted an error
2795 for an expected `;', identifier or `(', and we also
2796 recovered already. Go on with the next field. */
27bf414c
JM
2797 }
2798 }
2799 postfix_attrs = c_parser_attributes (parser);
c2255bc4 2800 ret.spec = finish_struct (struct_loc, type, nreverse (contents),
dc491a25 2801 chainon (attrs, postfix_attrs), struct_info);
27bf414c 2802 ret.kind = ctsk_tagdef;
928c19bb
JM
2803 ret.expr = NULL_TREE;
2804 ret.expr_const_operands = true;
575bfb00 2805 timevar_pop (TV_PARSE_STRUCT);
27bf414c
JM
2806 return ret;
2807 }
2808 else if (!ident)
2809 {
2810 c_parser_error (parser, "expected %<{%>");
2811 ret.spec = error_mark_node;
2812 ret.kind = ctsk_tagref;
928c19bb
JM
2813 ret.expr = NULL_TREE;
2814 ret.expr_const_operands = true;
67c2939d 2815 return ret;
27bf414c 2816 }
c2255bc4 2817 ret = parser_xref_tag (ident_loc, code, ident);
27bf414c
JM
2818 return ret;
2819}
2820
2821/* Parse a struct-declaration (C90 6.5.2.1, C99 6.7.2.1), *without*
2822 the trailing semicolon.
2823
2824 struct-declaration:
2825 specifier-qualifier-list struct-declarator-list
32912286 2826 static_assert-declaration-no-semi
27bf414c
JM
2827
2828 specifier-qualifier-list:
2829 type-specifier specifier-qualifier-list[opt]
2830 type-qualifier specifier-qualifier-list[opt]
2831 attributes specifier-qualifier-list[opt]
2832
2833 struct-declarator-list:
2834 struct-declarator
2835 struct-declarator-list , attributes[opt] struct-declarator
2836
2837 struct-declarator:
2838 declarator attributes[opt]
2839 declarator[opt] : constant-expression attributes[opt]
2840
2841 GNU extensions:
2842
2843 struct-declaration:
2844 __extension__ struct-declaration
2845 specifier-qualifier-list
2846
2847 Unlike the ISO C syntax, semicolons are handled elsewhere. The use
2848 of attributes where shown is a GNU extension. In GNU C, we accept
2849 any expression without commas in the syntax (assignment
2850 expressions, not just conditional expressions); assignment
2851 expressions will be diagnosed as non-constant. */
2852
2853static tree
2854c_parser_struct_declaration (c_parser *parser)
2855{
2856 struct c_declspecs *specs;
2857 tree prefix_attrs;
2858 tree all_prefix_attrs;
2859 tree decls;
c7412148 2860 location_t decl_loc;
27bf414c
JM
2861 if (c_parser_next_token_is_keyword (parser, RID_EXTENSION))
2862 {
2863 int ext;
2864 tree decl;
2865 ext = disable_extension_diagnostics ();
2866 c_parser_consume_token (parser);
2867 decl = c_parser_struct_declaration (parser);
2868 restore_extension_diagnostics (ext);
2869 return decl;
2870 }
32912286
JM
2871 if (c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT))
2872 {
2873 c_parser_static_assert_declaration_no_semi (parser);
2874 return NULL_TREE;
2875 }
27bf414c 2876 specs = build_null_declspecs ();
c7412148 2877 decl_loc = c_parser_peek_token (parser)->location;
f28aa681
MP
2878 /* Strictly by the standard, we shouldn't allow _Alignas here,
2879 but it appears to have been intended to allow it there, so
2880 we're keeping it as it is until WG14 reaches a conclusion
2881 of N1731.
2882 <http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1731.pdf> */
568a31f2 2883 c_parser_declspecs (parser, specs, false, true, true,
38b7bc7f 2884 true, false, cla_nonabstract_decl);
27bf414c 2885 if (parser->error)
67c2939d 2886 return NULL_TREE;
27bf414c
JM
2887 if (!specs->declspecs_seen_p)
2888 {
2889 c_parser_error (parser, "expected specifier-qualifier-list");
2890 return NULL_TREE;
2891 }
2892 finish_declspecs (specs);
b8cbdff5
JM
2893 if (c_parser_next_token_is (parser, CPP_SEMICOLON)
2894 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
27bf414c
JM
2895 {
2896 tree ret;
9e5b2115 2897 if (specs->typespec_kind == ctsk_none)
27bf414c 2898 {
c1771a20 2899 pedwarn (decl_loc, OPT_Wpedantic,
509c9d60 2900 "ISO C forbids member declarations with no members");
27bf414c
JM
2901 shadow_tag_warned (specs, pedantic);
2902 ret = NULL_TREE;
2903 }
2904 else
2905 {
2906 /* Support for unnamed structs or unions as members of
2907 structs or unions (which is [a] useful and [b] supports
2908 MS P-SDK). */
b9baeecd 2909 tree attrs = NULL;
3d10ed6c
AH
2910
2911 ret = grokfield (c_parser_peek_token (parser)->location,
2912 build_id_declarator (NULL_TREE), specs,
b9baeecd 2913 NULL_TREE, &attrs);
0ad7e054
RS
2914 if (ret)
2915 decl_attributes (&ret, attrs, 0);
27bf414c
JM
2916 }
2917 return ret;
2918 }
f725e721
PB
2919
2920 /* Provide better error recovery. Note that a type name here is valid,
2921 and will be treated as a field name. */
2922 if (specs->typespec_kind == ctsk_tagdef
2923 && TREE_CODE (specs->type) != ENUMERAL_TYPE
2924 && c_parser_next_token_starts_declspecs (parser)
2925 && !c_parser_next_token_is (parser, CPP_NAME))
2926 {
2927 c_parser_error (parser, "expected %<;%>, identifier or %<(%>");
2928 parser->error = false;
2929 return NULL_TREE;
2930 }
2931
27bf414c
JM
2932 pending_xref_error ();
2933 prefix_attrs = specs->attrs;
2934 all_prefix_attrs = prefix_attrs;
2935 specs->attrs = NULL_TREE;
2936 decls = NULL_TREE;
2937 while (true)
2938 {
2939 /* Declaring one or more declarators or un-named bit-fields. */
2940 struct c_declarator *declarator;
2941 bool dummy = false;
2942 if (c_parser_next_token_is (parser, CPP_COLON))
2943 declarator = build_id_declarator (NULL_TREE);
2944 else
9e5b2115
PB
2945 declarator = c_parser_declarator (parser,
2946 specs->typespec_kind != ctsk_none,
27bf414c
JM
2947 C_DTR_NORMAL, &dummy);
2948 if (declarator == NULL)
2949 {
2950 c_parser_skip_to_end_of_block_or_statement (parser);
2951 break;
2952 }
2953 if (c_parser_next_token_is (parser, CPP_COLON)
2954 || c_parser_next_token_is (parser, CPP_COMMA)
2955 || c_parser_next_token_is (parser, CPP_SEMICOLON)
2956 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE)
2957 || c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2958 {
2959 tree postfix_attrs = NULL_TREE;
2960 tree width = NULL_TREE;
2961 tree d;
2962 if (c_parser_next_token_is (parser, CPP_COLON))
2963 {
2964 c_parser_consume_token (parser);
2965 width = c_parser_expr_no_commas (parser, NULL).value;
2966 }
2967 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2968 postfix_attrs = c_parser_attributes (parser);
3d10ed6c
AH
2969 d = grokfield (c_parser_peek_token (parser)->location,
2970 declarator, specs, width, &all_prefix_attrs);
27bf414c
JM
2971 decl_attributes (&d, chainon (postfix_attrs,
2972 all_prefix_attrs), 0);
910ad8de 2973 DECL_CHAIN (d) = decls;
27bf414c
JM
2974 decls = d;
2975 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2976 all_prefix_attrs = chainon (c_parser_attributes (parser),
2977 prefix_attrs);
2978 else
2979 all_prefix_attrs = prefix_attrs;
2980 if (c_parser_next_token_is (parser, CPP_COMMA))
2981 c_parser_consume_token (parser);
2982 else if (c_parser_next_token_is (parser, CPP_SEMICOLON)
2983 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2984 {
2985 /* Semicolon consumed in caller. */
2986 break;
2987 }
2988 else
2989 {
2990 c_parser_error (parser, "expected %<,%>, %<;%> or %<}%>");
2991 break;
2992 }
2993 }
2994 else
2995 {
2996 c_parser_error (parser,
2997 "expected %<:%>, %<,%>, %<;%>, %<}%> or "
2998 "%<__attribute__%>");
2999 break;
3000 }
3001 }
3002 return decls;
3003}
3004
3005/* Parse a typeof specifier (a GNU extension).
3006
3007 typeof-specifier:
3008 typeof ( expression )
3009 typeof ( type-name )
3010*/
3011
3012static struct c_typespec
3013c_parser_typeof_specifier (c_parser *parser)
3014{
3015 struct c_typespec ret;
3016 ret.kind = ctsk_typeof;
3017 ret.spec = error_mark_node;
928c19bb
JM
3018 ret.expr = NULL_TREE;
3019 ret.expr_const_operands = true;
27bf414c
JM
3020 gcc_assert (c_parser_next_token_is_keyword (parser, RID_TYPEOF));
3021 c_parser_consume_token (parser);
7d882b83 3022 c_inhibit_evaluation_warnings++;
27bf414c
JM
3023 in_typeof++;
3024 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3025 {
7d882b83 3026 c_inhibit_evaluation_warnings--;
27bf414c
JM
3027 in_typeof--;
3028 return ret;
3029 }
29ce73cb 3030 if (c_parser_next_tokens_start_typename (parser, cla_prefer_id))
27bf414c
JM
3031 {
3032 struct c_type_name *type = c_parser_type_name (parser);
7d882b83 3033 c_inhibit_evaluation_warnings--;
27bf414c
JM
3034 in_typeof--;
3035 if (type != NULL)
3036 {
928c19bb 3037 ret.spec = groktypename (type, &ret.expr, &ret.expr_const_operands);
27bf414c
JM
3038 pop_maybe_used (variably_modified_type_p (ret.spec, NULL_TREE));
3039 }
3040 }
3041 else
3042 {
52ffd86e 3043 bool was_vm;
c7412148 3044 location_t here = c_parser_peek_token (parser)->location;
27bf414c 3045 struct c_expr expr = c_parser_expression (parser);
7d882b83 3046 c_inhibit_evaluation_warnings--;
27bf414c
JM
3047 in_typeof--;
3048 if (TREE_CODE (expr.value) == COMPONENT_REF
3049 && DECL_C_BIT_FIELD (TREE_OPERAND (expr.value, 1)))
3ba09659 3050 error_at (here, "%<typeof%> applied to a bit-field");
ebfbbdc5 3051 mark_exp_read (expr.value);
27bf414c 3052 ret.spec = TREE_TYPE (expr.value);
52ffd86e 3053 was_vm = variably_modified_type_p (ret.spec, NULL_TREE);
928c19bb
JM
3054 /* This is returned with the type so that when the type is
3055 evaluated, this can be evaluated. */
3056 if (was_vm)
3057 ret.expr = c_fully_fold (expr.value, false, &ret.expr_const_operands);
52ffd86e 3058 pop_maybe_used (was_vm);
9698b078
SH
3059 /* For use in macros such as those in <stdatomic.h>, remove all
3060 qualifiers from atomic types. (const can be an issue for more macros
3061 using typeof than just the <stdatomic.h> ones.) */
267bac10 3062 if (ret.spec != error_mark_node && TYPE_ATOMIC (ret.spec))
9698b078 3063 ret.spec = c_build_qualified_type (ret.spec, TYPE_UNQUALIFIED);
27bf414c
JM
3064 }
3065 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
3066 return ret;
3067}
3068
d19fa6b5
JM
3069/* Parse an alignment-specifier.
3070
48b0b196 3071 C11 6.7.5:
d19fa6b5
JM
3072
3073 alignment-specifier:
3074 _Alignas ( type-name )
3075 _Alignas ( constant-expression )
3076*/
3077
3078static tree
3079c_parser_alignas_specifier (c_parser * parser)
3080{
3081 tree ret = error_mark_node;
3082 location_t loc = c_parser_peek_token (parser)->location;
3083 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ALIGNAS));
3084 c_parser_consume_token (parser);
35aff4fb
MP
3085 if (flag_isoc99)
3086 pedwarn_c99 (loc, OPT_Wpedantic,
d19fa6b5 3087 "ISO C99 does not support %<_Alignas%>");
35aff4fb
MP
3088 else
3089 pedwarn_c99 (loc, OPT_Wpedantic,
d19fa6b5 3090 "ISO C90 does not support %<_Alignas%>");
d19fa6b5
JM
3091 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3092 return ret;
3093 if (c_parser_next_tokens_start_typename (parser, cla_prefer_id))
3094 {
3095 struct c_type_name *type = c_parser_type_name (parser);
3096 if (type != NULL)
296674db
JM
3097 ret = c_sizeof_or_alignof_type (loc, groktypename (type, NULL, NULL),
3098 false, true, 1);
d19fa6b5
JM
3099 }
3100 else
3101 ret = c_parser_expr_no_commas (parser, NULL).value;
3102 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
3103 return ret;
3104}
3105
27bf414c
JM
3106/* Parse a declarator, possibly an abstract declarator (C90 6.5.4,
3107 6.5.5, C99 6.7.5, 6.7.6). If TYPE_SEEN_P then a typedef name may
3108 be redeclared; otherwise it may not. KIND indicates which kind of
3109 declarator is wanted. Returns a valid declarator except in the
3110 case of a syntax error in which case NULL is returned. *SEEN_ID is
3111 set to true if an identifier being declared is seen; this is used
3112 to diagnose bad forms of abstract array declarators and to
3113 determine whether an identifier list is syntactically permitted.
3114
3115 declarator:
3116 pointer[opt] direct-declarator
3117
3118 direct-declarator:
3119 identifier
3120 ( attributes[opt] declarator )
3121 direct-declarator array-declarator
3122 direct-declarator ( parameter-type-list )
3123 direct-declarator ( identifier-list[opt] )
3124
3125 pointer:
3126 * type-qualifier-list[opt]
3127 * type-qualifier-list[opt] pointer
3128
3129 type-qualifier-list:
3130 type-qualifier
3131 attributes
3132 type-qualifier-list type-qualifier
3133 type-qualifier-list attributes
3134
568a31f2
MP
3135 array-declarator:
3136 [ type-qualifier-list[opt] assignment-expression[opt] ]
3137 [ static type-qualifier-list[opt] assignment-expression ]
3138 [ type-qualifier-list static assignment-expression ]
3139 [ type-qualifier-list[opt] * ]
3140
27bf414c
JM
3141 parameter-type-list:
3142 parameter-list
3143 parameter-list , ...
3144
3145 parameter-list:
3146 parameter-declaration
3147 parameter-list , parameter-declaration
3148
3149 parameter-declaration:
3150 declaration-specifiers declarator attributes[opt]
3151 declaration-specifiers abstract-declarator[opt] attributes[opt]
3152
3153 identifier-list:
3154 identifier
3155 identifier-list , identifier
3156
3157 abstract-declarator:
3158 pointer
3159 pointer[opt] direct-abstract-declarator
3160
3161 direct-abstract-declarator:
3162 ( attributes[opt] abstract-declarator )
3163 direct-abstract-declarator[opt] array-declarator
3164 direct-abstract-declarator[opt] ( parameter-type-list[opt] )
3165
3166 GNU extensions:
3167
3168 direct-declarator:
3169 direct-declarator ( parameter-forward-declarations
3170 parameter-type-list[opt] )
3171
3172 direct-abstract-declarator:
c22cacf3 3173 direct-abstract-declarator[opt] ( parameter-forward-declarations
27bf414c
JM
3174 parameter-type-list[opt] )
3175
3176 parameter-forward-declarations:
3177 parameter-list ;
3178 parameter-forward-declarations parameter-list ;
3179
3180 The uses of attributes shown above are GNU extensions.
3181
3182 Some forms of array declarator are not included in C99 in the
3183 syntax for abstract declarators; these are disallowed elsewhere.
3184 This may be a defect (DR#289).
3185
3186 This function also accepts an omitted abstract declarator as being
3187 an abstract declarator, although not part of the formal syntax. */
3188
3189static struct c_declarator *
3190c_parser_declarator (c_parser *parser, bool type_seen_p, c_dtr_syn kind,
3191 bool *seen_id)
3192{
3193 /* Parse any initial pointer part. */
3194 if (c_parser_next_token_is (parser, CPP_MULT))
3195 {
3196 struct c_declspecs *quals_attrs = build_null_declspecs ();
3197 struct c_declarator *inner;
3198 c_parser_consume_token (parser);
568a31f2 3199 c_parser_declspecs (parser, quals_attrs, false, false, true,
38b7bc7f 3200 false, false, cla_prefer_id);
27bf414c
JM
3201 inner = c_parser_declarator (parser, type_seen_p, kind, seen_id);
3202 if (inner == NULL)
3203 return NULL;
3204 else
3205 return make_pointer_declarator (quals_attrs, inner);
3206 }
3207 /* Now we have a direct declarator, direct abstract declarator or
3208 nothing (which counts as a direct abstract declarator here). */
3209 return c_parser_direct_declarator (parser, type_seen_p, kind, seen_id);
3210}
3211
3212/* Parse a direct declarator or direct abstract declarator; arguments
3213 as c_parser_declarator. */
3214
3215static struct c_declarator *
3216c_parser_direct_declarator (c_parser *parser, bool type_seen_p, c_dtr_syn kind,
3217 bool *seen_id)
3218{
3219 /* The direct declarator must start with an identifier (possibly
3220 omitted) or a parenthesized declarator (possibly abstract). In
3221 an ordinary declarator, initial parentheses must start a
3222 parenthesized declarator. In an abstract declarator or parameter
3223 declarator, they could start a parenthesized declarator or a
3224 parameter list. To tell which, the open parenthesis and any
3225 following attributes must be read. If a declaration specifier
3226 follows, then it is a parameter list; if the specifier is a
3227 typedef name, there might be an ambiguity about redeclaring it,
3228 which is resolved in the direction of treating it as a typedef
3229 name. If a close parenthesis follows, it is also an empty
3230 parameter list, as the syntax does not permit empty abstract
0fa2e4df 3231 declarators. Otherwise, it is a parenthesized declarator (in
27bf414c
JM
3232 which case the analysis may be repeated inside it, recursively).
3233
3234 ??? There is an ambiguity in a parameter declaration "int
3235 (__attribute__((foo)) x)", where x is not a typedef name: it
3236 could be an abstract declarator for a function, or declare x with
3237 parentheses. The proper resolution of this ambiguity needs
3238 documenting. At present we follow an accident of the old
3239 parser's implementation, whereby the first parameter must have
3240 some declaration specifiers other than just attributes. Thus as
0fa2e4df 3241 a parameter declaration it is treated as a parenthesized
27bf414c
JM
3242 parameter named x, and as an abstract declarator it is
3243 rejected.
3244
3245 ??? Also following the old parser, attributes inside an empty
3246 parameter list are ignored, making it a list not yielding a
3247 prototype, rather than giving an error or making it have one
3248 parameter with implicit type int.
3249
3250 ??? Also following the old parser, typedef names may be
3251 redeclared in declarators, but not Objective-C class names. */
3252
3253 if (kind != C_DTR_ABSTRACT
3254 && c_parser_next_token_is (parser, CPP_NAME)
3255 && ((type_seen_p
a6341d57
NP
3256 && (c_parser_peek_token (parser)->id_kind == C_ID_TYPENAME
3257 || c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME))
27bf414c
JM
3258 || c_parser_peek_token (parser)->id_kind == C_ID_ID))
3259 {
3260 struct c_declarator *inner
3261 = build_id_declarator (c_parser_peek_token (parser)->value);
3262 *seen_id = true;
6037d88d 3263 inner->id_loc = c_parser_peek_token (parser)->location;
27bf414c
JM
3264 c_parser_consume_token (parser);
3265 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
3266 }
3267
3268 if (kind != C_DTR_NORMAL
3269 && c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
3270 {
3271 struct c_declarator *inner = build_id_declarator (NULL_TREE);
3272 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
3273 }
3274
3275 /* Either we are at the end of an abstract declarator, or we have
3276 parentheses. */
3277
3278 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
3279 {
3280 tree attrs;
3281 struct c_declarator *inner;
3282 c_parser_consume_token (parser);
3283 attrs = c_parser_attributes (parser);
3284 if (kind != C_DTR_NORMAL
3285 && (c_parser_next_token_starts_declspecs (parser)
3286 || c_parser_next_token_is (parser, CPP_CLOSE_PAREN)))
3287 {
3288 struct c_arg_info *args
3289 = c_parser_parms_declarator (parser, kind == C_DTR_NORMAL,
3290 attrs);
3291 if (args == NULL)
3292 return NULL;
3293 else
3294 {
3295 inner
3296 = build_function_declarator (args,
3297 build_id_declarator (NULL_TREE));
3298 return c_parser_direct_declarator_inner (parser, *seen_id,
3299 inner);
3300 }
3301 }
3302 /* A parenthesized declarator. */
3303 inner = c_parser_declarator (parser, type_seen_p, kind, seen_id);
3304 if (inner != NULL && attrs != NULL)
3305 inner = build_attrs_declarator (attrs, inner);
3306 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3307 {
3308 c_parser_consume_token (parser);
3309 if (inner == NULL)
3310 return NULL;
3311 else
3312 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
3313 }
3314 else
3315 {
3316 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3317 "expected %<)%>");
3318 return NULL;
3319 }
3320 }
3321 else
3322 {
3323 if (kind == C_DTR_NORMAL)
3324 {
3325 c_parser_error (parser, "expected identifier or %<(%>");
3326 return NULL;
3327 }
3328 else
3329 return build_id_declarator (NULL_TREE);
3330 }
3331}
3332
3333/* Parse part of a direct declarator or direct abstract declarator,
3334 given that some (in INNER) has already been parsed; ID_PRESENT is
3335 true if an identifier is present, false for an abstract
3336 declarator. */
3337
3338static struct c_declarator *
3339c_parser_direct_declarator_inner (c_parser *parser, bool id_present,
3340 struct c_declarator *inner)
3341{
3342 /* Parse a sequence of array declarators and parameter lists. */
3343 if (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
3344 {
b8698a0f 3345 location_t brace_loc = c_parser_peek_token (parser)->location;
27bf414c
JM
3346 struct c_declarator *declarator;
3347 struct c_declspecs *quals_attrs = build_null_declspecs ();
3348 bool static_seen;
3349 bool star_seen;
267bac10
JM
3350 struct c_expr dimen;
3351 dimen.value = NULL_TREE;
3352 dimen.original_code = ERROR_MARK;
3353 dimen.original_type = NULL_TREE;
27bf414c 3354 c_parser_consume_token (parser);
568a31f2 3355 c_parser_declspecs (parser, quals_attrs, false, false, true,
38b7bc7f 3356 false, false, cla_prefer_id);
27bf414c
JM
3357 static_seen = c_parser_next_token_is_keyword (parser, RID_STATIC);
3358 if (static_seen)
3359 c_parser_consume_token (parser);
3360 if (static_seen && !quals_attrs->declspecs_seen_p)
568a31f2 3361 c_parser_declspecs (parser, quals_attrs, false, false, true,
38b7bc7f 3362 false, false, cla_prefer_id);
27bf414c
JM
3363 if (!quals_attrs->declspecs_seen_p)
3364 quals_attrs = NULL;
3365 /* If "static" is present, there must be an array dimension.
3366 Otherwise, there may be a dimension, "*", or no
3367 dimension. */
3368 if (static_seen)
3369 {
3370 star_seen = false;
267bac10 3371 dimen = c_parser_expr_no_commas (parser, NULL);
27bf414c
JM
3372 }
3373 else
3374 {
3375 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
3376 {
267bac10 3377 dimen.value = NULL_TREE;
27bf414c
JM
3378 star_seen = false;
3379 }
b72271b9 3380 else if (flag_cilkplus
36536d79
BI
3381 && c_parser_next_token_is (parser, CPP_COLON))
3382 {
267bac10 3383 dimen.value = error_mark_node;
36536d79
BI
3384 star_seen = false;
3385 error_at (c_parser_peek_token (parser)->location,
3386 "array notations cannot be used in declaration");
3387 c_parser_consume_token (parser);
3388 }
27bf414c
JM
3389 else if (c_parser_next_token_is (parser, CPP_MULT))
3390 {
3391 if (c_parser_peek_2nd_token (parser)->type == CPP_CLOSE_SQUARE)
3392 {
267bac10 3393 dimen.value = NULL_TREE;
27bf414c
JM
3394 star_seen = true;
3395 c_parser_consume_token (parser);
3396 }
3397 else
3398 {
3399 star_seen = false;
267bac10 3400 dimen = c_parser_expr_no_commas (parser, NULL);
27bf414c
JM
3401 }
3402 }
3403 else
3404 {
3405 star_seen = false;
267bac10 3406 dimen = c_parser_expr_no_commas (parser, NULL);
27bf414c
JM
3407 }
3408 }
3409 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
3410 c_parser_consume_token (parser);
b72271b9 3411 else if (flag_cilkplus
36536d79
BI
3412 && c_parser_next_token_is (parser, CPP_COLON))
3413 {
3414 error_at (c_parser_peek_token (parser)->location,
3415 "array notations cannot be used in declaration");
3416 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
3417 return NULL;
3418 }
27bf414c
JM
3419 else
3420 {
3421 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
3422 "expected %<]%>");
3423 return NULL;
3424 }
267bac10
JM
3425 if (dimen.value)
3426 dimen = convert_lvalue_to_rvalue (brace_loc, dimen, true, true);
3427 declarator = build_array_declarator (brace_loc, dimen.value, quals_attrs,
c2255bc4 3428 static_seen, star_seen);
52ffd86e
MS
3429 if (declarator == NULL)
3430 return NULL;
6ac0194d 3431 inner = set_array_declarator_inner (declarator, inner);
27bf414c
JM
3432 return c_parser_direct_declarator_inner (parser, id_present, inner);
3433 }
3434 else if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
3435 {
3436 tree attrs;
3437 struct c_arg_info *args;
3438 c_parser_consume_token (parser);
3439 attrs = c_parser_attributes (parser);
3440 args = c_parser_parms_declarator (parser, id_present, attrs);
3441 if (args == NULL)
3442 return NULL;
3443 else
3444 {
3445 inner = build_function_declarator (args, inner);
3446 return c_parser_direct_declarator_inner (parser, id_present, inner);
3447 }
3448 }
3449 return inner;
3450}
3451
3452/* Parse a parameter list or identifier list, including the closing
3453 parenthesis but not the opening one. ATTRS are the attributes at
3454 the start of the list. ID_LIST_OK is true if an identifier list is
3455 acceptable; such a list must not have attributes at the start. */
3456
3457static struct c_arg_info *
3458c_parser_parms_declarator (c_parser *parser, bool id_list_ok, tree attrs)
3459{
3460 push_scope ();
3461 declare_parm_level ();
3462 /* If the list starts with an identifier, it is an identifier list.
3463 Otherwise, it is either a prototype list or an empty list. */
3464 if (id_list_ok
3465 && !attrs
3466 && c_parser_next_token_is (parser, CPP_NAME)
29ce73cb
PB
3467 && c_parser_peek_token (parser)->id_kind == C_ID_ID
3468
3469 /* Look ahead to detect typos in type names. */
3470 && c_parser_peek_2nd_token (parser)->type != CPP_NAME
3471 && c_parser_peek_2nd_token (parser)->type != CPP_MULT
3472 && c_parser_peek_2nd_token (parser)->type != CPP_OPEN_PAREN
3473 && c_parser_peek_2nd_token (parser)->type != CPP_OPEN_SQUARE)
27bf414c 3474 {
7fa3585c 3475 tree list = NULL_TREE, *nextp = &list;
27bf414c
JM
3476 while (c_parser_next_token_is (parser, CPP_NAME)
3477 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
3478 {
7fa3585c
GK
3479 *nextp = build_tree_list (NULL_TREE,
3480 c_parser_peek_token (parser)->value);
3481 nextp = & TREE_CHAIN (*nextp);
27bf414c
JM
3482 c_parser_consume_token (parser);
3483 if (c_parser_next_token_is_not (parser, CPP_COMMA))
3484 break;
3485 c_parser_consume_token (parser);
3486 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3487 {
3488 c_parser_error (parser, "expected identifier");
3489 break;
3490 }
3491 }
3492 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3493 {
b3399d18 3494 struct c_arg_info *ret = build_arg_info ();
27bf414c 3495 ret->types = list;
27bf414c
JM
3496 c_parser_consume_token (parser);
3497 pop_scope ();
3498 return ret;
3499 }
3500 else
3501 {
3502 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3503 "expected %<)%>");
3504 pop_scope ();
3505 return NULL;
3506 }
3507 }
3508 else
3509 {
a04a722b
JM
3510 struct c_arg_info *ret = c_parser_parms_list_declarator (parser, attrs,
3511 NULL);
27bf414c
JM
3512 pop_scope ();
3513 return ret;
3514 }
3515}
3516
3517/* Parse a parameter list (possibly empty), including the closing
3518 parenthesis but not the opening one. ATTRS are the attributes at
a04a722b
JM
3519 the start of the list. EXPR is NULL or an expression that needs to
3520 be evaluated for the side effects of array size expressions in the
3521 parameters. */
27bf414c
JM
3522
3523static struct c_arg_info *
a04a722b 3524c_parser_parms_list_declarator (c_parser *parser, tree attrs, tree expr)
27bf414c 3525{
09a1e889 3526 bool bad_parm = false;
a04a722b 3527
27bf414c
JM
3528 /* ??? Following the old parser, forward parameter declarations may
3529 use abstract declarators, and if no real parameter declarations
3530 follow the forward declarations then this is not diagnosed. Also
3531 note as above that attributes are ignored as the only contents of
3532 the parentheses, or as the only contents after forward
3533 declarations. */
3534 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3535 {
b3399d18 3536 struct c_arg_info *ret = build_arg_info ();
27bf414c
JM
3537 c_parser_consume_token (parser);
3538 return ret;
3539 }
3540 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
3541 {
b3399d18 3542 struct c_arg_info *ret = build_arg_info ();
6637388f
TG
3543
3544 if (flag_allow_parameterless_variadic_functions)
3545 {
3546 /* F (...) is allowed. */
3547 ret->types = NULL_TREE;
3548 }
3549 else
3550 {
3551 /* Suppress -Wold-style-definition for this case. */
3552 ret->types = error_mark_node;
3553 error_at (c_parser_peek_token (parser)->location,
3554 "ISO C requires a named argument before %<...%>");
3555 }
27bf414c
JM
3556 c_parser_consume_token (parser);
3557 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3558 {
3559 c_parser_consume_token (parser);
3560 return ret;
3561 }
3562 else
3563 {
3564 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3565 "expected %<)%>");
3566 return NULL;
3567 }
3568 }
3569 /* Nonempty list of parameters, either terminated with semicolon
3570 (forward declarations; recurse) or with close parenthesis (normal
3571 function) or with ", ... )" (variadic function). */
3572 while (true)
3573 {
3574 /* Parse a parameter. */
3575 struct c_parm *parm = c_parser_parameter_declaration (parser, attrs);
3576 attrs = NULL_TREE;
09a1e889
SZ
3577 if (parm == NULL)
3578 bad_parm = true;
3579 else
a04a722b 3580 push_parm_decl (parm, &expr);
27bf414c
JM
3581 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
3582 {
3583 tree new_attrs;
3584 c_parser_consume_token (parser);
91d975b8 3585 mark_forward_parm_decls ();
27bf414c 3586 new_attrs = c_parser_attributes (parser);
a04a722b 3587 return c_parser_parms_list_declarator (parser, new_attrs, expr);
27bf414c
JM
3588 }
3589 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3590 {
3591 c_parser_consume_token (parser);
09a1e889 3592 if (bad_parm)
a04a722b 3593 return NULL;
09a1e889 3594 else
a04a722b 3595 return get_parm_info (false, expr);
27bf414c
JM
3596 }
3597 if (!c_parser_require (parser, CPP_COMMA,
3598 "expected %<;%>, %<,%> or %<)%>"))
3599 {
3600 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
3601 return NULL;
3602 }
3603 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
3604 {
3605 c_parser_consume_token (parser);
3606 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3607 {
3608 c_parser_consume_token (parser);
09a1e889 3609 if (bad_parm)
a04a722b 3610 return NULL;
09a1e889 3611 else
a04a722b 3612 return get_parm_info (true, expr);
27bf414c
JM
3613 }
3614 else
3615 {
3616 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3617 "expected %<)%>");
3618 return NULL;
3619 }
3620 }
3621 }
3622}
3623
3624/* Parse a parameter declaration. ATTRS are the attributes at the
3625 start of the declaration if it is the first parameter. */
3626
3627static struct c_parm *
3628c_parser_parameter_declaration (c_parser *parser, tree attrs)
3629{
3630 struct c_declspecs *specs;
3631 struct c_declarator *declarator;
3632 tree prefix_attrs;
3633 tree postfix_attrs = NULL_TREE;
3634 bool dummy = false;
51a1aacf
TG
3635
3636 /* Accept #pragmas between parameter declarations. */
3637 while (c_parser_next_token_is (parser, CPP_PRAGMA))
acf0174b 3638 c_parser_pragma (parser, pragma_param);
51a1aacf 3639
27bf414c
JM
3640 if (!c_parser_next_token_starts_declspecs (parser))
3641 {
09a1e889
SZ
3642 c_token *token = c_parser_peek_token (parser);
3643 if (parser->error)
3644 return NULL;
3645 c_parser_set_source_position_from_token (token);
29ce73cb 3646 if (c_parser_next_tokens_start_typename (parser, cla_prefer_type))
09a1e889 3647 {
1c9f5f33 3648 error_at (token->location, "unknown type name %qE", token->value);
09a1e889
SZ
3649 parser->error = true;
3650 }
27bf414c
JM
3651 /* ??? In some Objective-C cases '...' isn't applicable so there
3652 should be a different message. */
09a1e889
SZ
3653 else
3654 c_parser_error (parser,
3655 "expected declaration specifiers or %<...%>");
27bf414c
JM
3656 c_parser_skip_to_end_of_parameter (parser);
3657 return NULL;
3658 }
3659 specs = build_null_declspecs ();
3660 if (attrs)
3661 {
0b2c4be5 3662 declspecs_add_attrs (input_location, specs, attrs);
27bf414c
JM
3663 attrs = NULL_TREE;
3664 }
38b7bc7f 3665 c_parser_declspecs (parser, specs, true, true, true, true, false,
568a31f2 3666 cla_nonabstract_decl);
27bf414c
JM
3667 finish_declspecs (specs);
3668 pending_xref_error ();
3669 prefix_attrs = specs->attrs;
3670 specs->attrs = NULL_TREE;
9e5b2115
PB
3671 declarator = c_parser_declarator (parser,
3672 specs->typespec_kind != ctsk_none,
27bf414c
JM
3673 C_DTR_PARM, &dummy);
3674 if (declarator == NULL)
3675 {
3676 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
3677 return NULL;
3678 }
3679 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
3680 postfix_attrs = c_parser_attributes (parser);
3681 return build_c_parm (specs, chainon (postfix_attrs, prefix_attrs),
3682 declarator);
3683}
3684
3685/* Parse a string literal in an asm expression. It should not be
3686 translated, and wide string literals are an error although
3687 permitted by the syntax. This is a GNU extension.
3688
3689 asm-string-literal:
3690 string-literal
3691
3692 ??? At present, following the old parser, the caller needs to have
46c2514e
TT
3693 set lex_untranslated_string to 1. It would be better to follow the
3694 C++ parser rather than using this kludge. */
27bf414c
JM
3695
3696static tree
3697c_parser_asm_string_literal (c_parser *parser)
3698{
3699 tree str;
87f9e23d
TT
3700 int save_flag = warn_overlength_strings;
3701 warn_overlength_strings = 0;
27bf414c
JM
3702 if (c_parser_next_token_is (parser, CPP_STRING))
3703 {
3704 str = c_parser_peek_token (parser)->value;
3705 c_parser_consume_token (parser);
3706 }
3707 else if (c_parser_next_token_is (parser, CPP_WSTRING))
3708 {
3ba09659
AH
3709 error_at (c_parser_peek_token (parser)->location,
3710 "wide string literal in %<asm%>");
27bf414c
JM
3711 str = build_string (1, "");
3712 c_parser_consume_token (parser);
3713 }
3714 else
3715 {
3716 c_parser_error (parser, "expected string literal");
3717 str = NULL_TREE;
3718 }
87f9e23d 3719 warn_overlength_strings = save_flag;
27bf414c
JM
3720 return str;
3721}
3722
3723/* Parse a simple asm expression. This is used in restricted
3724 contexts, where a full expression with inputs and outputs does not
3725 make sense. This is a GNU extension.
3726
3727 simple-asm-expr:
3728 asm ( asm-string-literal )
3729*/
3730
3731static tree
3732c_parser_simple_asm_expr (c_parser *parser)
3733{
3734 tree str;
3735 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ASM));
3736 /* ??? Follow the C++ parser rather than using the
46c2514e
TT
3737 lex_untranslated_string kludge. */
3738 parser->lex_untranslated_string = true;
27bf414c
JM
3739 c_parser_consume_token (parser);
3740 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3741 {
46c2514e 3742 parser->lex_untranslated_string = false;
27bf414c
JM
3743 return NULL_TREE;
3744 }
3745 str = c_parser_asm_string_literal (parser);
46c2514e 3746 parser->lex_untranslated_string = false;
27bf414c
JM
3747 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
3748 {
3749 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
3750 return NULL_TREE;
3751 }
3752 return str;
3753}
3754
0a35513e
AH
3755static tree
3756c_parser_attribute_any_word (c_parser *parser)
3757{
3758 tree attr_name = NULL_TREE;
3759
3760 if (c_parser_next_token_is (parser, CPP_KEYWORD))
3761 {
3762 /* ??? See comment above about what keywords are accepted here. */
3763 bool ok;
3764 switch (c_parser_peek_token (parser)->keyword)
3765 {
3766 case RID_STATIC:
3767 case RID_UNSIGNED:
3768 case RID_LONG:
0a35513e
AH
3769 case RID_CONST:
3770 case RID_EXTERN:
3771 case RID_REGISTER:
3772 case RID_TYPEDEF:
3773 case RID_SHORT:
3774 case RID_INLINE:
3775 case RID_NORETURN:
3776 case RID_VOLATILE:
3777 case RID_SIGNED:
3778 case RID_AUTO:
3779 case RID_RESTRICT:
3780 case RID_COMPLEX:
3781 case RID_THREAD:
3782 case RID_INT:
3783 case RID_CHAR:
3784 case RID_FLOAT:
3785 case RID_DOUBLE:
3786 case RID_VOID:
3787 case RID_DFLOAT32:
3788 case RID_DFLOAT64:
3789 case RID_DFLOAT128:
3790 case RID_BOOL:
3791 case RID_FRACT:
3792 case RID_ACCUM:
3793 case RID_SAT:
3794 case RID_TRANSACTION_ATOMIC:
3795 case RID_TRANSACTION_CANCEL:
267bac10 3796 case RID_ATOMIC:
38b7bc7f 3797 case RID_AUTO_TYPE:
78a7c317
DD
3798 case RID_INT_N_0:
3799 case RID_INT_N_1:
3800 case RID_INT_N_2:
3801 case RID_INT_N_3:
0a35513e
AH
3802 ok = true;
3803 break;
3804 default:
3805 ok = false;
3806 break;
3807 }
3808 if (!ok)
3809 return NULL_TREE;
3810
3811 /* Accept __attribute__((__const)) as __attribute__((const)) etc. */
3812 attr_name = ridpointers[(int) c_parser_peek_token (parser)->keyword];
3813 }
3814 else if (c_parser_next_token_is (parser, CPP_NAME))
3815 attr_name = c_parser_peek_token (parser)->value;
3816
3817 return attr_name;
3818}
3819
41958c28
BI
3820/* Returns true of NAME is an IDENTIFIER_NODE with identiifer "vector,"
3821 "__vector" or "__vector__." */
3822
3823static inline bool
3824is_cilkplus_vector_p (tree name)
3825{
b72271b9 3826 if (flag_cilkplus && is_attribute_p ("vector", name))
41958c28
BI
3827 return true;
3828 return false;
3829}
3830
3831#define CILK_SIMD_FN_CLAUSE_MASK \
3832 ((OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_VECTORLENGTH) \
3833 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_LINEAR) \
3834 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_UNIFORM) \
3835 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_MASK) \
3836 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_NOMASK))
3837
3838/* Parses the vector attribute of SIMD enabled functions in Cilk Plus.
3839 VEC_TOKEN is the "vector" token that is replaced with "simd" and
3840 pushed into the token list.
3841 Syntax:
3842 vector
3843 vector (<vector attributes>). */
3844
3845static void
3846c_parser_cilk_simd_fn_vector_attrs (c_parser *parser, c_token vec_token)
3847{
3848 gcc_assert (is_cilkplus_vector_p (vec_token.value));
3849
3850 int paren_scope = 0;
3851 vec_safe_push (parser->cilk_simd_fn_tokens, vec_token);
3852 /* Consume the "vector" token. */
3853 c_parser_consume_token (parser);
3854
3855 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
3856 {
3857 c_parser_consume_token (parser);
3858 paren_scope++;
3859 }
3860 while (paren_scope > 0)
3861 {
3862 c_token *token = c_parser_peek_token (parser);
3863 if (token->type == CPP_OPEN_PAREN)
3864 paren_scope++;
3865 else if (token->type == CPP_CLOSE_PAREN)
3866 paren_scope--;
3867 /* Do not push the last ')' since we are not pushing the '('. */
3868 if (!(token->type == CPP_CLOSE_PAREN && paren_scope == 0))
3869 vec_safe_push (parser->cilk_simd_fn_tokens, *token);
3870 c_parser_consume_token (parser);
3871 }
3872
3873 /* Since we are converting an attribute to a pragma, we need to end the
3874 attribute with PRAGMA_EOL. */
3875 c_token eol_token;
3876 memset (&eol_token, 0, sizeof (eol_token));
3877 eol_token.type = CPP_PRAGMA_EOL;
3878 vec_safe_push (parser->cilk_simd_fn_tokens, eol_token);
3879}
3880
3881/* Add 2 CPP_EOF at the end of PARSER->ELEM_FN_TOKENS vector. */
3882
3883static void
3884c_finish_cilk_simd_fn_tokens (c_parser *parser)
3885{
3886 c_token last_token = parser->cilk_simd_fn_tokens->last ();
3887
3888 /* c_parser_attributes is called in several places, so if these EOF
3889 tokens are already inserted, then don't do them again. */
3890 if (last_token.type == CPP_EOF)
3891 return;
3892
3893 /* Two CPP_EOF token are added as a safety net since the normal C
3894 front-end has two token look-ahead. */
3895 c_token eof_token;
3896 eof_token.type = CPP_EOF;
3897 vec_safe_push (parser->cilk_simd_fn_tokens, eof_token);
3898 vec_safe_push (parser->cilk_simd_fn_tokens, eof_token);
3899}
3900
27bf414c
JM
3901/* Parse (possibly empty) attributes. This is a GNU extension.
3902
3903 attributes:
3904 empty
3905 attributes attribute
3906
3907 attribute:
3908 __attribute__ ( ( attribute-list ) )
3909
3910 attribute-list:
3911 attrib
3912 attribute_list , attrib
3913
3914 attrib:
3915 empty
3916 any-word
3917 any-word ( identifier )
3918 any-word ( identifier , nonempty-expr-list )
3919 any-word ( expr-list )
3920
3921 where the "identifier" must not be declared as a type, and
3922 "any-word" may be any identifier (including one declared as a
3923 type), a reserved word storage class specifier, type specifier or
3924 type qualifier. ??? This still leaves out most reserved keywords
3925 (following the old parser), shouldn't we include them, and why not
3926 allow identifiers declared as types to start the arguments? */
3927
3928static tree
3929c_parser_attributes (c_parser *parser)
3930{
3931 tree attrs = NULL_TREE;
3932 while (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
3933 {
3934 /* ??? Follow the C++ parser rather than using the
46c2514e
TT
3935 lex_untranslated_string kludge. */
3936 parser->lex_untranslated_string = true;
27bf414c
JM
3937 c_parser_consume_token (parser);
3938 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3939 {
46c2514e 3940 parser->lex_untranslated_string = false;
27bf414c
JM
3941 return attrs;
3942 }
3943 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3944 {
46c2514e 3945 parser->lex_untranslated_string = false;
27bf414c
JM
3946 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
3947 return attrs;
3948 }
3949 /* Parse the attribute list. */
3950 while (c_parser_next_token_is (parser, CPP_COMMA)
3951 || c_parser_next_token_is (parser, CPP_NAME)
3952 || c_parser_next_token_is (parser, CPP_KEYWORD))
3953 {
3954 tree attr, attr_name, attr_args;
9771b263 3955 vec<tree, va_gc> *expr_list;
27bf414c
JM
3956 if (c_parser_next_token_is (parser, CPP_COMMA))
3957 {
3958 c_parser_consume_token (parser);
3959 continue;
3960 }
0a35513e
AH
3961
3962 attr_name = c_parser_attribute_any_word (parser);
3963 if (attr_name == NULL)
3964 break;
41958c28
BI
3965 if (is_cilkplus_vector_p (attr_name))
3966 {
3967 c_token *v_token = c_parser_peek_token (parser);
3968 c_parser_cilk_simd_fn_vector_attrs (parser, *v_token);
3969 continue;
3970 }
27bf414c
JM
3971 c_parser_consume_token (parser);
3972 if (c_parser_next_token_is_not (parser, CPP_OPEN_PAREN))
3973 {
3974 attr = build_tree_list (attr_name, NULL_TREE);
3975 attrs = chainon (attrs, attr);
3976 continue;
3977 }
3978 c_parser_consume_token (parser);
3979 /* Parse the attribute contents. If they start with an
3980 identifier which is followed by a comma or close
3981 parenthesis, then the arguments start with that
91ebb981
IS
3982 identifier; otherwise they are an expression list.
3983 In objective-c the identifier may be a classname. */
27bf414c 3984 if (c_parser_next_token_is (parser, CPP_NAME)
91ebb981 3985 && (c_parser_peek_token (parser)->id_kind == C_ID_ID
661a0813
MP
3986 || (c_dialect_objc ()
3987 && c_parser_peek_token (parser)->id_kind
3988 == C_ID_CLASSNAME))
27bf414c
JM
3989 && ((c_parser_peek_2nd_token (parser)->type == CPP_COMMA)
3990 || (c_parser_peek_2nd_token (parser)->type
661a0813
MP
3991 == CPP_CLOSE_PAREN))
3992 && (attribute_takes_identifier_p (attr_name)
3993 || (c_dialect_objc ()
3994 && c_parser_peek_token (parser)->id_kind
3995 == C_ID_CLASSNAME)))
27bf414c
JM
3996 {
3997 tree arg1 = c_parser_peek_token (parser)->value;
3998 c_parser_consume_token (parser);
3999 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4000 attr_args = build_tree_list (NULL_TREE, arg1);
4001 else
4002 {
bbbbb16a 4003 tree tree_list;
27bf414c 4004 c_parser_consume_token (parser);
1a4049e7 4005 expr_list = c_parser_expr_list (parser, false, true,
81e5eca8 4006 NULL, NULL, NULL, NULL);
c166b898 4007 tree_list = build_tree_list_vec (expr_list);
bbbbb16a 4008 attr_args = tree_cons (NULL_TREE, arg1, tree_list);
c166b898 4009 release_tree_vector (expr_list);
27bf414c
JM
4010 }
4011 }
4012 else
4013 {
4014 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4015 attr_args = NULL_TREE;
4016 else
bbbbb16a 4017 {
1a4049e7 4018 expr_list = c_parser_expr_list (parser, false, true,
81e5eca8 4019 NULL, NULL, NULL, NULL);
c166b898
ILT
4020 attr_args = build_tree_list_vec (expr_list);
4021 release_tree_vector (expr_list);
bbbbb16a 4022 }
27bf414c
JM
4023 }
4024 attr = build_tree_list (attr_name, attr_args);
4025 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4026 c_parser_consume_token (parser);
4027 else
4028 {
46c2514e 4029 parser->lex_untranslated_string = false;
27bf414c
JM
4030 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
4031 "expected %<)%>");
4032 return attrs;
4033 }
4034 attrs = chainon (attrs, attr);
4035 }
4036 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4037 c_parser_consume_token (parser);
4038 else
4039 {
46c2514e 4040 parser->lex_untranslated_string = false;
27bf414c
JM
4041 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
4042 "expected %<)%>");
4043 return attrs;
4044 }
4045 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4046 c_parser_consume_token (parser);
4047 else
4048 {
46c2514e 4049 parser->lex_untranslated_string = false;
27bf414c
JM
4050 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
4051 "expected %<)%>");
4052 return attrs;
4053 }
46c2514e 4054 parser->lex_untranslated_string = false;
27bf414c 4055 }
41958c28 4056
b72271b9 4057 if (flag_cilkplus && !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
41958c28 4058 c_finish_cilk_simd_fn_tokens (parser);
27bf414c
JM
4059 return attrs;
4060}
4061
4062/* Parse a type name (C90 6.5.5, C99 6.7.6).
4063
4064 type-name:
4065 specifier-qualifier-list abstract-declarator[opt]
4066*/
4067
4068static struct c_type_name *
4069c_parser_type_name (c_parser *parser)
4070{
4071 struct c_declspecs *specs = build_null_declspecs ();
4072 struct c_declarator *declarator;
4073 struct c_type_name *ret;
4074 bool dummy = false;
38b7bc7f 4075 c_parser_declspecs (parser, specs, false, true, true, false, false,
568a31f2 4076 cla_prefer_type);
27bf414c
JM
4077 if (!specs->declspecs_seen_p)
4078 {
4079 c_parser_error (parser, "expected specifier-qualifier-list");
4080 return NULL;
4081 }
29ce73cb
PB
4082 if (specs->type != error_mark_node)
4083 {
4084 pending_xref_error ();
4085 finish_declspecs (specs);
4086 }
9e5b2115
PB
4087 declarator = c_parser_declarator (parser,
4088 specs->typespec_kind != ctsk_none,
27bf414c
JM
4089 C_DTR_ABSTRACT, &dummy);
4090 if (declarator == NULL)
4091 return NULL;
4092 ret = XOBNEW (&parser_obstack, struct c_type_name);
4093 ret->specs = specs;
4094 ret->declarator = declarator;
4095 return ret;
4096}
4097
4098/* Parse an initializer (C90 6.5.7, C99 6.7.8).
4099
4100 initializer:
4101 assignment-expression
4102 { initializer-list }
4103 { initializer-list , }
4104
4105 initializer-list:
4106 designation[opt] initializer
4107 initializer-list , designation[opt] initializer
4108
4109 designation:
4110 designator-list =
4111
4112 designator-list:
4113 designator
4114 designator-list designator
4115
4116 designator:
4117 array-designator
4118 . identifier
4119
4120 array-designator:
4121 [ constant-expression ]
4122
4123 GNU extensions:
4124
4125 initializer:
4126 { }
4127
4128 designation:
4129 array-designator
4130 identifier :
4131
4132 array-designator:
4133 [ constant-expression ... constant-expression ]
4134
4135 Any expression without commas is accepted in the syntax for the
4136 constant-expressions, with non-constant expressions rejected later.
4137
4138 This function is only used for top-level initializers; for nested
4139 ones, see c_parser_initval. */
4140
4141static struct c_expr
4142c_parser_initializer (c_parser *parser)
4143{
4144 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
4145 return c_parser_braced_init (parser, NULL_TREE, false);
4146 else
46bdb9cf
JM
4147 {
4148 struct c_expr ret;
c2255bc4 4149 location_t loc = c_parser_peek_token (parser)->location;
46bdb9cf
JM
4150 ret = c_parser_expr_no_commas (parser, NULL);
4151 if (TREE_CODE (ret.value) != STRING_CST
4152 && TREE_CODE (ret.value) != COMPOUND_LITERAL_EXPR)
267bac10 4153 ret = convert_lvalue_to_rvalue (loc, ret, true, true);
46bdb9cf
JM
4154 return ret;
4155 }
27bf414c
JM
4156}
4157
4158/* Parse a braced initializer list. TYPE is the type specified for a
4159 compound literal, and NULL_TREE for other initializers and for
4160 nested braced lists. NESTED_P is true for nested braced lists,
4161 false for the list of a compound literal or the list that is the
4162 top-level initializer in a declaration. */
4163
4164static struct c_expr
4165c_parser_braced_init (c_parser *parser, tree type, bool nested_p)
4166{
a1e3b3d9
LB
4167 struct c_expr ret;
4168 struct obstack braced_init_obstack;
c7412148 4169 location_t brace_loc = c_parser_peek_token (parser)->location;
a1e3b3d9 4170 gcc_obstack_init (&braced_init_obstack);
27bf414c
JM
4171 gcc_assert (c_parser_next_token_is (parser, CPP_OPEN_BRACE));
4172 c_parser_consume_token (parser);
4173 if (nested_p)
ea58ef42 4174 push_init_level (brace_loc, 0, &braced_init_obstack);
27bf414c
JM
4175 else
4176 really_start_incremental_init (type);
4177 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
4178 {
c1771a20 4179 pedwarn (brace_loc, OPT_Wpedantic, "ISO C forbids empty initializer braces");
27bf414c
JM
4180 }
4181 else
4182 {
4183 /* Parse a non-empty initializer list, possibly with a trailing
4184 comma. */
4185 while (true)
4186 {
a1e3b3d9 4187 c_parser_initelt (parser, &braced_init_obstack);
27bf414c
JM
4188 if (parser->error)
4189 break;
4190 if (c_parser_next_token_is (parser, CPP_COMMA))
4191 c_parser_consume_token (parser);
4192 else
4193 break;
4194 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
4195 break;
4196 }
4197 }
4198 if (c_parser_next_token_is_not (parser, CPP_CLOSE_BRACE))
4199 {
27bf414c
JM
4200 ret.value = error_mark_node;
4201 ret.original_code = ERROR_MARK;
6866c6e8 4202 ret.original_type = NULL;
27bf414c 4203 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, "expected %<}%>");
ea58ef42 4204 pop_init_level (brace_loc, 0, &braced_init_obstack);
a1e3b3d9 4205 obstack_free (&braced_init_obstack, NULL);
27bf414c
JM
4206 return ret;
4207 }
4208 c_parser_consume_token (parser);
ea58ef42 4209 ret = pop_init_level (brace_loc, 0, &braced_init_obstack);
a1e3b3d9
LB
4210 obstack_free (&braced_init_obstack, NULL);
4211 return ret;
27bf414c
JM
4212}
4213
4214/* Parse a nested initializer, including designators. */
4215
4216static void
a1e3b3d9 4217c_parser_initelt (c_parser *parser, struct obstack * braced_init_obstack)
27bf414c
JM
4218{
4219 /* Parse any designator or designator list. A single array
4220 designator may have the subsequent "=" omitted in GNU C, but a
4221 longer list or a structure member designator may not. */
4222 if (c_parser_next_token_is (parser, CPP_NAME)
4223 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
4224 {
4225 /* Old-style structure member designator. */
ea58ef42
MP
4226 set_init_label (c_parser_peek_token (parser)->location,
4227 c_parser_peek_token (parser)->value,
a1e3b3d9 4228 braced_init_obstack);
fcf73884 4229 /* Use the colon as the error location. */
c1771a20 4230 pedwarn (c_parser_peek_2nd_token (parser)->location, OPT_Wpedantic,
509c9d60 4231 "obsolete use of designated initializer with %<:%>");
27bf414c
JM
4232 c_parser_consume_token (parser);
4233 c_parser_consume_token (parser);
4234 }
4235 else
4236 {
4237 /* des_seen is 0 if there have been no designators, 1 if there
4238 has been a single array designator and 2 otherwise. */
4239 int des_seen = 0;
c7412148 4240 /* Location of a designator. */
922f2908 4241 location_t des_loc = UNKNOWN_LOCATION; /* Quiet warning. */
27bf414c
JM
4242 while (c_parser_next_token_is (parser, CPP_OPEN_SQUARE)
4243 || c_parser_next_token_is (parser, CPP_DOT))
4244 {
4245 int des_prev = des_seen;
c7412148
TT
4246 if (!des_seen)
4247 des_loc = c_parser_peek_token (parser)->location;
27bf414c
JM
4248 if (des_seen < 2)
4249 des_seen++;
4250 if (c_parser_next_token_is (parser, CPP_DOT))
4251 {
4252 des_seen = 2;
4253 c_parser_consume_token (parser);
4254 if (c_parser_next_token_is (parser, CPP_NAME))
4255 {
ea58ef42 4256 set_init_label (des_loc, c_parser_peek_token (parser)->value,
a1e3b3d9 4257 braced_init_obstack);
27bf414c
JM
4258 c_parser_consume_token (parser);
4259 }
4260 else
4261 {
4262 struct c_expr init;
4263 init.value = error_mark_node;
4264 init.original_code = ERROR_MARK;
6866c6e8 4265 init.original_type = NULL;
27bf414c
JM
4266 c_parser_error (parser, "expected identifier");
4267 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
34cf811f
MP
4268 process_init_element (input_location, init, false,
4269 braced_init_obstack);
27bf414c
JM
4270 return;
4271 }
4272 }
4273 else
4274 {
4275 tree first, second;
922f2908 4276 location_t ellipsis_loc = UNKNOWN_LOCATION; /* Quiet warning. */
ea58ef42 4277 location_t array_index_loc = UNKNOWN_LOCATION;
27bf414c
JM
4278 /* ??? Following the old parser, [ objc-receiver
4279 objc-message-args ] is accepted as an initializer,
4280 being distinguished from a designator by what follows
4281 the first assignment expression inside the square
4282 brackets, but after a first array designator a
4283 subsequent square bracket is for Objective-C taken to
4284 start an expression, using the obsolete form of
4285 designated initializer without '=', rather than
4286 possibly being a second level of designation: in LALR
4287 terms, the '[' is shifted rather than reducing
4288 designator to designator-list. */
4289 if (des_prev == 1 && c_dialect_objc ())
4290 {
4291 des_seen = des_prev;
4292 break;
4293 }
4294 if (des_prev == 0 && c_dialect_objc ())
4295 {
4296 /* This might be an array designator or an
4297 Objective-C message expression. If the former,
4298 continue parsing here; if the latter, parse the
4299 remainder of the initializer given the starting
4300 primary-expression. ??? It might make sense to
4301 distinguish when des_prev == 1 as well; see
4302 previous comment. */
4303 tree rec, args;
4304 struct c_expr mexpr;
4305 c_parser_consume_token (parser);
4306 if (c_parser_peek_token (parser)->type == CPP_NAME
4307 && ((c_parser_peek_token (parser)->id_kind
4308 == C_ID_TYPENAME)
4309 || (c_parser_peek_token (parser)->id_kind
4310 == C_ID_CLASSNAME)))
4311 {
4312 /* Type name receiver. */
4313 tree id = c_parser_peek_token (parser)->value;
4314 c_parser_consume_token (parser);
4315 rec = objc_get_class_reference (id);
4316 goto parse_message_args;
4317 }
4318 first = c_parser_expr_no_commas (parser, NULL).value;
ebfbbdc5 4319 mark_exp_read (first);
27bf414c
JM
4320 if (c_parser_next_token_is (parser, CPP_ELLIPSIS)
4321 || c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
4322 goto array_desig_after_first;
4323 /* Expression receiver. So far only one part
4324 without commas has been parsed; there might be
4325 more of the expression. */
4326 rec = first;
4327 while (c_parser_next_token_is (parser, CPP_COMMA))
4328 {
f2a71bbc 4329 struct c_expr next;
c2255bc4
AH
4330 location_t comma_loc, exp_loc;
4331 comma_loc = c_parser_peek_token (parser)->location;
27bf414c 4332 c_parser_consume_token (parser);
c2255bc4 4333 exp_loc = c_parser_peek_token (parser)->location;
f2a71bbc 4334 next = c_parser_expr_no_commas (parser, NULL);
267bac10
JM
4335 next = convert_lvalue_to_rvalue (exp_loc, next,
4336 true, true);
c2255bc4 4337 rec = build_compound_expr (comma_loc, rec, next.value);
27bf414c
JM
4338 }
4339 parse_message_args:
4340 /* Now parse the objc-message-args. */
4341 args = c_parser_objc_message_args (parser);
4342 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
4343 "expected %<]%>");
4344 mexpr.value
eb345401 4345 = objc_build_message_expr (rec, args);
27bf414c 4346 mexpr.original_code = ERROR_MARK;
6866c6e8 4347 mexpr.original_type = NULL;
27bf414c
JM
4348 /* Now parse and process the remainder of the
4349 initializer, starting with this message
4350 expression as a primary-expression. */
a1e3b3d9 4351 c_parser_initval (parser, &mexpr, braced_init_obstack);
27bf414c
JM
4352 return;
4353 }
4354 c_parser_consume_token (parser);
ea58ef42 4355 array_index_loc = c_parser_peek_token (parser)->location;
27bf414c 4356 first = c_parser_expr_no_commas (parser, NULL).value;
ebfbbdc5 4357 mark_exp_read (first);
27bf414c
JM
4358 array_desig_after_first:
4359 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
4360 {
c7412148 4361 ellipsis_loc = c_parser_peek_token (parser)->location;
27bf414c
JM
4362 c_parser_consume_token (parser);
4363 second = c_parser_expr_no_commas (parser, NULL).value;
ebfbbdc5 4364 mark_exp_read (second);
27bf414c
JM
4365 }
4366 else
4367 second = NULL_TREE;
4368 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
4369 {
4370 c_parser_consume_token (parser);
ea58ef42
MP
4371 set_init_index (array_index_loc, first, second,
4372 braced_init_obstack);
fcf73884 4373 if (second)
c1771a20 4374 pedwarn (ellipsis_loc, OPT_Wpedantic,
509c9d60 4375 "ISO C forbids specifying range of elements to initialize");
27bf414c
JM
4376 }
4377 else
4378 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
4379 "expected %<]%>");
4380 }
4381 }
4382 if (des_seen >= 1)
4383 {
4384 if (c_parser_next_token_is (parser, CPP_EQ))
4385 {
f3bede71
MP
4386 pedwarn_c90 (des_loc, OPT_Wpedantic,
4387 "ISO C90 forbids specifying subobject "
4388 "to initialize");
27bf414c
JM
4389 c_parser_consume_token (parser);
4390 }
4391 else
4392 {
4393 if (des_seen == 1)
c1771a20 4394 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
509c9d60 4395 "obsolete use of designated initializer without %<=%>");
27bf414c
JM
4396 else
4397 {
4398 struct c_expr init;
4399 init.value = error_mark_node;
4400 init.original_code = ERROR_MARK;
6866c6e8 4401 init.original_type = NULL;
27bf414c
JM
4402 c_parser_error (parser, "expected %<=%>");
4403 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
34cf811f
MP
4404 process_init_element (input_location, init, false,
4405 braced_init_obstack);
27bf414c
JM
4406 return;
4407 }
4408 }
4409 }
4410 }
a1e3b3d9 4411 c_parser_initval (parser, NULL, braced_init_obstack);
27bf414c
JM
4412}
4413
4414/* Parse a nested initializer; as c_parser_initializer but parses
4415 initializers within braced lists, after any designators have been
4416 applied. If AFTER is not NULL then it is an Objective-C message
4417 expression which is the primary-expression starting the
4418 initializer. */
4419
4420static void
a1e3b3d9
LB
4421c_parser_initval (c_parser *parser, struct c_expr *after,
4422 struct obstack * braced_init_obstack)
27bf414c
JM
4423{
4424 struct c_expr init;
4425 gcc_assert (!after || c_dialect_objc ());
34cf811f
MP
4426 location_t loc = c_parser_peek_token (parser)->location;
4427
27bf414c
JM
4428 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE) && !after)
4429 init = c_parser_braced_init (parser, NULL_TREE, true);
4430 else
46bdb9cf
JM
4431 {
4432 init = c_parser_expr_no_commas (parser, after);
4433 if (init.value != NULL_TREE
4434 && TREE_CODE (init.value) != STRING_CST
4435 && TREE_CODE (init.value) != COMPOUND_LITERAL_EXPR)
267bac10 4436 init = convert_lvalue_to_rvalue (loc, init, true, true);
46bdb9cf 4437 }
34cf811f 4438 process_init_element (loc, init, false, braced_init_obstack);
27bf414c
JM
4439}
4440
4441/* Parse a compound statement (possibly a function body) (C90 6.6.2,
4442 C99 6.8.2).
4443
4444 compound-statement:
4445 { block-item-list[opt] }
4446 { label-declarations block-item-list }
4447
4448 block-item-list:
4449 block-item
4450 block-item-list block-item
4451
4452 block-item:
4453 nested-declaration
4454 statement
4455
4456 nested-declaration:
4457 declaration
4458
4459 GNU extensions:
4460
4461 compound-statement:
4462 { label-declarations block-item-list }
4463
4464 nested-declaration:
4465 __extension__ nested-declaration
4466 nested-function-definition
4467
4468 label-declarations:
4469 label-declaration
4470 label-declarations label-declaration
4471
4472 label-declaration:
4473 __label__ identifier-list ;
4474
4475 Allowing the mixing of declarations and code is new in C99. The
4476 GNU syntax also permits (not shown above) labels at the end of
4477 compound statements, which yield an error. We don't allow labels
4478 on declarations; this might seem like a natural extension, but
4479 there would be a conflict between attributes on the label and
4480 prefix attributes on the declaration. ??? The syntax follows the
4481 old parser in requiring something after label declarations.
4482 Although they are erroneous if the labels declared aren't defined,
953ff289 4483 is it useful for the syntax to be this way?
b8698a0f 4484
953ff289 4485 OpenMP:
b8698a0f 4486
953ff289
DN
4487 block-item:
4488 openmp-directive
4489
4490 openmp-directive:
4491 barrier-directive
acf0174b
JJ
4492 flush-directive
4493 taskwait-directive
4494 taskyield-directive
4495 cancel-directive
4496 cancellation-point-directive */
27bf414c
JM
4497
4498static tree
4499c_parser_compound_statement (c_parser *parser)
4500{
4501 tree stmt;
c2255bc4
AH
4502 location_t brace_loc;
4503 brace_loc = c_parser_peek_token (parser)->location;
27bf414c 4504 if (!c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
5600f233
JM
4505 {
4506 /* Ensure a scope is entered and left anyway to avoid confusion
4507 if we have just prepared to enter a function body. */
4508 stmt = c_begin_compound_stmt (true);
c2255bc4 4509 c_end_compound_stmt (brace_loc, stmt, true);
5600f233
JM
4510 return error_mark_node;
4511 }
27bf414c
JM
4512 stmt = c_begin_compound_stmt (true);
4513 c_parser_compound_statement_nostart (parser);
36536d79
BI
4514
4515 /* If the compound stmt contains array notations, then we expand them. */
b72271b9 4516 if (flag_cilkplus && contains_array_notation_expr (stmt))
36536d79 4517 stmt = expand_array_notation_exprs (stmt);
c2255bc4 4518 return c_end_compound_stmt (brace_loc, stmt, true);
27bf414c
JM
4519}
4520
4521/* Parse a compound statement except for the opening brace. This is
4522 used for parsing both compound statements and statement expressions
4523 (which follow different paths to handling the opening). */
4524
4525static void
4526c_parser_compound_statement_nostart (c_parser *parser)
4527{
4528 bool last_stmt = false;
4529 bool last_label = false;
6ec637a4 4530 bool save_valid_for_pragma = valid_location_for_stdc_pragma_p ();
3ba09659 4531 location_t label_loc = UNKNOWN_LOCATION; /* Quiet warning. */
27bf414c
JM
4532 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
4533 {
4534 c_parser_consume_token (parser);
4535 return;
4536 }
6ec637a4 4537 mark_valid_location_for_stdc_pragma (true);
27bf414c
JM
4538 if (c_parser_next_token_is_keyword (parser, RID_LABEL))
4539 {
4540 /* Read zero or more forward-declarations for labels that nested
4541 functions can jump to. */
6ec637a4 4542 mark_valid_location_for_stdc_pragma (false);
27bf414c
JM
4543 while (c_parser_next_token_is_keyword (parser, RID_LABEL))
4544 {
c2255bc4 4545 label_loc = c_parser_peek_token (parser)->location;
27bf414c
JM
4546 c_parser_consume_token (parser);
4547 /* Any identifiers, including those declared as type names,
4548 are OK here. */
4549 while (true)
4550 {
4551 tree label;
4552 if (c_parser_next_token_is_not (parser, CPP_NAME))
4553 {
4554 c_parser_error (parser, "expected identifier");
4555 break;
4556 }
4557 label
4558 = declare_label (c_parser_peek_token (parser)->value);
4559 C_DECLARED_LABEL_FLAG (label) = 1;
c2255bc4 4560 add_stmt (build_stmt (label_loc, DECL_EXPR, label));
27bf414c
JM
4561 c_parser_consume_token (parser);
4562 if (c_parser_next_token_is (parser, CPP_COMMA))
4563 c_parser_consume_token (parser);
4564 else
4565 break;
4566 }
4567 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
4568 }
c1771a20 4569 pedwarn (label_loc, OPT_Wpedantic, "ISO C forbids label declarations");
27bf414c
JM
4570 }
4571 /* We must now have at least one statement, label or declaration. */
4572 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
4573 {
6ec637a4 4574 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
27bf414c
JM
4575 c_parser_error (parser, "expected declaration or statement");
4576 c_parser_consume_token (parser);
4577 return;
4578 }
4579 while (c_parser_next_token_is_not (parser, CPP_CLOSE_BRACE))
4580 {
4581 location_t loc = c_parser_peek_token (parser)->location;
27bf414c
JM
4582 if (c_parser_next_token_is_keyword (parser, RID_CASE)
4583 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
4584 || (c_parser_next_token_is (parser, CPP_NAME)
4585 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
4586 {
c7412148
TT
4587 if (c_parser_next_token_is_keyword (parser, RID_CASE))
4588 label_loc = c_parser_peek_2nd_token (parser)->location;
4589 else
4590 label_loc = c_parser_peek_token (parser)->location;
27bf414c
JM
4591 last_label = true;
4592 last_stmt = false;
6ec637a4 4593 mark_valid_location_for_stdc_pragma (false);
27bf414c
JM
4594 c_parser_label (parser);
4595 }
4596 else if (!last_label
2f413185 4597 && c_parser_next_tokens_start_declaration (parser))
27bf414c
JM
4598 {
4599 last_label = false;
6ec637a4 4600 mark_valid_location_for_stdc_pragma (false);
acf0174b
JJ
4601 c_parser_declaration_or_fndef (parser, true, true, true, true,
4602 true, NULL, vNULL);
fcf73884 4603 if (last_stmt)
177cce46 4604 pedwarn_c90 (loc, OPT_Wdeclaration_after_statement,
509c9d60 4605 "ISO C90 forbids mixed declarations and code");
27bf414c
JM
4606 last_stmt = false;
4607 }
4608 else if (!last_label
4609 && c_parser_next_token_is_keyword (parser, RID_EXTENSION))
4610 {
4611 /* __extension__ can start a declaration, but is also an
4612 unary operator that can start an expression. Consume all
4613 but the last of a possible series of __extension__ to
4614 determine which. */
4615 while (c_parser_peek_2nd_token (parser)->type == CPP_KEYWORD
4616 && (c_parser_peek_2nd_token (parser)->keyword
4617 == RID_EXTENSION))
4618 c_parser_consume_token (parser);
32912286 4619 if (c_token_starts_declaration (c_parser_peek_2nd_token (parser)))
27bf414c
JM
4620 {
4621 int ext;
4622 ext = disable_extension_diagnostics ();
4623 c_parser_consume_token (parser);
4624 last_label = false;
6ec637a4 4625 mark_valid_location_for_stdc_pragma (false);
32912286 4626 c_parser_declaration_or_fndef (parser, true, true, true, true,
acf0174b 4627 true, NULL, vNULL);
27bf414c
JM
4628 /* Following the old parser, __extension__ does not
4629 disable this diagnostic. */
4630 restore_extension_diagnostics (ext);
fcf73884 4631 if (last_stmt)
177cce46 4632 pedwarn_c90 (loc, OPT_Wdeclaration_after_statement,
509c9d60 4633 "ISO C90 forbids mixed declarations and code");
27bf414c
JM
4634 last_stmt = false;
4635 }
4636 else
4637 goto statement;
4638 }
bc4071dd
RH
4639 else if (c_parser_next_token_is (parser, CPP_PRAGMA))
4640 {
4641 /* External pragmas, and some omp pragmas, are not associated
4642 with regular c code, and so are not to be considered statements
4643 syntactically. This ensures that the user doesn't put them
4644 places that would turn into syntax errors if the directive
4645 were ignored. */
4646 if (c_parser_pragma (parser, pragma_compound))
4647 last_label = false, last_stmt = true;
4648 }
4649 else if (c_parser_next_token_is (parser, CPP_EOF))
4650 {
6ec637a4 4651 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
bc4071dd
RH
4652 c_parser_error (parser, "expected declaration or statement");
4653 return;
4654 }
b4b56033
MLI
4655 else if (c_parser_next_token_is_keyword (parser, RID_ELSE))
4656 {
b8698a0f 4657 if (parser->in_if_block)
b4b56033 4658 {
6ec637a4 4659 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
3ba09659 4660 error_at (loc, """expected %<}%> before %<else%>");
b4b56033
MLI
4661 return;
4662 }
b8698a0f 4663 else
b4b56033 4664 {
3ba09659 4665 error_at (loc, "%<else%> without a previous %<if%>");
b4b56033
MLI
4666 c_parser_consume_token (parser);
4667 continue;
4668 }
4669 }
27bf414c
JM
4670 else
4671 {
4672 statement:
4673 last_label = false;
4674 last_stmt = true;
6ec637a4 4675 mark_valid_location_for_stdc_pragma (false);
27bf414c
JM
4676 c_parser_statement_after_labels (parser);
4677 }
2c14ae9a
VR
4678
4679 parser->error = false;
27bf414c
JM
4680 }
4681 if (last_label)
3ba09659 4682 error_at (label_loc, "label at end of compound statement");
27bf414c 4683 c_parser_consume_token (parser);
6ec637a4
JJ
4684 /* Restore the value we started with. */
4685 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
27bf414c
JM
4686}
4687
74d98c1e
AB
4688/* Parse all consecutive labels. */
4689
4690static void
4691c_parser_all_labels (c_parser *parser)
4692{
4693 while (c_parser_next_token_is_keyword (parser, RID_CASE)
4694 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
4695 || (c_parser_next_token_is (parser, CPP_NAME)
4696 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
4697 c_parser_label (parser);
4698}
4699
27bf414c
JM
4700/* Parse a label (C90 6.6.1, C99 6.8.1).
4701
4702 label:
4703 identifier : attributes[opt]
4704 case constant-expression :
4705 default :
4706
4707 GNU extensions:
4708
4709 label:
4710 case constant-expression ... constant-expression :
4711
4712 The use of attributes on labels is a GNU extension. The syntax in
4713 GNU C accepts any expressions without commas, non-constant
4714 expressions being rejected later. */
4715
4716static void
4717c_parser_label (c_parser *parser)
4718{
4719 location_t loc1 = c_parser_peek_token (parser)->location;
4720 tree label = NULL_TREE;
4721 if (c_parser_next_token_is_keyword (parser, RID_CASE))
4722 {
4723 tree exp1, exp2;
4724 c_parser_consume_token (parser);
4725 exp1 = c_parser_expr_no_commas (parser, NULL).value;
4726 if (c_parser_next_token_is (parser, CPP_COLON))
4727 {
4728 c_parser_consume_token (parser);
c2255bc4 4729 label = do_case (loc1, exp1, NULL_TREE);
27bf414c
JM
4730 }
4731 else if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
4732 {
4733 c_parser_consume_token (parser);
4734 exp2 = c_parser_expr_no_commas (parser, NULL).value;
4735 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
c2255bc4 4736 label = do_case (loc1, exp1, exp2);
27bf414c
JM
4737 }
4738 else
4739 c_parser_error (parser, "expected %<:%> or %<...%>");
4740 }
4741 else if (c_parser_next_token_is_keyword (parser, RID_DEFAULT))
4742 {
4743 c_parser_consume_token (parser);
4744 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
c2255bc4 4745 label = do_case (loc1, NULL_TREE, NULL_TREE);
27bf414c
JM
4746 }
4747 else
4748 {
4749 tree name = c_parser_peek_token (parser)->value;
4750 tree tlab;
27bf414c 4751 tree attrs;
c7412148 4752 location_t loc2 = c_parser_peek_token (parser)->location;
27bf414c
JM
4753 gcc_assert (c_parser_next_token_is (parser, CPP_NAME));
4754 c_parser_consume_token (parser);
4755 gcc_assert (c_parser_next_token_is (parser, CPP_COLON));
27bf414c
JM
4756 c_parser_consume_token (parser);
4757 attrs = c_parser_attributes (parser);
4758 tlab = define_label (loc2, name);
4759 if (tlab)
4760 {
4761 decl_attributes (&tlab, attrs, 0);
c2255bc4 4762 label = add_stmt (build_stmt (loc1, LABEL_EXPR, tlab));
27bf414c
JM
4763 }
4764 }
4765 if (label)
3d57f0f0 4766 {
2f413185 4767 if (c_parser_next_tokens_start_declaration (parser))
3d57f0f0 4768 {
3ba09659
AH
4769 error_at (c_parser_peek_token (parser)->location,
4770 "a label can only be part of a statement and "
4771 "a declaration is not a statement");
b8698a0f 4772 c_parser_declaration_or_fndef (parser, /*fndef_ok*/ false,
32912286 4773 /*static_assert_ok*/ true,
6265d07c 4774 /*empty_ok*/ true, /*nested*/ true,
acf0174b
JJ
4775 /*start_attr_ok*/ true, NULL,
4776 vNULL);
3d57f0f0
MLI
4777 }
4778 }
27bf414c
JM
4779}
4780
4781/* Parse a statement (C90 6.6, C99 6.8).
4782
4783 statement:
4784 labeled-statement
4785 compound-statement
4786 expression-statement
4787 selection-statement
4788 iteration-statement
4789 jump-statement
4790
4791 labeled-statement:
4792 label statement
4793
4794 expression-statement:
4795 expression[opt] ;
4796
4797 selection-statement:
4798 if-statement
4799 switch-statement
4800
4801 iteration-statement:
4802 while-statement
4803 do-statement
4804 for-statement
4805
4806 jump-statement:
4807 goto identifier ;
4808 continue ;
4809 break ;
4810 return expression[opt] ;
4811
4812 GNU extensions:
4813
4814 statement:
4815 asm-statement
4816
4817 jump-statement:
4818 goto * expression ;
4819
4820 Objective-C:
4821
4822 statement:
4823 objc-throw-statement
4824 objc-try-catch-statement
4825 objc-synchronized-statement
4826
4827 objc-throw-statement:
4828 @throw expression ;
4829 @throw ;
953ff289
DN
4830
4831 OpenMP:
4832
4833 statement:
4834 openmp-construct
4835
4836 openmp-construct:
4837 parallel-construct
4838 for-construct
acf0174b
JJ
4839 simd-construct
4840 for-simd-construct
953ff289
DN
4841 sections-construct
4842 single-construct
4843 parallel-for-construct
acf0174b 4844 parallel-for-simd-construct
953ff289
DN
4845 parallel-sections-construct
4846 master-construct
4847 critical-construct
4848 atomic-construct
4849 ordered-construct
4850
4851 parallel-construct:
4852 parallel-directive structured-block
4853
4854 for-construct:
4855 for-directive iteration-statement
4856
acf0174b
JJ
4857 simd-construct:
4858 simd-directive iteration-statements
4859
4860 for-simd-construct:
4861 for-simd-directive iteration-statements
4862
953ff289
DN
4863 sections-construct:
4864 sections-directive section-scope
4865
4866 single-construct:
4867 single-directive structured-block
4868
4869 parallel-for-construct:
4870 parallel-for-directive iteration-statement
4871
acf0174b
JJ
4872 parallel-for-simd-construct:
4873 parallel-for-simd-directive iteration-statement
4874
953ff289
DN
4875 parallel-sections-construct:
4876 parallel-sections-directive section-scope
4877
4878 master-construct:
4879 master-directive structured-block
4880
4881 critical-construct:
4882 critical-directive structured-block
4883
4884 atomic-construct:
4885 atomic-directive expression-statement
4886
4887 ordered-construct:
0a35513e
AH
4888 ordered-directive structured-block
4889
4890 Transactional Memory:
4891
4892 statement:
4893 transaction-statement
4894 transaction-cancel-statement
4895*/
27bf414c
JM
4896
4897static void
4898c_parser_statement (c_parser *parser)
4899{
74d98c1e 4900 c_parser_all_labels (parser);
27bf414c
JM
4901 c_parser_statement_after_labels (parser);
4902}
4903
4904/* Parse a statement, other than a labeled statement. */
4905
4906static void
4907c_parser_statement_after_labels (c_parser *parser)
4908{
4909 location_t loc = c_parser_peek_token (parser)->location;
4910 tree stmt = NULL_TREE;
b4b56033
MLI
4911 bool in_if_block = parser->in_if_block;
4912 parser->in_if_block = false;
27bf414c
JM
4913 switch (c_parser_peek_token (parser)->type)
4914 {
4915 case CPP_OPEN_BRACE:
4916 add_stmt (c_parser_compound_statement (parser));
4917 break;
4918 case CPP_KEYWORD:
4919 switch (c_parser_peek_token (parser)->keyword)
4920 {
4921 case RID_IF:
4922 c_parser_if_statement (parser);
4923 break;
4924 case RID_SWITCH:
4925 c_parser_switch_statement (parser);
4926 break;
4927 case RID_WHILE:
d4af74d4 4928 c_parser_while_statement (parser, false);
27bf414c
JM
4929 break;
4930 case RID_DO:
d4af74d4 4931 c_parser_do_statement (parser, false);
27bf414c
JM
4932 break;
4933 case RID_FOR:
8170608b 4934 c_parser_for_statement (parser, false);
27bf414c 4935 break;
9a771876
JJ
4936 case RID_CILK_FOR:
4937 if (!flag_cilkplus)
4938 {
4939 error_at (c_parser_peek_token (parser)->location,
4940 "-fcilkplus must be enabled to use %<_Cilk_for%>");
4941 c_parser_skip_to_end_of_block_or_statement (parser);
4942 }
4943 else
4944 c_parser_cilk_for (parser, integer_zero_node);
4945 break;
939b37da
BI
4946 case RID_CILK_SYNC:
4947 c_parser_consume_token (parser);
4948 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
b72271b9 4949 if (!flag_cilkplus)
939b37da
BI
4950 error_at (loc, "-fcilkplus must be enabled to use %<_Cilk_sync%>");
4951 else
4952 add_stmt (build_cilk_sync ());
4953 break;
27bf414c
JM
4954 case RID_GOTO:
4955 c_parser_consume_token (parser);
4956 if (c_parser_next_token_is (parser, CPP_NAME))
4957 {
c2255bc4
AH
4958 stmt = c_finish_goto_label (loc,
4959 c_parser_peek_token (parser)->value);
27bf414c
JM
4960 c_parser_consume_token (parser);
4961 }
4962 else if (c_parser_next_token_is (parser, CPP_MULT))
4963 {
267bac10 4964 struct c_expr val;
84628aa8 4965
27bf414c 4966 c_parser_consume_token (parser);
267bac10 4967 val = c_parser_expression (parser);
e5e44252
AK
4968 if (check_no_cilk (val.value,
4969 "Cilk array notation cannot be used as a computed goto expression",
4970 "%<_Cilk_spawn%> statement cannot be used as a computed goto expression",
4971 loc))
4972 val.value = error_mark_node;
267bac10
JM
4973 val = convert_lvalue_to_rvalue (loc, val, false, true);
4974 stmt = c_finish_goto_ptr (loc, val.value);
27bf414c
JM
4975 }
4976 else
4977 c_parser_error (parser, "expected identifier or %<*%>");
4978 goto expect_semicolon;
4979 case RID_CONTINUE:
4980 c_parser_consume_token (parser);
c2255bc4 4981 stmt = c_finish_bc_stmt (loc, &c_cont_label, false);
27bf414c
JM
4982 goto expect_semicolon;
4983 case RID_BREAK:
4984 c_parser_consume_token (parser);
c2255bc4 4985 stmt = c_finish_bc_stmt (loc, &c_break_label, true);
27bf414c
JM
4986 goto expect_semicolon;
4987 case RID_RETURN:
4988 c_parser_consume_token (parser);
4989 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
4990 {
c2255bc4 4991 stmt = c_finish_return (loc, NULL_TREE, NULL_TREE);
27bf414c
JM
4992 c_parser_consume_token (parser);
4993 }
4994 else
4995 {
6e07c515 4996 location_t xloc = c_parser_peek_token (parser)->location;
bbbbb16a 4997 struct c_expr expr = c_parser_expression_conv (parser);
ebfbbdc5 4998 mark_exp_read (expr.value);
6e07c515 4999 stmt = c_finish_return (xloc, expr.value, expr.original_type);
27bf414c
JM
5000 goto expect_semicolon;
5001 }
5002 break;
5003 case RID_ASM:
5004 stmt = c_parser_asm_statement (parser);
5005 break;
0a35513e
AH
5006 case RID_TRANSACTION_ATOMIC:
5007 case RID_TRANSACTION_RELAXED:
5008 stmt = c_parser_transaction (parser,
5009 c_parser_peek_token (parser)->keyword);
5010 break;
5011 case RID_TRANSACTION_CANCEL:
5012 stmt = c_parser_transaction_cancel (parser);
5013 goto expect_semicolon;
49b91f05 5014 case RID_AT_THROW:
27bf414c
JM
5015 gcc_assert (c_dialect_objc ());
5016 c_parser_consume_token (parser);
5017 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5018 {
c2255bc4 5019 stmt = objc_build_throw_stmt (loc, NULL_TREE);
27bf414c
JM
5020 c_parser_consume_token (parser);
5021 }
5022 else
5023 {
267bac10
JM
5024 struct c_expr expr = c_parser_expression (parser);
5025 expr = convert_lvalue_to_rvalue (loc, expr, false, false);
e5e44252
AK
5026 if (check_no_cilk (expr.value,
5027 "Cilk array notation cannot be used for a throw expression",
5028 "%<_Cilk_spawn%> statement cannot be used for a throw expression"))
5029 expr.value = error_mark_node;
5030 else
5031 {
5032 expr.value = c_fully_fold (expr.value, false, NULL);
5033 stmt = objc_build_throw_stmt (loc, expr.value);
5034 }
27bf414c
JM
5035 goto expect_semicolon;
5036 }
5037 break;
49b91f05 5038 case RID_AT_TRY:
27bf414c 5039 gcc_assert (c_dialect_objc ());
437c2322 5040 c_parser_objc_try_catch_finally_statement (parser);
27bf414c
JM
5041 break;
5042 case RID_AT_SYNCHRONIZED:
5043 gcc_assert (c_dialect_objc ());
5044 c_parser_objc_synchronized_statement (parser);
5045 break;
5046 default:
5047 goto expr_stmt;
5048 }
5049 break;
5050 case CPP_SEMICOLON:
5051 c_parser_consume_token (parser);
5052 break;
5053 case CPP_CLOSE_PAREN:
5054 case CPP_CLOSE_SQUARE:
5055 /* Avoid infinite loop in error recovery:
5056 c_parser_skip_until_found stops at a closing nesting
5057 delimiter without consuming it, but here we need to consume
5058 it to proceed further. */
5059 c_parser_error (parser, "expected statement");
5060 c_parser_consume_token (parser);
5061 break;
bc4071dd
RH
5062 case CPP_PRAGMA:
5063 c_parser_pragma (parser, pragma_stmt);
5064 break;
27bf414c
JM
5065 default:
5066 expr_stmt:
c2255bc4 5067 stmt = c_finish_expr_stmt (loc, c_parser_expression_conv (parser).value);
27bf414c
JM
5068 expect_semicolon:
5069 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
5070 break;
5071 }
5072 /* Two cases cannot and do not have line numbers associated: If stmt
5073 is degenerate, such as "2;", then stmt is an INTEGER_CST, which
5074 cannot hold line numbers. But that's OK because the statement
5075 will either be changed to a MODIFY_EXPR during gimplification of
5076 the statement expr, or discarded. If stmt was compound, but
5077 without new variables, we will have skipped the creation of a
5078 BIND and will have a bare STATEMENT_LIST. But that's OK because
5079 (recursively) all of the component statements should already have
5080 line numbers assigned. ??? Can we discard no-op statements
5081 earlier? */
c2255bc4
AH
5082 if (CAN_HAVE_LOCATION_P (stmt)
5083 && EXPR_LOCATION (stmt) == UNKNOWN_LOCATION)
5084 SET_EXPR_LOCATION (stmt, loc);
b4b56033
MLI
5085
5086 parser->in_if_block = in_if_block;
27bf414c
JM
5087}
5088
ca085fd7
MLI
5089/* Parse the condition from an if, do, while or for statements. */
5090
5091static tree
5092c_parser_condition (c_parser *parser)
5093{
c2255bc4 5094 location_t loc = c_parser_peek_token (parser)->location;
ca085fd7 5095 tree cond;
928c19bb
JM
5096 cond = c_parser_expression_conv (parser).value;
5097 cond = c_objc_common_truthvalue_conversion (loc, cond);
5098 cond = c_fully_fold (cond, false, NULL);
ca085fd7
MLI
5099 if (warn_sequence_point)
5100 verify_sequence_points (cond);
5101 return cond;
5102}
5103
27bf414c
JM
5104/* Parse a parenthesized condition from an if, do or while statement.
5105
5106 condition:
5107 ( expression )
5108*/
5109static tree
5110c_parser_paren_condition (c_parser *parser)
5111{
27bf414c
JM
5112 tree cond;
5113 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5114 return error_mark_node;
ca085fd7 5115 cond = c_parser_condition (parser);
27bf414c
JM
5116 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
5117 return cond;
5118}
5119
5120/* Parse a statement which is a block in C99. */
5121
5122static tree
5123c_parser_c99_block_statement (c_parser *parser)
5124{
5125 tree block = c_begin_compound_stmt (flag_isoc99);
c2255bc4 5126 location_t loc = c_parser_peek_token (parser)->location;
27bf414c 5127 c_parser_statement (parser);
c2255bc4 5128 return c_end_compound_stmt (loc, block, flag_isoc99);
27bf414c
JM
5129}
5130
b4b56033
MLI
5131/* Parse the body of an if statement. This is just parsing a
5132 statement but (a) it is a block in C99, (b) we track whether the
5133 body is an if statement for the sake of -Wparentheses warnings, (c)
5134 we handle an empty body specially for the sake of -Wempty-body
5135 warnings, and (d) we call parser_compound_statement directly
5136 because c_parser_statement_after_labels resets
5137 parser->in_if_block. */
27bf414c
JM
5138
5139static tree
5140c_parser_if_body (c_parser *parser, bool *if_p)
5141{
5142 tree block = c_begin_compound_stmt (flag_isoc99);
c2255bc4 5143 location_t body_loc = c_parser_peek_token (parser)->location;
74d98c1e 5144 c_parser_all_labels (parser);
27bf414c 5145 *if_p = c_parser_next_token_is_keyword (parser, RID_IF);
62e00e94 5146 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
b4b56033 5147 {
626c34b5 5148 location_t loc = c_parser_peek_token (parser)->location;
c2255bc4 5149 add_stmt (build_empty_stmt (loc));
b4b56033 5150 c_parser_consume_token (parser);
626c34b5
PB
5151 if (!c_parser_next_token_is_keyword (parser, RID_ELSE))
5152 warning_at (loc, OPT_Wempty_body,
5153 "suggest braces around empty body in an %<if%> statement");
b4b56033
MLI
5154 }
5155 else if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
5156 add_stmt (c_parser_compound_statement (parser));
5157 else
5158 c_parser_statement_after_labels (parser);
c2255bc4 5159 return c_end_compound_stmt (body_loc, block, flag_isoc99);
b4b56033
MLI
5160}
5161
5162/* Parse the else body of an if statement. This is just parsing a
5163 statement but (a) it is a block in C99, (b) we handle an empty body
5164 specially for the sake of -Wempty-body warnings. */
5165
5166static tree
5167c_parser_else_body (c_parser *parser)
5168{
c2255bc4 5169 location_t else_loc = c_parser_peek_token (parser)->location;
b4b56033 5170 tree block = c_begin_compound_stmt (flag_isoc99);
74d98c1e 5171 c_parser_all_labels (parser);
b4b56033
MLI
5172 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5173 {
c2255bc4
AH
5174 location_t loc = c_parser_peek_token (parser)->location;
5175 warning_at (loc,
626c34b5
PB
5176 OPT_Wempty_body,
5177 "suggest braces around empty body in an %<else%> statement");
c2255bc4 5178 add_stmt (build_empty_stmt (loc));
b4b56033
MLI
5179 c_parser_consume_token (parser);
5180 }
b8698a0f 5181 else
b4b56033 5182 c_parser_statement_after_labels (parser);
c2255bc4 5183 return c_end_compound_stmt (else_loc, block, flag_isoc99);
27bf414c
JM
5184}
5185
5186/* Parse an if statement (C90 6.6.4, C99 6.8.4).
5187
5188 if-statement:
5189 if ( expression ) statement
5190 if ( expression ) statement else statement
5191*/
5192
5193static void
5194c_parser_if_statement (c_parser *parser)
5195{
5196 tree block;
5197 location_t loc;
5198 tree cond;
b4b56033 5199 bool first_if = false;
27bf414c 5200 tree first_body, second_body;
b4b56033 5201 bool in_if_block;
36536d79 5202 tree if_stmt;
b4b56033 5203
27bf414c
JM
5204 gcc_assert (c_parser_next_token_is_keyword (parser, RID_IF));
5205 c_parser_consume_token (parser);
5206 block = c_begin_compound_stmt (flag_isoc99);
5207 loc = c_parser_peek_token (parser)->location;
5208 cond = c_parser_paren_condition (parser);
e5e44252
AK
5209 if (flag_cilkplus && contains_cilk_spawn_stmt (cond))
5210 {
5211 error_at (loc, "if statement cannot contain %<Cilk_spawn%>");
5212 cond = error_mark_node;
5213 }
b4b56033
MLI
5214 in_if_block = parser->in_if_block;
5215 parser->in_if_block = true;
27bf414c 5216 first_body = c_parser_if_body (parser, &first_if);
b4b56033 5217 parser->in_if_block = in_if_block;
27bf414c
JM
5218 if (c_parser_next_token_is_keyword (parser, RID_ELSE))
5219 {
5220 c_parser_consume_token (parser);
b4b56033 5221 second_body = c_parser_else_body (parser);
27bf414c
JM
5222 }
5223 else
5224 second_body = NULL_TREE;
5225 c_finish_if_stmt (loc, cond, first_body, second_body, first_if);
36536d79
BI
5226 if_stmt = c_end_compound_stmt (loc, block, flag_isoc99);
5227
5228 /* If the if statement contains array notations, then we expand them. */
b72271b9 5229 if (flag_cilkplus && contains_array_notation_expr (if_stmt))
36536d79
BI
5230 if_stmt = fix_conditional_array_notations (if_stmt);
5231 add_stmt (if_stmt);
27bf414c
JM
5232}
5233
5234/* Parse a switch statement (C90 6.6.4, C99 6.8.4).
5235
5236 switch-statement:
5237 switch (expression) statement
5238*/
5239
5240static void
5241c_parser_switch_statement (c_parser *parser)
5242{
267bac10 5243 struct c_expr ce;
27bf414c 5244 tree block, expr, body, save_break;
c2255bc4
AH
5245 location_t switch_loc = c_parser_peek_token (parser)->location;
5246 location_t switch_cond_loc;
27bf414c
JM
5247 gcc_assert (c_parser_next_token_is_keyword (parser, RID_SWITCH));
5248 c_parser_consume_token (parser);
5249 block = c_begin_compound_stmt (flag_isoc99);
fedfecef 5250 bool explicit_cast_p = false;
27bf414c
JM
5251 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5252 {
c2255bc4 5253 switch_cond_loc = c_parser_peek_token (parser)->location;
fedfecef
MP
5254 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
5255 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
5256 explicit_cast_p = true;
267bac10
JM
5257 ce = c_parser_expression (parser);
5258 ce = convert_lvalue_to_rvalue (switch_cond_loc, ce, true, false);
5259 expr = ce.value;
e5e44252
AK
5260 /* ??? expr has no valid location? */
5261 if (check_no_cilk (expr,
5262 "Cilk array notation cannot be used as a condition for switch statement",
5263 "%<_Cilk_spawn%> statement cannot be used as a condition for switch statement",
5264 switch_cond_loc))
5265 expr = error_mark_node;
27bf414c
JM
5266 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
5267 }
5268 else
c2255bc4
AH
5269 {
5270 switch_cond_loc = UNKNOWN_LOCATION;
5271 expr = error_mark_node;
5272 }
fedfecef 5273 c_start_case (switch_loc, switch_cond_loc, expr, explicit_cast_p);
27bf414c
JM
5274 save_break = c_break_label;
5275 c_break_label = NULL_TREE;
5276 body = c_parser_c99_block_statement (parser);
083e891e 5277 c_finish_case (body, ce.original_type);
27bf414c 5278 if (c_break_label)
c2255bc4
AH
5279 {
5280 location_t here = c_parser_peek_token (parser)->location;
5281 tree t = build1 (LABEL_EXPR, void_type_node, c_break_label);
5282 SET_EXPR_LOCATION (t, here);
5283 add_stmt (t);
5284 }
27bf414c 5285 c_break_label = save_break;
c2255bc4 5286 add_stmt (c_end_compound_stmt (switch_loc, block, flag_isoc99));
27bf414c
JM
5287}
5288
5289/* Parse a while statement (C90 6.6.5, C99 6.8.5).
5290
5291 while-statement:
5292 while (expression) statement
5293*/
5294
5295static void
d4af74d4 5296c_parser_while_statement (c_parser *parser, bool ivdep)
27bf414c
JM
5297{
5298 tree block, cond, body, save_break, save_cont;
5299 location_t loc;
5300 gcc_assert (c_parser_next_token_is_keyword (parser, RID_WHILE));
5301 c_parser_consume_token (parser);
5302 block = c_begin_compound_stmt (flag_isoc99);
5303 loc = c_parser_peek_token (parser)->location;
5304 cond = c_parser_paren_condition (parser);
e5e44252
AK
5305 if (check_no_cilk (cond,
5306 "Cilk array notation cannot be used as a condition for while statement",
5307 "%<_Cilk_spawn%> statement cannot be used as a condition for while statement"))
5308 cond = error_mark_node;
d4af74d4
TB
5309 if (ivdep && cond != error_mark_node)
5310 cond = build2 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
5311 build_int_cst (integer_type_node,
5312 annot_expr_ivdep_kind));
27bf414c
JM
5313 save_break = c_break_label;
5314 c_break_label = NULL_TREE;
5315 save_cont = c_cont_label;
5316 c_cont_label = NULL_TREE;
5317 body = c_parser_c99_block_statement (parser);
5318 c_finish_loop (loc, cond, NULL, body, c_break_label, c_cont_label, true);
c2255bc4 5319 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99));
27bf414c
JM
5320 c_break_label = save_break;
5321 c_cont_label = save_cont;
5322}
5323
5324/* Parse a do statement (C90 6.6.5, C99 6.8.5).
5325
5326 do-statement:
5327 do statement while ( expression ) ;
5328*/
5329
5330static void
d4af74d4 5331c_parser_do_statement (c_parser *parser, bool ivdep)
27bf414c
JM
5332{
5333 tree block, cond, body, save_break, save_cont, new_break, new_cont;
5334 location_t loc;
5335 gcc_assert (c_parser_next_token_is_keyword (parser, RID_DO));
5336 c_parser_consume_token (parser);
62e00e94 5337 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
3ba09659
AH
5338 warning_at (c_parser_peek_token (parser)->location,
5339 OPT_Wempty_body,
5340 "suggest braces around empty body in %<do%> statement");
27bf414c
JM
5341 block = c_begin_compound_stmt (flag_isoc99);
5342 loc = c_parser_peek_token (parser)->location;
5343 save_break = c_break_label;
5344 c_break_label = NULL_TREE;
5345 save_cont = c_cont_label;
5346 c_cont_label = NULL_TREE;
5347 body = c_parser_c99_block_statement (parser);
5348 c_parser_require_keyword (parser, RID_WHILE, "expected %<while%>");
5349 new_break = c_break_label;
5350 c_break_label = save_break;
5351 new_cont = c_cont_label;
5352 c_cont_label = save_cont;
5353 cond = c_parser_paren_condition (parser);
e5e44252
AK
5354 if (check_no_cilk (cond,
5355 "Cilk array notation cannot be used as a condition for a do-while statement",
5356 "%<_Cilk_spawn%> statement cannot be used as a condition for a do-while statement"))
5357 cond = error_mark_node;
d4af74d4
TB
5358 if (ivdep && cond != error_mark_node)
5359 cond = build2 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
5360 build_int_cst (integer_type_node,
5361 annot_expr_ivdep_kind));
27bf414c
JM
5362 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
5363 c_parser_skip_to_end_of_block_or_statement (parser);
5364 c_finish_loop (loc, cond, NULL, body, new_break, new_cont, false);
c2255bc4 5365 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99));
27bf414c
JM
5366}
5367
5368/* Parse a for statement (C90 6.6.5, C99 6.8.5).
5369
5370 for-statement:
5371 for ( expression[opt] ; expression[opt] ; expression[opt] ) statement
5372 for ( nested-declaration expression[opt] ; expression[opt] ) statement
5373
5374 The form with a declaration is new in C99.
5375
5376 ??? In accordance with the old parser, the declaration may be a
5377 nested function, which is then rejected in check_for_loop_decls,
5378 but does it make any sense for this to be included in the grammar?
5379 Note in particular that the nested function does not include a
5380 trailing ';', whereas the "declaration" production includes one.
5381 Also, can we reject bad declarations earlier and cheaper than
f05b9d93
NP
5382 check_for_loop_decls?
5383
5384 In Objective-C, there are two additional variants:
5385
5386 foreach-statement:
5387 for ( expression in expresssion ) statement
5388 for ( declaration in expression ) statement
5389
5390 This is inconsistent with C, because the second variant is allowed
5391 even if c99 is not enabled.
5392
5393 The rest of the comment documents these Objective-C foreach-statement.
5394
5395 Here is the canonical example of the first variant:
5396 for (object in array) { do something with object }
5397 we call the first expression ("object") the "object_expression" and
5398 the second expression ("array") the "collection_expression".
5399 object_expression must be an lvalue of type "id" (a generic Objective-C
5400 object) because the loop works by assigning to object_expression the
5401 various objects from the collection_expression. collection_expression
5402 must evaluate to something of type "id" which responds to the method
5403 countByEnumeratingWithState:objects:count:.
5404
5405 The canonical example of the second variant is:
5406 for (id object in array) { do something with object }
5407 which is completely equivalent to
5408 {
5409 id object;
5410 for (object in array) { do something with object }
5411 }
5412 Note that initizializing 'object' in some way (eg, "for ((object =
5413 xxx) in array) { do something with object }") is possibly
5414 technically valid, but completely pointless as 'object' will be
5415 assigned to something else as soon as the loop starts. We should
5416 most likely reject it (TODO).
5417
5418 The beginning of the Objective-C foreach-statement looks exactly
5419 like the beginning of the for-statement, and we can tell it is a
5420 foreach-statement only because the initial declaration or
5421 expression is terminated by 'in' instead of ';'.
5422*/
27bf414c
JM
5423
5424static void
8170608b 5425c_parser_for_statement (c_parser *parser, bool ivdep)
27bf414c
JM
5426{
5427 tree block, cond, incr, save_break, save_cont, body;
f05b9d93 5428 /* The following are only used when parsing an ObjC foreach statement. */
689f2c82
AO
5429 tree object_expression;
5430 /* Silence the bogus uninitialized warning. */
5431 tree collection_expression = NULL;
c2255bc4
AH
5432 location_t loc = c_parser_peek_token (parser)->location;
5433 location_t for_loc = c_parser_peek_token (parser)->location;
f05b9d93 5434 bool is_foreach_statement = false;
27bf414c
JM
5435 gcc_assert (c_parser_next_token_is_keyword (parser, RID_FOR));
5436 c_parser_consume_token (parser);
f05b9d93
NP
5437 /* Open a compound statement in Objective-C as well, just in case this is
5438 as foreach expression. */
5439 block = c_begin_compound_stmt (flag_isoc99 || c_dialect_objc ());
91b90ead
UB
5440 cond = error_mark_node;
5441 incr = error_mark_node;
27bf414c
JM
5442 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5443 {
5444 /* Parse the initialization declaration or expression. */
f05b9d93 5445 object_expression = error_mark_node;
a5812bdc 5446 parser->objc_could_be_foreach_context = c_dialect_objc ();
27bf414c
JM
5447 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5448 {
a5812bdc 5449 parser->objc_could_be_foreach_context = false;
27bf414c 5450 c_parser_consume_token (parser);
c2255bc4 5451 c_finish_expr_stmt (loc, NULL_TREE);
27bf414c 5452 }
2f413185 5453 else if (c_parser_next_tokens_start_declaration (parser))
27bf414c 5454 {
f05b9d93 5455 c_parser_declaration_or_fndef (parser, true, true, true, true, true,
acf0174b 5456 &object_expression, vNULL);
f05b9d93
NP
5457 parser->objc_could_be_foreach_context = false;
5458
5459 if (c_parser_next_token_is_keyword (parser, RID_IN))
5460 {
5461 c_parser_consume_token (parser);
5462 is_foreach_statement = true;
5463 if (check_for_loop_decls (for_loc, true) == NULL_TREE)
5464 c_parser_error (parser, "multiple iterating variables in fast enumeration");
5465 }
5466 else
5467 check_for_loop_decls (for_loc, flag_isoc99);
27bf414c
JM
5468 }
5469 else if (c_parser_next_token_is_keyword (parser, RID_EXTENSION))
5470 {
5471 /* __extension__ can start a declaration, but is also an
5472 unary operator that can start an expression. Consume all
5473 but the last of a possible series of __extension__ to
5474 determine which. */
5475 while (c_parser_peek_2nd_token (parser)->type == CPP_KEYWORD
5476 && (c_parser_peek_2nd_token (parser)->keyword
5477 == RID_EXTENSION))
5478 c_parser_consume_token (parser);
32912286 5479 if (c_token_starts_declaration (c_parser_peek_2nd_token (parser)))
27bf414c
JM
5480 {
5481 int ext;
5482 ext = disable_extension_diagnostics ();
5483 c_parser_consume_token (parser);
32912286 5484 c_parser_declaration_or_fndef (parser, true, true, true, true,
acf0174b 5485 true, &object_expression, vNULL);
f05b9d93
NP
5486 parser->objc_could_be_foreach_context = false;
5487
27bf414c 5488 restore_extension_diagnostics (ext);
f05b9d93
NP
5489 if (c_parser_next_token_is_keyword (parser, RID_IN))
5490 {
5491 c_parser_consume_token (parser);
5492 is_foreach_statement = true;
5493 if (check_for_loop_decls (for_loc, true) == NULL_TREE)
5494 c_parser_error (parser, "multiple iterating variables in fast enumeration");
5495 }
5496 else
5497 check_for_loop_decls (for_loc, flag_isoc99);
27bf414c
JM
5498 }
5499 else
5500 goto init_expr;
5501 }
5502 else
5503 {
5504 init_expr:
f05b9d93 5505 {
267bac10 5506 struct c_expr ce;
f05b9d93 5507 tree init_expression;
267bac10 5508 ce = c_parser_expression (parser);
e5e44252
AK
5509 /* In theory we could forbid _Cilk_spawn here, as the spec says "only in top
5510 level statement", but it works just fine, so allow it. */
267bac10 5511 init_expression = ce.value;
f05b9d93
NP
5512 parser->objc_could_be_foreach_context = false;
5513 if (c_parser_next_token_is_keyword (parser, RID_IN))
5514 {
5515 c_parser_consume_token (parser);
5516 is_foreach_statement = true;
5517 if (! lvalue_p (init_expression))
5518 c_parser_error (parser, "invalid iterating variable in fast enumeration");
69a97201 5519 object_expression = c_fully_fold (init_expression, false, NULL);
f05b9d93
NP
5520 }
5521 else
5522 {
267bac10
JM
5523 ce = convert_lvalue_to_rvalue (loc, ce, true, false);
5524 init_expression = ce.value;
f05b9d93
NP
5525 c_finish_expr_stmt (loc, init_expression);
5526 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
5527 }
5528 }
27bf414c 5529 }
f05b9d93
NP
5530 /* Parse the loop condition. In the case of a foreach
5531 statement, there is no loop condition. */
a5812bdc 5532 gcc_assert (!parser->objc_could_be_foreach_context);
f05b9d93 5533 if (!is_foreach_statement)
27bf414c 5534 {
f05b9d93
NP
5535 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5536 {
8170608b
TB
5537 if (ivdep)
5538 {
5539 c_parser_error (parser, "missing loop condition in loop with "
5540 "%<GCC ivdep%> pragma");
5541 cond = error_mark_node;
5542 }
5543 else
5544 {
5545 c_parser_consume_token (parser);
5546 cond = NULL_TREE;
5547 }
f05b9d93
NP
5548 }
5549 else
5550 {
5551 cond = c_parser_condition (parser);
e5e44252
AK
5552 if (check_no_cilk (cond,
5553 "Cilk array notation cannot be used in a condition for a for-loop",
5554 "%<_Cilk_spawn%> statement cannot be used in a condition for a for-loop"))
5555 cond = error_mark_node;
36536d79
BI
5556 c_parser_skip_until_found (parser, CPP_SEMICOLON,
5557 "expected %<;%>");
f05b9d93 5558 }
8170608b
TB
5559 if (ivdep && cond != error_mark_node)
5560 cond = build2 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
5561 build_int_cst (integer_type_node,
5562 annot_expr_ivdep_kind));
27bf414c 5563 }
f05b9d93
NP
5564 /* Parse the increment expression (the third expression in a
5565 for-statement). In the case of a foreach-statement, this is
5566 the expression that follows the 'in'. */
5567 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
27bf414c 5568 {
f05b9d93
NP
5569 if (is_foreach_statement)
5570 {
5571 c_parser_error (parser, "missing collection in fast enumeration");
5572 collection_expression = error_mark_node;
5573 }
5574 else
5575 incr = c_process_expr_stmt (loc, NULL_TREE);
27bf414c 5576 }
27bf414c 5577 else
f05b9d93
NP
5578 {
5579 if (is_foreach_statement)
69a97201
NP
5580 collection_expression = c_fully_fold (c_parser_expression (parser).value,
5581 false, NULL);
f05b9d93 5582 else
267bac10
JM
5583 {
5584 struct c_expr ce = c_parser_expression (parser);
5585 ce = convert_lvalue_to_rvalue (loc, ce, true, false);
5586 incr = c_process_expr_stmt (loc, ce.value);
5587 }
f05b9d93 5588 }
27bf414c
JM
5589 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
5590 }
27bf414c
JM
5591 save_break = c_break_label;
5592 c_break_label = NULL_TREE;
5593 save_cont = c_cont_label;
5594 c_cont_label = NULL_TREE;
5595 body = c_parser_c99_block_statement (parser);
f05b9d93
NP
5596 if (is_foreach_statement)
5597 objc_finish_foreach_loop (loc, object_expression, collection_expression, body, c_break_label, c_cont_label);
5598 else
5599 c_finish_loop (loc, cond, incr, body, c_break_label, c_cont_label, true);
5600 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99 || c_dialect_objc ()));
27bf414c
JM
5601 c_break_label = save_break;
5602 c_cont_label = save_cont;
5603}
5604
5605/* Parse an asm statement, a GNU extension. This is a full-blown asm
5606 statement with inputs, outputs, clobbers, and volatile tag
5607 allowed.
5608
5609 asm-statement:
5610 asm type-qualifier[opt] ( asm-argument ) ;
1c384bf1 5611 asm type-qualifier[opt] goto ( asm-goto-argument ) ;
27bf414c
JM
5612
5613 asm-argument:
5614 asm-string-literal
5615 asm-string-literal : asm-operands[opt]
5616 asm-string-literal : asm-operands[opt] : asm-operands[opt]
1c384bf1
RH
5617 asm-string-literal : asm-operands[opt] : asm-operands[opt] : asm-clobbers[opt]
5618
5619 asm-goto-argument:
5620 asm-string-literal : : asm-operands[opt] : asm-clobbers[opt] \
5621 : asm-goto-operands
27bf414c
JM
5622
5623 Qualifiers other than volatile are accepted in the syntax but
5624 warned for. */
5625
5626static tree
5627c_parser_asm_statement (c_parser *parser)
5628{
1c384bf1
RH
5629 tree quals, str, outputs, inputs, clobbers, labels, ret;
5630 bool simple, is_goto;
c2255bc4 5631 location_t asm_loc = c_parser_peek_token (parser)->location;
1c384bf1
RH
5632 int section, nsections;
5633
27bf414c
JM
5634 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ASM));
5635 c_parser_consume_token (parser);
5636 if (c_parser_next_token_is_keyword (parser, RID_VOLATILE))
5637 {
5638 quals = c_parser_peek_token (parser)->value;
5639 c_parser_consume_token (parser);
5640 }
5641 else if (c_parser_next_token_is_keyword (parser, RID_CONST)
5642 || c_parser_next_token_is_keyword (parser, RID_RESTRICT))
5643 {
3ba09659
AH
5644 warning_at (c_parser_peek_token (parser)->location,
5645 0,
5646 "%E qualifier ignored on asm",
5647 c_parser_peek_token (parser)->value);
27bf414c
JM
5648 quals = NULL_TREE;
5649 c_parser_consume_token (parser);
5650 }
5651 else
5652 quals = NULL_TREE;
1c384bf1
RH
5653
5654 is_goto = false;
5655 if (c_parser_next_token_is_keyword (parser, RID_GOTO))
5656 {
5657 c_parser_consume_token (parser);
5658 is_goto = true;
5659 }
5660
27bf414c 5661 /* ??? Follow the C++ parser rather than using the
46c2514e
TT
5662 lex_untranslated_string kludge. */
5663 parser->lex_untranslated_string = true;
1c384bf1
RH
5664 ret = NULL;
5665
27bf414c 5666 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
1c384bf1
RH
5667 goto error;
5668
27bf414c 5669 str = c_parser_asm_string_literal (parser);
b85eb797 5670 if (str == NULL_TREE)
1c384bf1
RH
5671 goto error_close_paren;
5672
5673 simple = true;
5674 outputs = NULL_TREE;
5675 inputs = NULL_TREE;
5676 clobbers = NULL_TREE;
5677 labels = NULL_TREE;
5678
5679 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN) && !is_goto)
5680 goto done_asm;
5681
5682 /* Parse each colon-delimited section of operands. */
5683 nsections = 3 + is_goto;
5684 for (section = 0; section < nsections; ++section)
b85eb797 5685 {
1c384bf1
RH
5686 if (!c_parser_require (parser, CPP_COLON,
5687 is_goto
5688 ? "expected %<:%>"
5689 : "expected %<:%> or %<)%>"))
5690 goto error_close_paren;
5691
5692 /* Once past any colon, we're no longer a simple asm. */
5693 simple = false;
5694
5695 if ((!c_parser_next_token_is (parser, CPP_COLON)
5696 && !c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
5697 || section == 3)
5698 switch (section)
5699 {
5700 case 0:
5701 /* For asm goto, we don't allow output operands, but reserve
5702 the slot for a future extension that does allow them. */
5703 if (!is_goto)
eadd3d0d 5704 outputs = c_parser_asm_operands (parser);
1c384bf1
RH
5705 break;
5706 case 1:
eadd3d0d 5707 inputs = c_parser_asm_operands (parser);
1c384bf1
RH
5708 break;
5709 case 2:
5710 clobbers = c_parser_asm_clobbers (parser);
5711 break;
5712 case 3:
5713 labels = c_parser_asm_goto_operands (parser);
5714 break;
5715 default:
5716 gcc_unreachable ();
5717 }
5718
5719 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN) && !is_goto)
5720 goto done_asm;
27bf414c 5721 }
1c384bf1 5722
27bf414c 5723 done_asm:
27bf414c
JM
5724 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
5725 {
5726 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
1c384bf1 5727 goto error;
27bf414c 5728 }
1c384bf1 5729
27bf414c
JM
5730 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
5731 c_parser_skip_to_end_of_block_or_statement (parser);
1c384bf1 5732
c2255bc4 5733 ret = build_asm_stmt (quals, build_asm_expr (asm_loc, str, outputs, inputs,
1c384bf1
RH
5734 clobbers, labels, simple));
5735
5736 error:
5737 parser->lex_untranslated_string = false;
27bf414c 5738 return ret;
1c384bf1
RH
5739
5740 error_close_paren:
5741 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5742 goto error;
27bf414c
JM
5743}
5744
eadd3d0d 5745/* Parse asm operands, a GNU extension.
27bf414c
JM
5746
5747 asm-operands:
5748 asm-operand
5749 asm-operands , asm-operand
5750
5751 asm-operand:
5752 asm-string-literal ( expression )
5753 [ identifier ] asm-string-literal ( expression )
5754*/
5755
5756static tree
eadd3d0d 5757c_parser_asm_operands (c_parser *parser)
27bf414c
JM
5758{
5759 tree list = NULL_TREE;
5760 while (true)
5761 {
f2a71bbc
JM
5762 tree name, str;
5763 struct c_expr expr;
27bf414c
JM
5764 if (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
5765 {
5766 c_parser_consume_token (parser);
5767 if (c_parser_next_token_is (parser, CPP_NAME))
5768 {
5769 tree id = c_parser_peek_token (parser)->value;
5770 c_parser_consume_token (parser);
5771 name = build_string (IDENTIFIER_LENGTH (id),
5772 IDENTIFIER_POINTER (id));
5773 }
5774 else
5775 {
5776 c_parser_error (parser, "expected identifier");
5777 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
5778 return NULL_TREE;
5779 }
5780 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
5781 "expected %<]%>");
5782 }
5783 else
5784 name = NULL_TREE;
5785 str = c_parser_asm_string_literal (parser);
5786 if (str == NULL_TREE)
5787 return NULL_TREE;
46c2514e 5788 parser->lex_untranslated_string = false;
27bf414c
JM
5789 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5790 {
46c2514e 5791 parser->lex_untranslated_string = true;
27bf414c
JM
5792 return NULL_TREE;
5793 }
f2a71bbc 5794 expr = c_parser_expression (parser);
ebfbbdc5 5795 mark_exp_read (expr.value);
46c2514e 5796 parser->lex_untranslated_string = true;
27bf414c
JM
5797 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
5798 {
5799 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5800 return NULL_TREE;
5801 }
5802 list = chainon (list, build_tree_list (build_tree_list (name, str),
f2a71bbc 5803 expr.value));
27bf414c
JM
5804 if (c_parser_next_token_is (parser, CPP_COMMA))
5805 c_parser_consume_token (parser);
5806 else
5807 break;
5808 }
5809 return list;
5810}
5811
5812/* Parse asm clobbers, a GNU extension.
5813
5814 asm-clobbers:
5815 asm-string-literal
5816 asm-clobbers , asm-string-literal
5817*/
5818
5819static tree
5820c_parser_asm_clobbers (c_parser *parser)
5821{
5822 tree list = NULL_TREE;
5823 while (true)
5824 {
5825 tree str = c_parser_asm_string_literal (parser);
5826 if (str)
5827 list = tree_cons (NULL_TREE, str, list);
5828 else
5829 return NULL_TREE;
5830 if (c_parser_next_token_is (parser, CPP_COMMA))
5831 c_parser_consume_token (parser);
5832 else
5833 break;
5834 }
5835 return list;
5836}
5837
1c384bf1 5838/* Parse asm goto labels, a GNU extension.
b8698a0f 5839
1c384bf1
RH
5840 asm-goto-operands:
5841 identifier
5842 asm-goto-operands , identifier
5843*/
5844
5845static tree
5846c_parser_asm_goto_operands (c_parser *parser)
5847{
5848 tree list = NULL_TREE;
5849 while (true)
5850 {
5851 tree name, label;
5852
5853 if (c_parser_next_token_is (parser, CPP_NAME))
5854 {
5855 c_token *tok = c_parser_peek_token (parser);
5856 name = tok->value;
5857 label = lookup_label_for_goto (tok->location, name);
5858 c_parser_consume_token (parser);
5859 TREE_USED (label) = 1;
5860 }
5861 else
5862 {
5863 c_parser_error (parser, "expected identifier");
5864 return NULL_TREE;
5865 }
5866
5867 name = build_string (IDENTIFIER_LENGTH (name),
5868 IDENTIFIER_POINTER (name));
5869 list = tree_cons (name, label, list);
5870 if (c_parser_next_token_is (parser, CPP_COMMA))
5871 c_parser_consume_token (parser);
5872 else
5873 return nreverse (list);
5874 }
5875}
5876
27bf414c
JM
5877/* Parse an expression other than a compound expression; that is, an
5878 assignment expression (C90 6.3.16, C99 6.5.16). If AFTER is not
5879 NULL then it is an Objective-C message expression which is the
5880 primary-expression starting the expression as an initializer.
5881
5882 assignment-expression:
5883 conditional-expression
5884 unary-expression assignment-operator assignment-expression
5885
5886 assignment-operator: one of
5887 = *= /= %= += -= <<= >>= &= ^= |=
5888
5889 In GNU C we accept any conditional expression on the LHS and
5890 diagnose the invalid lvalue rather than producing a syntax
5891 error. */
5892
5893static struct c_expr
acf0174b
JJ
5894c_parser_expr_no_commas (c_parser *parser, struct c_expr *after,
5895 tree omp_atomic_lhs)
27bf414c
JM
5896{
5897 struct c_expr lhs, rhs, ret;
5898 enum tree_code code;
c2255bc4 5899 location_t op_location, exp_location;
27bf414c 5900 gcc_assert (!after || c_dialect_objc ());
acf0174b 5901 lhs = c_parser_conditional_expression (parser, after, omp_atomic_lhs);
c9f9eb5d 5902 op_location = c_parser_peek_token (parser)->location;
27bf414c
JM
5903 switch (c_parser_peek_token (parser)->type)
5904 {
5905 case CPP_EQ:
5906 code = NOP_EXPR;
5907 break;
5908 case CPP_MULT_EQ:
5909 code = MULT_EXPR;
5910 break;
5911 case CPP_DIV_EQ:
5912 code = TRUNC_DIV_EXPR;
5913 break;
5914 case CPP_MOD_EQ:
5915 code = TRUNC_MOD_EXPR;
5916 break;
5917 case CPP_PLUS_EQ:
5918 code = PLUS_EXPR;
5919 break;
5920 case CPP_MINUS_EQ:
5921 code = MINUS_EXPR;
5922 break;
5923 case CPP_LSHIFT_EQ:
5924 code = LSHIFT_EXPR;
5925 break;
5926 case CPP_RSHIFT_EQ:
5927 code = RSHIFT_EXPR;
5928 break;
5929 case CPP_AND_EQ:
5930 code = BIT_AND_EXPR;
5931 break;
5932 case CPP_XOR_EQ:
5933 code = BIT_XOR_EXPR;
5934 break;
5935 case CPP_OR_EQ:
5936 code = BIT_IOR_EXPR;
5937 break;
5938 default:
5939 return lhs;
5940 }
5941 c_parser_consume_token (parser);
c2255bc4 5942 exp_location = c_parser_peek_token (parser)->location;
27bf414c 5943 rhs = c_parser_expr_no_commas (parser, NULL);
267bac10 5944 rhs = convert_lvalue_to_rvalue (exp_location, rhs, true, true);
36536d79 5945
25c22937
BI
5946 ret.value = build_modify_expr (op_location, lhs.value, lhs.original_type,
5947 code, exp_location, rhs.value,
5948 rhs.original_type);
27bf414c
JM
5949 if (code == NOP_EXPR)
5950 ret.original_code = MODIFY_EXPR;
5951 else
5952 {
5953 TREE_NO_WARNING (ret.value) = 1;
5954 ret.original_code = ERROR_MARK;
5955 }
6866c6e8 5956 ret.original_type = NULL;
27bf414c
JM
5957 return ret;
5958}
5959
5960/* Parse a conditional expression (C90 6.3.15, C99 6.5.15). If AFTER
5961 is not NULL then it is an Objective-C message expression which is
5962 the primary-expression starting the expression as an initializer.
5963
5964 conditional-expression:
5965 logical-OR-expression
5966 logical-OR-expression ? expression : conditional-expression
5967
5968 GNU extensions:
5969
5970 conditional-expression:
5971 logical-OR-expression ? : conditional-expression
5972*/
5973
5974static struct c_expr
acf0174b
JJ
5975c_parser_conditional_expression (c_parser *parser, struct c_expr *after,
5976 tree omp_atomic_lhs)
27bf414c
JM
5977{
5978 struct c_expr cond, exp1, exp2, ret;
d166d4c3 5979 location_t cond_loc, colon_loc, middle_loc;
ba47d38d 5980
27bf414c 5981 gcc_assert (!after || c_dialect_objc ());
ba47d38d 5982
acf0174b 5983 cond = c_parser_binary_expression (parser, after, omp_atomic_lhs);
ba47d38d 5984
27bf414c
JM
5985 if (c_parser_next_token_is_not (parser, CPP_QUERY))
5986 return cond;
c2255bc4 5987 cond_loc = c_parser_peek_token (parser)->location;
267bac10 5988 cond = convert_lvalue_to_rvalue (cond_loc, cond, true, true);
27bf414c
JM
5989 c_parser_consume_token (parser);
5990 if (c_parser_next_token_is (parser, CPP_COLON))
5991 {
8ce94e44 5992 tree eptype = NULL_TREE;
d166d4c3
AK
5993
5994 middle_loc = c_parser_peek_token (parser)->location;
c1771a20 5995 pedwarn (middle_loc, OPT_Wpedantic,
509c9d60 5996 "ISO C forbids omitting the middle term of a ?: expression");
d166d4c3 5997 warn_for_omitted_condop (middle_loc, cond.value);
8ce94e44
JM
5998 if (TREE_CODE (cond.value) == EXCESS_PRECISION_EXPR)
5999 {
6000 eptype = TREE_TYPE (cond.value);
6001 cond.value = TREE_OPERAND (cond.value, 0);
6002 }
27bf414c 6003 /* Make sure first operand is calculated only once. */
928c19bb 6004 exp1.value = c_save_expr (default_conversion (cond.value));
8ce94e44
JM
6005 if (eptype)
6006 exp1.value = build1 (EXCESS_PRECISION_EXPR, eptype, exp1.value);
6866c6e8 6007 exp1.original_type = NULL;
ba47d38d 6008 cond.value = c_objc_common_truthvalue_conversion (cond_loc, exp1.value);
7d882b83 6009 c_inhibit_evaluation_warnings += cond.value == truthvalue_true_node;
27bf414c
JM
6010 }
6011 else
6012 {
6013 cond.value
85498824 6014 = c_objc_common_truthvalue_conversion
ba47d38d 6015 (cond_loc, default_conversion (cond.value));
7d882b83 6016 c_inhibit_evaluation_warnings += cond.value == truthvalue_false_node;
46bdb9cf 6017 exp1 = c_parser_expression_conv (parser);
ebfbbdc5 6018 mark_exp_read (exp1.value);
7d882b83
ILT
6019 c_inhibit_evaluation_warnings +=
6020 ((cond.value == truthvalue_true_node)
6021 - (cond.value == truthvalue_false_node));
27bf414c 6022 }
744aa42f
ILT
6023
6024 colon_loc = c_parser_peek_token (parser)->location;
27bf414c
JM
6025 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
6026 {
7d882b83 6027 c_inhibit_evaluation_warnings -= cond.value == truthvalue_true_node;
27bf414c
JM
6028 ret.value = error_mark_node;
6029 ret.original_code = ERROR_MARK;
6866c6e8 6030 ret.original_type = NULL;
27bf414c
JM
6031 return ret;
6032 }
c2255bc4
AH
6033 {
6034 location_t exp2_loc = c_parser_peek_token (parser)->location;
acf0174b 6035 exp2 = c_parser_conditional_expression (parser, NULL, NULL_TREE);
267bac10 6036 exp2 = convert_lvalue_to_rvalue (exp2_loc, exp2, true, true);
c2255bc4 6037 }
7d882b83 6038 c_inhibit_evaluation_warnings -= cond.value == truthvalue_true_node;
744aa42f 6039 ret.value = build_conditional_expr (colon_loc, cond.value,
928c19bb 6040 cond.original_code == C_MAYBE_CONST_EXPR,
d130ae11
ILT
6041 exp1.value, exp1.original_type,
6042 exp2.value, exp2.original_type);
27bf414c 6043 ret.original_code = ERROR_MARK;
6866c6e8
ILT
6044 if (exp1.value == error_mark_node || exp2.value == error_mark_node)
6045 ret.original_type = NULL;
6046 else
6047 {
6048 tree t1, t2;
6049
6050 /* If both sides are enum type, the default conversion will have
6051 made the type of the result be an integer type. We want to
6052 remember the enum types we started with. */
6053 t1 = exp1.original_type ? exp1.original_type : TREE_TYPE (exp1.value);
6054 t2 = exp2.original_type ? exp2.original_type : TREE_TYPE (exp2.value);
6055 ret.original_type = ((t1 != error_mark_node
6056 && t2 != error_mark_node
6057 && (TYPE_MAIN_VARIANT (t1)
6058 == TYPE_MAIN_VARIANT (t2)))
6059 ? t1
6060 : NULL);
6061 }
27bf414c
JM
6062 return ret;
6063}
6064
6065/* Parse a binary expression; that is, a logical-OR-expression (C90
6066 6.3.5-6.3.14, C99 6.5.5-6.5.14). If AFTER is not NULL then it is
6067 an Objective-C message expression which is the primary-expression
acf0174b
JJ
6068 starting the expression as an initializer.
6069
6070 OMP_ATOMIC_LHS is NULL, unless parsing OpenMP #pragma omp atomic,
6071 when it should be the unfolded lhs. In a valid OpenMP source,
6072 one of the operands of the toplevel binary expression must be equal
6073 to it. In that case, just return a build2 created binary operation
6074 rather than result of parser_build_binary_op.
27bf414c
JM
6075
6076 multiplicative-expression:
6077 cast-expression
6078 multiplicative-expression * cast-expression
6079 multiplicative-expression / cast-expression
6080 multiplicative-expression % cast-expression
6081
6082 additive-expression:
6083 multiplicative-expression
6084 additive-expression + multiplicative-expression
6085 additive-expression - multiplicative-expression
6086
6087 shift-expression:
6088 additive-expression
6089 shift-expression << additive-expression
6090 shift-expression >> additive-expression
6091
6092 relational-expression:
6093 shift-expression
6094 relational-expression < shift-expression
6095 relational-expression > shift-expression
6096 relational-expression <= shift-expression
6097 relational-expression >= shift-expression
6098
6099 equality-expression:
6100 relational-expression
6101 equality-expression == relational-expression
6102 equality-expression != relational-expression
6103
6104 AND-expression:
6105 equality-expression
6106 AND-expression & equality-expression
6107
6108 exclusive-OR-expression:
6109 AND-expression
6110 exclusive-OR-expression ^ AND-expression
6111
6112 inclusive-OR-expression:
6113 exclusive-OR-expression
6114 inclusive-OR-expression | exclusive-OR-expression
6115
6116 logical-AND-expression:
6117 inclusive-OR-expression
6118 logical-AND-expression && inclusive-OR-expression
6119
6120 logical-OR-expression:
6121 logical-AND-expression
6122 logical-OR-expression || logical-AND-expression
6123*/
6124
6125static struct c_expr
20906c66 6126c_parser_binary_expression (c_parser *parser, struct c_expr *after,
acf0174b 6127 tree omp_atomic_lhs)
27bf414c
JM
6128{
6129 /* A binary expression is parsed using operator-precedence parsing,
6130 with the operands being cast expressions. All the binary
6131 operators are left-associative. Thus a binary expression is of
6132 form:
6133
6134 E0 op1 E1 op2 E2 ...
6135
6136 which we represent on a stack. On the stack, the precedence
6137 levels are strictly increasing. When a new operator is
6138 encountered of higher precedence than that at the top of the
6139 stack, it is pushed; its LHS is the top expression, and its RHS
6140 is everything parsed until it is popped. When a new operator is
6141 encountered with precedence less than or equal to that at the top
6142 of the stack, triples E[i-1] op[i] E[i] are popped and replaced
6143 by the result of the operation until the operator at the top of
6144 the stack has lower precedence than the new operator or there is
6145 only one element on the stack; then the top expression is the LHS
6146 of the new operator. In the case of logical AND and OR
7d882b83
ILT
6147 expressions, we also need to adjust c_inhibit_evaluation_warnings
6148 as appropriate when the operators are pushed and popped. */
27bf414c 6149
27bf414c
JM
6150 struct {
6151 /* The expression at this stack level. */
6152 struct c_expr expr;
6153 /* The precedence of the operator on its left, PREC_NONE at the
6154 bottom of the stack. */
20906c66 6155 enum c_parser_prec prec;
27bf414c
JM
6156 /* The operation on its left. */
6157 enum tree_code op;
ca80e52b
EB
6158 /* The source location of this operation. */
6159 location_t loc;
27bf414c
JM
6160 } stack[NUM_PRECS];
6161 int sp;
ba47d38d 6162 /* Location of the binary operator. */
1f6d0c60 6163 location_t binary_loc = UNKNOWN_LOCATION; /* Quiet warning. */
27bf414c
JM
6164#define POP \
6165 do { \
6166 switch (stack[sp].op) \
6167 { \
6168 case TRUTH_ANDIF_EXPR: \
7d882b83
ILT
6169 c_inhibit_evaluation_warnings -= (stack[sp - 1].expr.value \
6170 == truthvalue_false_node); \
27bf414c
JM
6171 break; \
6172 case TRUTH_ORIF_EXPR: \
7d882b83
ILT
6173 c_inhibit_evaluation_warnings -= (stack[sp - 1].expr.value \
6174 == truthvalue_true_node); \
27bf414c
JM
6175 break; \
6176 default: \
6177 break; \
6178 } \
f2a71bbc 6179 stack[sp - 1].expr \
267bac10
JM
6180 = convert_lvalue_to_rvalue (stack[sp - 1].loc, \
6181 stack[sp - 1].expr, true, true); \
f2a71bbc 6182 stack[sp].expr \
267bac10
JM
6183 = convert_lvalue_to_rvalue (stack[sp].loc, \
6184 stack[sp].expr, true, true); \
acf0174b
JJ
6185 if (__builtin_expect (omp_atomic_lhs != NULL_TREE, 0) && sp == 1 \
6186 && c_parser_peek_token (parser)->type == CPP_SEMICOLON \
6187 && ((1 << stack[sp].prec) \
6188 & (1 << (PREC_BITOR | PREC_BITXOR | PREC_BITAND | PREC_SHIFT \
6189 | PREC_ADD | PREC_MULT))) \
6190 && stack[sp].op != TRUNC_MOD_EXPR \
6191 && stack[0].expr.value != error_mark_node \
6192 && stack[1].expr.value != error_mark_node \
6193 && (c_tree_equal (stack[0].expr.value, omp_atomic_lhs) \
6194 || c_tree_equal (stack[1].expr.value, omp_atomic_lhs))) \
6195 stack[0].expr.value \
6196 = build2 (stack[1].op, TREE_TYPE (stack[0].expr.value), \
6197 stack[0].expr.value, stack[1].expr.value); \
6198 else \
6199 stack[sp - 1].expr = parser_build_binary_op (stack[sp].loc, \
6200 stack[sp].op, \
6201 stack[sp - 1].expr, \
6202 stack[sp].expr); \
27bf414c
JM
6203 sp--; \
6204 } while (0)
6205 gcc_assert (!after || c_dialect_objc ());
ca80e52b 6206 stack[0].loc = c_parser_peek_token (parser)->location;
27bf414c 6207 stack[0].expr = c_parser_cast_expression (parser, after);
acf0174b 6208 stack[0].prec = PREC_NONE;
27bf414c
JM
6209 sp = 0;
6210 while (true)
6211 {
20906c66 6212 enum c_parser_prec oprec;
27bf414c
JM
6213 enum tree_code ocode;
6214 if (parser->error)
6215 goto out;
6216 switch (c_parser_peek_token (parser)->type)
6217 {
6218 case CPP_MULT:
6219 oprec = PREC_MULT;
6220 ocode = MULT_EXPR;
6221 break;
6222 case CPP_DIV:
6223 oprec = PREC_MULT;
6224 ocode = TRUNC_DIV_EXPR;
6225 break;
6226 case CPP_MOD:
6227 oprec = PREC_MULT;
6228 ocode = TRUNC_MOD_EXPR;
6229 break;
6230 case CPP_PLUS:
6231 oprec = PREC_ADD;
6232 ocode = PLUS_EXPR;
6233 break;
6234 case CPP_MINUS:
6235 oprec = PREC_ADD;
6236 ocode = MINUS_EXPR;
6237 break;
6238 case CPP_LSHIFT:
6239 oprec = PREC_SHIFT;
6240 ocode = LSHIFT_EXPR;
6241 break;
6242 case CPP_RSHIFT:
6243 oprec = PREC_SHIFT;
6244 ocode = RSHIFT_EXPR;
6245 break;
6246 case CPP_LESS:
6247 oprec = PREC_REL;
6248 ocode = LT_EXPR;
6249 break;
6250 case CPP_GREATER:
6251 oprec = PREC_REL;
6252 ocode = GT_EXPR;
6253 break;
6254 case CPP_LESS_EQ:
6255 oprec = PREC_REL;
6256 ocode = LE_EXPR;
6257 break;
6258 case CPP_GREATER_EQ:
6259 oprec = PREC_REL;
6260 ocode = GE_EXPR;
6261 break;
6262 case CPP_EQ_EQ:
6263 oprec = PREC_EQ;
6264 ocode = EQ_EXPR;
6265 break;
6266 case CPP_NOT_EQ:
6267 oprec = PREC_EQ;
6268 ocode = NE_EXPR;
6269 break;
6270 case CPP_AND:
6271 oprec = PREC_BITAND;
6272 ocode = BIT_AND_EXPR;
6273 break;
6274 case CPP_XOR:
6275 oprec = PREC_BITXOR;
6276 ocode = BIT_XOR_EXPR;
6277 break;
6278 case CPP_OR:
6279 oprec = PREC_BITOR;
6280 ocode = BIT_IOR_EXPR;
6281 break;
6282 case CPP_AND_AND:
6283 oprec = PREC_LOGAND;
6284 ocode = TRUTH_ANDIF_EXPR;
6285 break;
6286 case CPP_OR_OR:
6287 oprec = PREC_LOGOR;
6288 ocode = TRUTH_ORIF_EXPR;
6289 break;
6290 default:
6291 /* Not a binary operator, so end of the binary
6292 expression. */
6293 goto out;
6294 }
ba47d38d 6295 binary_loc = c_parser_peek_token (parser)->location;
27bf414c 6296 while (oprec <= stack[sp].prec)
acf0174b 6297 POP;
20906c66 6298 c_parser_consume_token (parser);
27bf414c
JM
6299 switch (ocode)
6300 {
6301 case TRUTH_ANDIF_EXPR:
f2a71bbc 6302 stack[sp].expr
267bac10
JM
6303 = convert_lvalue_to_rvalue (stack[sp].loc,
6304 stack[sp].expr, true, true);
85498824 6305 stack[sp].expr.value = c_objc_common_truthvalue_conversion
ca80e52b 6306 (stack[sp].loc, default_conversion (stack[sp].expr.value));
7d882b83
ILT
6307 c_inhibit_evaluation_warnings += (stack[sp].expr.value
6308 == truthvalue_false_node);
27bf414c
JM
6309 break;
6310 case TRUTH_ORIF_EXPR:
f2a71bbc 6311 stack[sp].expr
267bac10
JM
6312 = convert_lvalue_to_rvalue (stack[sp].loc,
6313 stack[sp].expr, true, true);
85498824 6314 stack[sp].expr.value = c_objc_common_truthvalue_conversion
ca80e52b 6315 (stack[sp].loc, default_conversion (stack[sp].expr.value));
7d882b83
ILT
6316 c_inhibit_evaluation_warnings += (stack[sp].expr.value
6317 == truthvalue_true_node);
27bf414c
JM
6318 break;
6319 default:
6320 break;
6321 }
6322 sp++;
ca80e52b 6323 stack[sp].loc = binary_loc;
27bf414c
JM
6324 stack[sp].expr = c_parser_cast_expression (parser, NULL);
6325 stack[sp].prec = oprec;
6326 stack[sp].op = ocode;
c2255bc4 6327 stack[sp].loc = binary_loc;
27bf414c
JM
6328 }
6329 out:
6330 while (sp > 0)
6331 POP;
6332 return stack[0].expr;
6333#undef POP
6334}
6335
6336/* Parse a cast expression (C90 6.3.4, C99 6.5.4). If AFTER is not
6337 NULL then it is an Objective-C message expression which is the
6338 primary-expression starting the expression as an initializer.
6339
6340 cast-expression:
6341 unary-expression
6342 ( type-name ) unary-expression
6343*/
6344
6345static struct c_expr
6346c_parser_cast_expression (c_parser *parser, struct c_expr *after)
6347{
c2255bc4 6348 location_t cast_loc = c_parser_peek_token (parser)->location;
27bf414c
JM
6349 gcc_assert (!after || c_dialect_objc ());
6350 if (after)
c2255bc4
AH
6351 return c_parser_postfix_expression_after_primary (parser,
6352 cast_loc, *after);
27bf414c
JM
6353 /* If the expression begins with a parenthesized type name, it may
6354 be either a cast or a compound literal; we need to see whether
6355 the next character is '{' to tell the difference. If not, it is
29ce73cb
PB
6356 an unary expression. Full detection of unknown typenames here
6357 would require a 3-token lookahead. */
27bf414c
JM
6358 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
6359 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
6360 {
6361 struct c_type_name *type_name;
6362 struct c_expr ret;
f2a71bbc 6363 struct c_expr expr;
27bf414c
JM
6364 c_parser_consume_token (parser);
6365 type_name = c_parser_type_name (parser);
6366 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
6367 if (type_name == NULL)
6368 {
6369 ret.value = error_mark_node;
6370 ret.original_code = ERROR_MARK;
6866c6e8 6371 ret.original_type = NULL;
27bf414c
JM
6372 return ret;
6373 }
33c9159e
AH
6374
6375 /* Save casted types in the function's used types hash table. */
8d8d1a28 6376 used_types_insert (type_name->specs->type);
33c9159e 6377
27bf414c 6378 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
24b97832 6379 return c_parser_postfix_expression_after_paren_type (parser, type_name,
c2255bc4
AH
6380 cast_loc);
6381 {
6382 location_t expr_loc = c_parser_peek_token (parser)->location;
6383 expr = c_parser_cast_expression (parser, NULL);
267bac10 6384 expr = convert_lvalue_to_rvalue (expr_loc, expr, true, true);
c2255bc4
AH
6385 }
6386 ret.value = c_cast_expr (cast_loc, type_name, expr.value);
27bf414c 6387 ret.original_code = ERROR_MARK;
6866c6e8 6388 ret.original_type = NULL;
27bf414c
JM
6389 return ret;
6390 }
6391 else
6392 return c_parser_unary_expression (parser);
6393}
6394
6395/* Parse an unary expression (C90 6.3.3, C99 6.5.3).
6396
6397 unary-expression:
6398 postfix-expression
6399 ++ unary-expression
6400 -- unary-expression
6401 unary-operator cast-expression
6402 sizeof unary-expression
6403 sizeof ( type-name )
6404
6405 unary-operator: one of
6406 & * + - ~ !
6407
6408 GNU extensions:
6409
6410 unary-expression:
6411 __alignof__ unary-expression
6412 __alignof__ ( type-name )
6413 && identifier
6414
48b0b196 6415 (C11 permits _Alignof with type names only.)
d19fa6b5 6416
27bf414c
JM
6417 unary-operator: one of
6418 __extension__ __real__ __imag__
6419
0a35513e
AH
6420 Transactional Memory:
6421
6422 unary-expression:
6423 transaction-expression
6424
27bf414c
JM
6425 In addition, the GNU syntax treats ++ and -- as unary operators, so
6426 they may be applied to cast expressions with errors for non-lvalues
6427 given later. */
6428
6429static struct c_expr
6430c_parser_unary_expression (c_parser *parser)
6431{
6432 int ext;
46bdb9cf 6433 struct c_expr ret, op;
c2255bc4
AH
6434 location_t op_loc = c_parser_peek_token (parser)->location;
6435 location_t exp_loc;
6866c6e8
ILT
6436 ret.original_code = ERROR_MARK;
6437 ret.original_type = NULL;
27bf414c
JM
6438 switch (c_parser_peek_token (parser)->type)
6439 {
6440 case CPP_PLUS_PLUS:
6441 c_parser_consume_token (parser);
c2255bc4 6442 exp_loc = c_parser_peek_token (parser)->location;
46bdb9cf 6443 op = c_parser_cast_expression (parser, NULL);
36536d79
BI
6444
6445 /* If there is array notations in op, we expand them. */
b72271b9 6446 if (flag_cilkplus && TREE_CODE (op.value) == ARRAY_NOTATION_REF)
36536d79
BI
6447 return fix_array_notation_expr (exp_loc, PREINCREMENT_EXPR, op);
6448 else
6449 {
6450 op = default_function_array_read_conversion (exp_loc, op);
6451 return parser_build_unary_op (op_loc, PREINCREMENT_EXPR, op);
6452 }
27bf414c
JM
6453 case CPP_MINUS_MINUS:
6454 c_parser_consume_token (parser);
c2255bc4 6455 exp_loc = c_parser_peek_token (parser)->location;
46bdb9cf 6456 op = c_parser_cast_expression (parser, NULL);
36536d79
BI
6457
6458 /* If there is array notations in op, we expand them. */
b72271b9 6459 if (flag_cilkplus && TREE_CODE (op.value) == ARRAY_NOTATION_REF)
36536d79
BI
6460 return fix_array_notation_expr (exp_loc, PREDECREMENT_EXPR, op);
6461 else
6462 {
6463 op = default_function_array_read_conversion (exp_loc, op);
6464 return parser_build_unary_op (op_loc, PREDECREMENT_EXPR, op);
6465 }
27bf414c
JM
6466 case CPP_AND:
6467 c_parser_consume_token (parser);
ebfbbdc5
JJ
6468 op = c_parser_cast_expression (parser, NULL);
6469 mark_exp_read (op.value);
6470 return parser_build_unary_op (op_loc, ADDR_EXPR, op);
27bf414c
JM
6471 case CPP_MULT:
6472 c_parser_consume_token (parser);
c2255bc4 6473 exp_loc = c_parser_peek_token (parser)->location;
46bdb9cf 6474 op = c_parser_cast_expression (parser, NULL);
267bac10 6475 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
dd865ef6 6476 ret.value = build_indirect_ref (op_loc, op.value, RO_UNARY_STAR);
27bf414c
JM
6477 return ret;
6478 case CPP_PLUS:
8400e75e 6479 if (!c_dialect_objc () && !in_system_header_at (input_location))
c2255bc4 6480 warning_at (op_loc,
3ba09659
AH
6481 OPT_Wtraditional,
6482 "traditional C rejects the unary plus operator");
c7412148 6483 c_parser_consume_token (parser);
c2255bc4 6484 exp_loc = c_parser_peek_token (parser)->location;
46bdb9cf 6485 op = c_parser_cast_expression (parser, NULL);
267bac10 6486 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
c2255bc4 6487 return parser_build_unary_op (op_loc, CONVERT_EXPR, op);
27bf414c
JM
6488 case CPP_MINUS:
6489 c_parser_consume_token (parser);
c2255bc4 6490 exp_loc = c_parser_peek_token (parser)->location;
46bdb9cf 6491 op = c_parser_cast_expression (parser, NULL);
267bac10 6492 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
c2255bc4 6493 return parser_build_unary_op (op_loc, NEGATE_EXPR, op);
27bf414c
JM
6494 case CPP_COMPL:
6495 c_parser_consume_token (parser);
c2255bc4 6496 exp_loc = c_parser_peek_token (parser)->location;
46bdb9cf 6497 op = c_parser_cast_expression (parser, NULL);
267bac10 6498 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
c2255bc4 6499 return parser_build_unary_op (op_loc, BIT_NOT_EXPR, op);
27bf414c
JM
6500 case CPP_NOT:
6501 c_parser_consume_token (parser);
c2255bc4 6502 exp_loc = c_parser_peek_token (parser)->location;
46bdb9cf 6503 op = c_parser_cast_expression (parser, NULL);
267bac10 6504 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
c2255bc4 6505 return parser_build_unary_op (op_loc, TRUTH_NOT_EXPR, op);
27bf414c
JM
6506 case CPP_AND_AND:
6507 /* Refer to the address of a label as a pointer. */
6508 c_parser_consume_token (parser);
6509 if (c_parser_next_token_is (parser, CPP_NAME))
6510 {
6511 ret.value = finish_label_address_expr
c2255bc4 6512 (c_parser_peek_token (parser)->value, op_loc);
27bf414c 6513 c_parser_consume_token (parser);
27bf414c
JM
6514 }
6515 else
6516 {
6517 c_parser_error (parser, "expected identifier");
6518 ret.value = error_mark_node;
27bf414c 6519 }
43f6dfd3 6520 return ret;
27bf414c
JM
6521 case CPP_KEYWORD:
6522 switch (c_parser_peek_token (parser)->keyword)
6523 {
6524 case RID_SIZEOF:
6525 return c_parser_sizeof_expression (parser);
6526 case RID_ALIGNOF:
6527 return c_parser_alignof_expression (parser);
6528 case RID_EXTENSION:
6529 c_parser_consume_token (parser);
6530 ext = disable_extension_diagnostics ();
6531 ret = c_parser_cast_expression (parser, NULL);
6532 restore_extension_diagnostics (ext);
6533 return ret;
6534 case RID_REALPART:
6535 c_parser_consume_token (parser);
c2255bc4 6536 exp_loc = c_parser_peek_token (parser)->location;
46bdb9cf 6537 op = c_parser_cast_expression (parser, NULL);
c2255bc4
AH
6538 op = default_function_array_conversion (exp_loc, op);
6539 return parser_build_unary_op (op_loc, REALPART_EXPR, op);
27bf414c
JM
6540 case RID_IMAGPART:
6541 c_parser_consume_token (parser);
c2255bc4 6542 exp_loc = c_parser_peek_token (parser)->location;
46bdb9cf 6543 op = c_parser_cast_expression (parser, NULL);
c2255bc4
AH
6544 op = default_function_array_conversion (exp_loc, op);
6545 return parser_build_unary_op (op_loc, IMAGPART_EXPR, op);
0a35513e
AH
6546 case RID_TRANSACTION_ATOMIC:
6547 case RID_TRANSACTION_RELAXED:
6548 return c_parser_transaction_expression (parser,
6549 c_parser_peek_token (parser)->keyword);
27bf414c
JM
6550 default:
6551 return c_parser_postfix_expression (parser);
6552 }
6553 default:
6554 return c_parser_postfix_expression (parser);
6555 }
6556}
6557
6558/* Parse a sizeof expression. */
6559
6560static struct c_expr
6561c_parser_sizeof_expression (c_parser *parser)
6562{
6563 struct c_expr expr;
c7412148 6564 location_t expr_loc;
27bf414c
JM
6565 gcc_assert (c_parser_next_token_is_keyword (parser, RID_SIZEOF));
6566 c_parser_consume_token (parser);
7d882b83 6567 c_inhibit_evaluation_warnings++;
27bf414c
JM
6568 in_sizeof++;
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 sizeof ( type-name ) or sizeof unary-expression
6573 starting with a compound literal. */
6574 struct c_type_name *type_name;
6575 c_parser_consume_token (parser);
c7412148 6576 expr_loc = c_parser_peek_token (parser)->location;
27bf414c
JM
6577 type_name = c_parser_type_name (parser);
6578 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
6579 if (type_name == NULL)
6580 {
6581 struct c_expr ret;
7d882b83 6582 c_inhibit_evaluation_warnings--;
27bf414c
JM
6583 in_sizeof--;
6584 ret.value = error_mark_node;
6585 ret.original_code = ERROR_MARK;
6866c6e8 6586 ret.original_type = NULL;
27bf414c
JM
6587 return ret;
6588 }
6589 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
6590 {
4bd2511b
JL
6591 expr = c_parser_postfix_expression_after_paren_type (parser,
6592 type_name,
6593 expr_loc);
6594 goto sizeof_expr;
27bf414c 6595 }
4bd2511b
JL
6596 /* sizeof ( type-name ). */
6597 c_inhibit_evaluation_warnings--;
6598 in_sizeof--;
6599 return c_expr_sizeof_type (expr_loc, type_name);
27bf414c
JM
6600 }
6601 else
6602 {
c7412148 6603 expr_loc = c_parser_peek_token (parser)->location;
27bf414c 6604 expr = c_parser_unary_expression (parser);
4bd2511b
JL
6605 sizeof_expr:
6606 c_inhibit_evaluation_warnings--;
6607 in_sizeof--;
6608 mark_exp_read (expr.value);
6609 if (TREE_CODE (expr.value) == COMPONENT_REF
6610 && DECL_C_BIT_FIELD (TREE_OPERAND (expr.value, 1)))
6611 error_at (expr_loc, "%<sizeof%> applied to a bit-field");
6612 return c_expr_sizeof_expr (expr_loc, expr);
27bf414c
JM
6613 }
6614}
6615
6616/* Parse an alignof expression. */
6617
6618static struct c_expr
6619c_parser_alignof_expression (c_parser *parser)
6620{
6621 struct c_expr expr;
c2255bc4 6622 location_t loc = c_parser_peek_token (parser)->location;
d19fa6b5 6623 tree alignof_spelling = c_parser_peek_token (parser)->value;
27bf414c 6624 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ALIGNOF));
296674db
JM
6625 bool is_c11_alignof = strcmp (IDENTIFIER_POINTER (alignof_spelling),
6626 "_Alignof") == 0;
d19fa6b5 6627 /* A diagnostic is not required for the use of this identifier in
48b0b196 6628 the implementation namespace; only diagnose it for the C11
d19fa6b5 6629 spelling because of existing code using the other spellings. */
35aff4fb 6630 if (is_c11_alignof)
d19fa6b5
JM
6631 {
6632 if (flag_isoc99)
35aff4fb
MP
6633 pedwarn_c99 (loc, OPT_Wpedantic, "ISO C99 does not support %qE",
6634 alignof_spelling);
d19fa6b5 6635 else
35aff4fb
MP
6636 pedwarn_c99 (loc, OPT_Wpedantic, "ISO C90 does not support %qE",
6637 alignof_spelling);
d19fa6b5 6638 }
27bf414c 6639 c_parser_consume_token (parser);
7d882b83 6640 c_inhibit_evaluation_warnings++;
27bf414c
JM
6641 in_alignof++;
6642 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
6643 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
6644 {
6645 /* Either __alignof__ ( type-name ) or __alignof__
6646 unary-expression starting with a compound literal. */
24b97832 6647 location_t loc;
27bf414c
JM
6648 struct c_type_name *type_name;
6649 struct c_expr ret;
6650 c_parser_consume_token (parser);
24b97832 6651 loc = c_parser_peek_token (parser)->location;
27bf414c
JM
6652 type_name = c_parser_type_name (parser);
6653 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
6654 if (type_name == NULL)
6655 {
6656 struct c_expr ret;
7d882b83 6657 c_inhibit_evaluation_warnings--;
27bf414c
JM
6658 in_alignof--;
6659 ret.value = error_mark_node;
6660 ret.original_code = ERROR_MARK;
6866c6e8 6661 ret.original_type = NULL;
27bf414c
JM
6662 return ret;
6663 }
6664 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
6665 {
6666 expr = c_parser_postfix_expression_after_paren_type (parser,
24b97832
ILT
6667 type_name,
6668 loc);
27bf414c
JM
6669 goto alignof_expr;
6670 }
6671 /* alignof ( type-name ). */
7d882b83 6672 c_inhibit_evaluation_warnings--;
27bf414c 6673 in_alignof--;
296674db
JM
6674 ret.value = c_sizeof_or_alignof_type (loc, groktypename (type_name,
6675 NULL, NULL),
6676 false, is_c11_alignof, 1);
27bf414c 6677 ret.original_code = ERROR_MARK;
6866c6e8 6678 ret.original_type = NULL;
27bf414c
JM
6679 return ret;
6680 }
6681 else
6682 {
6683 struct c_expr ret;
6684 expr = c_parser_unary_expression (parser);
6685 alignof_expr:
ebfbbdc5 6686 mark_exp_read (expr.value);
7d882b83 6687 c_inhibit_evaluation_warnings--;
27bf414c 6688 in_alignof--;
c1771a20 6689 pedwarn (loc, OPT_Wpedantic, "ISO C does not allow %<%E (expression)%>",
d19fa6b5 6690 alignof_spelling);
c2255bc4 6691 ret.value = c_alignof_expr (loc, expr.value);
27bf414c 6692 ret.original_code = ERROR_MARK;
6866c6e8 6693 ret.original_type = NULL;
27bf414c
JM
6694 return ret;
6695 }
6696}
6697
f90e8e2e 6698/* Helper function to read arguments of builtins which are interfaces
2205ed25 6699 for the middle-end nodes like COMPLEX_EXPR, VEC_PERM_EXPR and
f90e8e2e
AS
6700 others. The name of the builtin is passed using BNAME parameter.
6701 Function returns true if there were no errors while parsing and
86785830 6702 stores the arguments in CEXPR_LIST. */
f90e8e2e
AS
6703static bool
6704c_parser_get_builtin_args (c_parser *parser, const char *bname,
4e7d7b3d
JJ
6705 vec<c_expr_t, va_gc> **ret_cexpr_list,
6706 bool choose_expr_p)
f90e8e2e
AS
6707{
6708 location_t loc = c_parser_peek_token (parser)->location;
9771b263 6709 vec<c_expr_t, va_gc> *cexpr_list;
86785830 6710 c_expr_t expr;
4e7d7b3d 6711 bool saved_force_folding_builtin_constant_p;
f90e8e2e 6712
86785830 6713 *ret_cexpr_list = NULL;
f90e8e2e
AS
6714 if (c_parser_next_token_is_not (parser, CPP_OPEN_PAREN))
6715 {
6716 error_at (loc, "cannot take address of %qs", bname);
6717 return false;
6718 }
6719
6720 c_parser_consume_token (parser);
6721
6722 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
6723 {
6724 c_parser_consume_token (parser);
6725 return true;
6726 }
86785830 6727
4e7d7b3d
JJ
6728 saved_force_folding_builtin_constant_p
6729 = force_folding_builtin_constant_p;
6730 force_folding_builtin_constant_p |= choose_expr_p;
86785830 6731 expr = c_parser_expr_no_commas (parser, NULL);
4e7d7b3d
JJ
6732 force_folding_builtin_constant_p
6733 = saved_force_folding_builtin_constant_p;
9771b263 6734 vec_alloc (cexpr_list, 1);
b581c05c 6735 vec_safe_push (cexpr_list, expr);
86785830
AS
6736 while (c_parser_next_token_is (parser, CPP_COMMA))
6737 {
6738 c_parser_consume_token (parser);
6739 expr = c_parser_expr_no_commas (parser, NULL);
b581c05c 6740 vec_safe_push (cexpr_list, expr);
86785830 6741 }
f90e8e2e
AS
6742
6743 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
6744 return false;
6745
86785830 6746 *ret_cexpr_list = cexpr_list;
f90e8e2e
AS
6747 return true;
6748}
6749
433cc7b0
TT
6750/* This represents a single generic-association. */
6751
6752struct c_generic_association
6753{
6754 /* The location of the starting token of the type. */
6755 location_t type_location;
fb48aadc 6756 /* The association's type, or NULL_TREE for 'default'. */
433cc7b0
TT
6757 tree type;
6758 /* The association's expression. */
6759 struct c_expr expression;
6760};
6761
6762/* Parse a generic-selection. (C11 6.5.1.1).
6763
6764 generic-selection:
6765 _Generic ( assignment-expression , generic-assoc-list )
6766
6767 generic-assoc-list:
6768 generic-association
6769 generic-assoc-list , generic-association
6770
6771 generic-association:
6772 type-name : assignment-expression
6773 default : assignment-expression
6774*/
6775
6776static struct c_expr
6777c_parser_generic_selection (c_parser *parser)
6778{
6779 vec<c_generic_association> associations = vNULL;
6780 struct c_expr selector, error_expr;
6781 tree selector_type;
6782 struct c_generic_association matched_assoc;
6783 bool match_found = false;
6784 location_t generic_loc, selector_loc;
6785
6786 error_expr.original_code = ERROR_MARK;
6787 error_expr.original_type = NULL;
6788 error_expr.value = error_mark_node;
6789 matched_assoc.type_location = UNKNOWN_LOCATION;
6790 matched_assoc.type = NULL_TREE;
6791 matched_assoc.expression = error_expr;
6792
6793 gcc_assert (c_parser_next_token_is_keyword (parser, RID_GENERIC));
6794 generic_loc = c_parser_peek_token (parser)->location;
6795 c_parser_consume_token (parser);
35aff4fb
MP
6796 if (flag_isoc99)
6797 pedwarn_c99 (generic_loc, OPT_Wpedantic,
433cc7b0 6798 "ISO C99 does not support %<_Generic%>");
35aff4fb
MP
6799 else
6800 pedwarn_c99 (generic_loc, OPT_Wpedantic,
433cc7b0 6801 "ISO C90 does not support %<_Generic%>");
433cc7b0
TT
6802
6803 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6804 return error_expr;
6805
6806 c_inhibit_evaluation_warnings++;
6807 selector_loc = c_parser_peek_token (parser)->location;
6808 selector = c_parser_expr_no_commas (parser, NULL);
6809 selector = default_function_array_conversion (selector_loc, selector);
6810 c_inhibit_evaluation_warnings--;
6811
6812 if (selector.value == error_mark_node)
6813 {
6814 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6815 return selector;
6816 }
6817 selector_type = TREE_TYPE (selector.value);
6818 /* In ISO C terms, rvalues (including the controlling expression of
6819 _Generic) do not have qualified types. */
6820 if (TREE_CODE (selector_type) != ARRAY_TYPE)
6821 selector_type = TYPE_MAIN_VARIANT (selector_type);
6822 /* In ISO C terms, _Noreturn is not part of the type of expressions
6823 such as &abort, but in GCC it is represented internally as a type
6824 qualifier. */
6825 if (FUNCTION_POINTER_TYPE_P (selector_type)
6826 && TYPE_QUALS (TREE_TYPE (selector_type)) != TYPE_UNQUALIFIED)
6827 selector_type
6828 = build_pointer_type (TYPE_MAIN_VARIANT (TREE_TYPE (selector_type)));
6829
6830 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
6831 {
6832 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6833 return error_expr;
6834 }
6835
6836 while (1)
6837 {
6838 struct c_generic_association assoc, *iter;
6839 unsigned int ix;
6840 c_token *token = c_parser_peek_token (parser);
6841
6842 assoc.type_location = token->location;
6843 if (token->type == CPP_KEYWORD && token->keyword == RID_DEFAULT)
6844 {
6845 c_parser_consume_token (parser);
6846 assoc.type = NULL_TREE;
6847 }
6848 else
6849 {
6850 struct c_type_name *type_name;
6851
6852 type_name = c_parser_type_name (parser);
6853 if (type_name == NULL)
6854 {
6855 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6856 goto error_exit;
6857 }
6858 assoc.type = groktypename (type_name, NULL, NULL);
6859 if (assoc.type == error_mark_node)
6860 {
6861 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6862 goto error_exit;
6863 }
6864
6865 if (TREE_CODE (assoc.type) == FUNCTION_TYPE)
6866 error_at (assoc.type_location,
6867 "%<_Generic%> association has function type");
6868 else if (!COMPLETE_TYPE_P (assoc.type))
6869 error_at (assoc.type_location,
6870 "%<_Generic%> association has incomplete type");
6871
6872 if (variably_modified_type_p (assoc.type, NULL_TREE))
6873 error_at (assoc.type_location,
6874 "%<_Generic%> association has "
6875 "variable length type");
6876 }
6877
6878 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
6879 {
6880 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6881 goto error_exit;
6882 }
6883
6884 assoc.expression = c_parser_expr_no_commas (parser, NULL);
6885 if (assoc.expression.value == error_mark_node)
6886 {
6887 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6888 goto error_exit;
6889 }
6890
6891 for (ix = 0; associations.iterate (ix, &iter); ++ix)
6892 {
6893 if (assoc.type == NULL_TREE)
6894 {
6895 if (iter->type == NULL_TREE)
6896 {
6897 error_at (assoc.type_location,
6898 "duplicate %<default%> case in %<_Generic%>");
6899 inform (iter->type_location, "original %<default%> is here");
6900 }
6901 }
6902 else if (iter->type != NULL_TREE)
6903 {
6904 if (comptypes (assoc.type, iter->type))
6905 {
6906 error_at (assoc.type_location,
6907 "%<_Generic%> specifies two compatible types");
6908 inform (iter->type_location, "compatible type is here");
6909 }
6910 }
6911 }
6912
6913 if (assoc.type == NULL_TREE)
6914 {
6915 if (!match_found)
6916 {
6917 matched_assoc = assoc;
6918 match_found = true;
6919 }
6920 }
6921 else if (comptypes (assoc.type, selector_type))
6922 {
6923 if (!match_found || matched_assoc.type == NULL_TREE)
6924 {
6925 matched_assoc = assoc;
6926 match_found = true;
6927 }
6928 else
6929 {
6930 error_at (assoc.type_location,
6931 "%<_Generic> selector matches multiple associations");
6932 inform (matched_assoc.type_location,
6933 "other match is here");
6934 }
6935 }
6936
6937 associations.safe_push (assoc);
6938
6939 if (c_parser_peek_token (parser)->type != CPP_COMMA)
6940 break;
6941 c_parser_consume_token (parser);
6942 }
6943
6944 associations.release ();
6945
6946 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
6947 {
6948 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6949 return error_expr;
6950 }
6951
6952 if (!match_found)
6953 {
6954 error_at (selector_loc, "%<_Generic%> selector of type %qT is not "
6955 "compatible with any association",
6956 selector_type);
6957 return error_expr;
6958 }
6959
6960 return matched_assoc.expression;
6961
6962 error_exit:
6963 associations.release ();
6964 return error_expr;
6965}
f90e8e2e 6966
27bf414c
JM
6967/* Parse a postfix expression (C90 6.3.1-6.3.2, C99 6.5.1-6.5.2).
6968
6969 postfix-expression:
6970 primary-expression
6971 postfix-expression [ expression ]
6972 postfix-expression ( argument-expression-list[opt] )
6973 postfix-expression . identifier
6974 postfix-expression -> identifier
6975 postfix-expression ++
6976 postfix-expression --
6977 ( type-name ) { initializer-list }
6978 ( type-name ) { initializer-list , }
6979
6980 argument-expression-list:
6981 argument-expression
6982 argument-expression-list , argument-expression
6983
6984 primary-expression:
6985 identifier
6986 constant
6987 string-literal
6988 ( expression )
433cc7b0 6989 generic-selection
27bf414c
JM
6990
6991 GNU extensions:
6992
6993 primary-expression:
6994 __func__
6995 (treated as a keyword in GNU C)
6996 __FUNCTION__
6997 __PRETTY_FUNCTION__
6998 ( compound-statement )
6999 __builtin_va_arg ( assignment-expression , type-name )
7000 __builtin_offsetof ( type-name , offsetof-member-designator )
7001 __builtin_choose_expr ( assignment-expression ,
7002 assignment-expression ,
7003 assignment-expression )
7004 __builtin_types_compatible_p ( type-name , type-name )
d4a83c10 7005 __builtin_complex ( assignment-expression , assignment-expression )
f90e8e2e
AS
7006 __builtin_shuffle ( assignment-expression , assignment-expression )
7007 __builtin_shuffle ( assignment-expression ,
7008 assignment-expression ,
7009 assignment-expression, )
27bf414c
JM
7010
7011 offsetof-member-designator:
7012 identifier
7013 offsetof-member-designator . identifier
7014 offsetof-member-designator [ expression ]
7015
7016 Objective-C:
7017
7018 primary-expression:
7019 [ objc-receiver objc-message-args ]
7020 @selector ( objc-selector-arg )
7021 @protocol ( identifier )
7022 @encode ( type-name )
7023 objc-string-literal
bede2adc 7024 Classname . identifier
27bf414c
JM
7025*/
7026
7027static struct c_expr
7028c_parser_postfix_expression (c_parser *parser)
7029{
f90e8e2e 7030 struct c_expr expr, e1;
27bf414c 7031 struct c_type_name *t1, *t2;
c2255bc4 7032 location_t loc = c_parser_peek_token (parser)->location;;
6866c6e8
ILT
7033 expr.original_code = ERROR_MARK;
7034 expr.original_type = NULL;
27bf414c
JM
7035 switch (c_parser_peek_token (parser)->type)
7036 {
7037 case CPP_NUMBER:
754ccf7c 7038 expr.value = c_parser_peek_token (parser)->value;
754ccf7c
JJ
7039 loc = c_parser_peek_token (parser)->location;
7040 c_parser_consume_token (parser);
7041 if (TREE_CODE (expr.value) == FIXED_CST
7042 && !targetm.fixed_point_supported_p ())
7043 {
7044 error_at (loc, "fixed-point types not supported for this target");
7045 expr.value = error_mark_node;
7046 }
7047 break;
27bf414c 7048 case CPP_CHAR:
b6baa67d
KVH
7049 case CPP_CHAR16:
7050 case CPP_CHAR32:
27bf414c
JM
7051 case CPP_WCHAR:
7052 expr.value = c_parser_peek_token (parser)->value;
27bf414c
JM
7053 c_parser_consume_token (parser);
7054 break;
7055 case CPP_STRING:
b6baa67d
KVH
7056 case CPP_STRING16:
7057 case CPP_STRING32:
27bf414c 7058 case CPP_WSTRING:
2c6e3f55 7059 case CPP_UTF8STRING:
27bf414c
JM
7060 expr.value = c_parser_peek_token (parser)->value;
7061 expr.original_code = STRING_CST;
7062 c_parser_consume_token (parser);
7063 break;
7064 case CPP_OBJC_STRING:
7065 gcc_assert (c_dialect_objc ());
7066 expr.value
7067 = objc_build_string_object (c_parser_peek_token (parser)->value);
27bf414c
JM
7068 c_parser_consume_token (parser);
7069 break;
7070 case CPP_NAME:
bede2adc 7071 switch (c_parser_peek_token (parser)->id_kind)
27bf414c 7072 {
bede2adc
NP
7073 case C_ID_ID:
7074 {
7075 tree id = c_parser_peek_token (parser)->value;
7076 c_parser_consume_token (parser);
7077 expr.value = build_external_ref (loc, id,
7078 (c_parser_peek_token (parser)->type
7079 == CPP_OPEN_PAREN),
7080 &expr.original_type);
7081 break;
7082 }
7083 case C_ID_CLASSNAME:
7084 {
7085 /* Here we parse the Objective-C 2.0 Class.name dot
7086 syntax. */
7087 tree class_name = c_parser_peek_token (parser)->value;
7088 tree component;
7089 c_parser_consume_token (parser);
7090 gcc_assert (c_dialect_objc ());
7091 if (!c_parser_require (parser, CPP_DOT, "expected %<.%>"))
7092 {
7093 expr.value = error_mark_node;
7094 break;
7095 }
7096 if (c_parser_next_token_is_not (parser, CPP_NAME))
7097 {
7098 c_parser_error (parser, "expected identifier");
7099 expr.value = error_mark_node;
7100 break;
7101 }
7102 component = c_parser_peek_token (parser)->value;
7103 c_parser_consume_token (parser);
7104 expr.value = objc_build_class_component_ref (class_name,
7105 component);
7106 break;
7107 }
7108 default:
27bf414c
JM
7109 c_parser_error (parser, "expected expression");
7110 expr.value = error_mark_node;
27bf414c
JM
7111 break;
7112 }
27bf414c
JM
7113 break;
7114 case CPP_OPEN_PAREN:
7115 /* A parenthesized expression, statement expression or compound
7116 literal. */
7117 if (c_parser_peek_2nd_token (parser)->type == CPP_OPEN_BRACE)
7118 {
7119 /* A statement expression. */
7120 tree stmt;
c2255bc4 7121 location_t brace_loc;
27bf414c 7122 c_parser_consume_token (parser);
c2255bc4 7123 brace_loc = c_parser_peek_token (parser)->location;
27bf414c 7124 c_parser_consume_token (parser);
38e01f9e 7125 if (!building_stmt_list_p ())
27bf414c 7126 {
c2255bc4 7127 error_at (loc, "braced-group within expression allowed "
3ba09659 7128 "only inside a function");
27bf414c
JM
7129 parser->error = true;
7130 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
7131 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7132 expr.value = error_mark_node;
27bf414c
JM
7133 break;
7134 }
7135 stmt = c_begin_stmt_expr ();
7136 c_parser_compound_statement_nostart (parser);
7137 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7138 "expected %<)%>");
c1771a20 7139 pedwarn (loc, OPT_Wpedantic,
509c9d60 7140 "ISO C forbids braced-groups within expressions");
c2255bc4 7141 expr.value = c_finish_stmt_expr (brace_loc, stmt);
82c3c067 7142 mark_exp_read (expr.value);
27bf414c
JM
7143 }
7144 else if (c_token_starts_typename (c_parser_peek_2nd_token (parser)))
7145 {
7146 /* A compound literal. ??? Can we actually get here rather
7147 than going directly to
7148 c_parser_postfix_expression_after_paren_type from
7149 elsewhere? */
24b97832 7150 location_t loc;
27bf414c
JM
7151 struct c_type_name *type_name;
7152 c_parser_consume_token (parser);
24b97832 7153 loc = c_parser_peek_token (parser)->location;
27bf414c
JM
7154 type_name = c_parser_type_name (parser);
7155 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7156 "expected %<)%>");
7157 if (type_name == NULL)
7158 {
7159 expr.value = error_mark_node;
27bf414c
JM
7160 }
7161 else
7162 expr = c_parser_postfix_expression_after_paren_type (parser,
24b97832
ILT
7163 type_name,
7164 loc);
27bf414c
JM
7165 }
7166 else
7167 {
7168 /* A parenthesized expression. */
7169 c_parser_consume_token (parser);
7170 expr = c_parser_expression (parser);
7171 if (TREE_CODE (expr.value) == MODIFY_EXPR)
7172 TREE_NO_WARNING (expr.value) = 1;
928c19bb
JM
7173 if (expr.original_code != C_MAYBE_CONST_EXPR)
7174 expr.original_code = ERROR_MARK;
6866c6e8 7175 /* Don't change EXPR.ORIGINAL_TYPE. */
27bf414c
JM
7176 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7177 "expected %<)%>");
7178 }
7179 break;
7180 case CPP_KEYWORD:
7181 switch (c_parser_peek_token (parser)->keyword)
7182 {
7183 case RID_FUNCTION_NAME:
6dc99c33
MP
7184 pedwarn (loc, OPT_Wpedantic, "ISO C does not support "
7185 "%<__FUNCTION__%> predefined identifier");
7186 expr.value = fname_decl (loc,
7187 c_parser_peek_token (parser)->keyword,
7188 c_parser_peek_token (parser)->value);
7189 c_parser_consume_token (parser);
7190 break;
27bf414c 7191 case RID_PRETTY_FUNCTION_NAME:
6dc99c33
MP
7192 pedwarn (loc, OPT_Wpedantic, "ISO C does not support "
7193 "%<__PRETTY_FUNCTION__%> predefined identifier");
7194 expr.value = fname_decl (loc,
7195 c_parser_peek_token (parser)->keyword,
7196 c_parser_peek_token (parser)->value);
7197 c_parser_consume_token (parser);
7198 break;
27bf414c 7199 case RID_C99_FUNCTION_NAME:
6dc99c33
MP
7200 pedwarn_c90 (loc, OPT_Wpedantic, "ISO C90 does not support "
7201 "%<__func__%> predefined identifier");
c2255bc4 7202 expr.value = fname_decl (loc,
3ba09659 7203 c_parser_peek_token (parser)->keyword,
27bf414c 7204 c_parser_peek_token (parser)->value);
27bf414c
JM
7205 c_parser_consume_token (parser);
7206 break;
7207 case RID_VA_ARG:
7208 c_parser_consume_token (parser);
7209 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7210 {
7211 expr.value = error_mark_node;
27bf414c
JM
7212 break;
7213 }
7214 e1 = c_parser_expr_no_commas (parser, NULL);
ebfbbdc5 7215 mark_exp_read (e1.value);
928c19bb 7216 e1.value = c_fully_fold (e1.value, false, NULL);
27bf414c
JM
7217 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
7218 {
7219 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7220 expr.value = error_mark_node;
27bf414c
JM
7221 break;
7222 }
72b5577d 7223 loc = c_parser_peek_token (parser)->location;
27bf414c
JM
7224 t1 = c_parser_type_name (parser);
7225 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7226 "expected %<)%>");
7227 if (t1 == NULL)
7228 {
7229 expr.value = error_mark_node;
27bf414c
JM
7230 }
7231 else
7232 {
928c19bb 7233 tree type_expr = NULL_TREE;
c2255bc4
AH
7234 expr.value = c_build_va_arg (loc, e1.value,
7235 groktypename (t1, &type_expr, NULL));
928c19bb
JM
7236 if (type_expr)
7237 {
7238 expr.value = build2 (C_MAYBE_CONST_EXPR,
7239 TREE_TYPE (expr.value), type_expr,
7240 expr.value);
7241 C_MAYBE_CONST_EXPR_NON_CONST (expr.value) = true;
7242 }
27bf414c
JM
7243 }
7244 break;
7245 case RID_OFFSETOF:
7246 c_parser_consume_token (parser);
7247 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7248 {
7249 expr.value = error_mark_node;
27bf414c
JM
7250 break;
7251 }
7252 t1 = c_parser_type_name (parser);
7253 if (t1 == NULL)
29ce73cb 7254 parser->error = true;
27bf414c 7255 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
29ce73cb
PB
7256 gcc_assert (parser->error);
7257 if (parser->error)
27bf414c
JM
7258 {
7259 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7260 expr.value = error_mark_node;
27bf414c
JM
7261 break;
7262 }
29ce73cb 7263
27bf414c 7264 {
928c19bb 7265 tree type = groktypename (t1, NULL, NULL);
27bf414c
JM
7266 tree offsetof_ref;
7267 if (type == error_mark_node)
7268 offsetof_ref = error_mark_node;
7269 else
c2255bc4
AH
7270 {
7271 offsetof_ref = build1 (INDIRECT_REF, type, null_pointer_node);
7272 SET_EXPR_LOCATION (offsetof_ref, loc);
7273 }
27bf414c
JM
7274 /* Parse the second argument to __builtin_offsetof. We
7275 must have one identifier, and beyond that we want to
7276 accept sub structure and sub array references. */
7277 if (c_parser_next_token_is (parser, CPP_NAME))
7278 {
7279 offsetof_ref = build_component_ref
c2255bc4 7280 (loc, offsetof_ref, c_parser_peek_token (parser)->value);
27bf414c
JM
7281 c_parser_consume_token (parser);
7282 while (c_parser_next_token_is (parser, CPP_DOT)
7283 || c_parser_next_token_is (parser,
634b5df5
JJ
7284 CPP_OPEN_SQUARE)
7285 || c_parser_next_token_is (parser,
7286 CPP_DEREF))
27bf414c 7287 {
634b5df5
JJ
7288 if (c_parser_next_token_is (parser, CPP_DEREF))
7289 {
7290 loc = c_parser_peek_token (parser)->location;
c2255bc4
AH
7291 offsetof_ref = build_array_ref (loc,
7292 offsetof_ref,
7293 integer_zero_node);
634b5df5
JJ
7294 goto do_dot;
7295 }
7296 else if (c_parser_next_token_is (parser, CPP_DOT))
27bf414c 7297 {
634b5df5 7298 do_dot:
27bf414c
JM
7299 c_parser_consume_token (parser);
7300 if (c_parser_next_token_is_not (parser,
7301 CPP_NAME))
7302 {
7303 c_parser_error (parser, "expected identifier");
7304 break;
7305 }
7306 offsetof_ref = build_component_ref
c2255bc4 7307 (loc, offsetof_ref,
27bf414c
JM
7308 c_parser_peek_token (parser)->value);
7309 c_parser_consume_token (parser);
7310 }
7311 else
7312 {
267bac10 7313 struct c_expr ce;
27bf414c 7314 tree idx;
6a3799eb 7315 loc = c_parser_peek_token (parser)->location;
27bf414c 7316 c_parser_consume_token (parser);
267bac10
JM
7317 ce = c_parser_expression (parser);
7318 ce = convert_lvalue_to_rvalue (loc, ce, false, false);
7319 idx = ce.value;
928c19bb 7320 idx = c_fully_fold (idx, false, NULL);
27bf414c
JM
7321 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
7322 "expected %<]%>");
c2255bc4 7323 offsetof_ref = build_array_ref (loc, offsetof_ref, idx);
27bf414c
JM
7324 }
7325 }
7326 }
7327 else
7328 c_parser_error (parser, "expected identifier");
7329 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7330 "expected %<)%>");
cf9e9959 7331 expr.value = fold_offsetof (offsetof_ref);
27bf414c
JM
7332 }
7333 break;
7334 case RID_CHOOSE_EXPR:
27bf414c 7335 {
9771b263 7336 vec<c_expr_t, va_gc> *cexpr_list;
86785830
AS
7337 c_expr_t *e1_p, *e2_p, *e3_p;
7338 tree c;
27bf414c 7339
f90e8e2e
AS
7340 c_parser_consume_token (parser);
7341 if (!c_parser_get_builtin_args (parser,
7342 "__builtin_choose_expr",
4e7d7b3d 7343 &cexpr_list, true))
f90e8e2e
AS
7344 {
7345 expr.value = error_mark_node;
7346 break;
7347 }
7348
9771b263 7349 if (vec_safe_length (cexpr_list) != 3)
f90e8e2e
AS
7350 {
7351 error_at (loc, "wrong number of arguments to "
7352 "%<__builtin_choose_expr%>");
7353 expr.value = error_mark_node;
7354 break;
7355 }
86785830 7356
9771b263
DN
7357 e1_p = &(*cexpr_list)[0];
7358 e2_p = &(*cexpr_list)[1];
7359 e3_p = &(*cexpr_list)[2];
86785830
AS
7360
7361 c = e1_p->value;
7362 mark_exp_read (e2_p->value);
7363 mark_exp_read (e3_p->value);
928c19bb
JM
7364 if (TREE_CODE (c) != INTEGER_CST
7365 || !INTEGRAL_TYPE_P (TREE_TYPE (c)))
3ba09659
AH
7366 error_at (loc,
7367 "first argument to %<__builtin_choose_expr%> not"
7368 " a constant");
928c19bb 7369 constant_expression_warning (c);
86785830 7370 expr = integer_zerop (c) ? *e3_p : *e2_p;
f90e8e2e 7371 break;
27bf414c 7372 }
27bf414c
JM
7373 case RID_TYPES_COMPATIBLE_P:
7374 c_parser_consume_token (parser);
7375 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7376 {
7377 expr.value = error_mark_node;
27bf414c
JM
7378 break;
7379 }
7380 t1 = c_parser_type_name (parser);
7381 if (t1 == NULL)
7382 {
7383 expr.value = error_mark_node;
27bf414c
JM
7384 break;
7385 }
7386 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
7387 {
7388 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7389 expr.value = error_mark_node;
27bf414c
JM
7390 break;
7391 }
7392 t2 = c_parser_type_name (parser);
7393 if (t2 == NULL)
7394 {
7395 expr.value = error_mark_node;
27bf414c
JM
7396 break;
7397 }
7398 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7399 "expected %<)%>");
7400 {
7401 tree e1, e2;
9abfe986
AP
7402 e1 = groktypename (t1, NULL, NULL);
7403 e2 = groktypename (t2, NULL, NULL);
7404 if (e1 == error_mark_node || e2 == error_mark_node)
7405 {
7406 expr.value = error_mark_node;
7407 break;
7408 }
27bf414c 7409
9abfe986
AP
7410 e1 = TYPE_MAIN_VARIANT (e1);
7411 e2 = TYPE_MAIN_VARIANT (e2);
27bf414c 7412
9a9d280e
AS
7413 expr.value
7414 = comptypes (e1, e2) ? integer_one_node : integer_zero_node;
27bf414c
JM
7415 }
7416 break;
74893f25
RH
7417 case RID_BUILTIN_CALL_WITH_STATIC_CHAIN:
7418 {
7419 vec<c_expr_t, va_gc> *cexpr_list;
7420 c_expr_t *e2_p;
7421 tree chain_value;
7422
7423 c_parser_consume_token (parser);
7424 if (!c_parser_get_builtin_args (parser,
7425 "__builtin_call_with_static_chain",
7426 &cexpr_list, false))
7427 {
7428 expr.value = error_mark_node;
7429 break;
7430 }
7431 if (vec_safe_length (cexpr_list) != 2)
7432 {
7433 error_at (loc, "wrong number of arguments to "
7434 "%<__builtin_call_with_static_chain%>");
7435 expr.value = error_mark_node;
7436 break;
7437 }
7438
7439 expr = (*cexpr_list)[0];
7440 e2_p = &(*cexpr_list)[1];
7441 *e2_p = convert_lvalue_to_rvalue (loc, *e2_p, true, true);
7442 chain_value = e2_p->value;
7443 mark_exp_read (chain_value);
7444
7445 if (TREE_CODE (expr.value) != CALL_EXPR)
7446 error_at (loc, "first argument to "
7447 "%<__builtin_call_with_static_chain%> "
7448 "must be a call expression");
7449 else if (TREE_CODE (TREE_TYPE (chain_value)) != POINTER_TYPE)
7450 error_at (loc, "second argument to "
7451 "%<__builtin_call_with_static_chain%> "
7452 "must be a pointer type");
7453 else
7454 CALL_EXPR_STATIC_CHAIN (expr.value) = chain_value;
7455 break;
7456 }
d4a83c10 7457 case RID_BUILTIN_COMPLEX:
86785830 7458 {
9771b263 7459 vec<c_expr_t, va_gc> *cexpr_list;
86785830
AS
7460 c_expr_t *e1_p, *e2_p;
7461
f90e8e2e
AS
7462 c_parser_consume_token (parser);
7463 if (!c_parser_get_builtin_args (parser,
7464 "__builtin_complex",
4e7d7b3d 7465 &cexpr_list, false))
f90e8e2e
AS
7466 {
7467 expr.value = error_mark_node;
7468 break;
7469 }
7470
9771b263 7471 if (vec_safe_length (cexpr_list) != 2)
f90e8e2e
AS
7472 {
7473 error_at (loc, "wrong number of arguments to "
7474 "%<__builtin_complex%>");
7475 expr.value = error_mark_node;
7476 break;
7477 }
86785830 7478
9771b263
DN
7479 e1_p = &(*cexpr_list)[0];
7480 e2_p = &(*cexpr_list)[1];
86785830 7481
267bac10 7482 *e1_p = convert_lvalue_to_rvalue (loc, *e1_p, true, true);
86785830
AS
7483 if (TREE_CODE (e1_p->value) == EXCESS_PRECISION_EXPR)
7484 e1_p->value = convert (TREE_TYPE (e1_p->value),
7485 TREE_OPERAND (e1_p->value, 0));
267bac10 7486 *e2_p = convert_lvalue_to_rvalue (loc, *e2_p, true, true);
86785830
AS
7487 if (TREE_CODE (e2_p->value) == EXCESS_PRECISION_EXPR)
7488 e2_p->value = convert (TREE_TYPE (e2_p->value),
7489 TREE_OPERAND (e2_p->value, 0));
7490 if (!SCALAR_FLOAT_TYPE_P (TREE_TYPE (e1_p->value))
7491 || DECIMAL_FLOAT_TYPE_P (TREE_TYPE (e1_p->value))
7492 || !SCALAR_FLOAT_TYPE_P (TREE_TYPE (e2_p->value))
7493 || DECIMAL_FLOAT_TYPE_P (TREE_TYPE (e2_p->value)))
f90e8e2e
AS
7494 {
7495 error_at (loc, "%<__builtin_complex%> operand "
7496 "not of real binary floating-point type");
7497 expr.value = error_mark_node;
7498 break;
7499 }
86785830
AS
7500 if (TYPE_MAIN_VARIANT (TREE_TYPE (e1_p->value))
7501 != TYPE_MAIN_VARIANT (TREE_TYPE (e2_p->value)))
f90e8e2e
AS
7502 {
7503 error_at (loc,
7504 "%<__builtin_complex%> operands of different types");
7505 expr.value = error_mark_node;
7506 break;
7507 }
f3bede71
MP
7508 pedwarn_c90 (loc, OPT_Wpedantic,
7509 "ISO C90 does not support complex types");
f90e8e2e 7510 expr.value = build2 (COMPLEX_EXPR,
86785830
AS
7511 build_complex_type
7512 (TYPE_MAIN_VARIANT
7513 (TREE_TYPE (e1_p->value))),
7514 e1_p->value, e2_p->value);
f90e8e2e
AS
7515 break;
7516 }
7517 case RID_BUILTIN_SHUFFLE:
7518 {
9771b263 7519 vec<c_expr_t, va_gc> *cexpr_list;
9243c51d
JJ
7520 unsigned int i;
7521 c_expr_t *p;
86785830 7522
f90e8e2e
AS
7523 c_parser_consume_token (parser);
7524 if (!c_parser_get_builtin_args (parser,
7525 "__builtin_shuffle",
4e7d7b3d 7526 &cexpr_list, false))
f90e8e2e
AS
7527 {
7528 expr.value = error_mark_node;
7529 break;
7530 }
7531
9771b263 7532 FOR_EACH_VEC_SAFE_ELT (cexpr_list, i, p)
267bac10 7533 *p = convert_lvalue_to_rvalue (loc, *p, true, true);
9243c51d 7534
9771b263 7535 if (vec_safe_length (cexpr_list) == 2)
86785830 7536 expr.value =
2205ed25 7537 c_build_vec_perm_expr
9771b263
DN
7538 (loc, (*cexpr_list)[0].value,
7539 NULL_TREE, (*cexpr_list)[1].value);
86785830 7540
9771b263 7541 else if (vec_safe_length (cexpr_list) == 3)
86785830 7542 expr.value =
2205ed25 7543 c_build_vec_perm_expr
9771b263
DN
7544 (loc, (*cexpr_list)[0].value,
7545 (*cexpr_list)[1].value,
7546 (*cexpr_list)[2].value);
f90e8e2e
AS
7547 else
7548 {
7549 error_at (loc, "wrong number of arguments to "
7550 "%<__builtin_shuffle%>");
7551 expr.value = error_mark_node;
7552 }
7553 break;
7554 }
27bf414c
JM
7555 case RID_AT_SELECTOR:
7556 gcc_assert (c_dialect_objc ());
7557 c_parser_consume_token (parser);
7558 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7559 {
7560 expr.value = error_mark_node;
27bf414c
JM
7561 break;
7562 }
7563 {
7564 tree sel = c_parser_objc_selector_arg (parser);
7565 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7566 "expected %<)%>");
c2255bc4 7567 expr.value = objc_build_selector_expr (loc, sel);
27bf414c
JM
7568 }
7569 break;
7570 case RID_AT_PROTOCOL:
7571 gcc_assert (c_dialect_objc ());
7572 c_parser_consume_token (parser);
7573 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7574 {
7575 expr.value = error_mark_node;
27bf414c
JM
7576 break;
7577 }
7578 if (c_parser_next_token_is_not (parser, CPP_NAME))
7579 {
7580 c_parser_error (parser, "expected identifier");
7581 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7582 expr.value = error_mark_node;
27bf414c
JM
7583 break;
7584 }
7585 {
7586 tree id = c_parser_peek_token (parser)->value;
7587 c_parser_consume_token (parser);
7588 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7589 "expected %<)%>");
7590 expr.value = objc_build_protocol_expr (id);
27bf414c
JM
7591 }
7592 break;
7593 case RID_AT_ENCODE:
7594 /* Extension to support C-structures in the archiver. */
7595 gcc_assert (c_dialect_objc ());
7596 c_parser_consume_token (parser);
7597 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7598 {
7599 expr.value = error_mark_node;
27bf414c
JM
7600 break;
7601 }
7602 t1 = c_parser_type_name (parser);
7603 if (t1 == NULL)
7604 {
7605 expr.value = error_mark_node;
27bf414c
JM
7606 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7607 break;
7608 }
7609 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7610 "expected %<)%>");
7611 {
928c19bb 7612 tree type = groktypename (t1, NULL, NULL);
27bf414c 7613 expr.value = objc_build_encode_expr (type);
27bf414c
JM
7614 }
7615 break;
433cc7b0
TT
7616 case RID_GENERIC:
7617 expr = c_parser_generic_selection (parser);
7618 break;
939b37da
BI
7619 case RID_CILK_SPAWN:
7620 c_parser_consume_token (parser);
b72271b9 7621 if (!flag_cilkplus)
939b37da
BI
7622 {
7623 error_at (loc, "-fcilkplus must be enabled to use "
7624 "%<_Cilk_spawn%>");
7625 expr = c_parser_postfix_expression (parser);
7626 expr.value = error_mark_node;
7627 }
9a74f20c 7628 else if (c_parser_peek_token (parser)->keyword == RID_CILK_SPAWN)
939b37da
BI
7629 {
7630 error_at (loc, "consecutive %<_Cilk_spawn%> keywords "
7631 "are not permitted");
7632 /* Now flush out all the _Cilk_spawns. */
7633 while (c_parser_peek_token (parser)->keyword == RID_CILK_SPAWN)
7634 c_parser_consume_token (parser);
7635 expr = c_parser_postfix_expression (parser);
7636 }
7637 else
7638 {
7639 expr = c_parser_postfix_expression (parser);
7640 expr.value = build_cilk_spawn (loc, expr.value);
7641 }
7642 break;
27bf414c
JM
7643 default:
7644 c_parser_error (parser, "expected expression");
7645 expr.value = error_mark_node;
27bf414c
JM
7646 break;
7647 }
7648 break;
7649 case CPP_OPEN_SQUARE:
7650 if (c_dialect_objc ())
7651 {
7652 tree receiver, args;
7653 c_parser_consume_token (parser);
7654 receiver = c_parser_objc_receiver (parser);
7655 args = c_parser_objc_message_args (parser);
7656 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
7657 "expected %<]%>");
eb345401 7658 expr.value = objc_build_message_expr (receiver, args);
27bf414c
JM
7659 break;
7660 }
7661 /* Else fall through to report error. */
7662 default:
7663 c_parser_error (parser, "expected expression");
7664 expr.value = error_mark_node;
27bf414c
JM
7665 break;
7666 }
c2255bc4 7667 return c_parser_postfix_expression_after_primary (parser, loc, expr);
27bf414c
JM
7668}
7669
7670/* Parse a postfix expression after a parenthesized type name: the
7671 brace-enclosed initializer of a compound literal, possibly followed
7672 by some postfix operators. This is separate because it is not
7673 possible to tell until after the type name whether a cast
7674 expression has a cast or a compound literal, or whether the operand
7675 of sizeof is a parenthesized type name or starts with a compound
24b97832
ILT
7676 literal. TYPE_LOC is the location where TYPE_NAME starts--the
7677 location of the first token after the parentheses around the type
7678 name. */
27bf414c
JM
7679
7680static struct c_expr
7681c_parser_postfix_expression_after_paren_type (c_parser *parser,
24b97832
ILT
7682 struct c_type_name *type_name,
7683 location_t type_loc)
27bf414c
JM
7684{
7685 tree type;
7686 struct c_expr init;
928c19bb 7687 bool non_const;
27bf414c 7688 struct c_expr expr;
c7412148 7689 location_t start_loc;
928c19bb
JM
7690 tree type_expr = NULL_TREE;
7691 bool type_expr_const = true;
c2255bc4 7692 check_compound_literal_type (type_loc, type_name);
27bf414c 7693 start_init (NULL_TREE, NULL, 0);
928c19bb 7694 type = groktypename (type_name, &type_expr, &type_expr_const);
c7412148 7695 start_loc = c_parser_peek_token (parser)->location;
85cad37c 7696 if (type != error_mark_node && C_TYPE_VARIABLE_SIZE (type))
27bf414c 7697 {
24b97832 7698 error_at (type_loc, "compound literal has variable size");
27bf414c
JM
7699 type = error_mark_node;
7700 }
7701 init = c_parser_braced_init (parser, type, false);
7702 finish_init ();
d033409e 7703 maybe_warn_string_init (type_loc, type, init);
27bf414c 7704
36c5e70a
BE
7705 if (type != error_mark_node
7706 && !ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (type))
7707 && current_function_decl)
7708 {
7709 error ("compound literal qualified by address-space qualifier");
7710 type = error_mark_node;
7711 }
7712
f3bede71 7713 pedwarn_c90 (start_loc, OPT_Wpedantic, "ISO C90 forbids compound literals");
928c19bb
JM
7714 non_const = ((init.value && TREE_CODE (init.value) == CONSTRUCTOR)
7715 ? CONSTRUCTOR_NON_CONST (init.value)
7716 : init.original_code == C_MAYBE_CONST_EXPR);
7717 non_const |= !type_expr_const;
c2255bc4 7718 expr.value = build_compound_literal (start_loc, type, init.value, non_const);
27bf414c 7719 expr.original_code = ERROR_MARK;
6866c6e8 7720 expr.original_type = NULL;
928c19bb
JM
7721 if (type_expr)
7722 {
7723 if (TREE_CODE (expr.value) == C_MAYBE_CONST_EXPR)
7724 {
7725 gcc_assert (C_MAYBE_CONST_EXPR_PRE (expr.value) == NULL_TREE);
7726 C_MAYBE_CONST_EXPR_PRE (expr.value) = type_expr;
7727 }
7728 else
7729 {
7730 gcc_assert (!non_const);
7731 expr.value = build2 (C_MAYBE_CONST_EXPR, type,
7732 type_expr, expr.value);
7733 }
7734 }
c2255bc4 7735 return c_parser_postfix_expression_after_primary (parser, start_loc, expr);
27bf414c
JM
7736}
7737
1a4049e7
JJ
7738/* Callback function for sizeof_pointer_memaccess_warning to compare
7739 types. */
7740
7741static bool
7742sizeof_ptr_memacc_comptypes (tree type1, tree type2)
7743{
7744 return comptypes (type1, type2) == 1;
7745}
7746
27bf414c 7747/* Parse a postfix expression after the initial primary or compound
c2255bc4
AH
7748 literal; that is, parse a series of postfix operators.
7749
7750 EXPR_LOC is the location of the primary expression. */
27bf414c
JM
7751
7752static struct c_expr
7753c_parser_postfix_expression_after_primary (c_parser *parser,
c2255bc4 7754 location_t expr_loc,
27bf414c
JM
7755 struct c_expr expr)
7756{
928c19bb 7757 struct c_expr orig_expr;
bbbbb16a 7758 tree ident, idx;
3a785c97
JJ
7759 location_t sizeof_arg_loc[3];
7760 tree sizeof_arg[3];
b108f48f 7761 unsigned int literal_zero_mask;
3a785c97 7762 unsigned int i;
9771b263 7763 vec<tree, va_gc> *exprlist;
2ee028f1 7764 vec<tree, va_gc> *origtypes = NULL;
81e5eca8
MP
7765 vec<location_t> arg_loc = vNULL;
7766
27bf414c
JM
7767 while (true)
7768 {
c2255bc4 7769 location_t op_loc = c_parser_peek_token (parser)->location;
27bf414c
JM
7770 switch (c_parser_peek_token (parser)->type)
7771 {
7772 case CPP_OPEN_SQUARE:
7773 /* Array reference. */
7774 c_parser_consume_token (parser);
b72271b9 7775 if (flag_cilkplus
36536d79
BI
7776 && c_parser_peek_token (parser)->type == CPP_COLON)
7777 /* If we are here, then we have something like this:
7778 Array [ : ]
7779 */
7780 expr.value = c_parser_array_notation (expr_loc, parser, NULL_TREE,
7781 expr.value);
7782 else
7783 {
7784 idx = c_parser_expression (parser).value;
7785 /* Here we have 3 options:
7786 1. Array [EXPR] -- Normal Array call.
7787 2. Array [EXPR : EXPR] -- Array notation without stride.
7788 3. Array [EXPR : EXPR : EXPR] -- Array notation with stride.
7789
7790 For 1, we just handle it just like a normal array expression.
7791 For 2 and 3 we handle it like we handle array notations. The
7792 idx value we have above becomes the initial/start index.
7793 */
b72271b9 7794 if (flag_cilkplus
36536d79
BI
7795 && c_parser_peek_token (parser)->type == CPP_COLON)
7796 expr.value = c_parser_array_notation (expr_loc, parser, idx,
7797 expr.value);
7798 else
7799 {
7800 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
7801 "expected %<]%>");
7802 expr.value = build_array_ref (op_loc, expr.value, idx);
7803 }
7804 }
27bf414c 7805 expr.original_code = ERROR_MARK;
6866c6e8 7806 expr.original_type = NULL;
27bf414c
JM
7807 break;
7808 case CPP_OPEN_PAREN:
7809 /* Function call. */
7810 c_parser_consume_token (parser);
3a785c97
JJ
7811 for (i = 0; i < 3; i++)
7812 {
7813 sizeof_arg[i] = NULL_TREE;
7814 sizeof_arg_loc[i] = UNKNOWN_LOCATION;
7815 }
b108f48f 7816 literal_zero_mask = 0;
27bf414c 7817 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
bbbbb16a 7818 exprlist = NULL;
27bf414c 7819 else
1a4049e7 7820 exprlist = c_parser_expr_list (parser, true, false, &origtypes,
81e5eca8 7821 sizeof_arg_loc, sizeof_arg,
b108f48f 7822 &arg_loc, &literal_zero_mask);
27bf414c
JM
7823 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7824 "expected %<)%>");
928c19bb 7825 orig_expr = expr;
ebfbbdc5 7826 mark_exp_read (expr.value);
3a785c97
JJ
7827 if (warn_sizeof_pointer_memaccess)
7828 sizeof_pointer_memaccess_warning (sizeof_arg_loc,
1a4049e7 7829 expr.value, exprlist,
3a785c97 7830 sizeof_arg,
1a4049e7 7831 sizeof_ptr_memacc_comptypes);
b108f48f
JJ
7832 if (warn_memset_transposed_args
7833 && TREE_CODE (expr.value) == FUNCTION_DECL
7834 && DECL_BUILT_IN_CLASS (expr.value) == BUILT_IN_NORMAL
7835 && DECL_FUNCTION_CODE (expr.value) == BUILT_IN_MEMSET
7836 && vec_safe_length (exprlist) == 3
7837 && integer_zerop ((*exprlist)[2])
7838 && (literal_zero_mask & (1 << 2)) != 0
7839 && (!integer_zerop ((*exprlist)[1])
7840 || (literal_zero_mask & (1 << 1)) == 0))
7841 warning_at (expr_loc, OPT_Wmemset_transposed_args,
7842 "%<memset%> used with constant zero length parameter; "
7843 "this could be due to transposed parameters");
7844
8edbfaa6
JJ
7845 expr.value
7846 = c_build_function_call_vec (expr_loc, arg_loc, expr.value,
7847 exprlist, origtypes);
27bf414c 7848 expr.original_code = ERROR_MARK;
928c19bb
JM
7849 if (TREE_CODE (expr.value) == INTEGER_CST
7850 && TREE_CODE (orig_expr.value) == FUNCTION_DECL
7851 && DECL_BUILT_IN_CLASS (orig_expr.value) == BUILT_IN_NORMAL
7852 && DECL_FUNCTION_CODE (orig_expr.value) == BUILT_IN_CONSTANT_P)
7853 expr.original_code = C_MAYBE_CONST_EXPR;
6866c6e8 7854 expr.original_type = NULL;
9771b263 7855 if (exprlist)
bbbbb16a 7856 {
c166b898
ILT
7857 release_tree_vector (exprlist);
7858 release_tree_vector (origtypes);
bbbbb16a 7859 }
81e5eca8 7860 arg_loc.release ();
27bf414c
JM
7861 break;
7862 case CPP_DOT:
7863 /* Structure element reference. */
7864 c_parser_consume_token (parser);
c2255bc4 7865 expr = default_function_array_conversion (expr_loc, expr);
27bf414c
JM
7866 if (c_parser_next_token_is (parser, CPP_NAME))
7867 ident = c_parser_peek_token (parser)->value;
7868 else
7869 {
7870 c_parser_error (parser, "expected identifier");
7871 expr.value = error_mark_node;
7872 expr.original_code = ERROR_MARK;
6866c6e8 7873 expr.original_type = NULL;
27bf414c
JM
7874 return expr;
7875 }
7876 c_parser_consume_token (parser);
c2255bc4 7877 expr.value = build_component_ref (op_loc, expr.value, ident);
27bf414c 7878 expr.original_code = ERROR_MARK;
6866c6e8
ILT
7879 if (TREE_CODE (expr.value) != COMPONENT_REF)
7880 expr.original_type = NULL;
7881 else
7882 {
7883 /* Remember the original type of a bitfield. */
7884 tree field = TREE_OPERAND (expr.value, 1);
7885 if (TREE_CODE (field) != FIELD_DECL)
7886 expr.original_type = NULL;
7887 else
7888 expr.original_type = DECL_BIT_FIELD_TYPE (field);
7889 }
27bf414c
JM
7890 break;
7891 case CPP_DEREF:
7892 /* Structure element reference. */
7893 c_parser_consume_token (parser);
267bac10 7894 expr = convert_lvalue_to_rvalue (expr_loc, expr, true, false);
27bf414c
JM
7895 if (c_parser_next_token_is (parser, CPP_NAME))
7896 ident = c_parser_peek_token (parser)->value;
7897 else
7898 {
7899 c_parser_error (parser, "expected identifier");
7900 expr.value = error_mark_node;
7901 expr.original_code = ERROR_MARK;
6866c6e8 7902 expr.original_type = NULL;
27bf414c
JM
7903 return expr;
7904 }
7905 c_parser_consume_token (parser);
c2255bc4
AH
7906 expr.value = build_component_ref (op_loc,
7907 build_indirect_ref (op_loc,
c9f9eb5d 7908 expr.value,
dd865ef6 7909 RO_ARROW),
6a3799eb 7910 ident);
27bf414c 7911 expr.original_code = ERROR_MARK;
6866c6e8
ILT
7912 if (TREE_CODE (expr.value) != COMPONENT_REF)
7913 expr.original_type = NULL;
7914 else
7915 {
7916 /* Remember the original type of a bitfield. */
7917 tree field = TREE_OPERAND (expr.value, 1);
7918 if (TREE_CODE (field) != FIELD_DECL)
7919 expr.original_type = NULL;
7920 else
7921 expr.original_type = DECL_BIT_FIELD_TYPE (field);
7922 }
27bf414c
JM
7923 break;
7924 case CPP_PLUS_PLUS:
7925 /* Postincrement. */
7926 c_parser_consume_token (parser);
36536d79 7927 /* If the expressions have array notations, we expand them. */
b72271b9 7928 if (flag_cilkplus
36536d79
BI
7929 && TREE_CODE (expr.value) == ARRAY_NOTATION_REF)
7930 expr = fix_array_notation_expr (expr_loc, POSTINCREMENT_EXPR, expr);
7931 else
7932 {
7933 expr = default_function_array_read_conversion (expr_loc, expr);
7934 expr.value = build_unary_op (op_loc,
7935 POSTINCREMENT_EXPR, expr.value, 0);
7936 }
27bf414c 7937 expr.original_code = ERROR_MARK;
6866c6e8 7938 expr.original_type = NULL;
27bf414c
JM
7939 break;
7940 case CPP_MINUS_MINUS:
7941 /* Postdecrement. */
7942 c_parser_consume_token (parser);
36536d79 7943 /* If the expressions have array notations, we expand them. */
b72271b9 7944 if (flag_cilkplus
36536d79
BI
7945 && TREE_CODE (expr.value) == ARRAY_NOTATION_REF)
7946 expr = fix_array_notation_expr (expr_loc, POSTDECREMENT_EXPR, expr);
7947 else
7948 {
7949 expr = default_function_array_read_conversion (expr_loc, expr);
7950 expr.value = build_unary_op (op_loc,
7951 POSTDECREMENT_EXPR, expr.value, 0);
7952 }
27bf414c 7953 expr.original_code = ERROR_MARK;
6866c6e8 7954 expr.original_type = NULL;
27bf414c
JM
7955 break;
7956 default:
7957 return expr;
7958 }
7959 }
7960}
7961
7962/* Parse an expression (C90 6.3.17, C99 6.5.17).
7963
7964 expression:
7965 assignment-expression
7966 expression , assignment-expression
7967*/
7968
7969static struct c_expr
7970c_parser_expression (c_parser *parser)
7971{
267bac10 7972 location_t tloc = c_parser_peek_token (parser)->location;
27bf414c
JM
7973 struct c_expr expr;
7974 expr = c_parser_expr_no_commas (parser, NULL);
267bac10
JM
7975 if (c_parser_next_token_is (parser, CPP_COMMA))
7976 expr = convert_lvalue_to_rvalue (tloc, expr, true, false);
27bf414c
JM
7977 while (c_parser_next_token_is (parser, CPP_COMMA))
7978 {
7979 struct c_expr next;
056928b2 7980 tree lhsval;
c2255bc4
AH
7981 location_t loc = c_parser_peek_token (parser)->location;
7982 location_t expr_loc;
27bf414c 7983 c_parser_consume_token (parser);
c2255bc4 7984 expr_loc = c_parser_peek_token (parser)->location;
056928b2
JJ
7985 lhsval = expr.value;
7986 while (TREE_CODE (lhsval) == COMPOUND_EXPR)
7987 lhsval = TREE_OPERAND (lhsval, 1);
7988 if (DECL_P (lhsval) || handled_component_p (lhsval))
7989 mark_exp_read (lhsval);
27bf414c 7990 next = c_parser_expr_no_commas (parser, NULL);
267bac10 7991 next = convert_lvalue_to_rvalue (expr_loc, next, true, false);
c2255bc4 7992 expr.value = build_compound_expr (loc, expr.value, next.value);
27bf414c 7993 expr.original_code = COMPOUND_EXPR;
81f40b79 7994 expr.original_type = next.original_type;
27bf414c
JM
7995 }
7996 return expr;
7997}
7998
267bac10
JM
7999/* Parse an expression and convert functions or arrays to pointers and
8000 lvalues to rvalues. */
46bdb9cf
JM
8001
8002static struct c_expr
8003c_parser_expression_conv (c_parser *parser)
8004{
8005 struct c_expr expr;
c2255bc4 8006 location_t loc = c_parser_peek_token (parser)->location;
46bdb9cf 8007 expr = c_parser_expression (parser);
267bac10 8008 expr = convert_lvalue_to_rvalue (loc, expr, true, false);
46bdb9cf
JM
8009 return expr;
8010}
8011
b108f48f
JJ
8012/* Helper function of c_parser_expr_list. Check if IDXth (0 based)
8013 argument is a literal zero alone and if so, set it in literal_zero_mask. */
8014
8015static inline void
8016c_parser_check_literal_zero (c_parser *parser, unsigned *literal_zero_mask,
8017 unsigned int idx)
8018{
8019 if (idx >= HOST_BITS_PER_INT)
8020 return;
8021
8022 c_token *tok = c_parser_peek_token (parser);
8023 switch (tok->type)
8024 {
8025 case CPP_NUMBER:
8026 case CPP_CHAR:
8027 case CPP_WCHAR:
8028 case CPP_CHAR16:
8029 case CPP_CHAR32:
8030 /* If a parameter is literal zero alone, remember it
8031 for -Wmemset-transposed-args warning. */
8032 if (integer_zerop (tok->value)
8033 && !TREE_OVERFLOW (tok->value)
8034 && (c_parser_peek_2nd_token (parser)->type == CPP_COMMA
8035 || c_parser_peek_2nd_token (parser)->type == CPP_CLOSE_PAREN))
8036 *literal_zero_mask |= 1U << idx;
8037 default:
8038 break;
8039 }
8040}
8041
46bdb9cf 8042/* Parse a non-empty list of expressions. If CONVERT_P, convert
267bac10 8043 functions and arrays to pointers and lvalues to rvalues. If
81e5eca8
MP
8044 FOLD_P, fold the expressions. If LOCATIONS is non-NULL, save the
8045 locations of function arguments into this vector.
27bf414c
JM
8046
8047 nonempty-expr-list:
8048 assignment-expression
8049 nonempty-expr-list , assignment-expression
8050*/
8051
9771b263 8052static vec<tree, va_gc> *
bbbbb16a 8053c_parser_expr_list (c_parser *parser, bool convert_p, bool fold_p,
9771b263 8054 vec<tree, va_gc> **p_orig_types,
81e5eca8 8055 location_t *sizeof_arg_loc, tree *sizeof_arg,
b108f48f
JJ
8056 vec<location_t> *locations,
8057 unsigned int *literal_zero_mask)
27bf414c 8058{
9771b263
DN
8059 vec<tree, va_gc> *ret;
8060 vec<tree, va_gc> *orig_types;
27bf414c 8061 struct c_expr expr;
c2255bc4 8062 location_t loc = c_parser_peek_token (parser)->location;
3a785c97
JJ
8063 location_t cur_sizeof_arg_loc = UNKNOWN_LOCATION;
8064 unsigned int idx = 0;
bbbbb16a 8065
c166b898 8066 ret = make_tree_vector ();
bbbbb16a
ILT
8067 if (p_orig_types == NULL)
8068 orig_types = NULL;
8069 else
c166b898 8070 orig_types = make_tree_vector ();
bbbbb16a 8071
1a4049e7
JJ
8072 if (sizeof_arg != NULL
8073 && c_parser_next_token_is_keyword (parser, RID_SIZEOF))
3a785c97 8074 cur_sizeof_arg_loc = c_parser_peek_2nd_token (parser)->location;
b108f48f
JJ
8075 if (literal_zero_mask)
8076 c_parser_check_literal_zero (parser, literal_zero_mask, 0);
27bf414c 8077 expr = c_parser_expr_no_commas (parser, NULL);
46bdb9cf 8078 if (convert_p)
267bac10 8079 expr = convert_lvalue_to_rvalue (loc, expr, true, true);
928c19bb
JM
8080 if (fold_p)
8081 expr.value = c_fully_fold (expr.value, false, NULL);
9771b263
DN
8082 ret->quick_push (expr.value);
8083 if (orig_types)
8084 orig_types->quick_push (expr.original_type);
81e5eca8
MP
8085 if (locations)
8086 locations->safe_push (loc);
3a785c97
JJ
8087 if (sizeof_arg != NULL
8088 && cur_sizeof_arg_loc != UNKNOWN_LOCATION
8089 && expr.original_code == SIZEOF_EXPR)
8090 {
8091 sizeof_arg[0] = c_last_sizeof_arg;
8092 sizeof_arg_loc[0] = cur_sizeof_arg_loc;
8093 }
27bf414c
JM
8094 while (c_parser_next_token_is (parser, CPP_COMMA))
8095 {
8096 c_parser_consume_token (parser);
c2255bc4 8097 loc = c_parser_peek_token (parser)->location;
1a4049e7
JJ
8098 if (sizeof_arg != NULL
8099 && c_parser_next_token_is_keyword (parser, RID_SIZEOF))
3a785c97 8100 cur_sizeof_arg_loc = c_parser_peek_2nd_token (parser)->location;
1a4049e7 8101 else
3a785c97 8102 cur_sizeof_arg_loc = UNKNOWN_LOCATION;
b108f48f
JJ
8103 if (literal_zero_mask)
8104 c_parser_check_literal_zero (parser, literal_zero_mask, idx + 1);
27bf414c 8105 expr = c_parser_expr_no_commas (parser, NULL);
46bdb9cf 8106 if (convert_p)
267bac10 8107 expr = convert_lvalue_to_rvalue (loc, expr, true, true);
928c19bb
JM
8108 if (fold_p)
8109 expr.value = c_fully_fold (expr.value, false, NULL);
9771b263
DN
8110 vec_safe_push (ret, expr.value);
8111 if (orig_types)
8112 vec_safe_push (orig_types, expr.original_type);
81e5eca8
MP
8113 if (locations)
8114 locations->safe_push (loc);
3a785c97
JJ
8115 if (++idx < 3
8116 && sizeof_arg != NULL
8117 && cur_sizeof_arg_loc != UNKNOWN_LOCATION
1a4049e7
JJ
8118 && expr.original_code == SIZEOF_EXPR)
8119 {
3a785c97
JJ
8120 sizeof_arg[idx] = c_last_sizeof_arg;
8121 sizeof_arg_loc[idx] = cur_sizeof_arg_loc;
1a4049e7
JJ
8122 }
8123 }
9771b263 8124 if (orig_types)
bbbbb16a 8125 *p_orig_types = orig_types;
27bf414c
JM
8126 return ret;
8127}
27bf414c
JM
8128\f
8129/* Parse Objective-C-specific constructs. */
8130
8131/* Parse an objc-class-definition.
8132
8133 objc-class-definition:
8134 @interface identifier objc-superclass[opt] objc-protocol-refs[opt]
8135 objc-class-instance-variables[opt] objc-methodprotolist @end
8136 @implementation identifier objc-superclass[opt]
8137 objc-class-instance-variables[opt]
8138 @interface identifier ( identifier ) objc-protocol-refs[opt]
8139 objc-methodprotolist @end
ec3e9f82
NP
8140 @interface identifier ( ) objc-protocol-refs[opt]
8141 objc-methodprotolist @end
27bf414c
JM
8142 @implementation identifier ( identifier )
8143
8144 objc-superclass:
8145 : identifier
8146
8147 "@interface identifier (" must start "@interface identifier (
8148 identifier ) ...": objc-methodprotolist in the first production may
0fa2e4df 8149 not start with a parenthesized identifier as a declarator of a data
27bf414c
JM
8150 definition with no declaration specifiers if the objc-superclass,
8151 objc-protocol-refs and objc-class-instance-variables are omitted. */
8152
8153static void
c165dca7 8154c_parser_objc_class_definition (c_parser *parser, tree attributes)
27bf414c
JM
8155{
8156 bool iface_p;
8157 tree id1;
8158 tree superclass;
8159 if (c_parser_next_token_is_keyword (parser, RID_AT_INTERFACE))
8160 iface_p = true;
8161 else if (c_parser_next_token_is_keyword (parser, RID_AT_IMPLEMENTATION))
8162 iface_p = false;
8163 else
8164 gcc_unreachable ();
c165dca7 8165
27bf414c
JM
8166 c_parser_consume_token (parser);
8167 if (c_parser_next_token_is_not (parser, CPP_NAME))
8168 {
8169 c_parser_error (parser, "expected identifier");
8170 return;
8171 }
8172 id1 = c_parser_peek_token (parser)->value;
8173 c_parser_consume_token (parser);
8174 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
8175 {
ec3e9f82 8176 /* We have a category or class extension. */
27bf414c
JM
8177 tree id2;
8178 tree proto = NULL_TREE;
8179 c_parser_consume_token (parser);
8180 if (c_parser_next_token_is_not (parser, CPP_NAME))
8181 {
ec3e9f82
NP
8182 if (iface_p && c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
8183 {
8184 /* We have a class extension. */
8185 id2 = NULL_TREE;
8186 }
8187 else
8188 {
8189 c_parser_error (parser, "expected identifier or %<)%>");
8190 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
8191 return;
8192 }
8193 }
8194 else
8195 {
8196 id2 = c_parser_peek_token (parser)->value;
8197 c_parser_consume_token (parser);
27bf414c 8198 }
27bf414c
JM
8199 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
8200 if (!iface_p)
8201 {
8202 objc_start_category_implementation (id1, id2);
8203 return;
8204 }
8205 if (c_parser_next_token_is (parser, CPP_LESS))
8206 proto = c_parser_objc_protocol_refs (parser);
c165dca7 8207 objc_start_category_interface (id1, id2, proto, attributes);
27bf414c
JM
8208 c_parser_objc_methodprotolist (parser);
8209 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
8210 objc_finish_interface ();
8211 return;
8212 }
8213 if (c_parser_next_token_is (parser, CPP_COLON))
8214 {
8215 c_parser_consume_token (parser);
8216 if (c_parser_next_token_is_not (parser, CPP_NAME))
8217 {
8218 c_parser_error (parser, "expected identifier");
8219 return;
8220 }
8221 superclass = c_parser_peek_token (parser)->value;
8222 c_parser_consume_token (parser);
8223 }
8224 else
8225 superclass = NULL_TREE;
8226 if (iface_p)
8227 {
8228 tree proto = NULL_TREE;
8229 if (c_parser_next_token_is (parser, CPP_LESS))
8230 proto = c_parser_objc_protocol_refs (parser);
c165dca7 8231 objc_start_class_interface (id1, superclass, proto, attributes);
27bf414c
JM
8232 }
8233 else
8234 objc_start_class_implementation (id1, superclass);
8235 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
8236 c_parser_objc_class_instance_variables (parser);
8237 if (iface_p)
8238 {
8239 objc_continue_interface ();
8240 c_parser_objc_methodprotolist (parser);
8241 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
8242 objc_finish_interface ();
8243 }
8244 else
8245 {
8246 objc_continue_implementation ();
8247 return;
8248 }
8249}
8250
8251/* Parse objc-class-instance-variables.
8252
8253 objc-class-instance-variables:
8254 { objc-instance-variable-decl-list[opt] }
8255
8256 objc-instance-variable-decl-list:
8257 objc-visibility-spec
8258 objc-instance-variable-decl ;
8259 ;
8260 objc-instance-variable-decl-list objc-visibility-spec
8261 objc-instance-variable-decl-list objc-instance-variable-decl ;
8262 objc-instance-variable-decl-list ;
8263
8264 objc-visibility-spec:
8265 @private
8266 @protected
8267 @public
8268
8269 objc-instance-variable-decl:
8270 struct-declaration
8271*/
8272
8273static void
8274c_parser_objc_class_instance_variables (c_parser *parser)
8275{
8276 gcc_assert (c_parser_next_token_is (parser, CPP_OPEN_BRACE));
8277 c_parser_consume_token (parser);
8278 while (c_parser_next_token_is_not (parser, CPP_EOF))
8279 {
8280 tree decls;
8281 /* Parse any stray semicolon. */
8282 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
8283 {
c1771a20 8284 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
4e26ba90 8285 "extra semicolon");
27bf414c
JM
8286 c_parser_consume_token (parser);
8287 continue;
8288 }
8289 /* Stop if at the end of the instance variables. */
8290 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
8291 {
8292 c_parser_consume_token (parser);
8293 break;
8294 }
8295 /* Parse any objc-visibility-spec. */
49b91f05 8296 if (c_parser_next_token_is_keyword (parser, RID_AT_PRIVATE))
27bf414c
JM
8297 {
8298 c_parser_consume_token (parser);
c37d8c30 8299 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
27bf414c
JM
8300 continue;
8301 }
49b91f05 8302 else if (c_parser_next_token_is_keyword (parser, RID_AT_PROTECTED))
27bf414c
JM
8303 {
8304 c_parser_consume_token (parser);
c37d8c30 8305 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
27bf414c
JM
8306 continue;
8307 }
49b91f05 8308 else if (c_parser_next_token_is_keyword (parser, RID_AT_PUBLIC))
27bf414c
JM
8309 {
8310 c_parser_consume_token (parser);
c37d8c30
IS
8311 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
8312 continue;
8313 }
8314 else if (c_parser_next_token_is_keyword (parser, RID_AT_PACKAGE))
8315 {
8316 c_parser_consume_token (parser);
8317 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
27bf414c
JM
8318 continue;
8319 }
bc4071dd
RH
8320 else if (c_parser_next_token_is (parser, CPP_PRAGMA))
8321 {
8322 c_parser_pragma (parser, pragma_external);
8323 continue;
8324 }
8325
27bf414c
JM
8326 /* Parse some comma-separated declarations. */
8327 decls = c_parser_struct_declaration (parser);
4e26ba90
NP
8328 if (decls == NULL)
8329 {
8330 /* There is a syntax error. We want to skip the offending
8331 tokens up to the next ';' (included) or '}'
8332 (excluded). */
8333
8334 /* First, skip manually a ')' or ']'. This is because they
8335 reduce the nesting level, so c_parser_skip_until_found()
8336 wouldn't be able to skip past them. */
8337 c_token *token = c_parser_peek_token (parser);
8338 if (token->type == CPP_CLOSE_PAREN || token->type == CPP_CLOSE_SQUARE)
8339 c_parser_consume_token (parser);
8340
8341 /* Then, do the standard skipping. */
8342 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
8343
8344 /* We hopefully recovered. Start normal parsing again. */
8345 parser->error = false;
8346 continue;
8347 }
8348 else
8349 {
8350 /* Comma-separated instance variables are chained together
8351 in reverse order; add them one by one. */
8352 tree ivar = nreverse (decls);
8353 for (; ivar; ivar = DECL_CHAIN (ivar))
8354 objc_add_instance_variable (copy_node (ivar));
8355 }
27bf414c
JM
8356 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
8357 }
8358}
8359
8360/* Parse an objc-class-declaration.
8361
8362 objc-class-declaration:
8363 @class identifier-list ;
8364*/
8365
8366static void
8367c_parser_objc_class_declaration (c_parser *parser)
8368{
49b91f05 8369 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_CLASS));
27bf414c
JM
8370 c_parser_consume_token (parser);
8371 /* Any identifiers, including those declared as type names, are OK
8372 here. */
8373 while (true)
8374 {
8375 tree id;
8376 if (c_parser_next_token_is_not (parser, CPP_NAME))
8377 {
8378 c_parser_error (parser, "expected identifier");
da57d1b9
NP
8379 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
8380 parser->error = false;
8381 return;
27bf414c
JM
8382 }
8383 id = c_parser_peek_token (parser)->value;
32dabdaf 8384 objc_declare_class (id);
27bf414c
JM
8385 c_parser_consume_token (parser);
8386 if (c_parser_next_token_is (parser, CPP_COMMA))
8387 c_parser_consume_token (parser);
8388 else
8389 break;
8390 }
8391 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
27bf414c
JM
8392}
8393
8394/* Parse an objc-alias-declaration.
8395
8396 objc-alias-declaration:
8397 @compatibility_alias identifier identifier ;
8398*/
8399
8400static void
8401c_parser_objc_alias_declaration (c_parser *parser)
8402{
8403 tree id1, id2;
8404 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_ALIAS));
8405 c_parser_consume_token (parser);
8406 if (c_parser_next_token_is_not (parser, CPP_NAME))
8407 {
8408 c_parser_error (parser, "expected identifier");
8409 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
8410 return;
8411 }
8412 id1 = c_parser_peek_token (parser)->value;
8413 c_parser_consume_token (parser);
8414 if (c_parser_next_token_is_not (parser, CPP_NAME))
8415 {
8416 c_parser_error (parser, "expected identifier");
8417 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
8418 return;
8419 }
8420 id2 = c_parser_peek_token (parser)->value;
8421 c_parser_consume_token (parser);
8422 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
8423 objc_declare_alias (id1, id2);
8424}
8425
8426/* Parse an objc-protocol-definition.
8427
8428 objc-protocol-definition:
8429 @protocol identifier objc-protocol-refs[opt] objc-methodprotolist @end
8430 @protocol identifier-list ;
8431
8432 "@protocol identifier ;" should be resolved as "@protocol
8433 identifier-list ;": objc-methodprotolist may not start with a
8434 semicolon in the first alternative if objc-protocol-refs are
8435 omitted. */
8436
8437static void
c165dca7 8438c_parser_objc_protocol_definition (c_parser *parser, tree attributes)
27bf414c
JM
8439{
8440 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_PROTOCOL));
c165dca7 8441
27bf414c
JM
8442 c_parser_consume_token (parser);
8443 if (c_parser_next_token_is_not (parser, CPP_NAME))
8444 {
8445 c_parser_error (parser, "expected identifier");
8446 return;
8447 }
8448 if (c_parser_peek_2nd_token (parser)->type == CPP_COMMA
8449 || c_parser_peek_2nd_token (parser)->type == CPP_SEMICOLON)
8450 {
27bf414c
JM
8451 /* Any identifiers, including those declared as type names, are
8452 OK here. */
8453 while (true)
8454 {
8455 tree id;
8456 if (c_parser_next_token_is_not (parser, CPP_NAME))
8457 {
8458 c_parser_error (parser, "expected identifier");
8459 break;
8460 }
8461 id = c_parser_peek_token (parser)->value;
c59633d9 8462 objc_declare_protocol (id, attributes);
27bf414c
JM
8463 c_parser_consume_token (parser);
8464 if (c_parser_next_token_is (parser, CPP_COMMA))
8465 c_parser_consume_token (parser);
8466 else
8467 break;
8468 }
8469 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
27bf414c
JM
8470 }
8471 else
8472 {
8473 tree id = c_parser_peek_token (parser)->value;
8474 tree proto = NULL_TREE;
8475 c_parser_consume_token (parser);
8476 if (c_parser_next_token_is (parser, CPP_LESS))
8477 proto = c_parser_objc_protocol_refs (parser);
0bacb8c7 8478 parser->objc_pq_context = true;
c165dca7 8479 objc_start_protocol (id, proto, attributes);
27bf414c
JM
8480 c_parser_objc_methodprotolist (parser);
8481 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
0bacb8c7 8482 parser->objc_pq_context = false;
27bf414c
JM
8483 objc_finish_interface ();
8484 }
8485}
8486
8487/* Parse an objc-method-type.
8488
8489 objc-method-type:
8490 +
8491 -
27bf414c 8492
249a82c4
NP
8493 Return true if it is a class method (+) and false if it is
8494 an instance method (-).
8495*/
8496static inline bool
27bf414c
JM
8497c_parser_objc_method_type (c_parser *parser)
8498{
8499 switch (c_parser_peek_token (parser)->type)
8500 {
8501 case CPP_PLUS:
8502 c_parser_consume_token (parser);
249a82c4 8503 return true;
27bf414c
JM
8504 case CPP_MINUS:
8505 c_parser_consume_token (parser);
249a82c4 8506 return false;
27bf414c
JM
8507 default:
8508 gcc_unreachable ();
8509 }
8510}
8511
8512/* Parse an objc-method-definition.
8513
8514 objc-method-definition:
8515 objc-method-type objc-method-decl ;[opt] compound-statement
8516*/
8517
8518static void
8519c_parser_objc_method_definition (c_parser *parser)
8520{
249a82c4 8521 bool is_class_method = c_parser_objc_method_type (parser);
a04a722b 8522 tree decl, attributes = NULL_TREE, expr = NULL_TREE;
0bacb8c7 8523 parser->objc_pq_context = true;
a04a722b
JM
8524 decl = c_parser_objc_method_decl (parser, is_class_method, &attributes,
8525 &expr);
f7e71da5
IS
8526 if (decl == error_mark_node)
8527 return; /* Bail here. */
8528
27bf414c
JM
8529 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
8530 {
8531 c_parser_consume_token (parser);
c1771a20 8532 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
509c9d60 8533 "extra semicolon in method definition specified");
27bf414c 8534 }
f7e71da5 8535
8f078c08
AP
8536 if (!c_parser_next_token_is (parser, CPP_OPEN_BRACE))
8537 {
8538 c_parser_error (parser, "expected %<{%>");
8539 return;
8540 }
f7e71da5 8541
0bacb8c7 8542 parser->objc_pq_context = false;
a04a722b 8543 if (objc_start_method_definition (is_class_method, decl, attributes, expr))
45547c7f
NP
8544 {
8545 add_stmt (c_parser_compound_statement (parser));
8546 objc_finish_method_definition (current_function_decl);
8547 }
8548 else
8549 {
8550 /* This code is executed when we find a method definition
a26d8862
NP
8551 outside of an @implementation context (or invalid for other
8552 reasons). Parse the method (to keep going) but do not emit
8553 any code.
45547c7f
NP
8554 */
8555 c_parser_compound_statement (parser);
8556 }
27bf414c
JM
8557}
8558
8559/* Parse an objc-methodprotolist.
8560
8561 objc-methodprotolist:
8562 empty
8563 objc-methodprotolist objc-methodproto
8564 objc-methodprotolist declaration
8565 objc-methodprotolist ;
92902b1b
IS
8566 @optional
8567 @required
27bf414c
JM
8568
8569 The declaration is a data definition, which may be missing
8570 declaration specifiers under the same rules and diagnostics as
8571 other data definitions outside functions, and the stray semicolon
8572 is diagnosed the same way as a stray semicolon outside a
8573 function. */
8574
8575static void
8576c_parser_objc_methodprotolist (c_parser *parser)
8577{
8578 while (true)
8579 {
8580 /* The list is terminated by @end. */
8581 switch (c_parser_peek_token (parser)->type)
8582 {
8583 case CPP_SEMICOLON:
c1771a20 8584 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
509c9d60 8585 "ISO C does not allow extra %<;%> outside of a function");
27bf414c
JM
8586 c_parser_consume_token (parser);
8587 break;
8588 case CPP_PLUS:
8589 case CPP_MINUS:
8590 c_parser_objc_methodproto (parser);
8591 break;
b9b58168
RH
8592 case CPP_PRAGMA:
8593 c_parser_pragma (parser, pragma_external);
8594 break;
27bf414c
JM
8595 case CPP_EOF:
8596 return;
8597 default:
8598 if (c_parser_next_token_is_keyword (parser, RID_AT_END))
8599 return;
668ea4b1 8600 else if (c_parser_next_token_is_keyword (parser, RID_AT_PROPERTY))
f614132b 8601 c_parser_objc_at_property_declaration (parser);
92902b1b
IS
8602 else if (c_parser_next_token_is_keyword (parser, RID_AT_OPTIONAL))
8603 {
8604 objc_set_method_opt (true);
8605 c_parser_consume_token (parser);
8606 }
8607 else if (c_parser_next_token_is_keyword (parser, RID_AT_REQUIRED))
8608 {
8609 objc_set_method_opt (false);
8610 c_parser_consume_token (parser);
8611 }
8612 else
8613 c_parser_declaration_or_fndef (parser, false, false, true,
acf0174b 8614 false, true, NULL, vNULL);
27bf414c
JM
8615 break;
8616 }
8617 }
8618}
8619
8620/* Parse an objc-methodproto.
8621
8622 objc-methodproto:
8623 objc-method-type objc-method-decl ;
8624*/
8625
8626static void
8627c_parser_objc_methodproto (c_parser *parser)
8628{
249a82c4 8629 bool is_class_method = c_parser_objc_method_type (parser);
f7e71da5 8630 tree decl, attributes = NULL_TREE;
249a82c4 8631
27bf414c 8632 /* Remember protocol qualifiers in prototypes. */
0bacb8c7 8633 parser->objc_pq_context = true;
a04a722b
JM
8634 decl = c_parser_objc_method_decl (parser, is_class_method, &attributes,
8635 NULL);
f7e71da5 8636 /* Forget protocol qualifiers now. */
0bacb8c7 8637 parser->objc_pq_context = false;
f7e71da5
IS
8638
8639 /* Do not allow the presence of attributes to hide an erroneous
8640 method implementation in the interface section. */
8641 if (!c_parser_next_token_is (parser, CPP_SEMICOLON))
8642 {
8643 c_parser_error (parser, "expected %<;%>");
8644 return;
8645 }
8646
8647 if (decl != error_mark_node)
249a82c4 8648 objc_add_method_declaration (is_class_method, decl, attributes);
f7e71da5 8649
27bf414c
JM
8650 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
8651}
8652
f7e71da5
IS
8653/* If we are at a position that method attributes may be present, check that
8654 there are not any parsed already (a syntax error) and then collect any
8655 specified at the current location. Finally, if new attributes were present,
8656 check that the next token is legal ( ';' for decls and '{' for defs). */
8657
8658static bool
8659c_parser_objc_maybe_method_attributes (c_parser* parser, tree* attributes)
8660{
8661 bool bad = false;
8662 if (*attributes)
8663 {
8664 c_parser_error (parser,
8665 "method attributes must be specified at the end only");
8666 *attributes = NULL_TREE;
8667 bad = true;
8668 }
8669
8670 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
8671 *attributes = c_parser_attributes (parser);
8672
8673 /* If there were no attributes here, just report any earlier error. */
8674 if (*attributes == NULL_TREE || bad)
8675 return bad;
8676
8677 /* If the attributes are followed by a ; or {, then just report any earlier
8678 error. */
8679 if (c_parser_next_token_is (parser, CPP_SEMICOLON)
8680 || c_parser_next_token_is (parser, CPP_OPEN_BRACE))
8681 return bad;
8682
8683 /* We've got attributes, but not at the end. */
8684 c_parser_error (parser,
8685 "expected %<;%> or %<{%> after method attribute definition");
8686 return true;
8687}
8688
27bf414c
JM
8689/* Parse an objc-method-decl.
8690
8691 objc-method-decl:
8692 ( objc-type-name ) objc-selector
8693 objc-selector
8694 ( objc-type-name ) objc-keyword-selector objc-optparmlist
8695 objc-keyword-selector objc-optparmlist
f7e71da5 8696 attributes
27bf414c
JM
8697
8698 objc-keyword-selector:
8699 objc-keyword-decl
8700 objc-keyword-selector objc-keyword-decl
8701
8702 objc-keyword-decl:
8703 objc-selector : ( objc-type-name ) identifier
8704 objc-selector : identifier
8705 : ( objc-type-name ) identifier
8706 : identifier
8707
8708 objc-optparmlist:
8709 objc-optparms objc-optellipsis
8710
8711 objc-optparms:
8712 empty
8713 objc-opt-parms , parameter-declaration
8714
8715 objc-optellipsis:
8716 empty
8717 , ...
8718*/
8719
8720static tree
a04a722b
JM
8721c_parser_objc_method_decl (c_parser *parser, bool is_class_method,
8722 tree *attributes, tree *expr)
27bf414c
JM
8723{
8724 tree type = NULL_TREE;
8725 tree sel;
8726 tree parms = NULL_TREE;
dbb74365 8727 bool ellipsis = false;
f7e71da5 8728 bool attr_err = false;
dbb74365 8729
f7e71da5 8730 *attributes = NULL_TREE;
27bf414c
JM
8731 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
8732 {
8733 c_parser_consume_token (parser);
8734 type = c_parser_objc_type_name (parser);
8735 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
8736 }
8737 sel = c_parser_objc_selector (parser);
8738 /* If there is no selector, or a colon follows, we have an
8739 objc-keyword-selector. If there is a selector, and a colon does
8740 not follow, that selector ends the objc-method-decl. */
8741 if (!sel || c_parser_next_token_is (parser, CPP_COLON))
8742 {
8743 tree tsel = sel;
8744 tree list = NULL_TREE;
27bf414c
JM
8745 while (true)
8746 {
8747 tree atype = NULL_TREE, id, keyworddecl;
f7e71da5 8748 tree param_attr = NULL_TREE;
27bf414c
JM
8749 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
8750 break;
8751 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
8752 {
8753 c_parser_consume_token (parser);
8754 atype = c_parser_objc_type_name (parser);
8755 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
8756 "expected %<)%>");
8757 }
f7e71da5
IS
8758 /* New ObjC allows attributes on method parameters. */
8759 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
8760 param_attr = c_parser_attributes (parser);
27bf414c
JM
8761 if (c_parser_next_token_is_not (parser, CPP_NAME))
8762 {
8763 c_parser_error (parser, "expected identifier");
8764 return error_mark_node;
8765 }
8766 id = c_parser_peek_token (parser)->value;
8767 c_parser_consume_token (parser);
f7e71da5 8768 keyworddecl = objc_build_keyword_decl (tsel, atype, id, param_attr);
27bf414c
JM
8769 list = chainon (list, keyworddecl);
8770 tsel = c_parser_objc_selector (parser);
8771 if (!tsel && c_parser_next_token_is_not (parser, CPP_COLON))
8772 break;
8773 }
f7e71da5
IS
8774
8775 attr_err |= c_parser_objc_maybe_method_attributes (parser, attributes) ;
8776
27bf414c
JM
8777 /* Parse the optional parameter list. Optional Objective-C
8778 method parameters follow the C syntax, and may include '...'
8779 to denote a variable number of arguments. */
8780 parms = make_node (TREE_LIST);
27bf414c
JM
8781 while (c_parser_next_token_is (parser, CPP_COMMA))
8782 {
8783 struct c_parm *parm;
8784 c_parser_consume_token (parser);
8785 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
8786 {
8787 ellipsis = true;
8788 c_parser_consume_token (parser);
f7e71da5
IS
8789 attr_err |= c_parser_objc_maybe_method_attributes
8790 (parser, attributes) ;
27bf414c
JM
8791 break;
8792 }
8793 parm = c_parser_parameter_declaration (parser, NULL_TREE);
8794 if (parm == NULL)
8795 break;
8796 parms = chainon (parms,
a04a722b 8797 build_tree_list (NULL_TREE, grokparm (parm, expr)));
27bf414c 8798 }
27bf414c
JM
8799 sel = list;
8800 }
f7e71da5
IS
8801 else
8802 attr_err |= c_parser_objc_maybe_method_attributes (parser, attributes) ;
8803
8804 if (sel == NULL)
8805 {
8806 c_parser_error (parser, "objective-c method declaration is expected");
8807 return error_mark_node;
8808 }
8809
8810 if (attr_err)
8811 return error_mark_node;
8812
249a82c4 8813 return objc_build_method_signature (is_class_method, type, sel, parms, ellipsis);
27bf414c
JM
8814}
8815
8816/* Parse an objc-type-name.
8817
8818 objc-type-name:
8819 objc-type-qualifiers[opt] type-name
8820 objc-type-qualifiers[opt]
8821
8822 objc-type-qualifiers:
8823 objc-type-qualifier
8824 objc-type-qualifiers objc-type-qualifier
8825
8826 objc-type-qualifier: one of
8827 in out inout bycopy byref oneway
8828*/
8829
8830static tree
8831c_parser_objc_type_name (c_parser *parser)
8832{
8833 tree quals = NULL_TREE;
d75d71e0 8834 struct c_type_name *type_name = NULL;
27bf414c
JM
8835 tree type = NULL_TREE;
8836 while (true)
8837 {
8838 c_token *token = c_parser_peek_token (parser);
8839 if (token->type == CPP_KEYWORD
8840 && (token->keyword == RID_IN
8841 || token->keyword == RID_OUT
8842 || token->keyword == RID_INOUT
8843 || token->keyword == RID_BYCOPY
8844 || token->keyword == RID_BYREF
8845 || token->keyword == RID_ONEWAY))
8846 {
71fc71d8 8847 quals = chainon (build_tree_list (NULL_TREE, token->value), quals);
27bf414c
JM
8848 c_parser_consume_token (parser);
8849 }
8850 else
8851 break;
8852 }
29ce73cb 8853 if (c_parser_next_tokens_start_typename (parser, cla_prefer_type))
d75d71e0
ILT
8854 type_name = c_parser_type_name (parser);
8855 if (type_name)
928c19bb 8856 type = groktypename (type_name, NULL, NULL);
046608a3
NP
8857
8858 /* If the type is unknown, and error has already been produced and
8859 we need to recover from the error. In that case, use NULL_TREE
8860 for the type, as if no type had been specified; this will use the
8861 default type ('id') which is good for error recovery. */
8862 if (type == error_mark_node)
8863 type = NULL_TREE;
8864
27bf414c
JM
8865 return build_tree_list (quals, type);
8866}
8867
8868/* Parse objc-protocol-refs.
8869
8870 objc-protocol-refs:
8871 < identifier-list >
8872*/
8873
8874static tree
8875c_parser_objc_protocol_refs (c_parser *parser)
8876{
8877 tree list = NULL_TREE;
8878 gcc_assert (c_parser_next_token_is (parser, CPP_LESS));
8879 c_parser_consume_token (parser);
8880 /* Any identifiers, including those declared as type names, are OK
8881 here. */
8882 while (true)
8883 {
8884 tree id;
8885 if (c_parser_next_token_is_not (parser, CPP_NAME))
8886 {
8887 c_parser_error (parser, "expected identifier");
8888 break;
8889 }
8890 id = c_parser_peek_token (parser)->value;
8891 list = chainon (list, build_tree_list (NULL_TREE, id));
8892 c_parser_consume_token (parser);
8893 if (c_parser_next_token_is (parser, CPP_COMMA))
8894 c_parser_consume_token (parser);
8895 else
8896 break;
8897 }
8898 c_parser_require (parser, CPP_GREATER, "expected %<>%>");
8899 return list;
8900}
8901
437c2322 8902/* Parse an objc-try-catch-finally-statement.
27bf414c 8903
437c2322 8904 objc-try-catch-finally-statement:
27bf414c
JM
8905 @try compound-statement objc-catch-list[opt]
8906 @try compound-statement objc-catch-list[opt] @finally compound-statement
8907
8908 objc-catch-list:
437c2322
NP
8909 @catch ( objc-catch-parameter-declaration ) compound-statement
8910 objc-catch-list @catch ( objc-catch-parameter-declaration ) compound-statement
8911
8912 objc-catch-parameter-declaration:
8913 parameter-declaration
8914 '...'
8915
8916 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
8917
8918 PS: This function is identical to cp_parser_objc_try_catch_finally_statement
8919 for C++. Keep them in sync. */
27bf414c
JM
8920
8921static void
437c2322 8922c_parser_objc_try_catch_finally_statement (c_parser *parser)
27bf414c 8923{
437c2322 8924 location_t location;
27bf414c 8925 tree stmt;
437c2322 8926
49b91f05 8927 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_TRY));
27bf414c 8928 c_parser_consume_token (parser);
437c2322 8929 location = c_parser_peek_token (parser)->location;
46270f14 8930 objc_maybe_warn_exceptions (location);
27bf414c 8931 stmt = c_parser_compound_statement (parser);
437c2322
NP
8932 objc_begin_try_stmt (location, stmt);
8933
49b91f05 8934 while (c_parser_next_token_is_keyword (parser, RID_AT_CATCH))
27bf414c
JM
8935 {
8936 struct c_parm *parm;
437c2322
NP
8937 tree parameter_declaration = error_mark_node;
8938 bool seen_open_paren = false;
8939
27bf414c
JM
8940 c_parser_consume_token (parser);
8941 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
437c2322
NP
8942 seen_open_paren = true;
8943 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
27bf414c 8944 {
437c2322
NP
8945 /* We have "@catch (...)" (where the '...' are literally
8946 what is in the code). Skip the '...'.
8947 parameter_declaration is set to NULL_TREE, and
8948 objc_being_catch_clauses() knows that that means
8949 '...'. */
8950 c_parser_consume_token (parser);
8951 parameter_declaration = NULL_TREE;
27bf414c 8952 }
437c2322
NP
8953 else
8954 {
8955 /* We have "@catch (NSException *exception)" or something
8956 like that. Parse the parameter declaration. */
8957 parm = c_parser_parameter_declaration (parser, NULL_TREE);
8958 if (parm == NULL)
8959 parameter_declaration = error_mark_node;
8960 else
a04a722b 8961 parameter_declaration = grokparm (parm, NULL);
437c2322
NP
8962 }
8963 if (seen_open_paren)
8964 c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
8965 else
8966 {
8967 /* If there was no open parenthesis, we are recovering from
8968 an error, and we are trying to figure out what mistake
8969 the user has made. */
8970
8971 /* If there is an immediate closing parenthesis, the user
8972 probably forgot the opening one (ie, they typed "@catch
8973 NSException *e)". Parse the closing parenthesis and keep
8974 going. */
8975 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
8976 c_parser_consume_token (parser);
8977
8978 /* If these is no immediate closing parenthesis, the user
8979 probably doesn't know that parenthesis are required at
8980 all (ie, they typed "@catch NSException *e"). So, just
8981 forget about the closing parenthesis and keep going. */
8982 }
8983 objc_begin_catch_clause (parameter_declaration);
27bf414c
JM
8984 if (c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
8985 c_parser_compound_statement_nostart (parser);
8986 objc_finish_catch_clause ();
8987 }
8988 if (c_parser_next_token_is_keyword (parser, RID_AT_FINALLY))
8989 {
27bf414c 8990 c_parser_consume_token (parser);
437c2322
NP
8991 location = c_parser_peek_token (parser)->location;
8992 stmt = c_parser_compound_statement (parser);
8993 objc_build_finally_clause (location, stmt);
27bf414c
JM
8994 }
8995 objc_finish_try_stmt ();
8996}
8997
8998/* Parse an objc-synchronized-statement.
8999
9000 objc-synchronized-statement:
9001 @synchronized ( expression ) compound-statement
9002*/
9003
9004static void
9005c_parser_objc_synchronized_statement (c_parser *parser)
9006{
9007 location_t loc;
9008 tree expr, stmt;
9009 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_SYNCHRONIZED));
9010 c_parser_consume_token (parser);
9011 loc = c_parser_peek_token (parser)->location;
46270f14 9012 objc_maybe_warn_exceptions (loc);
27bf414c
JM
9013 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
9014 {
267bac10
JM
9015 struct c_expr ce = c_parser_expression (parser);
9016 ce = convert_lvalue_to_rvalue (loc, ce, false, false);
9017 expr = ce.value;
928c19bb 9018 expr = c_fully_fold (expr, false, NULL);
27bf414c
JM
9019 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
9020 }
9021 else
9022 expr = error_mark_node;
9023 stmt = c_parser_compound_statement (parser);
9024 objc_build_synchronized (loc, expr, stmt);
9025}
9026
9027/* Parse an objc-selector; return NULL_TREE without an error if the
9028 next token is not an objc-selector.
9029
9030 objc-selector:
9031 identifier
9032 one of
9033 enum struct union if else while do for switch case default
9034 break continue return goto asm sizeof typeof __alignof
9035 unsigned long const short volatile signed restrict _Complex
9036 in out inout bycopy byref oneway int char float double void _Bool
267bac10 9037 _Atomic
27bf414c
JM
9038
9039 ??? Why this selection of keywords but not, for example, storage
9040 class specifiers? */
9041
9042static tree
9043c_parser_objc_selector (c_parser *parser)
9044{
9045 c_token *token = c_parser_peek_token (parser);
9046 tree value = token->value;
9047 if (token->type == CPP_NAME)
9048 {
9049 c_parser_consume_token (parser);
9050 return value;
9051 }
9052 if (token->type != CPP_KEYWORD)
9053 return NULL_TREE;
9054 switch (token->keyword)
9055 {
9056 case RID_ENUM:
9057 case RID_STRUCT:
9058 case RID_UNION:
9059 case RID_IF:
9060 case RID_ELSE:
9061 case RID_WHILE:
9062 case RID_DO:
9063 case RID_FOR:
9064 case RID_SWITCH:
9065 case RID_CASE:
9066 case RID_DEFAULT:
9067 case RID_BREAK:
9068 case RID_CONTINUE:
9069 case RID_RETURN:
9070 case RID_GOTO:
9071 case RID_ASM:
9072 case RID_SIZEOF:
9073 case RID_TYPEOF:
9074 case RID_ALIGNOF:
9075 case RID_UNSIGNED:
9076 case RID_LONG:
9077 case RID_CONST:
9078 case RID_SHORT:
9079 case RID_VOLATILE:
9080 case RID_SIGNED:
9081 case RID_RESTRICT:
9082 case RID_COMPLEX:
9083 case RID_IN:
9084 case RID_OUT:
9085 case RID_INOUT:
9086 case RID_BYCOPY:
9087 case RID_BYREF:
9088 case RID_ONEWAY:
9089 case RID_INT:
9090 case RID_CHAR:
9091 case RID_FLOAT:
9092 case RID_DOUBLE:
9093 case RID_VOID:
9094 case RID_BOOL:
267bac10 9095 case RID_ATOMIC:
38b7bc7f 9096 case RID_AUTO_TYPE:
78a7c317
DD
9097 case RID_INT_N_0:
9098 case RID_INT_N_1:
9099 case RID_INT_N_2:
9100 case RID_INT_N_3:
27bf414c
JM
9101 c_parser_consume_token (parser);
9102 return value;
9103 default:
9104 return NULL_TREE;
9105 }
9106}
9107
9108/* Parse an objc-selector-arg.
9109
9110 objc-selector-arg:
9111 objc-selector
9112 objc-keywordname-list
9113
9114 objc-keywordname-list:
9115 objc-keywordname
9116 objc-keywordname-list objc-keywordname
9117
9118 objc-keywordname:
9119 objc-selector :
9120 :
9121*/
9122
9123static tree
9124c_parser_objc_selector_arg (c_parser *parser)
9125{
9126 tree sel = c_parser_objc_selector (parser);
9127 tree list = NULL_TREE;
9128 if (sel && c_parser_next_token_is_not (parser, CPP_COLON))
9129 return sel;
9130 while (true)
9131 {
9132 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
9133 return list;
9134 list = chainon (list, build_tree_list (sel, NULL_TREE));
9135 sel = c_parser_objc_selector (parser);
9136 if (!sel && c_parser_next_token_is_not (parser, CPP_COLON))
9137 break;
9138 }
9139 return list;
9140}
9141
9142/* Parse an objc-receiver.
9143
9144 objc-receiver:
9145 expression
9146 class-name
9147 type-name
9148*/
9149
9150static tree
9151c_parser_objc_receiver (c_parser *parser)
9152{
267bac10
JM
9153 location_t loc = c_parser_peek_token (parser)->location;
9154
27bf414c
JM
9155 if (c_parser_peek_token (parser)->type == CPP_NAME
9156 && (c_parser_peek_token (parser)->id_kind == C_ID_TYPENAME
9157 || c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME))
9158 {
9159 tree id = c_parser_peek_token (parser)->value;
9160 c_parser_consume_token (parser);
9161 return objc_get_class_reference (id);
9162 }
267bac10
JM
9163 struct c_expr ce = c_parser_expression (parser);
9164 ce = convert_lvalue_to_rvalue (loc, ce, false, false);
9165 return c_fully_fold (ce.value, false, NULL);
27bf414c
JM
9166}
9167
9168/* Parse objc-message-args.
9169
9170 objc-message-args:
9171 objc-selector
9172 objc-keywordarg-list
9173
9174 objc-keywordarg-list:
9175 objc-keywordarg
9176 objc-keywordarg-list objc-keywordarg
9177
9178 objc-keywordarg:
9179 objc-selector : objc-keywordexpr
9180 : objc-keywordexpr
9181*/
9182
9183static tree
9184c_parser_objc_message_args (c_parser *parser)
9185{
9186 tree sel = c_parser_objc_selector (parser);
9187 tree list = NULL_TREE;
9188 if (sel && c_parser_next_token_is_not (parser, CPP_COLON))
9189 return sel;
9190 while (true)
9191 {
9192 tree keywordexpr;
9193 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
0a7d7dea 9194 return error_mark_node;
27bf414c
JM
9195 keywordexpr = c_parser_objc_keywordexpr (parser);
9196 list = chainon (list, build_tree_list (sel, keywordexpr));
9197 sel = c_parser_objc_selector (parser);
9198 if (!sel && c_parser_next_token_is_not (parser, CPP_COLON))
9199 break;
9200 }
9201 return list;
9202}
9203
9204/* Parse an objc-keywordexpr.
9205
9206 objc-keywordexpr:
9207 nonempty-expr-list
9208*/
9209
9210static tree
9211c_parser_objc_keywordexpr (c_parser *parser)
9212{
bbbbb16a 9213 tree ret;
9771b263 9214 vec<tree, va_gc> *expr_list = c_parser_expr_list (parser, true, true,
81e5eca8 9215 NULL, NULL, NULL, NULL);
9771b263 9216 if (vec_safe_length (expr_list) == 1)
27bf414c
JM
9217 {
9218 /* Just return the expression, remove a level of
9219 indirection. */
9771b263 9220 ret = (*expr_list)[0];
27bf414c
JM
9221 }
9222 else
9223 {
9224 /* We have a comma expression, we will collapse later. */
c166b898 9225 ret = build_tree_list_vec (expr_list);
27bf414c 9226 }
c166b898 9227 release_tree_vector (expr_list);
bbbbb16a 9228 return ret;
27bf414c
JM
9229}
9230
c165dca7
IS
9231/* A check, needed in several places, that ObjC interface, implementation or
9232 method definitions are not prefixed by incorrect items. */
9233static bool
9234c_parser_objc_diagnose_bad_element_prefix (c_parser *parser,
9235 struct c_declspecs *specs)
9236{
9e5b2115
PB
9237 if (!specs->declspecs_seen_p || specs->non_sc_seen_p
9238 || specs->typespec_kind != ctsk_none)
c165dca7
IS
9239 {
9240 c_parser_error (parser,
9241 "no type or storage class may be specified here,");
9242 c_parser_skip_to_end_of_block_or_statement (parser);
9243 return true;
9244 }
9245 return false;
9246}
668ea4b1 9247
f614132b 9248/* Parse an Objective-C @property declaration. The syntax is:
668ea4b1 9249
f614132b
NP
9250 objc-property-declaration:
9251 '@property' objc-property-attributes[opt] struct-declaration ;
668ea4b1 9252
f614132b
NP
9253 objc-property-attributes:
9254 '(' objc-property-attribute-list ')'
9255
9256 objc-property-attribute-list:
9257 objc-property-attribute
9258 objc-property-attribute-list, objc-property-attribute
9259
9260 objc-property-attribute
9261 'getter' = identifier
9262 'setter' = identifier
9263 'readonly'
9264 'readwrite'
9265 'assign'
9266 'retain'
9267 'copy'
9268 'nonatomic'
9269
9270 For example:
9271 @property NSString *name;
9272 @property (readonly) id object;
9273 @property (retain, nonatomic, getter=getTheName) id name;
9274 @property int a, b, c;
9275
9276 PS: This function is identical to cp_parser_objc_at_propery_declaration
200290f2 9277 for C++. Keep them in sync. */
668ea4b1 9278static void
f614132b 9279c_parser_objc_at_property_declaration (c_parser *parser)
668ea4b1 9280{
200290f2
NP
9281 /* The following variables hold the attributes of the properties as
9282 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
9283 seen. When we see an attribute, we set them to 'true' (if they
9284 are boolean properties) or to the identifier (if they have an
9285 argument, ie, for getter and setter). Note that here we only
9286 parse the list of attributes, check the syntax and accumulate the
9287 attributes that we find. objc_add_property_declaration() will
9288 then process the information. */
9289 bool property_assign = false;
9290 bool property_copy = false;
9291 tree property_getter_ident = NULL_TREE;
9292 bool property_nonatomic = false;
9293 bool property_readonly = false;
9294 bool property_readwrite = false;
9295 bool property_retain = false;
9296 tree property_setter_ident = NULL_TREE;
200290f2
NP
9297
9298 /* 'properties' is the list of properties that we read. Usually a
9299 single one, but maybe more (eg, in "@property int a, b, c;" there
9300 are three). */
f614132b
NP
9301 tree properties;
9302 location_t loc;
200290f2 9303
f614132b
NP
9304 loc = c_parser_peek_token (parser)->location;
9305 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_PROPERTY));
668ea4b1 9306
f614132b 9307 c_parser_consume_token (parser); /* Eat '@property'. */
668ea4b1 9308
f614132b
NP
9309 /* Parse the optional attribute list... */
9310 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
668ea4b1 9311 {
f614132b
NP
9312 /* Eat the '(' */
9313 c_parser_consume_token (parser);
9314
9315 /* Property attribute keywords are valid now. */
9316 parser->objc_property_attr_context = true;
9317
9318 while (true)
668ea4b1 9319 {
f614132b
NP
9320 bool syntax_error = false;
9321 c_token *token = c_parser_peek_token (parser);
9322 enum rid keyword;
9323
9324 if (token->type != CPP_KEYWORD)
9325 {
9326 if (token->type == CPP_CLOSE_PAREN)
9327 c_parser_error (parser, "expected identifier");
9328 else
9329 {
9330 c_parser_consume_token (parser);
9331 c_parser_error (parser, "unknown property attribute");
9332 }
9333 break;
9334 }
9335 keyword = token->keyword;
200290f2 9336 c_parser_consume_token (parser);
f614132b
NP
9337 switch (keyword)
9338 {
200290f2 9339 case RID_ASSIGN: property_assign = true; break;
200290f2
NP
9340 case RID_COPY: property_copy = true; break;
9341 case RID_NONATOMIC: property_nonatomic = true; break;
9342 case RID_READONLY: property_readonly = true; break;
9343 case RID_READWRITE: property_readwrite = true; break;
9344 case RID_RETAIN: property_retain = true; break;
9345
f614132b
NP
9346 case RID_GETTER:
9347 case RID_SETTER:
f614132b
NP
9348 if (c_parser_next_token_is_not (parser, CPP_EQ))
9349 {
d853ee42
NP
9350 if (keyword == RID_GETTER)
9351 c_parser_error (parser,
9352 "missing %<=%> (after %<getter%> attribute)");
9353 else
9354 c_parser_error (parser,
9355 "missing %<=%> (after %<setter%> attribute)");
f614132b
NP
9356 syntax_error = true;
9357 break;
9358 }
9359 c_parser_consume_token (parser); /* eat the = */
9360 if (c_parser_next_token_is_not (parser, CPP_NAME))
9361 {
9362 c_parser_error (parser, "expected identifier");
9363 syntax_error = true;
9364 break;
9365 }
f614132b
NP
9366 if (keyword == RID_SETTER)
9367 {
200290f2
NP
9368 if (property_setter_ident != NULL_TREE)
9369 c_parser_error (parser, "the %<setter%> attribute may only be specified once");
9370 else
9371 property_setter_ident = c_parser_peek_token (parser)->value;
f614132b 9372 c_parser_consume_token (parser);
200290f2
NP
9373 if (c_parser_next_token_is_not (parser, CPP_COLON))
9374 c_parser_error (parser, "setter name must terminate with %<:%>");
9375 else
9376 c_parser_consume_token (parser);
f614132b 9377 }
46a88c12 9378 else
f614132b 9379 {
200290f2
NP
9380 if (property_getter_ident != NULL_TREE)
9381 c_parser_error (parser, "the %<getter%> attribute may only be specified once");
9382 else
9383 property_getter_ident = c_parser_peek_token (parser)->value;
f614132b 9384 c_parser_consume_token (parser);
f614132b 9385 }
200290f2
NP
9386 break;
9387 default:
9388 c_parser_error (parser, "unknown property attribute");
f614132b
NP
9389 syntax_error = true;
9390 break;
9391 }
9392
9393 if (syntax_error)
668ea4b1 9394 break;
f614132b
NP
9395
9396 if (c_parser_next_token_is (parser, CPP_COMMA))
668ea4b1 9397 c_parser_consume_token (parser);
f614132b 9398 else
668ea4b1
IS
9399 break;
9400 }
f614132b
NP
9401 parser->objc_property_attr_context = false;
9402 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
9403 }
9404 /* ... and the property declaration(s). */
9405 properties = c_parser_struct_declaration (parser);
668ea4b1 9406
f614132b
NP
9407 if (properties == error_mark_node)
9408 {
9409 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
9410 parser->error = false;
9411 return;
9412 }
668ea4b1 9413
f614132b
NP
9414 if (properties == NULL_TREE)
9415 c_parser_error (parser, "expected identifier");
9416 else
9417 {
9418 /* Comma-separated properties are chained together in
9419 reverse order; add them one by one. */
9420 properties = nreverse (properties);
9421
9422 for (; properties; properties = TREE_CHAIN (properties))
200290f2
NP
9423 objc_add_property_declaration (loc, copy_node (properties),
9424 property_readonly, property_readwrite,
9425 property_assign, property_retain,
9426 property_copy, property_nonatomic,
46a88c12 9427 property_getter_ident, property_setter_ident);
f614132b 9428 }
668ea4b1
IS
9429
9430 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
f614132b 9431 parser->error = false;
668ea4b1
IS
9432}
9433
da57d1b9
NP
9434/* Parse an Objective-C @synthesize declaration. The syntax is:
9435
9436 objc-synthesize-declaration:
9437 @synthesize objc-synthesize-identifier-list ;
9438
9439 objc-synthesize-identifier-list:
9440 objc-synthesize-identifier
9441 objc-synthesize-identifier-list, objc-synthesize-identifier
9442
9443 objc-synthesize-identifier
9444 identifier
9445 identifier = identifier
9446
9447 For example:
9448 @synthesize MyProperty;
9449 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
9450
9451 PS: This function is identical to cp_parser_objc_at_synthesize_declaration
9452 for C++. Keep them in sync.
9453*/
9454static void
9455c_parser_objc_at_synthesize_declaration (c_parser *parser)
9456{
9457 tree list = NULL_TREE;
9458 location_t loc;
9459 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_SYNTHESIZE));
9460 loc = c_parser_peek_token (parser)->location;
9461
9462 c_parser_consume_token (parser);
9463 while (true)
9464 {
9465 tree property, ivar;
9466 if (c_parser_next_token_is_not (parser, CPP_NAME))
9467 {
9468 c_parser_error (parser, "expected identifier");
9469 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
9470 /* Once we find the semicolon, we can resume normal parsing.
9471 We have to reset parser->error manually because
9472 c_parser_skip_until_found() won't reset it for us if the
9473 next token is precisely a semicolon. */
9474 parser->error = false;
9475 return;
9476 }
9477 property = c_parser_peek_token (parser)->value;
9478 c_parser_consume_token (parser);
9479 if (c_parser_next_token_is (parser, CPP_EQ))
9480 {
9481 c_parser_consume_token (parser);
9482 if (c_parser_next_token_is_not (parser, CPP_NAME))
9483 {
9484 c_parser_error (parser, "expected identifier");
9485 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
9486 parser->error = false;
9487 return;
9488 }
9489 ivar = c_parser_peek_token (parser)->value;
9490 c_parser_consume_token (parser);
9491 }
9492 else
9493 ivar = NULL_TREE;
9494 list = chainon (list, build_tree_list (ivar, property));
9495 if (c_parser_next_token_is (parser, CPP_COMMA))
9496 c_parser_consume_token (parser);
9497 else
9498 break;
9499 }
9500 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
9501 objc_add_synthesize_declaration (loc, list);
9502}
9503
9504/* Parse an Objective-C @dynamic declaration. The syntax is:
9505
9506 objc-dynamic-declaration:
9507 @dynamic identifier-list ;
9508
9509 For example:
9510 @dynamic MyProperty;
9511 @dynamic MyProperty, AnotherProperty;
9512
9513 PS: This function is identical to cp_parser_objc_at_dynamic_declaration
9514 for C++. Keep them in sync.
9515*/
9516static void
9517c_parser_objc_at_dynamic_declaration (c_parser *parser)
9518{
9519 tree list = NULL_TREE;
9520 location_t loc;
9521 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_DYNAMIC));
9522 loc = c_parser_peek_token (parser)->location;
9523
9524 c_parser_consume_token (parser);
9525 while (true)
9526 {
9527 tree property;
9528 if (c_parser_next_token_is_not (parser, CPP_NAME))
9529 {
9530 c_parser_error (parser, "expected identifier");
9531 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
9532 parser->error = false;
9533 return;
9534 }
9535 property = c_parser_peek_token (parser)->value;
9536 list = chainon (list, build_tree_list (NULL_TREE, property));
9537 c_parser_consume_token (parser);
9538 if (c_parser_next_token_is (parser, CPP_COMMA))
9539 c_parser_consume_token (parser);
9540 else
9541 break;
9542 }
9543 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
9544 objc_add_dynamic_declaration (loc, list);
9545}
9546
27bf414c 9547\f
953ff289
DN
9548/* Handle pragmas. Some OpenMP pragmas are associated with, and therefore
9549 should be considered, statements. ALLOW_STMT is true if we're within
9550 the context of a function and such pragmas are to be allowed. Returns
9551 true if we actually parsed such a pragma. */
27bf414c 9552
bc4071dd 9553static bool
953ff289 9554c_parser_pragma (c_parser *parser, enum pragma_context context)
bc4071dd
RH
9555{
9556 unsigned int id;
9557
9558 id = c_parser_peek_token (parser)->pragma_kind;
9559 gcc_assert (id != PRAGMA_NONE);
9560
9561 switch (id)
9562 {
953ff289
DN
9563 case PRAGMA_OMP_BARRIER:
9564 if (context != pragma_compound)
9565 {
9566 if (context == pragma_stmt)
9567 c_parser_error (parser, "%<#pragma omp barrier%> may only be "
9568 "used in compound statements");
9569 goto bad_stmt;
9570 }
9571 c_parser_omp_barrier (parser);
9572 return false;
9573
9574 case PRAGMA_OMP_FLUSH:
9575 if (context != pragma_compound)
9576 {
9577 if (context == pragma_stmt)
9578 c_parser_error (parser, "%<#pragma omp flush%> may only be "
9579 "used in compound statements");
9580 goto bad_stmt;
9581 }
9582 c_parser_omp_flush (parser);
9583 return false;
9584
a68ab351
JJ
9585 case PRAGMA_OMP_TASKWAIT:
9586 if (context != pragma_compound)
9587 {
9588 if (context == pragma_stmt)
9589 c_parser_error (parser, "%<#pragma omp taskwait%> may only be "
9590 "used in compound statements");
9591 goto bad_stmt;
9592 }
9593 c_parser_omp_taskwait (parser);
9594 return false;
9595
20906c66
JJ
9596 case PRAGMA_OMP_TASKYIELD:
9597 if (context != pragma_compound)
9598 {
9599 if (context == pragma_stmt)
9600 c_parser_error (parser, "%<#pragma omp taskyield%> may only be "
9601 "used in compound statements");
9602 goto bad_stmt;
9603 }
9604 c_parser_omp_taskyield (parser);
9605 return false;
9606
acf0174b
JJ
9607 case PRAGMA_OMP_CANCEL:
9608 if (context != pragma_compound)
9609 {
9610 if (context == pragma_stmt)
9611 c_parser_error (parser, "%<#pragma omp cancel%> may only be "
9612 "used in compound statements");
9613 goto bad_stmt;
9614 }
9615 c_parser_omp_cancel (parser);
9616 return false;
9617
9618 case PRAGMA_OMP_CANCELLATION_POINT:
9619 if (context != pragma_compound)
9620 {
9621 if (context == pragma_stmt)
9622 c_parser_error (parser, "%<#pragma omp cancellation point%> may "
9623 "only be used in compound statements");
9624 goto bad_stmt;
9625 }
9626 c_parser_omp_cancellation_point (parser);
9627 return false;
9628
953ff289
DN
9629 case PRAGMA_OMP_THREADPRIVATE:
9630 c_parser_omp_threadprivate (parser);
9631 return false;
9632
acf0174b
JJ
9633 case PRAGMA_OMP_TARGET:
9634 return c_parser_omp_target (parser, context);
9635
9636 case PRAGMA_OMP_END_DECLARE_TARGET:
9637 c_parser_omp_end_declare_target (parser);
9638 return false;
9639
953ff289 9640 case PRAGMA_OMP_SECTION:
3ba09659
AH
9641 error_at (c_parser_peek_token (parser)->location,
9642 "%<#pragma omp section%> may only be used in "
9643 "%<#pragma omp sections%> construct");
953ff289
DN
9644 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
9645 return false;
9646
acf0174b
JJ
9647 case PRAGMA_OMP_DECLARE_REDUCTION:
9648 c_parser_omp_declare (parser, context);
9649 return false;
8170608b
TB
9650 case PRAGMA_IVDEP:
9651 c_parser_consume_pragma (parser);
9652 c_parser_skip_to_pragma_eol (parser);
d4af74d4
TB
9653 if (!c_parser_next_token_is_keyword (parser, RID_FOR)
9654 && !c_parser_next_token_is_keyword (parser, RID_WHILE)
9655 && !c_parser_next_token_is_keyword (parser, RID_DO))
8170608b 9656 {
d4af74d4 9657 c_parser_error (parser, "for, while or do statement expected");
8170608b
TB
9658 return false;
9659 }
d4af74d4
TB
9660 if (c_parser_next_token_is_keyword (parser, RID_FOR))
9661 c_parser_for_statement (parser, true);
9662 else if (c_parser_next_token_is_keyword (parser, RID_WHILE))
9663 c_parser_while_statement (parser, true);
9664 else
9665 c_parser_do_statement (parser, true);
8170608b 9666 return false;
acf0174b 9667
bc4071dd
RH
9668 case PRAGMA_GCC_PCH_PREPROCESS:
9669 c_parser_error (parser, "%<#pragma GCC pch_preprocess%> must be first");
9670 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
9671 return false;
9672
c02065fc
AH
9673 case PRAGMA_CILK_SIMD:
9674 if (!c_parser_cilk_verify_simd (parser, context))
9675 return false;
9676 c_parser_consume_pragma (parser);
9677 c_parser_cilk_simd (parser);
9678 return false;
9a771876
JJ
9679 case PRAGMA_CILK_GRAINSIZE:
9680 if (!flag_cilkplus)
9681 {
9682 warning (0, "%<#pragma grainsize%> ignored because -fcilkplus is not"
9683 " enabled");
9684 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
9685 return false;
9686 }
9687 if (context == pragma_external)
9688 {
9689 error_at (c_parser_peek_token (parser)->location,
9690 "%<#pragma grainsize%> must be inside a function");
9691 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
9692 return false;
9693 }
9694 c_parser_cilk_grainsize (parser);
9695 return false;
c02065fc 9696
bc4071dd 9697 default:
953ff289
DN
9698 if (id < PRAGMA_FIRST_EXTERNAL)
9699 {
acf0174b 9700 if (context != pragma_stmt && context != pragma_compound)
953ff289
DN
9701 {
9702 bad_stmt:
9703 c_parser_error (parser, "expected declaration specifiers");
9704 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
9705 return false;
9706 }
9707 c_parser_omp_construct (parser);
9708 return true;
9709 }
bc4071dd
RH
9710 break;
9711 }
9712
9713 c_parser_consume_pragma (parser);
9714 c_invoke_pragma_handler (id);
27bf414c 9715
b8698a0f 9716 /* Skip to EOL, but suppress any error message. Those will have been
bc4071dd
RH
9717 generated by the handler routine through calling error, as opposed
9718 to calling c_parser_error. */
9719 parser->error = true;
9720 c_parser_skip_to_pragma_eol (parser);
9721
9722 return false;
9723}
9724
9725/* The interface the pragma parsers have to the lexer. */
9726
9727enum cpp_ttype
9728pragma_lex (tree *value)
9729{
9730 c_token *tok = c_parser_peek_token (the_parser);
9731 enum cpp_ttype ret = tok->type;
9732
9733 *value = tok->value;
9734 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
9735 ret = CPP_EOF;
9736 else
9737 {
9738 if (ret == CPP_KEYWORD)
9739 ret = CPP_NAME;
9740 c_parser_consume_token (the_parser);
9741 }
9742
9743 return ret;
9744}
9745
9746static void
9747c_parser_pragma_pch_preprocess (c_parser *parser)
9748{
9749 tree name = NULL;
9750
9751 c_parser_consume_pragma (parser);
9752 if (c_parser_next_token_is (parser, CPP_STRING))
9753 {
9754 name = c_parser_peek_token (parser)->value;
9755 c_parser_consume_token (parser);
9756 }
9757 else
9758 c_parser_error (parser, "expected string literal");
9759 c_parser_skip_to_pragma_eol (parser);
9760
9761 if (name)
9762 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
9763}
953ff289 9764\f
acf0174b 9765/* OpenMP 2.5 / 3.0 / 3.1 / 4.0 parsing routines. */
953ff289
DN
9766
9767/* Returns name of the next clause.
9768 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
9769 the token is not consumed. Otherwise appropriate pragma_omp_clause is
9770 returned and the token is consumed. */
9771
9772static pragma_omp_clause
9773c_parser_omp_clause_name (c_parser *parser)
9774{
9775 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
9776
9777 if (c_parser_next_token_is_keyword (parser, RID_IF))
9778 result = PRAGMA_OMP_CLAUSE_IF;
9779 else if (c_parser_next_token_is_keyword (parser, RID_DEFAULT))
9780 result = PRAGMA_OMP_CLAUSE_DEFAULT;
acf0174b
JJ
9781 else if (c_parser_next_token_is_keyword (parser, RID_FOR))
9782 result = PRAGMA_OMP_CLAUSE_FOR;
953ff289
DN
9783 else if (c_parser_next_token_is (parser, CPP_NAME))
9784 {
9785 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
9786
9787 switch (p[0])
9788 {
acf0174b
JJ
9789 case 'a':
9790 if (!strcmp ("aligned", p))
9791 result = PRAGMA_OMP_CLAUSE_ALIGNED;
9792 break;
953ff289 9793 case 'c':
a68ab351
JJ
9794 if (!strcmp ("collapse", p))
9795 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
9796 else if (!strcmp ("copyin", p))
953ff289
DN
9797 result = PRAGMA_OMP_CLAUSE_COPYIN;
9798 else if (!strcmp ("copyprivate", p))
9799 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
9800 break;
acf0174b
JJ
9801 case 'd':
9802 if (!strcmp ("depend", p))
9803 result = PRAGMA_OMP_CLAUSE_DEPEND;
9804 else if (!strcmp ("device", p))
9805 result = PRAGMA_OMP_CLAUSE_DEVICE;
9806 else if (!strcmp ("dist_schedule", p))
9807 result = PRAGMA_OMP_CLAUSE_DIST_SCHEDULE;
9808 break;
953ff289 9809 case 'f':
20906c66
JJ
9810 if (!strcmp ("final", p))
9811 result = PRAGMA_OMP_CLAUSE_FINAL;
9812 else if (!strcmp ("firstprivate", p))
953ff289 9813 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
acf0174b
JJ
9814 else if (!strcmp ("from", p))
9815 result = PRAGMA_OMP_CLAUSE_FROM;
9816 break;
9817 case 'i':
9818 if (!strcmp ("inbranch", p))
9819 result = PRAGMA_OMP_CLAUSE_INBRANCH;
953ff289
DN
9820 break;
9821 case 'l':
9822 if (!strcmp ("lastprivate", p))
9823 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
acf0174b
JJ
9824 else if (!strcmp ("linear", p))
9825 result = PRAGMA_OMP_CLAUSE_LINEAR;
953ff289 9826 break;
20906c66 9827 case 'm':
acf0174b
JJ
9828 if (!strcmp ("map", p))
9829 result = PRAGMA_OMP_CLAUSE_MAP;
9830 else if (!strcmp ("mergeable", p))
20906c66 9831 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
b72271b9 9832 else if (flag_cilkplus && !strcmp ("mask", p))
41958c28 9833 result = PRAGMA_CILK_CLAUSE_MASK;
20906c66 9834 break;
953ff289 9835 case 'n':
acf0174b
JJ
9836 if (!strcmp ("notinbranch", p))
9837 result = PRAGMA_OMP_CLAUSE_NOTINBRANCH;
9838 else if (!strcmp ("nowait", p))
953ff289 9839 result = PRAGMA_OMP_CLAUSE_NOWAIT;
acf0174b
JJ
9840 else if (!strcmp ("num_teams", p))
9841 result = PRAGMA_OMP_CLAUSE_NUM_TEAMS;
953ff289
DN
9842 else if (!strcmp ("num_threads", p))
9843 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
b72271b9 9844 else if (flag_cilkplus && !strcmp ("nomask", p))
41958c28 9845 result = PRAGMA_CILK_CLAUSE_NOMASK;
953ff289
DN
9846 break;
9847 case 'o':
9848 if (!strcmp ("ordered", p))
9849 result = PRAGMA_OMP_CLAUSE_ORDERED;
9850 break;
9851 case 'p':
acf0174b
JJ
9852 if (!strcmp ("parallel", p))
9853 result = PRAGMA_OMP_CLAUSE_PARALLEL;
9854 else if (!strcmp ("private", p))
953ff289 9855 result = PRAGMA_OMP_CLAUSE_PRIVATE;
acf0174b
JJ
9856 else if (!strcmp ("proc_bind", p))
9857 result = PRAGMA_OMP_CLAUSE_PROC_BIND;
953ff289
DN
9858 break;
9859 case 'r':
9860 if (!strcmp ("reduction", p))
9861 result = PRAGMA_OMP_CLAUSE_REDUCTION;
9862 break;
9863 case 's':
acf0174b
JJ
9864 if (!strcmp ("safelen", p))
9865 result = PRAGMA_OMP_CLAUSE_SAFELEN;
9866 else if (!strcmp ("schedule", p))
953ff289 9867 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
acf0174b
JJ
9868 else if (!strcmp ("sections", p))
9869 result = PRAGMA_OMP_CLAUSE_SECTIONS;
953ff289
DN
9870 else if (!strcmp ("shared", p))
9871 result = PRAGMA_OMP_CLAUSE_SHARED;
acf0174b
JJ
9872 else if (!strcmp ("simdlen", p))
9873 result = PRAGMA_OMP_CLAUSE_SIMDLEN;
9874 break;
9875 case 't':
9876 if (!strcmp ("taskgroup", p))
9877 result = PRAGMA_OMP_CLAUSE_TASKGROUP;
9878 else if (!strcmp ("thread_limit", p))
9879 result = PRAGMA_OMP_CLAUSE_THREAD_LIMIT;
9880 else if (!strcmp ("to", p))
9881 result = PRAGMA_OMP_CLAUSE_TO;
953ff289 9882 break;
a68ab351 9883 case 'u':
acf0174b
JJ
9884 if (!strcmp ("uniform", p))
9885 result = PRAGMA_OMP_CLAUSE_UNIFORM;
9886 else if (!strcmp ("untied", p))
a68ab351
JJ
9887 result = PRAGMA_OMP_CLAUSE_UNTIED;
9888 break;
41958c28 9889 case 'v':
b72271b9 9890 if (flag_cilkplus && !strcmp ("vectorlength", p))
41958c28
BI
9891 result = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
9892 break;
953ff289
DN
9893 }
9894 }
9895
9896 if (result != PRAGMA_OMP_CLAUSE_NONE)
9897 c_parser_consume_token (parser);
9898
9899 return result;
9900}
9901
9902/* Validate that a clause of the given type does not already exist. */
9903
9904static void
d75d71e0
ILT
9905check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
9906 const char *name)
953ff289
DN
9907{
9908 tree c;
9909
9910 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
aaf46ef9 9911 if (OMP_CLAUSE_CODE (c) == code)
953ff289 9912 {
c2255bc4
AH
9913 location_t loc = OMP_CLAUSE_LOCATION (c);
9914 error_at (loc, "too many %qs clauses", name);
953ff289
DN
9915 break;
9916 }
9917}
9918
9919/* OpenMP 2.5:
9920 variable-list:
9921 identifier
9922 variable-list , identifier
9923
c2255bc4
AH
9924 If KIND is nonzero, create the appropriate node and install the
9925 decl in OMP_CLAUSE_DECL and add the node to the head of the list.
9926 If KIND is nonzero, CLAUSE_LOC is the location of the clause.
953ff289
DN
9927
9928 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
9929 return the list created. */
9930
9931static tree
c2255bc4
AH
9932c_parser_omp_variable_list (c_parser *parser,
9933 location_t clause_loc,
acf0174b 9934 enum omp_clause_code kind, tree list)
953ff289
DN
9935{
9936 if (c_parser_next_token_is_not (parser, CPP_NAME)
9937 || c_parser_peek_token (parser)->id_kind != C_ID_ID)
9938 c_parser_error (parser, "expected identifier");
9939
9940 while (c_parser_next_token_is (parser, CPP_NAME)
9941 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
9942 {
9943 tree t = lookup_name (c_parser_peek_token (parser)->value);
9944
9945 if (t == NULL_TREE)
acf0174b
JJ
9946 {
9947 undeclared_variable (c_parser_peek_token (parser)->location,
9948 c_parser_peek_token (parser)->value);
9949 t = error_mark_node;
9950 }
9951
9952 c_parser_consume_token (parser);
9953
9954 if (t == error_mark_node)
953ff289
DN
9955 ;
9956 else if (kind != 0)
9957 {
acf0174b
JJ
9958 switch (kind)
9959 {
9960 case OMP_CLAUSE_MAP:
9961 case OMP_CLAUSE_FROM:
9962 case OMP_CLAUSE_TO:
9963 case OMP_CLAUSE_DEPEND:
9964 while (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
9965 {
9966 tree low_bound = NULL_TREE, length = NULL_TREE;
9967
9968 c_parser_consume_token (parser);
9969 if (!c_parser_next_token_is (parser, CPP_COLON))
d90c0a59
JJ
9970 {
9971 low_bound = c_parser_expression (parser).value;
9972 mark_exp_read (low_bound);
9973 }
acf0174b
JJ
9974 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
9975 length = integer_one_node;
9976 else
9977 {
9978 /* Look for `:'. */
9979 if (!c_parser_require (parser, CPP_COLON,
9980 "expected %<:%>"))
9981 {
9982 t = error_mark_node;
9983 break;
9984 }
9985 if (!c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
d90c0a59
JJ
9986 {
9987 length = c_parser_expression (parser).value;
9988 mark_exp_read (length);
9989 }
acf0174b
JJ
9990 }
9991 /* Look for the closing `]'. */
9992 if (!c_parser_require (parser, CPP_CLOSE_SQUARE,
9993 "expected %<]%>"))
9994 {
9995 t = error_mark_node;
9996 break;
9997 }
9998 t = tree_cons (low_bound, length, t);
9999 }
10000 break;
10001 default:
10002 break;
10003 }
10004
10005 if (t != error_mark_node)
10006 {
10007 tree u = build_omp_clause (clause_loc, kind);
10008 OMP_CLAUSE_DECL (u) = t;
10009 OMP_CLAUSE_CHAIN (u) = list;
10010 list = u;
10011 }
953ff289
DN
10012 }
10013 else
10014 list = tree_cons (t, NULL_TREE, list);
10015
953ff289
DN
10016 if (c_parser_next_token_is_not (parser, CPP_COMMA))
10017 break;
10018
10019 c_parser_consume_token (parser);
10020 }
10021
10022 return list;
10023}
10024
10025/* Similarly, but expect leading and trailing parenthesis. This is a very
10026 common case for omp clauses. */
10027
10028static tree
d75d71e0
ILT
10029c_parser_omp_var_list_parens (c_parser *parser, enum omp_clause_code kind,
10030 tree list)
953ff289 10031{
c2255bc4
AH
10032 /* The clauses location. */
10033 location_t loc = c_parser_peek_token (parser)->location;
10034
953ff289
DN
10035 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10036 {
c2255bc4 10037 list = c_parser_omp_variable_list (parser, loc, kind, list);
953ff289
DN
10038 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10039 }
10040 return list;
10041}
10042
a68ab351
JJ
10043/* OpenMP 3.0:
10044 collapse ( constant-expression ) */
10045
10046static tree
10047c_parser_omp_clause_collapse (c_parser *parser, tree list)
10048{
10049 tree c, num = error_mark_node;
10050 HOST_WIDE_INT n;
10051 location_t loc;
10052
10053 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse");
10054
10055 loc = c_parser_peek_token (parser)->location;
10056 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10057 {
10058 num = c_parser_expr_no_commas (parser, NULL).value;
10059 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10060 }
10061 if (num == error_mark_node)
10062 return list;
acf0174b
JJ
10063 mark_exp_read (num);
10064 num = c_fully_fold (num, false, NULL);
a68ab351 10065 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
9541ffee 10066 || !tree_fits_shwi_p (num)
9439e9a1 10067 || (n = tree_to_shwi (num)) <= 0
a68ab351
JJ
10068 || (int) n != n)
10069 {
3ba09659
AH
10070 error_at (loc,
10071 "collapse argument needs positive constant integer expression");
a68ab351
JJ
10072 return list;
10073 }
c2255bc4 10074 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
a68ab351
JJ
10075 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
10076 OMP_CLAUSE_CHAIN (c) = list;
10077 return c;
10078}
10079
953ff289
DN
10080/* OpenMP 2.5:
10081 copyin ( variable-list ) */
10082
10083static tree
10084c_parser_omp_clause_copyin (c_parser *parser, tree list)
10085{
10086 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_COPYIN, list);
10087}
10088
10089/* OpenMP 2.5:
10090 copyprivate ( variable-list ) */
10091
10092static tree
10093c_parser_omp_clause_copyprivate (c_parser *parser, tree list)
10094{
10095 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_COPYPRIVATE, list);
10096}
10097
10098/* OpenMP 2.5:
10099 default ( shared | none ) */
10100
10101static tree
10102c_parser_omp_clause_default (c_parser *parser, tree list)
10103{
10104 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
c2255bc4 10105 location_t loc = c_parser_peek_token (parser)->location;
953ff289
DN
10106 tree c;
10107
10108 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10109 return list;
10110 if (c_parser_next_token_is (parser, CPP_NAME))
10111 {
10112 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
10113
10114 switch (p[0])
10115 {
10116 case 'n':
10117 if (strcmp ("none", p) != 0)
10118 goto invalid_kind;
10119 kind = OMP_CLAUSE_DEFAULT_NONE;
10120 break;
10121
10122 case 's':
10123 if (strcmp ("shared", p) != 0)
10124 goto invalid_kind;
10125 kind = OMP_CLAUSE_DEFAULT_SHARED;
10126 break;
10127
10128 default:
10129 goto invalid_kind;
10130 }
10131
10132 c_parser_consume_token (parser);
10133 }
10134 else
10135 {
10136 invalid_kind:
10137 c_parser_error (parser, "expected %<none%> or %<shared%>");
10138 }
10139 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10140
10141 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
10142 return list;
10143
10144 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default");
c2255bc4 10145 c = build_omp_clause (loc, OMP_CLAUSE_DEFAULT);
953ff289
DN
10146 OMP_CLAUSE_CHAIN (c) = list;
10147 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
10148
10149 return c;
10150}
10151
10152/* OpenMP 2.5:
10153 firstprivate ( variable-list ) */
10154
10155static tree
10156c_parser_omp_clause_firstprivate (c_parser *parser, tree list)
10157{
10158 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_FIRSTPRIVATE, list);
10159}
10160
20906c66
JJ
10161/* OpenMP 3.1:
10162 final ( expression ) */
10163
10164static tree
10165c_parser_omp_clause_final (c_parser *parser, tree list)
10166{
10167 location_t loc = c_parser_peek_token (parser)->location;
10168 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
10169 {
10170 tree t = c_parser_paren_condition (parser);
10171 tree c;
10172
10173 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final");
10174
10175 c = build_omp_clause (loc, OMP_CLAUSE_FINAL);
10176 OMP_CLAUSE_FINAL_EXPR (c) = t;
10177 OMP_CLAUSE_CHAIN (c) = list;
10178 list = c;
10179 }
10180 else
10181 c_parser_error (parser, "expected %<(%>");
10182
10183 return list;
10184}
10185
953ff289
DN
10186/* OpenMP 2.5:
10187 if ( expression ) */
10188
10189static tree
10190c_parser_omp_clause_if (c_parser *parser, tree list)
10191{
c2255bc4 10192 location_t loc = c_parser_peek_token (parser)->location;
953ff289
DN
10193 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
10194 {
10195 tree t = c_parser_paren_condition (parser);
10196 tree c;
10197
10198 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if");
10199
c2255bc4 10200 c = build_omp_clause (loc, OMP_CLAUSE_IF);
953ff289
DN
10201 OMP_CLAUSE_IF_EXPR (c) = t;
10202 OMP_CLAUSE_CHAIN (c) = list;
10203 list = c;
10204 }
10205 else
10206 c_parser_error (parser, "expected %<(%>");
10207
10208 return list;
10209}
10210
10211/* OpenMP 2.5:
10212 lastprivate ( variable-list ) */
10213
10214static tree
10215c_parser_omp_clause_lastprivate (c_parser *parser, tree list)
10216{
10217 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_LASTPRIVATE, list);
10218}
10219
20906c66
JJ
10220/* OpenMP 3.1:
10221 mergeable */
10222
10223static tree
10224c_parser_omp_clause_mergeable (c_parser *parser ATTRIBUTE_UNUSED, tree list)
10225{
10226 tree c;
10227
10228 /* FIXME: Should we allow duplicates? */
10229 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable");
10230
10231 c = build_omp_clause (c_parser_peek_token (parser)->location,
10232 OMP_CLAUSE_MERGEABLE);
10233 OMP_CLAUSE_CHAIN (c) = list;
10234
10235 return c;
10236}
10237
953ff289
DN
10238/* OpenMP 2.5:
10239 nowait */
10240
10241static tree
10242c_parser_omp_clause_nowait (c_parser *parser ATTRIBUTE_UNUSED, tree list)
10243{
10244 tree c;
c2255bc4 10245 location_t loc = c_parser_peek_token (parser)->location;
953ff289
DN
10246
10247 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait");
10248
c2255bc4 10249 c = build_omp_clause (loc, OMP_CLAUSE_NOWAIT);
953ff289
DN
10250 OMP_CLAUSE_CHAIN (c) = list;
10251 return c;
10252}
10253
10254/* OpenMP 2.5:
10255 num_threads ( expression ) */
10256
10257static tree
10258c_parser_omp_clause_num_threads (c_parser *parser, tree list)
10259{
c2255bc4 10260 location_t num_threads_loc = c_parser_peek_token (parser)->location;
953ff289
DN
10261 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10262 {
c7412148 10263 location_t expr_loc = c_parser_peek_token (parser)->location;
953ff289 10264 tree c, t = c_parser_expression (parser).value;
7d1362bc 10265 mark_exp_read (t);
928c19bb 10266 t = c_fully_fold (t, false, NULL);
953ff289
DN
10267
10268 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10269
10270 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
10271 {
10272 c_parser_error (parser, "expected integer expression");
10273 return list;
10274 }
10275
10276 /* Attempt to statically determine when the number isn't positive. */
db3927fb 10277 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
953ff289 10278 build_int_cst (TREE_TYPE (t), 0));
c2255bc4
AH
10279 if (CAN_HAVE_LOCATION_P (c))
10280 SET_EXPR_LOCATION (c, expr_loc);
953ff289
DN
10281 if (c == boolean_true_node)
10282 {
3ba09659
AH
10283 warning_at (expr_loc, 0,
10284 "%<num_threads%> value must be positive");
953ff289
DN
10285 t = integer_one_node;
10286 }
10287
10288 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS, "num_threads");
10289
c2255bc4 10290 c = build_omp_clause (num_threads_loc, OMP_CLAUSE_NUM_THREADS);
953ff289
DN
10291 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
10292 OMP_CLAUSE_CHAIN (c) = list;
10293 list = c;
10294 }
10295
10296 return list;
10297}
10298
10299/* OpenMP 2.5:
10300 ordered */
10301
10302static tree
c2255bc4 10303c_parser_omp_clause_ordered (c_parser *parser, tree list)
953ff289
DN
10304{
10305 tree c;
10306
10307 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED, "ordered");
10308
c2255bc4
AH
10309 c = build_omp_clause (c_parser_peek_token (parser)->location,
10310 OMP_CLAUSE_ORDERED);
953ff289 10311 OMP_CLAUSE_CHAIN (c) = list;
c2255bc4 10312
953ff289
DN
10313 return c;
10314}
10315
10316/* OpenMP 2.5:
10317 private ( variable-list ) */
10318
10319static tree
10320c_parser_omp_clause_private (c_parser *parser, tree list)
10321{
10322 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_PRIVATE, list);
10323}
10324
10325/* OpenMP 2.5:
10326 reduction ( reduction-operator : variable-list )
10327
10328 reduction-operator:
20906c66 10329 One of: + * - & ^ | && ||
acf0174b 10330
20906c66
JJ
10331 OpenMP 3.1:
10332
10333 reduction-operator:
acf0174b
JJ
10334 One of: + * - & ^ | && || max min
10335
10336 OpenMP 4.0:
10337
10338 reduction-operator:
10339 One of: + * - & ^ | && ||
10340 identifier */
953ff289
DN
10341
10342static tree
10343c_parser_omp_clause_reduction (c_parser *parser, tree list)
10344{
c2255bc4 10345 location_t clause_loc = c_parser_peek_token (parser)->location;
953ff289
DN
10346 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10347 {
acf0174b
JJ
10348 enum tree_code code = ERROR_MARK;
10349 tree reduc_id = NULL_TREE;
953ff289
DN
10350
10351 switch (c_parser_peek_token (parser)->type)
10352 {
10353 case CPP_PLUS:
10354 code = PLUS_EXPR;
10355 break;
10356 case CPP_MULT:
10357 code = MULT_EXPR;
10358 break;
10359 case CPP_MINUS:
10360 code = MINUS_EXPR;
10361 break;
10362 case CPP_AND:
10363 code = BIT_AND_EXPR;
10364 break;
10365 case CPP_XOR:
10366 code = BIT_XOR_EXPR;
10367 break;
10368 case CPP_OR:
10369 code = BIT_IOR_EXPR;
10370 break;
10371 case CPP_AND_AND:
10372 code = TRUTH_ANDIF_EXPR;
10373 break;
10374 case CPP_OR_OR:
10375 code = TRUTH_ORIF_EXPR;
10376 break;
20906c66
JJ
10377 case CPP_NAME:
10378 {
10379 const char *p
10380 = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
10381 if (strcmp (p, "min") == 0)
10382 {
10383 code = MIN_EXPR;
10384 break;
10385 }
10386 if (strcmp (p, "max") == 0)
10387 {
10388 code = MAX_EXPR;
10389 break;
10390 }
acf0174b
JJ
10391 reduc_id = c_parser_peek_token (parser)->value;
10392 break;
20906c66 10393 }
953ff289
DN
10394 default:
10395 c_parser_error (parser,
10396 "expected %<+%>, %<*%>, %<-%>, %<&%>, "
20906c66 10397 "%<^%>, %<|%>, %<&&%>, %<||%>, %<min%> or %<max%>");
953ff289
DN
10398 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 0);
10399 return list;
10400 }
10401 c_parser_consume_token (parser);
acf0174b 10402 reduc_id = c_omp_reduction_id (code, reduc_id);
953ff289
DN
10403 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
10404 {
10405 tree nl, c;
10406
c2255bc4
AH
10407 nl = c_parser_omp_variable_list (parser, clause_loc,
10408 OMP_CLAUSE_REDUCTION, list);
953ff289 10409 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
acf0174b
JJ
10410 {
10411 tree type = TREE_TYPE (OMP_CLAUSE_DECL (c));
10412 OMP_CLAUSE_REDUCTION_CODE (c) = code;
10413 if (code == ERROR_MARK
10414 || !(INTEGRAL_TYPE_P (type)
10415 || TREE_CODE (type) == REAL_TYPE
10416 || TREE_CODE (type) == COMPLEX_TYPE))
10417 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c)
10418 = c_omp_reduction_lookup (reduc_id,
10419 TYPE_MAIN_VARIANT (type));
10420 }
953ff289
DN
10421
10422 list = nl;
10423 }
10424 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10425 }
10426 return list;
10427}
10428
10429/* OpenMP 2.5:
10430 schedule ( schedule-kind )
10431 schedule ( schedule-kind , expression )
10432
10433 schedule-kind:
a68ab351 10434 static | dynamic | guided | runtime | auto
953ff289
DN
10435*/
10436
10437static tree
10438c_parser_omp_clause_schedule (c_parser *parser, tree list)
10439{
10440 tree c, t;
c2255bc4 10441 location_t loc = c_parser_peek_token (parser)->location;
953ff289
DN
10442
10443 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10444 return list;
10445
c2255bc4 10446 c = build_omp_clause (loc, OMP_CLAUSE_SCHEDULE);
953ff289
DN
10447
10448 if (c_parser_next_token_is (parser, CPP_NAME))
10449 {
10450 tree kind = c_parser_peek_token (parser)->value;
10451 const char *p = IDENTIFIER_POINTER (kind);
10452
10453 switch (p[0])
10454 {
10455 case 'd':
10456 if (strcmp ("dynamic", p) != 0)
10457 goto invalid_kind;
10458 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
10459 break;
10460
10461 case 'g':
10462 if (strcmp ("guided", p) != 0)
10463 goto invalid_kind;
10464 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
10465 break;
10466
10467 case 'r':
10468 if (strcmp ("runtime", p) != 0)
10469 goto invalid_kind;
10470 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
10471 break;
10472
10473 default:
10474 goto invalid_kind;
10475 }
10476 }
10477 else if (c_parser_next_token_is_keyword (parser, RID_STATIC))
10478 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
a68ab351
JJ
10479 else if (c_parser_next_token_is_keyword (parser, RID_AUTO))
10480 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
953ff289
DN
10481 else
10482 goto invalid_kind;
10483
10484 c_parser_consume_token (parser);
10485 if (c_parser_next_token_is (parser, CPP_COMMA))
10486 {
c7412148 10487 location_t here;
953ff289
DN
10488 c_parser_consume_token (parser);
10489
c7412148 10490 here = c_parser_peek_token (parser)->location;
953ff289 10491 t = c_parser_expr_no_commas (parser, NULL).value;
7d1362bc 10492 mark_exp_read (t);
928c19bb 10493 t = c_fully_fold (t, false, NULL);
953ff289
DN
10494
10495 if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
3ba09659
AH
10496 error_at (here, "schedule %<runtime%> does not take "
10497 "a %<chunk_size%> parameter");
a68ab351 10498 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
3ba09659
AH
10499 error_at (here,
10500 "schedule %<auto%> does not take "
10501 "a %<chunk_size%> parameter");
953ff289
DN
10502 else if (TREE_CODE (TREE_TYPE (t)) == INTEGER_TYPE)
10503 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
10504 else
10505 c_parser_error (parser, "expected integer expression");
10506
10507 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10508 }
10509 else
10510 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
10511 "expected %<,%> or %<)%>");
10512
10513 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule");
10514 OMP_CLAUSE_CHAIN (c) = list;
10515 return c;
10516
10517 invalid_kind:
10518 c_parser_error (parser, "invalid schedule kind");
10519 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 0);
10520 return list;
10521}
10522
10523/* OpenMP 2.5:
10524 shared ( variable-list ) */
10525
10526static tree
10527c_parser_omp_clause_shared (c_parser *parser, tree list)
10528{
10529 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_SHARED, list);
10530}
10531
a68ab351
JJ
10532/* OpenMP 3.0:
10533 untied */
10534
10535static tree
10536c_parser_omp_clause_untied (c_parser *parser ATTRIBUTE_UNUSED, tree list)
10537{
10538 tree c;
10539
10540 /* FIXME: Should we allow duplicates? */
10541 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied");
10542
c2255bc4
AH
10543 c = build_omp_clause (c_parser_peek_token (parser)->location,
10544 OMP_CLAUSE_UNTIED);
a68ab351 10545 OMP_CLAUSE_CHAIN (c) = list;
c2255bc4 10546
a68ab351
JJ
10547 return c;
10548}
10549
acf0174b
JJ
10550/* OpenMP 4.0:
10551 inbranch
10552 notinbranch */
953ff289
DN
10553
10554static tree
acf0174b
JJ
10555c_parser_omp_clause_branch (c_parser *parser ATTRIBUTE_UNUSED,
10556 enum omp_clause_code code, tree list)
953ff289 10557{
acf0174b 10558 check_no_duplicate_clause (list, code, omp_clause_code_name[code]);
953ff289 10559
acf0174b
JJ
10560 tree c = build_omp_clause (c_parser_peek_token (parser)->location, code);
10561 OMP_CLAUSE_CHAIN (c) = list;
953ff289 10562
acf0174b
JJ
10563 return c;
10564}
8085ca15 10565
acf0174b
JJ
10566/* OpenMP 4.0:
10567 parallel
10568 for
10569 sections
10570 taskgroup */
8085ca15 10571
acf0174b
JJ
10572static tree
10573c_parser_omp_clause_cancelkind (c_parser *parser ATTRIBUTE_UNUSED,
10574 enum omp_clause_code code, tree list)
10575{
10576 tree c = build_omp_clause (c_parser_peek_token (parser)->location, code);
10577 OMP_CLAUSE_CHAIN (c) = list;
10578
10579 return c;
10580}
10581
10582/* OpenMP 4.0:
10583 num_teams ( expression ) */
10584
10585static tree
10586c_parser_omp_clause_num_teams (c_parser *parser, tree list)
10587{
10588 location_t num_teams_loc = c_parser_peek_token (parser)->location;
10589 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10590 {
10591 location_t expr_loc = c_parser_peek_token (parser)->location;
10592 tree c, t = c_parser_expression (parser).value;
10593 mark_exp_read (t);
10594 t = c_fully_fold (t, false, NULL);
10595
10596 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10597
10598 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
953ff289 10599 {
acf0174b
JJ
10600 c_parser_error (parser, "expected integer expression");
10601 return list;
953ff289
DN
10602 }
10603
acf0174b
JJ
10604 /* Attempt to statically determine when the number isn't positive. */
10605 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
10606 build_int_cst (TREE_TYPE (t), 0));
10607 if (CAN_HAVE_LOCATION_P (c))
10608 SET_EXPR_LOCATION (c, expr_loc);
10609 if (c == boolean_true_node)
953ff289 10610 {
acf0174b
JJ
10611 warning_at (expr_loc, 0, "%<num_teams%> value must be positive");
10612 t = integer_one_node;
953ff289 10613 }
953ff289 10614
acf0174b 10615 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TEAMS, "num_teams");
953ff289 10616
acf0174b
JJ
10617 c = build_omp_clause (num_teams_loc, OMP_CLAUSE_NUM_TEAMS);
10618 OMP_CLAUSE_NUM_TEAMS_EXPR (c) = t;
10619 OMP_CLAUSE_CHAIN (c) = list;
10620 list = c;
10621 }
953ff289 10622
acf0174b
JJ
10623 return list;
10624}
953ff289 10625
acf0174b
JJ
10626/* OpenMP 4.0:
10627 thread_limit ( expression ) */
953ff289
DN
10628
10629static tree
acf0174b 10630c_parser_omp_clause_thread_limit (c_parser *parser, tree list)
953ff289 10631{
68c81f24 10632 location_t num_thread_limit_loc = c_parser_peek_token (parser)->location;
acf0174b
JJ
10633 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10634 {
10635 location_t expr_loc = c_parser_peek_token (parser)->location;
10636 tree c, t = c_parser_expression (parser).value;
10637 mark_exp_read (t);
10638 t = c_fully_fold (t, false, NULL);
953ff289 10639
acf0174b 10640 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
953ff289 10641
acf0174b
JJ
10642 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
10643 {
10644 c_parser_error (parser, "expected integer expression");
10645 return list;
10646 }
c2255bc4 10647
acf0174b
JJ
10648 /* Attempt to statically determine when the number isn't positive. */
10649 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
10650 build_int_cst (TREE_TYPE (t), 0));
10651 if (CAN_HAVE_LOCATION_P (c))
10652 SET_EXPR_LOCATION (c, expr_loc);
10653 if (c == boolean_true_node)
10654 {
10655 warning_at (expr_loc, 0, "%<thread_limit%> value must be positive");
10656 t = integer_one_node;
10657 }
20906c66 10658
acf0174b
JJ
10659 check_no_duplicate_clause (list, OMP_CLAUSE_THREAD_LIMIT,
10660 "thread_limit");
20906c66 10661
68c81f24 10662 c = build_omp_clause (num_thread_limit_loc, OMP_CLAUSE_THREAD_LIMIT);
acf0174b
JJ
10663 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
10664 OMP_CLAUSE_CHAIN (c) = list;
10665 list = c;
10666 }
20906c66 10667
acf0174b
JJ
10668 return list;
10669}
20906c66 10670
acf0174b
JJ
10671/* OpenMP 4.0:
10672 aligned ( variable-list )
10673 aligned ( variable-list : constant-expression ) */
20906c66 10674
acf0174b
JJ
10675static tree
10676c_parser_omp_clause_aligned (c_parser *parser, tree list)
10677{
10678 location_t clause_loc = c_parser_peek_token (parser)->location;
10679 tree nl, c;
20906c66 10680
acf0174b
JJ
10681 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10682 return list;
20906c66 10683
acf0174b
JJ
10684 nl = c_parser_omp_variable_list (parser, clause_loc,
10685 OMP_CLAUSE_ALIGNED, list);
20906c66 10686
acf0174b
JJ
10687 if (c_parser_next_token_is (parser, CPP_COLON))
10688 {
10689 c_parser_consume_token (parser);
10690 tree alignment = c_parser_expr_no_commas (parser, NULL).value;
10691 mark_exp_read (alignment);
10692 alignment = c_fully_fold (alignment, false, NULL);
10693 if (!INTEGRAL_TYPE_P (TREE_TYPE (alignment))
10694 && TREE_CODE (alignment) != INTEGER_CST
10695 && tree_int_cst_sgn (alignment) != 1)
10696 {
10697 error_at (clause_loc, "%<aligned%> clause alignment expression must "
10698 "be positive constant integer expression");
10699 alignment = NULL_TREE;
10700 }
953ff289 10701
acf0174b
JJ
10702 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
10703 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = alignment;
10704 }
10705
10706 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10707 return nl;
10708}
10709
10710/* OpenMP 4.0:
10711 linear ( variable-list )
10712 linear ( variable-list : expression ) */
10713
10714static tree
41958c28 10715c_parser_omp_clause_linear (c_parser *parser, tree list, bool is_cilk_simd_fn)
953ff289 10716{
acf0174b
JJ
10717 location_t clause_loc = c_parser_peek_token (parser)->location;
10718 tree nl, c, step;
10719
10720 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10721 return list;
10722
10723 nl = c_parser_omp_variable_list (parser, clause_loc,
10724 OMP_CLAUSE_LINEAR, list);
10725
10726 if (c_parser_next_token_is (parser, CPP_COLON))
10727 {
10728 c_parser_consume_token (parser);
10729 step = c_parser_expression (parser).value;
10730 mark_exp_read (step);
10731 step = c_fully_fold (step, false, NULL);
41958c28
BI
10732 if (is_cilk_simd_fn && TREE_CODE (step) == PARM_DECL)
10733 {
10734 sorry ("using parameters for %<linear%> step is not supported yet");
10735 step = integer_one_node;
10736 }
acf0174b
JJ
10737 if (!INTEGRAL_TYPE_P (TREE_TYPE (step)))
10738 {
10739 error_at (clause_loc, "%<linear%> clause step expression must "
10740 "be integral");
10741 step = integer_one_node;
10742 }
10743
10744 }
10745 else
10746 step = integer_one_node;
10747
10748 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
10749 {
10750 OMP_CLAUSE_LINEAR_STEP (c) = step;
10751 }
10752
10753 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10754 return nl;
10755}
10756
10757/* OpenMP 4.0:
10758 safelen ( constant-expression ) */
10759
10760static tree
10761c_parser_omp_clause_safelen (c_parser *parser, tree list)
10762{
10763 location_t clause_loc = c_parser_peek_token (parser)->location;
10764 tree c, t;
10765
10766 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10767 return list;
10768
10769 t = c_parser_expr_no_commas (parser, NULL).value;
10770 mark_exp_read (t);
10771 t = c_fully_fold (t, false, NULL);
10772 if (!INTEGRAL_TYPE_P (TREE_TYPE (t))
10773 && TREE_CODE (t) != INTEGER_CST
10774 && tree_int_cst_sgn (t) != 1)
10775 {
10776 error_at (clause_loc, "%<safelen%> clause expression must "
10777 "be positive constant integer expression");
10778 t = NULL_TREE;
10779 }
10780
10781 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10782 if (t == NULL_TREE || t == error_mark_node)
10783 return list;
10784
10785 check_no_duplicate_clause (list, OMP_CLAUSE_SAFELEN, "safelen");
10786
10787 c = build_omp_clause (clause_loc, OMP_CLAUSE_SAFELEN);
10788 OMP_CLAUSE_SAFELEN_EXPR (c) = t;
10789 OMP_CLAUSE_CHAIN (c) = list;
10790 return c;
10791}
10792
10793/* OpenMP 4.0:
10794 simdlen ( constant-expression ) */
10795
10796static tree
10797c_parser_omp_clause_simdlen (c_parser *parser, tree list)
10798{
10799 location_t clause_loc = c_parser_peek_token (parser)->location;
10800 tree c, t;
10801
10802 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10803 return list;
10804
10805 t = c_parser_expr_no_commas (parser, NULL).value;
10806 mark_exp_read (t);
10807 t = c_fully_fold (t, false, NULL);
10808 if (!INTEGRAL_TYPE_P (TREE_TYPE (t))
10809 && TREE_CODE (t) != INTEGER_CST
10810 && tree_int_cst_sgn (t) != 1)
10811 {
10812 error_at (clause_loc, "%<simdlen%> clause expression must "
10813 "be positive constant integer expression");
10814 t = NULL_TREE;
10815 }
10816
10817 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10818 if (t == NULL_TREE || t == error_mark_node)
10819 return list;
10820
10821 check_no_duplicate_clause (list, OMP_CLAUSE_SIMDLEN, "simdlen");
10822
10823 c = build_omp_clause (clause_loc, OMP_CLAUSE_SIMDLEN);
10824 OMP_CLAUSE_SIMDLEN_EXPR (c) = t;
10825 OMP_CLAUSE_CHAIN (c) = list;
10826 return c;
10827}
10828
10829/* OpenMP 4.0:
10830 depend ( depend-kind: variable-list )
10831
10832 depend-kind:
10833 in | out | inout */
10834
10835static tree
10836c_parser_omp_clause_depend (c_parser *parser, tree list)
10837{
10838 location_t clause_loc = c_parser_peek_token (parser)->location;
10839 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_INOUT;
10840 tree nl, c;
10841
10842 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10843 return list;
953ff289 10844
20906c66
JJ
10845 if (c_parser_next_token_is (parser, CPP_NAME))
10846 {
10847 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
acf0174b
JJ
10848 if (strcmp ("in", p) == 0)
10849 kind = OMP_CLAUSE_DEPEND_IN;
10850 else if (strcmp ("inout", p) == 0)
10851 kind = OMP_CLAUSE_DEPEND_INOUT;
10852 else if (strcmp ("out", p) == 0)
10853 kind = OMP_CLAUSE_DEPEND_OUT;
20906c66 10854 else
acf0174b 10855 goto invalid_kind;
20906c66 10856 }
acf0174b
JJ
10857 else
10858 goto invalid_kind;
953ff289 10859
acf0174b
JJ
10860 c_parser_consume_token (parser);
10861 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
10862 goto resync_fail;
10863
10864 nl = c_parser_omp_variable_list (parser, clause_loc,
10865 OMP_CLAUSE_DEPEND, list);
10866
10867 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
10868 OMP_CLAUSE_DEPEND_KIND (c) = kind;
10869
10870 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10871 return nl;
10872
10873 invalid_kind:
10874 c_parser_error (parser, "invalid depend kind");
10875 resync_fail:
10876 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10877 return list;
10878}
10879
10880/* OpenMP 4.0:
10881 map ( map-kind: variable-list )
10882 map ( variable-list )
10883
10884 map-kind:
10885 alloc | to | from | tofrom */
10886
10887static tree
10888c_parser_omp_clause_map (c_parser *parser, tree list)
10889{
10890 location_t clause_loc = c_parser_peek_token (parser)->location;
10891 enum omp_clause_map_kind kind = OMP_CLAUSE_MAP_TOFROM;
10892 tree nl, c;
10893
10894 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10895 return list;
10896
10897 if (c_parser_next_token_is (parser, CPP_NAME)
10898 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
20906c66 10899 {
acf0174b
JJ
10900 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
10901 if (strcmp ("alloc", p) == 0)
10902 kind = OMP_CLAUSE_MAP_ALLOC;
10903 else if (strcmp ("to", p) == 0)
10904 kind = OMP_CLAUSE_MAP_TO;
10905 else if (strcmp ("from", p) == 0)
10906 kind = OMP_CLAUSE_MAP_FROM;
10907 else if (strcmp ("tofrom", p) == 0)
10908 kind = OMP_CLAUSE_MAP_TOFROM;
20906c66
JJ
10909 else
10910 {
acf0174b
JJ
10911 c_parser_error (parser, "invalid map kind");
10912 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
10913 "expected %<)%>");
10914 return list;
20906c66 10915 }
acf0174b
JJ
10916 c_parser_consume_token (parser);
10917 c_parser_consume_token (parser);
20906c66
JJ
10918 }
10919
acf0174b
JJ
10920 nl = c_parser_omp_variable_list (parser, clause_loc, OMP_CLAUSE_MAP, list);
10921
10922 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
10923 OMP_CLAUSE_MAP_KIND (c) = kind;
10924
10925 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10926 return nl;
10927}
10928
10929/* OpenMP 4.0:
10930 device ( expression ) */
10931
10932static tree
10933c_parser_omp_clause_device (c_parser *parser, tree list)
10934{
10935 location_t clause_loc = c_parser_peek_token (parser)->location;
10936 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
953ff289 10937 {
acf0174b
JJ
10938 tree c, t = c_parser_expr_no_commas (parser, NULL).value;
10939 mark_exp_read (t);
10940 t = c_fully_fold (t, false, NULL);
10941
10942 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10943
10944 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
20906c66 10945 {
acf0174b
JJ
10946 c_parser_error (parser, "expected integer expression");
10947 return list;
20906c66 10948 }
953ff289 10949
acf0174b 10950 check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE, "device");
953ff289 10951
acf0174b
JJ
10952 c = build_omp_clause (clause_loc, OMP_CLAUSE_DEVICE);
10953 OMP_CLAUSE_DEVICE_ID (c) = t;
10954 OMP_CLAUSE_CHAIN (c) = list;
10955 list = c;
10956 }
953ff289 10957
acf0174b
JJ
10958 return list;
10959}
10960
10961/* OpenMP 4.0:
10962 dist_schedule ( static )
10963 dist_schedule ( static , expression ) */
10964
10965static tree
10966c_parser_omp_clause_dist_schedule (c_parser *parser, tree list)
10967{
10968 tree c, t = NULL_TREE;
10969 location_t loc = c_parser_peek_token (parser)->location;
10970
10971 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10972 return list;
10973
10974 if (!c_parser_next_token_is_keyword (parser, RID_STATIC))
10975 {
10976 c_parser_error (parser, "invalid dist_schedule kind");
10977 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
10978 "expected %<)%>");
10979 return list;
10980 }
10981
10982 c_parser_consume_token (parser);
10983 if (c_parser_next_token_is (parser, CPP_COMMA))
10984 {
10985 c_parser_consume_token (parser);
10986
10987 t = c_parser_expr_no_commas (parser, NULL).value;
10988 mark_exp_read (t);
10989 t = c_fully_fold (t, false, NULL);
10990 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10991 }
10992 else
10993 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
10994 "expected %<,%> or %<)%>");
10995
10996 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule");
10997 if (t == error_mark_node)
10998 return list;
10999
11000 c = build_omp_clause (loc, OMP_CLAUSE_DIST_SCHEDULE);
11001 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
11002 OMP_CLAUSE_CHAIN (c) = list;
11003 return c;
11004}
11005
11006/* OpenMP 4.0:
11007 proc_bind ( proc-bind-kind )
11008
11009 proc-bind-kind:
11010 master | close | spread */
11011
11012static tree
11013c_parser_omp_clause_proc_bind (c_parser *parser, tree list)
11014{
11015 location_t clause_loc = c_parser_peek_token (parser)->location;
11016 enum omp_clause_proc_bind_kind kind;
11017 tree c;
11018
11019 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11020 return list;
11021
11022 if (c_parser_next_token_is (parser, CPP_NAME))
11023 {
11024 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
11025 if (strcmp ("master", p) == 0)
11026 kind = OMP_CLAUSE_PROC_BIND_MASTER;
11027 else if (strcmp ("close", p) == 0)
11028 kind = OMP_CLAUSE_PROC_BIND_CLOSE;
11029 else if (strcmp ("spread", p) == 0)
11030 kind = OMP_CLAUSE_PROC_BIND_SPREAD;
11031 else
11032 goto invalid_kind;
11033 }
11034 else
11035 goto invalid_kind;
11036
11037 c_parser_consume_token (parser);
11038 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11039 c = build_omp_clause (clause_loc, OMP_CLAUSE_PROC_BIND);
11040 OMP_CLAUSE_PROC_BIND_KIND (c) = kind;
11041 OMP_CLAUSE_CHAIN (c) = list;
11042 return c;
11043
11044 invalid_kind:
11045 c_parser_error (parser, "invalid proc_bind kind");
11046 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11047 return list;
11048}
11049
11050/* OpenMP 4.0:
11051 to ( variable-list ) */
11052
11053static tree
11054c_parser_omp_clause_to (c_parser *parser, tree list)
11055{
11056 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_TO, list);
11057}
11058
11059/* OpenMP 4.0:
11060 from ( variable-list ) */
11061
11062static tree
11063c_parser_omp_clause_from (c_parser *parser, tree list)
11064{
11065 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_FROM, list);
11066}
11067
11068/* OpenMP 4.0:
11069 uniform ( variable-list ) */
11070
11071static tree
11072c_parser_omp_clause_uniform (c_parser *parser, tree list)
11073{
11074 /* The clauses location. */
11075 location_t loc = c_parser_peek_token (parser)->location;
11076
11077 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11078 {
11079 list = c_parser_omp_variable_list (parser, loc, OMP_CLAUSE_UNIFORM,
11080 list);
11081 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11082 }
11083 return list;
11084}
11085
11086/* Parse all OpenMP clauses. The set clauses allowed by the directive
11087 is a bitmask in MASK. Return the list of clauses found; the result
11088 of clause default goes in *pdefault. */
11089
11090static tree
11091c_parser_omp_all_clauses (c_parser *parser, omp_clause_mask mask,
11092 const char *where, bool finish_p = true)
11093{
11094 tree clauses = NULL;
41958c28 11095 bool first = true, cilk_simd_fn = false;
acf0174b
JJ
11096
11097 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
11098 {
11099 location_t here;
11100 pragma_omp_clause c_kind;
11101 const char *c_name;
11102 tree prev = clauses;
11103
11104 if (!first && c_parser_next_token_is (parser, CPP_COMMA))
11105 c_parser_consume_token (parser);
11106
11107 here = c_parser_peek_token (parser)->location;
11108 c_kind = c_parser_omp_clause_name (parser);
11109
11110 switch (c_kind)
953ff289 11111 {
acf0174b
JJ
11112 case PRAGMA_OMP_CLAUSE_COLLAPSE:
11113 clauses = c_parser_omp_clause_collapse (parser, clauses);
11114 c_name = "collapse";
953ff289 11115 break;
acf0174b
JJ
11116 case PRAGMA_OMP_CLAUSE_COPYIN:
11117 clauses = c_parser_omp_clause_copyin (parser, clauses);
11118 c_name = "copyin";
953ff289 11119 break;
acf0174b
JJ
11120 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
11121 clauses = c_parser_omp_clause_copyprivate (parser, clauses);
11122 c_name = "copyprivate";
953ff289 11123 break;
acf0174b
JJ
11124 case PRAGMA_OMP_CLAUSE_DEFAULT:
11125 clauses = c_parser_omp_clause_default (parser, clauses);
11126 c_name = "default";
953ff289 11127 break;
acf0174b
JJ
11128 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
11129 clauses = c_parser_omp_clause_firstprivate (parser, clauses);
11130 c_name = "firstprivate";
953ff289 11131 break;
acf0174b
JJ
11132 case PRAGMA_OMP_CLAUSE_FINAL:
11133 clauses = c_parser_omp_clause_final (parser, clauses);
11134 c_name = "final";
953ff289 11135 break;
acf0174b
JJ
11136 case PRAGMA_OMP_CLAUSE_IF:
11137 clauses = c_parser_omp_clause_if (parser, clauses);
11138 c_name = "if";
953ff289 11139 break;
acf0174b
JJ
11140 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
11141 clauses = c_parser_omp_clause_lastprivate (parser, clauses);
11142 c_name = "lastprivate";
953ff289 11143 break;
acf0174b
JJ
11144 case PRAGMA_OMP_CLAUSE_MERGEABLE:
11145 clauses = c_parser_omp_clause_mergeable (parser, clauses);
11146 c_name = "mergeable";
953ff289 11147 break;
acf0174b
JJ
11148 case PRAGMA_OMP_CLAUSE_NOWAIT:
11149 clauses = c_parser_omp_clause_nowait (parser, clauses);
11150 c_name = "nowait";
11151 break;
11152 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
11153 clauses = c_parser_omp_clause_num_threads (parser, clauses);
11154 c_name = "num_threads";
11155 break;
11156 case PRAGMA_OMP_CLAUSE_ORDERED:
11157 clauses = c_parser_omp_clause_ordered (parser, clauses);
11158 c_name = "ordered";
11159 break;
11160 case PRAGMA_OMP_CLAUSE_PRIVATE:
11161 clauses = c_parser_omp_clause_private (parser, clauses);
11162 c_name = "private";
11163 break;
11164 case PRAGMA_OMP_CLAUSE_REDUCTION:
11165 clauses = c_parser_omp_clause_reduction (parser, clauses);
11166 c_name = "reduction";
11167 break;
11168 case PRAGMA_OMP_CLAUSE_SCHEDULE:
11169 clauses = c_parser_omp_clause_schedule (parser, clauses);
11170 c_name = "schedule";
11171 break;
11172 case PRAGMA_OMP_CLAUSE_SHARED:
11173 clauses = c_parser_omp_clause_shared (parser, clauses);
11174 c_name = "shared";
11175 break;
11176 case PRAGMA_OMP_CLAUSE_UNTIED:
11177 clauses = c_parser_omp_clause_untied (parser, clauses);
11178 c_name = "untied";
11179 break;
11180 case PRAGMA_OMP_CLAUSE_INBRANCH:
41958c28 11181 case PRAGMA_CILK_CLAUSE_MASK:
acf0174b
JJ
11182 clauses = c_parser_omp_clause_branch (parser, OMP_CLAUSE_INBRANCH,
11183 clauses);
11184 c_name = "inbranch";
11185 break;
11186 case PRAGMA_OMP_CLAUSE_NOTINBRANCH:
41958c28 11187 case PRAGMA_CILK_CLAUSE_NOMASK:
acf0174b
JJ
11188 clauses = c_parser_omp_clause_branch (parser, OMP_CLAUSE_NOTINBRANCH,
11189 clauses);
11190 c_name = "notinbranch";
11191 break;
11192 case PRAGMA_OMP_CLAUSE_PARALLEL:
11193 clauses
11194 = c_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_PARALLEL,
11195 clauses);
11196 c_name = "parallel";
11197 if (!first)
20906c66 11198 {
acf0174b
JJ
11199 clause_not_first:
11200 error_at (here, "%qs must be the first clause of %qs",
11201 c_name, where);
11202 clauses = prev;
11203 }
11204 break;
11205 case PRAGMA_OMP_CLAUSE_FOR:
11206 clauses
11207 = c_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_FOR,
11208 clauses);
11209 c_name = "for";
11210 if (!first)
11211 goto clause_not_first;
11212 break;
11213 case PRAGMA_OMP_CLAUSE_SECTIONS:
11214 clauses
11215 = c_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_SECTIONS,
11216 clauses);
11217 c_name = "sections";
11218 if (!first)
11219 goto clause_not_first;
11220 break;
11221 case PRAGMA_OMP_CLAUSE_TASKGROUP:
11222 clauses
11223 = c_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_TASKGROUP,
11224 clauses);
11225 c_name = "taskgroup";
11226 if (!first)
11227 goto clause_not_first;
11228 break;
11229 case PRAGMA_OMP_CLAUSE_TO:
11230 clauses = c_parser_omp_clause_to (parser, clauses);
11231 c_name = "to";
11232 break;
11233 case PRAGMA_OMP_CLAUSE_FROM:
11234 clauses = c_parser_omp_clause_from (parser, clauses);
11235 c_name = "from";
11236 break;
11237 case PRAGMA_OMP_CLAUSE_UNIFORM:
11238 clauses = c_parser_omp_clause_uniform (parser, clauses);
11239 c_name = "uniform";
11240 break;
11241 case PRAGMA_OMP_CLAUSE_NUM_TEAMS:
11242 clauses = c_parser_omp_clause_num_teams (parser, clauses);
11243 c_name = "num_teams";
11244 break;
11245 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT:
11246 clauses = c_parser_omp_clause_thread_limit (parser, clauses);
11247 c_name = "thread_limit";
11248 break;
11249 case PRAGMA_OMP_CLAUSE_ALIGNED:
11250 clauses = c_parser_omp_clause_aligned (parser, clauses);
11251 c_name = "aligned";
11252 break;
41958c28
BI
11253 case PRAGMA_OMP_CLAUSE_LINEAR:
11254 if (((mask >> PRAGMA_CILK_CLAUSE_VECTORLENGTH) & 1) != 0)
11255 cilk_simd_fn = true;
11256 clauses = c_parser_omp_clause_linear (parser, clauses, cilk_simd_fn);
acf0174b
JJ
11257 c_name = "linear";
11258 break;
11259 case PRAGMA_OMP_CLAUSE_DEPEND:
11260 clauses = c_parser_omp_clause_depend (parser, clauses);
11261 c_name = "depend";
11262 break;
11263 case PRAGMA_OMP_CLAUSE_MAP:
11264 clauses = c_parser_omp_clause_map (parser, clauses);
11265 c_name = "map";
11266 break;
11267 case PRAGMA_OMP_CLAUSE_DEVICE:
11268 clauses = c_parser_omp_clause_device (parser, clauses);
11269 c_name = "device";
11270 break;
11271 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE:
11272 clauses = c_parser_omp_clause_dist_schedule (parser, clauses);
11273 c_name = "dist_schedule";
11274 break;
11275 case PRAGMA_OMP_CLAUSE_PROC_BIND:
11276 clauses = c_parser_omp_clause_proc_bind (parser, clauses);
11277 c_name = "proc_bind";
11278 break;
11279 case PRAGMA_OMP_CLAUSE_SAFELEN:
11280 clauses = c_parser_omp_clause_safelen (parser, clauses);
11281 c_name = "safelen";
11282 break;
41958c28
BI
11283 case PRAGMA_CILK_CLAUSE_VECTORLENGTH:
11284 clauses = c_parser_cilk_clause_vectorlength (parser, clauses, true);
11285 c_name = "simdlen";
11286 break;
acf0174b
JJ
11287 case PRAGMA_OMP_CLAUSE_SIMDLEN:
11288 clauses = c_parser_omp_clause_simdlen (parser, clauses);
11289 c_name = "simdlen";
11290 break;
953ff289 11291 default:
acf0174b 11292 c_parser_error (parser, "expected %<#pragma omp%> clause");
953ff289
DN
11293 goto saw_error;
11294 }
11295
acf0174b
JJ
11296 first = false;
11297
11298 if (((mask >> c_kind) & 1) == 0 && !parser->error)
11299 {
11300 /* Remove the invalid clause(s) from the list to avoid
11301 confusing the rest of the compiler. */
11302 clauses = prev;
11303 error_at (here, "%qs is not valid for %qs", c_name, where);
11304 }
11305 }
11306
11307 saw_error:
11308 c_parser_skip_to_pragma_eol (parser);
11309
11310 if (finish_p)
11311 return c_finish_omp_clauses (clauses);
11312
11313 return clauses;
11314}
11315
11316/* OpenMP 2.5:
11317 structured-block:
11318 statement
11319
11320 In practice, we're also interested in adding the statement to an
11321 outer node. So it is convenient if we work around the fact that
11322 c_parser_statement calls add_stmt. */
11323
11324static tree
11325c_parser_omp_structured_block (c_parser *parser)
11326{
11327 tree stmt = push_stmt_list ();
11328 c_parser_statement (parser);
11329 return pop_stmt_list (stmt);
11330}
11331
11332/* OpenMP 2.5:
11333 # pragma omp atomic new-line
11334 expression-stmt
11335
11336 expression-stmt:
11337 x binop= expr | x++ | ++x | x-- | --x
11338 binop:
11339 +, *, -, /, &, ^, |, <<, >>
11340
11341 where x is an lvalue expression with scalar type.
11342
11343 OpenMP 3.1:
11344 # pragma omp atomic new-line
11345 update-stmt
11346
11347 # pragma omp atomic read new-line
11348 read-stmt
11349
11350 # pragma omp atomic write new-line
11351 write-stmt
11352
11353 # pragma omp atomic update new-line
11354 update-stmt
11355
11356 # pragma omp atomic capture new-line
11357 capture-stmt
11358
11359 # pragma omp atomic capture new-line
11360 capture-block
11361
11362 read-stmt:
11363 v = x
11364 write-stmt:
11365 x = expr
11366 update-stmt:
11367 expression-stmt | x = x binop expr
11368 capture-stmt:
11369 v = expression-stmt
11370 capture-block:
11371 { v = x; update-stmt; } | { update-stmt; v = x; }
11372
11373 OpenMP 4.0:
11374 update-stmt:
11375 expression-stmt | x = x binop expr | x = expr binop x
11376 capture-stmt:
11377 v = update-stmt
11378 capture-block:
11379 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
11380
11381 where x and v are lvalue expressions with scalar type.
11382
11383 LOC is the location of the #pragma token. */
11384
11385static void
11386c_parser_omp_atomic (location_t loc, c_parser *parser)
11387{
11388 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE;
11389 tree lhs1 = NULL_TREE, rhs1 = NULL_TREE;
11390 tree stmt, orig_lhs, unfolded_lhs = NULL_TREE, unfolded_lhs1 = NULL_TREE;
11391 enum tree_code code = OMP_ATOMIC, opcode = NOP_EXPR;
11392 struct c_expr expr;
11393 location_t eloc;
11394 bool structured_block = false;
11395 bool swapped = false;
11396 bool seq_cst = false;
11397
42056eac
JJ
11398 if (c_parser_next_token_is (parser, CPP_NAME))
11399 {
11400 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
11401 if (!strcmp (p, "seq_cst"))
11402 {
11403 seq_cst = true;
11404 c_parser_consume_token (parser);
11405 if (c_parser_next_token_is (parser, CPP_COMMA)
11406 && c_parser_peek_2nd_token (parser)->type == CPP_NAME)
11407 c_parser_consume_token (parser);
11408 }
11409 }
acf0174b
JJ
11410 if (c_parser_next_token_is (parser, CPP_NAME))
11411 {
11412 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
11413
11414 if (!strcmp (p, "read"))
11415 code = OMP_ATOMIC_READ;
11416 else if (!strcmp (p, "write"))
11417 code = NOP_EXPR;
11418 else if (!strcmp (p, "update"))
11419 code = OMP_ATOMIC;
11420 else if (!strcmp (p, "capture"))
11421 code = OMP_ATOMIC_CAPTURE_NEW;
11422 else
11423 p = NULL;
11424 if (p)
11425 c_parser_consume_token (parser);
11426 }
42056eac 11427 if (!seq_cst)
acf0174b 11428 {
42056eac
JJ
11429 if (c_parser_next_token_is (parser, CPP_COMMA)
11430 && c_parser_peek_2nd_token (parser)->type == CPP_NAME)
11431 c_parser_consume_token (parser);
11432
11433 if (c_parser_next_token_is (parser, CPP_NAME))
acf0174b 11434 {
42056eac
JJ
11435 const char *p
11436 = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
11437 if (!strcmp (p, "seq_cst"))
11438 {
11439 seq_cst = true;
11440 c_parser_consume_token (parser);
11441 }
acf0174b
JJ
11442 }
11443 }
11444 c_parser_skip_to_pragma_eol (parser);
11445
11446 switch (code)
11447 {
11448 case OMP_ATOMIC_READ:
11449 case NOP_EXPR: /* atomic write */
11450 v = c_parser_unary_expression (parser).value;
11451 v = c_fully_fold (v, false, NULL);
11452 if (v == error_mark_node)
11453 goto saw_error;
11454 loc = c_parser_peek_token (parser)->location;
11455 if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
11456 goto saw_error;
11457 if (code == NOP_EXPR)
11458 lhs = c_parser_expression (parser).value;
11459 else
11460 lhs = c_parser_unary_expression (parser).value;
11461 lhs = c_fully_fold (lhs, false, NULL);
11462 if (lhs == error_mark_node)
11463 goto saw_error;
11464 if (code == NOP_EXPR)
11465 {
11466 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
11467 opcode. */
11468 code = OMP_ATOMIC;
11469 rhs = lhs;
11470 lhs = v;
11471 v = NULL_TREE;
11472 }
11473 goto done;
11474 case OMP_ATOMIC_CAPTURE_NEW:
11475 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
11476 {
11477 c_parser_consume_token (parser);
11478 structured_block = true;
11479 }
11480 else
11481 {
11482 v = c_parser_unary_expression (parser).value;
11483 v = c_fully_fold (v, false, NULL);
11484 if (v == error_mark_node)
11485 goto saw_error;
11486 if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
11487 goto saw_error;
11488 }
11489 break;
11490 default:
11491 break;
11492 }
11493
11494 /* For structured_block case we don't know yet whether
11495 old or new x should be captured. */
11496restart:
11497 eloc = c_parser_peek_token (parser)->location;
11498 expr = c_parser_unary_expression (parser);
11499 lhs = expr.value;
11500 expr = default_function_array_conversion (eloc, expr);
11501 unfolded_lhs = expr.value;
11502 lhs = c_fully_fold (lhs, false, NULL);
11503 orig_lhs = lhs;
11504 switch (TREE_CODE (lhs))
11505 {
11506 case ERROR_MARK:
11507 saw_error:
11508 c_parser_skip_to_end_of_block_or_statement (parser);
11509 if (structured_block)
11510 {
11511 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
11512 c_parser_consume_token (parser);
11513 else if (code == OMP_ATOMIC_CAPTURE_NEW)
11514 {
11515 c_parser_skip_to_end_of_block_or_statement (parser);
11516 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
11517 c_parser_consume_token (parser);
11518 }
11519 }
11520 return;
11521
11522 case POSTINCREMENT_EXPR:
11523 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
11524 code = OMP_ATOMIC_CAPTURE_OLD;
11525 /* FALLTHROUGH */
11526 case PREINCREMENT_EXPR:
11527 lhs = TREE_OPERAND (lhs, 0);
11528 unfolded_lhs = NULL_TREE;
11529 opcode = PLUS_EXPR;
11530 rhs = integer_one_node;
11531 break;
11532
11533 case POSTDECREMENT_EXPR:
11534 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
11535 code = OMP_ATOMIC_CAPTURE_OLD;
11536 /* FALLTHROUGH */
11537 case PREDECREMENT_EXPR:
11538 lhs = TREE_OPERAND (lhs, 0);
11539 unfolded_lhs = NULL_TREE;
11540 opcode = MINUS_EXPR;
11541 rhs = integer_one_node;
11542 break;
11543
11544 case COMPOUND_EXPR:
11545 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
11546 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
11547 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
11548 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
11549 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
11550 (TREE_OPERAND (lhs, 1), 0), 0)))
11551 == BOOLEAN_TYPE)
11552 /* Undo effects of boolean_increment for post {in,de}crement. */
11553 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
11554 /* FALLTHRU */
11555 case MODIFY_EXPR:
11556 if (TREE_CODE (lhs) == MODIFY_EXPR
11557 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
11558 {
11559 /* Undo effects of boolean_increment. */
11560 if (integer_onep (TREE_OPERAND (lhs, 1)))
11561 {
11562 /* This is pre or post increment. */
11563 rhs = TREE_OPERAND (lhs, 1);
11564 lhs = TREE_OPERAND (lhs, 0);
11565 unfolded_lhs = NULL_TREE;
11566 opcode = NOP_EXPR;
11567 if (code == OMP_ATOMIC_CAPTURE_NEW
11568 && !structured_block
11569 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
11570 code = OMP_ATOMIC_CAPTURE_OLD;
11571 break;
11572 }
11573 if (TREE_CODE (TREE_OPERAND (lhs, 1)) == TRUTH_NOT_EXPR
11574 && TREE_OPERAND (lhs, 0)
11575 == TREE_OPERAND (TREE_OPERAND (lhs, 1), 0))
11576 {
11577 /* This is pre or post decrement. */
11578 rhs = TREE_OPERAND (lhs, 1);
11579 lhs = TREE_OPERAND (lhs, 0);
11580 unfolded_lhs = NULL_TREE;
11581 opcode = NOP_EXPR;
11582 if (code == OMP_ATOMIC_CAPTURE_NEW
11583 && !structured_block
11584 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
11585 code = OMP_ATOMIC_CAPTURE_OLD;
11586 break;
11587 }
11588 }
11589 /* FALLTHRU */
11590 default:
11591 switch (c_parser_peek_token (parser)->type)
11592 {
11593 case CPP_MULT_EQ:
11594 opcode = MULT_EXPR;
11595 break;
11596 case CPP_DIV_EQ:
11597 opcode = TRUNC_DIV_EXPR;
11598 break;
11599 case CPP_PLUS_EQ:
11600 opcode = PLUS_EXPR;
11601 break;
11602 case CPP_MINUS_EQ:
11603 opcode = MINUS_EXPR;
11604 break;
11605 case CPP_LSHIFT_EQ:
11606 opcode = LSHIFT_EXPR;
11607 break;
11608 case CPP_RSHIFT_EQ:
11609 opcode = RSHIFT_EXPR;
11610 break;
11611 case CPP_AND_EQ:
11612 opcode = BIT_AND_EXPR;
11613 break;
11614 case CPP_OR_EQ:
11615 opcode = BIT_IOR_EXPR;
11616 break;
11617 case CPP_XOR_EQ:
11618 opcode = BIT_XOR_EXPR;
11619 break;
11620 case CPP_EQ:
11621 c_parser_consume_token (parser);
11622 eloc = c_parser_peek_token (parser)->location;
11623 expr = c_parser_expr_no_commas (parser, NULL, unfolded_lhs);
11624 rhs1 = expr.value;
11625 switch (TREE_CODE (rhs1))
11626 {
11627 case MULT_EXPR:
11628 case TRUNC_DIV_EXPR:
11629 case PLUS_EXPR:
11630 case MINUS_EXPR:
11631 case LSHIFT_EXPR:
11632 case RSHIFT_EXPR:
11633 case BIT_AND_EXPR:
11634 case BIT_IOR_EXPR:
11635 case BIT_XOR_EXPR:
11636 if (c_tree_equal (TREE_OPERAND (rhs1, 0), unfolded_lhs))
11637 {
11638 opcode = TREE_CODE (rhs1);
11639 rhs = c_fully_fold (TREE_OPERAND (rhs1, 1), false, NULL);
11640 rhs1 = c_fully_fold (TREE_OPERAND (rhs1, 0), false, NULL);
11641 goto stmt_done;
11642 }
11643 if (c_tree_equal (TREE_OPERAND (rhs1, 1), unfolded_lhs))
11644 {
11645 opcode = TREE_CODE (rhs1);
11646 rhs = c_fully_fold (TREE_OPERAND (rhs1, 0), false, NULL);
11647 rhs1 = c_fully_fold (TREE_OPERAND (rhs1, 1), false, NULL);
11648 swapped = !commutative_tree_code (opcode);
11649 goto stmt_done;
11650 }
11651 break;
11652 case ERROR_MARK:
11653 goto saw_error;
11654 default:
11655 break;
11656 }
11657 if (c_parser_peek_token (parser)->type == CPP_SEMICOLON)
11658 {
11659 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
11660 {
11661 code = OMP_ATOMIC_CAPTURE_OLD;
11662 v = lhs;
11663 lhs = NULL_TREE;
11664 expr = default_function_array_read_conversion (eloc, expr);
11665 unfolded_lhs1 = expr.value;
11666 lhs1 = c_fully_fold (unfolded_lhs1, false, NULL);
11667 rhs1 = NULL_TREE;
11668 c_parser_consume_token (parser);
11669 goto restart;
11670 }
11671 if (structured_block)
11672 {
11673 opcode = NOP_EXPR;
11674 expr = default_function_array_read_conversion (eloc, expr);
11675 rhs = c_fully_fold (expr.value, false, NULL);
11676 rhs1 = NULL_TREE;
11677 goto stmt_done;
11678 }
11679 }
11680 c_parser_error (parser, "invalid form of %<#pragma omp atomic%>");
11681 goto saw_error;
11682 default:
11683 c_parser_error (parser,
11684 "invalid operator for %<#pragma omp atomic%>");
11685 goto saw_error;
11686 }
11687
11688 /* Arrange to pass the location of the assignment operator to
11689 c_finish_omp_atomic. */
11690 loc = c_parser_peek_token (parser)->location;
11691 c_parser_consume_token (parser);
11692 eloc = c_parser_peek_token (parser)->location;
11693 expr = c_parser_expression (parser);
11694 expr = default_function_array_read_conversion (eloc, expr);
11695 rhs = expr.value;
11696 rhs = c_fully_fold (rhs, false, NULL);
11697 break;
11698 }
11699stmt_done:
11700 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
11701 {
11702 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
11703 goto saw_error;
11704 v = c_parser_unary_expression (parser).value;
11705 v = c_fully_fold (v, false, NULL);
11706 if (v == error_mark_node)
11707 goto saw_error;
11708 if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
11709 goto saw_error;
11710 eloc = c_parser_peek_token (parser)->location;
11711 expr = c_parser_unary_expression (parser);
11712 lhs1 = expr.value;
11713 expr = default_function_array_read_conversion (eloc, expr);
11714 unfolded_lhs1 = expr.value;
11715 lhs1 = c_fully_fold (lhs1, false, NULL);
11716 if (lhs1 == error_mark_node)
11717 goto saw_error;
11718 }
11719 if (structured_block)
11720 {
11721 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
11722 c_parser_require (parser, CPP_CLOSE_BRACE, "expected %<}%>");
11723 }
11724done:
11725 if (unfolded_lhs && unfolded_lhs1
11726 && !c_tree_equal (unfolded_lhs, unfolded_lhs1))
11727 {
11728 error ("%<#pragma omp atomic capture%> uses two different "
11729 "expressions for memory");
11730 stmt = error_mark_node;
11731 }
11732 else
11733 stmt = c_finish_omp_atomic (loc, code, opcode, lhs, rhs, v, lhs1, rhs1,
11734 swapped, seq_cst);
11735 if (stmt != error_mark_node)
11736 add_stmt (stmt);
11737
11738 if (!structured_block)
11739 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
11740}
11741
11742
11743/* OpenMP 2.5:
11744 # pragma omp barrier new-line
11745*/
11746
11747static void
11748c_parser_omp_barrier (c_parser *parser)
11749{
11750 location_t loc = c_parser_peek_token (parser)->location;
11751 c_parser_consume_pragma (parser);
11752 c_parser_skip_to_pragma_eol (parser);
11753
11754 c_finish_omp_barrier (loc);
11755}
11756
11757/* OpenMP 2.5:
11758 # pragma omp critical [(name)] new-line
11759 structured-block
11760
11761 LOC is the location of the #pragma itself. */
11762
11763static tree
11764c_parser_omp_critical (location_t loc, c_parser *parser)
11765{
11766 tree stmt, name = NULL;
11767
11768 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
11769 {
11770 c_parser_consume_token (parser);
11771 if (c_parser_next_token_is (parser, CPP_NAME))
11772 {
11773 name = c_parser_peek_token (parser)->value;
11774 c_parser_consume_token (parser);
11775 c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11776 }
11777 else
11778 c_parser_error (parser, "expected identifier");
11779 }
11780 else if (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
11781 c_parser_error (parser, "expected %<(%> or end of line");
11782 c_parser_skip_to_pragma_eol (parser);
11783
11784 stmt = c_parser_omp_structured_block (parser);
11785 return c_finish_omp_critical (loc, stmt, name);
11786}
11787
11788/* OpenMP 2.5:
11789 # pragma omp flush flush-vars[opt] new-line
11790
11791 flush-vars:
11792 ( variable-list ) */
11793
11794static void
11795c_parser_omp_flush (c_parser *parser)
11796{
11797 location_t loc = c_parser_peek_token (parser)->location;
11798 c_parser_consume_pragma (parser);
11799 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
11800 c_parser_omp_var_list_parens (parser, OMP_CLAUSE_ERROR, NULL);
11801 else if (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
11802 c_parser_error (parser, "expected %<(%> or end of line");
11803 c_parser_skip_to_pragma_eol (parser);
11804
11805 c_finish_omp_flush (loc);
11806}
11807
11808/* Parse the restricted form of the for statement allowed by OpenMP.
11809 The real trick here is to determine the loop control variable early
11810 so that we can push a new decl if necessary to make it private.
11811 LOC is the location of the OMP in "#pragma omp". */
11812
11813static tree
11814c_parser_omp_for_loop (location_t loc, c_parser *parser, enum tree_code code,
11815 tree clauses, tree *cclauses)
11816{
11817 tree decl, cond, incr, save_break, save_cont, body, init, stmt, cl;
11818 tree declv, condv, incrv, initv, ret = NULL;
11819 bool fail = false, open_brace_parsed = false;
11820 int i, collapse = 1, nbraces = 0;
11821 location_t for_loc;
11822 vec<tree, va_gc> *for_block = make_tree_vector ();
11823
11824 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
11825 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
9439e9a1 11826 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl));
acf0174b
JJ
11827
11828 gcc_assert (collapse >= 1);
11829
11830 declv = make_tree_vec (collapse);
11831 initv = make_tree_vec (collapse);
11832 condv = make_tree_vec (collapse);
11833 incrv = make_tree_vec (collapse);
11834
9a771876
JJ
11835 if (code != CILK_FOR
11836 && !c_parser_next_token_is_keyword (parser, RID_FOR))
acf0174b
JJ
11837 {
11838 c_parser_error (parser, "for statement expected");
11839 return NULL;
11840 }
9a771876
JJ
11841 if (code == CILK_FOR
11842 && !c_parser_next_token_is_keyword (parser, RID_CILK_FOR))
11843 {
11844 c_parser_error (parser, "_Cilk_for statement expected");
11845 return NULL;
11846 }
acf0174b
JJ
11847 for_loc = c_parser_peek_token (parser)->location;
11848 c_parser_consume_token (parser);
11849
11850 for (i = 0; i < collapse; i++)
11851 {
11852 int bracecount = 0;
11853
11854 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11855 goto pop_scopes;
11856
11857 /* Parse the initialization declaration or expression. */
11858 if (c_parser_next_tokens_start_declaration (parser))
11859 {
11860 if (i > 0)
11861 vec_safe_push (for_block, c_begin_compound_stmt (true));
11862 c_parser_declaration_or_fndef (parser, true, true, true, true, true,
11863 NULL, vNULL);
11864 decl = check_for_loop_decls (for_loc, flag_isoc99);
11865 if (decl == NULL)
11866 goto error_init;
11867 if (DECL_INITIAL (decl) == error_mark_node)
11868 decl = error_mark_node;
11869 init = decl;
11870 }
11871 else if (c_parser_next_token_is (parser, CPP_NAME)
11872 && c_parser_peek_2nd_token (parser)->type == CPP_EQ)
11873 {
11874 struct c_expr decl_exp;
11875 struct c_expr init_exp;
11876 location_t init_loc;
11877
11878 decl_exp = c_parser_postfix_expression (parser);
11879 decl = decl_exp.value;
11880
11881 c_parser_require (parser, CPP_EQ, "expected %<=%>");
11882
11883 init_loc = c_parser_peek_token (parser)->location;
11884 init_exp = c_parser_expr_no_commas (parser, NULL);
11885 init_exp = default_function_array_read_conversion (init_loc,
11886 init_exp);
11887 init = build_modify_expr (init_loc, decl, decl_exp.original_type,
11888 NOP_EXPR, init_loc, init_exp.value,
11889 init_exp.original_type);
11890 init = c_process_expr_stmt (init_loc, init);
11891
11892 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
11893 }
11894 else
11895 {
11896 error_init:
11897 c_parser_error (parser,
11898 "expected iteration declaration or initialization");
11899 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
11900 "expected %<)%>");
11901 fail = true;
11902 goto parse_next;
11903 }
11904
11905 /* Parse the loop condition. */
11906 cond = NULL_TREE;
11907 if (c_parser_next_token_is_not (parser, CPP_SEMICOLON))
11908 {
11909 location_t cond_loc = c_parser_peek_token (parser)->location;
11910 struct c_expr cond_expr
11911 = c_parser_binary_expression (parser, NULL, NULL_TREE);
11912
11913 cond = cond_expr.value;
11914 cond = c_objc_common_truthvalue_conversion (cond_loc, cond);
11915 cond = c_fully_fold (cond, false, NULL);
11916 switch (cond_expr.original_code)
11917 {
11918 case GT_EXPR:
11919 case GE_EXPR:
11920 case LT_EXPR:
11921 case LE_EXPR:
11922 break;
c02065fc 11923 case NE_EXPR:
9a771876 11924 if (code == CILK_SIMD || code == CILK_FOR)
c02065fc
AH
11925 break;
11926 /* FALLTHRU. */
acf0174b
JJ
11927 default:
11928 /* Can't be cond = error_mark_node, because we want to preserve
11929 the location until c_finish_omp_for. */
11930 cond = build1 (NOP_EXPR, boolean_type_node, error_mark_node);
11931 break;
11932 }
11933 protected_set_expr_location (cond, cond_loc);
11934 }
11935 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
11936
11937 /* Parse the increment expression. */
11938 incr = NULL_TREE;
11939 if (c_parser_next_token_is_not (parser, CPP_CLOSE_PAREN))
11940 {
11941 location_t incr_loc = c_parser_peek_token (parser)->location;
11942
11943 incr = c_process_expr_stmt (incr_loc,
11944 c_parser_expression (parser).value);
11945 }
11946 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11947
11948 if (decl == NULL || decl == error_mark_node || init == error_mark_node)
11949 fail = true;
11950 else
11951 {
11952 TREE_VEC_ELT (declv, i) = decl;
11953 TREE_VEC_ELT (initv, i) = init;
11954 TREE_VEC_ELT (condv, i) = cond;
11955 TREE_VEC_ELT (incrv, i) = incr;
11956 }
11957
11958 parse_next:
11959 if (i == collapse - 1)
11960 break;
11961
11962 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
11963 in between the collapsed for loops to be still considered perfectly
11964 nested. Hopefully the final version clarifies this.
11965 For now handle (multiple) {'s and empty statements. */
11966 do
11967 {
11968 if (c_parser_next_token_is_keyword (parser, RID_FOR))
11969 {
11970 c_parser_consume_token (parser);
11971 break;
11972 }
11973 else if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
11974 {
11975 c_parser_consume_token (parser);
11976 bracecount++;
11977 }
11978 else if (bracecount
11979 && c_parser_next_token_is (parser, CPP_SEMICOLON))
11980 c_parser_consume_token (parser);
11981 else
11982 {
11983 c_parser_error (parser, "not enough perfectly nested loops");
11984 if (bracecount)
11985 {
11986 open_brace_parsed = true;
11987 bracecount--;
11988 }
11989 fail = true;
11990 collapse = 0;
11991 break;
11992 }
11993 }
11994 while (1);
11995
11996 nbraces += bracecount;
11997 }
11998
11999 save_break = c_break_label;
c02065fc
AH
12000 if (code == CILK_SIMD)
12001 c_break_label = build_int_cst (size_type_node, 2);
12002 else
12003 c_break_label = size_one_node;
acf0174b
JJ
12004 save_cont = c_cont_label;
12005 c_cont_label = NULL_TREE;
12006 body = push_stmt_list ();
12007
12008 if (open_brace_parsed)
12009 {
12010 location_t here = c_parser_peek_token (parser)->location;
12011 stmt = c_begin_compound_stmt (true);
12012 c_parser_compound_statement_nostart (parser);
12013 add_stmt (c_end_compound_stmt (here, stmt, true));
12014 }
12015 else
12016 add_stmt (c_parser_c99_block_statement (parser));
12017 if (c_cont_label)
12018 {
12019 tree t = build1 (LABEL_EXPR, void_type_node, c_cont_label);
12020 SET_EXPR_LOCATION (t, loc);
12021 add_stmt (t);
12022 }
12023
12024 body = pop_stmt_list (body);
12025 c_break_label = save_break;
12026 c_cont_label = save_cont;
12027
12028 while (nbraces)
12029 {
12030 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
12031 {
12032 c_parser_consume_token (parser);
12033 nbraces--;
12034 }
12035 else if (c_parser_next_token_is (parser, CPP_SEMICOLON))
12036 c_parser_consume_token (parser);
12037 else
12038 {
12039 c_parser_error (parser, "collapsed loops not perfectly nested");
12040 while (nbraces)
12041 {
12042 location_t here = c_parser_peek_token (parser)->location;
12043 stmt = c_begin_compound_stmt (true);
12044 add_stmt (body);
12045 c_parser_compound_statement_nostart (parser);
12046 body = c_end_compound_stmt (here, stmt, true);
12047 nbraces--;
12048 }
12049 goto pop_scopes;
12050 }
12051 }
12052
12053 /* Only bother calling c_finish_omp_for if we haven't already generated
12054 an error from the initialization parsing. */
12055 if (!fail)
12056 {
12057 stmt = c_finish_omp_for (loc, code, declv, initv, condv,
12058 incrv, body, NULL);
12059 if (stmt)
12060 {
12061 if (cclauses != NULL
12062 && cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL] != NULL)
12063 {
12064 tree *c;
12065 for (c = &cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL]; *c ; )
12066 if (OMP_CLAUSE_CODE (*c) != OMP_CLAUSE_FIRSTPRIVATE
12067 && OMP_CLAUSE_CODE (*c) != OMP_CLAUSE_LASTPRIVATE)
12068 c = &OMP_CLAUSE_CHAIN (*c);
12069 else
12070 {
12071 for (i = 0; i < collapse; i++)
12072 if (TREE_VEC_ELT (declv, i) == OMP_CLAUSE_DECL (*c))
12073 break;
12074 if (i == collapse)
12075 c = &OMP_CLAUSE_CHAIN (*c);
12076 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE)
12077 {
12078 error_at (loc,
12079 "iteration variable %qD should not be firstprivate",
12080 OMP_CLAUSE_DECL (*c));
12081 *c = OMP_CLAUSE_CHAIN (*c);
12082 }
12083 else
12084 {
12085 /* Copy lastprivate (decl) clause to OMP_FOR_CLAUSES,
12086 change it to shared (decl) in
12087 OMP_PARALLEL_CLAUSES. */
12088 tree l = build_omp_clause (OMP_CLAUSE_LOCATION (*c),
12089 OMP_CLAUSE_LASTPRIVATE);
12090 OMP_CLAUSE_DECL (l) = OMP_CLAUSE_DECL (*c);
56ad0e38
JJ
12091 if (code == OMP_SIMD)
12092 {
12093 OMP_CLAUSE_CHAIN (l)
12094 = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
12095 cclauses[C_OMP_CLAUSE_SPLIT_FOR] = l;
12096 }
12097 else
12098 {
12099 OMP_CLAUSE_CHAIN (l) = clauses;
12100 clauses = l;
12101 }
acf0174b
JJ
12102 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
12103 }
12104 }
12105 }
12106 OMP_FOR_CLAUSES (stmt) = clauses;
12107 }
12108 ret = stmt;
12109 }
12110pop_scopes:
12111 while (!for_block->is_empty ())
12112 {
12113 /* FIXME diagnostics: LOC below should be the actual location of
12114 this particular for block. We need to build a list of
12115 locations to go along with FOR_BLOCK. */
12116 stmt = c_end_compound_stmt (loc, for_block->pop (), true);
12117 add_stmt (stmt);
12118 }
12119 release_tree_vector (for_block);
12120 return ret;
12121}
12122
12123/* Helper function for OpenMP parsing, split clauses and call
12124 finish_omp_clauses on each of the set of clauses afterwards. */
12125
12126static void
12127omp_split_clauses (location_t loc, enum tree_code code,
12128 omp_clause_mask mask, tree clauses, tree *cclauses)
12129{
12130 int i;
12131 c_omp_split_clauses (loc, code, mask, clauses, cclauses);
12132 for (i = 0; i < C_OMP_CLAUSE_SPLIT_COUNT; i++)
12133 if (cclauses[i])
12134 cclauses[i] = c_finish_omp_clauses (cclauses[i]);
12135}
12136
12137/* OpenMP 4.0:
12138 #pragma omp simd simd-clause[optseq] new-line
12139 for-loop
12140
12141 LOC is the location of the #pragma token.
12142*/
12143
12144#define OMP_SIMD_CLAUSE_MASK \
12145 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
12146 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
12147 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
12148 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
12149 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
12150 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
12151 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
12152
12153static tree
12154c_parser_omp_simd (location_t loc, c_parser *parser,
12155 char *p_name, omp_clause_mask mask, tree *cclauses)
12156{
12157 tree block, clauses, ret;
12158
12159 strcat (p_name, " simd");
12160 mask |= OMP_SIMD_CLAUSE_MASK;
12161 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED);
12162
12163 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
12164 if (cclauses)
12165 {
12166 omp_split_clauses (loc, OMP_SIMD, mask, clauses, cclauses);
12167 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SIMD];
12168 }
12169
12170 block = c_begin_compound_stmt (true);
12171 ret = c_parser_omp_for_loop (loc, parser, OMP_SIMD, clauses, cclauses);
12172 block = c_end_compound_stmt (loc, block, true);
12173 add_stmt (block);
12174
12175 return ret;
12176}
12177
12178/* OpenMP 2.5:
12179 #pragma omp for for-clause[optseq] new-line
12180 for-loop
12181
12182 OpenMP 4.0:
12183 #pragma omp for simd for-simd-clause[optseq] new-line
12184 for-loop
12185
12186 LOC is the location of the #pragma token.
12187*/
12188
12189#define OMP_FOR_CLAUSE_MASK \
12190 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
12191 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
12192 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
12193 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
12194 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
12195 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
12196 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
12197 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
12198
12199static tree
12200c_parser_omp_for (location_t loc, c_parser *parser,
12201 char *p_name, omp_clause_mask mask, tree *cclauses)
12202{
12203 tree block, clauses, ret;
12204
12205 strcat (p_name, " for");
12206 mask |= OMP_FOR_CLAUSE_MASK;
12207 if (cclauses)
12208 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
12209
12210 if (c_parser_next_token_is (parser, CPP_NAME))
20906c66 12211 {
acf0174b
JJ
12212 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
12213
12214 if (strcmp (p, "simd") == 0)
12215 {
12216 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
12217 if (cclauses == NULL)
12218 cclauses = cclauses_buf;
12219
12220 c_parser_consume_token (parser);
6d7f7e0a
TB
12221 if (!flag_openmp) /* flag_openmp_simd */
12222 return c_parser_omp_simd (loc, parser, p_name, mask, cclauses);
acf0174b
JJ
12223 block = c_begin_compound_stmt (true);
12224 ret = c_parser_omp_simd (loc, parser, p_name, mask, cclauses);
12225 block = c_end_compound_stmt (loc, block, true);
12226 if (ret == NULL_TREE)
12227 return ret;
12228 ret = make_node (OMP_FOR);
12229 TREE_TYPE (ret) = void_type_node;
12230 OMP_FOR_BODY (ret) = block;
12231 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
12232 SET_EXPR_LOCATION (ret, loc);
12233 add_stmt (ret);
12234 return ret;
12235 }
20906c66 12236 }
6d7f7e0a
TB
12237 if (!flag_openmp) /* flag_openmp_simd */
12238 {
12239 c_parser_skip_to_pragma_eol (parser);
12240 return NULL_TREE;
12241 }
acf0174b
JJ
12242
12243 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
12244 if (cclauses)
20906c66 12245 {
acf0174b
JJ
12246 omp_split_clauses (loc, OMP_FOR, mask, clauses, cclauses);
12247 clauses = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
20906c66 12248 }
20906c66 12249
acf0174b
JJ
12250 block = c_begin_compound_stmt (true);
12251 ret = c_parser_omp_for_loop (loc, parser, OMP_FOR, clauses, cclauses);
12252 block = c_end_compound_stmt (loc, block, true);
12253 add_stmt (block);
12254
12255 return ret;
953ff289
DN
12256}
12257
acf0174b
JJ
12258/* OpenMP 2.5:
12259 # pragma omp master new-line
12260 structured-block
12261
12262 LOC is the location of the #pragma token.
12263*/
12264
12265static tree
12266c_parser_omp_master (location_t loc, c_parser *parser)
12267{
12268 c_parser_skip_to_pragma_eol (parser);
12269 return c_finish_omp_master (loc, c_parser_omp_structured_block (parser));
12270}
953ff289
DN
12271
12272/* OpenMP 2.5:
acf0174b
JJ
12273 # pragma omp ordered new-line
12274 structured-block
12275
12276 LOC is the location of the #pragma itself.
953ff289
DN
12277*/
12278
acf0174b
JJ
12279static tree
12280c_parser_omp_ordered (location_t loc, c_parser *parser)
953ff289 12281{
953ff289 12282 c_parser_skip_to_pragma_eol (parser);
acf0174b
JJ
12283 return c_finish_omp_ordered (loc, c_parser_omp_structured_block (parser));
12284}
953ff289 12285
acf0174b
JJ
12286/* OpenMP 2.5:
12287
12288 section-scope:
12289 { section-sequence }
12290
12291 section-sequence:
12292 section-directive[opt] structured-block
12293 section-sequence section-directive structured-block
12294
12295 SECTIONS_LOC is the location of the #pragma omp sections. */
12296
12297static tree
12298c_parser_omp_sections_scope (location_t sections_loc, c_parser *parser)
12299{
12300 tree stmt, substmt;
12301 bool error_suppress = false;
12302 location_t loc;
12303
12304 loc = c_parser_peek_token (parser)->location;
12305 if (!c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
12306 {
12307 /* Avoid skipping until the end of the block. */
12308 parser->error = false;
12309 return NULL_TREE;
12310 }
12311
12312 stmt = push_stmt_list ();
12313
12314 if (c_parser_peek_token (parser)->pragma_kind != PRAGMA_OMP_SECTION)
12315 {
12316 substmt = c_parser_omp_structured_block (parser);
12317 substmt = build1 (OMP_SECTION, void_type_node, substmt);
12318 SET_EXPR_LOCATION (substmt, loc);
12319 add_stmt (substmt);
12320 }
12321
12322 while (1)
12323 {
12324 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
12325 break;
12326 if (c_parser_next_token_is (parser, CPP_EOF))
12327 break;
12328
12329 loc = c_parser_peek_token (parser)->location;
12330 if (c_parser_peek_token (parser)->pragma_kind == PRAGMA_OMP_SECTION)
12331 {
12332 c_parser_consume_pragma (parser);
12333 c_parser_skip_to_pragma_eol (parser);
12334 error_suppress = false;
12335 }
12336 else if (!error_suppress)
12337 {
12338 error_at (loc, "expected %<#pragma omp section%> or %<}%>");
12339 error_suppress = true;
12340 }
12341
12342 substmt = c_parser_omp_structured_block (parser);
12343 substmt = build1 (OMP_SECTION, void_type_node, substmt);
12344 SET_EXPR_LOCATION (substmt, loc);
12345 add_stmt (substmt);
12346 }
12347 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE,
12348 "expected %<#pragma omp section%> or %<}%>");
12349
12350 substmt = pop_stmt_list (stmt);
12351
12352 stmt = make_node (OMP_SECTIONS);
12353 SET_EXPR_LOCATION (stmt, sections_loc);
12354 TREE_TYPE (stmt) = void_type_node;
12355 OMP_SECTIONS_BODY (stmt) = substmt;
12356
12357 return add_stmt (stmt);
953ff289
DN
12358}
12359
12360/* OpenMP 2.5:
acf0174b
JJ
12361 # pragma omp sections sections-clause[optseq] newline
12362 sections-scope
c2255bc4 12363
acf0174b
JJ
12364 LOC is the location of the #pragma token.
12365*/
12366
12367#define OMP_SECTIONS_CLAUSE_MASK \
12368 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
12369 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
12370 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
12371 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
12372 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
953ff289
DN
12373
12374static tree
acf0174b
JJ
12375c_parser_omp_sections (location_t loc, c_parser *parser,
12376 char *p_name, omp_clause_mask mask, tree *cclauses)
953ff289 12377{
acf0174b 12378 tree block, clauses, ret;
953ff289 12379
acf0174b
JJ
12380 strcat (p_name, " sections");
12381 mask |= OMP_SECTIONS_CLAUSE_MASK;
12382 if (cclauses)
12383 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
12384
12385 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
12386 if (cclauses)
12387 {
12388 omp_split_clauses (loc, OMP_SECTIONS, mask, clauses, cclauses);
12389 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SECTIONS];
12390 }
12391
12392 block = c_begin_compound_stmt (true);
12393 ret = c_parser_omp_sections_scope (loc, parser);
12394 if (ret)
12395 OMP_SECTIONS_CLAUSES (ret) = clauses;
12396 block = c_end_compound_stmt (loc, block, true);
12397 add_stmt (block);
12398
12399 return ret;
12400}
12401
12402/* OpenMP 2.5:
cef0fd0e
TS
12403 # pragma omp parallel parallel-clause[optseq] new-line
12404 structured-block
12405 # pragma omp parallel for parallel-for-clause[optseq] new-line
12406 structured-block
12407 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
12408 structured-block
12409
12410 OpenMP 4.0:
12411 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
12412 structured-block
acf0174b
JJ
12413
12414 LOC is the location of the #pragma token.
12415*/
12416
12417#define OMP_PARALLEL_CLAUSE_MASK \
12418 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
12419 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
12420 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
12421 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
12422 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
12423 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
12424 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
12425 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
12426 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
12427
12428static tree
12429c_parser_omp_parallel (location_t loc, c_parser *parser,
12430 char *p_name, omp_clause_mask mask, tree *cclauses)
12431{
12432 tree stmt, clauses, block;
12433
12434 strcat (p_name, " parallel");
12435 mask |= OMP_PARALLEL_CLAUSE_MASK;
12436
12437 if (c_parser_next_token_is_keyword (parser, RID_FOR))
953ff289 12438 {
acf0174b
JJ
12439 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
12440 if (cclauses == NULL)
12441 cclauses = cclauses_buf;
12442
953ff289 12443 c_parser_consume_token (parser);
6d7f7e0a
TB
12444 if (!flag_openmp) /* flag_openmp_simd */
12445 return c_parser_omp_for (loc, parser, p_name, mask, cclauses);
acf0174b 12446 block = c_begin_omp_parallel ();
e162a134 12447 tree ret = c_parser_omp_for (loc, parser, p_name, mask, cclauses);
acf0174b
JJ
12448 stmt
12449 = c_finish_omp_parallel (loc, cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
12450 block);
e162a134
JJ
12451 if (ret == NULL_TREE)
12452 return ret;
acf0174b
JJ
12453 OMP_PARALLEL_COMBINED (stmt) = 1;
12454 return stmt;
12455 }
12456 else if (cclauses)
12457 {
12458 error_at (loc, "expected %<for%> after %qs", p_name);
12459 c_parser_skip_to_pragma_eol (parser);
12460 return NULL_TREE;
12461 }
6d7f7e0a
TB
12462 else if (!flag_openmp) /* flag_openmp_simd */
12463 {
12464 c_parser_skip_to_pragma_eol (parser);
12465 return NULL_TREE;
12466 }
acf0174b
JJ
12467 else if (c_parser_next_token_is (parser, CPP_NAME))
12468 {
12469 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
12470 if (strcmp (p, "sections") == 0)
953ff289 12471 {
acf0174b
JJ
12472 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
12473 if (cclauses == NULL)
12474 cclauses = cclauses_buf;
12475
953ff289 12476 c_parser_consume_token (parser);
acf0174b
JJ
12477 block = c_begin_omp_parallel ();
12478 c_parser_omp_sections (loc, parser, p_name, mask, cclauses);
12479 stmt = c_finish_omp_parallel (loc,
12480 cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
12481 block);
12482 OMP_PARALLEL_COMBINED (stmt) = 1;
12483 return stmt;
953ff289 12484 }
953ff289 12485 }
953ff289 12486
acf0174b
JJ
12487 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
12488
12489 block = c_begin_omp_parallel ();
12490 c_parser_statement (parser);
12491 stmt = c_finish_omp_parallel (loc, clauses, block);
12492
12493 return stmt;
12494}
12495
12496/* OpenMP 2.5:
12497 # pragma omp single single-clause[optseq] new-line
12498 structured-block
12499
12500 LOC is the location of the #pragma.
12501*/
12502
12503#define OMP_SINGLE_CLAUSE_MASK \
12504 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
12505 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
12506 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
12507 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
12508
12509static tree
12510c_parser_omp_single (location_t loc, c_parser *parser)
12511{
12512 tree stmt = make_node (OMP_SINGLE);
12513 SET_EXPR_LOCATION (stmt, loc);
12514 TREE_TYPE (stmt) = void_type_node;
12515
12516 OMP_SINGLE_CLAUSES (stmt)
12517 = c_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
12518 "#pragma omp single");
12519 OMP_SINGLE_BODY (stmt) = c_parser_omp_structured_block (parser);
12520
12521 return add_stmt (stmt);
12522}
12523
12524/* OpenMP 3.0:
12525 # pragma omp task task-clause[optseq] new-line
12526
12527 LOC is the location of the #pragma.
12528*/
12529
12530#define OMP_TASK_CLAUSE_MASK \
12531 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
12532 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
12533 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
12534 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
12535 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
12536 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
12537 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
12538 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
12539 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND))
12540
12541static tree
12542c_parser_omp_task (location_t loc, c_parser *parser)
12543{
12544 tree clauses, block;
12545
12546 clauses = c_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
12547 "#pragma omp task");
12548
12549 block = c_begin_omp_task ();
12550 c_parser_statement (parser);
12551 return c_finish_omp_task (loc, clauses, block);
953ff289
DN
12552}
12553
acf0174b
JJ
12554/* OpenMP 3.0:
12555 # pragma omp taskwait new-line
12556*/
953ff289
DN
12557
12558static void
acf0174b 12559c_parser_omp_taskwait (c_parser *parser)
953ff289 12560{
c2255bc4 12561 location_t loc = c_parser_peek_token (parser)->location;
953ff289 12562 c_parser_consume_pragma (parser);
953ff289
DN
12563 c_parser_skip_to_pragma_eol (parser);
12564
acf0174b 12565 c_finish_omp_taskwait (loc);
953ff289
DN
12566}
12567
acf0174b
JJ
12568/* OpenMP 3.1:
12569 # pragma omp taskyield new-line
12570*/
953ff289 12571
acf0174b
JJ
12572static void
12573c_parser_omp_taskyield (c_parser *parser)
953ff289 12574{
acf0174b
JJ
12575 location_t loc = c_parser_peek_token (parser)->location;
12576 c_parser_consume_pragma (parser);
12577 c_parser_skip_to_pragma_eol (parser);
a68ab351 12578
acf0174b
JJ
12579 c_finish_omp_taskyield (loc);
12580}
3ba09659 12581
acf0174b
JJ
12582/* OpenMP 4.0:
12583 # pragma omp taskgroup new-line
12584*/
953ff289 12585
acf0174b
JJ
12586static tree
12587c_parser_omp_taskgroup (c_parser *parser)
12588{
12589 location_t loc = c_parser_peek_token (parser)->location;
12590 c_parser_skip_to_pragma_eol (parser);
12591 return c_finish_omp_taskgroup (loc, c_parser_omp_structured_block (parser));
12592}
c9f9eb5d 12593
acf0174b
JJ
12594/* OpenMP 4.0:
12595 # pragma omp cancel cancel-clause[optseq] new-line
953ff289 12596
acf0174b
JJ
12597 LOC is the location of the #pragma.
12598*/
a68ab351 12599
acf0174b
JJ
12600#define OMP_CANCEL_CLAUSE_MASK \
12601 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
12602 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
12603 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
12604 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
12605 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
a68ab351 12606
acf0174b
JJ
12607static void
12608c_parser_omp_cancel (c_parser *parser)
12609{
12610 location_t loc = c_parser_peek_token (parser)->location;
a68ab351 12611
acf0174b
JJ
12612 c_parser_consume_pragma (parser);
12613 tree clauses = c_parser_omp_all_clauses (parser, OMP_CANCEL_CLAUSE_MASK,
12614 "#pragma omp cancel");
953ff289 12615
acf0174b
JJ
12616 c_finish_omp_cancel (loc, clauses);
12617}
953ff289 12618
acf0174b
JJ
12619/* OpenMP 4.0:
12620 # pragma omp cancellation point cancelpt-clause[optseq] new-line
953ff289 12621
acf0174b
JJ
12622 LOC is the location of the #pragma.
12623*/
953ff289 12624
acf0174b
JJ
12625#define OMP_CANCELLATION_POINT_CLAUSE_MASK \
12626 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
12627 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
12628 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
12629 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
a68ab351 12630
acf0174b
JJ
12631static void
12632c_parser_omp_cancellation_point (c_parser *parser)
12633{
12634 location_t loc = c_parser_peek_token (parser)->location;
12635 tree clauses;
12636 bool point_seen = false;
12637
12638 c_parser_consume_pragma (parser);
12639 if (c_parser_next_token_is (parser, CPP_NAME))
a68ab351 12640 {
acf0174b
JJ
12641 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
12642 if (strcmp (p, "point") == 0)
a68ab351 12643 {
acf0174b
JJ
12644 c_parser_consume_token (parser);
12645 point_seen = true;
a68ab351 12646 }
a68ab351 12647 }
acf0174b 12648 if (!point_seen)
a68ab351 12649 {
acf0174b
JJ
12650 c_parser_error (parser, "expected %<point%>");
12651 c_parser_skip_to_pragma_eol (parser);
12652 return;
a68ab351 12653 }
953ff289 12654
acf0174b
JJ
12655 clauses
12656 = c_parser_omp_all_clauses (parser, OMP_CANCELLATION_POINT_CLAUSE_MASK,
12657 "#pragma omp cancellation point");
c2255bc4 12658
acf0174b
JJ
12659 c_finish_omp_cancellation_point (loc, clauses);
12660}
953ff289 12661
acf0174b
JJ
12662/* OpenMP 4.0:
12663 #pragma omp distribute distribute-clause[optseq] new-line
12664 for-loop */
12665
12666#define OMP_DISTRIBUTE_CLAUSE_MASK \
12667 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
12668 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
12669 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
12670 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
953ff289
DN
12671
12672static tree
acf0174b
JJ
12673c_parser_omp_distribute (location_t loc, c_parser *parser,
12674 char *p_name, omp_clause_mask mask, tree *cclauses)
953ff289 12675{
acf0174b
JJ
12676 tree clauses, block, ret;
12677
12678 strcat (p_name, " distribute");
12679 mask |= OMP_DISTRIBUTE_CLAUSE_MASK;
12680
12681 if (c_parser_next_token_is (parser, CPP_NAME))
12682 {
12683 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
12684 bool simd = false;
12685 bool parallel = false;
12686
12687 if (strcmp (p, "simd") == 0)
12688 simd = true;
12689 else
12690 parallel = strcmp (p, "parallel") == 0;
12691 if (parallel || simd)
12692 {
12693 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
12694 if (cclauses == NULL)
12695 cclauses = cclauses_buf;
12696 c_parser_consume_token (parser);
6d7f7e0a
TB
12697 if (!flag_openmp) /* flag_openmp_simd */
12698 {
12699 if (simd)
12700 return c_parser_omp_simd (loc, parser, p_name, mask, cclauses);
12701 else
12702 return c_parser_omp_parallel (loc, parser, p_name, mask,
12703 cclauses);
12704 }
acf0174b
JJ
12705 block = c_begin_compound_stmt (true);
12706 if (simd)
12707 ret = c_parser_omp_simd (loc, parser, p_name, mask, cclauses);
12708 else
12709 ret = c_parser_omp_parallel (loc, parser, p_name, mask, cclauses);
12710 block = c_end_compound_stmt (loc, block, true);
12711 if (ret == NULL)
12712 return ret;
12713 ret = make_node (OMP_DISTRIBUTE);
12714 TREE_TYPE (ret) = void_type_node;
12715 OMP_FOR_BODY (ret) = block;
12716 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
12717 SET_EXPR_LOCATION (ret, loc);
12718 add_stmt (ret);
12719 return ret;
12720 }
12721 }
6d7f7e0a
TB
12722 if (!flag_openmp) /* flag_openmp_simd */
12723 {
12724 c_parser_skip_to_pragma_eol (parser);
12725 return NULL_TREE;
12726 }
953ff289 12727
acf0174b
JJ
12728 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
12729 if (cclauses)
12730 {
12731 omp_split_clauses (loc, OMP_DISTRIBUTE, mask, clauses, cclauses);
12732 clauses = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
12733 }
953ff289
DN
12734
12735 block = c_begin_compound_stmt (true);
acf0174b 12736 ret = c_parser_omp_for_loop (loc, parser, OMP_DISTRIBUTE, clauses, NULL);
c2255bc4 12737 block = c_end_compound_stmt (loc, block, true);
953ff289
DN
12738 add_stmt (block);
12739
12740 return ret;
12741}
12742
acf0174b
JJ
12743/* OpenMP 4.0:
12744 # pragma omp teams teams-clause[optseq] new-line
12745 structured-block */
c2255bc4 12746
acf0174b
JJ
12747#define OMP_TEAMS_CLAUSE_MASK \
12748 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
12749 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
12750 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
12751 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
12752 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
12753 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
12754 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
953ff289
DN
12755
12756static tree
acf0174b
JJ
12757c_parser_omp_teams (location_t loc, c_parser *parser,
12758 char *p_name, omp_clause_mask mask, tree *cclauses)
953ff289 12759{
acf0174b
JJ
12760 tree clauses, block, ret;
12761
12762 strcat (p_name, " teams");
12763 mask |= OMP_TEAMS_CLAUSE_MASK;
12764
12765 if (c_parser_next_token_is (parser, CPP_NAME))
12766 {
12767 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
12768 if (strcmp (p, "distribute") == 0)
12769 {
12770 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
12771 if (cclauses == NULL)
12772 cclauses = cclauses_buf;
12773
12774 c_parser_consume_token (parser);
6d7f7e0a
TB
12775 if (!flag_openmp) /* flag_openmp_simd */
12776 return c_parser_omp_distribute (loc, parser, p_name, mask, cclauses);
acf0174b
JJ
12777 block = c_begin_compound_stmt (true);
12778 ret = c_parser_omp_distribute (loc, parser, p_name, mask, cclauses);
12779 block = c_end_compound_stmt (loc, block, true);
12780 if (ret == NULL)
12781 return ret;
12782 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
12783 ret = make_node (OMP_TEAMS);
12784 TREE_TYPE (ret) = void_type_node;
12785 OMP_TEAMS_CLAUSES (ret) = clauses;
12786 OMP_TEAMS_BODY (ret) = block;
12787 return add_stmt (ret);
12788 }
12789 }
6d7f7e0a
TB
12790 if (!flag_openmp) /* flag_openmp_simd */
12791 {
12792 c_parser_skip_to_pragma_eol (parser);
12793 return NULL_TREE;
12794 }
acf0174b
JJ
12795
12796 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
12797 if (cclauses)
12798 {
12799 omp_split_clauses (loc, OMP_TEAMS, mask, clauses, cclauses);
12800 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
12801 }
12802
12803 tree stmt = make_node (OMP_TEAMS);
12804 TREE_TYPE (stmt) = void_type_node;
12805 OMP_TEAMS_CLAUSES (stmt) = clauses;
12806 OMP_TEAMS_BODY (stmt) = c_parser_omp_structured_block (parser);
12807
12808 return add_stmt (stmt);
953ff289
DN
12809}
12810
acf0174b
JJ
12811/* OpenMP 4.0:
12812 # pragma omp target data target-data-clause[optseq] new-line
12813 structured-block */
c2255bc4 12814
acf0174b
JJ
12815#define OMP_TARGET_DATA_CLAUSE_MASK \
12816 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
12817 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
12818 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
953ff289
DN
12819
12820static tree
acf0174b 12821c_parser_omp_target_data (location_t loc, c_parser *parser)
953ff289 12822{
acf0174b
JJ
12823 tree stmt = make_node (OMP_TARGET_DATA);
12824 TREE_TYPE (stmt) = void_type_node;
953ff289 12825
acf0174b
JJ
12826 OMP_TARGET_DATA_CLAUSES (stmt)
12827 = c_parser_omp_all_clauses (parser, OMP_TARGET_DATA_CLAUSE_MASK,
12828 "#pragma omp target data");
12829 keep_next_level ();
12830 tree block = c_begin_compound_stmt (true);
12831 add_stmt (c_parser_omp_structured_block (parser));
12832 OMP_TARGET_DATA_BODY (stmt) = c_end_compound_stmt (loc, block, true);
953ff289 12833
acf0174b
JJ
12834 SET_EXPR_LOCATION (stmt, loc);
12835 return add_stmt (stmt);
12836}
953ff289 12837
acf0174b
JJ
12838/* OpenMP 4.0:
12839 # pragma omp target update target-update-clause[optseq] new-line */
c2255bc4 12840
acf0174b
JJ
12841#define OMP_TARGET_UPDATE_CLAUSE_MASK \
12842 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
12843 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
12844 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
12845 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
953ff289 12846
acf0174b
JJ
12847static bool
12848c_parser_omp_target_update (location_t loc, c_parser *parser,
12849 enum pragma_context context)
953ff289 12850{
acf0174b
JJ
12851 if (context == pragma_stmt)
12852 {
12853 error_at (loc,
12854 "%<#pragma omp target update%> may only be "
12855 "used in compound statements");
12856 c_parser_skip_to_pragma_eol (parser);
12857 return false;
12858 }
953ff289 12859
acf0174b
JJ
12860 tree clauses
12861 = c_parser_omp_all_clauses (parser, OMP_TARGET_UPDATE_CLAUSE_MASK,
12862 "#pragma omp target update");
12863 if (find_omp_clause (clauses, OMP_CLAUSE_TO) == NULL_TREE
12864 && find_omp_clause (clauses, OMP_CLAUSE_FROM) == NULL_TREE)
953ff289 12865 {
acf0174b
JJ
12866 error_at (loc,
12867 "%<#pragma omp target update must contain at least one "
12868 "%<from%> or %<to%> clauses");
12869 return false;
953ff289
DN
12870 }
12871
acf0174b
JJ
12872 tree stmt = make_node (OMP_TARGET_UPDATE);
12873 TREE_TYPE (stmt) = void_type_node;
12874 OMP_TARGET_UPDATE_CLAUSES (stmt) = clauses;
12875 SET_EXPR_LOCATION (stmt, loc);
12876 add_stmt (stmt);
12877 return false;
12878}
953ff289 12879
acf0174b
JJ
12880/* OpenMP 4.0:
12881 # pragma omp target target-clause[optseq] new-line
12882 structured-block */
953ff289 12883
acf0174b
JJ
12884#define OMP_TARGET_CLAUSE_MASK \
12885 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
12886 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
12887 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
953ff289 12888
acf0174b
JJ
12889static bool
12890c_parser_omp_target (c_parser *parser, enum pragma_context context)
12891{
12892 location_t loc = c_parser_peek_token (parser)->location;
12893 c_parser_consume_pragma (parser);
953ff289 12894
acf0174b
JJ
12895 if (context != pragma_stmt && context != pragma_compound)
12896 {
12897 c_parser_error (parser, "expected declaration specifiers");
12898 c_parser_skip_to_pragma_eol (parser);
12899 return false;
953ff289
DN
12900 }
12901
acf0174b 12902 if (c_parser_next_token_is (parser, CPP_NAME))
953ff289 12903 {
acf0174b 12904 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
953ff289 12905
6d7f7e0a 12906 if (strcmp (p, "teams") == 0)
acf0174b
JJ
12907 {
12908 tree cclauses[C_OMP_CLAUSE_SPLIT_COUNT];
12909 char p_name[sizeof ("#pragma omp target teams distribute "
12910 "parallel for simd")];
953ff289 12911
acf0174b 12912 c_parser_consume_token (parser);
e7bd1de1 12913 strcpy (p_name, "#pragma omp target");
6d7f7e0a 12914 if (!flag_openmp) /* flag_openmp_simd */
edbba2ce
TS
12915 {
12916 tree stmt = c_parser_omp_teams (loc, parser, p_name,
12917 OMP_TARGET_CLAUSE_MASK,
12918 cclauses);
12919 return stmt != NULL_TREE;
12920 }
acf0174b
JJ
12921 keep_next_level ();
12922 tree block = c_begin_compound_stmt (true);
12923 tree ret = c_parser_omp_teams (loc, parser, p_name,
12924 OMP_TARGET_CLAUSE_MASK, cclauses);
12925 block = c_end_compound_stmt (loc, block, true);
edbba2ce
TS
12926 if (ret == NULL_TREE)
12927 return false;
acf0174b
JJ
12928 tree stmt = make_node (OMP_TARGET);
12929 TREE_TYPE (stmt) = void_type_node;
12930 OMP_TARGET_CLAUSES (stmt) = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
12931 OMP_TARGET_BODY (stmt) = block;
12932 add_stmt (stmt);
12933 return true;
12934 }
6d7f7e0a
TB
12935 else if (!flag_openmp) /* flag_openmp_simd */
12936 {
12937 c_parser_skip_to_pragma_eol (parser);
edbba2ce 12938 return false;
6d7f7e0a
TB
12939 }
12940 else if (strcmp (p, "data") == 0)
12941 {
12942 c_parser_consume_token (parser);
12943 c_parser_omp_target_data (loc, parser);
12944 return true;
12945 }
12946 else if (strcmp (p, "update") == 0)
12947 {
12948 c_parser_consume_token (parser);
12949 return c_parser_omp_target_update (loc, parser, context);
12950 }
953ff289 12951 }
953ff289 12952
acf0174b 12953 tree stmt = make_node (OMP_TARGET);
953ff289 12954 TREE_TYPE (stmt) = void_type_node;
953ff289 12955
acf0174b
JJ
12956 OMP_TARGET_CLAUSES (stmt)
12957 = c_parser_omp_all_clauses (parser, OMP_TARGET_CLAUSE_MASK,
12958 "#pragma omp target");
12959 keep_next_level ();
12960 tree block = c_begin_compound_stmt (true);
12961 add_stmt (c_parser_omp_structured_block (parser));
12962 OMP_TARGET_BODY (stmt) = c_end_compound_stmt (loc, block, true);
12963
12964 SET_EXPR_LOCATION (stmt, loc);
12965 add_stmt (stmt);
12966 return true;
12967}
12968
12969/* OpenMP 4.0:
12970 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
12971
12972#define OMP_DECLARE_SIMD_CLAUSE_MASK \
12973 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
12974 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
12975 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
12976 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
12977 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
12978 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
12979
12980static void
12981c_parser_omp_declare_simd (c_parser *parser, enum pragma_context context)
12982{
12983 vec<c_token> clauses = vNULL;
12984 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
12985 {
12986 c_token *token = c_parser_peek_token (parser);
12987 if (token->type == CPP_EOF)
12988 {
12989 c_parser_skip_to_pragma_eol (parser);
12990 clauses.release ();
12991 return;
12992 }
12993 clauses.safe_push (*token);
12994 c_parser_consume_token (parser);
12995 }
12996 clauses.safe_push (*c_parser_peek_token (parser));
12997 c_parser_skip_to_pragma_eol (parser);
12998
12999 while (c_parser_next_token_is (parser, CPP_PRAGMA))
13000 {
13001 if (c_parser_peek_token (parser)->pragma_kind
13002 != PRAGMA_OMP_DECLARE_REDUCTION
13003 || c_parser_peek_2nd_token (parser)->type != CPP_NAME
13004 || strcmp (IDENTIFIER_POINTER
13005 (c_parser_peek_2nd_token (parser)->value),
13006 "simd") != 0)
13007 {
13008 c_parser_error (parser,
13009 "%<#pragma omp declare simd%> must be followed by "
13010 "function declaration or definition or another "
13011 "%<#pragma omp declare simd%>");
13012 clauses.release ();
13013 return;
13014 }
13015 c_parser_consume_pragma (parser);
13016 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
13017 {
13018 c_token *token = c_parser_peek_token (parser);
13019 if (token->type == CPP_EOF)
13020 {
13021 c_parser_skip_to_pragma_eol (parser);
13022 clauses.release ();
13023 return;
13024 }
13025 clauses.safe_push (*token);
13026 c_parser_consume_token (parser);
13027 }
13028 clauses.safe_push (*c_parser_peek_token (parser));
13029 c_parser_skip_to_pragma_eol (parser);
13030 }
13031
13032 /* Make sure nothing tries to read past the end of the tokens. */
13033 c_token eof_token;
13034 memset (&eof_token, 0, sizeof (eof_token));
13035 eof_token.type = CPP_EOF;
13036 clauses.safe_push (eof_token);
13037 clauses.safe_push (eof_token);
13038
13039 switch (context)
13040 {
13041 case pragma_external:
13042 if (c_parser_next_token_is (parser, CPP_KEYWORD)
13043 && c_parser_peek_token (parser)->keyword == RID_EXTENSION)
13044 {
13045 int ext = disable_extension_diagnostics ();
13046 do
13047 c_parser_consume_token (parser);
13048 while (c_parser_next_token_is (parser, CPP_KEYWORD)
13049 && c_parser_peek_token (parser)->keyword == RID_EXTENSION);
13050 c_parser_declaration_or_fndef (parser, true, true, true, false, true,
13051 NULL, clauses);
13052 restore_extension_diagnostics (ext);
13053 }
13054 else
13055 c_parser_declaration_or_fndef (parser, true, true, true, false, true,
13056 NULL, clauses);
13057 break;
13058 case pragma_struct:
13059 case pragma_param:
13060 c_parser_error (parser, "%<#pragma omp declare simd%> must be followed by "
13061 "function declaration or definition");
13062 break;
13063 case pragma_compound:
13064 case pragma_stmt:
13065 if (c_parser_next_token_is (parser, CPP_KEYWORD)
13066 && c_parser_peek_token (parser)->keyword == RID_EXTENSION)
13067 {
13068 int ext = disable_extension_diagnostics ();
13069 do
13070 c_parser_consume_token (parser);
13071 while (c_parser_next_token_is (parser, CPP_KEYWORD)
13072 && c_parser_peek_token (parser)->keyword == RID_EXTENSION);
13073 if (c_parser_next_tokens_start_declaration (parser))
13074 {
13075 c_parser_declaration_or_fndef (parser, true, true, true, true,
13076 true, NULL, clauses);
13077 restore_extension_diagnostics (ext);
13078 break;
13079 }
13080 restore_extension_diagnostics (ext);
13081 }
13082 else if (c_parser_next_tokens_start_declaration (parser))
13083 {
13084 c_parser_declaration_or_fndef (parser, true, true, true, true, true,
13085 NULL, clauses);
13086 break;
13087 }
13088 c_parser_error (parser, "%<#pragma omp declare simd%> must be followed by "
13089 "function declaration or definition");
13090 break;
13091 default:
13092 gcc_unreachable ();
13093 }
13094 clauses.release ();
953ff289
DN
13095}
13096
acf0174b
JJ
13097/* Finalize #pragma omp declare simd clauses after FNDECL has been parsed,
13098 and put that into "omp declare simd" attribute. */
c2255bc4 13099
acf0174b
JJ
13100static void
13101c_finish_omp_declare_simd (c_parser *parser, tree fndecl, tree parms,
13102 vec<c_token> clauses)
13103{
b72271b9 13104 if (flag_cilkplus
41958c28
BI
13105 && clauses.exists () && !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
13106 {
13107 error ("%<#pragma omp declare simd%> cannot be used in the same "
13108 "function marked as a Cilk Plus SIMD-enabled function");
13109 vec_free (parser->cilk_simd_fn_tokens);
13110 return;
13111 }
13112
acf0174b
JJ
13113 /* Normally first token is CPP_NAME "simd". CPP_EOF there indicates
13114 error has been reported and CPP_PRAGMA that c_finish_omp_declare_simd
13115 has already processed the tokens. */
41958c28 13116 if (clauses.exists () && clauses[0].type == CPP_EOF)
acf0174b
JJ
13117 return;
13118 if (fndecl == NULL_TREE || TREE_CODE (fndecl) != FUNCTION_DECL)
13119 {
13120 error ("%<#pragma omp declare simd%> not immediately followed by "
13121 "a function declaration or definition");
13122 clauses[0].type = CPP_EOF;
13123 return;
13124 }
41958c28 13125 if (clauses.exists () && clauses[0].type != CPP_NAME)
acf0174b
JJ
13126 {
13127 error_at (DECL_SOURCE_LOCATION (fndecl),
13128 "%<#pragma omp declare simd%> not immediately followed by "
13129 "a single function declaration or definition");
13130 clauses[0].type = CPP_EOF;
13131 return;
13132 }
953ff289 13133
acf0174b
JJ
13134 if (parms == NULL_TREE)
13135 parms = DECL_ARGUMENTS (fndecl);
953ff289 13136
acf0174b
JJ
13137 unsigned int tokens_avail = parser->tokens_avail;
13138 gcc_assert (parser->tokens == &parser->tokens_buf[0]);
41958c28
BI
13139 bool is_cilkplus_cilk_simd_fn = false;
13140
b72271b9 13141 if (flag_cilkplus && !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
41958c28
BI
13142 {
13143 parser->tokens = parser->cilk_simd_fn_tokens->address ();
13144 parser->tokens_avail = vec_safe_length (parser->cilk_simd_fn_tokens);
13145 is_cilkplus_cilk_simd_fn = true;
13146 }
13147 else
13148 {
13149 parser->tokens = clauses.address ();
13150 parser->tokens_avail = clauses.length ();
13151 }
13152
acf0174b
JJ
13153 /* c_parser_omp_declare_simd pushed 2 extra CPP_EOF tokens at the end. */
13154 while (parser->tokens_avail > 3)
13155 {
13156 c_token *token = c_parser_peek_token (parser);
41958c28
BI
13157 if (!is_cilkplus_cilk_simd_fn)
13158 gcc_assert (token->type == CPP_NAME
13159 && strcmp (IDENTIFIER_POINTER (token->value), "simd") == 0);
13160 else
13161 gcc_assert (token->type == CPP_NAME
13162 && is_cilkplus_vector_p (token->value));
acf0174b
JJ
13163 c_parser_consume_token (parser);
13164 parser->in_pragma = true;
953ff289 13165
41958c28
BI
13166 tree c = NULL_TREE;
13167 if (is_cilkplus_cilk_simd_fn)
13168 c = c_parser_omp_all_clauses (parser, CILK_SIMD_FN_CLAUSE_MASK,
13169 "SIMD-enabled functions attribute");
13170 else
13171 c = c_parser_omp_all_clauses (parser, OMP_DECLARE_SIMD_CLAUSE_MASK,
13172 "#pragma omp declare simd");
acf0174b
JJ
13173 c = c_omp_declare_simd_clauses_to_numbers (parms, c);
13174 if (c != NULL_TREE)
13175 c = tree_cons (NULL_TREE, c, NULL_TREE);
41958c28
BI
13176 if (is_cilkplus_cilk_simd_fn)
13177 {
74558dd9
BI
13178 tree k = build_tree_list (get_identifier ("cilk simd function"),
13179 NULL_TREE);
41958c28
BI
13180 TREE_CHAIN (k) = DECL_ATTRIBUTES (fndecl);
13181 DECL_ATTRIBUTES (fndecl) = k;
13182 }
acf0174b
JJ
13183 c = build_tree_list (get_identifier ("omp declare simd"), c);
13184 TREE_CHAIN (c) = DECL_ATTRIBUTES (fndecl);
13185 DECL_ATTRIBUTES (fndecl) = c;
13186 }
953ff289 13187
acf0174b
JJ
13188 parser->tokens = &parser->tokens_buf[0];
13189 parser->tokens_avail = tokens_avail;
41958c28
BI
13190 if (clauses.exists ())
13191 clauses[0].type = CPP_PRAGMA;
13192
13193 if (!vec_safe_is_empty (parser->cilk_simd_fn_tokens))
13194 vec_free (parser->cilk_simd_fn_tokens);
953ff289
DN
13195}
13196
953ff289 13197
acf0174b
JJ
13198/* OpenMP 4.0:
13199 # pragma omp declare target new-line
13200 declarations and definitions
13201 # pragma omp end declare target new-line */
953ff289 13202
acf0174b
JJ
13203static void
13204c_parser_omp_declare_target (c_parser *parser)
953ff289 13205{
acf0174b
JJ
13206 c_parser_skip_to_pragma_eol (parser);
13207 current_omp_declare_target_attribute++;
13208}
953ff289 13209
acf0174b
JJ
13210static void
13211c_parser_omp_end_declare_target (c_parser *parser)
13212{
13213 location_t loc = c_parser_peek_token (parser)->location;
13214 c_parser_consume_pragma (parser);
13215 if (c_parser_next_token_is (parser, CPP_NAME)
13216 && strcmp (IDENTIFIER_POINTER (c_parser_peek_token (parser)->value),
13217 "declare") == 0)
953ff289
DN
13218 {
13219 c_parser_consume_token (parser);
acf0174b
JJ
13220 if (c_parser_next_token_is (parser, CPP_NAME)
13221 && strcmp (IDENTIFIER_POINTER (c_parser_peek_token (parser)->value),
13222 "target") == 0)
13223 c_parser_consume_token (parser);
13224 else
953ff289 13225 {
acf0174b
JJ
13226 c_parser_error (parser, "expected %<target%>");
13227 c_parser_skip_to_pragma_eol (parser);
13228 return;
953ff289
DN
13229 }
13230 }
acf0174b
JJ
13231 else
13232 {
13233 c_parser_error (parser, "expected %<declare%>");
13234 c_parser_skip_to_pragma_eol (parser);
13235 return;
13236 }
13237 c_parser_skip_to_pragma_eol (parser);
13238 if (!current_omp_declare_target_attribute)
13239 error_at (loc, "%<#pragma omp end declare target%> without corresponding "
13240 "%<#pragma omp declare target%>");
13241 else
13242 current_omp_declare_target_attribute--;
13243}
953ff289 13244
953ff289 13245
acf0174b
JJ
13246/* OpenMP 4.0
13247 #pragma omp declare reduction (reduction-id : typename-list : expression) \
13248 initializer-clause[opt] new-line
13249
13250 initializer-clause:
13251 initializer (omp_priv = initializer)
13252 initializer (function-name (argument-list)) */
13253
13254static void
13255c_parser_omp_declare_reduction (c_parser *parser, enum pragma_context context)
13256{
13257 unsigned int tokens_avail = 0, i;
13258 vec<tree> types = vNULL;
13259 vec<c_token> clauses = vNULL;
13260 enum tree_code reduc_code = ERROR_MARK;
13261 tree reduc_id = NULL_TREE;
13262 tree type;
13263 location_t rloc = c_parser_peek_token (parser)->location;
13264
13265 if (context == pragma_struct || context == pragma_param)
953ff289 13266 {
acf0174b
JJ
13267 error ("%<#pragma omp declare reduction%> not at file or block scope");
13268 goto fail;
13269 }
953ff289 13270
acf0174b
JJ
13271 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
13272 goto fail;
953ff289 13273
acf0174b
JJ
13274 switch (c_parser_peek_token (parser)->type)
13275 {
13276 case CPP_PLUS:
13277 reduc_code = PLUS_EXPR;
13278 break;
13279 case CPP_MULT:
13280 reduc_code = MULT_EXPR;
13281 break;
13282 case CPP_MINUS:
13283 reduc_code = MINUS_EXPR;
13284 break;
13285 case CPP_AND:
13286 reduc_code = BIT_AND_EXPR;
13287 break;
13288 case CPP_XOR:
13289 reduc_code = BIT_XOR_EXPR;
13290 break;
13291 case CPP_OR:
13292 reduc_code = BIT_IOR_EXPR;
13293 break;
13294 case CPP_AND_AND:
13295 reduc_code = TRUTH_ANDIF_EXPR;
13296 break;
13297 case CPP_OR_OR:
13298 reduc_code = TRUTH_ORIF_EXPR;
13299 break;
13300 case CPP_NAME:
13301 const char *p;
13302 p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
13303 if (strcmp (p, "min") == 0)
13304 {
13305 reduc_code = MIN_EXPR;
13306 break;
13307 }
13308 if (strcmp (p, "max") == 0)
13309 {
13310 reduc_code = MAX_EXPR;
13311 break;
13312 }
13313 reduc_id = c_parser_peek_token (parser)->value;
953ff289 13314 break;
953ff289 13315 default:
acf0174b
JJ
13316 c_parser_error (parser,
13317 "expected %<+%>, %<*%>, %<-%>, %<&%>, "
13318 "%<^%>, %<|%>, %<&&%>, %<||%>, %<min%> or identifier");
13319 goto fail;
953ff289
DN
13320 }
13321
acf0174b
JJ
13322 tree orig_reduc_id, reduc_decl;
13323 orig_reduc_id = reduc_id;
13324 reduc_id = c_omp_reduction_id (reduc_code, reduc_id);
13325 reduc_decl = c_omp_reduction_decl (reduc_id);
13326 c_parser_consume_token (parser);
953ff289 13327
acf0174b
JJ
13328 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
13329 goto fail;
c2255bc4 13330
acf0174b
JJ
13331 while (true)
13332 {
13333 location_t loc = c_parser_peek_token (parser)->location;
13334 struct c_type_name *ctype = c_parser_type_name (parser);
13335 if (ctype != NULL)
13336 {
13337 type = groktypename (ctype, NULL, NULL);
13338 if (type == error_mark_node)
13339 ;
13340 else if ((INTEGRAL_TYPE_P (type)
13341 || TREE_CODE (type) == REAL_TYPE
13342 || TREE_CODE (type) == COMPLEX_TYPE)
13343 && orig_reduc_id == NULL_TREE)
13344 error_at (loc, "predeclared arithmetic type in "
13345 "%<#pragma omp declare reduction%>");
13346 else if (TREE_CODE (type) == FUNCTION_TYPE
13347 || TREE_CODE (type) == ARRAY_TYPE)
13348 error_at (loc, "function or array type in "
13349 "%<#pragma omp declare reduction%>");
13350 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
13351 error_at (loc, "const, volatile or restrict qualified type in "
13352 "%<#pragma omp declare reduction%>");
13353 else
13354 {
13355 tree t;
13356 for (t = DECL_INITIAL (reduc_decl); t; t = TREE_CHAIN (t))
13357 if (comptypes (TREE_PURPOSE (t), type))
13358 {
13359 error_at (loc, "redeclaration of %qs "
13360 "%<#pragma omp declare reduction%> for "
13361 "type %qT",
13362 IDENTIFIER_POINTER (reduc_id)
13363 + sizeof ("omp declare reduction ") - 1,
13364 type);
13365 location_t ploc
13366 = DECL_SOURCE_LOCATION (TREE_VEC_ELT (TREE_VALUE (t),
13367 0));
13368 error_at (ploc, "previous %<#pragma omp declare "
13369 "reduction%>");
13370 break;
13371 }
13372 if (t == NULL_TREE)
13373 types.safe_push (type);
13374 }
13375 if (c_parser_next_token_is (parser, CPP_COMMA))
13376 c_parser_consume_token (parser);
13377 else
13378 break;
13379 }
13380 else
13381 break;
13382 }
953ff289 13383
acf0174b
JJ
13384 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>")
13385 || types.is_empty ())
13386 {
13387 fail:
13388 clauses.release ();
13389 types.release ();
13390 while (true)
13391 {
13392 c_token *token = c_parser_peek_token (parser);
13393 if (token->type == CPP_EOF || token->type == CPP_PRAGMA_EOL)
13394 break;
13395 c_parser_consume_token (parser);
13396 }
13397 c_parser_skip_to_pragma_eol (parser);
13398 return;
13399 }
953ff289 13400
acf0174b
JJ
13401 if (types.length () > 1)
13402 {
13403 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
13404 {
13405 c_token *token = c_parser_peek_token (parser);
13406 if (token->type == CPP_EOF)
13407 goto fail;
13408 clauses.safe_push (*token);
13409 c_parser_consume_token (parser);
13410 }
13411 clauses.safe_push (*c_parser_peek_token (parser));
13412 c_parser_skip_to_pragma_eol (parser);
953ff289 13413
acf0174b
JJ
13414 /* Make sure nothing tries to read past the end of the tokens. */
13415 c_token eof_token;
13416 memset (&eof_token, 0, sizeof (eof_token));
13417 eof_token.type = CPP_EOF;
13418 clauses.safe_push (eof_token);
13419 clauses.safe_push (eof_token);
13420 }
953ff289 13421
acf0174b
JJ
13422 int errs = errorcount;
13423 FOR_EACH_VEC_ELT (types, i, type)
13424 {
13425 tokens_avail = parser->tokens_avail;
13426 gcc_assert (parser->tokens == &parser->tokens_buf[0]);
13427 if (!clauses.is_empty ())
13428 {
13429 parser->tokens = clauses.address ();
13430 parser->tokens_avail = clauses.length ();
13431 parser->in_pragma = true;
13432 }
953ff289 13433
acf0174b
JJ
13434 bool nested = current_function_decl != NULL_TREE;
13435 if (nested)
13436 c_push_function_context ();
13437 tree fndecl = build_decl (BUILTINS_LOCATION, FUNCTION_DECL,
13438 reduc_id, default_function_type);
13439 current_function_decl = fndecl;
13440 allocate_struct_function (fndecl, true);
13441 push_scope ();
13442 tree stmt = push_stmt_list ();
13443 /* Intentionally BUILTINS_LOCATION, so that -Wshadow doesn't
13444 warn about these. */
13445 tree omp_out = build_decl (BUILTINS_LOCATION, VAR_DECL,
13446 get_identifier ("omp_out"), type);
13447 DECL_ARTIFICIAL (omp_out) = 1;
13448 DECL_CONTEXT (omp_out) = fndecl;
13449 pushdecl (omp_out);
13450 tree omp_in = build_decl (BUILTINS_LOCATION, VAR_DECL,
13451 get_identifier ("omp_in"), type);
13452 DECL_ARTIFICIAL (omp_in) = 1;
13453 DECL_CONTEXT (omp_in) = fndecl;
13454 pushdecl (omp_in);
13455 struct c_expr combiner = c_parser_expression (parser);
13456 struct c_expr initializer;
13457 tree omp_priv = NULL_TREE, omp_orig = NULL_TREE;
13458 bool bad = false;
13459 initializer.value = error_mark_node;
13460 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
13461 bad = true;
13462 else if (c_parser_next_token_is (parser, CPP_NAME)
13463 && strcmp (IDENTIFIER_POINTER
13464 (c_parser_peek_token (parser)->value),
13465 "initializer") == 0)
13466 {
13467 c_parser_consume_token (parser);
13468 pop_scope ();
13469 push_scope ();
13470 omp_priv = build_decl (BUILTINS_LOCATION, VAR_DECL,
13471 get_identifier ("omp_priv"), type);
13472 DECL_ARTIFICIAL (omp_priv) = 1;
13473 DECL_INITIAL (omp_priv) = error_mark_node;
13474 DECL_CONTEXT (omp_priv) = fndecl;
13475 pushdecl (omp_priv);
13476 omp_orig = build_decl (BUILTINS_LOCATION, VAR_DECL,
13477 get_identifier ("omp_orig"), type);
13478 DECL_ARTIFICIAL (omp_orig) = 1;
13479 DECL_CONTEXT (omp_orig) = fndecl;
13480 pushdecl (omp_orig);
13481 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
13482 bad = true;
13483 else if (!c_parser_next_token_is (parser, CPP_NAME))
13484 {
13485 c_parser_error (parser, "expected %<omp_priv%> or "
13486 "function-name");
13487 bad = true;
13488 }
13489 else if (strcmp (IDENTIFIER_POINTER
13490 (c_parser_peek_token (parser)->value),
13491 "omp_priv") != 0)
13492 {
13493 if (c_parser_peek_2nd_token (parser)->type != CPP_OPEN_PAREN
13494 || c_parser_peek_token (parser)->id_kind != C_ID_ID)
13495 {
13496 c_parser_error (parser, "expected function-name %<(%>");
13497 bad = true;
13498 }
13499 else
13500 initializer = c_parser_postfix_expression (parser);
13501 if (initializer.value
13502 && TREE_CODE (initializer.value) == CALL_EXPR)
13503 {
13504 int j;
13505 tree c = initializer.value;
13506 for (j = 0; j < call_expr_nargs (c); j++)
13507 if (TREE_CODE (CALL_EXPR_ARG (c, j)) == ADDR_EXPR
13508 && TREE_OPERAND (CALL_EXPR_ARG (c, j), 0) == omp_priv)
13509 break;
13510 if (j == call_expr_nargs (c))
13511 error ("one of the initializer call arguments should be "
13512 "%<&omp_priv%>");
13513 }
13514 }
13515 else
13516 {
13517 c_parser_consume_token (parser);
13518 if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
13519 bad = true;
13520 else
13521 {
13522 tree st = push_stmt_list ();
13523 start_init (omp_priv, NULL_TREE, 0);
13524 location_t loc = c_parser_peek_token (parser)->location;
13525 struct c_expr init = c_parser_initializer (parser);
13526 finish_init ();
13527 finish_decl (omp_priv, loc, init.value,
13528 init.original_type, NULL_TREE);
13529 pop_stmt_list (st);
13530 }
13531 }
13532 if (!bad
13533 && !c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
13534 bad = true;
13535 }
c2255bc4 13536
acf0174b
JJ
13537 if (!bad)
13538 {
13539 c_parser_skip_to_pragma_eol (parser);
a68ab351 13540
acf0174b
JJ
13541 tree t = tree_cons (type, make_tree_vec (omp_priv ? 6 : 3),
13542 DECL_INITIAL (reduc_decl));
13543 DECL_INITIAL (reduc_decl) = t;
13544 DECL_SOURCE_LOCATION (omp_out) = rloc;
13545 TREE_VEC_ELT (TREE_VALUE (t), 0) = omp_out;
13546 TREE_VEC_ELT (TREE_VALUE (t), 1) = omp_in;
13547 TREE_VEC_ELT (TREE_VALUE (t), 2) = combiner.value;
13548 walk_tree (&combiner.value, c_check_omp_declare_reduction_r,
13549 &TREE_VEC_ELT (TREE_VALUE (t), 0), NULL);
13550 if (omp_priv)
13551 {
13552 DECL_SOURCE_LOCATION (omp_priv) = rloc;
13553 TREE_VEC_ELT (TREE_VALUE (t), 3) = omp_priv;
13554 TREE_VEC_ELT (TREE_VALUE (t), 4) = omp_orig;
13555 TREE_VEC_ELT (TREE_VALUE (t), 5) = initializer.value;
13556 walk_tree (&initializer.value, c_check_omp_declare_reduction_r,
13557 &TREE_VEC_ELT (TREE_VALUE (t), 3), NULL);
13558 walk_tree (&DECL_INITIAL (omp_priv),
13559 c_check_omp_declare_reduction_r,
13560 &TREE_VEC_ELT (TREE_VALUE (t), 3), NULL);
13561 }
13562 }
a68ab351 13563
acf0174b
JJ
13564 pop_stmt_list (stmt);
13565 pop_scope ();
13566 if (cfun->language != NULL)
13567 {
13568 ggc_free (cfun->language);
13569 cfun->language = NULL;
13570 }
13571 set_cfun (NULL);
13572 current_function_decl = NULL_TREE;
13573 if (nested)
13574 c_pop_function_context ();
a68ab351 13575
acf0174b
JJ
13576 if (!clauses.is_empty ())
13577 {
13578 parser->tokens = &parser->tokens_buf[0];
13579 parser->tokens_avail = tokens_avail;
13580 }
13581 if (bad)
13582 goto fail;
13583 if (errs != errorcount)
13584 break;
13585 }
a68ab351 13586
acf0174b
JJ
13587 clauses.release ();
13588 types.release ();
a68ab351
JJ
13589}
13590
953ff289 13591
acf0174b
JJ
13592/* OpenMP 4.0
13593 #pragma omp declare simd declare-simd-clauses[optseq] new-line
13594 #pragma omp declare reduction (reduction-id : typename-list : expression) \
13595 initializer-clause[opt] new-line
13596 #pragma omp declare target new-line */
20906c66
JJ
13597
13598static void
acf0174b 13599c_parser_omp_declare (c_parser *parser, enum pragma_context context)
20906c66 13600{
20906c66 13601 c_parser_consume_pragma (parser);
acf0174b
JJ
13602 if (c_parser_next_token_is (parser, CPP_NAME))
13603 {
13604 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
13605 if (strcmp (p, "simd") == 0)
13606 {
13607 /* c_parser_consume_token (parser); done in
13608 c_parser_omp_declare_simd. */
13609 c_parser_omp_declare_simd (parser, context);
13610 return;
13611 }
13612 if (strcmp (p, "reduction") == 0)
13613 {
13614 c_parser_consume_token (parser);
13615 c_parser_omp_declare_reduction (parser, context);
13616 return;
13617 }
6d7f7e0a
TB
13618 if (!flag_openmp) /* flag_openmp_simd */
13619 {
13620 c_parser_skip_to_pragma_eol (parser);
13621 return;
13622 }
acf0174b
JJ
13623 if (strcmp (p, "target") == 0)
13624 {
13625 c_parser_consume_token (parser);
13626 c_parser_omp_declare_target (parser);
13627 return;
13628 }
13629 }
20906c66 13630
acf0174b
JJ
13631 c_parser_error (parser, "expected %<simd%> or %<reduction%> "
13632 "or %<target%>");
13633 c_parser_skip_to_pragma_eol (parser);
20906c66
JJ
13634}
13635
953ff289
DN
13636/* Main entry point to parsing most OpenMP pragmas. */
13637
13638static void
13639c_parser_omp_construct (c_parser *parser)
13640{
13641 enum pragma_kind p_kind;
13642 location_t loc;
13643 tree stmt;
acf0174b
JJ
13644 char p_name[sizeof "#pragma omp teams distribute parallel for simd"];
13645 omp_clause_mask mask (0);
953ff289
DN
13646
13647 loc = c_parser_peek_token (parser)->location;
13648 p_kind = c_parser_peek_token (parser)->pragma_kind;
13649 c_parser_consume_pragma (parser);
13650
13651 switch (p_kind)
13652 {
13653 case PRAGMA_OMP_ATOMIC:
c2255bc4 13654 c_parser_omp_atomic (loc, parser);
953ff289
DN
13655 return;
13656 case PRAGMA_OMP_CRITICAL:
c2255bc4 13657 stmt = c_parser_omp_critical (loc, parser);
953ff289 13658 break;
acf0174b
JJ
13659 case PRAGMA_OMP_DISTRIBUTE:
13660 strcpy (p_name, "#pragma omp");
13661 stmt = c_parser_omp_distribute (loc, parser, p_name, mask, NULL);
13662 break;
953ff289 13663 case PRAGMA_OMP_FOR:
acf0174b
JJ
13664 strcpy (p_name, "#pragma omp");
13665 stmt = c_parser_omp_for (loc, parser, p_name, mask, NULL);
953ff289
DN
13666 break;
13667 case PRAGMA_OMP_MASTER:
c2255bc4 13668 stmt = c_parser_omp_master (loc, parser);
953ff289
DN
13669 break;
13670 case PRAGMA_OMP_ORDERED:
c2255bc4 13671 stmt = c_parser_omp_ordered (loc, parser);
953ff289
DN
13672 break;
13673 case PRAGMA_OMP_PARALLEL:
acf0174b
JJ
13674 strcpy (p_name, "#pragma omp");
13675 stmt = c_parser_omp_parallel (loc, parser, p_name, mask, NULL);
953ff289
DN
13676 break;
13677 case PRAGMA_OMP_SECTIONS:
acf0174b
JJ
13678 strcpy (p_name, "#pragma omp");
13679 stmt = c_parser_omp_sections (loc, parser, p_name, mask, NULL);
13680 break;
13681 case PRAGMA_OMP_SIMD:
13682 strcpy (p_name, "#pragma omp");
13683 stmt = c_parser_omp_simd (loc, parser, p_name, mask, NULL);
953ff289
DN
13684 break;
13685 case PRAGMA_OMP_SINGLE:
c2255bc4 13686 stmt = c_parser_omp_single (loc, parser);
953ff289 13687 break;
a68ab351 13688 case PRAGMA_OMP_TASK:
c2255bc4 13689 stmt = c_parser_omp_task (loc, parser);
a68ab351 13690 break;
acf0174b
JJ
13691 case PRAGMA_OMP_TASKGROUP:
13692 stmt = c_parser_omp_taskgroup (parser);
13693 break;
13694 case PRAGMA_OMP_TEAMS:
13695 strcpy (p_name, "#pragma omp");
13696 stmt = c_parser_omp_teams (loc, parser, p_name, mask, NULL);
13697 break;
953ff289
DN
13698 default:
13699 gcc_unreachable ();
13700 }
13701
13702 if (stmt)
c2255bc4 13703 gcc_assert (EXPR_LOCATION (stmt) != UNKNOWN_LOCATION);
953ff289
DN
13704}
13705
13706
13707/* OpenMP 2.5:
13708 # pragma omp threadprivate (variable-list) */
13709
13710static void
13711c_parser_omp_threadprivate (c_parser *parser)
13712{
13713 tree vars, t;
c2255bc4 13714 location_t loc;
953ff289
DN
13715
13716 c_parser_consume_pragma (parser);
c2255bc4 13717 loc = c_parser_peek_token (parser)->location;
d75d71e0 13718 vars = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_ERROR, NULL);
953ff289 13719
953ff289
DN
13720 /* Mark every variable in VARS to be assigned thread local storage. */
13721 for (t = vars; t; t = TREE_CHAIN (t))
13722 {
13723 tree v = TREE_PURPOSE (t);
13724
c2255bc4
AH
13725 /* FIXME diagnostics: Ideally we should keep individual
13726 locations for all the variables in the var list to make the
13727 following errors more precise. Perhaps
13728 c_parser_omp_var_list_parens() should construct a list of
13729 locations to go along with the var list. */
13730
953ff289
DN
13731 /* If V had already been marked threadprivate, it doesn't matter
13732 whether it had been used prior to this point. */
5df27e4a 13733 if (TREE_CODE (v) != VAR_DECL)
c2255bc4 13734 error_at (loc, "%qD is not a variable", v);
5df27e4a 13735 else if (TREE_USED (v) && !C_DECL_THREADPRIVATE_P (v))
c2255bc4 13736 error_at (loc, "%qE declared %<threadprivate%> after first use", v);
953ff289 13737 else if (! TREE_STATIC (v) && ! DECL_EXTERNAL (v))
c2255bc4 13738 error_at (loc, "automatic variable %qE cannot be %<threadprivate%>", v);
5df27e4a
JJ
13739 else if (TREE_TYPE (v) == error_mark_node)
13740 ;
953ff289 13741 else if (! COMPLETE_TYPE_P (TREE_TYPE (v)))
c2255bc4 13742 error_at (loc, "%<threadprivate%> %qE has incomplete type", v);
953ff289
DN
13743 else
13744 {
13745 if (! DECL_THREAD_LOCAL_P (v))
13746 {
56363ffd 13747 set_decl_tls_model (v, decl_default_tls_model (v));
953ff289
DN
13748 /* If rtl has been already set for this var, call
13749 make_decl_rtl once again, so that encode_section_info
13750 has a chance to look at the new decl flags. */
13751 if (DECL_RTL_SET_P (v))
13752 make_decl_rtl (v);
13753 }
13754 C_DECL_THREADPRIVATE_P (v) = 1;
13755 }
13756 }
13757
13758 c_parser_skip_to_pragma_eol (parser);
13759}
c02065fc
AH
13760\f
13761/* Cilk Plus <#pragma simd> parsing routines. */
13762
13763/* Helper function for c_parser_pragma. Perform some sanity checking
13764 for <#pragma simd> constructs. Returns FALSE if there was a
13765 problem. */
13766
13767static bool
13768c_parser_cilk_verify_simd (c_parser *parser,
13769 enum pragma_context context)
13770{
b72271b9 13771 if (!flag_cilkplus)
c02065fc
AH
13772 {
13773 warning (0, "pragma simd ignored because -fcilkplus is not enabled");
13774 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
13775 return false;
13776 }
13777 if (context == pragma_external)
13778 {
13779 c_parser_error (parser,"pragma simd must be inside a function");
13780 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
13781 return false;
13782 }
13783 return true;
13784}
13785
13786/* Cilk Plus:
41958c28
BI
13787 This function is shared by SIMD-enabled functions and #pragma simd.
13788 If IS_SIMD_FN is true then it is parsing a SIMD-enabled function and
13789 CLAUSES is unused. The main purpose of this function is to parse a
13790 vectorlength attribute or clause and check for parse errors.
13791 When IS_SIMD_FN is true then the function is merely caching the tokens
13792 in PARSER->CILK_SIMD_FN_TOKENS. If errors are found then the token
13793 cache is cleared since there is no reason to continue.
13794 Syntax:
13795 vectorlength ( constant-expression ) */
c02065fc
AH
13796
13797static tree
41958c28
BI
13798c_parser_cilk_clause_vectorlength (c_parser *parser, tree clauses,
13799 bool is_simd_fn)
c02065fc 13800{
41958c28
BI
13801 if (is_simd_fn)
13802 check_no_duplicate_clause (clauses, OMP_CLAUSE_SIMDLEN, "vectorlength");
13803 else
c02065fc
AH
13804 /* The vectorlength clause behaves exactly like OpenMP's safelen
13805 clause. Represent it in OpenMP terms. */
41958c28 13806 check_no_duplicate_clause (clauses, OMP_CLAUSE_SAFELEN, "vectorlength");
c02065fc
AH
13807
13808 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
13809 return clauses;
13810
13811 location_t loc = c_parser_peek_token (parser)->location;
13812 tree expr = c_parser_expr_no_commas (parser, NULL).value;
13813 expr = c_fully_fold (expr, false, NULL);
13814
41958c28
BI
13815 /* If expr is an error_mark_node then the above function would have
13816 emitted an error. No reason to do it twice. */
13817 if (expr == error_mark_node)
13818 ;
13819 else if (!TREE_TYPE (expr)
13820 || !TREE_CONSTANT (expr)
13821 || !INTEGRAL_TYPE_P (TREE_TYPE (expr)))
13822
13823 error_at (loc, "vectorlength must be an integer constant");
807e902e 13824 else if (wi::exact_log2 (expr) == -1)
c02065fc
AH
13825 error_at (loc, "vectorlength must be a power of 2");
13826 else
13827 {
41958c28
BI
13828 if (is_simd_fn)
13829 {
13830 tree u = build_omp_clause (loc, OMP_CLAUSE_SIMDLEN);
13831 OMP_CLAUSE_SIMDLEN_EXPR (u) = expr;
13832 OMP_CLAUSE_CHAIN (u) = clauses;
13833 clauses = u;
13834 }
13835 else
13836 {
13837 tree u = build_omp_clause (loc, OMP_CLAUSE_SAFELEN);
13838 OMP_CLAUSE_SAFELEN_EXPR (u) = expr;
13839 OMP_CLAUSE_CHAIN (u) = clauses;
13840 clauses = u;
13841 }
c02065fc
AH
13842 }
13843
13844 c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
13845
13846 return clauses;
13847}
13848
13849/* Cilk Plus:
13850 linear ( simd-linear-variable-list )
13851
13852 simd-linear-variable-list:
13853 simd-linear-variable
13854 simd-linear-variable-list , simd-linear-variable
13855
13856 simd-linear-variable:
13857 id-expression
13858 id-expression : simd-linear-step
13859
13860 simd-linear-step:
13861 conditional-expression */
13862
13863static tree
13864c_parser_cilk_clause_linear (c_parser *parser, tree clauses)
13865{
13866 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
13867 return clauses;
13868
13869 location_t loc = c_parser_peek_token (parser)->location;
13870
13871 if (c_parser_next_token_is_not (parser, CPP_NAME)
13872 || c_parser_peek_token (parser)->id_kind != C_ID_ID)
13873 c_parser_error (parser, "expected identifier");
13874
13875 while (c_parser_next_token_is (parser, CPP_NAME)
13876 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
13877 {
13878 tree var = lookup_name (c_parser_peek_token (parser)->value);
13879
13880 if (var == NULL)
13881 {
13882 undeclared_variable (c_parser_peek_token (parser)->location,
13883 c_parser_peek_token (parser)->value);
13884 c_parser_consume_token (parser);
13885 }
13886 else if (var == error_mark_node)
13887 c_parser_consume_token (parser);
13888 else
13889 {
13890 tree step = integer_one_node;
13891
13892 /* Parse the linear step if present. */
13893 if (c_parser_peek_2nd_token (parser)->type == CPP_COLON)
13894 {
13895 c_parser_consume_token (parser);
13896 c_parser_consume_token (parser);
13897
13898 tree expr = c_parser_expr_no_commas (parser, NULL).value;
13899 expr = c_fully_fold (expr, false, NULL);
13900
13901 if (TREE_TYPE (expr)
13902 && INTEGRAL_TYPE_P (TREE_TYPE (expr))
13903 && (TREE_CONSTANT (expr)
13904 || DECL_P (expr)))
13905 step = expr;
13906 else
13907 c_parser_error (parser,
13908 "step size must be an integer constant "
13909 "expression or an integer variable");
13910 }
13911 else
13912 c_parser_consume_token (parser);
13913
13914 /* Use OMP_CLAUSE_LINEAR, which has the same semantics. */
13915 tree u = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
13916 OMP_CLAUSE_DECL (u) = var;
13917 OMP_CLAUSE_LINEAR_STEP (u) = step;
13918 OMP_CLAUSE_CHAIN (u) = clauses;
13919 clauses = u;
13920 }
13921
13922 if (c_parser_next_token_is_not (parser, CPP_COMMA))
13923 break;
13924
13925 c_parser_consume_token (parser);
13926 }
13927
13928 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
13929
13930 return clauses;
13931}
13932
13933/* Returns the name of the next clause. If the clause is not
13934 recognized SIMD_OMP_CLAUSE_NONE is returned and the next token is
13935 not consumed. Otherwise, the appropriate pragma_simd_clause is
13936 returned and the token is consumed. */
13937
41958c28 13938static pragma_omp_clause
c02065fc
AH
13939c_parser_cilk_clause_name (c_parser *parser)
13940{
41958c28 13941 pragma_omp_clause result;
c02065fc
AH
13942 c_token *token = c_parser_peek_token (parser);
13943
13944 if (!token->value || token->type != CPP_NAME)
13945 return PRAGMA_CILK_CLAUSE_NONE;
13946
13947 const char *p = IDENTIFIER_POINTER (token->value);
13948
13949 if (!strcmp (p, "vectorlength"))
13950 result = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
13951 else if (!strcmp (p, "linear"))
13952 result = PRAGMA_CILK_CLAUSE_LINEAR;
13953 else if (!strcmp (p, "private"))
13954 result = PRAGMA_CILK_CLAUSE_PRIVATE;
13955 else if (!strcmp (p, "firstprivate"))
13956 result = PRAGMA_CILK_CLAUSE_FIRSTPRIVATE;
13957 else if (!strcmp (p, "lastprivate"))
13958 result = PRAGMA_CILK_CLAUSE_LASTPRIVATE;
13959 else if (!strcmp (p, "reduction"))
13960 result = PRAGMA_CILK_CLAUSE_REDUCTION;
13961 else
13962 return PRAGMA_CILK_CLAUSE_NONE;
13963
13964 c_parser_consume_token (parser);
13965 return result;
13966}
13967
13968/* Parse all #<pragma simd> clauses. Return the list of clauses
13969 found. */
13970
13971static tree
13972c_parser_cilk_all_clauses (c_parser *parser)
13973{
13974 tree clauses = NULL;
13975
13976 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
13977 {
41958c28 13978 pragma_omp_clause c_kind;
c02065fc
AH
13979
13980 c_kind = c_parser_cilk_clause_name (parser);
13981
13982 switch (c_kind)
13983 {
13984 case PRAGMA_CILK_CLAUSE_VECTORLENGTH:
41958c28 13985 clauses = c_parser_cilk_clause_vectorlength (parser, clauses, false);
c02065fc
AH
13986 break;
13987 case PRAGMA_CILK_CLAUSE_LINEAR:
13988 clauses = c_parser_cilk_clause_linear (parser, clauses);
13989 break;
13990 case PRAGMA_CILK_CLAUSE_PRIVATE:
13991 /* Use the OpenMP counterpart. */
13992 clauses = c_parser_omp_clause_private (parser, clauses);
13993 break;
13994 case PRAGMA_CILK_CLAUSE_FIRSTPRIVATE:
13995 /* Use the OpenMP counterpart. */
13996 clauses = c_parser_omp_clause_firstprivate (parser, clauses);
13997 break;
13998 case PRAGMA_CILK_CLAUSE_LASTPRIVATE:
13999 /* Use the OpenMP counterpart. */
14000 clauses = c_parser_omp_clause_lastprivate (parser, clauses);
14001 break;
14002 case PRAGMA_CILK_CLAUSE_REDUCTION:
14003 /* Use the OpenMP counterpart. */
14004 clauses = c_parser_omp_clause_reduction (parser, clauses);
14005 break;
14006 default:
14007 c_parser_error (parser, "expected %<#pragma simd%> clause");
14008 goto saw_error;
14009 }
14010 }
14011
14012 saw_error:
14013 c_parser_skip_to_pragma_eol (parser);
14014 return c_finish_cilk_clauses (clauses);
14015}
14016
9a771876
JJ
14017/* This function helps parse the grainsize pragma for a _Cilk_for statement.
14018 Here is the correct syntax of this pragma:
14019 #pragma cilk grainsize = <EXP>
14020 */
14021
14022static void
14023c_parser_cilk_grainsize (c_parser *parser)
14024{
14025 extern tree convert_to_integer (tree, tree);
14026
14027 /* consume the 'grainsize' keyword. */
14028 c_parser_consume_pragma (parser);
14029
14030 if (c_parser_require (parser, CPP_EQ, "expected %<=%>") != 0)
14031 {
14032 struct c_expr g_expr = c_parser_binary_expression (parser, NULL, NULL);
14033 if (g_expr.value == error_mark_node)
14034 {
14035 c_parser_skip_to_pragma_eol (parser);
14036 return;
14037 }
14038 tree grain = convert_to_integer (long_integer_type_node,
14039 c_fully_fold (g_expr.value, false,
14040 NULL));
14041 c_parser_skip_to_pragma_eol (parser);
14042 c_token *token = c_parser_peek_token (parser);
14043 if (token && token->type == CPP_KEYWORD
14044 && token->keyword == RID_CILK_FOR)
14045 {
14046 if (grain == NULL_TREE || grain == error_mark_node)
14047 grain = integer_zero_node;
14048 c_parser_cilk_for (parser, grain);
14049 }
14050 else
14051 warning (0, "%<#pragma cilk grainsize%> is not followed by "
14052 "%<_Cilk_for%>");
14053 }
14054 else
14055 c_parser_skip_to_pragma_eol (parser);
14056}
14057
14058/* Main entry point for parsing Cilk Plus <#pragma simd> for loops. */
953ff289 14059
c02065fc 14060static void
e7bd1de1 14061c_parser_cilk_simd (c_parser *parser)
c02065fc 14062{
c02065fc
AH
14063 tree clauses = c_parser_cilk_all_clauses (parser);
14064 tree block = c_begin_compound_stmt (true);
14065 location_t loc = c_parser_peek_token (parser)->location;
14066 c_parser_omp_for_loop (loc, parser, CILK_SIMD, clauses, NULL);
14067 block = c_end_compound_stmt (loc, block, true);
14068 add_stmt (block);
14069}
9a771876
JJ
14070
14071/* Create an artificial decl with TYPE and emit initialization of it with
14072 INIT. */
14073
14074static tree
14075c_get_temp_regvar (tree type, tree init)
14076{
14077 location_t loc = EXPR_LOCATION (init);
14078 tree decl = build_decl (loc, VAR_DECL, NULL_TREE, type);
14079 DECL_ARTIFICIAL (decl) = 1;
14080 DECL_IGNORED_P (decl) = 1;
14081 pushdecl (decl);
14082 tree t = build2 (INIT_EXPR, type, decl, init);
14083 add_stmt (t);
14084 return decl;
14085}
14086
14087/* Main entry point for parsing Cilk Plus _Cilk_for loops.
14088 GRAIN is the grain value passed in through pragma or 0. */
14089
14090static void
14091c_parser_cilk_for (c_parser *parser, tree grain)
14092{
14093 tree clauses = build_omp_clause (EXPR_LOCATION (grain), OMP_CLAUSE_SCHEDULE);
14094 OMP_CLAUSE_SCHEDULE_KIND (clauses) = OMP_CLAUSE_SCHEDULE_CILKFOR;
14095 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (clauses) = grain;
14096 clauses = c_finish_omp_clauses (clauses);
14097
14098 tree block = c_begin_compound_stmt (true);
14099 tree sb = push_stmt_list ();
14100 location_t loc = c_parser_peek_token (parser)->location;
14101 tree omp_for = c_parser_omp_for_loop (loc, parser, CILK_FOR, clauses, NULL);
14102 sb = pop_stmt_list (sb);
14103
14104 if (omp_for)
14105 {
14106 tree omp_par = make_node (OMP_PARALLEL);
14107 TREE_TYPE (omp_par) = void_type_node;
14108 OMP_PARALLEL_CLAUSES (omp_par) = NULL_TREE;
14109 tree bind = build3 (BIND_EXPR, void_type_node, NULL, NULL, NULL);
14110 TREE_SIDE_EFFECTS (bind) = 1;
14111 BIND_EXPR_BODY (bind) = sb;
14112 OMP_PARALLEL_BODY (omp_par) = bind;
14113 if (OMP_FOR_PRE_BODY (omp_for))
14114 {
14115 add_stmt (OMP_FOR_PRE_BODY (omp_for));
14116 OMP_FOR_PRE_BODY (omp_for) = NULL_TREE;
14117 }
14118 tree init = TREE_VEC_ELT (OMP_FOR_INIT (omp_for), 0);
14119 tree decl = TREE_OPERAND (init, 0);
14120 tree cond = TREE_VEC_ELT (OMP_FOR_COND (omp_for), 0);
14121 tree incr = TREE_VEC_ELT (OMP_FOR_INCR (omp_for), 0);
14122 tree t = TREE_OPERAND (cond, 1), c, clauses = NULL_TREE;
14123 if (TREE_CODE (t) != INTEGER_CST)
14124 {
14125 TREE_OPERAND (cond, 1) = c_get_temp_regvar (TREE_TYPE (t), t);
14126 c = build_omp_clause (input_location, OMP_CLAUSE_FIRSTPRIVATE);
14127 OMP_CLAUSE_DECL (c) = TREE_OPERAND (cond, 1);
14128 OMP_CLAUSE_CHAIN (c) = clauses;
14129 clauses = c;
14130 }
14131 if (TREE_CODE (incr) == MODIFY_EXPR)
14132 {
14133 t = TREE_OPERAND (TREE_OPERAND (incr, 1), 1);
14134 if (TREE_CODE (t) != INTEGER_CST)
14135 {
14136 TREE_OPERAND (TREE_OPERAND (incr, 1), 1)
14137 = c_get_temp_regvar (TREE_TYPE (t), t);
14138 c = build_omp_clause (input_location, OMP_CLAUSE_FIRSTPRIVATE);
14139 OMP_CLAUSE_DECL (c) = TREE_OPERAND (TREE_OPERAND (incr, 1), 1);
14140 OMP_CLAUSE_CHAIN (c) = clauses;
14141 clauses = c;
14142 }
14143 }
14144 t = TREE_OPERAND (init, 1);
14145 if (TREE_CODE (t) != INTEGER_CST)
14146 {
14147 TREE_OPERAND (init, 1) = c_get_temp_regvar (TREE_TYPE (t), t);
14148 c = build_omp_clause (input_location, OMP_CLAUSE_FIRSTPRIVATE);
14149 OMP_CLAUSE_DECL (c) = TREE_OPERAND (init, 1);
14150 OMP_CLAUSE_CHAIN (c) = clauses;
14151 clauses = c;
14152 }
14153 c = build_omp_clause (input_location, OMP_CLAUSE_PRIVATE);
14154 OMP_CLAUSE_DECL (c) = decl;
14155 OMP_CLAUSE_CHAIN (c) = clauses;
14156 clauses = c;
14157 c = build_omp_clause (input_location, OMP_CLAUSE__CILK_FOR_COUNT_);
14158 OMP_CLAUSE_OPERAND (c, 0)
14159 = cilk_for_number_of_iterations (omp_for);
14160 OMP_CLAUSE_CHAIN (c) = clauses;
14161 OMP_PARALLEL_CLAUSES (omp_par) = c_finish_omp_clauses (c);
14162 add_stmt (omp_par);
14163 }
14164
14165 block = c_end_compound_stmt (loc, block, true);
14166 add_stmt (block);
14167}
14168
c02065fc 14169\f
0a35513e
AH
14170/* Parse a transaction attribute (GCC Extension).
14171
14172 transaction-attribute:
14173 attributes
14174 [ [ any-word ] ]
14175
14176 The transactional memory language description is written for C++,
14177 and uses the C++0x attribute syntax. For compatibility, allow the
14178 bracket style for transactions in C as well. */
14179
14180static tree
14181c_parser_transaction_attributes (c_parser *parser)
14182{
14183 tree attr_name, attr = NULL;
14184
14185 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
14186 return c_parser_attributes (parser);
14187
14188 if (!c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
14189 return NULL_TREE;
14190 c_parser_consume_token (parser);
14191 if (!c_parser_require (parser, CPP_OPEN_SQUARE, "expected %<[%>"))
14192 goto error1;
14193
14194 attr_name = c_parser_attribute_any_word (parser);
14195 if (attr_name)
14196 {
14197 c_parser_consume_token (parser);
14198 attr = build_tree_list (attr_name, NULL_TREE);
14199 }
14200 else
14201 c_parser_error (parser, "expected identifier");
14202
14203 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, "expected %<]%>");
14204 error1:
14205 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, "expected %<]%>");
14206 return attr;
14207}
14208
14209/* Parse a __transaction_atomic or __transaction_relaxed statement
14210 (GCC Extension).
14211
14212 transaction-statement:
14213 __transaction_atomic transaction-attribute[opt] compound-statement
14214 __transaction_relaxed compound-statement
14215
14216 Note that the only valid attribute is: "outer".
14217*/
14218
14219static tree
14220c_parser_transaction (c_parser *parser, enum rid keyword)
14221{
14222 unsigned int old_in = parser->in_transaction;
14223 unsigned int this_in = 1, new_in;
14224 location_t loc = c_parser_peek_token (parser)->location;
14225 tree stmt, attrs;
14226
14227 gcc_assert ((keyword == RID_TRANSACTION_ATOMIC
14228 || keyword == RID_TRANSACTION_RELAXED)
14229 && c_parser_next_token_is_keyword (parser, keyword));
14230 c_parser_consume_token (parser);
14231
14232 if (keyword == RID_TRANSACTION_RELAXED)
14233 this_in |= TM_STMT_ATTR_RELAXED;
14234 else
14235 {
14236 attrs = c_parser_transaction_attributes (parser);
14237 if (attrs)
14238 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
14239 }
14240
14241 /* Keep track if we're in the lexical scope of an outer transaction. */
14242 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
14243
14244 parser->in_transaction = new_in;
14245 stmt = c_parser_compound_statement (parser);
14246 parser->in_transaction = old_in;
14247
14248 if (flag_tm)
14249 stmt = c_finish_transaction (loc, stmt, this_in);
14250 else
14251 error_at (loc, (keyword == RID_TRANSACTION_ATOMIC ?
14252 "%<__transaction_atomic%> without transactional memory support enabled"
14253 : "%<__transaction_relaxed %> "
14254 "without transactional memory support enabled"));
14255
14256 return stmt;
14257}
14258
14259/* Parse a __transaction_atomic or __transaction_relaxed expression
14260 (GCC Extension).
14261
14262 transaction-expression:
14263 __transaction_atomic ( expression )
14264 __transaction_relaxed ( expression )
14265*/
14266
14267static struct c_expr
14268c_parser_transaction_expression (c_parser *parser, enum rid keyword)
14269{
14270 struct c_expr ret;
14271 unsigned int old_in = parser->in_transaction;
14272 unsigned int this_in = 1;
14273 location_t loc = c_parser_peek_token (parser)->location;
14274 tree attrs;
14275
14276 gcc_assert ((keyword == RID_TRANSACTION_ATOMIC
14277 || keyword == RID_TRANSACTION_RELAXED)
14278 && c_parser_next_token_is_keyword (parser, keyword));
14279 c_parser_consume_token (parser);
14280
14281 if (keyword == RID_TRANSACTION_RELAXED)
14282 this_in |= TM_STMT_ATTR_RELAXED;
14283 else
14284 {
14285 attrs = c_parser_transaction_attributes (parser);
14286 if (attrs)
14287 this_in |= parse_tm_stmt_attr (attrs, 0);
14288 }
14289
14290 parser->in_transaction = this_in;
69b76518 14291 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
0a35513e
AH
14292 {
14293 tree expr = c_parser_expression (parser).value;
14294 ret.original_type = TREE_TYPE (expr);
14295 ret.value = build1 (TRANSACTION_EXPR, ret.original_type, expr);
14296 if (this_in & TM_STMT_ATTR_RELAXED)
14297 TRANSACTION_EXPR_RELAXED (ret.value) = 1;
14298 SET_EXPR_LOCATION (ret.value, loc);
14299 ret.original_code = TRANSACTION_EXPR;
69b76518
TR
14300 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
14301 {
14302 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
14303 goto error;
14304 }
0a35513e
AH
14305 }
14306 else
14307 {
69b76518 14308 error:
0a35513e
AH
14309 ret.value = error_mark_node;
14310 ret.original_code = ERROR_MARK;
14311 ret.original_type = NULL;
14312 }
14313 parser->in_transaction = old_in;
14314
14315 if (!flag_tm)
14316 error_at (loc, (keyword == RID_TRANSACTION_ATOMIC ?
14317 "%<__transaction_atomic%> without transactional memory support enabled"
14318 : "%<__transaction_relaxed %> "
14319 "without transactional memory support enabled"));
14320
14321 return ret;
14322}
14323
14324/* Parse a __transaction_cancel statement (GCC Extension).
14325
14326 transaction-cancel-statement:
14327 __transaction_cancel transaction-attribute[opt] ;
14328
14329 Note that the only valid attribute is "outer".
14330*/
14331
14332static tree
acf0174b 14333c_parser_transaction_cancel (c_parser *parser)
0a35513e
AH
14334{
14335 location_t loc = c_parser_peek_token (parser)->location;
14336 tree attrs;
14337 bool is_outer = false;
14338
14339 gcc_assert (c_parser_next_token_is_keyword (parser, RID_TRANSACTION_CANCEL));
14340 c_parser_consume_token (parser);
14341
14342 attrs = c_parser_transaction_attributes (parser);
14343 if (attrs)
14344 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
14345
14346 if (!flag_tm)
14347 {
14348 error_at (loc, "%<__transaction_cancel%> without "
14349 "transactional memory support enabled");
14350 goto ret_error;
14351 }
14352 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
14353 {
14354 error_at (loc, "%<__transaction_cancel%> within a "
14355 "%<__transaction_relaxed%>");
14356 goto ret_error;
14357 }
14358 else if (is_outer)
14359 {
14360 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
14361 && !is_tm_may_cancel_outer (current_function_decl))
14362 {
14363 error_at (loc, "outer %<__transaction_cancel%> not "
14364 "within outer %<__transaction_atomic%>");
14365 error_at (loc, " or a %<transaction_may_cancel_outer%> function");
14366 goto ret_error;
14367 }
14368 }
14369 else if (parser->in_transaction == 0)
14370 {
14371 error_at (loc, "%<__transaction_cancel%> not within "
14372 "%<__transaction_atomic%>");
14373 goto ret_error;
14374 }
14375
14376 return add_stmt (build_tm_abort_call (loc, is_outer));
14377
14378 ret_error:
14379 return build1 (NOP_EXPR, void_type_node, error_mark_node);
14380}
bc4071dd 14381\f
27bf414c
JM
14382/* Parse a single source file. */
14383
14384void
14385c_parse_file (void)
14386{
bc4071dd
RH
14387 /* Use local storage to begin. If the first token is a pragma, parse it.
14388 If it is #pragma GCC pch_preprocess, then this will load a PCH file
14389 which will cause garbage collection. */
14390 c_parser tparser;
14391
14392 memset (&tparser, 0, sizeof tparser);
acf0174b 14393 tparser.tokens = &tparser.tokens_buf[0];
bc4071dd
RH
14394 the_parser = &tparser;
14395
14396 if (c_parser_peek_token (&tparser)->pragma_kind == PRAGMA_GCC_PCH_PREPROCESS)
14397 c_parser_pragma_pch_preprocess (&tparser);
14398
766090c2 14399 the_parser = ggc_alloc<c_parser> ();
bc4071dd 14400 *the_parser = tparser;
acf0174b
JJ
14401 if (tparser.tokens == &tparser.tokens_buf[0])
14402 the_parser->tokens = &the_parser->tokens_buf[0];
bc4071dd 14403
f9417da1
RG
14404 /* Initialize EH, if we've been told to do so. */
14405 if (flag_exceptions)
1d65f45c 14406 using_eh_for_cleanups ();
f9417da1 14407
27bf414c
JM
14408 c_parser_translation_unit (the_parser);
14409 the_parser = NULL;
14410}
14411
36536d79
BI
14412/* This function parses Cilk Plus array notation. The starting index is
14413 passed in INITIAL_INDEX and the array name is passes in ARRAY_VALUE. The
14414 return value of this function is a tree_node called VALUE_TREE of type
14415 ARRAY_NOTATION_REF. */
14416
14417static tree
14418c_parser_array_notation (location_t loc, c_parser *parser, tree initial_index,
14419 tree array_value)
14420{
14421 c_token *token = NULL;
14422 tree start_index = NULL_TREE, end_index = NULL_TREE, stride = NULL_TREE;
14423 tree value_tree = NULL_TREE, type = NULL_TREE, array_type = NULL_TREE;
14424 tree array_type_domain = NULL_TREE;
14425
5e88a8f4 14426 if (array_value == error_mark_node || initial_index == error_mark_node)
36536d79
BI
14427 {
14428 /* No need to continue. If either of these 2 were true, then an error
14429 must be emitted already. Thus, no need to emit them twice. */
14430 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
14431 return error_mark_node;
14432 }
14433
14434 array_type = TREE_TYPE (array_value);
14435 gcc_assert (array_type);
671a475e
IZ
14436 if (TREE_CODE (array_type) != ARRAY_TYPE
14437 && TREE_CODE (array_type) != POINTER_TYPE)
14438 {
14439 error_at (loc, "base of array section must be pointer or array type");
14440 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
14441 return error_mark_node;
14442 }
36536d79
BI
14443 type = TREE_TYPE (array_type);
14444 token = c_parser_peek_token (parser);
14445
14446 if (token->type == CPP_EOF)
14447 {
14448 c_parser_error (parser, "expected %<:%> or numeral");
14449 return value_tree;
14450 }
14451 else if (token->type == CPP_COLON)
14452 {
14453 if (!initial_index)
14454 {
14455 /* If we are here, then we have a case like this A[:]. */
14456 c_parser_consume_token (parser);
14457 if (TREE_CODE (array_type) == POINTER_TYPE)
14458 {
14459 error_at (loc, "start-index and length fields necessary for "
14460 "using array notations in pointers");
14461 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
14462 return error_mark_node;
14463 }
14464 if (TREE_CODE (array_type) == FUNCTION_TYPE)
14465 {
14466 error_at (loc, "array notations cannot be used with function "
14467 "type");
14468 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
14469 return error_mark_node;
14470 }
36536d79
BI
14471 array_type_domain = TYPE_DOMAIN (array_type);
14472
14473 if (!array_type_domain)
14474 {
14475 error_at (loc, "start-index and length fields necessary for "
14476 "using array notations in dimensionless arrays");
14477 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
14478 return error_mark_node;
14479 }
14480
14481 start_index = TYPE_MINVAL (array_type_domain);
14482 start_index = fold_build1 (CONVERT_EXPR, ptrdiff_type_node,
14483 start_index);
14484 if (!TYPE_MAXVAL (array_type_domain)
14485 || !TREE_CONSTANT (TYPE_MAXVAL (array_type_domain)))
14486 {
14487 error_at (loc, "start-index and length fields necessary for "
14488 "using array notations in variable-length arrays");
14489 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
14490 return error_mark_node;
14491 }
14492 end_index = TYPE_MAXVAL (array_type_domain);
14493 end_index = fold_build2 (PLUS_EXPR, TREE_TYPE (end_index),
14494 end_index, integer_one_node);
14495 end_index = fold_build1 (CONVERT_EXPR, ptrdiff_type_node, end_index);
14496 stride = build_int_cst (integer_type_node, 1);
14497 stride = fold_build1 (CONVERT_EXPR, ptrdiff_type_node, stride);
14498 }
14499 else if (initial_index != error_mark_node)
14500 {
14501 /* If we are here, then there should be 2 possibilities:
14502 1. Array [EXPR : EXPR]
14503 2. Array [EXPR : EXPR : EXPR]
14504 */
14505 start_index = initial_index;
14506
14507 if (TREE_CODE (array_type) == FUNCTION_TYPE)
14508 {
14509 error_at (loc, "array notations cannot be used with function "
14510 "type");
14511 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
14512 return error_mark_node;
14513 }
36536d79 14514 c_parser_consume_token (parser); /* consume the ':' */
267bac10
JM
14515 struct c_expr ce = c_parser_expression (parser);
14516 ce = convert_lvalue_to_rvalue (loc, ce, false, false);
14517 end_index = ce.value;
36536d79
BI
14518 if (!end_index || end_index == error_mark_node)
14519 {
14520 c_parser_skip_to_end_of_block_or_statement (parser);
14521 return error_mark_node;
14522 }
14523 if (c_parser_peek_token (parser)->type == CPP_COLON)
14524 {
14525 c_parser_consume_token (parser);
267bac10
JM
14526 ce = c_parser_expression (parser);
14527 ce = convert_lvalue_to_rvalue (loc, ce, false, false);
14528 stride = ce.value;
36536d79
BI
14529 if (!stride || stride == error_mark_node)
14530 {
14531 c_parser_skip_to_end_of_block_or_statement (parser);
14532 return error_mark_node;
14533 }
14534 }
14535 }
14536 else
14537 c_parser_error (parser, "expected array notation expression");
14538 }
14539 else
14540 c_parser_error (parser, "expected array notation expression");
14541
14542 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, "expected %<]%>");
14543
14544 value_tree = build_array_notation_ref (loc, array_value, start_index,
14545 end_index, stride, type);
14546 if (value_tree != error_mark_node)
14547 SET_EXPR_LOCATION (value_tree, loc);
14548 return value_tree;
14549}
14550
d4a10d0a 14551#include "gt-c-c-parser.h"