re PR c/68090 (VLA compound literal -- "confused by earlier errors, bailing out")
[gcc.git] / gcc / c / c-parser.c
1 /* Parser for C and Objective-C.
2 Copyright (C) 1987-2015 Free Software Foundation, Inc.
3
4 Parser actions based on the old Bison parser; structure somewhat
5 influenced by and fragments based on the C++ parser.
6
7 This file is part of GCC.
8
9 GCC is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 3, or (at your option) any later
12 version.
13
14 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3. If not see
21 <http://www.gnu.org/licenses/>. */
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"
41 #include "target.h"
42 #include "function.h"
43 #include "tree.h"
44 #include "c-family/c-common.h"
45 #include "c-tree.h"
46 #include "timevar.h"
47 #include "stringpool.h"
48 #include "cgraph.h"
49 #include "flags.h"
50 #include "attribs.h"
51 #include "stor-layout.h"
52 #include "varasm.h"
53 #include "trans-mem.h"
54 #include "langhooks.h"
55 #include "c-family/c-pragma.h"
56 #include "c-lang.h"
57 #include "c-family/c-objc.h"
58 #include "plugin.h"
59 #include "omp-low.h"
60 #include "builtins.h"
61 #include "gomp-constants.h"
62 #include "c-family/c-indentation.h"
63 #include "gimple-expr.h"
64 #include "context.h"
65
66 \f
67 /* Initialization routine for this file. */
68
69 void
70 c_parse_init (void)
71 {
72 /* The only initialization required is of the reserved word
73 identifiers. */
74 unsigned int i;
75 tree id;
76 int mask = 0;
77
78 /* Make sure RID_MAX hasn't grown past the 8 bits used to hold the keyword in
79 the c_token structure. */
80 gcc_assert (RID_MAX <= 255);
81
82 mask |= D_CXXONLY;
83 if (!flag_isoc99)
84 mask |= D_C99;
85 if (flag_no_asm)
86 {
87 mask |= D_ASM | D_EXT;
88 if (!flag_isoc99)
89 mask |= D_EXT89;
90 }
91 if (!c_dialect_objc ())
92 mask |= D_OBJC | D_CXX_OBJC;
93
94 ridpointers = ggc_cleared_vec_alloc<tree> ((int) RID_MAX);
95 for (i = 0; i < num_c_common_reswords; i++)
96 {
97 /* If a keyword is disabled, do not enter it into the table
98 and so create a canonical spelling that isn't a keyword. */
99 if (c_common_reswords[i].disable & mask)
100 {
101 if (warn_cxx_compat
102 && (c_common_reswords[i].disable & D_CXXWARN))
103 {
104 id = get_identifier (c_common_reswords[i].word);
105 C_SET_RID_CODE (id, RID_CXX_COMPAT_WARN);
106 C_IS_RESERVED_WORD (id) = 1;
107 }
108 continue;
109 }
110
111 id = get_identifier (c_common_reswords[i].word);
112 C_SET_RID_CODE (id, c_common_reswords[i].rid);
113 C_IS_RESERVED_WORD (id) = 1;
114 ridpointers [(int) c_common_reswords[i].rid] = id;
115 }
116
117 for (i = 0; i < NUM_INT_N_ENTS; i++)
118 {
119 /* We always create the symbols but they aren't always supported. */
120 char name[50];
121 sprintf (name, "__int%d", int_n_data[i].bitsize);
122 id = get_identifier (name);
123 C_SET_RID_CODE (id, RID_FIRST_INT_N + i);
124 C_IS_RESERVED_WORD (id) = 1;
125 }
126 }
127 \f
128 /* The C lexer intermediates between the lexer in cpplib and c-lex.c
129 and the C parser. Unlike the C++ lexer, the parser structure
130 stores the lexer information instead of using a separate structure.
131 Identifiers are separated into ordinary identifiers, type names,
132 keywords and some other Objective-C types of identifiers, and some
133 look-ahead is maintained.
134
135 ??? It might be a good idea to lex the whole file up front (as for
136 C++). It would then be possible to share more of the C and C++
137 lexer code, if desired. */
138
139 /* More information about the type of a CPP_NAME token. */
140 enum c_id_kind {
141 /* An ordinary identifier. */
142 C_ID_ID,
143 /* An identifier declared as a typedef name. */
144 C_ID_TYPENAME,
145 /* An identifier declared as an Objective-C class name. */
146 C_ID_CLASSNAME,
147 /* An address space identifier. */
148 C_ID_ADDRSPACE,
149 /* Not an identifier. */
150 C_ID_NONE
151 };
152
153 /* A single C token after string literal concatenation and conversion
154 of preprocessing tokens to tokens. */
155 struct GTY (()) c_token {
156 /* The kind of token. */
157 ENUM_BITFIELD (cpp_ttype) type : 8;
158 /* If this token is a CPP_NAME, this value indicates whether also
159 declared as some kind of type. Otherwise, it is C_ID_NONE. */
160 ENUM_BITFIELD (c_id_kind) id_kind : 8;
161 /* If this token is a keyword, this value indicates which keyword.
162 Otherwise, this value is RID_MAX. */
163 ENUM_BITFIELD (rid) keyword : 8;
164 /* If this token is a CPP_PRAGMA, this indicates the pragma that
165 was seen. Otherwise it is PRAGMA_NONE. */
166 ENUM_BITFIELD (pragma_kind) pragma_kind : 8;
167 /* The location at which this token was found. */
168 location_t location;
169 /* The value associated with this token, if any. */
170 tree value;
171 };
172
173 /* A parser structure recording information about the state and
174 context of parsing. Includes lexer information with up to two
175 tokens of look-ahead; more are not needed for C. */
176 struct GTY(()) c_parser {
177 /* The look-ahead tokens. */
178 c_token * GTY((skip)) tokens;
179 /* Buffer for look-ahead tokens. */
180 c_token tokens_buf[2];
181 /* How many look-ahead tokens are available (0, 1 or 2, or
182 more if parsing from pre-lexed tokens). */
183 unsigned int tokens_avail;
184 /* True if a syntax error is being recovered from; false otherwise.
185 c_parser_error sets this flag. It should clear this flag when
186 enough tokens have been consumed to recover from the error. */
187 BOOL_BITFIELD error : 1;
188 /* True if we're processing a pragma, and shouldn't automatically
189 consume CPP_PRAGMA_EOL. */
190 BOOL_BITFIELD in_pragma : 1;
191 /* True if we're parsing the outermost block of an if statement. */
192 BOOL_BITFIELD in_if_block : 1;
193 /* True if we want to lex an untranslated string. */
194 BOOL_BITFIELD lex_untranslated_string : 1;
195
196 /* Objective-C specific parser/lexer information. */
197
198 /* True if we are in a context where the Objective-C "PQ" keywords
199 are considered keywords. */
200 BOOL_BITFIELD objc_pq_context : 1;
201 /* True if we are parsing a (potential) Objective-C foreach
202 statement. This is set to true after we parsed 'for (' and while
203 we wait for 'in' or ';' to decide if it's a standard C for loop or an
204 Objective-C foreach loop. */
205 BOOL_BITFIELD objc_could_be_foreach_context : 1;
206 /* The following flag is needed to contextualize Objective-C lexical
207 analysis. In some cases (e.g., 'int NSObject;'), it is
208 undesirable to bind an identifier to an Objective-C class, even
209 if a class with that name exists. */
210 BOOL_BITFIELD objc_need_raw_identifier : 1;
211 /* Nonzero if we're processing a __transaction statement. The value
212 is 1 | TM_STMT_ATTR_*. */
213 unsigned int in_transaction : 4;
214 /* True if we are in a context where the Objective-C "Property attribute"
215 keywords are valid. */
216 BOOL_BITFIELD objc_property_attr_context : 1;
217
218 /* Cilk Plus specific parser/lexer information. */
219
220 /* Buffer to hold all the tokens from parsing the vector attribute for the
221 SIMD-enabled functions (formerly known as elemental functions). */
222 vec <c_token, va_gc> *cilk_simd_fn_tokens;
223 };
224
225
226 /* The actual parser and external interface. ??? Does this need to be
227 garbage-collected? */
228
229 static GTY (()) c_parser *the_parser;
230
231 /* Read in and lex a single token, storing it in *TOKEN. */
232
233 static void
234 c_lex_one_token (c_parser *parser, c_token *token)
235 {
236 timevar_push (TV_LEX);
237
238 token->type = c_lex_with_flags (&token->value, &token->location, NULL,
239 (parser->lex_untranslated_string
240 ? C_LEX_STRING_NO_TRANSLATE : 0));
241 token->id_kind = C_ID_NONE;
242 token->keyword = RID_MAX;
243 token->pragma_kind = PRAGMA_NONE;
244
245 switch (token->type)
246 {
247 case CPP_NAME:
248 {
249 tree decl;
250
251 bool objc_force_identifier = parser->objc_need_raw_identifier;
252 if (c_dialect_objc ())
253 parser->objc_need_raw_identifier = false;
254
255 if (C_IS_RESERVED_WORD (token->value))
256 {
257 enum rid rid_code = C_RID_CODE (token->value);
258
259 if (rid_code == RID_CXX_COMPAT_WARN)
260 {
261 warning_at (token->location,
262 OPT_Wc___compat,
263 "identifier %qE conflicts with C++ keyword",
264 token->value);
265 }
266 else if (rid_code >= RID_FIRST_ADDR_SPACE
267 && rid_code <= RID_LAST_ADDR_SPACE)
268 {
269 token->id_kind = C_ID_ADDRSPACE;
270 token->keyword = rid_code;
271 break;
272 }
273 else if (c_dialect_objc () && OBJC_IS_PQ_KEYWORD (rid_code))
274 {
275 /* We found an Objective-C "pq" keyword (in, out,
276 inout, bycopy, byref, oneway). They need special
277 care because the interpretation depends on the
278 context. */
279 if (parser->objc_pq_context)
280 {
281 token->type = CPP_KEYWORD;
282 token->keyword = rid_code;
283 break;
284 }
285 else if (parser->objc_could_be_foreach_context
286 && rid_code == RID_IN)
287 {
288 /* We are in Objective-C, inside a (potential)
289 foreach context (which means after having
290 parsed 'for (', but before having parsed ';'),
291 and we found 'in'. We consider it the keyword
292 which terminates the declaration at the
293 beginning of a foreach-statement. Note that
294 this means you can't use 'in' for anything else
295 in that context; in particular, in Objective-C
296 you can't use 'in' as the name of the running
297 variable in a C for loop. We could potentially
298 try to add code here to disambiguate, but it
299 seems a reasonable limitation. */
300 token->type = CPP_KEYWORD;
301 token->keyword = rid_code;
302 break;
303 }
304 /* Else, "pq" keywords outside of the "pq" context are
305 not keywords, and we fall through to the code for
306 normal tokens. */
307 }
308 else if (c_dialect_objc () && OBJC_IS_PATTR_KEYWORD (rid_code))
309 {
310 /* We found an Objective-C "property attribute"
311 keyword (getter, setter, readonly, etc). These are
312 only valid in the property context. */
313 if (parser->objc_property_attr_context)
314 {
315 token->type = CPP_KEYWORD;
316 token->keyword = rid_code;
317 break;
318 }
319 /* Else they are not special keywords.
320 */
321 }
322 else if (c_dialect_objc ()
323 && (OBJC_IS_AT_KEYWORD (rid_code)
324 || OBJC_IS_CXX_KEYWORD (rid_code)))
325 {
326 /* We found one of the Objective-C "@" keywords (defs,
327 selector, synchronized, etc) or one of the
328 Objective-C "cxx" keywords (class, private,
329 protected, public, try, catch, throw) without a
330 preceding '@' sign. Do nothing and fall through to
331 the code for normal tokens (in C++ we would still
332 consider the CXX ones keywords, but not in C). */
333 ;
334 }
335 else
336 {
337 token->type = CPP_KEYWORD;
338 token->keyword = rid_code;
339 break;
340 }
341 }
342
343 decl = lookup_name (token->value);
344 if (decl)
345 {
346 if (TREE_CODE (decl) == TYPE_DECL)
347 {
348 token->id_kind = C_ID_TYPENAME;
349 break;
350 }
351 }
352 else if (c_dialect_objc ())
353 {
354 tree objc_interface_decl = objc_is_class_name (token->value);
355 /* Objective-C class names are in the same namespace as
356 variables and typedefs, and hence are shadowed by local
357 declarations. */
358 if (objc_interface_decl
359 && (!objc_force_identifier || global_bindings_p ()))
360 {
361 token->value = objc_interface_decl;
362 token->id_kind = C_ID_CLASSNAME;
363 break;
364 }
365 }
366 token->id_kind = C_ID_ID;
367 }
368 break;
369 case CPP_AT_NAME:
370 /* This only happens in Objective-C; it must be a keyword. */
371 token->type = CPP_KEYWORD;
372 switch (C_RID_CODE (token->value))
373 {
374 /* Replace 'class' with '@class', 'private' with '@private',
375 etc. This prevents confusion with the C++ keyword
376 'class', and makes the tokens consistent with other
377 Objective-C 'AT' keywords. For example '@class' is
378 reported as RID_AT_CLASS which is consistent with
379 '@synchronized', which is reported as
380 RID_AT_SYNCHRONIZED.
381 */
382 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
383 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
384 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
385 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
386 case RID_THROW: token->keyword = RID_AT_THROW; break;
387 case RID_TRY: token->keyword = RID_AT_TRY; break;
388 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
389 case RID_SYNCHRONIZED: token->keyword = RID_AT_SYNCHRONIZED; break;
390 default: token->keyword = C_RID_CODE (token->value);
391 }
392 break;
393 case CPP_COLON:
394 case CPP_COMMA:
395 case CPP_CLOSE_PAREN:
396 case CPP_SEMICOLON:
397 /* These tokens may affect the interpretation of any identifiers
398 following, if doing Objective-C. */
399 if (c_dialect_objc ())
400 parser->objc_need_raw_identifier = false;
401 break;
402 case CPP_PRAGMA:
403 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
404 token->pragma_kind = (enum pragma_kind) TREE_INT_CST_LOW (token->value);
405 token->value = NULL;
406 break;
407 default:
408 break;
409 }
410 timevar_pop (TV_LEX);
411 }
412
413 /* Return a pointer to the next token from PARSER, reading it in if
414 necessary. */
415
416 static inline c_token *
417 c_parser_peek_token (c_parser *parser)
418 {
419 if (parser->tokens_avail == 0)
420 {
421 c_lex_one_token (parser, &parser->tokens[0]);
422 parser->tokens_avail = 1;
423 }
424 return &parser->tokens[0];
425 }
426
427 /* Return true if the next token from PARSER has the indicated
428 TYPE. */
429
430 static inline bool
431 c_parser_next_token_is (c_parser *parser, enum cpp_ttype type)
432 {
433 return c_parser_peek_token (parser)->type == type;
434 }
435
436 /* Return true if the next token from PARSER does not have the
437 indicated TYPE. */
438
439 static inline bool
440 c_parser_next_token_is_not (c_parser *parser, enum cpp_ttype type)
441 {
442 return !c_parser_next_token_is (parser, type);
443 }
444
445 /* Return true if the next token from PARSER is the indicated
446 KEYWORD. */
447
448 static inline bool
449 c_parser_next_token_is_keyword (c_parser *parser, enum rid keyword)
450 {
451 return c_parser_peek_token (parser)->keyword == keyword;
452 }
453
454 /* Return a pointer to the next-but-one token from PARSER, reading it
455 in if necessary. The next token is already read in. */
456
457 static c_token *
458 c_parser_peek_2nd_token (c_parser *parser)
459 {
460 if (parser->tokens_avail >= 2)
461 return &parser->tokens[1];
462 gcc_assert (parser->tokens_avail == 1);
463 gcc_assert (parser->tokens[0].type != CPP_EOF);
464 gcc_assert (parser->tokens[0].type != CPP_PRAGMA_EOL);
465 c_lex_one_token (parser, &parser->tokens[1]);
466 parser->tokens_avail = 2;
467 return &parser->tokens[1];
468 }
469
470 /* Return true if TOKEN can start a type name,
471 false otherwise. */
472 static bool
473 c_token_starts_typename (c_token *token)
474 {
475 switch (token->type)
476 {
477 case CPP_NAME:
478 switch (token->id_kind)
479 {
480 case C_ID_ID:
481 return false;
482 case C_ID_ADDRSPACE:
483 return true;
484 case C_ID_TYPENAME:
485 return true;
486 case C_ID_CLASSNAME:
487 gcc_assert (c_dialect_objc ());
488 return true;
489 default:
490 gcc_unreachable ();
491 }
492 case CPP_KEYWORD:
493 switch (token->keyword)
494 {
495 case RID_UNSIGNED:
496 case RID_LONG:
497 case RID_SHORT:
498 case RID_SIGNED:
499 case RID_COMPLEX:
500 case RID_INT:
501 case RID_CHAR:
502 case RID_FLOAT:
503 case RID_DOUBLE:
504 case RID_VOID:
505 case RID_DFLOAT32:
506 case RID_DFLOAT64:
507 case RID_DFLOAT128:
508 case RID_BOOL:
509 case RID_ENUM:
510 case RID_STRUCT:
511 case RID_UNION:
512 case RID_TYPEOF:
513 case RID_CONST:
514 case RID_ATOMIC:
515 case RID_VOLATILE:
516 case RID_RESTRICT:
517 case RID_ATTRIBUTE:
518 case RID_FRACT:
519 case RID_ACCUM:
520 case RID_SAT:
521 case RID_AUTO_TYPE:
522 return true;
523 default:
524 if (token->keyword >= RID_FIRST_INT_N
525 && token->keyword < RID_FIRST_INT_N + NUM_INT_N_ENTS
526 && int_n_enabled_p[token->keyword - RID_FIRST_INT_N])
527 return true;
528 return false;
529 }
530 case CPP_LESS:
531 if (c_dialect_objc ())
532 return true;
533 return false;
534 default:
535 return false;
536 }
537 }
538
539 enum c_lookahead_kind {
540 /* Always treat unknown identifiers as typenames. */
541 cla_prefer_type,
542
543 /* Could be parsing a nonabstract declarator. Only treat an identifier
544 as a typename if followed by another identifier or a star. */
545 cla_nonabstract_decl,
546
547 /* Never treat identifiers as typenames. */
548 cla_prefer_id
549 };
550
551 /* Return true if the next token from PARSER can start a type name,
552 false otherwise. LA specifies how to do lookahead in order to
553 detect unknown type names. If unsure, pick CLA_PREFER_ID. */
554
555 static inline bool
556 c_parser_next_tokens_start_typename (c_parser *parser, enum c_lookahead_kind la)
557 {
558 c_token *token = c_parser_peek_token (parser);
559 if (c_token_starts_typename (token))
560 return true;
561
562 /* Try a bit harder to detect an unknown typename. */
563 if (la != cla_prefer_id
564 && token->type == CPP_NAME
565 && token->id_kind == C_ID_ID
566
567 /* Do not try too hard when we could have "object in array". */
568 && !parser->objc_could_be_foreach_context
569
570 && (la == cla_prefer_type
571 || c_parser_peek_2nd_token (parser)->type == CPP_NAME
572 || c_parser_peek_2nd_token (parser)->type == CPP_MULT)
573
574 /* Only unknown identifiers. */
575 && !lookup_name (token->value))
576 return true;
577
578 return false;
579 }
580
581 /* Return true if TOKEN is a type qualifier, false otherwise. */
582 static bool
583 c_token_is_qualifier (c_token *token)
584 {
585 switch (token->type)
586 {
587 case CPP_NAME:
588 switch (token->id_kind)
589 {
590 case C_ID_ADDRSPACE:
591 return true;
592 default:
593 return false;
594 }
595 case CPP_KEYWORD:
596 switch (token->keyword)
597 {
598 case RID_CONST:
599 case RID_VOLATILE:
600 case RID_RESTRICT:
601 case RID_ATTRIBUTE:
602 case RID_ATOMIC:
603 return true;
604 default:
605 return false;
606 }
607 case CPP_LESS:
608 return false;
609 default:
610 gcc_unreachable ();
611 }
612 }
613
614 /* Return true if the next token from PARSER is a type qualifier,
615 false otherwise. */
616 static inline bool
617 c_parser_next_token_is_qualifier (c_parser *parser)
618 {
619 c_token *token = c_parser_peek_token (parser);
620 return c_token_is_qualifier (token);
621 }
622
623 /* Return true if TOKEN can start declaration specifiers, false
624 otherwise. */
625 static bool
626 c_token_starts_declspecs (c_token *token)
627 {
628 switch (token->type)
629 {
630 case CPP_NAME:
631 switch (token->id_kind)
632 {
633 case C_ID_ID:
634 return false;
635 case C_ID_ADDRSPACE:
636 return true;
637 case C_ID_TYPENAME:
638 return true;
639 case C_ID_CLASSNAME:
640 gcc_assert (c_dialect_objc ());
641 return true;
642 default:
643 gcc_unreachable ();
644 }
645 case CPP_KEYWORD:
646 switch (token->keyword)
647 {
648 case RID_STATIC:
649 case RID_EXTERN:
650 case RID_REGISTER:
651 case RID_TYPEDEF:
652 case RID_INLINE:
653 case RID_NORETURN:
654 case RID_AUTO:
655 case RID_THREAD:
656 case RID_UNSIGNED:
657 case RID_LONG:
658 case RID_SHORT:
659 case RID_SIGNED:
660 case RID_COMPLEX:
661 case RID_INT:
662 case RID_CHAR:
663 case RID_FLOAT:
664 case RID_DOUBLE:
665 case RID_VOID:
666 case RID_DFLOAT32:
667 case RID_DFLOAT64:
668 case RID_DFLOAT128:
669 case RID_BOOL:
670 case RID_ENUM:
671 case RID_STRUCT:
672 case RID_UNION:
673 case RID_TYPEOF:
674 case RID_CONST:
675 case RID_VOLATILE:
676 case RID_RESTRICT:
677 case RID_ATTRIBUTE:
678 case RID_FRACT:
679 case RID_ACCUM:
680 case RID_SAT:
681 case RID_ALIGNAS:
682 case RID_ATOMIC:
683 case RID_AUTO_TYPE:
684 return true;
685 default:
686 if (token->keyword >= RID_FIRST_INT_N
687 && token->keyword < RID_FIRST_INT_N + NUM_INT_N_ENTS
688 && int_n_enabled_p[token->keyword - RID_FIRST_INT_N])
689 return true;
690 return false;
691 }
692 case CPP_LESS:
693 if (c_dialect_objc ())
694 return true;
695 return false;
696 default:
697 return false;
698 }
699 }
700
701
702 /* Return true if TOKEN can start declaration specifiers or a static
703 assertion, false otherwise. */
704 static bool
705 c_token_starts_declaration (c_token *token)
706 {
707 if (c_token_starts_declspecs (token)
708 || token->keyword == RID_STATIC_ASSERT)
709 return true;
710 else
711 return false;
712 }
713
714 /* Return true if the next token from PARSER can start declaration
715 specifiers, false otherwise. */
716 static inline bool
717 c_parser_next_token_starts_declspecs (c_parser *parser)
718 {
719 c_token *token = c_parser_peek_token (parser);
720
721 /* In Objective-C, a classname normally starts a declspecs unless it
722 is immediately followed by a dot. In that case, it is the
723 Objective-C 2.0 "dot-syntax" for class objects, ie, calls the
724 setter/getter on the class. c_token_starts_declspecs() can't
725 differentiate between the two cases because it only checks the
726 current token, so we have a special check here. */
727 if (c_dialect_objc ()
728 && token->type == CPP_NAME
729 && token->id_kind == C_ID_CLASSNAME
730 && c_parser_peek_2nd_token (parser)->type == CPP_DOT)
731 return false;
732
733 return c_token_starts_declspecs (token);
734 }
735
736 /* Return true if the next tokens from PARSER can start declaration
737 specifiers or a static assertion, false otherwise. */
738 static inline bool
739 c_parser_next_tokens_start_declaration (c_parser *parser)
740 {
741 c_token *token = c_parser_peek_token (parser);
742
743 /* Same as above. */
744 if (c_dialect_objc ()
745 && token->type == CPP_NAME
746 && token->id_kind == C_ID_CLASSNAME
747 && c_parser_peek_2nd_token (parser)->type == CPP_DOT)
748 return false;
749
750 /* Labels do not start declarations. */
751 if (token->type == CPP_NAME
752 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
753 return false;
754
755 if (c_token_starts_declaration (token))
756 return true;
757
758 if (c_parser_next_tokens_start_typename (parser, cla_nonabstract_decl))
759 return true;
760
761 return false;
762 }
763
764 /* Consume the next token from PARSER. */
765
766 static void
767 c_parser_consume_token (c_parser *parser)
768 {
769 gcc_assert (parser->tokens_avail >= 1);
770 gcc_assert (parser->tokens[0].type != CPP_EOF);
771 gcc_assert (!parser->in_pragma || parser->tokens[0].type != CPP_PRAGMA_EOL);
772 gcc_assert (parser->error || parser->tokens[0].type != CPP_PRAGMA);
773 if (parser->tokens != &parser->tokens_buf[0])
774 parser->tokens++;
775 else if (parser->tokens_avail == 2)
776 parser->tokens[0] = parser->tokens[1];
777 parser->tokens_avail--;
778 }
779
780 /* Expect the current token to be a #pragma. Consume it and remember
781 that we've begun parsing a pragma. */
782
783 static void
784 c_parser_consume_pragma (c_parser *parser)
785 {
786 gcc_assert (!parser->in_pragma);
787 gcc_assert (parser->tokens_avail >= 1);
788 gcc_assert (parser->tokens[0].type == CPP_PRAGMA);
789 if (parser->tokens != &parser->tokens_buf[0])
790 parser->tokens++;
791 else if (parser->tokens_avail == 2)
792 parser->tokens[0] = parser->tokens[1];
793 parser->tokens_avail--;
794 parser->in_pragma = true;
795 }
796
797 /* Update the global input_location from TOKEN. */
798 static inline void
799 c_parser_set_source_position_from_token (c_token *token)
800 {
801 if (token->type != CPP_EOF)
802 {
803 input_location = token->location;
804 }
805 }
806
807 /* Issue a diagnostic of the form
808 FILE:LINE: MESSAGE before TOKEN
809 where TOKEN is the next token in the input stream of PARSER.
810 MESSAGE (specified by the caller) is usually of the form "expected
811 OTHER-TOKEN".
812
813 Do not issue a diagnostic if still recovering from an error.
814
815 ??? This is taken from the C++ parser, but building up messages in
816 this way is not i18n-friendly and some other approach should be
817 used. */
818
819 static void
820 c_parser_error (c_parser *parser, const char *gmsgid)
821 {
822 c_token *token = c_parser_peek_token (parser);
823 if (parser->error)
824 return;
825 parser->error = true;
826 if (!gmsgid)
827 return;
828 /* This diagnostic makes more sense if it is tagged to the line of
829 the token we just peeked at. */
830 c_parser_set_source_position_from_token (token);
831 c_parse_error (gmsgid,
832 /* Because c_parse_error does not understand
833 CPP_KEYWORD, keywords are treated like
834 identifiers. */
835 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
836 /* ??? The C parser does not save the cpp flags of a
837 token, we need to pass 0 here and we will not get
838 the source spelling of some tokens but rather the
839 canonical spelling. */
840 token->value, /*flags=*/0);
841 }
842
843 /* If the next token is of the indicated TYPE, consume it. Otherwise,
844 issue the error MSGID. If MSGID is NULL then a message has already
845 been produced and no message will be produced this time. Returns
846 true if found, false otherwise. */
847
848 static bool
849 c_parser_require (c_parser *parser,
850 enum cpp_ttype type,
851 const char *msgid)
852 {
853 if (c_parser_next_token_is (parser, type))
854 {
855 c_parser_consume_token (parser);
856 return true;
857 }
858 else
859 {
860 c_parser_error (parser, msgid);
861 return false;
862 }
863 }
864
865 /* If the next token is the indicated keyword, consume it. Otherwise,
866 issue the error MSGID. Returns true if found, false otherwise. */
867
868 static bool
869 c_parser_require_keyword (c_parser *parser,
870 enum rid keyword,
871 const char *msgid)
872 {
873 if (c_parser_next_token_is_keyword (parser, keyword))
874 {
875 c_parser_consume_token (parser);
876 return true;
877 }
878 else
879 {
880 c_parser_error (parser, msgid);
881 return false;
882 }
883 }
884
885 /* Like c_parser_require, except that tokens will be skipped until the
886 desired token is found. An error message is still produced if the
887 next token is not as expected. If MSGID is NULL then a message has
888 already been produced and no message will be produced this
889 time. */
890
891 static void
892 c_parser_skip_until_found (c_parser *parser,
893 enum cpp_ttype type,
894 const char *msgid)
895 {
896 unsigned nesting_depth = 0;
897
898 if (c_parser_require (parser, type, msgid))
899 return;
900
901 /* Skip tokens until the desired token is found. */
902 while (true)
903 {
904 /* Peek at the next token. */
905 c_token *token = c_parser_peek_token (parser);
906 /* If we've reached the token we want, consume it and stop. */
907 if (token->type == type && !nesting_depth)
908 {
909 c_parser_consume_token (parser);
910 break;
911 }
912
913 /* If we've run out of tokens, stop. */
914 if (token->type == CPP_EOF)
915 return;
916 if (token->type == CPP_PRAGMA_EOL && parser->in_pragma)
917 return;
918 if (token->type == CPP_OPEN_BRACE
919 || token->type == CPP_OPEN_PAREN
920 || token->type == CPP_OPEN_SQUARE)
921 ++nesting_depth;
922 else if (token->type == CPP_CLOSE_BRACE
923 || token->type == CPP_CLOSE_PAREN
924 || token->type == CPP_CLOSE_SQUARE)
925 {
926 if (nesting_depth-- == 0)
927 break;
928 }
929 /* Consume this token. */
930 c_parser_consume_token (parser);
931 }
932 parser->error = false;
933 }
934
935 /* Skip tokens until the end of a parameter is found, but do not
936 consume the comma, semicolon or closing delimiter. */
937
938 static void
939 c_parser_skip_to_end_of_parameter (c_parser *parser)
940 {
941 unsigned nesting_depth = 0;
942
943 while (true)
944 {
945 c_token *token = c_parser_peek_token (parser);
946 if ((token->type == CPP_COMMA || token->type == CPP_SEMICOLON)
947 && !nesting_depth)
948 break;
949 /* If we've run out of tokens, stop. */
950 if (token->type == CPP_EOF)
951 return;
952 if (token->type == CPP_PRAGMA_EOL && parser->in_pragma)
953 return;
954 if (token->type == CPP_OPEN_BRACE
955 || token->type == CPP_OPEN_PAREN
956 || token->type == CPP_OPEN_SQUARE)
957 ++nesting_depth;
958 else if (token->type == CPP_CLOSE_BRACE
959 || token->type == CPP_CLOSE_PAREN
960 || token->type == CPP_CLOSE_SQUARE)
961 {
962 if (nesting_depth-- == 0)
963 break;
964 }
965 /* Consume this token. */
966 c_parser_consume_token (parser);
967 }
968 parser->error = false;
969 }
970
971 /* Expect to be at the end of the pragma directive and consume an
972 end of line marker. */
973
974 static void
975 c_parser_skip_to_pragma_eol (c_parser *parser, bool error_if_not_eol = true)
976 {
977 gcc_assert (parser->in_pragma);
978 parser->in_pragma = false;
979
980 if (error_if_not_eol && c_parser_peek_token (parser)->type != CPP_PRAGMA_EOL)
981 c_parser_error (parser, "expected end of line");
982
983 cpp_ttype token_type;
984 do
985 {
986 c_token *token = c_parser_peek_token (parser);
987 token_type = token->type;
988 if (token_type == CPP_EOF)
989 break;
990 c_parser_consume_token (parser);
991 }
992 while (token_type != CPP_PRAGMA_EOL);
993
994 parser->error = false;
995 }
996
997 /* Skip tokens until we have consumed an entire block, or until we
998 have consumed a non-nested ';'. */
999
1000 static void
1001 c_parser_skip_to_end_of_block_or_statement (c_parser *parser)
1002 {
1003 unsigned nesting_depth = 0;
1004 bool save_error = parser->error;
1005
1006 while (true)
1007 {
1008 c_token *token;
1009
1010 /* Peek at the next token. */
1011 token = c_parser_peek_token (parser);
1012
1013 switch (token->type)
1014 {
1015 case CPP_EOF:
1016 return;
1017
1018 case CPP_PRAGMA_EOL:
1019 if (parser->in_pragma)
1020 return;
1021 break;
1022
1023 case CPP_SEMICOLON:
1024 /* If the next token is a ';', we have reached the
1025 end of the statement. */
1026 if (!nesting_depth)
1027 {
1028 /* Consume the ';'. */
1029 c_parser_consume_token (parser);
1030 goto finished;
1031 }
1032 break;
1033
1034 case CPP_CLOSE_BRACE:
1035 /* If the next token is a non-nested '}', then we have
1036 reached the end of the current block. */
1037 if (nesting_depth == 0 || --nesting_depth == 0)
1038 {
1039 c_parser_consume_token (parser);
1040 goto finished;
1041 }
1042 break;
1043
1044 case CPP_OPEN_BRACE:
1045 /* If it the next token is a '{', then we are entering a new
1046 block. Consume the entire block. */
1047 ++nesting_depth;
1048 break;
1049
1050 case CPP_PRAGMA:
1051 /* If we see a pragma, consume the whole thing at once. We
1052 have some safeguards against consuming pragmas willy-nilly.
1053 Normally, we'd expect to be here with parser->error set,
1054 which disables these safeguards. But it's possible to get
1055 here for secondary error recovery, after parser->error has
1056 been cleared. */
1057 c_parser_consume_pragma (parser);
1058 c_parser_skip_to_pragma_eol (parser);
1059 parser->error = save_error;
1060 continue;
1061
1062 default:
1063 break;
1064 }
1065
1066 c_parser_consume_token (parser);
1067 }
1068
1069 finished:
1070 parser->error = false;
1071 }
1072
1073 /* CPP's options (initialized by c-opts.c). */
1074 extern cpp_options *cpp_opts;
1075
1076 /* Save the warning flags which are controlled by __extension__. */
1077
1078 static inline int
1079 disable_extension_diagnostics (void)
1080 {
1081 int ret = (pedantic
1082 | (warn_pointer_arith << 1)
1083 | (warn_traditional << 2)
1084 | (flag_iso << 3)
1085 | (warn_long_long << 4)
1086 | (warn_cxx_compat << 5)
1087 | (warn_overlength_strings << 6)
1088 /* warn_c90_c99_compat has three states: -1/0/1, so we must
1089 play tricks to properly restore it. */
1090 | ((warn_c90_c99_compat == 1) << 7)
1091 | ((warn_c90_c99_compat == -1) << 8)
1092 /* Similarly for warn_c99_c11_compat. */
1093 | ((warn_c99_c11_compat == 1) << 9)
1094 | ((warn_c99_c11_compat == -1) << 10)
1095 );
1096 cpp_opts->cpp_pedantic = pedantic = 0;
1097 warn_pointer_arith = 0;
1098 cpp_opts->cpp_warn_traditional = warn_traditional = 0;
1099 flag_iso = 0;
1100 cpp_opts->cpp_warn_long_long = warn_long_long = 0;
1101 warn_cxx_compat = 0;
1102 warn_overlength_strings = 0;
1103 warn_c90_c99_compat = 0;
1104 warn_c99_c11_compat = 0;
1105 return ret;
1106 }
1107
1108 /* Restore the warning flags which are controlled by __extension__.
1109 FLAGS is the return value from disable_extension_diagnostics. */
1110
1111 static inline void
1112 restore_extension_diagnostics (int flags)
1113 {
1114 cpp_opts->cpp_pedantic = pedantic = flags & 1;
1115 warn_pointer_arith = (flags >> 1) & 1;
1116 cpp_opts->cpp_warn_traditional = warn_traditional = (flags >> 2) & 1;
1117 flag_iso = (flags >> 3) & 1;
1118 cpp_opts->cpp_warn_long_long = warn_long_long = (flags >> 4) & 1;
1119 warn_cxx_compat = (flags >> 5) & 1;
1120 warn_overlength_strings = (flags >> 6) & 1;
1121 /* See above for why is this needed. */
1122 warn_c90_c99_compat = (flags >> 7) & 1 ? 1 : ((flags >> 8) & 1 ? -1 : 0);
1123 warn_c99_c11_compat = (flags >> 9) & 1 ? 1 : ((flags >> 10) & 1 ? -1 : 0);
1124 }
1125
1126 /* Possibly kinds of declarator to parse. */
1127 enum c_dtr_syn {
1128 /* A normal declarator with an identifier. */
1129 C_DTR_NORMAL,
1130 /* An abstract declarator (maybe empty). */
1131 C_DTR_ABSTRACT,
1132 /* A parameter declarator: may be either, but after a type name does
1133 not redeclare a typedef name as an identifier if it can
1134 alternatively be interpreted as a typedef name; see DR#009,
1135 applied in C90 TC1, omitted from C99 and reapplied in C99 TC2
1136 following DR#249. For example, given a typedef T, "int T" and
1137 "int *T" are valid parameter declarations redeclaring T, while
1138 "int (T)" and "int * (T)" and "int (T[])" and "int (T (int))" are
1139 abstract declarators rather than involving redundant parentheses;
1140 the same applies with attributes inside the parentheses before
1141 "T". */
1142 C_DTR_PARM
1143 };
1144
1145 /* The binary operation precedence levels, where 0 is a dummy lowest level
1146 used for the bottom of the stack. */
1147 enum c_parser_prec {
1148 PREC_NONE,
1149 PREC_LOGOR,
1150 PREC_LOGAND,
1151 PREC_BITOR,
1152 PREC_BITXOR,
1153 PREC_BITAND,
1154 PREC_EQ,
1155 PREC_REL,
1156 PREC_SHIFT,
1157 PREC_ADD,
1158 PREC_MULT,
1159 NUM_PRECS
1160 };
1161
1162 static void c_parser_external_declaration (c_parser *);
1163 static void c_parser_asm_definition (c_parser *);
1164 static void c_parser_declaration_or_fndef (c_parser *, bool, bool, bool,
1165 bool, bool, tree *, vec<c_token>);
1166 static void c_parser_static_assert_declaration_no_semi (c_parser *);
1167 static void c_parser_static_assert_declaration (c_parser *);
1168 static void c_parser_declspecs (c_parser *, struct c_declspecs *, bool, bool,
1169 bool, bool, bool, enum c_lookahead_kind);
1170 static struct c_typespec c_parser_enum_specifier (c_parser *);
1171 static struct c_typespec c_parser_struct_or_union_specifier (c_parser *);
1172 static tree c_parser_struct_declaration (c_parser *);
1173 static struct c_typespec c_parser_typeof_specifier (c_parser *);
1174 static tree c_parser_alignas_specifier (c_parser *);
1175 static struct c_declarator *c_parser_declarator (c_parser *, bool, c_dtr_syn,
1176 bool *);
1177 static struct c_declarator *c_parser_direct_declarator (c_parser *, bool,
1178 c_dtr_syn, bool *);
1179 static struct c_declarator *c_parser_direct_declarator_inner (c_parser *,
1180 bool,
1181 struct c_declarator *);
1182 static struct c_arg_info *c_parser_parms_declarator (c_parser *, bool, tree);
1183 static struct c_arg_info *c_parser_parms_list_declarator (c_parser *, tree,
1184 tree);
1185 static struct c_parm *c_parser_parameter_declaration (c_parser *, tree);
1186 static tree c_parser_simple_asm_expr (c_parser *);
1187 static tree c_parser_attributes (c_parser *);
1188 static struct c_type_name *c_parser_type_name (c_parser *);
1189 static struct c_expr c_parser_initializer (c_parser *);
1190 static struct c_expr c_parser_braced_init (c_parser *, tree, bool);
1191 static void c_parser_initelt (c_parser *, struct obstack *);
1192 static void c_parser_initval (c_parser *, struct c_expr *,
1193 struct obstack *);
1194 static tree c_parser_compound_statement (c_parser *);
1195 static void c_parser_compound_statement_nostart (c_parser *);
1196 static void c_parser_label (c_parser *);
1197 static void c_parser_statement (c_parser *);
1198 static void c_parser_statement_after_labels (c_parser *, vec<tree> * = NULL);
1199 static void c_parser_if_statement (c_parser *, vec<tree> *);
1200 static void c_parser_switch_statement (c_parser *);
1201 static void c_parser_while_statement (c_parser *, bool);
1202 static void c_parser_do_statement (c_parser *, bool);
1203 static void c_parser_for_statement (c_parser *, bool);
1204 static tree c_parser_asm_statement (c_parser *);
1205 static tree c_parser_asm_operands (c_parser *);
1206 static tree c_parser_asm_goto_operands (c_parser *);
1207 static tree c_parser_asm_clobbers (c_parser *);
1208 static struct c_expr c_parser_expr_no_commas (c_parser *, struct c_expr *,
1209 tree = NULL_TREE);
1210 static struct c_expr c_parser_conditional_expression (c_parser *,
1211 struct c_expr *, tree);
1212 static struct c_expr c_parser_binary_expression (c_parser *, struct c_expr *,
1213 tree);
1214 static struct c_expr c_parser_cast_expression (c_parser *, struct c_expr *);
1215 static struct c_expr c_parser_unary_expression (c_parser *);
1216 static struct c_expr c_parser_sizeof_expression (c_parser *);
1217 static struct c_expr c_parser_alignof_expression (c_parser *);
1218 static struct c_expr c_parser_postfix_expression (c_parser *);
1219 static struct c_expr c_parser_postfix_expression_after_paren_type (c_parser *,
1220 struct c_type_name *,
1221 location_t);
1222 static struct c_expr c_parser_postfix_expression_after_primary (c_parser *,
1223 location_t loc,
1224 struct c_expr);
1225 static tree c_parser_transaction (c_parser *, enum rid);
1226 static struct c_expr c_parser_transaction_expression (c_parser *, enum rid);
1227 static tree c_parser_transaction_cancel (c_parser *);
1228 static struct c_expr c_parser_expression (c_parser *);
1229 static struct c_expr c_parser_expression_conv (c_parser *);
1230 static vec<tree, va_gc> *c_parser_expr_list (c_parser *, bool, bool,
1231 vec<tree, va_gc> **, location_t *,
1232 tree *, vec<location_t> *,
1233 unsigned int * = NULL);
1234 static void c_parser_oacc_enter_exit_data (c_parser *, bool);
1235 static void c_parser_oacc_update (c_parser *);
1236 static void c_parser_omp_construct (c_parser *);
1237 static void c_parser_omp_threadprivate (c_parser *);
1238 static void c_parser_omp_barrier (c_parser *);
1239 static void c_parser_omp_flush (c_parser *);
1240 static tree c_parser_omp_for_loop (location_t, c_parser *, enum tree_code,
1241 tree, tree *);
1242 static void c_parser_omp_taskwait (c_parser *);
1243 static void c_parser_omp_taskyield (c_parser *);
1244 static void c_parser_omp_cancel (c_parser *);
1245 static void c_parser_omp_cancellation_point (c_parser *);
1246
1247 enum pragma_context { pragma_external, pragma_struct, pragma_param,
1248 pragma_stmt, pragma_compound };
1249 static bool c_parser_pragma (c_parser *, enum pragma_context);
1250 static bool c_parser_omp_target (c_parser *, enum pragma_context);
1251 static void c_parser_omp_end_declare_target (c_parser *);
1252 static void c_parser_omp_declare (c_parser *, enum pragma_context);
1253 static bool c_parser_omp_ordered (c_parser *, enum pragma_context);
1254
1255 /* These Objective-C parser functions are only ever called when
1256 compiling Objective-C. */
1257 static void c_parser_objc_class_definition (c_parser *, tree);
1258 static void c_parser_objc_class_instance_variables (c_parser *);
1259 static void c_parser_objc_class_declaration (c_parser *);
1260 static void c_parser_objc_alias_declaration (c_parser *);
1261 static void c_parser_objc_protocol_definition (c_parser *, tree);
1262 static bool c_parser_objc_method_type (c_parser *);
1263 static void c_parser_objc_method_definition (c_parser *);
1264 static void c_parser_objc_methodprotolist (c_parser *);
1265 static void c_parser_objc_methodproto (c_parser *);
1266 static tree c_parser_objc_method_decl (c_parser *, bool, tree *, tree *);
1267 static tree c_parser_objc_type_name (c_parser *);
1268 static tree c_parser_objc_protocol_refs (c_parser *);
1269 static void c_parser_objc_try_catch_finally_statement (c_parser *);
1270 static void c_parser_objc_synchronized_statement (c_parser *);
1271 static tree c_parser_objc_selector (c_parser *);
1272 static tree c_parser_objc_selector_arg (c_parser *);
1273 static tree c_parser_objc_receiver (c_parser *);
1274 static tree c_parser_objc_message_args (c_parser *);
1275 static tree c_parser_objc_keywordexpr (c_parser *);
1276 static void c_parser_objc_at_property_declaration (c_parser *);
1277 static void c_parser_objc_at_synthesize_declaration (c_parser *);
1278 static void c_parser_objc_at_dynamic_declaration (c_parser *);
1279 static bool c_parser_objc_diagnose_bad_element_prefix
1280 (c_parser *, struct c_declspecs *);
1281
1282 /* Cilk Plus supporting routines. */
1283 static void c_parser_cilk_simd (c_parser *);
1284 static void c_parser_cilk_for (c_parser *, tree);
1285 static bool c_parser_cilk_verify_simd (c_parser *, enum pragma_context);
1286 static tree c_parser_array_notation (location_t, c_parser *, tree, tree);
1287 static tree c_parser_cilk_clause_vectorlength (c_parser *, tree, bool);
1288 static void c_parser_cilk_grainsize (c_parser *);
1289
1290 /* Parse a translation unit (C90 6.7, C99 6.9).
1291
1292 translation-unit:
1293 external-declarations
1294
1295 external-declarations:
1296 external-declaration
1297 external-declarations external-declaration
1298
1299 GNU extensions:
1300
1301 translation-unit:
1302 empty
1303 */
1304
1305 static void
1306 c_parser_translation_unit (c_parser *parser)
1307 {
1308 if (c_parser_next_token_is (parser, CPP_EOF))
1309 {
1310 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
1311 "ISO C forbids an empty translation unit");
1312 }
1313 else
1314 {
1315 void *obstack_position = obstack_alloc (&parser_obstack, 0);
1316 mark_valid_location_for_stdc_pragma (false);
1317 do
1318 {
1319 ggc_collect ();
1320 c_parser_external_declaration (parser);
1321 obstack_free (&parser_obstack, obstack_position);
1322 }
1323 while (c_parser_next_token_is_not (parser, CPP_EOF));
1324 }
1325 }
1326
1327 /* Parse an external declaration (C90 6.7, C99 6.9).
1328
1329 external-declaration:
1330 function-definition
1331 declaration
1332
1333 GNU extensions:
1334
1335 external-declaration:
1336 asm-definition
1337 ;
1338 __extension__ external-declaration
1339
1340 Objective-C:
1341
1342 external-declaration:
1343 objc-class-definition
1344 objc-class-declaration
1345 objc-alias-declaration
1346 objc-protocol-definition
1347 objc-method-definition
1348 @end
1349 */
1350
1351 static void
1352 c_parser_external_declaration (c_parser *parser)
1353 {
1354 int ext;
1355 switch (c_parser_peek_token (parser)->type)
1356 {
1357 case CPP_KEYWORD:
1358 switch (c_parser_peek_token (parser)->keyword)
1359 {
1360 case RID_EXTENSION:
1361 ext = disable_extension_diagnostics ();
1362 c_parser_consume_token (parser);
1363 c_parser_external_declaration (parser);
1364 restore_extension_diagnostics (ext);
1365 break;
1366 case RID_ASM:
1367 c_parser_asm_definition (parser);
1368 break;
1369 case RID_AT_INTERFACE:
1370 case RID_AT_IMPLEMENTATION:
1371 gcc_assert (c_dialect_objc ());
1372 c_parser_objc_class_definition (parser, NULL_TREE);
1373 break;
1374 case RID_AT_CLASS:
1375 gcc_assert (c_dialect_objc ());
1376 c_parser_objc_class_declaration (parser);
1377 break;
1378 case RID_AT_ALIAS:
1379 gcc_assert (c_dialect_objc ());
1380 c_parser_objc_alias_declaration (parser);
1381 break;
1382 case RID_AT_PROTOCOL:
1383 gcc_assert (c_dialect_objc ());
1384 c_parser_objc_protocol_definition (parser, NULL_TREE);
1385 break;
1386 case RID_AT_PROPERTY:
1387 gcc_assert (c_dialect_objc ());
1388 c_parser_objc_at_property_declaration (parser);
1389 break;
1390 case RID_AT_SYNTHESIZE:
1391 gcc_assert (c_dialect_objc ());
1392 c_parser_objc_at_synthesize_declaration (parser);
1393 break;
1394 case RID_AT_DYNAMIC:
1395 gcc_assert (c_dialect_objc ());
1396 c_parser_objc_at_dynamic_declaration (parser);
1397 break;
1398 case RID_AT_END:
1399 gcc_assert (c_dialect_objc ());
1400 c_parser_consume_token (parser);
1401 objc_finish_implementation ();
1402 break;
1403 default:
1404 goto decl_or_fndef;
1405 }
1406 break;
1407 case CPP_SEMICOLON:
1408 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
1409 "ISO C does not allow extra %<;%> outside of a function");
1410 c_parser_consume_token (parser);
1411 break;
1412 case CPP_PRAGMA:
1413 mark_valid_location_for_stdc_pragma (true);
1414 c_parser_pragma (parser, pragma_external);
1415 mark_valid_location_for_stdc_pragma (false);
1416 break;
1417 case CPP_PLUS:
1418 case CPP_MINUS:
1419 if (c_dialect_objc ())
1420 {
1421 c_parser_objc_method_definition (parser);
1422 break;
1423 }
1424 /* Else fall through, and yield a syntax error trying to parse
1425 as a declaration or function definition. */
1426 default:
1427 decl_or_fndef:
1428 /* A declaration or a function definition (or, in Objective-C,
1429 an @interface or @protocol with prefix attributes). We can
1430 only tell which after parsing the declaration specifiers, if
1431 any, and the first declarator. */
1432 c_parser_declaration_or_fndef (parser, true, true, true, false, true,
1433 NULL, vNULL);
1434 break;
1435 }
1436 }
1437
1438 static void c_finish_omp_declare_simd (c_parser *, tree, tree, vec<c_token>);
1439
1440 /* Parse a declaration or function definition (C90 6.5, 6.7.1, C99
1441 6.7, 6.9.1). If FNDEF_OK is true, a function definition is
1442 accepted; otherwise (old-style parameter declarations) only other
1443 declarations are accepted. If STATIC_ASSERT_OK is true, a static
1444 assertion is accepted; otherwise (old-style parameter declarations)
1445 it is not. If NESTED is true, we are inside a function or parsing
1446 old-style parameter declarations; any functions encountered are
1447 nested functions and declaration specifiers are required; otherwise
1448 we are at top level and functions are normal functions and
1449 declaration specifiers may be optional. If EMPTY_OK is true, empty
1450 declarations are OK (subject to all other constraints); otherwise
1451 (old-style parameter declarations) they are diagnosed. If
1452 START_ATTR_OK is true, the declaration specifiers may start with
1453 attributes; otherwise they may not.
1454 OBJC_FOREACH_OBJECT_DECLARATION can be used to get back the parsed
1455 declaration when parsing an Objective-C foreach statement.
1456
1457 declaration:
1458 declaration-specifiers init-declarator-list[opt] ;
1459 static_assert-declaration
1460
1461 function-definition:
1462 declaration-specifiers[opt] declarator declaration-list[opt]
1463 compound-statement
1464
1465 declaration-list:
1466 declaration
1467 declaration-list declaration
1468
1469 init-declarator-list:
1470 init-declarator
1471 init-declarator-list , init-declarator
1472
1473 init-declarator:
1474 declarator simple-asm-expr[opt] attributes[opt]
1475 declarator simple-asm-expr[opt] attributes[opt] = initializer
1476
1477 GNU extensions:
1478
1479 nested-function-definition:
1480 declaration-specifiers declarator declaration-list[opt]
1481 compound-statement
1482
1483 Objective-C:
1484 attributes objc-class-definition
1485 attributes objc-category-definition
1486 attributes objc-protocol-definition
1487
1488 The simple-asm-expr and attributes are GNU extensions.
1489
1490 This function does not handle __extension__; that is handled in its
1491 callers. ??? Following the old parser, __extension__ may start
1492 external declarations, declarations in functions and declarations
1493 at the start of "for" loops, but not old-style parameter
1494 declarations.
1495
1496 C99 requires declaration specifiers in a function definition; the
1497 absence is diagnosed through the diagnosis of implicit int. In GNU
1498 C we also allow but diagnose declarations without declaration
1499 specifiers, but only at top level (elsewhere they conflict with
1500 other syntax).
1501
1502 In Objective-C, declarations of the looping variable in a foreach
1503 statement are exceptionally terminated by 'in' (for example, 'for
1504 (NSObject *object in array) { ... }').
1505
1506 OpenMP:
1507
1508 declaration:
1509 threadprivate-directive */
1510
1511 static void
1512 c_parser_declaration_or_fndef (c_parser *parser, bool fndef_ok,
1513 bool static_assert_ok, bool empty_ok,
1514 bool nested, bool start_attr_ok,
1515 tree *objc_foreach_object_declaration,
1516 vec<c_token> omp_declare_simd_clauses)
1517 {
1518 struct c_declspecs *specs;
1519 tree prefix_attrs;
1520 tree all_prefix_attrs;
1521 bool diagnosed_no_specs = false;
1522 location_t here = c_parser_peek_token (parser)->location;
1523
1524 if (static_assert_ok
1525 && c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT))
1526 {
1527 c_parser_static_assert_declaration (parser);
1528 return;
1529 }
1530 specs = build_null_declspecs ();
1531
1532 /* Try to detect an unknown type name when we have "A B" or "A *B". */
1533 if (c_parser_peek_token (parser)->type == CPP_NAME
1534 && c_parser_peek_token (parser)->id_kind == C_ID_ID
1535 && (c_parser_peek_2nd_token (parser)->type == CPP_NAME
1536 || c_parser_peek_2nd_token (parser)->type == CPP_MULT)
1537 && (!nested || !lookup_name (c_parser_peek_token (parser)->value)))
1538 {
1539 tree name = c_parser_peek_token (parser)->value;
1540 error_at (here, "unknown type name %qE", name);
1541 /* Give a hint to the user. This is not C++ with its implicit
1542 typedef. */
1543 if (tag_exists_p (RECORD_TYPE, name))
1544 inform (here, "use %<struct%> keyword to refer to the type");
1545 else if (tag_exists_p (UNION_TYPE, name))
1546 inform (here, "use %<union%> keyword to refer to the type");
1547 else if (tag_exists_p (ENUMERAL_TYPE, name))
1548 inform (here, "use %<enum%> keyword to refer to the type");
1549
1550 /* Parse declspecs normally to get a correct pointer type, but avoid
1551 a further "fails to be a type name" error. Refuse nested functions
1552 since it is not how the user likely wants us to recover. */
1553 c_parser_peek_token (parser)->type = CPP_KEYWORD;
1554 c_parser_peek_token (parser)->keyword = RID_VOID;
1555 c_parser_peek_token (parser)->value = error_mark_node;
1556 fndef_ok = !nested;
1557 }
1558
1559 c_parser_declspecs (parser, specs, true, true, start_attr_ok,
1560 true, true, cla_nonabstract_decl);
1561 if (parser->error)
1562 {
1563 c_parser_skip_to_end_of_block_or_statement (parser);
1564 return;
1565 }
1566 if (nested && !specs->declspecs_seen_p)
1567 {
1568 c_parser_error (parser, "expected declaration specifiers");
1569 c_parser_skip_to_end_of_block_or_statement (parser);
1570 return;
1571 }
1572 finish_declspecs (specs);
1573 bool auto_type_p = specs->typespec_word == cts_auto_type;
1574 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1575 {
1576 if (auto_type_p)
1577 error_at (here, "%<__auto_type%> in empty declaration");
1578 else if (empty_ok)
1579 shadow_tag (specs);
1580 else
1581 {
1582 shadow_tag_warned (specs, 1);
1583 pedwarn (here, 0, "empty declaration");
1584 }
1585 c_parser_consume_token (parser);
1586 return;
1587 }
1588
1589 /* Provide better error recovery. Note that a type name here is usually
1590 better diagnosed as a redeclaration. */
1591 if (empty_ok
1592 && specs->typespec_kind == ctsk_tagdef
1593 && c_parser_next_token_starts_declspecs (parser)
1594 && !c_parser_next_token_is (parser, CPP_NAME))
1595 {
1596 c_parser_error (parser, "expected %<;%>, identifier or %<(%>");
1597 parser->error = false;
1598 shadow_tag_warned (specs, 1);
1599 return;
1600 }
1601 else if (c_dialect_objc () && !auto_type_p)
1602 {
1603 /* Prefix attributes are an error on method decls. */
1604 switch (c_parser_peek_token (parser)->type)
1605 {
1606 case CPP_PLUS:
1607 case CPP_MINUS:
1608 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1609 return;
1610 if (specs->attrs)
1611 {
1612 warning_at (c_parser_peek_token (parser)->location,
1613 OPT_Wattributes,
1614 "prefix attributes are ignored for methods");
1615 specs->attrs = NULL_TREE;
1616 }
1617 if (fndef_ok)
1618 c_parser_objc_method_definition (parser);
1619 else
1620 c_parser_objc_methodproto (parser);
1621 return;
1622 break;
1623 default:
1624 break;
1625 }
1626 /* This is where we parse 'attributes @interface ...',
1627 'attributes @implementation ...', 'attributes @protocol ...'
1628 (where attributes could be, for example, __attribute__
1629 ((deprecated)).
1630 */
1631 switch (c_parser_peek_token (parser)->keyword)
1632 {
1633 case RID_AT_INTERFACE:
1634 {
1635 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1636 return;
1637 c_parser_objc_class_definition (parser, specs->attrs);
1638 return;
1639 }
1640 break;
1641 case RID_AT_IMPLEMENTATION:
1642 {
1643 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1644 return;
1645 if (specs->attrs)
1646 {
1647 warning_at (c_parser_peek_token (parser)->location,
1648 OPT_Wattributes,
1649 "prefix attributes are ignored for implementations");
1650 specs->attrs = NULL_TREE;
1651 }
1652 c_parser_objc_class_definition (parser, NULL_TREE);
1653 return;
1654 }
1655 break;
1656 case RID_AT_PROTOCOL:
1657 {
1658 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1659 return;
1660 c_parser_objc_protocol_definition (parser, specs->attrs);
1661 return;
1662 }
1663 break;
1664 case RID_AT_ALIAS:
1665 case RID_AT_CLASS:
1666 case RID_AT_END:
1667 case RID_AT_PROPERTY:
1668 if (specs->attrs)
1669 {
1670 c_parser_error (parser, "unexpected attribute");
1671 specs->attrs = NULL;
1672 }
1673 break;
1674 default:
1675 break;
1676 }
1677 }
1678
1679 pending_xref_error ();
1680 prefix_attrs = specs->attrs;
1681 all_prefix_attrs = prefix_attrs;
1682 specs->attrs = NULL_TREE;
1683 while (true)
1684 {
1685 struct c_declarator *declarator;
1686 bool dummy = false;
1687 timevar_id_t tv;
1688 tree fnbody;
1689 /* Declaring either one or more declarators (in which case we
1690 should diagnose if there were no declaration specifiers) or a
1691 function definition (in which case the diagnostic for
1692 implicit int suffices). */
1693 declarator = c_parser_declarator (parser,
1694 specs->typespec_kind != ctsk_none,
1695 C_DTR_NORMAL, &dummy);
1696 if (declarator == NULL)
1697 {
1698 if (omp_declare_simd_clauses.exists ()
1699 || !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
1700 c_finish_omp_declare_simd (parser, NULL_TREE, NULL_TREE,
1701 omp_declare_simd_clauses);
1702 c_parser_skip_to_end_of_block_or_statement (parser);
1703 return;
1704 }
1705 if (auto_type_p && declarator->kind != cdk_id)
1706 {
1707 error_at (here,
1708 "%<__auto_type%> requires a plain identifier"
1709 " as declarator");
1710 c_parser_skip_to_end_of_block_or_statement (parser);
1711 return;
1712 }
1713 if (c_parser_next_token_is (parser, CPP_EQ)
1714 || c_parser_next_token_is (parser, CPP_COMMA)
1715 || c_parser_next_token_is (parser, CPP_SEMICOLON)
1716 || c_parser_next_token_is_keyword (parser, RID_ASM)
1717 || c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE)
1718 || c_parser_next_token_is_keyword (parser, RID_IN))
1719 {
1720 tree asm_name = NULL_TREE;
1721 tree postfix_attrs = NULL_TREE;
1722 if (!diagnosed_no_specs && !specs->declspecs_seen_p)
1723 {
1724 diagnosed_no_specs = true;
1725 pedwarn (here, 0, "data definition has no type or storage class");
1726 }
1727 /* Having seen a data definition, there cannot now be a
1728 function definition. */
1729 fndef_ok = false;
1730 if (c_parser_next_token_is_keyword (parser, RID_ASM))
1731 asm_name = c_parser_simple_asm_expr (parser);
1732 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
1733 {
1734 postfix_attrs = c_parser_attributes (parser);
1735 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
1736 {
1737 /* This means there is an attribute specifier after
1738 the declarator in a function definition. Provide
1739 some more information for the user. */
1740 error_at (here, "attributes should be specified before the "
1741 "declarator in a function definition");
1742 c_parser_skip_to_end_of_block_or_statement (parser);
1743 return;
1744 }
1745 }
1746 if (c_parser_next_token_is (parser, CPP_EQ))
1747 {
1748 tree d;
1749 struct c_expr init;
1750 location_t init_loc;
1751 c_parser_consume_token (parser);
1752 if (auto_type_p)
1753 {
1754 start_init (NULL_TREE, asm_name, global_bindings_p ());
1755 init_loc = c_parser_peek_token (parser)->location;
1756 init = c_parser_expr_no_commas (parser, NULL);
1757 if (TREE_CODE (init.value) == COMPONENT_REF
1758 && DECL_C_BIT_FIELD (TREE_OPERAND (init.value, 1)))
1759 error_at (here,
1760 "%<__auto_type%> used with a bit-field"
1761 " initializer");
1762 init = convert_lvalue_to_rvalue (init_loc, init, true, true);
1763 tree init_type = TREE_TYPE (init.value);
1764 /* As with typeof, remove all qualifiers from atomic types. */
1765 if (init_type != error_mark_node && TYPE_ATOMIC (init_type))
1766 init_type
1767 = c_build_qualified_type (init_type, TYPE_UNQUALIFIED);
1768 bool vm_type = variably_modified_type_p (init_type,
1769 NULL_TREE);
1770 if (vm_type)
1771 init.value = c_save_expr (init.value);
1772 finish_init ();
1773 specs->typespec_kind = ctsk_typeof;
1774 specs->locations[cdw_typedef] = init_loc;
1775 specs->typedef_p = true;
1776 specs->type = init_type;
1777 if (vm_type)
1778 {
1779 bool maybe_const = true;
1780 tree type_expr = c_fully_fold (init.value, false,
1781 &maybe_const);
1782 specs->expr_const_operands &= maybe_const;
1783 if (specs->expr)
1784 specs->expr = build2 (COMPOUND_EXPR,
1785 TREE_TYPE (type_expr),
1786 specs->expr, type_expr);
1787 else
1788 specs->expr = type_expr;
1789 }
1790 d = start_decl (declarator, specs, true,
1791 chainon (postfix_attrs, all_prefix_attrs));
1792 if (!d)
1793 d = error_mark_node;
1794 if (omp_declare_simd_clauses.exists ()
1795 || !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
1796 c_finish_omp_declare_simd (parser, d, NULL_TREE,
1797 omp_declare_simd_clauses);
1798 }
1799 else
1800 {
1801 /* The declaration of the variable is in effect while
1802 its initializer is parsed. */
1803 d = start_decl (declarator, specs, true,
1804 chainon (postfix_attrs, all_prefix_attrs));
1805 if (!d)
1806 d = error_mark_node;
1807 if (omp_declare_simd_clauses.exists ()
1808 || !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
1809 c_finish_omp_declare_simd (parser, d, NULL_TREE,
1810 omp_declare_simd_clauses);
1811 start_init (d, asm_name, global_bindings_p ());
1812 init_loc = c_parser_peek_token (parser)->location;
1813 init = c_parser_initializer (parser);
1814 finish_init ();
1815 }
1816 if (d != error_mark_node)
1817 {
1818 maybe_warn_string_init (init_loc, TREE_TYPE (d), init);
1819 finish_decl (d, init_loc, init.value,
1820 init.original_type, asm_name);
1821 }
1822 }
1823 else
1824 {
1825 if (auto_type_p)
1826 {
1827 error_at (here,
1828 "%<__auto_type%> requires an initialized "
1829 "data declaration");
1830 c_parser_skip_to_end_of_block_or_statement (parser);
1831 return;
1832 }
1833 tree d = start_decl (declarator, specs, false,
1834 chainon (postfix_attrs,
1835 all_prefix_attrs));
1836 if (omp_declare_simd_clauses.exists ()
1837 || !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
1838 {
1839 tree parms = NULL_TREE;
1840 if (d && TREE_CODE (d) == FUNCTION_DECL)
1841 {
1842 struct c_declarator *ce = declarator;
1843 while (ce != NULL)
1844 if (ce->kind == cdk_function)
1845 {
1846 parms = ce->u.arg_info->parms;
1847 break;
1848 }
1849 else
1850 ce = ce->declarator;
1851 }
1852 if (parms)
1853 temp_store_parm_decls (d, parms);
1854 c_finish_omp_declare_simd (parser, d, parms,
1855 omp_declare_simd_clauses);
1856 if (parms)
1857 temp_pop_parm_decls ();
1858 }
1859 if (d)
1860 finish_decl (d, UNKNOWN_LOCATION, NULL_TREE,
1861 NULL_TREE, asm_name);
1862
1863 if (c_parser_next_token_is_keyword (parser, RID_IN))
1864 {
1865 if (d)
1866 *objc_foreach_object_declaration = d;
1867 else
1868 *objc_foreach_object_declaration = error_mark_node;
1869 }
1870 }
1871 if (c_parser_next_token_is (parser, CPP_COMMA))
1872 {
1873 if (auto_type_p)
1874 {
1875 error_at (here,
1876 "%<__auto_type%> may only be used with"
1877 " a single declarator");
1878 c_parser_skip_to_end_of_block_or_statement (parser);
1879 return;
1880 }
1881 c_parser_consume_token (parser);
1882 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
1883 all_prefix_attrs = chainon (c_parser_attributes (parser),
1884 prefix_attrs);
1885 else
1886 all_prefix_attrs = prefix_attrs;
1887 continue;
1888 }
1889 else if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1890 {
1891 c_parser_consume_token (parser);
1892 return;
1893 }
1894 else if (c_parser_next_token_is_keyword (parser, RID_IN))
1895 {
1896 /* This can only happen in Objective-C: we found the
1897 'in' that terminates the declaration inside an
1898 Objective-C foreach statement. Do not consume the
1899 token, so that the caller can use it to determine
1900 that this indeed is a foreach context. */
1901 return;
1902 }
1903 else
1904 {
1905 c_parser_error (parser, "expected %<,%> or %<;%>");
1906 c_parser_skip_to_end_of_block_or_statement (parser);
1907 return;
1908 }
1909 }
1910 else if (auto_type_p)
1911 {
1912 error_at (here,
1913 "%<__auto_type%> requires an initialized data declaration");
1914 c_parser_skip_to_end_of_block_or_statement (parser);
1915 return;
1916 }
1917 else if (!fndef_ok)
1918 {
1919 c_parser_error (parser, "expected %<=%>, %<,%>, %<;%>, "
1920 "%<asm%> or %<__attribute__%>");
1921 c_parser_skip_to_end_of_block_or_statement (parser);
1922 return;
1923 }
1924 /* Function definition (nested or otherwise). */
1925 if (nested)
1926 {
1927 pedwarn (here, OPT_Wpedantic, "ISO C forbids nested functions");
1928 c_push_function_context ();
1929 }
1930 if (!start_function (specs, declarator, all_prefix_attrs))
1931 {
1932 /* This can appear in many cases looking nothing like a
1933 function definition, so we don't give a more specific
1934 error suggesting there was one. */
1935 c_parser_error (parser, "expected %<=%>, %<,%>, %<;%>, %<asm%> "
1936 "or %<__attribute__%>");
1937 if (nested)
1938 c_pop_function_context ();
1939 break;
1940 }
1941
1942 if (DECL_DECLARED_INLINE_P (current_function_decl))
1943 tv = TV_PARSE_INLINE;
1944 else
1945 tv = TV_PARSE_FUNC;
1946 timevar_push (tv);
1947
1948 /* Parse old-style parameter declarations. ??? Attributes are
1949 not allowed to start declaration specifiers here because of a
1950 syntax conflict between a function declaration with attribute
1951 suffix and a function definition with an attribute prefix on
1952 first old-style parameter declaration. Following the old
1953 parser, they are not accepted on subsequent old-style
1954 parameter declarations either. However, there is no
1955 ambiguity after the first declaration, nor indeed on the
1956 first as long as we don't allow postfix attributes after a
1957 declarator with a nonempty identifier list in a definition;
1958 and postfix attributes have never been accepted here in
1959 function definitions either. */
1960 while (c_parser_next_token_is_not (parser, CPP_EOF)
1961 && c_parser_next_token_is_not (parser, CPP_OPEN_BRACE))
1962 c_parser_declaration_or_fndef (parser, false, false, false,
1963 true, false, NULL, vNULL);
1964 store_parm_decls ();
1965 if (omp_declare_simd_clauses.exists ()
1966 || !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
1967 c_finish_omp_declare_simd (parser, current_function_decl, NULL_TREE,
1968 omp_declare_simd_clauses);
1969 DECL_STRUCT_FUNCTION (current_function_decl)->function_start_locus
1970 = c_parser_peek_token (parser)->location;
1971 fnbody = c_parser_compound_statement (parser);
1972 if (flag_cilkplus && contains_array_notation_expr (fnbody))
1973 fnbody = expand_array_notation_exprs (fnbody);
1974 if (nested)
1975 {
1976 tree decl = current_function_decl;
1977 /* Mark nested functions as needing static-chain initially.
1978 lower_nested_functions will recompute it but the
1979 DECL_STATIC_CHAIN flag is also used before that happens,
1980 by initializer_constant_valid_p. See gcc.dg/nested-fn-2.c. */
1981 DECL_STATIC_CHAIN (decl) = 1;
1982 add_stmt (fnbody);
1983 finish_function ();
1984 c_pop_function_context ();
1985 add_stmt (build_stmt (DECL_SOURCE_LOCATION (decl), DECL_EXPR, decl));
1986 }
1987 else
1988 {
1989 add_stmt (fnbody);
1990 finish_function ();
1991 }
1992
1993 timevar_pop (tv);
1994 break;
1995 }
1996 }
1997
1998 /* Parse an asm-definition (asm() outside a function body). This is a
1999 GNU extension.
2000
2001 asm-definition:
2002 simple-asm-expr ;
2003 */
2004
2005 static void
2006 c_parser_asm_definition (c_parser *parser)
2007 {
2008 tree asm_str = c_parser_simple_asm_expr (parser);
2009 if (asm_str)
2010 symtab->finalize_toplevel_asm (asm_str);
2011 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
2012 }
2013
2014 /* Parse a static assertion (C11 6.7.10).
2015
2016 static_assert-declaration:
2017 static_assert-declaration-no-semi ;
2018 */
2019
2020 static void
2021 c_parser_static_assert_declaration (c_parser *parser)
2022 {
2023 c_parser_static_assert_declaration_no_semi (parser);
2024 if (parser->error
2025 || !c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
2026 c_parser_skip_to_end_of_block_or_statement (parser);
2027 }
2028
2029 /* Parse a static assertion (C11 6.7.10), without the trailing
2030 semicolon.
2031
2032 static_assert-declaration-no-semi:
2033 _Static_assert ( constant-expression , string-literal )
2034 */
2035
2036 static void
2037 c_parser_static_assert_declaration_no_semi (c_parser *parser)
2038 {
2039 location_t assert_loc, value_loc;
2040 tree value;
2041 tree string;
2042
2043 gcc_assert (c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT));
2044 assert_loc = c_parser_peek_token (parser)->location;
2045 if (flag_isoc99)
2046 pedwarn_c99 (assert_loc, OPT_Wpedantic,
2047 "ISO C99 does not support %<_Static_assert%>");
2048 else
2049 pedwarn_c99 (assert_loc, OPT_Wpedantic,
2050 "ISO C90 does not support %<_Static_assert%>");
2051 c_parser_consume_token (parser);
2052 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2053 return;
2054 value_loc = c_parser_peek_token (parser)->location;
2055 value = c_parser_expr_no_commas (parser, NULL).value;
2056 parser->lex_untranslated_string = true;
2057 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
2058 {
2059 parser->lex_untranslated_string = false;
2060 return;
2061 }
2062 switch (c_parser_peek_token (parser)->type)
2063 {
2064 case CPP_STRING:
2065 case CPP_STRING16:
2066 case CPP_STRING32:
2067 case CPP_WSTRING:
2068 case CPP_UTF8STRING:
2069 string = c_parser_peek_token (parser)->value;
2070 c_parser_consume_token (parser);
2071 parser->lex_untranslated_string = false;
2072 break;
2073 default:
2074 c_parser_error (parser, "expected string literal");
2075 parser->lex_untranslated_string = false;
2076 return;
2077 }
2078 c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
2079
2080 if (!INTEGRAL_TYPE_P (TREE_TYPE (value)))
2081 {
2082 error_at (value_loc, "expression in static assertion is not an integer");
2083 return;
2084 }
2085 if (TREE_CODE (value) != INTEGER_CST)
2086 {
2087 value = c_fully_fold (value, false, NULL);
2088 /* Strip no-op conversions. */
2089 STRIP_TYPE_NOPS (value);
2090 if (TREE_CODE (value) == INTEGER_CST)
2091 pedwarn (value_loc, OPT_Wpedantic, "expression in static assertion "
2092 "is not an integer constant expression");
2093 }
2094 if (TREE_CODE (value) != INTEGER_CST)
2095 {
2096 error_at (value_loc, "expression in static assertion is not constant");
2097 return;
2098 }
2099 constant_expression_warning (value);
2100 if (integer_zerop (value))
2101 error_at (assert_loc, "static assertion failed: %E", string);
2102 }
2103
2104 /* Parse some declaration specifiers (possibly none) (C90 6.5, C99
2105 6.7), adding them to SPECS (which may already include some).
2106 Storage class specifiers are accepted iff SCSPEC_OK; type
2107 specifiers are accepted iff TYPESPEC_OK; alignment specifiers are
2108 accepted iff ALIGNSPEC_OK; attributes are accepted at the start
2109 iff START_ATTR_OK; __auto_type is accepted iff AUTO_TYPE_OK.
2110
2111 declaration-specifiers:
2112 storage-class-specifier declaration-specifiers[opt]
2113 type-specifier declaration-specifiers[opt]
2114 type-qualifier declaration-specifiers[opt]
2115 function-specifier declaration-specifiers[opt]
2116 alignment-specifier declaration-specifiers[opt]
2117
2118 Function specifiers (inline) are from C99, and are currently
2119 handled as storage class specifiers, as is __thread. Alignment
2120 specifiers are from C11.
2121
2122 C90 6.5.1, C99 6.7.1:
2123 storage-class-specifier:
2124 typedef
2125 extern
2126 static
2127 auto
2128 register
2129 _Thread_local
2130
2131 (_Thread_local is new in C11.)
2132
2133 C99 6.7.4:
2134 function-specifier:
2135 inline
2136 _Noreturn
2137
2138 (_Noreturn is new in C11.)
2139
2140 C90 6.5.2, C99 6.7.2:
2141 type-specifier:
2142 void
2143 char
2144 short
2145 int
2146 long
2147 float
2148 double
2149 signed
2150 unsigned
2151 _Bool
2152 _Complex
2153 [_Imaginary removed in C99 TC2]
2154 struct-or-union-specifier
2155 enum-specifier
2156 typedef-name
2157 atomic-type-specifier
2158
2159 (_Bool and _Complex are new in C99.)
2160 (atomic-type-specifier is new in C11.)
2161
2162 C90 6.5.3, C99 6.7.3:
2163
2164 type-qualifier:
2165 const
2166 restrict
2167 volatile
2168 address-space-qualifier
2169 _Atomic
2170
2171 (restrict is new in C99.)
2172 (_Atomic is new in C11.)
2173
2174 GNU extensions:
2175
2176 declaration-specifiers:
2177 attributes declaration-specifiers[opt]
2178
2179 type-qualifier:
2180 address-space
2181
2182 address-space:
2183 identifier recognized by the target
2184
2185 storage-class-specifier:
2186 __thread
2187
2188 type-specifier:
2189 typeof-specifier
2190 __auto_type
2191 __intN
2192 _Decimal32
2193 _Decimal64
2194 _Decimal128
2195 _Fract
2196 _Accum
2197 _Sat
2198
2199 (_Fract, _Accum, and _Sat are new from ISO/IEC DTR 18037:
2200 http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1169.pdf)
2201
2202 atomic-type-specifier
2203 _Atomic ( type-name )
2204
2205 Objective-C:
2206
2207 type-specifier:
2208 class-name objc-protocol-refs[opt]
2209 typedef-name objc-protocol-refs
2210 objc-protocol-refs
2211 */
2212
2213 static void
2214 c_parser_declspecs (c_parser *parser, struct c_declspecs *specs,
2215 bool scspec_ok, bool typespec_ok, bool start_attr_ok,
2216 bool alignspec_ok, bool auto_type_ok,
2217 enum c_lookahead_kind la)
2218 {
2219 bool attrs_ok = start_attr_ok;
2220 bool seen_type = specs->typespec_kind != ctsk_none;
2221
2222 if (!typespec_ok)
2223 gcc_assert (la == cla_prefer_id);
2224
2225 while (c_parser_next_token_is (parser, CPP_NAME)
2226 || c_parser_next_token_is (parser, CPP_KEYWORD)
2227 || (c_dialect_objc () && c_parser_next_token_is (parser, CPP_LESS)))
2228 {
2229 struct c_typespec t;
2230 tree attrs;
2231 tree align;
2232 location_t loc = c_parser_peek_token (parser)->location;
2233
2234 /* If we cannot accept a type, exit if the next token must start
2235 one. Also, if we already have seen a tagged definition,
2236 a typename would be an error anyway and likely the user
2237 has simply forgotten a semicolon, so we exit. */
2238 if ((!typespec_ok || specs->typespec_kind == ctsk_tagdef)
2239 && c_parser_next_tokens_start_typename (parser, la)
2240 && !c_parser_next_token_is_qualifier (parser))
2241 break;
2242
2243 if (c_parser_next_token_is (parser, CPP_NAME))
2244 {
2245 c_token *name_token = c_parser_peek_token (parser);
2246 tree value = name_token->value;
2247 c_id_kind kind = name_token->id_kind;
2248
2249 if (kind == C_ID_ADDRSPACE)
2250 {
2251 addr_space_t as
2252 = name_token->keyword - RID_FIRST_ADDR_SPACE;
2253 declspecs_add_addrspace (name_token->location, specs, as);
2254 c_parser_consume_token (parser);
2255 attrs_ok = true;
2256 continue;
2257 }
2258
2259 gcc_assert (!c_parser_next_token_is_qualifier (parser));
2260
2261 /* If we cannot accept a type, and the next token must start one,
2262 exit. Do the same if we already have seen a tagged definition,
2263 since it would be an error anyway and likely the user has simply
2264 forgotten a semicolon. */
2265 if (seen_type || !c_parser_next_tokens_start_typename (parser, la))
2266 break;
2267
2268 /* Now at an unknown typename (C_ID_ID), a C_ID_TYPENAME or
2269 a C_ID_CLASSNAME. */
2270 c_parser_consume_token (parser);
2271 seen_type = true;
2272 attrs_ok = true;
2273 if (kind == C_ID_ID)
2274 {
2275 error_at (loc, "unknown type name %qE", value);
2276 t.kind = ctsk_typedef;
2277 t.spec = error_mark_node;
2278 }
2279 else if (kind == C_ID_TYPENAME
2280 && (!c_dialect_objc ()
2281 || c_parser_next_token_is_not (parser, CPP_LESS)))
2282 {
2283 t.kind = ctsk_typedef;
2284 /* For a typedef name, record the meaning, not the name.
2285 In case of 'foo foo, bar;'. */
2286 t.spec = lookup_name (value);
2287 }
2288 else
2289 {
2290 tree proto = NULL_TREE;
2291 gcc_assert (c_dialect_objc ());
2292 t.kind = ctsk_objc;
2293 if (c_parser_next_token_is (parser, CPP_LESS))
2294 proto = c_parser_objc_protocol_refs (parser);
2295 t.spec = objc_get_protocol_qualified_type (value, proto);
2296 }
2297 t.expr = NULL_TREE;
2298 t.expr_const_operands = true;
2299 declspecs_add_type (name_token->location, specs, t);
2300 continue;
2301 }
2302 if (c_parser_next_token_is (parser, CPP_LESS))
2303 {
2304 /* Make "<SomeProtocol>" equivalent to "id <SomeProtocol>" -
2305 nisse@lysator.liu.se. */
2306 tree proto;
2307 gcc_assert (c_dialect_objc ());
2308 if (!typespec_ok || seen_type)
2309 break;
2310 proto = c_parser_objc_protocol_refs (parser);
2311 t.kind = ctsk_objc;
2312 t.spec = objc_get_protocol_qualified_type (NULL_TREE, proto);
2313 t.expr = NULL_TREE;
2314 t.expr_const_operands = true;
2315 declspecs_add_type (loc, specs, t);
2316 continue;
2317 }
2318 gcc_assert (c_parser_next_token_is (parser, CPP_KEYWORD));
2319 switch (c_parser_peek_token (parser)->keyword)
2320 {
2321 case RID_STATIC:
2322 case RID_EXTERN:
2323 case RID_REGISTER:
2324 case RID_TYPEDEF:
2325 case RID_INLINE:
2326 case RID_NORETURN:
2327 case RID_AUTO:
2328 case RID_THREAD:
2329 if (!scspec_ok)
2330 goto out;
2331 attrs_ok = true;
2332 /* TODO: Distinguish between function specifiers (inline, noreturn)
2333 and storage class specifiers, either here or in
2334 declspecs_add_scspec. */
2335 declspecs_add_scspec (loc, specs,
2336 c_parser_peek_token (parser)->value);
2337 c_parser_consume_token (parser);
2338 break;
2339 case RID_AUTO_TYPE:
2340 if (!auto_type_ok)
2341 goto out;
2342 /* Fall through. */
2343 case RID_UNSIGNED:
2344 case RID_LONG:
2345 case RID_SHORT:
2346 case RID_SIGNED:
2347 case RID_COMPLEX:
2348 case RID_INT:
2349 case RID_CHAR:
2350 case RID_FLOAT:
2351 case RID_DOUBLE:
2352 case RID_VOID:
2353 case RID_DFLOAT32:
2354 case RID_DFLOAT64:
2355 case RID_DFLOAT128:
2356 case RID_BOOL:
2357 case RID_FRACT:
2358 case RID_ACCUM:
2359 case RID_SAT:
2360 case RID_INT_N_0:
2361 case RID_INT_N_1:
2362 case RID_INT_N_2:
2363 case RID_INT_N_3:
2364 if (!typespec_ok)
2365 goto out;
2366 attrs_ok = true;
2367 seen_type = true;
2368 if (c_dialect_objc ())
2369 parser->objc_need_raw_identifier = true;
2370 t.kind = ctsk_resword;
2371 t.spec = c_parser_peek_token (parser)->value;
2372 t.expr = NULL_TREE;
2373 t.expr_const_operands = true;
2374 declspecs_add_type (loc, specs, t);
2375 c_parser_consume_token (parser);
2376 break;
2377 case RID_ENUM:
2378 if (!typespec_ok)
2379 goto out;
2380 attrs_ok = true;
2381 seen_type = true;
2382 t = c_parser_enum_specifier (parser);
2383 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, t.spec);
2384 declspecs_add_type (loc, specs, t);
2385 break;
2386 case RID_STRUCT:
2387 case RID_UNION:
2388 if (!typespec_ok)
2389 goto out;
2390 attrs_ok = true;
2391 seen_type = true;
2392 t = c_parser_struct_or_union_specifier (parser);
2393 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, t.spec);
2394 declspecs_add_type (loc, specs, t);
2395 break;
2396 case RID_TYPEOF:
2397 /* ??? The old parser rejected typeof after other type
2398 specifiers, but is a syntax error the best way of
2399 handling this? */
2400 if (!typespec_ok || seen_type)
2401 goto out;
2402 attrs_ok = true;
2403 seen_type = true;
2404 t = c_parser_typeof_specifier (parser);
2405 declspecs_add_type (loc, specs, t);
2406 break;
2407 case RID_ATOMIC:
2408 /* C parser handling of Objective-C constructs needs
2409 checking for correct lvalue-to-rvalue conversions, and
2410 the code in build_modify_expr handling various
2411 Objective-C cases, and that in build_unary_op handling
2412 Objective-C cases for increment / decrement, also needs
2413 updating; uses of TYPE_MAIN_VARIANT in objc_compare_types
2414 and objc_types_are_equivalent may also need updates. */
2415 if (c_dialect_objc ())
2416 sorry ("%<_Atomic%> in Objective-C");
2417 /* C parser handling of OpenMP constructs needs checking for
2418 correct lvalue-to-rvalue conversions. */
2419 if (flag_openmp)
2420 sorry ("%<_Atomic%> with OpenMP");
2421 if (flag_isoc99)
2422 pedwarn_c99 (loc, OPT_Wpedantic,
2423 "ISO C99 does not support the %<_Atomic%> qualifier");
2424 else
2425 pedwarn_c99 (loc, OPT_Wpedantic,
2426 "ISO C90 does not support the %<_Atomic%> qualifier");
2427 attrs_ok = true;
2428 tree value;
2429 value = c_parser_peek_token (parser)->value;
2430 c_parser_consume_token (parser);
2431 if (typespec_ok && c_parser_next_token_is (parser, CPP_OPEN_PAREN))
2432 {
2433 /* _Atomic ( type-name ). */
2434 seen_type = true;
2435 c_parser_consume_token (parser);
2436 struct c_type_name *type = c_parser_type_name (parser);
2437 t.kind = ctsk_typeof;
2438 t.spec = error_mark_node;
2439 t.expr = NULL_TREE;
2440 t.expr_const_operands = true;
2441 if (type != NULL)
2442 t.spec = groktypename (type, &t.expr,
2443 &t.expr_const_operands);
2444 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2445 "expected %<)%>");
2446 if (t.spec != error_mark_node)
2447 {
2448 if (TREE_CODE (t.spec) == ARRAY_TYPE)
2449 error_at (loc, "%<_Atomic%>-qualified array type");
2450 else if (TREE_CODE (t.spec) == FUNCTION_TYPE)
2451 error_at (loc, "%<_Atomic%>-qualified function type");
2452 else if (TYPE_QUALS (t.spec) != TYPE_UNQUALIFIED)
2453 error_at (loc, "%<_Atomic%> applied to a qualified type");
2454 else
2455 t.spec = c_build_qualified_type (t.spec, TYPE_QUAL_ATOMIC);
2456 }
2457 declspecs_add_type (loc, specs, t);
2458 }
2459 else
2460 declspecs_add_qual (loc, specs, value);
2461 break;
2462 case RID_CONST:
2463 case RID_VOLATILE:
2464 case RID_RESTRICT:
2465 attrs_ok = true;
2466 declspecs_add_qual (loc, specs, c_parser_peek_token (parser)->value);
2467 c_parser_consume_token (parser);
2468 break;
2469 case RID_ATTRIBUTE:
2470 if (!attrs_ok)
2471 goto out;
2472 attrs = c_parser_attributes (parser);
2473 declspecs_add_attrs (loc, specs, attrs);
2474 break;
2475 case RID_ALIGNAS:
2476 if (!alignspec_ok)
2477 goto out;
2478 align = c_parser_alignas_specifier (parser);
2479 declspecs_add_alignas (loc, specs, align);
2480 break;
2481 default:
2482 goto out;
2483 }
2484 }
2485 out: ;
2486 }
2487
2488 /* Parse an enum specifier (C90 6.5.2.2, C99 6.7.2.2).
2489
2490 enum-specifier:
2491 enum attributes[opt] identifier[opt] { enumerator-list } attributes[opt]
2492 enum attributes[opt] identifier[opt] { enumerator-list , } attributes[opt]
2493 enum attributes[opt] identifier
2494
2495 The form with trailing comma is new in C99. The forms with
2496 attributes are GNU extensions. In GNU C, we accept any expression
2497 without commas in the syntax (assignment expressions, not just
2498 conditional expressions); assignment expressions will be diagnosed
2499 as non-constant.
2500
2501 enumerator-list:
2502 enumerator
2503 enumerator-list , enumerator
2504
2505 enumerator:
2506 enumeration-constant
2507 enumeration-constant = constant-expression
2508
2509 GNU Extensions:
2510
2511 enumerator:
2512 enumeration-constant attributes[opt]
2513 enumeration-constant attributes[opt] = constant-expression
2514
2515 */
2516
2517 static struct c_typespec
2518 c_parser_enum_specifier (c_parser *parser)
2519 {
2520 struct c_typespec ret;
2521 tree attrs;
2522 tree ident = NULL_TREE;
2523 location_t enum_loc;
2524 location_t ident_loc = UNKNOWN_LOCATION; /* Quiet warning. */
2525 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ENUM));
2526 enum_loc = c_parser_peek_token (parser)->location;
2527 c_parser_consume_token (parser);
2528 attrs = c_parser_attributes (parser);
2529 enum_loc = c_parser_peek_token (parser)->location;
2530 /* Set the location in case we create a decl now. */
2531 c_parser_set_source_position_from_token (c_parser_peek_token (parser));
2532 if (c_parser_next_token_is (parser, CPP_NAME))
2533 {
2534 ident = c_parser_peek_token (parser)->value;
2535 ident_loc = c_parser_peek_token (parser)->location;
2536 enum_loc = ident_loc;
2537 c_parser_consume_token (parser);
2538 }
2539 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
2540 {
2541 /* Parse an enum definition. */
2542 struct c_enum_contents the_enum;
2543 tree type;
2544 tree postfix_attrs;
2545 /* We chain the enumerators in reverse order, then put them in
2546 forward order at the end. */
2547 tree values;
2548 timevar_push (TV_PARSE_ENUM);
2549 type = start_enum (enum_loc, &the_enum, ident);
2550 values = NULL_TREE;
2551 c_parser_consume_token (parser);
2552 while (true)
2553 {
2554 tree enum_id;
2555 tree enum_value;
2556 tree enum_decl;
2557 bool seen_comma;
2558 c_token *token;
2559 location_t comma_loc = UNKNOWN_LOCATION; /* Quiet warning. */
2560 location_t decl_loc, value_loc;
2561 if (c_parser_next_token_is_not (parser, CPP_NAME))
2562 {
2563 /* Give a nicer error for "enum {}". */
2564 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE)
2565 && !parser->error)
2566 {
2567 error_at (c_parser_peek_token (parser)->location,
2568 "empty enum is invalid");
2569 parser->error = true;
2570 }
2571 else
2572 c_parser_error (parser, "expected identifier");
2573 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
2574 values = error_mark_node;
2575 break;
2576 }
2577 token = c_parser_peek_token (parser);
2578 enum_id = token->value;
2579 /* Set the location in case we create a decl now. */
2580 c_parser_set_source_position_from_token (token);
2581 decl_loc = value_loc = token->location;
2582 c_parser_consume_token (parser);
2583 /* Parse any specified attributes. */
2584 tree enum_attrs = c_parser_attributes (parser);
2585 if (c_parser_next_token_is (parser, CPP_EQ))
2586 {
2587 c_parser_consume_token (parser);
2588 value_loc = c_parser_peek_token (parser)->location;
2589 enum_value = c_parser_expr_no_commas (parser, NULL).value;
2590 }
2591 else
2592 enum_value = NULL_TREE;
2593 enum_decl = build_enumerator (decl_loc, value_loc,
2594 &the_enum, enum_id, enum_value);
2595 if (enum_attrs)
2596 decl_attributes (&TREE_PURPOSE (enum_decl), enum_attrs, 0);
2597 TREE_CHAIN (enum_decl) = values;
2598 values = enum_decl;
2599 seen_comma = false;
2600 if (c_parser_next_token_is (parser, CPP_COMMA))
2601 {
2602 comma_loc = c_parser_peek_token (parser)->location;
2603 seen_comma = true;
2604 c_parser_consume_token (parser);
2605 }
2606 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2607 {
2608 if (seen_comma)
2609 pedwarn_c90 (comma_loc, OPT_Wpedantic,
2610 "comma at end of enumerator list");
2611 c_parser_consume_token (parser);
2612 break;
2613 }
2614 if (!seen_comma)
2615 {
2616 c_parser_error (parser, "expected %<,%> or %<}%>");
2617 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
2618 values = error_mark_node;
2619 break;
2620 }
2621 }
2622 postfix_attrs = c_parser_attributes (parser);
2623 ret.spec = finish_enum (type, nreverse (values),
2624 chainon (attrs, postfix_attrs));
2625 ret.kind = ctsk_tagdef;
2626 ret.expr = NULL_TREE;
2627 ret.expr_const_operands = true;
2628 timevar_pop (TV_PARSE_ENUM);
2629 return ret;
2630 }
2631 else if (!ident)
2632 {
2633 c_parser_error (parser, "expected %<{%>");
2634 ret.spec = error_mark_node;
2635 ret.kind = ctsk_tagref;
2636 ret.expr = NULL_TREE;
2637 ret.expr_const_operands = true;
2638 return ret;
2639 }
2640 ret = parser_xref_tag (ident_loc, ENUMERAL_TYPE, ident);
2641 /* In ISO C, enumerated types can be referred to only if already
2642 defined. */
2643 if (pedantic && !COMPLETE_TYPE_P (ret.spec))
2644 {
2645 gcc_assert (ident);
2646 pedwarn (enum_loc, OPT_Wpedantic,
2647 "ISO C forbids forward references to %<enum%> types");
2648 }
2649 return ret;
2650 }
2651
2652 /* Parse a struct or union specifier (C90 6.5.2.1, C99 6.7.2.1).
2653
2654 struct-or-union-specifier:
2655 struct-or-union attributes[opt] identifier[opt]
2656 { struct-contents } attributes[opt]
2657 struct-or-union attributes[opt] identifier
2658
2659 struct-contents:
2660 struct-declaration-list
2661
2662 struct-declaration-list:
2663 struct-declaration ;
2664 struct-declaration-list struct-declaration ;
2665
2666 GNU extensions:
2667
2668 struct-contents:
2669 empty
2670 struct-declaration
2671 struct-declaration-list struct-declaration
2672
2673 struct-declaration-list:
2674 struct-declaration-list ;
2675 ;
2676
2677 (Note that in the syntax here, unlike that in ISO C, the semicolons
2678 are included here rather than in struct-declaration, in order to
2679 describe the syntax with extra semicolons and missing semicolon at
2680 end.)
2681
2682 Objective-C:
2683
2684 struct-declaration-list:
2685 @defs ( class-name )
2686
2687 (Note this does not include a trailing semicolon, but can be
2688 followed by further declarations, and gets a pedwarn-if-pedantic
2689 when followed by a semicolon.) */
2690
2691 static struct c_typespec
2692 c_parser_struct_or_union_specifier (c_parser *parser)
2693 {
2694 struct c_typespec ret;
2695 tree attrs;
2696 tree ident = NULL_TREE;
2697 location_t struct_loc;
2698 location_t ident_loc = UNKNOWN_LOCATION;
2699 enum tree_code code;
2700 switch (c_parser_peek_token (parser)->keyword)
2701 {
2702 case RID_STRUCT:
2703 code = RECORD_TYPE;
2704 break;
2705 case RID_UNION:
2706 code = UNION_TYPE;
2707 break;
2708 default:
2709 gcc_unreachable ();
2710 }
2711 struct_loc = c_parser_peek_token (parser)->location;
2712 c_parser_consume_token (parser);
2713 attrs = c_parser_attributes (parser);
2714
2715 /* Set the location in case we create a decl now. */
2716 c_parser_set_source_position_from_token (c_parser_peek_token (parser));
2717
2718 if (c_parser_next_token_is (parser, CPP_NAME))
2719 {
2720 ident = c_parser_peek_token (parser)->value;
2721 ident_loc = c_parser_peek_token (parser)->location;
2722 struct_loc = ident_loc;
2723 c_parser_consume_token (parser);
2724 }
2725 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
2726 {
2727 /* Parse a struct or union definition. Start the scope of the
2728 tag before parsing components. */
2729 struct c_struct_parse_info *struct_info;
2730 tree type = start_struct (struct_loc, code, ident, &struct_info);
2731 tree postfix_attrs;
2732 /* We chain the components in reverse order, then put them in
2733 forward order at the end. Each struct-declaration may
2734 declare multiple components (comma-separated), so we must use
2735 chainon to join them, although when parsing each
2736 struct-declaration we can use TREE_CHAIN directly.
2737
2738 The theory behind all this is that there will be more
2739 semicolon separated fields than comma separated fields, and
2740 so we'll be minimizing the number of node traversals required
2741 by chainon. */
2742 tree contents;
2743 timevar_push (TV_PARSE_STRUCT);
2744 contents = NULL_TREE;
2745 c_parser_consume_token (parser);
2746 /* Handle the Objective-C @defs construct,
2747 e.g. foo(sizeof(struct{ @defs(ClassName) }));. */
2748 if (c_parser_next_token_is_keyword (parser, RID_AT_DEFS))
2749 {
2750 tree name;
2751 gcc_assert (c_dialect_objc ());
2752 c_parser_consume_token (parser);
2753 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2754 goto end_at_defs;
2755 if (c_parser_next_token_is (parser, CPP_NAME)
2756 && c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME)
2757 {
2758 name = c_parser_peek_token (parser)->value;
2759 c_parser_consume_token (parser);
2760 }
2761 else
2762 {
2763 c_parser_error (parser, "expected class name");
2764 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
2765 goto end_at_defs;
2766 }
2767 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2768 "expected %<)%>");
2769 contents = nreverse (objc_get_class_ivars (name));
2770 }
2771 end_at_defs:
2772 /* Parse the struct-declarations and semicolons. Problems with
2773 semicolons are diagnosed here; empty structures are diagnosed
2774 elsewhere. */
2775 while (true)
2776 {
2777 tree decls;
2778 /* Parse any stray semicolon. */
2779 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
2780 {
2781 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
2782 "extra semicolon in struct or union specified");
2783 c_parser_consume_token (parser);
2784 continue;
2785 }
2786 /* Stop if at the end of the struct or union contents. */
2787 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2788 {
2789 c_parser_consume_token (parser);
2790 break;
2791 }
2792 /* Accept #pragmas at struct scope. */
2793 if (c_parser_next_token_is (parser, CPP_PRAGMA))
2794 {
2795 c_parser_pragma (parser, pragma_struct);
2796 continue;
2797 }
2798 /* Parse some comma-separated declarations, but not the
2799 trailing semicolon if any. */
2800 decls = c_parser_struct_declaration (parser);
2801 contents = chainon (decls, contents);
2802 /* If no semicolon follows, either we have a parse error or
2803 are at the end of the struct or union and should
2804 pedwarn. */
2805 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
2806 c_parser_consume_token (parser);
2807 else
2808 {
2809 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2810 pedwarn (c_parser_peek_token (parser)->location, 0,
2811 "no semicolon at end of struct or union");
2812 else if (parser->error
2813 || !c_parser_next_token_starts_declspecs (parser))
2814 {
2815 c_parser_error (parser, "expected %<;%>");
2816 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
2817 break;
2818 }
2819
2820 /* If we come here, we have already emitted an error
2821 for an expected `;', identifier or `(', and we also
2822 recovered already. Go on with the next field. */
2823 }
2824 }
2825 postfix_attrs = c_parser_attributes (parser);
2826 ret.spec = finish_struct (struct_loc, type, nreverse (contents),
2827 chainon (attrs, postfix_attrs), struct_info);
2828 ret.kind = ctsk_tagdef;
2829 ret.expr = NULL_TREE;
2830 ret.expr_const_operands = true;
2831 timevar_pop (TV_PARSE_STRUCT);
2832 return ret;
2833 }
2834 else if (!ident)
2835 {
2836 c_parser_error (parser, "expected %<{%>");
2837 ret.spec = error_mark_node;
2838 ret.kind = ctsk_tagref;
2839 ret.expr = NULL_TREE;
2840 ret.expr_const_operands = true;
2841 return ret;
2842 }
2843 ret = parser_xref_tag (ident_loc, code, ident);
2844 return ret;
2845 }
2846
2847 /* Parse a struct-declaration (C90 6.5.2.1, C99 6.7.2.1), *without*
2848 the trailing semicolon.
2849
2850 struct-declaration:
2851 specifier-qualifier-list struct-declarator-list
2852 static_assert-declaration-no-semi
2853
2854 specifier-qualifier-list:
2855 type-specifier specifier-qualifier-list[opt]
2856 type-qualifier specifier-qualifier-list[opt]
2857 attributes specifier-qualifier-list[opt]
2858
2859 struct-declarator-list:
2860 struct-declarator
2861 struct-declarator-list , attributes[opt] struct-declarator
2862
2863 struct-declarator:
2864 declarator attributes[opt]
2865 declarator[opt] : constant-expression attributes[opt]
2866
2867 GNU extensions:
2868
2869 struct-declaration:
2870 __extension__ struct-declaration
2871 specifier-qualifier-list
2872
2873 Unlike the ISO C syntax, semicolons are handled elsewhere. The use
2874 of attributes where shown is a GNU extension. In GNU C, we accept
2875 any expression without commas in the syntax (assignment
2876 expressions, not just conditional expressions); assignment
2877 expressions will be diagnosed as non-constant. */
2878
2879 static tree
2880 c_parser_struct_declaration (c_parser *parser)
2881 {
2882 struct c_declspecs *specs;
2883 tree prefix_attrs;
2884 tree all_prefix_attrs;
2885 tree decls;
2886 location_t decl_loc;
2887 if (c_parser_next_token_is_keyword (parser, RID_EXTENSION))
2888 {
2889 int ext;
2890 tree decl;
2891 ext = disable_extension_diagnostics ();
2892 c_parser_consume_token (parser);
2893 decl = c_parser_struct_declaration (parser);
2894 restore_extension_diagnostics (ext);
2895 return decl;
2896 }
2897 if (c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT))
2898 {
2899 c_parser_static_assert_declaration_no_semi (parser);
2900 return NULL_TREE;
2901 }
2902 specs = build_null_declspecs ();
2903 decl_loc = c_parser_peek_token (parser)->location;
2904 /* Strictly by the standard, we shouldn't allow _Alignas here,
2905 but it appears to have been intended to allow it there, so
2906 we're keeping it as it is until WG14 reaches a conclusion
2907 of N1731.
2908 <http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1731.pdf> */
2909 c_parser_declspecs (parser, specs, false, true, true,
2910 true, false, cla_nonabstract_decl);
2911 if (parser->error)
2912 return NULL_TREE;
2913 if (!specs->declspecs_seen_p)
2914 {
2915 c_parser_error (parser, "expected specifier-qualifier-list");
2916 return NULL_TREE;
2917 }
2918 finish_declspecs (specs);
2919 if (c_parser_next_token_is (parser, CPP_SEMICOLON)
2920 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2921 {
2922 tree ret;
2923 if (specs->typespec_kind == ctsk_none)
2924 {
2925 pedwarn (decl_loc, OPT_Wpedantic,
2926 "ISO C forbids member declarations with no members");
2927 shadow_tag_warned (specs, pedantic);
2928 ret = NULL_TREE;
2929 }
2930 else
2931 {
2932 /* Support for unnamed structs or unions as members of
2933 structs or unions (which is [a] useful and [b] supports
2934 MS P-SDK). */
2935 tree attrs = NULL;
2936
2937 ret = grokfield (c_parser_peek_token (parser)->location,
2938 build_id_declarator (NULL_TREE), specs,
2939 NULL_TREE, &attrs);
2940 if (ret)
2941 decl_attributes (&ret, attrs, 0);
2942 }
2943 return ret;
2944 }
2945
2946 /* Provide better error recovery. Note that a type name here is valid,
2947 and will be treated as a field name. */
2948 if (specs->typespec_kind == ctsk_tagdef
2949 && TREE_CODE (specs->type) != ENUMERAL_TYPE
2950 && c_parser_next_token_starts_declspecs (parser)
2951 && !c_parser_next_token_is (parser, CPP_NAME))
2952 {
2953 c_parser_error (parser, "expected %<;%>, identifier or %<(%>");
2954 parser->error = false;
2955 return NULL_TREE;
2956 }
2957
2958 pending_xref_error ();
2959 prefix_attrs = specs->attrs;
2960 all_prefix_attrs = prefix_attrs;
2961 specs->attrs = NULL_TREE;
2962 decls = NULL_TREE;
2963 while (true)
2964 {
2965 /* Declaring one or more declarators or un-named bit-fields. */
2966 struct c_declarator *declarator;
2967 bool dummy = false;
2968 if (c_parser_next_token_is (parser, CPP_COLON))
2969 declarator = build_id_declarator (NULL_TREE);
2970 else
2971 declarator = c_parser_declarator (parser,
2972 specs->typespec_kind != ctsk_none,
2973 C_DTR_NORMAL, &dummy);
2974 if (declarator == NULL)
2975 {
2976 c_parser_skip_to_end_of_block_or_statement (parser);
2977 break;
2978 }
2979 if (c_parser_next_token_is (parser, CPP_COLON)
2980 || c_parser_next_token_is (parser, CPP_COMMA)
2981 || c_parser_next_token_is (parser, CPP_SEMICOLON)
2982 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE)
2983 || c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2984 {
2985 tree postfix_attrs = NULL_TREE;
2986 tree width = NULL_TREE;
2987 tree d;
2988 if (c_parser_next_token_is (parser, CPP_COLON))
2989 {
2990 c_parser_consume_token (parser);
2991 width = c_parser_expr_no_commas (parser, NULL).value;
2992 }
2993 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2994 postfix_attrs = c_parser_attributes (parser);
2995 d = grokfield (c_parser_peek_token (parser)->location,
2996 declarator, specs, width, &all_prefix_attrs);
2997 decl_attributes (&d, chainon (postfix_attrs,
2998 all_prefix_attrs), 0);
2999 DECL_CHAIN (d) = decls;
3000 decls = d;
3001 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
3002 all_prefix_attrs = chainon (c_parser_attributes (parser),
3003 prefix_attrs);
3004 else
3005 all_prefix_attrs = prefix_attrs;
3006 if (c_parser_next_token_is (parser, CPP_COMMA))
3007 c_parser_consume_token (parser);
3008 else if (c_parser_next_token_is (parser, CPP_SEMICOLON)
3009 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3010 {
3011 /* Semicolon consumed in caller. */
3012 break;
3013 }
3014 else
3015 {
3016 c_parser_error (parser, "expected %<,%>, %<;%> or %<}%>");
3017 break;
3018 }
3019 }
3020 else
3021 {
3022 c_parser_error (parser,
3023 "expected %<:%>, %<,%>, %<;%>, %<}%> or "
3024 "%<__attribute__%>");
3025 break;
3026 }
3027 }
3028 return decls;
3029 }
3030
3031 /* Parse a typeof specifier (a GNU extension).
3032
3033 typeof-specifier:
3034 typeof ( expression )
3035 typeof ( type-name )
3036 */
3037
3038 static struct c_typespec
3039 c_parser_typeof_specifier (c_parser *parser)
3040 {
3041 struct c_typespec ret;
3042 ret.kind = ctsk_typeof;
3043 ret.spec = error_mark_node;
3044 ret.expr = NULL_TREE;
3045 ret.expr_const_operands = true;
3046 gcc_assert (c_parser_next_token_is_keyword (parser, RID_TYPEOF));
3047 c_parser_consume_token (parser);
3048 c_inhibit_evaluation_warnings++;
3049 in_typeof++;
3050 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3051 {
3052 c_inhibit_evaluation_warnings--;
3053 in_typeof--;
3054 return ret;
3055 }
3056 if (c_parser_next_tokens_start_typename (parser, cla_prefer_id))
3057 {
3058 struct c_type_name *type = c_parser_type_name (parser);
3059 c_inhibit_evaluation_warnings--;
3060 in_typeof--;
3061 if (type != NULL)
3062 {
3063 ret.spec = groktypename (type, &ret.expr, &ret.expr_const_operands);
3064 pop_maybe_used (variably_modified_type_p (ret.spec, NULL_TREE));
3065 }
3066 }
3067 else
3068 {
3069 bool was_vm;
3070 location_t here = c_parser_peek_token (parser)->location;
3071 struct c_expr expr = c_parser_expression (parser);
3072 c_inhibit_evaluation_warnings--;
3073 in_typeof--;
3074 if (TREE_CODE (expr.value) == COMPONENT_REF
3075 && DECL_C_BIT_FIELD (TREE_OPERAND (expr.value, 1)))
3076 error_at (here, "%<typeof%> applied to a bit-field");
3077 mark_exp_read (expr.value);
3078 ret.spec = TREE_TYPE (expr.value);
3079 was_vm = variably_modified_type_p (ret.spec, NULL_TREE);
3080 /* This is returned with the type so that when the type is
3081 evaluated, this can be evaluated. */
3082 if (was_vm)
3083 ret.expr = c_fully_fold (expr.value, false, &ret.expr_const_operands);
3084 pop_maybe_used (was_vm);
3085 /* For use in macros such as those in <stdatomic.h>, remove all
3086 qualifiers from atomic types. (const can be an issue for more macros
3087 using typeof than just the <stdatomic.h> ones.) */
3088 if (ret.spec != error_mark_node && TYPE_ATOMIC (ret.spec))
3089 ret.spec = c_build_qualified_type (ret.spec, TYPE_UNQUALIFIED);
3090 }
3091 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
3092 return ret;
3093 }
3094
3095 /* Parse an alignment-specifier.
3096
3097 C11 6.7.5:
3098
3099 alignment-specifier:
3100 _Alignas ( type-name )
3101 _Alignas ( constant-expression )
3102 */
3103
3104 static tree
3105 c_parser_alignas_specifier (c_parser * parser)
3106 {
3107 tree ret = error_mark_node;
3108 location_t loc = c_parser_peek_token (parser)->location;
3109 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ALIGNAS));
3110 c_parser_consume_token (parser);
3111 if (flag_isoc99)
3112 pedwarn_c99 (loc, OPT_Wpedantic,
3113 "ISO C99 does not support %<_Alignas%>");
3114 else
3115 pedwarn_c99 (loc, OPT_Wpedantic,
3116 "ISO C90 does not support %<_Alignas%>");
3117 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3118 return ret;
3119 if (c_parser_next_tokens_start_typename (parser, cla_prefer_id))
3120 {
3121 struct c_type_name *type = c_parser_type_name (parser);
3122 if (type != NULL)
3123 ret = c_sizeof_or_alignof_type (loc, groktypename (type, NULL, NULL),
3124 false, true, 1);
3125 }
3126 else
3127 ret = c_parser_expr_no_commas (parser, NULL).value;
3128 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
3129 return ret;
3130 }
3131
3132 /* Parse a declarator, possibly an abstract declarator (C90 6.5.4,
3133 6.5.5, C99 6.7.5, 6.7.6). If TYPE_SEEN_P then a typedef name may
3134 be redeclared; otherwise it may not. KIND indicates which kind of
3135 declarator is wanted. Returns a valid declarator except in the
3136 case of a syntax error in which case NULL is returned. *SEEN_ID is
3137 set to true if an identifier being declared is seen; this is used
3138 to diagnose bad forms of abstract array declarators and to
3139 determine whether an identifier list is syntactically permitted.
3140
3141 declarator:
3142 pointer[opt] direct-declarator
3143
3144 direct-declarator:
3145 identifier
3146 ( attributes[opt] declarator )
3147 direct-declarator array-declarator
3148 direct-declarator ( parameter-type-list )
3149 direct-declarator ( identifier-list[opt] )
3150
3151 pointer:
3152 * type-qualifier-list[opt]
3153 * type-qualifier-list[opt] pointer
3154
3155 type-qualifier-list:
3156 type-qualifier
3157 attributes
3158 type-qualifier-list type-qualifier
3159 type-qualifier-list attributes
3160
3161 array-declarator:
3162 [ type-qualifier-list[opt] assignment-expression[opt] ]
3163 [ static type-qualifier-list[opt] assignment-expression ]
3164 [ type-qualifier-list static assignment-expression ]
3165 [ type-qualifier-list[opt] * ]
3166
3167 parameter-type-list:
3168 parameter-list
3169 parameter-list , ...
3170
3171 parameter-list:
3172 parameter-declaration
3173 parameter-list , parameter-declaration
3174
3175 parameter-declaration:
3176 declaration-specifiers declarator attributes[opt]
3177 declaration-specifiers abstract-declarator[opt] attributes[opt]
3178
3179 identifier-list:
3180 identifier
3181 identifier-list , identifier
3182
3183 abstract-declarator:
3184 pointer
3185 pointer[opt] direct-abstract-declarator
3186
3187 direct-abstract-declarator:
3188 ( attributes[opt] abstract-declarator )
3189 direct-abstract-declarator[opt] array-declarator
3190 direct-abstract-declarator[opt] ( parameter-type-list[opt] )
3191
3192 GNU extensions:
3193
3194 direct-declarator:
3195 direct-declarator ( parameter-forward-declarations
3196 parameter-type-list[opt] )
3197
3198 direct-abstract-declarator:
3199 direct-abstract-declarator[opt] ( parameter-forward-declarations
3200 parameter-type-list[opt] )
3201
3202 parameter-forward-declarations:
3203 parameter-list ;
3204 parameter-forward-declarations parameter-list ;
3205
3206 The uses of attributes shown above are GNU extensions.
3207
3208 Some forms of array declarator are not included in C99 in the
3209 syntax for abstract declarators; these are disallowed elsewhere.
3210 This may be a defect (DR#289).
3211
3212 This function also accepts an omitted abstract declarator as being
3213 an abstract declarator, although not part of the formal syntax. */
3214
3215 static struct c_declarator *
3216 c_parser_declarator (c_parser *parser, bool type_seen_p, c_dtr_syn kind,
3217 bool *seen_id)
3218 {
3219 /* Parse any initial pointer part. */
3220 if (c_parser_next_token_is (parser, CPP_MULT))
3221 {
3222 struct c_declspecs *quals_attrs = build_null_declspecs ();
3223 struct c_declarator *inner;
3224 c_parser_consume_token (parser);
3225 c_parser_declspecs (parser, quals_attrs, false, false, true,
3226 false, false, cla_prefer_id);
3227 inner = c_parser_declarator (parser, type_seen_p, kind, seen_id);
3228 if (inner == NULL)
3229 return NULL;
3230 else
3231 return make_pointer_declarator (quals_attrs, inner);
3232 }
3233 /* Now we have a direct declarator, direct abstract declarator or
3234 nothing (which counts as a direct abstract declarator here). */
3235 return c_parser_direct_declarator (parser, type_seen_p, kind, seen_id);
3236 }
3237
3238 /* Parse a direct declarator or direct abstract declarator; arguments
3239 as c_parser_declarator. */
3240
3241 static struct c_declarator *
3242 c_parser_direct_declarator (c_parser *parser, bool type_seen_p, c_dtr_syn kind,
3243 bool *seen_id)
3244 {
3245 /* The direct declarator must start with an identifier (possibly
3246 omitted) or a parenthesized declarator (possibly abstract). In
3247 an ordinary declarator, initial parentheses must start a
3248 parenthesized declarator. In an abstract declarator or parameter
3249 declarator, they could start a parenthesized declarator or a
3250 parameter list. To tell which, the open parenthesis and any
3251 following attributes must be read. If a declaration specifier
3252 follows, then it is a parameter list; if the specifier is a
3253 typedef name, there might be an ambiguity about redeclaring it,
3254 which is resolved in the direction of treating it as a typedef
3255 name. If a close parenthesis follows, it is also an empty
3256 parameter list, as the syntax does not permit empty abstract
3257 declarators. Otherwise, it is a parenthesized declarator (in
3258 which case the analysis may be repeated inside it, recursively).
3259
3260 ??? There is an ambiguity in a parameter declaration "int
3261 (__attribute__((foo)) x)", where x is not a typedef name: it
3262 could be an abstract declarator for a function, or declare x with
3263 parentheses. The proper resolution of this ambiguity needs
3264 documenting. At present we follow an accident of the old
3265 parser's implementation, whereby the first parameter must have
3266 some declaration specifiers other than just attributes. Thus as
3267 a parameter declaration it is treated as a parenthesized
3268 parameter named x, and as an abstract declarator it is
3269 rejected.
3270
3271 ??? Also following the old parser, attributes inside an empty
3272 parameter list are ignored, making it a list not yielding a
3273 prototype, rather than giving an error or making it have one
3274 parameter with implicit type int.
3275
3276 ??? Also following the old parser, typedef names may be
3277 redeclared in declarators, but not Objective-C class names. */
3278
3279 if (kind != C_DTR_ABSTRACT
3280 && c_parser_next_token_is (parser, CPP_NAME)
3281 && ((type_seen_p
3282 && (c_parser_peek_token (parser)->id_kind == C_ID_TYPENAME
3283 || c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME))
3284 || c_parser_peek_token (parser)->id_kind == C_ID_ID))
3285 {
3286 struct c_declarator *inner
3287 = build_id_declarator (c_parser_peek_token (parser)->value);
3288 *seen_id = true;
3289 inner->id_loc = c_parser_peek_token (parser)->location;
3290 c_parser_consume_token (parser);
3291 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
3292 }
3293
3294 if (kind != C_DTR_NORMAL
3295 && c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
3296 {
3297 struct c_declarator *inner = build_id_declarator (NULL_TREE);
3298 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
3299 }
3300
3301 /* Either we are at the end of an abstract declarator, or we have
3302 parentheses. */
3303
3304 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
3305 {
3306 tree attrs;
3307 struct c_declarator *inner;
3308 c_parser_consume_token (parser);
3309 attrs = c_parser_attributes (parser);
3310 if (kind != C_DTR_NORMAL
3311 && (c_parser_next_token_starts_declspecs (parser)
3312 || c_parser_next_token_is (parser, CPP_CLOSE_PAREN)))
3313 {
3314 struct c_arg_info *args
3315 = c_parser_parms_declarator (parser, kind == C_DTR_NORMAL,
3316 attrs);
3317 if (args == NULL)
3318 return NULL;
3319 else
3320 {
3321 inner
3322 = build_function_declarator (args,
3323 build_id_declarator (NULL_TREE));
3324 return c_parser_direct_declarator_inner (parser, *seen_id,
3325 inner);
3326 }
3327 }
3328 /* A parenthesized declarator. */
3329 inner = c_parser_declarator (parser, type_seen_p, kind, seen_id);
3330 if (inner != NULL && attrs != NULL)
3331 inner = build_attrs_declarator (attrs, inner);
3332 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3333 {
3334 c_parser_consume_token (parser);
3335 if (inner == NULL)
3336 return NULL;
3337 else
3338 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
3339 }
3340 else
3341 {
3342 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3343 "expected %<)%>");
3344 return NULL;
3345 }
3346 }
3347 else
3348 {
3349 if (kind == C_DTR_NORMAL)
3350 {
3351 c_parser_error (parser, "expected identifier or %<(%>");
3352 return NULL;
3353 }
3354 else
3355 return build_id_declarator (NULL_TREE);
3356 }
3357 }
3358
3359 /* Parse part of a direct declarator or direct abstract declarator,
3360 given that some (in INNER) has already been parsed; ID_PRESENT is
3361 true if an identifier is present, false for an abstract
3362 declarator. */
3363
3364 static struct c_declarator *
3365 c_parser_direct_declarator_inner (c_parser *parser, bool id_present,
3366 struct c_declarator *inner)
3367 {
3368 /* Parse a sequence of array declarators and parameter lists. */
3369 if (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
3370 {
3371 location_t brace_loc = c_parser_peek_token (parser)->location;
3372 struct c_declarator *declarator;
3373 struct c_declspecs *quals_attrs = build_null_declspecs ();
3374 bool static_seen;
3375 bool star_seen;
3376 struct c_expr dimen;
3377 dimen.value = NULL_TREE;
3378 dimen.original_code = ERROR_MARK;
3379 dimen.original_type = NULL_TREE;
3380 c_parser_consume_token (parser);
3381 c_parser_declspecs (parser, quals_attrs, false, false, true,
3382 false, false, cla_prefer_id);
3383 static_seen = c_parser_next_token_is_keyword (parser, RID_STATIC);
3384 if (static_seen)
3385 c_parser_consume_token (parser);
3386 if (static_seen && !quals_attrs->declspecs_seen_p)
3387 c_parser_declspecs (parser, quals_attrs, false, false, true,
3388 false, false, cla_prefer_id);
3389 if (!quals_attrs->declspecs_seen_p)
3390 quals_attrs = NULL;
3391 /* If "static" is present, there must be an array dimension.
3392 Otherwise, there may be a dimension, "*", or no
3393 dimension. */
3394 if (static_seen)
3395 {
3396 star_seen = false;
3397 dimen = c_parser_expr_no_commas (parser, NULL);
3398 }
3399 else
3400 {
3401 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
3402 {
3403 dimen.value = NULL_TREE;
3404 star_seen = false;
3405 }
3406 else if (flag_cilkplus
3407 && c_parser_next_token_is (parser, CPP_COLON))
3408 {
3409 dimen.value = error_mark_node;
3410 star_seen = false;
3411 error_at (c_parser_peek_token (parser)->location,
3412 "array notations cannot be used in declaration");
3413 c_parser_consume_token (parser);
3414 }
3415 else if (c_parser_next_token_is (parser, CPP_MULT))
3416 {
3417 if (c_parser_peek_2nd_token (parser)->type == CPP_CLOSE_SQUARE)
3418 {
3419 dimen.value = NULL_TREE;
3420 star_seen = true;
3421 c_parser_consume_token (parser);
3422 }
3423 else
3424 {
3425 star_seen = false;
3426 dimen = c_parser_expr_no_commas (parser, NULL);
3427 }
3428 }
3429 else
3430 {
3431 star_seen = false;
3432 dimen = c_parser_expr_no_commas (parser, NULL);
3433 }
3434 }
3435 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
3436 c_parser_consume_token (parser);
3437 else if (flag_cilkplus
3438 && c_parser_next_token_is (parser, CPP_COLON))
3439 {
3440 error_at (c_parser_peek_token (parser)->location,
3441 "array notations cannot be used in declaration");
3442 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
3443 return NULL;
3444 }
3445 else
3446 {
3447 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
3448 "expected %<]%>");
3449 return NULL;
3450 }
3451 if (dimen.value)
3452 dimen = convert_lvalue_to_rvalue (brace_loc, dimen, true, true);
3453 declarator = build_array_declarator (brace_loc, dimen.value, quals_attrs,
3454 static_seen, star_seen);
3455 if (declarator == NULL)
3456 return NULL;
3457 inner = set_array_declarator_inner (declarator, inner);
3458 return c_parser_direct_declarator_inner (parser, id_present, inner);
3459 }
3460 else if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
3461 {
3462 tree attrs;
3463 struct c_arg_info *args;
3464 c_parser_consume_token (parser);
3465 attrs = c_parser_attributes (parser);
3466 args = c_parser_parms_declarator (parser, id_present, attrs);
3467 if (args == NULL)
3468 return NULL;
3469 else
3470 {
3471 inner = build_function_declarator (args, inner);
3472 return c_parser_direct_declarator_inner (parser, id_present, inner);
3473 }
3474 }
3475 return inner;
3476 }
3477
3478 /* Parse a parameter list or identifier list, including the closing
3479 parenthesis but not the opening one. ATTRS are the attributes at
3480 the start of the list. ID_LIST_OK is true if an identifier list is
3481 acceptable; such a list must not have attributes at the start. */
3482
3483 static struct c_arg_info *
3484 c_parser_parms_declarator (c_parser *parser, bool id_list_ok, tree attrs)
3485 {
3486 push_scope ();
3487 declare_parm_level ();
3488 /* If the list starts with an identifier, it is an identifier list.
3489 Otherwise, it is either a prototype list or an empty list. */
3490 if (id_list_ok
3491 && !attrs
3492 && c_parser_next_token_is (parser, CPP_NAME)
3493 && c_parser_peek_token (parser)->id_kind == C_ID_ID
3494
3495 /* Look ahead to detect typos in type names. */
3496 && c_parser_peek_2nd_token (parser)->type != CPP_NAME
3497 && c_parser_peek_2nd_token (parser)->type != CPP_MULT
3498 && c_parser_peek_2nd_token (parser)->type != CPP_OPEN_PAREN
3499 && c_parser_peek_2nd_token (parser)->type != CPP_OPEN_SQUARE)
3500 {
3501 tree list = NULL_TREE, *nextp = &list;
3502 while (c_parser_next_token_is (parser, CPP_NAME)
3503 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
3504 {
3505 *nextp = build_tree_list (NULL_TREE,
3506 c_parser_peek_token (parser)->value);
3507 nextp = & TREE_CHAIN (*nextp);
3508 c_parser_consume_token (parser);
3509 if (c_parser_next_token_is_not (parser, CPP_COMMA))
3510 break;
3511 c_parser_consume_token (parser);
3512 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3513 {
3514 c_parser_error (parser, "expected identifier");
3515 break;
3516 }
3517 }
3518 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3519 {
3520 struct c_arg_info *ret = build_arg_info ();
3521 ret->types = list;
3522 c_parser_consume_token (parser);
3523 pop_scope ();
3524 return ret;
3525 }
3526 else
3527 {
3528 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3529 "expected %<)%>");
3530 pop_scope ();
3531 return NULL;
3532 }
3533 }
3534 else
3535 {
3536 struct c_arg_info *ret = c_parser_parms_list_declarator (parser, attrs,
3537 NULL);
3538 pop_scope ();
3539 return ret;
3540 }
3541 }
3542
3543 /* Parse a parameter list (possibly empty), including the closing
3544 parenthesis but not the opening one. ATTRS are the attributes at
3545 the start of the list. EXPR is NULL or an expression that needs to
3546 be evaluated for the side effects of array size expressions in the
3547 parameters. */
3548
3549 static struct c_arg_info *
3550 c_parser_parms_list_declarator (c_parser *parser, tree attrs, tree expr)
3551 {
3552 bool bad_parm = false;
3553
3554 /* ??? Following the old parser, forward parameter declarations may
3555 use abstract declarators, and if no real parameter declarations
3556 follow the forward declarations then this is not diagnosed. Also
3557 note as above that attributes are ignored as the only contents of
3558 the parentheses, or as the only contents after forward
3559 declarations. */
3560 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3561 {
3562 struct c_arg_info *ret = build_arg_info ();
3563 c_parser_consume_token (parser);
3564 return ret;
3565 }
3566 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
3567 {
3568 struct c_arg_info *ret = build_arg_info ();
3569
3570 if (flag_allow_parameterless_variadic_functions)
3571 {
3572 /* F (...) is allowed. */
3573 ret->types = NULL_TREE;
3574 }
3575 else
3576 {
3577 /* Suppress -Wold-style-definition for this case. */
3578 ret->types = error_mark_node;
3579 error_at (c_parser_peek_token (parser)->location,
3580 "ISO C requires a named argument before %<...%>");
3581 }
3582 c_parser_consume_token (parser);
3583 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3584 {
3585 c_parser_consume_token (parser);
3586 return ret;
3587 }
3588 else
3589 {
3590 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3591 "expected %<)%>");
3592 return NULL;
3593 }
3594 }
3595 /* Nonempty list of parameters, either terminated with semicolon
3596 (forward declarations; recurse) or with close parenthesis (normal
3597 function) or with ", ... )" (variadic function). */
3598 while (true)
3599 {
3600 /* Parse a parameter. */
3601 struct c_parm *parm = c_parser_parameter_declaration (parser, attrs);
3602 attrs = NULL_TREE;
3603 if (parm == NULL)
3604 bad_parm = true;
3605 else
3606 push_parm_decl (parm, &expr);
3607 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
3608 {
3609 tree new_attrs;
3610 c_parser_consume_token (parser);
3611 mark_forward_parm_decls ();
3612 new_attrs = c_parser_attributes (parser);
3613 return c_parser_parms_list_declarator (parser, new_attrs, expr);
3614 }
3615 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3616 {
3617 c_parser_consume_token (parser);
3618 if (bad_parm)
3619 return NULL;
3620 else
3621 return get_parm_info (false, expr);
3622 }
3623 if (!c_parser_require (parser, CPP_COMMA,
3624 "expected %<;%>, %<,%> or %<)%>"))
3625 {
3626 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
3627 return NULL;
3628 }
3629 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
3630 {
3631 c_parser_consume_token (parser);
3632 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3633 {
3634 c_parser_consume_token (parser);
3635 if (bad_parm)
3636 return NULL;
3637 else
3638 return get_parm_info (true, expr);
3639 }
3640 else
3641 {
3642 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3643 "expected %<)%>");
3644 return NULL;
3645 }
3646 }
3647 }
3648 }
3649
3650 /* Parse a parameter declaration. ATTRS are the attributes at the
3651 start of the declaration if it is the first parameter. */
3652
3653 static struct c_parm *
3654 c_parser_parameter_declaration (c_parser *parser, tree attrs)
3655 {
3656 struct c_declspecs *specs;
3657 struct c_declarator *declarator;
3658 tree prefix_attrs;
3659 tree postfix_attrs = NULL_TREE;
3660 bool dummy = false;
3661
3662 /* Accept #pragmas between parameter declarations. */
3663 while (c_parser_next_token_is (parser, CPP_PRAGMA))
3664 c_parser_pragma (parser, pragma_param);
3665
3666 if (!c_parser_next_token_starts_declspecs (parser))
3667 {
3668 c_token *token = c_parser_peek_token (parser);
3669 if (parser->error)
3670 return NULL;
3671 c_parser_set_source_position_from_token (token);
3672 if (c_parser_next_tokens_start_typename (parser, cla_prefer_type))
3673 {
3674 error_at (token->location, "unknown type name %qE", token->value);
3675 parser->error = true;
3676 }
3677 /* ??? In some Objective-C cases '...' isn't applicable so there
3678 should be a different message. */
3679 else
3680 c_parser_error (parser,
3681 "expected declaration specifiers or %<...%>");
3682 c_parser_skip_to_end_of_parameter (parser);
3683 return NULL;
3684 }
3685 specs = build_null_declspecs ();
3686 if (attrs)
3687 {
3688 declspecs_add_attrs (input_location, specs, attrs);
3689 attrs = NULL_TREE;
3690 }
3691 c_parser_declspecs (parser, specs, true, true, true, true, false,
3692 cla_nonabstract_decl);
3693 finish_declspecs (specs);
3694 pending_xref_error ();
3695 prefix_attrs = specs->attrs;
3696 specs->attrs = NULL_TREE;
3697 declarator = c_parser_declarator (parser,
3698 specs->typespec_kind != ctsk_none,
3699 C_DTR_PARM, &dummy);
3700 if (declarator == NULL)
3701 {
3702 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
3703 return NULL;
3704 }
3705 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
3706 postfix_attrs = c_parser_attributes (parser);
3707 return build_c_parm (specs, chainon (postfix_attrs, prefix_attrs),
3708 declarator);
3709 }
3710
3711 /* Parse a string literal in an asm expression. It should not be
3712 translated, and wide string literals are an error although
3713 permitted by the syntax. This is a GNU extension.
3714
3715 asm-string-literal:
3716 string-literal
3717
3718 ??? At present, following the old parser, the caller needs to have
3719 set lex_untranslated_string to 1. It would be better to follow the
3720 C++ parser rather than using this kludge. */
3721
3722 static tree
3723 c_parser_asm_string_literal (c_parser *parser)
3724 {
3725 tree str;
3726 int save_flag = warn_overlength_strings;
3727 warn_overlength_strings = 0;
3728 if (c_parser_next_token_is (parser, CPP_STRING))
3729 {
3730 str = c_parser_peek_token (parser)->value;
3731 c_parser_consume_token (parser);
3732 }
3733 else if (c_parser_next_token_is (parser, CPP_WSTRING))
3734 {
3735 error_at (c_parser_peek_token (parser)->location,
3736 "wide string literal in %<asm%>");
3737 str = build_string (1, "");
3738 c_parser_consume_token (parser);
3739 }
3740 else
3741 {
3742 c_parser_error (parser, "expected string literal");
3743 str = NULL_TREE;
3744 }
3745 warn_overlength_strings = save_flag;
3746 return str;
3747 }
3748
3749 /* Parse a simple asm expression. This is used in restricted
3750 contexts, where a full expression with inputs and outputs does not
3751 make sense. This is a GNU extension.
3752
3753 simple-asm-expr:
3754 asm ( asm-string-literal )
3755 */
3756
3757 static tree
3758 c_parser_simple_asm_expr (c_parser *parser)
3759 {
3760 tree str;
3761 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ASM));
3762 /* ??? Follow the C++ parser rather than using the
3763 lex_untranslated_string kludge. */
3764 parser->lex_untranslated_string = true;
3765 c_parser_consume_token (parser);
3766 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3767 {
3768 parser->lex_untranslated_string = false;
3769 return NULL_TREE;
3770 }
3771 str = c_parser_asm_string_literal (parser);
3772 parser->lex_untranslated_string = false;
3773 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
3774 {
3775 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
3776 return NULL_TREE;
3777 }
3778 return str;
3779 }
3780
3781 static tree
3782 c_parser_attribute_any_word (c_parser *parser)
3783 {
3784 tree attr_name = NULL_TREE;
3785
3786 if (c_parser_next_token_is (parser, CPP_KEYWORD))
3787 {
3788 /* ??? See comment above about what keywords are accepted here. */
3789 bool ok;
3790 switch (c_parser_peek_token (parser)->keyword)
3791 {
3792 case RID_STATIC:
3793 case RID_UNSIGNED:
3794 case RID_LONG:
3795 case RID_CONST:
3796 case RID_EXTERN:
3797 case RID_REGISTER:
3798 case RID_TYPEDEF:
3799 case RID_SHORT:
3800 case RID_INLINE:
3801 case RID_NORETURN:
3802 case RID_VOLATILE:
3803 case RID_SIGNED:
3804 case RID_AUTO:
3805 case RID_RESTRICT:
3806 case RID_COMPLEX:
3807 case RID_THREAD:
3808 case RID_INT:
3809 case RID_CHAR:
3810 case RID_FLOAT:
3811 case RID_DOUBLE:
3812 case RID_VOID:
3813 case RID_DFLOAT32:
3814 case RID_DFLOAT64:
3815 case RID_DFLOAT128:
3816 case RID_BOOL:
3817 case RID_FRACT:
3818 case RID_ACCUM:
3819 case RID_SAT:
3820 case RID_TRANSACTION_ATOMIC:
3821 case RID_TRANSACTION_CANCEL:
3822 case RID_ATOMIC:
3823 case RID_AUTO_TYPE:
3824 case RID_INT_N_0:
3825 case RID_INT_N_1:
3826 case RID_INT_N_2:
3827 case RID_INT_N_3:
3828 ok = true;
3829 break;
3830 default:
3831 ok = false;
3832 break;
3833 }
3834 if (!ok)
3835 return NULL_TREE;
3836
3837 /* Accept __attribute__((__const)) as __attribute__((const)) etc. */
3838 attr_name = ridpointers[(int) c_parser_peek_token (parser)->keyword];
3839 }
3840 else if (c_parser_next_token_is (parser, CPP_NAME))
3841 attr_name = c_parser_peek_token (parser)->value;
3842
3843 return attr_name;
3844 }
3845
3846 #define CILK_SIMD_FN_CLAUSE_MASK \
3847 ((OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_VECTORLENGTH) \
3848 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_LINEAR) \
3849 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_UNIFORM) \
3850 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_MASK) \
3851 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_NOMASK))
3852
3853 /* Parses the vector attribute of SIMD enabled functions in Cilk Plus.
3854 VEC_TOKEN is the "vector" token that is replaced with "simd" and
3855 pushed into the token list.
3856 Syntax:
3857 vector
3858 vector (<vector attributes>). */
3859
3860 static void
3861 c_parser_cilk_simd_fn_vector_attrs (c_parser *parser, c_token vec_token)
3862 {
3863 gcc_assert (is_cilkplus_vector_p (vec_token.value));
3864
3865 int paren_scope = 0;
3866 vec_safe_push (parser->cilk_simd_fn_tokens, vec_token);
3867 /* Consume the "vector" token. */
3868 c_parser_consume_token (parser);
3869
3870 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
3871 {
3872 c_parser_consume_token (parser);
3873 paren_scope++;
3874 }
3875 while (paren_scope > 0)
3876 {
3877 c_token *token = c_parser_peek_token (parser);
3878 if (token->type == CPP_OPEN_PAREN)
3879 paren_scope++;
3880 else if (token->type == CPP_CLOSE_PAREN)
3881 paren_scope--;
3882 /* Do not push the last ')' since we are not pushing the '('. */
3883 if (!(token->type == CPP_CLOSE_PAREN && paren_scope == 0))
3884 vec_safe_push (parser->cilk_simd_fn_tokens, *token);
3885 c_parser_consume_token (parser);
3886 }
3887
3888 /* Since we are converting an attribute to a pragma, we need to end the
3889 attribute with PRAGMA_EOL. */
3890 c_token eol_token;
3891 memset (&eol_token, 0, sizeof (eol_token));
3892 eol_token.type = CPP_PRAGMA_EOL;
3893 vec_safe_push (parser->cilk_simd_fn_tokens, eol_token);
3894 }
3895
3896 /* Add 2 CPP_EOF at the end of PARSER->ELEM_FN_TOKENS vector. */
3897
3898 static void
3899 c_finish_cilk_simd_fn_tokens (c_parser *parser)
3900 {
3901 c_token last_token = parser->cilk_simd_fn_tokens->last ();
3902
3903 /* c_parser_attributes is called in several places, so if these EOF
3904 tokens are already inserted, then don't do them again. */
3905 if (last_token.type == CPP_EOF)
3906 return;
3907
3908 /* Two CPP_EOF token are added as a safety net since the normal C
3909 front-end has two token look-ahead. */
3910 c_token eof_token;
3911 eof_token.type = CPP_EOF;
3912 vec_safe_push (parser->cilk_simd_fn_tokens, eof_token);
3913 vec_safe_push (parser->cilk_simd_fn_tokens, eof_token);
3914 }
3915
3916 /* Parse (possibly empty) attributes. This is a GNU extension.
3917
3918 attributes:
3919 empty
3920 attributes attribute
3921
3922 attribute:
3923 __attribute__ ( ( attribute-list ) )
3924
3925 attribute-list:
3926 attrib
3927 attribute_list , attrib
3928
3929 attrib:
3930 empty
3931 any-word
3932 any-word ( identifier )
3933 any-word ( identifier , nonempty-expr-list )
3934 any-word ( expr-list )
3935
3936 where the "identifier" must not be declared as a type, and
3937 "any-word" may be any identifier (including one declared as a
3938 type), a reserved word storage class specifier, type specifier or
3939 type qualifier. ??? This still leaves out most reserved keywords
3940 (following the old parser), shouldn't we include them, and why not
3941 allow identifiers declared as types to start the arguments? */
3942
3943 static tree
3944 c_parser_attributes (c_parser *parser)
3945 {
3946 tree attrs = NULL_TREE;
3947 while (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
3948 {
3949 /* ??? Follow the C++ parser rather than using the
3950 lex_untranslated_string kludge. */
3951 parser->lex_untranslated_string = true;
3952 /* Consume the `__attribute__' keyword. */
3953 c_parser_consume_token (parser);
3954 /* Look for the two `(' tokens. */
3955 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3956 {
3957 parser->lex_untranslated_string = false;
3958 return attrs;
3959 }
3960 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3961 {
3962 parser->lex_untranslated_string = false;
3963 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
3964 return attrs;
3965 }
3966 /* Parse the attribute list. */
3967 while (c_parser_next_token_is (parser, CPP_COMMA)
3968 || c_parser_next_token_is (parser, CPP_NAME)
3969 || c_parser_next_token_is (parser, CPP_KEYWORD))
3970 {
3971 tree attr, attr_name, attr_args;
3972 vec<tree, va_gc> *expr_list;
3973 if (c_parser_next_token_is (parser, CPP_COMMA))
3974 {
3975 c_parser_consume_token (parser);
3976 continue;
3977 }
3978
3979 attr_name = c_parser_attribute_any_word (parser);
3980 if (attr_name == NULL)
3981 break;
3982 if (is_cilkplus_vector_p (attr_name))
3983 {
3984 c_token *v_token = c_parser_peek_token (parser);
3985 c_parser_cilk_simd_fn_vector_attrs (parser, *v_token);
3986 /* If the next token isn't a comma, we're done. */
3987 if (!c_parser_next_token_is (parser, CPP_COMMA))
3988 break;
3989 continue;
3990 }
3991 c_parser_consume_token (parser);
3992 if (c_parser_next_token_is_not (parser, CPP_OPEN_PAREN))
3993 {
3994 attr = build_tree_list (attr_name, NULL_TREE);
3995 /* Add this attribute to the list. */
3996 attrs = chainon (attrs, attr);
3997 /* If the next token isn't a comma, we're done. */
3998 if (!c_parser_next_token_is (parser, CPP_COMMA))
3999 break;
4000 continue;
4001 }
4002 c_parser_consume_token (parser);
4003 /* Parse the attribute contents. If they start with an
4004 identifier which is followed by a comma or close
4005 parenthesis, then the arguments start with that
4006 identifier; otherwise they are an expression list.
4007 In objective-c the identifier may be a classname. */
4008 if (c_parser_next_token_is (parser, CPP_NAME)
4009 && (c_parser_peek_token (parser)->id_kind == C_ID_ID
4010 || (c_dialect_objc ()
4011 && c_parser_peek_token (parser)->id_kind
4012 == C_ID_CLASSNAME))
4013 && ((c_parser_peek_2nd_token (parser)->type == CPP_COMMA)
4014 || (c_parser_peek_2nd_token (parser)->type
4015 == CPP_CLOSE_PAREN))
4016 && (attribute_takes_identifier_p (attr_name)
4017 || (c_dialect_objc ()
4018 && c_parser_peek_token (parser)->id_kind
4019 == C_ID_CLASSNAME)))
4020 {
4021 tree arg1 = c_parser_peek_token (parser)->value;
4022 c_parser_consume_token (parser);
4023 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4024 attr_args = build_tree_list (NULL_TREE, arg1);
4025 else
4026 {
4027 tree tree_list;
4028 c_parser_consume_token (parser);
4029 expr_list = c_parser_expr_list (parser, false, true,
4030 NULL, NULL, NULL, NULL);
4031 tree_list = build_tree_list_vec (expr_list);
4032 attr_args = tree_cons (NULL_TREE, arg1, tree_list);
4033 release_tree_vector (expr_list);
4034 }
4035 }
4036 else
4037 {
4038 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4039 attr_args = NULL_TREE;
4040 else
4041 {
4042 expr_list = c_parser_expr_list (parser, false, true,
4043 NULL, NULL, NULL, NULL);
4044 attr_args = build_tree_list_vec (expr_list);
4045 release_tree_vector (expr_list);
4046 }
4047 }
4048 attr = build_tree_list (attr_name, attr_args);
4049 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4050 c_parser_consume_token (parser);
4051 else
4052 {
4053 parser->lex_untranslated_string = false;
4054 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
4055 "expected %<)%>");
4056 return attrs;
4057 }
4058 /* Add this attribute to the list. */
4059 attrs = chainon (attrs, attr);
4060 /* If the next token isn't a comma, we're done. */
4061 if (!c_parser_next_token_is (parser, CPP_COMMA))
4062 break;
4063 }
4064 /* Look for the two `)' tokens. */
4065 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4066 c_parser_consume_token (parser);
4067 else
4068 {
4069 parser->lex_untranslated_string = false;
4070 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
4071 "expected %<)%>");
4072 return attrs;
4073 }
4074 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4075 c_parser_consume_token (parser);
4076 else
4077 {
4078 parser->lex_untranslated_string = false;
4079 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
4080 "expected %<)%>");
4081 return attrs;
4082 }
4083 parser->lex_untranslated_string = false;
4084 }
4085
4086 if (flag_cilkplus && !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
4087 c_finish_cilk_simd_fn_tokens (parser);
4088 return attrs;
4089 }
4090
4091 /* Parse a type name (C90 6.5.5, C99 6.7.6).
4092
4093 type-name:
4094 specifier-qualifier-list abstract-declarator[opt]
4095 */
4096
4097 static struct c_type_name *
4098 c_parser_type_name (c_parser *parser)
4099 {
4100 struct c_declspecs *specs = build_null_declspecs ();
4101 struct c_declarator *declarator;
4102 struct c_type_name *ret;
4103 bool dummy = false;
4104 c_parser_declspecs (parser, specs, false, true, true, false, false,
4105 cla_prefer_type);
4106 if (!specs->declspecs_seen_p)
4107 {
4108 c_parser_error (parser, "expected specifier-qualifier-list");
4109 return NULL;
4110 }
4111 if (specs->type != error_mark_node)
4112 {
4113 pending_xref_error ();
4114 finish_declspecs (specs);
4115 }
4116 declarator = c_parser_declarator (parser,
4117 specs->typespec_kind != ctsk_none,
4118 C_DTR_ABSTRACT, &dummy);
4119 if (declarator == NULL)
4120 return NULL;
4121 ret = XOBNEW (&parser_obstack, struct c_type_name);
4122 ret->specs = specs;
4123 ret->declarator = declarator;
4124 return ret;
4125 }
4126
4127 /* Parse an initializer (C90 6.5.7, C99 6.7.8).
4128
4129 initializer:
4130 assignment-expression
4131 { initializer-list }
4132 { initializer-list , }
4133
4134 initializer-list:
4135 designation[opt] initializer
4136 initializer-list , designation[opt] initializer
4137
4138 designation:
4139 designator-list =
4140
4141 designator-list:
4142 designator
4143 designator-list designator
4144
4145 designator:
4146 array-designator
4147 . identifier
4148
4149 array-designator:
4150 [ constant-expression ]
4151
4152 GNU extensions:
4153
4154 initializer:
4155 { }
4156
4157 designation:
4158 array-designator
4159 identifier :
4160
4161 array-designator:
4162 [ constant-expression ... constant-expression ]
4163
4164 Any expression without commas is accepted in the syntax for the
4165 constant-expressions, with non-constant expressions rejected later.
4166
4167 This function is only used for top-level initializers; for nested
4168 ones, see c_parser_initval. */
4169
4170 static struct c_expr
4171 c_parser_initializer (c_parser *parser)
4172 {
4173 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
4174 return c_parser_braced_init (parser, NULL_TREE, false);
4175 else
4176 {
4177 struct c_expr ret;
4178 location_t loc = c_parser_peek_token (parser)->location;
4179 ret = c_parser_expr_no_commas (parser, NULL);
4180 if (TREE_CODE (ret.value) != STRING_CST
4181 && TREE_CODE (ret.value) != COMPOUND_LITERAL_EXPR)
4182 ret = convert_lvalue_to_rvalue (loc, ret, true, true);
4183 return ret;
4184 }
4185 }
4186
4187 /* Parse a braced initializer list. TYPE is the type specified for a
4188 compound literal, and NULL_TREE for other initializers and for
4189 nested braced lists. NESTED_P is true for nested braced lists,
4190 false for the list of a compound literal or the list that is the
4191 top-level initializer in a declaration. */
4192
4193 static struct c_expr
4194 c_parser_braced_init (c_parser *parser, tree type, bool nested_p)
4195 {
4196 struct c_expr ret;
4197 struct obstack braced_init_obstack;
4198 location_t brace_loc = c_parser_peek_token (parser)->location;
4199 gcc_obstack_init (&braced_init_obstack);
4200 gcc_assert (c_parser_next_token_is (parser, CPP_OPEN_BRACE));
4201 c_parser_consume_token (parser);
4202 if (nested_p)
4203 push_init_level (brace_loc, 0, &braced_init_obstack);
4204 else
4205 really_start_incremental_init (type);
4206 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
4207 {
4208 pedwarn (brace_loc, OPT_Wpedantic, "ISO C forbids empty initializer braces");
4209 }
4210 else
4211 {
4212 /* Parse a non-empty initializer list, possibly with a trailing
4213 comma. */
4214 while (true)
4215 {
4216 c_parser_initelt (parser, &braced_init_obstack);
4217 if (parser->error)
4218 break;
4219 if (c_parser_next_token_is (parser, CPP_COMMA))
4220 c_parser_consume_token (parser);
4221 else
4222 break;
4223 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
4224 break;
4225 }
4226 }
4227 if (c_parser_next_token_is_not (parser, CPP_CLOSE_BRACE))
4228 {
4229 ret.value = error_mark_node;
4230 ret.original_code = ERROR_MARK;
4231 ret.original_type = NULL;
4232 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, "expected %<}%>");
4233 pop_init_level (brace_loc, 0, &braced_init_obstack);
4234 obstack_free (&braced_init_obstack, NULL);
4235 return ret;
4236 }
4237 c_parser_consume_token (parser);
4238 ret = pop_init_level (brace_loc, 0, &braced_init_obstack);
4239 obstack_free (&braced_init_obstack, NULL);
4240 return ret;
4241 }
4242
4243 /* Parse a nested initializer, including designators. */
4244
4245 static void
4246 c_parser_initelt (c_parser *parser, struct obstack * braced_init_obstack)
4247 {
4248 /* Parse any designator or designator list. A single array
4249 designator may have the subsequent "=" omitted in GNU C, but a
4250 longer list or a structure member designator may not. */
4251 if (c_parser_next_token_is (parser, CPP_NAME)
4252 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
4253 {
4254 /* Old-style structure member designator. */
4255 set_init_label (c_parser_peek_token (parser)->location,
4256 c_parser_peek_token (parser)->value,
4257 braced_init_obstack);
4258 /* Use the colon as the error location. */
4259 pedwarn (c_parser_peek_2nd_token (parser)->location, OPT_Wpedantic,
4260 "obsolete use of designated initializer with %<:%>");
4261 c_parser_consume_token (parser);
4262 c_parser_consume_token (parser);
4263 }
4264 else
4265 {
4266 /* des_seen is 0 if there have been no designators, 1 if there
4267 has been a single array designator and 2 otherwise. */
4268 int des_seen = 0;
4269 /* Location of a designator. */
4270 location_t des_loc = UNKNOWN_LOCATION; /* Quiet warning. */
4271 while (c_parser_next_token_is (parser, CPP_OPEN_SQUARE)
4272 || c_parser_next_token_is (parser, CPP_DOT))
4273 {
4274 int des_prev = des_seen;
4275 if (!des_seen)
4276 des_loc = c_parser_peek_token (parser)->location;
4277 if (des_seen < 2)
4278 des_seen++;
4279 if (c_parser_next_token_is (parser, CPP_DOT))
4280 {
4281 des_seen = 2;
4282 c_parser_consume_token (parser);
4283 if (c_parser_next_token_is (parser, CPP_NAME))
4284 {
4285 set_init_label (des_loc, c_parser_peek_token (parser)->value,
4286 braced_init_obstack);
4287 c_parser_consume_token (parser);
4288 }
4289 else
4290 {
4291 struct c_expr init;
4292 init.value = error_mark_node;
4293 init.original_code = ERROR_MARK;
4294 init.original_type = NULL;
4295 c_parser_error (parser, "expected identifier");
4296 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
4297 process_init_element (input_location, init, false,
4298 braced_init_obstack);
4299 return;
4300 }
4301 }
4302 else
4303 {
4304 tree first, second;
4305 location_t ellipsis_loc = UNKNOWN_LOCATION; /* Quiet warning. */
4306 location_t array_index_loc = UNKNOWN_LOCATION;
4307 /* ??? Following the old parser, [ objc-receiver
4308 objc-message-args ] is accepted as an initializer,
4309 being distinguished from a designator by what follows
4310 the first assignment expression inside the square
4311 brackets, but after a first array designator a
4312 subsequent square bracket is for Objective-C taken to
4313 start an expression, using the obsolete form of
4314 designated initializer without '=', rather than
4315 possibly being a second level of designation: in LALR
4316 terms, the '[' is shifted rather than reducing
4317 designator to designator-list. */
4318 if (des_prev == 1 && c_dialect_objc ())
4319 {
4320 des_seen = des_prev;
4321 break;
4322 }
4323 if (des_prev == 0 && c_dialect_objc ())
4324 {
4325 /* This might be an array designator or an
4326 Objective-C message expression. If the former,
4327 continue parsing here; if the latter, parse the
4328 remainder of the initializer given the starting
4329 primary-expression. ??? It might make sense to
4330 distinguish when des_prev == 1 as well; see
4331 previous comment. */
4332 tree rec, args;
4333 struct c_expr mexpr;
4334 c_parser_consume_token (parser);
4335 if (c_parser_peek_token (parser)->type == CPP_NAME
4336 && ((c_parser_peek_token (parser)->id_kind
4337 == C_ID_TYPENAME)
4338 || (c_parser_peek_token (parser)->id_kind
4339 == C_ID_CLASSNAME)))
4340 {
4341 /* Type name receiver. */
4342 tree id = c_parser_peek_token (parser)->value;
4343 c_parser_consume_token (parser);
4344 rec = objc_get_class_reference (id);
4345 goto parse_message_args;
4346 }
4347 first = c_parser_expr_no_commas (parser, NULL).value;
4348 mark_exp_read (first);
4349 if (c_parser_next_token_is (parser, CPP_ELLIPSIS)
4350 || c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
4351 goto array_desig_after_first;
4352 /* Expression receiver. So far only one part
4353 without commas has been parsed; there might be
4354 more of the expression. */
4355 rec = first;
4356 while (c_parser_next_token_is (parser, CPP_COMMA))
4357 {
4358 struct c_expr next;
4359 location_t comma_loc, exp_loc;
4360 comma_loc = c_parser_peek_token (parser)->location;
4361 c_parser_consume_token (parser);
4362 exp_loc = c_parser_peek_token (parser)->location;
4363 next = c_parser_expr_no_commas (parser, NULL);
4364 next = convert_lvalue_to_rvalue (exp_loc, next,
4365 true, true);
4366 rec = build_compound_expr (comma_loc, rec, next.value);
4367 }
4368 parse_message_args:
4369 /* Now parse the objc-message-args. */
4370 args = c_parser_objc_message_args (parser);
4371 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
4372 "expected %<]%>");
4373 mexpr.value
4374 = objc_build_message_expr (rec, args);
4375 mexpr.original_code = ERROR_MARK;
4376 mexpr.original_type = NULL;
4377 /* Now parse and process the remainder of the
4378 initializer, starting with this message
4379 expression as a primary-expression. */
4380 c_parser_initval (parser, &mexpr, braced_init_obstack);
4381 return;
4382 }
4383 c_parser_consume_token (parser);
4384 array_index_loc = c_parser_peek_token (parser)->location;
4385 first = c_parser_expr_no_commas (parser, NULL).value;
4386 mark_exp_read (first);
4387 array_desig_after_first:
4388 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
4389 {
4390 ellipsis_loc = c_parser_peek_token (parser)->location;
4391 c_parser_consume_token (parser);
4392 second = c_parser_expr_no_commas (parser, NULL).value;
4393 mark_exp_read (second);
4394 }
4395 else
4396 second = NULL_TREE;
4397 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
4398 {
4399 c_parser_consume_token (parser);
4400 set_init_index (array_index_loc, first, second,
4401 braced_init_obstack);
4402 if (second)
4403 pedwarn (ellipsis_loc, OPT_Wpedantic,
4404 "ISO C forbids specifying range of elements to initialize");
4405 }
4406 else
4407 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
4408 "expected %<]%>");
4409 }
4410 }
4411 if (des_seen >= 1)
4412 {
4413 if (c_parser_next_token_is (parser, CPP_EQ))
4414 {
4415 pedwarn_c90 (des_loc, OPT_Wpedantic,
4416 "ISO C90 forbids specifying subobject "
4417 "to initialize");
4418 c_parser_consume_token (parser);
4419 }
4420 else
4421 {
4422 if (des_seen == 1)
4423 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
4424 "obsolete use of designated initializer without %<=%>");
4425 else
4426 {
4427 struct c_expr init;
4428 init.value = error_mark_node;
4429 init.original_code = ERROR_MARK;
4430 init.original_type = NULL;
4431 c_parser_error (parser, "expected %<=%>");
4432 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
4433 process_init_element (input_location, init, false,
4434 braced_init_obstack);
4435 return;
4436 }
4437 }
4438 }
4439 }
4440 c_parser_initval (parser, NULL, braced_init_obstack);
4441 }
4442
4443 /* Parse a nested initializer; as c_parser_initializer but parses
4444 initializers within braced lists, after any designators have been
4445 applied. If AFTER is not NULL then it is an Objective-C message
4446 expression which is the primary-expression starting the
4447 initializer. */
4448
4449 static void
4450 c_parser_initval (c_parser *parser, struct c_expr *after,
4451 struct obstack * braced_init_obstack)
4452 {
4453 struct c_expr init;
4454 gcc_assert (!after || c_dialect_objc ());
4455 location_t loc = c_parser_peek_token (parser)->location;
4456
4457 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE) && !after)
4458 init = c_parser_braced_init (parser, NULL_TREE, true);
4459 else
4460 {
4461 init = c_parser_expr_no_commas (parser, after);
4462 if (init.value != NULL_TREE
4463 && TREE_CODE (init.value) != STRING_CST
4464 && TREE_CODE (init.value) != COMPOUND_LITERAL_EXPR)
4465 init = convert_lvalue_to_rvalue (loc, init, true, true);
4466 }
4467 process_init_element (loc, init, false, braced_init_obstack);
4468 }
4469
4470 /* Parse a compound statement (possibly a function body) (C90 6.6.2,
4471 C99 6.8.2).
4472
4473 compound-statement:
4474 { block-item-list[opt] }
4475 { label-declarations block-item-list }
4476
4477 block-item-list:
4478 block-item
4479 block-item-list block-item
4480
4481 block-item:
4482 nested-declaration
4483 statement
4484
4485 nested-declaration:
4486 declaration
4487
4488 GNU extensions:
4489
4490 compound-statement:
4491 { label-declarations block-item-list }
4492
4493 nested-declaration:
4494 __extension__ nested-declaration
4495 nested-function-definition
4496
4497 label-declarations:
4498 label-declaration
4499 label-declarations label-declaration
4500
4501 label-declaration:
4502 __label__ identifier-list ;
4503
4504 Allowing the mixing of declarations and code is new in C99. The
4505 GNU syntax also permits (not shown above) labels at the end of
4506 compound statements, which yield an error. We don't allow labels
4507 on declarations; this might seem like a natural extension, but
4508 there would be a conflict between attributes on the label and
4509 prefix attributes on the declaration. ??? The syntax follows the
4510 old parser in requiring something after label declarations.
4511 Although they are erroneous if the labels declared aren't defined,
4512 is it useful for the syntax to be this way?
4513
4514 OpenACC:
4515
4516 block-item:
4517 openacc-directive
4518
4519 openacc-directive:
4520 update-directive
4521
4522 OpenMP:
4523
4524 block-item:
4525 openmp-directive
4526
4527 openmp-directive:
4528 barrier-directive
4529 flush-directive
4530 taskwait-directive
4531 taskyield-directive
4532 cancel-directive
4533 cancellation-point-directive */
4534
4535 static tree
4536 c_parser_compound_statement (c_parser *parser)
4537 {
4538 tree stmt;
4539 location_t brace_loc;
4540 brace_loc = c_parser_peek_token (parser)->location;
4541 if (!c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
4542 {
4543 /* Ensure a scope is entered and left anyway to avoid confusion
4544 if we have just prepared to enter a function body. */
4545 stmt = c_begin_compound_stmt (true);
4546 c_end_compound_stmt (brace_loc, stmt, true);
4547 return error_mark_node;
4548 }
4549 stmt = c_begin_compound_stmt (true);
4550 c_parser_compound_statement_nostart (parser);
4551
4552 /* If the compound stmt contains array notations, then we expand them. */
4553 if (flag_cilkplus && contains_array_notation_expr (stmt))
4554 stmt = expand_array_notation_exprs (stmt);
4555 return c_end_compound_stmt (brace_loc, stmt, true);
4556 }
4557
4558 /* Parse a compound statement except for the opening brace. This is
4559 used for parsing both compound statements and statement expressions
4560 (which follow different paths to handling the opening). */
4561
4562 static void
4563 c_parser_compound_statement_nostart (c_parser *parser)
4564 {
4565 bool last_stmt = false;
4566 bool last_label = false;
4567 bool save_valid_for_pragma = valid_location_for_stdc_pragma_p ();
4568 location_t label_loc = UNKNOWN_LOCATION; /* Quiet warning. */
4569 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
4570 {
4571 c_parser_consume_token (parser);
4572 return;
4573 }
4574 mark_valid_location_for_stdc_pragma (true);
4575 if (c_parser_next_token_is_keyword (parser, RID_LABEL))
4576 {
4577 /* Read zero or more forward-declarations for labels that nested
4578 functions can jump to. */
4579 mark_valid_location_for_stdc_pragma (false);
4580 while (c_parser_next_token_is_keyword (parser, RID_LABEL))
4581 {
4582 label_loc = c_parser_peek_token (parser)->location;
4583 c_parser_consume_token (parser);
4584 /* Any identifiers, including those declared as type names,
4585 are OK here. */
4586 while (true)
4587 {
4588 tree label;
4589 if (c_parser_next_token_is_not (parser, CPP_NAME))
4590 {
4591 c_parser_error (parser, "expected identifier");
4592 break;
4593 }
4594 label
4595 = declare_label (c_parser_peek_token (parser)->value);
4596 C_DECLARED_LABEL_FLAG (label) = 1;
4597 add_stmt (build_stmt (label_loc, DECL_EXPR, label));
4598 c_parser_consume_token (parser);
4599 if (c_parser_next_token_is (parser, CPP_COMMA))
4600 c_parser_consume_token (parser);
4601 else
4602 break;
4603 }
4604 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
4605 }
4606 pedwarn (label_loc, OPT_Wpedantic, "ISO C forbids label declarations");
4607 }
4608 /* We must now have at least one statement, label or declaration. */
4609 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
4610 {
4611 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
4612 c_parser_error (parser, "expected declaration or statement");
4613 c_parser_consume_token (parser);
4614 return;
4615 }
4616 while (c_parser_next_token_is_not (parser, CPP_CLOSE_BRACE))
4617 {
4618 location_t loc = c_parser_peek_token (parser)->location;
4619 if (c_parser_next_token_is_keyword (parser, RID_CASE)
4620 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
4621 || (c_parser_next_token_is (parser, CPP_NAME)
4622 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
4623 {
4624 if (c_parser_next_token_is_keyword (parser, RID_CASE))
4625 label_loc = c_parser_peek_2nd_token (parser)->location;
4626 else
4627 label_loc = c_parser_peek_token (parser)->location;
4628 last_label = true;
4629 last_stmt = false;
4630 mark_valid_location_for_stdc_pragma (false);
4631 c_parser_label (parser);
4632 }
4633 else if (!last_label
4634 && c_parser_next_tokens_start_declaration (parser))
4635 {
4636 last_label = false;
4637 mark_valid_location_for_stdc_pragma (false);
4638 c_parser_declaration_or_fndef (parser, true, true, true, true,
4639 true, NULL, vNULL);
4640 if (last_stmt)
4641 pedwarn_c90 (loc, OPT_Wdeclaration_after_statement,
4642 "ISO C90 forbids mixed declarations and code");
4643 last_stmt = false;
4644 }
4645 else if (!last_label
4646 && c_parser_next_token_is_keyword (parser, RID_EXTENSION))
4647 {
4648 /* __extension__ can start a declaration, but is also an
4649 unary operator that can start an expression. Consume all
4650 but the last of a possible series of __extension__ to
4651 determine which. */
4652 while (c_parser_peek_2nd_token (parser)->type == CPP_KEYWORD
4653 && (c_parser_peek_2nd_token (parser)->keyword
4654 == RID_EXTENSION))
4655 c_parser_consume_token (parser);
4656 if (c_token_starts_declaration (c_parser_peek_2nd_token (parser)))
4657 {
4658 int ext;
4659 ext = disable_extension_diagnostics ();
4660 c_parser_consume_token (parser);
4661 last_label = false;
4662 mark_valid_location_for_stdc_pragma (false);
4663 c_parser_declaration_or_fndef (parser, true, true, true, true,
4664 true, NULL, vNULL);
4665 /* Following the old parser, __extension__ does not
4666 disable this diagnostic. */
4667 restore_extension_diagnostics (ext);
4668 if (last_stmt)
4669 pedwarn_c90 (loc, OPT_Wdeclaration_after_statement,
4670 "ISO C90 forbids mixed declarations and code");
4671 last_stmt = false;
4672 }
4673 else
4674 goto statement;
4675 }
4676 else if (c_parser_next_token_is (parser, CPP_PRAGMA))
4677 {
4678 /* External pragmas, and some omp pragmas, are not associated
4679 with regular c code, and so are not to be considered statements
4680 syntactically. This ensures that the user doesn't put them
4681 places that would turn into syntax errors if the directive
4682 were ignored. */
4683 if (c_parser_pragma (parser, pragma_compound))
4684 last_label = false, last_stmt = true;
4685 }
4686 else if (c_parser_next_token_is (parser, CPP_EOF))
4687 {
4688 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
4689 c_parser_error (parser, "expected declaration or statement");
4690 return;
4691 }
4692 else if (c_parser_next_token_is_keyword (parser, RID_ELSE))
4693 {
4694 if (parser->in_if_block)
4695 {
4696 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
4697 error_at (loc, """expected %<}%> before %<else%>");
4698 return;
4699 }
4700 else
4701 {
4702 error_at (loc, "%<else%> without a previous %<if%>");
4703 c_parser_consume_token (parser);
4704 continue;
4705 }
4706 }
4707 else
4708 {
4709 statement:
4710 last_label = false;
4711 last_stmt = true;
4712 mark_valid_location_for_stdc_pragma (false);
4713 c_parser_statement_after_labels (parser);
4714 }
4715
4716 parser->error = false;
4717 }
4718 if (last_label)
4719 error_at (label_loc, "label at end of compound statement");
4720 c_parser_consume_token (parser);
4721 /* Restore the value we started with. */
4722 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
4723 }
4724
4725 /* Parse all consecutive labels. */
4726
4727 static void
4728 c_parser_all_labels (c_parser *parser)
4729 {
4730 while (c_parser_next_token_is_keyword (parser, RID_CASE)
4731 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
4732 || (c_parser_next_token_is (parser, CPP_NAME)
4733 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
4734 c_parser_label (parser);
4735 }
4736
4737 /* Parse a label (C90 6.6.1, C99 6.8.1).
4738
4739 label:
4740 identifier : attributes[opt]
4741 case constant-expression :
4742 default :
4743
4744 GNU extensions:
4745
4746 label:
4747 case constant-expression ... constant-expression :
4748
4749 The use of attributes on labels is a GNU extension. The syntax in
4750 GNU C accepts any expressions without commas, non-constant
4751 expressions being rejected later. */
4752
4753 static void
4754 c_parser_label (c_parser *parser)
4755 {
4756 location_t loc1 = c_parser_peek_token (parser)->location;
4757 tree label = NULL_TREE;
4758 if (c_parser_next_token_is_keyword (parser, RID_CASE))
4759 {
4760 tree exp1, exp2;
4761 c_parser_consume_token (parser);
4762 exp1 = c_parser_expr_no_commas (parser, NULL).value;
4763 if (c_parser_next_token_is (parser, CPP_COLON))
4764 {
4765 c_parser_consume_token (parser);
4766 label = do_case (loc1, exp1, NULL_TREE);
4767 }
4768 else if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
4769 {
4770 c_parser_consume_token (parser);
4771 exp2 = c_parser_expr_no_commas (parser, NULL).value;
4772 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
4773 label = do_case (loc1, exp1, exp2);
4774 }
4775 else
4776 c_parser_error (parser, "expected %<:%> or %<...%>");
4777 }
4778 else if (c_parser_next_token_is_keyword (parser, RID_DEFAULT))
4779 {
4780 c_parser_consume_token (parser);
4781 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
4782 label = do_case (loc1, NULL_TREE, NULL_TREE);
4783 }
4784 else
4785 {
4786 tree name = c_parser_peek_token (parser)->value;
4787 tree tlab;
4788 tree attrs;
4789 location_t loc2 = c_parser_peek_token (parser)->location;
4790 gcc_assert (c_parser_next_token_is (parser, CPP_NAME));
4791 c_parser_consume_token (parser);
4792 gcc_assert (c_parser_next_token_is (parser, CPP_COLON));
4793 c_parser_consume_token (parser);
4794 attrs = c_parser_attributes (parser);
4795 tlab = define_label (loc2, name);
4796 if (tlab)
4797 {
4798 decl_attributes (&tlab, attrs, 0);
4799 label = add_stmt (build_stmt (loc1, LABEL_EXPR, tlab));
4800 }
4801 }
4802 if (label)
4803 {
4804 if (c_parser_next_tokens_start_declaration (parser))
4805 {
4806 error_at (c_parser_peek_token (parser)->location,
4807 "a label can only be part of a statement and "
4808 "a declaration is not a statement");
4809 c_parser_declaration_or_fndef (parser, /*fndef_ok*/ false,
4810 /*static_assert_ok*/ true,
4811 /*empty_ok*/ true, /*nested*/ true,
4812 /*start_attr_ok*/ true, NULL,
4813 vNULL);
4814 }
4815 }
4816 }
4817
4818 /* Parse a statement (C90 6.6, C99 6.8).
4819
4820 statement:
4821 labeled-statement
4822 compound-statement
4823 expression-statement
4824 selection-statement
4825 iteration-statement
4826 jump-statement
4827
4828 labeled-statement:
4829 label statement
4830
4831 expression-statement:
4832 expression[opt] ;
4833
4834 selection-statement:
4835 if-statement
4836 switch-statement
4837
4838 iteration-statement:
4839 while-statement
4840 do-statement
4841 for-statement
4842
4843 jump-statement:
4844 goto identifier ;
4845 continue ;
4846 break ;
4847 return expression[opt] ;
4848
4849 GNU extensions:
4850
4851 statement:
4852 asm-statement
4853
4854 jump-statement:
4855 goto * expression ;
4856
4857 Objective-C:
4858
4859 statement:
4860 objc-throw-statement
4861 objc-try-catch-statement
4862 objc-synchronized-statement
4863
4864 objc-throw-statement:
4865 @throw expression ;
4866 @throw ;
4867
4868 OpenACC:
4869
4870 statement:
4871 openacc-construct
4872
4873 openacc-construct:
4874 parallel-construct
4875 kernels-construct
4876 data-construct
4877 loop-construct
4878
4879 parallel-construct:
4880 parallel-directive structured-block
4881
4882 kernels-construct:
4883 kernels-directive structured-block
4884
4885 data-construct:
4886 data-directive structured-block
4887
4888 loop-construct:
4889 loop-directive structured-block
4890
4891 OpenMP:
4892
4893 statement:
4894 openmp-construct
4895
4896 openmp-construct:
4897 parallel-construct
4898 for-construct
4899 simd-construct
4900 for-simd-construct
4901 sections-construct
4902 single-construct
4903 parallel-for-construct
4904 parallel-for-simd-construct
4905 parallel-sections-construct
4906 master-construct
4907 critical-construct
4908 atomic-construct
4909 ordered-construct
4910
4911 parallel-construct:
4912 parallel-directive structured-block
4913
4914 for-construct:
4915 for-directive iteration-statement
4916
4917 simd-construct:
4918 simd-directive iteration-statements
4919
4920 for-simd-construct:
4921 for-simd-directive iteration-statements
4922
4923 sections-construct:
4924 sections-directive section-scope
4925
4926 single-construct:
4927 single-directive structured-block
4928
4929 parallel-for-construct:
4930 parallel-for-directive iteration-statement
4931
4932 parallel-for-simd-construct:
4933 parallel-for-simd-directive iteration-statement
4934
4935 parallel-sections-construct:
4936 parallel-sections-directive section-scope
4937
4938 master-construct:
4939 master-directive structured-block
4940
4941 critical-construct:
4942 critical-directive structured-block
4943
4944 atomic-construct:
4945 atomic-directive expression-statement
4946
4947 ordered-construct:
4948 ordered-directive structured-block
4949
4950 Transactional Memory:
4951
4952 statement:
4953 transaction-statement
4954 transaction-cancel-statement
4955 */
4956
4957 static void
4958 c_parser_statement (c_parser *parser)
4959 {
4960 c_parser_all_labels (parser);
4961 c_parser_statement_after_labels (parser);
4962 }
4963
4964 /* Parse a statement, other than a labeled statement. CHAIN is a vector
4965 of if-else-if conditions. */
4966
4967 static void
4968 c_parser_statement_after_labels (c_parser *parser, vec<tree> *chain)
4969 {
4970 location_t loc = c_parser_peek_token (parser)->location;
4971 tree stmt = NULL_TREE;
4972 bool in_if_block = parser->in_if_block;
4973 parser->in_if_block = false;
4974 switch (c_parser_peek_token (parser)->type)
4975 {
4976 case CPP_OPEN_BRACE:
4977 add_stmt (c_parser_compound_statement (parser));
4978 break;
4979 case CPP_KEYWORD:
4980 switch (c_parser_peek_token (parser)->keyword)
4981 {
4982 case RID_IF:
4983 c_parser_if_statement (parser, chain);
4984 break;
4985 case RID_SWITCH:
4986 c_parser_switch_statement (parser);
4987 break;
4988 case RID_WHILE:
4989 c_parser_while_statement (parser, false);
4990 break;
4991 case RID_DO:
4992 c_parser_do_statement (parser, false);
4993 break;
4994 case RID_FOR:
4995 c_parser_for_statement (parser, false);
4996 break;
4997 case RID_CILK_FOR:
4998 if (!flag_cilkplus)
4999 {
5000 error_at (c_parser_peek_token (parser)->location,
5001 "-fcilkplus must be enabled to use %<_Cilk_for%>");
5002 c_parser_skip_to_end_of_block_or_statement (parser);
5003 }
5004 else
5005 c_parser_cilk_for (parser, integer_zero_node);
5006 break;
5007 case RID_CILK_SYNC:
5008 c_parser_consume_token (parser);
5009 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
5010 if (!flag_cilkplus)
5011 error_at (loc, "-fcilkplus must be enabled to use %<_Cilk_sync%>");
5012 else
5013 add_stmt (build_cilk_sync ());
5014 break;
5015 case RID_GOTO:
5016 c_parser_consume_token (parser);
5017 if (c_parser_next_token_is (parser, CPP_NAME))
5018 {
5019 stmt = c_finish_goto_label (loc,
5020 c_parser_peek_token (parser)->value);
5021 c_parser_consume_token (parser);
5022 }
5023 else if (c_parser_next_token_is (parser, CPP_MULT))
5024 {
5025 struct c_expr val;
5026
5027 c_parser_consume_token (parser);
5028 val = c_parser_expression (parser);
5029 if (check_no_cilk (val.value,
5030 "Cilk array notation cannot be used as a computed goto expression",
5031 "%<_Cilk_spawn%> statement cannot be used as a computed goto expression",
5032 loc))
5033 val.value = error_mark_node;
5034 val = convert_lvalue_to_rvalue (loc, val, false, true);
5035 stmt = c_finish_goto_ptr (loc, val.value);
5036 }
5037 else
5038 c_parser_error (parser, "expected identifier or %<*%>");
5039 goto expect_semicolon;
5040 case RID_CONTINUE:
5041 c_parser_consume_token (parser);
5042 stmt = c_finish_bc_stmt (loc, &c_cont_label, false);
5043 goto expect_semicolon;
5044 case RID_BREAK:
5045 c_parser_consume_token (parser);
5046 stmt = c_finish_bc_stmt (loc, &c_break_label, true);
5047 goto expect_semicolon;
5048 case RID_RETURN:
5049 c_parser_consume_token (parser);
5050 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5051 {
5052 stmt = c_finish_return (loc, NULL_TREE, NULL_TREE);
5053 c_parser_consume_token (parser);
5054 }
5055 else
5056 {
5057 location_t xloc = c_parser_peek_token (parser)->location;
5058 struct c_expr expr = c_parser_expression_conv (parser);
5059 mark_exp_read (expr.value);
5060 stmt = c_finish_return (xloc, expr.value, expr.original_type);
5061 goto expect_semicolon;
5062 }
5063 break;
5064 case RID_ASM:
5065 stmt = c_parser_asm_statement (parser);
5066 break;
5067 case RID_TRANSACTION_ATOMIC:
5068 case RID_TRANSACTION_RELAXED:
5069 stmt = c_parser_transaction (parser,
5070 c_parser_peek_token (parser)->keyword);
5071 break;
5072 case RID_TRANSACTION_CANCEL:
5073 stmt = c_parser_transaction_cancel (parser);
5074 goto expect_semicolon;
5075 case RID_AT_THROW:
5076 gcc_assert (c_dialect_objc ());
5077 c_parser_consume_token (parser);
5078 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5079 {
5080 stmt = objc_build_throw_stmt (loc, NULL_TREE);
5081 c_parser_consume_token (parser);
5082 }
5083 else
5084 {
5085 struct c_expr expr = c_parser_expression (parser);
5086 expr = convert_lvalue_to_rvalue (loc, expr, false, false);
5087 if (check_no_cilk (expr.value,
5088 "Cilk array notation cannot be used for a throw expression",
5089 "%<_Cilk_spawn%> statement cannot be used for a throw expression"))
5090 expr.value = error_mark_node;
5091 else
5092 {
5093 expr.value = c_fully_fold (expr.value, false, NULL);
5094 stmt = objc_build_throw_stmt (loc, expr.value);
5095 }
5096 goto expect_semicolon;
5097 }
5098 break;
5099 case RID_AT_TRY:
5100 gcc_assert (c_dialect_objc ());
5101 c_parser_objc_try_catch_finally_statement (parser);
5102 break;
5103 case RID_AT_SYNCHRONIZED:
5104 gcc_assert (c_dialect_objc ());
5105 c_parser_objc_synchronized_statement (parser);
5106 break;
5107 default:
5108 goto expr_stmt;
5109 }
5110 break;
5111 case CPP_SEMICOLON:
5112 c_parser_consume_token (parser);
5113 break;
5114 case CPP_CLOSE_PAREN:
5115 case CPP_CLOSE_SQUARE:
5116 /* Avoid infinite loop in error recovery:
5117 c_parser_skip_until_found stops at a closing nesting
5118 delimiter without consuming it, but here we need to consume
5119 it to proceed further. */
5120 c_parser_error (parser, "expected statement");
5121 c_parser_consume_token (parser);
5122 break;
5123 case CPP_PRAGMA:
5124 c_parser_pragma (parser, pragma_stmt);
5125 break;
5126 default:
5127 expr_stmt:
5128 stmt = c_finish_expr_stmt (loc, c_parser_expression_conv (parser).value);
5129 expect_semicolon:
5130 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
5131 break;
5132 }
5133 /* Two cases cannot and do not have line numbers associated: If stmt
5134 is degenerate, such as "2;", then stmt is an INTEGER_CST, which
5135 cannot hold line numbers. But that's OK because the statement
5136 will either be changed to a MODIFY_EXPR during gimplification of
5137 the statement expr, or discarded. If stmt was compound, but
5138 without new variables, we will have skipped the creation of a
5139 BIND and will have a bare STATEMENT_LIST. But that's OK because
5140 (recursively) all of the component statements should already have
5141 line numbers assigned. ??? Can we discard no-op statements
5142 earlier? */
5143 if (EXPR_LOCATION (stmt) == UNKNOWN_LOCATION)
5144 protected_set_expr_location (stmt, loc);
5145
5146 parser->in_if_block = in_if_block;
5147 }
5148
5149 /* Parse the condition from an if, do, while or for statements. */
5150
5151 static tree
5152 c_parser_condition (c_parser *parser)
5153 {
5154 location_t loc = c_parser_peek_token (parser)->location;
5155 tree cond;
5156 cond = c_parser_expression_conv (parser).value;
5157 cond = c_objc_common_truthvalue_conversion (loc, cond);
5158 cond = c_fully_fold (cond, false, NULL);
5159 if (warn_sequence_point)
5160 verify_sequence_points (cond);
5161 return cond;
5162 }
5163
5164 /* Parse a parenthesized condition from an if, do or while statement.
5165
5166 condition:
5167 ( expression )
5168 */
5169 static tree
5170 c_parser_paren_condition (c_parser *parser)
5171 {
5172 tree cond;
5173 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5174 return error_mark_node;
5175 cond = c_parser_condition (parser);
5176 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
5177 return cond;
5178 }
5179
5180 /* Parse a statement which is a block in C99. */
5181
5182 static tree
5183 c_parser_c99_block_statement (c_parser *parser)
5184 {
5185 tree block = c_begin_compound_stmt (flag_isoc99);
5186 location_t loc = c_parser_peek_token (parser)->location;
5187 c_parser_statement (parser);
5188 return c_end_compound_stmt (loc, block, flag_isoc99);
5189 }
5190
5191 /* Parse the body of an if statement. This is just parsing a
5192 statement but (a) it is a block in C99, (b) we track whether the
5193 body is an if statement for the sake of -Wparentheses warnings, (c)
5194 we handle an empty body specially for the sake of -Wempty-body
5195 warnings, and (d) we call parser_compound_statement directly
5196 because c_parser_statement_after_labels resets
5197 parser->in_if_block. */
5198
5199 static tree
5200 c_parser_if_body (c_parser *parser, bool *if_p,
5201 const token_indent_info &if_tinfo)
5202 {
5203 tree block = c_begin_compound_stmt (flag_isoc99);
5204 location_t body_loc = c_parser_peek_token (parser)->location;
5205 token_indent_info body_tinfo
5206 = get_token_indent_info (c_parser_peek_token (parser));
5207
5208 c_parser_all_labels (parser);
5209 *if_p = c_parser_next_token_is_keyword (parser, RID_IF);
5210 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5211 {
5212 location_t loc = c_parser_peek_token (parser)->location;
5213 add_stmt (build_empty_stmt (loc));
5214 c_parser_consume_token (parser);
5215 if (!c_parser_next_token_is_keyword (parser, RID_ELSE))
5216 warning_at (loc, OPT_Wempty_body,
5217 "suggest braces around empty body in an %<if%> statement");
5218 }
5219 else if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
5220 add_stmt (c_parser_compound_statement (parser));
5221 else
5222 c_parser_statement_after_labels (parser);
5223
5224 token_indent_info next_tinfo
5225 = get_token_indent_info (c_parser_peek_token (parser));
5226 warn_for_misleading_indentation (if_tinfo, body_tinfo, next_tinfo);
5227
5228 return c_end_compound_stmt (body_loc, block, flag_isoc99);
5229 }
5230
5231 /* Parse the else body of an if statement. This is just parsing a
5232 statement but (a) it is a block in C99, (b) we handle an empty body
5233 specially for the sake of -Wempty-body warnings. CHAIN is a vector
5234 of if-else-if conditions. */
5235
5236 static tree
5237 c_parser_else_body (c_parser *parser, const token_indent_info &else_tinfo,
5238 vec<tree> *chain)
5239 {
5240 location_t body_loc = c_parser_peek_token (parser)->location;
5241 tree block = c_begin_compound_stmt (flag_isoc99);
5242 token_indent_info body_tinfo
5243 = get_token_indent_info (c_parser_peek_token (parser));
5244
5245 c_parser_all_labels (parser);
5246 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5247 {
5248 location_t loc = c_parser_peek_token (parser)->location;
5249 warning_at (loc,
5250 OPT_Wempty_body,
5251 "suggest braces around empty body in an %<else%> statement");
5252 add_stmt (build_empty_stmt (loc));
5253 c_parser_consume_token (parser);
5254 }
5255 else
5256 c_parser_statement_after_labels (parser, chain);
5257
5258 token_indent_info next_tinfo
5259 = get_token_indent_info (c_parser_peek_token (parser));
5260 warn_for_misleading_indentation (else_tinfo, body_tinfo, next_tinfo);
5261
5262 return c_end_compound_stmt (body_loc, block, flag_isoc99);
5263 }
5264
5265 /* Parse an if statement (C90 6.6.4, C99 6.8.4).
5266
5267 if-statement:
5268 if ( expression ) statement
5269 if ( expression ) statement else statement
5270
5271 CHAIN is a vector of if-else-if conditions. */
5272
5273 static void
5274 c_parser_if_statement (c_parser *parser, vec<tree> *chain)
5275 {
5276 tree block;
5277 location_t loc;
5278 tree cond;
5279 bool first_if = false;
5280 tree first_body, second_body;
5281 bool in_if_block;
5282 tree if_stmt;
5283
5284 gcc_assert (c_parser_next_token_is_keyword (parser, RID_IF));
5285 token_indent_info if_tinfo
5286 = get_token_indent_info (c_parser_peek_token (parser));
5287 c_parser_consume_token (parser);
5288 block = c_begin_compound_stmt (flag_isoc99);
5289 loc = c_parser_peek_token (parser)->location;
5290 cond = c_parser_paren_condition (parser);
5291 if (flag_cilkplus && contains_cilk_spawn_stmt (cond))
5292 {
5293 error_at (loc, "if statement cannot contain %<Cilk_spawn%>");
5294 cond = error_mark_node;
5295 }
5296 in_if_block = parser->in_if_block;
5297 parser->in_if_block = true;
5298 first_body = c_parser_if_body (parser, &first_if, if_tinfo);
5299 parser->in_if_block = in_if_block;
5300
5301 if (warn_duplicated_cond)
5302 warn_duplicated_cond_add_or_warn (EXPR_LOCATION (cond), cond, &chain);
5303
5304 if (c_parser_next_token_is_keyword (parser, RID_ELSE))
5305 {
5306 token_indent_info else_tinfo
5307 = get_token_indent_info (c_parser_peek_token (parser));
5308 c_parser_consume_token (parser);
5309 if (warn_duplicated_cond)
5310 {
5311 if (c_parser_next_token_is_keyword (parser, RID_IF)
5312 && chain == NULL)
5313 {
5314 /* We've got "if (COND) else if (COND2)". Start the
5315 condition chain and add COND as the first element. */
5316 chain = new vec<tree> ();
5317 if (!CONSTANT_CLASS_P (cond) && !TREE_SIDE_EFFECTS (cond))
5318 chain->safe_push (cond);
5319 }
5320 else if (!c_parser_next_token_is_keyword (parser, RID_IF))
5321 {
5322 /* This is if-else without subsequent if. Zap the condition
5323 chain; we would have already warned at this point. */
5324 delete chain;
5325 chain = NULL;
5326 }
5327 }
5328 second_body = c_parser_else_body (parser, else_tinfo, chain);
5329 }
5330 else
5331 {
5332 second_body = NULL_TREE;
5333 if (warn_duplicated_cond)
5334 {
5335 /* This if statement does not have an else clause. We don't
5336 need the condition chain anymore. */
5337 delete chain;
5338 chain = NULL;
5339 }
5340 }
5341 c_finish_if_stmt (loc, cond, first_body, second_body, first_if);
5342 if_stmt = c_end_compound_stmt (loc, block, flag_isoc99);
5343
5344 /* If the if statement contains array notations, then we expand them. */
5345 if (flag_cilkplus && contains_array_notation_expr (if_stmt))
5346 if_stmt = fix_conditional_array_notations (if_stmt);
5347 add_stmt (if_stmt);
5348 }
5349
5350 /* Parse a switch statement (C90 6.6.4, C99 6.8.4).
5351
5352 switch-statement:
5353 switch (expression) statement
5354 */
5355
5356 static void
5357 c_parser_switch_statement (c_parser *parser)
5358 {
5359 struct c_expr ce;
5360 tree block, expr, body, save_break;
5361 location_t switch_loc = c_parser_peek_token (parser)->location;
5362 location_t switch_cond_loc;
5363 gcc_assert (c_parser_next_token_is_keyword (parser, RID_SWITCH));
5364 c_parser_consume_token (parser);
5365 block = c_begin_compound_stmt (flag_isoc99);
5366 bool explicit_cast_p = false;
5367 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5368 {
5369 switch_cond_loc = c_parser_peek_token (parser)->location;
5370 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
5371 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
5372 explicit_cast_p = true;
5373 ce = c_parser_expression (parser);
5374 ce = convert_lvalue_to_rvalue (switch_cond_loc, ce, true, false);
5375 expr = ce.value;
5376 /* ??? expr has no valid location? */
5377 if (check_no_cilk (expr,
5378 "Cilk array notation cannot be used as a condition for switch statement",
5379 "%<_Cilk_spawn%> statement cannot be used as a condition for switch statement",
5380 switch_cond_loc))
5381 expr = error_mark_node;
5382 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
5383 }
5384 else
5385 {
5386 switch_cond_loc = UNKNOWN_LOCATION;
5387 expr = error_mark_node;
5388 }
5389 c_start_case (switch_loc, switch_cond_loc, expr, explicit_cast_p);
5390 save_break = c_break_label;
5391 c_break_label = NULL_TREE;
5392 body = c_parser_c99_block_statement (parser);
5393 c_finish_case (body, ce.original_type);
5394 if (c_break_label)
5395 {
5396 location_t here = c_parser_peek_token (parser)->location;
5397 tree t = build1 (LABEL_EXPR, void_type_node, c_break_label);
5398 SET_EXPR_LOCATION (t, here);
5399 add_stmt (t);
5400 }
5401 c_break_label = save_break;
5402 add_stmt (c_end_compound_stmt (switch_loc, block, flag_isoc99));
5403 }
5404
5405 /* Parse a while statement (C90 6.6.5, C99 6.8.5).
5406
5407 while-statement:
5408 while (expression) statement
5409 */
5410
5411 static void
5412 c_parser_while_statement (c_parser *parser, bool ivdep)
5413 {
5414 tree block, cond, body, save_break, save_cont;
5415 location_t loc;
5416 gcc_assert (c_parser_next_token_is_keyword (parser, RID_WHILE));
5417 token_indent_info while_tinfo
5418 = get_token_indent_info (c_parser_peek_token (parser));
5419 c_parser_consume_token (parser);
5420 block = c_begin_compound_stmt (flag_isoc99);
5421 loc = c_parser_peek_token (parser)->location;
5422 cond = c_parser_paren_condition (parser);
5423 if (check_no_cilk (cond,
5424 "Cilk array notation cannot be used as a condition for while statement",
5425 "%<_Cilk_spawn%> statement cannot be used as a condition for while statement"))
5426 cond = error_mark_node;
5427 if (ivdep && cond != error_mark_node)
5428 cond = build2 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
5429 build_int_cst (integer_type_node,
5430 annot_expr_ivdep_kind));
5431 save_break = c_break_label;
5432 c_break_label = NULL_TREE;
5433 save_cont = c_cont_label;
5434 c_cont_label = NULL_TREE;
5435
5436 token_indent_info body_tinfo
5437 = get_token_indent_info (c_parser_peek_token (parser));
5438
5439 body = c_parser_c99_block_statement (parser);
5440
5441 token_indent_info next_tinfo
5442 = get_token_indent_info (c_parser_peek_token (parser));
5443 warn_for_misleading_indentation (while_tinfo, body_tinfo, next_tinfo);
5444
5445 c_finish_loop (loc, cond, NULL, body, c_break_label, c_cont_label, true);
5446 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99));
5447 c_break_label = save_break;
5448 c_cont_label = save_cont;
5449 }
5450
5451 /* Parse a do statement (C90 6.6.5, C99 6.8.5).
5452
5453 do-statement:
5454 do statement while ( expression ) ;
5455 */
5456
5457 static void
5458 c_parser_do_statement (c_parser *parser, bool ivdep)
5459 {
5460 tree block, cond, body, save_break, save_cont, new_break, new_cont;
5461 location_t loc;
5462 gcc_assert (c_parser_next_token_is_keyword (parser, RID_DO));
5463 c_parser_consume_token (parser);
5464 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5465 warning_at (c_parser_peek_token (parser)->location,
5466 OPT_Wempty_body,
5467 "suggest braces around empty body in %<do%> statement");
5468 block = c_begin_compound_stmt (flag_isoc99);
5469 loc = c_parser_peek_token (parser)->location;
5470 save_break = c_break_label;
5471 c_break_label = NULL_TREE;
5472 save_cont = c_cont_label;
5473 c_cont_label = NULL_TREE;
5474 body = c_parser_c99_block_statement (parser);
5475 c_parser_require_keyword (parser, RID_WHILE, "expected %<while%>");
5476 new_break = c_break_label;
5477 c_break_label = save_break;
5478 new_cont = c_cont_label;
5479 c_cont_label = save_cont;
5480 cond = c_parser_paren_condition (parser);
5481 if (check_no_cilk (cond,
5482 "Cilk array notation cannot be used as a condition for a do-while statement",
5483 "%<_Cilk_spawn%> statement cannot be used as a condition for a do-while statement"))
5484 cond = error_mark_node;
5485 if (ivdep && cond != error_mark_node)
5486 cond = build2 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
5487 build_int_cst (integer_type_node,
5488 annot_expr_ivdep_kind));
5489 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
5490 c_parser_skip_to_end_of_block_or_statement (parser);
5491 c_finish_loop (loc, cond, NULL, body, new_break, new_cont, false);
5492 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99));
5493 }
5494
5495 /* Parse a for statement (C90 6.6.5, C99 6.8.5).
5496
5497 for-statement:
5498 for ( expression[opt] ; expression[opt] ; expression[opt] ) statement
5499 for ( nested-declaration expression[opt] ; expression[opt] ) statement
5500
5501 The form with a declaration is new in C99.
5502
5503 ??? In accordance with the old parser, the declaration may be a
5504 nested function, which is then rejected in check_for_loop_decls,
5505 but does it make any sense for this to be included in the grammar?
5506 Note in particular that the nested function does not include a
5507 trailing ';', whereas the "declaration" production includes one.
5508 Also, can we reject bad declarations earlier and cheaper than
5509 check_for_loop_decls?
5510
5511 In Objective-C, there are two additional variants:
5512
5513 foreach-statement:
5514 for ( expression in expresssion ) statement
5515 for ( declaration in expression ) statement
5516
5517 This is inconsistent with C, because the second variant is allowed
5518 even if c99 is not enabled.
5519
5520 The rest of the comment documents these Objective-C foreach-statement.
5521
5522 Here is the canonical example of the first variant:
5523 for (object in array) { do something with object }
5524 we call the first expression ("object") the "object_expression" and
5525 the second expression ("array") the "collection_expression".
5526 object_expression must be an lvalue of type "id" (a generic Objective-C
5527 object) because the loop works by assigning to object_expression the
5528 various objects from the collection_expression. collection_expression
5529 must evaluate to something of type "id" which responds to the method
5530 countByEnumeratingWithState:objects:count:.
5531
5532 The canonical example of the second variant is:
5533 for (id object in array) { do something with object }
5534 which is completely equivalent to
5535 {
5536 id object;
5537 for (object in array) { do something with object }
5538 }
5539 Note that initizializing 'object' in some way (eg, "for ((object =
5540 xxx) in array) { do something with object }") is possibly
5541 technically valid, but completely pointless as 'object' will be
5542 assigned to something else as soon as the loop starts. We should
5543 most likely reject it (TODO).
5544
5545 The beginning of the Objective-C foreach-statement looks exactly
5546 like the beginning of the for-statement, and we can tell it is a
5547 foreach-statement only because the initial declaration or
5548 expression is terminated by 'in' instead of ';'.
5549 */
5550
5551 static void
5552 c_parser_for_statement (c_parser *parser, bool ivdep)
5553 {
5554 tree block, cond, incr, save_break, save_cont, body;
5555 /* The following are only used when parsing an ObjC foreach statement. */
5556 tree object_expression;
5557 /* Silence the bogus uninitialized warning. */
5558 tree collection_expression = NULL;
5559 location_t loc = c_parser_peek_token (parser)->location;
5560 location_t for_loc = c_parser_peek_token (parser)->location;
5561 bool is_foreach_statement = false;
5562 gcc_assert (c_parser_next_token_is_keyword (parser, RID_FOR));
5563 token_indent_info for_tinfo
5564 = get_token_indent_info (c_parser_peek_token (parser));
5565 c_parser_consume_token (parser);
5566 /* Open a compound statement in Objective-C as well, just in case this is
5567 as foreach expression. */
5568 block = c_begin_compound_stmt (flag_isoc99 || c_dialect_objc ());
5569 cond = error_mark_node;
5570 incr = error_mark_node;
5571 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5572 {
5573 /* Parse the initialization declaration or expression. */
5574 object_expression = error_mark_node;
5575 parser->objc_could_be_foreach_context = c_dialect_objc ();
5576 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5577 {
5578 parser->objc_could_be_foreach_context = false;
5579 c_parser_consume_token (parser);
5580 c_finish_expr_stmt (loc, NULL_TREE);
5581 }
5582 else if (c_parser_next_tokens_start_declaration (parser))
5583 {
5584 c_parser_declaration_or_fndef (parser, true, true, true, true, true,
5585 &object_expression, vNULL);
5586 parser->objc_could_be_foreach_context = false;
5587
5588 if (c_parser_next_token_is_keyword (parser, RID_IN))
5589 {
5590 c_parser_consume_token (parser);
5591 is_foreach_statement = true;
5592 if (check_for_loop_decls (for_loc, true) == NULL_TREE)
5593 c_parser_error (parser, "multiple iterating variables in fast enumeration");
5594 }
5595 else
5596 check_for_loop_decls (for_loc, flag_isoc99);
5597 }
5598 else if (c_parser_next_token_is_keyword (parser, RID_EXTENSION))
5599 {
5600 /* __extension__ can start a declaration, but is also an
5601 unary operator that can start an expression. Consume all
5602 but the last of a possible series of __extension__ to
5603 determine which. */
5604 while (c_parser_peek_2nd_token (parser)->type == CPP_KEYWORD
5605 && (c_parser_peek_2nd_token (parser)->keyword
5606 == RID_EXTENSION))
5607 c_parser_consume_token (parser);
5608 if (c_token_starts_declaration (c_parser_peek_2nd_token (parser)))
5609 {
5610 int ext;
5611 ext = disable_extension_diagnostics ();
5612 c_parser_consume_token (parser);
5613 c_parser_declaration_or_fndef (parser, true, true, true, true,
5614 true, &object_expression, vNULL);
5615 parser->objc_could_be_foreach_context = false;
5616
5617 restore_extension_diagnostics (ext);
5618 if (c_parser_next_token_is_keyword (parser, RID_IN))
5619 {
5620 c_parser_consume_token (parser);
5621 is_foreach_statement = true;
5622 if (check_for_loop_decls (for_loc, true) == NULL_TREE)
5623 c_parser_error (parser, "multiple iterating variables in fast enumeration");
5624 }
5625 else
5626 check_for_loop_decls (for_loc, flag_isoc99);
5627 }
5628 else
5629 goto init_expr;
5630 }
5631 else
5632 {
5633 init_expr:
5634 {
5635 struct c_expr ce;
5636 tree init_expression;
5637 ce = c_parser_expression (parser);
5638 /* In theory we could forbid _Cilk_spawn here, as the spec says "only in top
5639 level statement", but it works just fine, so allow it. */
5640 init_expression = ce.value;
5641 parser->objc_could_be_foreach_context = false;
5642 if (c_parser_next_token_is_keyword (parser, RID_IN))
5643 {
5644 c_parser_consume_token (parser);
5645 is_foreach_statement = true;
5646 if (! lvalue_p (init_expression))
5647 c_parser_error (parser, "invalid iterating variable in fast enumeration");
5648 object_expression = c_fully_fold (init_expression, false, NULL);
5649 }
5650 else
5651 {
5652 ce = convert_lvalue_to_rvalue (loc, ce, true, false);
5653 init_expression = ce.value;
5654 c_finish_expr_stmt (loc, init_expression);
5655 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
5656 }
5657 }
5658 }
5659 /* Parse the loop condition. In the case of a foreach
5660 statement, there is no loop condition. */
5661 gcc_assert (!parser->objc_could_be_foreach_context);
5662 if (!is_foreach_statement)
5663 {
5664 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5665 {
5666 if (ivdep)
5667 {
5668 c_parser_error (parser, "missing loop condition in loop with "
5669 "%<GCC ivdep%> pragma");
5670 cond = error_mark_node;
5671 }
5672 else
5673 {
5674 c_parser_consume_token (parser);
5675 cond = NULL_TREE;
5676 }
5677 }
5678 else
5679 {
5680 cond = c_parser_condition (parser);
5681 if (check_no_cilk (cond,
5682 "Cilk array notation cannot be used in a condition for a for-loop",
5683 "%<_Cilk_spawn%> statement cannot be used in a condition for a for-loop"))
5684 cond = error_mark_node;
5685 c_parser_skip_until_found (parser, CPP_SEMICOLON,
5686 "expected %<;%>");
5687 }
5688 if (ivdep && cond != error_mark_node)
5689 cond = build2 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
5690 build_int_cst (integer_type_node,
5691 annot_expr_ivdep_kind));
5692 }
5693 /* Parse the increment expression (the third expression in a
5694 for-statement). In the case of a foreach-statement, this is
5695 the expression that follows the 'in'. */
5696 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
5697 {
5698 if (is_foreach_statement)
5699 {
5700 c_parser_error (parser, "missing collection in fast enumeration");
5701 collection_expression = error_mark_node;
5702 }
5703 else
5704 incr = c_process_expr_stmt (loc, NULL_TREE);
5705 }
5706 else
5707 {
5708 if (is_foreach_statement)
5709 collection_expression = c_fully_fold (c_parser_expression (parser).value,
5710 false, NULL);
5711 else
5712 {
5713 struct c_expr ce = c_parser_expression (parser);
5714 ce = convert_lvalue_to_rvalue (loc, ce, true, false);
5715 incr = c_process_expr_stmt (loc, ce.value);
5716 }
5717 }
5718 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
5719 }
5720 save_break = c_break_label;
5721 c_break_label = NULL_TREE;
5722 save_cont = c_cont_label;
5723 c_cont_label = NULL_TREE;
5724
5725 token_indent_info body_tinfo
5726 = get_token_indent_info (c_parser_peek_token (parser));
5727
5728 body = c_parser_c99_block_statement (parser);
5729
5730 token_indent_info next_tinfo
5731 = get_token_indent_info (c_parser_peek_token (parser));
5732 warn_for_misleading_indentation (for_tinfo, body_tinfo, next_tinfo);
5733
5734 if (is_foreach_statement)
5735 objc_finish_foreach_loop (loc, object_expression, collection_expression, body, c_break_label, c_cont_label);
5736 else
5737 c_finish_loop (loc, cond, incr, body, c_break_label, c_cont_label, true);
5738 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99 || c_dialect_objc ()));
5739 c_break_label = save_break;
5740 c_cont_label = save_cont;
5741 }
5742
5743 /* Parse an asm statement, a GNU extension. This is a full-blown asm
5744 statement with inputs, outputs, clobbers, and volatile tag
5745 allowed.
5746
5747 asm-statement:
5748 asm type-qualifier[opt] ( asm-argument ) ;
5749 asm type-qualifier[opt] goto ( asm-goto-argument ) ;
5750
5751 asm-argument:
5752 asm-string-literal
5753 asm-string-literal : asm-operands[opt]
5754 asm-string-literal : asm-operands[opt] : asm-operands[opt]
5755 asm-string-literal : asm-operands[opt] : asm-operands[opt] : asm-clobbers[opt]
5756
5757 asm-goto-argument:
5758 asm-string-literal : : asm-operands[opt] : asm-clobbers[opt] \
5759 : asm-goto-operands
5760
5761 Qualifiers other than volatile are accepted in the syntax but
5762 warned for. */
5763
5764 static tree
5765 c_parser_asm_statement (c_parser *parser)
5766 {
5767 tree quals, str, outputs, inputs, clobbers, labels, ret;
5768 bool simple, is_goto;
5769 location_t asm_loc = c_parser_peek_token (parser)->location;
5770 int section, nsections;
5771
5772 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ASM));
5773 c_parser_consume_token (parser);
5774 if (c_parser_next_token_is_keyword (parser, RID_VOLATILE))
5775 {
5776 quals = c_parser_peek_token (parser)->value;
5777 c_parser_consume_token (parser);
5778 }
5779 else if (c_parser_next_token_is_keyword (parser, RID_CONST)
5780 || c_parser_next_token_is_keyword (parser, RID_RESTRICT))
5781 {
5782 warning_at (c_parser_peek_token (parser)->location,
5783 0,
5784 "%E qualifier ignored on asm",
5785 c_parser_peek_token (parser)->value);
5786 quals = NULL_TREE;
5787 c_parser_consume_token (parser);
5788 }
5789 else
5790 quals = NULL_TREE;
5791
5792 is_goto = false;
5793 if (c_parser_next_token_is_keyword (parser, RID_GOTO))
5794 {
5795 c_parser_consume_token (parser);
5796 is_goto = true;
5797 }
5798
5799 /* ??? Follow the C++ parser rather than using the
5800 lex_untranslated_string kludge. */
5801 parser->lex_untranslated_string = true;
5802 ret = NULL;
5803
5804 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5805 goto error;
5806
5807 str = c_parser_asm_string_literal (parser);
5808 if (str == NULL_TREE)
5809 goto error_close_paren;
5810
5811 simple = true;
5812 outputs = NULL_TREE;
5813 inputs = NULL_TREE;
5814 clobbers = NULL_TREE;
5815 labels = NULL_TREE;
5816
5817 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN) && !is_goto)
5818 goto done_asm;
5819
5820 /* Parse each colon-delimited section of operands. */
5821 nsections = 3 + is_goto;
5822 for (section = 0; section < nsections; ++section)
5823 {
5824 if (!c_parser_require (parser, CPP_COLON,
5825 is_goto
5826 ? "expected %<:%>"
5827 : "expected %<:%> or %<)%>"))
5828 goto error_close_paren;
5829
5830 /* Once past any colon, we're no longer a simple asm. */
5831 simple = false;
5832
5833 if ((!c_parser_next_token_is (parser, CPP_COLON)
5834 && !c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
5835 || section == 3)
5836 switch (section)
5837 {
5838 case 0:
5839 /* For asm goto, we don't allow output operands, but reserve
5840 the slot for a future extension that does allow them. */
5841 if (!is_goto)
5842 outputs = c_parser_asm_operands (parser);
5843 break;
5844 case 1:
5845 inputs = c_parser_asm_operands (parser);
5846 break;
5847 case 2:
5848 clobbers = c_parser_asm_clobbers (parser);
5849 break;
5850 case 3:
5851 labels = c_parser_asm_goto_operands (parser);
5852 break;
5853 default:
5854 gcc_unreachable ();
5855 }
5856
5857 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN) && !is_goto)
5858 goto done_asm;
5859 }
5860
5861 done_asm:
5862 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
5863 {
5864 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5865 goto error;
5866 }
5867
5868 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
5869 c_parser_skip_to_end_of_block_or_statement (parser);
5870
5871 ret = build_asm_stmt (quals, build_asm_expr (asm_loc, str, outputs, inputs,
5872 clobbers, labels, simple));
5873
5874 error:
5875 parser->lex_untranslated_string = false;
5876 return ret;
5877
5878 error_close_paren:
5879 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5880 goto error;
5881 }
5882
5883 /* Parse asm operands, a GNU extension.
5884
5885 asm-operands:
5886 asm-operand
5887 asm-operands , asm-operand
5888
5889 asm-operand:
5890 asm-string-literal ( expression )
5891 [ identifier ] asm-string-literal ( expression )
5892 */
5893
5894 static tree
5895 c_parser_asm_operands (c_parser *parser)
5896 {
5897 tree list = NULL_TREE;
5898 while (true)
5899 {
5900 tree name, str;
5901 struct c_expr expr;
5902 if (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
5903 {
5904 c_parser_consume_token (parser);
5905 if (c_parser_next_token_is (parser, CPP_NAME))
5906 {
5907 tree id = c_parser_peek_token (parser)->value;
5908 c_parser_consume_token (parser);
5909 name = build_string (IDENTIFIER_LENGTH (id),
5910 IDENTIFIER_POINTER (id));
5911 }
5912 else
5913 {
5914 c_parser_error (parser, "expected identifier");
5915 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
5916 return NULL_TREE;
5917 }
5918 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
5919 "expected %<]%>");
5920 }
5921 else
5922 name = NULL_TREE;
5923 str = c_parser_asm_string_literal (parser);
5924 if (str == NULL_TREE)
5925 return NULL_TREE;
5926 parser->lex_untranslated_string = false;
5927 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5928 {
5929 parser->lex_untranslated_string = true;
5930 return NULL_TREE;
5931 }
5932 expr = c_parser_expression (parser);
5933 mark_exp_read (expr.value);
5934 parser->lex_untranslated_string = true;
5935 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
5936 {
5937 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5938 return NULL_TREE;
5939 }
5940 list = chainon (list, build_tree_list (build_tree_list (name, str),
5941 expr.value));
5942 if (c_parser_next_token_is (parser, CPP_COMMA))
5943 c_parser_consume_token (parser);
5944 else
5945 break;
5946 }
5947 return list;
5948 }
5949
5950 /* Parse asm clobbers, a GNU extension.
5951
5952 asm-clobbers:
5953 asm-string-literal
5954 asm-clobbers , asm-string-literal
5955 */
5956
5957 static tree
5958 c_parser_asm_clobbers (c_parser *parser)
5959 {
5960 tree list = NULL_TREE;
5961 while (true)
5962 {
5963 tree str = c_parser_asm_string_literal (parser);
5964 if (str)
5965 list = tree_cons (NULL_TREE, str, list);
5966 else
5967 return NULL_TREE;
5968 if (c_parser_next_token_is (parser, CPP_COMMA))
5969 c_parser_consume_token (parser);
5970 else
5971 break;
5972 }
5973 return list;
5974 }
5975
5976 /* Parse asm goto labels, a GNU extension.
5977
5978 asm-goto-operands:
5979 identifier
5980 asm-goto-operands , identifier
5981 */
5982
5983 static tree
5984 c_parser_asm_goto_operands (c_parser *parser)
5985 {
5986 tree list = NULL_TREE;
5987 while (true)
5988 {
5989 tree name, label;
5990
5991 if (c_parser_next_token_is (parser, CPP_NAME))
5992 {
5993 c_token *tok = c_parser_peek_token (parser);
5994 name = tok->value;
5995 label = lookup_label_for_goto (tok->location, name);
5996 c_parser_consume_token (parser);
5997 TREE_USED (label) = 1;
5998 }
5999 else
6000 {
6001 c_parser_error (parser, "expected identifier");
6002 return NULL_TREE;
6003 }
6004
6005 name = build_string (IDENTIFIER_LENGTH (name),
6006 IDENTIFIER_POINTER (name));
6007 list = tree_cons (name, label, list);
6008 if (c_parser_next_token_is (parser, CPP_COMMA))
6009 c_parser_consume_token (parser);
6010 else
6011 return nreverse (list);
6012 }
6013 }
6014
6015 /* Parse an expression other than a compound expression; that is, an
6016 assignment expression (C90 6.3.16, C99 6.5.16). If AFTER is not
6017 NULL then it is an Objective-C message expression which is the
6018 primary-expression starting the expression as an initializer.
6019
6020 assignment-expression:
6021 conditional-expression
6022 unary-expression assignment-operator assignment-expression
6023
6024 assignment-operator: one of
6025 = *= /= %= += -= <<= >>= &= ^= |=
6026
6027 In GNU C we accept any conditional expression on the LHS and
6028 diagnose the invalid lvalue rather than producing a syntax
6029 error. */
6030
6031 static struct c_expr
6032 c_parser_expr_no_commas (c_parser *parser, struct c_expr *after,
6033 tree omp_atomic_lhs)
6034 {
6035 struct c_expr lhs, rhs, ret;
6036 enum tree_code code;
6037 location_t op_location, exp_location;
6038 gcc_assert (!after || c_dialect_objc ());
6039 lhs = c_parser_conditional_expression (parser, after, omp_atomic_lhs);
6040 op_location = c_parser_peek_token (parser)->location;
6041 switch (c_parser_peek_token (parser)->type)
6042 {
6043 case CPP_EQ:
6044 code = NOP_EXPR;
6045 break;
6046 case CPP_MULT_EQ:
6047 code = MULT_EXPR;
6048 break;
6049 case CPP_DIV_EQ:
6050 code = TRUNC_DIV_EXPR;
6051 break;
6052 case CPP_MOD_EQ:
6053 code = TRUNC_MOD_EXPR;
6054 break;
6055 case CPP_PLUS_EQ:
6056 code = PLUS_EXPR;
6057 break;
6058 case CPP_MINUS_EQ:
6059 code = MINUS_EXPR;
6060 break;
6061 case CPP_LSHIFT_EQ:
6062 code = LSHIFT_EXPR;
6063 break;
6064 case CPP_RSHIFT_EQ:
6065 code = RSHIFT_EXPR;
6066 break;
6067 case CPP_AND_EQ:
6068 code = BIT_AND_EXPR;
6069 break;
6070 case CPP_XOR_EQ:
6071 code = BIT_XOR_EXPR;
6072 break;
6073 case CPP_OR_EQ:
6074 code = BIT_IOR_EXPR;
6075 break;
6076 default:
6077 return lhs;
6078 }
6079 c_parser_consume_token (parser);
6080 exp_location = c_parser_peek_token (parser)->location;
6081 rhs = c_parser_expr_no_commas (parser, NULL);
6082 rhs = convert_lvalue_to_rvalue (exp_location, rhs, true, true);
6083
6084 ret.value = build_modify_expr (op_location, lhs.value, lhs.original_type,
6085 code, exp_location, rhs.value,
6086 rhs.original_type);
6087 if (code == NOP_EXPR)
6088 ret.original_code = MODIFY_EXPR;
6089 else
6090 {
6091 TREE_NO_WARNING (ret.value) = 1;
6092 ret.original_code = ERROR_MARK;
6093 }
6094 ret.original_type = NULL;
6095 return ret;
6096 }
6097
6098 /* Parse a conditional expression (C90 6.3.15, C99 6.5.15). If AFTER
6099 is not NULL then it is an Objective-C message expression which is
6100 the primary-expression starting the expression as an initializer.
6101
6102 conditional-expression:
6103 logical-OR-expression
6104 logical-OR-expression ? expression : conditional-expression
6105
6106 GNU extensions:
6107
6108 conditional-expression:
6109 logical-OR-expression ? : conditional-expression
6110 */
6111
6112 static struct c_expr
6113 c_parser_conditional_expression (c_parser *parser, struct c_expr *after,
6114 tree omp_atomic_lhs)
6115 {
6116 struct c_expr cond, exp1, exp2, ret;
6117 location_t cond_loc, colon_loc, middle_loc;
6118
6119 gcc_assert (!after || c_dialect_objc ());
6120
6121 cond = c_parser_binary_expression (parser, after, omp_atomic_lhs);
6122
6123 if (c_parser_next_token_is_not (parser, CPP_QUERY))
6124 return cond;
6125 cond_loc = c_parser_peek_token (parser)->location;
6126 cond = convert_lvalue_to_rvalue (cond_loc, cond, true, true);
6127 c_parser_consume_token (parser);
6128 if (c_parser_next_token_is (parser, CPP_COLON))
6129 {
6130 tree eptype = NULL_TREE;
6131
6132 middle_loc = c_parser_peek_token (parser)->location;
6133 pedwarn (middle_loc, OPT_Wpedantic,
6134 "ISO C forbids omitting the middle term of a ?: expression");
6135 warn_for_omitted_condop (middle_loc, cond.value);
6136 if (TREE_CODE (cond.value) == EXCESS_PRECISION_EXPR)
6137 {
6138 eptype = TREE_TYPE (cond.value);
6139 cond.value = TREE_OPERAND (cond.value, 0);
6140 }
6141 /* Make sure first operand is calculated only once. */
6142 exp1.value = c_save_expr (default_conversion (cond.value));
6143 if (eptype)
6144 exp1.value = build1 (EXCESS_PRECISION_EXPR, eptype, exp1.value);
6145 exp1.original_type = NULL;
6146 cond.value = c_objc_common_truthvalue_conversion (cond_loc, exp1.value);
6147 c_inhibit_evaluation_warnings += cond.value == truthvalue_true_node;
6148 }
6149 else
6150 {
6151 cond.value
6152 = c_objc_common_truthvalue_conversion
6153 (cond_loc, default_conversion (cond.value));
6154 c_inhibit_evaluation_warnings += cond.value == truthvalue_false_node;
6155 exp1 = c_parser_expression_conv (parser);
6156 mark_exp_read (exp1.value);
6157 c_inhibit_evaluation_warnings +=
6158 ((cond.value == truthvalue_true_node)
6159 - (cond.value == truthvalue_false_node));
6160 }
6161
6162 colon_loc = c_parser_peek_token (parser)->location;
6163 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
6164 {
6165 c_inhibit_evaluation_warnings -= cond.value == truthvalue_true_node;
6166 ret.value = error_mark_node;
6167 ret.original_code = ERROR_MARK;
6168 ret.original_type = NULL;
6169 return ret;
6170 }
6171 {
6172 location_t exp2_loc = c_parser_peek_token (parser)->location;
6173 exp2 = c_parser_conditional_expression (parser, NULL, NULL_TREE);
6174 exp2 = convert_lvalue_to_rvalue (exp2_loc, exp2, true, true);
6175 }
6176 c_inhibit_evaluation_warnings -= cond.value == truthvalue_true_node;
6177 ret.value = build_conditional_expr (colon_loc, cond.value,
6178 cond.original_code == C_MAYBE_CONST_EXPR,
6179 exp1.value, exp1.original_type,
6180 exp2.value, exp2.original_type);
6181 ret.original_code = ERROR_MARK;
6182 if (exp1.value == error_mark_node || exp2.value == error_mark_node)
6183 ret.original_type = NULL;
6184 else
6185 {
6186 tree t1, t2;
6187
6188 /* If both sides are enum type, the default conversion will have
6189 made the type of the result be an integer type. We want to
6190 remember the enum types we started with. */
6191 t1 = exp1.original_type ? exp1.original_type : TREE_TYPE (exp1.value);
6192 t2 = exp2.original_type ? exp2.original_type : TREE_TYPE (exp2.value);
6193 ret.original_type = ((t1 != error_mark_node
6194 && t2 != error_mark_node
6195 && (TYPE_MAIN_VARIANT (t1)
6196 == TYPE_MAIN_VARIANT (t2)))
6197 ? t1
6198 : NULL);
6199 }
6200 return ret;
6201 }
6202
6203 /* Parse a binary expression; that is, a logical-OR-expression (C90
6204 6.3.5-6.3.14, C99 6.5.5-6.5.14). If AFTER is not NULL then it is
6205 an Objective-C message expression which is the primary-expression
6206 starting the expression as an initializer.
6207
6208 OMP_ATOMIC_LHS is NULL, unless parsing OpenMP #pragma omp atomic,
6209 when it should be the unfolded lhs. In a valid OpenMP source,
6210 one of the operands of the toplevel binary expression must be equal
6211 to it. In that case, just return a build2 created binary operation
6212 rather than result of parser_build_binary_op.
6213
6214 multiplicative-expression:
6215 cast-expression
6216 multiplicative-expression * cast-expression
6217 multiplicative-expression / cast-expression
6218 multiplicative-expression % cast-expression
6219
6220 additive-expression:
6221 multiplicative-expression
6222 additive-expression + multiplicative-expression
6223 additive-expression - multiplicative-expression
6224
6225 shift-expression:
6226 additive-expression
6227 shift-expression << additive-expression
6228 shift-expression >> additive-expression
6229
6230 relational-expression:
6231 shift-expression
6232 relational-expression < shift-expression
6233 relational-expression > shift-expression
6234 relational-expression <= shift-expression
6235 relational-expression >= shift-expression
6236
6237 equality-expression:
6238 relational-expression
6239 equality-expression == relational-expression
6240 equality-expression != relational-expression
6241
6242 AND-expression:
6243 equality-expression
6244 AND-expression & equality-expression
6245
6246 exclusive-OR-expression:
6247 AND-expression
6248 exclusive-OR-expression ^ AND-expression
6249
6250 inclusive-OR-expression:
6251 exclusive-OR-expression
6252 inclusive-OR-expression | exclusive-OR-expression
6253
6254 logical-AND-expression:
6255 inclusive-OR-expression
6256 logical-AND-expression && inclusive-OR-expression
6257
6258 logical-OR-expression:
6259 logical-AND-expression
6260 logical-OR-expression || logical-AND-expression
6261 */
6262
6263 static struct c_expr
6264 c_parser_binary_expression (c_parser *parser, struct c_expr *after,
6265 tree omp_atomic_lhs)
6266 {
6267 /* A binary expression is parsed using operator-precedence parsing,
6268 with the operands being cast expressions. All the binary
6269 operators are left-associative. Thus a binary expression is of
6270 form:
6271
6272 E0 op1 E1 op2 E2 ...
6273
6274 which we represent on a stack. On the stack, the precedence
6275 levels are strictly increasing. When a new operator is
6276 encountered of higher precedence than that at the top of the
6277 stack, it is pushed; its LHS is the top expression, and its RHS
6278 is everything parsed until it is popped. When a new operator is
6279 encountered with precedence less than or equal to that at the top
6280 of the stack, triples E[i-1] op[i] E[i] are popped and replaced
6281 by the result of the operation until the operator at the top of
6282 the stack has lower precedence than the new operator or there is
6283 only one element on the stack; then the top expression is the LHS
6284 of the new operator. In the case of logical AND and OR
6285 expressions, we also need to adjust c_inhibit_evaluation_warnings
6286 as appropriate when the operators are pushed and popped. */
6287
6288 struct {
6289 /* The expression at this stack level. */
6290 struct c_expr expr;
6291 /* The precedence of the operator on its left, PREC_NONE at the
6292 bottom of the stack. */
6293 enum c_parser_prec prec;
6294 /* The operation on its left. */
6295 enum tree_code op;
6296 /* The source location of this operation. */
6297 location_t loc;
6298 } stack[NUM_PRECS];
6299 int sp;
6300 /* Location of the binary operator. */
6301 location_t binary_loc = UNKNOWN_LOCATION; /* Quiet warning. */
6302 #define POP \
6303 do { \
6304 switch (stack[sp].op) \
6305 { \
6306 case TRUTH_ANDIF_EXPR: \
6307 c_inhibit_evaluation_warnings -= (stack[sp - 1].expr.value \
6308 == truthvalue_false_node); \
6309 break; \
6310 case TRUTH_ORIF_EXPR: \
6311 c_inhibit_evaluation_warnings -= (stack[sp - 1].expr.value \
6312 == truthvalue_true_node); \
6313 break; \
6314 default: \
6315 break; \
6316 } \
6317 stack[sp - 1].expr \
6318 = convert_lvalue_to_rvalue (stack[sp - 1].loc, \
6319 stack[sp - 1].expr, true, true); \
6320 stack[sp].expr \
6321 = convert_lvalue_to_rvalue (stack[sp].loc, \
6322 stack[sp].expr, true, true); \
6323 if (__builtin_expect (omp_atomic_lhs != NULL_TREE, 0) && sp == 1 \
6324 && c_parser_peek_token (parser)->type == CPP_SEMICOLON \
6325 && ((1 << stack[sp].prec) \
6326 & ((1 << PREC_BITOR) | (1 << PREC_BITXOR) | (1 << PREC_BITAND) \
6327 | (1 << PREC_SHIFT) | (1 << PREC_ADD) | (1 << PREC_MULT))) \
6328 && stack[sp].op != TRUNC_MOD_EXPR \
6329 && stack[0].expr.value != error_mark_node \
6330 && stack[1].expr.value != error_mark_node \
6331 && (c_tree_equal (stack[0].expr.value, omp_atomic_lhs) \
6332 || c_tree_equal (stack[1].expr.value, omp_atomic_lhs))) \
6333 stack[0].expr.value \
6334 = build2 (stack[1].op, TREE_TYPE (stack[0].expr.value), \
6335 stack[0].expr.value, stack[1].expr.value); \
6336 else \
6337 stack[sp - 1].expr = parser_build_binary_op (stack[sp].loc, \
6338 stack[sp].op, \
6339 stack[sp - 1].expr, \
6340 stack[sp].expr); \
6341 sp--; \
6342 } while (0)
6343 gcc_assert (!after || c_dialect_objc ());
6344 stack[0].loc = c_parser_peek_token (parser)->location;
6345 stack[0].expr = c_parser_cast_expression (parser, after);
6346 stack[0].prec = PREC_NONE;
6347 sp = 0;
6348 while (true)
6349 {
6350 enum c_parser_prec oprec;
6351 enum tree_code ocode;
6352 if (parser->error)
6353 goto out;
6354 switch (c_parser_peek_token (parser)->type)
6355 {
6356 case CPP_MULT:
6357 oprec = PREC_MULT;
6358 ocode = MULT_EXPR;
6359 break;
6360 case CPP_DIV:
6361 oprec = PREC_MULT;
6362 ocode = TRUNC_DIV_EXPR;
6363 break;
6364 case CPP_MOD:
6365 oprec = PREC_MULT;
6366 ocode = TRUNC_MOD_EXPR;
6367 break;
6368 case CPP_PLUS:
6369 oprec = PREC_ADD;
6370 ocode = PLUS_EXPR;
6371 break;
6372 case CPP_MINUS:
6373 oprec = PREC_ADD;
6374 ocode = MINUS_EXPR;
6375 break;
6376 case CPP_LSHIFT:
6377 oprec = PREC_SHIFT;
6378 ocode = LSHIFT_EXPR;
6379 break;
6380 case CPP_RSHIFT:
6381 oprec = PREC_SHIFT;
6382 ocode = RSHIFT_EXPR;
6383 break;
6384 case CPP_LESS:
6385 oprec = PREC_REL;
6386 ocode = LT_EXPR;
6387 break;
6388 case CPP_GREATER:
6389 oprec = PREC_REL;
6390 ocode = GT_EXPR;
6391 break;
6392 case CPP_LESS_EQ:
6393 oprec = PREC_REL;
6394 ocode = LE_EXPR;
6395 break;
6396 case CPP_GREATER_EQ:
6397 oprec = PREC_REL;
6398 ocode = GE_EXPR;
6399 break;
6400 case CPP_EQ_EQ:
6401 oprec = PREC_EQ;
6402 ocode = EQ_EXPR;
6403 break;
6404 case CPP_NOT_EQ:
6405 oprec = PREC_EQ;
6406 ocode = NE_EXPR;
6407 break;
6408 case CPP_AND:
6409 oprec = PREC_BITAND;
6410 ocode = BIT_AND_EXPR;
6411 break;
6412 case CPP_XOR:
6413 oprec = PREC_BITXOR;
6414 ocode = BIT_XOR_EXPR;
6415 break;
6416 case CPP_OR:
6417 oprec = PREC_BITOR;
6418 ocode = BIT_IOR_EXPR;
6419 break;
6420 case CPP_AND_AND:
6421 oprec = PREC_LOGAND;
6422 ocode = TRUTH_ANDIF_EXPR;
6423 break;
6424 case CPP_OR_OR:
6425 oprec = PREC_LOGOR;
6426 ocode = TRUTH_ORIF_EXPR;
6427 break;
6428 default:
6429 /* Not a binary operator, so end of the binary
6430 expression. */
6431 goto out;
6432 }
6433 binary_loc = c_parser_peek_token (parser)->location;
6434 while (oprec <= stack[sp].prec)
6435 POP;
6436 c_parser_consume_token (parser);
6437 switch (ocode)
6438 {
6439 case TRUTH_ANDIF_EXPR:
6440 stack[sp].expr
6441 = convert_lvalue_to_rvalue (stack[sp].loc,
6442 stack[sp].expr, true, true);
6443 stack[sp].expr.value = c_objc_common_truthvalue_conversion
6444 (stack[sp].loc, default_conversion (stack[sp].expr.value));
6445 c_inhibit_evaluation_warnings += (stack[sp].expr.value
6446 == truthvalue_false_node);
6447 break;
6448 case TRUTH_ORIF_EXPR:
6449 stack[sp].expr
6450 = convert_lvalue_to_rvalue (stack[sp].loc,
6451 stack[sp].expr, true, true);
6452 stack[sp].expr.value = c_objc_common_truthvalue_conversion
6453 (stack[sp].loc, default_conversion (stack[sp].expr.value));
6454 c_inhibit_evaluation_warnings += (stack[sp].expr.value
6455 == truthvalue_true_node);
6456 break;
6457 default:
6458 break;
6459 }
6460 sp++;
6461 stack[sp].loc = binary_loc;
6462 stack[sp].expr = c_parser_cast_expression (parser, NULL);
6463 stack[sp].prec = oprec;
6464 stack[sp].op = ocode;
6465 }
6466 out:
6467 while (sp > 0)
6468 POP;
6469 return stack[0].expr;
6470 #undef POP
6471 }
6472
6473 /* Parse a cast expression (C90 6.3.4, C99 6.5.4). If AFTER is not
6474 NULL then it is an Objective-C message expression which is the
6475 primary-expression starting the expression as an initializer.
6476
6477 cast-expression:
6478 unary-expression
6479 ( type-name ) unary-expression
6480 */
6481
6482 static struct c_expr
6483 c_parser_cast_expression (c_parser *parser, struct c_expr *after)
6484 {
6485 location_t cast_loc = c_parser_peek_token (parser)->location;
6486 gcc_assert (!after || c_dialect_objc ());
6487 if (after)
6488 return c_parser_postfix_expression_after_primary (parser,
6489 cast_loc, *after);
6490 /* If the expression begins with a parenthesized type name, it may
6491 be either a cast or a compound literal; we need to see whether
6492 the next character is '{' to tell the difference. If not, it is
6493 an unary expression. Full detection of unknown typenames here
6494 would require a 3-token lookahead. */
6495 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
6496 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
6497 {
6498 struct c_type_name *type_name;
6499 struct c_expr ret;
6500 struct c_expr expr;
6501 c_parser_consume_token (parser);
6502 type_name = c_parser_type_name (parser);
6503 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
6504 if (type_name == NULL)
6505 {
6506 ret.value = error_mark_node;
6507 ret.original_code = ERROR_MARK;
6508 ret.original_type = NULL;
6509 return ret;
6510 }
6511
6512 /* Save casted types in the function's used types hash table. */
6513 used_types_insert (type_name->specs->type);
6514
6515 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
6516 return c_parser_postfix_expression_after_paren_type (parser, type_name,
6517 cast_loc);
6518 {
6519 location_t expr_loc = c_parser_peek_token (parser)->location;
6520 expr = c_parser_cast_expression (parser, NULL);
6521 expr = convert_lvalue_to_rvalue (expr_loc, expr, true, true);
6522 }
6523 ret.value = c_cast_expr (cast_loc, type_name, expr.value);
6524 ret.original_code = ERROR_MARK;
6525 ret.original_type = NULL;
6526 return ret;
6527 }
6528 else
6529 return c_parser_unary_expression (parser);
6530 }
6531
6532 /* Parse an unary expression (C90 6.3.3, C99 6.5.3).
6533
6534 unary-expression:
6535 postfix-expression
6536 ++ unary-expression
6537 -- unary-expression
6538 unary-operator cast-expression
6539 sizeof unary-expression
6540 sizeof ( type-name )
6541
6542 unary-operator: one of
6543 & * + - ~ !
6544
6545 GNU extensions:
6546
6547 unary-expression:
6548 __alignof__ unary-expression
6549 __alignof__ ( type-name )
6550 && identifier
6551
6552 (C11 permits _Alignof with type names only.)
6553
6554 unary-operator: one of
6555 __extension__ __real__ __imag__
6556
6557 Transactional Memory:
6558
6559 unary-expression:
6560 transaction-expression
6561
6562 In addition, the GNU syntax treats ++ and -- as unary operators, so
6563 they may be applied to cast expressions with errors for non-lvalues
6564 given later. */
6565
6566 static struct c_expr
6567 c_parser_unary_expression (c_parser *parser)
6568 {
6569 int ext;
6570 struct c_expr ret, op;
6571 location_t op_loc = c_parser_peek_token (parser)->location;
6572 location_t exp_loc;
6573 ret.original_code = ERROR_MARK;
6574 ret.original_type = NULL;
6575 switch (c_parser_peek_token (parser)->type)
6576 {
6577 case CPP_PLUS_PLUS:
6578 c_parser_consume_token (parser);
6579 exp_loc = c_parser_peek_token (parser)->location;
6580 op = c_parser_cast_expression (parser, NULL);
6581
6582 /* If there is array notations in op, we expand them. */
6583 if (flag_cilkplus && TREE_CODE (op.value) == ARRAY_NOTATION_REF)
6584 return fix_array_notation_expr (exp_loc, PREINCREMENT_EXPR, op);
6585 else
6586 {
6587 op = default_function_array_read_conversion (exp_loc, op);
6588 return parser_build_unary_op (op_loc, PREINCREMENT_EXPR, op);
6589 }
6590 case CPP_MINUS_MINUS:
6591 c_parser_consume_token (parser);
6592 exp_loc = c_parser_peek_token (parser)->location;
6593 op = c_parser_cast_expression (parser, NULL);
6594
6595 /* If there is array notations in op, we expand them. */
6596 if (flag_cilkplus && TREE_CODE (op.value) == ARRAY_NOTATION_REF)
6597 return fix_array_notation_expr (exp_loc, PREDECREMENT_EXPR, op);
6598 else
6599 {
6600 op = default_function_array_read_conversion (exp_loc, op);
6601 return parser_build_unary_op (op_loc, PREDECREMENT_EXPR, op);
6602 }
6603 case CPP_AND:
6604 c_parser_consume_token (parser);
6605 op = c_parser_cast_expression (parser, NULL);
6606 mark_exp_read (op.value);
6607 return parser_build_unary_op (op_loc, ADDR_EXPR, op);
6608 case CPP_MULT:
6609 c_parser_consume_token (parser);
6610 exp_loc = c_parser_peek_token (parser)->location;
6611 op = c_parser_cast_expression (parser, NULL);
6612 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
6613 ret.value = build_indirect_ref (op_loc, op.value, RO_UNARY_STAR);
6614 return ret;
6615 case CPP_PLUS:
6616 if (!c_dialect_objc () && !in_system_header_at (input_location))
6617 warning_at (op_loc,
6618 OPT_Wtraditional,
6619 "traditional C rejects the unary plus operator");
6620 c_parser_consume_token (parser);
6621 exp_loc = c_parser_peek_token (parser)->location;
6622 op = c_parser_cast_expression (parser, NULL);
6623 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
6624 return parser_build_unary_op (op_loc, CONVERT_EXPR, op);
6625 case CPP_MINUS:
6626 c_parser_consume_token (parser);
6627 exp_loc = c_parser_peek_token (parser)->location;
6628 op = c_parser_cast_expression (parser, NULL);
6629 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
6630 return parser_build_unary_op (op_loc, NEGATE_EXPR, op);
6631 case CPP_COMPL:
6632 c_parser_consume_token (parser);
6633 exp_loc = c_parser_peek_token (parser)->location;
6634 op = c_parser_cast_expression (parser, NULL);
6635 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
6636 return parser_build_unary_op (op_loc, BIT_NOT_EXPR, op);
6637 case CPP_NOT:
6638 c_parser_consume_token (parser);
6639 exp_loc = c_parser_peek_token (parser)->location;
6640 op = c_parser_cast_expression (parser, NULL);
6641 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
6642 return parser_build_unary_op (op_loc, TRUTH_NOT_EXPR, op);
6643 case CPP_AND_AND:
6644 /* Refer to the address of a label as a pointer. */
6645 c_parser_consume_token (parser);
6646 if (c_parser_next_token_is (parser, CPP_NAME))
6647 {
6648 ret.value = finish_label_address_expr
6649 (c_parser_peek_token (parser)->value, op_loc);
6650 c_parser_consume_token (parser);
6651 }
6652 else
6653 {
6654 c_parser_error (parser, "expected identifier");
6655 ret.value = error_mark_node;
6656 }
6657 return ret;
6658 case CPP_KEYWORD:
6659 switch (c_parser_peek_token (parser)->keyword)
6660 {
6661 case RID_SIZEOF:
6662 return c_parser_sizeof_expression (parser);
6663 case RID_ALIGNOF:
6664 return c_parser_alignof_expression (parser);
6665 case RID_EXTENSION:
6666 c_parser_consume_token (parser);
6667 ext = disable_extension_diagnostics ();
6668 ret = c_parser_cast_expression (parser, NULL);
6669 restore_extension_diagnostics (ext);
6670 return ret;
6671 case RID_REALPART:
6672 c_parser_consume_token (parser);
6673 exp_loc = c_parser_peek_token (parser)->location;
6674 op = c_parser_cast_expression (parser, NULL);
6675 op = default_function_array_conversion (exp_loc, op);
6676 return parser_build_unary_op (op_loc, REALPART_EXPR, op);
6677 case RID_IMAGPART:
6678 c_parser_consume_token (parser);
6679 exp_loc = c_parser_peek_token (parser)->location;
6680 op = c_parser_cast_expression (parser, NULL);
6681 op = default_function_array_conversion (exp_loc, op);
6682 return parser_build_unary_op (op_loc, IMAGPART_EXPR, op);
6683 case RID_TRANSACTION_ATOMIC:
6684 case RID_TRANSACTION_RELAXED:
6685 return c_parser_transaction_expression (parser,
6686 c_parser_peek_token (parser)->keyword);
6687 default:
6688 return c_parser_postfix_expression (parser);
6689 }
6690 default:
6691 return c_parser_postfix_expression (parser);
6692 }
6693 }
6694
6695 /* Parse a sizeof expression. */
6696
6697 static struct c_expr
6698 c_parser_sizeof_expression (c_parser *parser)
6699 {
6700 struct c_expr expr;
6701 location_t expr_loc;
6702 gcc_assert (c_parser_next_token_is_keyword (parser, RID_SIZEOF));
6703 c_parser_consume_token (parser);
6704 c_inhibit_evaluation_warnings++;
6705 in_sizeof++;
6706 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
6707 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
6708 {
6709 /* Either sizeof ( type-name ) or sizeof unary-expression
6710 starting with a compound literal. */
6711 struct c_type_name *type_name;
6712 c_parser_consume_token (parser);
6713 expr_loc = c_parser_peek_token (parser)->location;
6714 type_name = c_parser_type_name (parser);
6715 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
6716 if (type_name == NULL)
6717 {
6718 struct c_expr ret;
6719 c_inhibit_evaluation_warnings--;
6720 in_sizeof--;
6721 ret.value = error_mark_node;
6722 ret.original_code = ERROR_MARK;
6723 ret.original_type = NULL;
6724 return ret;
6725 }
6726 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
6727 {
6728 expr = c_parser_postfix_expression_after_paren_type (parser,
6729 type_name,
6730 expr_loc);
6731 goto sizeof_expr;
6732 }
6733 /* sizeof ( type-name ). */
6734 c_inhibit_evaluation_warnings--;
6735 in_sizeof--;
6736 return c_expr_sizeof_type (expr_loc, type_name);
6737 }
6738 else
6739 {
6740 expr_loc = c_parser_peek_token (parser)->location;
6741 expr = c_parser_unary_expression (parser);
6742 sizeof_expr:
6743 c_inhibit_evaluation_warnings--;
6744 in_sizeof--;
6745 mark_exp_read (expr.value);
6746 if (TREE_CODE (expr.value) == COMPONENT_REF
6747 && DECL_C_BIT_FIELD (TREE_OPERAND (expr.value, 1)))
6748 error_at (expr_loc, "%<sizeof%> applied to a bit-field");
6749 return c_expr_sizeof_expr (expr_loc, expr);
6750 }
6751 }
6752
6753 /* Parse an alignof expression. */
6754
6755 static struct c_expr
6756 c_parser_alignof_expression (c_parser *parser)
6757 {
6758 struct c_expr expr;
6759 location_t loc = c_parser_peek_token (parser)->location;
6760 tree alignof_spelling = c_parser_peek_token (parser)->value;
6761 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ALIGNOF));
6762 bool is_c11_alignof = strcmp (IDENTIFIER_POINTER (alignof_spelling),
6763 "_Alignof") == 0;
6764 /* A diagnostic is not required for the use of this identifier in
6765 the implementation namespace; only diagnose it for the C11
6766 spelling because of existing code using the other spellings. */
6767 if (is_c11_alignof)
6768 {
6769 if (flag_isoc99)
6770 pedwarn_c99 (loc, OPT_Wpedantic, "ISO C99 does not support %qE",
6771 alignof_spelling);
6772 else
6773 pedwarn_c99 (loc, OPT_Wpedantic, "ISO C90 does not support %qE",
6774 alignof_spelling);
6775 }
6776 c_parser_consume_token (parser);
6777 c_inhibit_evaluation_warnings++;
6778 in_alignof++;
6779 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
6780 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
6781 {
6782 /* Either __alignof__ ( type-name ) or __alignof__
6783 unary-expression starting with a compound literal. */
6784 location_t loc;
6785 struct c_type_name *type_name;
6786 struct c_expr ret;
6787 c_parser_consume_token (parser);
6788 loc = c_parser_peek_token (parser)->location;
6789 type_name = c_parser_type_name (parser);
6790 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
6791 if (type_name == NULL)
6792 {
6793 struct c_expr ret;
6794 c_inhibit_evaluation_warnings--;
6795 in_alignof--;
6796 ret.value = error_mark_node;
6797 ret.original_code = ERROR_MARK;
6798 ret.original_type = NULL;
6799 return ret;
6800 }
6801 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
6802 {
6803 expr = c_parser_postfix_expression_after_paren_type (parser,
6804 type_name,
6805 loc);
6806 goto alignof_expr;
6807 }
6808 /* alignof ( type-name ). */
6809 c_inhibit_evaluation_warnings--;
6810 in_alignof--;
6811 ret.value = c_sizeof_or_alignof_type (loc, groktypename (type_name,
6812 NULL, NULL),
6813 false, is_c11_alignof, 1);
6814 ret.original_code = ERROR_MARK;
6815 ret.original_type = NULL;
6816 return ret;
6817 }
6818 else
6819 {
6820 struct c_expr ret;
6821 expr = c_parser_unary_expression (parser);
6822 alignof_expr:
6823 mark_exp_read (expr.value);
6824 c_inhibit_evaluation_warnings--;
6825 in_alignof--;
6826 pedwarn (loc, OPT_Wpedantic, "ISO C does not allow %<%E (expression)%>",
6827 alignof_spelling);
6828 ret.value = c_alignof_expr (loc, expr.value);
6829 ret.original_code = ERROR_MARK;
6830 ret.original_type = NULL;
6831 return ret;
6832 }
6833 }
6834
6835 /* Helper function to read arguments of builtins which are interfaces
6836 for the middle-end nodes like COMPLEX_EXPR, VEC_PERM_EXPR and
6837 others. The name of the builtin is passed using BNAME parameter.
6838 Function returns true if there were no errors while parsing and
6839 stores the arguments in CEXPR_LIST. */
6840 static bool
6841 c_parser_get_builtin_args (c_parser *parser, const char *bname,
6842 vec<c_expr_t, va_gc> **ret_cexpr_list,
6843 bool choose_expr_p)
6844 {
6845 location_t loc = c_parser_peek_token (parser)->location;
6846 vec<c_expr_t, va_gc> *cexpr_list;
6847 c_expr_t expr;
6848 bool saved_force_folding_builtin_constant_p;
6849
6850 *ret_cexpr_list = NULL;
6851 if (c_parser_next_token_is_not (parser, CPP_OPEN_PAREN))
6852 {
6853 error_at (loc, "cannot take address of %qs", bname);
6854 return false;
6855 }
6856
6857 c_parser_consume_token (parser);
6858
6859 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
6860 {
6861 c_parser_consume_token (parser);
6862 return true;
6863 }
6864
6865 saved_force_folding_builtin_constant_p
6866 = force_folding_builtin_constant_p;
6867 force_folding_builtin_constant_p |= choose_expr_p;
6868 expr = c_parser_expr_no_commas (parser, NULL);
6869 force_folding_builtin_constant_p
6870 = saved_force_folding_builtin_constant_p;
6871 vec_alloc (cexpr_list, 1);
6872 vec_safe_push (cexpr_list, expr);
6873 while (c_parser_next_token_is (parser, CPP_COMMA))
6874 {
6875 c_parser_consume_token (parser);
6876 expr = c_parser_expr_no_commas (parser, NULL);
6877 vec_safe_push (cexpr_list, expr);
6878 }
6879
6880 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
6881 return false;
6882
6883 *ret_cexpr_list = cexpr_list;
6884 return true;
6885 }
6886
6887 /* This represents a single generic-association. */
6888
6889 struct c_generic_association
6890 {
6891 /* The location of the starting token of the type. */
6892 location_t type_location;
6893 /* The association's type, or NULL_TREE for 'default'. */
6894 tree type;
6895 /* The association's expression. */
6896 struct c_expr expression;
6897 };
6898
6899 /* Parse a generic-selection. (C11 6.5.1.1).
6900
6901 generic-selection:
6902 _Generic ( assignment-expression , generic-assoc-list )
6903
6904 generic-assoc-list:
6905 generic-association
6906 generic-assoc-list , generic-association
6907
6908 generic-association:
6909 type-name : assignment-expression
6910 default : assignment-expression
6911 */
6912
6913 static struct c_expr
6914 c_parser_generic_selection (c_parser *parser)
6915 {
6916 vec<c_generic_association> associations = vNULL;
6917 struct c_expr selector, error_expr;
6918 tree selector_type;
6919 struct c_generic_association matched_assoc;
6920 bool match_found = false;
6921 location_t generic_loc, selector_loc;
6922
6923 error_expr.original_code = ERROR_MARK;
6924 error_expr.original_type = NULL;
6925 error_expr.value = error_mark_node;
6926 matched_assoc.type_location = UNKNOWN_LOCATION;
6927 matched_assoc.type = NULL_TREE;
6928 matched_assoc.expression = error_expr;
6929
6930 gcc_assert (c_parser_next_token_is_keyword (parser, RID_GENERIC));
6931 generic_loc = c_parser_peek_token (parser)->location;
6932 c_parser_consume_token (parser);
6933 if (flag_isoc99)
6934 pedwarn_c99 (generic_loc, OPT_Wpedantic,
6935 "ISO C99 does not support %<_Generic%>");
6936 else
6937 pedwarn_c99 (generic_loc, OPT_Wpedantic,
6938 "ISO C90 does not support %<_Generic%>");
6939
6940 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6941 return error_expr;
6942
6943 c_inhibit_evaluation_warnings++;
6944 selector_loc = c_parser_peek_token (parser)->location;
6945 selector = c_parser_expr_no_commas (parser, NULL);
6946 selector = default_function_array_conversion (selector_loc, selector);
6947 c_inhibit_evaluation_warnings--;
6948
6949 if (selector.value == error_mark_node)
6950 {
6951 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6952 return selector;
6953 }
6954 selector_type = TREE_TYPE (selector.value);
6955 /* In ISO C terms, rvalues (including the controlling expression of
6956 _Generic) do not have qualified types. */
6957 if (TREE_CODE (selector_type) != ARRAY_TYPE)
6958 selector_type = TYPE_MAIN_VARIANT (selector_type);
6959 /* In ISO C terms, _Noreturn is not part of the type of expressions
6960 such as &abort, but in GCC it is represented internally as a type
6961 qualifier. */
6962 if (FUNCTION_POINTER_TYPE_P (selector_type)
6963 && TYPE_QUALS (TREE_TYPE (selector_type)) != TYPE_UNQUALIFIED)
6964 selector_type
6965 = build_pointer_type (TYPE_MAIN_VARIANT (TREE_TYPE (selector_type)));
6966
6967 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
6968 {
6969 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6970 return error_expr;
6971 }
6972
6973 while (1)
6974 {
6975 struct c_generic_association assoc, *iter;
6976 unsigned int ix;
6977 c_token *token = c_parser_peek_token (parser);
6978
6979 assoc.type_location = token->location;
6980 if (token->type == CPP_KEYWORD && token->keyword == RID_DEFAULT)
6981 {
6982 c_parser_consume_token (parser);
6983 assoc.type = NULL_TREE;
6984 }
6985 else
6986 {
6987 struct c_type_name *type_name;
6988
6989 type_name = c_parser_type_name (parser);
6990 if (type_name == NULL)
6991 {
6992 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6993 goto error_exit;
6994 }
6995 assoc.type = groktypename (type_name, NULL, NULL);
6996 if (assoc.type == error_mark_node)
6997 {
6998 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6999 goto error_exit;
7000 }
7001
7002 if (TREE_CODE (assoc.type) == FUNCTION_TYPE)
7003 error_at (assoc.type_location,
7004 "%<_Generic%> association has function type");
7005 else if (!COMPLETE_TYPE_P (assoc.type))
7006 error_at (assoc.type_location,
7007 "%<_Generic%> association has incomplete type");
7008
7009 if (variably_modified_type_p (assoc.type, NULL_TREE))
7010 error_at (assoc.type_location,
7011 "%<_Generic%> association has "
7012 "variable length type");
7013 }
7014
7015 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
7016 {
7017 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7018 goto error_exit;
7019 }
7020
7021 assoc.expression = c_parser_expr_no_commas (parser, NULL);
7022 if (assoc.expression.value == error_mark_node)
7023 {
7024 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7025 goto error_exit;
7026 }
7027
7028 for (ix = 0; associations.iterate (ix, &iter); ++ix)
7029 {
7030 if (assoc.type == NULL_TREE)
7031 {
7032 if (iter->type == NULL_TREE)
7033 {
7034 error_at (assoc.type_location,
7035 "duplicate %<default%> case in %<_Generic%>");
7036 inform (iter->type_location, "original %<default%> is here");
7037 }
7038 }
7039 else if (iter->type != NULL_TREE)
7040 {
7041 if (comptypes (assoc.type, iter->type))
7042 {
7043 error_at (assoc.type_location,
7044 "%<_Generic%> specifies two compatible types");
7045 inform (iter->type_location, "compatible type is here");
7046 }
7047 }
7048 }
7049
7050 if (assoc.type == NULL_TREE)
7051 {
7052 if (!match_found)
7053 {
7054 matched_assoc = assoc;
7055 match_found = true;
7056 }
7057 }
7058 else if (comptypes (assoc.type, selector_type))
7059 {
7060 if (!match_found || matched_assoc.type == NULL_TREE)
7061 {
7062 matched_assoc = assoc;
7063 match_found = true;
7064 }
7065 else
7066 {
7067 error_at (assoc.type_location,
7068 "%<_Generic> selector matches multiple associations");
7069 inform (matched_assoc.type_location,
7070 "other match is here");
7071 }
7072 }
7073
7074 associations.safe_push (assoc);
7075
7076 if (c_parser_peek_token (parser)->type != CPP_COMMA)
7077 break;
7078 c_parser_consume_token (parser);
7079 }
7080
7081 associations.release ();
7082
7083 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
7084 {
7085 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7086 return error_expr;
7087 }
7088
7089 if (!match_found)
7090 {
7091 error_at (selector_loc, "%<_Generic%> selector of type %qT is not "
7092 "compatible with any association",
7093 selector_type);
7094 return error_expr;
7095 }
7096
7097 return matched_assoc.expression;
7098
7099 error_exit:
7100 associations.release ();
7101 return error_expr;
7102 }
7103
7104 /* Parse a postfix expression (C90 6.3.1-6.3.2, C99 6.5.1-6.5.2).
7105
7106 postfix-expression:
7107 primary-expression
7108 postfix-expression [ expression ]
7109 postfix-expression ( argument-expression-list[opt] )
7110 postfix-expression . identifier
7111 postfix-expression -> identifier
7112 postfix-expression ++
7113 postfix-expression --
7114 ( type-name ) { initializer-list }
7115 ( type-name ) { initializer-list , }
7116
7117 argument-expression-list:
7118 argument-expression
7119 argument-expression-list , argument-expression
7120
7121 primary-expression:
7122 identifier
7123 constant
7124 string-literal
7125 ( expression )
7126 generic-selection
7127
7128 GNU extensions:
7129
7130 primary-expression:
7131 __func__
7132 (treated as a keyword in GNU C)
7133 __FUNCTION__
7134 __PRETTY_FUNCTION__
7135 ( compound-statement )
7136 __builtin_va_arg ( assignment-expression , type-name )
7137 __builtin_offsetof ( type-name , offsetof-member-designator )
7138 __builtin_choose_expr ( assignment-expression ,
7139 assignment-expression ,
7140 assignment-expression )
7141 __builtin_types_compatible_p ( type-name , type-name )
7142 __builtin_complex ( assignment-expression , assignment-expression )
7143 __builtin_shuffle ( assignment-expression , assignment-expression )
7144 __builtin_shuffle ( assignment-expression ,
7145 assignment-expression ,
7146 assignment-expression, )
7147
7148 offsetof-member-designator:
7149 identifier
7150 offsetof-member-designator . identifier
7151 offsetof-member-designator [ expression ]
7152
7153 Objective-C:
7154
7155 primary-expression:
7156 [ objc-receiver objc-message-args ]
7157 @selector ( objc-selector-arg )
7158 @protocol ( identifier )
7159 @encode ( type-name )
7160 objc-string-literal
7161 Classname . identifier
7162 */
7163
7164 static struct c_expr
7165 c_parser_postfix_expression (c_parser *parser)
7166 {
7167 struct c_expr expr, e1;
7168 struct c_type_name *t1, *t2;
7169 location_t loc = c_parser_peek_token (parser)->location;;
7170 expr.original_code = ERROR_MARK;
7171 expr.original_type = NULL;
7172 switch (c_parser_peek_token (parser)->type)
7173 {
7174 case CPP_NUMBER:
7175 expr.value = c_parser_peek_token (parser)->value;
7176 loc = c_parser_peek_token (parser)->location;
7177 c_parser_consume_token (parser);
7178 if (TREE_CODE (expr.value) == FIXED_CST
7179 && !targetm.fixed_point_supported_p ())
7180 {
7181 error_at (loc, "fixed-point types not supported for this target");
7182 expr.value = error_mark_node;
7183 }
7184 break;
7185 case CPP_CHAR:
7186 case CPP_CHAR16:
7187 case CPP_CHAR32:
7188 case CPP_WCHAR:
7189 expr.value = c_parser_peek_token (parser)->value;
7190 c_parser_consume_token (parser);
7191 break;
7192 case CPP_STRING:
7193 case CPP_STRING16:
7194 case CPP_STRING32:
7195 case CPP_WSTRING:
7196 case CPP_UTF8STRING:
7197 expr.value = c_parser_peek_token (parser)->value;
7198 expr.original_code = STRING_CST;
7199 c_parser_consume_token (parser);
7200 break;
7201 case CPP_OBJC_STRING:
7202 gcc_assert (c_dialect_objc ());
7203 expr.value
7204 = objc_build_string_object (c_parser_peek_token (parser)->value);
7205 c_parser_consume_token (parser);
7206 break;
7207 case CPP_NAME:
7208 switch (c_parser_peek_token (parser)->id_kind)
7209 {
7210 case C_ID_ID:
7211 {
7212 tree id = c_parser_peek_token (parser)->value;
7213 c_parser_consume_token (parser);
7214 expr.value = build_external_ref (loc, id,
7215 (c_parser_peek_token (parser)->type
7216 == CPP_OPEN_PAREN),
7217 &expr.original_type);
7218 break;
7219 }
7220 case C_ID_CLASSNAME:
7221 {
7222 /* Here we parse the Objective-C 2.0 Class.name dot
7223 syntax. */
7224 tree class_name = c_parser_peek_token (parser)->value;
7225 tree component;
7226 c_parser_consume_token (parser);
7227 gcc_assert (c_dialect_objc ());
7228 if (!c_parser_require (parser, CPP_DOT, "expected %<.%>"))
7229 {
7230 expr.value = error_mark_node;
7231 break;
7232 }
7233 if (c_parser_next_token_is_not (parser, CPP_NAME))
7234 {
7235 c_parser_error (parser, "expected identifier");
7236 expr.value = error_mark_node;
7237 break;
7238 }
7239 component = c_parser_peek_token (parser)->value;
7240 c_parser_consume_token (parser);
7241 expr.value = objc_build_class_component_ref (class_name,
7242 component);
7243 break;
7244 }
7245 default:
7246 c_parser_error (parser, "expected expression");
7247 expr.value = error_mark_node;
7248 break;
7249 }
7250 break;
7251 case CPP_OPEN_PAREN:
7252 /* A parenthesized expression, statement expression or compound
7253 literal. */
7254 if (c_parser_peek_2nd_token (parser)->type == CPP_OPEN_BRACE)
7255 {
7256 /* A statement expression. */
7257 tree stmt;
7258 location_t brace_loc;
7259 c_parser_consume_token (parser);
7260 brace_loc = c_parser_peek_token (parser)->location;
7261 c_parser_consume_token (parser);
7262 if (!building_stmt_list_p ())
7263 {
7264 error_at (loc, "braced-group within expression allowed "
7265 "only inside a function");
7266 parser->error = true;
7267 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
7268 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7269 expr.value = error_mark_node;
7270 break;
7271 }
7272 stmt = c_begin_stmt_expr ();
7273 c_parser_compound_statement_nostart (parser);
7274 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7275 "expected %<)%>");
7276 pedwarn (loc, OPT_Wpedantic,
7277 "ISO C forbids braced-groups within expressions");
7278 expr.value = c_finish_stmt_expr (brace_loc, stmt);
7279 mark_exp_read (expr.value);
7280 }
7281 else if (c_token_starts_typename (c_parser_peek_2nd_token (parser)))
7282 {
7283 /* A compound literal. ??? Can we actually get here rather
7284 than going directly to
7285 c_parser_postfix_expression_after_paren_type from
7286 elsewhere? */
7287 location_t loc;
7288 struct c_type_name *type_name;
7289 c_parser_consume_token (parser);
7290 loc = c_parser_peek_token (parser)->location;
7291 type_name = c_parser_type_name (parser);
7292 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7293 "expected %<)%>");
7294 if (type_name == NULL)
7295 {
7296 expr.value = error_mark_node;
7297 }
7298 else
7299 expr = c_parser_postfix_expression_after_paren_type (parser,
7300 type_name,
7301 loc);
7302 }
7303 else
7304 {
7305 /* A parenthesized expression. */
7306 c_parser_consume_token (parser);
7307 expr = c_parser_expression (parser);
7308 if (TREE_CODE (expr.value) == MODIFY_EXPR)
7309 TREE_NO_WARNING (expr.value) = 1;
7310 if (expr.original_code != C_MAYBE_CONST_EXPR)
7311 expr.original_code = ERROR_MARK;
7312 /* Don't change EXPR.ORIGINAL_TYPE. */
7313 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7314 "expected %<)%>");
7315 }
7316 break;
7317 case CPP_KEYWORD:
7318 switch (c_parser_peek_token (parser)->keyword)
7319 {
7320 case RID_FUNCTION_NAME:
7321 pedwarn (loc, OPT_Wpedantic, "ISO C does not support "
7322 "%<__FUNCTION__%> predefined identifier");
7323 expr.value = fname_decl (loc,
7324 c_parser_peek_token (parser)->keyword,
7325 c_parser_peek_token (parser)->value);
7326 c_parser_consume_token (parser);
7327 break;
7328 case RID_PRETTY_FUNCTION_NAME:
7329 pedwarn (loc, OPT_Wpedantic, "ISO C does not support "
7330 "%<__PRETTY_FUNCTION__%> predefined identifier");
7331 expr.value = fname_decl (loc,
7332 c_parser_peek_token (parser)->keyword,
7333 c_parser_peek_token (parser)->value);
7334 c_parser_consume_token (parser);
7335 break;
7336 case RID_C99_FUNCTION_NAME:
7337 pedwarn_c90 (loc, OPT_Wpedantic, "ISO C90 does not support "
7338 "%<__func__%> predefined identifier");
7339 expr.value = fname_decl (loc,
7340 c_parser_peek_token (parser)->keyword,
7341 c_parser_peek_token (parser)->value);
7342 c_parser_consume_token (parser);
7343 break;
7344 case RID_VA_ARG:
7345 c_parser_consume_token (parser);
7346 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7347 {
7348 expr.value = error_mark_node;
7349 break;
7350 }
7351 e1 = c_parser_expr_no_commas (parser, NULL);
7352 mark_exp_read (e1.value);
7353 e1.value = c_fully_fold (e1.value, false, NULL);
7354 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
7355 {
7356 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7357 expr.value = error_mark_node;
7358 break;
7359 }
7360 loc = c_parser_peek_token (parser)->location;
7361 t1 = c_parser_type_name (parser);
7362 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7363 "expected %<)%>");
7364 if (t1 == NULL)
7365 {
7366 expr.value = error_mark_node;
7367 }
7368 else
7369 {
7370 tree type_expr = NULL_TREE;
7371 expr.value = c_build_va_arg (loc, e1.value,
7372 groktypename (t1, &type_expr, NULL));
7373 if (type_expr)
7374 {
7375 expr.value = build2 (C_MAYBE_CONST_EXPR,
7376 TREE_TYPE (expr.value), type_expr,
7377 expr.value);
7378 C_MAYBE_CONST_EXPR_NON_CONST (expr.value) = true;
7379 }
7380 }
7381 break;
7382 case RID_OFFSETOF:
7383 c_parser_consume_token (parser);
7384 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7385 {
7386 expr.value = error_mark_node;
7387 break;
7388 }
7389 t1 = c_parser_type_name (parser);
7390 if (t1 == NULL)
7391 parser->error = true;
7392 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
7393 gcc_assert (parser->error);
7394 if (parser->error)
7395 {
7396 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7397 expr.value = error_mark_node;
7398 break;
7399 }
7400
7401 {
7402 tree type = groktypename (t1, NULL, NULL);
7403 tree offsetof_ref;
7404 if (type == error_mark_node)
7405 offsetof_ref = error_mark_node;
7406 else
7407 {
7408 offsetof_ref = build1 (INDIRECT_REF, type, null_pointer_node);
7409 SET_EXPR_LOCATION (offsetof_ref, loc);
7410 }
7411 /* Parse the second argument to __builtin_offsetof. We
7412 must have one identifier, and beyond that we want to
7413 accept sub structure and sub array references. */
7414 if (c_parser_next_token_is (parser, CPP_NAME))
7415 {
7416 offsetof_ref = build_component_ref
7417 (loc, offsetof_ref, c_parser_peek_token (parser)->value);
7418 c_parser_consume_token (parser);
7419 while (c_parser_next_token_is (parser, CPP_DOT)
7420 || c_parser_next_token_is (parser,
7421 CPP_OPEN_SQUARE)
7422 || c_parser_next_token_is (parser,
7423 CPP_DEREF))
7424 {
7425 if (c_parser_next_token_is (parser, CPP_DEREF))
7426 {
7427 loc = c_parser_peek_token (parser)->location;
7428 offsetof_ref = build_array_ref (loc,
7429 offsetof_ref,
7430 integer_zero_node);
7431 goto do_dot;
7432 }
7433 else if (c_parser_next_token_is (parser, CPP_DOT))
7434 {
7435 do_dot:
7436 c_parser_consume_token (parser);
7437 if (c_parser_next_token_is_not (parser,
7438 CPP_NAME))
7439 {
7440 c_parser_error (parser, "expected identifier");
7441 break;
7442 }
7443 offsetof_ref = build_component_ref
7444 (loc, offsetof_ref,
7445 c_parser_peek_token (parser)->value);
7446 c_parser_consume_token (parser);
7447 }
7448 else
7449 {
7450 struct c_expr ce;
7451 tree idx;
7452 loc = c_parser_peek_token (parser)->location;
7453 c_parser_consume_token (parser);
7454 ce = c_parser_expression (parser);
7455 ce = convert_lvalue_to_rvalue (loc, ce, false, false);
7456 idx = ce.value;
7457 idx = c_fully_fold (idx, false, NULL);
7458 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
7459 "expected %<]%>");
7460 offsetof_ref = build_array_ref (loc, offsetof_ref, idx);
7461 }
7462 }
7463 }
7464 else
7465 c_parser_error (parser, "expected identifier");
7466 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7467 "expected %<)%>");
7468 expr.value = fold_offsetof (offsetof_ref);
7469 }
7470 break;
7471 case RID_CHOOSE_EXPR:
7472 {
7473 vec<c_expr_t, va_gc> *cexpr_list;
7474 c_expr_t *e1_p, *e2_p, *e3_p;
7475 tree c;
7476
7477 c_parser_consume_token (parser);
7478 if (!c_parser_get_builtin_args (parser,
7479 "__builtin_choose_expr",
7480 &cexpr_list, true))
7481 {
7482 expr.value = error_mark_node;
7483 break;
7484 }
7485
7486 if (vec_safe_length (cexpr_list) != 3)
7487 {
7488 error_at (loc, "wrong number of arguments to "
7489 "%<__builtin_choose_expr%>");
7490 expr.value = error_mark_node;
7491 break;
7492 }
7493
7494 e1_p = &(*cexpr_list)[0];
7495 e2_p = &(*cexpr_list)[1];
7496 e3_p = &(*cexpr_list)[2];
7497
7498 c = e1_p->value;
7499 mark_exp_read (e2_p->value);
7500 mark_exp_read (e3_p->value);
7501 if (TREE_CODE (c) != INTEGER_CST
7502 || !INTEGRAL_TYPE_P (TREE_TYPE (c)))
7503 error_at (loc,
7504 "first argument to %<__builtin_choose_expr%> not"
7505 " a constant");
7506 constant_expression_warning (c);
7507 expr = integer_zerop (c) ? *e3_p : *e2_p;
7508 break;
7509 }
7510 case RID_TYPES_COMPATIBLE_P:
7511 c_parser_consume_token (parser);
7512 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7513 {
7514 expr.value = error_mark_node;
7515 break;
7516 }
7517 t1 = c_parser_type_name (parser);
7518 if (t1 == NULL)
7519 {
7520 expr.value = error_mark_node;
7521 break;
7522 }
7523 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
7524 {
7525 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7526 expr.value = error_mark_node;
7527 break;
7528 }
7529 t2 = c_parser_type_name (parser);
7530 if (t2 == NULL)
7531 {
7532 expr.value = error_mark_node;
7533 break;
7534 }
7535 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7536 "expected %<)%>");
7537 {
7538 tree e1, e2;
7539 e1 = groktypename (t1, NULL, NULL);
7540 e2 = groktypename (t2, NULL, NULL);
7541 if (e1 == error_mark_node || e2 == error_mark_node)
7542 {
7543 expr.value = error_mark_node;
7544 break;
7545 }
7546
7547 e1 = TYPE_MAIN_VARIANT (e1);
7548 e2 = TYPE_MAIN_VARIANT (e2);
7549
7550 expr.value
7551 = comptypes (e1, e2) ? integer_one_node : integer_zero_node;
7552 }
7553 break;
7554 case RID_BUILTIN_CALL_WITH_STATIC_CHAIN:
7555 {
7556 vec<c_expr_t, va_gc> *cexpr_list;
7557 c_expr_t *e2_p;
7558 tree chain_value;
7559
7560 c_parser_consume_token (parser);
7561 if (!c_parser_get_builtin_args (parser,
7562 "__builtin_call_with_static_chain",
7563 &cexpr_list, false))
7564 {
7565 expr.value = error_mark_node;
7566 break;
7567 }
7568 if (vec_safe_length (cexpr_list) != 2)
7569 {
7570 error_at (loc, "wrong number of arguments to "
7571 "%<__builtin_call_with_static_chain%>");
7572 expr.value = error_mark_node;
7573 break;
7574 }
7575
7576 expr = (*cexpr_list)[0];
7577 e2_p = &(*cexpr_list)[1];
7578 *e2_p = convert_lvalue_to_rvalue (loc, *e2_p, true, true);
7579 chain_value = e2_p->value;
7580 mark_exp_read (chain_value);
7581
7582 if (TREE_CODE (expr.value) != CALL_EXPR)
7583 error_at (loc, "first argument to "
7584 "%<__builtin_call_with_static_chain%> "
7585 "must be a call expression");
7586 else if (TREE_CODE (TREE_TYPE (chain_value)) != POINTER_TYPE)
7587 error_at (loc, "second argument to "
7588 "%<__builtin_call_with_static_chain%> "
7589 "must be a pointer type");
7590 else
7591 CALL_EXPR_STATIC_CHAIN (expr.value) = chain_value;
7592 break;
7593 }
7594 case RID_BUILTIN_COMPLEX:
7595 {
7596 vec<c_expr_t, va_gc> *cexpr_list;
7597 c_expr_t *e1_p, *e2_p;
7598
7599 c_parser_consume_token (parser);
7600 if (!c_parser_get_builtin_args (parser,
7601 "__builtin_complex",
7602 &cexpr_list, false))
7603 {
7604 expr.value = error_mark_node;
7605 break;
7606 }
7607
7608 if (vec_safe_length (cexpr_list) != 2)
7609 {
7610 error_at (loc, "wrong number of arguments to "
7611 "%<__builtin_complex%>");
7612 expr.value = error_mark_node;
7613 break;
7614 }
7615
7616 e1_p = &(*cexpr_list)[0];
7617 e2_p = &(*cexpr_list)[1];
7618
7619 *e1_p = convert_lvalue_to_rvalue (loc, *e1_p, true, true);
7620 if (TREE_CODE (e1_p->value) == EXCESS_PRECISION_EXPR)
7621 e1_p->value = convert (TREE_TYPE (e1_p->value),
7622 TREE_OPERAND (e1_p->value, 0));
7623 *e2_p = convert_lvalue_to_rvalue (loc, *e2_p, true, true);
7624 if (TREE_CODE (e2_p->value) == EXCESS_PRECISION_EXPR)
7625 e2_p->value = convert (TREE_TYPE (e2_p->value),
7626 TREE_OPERAND (e2_p->value, 0));
7627 if (!SCALAR_FLOAT_TYPE_P (TREE_TYPE (e1_p->value))
7628 || DECIMAL_FLOAT_TYPE_P (TREE_TYPE (e1_p->value))
7629 || !SCALAR_FLOAT_TYPE_P (TREE_TYPE (e2_p->value))
7630 || DECIMAL_FLOAT_TYPE_P (TREE_TYPE (e2_p->value)))
7631 {
7632 error_at (loc, "%<__builtin_complex%> operand "
7633 "not of real binary floating-point type");
7634 expr.value = error_mark_node;
7635 break;
7636 }
7637 if (TYPE_MAIN_VARIANT (TREE_TYPE (e1_p->value))
7638 != TYPE_MAIN_VARIANT (TREE_TYPE (e2_p->value)))
7639 {
7640 error_at (loc,
7641 "%<__builtin_complex%> operands of different types");
7642 expr.value = error_mark_node;
7643 break;
7644 }
7645 pedwarn_c90 (loc, OPT_Wpedantic,
7646 "ISO C90 does not support complex types");
7647 expr.value = build2 (COMPLEX_EXPR,
7648 build_complex_type
7649 (TYPE_MAIN_VARIANT
7650 (TREE_TYPE (e1_p->value))),
7651 e1_p->value, e2_p->value);
7652 break;
7653 }
7654 case RID_BUILTIN_SHUFFLE:
7655 {
7656 vec<c_expr_t, va_gc> *cexpr_list;
7657 unsigned int i;
7658 c_expr_t *p;
7659
7660 c_parser_consume_token (parser);
7661 if (!c_parser_get_builtin_args (parser,
7662 "__builtin_shuffle",
7663 &cexpr_list, false))
7664 {
7665 expr.value = error_mark_node;
7666 break;
7667 }
7668
7669 FOR_EACH_VEC_SAFE_ELT (cexpr_list, i, p)
7670 *p = convert_lvalue_to_rvalue (loc, *p, true, true);
7671
7672 if (vec_safe_length (cexpr_list) == 2)
7673 expr.value =
7674 c_build_vec_perm_expr
7675 (loc, (*cexpr_list)[0].value,
7676 NULL_TREE, (*cexpr_list)[1].value);
7677
7678 else if (vec_safe_length (cexpr_list) == 3)
7679 expr.value =
7680 c_build_vec_perm_expr
7681 (loc, (*cexpr_list)[0].value,
7682 (*cexpr_list)[1].value,
7683 (*cexpr_list)[2].value);
7684 else
7685 {
7686 error_at (loc, "wrong number of arguments to "
7687 "%<__builtin_shuffle%>");
7688 expr.value = error_mark_node;
7689 }
7690 break;
7691 }
7692 case RID_AT_SELECTOR:
7693 gcc_assert (c_dialect_objc ());
7694 c_parser_consume_token (parser);
7695 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7696 {
7697 expr.value = error_mark_node;
7698 break;
7699 }
7700 {
7701 tree sel = c_parser_objc_selector_arg (parser);
7702 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7703 "expected %<)%>");
7704 expr.value = objc_build_selector_expr (loc, sel);
7705 }
7706 break;
7707 case RID_AT_PROTOCOL:
7708 gcc_assert (c_dialect_objc ());
7709 c_parser_consume_token (parser);
7710 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7711 {
7712 expr.value = error_mark_node;
7713 break;
7714 }
7715 if (c_parser_next_token_is_not (parser, CPP_NAME))
7716 {
7717 c_parser_error (parser, "expected identifier");
7718 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7719 expr.value = error_mark_node;
7720 break;
7721 }
7722 {
7723 tree id = c_parser_peek_token (parser)->value;
7724 c_parser_consume_token (parser);
7725 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7726 "expected %<)%>");
7727 expr.value = objc_build_protocol_expr (id);
7728 }
7729 break;
7730 case RID_AT_ENCODE:
7731 /* Extension to support C-structures in the archiver. */
7732 gcc_assert (c_dialect_objc ());
7733 c_parser_consume_token (parser);
7734 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7735 {
7736 expr.value = error_mark_node;
7737 break;
7738 }
7739 t1 = c_parser_type_name (parser);
7740 if (t1 == NULL)
7741 {
7742 expr.value = error_mark_node;
7743 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7744 break;
7745 }
7746 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7747 "expected %<)%>");
7748 {
7749 tree type = groktypename (t1, NULL, NULL);
7750 expr.value = objc_build_encode_expr (type);
7751 }
7752 break;
7753 case RID_GENERIC:
7754 expr = c_parser_generic_selection (parser);
7755 break;
7756 case RID_CILK_SPAWN:
7757 c_parser_consume_token (parser);
7758 if (!flag_cilkplus)
7759 {
7760 error_at (loc, "-fcilkplus must be enabled to use "
7761 "%<_Cilk_spawn%>");
7762 expr = c_parser_postfix_expression (parser);
7763 expr.value = error_mark_node;
7764 }
7765 else if (c_parser_peek_token (parser)->keyword == RID_CILK_SPAWN)
7766 {
7767 error_at (loc, "consecutive %<_Cilk_spawn%> keywords "
7768 "are not permitted");
7769 /* Now flush out all the _Cilk_spawns. */
7770 while (c_parser_peek_token (parser)->keyword == RID_CILK_SPAWN)
7771 c_parser_consume_token (parser);
7772 expr = c_parser_postfix_expression (parser);
7773 }
7774 else
7775 {
7776 expr = c_parser_postfix_expression (parser);
7777 expr.value = build_cilk_spawn (loc, expr.value);
7778 }
7779 break;
7780 default:
7781 c_parser_error (parser, "expected expression");
7782 expr.value = error_mark_node;
7783 break;
7784 }
7785 break;
7786 case CPP_OPEN_SQUARE:
7787 if (c_dialect_objc ())
7788 {
7789 tree receiver, args;
7790 c_parser_consume_token (parser);
7791 receiver = c_parser_objc_receiver (parser);
7792 args = c_parser_objc_message_args (parser);
7793 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
7794 "expected %<]%>");
7795 expr.value = objc_build_message_expr (receiver, args);
7796 break;
7797 }
7798 /* Else fall through to report error. */
7799 default:
7800 c_parser_error (parser, "expected expression");
7801 expr.value = error_mark_node;
7802 break;
7803 }
7804 return c_parser_postfix_expression_after_primary (parser, loc, expr);
7805 }
7806
7807 /* Parse a postfix expression after a parenthesized type name: the
7808 brace-enclosed initializer of a compound literal, possibly followed
7809 by some postfix operators. This is separate because it is not
7810 possible to tell until after the type name whether a cast
7811 expression has a cast or a compound literal, or whether the operand
7812 of sizeof is a parenthesized type name or starts with a compound
7813 literal. TYPE_LOC is the location where TYPE_NAME starts--the
7814 location of the first token after the parentheses around the type
7815 name. */
7816
7817 static struct c_expr
7818 c_parser_postfix_expression_after_paren_type (c_parser *parser,
7819 struct c_type_name *type_name,
7820 location_t type_loc)
7821 {
7822 tree type;
7823 struct c_expr init;
7824 bool non_const;
7825 struct c_expr expr;
7826 location_t start_loc;
7827 tree type_expr = NULL_TREE;
7828 bool type_expr_const = true;
7829 check_compound_literal_type (type_loc, type_name);
7830 start_init (NULL_TREE, NULL, 0);
7831 type = groktypename (type_name, &type_expr, &type_expr_const);
7832 start_loc = c_parser_peek_token (parser)->location;
7833 if (type != error_mark_node && C_TYPE_VARIABLE_SIZE (type))
7834 {
7835 error_at (type_loc, "compound literal has variable size");
7836 type = error_mark_node;
7837 }
7838 init = c_parser_braced_init (parser, type, false);
7839 finish_init ();
7840 maybe_warn_string_init (type_loc, type, init);
7841
7842 if (type != error_mark_node
7843 && !ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (type))
7844 && current_function_decl)
7845 {
7846 error ("compound literal qualified by address-space qualifier");
7847 type = error_mark_node;
7848 }
7849
7850 pedwarn_c90 (start_loc, OPT_Wpedantic, "ISO C90 forbids compound literals");
7851 non_const = ((init.value && TREE_CODE (init.value) == CONSTRUCTOR)
7852 ? CONSTRUCTOR_NON_CONST (init.value)
7853 : init.original_code == C_MAYBE_CONST_EXPR);
7854 non_const |= !type_expr_const;
7855 expr.value = build_compound_literal (start_loc, type, init.value, non_const);
7856 expr.original_code = ERROR_MARK;
7857 expr.original_type = NULL;
7858 if (type != error_mark_node && type_expr)
7859 {
7860 if (TREE_CODE (expr.value) == C_MAYBE_CONST_EXPR)
7861 {
7862 gcc_assert (C_MAYBE_CONST_EXPR_PRE (expr.value) == NULL_TREE);
7863 C_MAYBE_CONST_EXPR_PRE (expr.value) = type_expr;
7864 }
7865 else
7866 {
7867 gcc_assert (!non_const);
7868 expr.value = build2 (C_MAYBE_CONST_EXPR, type,
7869 type_expr, expr.value);
7870 }
7871 }
7872 return c_parser_postfix_expression_after_primary (parser, start_loc, expr);
7873 }
7874
7875 /* Callback function for sizeof_pointer_memaccess_warning to compare
7876 types. */
7877
7878 static bool
7879 sizeof_ptr_memacc_comptypes (tree type1, tree type2)
7880 {
7881 return comptypes (type1, type2) == 1;
7882 }
7883
7884 /* Parse a postfix expression after the initial primary or compound
7885 literal; that is, parse a series of postfix operators.
7886
7887 EXPR_LOC is the location of the primary expression. */
7888
7889 static struct c_expr
7890 c_parser_postfix_expression_after_primary (c_parser *parser,
7891 location_t expr_loc,
7892 struct c_expr expr)
7893 {
7894 struct c_expr orig_expr;
7895 tree ident, idx;
7896 location_t sizeof_arg_loc[3];
7897 tree sizeof_arg[3];
7898 unsigned int literal_zero_mask;
7899 unsigned int i;
7900 vec<tree, va_gc> *exprlist;
7901 vec<tree, va_gc> *origtypes = NULL;
7902 vec<location_t> arg_loc = vNULL;
7903
7904 while (true)
7905 {
7906 location_t op_loc = c_parser_peek_token (parser)->location;
7907 switch (c_parser_peek_token (parser)->type)
7908 {
7909 case CPP_OPEN_SQUARE:
7910 /* Array reference. */
7911 c_parser_consume_token (parser);
7912 if (flag_cilkplus
7913 && c_parser_peek_token (parser)->type == CPP_COLON)
7914 /* If we are here, then we have something like this:
7915 Array [ : ]
7916 */
7917 expr.value = c_parser_array_notation (expr_loc, parser, NULL_TREE,
7918 expr.value);
7919 else
7920 {
7921 idx = c_parser_expression (parser).value;
7922 /* Here we have 3 options:
7923 1. Array [EXPR] -- Normal Array call.
7924 2. Array [EXPR : EXPR] -- Array notation without stride.
7925 3. Array [EXPR : EXPR : EXPR] -- Array notation with stride.
7926
7927 For 1, we just handle it just like a normal array expression.
7928 For 2 and 3 we handle it like we handle array notations. The
7929 idx value we have above becomes the initial/start index.
7930 */
7931 if (flag_cilkplus
7932 && c_parser_peek_token (parser)->type == CPP_COLON)
7933 expr.value = c_parser_array_notation (expr_loc, parser, idx,
7934 expr.value);
7935 else
7936 {
7937 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
7938 "expected %<]%>");
7939 expr.value = build_array_ref (op_loc, expr.value, idx);
7940 }
7941 }
7942 expr.original_code = ERROR_MARK;
7943 expr.original_type = NULL;
7944 break;
7945 case CPP_OPEN_PAREN:
7946 /* Function call. */
7947 c_parser_consume_token (parser);
7948 for (i = 0; i < 3; i++)
7949 {
7950 sizeof_arg[i] = NULL_TREE;
7951 sizeof_arg_loc[i] = UNKNOWN_LOCATION;
7952 }
7953 literal_zero_mask = 0;
7954 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
7955 exprlist = NULL;
7956 else
7957 exprlist = c_parser_expr_list (parser, true, false, &origtypes,
7958 sizeof_arg_loc, sizeof_arg,
7959 &arg_loc, &literal_zero_mask);
7960 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7961 "expected %<)%>");
7962 orig_expr = expr;
7963 mark_exp_read (expr.value);
7964 if (warn_sizeof_pointer_memaccess)
7965 sizeof_pointer_memaccess_warning (sizeof_arg_loc,
7966 expr.value, exprlist,
7967 sizeof_arg,
7968 sizeof_ptr_memacc_comptypes);
7969 if (warn_memset_transposed_args
7970 && TREE_CODE (expr.value) == FUNCTION_DECL
7971 && DECL_BUILT_IN_CLASS (expr.value) == BUILT_IN_NORMAL
7972 && DECL_FUNCTION_CODE (expr.value) == BUILT_IN_MEMSET
7973 && vec_safe_length (exprlist) == 3
7974 && integer_zerop ((*exprlist)[2])
7975 && (literal_zero_mask & (1 << 2)) != 0
7976 && (!integer_zerop ((*exprlist)[1])
7977 || (literal_zero_mask & (1 << 1)) == 0))
7978 warning_at (expr_loc, OPT_Wmemset_transposed_args,
7979 "%<memset%> used with constant zero length parameter; "
7980 "this could be due to transposed parameters");
7981
7982 expr.value
7983 = c_build_function_call_vec (expr_loc, arg_loc, expr.value,
7984 exprlist, origtypes);
7985 expr.original_code = ERROR_MARK;
7986 if (TREE_CODE (expr.value) == INTEGER_CST
7987 && TREE_CODE (orig_expr.value) == FUNCTION_DECL
7988 && DECL_BUILT_IN_CLASS (orig_expr.value) == BUILT_IN_NORMAL
7989 && DECL_FUNCTION_CODE (orig_expr.value) == BUILT_IN_CONSTANT_P)
7990 expr.original_code = C_MAYBE_CONST_EXPR;
7991 expr.original_type = NULL;
7992 if (exprlist)
7993 {
7994 release_tree_vector (exprlist);
7995 release_tree_vector (origtypes);
7996 }
7997 arg_loc.release ();
7998 break;
7999 case CPP_DOT:
8000 /* Structure element reference. */
8001 c_parser_consume_token (parser);
8002 expr = default_function_array_conversion (expr_loc, expr);
8003 if (c_parser_next_token_is (parser, CPP_NAME))
8004 ident = c_parser_peek_token (parser)->value;
8005 else
8006 {
8007 c_parser_error (parser, "expected identifier");
8008 expr.value = error_mark_node;
8009 expr.original_code = ERROR_MARK;
8010 expr.original_type = NULL;
8011 return expr;
8012 }
8013 c_parser_consume_token (parser);
8014 expr.value = build_component_ref (op_loc, expr.value, ident);
8015 expr.original_code = ERROR_MARK;
8016 if (TREE_CODE (expr.value) != COMPONENT_REF)
8017 expr.original_type = NULL;
8018 else
8019 {
8020 /* Remember the original type of a bitfield. */
8021 tree field = TREE_OPERAND (expr.value, 1);
8022 if (TREE_CODE (field) != FIELD_DECL)
8023 expr.original_type = NULL;
8024 else
8025 expr.original_type = DECL_BIT_FIELD_TYPE (field);
8026 }
8027 break;
8028 case CPP_DEREF:
8029 /* Structure element reference. */
8030 c_parser_consume_token (parser);
8031 expr = convert_lvalue_to_rvalue (expr_loc, expr, true, false);
8032 if (c_parser_next_token_is (parser, CPP_NAME))
8033 ident = c_parser_peek_token (parser)->value;
8034 else
8035 {
8036 c_parser_error (parser, "expected identifier");
8037 expr.value = error_mark_node;
8038 expr.original_code = ERROR_MARK;
8039 expr.original_type = NULL;
8040 return expr;
8041 }
8042 c_parser_consume_token (parser);
8043 expr.value = build_component_ref (op_loc,
8044 build_indirect_ref (op_loc,
8045 expr.value,
8046 RO_ARROW),
8047 ident);
8048 expr.original_code = ERROR_MARK;
8049 if (TREE_CODE (expr.value) != COMPONENT_REF)
8050 expr.original_type = NULL;
8051 else
8052 {
8053 /* Remember the original type of a bitfield. */
8054 tree field = TREE_OPERAND (expr.value, 1);
8055 if (TREE_CODE (field) != FIELD_DECL)
8056 expr.original_type = NULL;
8057 else
8058 expr.original_type = DECL_BIT_FIELD_TYPE (field);
8059 }
8060 break;
8061 case CPP_PLUS_PLUS:
8062 /* Postincrement. */
8063 c_parser_consume_token (parser);
8064 /* If the expressions have array notations, we expand them. */
8065 if (flag_cilkplus
8066 && TREE_CODE (expr.value) == ARRAY_NOTATION_REF)
8067 expr = fix_array_notation_expr (expr_loc, POSTINCREMENT_EXPR, expr);
8068 else
8069 {
8070 expr = default_function_array_read_conversion (expr_loc, expr);
8071 expr.value = build_unary_op (op_loc,
8072 POSTINCREMENT_EXPR, expr.value, 0);
8073 }
8074 expr.original_code = ERROR_MARK;
8075 expr.original_type = NULL;
8076 break;
8077 case CPP_MINUS_MINUS:
8078 /* Postdecrement. */
8079 c_parser_consume_token (parser);
8080 /* If the expressions have array notations, we expand them. */
8081 if (flag_cilkplus
8082 && TREE_CODE (expr.value) == ARRAY_NOTATION_REF)
8083 expr = fix_array_notation_expr (expr_loc, POSTDECREMENT_EXPR, expr);
8084 else
8085 {
8086 expr = default_function_array_read_conversion (expr_loc, expr);
8087 expr.value = build_unary_op (op_loc,
8088 POSTDECREMENT_EXPR, expr.value, 0);
8089 }
8090 expr.original_code = ERROR_MARK;
8091 expr.original_type = NULL;
8092 break;
8093 default:
8094 return expr;
8095 }
8096 }
8097 }
8098
8099 /* Parse an expression (C90 6.3.17, C99 6.5.17).
8100
8101 expression:
8102 assignment-expression
8103 expression , assignment-expression
8104 */
8105
8106 static struct c_expr
8107 c_parser_expression (c_parser *parser)
8108 {
8109 location_t tloc = c_parser_peek_token (parser)->location;
8110 struct c_expr expr;
8111 expr = c_parser_expr_no_commas (parser, NULL);
8112 if (c_parser_next_token_is (parser, CPP_COMMA))
8113 expr = convert_lvalue_to_rvalue (tloc, expr, true, false);
8114 while (c_parser_next_token_is (parser, CPP_COMMA))
8115 {
8116 struct c_expr next;
8117 tree lhsval;
8118 location_t loc = c_parser_peek_token (parser)->location;
8119 location_t expr_loc;
8120 c_parser_consume_token (parser);
8121 expr_loc = c_parser_peek_token (parser)->location;
8122 lhsval = expr.value;
8123 while (TREE_CODE (lhsval) == COMPOUND_EXPR)
8124 lhsval = TREE_OPERAND (lhsval, 1);
8125 if (DECL_P (lhsval) || handled_component_p (lhsval))
8126 mark_exp_read (lhsval);
8127 next = c_parser_expr_no_commas (parser, NULL);
8128 next = convert_lvalue_to_rvalue (expr_loc, next, true, false);
8129 expr.value = build_compound_expr (loc, expr.value, next.value);
8130 expr.original_code = COMPOUND_EXPR;
8131 expr.original_type = next.original_type;
8132 }
8133 return expr;
8134 }
8135
8136 /* Parse an expression and convert functions or arrays to pointers and
8137 lvalues to rvalues. */
8138
8139 static struct c_expr
8140 c_parser_expression_conv (c_parser *parser)
8141 {
8142 struct c_expr expr;
8143 location_t loc = c_parser_peek_token (parser)->location;
8144 expr = c_parser_expression (parser);
8145 expr = convert_lvalue_to_rvalue (loc, expr, true, false);
8146 return expr;
8147 }
8148
8149 /* Helper function of c_parser_expr_list. Check if IDXth (0 based)
8150 argument is a literal zero alone and if so, set it in literal_zero_mask. */
8151
8152 static inline void
8153 c_parser_check_literal_zero (c_parser *parser, unsigned *literal_zero_mask,
8154 unsigned int idx)
8155 {
8156 if (idx >= HOST_BITS_PER_INT)
8157 return;
8158
8159 c_token *tok = c_parser_peek_token (parser);
8160 switch (tok->type)
8161 {
8162 case CPP_NUMBER:
8163 case CPP_CHAR:
8164 case CPP_WCHAR:
8165 case CPP_CHAR16:
8166 case CPP_CHAR32:
8167 /* If a parameter is literal zero alone, remember it
8168 for -Wmemset-transposed-args warning. */
8169 if (integer_zerop (tok->value)
8170 && !TREE_OVERFLOW (tok->value)
8171 && (c_parser_peek_2nd_token (parser)->type == CPP_COMMA
8172 || c_parser_peek_2nd_token (parser)->type == CPP_CLOSE_PAREN))
8173 *literal_zero_mask |= 1U << idx;
8174 default:
8175 break;
8176 }
8177 }
8178
8179 /* Parse a non-empty list of expressions. If CONVERT_P, convert
8180 functions and arrays to pointers and lvalues to rvalues. If
8181 FOLD_P, fold the expressions. If LOCATIONS is non-NULL, save the
8182 locations of function arguments into this vector.
8183
8184 nonempty-expr-list:
8185 assignment-expression
8186 nonempty-expr-list , assignment-expression
8187 */
8188
8189 static vec<tree, va_gc> *
8190 c_parser_expr_list (c_parser *parser, bool convert_p, bool fold_p,
8191 vec<tree, va_gc> **p_orig_types,
8192 location_t *sizeof_arg_loc, tree *sizeof_arg,
8193 vec<location_t> *locations,
8194 unsigned int *literal_zero_mask)
8195 {
8196 vec<tree, va_gc> *ret;
8197 vec<tree, va_gc> *orig_types;
8198 struct c_expr expr;
8199 location_t loc = c_parser_peek_token (parser)->location;
8200 location_t cur_sizeof_arg_loc = UNKNOWN_LOCATION;
8201 unsigned int idx = 0;
8202
8203 ret = make_tree_vector ();
8204 if (p_orig_types == NULL)
8205 orig_types = NULL;
8206 else
8207 orig_types = make_tree_vector ();
8208
8209 if (sizeof_arg != NULL
8210 && c_parser_next_token_is_keyword (parser, RID_SIZEOF))
8211 cur_sizeof_arg_loc = c_parser_peek_2nd_token (parser)->location;
8212 if (literal_zero_mask)
8213 c_parser_check_literal_zero (parser, literal_zero_mask, 0);
8214 expr = c_parser_expr_no_commas (parser, NULL);
8215 if (convert_p)
8216 expr = convert_lvalue_to_rvalue (loc, expr, true, true);
8217 if (fold_p)
8218 expr.value = c_fully_fold (expr.value, false, NULL);
8219 ret->quick_push (expr.value);
8220 if (orig_types)
8221 orig_types->quick_push (expr.original_type);
8222 if (locations)
8223 locations->safe_push (loc);
8224 if (sizeof_arg != NULL
8225 && cur_sizeof_arg_loc != UNKNOWN_LOCATION
8226 && expr.original_code == SIZEOF_EXPR)
8227 {
8228 sizeof_arg[0] = c_last_sizeof_arg;
8229 sizeof_arg_loc[0] = cur_sizeof_arg_loc;
8230 }
8231 while (c_parser_next_token_is (parser, CPP_COMMA))
8232 {
8233 c_parser_consume_token (parser);
8234 loc = c_parser_peek_token (parser)->location;
8235 if (sizeof_arg != NULL
8236 && c_parser_next_token_is_keyword (parser, RID_SIZEOF))
8237 cur_sizeof_arg_loc = c_parser_peek_2nd_token (parser)->location;
8238 else
8239 cur_sizeof_arg_loc = UNKNOWN_LOCATION;
8240 if (literal_zero_mask)
8241 c_parser_check_literal_zero (parser, literal_zero_mask, idx + 1);
8242 expr = c_parser_expr_no_commas (parser, NULL);
8243 if (convert_p)
8244 expr = convert_lvalue_to_rvalue (loc, expr, true, true);
8245 if (fold_p)
8246 expr.value = c_fully_fold (expr.value, false, NULL);
8247 vec_safe_push (ret, expr.value);
8248 if (orig_types)
8249 vec_safe_push (orig_types, expr.original_type);
8250 if (locations)
8251 locations->safe_push (loc);
8252 if (++idx < 3
8253 && sizeof_arg != NULL
8254 && cur_sizeof_arg_loc != UNKNOWN_LOCATION
8255 && expr.original_code == SIZEOF_EXPR)
8256 {
8257 sizeof_arg[idx] = c_last_sizeof_arg;
8258 sizeof_arg_loc[idx] = cur_sizeof_arg_loc;
8259 }
8260 }
8261 if (orig_types)
8262 *p_orig_types = orig_types;
8263 return ret;
8264 }
8265 \f
8266 /* Parse Objective-C-specific constructs. */
8267
8268 /* Parse an objc-class-definition.
8269
8270 objc-class-definition:
8271 @interface identifier objc-superclass[opt] objc-protocol-refs[opt]
8272 objc-class-instance-variables[opt] objc-methodprotolist @end
8273 @implementation identifier objc-superclass[opt]
8274 objc-class-instance-variables[opt]
8275 @interface identifier ( identifier ) objc-protocol-refs[opt]
8276 objc-methodprotolist @end
8277 @interface identifier ( ) objc-protocol-refs[opt]
8278 objc-methodprotolist @end
8279 @implementation identifier ( identifier )
8280
8281 objc-superclass:
8282 : identifier
8283
8284 "@interface identifier (" must start "@interface identifier (
8285 identifier ) ...": objc-methodprotolist in the first production may
8286 not start with a parenthesized identifier as a declarator of a data
8287 definition with no declaration specifiers if the objc-superclass,
8288 objc-protocol-refs and objc-class-instance-variables are omitted. */
8289
8290 static void
8291 c_parser_objc_class_definition (c_parser *parser, tree attributes)
8292 {
8293 bool iface_p;
8294 tree id1;
8295 tree superclass;
8296 if (c_parser_next_token_is_keyword (parser, RID_AT_INTERFACE))
8297 iface_p = true;
8298 else if (c_parser_next_token_is_keyword (parser, RID_AT_IMPLEMENTATION))
8299 iface_p = false;
8300 else
8301 gcc_unreachable ();
8302
8303 c_parser_consume_token (parser);
8304 if (c_parser_next_token_is_not (parser, CPP_NAME))
8305 {
8306 c_parser_error (parser, "expected identifier");
8307 return;
8308 }
8309 id1 = c_parser_peek_token (parser)->value;
8310 c_parser_consume_token (parser);
8311 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
8312 {
8313 /* We have a category or class extension. */
8314 tree id2;
8315 tree proto = NULL_TREE;
8316 c_parser_consume_token (parser);
8317 if (c_parser_next_token_is_not (parser, CPP_NAME))
8318 {
8319 if (iface_p && c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
8320 {
8321 /* We have a class extension. */
8322 id2 = NULL_TREE;
8323 }
8324 else
8325 {
8326 c_parser_error (parser, "expected identifier or %<)%>");
8327 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
8328 return;
8329 }
8330 }
8331 else
8332 {
8333 id2 = c_parser_peek_token (parser)->value;
8334 c_parser_consume_token (parser);
8335 }
8336 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
8337 if (!iface_p)
8338 {
8339 objc_start_category_implementation (id1, id2);
8340 return;
8341 }
8342 if (c_parser_next_token_is (parser, CPP_LESS))
8343 proto = c_parser_objc_protocol_refs (parser);
8344 objc_start_category_interface (id1, id2, proto, attributes);
8345 c_parser_objc_methodprotolist (parser);
8346 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
8347 objc_finish_interface ();
8348 return;
8349 }
8350 if (c_parser_next_token_is (parser, CPP_COLON))
8351 {
8352 c_parser_consume_token (parser);
8353 if (c_parser_next_token_is_not (parser, CPP_NAME))
8354 {
8355 c_parser_error (parser, "expected identifier");
8356 return;
8357 }
8358 superclass = c_parser_peek_token (parser)->value;
8359 c_parser_consume_token (parser);
8360 }
8361 else
8362 superclass = NULL_TREE;
8363 if (iface_p)
8364 {
8365 tree proto = NULL_TREE;
8366 if (c_parser_next_token_is (parser, CPP_LESS))
8367 proto = c_parser_objc_protocol_refs (parser);
8368 objc_start_class_interface (id1, superclass, proto, attributes);
8369 }
8370 else
8371 objc_start_class_implementation (id1, superclass);
8372 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
8373 c_parser_objc_class_instance_variables (parser);
8374 if (iface_p)
8375 {
8376 objc_continue_interface ();
8377 c_parser_objc_methodprotolist (parser);
8378 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
8379 objc_finish_interface ();
8380 }
8381 else
8382 {
8383 objc_continue_implementation ();
8384 return;
8385 }
8386 }
8387
8388 /* Parse objc-class-instance-variables.
8389
8390 objc-class-instance-variables:
8391 { objc-instance-variable-decl-list[opt] }
8392
8393 objc-instance-variable-decl-list:
8394 objc-visibility-spec
8395 objc-instance-variable-decl ;
8396 ;
8397 objc-instance-variable-decl-list objc-visibility-spec
8398 objc-instance-variable-decl-list objc-instance-variable-decl ;
8399 objc-instance-variable-decl-list ;
8400
8401 objc-visibility-spec:
8402 @private
8403 @protected
8404 @public
8405
8406 objc-instance-variable-decl:
8407 struct-declaration
8408 */
8409
8410 static void
8411 c_parser_objc_class_instance_variables (c_parser *parser)
8412 {
8413 gcc_assert (c_parser_next_token_is (parser, CPP_OPEN_BRACE));
8414 c_parser_consume_token (parser);
8415 while (c_parser_next_token_is_not (parser, CPP_EOF))
8416 {
8417 tree decls;
8418 /* Parse any stray semicolon. */
8419 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
8420 {
8421 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
8422 "extra semicolon");
8423 c_parser_consume_token (parser);
8424 continue;
8425 }
8426 /* Stop if at the end of the instance variables. */
8427 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
8428 {
8429 c_parser_consume_token (parser);
8430 break;
8431 }
8432 /* Parse any objc-visibility-spec. */
8433 if (c_parser_next_token_is_keyword (parser, RID_AT_PRIVATE))
8434 {
8435 c_parser_consume_token (parser);
8436 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
8437 continue;
8438 }
8439 else if (c_parser_next_token_is_keyword (parser, RID_AT_PROTECTED))
8440 {
8441 c_parser_consume_token (parser);
8442 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
8443 continue;
8444 }
8445 else if (c_parser_next_token_is_keyword (parser, RID_AT_PUBLIC))
8446 {
8447 c_parser_consume_token (parser);
8448 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
8449 continue;
8450 }
8451 else if (c_parser_next_token_is_keyword (parser, RID_AT_PACKAGE))
8452 {
8453 c_parser_consume_token (parser);
8454 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
8455 continue;
8456 }
8457 else if (c_parser_next_token_is (parser, CPP_PRAGMA))
8458 {
8459 c_parser_pragma (parser, pragma_external);
8460 continue;
8461 }
8462
8463 /* Parse some comma-separated declarations. */
8464 decls = c_parser_struct_declaration (parser);
8465 if (decls == NULL)
8466 {
8467 /* There is a syntax error. We want to skip the offending
8468 tokens up to the next ';' (included) or '}'
8469 (excluded). */
8470
8471 /* First, skip manually a ')' or ']'. This is because they
8472 reduce the nesting level, so c_parser_skip_until_found()
8473 wouldn't be able to skip past them. */
8474 c_token *token = c_parser_peek_token (parser);
8475 if (token->type == CPP_CLOSE_PAREN || token->type == CPP_CLOSE_SQUARE)
8476 c_parser_consume_token (parser);
8477
8478 /* Then, do the standard skipping. */
8479 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
8480
8481 /* We hopefully recovered. Start normal parsing again. */
8482 parser->error = false;
8483 continue;
8484 }
8485 else
8486 {
8487 /* Comma-separated instance variables are chained together
8488 in reverse order; add them one by one. */
8489 tree ivar = nreverse (decls);
8490 for (; ivar; ivar = DECL_CHAIN (ivar))
8491 objc_add_instance_variable (copy_node (ivar));
8492 }
8493 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
8494 }
8495 }
8496
8497 /* Parse an objc-class-declaration.
8498
8499 objc-class-declaration:
8500 @class identifier-list ;
8501 */
8502
8503 static void
8504 c_parser_objc_class_declaration (c_parser *parser)
8505 {
8506 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_CLASS));
8507 c_parser_consume_token (parser);
8508 /* Any identifiers, including those declared as type names, are OK
8509 here. */
8510 while (true)
8511 {
8512 tree id;
8513 if (c_parser_next_token_is_not (parser, CPP_NAME))
8514 {
8515 c_parser_error (parser, "expected identifier");
8516 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
8517 parser->error = false;
8518 return;
8519 }
8520 id = c_parser_peek_token (parser)->value;
8521 objc_declare_class (id);
8522 c_parser_consume_token (parser);
8523 if (c_parser_next_token_is (parser, CPP_COMMA))
8524 c_parser_consume_token (parser);
8525 else
8526 break;
8527 }
8528 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
8529 }
8530
8531 /* Parse an objc-alias-declaration.
8532
8533 objc-alias-declaration:
8534 @compatibility_alias identifier identifier ;
8535 */
8536
8537 static void
8538 c_parser_objc_alias_declaration (c_parser *parser)
8539 {
8540 tree id1, id2;
8541 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_ALIAS));
8542 c_parser_consume_token (parser);
8543 if (c_parser_next_token_is_not (parser, CPP_NAME))
8544 {
8545 c_parser_error (parser, "expected identifier");
8546 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
8547 return;
8548 }
8549 id1 = c_parser_peek_token (parser)->value;
8550 c_parser_consume_token (parser);
8551 if (c_parser_next_token_is_not (parser, CPP_NAME))
8552 {
8553 c_parser_error (parser, "expected identifier");
8554 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
8555 return;
8556 }
8557 id2 = c_parser_peek_token (parser)->value;
8558 c_parser_consume_token (parser);
8559 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
8560 objc_declare_alias (id1, id2);
8561 }
8562
8563 /* Parse an objc-protocol-definition.
8564
8565 objc-protocol-definition:
8566 @protocol identifier objc-protocol-refs[opt] objc-methodprotolist @end
8567 @protocol identifier-list ;
8568
8569 "@protocol identifier ;" should be resolved as "@protocol
8570 identifier-list ;": objc-methodprotolist may not start with a
8571 semicolon in the first alternative if objc-protocol-refs are
8572 omitted. */
8573
8574 static void
8575 c_parser_objc_protocol_definition (c_parser *parser, tree attributes)
8576 {
8577 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_PROTOCOL));
8578
8579 c_parser_consume_token (parser);
8580 if (c_parser_next_token_is_not (parser, CPP_NAME))
8581 {
8582 c_parser_error (parser, "expected identifier");
8583 return;
8584 }
8585 if (c_parser_peek_2nd_token (parser)->type == CPP_COMMA
8586 || c_parser_peek_2nd_token (parser)->type == CPP_SEMICOLON)
8587 {
8588 /* Any identifiers, including those declared as type names, are
8589 OK here. */
8590 while (true)
8591 {
8592 tree id;
8593 if (c_parser_next_token_is_not (parser, CPP_NAME))
8594 {
8595 c_parser_error (parser, "expected identifier");
8596 break;
8597 }
8598 id = c_parser_peek_token (parser)->value;
8599 objc_declare_protocol (id, attributes);
8600 c_parser_consume_token (parser);
8601 if (c_parser_next_token_is (parser, CPP_COMMA))
8602 c_parser_consume_token (parser);
8603 else
8604 break;
8605 }
8606 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
8607 }
8608 else
8609 {
8610 tree id = c_parser_peek_token (parser)->value;
8611 tree proto = NULL_TREE;
8612 c_parser_consume_token (parser);
8613 if (c_parser_next_token_is (parser, CPP_LESS))
8614 proto = c_parser_objc_protocol_refs (parser);
8615 parser->objc_pq_context = true;
8616 objc_start_protocol (id, proto, attributes);
8617 c_parser_objc_methodprotolist (parser);
8618 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
8619 parser->objc_pq_context = false;
8620 objc_finish_interface ();
8621 }
8622 }
8623
8624 /* Parse an objc-method-type.
8625
8626 objc-method-type:
8627 +
8628 -
8629
8630 Return true if it is a class method (+) and false if it is
8631 an instance method (-).
8632 */
8633 static inline bool
8634 c_parser_objc_method_type (c_parser *parser)
8635 {
8636 switch (c_parser_peek_token (parser)->type)
8637 {
8638 case CPP_PLUS:
8639 c_parser_consume_token (parser);
8640 return true;
8641 case CPP_MINUS:
8642 c_parser_consume_token (parser);
8643 return false;
8644 default:
8645 gcc_unreachable ();
8646 }
8647 }
8648
8649 /* Parse an objc-method-definition.
8650
8651 objc-method-definition:
8652 objc-method-type objc-method-decl ;[opt] compound-statement
8653 */
8654
8655 static void
8656 c_parser_objc_method_definition (c_parser *parser)
8657 {
8658 bool is_class_method = c_parser_objc_method_type (parser);
8659 tree decl, attributes = NULL_TREE, expr = NULL_TREE;
8660 parser->objc_pq_context = true;
8661 decl = c_parser_objc_method_decl (parser, is_class_method, &attributes,
8662 &expr);
8663 if (decl == error_mark_node)
8664 return; /* Bail here. */
8665
8666 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
8667 {
8668 c_parser_consume_token (parser);
8669 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
8670 "extra semicolon in method definition specified");
8671 }
8672
8673 if (!c_parser_next_token_is (parser, CPP_OPEN_BRACE))
8674 {
8675 c_parser_error (parser, "expected %<{%>");
8676 return;
8677 }
8678
8679 parser->objc_pq_context = false;
8680 if (objc_start_method_definition (is_class_method, decl, attributes, expr))
8681 {
8682 add_stmt (c_parser_compound_statement (parser));
8683 objc_finish_method_definition (current_function_decl);
8684 }
8685 else
8686 {
8687 /* This code is executed when we find a method definition
8688 outside of an @implementation context (or invalid for other
8689 reasons). Parse the method (to keep going) but do not emit
8690 any code.
8691 */
8692 c_parser_compound_statement (parser);
8693 }
8694 }
8695
8696 /* Parse an objc-methodprotolist.
8697
8698 objc-methodprotolist:
8699 empty
8700 objc-methodprotolist objc-methodproto
8701 objc-methodprotolist declaration
8702 objc-methodprotolist ;
8703 @optional
8704 @required
8705
8706 The declaration is a data definition, which may be missing
8707 declaration specifiers under the same rules and diagnostics as
8708 other data definitions outside functions, and the stray semicolon
8709 is diagnosed the same way as a stray semicolon outside a
8710 function. */
8711
8712 static void
8713 c_parser_objc_methodprotolist (c_parser *parser)
8714 {
8715 while (true)
8716 {
8717 /* The list is terminated by @end. */
8718 switch (c_parser_peek_token (parser)->type)
8719 {
8720 case CPP_SEMICOLON:
8721 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
8722 "ISO C does not allow extra %<;%> outside of a function");
8723 c_parser_consume_token (parser);
8724 break;
8725 case CPP_PLUS:
8726 case CPP_MINUS:
8727 c_parser_objc_methodproto (parser);
8728 break;
8729 case CPP_PRAGMA:
8730 c_parser_pragma (parser, pragma_external);
8731 break;
8732 case CPP_EOF:
8733 return;
8734 default:
8735 if (c_parser_next_token_is_keyword (parser, RID_AT_END))
8736 return;
8737 else if (c_parser_next_token_is_keyword (parser, RID_AT_PROPERTY))
8738 c_parser_objc_at_property_declaration (parser);
8739 else if (c_parser_next_token_is_keyword (parser, RID_AT_OPTIONAL))
8740 {
8741 objc_set_method_opt (true);
8742 c_parser_consume_token (parser);
8743 }
8744 else if (c_parser_next_token_is_keyword (parser, RID_AT_REQUIRED))
8745 {
8746 objc_set_method_opt (false);
8747 c_parser_consume_token (parser);
8748 }
8749 else
8750 c_parser_declaration_or_fndef (parser, false, false, true,
8751 false, true, NULL, vNULL);
8752 break;
8753 }
8754 }
8755 }
8756
8757 /* Parse an objc-methodproto.
8758
8759 objc-methodproto:
8760 objc-method-type objc-method-decl ;
8761 */
8762
8763 static void
8764 c_parser_objc_methodproto (c_parser *parser)
8765 {
8766 bool is_class_method = c_parser_objc_method_type (parser);
8767 tree decl, attributes = NULL_TREE;
8768
8769 /* Remember protocol qualifiers in prototypes. */
8770 parser->objc_pq_context = true;
8771 decl = c_parser_objc_method_decl (parser, is_class_method, &attributes,
8772 NULL);
8773 /* Forget protocol qualifiers now. */
8774 parser->objc_pq_context = false;
8775
8776 /* Do not allow the presence of attributes to hide an erroneous
8777 method implementation in the interface section. */
8778 if (!c_parser_next_token_is (parser, CPP_SEMICOLON))
8779 {
8780 c_parser_error (parser, "expected %<;%>");
8781 return;
8782 }
8783
8784 if (decl != error_mark_node)
8785 objc_add_method_declaration (is_class_method, decl, attributes);
8786
8787 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
8788 }
8789
8790 /* If we are at a position that method attributes may be present, check that
8791 there are not any parsed already (a syntax error) and then collect any
8792 specified at the current location. Finally, if new attributes were present,
8793 check that the next token is legal ( ';' for decls and '{' for defs). */
8794
8795 static bool
8796 c_parser_objc_maybe_method_attributes (c_parser* parser, tree* attributes)
8797 {
8798 bool bad = false;
8799 if (*attributes)
8800 {
8801 c_parser_error (parser,
8802 "method attributes must be specified at the end only");
8803 *attributes = NULL_TREE;
8804 bad = true;
8805 }
8806
8807 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
8808 *attributes = c_parser_attributes (parser);
8809
8810 /* If there were no attributes here, just report any earlier error. */
8811 if (*attributes == NULL_TREE || bad)
8812 return bad;
8813
8814 /* If the attributes are followed by a ; or {, then just report any earlier
8815 error. */
8816 if (c_parser_next_token_is (parser, CPP_SEMICOLON)
8817 || c_parser_next_token_is (parser, CPP_OPEN_BRACE))
8818 return bad;
8819
8820 /* We've got attributes, but not at the end. */
8821 c_parser_error (parser,
8822 "expected %<;%> or %<{%> after method attribute definition");
8823 return true;
8824 }
8825
8826 /* Parse an objc-method-decl.
8827
8828 objc-method-decl:
8829 ( objc-type-name ) objc-selector
8830 objc-selector
8831 ( objc-type-name ) objc-keyword-selector objc-optparmlist
8832 objc-keyword-selector objc-optparmlist
8833 attributes
8834
8835 objc-keyword-selector:
8836 objc-keyword-decl
8837 objc-keyword-selector objc-keyword-decl
8838
8839 objc-keyword-decl:
8840 objc-selector : ( objc-type-name ) identifier
8841 objc-selector : identifier
8842 : ( objc-type-name ) identifier
8843 : identifier
8844
8845 objc-optparmlist:
8846 objc-optparms objc-optellipsis
8847
8848 objc-optparms:
8849 empty
8850 objc-opt-parms , parameter-declaration
8851
8852 objc-optellipsis:
8853 empty
8854 , ...
8855 */
8856
8857 static tree
8858 c_parser_objc_method_decl (c_parser *parser, bool is_class_method,
8859 tree *attributes, tree *expr)
8860 {
8861 tree type = NULL_TREE;
8862 tree sel;
8863 tree parms = NULL_TREE;
8864 bool ellipsis = false;
8865 bool attr_err = false;
8866
8867 *attributes = NULL_TREE;
8868 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
8869 {
8870 c_parser_consume_token (parser);
8871 type = c_parser_objc_type_name (parser);
8872 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
8873 }
8874 sel = c_parser_objc_selector (parser);
8875 /* If there is no selector, or a colon follows, we have an
8876 objc-keyword-selector. If there is a selector, and a colon does
8877 not follow, that selector ends the objc-method-decl. */
8878 if (!sel || c_parser_next_token_is (parser, CPP_COLON))
8879 {
8880 tree tsel = sel;
8881 tree list = NULL_TREE;
8882 while (true)
8883 {
8884 tree atype = NULL_TREE, id, keyworddecl;
8885 tree param_attr = NULL_TREE;
8886 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
8887 break;
8888 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
8889 {
8890 c_parser_consume_token (parser);
8891 atype = c_parser_objc_type_name (parser);
8892 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
8893 "expected %<)%>");
8894 }
8895 /* New ObjC allows attributes on method parameters. */
8896 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
8897 param_attr = c_parser_attributes (parser);
8898 if (c_parser_next_token_is_not (parser, CPP_NAME))
8899 {
8900 c_parser_error (parser, "expected identifier");
8901 return error_mark_node;
8902 }
8903 id = c_parser_peek_token (parser)->value;
8904 c_parser_consume_token (parser);
8905 keyworddecl = objc_build_keyword_decl (tsel, atype, id, param_attr);
8906 list = chainon (list, keyworddecl);
8907 tsel = c_parser_objc_selector (parser);
8908 if (!tsel && c_parser_next_token_is_not (parser, CPP_COLON))
8909 break;
8910 }
8911
8912 attr_err |= c_parser_objc_maybe_method_attributes (parser, attributes) ;
8913
8914 /* Parse the optional parameter list. Optional Objective-C
8915 method parameters follow the C syntax, and may include '...'
8916 to denote a variable number of arguments. */
8917 parms = make_node (TREE_LIST);
8918 while (c_parser_next_token_is (parser, CPP_COMMA))
8919 {
8920 struct c_parm *parm;
8921 c_parser_consume_token (parser);
8922 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
8923 {
8924 ellipsis = true;
8925 c_parser_consume_token (parser);
8926 attr_err |= c_parser_objc_maybe_method_attributes
8927 (parser, attributes) ;
8928 break;
8929 }
8930 parm = c_parser_parameter_declaration (parser, NULL_TREE);
8931 if (parm == NULL)
8932 break;
8933 parms = chainon (parms,
8934 build_tree_list (NULL_TREE, grokparm (parm, expr)));
8935 }
8936 sel = list;
8937 }
8938 else
8939 attr_err |= c_parser_objc_maybe_method_attributes (parser, attributes) ;
8940
8941 if (sel == NULL)
8942 {
8943 c_parser_error (parser, "objective-c method declaration is expected");
8944 return error_mark_node;
8945 }
8946
8947 if (attr_err)
8948 return error_mark_node;
8949
8950 return objc_build_method_signature (is_class_method, type, sel, parms, ellipsis);
8951 }
8952
8953 /* Parse an objc-type-name.
8954
8955 objc-type-name:
8956 objc-type-qualifiers[opt] type-name
8957 objc-type-qualifiers[opt]
8958
8959 objc-type-qualifiers:
8960 objc-type-qualifier
8961 objc-type-qualifiers objc-type-qualifier
8962
8963 objc-type-qualifier: one of
8964 in out inout bycopy byref oneway
8965 */
8966
8967 static tree
8968 c_parser_objc_type_name (c_parser *parser)
8969 {
8970 tree quals = NULL_TREE;
8971 struct c_type_name *type_name = NULL;
8972 tree type = NULL_TREE;
8973 while (true)
8974 {
8975 c_token *token = c_parser_peek_token (parser);
8976 if (token->type == CPP_KEYWORD
8977 && (token->keyword == RID_IN
8978 || token->keyword == RID_OUT
8979 || token->keyword == RID_INOUT
8980 || token->keyword == RID_BYCOPY
8981 || token->keyword == RID_BYREF
8982 || token->keyword == RID_ONEWAY))
8983 {
8984 quals = chainon (build_tree_list (NULL_TREE, token->value), quals);
8985 c_parser_consume_token (parser);
8986 }
8987 else
8988 break;
8989 }
8990 if (c_parser_next_tokens_start_typename (parser, cla_prefer_type))
8991 type_name = c_parser_type_name (parser);
8992 if (type_name)
8993 type = groktypename (type_name, NULL, NULL);
8994
8995 /* If the type is unknown, and error has already been produced and
8996 we need to recover from the error. In that case, use NULL_TREE
8997 for the type, as if no type had been specified; this will use the
8998 default type ('id') which is good for error recovery. */
8999 if (type == error_mark_node)
9000 type = NULL_TREE;
9001
9002 return build_tree_list (quals, type);
9003 }
9004
9005 /* Parse objc-protocol-refs.
9006
9007 objc-protocol-refs:
9008 < identifier-list >
9009 */
9010
9011 static tree
9012 c_parser_objc_protocol_refs (c_parser *parser)
9013 {
9014 tree list = NULL_TREE;
9015 gcc_assert (c_parser_next_token_is (parser, CPP_LESS));
9016 c_parser_consume_token (parser);
9017 /* Any identifiers, including those declared as type names, are OK
9018 here. */
9019 while (true)
9020 {
9021 tree id;
9022 if (c_parser_next_token_is_not (parser, CPP_NAME))
9023 {
9024 c_parser_error (parser, "expected identifier");
9025 break;
9026 }
9027 id = c_parser_peek_token (parser)->value;
9028 list = chainon (list, build_tree_list (NULL_TREE, id));
9029 c_parser_consume_token (parser);
9030 if (c_parser_next_token_is (parser, CPP_COMMA))
9031 c_parser_consume_token (parser);
9032 else
9033 break;
9034 }
9035 c_parser_require (parser, CPP_GREATER, "expected %<>%>");
9036 return list;
9037 }
9038
9039 /* Parse an objc-try-catch-finally-statement.
9040
9041 objc-try-catch-finally-statement:
9042 @try compound-statement objc-catch-list[opt]
9043 @try compound-statement objc-catch-list[opt] @finally compound-statement
9044
9045 objc-catch-list:
9046 @catch ( objc-catch-parameter-declaration ) compound-statement
9047 objc-catch-list @catch ( objc-catch-parameter-declaration ) compound-statement
9048
9049 objc-catch-parameter-declaration:
9050 parameter-declaration
9051 '...'
9052
9053 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
9054
9055 PS: This function is identical to cp_parser_objc_try_catch_finally_statement
9056 for C++. Keep them in sync. */
9057
9058 static void
9059 c_parser_objc_try_catch_finally_statement (c_parser *parser)
9060 {
9061 location_t location;
9062 tree stmt;
9063
9064 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_TRY));
9065 c_parser_consume_token (parser);
9066 location = c_parser_peek_token (parser)->location;
9067 objc_maybe_warn_exceptions (location);
9068 stmt = c_parser_compound_statement (parser);
9069 objc_begin_try_stmt (location, stmt);
9070
9071 while (c_parser_next_token_is_keyword (parser, RID_AT_CATCH))
9072 {
9073 struct c_parm *parm;
9074 tree parameter_declaration = error_mark_node;
9075 bool seen_open_paren = false;
9076
9077 c_parser_consume_token (parser);
9078 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
9079 seen_open_paren = true;
9080 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
9081 {
9082 /* We have "@catch (...)" (where the '...' are literally
9083 what is in the code). Skip the '...'.
9084 parameter_declaration is set to NULL_TREE, and
9085 objc_being_catch_clauses() knows that that means
9086 '...'. */
9087 c_parser_consume_token (parser);
9088 parameter_declaration = NULL_TREE;
9089 }
9090 else
9091 {
9092 /* We have "@catch (NSException *exception)" or something
9093 like that. Parse the parameter declaration. */
9094 parm = c_parser_parameter_declaration (parser, NULL_TREE);
9095 if (parm == NULL)
9096 parameter_declaration = error_mark_node;
9097 else
9098 parameter_declaration = grokparm (parm, NULL);
9099 }
9100 if (seen_open_paren)
9101 c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
9102 else
9103 {
9104 /* If there was no open parenthesis, we are recovering from
9105 an error, and we are trying to figure out what mistake
9106 the user has made. */
9107
9108 /* If there is an immediate closing parenthesis, the user
9109 probably forgot the opening one (ie, they typed "@catch
9110 NSException *e)". Parse the closing parenthesis and keep
9111 going. */
9112 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
9113 c_parser_consume_token (parser);
9114
9115 /* If these is no immediate closing parenthesis, the user
9116 probably doesn't know that parenthesis are required at
9117 all (ie, they typed "@catch NSException *e"). So, just
9118 forget about the closing parenthesis and keep going. */
9119 }
9120 objc_begin_catch_clause (parameter_declaration);
9121 if (c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
9122 c_parser_compound_statement_nostart (parser);
9123 objc_finish_catch_clause ();
9124 }
9125 if (c_parser_next_token_is_keyword (parser, RID_AT_FINALLY))
9126 {
9127 c_parser_consume_token (parser);
9128 location = c_parser_peek_token (parser)->location;
9129 stmt = c_parser_compound_statement (parser);
9130 objc_build_finally_clause (location, stmt);
9131 }
9132 objc_finish_try_stmt ();
9133 }
9134
9135 /* Parse an objc-synchronized-statement.
9136
9137 objc-synchronized-statement:
9138 @synchronized ( expression ) compound-statement
9139 */
9140
9141 static void
9142 c_parser_objc_synchronized_statement (c_parser *parser)
9143 {
9144 location_t loc;
9145 tree expr, stmt;
9146 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_SYNCHRONIZED));
9147 c_parser_consume_token (parser);
9148 loc = c_parser_peek_token (parser)->location;
9149 objc_maybe_warn_exceptions (loc);
9150 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
9151 {
9152 struct c_expr ce = c_parser_expression (parser);
9153 ce = convert_lvalue_to_rvalue (loc, ce, false, false);
9154 expr = ce.value;
9155 expr = c_fully_fold (expr, false, NULL);
9156 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
9157 }
9158 else
9159 expr = error_mark_node;
9160 stmt = c_parser_compound_statement (parser);
9161 objc_build_synchronized (loc, expr, stmt);
9162 }
9163
9164 /* Parse an objc-selector; return NULL_TREE without an error if the
9165 next token is not an objc-selector.
9166
9167 objc-selector:
9168 identifier
9169 one of
9170 enum struct union if else while do for switch case default
9171 break continue return goto asm sizeof typeof __alignof
9172 unsigned long const short volatile signed restrict _Complex
9173 in out inout bycopy byref oneway int char float double void _Bool
9174 _Atomic
9175
9176 ??? Why this selection of keywords but not, for example, storage
9177 class specifiers? */
9178
9179 static tree
9180 c_parser_objc_selector (c_parser *parser)
9181 {
9182 c_token *token = c_parser_peek_token (parser);
9183 tree value = token->value;
9184 if (token->type == CPP_NAME)
9185 {
9186 c_parser_consume_token (parser);
9187 return value;
9188 }
9189 if (token->type != CPP_KEYWORD)
9190 return NULL_TREE;
9191 switch (token->keyword)
9192 {
9193 case RID_ENUM:
9194 case RID_STRUCT:
9195 case RID_UNION:
9196 case RID_IF:
9197 case RID_ELSE:
9198 case RID_WHILE:
9199 case RID_DO:
9200 case RID_FOR:
9201 case RID_SWITCH:
9202 case RID_CASE:
9203 case RID_DEFAULT:
9204 case RID_BREAK:
9205 case RID_CONTINUE:
9206 case RID_RETURN:
9207 case RID_GOTO:
9208 case RID_ASM:
9209 case RID_SIZEOF:
9210 case RID_TYPEOF:
9211 case RID_ALIGNOF:
9212 case RID_UNSIGNED:
9213 case RID_LONG:
9214 case RID_CONST:
9215 case RID_SHORT:
9216 case RID_VOLATILE:
9217 case RID_SIGNED:
9218 case RID_RESTRICT:
9219 case RID_COMPLEX:
9220 case RID_IN:
9221 case RID_OUT:
9222 case RID_INOUT:
9223 case RID_BYCOPY:
9224 case RID_BYREF:
9225 case RID_ONEWAY:
9226 case RID_INT:
9227 case RID_CHAR:
9228 case RID_FLOAT:
9229 case RID_DOUBLE:
9230 case RID_VOID:
9231 case RID_BOOL:
9232 case RID_ATOMIC:
9233 case RID_AUTO_TYPE:
9234 case RID_INT_N_0:
9235 case RID_INT_N_1:
9236 case RID_INT_N_2:
9237 case RID_INT_N_3:
9238 c_parser_consume_token (parser);
9239 return value;
9240 default:
9241 return NULL_TREE;
9242 }
9243 }
9244
9245 /* Parse an objc-selector-arg.
9246
9247 objc-selector-arg:
9248 objc-selector
9249 objc-keywordname-list
9250
9251 objc-keywordname-list:
9252 objc-keywordname
9253 objc-keywordname-list objc-keywordname
9254
9255 objc-keywordname:
9256 objc-selector :
9257 :
9258 */
9259
9260 static tree
9261 c_parser_objc_selector_arg (c_parser *parser)
9262 {
9263 tree sel = c_parser_objc_selector (parser);
9264 tree list = NULL_TREE;
9265 if (sel && c_parser_next_token_is_not (parser, CPP_COLON))
9266 return sel;
9267 while (true)
9268 {
9269 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
9270 return list;
9271 list = chainon (list, build_tree_list (sel, NULL_TREE));
9272 sel = c_parser_objc_selector (parser);
9273 if (!sel && c_parser_next_token_is_not (parser, CPP_COLON))
9274 break;
9275 }
9276 return list;
9277 }
9278
9279 /* Parse an objc-receiver.
9280
9281 objc-receiver:
9282 expression
9283 class-name
9284 type-name
9285 */
9286
9287 static tree
9288 c_parser_objc_receiver (c_parser *parser)
9289 {
9290 location_t loc = c_parser_peek_token (parser)->location;
9291
9292 if (c_parser_peek_token (parser)->type == CPP_NAME
9293 && (c_parser_peek_token (parser)->id_kind == C_ID_TYPENAME
9294 || c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME))
9295 {
9296 tree id = c_parser_peek_token (parser)->value;
9297 c_parser_consume_token (parser);
9298 return objc_get_class_reference (id);
9299 }
9300 struct c_expr ce = c_parser_expression (parser);
9301 ce = convert_lvalue_to_rvalue (loc, ce, false, false);
9302 return c_fully_fold (ce.value, false, NULL);
9303 }
9304
9305 /* Parse objc-message-args.
9306
9307 objc-message-args:
9308 objc-selector
9309 objc-keywordarg-list
9310
9311 objc-keywordarg-list:
9312 objc-keywordarg
9313 objc-keywordarg-list objc-keywordarg
9314
9315 objc-keywordarg:
9316 objc-selector : objc-keywordexpr
9317 : objc-keywordexpr
9318 */
9319
9320 static tree
9321 c_parser_objc_message_args (c_parser *parser)
9322 {
9323 tree sel = c_parser_objc_selector (parser);
9324 tree list = NULL_TREE;
9325 if (sel && c_parser_next_token_is_not (parser, CPP_COLON))
9326 return sel;
9327 while (true)
9328 {
9329 tree keywordexpr;
9330 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
9331 return error_mark_node;
9332 keywordexpr = c_parser_objc_keywordexpr (parser);
9333 list = chainon (list, build_tree_list (sel, keywordexpr));
9334 sel = c_parser_objc_selector (parser);
9335 if (!sel && c_parser_next_token_is_not (parser, CPP_COLON))
9336 break;
9337 }
9338 return list;
9339 }
9340
9341 /* Parse an objc-keywordexpr.
9342
9343 objc-keywordexpr:
9344 nonempty-expr-list
9345 */
9346
9347 static tree
9348 c_parser_objc_keywordexpr (c_parser *parser)
9349 {
9350 tree ret;
9351 vec<tree, va_gc> *expr_list = c_parser_expr_list (parser, true, true,
9352 NULL, NULL, NULL, NULL);
9353 if (vec_safe_length (expr_list) == 1)
9354 {
9355 /* Just return the expression, remove a level of
9356 indirection. */
9357 ret = (*expr_list)[0];
9358 }
9359 else
9360 {
9361 /* We have a comma expression, we will collapse later. */
9362 ret = build_tree_list_vec (expr_list);
9363 }
9364 release_tree_vector (expr_list);
9365 return ret;
9366 }
9367
9368 /* A check, needed in several places, that ObjC interface, implementation or
9369 method definitions are not prefixed by incorrect items. */
9370 static bool
9371 c_parser_objc_diagnose_bad_element_prefix (c_parser *parser,
9372 struct c_declspecs *specs)
9373 {
9374 if (!specs->declspecs_seen_p || specs->non_sc_seen_p
9375 || specs->typespec_kind != ctsk_none)
9376 {
9377 c_parser_error (parser,
9378 "no type or storage class may be specified here,");
9379 c_parser_skip_to_end_of_block_or_statement (parser);
9380 return true;
9381 }
9382 return false;
9383 }
9384
9385 /* Parse an Objective-C @property declaration. The syntax is:
9386
9387 objc-property-declaration:
9388 '@property' objc-property-attributes[opt] struct-declaration ;
9389
9390 objc-property-attributes:
9391 '(' objc-property-attribute-list ')'
9392
9393 objc-property-attribute-list:
9394 objc-property-attribute
9395 objc-property-attribute-list, objc-property-attribute
9396
9397 objc-property-attribute
9398 'getter' = identifier
9399 'setter' = identifier
9400 'readonly'
9401 'readwrite'
9402 'assign'
9403 'retain'
9404 'copy'
9405 'nonatomic'
9406
9407 For example:
9408 @property NSString *name;
9409 @property (readonly) id object;
9410 @property (retain, nonatomic, getter=getTheName) id name;
9411 @property int a, b, c;
9412
9413 PS: This function is identical to cp_parser_objc_at_propery_declaration
9414 for C++. Keep them in sync. */
9415 static void
9416 c_parser_objc_at_property_declaration (c_parser *parser)
9417 {
9418 /* The following variables hold the attributes of the properties as
9419 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
9420 seen. When we see an attribute, we set them to 'true' (if they
9421 are boolean properties) or to the identifier (if they have an
9422 argument, ie, for getter and setter). Note that here we only
9423 parse the list of attributes, check the syntax and accumulate the
9424 attributes that we find. objc_add_property_declaration() will
9425 then process the information. */
9426 bool property_assign = false;
9427 bool property_copy = false;
9428 tree property_getter_ident = NULL_TREE;
9429 bool property_nonatomic = false;
9430 bool property_readonly = false;
9431 bool property_readwrite = false;
9432 bool property_retain = false;
9433 tree property_setter_ident = NULL_TREE;
9434
9435 /* 'properties' is the list of properties that we read. Usually a
9436 single one, but maybe more (eg, in "@property int a, b, c;" there
9437 are three). */
9438 tree properties;
9439 location_t loc;
9440
9441 loc = c_parser_peek_token (parser)->location;
9442 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_PROPERTY));
9443
9444 c_parser_consume_token (parser); /* Eat '@property'. */
9445
9446 /* Parse the optional attribute list... */
9447 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
9448 {
9449 /* Eat the '(' */
9450 c_parser_consume_token (parser);
9451
9452 /* Property attribute keywords are valid now. */
9453 parser->objc_property_attr_context = true;
9454
9455 while (true)
9456 {
9457 bool syntax_error = false;
9458 c_token *token = c_parser_peek_token (parser);
9459 enum rid keyword;
9460
9461 if (token->type != CPP_KEYWORD)
9462 {
9463 if (token->type == CPP_CLOSE_PAREN)
9464 c_parser_error (parser, "expected identifier");
9465 else
9466 {
9467 c_parser_consume_token (parser);
9468 c_parser_error (parser, "unknown property attribute");
9469 }
9470 break;
9471 }
9472 keyword = token->keyword;
9473 c_parser_consume_token (parser);
9474 switch (keyword)
9475 {
9476 case RID_ASSIGN: property_assign = true; break;
9477 case RID_COPY: property_copy = true; break;
9478 case RID_NONATOMIC: property_nonatomic = true; break;
9479 case RID_READONLY: property_readonly = true; break;
9480 case RID_READWRITE: property_readwrite = true; break;
9481 case RID_RETAIN: property_retain = true; break;
9482
9483 case RID_GETTER:
9484 case RID_SETTER:
9485 if (c_parser_next_token_is_not (parser, CPP_EQ))
9486 {
9487 if (keyword == RID_GETTER)
9488 c_parser_error (parser,
9489 "missing %<=%> (after %<getter%> attribute)");
9490 else
9491 c_parser_error (parser,
9492 "missing %<=%> (after %<setter%> attribute)");
9493 syntax_error = true;
9494 break;
9495 }
9496 c_parser_consume_token (parser); /* eat the = */
9497 if (c_parser_next_token_is_not (parser, CPP_NAME))
9498 {
9499 c_parser_error (parser, "expected identifier");
9500 syntax_error = true;
9501 break;
9502 }
9503 if (keyword == RID_SETTER)
9504 {
9505 if (property_setter_ident != NULL_TREE)
9506 c_parser_error (parser, "the %<setter%> attribute may only be specified once");
9507 else
9508 property_setter_ident = c_parser_peek_token (parser)->value;
9509 c_parser_consume_token (parser);
9510 if (c_parser_next_token_is_not (parser, CPP_COLON))
9511 c_parser_error (parser, "setter name must terminate with %<:%>");
9512 else
9513 c_parser_consume_token (parser);
9514 }
9515 else
9516 {
9517 if (property_getter_ident != NULL_TREE)
9518 c_parser_error (parser, "the %<getter%> attribute may only be specified once");
9519 else
9520 property_getter_ident = c_parser_peek_token (parser)->value;
9521 c_parser_consume_token (parser);
9522 }
9523 break;
9524 default:
9525 c_parser_error (parser, "unknown property attribute");
9526 syntax_error = true;
9527 break;
9528 }
9529
9530 if (syntax_error)
9531 break;
9532
9533 if (c_parser_next_token_is (parser, CPP_COMMA))
9534 c_parser_consume_token (parser);
9535 else
9536 break;
9537 }
9538 parser->objc_property_attr_context = false;
9539 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
9540 }
9541 /* ... and the property declaration(s). */
9542 properties = c_parser_struct_declaration (parser);
9543
9544 if (properties == error_mark_node)
9545 {
9546 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
9547 parser->error = false;
9548 return;
9549 }
9550
9551 if (properties == NULL_TREE)
9552 c_parser_error (parser, "expected identifier");
9553 else
9554 {
9555 /* Comma-separated properties are chained together in
9556 reverse order; add them one by one. */
9557 properties = nreverse (properties);
9558
9559 for (; properties; properties = TREE_CHAIN (properties))
9560 objc_add_property_declaration (loc, copy_node (properties),
9561 property_readonly, property_readwrite,
9562 property_assign, property_retain,
9563 property_copy, property_nonatomic,
9564 property_getter_ident, property_setter_ident);
9565 }
9566
9567 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
9568 parser->error = false;
9569 }
9570
9571 /* Parse an Objective-C @synthesize declaration. The syntax is:
9572
9573 objc-synthesize-declaration:
9574 @synthesize objc-synthesize-identifier-list ;
9575
9576 objc-synthesize-identifier-list:
9577 objc-synthesize-identifier
9578 objc-synthesize-identifier-list, objc-synthesize-identifier
9579
9580 objc-synthesize-identifier
9581 identifier
9582 identifier = identifier
9583
9584 For example:
9585 @synthesize MyProperty;
9586 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
9587
9588 PS: This function is identical to cp_parser_objc_at_synthesize_declaration
9589 for C++. Keep them in sync.
9590 */
9591 static void
9592 c_parser_objc_at_synthesize_declaration (c_parser *parser)
9593 {
9594 tree list = NULL_TREE;
9595 location_t loc;
9596 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_SYNTHESIZE));
9597 loc = c_parser_peek_token (parser)->location;
9598
9599 c_parser_consume_token (parser);
9600 while (true)
9601 {
9602 tree property, ivar;
9603 if (c_parser_next_token_is_not (parser, CPP_NAME))
9604 {
9605 c_parser_error (parser, "expected identifier");
9606 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
9607 /* Once we find the semicolon, we can resume normal parsing.
9608 We have to reset parser->error manually because
9609 c_parser_skip_until_found() won't reset it for us if the
9610 next token is precisely a semicolon. */
9611 parser->error = false;
9612 return;
9613 }
9614 property = c_parser_peek_token (parser)->value;
9615 c_parser_consume_token (parser);
9616 if (c_parser_next_token_is (parser, CPP_EQ))
9617 {
9618 c_parser_consume_token (parser);
9619 if (c_parser_next_token_is_not (parser, CPP_NAME))
9620 {
9621 c_parser_error (parser, "expected identifier");
9622 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
9623 parser->error = false;
9624 return;
9625 }
9626 ivar = c_parser_peek_token (parser)->value;
9627 c_parser_consume_token (parser);
9628 }
9629 else
9630 ivar = NULL_TREE;
9631 list = chainon (list, build_tree_list (ivar, property));
9632 if (c_parser_next_token_is (parser, CPP_COMMA))
9633 c_parser_consume_token (parser);
9634 else
9635 break;
9636 }
9637 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
9638 objc_add_synthesize_declaration (loc, list);
9639 }
9640
9641 /* Parse an Objective-C @dynamic declaration. The syntax is:
9642
9643 objc-dynamic-declaration:
9644 @dynamic identifier-list ;
9645
9646 For example:
9647 @dynamic MyProperty;
9648 @dynamic MyProperty, AnotherProperty;
9649
9650 PS: This function is identical to cp_parser_objc_at_dynamic_declaration
9651 for C++. Keep them in sync.
9652 */
9653 static void
9654 c_parser_objc_at_dynamic_declaration (c_parser *parser)
9655 {
9656 tree list = NULL_TREE;
9657 location_t loc;
9658 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_DYNAMIC));
9659 loc = c_parser_peek_token (parser)->location;
9660
9661 c_parser_consume_token (parser);
9662 while (true)
9663 {
9664 tree property;
9665 if (c_parser_next_token_is_not (parser, CPP_NAME))
9666 {
9667 c_parser_error (parser, "expected identifier");
9668 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
9669 parser->error = false;
9670 return;
9671 }
9672 property = c_parser_peek_token (parser)->value;
9673 list = chainon (list, build_tree_list (NULL_TREE, property));
9674 c_parser_consume_token (parser);
9675 if (c_parser_next_token_is (parser, CPP_COMMA))
9676 c_parser_consume_token (parser);
9677 else
9678 break;
9679 }
9680 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
9681 objc_add_dynamic_declaration (loc, list);
9682 }
9683
9684 \f
9685 /* Handle pragmas. Some OpenMP pragmas are associated with, and therefore
9686 should be considered, statements. ALLOW_STMT is true if we're within
9687 the context of a function and such pragmas are to be allowed. Returns
9688 true if we actually parsed such a pragma. */
9689
9690 static bool
9691 c_parser_pragma (c_parser *parser, enum pragma_context context)
9692 {
9693 unsigned int id;
9694
9695 id = c_parser_peek_token (parser)->pragma_kind;
9696 gcc_assert (id != PRAGMA_NONE);
9697
9698 switch (id)
9699 {
9700 case PRAGMA_OACC_ENTER_DATA:
9701 c_parser_oacc_enter_exit_data (parser, true);
9702 return false;
9703
9704 case PRAGMA_OACC_EXIT_DATA:
9705 c_parser_oacc_enter_exit_data (parser, false);
9706 return false;
9707
9708 case PRAGMA_OACC_UPDATE:
9709 if (context != pragma_compound)
9710 {
9711 if (context == pragma_stmt)
9712 c_parser_error (parser, "%<#pragma acc update%> may only be "
9713 "used in compound statements");
9714 goto bad_stmt;
9715 }
9716 c_parser_oacc_update (parser);
9717 return false;
9718
9719 case PRAGMA_OMP_BARRIER:
9720 if (context != pragma_compound)
9721 {
9722 if (context == pragma_stmt)
9723 c_parser_error (parser, "%<#pragma omp barrier%> may only be "
9724 "used in compound statements");
9725 goto bad_stmt;
9726 }
9727 c_parser_omp_barrier (parser);
9728 return false;
9729
9730 case PRAGMA_OMP_FLUSH:
9731 if (context != pragma_compound)
9732 {
9733 if (context == pragma_stmt)
9734 c_parser_error (parser, "%<#pragma omp flush%> may only be "
9735 "used in compound statements");
9736 goto bad_stmt;
9737 }
9738 c_parser_omp_flush (parser);
9739 return false;
9740
9741 case PRAGMA_OMP_TASKWAIT:
9742 if (context != pragma_compound)
9743 {
9744 if (context == pragma_stmt)
9745 c_parser_error (parser, "%<#pragma omp taskwait%> may only be "
9746 "used in compound statements");
9747 goto bad_stmt;
9748 }
9749 c_parser_omp_taskwait (parser);
9750 return false;
9751
9752 case PRAGMA_OMP_TASKYIELD:
9753 if (context != pragma_compound)
9754 {
9755 if (context == pragma_stmt)
9756 c_parser_error (parser, "%<#pragma omp taskyield%> may only be "
9757 "used in compound statements");
9758 goto bad_stmt;
9759 }
9760 c_parser_omp_taskyield (parser);
9761 return false;
9762
9763 case PRAGMA_OMP_CANCEL:
9764 if (context != pragma_compound)
9765 {
9766 if (context == pragma_stmt)
9767 c_parser_error (parser, "%<#pragma omp cancel%> may only be "
9768 "used in compound statements");
9769 goto bad_stmt;
9770 }
9771 c_parser_omp_cancel (parser);
9772 return false;
9773
9774 case PRAGMA_OMP_CANCELLATION_POINT:
9775 if (context != pragma_compound)
9776 {
9777 if (context == pragma_stmt)
9778 c_parser_error (parser, "%<#pragma omp cancellation point%> may "
9779 "only be used in compound statements");
9780 goto bad_stmt;
9781 }
9782 c_parser_omp_cancellation_point (parser);
9783 return false;
9784
9785 case PRAGMA_OMP_THREADPRIVATE:
9786 c_parser_omp_threadprivate (parser);
9787 return false;
9788
9789 case PRAGMA_OMP_TARGET:
9790 return c_parser_omp_target (parser, context);
9791
9792 case PRAGMA_OMP_END_DECLARE_TARGET:
9793 c_parser_omp_end_declare_target (parser);
9794 return false;
9795
9796 case PRAGMA_OMP_SECTION:
9797 error_at (c_parser_peek_token (parser)->location,
9798 "%<#pragma omp section%> may only be used in "
9799 "%<#pragma omp sections%> construct");
9800 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
9801 return false;
9802
9803 case PRAGMA_OMP_DECLARE_REDUCTION:
9804 c_parser_omp_declare (parser, context);
9805 return false;
9806
9807 case PRAGMA_OMP_ORDERED:
9808 return c_parser_omp_ordered (parser, context);
9809
9810 case PRAGMA_IVDEP:
9811 c_parser_consume_pragma (parser);
9812 c_parser_skip_to_pragma_eol (parser);
9813 if (!c_parser_next_token_is_keyword (parser, RID_FOR)
9814 && !c_parser_next_token_is_keyword (parser, RID_WHILE)
9815 && !c_parser_next_token_is_keyword (parser, RID_DO))
9816 {
9817 c_parser_error (parser, "for, while or do statement expected");
9818 return false;
9819 }
9820 if (c_parser_next_token_is_keyword (parser, RID_FOR))
9821 c_parser_for_statement (parser, true);
9822 else if (c_parser_next_token_is_keyword (parser, RID_WHILE))
9823 c_parser_while_statement (parser, true);
9824 else
9825 c_parser_do_statement (parser, true);
9826 return false;
9827
9828 case PRAGMA_GCC_PCH_PREPROCESS:
9829 c_parser_error (parser, "%<#pragma GCC pch_preprocess%> must be first");
9830 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
9831 return false;
9832
9833 case PRAGMA_CILK_SIMD:
9834 if (!c_parser_cilk_verify_simd (parser, context))
9835 return false;
9836 c_parser_consume_pragma (parser);
9837 c_parser_cilk_simd (parser);
9838 return false;
9839 case PRAGMA_CILK_GRAINSIZE:
9840 if (!flag_cilkplus)
9841 {
9842 warning (0, "%<#pragma grainsize%> ignored because -fcilkplus is not"
9843 " enabled");
9844 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
9845 return false;
9846 }
9847 if (context == pragma_external)
9848 {
9849 error_at (c_parser_peek_token (parser)->location,
9850 "%<#pragma grainsize%> must be inside a function");
9851 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
9852 return false;
9853 }
9854 c_parser_cilk_grainsize (parser);
9855 return false;
9856
9857 default:
9858 if (id < PRAGMA_FIRST_EXTERNAL)
9859 {
9860 if (context != pragma_stmt && context != pragma_compound)
9861 {
9862 bad_stmt:
9863 c_parser_error (parser, "expected declaration specifiers");
9864 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
9865 return false;
9866 }
9867 c_parser_omp_construct (parser);
9868 return true;
9869 }
9870 break;
9871 }
9872
9873 c_parser_consume_pragma (parser);
9874 c_invoke_pragma_handler (id);
9875
9876 /* Skip to EOL, but suppress any error message. Those will have been
9877 generated by the handler routine through calling error, as opposed
9878 to calling c_parser_error. */
9879 parser->error = true;
9880 c_parser_skip_to_pragma_eol (parser);
9881
9882 return false;
9883 }
9884
9885 /* The interface the pragma parsers have to the lexer. */
9886
9887 enum cpp_ttype
9888 pragma_lex (tree *value, location_t *loc)
9889 {
9890 c_token *tok = c_parser_peek_token (the_parser);
9891 enum cpp_ttype ret = tok->type;
9892
9893 *value = tok->value;
9894 if (loc)
9895 *loc = tok->location;
9896
9897 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
9898 ret = CPP_EOF;
9899 else
9900 {
9901 if (ret == CPP_KEYWORD)
9902 ret = CPP_NAME;
9903 c_parser_consume_token (the_parser);
9904 }
9905
9906 return ret;
9907 }
9908
9909 static void
9910 c_parser_pragma_pch_preprocess (c_parser *parser)
9911 {
9912 tree name = NULL;
9913
9914 c_parser_consume_pragma (parser);
9915 if (c_parser_next_token_is (parser, CPP_STRING))
9916 {
9917 name = c_parser_peek_token (parser)->value;
9918 c_parser_consume_token (parser);
9919 }
9920 else
9921 c_parser_error (parser, "expected string literal");
9922 c_parser_skip_to_pragma_eol (parser);
9923
9924 if (name)
9925 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
9926 }
9927 \f
9928 /* OpenACC and OpenMP parsing routines. */
9929
9930 /* Returns name of the next clause.
9931 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
9932 the token is not consumed. Otherwise appropriate pragma_omp_clause is
9933 returned and the token is consumed. */
9934
9935 static pragma_omp_clause
9936 c_parser_omp_clause_name (c_parser *parser)
9937 {
9938 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
9939
9940 if (c_parser_next_token_is_keyword (parser, RID_AUTO))
9941 result = PRAGMA_OACC_CLAUSE_AUTO;
9942 else if (c_parser_next_token_is_keyword (parser, RID_IF))
9943 result = PRAGMA_OMP_CLAUSE_IF;
9944 else if (c_parser_next_token_is_keyword (parser, RID_DEFAULT))
9945 result = PRAGMA_OMP_CLAUSE_DEFAULT;
9946 else if (c_parser_next_token_is_keyword (parser, RID_FOR))
9947 result = PRAGMA_OMP_CLAUSE_FOR;
9948 else if (c_parser_next_token_is (parser, CPP_NAME))
9949 {
9950 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
9951
9952 switch (p[0])
9953 {
9954 case 'a':
9955 if (!strcmp ("aligned", p))
9956 result = PRAGMA_OMP_CLAUSE_ALIGNED;
9957 else if (!strcmp ("async", p))
9958 result = PRAGMA_OACC_CLAUSE_ASYNC;
9959 break;
9960 case 'c':
9961 if (!strcmp ("collapse", p))
9962 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
9963 else if (!strcmp ("copy", p))
9964 result = PRAGMA_OACC_CLAUSE_COPY;
9965 else if (!strcmp ("copyin", p))
9966 result = PRAGMA_OMP_CLAUSE_COPYIN;
9967 else if (!strcmp ("copyout", p))
9968 result = PRAGMA_OACC_CLAUSE_COPYOUT;
9969 else if (!strcmp ("copyprivate", p))
9970 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
9971 else if (!strcmp ("create", p))
9972 result = PRAGMA_OACC_CLAUSE_CREATE;
9973 break;
9974 case 'd':
9975 if (!strcmp ("defaultmap", p))
9976 result = PRAGMA_OMP_CLAUSE_DEFAULTMAP;
9977 else if (!strcmp ("delete", p))
9978 result = PRAGMA_OACC_CLAUSE_DELETE;
9979 else if (!strcmp ("depend", p))
9980 result = PRAGMA_OMP_CLAUSE_DEPEND;
9981 else if (!strcmp ("device", p))
9982 result = PRAGMA_OMP_CLAUSE_DEVICE;
9983 else if (!strcmp ("deviceptr", p))
9984 result = PRAGMA_OACC_CLAUSE_DEVICEPTR;
9985 else if (!strcmp ("dist_schedule", p))
9986 result = PRAGMA_OMP_CLAUSE_DIST_SCHEDULE;
9987 break;
9988 case 'f':
9989 if (!strcmp ("final", p))
9990 result = PRAGMA_OMP_CLAUSE_FINAL;
9991 else if (!strcmp ("firstprivate", p))
9992 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
9993 else if (!strcmp ("from", p))
9994 result = PRAGMA_OMP_CLAUSE_FROM;
9995 break;
9996 case 'g':
9997 if (!strcmp ("gang", p))
9998 result = PRAGMA_OACC_CLAUSE_GANG;
9999 else if (!strcmp ("grainsize", p))
10000 result = PRAGMA_OMP_CLAUSE_GRAINSIZE;
10001 break;
10002 case 'h':
10003 if (!strcmp ("hint", p))
10004 result = PRAGMA_OMP_CLAUSE_HINT;
10005 else if (!strcmp ("host", p))
10006 result = PRAGMA_OACC_CLAUSE_HOST;
10007 break;
10008 case 'i':
10009 if (!strcmp ("inbranch", p))
10010 result = PRAGMA_OMP_CLAUSE_INBRANCH;
10011 else if (!strcmp ("is_device_ptr", p))
10012 result = PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR;
10013 break;
10014 case 'l':
10015 if (!strcmp ("lastprivate", p))
10016 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
10017 else if (!strcmp ("linear", p))
10018 result = PRAGMA_OMP_CLAUSE_LINEAR;
10019 else if (!strcmp ("link", p))
10020 result = PRAGMA_OMP_CLAUSE_LINK;
10021 break;
10022 case 'm':
10023 if (!strcmp ("map", p))
10024 result = PRAGMA_OMP_CLAUSE_MAP;
10025 else if (!strcmp ("mergeable", p))
10026 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
10027 else if (flag_cilkplus && !strcmp ("mask", p))
10028 result = PRAGMA_CILK_CLAUSE_MASK;
10029 break;
10030 case 'n':
10031 if (!strcmp ("nogroup", p))
10032 result = PRAGMA_OMP_CLAUSE_NOGROUP;
10033 else if (!strcmp ("notinbranch", p))
10034 result = PRAGMA_OMP_CLAUSE_NOTINBRANCH;
10035 else if (!strcmp ("nowait", p))
10036 result = PRAGMA_OMP_CLAUSE_NOWAIT;
10037 else if (!strcmp ("num_gangs", p))
10038 result = PRAGMA_OACC_CLAUSE_NUM_GANGS;
10039 else if (!strcmp ("num_tasks", p))
10040 result = PRAGMA_OMP_CLAUSE_NUM_TASKS;
10041 else if (!strcmp ("num_teams", p))
10042 result = PRAGMA_OMP_CLAUSE_NUM_TEAMS;
10043 else if (!strcmp ("num_threads", p))
10044 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
10045 else if (!strcmp ("num_workers", p))
10046 result = PRAGMA_OACC_CLAUSE_NUM_WORKERS;
10047 else if (flag_cilkplus && !strcmp ("nomask", p))
10048 result = PRAGMA_CILK_CLAUSE_NOMASK;
10049 break;
10050 case 'o':
10051 if (!strcmp ("ordered", p))
10052 result = PRAGMA_OMP_CLAUSE_ORDERED;
10053 break;
10054 case 'p':
10055 if (!strcmp ("parallel", p))
10056 result = PRAGMA_OMP_CLAUSE_PARALLEL;
10057 else if (!strcmp ("present", p))
10058 result = PRAGMA_OACC_CLAUSE_PRESENT;
10059 else if (!strcmp ("present_or_copy", p)
10060 || !strcmp ("pcopy", p))
10061 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY;
10062 else if (!strcmp ("present_or_copyin", p)
10063 || !strcmp ("pcopyin", p))
10064 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN;
10065 else if (!strcmp ("present_or_copyout", p)
10066 || !strcmp ("pcopyout", p))
10067 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT;
10068 else if (!strcmp ("present_or_create", p)
10069 || !strcmp ("pcreate", p))
10070 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE;
10071 else if (!strcmp ("priority", p))
10072 result = PRAGMA_OMP_CLAUSE_PRIORITY;
10073 else if (!strcmp ("private", p))
10074 result = PRAGMA_OMP_CLAUSE_PRIVATE;
10075 else if (!strcmp ("proc_bind", p))
10076 result = PRAGMA_OMP_CLAUSE_PROC_BIND;
10077 break;
10078 case 'r':
10079 if (!strcmp ("reduction", p))
10080 result = PRAGMA_OMP_CLAUSE_REDUCTION;
10081 break;
10082 case 's':
10083 if (!strcmp ("safelen", p))
10084 result = PRAGMA_OMP_CLAUSE_SAFELEN;
10085 else if (!strcmp ("schedule", p))
10086 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
10087 else if (!strcmp ("sections", p))
10088 result = PRAGMA_OMP_CLAUSE_SECTIONS;
10089 else if (!strcmp ("seq", p))
10090 result = PRAGMA_OACC_CLAUSE_SEQ;
10091 else if (!strcmp ("shared", p))
10092 result = PRAGMA_OMP_CLAUSE_SHARED;
10093 else if (!strcmp ("simd", p))
10094 result = PRAGMA_OMP_CLAUSE_SIMD;
10095 else if (!strcmp ("simdlen", p))
10096 result = PRAGMA_OMP_CLAUSE_SIMDLEN;
10097 else if (!strcmp ("self", p))
10098 result = PRAGMA_OACC_CLAUSE_SELF;
10099 break;
10100 case 't':
10101 if (!strcmp ("taskgroup", p))
10102 result = PRAGMA_OMP_CLAUSE_TASKGROUP;
10103 else if (!strcmp ("thread_limit", p))
10104 result = PRAGMA_OMP_CLAUSE_THREAD_LIMIT;
10105 else if (!strcmp ("threads", p))
10106 result = PRAGMA_OMP_CLAUSE_THREADS;
10107 else if (!strcmp ("to", p))
10108 result = PRAGMA_OMP_CLAUSE_TO;
10109 break;
10110 case 'u':
10111 if (!strcmp ("uniform", p))
10112 result = PRAGMA_OMP_CLAUSE_UNIFORM;
10113 else if (!strcmp ("untied", p))
10114 result = PRAGMA_OMP_CLAUSE_UNTIED;
10115 else if (!strcmp ("use_device_ptr", p))
10116 result = PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR;
10117 break;
10118 case 'v':
10119 if (!strcmp ("vector", p))
10120 result = PRAGMA_OACC_CLAUSE_VECTOR;
10121 else if (!strcmp ("vector_length", p))
10122 result = PRAGMA_OACC_CLAUSE_VECTOR_LENGTH;
10123 else if (flag_cilkplus && !strcmp ("vectorlength", p))
10124 result = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
10125 break;
10126 case 'w':
10127 if (!strcmp ("wait", p))
10128 result = PRAGMA_OACC_CLAUSE_WAIT;
10129 else if (!strcmp ("worker", p))
10130 result = PRAGMA_OACC_CLAUSE_WORKER;
10131 break;
10132 }
10133 }
10134
10135 if (result != PRAGMA_OMP_CLAUSE_NONE)
10136 c_parser_consume_token (parser);
10137
10138 return result;
10139 }
10140
10141 /* Validate that a clause of the given type does not already exist. */
10142
10143 static void
10144 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
10145 const char *name)
10146 {
10147 tree c;
10148
10149 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
10150 if (OMP_CLAUSE_CODE (c) == code)
10151 {
10152 location_t loc = OMP_CLAUSE_LOCATION (c);
10153 error_at (loc, "too many %qs clauses", name);
10154 break;
10155 }
10156 }
10157
10158 /* OpenACC 2.0
10159 Parse wait clause or wait directive parameters. */
10160
10161 static tree
10162 c_parser_oacc_wait_list (c_parser *parser, location_t clause_loc, tree list)
10163 {
10164 vec<tree, va_gc> *args;
10165 tree t, args_tree;
10166
10167 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10168 return list;
10169
10170 args = c_parser_expr_list (parser, false, true, NULL, NULL, NULL, NULL);
10171
10172 if (args->length () == 0)
10173 {
10174 c_parser_error (parser, "expected integer expression before ')'");
10175 release_tree_vector (args);
10176 return list;
10177 }
10178
10179 args_tree = build_tree_list_vec (args);
10180
10181 for (t = args_tree; t; t = TREE_CHAIN (t))
10182 {
10183 tree targ = TREE_VALUE (t);
10184
10185 if (targ != error_mark_node)
10186 {
10187 if (!INTEGRAL_TYPE_P (TREE_TYPE (targ)))
10188 {
10189 c_parser_error (parser, "expression must be integral");
10190 targ = error_mark_node;
10191 }
10192 else
10193 {
10194 tree c = build_omp_clause (clause_loc, OMP_CLAUSE_WAIT);
10195
10196 OMP_CLAUSE_DECL (c) = targ;
10197 OMP_CLAUSE_CHAIN (c) = list;
10198 list = c;
10199 }
10200 }
10201 }
10202
10203 release_tree_vector (args);
10204 c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10205 return list;
10206 }
10207
10208 /* OpenACC 2.0, OpenMP 2.5:
10209 variable-list:
10210 identifier
10211 variable-list , identifier
10212
10213 If KIND is nonzero, create the appropriate node and install the
10214 decl in OMP_CLAUSE_DECL and add the node to the head of the list.
10215 If KIND is nonzero, CLAUSE_LOC is the location of the clause.
10216
10217 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
10218 return the list created. */
10219
10220 static tree
10221 c_parser_omp_variable_list (c_parser *parser,
10222 location_t clause_loc,
10223 enum omp_clause_code kind, tree list)
10224 {
10225 if (c_parser_next_token_is_not (parser, CPP_NAME)
10226 || c_parser_peek_token (parser)->id_kind != C_ID_ID)
10227 c_parser_error (parser, "expected identifier");
10228
10229 while (c_parser_next_token_is (parser, CPP_NAME)
10230 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
10231 {
10232 tree t = lookup_name (c_parser_peek_token (parser)->value);
10233
10234 if (t == NULL_TREE)
10235 {
10236 undeclared_variable (c_parser_peek_token (parser)->location,
10237 c_parser_peek_token (parser)->value);
10238 t = error_mark_node;
10239 }
10240
10241 c_parser_consume_token (parser);
10242
10243 if (t == error_mark_node)
10244 ;
10245 else if (kind != 0)
10246 {
10247 switch (kind)
10248 {
10249 case OMP_CLAUSE__CACHE_:
10250 if (c_parser_peek_token (parser)->type != CPP_OPEN_SQUARE)
10251 {
10252 c_parser_error (parser, "expected %<[%>");
10253 t = error_mark_node;
10254 break;
10255 }
10256 /* FALLTHROUGH */
10257 case OMP_CLAUSE_MAP:
10258 case OMP_CLAUSE_FROM:
10259 case OMP_CLAUSE_TO:
10260 while (c_parser_next_token_is (parser, CPP_DOT))
10261 {
10262 location_t op_loc = c_parser_peek_token (parser)->location;
10263 c_parser_consume_token (parser);
10264 if (!c_parser_next_token_is (parser, CPP_NAME))
10265 {
10266 c_parser_error (parser, "expected identifier");
10267 t = error_mark_node;
10268 break;
10269 }
10270 tree ident = c_parser_peek_token (parser)->value;
10271 c_parser_consume_token (parser);
10272 t = build_component_ref (op_loc, t, ident);
10273 }
10274 /* FALLTHROUGH */
10275 case OMP_CLAUSE_DEPEND:
10276 case OMP_CLAUSE_REDUCTION:
10277 while (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
10278 {
10279 tree low_bound = NULL_TREE, length = NULL_TREE;
10280
10281 c_parser_consume_token (parser);
10282 if (!c_parser_next_token_is (parser, CPP_COLON))
10283 {
10284 low_bound = c_parser_expression (parser).value;
10285 mark_exp_read (low_bound);
10286 }
10287 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
10288 length = integer_one_node;
10289 else
10290 {
10291 /* Look for `:'. */
10292 if (!c_parser_require (parser, CPP_COLON,
10293 "expected %<:%>"))
10294 {
10295 t = error_mark_node;
10296 break;
10297 }
10298 if (!c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
10299 {
10300 length = c_parser_expression (parser).value;
10301 mark_exp_read (length);
10302 }
10303 }
10304 /* Look for the closing `]'. */
10305 if (!c_parser_require (parser, CPP_CLOSE_SQUARE,
10306 "expected %<]%>"))
10307 {
10308 t = error_mark_node;
10309 break;
10310 }
10311
10312 if (kind == OMP_CLAUSE__CACHE_)
10313 {
10314 if (TREE_CODE (low_bound) != INTEGER_CST
10315 && !TREE_READONLY (low_bound))
10316 {
10317 error_at (clause_loc,
10318 "%qD is not a constant", low_bound);
10319 t = error_mark_node;
10320 }
10321
10322 if (TREE_CODE (length) != INTEGER_CST
10323 && !TREE_READONLY (length))
10324 {
10325 error_at (clause_loc,
10326 "%qD is not a constant", length);
10327 t = error_mark_node;
10328 }
10329 }
10330
10331 t = tree_cons (low_bound, length, t);
10332 }
10333 break;
10334 default:
10335 break;
10336 }
10337
10338 if (t != error_mark_node)
10339 {
10340 tree u = build_omp_clause (clause_loc, kind);
10341 OMP_CLAUSE_DECL (u) = t;
10342 OMP_CLAUSE_CHAIN (u) = list;
10343 list = u;
10344 }
10345 }
10346 else
10347 list = tree_cons (t, NULL_TREE, list);
10348
10349 if (c_parser_next_token_is_not (parser, CPP_COMMA))
10350 break;
10351
10352 c_parser_consume_token (parser);
10353 }
10354
10355 return list;
10356 }
10357
10358 /* Similarly, but expect leading and trailing parenthesis. This is a very
10359 common case for OpenACC and OpenMP clauses. */
10360
10361 static tree
10362 c_parser_omp_var_list_parens (c_parser *parser, enum omp_clause_code kind,
10363 tree list)
10364 {
10365 /* The clauses location. */
10366 location_t loc = c_parser_peek_token (parser)->location;
10367
10368 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10369 {
10370 list = c_parser_omp_variable_list (parser, loc, kind, list);
10371 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10372 }
10373 return list;
10374 }
10375
10376 /* OpenACC 2.0:
10377 copy ( variable-list )
10378 copyin ( variable-list )
10379 copyout ( variable-list )
10380 create ( variable-list )
10381 delete ( variable-list )
10382 present ( variable-list )
10383 present_or_copy ( variable-list )
10384 pcopy ( variable-list )
10385 present_or_copyin ( variable-list )
10386 pcopyin ( variable-list )
10387 present_or_copyout ( variable-list )
10388 pcopyout ( variable-list )
10389 present_or_create ( variable-list )
10390 pcreate ( variable-list ) */
10391
10392 static tree
10393 c_parser_oacc_data_clause (c_parser *parser, pragma_omp_clause c_kind,
10394 tree list)
10395 {
10396 enum gomp_map_kind kind;
10397 switch (c_kind)
10398 {
10399 case PRAGMA_OACC_CLAUSE_COPY:
10400 kind = GOMP_MAP_FORCE_TOFROM;
10401 break;
10402 case PRAGMA_OACC_CLAUSE_COPYIN:
10403 kind = GOMP_MAP_FORCE_TO;
10404 break;
10405 case PRAGMA_OACC_CLAUSE_COPYOUT:
10406 kind = GOMP_MAP_FORCE_FROM;
10407 break;
10408 case PRAGMA_OACC_CLAUSE_CREATE:
10409 kind = GOMP_MAP_FORCE_ALLOC;
10410 break;
10411 case PRAGMA_OACC_CLAUSE_DELETE:
10412 kind = GOMP_MAP_FORCE_DEALLOC;
10413 break;
10414 case PRAGMA_OACC_CLAUSE_DEVICE:
10415 kind = GOMP_MAP_FORCE_TO;
10416 break;
10417 case PRAGMA_OACC_CLAUSE_HOST:
10418 case PRAGMA_OACC_CLAUSE_SELF:
10419 kind = GOMP_MAP_FORCE_FROM;
10420 break;
10421 case PRAGMA_OACC_CLAUSE_PRESENT:
10422 kind = GOMP_MAP_FORCE_PRESENT;
10423 break;
10424 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY:
10425 kind = GOMP_MAP_TOFROM;
10426 break;
10427 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN:
10428 kind = GOMP_MAP_TO;
10429 break;
10430 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT:
10431 kind = GOMP_MAP_FROM;
10432 break;
10433 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE:
10434 kind = GOMP_MAP_ALLOC;
10435 break;
10436 default:
10437 gcc_unreachable ();
10438 }
10439 tree nl, c;
10440 nl = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_MAP, list);
10441
10442 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
10443 OMP_CLAUSE_SET_MAP_KIND (c, kind);
10444
10445 return nl;
10446 }
10447
10448 /* OpenACC 2.0:
10449 deviceptr ( variable-list ) */
10450
10451 static tree
10452 c_parser_oacc_data_clause_deviceptr (c_parser *parser, tree list)
10453 {
10454 location_t loc = c_parser_peek_token (parser)->location;
10455 tree vars, t;
10456
10457 /* Can't use OMP_CLAUSE_MAP here (that is, can't use the generic
10458 c_parser_oacc_data_clause), as for PRAGMA_OACC_CLAUSE_DEVICEPTR,
10459 variable-list must only allow for pointer variables. */
10460 vars = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_ERROR, NULL);
10461 for (t = vars; t && t; t = TREE_CHAIN (t))
10462 {
10463 tree v = TREE_PURPOSE (t);
10464
10465 /* FIXME diagnostics: Ideally we should keep individual
10466 locations for all the variables in the var list to make the
10467 following errors more precise. Perhaps
10468 c_parser_omp_var_list_parens() should construct a list of
10469 locations to go along with the var list. */
10470
10471 if (!VAR_P (v))
10472 error_at (loc, "%qD is not a variable", v);
10473 else if (TREE_TYPE (v) == error_mark_node)
10474 ;
10475 else if (!POINTER_TYPE_P (TREE_TYPE (v)))
10476 error_at (loc, "%qD is not a pointer variable", v);
10477
10478 tree u = build_omp_clause (loc, OMP_CLAUSE_MAP);
10479 OMP_CLAUSE_SET_MAP_KIND (u, GOMP_MAP_FORCE_DEVICEPTR);
10480 OMP_CLAUSE_DECL (u) = v;
10481 OMP_CLAUSE_CHAIN (u) = list;
10482 list = u;
10483 }
10484
10485 return list;
10486 }
10487
10488 /* OpenACC 2.0, OpenMP 3.0:
10489 collapse ( constant-expression ) */
10490
10491 static tree
10492 c_parser_omp_clause_collapse (c_parser *parser, tree list)
10493 {
10494 tree c, num = error_mark_node;
10495 HOST_WIDE_INT n;
10496 location_t loc;
10497
10498 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse");
10499
10500 loc = c_parser_peek_token (parser)->location;
10501 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10502 {
10503 num = c_parser_expr_no_commas (parser, NULL).value;
10504 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10505 }
10506 if (num == error_mark_node)
10507 return list;
10508 mark_exp_read (num);
10509 num = c_fully_fold (num, false, NULL);
10510 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
10511 || !tree_fits_shwi_p (num)
10512 || (n = tree_to_shwi (num)) <= 0
10513 || (int) n != n)
10514 {
10515 error_at (loc,
10516 "collapse argument needs positive constant integer expression");
10517 return list;
10518 }
10519 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
10520 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
10521 OMP_CLAUSE_CHAIN (c) = list;
10522 return c;
10523 }
10524
10525 /* OpenMP 2.5:
10526 copyin ( variable-list ) */
10527
10528 static tree
10529 c_parser_omp_clause_copyin (c_parser *parser, tree list)
10530 {
10531 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_COPYIN, list);
10532 }
10533
10534 /* OpenMP 2.5:
10535 copyprivate ( variable-list ) */
10536
10537 static tree
10538 c_parser_omp_clause_copyprivate (c_parser *parser, tree list)
10539 {
10540 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_COPYPRIVATE, list);
10541 }
10542
10543 /* OpenMP 2.5:
10544 default ( shared | none ) */
10545
10546 static tree
10547 c_parser_omp_clause_default (c_parser *parser, tree list)
10548 {
10549 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
10550 location_t loc = c_parser_peek_token (parser)->location;
10551 tree c;
10552
10553 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10554 return list;
10555 if (c_parser_next_token_is (parser, CPP_NAME))
10556 {
10557 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
10558
10559 switch (p[0])
10560 {
10561 case 'n':
10562 if (strcmp ("none", p) != 0)
10563 goto invalid_kind;
10564 kind = OMP_CLAUSE_DEFAULT_NONE;
10565 break;
10566
10567 case 's':
10568 if (strcmp ("shared", p) != 0)
10569 goto invalid_kind;
10570 kind = OMP_CLAUSE_DEFAULT_SHARED;
10571 break;
10572
10573 default:
10574 goto invalid_kind;
10575 }
10576
10577 c_parser_consume_token (parser);
10578 }
10579 else
10580 {
10581 invalid_kind:
10582 c_parser_error (parser, "expected %<none%> or %<shared%>");
10583 }
10584 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10585
10586 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
10587 return list;
10588
10589 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default");
10590 c = build_omp_clause (loc, OMP_CLAUSE_DEFAULT);
10591 OMP_CLAUSE_CHAIN (c) = list;
10592 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
10593
10594 return c;
10595 }
10596
10597 /* OpenMP 2.5:
10598 firstprivate ( variable-list ) */
10599
10600 static tree
10601 c_parser_omp_clause_firstprivate (c_parser *parser, tree list)
10602 {
10603 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_FIRSTPRIVATE, list);
10604 }
10605
10606 /* OpenMP 3.1:
10607 final ( expression ) */
10608
10609 static tree
10610 c_parser_omp_clause_final (c_parser *parser, tree list)
10611 {
10612 location_t loc = c_parser_peek_token (parser)->location;
10613 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
10614 {
10615 tree t = c_parser_paren_condition (parser);
10616 tree c;
10617
10618 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final");
10619
10620 c = build_omp_clause (loc, OMP_CLAUSE_FINAL);
10621 OMP_CLAUSE_FINAL_EXPR (c) = t;
10622 OMP_CLAUSE_CHAIN (c) = list;
10623 list = c;
10624 }
10625 else
10626 c_parser_error (parser, "expected %<(%>");
10627
10628 return list;
10629 }
10630
10631 /* OpenACC, OpenMP 2.5:
10632 if ( expression )
10633
10634 OpenMP 4.5:
10635 if ( directive-name-modifier : expression )
10636
10637 directive-name-modifier:
10638 parallel | task | taskloop | target data | target | target update
10639 | target enter data | target exit data */
10640
10641 static tree
10642 c_parser_omp_clause_if (c_parser *parser, tree list, bool is_omp)
10643 {
10644 location_t location = c_parser_peek_token (parser)->location;
10645 enum tree_code if_modifier = ERROR_MARK;
10646
10647 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10648 return list;
10649
10650 if (is_omp && c_parser_next_token_is (parser, CPP_NAME))
10651 {
10652 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
10653 int n = 2;
10654 if (strcmp (p, "parallel") == 0)
10655 if_modifier = OMP_PARALLEL;
10656 else if (strcmp (p, "task") == 0)
10657 if_modifier = OMP_TASK;
10658 else if (strcmp (p, "taskloop") == 0)
10659 if_modifier = OMP_TASKLOOP;
10660 else if (strcmp (p, "target") == 0)
10661 {
10662 if_modifier = OMP_TARGET;
10663 if (c_parser_peek_2nd_token (parser)->type == CPP_NAME)
10664 {
10665 p = IDENTIFIER_POINTER (c_parser_peek_2nd_token (parser)->value);
10666 if (strcmp ("data", p) == 0)
10667 if_modifier = OMP_TARGET_DATA;
10668 else if (strcmp ("update", p) == 0)
10669 if_modifier = OMP_TARGET_UPDATE;
10670 else if (strcmp ("enter", p) == 0)
10671 if_modifier = OMP_TARGET_ENTER_DATA;
10672 else if (strcmp ("exit", p) == 0)
10673 if_modifier = OMP_TARGET_EXIT_DATA;
10674 if (if_modifier != OMP_TARGET)
10675 {
10676 n = 3;
10677 c_parser_consume_token (parser);
10678 }
10679 else
10680 {
10681 location_t loc = c_parser_peek_2nd_token (parser)->location;
10682 error_at (loc, "expected %<data%>, %<update%>, %<enter%> "
10683 "or %<exit%>");
10684 if_modifier = ERROR_MARK;
10685 }
10686 if (if_modifier == OMP_TARGET_ENTER_DATA
10687 || if_modifier == OMP_TARGET_EXIT_DATA)
10688 {
10689 if (c_parser_peek_2nd_token (parser)->type == CPP_NAME)
10690 {
10691 p = IDENTIFIER_POINTER
10692 (c_parser_peek_2nd_token (parser)->value);
10693 if (strcmp ("data", p) == 0)
10694 n = 4;
10695 }
10696 if (n == 4)
10697 c_parser_consume_token (parser);
10698 else
10699 {
10700 location_t loc
10701 = c_parser_peek_2nd_token (parser)->location;
10702 error_at (loc, "expected %<data%>");
10703 if_modifier = ERROR_MARK;
10704 }
10705 }
10706 }
10707 }
10708 if (if_modifier != ERROR_MARK)
10709 {
10710 if (c_parser_peek_2nd_token (parser)->type == CPP_COLON)
10711 {
10712 c_parser_consume_token (parser);
10713 c_parser_consume_token (parser);
10714 }
10715 else
10716 {
10717 if (n > 2)
10718 {
10719 location_t loc = c_parser_peek_2nd_token (parser)->location;
10720 error_at (loc, "expected %<:%>");
10721 }
10722 if_modifier = ERROR_MARK;
10723 }
10724 }
10725 }
10726
10727 tree t = c_parser_condition (parser), c;
10728 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10729
10730 for (c = list; c ; c = OMP_CLAUSE_CHAIN (c))
10731 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IF)
10732 {
10733 if (if_modifier != ERROR_MARK
10734 && OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
10735 {
10736 const char *p = NULL;
10737 switch (if_modifier)
10738 {
10739 case OMP_PARALLEL: p = "parallel"; break;
10740 case OMP_TASK: p = "task"; break;
10741 case OMP_TASKLOOP: p = "taskloop"; break;
10742 case OMP_TARGET_DATA: p = "target data"; break;
10743 case OMP_TARGET: p = "target"; break;
10744 case OMP_TARGET_UPDATE: p = "target update"; break;
10745 case OMP_TARGET_ENTER_DATA: p = "enter data"; break;
10746 case OMP_TARGET_EXIT_DATA: p = "exit data"; break;
10747 default: gcc_unreachable ();
10748 }
10749 error_at (location, "too many %<if%> clauses with %qs modifier",
10750 p);
10751 return list;
10752 }
10753 else if (OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
10754 {
10755 if (!is_omp)
10756 error_at (location, "too many %<if%> clauses");
10757 else
10758 error_at (location, "too many %<if%> clauses without modifier");
10759 return list;
10760 }
10761 else if (if_modifier == ERROR_MARK
10762 || OMP_CLAUSE_IF_MODIFIER (c) == ERROR_MARK)
10763 {
10764 error_at (location, "if any %<if%> clause has modifier, then all "
10765 "%<if%> clauses have to use modifier");
10766 return list;
10767 }
10768 }
10769
10770 c = build_omp_clause (location, OMP_CLAUSE_IF);
10771 OMP_CLAUSE_IF_MODIFIER (c) = if_modifier;
10772 OMP_CLAUSE_IF_EXPR (c) = t;
10773 OMP_CLAUSE_CHAIN (c) = list;
10774 return c;
10775 }
10776
10777 /* OpenMP 2.5:
10778 lastprivate ( variable-list ) */
10779
10780 static tree
10781 c_parser_omp_clause_lastprivate (c_parser *parser, tree list)
10782 {
10783 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_LASTPRIVATE, list);
10784 }
10785
10786 /* OpenMP 3.1:
10787 mergeable */
10788
10789 static tree
10790 c_parser_omp_clause_mergeable (c_parser *parser ATTRIBUTE_UNUSED, tree list)
10791 {
10792 tree c;
10793
10794 /* FIXME: Should we allow duplicates? */
10795 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable");
10796
10797 c = build_omp_clause (c_parser_peek_token (parser)->location,
10798 OMP_CLAUSE_MERGEABLE);
10799 OMP_CLAUSE_CHAIN (c) = list;
10800
10801 return c;
10802 }
10803
10804 /* OpenMP 2.5:
10805 nowait */
10806
10807 static tree
10808 c_parser_omp_clause_nowait (c_parser *parser ATTRIBUTE_UNUSED, tree list)
10809 {
10810 tree c;
10811 location_t loc = c_parser_peek_token (parser)->location;
10812
10813 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait");
10814
10815 c = build_omp_clause (loc, OMP_CLAUSE_NOWAIT);
10816 OMP_CLAUSE_CHAIN (c) = list;
10817 return c;
10818 }
10819
10820 /* OpenACC:
10821 num_gangs ( expression ) */
10822
10823 static tree
10824 c_parser_omp_clause_num_gangs (c_parser *parser, tree list)
10825 {
10826 location_t num_gangs_loc = c_parser_peek_token (parser)->location;
10827 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10828 {
10829 location_t expr_loc = c_parser_peek_token (parser)->location;
10830 tree c, t = c_parser_expression (parser).value;
10831 mark_exp_read (t);
10832 t = c_fully_fold (t, false, NULL);
10833
10834 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10835
10836 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
10837 {
10838 c_parser_error (parser, "expected integer expression");
10839 return list;
10840 }
10841
10842 /* Attempt to statically determine when the number isn't positive. */
10843 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
10844 build_int_cst (TREE_TYPE (t), 0));
10845 protected_set_expr_location (c, expr_loc);
10846 if (c == boolean_true_node)
10847 {
10848 warning_at (expr_loc, 0,
10849 "%<num_gangs%> value must be positive");
10850 t = integer_one_node;
10851 }
10852
10853 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_GANGS, "num_gangs");
10854
10855 c = build_omp_clause (num_gangs_loc, OMP_CLAUSE_NUM_GANGS);
10856 OMP_CLAUSE_NUM_GANGS_EXPR (c) = t;
10857 OMP_CLAUSE_CHAIN (c) = list;
10858 list = c;
10859 }
10860
10861 return list;
10862 }
10863
10864 /* OpenMP 2.5:
10865 num_threads ( expression ) */
10866
10867 static tree
10868 c_parser_omp_clause_num_threads (c_parser *parser, tree list)
10869 {
10870 location_t num_threads_loc = c_parser_peek_token (parser)->location;
10871 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10872 {
10873 location_t expr_loc = c_parser_peek_token (parser)->location;
10874 tree c, t = c_parser_expression (parser).value;
10875 mark_exp_read (t);
10876 t = c_fully_fold (t, false, NULL);
10877
10878 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10879
10880 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
10881 {
10882 c_parser_error (parser, "expected integer expression");
10883 return list;
10884 }
10885
10886 /* Attempt to statically determine when the number isn't positive. */
10887 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
10888 build_int_cst (TREE_TYPE (t), 0));
10889 protected_set_expr_location (c, expr_loc);
10890 if (c == boolean_true_node)
10891 {
10892 warning_at (expr_loc, 0,
10893 "%<num_threads%> value must be positive");
10894 t = integer_one_node;
10895 }
10896
10897 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS, "num_threads");
10898
10899 c = build_omp_clause (num_threads_loc, OMP_CLAUSE_NUM_THREADS);
10900 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
10901 OMP_CLAUSE_CHAIN (c) = list;
10902 list = c;
10903 }
10904
10905 return list;
10906 }
10907
10908 /* OpenMP 4.5:
10909 num_tasks ( expression ) */
10910
10911 static tree
10912 c_parser_omp_clause_num_tasks (c_parser *parser, tree list)
10913 {
10914 location_t num_tasks_loc = c_parser_peek_token (parser)->location;
10915 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10916 {
10917 location_t expr_loc = c_parser_peek_token (parser)->location;
10918 tree c, t = c_parser_expression (parser).value;
10919 mark_exp_read (t);
10920 t = c_fully_fold (t, false, NULL);
10921
10922 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10923
10924 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
10925 {
10926 c_parser_error (parser, "expected integer expression");
10927 return list;
10928 }
10929
10930 /* Attempt to statically determine when the number isn't positive. */
10931 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
10932 build_int_cst (TREE_TYPE (t), 0));
10933 if (CAN_HAVE_LOCATION_P (c))
10934 SET_EXPR_LOCATION (c, expr_loc);
10935 if (c == boolean_true_node)
10936 {
10937 warning_at (expr_loc, 0, "%<num_tasks%> value must be positive");
10938 t = integer_one_node;
10939 }
10940
10941 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TASKS, "num_tasks");
10942
10943 c = build_omp_clause (num_tasks_loc, OMP_CLAUSE_NUM_TASKS);
10944 OMP_CLAUSE_NUM_TASKS_EXPR (c) = t;
10945 OMP_CLAUSE_CHAIN (c) = list;
10946 list = c;
10947 }
10948
10949 return list;
10950 }
10951
10952 /* OpenMP 4.5:
10953 grainsize ( expression ) */
10954
10955 static tree
10956 c_parser_omp_clause_grainsize (c_parser *parser, tree list)
10957 {
10958 location_t grainsize_loc = c_parser_peek_token (parser)->location;
10959 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10960 {
10961 location_t expr_loc = c_parser_peek_token (parser)->location;
10962 tree c, t = c_parser_expression (parser).value;
10963 mark_exp_read (t);
10964 t = c_fully_fold (t, false, NULL);
10965
10966 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10967
10968 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
10969 {
10970 c_parser_error (parser, "expected integer expression");
10971 return list;
10972 }
10973
10974 /* Attempt to statically determine when the number isn't positive. */
10975 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
10976 build_int_cst (TREE_TYPE (t), 0));
10977 if (CAN_HAVE_LOCATION_P (c))
10978 SET_EXPR_LOCATION (c, expr_loc);
10979 if (c == boolean_true_node)
10980 {
10981 warning_at (expr_loc, 0, "%<grainsize%> value must be positive");
10982 t = integer_one_node;
10983 }
10984
10985 check_no_duplicate_clause (list, OMP_CLAUSE_GRAINSIZE, "grainsize");
10986
10987 c = build_omp_clause (grainsize_loc, OMP_CLAUSE_GRAINSIZE);
10988 OMP_CLAUSE_GRAINSIZE_EXPR (c) = t;
10989 OMP_CLAUSE_CHAIN (c) = list;
10990 list = c;
10991 }
10992
10993 return list;
10994 }
10995
10996 /* OpenMP 4.5:
10997 priority ( expression ) */
10998
10999 static tree
11000 c_parser_omp_clause_priority (c_parser *parser, tree list)
11001 {
11002 location_t priority_loc = c_parser_peek_token (parser)->location;
11003 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11004 {
11005 location_t expr_loc = c_parser_peek_token (parser)->location;
11006 tree c, t = c_parser_expression (parser).value;
11007 mark_exp_read (t);
11008 t = c_fully_fold (t, false, NULL);
11009
11010 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11011
11012 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
11013 {
11014 c_parser_error (parser, "expected integer expression");
11015 return list;
11016 }
11017
11018 /* Attempt to statically determine when the number isn't
11019 non-negative. */
11020 c = fold_build2_loc (expr_loc, LT_EXPR, boolean_type_node, t,
11021 build_int_cst (TREE_TYPE (t), 0));
11022 if (CAN_HAVE_LOCATION_P (c))
11023 SET_EXPR_LOCATION (c, expr_loc);
11024 if (c == boolean_true_node)
11025 {
11026 warning_at (expr_loc, 0, "%<priority%> value must be non-negative");
11027 t = integer_one_node;
11028 }
11029
11030 check_no_duplicate_clause (list, OMP_CLAUSE_PRIORITY, "priority");
11031
11032 c = build_omp_clause (priority_loc, OMP_CLAUSE_PRIORITY);
11033 OMP_CLAUSE_PRIORITY_EXPR (c) = t;
11034 OMP_CLAUSE_CHAIN (c) = list;
11035 list = c;
11036 }
11037
11038 return list;
11039 }
11040
11041 /* OpenMP 4.5:
11042 hint ( expression ) */
11043
11044 static tree
11045 c_parser_omp_clause_hint (c_parser *parser, tree list)
11046 {
11047 location_t hint_loc = c_parser_peek_token (parser)->location;
11048 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11049 {
11050 tree c, t = c_parser_expression (parser).value;
11051 mark_exp_read (t);
11052 t = c_fully_fold (t, false, NULL);
11053
11054 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11055
11056 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
11057 {
11058 c_parser_error (parser, "expected integer expression");
11059 return list;
11060 }
11061
11062 check_no_duplicate_clause (list, OMP_CLAUSE_HINT, "hint");
11063
11064 c = build_omp_clause (hint_loc, OMP_CLAUSE_HINT);
11065 OMP_CLAUSE_HINT_EXPR (c) = t;
11066 OMP_CLAUSE_CHAIN (c) = list;
11067 list = c;
11068 }
11069
11070 return list;
11071 }
11072
11073 /* OpenMP 4.5:
11074 defaultmap ( tofrom : scalar ) */
11075
11076 static tree
11077 c_parser_omp_clause_defaultmap (c_parser *parser, tree list)
11078 {
11079 location_t loc = c_parser_peek_token (parser)->location;
11080 tree c;
11081 const char *p;
11082
11083 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11084 return list;
11085 if (!c_parser_next_token_is (parser, CPP_NAME))
11086 {
11087 c_parser_error (parser, "expected %<tofrom%>");
11088 goto out_err;
11089 }
11090 p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
11091 if (strcmp (p, "tofrom") != 0)
11092 {
11093 c_parser_error (parser, "expected %<tofrom%>");
11094 goto out_err;
11095 }
11096 c_parser_consume_token (parser);
11097 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
11098 goto out_err;
11099 if (!c_parser_next_token_is (parser, CPP_NAME))
11100 {
11101 c_parser_error (parser, "expected %<scalar%>");
11102 goto out_err;
11103 }
11104 p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
11105 if (strcmp (p, "scalar") != 0)
11106 {
11107 c_parser_error (parser, "expected %<scalar%>");
11108 goto out_err;
11109 }
11110 c_parser_consume_token (parser);
11111 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11112 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULTMAP, "defaultmap");
11113 c = build_omp_clause (loc, OMP_CLAUSE_DEFAULTMAP);
11114 OMP_CLAUSE_CHAIN (c) = list;
11115 return c;
11116
11117 out_err:
11118 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11119 return list;
11120 }
11121
11122 /* OpenMP 4.5:
11123 use_device_ptr ( variable-list ) */
11124
11125 static tree
11126 c_parser_omp_clause_use_device_ptr (c_parser *parser, tree list)
11127 {
11128 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_USE_DEVICE_PTR,
11129 list);
11130 }
11131
11132 /* OpenMP 4.5:
11133 is_device_ptr ( variable-list ) */
11134
11135 static tree
11136 c_parser_omp_clause_is_device_ptr (c_parser *parser, tree list)
11137 {
11138 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_IS_DEVICE_PTR, list);
11139 }
11140
11141 /* OpenACC:
11142 num_workers ( expression ) */
11143
11144 static tree
11145 c_parser_omp_clause_num_workers (c_parser *parser, tree list)
11146 {
11147 location_t num_workers_loc = c_parser_peek_token (parser)->location;
11148 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11149 {
11150 location_t expr_loc = c_parser_peek_token (parser)->location;
11151 tree c, t = c_parser_expression (parser).value;
11152 mark_exp_read (t);
11153 t = c_fully_fold (t, false, NULL);
11154
11155 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11156
11157 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
11158 {
11159 c_parser_error (parser, "expected integer expression");
11160 return list;
11161 }
11162
11163 /* Attempt to statically determine when the number isn't positive. */
11164 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
11165 build_int_cst (TREE_TYPE (t), 0));
11166 protected_set_expr_location (c, expr_loc);
11167 if (c == boolean_true_node)
11168 {
11169 warning_at (expr_loc, 0,
11170 "%<num_workers%> value must be positive");
11171 t = integer_one_node;
11172 }
11173
11174 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_WORKERS, "num_workers");
11175
11176 c = build_omp_clause (num_workers_loc, OMP_CLAUSE_NUM_WORKERS);
11177 OMP_CLAUSE_NUM_WORKERS_EXPR (c) = t;
11178 OMP_CLAUSE_CHAIN (c) = list;
11179 list = c;
11180 }
11181
11182 return list;
11183 }
11184
11185 /* OpenACC:
11186
11187 gang [( gang-arg-list )]
11188 worker [( [num:] int-expr )]
11189 vector [( [length:] int-expr )]
11190
11191 where gang-arg is one of:
11192
11193 [num:] int-expr
11194 static: size-expr
11195
11196 and size-expr may be:
11197
11198 *
11199 int-expr
11200 */
11201
11202 static tree
11203 c_parser_oacc_shape_clause (c_parser *parser, omp_clause_code kind,
11204 const char *str, tree list)
11205 {
11206 const char *id = "num";
11207 tree ops[2] = { NULL_TREE, NULL_TREE }, c;
11208 location_t loc = c_parser_peek_token (parser)->location;
11209
11210 if (kind == OMP_CLAUSE_VECTOR)
11211 id = "length";
11212
11213 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
11214 {
11215 c_parser_consume_token (parser);
11216
11217 do
11218 {
11219 c_token *next = c_parser_peek_token (parser);
11220 int idx = 0;
11221
11222 /* Gang static argument. */
11223 if (kind == OMP_CLAUSE_GANG
11224 && c_parser_next_token_is_keyword (parser, RID_STATIC))
11225 {
11226 c_parser_consume_token (parser);
11227
11228 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
11229 goto cleanup_error;
11230
11231 idx = 1;
11232 if (ops[idx] != NULL_TREE)
11233 {
11234 c_parser_error (parser, "too many %<static%> arguments");
11235 goto cleanup_error;
11236 }
11237
11238 /* Check for the '*' argument. */
11239 if (c_parser_next_token_is (parser, CPP_MULT))
11240 {
11241 c_parser_consume_token (parser);
11242 ops[idx] = integer_minus_one_node;
11243
11244 if (c_parser_next_token_is (parser, CPP_COMMA))
11245 {
11246 c_parser_consume_token (parser);
11247 continue;
11248 }
11249 else
11250 break;
11251 }
11252 }
11253 /* Worker num: argument and vector length: arguments. */
11254 else if (c_parser_next_token_is (parser, CPP_NAME)
11255 && strcmp (id, IDENTIFIER_POINTER (next->value)) == 0
11256 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
11257 {
11258 c_parser_consume_token (parser); /* id */
11259 c_parser_consume_token (parser); /* ':' */
11260 }
11261
11262 /* Now collect the actual argument. */
11263 if (ops[idx] != NULL_TREE)
11264 {
11265 c_parser_error (parser, "unexpected argument");
11266 goto cleanup_error;
11267 }
11268
11269 location_t expr_loc = c_parser_peek_token (parser)->location;
11270 tree expr = c_parser_expr_no_commas (parser, NULL).value;
11271 if (expr == error_mark_node)
11272 goto cleanup_error;
11273
11274 mark_exp_read (expr);
11275 expr = c_fully_fold (expr, false, NULL);
11276
11277 /* Attempt to statically determine when the number isn't a
11278 positive integer. */
11279
11280 if (!INTEGRAL_TYPE_P (TREE_TYPE (expr)))
11281 {
11282 c_parser_error (parser, "expected integer expression");
11283 return list;
11284 }
11285
11286 tree c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, expr,
11287 build_int_cst (TREE_TYPE (expr), 0));
11288 if (c == boolean_true_node)
11289 {
11290 warning_at (loc, 0,
11291 "%<%s%> value must be positive", str);
11292 expr = integer_one_node;
11293 }
11294
11295 ops[idx] = expr;
11296
11297 if (kind == OMP_CLAUSE_GANG
11298 && c_parser_next_token_is (parser, CPP_COMMA))
11299 {
11300 c_parser_consume_token (parser);
11301 continue;
11302 }
11303 break;
11304 }
11305 while (1);
11306
11307 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
11308 goto cleanup_error;
11309 }
11310
11311 check_no_duplicate_clause (list, kind, str);
11312
11313 c = build_omp_clause (loc, kind);
11314
11315 if (ops[1])
11316 OMP_CLAUSE_OPERAND (c, 1) = ops[1];
11317
11318 OMP_CLAUSE_OPERAND (c, 0) = ops[0];
11319 OMP_CLAUSE_CHAIN (c) = list;
11320
11321 return c;
11322
11323 cleanup_error:
11324 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 0);
11325 return list;
11326 }
11327
11328 /* OpenACC:
11329 auto
11330 independent
11331 nohost
11332 seq */
11333
11334 static tree
11335 c_parser_oacc_simple_clause (c_parser *parser, enum omp_clause_code code,
11336 tree list)
11337 {
11338 check_no_duplicate_clause (list, code, omp_clause_code_name[code]);
11339
11340 tree c = build_omp_clause (c_parser_peek_token (parser)->location, code);
11341 OMP_CLAUSE_CHAIN (c) = list;
11342
11343 return c;
11344 }
11345
11346 /* OpenACC:
11347 async [( int-expr )] */
11348
11349 static tree
11350 c_parser_oacc_clause_async (c_parser *parser, tree list)
11351 {
11352 tree c, t;
11353 location_t loc = c_parser_peek_token (parser)->location;
11354
11355 t = build_int_cst (integer_type_node, GOMP_ASYNC_NOVAL);
11356
11357 if (c_parser_peek_token (parser)->type == CPP_OPEN_PAREN)
11358 {
11359 c_parser_consume_token (parser);
11360
11361 t = c_parser_expression (parser).value;
11362 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
11363 c_parser_error (parser, "expected integer expression");
11364 else if (t == error_mark_node
11365 || !c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
11366 return list;
11367 }
11368 else
11369 t = c_fully_fold (t, false, NULL);
11370
11371 check_no_duplicate_clause (list, OMP_CLAUSE_ASYNC, "async");
11372
11373 c = build_omp_clause (loc, OMP_CLAUSE_ASYNC);
11374 OMP_CLAUSE_ASYNC_EXPR (c) = t;
11375 OMP_CLAUSE_CHAIN (c) = list;
11376 list = c;
11377
11378 return list;
11379 }
11380
11381 /* OpenACC:
11382 wait ( int-expr-list ) */
11383
11384 static tree
11385 c_parser_oacc_clause_wait (c_parser *parser, tree list)
11386 {
11387 location_t clause_loc = c_parser_peek_token (parser)->location;
11388
11389 if (c_parser_peek_token (parser)->type == CPP_OPEN_PAREN)
11390 list = c_parser_oacc_wait_list (parser, clause_loc, list);
11391
11392 return list;
11393 }
11394
11395 /* OpenMP 2.5:
11396 ordered
11397
11398 OpenMP 4.5:
11399 ordered ( constant-expression ) */
11400
11401 static tree
11402 c_parser_omp_clause_ordered (c_parser *parser, tree list)
11403 {
11404 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED, "ordered");
11405
11406 tree c, num = NULL_TREE;
11407 HOST_WIDE_INT n;
11408 location_t loc = c_parser_peek_token (parser)->location;
11409 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
11410 {
11411 c_parser_consume_token (parser);
11412 num = c_parser_expr_no_commas (parser, NULL).value;
11413 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11414 }
11415 if (num == error_mark_node)
11416 return list;
11417 if (num)
11418 {
11419 mark_exp_read (num);
11420 num = c_fully_fold (num, false, NULL);
11421 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
11422 || !tree_fits_shwi_p (num)
11423 || (n = tree_to_shwi (num)) <= 0
11424 || (int) n != n)
11425 {
11426 error_at (loc, "ordered argument needs positive "
11427 "constant integer expression");
11428 return list;
11429 }
11430 }
11431 c = build_omp_clause (loc, OMP_CLAUSE_ORDERED);
11432 OMP_CLAUSE_ORDERED_EXPR (c) = num;
11433 OMP_CLAUSE_CHAIN (c) = list;
11434 return c;
11435 }
11436
11437 /* OpenMP 2.5:
11438 private ( variable-list ) */
11439
11440 static tree
11441 c_parser_omp_clause_private (c_parser *parser, tree list)
11442 {
11443 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_PRIVATE, list);
11444 }
11445
11446 /* OpenMP 2.5:
11447 reduction ( reduction-operator : variable-list )
11448
11449 reduction-operator:
11450 One of: + * - & ^ | && ||
11451
11452 OpenMP 3.1:
11453
11454 reduction-operator:
11455 One of: + * - & ^ | && || max min
11456
11457 OpenMP 4.0:
11458
11459 reduction-operator:
11460 One of: + * - & ^ | && ||
11461 identifier */
11462
11463 static tree
11464 c_parser_omp_clause_reduction (c_parser *parser, tree list)
11465 {
11466 location_t clause_loc = c_parser_peek_token (parser)->location;
11467 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11468 {
11469 enum tree_code code = ERROR_MARK;
11470 tree reduc_id = NULL_TREE;
11471
11472 switch (c_parser_peek_token (parser)->type)
11473 {
11474 case CPP_PLUS:
11475 code = PLUS_EXPR;
11476 break;
11477 case CPP_MULT:
11478 code = MULT_EXPR;
11479 break;
11480 case CPP_MINUS:
11481 code = MINUS_EXPR;
11482 break;
11483 case CPP_AND:
11484 code = BIT_AND_EXPR;
11485 break;
11486 case CPP_XOR:
11487 code = BIT_XOR_EXPR;
11488 break;
11489 case CPP_OR:
11490 code = BIT_IOR_EXPR;
11491 break;
11492 case CPP_AND_AND:
11493 code = TRUTH_ANDIF_EXPR;
11494 break;
11495 case CPP_OR_OR:
11496 code = TRUTH_ORIF_EXPR;
11497 break;
11498 case CPP_NAME:
11499 {
11500 const char *p
11501 = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
11502 if (strcmp (p, "min") == 0)
11503 {
11504 code = MIN_EXPR;
11505 break;
11506 }
11507 if (strcmp (p, "max") == 0)
11508 {
11509 code = MAX_EXPR;
11510 break;
11511 }
11512 reduc_id = c_parser_peek_token (parser)->value;
11513 break;
11514 }
11515 default:
11516 c_parser_error (parser,
11517 "expected %<+%>, %<*%>, %<-%>, %<&%>, "
11518 "%<^%>, %<|%>, %<&&%>, %<||%>, %<min%> or %<max%>");
11519 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 0);
11520 return list;
11521 }
11522 c_parser_consume_token (parser);
11523 reduc_id = c_omp_reduction_id (code, reduc_id);
11524 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
11525 {
11526 tree nl, c;
11527
11528 nl = c_parser_omp_variable_list (parser, clause_loc,
11529 OMP_CLAUSE_REDUCTION, list);
11530 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
11531 {
11532 tree d = OMP_CLAUSE_DECL (c), type;
11533 if (TREE_CODE (d) != TREE_LIST)
11534 type = TREE_TYPE (d);
11535 else
11536 {
11537 int cnt = 0;
11538 tree t;
11539 for (t = d; TREE_CODE (t) == TREE_LIST; t = TREE_CHAIN (t))
11540 cnt++;
11541 type = TREE_TYPE (t);
11542 while (cnt > 0)
11543 {
11544 if (TREE_CODE (type) != POINTER_TYPE
11545 && TREE_CODE (type) != ARRAY_TYPE)
11546 break;
11547 type = TREE_TYPE (type);
11548 cnt--;
11549 }
11550 }
11551 while (TREE_CODE (type) == ARRAY_TYPE)
11552 type = TREE_TYPE (type);
11553 OMP_CLAUSE_REDUCTION_CODE (c) = code;
11554 if (code == ERROR_MARK
11555 || !(INTEGRAL_TYPE_P (type)
11556 || TREE_CODE (type) == REAL_TYPE
11557 || TREE_CODE (type) == COMPLEX_TYPE))
11558 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c)
11559 = c_omp_reduction_lookup (reduc_id,
11560 TYPE_MAIN_VARIANT (type));
11561 }
11562
11563 list = nl;
11564 }
11565 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11566 }
11567 return list;
11568 }
11569
11570 /* OpenMP 2.5:
11571 schedule ( schedule-kind )
11572 schedule ( schedule-kind , expression )
11573
11574 schedule-kind:
11575 static | dynamic | guided | runtime | auto
11576
11577 OpenMP 4.5:
11578 schedule ( schedule-modifier : schedule-kind )
11579 schedule ( schedule-modifier [ , schedule-modifier ] : schedule-kind , expression )
11580
11581 schedule-modifier:
11582 simd
11583 monotonic
11584 nonmonotonic */
11585
11586 static tree
11587 c_parser_omp_clause_schedule (c_parser *parser, tree list)
11588 {
11589 tree c, t;
11590 location_t loc = c_parser_peek_token (parser)->location;
11591 int modifiers = 0, nmodifiers = 0;
11592
11593 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11594 return list;
11595
11596 c = build_omp_clause (loc, OMP_CLAUSE_SCHEDULE);
11597
11598 while (c_parser_next_token_is (parser, CPP_NAME))
11599 {
11600 tree kind = c_parser_peek_token (parser)->value;
11601 const char *p = IDENTIFIER_POINTER (kind);
11602 if (strcmp ("simd", p) == 0)
11603 OMP_CLAUSE_SCHEDULE_SIMD (c) = 1;
11604 else if (strcmp ("monotonic", p) == 0)
11605 modifiers |= OMP_CLAUSE_SCHEDULE_MONOTONIC;
11606 else if (strcmp ("nonmonotonic", p) == 0)
11607 modifiers |= OMP_CLAUSE_SCHEDULE_NONMONOTONIC;
11608 else
11609 break;
11610 c_parser_consume_token (parser);
11611 if (nmodifiers++ == 0
11612 && c_parser_next_token_is (parser, CPP_COMMA))
11613 c_parser_consume_token (parser);
11614 else
11615 {
11616 c_parser_require (parser, CPP_COLON, "expected %<:%>");
11617 break;
11618 }
11619 }
11620
11621 if ((modifiers & (OMP_CLAUSE_SCHEDULE_MONOTONIC
11622 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
11623 == (OMP_CLAUSE_SCHEDULE_MONOTONIC
11624 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
11625 {
11626 error_at (loc, "both %<monotonic%> and %<nonmonotonic%> modifiers "
11627 "specified");
11628 modifiers = 0;
11629 }
11630
11631 if (c_parser_next_token_is (parser, CPP_NAME))
11632 {
11633 tree kind = c_parser_peek_token (parser)->value;
11634 const char *p = IDENTIFIER_POINTER (kind);
11635
11636 switch (p[0])
11637 {
11638 case 'd':
11639 if (strcmp ("dynamic", p) != 0)
11640 goto invalid_kind;
11641 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
11642 break;
11643
11644 case 'g':
11645 if (strcmp ("guided", p) != 0)
11646 goto invalid_kind;
11647 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
11648 break;
11649
11650 case 'r':
11651 if (strcmp ("runtime", p) != 0)
11652 goto invalid_kind;
11653 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
11654 break;
11655
11656 default:
11657 goto invalid_kind;
11658 }
11659 }
11660 else if (c_parser_next_token_is_keyword (parser, RID_STATIC))
11661 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
11662 else if (c_parser_next_token_is_keyword (parser, RID_AUTO))
11663 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
11664 else
11665 goto invalid_kind;
11666
11667 c_parser_consume_token (parser);
11668 if (c_parser_next_token_is (parser, CPP_COMMA))
11669 {
11670 location_t here;
11671 c_parser_consume_token (parser);
11672
11673 here = c_parser_peek_token (parser)->location;
11674 t = c_parser_expr_no_commas (parser, NULL).value;
11675 mark_exp_read (t);
11676 t = c_fully_fold (t, false, NULL);
11677
11678 if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
11679 error_at (here, "schedule %<runtime%> does not take "
11680 "a %<chunk_size%> parameter");
11681 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
11682 error_at (here,
11683 "schedule %<auto%> does not take "
11684 "a %<chunk_size%> parameter");
11685 else if (TREE_CODE (TREE_TYPE (t)) == INTEGER_TYPE)
11686 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
11687 else
11688 c_parser_error (parser, "expected integer expression");
11689
11690 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11691 }
11692 else
11693 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
11694 "expected %<,%> or %<)%>");
11695
11696 OMP_CLAUSE_SCHEDULE_KIND (c)
11697 = (enum omp_clause_schedule_kind)
11698 (OMP_CLAUSE_SCHEDULE_KIND (c) | modifiers);
11699
11700 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule");
11701 OMP_CLAUSE_CHAIN (c) = list;
11702 return c;
11703
11704 invalid_kind:
11705 c_parser_error (parser, "invalid schedule kind");
11706 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 0);
11707 return list;
11708 }
11709
11710 /* OpenMP 2.5:
11711 shared ( variable-list ) */
11712
11713 static tree
11714 c_parser_omp_clause_shared (c_parser *parser, tree list)
11715 {
11716 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_SHARED, list);
11717 }
11718
11719 /* OpenMP 3.0:
11720 untied */
11721
11722 static tree
11723 c_parser_omp_clause_untied (c_parser *parser ATTRIBUTE_UNUSED, tree list)
11724 {
11725 tree c;
11726
11727 /* FIXME: Should we allow duplicates? */
11728 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied");
11729
11730 c = build_omp_clause (c_parser_peek_token (parser)->location,
11731 OMP_CLAUSE_UNTIED);
11732 OMP_CLAUSE_CHAIN (c) = list;
11733
11734 return c;
11735 }
11736
11737 /* OpenACC:
11738 vector_length ( expression ) */
11739
11740 static tree
11741 c_parser_omp_clause_vector_length (c_parser *parser, tree list)
11742 {
11743 location_t vector_length_loc = c_parser_peek_token (parser)->location;
11744 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11745 {
11746 location_t expr_loc = c_parser_peek_token (parser)->location;
11747 tree c, t = c_parser_expression (parser).value;
11748 mark_exp_read (t);
11749 t = c_fully_fold (t, false, NULL);
11750
11751 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11752
11753 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
11754 {
11755 c_parser_error (parser, "expected integer expression");
11756 return list;
11757 }
11758
11759 /* Attempt to statically determine when the number isn't positive. */
11760 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
11761 build_int_cst (TREE_TYPE (t), 0));
11762 protected_set_expr_location (c, expr_loc);
11763 if (c == boolean_true_node)
11764 {
11765 warning_at (expr_loc, 0,
11766 "%<vector_length%> value must be positive");
11767 t = integer_one_node;
11768 }
11769
11770 check_no_duplicate_clause (list, OMP_CLAUSE_VECTOR_LENGTH, "vector_length");
11771
11772 c = build_omp_clause (vector_length_loc, OMP_CLAUSE_VECTOR_LENGTH);
11773 OMP_CLAUSE_VECTOR_LENGTH_EXPR (c) = t;
11774 OMP_CLAUSE_CHAIN (c) = list;
11775 list = c;
11776 }
11777
11778 return list;
11779 }
11780
11781 /* OpenMP 4.0:
11782 inbranch
11783 notinbranch */
11784
11785 static tree
11786 c_parser_omp_clause_branch (c_parser *parser ATTRIBUTE_UNUSED,
11787 enum omp_clause_code code, tree list)
11788 {
11789 check_no_duplicate_clause (list, code, omp_clause_code_name[code]);
11790
11791 tree c = build_omp_clause (c_parser_peek_token (parser)->location, code);
11792 OMP_CLAUSE_CHAIN (c) = list;
11793
11794 return c;
11795 }
11796
11797 /* OpenMP 4.0:
11798 parallel
11799 for
11800 sections
11801 taskgroup */
11802
11803 static tree
11804 c_parser_omp_clause_cancelkind (c_parser *parser ATTRIBUTE_UNUSED,
11805 enum omp_clause_code code, tree list)
11806 {
11807 tree c = build_omp_clause (c_parser_peek_token (parser)->location, code);
11808 OMP_CLAUSE_CHAIN (c) = list;
11809
11810 return c;
11811 }
11812
11813 /* OpenMP 4.5:
11814 nogroup */
11815
11816 static tree
11817 c_parser_omp_clause_nogroup (c_parser *parser ATTRIBUTE_UNUSED, tree list)
11818 {
11819 check_no_duplicate_clause (list, OMP_CLAUSE_NOGROUP, "nogroup");
11820 tree c = build_omp_clause (c_parser_peek_token (parser)->location,
11821 OMP_CLAUSE_NOGROUP);
11822 OMP_CLAUSE_CHAIN (c) = list;
11823 return c;
11824 }
11825
11826 /* OpenMP 4.5:
11827 simd
11828 threads */
11829
11830 static tree
11831 c_parser_omp_clause_orderedkind (c_parser *parser ATTRIBUTE_UNUSED,
11832 enum omp_clause_code code, tree list)
11833 {
11834 check_no_duplicate_clause (list, code, omp_clause_code_name[code]);
11835 tree c = build_omp_clause (c_parser_peek_token (parser)->location, code);
11836 OMP_CLAUSE_CHAIN (c) = list;
11837 return c;
11838 }
11839
11840 /* OpenMP 4.0:
11841 num_teams ( expression ) */
11842
11843 static tree
11844 c_parser_omp_clause_num_teams (c_parser *parser, tree list)
11845 {
11846 location_t num_teams_loc = c_parser_peek_token (parser)->location;
11847 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11848 {
11849 location_t expr_loc = c_parser_peek_token (parser)->location;
11850 tree c, t = c_parser_expression (parser).value;
11851 mark_exp_read (t);
11852 t = c_fully_fold (t, false, NULL);
11853
11854 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11855
11856 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
11857 {
11858 c_parser_error (parser, "expected integer expression");
11859 return list;
11860 }
11861
11862 /* Attempt to statically determine when the number isn't positive. */
11863 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
11864 build_int_cst (TREE_TYPE (t), 0));
11865 protected_set_expr_location (c, expr_loc);
11866 if (c == boolean_true_node)
11867 {
11868 warning_at (expr_loc, 0, "%<num_teams%> value must be positive");
11869 t = integer_one_node;
11870 }
11871
11872 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TEAMS, "num_teams");
11873
11874 c = build_omp_clause (num_teams_loc, OMP_CLAUSE_NUM_TEAMS);
11875 OMP_CLAUSE_NUM_TEAMS_EXPR (c) = t;
11876 OMP_CLAUSE_CHAIN (c) = list;
11877 list = c;
11878 }
11879
11880 return list;
11881 }
11882
11883 /* OpenMP 4.0:
11884 thread_limit ( expression ) */
11885
11886 static tree
11887 c_parser_omp_clause_thread_limit (c_parser *parser, tree list)
11888 {
11889 location_t num_thread_limit_loc = c_parser_peek_token (parser)->location;
11890 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11891 {
11892 location_t expr_loc = c_parser_peek_token (parser)->location;
11893 tree c, t = c_parser_expression (parser).value;
11894 mark_exp_read (t);
11895 t = c_fully_fold (t, false, NULL);
11896
11897 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11898
11899 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
11900 {
11901 c_parser_error (parser, "expected integer expression");
11902 return list;
11903 }
11904
11905 /* Attempt to statically determine when the number isn't positive. */
11906 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
11907 build_int_cst (TREE_TYPE (t), 0));
11908 protected_set_expr_location (c, expr_loc);
11909 if (c == boolean_true_node)
11910 {
11911 warning_at (expr_loc, 0, "%<thread_limit%> value must be positive");
11912 t = integer_one_node;
11913 }
11914
11915 check_no_duplicate_clause (list, OMP_CLAUSE_THREAD_LIMIT,
11916 "thread_limit");
11917
11918 c = build_omp_clause (num_thread_limit_loc, OMP_CLAUSE_THREAD_LIMIT);
11919 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
11920 OMP_CLAUSE_CHAIN (c) = list;
11921 list = c;
11922 }
11923
11924 return list;
11925 }
11926
11927 /* OpenMP 4.0:
11928 aligned ( variable-list )
11929 aligned ( variable-list : constant-expression ) */
11930
11931 static tree
11932 c_parser_omp_clause_aligned (c_parser *parser, tree list)
11933 {
11934 location_t clause_loc = c_parser_peek_token (parser)->location;
11935 tree nl, c;
11936
11937 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11938 return list;
11939
11940 nl = c_parser_omp_variable_list (parser, clause_loc,
11941 OMP_CLAUSE_ALIGNED, list);
11942
11943 if (c_parser_next_token_is (parser, CPP_COLON))
11944 {
11945 c_parser_consume_token (parser);
11946 tree alignment = c_parser_expr_no_commas (parser, NULL).value;
11947 mark_exp_read (alignment);
11948 alignment = c_fully_fold (alignment, false, NULL);
11949 if (TREE_CODE (alignment) != INTEGER_CST
11950 || !INTEGRAL_TYPE_P (TREE_TYPE (alignment))
11951 || tree_int_cst_sgn (alignment) != 1)
11952 {
11953 error_at (clause_loc, "%<aligned%> clause alignment expression must "
11954 "be positive constant integer expression");
11955 alignment = NULL_TREE;
11956 }
11957
11958 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
11959 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = alignment;
11960 }
11961
11962 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11963 return nl;
11964 }
11965
11966 /* OpenMP 4.0:
11967 linear ( variable-list )
11968 linear ( variable-list : expression )
11969
11970 OpenMP 4.5:
11971 linear ( modifier ( variable-list ) )
11972 linear ( modifier ( variable-list ) : expression ) */
11973
11974 static tree
11975 c_parser_omp_clause_linear (c_parser *parser, tree list, bool is_cilk_simd_fn)
11976 {
11977 location_t clause_loc = c_parser_peek_token (parser)->location;
11978 tree nl, c, step;
11979 enum omp_clause_linear_kind kind = OMP_CLAUSE_LINEAR_DEFAULT;
11980
11981 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11982 return list;
11983
11984 if (!is_cilk_simd_fn
11985 && c_parser_next_token_is (parser, CPP_NAME))
11986 {
11987 c_token *tok = c_parser_peek_token (parser);
11988 const char *p = IDENTIFIER_POINTER (tok->value);
11989 if (strcmp ("val", p) == 0)
11990 kind = OMP_CLAUSE_LINEAR_VAL;
11991 if (c_parser_peek_2nd_token (parser)->type != CPP_OPEN_PAREN)
11992 kind = OMP_CLAUSE_LINEAR_DEFAULT;
11993 if (kind != OMP_CLAUSE_LINEAR_DEFAULT)
11994 {
11995 c_parser_consume_token (parser);
11996 c_parser_consume_token (parser);
11997 }
11998 }
11999
12000 nl = c_parser_omp_variable_list (parser, clause_loc,
12001 OMP_CLAUSE_LINEAR, list);
12002
12003 if (kind != OMP_CLAUSE_LINEAR_DEFAULT)
12004 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
12005
12006 if (c_parser_next_token_is (parser, CPP_COLON))
12007 {
12008 c_parser_consume_token (parser);
12009 step = c_parser_expression (parser).value;
12010 mark_exp_read (step);
12011 step = c_fully_fold (step, false, NULL);
12012 if (is_cilk_simd_fn && TREE_CODE (step) == PARM_DECL)
12013 {
12014 sorry ("using parameters for %<linear%> step is not supported yet");
12015 step = integer_one_node;
12016 }
12017 if (!INTEGRAL_TYPE_P (TREE_TYPE (step)))
12018 {
12019 error_at (clause_loc, "%<linear%> clause step expression must "
12020 "be integral");
12021 step = integer_one_node;
12022 }
12023
12024 }
12025 else
12026 step = integer_one_node;
12027
12028 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
12029 {
12030 OMP_CLAUSE_LINEAR_STEP (c) = step;
12031 OMP_CLAUSE_LINEAR_KIND (c) = kind;
12032 }
12033
12034 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
12035 return nl;
12036 }
12037
12038 /* OpenMP 4.0:
12039 safelen ( constant-expression ) */
12040
12041 static tree
12042 c_parser_omp_clause_safelen (c_parser *parser, tree list)
12043 {
12044 location_t clause_loc = c_parser_peek_token (parser)->location;
12045 tree c, t;
12046
12047 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
12048 return list;
12049
12050 t = c_parser_expr_no_commas (parser, NULL).value;
12051 mark_exp_read (t);
12052 t = c_fully_fold (t, false, NULL);
12053 if (TREE_CODE (t) != INTEGER_CST
12054 || !INTEGRAL_TYPE_P (TREE_TYPE (t))
12055 || tree_int_cst_sgn (t) != 1)
12056 {
12057 error_at (clause_loc, "%<safelen%> clause expression must "
12058 "be positive constant integer expression");
12059 t = NULL_TREE;
12060 }
12061
12062 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
12063 if (t == NULL_TREE || t == error_mark_node)
12064 return list;
12065
12066 check_no_duplicate_clause (list, OMP_CLAUSE_SAFELEN, "safelen");
12067
12068 c = build_omp_clause (clause_loc, OMP_CLAUSE_SAFELEN);
12069 OMP_CLAUSE_SAFELEN_EXPR (c) = t;
12070 OMP_CLAUSE_CHAIN (c) = list;
12071 return c;
12072 }
12073
12074 /* OpenMP 4.0:
12075 simdlen ( constant-expression ) */
12076
12077 static tree
12078 c_parser_omp_clause_simdlen (c_parser *parser, tree list)
12079 {
12080 location_t clause_loc = c_parser_peek_token (parser)->location;
12081 tree c, t;
12082
12083 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
12084 return list;
12085
12086 t = c_parser_expr_no_commas (parser, NULL).value;
12087 mark_exp_read (t);
12088 t = c_fully_fold (t, false, NULL);
12089 if (TREE_CODE (t) != INTEGER_CST
12090 || !INTEGRAL_TYPE_P (TREE_TYPE (t))
12091 || tree_int_cst_sgn (t) != 1)
12092 {
12093 error_at (clause_loc, "%<simdlen%> clause expression must "
12094 "be positive constant integer expression");
12095 t = NULL_TREE;
12096 }
12097
12098 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
12099 if (t == NULL_TREE || t == error_mark_node)
12100 return list;
12101
12102 check_no_duplicate_clause (list, OMP_CLAUSE_SIMDLEN, "simdlen");
12103
12104 c = build_omp_clause (clause_loc, OMP_CLAUSE_SIMDLEN);
12105 OMP_CLAUSE_SIMDLEN_EXPR (c) = t;
12106 OMP_CLAUSE_CHAIN (c) = list;
12107 return c;
12108 }
12109
12110 /* OpenMP 4.5:
12111 vec:
12112 identifier [+/- integer]
12113 vec , identifier [+/- integer]
12114 */
12115
12116 static tree
12117 c_parser_omp_clause_depend_sink (c_parser *parser, location_t clause_loc,
12118 tree list)
12119 {
12120 tree vec = NULL;
12121 if (c_parser_next_token_is_not (parser, CPP_NAME)
12122 || c_parser_peek_token (parser)->id_kind != C_ID_ID)
12123 {
12124 c_parser_error (parser, "expected identifier");
12125 return list;
12126 }
12127
12128 while (c_parser_next_token_is (parser, CPP_NAME)
12129 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
12130 {
12131 tree t = lookup_name (c_parser_peek_token (parser)->value);
12132 tree addend = NULL;
12133
12134 if (t == NULL_TREE)
12135 {
12136 undeclared_variable (c_parser_peek_token (parser)->location,
12137 c_parser_peek_token (parser)->value);
12138 t = error_mark_node;
12139 }
12140
12141 c_parser_consume_token (parser);
12142
12143 bool neg = false;
12144 if (c_parser_next_token_is (parser, CPP_MINUS))
12145 neg = true;
12146 else if (!c_parser_next_token_is (parser, CPP_PLUS))
12147 {
12148 addend = integer_zero_node;
12149 neg = false;
12150 goto add_to_vector;
12151 }
12152 c_parser_consume_token (parser);
12153
12154 if (c_parser_next_token_is_not (parser, CPP_NUMBER))
12155 {
12156 c_parser_error (parser, "expected integer");
12157 return list;
12158 }
12159
12160 addend = c_parser_peek_token (parser)->value;
12161 if (TREE_CODE (addend) != INTEGER_CST)
12162 {
12163 c_parser_error (parser, "expected integer");
12164 return list;
12165 }
12166 c_parser_consume_token (parser);
12167
12168 add_to_vector:
12169 if (t != error_mark_node)
12170 {
12171 vec = tree_cons (addend, t, vec);
12172 if (neg)
12173 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (vec) = 1;
12174 }
12175
12176 if (c_parser_next_token_is_not (parser, CPP_COMMA))
12177 break;
12178
12179 c_parser_consume_token (parser);
12180 }
12181
12182 if (vec == NULL_TREE)
12183 return list;
12184
12185 tree u = build_omp_clause (clause_loc, OMP_CLAUSE_DEPEND);
12186 OMP_CLAUSE_DEPEND_KIND (u) = OMP_CLAUSE_DEPEND_SINK;
12187 OMP_CLAUSE_DECL (u) = nreverse (vec);
12188 OMP_CLAUSE_CHAIN (u) = list;
12189 return u;
12190 }
12191
12192 /* OpenMP 4.0:
12193 depend ( depend-kind: variable-list )
12194
12195 depend-kind:
12196 in | out | inout
12197
12198 OpenMP 4.5:
12199 depend ( source )
12200
12201 depend ( sink : vec ) */
12202
12203 static tree
12204 c_parser_omp_clause_depend (c_parser *parser, tree list)
12205 {
12206 location_t clause_loc = c_parser_peek_token (parser)->location;
12207 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_INOUT;
12208 tree nl, c;
12209
12210 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
12211 return list;
12212
12213 if (c_parser_next_token_is (parser, CPP_NAME))
12214 {
12215 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
12216 if (strcmp ("in", p) == 0)
12217 kind = OMP_CLAUSE_DEPEND_IN;
12218 else if (strcmp ("inout", p) == 0)
12219 kind = OMP_CLAUSE_DEPEND_INOUT;
12220 else if (strcmp ("out", p) == 0)
12221 kind = OMP_CLAUSE_DEPEND_OUT;
12222 else if (strcmp ("source", p) == 0)
12223 kind = OMP_CLAUSE_DEPEND_SOURCE;
12224 else if (strcmp ("sink", p) == 0)
12225 kind = OMP_CLAUSE_DEPEND_SINK;
12226 else
12227 goto invalid_kind;
12228 }
12229 else
12230 goto invalid_kind;
12231
12232 c_parser_consume_token (parser);
12233
12234 if (kind == OMP_CLAUSE_DEPEND_SOURCE)
12235 {
12236 c = build_omp_clause (clause_loc, OMP_CLAUSE_DEPEND);
12237 OMP_CLAUSE_DEPEND_KIND (c) = kind;
12238 OMP_CLAUSE_DECL (c) = NULL_TREE;
12239 OMP_CLAUSE_CHAIN (c) = list;
12240 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
12241 return c;
12242 }
12243
12244 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
12245 goto resync_fail;
12246
12247 if (kind == OMP_CLAUSE_DEPEND_SINK)
12248 nl = c_parser_omp_clause_depend_sink (parser, clause_loc, list);
12249 else
12250 {
12251 nl = c_parser_omp_variable_list (parser, clause_loc,
12252 OMP_CLAUSE_DEPEND, list);
12253
12254 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
12255 OMP_CLAUSE_DEPEND_KIND (c) = kind;
12256 }
12257
12258 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
12259 return nl;
12260
12261 invalid_kind:
12262 c_parser_error (parser, "invalid depend kind");
12263 resync_fail:
12264 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
12265 return list;
12266 }
12267
12268 /* OpenMP 4.0:
12269 map ( map-kind: variable-list )
12270 map ( variable-list )
12271
12272 map-kind:
12273 alloc | to | from | tofrom
12274
12275 OpenMP 4.5:
12276 map-kind:
12277 alloc | to | from | tofrom | release | delete
12278
12279 map ( always [,] map-kind: variable-list ) */
12280
12281 static tree
12282 c_parser_omp_clause_map (c_parser *parser, tree list)
12283 {
12284 location_t clause_loc = c_parser_peek_token (parser)->location;
12285 enum gomp_map_kind kind = GOMP_MAP_TOFROM;
12286 int always = 0;
12287 enum c_id_kind always_id_kind = C_ID_NONE;
12288 location_t always_loc = UNKNOWN_LOCATION;
12289 tree always_id = NULL_TREE;
12290 tree nl, c;
12291
12292 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
12293 return list;
12294
12295 if (c_parser_next_token_is (parser, CPP_NAME))
12296 {
12297 c_token *tok = c_parser_peek_token (parser);
12298 const char *p = IDENTIFIER_POINTER (tok->value);
12299 always_id_kind = tok->id_kind;
12300 always_loc = tok->location;
12301 always_id = tok->value;
12302 if (strcmp ("always", p) == 0)
12303 {
12304 c_token *sectok = c_parser_peek_2nd_token (parser);
12305 if (sectok->type == CPP_COMMA)
12306 {
12307 c_parser_consume_token (parser);
12308 c_parser_consume_token (parser);
12309 always = 2;
12310 }
12311 else if (sectok->type == CPP_NAME)
12312 {
12313 p = IDENTIFIER_POINTER (sectok->value);
12314 if (strcmp ("alloc", p) == 0
12315 || strcmp ("to", p) == 0
12316 || strcmp ("from", p) == 0
12317 || strcmp ("tofrom", p) == 0
12318 || strcmp ("release", p) == 0
12319 || strcmp ("delete", p) == 0)
12320 {
12321 c_parser_consume_token (parser);
12322 always = 1;
12323 }
12324 }
12325 }
12326 }
12327
12328 if (c_parser_next_token_is (parser, CPP_NAME)
12329 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
12330 {
12331 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
12332 if (strcmp ("alloc", p) == 0)
12333 kind = GOMP_MAP_ALLOC;
12334 else if (strcmp ("to", p) == 0)
12335 kind = always ? GOMP_MAP_ALWAYS_TO : GOMP_MAP_TO;
12336 else if (strcmp ("from", p) == 0)
12337 kind = always ? GOMP_MAP_ALWAYS_FROM : GOMP_MAP_FROM;
12338 else if (strcmp ("tofrom", p) == 0)
12339 kind = always ? GOMP_MAP_ALWAYS_TOFROM : GOMP_MAP_TOFROM;
12340 else if (strcmp ("release", p) == 0)
12341 kind = GOMP_MAP_RELEASE;
12342 else if (strcmp ("delete", p) == 0)
12343 kind = GOMP_MAP_DELETE;
12344 else
12345 {
12346 c_parser_error (parser, "invalid map kind");
12347 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
12348 "expected %<)%>");
12349 return list;
12350 }
12351 c_parser_consume_token (parser);
12352 c_parser_consume_token (parser);
12353 }
12354 else if (always)
12355 {
12356 if (always_id_kind != C_ID_ID)
12357 {
12358 c_parser_error (parser, "expected identifier");
12359 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
12360 return list;
12361 }
12362
12363 tree t = lookup_name (always_id);
12364 if (t == NULL_TREE)
12365 {
12366 undeclared_variable (always_loc, always_id);
12367 t = error_mark_node;
12368 }
12369 if (t != error_mark_node)
12370 {
12371 tree u = build_omp_clause (clause_loc, OMP_CLAUSE_MAP);
12372 OMP_CLAUSE_DECL (u) = t;
12373 OMP_CLAUSE_CHAIN (u) = list;
12374 OMP_CLAUSE_SET_MAP_KIND (u, kind);
12375 list = u;
12376 }
12377 if (always == 1)
12378 {
12379 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
12380 return list;
12381 }
12382 }
12383
12384 nl = c_parser_omp_variable_list (parser, clause_loc, OMP_CLAUSE_MAP, list);
12385
12386 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
12387 OMP_CLAUSE_SET_MAP_KIND (c, kind);
12388
12389 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
12390 return nl;
12391 }
12392
12393 /* OpenMP 4.0:
12394 device ( expression ) */
12395
12396 static tree
12397 c_parser_omp_clause_device (c_parser *parser, tree list)
12398 {
12399 location_t clause_loc = c_parser_peek_token (parser)->location;
12400 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
12401 {
12402 tree c, t = c_parser_expr_no_commas (parser, NULL).value;
12403 mark_exp_read (t);
12404 t = c_fully_fold (t, false, NULL);
12405
12406 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
12407
12408 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
12409 {
12410 c_parser_error (parser, "expected integer expression");
12411 return list;
12412 }
12413
12414 check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE, "device");
12415
12416 c = build_omp_clause (clause_loc, OMP_CLAUSE_DEVICE);
12417 OMP_CLAUSE_DEVICE_ID (c) = t;
12418 OMP_CLAUSE_CHAIN (c) = list;
12419 list = c;
12420 }
12421
12422 return list;
12423 }
12424
12425 /* OpenMP 4.0:
12426 dist_schedule ( static )
12427 dist_schedule ( static , expression ) */
12428
12429 static tree
12430 c_parser_omp_clause_dist_schedule (c_parser *parser, tree list)
12431 {
12432 tree c, t = NULL_TREE;
12433 location_t loc = c_parser_peek_token (parser)->location;
12434
12435 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
12436 return list;
12437
12438 if (!c_parser_next_token_is_keyword (parser, RID_STATIC))
12439 {
12440 c_parser_error (parser, "invalid dist_schedule kind");
12441 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
12442 "expected %<)%>");
12443 return list;
12444 }
12445
12446 c_parser_consume_token (parser);
12447 if (c_parser_next_token_is (parser, CPP_COMMA))
12448 {
12449 c_parser_consume_token (parser);
12450
12451 t = c_parser_expr_no_commas (parser, NULL).value;
12452 mark_exp_read (t);
12453 t = c_fully_fold (t, false, NULL);
12454 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
12455 }
12456 else
12457 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
12458 "expected %<,%> or %<)%>");
12459
12460 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule");
12461 if (t == error_mark_node)
12462 return list;
12463
12464 c = build_omp_clause (loc, OMP_CLAUSE_DIST_SCHEDULE);
12465 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
12466 OMP_CLAUSE_CHAIN (c) = list;
12467 return c;
12468 }
12469
12470 /* OpenMP 4.0:
12471 proc_bind ( proc-bind-kind )
12472
12473 proc-bind-kind:
12474 master | close | spread */
12475
12476 static tree
12477 c_parser_omp_clause_proc_bind (c_parser *parser, tree list)
12478 {
12479 location_t clause_loc = c_parser_peek_token (parser)->location;
12480 enum omp_clause_proc_bind_kind kind;
12481 tree c;
12482
12483 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
12484 return list;
12485
12486 if (c_parser_next_token_is (parser, CPP_NAME))
12487 {
12488 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
12489 if (strcmp ("master", p) == 0)
12490 kind = OMP_CLAUSE_PROC_BIND_MASTER;
12491 else if (strcmp ("close", p) == 0)
12492 kind = OMP_CLAUSE_PROC_BIND_CLOSE;
12493 else if (strcmp ("spread", p) == 0)
12494 kind = OMP_CLAUSE_PROC_BIND_SPREAD;
12495 else
12496 goto invalid_kind;
12497 }
12498 else
12499 goto invalid_kind;
12500
12501 c_parser_consume_token (parser);
12502 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
12503 c = build_omp_clause (clause_loc, OMP_CLAUSE_PROC_BIND);
12504 OMP_CLAUSE_PROC_BIND_KIND (c) = kind;
12505 OMP_CLAUSE_CHAIN (c) = list;
12506 return c;
12507
12508 invalid_kind:
12509 c_parser_error (parser, "invalid proc_bind kind");
12510 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
12511 return list;
12512 }
12513
12514 /* OpenMP 4.0:
12515 to ( variable-list ) */
12516
12517 static tree
12518 c_parser_omp_clause_to (c_parser *parser, tree list)
12519 {
12520 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_TO, list);
12521 }
12522
12523 /* OpenMP 4.0:
12524 from ( variable-list ) */
12525
12526 static tree
12527 c_parser_omp_clause_from (c_parser *parser, tree list)
12528 {
12529 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_FROM, list);
12530 }
12531
12532 /* OpenMP 4.0:
12533 uniform ( variable-list ) */
12534
12535 static tree
12536 c_parser_omp_clause_uniform (c_parser *parser, tree list)
12537 {
12538 /* The clauses location. */
12539 location_t loc = c_parser_peek_token (parser)->location;
12540
12541 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
12542 {
12543 list = c_parser_omp_variable_list (parser, loc, OMP_CLAUSE_UNIFORM,
12544 list);
12545 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
12546 }
12547 return list;
12548 }
12549
12550 /* Parse all OpenACC clauses. The set clauses allowed by the directive
12551 is a bitmask in MASK. Return the list of clauses found. */
12552
12553 static tree
12554 c_parser_oacc_all_clauses (c_parser *parser, omp_clause_mask mask,
12555 const char *where, bool finish_p = true)
12556 {
12557 tree clauses = NULL;
12558 bool first = true;
12559
12560 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
12561 {
12562 location_t here;
12563 pragma_omp_clause c_kind;
12564 const char *c_name;
12565 tree prev = clauses;
12566
12567 if (!first && c_parser_next_token_is (parser, CPP_COMMA))
12568 c_parser_consume_token (parser);
12569
12570 here = c_parser_peek_token (parser)->location;
12571 c_kind = c_parser_omp_clause_name (parser);
12572
12573 switch (c_kind)
12574 {
12575 case PRAGMA_OACC_CLAUSE_ASYNC:
12576 clauses = c_parser_oacc_clause_async (parser, clauses);
12577 c_name = "async";
12578 break;
12579 case PRAGMA_OACC_CLAUSE_AUTO:
12580 clauses = c_parser_oacc_simple_clause (parser, OMP_CLAUSE_AUTO,
12581 clauses);
12582 c_name = "auto";
12583 break;
12584 case PRAGMA_OACC_CLAUSE_COLLAPSE:
12585 clauses = c_parser_omp_clause_collapse (parser, clauses);
12586 c_name = "collapse";
12587 break;
12588 case PRAGMA_OACC_CLAUSE_COPY:
12589 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
12590 c_name = "copy";
12591 break;
12592 case PRAGMA_OACC_CLAUSE_COPYIN:
12593 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
12594 c_name = "copyin";
12595 break;
12596 case PRAGMA_OACC_CLAUSE_COPYOUT:
12597 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
12598 c_name = "copyout";
12599 break;
12600 case PRAGMA_OACC_CLAUSE_CREATE:
12601 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
12602 c_name = "create";
12603 break;
12604 case PRAGMA_OACC_CLAUSE_DELETE:
12605 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
12606 c_name = "delete";
12607 break;
12608 case PRAGMA_OACC_CLAUSE_DEVICE:
12609 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
12610 c_name = "device";
12611 break;
12612 case PRAGMA_OACC_CLAUSE_DEVICEPTR:
12613 clauses = c_parser_oacc_data_clause_deviceptr (parser, clauses);
12614 c_name = "deviceptr";
12615 break;
12616 case PRAGMA_OACC_CLAUSE_FIRSTPRIVATE:
12617 clauses = c_parser_omp_clause_firstprivate (parser, clauses);
12618 c_name = "firstprivate";
12619 break;
12620 case PRAGMA_OACC_CLAUSE_GANG:
12621 c_name = "gang";
12622 clauses = c_parser_oacc_shape_clause (parser, OMP_CLAUSE_GANG,
12623 c_name, clauses);
12624 break;
12625 case PRAGMA_OACC_CLAUSE_HOST:
12626 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
12627 c_name = "host";
12628 break;
12629 case PRAGMA_OACC_CLAUSE_IF:
12630 clauses = c_parser_omp_clause_if (parser, clauses, false);
12631 c_name = "if";
12632 break;
12633 case PRAGMA_OACC_CLAUSE_NUM_GANGS:
12634 clauses = c_parser_omp_clause_num_gangs (parser, clauses);
12635 c_name = "num_gangs";
12636 break;
12637 case PRAGMA_OACC_CLAUSE_NUM_WORKERS:
12638 clauses = c_parser_omp_clause_num_workers (parser, clauses);
12639 c_name = "num_workers";
12640 break;
12641 case PRAGMA_OACC_CLAUSE_PRESENT:
12642 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
12643 c_name = "present";
12644 break;
12645 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY:
12646 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
12647 c_name = "present_or_copy";
12648 break;
12649 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN:
12650 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
12651 c_name = "present_or_copyin";
12652 break;
12653 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT:
12654 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
12655 c_name = "present_or_copyout";
12656 break;
12657 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE:
12658 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
12659 c_name = "present_or_create";
12660 break;
12661 case PRAGMA_OACC_CLAUSE_PRIVATE:
12662 clauses = c_parser_omp_clause_private (parser, clauses);
12663 c_name = "private";
12664 break;
12665 case PRAGMA_OACC_CLAUSE_REDUCTION:
12666 clauses = c_parser_omp_clause_reduction (parser, clauses);
12667 c_name = "reduction";
12668 break;
12669 case PRAGMA_OACC_CLAUSE_SELF:
12670 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
12671 c_name = "self";
12672 break;
12673 case PRAGMA_OACC_CLAUSE_SEQ:
12674 clauses = c_parser_oacc_simple_clause (parser, OMP_CLAUSE_SEQ,
12675 clauses);
12676 c_name = "seq";
12677 break;
12678 case PRAGMA_OACC_CLAUSE_VECTOR:
12679 c_name = "vector";
12680 clauses = c_parser_oacc_shape_clause (parser, OMP_CLAUSE_VECTOR,
12681 c_name, clauses);
12682 break;
12683 case PRAGMA_OACC_CLAUSE_VECTOR_LENGTH:
12684 clauses = c_parser_omp_clause_vector_length (parser, clauses);
12685 c_name = "vector_length";
12686 break;
12687 case PRAGMA_OACC_CLAUSE_WAIT:
12688 clauses = c_parser_oacc_clause_wait (parser, clauses);
12689 c_name = "wait";
12690 break;
12691 case PRAGMA_OACC_CLAUSE_WORKER:
12692 c_name = "worker";
12693 clauses = c_parser_oacc_shape_clause (parser, OMP_CLAUSE_WORKER,
12694 c_name, clauses);
12695 break;
12696 default:
12697 c_parser_error (parser, "expected %<#pragma acc%> clause");
12698 goto saw_error;
12699 }
12700
12701 first = false;
12702
12703 if (((mask >> c_kind) & 1) == 0)
12704 {
12705 /* Remove the invalid clause(s) from the list to avoid
12706 confusing the rest of the compiler. */
12707 clauses = prev;
12708 error_at (here, "%qs is not valid for %qs", c_name, where);
12709 }
12710 }
12711
12712 saw_error:
12713 c_parser_skip_to_pragma_eol (parser);
12714
12715 if (finish_p)
12716 return c_finish_omp_clauses (clauses, false);
12717
12718 return clauses;
12719 }
12720
12721 /* Parse all OpenMP clauses. The set clauses allowed by the directive
12722 is a bitmask in MASK. Return the list of clauses found. */
12723
12724 static tree
12725 c_parser_omp_all_clauses (c_parser *parser, omp_clause_mask mask,
12726 const char *where, bool finish_p = true)
12727 {
12728 tree clauses = NULL;
12729 bool first = true, cilk_simd_fn = false;
12730
12731 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
12732 {
12733 location_t here;
12734 pragma_omp_clause c_kind;
12735 const char *c_name;
12736 tree prev = clauses;
12737
12738 if (!first && c_parser_next_token_is (parser, CPP_COMMA))
12739 c_parser_consume_token (parser);
12740
12741 here = c_parser_peek_token (parser)->location;
12742 c_kind = c_parser_omp_clause_name (parser);
12743
12744 switch (c_kind)
12745 {
12746 case PRAGMA_OMP_CLAUSE_COLLAPSE:
12747 clauses = c_parser_omp_clause_collapse (parser, clauses);
12748 c_name = "collapse";
12749 break;
12750 case PRAGMA_OMP_CLAUSE_COPYIN:
12751 clauses = c_parser_omp_clause_copyin (parser, clauses);
12752 c_name = "copyin";
12753 break;
12754 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
12755 clauses = c_parser_omp_clause_copyprivate (parser, clauses);
12756 c_name = "copyprivate";
12757 break;
12758 case PRAGMA_OMP_CLAUSE_DEFAULT:
12759 clauses = c_parser_omp_clause_default (parser, clauses);
12760 c_name = "default";
12761 break;
12762 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
12763 clauses = c_parser_omp_clause_firstprivate (parser, clauses);
12764 c_name = "firstprivate";
12765 break;
12766 case PRAGMA_OMP_CLAUSE_FINAL:
12767 clauses = c_parser_omp_clause_final (parser, clauses);
12768 c_name = "final";
12769 break;
12770 case PRAGMA_OMP_CLAUSE_GRAINSIZE:
12771 clauses = c_parser_omp_clause_grainsize (parser, clauses);
12772 c_name = "grainsize";
12773 break;
12774 case PRAGMA_OMP_CLAUSE_HINT:
12775 clauses = c_parser_omp_clause_hint (parser, clauses);
12776 c_name = "hint";
12777 break;
12778 case PRAGMA_OMP_CLAUSE_DEFAULTMAP:
12779 clauses = c_parser_omp_clause_defaultmap (parser, clauses);
12780 c_name = "defaultmap";
12781 break;
12782 case PRAGMA_OMP_CLAUSE_IF:
12783 clauses = c_parser_omp_clause_if (parser, clauses, true);
12784 c_name = "if";
12785 break;
12786 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
12787 clauses = c_parser_omp_clause_lastprivate (parser, clauses);
12788 c_name = "lastprivate";
12789 break;
12790 case PRAGMA_OMP_CLAUSE_MERGEABLE:
12791 clauses = c_parser_omp_clause_mergeable (parser, clauses);
12792 c_name = "mergeable";
12793 break;
12794 case PRAGMA_OMP_CLAUSE_NOWAIT:
12795 clauses = c_parser_omp_clause_nowait (parser, clauses);
12796 c_name = "nowait";
12797 break;
12798 case PRAGMA_OMP_CLAUSE_NUM_TASKS:
12799 clauses = c_parser_omp_clause_num_tasks (parser, clauses);
12800 c_name = "num_tasks";
12801 break;
12802 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
12803 clauses = c_parser_omp_clause_num_threads (parser, clauses);
12804 c_name = "num_threads";
12805 break;
12806 case PRAGMA_OMP_CLAUSE_ORDERED:
12807 clauses = c_parser_omp_clause_ordered (parser, clauses);
12808 c_name = "ordered";
12809 break;
12810 case PRAGMA_OMP_CLAUSE_PRIORITY:
12811 clauses = c_parser_omp_clause_priority (parser, clauses);
12812 c_name = "priority";
12813 break;
12814 case PRAGMA_OMP_CLAUSE_PRIVATE:
12815 clauses = c_parser_omp_clause_private (parser, clauses);
12816 c_name = "private";
12817 break;
12818 case PRAGMA_OMP_CLAUSE_REDUCTION:
12819 clauses = c_parser_omp_clause_reduction (parser, clauses);
12820 c_name = "reduction";
12821 break;
12822 case PRAGMA_OMP_CLAUSE_SCHEDULE:
12823 clauses = c_parser_omp_clause_schedule (parser, clauses);
12824 c_name = "schedule";
12825 break;
12826 case PRAGMA_OMP_CLAUSE_SHARED:
12827 clauses = c_parser_omp_clause_shared (parser, clauses);
12828 c_name = "shared";
12829 break;
12830 case PRAGMA_OMP_CLAUSE_UNTIED:
12831 clauses = c_parser_omp_clause_untied (parser, clauses);
12832 c_name = "untied";
12833 break;
12834 case PRAGMA_OMP_CLAUSE_INBRANCH:
12835 case PRAGMA_CILK_CLAUSE_MASK:
12836 clauses = c_parser_omp_clause_branch (parser, OMP_CLAUSE_INBRANCH,
12837 clauses);
12838 c_name = "inbranch";
12839 break;
12840 case PRAGMA_OMP_CLAUSE_NOTINBRANCH:
12841 case PRAGMA_CILK_CLAUSE_NOMASK:
12842 clauses = c_parser_omp_clause_branch (parser, OMP_CLAUSE_NOTINBRANCH,
12843 clauses);
12844 c_name = "notinbranch";
12845 break;
12846 case PRAGMA_OMP_CLAUSE_PARALLEL:
12847 clauses
12848 = c_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_PARALLEL,
12849 clauses);
12850 c_name = "parallel";
12851 if (!first)
12852 {
12853 clause_not_first:
12854 error_at (here, "%qs must be the first clause of %qs",
12855 c_name, where);
12856 clauses = prev;
12857 }
12858 break;
12859 case PRAGMA_OMP_CLAUSE_FOR:
12860 clauses
12861 = c_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_FOR,
12862 clauses);
12863 c_name = "for";
12864 if (!first)
12865 goto clause_not_first;
12866 break;
12867 case PRAGMA_OMP_CLAUSE_SECTIONS:
12868 clauses
12869 = c_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_SECTIONS,
12870 clauses);
12871 c_name = "sections";
12872 if (!first)
12873 goto clause_not_first;
12874 break;
12875 case PRAGMA_OMP_CLAUSE_TASKGROUP:
12876 clauses
12877 = c_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_TASKGROUP,
12878 clauses);
12879 c_name = "taskgroup";
12880 if (!first)
12881 goto clause_not_first;
12882 break;
12883 case PRAGMA_OMP_CLAUSE_LINK:
12884 clauses
12885 = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_LINK, clauses);
12886 c_name = "link";
12887 break;
12888 case PRAGMA_OMP_CLAUSE_TO:
12889 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK)) != 0)
12890 clauses
12891 = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_TO_DECLARE,
12892 clauses);
12893 else
12894 clauses = c_parser_omp_clause_to (parser, clauses);
12895 c_name = "to";
12896 break;
12897 case PRAGMA_OMP_CLAUSE_FROM:
12898 clauses = c_parser_omp_clause_from (parser, clauses);
12899 c_name = "from";
12900 break;
12901 case PRAGMA_OMP_CLAUSE_UNIFORM:
12902 clauses = c_parser_omp_clause_uniform (parser, clauses);
12903 c_name = "uniform";
12904 break;
12905 case PRAGMA_OMP_CLAUSE_NUM_TEAMS:
12906 clauses = c_parser_omp_clause_num_teams (parser, clauses);
12907 c_name = "num_teams";
12908 break;
12909 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT:
12910 clauses = c_parser_omp_clause_thread_limit (parser, clauses);
12911 c_name = "thread_limit";
12912 break;
12913 case PRAGMA_OMP_CLAUSE_ALIGNED:
12914 clauses = c_parser_omp_clause_aligned (parser, clauses);
12915 c_name = "aligned";
12916 break;
12917 case PRAGMA_OMP_CLAUSE_LINEAR:
12918 if (((mask >> PRAGMA_CILK_CLAUSE_VECTORLENGTH) & 1) != 0)
12919 cilk_simd_fn = true;
12920 clauses = c_parser_omp_clause_linear (parser, clauses, cilk_simd_fn);
12921 c_name = "linear";
12922 break;
12923 case PRAGMA_OMP_CLAUSE_DEPEND:
12924 clauses = c_parser_omp_clause_depend (parser, clauses);
12925 c_name = "depend";
12926 break;
12927 case PRAGMA_OMP_CLAUSE_MAP:
12928 clauses = c_parser_omp_clause_map (parser, clauses);
12929 c_name = "map";
12930 break;
12931 case PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR:
12932 clauses = c_parser_omp_clause_use_device_ptr (parser, clauses);
12933 c_name = "use_device_ptr";
12934 break;
12935 case PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR:
12936 clauses = c_parser_omp_clause_is_device_ptr (parser, clauses);
12937 c_name = "is_device_ptr";
12938 break;
12939 case PRAGMA_OMP_CLAUSE_DEVICE:
12940 clauses = c_parser_omp_clause_device (parser, clauses);
12941 c_name = "device";
12942 break;
12943 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE:
12944 clauses = c_parser_omp_clause_dist_schedule (parser, clauses);
12945 c_name = "dist_schedule";
12946 break;
12947 case PRAGMA_OMP_CLAUSE_PROC_BIND:
12948 clauses = c_parser_omp_clause_proc_bind (parser, clauses);
12949 c_name = "proc_bind";
12950 break;
12951 case PRAGMA_OMP_CLAUSE_SAFELEN:
12952 clauses = c_parser_omp_clause_safelen (parser, clauses);
12953 c_name = "safelen";
12954 break;
12955 case PRAGMA_CILK_CLAUSE_VECTORLENGTH:
12956 clauses = c_parser_cilk_clause_vectorlength (parser, clauses, true);
12957 c_name = "simdlen";
12958 break;
12959 case PRAGMA_OMP_CLAUSE_SIMDLEN:
12960 clauses = c_parser_omp_clause_simdlen (parser, clauses);
12961 c_name = "simdlen";
12962 break;
12963 case PRAGMA_OMP_CLAUSE_NOGROUP:
12964 clauses = c_parser_omp_clause_nogroup (parser, clauses);
12965 c_name = "nogroup";
12966 break;
12967 case PRAGMA_OMP_CLAUSE_THREADS:
12968 clauses
12969 = c_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_THREADS,
12970 clauses);
12971 c_name = "threads";
12972 break;
12973 case PRAGMA_OMP_CLAUSE_SIMD:
12974 clauses
12975 = c_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_SIMD,
12976 clauses);
12977 c_name = "simd";
12978 break;
12979 default:
12980 c_parser_error (parser, "expected %<#pragma omp%> clause");
12981 goto saw_error;
12982 }
12983
12984 first = false;
12985
12986 if (((mask >> c_kind) & 1) == 0)
12987 {
12988 /* Remove the invalid clause(s) from the list to avoid
12989 confusing the rest of the compiler. */
12990 clauses = prev;
12991 error_at (here, "%qs is not valid for %qs", c_name, where);
12992 }
12993 }
12994
12995 saw_error:
12996 c_parser_skip_to_pragma_eol (parser);
12997
12998 if (finish_p)
12999 {
13000 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM)) != 0)
13001 return c_finish_omp_clauses (clauses, true, true);
13002 return c_finish_omp_clauses (clauses, true);
13003 }
13004
13005 return clauses;
13006 }
13007
13008 /* OpenACC 2.0, OpenMP 2.5:
13009 structured-block:
13010 statement
13011
13012 In practice, we're also interested in adding the statement to an
13013 outer node. So it is convenient if we work around the fact that
13014 c_parser_statement calls add_stmt. */
13015
13016 static tree
13017 c_parser_omp_structured_block (c_parser *parser)
13018 {
13019 tree stmt = push_stmt_list ();
13020 c_parser_statement (parser);
13021 return pop_stmt_list (stmt);
13022 }
13023
13024 /* OpenACC 2.0:
13025 # pragma acc cache (variable-list) new-line
13026
13027 LOC is the location of the #pragma token.
13028 */
13029
13030 static tree
13031 c_parser_oacc_cache (location_t loc, c_parser *parser)
13032 {
13033 tree stmt, clauses;
13034
13035 clauses = c_parser_omp_var_list_parens (parser, OMP_CLAUSE__CACHE_, NULL);
13036 clauses = c_finish_omp_clauses (clauses, false);
13037
13038 c_parser_skip_to_pragma_eol (parser);
13039
13040 stmt = make_node (OACC_CACHE);
13041 TREE_TYPE (stmt) = void_type_node;
13042 OACC_CACHE_CLAUSES (stmt) = clauses;
13043 SET_EXPR_LOCATION (stmt, loc);
13044 add_stmt (stmt);
13045
13046 return stmt;
13047 }
13048
13049 /* OpenACC 2.0:
13050 # pragma acc data oacc-data-clause[optseq] new-line
13051 structured-block
13052
13053 LOC is the location of the #pragma token.
13054 */
13055
13056 #define OACC_DATA_CLAUSE_MASK \
13057 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
13058 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
13059 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
13060 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
13061 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
13062 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
13063 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
13064 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
13065 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
13066 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
13067 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) )
13068
13069 static tree
13070 c_parser_oacc_data (location_t loc, c_parser *parser)
13071 {
13072 tree stmt, clauses, block;
13073
13074 clauses = c_parser_oacc_all_clauses (parser, OACC_DATA_CLAUSE_MASK,
13075 "#pragma acc data");
13076
13077 block = c_begin_omp_parallel ();
13078 add_stmt (c_parser_omp_structured_block (parser));
13079
13080 stmt = c_finish_oacc_data (loc, clauses, block);
13081
13082 return stmt;
13083 }
13084
13085 /* OpenACC 2.0:
13086 # pragma acc enter data oacc-enter-data-clause[optseq] new-line
13087
13088 or
13089
13090 # pragma acc exit data oacc-exit-data-clause[optseq] new-line
13091
13092
13093 LOC is the location of the #pragma token.
13094 */
13095
13096 #define OACC_ENTER_DATA_CLAUSE_MASK \
13097 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
13098 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
13099 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
13100 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
13101 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
13102 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
13103 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
13104
13105 #define OACC_EXIT_DATA_CLAUSE_MASK \
13106 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
13107 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
13108 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
13109 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DELETE) \
13110 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
13111
13112 static void
13113 c_parser_oacc_enter_exit_data (c_parser *parser, bool enter)
13114 {
13115 location_t loc = c_parser_peek_token (parser)->location;
13116 tree clauses, stmt;
13117
13118 c_parser_consume_pragma (parser);
13119
13120 if (!c_parser_next_token_is (parser, CPP_NAME))
13121 {
13122 c_parser_error (parser, enter
13123 ? "expected %<data%> in %<#pragma acc enter data%>"
13124 : "expected %<data%> in %<#pragma acc exit data%>");
13125 c_parser_skip_to_pragma_eol (parser);
13126 return;
13127 }
13128
13129 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
13130 if (strcmp (p, "data") != 0)
13131 {
13132 c_parser_error (parser, "invalid pragma");
13133 c_parser_skip_to_pragma_eol (parser);
13134 return;
13135 }
13136
13137 c_parser_consume_token (parser);
13138
13139 if (enter)
13140 clauses = c_parser_oacc_all_clauses (parser, OACC_ENTER_DATA_CLAUSE_MASK,
13141 "#pragma acc enter data");
13142 else
13143 clauses = c_parser_oacc_all_clauses (parser, OACC_EXIT_DATA_CLAUSE_MASK,
13144 "#pragma acc exit data");
13145
13146 if (find_omp_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
13147 {
13148 error_at (loc, enter
13149 ? "%<#pragma acc enter data%> has no data movement clause"
13150 : "%<#pragma acc exit data%> has no data movement clause");
13151 return;
13152 }
13153
13154 stmt = enter ? make_node (OACC_ENTER_DATA) : make_node (OACC_EXIT_DATA);
13155 TREE_TYPE (stmt) = void_type_node;
13156 OMP_STANDALONE_CLAUSES (stmt) = clauses;
13157 SET_EXPR_LOCATION (stmt, loc);
13158 add_stmt (stmt);
13159 }
13160
13161
13162 /* OpenACC 2.0:
13163
13164 # pragma acc loop oacc-loop-clause[optseq] new-line
13165 structured-block
13166
13167 LOC is the location of the #pragma token.
13168 */
13169
13170 #define OACC_LOOP_CLAUSE_MASK \
13171 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COLLAPSE) \
13172 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
13173 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
13174 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
13175 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_AUTO) \
13176 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ) \
13177 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) )
13178
13179 static tree
13180 c_parser_oacc_loop (location_t loc, c_parser *parser, char *p_name,
13181 omp_clause_mask mask, tree *cclauses)
13182 {
13183 strcat (p_name, " loop");
13184 mask |= OACC_LOOP_CLAUSE_MASK;
13185
13186 tree clauses = c_parser_oacc_all_clauses (parser, mask, p_name,
13187 cclauses == NULL);
13188 if (cclauses)
13189 {
13190 clauses = c_oacc_split_loop_clauses (clauses, cclauses);
13191 if (*cclauses)
13192 c_finish_omp_clauses (*cclauses, false);
13193 if (clauses)
13194 c_finish_omp_clauses (clauses, false);
13195 }
13196
13197 tree block = c_begin_compound_stmt (true);
13198 tree stmt = c_parser_omp_for_loop (loc, parser, OACC_LOOP, clauses, NULL);
13199 block = c_end_compound_stmt (loc, block, true);
13200 add_stmt (block);
13201
13202 return stmt;
13203 }
13204
13205 /* OpenACC 2.0:
13206 # pragma acc kernels oacc-kernels-clause[optseq] new-line
13207 structured-block
13208
13209 or
13210
13211 # pragma acc parallel oacc-parallel-clause[optseq] new-line
13212 structured-block
13213
13214 LOC is the location of the #pragma token.
13215 */
13216
13217 #define OACC_KERNELS_CLAUSE_MASK \
13218 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
13219 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
13220 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
13221 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
13222 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
13223 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
13224 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
13225 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
13226 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
13227 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
13228 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
13229 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
13230 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
13231
13232 #define OACC_PARALLEL_CLAUSE_MASK \
13233 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
13234 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
13235 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
13236 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
13237 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
13238 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
13239 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
13240 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
13241 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
13242 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
13243 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
13244 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
13245 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
13246 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
13247 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
13248 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
13249 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
13250
13251 static tree
13252 c_parser_oacc_kernels_parallel (location_t loc, c_parser *parser,
13253 enum pragma_kind p_kind, char *p_name)
13254 {
13255 omp_clause_mask mask;
13256 enum tree_code code;
13257 switch (p_kind)
13258 {
13259 case PRAGMA_OACC_KERNELS:
13260 strcat (p_name, " kernels");
13261 mask = OACC_KERNELS_CLAUSE_MASK;
13262 code = OACC_KERNELS;
13263 break;
13264 case PRAGMA_OACC_PARALLEL:
13265 strcat (p_name, " parallel");
13266 mask = OACC_PARALLEL_CLAUSE_MASK;
13267 code = OACC_PARALLEL;
13268 break;
13269 default:
13270 gcc_unreachable ();
13271 }
13272
13273 if (c_parser_next_token_is (parser, CPP_NAME))
13274 {
13275 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
13276 if (strcmp (p, "loop") == 0)
13277 {
13278 c_parser_consume_token (parser);
13279 mask |= OACC_LOOP_CLAUSE_MASK;
13280
13281 tree block = c_begin_omp_parallel ();
13282 tree clauses;
13283 c_parser_oacc_loop (loc, parser, p_name, mask, &clauses);
13284 return c_finish_omp_construct (loc, code, block, clauses);
13285 }
13286 }
13287
13288 tree clauses = c_parser_oacc_all_clauses (parser, mask, p_name);
13289
13290 tree block = c_begin_omp_parallel ();
13291 add_stmt (c_parser_omp_structured_block (parser));
13292
13293 return c_finish_omp_construct (loc, code, block, clauses);
13294 }
13295
13296 /* OpenACC 2.0:
13297 # pragma acc update oacc-update-clause[optseq] new-line
13298 */
13299
13300 #define OACC_UPDATE_CLAUSE_MASK \
13301 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
13302 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE) \
13303 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_HOST) \
13304 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
13305 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SELF) \
13306 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
13307
13308 static void
13309 c_parser_oacc_update (c_parser *parser)
13310 {
13311 location_t loc = c_parser_peek_token (parser)->location;
13312
13313 c_parser_consume_pragma (parser);
13314
13315 tree clauses = c_parser_oacc_all_clauses (parser, OACC_UPDATE_CLAUSE_MASK,
13316 "#pragma acc update");
13317 if (find_omp_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
13318 {
13319 error_at (loc,
13320 "%<#pragma acc update%> must contain at least one "
13321 "%<device%> or %<host/self%> clause");
13322 return;
13323 }
13324
13325 if (parser->error)
13326 return;
13327
13328 tree stmt = make_node (OACC_UPDATE);
13329 TREE_TYPE (stmt) = void_type_node;
13330 OACC_UPDATE_CLAUSES (stmt) = clauses;
13331 SET_EXPR_LOCATION (stmt, loc);
13332 add_stmt (stmt);
13333 }
13334
13335 /* OpenACC 2.0:
13336 # pragma acc wait [(intseq)] oacc-wait-clause[optseq] new-line
13337
13338 LOC is the location of the #pragma token.
13339 */
13340
13341 #define OACC_WAIT_CLAUSE_MASK \
13342 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) )
13343
13344 static tree
13345 c_parser_oacc_wait (location_t loc, c_parser *parser, char *p_name)
13346 {
13347 tree clauses, list = NULL_TREE, stmt = NULL_TREE;
13348
13349 if (c_parser_peek_token (parser)->type == CPP_OPEN_PAREN)
13350 list = c_parser_oacc_wait_list (parser, loc, list);
13351
13352 strcpy (p_name, " wait");
13353 clauses = c_parser_oacc_all_clauses (parser, OACC_WAIT_CLAUSE_MASK, p_name);
13354 stmt = c_finish_oacc_wait (loc, list, clauses);
13355
13356 return stmt;
13357 }
13358
13359 /* OpenMP 2.5:
13360 # pragma omp atomic new-line
13361 expression-stmt
13362
13363 expression-stmt:
13364 x binop= expr | x++ | ++x | x-- | --x
13365 binop:
13366 +, *, -, /, &, ^, |, <<, >>
13367
13368 where x is an lvalue expression with scalar type.
13369
13370 OpenMP 3.1:
13371 # pragma omp atomic new-line
13372 update-stmt
13373
13374 # pragma omp atomic read new-line
13375 read-stmt
13376
13377 # pragma omp atomic write new-line
13378 write-stmt
13379
13380 # pragma omp atomic update new-line
13381 update-stmt
13382
13383 # pragma omp atomic capture new-line
13384 capture-stmt
13385
13386 # pragma omp atomic capture new-line
13387 capture-block
13388
13389 read-stmt:
13390 v = x
13391 write-stmt:
13392 x = expr
13393 update-stmt:
13394 expression-stmt | x = x binop expr
13395 capture-stmt:
13396 v = expression-stmt
13397 capture-block:
13398 { v = x; update-stmt; } | { update-stmt; v = x; }
13399
13400 OpenMP 4.0:
13401 update-stmt:
13402 expression-stmt | x = x binop expr | x = expr binop x
13403 capture-stmt:
13404 v = update-stmt
13405 capture-block:
13406 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
13407
13408 where x and v are lvalue expressions with scalar type.
13409
13410 LOC is the location of the #pragma token. */
13411
13412 static void
13413 c_parser_omp_atomic (location_t loc, c_parser *parser)
13414 {
13415 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE;
13416 tree lhs1 = NULL_TREE, rhs1 = NULL_TREE;
13417 tree stmt, orig_lhs, unfolded_lhs = NULL_TREE, unfolded_lhs1 = NULL_TREE;
13418 enum tree_code code = OMP_ATOMIC, opcode = NOP_EXPR;
13419 struct c_expr expr;
13420 location_t eloc;
13421 bool structured_block = false;
13422 bool swapped = false;
13423 bool seq_cst = false;
13424 bool non_lvalue_p;
13425
13426 if (c_parser_next_token_is (parser, CPP_NAME))
13427 {
13428 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
13429 if (!strcmp (p, "seq_cst"))
13430 {
13431 seq_cst = true;
13432 c_parser_consume_token (parser);
13433 if (c_parser_next_token_is (parser, CPP_COMMA)
13434 && c_parser_peek_2nd_token (parser)->type == CPP_NAME)
13435 c_parser_consume_token (parser);
13436 }
13437 }
13438 if (c_parser_next_token_is (parser, CPP_NAME))
13439 {
13440 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
13441
13442 if (!strcmp (p, "read"))
13443 code = OMP_ATOMIC_READ;
13444 else if (!strcmp (p, "write"))
13445 code = NOP_EXPR;
13446 else if (!strcmp (p, "update"))
13447 code = OMP_ATOMIC;
13448 else if (!strcmp (p, "capture"))
13449 code = OMP_ATOMIC_CAPTURE_NEW;
13450 else
13451 p = NULL;
13452 if (p)
13453 c_parser_consume_token (parser);
13454 }
13455 if (!seq_cst)
13456 {
13457 if (c_parser_next_token_is (parser, CPP_COMMA)
13458 && c_parser_peek_2nd_token (parser)->type == CPP_NAME)
13459 c_parser_consume_token (parser);
13460
13461 if (c_parser_next_token_is (parser, CPP_NAME))
13462 {
13463 const char *p
13464 = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
13465 if (!strcmp (p, "seq_cst"))
13466 {
13467 seq_cst = true;
13468 c_parser_consume_token (parser);
13469 }
13470 }
13471 }
13472 c_parser_skip_to_pragma_eol (parser);
13473
13474 switch (code)
13475 {
13476 case OMP_ATOMIC_READ:
13477 case NOP_EXPR: /* atomic write */
13478 v = c_parser_cast_expression (parser, NULL).value;
13479 non_lvalue_p = !lvalue_p (v);
13480 v = c_fully_fold (v, false, NULL);
13481 if (v == error_mark_node)
13482 goto saw_error;
13483 if (non_lvalue_p)
13484 v = non_lvalue (v);
13485 loc = c_parser_peek_token (parser)->location;
13486 if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
13487 goto saw_error;
13488 if (code == NOP_EXPR)
13489 {
13490 lhs = c_parser_expression (parser).value;
13491 lhs = c_fully_fold (lhs, false, NULL);
13492 if (lhs == error_mark_node)
13493 goto saw_error;
13494 }
13495 else
13496 {
13497 lhs = c_parser_cast_expression (parser, NULL).value;
13498 non_lvalue_p = !lvalue_p (lhs);
13499 lhs = c_fully_fold (lhs, false, NULL);
13500 if (lhs == error_mark_node)
13501 goto saw_error;
13502 if (non_lvalue_p)
13503 lhs = non_lvalue (lhs);
13504 }
13505 if (code == NOP_EXPR)
13506 {
13507 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
13508 opcode. */
13509 code = OMP_ATOMIC;
13510 rhs = lhs;
13511 lhs = v;
13512 v = NULL_TREE;
13513 }
13514 goto done;
13515 case OMP_ATOMIC_CAPTURE_NEW:
13516 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
13517 {
13518 c_parser_consume_token (parser);
13519 structured_block = true;
13520 }
13521 else
13522 {
13523 v = c_parser_cast_expression (parser, NULL).value;
13524 non_lvalue_p = !lvalue_p (v);
13525 v = c_fully_fold (v, false, NULL);
13526 if (v == error_mark_node)
13527 goto saw_error;
13528 if (non_lvalue_p)
13529 v = non_lvalue (v);
13530 if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
13531 goto saw_error;
13532 }
13533 break;
13534 default:
13535 break;
13536 }
13537
13538 /* For structured_block case we don't know yet whether
13539 old or new x should be captured. */
13540 restart:
13541 eloc = c_parser_peek_token (parser)->location;
13542 expr = c_parser_cast_expression (parser, NULL);
13543 lhs = expr.value;
13544 expr = default_function_array_conversion (eloc, expr);
13545 unfolded_lhs = expr.value;
13546 lhs = c_fully_fold (lhs, false, NULL);
13547 orig_lhs = lhs;
13548 switch (TREE_CODE (lhs))
13549 {
13550 case ERROR_MARK:
13551 saw_error:
13552 c_parser_skip_to_end_of_block_or_statement (parser);
13553 if (structured_block)
13554 {
13555 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
13556 c_parser_consume_token (parser);
13557 else if (code == OMP_ATOMIC_CAPTURE_NEW)
13558 {
13559 c_parser_skip_to_end_of_block_or_statement (parser);
13560 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
13561 c_parser_consume_token (parser);
13562 }
13563 }
13564 return;
13565
13566 case POSTINCREMENT_EXPR:
13567 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
13568 code = OMP_ATOMIC_CAPTURE_OLD;
13569 /* FALLTHROUGH */
13570 case PREINCREMENT_EXPR:
13571 lhs = TREE_OPERAND (lhs, 0);
13572 unfolded_lhs = NULL_TREE;
13573 opcode = PLUS_EXPR;
13574 rhs = integer_one_node;
13575 break;
13576
13577 case POSTDECREMENT_EXPR:
13578 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
13579 code = OMP_ATOMIC_CAPTURE_OLD;
13580 /* FALLTHROUGH */
13581 case PREDECREMENT_EXPR:
13582 lhs = TREE_OPERAND (lhs, 0);
13583 unfolded_lhs = NULL_TREE;
13584 opcode = MINUS_EXPR;
13585 rhs = integer_one_node;
13586 break;
13587
13588 case COMPOUND_EXPR:
13589 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
13590 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
13591 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
13592 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
13593 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
13594 (TREE_OPERAND (lhs, 1), 0), 0)))
13595 == BOOLEAN_TYPE)
13596 /* Undo effects of boolean_increment for post {in,de}crement. */
13597 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
13598 /* FALLTHRU */
13599 case MODIFY_EXPR:
13600 if (TREE_CODE (lhs) == MODIFY_EXPR
13601 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
13602 {
13603 /* Undo effects of boolean_increment. */
13604 if (integer_onep (TREE_OPERAND (lhs, 1)))
13605 {
13606 /* This is pre or post increment. */
13607 rhs = TREE_OPERAND (lhs, 1);
13608 lhs = TREE_OPERAND (lhs, 0);
13609 unfolded_lhs = NULL_TREE;
13610 opcode = NOP_EXPR;
13611 if (code == OMP_ATOMIC_CAPTURE_NEW
13612 && !structured_block
13613 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
13614 code = OMP_ATOMIC_CAPTURE_OLD;
13615 break;
13616 }
13617 if (TREE_CODE (TREE_OPERAND (lhs, 1)) == TRUTH_NOT_EXPR
13618 && TREE_OPERAND (lhs, 0)
13619 == TREE_OPERAND (TREE_OPERAND (lhs, 1), 0))
13620 {
13621 /* This is pre or post decrement. */
13622 rhs = TREE_OPERAND (lhs, 1);
13623 lhs = TREE_OPERAND (lhs, 0);
13624 unfolded_lhs = NULL_TREE;
13625 opcode = NOP_EXPR;
13626 if (code == OMP_ATOMIC_CAPTURE_NEW
13627 && !structured_block
13628 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
13629 code = OMP_ATOMIC_CAPTURE_OLD;
13630 break;
13631 }
13632 }
13633 /* FALLTHRU */
13634 default:
13635 if (!lvalue_p (unfolded_lhs))
13636 lhs = non_lvalue (lhs);
13637 switch (c_parser_peek_token (parser)->type)
13638 {
13639 case CPP_MULT_EQ:
13640 opcode = MULT_EXPR;
13641 break;
13642 case CPP_DIV_EQ:
13643 opcode = TRUNC_DIV_EXPR;
13644 break;
13645 case CPP_PLUS_EQ:
13646 opcode = PLUS_EXPR;
13647 break;
13648 case CPP_MINUS_EQ:
13649 opcode = MINUS_EXPR;
13650 break;
13651 case CPP_LSHIFT_EQ:
13652 opcode = LSHIFT_EXPR;
13653 break;
13654 case CPP_RSHIFT_EQ:
13655 opcode = RSHIFT_EXPR;
13656 break;
13657 case CPP_AND_EQ:
13658 opcode = BIT_AND_EXPR;
13659 break;
13660 case CPP_OR_EQ:
13661 opcode = BIT_IOR_EXPR;
13662 break;
13663 case CPP_XOR_EQ:
13664 opcode = BIT_XOR_EXPR;
13665 break;
13666 case CPP_EQ:
13667 c_parser_consume_token (parser);
13668 eloc = c_parser_peek_token (parser)->location;
13669 expr = c_parser_expr_no_commas (parser, NULL, unfolded_lhs);
13670 rhs1 = expr.value;
13671 switch (TREE_CODE (rhs1))
13672 {
13673 case MULT_EXPR:
13674 case TRUNC_DIV_EXPR:
13675 case RDIV_EXPR:
13676 case PLUS_EXPR:
13677 case MINUS_EXPR:
13678 case LSHIFT_EXPR:
13679 case RSHIFT_EXPR:
13680 case BIT_AND_EXPR:
13681 case BIT_IOR_EXPR:
13682 case BIT_XOR_EXPR:
13683 if (c_tree_equal (TREE_OPERAND (rhs1, 0), unfolded_lhs))
13684 {
13685 opcode = TREE_CODE (rhs1);
13686 rhs = c_fully_fold (TREE_OPERAND (rhs1, 1), false, NULL);
13687 rhs1 = c_fully_fold (TREE_OPERAND (rhs1, 0), false, NULL);
13688 goto stmt_done;
13689 }
13690 if (c_tree_equal (TREE_OPERAND (rhs1, 1), unfolded_lhs))
13691 {
13692 opcode = TREE_CODE (rhs1);
13693 rhs = c_fully_fold (TREE_OPERAND (rhs1, 0), false, NULL);
13694 rhs1 = c_fully_fold (TREE_OPERAND (rhs1, 1), false, NULL);
13695 swapped = !commutative_tree_code (opcode);
13696 goto stmt_done;
13697 }
13698 break;
13699 case ERROR_MARK:
13700 goto saw_error;
13701 default:
13702 break;
13703 }
13704 if (c_parser_peek_token (parser)->type == CPP_SEMICOLON)
13705 {
13706 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
13707 {
13708 code = OMP_ATOMIC_CAPTURE_OLD;
13709 v = lhs;
13710 lhs = NULL_TREE;
13711 expr = default_function_array_read_conversion (eloc, expr);
13712 unfolded_lhs1 = expr.value;
13713 lhs1 = c_fully_fold (unfolded_lhs1, false, NULL);
13714 rhs1 = NULL_TREE;
13715 c_parser_consume_token (parser);
13716 goto restart;
13717 }
13718 if (structured_block)
13719 {
13720 opcode = NOP_EXPR;
13721 expr = default_function_array_read_conversion (eloc, expr);
13722 rhs = c_fully_fold (expr.value, false, NULL);
13723 rhs1 = NULL_TREE;
13724 goto stmt_done;
13725 }
13726 }
13727 c_parser_error (parser, "invalid form of %<#pragma omp atomic%>");
13728 goto saw_error;
13729 default:
13730 c_parser_error (parser,
13731 "invalid operator for %<#pragma omp atomic%>");
13732 goto saw_error;
13733 }
13734
13735 /* Arrange to pass the location of the assignment operator to
13736 c_finish_omp_atomic. */
13737 loc = c_parser_peek_token (parser)->location;
13738 c_parser_consume_token (parser);
13739 eloc = c_parser_peek_token (parser)->location;
13740 expr = c_parser_expression (parser);
13741 expr = default_function_array_read_conversion (eloc, expr);
13742 rhs = expr.value;
13743 rhs = c_fully_fold (rhs, false, NULL);
13744 break;
13745 }
13746 stmt_done:
13747 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
13748 {
13749 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
13750 goto saw_error;
13751 v = c_parser_cast_expression (parser, NULL).value;
13752 non_lvalue_p = !lvalue_p (v);
13753 v = c_fully_fold (v, false, NULL);
13754 if (v == error_mark_node)
13755 goto saw_error;
13756 if (non_lvalue_p)
13757 v = non_lvalue (v);
13758 if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
13759 goto saw_error;
13760 eloc = c_parser_peek_token (parser)->location;
13761 expr = c_parser_cast_expression (parser, NULL);
13762 lhs1 = expr.value;
13763 expr = default_function_array_read_conversion (eloc, expr);
13764 unfolded_lhs1 = expr.value;
13765 lhs1 = c_fully_fold (lhs1, false, NULL);
13766 if (lhs1 == error_mark_node)
13767 goto saw_error;
13768 if (!lvalue_p (unfolded_lhs1))
13769 lhs1 = non_lvalue (lhs1);
13770 }
13771 if (structured_block)
13772 {
13773 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
13774 c_parser_require (parser, CPP_CLOSE_BRACE, "expected %<}%>");
13775 }
13776 done:
13777 if (unfolded_lhs && unfolded_lhs1
13778 && !c_tree_equal (unfolded_lhs, unfolded_lhs1))
13779 {
13780 error ("%<#pragma omp atomic capture%> uses two different "
13781 "expressions for memory");
13782 stmt = error_mark_node;
13783 }
13784 else
13785 stmt = c_finish_omp_atomic (loc, code, opcode, lhs, rhs, v, lhs1, rhs1,
13786 swapped, seq_cst);
13787 if (stmt != error_mark_node)
13788 add_stmt (stmt);
13789
13790 if (!structured_block)
13791 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
13792 }
13793
13794
13795 /* OpenMP 2.5:
13796 # pragma omp barrier new-line
13797 */
13798
13799 static void
13800 c_parser_omp_barrier (c_parser *parser)
13801 {
13802 location_t loc = c_parser_peek_token (parser)->location;
13803 c_parser_consume_pragma (parser);
13804 c_parser_skip_to_pragma_eol (parser);
13805
13806 c_finish_omp_barrier (loc);
13807 }
13808
13809 /* OpenMP 2.5:
13810 # pragma omp critical [(name)] new-line
13811 structured-block
13812
13813 OpenMP 4.5:
13814 # pragma omp critical [(name) [hint(expression)]] new-line
13815
13816 LOC is the location of the #pragma itself. */
13817
13818 #define OMP_CRITICAL_CLAUSE_MASK \
13819 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_HINT) )
13820
13821 static tree
13822 c_parser_omp_critical (location_t loc, c_parser *parser)
13823 {
13824 tree stmt, name = NULL_TREE, clauses = NULL_TREE;
13825
13826 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
13827 {
13828 c_parser_consume_token (parser);
13829 if (c_parser_next_token_is (parser, CPP_NAME))
13830 {
13831 name = c_parser_peek_token (parser)->value;
13832 c_parser_consume_token (parser);
13833 c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
13834 }
13835 else
13836 c_parser_error (parser, "expected identifier");
13837
13838 clauses = c_parser_omp_all_clauses (parser,
13839 OMP_CRITICAL_CLAUSE_MASK,
13840 "#pragma omp critical");
13841 }
13842 else
13843 {
13844 if (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
13845 c_parser_error (parser, "expected %<(%> or end of line");
13846 c_parser_skip_to_pragma_eol (parser);
13847 }
13848
13849 stmt = c_parser_omp_structured_block (parser);
13850 return c_finish_omp_critical (loc, stmt, name, clauses);
13851 }
13852
13853 /* OpenMP 2.5:
13854 # pragma omp flush flush-vars[opt] new-line
13855
13856 flush-vars:
13857 ( variable-list ) */
13858
13859 static void
13860 c_parser_omp_flush (c_parser *parser)
13861 {
13862 location_t loc = c_parser_peek_token (parser)->location;
13863 c_parser_consume_pragma (parser);
13864 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
13865 c_parser_omp_var_list_parens (parser, OMP_CLAUSE_ERROR, NULL);
13866 else if (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
13867 c_parser_error (parser, "expected %<(%> or end of line");
13868 c_parser_skip_to_pragma_eol (parser);
13869
13870 c_finish_omp_flush (loc);
13871 }
13872
13873 /* Parse the restricted form of loop statements allowed by OpenACC and OpenMP.
13874 The real trick here is to determine the loop control variable early
13875 so that we can push a new decl if necessary to make it private.
13876 LOC is the location of the "acc" or "omp" in "#pragma acc" or "#pragma omp",
13877 respectively. */
13878
13879 static tree
13880 c_parser_omp_for_loop (location_t loc, c_parser *parser, enum tree_code code,
13881 tree clauses, tree *cclauses)
13882 {
13883 tree decl, cond, incr, save_break, save_cont, body, init, stmt, cl;
13884 tree declv, condv, incrv, initv, ret = NULL_TREE;
13885 tree pre_body = NULL_TREE, this_pre_body;
13886 tree ordered_cl = NULL_TREE;
13887 bool fail = false, open_brace_parsed = false;
13888 int i, collapse = 1, ordered = 0, count, nbraces = 0;
13889 location_t for_loc;
13890 vec<tree, va_gc> *for_block = make_tree_vector ();
13891
13892 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
13893 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
13894 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl));
13895 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_ORDERED
13896 && OMP_CLAUSE_ORDERED_EXPR (cl))
13897 {
13898 ordered_cl = cl;
13899 ordered = tree_to_shwi (OMP_CLAUSE_ORDERED_EXPR (cl));
13900 }
13901
13902 if (ordered && ordered < collapse)
13903 {
13904 error_at (OMP_CLAUSE_LOCATION (ordered_cl),
13905 "%<ordered%> clause parameter is less than %<collapse%>");
13906 OMP_CLAUSE_ORDERED_EXPR (ordered_cl)
13907 = build_int_cst (NULL_TREE, collapse);
13908 ordered = collapse;
13909 }
13910 if (ordered)
13911 {
13912 for (tree *pc = &clauses; *pc; )
13913 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LINEAR)
13914 {
13915 error_at (OMP_CLAUSE_LOCATION (*pc),
13916 "%<linear%> clause may not be specified together "
13917 "with %<ordered%> clause with a parameter");
13918 *pc = OMP_CLAUSE_CHAIN (*pc);
13919 }
13920 else
13921 pc = &OMP_CLAUSE_CHAIN (*pc);
13922 }
13923
13924 gcc_assert (collapse >= 1 && ordered >= 0);
13925 count = ordered ? ordered : collapse;
13926
13927 declv = make_tree_vec (count);
13928 initv = make_tree_vec (count);
13929 condv = make_tree_vec (count);
13930 incrv = make_tree_vec (count);
13931
13932 if (code != CILK_FOR
13933 && !c_parser_next_token_is_keyword (parser, RID_FOR))
13934 {
13935 c_parser_error (parser, "for statement expected");
13936 return NULL;
13937 }
13938 if (code == CILK_FOR
13939 && !c_parser_next_token_is_keyword (parser, RID_CILK_FOR))
13940 {
13941 c_parser_error (parser, "_Cilk_for statement expected");
13942 return NULL;
13943 }
13944 for_loc = c_parser_peek_token (parser)->location;
13945 c_parser_consume_token (parser);
13946
13947 for (i = 0; i < count; i++)
13948 {
13949 int bracecount = 0;
13950
13951 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
13952 goto pop_scopes;
13953
13954 /* Parse the initialization declaration or expression. */
13955 if (c_parser_next_tokens_start_declaration (parser))
13956 {
13957 if (i > 0)
13958 vec_safe_push (for_block, c_begin_compound_stmt (true));
13959 this_pre_body = push_stmt_list ();
13960 c_parser_declaration_or_fndef (parser, true, true, true, true, true,
13961 NULL, vNULL);
13962 if (this_pre_body)
13963 {
13964 this_pre_body = pop_stmt_list (this_pre_body);
13965 if (pre_body)
13966 {
13967 tree t = pre_body;
13968 pre_body = push_stmt_list ();
13969 add_stmt (t);
13970 add_stmt (this_pre_body);
13971 pre_body = pop_stmt_list (pre_body);
13972 }
13973 else
13974 pre_body = this_pre_body;
13975 }
13976 decl = check_for_loop_decls (for_loc, flag_isoc99);
13977 if (decl == NULL)
13978 goto error_init;
13979 if (DECL_INITIAL (decl) == error_mark_node)
13980 decl = error_mark_node;
13981 init = decl;
13982 }
13983 else if (c_parser_next_token_is (parser, CPP_NAME)
13984 && c_parser_peek_2nd_token (parser)->type == CPP_EQ)
13985 {
13986 struct c_expr decl_exp;
13987 struct c_expr init_exp;
13988 location_t init_loc;
13989
13990 decl_exp = c_parser_postfix_expression (parser);
13991 decl = decl_exp.value;
13992
13993 c_parser_require (parser, CPP_EQ, "expected %<=%>");
13994
13995 init_loc = c_parser_peek_token (parser)->location;
13996 init_exp = c_parser_expr_no_commas (parser, NULL);
13997 init_exp = default_function_array_read_conversion (init_loc,
13998 init_exp);
13999 init = build_modify_expr (init_loc, decl, decl_exp.original_type,
14000 NOP_EXPR, init_loc, init_exp.value,
14001 init_exp.original_type);
14002 init = c_process_expr_stmt (init_loc, init);
14003
14004 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
14005 }
14006 else
14007 {
14008 error_init:
14009 c_parser_error (parser,
14010 "expected iteration declaration or initialization");
14011 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
14012 "expected %<)%>");
14013 fail = true;
14014 goto parse_next;
14015 }
14016
14017 /* Parse the loop condition. */
14018 cond = NULL_TREE;
14019 if (c_parser_next_token_is_not (parser, CPP_SEMICOLON))
14020 {
14021 location_t cond_loc = c_parser_peek_token (parser)->location;
14022 struct c_expr cond_expr
14023 = c_parser_binary_expression (parser, NULL, NULL_TREE);
14024
14025 cond = cond_expr.value;
14026 cond = c_objc_common_truthvalue_conversion (cond_loc, cond);
14027 cond = c_fully_fold (cond, false, NULL);
14028 switch (cond_expr.original_code)
14029 {
14030 case GT_EXPR:
14031 case GE_EXPR:
14032 case LT_EXPR:
14033 case LE_EXPR:
14034 break;
14035 case NE_EXPR:
14036 if (code == CILK_SIMD || code == CILK_FOR)
14037 break;
14038 /* FALLTHRU. */
14039 default:
14040 /* Can't be cond = error_mark_node, because we want to preserve
14041 the location until c_finish_omp_for. */
14042 cond = build1 (NOP_EXPR, boolean_type_node, error_mark_node);
14043 break;
14044 }
14045 protected_set_expr_location (cond, cond_loc);
14046 }
14047 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
14048
14049 /* Parse the increment expression. */
14050 incr = NULL_TREE;
14051 if (c_parser_next_token_is_not (parser, CPP_CLOSE_PAREN))
14052 {
14053 location_t incr_loc = c_parser_peek_token (parser)->location;
14054
14055 incr = c_process_expr_stmt (incr_loc,
14056 c_parser_expression (parser).value);
14057 }
14058 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
14059
14060 if (decl == NULL || decl == error_mark_node || init == error_mark_node)
14061 fail = true;
14062 else
14063 {
14064 TREE_VEC_ELT (declv, i) = decl;
14065 TREE_VEC_ELT (initv, i) = init;
14066 TREE_VEC_ELT (condv, i) = cond;
14067 TREE_VEC_ELT (incrv, i) = incr;
14068 }
14069
14070 parse_next:
14071 if (i == count - 1)
14072 break;
14073
14074 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
14075 in between the collapsed for loops to be still considered perfectly
14076 nested. Hopefully the final version clarifies this.
14077 For now handle (multiple) {'s and empty statements. */
14078 do
14079 {
14080 if (c_parser_next_token_is_keyword (parser, RID_FOR))
14081 {
14082 c_parser_consume_token (parser);
14083 break;
14084 }
14085 else if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
14086 {
14087 c_parser_consume_token (parser);
14088 bracecount++;
14089 }
14090 else if (bracecount
14091 && c_parser_next_token_is (parser, CPP_SEMICOLON))
14092 c_parser_consume_token (parser);
14093 else
14094 {
14095 c_parser_error (parser, "not enough perfectly nested loops");
14096 if (bracecount)
14097 {
14098 open_brace_parsed = true;
14099 bracecount--;
14100 }
14101 fail = true;
14102 count = 0;
14103 break;
14104 }
14105 }
14106 while (1);
14107
14108 nbraces += bracecount;
14109 }
14110
14111 save_break = c_break_label;
14112 if (code == CILK_SIMD)
14113 c_break_label = build_int_cst (size_type_node, 2);
14114 else
14115 c_break_label = size_one_node;
14116 save_cont = c_cont_label;
14117 c_cont_label = NULL_TREE;
14118 body = push_stmt_list ();
14119
14120 if (open_brace_parsed)
14121 {
14122 location_t here = c_parser_peek_token (parser)->location;
14123 stmt = c_begin_compound_stmt (true);
14124 c_parser_compound_statement_nostart (parser);
14125 add_stmt (c_end_compound_stmt (here, stmt, true));
14126 }
14127 else
14128 add_stmt (c_parser_c99_block_statement (parser));
14129 if (c_cont_label)
14130 {
14131 tree t = build1 (LABEL_EXPR, void_type_node, c_cont_label);
14132 SET_EXPR_LOCATION (t, loc);
14133 add_stmt (t);
14134 }
14135
14136 body = pop_stmt_list (body);
14137 c_break_label = save_break;
14138 c_cont_label = save_cont;
14139
14140 while (nbraces)
14141 {
14142 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
14143 {
14144 c_parser_consume_token (parser);
14145 nbraces--;
14146 }
14147 else if (c_parser_next_token_is (parser, CPP_SEMICOLON))
14148 c_parser_consume_token (parser);
14149 else
14150 {
14151 c_parser_error (parser, "collapsed loops not perfectly nested");
14152 while (nbraces)
14153 {
14154 location_t here = c_parser_peek_token (parser)->location;
14155 stmt = c_begin_compound_stmt (true);
14156 add_stmt (body);
14157 c_parser_compound_statement_nostart (parser);
14158 body = c_end_compound_stmt (here, stmt, true);
14159 nbraces--;
14160 }
14161 goto pop_scopes;
14162 }
14163 }
14164
14165 /* Only bother calling c_finish_omp_for if we haven't already generated
14166 an error from the initialization parsing. */
14167 if (!fail)
14168 {
14169 stmt = c_finish_omp_for (loc, code, declv, NULL, initv, condv,
14170 incrv, body, pre_body);
14171
14172 /* Check for iterators appearing in lb, b or incr expressions. */
14173 if (stmt && !c_omp_check_loop_iv (stmt, declv, NULL))
14174 stmt = NULL_TREE;
14175
14176 if (stmt)
14177 {
14178 add_stmt (stmt);
14179
14180 if (cclauses != NULL
14181 && cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL] != NULL)
14182 {
14183 tree *c;
14184 for (c = &cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL]; *c ; )
14185 if (OMP_CLAUSE_CODE (*c) != OMP_CLAUSE_FIRSTPRIVATE
14186 && OMP_CLAUSE_CODE (*c) != OMP_CLAUSE_LASTPRIVATE)
14187 c = &OMP_CLAUSE_CHAIN (*c);
14188 else
14189 {
14190 for (i = 0; i < count; i++)
14191 if (TREE_VEC_ELT (declv, i) == OMP_CLAUSE_DECL (*c))
14192 break;
14193 if (i == count)
14194 c = &OMP_CLAUSE_CHAIN (*c);
14195 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE)
14196 {
14197 error_at (loc,
14198 "iteration variable %qD should not be firstprivate",
14199 OMP_CLAUSE_DECL (*c));
14200 *c = OMP_CLAUSE_CHAIN (*c);
14201 }
14202 else
14203 {
14204 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
14205 tree l = *c;
14206 *c = OMP_CLAUSE_CHAIN (*c);
14207 if (code == OMP_SIMD)
14208 {
14209 OMP_CLAUSE_CHAIN (l)
14210 = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
14211 cclauses[C_OMP_CLAUSE_SPLIT_FOR] = l;
14212 }
14213 else
14214 {
14215 OMP_CLAUSE_CHAIN (l) = clauses;
14216 clauses = l;
14217 }
14218 }
14219 }
14220 }
14221 OMP_FOR_CLAUSES (stmt) = clauses;
14222 }
14223 ret = stmt;
14224 }
14225 pop_scopes:
14226 while (!for_block->is_empty ())
14227 {
14228 /* FIXME diagnostics: LOC below should be the actual location of
14229 this particular for block. We need to build a list of
14230 locations to go along with FOR_BLOCK. */
14231 stmt = c_end_compound_stmt (loc, for_block->pop (), true);
14232 add_stmt (stmt);
14233 }
14234 release_tree_vector (for_block);
14235 return ret;
14236 }
14237
14238 /* Helper function for OpenMP parsing, split clauses and call
14239 finish_omp_clauses on each of the set of clauses afterwards. */
14240
14241 static void
14242 omp_split_clauses (location_t loc, enum tree_code code,
14243 omp_clause_mask mask, tree clauses, tree *cclauses)
14244 {
14245 int i;
14246 c_omp_split_clauses (loc, code, mask, clauses, cclauses);
14247 for (i = 0; i < C_OMP_CLAUSE_SPLIT_COUNT; i++)
14248 if (cclauses[i])
14249 cclauses[i] = c_finish_omp_clauses (cclauses[i], true);
14250 }
14251
14252 /* OpenMP 4.0:
14253 #pragma omp simd simd-clause[optseq] new-line
14254 for-loop
14255
14256 LOC is the location of the #pragma token.
14257 */
14258
14259 #define OMP_SIMD_CLAUSE_MASK \
14260 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
14261 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
14262 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
14263 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
14264 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
14265 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
14266 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
14267 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
14268
14269 static tree
14270 c_parser_omp_simd (location_t loc, c_parser *parser,
14271 char *p_name, omp_clause_mask mask, tree *cclauses)
14272 {
14273 tree block, clauses, ret;
14274
14275 strcat (p_name, " simd");
14276 mask |= OMP_SIMD_CLAUSE_MASK;
14277
14278 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
14279 if (cclauses)
14280 {
14281 omp_split_clauses (loc, OMP_SIMD, mask, clauses, cclauses);
14282 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SIMD];
14283 tree c = find_omp_clause (cclauses[C_OMP_CLAUSE_SPLIT_FOR],
14284 OMP_CLAUSE_ORDERED);
14285 if (c && OMP_CLAUSE_ORDERED_EXPR (c))
14286 {
14287 error_at (OMP_CLAUSE_LOCATION (c),
14288 "%<ordered%> clause with parameter may not be specified "
14289 "on %qs construct", p_name);
14290 OMP_CLAUSE_ORDERED_EXPR (c) = NULL_TREE;
14291 }
14292 }
14293
14294 block = c_begin_compound_stmt (true);
14295 ret = c_parser_omp_for_loop (loc, parser, OMP_SIMD, clauses, cclauses);
14296 block = c_end_compound_stmt (loc, block, true);
14297 add_stmt (block);
14298
14299 return ret;
14300 }
14301
14302 /* OpenMP 2.5:
14303 #pragma omp for for-clause[optseq] new-line
14304 for-loop
14305
14306 OpenMP 4.0:
14307 #pragma omp for simd for-simd-clause[optseq] new-line
14308 for-loop
14309
14310 LOC is the location of the #pragma token.
14311 */
14312
14313 #define OMP_FOR_CLAUSE_MASK \
14314 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
14315 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
14316 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
14317 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
14318 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
14319 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
14320 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
14321 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
14322 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
14323
14324 static tree
14325 c_parser_omp_for (location_t loc, c_parser *parser,
14326 char *p_name, omp_clause_mask mask, tree *cclauses)
14327 {
14328 tree block, clauses, ret;
14329
14330 strcat (p_name, " for");
14331 mask |= OMP_FOR_CLAUSE_MASK;
14332 if (cclauses)
14333 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
14334 /* Composite distribute parallel for{, simd} disallows ordered clause. */
14335 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
14336 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED);
14337
14338 if (c_parser_next_token_is (parser, CPP_NAME))
14339 {
14340 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
14341
14342 if (strcmp (p, "simd") == 0)
14343 {
14344 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
14345 if (cclauses == NULL)
14346 cclauses = cclauses_buf;
14347
14348 c_parser_consume_token (parser);
14349 if (!flag_openmp) /* flag_openmp_simd */
14350 return c_parser_omp_simd (loc, parser, p_name, mask, cclauses);
14351 block = c_begin_compound_stmt (true);
14352 ret = c_parser_omp_simd (loc, parser, p_name, mask, cclauses);
14353 block = c_end_compound_stmt (loc, block, true);
14354 if (ret == NULL_TREE)
14355 return ret;
14356 ret = make_node (OMP_FOR);
14357 TREE_TYPE (ret) = void_type_node;
14358 OMP_FOR_BODY (ret) = block;
14359 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
14360 SET_EXPR_LOCATION (ret, loc);
14361 add_stmt (ret);
14362 return ret;
14363 }
14364 }
14365 if (!flag_openmp) /* flag_openmp_simd */
14366 {
14367 c_parser_skip_to_pragma_eol (parser, false);
14368 return NULL_TREE;
14369 }
14370
14371 /* Composite distribute parallel for disallows linear clause. */
14372 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
14373 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR);
14374
14375 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
14376 if (cclauses)
14377 {
14378 omp_split_clauses (loc, OMP_FOR, mask, clauses, cclauses);
14379 clauses = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
14380 }
14381
14382 block = c_begin_compound_stmt (true);
14383 ret = c_parser_omp_for_loop (loc, parser, OMP_FOR, clauses, cclauses);
14384 block = c_end_compound_stmt (loc, block, true);
14385 add_stmt (block);
14386
14387 return ret;
14388 }
14389
14390 /* OpenMP 2.5:
14391 # pragma omp master new-line
14392 structured-block
14393
14394 LOC is the location of the #pragma token.
14395 */
14396
14397 static tree
14398 c_parser_omp_master (location_t loc, c_parser *parser)
14399 {
14400 c_parser_skip_to_pragma_eol (parser);
14401 return c_finish_omp_master (loc, c_parser_omp_structured_block (parser));
14402 }
14403
14404 /* OpenMP 2.5:
14405 # pragma omp ordered new-line
14406 structured-block
14407
14408 OpenMP 4.5:
14409 # pragma omp ordered ordered-clauses new-line
14410 structured-block
14411
14412 # pragma omp ordered depend-clauses new-line */
14413
14414 #define OMP_ORDERED_CLAUSE_MASK \
14415 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREADS) \
14416 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMD))
14417
14418 #define OMP_ORDERED_DEPEND_CLAUSE_MASK \
14419 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
14420
14421 static bool
14422 c_parser_omp_ordered (c_parser *parser, enum pragma_context context)
14423 {
14424 location_t loc = c_parser_peek_token (parser)->location;
14425 c_parser_consume_pragma (parser);
14426
14427 if (context != pragma_stmt && context != pragma_compound)
14428 {
14429 c_parser_error (parser, "expected declaration specifiers");
14430 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
14431 return false;
14432 }
14433
14434 if (c_parser_next_token_is (parser, CPP_NAME))
14435 {
14436 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
14437
14438 if (!strcmp ("depend", p))
14439 {
14440 if (context == pragma_stmt)
14441 {
14442 error_at (loc,
14443 "%<#pragma omp ordered%> with %<depend> clause may "
14444 "only be used in compound statements");
14445 c_parser_skip_to_pragma_eol (parser);
14446 return false;
14447 }
14448
14449 tree clauses
14450 = c_parser_omp_all_clauses (parser,
14451 OMP_ORDERED_DEPEND_CLAUSE_MASK,
14452 "#pragma omp ordered");
14453 c_finish_omp_ordered (loc, clauses, NULL_TREE);
14454 return false;
14455 }
14456 }
14457
14458 tree clauses = c_parser_omp_all_clauses (parser, OMP_ORDERED_CLAUSE_MASK,
14459 "#pragma omp ordered");
14460 c_finish_omp_ordered (loc, clauses,
14461 c_parser_omp_structured_block (parser));
14462 return true;
14463 }
14464
14465 /* OpenMP 2.5:
14466
14467 section-scope:
14468 { section-sequence }
14469
14470 section-sequence:
14471 section-directive[opt] structured-block
14472 section-sequence section-directive structured-block
14473
14474 SECTIONS_LOC is the location of the #pragma omp sections. */
14475
14476 static tree
14477 c_parser_omp_sections_scope (location_t sections_loc, c_parser *parser)
14478 {
14479 tree stmt, substmt;
14480 bool error_suppress = false;
14481 location_t loc;
14482
14483 loc = c_parser_peek_token (parser)->location;
14484 if (!c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
14485 {
14486 /* Avoid skipping until the end of the block. */
14487 parser->error = false;
14488 return NULL_TREE;
14489 }
14490
14491 stmt = push_stmt_list ();
14492
14493 if (c_parser_peek_token (parser)->pragma_kind != PRAGMA_OMP_SECTION)
14494 {
14495 substmt = c_parser_omp_structured_block (parser);
14496 substmt = build1 (OMP_SECTION, void_type_node, substmt);
14497 SET_EXPR_LOCATION (substmt, loc);
14498 add_stmt (substmt);
14499 }
14500
14501 while (1)
14502 {
14503 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
14504 break;
14505 if (c_parser_next_token_is (parser, CPP_EOF))
14506 break;
14507
14508 loc = c_parser_peek_token (parser)->location;
14509 if (c_parser_peek_token (parser)->pragma_kind == PRAGMA_OMP_SECTION)
14510 {
14511 c_parser_consume_pragma (parser);
14512 c_parser_skip_to_pragma_eol (parser);
14513 error_suppress = false;
14514 }
14515 else if (!error_suppress)
14516 {
14517 error_at (loc, "expected %<#pragma omp section%> or %<}%>");
14518 error_suppress = true;
14519 }
14520
14521 substmt = c_parser_omp_structured_block (parser);
14522 substmt = build1 (OMP_SECTION, void_type_node, substmt);
14523 SET_EXPR_LOCATION (substmt, loc);
14524 add_stmt (substmt);
14525 }
14526 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE,
14527 "expected %<#pragma omp section%> or %<}%>");
14528
14529 substmt = pop_stmt_list (stmt);
14530
14531 stmt = make_node (OMP_SECTIONS);
14532 SET_EXPR_LOCATION (stmt, sections_loc);
14533 TREE_TYPE (stmt) = void_type_node;
14534 OMP_SECTIONS_BODY (stmt) = substmt;
14535
14536 return add_stmt (stmt);
14537 }
14538
14539 /* OpenMP 2.5:
14540 # pragma omp sections sections-clause[optseq] newline
14541 sections-scope
14542
14543 LOC is the location of the #pragma token.
14544 */
14545
14546 #define OMP_SECTIONS_CLAUSE_MASK \
14547 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
14548 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
14549 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
14550 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
14551 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
14552
14553 static tree
14554 c_parser_omp_sections (location_t loc, c_parser *parser,
14555 char *p_name, omp_clause_mask mask, tree *cclauses)
14556 {
14557 tree block, clauses, ret;
14558
14559 strcat (p_name, " sections");
14560 mask |= OMP_SECTIONS_CLAUSE_MASK;
14561 if (cclauses)
14562 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
14563
14564 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
14565 if (cclauses)
14566 {
14567 omp_split_clauses (loc, OMP_SECTIONS, mask, clauses, cclauses);
14568 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SECTIONS];
14569 }
14570
14571 block = c_begin_compound_stmt (true);
14572 ret = c_parser_omp_sections_scope (loc, parser);
14573 if (ret)
14574 OMP_SECTIONS_CLAUSES (ret) = clauses;
14575 block = c_end_compound_stmt (loc, block, true);
14576 add_stmt (block);
14577
14578 return ret;
14579 }
14580
14581 /* OpenMP 2.5:
14582 # pragma omp parallel parallel-clause[optseq] new-line
14583 structured-block
14584 # pragma omp parallel for parallel-for-clause[optseq] new-line
14585 structured-block
14586 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
14587 structured-block
14588
14589 OpenMP 4.0:
14590 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
14591 structured-block
14592
14593 LOC is the location of the #pragma token.
14594 */
14595
14596 #define OMP_PARALLEL_CLAUSE_MASK \
14597 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
14598 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
14599 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
14600 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
14601 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
14602 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
14603 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
14604 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
14605 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
14606
14607 static tree
14608 c_parser_omp_parallel (location_t loc, c_parser *parser,
14609 char *p_name, omp_clause_mask mask, tree *cclauses)
14610 {
14611 tree stmt, clauses, block;
14612
14613 strcat (p_name, " parallel");
14614 mask |= OMP_PARALLEL_CLAUSE_MASK;
14615 /* #pragma omp target parallel{, for, for simd} disallow copyin clause. */
14616 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) != 0
14617 && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) == 0)
14618 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN);
14619
14620 if (c_parser_next_token_is_keyword (parser, RID_FOR))
14621 {
14622 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
14623 if (cclauses == NULL)
14624 cclauses = cclauses_buf;
14625
14626 c_parser_consume_token (parser);
14627 if (!flag_openmp) /* flag_openmp_simd */
14628 return c_parser_omp_for (loc, parser, p_name, mask, cclauses);
14629 block = c_begin_omp_parallel ();
14630 tree ret = c_parser_omp_for (loc, parser, p_name, mask, cclauses);
14631 stmt
14632 = c_finish_omp_parallel (loc, cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
14633 block);
14634 if (ret == NULL_TREE)
14635 return ret;
14636 OMP_PARALLEL_COMBINED (stmt) = 1;
14637 return stmt;
14638 }
14639 /* When combined with distribute, parallel has to be followed by for.
14640 #pragma omp target parallel is allowed though. */
14641 else if (cclauses
14642 && (mask & (OMP_CLAUSE_MASK_1
14643 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
14644 {
14645 error_at (loc, "expected %<for%> after %qs", p_name);
14646 c_parser_skip_to_pragma_eol (parser);
14647 return NULL_TREE;
14648 }
14649 else if (!flag_openmp) /* flag_openmp_simd */
14650 {
14651 c_parser_skip_to_pragma_eol (parser, false);
14652 return NULL_TREE;
14653 }
14654 else if (cclauses == NULL && c_parser_next_token_is (parser, CPP_NAME))
14655 {
14656 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
14657 if (strcmp (p, "sections") == 0)
14658 {
14659 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
14660 if (cclauses == NULL)
14661 cclauses = cclauses_buf;
14662
14663 c_parser_consume_token (parser);
14664 block = c_begin_omp_parallel ();
14665 c_parser_omp_sections (loc, parser, p_name, mask, cclauses);
14666 stmt = c_finish_omp_parallel (loc,
14667 cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
14668 block);
14669 OMP_PARALLEL_COMBINED (stmt) = 1;
14670 return stmt;
14671 }
14672 }
14673
14674 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
14675 if (cclauses)
14676 {
14677 omp_split_clauses (loc, OMP_PARALLEL, mask, clauses, cclauses);
14678 clauses = cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL];
14679 }
14680
14681 block = c_begin_omp_parallel ();
14682 c_parser_statement (parser);
14683 stmt = c_finish_omp_parallel (loc, clauses, block);
14684
14685 return stmt;
14686 }
14687
14688 /* OpenMP 2.5:
14689 # pragma omp single single-clause[optseq] new-line
14690 structured-block
14691
14692 LOC is the location of the #pragma.
14693 */
14694
14695 #define OMP_SINGLE_CLAUSE_MASK \
14696 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
14697 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
14698 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
14699 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
14700
14701 static tree
14702 c_parser_omp_single (location_t loc, c_parser *parser)
14703 {
14704 tree stmt = make_node (OMP_SINGLE);
14705 SET_EXPR_LOCATION (stmt, loc);
14706 TREE_TYPE (stmt) = void_type_node;
14707
14708 OMP_SINGLE_CLAUSES (stmt)
14709 = c_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
14710 "#pragma omp single");
14711 OMP_SINGLE_BODY (stmt) = c_parser_omp_structured_block (parser);
14712
14713 return add_stmt (stmt);
14714 }
14715
14716 /* OpenMP 3.0:
14717 # pragma omp task task-clause[optseq] new-line
14718
14719 LOC is the location of the #pragma.
14720 */
14721
14722 #define OMP_TASK_CLAUSE_MASK \
14723 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
14724 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
14725 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
14726 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
14727 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
14728 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
14729 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
14730 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
14731 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
14732 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
14733
14734 static tree
14735 c_parser_omp_task (location_t loc, c_parser *parser)
14736 {
14737 tree clauses, block;
14738
14739 clauses = c_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
14740 "#pragma omp task");
14741
14742 block = c_begin_omp_task ();
14743 c_parser_statement (parser);
14744 return c_finish_omp_task (loc, clauses, block);
14745 }
14746
14747 /* OpenMP 3.0:
14748 # pragma omp taskwait new-line
14749 */
14750
14751 static void
14752 c_parser_omp_taskwait (c_parser *parser)
14753 {
14754 location_t loc = c_parser_peek_token (parser)->location;
14755 c_parser_consume_pragma (parser);
14756 c_parser_skip_to_pragma_eol (parser);
14757
14758 c_finish_omp_taskwait (loc);
14759 }
14760
14761 /* OpenMP 3.1:
14762 # pragma omp taskyield new-line
14763 */
14764
14765 static void
14766 c_parser_omp_taskyield (c_parser *parser)
14767 {
14768 location_t loc = c_parser_peek_token (parser)->location;
14769 c_parser_consume_pragma (parser);
14770 c_parser_skip_to_pragma_eol (parser);
14771
14772 c_finish_omp_taskyield (loc);
14773 }
14774
14775 /* OpenMP 4.0:
14776 # pragma omp taskgroup new-line
14777 */
14778
14779 static tree
14780 c_parser_omp_taskgroup (c_parser *parser)
14781 {
14782 location_t loc = c_parser_peek_token (parser)->location;
14783 c_parser_skip_to_pragma_eol (parser);
14784 return c_finish_omp_taskgroup (loc, c_parser_omp_structured_block (parser));
14785 }
14786
14787 /* OpenMP 4.0:
14788 # pragma omp cancel cancel-clause[optseq] new-line
14789
14790 LOC is the location of the #pragma.
14791 */
14792
14793 #define OMP_CANCEL_CLAUSE_MASK \
14794 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
14795 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
14796 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
14797 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
14798 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
14799
14800 static void
14801 c_parser_omp_cancel (c_parser *parser)
14802 {
14803 location_t loc = c_parser_peek_token (parser)->location;
14804
14805 c_parser_consume_pragma (parser);
14806 tree clauses = c_parser_omp_all_clauses (parser, OMP_CANCEL_CLAUSE_MASK,
14807 "#pragma omp cancel");
14808
14809 c_finish_omp_cancel (loc, clauses);
14810 }
14811
14812 /* OpenMP 4.0:
14813 # pragma omp cancellation point cancelpt-clause[optseq] new-line
14814
14815 LOC is the location of the #pragma.
14816 */
14817
14818 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
14819 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
14820 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
14821 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
14822 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
14823
14824 static void
14825 c_parser_omp_cancellation_point (c_parser *parser)
14826 {
14827 location_t loc = c_parser_peek_token (parser)->location;
14828 tree clauses;
14829 bool point_seen = false;
14830
14831 c_parser_consume_pragma (parser);
14832 if (c_parser_next_token_is (parser, CPP_NAME))
14833 {
14834 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
14835 if (strcmp (p, "point") == 0)
14836 {
14837 c_parser_consume_token (parser);
14838 point_seen = true;
14839 }
14840 }
14841 if (!point_seen)
14842 {
14843 c_parser_error (parser, "expected %<point%>");
14844 c_parser_skip_to_pragma_eol (parser);
14845 return;
14846 }
14847
14848 clauses
14849 = c_parser_omp_all_clauses (parser, OMP_CANCELLATION_POINT_CLAUSE_MASK,
14850 "#pragma omp cancellation point");
14851
14852 c_finish_omp_cancellation_point (loc, clauses);
14853 }
14854
14855 /* OpenMP 4.0:
14856 #pragma omp distribute distribute-clause[optseq] new-line
14857 for-loop */
14858
14859 #define OMP_DISTRIBUTE_CLAUSE_MASK \
14860 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
14861 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
14862 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
14863 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
14864 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
14865
14866 static tree
14867 c_parser_omp_distribute (location_t loc, c_parser *parser,
14868 char *p_name, omp_clause_mask mask, tree *cclauses)
14869 {
14870 tree clauses, block, ret;
14871
14872 strcat (p_name, " distribute");
14873 mask |= OMP_DISTRIBUTE_CLAUSE_MASK;
14874
14875 if (c_parser_next_token_is (parser, CPP_NAME))
14876 {
14877 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
14878 bool simd = false;
14879 bool parallel = false;
14880
14881 if (strcmp (p, "simd") == 0)
14882 simd = true;
14883 else
14884 parallel = strcmp (p, "parallel") == 0;
14885 if (parallel || simd)
14886 {
14887 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
14888 if (cclauses == NULL)
14889 cclauses = cclauses_buf;
14890 c_parser_consume_token (parser);
14891 if (!flag_openmp) /* flag_openmp_simd */
14892 {
14893 if (simd)
14894 return c_parser_omp_simd (loc, parser, p_name, mask, cclauses);
14895 else
14896 return c_parser_omp_parallel (loc, parser, p_name, mask,
14897 cclauses);
14898 }
14899 block = c_begin_compound_stmt (true);
14900 if (simd)
14901 ret = c_parser_omp_simd (loc, parser, p_name, mask, cclauses);
14902 else
14903 ret = c_parser_omp_parallel (loc, parser, p_name, mask, cclauses);
14904 block = c_end_compound_stmt (loc, block, true);
14905 if (ret == NULL)
14906 return ret;
14907 ret = make_node (OMP_DISTRIBUTE);
14908 TREE_TYPE (ret) = void_type_node;
14909 OMP_FOR_BODY (ret) = block;
14910 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
14911 SET_EXPR_LOCATION (ret, loc);
14912 add_stmt (ret);
14913 return ret;
14914 }
14915 }
14916 if (!flag_openmp) /* flag_openmp_simd */
14917 {
14918 c_parser_skip_to_pragma_eol (parser, false);
14919 return NULL_TREE;
14920 }
14921
14922 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
14923 if (cclauses)
14924 {
14925 omp_split_clauses (loc, OMP_DISTRIBUTE, mask, clauses, cclauses);
14926 clauses = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
14927 }
14928
14929 block = c_begin_compound_stmt (true);
14930 ret = c_parser_omp_for_loop (loc, parser, OMP_DISTRIBUTE, clauses, NULL);
14931 block = c_end_compound_stmt (loc, block, true);
14932 add_stmt (block);
14933
14934 return ret;
14935 }
14936
14937 /* OpenMP 4.0:
14938 # pragma omp teams teams-clause[optseq] new-line
14939 structured-block */
14940
14941 #define OMP_TEAMS_CLAUSE_MASK \
14942 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
14943 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
14944 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
14945 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
14946 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
14947 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
14948 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
14949
14950 static tree
14951 c_parser_omp_teams (location_t loc, c_parser *parser,
14952 char *p_name, omp_clause_mask mask, tree *cclauses)
14953 {
14954 tree clauses, block, ret;
14955
14956 strcat (p_name, " teams");
14957 mask |= OMP_TEAMS_CLAUSE_MASK;
14958
14959 if (c_parser_next_token_is (parser, CPP_NAME))
14960 {
14961 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
14962 if (strcmp (p, "distribute") == 0)
14963 {
14964 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
14965 if (cclauses == NULL)
14966 cclauses = cclauses_buf;
14967
14968 c_parser_consume_token (parser);
14969 if (!flag_openmp) /* flag_openmp_simd */
14970 return c_parser_omp_distribute (loc, parser, p_name, mask, cclauses);
14971 block = c_begin_compound_stmt (true);
14972 ret = c_parser_omp_distribute (loc, parser, p_name, mask, cclauses);
14973 block = c_end_compound_stmt (loc, block, true);
14974 if (ret == NULL)
14975 return ret;
14976 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
14977 ret = make_node (OMP_TEAMS);
14978 TREE_TYPE (ret) = void_type_node;
14979 OMP_TEAMS_CLAUSES (ret) = clauses;
14980 OMP_TEAMS_BODY (ret) = block;
14981 OMP_TEAMS_COMBINED (ret) = 1;
14982 return add_stmt (ret);
14983 }
14984 }
14985 if (!flag_openmp) /* flag_openmp_simd */
14986 {
14987 c_parser_skip_to_pragma_eol (parser, false);
14988 return NULL_TREE;
14989 }
14990
14991 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
14992 if (cclauses)
14993 {
14994 omp_split_clauses (loc, OMP_TEAMS, mask, clauses, cclauses);
14995 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
14996 }
14997
14998 tree stmt = make_node (OMP_TEAMS);
14999 TREE_TYPE (stmt) = void_type_node;
15000 OMP_TEAMS_CLAUSES (stmt) = clauses;
15001 OMP_TEAMS_BODY (stmt) = c_parser_omp_structured_block (parser);
15002
15003 return add_stmt (stmt);
15004 }
15005
15006 /* OpenMP 4.0:
15007 # pragma omp target data target-data-clause[optseq] new-line
15008 structured-block */
15009
15010 #define OMP_TARGET_DATA_CLAUSE_MASK \
15011 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
15012 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
15013 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
15014 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR))
15015
15016 static tree
15017 c_parser_omp_target_data (location_t loc, c_parser *parser)
15018 {
15019 tree clauses
15020 = c_parser_omp_all_clauses (parser, OMP_TARGET_DATA_CLAUSE_MASK,
15021 "#pragma omp target data");
15022 int map_seen = 0;
15023 for (tree *pc = &clauses; *pc;)
15024 {
15025 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
15026 switch (OMP_CLAUSE_MAP_KIND (*pc))
15027 {
15028 case GOMP_MAP_TO:
15029 case GOMP_MAP_ALWAYS_TO:
15030 case GOMP_MAP_FROM:
15031 case GOMP_MAP_ALWAYS_FROM:
15032 case GOMP_MAP_TOFROM:
15033 case GOMP_MAP_ALWAYS_TOFROM:
15034 case GOMP_MAP_ALLOC:
15035 map_seen = 3;
15036 break;
15037 case GOMP_MAP_FIRSTPRIVATE_POINTER:
15038 case GOMP_MAP_ALWAYS_POINTER:
15039 break;
15040 default:
15041 map_seen |= 1;
15042 error_at (OMP_CLAUSE_LOCATION (*pc),
15043 "%<#pragma omp target data%> with map-type other "
15044 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
15045 "on %<map%> clause");
15046 *pc = OMP_CLAUSE_CHAIN (*pc);
15047 continue;
15048 }
15049 pc = &OMP_CLAUSE_CHAIN (*pc);
15050 }
15051
15052 if (map_seen != 3)
15053 {
15054 if (map_seen == 0)
15055 error_at (loc,
15056 "%<#pragma omp target data%> must contain at least "
15057 "one %<map%> clause");
15058 return NULL_TREE;
15059 }
15060
15061 tree stmt = make_node (OMP_TARGET_DATA);
15062 TREE_TYPE (stmt) = void_type_node;
15063 OMP_TARGET_DATA_CLAUSES (stmt) = clauses;
15064 keep_next_level ();
15065 tree block = c_begin_compound_stmt (true);
15066 add_stmt (c_parser_omp_structured_block (parser));
15067 OMP_TARGET_DATA_BODY (stmt) = c_end_compound_stmt (loc, block, true);
15068
15069 SET_EXPR_LOCATION (stmt, loc);
15070 return add_stmt (stmt);
15071 }
15072
15073 /* OpenMP 4.0:
15074 # pragma omp target update target-update-clause[optseq] new-line */
15075
15076 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
15077 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
15078 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
15079 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
15080 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
15081 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
15082 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
15083
15084 static bool
15085 c_parser_omp_target_update (location_t loc, c_parser *parser,
15086 enum pragma_context context)
15087 {
15088 if (context == pragma_stmt)
15089 {
15090 error_at (loc,
15091 "%<#pragma omp target update%> may only be "
15092 "used in compound statements");
15093 c_parser_skip_to_pragma_eol (parser);
15094 return false;
15095 }
15096
15097 tree clauses
15098 = c_parser_omp_all_clauses (parser, OMP_TARGET_UPDATE_CLAUSE_MASK,
15099 "#pragma omp target update");
15100 if (find_omp_clause (clauses, OMP_CLAUSE_TO) == NULL_TREE
15101 && find_omp_clause (clauses, OMP_CLAUSE_FROM) == NULL_TREE)
15102 {
15103 error_at (loc,
15104 "%<#pragma omp target update%> must contain at least one "
15105 "%<from%> or %<to%> clauses");
15106 return false;
15107 }
15108
15109 tree stmt = make_node (OMP_TARGET_UPDATE);
15110 TREE_TYPE (stmt) = void_type_node;
15111 OMP_TARGET_UPDATE_CLAUSES (stmt) = clauses;
15112 SET_EXPR_LOCATION (stmt, loc);
15113 add_stmt (stmt);
15114 return false;
15115 }
15116
15117 /* OpenMP 4.5:
15118 # pragma omp target enter data target-data-clause[optseq] new-line */
15119
15120 #define OMP_TARGET_ENTER_DATA_CLAUSE_MASK \
15121 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
15122 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
15123 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
15124 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
15125 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
15126
15127 static tree
15128 c_parser_omp_target_enter_data (location_t loc, c_parser *parser,
15129 enum pragma_context context)
15130 {
15131 bool data_seen = false;
15132 if (c_parser_next_token_is (parser, CPP_NAME))
15133 {
15134 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
15135 if (strcmp (p, "data") == 0)
15136 {
15137 c_parser_consume_token (parser);
15138 data_seen = true;
15139 }
15140 }
15141 if (!data_seen)
15142 {
15143 c_parser_error (parser, "expected %<data%>");
15144 c_parser_skip_to_pragma_eol (parser);
15145 return NULL_TREE;
15146 }
15147
15148 if (context == pragma_stmt)
15149 {
15150 error_at (loc,
15151 "%<#pragma omp target enter data%> may only be "
15152 "used in compound statements");
15153 c_parser_skip_to_pragma_eol (parser);
15154 return NULL_TREE;
15155 }
15156
15157 tree clauses
15158 = c_parser_omp_all_clauses (parser, OMP_TARGET_ENTER_DATA_CLAUSE_MASK,
15159 "#pragma omp target enter data");
15160 int map_seen = 0;
15161 for (tree *pc = &clauses; *pc;)
15162 {
15163 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
15164 switch (OMP_CLAUSE_MAP_KIND (*pc))
15165 {
15166 case GOMP_MAP_TO:
15167 case GOMP_MAP_ALWAYS_TO:
15168 case GOMP_MAP_ALLOC:
15169 map_seen = 3;
15170 break;
15171 case GOMP_MAP_FIRSTPRIVATE_POINTER:
15172 case GOMP_MAP_ALWAYS_POINTER:
15173 break;
15174 default:
15175 map_seen |= 1;
15176 error_at (OMP_CLAUSE_LOCATION (*pc),
15177 "%<#pragma omp target enter data%> with map-type other "
15178 "than %<to%> or %<alloc%> on %<map%> clause");
15179 *pc = OMP_CLAUSE_CHAIN (*pc);
15180 continue;
15181 }
15182 pc = &OMP_CLAUSE_CHAIN (*pc);
15183 }
15184
15185 if (map_seen != 3)
15186 {
15187 if (map_seen == 0)
15188 error_at (loc,
15189 "%<#pragma omp target enter data%> must contain at least "
15190 "one %<map%> clause");
15191 return NULL_TREE;
15192 }
15193
15194 tree stmt = make_node (OMP_TARGET_ENTER_DATA);
15195 TREE_TYPE (stmt) = void_type_node;
15196 OMP_TARGET_ENTER_DATA_CLAUSES (stmt) = clauses;
15197 SET_EXPR_LOCATION (stmt, loc);
15198 add_stmt (stmt);
15199 return stmt;
15200 }
15201
15202 /* OpenMP 4.5:
15203 # pragma omp target exit data target-data-clause[optseq] new-line */
15204
15205 #define OMP_TARGET_EXIT_DATA_CLAUSE_MASK \
15206 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
15207 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
15208 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
15209 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
15210 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
15211
15212 static tree
15213 c_parser_omp_target_exit_data (location_t loc, c_parser *parser,
15214 enum pragma_context context)
15215 {
15216 bool data_seen = false;
15217 if (c_parser_next_token_is (parser, CPP_NAME))
15218 {
15219 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
15220 if (strcmp (p, "data") == 0)
15221 {
15222 c_parser_consume_token (parser);
15223 data_seen = true;
15224 }
15225 }
15226 if (!data_seen)
15227 {
15228 c_parser_error (parser, "expected %<data%>");
15229 c_parser_skip_to_pragma_eol (parser);
15230 return NULL_TREE;
15231 }
15232
15233 if (context == pragma_stmt)
15234 {
15235 error_at (loc,
15236 "%<#pragma omp target exit data%> may only be "
15237 "used in compound statements");
15238 c_parser_skip_to_pragma_eol (parser);
15239 return NULL_TREE;
15240 }
15241
15242 tree clauses
15243 = c_parser_omp_all_clauses (parser, OMP_TARGET_EXIT_DATA_CLAUSE_MASK,
15244 "#pragma omp target exit data");
15245
15246 int map_seen = 0;
15247 for (tree *pc = &clauses; *pc;)
15248 {
15249 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
15250 switch (OMP_CLAUSE_MAP_KIND (*pc))
15251 {
15252 case GOMP_MAP_FROM:
15253 case GOMP_MAP_ALWAYS_FROM:
15254 case GOMP_MAP_RELEASE:
15255 case GOMP_MAP_DELETE:
15256 map_seen = 3;
15257 break;
15258 case GOMP_MAP_FIRSTPRIVATE_POINTER:
15259 case GOMP_MAP_ALWAYS_POINTER:
15260 break;
15261 default:
15262 map_seen |= 1;
15263 error_at (OMP_CLAUSE_LOCATION (*pc),
15264 "%<#pragma omp target exit data%> with map-type other "
15265 "than %<from%>, %<release> or %<delete%> on %<map%>"
15266 " clause");
15267 *pc = OMP_CLAUSE_CHAIN (*pc);
15268 continue;
15269 }
15270 pc = &OMP_CLAUSE_CHAIN (*pc);
15271 }
15272
15273 if (map_seen != 3)
15274 {
15275 if (map_seen == 0)
15276 error_at (loc,
15277 "%<#pragma omp target exit data%> must contain at least one "
15278 "%<map%> clause");
15279 return NULL_TREE;
15280 }
15281
15282 tree stmt = make_node (OMP_TARGET_EXIT_DATA);
15283 TREE_TYPE (stmt) = void_type_node;
15284 OMP_TARGET_EXIT_DATA_CLAUSES (stmt) = clauses;
15285 SET_EXPR_LOCATION (stmt, loc);
15286 add_stmt (stmt);
15287 return stmt;
15288 }
15289
15290 /* OpenMP 4.0:
15291 # pragma omp target target-clause[optseq] new-line
15292 structured-block */
15293
15294 #define OMP_TARGET_CLAUSE_MASK \
15295 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
15296 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
15297 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
15298 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
15299 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
15300 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
15301 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
15302 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULTMAP) \
15303 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR))
15304
15305 static bool
15306 c_parser_omp_target (c_parser *parser, enum pragma_context context)
15307 {
15308 location_t loc = c_parser_peek_token (parser)->location;
15309 c_parser_consume_pragma (parser);
15310 tree *pc = NULL, stmt, block;
15311
15312 if (context != pragma_stmt && context != pragma_compound)
15313 {
15314 c_parser_error (parser, "expected declaration specifiers");
15315 c_parser_skip_to_pragma_eol (parser);
15316 return false;
15317 }
15318
15319 if (c_parser_next_token_is (parser, CPP_NAME))
15320 {
15321 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
15322 enum tree_code ccode = ERROR_MARK;
15323
15324 if (strcmp (p, "teams") == 0)
15325 ccode = OMP_TEAMS;
15326 else if (strcmp (p, "parallel") == 0)
15327 ccode = OMP_PARALLEL;
15328 else if (strcmp (p, "simd") == 0)
15329 ccode = OMP_SIMD;
15330 if (ccode != ERROR_MARK)
15331 {
15332 tree cclauses[C_OMP_CLAUSE_SPLIT_COUNT];
15333 char p_name[sizeof ("#pragma omp target teams distribute "
15334 "parallel for simd")];
15335
15336 c_parser_consume_token (parser);
15337 strcpy (p_name, "#pragma omp target");
15338 if (!flag_openmp) /* flag_openmp_simd */
15339 {
15340 tree stmt;
15341 switch (ccode)
15342 {
15343 case OMP_TEAMS:
15344 stmt = c_parser_omp_teams (loc, parser, p_name,
15345 OMP_TARGET_CLAUSE_MASK,
15346 cclauses);
15347 break;
15348 case OMP_PARALLEL:
15349 stmt = c_parser_omp_parallel (loc, parser, p_name,
15350 OMP_TARGET_CLAUSE_MASK,
15351 cclauses);
15352 break;
15353 case OMP_SIMD:
15354 stmt = c_parser_omp_simd (loc, parser, p_name,
15355 OMP_TARGET_CLAUSE_MASK,
15356 cclauses);
15357 break;
15358 default:
15359 gcc_unreachable ();
15360 }
15361 return stmt != NULL_TREE;
15362 }
15363 keep_next_level ();
15364 tree block = c_begin_compound_stmt (true), ret;
15365 switch (ccode)
15366 {
15367 case OMP_TEAMS:
15368 ret = c_parser_omp_teams (loc, parser, p_name,
15369 OMP_TARGET_CLAUSE_MASK, cclauses);
15370 break;
15371 case OMP_PARALLEL:
15372 ret = c_parser_omp_parallel (loc, parser, p_name,
15373 OMP_TARGET_CLAUSE_MASK, cclauses);
15374 break;
15375 case OMP_SIMD:
15376 ret = c_parser_omp_simd (loc, parser, p_name,
15377 OMP_TARGET_CLAUSE_MASK, cclauses);
15378 break;
15379 default:
15380 gcc_unreachable ();
15381 }
15382 block = c_end_compound_stmt (loc, block, true);
15383 if (ret == NULL_TREE)
15384 return false;
15385 if (ccode == OMP_TEAMS)
15386 {
15387 /* For combined target teams, ensure the num_teams and
15388 thread_limit clause expressions are evaluated on the host,
15389 before entering the target construct. */
15390 tree c;
15391 for (c = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
15392 c; c = OMP_CLAUSE_CHAIN (c))
15393 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
15394 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
15395 && TREE_CODE (OMP_CLAUSE_OPERAND (c, 0)) != INTEGER_CST)
15396 {
15397 tree expr = OMP_CLAUSE_OPERAND (c, 0);
15398 tree tmp = create_tmp_var_raw (TREE_TYPE (expr));
15399 expr = build4 (TARGET_EXPR, TREE_TYPE (expr), tmp,
15400 expr, NULL_TREE, NULL_TREE);
15401 add_stmt (expr);
15402 OMP_CLAUSE_OPERAND (c, 0) = expr;
15403 tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
15404 OMP_CLAUSE_FIRSTPRIVATE);
15405 OMP_CLAUSE_DECL (tc) = tmp;
15406 OMP_CLAUSE_CHAIN (tc)
15407 = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
15408 cclauses[C_OMP_CLAUSE_SPLIT_TARGET] = tc;
15409 }
15410 }
15411 tree stmt = make_node (OMP_TARGET);
15412 TREE_TYPE (stmt) = void_type_node;
15413 OMP_TARGET_CLAUSES (stmt) = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
15414 OMP_TARGET_BODY (stmt) = block;
15415 OMP_TARGET_COMBINED (stmt) = 1;
15416 add_stmt (stmt);
15417 pc = &OMP_TARGET_CLAUSES (stmt);
15418 goto check_clauses;
15419 }
15420 else if (!flag_openmp) /* flag_openmp_simd */
15421 {
15422 c_parser_skip_to_pragma_eol (parser, false);
15423 return false;
15424 }
15425 else if (strcmp (p, "data") == 0)
15426 {
15427 c_parser_consume_token (parser);
15428 c_parser_omp_target_data (loc, parser);
15429 return true;
15430 }
15431 else if (strcmp (p, "enter") == 0)
15432 {
15433 c_parser_consume_token (parser);
15434 c_parser_omp_target_enter_data (loc, parser, context);
15435 return false;
15436 }
15437 else if (strcmp (p, "exit") == 0)
15438 {
15439 c_parser_consume_token (parser);
15440 c_parser_omp_target_exit_data (loc, parser, context);
15441 return false;
15442 }
15443 else if (strcmp (p, "update") == 0)
15444 {
15445 c_parser_consume_token (parser);
15446 return c_parser_omp_target_update (loc, parser, context);
15447 }
15448 }
15449
15450 stmt = make_node (OMP_TARGET);
15451 TREE_TYPE (stmt) = void_type_node;
15452
15453 OMP_TARGET_CLAUSES (stmt)
15454 = c_parser_omp_all_clauses (parser, OMP_TARGET_CLAUSE_MASK,
15455 "#pragma omp target");
15456 pc = &OMP_TARGET_CLAUSES (stmt);
15457 keep_next_level ();
15458 block = c_begin_compound_stmt (true);
15459 add_stmt (c_parser_omp_structured_block (parser));
15460 OMP_TARGET_BODY (stmt) = c_end_compound_stmt (loc, block, true);
15461
15462 SET_EXPR_LOCATION (stmt, loc);
15463 add_stmt (stmt);
15464
15465 check_clauses:
15466 while (*pc)
15467 {
15468 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
15469 switch (OMP_CLAUSE_MAP_KIND (*pc))
15470 {
15471 case GOMP_MAP_TO:
15472 case GOMP_MAP_ALWAYS_TO:
15473 case GOMP_MAP_FROM:
15474 case GOMP_MAP_ALWAYS_FROM:
15475 case GOMP_MAP_TOFROM:
15476 case GOMP_MAP_ALWAYS_TOFROM:
15477 case GOMP_MAP_ALLOC:
15478 case GOMP_MAP_FIRSTPRIVATE_POINTER:
15479 case GOMP_MAP_ALWAYS_POINTER:
15480 break;
15481 default:
15482 error_at (OMP_CLAUSE_LOCATION (*pc),
15483 "%<#pragma omp target%> with map-type other "
15484 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
15485 "on %<map%> clause");
15486 *pc = OMP_CLAUSE_CHAIN (*pc);
15487 continue;
15488 }
15489 pc = &OMP_CLAUSE_CHAIN (*pc);
15490 }
15491 return true;
15492 }
15493
15494 /* OpenMP 4.0:
15495 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
15496
15497 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
15498 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
15499 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
15500 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
15501 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
15502 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
15503 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
15504
15505 static void
15506 c_parser_omp_declare_simd (c_parser *parser, enum pragma_context context)
15507 {
15508 vec<c_token> clauses = vNULL;
15509 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
15510 {
15511 c_token *token = c_parser_peek_token (parser);
15512 if (token->type == CPP_EOF)
15513 {
15514 c_parser_skip_to_pragma_eol (parser);
15515 clauses.release ();
15516 return;
15517 }
15518 clauses.safe_push (*token);
15519 c_parser_consume_token (parser);
15520 }
15521 clauses.safe_push (*c_parser_peek_token (parser));
15522 c_parser_skip_to_pragma_eol (parser);
15523
15524 while (c_parser_next_token_is (parser, CPP_PRAGMA))
15525 {
15526 if (c_parser_peek_token (parser)->pragma_kind
15527 != PRAGMA_OMP_DECLARE_REDUCTION
15528 || c_parser_peek_2nd_token (parser)->type != CPP_NAME
15529 || strcmp (IDENTIFIER_POINTER
15530 (c_parser_peek_2nd_token (parser)->value),
15531 "simd") != 0)
15532 {
15533 c_parser_error (parser,
15534 "%<#pragma omp declare simd%> must be followed by "
15535 "function declaration or definition or another "
15536 "%<#pragma omp declare simd%>");
15537 clauses.release ();
15538 return;
15539 }
15540 c_parser_consume_pragma (parser);
15541 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
15542 {
15543 c_token *token = c_parser_peek_token (parser);
15544 if (token->type == CPP_EOF)
15545 {
15546 c_parser_skip_to_pragma_eol (parser);
15547 clauses.release ();
15548 return;
15549 }
15550 clauses.safe_push (*token);
15551 c_parser_consume_token (parser);
15552 }
15553 clauses.safe_push (*c_parser_peek_token (parser));
15554 c_parser_skip_to_pragma_eol (parser);
15555 }
15556
15557 /* Make sure nothing tries to read past the end of the tokens. */
15558 c_token eof_token;
15559 memset (&eof_token, 0, sizeof (eof_token));
15560 eof_token.type = CPP_EOF;
15561 clauses.safe_push (eof_token);
15562 clauses.safe_push (eof_token);
15563
15564 switch (context)
15565 {
15566 case pragma_external:
15567 if (c_parser_next_token_is (parser, CPP_KEYWORD)
15568 && c_parser_peek_token (parser)->keyword == RID_EXTENSION)
15569 {
15570 int ext = disable_extension_diagnostics ();
15571 do
15572 c_parser_consume_token (parser);
15573 while (c_parser_next_token_is (parser, CPP_KEYWORD)
15574 && c_parser_peek_token (parser)->keyword == RID_EXTENSION);
15575 c_parser_declaration_or_fndef (parser, true, true, true, false, true,
15576 NULL, clauses);
15577 restore_extension_diagnostics (ext);
15578 }
15579 else
15580 c_parser_declaration_or_fndef (parser, true, true, true, false, true,
15581 NULL, clauses);
15582 break;
15583 case pragma_struct:
15584 case pragma_param:
15585 c_parser_error (parser, "%<#pragma omp declare simd%> must be followed by "
15586 "function declaration or definition");
15587 break;
15588 case pragma_compound:
15589 case pragma_stmt:
15590 if (c_parser_next_token_is (parser, CPP_KEYWORD)
15591 && c_parser_peek_token (parser)->keyword == RID_EXTENSION)
15592 {
15593 int ext = disable_extension_diagnostics ();
15594 do
15595 c_parser_consume_token (parser);
15596 while (c_parser_next_token_is (parser, CPP_KEYWORD)
15597 && c_parser_peek_token (parser)->keyword == RID_EXTENSION);
15598 if (c_parser_next_tokens_start_declaration (parser))
15599 {
15600 c_parser_declaration_or_fndef (parser, true, true, true, true,
15601 true, NULL, clauses);
15602 restore_extension_diagnostics (ext);
15603 break;
15604 }
15605 restore_extension_diagnostics (ext);
15606 }
15607 else if (c_parser_next_tokens_start_declaration (parser))
15608 {
15609 c_parser_declaration_or_fndef (parser, true, true, true, true, true,
15610 NULL, clauses);
15611 break;
15612 }
15613 c_parser_error (parser, "%<#pragma omp declare simd%> must be followed by "
15614 "function declaration or definition");
15615 break;
15616 default:
15617 gcc_unreachable ();
15618 }
15619 clauses.release ();
15620 }
15621
15622 /* Finalize #pragma omp declare simd clauses after FNDECL has been parsed,
15623 and put that into "omp declare simd" attribute. */
15624
15625 static void
15626 c_finish_omp_declare_simd (c_parser *parser, tree fndecl, tree parms,
15627 vec<c_token> clauses)
15628 {
15629 if (flag_cilkplus
15630 && clauses.exists () && !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
15631 {
15632 error ("%<#pragma omp declare simd%> cannot be used in the same "
15633 "function marked as a Cilk Plus SIMD-enabled function");
15634 vec_free (parser->cilk_simd_fn_tokens);
15635 return;
15636 }
15637
15638 /* Normally first token is CPP_NAME "simd". CPP_EOF there indicates
15639 error has been reported and CPP_PRAGMA that c_finish_omp_declare_simd
15640 has already processed the tokens. */
15641 if (clauses.exists () && clauses[0].type == CPP_EOF)
15642 return;
15643 if (fndecl == NULL_TREE || TREE_CODE (fndecl) != FUNCTION_DECL)
15644 {
15645 error ("%<#pragma omp declare simd%> not immediately followed by "
15646 "a function declaration or definition");
15647 clauses[0].type = CPP_EOF;
15648 return;
15649 }
15650 if (clauses.exists () && clauses[0].type != CPP_NAME)
15651 {
15652 error_at (DECL_SOURCE_LOCATION (fndecl),
15653 "%<#pragma omp declare simd%> not immediately followed by "
15654 "a single function declaration or definition");
15655 clauses[0].type = CPP_EOF;
15656 return;
15657 }
15658
15659 if (parms == NULL_TREE)
15660 parms = DECL_ARGUMENTS (fndecl);
15661
15662 unsigned int tokens_avail = parser->tokens_avail;
15663 gcc_assert (parser->tokens == &parser->tokens_buf[0]);
15664 bool is_cilkplus_cilk_simd_fn = false;
15665
15666 if (flag_cilkplus && !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
15667 {
15668 parser->tokens = parser->cilk_simd_fn_tokens->address ();
15669 parser->tokens_avail = vec_safe_length (parser->cilk_simd_fn_tokens);
15670 is_cilkplus_cilk_simd_fn = true;
15671 }
15672 else
15673 {
15674 parser->tokens = clauses.address ();
15675 parser->tokens_avail = clauses.length ();
15676 }
15677
15678 /* c_parser_omp_declare_simd pushed 2 extra CPP_EOF tokens at the end. */
15679 while (parser->tokens_avail > 3)
15680 {
15681 c_token *token = c_parser_peek_token (parser);
15682 if (!is_cilkplus_cilk_simd_fn)
15683 gcc_assert (token->type == CPP_NAME
15684 && strcmp (IDENTIFIER_POINTER (token->value), "simd") == 0);
15685 else
15686 gcc_assert (token->type == CPP_NAME
15687 && is_cilkplus_vector_p (token->value));
15688 c_parser_consume_token (parser);
15689 parser->in_pragma = true;
15690
15691 tree c = NULL_TREE;
15692 if (is_cilkplus_cilk_simd_fn)
15693 c = c_parser_omp_all_clauses (parser, CILK_SIMD_FN_CLAUSE_MASK,
15694 "SIMD-enabled functions attribute");
15695 else
15696 c = c_parser_omp_all_clauses (parser, OMP_DECLARE_SIMD_CLAUSE_MASK,
15697 "#pragma omp declare simd");
15698 c = c_omp_declare_simd_clauses_to_numbers (parms, c);
15699 if (c != NULL_TREE)
15700 c = tree_cons (NULL_TREE, c, NULL_TREE);
15701 if (is_cilkplus_cilk_simd_fn)
15702 {
15703 tree k = build_tree_list (get_identifier ("cilk simd function"),
15704 NULL_TREE);
15705 TREE_CHAIN (k) = DECL_ATTRIBUTES (fndecl);
15706 DECL_ATTRIBUTES (fndecl) = k;
15707 }
15708 c = build_tree_list (get_identifier ("omp declare simd"), c);
15709 TREE_CHAIN (c) = DECL_ATTRIBUTES (fndecl);
15710 DECL_ATTRIBUTES (fndecl) = c;
15711 }
15712
15713 parser->tokens = &parser->tokens_buf[0];
15714 parser->tokens_avail = tokens_avail;
15715 if (clauses.exists ())
15716 clauses[0].type = CPP_PRAGMA;
15717
15718 if (!vec_safe_is_empty (parser->cilk_simd_fn_tokens))
15719 vec_free (parser->cilk_simd_fn_tokens);
15720 }
15721
15722
15723 /* OpenMP 4.0:
15724 # pragma omp declare target new-line
15725 declarations and definitions
15726 # pragma omp end declare target new-line
15727
15728 OpenMP 4.5:
15729 # pragma omp declare target ( extended-list ) new-line
15730
15731 # pragma omp declare target declare-target-clauses[seq] new-line */
15732
15733 #define OMP_DECLARE_TARGET_CLAUSE_MASK \
15734 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
15735 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK))
15736
15737 static void
15738 c_parser_omp_declare_target (c_parser *parser)
15739 {
15740 location_t loc = c_parser_peek_token (parser)->location;
15741 tree clauses = NULL_TREE;
15742 if (c_parser_next_token_is (parser, CPP_NAME))
15743 clauses = c_parser_omp_all_clauses (parser, OMP_DECLARE_TARGET_CLAUSE_MASK,
15744 "#pragma omp declare target");
15745 else if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
15746 {
15747 clauses = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_TO_DECLARE,
15748 clauses);
15749 clauses = c_finish_omp_clauses (clauses, true);
15750 c_parser_skip_to_pragma_eol (parser);
15751 }
15752 else
15753 {
15754 c_parser_skip_to_pragma_eol (parser);
15755 current_omp_declare_target_attribute++;
15756 return;
15757 }
15758 if (current_omp_declare_target_attribute)
15759 error_at (loc, "%<#pragma omp declare target%> with clauses in between "
15760 "%<#pragma omp declare target%> without clauses and "
15761 "%<#pragma omp end declare target%>");
15762 for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
15763 {
15764 tree t = OMP_CLAUSE_DECL (c), id;
15765 tree at1 = lookup_attribute ("omp declare target", DECL_ATTRIBUTES (t));
15766 tree at2 = lookup_attribute ("omp declare target link",
15767 DECL_ATTRIBUTES (t));
15768 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINK)
15769 {
15770 id = get_identifier ("omp declare target link");
15771 std::swap (at1, at2);
15772 }
15773 else
15774 id = get_identifier ("omp declare target");
15775 if (at2)
15776 {
15777 error_at (OMP_CLAUSE_LOCATION (c),
15778 "%qD specified both in declare target %<link%> and %<to%>"
15779 " clauses", t);
15780 continue;
15781 }
15782 if (!at1)
15783 {
15784 symtab_node *node = symtab_node::get (t);
15785 DECL_ATTRIBUTES (t) = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (t));
15786 if (node != NULL)
15787 {
15788 node->offloadable = 1;
15789 #ifdef ENABLE_OFFLOADING
15790 g->have_offload = true;
15791 if (is_a <varpool_node *> (node))
15792 {
15793 vec_safe_push (offload_vars, t);
15794 node->force_output = 1;
15795 }
15796 #endif
15797 }
15798 }
15799 }
15800 }
15801
15802 static void
15803 c_parser_omp_end_declare_target (c_parser *parser)
15804 {
15805 location_t loc = c_parser_peek_token (parser)->location;
15806 c_parser_consume_pragma (parser);
15807 if (c_parser_next_token_is (parser, CPP_NAME)
15808 && strcmp (IDENTIFIER_POINTER (c_parser_peek_token (parser)->value),
15809 "declare") == 0)
15810 {
15811 c_parser_consume_token (parser);
15812 if (c_parser_next_token_is (parser, CPP_NAME)
15813 && strcmp (IDENTIFIER_POINTER (c_parser_peek_token (parser)->value),
15814 "target") == 0)
15815 c_parser_consume_token (parser);
15816 else
15817 {
15818 c_parser_error (parser, "expected %<target%>");
15819 c_parser_skip_to_pragma_eol (parser);
15820 return;
15821 }
15822 }
15823 else
15824 {
15825 c_parser_error (parser, "expected %<declare%>");
15826 c_parser_skip_to_pragma_eol (parser);
15827 return;
15828 }
15829 c_parser_skip_to_pragma_eol (parser);
15830 if (!current_omp_declare_target_attribute)
15831 error_at (loc, "%<#pragma omp end declare target%> without corresponding "
15832 "%<#pragma omp declare target%>");
15833 else
15834 current_omp_declare_target_attribute--;
15835 }
15836
15837
15838 /* OpenMP 4.0
15839 #pragma omp declare reduction (reduction-id : typename-list : expression) \
15840 initializer-clause[opt] new-line
15841
15842 initializer-clause:
15843 initializer (omp_priv = initializer)
15844 initializer (function-name (argument-list)) */
15845
15846 static void
15847 c_parser_omp_declare_reduction (c_parser *parser, enum pragma_context context)
15848 {
15849 unsigned int tokens_avail = 0, i;
15850 vec<tree> types = vNULL;
15851 vec<c_token> clauses = vNULL;
15852 enum tree_code reduc_code = ERROR_MARK;
15853 tree reduc_id = NULL_TREE;
15854 tree type;
15855 location_t rloc = c_parser_peek_token (parser)->location;
15856
15857 if (context == pragma_struct || context == pragma_param)
15858 {
15859 error ("%<#pragma omp declare reduction%> not at file or block scope");
15860 goto fail;
15861 }
15862
15863 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
15864 goto fail;
15865
15866 switch (c_parser_peek_token (parser)->type)
15867 {
15868 case CPP_PLUS:
15869 reduc_code = PLUS_EXPR;
15870 break;
15871 case CPP_MULT:
15872 reduc_code = MULT_EXPR;
15873 break;
15874 case CPP_MINUS:
15875 reduc_code = MINUS_EXPR;
15876 break;
15877 case CPP_AND:
15878 reduc_code = BIT_AND_EXPR;
15879 break;
15880 case CPP_XOR:
15881 reduc_code = BIT_XOR_EXPR;
15882 break;
15883 case CPP_OR:
15884 reduc_code = BIT_IOR_EXPR;
15885 break;
15886 case CPP_AND_AND:
15887 reduc_code = TRUTH_ANDIF_EXPR;
15888 break;
15889 case CPP_OR_OR:
15890 reduc_code = TRUTH_ORIF_EXPR;
15891 break;
15892 case CPP_NAME:
15893 const char *p;
15894 p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
15895 if (strcmp (p, "min") == 0)
15896 {
15897 reduc_code = MIN_EXPR;
15898 break;
15899 }
15900 if (strcmp (p, "max") == 0)
15901 {
15902 reduc_code = MAX_EXPR;
15903 break;
15904 }
15905 reduc_id = c_parser_peek_token (parser)->value;
15906 break;
15907 default:
15908 c_parser_error (parser,
15909 "expected %<+%>, %<*%>, %<-%>, %<&%>, "
15910 "%<^%>, %<|%>, %<&&%>, %<||%>, %<min%> or identifier");
15911 goto fail;
15912 }
15913
15914 tree orig_reduc_id, reduc_decl;
15915 orig_reduc_id = reduc_id;
15916 reduc_id = c_omp_reduction_id (reduc_code, reduc_id);
15917 reduc_decl = c_omp_reduction_decl (reduc_id);
15918 c_parser_consume_token (parser);
15919
15920 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
15921 goto fail;
15922
15923 while (true)
15924 {
15925 location_t loc = c_parser_peek_token (parser)->location;
15926 struct c_type_name *ctype = c_parser_type_name (parser);
15927 if (ctype != NULL)
15928 {
15929 type = groktypename (ctype, NULL, NULL);
15930 if (type == error_mark_node)
15931 ;
15932 else if ((INTEGRAL_TYPE_P (type)
15933 || TREE_CODE (type) == REAL_TYPE
15934 || TREE_CODE (type) == COMPLEX_TYPE)
15935 && orig_reduc_id == NULL_TREE)
15936 error_at (loc, "predeclared arithmetic type in "
15937 "%<#pragma omp declare reduction%>");
15938 else if (TREE_CODE (type) == FUNCTION_TYPE
15939 || TREE_CODE (type) == ARRAY_TYPE)
15940 error_at (loc, "function or array type in "
15941 "%<#pragma omp declare reduction%>");
15942 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
15943 error_at (loc, "const, volatile or restrict qualified type in "
15944 "%<#pragma omp declare reduction%>");
15945 else
15946 {
15947 tree t;
15948 for (t = DECL_INITIAL (reduc_decl); t; t = TREE_CHAIN (t))
15949 if (comptypes (TREE_PURPOSE (t), type))
15950 {
15951 error_at (loc, "redeclaration of %qs "
15952 "%<#pragma omp declare reduction%> for "
15953 "type %qT",
15954 IDENTIFIER_POINTER (reduc_id)
15955 + sizeof ("omp declare reduction ") - 1,
15956 type);
15957 location_t ploc
15958 = DECL_SOURCE_LOCATION (TREE_VEC_ELT (TREE_VALUE (t),
15959 0));
15960 error_at (ploc, "previous %<#pragma omp declare "
15961 "reduction%>");
15962 break;
15963 }
15964 if (t == NULL_TREE)
15965 types.safe_push (type);
15966 }
15967 if (c_parser_next_token_is (parser, CPP_COMMA))
15968 c_parser_consume_token (parser);
15969 else
15970 break;
15971 }
15972 else
15973 break;
15974 }
15975
15976 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>")
15977 || types.is_empty ())
15978 {
15979 fail:
15980 clauses.release ();
15981 types.release ();
15982 while (true)
15983 {
15984 c_token *token = c_parser_peek_token (parser);
15985 if (token->type == CPP_EOF || token->type == CPP_PRAGMA_EOL)
15986 break;
15987 c_parser_consume_token (parser);
15988 }
15989 c_parser_skip_to_pragma_eol (parser);
15990 return;
15991 }
15992
15993 if (types.length () > 1)
15994 {
15995 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
15996 {
15997 c_token *token = c_parser_peek_token (parser);
15998 if (token->type == CPP_EOF)
15999 goto fail;
16000 clauses.safe_push (*token);
16001 c_parser_consume_token (parser);
16002 }
16003 clauses.safe_push (*c_parser_peek_token (parser));
16004 c_parser_skip_to_pragma_eol (parser);
16005
16006 /* Make sure nothing tries to read past the end of the tokens. */
16007 c_token eof_token;
16008 memset (&eof_token, 0, sizeof (eof_token));
16009 eof_token.type = CPP_EOF;
16010 clauses.safe_push (eof_token);
16011 clauses.safe_push (eof_token);
16012 }
16013
16014 int errs = errorcount;
16015 FOR_EACH_VEC_ELT (types, i, type)
16016 {
16017 tokens_avail = parser->tokens_avail;
16018 gcc_assert (parser->tokens == &parser->tokens_buf[0]);
16019 if (!clauses.is_empty ())
16020 {
16021 parser->tokens = clauses.address ();
16022 parser->tokens_avail = clauses.length ();
16023 parser->in_pragma = true;
16024 }
16025
16026 bool nested = current_function_decl != NULL_TREE;
16027 if (nested)
16028 c_push_function_context ();
16029 tree fndecl = build_decl (BUILTINS_LOCATION, FUNCTION_DECL,
16030 reduc_id, default_function_type);
16031 current_function_decl = fndecl;
16032 allocate_struct_function (fndecl, true);
16033 push_scope ();
16034 tree stmt = push_stmt_list ();
16035 /* Intentionally BUILTINS_LOCATION, so that -Wshadow doesn't
16036 warn about these. */
16037 tree omp_out = build_decl (BUILTINS_LOCATION, VAR_DECL,
16038 get_identifier ("omp_out"), type);
16039 DECL_ARTIFICIAL (omp_out) = 1;
16040 DECL_CONTEXT (omp_out) = fndecl;
16041 pushdecl (omp_out);
16042 tree omp_in = build_decl (BUILTINS_LOCATION, VAR_DECL,
16043 get_identifier ("omp_in"), type);
16044 DECL_ARTIFICIAL (omp_in) = 1;
16045 DECL_CONTEXT (omp_in) = fndecl;
16046 pushdecl (omp_in);
16047 struct c_expr combiner = c_parser_expression (parser);
16048 struct c_expr initializer;
16049 tree omp_priv = NULL_TREE, omp_orig = NULL_TREE;
16050 bool bad = false;
16051 initializer.value = error_mark_node;
16052 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
16053 bad = true;
16054 else if (c_parser_next_token_is (parser, CPP_NAME)
16055 && strcmp (IDENTIFIER_POINTER
16056 (c_parser_peek_token (parser)->value),
16057 "initializer") == 0)
16058 {
16059 c_parser_consume_token (parser);
16060 pop_scope ();
16061 push_scope ();
16062 omp_priv = build_decl (BUILTINS_LOCATION, VAR_DECL,
16063 get_identifier ("omp_priv"), type);
16064 DECL_ARTIFICIAL (omp_priv) = 1;
16065 DECL_INITIAL (omp_priv) = error_mark_node;
16066 DECL_CONTEXT (omp_priv) = fndecl;
16067 pushdecl (omp_priv);
16068 omp_orig = build_decl (BUILTINS_LOCATION, VAR_DECL,
16069 get_identifier ("omp_orig"), type);
16070 DECL_ARTIFICIAL (omp_orig) = 1;
16071 DECL_CONTEXT (omp_orig) = fndecl;
16072 pushdecl (omp_orig);
16073 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
16074 bad = true;
16075 else if (!c_parser_next_token_is (parser, CPP_NAME))
16076 {
16077 c_parser_error (parser, "expected %<omp_priv%> or "
16078 "function-name");
16079 bad = true;
16080 }
16081 else if (strcmp (IDENTIFIER_POINTER
16082 (c_parser_peek_token (parser)->value),
16083 "omp_priv") != 0)
16084 {
16085 if (c_parser_peek_2nd_token (parser)->type != CPP_OPEN_PAREN
16086 || c_parser_peek_token (parser)->id_kind != C_ID_ID)
16087 {
16088 c_parser_error (parser, "expected function-name %<(%>");
16089 bad = true;
16090 }
16091 else
16092 initializer = c_parser_postfix_expression (parser);
16093 if (initializer.value
16094 && TREE_CODE (initializer.value) == CALL_EXPR)
16095 {
16096 int j;
16097 tree c = initializer.value;
16098 for (j = 0; j < call_expr_nargs (c); j++)
16099 {
16100 tree a = CALL_EXPR_ARG (c, j);
16101 STRIP_NOPS (a);
16102 if (TREE_CODE (a) == ADDR_EXPR
16103 && TREE_OPERAND (a, 0) == omp_priv)
16104 break;
16105 }
16106 if (j == call_expr_nargs (c))
16107 error ("one of the initializer call arguments should be "
16108 "%<&omp_priv%>");
16109 }
16110 }
16111 else
16112 {
16113 c_parser_consume_token (parser);
16114 if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
16115 bad = true;
16116 else
16117 {
16118 tree st = push_stmt_list ();
16119 start_init (omp_priv, NULL_TREE, 0);
16120 location_t loc = c_parser_peek_token (parser)->location;
16121 struct c_expr init = c_parser_initializer (parser);
16122 finish_init ();
16123 finish_decl (omp_priv, loc, init.value,
16124 init.original_type, NULL_TREE);
16125 pop_stmt_list (st);
16126 }
16127 }
16128 if (!bad
16129 && !c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
16130 bad = true;
16131 }
16132
16133 if (!bad)
16134 {
16135 c_parser_skip_to_pragma_eol (parser);
16136
16137 tree t = tree_cons (type, make_tree_vec (omp_priv ? 6 : 3),
16138 DECL_INITIAL (reduc_decl));
16139 DECL_INITIAL (reduc_decl) = t;
16140 DECL_SOURCE_LOCATION (omp_out) = rloc;
16141 TREE_VEC_ELT (TREE_VALUE (t), 0) = omp_out;
16142 TREE_VEC_ELT (TREE_VALUE (t), 1) = omp_in;
16143 TREE_VEC_ELT (TREE_VALUE (t), 2) = combiner.value;
16144 walk_tree (&combiner.value, c_check_omp_declare_reduction_r,
16145 &TREE_VEC_ELT (TREE_VALUE (t), 0), NULL);
16146 if (omp_priv)
16147 {
16148 DECL_SOURCE_LOCATION (omp_priv) = rloc;
16149 TREE_VEC_ELT (TREE_VALUE (t), 3) = omp_priv;
16150 TREE_VEC_ELT (TREE_VALUE (t), 4) = omp_orig;
16151 TREE_VEC_ELT (TREE_VALUE (t), 5) = initializer.value;
16152 walk_tree (&initializer.value, c_check_omp_declare_reduction_r,
16153 &TREE_VEC_ELT (TREE_VALUE (t), 3), NULL);
16154 walk_tree (&DECL_INITIAL (omp_priv),
16155 c_check_omp_declare_reduction_r,
16156 &TREE_VEC_ELT (TREE_VALUE (t), 3), NULL);
16157 }
16158 }
16159
16160 pop_stmt_list (stmt);
16161 pop_scope ();
16162 if (cfun->language != NULL)
16163 {
16164 ggc_free (cfun->language);
16165 cfun->language = NULL;
16166 }
16167 set_cfun (NULL);
16168 current_function_decl = NULL_TREE;
16169 if (nested)
16170 c_pop_function_context ();
16171
16172 if (!clauses.is_empty ())
16173 {
16174 parser->tokens = &parser->tokens_buf[0];
16175 parser->tokens_avail = tokens_avail;
16176 }
16177 if (bad)
16178 goto fail;
16179 if (errs != errorcount)
16180 break;
16181 }
16182
16183 clauses.release ();
16184 types.release ();
16185 }
16186
16187
16188 /* OpenMP 4.0
16189 #pragma omp declare simd declare-simd-clauses[optseq] new-line
16190 #pragma omp declare reduction (reduction-id : typename-list : expression) \
16191 initializer-clause[opt] new-line
16192 #pragma omp declare target new-line */
16193
16194 static void
16195 c_parser_omp_declare (c_parser *parser, enum pragma_context context)
16196 {
16197 c_parser_consume_pragma (parser);
16198 if (c_parser_next_token_is (parser, CPP_NAME))
16199 {
16200 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
16201 if (strcmp (p, "simd") == 0)
16202 {
16203 /* c_parser_consume_token (parser); done in
16204 c_parser_omp_declare_simd. */
16205 c_parser_omp_declare_simd (parser, context);
16206 return;
16207 }
16208 if (strcmp (p, "reduction") == 0)
16209 {
16210 c_parser_consume_token (parser);
16211 c_parser_omp_declare_reduction (parser, context);
16212 return;
16213 }
16214 if (!flag_openmp) /* flag_openmp_simd */
16215 {
16216 c_parser_skip_to_pragma_eol (parser, false);
16217 return;
16218 }
16219 if (strcmp (p, "target") == 0)
16220 {
16221 c_parser_consume_token (parser);
16222 c_parser_omp_declare_target (parser);
16223 return;
16224 }
16225 }
16226
16227 c_parser_error (parser, "expected %<simd%> or %<reduction%> "
16228 "or %<target%>");
16229 c_parser_skip_to_pragma_eol (parser);
16230 }
16231
16232 /* OpenMP 4.5:
16233 #pragma omp taskloop taskloop-clause[optseq] new-line
16234 for-loop
16235
16236 #pragma omp taskloop simd taskloop-simd-clause[optseq] new-line
16237 for-loop */
16238
16239 #define OMP_TASKLOOP_CLAUSE_MASK \
16240 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
16241 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
16242 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
16243 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
16244 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
16245 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_GRAINSIZE) \
16246 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TASKS) \
16247 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
16248 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
16249 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
16250 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
16251 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
16252 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOGROUP) \
16253 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
16254
16255 static tree
16256 c_parser_omp_taskloop (location_t loc, c_parser *parser,
16257 char *p_name, omp_clause_mask mask, tree *cclauses)
16258 {
16259 tree clauses, block, ret;
16260
16261 strcat (p_name, " taskloop");
16262 mask |= OMP_TASKLOOP_CLAUSE_MASK;
16263
16264 if (c_parser_next_token_is (parser, CPP_NAME))
16265 {
16266 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
16267
16268 if (strcmp (p, "simd") == 0)
16269 {
16270 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
16271 if (cclauses == NULL)
16272 cclauses = cclauses_buf;
16273 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION);
16274 c_parser_consume_token (parser);
16275 if (!flag_openmp) /* flag_openmp_simd */
16276 return c_parser_omp_simd (loc, parser, p_name, mask, cclauses);
16277 block = c_begin_compound_stmt (true);
16278 ret = c_parser_omp_simd (loc, parser, p_name, mask, cclauses);
16279 block = c_end_compound_stmt (loc, block, true);
16280 if (ret == NULL)
16281 return ret;
16282 ret = make_node (OMP_TASKLOOP);
16283 TREE_TYPE (ret) = void_type_node;
16284 OMP_FOR_BODY (ret) = block;
16285 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
16286 SET_EXPR_LOCATION (ret, loc);
16287 add_stmt (ret);
16288 return ret;
16289 }
16290 }
16291 if (!flag_openmp) /* flag_openmp_simd */
16292 {
16293 c_parser_skip_to_pragma_eol (parser, false);
16294 return NULL_TREE;
16295 }
16296
16297 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
16298 if (cclauses)
16299 {
16300 omp_split_clauses (loc, OMP_TASKLOOP, mask, clauses, cclauses);
16301 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
16302 }
16303
16304 block = c_begin_compound_stmt (true);
16305 ret = c_parser_omp_for_loop (loc, parser, OMP_TASKLOOP, clauses, NULL);
16306 block = c_end_compound_stmt (loc, block, true);
16307 add_stmt (block);
16308
16309 return ret;
16310 }
16311
16312 /* Main entry point to parsing most OpenMP pragmas. */
16313
16314 static void
16315 c_parser_omp_construct (c_parser *parser)
16316 {
16317 enum pragma_kind p_kind;
16318 location_t loc;
16319 tree stmt;
16320 char p_name[sizeof "#pragma omp teams distribute parallel for simd"];
16321 omp_clause_mask mask (0);
16322
16323 loc = c_parser_peek_token (parser)->location;
16324 p_kind = c_parser_peek_token (parser)->pragma_kind;
16325 c_parser_consume_pragma (parser);
16326
16327 switch (p_kind)
16328 {
16329 case PRAGMA_OACC_ATOMIC:
16330 c_parser_omp_atomic (loc, parser);
16331 return;
16332 case PRAGMA_OACC_CACHE:
16333 strcpy (p_name, "#pragma acc");
16334 stmt = c_parser_oacc_cache (loc, parser);
16335 break;
16336 case PRAGMA_OACC_DATA:
16337 stmt = c_parser_oacc_data (loc, parser);
16338 break;
16339 case PRAGMA_OACC_KERNELS:
16340 case PRAGMA_OACC_PARALLEL:
16341 strcpy (p_name, "#pragma acc");
16342 stmt = c_parser_oacc_kernels_parallel (loc, parser, p_kind, p_name);
16343 break;
16344 case PRAGMA_OACC_LOOP:
16345 strcpy (p_name, "#pragma acc");
16346 stmt = c_parser_oacc_loop (loc, parser, p_name, mask, NULL);
16347 break;
16348 case PRAGMA_OACC_WAIT:
16349 strcpy (p_name, "#pragma wait");
16350 stmt = c_parser_oacc_wait (loc, parser, p_name);
16351 break;
16352 case PRAGMA_OMP_ATOMIC:
16353 c_parser_omp_atomic (loc, parser);
16354 return;
16355 case PRAGMA_OMP_CRITICAL:
16356 stmt = c_parser_omp_critical (loc, parser);
16357 break;
16358 case PRAGMA_OMP_DISTRIBUTE:
16359 strcpy (p_name, "#pragma omp");
16360 stmt = c_parser_omp_distribute (loc, parser, p_name, mask, NULL);
16361 break;
16362 case PRAGMA_OMP_FOR:
16363 strcpy (p_name, "#pragma omp");
16364 stmt = c_parser_omp_for (loc, parser, p_name, mask, NULL);
16365 break;
16366 case PRAGMA_OMP_MASTER:
16367 stmt = c_parser_omp_master (loc, parser);
16368 break;
16369 case PRAGMA_OMP_PARALLEL:
16370 strcpy (p_name, "#pragma omp");
16371 stmt = c_parser_omp_parallel (loc, parser, p_name, mask, NULL);
16372 break;
16373 case PRAGMA_OMP_SECTIONS:
16374 strcpy (p_name, "#pragma omp");
16375 stmt = c_parser_omp_sections (loc, parser, p_name, mask, NULL);
16376 break;
16377 case PRAGMA_OMP_SIMD:
16378 strcpy (p_name, "#pragma omp");
16379 stmt = c_parser_omp_simd (loc, parser, p_name, mask, NULL);
16380 break;
16381 case PRAGMA_OMP_SINGLE:
16382 stmt = c_parser_omp_single (loc, parser);
16383 break;
16384 case PRAGMA_OMP_TASK:
16385 stmt = c_parser_omp_task (loc, parser);
16386 break;
16387 case PRAGMA_OMP_TASKGROUP:
16388 stmt = c_parser_omp_taskgroup (parser);
16389 break;
16390 case PRAGMA_OMP_TASKLOOP:
16391 strcpy (p_name, "#pragma omp");
16392 stmt = c_parser_omp_taskloop (loc, parser, p_name, mask, NULL);
16393 break;
16394 case PRAGMA_OMP_TEAMS:
16395 strcpy (p_name, "#pragma omp");
16396 stmt = c_parser_omp_teams (loc, parser, p_name, mask, NULL);
16397 break;
16398 default:
16399 gcc_unreachable ();
16400 }
16401
16402 if (stmt)
16403 gcc_assert (EXPR_LOCATION (stmt) != UNKNOWN_LOCATION);
16404 }
16405
16406
16407 /* OpenMP 2.5:
16408 # pragma omp threadprivate (variable-list) */
16409
16410 static void
16411 c_parser_omp_threadprivate (c_parser *parser)
16412 {
16413 tree vars, t;
16414 location_t loc;
16415
16416 c_parser_consume_pragma (parser);
16417 loc = c_parser_peek_token (parser)->location;
16418 vars = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_ERROR, NULL);
16419
16420 /* Mark every variable in VARS to be assigned thread local storage. */
16421 for (t = vars; t; t = TREE_CHAIN (t))
16422 {
16423 tree v = TREE_PURPOSE (t);
16424
16425 /* FIXME diagnostics: Ideally we should keep individual
16426 locations for all the variables in the var list to make the
16427 following errors more precise. Perhaps
16428 c_parser_omp_var_list_parens() should construct a list of
16429 locations to go along with the var list. */
16430
16431 /* If V had already been marked threadprivate, it doesn't matter
16432 whether it had been used prior to this point. */
16433 if (!VAR_P (v))
16434 error_at (loc, "%qD is not a variable", v);
16435 else if (TREE_USED (v) && !C_DECL_THREADPRIVATE_P (v))
16436 error_at (loc, "%qE declared %<threadprivate%> after first use", v);
16437 else if (! is_global_var (v))
16438 error_at (loc, "automatic variable %qE cannot be %<threadprivate%>", v);
16439 else if (TREE_TYPE (v) == error_mark_node)
16440 ;
16441 else if (! COMPLETE_TYPE_P (TREE_TYPE (v)))
16442 error_at (loc, "%<threadprivate%> %qE has incomplete type", v);
16443 else
16444 {
16445 if (! DECL_THREAD_LOCAL_P (v))
16446 {
16447 set_decl_tls_model (v, decl_default_tls_model (v));
16448 /* If rtl has been already set for this var, call
16449 make_decl_rtl once again, so that encode_section_info
16450 has a chance to look at the new decl flags. */
16451 if (DECL_RTL_SET_P (v))
16452 make_decl_rtl (v);
16453 }
16454 C_DECL_THREADPRIVATE_P (v) = 1;
16455 }
16456 }
16457
16458 c_parser_skip_to_pragma_eol (parser);
16459 }
16460 \f
16461 /* Cilk Plus <#pragma simd> parsing routines. */
16462
16463 /* Helper function for c_parser_pragma. Perform some sanity checking
16464 for <#pragma simd> constructs. Returns FALSE if there was a
16465 problem. */
16466
16467 static bool
16468 c_parser_cilk_verify_simd (c_parser *parser,
16469 enum pragma_context context)
16470 {
16471 if (!flag_cilkplus)
16472 {
16473 warning (0, "pragma simd ignored because -fcilkplus is not enabled");
16474 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
16475 return false;
16476 }
16477 if (context == pragma_external)
16478 {
16479 c_parser_error (parser,"pragma simd must be inside a function");
16480 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
16481 return false;
16482 }
16483 return true;
16484 }
16485
16486 /* Cilk Plus:
16487 This function is shared by SIMD-enabled functions and #pragma simd.
16488 If IS_SIMD_FN is true then it is parsing a SIMD-enabled function and
16489 CLAUSES is unused. The main purpose of this function is to parse a
16490 vectorlength attribute or clause and check for parse errors.
16491 When IS_SIMD_FN is true then the function is merely caching the tokens
16492 in PARSER->CILK_SIMD_FN_TOKENS. If errors are found then the token
16493 cache is cleared since there is no reason to continue.
16494 Syntax:
16495 vectorlength ( constant-expression ) */
16496
16497 static tree
16498 c_parser_cilk_clause_vectorlength (c_parser *parser, tree clauses,
16499 bool is_simd_fn)
16500 {
16501 if (is_simd_fn)
16502 check_no_duplicate_clause (clauses, OMP_CLAUSE_SIMDLEN, "vectorlength");
16503 else
16504 /* The vectorlength clause behaves exactly like OpenMP's safelen
16505 clause. Represent it in OpenMP terms. */
16506 check_no_duplicate_clause (clauses, OMP_CLAUSE_SAFELEN, "vectorlength");
16507
16508 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
16509 return clauses;
16510
16511 location_t loc = c_parser_peek_token (parser)->location;
16512 tree expr = c_parser_expr_no_commas (parser, NULL).value;
16513 expr = c_fully_fold (expr, false, NULL);
16514
16515 /* If expr is an error_mark_node then the above function would have
16516 emitted an error. No reason to do it twice. */
16517 if (expr == error_mark_node)
16518 ;
16519 else if (!TREE_TYPE (expr)
16520 || !TREE_CONSTANT (expr)
16521 || !INTEGRAL_TYPE_P (TREE_TYPE (expr)))
16522
16523 error_at (loc, "vectorlength must be an integer constant");
16524 else if (wi::exact_log2 (expr) == -1)
16525 error_at (loc, "vectorlength must be a power of 2");
16526 else
16527 {
16528 if (is_simd_fn)
16529 {
16530 tree u = build_omp_clause (loc, OMP_CLAUSE_SIMDLEN);
16531 OMP_CLAUSE_SIMDLEN_EXPR (u) = expr;
16532 OMP_CLAUSE_CHAIN (u) = clauses;
16533 clauses = u;
16534 }
16535 else
16536 {
16537 tree u = build_omp_clause (loc, OMP_CLAUSE_SAFELEN);
16538 OMP_CLAUSE_SAFELEN_EXPR (u) = expr;
16539 OMP_CLAUSE_CHAIN (u) = clauses;
16540 clauses = u;
16541 }
16542 }
16543
16544 c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
16545
16546 return clauses;
16547 }
16548
16549 /* Cilk Plus:
16550 linear ( simd-linear-variable-list )
16551
16552 simd-linear-variable-list:
16553 simd-linear-variable
16554 simd-linear-variable-list , simd-linear-variable
16555
16556 simd-linear-variable:
16557 id-expression
16558 id-expression : simd-linear-step
16559
16560 simd-linear-step:
16561 conditional-expression */
16562
16563 static tree
16564 c_parser_cilk_clause_linear (c_parser *parser, tree clauses)
16565 {
16566 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
16567 return clauses;
16568
16569 location_t loc = c_parser_peek_token (parser)->location;
16570
16571 if (c_parser_next_token_is_not (parser, CPP_NAME)
16572 || c_parser_peek_token (parser)->id_kind != C_ID_ID)
16573 c_parser_error (parser, "expected identifier");
16574
16575 while (c_parser_next_token_is (parser, CPP_NAME)
16576 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
16577 {
16578 tree var = lookup_name (c_parser_peek_token (parser)->value);
16579
16580 if (var == NULL)
16581 {
16582 undeclared_variable (c_parser_peek_token (parser)->location,
16583 c_parser_peek_token (parser)->value);
16584 c_parser_consume_token (parser);
16585 }
16586 else if (var == error_mark_node)
16587 c_parser_consume_token (parser);
16588 else
16589 {
16590 tree step = integer_one_node;
16591
16592 /* Parse the linear step if present. */
16593 if (c_parser_peek_2nd_token (parser)->type == CPP_COLON)
16594 {
16595 c_parser_consume_token (parser);
16596 c_parser_consume_token (parser);
16597
16598 tree expr = c_parser_expr_no_commas (parser, NULL).value;
16599 expr = c_fully_fold (expr, false, NULL);
16600
16601 if (TREE_TYPE (expr)
16602 && INTEGRAL_TYPE_P (TREE_TYPE (expr))
16603 && (TREE_CONSTANT (expr)
16604 || DECL_P (expr)))
16605 step = expr;
16606 else
16607 c_parser_error (parser,
16608 "step size must be an integer constant "
16609 "expression or an integer variable");
16610 }
16611 else
16612 c_parser_consume_token (parser);
16613
16614 /* Use OMP_CLAUSE_LINEAR, which has the same semantics. */
16615 tree u = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
16616 OMP_CLAUSE_DECL (u) = var;
16617 OMP_CLAUSE_LINEAR_STEP (u) = step;
16618 OMP_CLAUSE_CHAIN (u) = clauses;
16619 clauses = u;
16620 }
16621
16622 if (c_parser_next_token_is_not (parser, CPP_COMMA))
16623 break;
16624
16625 c_parser_consume_token (parser);
16626 }
16627
16628 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
16629
16630 return clauses;
16631 }
16632
16633 /* Returns the name of the next clause. If the clause is not
16634 recognized SIMD_OMP_CLAUSE_NONE is returned and the next token is
16635 not consumed. Otherwise, the appropriate pragma_simd_clause is
16636 returned and the token is consumed. */
16637
16638 static pragma_omp_clause
16639 c_parser_cilk_clause_name (c_parser *parser)
16640 {
16641 pragma_omp_clause result;
16642 c_token *token = c_parser_peek_token (parser);
16643
16644 if (!token->value || token->type != CPP_NAME)
16645 return PRAGMA_CILK_CLAUSE_NONE;
16646
16647 const char *p = IDENTIFIER_POINTER (token->value);
16648
16649 if (!strcmp (p, "vectorlength"))
16650 result = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
16651 else if (!strcmp (p, "linear"))
16652 result = PRAGMA_CILK_CLAUSE_LINEAR;
16653 else if (!strcmp (p, "private"))
16654 result = PRAGMA_CILK_CLAUSE_PRIVATE;
16655 else if (!strcmp (p, "firstprivate"))
16656 result = PRAGMA_CILK_CLAUSE_FIRSTPRIVATE;
16657 else if (!strcmp (p, "lastprivate"))
16658 result = PRAGMA_CILK_CLAUSE_LASTPRIVATE;
16659 else if (!strcmp (p, "reduction"))
16660 result = PRAGMA_CILK_CLAUSE_REDUCTION;
16661 else
16662 return PRAGMA_CILK_CLAUSE_NONE;
16663
16664 c_parser_consume_token (parser);
16665 return result;
16666 }
16667
16668 /* Parse all #<pragma simd> clauses. Return the list of clauses
16669 found. */
16670
16671 static tree
16672 c_parser_cilk_all_clauses (c_parser *parser)
16673 {
16674 tree clauses = NULL;
16675
16676 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
16677 {
16678 pragma_omp_clause c_kind;
16679
16680 c_kind = c_parser_cilk_clause_name (parser);
16681
16682 switch (c_kind)
16683 {
16684 case PRAGMA_CILK_CLAUSE_VECTORLENGTH:
16685 clauses = c_parser_cilk_clause_vectorlength (parser, clauses, false);
16686 break;
16687 case PRAGMA_CILK_CLAUSE_LINEAR:
16688 clauses = c_parser_cilk_clause_linear (parser, clauses);
16689 break;
16690 case PRAGMA_CILK_CLAUSE_PRIVATE:
16691 /* Use the OpenMP counterpart. */
16692 clauses = c_parser_omp_clause_private (parser, clauses);
16693 break;
16694 case PRAGMA_CILK_CLAUSE_FIRSTPRIVATE:
16695 /* Use the OpenMP counterpart. */
16696 clauses = c_parser_omp_clause_firstprivate (parser, clauses);
16697 break;
16698 case PRAGMA_CILK_CLAUSE_LASTPRIVATE:
16699 /* Use the OpenMP counterpart. */
16700 clauses = c_parser_omp_clause_lastprivate (parser, clauses);
16701 break;
16702 case PRAGMA_CILK_CLAUSE_REDUCTION:
16703 /* Use the OpenMP counterpart. */
16704 clauses = c_parser_omp_clause_reduction (parser, clauses);
16705 break;
16706 default:
16707 c_parser_error (parser, "expected %<#pragma simd%> clause");
16708 goto saw_error;
16709 }
16710 }
16711
16712 saw_error:
16713 c_parser_skip_to_pragma_eol (parser);
16714 return c_finish_cilk_clauses (clauses);
16715 }
16716
16717 /* This function helps parse the grainsize pragma for a _Cilk_for statement.
16718 Here is the correct syntax of this pragma:
16719 #pragma cilk grainsize = <EXP>
16720 */
16721
16722 static void
16723 c_parser_cilk_grainsize (c_parser *parser)
16724 {
16725 extern tree convert_to_integer (tree, tree);
16726
16727 /* consume the 'grainsize' keyword. */
16728 c_parser_consume_pragma (parser);
16729
16730 if (c_parser_require (parser, CPP_EQ, "expected %<=%>") != 0)
16731 {
16732 struct c_expr g_expr = c_parser_binary_expression (parser, NULL, NULL);
16733 if (g_expr.value == error_mark_node)
16734 {
16735 c_parser_skip_to_pragma_eol (parser);
16736 return;
16737 }
16738 tree grain = convert_to_integer (long_integer_type_node,
16739 c_fully_fold (g_expr.value, false,
16740 NULL));
16741 c_parser_skip_to_pragma_eol (parser);
16742 c_token *token = c_parser_peek_token (parser);
16743 if (token && token->type == CPP_KEYWORD
16744 && token->keyword == RID_CILK_FOR)
16745 {
16746 if (grain == NULL_TREE || grain == error_mark_node)
16747 grain = integer_zero_node;
16748 c_parser_cilk_for (parser, grain);
16749 }
16750 else
16751 warning (0, "%<#pragma cilk grainsize%> is not followed by "
16752 "%<_Cilk_for%>");
16753 }
16754 else
16755 c_parser_skip_to_pragma_eol (parser);
16756 }
16757
16758 /* Main entry point for parsing Cilk Plus <#pragma simd> for loops. */
16759
16760 static void
16761 c_parser_cilk_simd (c_parser *parser)
16762 {
16763 tree clauses = c_parser_cilk_all_clauses (parser);
16764 tree block = c_begin_compound_stmt (true);
16765 location_t loc = c_parser_peek_token (parser)->location;
16766 c_parser_omp_for_loop (loc, parser, CILK_SIMD, clauses, NULL);
16767 block = c_end_compound_stmt (loc, block, true);
16768 add_stmt (block);
16769 }
16770
16771 /* Create an artificial decl with TYPE and emit initialization of it with
16772 INIT. */
16773
16774 static tree
16775 c_get_temp_regvar (tree type, tree init)
16776 {
16777 location_t loc = EXPR_LOCATION (init);
16778 tree decl = build_decl (loc, VAR_DECL, NULL_TREE, type);
16779 DECL_ARTIFICIAL (decl) = 1;
16780 DECL_IGNORED_P (decl) = 1;
16781 pushdecl (decl);
16782 tree t = build2 (INIT_EXPR, type, decl, init);
16783 add_stmt (t);
16784 return decl;
16785 }
16786
16787 /* Main entry point for parsing Cilk Plus _Cilk_for loops.
16788 GRAIN is the grain value passed in through pragma or 0. */
16789
16790 static void
16791 c_parser_cilk_for (c_parser *parser, tree grain)
16792 {
16793 tree clauses = build_omp_clause (EXPR_LOCATION (grain), OMP_CLAUSE_SCHEDULE);
16794 OMP_CLAUSE_SCHEDULE_KIND (clauses) = OMP_CLAUSE_SCHEDULE_CILKFOR;
16795 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (clauses) = grain;
16796 clauses = c_finish_omp_clauses (clauses, false);
16797
16798 tree block = c_begin_compound_stmt (true);
16799 tree sb = push_stmt_list ();
16800 location_t loc = c_parser_peek_token (parser)->location;
16801 tree omp_for = c_parser_omp_for_loop (loc, parser, CILK_FOR, clauses, NULL);
16802 sb = pop_stmt_list (sb);
16803
16804 if (omp_for)
16805 {
16806 tree omp_par = make_node (OMP_PARALLEL);
16807 TREE_TYPE (omp_par) = void_type_node;
16808 OMP_PARALLEL_CLAUSES (omp_par) = NULL_TREE;
16809 tree bind = build3 (BIND_EXPR, void_type_node, NULL, NULL, NULL);
16810 TREE_SIDE_EFFECTS (bind) = 1;
16811 BIND_EXPR_BODY (bind) = sb;
16812 OMP_PARALLEL_BODY (omp_par) = bind;
16813 if (OMP_FOR_PRE_BODY (omp_for))
16814 {
16815 add_stmt (OMP_FOR_PRE_BODY (omp_for));
16816 OMP_FOR_PRE_BODY (omp_for) = NULL_TREE;
16817 }
16818 tree init = TREE_VEC_ELT (OMP_FOR_INIT (omp_for), 0);
16819 tree decl = TREE_OPERAND (init, 0);
16820 tree cond = TREE_VEC_ELT (OMP_FOR_COND (omp_for), 0);
16821 tree incr = TREE_VEC_ELT (OMP_FOR_INCR (omp_for), 0);
16822 tree t = TREE_OPERAND (cond, 1), c, clauses = NULL_TREE;
16823 if (TREE_CODE (t) != INTEGER_CST)
16824 {
16825 TREE_OPERAND (cond, 1) = c_get_temp_regvar (TREE_TYPE (t), t);
16826 c = build_omp_clause (input_location, OMP_CLAUSE_FIRSTPRIVATE);
16827 OMP_CLAUSE_DECL (c) = TREE_OPERAND (cond, 1);
16828 OMP_CLAUSE_CHAIN (c) = clauses;
16829 clauses = c;
16830 }
16831 if (TREE_CODE (incr) == MODIFY_EXPR)
16832 {
16833 t = TREE_OPERAND (TREE_OPERAND (incr, 1), 1);
16834 if (TREE_CODE (t) != INTEGER_CST)
16835 {
16836 TREE_OPERAND (TREE_OPERAND (incr, 1), 1)
16837 = c_get_temp_regvar (TREE_TYPE (t), t);
16838 c = build_omp_clause (input_location, OMP_CLAUSE_FIRSTPRIVATE);
16839 OMP_CLAUSE_DECL (c) = TREE_OPERAND (TREE_OPERAND (incr, 1), 1);
16840 OMP_CLAUSE_CHAIN (c) = clauses;
16841 clauses = c;
16842 }
16843 }
16844 t = TREE_OPERAND (init, 1);
16845 if (TREE_CODE (t) != INTEGER_CST)
16846 {
16847 TREE_OPERAND (init, 1) = c_get_temp_regvar (TREE_TYPE (t), t);
16848 c = build_omp_clause (input_location, OMP_CLAUSE_FIRSTPRIVATE);
16849 OMP_CLAUSE_DECL (c) = TREE_OPERAND (init, 1);
16850 OMP_CLAUSE_CHAIN (c) = clauses;
16851 clauses = c;
16852 }
16853 c = build_omp_clause (input_location, OMP_CLAUSE_PRIVATE);
16854 OMP_CLAUSE_DECL (c) = decl;
16855 OMP_CLAUSE_CHAIN (c) = clauses;
16856 clauses = c;
16857 c = build_omp_clause (input_location, OMP_CLAUSE__CILK_FOR_COUNT_);
16858 OMP_CLAUSE_OPERAND (c, 0)
16859 = cilk_for_number_of_iterations (omp_for);
16860 OMP_CLAUSE_CHAIN (c) = clauses;
16861 OMP_PARALLEL_CLAUSES (omp_par) = c_finish_omp_clauses (c, true);
16862 add_stmt (omp_par);
16863 }
16864
16865 block = c_end_compound_stmt (loc, block, true);
16866 add_stmt (block);
16867 }
16868
16869 \f
16870 /* Parse a transaction attribute (GCC Extension).
16871
16872 transaction-attribute:
16873 attributes
16874 [ [ any-word ] ]
16875
16876 The transactional memory language description is written for C++,
16877 and uses the C++0x attribute syntax. For compatibility, allow the
16878 bracket style for transactions in C as well. */
16879
16880 static tree
16881 c_parser_transaction_attributes (c_parser *parser)
16882 {
16883 tree attr_name, attr = NULL;
16884
16885 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
16886 return c_parser_attributes (parser);
16887
16888 if (!c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
16889 return NULL_TREE;
16890 c_parser_consume_token (parser);
16891 if (!c_parser_require (parser, CPP_OPEN_SQUARE, "expected %<[%>"))
16892 goto error1;
16893
16894 attr_name = c_parser_attribute_any_word (parser);
16895 if (attr_name)
16896 {
16897 c_parser_consume_token (parser);
16898 attr = build_tree_list (attr_name, NULL_TREE);
16899 }
16900 else
16901 c_parser_error (parser, "expected identifier");
16902
16903 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, "expected %<]%>");
16904 error1:
16905 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, "expected %<]%>");
16906 return attr;
16907 }
16908
16909 /* Parse a __transaction_atomic or __transaction_relaxed statement
16910 (GCC Extension).
16911
16912 transaction-statement:
16913 __transaction_atomic transaction-attribute[opt] compound-statement
16914 __transaction_relaxed compound-statement
16915
16916 Note that the only valid attribute is: "outer".
16917 */
16918
16919 static tree
16920 c_parser_transaction (c_parser *parser, enum rid keyword)
16921 {
16922 unsigned int old_in = parser->in_transaction;
16923 unsigned int this_in = 1, new_in;
16924 location_t loc = c_parser_peek_token (parser)->location;
16925 tree stmt, attrs;
16926
16927 gcc_assert ((keyword == RID_TRANSACTION_ATOMIC
16928 || keyword == RID_TRANSACTION_RELAXED)
16929 && c_parser_next_token_is_keyword (parser, keyword));
16930 c_parser_consume_token (parser);
16931
16932 if (keyword == RID_TRANSACTION_RELAXED)
16933 this_in |= TM_STMT_ATTR_RELAXED;
16934 else
16935 {
16936 attrs = c_parser_transaction_attributes (parser);
16937 if (attrs)
16938 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
16939 }
16940
16941 /* Keep track if we're in the lexical scope of an outer transaction. */
16942 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
16943
16944 parser->in_transaction = new_in;
16945 stmt = c_parser_compound_statement (parser);
16946 parser->in_transaction = old_in;
16947
16948 if (flag_tm)
16949 stmt = c_finish_transaction (loc, stmt, this_in);
16950 else
16951 error_at (loc, (keyword == RID_TRANSACTION_ATOMIC ?
16952 "%<__transaction_atomic%> without transactional memory support enabled"
16953 : "%<__transaction_relaxed %> "
16954 "without transactional memory support enabled"));
16955
16956 return stmt;
16957 }
16958
16959 /* Parse a __transaction_atomic or __transaction_relaxed expression
16960 (GCC Extension).
16961
16962 transaction-expression:
16963 __transaction_atomic ( expression )
16964 __transaction_relaxed ( expression )
16965 */
16966
16967 static struct c_expr
16968 c_parser_transaction_expression (c_parser *parser, enum rid keyword)
16969 {
16970 struct c_expr ret;
16971 unsigned int old_in = parser->in_transaction;
16972 unsigned int this_in = 1;
16973 location_t loc = c_parser_peek_token (parser)->location;
16974 tree attrs;
16975
16976 gcc_assert ((keyword == RID_TRANSACTION_ATOMIC
16977 || keyword == RID_TRANSACTION_RELAXED)
16978 && c_parser_next_token_is_keyword (parser, keyword));
16979 c_parser_consume_token (parser);
16980
16981 if (keyword == RID_TRANSACTION_RELAXED)
16982 this_in |= TM_STMT_ATTR_RELAXED;
16983 else
16984 {
16985 attrs = c_parser_transaction_attributes (parser);
16986 if (attrs)
16987 this_in |= parse_tm_stmt_attr (attrs, 0);
16988 }
16989
16990 parser->in_transaction = this_in;
16991 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
16992 {
16993 tree expr = c_parser_expression (parser).value;
16994 ret.original_type = TREE_TYPE (expr);
16995 ret.value = build1 (TRANSACTION_EXPR, ret.original_type, expr);
16996 if (this_in & TM_STMT_ATTR_RELAXED)
16997 TRANSACTION_EXPR_RELAXED (ret.value) = 1;
16998 SET_EXPR_LOCATION (ret.value, loc);
16999 ret.original_code = TRANSACTION_EXPR;
17000 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
17001 {
17002 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
17003 goto error;
17004 }
17005 }
17006 else
17007 {
17008 error:
17009 ret.value = error_mark_node;
17010 ret.original_code = ERROR_MARK;
17011 ret.original_type = NULL;
17012 }
17013 parser->in_transaction = old_in;
17014
17015 if (!flag_tm)
17016 error_at (loc, (keyword == RID_TRANSACTION_ATOMIC ?
17017 "%<__transaction_atomic%> without transactional memory support enabled"
17018 : "%<__transaction_relaxed %> "
17019 "without transactional memory support enabled"));
17020
17021 return ret;
17022 }
17023
17024 /* Parse a __transaction_cancel statement (GCC Extension).
17025
17026 transaction-cancel-statement:
17027 __transaction_cancel transaction-attribute[opt] ;
17028
17029 Note that the only valid attribute is "outer".
17030 */
17031
17032 static tree
17033 c_parser_transaction_cancel (c_parser *parser)
17034 {
17035 location_t loc = c_parser_peek_token (parser)->location;
17036 tree attrs;
17037 bool is_outer = false;
17038
17039 gcc_assert (c_parser_next_token_is_keyword (parser, RID_TRANSACTION_CANCEL));
17040 c_parser_consume_token (parser);
17041
17042 attrs = c_parser_transaction_attributes (parser);
17043 if (attrs)
17044 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
17045
17046 if (!flag_tm)
17047 {
17048 error_at (loc, "%<__transaction_cancel%> without "
17049 "transactional memory support enabled");
17050 goto ret_error;
17051 }
17052 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
17053 {
17054 error_at (loc, "%<__transaction_cancel%> within a "
17055 "%<__transaction_relaxed%>");
17056 goto ret_error;
17057 }
17058 else if (is_outer)
17059 {
17060 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
17061 && !is_tm_may_cancel_outer (current_function_decl))
17062 {
17063 error_at (loc, "outer %<__transaction_cancel%> not "
17064 "within outer %<__transaction_atomic%>");
17065 error_at (loc, " or a %<transaction_may_cancel_outer%> function");
17066 goto ret_error;
17067 }
17068 }
17069 else if (parser->in_transaction == 0)
17070 {
17071 error_at (loc, "%<__transaction_cancel%> not within "
17072 "%<__transaction_atomic%>");
17073 goto ret_error;
17074 }
17075
17076 return add_stmt (build_tm_abort_call (loc, is_outer));
17077
17078 ret_error:
17079 return build1 (NOP_EXPR, void_type_node, error_mark_node);
17080 }
17081 \f
17082 /* Parse a single source file. */
17083
17084 void
17085 c_parse_file (void)
17086 {
17087 /* Use local storage to begin. If the first token is a pragma, parse it.
17088 If it is #pragma GCC pch_preprocess, then this will load a PCH file
17089 which will cause garbage collection. */
17090 c_parser tparser;
17091
17092 memset (&tparser, 0, sizeof tparser);
17093 tparser.tokens = &tparser.tokens_buf[0];
17094 the_parser = &tparser;
17095
17096 if (c_parser_peek_token (&tparser)->pragma_kind == PRAGMA_GCC_PCH_PREPROCESS)
17097 c_parser_pragma_pch_preprocess (&tparser);
17098
17099 the_parser = ggc_alloc<c_parser> ();
17100 *the_parser = tparser;
17101 if (tparser.tokens == &tparser.tokens_buf[0])
17102 the_parser->tokens = &the_parser->tokens_buf[0];
17103
17104 /* Initialize EH, if we've been told to do so. */
17105 if (flag_exceptions)
17106 using_eh_for_cleanups ();
17107
17108 c_parser_translation_unit (the_parser);
17109 the_parser = NULL;
17110 }
17111
17112 /* This function parses Cilk Plus array notation. The starting index is
17113 passed in INITIAL_INDEX and the array name is passes in ARRAY_VALUE. The
17114 return value of this function is a tree_node called VALUE_TREE of type
17115 ARRAY_NOTATION_REF. */
17116
17117 static tree
17118 c_parser_array_notation (location_t loc, c_parser *parser, tree initial_index,
17119 tree array_value)
17120 {
17121 c_token *token = NULL;
17122 tree start_index = NULL_TREE, end_index = NULL_TREE, stride = NULL_TREE;
17123 tree value_tree = NULL_TREE, type = NULL_TREE, array_type = NULL_TREE;
17124 tree array_type_domain = NULL_TREE;
17125
17126 if (array_value == error_mark_node || initial_index == error_mark_node)
17127 {
17128 /* No need to continue. If either of these 2 were true, then an error
17129 must be emitted already. Thus, no need to emit them twice. */
17130 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
17131 return error_mark_node;
17132 }
17133
17134 array_type = TREE_TYPE (array_value);
17135 gcc_assert (array_type);
17136 if (TREE_CODE (array_type) != ARRAY_TYPE
17137 && TREE_CODE (array_type) != POINTER_TYPE)
17138 {
17139 error_at (loc, "base of array section must be pointer or array type");
17140 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
17141 return error_mark_node;
17142 }
17143 type = TREE_TYPE (array_type);
17144 token = c_parser_peek_token (parser);
17145
17146 if (token->type == CPP_EOF)
17147 {
17148 c_parser_error (parser, "expected %<:%> or numeral");
17149 return value_tree;
17150 }
17151 else if (token->type == CPP_COLON)
17152 {
17153 if (!initial_index)
17154 {
17155 /* If we are here, then we have a case like this A[:]. */
17156 c_parser_consume_token (parser);
17157 if (TREE_CODE (array_type) == POINTER_TYPE)
17158 {
17159 error_at (loc, "start-index and length fields necessary for "
17160 "using array notations in pointers");
17161 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
17162 return error_mark_node;
17163 }
17164 if (TREE_CODE (array_type) == FUNCTION_TYPE)
17165 {
17166 error_at (loc, "array notations cannot be used with function "
17167 "type");
17168 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
17169 return error_mark_node;
17170 }
17171 array_type_domain = TYPE_DOMAIN (array_type);
17172
17173 if (!array_type_domain)
17174 {
17175 error_at (loc, "start-index and length fields necessary for "
17176 "using array notations in dimensionless arrays");
17177 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
17178 return error_mark_node;
17179 }
17180
17181 start_index = TYPE_MINVAL (array_type_domain);
17182 start_index = fold_build1 (CONVERT_EXPR, ptrdiff_type_node,
17183 start_index);
17184 if (!TYPE_MAXVAL (array_type_domain)
17185 || !TREE_CONSTANT (TYPE_MAXVAL (array_type_domain)))
17186 {
17187 error_at (loc, "start-index and length fields necessary for "
17188 "using array notations in variable-length arrays");
17189 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
17190 return error_mark_node;
17191 }
17192 end_index = TYPE_MAXVAL (array_type_domain);
17193 end_index = fold_build2 (PLUS_EXPR, TREE_TYPE (end_index),
17194 end_index, integer_one_node);
17195 end_index = fold_build1 (CONVERT_EXPR, ptrdiff_type_node, end_index);
17196 stride = build_int_cst (integer_type_node, 1);
17197 stride = fold_build1 (CONVERT_EXPR, ptrdiff_type_node, stride);
17198 }
17199 else if (initial_index != error_mark_node)
17200 {
17201 /* If we are here, then there should be 2 possibilities:
17202 1. Array [EXPR : EXPR]
17203 2. Array [EXPR : EXPR : EXPR]
17204 */
17205 start_index = initial_index;
17206
17207 if (TREE_CODE (array_type) == FUNCTION_TYPE)
17208 {
17209 error_at (loc, "array notations cannot be used with function "
17210 "type");
17211 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
17212 return error_mark_node;
17213 }
17214 c_parser_consume_token (parser); /* consume the ':' */
17215 struct c_expr ce = c_parser_expression (parser);
17216 ce = convert_lvalue_to_rvalue (loc, ce, false, false);
17217 end_index = ce.value;
17218 if (!end_index || end_index == error_mark_node)
17219 {
17220 c_parser_skip_to_end_of_block_or_statement (parser);
17221 return error_mark_node;
17222 }
17223 if (c_parser_peek_token (parser)->type == CPP_COLON)
17224 {
17225 c_parser_consume_token (parser);
17226 ce = c_parser_expression (parser);
17227 ce = convert_lvalue_to_rvalue (loc, ce, false, false);
17228 stride = ce.value;
17229 if (!stride || stride == error_mark_node)
17230 {
17231 c_parser_skip_to_end_of_block_or_statement (parser);
17232 return error_mark_node;
17233 }
17234 }
17235 }
17236 else
17237 c_parser_error (parser, "expected array notation expression");
17238 }
17239 else
17240 c_parser_error (parser, "expected array notation expression");
17241
17242 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, "expected %<]%>");
17243
17244 value_tree = build_array_notation_ref (loc, array_value, start_index,
17245 end_index, stride, type);
17246 if (value_tree != error_mark_node)
17247 SET_EXPR_LOCATION (value_tree, loc);
17248 return value_tree;
17249 }
17250
17251 #include "gt-c-c-parser.h"