]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/c/c-parser.c
Update copyright years.
[thirdparty/gcc.git] / gcc / c / c-parser.c
1 /* Parser for C and Objective-C.
2 Copyright (C) 1987-2020 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 #define INCLUDE_UNIQUE_PTR
40 #include "system.h"
41 #include "coretypes.h"
42 #include "target.h"
43 #include "function.h"
44 #include "c-tree.h"
45 #include "timevar.h"
46 #include "stringpool.h"
47 #include "cgraph.h"
48 #include "attribs.h"
49 #include "stor-layout.h"
50 #include "varasm.h"
51 #include "trans-mem.h"
52 #include "c-family/c-pragma.h"
53 #include "c-lang.h"
54 #include "c-family/c-objc.h"
55 #include "plugin.h"
56 #include "omp-general.h"
57 #include "omp-offload.h"
58 #include "builtins.h"
59 #include "gomp-constants.h"
60 #include "c-family/c-indentation.h"
61 #include "gimple-expr.h"
62 #include "context.h"
63 #include "gcc-rich-location.h"
64 #include "c-parser.h"
65 #include "gimple-parser.h"
66 #include "read-rtl-function.h"
67 #include "run-rtl-passes.h"
68 #include "intl.h"
69 #include "c-family/name-hint.h"
70 #include "tree-iterator.h"
71 #include "memmodel.h"
72
73 /* We need to walk over decls with incomplete struct/union/enum types
74 after parsing the whole translation unit.
75 In finish_decl(), if the decl is static, has incomplete
76 struct/union/enum type, it is appeneded to incomplete_record_decls.
77 In c_parser_translation_unit(), we iterate over incomplete_record_decls
78 and report error if any of the decls are still incomplete. */
79
80 vec<tree> incomplete_record_decls;
81
82 void
83 set_c_expr_source_range (c_expr *expr,
84 location_t start, location_t finish)
85 {
86 expr->src_range.m_start = start;
87 expr->src_range.m_finish = finish;
88 if (expr->value)
89 set_source_range (expr->value, start, finish);
90 }
91
92 void
93 set_c_expr_source_range (c_expr *expr,
94 source_range src_range)
95 {
96 expr->src_range = src_range;
97 if (expr->value)
98 set_source_range (expr->value, src_range);
99 }
100
101 \f
102 /* Initialization routine for this file. */
103
104 void
105 c_parse_init (void)
106 {
107 /* The only initialization required is of the reserved word
108 identifiers. */
109 unsigned int i;
110 tree id;
111 int mask = 0;
112
113 /* Make sure RID_MAX hasn't grown past the 8 bits used to hold the keyword in
114 the c_token structure. */
115 gcc_assert (RID_MAX <= 255);
116
117 mask |= D_CXXONLY;
118 if (!flag_isoc99)
119 mask |= D_C99;
120 if (flag_no_asm)
121 {
122 mask |= D_ASM | D_EXT;
123 if (!flag_isoc99)
124 mask |= D_EXT89;
125 }
126 if (!c_dialect_objc ())
127 mask |= D_OBJC | D_CXX_OBJC;
128
129 ridpointers = ggc_cleared_vec_alloc<tree> ((int) RID_MAX);
130 for (i = 0; i < num_c_common_reswords; i++)
131 {
132 /* If a keyword is disabled, do not enter it into the table
133 and so create a canonical spelling that isn't a keyword. */
134 if (c_common_reswords[i].disable & mask)
135 {
136 if (warn_cxx_compat
137 && (c_common_reswords[i].disable & D_CXXWARN))
138 {
139 id = get_identifier (c_common_reswords[i].word);
140 C_SET_RID_CODE (id, RID_CXX_COMPAT_WARN);
141 C_IS_RESERVED_WORD (id) = 1;
142 }
143 continue;
144 }
145
146 id = get_identifier (c_common_reswords[i].word);
147 C_SET_RID_CODE (id, c_common_reswords[i].rid);
148 C_IS_RESERVED_WORD (id) = 1;
149 ridpointers [(int) c_common_reswords[i].rid] = id;
150 }
151
152 for (i = 0; i < NUM_INT_N_ENTS; i++)
153 {
154 /* We always create the symbols but they aren't always supported. */
155 char name[50];
156 sprintf (name, "__int%d", int_n_data[i].bitsize);
157 id = get_identifier (name);
158 C_SET_RID_CODE (id, RID_FIRST_INT_N + i);
159 C_IS_RESERVED_WORD (id) = 1;
160
161 sprintf (name, "__int%d__", int_n_data[i].bitsize);
162 id = get_identifier (name);
163 C_SET_RID_CODE (id, RID_FIRST_INT_N + i);
164 C_IS_RESERVED_WORD (id) = 1;
165 }
166 }
167 \f
168 /* A parser structure recording information about the state and
169 context of parsing. Includes lexer information with up to two
170 tokens of look-ahead; more are not needed for C. */
171 struct GTY(()) c_parser {
172 /* The look-ahead tokens. */
173 c_token * GTY((skip)) tokens;
174 /* Buffer for look-ahead tokens. */
175 c_token tokens_buf[4];
176 /* How many look-ahead tokens are available (0 - 4, or
177 more if parsing from pre-lexed tokens). */
178 unsigned int tokens_avail;
179 /* Raw look-ahead tokens, used only for checking in Objective-C
180 whether '[[' starts attributes. */
181 vec<c_token, va_gc> *raw_tokens;
182 /* The number of raw look-ahead tokens that have since been fully
183 lexed. */
184 unsigned int raw_tokens_used;
185 /* True if a syntax error is being recovered from; false otherwise.
186 c_parser_error sets this flag. It should clear this flag when
187 enough tokens have been consumed to recover from the error. */
188 BOOL_BITFIELD error : 1;
189 /* True if we're processing a pragma, and shouldn't automatically
190 consume CPP_PRAGMA_EOL. */
191 BOOL_BITFIELD in_pragma : 1;
192 /* True if we're parsing the outermost block of an if statement. */
193 BOOL_BITFIELD in_if_block : 1;
194 /* True if we want to lex a translated, joined string (for an
195 initial #pragma pch_preprocess). Otherwise the parser is
196 responsible for concatenating strings and translating to the
197 execution character set as needed. */
198 BOOL_BITFIELD lex_joined_string : 1;
199 /* True if, when the parser is concatenating string literals, it
200 should translate them to the execution character set (false
201 inside attributes). */
202 BOOL_BITFIELD translate_strings_p : 1;
203
204 /* Objective-C specific parser/lexer information. */
205
206 /* True if we are in a context where the Objective-C "PQ" keywords
207 are considered keywords. */
208 BOOL_BITFIELD objc_pq_context : 1;
209 /* True if we are parsing a (potential) Objective-C foreach
210 statement. This is set to true after we parsed 'for (' and while
211 we wait for 'in' or ';' to decide if it's a standard C for loop or an
212 Objective-C foreach loop. */
213 BOOL_BITFIELD objc_could_be_foreach_context : 1;
214 /* The following flag is needed to contextualize Objective-C lexical
215 analysis. In some cases (e.g., 'int NSObject;'), it is
216 undesirable to bind an identifier to an Objective-C class, even
217 if a class with that name exists. */
218 BOOL_BITFIELD objc_need_raw_identifier : 1;
219 /* Nonzero if we're processing a __transaction statement. The value
220 is 1 | TM_STMT_ATTR_*. */
221 unsigned int in_transaction : 4;
222 /* True if we are in a context where the Objective-C "Property attribute"
223 keywords are valid. */
224 BOOL_BITFIELD objc_property_attr_context : 1;
225
226 /* Location of the last consumed token. */
227 location_t last_token_location;
228 };
229
230 /* Return a pointer to the Nth token in PARSERs tokens_buf. */
231
232 c_token *
233 c_parser_tokens_buf (c_parser *parser, unsigned n)
234 {
235 return &parser->tokens_buf[n];
236 }
237
238 /* Return the error state of PARSER. */
239
240 bool
241 c_parser_error (c_parser *parser)
242 {
243 return parser->error;
244 }
245
246 /* Set the error state of PARSER to ERR. */
247
248 void
249 c_parser_set_error (c_parser *parser, bool err)
250 {
251 parser->error = err;
252 }
253
254
255 /* The actual parser and external interface. ??? Does this need to be
256 garbage-collected? */
257
258 static GTY (()) c_parser *the_parser;
259
260 /* Read in and lex a single token, storing it in *TOKEN. If RAW,
261 context-sensitive postprocessing of the token is not done. */
262
263 static void
264 c_lex_one_token (c_parser *parser, c_token *token, bool raw = false)
265 {
266 timevar_push (TV_LEX);
267
268 if (raw || vec_safe_length (parser->raw_tokens) == 0)
269 {
270 token->type = c_lex_with_flags (&token->value, &token->location,
271 &token->flags,
272 (parser->lex_joined_string
273 ? 0 : C_LEX_STRING_NO_JOIN));
274 token->id_kind = C_ID_NONE;
275 token->keyword = RID_MAX;
276 token->pragma_kind = PRAGMA_NONE;
277 }
278 else
279 {
280 /* Use a token previously lexed as a raw look-ahead token, and
281 complete the processing on it. */
282 *token = (*parser->raw_tokens)[parser->raw_tokens_used];
283 ++parser->raw_tokens_used;
284 if (parser->raw_tokens_used == vec_safe_length (parser->raw_tokens))
285 {
286 vec_free (parser->raw_tokens);
287 parser->raw_tokens_used = 0;
288 }
289 }
290
291 if (raw)
292 goto out;
293
294 switch (token->type)
295 {
296 case CPP_NAME:
297 {
298 tree decl;
299
300 bool objc_force_identifier = parser->objc_need_raw_identifier;
301 if (c_dialect_objc ())
302 parser->objc_need_raw_identifier = false;
303
304 if (C_IS_RESERVED_WORD (token->value))
305 {
306 enum rid rid_code = C_RID_CODE (token->value);
307
308 if (rid_code == RID_CXX_COMPAT_WARN)
309 {
310 warning_at (token->location,
311 OPT_Wc___compat,
312 "identifier %qE conflicts with C++ keyword",
313 token->value);
314 }
315 else if (rid_code >= RID_FIRST_ADDR_SPACE
316 && rid_code <= RID_LAST_ADDR_SPACE)
317 {
318 addr_space_t as;
319 as = (addr_space_t) (rid_code - RID_FIRST_ADDR_SPACE);
320 targetm.addr_space.diagnose_usage (as, token->location);
321 token->id_kind = C_ID_ADDRSPACE;
322 token->keyword = rid_code;
323 break;
324 }
325 else if (c_dialect_objc () && OBJC_IS_PQ_KEYWORD (rid_code))
326 {
327 /* We found an Objective-C "pq" keyword (in, out,
328 inout, bycopy, byref, oneway). They need special
329 care because the interpretation depends on the
330 context. */
331 if (parser->objc_pq_context)
332 {
333 token->type = CPP_KEYWORD;
334 token->keyword = rid_code;
335 break;
336 }
337 else if (parser->objc_could_be_foreach_context
338 && rid_code == RID_IN)
339 {
340 /* We are in Objective-C, inside a (potential)
341 foreach context (which means after having
342 parsed 'for (', but before having parsed ';'),
343 and we found 'in'. We consider it the keyword
344 which terminates the declaration at the
345 beginning of a foreach-statement. Note that
346 this means you can't use 'in' for anything else
347 in that context; in particular, in Objective-C
348 you can't use 'in' as the name of the running
349 variable in a C for loop. We could potentially
350 try to add code here to disambiguate, but it
351 seems a reasonable limitation. */
352 token->type = CPP_KEYWORD;
353 token->keyword = rid_code;
354 break;
355 }
356 /* Else, "pq" keywords outside of the "pq" context are
357 not keywords, and we fall through to the code for
358 normal tokens. */
359 }
360 else if (c_dialect_objc () && OBJC_IS_PATTR_KEYWORD (rid_code))
361 {
362 /* We found an Objective-C "property attribute"
363 keyword (getter, setter, readonly, etc). These are
364 only valid in the property context. */
365 if (parser->objc_property_attr_context)
366 {
367 token->type = CPP_KEYWORD;
368 token->keyword = rid_code;
369 break;
370 }
371 /* Else they are not special keywords.
372 */
373 }
374 else if (c_dialect_objc ()
375 && (OBJC_IS_AT_KEYWORD (rid_code)
376 || OBJC_IS_CXX_KEYWORD (rid_code)))
377 {
378 /* We found one of the Objective-C "@" keywords (defs,
379 selector, synchronized, etc) or one of the
380 Objective-C "cxx" keywords (class, private,
381 protected, public, try, catch, throw) without a
382 preceding '@' sign. Do nothing and fall through to
383 the code for normal tokens (in C++ we would still
384 consider the CXX ones keywords, but not in C). */
385 ;
386 }
387 else
388 {
389 token->type = CPP_KEYWORD;
390 token->keyword = rid_code;
391 break;
392 }
393 }
394
395 decl = lookup_name (token->value);
396 if (decl)
397 {
398 if (TREE_CODE (decl) == TYPE_DECL)
399 {
400 token->id_kind = C_ID_TYPENAME;
401 break;
402 }
403 }
404 else if (c_dialect_objc ())
405 {
406 tree objc_interface_decl = objc_is_class_name (token->value);
407 /* Objective-C class names are in the same namespace as
408 variables and typedefs, and hence are shadowed by local
409 declarations. */
410 if (objc_interface_decl
411 && (!objc_force_identifier || global_bindings_p ()))
412 {
413 token->value = objc_interface_decl;
414 token->id_kind = C_ID_CLASSNAME;
415 break;
416 }
417 }
418 token->id_kind = C_ID_ID;
419 }
420 break;
421 case CPP_AT_NAME:
422 /* This only happens in Objective-C; it must be a keyword. */
423 token->type = CPP_KEYWORD;
424 switch (C_RID_CODE (token->value))
425 {
426 /* Replace 'class' with '@class', 'private' with '@private',
427 etc. This prevents confusion with the C++ keyword
428 'class', and makes the tokens consistent with other
429 Objective-C 'AT' keywords. For example '@class' is
430 reported as RID_AT_CLASS which is consistent with
431 '@synchronized', which is reported as
432 RID_AT_SYNCHRONIZED.
433 */
434 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
435 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
436 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
437 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
438 case RID_THROW: token->keyword = RID_AT_THROW; break;
439 case RID_TRY: token->keyword = RID_AT_TRY; break;
440 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
441 case RID_SYNCHRONIZED: token->keyword = RID_AT_SYNCHRONIZED; break;
442 default: token->keyword = C_RID_CODE (token->value);
443 }
444 break;
445 case CPP_COLON:
446 case CPP_COMMA:
447 case CPP_CLOSE_PAREN:
448 case CPP_SEMICOLON:
449 /* These tokens may affect the interpretation of any identifiers
450 following, if doing Objective-C. */
451 if (c_dialect_objc ())
452 parser->objc_need_raw_identifier = false;
453 break;
454 case CPP_PRAGMA:
455 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
456 token->pragma_kind = (enum pragma_kind) TREE_INT_CST_LOW (token->value);
457 token->value = NULL;
458 break;
459 default:
460 break;
461 }
462 out:
463 timevar_pop (TV_LEX);
464 }
465
466 /* Return a pointer to the next token from PARSER, reading it in if
467 necessary. */
468
469 c_token *
470 c_parser_peek_token (c_parser *parser)
471 {
472 if (parser->tokens_avail == 0)
473 {
474 c_lex_one_token (parser, &parser->tokens[0]);
475 parser->tokens_avail = 1;
476 }
477 return &parser->tokens[0];
478 }
479
480 /* Return a pointer to the next-but-one token from PARSER, reading it
481 in if necessary. The next token is already read in. */
482
483 c_token *
484 c_parser_peek_2nd_token (c_parser *parser)
485 {
486 if (parser->tokens_avail >= 2)
487 return &parser->tokens[1];
488 gcc_assert (parser->tokens_avail == 1);
489 gcc_assert (parser->tokens[0].type != CPP_EOF);
490 gcc_assert (parser->tokens[0].type != CPP_PRAGMA_EOL);
491 c_lex_one_token (parser, &parser->tokens[1]);
492 parser->tokens_avail = 2;
493 return &parser->tokens[1];
494 }
495
496 /* Return a pointer to the Nth token from PARSER, reading it
497 in if necessary. The N-1th token is already read in. */
498
499 c_token *
500 c_parser_peek_nth_token (c_parser *parser, unsigned int n)
501 {
502 /* N is 1-based, not zero-based. */
503 gcc_assert (n > 0);
504
505 if (parser->tokens_avail >= n)
506 return &parser->tokens[n - 1];
507 gcc_assert (parser->tokens_avail == n - 1);
508 c_lex_one_token (parser, &parser->tokens[n - 1]);
509 parser->tokens_avail = n;
510 return &parser->tokens[n - 1];
511 }
512
513 /* Return a pointer to the Nth token from PARSER, reading it in as a
514 raw look-ahead token if necessary. The N-1th token is already read
515 in. Raw look-ahead tokens remain available for when the non-raw
516 functions above are called. */
517
518 c_token *
519 c_parser_peek_nth_token_raw (c_parser *parser, unsigned int n)
520 {
521 /* N is 1-based, not zero-based. */
522 gcc_assert (n > 0);
523
524 if (parser->tokens_avail >= n)
525 return &parser->tokens[n - 1];
526 unsigned int raw_len = vec_safe_length (parser->raw_tokens);
527 unsigned int raw_avail
528 = parser->tokens_avail + raw_len - parser->raw_tokens_used;
529 gcc_assert (raw_avail >= n - 1);
530 if (raw_avail >= n)
531 return &(*parser->raw_tokens)[parser->raw_tokens_used
532 + n - 1 - parser->tokens_avail];
533 vec_safe_reserve (parser->raw_tokens, 1);
534 parser->raw_tokens->quick_grow (raw_len + 1);
535 c_lex_one_token (parser, &(*parser->raw_tokens)[raw_len], true);
536 return &(*parser->raw_tokens)[raw_len];
537 }
538
539 bool
540 c_keyword_starts_typename (enum rid keyword)
541 {
542 switch (keyword)
543 {
544 case RID_UNSIGNED:
545 case RID_LONG:
546 case RID_SHORT:
547 case RID_SIGNED:
548 case RID_COMPLEX:
549 case RID_INT:
550 case RID_CHAR:
551 case RID_FLOAT:
552 case RID_DOUBLE:
553 case RID_VOID:
554 case RID_DFLOAT32:
555 case RID_DFLOAT64:
556 case RID_DFLOAT128:
557 CASE_RID_FLOATN_NX:
558 case RID_BOOL:
559 case RID_ENUM:
560 case RID_STRUCT:
561 case RID_UNION:
562 case RID_TYPEOF:
563 case RID_CONST:
564 case RID_ATOMIC:
565 case RID_VOLATILE:
566 case RID_RESTRICT:
567 case RID_ATTRIBUTE:
568 case RID_FRACT:
569 case RID_ACCUM:
570 case RID_SAT:
571 case RID_AUTO_TYPE:
572 case RID_ALIGNAS:
573 return true;
574 default:
575 if (keyword >= RID_FIRST_INT_N
576 && keyword < RID_FIRST_INT_N + NUM_INT_N_ENTS
577 && int_n_enabled_p[keyword - RID_FIRST_INT_N])
578 return true;
579 return false;
580 }
581 }
582
583 /* Return true if TOKEN can start a type name,
584 false otherwise. */
585 bool
586 c_token_starts_typename (c_token *token)
587 {
588 switch (token->type)
589 {
590 case CPP_NAME:
591 switch (token->id_kind)
592 {
593 case C_ID_ID:
594 return false;
595 case C_ID_ADDRSPACE:
596 return true;
597 case C_ID_TYPENAME:
598 return true;
599 case C_ID_CLASSNAME:
600 gcc_assert (c_dialect_objc ());
601 return true;
602 default:
603 gcc_unreachable ();
604 }
605 case CPP_KEYWORD:
606 return c_keyword_starts_typename (token->keyword);
607 case CPP_LESS:
608 if (c_dialect_objc ())
609 return true;
610 return false;
611 default:
612 return false;
613 }
614 }
615
616 /* Return true if the next token from PARSER can start a type name,
617 false otherwise. LA specifies how to do lookahead in order to
618 detect unknown type names. If unsure, pick CLA_PREFER_ID. */
619
620 static inline bool
621 c_parser_next_tokens_start_typename (c_parser *parser, enum c_lookahead_kind la)
622 {
623 c_token *token = c_parser_peek_token (parser);
624 if (c_token_starts_typename (token))
625 return true;
626
627 /* Try a bit harder to detect an unknown typename. */
628 if (la != cla_prefer_id
629 && token->type == CPP_NAME
630 && token->id_kind == C_ID_ID
631
632 /* Do not try too hard when we could have "object in array". */
633 && !parser->objc_could_be_foreach_context
634
635 && (la == cla_prefer_type
636 || c_parser_peek_2nd_token (parser)->type == CPP_NAME
637 || c_parser_peek_2nd_token (parser)->type == CPP_MULT)
638
639 /* Only unknown identifiers. */
640 && !lookup_name (token->value))
641 return true;
642
643 return false;
644 }
645
646 /* Return true if TOKEN is a type qualifier, false otherwise. */
647 static bool
648 c_token_is_qualifier (c_token *token)
649 {
650 switch (token->type)
651 {
652 case CPP_NAME:
653 switch (token->id_kind)
654 {
655 case C_ID_ADDRSPACE:
656 return true;
657 default:
658 return false;
659 }
660 case CPP_KEYWORD:
661 switch (token->keyword)
662 {
663 case RID_CONST:
664 case RID_VOLATILE:
665 case RID_RESTRICT:
666 case RID_ATTRIBUTE:
667 case RID_ATOMIC:
668 return true;
669 default:
670 return false;
671 }
672 case CPP_LESS:
673 return false;
674 default:
675 gcc_unreachable ();
676 }
677 }
678
679 /* Return true if the next token from PARSER is a type qualifier,
680 false otherwise. */
681 static inline bool
682 c_parser_next_token_is_qualifier (c_parser *parser)
683 {
684 c_token *token = c_parser_peek_token (parser);
685 return c_token_is_qualifier (token);
686 }
687
688 /* Return true if TOKEN can start declaration specifiers (not
689 including standard attributes), false otherwise. */
690 static bool
691 c_token_starts_declspecs (c_token *token)
692 {
693 switch (token->type)
694 {
695 case CPP_NAME:
696 switch (token->id_kind)
697 {
698 case C_ID_ID:
699 return false;
700 case C_ID_ADDRSPACE:
701 return true;
702 case C_ID_TYPENAME:
703 return true;
704 case C_ID_CLASSNAME:
705 gcc_assert (c_dialect_objc ());
706 return true;
707 default:
708 gcc_unreachable ();
709 }
710 case CPP_KEYWORD:
711 switch (token->keyword)
712 {
713 case RID_STATIC:
714 case RID_EXTERN:
715 case RID_REGISTER:
716 case RID_TYPEDEF:
717 case RID_INLINE:
718 case RID_NORETURN:
719 case RID_AUTO:
720 case RID_THREAD:
721 case RID_UNSIGNED:
722 case RID_LONG:
723 case RID_SHORT:
724 case RID_SIGNED:
725 case RID_COMPLEX:
726 case RID_INT:
727 case RID_CHAR:
728 case RID_FLOAT:
729 case RID_DOUBLE:
730 case RID_VOID:
731 case RID_DFLOAT32:
732 case RID_DFLOAT64:
733 case RID_DFLOAT128:
734 CASE_RID_FLOATN_NX:
735 case RID_BOOL:
736 case RID_ENUM:
737 case RID_STRUCT:
738 case RID_UNION:
739 case RID_TYPEOF:
740 case RID_CONST:
741 case RID_VOLATILE:
742 case RID_RESTRICT:
743 case RID_ATTRIBUTE:
744 case RID_FRACT:
745 case RID_ACCUM:
746 case RID_SAT:
747 case RID_ALIGNAS:
748 case RID_ATOMIC:
749 case RID_AUTO_TYPE:
750 return true;
751 default:
752 if (token->keyword >= RID_FIRST_INT_N
753 && token->keyword < RID_FIRST_INT_N + NUM_INT_N_ENTS
754 && int_n_enabled_p[token->keyword - RID_FIRST_INT_N])
755 return true;
756 return false;
757 }
758 case CPP_LESS:
759 if (c_dialect_objc ())
760 return true;
761 return false;
762 default:
763 return false;
764 }
765 }
766
767
768 /* Return true if TOKEN can start declaration specifiers (not
769 including standard attributes) or a static assertion, false
770 otherwise. */
771 static bool
772 c_token_starts_declaration (c_token *token)
773 {
774 if (c_token_starts_declspecs (token)
775 || token->keyword == RID_STATIC_ASSERT)
776 return true;
777 else
778 return false;
779 }
780
781 /* Return true if the next token from PARSER can start declaration
782 specifiers (not including standard attributes), false
783 otherwise. */
784 bool
785 c_parser_next_token_starts_declspecs (c_parser *parser)
786 {
787 c_token *token = c_parser_peek_token (parser);
788
789 /* In Objective-C, a classname normally starts a declspecs unless it
790 is immediately followed by a dot. In that case, it is the
791 Objective-C 2.0 "dot-syntax" for class objects, ie, calls the
792 setter/getter on the class. c_token_starts_declspecs() can't
793 differentiate between the two cases because it only checks the
794 current token, so we have a special check here. */
795 if (c_dialect_objc ()
796 && token->type == CPP_NAME
797 && token->id_kind == C_ID_CLASSNAME
798 && c_parser_peek_2nd_token (parser)->type == CPP_DOT)
799 return false;
800
801 return c_token_starts_declspecs (token);
802 }
803
804 /* Return true if the next tokens from PARSER can start declaration
805 specifiers (not including standard attributes) or a static
806 assertion, false otherwise. */
807 bool
808 c_parser_next_tokens_start_declaration (c_parser *parser)
809 {
810 c_token *token = c_parser_peek_token (parser);
811
812 /* Same as above. */
813 if (c_dialect_objc ()
814 && token->type == CPP_NAME
815 && token->id_kind == C_ID_CLASSNAME
816 && c_parser_peek_2nd_token (parser)->type == CPP_DOT)
817 return false;
818
819 /* Labels do not start declarations. */
820 if (token->type == CPP_NAME
821 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
822 return false;
823
824 if (c_token_starts_declaration (token))
825 return true;
826
827 if (c_parser_next_tokens_start_typename (parser, cla_nonabstract_decl))
828 return true;
829
830 return false;
831 }
832
833 /* Consume the next token from PARSER. */
834
835 void
836 c_parser_consume_token (c_parser *parser)
837 {
838 gcc_assert (parser->tokens_avail >= 1);
839 gcc_assert (parser->tokens[0].type != CPP_EOF);
840 gcc_assert (!parser->in_pragma || parser->tokens[0].type != CPP_PRAGMA_EOL);
841 gcc_assert (parser->error || parser->tokens[0].type != CPP_PRAGMA);
842 parser->last_token_location = parser->tokens[0].location;
843 if (parser->tokens != &parser->tokens_buf[0])
844 parser->tokens++;
845 else if (parser->tokens_avail >= 2)
846 {
847 parser->tokens[0] = parser->tokens[1];
848 if (parser->tokens_avail >= 3)
849 parser->tokens[1] = parser->tokens[2];
850 }
851 parser->tokens_avail--;
852 }
853
854 /* Expect the current token to be a #pragma. Consume it and remember
855 that we've begun parsing a pragma. */
856
857 static void
858 c_parser_consume_pragma (c_parser *parser)
859 {
860 gcc_assert (!parser->in_pragma);
861 gcc_assert (parser->tokens_avail >= 1);
862 gcc_assert (parser->tokens[0].type == CPP_PRAGMA);
863 if (parser->tokens != &parser->tokens_buf[0])
864 parser->tokens++;
865 else if (parser->tokens_avail >= 2)
866 {
867 parser->tokens[0] = parser->tokens[1];
868 if (parser->tokens_avail >= 3)
869 parser->tokens[1] = parser->tokens[2];
870 }
871 parser->tokens_avail--;
872 parser->in_pragma = true;
873 }
874
875 /* Update the global input_location from TOKEN. */
876 static inline void
877 c_parser_set_source_position_from_token (c_token *token)
878 {
879 if (token->type != CPP_EOF)
880 {
881 input_location = token->location;
882 }
883 }
884
885 /* Helper function for c_parser_error.
886 Having peeked a token of kind TOK1_KIND that might signify
887 a conflict marker, peek successor tokens to determine
888 if we actually do have a conflict marker.
889 Specifically, we consider a run of 7 '<', '=' or '>' characters
890 at the start of a line as a conflict marker.
891 These come through the lexer as three pairs and a single,
892 e.g. three CPP_LSHIFT ("<<") and a CPP_LESS ('<').
893 If it returns true, *OUT_LOC is written to with the location/range
894 of the marker. */
895
896 static bool
897 c_parser_peek_conflict_marker (c_parser *parser, enum cpp_ttype tok1_kind,
898 location_t *out_loc)
899 {
900 c_token *token2 = c_parser_peek_2nd_token (parser);
901 if (token2->type != tok1_kind)
902 return false;
903 c_token *token3 = c_parser_peek_nth_token (parser, 3);
904 if (token3->type != tok1_kind)
905 return false;
906 c_token *token4 = c_parser_peek_nth_token (parser, 4);
907 if (token4->type != conflict_marker_get_final_tok_kind (tok1_kind))
908 return false;
909
910 /* It must be at the start of the line. */
911 location_t start_loc = c_parser_peek_token (parser)->location;
912 if (LOCATION_COLUMN (start_loc) != 1)
913 return false;
914
915 /* We have a conflict marker. Construct a location of the form:
916 <<<<<<<
917 ^~~~~~~
918 with start == caret, finishing at the end of the marker. */
919 location_t finish_loc = get_finish (token4->location);
920 *out_loc = make_location (start_loc, start_loc, finish_loc);
921
922 return true;
923 }
924
925 /* Issue a diagnostic of the form
926 FILE:LINE: MESSAGE before TOKEN
927 where TOKEN is the next token in the input stream of PARSER.
928 MESSAGE (specified by the caller) is usually of the form "expected
929 OTHER-TOKEN".
930
931 Use RICHLOC as the location of the diagnostic.
932
933 Do not issue a diagnostic if still recovering from an error.
934
935 Return true iff an error was actually emitted.
936
937 ??? This is taken from the C++ parser, but building up messages in
938 this way is not i18n-friendly and some other approach should be
939 used. */
940
941 static bool
942 c_parser_error_richloc (c_parser *parser, const char *gmsgid,
943 rich_location *richloc)
944 {
945 c_token *token = c_parser_peek_token (parser);
946 if (parser->error)
947 return false;
948 parser->error = true;
949 if (!gmsgid)
950 return false;
951
952 /* If this is actually a conflict marker, report it as such. */
953 if (token->type == CPP_LSHIFT
954 || token->type == CPP_RSHIFT
955 || token->type == CPP_EQ_EQ)
956 {
957 location_t loc;
958 if (c_parser_peek_conflict_marker (parser, token->type, &loc))
959 {
960 error_at (loc, "version control conflict marker in file");
961 return true;
962 }
963 }
964
965 c_parse_error (gmsgid,
966 /* Because c_parse_error does not understand
967 CPP_KEYWORD, keywords are treated like
968 identifiers. */
969 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
970 /* ??? The C parser does not save the cpp flags of a
971 token, we need to pass 0 here and we will not get
972 the source spelling of some tokens but rather the
973 canonical spelling. */
974 token->value, /*flags=*/0, richloc);
975 return true;
976 }
977
978 /* As c_parser_error_richloc, but issue the message at the
979 location of PARSER's next token, or at input_location
980 if the next token is EOF. */
981
982 bool
983 c_parser_error (c_parser *parser, const char *gmsgid)
984 {
985 c_token *token = c_parser_peek_token (parser);
986 c_parser_set_source_position_from_token (token);
987 rich_location richloc (line_table, input_location);
988 return c_parser_error_richloc (parser, gmsgid, &richloc);
989 }
990
991 /* Some tokens naturally come in pairs e.g.'(' and ')'.
992 This class is for tracking such a matching pair of symbols.
993 In particular, it tracks the location of the first token,
994 so that if the second token is missing, we can highlight the
995 location of the first token when notifying the user about the
996 problem. */
997
998 template <typename traits_t>
999 class token_pair
1000 {
1001 public:
1002 /* token_pair's ctor. */
1003 token_pair () : m_open_loc (UNKNOWN_LOCATION) {}
1004
1005 /* If the next token is the opening symbol for this pair, consume it and
1006 return true.
1007 Otherwise, issue an error and return false.
1008 In either case, record the location of the opening token. */
1009
1010 bool require_open (c_parser *parser)
1011 {
1012 c_token *token = c_parser_peek_token (parser);
1013 if (token)
1014 m_open_loc = token->location;
1015
1016 return c_parser_require (parser, traits_t::open_token_type,
1017 traits_t::open_gmsgid);
1018 }
1019
1020 /* Consume the next token from PARSER, recording its location as
1021 that of the opening token within the pair. */
1022
1023 void consume_open (c_parser *parser)
1024 {
1025 c_token *token = c_parser_peek_token (parser);
1026 gcc_assert (token->type == traits_t::open_token_type);
1027 m_open_loc = token->location;
1028 c_parser_consume_token (parser);
1029 }
1030
1031 /* If the next token is the closing symbol for this pair, consume it
1032 and return true.
1033 Otherwise, issue an error, highlighting the location of the
1034 corresponding opening token, and return false. */
1035
1036 bool require_close (c_parser *parser) const
1037 {
1038 return c_parser_require (parser, traits_t::close_token_type,
1039 traits_t::close_gmsgid, m_open_loc);
1040 }
1041
1042 /* Like token_pair::require_close, except that tokens will be skipped
1043 until the desired token is found. An error message is still produced
1044 if the next token is not as expected. */
1045
1046 void skip_until_found_close (c_parser *parser) const
1047 {
1048 c_parser_skip_until_found (parser, traits_t::close_token_type,
1049 traits_t::close_gmsgid, m_open_loc);
1050 }
1051
1052 private:
1053 location_t m_open_loc;
1054 };
1055
1056 /* Traits for token_pair<T> for tracking matching pairs of parentheses. */
1057
1058 struct matching_paren_traits
1059 {
1060 static const enum cpp_ttype open_token_type = CPP_OPEN_PAREN;
1061 static const char * const open_gmsgid;
1062 static const enum cpp_ttype close_token_type = CPP_CLOSE_PAREN;
1063 static const char * const close_gmsgid;
1064 };
1065
1066 const char * const matching_paren_traits::open_gmsgid = "expected %<(%>";
1067 const char * const matching_paren_traits::close_gmsgid = "expected %<)%>";
1068
1069 /* "matching_parens" is a token_pair<T> class for tracking matching
1070 pairs of parentheses. */
1071
1072 typedef token_pair<matching_paren_traits> matching_parens;
1073
1074 /* Traits for token_pair<T> for tracking matching pairs of braces. */
1075
1076 struct matching_brace_traits
1077 {
1078 static const enum cpp_ttype open_token_type = CPP_OPEN_BRACE;
1079 static const char * const open_gmsgid;
1080 static const enum cpp_ttype close_token_type = CPP_CLOSE_BRACE;
1081 static const char * const close_gmsgid;
1082 };
1083
1084 const char * const matching_brace_traits::open_gmsgid = "expected %<{%>";
1085 const char * const matching_brace_traits::close_gmsgid = "expected %<}%>";
1086
1087 /* "matching_braces" is a token_pair<T> class for tracking matching
1088 pairs of braces. */
1089
1090 typedef token_pair<matching_brace_traits> matching_braces;
1091
1092 /* Get a description of the matching symbol to TYPE e.g. "(" for
1093 CPP_CLOSE_PAREN. */
1094
1095 static const char *
1096 get_matching_symbol (enum cpp_ttype type)
1097 {
1098 switch (type)
1099 {
1100 default:
1101 gcc_unreachable ();
1102 return "";
1103 case CPP_CLOSE_PAREN:
1104 return "(";
1105 case CPP_CLOSE_BRACE:
1106 return "{";
1107 }
1108 }
1109
1110 /* If the next token is of the indicated TYPE, consume it. Otherwise,
1111 issue the error MSGID. If MSGID is NULL then a message has already
1112 been produced and no message will be produced this time. Returns
1113 true if found, false otherwise.
1114
1115 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
1116 within any error as the location of an "opening" token matching
1117 the close token TYPE (e.g. the location of the '(' when TYPE is
1118 CPP_CLOSE_PAREN).
1119
1120 If TYPE_IS_UNIQUE is true (the default) then msgid describes exactly
1121 one type (e.g. "expected %<)%>") and thus it may be reasonable to
1122 attempt to generate a fix-it hint for the problem.
1123 Otherwise msgid describes multiple token types (e.g.
1124 "expected %<;%>, %<,%> or %<)%>"), and thus we shouldn't attempt to
1125 generate a fix-it hint. */
1126
1127 bool
1128 c_parser_require (c_parser *parser,
1129 enum cpp_ttype type,
1130 const char *msgid,
1131 location_t matching_location,
1132 bool type_is_unique)
1133 {
1134 if (c_parser_next_token_is (parser, type))
1135 {
1136 c_parser_consume_token (parser);
1137 return true;
1138 }
1139 else
1140 {
1141 location_t next_token_loc = c_parser_peek_token (parser)->location;
1142 gcc_rich_location richloc (next_token_loc);
1143
1144 /* Potentially supply a fix-it hint, suggesting to add the
1145 missing token immediately after the *previous* token.
1146 This may move the primary location within richloc. */
1147 if (!parser->error && type_is_unique)
1148 maybe_suggest_missing_token_insertion (&richloc, type,
1149 parser->last_token_location);
1150
1151 /* If matching_location != UNKNOWN_LOCATION, highlight it.
1152 Attempt to consolidate diagnostics by printing it as a
1153 secondary range within the main diagnostic. */
1154 bool added_matching_location = false;
1155 if (matching_location != UNKNOWN_LOCATION)
1156 added_matching_location
1157 = richloc.add_location_if_nearby (matching_location);
1158
1159 if (c_parser_error_richloc (parser, msgid, &richloc))
1160 /* If we weren't able to consolidate matching_location, then
1161 print it as a secondary diagnostic. */
1162 if (matching_location != UNKNOWN_LOCATION && !added_matching_location)
1163 inform (matching_location, "to match this %qs",
1164 get_matching_symbol (type));
1165
1166 return false;
1167 }
1168 }
1169
1170 /* If the next token is the indicated keyword, consume it. Otherwise,
1171 issue the error MSGID. Returns true if found, false otherwise. */
1172
1173 static bool
1174 c_parser_require_keyword (c_parser *parser,
1175 enum rid keyword,
1176 const char *msgid)
1177 {
1178 if (c_parser_next_token_is_keyword (parser, keyword))
1179 {
1180 c_parser_consume_token (parser);
1181 return true;
1182 }
1183 else
1184 {
1185 c_parser_error (parser, msgid);
1186 return false;
1187 }
1188 }
1189
1190 /* Like c_parser_require, except that tokens will be skipped until the
1191 desired token is found. An error message is still produced if the
1192 next token is not as expected. If MSGID is NULL then a message has
1193 already been produced and no message will be produced this
1194 time.
1195
1196 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
1197 within any error as the location of an "opening" token matching
1198 the close token TYPE (e.g. the location of the '(' when TYPE is
1199 CPP_CLOSE_PAREN). */
1200
1201 void
1202 c_parser_skip_until_found (c_parser *parser,
1203 enum cpp_ttype type,
1204 const char *msgid,
1205 location_t matching_location)
1206 {
1207 unsigned nesting_depth = 0;
1208
1209 if (c_parser_require (parser, type, msgid, matching_location))
1210 return;
1211
1212 /* Skip tokens until the desired token is found. */
1213 while (true)
1214 {
1215 /* Peek at the next token. */
1216 c_token *token = c_parser_peek_token (parser);
1217 /* If we've reached the token we want, consume it and stop. */
1218 if (token->type == type && !nesting_depth)
1219 {
1220 c_parser_consume_token (parser);
1221 break;
1222 }
1223
1224 /* If we've run out of tokens, stop. */
1225 if (token->type == CPP_EOF)
1226 return;
1227 if (token->type == CPP_PRAGMA_EOL && parser->in_pragma)
1228 return;
1229 if (token->type == CPP_OPEN_BRACE
1230 || token->type == CPP_OPEN_PAREN
1231 || token->type == CPP_OPEN_SQUARE)
1232 ++nesting_depth;
1233 else if (token->type == CPP_CLOSE_BRACE
1234 || token->type == CPP_CLOSE_PAREN
1235 || token->type == CPP_CLOSE_SQUARE)
1236 {
1237 if (nesting_depth-- == 0)
1238 break;
1239 }
1240 /* Consume this token. */
1241 c_parser_consume_token (parser);
1242 }
1243 parser->error = false;
1244 }
1245
1246 /* Skip tokens until the end of a parameter is found, but do not
1247 consume the comma, semicolon or closing delimiter. */
1248
1249 static void
1250 c_parser_skip_to_end_of_parameter (c_parser *parser)
1251 {
1252 unsigned nesting_depth = 0;
1253
1254 while (true)
1255 {
1256 c_token *token = c_parser_peek_token (parser);
1257 if ((token->type == CPP_COMMA || token->type == CPP_SEMICOLON)
1258 && !nesting_depth)
1259 break;
1260 /* If we've run out of tokens, stop. */
1261 if (token->type == CPP_EOF)
1262 return;
1263 if (token->type == CPP_PRAGMA_EOL && parser->in_pragma)
1264 return;
1265 if (token->type == CPP_OPEN_BRACE
1266 || token->type == CPP_OPEN_PAREN
1267 || token->type == CPP_OPEN_SQUARE)
1268 ++nesting_depth;
1269 else if (token->type == CPP_CLOSE_BRACE
1270 || token->type == CPP_CLOSE_PAREN
1271 || token->type == CPP_CLOSE_SQUARE)
1272 {
1273 if (nesting_depth-- == 0)
1274 break;
1275 }
1276 /* Consume this token. */
1277 c_parser_consume_token (parser);
1278 }
1279 parser->error = false;
1280 }
1281
1282 /* Expect to be at the end of the pragma directive and consume an
1283 end of line marker. */
1284
1285 static void
1286 c_parser_skip_to_pragma_eol (c_parser *parser, bool error_if_not_eol = true)
1287 {
1288 gcc_assert (parser->in_pragma);
1289 parser->in_pragma = false;
1290
1291 if (error_if_not_eol && c_parser_peek_token (parser)->type != CPP_PRAGMA_EOL)
1292 c_parser_error (parser, "expected end of line");
1293
1294 cpp_ttype token_type;
1295 do
1296 {
1297 c_token *token = c_parser_peek_token (parser);
1298 token_type = token->type;
1299 if (token_type == CPP_EOF)
1300 break;
1301 c_parser_consume_token (parser);
1302 }
1303 while (token_type != CPP_PRAGMA_EOL);
1304
1305 parser->error = false;
1306 }
1307
1308 /* Skip tokens until we have consumed an entire block, or until we
1309 have consumed a non-nested ';'. */
1310
1311 static void
1312 c_parser_skip_to_end_of_block_or_statement (c_parser *parser)
1313 {
1314 unsigned nesting_depth = 0;
1315 bool save_error = parser->error;
1316
1317 while (true)
1318 {
1319 c_token *token;
1320
1321 /* Peek at the next token. */
1322 token = c_parser_peek_token (parser);
1323
1324 switch (token->type)
1325 {
1326 case CPP_EOF:
1327 return;
1328
1329 case CPP_PRAGMA_EOL:
1330 if (parser->in_pragma)
1331 return;
1332 break;
1333
1334 case CPP_SEMICOLON:
1335 /* If the next token is a ';', we have reached the
1336 end of the statement. */
1337 if (!nesting_depth)
1338 {
1339 /* Consume the ';'. */
1340 c_parser_consume_token (parser);
1341 goto finished;
1342 }
1343 break;
1344
1345 case CPP_CLOSE_BRACE:
1346 /* If the next token is a non-nested '}', then we have
1347 reached the end of the current block. */
1348 if (nesting_depth == 0 || --nesting_depth == 0)
1349 {
1350 c_parser_consume_token (parser);
1351 goto finished;
1352 }
1353 break;
1354
1355 case CPP_OPEN_BRACE:
1356 /* If it the next token is a '{', then we are entering a new
1357 block. Consume the entire block. */
1358 ++nesting_depth;
1359 break;
1360
1361 case CPP_PRAGMA:
1362 /* If we see a pragma, consume the whole thing at once. We
1363 have some safeguards against consuming pragmas willy-nilly.
1364 Normally, we'd expect to be here with parser->error set,
1365 which disables these safeguards. But it's possible to get
1366 here for secondary error recovery, after parser->error has
1367 been cleared. */
1368 c_parser_consume_pragma (parser);
1369 c_parser_skip_to_pragma_eol (parser);
1370 parser->error = save_error;
1371 continue;
1372
1373 default:
1374 break;
1375 }
1376
1377 c_parser_consume_token (parser);
1378 }
1379
1380 finished:
1381 parser->error = false;
1382 }
1383
1384 /* CPP's options (initialized by c-opts.c). */
1385 extern cpp_options *cpp_opts;
1386
1387 /* Save the warning flags which are controlled by __extension__. */
1388
1389 static inline int
1390 disable_extension_diagnostics (void)
1391 {
1392 int ret = (pedantic
1393 | (warn_pointer_arith << 1)
1394 | (warn_traditional << 2)
1395 | (flag_iso << 3)
1396 | (warn_long_long << 4)
1397 | (warn_cxx_compat << 5)
1398 | (warn_overlength_strings << 6)
1399 /* warn_c90_c99_compat has three states: -1/0/1, so we must
1400 play tricks to properly restore it. */
1401 | ((warn_c90_c99_compat == 1) << 7)
1402 | ((warn_c90_c99_compat == -1) << 8)
1403 /* Similarly for warn_c99_c11_compat. */
1404 | ((warn_c99_c11_compat == 1) << 9)
1405 | ((warn_c99_c11_compat == -1) << 10)
1406 /* Similarly for warn_c11_c2x_compat. */
1407 | ((warn_c11_c2x_compat == 1) << 11)
1408 | ((warn_c11_c2x_compat == -1) << 12)
1409 );
1410 cpp_opts->cpp_pedantic = pedantic = 0;
1411 warn_pointer_arith = 0;
1412 cpp_opts->cpp_warn_traditional = warn_traditional = 0;
1413 flag_iso = 0;
1414 cpp_opts->cpp_warn_long_long = warn_long_long = 0;
1415 warn_cxx_compat = 0;
1416 warn_overlength_strings = 0;
1417 warn_c90_c99_compat = 0;
1418 warn_c99_c11_compat = 0;
1419 warn_c11_c2x_compat = 0;
1420 return ret;
1421 }
1422
1423 /* Restore the warning flags which are controlled by __extension__.
1424 FLAGS is the return value from disable_extension_diagnostics. */
1425
1426 static inline void
1427 restore_extension_diagnostics (int flags)
1428 {
1429 cpp_opts->cpp_pedantic = pedantic = flags & 1;
1430 warn_pointer_arith = (flags >> 1) & 1;
1431 cpp_opts->cpp_warn_traditional = warn_traditional = (flags >> 2) & 1;
1432 flag_iso = (flags >> 3) & 1;
1433 cpp_opts->cpp_warn_long_long = warn_long_long = (flags >> 4) & 1;
1434 warn_cxx_compat = (flags >> 5) & 1;
1435 warn_overlength_strings = (flags >> 6) & 1;
1436 /* See above for why is this needed. */
1437 warn_c90_c99_compat = (flags >> 7) & 1 ? 1 : ((flags >> 8) & 1 ? -1 : 0);
1438 warn_c99_c11_compat = (flags >> 9) & 1 ? 1 : ((flags >> 10) & 1 ? -1 : 0);
1439 warn_c11_c2x_compat = (flags >> 11) & 1 ? 1 : ((flags >> 12) & 1 ? -1 : 0);
1440 }
1441
1442 /* Helper data structure for parsing #pragma acc routine. */
1443 struct oacc_routine_data {
1444 bool error_seen; /* Set if error has been reported. */
1445 bool fndecl_seen; /* Set if one fn decl/definition has been seen already. */
1446 tree clauses;
1447 location_t loc;
1448 };
1449
1450 static bool c_parser_nth_token_starts_std_attributes (c_parser *,
1451 unsigned int);
1452 static tree c_parser_std_attribute_specifier_sequence (c_parser *);
1453 static void c_parser_external_declaration (c_parser *);
1454 static void c_parser_asm_definition (c_parser *);
1455 static void c_parser_declaration_or_fndef (c_parser *, bool, bool, bool,
1456 bool, bool, tree *, vec<c_token>,
1457 bool have_attrs = false,
1458 tree attrs = NULL,
1459 struct oacc_routine_data * = NULL,
1460 bool * = NULL);
1461 static void c_parser_static_assert_declaration_no_semi (c_parser *);
1462 static void c_parser_static_assert_declaration (c_parser *);
1463 static struct c_typespec c_parser_enum_specifier (c_parser *);
1464 static struct c_typespec c_parser_struct_or_union_specifier (c_parser *);
1465 static tree c_parser_struct_declaration (c_parser *);
1466 static struct c_typespec c_parser_typeof_specifier (c_parser *);
1467 static tree c_parser_alignas_specifier (c_parser *);
1468 static struct c_declarator *c_parser_direct_declarator (c_parser *, bool,
1469 c_dtr_syn, bool *);
1470 static struct c_declarator *c_parser_direct_declarator_inner (c_parser *,
1471 bool,
1472 struct c_declarator *);
1473 static struct c_arg_info *c_parser_parms_declarator (c_parser *, bool, tree,
1474 bool);
1475 static struct c_arg_info *c_parser_parms_list_declarator (c_parser *, tree,
1476 tree, bool);
1477 static struct c_parm *c_parser_parameter_declaration (c_parser *, tree, bool);
1478 static tree c_parser_simple_asm_expr (c_parser *);
1479 static tree c_parser_gnu_attributes (c_parser *);
1480 static struct c_expr c_parser_initializer (c_parser *);
1481 static struct c_expr c_parser_braced_init (c_parser *, tree, bool,
1482 struct obstack *);
1483 static void c_parser_initelt (c_parser *, struct obstack *);
1484 static void c_parser_initval (c_parser *, struct c_expr *,
1485 struct obstack *);
1486 static tree c_parser_compound_statement (c_parser *);
1487 static void c_parser_compound_statement_nostart (c_parser *);
1488 static void c_parser_label (c_parser *);
1489 static void c_parser_statement (c_parser *, bool *, location_t * = NULL);
1490 static void c_parser_statement_after_labels (c_parser *, bool *,
1491 vec<tree> * = NULL);
1492 static tree c_parser_c99_block_statement (c_parser *, bool *,
1493 location_t * = NULL);
1494 static void c_parser_if_statement (c_parser *, bool *, vec<tree> *);
1495 static void c_parser_switch_statement (c_parser *, bool *);
1496 static void c_parser_while_statement (c_parser *, bool, unsigned short, bool *);
1497 static void c_parser_do_statement (c_parser *, bool, unsigned short);
1498 static void c_parser_for_statement (c_parser *, bool, unsigned short, bool *);
1499 static tree c_parser_asm_statement (c_parser *);
1500 static tree c_parser_asm_operands (c_parser *);
1501 static tree c_parser_asm_goto_operands (c_parser *);
1502 static tree c_parser_asm_clobbers (c_parser *);
1503 static struct c_expr c_parser_expr_no_commas (c_parser *, struct c_expr *,
1504 tree = NULL_TREE);
1505 static struct c_expr c_parser_conditional_expression (c_parser *,
1506 struct c_expr *, tree);
1507 static struct c_expr c_parser_binary_expression (c_parser *, struct c_expr *,
1508 tree);
1509 static struct c_expr c_parser_cast_expression (c_parser *, struct c_expr *);
1510 static struct c_expr c_parser_unary_expression (c_parser *);
1511 static struct c_expr c_parser_sizeof_expression (c_parser *);
1512 static struct c_expr c_parser_alignof_expression (c_parser *);
1513 static struct c_expr c_parser_postfix_expression (c_parser *);
1514 static struct c_expr c_parser_postfix_expression_after_paren_type (c_parser *,
1515 struct c_type_name *,
1516 location_t);
1517 static struct c_expr c_parser_postfix_expression_after_primary (c_parser *,
1518 location_t loc,
1519 struct c_expr);
1520 static tree c_parser_transaction (c_parser *, enum rid);
1521 static struct c_expr c_parser_transaction_expression (c_parser *, enum rid);
1522 static tree c_parser_transaction_cancel (c_parser *);
1523 static struct c_expr c_parser_expression (c_parser *);
1524 static struct c_expr c_parser_expression_conv (c_parser *);
1525 static vec<tree, va_gc> *c_parser_expr_list (c_parser *, bool, bool,
1526 vec<tree, va_gc> **, location_t *,
1527 tree *, vec<location_t> *,
1528 unsigned int * = NULL);
1529 static struct c_expr c_parser_has_attribute_expression (c_parser *);
1530
1531 static void c_parser_oacc_declare (c_parser *);
1532 static void c_parser_oacc_enter_exit_data (c_parser *, bool);
1533 static void c_parser_oacc_update (c_parser *);
1534 static void c_parser_omp_construct (c_parser *, bool *);
1535 static void c_parser_omp_threadprivate (c_parser *);
1536 static void c_parser_omp_barrier (c_parser *);
1537 static void c_parser_omp_depobj (c_parser *);
1538 static void c_parser_omp_flush (c_parser *);
1539 static tree c_parser_omp_for_loop (location_t, c_parser *, enum tree_code,
1540 tree, tree *, bool *);
1541 static void c_parser_omp_taskwait (c_parser *);
1542 static void c_parser_omp_taskyield (c_parser *);
1543 static void c_parser_omp_cancel (c_parser *);
1544
1545 enum pragma_context { pragma_external, pragma_struct, pragma_param,
1546 pragma_stmt, pragma_compound };
1547 static bool c_parser_pragma (c_parser *, enum pragma_context, bool *);
1548 static void c_parser_omp_cancellation_point (c_parser *, enum pragma_context);
1549 static bool c_parser_omp_target (c_parser *, enum pragma_context, bool *);
1550 static void c_parser_omp_end_declare_target (c_parser *);
1551 static void c_parser_omp_declare (c_parser *, enum pragma_context);
1552 static void c_parser_omp_requires (c_parser *);
1553 static bool c_parser_omp_ordered (c_parser *, enum pragma_context, bool *);
1554 static void c_parser_oacc_routine (c_parser *, enum pragma_context);
1555
1556 /* These Objective-C parser functions are only ever called when
1557 compiling Objective-C. */
1558 static void c_parser_objc_class_definition (c_parser *, tree);
1559 static void c_parser_objc_class_instance_variables (c_parser *);
1560 static void c_parser_objc_class_declaration (c_parser *);
1561 static void c_parser_objc_alias_declaration (c_parser *);
1562 static void c_parser_objc_protocol_definition (c_parser *, tree);
1563 static bool c_parser_objc_method_type (c_parser *);
1564 static void c_parser_objc_method_definition (c_parser *);
1565 static void c_parser_objc_methodprotolist (c_parser *);
1566 static void c_parser_objc_methodproto (c_parser *);
1567 static tree c_parser_objc_method_decl (c_parser *, bool, tree *, tree *);
1568 static tree c_parser_objc_type_name (c_parser *);
1569 static tree c_parser_objc_protocol_refs (c_parser *);
1570 static void c_parser_objc_try_catch_finally_statement (c_parser *);
1571 static void c_parser_objc_synchronized_statement (c_parser *);
1572 static tree c_parser_objc_selector (c_parser *);
1573 static tree c_parser_objc_selector_arg (c_parser *);
1574 static tree c_parser_objc_receiver (c_parser *);
1575 static tree c_parser_objc_message_args (c_parser *);
1576 static tree c_parser_objc_keywordexpr (c_parser *);
1577 static void c_parser_objc_at_property_declaration (c_parser *);
1578 static void c_parser_objc_at_synthesize_declaration (c_parser *);
1579 static void c_parser_objc_at_dynamic_declaration (c_parser *);
1580 static bool c_parser_objc_diagnose_bad_element_prefix
1581 (c_parser *, struct c_declspecs *);
1582
1583 static void c_parser_parse_rtl_body (c_parser *parser, char *start_with_pass);
1584
1585 /* Parse a translation unit (C90 6.7, C99 6.9, C11 6.9).
1586
1587 translation-unit:
1588 external-declarations
1589
1590 external-declarations:
1591 external-declaration
1592 external-declarations external-declaration
1593
1594 GNU extensions:
1595
1596 translation-unit:
1597 empty
1598 */
1599
1600 static void
1601 c_parser_translation_unit (c_parser *parser)
1602 {
1603 if (c_parser_next_token_is (parser, CPP_EOF))
1604 {
1605 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
1606 "ISO C forbids an empty translation unit");
1607 }
1608 else
1609 {
1610 void *obstack_position = obstack_alloc (&parser_obstack, 0);
1611 mark_valid_location_for_stdc_pragma (false);
1612 do
1613 {
1614 ggc_collect ();
1615 c_parser_external_declaration (parser);
1616 obstack_free (&parser_obstack, obstack_position);
1617 }
1618 while (c_parser_next_token_is_not (parser, CPP_EOF));
1619 }
1620
1621 unsigned int i;
1622 tree decl;
1623 FOR_EACH_VEC_ELT (incomplete_record_decls, i, decl)
1624 if (DECL_SIZE (decl) == NULL_TREE && TREE_TYPE (decl) != error_mark_node)
1625 error ("storage size of %q+D isn%'t known", decl);
1626
1627 if (current_omp_declare_target_attribute)
1628 {
1629 if (!errorcount)
1630 error ("%<#pragma omp declare target%> without corresponding "
1631 "%<#pragma omp end declare target%>");
1632 current_omp_declare_target_attribute = 0;
1633 }
1634 }
1635
1636 /* Parse an external declaration (C90 6.7, C99 6.9, C11 6.9).
1637
1638 external-declaration:
1639 function-definition
1640 declaration
1641
1642 GNU extensions:
1643
1644 external-declaration:
1645 asm-definition
1646 ;
1647 __extension__ external-declaration
1648
1649 Objective-C:
1650
1651 external-declaration:
1652 objc-class-definition
1653 objc-class-declaration
1654 objc-alias-declaration
1655 objc-protocol-definition
1656 objc-method-definition
1657 @end
1658 */
1659
1660 static void
1661 c_parser_external_declaration (c_parser *parser)
1662 {
1663 int ext;
1664 switch (c_parser_peek_token (parser)->type)
1665 {
1666 case CPP_KEYWORD:
1667 switch (c_parser_peek_token (parser)->keyword)
1668 {
1669 case RID_EXTENSION:
1670 ext = disable_extension_diagnostics ();
1671 c_parser_consume_token (parser);
1672 c_parser_external_declaration (parser);
1673 restore_extension_diagnostics (ext);
1674 break;
1675 case RID_ASM:
1676 c_parser_asm_definition (parser);
1677 break;
1678 case RID_AT_INTERFACE:
1679 case RID_AT_IMPLEMENTATION:
1680 gcc_assert (c_dialect_objc ());
1681 c_parser_objc_class_definition (parser, NULL_TREE);
1682 break;
1683 case RID_AT_CLASS:
1684 gcc_assert (c_dialect_objc ());
1685 c_parser_objc_class_declaration (parser);
1686 break;
1687 case RID_AT_ALIAS:
1688 gcc_assert (c_dialect_objc ());
1689 c_parser_objc_alias_declaration (parser);
1690 break;
1691 case RID_AT_PROTOCOL:
1692 gcc_assert (c_dialect_objc ());
1693 c_parser_objc_protocol_definition (parser, NULL_TREE);
1694 break;
1695 case RID_AT_PROPERTY:
1696 gcc_assert (c_dialect_objc ());
1697 c_parser_objc_at_property_declaration (parser);
1698 break;
1699 case RID_AT_SYNTHESIZE:
1700 gcc_assert (c_dialect_objc ());
1701 c_parser_objc_at_synthesize_declaration (parser);
1702 break;
1703 case RID_AT_DYNAMIC:
1704 gcc_assert (c_dialect_objc ());
1705 c_parser_objc_at_dynamic_declaration (parser);
1706 break;
1707 case RID_AT_END:
1708 gcc_assert (c_dialect_objc ());
1709 c_parser_consume_token (parser);
1710 objc_finish_implementation ();
1711 break;
1712 default:
1713 goto decl_or_fndef;
1714 }
1715 break;
1716 case CPP_SEMICOLON:
1717 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
1718 "ISO C does not allow extra %<;%> outside of a function");
1719 c_parser_consume_token (parser);
1720 break;
1721 case CPP_PRAGMA:
1722 mark_valid_location_for_stdc_pragma (true);
1723 c_parser_pragma (parser, pragma_external, NULL);
1724 mark_valid_location_for_stdc_pragma (false);
1725 break;
1726 case CPP_PLUS:
1727 case CPP_MINUS:
1728 if (c_dialect_objc ())
1729 {
1730 c_parser_objc_method_definition (parser);
1731 break;
1732 }
1733 /* Else fall through, and yield a syntax error trying to parse
1734 as a declaration or function definition. */
1735 /* FALLTHRU */
1736 default:
1737 decl_or_fndef:
1738 /* A declaration or a function definition (or, in Objective-C,
1739 an @interface or @protocol with prefix attributes). We can
1740 only tell which after parsing the declaration specifiers, if
1741 any, and the first declarator. */
1742 c_parser_declaration_or_fndef (parser, true, true, true, false, true,
1743 NULL, vNULL);
1744 break;
1745 }
1746 }
1747
1748 static void c_finish_omp_declare_simd (c_parser *, tree, tree, vec<c_token>);
1749 static void c_finish_oacc_routine (struct oacc_routine_data *, tree, bool);
1750
1751 /* Build and add a DEBUG_BEGIN_STMT statement with location LOC. */
1752
1753 static void
1754 add_debug_begin_stmt (location_t loc)
1755 {
1756 /* Don't add DEBUG_BEGIN_STMTs outside of functions, see PR84721. */
1757 if (!MAY_HAVE_DEBUG_MARKER_STMTS || !building_stmt_list_p ())
1758 return;
1759
1760 tree stmt = build0 (DEBUG_BEGIN_STMT, void_type_node);
1761 SET_EXPR_LOCATION (stmt, loc);
1762 add_stmt (stmt);
1763 }
1764
1765 /* Parse a declaration or function definition (C90 6.5, 6.7.1, C99
1766 6.7, 6.9.1, C11 6.7, 6.9.1). If FNDEF_OK is true, a function definition
1767 is accepted; otherwise (old-style parameter declarations) only other
1768 declarations are accepted. If STATIC_ASSERT_OK is true, a static
1769 assertion is accepted; otherwise (old-style parameter declarations)
1770 it is not. If NESTED is true, we are inside a function or parsing
1771 old-style parameter declarations; any functions encountered are
1772 nested functions and declaration specifiers are required; otherwise
1773 we are at top level and functions are normal functions and
1774 declaration specifiers may be optional. If EMPTY_OK is true, empty
1775 declarations are OK (subject to all other constraints); otherwise
1776 (old-style parameter declarations) they are diagnosed. If
1777 START_ATTR_OK is true, the declaration specifiers may start with
1778 attributes (GNU or standard); otherwise they may not.
1779 OBJC_FOREACH_OBJECT_DECLARATION can be used to get back the parsed
1780 declaration when parsing an Objective-C foreach statement.
1781 FALLTHRU_ATTR_P is used to signal whether this function parsed
1782 "__attribute__((fallthrough));". ATTRS are any standard attributes
1783 parsed in the caller (in contexts where such attributes had to be
1784 parsed to determine whether what follows is a declaration or a
1785 statement); HAVE_ATTRS says whether there were any such attributes
1786 (even empty).
1787
1788 declaration:
1789 declaration-specifiers init-declarator-list[opt] ;
1790 static_assert-declaration
1791
1792 function-definition:
1793 declaration-specifiers[opt] declarator declaration-list[opt]
1794 compound-statement
1795
1796 declaration-list:
1797 declaration
1798 declaration-list declaration
1799
1800 init-declarator-list:
1801 init-declarator
1802 init-declarator-list , init-declarator
1803
1804 init-declarator:
1805 declarator simple-asm-expr[opt] gnu-attributes[opt]
1806 declarator simple-asm-expr[opt] gnu-attributes[opt] = initializer
1807
1808 GNU extensions:
1809
1810 nested-function-definition:
1811 declaration-specifiers declarator declaration-list[opt]
1812 compound-statement
1813
1814 attribute ;
1815
1816 Objective-C:
1817 gnu-attributes objc-class-definition
1818 gnu-attributes objc-category-definition
1819 gnu-attributes objc-protocol-definition
1820
1821 The simple-asm-expr and gnu-attributes are GNU extensions.
1822
1823 This function does not handle __extension__; that is handled in its
1824 callers. ??? Following the old parser, __extension__ may start
1825 external declarations, declarations in functions and declarations
1826 at the start of "for" loops, but not old-style parameter
1827 declarations.
1828
1829 C99 requires declaration specifiers in a function definition; the
1830 absence is diagnosed through the diagnosis of implicit int. In GNU
1831 C we also allow but diagnose declarations without declaration
1832 specifiers, but only at top level (elsewhere they conflict with
1833 other syntax).
1834
1835 In Objective-C, declarations of the looping variable in a foreach
1836 statement are exceptionally terminated by 'in' (for example, 'for
1837 (NSObject *object in array) { ... }').
1838
1839 OpenMP:
1840
1841 declaration:
1842 threadprivate-directive
1843
1844 GIMPLE:
1845
1846 gimple-function-definition:
1847 declaration-specifiers[opt] __GIMPLE (gimple-or-rtl-pass-list) declarator
1848 declaration-list[opt] compound-statement
1849
1850 rtl-function-definition:
1851 declaration-specifiers[opt] __RTL (gimple-or-rtl-pass-list) declarator
1852 declaration-list[opt] compound-statement */
1853
1854 static void
1855 c_parser_declaration_or_fndef (c_parser *parser, bool fndef_ok,
1856 bool static_assert_ok, bool empty_ok,
1857 bool nested, bool start_attr_ok,
1858 tree *objc_foreach_object_declaration,
1859 vec<c_token> omp_declare_simd_clauses,
1860 bool have_attrs, tree attrs,
1861 struct oacc_routine_data *oacc_routine_data,
1862 bool *fallthru_attr_p)
1863 {
1864 struct c_declspecs *specs;
1865 tree prefix_attrs;
1866 tree all_prefix_attrs;
1867 bool diagnosed_no_specs = false;
1868 location_t here = c_parser_peek_token (parser)->location;
1869
1870 add_debug_begin_stmt (c_parser_peek_token (parser)->location);
1871
1872 if (static_assert_ok
1873 && c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT))
1874 {
1875 c_parser_static_assert_declaration (parser);
1876 return;
1877 }
1878 specs = build_null_declspecs ();
1879
1880 /* Handle any standard attributes parsed in the caller. */
1881 if (have_attrs)
1882 {
1883 declspecs_add_attrs (here, specs, attrs);
1884 specs->non_std_attrs_seen_p = false;
1885 }
1886
1887 /* Try to detect an unknown type name when we have "A B" or "A *B". */
1888 if (c_parser_peek_token (parser)->type == CPP_NAME
1889 && c_parser_peek_token (parser)->id_kind == C_ID_ID
1890 && (c_parser_peek_2nd_token (parser)->type == CPP_NAME
1891 || c_parser_peek_2nd_token (parser)->type == CPP_MULT)
1892 && (!nested || !lookup_name (c_parser_peek_token (parser)->value)))
1893 {
1894 tree name = c_parser_peek_token (parser)->value;
1895
1896 /* Issue a warning about NAME being an unknown type name, perhaps
1897 with some kind of hint.
1898 If the user forgot a "struct" etc, suggest inserting
1899 it. Otherwise, attempt to look for misspellings. */
1900 gcc_rich_location richloc (here);
1901 if (tag_exists_p (RECORD_TYPE, name))
1902 {
1903 /* This is not C++ with its implicit typedef. */
1904 richloc.add_fixit_insert_before ("struct ");
1905 error_at (&richloc,
1906 "unknown type name %qE;"
1907 " use %<struct%> keyword to refer to the type",
1908 name);
1909 }
1910 else if (tag_exists_p (UNION_TYPE, name))
1911 {
1912 richloc.add_fixit_insert_before ("union ");
1913 error_at (&richloc,
1914 "unknown type name %qE;"
1915 " use %<union%> keyword to refer to the type",
1916 name);
1917 }
1918 else if (tag_exists_p (ENUMERAL_TYPE, name))
1919 {
1920 richloc.add_fixit_insert_before ("enum ");
1921 error_at (&richloc,
1922 "unknown type name %qE;"
1923 " use %<enum%> keyword to refer to the type",
1924 name);
1925 }
1926 else
1927 {
1928 auto_diagnostic_group d;
1929 name_hint hint = lookup_name_fuzzy (name, FUZZY_LOOKUP_TYPENAME,
1930 here);
1931 if (const char *suggestion = hint.suggestion ())
1932 {
1933 richloc.add_fixit_replace (suggestion);
1934 error_at (&richloc,
1935 "unknown type name %qE; did you mean %qs?",
1936 name, suggestion);
1937 }
1938 else
1939 error_at (here, "unknown type name %qE", name);
1940 }
1941
1942 /* Parse declspecs normally to get a correct pointer type, but avoid
1943 a further "fails to be a type name" error. Refuse nested functions
1944 since it is not how the user likely wants us to recover. */
1945 c_parser_peek_token (parser)->type = CPP_KEYWORD;
1946 c_parser_peek_token (parser)->keyword = RID_VOID;
1947 c_parser_peek_token (parser)->value = error_mark_node;
1948 fndef_ok = !nested;
1949 }
1950
1951 /* When there are standard attributes at the start of the
1952 declaration (to apply to the entity being declared), an
1953 init-declarator-list or function definition must be present. */
1954 if (c_parser_nth_token_starts_std_attributes (parser, 1))
1955 have_attrs = true;
1956
1957 c_parser_declspecs (parser, specs, true, true, start_attr_ok,
1958 true, true, start_attr_ok, true, cla_nonabstract_decl);
1959 if (parser->error)
1960 {
1961 c_parser_skip_to_end_of_block_or_statement (parser);
1962 return;
1963 }
1964 if (nested && !specs->declspecs_seen_p)
1965 {
1966 c_parser_error (parser, "expected declaration specifiers");
1967 c_parser_skip_to_end_of_block_or_statement (parser);
1968 return;
1969 }
1970
1971 finish_declspecs (specs);
1972 bool auto_type_p = specs->typespec_word == cts_auto_type;
1973 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1974 {
1975 if (auto_type_p)
1976 error_at (here, "%<__auto_type%> in empty declaration");
1977 else if (specs->typespec_kind == ctsk_none
1978 && attribute_fallthrough_p (specs->attrs))
1979 {
1980 if (fallthru_attr_p != NULL)
1981 *fallthru_attr_p = true;
1982 if (nested)
1983 {
1984 tree fn = build_call_expr_internal_loc (here, IFN_FALLTHROUGH,
1985 void_type_node, 0);
1986 add_stmt (fn);
1987 }
1988 else
1989 pedwarn (here, OPT_Wattributes,
1990 "%<fallthrough%> attribute at top level");
1991 }
1992 else if (empty_ok && !(have_attrs
1993 && specs->non_std_attrs_seen_p))
1994 shadow_tag (specs);
1995 else
1996 {
1997 shadow_tag_warned (specs, 1);
1998 pedwarn (here, 0, "empty declaration");
1999 }
2000 c_parser_consume_token (parser);
2001 if (oacc_routine_data)
2002 c_finish_oacc_routine (oacc_routine_data, NULL_TREE, false);
2003 return;
2004 }
2005
2006 /* Provide better error recovery. Note that a type name here is usually
2007 better diagnosed as a redeclaration. */
2008 if (empty_ok
2009 && specs->typespec_kind == ctsk_tagdef
2010 && c_parser_next_token_starts_declspecs (parser)
2011 && !c_parser_next_token_is (parser, CPP_NAME))
2012 {
2013 c_parser_error (parser, "expected %<;%>, identifier or %<(%>");
2014 parser->error = false;
2015 shadow_tag_warned (specs, 1);
2016 return;
2017 }
2018 else if (c_dialect_objc () && !auto_type_p)
2019 {
2020 /* Prefix attributes are an error on method decls. */
2021 switch (c_parser_peek_token (parser)->type)
2022 {
2023 case CPP_PLUS:
2024 case CPP_MINUS:
2025 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
2026 return;
2027 if (specs->attrs)
2028 {
2029 warning_at (c_parser_peek_token (parser)->location,
2030 OPT_Wattributes,
2031 "prefix attributes are ignored for methods");
2032 specs->attrs = NULL_TREE;
2033 }
2034 if (fndef_ok)
2035 c_parser_objc_method_definition (parser);
2036 else
2037 c_parser_objc_methodproto (parser);
2038 return;
2039 break;
2040 default:
2041 break;
2042 }
2043 /* This is where we parse 'attributes @interface ...',
2044 'attributes @implementation ...', 'attributes @protocol ...'
2045 (where attributes could be, for example, __attribute__
2046 ((deprecated)).
2047 */
2048 switch (c_parser_peek_token (parser)->keyword)
2049 {
2050 case RID_AT_INTERFACE:
2051 {
2052 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
2053 return;
2054 c_parser_objc_class_definition (parser, specs->attrs);
2055 return;
2056 }
2057 break;
2058 case RID_AT_IMPLEMENTATION:
2059 {
2060 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
2061 return;
2062 if (specs->attrs)
2063 {
2064 warning_at (c_parser_peek_token (parser)->location,
2065 OPT_Wattributes,
2066 "prefix attributes are ignored for implementations");
2067 specs->attrs = NULL_TREE;
2068 }
2069 c_parser_objc_class_definition (parser, NULL_TREE);
2070 return;
2071 }
2072 break;
2073 case RID_AT_PROTOCOL:
2074 {
2075 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
2076 return;
2077 c_parser_objc_protocol_definition (parser, specs->attrs);
2078 return;
2079 }
2080 break;
2081 case RID_AT_ALIAS:
2082 case RID_AT_CLASS:
2083 case RID_AT_END:
2084 case RID_AT_PROPERTY:
2085 if (specs->attrs)
2086 {
2087 c_parser_error (parser, "unexpected attribute");
2088 specs->attrs = NULL;
2089 }
2090 break;
2091 default:
2092 break;
2093 }
2094 }
2095 else if (attribute_fallthrough_p (specs->attrs))
2096 warning_at (here, OPT_Wattributes,
2097 "%<fallthrough%> attribute not followed by %<;%>");
2098
2099 pending_xref_error ();
2100 prefix_attrs = specs->attrs;
2101 all_prefix_attrs = prefix_attrs;
2102 specs->attrs = NULL_TREE;
2103 while (true)
2104 {
2105 struct c_declarator *declarator;
2106 bool dummy = false;
2107 timevar_id_t tv;
2108 tree fnbody = NULL_TREE;
2109 /* Declaring either one or more declarators (in which case we
2110 should diagnose if there were no declaration specifiers) or a
2111 function definition (in which case the diagnostic for
2112 implicit int suffices). */
2113 declarator = c_parser_declarator (parser,
2114 specs->typespec_kind != ctsk_none,
2115 C_DTR_NORMAL, &dummy);
2116 if (declarator == NULL)
2117 {
2118 if (omp_declare_simd_clauses.exists ())
2119 c_finish_omp_declare_simd (parser, NULL_TREE, NULL_TREE,
2120 omp_declare_simd_clauses);
2121 if (oacc_routine_data)
2122 c_finish_oacc_routine (oacc_routine_data, NULL_TREE, false);
2123 c_parser_skip_to_end_of_block_or_statement (parser);
2124 return;
2125 }
2126 if (auto_type_p && declarator->kind != cdk_id)
2127 {
2128 error_at (here,
2129 "%<__auto_type%> requires a plain identifier"
2130 " as declarator");
2131 c_parser_skip_to_end_of_block_or_statement (parser);
2132 return;
2133 }
2134 if (c_parser_next_token_is (parser, CPP_EQ)
2135 || c_parser_next_token_is (parser, CPP_COMMA)
2136 || c_parser_next_token_is (parser, CPP_SEMICOLON)
2137 || c_parser_next_token_is_keyword (parser, RID_ASM)
2138 || c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE)
2139 || c_parser_next_token_is_keyword (parser, RID_IN))
2140 {
2141 tree asm_name = NULL_TREE;
2142 tree postfix_attrs = NULL_TREE;
2143 if (!diagnosed_no_specs && !specs->declspecs_seen_p)
2144 {
2145 diagnosed_no_specs = true;
2146 pedwarn (here, 0, "data definition has no type or storage class");
2147 }
2148 /* Having seen a data definition, there cannot now be a
2149 function definition. */
2150 fndef_ok = false;
2151 if (c_parser_next_token_is_keyword (parser, RID_ASM))
2152 asm_name = c_parser_simple_asm_expr (parser);
2153 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2154 {
2155 postfix_attrs = c_parser_gnu_attributes (parser);
2156 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
2157 {
2158 /* This means there is an attribute specifier after
2159 the declarator in a function definition. Provide
2160 some more information for the user. */
2161 error_at (here, "attributes should be specified before the "
2162 "declarator in a function definition");
2163 c_parser_skip_to_end_of_block_or_statement (parser);
2164 return;
2165 }
2166 }
2167 if (c_parser_next_token_is (parser, CPP_EQ))
2168 {
2169 tree d;
2170 struct c_expr init;
2171 location_t init_loc;
2172 c_parser_consume_token (parser);
2173 if (auto_type_p)
2174 {
2175 init_loc = c_parser_peek_token (parser)->location;
2176 rich_location richloc (line_table, init_loc);
2177 start_init (NULL_TREE, asm_name, global_bindings_p (), &richloc);
2178 /* A parameter is initialized, which is invalid. Don't
2179 attempt to instrument the initializer. */
2180 int flag_sanitize_save = flag_sanitize;
2181 if (nested && !empty_ok)
2182 flag_sanitize = 0;
2183 init = c_parser_expr_no_commas (parser, NULL);
2184 flag_sanitize = flag_sanitize_save;
2185 if (TREE_CODE (init.value) == COMPONENT_REF
2186 && DECL_C_BIT_FIELD (TREE_OPERAND (init.value, 1)))
2187 error_at (here,
2188 "%<__auto_type%> used with a bit-field"
2189 " initializer");
2190 init = convert_lvalue_to_rvalue (init_loc, init, true, true);
2191 tree init_type = TREE_TYPE (init.value);
2192 /* As with typeof, remove all qualifiers from atomic types. */
2193 if (init_type != error_mark_node && TYPE_ATOMIC (init_type))
2194 init_type
2195 = c_build_qualified_type (init_type, TYPE_UNQUALIFIED);
2196 bool vm_type = variably_modified_type_p (init_type,
2197 NULL_TREE);
2198 if (vm_type)
2199 init.value = save_expr (init.value);
2200 finish_init ();
2201 specs->typespec_kind = ctsk_typeof;
2202 specs->locations[cdw_typedef] = init_loc;
2203 specs->typedef_p = true;
2204 specs->type = init_type;
2205 if (vm_type)
2206 {
2207 bool maybe_const = true;
2208 tree type_expr = c_fully_fold (init.value, false,
2209 &maybe_const);
2210 specs->expr_const_operands &= maybe_const;
2211 if (specs->expr)
2212 specs->expr = build2 (COMPOUND_EXPR,
2213 TREE_TYPE (type_expr),
2214 specs->expr, type_expr);
2215 else
2216 specs->expr = type_expr;
2217 }
2218 d = start_decl (declarator, specs, true,
2219 chainon (postfix_attrs, all_prefix_attrs));
2220 if (!d)
2221 d = error_mark_node;
2222 if (omp_declare_simd_clauses.exists ())
2223 c_finish_omp_declare_simd (parser, d, NULL_TREE,
2224 omp_declare_simd_clauses);
2225 }
2226 else
2227 {
2228 /* The declaration of the variable is in effect while
2229 its initializer is parsed. */
2230 d = start_decl (declarator, specs, true,
2231 chainon (postfix_attrs, all_prefix_attrs));
2232 if (!d)
2233 d = error_mark_node;
2234 if (omp_declare_simd_clauses.exists ())
2235 c_finish_omp_declare_simd (parser, d, NULL_TREE,
2236 omp_declare_simd_clauses);
2237 init_loc = c_parser_peek_token (parser)->location;
2238 rich_location richloc (line_table, init_loc);
2239 start_init (d, asm_name, global_bindings_p (), &richloc);
2240 /* A parameter is initialized, which is invalid. Don't
2241 attempt to instrument the initializer. */
2242 int flag_sanitize_save = flag_sanitize;
2243 if (TREE_CODE (d) == PARM_DECL)
2244 flag_sanitize = 0;
2245 init = c_parser_initializer (parser);
2246 flag_sanitize = flag_sanitize_save;
2247 finish_init ();
2248 }
2249 if (oacc_routine_data)
2250 c_finish_oacc_routine (oacc_routine_data, d, false);
2251 if (d != error_mark_node)
2252 {
2253 maybe_warn_string_init (init_loc, TREE_TYPE (d), init);
2254 finish_decl (d, init_loc, init.value,
2255 init.original_type, asm_name);
2256 }
2257 }
2258 else
2259 {
2260 if (auto_type_p)
2261 {
2262 error_at (here,
2263 "%<__auto_type%> requires an initialized "
2264 "data declaration");
2265 c_parser_skip_to_end_of_block_or_statement (parser);
2266 return;
2267 }
2268 tree d = start_decl (declarator, specs, false,
2269 chainon (postfix_attrs,
2270 all_prefix_attrs));
2271 if (d
2272 && TREE_CODE (d) == FUNCTION_DECL
2273 && DECL_ARGUMENTS (d) == NULL_TREE
2274 && DECL_INITIAL (d) == NULL_TREE)
2275 {
2276 /* Find the innermost declarator that is neither cdk_id
2277 nor cdk_attrs. */
2278 const struct c_declarator *decl = declarator;
2279 const struct c_declarator *last_non_id_attrs = NULL;
2280
2281 while (decl)
2282 switch (decl->kind)
2283 {
2284 case cdk_array:
2285 case cdk_function:
2286 case cdk_pointer:
2287 last_non_id_attrs = decl;
2288 decl = decl->declarator;
2289 break;
2290
2291 case cdk_attrs:
2292 decl = decl->declarator;
2293 break;
2294
2295 case cdk_id:
2296 decl = 0;
2297 break;
2298
2299 default:
2300 gcc_unreachable ();
2301 }
2302
2303 /* If it exists and is cdk_function, use its parameters. */
2304 if (last_non_id_attrs
2305 && last_non_id_attrs->kind == cdk_function)
2306 DECL_ARGUMENTS (d) = last_non_id_attrs->u.arg_info->parms;
2307 }
2308 if (omp_declare_simd_clauses.exists ())
2309 {
2310 tree parms = NULL_TREE;
2311 if (d && TREE_CODE (d) == FUNCTION_DECL)
2312 {
2313 struct c_declarator *ce = declarator;
2314 while (ce != NULL)
2315 if (ce->kind == cdk_function)
2316 {
2317 parms = ce->u.arg_info->parms;
2318 break;
2319 }
2320 else
2321 ce = ce->declarator;
2322 }
2323 if (parms)
2324 temp_store_parm_decls (d, parms);
2325 c_finish_omp_declare_simd (parser, d, parms,
2326 omp_declare_simd_clauses);
2327 if (parms)
2328 temp_pop_parm_decls ();
2329 }
2330 if (oacc_routine_data)
2331 c_finish_oacc_routine (oacc_routine_data, d, false);
2332 if (d)
2333 finish_decl (d, UNKNOWN_LOCATION, NULL_TREE,
2334 NULL_TREE, asm_name);
2335
2336 if (c_parser_next_token_is_keyword (parser, RID_IN))
2337 {
2338 if (d)
2339 *objc_foreach_object_declaration = d;
2340 else
2341 *objc_foreach_object_declaration = error_mark_node;
2342 }
2343 }
2344 if (c_parser_next_token_is (parser, CPP_COMMA))
2345 {
2346 if (auto_type_p)
2347 {
2348 error_at (here,
2349 "%<__auto_type%> may only be used with"
2350 " a single declarator");
2351 c_parser_skip_to_end_of_block_or_statement (parser);
2352 return;
2353 }
2354 c_parser_consume_token (parser);
2355 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2356 all_prefix_attrs = chainon (c_parser_gnu_attributes (parser),
2357 prefix_attrs);
2358 else
2359 all_prefix_attrs = prefix_attrs;
2360 continue;
2361 }
2362 else if (c_parser_next_token_is (parser, CPP_SEMICOLON))
2363 {
2364 c_parser_consume_token (parser);
2365 return;
2366 }
2367 else if (c_parser_next_token_is_keyword (parser, RID_IN))
2368 {
2369 /* This can only happen in Objective-C: we found the
2370 'in' that terminates the declaration inside an
2371 Objective-C foreach statement. Do not consume the
2372 token, so that the caller can use it to determine
2373 that this indeed is a foreach context. */
2374 return;
2375 }
2376 else
2377 {
2378 c_parser_error (parser, "expected %<,%> or %<;%>");
2379 c_parser_skip_to_end_of_block_or_statement (parser);
2380 return;
2381 }
2382 }
2383 else if (auto_type_p)
2384 {
2385 error_at (here,
2386 "%<__auto_type%> requires an initialized data declaration");
2387 c_parser_skip_to_end_of_block_or_statement (parser);
2388 return;
2389 }
2390 else if (!fndef_ok)
2391 {
2392 c_parser_error (parser, "expected %<=%>, %<,%>, %<;%>, "
2393 "%<asm%> or %<__attribute__%>");
2394 c_parser_skip_to_end_of_block_or_statement (parser);
2395 return;
2396 }
2397 /* Function definition (nested or otherwise). */
2398 if (nested)
2399 {
2400 pedwarn (here, OPT_Wpedantic, "ISO C forbids nested functions");
2401 c_push_function_context ();
2402 }
2403 if (!start_function (specs, declarator, all_prefix_attrs))
2404 {
2405 /* At this point we've consumed:
2406 declaration-specifiers declarator
2407 and the next token isn't CPP_EQ, CPP_COMMA, CPP_SEMICOLON,
2408 RID_ASM, RID_ATTRIBUTE, or RID_IN,
2409 but the
2410 declaration-specifiers declarator
2411 aren't grokkable as a function definition, so we have
2412 an error. */
2413 gcc_assert (!c_parser_next_token_is (parser, CPP_SEMICOLON));
2414 if (c_parser_next_token_starts_declspecs (parser))
2415 {
2416 /* If we have
2417 declaration-specifiers declarator decl-specs
2418 then assume we have a missing semicolon, which would
2419 give us:
2420 declaration-specifiers declarator decl-specs
2421 ^
2422 ;
2423 <~~~~~~~~~ declaration ~~~~~~~~~~>
2424 Use c_parser_require to get an error with a fix-it hint. */
2425 c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>");
2426 parser->error = false;
2427 }
2428 else
2429 {
2430 /* This can appear in many cases looking nothing like a
2431 function definition, so we don't give a more specific
2432 error suggesting there was one. */
2433 c_parser_error (parser, "expected %<=%>, %<,%>, %<;%>, %<asm%> "
2434 "or %<__attribute__%>");
2435 }
2436 if (nested)
2437 c_pop_function_context ();
2438 break;
2439 }
2440
2441 if (DECL_DECLARED_INLINE_P (current_function_decl))
2442 tv = TV_PARSE_INLINE;
2443 else
2444 tv = TV_PARSE_FUNC;
2445 auto_timevar at (g_timer, tv);
2446
2447 /* Parse old-style parameter declarations. ??? Attributes are
2448 not allowed to start declaration specifiers here because of a
2449 syntax conflict between a function declaration with attribute
2450 suffix and a function definition with an attribute prefix on
2451 first old-style parameter declaration. Following the old
2452 parser, they are not accepted on subsequent old-style
2453 parameter declarations either. However, there is no
2454 ambiguity after the first declaration, nor indeed on the
2455 first as long as we don't allow postfix attributes after a
2456 declarator with a nonempty identifier list in a definition;
2457 and postfix attributes have never been accepted here in
2458 function definitions either. */
2459 while (c_parser_next_token_is_not (parser, CPP_EOF)
2460 && c_parser_next_token_is_not (parser, CPP_OPEN_BRACE))
2461 c_parser_declaration_or_fndef (parser, false, false, false,
2462 true, false, NULL, vNULL);
2463 store_parm_decls ();
2464 if (omp_declare_simd_clauses.exists ())
2465 c_finish_omp_declare_simd (parser, current_function_decl, NULL_TREE,
2466 omp_declare_simd_clauses);
2467 if (oacc_routine_data)
2468 c_finish_oacc_routine (oacc_routine_data, current_function_decl, true);
2469 DECL_STRUCT_FUNCTION (current_function_decl)->function_start_locus
2470 = c_parser_peek_token (parser)->location;
2471
2472 /* If the definition was marked with __RTL, use the RTL parser now,
2473 consuming the function body. */
2474 if (specs->declspec_il == cdil_rtl)
2475 {
2476 c_parser_parse_rtl_body (parser, specs->gimple_or_rtl_pass);
2477
2478 /* Normally, store_parm_decls sets next_is_function_body,
2479 anticipating a function body. We need a push_scope/pop_scope
2480 pair to flush out this state, or subsequent function parsing
2481 will go wrong. */
2482 push_scope ();
2483 pop_scope ();
2484
2485 finish_function ();
2486 return;
2487 }
2488 /* If the definition was marked with __GIMPLE then parse the
2489 function body as GIMPLE. */
2490 else if (specs->declspec_il != cdil_none)
2491 {
2492 bool saved = in_late_binary_op;
2493 in_late_binary_op = true;
2494 c_parser_parse_gimple_body (parser, specs->gimple_or_rtl_pass,
2495 specs->declspec_il,
2496 specs->entry_bb_count);
2497 in_late_binary_op = saved;
2498 }
2499 else
2500 fnbody = c_parser_compound_statement (parser);
2501 tree fndecl = current_function_decl;
2502 if (nested)
2503 {
2504 tree decl = current_function_decl;
2505 /* Mark nested functions as needing static-chain initially.
2506 lower_nested_functions will recompute it but the
2507 DECL_STATIC_CHAIN flag is also used before that happens,
2508 by initializer_constant_valid_p. See gcc.dg/nested-fn-2.c. */
2509 DECL_STATIC_CHAIN (decl) = 1;
2510 add_stmt (fnbody);
2511 finish_function ();
2512 c_pop_function_context ();
2513 add_stmt (build_stmt (DECL_SOURCE_LOCATION (decl), DECL_EXPR, decl));
2514 }
2515 else
2516 {
2517 if (fnbody)
2518 add_stmt (fnbody);
2519 finish_function ();
2520 }
2521 /* Get rid of the empty stmt list for GIMPLE/RTL. */
2522 if (specs->declspec_il != cdil_none)
2523 DECL_SAVED_TREE (fndecl) = NULL_TREE;
2524
2525 break;
2526 }
2527 }
2528
2529 /* Parse an asm-definition (asm() outside a function body). This is a
2530 GNU extension.
2531
2532 asm-definition:
2533 simple-asm-expr ;
2534 */
2535
2536 static void
2537 c_parser_asm_definition (c_parser *parser)
2538 {
2539 tree asm_str = c_parser_simple_asm_expr (parser);
2540 if (asm_str)
2541 symtab->finalize_toplevel_asm (asm_str);
2542 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
2543 }
2544
2545 /* Parse a static assertion (C11 6.7.10).
2546
2547 static_assert-declaration:
2548 static_assert-declaration-no-semi ;
2549 */
2550
2551 static void
2552 c_parser_static_assert_declaration (c_parser *parser)
2553 {
2554 c_parser_static_assert_declaration_no_semi (parser);
2555 if (parser->error
2556 || !c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
2557 c_parser_skip_to_end_of_block_or_statement (parser);
2558 }
2559
2560 /* Parse a static assertion (C11 6.7.10), without the trailing
2561 semicolon.
2562
2563 static_assert-declaration-no-semi:
2564 _Static_assert ( constant-expression , string-literal )
2565
2566 C2X:
2567 static_assert-declaration-no-semi:
2568 _Static_assert ( constant-expression )
2569 */
2570
2571 static void
2572 c_parser_static_assert_declaration_no_semi (c_parser *parser)
2573 {
2574 location_t assert_loc, value_loc;
2575 tree value;
2576 tree string = NULL_TREE;
2577
2578 gcc_assert (c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT));
2579 assert_loc = c_parser_peek_token (parser)->location;
2580 if (flag_isoc99)
2581 pedwarn_c99 (assert_loc, OPT_Wpedantic,
2582 "ISO C99 does not support %<_Static_assert%>");
2583 else
2584 pedwarn_c99 (assert_loc, OPT_Wpedantic,
2585 "ISO C90 does not support %<_Static_assert%>");
2586 c_parser_consume_token (parser);
2587 matching_parens parens;
2588 if (!parens.require_open (parser))
2589 return;
2590 location_t value_tok_loc = c_parser_peek_token (parser)->location;
2591 value = c_parser_expr_no_commas (parser, NULL).value;
2592 value_loc = EXPR_LOC_OR_LOC (value, value_tok_loc);
2593 if (c_parser_next_token_is (parser, CPP_COMMA))
2594 {
2595 c_parser_consume_token (parser);
2596 switch (c_parser_peek_token (parser)->type)
2597 {
2598 case CPP_STRING:
2599 case CPP_STRING16:
2600 case CPP_STRING32:
2601 case CPP_WSTRING:
2602 case CPP_UTF8STRING:
2603 string = c_parser_string_literal (parser, false, true).value;
2604 break;
2605 default:
2606 c_parser_error (parser, "expected string literal");
2607 return;
2608 }
2609 }
2610 else if (flag_isoc11)
2611 /* If pedantic for pre-C11, the use of _Static_assert itself will
2612 have been diagnosed, so do not also diagnose the use of this
2613 new C2X feature of _Static_assert. */
2614 pedwarn_c11 (assert_loc, OPT_Wpedantic,
2615 "ISO C11 does not support omitting the string in "
2616 "%<_Static_assert%>");
2617 parens.require_close (parser);
2618
2619 if (!INTEGRAL_TYPE_P (TREE_TYPE (value)))
2620 {
2621 error_at (value_loc, "expression in static assertion is not an integer");
2622 return;
2623 }
2624 if (TREE_CODE (value) != INTEGER_CST)
2625 {
2626 value = c_fully_fold (value, false, NULL);
2627 /* Strip no-op conversions. */
2628 STRIP_TYPE_NOPS (value);
2629 if (TREE_CODE (value) == INTEGER_CST)
2630 pedwarn (value_loc, OPT_Wpedantic, "expression in static assertion "
2631 "is not an integer constant expression");
2632 }
2633 if (TREE_CODE (value) != INTEGER_CST)
2634 {
2635 error_at (value_loc, "expression in static assertion is not constant");
2636 return;
2637 }
2638 constant_expression_warning (value);
2639 if (integer_zerop (value))
2640 {
2641 if (string)
2642 error_at (assert_loc, "static assertion failed: %E", string);
2643 else
2644 error_at (assert_loc, "static assertion failed");
2645 }
2646 }
2647
2648 /* Parse some declaration specifiers (possibly none) (C90 6.5, C99
2649 6.7, C11 6.7), adding them to SPECS (which may already include some).
2650 Storage class specifiers are accepted iff SCSPEC_OK; type
2651 specifiers are accepted iff TYPESPEC_OK; alignment specifiers are
2652 accepted iff ALIGNSPEC_OK; gnu-attributes are accepted at the start
2653 iff START_ATTR_OK; __auto_type is accepted iff AUTO_TYPE_OK. In
2654 addition to the syntax shown, standard attributes are accepted at
2655 the start iff START_STD_ATTR_OK and at the end iff END_STD_ATTR_OK;
2656 unlike gnu-attributes, they are not accepted in the middle of the
2657 list. (This combines various different syntax productions in the C
2658 standard, and in some cases gnu-attributes and standard attributes
2659 at the start may already have been parsed before this function is
2660 called.)
2661
2662 declaration-specifiers:
2663 storage-class-specifier declaration-specifiers[opt]
2664 type-specifier declaration-specifiers[opt]
2665 type-qualifier declaration-specifiers[opt]
2666 function-specifier declaration-specifiers[opt]
2667 alignment-specifier declaration-specifiers[opt]
2668
2669 Function specifiers (inline) are from C99, and are currently
2670 handled as storage class specifiers, as is __thread. Alignment
2671 specifiers are from C11.
2672
2673 C90 6.5.1, C99 6.7.1, C11 6.7.1:
2674 storage-class-specifier:
2675 typedef
2676 extern
2677 static
2678 auto
2679 register
2680 _Thread_local
2681
2682 (_Thread_local is new in C11.)
2683
2684 C99 6.7.4, C11 6.7.4:
2685 function-specifier:
2686 inline
2687 _Noreturn
2688
2689 (_Noreturn is new in C11.)
2690
2691 C90 6.5.2, C99 6.7.2, C11 6.7.2:
2692 type-specifier:
2693 void
2694 char
2695 short
2696 int
2697 long
2698 float
2699 double
2700 signed
2701 unsigned
2702 _Bool
2703 _Complex
2704 [_Imaginary removed in C99 TC2]
2705 struct-or-union-specifier
2706 enum-specifier
2707 typedef-name
2708 atomic-type-specifier
2709
2710 (_Bool and _Complex are new in C99.)
2711 (atomic-type-specifier is new in C11.)
2712
2713 C90 6.5.3, C99 6.7.3, C11 6.7.3:
2714
2715 type-qualifier:
2716 const
2717 restrict
2718 volatile
2719 address-space-qualifier
2720 _Atomic
2721
2722 (restrict is new in C99.)
2723 (_Atomic is new in C11.)
2724
2725 GNU extensions:
2726
2727 declaration-specifiers:
2728 gnu-attributes declaration-specifiers[opt]
2729
2730 type-qualifier:
2731 address-space
2732
2733 address-space:
2734 identifier recognized by the target
2735
2736 storage-class-specifier:
2737 __thread
2738
2739 type-specifier:
2740 typeof-specifier
2741 __auto_type
2742 __intN
2743 _Decimal32
2744 _Decimal64
2745 _Decimal128
2746 _Fract
2747 _Accum
2748 _Sat
2749
2750 (_Fract, _Accum, and _Sat are new from ISO/IEC DTR 18037:
2751 http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1169.pdf)
2752
2753 atomic-type-specifier
2754 _Atomic ( type-name )
2755
2756 Objective-C:
2757
2758 type-specifier:
2759 class-name objc-protocol-refs[opt]
2760 typedef-name objc-protocol-refs
2761 objc-protocol-refs
2762 */
2763
2764 void
2765 c_parser_declspecs (c_parser *parser, struct c_declspecs *specs,
2766 bool scspec_ok, bool typespec_ok, bool start_attr_ok,
2767 bool alignspec_ok, bool auto_type_ok,
2768 bool start_std_attr_ok, bool end_std_attr_ok,
2769 enum c_lookahead_kind la)
2770 {
2771 bool attrs_ok = start_attr_ok;
2772 bool seen_type = specs->typespec_kind != ctsk_none;
2773
2774 if (!typespec_ok)
2775 gcc_assert (la == cla_prefer_id);
2776
2777 if (start_std_attr_ok
2778 && c_parser_nth_token_starts_std_attributes (parser, 1))
2779 {
2780 gcc_assert (!specs->non_std_attrs_seen_p);
2781 location_t loc = c_parser_peek_token (parser)->location;
2782 tree attrs = c_parser_std_attribute_specifier_sequence (parser);
2783 declspecs_add_attrs (loc, specs, attrs);
2784 specs->non_std_attrs_seen_p = false;
2785 }
2786
2787 while (c_parser_next_token_is (parser, CPP_NAME)
2788 || c_parser_next_token_is (parser, CPP_KEYWORD)
2789 || (c_dialect_objc () && c_parser_next_token_is (parser, CPP_LESS)))
2790 {
2791 struct c_typespec t;
2792 tree attrs;
2793 tree align;
2794 location_t loc = c_parser_peek_token (parser)->location;
2795
2796 /* If we cannot accept a type, exit if the next token must start
2797 one. Also, if we already have seen a tagged definition,
2798 a typename would be an error anyway and likely the user
2799 has simply forgotten a semicolon, so we exit. */
2800 if ((!typespec_ok || specs->typespec_kind == ctsk_tagdef)
2801 && c_parser_next_tokens_start_typename (parser, la)
2802 && !c_parser_next_token_is_qualifier (parser)
2803 && !c_parser_next_token_is_keyword (parser, RID_ALIGNAS))
2804 break;
2805
2806 if (c_parser_next_token_is (parser, CPP_NAME))
2807 {
2808 c_token *name_token = c_parser_peek_token (parser);
2809 tree value = name_token->value;
2810 c_id_kind kind = name_token->id_kind;
2811
2812 if (kind == C_ID_ADDRSPACE)
2813 {
2814 addr_space_t as
2815 = name_token->keyword - RID_FIRST_ADDR_SPACE;
2816 declspecs_add_addrspace (name_token->location, specs, as);
2817 c_parser_consume_token (parser);
2818 attrs_ok = true;
2819 continue;
2820 }
2821
2822 gcc_assert (!c_parser_next_token_is_qualifier (parser));
2823
2824 /* If we cannot accept a type, and the next token must start one,
2825 exit. Do the same if we already have seen a tagged definition,
2826 since it would be an error anyway and likely the user has simply
2827 forgotten a semicolon. */
2828 if (seen_type || !c_parser_next_tokens_start_typename (parser, la))
2829 break;
2830
2831 /* Now at an unknown typename (C_ID_ID), a C_ID_TYPENAME or
2832 a C_ID_CLASSNAME. */
2833 c_parser_consume_token (parser);
2834 seen_type = true;
2835 attrs_ok = true;
2836 if (kind == C_ID_ID)
2837 {
2838 error_at (loc, "unknown type name %qE", value);
2839 t.kind = ctsk_typedef;
2840 t.spec = error_mark_node;
2841 }
2842 else if (kind == C_ID_TYPENAME
2843 && (!c_dialect_objc ()
2844 || c_parser_next_token_is_not (parser, CPP_LESS)))
2845 {
2846 t.kind = ctsk_typedef;
2847 /* For a typedef name, record the meaning, not the name.
2848 In case of 'foo foo, bar;'. */
2849 t.spec = lookup_name (value);
2850 }
2851 else
2852 {
2853 tree proto = NULL_TREE;
2854 gcc_assert (c_dialect_objc ());
2855 t.kind = ctsk_objc;
2856 if (c_parser_next_token_is (parser, CPP_LESS))
2857 proto = c_parser_objc_protocol_refs (parser);
2858 t.spec = objc_get_protocol_qualified_type (value, proto);
2859 }
2860 t.expr = NULL_TREE;
2861 t.expr_const_operands = true;
2862 declspecs_add_type (name_token->location, specs, t);
2863 continue;
2864 }
2865 if (c_parser_next_token_is (parser, CPP_LESS))
2866 {
2867 /* Make "<SomeProtocol>" equivalent to "id <SomeProtocol>" -
2868 nisse@lysator.liu.se. */
2869 tree proto;
2870 gcc_assert (c_dialect_objc ());
2871 if (!typespec_ok || seen_type)
2872 break;
2873 proto = c_parser_objc_protocol_refs (parser);
2874 t.kind = ctsk_objc;
2875 t.spec = objc_get_protocol_qualified_type (NULL_TREE, proto);
2876 t.expr = NULL_TREE;
2877 t.expr_const_operands = true;
2878 declspecs_add_type (loc, specs, t);
2879 continue;
2880 }
2881 gcc_assert (c_parser_next_token_is (parser, CPP_KEYWORD));
2882 switch (c_parser_peek_token (parser)->keyword)
2883 {
2884 case RID_STATIC:
2885 case RID_EXTERN:
2886 case RID_REGISTER:
2887 case RID_TYPEDEF:
2888 case RID_INLINE:
2889 case RID_NORETURN:
2890 case RID_AUTO:
2891 case RID_THREAD:
2892 if (!scspec_ok)
2893 goto out;
2894 attrs_ok = true;
2895 /* TODO: Distinguish between function specifiers (inline, noreturn)
2896 and storage class specifiers, either here or in
2897 declspecs_add_scspec. */
2898 declspecs_add_scspec (loc, specs,
2899 c_parser_peek_token (parser)->value);
2900 c_parser_consume_token (parser);
2901 break;
2902 case RID_AUTO_TYPE:
2903 if (!auto_type_ok)
2904 goto out;
2905 /* Fall through. */
2906 case RID_UNSIGNED:
2907 case RID_LONG:
2908 case RID_SHORT:
2909 case RID_SIGNED:
2910 case RID_COMPLEX:
2911 case RID_INT:
2912 case RID_CHAR:
2913 case RID_FLOAT:
2914 case RID_DOUBLE:
2915 case RID_VOID:
2916 case RID_DFLOAT32:
2917 case RID_DFLOAT64:
2918 case RID_DFLOAT128:
2919 CASE_RID_FLOATN_NX:
2920 case RID_BOOL:
2921 case RID_FRACT:
2922 case RID_ACCUM:
2923 case RID_SAT:
2924 case RID_INT_N_0:
2925 case RID_INT_N_1:
2926 case RID_INT_N_2:
2927 case RID_INT_N_3:
2928 if (!typespec_ok)
2929 goto out;
2930 attrs_ok = true;
2931 seen_type = true;
2932 if (c_dialect_objc ())
2933 parser->objc_need_raw_identifier = true;
2934 t.kind = ctsk_resword;
2935 t.spec = c_parser_peek_token (parser)->value;
2936 t.expr = NULL_TREE;
2937 t.expr_const_operands = true;
2938 declspecs_add_type (loc, specs, t);
2939 c_parser_consume_token (parser);
2940 break;
2941 case RID_ENUM:
2942 if (!typespec_ok)
2943 goto out;
2944 attrs_ok = true;
2945 seen_type = true;
2946 t = c_parser_enum_specifier (parser);
2947 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, t.spec);
2948 declspecs_add_type (loc, specs, t);
2949 break;
2950 case RID_STRUCT:
2951 case RID_UNION:
2952 if (!typespec_ok)
2953 goto out;
2954 attrs_ok = true;
2955 seen_type = true;
2956 t = c_parser_struct_or_union_specifier (parser);
2957 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, t.spec);
2958 declspecs_add_type (loc, specs, t);
2959 break;
2960 case RID_TYPEOF:
2961 /* ??? The old parser rejected typeof after other type
2962 specifiers, but is a syntax error the best way of
2963 handling this? */
2964 if (!typespec_ok || seen_type)
2965 goto out;
2966 attrs_ok = true;
2967 seen_type = true;
2968 t = c_parser_typeof_specifier (parser);
2969 declspecs_add_type (loc, specs, t);
2970 break;
2971 case RID_ATOMIC:
2972 /* C parser handling of Objective-C constructs needs
2973 checking for correct lvalue-to-rvalue conversions, and
2974 the code in build_modify_expr handling various
2975 Objective-C cases, and that in build_unary_op handling
2976 Objective-C cases for increment / decrement, also needs
2977 updating; uses of TYPE_MAIN_VARIANT in objc_compare_types
2978 and objc_types_are_equivalent may also need updates. */
2979 if (c_dialect_objc ())
2980 sorry ("%<_Atomic%> in Objective-C");
2981 if (flag_isoc99)
2982 pedwarn_c99 (loc, OPT_Wpedantic,
2983 "ISO C99 does not support the %<_Atomic%> qualifier");
2984 else
2985 pedwarn_c99 (loc, OPT_Wpedantic,
2986 "ISO C90 does not support the %<_Atomic%> qualifier");
2987 attrs_ok = true;
2988 tree value;
2989 value = c_parser_peek_token (parser)->value;
2990 c_parser_consume_token (parser);
2991 if (typespec_ok && c_parser_next_token_is (parser, CPP_OPEN_PAREN))
2992 {
2993 /* _Atomic ( type-name ). */
2994 seen_type = true;
2995 c_parser_consume_token (parser);
2996 struct c_type_name *type = c_parser_type_name (parser);
2997 t.kind = ctsk_typeof;
2998 t.spec = error_mark_node;
2999 t.expr = NULL_TREE;
3000 t.expr_const_operands = true;
3001 if (type != NULL)
3002 t.spec = groktypename (type, &t.expr,
3003 &t.expr_const_operands);
3004 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3005 "expected %<)%>");
3006 if (t.spec != error_mark_node)
3007 {
3008 if (TREE_CODE (t.spec) == ARRAY_TYPE)
3009 error_at (loc, "%<_Atomic%>-qualified array type");
3010 else if (TREE_CODE (t.spec) == FUNCTION_TYPE)
3011 error_at (loc, "%<_Atomic%>-qualified function type");
3012 else if (TYPE_QUALS (t.spec) != TYPE_UNQUALIFIED)
3013 error_at (loc, "%<_Atomic%> applied to a qualified type");
3014 else
3015 t.spec = c_build_qualified_type (t.spec, TYPE_QUAL_ATOMIC);
3016 }
3017 declspecs_add_type (loc, specs, t);
3018 }
3019 else
3020 declspecs_add_qual (loc, specs, value);
3021 break;
3022 case RID_CONST:
3023 case RID_VOLATILE:
3024 case RID_RESTRICT:
3025 attrs_ok = true;
3026 declspecs_add_qual (loc, specs, c_parser_peek_token (parser)->value);
3027 c_parser_consume_token (parser);
3028 break;
3029 case RID_ATTRIBUTE:
3030 if (!attrs_ok)
3031 goto out;
3032 attrs = c_parser_gnu_attributes (parser);
3033 declspecs_add_attrs (loc, specs, attrs);
3034 break;
3035 case RID_ALIGNAS:
3036 if (!alignspec_ok)
3037 goto out;
3038 align = c_parser_alignas_specifier (parser);
3039 declspecs_add_alignas (loc, specs, align);
3040 break;
3041 case RID_GIMPLE:
3042 if (! flag_gimple)
3043 error_at (loc, "%<__GIMPLE%> only valid with %<-fgimple%>");
3044 c_parser_consume_token (parser);
3045 specs->declspec_il = cdil_gimple;
3046 specs->locations[cdw_gimple] = loc;
3047 c_parser_gimple_or_rtl_pass_list (parser, specs);
3048 break;
3049 case RID_RTL:
3050 c_parser_consume_token (parser);
3051 specs->declspec_il = cdil_rtl;
3052 specs->locations[cdw_rtl] = loc;
3053 c_parser_gimple_or_rtl_pass_list (parser, specs);
3054 break;
3055 default:
3056 goto out;
3057 }
3058 }
3059 out:
3060 if (end_std_attr_ok
3061 && c_parser_nth_token_starts_std_attributes (parser, 1))
3062 specs->postfix_attrs = c_parser_std_attribute_specifier_sequence (parser);
3063 }
3064
3065 /* Parse an enum specifier (C90 6.5.2.2, C99 6.7.2.2, C11 6.7.2.2).
3066
3067 enum-specifier:
3068 enum gnu-attributes[opt] identifier[opt] { enumerator-list }
3069 gnu-attributes[opt]
3070 enum gnu-attributes[opt] identifier[opt] { enumerator-list , }
3071 gnu-attributes[opt]
3072 enum gnu-attributes[opt] identifier
3073
3074 The form with trailing comma is new in C99. The forms with
3075 gnu-attributes are GNU extensions. In GNU C, we accept any expression
3076 without commas in the syntax (assignment expressions, not just
3077 conditional expressions); assignment expressions will be diagnosed
3078 as non-constant.
3079
3080 enumerator-list:
3081 enumerator
3082 enumerator-list , enumerator
3083
3084 enumerator:
3085 enumeration-constant attribute-specifier-sequence[opt]
3086 enumeration-constant attribute-specifier-sequence[opt]
3087 = constant-expression
3088
3089 GNU Extensions:
3090
3091 enumerator:
3092 enumeration-constant attribute-specifier-sequence[opt] gnu-attributes[opt]
3093 enumeration-constant attribute-specifier-sequence[opt] gnu-attributes[opt]
3094 = constant-expression
3095
3096 */
3097
3098 static struct c_typespec
3099 c_parser_enum_specifier (c_parser *parser)
3100 {
3101 struct c_typespec ret;
3102 bool have_std_attrs;
3103 tree std_attrs = NULL_TREE;
3104 tree attrs;
3105 tree ident = NULL_TREE;
3106 location_t enum_loc;
3107 location_t ident_loc = UNKNOWN_LOCATION; /* Quiet warning. */
3108 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ENUM));
3109 c_parser_consume_token (parser);
3110 have_std_attrs = c_parser_nth_token_starts_std_attributes (parser, 1);
3111 if (have_std_attrs)
3112 std_attrs = c_parser_std_attribute_specifier_sequence (parser);
3113 attrs = c_parser_gnu_attributes (parser);
3114 enum_loc = c_parser_peek_token (parser)->location;
3115 /* Set the location in case we create a decl now. */
3116 c_parser_set_source_position_from_token (c_parser_peek_token (parser));
3117 if (c_parser_next_token_is (parser, CPP_NAME))
3118 {
3119 ident = c_parser_peek_token (parser)->value;
3120 ident_loc = c_parser_peek_token (parser)->location;
3121 enum_loc = ident_loc;
3122 c_parser_consume_token (parser);
3123 }
3124 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
3125 {
3126 /* Parse an enum definition. */
3127 struct c_enum_contents the_enum;
3128 tree type;
3129 tree postfix_attrs;
3130 /* We chain the enumerators in reverse order, then put them in
3131 forward order at the end. */
3132 tree values;
3133 timevar_push (TV_PARSE_ENUM);
3134 type = start_enum (enum_loc, &the_enum, ident);
3135 values = NULL_TREE;
3136 c_parser_consume_token (parser);
3137 while (true)
3138 {
3139 tree enum_id;
3140 tree enum_value;
3141 tree enum_decl;
3142 bool seen_comma;
3143 c_token *token;
3144 location_t comma_loc = UNKNOWN_LOCATION; /* Quiet warning. */
3145 location_t decl_loc, value_loc;
3146 if (c_parser_next_token_is_not (parser, CPP_NAME))
3147 {
3148 /* Give a nicer error for "enum {}". */
3149 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE)
3150 && !parser->error)
3151 {
3152 error_at (c_parser_peek_token (parser)->location,
3153 "empty enum is invalid");
3154 parser->error = true;
3155 }
3156 else
3157 c_parser_error (parser, "expected identifier");
3158 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
3159 values = error_mark_node;
3160 break;
3161 }
3162 token = c_parser_peek_token (parser);
3163 enum_id = token->value;
3164 /* Set the location in case we create a decl now. */
3165 c_parser_set_source_position_from_token (token);
3166 decl_loc = value_loc = token->location;
3167 c_parser_consume_token (parser);
3168 /* Parse any specified attributes. */
3169 tree std_attrs = NULL_TREE;
3170 if (c_parser_nth_token_starts_std_attributes (parser, 1))
3171 std_attrs = c_parser_std_attribute_specifier_sequence (parser);
3172 tree enum_attrs = chainon (std_attrs,
3173 c_parser_gnu_attributes (parser));
3174 if (c_parser_next_token_is (parser, CPP_EQ))
3175 {
3176 c_parser_consume_token (parser);
3177 value_loc = c_parser_peek_token (parser)->location;
3178 enum_value = c_parser_expr_no_commas (parser, NULL).value;
3179 }
3180 else
3181 enum_value = NULL_TREE;
3182 enum_decl = build_enumerator (decl_loc, value_loc,
3183 &the_enum, enum_id, enum_value);
3184 if (enum_attrs)
3185 decl_attributes (&TREE_PURPOSE (enum_decl), enum_attrs, 0);
3186 TREE_CHAIN (enum_decl) = values;
3187 values = enum_decl;
3188 seen_comma = false;
3189 if (c_parser_next_token_is (parser, CPP_COMMA))
3190 {
3191 comma_loc = c_parser_peek_token (parser)->location;
3192 seen_comma = true;
3193 c_parser_consume_token (parser);
3194 }
3195 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3196 {
3197 if (seen_comma)
3198 pedwarn_c90 (comma_loc, OPT_Wpedantic,
3199 "comma at end of enumerator list");
3200 c_parser_consume_token (parser);
3201 break;
3202 }
3203 if (!seen_comma)
3204 {
3205 c_parser_error (parser, "expected %<,%> or %<}%>");
3206 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
3207 values = error_mark_node;
3208 break;
3209 }
3210 }
3211 postfix_attrs = c_parser_gnu_attributes (parser);
3212 ret.spec = finish_enum (type, nreverse (values),
3213 chainon (std_attrs,
3214 chainon (attrs, postfix_attrs)));
3215 ret.kind = ctsk_tagdef;
3216 ret.expr = NULL_TREE;
3217 ret.expr_const_operands = true;
3218 timevar_pop (TV_PARSE_ENUM);
3219 return ret;
3220 }
3221 else if (!ident)
3222 {
3223 c_parser_error (parser, "expected %<{%>");
3224 ret.spec = error_mark_node;
3225 ret.kind = ctsk_tagref;
3226 ret.expr = NULL_TREE;
3227 ret.expr_const_operands = true;
3228 return ret;
3229 }
3230 /* Attributes may only appear when the members are defined or in
3231 certain forward declarations (treat enum forward declarations in
3232 GNU C analogously to struct and union forward declarations in
3233 standard C). */
3234 if (have_std_attrs && c_parser_next_token_is_not (parser, CPP_SEMICOLON))
3235 c_parser_error (parser, "expected %<;%>");
3236 ret = parser_xref_tag (ident_loc, ENUMERAL_TYPE, ident, have_std_attrs,
3237 std_attrs);
3238 /* In ISO C, enumerated types can be referred to only if already
3239 defined. */
3240 if (pedantic && !COMPLETE_TYPE_P (ret.spec))
3241 {
3242 gcc_assert (ident);
3243 pedwarn (enum_loc, OPT_Wpedantic,
3244 "ISO C forbids forward references to %<enum%> types");
3245 }
3246 return ret;
3247 }
3248
3249 /* Parse a struct or union specifier (C90 6.5.2.1, C99 6.7.2.1, C11 6.7.2.1).
3250
3251 struct-or-union-specifier:
3252 struct-or-union attribute-specifier-sequence[opt] gnu-attributes[opt]
3253 identifier[opt] { struct-contents } gnu-attributes[opt]
3254 struct-or-union attribute-specifier-sequence[opt] gnu-attributes[opt]
3255 identifier
3256
3257 struct-contents:
3258 struct-declaration-list
3259
3260 struct-declaration-list:
3261 struct-declaration ;
3262 struct-declaration-list struct-declaration ;
3263
3264 GNU extensions:
3265
3266 struct-contents:
3267 empty
3268 struct-declaration
3269 struct-declaration-list struct-declaration
3270
3271 struct-declaration-list:
3272 struct-declaration-list ;
3273 ;
3274
3275 (Note that in the syntax here, unlike that in ISO C, the semicolons
3276 are included here rather than in struct-declaration, in order to
3277 describe the syntax with extra semicolons and missing semicolon at
3278 end.)
3279
3280 Objective-C:
3281
3282 struct-declaration-list:
3283 @defs ( class-name )
3284
3285 (Note this does not include a trailing semicolon, but can be
3286 followed by further declarations, and gets a pedwarn-if-pedantic
3287 when followed by a semicolon.) */
3288
3289 static struct c_typespec
3290 c_parser_struct_or_union_specifier (c_parser *parser)
3291 {
3292 struct c_typespec ret;
3293 bool have_std_attrs;
3294 tree std_attrs = NULL_TREE;
3295 tree attrs;
3296 tree ident = NULL_TREE;
3297 location_t struct_loc;
3298 location_t ident_loc = UNKNOWN_LOCATION;
3299 enum tree_code code;
3300 switch (c_parser_peek_token (parser)->keyword)
3301 {
3302 case RID_STRUCT:
3303 code = RECORD_TYPE;
3304 break;
3305 case RID_UNION:
3306 code = UNION_TYPE;
3307 break;
3308 default:
3309 gcc_unreachable ();
3310 }
3311 struct_loc = c_parser_peek_token (parser)->location;
3312 c_parser_consume_token (parser);
3313 have_std_attrs = c_parser_nth_token_starts_std_attributes (parser, 1);
3314 if (have_std_attrs)
3315 std_attrs = c_parser_std_attribute_specifier_sequence (parser);
3316 attrs = c_parser_gnu_attributes (parser);
3317
3318 /* Set the location in case we create a decl now. */
3319 c_parser_set_source_position_from_token (c_parser_peek_token (parser));
3320
3321 if (c_parser_next_token_is (parser, CPP_NAME))
3322 {
3323 ident = c_parser_peek_token (parser)->value;
3324 ident_loc = c_parser_peek_token (parser)->location;
3325 struct_loc = ident_loc;
3326 c_parser_consume_token (parser);
3327 }
3328 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
3329 {
3330 /* Parse a struct or union definition. Start the scope of the
3331 tag before parsing components. */
3332 class c_struct_parse_info *struct_info;
3333 tree type = start_struct (struct_loc, code, ident, &struct_info);
3334 tree postfix_attrs;
3335 /* We chain the components in reverse order, then put them in
3336 forward order at the end. Each struct-declaration may
3337 declare multiple components (comma-separated), so we must use
3338 chainon to join them, although when parsing each
3339 struct-declaration we can use TREE_CHAIN directly.
3340
3341 The theory behind all this is that there will be more
3342 semicolon separated fields than comma separated fields, and
3343 so we'll be minimizing the number of node traversals required
3344 by chainon. */
3345 tree contents;
3346 timevar_push (TV_PARSE_STRUCT);
3347 contents = NULL_TREE;
3348 c_parser_consume_token (parser);
3349 /* Handle the Objective-C @defs construct,
3350 e.g. foo(sizeof(struct{ @defs(ClassName) }));. */
3351 if (c_parser_next_token_is_keyword (parser, RID_AT_DEFS))
3352 {
3353 tree name;
3354 gcc_assert (c_dialect_objc ());
3355 c_parser_consume_token (parser);
3356 matching_parens parens;
3357 if (!parens.require_open (parser))
3358 goto end_at_defs;
3359 if (c_parser_next_token_is (parser, CPP_NAME)
3360 && c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME)
3361 {
3362 name = c_parser_peek_token (parser)->value;
3363 c_parser_consume_token (parser);
3364 }
3365 else
3366 {
3367 c_parser_error (parser, "expected class name");
3368 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
3369 goto end_at_defs;
3370 }
3371 parens.skip_until_found_close (parser);
3372 contents = nreverse (objc_get_class_ivars (name));
3373 }
3374 end_at_defs:
3375 /* Parse the struct-declarations and semicolons. Problems with
3376 semicolons are diagnosed here; empty structures are diagnosed
3377 elsewhere. */
3378 while (true)
3379 {
3380 tree decls;
3381 /* Parse any stray semicolon. */
3382 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
3383 {
3384 location_t semicolon_loc
3385 = c_parser_peek_token (parser)->location;
3386 gcc_rich_location richloc (semicolon_loc);
3387 richloc.add_fixit_remove ();
3388 pedwarn (&richloc, OPT_Wpedantic,
3389 "extra semicolon in struct or union specified");
3390 c_parser_consume_token (parser);
3391 continue;
3392 }
3393 /* Stop if at the end of the struct or union contents. */
3394 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3395 {
3396 c_parser_consume_token (parser);
3397 break;
3398 }
3399 /* Accept #pragmas at struct scope. */
3400 if (c_parser_next_token_is (parser, CPP_PRAGMA))
3401 {
3402 c_parser_pragma (parser, pragma_struct, NULL);
3403 continue;
3404 }
3405 /* Parse some comma-separated declarations, but not the
3406 trailing semicolon if any. */
3407 decls = c_parser_struct_declaration (parser);
3408 contents = chainon (decls, contents);
3409 /* If no semicolon follows, either we have a parse error or
3410 are at the end of the struct or union and should
3411 pedwarn. */
3412 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
3413 c_parser_consume_token (parser);
3414 else
3415 {
3416 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3417 pedwarn (c_parser_peek_token (parser)->location, 0,
3418 "no semicolon at end of struct or union");
3419 else if (parser->error
3420 || !c_parser_next_token_starts_declspecs (parser))
3421 {
3422 c_parser_error (parser, "expected %<;%>");
3423 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
3424 break;
3425 }
3426
3427 /* If we come here, we have already emitted an error
3428 for an expected `;', identifier or `(', and we also
3429 recovered already. Go on with the next field. */
3430 }
3431 }
3432 postfix_attrs = c_parser_gnu_attributes (parser);
3433 ret.spec = finish_struct (struct_loc, type, nreverse (contents),
3434 chainon (std_attrs,
3435 chainon (attrs, postfix_attrs)),
3436 struct_info);
3437 ret.kind = ctsk_tagdef;
3438 ret.expr = NULL_TREE;
3439 ret.expr_const_operands = true;
3440 timevar_pop (TV_PARSE_STRUCT);
3441 return ret;
3442 }
3443 else if (!ident)
3444 {
3445 c_parser_error (parser, "expected %<{%>");
3446 ret.spec = error_mark_node;
3447 ret.kind = ctsk_tagref;
3448 ret.expr = NULL_TREE;
3449 ret.expr_const_operands = true;
3450 return ret;
3451 }
3452 /* Attributes may only appear when the members are defined or in
3453 certain forward declarations. */
3454 if (have_std_attrs && c_parser_next_token_is_not (parser, CPP_SEMICOLON))
3455 c_parser_error (parser, "expected %<;%>");
3456 /* ??? Existing practice is that GNU attributes are ignored after
3457 the struct or union keyword when not defining the members. */
3458 ret = parser_xref_tag (ident_loc, code, ident, have_std_attrs, std_attrs);
3459 return ret;
3460 }
3461
3462 /* Parse a struct-declaration (C90 6.5.2.1, C99 6.7.2.1, C11 6.7.2.1),
3463 *without* the trailing semicolon.
3464
3465 struct-declaration:
3466 attribute-specifier-sequence[opt] specifier-qualifier-list
3467 attribute-specifier-sequence[opt] struct-declarator-list
3468 static_assert-declaration-no-semi
3469
3470 specifier-qualifier-list:
3471 type-specifier specifier-qualifier-list[opt]
3472 type-qualifier specifier-qualifier-list[opt]
3473 alignment-specifier specifier-qualifier-list[opt]
3474 gnu-attributes specifier-qualifier-list[opt]
3475
3476 struct-declarator-list:
3477 struct-declarator
3478 struct-declarator-list , gnu-attributes[opt] struct-declarator
3479
3480 struct-declarator:
3481 declarator gnu-attributes[opt]
3482 declarator[opt] : constant-expression gnu-attributes[opt]
3483
3484 GNU extensions:
3485
3486 struct-declaration:
3487 __extension__ struct-declaration
3488 specifier-qualifier-list
3489
3490 Unlike the ISO C syntax, semicolons are handled elsewhere. The use
3491 of gnu-attributes where shown is a GNU extension. In GNU C, we accept
3492 any expression without commas in the syntax (assignment
3493 expressions, not just conditional expressions); assignment
3494 expressions will be diagnosed as non-constant. */
3495
3496 static tree
3497 c_parser_struct_declaration (c_parser *parser)
3498 {
3499 struct c_declspecs *specs;
3500 tree prefix_attrs;
3501 tree all_prefix_attrs;
3502 tree decls;
3503 location_t decl_loc;
3504 if (c_parser_next_token_is_keyword (parser, RID_EXTENSION))
3505 {
3506 int ext;
3507 tree decl;
3508 ext = disable_extension_diagnostics ();
3509 c_parser_consume_token (parser);
3510 decl = c_parser_struct_declaration (parser);
3511 restore_extension_diagnostics (ext);
3512 return decl;
3513 }
3514 if (c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT))
3515 {
3516 c_parser_static_assert_declaration_no_semi (parser);
3517 return NULL_TREE;
3518 }
3519 specs = build_null_declspecs ();
3520 decl_loc = c_parser_peek_token (parser)->location;
3521 /* Strictly by the standard, we shouldn't allow _Alignas here,
3522 but it appears to have been intended to allow it there, so
3523 we're keeping it as it is until WG14 reaches a conclusion
3524 of N1731.
3525 <http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1731.pdf> */
3526 c_parser_declspecs (parser, specs, false, true, true,
3527 true, false, true, true, cla_nonabstract_decl);
3528 if (parser->error)
3529 return NULL_TREE;
3530 if (!specs->declspecs_seen_p)
3531 {
3532 c_parser_error (parser, "expected specifier-qualifier-list");
3533 return NULL_TREE;
3534 }
3535 finish_declspecs (specs);
3536 if (c_parser_next_token_is (parser, CPP_SEMICOLON)
3537 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3538 {
3539 tree ret;
3540 if (specs->typespec_kind == ctsk_none)
3541 {
3542 pedwarn (decl_loc, OPT_Wpedantic,
3543 "ISO C forbids member declarations with no members");
3544 shadow_tag_warned (specs, pedantic);
3545 ret = NULL_TREE;
3546 }
3547 else
3548 {
3549 /* Support for unnamed structs or unions as members of
3550 structs or unions (which is [a] useful and [b] supports
3551 MS P-SDK). */
3552 tree attrs = NULL;
3553
3554 ret = grokfield (c_parser_peek_token (parser)->location,
3555 build_id_declarator (NULL_TREE), specs,
3556 NULL_TREE, &attrs);
3557 if (ret)
3558 decl_attributes (&ret, attrs, 0);
3559 }
3560 return ret;
3561 }
3562
3563 /* Provide better error recovery. Note that a type name here is valid,
3564 and will be treated as a field name. */
3565 if (specs->typespec_kind == ctsk_tagdef
3566 && TREE_CODE (specs->type) != ENUMERAL_TYPE
3567 && c_parser_next_token_starts_declspecs (parser)
3568 && !c_parser_next_token_is (parser, CPP_NAME))
3569 {
3570 c_parser_error (parser, "expected %<;%>, identifier or %<(%>");
3571 parser->error = false;
3572 return NULL_TREE;
3573 }
3574
3575 pending_xref_error ();
3576 prefix_attrs = specs->attrs;
3577 all_prefix_attrs = prefix_attrs;
3578 specs->attrs = NULL_TREE;
3579 decls = NULL_TREE;
3580 while (true)
3581 {
3582 /* Declaring one or more declarators or un-named bit-fields. */
3583 struct c_declarator *declarator;
3584 bool dummy = false;
3585 if (c_parser_next_token_is (parser, CPP_COLON))
3586 declarator = build_id_declarator (NULL_TREE);
3587 else
3588 declarator = c_parser_declarator (parser,
3589 specs->typespec_kind != ctsk_none,
3590 C_DTR_NORMAL, &dummy);
3591 if (declarator == NULL)
3592 {
3593 c_parser_skip_to_end_of_block_or_statement (parser);
3594 break;
3595 }
3596 if (c_parser_next_token_is (parser, CPP_COLON)
3597 || c_parser_next_token_is (parser, CPP_COMMA)
3598 || c_parser_next_token_is (parser, CPP_SEMICOLON)
3599 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE)
3600 || c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
3601 {
3602 tree postfix_attrs = NULL_TREE;
3603 tree width = NULL_TREE;
3604 tree d;
3605 if (c_parser_next_token_is (parser, CPP_COLON))
3606 {
3607 c_parser_consume_token (parser);
3608 width = c_parser_expr_no_commas (parser, NULL).value;
3609 }
3610 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
3611 postfix_attrs = c_parser_gnu_attributes (parser);
3612 d = grokfield (c_parser_peek_token (parser)->location,
3613 declarator, specs, width, &all_prefix_attrs);
3614 decl_attributes (&d, chainon (postfix_attrs,
3615 all_prefix_attrs), 0);
3616 DECL_CHAIN (d) = decls;
3617 decls = d;
3618 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
3619 all_prefix_attrs = chainon (c_parser_gnu_attributes (parser),
3620 prefix_attrs);
3621 else
3622 all_prefix_attrs = prefix_attrs;
3623 if (c_parser_next_token_is (parser, CPP_COMMA))
3624 c_parser_consume_token (parser);
3625 else if (c_parser_next_token_is (parser, CPP_SEMICOLON)
3626 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3627 {
3628 /* Semicolon consumed in caller. */
3629 break;
3630 }
3631 else
3632 {
3633 c_parser_error (parser, "expected %<,%>, %<;%> or %<}%>");
3634 break;
3635 }
3636 }
3637 else
3638 {
3639 c_parser_error (parser,
3640 "expected %<:%>, %<,%>, %<;%>, %<}%> or "
3641 "%<__attribute__%>");
3642 break;
3643 }
3644 }
3645 return decls;
3646 }
3647
3648 /* Parse a typeof specifier (a GNU extension).
3649
3650 typeof-specifier:
3651 typeof ( expression )
3652 typeof ( type-name )
3653 */
3654
3655 static struct c_typespec
3656 c_parser_typeof_specifier (c_parser *parser)
3657 {
3658 struct c_typespec ret;
3659 ret.kind = ctsk_typeof;
3660 ret.spec = error_mark_node;
3661 ret.expr = NULL_TREE;
3662 ret.expr_const_operands = true;
3663 gcc_assert (c_parser_next_token_is_keyword (parser, RID_TYPEOF));
3664 c_parser_consume_token (parser);
3665 c_inhibit_evaluation_warnings++;
3666 in_typeof++;
3667 matching_parens parens;
3668 if (!parens.require_open (parser))
3669 {
3670 c_inhibit_evaluation_warnings--;
3671 in_typeof--;
3672 return ret;
3673 }
3674 if (c_parser_next_tokens_start_typename (parser, cla_prefer_id))
3675 {
3676 struct c_type_name *type = c_parser_type_name (parser);
3677 c_inhibit_evaluation_warnings--;
3678 in_typeof--;
3679 if (type != NULL)
3680 {
3681 ret.spec = groktypename (type, &ret.expr, &ret.expr_const_operands);
3682 pop_maybe_used (variably_modified_type_p (ret.spec, NULL_TREE));
3683 }
3684 }
3685 else
3686 {
3687 bool was_vm;
3688 location_t here = c_parser_peek_token (parser)->location;
3689 struct c_expr expr = c_parser_expression (parser);
3690 c_inhibit_evaluation_warnings--;
3691 in_typeof--;
3692 if (TREE_CODE (expr.value) == COMPONENT_REF
3693 && DECL_C_BIT_FIELD (TREE_OPERAND (expr.value, 1)))
3694 error_at (here, "%<typeof%> applied to a bit-field");
3695 mark_exp_read (expr.value);
3696 ret.spec = TREE_TYPE (expr.value);
3697 was_vm = variably_modified_type_p (ret.spec, NULL_TREE);
3698 /* This is returned with the type so that when the type is
3699 evaluated, this can be evaluated. */
3700 if (was_vm)
3701 ret.expr = c_fully_fold (expr.value, false, &ret.expr_const_operands);
3702 pop_maybe_used (was_vm);
3703 /* For use in macros such as those in <stdatomic.h>, remove all
3704 qualifiers from atomic types. (const can be an issue for more macros
3705 using typeof than just the <stdatomic.h> ones.) */
3706 if (ret.spec != error_mark_node && TYPE_ATOMIC (ret.spec))
3707 ret.spec = c_build_qualified_type (ret.spec, TYPE_UNQUALIFIED);
3708 }
3709 parens.skip_until_found_close (parser);
3710 return ret;
3711 }
3712
3713 /* Parse an alignment-specifier.
3714
3715 C11 6.7.5:
3716
3717 alignment-specifier:
3718 _Alignas ( type-name )
3719 _Alignas ( constant-expression )
3720 */
3721
3722 static tree
3723 c_parser_alignas_specifier (c_parser * parser)
3724 {
3725 tree ret = error_mark_node;
3726 location_t loc = c_parser_peek_token (parser)->location;
3727 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ALIGNAS));
3728 c_parser_consume_token (parser);
3729 if (flag_isoc99)
3730 pedwarn_c99 (loc, OPT_Wpedantic,
3731 "ISO C99 does not support %<_Alignas%>");
3732 else
3733 pedwarn_c99 (loc, OPT_Wpedantic,
3734 "ISO C90 does not support %<_Alignas%>");
3735 matching_parens parens;
3736 if (!parens.require_open (parser))
3737 return ret;
3738 if (c_parser_next_tokens_start_typename (parser, cla_prefer_id))
3739 {
3740 struct c_type_name *type = c_parser_type_name (parser);
3741 if (type != NULL)
3742 ret = c_sizeof_or_alignof_type (loc, groktypename (type, NULL, NULL),
3743 false, true, 1);
3744 }
3745 else
3746 ret = c_parser_expr_no_commas (parser, NULL).value;
3747 parens.skip_until_found_close (parser);
3748 return ret;
3749 }
3750
3751 /* Parse a declarator, possibly an abstract declarator (C90 6.5.4,
3752 6.5.5, C99 6.7.5, 6.7.6, C11 6.7.6, 6.7.7). If TYPE_SEEN_P then
3753 a typedef name may be redeclared; otherwise it may not. KIND
3754 indicates which kind of declarator is wanted. Returns a valid
3755 declarator except in the case of a syntax error in which case NULL is
3756 returned. *SEEN_ID is set to true if an identifier being declared is
3757 seen; this is used to diagnose bad forms of abstract array declarators
3758 and to determine whether an identifier list is syntactically permitted.
3759
3760 declarator:
3761 pointer[opt] direct-declarator
3762
3763 direct-declarator:
3764 identifier
3765 ( gnu-attributes[opt] declarator )
3766 direct-declarator array-declarator
3767 direct-declarator ( parameter-type-list )
3768 direct-declarator ( identifier-list[opt] )
3769
3770 pointer:
3771 * type-qualifier-list[opt]
3772 * type-qualifier-list[opt] pointer
3773
3774 type-qualifier-list:
3775 type-qualifier
3776 gnu-attributes
3777 type-qualifier-list type-qualifier
3778 type-qualifier-list gnu-attributes
3779
3780 array-declarator:
3781 [ type-qualifier-list[opt] assignment-expression[opt] ]
3782 [ static type-qualifier-list[opt] assignment-expression ]
3783 [ type-qualifier-list static assignment-expression ]
3784 [ type-qualifier-list[opt] * ]
3785
3786 parameter-type-list:
3787 parameter-list
3788 parameter-list , ...
3789
3790 parameter-list:
3791 parameter-declaration
3792 parameter-list , parameter-declaration
3793
3794 parameter-declaration:
3795 declaration-specifiers declarator gnu-attributes[opt]
3796 declaration-specifiers abstract-declarator[opt] gnu-attributes[opt]
3797
3798 identifier-list:
3799 identifier
3800 identifier-list , identifier
3801
3802 abstract-declarator:
3803 pointer
3804 pointer[opt] direct-abstract-declarator
3805
3806 direct-abstract-declarator:
3807 ( gnu-attributes[opt] abstract-declarator )
3808 direct-abstract-declarator[opt] array-declarator
3809 direct-abstract-declarator[opt] ( parameter-type-list[opt] )
3810
3811 GNU extensions:
3812
3813 direct-declarator:
3814 direct-declarator ( parameter-forward-declarations
3815 parameter-type-list[opt] )
3816
3817 direct-abstract-declarator:
3818 direct-abstract-declarator[opt] ( parameter-forward-declarations
3819 parameter-type-list[opt] )
3820
3821 parameter-forward-declarations:
3822 parameter-list ;
3823 parameter-forward-declarations parameter-list ;
3824
3825 The uses of gnu-attributes shown above are GNU extensions.
3826
3827 Some forms of array declarator are not included in C99 in the
3828 syntax for abstract declarators; these are disallowed elsewhere.
3829 This may be a defect (DR#289).
3830
3831 This function also accepts an omitted abstract declarator as being
3832 an abstract declarator, although not part of the formal syntax. */
3833
3834 struct c_declarator *
3835 c_parser_declarator (c_parser *parser, bool type_seen_p, c_dtr_syn kind,
3836 bool *seen_id)
3837 {
3838 /* Parse any initial pointer part. */
3839 if (c_parser_next_token_is (parser, CPP_MULT))
3840 {
3841 struct c_declspecs *quals_attrs = build_null_declspecs ();
3842 struct c_declarator *inner;
3843 c_parser_consume_token (parser);
3844 c_parser_declspecs (parser, quals_attrs, false, false, true,
3845 false, false, true, false, cla_prefer_id);
3846 inner = c_parser_declarator (parser, type_seen_p, kind, seen_id);
3847 if (inner == NULL)
3848 return NULL;
3849 else
3850 return make_pointer_declarator (quals_attrs, inner);
3851 }
3852 /* Now we have a direct declarator, direct abstract declarator or
3853 nothing (which counts as a direct abstract declarator here). */
3854 return c_parser_direct_declarator (parser, type_seen_p, kind, seen_id);
3855 }
3856
3857 /* Parse a direct declarator or direct abstract declarator; arguments
3858 as c_parser_declarator. */
3859
3860 static struct c_declarator *
3861 c_parser_direct_declarator (c_parser *parser, bool type_seen_p, c_dtr_syn kind,
3862 bool *seen_id)
3863 {
3864 /* The direct declarator must start with an identifier (possibly
3865 omitted) or a parenthesized declarator (possibly abstract). In
3866 an ordinary declarator, initial parentheses must start a
3867 parenthesized declarator. In an abstract declarator or parameter
3868 declarator, they could start a parenthesized declarator or a
3869 parameter list. To tell which, the open parenthesis and any
3870 following gnu-attributes must be read. If a declaration
3871 specifier or standard attributes follow, then it is a parameter
3872 list; if the specifier is a typedef name, there might be an
3873 ambiguity about redeclaring it, which is resolved in the
3874 direction of treating it as a typedef name. If a close
3875 parenthesis follows, it is also an empty parameter list, as the
3876 syntax does not permit empty abstract declarators. Otherwise, it
3877 is a parenthesized declarator (in which case the analysis may be
3878 repeated inside it, recursively).
3879
3880 ??? There is an ambiguity in a parameter declaration "int
3881 (__attribute__((foo)) x)", where x is not a typedef name: it
3882 could be an abstract declarator for a function, or declare x with
3883 parentheses. The proper resolution of this ambiguity needs
3884 documenting. At present we follow an accident of the old
3885 parser's implementation, whereby the first parameter must have
3886 some declaration specifiers other than just gnu-attributes. Thus as
3887 a parameter declaration it is treated as a parenthesized
3888 parameter named x, and as an abstract declarator it is
3889 rejected.
3890
3891 ??? Also following the old parser, gnu-attributes inside an empty
3892 parameter list are ignored, making it a list not yielding a
3893 prototype, rather than giving an error or making it have one
3894 parameter with implicit type int.
3895
3896 ??? Also following the old parser, typedef names may be
3897 redeclared in declarators, but not Objective-C class names. */
3898
3899 if (kind != C_DTR_ABSTRACT
3900 && c_parser_next_token_is (parser, CPP_NAME)
3901 && ((type_seen_p
3902 && (c_parser_peek_token (parser)->id_kind == C_ID_TYPENAME
3903 || c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME))
3904 || c_parser_peek_token (parser)->id_kind == C_ID_ID))
3905 {
3906 struct c_declarator *inner
3907 = build_id_declarator (c_parser_peek_token (parser)->value);
3908 *seen_id = true;
3909 inner->id_loc = c_parser_peek_token (parser)->location;
3910 c_parser_consume_token (parser);
3911 if (c_parser_nth_token_starts_std_attributes (parser, 1))
3912 inner->u.id.attrs = c_parser_std_attribute_specifier_sequence (parser);
3913 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
3914 }
3915
3916 if (kind != C_DTR_NORMAL
3917 && c_parser_next_token_is (parser, CPP_OPEN_SQUARE)
3918 && !c_parser_nth_token_starts_std_attributes (parser, 1))
3919 {
3920 struct c_declarator *inner = build_id_declarator (NULL_TREE);
3921 inner->id_loc = c_parser_peek_token (parser)->location;
3922 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
3923 }
3924
3925 /* Either we are at the end of an abstract declarator, or we have
3926 parentheses. */
3927
3928 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
3929 {
3930 tree attrs;
3931 struct c_declarator *inner;
3932 c_parser_consume_token (parser);
3933 bool have_gnu_attrs = c_parser_next_token_is_keyword (parser,
3934 RID_ATTRIBUTE);
3935 attrs = c_parser_gnu_attributes (parser);
3936 if (kind != C_DTR_NORMAL
3937 && (c_parser_next_token_starts_declspecs (parser)
3938 || (!have_gnu_attrs
3939 && c_parser_nth_token_starts_std_attributes (parser, 1))
3940 || c_parser_next_token_is (parser, CPP_CLOSE_PAREN)))
3941 {
3942 struct c_arg_info *args
3943 = c_parser_parms_declarator (parser, kind == C_DTR_NORMAL,
3944 attrs, have_gnu_attrs);
3945 if (args == NULL)
3946 return NULL;
3947 else
3948 {
3949 inner = build_id_declarator (NULL_TREE);
3950 if (!(args->types
3951 && args->types != error_mark_node
3952 && TREE_CODE (TREE_VALUE (args->types)) == IDENTIFIER_NODE)
3953 && c_parser_nth_token_starts_std_attributes (parser, 1))
3954 {
3955 tree std_attrs
3956 = c_parser_std_attribute_specifier_sequence (parser);
3957 if (std_attrs)
3958 inner = build_attrs_declarator (std_attrs, inner);
3959 }
3960 inner = build_function_declarator (args, inner);
3961 return c_parser_direct_declarator_inner (parser, *seen_id,
3962 inner);
3963 }
3964 }
3965 /* A parenthesized declarator. */
3966 inner = c_parser_declarator (parser, type_seen_p, kind, seen_id);
3967 if (inner != NULL && attrs != NULL)
3968 inner = build_attrs_declarator (attrs, inner);
3969 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3970 {
3971 c_parser_consume_token (parser);
3972 if (inner == NULL)
3973 return NULL;
3974 else
3975 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
3976 }
3977 else
3978 {
3979 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3980 "expected %<)%>");
3981 return NULL;
3982 }
3983 }
3984 else
3985 {
3986 if (kind == C_DTR_NORMAL)
3987 {
3988 c_parser_error (parser, "expected identifier or %<(%>");
3989 return NULL;
3990 }
3991 else
3992 return build_id_declarator (NULL_TREE);
3993 }
3994 }
3995
3996 /* Parse part of a direct declarator or direct abstract declarator,
3997 given that some (in INNER) has already been parsed; ID_PRESENT is
3998 true if an identifier is present, false for an abstract
3999 declarator. */
4000
4001 static struct c_declarator *
4002 c_parser_direct_declarator_inner (c_parser *parser, bool id_present,
4003 struct c_declarator *inner)
4004 {
4005 /* Parse a sequence of array declarators and parameter lists. */
4006 if (c_parser_next_token_is (parser, CPP_OPEN_SQUARE)
4007 && !c_parser_nth_token_starts_std_attributes (parser, 1))
4008 {
4009 location_t brace_loc = c_parser_peek_token (parser)->location;
4010 struct c_declarator *declarator;
4011 struct c_declspecs *quals_attrs = build_null_declspecs ();
4012 bool static_seen;
4013 bool star_seen;
4014 struct c_expr dimen;
4015 dimen.value = NULL_TREE;
4016 dimen.original_code = ERROR_MARK;
4017 dimen.original_type = NULL_TREE;
4018 c_parser_consume_token (parser);
4019 c_parser_declspecs (parser, quals_attrs, false, false, true,
4020 false, false, false, false, cla_prefer_id);
4021 static_seen = c_parser_next_token_is_keyword (parser, RID_STATIC);
4022 if (static_seen)
4023 c_parser_consume_token (parser);
4024 if (static_seen && !quals_attrs->declspecs_seen_p)
4025 c_parser_declspecs (parser, quals_attrs, false, false, true,
4026 false, false, false, false, cla_prefer_id);
4027 if (!quals_attrs->declspecs_seen_p)
4028 quals_attrs = NULL;
4029 /* If "static" is present, there must be an array dimension.
4030 Otherwise, there may be a dimension, "*", or no
4031 dimension. */
4032 if (static_seen)
4033 {
4034 star_seen = false;
4035 dimen = c_parser_expr_no_commas (parser, NULL);
4036 }
4037 else
4038 {
4039 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
4040 {
4041 dimen.value = NULL_TREE;
4042 star_seen = false;
4043 }
4044 else if (c_parser_next_token_is (parser, CPP_MULT))
4045 {
4046 if (c_parser_peek_2nd_token (parser)->type == CPP_CLOSE_SQUARE)
4047 {
4048 dimen.value = NULL_TREE;
4049 star_seen = true;
4050 c_parser_consume_token (parser);
4051 }
4052 else
4053 {
4054 star_seen = false;
4055 dimen = c_parser_expr_no_commas (parser, NULL);
4056 }
4057 }
4058 else
4059 {
4060 star_seen = false;
4061 dimen = c_parser_expr_no_commas (parser, NULL);
4062 }
4063 }
4064 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
4065 c_parser_consume_token (parser);
4066 else
4067 {
4068 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
4069 "expected %<]%>");
4070 return NULL;
4071 }
4072 if (dimen.value)
4073 dimen = convert_lvalue_to_rvalue (brace_loc, dimen, true, true);
4074 declarator = build_array_declarator (brace_loc, dimen.value, quals_attrs,
4075 static_seen, star_seen);
4076 if (declarator == NULL)
4077 return NULL;
4078 if (c_parser_nth_token_starts_std_attributes (parser, 1))
4079 {
4080 tree std_attrs
4081 = c_parser_std_attribute_specifier_sequence (parser);
4082 if (std_attrs)
4083 inner = build_attrs_declarator (std_attrs, inner);
4084 }
4085 inner = set_array_declarator_inner (declarator, inner);
4086 return c_parser_direct_declarator_inner (parser, id_present, inner);
4087 }
4088 else if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
4089 {
4090 tree attrs;
4091 struct c_arg_info *args;
4092 c_parser_consume_token (parser);
4093 bool have_gnu_attrs = c_parser_next_token_is_keyword (parser,
4094 RID_ATTRIBUTE);
4095 attrs = c_parser_gnu_attributes (parser);
4096 args = c_parser_parms_declarator (parser, id_present, attrs,
4097 have_gnu_attrs);
4098 if (args == NULL)
4099 return NULL;
4100 else
4101 {
4102 if (!(args->types
4103 && args->types != error_mark_node
4104 && TREE_CODE (TREE_VALUE (args->types)) == IDENTIFIER_NODE)
4105 && c_parser_nth_token_starts_std_attributes (parser, 1))
4106 {
4107 tree std_attrs
4108 = c_parser_std_attribute_specifier_sequence (parser);
4109 if (std_attrs)
4110 inner = build_attrs_declarator (std_attrs, inner);
4111 }
4112 inner = build_function_declarator (args, inner);
4113 return c_parser_direct_declarator_inner (parser, id_present, inner);
4114 }
4115 }
4116 return inner;
4117 }
4118
4119 /* Parse a parameter list or identifier list, including the closing
4120 parenthesis but not the opening one. ATTRS are the gnu-attributes
4121 at the start of the list. ID_LIST_OK is true if an identifier list
4122 is acceptable; such a list must not have attributes at the start.
4123 HAVE_GNU_ATTRS says whether any gnu-attributes (including empty
4124 attributes) were present (in which case standard attributes cannot
4125 occur). */
4126
4127 static struct c_arg_info *
4128 c_parser_parms_declarator (c_parser *parser, bool id_list_ok, tree attrs,
4129 bool have_gnu_attrs)
4130 {
4131 push_scope ();
4132 declare_parm_level ();
4133 /* If the list starts with an identifier, it is an identifier list.
4134 Otherwise, it is either a prototype list or an empty list. */
4135 if (id_list_ok
4136 && !attrs
4137 && c_parser_next_token_is (parser, CPP_NAME)
4138 && c_parser_peek_token (parser)->id_kind == C_ID_ID
4139
4140 /* Look ahead to detect typos in type names. */
4141 && c_parser_peek_2nd_token (parser)->type != CPP_NAME
4142 && c_parser_peek_2nd_token (parser)->type != CPP_MULT
4143 && c_parser_peek_2nd_token (parser)->type != CPP_OPEN_PAREN
4144 && c_parser_peek_2nd_token (parser)->type != CPP_OPEN_SQUARE
4145 && c_parser_peek_2nd_token (parser)->type != CPP_KEYWORD)
4146 {
4147 tree list = NULL_TREE, *nextp = &list;
4148 while (c_parser_next_token_is (parser, CPP_NAME)
4149 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
4150 {
4151 *nextp = build_tree_list (NULL_TREE,
4152 c_parser_peek_token (parser)->value);
4153 nextp = & TREE_CHAIN (*nextp);
4154 c_parser_consume_token (parser);
4155 if (c_parser_next_token_is_not (parser, CPP_COMMA))
4156 break;
4157 c_parser_consume_token (parser);
4158 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4159 {
4160 c_parser_error (parser, "expected identifier");
4161 break;
4162 }
4163 }
4164 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4165 {
4166 struct c_arg_info *ret = build_arg_info ();
4167 ret->types = list;
4168 c_parser_consume_token (parser);
4169 pop_scope ();
4170 return ret;
4171 }
4172 else
4173 {
4174 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
4175 "expected %<)%>");
4176 pop_scope ();
4177 return NULL;
4178 }
4179 }
4180 else
4181 {
4182 struct c_arg_info *ret
4183 = c_parser_parms_list_declarator (parser, attrs, NULL, have_gnu_attrs);
4184 pop_scope ();
4185 return ret;
4186 }
4187 }
4188
4189 /* Parse a parameter list (possibly empty), including the closing
4190 parenthesis but not the opening one. ATTRS are the gnu-attributes
4191 at the start of the list; if HAVE_GNU_ATTRS, there were some such
4192 attributes (possibly empty, in which case ATTRS is NULL_TREE),
4193 which means standard attributes cannot start the list. EXPR is
4194 NULL or an expression that needs to be evaluated for the side
4195 effects of array size expressions in the parameters. */
4196
4197 static struct c_arg_info *
4198 c_parser_parms_list_declarator (c_parser *parser, tree attrs, tree expr,
4199 bool have_gnu_attrs)
4200 {
4201 bool bad_parm = false;
4202
4203 /* ??? Following the old parser, forward parameter declarations may
4204 use abstract declarators, and if no real parameter declarations
4205 follow the forward declarations then this is not diagnosed. Also
4206 note as above that gnu-attributes are ignored as the only contents of
4207 the parentheses, or as the only contents after forward
4208 declarations. */
4209 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4210 {
4211 struct c_arg_info *ret = build_arg_info ();
4212 c_parser_consume_token (parser);
4213 return ret;
4214 }
4215 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
4216 {
4217 struct c_arg_info *ret = build_arg_info ();
4218
4219 if (flag_allow_parameterless_variadic_functions)
4220 {
4221 /* F (...) is allowed. */
4222 ret->types = NULL_TREE;
4223 }
4224 else
4225 {
4226 /* Suppress -Wold-style-definition for this case. */
4227 ret->types = error_mark_node;
4228 error_at (c_parser_peek_token (parser)->location,
4229 "ISO C requires a named argument before %<...%>");
4230 }
4231 c_parser_consume_token (parser);
4232 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4233 {
4234 c_parser_consume_token (parser);
4235 return ret;
4236 }
4237 else
4238 {
4239 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
4240 "expected %<)%>");
4241 return NULL;
4242 }
4243 }
4244 /* Nonempty list of parameters, either terminated with semicolon
4245 (forward declarations; recurse) or with close parenthesis (normal
4246 function) or with ", ... )" (variadic function). */
4247 while (true)
4248 {
4249 /* Parse a parameter. */
4250 struct c_parm *parm = c_parser_parameter_declaration (parser, attrs,
4251 have_gnu_attrs);
4252 attrs = NULL_TREE;
4253 have_gnu_attrs = false;
4254 if (parm == NULL)
4255 bad_parm = true;
4256 else
4257 push_parm_decl (parm, &expr);
4258 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
4259 {
4260 tree new_attrs;
4261 c_parser_consume_token (parser);
4262 mark_forward_parm_decls ();
4263 bool new_have_gnu_attrs
4264 = c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE);
4265 new_attrs = c_parser_gnu_attributes (parser);
4266 return c_parser_parms_list_declarator (parser, new_attrs, expr,
4267 new_have_gnu_attrs);
4268 }
4269 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4270 {
4271 c_parser_consume_token (parser);
4272 if (bad_parm)
4273 return NULL;
4274 else
4275 return get_parm_info (false, expr);
4276 }
4277 if (!c_parser_require (parser, CPP_COMMA,
4278 "expected %<;%>, %<,%> or %<)%>",
4279 UNKNOWN_LOCATION, false))
4280 {
4281 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
4282 return NULL;
4283 }
4284 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
4285 {
4286 c_parser_consume_token (parser);
4287 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4288 {
4289 c_parser_consume_token (parser);
4290 if (bad_parm)
4291 return NULL;
4292 else
4293 return get_parm_info (true, expr);
4294 }
4295 else
4296 {
4297 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
4298 "expected %<)%>");
4299 return NULL;
4300 }
4301 }
4302 }
4303 }
4304
4305 /* Parse a parameter declaration. ATTRS are the gnu-attributes at the
4306 start of the declaration if it is the first parameter;
4307 HAVE_GNU_ATTRS is true if there were any gnu-attributes there (even
4308 empty) there. */
4309
4310 static struct c_parm *
4311 c_parser_parameter_declaration (c_parser *parser, tree attrs,
4312 bool have_gnu_attrs)
4313 {
4314 struct c_declspecs *specs;
4315 struct c_declarator *declarator;
4316 tree prefix_attrs;
4317 tree postfix_attrs = NULL_TREE;
4318 bool dummy = false;
4319
4320 /* Accept #pragmas between parameter declarations. */
4321 while (c_parser_next_token_is (parser, CPP_PRAGMA))
4322 c_parser_pragma (parser, pragma_param, NULL);
4323
4324 if (!c_parser_next_token_starts_declspecs (parser)
4325 && !c_parser_nth_token_starts_std_attributes (parser, 1))
4326 {
4327 c_token *token = c_parser_peek_token (parser);
4328 if (parser->error)
4329 return NULL;
4330 c_parser_set_source_position_from_token (token);
4331 if (c_parser_next_tokens_start_typename (parser, cla_prefer_type))
4332 {
4333 auto_diagnostic_group d;
4334 name_hint hint = lookup_name_fuzzy (token->value,
4335 FUZZY_LOOKUP_TYPENAME,
4336 token->location);
4337 if (const char *suggestion = hint.suggestion ())
4338 {
4339 gcc_rich_location richloc (token->location);
4340 richloc.add_fixit_replace (suggestion);
4341 error_at (&richloc,
4342 "unknown type name %qE; did you mean %qs?",
4343 token->value, suggestion);
4344 }
4345 else
4346 error_at (token->location, "unknown type name %qE", token->value);
4347 parser->error = true;
4348 }
4349 /* ??? In some Objective-C cases '...' isn't applicable so there
4350 should be a different message. */
4351 else
4352 c_parser_error (parser,
4353 "expected declaration specifiers or %<...%>");
4354 c_parser_skip_to_end_of_parameter (parser);
4355 return NULL;
4356 }
4357
4358 location_t start_loc = c_parser_peek_token (parser)->location;
4359
4360 specs = build_null_declspecs ();
4361 if (attrs)
4362 {
4363 declspecs_add_attrs (input_location, specs, attrs);
4364 attrs = NULL_TREE;
4365 }
4366 c_parser_declspecs (parser, specs, true, true, true, true, false,
4367 !have_gnu_attrs, true, cla_nonabstract_decl);
4368 finish_declspecs (specs);
4369 pending_xref_error ();
4370 prefix_attrs = specs->attrs;
4371 specs->attrs = NULL_TREE;
4372 declarator = c_parser_declarator (parser,
4373 specs->typespec_kind != ctsk_none,
4374 C_DTR_PARM, &dummy);
4375 if (declarator == NULL)
4376 {
4377 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
4378 return NULL;
4379 }
4380 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
4381 postfix_attrs = c_parser_gnu_attributes (parser);
4382
4383 /* Generate a location for the parameter, ranging from the start of the
4384 initial token to the end of the final token.
4385
4386 If we have a identifier, then use it for the caret location, e.g.
4387
4388 extern int callee (int one, int (*two)(int, int), float three);
4389 ~~~~~~^~~~~~~~~~~~~~
4390
4391 otherwise, reuse the start location for the caret location e.g.:
4392
4393 extern int callee (int one, int (*)(int, int), float three);
4394 ^~~~~~~~~~~~~~~~~
4395 */
4396 location_t end_loc = parser->last_token_location;
4397
4398 /* Find any cdk_id declarator; determine if we have an identifier. */
4399 c_declarator *id_declarator = declarator;
4400 while (id_declarator && id_declarator->kind != cdk_id)
4401 id_declarator = id_declarator->declarator;
4402 location_t caret_loc = (id_declarator->u.id.id
4403 ? id_declarator->id_loc
4404 : start_loc);
4405 location_t param_loc = make_location (caret_loc, start_loc, end_loc);
4406
4407 return build_c_parm (specs, chainon (postfix_attrs, prefix_attrs),
4408 declarator, param_loc);
4409 }
4410
4411 /* Parse a string literal in an asm expression. It should not be
4412 translated, and wide string literals are an error although
4413 permitted by the syntax. This is a GNU extension.
4414
4415 asm-string-literal:
4416 string-literal
4417 */
4418
4419 static tree
4420 c_parser_asm_string_literal (c_parser *parser)
4421 {
4422 tree str;
4423 int save_flag = warn_overlength_strings;
4424 warn_overlength_strings = 0;
4425 str = c_parser_string_literal (parser, false, false).value;
4426 warn_overlength_strings = save_flag;
4427 return str;
4428 }
4429
4430 /* Parse a simple asm expression. This is used in restricted
4431 contexts, where a full expression with inputs and outputs does not
4432 make sense. This is a GNU extension.
4433
4434 simple-asm-expr:
4435 asm ( asm-string-literal )
4436 */
4437
4438 static tree
4439 c_parser_simple_asm_expr (c_parser *parser)
4440 {
4441 tree str;
4442 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ASM));
4443 c_parser_consume_token (parser);
4444 matching_parens parens;
4445 if (!parens.require_open (parser))
4446 return NULL_TREE;
4447 str = c_parser_asm_string_literal (parser);
4448 if (!parens.require_close (parser))
4449 {
4450 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
4451 return NULL_TREE;
4452 }
4453 return str;
4454 }
4455
4456 static tree
4457 c_parser_gnu_attribute_any_word (c_parser *parser)
4458 {
4459 tree attr_name = NULL_TREE;
4460
4461 if (c_parser_next_token_is (parser, CPP_KEYWORD))
4462 {
4463 /* ??? See comment above about what keywords are accepted here. */
4464 bool ok;
4465 switch (c_parser_peek_token (parser)->keyword)
4466 {
4467 case RID_STATIC:
4468 case RID_UNSIGNED:
4469 case RID_LONG:
4470 case RID_CONST:
4471 case RID_EXTERN:
4472 case RID_REGISTER:
4473 case RID_TYPEDEF:
4474 case RID_SHORT:
4475 case RID_INLINE:
4476 case RID_NORETURN:
4477 case RID_VOLATILE:
4478 case RID_SIGNED:
4479 case RID_AUTO:
4480 case RID_RESTRICT:
4481 case RID_COMPLEX:
4482 case RID_THREAD:
4483 case RID_INT:
4484 case RID_CHAR:
4485 case RID_FLOAT:
4486 case RID_DOUBLE:
4487 case RID_VOID:
4488 case RID_DFLOAT32:
4489 case RID_DFLOAT64:
4490 case RID_DFLOAT128:
4491 CASE_RID_FLOATN_NX:
4492 case RID_BOOL:
4493 case RID_FRACT:
4494 case RID_ACCUM:
4495 case RID_SAT:
4496 case RID_TRANSACTION_ATOMIC:
4497 case RID_TRANSACTION_CANCEL:
4498 case RID_ATOMIC:
4499 case RID_AUTO_TYPE:
4500 case RID_INT_N_0:
4501 case RID_INT_N_1:
4502 case RID_INT_N_2:
4503 case RID_INT_N_3:
4504 ok = true;
4505 break;
4506 default:
4507 ok = false;
4508 break;
4509 }
4510 if (!ok)
4511 return NULL_TREE;
4512
4513 /* Accept __attribute__((__const)) as __attribute__((const)) etc. */
4514 attr_name = ridpointers[(int) c_parser_peek_token (parser)->keyword];
4515 }
4516 else if (c_parser_next_token_is (parser, CPP_NAME))
4517 attr_name = c_parser_peek_token (parser)->value;
4518
4519 return attr_name;
4520 }
4521
4522 /* Parse attribute arguments. This is a common form of syntax
4523 covering all currently valid GNU and standard attributes.
4524
4525 gnu-attribute-arguments:
4526 identifier
4527 identifier , nonempty-expr-list
4528 expr-list
4529
4530 where the "identifier" must not be declared as a type. ??? Why not
4531 allow identifiers declared as types to start the arguments? */
4532
4533 static tree
4534 c_parser_attribute_arguments (c_parser *parser, bool takes_identifier,
4535 bool require_string, bool allow_empty_args)
4536 {
4537 vec<tree, va_gc> *expr_list;
4538 tree attr_args;
4539 /* Parse the attribute contents. If they start with an
4540 identifier which is followed by a comma or close
4541 parenthesis, then the arguments start with that
4542 identifier; otherwise they are an expression list.
4543 In objective-c the identifier may be a classname. */
4544 if (c_parser_next_token_is (parser, CPP_NAME)
4545 && (c_parser_peek_token (parser)->id_kind == C_ID_ID
4546 || (c_dialect_objc ()
4547 && c_parser_peek_token (parser)->id_kind
4548 == C_ID_CLASSNAME))
4549 && ((c_parser_peek_2nd_token (parser)->type == CPP_COMMA)
4550 || (c_parser_peek_2nd_token (parser)->type
4551 == CPP_CLOSE_PAREN))
4552 && (takes_identifier
4553 || (c_dialect_objc ()
4554 && c_parser_peek_token (parser)->id_kind
4555 == C_ID_CLASSNAME)))
4556 {
4557 tree arg1 = c_parser_peek_token (parser)->value;
4558 c_parser_consume_token (parser);
4559 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4560 attr_args = build_tree_list (NULL_TREE, arg1);
4561 else
4562 {
4563 tree tree_list;
4564 c_parser_consume_token (parser);
4565 expr_list = c_parser_expr_list (parser, false, true,
4566 NULL, NULL, NULL, NULL);
4567 tree_list = build_tree_list_vec (expr_list);
4568 attr_args = tree_cons (NULL_TREE, arg1, tree_list);
4569 release_tree_vector (expr_list);
4570 }
4571 }
4572 else
4573 {
4574 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4575 {
4576 if (!allow_empty_args)
4577 error_at (c_parser_peek_token (parser)->location,
4578 "parentheses must be omitted if "
4579 "attribute argument list is empty");
4580 attr_args = NULL_TREE;
4581 }
4582 else if (require_string)
4583 {
4584 /* The only valid argument for this attribute is a string
4585 literal. Handle this specially here to avoid accepting
4586 string literals with excess parentheses. */
4587 tree string = c_parser_string_literal (parser, false, true).value;
4588 attr_args = build_tree_list (NULL_TREE, string);
4589 }
4590 else
4591 {
4592 expr_list = c_parser_expr_list (parser, false, true,
4593 NULL, NULL, NULL, NULL);
4594 attr_args = build_tree_list_vec (expr_list);
4595 release_tree_vector (expr_list);
4596 }
4597 }
4598 return attr_args;
4599 }
4600
4601 /* Parse (possibly empty) gnu-attributes. This is a GNU extension.
4602
4603 gnu-attributes:
4604 empty
4605 gnu-attributes gnu-attribute
4606
4607 gnu-attribute:
4608 __attribute__ ( ( gnu-attribute-list ) )
4609
4610 gnu-attribute-list:
4611 gnu-attrib
4612 gnu-attribute_list , gnu-attrib
4613
4614 gnu-attrib:
4615 empty
4616 any-word
4617 any-word ( gnu-attribute-arguments )
4618
4619 where "any-word" may be any identifier (including one declared as a
4620 type), a reserved word storage class specifier, type specifier or
4621 type qualifier. ??? This still leaves out most reserved keywords
4622 (following the old parser), shouldn't we include them?
4623 When EXPECT_COMMA is true, expect the attribute to be preceded
4624 by a comma and fail if it isn't.
4625 When EMPTY_OK is true, allow and consume any number of consecutive
4626 commas with no attributes in between. */
4627
4628 static tree
4629 c_parser_gnu_attribute (c_parser *parser, tree attrs,
4630 bool expect_comma = false, bool empty_ok = true)
4631 {
4632 bool comma_first = c_parser_next_token_is (parser, CPP_COMMA);
4633 if (!comma_first
4634 && !c_parser_next_token_is (parser, CPP_NAME)
4635 && !c_parser_next_token_is (parser, CPP_KEYWORD))
4636 return NULL_TREE;
4637
4638 while (c_parser_next_token_is (parser, CPP_COMMA))
4639 {
4640 c_parser_consume_token (parser);
4641 if (!empty_ok)
4642 return attrs;
4643 }
4644
4645 tree attr_name = c_parser_gnu_attribute_any_word (parser);
4646 if (attr_name == NULL_TREE)
4647 return NULL_TREE;
4648
4649 attr_name = canonicalize_attr_name (attr_name);
4650 c_parser_consume_token (parser);
4651
4652 tree attr;
4653 if (c_parser_next_token_is_not (parser, CPP_OPEN_PAREN))
4654 {
4655 if (expect_comma && !comma_first)
4656 {
4657 /* A comma is missing between the last attribute on the chain
4658 and this one. */
4659 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
4660 "expected %<)%>");
4661 return error_mark_node;
4662 }
4663 attr = build_tree_list (attr_name, NULL_TREE);
4664 /* Add this attribute to the list. */
4665 attrs = chainon (attrs, attr);
4666 return attrs;
4667 }
4668 c_parser_consume_token (parser);
4669
4670 tree attr_args
4671 = c_parser_attribute_arguments (parser,
4672 attribute_takes_identifier_p (attr_name),
4673 false, true);
4674
4675 attr = build_tree_list (attr_name, attr_args);
4676 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4677 c_parser_consume_token (parser);
4678 else
4679 {
4680 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
4681 "expected %<)%>");
4682 return error_mark_node;
4683 }
4684
4685 if (expect_comma && !comma_first)
4686 {
4687 /* A comma is missing between the last attribute on the chain
4688 and this one. */
4689 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
4690 "expected %<)%>");
4691 return error_mark_node;
4692 }
4693
4694 /* Add this attribute to the list. */
4695 attrs = chainon (attrs, attr);
4696 return attrs;
4697 }
4698
4699 static tree
4700 c_parser_gnu_attributes (c_parser *parser)
4701 {
4702 tree attrs = NULL_TREE;
4703 while (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
4704 {
4705 bool save_translate_strings_p = parser->translate_strings_p;
4706 parser->translate_strings_p = false;
4707 /* Consume the `__attribute__' keyword. */
4708 c_parser_consume_token (parser);
4709 /* Look for the two `(' tokens. */
4710 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4711 {
4712 parser->translate_strings_p = save_translate_strings_p;
4713 return attrs;
4714 }
4715 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4716 {
4717 parser->translate_strings_p = save_translate_strings_p;
4718 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
4719 return attrs;
4720 }
4721 /* Parse the attribute list. Require a comma between successive
4722 (possibly empty) attributes. */
4723 for (bool expect_comma = false; ; expect_comma = true)
4724 {
4725 /* Parse a single attribute. */
4726 tree attr = c_parser_gnu_attribute (parser, attrs, expect_comma);
4727 if (attr == error_mark_node)
4728 return attrs;
4729 if (!attr)
4730 break;
4731 attrs = attr;
4732 }
4733
4734 /* Look for the two `)' tokens. */
4735 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4736 c_parser_consume_token (parser);
4737 else
4738 {
4739 parser->translate_strings_p = save_translate_strings_p;
4740 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
4741 "expected %<)%>");
4742 return attrs;
4743 }
4744 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4745 c_parser_consume_token (parser);
4746 else
4747 {
4748 parser->translate_strings_p = save_translate_strings_p;
4749 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
4750 "expected %<)%>");
4751 return attrs;
4752 }
4753 parser->translate_strings_p = save_translate_strings_p;
4754 }
4755
4756 return attrs;
4757 }
4758
4759 /* Parse an optional balanced token sequence.
4760
4761 balanced-token-sequence:
4762 balanced-token
4763 balanced-token-sequence balanced-token
4764
4765 balanced-token:
4766 ( balanced-token-sequence[opt] )
4767 [ balanced-token-sequence[opt] ]
4768 { balanced-token-sequence[opt] }
4769 any token other than ()[]{}
4770 */
4771
4772 static void
4773 c_parser_balanced_token_sequence (c_parser *parser)
4774 {
4775 while (true)
4776 {
4777 c_token *token = c_parser_peek_token (parser);
4778 switch (token->type)
4779 {
4780 case CPP_OPEN_BRACE:
4781 {
4782 matching_braces braces;
4783 braces.consume_open (parser);
4784 c_parser_balanced_token_sequence (parser);
4785 braces.require_close (parser);
4786 break;
4787 }
4788
4789 case CPP_OPEN_PAREN:
4790 {
4791 matching_parens parens;
4792 parens.consume_open (parser);
4793 c_parser_balanced_token_sequence (parser);
4794 parens.require_close (parser);
4795 break;
4796 }
4797
4798 case CPP_OPEN_SQUARE:
4799 c_parser_consume_token (parser);
4800 c_parser_balanced_token_sequence (parser);
4801 c_parser_require (parser, CPP_CLOSE_SQUARE, "expected %<]%>");
4802 break;
4803
4804 case CPP_CLOSE_BRACE:
4805 case CPP_CLOSE_PAREN:
4806 case CPP_CLOSE_SQUARE:
4807 case CPP_EOF:
4808 return;
4809
4810 default:
4811 c_parser_consume_token (parser);
4812 break;
4813 }
4814 }
4815 }
4816
4817 /* Parse standard (C2X) attributes (including GNU attributes in the
4818 gnu:: namespace).
4819
4820 attribute-specifier-sequence:
4821 attribute-specifier-sequence[opt] attribute-specifier
4822
4823 attribute-specifier:
4824 [ [ attribute-list ] ]
4825
4826 attribute-list:
4827 attribute[opt]
4828 attribute-list, attribute[opt]
4829
4830 attribute:
4831 attribute-token attribute-argument-clause[opt]
4832
4833 attribute-token:
4834 standard-attribute
4835 attribute-prefixed-token
4836
4837 standard-attribute:
4838 identifier
4839
4840 attribute-prefixed-token:
4841 attribute-prefix :: identifier
4842
4843 attribute-prefix:
4844 identifier
4845
4846 attribute-argument-clause:
4847 ( balanced-token-sequence[opt] )
4848
4849 Keywords are accepted as identifiers for this purpose.
4850 */
4851
4852 static tree
4853 c_parser_std_attribute (c_parser *parser, bool for_tm)
4854 {
4855 c_token *token = c_parser_peek_token (parser);
4856 tree ns, name, attribute;
4857
4858 /* Parse the attribute-token. */
4859 if (token->type != CPP_NAME && token->type != CPP_KEYWORD)
4860 {
4861 c_parser_error (parser, "expected identifier");
4862 return error_mark_node;
4863 }
4864 name = canonicalize_attr_name (token->value);
4865 c_parser_consume_token (parser);
4866 if (c_parser_next_token_is (parser, CPP_SCOPE))
4867 {
4868 ns = name;
4869 c_parser_consume_token (parser);
4870 token = c_parser_peek_token (parser);
4871 if (token->type != CPP_NAME && token->type != CPP_KEYWORD)
4872 {
4873 c_parser_error (parser, "expected identifier");
4874 return error_mark_node;
4875 }
4876 name = canonicalize_attr_name (token->value);
4877 c_parser_consume_token (parser);
4878 }
4879 else
4880 ns = NULL_TREE;
4881 attribute = build_tree_list (build_tree_list (ns, name), NULL_TREE);
4882
4883 /* Parse the arguments, if any. */
4884 const attribute_spec *as = lookup_attribute_spec (TREE_PURPOSE (attribute));
4885 if (c_parser_next_token_is_not (parser, CPP_OPEN_PAREN))
4886 goto out;
4887 {
4888 location_t open_loc = c_parser_peek_token (parser)->location;
4889 matching_parens parens;
4890 parens.consume_open (parser);
4891 if ((as && as->max_length == 0)
4892 /* Special-case the transactional-memory attribute "outer",
4893 which is specially handled but not registered as an
4894 attribute, to avoid allowing arbitrary balanced token
4895 sequences as arguments. */
4896 || is_attribute_p ("outer", name))
4897 {
4898 error_at (open_loc, "%qE attribute does not take any arguments", name);
4899 parens.skip_until_found_close (parser);
4900 return error_mark_node;
4901 }
4902 if (as)
4903 {
4904 bool takes_identifier
4905 = (ns != NULL_TREE
4906 && strcmp (IDENTIFIER_POINTER (ns), "gnu") == 0
4907 && attribute_takes_identifier_p (name));
4908 bool require_string
4909 = (ns == NULL_TREE
4910 && strcmp (IDENTIFIER_POINTER (name), "deprecated") == 0);
4911 TREE_VALUE (attribute)
4912 = c_parser_attribute_arguments (parser, takes_identifier,
4913 require_string, false);
4914 }
4915 else
4916 c_parser_balanced_token_sequence (parser);
4917 parens.require_close (parser);
4918 }
4919 out:
4920 if (ns == NULL_TREE && !for_tm && !as && !is_attribute_p ("nodiscard", name))
4921 {
4922 /* An attribute with standard syntax and no namespace specified
4923 is a constraint violation if it is not one of the known
4924 standard attributes (of which nodiscard is the only one
4925 without a handler in GCC). Diagnose it here with a pedwarn
4926 and then discard it to prevent a duplicate warning later. */
4927 pedwarn (input_location, OPT_Wattributes, "%qE attribute ignored",
4928 name);
4929 return error_mark_node;
4930 }
4931 return attribute;
4932 }
4933
4934 static tree
4935 c_parser_std_attribute_specifier (c_parser *parser, bool for_tm)
4936 {
4937 bool seen_deprecated = false;
4938 bool seen_fallthrough = false;
4939 bool seen_maybe_unused = false;
4940 location_t loc = c_parser_peek_token (parser)->location;
4941 if (!c_parser_require (parser, CPP_OPEN_SQUARE, "expected %<[%>"))
4942 return NULL_TREE;
4943 if (!c_parser_require (parser, CPP_OPEN_SQUARE, "expected %<[%>"))
4944 {
4945 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, "expected %<]%>");
4946 return NULL_TREE;
4947 }
4948 if (!for_tm)
4949 pedwarn_c11 (loc, OPT_Wpedantic,
4950 "ISO C does not support %<[[]]%> attributes before C2X");
4951 tree attributes = NULL_TREE;
4952 while (true)
4953 {
4954 c_token *token = c_parser_peek_token (parser);
4955 if (token->type == CPP_CLOSE_SQUARE)
4956 break;
4957 if (token->type == CPP_COMMA)
4958 {
4959 c_parser_consume_token (parser);
4960 continue;
4961 }
4962 tree attribute = c_parser_std_attribute (parser, for_tm);
4963 if (attribute != error_mark_node)
4964 {
4965 bool duplicate = false;
4966 tree name = get_attribute_name (attribute);
4967 tree ns = get_attribute_namespace (attribute);
4968 if (ns == NULL_TREE)
4969 {
4970 /* Some standard attributes may appear at most once in
4971 each attribute list. Diagnose duplicates and remove
4972 them from the list to avoid subsequent diagnostics
4973 such as the more general one for multiple
4974 "fallthrough" attributes in the same place (including
4975 in separate attribute lists in the same attribute
4976 specifier sequence, which is not a constraint
4977 violation). */
4978 if (is_attribute_p ("deprecated", name))
4979 {
4980 if (seen_deprecated)
4981 {
4982 error ("attribute %<deprecated%> can appear at most "
4983 "once in an attribute-list");
4984 duplicate = true;
4985 }
4986 seen_deprecated = true;
4987 }
4988 else if (is_attribute_p ("fallthrough", name))
4989 {
4990 if (seen_fallthrough)
4991 {
4992 error ("attribute %<fallthrough%> can appear at most "
4993 "once in an attribute-list");
4994 duplicate = true;
4995 }
4996 seen_fallthrough = true;
4997 }
4998 else if (is_attribute_p ("maybe_unused", name))
4999 {
5000 if (seen_maybe_unused)
5001 {
5002 error ("attribute %<maybe_unused%> can appear at most "
5003 "once in an attribute-list");
5004 duplicate = true;
5005 }
5006 seen_maybe_unused = true;
5007 }
5008 }
5009 if (!duplicate)
5010 {
5011 TREE_CHAIN (attribute) = attributes;
5012 attributes = attribute;
5013 }
5014 }
5015 if (c_parser_next_token_is_not (parser, CPP_COMMA))
5016 break;
5017 }
5018 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, "expected %<]%>");
5019 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, "expected %<]%>");
5020 return nreverse (attributes);
5021 }
5022
5023 /* Look past an optional balanced token sequence of raw look-ahead
5024 tokens starting with the *Nth token. *N is updated to point to the
5025 following token. Return true if such a sequence was found, false
5026 if the tokens parsed were not balanced. */
5027
5028 static bool
5029 c_parser_check_balanced_raw_token_sequence (c_parser *parser, unsigned int *n)
5030 {
5031 while (true)
5032 {
5033 c_token *token = c_parser_peek_nth_token_raw (parser, *n);
5034 switch (token->type)
5035 {
5036 case CPP_OPEN_BRACE:
5037 {
5038 ++*n;
5039 if (c_parser_check_balanced_raw_token_sequence (parser, n))
5040 {
5041 token = c_parser_peek_nth_token_raw (parser, *n);
5042 if (token->type == CPP_CLOSE_BRACE)
5043 ++*n;
5044 else
5045 return false;
5046 }
5047 else
5048 return false;
5049 break;
5050 }
5051
5052 case CPP_OPEN_PAREN:
5053 {
5054 ++*n;
5055 if (c_parser_check_balanced_raw_token_sequence (parser, n))
5056 {
5057 token = c_parser_peek_nth_token_raw (parser, *n);
5058 if (token->type == CPP_CLOSE_PAREN)
5059 ++*n;
5060 else
5061 return false;
5062 }
5063 else
5064 return false;
5065 break;
5066 }
5067
5068 case CPP_OPEN_SQUARE:
5069 {
5070 ++*n;
5071 if (c_parser_check_balanced_raw_token_sequence (parser, n))
5072 {
5073 token = c_parser_peek_nth_token_raw (parser, *n);
5074 if (token->type == CPP_CLOSE_SQUARE)
5075 ++*n;
5076 else
5077 return false;
5078 }
5079 else
5080 return false;
5081 break;
5082 }
5083
5084 case CPP_CLOSE_BRACE:
5085 case CPP_CLOSE_PAREN:
5086 case CPP_CLOSE_SQUARE:
5087 case CPP_EOF:
5088 return true;
5089
5090 default:
5091 ++*n;
5092 break;
5093 }
5094 }
5095 }
5096
5097 /* Return whether standard attributes start with the Nth token. */
5098
5099 static bool
5100 c_parser_nth_token_starts_std_attributes (c_parser *parser, unsigned int n)
5101 {
5102 if (!(c_parser_peek_nth_token (parser, n)->type == CPP_OPEN_SQUARE
5103 && c_parser_peek_nth_token (parser, n + 1)->type == CPP_OPEN_SQUARE))
5104 return false;
5105 /* In C, '[[' must start attributes. In Objective-C, we need to
5106 check whether '[[' is matched by ']]'. */
5107 if (!c_dialect_objc ())
5108 return true;
5109 n += 2;
5110 if (!c_parser_check_balanced_raw_token_sequence (parser, &n))
5111 return false;
5112 c_token *token = c_parser_peek_nth_token_raw (parser, n);
5113 if (token->type != CPP_CLOSE_SQUARE)
5114 return false;
5115 token = c_parser_peek_nth_token_raw (parser, n + 1);
5116 return token->type == CPP_CLOSE_SQUARE;
5117 }
5118
5119 static tree
5120 c_parser_std_attribute_specifier_sequence (c_parser *parser)
5121 {
5122 tree attributes = NULL_TREE;
5123 do
5124 {
5125 tree attrs = c_parser_std_attribute_specifier (parser, false);
5126 attributes = chainon (attributes, attrs);
5127 }
5128 while (c_parser_nth_token_starts_std_attributes (parser, 1));
5129 return attributes;
5130 }
5131
5132 /* Parse a type name (C90 6.5.5, C99 6.7.6, C11 6.7.7). ALIGNAS_OK
5133 says whether alignment specifiers are OK (only in cases that might
5134 be the type name of a compound literal).
5135
5136 type-name:
5137 specifier-qualifier-list abstract-declarator[opt]
5138 */
5139
5140 struct c_type_name *
5141 c_parser_type_name (c_parser *parser, bool alignas_ok)
5142 {
5143 struct c_declspecs *specs = build_null_declspecs ();
5144 struct c_declarator *declarator;
5145 struct c_type_name *ret;
5146 bool dummy = false;
5147 c_parser_declspecs (parser, specs, false, true, true, alignas_ok, false,
5148 false, true, cla_prefer_type);
5149 if (!specs->declspecs_seen_p)
5150 {
5151 c_parser_error (parser, "expected specifier-qualifier-list");
5152 return NULL;
5153 }
5154 if (specs->type != error_mark_node)
5155 {
5156 pending_xref_error ();
5157 finish_declspecs (specs);
5158 }
5159 declarator = c_parser_declarator (parser,
5160 specs->typespec_kind != ctsk_none,
5161 C_DTR_ABSTRACT, &dummy);
5162 if (declarator == NULL)
5163 return NULL;
5164 ret = XOBNEW (&parser_obstack, struct c_type_name);
5165 ret->specs = specs;
5166 ret->declarator = declarator;
5167 return ret;
5168 }
5169
5170 /* Parse an initializer (C90 6.5.7, C99 6.7.8, C11 6.7.9).
5171
5172 initializer:
5173 assignment-expression
5174 { initializer-list }
5175 { initializer-list , }
5176
5177 initializer-list:
5178 designation[opt] initializer
5179 initializer-list , designation[opt] initializer
5180
5181 designation:
5182 designator-list =
5183
5184 designator-list:
5185 designator
5186 designator-list designator
5187
5188 designator:
5189 array-designator
5190 . identifier
5191
5192 array-designator:
5193 [ constant-expression ]
5194
5195 GNU extensions:
5196
5197 initializer:
5198 { }
5199
5200 designation:
5201 array-designator
5202 identifier :
5203
5204 array-designator:
5205 [ constant-expression ... constant-expression ]
5206
5207 Any expression without commas is accepted in the syntax for the
5208 constant-expressions, with non-constant expressions rejected later.
5209
5210 This function is only used for top-level initializers; for nested
5211 ones, see c_parser_initval. */
5212
5213 static struct c_expr
5214 c_parser_initializer (c_parser *parser)
5215 {
5216 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
5217 return c_parser_braced_init (parser, NULL_TREE, false, NULL);
5218 else
5219 {
5220 struct c_expr ret;
5221 location_t loc = c_parser_peek_token (parser)->location;
5222 ret = c_parser_expr_no_commas (parser, NULL);
5223 if (TREE_CODE (ret.value) != STRING_CST
5224 && TREE_CODE (ret.value) != COMPOUND_LITERAL_EXPR)
5225 ret = convert_lvalue_to_rvalue (loc, ret, true, true);
5226 return ret;
5227 }
5228 }
5229
5230 /* The location of the last comma within the current initializer list,
5231 or UNKNOWN_LOCATION if not within one. */
5232
5233 location_t last_init_list_comma;
5234
5235 /* Parse a braced initializer list. TYPE is the type specified for a
5236 compound literal, and NULL_TREE for other initializers and for
5237 nested braced lists. NESTED_P is true for nested braced lists,
5238 false for the list of a compound literal or the list that is the
5239 top-level initializer in a declaration. */
5240
5241 static struct c_expr
5242 c_parser_braced_init (c_parser *parser, tree type, bool nested_p,
5243 struct obstack *outer_obstack)
5244 {
5245 struct c_expr ret;
5246 struct obstack braced_init_obstack;
5247 location_t brace_loc = c_parser_peek_token (parser)->location;
5248 gcc_obstack_init (&braced_init_obstack);
5249 gcc_assert (c_parser_next_token_is (parser, CPP_OPEN_BRACE));
5250 matching_braces braces;
5251 braces.consume_open (parser);
5252 if (nested_p)
5253 {
5254 finish_implicit_inits (brace_loc, outer_obstack);
5255 push_init_level (brace_loc, 0, &braced_init_obstack);
5256 }
5257 else
5258 really_start_incremental_init (type);
5259 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
5260 {
5261 pedwarn (brace_loc, OPT_Wpedantic, "ISO C forbids empty initializer braces");
5262 }
5263 else
5264 {
5265 /* Parse a non-empty initializer list, possibly with a trailing
5266 comma. */
5267 while (true)
5268 {
5269 c_parser_initelt (parser, &braced_init_obstack);
5270 if (parser->error)
5271 break;
5272 if (c_parser_next_token_is (parser, CPP_COMMA))
5273 {
5274 last_init_list_comma = c_parser_peek_token (parser)->location;
5275 c_parser_consume_token (parser);
5276 }
5277 else
5278 break;
5279 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
5280 break;
5281 }
5282 }
5283 c_token *next_tok = c_parser_peek_token (parser);
5284 if (next_tok->type != CPP_CLOSE_BRACE)
5285 {
5286 ret.set_error ();
5287 ret.original_code = ERROR_MARK;
5288 ret.original_type = NULL;
5289 braces.skip_until_found_close (parser);
5290 pop_init_level (brace_loc, 0, &braced_init_obstack, last_init_list_comma);
5291 obstack_free (&braced_init_obstack, NULL);
5292 return ret;
5293 }
5294 location_t close_loc = next_tok->location;
5295 c_parser_consume_token (parser);
5296 ret = pop_init_level (brace_loc, 0, &braced_init_obstack, close_loc);
5297 obstack_free (&braced_init_obstack, NULL);
5298 set_c_expr_source_range (&ret, brace_loc, close_loc);
5299 return ret;
5300 }
5301
5302 /* Parse a nested initializer, including designators. */
5303
5304 static void
5305 c_parser_initelt (c_parser *parser, struct obstack * braced_init_obstack)
5306 {
5307 /* Parse any designator or designator list. A single array
5308 designator may have the subsequent "=" omitted in GNU C, but a
5309 longer list or a structure member designator may not. */
5310 if (c_parser_next_token_is (parser, CPP_NAME)
5311 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
5312 {
5313 /* Old-style structure member designator. */
5314 set_init_label (c_parser_peek_token (parser)->location,
5315 c_parser_peek_token (parser)->value,
5316 c_parser_peek_token (parser)->location,
5317 braced_init_obstack);
5318 /* Use the colon as the error location. */
5319 pedwarn (c_parser_peek_2nd_token (parser)->location, OPT_Wpedantic,
5320 "obsolete use of designated initializer with %<:%>");
5321 c_parser_consume_token (parser);
5322 c_parser_consume_token (parser);
5323 }
5324 else
5325 {
5326 /* des_seen is 0 if there have been no designators, 1 if there
5327 has been a single array designator and 2 otherwise. */
5328 int des_seen = 0;
5329 /* Location of a designator. */
5330 location_t des_loc = UNKNOWN_LOCATION; /* Quiet warning. */
5331 while (c_parser_next_token_is (parser, CPP_OPEN_SQUARE)
5332 || c_parser_next_token_is (parser, CPP_DOT))
5333 {
5334 int des_prev = des_seen;
5335 if (!des_seen)
5336 des_loc = c_parser_peek_token (parser)->location;
5337 if (des_seen < 2)
5338 des_seen++;
5339 if (c_parser_next_token_is (parser, CPP_DOT))
5340 {
5341 des_seen = 2;
5342 c_parser_consume_token (parser);
5343 if (c_parser_next_token_is (parser, CPP_NAME))
5344 {
5345 set_init_label (des_loc, c_parser_peek_token (parser)->value,
5346 c_parser_peek_token (parser)->location,
5347 braced_init_obstack);
5348 c_parser_consume_token (parser);
5349 }
5350 else
5351 {
5352 struct c_expr init;
5353 init.set_error ();
5354 init.original_code = ERROR_MARK;
5355 init.original_type = NULL;
5356 c_parser_error (parser, "expected identifier");
5357 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
5358 process_init_element (input_location, init, false,
5359 braced_init_obstack);
5360 return;
5361 }
5362 }
5363 else
5364 {
5365 tree first, second;
5366 location_t ellipsis_loc = UNKNOWN_LOCATION; /* Quiet warning. */
5367 location_t array_index_loc = UNKNOWN_LOCATION;
5368 /* ??? Following the old parser, [ objc-receiver
5369 objc-message-args ] is accepted as an initializer,
5370 being distinguished from a designator by what follows
5371 the first assignment expression inside the square
5372 brackets, but after a first array designator a
5373 subsequent square bracket is for Objective-C taken to
5374 start an expression, using the obsolete form of
5375 designated initializer without '=', rather than
5376 possibly being a second level of designation: in LALR
5377 terms, the '[' is shifted rather than reducing
5378 designator to designator-list. */
5379 if (des_prev == 1 && c_dialect_objc ())
5380 {
5381 des_seen = des_prev;
5382 break;
5383 }
5384 if (des_prev == 0 && c_dialect_objc ())
5385 {
5386 /* This might be an array designator or an
5387 Objective-C message expression. If the former,
5388 continue parsing here; if the latter, parse the
5389 remainder of the initializer given the starting
5390 primary-expression. ??? It might make sense to
5391 distinguish when des_prev == 1 as well; see
5392 previous comment. */
5393 tree rec, args;
5394 struct c_expr mexpr;
5395 c_parser_consume_token (parser);
5396 if (c_parser_peek_token (parser)->type == CPP_NAME
5397 && ((c_parser_peek_token (parser)->id_kind
5398 == C_ID_TYPENAME)
5399 || (c_parser_peek_token (parser)->id_kind
5400 == C_ID_CLASSNAME)))
5401 {
5402 /* Type name receiver. */
5403 tree id = c_parser_peek_token (parser)->value;
5404 c_parser_consume_token (parser);
5405 rec = objc_get_class_reference (id);
5406 goto parse_message_args;
5407 }
5408 first = c_parser_expr_no_commas (parser, NULL).value;
5409 mark_exp_read (first);
5410 if (c_parser_next_token_is (parser, CPP_ELLIPSIS)
5411 || c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
5412 goto array_desig_after_first;
5413 /* Expression receiver. So far only one part
5414 without commas has been parsed; there might be
5415 more of the expression. */
5416 rec = first;
5417 while (c_parser_next_token_is (parser, CPP_COMMA))
5418 {
5419 struct c_expr next;
5420 location_t comma_loc, exp_loc;
5421 comma_loc = c_parser_peek_token (parser)->location;
5422 c_parser_consume_token (parser);
5423 exp_loc = c_parser_peek_token (parser)->location;
5424 next = c_parser_expr_no_commas (parser, NULL);
5425 next = convert_lvalue_to_rvalue (exp_loc, next,
5426 true, true);
5427 rec = build_compound_expr (comma_loc, rec, next.value);
5428 }
5429 parse_message_args:
5430 /* Now parse the objc-message-args. */
5431 args = c_parser_objc_message_args (parser);
5432 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
5433 "expected %<]%>");
5434 mexpr.value
5435 = objc_build_message_expr (rec, args);
5436 mexpr.original_code = ERROR_MARK;
5437 mexpr.original_type = NULL;
5438 /* Now parse and process the remainder of the
5439 initializer, starting with this message
5440 expression as a primary-expression. */
5441 c_parser_initval (parser, &mexpr, braced_init_obstack);
5442 return;
5443 }
5444 c_parser_consume_token (parser);
5445 array_index_loc = c_parser_peek_token (parser)->location;
5446 first = c_parser_expr_no_commas (parser, NULL).value;
5447 mark_exp_read (first);
5448 array_desig_after_first:
5449 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
5450 {
5451 ellipsis_loc = c_parser_peek_token (parser)->location;
5452 c_parser_consume_token (parser);
5453 second = c_parser_expr_no_commas (parser, NULL).value;
5454 mark_exp_read (second);
5455 }
5456 else
5457 second = NULL_TREE;
5458 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
5459 {
5460 c_parser_consume_token (parser);
5461 set_init_index (array_index_loc, first, second,
5462 braced_init_obstack);
5463 if (second)
5464 pedwarn (ellipsis_loc, OPT_Wpedantic,
5465 "ISO C forbids specifying range of elements to initialize");
5466 }
5467 else
5468 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
5469 "expected %<]%>");
5470 }
5471 }
5472 if (des_seen >= 1)
5473 {
5474 if (c_parser_next_token_is (parser, CPP_EQ))
5475 {
5476 pedwarn_c90 (des_loc, OPT_Wpedantic,
5477 "ISO C90 forbids specifying subobject "
5478 "to initialize");
5479 c_parser_consume_token (parser);
5480 }
5481 else
5482 {
5483 if (des_seen == 1)
5484 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
5485 "obsolete use of designated initializer without %<=%>");
5486 else
5487 {
5488 struct c_expr init;
5489 init.set_error ();
5490 init.original_code = ERROR_MARK;
5491 init.original_type = NULL;
5492 c_parser_error (parser, "expected %<=%>");
5493 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
5494 process_init_element (input_location, init, false,
5495 braced_init_obstack);
5496 return;
5497 }
5498 }
5499 }
5500 }
5501 c_parser_initval (parser, NULL, braced_init_obstack);
5502 }
5503
5504 /* Parse a nested initializer; as c_parser_initializer but parses
5505 initializers within braced lists, after any designators have been
5506 applied. If AFTER is not NULL then it is an Objective-C message
5507 expression which is the primary-expression starting the
5508 initializer. */
5509
5510 static void
5511 c_parser_initval (c_parser *parser, struct c_expr *after,
5512 struct obstack * braced_init_obstack)
5513 {
5514 struct c_expr init;
5515 gcc_assert (!after || c_dialect_objc ());
5516 location_t loc = c_parser_peek_token (parser)->location;
5517
5518 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE) && !after)
5519 init = c_parser_braced_init (parser, NULL_TREE, true,
5520 braced_init_obstack);
5521 else
5522 {
5523 init = c_parser_expr_no_commas (parser, after);
5524 if (init.value != NULL_TREE
5525 && TREE_CODE (init.value) != STRING_CST
5526 && TREE_CODE (init.value) != COMPOUND_LITERAL_EXPR)
5527 init = convert_lvalue_to_rvalue (loc, init, true, true);
5528 }
5529 process_init_element (loc, init, false, braced_init_obstack);
5530 }
5531
5532 /* Parse a compound statement (possibly a function body) (C90 6.6.2,
5533 C99 6.8.2, C11 6.8.2).
5534
5535 compound-statement:
5536 { block-item-list[opt] }
5537 { label-declarations block-item-list }
5538
5539 block-item-list:
5540 block-item
5541 block-item-list block-item
5542
5543 block-item:
5544 nested-declaration
5545 statement
5546
5547 nested-declaration:
5548 declaration
5549
5550 GNU extensions:
5551
5552 compound-statement:
5553 { label-declarations block-item-list }
5554
5555 nested-declaration:
5556 __extension__ nested-declaration
5557 nested-function-definition
5558
5559 label-declarations:
5560 label-declaration
5561 label-declarations label-declaration
5562
5563 label-declaration:
5564 __label__ identifier-list ;
5565
5566 Allowing the mixing of declarations and code is new in C99. The
5567 GNU syntax also permits (not shown above) labels at the end of
5568 compound statements, which yield an error. We don't allow labels
5569 on declarations; this might seem like a natural extension, but
5570 there would be a conflict between gnu-attributes on the label and
5571 prefix gnu-attributes on the declaration. ??? The syntax follows the
5572 old parser in requiring something after label declarations.
5573 Although they are erroneous if the labels declared aren't defined,
5574 is it useful for the syntax to be this way?
5575
5576 OpenACC:
5577
5578 block-item:
5579 openacc-directive
5580
5581 openacc-directive:
5582 update-directive
5583
5584 OpenMP:
5585
5586 block-item:
5587 openmp-directive
5588
5589 openmp-directive:
5590 barrier-directive
5591 flush-directive
5592 taskwait-directive
5593 taskyield-directive
5594 cancel-directive
5595 cancellation-point-directive */
5596
5597 static tree
5598 c_parser_compound_statement (c_parser *parser)
5599 {
5600 tree stmt;
5601 location_t brace_loc;
5602 brace_loc = c_parser_peek_token (parser)->location;
5603 if (!c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
5604 {
5605 /* Ensure a scope is entered and left anyway to avoid confusion
5606 if we have just prepared to enter a function body. */
5607 stmt = c_begin_compound_stmt (true);
5608 c_end_compound_stmt (brace_loc, stmt, true);
5609 return error_mark_node;
5610 }
5611 stmt = c_begin_compound_stmt (true);
5612 c_parser_compound_statement_nostart (parser);
5613
5614 return c_end_compound_stmt (brace_loc, stmt, true);
5615 }
5616
5617 /* Parse a compound statement except for the opening brace. This is
5618 used for parsing both compound statements and statement expressions
5619 (which follow different paths to handling the opening). */
5620
5621 static void
5622 c_parser_compound_statement_nostart (c_parser *parser)
5623 {
5624 bool last_stmt = false;
5625 bool last_label = false;
5626 bool save_valid_for_pragma = valid_location_for_stdc_pragma_p ();
5627 location_t label_loc = UNKNOWN_LOCATION; /* Quiet warning. */
5628 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
5629 {
5630 add_debug_begin_stmt (c_parser_peek_token (parser)->location);
5631 c_parser_consume_token (parser);
5632 return;
5633 }
5634 mark_valid_location_for_stdc_pragma (true);
5635 if (c_parser_next_token_is_keyword (parser, RID_LABEL))
5636 {
5637 /* Read zero or more forward-declarations for labels that nested
5638 functions can jump to. */
5639 mark_valid_location_for_stdc_pragma (false);
5640 while (c_parser_next_token_is_keyword (parser, RID_LABEL))
5641 {
5642 label_loc = c_parser_peek_token (parser)->location;
5643 c_parser_consume_token (parser);
5644 /* Any identifiers, including those declared as type names,
5645 are OK here. */
5646 while (true)
5647 {
5648 tree label;
5649 if (c_parser_next_token_is_not (parser, CPP_NAME))
5650 {
5651 c_parser_error (parser, "expected identifier");
5652 break;
5653 }
5654 label
5655 = declare_label (c_parser_peek_token (parser)->value);
5656 C_DECLARED_LABEL_FLAG (label) = 1;
5657 add_stmt (build_stmt (label_loc, DECL_EXPR, label));
5658 c_parser_consume_token (parser);
5659 if (c_parser_next_token_is (parser, CPP_COMMA))
5660 c_parser_consume_token (parser);
5661 else
5662 break;
5663 }
5664 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
5665 }
5666 pedwarn (label_loc, OPT_Wpedantic, "ISO C forbids label declarations");
5667 }
5668 /* We must now have at least one statement, label or declaration. */
5669 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
5670 {
5671 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
5672 c_parser_error (parser, "expected declaration or statement");
5673 c_parser_consume_token (parser);
5674 return;
5675 }
5676 while (c_parser_next_token_is_not (parser, CPP_CLOSE_BRACE))
5677 {
5678 location_t loc = c_parser_peek_token (parser)->location;
5679 loc = expansion_point_location_if_in_system_header (loc);
5680 /* Standard attributes may start a statement or a declaration. */
5681 bool have_std_attrs
5682 = c_parser_nth_token_starts_std_attributes (parser, 1);
5683 tree std_attrs = NULL_TREE;
5684 if (have_std_attrs)
5685 std_attrs = c_parser_std_attribute_specifier_sequence (parser);
5686 if (c_parser_next_token_is_keyword (parser, RID_CASE)
5687 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
5688 || (c_parser_next_token_is (parser, CPP_NAME)
5689 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
5690 {
5691 c_warn_unused_attributes (std_attrs);
5692 if (c_parser_next_token_is_keyword (parser, RID_CASE))
5693 label_loc = c_parser_peek_2nd_token (parser)->location;
5694 else
5695 label_loc = c_parser_peek_token (parser)->location;
5696 last_label = true;
5697 last_stmt = false;
5698 mark_valid_location_for_stdc_pragma (false);
5699 c_parser_label (parser);
5700 }
5701 else if (!last_label
5702 && (c_parser_next_tokens_start_declaration (parser)
5703 || (have_std_attrs
5704 && c_parser_next_token_is (parser, CPP_SEMICOLON))))
5705 {
5706 last_label = false;
5707 mark_valid_location_for_stdc_pragma (false);
5708 bool fallthru_attr_p = false;
5709 c_parser_declaration_or_fndef (parser, true, !have_std_attrs,
5710 true, true, true, NULL,
5711 vNULL, have_std_attrs, std_attrs,
5712 NULL, &fallthru_attr_p);
5713 if (last_stmt && !fallthru_attr_p)
5714 pedwarn_c90 (loc, OPT_Wdeclaration_after_statement,
5715 "ISO C90 forbids mixed declarations and code");
5716 last_stmt = fallthru_attr_p;
5717 }
5718 else if (!last_label
5719 && c_parser_next_token_is_keyword (parser, RID_EXTENSION))
5720 {
5721 /* __extension__ can start a declaration, but is also an
5722 unary operator that can start an expression. Consume all
5723 but the last of a possible series of __extension__ to
5724 determine which. If standard attributes have already
5725 been seen, it must start a statement, not a declaration,
5726 but standard attributes starting a declaration may appear
5727 after __extension__. */
5728 while (c_parser_peek_2nd_token (parser)->type == CPP_KEYWORD
5729 && (c_parser_peek_2nd_token (parser)->keyword
5730 == RID_EXTENSION))
5731 c_parser_consume_token (parser);
5732 if (!have_std_attrs
5733 && (c_token_starts_declaration (c_parser_peek_2nd_token (parser))
5734 || c_parser_nth_token_starts_std_attributes (parser, 2)))
5735 {
5736 int ext;
5737 ext = disable_extension_diagnostics ();
5738 c_parser_consume_token (parser);
5739 last_label = false;
5740 mark_valid_location_for_stdc_pragma (false);
5741 c_parser_declaration_or_fndef (parser, true, true, true, true,
5742 true, NULL, vNULL);
5743 /* Following the old parser, __extension__ does not
5744 disable this diagnostic. */
5745 restore_extension_diagnostics (ext);
5746 if (last_stmt)
5747 pedwarn_c90 (loc, OPT_Wdeclaration_after_statement,
5748 "ISO C90 forbids mixed declarations and code");
5749 last_stmt = false;
5750 }
5751 else
5752 goto statement;
5753 }
5754 else if (c_parser_next_token_is (parser, CPP_PRAGMA))
5755 {
5756 if (have_std_attrs)
5757 c_parser_error (parser, "expected declaration or statement");
5758 /* External pragmas, and some omp pragmas, are not associated
5759 with regular c code, and so are not to be considered statements
5760 syntactically. This ensures that the user doesn't put them
5761 places that would turn into syntax errors if the directive
5762 were ignored. */
5763 if (c_parser_pragma (parser,
5764 last_label ? pragma_stmt : pragma_compound,
5765 NULL))
5766 last_label = false, last_stmt = true;
5767 }
5768 else if (c_parser_next_token_is (parser, CPP_EOF))
5769 {
5770 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
5771 c_parser_error (parser, "expected declaration or statement");
5772 return;
5773 }
5774 else if (c_parser_next_token_is_keyword (parser, RID_ELSE))
5775 {
5776 if (parser->in_if_block)
5777 {
5778 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
5779 error_at (loc, "expected %<}%> before %<else%>");
5780 return;
5781 }
5782 else
5783 {
5784 error_at (loc, "%<else%> without a previous %<if%>");
5785 c_parser_consume_token (parser);
5786 continue;
5787 }
5788 }
5789 else
5790 {
5791 statement:
5792 c_warn_unused_attributes (std_attrs);
5793 last_label = false;
5794 last_stmt = true;
5795 mark_valid_location_for_stdc_pragma (false);
5796 c_parser_statement_after_labels (parser, NULL);
5797 }
5798
5799 parser->error = false;
5800 }
5801 if (last_label)
5802 error_at (label_loc, "label at end of compound statement");
5803 c_parser_consume_token (parser);
5804 /* Restore the value we started with. */
5805 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
5806 }
5807
5808 /* Parse all consecutive labels, possibly preceded by standard
5809 attributes. In this context, a statement is required, not a
5810 declaration, so attributes must be followed by a statement that is
5811 not just a semicolon. */
5812
5813 static void
5814 c_parser_all_labels (c_parser *parser)
5815 {
5816 if (c_parser_nth_token_starts_std_attributes (parser, 1))
5817 {
5818 tree std_attrs = c_parser_std_attribute_specifier_sequence (parser);
5819 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5820 c_parser_error (parser, "expected statement");
5821 else
5822 c_warn_unused_attributes (std_attrs);
5823 }
5824 while (c_parser_next_token_is_keyword (parser, RID_CASE)
5825 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
5826 || (c_parser_next_token_is (parser, CPP_NAME)
5827 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
5828 c_parser_label (parser);
5829 }
5830
5831 /* Parse a label (C90 6.6.1, C99 6.8.1, C11 6.8.1).
5832
5833 label:
5834 identifier : gnu-attributes[opt]
5835 case constant-expression :
5836 default :
5837
5838 GNU extensions:
5839
5840 label:
5841 case constant-expression ... constant-expression :
5842
5843 The use of gnu-attributes on labels is a GNU extension. The syntax in
5844 GNU C accepts any expressions without commas, non-constant
5845 expressions being rejected later. Any standard
5846 attribute-specifier-sequence before the first label has been parsed
5847 in the caller, to distinguish statements from declarations. Any
5848 attribute-specifier-sequence after the label is parsed in this
5849 function. */
5850
5851 static void
5852 c_parser_label (c_parser *parser)
5853 {
5854 location_t loc1 = c_parser_peek_token (parser)->location;
5855 tree label = NULL_TREE;
5856
5857 /* Remember whether this case or a user-defined label is allowed to fall
5858 through to. */
5859 bool fallthrough_p = c_parser_peek_token (parser)->flags & PREV_FALLTHROUGH;
5860
5861 if (c_parser_next_token_is_keyword (parser, RID_CASE))
5862 {
5863 tree exp1, exp2;
5864 c_parser_consume_token (parser);
5865 exp1 = c_parser_expr_no_commas (parser, NULL).value;
5866 if (c_parser_next_token_is (parser, CPP_COLON))
5867 {
5868 c_parser_consume_token (parser);
5869 label = do_case (loc1, exp1, NULL_TREE);
5870 }
5871 else if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
5872 {
5873 c_parser_consume_token (parser);
5874 exp2 = c_parser_expr_no_commas (parser, NULL).value;
5875 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
5876 label = do_case (loc1, exp1, exp2);
5877 }
5878 else
5879 c_parser_error (parser, "expected %<:%> or %<...%>");
5880 }
5881 else if (c_parser_next_token_is_keyword (parser, RID_DEFAULT))
5882 {
5883 c_parser_consume_token (parser);
5884 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
5885 label = do_case (loc1, NULL_TREE, NULL_TREE);
5886 }
5887 else
5888 {
5889 tree name = c_parser_peek_token (parser)->value;
5890 tree tlab;
5891 tree attrs;
5892 location_t loc2 = c_parser_peek_token (parser)->location;
5893 gcc_assert (c_parser_next_token_is (parser, CPP_NAME));
5894 c_parser_consume_token (parser);
5895 gcc_assert (c_parser_next_token_is (parser, CPP_COLON));
5896 c_parser_consume_token (parser);
5897 attrs = c_parser_gnu_attributes (parser);
5898 tlab = define_label (loc2, name);
5899 if (tlab)
5900 {
5901 decl_attributes (&tlab, attrs, 0);
5902 label = add_stmt (build_stmt (loc1, LABEL_EXPR, tlab));
5903 }
5904 }
5905 if (label)
5906 {
5907 if (TREE_CODE (label) == LABEL_EXPR)
5908 FALLTHROUGH_LABEL_P (LABEL_EXPR_LABEL (label)) = fallthrough_p;
5909 else
5910 FALLTHROUGH_LABEL_P (CASE_LABEL (label)) = fallthrough_p;
5911
5912 /* Standard attributes are only allowed here if they start a
5913 statement, not a declaration (including the case of an
5914 attribute-declaration with only attributes). */
5915 bool have_std_attrs
5916 = c_parser_nth_token_starts_std_attributes (parser, 1);
5917 tree std_attrs = NULL_TREE;
5918 if (have_std_attrs)
5919 std_attrs = c_parser_std_attribute_specifier_sequence (parser);
5920
5921 /* Allow '__attribute__((fallthrough));'. */
5922 if (!have_std_attrs
5923 && c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
5924 {
5925 location_t loc = c_parser_peek_token (parser)->location;
5926 tree attrs = c_parser_gnu_attributes (parser);
5927 if (attribute_fallthrough_p (attrs))
5928 {
5929 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5930 {
5931 tree fn = build_call_expr_internal_loc (loc,
5932 IFN_FALLTHROUGH,
5933 void_type_node, 0);
5934 add_stmt (fn);
5935 }
5936 else
5937 warning_at (loc, OPT_Wattributes, "%<fallthrough%> attribute "
5938 "not followed by %<;%>");
5939 }
5940 else if (attrs != NULL_TREE)
5941 warning_at (loc, OPT_Wattributes, "only attribute %<fallthrough%>"
5942 " can be applied to a null statement");
5943 }
5944 if (c_parser_next_tokens_start_declaration (parser)
5945 || (have_std_attrs
5946 && c_parser_next_token_is (parser, CPP_SEMICOLON)))
5947 {
5948 error_at (c_parser_peek_token (parser)->location,
5949 "a label can only be part of a statement and "
5950 "a declaration is not a statement");
5951 c_parser_declaration_or_fndef (parser, /*fndef_ok*/ false,
5952 /*static_assert_ok*/ true,
5953 /*empty_ok*/ true, /*nested*/ true,
5954 /*start_attr_ok*/ true, NULL,
5955 vNULL, have_std_attrs, std_attrs);
5956 }
5957 else if (std_attrs)
5958 /* Nonempty attributes on the following statement are ignored. */
5959 c_warn_unused_attributes (std_attrs);
5960 }
5961 }
5962
5963 /* Parse a statement (C90 6.6, C99 6.8, C11 6.8).
5964
5965 statement:
5966 labeled-statement
5967 attribute-specifier-sequence[opt] compound-statement
5968 expression-statement
5969 attribute-specifier-sequence[opt] selection-statement
5970 attribute-specifier-sequence[opt] iteration-statement
5971 attribute-specifier-sequence[opt] jump-statement
5972
5973 labeled-statement:
5974 attribute-specifier-sequence[opt] label statement
5975
5976 expression-statement:
5977 expression[opt] ;
5978 attribute-specifier-sequence expression ;
5979
5980 selection-statement:
5981 if-statement
5982 switch-statement
5983
5984 iteration-statement:
5985 while-statement
5986 do-statement
5987 for-statement
5988
5989 jump-statement:
5990 goto identifier ;
5991 continue ;
5992 break ;
5993 return expression[opt] ;
5994
5995 GNU extensions:
5996
5997 statement:
5998 attribute-specifier-sequence[opt] asm-statement
5999
6000 jump-statement:
6001 goto * expression ;
6002
6003 expression-statement:
6004 gnu-attributes ;
6005
6006 Objective-C:
6007
6008 statement:
6009 attribute-specifier-sequence[opt] objc-throw-statement
6010 attribute-specifier-sequence[opt] objc-try-catch-statement
6011 attribute-specifier-sequence[opt] objc-synchronized-statement
6012
6013 objc-throw-statement:
6014 @throw expression ;
6015 @throw ;
6016
6017 OpenACC:
6018
6019 statement:
6020 attribute-specifier-sequence[opt] openacc-construct
6021
6022 openacc-construct:
6023 parallel-construct
6024 kernels-construct
6025 data-construct
6026 loop-construct
6027
6028 parallel-construct:
6029 parallel-directive structured-block
6030
6031 kernels-construct:
6032 kernels-directive structured-block
6033
6034 data-construct:
6035 data-directive structured-block
6036
6037 loop-construct:
6038 loop-directive structured-block
6039
6040 OpenMP:
6041
6042 statement:
6043 attribute-specifier-sequence[opt] openmp-construct
6044
6045 openmp-construct:
6046 parallel-construct
6047 for-construct
6048 simd-construct
6049 for-simd-construct
6050 sections-construct
6051 single-construct
6052 parallel-for-construct
6053 parallel-for-simd-construct
6054 parallel-sections-construct
6055 master-construct
6056 critical-construct
6057 atomic-construct
6058 ordered-construct
6059
6060 parallel-construct:
6061 parallel-directive structured-block
6062
6063 for-construct:
6064 for-directive iteration-statement
6065
6066 simd-construct:
6067 simd-directive iteration-statements
6068
6069 for-simd-construct:
6070 for-simd-directive iteration-statements
6071
6072 sections-construct:
6073 sections-directive section-scope
6074
6075 single-construct:
6076 single-directive structured-block
6077
6078 parallel-for-construct:
6079 parallel-for-directive iteration-statement
6080
6081 parallel-for-simd-construct:
6082 parallel-for-simd-directive iteration-statement
6083
6084 parallel-sections-construct:
6085 parallel-sections-directive section-scope
6086
6087 master-construct:
6088 master-directive structured-block
6089
6090 critical-construct:
6091 critical-directive structured-block
6092
6093 atomic-construct:
6094 atomic-directive expression-statement
6095
6096 ordered-construct:
6097 ordered-directive structured-block
6098
6099 Transactional Memory:
6100
6101 statement:
6102 attribute-specifier-sequence[opt] transaction-statement
6103 attribute-specifier-sequence[opt] transaction-cancel-statement
6104
6105 IF_P is used to track whether there's a (possibly labeled) if statement
6106 which is not enclosed in braces and has an else clause. This is used to
6107 implement -Wparentheses. */
6108
6109 static void
6110 c_parser_statement (c_parser *parser, bool *if_p, location_t *loc_after_labels)
6111 {
6112 c_parser_all_labels (parser);
6113 if (loc_after_labels)
6114 *loc_after_labels = c_parser_peek_token (parser)->location;
6115 c_parser_statement_after_labels (parser, if_p, NULL);
6116 }
6117
6118 /* Parse a statement, other than a labeled statement. CHAIN is a vector
6119 of if-else-if conditions. All labels and standard attributes have
6120 been parsed in the caller.
6121
6122 IF_P is used to track whether there's a (possibly labeled) if statement
6123 which is not enclosed in braces and has an else clause. This is used to
6124 implement -Wparentheses. */
6125
6126 static void
6127 c_parser_statement_after_labels (c_parser *parser, bool *if_p,
6128 vec<tree> *chain)
6129 {
6130 location_t loc = c_parser_peek_token (parser)->location;
6131 tree stmt = NULL_TREE;
6132 bool in_if_block = parser->in_if_block;
6133 parser->in_if_block = false;
6134 if (if_p != NULL)
6135 *if_p = false;
6136
6137 if (c_parser_peek_token (parser)->type != CPP_OPEN_BRACE)
6138 add_debug_begin_stmt (loc);
6139
6140 switch (c_parser_peek_token (parser)->type)
6141 {
6142 case CPP_OPEN_BRACE:
6143 add_stmt (c_parser_compound_statement (parser));
6144 break;
6145 case CPP_KEYWORD:
6146 switch (c_parser_peek_token (parser)->keyword)
6147 {
6148 case RID_IF:
6149 c_parser_if_statement (parser, if_p, chain);
6150 break;
6151 case RID_SWITCH:
6152 c_parser_switch_statement (parser, if_p);
6153 break;
6154 case RID_WHILE:
6155 c_parser_while_statement (parser, false, 0, if_p);
6156 break;
6157 case RID_DO:
6158 c_parser_do_statement (parser, 0, false);
6159 break;
6160 case RID_FOR:
6161 c_parser_for_statement (parser, false, 0, if_p);
6162 break;
6163 case RID_GOTO:
6164 c_parser_consume_token (parser);
6165 if (c_parser_next_token_is (parser, CPP_NAME))
6166 {
6167 stmt = c_finish_goto_label (loc,
6168 c_parser_peek_token (parser)->value);
6169 c_parser_consume_token (parser);
6170 }
6171 else if (c_parser_next_token_is (parser, CPP_MULT))
6172 {
6173 struct c_expr val;
6174
6175 c_parser_consume_token (parser);
6176 val = c_parser_expression (parser);
6177 val = convert_lvalue_to_rvalue (loc, val, false, true);
6178 stmt = c_finish_goto_ptr (loc, val.value);
6179 }
6180 else
6181 c_parser_error (parser, "expected identifier or %<*%>");
6182 goto expect_semicolon;
6183 case RID_CONTINUE:
6184 c_parser_consume_token (parser);
6185 stmt = c_finish_bc_stmt (loc, &c_cont_label, false);
6186 goto expect_semicolon;
6187 case RID_BREAK:
6188 c_parser_consume_token (parser);
6189 stmt = c_finish_bc_stmt (loc, &c_break_label, true);
6190 goto expect_semicolon;
6191 case RID_RETURN:
6192 c_parser_consume_token (parser);
6193 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
6194 {
6195 stmt = c_finish_return (loc, NULL_TREE, NULL_TREE);
6196 c_parser_consume_token (parser);
6197 }
6198 else
6199 {
6200 location_t xloc = c_parser_peek_token (parser)->location;
6201 struct c_expr expr = c_parser_expression_conv (parser);
6202 mark_exp_read (expr.value);
6203 stmt = c_finish_return (EXPR_LOC_OR_LOC (expr.value, xloc),
6204 expr.value, expr.original_type);
6205 goto expect_semicolon;
6206 }
6207 break;
6208 case RID_ASM:
6209 stmt = c_parser_asm_statement (parser);
6210 break;
6211 case RID_TRANSACTION_ATOMIC:
6212 case RID_TRANSACTION_RELAXED:
6213 stmt = c_parser_transaction (parser,
6214 c_parser_peek_token (parser)->keyword);
6215 break;
6216 case RID_TRANSACTION_CANCEL:
6217 stmt = c_parser_transaction_cancel (parser);
6218 goto expect_semicolon;
6219 case RID_AT_THROW:
6220 gcc_assert (c_dialect_objc ());
6221 c_parser_consume_token (parser);
6222 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
6223 {
6224 stmt = objc_build_throw_stmt (loc, NULL_TREE);
6225 c_parser_consume_token (parser);
6226 }
6227 else
6228 {
6229 struct c_expr expr = c_parser_expression (parser);
6230 expr = convert_lvalue_to_rvalue (loc, expr, false, false);
6231 expr.value = c_fully_fold (expr.value, false, NULL);
6232 stmt = objc_build_throw_stmt (loc, expr.value);
6233 goto expect_semicolon;
6234 }
6235 break;
6236 case RID_AT_TRY:
6237 gcc_assert (c_dialect_objc ());
6238 c_parser_objc_try_catch_finally_statement (parser);
6239 break;
6240 case RID_AT_SYNCHRONIZED:
6241 gcc_assert (c_dialect_objc ());
6242 c_parser_objc_synchronized_statement (parser);
6243 break;
6244 case RID_ATTRIBUTE:
6245 {
6246 /* Allow '__attribute__((fallthrough));'. */
6247 tree attrs = c_parser_gnu_attributes (parser);
6248 if (attribute_fallthrough_p (attrs))
6249 {
6250 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
6251 {
6252 tree fn = build_call_expr_internal_loc (loc,
6253 IFN_FALLTHROUGH,
6254 void_type_node, 0);
6255 add_stmt (fn);
6256 /* Eat the ';'. */
6257 c_parser_consume_token (parser);
6258 }
6259 else
6260 warning_at (loc, OPT_Wattributes,
6261 "%<fallthrough%> attribute not followed "
6262 "by %<;%>");
6263 }
6264 else if (attrs != NULL_TREE)
6265 warning_at (loc, OPT_Wattributes, "only attribute %<fallthrough%>"
6266 " can be applied to a null statement");
6267 break;
6268 }
6269 default:
6270 goto expr_stmt;
6271 }
6272 break;
6273 case CPP_SEMICOLON:
6274 c_parser_consume_token (parser);
6275 break;
6276 case CPP_CLOSE_PAREN:
6277 case CPP_CLOSE_SQUARE:
6278 /* Avoid infinite loop in error recovery:
6279 c_parser_skip_until_found stops at a closing nesting
6280 delimiter without consuming it, but here we need to consume
6281 it to proceed further. */
6282 c_parser_error (parser, "expected statement");
6283 c_parser_consume_token (parser);
6284 break;
6285 case CPP_PRAGMA:
6286 c_parser_pragma (parser, pragma_stmt, if_p);
6287 break;
6288 default:
6289 expr_stmt:
6290 stmt = c_finish_expr_stmt (loc, c_parser_expression_conv (parser).value);
6291 expect_semicolon:
6292 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
6293 break;
6294 }
6295 /* Two cases cannot and do not have line numbers associated: If stmt
6296 is degenerate, such as "2;", then stmt is an INTEGER_CST, which
6297 cannot hold line numbers. But that's OK because the statement
6298 will either be changed to a MODIFY_EXPR during gimplification of
6299 the statement expr, or discarded. If stmt was compound, but
6300 without new variables, we will have skipped the creation of a
6301 BIND and will have a bare STATEMENT_LIST. But that's OK because
6302 (recursively) all of the component statements should already have
6303 line numbers assigned. ??? Can we discard no-op statements
6304 earlier? */
6305 if (EXPR_LOCATION (stmt) == UNKNOWN_LOCATION)
6306 protected_set_expr_location (stmt, loc);
6307
6308 parser->in_if_block = in_if_block;
6309 }
6310
6311 /* Parse the condition from an if, do, while or for statements. */
6312
6313 static tree
6314 c_parser_condition (c_parser *parser)
6315 {
6316 location_t loc = c_parser_peek_token (parser)->location;
6317 tree cond;
6318 cond = c_parser_expression_conv (parser).value;
6319 cond = c_objc_common_truthvalue_conversion (loc, cond);
6320 cond = c_fully_fold (cond, false, NULL);
6321 if (warn_sequence_point)
6322 verify_sequence_points (cond);
6323 return cond;
6324 }
6325
6326 /* Parse a parenthesized condition from an if, do or while statement.
6327
6328 condition:
6329 ( expression )
6330 */
6331 static tree
6332 c_parser_paren_condition (c_parser *parser)
6333 {
6334 tree cond;
6335 matching_parens parens;
6336 if (!parens.require_open (parser))
6337 return error_mark_node;
6338 cond = c_parser_condition (parser);
6339 parens.skip_until_found_close (parser);
6340 return cond;
6341 }
6342
6343 /* Parse a statement which is a block in C99.
6344
6345 IF_P is used to track whether there's a (possibly labeled) if statement
6346 which is not enclosed in braces and has an else clause. This is used to
6347 implement -Wparentheses. */
6348
6349 static tree
6350 c_parser_c99_block_statement (c_parser *parser, bool *if_p,
6351 location_t *loc_after_labels)
6352 {
6353 tree block = c_begin_compound_stmt (flag_isoc99);
6354 location_t loc = c_parser_peek_token (parser)->location;
6355 c_parser_statement (parser, if_p, loc_after_labels);
6356 return c_end_compound_stmt (loc, block, flag_isoc99);
6357 }
6358
6359 /* Parse the body of an if statement. This is just parsing a
6360 statement but (a) it is a block in C99, (b) we track whether the
6361 body is an if statement for the sake of -Wparentheses warnings, (c)
6362 we handle an empty body specially for the sake of -Wempty-body
6363 warnings, and (d) we call parser_compound_statement directly
6364 because c_parser_statement_after_labels resets
6365 parser->in_if_block.
6366
6367 IF_P is used to track whether there's a (possibly labeled) if statement
6368 which is not enclosed in braces and has an else clause. This is used to
6369 implement -Wparentheses. */
6370
6371 static tree
6372 c_parser_if_body (c_parser *parser, bool *if_p,
6373 const token_indent_info &if_tinfo)
6374 {
6375 tree block = c_begin_compound_stmt (flag_isoc99);
6376 location_t body_loc = c_parser_peek_token (parser)->location;
6377 location_t body_loc_after_labels = UNKNOWN_LOCATION;
6378 token_indent_info body_tinfo
6379 = get_token_indent_info (c_parser_peek_token (parser));
6380
6381 c_parser_all_labels (parser);
6382 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
6383 {
6384 location_t loc = c_parser_peek_token (parser)->location;
6385 add_stmt (build_empty_stmt (loc));
6386 c_parser_consume_token (parser);
6387 if (!c_parser_next_token_is_keyword (parser, RID_ELSE))
6388 warning_at (loc, OPT_Wempty_body,
6389 "suggest braces around empty body in an %<if%> statement");
6390 }
6391 else if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
6392 add_stmt (c_parser_compound_statement (parser));
6393 else
6394 {
6395 body_loc_after_labels = c_parser_peek_token (parser)->location;
6396 c_parser_statement_after_labels (parser, if_p);
6397 }
6398
6399 token_indent_info next_tinfo
6400 = get_token_indent_info (c_parser_peek_token (parser));
6401 warn_for_misleading_indentation (if_tinfo, body_tinfo, next_tinfo);
6402 if (body_loc_after_labels != UNKNOWN_LOCATION
6403 && next_tinfo.type != CPP_SEMICOLON)
6404 warn_for_multistatement_macros (body_loc_after_labels, next_tinfo.location,
6405 if_tinfo.location, RID_IF);
6406
6407 return c_end_compound_stmt (body_loc, block, flag_isoc99);
6408 }
6409
6410 /* Parse the else body of an if statement. This is just parsing a
6411 statement but (a) it is a block in C99, (b) we handle an empty body
6412 specially for the sake of -Wempty-body warnings. CHAIN is a vector
6413 of if-else-if conditions. */
6414
6415 static tree
6416 c_parser_else_body (c_parser *parser, const token_indent_info &else_tinfo,
6417 vec<tree> *chain)
6418 {
6419 location_t body_loc = c_parser_peek_token (parser)->location;
6420 tree block = c_begin_compound_stmt (flag_isoc99);
6421 token_indent_info body_tinfo
6422 = get_token_indent_info (c_parser_peek_token (parser));
6423 location_t body_loc_after_labels = UNKNOWN_LOCATION;
6424
6425 c_parser_all_labels (parser);
6426 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
6427 {
6428 location_t loc = c_parser_peek_token (parser)->location;
6429 warning_at (loc,
6430 OPT_Wempty_body,
6431 "suggest braces around empty body in an %<else%> statement");
6432 add_stmt (build_empty_stmt (loc));
6433 c_parser_consume_token (parser);
6434 }
6435 else
6436 {
6437 if (!c_parser_next_token_is (parser, CPP_OPEN_BRACE))
6438 body_loc_after_labels = c_parser_peek_token (parser)->location;
6439 c_parser_statement_after_labels (parser, NULL, chain);
6440 }
6441
6442 token_indent_info next_tinfo
6443 = get_token_indent_info (c_parser_peek_token (parser));
6444 warn_for_misleading_indentation (else_tinfo, body_tinfo, next_tinfo);
6445 if (body_loc_after_labels != UNKNOWN_LOCATION
6446 && next_tinfo.type != CPP_SEMICOLON)
6447 warn_for_multistatement_macros (body_loc_after_labels, next_tinfo.location,
6448 else_tinfo.location, RID_ELSE);
6449
6450 return c_end_compound_stmt (body_loc, block, flag_isoc99);
6451 }
6452
6453 /* We might need to reclassify any previously-lexed identifier, e.g.
6454 when we've left a for loop with an if-statement without else in the
6455 body - we might have used a wrong scope for the token. See PR67784. */
6456
6457 static void
6458 c_parser_maybe_reclassify_token (c_parser *parser)
6459 {
6460 if (c_parser_next_token_is (parser, CPP_NAME))
6461 {
6462 c_token *token = c_parser_peek_token (parser);
6463
6464 if (token->id_kind != C_ID_CLASSNAME)
6465 {
6466 tree decl = lookup_name (token->value);
6467
6468 token->id_kind = C_ID_ID;
6469 if (decl)
6470 {
6471 if (TREE_CODE (decl) == TYPE_DECL)
6472 token->id_kind = C_ID_TYPENAME;
6473 }
6474 else if (c_dialect_objc ())
6475 {
6476 tree objc_interface_decl = objc_is_class_name (token->value);
6477 /* Objective-C class names are in the same namespace as
6478 variables and typedefs, and hence are shadowed by local
6479 declarations. */
6480 if (objc_interface_decl)
6481 {
6482 token->value = objc_interface_decl;
6483 token->id_kind = C_ID_CLASSNAME;
6484 }
6485 }
6486 }
6487 }
6488 }
6489
6490 /* Parse an if statement (C90 6.6.4, C99 6.8.4, C11 6.8.4).
6491
6492 if-statement:
6493 if ( expression ) statement
6494 if ( expression ) statement else statement
6495
6496 CHAIN is a vector of if-else-if conditions.
6497 IF_P is used to track whether there's a (possibly labeled) if statement
6498 which is not enclosed in braces and has an else clause. This is used to
6499 implement -Wparentheses. */
6500
6501 static void
6502 c_parser_if_statement (c_parser *parser, bool *if_p, vec<tree> *chain)
6503 {
6504 tree block;
6505 location_t loc;
6506 tree cond;
6507 bool nested_if = false;
6508 tree first_body, second_body;
6509 bool in_if_block;
6510
6511 gcc_assert (c_parser_next_token_is_keyword (parser, RID_IF));
6512 token_indent_info if_tinfo
6513 = get_token_indent_info (c_parser_peek_token (parser));
6514 c_parser_consume_token (parser);
6515 block = c_begin_compound_stmt (flag_isoc99);
6516 loc = c_parser_peek_token (parser)->location;
6517 cond = c_parser_paren_condition (parser);
6518 in_if_block = parser->in_if_block;
6519 parser->in_if_block = true;
6520 first_body = c_parser_if_body (parser, &nested_if, if_tinfo);
6521 parser->in_if_block = in_if_block;
6522
6523 if (warn_duplicated_cond)
6524 warn_duplicated_cond_add_or_warn (EXPR_LOCATION (cond), cond, &chain);
6525
6526 if (c_parser_next_token_is_keyword (parser, RID_ELSE))
6527 {
6528 token_indent_info else_tinfo
6529 = get_token_indent_info (c_parser_peek_token (parser));
6530 c_parser_consume_token (parser);
6531 if (warn_duplicated_cond)
6532 {
6533 if (c_parser_next_token_is_keyword (parser, RID_IF)
6534 && chain == NULL)
6535 {
6536 /* We've got "if (COND) else if (COND2)". Start the
6537 condition chain and add COND as the first element. */
6538 chain = new vec<tree> ();
6539 if (!CONSTANT_CLASS_P (cond) && !TREE_SIDE_EFFECTS (cond))
6540 chain->safe_push (cond);
6541 }
6542 else if (!c_parser_next_token_is_keyword (parser, RID_IF))
6543 {
6544 /* This is if-else without subsequent if. Zap the condition
6545 chain; we would have already warned at this point. */
6546 delete chain;
6547 chain = NULL;
6548 }
6549 }
6550 second_body = c_parser_else_body (parser, else_tinfo, chain);
6551 /* Set IF_P to true to indicate that this if statement has an
6552 else clause. This may trigger the Wparentheses warning
6553 below when we get back up to the parent if statement. */
6554 if (if_p != NULL)
6555 *if_p = true;
6556 }
6557 else
6558 {
6559 second_body = NULL_TREE;
6560
6561 /* Diagnose an ambiguous else if if-then-else is nested inside
6562 if-then. */
6563 if (nested_if)
6564 warning_at (loc, OPT_Wdangling_else,
6565 "suggest explicit braces to avoid ambiguous %<else%>");
6566
6567 if (warn_duplicated_cond)
6568 {
6569 /* This if statement does not have an else clause. We don't
6570 need the condition chain anymore. */
6571 delete chain;
6572 chain = NULL;
6573 }
6574 }
6575 c_finish_if_stmt (loc, cond, first_body, second_body);
6576 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99));
6577
6578 c_parser_maybe_reclassify_token (parser);
6579 }
6580
6581 /* Parse a switch statement (C90 6.6.4, C99 6.8.4, C11 6.8.4).
6582
6583 switch-statement:
6584 switch (expression) statement
6585 */
6586
6587 static void
6588 c_parser_switch_statement (c_parser *parser, bool *if_p)
6589 {
6590 struct c_expr ce;
6591 tree block, expr, body, save_break;
6592 location_t switch_loc = c_parser_peek_token (parser)->location;
6593 location_t switch_cond_loc;
6594 gcc_assert (c_parser_next_token_is_keyword (parser, RID_SWITCH));
6595 c_parser_consume_token (parser);
6596 block = c_begin_compound_stmt (flag_isoc99);
6597 bool explicit_cast_p = false;
6598 matching_parens parens;
6599 if (parens.require_open (parser))
6600 {
6601 switch_cond_loc = c_parser_peek_token (parser)->location;
6602 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
6603 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
6604 explicit_cast_p = true;
6605 ce = c_parser_expression (parser);
6606 ce = convert_lvalue_to_rvalue (switch_cond_loc, ce, true, false);
6607 expr = ce.value;
6608 /* ??? expr has no valid location? */
6609 parens.skip_until_found_close (parser);
6610 }
6611 else
6612 {
6613 switch_cond_loc = UNKNOWN_LOCATION;
6614 expr = error_mark_node;
6615 ce.original_type = error_mark_node;
6616 }
6617 c_start_case (switch_loc, switch_cond_loc, expr, explicit_cast_p);
6618 save_break = c_break_label;
6619 c_break_label = NULL_TREE;
6620 location_t loc_after_labels;
6621 bool open_brace_p = c_parser_peek_token (parser)->type == CPP_OPEN_BRACE;
6622 body = c_parser_c99_block_statement (parser, if_p, &loc_after_labels);
6623 location_t next_loc = c_parser_peek_token (parser)->location;
6624 if (!open_brace_p && c_parser_peek_token (parser)->type != CPP_SEMICOLON)
6625 warn_for_multistatement_macros (loc_after_labels, next_loc, switch_loc,
6626 RID_SWITCH);
6627 if (c_break_label)
6628 {
6629 location_t here = c_parser_peek_token (parser)->location;
6630 tree t = build1 (LABEL_EXPR, void_type_node, c_break_label);
6631 SET_EXPR_LOCATION (t, here);
6632 SWITCH_BREAK_LABEL_P (c_break_label) = 1;
6633 append_to_statement_list_force (t, &body);
6634 }
6635 c_finish_case (body, ce.original_type);
6636 c_break_label = save_break;
6637 add_stmt (c_end_compound_stmt (switch_loc, block, flag_isoc99));
6638 c_parser_maybe_reclassify_token (parser);
6639 }
6640
6641 /* Parse a while statement (C90 6.6.5, C99 6.8.5, C11 6.8.5).
6642
6643 while-statement:
6644 while (expression) statement
6645
6646 IF_P is used to track whether there's a (possibly labeled) if statement
6647 which is not enclosed in braces and has an else clause. This is used to
6648 implement -Wparentheses. */
6649
6650 static void
6651 c_parser_while_statement (c_parser *parser, bool ivdep, unsigned short unroll,
6652 bool *if_p)
6653 {
6654 tree block, cond, body, save_break, save_cont;
6655 location_t loc;
6656 gcc_assert (c_parser_next_token_is_keyword (parser, RID_WHILE));
6657 token_indent_info while_tinfo
6658 = get_token_indent_info (c_parser_peek_token (parser));
6659 c_parser_consume_token (parser);
6660 block = c_begin_compound_stmt (flag_isoc99);
6661 loc = c_parser_peek_token (parser)->location;
6662 cond = c_parser_paren_condition (parser);
6663 if (ivdep && cond != error_mark_node)
6664 cond = build3 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
6665 build_int_cst (integer_type_node,
6666 annot_expr_ivdep_kind),
6667 integer_zero_node);
6668 if (unroll && cond != error_mark_node)
6669 cond = build3 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
6670 build_int_cst (integer_type_node,
6671 annot_expr_unroll_kind),
6672 build_int_cst (integer_type_node, unroll));
6673 save_break = c_break_label;
6674 c_break_label = NULL_TREE;
6675 save_cont = c_cont_label;
6676 c_cont_label = NULL_TREE;
6677
6678 token_indent_info body_tinfo
6679 = get_token_indent_info (c_parser_peek_token (parser));
6680
6681 location_t loc_after_labels;
6682 bool open_brace = c_parser_next_token_is (parser, CPP_OPEN_BRACE);
6683 body = c_parser_c99_block_statement (parser, if_p, &loc_after_labels);
6684 c_finish_loop (loc, loc, cond, UNKNOWN_LOCATION, NULL, body,
6685 c_break_label, c_cont_label, true);
6686 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99));
6687 c_parser_maybe_reclassify_token (parser);
6688
6689 token_indent_info next_tinfo
6690 = get_token_indent_info (c_parser_peek_token (parser));
6691 warn_for_misleading_indentation (while_tinfo, body_tinfo, next_tinfo);
6692
6693 if (next_tinfo.type != CPP_SEMICOLON && !open_brace)
6694 warn_for_multistatement_macros (loc_after_labels, next_tinfo.location,
6695 while_tinfo.location, RID_WHILE);
6696
6697 c_break_label = save_break;
6698 c_cont_label = save_cont;
6699 }
6700
6701 /* Parse a do statement (C90 6.6.5, C99 6.8.5, C11 6.8.5).
6702
6703 do-statement:
6704 do statement while ( expression ) ;
6705 */
6706
6707 static void
6708 c_parser_do_statement (c_parser *parser, bool ivdep, unsigned short unroll)
6709 {
6710 tree block, cond, body, save_break, save_cont, new_break, new_cont;
6711 location_t loc;
6712 gcc_assert (c_parser_next_token_is_keyword (parser, RID_DO));
6713 c_parser_consume_token (parser);
6714 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
6715 warning_at (c_parser_peek_token (parser)->location,
6716 OPT_Wempty_body,
6717 "suggest braces around empty body in %<do%> statement");
6718 block = c_begin_compound_stmt (flag_isoc99);
6719 loc = c_parser_peek_token (parser)->location;
6720 save_break = c_break_label;
6721 c_break_label = NULL_TREE;
6722 save_cont = c_cont_label;
6723 c_cont_label = NULL_TREE;
6724 body = c_parser_c99_block_statement (parser, NULL);
6725 c_parser_require_keyword (parser, RID_WHILE, "expected %<while%>");
6726 new_break = c_break_label;
6727 c_break_label = save_break;
6728 new_cont = c_cont_label;
6729 c_cont_label = save_cont;
6730 location_t cond_loc = c_parser_peek_token (parser)->location;
6731 cond = c_parser_paren_condition (parser);
6732 if (ivdep && cond != error_mark_node)
6733 cond = build3 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
6734 build_int_cst (integer_type_node,
6735 annot_expr_ivdep_kind),
6736 integer_zero_node);
6737 if (unroll && cond != error_mark_node)
6738 cond = build3 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
6739 build_int_cst (integer_type_node,
6740 annot_expr_unroll_kind),
6741 build_int_cst (integer_type_node, unroll));
6742 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
6743 c_parser_skip_to_end_of_block_or_statement (parser);
6744 c_finish_loop (loc, cond_loc, cond, UNKNOWN_LOCATION, NULL, body,
6745 new_break, new_cont, false);
6746 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99));
6747 }
6748
6749 /* Parse a for statement (C90 6.6.5, C99 6.8.5, C11 6.8.5).
6750
6751 for-statement:
6752 for ( expression[opt] ; expression[opt] ; expression[opt] ) statement
6753 for ( nested-declaration expression[opt] ; expression[opt] ) statement
6754
6755 The form with a declaration is new in C99.
6756
6757 ??? In accordance with the old parser, the declaration may be a
6758 nested function, which is then rejected in check_for_loop_decls,
6759 but does it make any sense for this to be included in the grammar?
6760 Note in particular that the nested function does not include a
6761 trailing ';', whereas the "declaration" production includes one.
6762 Also, can we reject bad declarations earlier and cheaper than
6763 check_for_loop_decls?
6764
6765 In Objective-C, there are two additional variants:
6766
6767 foreach-statement:
6768 for ( expression in expresssion ) statement
6769 for ( declaration in expression ) statement
6770
6771 This is inconsistent with C, because the second variant is allowed
6772 even if c99 is not enabled.
6773
6774 The rest of the comment documents these Objective-C foreach-statement.
6775
6776 Here is the canonical example of the first variant:
6777 for (object in array) { do something with object }
6778 we call the first expression ("object") the "object_expression" and
6779 the second expression ("array") the "collection_expression".
6780 object_expression must be an lvalue of type "id" (a generic Objective-C
6781 object) because the loop works by assigning to object_expression the
6782 various objects from the collection_expression. collection_expression
6783 must evaluate to something of type "id" which responds to the method
6784 countByEnumeratingWithState:objects:count:.
6785
6786 The canonical example of the second variant is:
6787 for (id object in array) { do something with object }
6788 which is completely equivalent to
6789 {
6790 id object;
6791 for (object in array) { do something with object }
6792 }
6793 Note that initizializing 'object' in some way (eg, "for ((object =
6794 xxx) in array) { do something with object }") is possibly
6795 technically valid, but completely pointless as 'object' will be
6796 assigned to something else as soon as the loop starts. We should
6797 most likely reject it (TODO).
6798
6799 The beginning of the Objective-C foreach-statement looks exactly
6800 like the beginning of the for-statement, and we can tell it is a
6801 foreach-statement only because the initial declaration or
6802 expression is terminated by 'in' instead of ';'.
6803
6804 IF_P is used to track whether there's a (possibly labeled) if statement
6805 which is not enclosed in braces and has an else clause. This is used to
6806 implement -Wparentheses. */
6807
6808 static void
6809 c_parser_for_statement (c_parser *parser, bool ivdep, unsigned short unroll,
6810 bool *if_p)
6811 {
6812 tree block, cond, incr, save_break, save_cont, body;
6813 /* The following are only used when parsing an ObjC foreach statement. */
6814 tree object_expression;
6815 /* Silence the bogus uninitialized warning. */
6816 tree collection_expression = NULL;
6817 location_t loc = c_parser_peek_token (parser)->location;
6818 location_t for_loc = loc;
6819 location_t cond_loc = UNKNOWN_LOCATION;
6820 location_t incr_loc = UNKNOWN_LOCATION;
6821 bool is_foreach_statement = false;
6822 gcc_assert (c_parser_next_token_is_keyword (parser, RID_FOR));
6823 token_indent_info for_tinfo
6824 = get_token_indent_info (c_parser_peek_token (parser));
6825 c_parser_consume_token (parser);
6826 /* Open a compound statement in Objective-C as well, just in case this is
6827 as foreach expression. */
6828 block = c_begin_compound_stmt (flag_isoc99 || c_dialect_objc ());
6829 cond = error_mark_node;
6830 incr = error_mark_node;
6831 matching_parens parens;
6832 if (parens.require_open (parser))
6833 {
6834 /* Parse the initialization declaration or expression. */
6835 object_expression = error_mark_node;
6836 parser->objc_could_be_foreach_context = c_dialect_objc ();
6837 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
6838 {
6839 parser->objc_could_be_foreach_context = false;
6840 c_parser_consume_token (parser);
6841 c_finish_expr_stmt (loc, NULL_TREE);
6842 }
6843 else if (c_parser_next_tokens_start_declaration (parser)
6844 || c_parser_nth_token_starts_std_attributes (parser, 1))
6845 {
6846 c_parser_declaration_or_fndef (parser, true, true, true, true, true,
6847 &object_expression, vNULL);
6848 parser->objc_could_be_foreach_context = false;
6849
6850 if (c_parser_next_token_is_keyword (parser, RID_IN))
6851 {
6852 c_parser_consume_token (parser);
6853 is_foreach_statement = true;
6854 if (check_for_loop_decls (for_loc, true) == NULL_TREE)
6855 c_parser_error (parser, "multiple iterating variables in "
6856 "fast enumeration");
6857 }
6858 else
6859 check_for_loop_decls (for_loc, flag_isoc99);
6860 }
6861 else if (c_parser_next_token_is_keyword (parser, RID_EXTENSION))
6862 {
6863 /* __extension__ can start a declaration, but is also an
6864 unary operator that can start an expression. Consume all
6865 but the last of a possible series of __extension__ to
6866 determine which. */
6867 while (c_parser_peek_2nd_token (parser)->type == CPP_KEYWORD
6868 && (c_parser_peek_2nd_token (parser)->keyword
6869 == RID_EXTENSION))
6870 c_parser_consume_token (parser);
6871 if (c_token_starts_declaration (c_parser_peek_2nd_token (parser))
6872 || c_parser_nth_token_starts_std_attributes (parser, 2))
6873 {
6874 int ext;
6875 ext = disable_extension_diagnostics ();
6876 c_parser_consume_token (parser);
6877 c_parser_declaration_or_fndef (parser, true, true, true, true,
6878 true, &object_expression, vNULL);
6879 parser->objc_could_be_foreach_context = false;
6880
6881 restore_extension_diagnostics (ext);
6882 if (c_parser_next_token_is_keyword (parser, RID_IN))
6883 {
6884 c_parser_consume_token (parser);
6885 is_foreach_statement = true;
6886 if (check_for_loop_decls (for_loc, true) == NULL_TREE)
6887 c_parser_error (parser, "multiple iterating variables in "
6888 "fast enumeration");
6889 }
6890 else
6891 check_for_loop_decls (for_loc, flag_isoc99);
6892 }
6893 else
6894 goto init_expr;
6895 }
6896 else
6897 {
6898 init_expr:
6899 {
6900 struct c_expr ce;
6901 tree init_expression;
6902 ce = c_parser_expression (parser);
6903 init_expression = ce.value;
6904 parser->objc_could_be_foreach_context = false;
6905 if (c_parser_next_token_is_keyword (parser, RID_IN))
6906 {
6907 c_parser_consume_token (parser);
6908 is_foreach_statement = true;
6909 if (! lvalue_p (init_expression))
6910 c_parser_error (parser, "invalid iterating variable in "
6911 "fast enumeration");
6912 object_expression
6913 = c_fully_fold (init_expression, false, NULL);
6914 }
6915 else
6916 {
6917 ce = convert_lvalue_to_rvalue (loc, ce, true, false);
6918 init_expression = ce.value;
6919 c_finish_expr_stmt (loc, init_expression);
6920 c_parser_skip_until_found (parser, CPP_SEMICOLON,
6921 "expected %<;%>");
6922 }
6923 }
6924 }
6925 /* Parse the loop condition. In the case of a foreach
6926 statement, there is no loop condition. */
6927 gcc_assert (!parser->objc_could_be_foreach_context);
6928 if (!is_foreach_statement)
6929 {
6930 cond_loc = c_parser_peek_token (parser)->location;
6931 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
6932 {
6933 if (ivdep)
6934 {
6935 c_parser_error (parser, "missing loop condition in loop "
6936 "with %<GCC ivdep%> pragma");
6937 cond = error_mark_node;
6938 }
6939 else if (unroll)
6940 {
6941 c_parser_error (parser, "missing loop condition in loop "
6942 "with %<GCC unroll%> pragma");
6943 cond = error_mark_node;
6944 }
6945 else
6946 {
6947 c_parser_consume_token (parser);
6948 cond = NULL_TREE;
6949 }
6950 }
6951 else
6952 {
6953 cond = c_parser_condition (parser);
6954 c_parser_skip_until_found (parser, CPP_SEMICOLON,
6955 "expected %<;%>");
6956 }
6957 if (ivdep && cond != error_mark_node)
6958 cond = build3 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
6959 build_int_cst (integer_type_node,
6960 annot_expr_ivdep_kind),
6961 integer_zero_node);
6962 if (unroll && cond != error_mark_node)
6963 cond = build3 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
6964 build_int_cst (integer_type_node,
6965 annot_expr_unroll_kind),
6966 build_int_cst (integer_type_node, unroll));
6967 }
6968 /* Parse the increment expression (the third expression in a
6969 for-statement). In the case of a foreach-statement, this is
6970 the expression that follows the 'in'. */
6971 loc = incr_loc = c_parser_peek_token (parser)->location;
6972 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
6973 {
6974 if (is_foreach_statement)
6975 {
6976 c_parser_error (parser,
6977 "missing collection in fast enumeration");
6978 collection_expression = error_mark_node;
6979 }
6980 else
6981 incr = c_process_expr_stmt (loc, NULL_TREE);
6982 }
6983 else
6984 {
6985 if (is_foreach_statement)
6986 collection_expression
6987 = c_fully_fold (c_parser_expression (parser).value, false, NULL);
6988 else
6989 {
6990 struct c_expr ce = c_parser_expression (parser);
6991 ce = convert_lvalue_to_rvalue (loc, ce, true, false);
6992 incr = c_process_expr_stmt (loc, ce.value);
6993 }
6994 }
6995 parens.skip_until_found_close (parser);
6996 }
6997 save_break = c_break_label;
6998 c_break_label = NULL_TREE;
6999 save_cont = c_cont_label;
7000 c_cont_label = NULL_TREE;
7001
7002 token_indent_info body_tinfo
7003 = get_token_indent_info (c_parser_peek_token (parser));
7004
7005 location_t loc_after_labels;
7006 bool open_brace = c_parser_next_token_is (parser, CPP_OPEN_BRACE);
7007 body = c_parser_c99_block_statement (parser, if_p, &loc_after_labels);
7008
7009 if (is_foreach_statement)
7010 objc_finish_foreach_loop (for_loc, object_expression,
7011 collection_expression, body, c_break_label,
7012 c_cont_label);
7013 else
7014 c_finish_loop (for_loc, cond_loc, cond, incr_loc, incr, body,
7015 c_break_label, c_cont_label, true);
7016 add_stmt (c_end_compound_stmt (for_loc, block,
7017 flag_isoc99 || c_dialect_objc ()));
7018 c_parser_maybe_reclassify_token (parser);
7019
7020 token_indent_info next_tinfo
7021 = get_token_indent_info (c_parser_peek_token (parser));
7022 warn_for_misleading_indentation (for_tinfo, body_tinfo, next_tinfo);
7023
7024 if (next_tinfo.type != CPP_SEMICOLON && !open_brace)
7025 warn_for_multistatement_macros (loc_after_labels, next_tinfo.location,
7026 for_tinfo.location, RID_FOR);
7027
7028 c_break_label = save_break;
7029 c_cont_label = save_cont;
7030 }
7031
7032 /* Parse an asm statement, a GNU extension. This is a full-blown asm
7033 statement with inputs, outputs, clobbers, and volatile, inline, and goto
7034 tags allowed.
7035
7036 asm-qualifier:
7037 volatile
7038 inline
7039 goto
7040
7041 asm-qualifier-list:
7042 asm-qualifier-list asm-qualifier
7043 asm-qualifier
7044
7045 asm-statement:
7046 asm asm-qualifier-list[opt] ( asm-argument ) ;
7047
7048 asm-argument:
7049 asm-string-literal
7050 asm-string-literal : asm-operands[opt]
7051 asm-string-literal : asm-operands[opt] : asm-operands[opt]
7052 asm-string-literal : asm-operands[opt] : asm-operands[opt] \
7053 : asm-clobbers[opt]
7054 asm-string-literal : : asm-operands[opt] : asm-clobbers[opt] \
7055 : asm-goto-operands
7056
7057 The form with asm-goto-operands is valid if and only if the
7058 asm-qualifier-list contains goto, and is the only allowed form in that case.
7059 Duplicate asm-qualifiers are not allowed.
7060
7061 The :: token is considered equivalent to two consecutive : tokens. */
7062
7063 static tree
7064 c_parser_asm_statement (c_parser *parser)
7065 {
7066 tree str, outputs, inputs, clobbers, labels, ret;
7067 bool simple;
7068 location_t asm_loc = c_parser_peek_token (parser)->location;
7069 int section, nsections;
7070
7071 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ASM));
7072 c_parser_consume_token (parser);
7073
7074 /* Handle the asm-qualifier-list. */
7075 location_t volatile_loc = UNKNOWN_LOCATION;
7076 location_t inline_loc = UNKNOWN_LOCATION;
7077 location_t goto_loc = UNKNOWN_LOCATION;
7078 for (;;)
7079 {
7080 c_token *token = c_parser_peek_token (parser);
7081 location_t loc = token->location;
7082 switch (token->keyword)
7083 {
7084 case RID_VOLATILE:
7085 if (volatile_loc)
7086 {
7087 error_at (loc, "duplicate %<asm%> qualifier %qE", token->value);
7088 inform (volatile_loc, "first seen here");
7089 }
7090 else
7091 volatile_loc = loc;
7092 c_parser_consume_token (parser);
7093 continue;
7094
7095 case RID_INLINE:
7096 if (inline_loc)
7097 {
7098 error_at (loc, "duplicate %<asm%> qualifier %qE", token->value);
7099 inform (inline_loc, "first seen here");
7100 }
7101 else
7102 inline_loc = loc;
7103 c_parser_consume_token (parser);
7104 continue;
7105
7106 case RID_GOTO:
7107 if (goto_loc)
7108 {
7109 error_at (loc, "duplicate %<asm%> qualifier %qE", token->value);
7110 inform (goto_loc, "first seen here");
7111 }
7112 else
7113 goto_loc = loc;
7114 c_parser_consume_token (parser);
7115 continue;
7116
7117 case RID_CONST:
7118 case RID_RESTRICT:
7119 error_at (loc, "%qE is not a valid %<asm%> qualifier", token->value);
7120 c_parser_consume_token (parser);
7121 continue;
7122
7123 default:
7124 break;
7125 }
7126 break;
7127 }
7128
7129 bool is_volatile = (volatile_loc != UNKNOWN_LOCATION);
7130 bool is_inline = (inline_loc != UNKNOWN_LOCATION);
7131 bool is_goto = (goto_loc != UNKNOWN_LOCATION);
7132
7133 ret = NULL;
7134
7135 matching_parens parens;
7136 if (!parens.require_open (parser))
7137 goto error;
7138
7139 str = c_parser_asm_string_literal (parser);
7140 if (str == NULL_TREE)
7141 goto error_close_paren;
7142
7143 simple = true;
7144 outputs = NULL_TREE;
7145 inputs = NULL_TREE;
7146 clobbers = NULL_TREE;
7147 labels = NULL_TREE;
7148
7149 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN) && !is_goto)
7150 goto done_asm;
7151
7152 /* Parse each colon-delimited section of operands. */
7153 nsections = 3 + is_goto;
7154 for (section = 0; section < nsections; ++section)
7155 {
7156 if (c_parser_next_token_is (parser, CPP_SCOPE))
7157 {
7158 ++section;
7159 if (section == nsections)
7160 {
7161 c_parser_error (parser, "expected %<)%>");
7162 goto error_close_paren;
7163 }
7164 c_parser_consume_token (parser);
7165 }
7166 else if (!c_parser_require (parser, CPP_COLON,
7167 is_goto
7168 ? G_("expected %<:%>")
7169 : G_("expected %<:%> or %<)%>"),
7170 UNKNOWN_LOCATION, is_goto))
7171 goto error_close_paren;
7172
7173 /* Once past any colon, we're no longer a simple asm. */
7174 simple = false;
7175
7176 if ((!c_parser_next_token_is (parser, CPP_COLON)
7177 && !c_parser_next_token_is (parser, CPP_SCOPE)
7178 && !c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
7179 || section == 3)
7180 switch (section)
7181 {
7182 case 0:
7183 /* For asm goto, we don't allow output operands, but reserve
7184 the slot for a future extension that does allow them. */
7185 if (!is_goto)
7186 outputs = c_parser_asm_operands (parser);
7187 break;
7188 case 1:
7189 inputs = c_parser_asm_operands (parser);
7190 break;
7191 case 2:
7192 clobbers = c_parser_asm_clobbers (parser);
7193 break;
7194 case 3:
7195 labels = c_parser_asm_goto_operands (parser);
7196 break;
7197 default:
7198 gcc_unreachable ();
7199 }
7200
7201 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN) && !is_goto)
7202 goto done_asm;
7203 }
7204
7205 done_asm:
7206 if (!parens.require_close (parser))
7207 {
7208 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7209 goto error;
7210 }
7211
7212 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
7213 c_parser_skip_to_end_of_block_or_statement (parser);
7214
7215 ret = build_asm_stmt (is_volatile,
7216 build_asm_expr (asm_loc, str, outputs, inputs,
7217 clobbers, labels, simple, is_inline));
7218
7219 error:
7220 return ret;
7221
7222 error_close_paren:
7223 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7224 goto error;
7225 }
7226
7227 /* Parse asm operands, a GNU extension.
7228
7229 asm-operands:
7230 asm-operand
7231 asm-operands , asm-operand
7232
7233 asm-operand:
7234 asm-string-literal ( expression )
7235 [ identifier ] asm-string-literal ( expression )
7236 */
7237
7238 static tree
7239 c_parser_asm_operands (c_parser *parser)
7240 {
7241 tree list = NULL_TREE;
7242 while (true)
7243 {
7244 tree name, str;
7245 struct c_expr expr;
7246 if (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
7247 {
7248 c_parser_consume_token (parser);
7249 if (c_parser_next_token_is (parser, CPP_NAME))
7250 {
7251 tree id = c_parser_peek_token (parser)->value;
7252 c_parser_consume_token (parser);
7253 name = build_string (IDENTIFIER_LENGTH (id),
7254 IDENTIFIER_POINTER (id));
7255 }
7256 else
7257 {
7258 c_parser_error (parser, "expected identifier");
7259 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
7260 return NULL_TREE;
7261 }
7262 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
7263 "expected %<]%>");
7264 }
7265 else
7266 name = NULL_TREE;
7267 str = c_parser_asm_string_literal (parser);
7268 if (str == NULL_TREE)
7269 return NULL_TREE;
7270 matching_parens parens;
7271 if (!parens.require_open (parser))
7272 return NULL_TREE;
7273 expr = c_parser_expression (parser);
7274 mark_exp_read (expr.value);
7275 if (!parens.require_close (parser))
7276 {
7277 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7278 return NULL_TREE;
7279 }
7280 list = chainon (list, build_tree_list (build_tree_list (name, str),
7281 expr.value));
7282 if (c_parser_next_token_is (parser, CPP_COMMA))
7283 c_parser_consume_token (parser);
7284 else
7285 break;
7286 }
7287 return list;
7288 }
7289
7290 /* Parse asm clobbers, a GNU extension.
7291
7292 asm-clobbers:
7293 asm-string-literal
7294 asm-clobbers , asm-string-literal
7295 */
7296
7297 static tree
7298 c_parser_asm_clobbers (c_parser *parser)
7299 {
7300 tree list = NULL_TREE;
7301 while (true)
7302 {
7303 tree str = c_parser_asm_string_literal (parser);
7304 if (str)
7305 list = tree_cons (NULL_TREE, str, list);
7306 else
7307 return NULL_TREE;
7308 if (c_parser_next_token_is (parser, CPP_COMMA))
7309 c_parser_consume_token (parser);
7310 else
7311 break;
7312 }
7313 return list;
7314 }
7315
7316 /* Parse asm goto labels, a GNU extension.
7317
7318 asm-goto-operands:
7319 identifier
7320 asm-goto-operands , identifier
7321 */
7322
7323 static tree
7324 c_parser_asm_goto_operands (c_parser *parser)
7325 {
7326 tree list = NULL_TREE;
7327 while (true)
7328 {
7329 tree name, label;
7330
7331 if (c_parser_next_token_is (parser, CPP_NAME))
7332 {
7333 c_token *tok = c_parser_peek_token (parser);
7334 name = tok->value;
7335 label = lookup_label_for_goto (tok->location, name);
7336 c_parser_consume_token (parser);
7337 TREE_USED (label) = 1;
7338 }
7339 else
7340 {
7341 c_parser_error (parser, "expected identifier");
7342 return NULL_TREE;
7343 }
7344
7345 name = build_string (IDENTIFIER_LENGTH (name),
7346 IDENTIFIER_POINTER (name));
7347 list = tree_cons (name, label, list);
7348 if (c_parser_next_token_is (parser, CPP_COMMA))
7349 c_parser_consume_token (parser);
7350 else
7351 return nreverse (list);
7352 }
7353 }
7354
7355 /* Parse a possibly concatenated sequence of string literals.
7356 TRANSLATE says whether to translate them to the execution character
7357 set; WIDE_OK says whether any kind of prefixed string literal is
7358 permitted in this context. This code is based on that in
7359 lex_string. */
7360
7361 struct c_expr
7362 c_parser_string_literal (c_parser *parser, bool translate, bool wide_ok)
7363 {
7364 struct c_expr ret;
7365 size_t count;
7366 struct obstack str_ob;
7367 struct obstack loc_ob;
7368 cpp_string str, istr, *strs;
7369 c_token *tok;
7370 location_t loc, last_tok_loc;
7371 enum cpp_ttype type;
7372 tree value, string_tree;
7373
7374 tok = c_parser_peek_token (parser);
7375 loc = tok->location;
7376 last_tok_loc = linemap_resolve_location (line_table, loc,
7377 LRK_MACRO_DEFINITION_LOCATION,
7378 NULL);
7379 type = tok->type;
7380 switch (type)
7381 {
7382 case CPP_STRING:
7383 case CPP_WSTRING:
7384 case CPP_STRING16:
7385 case CPP_STRING32:
7386 case CPP_UTF8STRING:
7387 string_tree = tok->value;
7388 break;
7389
7390 default:
7391 c_parser_error (parser, "expected string literal");
7392 ret.set_error ();
7393 ret.value = NULL_TREE;
7394 ret.original_code = ERROR_MARK;
7395 ret.original_type = NULL_TREE;
7396 return ret;
7397 }
7398
7399 /* Try to avoid the overhead of creating and destroying an obstack
7400 for the common case of just one string. */
7401 switch (c_parser_peek_2nd_token (parser)->type)
7402 {
7403 default:
7404 c_parser_consume_token (parser);
7405 str.text = (const unsigned char *) TREE_STRING_POINTER (string_tree);
7406 str.len = TREE_STRING_LENGTH (string_tree);
7407 count = 1;
7408 strs = &str;
7409 break;
7410
7411 case CPP_STRING:
7412 case CPP_WSTRING:
7413 case CPP_STRING16:
7414 case CPP_STRING32:
7415 case CPP_UTF8STRING:
7416 gcc_obstack_init (&str_ob);
7417 gcc_obstack_init (&loc_ob);
7418 count = 0;
7419 do
7420 {
7421 c_parser_consume_token (parser);
7422 count++;
7423 str.text = (const unsigned char *) TREE_STRING_POINTER (string_tree);
7424 str.len = TREE_STRING_LENGTH (string_tree);
7425 if (type != tok->type)
7426 {
7427 if (type == CPP_STRING)
7428 type = tok->type;
7429 else if (tok->type != CPP_STRING)
7430 error ("unsupported non-standard concatenation "
7431 "of string literals");
7432 }
7433 obstack_grow (&str_ob, &str, sizeof (cpp_string));
7434 obstack_grow (&loc_ob, &last_tok_loc, sizeof (location_t));
7435 tok = c_parser_peek_token (parser);
7436 string_tree = tok->value;
7437 last_tok_loc
7438 = linemap_resolve_location (line_table, tok->location,
7439 LRK_MACRO_DEFINITION_LOCATION, NULL);
7440 }
7441 while (tok->type == CPP_STRING
7442 || tok->type == CPP_WSTRING
7443 || tok->type == CPP_STRING16
7444 || tok->type == CPP_STRING32
7445 || tok->type == CPP_UTF8STRING);
7446 strs = (cpp_string *) obstack_finish (&str_ob);
7447 }
7448
7449 if (count > 1 && !in_system_header_at (input_location))
7450 warning (OPT_Wtraditional,
7451 "traditional C rejects string constant concatenation");
7452
7453 if ((type == CPP_STRING || wide_ok)
7454 && ((translate
7455 ? cpp_interpret_string : cpp_interpret_string_notranslate)
7456 (parse_in, strs, count, &istr, type)))
7457 {
7458 value = build_string (istr.len, (const char *) istr.text);
7459 free (CONST_CAST (unsigned char *, istr.text));
7460 if (count > 1)
7461 {
7462 location_t *locs = (location_t *) obstack_finish (&loc_ob);
7463 gcc_assert (g_string_concat_db);
7464 g_string_concat_db->record_string_concatenation (count, locs);
7465 }
7466 }
7467 else
7468 {
7469 if (type != CPP_STRING && !wide_ok)
7470 {
7471 error_at (loc, "a wide string is invalid in this context");
7472 type = CPP_STRING;
7473 }
7474 /* Callers cannot generally handle error_mark_node in this
7475 context, so return the empty string instead. An error has
7476 been issued, either above or from cpp_interpret_string. */
7477 switch (type)
7478 {
7479 default:
7480 case CPP_STRING:
7481 case CPP_UTF8STRING:
7482 value = build_string (1, "");
7483 break;
7484 case CPP_STRING16:
7485 value = build_string (TYPE_PRECISION (char16_type_node)
7486 / TYPE_PRECISION (char_type_node),
7487 "\0"); /* char16_t is 16 bits */
7488 break;
7489 case CPP_STRING32:
7490 value = build_string (TYPE_PRECISION (char32_type_node)
7491 / TYPE_PRECISION (char_type_node),
7492 "\0\0\0"); /* char32_t is 32 bits */
7493 break;
7494 case CPP_WSTRING:
7495 value = build_string (TYPE_PRECISION (wchar_type_node)
7496 / TYPE_PRECISION (char_type_node),
7497 "\0\0\0"); /* widest supported wchar_t
7498 is 32 bits */
7499 break;
7500 }
7501 }
7502
7503 switch (type)
7504 {
7505 default:
7506 case CPP_STRING:
7507 case CPP_UTF8STRING:
7508 TREE_TYPE (value) = char_array_type_node;
7509 break;
7510 case CPP_STRING16:
7511 TREE_TYPE (value) = char16_array_type_node;
7512 break;
7513 case CPP_STRING32:
7514 TREE_TYPE (value) = char32_array_type_node;
7515 break;
7516 case CPP_WSTRING:
7517 TREE_TYPE (value) = wchar_array_type_node;
7518 }
7519 value = fix_string_type (value);
7520
7521 if (count > 1)
7522 {
7523 obstack_free (&str_ob, 0);
7524 obstack_free (&loc_ob, 0);
7525 }
7526
7527 ret.value = value;
7528 ret.original_code = STRING_CST;
7529 ret.original_type = NULL_TREE;
7530 set_c_expr_source_range (&ret, get_range_from_loc (line_table, loc));
7531 return ret;
7532 }
7533
7534 /* Parse an expression other than a compound expression; that is, an
7535 assignment expression (C90 6.3.16, C99 6.5.16, C11 6.5.16). If
7536 AFTER is not NULL then it is an Objective-C message expression which
7537 is the primary-expression starting the expression as an initializer.
7538
7539 assignment-expression:
7540 conditional-expression
7541 unary-expression assignment-operator assignment-expression
7542
7543 assignment-operator: one of
7544 = *= /= %= += -= <<= >>= &= ^= |=
7545
7546 In GNU C we accept any conditional expression on the LHS and
7547 diagnose the invalid lvalue rather than producing a syntax
7548 error. */
7549
7550 static struct c_expr
7551 c_parser_expr_no_commas (c_parser *parser, struct c_expr *after,
7552 tree omp_atomic_lhs)
7553 {
7554 struct c_expr lhs, rhs, ret;
7555 enum tree_code code;
7556 location_t op_location, exp_location;
7557 gcc_assert (!after || c_dialect_objc ());
7558 lhs = c_parser_conditional_expression (parser, after, omp_atomic_lhs);
7559 op_location = c_parser_peek_token (parser)->location;
7560 switch (c_parser_peek_token (parser)->type)
7561 {
7562 case CPP_EQ:
7563 code = NOP_EXPR;
7564 break;
7565 case CPP_MULT_EQ:
7566 code = MULT_EXPR;
7567 break;
7568 case CPP_DIV_EQ:
7569 code = TRUNC_DIV_EXPR;
7570 break;
7571 case CPP_MOD_EQ:
7572 code = TRUNC_MOD_EXPR;
7573 break;
7574 case CPP_PLUS_EQ:
7575 code = PLUS_EXPR;
7576 break;
7577 case CPP_MINUS_EQ:
7578 code = MINUS_EXPR;
7579 break;
7580 case CPP_LSHIFT_EQ:
7581 code = LSHIFT_EXPR;
7582 break;
7583 case CPP_RSHIFT_EQ:
7584 code = RSHIFT_EXPR;
7585 break;
7586 case CPP_AND_EQ:
7587 code = BIT_AND_EXPR;
7588 break;
7589 case CPP_XOR_EQ:
7590 code = BIT_XOR_EXPR;
7591 break;
7592 case CPP_OR_EQ:
7593 code = BIT_IOR_EXPR;
7594 break;
7595 default:
7596 return lhs;
7597 }
7598 c_parser_consume_token (parser);
7599 exp_location = c_parser_peek_token (parser)->location;
7600 rhs = c_parser_expr_no_commas (parser, NULL);
7601 rhs = convert_lvalue_to_rvalue (exp_location, rhs, true, true);
7602
7603 ret.value = build_modify_expr (op_location, lhs.value, lhs.original_type,
7604 code, exp_location, rhs.value,
7605 rhs.original_type);
7606 set_c_expr_source_range (&ret, lhs.get_start (), rhs.get_finish ());
7607 if (code == NOP_EXPR)
7608 ret.original_code = MODIFY_EXPR;
7609 else
7610 {
7611 TREE_NO_WARNING (ret.value) = 1;
7612 ret.original_code = ERROR_MARK;
7613 }
7614 ret.original_type = NULL;
7615 return ret;
7616 }
7617
7618 /* Parse a conditional expression (C90 6.3.15, C99 6.5.15, C11 6.5.15). If
7619 AFTER is not NULL then it is an Objective-C message expression which is
7620 the primary-expression starting the expression as an initializer.
7621
7622 conditional-expression:
7623 logical-OR-expression
7624 logical-OR-expression ? expression : conditional-expression
7625
7626 GNU extensions:
7627
7628 conditional-expression:
7629 logical-OR-expression ? : conditional-expression
7630 */
7631
7632 static struct c_expr
7633 c_parser_conditional_expression (c_parser *parser, struct c_expr *after,
7634 tree omp_atomic_lhs)
7635 {
7636 struct c_expr cond, exp1, exp2, ret;
7637 location_t start, cond_loc, colon_loc;
7638
7639 gcc_assert (!after || c_dialect_objc ());
7640
7641 cond = c_parser_binary_expression (parser, after, omp_atomic_lhs);
7642
7643 if (c_parser_next_token_is_not (parser, CPP_QUERY))
7644 return cond;
7645 if (cond.value != error_mark_node)
7646 start = cond.get_start ();
7647 else
7648 start = UNKNOWN_LOCATION;
7649 cond_loc = c_parser_peek_token (parser)->location;
7650 cond = convert_lvalue_to_rvalue (cond_loc, cond, true, true);
7651 c_parser_consume_token (parser);
7652 if (c_parser_next_token_is (parser, CPP_COLON))
7653 {
7654 tree eptype = NULL_TREE;
7655
7656 location_t middle_loc = c_parser_peek_token (parser)->location;
7657 pedwarn (middle_loc, OPT_Wpedantic,
7658 "ISO C forbids omitting the middle term of a %<?:%> expression");
7659 if (TREE_CODE (cond.value) == EXCESS_PRECISION_EXPR)
7660 {
7661 eptype = TREE_TYPE (cond.value);
7662 cond.value = TREE_OPERAND (cond.value, 0);
7663 }
7664 tree e = cond.value;
7665 while (TREE_CODE (e) == COMPOUND_EXPR)
7666 e = TREE_OPERAND (e, 1);
7667 warn_for_omitted_condop (middle_loc, e);
7668 /* Make sure first operand is calculated only once. */
7669 exp1.value = save_expr (default_conversion (cond.value));
7670 if (eptype)
7671 exp1.value = build1 (EXCESS_PRECISION_EXPR, eptype, exp1.value);
7672 exp1.original_type = NULL;
7673 exp1.src_range = cond.src_range;
7674 cond.value = c_objc_common_truthvalue_conversion (cond_loc, exp1.value);
7675 c_inhibit_evaluation_warnings += cond.value == truthvalue_true_node;
7676 }
7677 else
7678 {
7679 cond.value
7680 = c_objc_common_truthvalue_conversion
7681 (cond_loc, default_conversion (cond.value));
7682 c_inhibit_evaluation_warnings += cond.value == truthvalue_false_node;
7683 exp1 = c_parser_expression_conv (parser);
7684 mark_exp_read (exp1.value);
7685 c_inhibit_evaluation_warnings +=
7686 ((cond.value == truthvalue_true_node)
7687 - (cond.value == truthvalue_false_node));
7688 }
7689
7690 colon_loc = c_parser_peek_token (parser)->location;
7691 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
7692 {
7693 c_inhibit_evaluation_warnings -= cond.value == truthvalue_true_node;
7694 ret.set_error ();
7695 ret.original_code = ERROR_MARK;
7696 ret.original_type = NULL;
7697 return ret;
7698 }
7699 {
7700 location_t exp2_loc = c_parser_peek_token (parser)->location;
7701 exp2 = c_parser_conditional_expression (parser, NULL, NULL_TREE);
7702 exp2 = convert_lvalue_to_rvalue (exp2_loc, exp2, true, true);
7703 }
7704 c_inhibit_evaluation_warnings -= cond.value == truthvalue_true_node;
7705 location_t loc1 = make_location (exp1.get_start (), exp1.src_range);
7706 location_t loc2 = make_location (exp2.get_start (), exp2.src_range);
7707 ret.value = build_conditional_expr (colon_loc, cond.value,
7708 cond.original_code == C_MAYBE_CONST_EXPR,
7709 exp1.value, exp1.original_type, loc1,
7710 exp2.value, exp2.original_type, loc2);
7711 ret.original_code = ERROR_MARK;
7712 if (exp1.value == error_mark_node || exp2.value == error_mark_node)
7713 ret.original_type = NULL;
7714 else
7715 {
7716 tree t1, t2;
7717
7718 /* If both sides are enum type, the default conversion will have
7719 made the type of the result be an integer type. We want to
7720 remember the enum types we started with. */
7721 t1 = exp1.original_type ? exp1.original_type : TREE_TYPE (exp1.value);
7722 t2 = exp2.original_type ? exp2.original_type : TREE_TYPE (exp2.value);
7723 ret.original_type = ((t1 != error_mark_node
7724 && t2 != error_mark_node
7725 && (TYPE_MAIN_VARIANT (t1)
7726 == TYPE_MAIN_VARIANT (t2)))
7727 ? t1
7728 : NULL);
7729 }
7730 set_c_expr_source_range (&ret, start, exp2.get_finish ());
7731 return ret;
7732 }
7733
7734 /* Parse a binary expression; that is, a logical-OR-expression (C90
7735 6.3.5-6.3.14, C99 6.5.5-6.5.14, C11 6.5.5-6.5.14). If AFTER is not
7736 NULL then it is an Objective-C message expression which is the
7737 primary-expression starting the expression as an initializer.
7738
7739 OMP_ATOMIC_LHS is NULL, unless parsing OpenMP #pragma omp atomic,
7740 when it should be the unfolded lhs. In a valid OpenMP source,
7741 one of the operands of the toplevel binary expression must be equal
7742 to it. In that case, just return a build2 created binary operation
7743 rather than result of parser_build_binary_op.
7744
7745 multiplicative-expression:
7746 cast-expression
7747 multiplicative-expression * cast-expression
7748 multiplicative-expression / cast-expression
7749 multiplicative-expression % cast-expression
7750
7751 additive-expression:
7752 multiplicative-expression
7753 additive-expression + multiplicative-expression
7754 additive-expression - multiplicative-expression
7755
7756 shift-expression:
7757 additive-expression
7758 shift-expression << additive-expression
7759 shift-expression >> additive-expression
7760
7761 relational-expression:
7762 shift-expression
7763 relational-expression < shift-expression
7764 relational-expression > shift-expression
7765 relational-expression <= shift-expression
7766 relational-expression >= shift-expression
7767
7768 equality-expression:
7769 relational-expression
7770 equality-expression == relational-expression
7771 equality-expression != relational-expression
7772
7773 AND-expression:
7774 equality-expression
7775 AND-expression & equality-expression
7776
7777 exclusive-OR-expression:
7778 AND-expression
7779 exclusive-OR-expression ^ AND-expression
7780
7781 inclusive-OR-expression:
7782 exclusive-OR-expression
7783 inclusive-OR-expression | exclusive-OR-expression
7784
7785 logical-AND-expression:
7786 inclusive-OR-expression
7787 logical-AND-expression && inclusive-OR-expression
7788
7789 logical-OR-expression:
7790 logical-AND-expression
7791 logical-OR-expression || logical-AND-expression
7792 */
7793
7794 static struct c_expr
7795 c_parser_binary_expression (c_parser *parser, struct c_expr *after,
7796 tree omp_atomic_lhs)
7797 {
7798 /* A binary expression is parsed using operator-precedence parsing,
7799 with the operands being cast expressions. All the binary
7800 operators are left-associative. Thus a binary expression is of
7801 form:
7802
7803 E0 op1 E1 op2 E2 ...
7804
7805 which we represent on a stack. On the stack, the precedence
7806 levels are strictly increasing. When a new operator is
7807 encountered of higher precedence than that at the top of the
7808 stack, it is pushed; its LHS is the top expression, and its RHS
7809 is everything parsed until it is popped. When a new operator is
7810 encountered with precedence less than or equal to that at the top
7811 of the stack, triples E[i-1] op[i] E[i] are popped and replaced
7812 by the result of the operation until the operator at the top of
7813 the stack has lower precedence than the new operator or there is
7814 only one element on the stack; then the top expression is the LHS
7815 of the new operator. In the case of logical AND and OR
7816 expressions, we also need to adjust c_inhibit_evaluation_warnings
7817 as appropriate when the operators are pushed and popped. */
7818
7819 struct {
7820 /* The expression at this stack level. */
7821 struct c_expr expr;
7822 /* The precedence of the operator on its left, PREC_NONE at the
7823 bottom of the stack. */
7824 enum c_parser_prec prec;
7825 /* The operation on its left. */
7826 enum tree_code op;
7827 /* The source location of this operation. */
7828 location_t loc;
7829 /* The sizeof argument if expr.original_code == SIZEOF_EXPR. */
7830 tree sizeof_arg;
7831 } stack[NUM_PRECS];
7832 int sp;
7833 /* Location of the binary operator. */
7834 location_t binary_loc = UNKNOWN_LOCATION; /* Quiet warning. */
7835 #define POP \
7836 do { \
7837 switch (stack[sp].op) \
7838 { \
7839 case TRUTH_ANDIF_EXPR: \
7840 c_inhibit_evaluation_warnings -= (stack[sp - 1].expr.value \
7841 == truthvalue_false_node); \
7842 break; \
7843 case TRUTH_ORIF_EXPR: \
7844 c_inhibit_evaluation_warnings -= (stack[sp - 1].expr.value \
7845 == truthvalue_true_node); \
7846 break; \
7847 case TRUNC_DIV_EXPR: \
7848 if (stack[sp - 1].expr.original_code == SIZEOF_EXPR \
7849 && stack[sp].expr.original_code == SIZEOF_EXPR) \
7850 { \
7851 tree type0 = stack[sp - 1].sizeof_arg; \
7852 tree type1 = stack[sp].sizeof_arg; \
7853 tree first_arg = type0; \
7854 if (!TYPE_P (type0)) \
7855 type0 = TREE_TYPE (type0); \
7856 if (!TYPE_P (type1)) \
7857 type1 = TREE_TYPE (type1); \
7858 if (POINTER_TYPE_P (type0) \
7859 && comptypes (TREE_TYPE (type0), type1) \
7860 && !(TREE_CODE (first_arg) == PARM_DECL \
7861 && C_ARRAY_PARAMETER (first_arg) \
7862 && warn_sizeof_array_argument)) \
7863 { \
7864 auto_diagnostic_group d; \
7865 if (warning_at (stack[sp].loc, OPT_Wsizeof_pointer_div, \
7866 "division %<sizeof (%T) / sizeof (%T)%> " \
7867 "does not compute the number of array " \
7868 "elements", \
7869 type0, type1)) \
7870 if (DECL_P (first_arg)) \
7871 inform (DECL_SOURCE_LOCATION (first_arg), \
7872 "first %<sizeof%> operand was declared here"); \
7873 } \
7874 } \
7875 break; \
7876 default: \
7877 break; \
7878 } \
7879 stack[sp - 1].expr \
7880 = convert_lvalue_to_rvalue (stack[sp - 1].loc, \
7881 stack[sp - 1].expr, true, true); \
7882 stack[sp].expr \
7883 = convert_lvalue_to_rvalue (stack[sp].loc, \
7884 stack[sp].expr, true, true); \
7885 if (__builtin_expect (omp_atomic_lhs != NULL_TREE, 0) && sp == 1 \
7886 && c_parser_peek_token (parser)->type == CPP_SEMICOLON \
7887 && ((1 << stack[sp].prec) \
7888 & ((1 << PREC_BITOR) | (1 << PREC_BITXOR) | (1 << PREC_BITAND) \
7889 | (1 << PREC_SHIFT) | (1 << PREC_ADD) | (1 << PREC_MULT))) \
7890 && stack[sp].op != TRUNC_MOD_EXPR \
7891 && stack[0].expr.value != error_mark_node \
7892 && stack[1].expr.value != error_mark_node \
7893 && (c_tree_equal (stack[0].expr.value, omp_atomic_lhs) \
7894 || c_tree_equal (stack[1].expr.value, omp_atomic_lhs))) \
7895 stack[0].expr.value \
7896 = build2 (stack[1].op, TREE_TYPE (stack[0].expr.value), \
7897 stack[0].expr.value, stack[1].expr.value); \
7898 else \
7899 stack[sp - 1].expr = parser_build_binary_op (stack[sp].loc, \
7900 stack[sp].op, \
7901 stack[sp - 1].expr, \
7902 stack[sp].expr); \
7903 sp--; \
7904 } while (0)
7905 gcc_assert (!after || c_dialect_objc ());
7906 stack[0].loc = c_parser_peek_token (parser)->location;
7907 stack[0].expr = c_parser_cast_expression (parser, after);
7908 stack[0].prec = PREC_NONE;
7909 stack[0].sizeof_arg = c_last_sizeof_arg;
7910 sp = 0;
7911 while (true)
7912 {
7913 enum c_parser_prec oprec;
7914 enum tree_code ocode;
7915 source_range src_range;
7916 if (parser->error)
7917 goto out;
7918 switch (c_parser_peek_token (parser)->type)
7919 {
7920 case CPP_MULT:
7921 oprec = PREC_MULT;
7922 ocode = MULT_EXPR;
7923 break;
7924 case CPP_DIV:
7925 oprec = PREC_MULT;
7926 ocode = TRUNC_DIV_EXPR;
7927 break;
7928 case CPP_MOD:
7929 oprec = PREC_MULT;
7930 ocode = TRUNC_MOD_EXPR;
7931 break;
7932 case CPP_PLUS:
7933 oprec = PREC_ADD;
7934 ocode = PLUS_EXPR;
7935 break;
7936 case CPP_MINUS:
7937 oprec = PREC_ADD;
7938 ocode = MINUS_EXPR;
7939 break;
7940 case CPP_LSHIFT:
7941 oprec = PREC_SHIFT;
7942 ocode = LSHIFT_EXPR;
7943 break;
7944 case CPP_RSHIFT:
7945 oprec = PREC_SHIFT;
7946 ocode = RSHIFT_EXPR;
7947 break;
7948 case CPP_LESS:
7949 oprec = PREC_REL;
7950 ocode = LT_EXPR;
7951 break;
7952 case CPP_GREATER:
7953 oprec = PREC_REL;
7954 ocode = GT_EXPR;
7955 break;
7956 case CPP_LESS_EQ:
7957 oprec = PREC_REL;
7958 ocode = LE_EXPR;
7959 break;
7960 case CPP_GREATER_EQ:
7961 oprec = PREC_REL;
7962 ocode = GE_EXPR;
7963 break;
7964 case CPP_EQ_EQ:
7965 oprec = PREC_EQ;
7966 ocode = EQ_EXPR;
7967 break;
7968 case CPP_NOT_EQ:
7969 oprec = PREC_EQ;
7970 ocode = NE_EXPR;
7971 break;
7972 case CPP_AND:
7973 oprec = PREC_BITAND;
7974 ocode = BIT_AND_EXPR;
7975 break;
7976 case CPP_XOR:
7977 oprec = PREC_BITXOR;
7978 ocode = BIT_XOR_EXPR;
7979 break;
7980 case CPP_OR:
7981 oprec = PREC_BITOR;
7982 ocode = BIT_IOR_EXPR;
7983 break;
7984 case CPP_AND_AND:
7985 oprec = PREC_LOGAND;
7986 ocode = TRUTH_ANDIF_EXPR;
7987 break;
7988 case CPP_OR_OR:
7989 oprec = PREC_LOGOR;
7990 ocode = TRUTH_ORIF_EXPR;
7991 break;
7992 default:
7993 /* Not a binary operator, so end of the binary
7994 expression. */
7995 goto out;
7996 }
7997 binary_loc = c_parser_peek_token (parser)->location;
7998 while (oprec <= stack[sp].prec)
7999 POP;
8000 c_parser_consume_token (parser);
8001 switch (ocode)
8002 {
8003 case TRUTH_ANDIF_EXPR:
8004 src_range = stack[sp].expr.src_range;
8005 stack[sp].expr
8006 = convert_lvalue_to_rvalue (stack[sp].loc,
8007 stack[sp].expr, true, true);
8008 stack[sp].expr.value = c_objc_common_truthvalue_conversion
8009 (stack[sp].loc, default_conversion (stack[sp].expr.value));
8010 c_inhibit_evaluation_warnings += (stack[sp].expr.value
8011 == truthvalue_false_node);
8012 set_c_expr_source_range (&stack[sp].expr, src_range);
8013 break;
8014 case TRUTH_ORIF_EXPR:
8015 src_range = stack[sp].expr.src_range;
8016 stack[sp].expr
8017 = convert_lvalue_to_rvalue (stack[sp].loc,
8018 stack[sp].expr, true, true);
8019 stack[sp].expr.value = c_objc_common_truthvalue_conversion
8020 (stack[sp].loc, default_conversion (stack[sp].expr.value));
8021 c_inhibit_evaluation_warnings += (stack[sp].expr.value
8022 == truthvalue_true_node);
8023 set_c_expr_source_range (&stack[sp].expr, src_range);
8024 break;
8025 default:
8026 break;
8027 }
8028 sp++;
8029 stack[sp].loc = binary_loc;
8030 stack[sp].expr = c_parser_cast_expression (parser, NULL);
8031 stack[sp].prec = oprec;
8032 stack[sp].op = ocode;
8033 stack[sp].sizeof_arg = c_last_sizeof_arg;
8034 }
8035 out:
8036 while (sp > 0)
8037 POP;
8038 return stack[0].expr;
8039 #undef POP
8040 }
8041
8042 /* Parse a cast expression (C90 6.3.4, C99 6.5.4, C11 6.5.4). If AFTER
8043 is not NULL then it is an Objective-C message expression which is the
8044 primary-expression starting the expression as an initializer.
8045
8046 cast-expression:
8047 unary-expression
8048 ( type-name ) unary-expression
8049 */
8050
8051 static struct c_expr
8052 c_parser_cast_expression (c_parser *parser, struct c_expr *after)
8053 {
8054 location_t cast_loc = c_parser_peek_token (parser)->location;
8055 gcc_assert (!after || c_dialect_objc ());
8056 if (after)
8057 return c_parser_postfix_expression_after_primary (parser,
8058 cast_loc, *after);
8059 /* If the expression begins with a parenthesized type name, it may
8060 be either a cast or a compound literal; we need to see whether
8061 the next character is '{' to tell the difference. If not, it is
8062 an unary expression. Full detection of unknown typenames here
8063 would require a 3-token lookahead. */
8064 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
8065 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
8066 {
8067 struct c_type_name *type_name;
8068 struct c_expr ret;
8069 struct c_expr expr;
8070 matching_parens parens;
8071 parens.consume_open (parser);
8072 type_name = c_parser_type_name (parser, true);
8073 parens.skip_until_found_close (parser);
8074 if (type_name == NULL)
8075 {
8076 ret.set_error ();
8077 ret.original_code = ERROR_MARK;
8078 ret.original_type = NULL;
8079 return ret;
8080 }
8081
8082 /* Save casted types in the function's used types hash table. */
8083 used_types_insert (type_name->specs->type);
8084
8085 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
8086 return c_parser_postfix_expression_after_paren_type (parser, type_name,
8087 cast_loc);
8088 if (type_name->specs->alignas_p)
8089 error_at (type_name->specs->locations[cdw_alignas],
8090 "alignment specified for type name in cast");
8091 {
8092 location_t expr_loc = c_parser_peek_token (parser)->location;
8093 expr = c_parser_cast_expression (parser, NULL);
8094 expr = convert_lvalue_to_rvalue (expr_loc, expr, true, true);
8095 }
8096 ret.value = c_cast_expr (cast_loc, type_name, expr.value);
8097 if (ret.value && expr.value)
8098 set_c_expr_source_range (&ret, cast_loc, expr.get_finish ());
8099 ret.original_code = ERROR_MARK;
8100 ret.original_type = NULL;
8101 return ret;
8102 }
8103 else
8104 return c_parser_unary_expression (parser);
8105 }
8106
8107 /* Parse an unary expression (C90 6.3.3, C99 6.5.3, C11 6.5.3).
8108
8109 unary-expression:
8110 postfix-expression
8111 ++ unary-expression
8112 -- unary-expression
8113 unary-operator cast-expression
8114 sizeof unary-expression
8115 sizeof ( type-name )
8116
8117 unary-operator: one of
8118 & * + - ~ !
8119
8120 GNU extensions:
8121
8122 unary-expression:
8123 __alignof__ unary-expression
8124 __alignof__ ( type-name )
8125 && identifier
8126
8127 (C11 permits _Alignof with type names only.)
8128
8129 unary-operator: one of
8130 __extension__ __real__ __imag__
8131
8132 Transactional Memory:
8133
8134 unary-expression:
8135 transaction-expression
8136
8137 In addition, the GNU syntax treats ++ and -- as unary operators, so
8138 they may be applied to cast expressions with errors for non-lvalues
8139 given later. */
8140
8141 static struct c_expr
8142 c_parser_unary_expression (c_parser *parser)
8143 {
8144 int ext;
8145 struct c_expr ret, op;
8146 location_t op_loc = c_parser_peek_token (parser)->location;
8147 location_t exp_loc;
8148 location_t finish;
8149 ret.original_code = ERROR_MARK;
8150 ret.original_type = NULL;
8151 switch (c_parser_peek_token (parser)->type)
8152 {
8153 case CPP_PLUS_PLUS:
8154 c_parser_consume_token (parser);
8155 exp_loc = c_parser_peek_token (parser)->location;
8156 op = c_parser_cast_expression (parser, NULL);
8157
8158 op = default_function_array_read_conversion (exp_loc, op);
8159 return parser_build_unary_op (op_loc, PREINCREMENT_EXPR, op);
8160 case CPP_MINUS_MINUS:
8161 c_parser_consume_token (parser);
8162 exp_loc = c_parser_peek_token (parser)->location;
8163 op = c_parser_cast_expression (parser, NULL);
8164
8165 op = default_function_array_read_conversion (exp_loc, op);
8166 return parser_build_unary_op (op_loc, PREDECREMENT_EXPR, op);
8167 case CPP_AND:
8168 c_parser_consume_token (parser);
8169 op = c_parser_cast_expression (parser, NULL);
8170 mark_exp_read (op.value);
8171 return parser_build_unary_op (op_loc, ADDR_EXPR, op);
8172 case CPP_MULT:
8173 {
8174 c_parser_consume_token (parser);
8175 exp_loc = c_parser_peek_token (parser)->location;
8176 op = c_parser_cast_expression (parser, NULL);
8177 finish = op.get_finish ();
8178 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
8179 location_t combined_loc = make_location (op_loc, op_loc, finish);
8180 ret.value = build_indirect_ref (combined_loc, op.value, RO_UNARY_STAR);
8181 ret.src_range.m_start = op_loc;
8182 ret.src_range.m_finish = finish;
8183 return ret;
8184 }
8185 case CPP_PLUS:
8186 if (!c_dialect_objc () && !in_system_header_at (input_location))
8187 warning_at (op_loc,
8188 OPT_Wtraditional,
8189 "traditional C rejects the unary plus operator");
8190 c_parser_consume_token (parser);
8191 exp_loc = c_parser_peek_token (parser)->location;
8192 op = c_parser_cast_expression (parser, NULL);
8193 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
8194 return parser_build_unary_op (op_loc, CONVERT_EXPR, op);
8195 case CPP_MINUS:
8196 c_parser_consume_token (parser);
8197 exp_loc = c_parser_peek_token (parser)->location;
8198 op = c_parser_cast_expression (parser, NULL);
8199 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
8200 return parser_build_unary_op (op_loc, NEGATE_EXPR, op);
8201 case CPP_COMPL:
8202 c_parser_consume_token (parser);
8203 exp_loc = c_parser_peek_token (parser)->location;
8204 op = c_parser_cast_expression (parser, NULL);
8205 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
8206 return parser_build_unary_op (op_loc, BIT_NOT_EXPR, op);
8207 case CPP_NOT:
8208 c_parser_consume_token (parser);
8209 exp_loc = c_parser_peek_token (parser)->location;
8210 op = c_parser_cast_expression (parser, NULL);
8211 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
8212 return parser_build_unary_op (op_loc, TRUTH_NOT_EXPR, op);
8213 case CPP_AND_AND:
8214 /* Refer to the address of a label as a pointer. */
8215 c_parser_consume_token (parser);
8216 if (c_parser_next_token_is (parser, CPP_NAME))
8217 {
8218 ret.value = finish_label_address_expr
8219 (c_parser_peek_token (parser)->value, op_loc);
8220 set_c_expr_source_range (&ret, op_loc,
8221 c_parser_peek_token (parser)->get_finish ());
8222 c_parser_consume_token (parser);
8223 }
8224 else
8225 {
8226 c_parser_error (parser, "expected identifier");
8227 ret.set_error ();
8228 }
8229 return ret;
8230 case CPP_KEYWORD:
8231 switch (c_parser_peek_token (parser)->keyword)
8232 {
8233 case RID_SIZEOF:
8234 return c_parser_sizeof_expression (parser);
8235 case RID_ALIGNOF:
8236 return c_parser_alignof_expression (parser);
8237 case RID_BUILTIN_HAS_ATTRIBUTE:
8238 return c_parser_has_attribute_expression (parser);
8239 case RID_EXTENSION:
8240 c_parser_consume_token (parser);
8241 ext = disable_extension_diagnostics ();
8242 ret = c_parser_cast_expression (parser, NULL);
8243 restore_extension_diagnostics (ext);
8244 return ret;
8245 case RID_REALPART:
8246 c_parser_consume_token (parser);
8247 exp_loc = c_parser_peek_token (parser)->location;
8248 op = c_parser_cast_expression (parser, NULL);
8249 op = default_function_array_conversion (exp_loc, op);
8250 return parser_build_unary_op (op_loc, REALPART_EXPR, op);
8251 case RID_IMAGPART:
8252 c_parser_consume_token (parser);
8253 exp_loc = c_parser_peek_token (parser)->location;
8254 op = c_parser_cast_expression (parser, NULL);
8255 op = default_function_array_conversion (exp_loc, op);
8256 return parser_build_unary_op (op_loc, IMAGPART_EXPR, op);
8257 case RID_TRANSACTION_ATOMIC:
8258 case RID_TRANSACTION_RELAXED:
8259 return c_parser_transaction_expression (parser,
8260 c_parser_peek_token (parser)->keyword);
8261 default:
8262 return c_parser_postfix_expression (parser);
8263 }
8264 default:
8265 return c_parser_postfix_expression (parser);
8266 }
8267 }
8268
8269 /* Parse a sizeof expression. */
8270
8271 static struct c_expr
8272 c_parser_sizeof_expression (c_parser *parser)
8273 {
8274 struct c_expr expr;
8275 struct c_expr result;
8276 location_t expr_loc;
8277 gcc_assert (c_parser_next_token_is_keyword (parser, RID_SIZEOF));
8278
8279 location_t start;
8280 location_t finish = UNKNOWN_LOCATION;
8281
8282 start = c_parser_peek_token (parser)->location;
8283
8284 c_parser_consume_token (parser);
8285 c_inhibit_evaluation_warnings++;
8286 in_sizeof++;
8287 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
8288 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
8289 {
8290 /* Either sizeof ( type-name ) or sizeof unary-expression
8291 starting with a compound literal. */
8292 struct c_type_name *type_name;
8293 matching_parens parens;
8294 parens.consume_open (parser);
8295 expr_loc = c_parser_peek_token (parser)->location;
8296 type_name = c_parser_type_name (parser, true);
8297 parens.skip_until_found_close (parser);
8298 finish = parser->tokens_buf[0].location;
8299 if (type_name == NULL)
8300 {
8301 struct c_expr ret;
8302 c_inhibit_evaluation_warnings--;
8303 in_sizeof--;
8304 ret.set_error ();
8305 ret.original_code = ERROR_MARK;
8306 ret.original_type = NULL;
8307 return ret;
8308 }
8309 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
8310 {
8311 expr = c_parser_postfix_expression_after_paren_type (parser,
8312 type_name,
8313 expr_loc);
8314 finish = expr.get_finish ();
8315 goto sizeof_expr;
8316 }
8317 /* sizeof ( type-name ). */
8318 if (type_name->specs->alignas_p)
8319 error_at (type_name->specs->locations[cdw_alignas],
8320 "alignment specified for type name in %<sizeof%>");
8321 c_inhibit_evaluation_warnings--;
8322 in_sizeof--;
8323 result = c_expr_sizeof_type (expr_loc, type_name);
8324 }
8325 else
8326 {
8327 expr_loc = c_parser_peek_token (parser)->location;
8328 expr = c_parser_unary_expression (parser);
8329 finish = expr.get_finish ();
8330 sizeof_expr:
8331 c_inhibit_evaluation_warnings--;
8332 in_sizeof--;
8333 mark_exp_read (expr.value);
8334 if (TREE_CODE (expr.value) == COMPONENT_REF
8335 && DECL_C_BIT_FIELD (TREE_OPERAND (expr.value, 1)))
8336 error_at (expr_loc, "%<sizeof%> applied to a bit-field");
8337 result = c_expr_sizeof_expr (expr_loc, expr);
8338 }
8339 if (finish == UNKNOWN_LOCATION)
8340 finish = start;
8341 set_c_expr_source_range (&result, start, finish);
8342 return result;
8343 }
8344
8345 /* Parse an alignof expression. */
8346
8347 static struct c_expr
8348 c_parser_alignof_expression (c_parser *parser)
8349 {
8350 struct c_expr expr;
8351 location_t start_loc = c_parser_peek_token (parser)->location;
8352 location_t end_loc;
8353 tree alignof_spelling = c_parser_peek_token (parser)->value;
8354 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ALIGNOF));
8355 bool is_c11_alignof = strcmp (IDENTIFIER_POINTER (alignof_spelling),
8356 "_Alignof") == 0;
8357 /* A diagnostic is not required for the use of this identifier in
8358 the implementation namespace; only diagnose it for the C11
8359 spelling because of existing code using the other spellings. */
8360 if (is_c11_alignof)
8361 {
8362 if (flag_isoc99)
8363 pedwarn_c99 (start_loc, OPT_Wpedantic, "ISO C99 does not support %qE",
8364 alignof_spelling);
8365 else
8366 pedwarn_c99 (start_loc, OPT_Wpedantic, "ISO C90 does not support %qE",
8367 alignof_spelling);
8368 }
8369 c_parser_consume_token (parser);
8370 c_inhibit_evaluation_warnings++;
8371 in_alignof++;
8372 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
8373 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
8374 {
8375 /* Either __alignof__ ( type-name ) or __alignof__
8376 unary-expression starting with a compound literal. */
8377 location_t loc;
8378 struct c_type_name *type_name;
8379 struct c_expr ret;
8380 matching_parens parens;
8381 parens.consume_open (parser);
8382 loc = c_parser_peek_token (parser)->location;
8383 type_name = c_parser_type_name (parser, true);
8384 end_loc = c_parser_peek_token (parser)->location;
8385 parens.skip_until_found_close (parser);
8386 if (type_name == NULL)
8387 {
8388 struct c_expr ret;
8389 c_inhibit_evaluation_warnings--;
8390 in_alignof--;
8391 ret.set_error ();
8392 ret.original_code = ERROR_MARK;
8393 ret.original_type = NULL;
8394 return ret;
8395 }
8396 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
8397 {
8398 expr = c_parser_postfix_expression_after_paren_type (parser,
8399 type_name,
8400 loc);
8401 goto alignof_expr;
8402 }
8403 /* alignof ( type-name ). */
8404 if (type_name->specs->alignas_p)
8405 error_at (type_name->specs->locations[cdw_alignas],
8406 "alignment specified for type name in %qE",
8407 alignof_spelling);
8408 c_inhibit_evaluation_warnings--;
8409 in_alignof--;
8410 ret.value = c_sizeof_or_alignof_type (loc, groktypename (type_name,
8411 NULL, NULL),
8412 false, is_c11_alignof, 1);
8413 ret.original_code = ERROR_MARK;
8414 ret.original_type = NULL;
8415 set_c_expr_source_range (&ret, start_loc, end_loc);
8416 return ret;
8417 }
8418 else
8419 {
8420 struct c_expr ret;
8421 expr = c_parser_unary_expression (parser);
8422 end_loc = expr.src_range.m_finish;
8423 alignof_expr:
8424 mark_exp_read (expr.value);
8425 c_inhibit_evaluation_warnings--;
8426 in_alignof--;
8427 if (is_c11_alignof)
8428 pedwarn (start_loc,
8429 OPT_Wpedantic, "ISO C does not allow %<%E (expression)%>",
8430 alignof_spelling);
8431 ret.value = c_alignof_expr (start_loc, expr.value);
8432 ret.original_code = ERROR_MARK;
8433 ret.original_type = NULL;
8434 set_c_expr_source_range (&ret, start_loc, end_loc);
8435 return ret;
8436 }
8437 }
8438
8439 /* Parse the __builtin_has_attribute ([expr|type], attribute-spec)
8440 expression. */
8441
8442 static struct c_expr
8443 c_parser_has_attribute_expression (c_parser *parser)
8444 {
8445 gcc_assert (c_parser_next_token_is_keyword (parser,
8446 RID_BUILTIN_HAS_ATTRIBUTE));
8447 c_parser_consume_token (parser);
8448
8449 c_inhibit_evaluation_warnings++;
8450
8451 matching_parens parens;
8452 if (!parens.require_open (parser))
8453 {
8454 c_inhibit_evaluation_warnings--;
8455 in_typeof--;
8456
8457 struct c_expr result;
8458 result.set_error ();
8459 result.original_code = ERROR_MARK;
8460 result.original_type = NULL;
8461 return result;
8462 }
8463
8464 /* Treat the type argument the same way as in typeof for the purposes
8465 of warnings. FIXME: Generalize this so the warning refers to
8466 __builtin_has_attribute rather than typeof. */
8467 in_typeof++;
8468
8469 /* The first operand: one of DECL, EXPR, or TYPE. */
8470 tree oper = NULL_TREE;
8471 if (c_parser_next_tokens_start_typename (parser, cla_prefer_id))
8472 {
8473 struct c_type_name *tname = c_parser_type_name (parser);
8474 in_typeof--;
8475 if (tname)
8476 {
8477 oper = groktypename (tname, NULL, NULL);
8478 pop_maybe_used (variably_modified_type_p (oper, NULL_TREE));
8479 }
8480 }
8481 else
8482 {
8483 struct c_expr cexpr = c_parser_expr_no_commas (parser, NULL);
8484 c_inhibit_evaluation_warnings--;
8485 in_typeof--;
8486 if (cexpr.value != error_mark_node)
8487 {
8488 mark_exp_read (cexpr.value);
8489 oper = cexpr.value;
8490 tree etype = TREE_TYPE (oper);
8491 bool was_vm = variably_modified_type_p (etype, NULL_TREE);
8492 /* This is returned with the type so that when the type is
8493 evaluated, this can be evaluated. */
8494 if (was_vm)
8495 oper = c_fully_fold (oper, false, NULL);
8496 pop_maybe_used (was_vm);
8497 }
8498 }
8499
8500 struct c_expr result;
8501 result.original_code = ERROR_MARK;
8502 result.original_type = NULL;
8503
8504 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
8505 {
8506 /* Consume the closing parenthesis if that's the next token
8507 in the likely case the built-in was invoked with fewer
8508 than two arguments. */
8509 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
8510 c_parser_consume_token (parser);
8511 c_inhibit_evaluation_warnings--;
8512 result.set_error ();
8513 return result;
8514 }
8515
8516 bool save_translate_strings_p = parser->translate_strings_p;
8517
8518 location_t atloc = c_parser_peek_token (parser)->location;
8519 /* Parse a single attribute. Require no leading comma and do not
8520 allow empty attributes. */
8521 tree attr = c_parser_gnu_attribute (parser, NULL_TREE, false, false);
8522
8523 parser->translate_strings_p = save_translate_strings_p;
8524
8525 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
8526 c_parser_consume_token (parser);
8527 else
8528 {
8529 c_parser_error (parser, "expected identifier");
8530 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
8531
8532 result.set_error ();
8533 return result;
8534 }
8535
8536 if (!attr)
8537 {
8538 error_at (atloc, "expected identifier");
8539 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
8540 "expected %<)%>");
8541 result.set_error ();
8542 return result;
8543 }
8544
8545 result.original_code = INTEGER_CST;
8546 result.original_type = boolean_type_node;
8547
8548 if (has_attribute (atloc, oper, attr, default_conversion))
8549 result.value = boolean_true_node;
8550 else
8551 result.value = boolean_false_node;
8552
8553 return result;
8554 }
8555
8556 /* Helper function to read arguments of builtins which are interfaces
8557 for the middle-end nodes like COMPLEX_EXPR, VEC_PERM_EXPR and
8558 others. The name of the builtin is passed using BNAME parameter.
8559 Function returns true if there were no errors while parsing and
8560 stores the arguments in CEXPR_LIST. If it returns true,
8561 *OUT_CLOSE_PAREN_LOC is written to with the location of the closing
8562 parenthesis. */
8563 static bool
8564 c_parser_get_builtin_args (c_parser *parser, const char *bname,
8565 vec<c_expr_t, va_gc> **ret_cexpr_list,
8566 bool choose_expr_p,
8567 location_t *out_close_paren_loc)
8568 {
8569 location_t loc = c_parser_peek_token (parser)->location;
8570 vec<c_expr_t, va_gc> *cexpr_list;
8571 c_expr_t expr;
8572 bool saved_force_folding_builtin_constant_p;
8573
8574 *ret_cexpr_list = NULL;
8575 if (c_parser_next_token_is_not (parser, CPP_OPEN_PAREN))
8576 {
8577 error_at (loc, "cannot take address of %qs", bname);
8578 return false;
8579 }
8580
8581 c_parser_consume_token (parser);
8582
8583 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
8584 {
8585 *out_close_paren_loc = c_parser_peek_token (parser)->location;
8586 c_parser_consume_token (parser);
8587 return true;
8588 }
8589
8590 saved_force_folding_builtin_constant_p
8591 = force_folding_builtin_constant_p;
8592 force_folding_builtin_constant_p |= choose_expr_p;
8593 expr = c_parser_expr_no_commas (parser, NULL);
8594 force_folding_builtin_constant_p
8595 = saved_force_folding_builtin_constant_p;
8596 vec_alloc (cexpr_list, 1);
8597 vec_safe_push (cexpr_list, expr);
8598 while (c_parser_next_token_is (parser, CPP_COMMA))
8599 {
8600 c_parser_consume_token (parser);
8601 expr = c_parser_expr_no_commas (parser, NULL);
8602 vec_safe_push (cexpr_list, expr);
8603 }
8604
8605 *out_close_paren_loc = c_parser_peek_token (parser)->location;
8606 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
8607 return false;
8608
8609 *ret_cexpr_list = cexpr_list;
8610 return true;
8611 }
8612
8613 /* This represents a single generic-association. */
8614
8615 struct c_generic_association
8616 {
8617 /* The location of the starting token of the type. */
8618 location_t type_location;
8619 /* The association's type, or NULL_TREE for 'default'. */
8620 tree type;
8621 /* The association's expression. */
8622 struct c_expr expression;
8623 };
8624
8625 /* Parse a generic-selection. (C11 6.5.1.1).
8626
8627 generic-selection:
8628 _Generic ( assignment-expression , generic-assoc-list )
8629
8630 generic-assoc-list:
8631 generic-association
8632 generic-assoc-list , generic-association
8633
8634 generic-association:
8635 type-name : assignment-expression
8636 default : assignment-expression
8637 */
8638
8639 static struct c_expr
8640 c_parser_generic_selection (c_parser *parser)
8641 {
8642 struct c_expr selector, error_expr;
8643 tree selector_type;
8644 struct c_generic_association matched_assoc;
8645 bool match_found = false;
8646 location_t generic_loc, selector_loc;
8647
8648 error_expr.original_code = ERROR_MARK;
8649 error_expr.original_type = NULL;
8650 error_expr.set_error ();
8651 matched_assoc.type_location = UNKNOWN_LOCATION;
8652 matched_assoc.type = NULL_TREE;
8653 matched_assoc.expression = error_expr;
8654
8655 gcc_assert (c_parser_next_token_is_keyword (parser, RID_GENERIC));
8656 generic_loc = c_parser_peek_token (parser)->location;
8657 c_parser_consume_token (parser);
8658 if (flag_isoc99)
8659 pedwarn_c99 (generic_loc, OPT_Wpedantic,
8660 "ISO C99 does not support %<_Generic%>");
8661 else
8662 pedwarn_c99 (generic_loc, OPT_Wpedantic,
8663 "ISO C90 does not support %<_Generic%>");
8664
8665 matching_parens parens;
8666 if (!parens.require_open (parser))
8667 return error_expr;
8668
8669 c_inhibit_evaluation_warnings++;
8670 selector_loc = c_parser_peek_token (parser)->location;
8671 selector = c_parser_expr_no_commas (parser, NULL);
8672 selector = default_function_array_conversion (selector_loc, selector);
8673 c_inhibit_evaluation_warnings--;
8674
8675 if (selector.value == error_mark_node)
8676 {
8677 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
8678 return selector;
8679 }
8680 selector_type = TREE_TYPE (selector.value);
8681 /* In ISO C terms, rvalues (including the controlling expression of
8682 _Generic) do not have qualified types. */
8683 if (TREE_CODE (selector_type) != ARRAY_TYPE)
8684 selector_type = TYPE_MAIN_VARIANT (selector_type);
8685 /* In ISO C terms, _Noreturn is not part of the type of expressions
8686 such as &abort, but in GCC it is represented internally as a type
8687 qualifier. */
8688 if (FUNCTION_POINTER_TYPE_P (selector_type)
8689 && TYPE_QUALS (TREE_TYPE (selector_type)) != TYPE_UNQUALIFIED)
8690 selector_type
8691 = build_pointer_type (TYPE_MAIN_VARIANT (TREE_TYPE (selector_type)));
8692
8693 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
8694 {
8695 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
8696 return error_expr;
8697 }
8698
8699 auto_vec<c_generic_association> associations;
8700 while (1)
8701 {
8702 struct c_generic_association assoc, *iter;
8703 unsigned int ix;
8704 c_token *token = c_parser_peek_token (parser);
8705
8706 assoc.type_location = token->location;
8707 if (token->type == CPP_KEYWORD && token->keyword == RID_DEFAULT)
8708 {
8709 c_parser_consume_token (parser);
8710 assoc.type = NULL_TREE;
8711 }
8712 else
8713 {
8714 struct c_type_name *type_name;
8715
8716 type_name = c_parser_type_name (parser);
8717 if (type_name == NULL)
8718 {
8719 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
8720 return error_expr;
8721 }
8722 assoc.type = groktypename (type_name, NULL, NULL);
8723 if (assoc.type == error_mark_node)
8724 {
8725 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
8726 return error_expr;
8727 }
8728
8729 if (TREE_CODE (assoc.type) == FUNCTION_TYPE)
8730 error_at (assoc.type_location,
8731 "%<_Generic%> association has function type");
8732 else if (!COMPLETE_TYPE_P (assoc.type))
8733 error_at (assoc.type_location,
8734 "%<_Generic%> association has incomplete type");
8735
8736 if (variably_modified_type_p (assoc.type, NULL_TREE))
8737 error_at (assoc.type_location,
8738 "%<_Generic%> association has "
8739 "variable length type");
8740 }
8741
8742 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
8743 {
8744 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
8745 return error_expr;
8746 }
8747
8748 assoc.expression = c_parser_expr_no_commas (parser, NULL);
8749 if (assoc.expression.value == error_mark_node)
8750 {
8751 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
8752 return error_expr;
8753 }
8754
8755 for (ix = 0; associations.iterate (ix, &iter); ++ix)
8756 {
8757 if (assoc.type == NULL_TREE)
8758 {
8759 if (iter->type == NULL_TREE)
8760 {
8761 error_at (assoc.type_location,
8762 "duplicate %<default%> case in %<_Generic%>");
8763 inform (iter->type_location, "original %<default%> is here");
8764 }
8765 }
8766 else if (iter->type != NULL_TREE)
8767 {
8768 if (comptypes (assoc.type, iter->type))
8769 {
8770 error_at (assoc.type_location,
8771 "%<_Generic%> specifies two compatible types");
8772 inform (iter->type_location, "compatible type is here");
8773 }
8774 }
8775 }
8776
8777 if (assoc.type == NULL_TREE)
8778 {
8779 if (!match_found)
8780 {
8781 matched_assoc = assoc;
8782 match_found = true;
8783 }
8784 }
8785 else if (comptypes (assoc.type, selector_type))
8786 {
8787 if (!match_found || matched_assoc.type == NULL_TREE)
8788 {
8789 matched_assoc = assoc;
8790 match_found = true;
8791 }
8792 else
8793 {
8794 error_at (assoc.type_location,
8795 "%<_Generic%> selector matches multiple associations");
8796 inform (matched_assoc.type_location,
8797 "other match is here");
8798 }
8799 }
8800
8801 associations.safe_push (assoc);
8802
8803 if (c_parser_peek_token (parser)->type != CPP_COMMA)
8804 break;
8805 c_parser_consume_token (parser);
8806 }
8807
8808 if (!parens.require_close (parser))
8809 {
8810 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
8811 return error_expr;
8812 }
8813
8814 if (!match_found)
8815 {
8816 error_at (selector_loc, "%<_Generic%> selector of type %qT is not "
8817 "compatible with any association",
8818 selector_type);
8819 return error_expr;
8820 }
8821
8822 return matched_assoc.expression;
8823 }
8824
8825 /* Check the validity of a function pointer argument *EXPR (argument
8826 position POS) to __builtin_tgmath. Return the number of function
8827 arguments if possibly valid; return 0 having reported an error if
8828 not valid. */
8829
8830 static unsigned int
8831 check_tgmath_function (c_expr *expr, unsigned int pos)
8832 {
8833 tree type = TREE_TYPE (expr->value);
8834 if (!FUNCTION_POINTER_TYPE_P (type))
8835 {
8836 error_at (expr->get_location (),
8837 "argument %u of %<__builtin_tgmath%> is not a function pointer",
8838 pos);
8839 return 0;
8840 }
8841 type = TREE_TYPE (type);
8842 if (!prototype_p (type))
8843 {
8844 error_at (expr->get_location (),
8845 "argument %u of %<__builtin_tgmath%> is unprototyped", pos);
8846 return 0;
8847 }
8848 if (stdarg_p (type))
8849 {
8850 error_at (expr->get_location (),
8851 "argument %u of %<__builtin_tgmath%> has variable arguments",
8852 pos);
8853 return 0;
8854 }
8855 unsigned int nargs = 0;
8856 function_args_iterator iter;
8857 tree t;
8858 FOREACH_FUNCTION_ARGS (type, t, iter)
8859 {
8860 if (t == void_type_node)
8861 break;
8862 nargs++;
8863 }
8864 if (nargs == 0)
8865 {
8866 error_at (expr->get_location (),
8867 "argument %u of %<__builtin_tgmath%> has no arguments", pos);
8868 return 0;
8869 }
8870 return nargs;
8871 }
8872
8873 /* Ways in which a parameter or return value of a type-generic macro
8874 may vary between the different functions the macro may call. */
8875 enum tgmath_parm_kind
8876 {
8877 tgmath_fixed, tgmath_real, tgmath_complex
8878 };
8879
8880 /* Helper function for c_parser_postfix_expression. Parse predefined
8881 identifiers. */
8882
8883 static struct c_expr
8884 c_parser_predefined_identifier (c_parser *parser)
8885 {
8886 location_t loc = c_parser_peek_token (parser)->location;
8887 switch (c_parser_peek_token (parser)->keyword)
8888 {
8889 case RID_FUNCTION_NAME:
8890 pedwarn (loc, OPT_Wpedantic, "ISO C does not support %qs predefined "
8891 "identifier", "__FUNCTION__");
8892 break;
8893 case RID_PRETTY_FUNCTION_NAME:
8894 pedwarn (loc, OPT_Wpedantic, "ISO C does not support %qs predefined "
8895 "identifier", "__PRETTY_FUNCTION__");
8896 break;
8897 case RID_C99_FUNCTION_NAME:
8898 pedwarn_c90 (loc, OPT_Wpedantic, "ISO C90 does not support "
8899 "%<__func__%> predefined identifier");
8900 break;
8901 default:
8902 gcc_unreachable ();
8903 }
8904
8905 struct c_expr expr;
8906 expr.original_code = ERROR_MARK;
8907 expr.original_type = NULL;
8908 expr.value = fname_decl (loc, c_parser_peek_token (parser)->keyword,
8909 c_parser_peek_token (parser)->value);
8910 set_c_expr_source_range (&expr, loc, loc);
8911 c_parser_consume_token (parser);
8912 return expr;
8913 }
8914
8915 /* Parse a postfix expression (C90 6.3.1-6.3.2, C99 6.5.1-6.5.2,
8916 C11 6.5.1-6.5.2). Compound literals aren't handled here; callers have to
8917 call c_parser_postfix_expression_after_paren_type on encountering them.
8918
8919 postfix-expression:
8920 primary-expression
8921 postfix-expression [ expression ]
8922 postfix-expression ( argument-expression-list[opt] )
8923 postfix-expression . identifier
8924 postfix-expression -> identifier
8925 postfix-expression ++
8926 postfix-expression --
8927 ( type-name ) { initializer-list }
8928 ( type-name ) { initializer-list , }
8929
8930 argument-expression-list:
8931 argument-expression
8932 argument-expression-list , argument-expression
8933
8934 primary-expression:
8935 identifier
8936 constant
8937 string-literal
8938 ( expression )
8939 generic-selection
8940
8941 GNU extensions:
8942
8943 primary-expression:
8944 __func__
8945 (treated as a keyword in GNU C)
8946 __FUNCTION__
8947 __PRETTY_FUNCTION__
8948 ( compound-statement )
8949 __builtin_va_arg ( assignment-expression , type-name )
8950 __builtin_offsetof ( type-name , offsetof-member-designator )
8951 __builtin_choose_expr ( assignment-expression ,
8952 assignment-expression ,
8953 assignment-expression )
8954 __builtin_types_compatible_p ( type-name , type-name )
8955 __builtin_tgmath ( expr-list )
8956 __builtin_complex ( assignment-expression , assignment-expression )
8957 __builtin_shuffle ( assignment-expression , assignment-expression )
8958 __builtin_shuffle ( assignment-expression ,
8959 assignment-expression ,
8960 assignment-expression, )
8961 __builtin_convertvector ( assignment-expression , type-name )
8962
8963 offsetof-member-designator:
8964 identifier
8965 offsetof-member-designator . identifier
8966 offsetof-member-designator [ expression ]
8967
8968 Objective-C:
8969
8970 primary-expression:
8971 [ objc-receiver objc-message-args ]
8972 @selector ( objc-selector-arg )
8973 @protocol ( identifier )
8974 @encode ( type-name )
8975 objc-string-literal
8976 Classname . identifier
8977 */
8978
8979 static struct c_expr
8980 c_parser_postfix_expression (c_parser *parser)
8981 {
8982 struct c_expr expr, e1;
8983 struct c_type_name *t1, *t2;
8984 location_t loc = c_parser_peek_token (parser)->location;
8985 source_range tok_range = c_parser_peek_token (parser)->get_range ();
8986 expr.original_code = ERROR_MARK;
8987 expr.original_type = NULL;
8988 switch (c_parser_peek_token (parser)->type)
8989 {
8990 case CPP_NUMBER:
8991 expr.value = c_parser_peek_token (parser)->value;
8992 set_c_expr_source_range (&expr, tok_range);
8993 loc = c_parser_peek_token (parser)->location;
8994 c_parser_consume_token (parser);
8995 if (TREE_CODE (expr.value) == FIXED_CST
8996 && !targetm.fixed_point_supported_p ())
8997 {
8998 error_at (loc, "fixed-point types not supported for this target");
8999 expr.set_error ();
9000 }
9001 break;
9002 case CPP_CHAR:
9003 case CPP_CHAR16:
9004 case CPP_CHAR32:
9005 case CPP_UTF8CHAR:
9006 case CPP_WCHAR:
9007 expr.value = c_parser_peek_token (parser)->value;
9008 /* For the purpose of warning when a pointer is compared with
9009 a zero character constant. */
9010 expr.original_type = char_type_node;
9011 set_c_expr_source_range (&expr, tok_range);
9012 c_parser_consume_token (parser);
9013 break;
9014 case CPP_STRING:
9015 case CPP_STRING16:
9016 case CPP_STRING32:
9017 case CPP_WSTRING:
9018 case CPP_UTF8STRING:
9019 expr = c_parser_string_literal (parser, parser->translate_strings_p,
9020 true);
9021 break;
9022 case CPP_OBJC_STRING:
9023 gcc_assert (c_dialect_objc ());
9024 expr.value
9025 = objc_build_string_object (c_parser_peek_token (parser)->value);
9026 set_c_expr_source_range (&expr, tok_range);
9027 c_parser_consume_token (parser);
9028 break;
9029 case CPP_NAME:
9030 switch (c_parser_peek_token (parser)->id_kind)
9031 {
9032 case C_ID_ID:
9033 {
9034 tree id = c_parser_peek_token (parser)->value;
9035 c_parser_consume_token (parser);
9036 expr.value = build_external_ref (loc, id,
9037 (c_parser_peek_token (parser)->type
9038 == CPP_OPEN_PAREN),
9039 &expr.original_type);
9040 set_c_expr_source_range (&expr, tok_range);
9041 break;
9042 }
9043 case C_ID_CLASSNAME:
9044 {
9045 /* Here we parse the Objective-C 2.0 Class.name dot
9046 syntax. */
9047 tree class_name = c_parser_peek_token (parser)->value;
9048 tree component;
9049 c_parser_consume_token (parser);
9050 gcc_assert (c_dialect_objc ());
9051 if (!c_parser_require (parser, CPP_DOT, "expected %<.%>"))
9052 {
9053 expr.set_error ();
9054 break;
9055 }
9056 if (c_parser_next_token_is_not (parser, CPP_NAME))
9057 {
9058 c_parser_error (parser, "expected identifier");
9059 expr.set_error ();
9060 break;
9061 }
9062 c_token *component_tok = c_parser_peek_token (parser);
9063 component = component_tok->value;
9064 location_t end_loc = component_tok->get_finish ();
9065 c_parser_consume_token (parser);
9066 expr.value = objc_build_class_component_ref (class_name,
9067 component);
9068 set_c_expr_source_range (&expr, loc, end_loc);
9069 break;
9070 }
9071 default:
9072 c_parser_error (parser, "expected expression");
9073 expr.set_error ();
9074 break;
9075 }
9076 break;
9077 case CPP_OPEN_PAREN:
9078 /* A parenthesized expression, statement expression or compound
9079 literal. */
9080 if (c_parser_peek_2nd_token (parser)->type == CPP_OPEN_BRACE)
9081 {
9082 /* A statement expression. */
9083 tree stmt;
9084 location_t brace_loc;
9085 c_parser_consume_token (parser);
9086 brace_loc = c_parser_peek_token (parser)->location;
9087 c_parser_consume_token (parser);
9088 /* If we've not yet started the current function's statement list,
9089 or we're in the parameter scope of an old-style function
9090 declaration, statement expressions are not allowed. */
9091 if (!building_stmt_list_p () || old_style_parameter_scope ())
9092 {
9093 error_at (loc, "braced-group within expression allowed "
9094 "only inside a function");
9095 parser->error = true;
9096 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
9097 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
9098 expr.set_error ();
9099 break;
9100 }
9101 stmt = c_begin_stmt_expr ();
9102 c_parser_compound_statement_nostart (parser);
9103 location_t close_loc = c_parser_peek_token (parser)->location;
9104 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
9105 "expected %<)%>");
9106 pedwarn (loc, OPT_Wpedantic,
9107 "ISO C forbids braced-groups within expressions");
9108 expr.value = c_finish_stmt_expr (brace_loc, stmt);
9109 set_c_expr_source_range (&expr, loc, close_loc);
9110 mark_exp_read (expr.value);
9111 }
9112 else
9113 {
9114 /* A parenthesized expression. */
9115 location_t loc_open_paren = c_parser_peek_token (parser)->location;
9116 c_parser_consume_token (parser);
9117 expr = c_parser_expression (parser);
9118 if (TREE_CODE (expr.value) == MODIFY_EXPR)
9119 TREE_NO_WARNING (expr.value) = 1;
9120 if (expr.original_code != C_MAYBE_CONST_EXPR
9121 && expr.original_code != SIZEOF_EXPR)
9122 expr.original_code = ERROR_MARK;
9123 /* Don't change EXPR.ORIGINAL_TYPE. */
9124 location_t loc_close_paren = c_parser_peek_token (parser)->location;
9125 set_c_expr_source_range (&expr, loc_open_paren, loc_close_paren);
9126 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
9127 "expected %<)%>", loc_open_paren);
9128 }
9129 break;
9130 case CPP_KEYWORD:
9131 switch (c_parser_peek_token (parser)->keyword)
9132 {
9133 case RID_FUNCTION_NAME:
9134 case RID_PRETTY_FUNCTION_NAME:
9135 case RID_C99_FUNCTION_NAME:
9136 expr = c_parser_predefined_identifier (parser);
9137 break;
9138 case RID_VA_ARG:
9139 {
9140 location_t start_loc = loc;
9141 c_parser_consume_token (parser);
9142 matching_parens parens;
9143 if (!parens.require_open (parser))
9144 {
9145 expr.set_error ();
9146 break;
9147 }
9148 e1 = c_parser_expr_no_commas (parser, NULL);
9149 mark_exp_read (e1.value);
9150 e1.value = c_fully_fold (e1.value, false, NULL);
9151 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
9152 {
9153 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
9154 expr.set_error ();
9155 break;
9156 }
9157 loc = c_parser_peek_token (parser)->location;
9158 t1 = c_parser_type_name (parser);
9159 location_t end_loc = c_parser_peek_token (parser)->get_finish ();
9160 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
9161 "expected %<)%>");
9162 if (t1 == NULL)
9163 {
9164 expr.set_error ();
9165 }
9166 else
9167 {
9168 tree type_expr = NULL_TREE;
9169 expr.value = c_build_va_arg (start_loc, e1.value, loc,
9170 groktypename (t1, &type_expr, NULL));
9171 if (type_expr)
9172 {
9173 expr.value = build2 (C_MAYBE_CONST_EXPR,
9174 TREE_TYPE (expr.value), type_expr,
9175 expr.value);
9176 C_MAYBE_CONST_EXPR_NON_CONST (expr.value) = true;
9177 }
9178 set_c_expr_source_range (&expr, start_loc, end_loc);
9179 }
9180 }
9181 break;
9182 case RID_OFFSETOF:
9183 {
9184 c_parser_consume_token (parser);
9185 matching_parens parens;
9186 if (!parens.require_open (parser))
9187 {
9188 expr.set_error ();
9189 break;
9190 }
9191 t1 = c_parser_type_name (parser);
9192 if (t1 == NULL)
9193 parser->error = true;
9194 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
9195 gcc_assert (parser->error);
9196 if (parser->error)
9197 {
9198 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
9199 expr.set_error ();
9200 break;
9201 }
9202 tree type = groktypename (t1, NULL, NULL);
9203 tree offsetof_ref;
9204 if (type == error_mark_node)
9205 offsetof_ref = error_mark_node;
9206 else
9207 {
9208 offsetof_ref = build1 (INDIRECT_REF, type, null_pointer_node);
9209 SET_EXPR_LOCATION (offsetof_ref, loc);
9210 }
9211 /* Parse the second argument to __builtin_offsetof. We
9212 must have one identifier, and beyond that we want to
9213 accept sub structure and sub array references. */
9214 if (c_parser_next_token_is (parser, CPP_NAME))
9215 {
9216 c_token *comp_tok = c_parser_peek_token (parser);
9217 offsetof_ref = build_component_ref
9218 (loc, offsetof_ref, comp_tok->value, comp_tok->location);
9219 c_parser_consume_token (parser);
9220 while (c_parser_next_token_is (parser, CPP_DOT)
9221 || c_parser_next_token_is (parser,
9222 CPP_OPEN_SQUARE)
9223 || c_parser_next_token_is (parser,
9224 CPP_DEREF))
9225 {
9226 if (c_parser_next_token_is (parser, CPP_DEREF))
9227 {
9228 loc = c_parser_peek_token (parser)->location;
9229 offsetof_ref = build_array_ref (loc,
9230 offsetof_ref,
9231 integer_zero_node);
9232 goto do_dot;
9233 }
9234 else if (c_parser_next_token_is (parser, CPP_DOT))
9235 {
9236 do_dot:
9237 c_parser_consume_token (parser);
9238 if (c_parser_next_token_is_not (parser,
9239 CPP_NAME))
9240 {
9241 c_parser_error (parser, "expected identifier");
9242 break;
9243 }
9244 c_token *comp_tok = c_parser_peek_token (parser);
9245 offsetof_ref = build_component_ref
9246 (loc, offsetof_ref, comp_tok->value,
9247 comp_tok->location);
9248 c_parser_consume_token (parser);
9249 }
9250 else
9251 {
9252 struct c_expr ce;
9253 tree idx;
9254 loc = c_parser_peek_token (parser)->location;
9255 c_parser_consume_token (parser);
9256 ce = c_parser_expression (parser);
9257 ce = convert_lvalue_to_rvalue (loc, ce, false, false);
9258 idx = ce.value;
9259 idx = c_fully_fold (idx, false, NULL);
9260 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
9261 "expected %<]%>");
9262 offsetof_ref = build_array_ref (loc, offsetof_ref, idx);
9263 }
9264 }
9265 }
9266 else
9267 c_parser_error (parser, "expected identifier");
9268 location_t end_loc = c_parser_peek_token (parser)->get_finish ();
9269 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
9270 "expected %<)%>");
9271 expr.value = fold_offsetof (offsetof_ref);
9272 set_c_expr_source_range (&expr, loc, end_loc);
9273 }
9274 break;
9275 case RID_CHOOSE_EXPR:
9276 {
9277 vec<c_expr_t, va_gc> *cexpr_list;
9278 c_expr_t *e1_p, *e2_p, *e3_p;
9279 tree c;
9280 location_t close_paren_loc;
9281
9282 c_parser_consume_token (parser);
9283 if (!c_parser_get_builtin_args (parser,
9284 "__builtin_choose_expr",
9285 &cexpr_list, true,
9286 &close_paren_loc))
9287 {
9288 expr.set_error ();
9289 break;
9290 }
9291
9292 if (vec_safe_length (cexpr_list) != 3)
9293 {
9294 error_at (loc, "wrong number of arguments to "
9295 "%<__builtin_choose_expr%>");
9296 expr.set_error ();
9297 break;
9298 }
9299
9300 e1_p = &(*cexpr_list)[0];
9301 e2_p = &(*cexpr_list)[1];
9302 e3_p = &(*cexpr_list)[2];
9303
9304 c = e1_p->value;
9305 mark_exp_read (e2_p->value);
9306 mark_exp_read (e3_p->value);
9307 if (TREE_CODE (c) != INTEGER_CST
9308 || !INTEGRAL_TYPE_P (TREE_TYPE (c)))
9309 error_at (loc,
9310 "first argument to %<__builtin_choose_expr%> not"
9311 " a constant");
9312 constant_expression_warning (c);
9313 expr = integer_zerop (c) ? *e3_p : *e2_p;
9314 set_c_expr_source_range (&expr, loc, close_paren_loc);
9315 break;
9316 }
9317 case RID_TYPES_COMPATIBLE_P:
9318 {
9319 c_parser_consume_token (parser);
9320 matching_parens parens;
9321 if (!parens.require_open (parser))
9322 {
9323 expr.set_error ();
9324 break;
9325 }
9326 t1 = c_parser_type_name (parser);
9327 if (t1 == NULL)
9328 {
9329 expr.set_error ();
9330 break;
9331 }
9332 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
9333 {
9334 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
9335 expr.set_error ();
9336 break;
9337 }
9338 t2 = c_parser_type_name (parser);
9339 if (t2 == NULL)
9340 {
9341 expr.set_error ();
9342 break;
9343 }
9344 location_t close_paren_loc = c_parser_peek_token (parser)->location;
9345 parens.skip_until_found_close (parser);
9346 tree e1, e2;
9347 e1 = groktypename (t1, NULL, NULL);
9348 e2 = groktypename (t2, NULL, NULL);
9349 if (e1 == error_mark_node || e2 == error_mark_node)
9350 {
9351 expr.set_error ();
9352 break;
9353 }
9354
9355 e1 = TYPE_MAIN_VARIANT (e1);
9356 e2 = TYPE_MAIN_VARIANT (e2);
9357
9358 expr.value
9359 = comptypes (e1, e2) ? integer_one_node : integer_zero_node;
9360 set_c_expr_source_range (&expr, loc, close_paren_loc);
9361 }
9362 break;
9363 case RID_BUILTIN_TGMATH:
9364 {
9365 vec<c_expr_t, va_gc> *cexpr_list;
9366 location_t close_paren_loc;
9367
9368 c_parser_consume_token (parser);
9369 if (!c_parser_get_builtin_args (parser,
9370 "__builtin_tgmath",
9371 &cexpr_list, false,
9372 &close_paren_loc))
9373 {
9374 expr.set_error ();
9375 break;
9376 }
9377
9378 if (vec_safe_length (cexpr_list) < 3)
9379 {
9380 error_at (loc, "too few arguments to %<__builtin_tgmath%>");
9381 expr.set_error ();
9382 break;
9383 }
9384
9385 unsigned int i;
9386 c_expr_t *p;
9387 FOR_EACH_VEC_ELT (*cexpr_list, i, p)
9388 *p = convert_lvalue_to_rvalue (loc, *p, true, true);
9389 unsigned int nargs = check_tgmath_function (&(*cexpr_list)[0], 1);
9390 if (nargs == 0)
9391 {
9392 expr.set_error ();
9393 break;
9394 }
9395 if (vec_safe_length (cexpr_list) < nargs)
9396 {
9397 error_at (loc, "too few arguments to %<__builtin_tgmath%>");
9398 expr.set_error ();
9399 break;
9400 }
9401 unsigned int num_functions = vec_safe_length (cexpr_list) - nargs;
9402 if (num_functions < 2)
9403 {
9404 error_at (loc, "too few arguments to %<__builtin_tgmath%>");
9405 expr.set_error ();
9406 break;
9407 }
9408
9409 /* The first NUM_FUNCTIONS expressions are the function
9410 pointers. The remaining NARGS expressions are the
9411 arguments that are to be passed to one of those
9412 functions, chosen following <tgmath.h> rules. */
9413 for (unsigned int j = 1; j < num_functions; j++)
9414 {
9415 unsigned int this_nargs
9416 = check_tgmath_function (&(*cexpr_list)[j], j + 1);
9417 if (this_nargs == 0)
9418 {
9419 expr.set_error ();
9420 goto out;
9421 }
9422 if (this_nargs != nargs)
9423 {
9424 error_at ((*cexpr_list)[j].get_location (),
9425 "argument %u of %<__builtin_tgmath%> has "
9426 "wrong number of arguments", j + 1);
9427 expr.set_error ();
9428 goto out;
9429 }
9430 }
9431
9432 /* The functions all have the same number of arguments.
9433 Determine whether arguments and return types vary in
9434 ways permitted for <tgmath.h> functions. */
9435 /* The first entry in each of these vectors is for the
9436 return type, subsequent entries for parameter
9437 types. */
9438 auto_vec<enum tgmath_parm_kind> parm_kind (nargs + 1);
9439 auto_vec<tree> parm_first (nargs + 1);
9440 auto_vec<bool> parm_complex (nargs + 1);
9441 auto_vec<bool> parm_varies (nargs + 1);
9442 tree first_type = TREE_TYPE (TREE_TYPE ((*cexpr_list)[0].value));
9443 tree first_ret = TYPE_MAIN_VARIANT (TREE_TYPE (first_type));
9444 parm_first.quick_push (first_ret);
9445 parm_complex.quick_push (TREE_CODE (first_ret) == COMPLEX_TYPE);
9446 parm_varies.quick_push (false);
9447 function_args_iterator iter;
9448 tree t;
9449 unsigned int argpos;
9450 FOREACH_FUNCTION_ARGS (first_type, t, iter)
9451 {
9452 if (t == void_type_node)
9453 break;
9454 parm_first.quick_push (TYPE_MAIN_VARIANT (t));
9455 parm_complex.quick_push (TREE_CODE (t) == COMPLEX_TYPE);
9456 parm_varies.quick_push (false);
9457 }
9458 for (unsigned int j = 1; j < num_functions; j++)
9459 {
9460 tree type = TREE_TYPE (TREE_TYPE ((*cexpr_list)[j].value));
9461 tree ret = TYPE_MAIN_VARIANT (TREE_TYPE (type));
9462 if (ret != parm_first[0])
9463 {
9464 parm_varies[0] = true;
9465 if (!SCALAR_FLOAT_TYPE_P (parm_first[0])
9466 && !COMPLEX_FLOAT_TYPE_P (parm_first[0]))
9467 {
9468 error_at ((*cexpr_list)[0].get_location (),
9469 "invalid type-generic return type for "
9470 "argument %u of %<__builtin_tgmath%>",
9471 1);
9472 expr.set_error ();
9473 goto out;
9474 }
9475 if (!SCALAR_FLOAT_TYPE_P (ret)
9476 && !COMPLEX_FLOAT_TYPE_P (ret))
9477 {
9478 error_at ((*cexpr_list)[j].get_location (),
9479 "invalid type-generic return type for "
9480 "argument %u of %<__builtin_tgmath%>",
9481 j + 1);
9482 expr.set_error ();
9483 goto out;
9484 }
9485 }
9486 if (TREE_CODE (ret) == COMPLEX_TYPE)
9487 parm_complex[0] = true;
9488 argpos = 1;
9489 FOREACH_FUNCTION_ARGS (type, t, iter)
9490 {
9491 if (t == void_type_node)
9492 break;
9493 t = TYPE_MAIN_VARIANT (t);
9494 if (t != parm_first[argpos])
9495 {
9496 parm_varies[argpos] = true;
9497 if (!SCALAR_FLOAT_TYPE_P (parm_first[argpos])
9498 && !COMPLEX_FLOAT_TYPE_P (parm_first[argpos]))
9499 {
9500 error_at ((*cexpr_list)[0].get_location (),
9501 "invalid type-generic type for "
9502 "argument %u of argument %u of "
9503 "%<__builtin_tgmath%>", argpos, 1);
9504 expr.set_error ();
9505 goto out;
9506 }
9507 if (!SCALAR_FLOAT_TYPE_P (t)
9508 && !COMPLEX_FLOAT_TYPE_P (t))
9509 {
9510 error_at ((*cexpr_list)[j].get_location (),
9511 "invalid type-generic type for "
9512 "argument %u of argument %u of "
9513 "%<__builtin_tgmath%>", argpos, j + 1);
9514 expr.set_error ();
9515 goto out;
9516 }
9517 }
9518 if (TREE_CODE (t) == COMPLEX_TYPE)
9519 parm_complex[argpos] = true;
9520 argpos++;
9521 }
9522 }
9523 enum tgmath_parm_kind max_variation = tgmath_fixed;
9524 for (unsigned int j = 0; j <= nargs; j++)
9525 {
9526 enum tgmath_parm_kind this_kind;
9527 if (parm_varies[j])
9528 {
9529 if (parm_complex[j])
9530 max_variation = this_kind = tgmath_complex;
9531 else
9532 {
9533 this_kind = tgmath_real;
9534 if (max_variation != tgmath_complex)
9535 max_variation = tgmath_real;
9536 }
9537 }
9538 else
9539 this_kind = tgmath_fixed;
9540 parm_kind.quick_push (this_kind);
9541 }
9542 if (max_variation == tgmath_fixed)
9543 {
9544 error_at (loc, "function arguments of %<__builtin_tgmath%> "
9545 "all have the same type");
9546 expr.set_error ();
9547 break;
9548 }
9549
9550 /* Identify a parameter (not the return type) that varies,
9551 including with complex types if any variation includes
9552 complex types; there must be at least one such
9553 parameter. */
9554 unsigned int tgarg = 0;
9555 for (unsigned int j = 1; j <= nargs; j++)
9556 if (parm_kind[j] == max_variation)
9557 {
9558 tgarg = j;
9559 break;
9560 }
9561 if (tgarg == 0)
9562 {
9563 error_at (loc, "function arguments of %<__builtin_tgmath%> "
9564 "lack type-generic parameter");
9565 expr.set_error ();
9566 break;
9567 }
9568
9569 /* Determine the type of the relevant parameter for each
9570 function. */
9571 auto_vec<tree> tg_type (num_functions);
9572 for (unsigned int j = 0; j < num_functions; j++)
9573 {
9574 tree type = TREE_TYPE (TREE_TYPE ((*cexpr_list)[j].value));
9575 argpos = 1;
9576 FOREACH_FUNCTION_ARGS (type, t, iter)
9577 {
9578 if (argpos == tgarg)
9579 {
9580 tg_type.quick_push (TYPE_MAIN_VARIANT (t));
9581 break;
9582 }
9583 argpos++;
9584 }
9585 }
9586
9587 /* Verify that the corresponding types are different for
9588 all the listed functions. Also determine whether all
9589 the types are complex, whether all the types are
9590 standard or binary, and whether all the types are
9591 decimal. */
9592 bool all_complex = true;
9593 bool all_binary = true;
9594 bool all_decimal = true;
9595 hash_set<tree> tg_types;
9596 FOR_EACH_VEC_ELT (tg_type, i, t)
9597 {
9598 if (TREE_CODE (t) == COMPLEX_TYPE)
9599 all_decimal = false;
9600 else
9601 {
9602 all_complex = false;
9603 if (DECIMAL_FLOAT_TYPE_P (t))
9604 all_binary = false;
9605 else
9606 all_decimal = false;
9607 }
9608 if (tg_types.add (t))
9609 {
9610 error_at ((*cexpr_list)[i].get_location (),
9611 "duplicate type-generic parameter type for "
9612 "function argument %u of %<__builtin_tgmath%>",
9613 i + 1);
9614 expr.set_error ();
9615 goto out;
9616 }
9617 }
9618
9619 /* Verify that other parameters and the return type whose
9620 types vary have their types varying in the correct
9621 way. */
9622 for (unsigned int j = 0; j < num_functions; j++)
9623 {
9624 tree exp_type = tg_type[j];
9625 tree exp_real_type = exp_type;
9626 if (TREE_CODE (exp_type) == COMPLEX_TYPE)
9627 exp_real_type = TREE_TYPE (exp_type);
9628 tree type = TREE_TYPE (TREE_TYPE ((*cexpr_list)[j].value));
9629 tree ret = TYPE_MAIN_VARIANT (TREE_TYPE (type));
9630 if ((parm_kind[0] == tgmath_complex && ret != exp_type)
9631 || (parm_kind[0] == tgmath_real && ret != exp_real_type))
9632 {
9633 error_at ((*cexpr_list)[j].get_location (),
9634 "bad return type for function argument %u "
9635 "of %<__builtin_tgmath%>", j + 1);
9636 expr.set_error ();
9637 goto out;
9638 }
9639 argpos = 1;
9640 FOREACH_FUNCTION_ARGS (type, t, iter)
9641 {
9642 if (t == void_type_node)
9643 break;
9644 t = TYPE_MAIN_VARIANT (t);
9645 if ((parm_kind[argpos] == tgmath_complex
9646 && t != exp_type)
9647 || (parm_kind[argpos] == tgmath_real
9648 && t != exp_real_type))
9649 {
9650 error_at ((*cexpr_list)[j].get_location (),
9651 "bad type for argument %u of "
9652 "function argument %u of "
9653 "%<__builtin_tgmath%>", argpos, j + 1);
9654 expr.set_error ();
9655 goto out;
9656 }
9657 argpos++;
9658 }
9659 }
9660
9661 /* The functions listed are a valid set of functions for a
9662 <tgmath.h> macro to select between. Identify the
9663 matching function, if any. First, the argument types
9664 must be combined following <tgmath.h> rules. Integer
9665 types are treated as _Decimal64 if any type-generic
9666 argument is decimal, or if the only alternatives for
9667 type-generic arguments are of decimal types, and are
9668 otherwise treated as double (or _Complex double for
9669 complex integer types, or _Float64 or _Complex _Float64
9670 if all the return types are the same _FloatN or
9671 _FloatNx type). After that adjustment, types are
9672 combined following the usual arithmetic conversions.
9673 If the function only accepts complex arguments, a
9674 complex type is produced. */
9675 bool arg_complex = all_complex;
9676 bool arg_binary = all_binary;
9677 bool arg_int_decimal = all_decimal;
9678 for (unsigned int j = 1; j <= nargs; j++)
9679 {
9680 if (parm_kind[j] == tgmath_fixed)
9681 continue;
9682 c_expr_t *ce = &(*cexpr_list)[num_functions + j - 1];
9683 tree type = TREE_TYPE (ce->value);
9684 if (!INTEGRAL_TYPE_P (type)
9685 && !SCALAR_FLOAT_TYPE_P (type)
9686 && TREE_CODE (type) != COMPLEX_TYPE)
9687 {
9688 error_at (ce->get_location (),
9689 "invalid type of argument %u of type-generic "
9690 "function", j);
9691 expr.set_error ();
9692 goto out;
9693 }
9694 if (DECIMAL_FLOAT_TYPE_P (type))
9695 {
9696 arg_int_decimal = true;
9697 if (all_complex)
9698 {
9699 error_at (ce->get_location (),
9700 "decimal floating-point argument %u to "
9701 "complex-only type-generic function", j);
9702 expr.set_error ();
9703 goto out;
9704 }
9705 else if (all_binary)
9706 {
9707 error_at (ce->get_location (),
9708 "decimal floating-point argument %u to "
9709 "binary-only type-generic function", j);
9710 expr.set_error ();
9711 goto out;
9712 }
9713 else if (arg_complex)
9714 {
9715 error_at (ce->get_location (),
9716 "both complex and decimal floating-point "
9717 "arguments to type-generic function");
9718 expr.set_error ();
9719 goto out;
9720 }
9721 else if (arg_binary)
9722 {
9723 error_at (ce->get_location (),
9724 "both binary and decimal floating-point "
9725 "arguments to type-generic function");
9726 expr.set_error ();
9727 goto out;
9728 }
9729 }
9730 else if (TREE_CODE (type) == COMPLEX_TYPE)
9731 {
9732 arg_complex = true;
9733 if (COMPLEX_FLOAT_TYPE_P (type))
9734 arg_binary = true;
9735 if (all_decimal)
9736 {
9737 error_at (ce->get_location (),
9738 "complex argument %u to "
9739 "decimal-only type-generic function", j);
9740 expr.set_error ();
9741 goto out;
9742 }
9743 else if (arg_int_decimal)
9744 {
9745 error_at (ce->get_location (),
9746 "both complex and decimal floating-point "
9747 "arguments to type-generic function");
9748 expr.set_error ();
9749 goto out;
9750 }
9751 }
9752 else if (SCALAR_FLOAT_TYPE_P (type))
9753 {
9754 arg_binary = true;
9755 if (all_decimal)
9756 {
9757 error_at (ce->get_location (),
9758 "binary argument %u to "
9759 "decimal-only type-generic function", j);
9760 expr.set_error ();
9761 goto out;
9762 }
9763 else if (arg_int_decimal)
9764 {
9765 error_at (ce->get_location (),
9766 "both binary and decimal floating-point "
9767 "arguments to type-generic function");
9768 expr.set_error ();
9769 goto out;
9770 }
9771 }
9772 }
9773 /* For a macro rounding its result to a narrower type, map
9774 integer types to _Float64 not double if the return type
9775 is a _FloatN or _FloatNx type. */
9776 bool arg_int_float64 = false;
9777 if (parm_kind[0] == tgmath_fixed
9778 && SCALAR_FLOAT_TYPE_P (parm_first[0])
9779 && float64_type_node != NULL_TREE)
9780 for (unsigned int j = 0; j < NUM_FLOATN_NX_TYPES; j++)
9781 if (parm_first[0] == FLOATN_TYPE_NODE (j))
9782 {
9783 arg_int_float64 = true;
9784 break;
9785 }
9786 tree arg_real = NULL_TREE;
9787 for (unsigned int j = 1; j <= nargs; j++)
9788 {
9789 if (parm_kind[j] == tgmath_fixed)
9790 continue;
9791 c_expr_t *ce = &(*cexpr_list)[num_functions + j - 1];
9792 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (ce->value));
9793 if (TREE_CODE (type) == COMPLEX_TYPE)
9794 type = TREE_TYPE (type);
9795 if (INTEGRAL_TYPE_P (type))
9796 type = (arg_int_decimal
9797 ? dfloat64_type_node
9798 : arg_int_float64
9799 ? float64_type_node
9800 : double_type_node);
9801 if (arg_real == NULL_TREE)
9802 arg_real = type;
9803 else
9804 arg_real = common_type (arg_real, type);
9805 if (arg_real == error_mark_node)
9806 {
9807 expr.set_error ();
9808 goto out;
9809 }
9810 }
9811 tree arg_type = (arg_complex
9812 ? build_complex_type (arg_real)
9813 : arg_real);
9814
9815 /* Look for a function to call with type-generic parameter
9816 type ARG_TYPE. */
9817 c_expr_t *fn = NULL;
9818 for (unsigned int j = 0; j < num_functions; j++)
9819 {
9820 if (tg_type[j] == arg_type)
9821 {
9822 fn = &(*cexpr_list)[j];
9823 break;
9824 }
9825 }
9826 if (fn == NULL
9827 && parm_kind[0] == tgmath_fixed
9828 && SCALAR_FLOAT_TYPE_P (parm_first[0]))
9829 {
9830 /* Presume this is a macro that rounds its result to a
9831 narrower type, and look for the first function with
9832 at least the range and precision of the argument
9833 type. */
9834 for (unsigned int j = 0; j < num_functions; j++)
9835 {
9836 if (arg_complex
9837 != (TREE_CODE (tg_type[j]) == COMPLEX_TYPE))
9838 continue;
9839 tree real_tg_type = (arg_complex
9840 ? TREE_TYPE (tg_type[j])
9841 : tg_type[j]);
9842 if (DECIMAL_FLOAT_TYPE_P (arg_real)
9843 != DECIMAL_FLOAT_TYPE_P (real_tg_type))
9844 continue;
9845 scalar_float_mode arg_mode
9846 = SCALAR_FLOAT_TYPE_MODE (arg_real);
9847 scalar_float_mode tg_mode
9848 = SCALAR_FLOAT_TYPE_MODE (real_tg_type);
9849 const real_format *arg_fmt = REAL_MODE_FORMAT (arg_mode);
9850 const real_format *tg_fmt = REAL_MODE_FORMAT (tg_mode);
9851 if (arg_fmt->b == tg_fmt->b
9852 && arg_fmt->p <= tg_fmt->p
9853 && arg_fmt->emax <= tg_fmt->emax
9854 && (arg_fmt->emin - arg_fmt->p
9855 >= tg_fmt->emin - tg_fmt->p))
9856 {
9857 fn = &(*cexpr_list)[j];
9858 break;
9859 }
9860 }
9861 }
9862 if (fn == NULL)
9863 {
9864 error_at (loc, "no matching function for type-generic call");
9865 expr.set_error ();
9866 break;
9867 }
9868
9869 /* Construct a call to FN. */
9870 vec<tree, va_gc> *args;
9871 vec_alloc (args, nargs);
9872 vec<tree, va_gc> *origtypes;
9873 vec_alloc (origtypes, nargs);
9874 auto_vec<location_t> arg_loc (nargs);
9875 for (unsigned int j = 0; j < nargs; j++)
9876 {
9877 c_expr_t *ce = &(*cexpr_list)[num_functions + j];
9878 args->quick_push (ce->value);
9879 arg_loc.quick_push (ce->get_location ());
9880 origtypes->quick_push (ce->original_type);
9881 }
9882 expr.value = c_build_function_call_vec (loc, arg_loc, fn->value,
9883 args, origtypes);
9884 set_c_expr_source_range (&expr, loc, close_paren_loc);
9885 break;
9886 }
9887 case RID_BUILTIN_CALL_WITH_STATIC_CHAIN:
9888 {
9889 vec<c_expr_t, va_gc> *cexpr_list;
9890 c_expr_t *e2_p;
9891 tree chain_value;
9892 location_t close_paren_loc;
9893
9894 c_parser_consume_token (parser);
9895 if (!c_parser_get_builtin_args (parser,
9896 "__builtin_call_with_static_chain",
9897 &cexpr_list, false,
9898 &close_paren_loc))
9899 {
9900 expr.set_error ();
9901 break;
9902 }
9903 if (vec_safe_length (cexpr_list) != 2)
9904 {
9905 error_at (loc, "wrong number of arguments to "
9906 "%<__builtin_call_with_static_chain%>");
9907 expr.set_error ();
9908 break;
9909 }
9910
9911 expr = (*cexpr_list)[0];
9912 e2_p = &(*cexpr_list)[1];
9913 *e2_p = convert_lvalue_to_rvalue (loc, *e2_p, true, true);
9914 chain_value = e2_p->value;
9915 mark_exp_read (chain_value);
9916
9917 if (TREE_CODE (expr.value) != CALL_EXPR)
9918 error_at (loc, "first argument to "
9919 "%<__builtin_call_with_static_chain%> "
9920 "must be a call expression");
9921 else if (TREE_CODE (TREE_TYPE (chain_value)) != POINTER_TYPE)
9922 error_at (loc, "second argument to "
9923 "%<__builtin_call_with_static_chain%> "
9924 "must be a pointer type");
9925 else
9926 CALL_EXPR_STATIC_CHAIN (expr.value) = chain_value;
9927 set_c_expr_source_range (&expr, loc, close_paren_loc);
9928 break;
9929 }
9930 case RID_BUILTIN_COMPLEX:
9931 {
9932 vec<c_expr_t, va_gc> *cexpr_list;
9933 c_expr_t *e1_p, *e2_p;
9934 location_t close_paren_loc;
9935
9936 c_parser_consume_token (parser);
9937 if (!c_parser_get_builtin_args (parser,
9938 "__builtin_complex",
9939 &cexpr_list, false,
9940 &close_paren_loc))
9941 {
9942 expr.set_error ();
9943 break;
9944 }
9945
9946 if (vec_safe_length (cexpr_list) != 2)
9947 {
9948 error_at (loc, "wrong number of arguments to "
9949 "%<__builtin_complex%>");
9950 expr.set_error ();
9951 break;
9952 }
9953
9954 e1_p = &(*cexpr_list)[0];
9955 e2_p = &(*cexpr_list)[1];
9956
9957 *e1_p = convert_lvalue_to_rvalue (loc, *e1_p, true, true);
9958 if (TREE_CODE (e1_p->value) == EXCESS_PRECISION_EXPR)
9959 e1_p->value = convert (TREE_TYPE (e1_p->value),
9960 TREE_OPERAND (e1_p->value, 0));
9961 *e2_p = convert_lvalue_to_rvalue (loc, *e2_p, true, true);
9962 if (TREE_CODE (e2_p->value) == EXCESS_PRECISION_EXPR)
9963 e2_p->value = convert (TREE_TYPE (e2_p->value),
9964 TREE_OPERAND (e2_p->value, 0));
9965 if (!SCALAR_FLOAT_TYPE_P (TREE_TYPE (e1_p->value))
9966 || DECIMAL_FLOAT_TYPE_P (TREE_TYPE (e1_p->value))
9967 || !SCALAR_FLOAT_TYPE_P (TREE_TYPE (e2_p->value))
9968 || DECIMAL_FLOAT_TYPE_P (TREE_TYPE (e2_p->value)))
9969 {
9970 error_at (loc, "%<__builtin_complex%> operand "
9971 "not of real binary floating-point type");
9972 expr.set_error ();
9973 break;
9974 }
9975 if (TYPE_MAIN_VARIANT (TREE_TYPE (e1_p->value))
9976 != TYPE_MAIN_VARIANT (TREE_TYPE (e2_p->value)))
9977 {
9978 error_at (loc,
9979 "%<__builtin_complex%> operands of different types");
9980 expr.set_error ();
9981 break;
9982 }
9983 pedwarn_c90 (loc, OPT_Wpedantic,
9984 "ISO C90 does not support complex types");
9985 expr.value = build2_loc (loc, COMPLEX_EXPR,
9986 build_complex_type
9987 (TYPE_MAIN_VARIANT
9988 (TREE_TYPE (e1_p->value))),
9989 e1_p->value, e2_p->value);
9990 set_c_expr_source_range (&expr, loc, close_paren_loc);
9991 break;
9992 }
9993 case RID_BUILTIN_SHUFFLE:
9994 {
9995 vec<c_expr_t, va_gc> *cexpr_list;
9996 unsigned int i;
9997 c_expr_t *p;
9998 location_t close_paren_loc;
9999
10000 c_parser_consume_token (parser);
10001 if (!c_parser_get_builtin_args (parser,
10002 "__builtin_shuffle",
10003 &cexpr_list, false,
10004 &close_paren_loc))
10005 {
10006 expr.set_error ();
10007 break;
10008 }
10009
10010 FOR_EACH_VEC_SAFE_ELT (cexpr_list, i, p)
10011 *p = convert_lvalue_to_rvalue (loc, *p, true, true);
10012
10013 if (vec_safe_length (cexpr_list) == 2)
10014 expr.value = c_build_vec_perm_expr (loc, (*cexpr_list)[0].value,
10015 NULL_TREE,
10016 (*cexpr_list)[1].value);
10017
10018 else if (vec_safe_length (cexpr_list) == 3)
10019 expr.value = c_build_vec_perm_expr (loc, (*cexpr_list)[0].value,
10020 (*cexpr_list)[1].value,
10021 (*cexpr_list)[2].value);
10022 else
10023 {
10024 error_at (loc, "wrong number of arguments to "
10025 "%<__builtin_shuffle%>");
10026 expr.set_error ();
10027 }
10028 set_c_expr_source_range (&expr, loc, close_paren_loc);
10029 break;
10030 }
10031 case RID_BUILTIN_CONVERTVECTOR:
10032 {
10033 location_t start_loc = loc;
10034 c_parser_consume_token (parser);
10035 matching_parens parens;
10036 if (!parens.require_open (parser))
10037 {
10038 expr.set_error ();
10039 break;
10040 }
10041 e1 = c_parser_expr_no_commas (parser, NULL);
10042 mark_exp_read (e1.value);
10043 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
10044 {
10045 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
10046 expr.set_error ();
10047 break;
10048 }
10049 loc = c_parser_peek_token (parser)->location;
10050 t1 = c_parser_type_name (parser);
10051 location_t end_loc = c_parser_peek_token (parser)->get_finish ();
10052 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
10053 "expected %<)%>");
10054 if (t1 == NULL)
10055 expr.set_error ();
10056 else
10057 {
10058 tree type_expr = NULL_TREE;
10059 expr.value = c_build_vec_convert (start_loc, e1.value, loc,
10060 groktypename (t1, &type_expr,
10061 NULL));
10062 set_c_expr_source_range (&expr, start_loc, end_loc);
10063 }
10064 }
10065 break;
10066 case RID_AT_SELECTOR:
10067 {
10068 gcc_assert (c_dialect_objc ());
10069 c_parser_consume_token (parser);
10070 matching_parens parens;
10071 if (!parens.require_open (parser))
10072 {
10073 expr.set_error ();
10074 break;
10075 }
10076 tree sel = c_parser_objc_selector_arg (parser);
10077 location_t close_loc = c_parser_peek_token (parser)->location;
10078 parens.skip_until_found_close (parser);
10079 expr.value = objc_build_selector_expr (loc, sel);
10080 set_c_expr_source_range (&expr, loc, close_loc);
10081 }
10082 break;
10083 case RID_AT_PROTOCOL:
10084 {
10085 gcc_assert (c_dialect_objc ());
10086 c_parser_consume_token (parser);
10087 matching_parens parens;
10088 if (!parens.require_open (parser))
10089 {
10090 expr.set_error ();
10091 break;
10092 }
10093 if (c_parser_next_token_is_not (parser, CPP_NAME))
10094 {
10095 c_parser_error (parser, "expected identifier");
10096 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
10097 expr.set_error ();
10098 break;
10099 }
10100 tree id = c_parser_peek_token (parser)->value;
10101 c_parser_consume_token (parser);
10102 location_t close_loc = c_parser_peek_token (parser)->location;
10103 parens.skip_until_found_close (parser);
10104 expr.value = objc_build_protocol_expr (id);
10105 set_c_expr_source_range (&expr, loc, close_loc);
10106 }
10107 break;
10108 case RID_AT_ENCODE:
10109 {
10110 /* Extension to support C-structures in the archiver. */
10111 gcc_assert (c_dialect_objc ());
10112 c_parser_consume_token (parser);
10113 matching_parens parens;
10114 if (!parens.require_open (parser))
10115 {
10116 expr.set_error ();
10117 break;
10118 }
10119 t1 = c_parser_type_name (parser);
10120 if (t1 == NULL)
10121 {
10122 expr.set_error ();
10123 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
10124 break;
10125 }
10126 location_t close_loc = c_parser_peek_token (parser)->location;
10127 parens.skip_until_found_close (parser);
10128 tree type = groktypename (t1, NULL, NULL);
10129 expr.value = objc_build_encode_expr (type);
10130 set_c_expr_source_range (&expr, loc, close_loc);
10131 }
10132 break;
10133 case RID_GENERIC:
10134 expr = c_parser_generic_selection (parser);
10135 break;
10136 default:
10137 c_parser_error (parser, "expected expression");
10138 expr.set_error ();
10139 break;
10140 }
10141 break;
10142 case CPP_OPEN_SQUARE:
10143 if (c_dialect_objc ())
10144 {
10145 tree receiver, args;
10146 c_parser_consume_token (parser);
10147 receiver = c_parser_objc_receiver (parser);
10148 args = c_parser_objc_message_args (parser);
10149 location_t close_loc = c_parser_peek_token (parser)->location;
10150 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
10151 "expected %<]%>");
10152 expr.value = objc_build_message_expr (receiver, args);
10153 set_c_expr_source_range (&expr, loc, close_loc);
10154 break;
10155 }
10156 /* Else fall through to report error. */
10157 /* FALLTHRU */
10158 default:
10159 c_parser_error (parser, "expected expression");
10160 expr.set_error ();
10161 break;
10162 }
10163 out:
10164 return c_parser_postfix_expression_after_primary
10165 (parser, EXPR_LOC_OR_LOC (expr.value, loc), expr);
10166 }
10167
10168 /* Parse a postfix expression after a parenthesized type name: the
10169 brace-enclosed initializer of a compound literal, possibly followed
10170 by some postfix operators. This is separate because it is not
10171 possible to tell until after the type name whether a cast
10172 expression has a cast or a compound literal, or whether the operand
10173 of sizeof is a parenthesized type name or starts with a compound
10174 literal. TYPE_LOC is the location where TYPE_NAME starts--the
10175 location of the first token after the parentheses around the type
10176 name. */
10177
10178 static struct c_expr
10179 c_parser_postfix_expression_after_paren_type (c_parser *parser,
10180 struct c_type_name *type_name,
10181 location_t type_loc)
10182 {
10183 tree type;
10184 struct c_expr init;
10185 bool non_const;
10186 struct c_expr expr;
10187 location_t start_loc;
10188 tree type_expr = NULL_TREE;
10189 bool type_expr_const = true;
10190 check_compound_literal_type (type_loc, type_name);
10191 rich_location richloc (line_table, type_loc);
10192 start_init (NULL_TREE, NULL, 0, &richloc);
10193 type = groktypename (type_name, &type_expr, &type_expr_const);
10194 start_loc = c_parser_peek_token (parser)->location;
10195 if (type != error_mark_node && C_TYPE_VARIABLE_SIZE (type))
10196 {
10197 error_at (type_loc, "compound literal has variable size");
10198 type = error_mark_node;
10199 }
10200 init = c_parser_braced_init (parser, type, false, NULL);
10201 finish_init ();
10202 maybe_warn_string_init (type_loc, type, init);
10203
10204 if (type != error_mark_node
10205 && !ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (type))
10206 && current_function_decl)
10207 {
10208 error ("compound literal qualified by address-space qualifier");
10209 type = error_mark_node;
10210 }
10211
10212 pedwarn_c90 (start_loc, OPT_Wpedantic, "ISO C90 forbids compound literals");
10213 non_const = ((init.value && TREE_CODE (init.value) == CONSTRUCTOR)
10214 ? CONSTRUCTOR_NON_CONST (init.value)
10215 : init.original_code == C_MAYBE_CONST_EXPR);
10216 non_const |= !type_expr_const;
10217 unsigned int alignas_align = 0;
10218 if (type != error_mark_node
10219 && type_name->specs->align_log != -1)
10220 {
10221 alignas_align = 1U << type_name->specs->align_log;
10222 if (alignas_align < min_align_of_type (type))
10223 {
10224 error_at (type_name->specs->locations[cdw_alignas],
10225 "%<_Alignas%> specifiers cannot reduce "
10226 "alignment of compound literal");
10227 alignas_align = 0;
10228 }
10229 }
10230 expr.value = build_compound_literal (start_loc, type, init.value, non_const,
10231 alignas_align);
10232 set_c_expr_source_range (&expr, init.src_range);
10233 expr.original_code = ERROR_MARK;
10234 expr.original_type = NULL;
10235 if (type != error_mark_node
10236 && expr.value != error_mark_node
10237 && type_expr)
10238 {
10239 if (TREE_CODE (expr.value) == C_MAYBE_CONST_EXPR)
10240 {
10241 gcc_assert (C_MAYBE_CONST_EXPR_PRE (expr.value) == NULL_TREE);
10242 C_MAYBE_CONST_EXPR_PRE (expr.value) = type_expr;
10243 }
10244 else
10245 {
10246 gcc_assert (!non_const);
10247 expr.value = build2 (C_MAYBE_CONST_EXPR, type,
10248 type_expr, expr.value);
10249 }
10250 }
10251 return c_parser_postfix_expression_after_primary (parser, start_loc, expr);
10252 }
10253
10254 /* Callback function for sizeof_pointer_memaccess_warning to compare
10255 types. */
10256
10257 static bool
10258 sizeof_ptr_memacc_comptypes (tree type1, tree type2)
10259 {
10260 return comptypes (type1, type2) == 1;
10261 }
10262
10263 /* Warn for patterns where abs-like function appears to be used incorrectly,
10264 gracefully ignore any non-abs-like function. The warning location should
10265 be LOC. FNDECL is the declaration of called function, it must be a
10266 BUILT_IN_NORMAL function. ARG is the first and only argument of the
10267 call. */
10268
10269 static void
10270 warn_for_abs (location_t loc, tree fndecl, tree arg)
10271 {
10272 /* Avoid warning in unreachable subexpressions. */
10273 if (c_inhibit_evaluation_warnings)
10274 return;
10275
10276 tree atype = TREE_TYPE (arg);
10277
10278 /* Casts from pointers (and thus arrays and fndecls) will generate
10279 -Wint-conversion warnings. Most other wrong types hopefully lead to type
10280 mismatch errors. TODO: Think about what to do with FIXED_POINT_TYPE_P
10281 types and possibly other exotic types. */
10282 if (!INTEGRAL_TYPE_P (atype)
10283 && !SCALAR_FLOAT_TYPE_P (atype)
10284 && TREE_CODE (atype) != COMPLEX_TYPE)
10285 return;
10286
10287 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
10288
10289 switch (fcode)
10290 {
10291 case BUILT_IN_ABS:
10292 case BUILT_IN_LABS:
10293 case BUILT_IN_LLABS:
10294 case BUILT_IN_IMAXABS:
10295 if (!INTEGRAL_TYPE_P (atype))
10296 {
10297 if (SCALAR_FLOAT_TYPE_P (atype))
10298 warning_at (loc, OPT_Wabsolute_value,
10299 "using integer absolute value function %qD when "
10300 "argument is of floating-point type %qT",
10301 fndecl, atype);
10302 else if (TREE_CODE (atype) == COMPLEX_TYPE)
10303 warning_at (loc, OPT_Wabsolute_value,
10304 "using integer absolute value function %qD when "
10305 "argument is of complex type %qT", fndecl, atype);
10306 else
10307 gcc_unreachable ();
10308 return;
10309 }
10310 if (TYPE_UNSIGNED (atype))
10311 warning_at (loc, OPT_Wabsolute_value,
10312 "taking the absolute value of unsigned type %qT "
10313 "has no effect", atype);
10314 break;
10315
10316 CASE_FLT_FN (BUILT_IN_FABS):
10317 CASE_FLT_FN_FLOATN_NX (BUILT_IN_FABS):
10318 if (!SCALAR_FLOAT_TYPE_P (atype)
10319 || DECIMAL_FLOAT_MODE_P (TYPE_MODE (atype)))
10320 {
10321 if (INTEGRAL_TYPE_P (atype))
10322 warning_at (loc, OPT_Wabsolute_value,
10323 "using floating-point absolute value function %qD "
10324 "when argument is of integer type %qT", fndecl, atype);
10325 else if (DECIMAL_FLOAT_TYPE_P (atype))
10326 warning_at (loc, OPT_Wabsolute_value,
10327 "using floating-point absolute value function %qD "
10328 "when argument is of decimal floating-point type %qT",
10329 fndecl, atype);
10330 else if (TREE_CODE (atype) == COMPLEX_TYPE)
10331 warning_at (loc, OPT_Wabsolute_value,
10332 "using floating-point absolute value function %qD when "
10333 "argument is of complex type %qT", fndecl, atype);
10334 else
10335 gcc_unreachable ();
10336 return;
10337 }
10338 break;
10339
10340 CASE_FLT_FN (BUILT_IN_CABS):
10341 if (TREE_CODE (atype) != COMPLEX_TYPE)
10342 {
10343 if (INTEGRAL_TYPE_P (atype))
10344 warning_at (loc, OPT_Wabsolute_value,
10345 "using complex absolute value function %qD when "
10346 "argument is of integer type %qT", fndecl, atype);
10347 else if (SCALAR_FLOAT_TYPE_P (atype))
10348 warning_at (loc, OPT_Wabsolute_value,
10349 "using complex absolute value function %qD when "
10350 "argument is of floating-point type %qT",
10351 fndecl, atype);
10352 else
10353 gcc_unreachable ();
10354
10355 return;
10356 }
10357 break;
10358
10359 case BUILT_IN_FABSD32:
10360 case BUILT_IN_FABSD64:
10361 case BUILT_IN_FABSD128:
10362 if (!DECIMAL_FLOAT_TYPE_P (atype))
10363 {
10364 if (INTEGRAL_TYPE_P (atype))
10365 warning_at (loc, OPT_Wabsolute_value,
10366 "using decimal floating-point absolute value "
10367 "function %qD when argument is of integer type %qT",
10368 fndecl, atype);
10369 else if (SCALAR_FLOAT_TYPE_P (atype))
10370 warning_at (loc, OPT_Wabsolute_value,
10371 "using decimal floating-point absolute value "
10372 "function %qD when argument is of floating-point "
10373 "type %qT", fndecl, atype);
10374 else if (TREE_CODE (atype) == COMPLEX_TYPE)
10375 warning_at (loc, OPT_Wabsolute_value,
10376 "using decimal floating-point absolute value "
10377 "function %qD when argument is of complex type %qT",
10378 fndecl, atype);
10379 else
10380 gcc_unreachable ();
10381 return;
10382 }
10383 break;
10384
10385 default:
10386 return;
10387 }
10388
10389 if (!TYPE_ARG_TYPES (TREE_TYPE (fndecl)))
10390 return;
10391
10392 tree ftype = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
10393 if (TREE_CODE (atype) == COMPLEX_TYPE)
10394 {
10395 gcc_assert (TREE_CODE (ftype) == COMPLEX_TYPE);
10396 atype = TREE_TYPE (atype);
10397 ftype = TREE_TYPE (ftype);
10398 }
10399
10400 if (TYPE_PRECISION (ftype) < TYPE_PRECISION (atype))
10401 warning_at (loc, OPT_Wabsolute_value,
10402 "absolute value function %qD given an argument of type %qT "
10403 "but has parameter of type %qT which may cause truncation "
10404 "of value", fndecl, atype, ftype);
10405 }
10406
10407
10408 /* Parse a postfix expression after the initial primary or compound
10409 literal; that is, parse a series of postfix operators.
10410
10411 EXPR_LOC is the location of the primary expression. */
10412
10413 static struct c_expr
10414 c_parser_postfix_expression_after_primary (c_parser *parser,
10415 location_t expr_loc,
10416 struct c_expr expr)
10417 {
10418 struct c_expr orig_expr;
10419 tree ident, idx;
10420 location_t sizeof_arg_loc[3], comp_loc;
10421 tree sizeof_arg[3];
10422 unsigned int literal_zero_mask;
10423 unsigned int i;
10424 vec<tree, va_gc> *exprlist;
10425 vec<tree, va_gc> *origtypes = NULL;
10426 vec<location_t> arg_loc = vNULL;
10427 location_t start;
10428 location_t finish;
10429
10430 while (true)
10431 {
10432 location_t op_loc = c_parser_peek_token (parser)->location;
10433 switch (c_parser_peek_token (parser)->type)
10434 {
10435 case CPP_OPEN_SQUARE:
10436 /* Array reference. */
10437 c_parser_consume_token (parser);
10438 idx = c_parser_expression (parser).value;
10439 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
10440 "expected %<]%>");
10441 start = expr.get_start ();
10442 finish = parser->tokens_buf[0].location;
10443 expr.value = build_array_ref (op_loc, expr.value, idx);
10444 set_c_expr_source_range (&expr, start, finish);
10445 expr.original_code = ERROR_MARK;
10446 expr.original_type = NULL;
10447 break;
10448 case CPP_OPEN_PAREN:
10449 /* Function call. */
10450 c_parser_consume_token (parser);
10451 for (i = 0; i < 3; i++)
10452 {
10453 sizeof_arg[i] = NULL_TREE;
10454 sizeof_arg_loc[i] = UNKNOWN_LOCATION;
10455 }
10456 literal_zero_mask = 0;
10457 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
10458 exprlist = NULL;
10459 else
10460 exprlist = c_parser_expr_list (parser, true, false, &origtypes,
10461 sizeof_arg_loc, sizeof_arg,
10462 &arg_loc, &literal_zero_mask);
10463 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
10464 "expected %<)%>");
10465 orig_expr = expr;
10466 mark_exp_read (expr.value);
10467 if (warn_sizeof_pointer_memaccess)
10468 sizeof_pointer_memaccess_warning (sizeof_arg_loc,
10469 expr.value, exprlist,
10470 sizeof_arg,
10471 sizeof_ptr_memacc_comptypes);
10472 if (TREE_CODE (expr.value) == FUNCTION_DECL)
10473 {
10474 if (fndecl_built_in_p (expr.value, BUILT_IN_MEMSET)
10475 && vec_safe_length (exprlist) == 3)
10476 {
10477 tree arg0 = (*exprlist)[0];
10478 tree arg2 = (*exprlist)[2];
10479 warn_for_memset (expr_loc, arg0, arg2, literal_zero_mask);
10480 }
10481 if (warn_absolute_value
10482 && fndecl_built_in_p (expr.value, BUILT_IN_NORMAL)
10483 && vec_safe_length (exprlist) == 1)
10484 warn_for_abs (expr_loc, expr.value, (*exprlist)[0]);
10485 }
10486
10487 start = expr.get_start ();
10488 finish = parser->tokens_buf[0].get_finish ();
10489 expr.value
10490 = c_build_function_call_vec (expr_loc, arg_loc, expr.value,
10491 exprlist, origtypes);
10492 set_c_expr_source_range (&expr, start, finish);
10493
10494 expr.original_code = ERROR_MARK;
10495 if (TREE_CODE (expr.value) == INTEGER_CST
10496 && TREE_CODE (orig_expr.value) == FUNCTION_DECL
10497 && fndecl_built_in_p (orig_expr.value, BUILT_IN_CONSTANT_P))
10498 expr.original_code = C_MAYBE_CONST_EXPR;
10499 expr.original_type = NULL;
10500 if (exprlist)
10501 {
10502 release_tree_vector (exprlist);
10503 release_tree_vector (origtypes);
10504 }
10505 arg_loc.release ();
10506 break;
10507 case CPP_DOT:
10508 /* Structure element reference. */
10509 c_parser_consume_token (parser);
10510 expr = default_function_array_conversion (expr_loc, expr);
10511 if (c_parser_next_token_is (parser, CPP_NAME))
10512 {
10513 c_token *comp_tok = c_parser_peek_token (parser);
10514 ident = comp_tok->value;
10515 comp_loc = comp_tok->location;
10516 }
10517 else
10518 {
10519 c_parser_error (parser, "expected identifier");
10520 expr.set_error ();
10521 expr.original_code = ERROR_MARK;
10522 expr.original_type = NULL;
10523 return expr;
10524 }
10525 start = expr.get_start ();
10526 finish = c_parser_peek_token (parser)->get_finish ();
10527 c_parser_consume_token (parser);
10528 expr.value = build_component_ref (op_loc, expr.value, ident,
10529 comp_loc);
10530 set_c_expr_source_range (&expr, start, finish);
10531 expr.original_code = ERROR_MARK;
10532 if (TREE_CODE (expr.value) != COMPONENT_REF)
10533 expr.original_type = NULL;
10534 else
10535 {
10536 /* Remember the original type of a bitfield. */
10537 tree field = TREE_OPERAND (expr.value, 1);
10538 if (TREE_CODE (field) != FIELD_DECL)
10539 expr.original_type = NULL;
10540 else
10541 expr.original_type = DECL_BIT_FIELD_TYPE (field);
10542 }
10543 break;
10544 case CPP_DEREF:
10545 /* Structure element reference. */
10546 c_parser_consume_token (parser);
10547 expr = convert_lvalue_to_rvalue (expr_loc, expr, true, false);
10548 if (c_parser_next_token_is (parser, CPP_NAME))
10549 {
10550 c_token *comp_tok = c_parser_peek_token (parser);
10551 ident = comp_tok->value;
10552 comp_loc = comp_tok->location;
10553 }
10554 else
10555 {
10556 c_parser_error (parser, "expected identifier");
10557 expr.set_error ();
10558 expr.original_code = ERROR_MARK;
10559 expr.original_type = NULL;
10560 return expr;
10561 }
10562 start = expr.get_start ();
10563 finish = c_parser_peek_token (parser)->get_finish ();
10564 c_parser_consume_token (parser);
10565 expr.value = build_component_ref (op_loc,
10566 build_indirect_ref (op_loc,
10567 expr.value,
10568 RO_ARROW),
10569 ident, comp_loc);
10570 set_c_expr_source_range (&expr, start, finish);
10571 expr.original_code = ERROR_MARK;
10572 if (TREE_CODE (expr.value) != COMPONENT_REF)
10573 expr.original_type = NULL;
10574 else
10575 {
10576 /* Remember the original type of a bitfield. */
10577 tree field = TREE_OPERAND (expr.value, 1);
10578 if (TREE_CODE (field) != FIELD_DECL)
10579 expr.original_type = NULL;
10580 else
10581 expr.original_type = DECL_BIT_FIELD_TYPE (field);
10582 }
10583 break;
10584 case CPP_PLUS_PLUS:
10585 /* Postincrement. */
10586 start = expr.get_start ();
10587 finish = c_parser_peek_token (parser)->get_finish ();
10588 c_parser_consume_token (parser);
10589 expr = default_function_array_read_conversion (expr_loc, expr);
10590 expr.value = build_unary_op (op_loc, POSTINCREMENT_EXPR,
10591 expr.value, false);
10592 set_c_expr_source_range (&expr, start, finish);
10593 expr.original_code = ERROR_MARK;
10594 expr.original_type = NULL;
10595 break;
10596 case CPP_MINUS_MINUS:
10597 /* Postdecrement. */
10598 start = expr.get_start ();
10599 finish = c_parser_peek_token (parser)->get_finish ();
10600 c_parser_consume_token (parser);
10601 expr = default_function_array_read_conversion (expr_loc, expr);
10602 expr.value = build_unary_op (op_loc, POSTDECREMENT_EXPR,
10603 expr.value, false);
10604 set_c_expr_source_range (&expr, start, finish);
10605 expr.original_code = ERROR_MARK;
10606 expr.original_type = NULL;
10607 break;
10608 default:
10609 return expr;
10610 }
10611 }
10612 }
10613
10614 /* Parse an expression (C90 6.3.17, C99 6.5.17, C11 6.5.17).
10615
10616 expression:
10617 assignment-expression
10618 expression , assignment-expression
10619 */
10620
10621 static struct c_expr
10622 c_parser_expression (c_parser *parser)
10623 {
10624 location_t tloc = c_parser_peek_token (parser)->location;
10625 struct c_expr expr;
10626 expr = c_parser_expr_no_commas (parser, NULL);
10627 if (c_parser_next_token_is (parser, CPP_COMMA))
10628 expr = convert_lvalue_to_rvalue (tloc, expr, true, false);
10629 while (c_parser_next_token_is (parser, CPP_COMMA))
10630 {
10631 struct c_expr next;
10632 tree lhsval;
10633 location_t loc = c_parser_peek_token (parser)->location;
10634 location_t expr_loc;
10635 c_parser_consume_token (parser);
10636 expr_loc = c_parser_peek_token (parser)->location;
10637 lhsval = expr.value;
10638 while (TREE_CODE (lhsval) == COMPOUND_EXPR)
10639 lhsval = TREE_OPERAND (lhsval, 1);
10640 if (DECL_P (lhsval) || handled_component_p (lhsval))
10641 mark_exp_read (lhsval);
10642 next = c_parser_expr_no_commas (parser, NULL);
10643 next = convert_lvalue_to_rvalue (expr_loc, next, true, false);
10644 expr.value = build_compound_expr (loc, expr.value, next.value);
10645 expr.original_code = COMPOUND_EXPR;
10646 expr.original_type = next.original_type;
10647 }
10648 return expr;
10649 }
10650
10651 /* Parse an expression and convert functions or arrays to pointers and
10652 lvalues to rvalues. */
10653
10654 static struct c_expr
10655 c_parser_expression_conv (c_parser *parser)
10656 {
10657 struct c_expr expr;
10658 location_t loc = c_parser_peek_token (parser)->location;
10659 expr = c_parser_expression (parser);
10660 expr = convert_lvalue_to_rvalue (loc, expr, true, false);
10661 return expr;
10662 }
10663
10664 /* Helper function of c_parser_expr_list. Check if IDXth (0 based)
10665 argument is a literal zero alone and if so, set it in literal_zero_mask. */
10666
10667 static inline void
10668 c_parser_check_literal_zero (c_parser *parser, unsigned *literal_zero_mask,
10669 unsigned int idx)
10670 {
10671 if (idx >= HOST_BITS_PER_INT)
10672 return;
10673
10674 c_token *tok = c_parser_peek_token (parser);
10675 switch (tok->type)
10676 {
10677 case CPP_NUMBER:
10678 case CPP_CHAR:
10679 case CPP_WCHAR:
10680 case CPP_CHAR16:
10681 case CPP_CHAR32:
10682 case CPP_UTF8CHAR:
10683 /* If a parameter is literal zero alone, remember it
10684 for -Wmemset-transposed-args warning. */
10685 if (integer_zerop (tok->value)
10686 && !TREE_OVERFLOW (tok->value)
10687 && (c_parser_peek_2nd_token (parser)->type == CPP_COMMA
10688 || c_parser_peek_2nd_token (parser)->type == CPP_CLOSE_PAREN))
10689 *literal_zero_mask |= 1U << idx;
10690 default:
10691 break;
10692 }
10693 }
10694
10695 /* Parse a non-empty list of expressions. If CONVERT_P, convert
10696 functions and arrays to pointers and lvalues to rvalues. If
10697 FOLD_P, fold the expressions. If LOCATIONS is non-NULL, save the
10698 locations of function arguments into this vector.
10699
10700 nonempty-expr-list:
10701 assignment-expression
10702 nonempty-expr-list , assignment-expression
10703 */
10704
10705 static vec<tree, va_gc> *
10706 c_parser_expr_list (c_parser *parser, bool convert_p, bool fold_p,
10707 vec<tree, va_gc> **p_orig_types,
10708 location_t *sizeof_arg_loc, tree *sizeof_arg,
10709 vec<location_t> *locations,
10710 unsigned int *literal_zero_mask)
10711 {
10712 vec<tree, va_gc> *ret;
10713 vec<tree, va_gc> *orig_types;
10714 struct c_expr expr;
10715 unsigned int idx = 0;
10716
10717 ret = make_tree_vector ();
10718 if (p_orig_types == NULL)
10719 orig_types = NULL;
10720 else
10721 orig_types = make_tree_vector ();
10722
10723 if (literal_zero_mask)
10724 c_parser_check_literal_zero (parser, literal_zero_mask, 0);
10725 expr = c_parser_expr_no_commas (parser, NULL);
10726 if (convert_p)
10727 expr = convert_lvalue_to_rvalue (expr.get_location (), expr, true, true);
10728 if (fold_p)
10729 expr.value = c_fully_fold (expr.value, false, NULL);
10730 ret->quick_push (expr.value);
10731 if (orig_types)
10732 orig_types->quick_push (expr.original_type);
10733 if (locations)
10734 locations->safe_push (expr.get_location ());
10735 if (sizeof_arg != NULL
10736 && expr.original_code == SIZEOF_EXPR)
10737 {
10738 sizeof_arg[0] = c_last_sizeof_arg;
10739 sizeof_arg_loc[0] = c_last_sizeof_loc;
10740 }
10741 while (c_parser_next_token_is (parser, CPP_COMMA))
10742 {
10743 c_parser_consume_token (parser);
10744 if (literal_zero_mask)
10745 c_parser_check_literal_zero (parser, literal_zero_mask, idx + 1);
10746 expr = c_parser_expr_no_commas (parser, NULL);
10747 if (convert_p)
10748 expr = convert_lvalue_to_rvalue (expr.get_location (), expr, true,
10749 true);
10750 if (fold_p)
10751 expr.value = c_fully_fold (expr.value, false, NULL);
10752 vec_safe_push (ret, expr.value);
10753 if (orig_types)
10754 vec_safe_push (orig_types, expr.original_type);
10755 if (locations)
10756 locations->safe_push (expr.get_location ());
10757 if (++idx < 3
10758 && sizeof_arg != NULL
10759 && expr.original_code == SIZEOF_EXPR)
10760 {
10761 sizeof_arg[idx] = c_last_sizeof_arg;
10762 sizeof_arg_loc[idx] = c_last_sizeof_loc;
10763 }
10764 }
10765 if (orig_types)
10766 *p_orig_types = orig_types;
10767 return ret;
10768 }
10769 \f
10770 /* Parse Objective-C-specific constructs. */
10771
10772 /* Parse an objc-class-definition.
10773
10774 objc-class-definition:
10775 @interface identifier objc-superclass[opt] objc-protocol-refs[opt]
10776 objc-class-instance-variables[opt] objc-methodprotolist @end
10777 @implementation identifier objc-superclass[opt]
10778 objc-class-instance-variables[opt]
10779 @interface identifier ( identifier ) objc-protocol-refs[opt]
10780 objc-methodprotolist @end
10781 @interface identifier ( ) objc-protocol-refs[opt]
10782 objc-methodprotolist @end
10783 @implementation identifier ( identifier )
10784
10785 objc-superclass:
10786 : identifier
10787
10788 "@interface identifier (" must start "@interface identifier (
10789 identifier ) ...": objc-methodprotolist in the first production may
10790 not start with a parenthesized identifier as a declarator of a data
10791 definition with no declaration specifiers if the objc-superclass,
10792 objc-protocol-refs and objc-class-instance-variables are omitted. */
10793
10794 static void
10795 c_parser_objc_class_definition (c_parser *parser, tree attributes)
10796 {
10797 bool iface_p;
10798 tree id1;
10799 tree superclass;
10800 if (c_parser_next_token_is_keyword (parser, RID_AT_INTERFACE))
10801 iface_p = true;
10802 else if (c_parser_next_token_is_keyword (parser, RID_AT_IMPLEMENTATION))
10803 iface_p = false;
10804 else
10805 gcc_unreachable ();
10806
10807 c_parser_consume_token (parser);
10808 if (c_parser_next_token_is_not (parser, CPP_NAME))
10809 {
10810 c_parser_error (parser, "expected identifier");
10811 return;
10812 }
10813 id1 = c_parser_peek_token (parser)->value;
10814 c_parser_consume_token (parser);
10815 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
10816 {
10817 /* We have a category or class extension. */
10818 tree id2;
10819 tree proto = NULL_TREE;
10820 matching_parens parens;
10821 parens.consume_open (parser);
10822 if (c_parser_next_token_is_not (parser, CPP_NAME))
10823 {
10824 if (iface_p && c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
10825 {
10826 /* We have a class extension. */
10827 id2 = NULL_TREE;
10828 }
10829 else
10830 {
10831 c_parser_error (parser, "expected identifier or %<)%>");
10832 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
10833 return;
10834 }
10835 }
10836 else
10837 {
10838 id2 = c_parser_peek_token (parser)->value;
10839 c_parser_consume_token (parser);
10840 }
10841 parens.skip_until_found_close (parser);
10842 if (!iface_p)
10843 {
10844 objc_start_category_implementation (id1, id2);
10845 return;
10846 }
10847 if (c_parser_next_token_is (parser, CPP_LESS))
10848 proto = c_parser_objc_protocol_refs (parser);
10849 objc_start_category_interface (id1, id2, proto, attributes);
10850 c_parser_objc_methodprotolist (parser);
10851 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
10852 objc_finish_interface ();
10853 return;
10854 }
10855 if (c_parser_next_token_is (parser, CPP_COLON))
10856 {
10857 c_parser_consume_token (parser);
10858 if (c_parser_next_token_is_not (parser, CPP_NAME))
10859 {
10860 c_parser_error (parser, "expected identifier");
10861 return;
10862 }
10863 superclass = c_parser_peek_token (parser)->value;
10864 c_parser_consume_token (parser);
10865 }
10866 else
10867 superclass = NULL_TREE;
10868 if (iface_p)
10869 {
10870 tree proto = NULL_TREE;
10871 if (c_parser_next_token_is (parser, CPP_LESS))
10872 proto = c_parser_objc_protocol_refs (parser);
10873 objc_start_class_interface (id1, superclass, proto, attributes);
10874 }
10875 else
10876 objc_start_class_implementation (id1, superclass);
10877 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
10878 c_parser_objc_class_instance_variables (parser);
10879 if (iface_p)
10880 {
10881 objc_continue_interface ();
10882 c_parser_objc_methodprotolist (parser);
10883 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
10884 objc_finish_interface ();
10885 }
10886 else
10887 {
10888 objc_continue_implementation ();
10889 return;
10890 }
10891 }
10892
10893 /* Parse objc-class-instance-variables.
10894
10895 objc-class-instance-variables:
10896 { objc-instance-variable-decl-list[opt] }
10897
10898 objc-instance-variable-decl-list:
10899 objc-visibility-spec
10900 objc-instance-variable-decl ;
10901 ;
10902 objc-instance-variable-decl-list objc-visibility-spec
10903 objc-instance-variable-decl-list objc-instance-variable-decl ;
10904 objc-instance-variable-decl-list ;
10905
10906 objc-visibility-spec:
10907 @private
10908 @protected
10909 @public
10910
10911 objc-instance-variable-decl:
10912 struct-declaration
10913 */
10914
10915 static void
10916 c_parser_objc_class_instance_variables (c_parser *parser)
10917 {
10918 gcc_assert (c_parser_next_token_is (parser, CPP_OPEN_BRACE));
10919 c_parser_consume_token (parser);
10920 while (c_parser_next_token_is_not (parser, CPP_EOF))
10921 {
10922 tree decls;
10923 /* Parse any stray semicolon. */
10924 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
10925 {
10926 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
10927 "extra semicolon");
10928 c_parser_consume_token (parser);
10929 continue;
10930 }
10931 /* Stop if at the end of the instance variables. */
10932 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
10933 {
10934 c_parser_consume_token (parser);
10935 break;
10936 }
10937 /* Parse any objc-visibility-spec. */
10938 if (c_parser_next_token_is_keyword (parser, RID_AT_PRIVATE))
10939 {
10940 c_parser_consume_token (parser);
10941 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
10942 continue;
10943 }
10944 else if (c_parser_next_token_is_keyword (parser, RID_AT_PROTECTED))
10945 {
10946 c_parser_consume_token (parser);
10947 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
10948 continue;
10949 }
10950 else if (c_parser_next_token_is_keyword (parser, RID_AT_PUBLIC))
10951 {
10952 c_parser_consume_token (parser);
10953 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
10954 continue;
10955 }
10956 else if (c_parser_next_token_is_keyword (parser, RID_AT_PACKAGE))
10957 {
10958 c_parser_consume_token (parser);
10959 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
10960 continue;
10961 }
10962 else if (c_parser_next_token_is (parser, CPP_PRAGMA))
10963 {
10964 c_parser_pragma (parser, pragma_external, NULL);
10965 continue;
10966 }
10967
10968 /* Parse some comma-separated declarations. */
10969 decls = c_parser_struct_declaration (parser);
10970 if (decls == NULL)
10971 {
10972 /* There is a syntax error. We want to skip the offending
10973 tokens up to the next ';' (included) or '}'
10974 (excluded). */
10975
10976 /* First, skip manually a ')' or ']'. This is because they
10977 reduce the nesting level, so c_parser_skip_until_found()
10978 wouldn't be able to skip past them. */
10979 c_token *token = c_parser_peek_token (parser);
10980 if (token->type == CPP_CLOSE_PAREN || token->type == CPP_CLOSE_SQUARE)
10981 c_parser_consume_token (parser);
10982
10983 /* Then, do the standard skipping. */
10984 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
10985
10986 /* We hopefully recovered. Start normal parsing again. */
10987 parser->error = false;
10988 continue;
10989 }
10990 else
10991 {
10992 /* Comma-separated instance variables are chained together
10993 in reverse order; add them one by one. */
10994 tree ivar = nreverse (decls);
10995 for (; ivar; ivar = DECL_CHAIN (ivar))
10996 objc_add_instance_variable (copy_node (ivar));
10997 }
10998 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
10999 }
11000 }
11001
11002 /* Parse an objc-class-declaration.
11003
11004 objc-class-declaration:
11005 @class identifier-list ;
11006 */
11007
11008 static void
11009 c_parser_objc_class_declaration (c_parser *parser)
11010 {
11011 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_CLASS));
11012 c_parser_consume_token (parser);
11013 /* Any identifiers, including those declared as type names, are OK
11014 here. */
11015 while (true)
11016 {
11017 tree id;
11018 if (c_parser_next_token_is_not (parser, CPP_NAME))
11019 {
11020 c_parser_error (parser, "expected identifier");
11021 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
11022 parser->error = false;
11023 return;
11024 }
11025 id = c_parser_peek_token (parser)->value;
11026 objc_declare_class (id);
11027 c_parser_consume_token (parser);
11028 if (c_parser_next_token_is (parser, CPP_COMMA))
11029 c_parser_consume_token (parser);
11030 else
11031 break;
11032 }
11033 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
11034 }
11035
11036 /* Parse an objc-alias-declaration.
11037
11038 objc-alias-declaration:
11039 @compatibility_alias identifier identifier ;
11040 */
11041
11042 static void
11043 c_parser_objc_alias_declaration (c_parser *parser)
11044 {
11045 tree id1, id2;
11046 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_ALIAS));
11047 c_parser_consume_token (parser);
11048 if (c_parser_next_token_is_not (parser, CPP_NAME))
11049 {
11050 c_parser_error (parser, "expected identifier");
11051 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
11052 return;
11053 }
11054 id1 = c_parser_peek_token (parser)->value;
11055 c_parser_consume_token (parser);
11056 if (c_parser_next_token_is_not (parser, CPP_NAME))
11057 {
11058 c_parser_error (parser, "expected identifier");
11059 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
11060 return;
11061 }
11062 id2 = c_parser_peek_token (parser)->value;
11063 c_parser_consume_token (parser);
11064 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
11065 objc_declare_alias (id1, id2);
11066 }
11067
11068 /* Parse an objc-protocol-definition.
11069
11070 objc-protocol-definition:
11071 @protocol identifier objc-protocol-refs[opt] objc-methodprotolist @end
11072 @protocol identifier-list ;
11073
11074 "@protocol identifier ;" should be resolved as "@protocol
11075 identifier-list ;": objc-methodprotolist may not start with a
11076 semicolon in the first alternative if objc-protocol-refs are
11077 omitted. */
11078
11079 static void
11080 c_parser_objc_protocol_definition (c_parser *parser, tree attributes)
11081 {
11082 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_PROTOCOL));
11083
11084 c_parser_consume_token (parser);
11085 if (c_parser_next_token_is_not (parser, CPP_NAME))
11086 {
11087 c_parser_error (parser, "expected identifier");
11088 return;
11089 }
11090 if (c_parser_peek_2nd_token (parser)->type == CPP_COMMA
11091 || c_parser_peek_2nd_token (parser)->type == CPP_SEMICOLON)
11092 {
11093 /* Any identifiers, including those declared as type names, are
11094 OK here. */
11095 while (true)
11096 {
11097 tree id;
11098 if (c_parser_next_token_is_not (parser, CPP_NAME))
11099 {
11100 c_parser_error (parser, "expected identifier");
11101 break;
11102 }
11103 id = c_parser_peek_token (parser)->value;
11104 objc_declare_protocol (id, attributes);
11105 c_parser_consume_token (parser);
11106 if (c_parser_next_token_is (parser, CPP_COMMA))
11107 c_parser_consume_token (parser);
11108 else
11109 break;
11110 }
11111 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
11112 }
11113 else
11114 {
11115 tree id = c_parser_peek_token (parser)->value;
11116 tree proto = NULL_TREE;
11117 c_parser_consume_token (parser);
11118 if (c_parser_next_token_is (parser, CPP_LESS))
11119 proto = c_parser_objc_protocol_refs (parser);
11120 parser->objc_pq_context = true;
11121 objc_start_protocol (id, proto, attributes);
11122 c_parser_objc_methodprotolist (parser);
11123 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
11124 parser->objc_pq_context = false;
11125 objc_finish_interface ();
11126 }
11127 }
11128
11129 /* Parse an objc-method-type.
11130
11131 objc-method-type:
11132 +
11133 -
11134
11135 Return true if it is a class method (+) and false if it is
11136 an instance method (-).
11137 */
11138 static inline bool
11139 c_parser_objc_method_type (c_parser *parser)
11140 {
11141 switch (c_parser_peek_token (parser)->type)
11142 {
11143 case CPP_PLUS:
11144 c_parser_consume_token (parser);
11145 return true;
11146 case CPP_MINUS:
11147 c_parser_consume_token (parser);
11148 return false;
11149 default:
11150 gcc_unreachable ();
11151 }
11152 }
11153
11154 /* Parse an objc-method-definition.
11155
11156 objc-method-definition:
11157 objc-method-type objc-method-decl ;[opt] compound-statement
11158 */
11159
11160 static void
11161 c_parser_objc_method_definition (c_parser *parser)
11162 {
11163 bool is_class_method = c_parser_objc_method_type (parser);
11164 tree decl, attributes = NULL_TREE, expr = NULL_TREE;
11165 parser->objc_pq_context = true;
11166 decl = c_parser_objc_method_decl (parser, is_class_method, &attributes,
11167 &expr);
11168 if (decl == error_mark_node)
11169 return; /* Bail here. */
11170
11171 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
11172 {
11173 c_parser_consume_token (parser);
11174 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
11175 "extra semicolon in method definition specified");
11176 }
11177
11178 if (!c_parser_next_token_is (parser, CPP_OPEN_BRACE))
11179 {
11180 c_parser_error (parser, "expected %<{%>");
11181 return;
11182 }
11183
11184 parser->objc_pq_context = false;
11185 if (objc_start_method_definition (is_class_method, decl, attributes, expr))
11186 {
11187 add_stmt (c_parser_compound_statement (parser));
11188 objc_finish_method_definition (current_function_decl);
11189 }
11190 else
11191 {
11192 /* This code is executed when we find a method definition
11193 outside of an @implementation context (or invalid for other
11194 reasons). Parse the method (to keep going) but do not emit
11195 any code.
11196 */
11197 c_parser_compound_statement (parser);
11198 }
11199 }
11200
11201 /* Parse an objc-methodprotolist.
11202
11203 objc-methodprotolist:
11204 empty
11205 objc-methodprotolist objc-methodproto
11206 objc-methodprotolist declaration
11207 objc-methodprotolist ;
11208 @optional
11209 @required
11210
11211 The declaration is a data definition, which may be missing
11212 declaration specifiers under the same rules and diagnostics as
11213 other data definitions outside functions, and the stray semicolon
11214 is diagnosed the same way as a stray semicolon outside a
11215 function. */
11216
11217 static void
11218 c_parser_objc_methodprotolist (c_parser *parser)
11219 {
11220 while (true)
11221 {
11222 /* The list is terminated by @end. */
11223 switch (c_parser_peek_token (parser)->type)
11224 {
11225 case CPP_SEMICOLON:
11226 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
11227 "ISO C does not allow extra %<;%> outside of a function");
11228 c_parser_consume_token (parser);
11229 break;
11230 case CPP_PLUS:
11231 case CPP_MINUS:
11232 c_parser_objc_methodproto (parser);
11233 break;
11234 case CPP_PRAGMA:
11235 c_parser_pragma (parser, pragma_external, NULL);
11236 break;
11237 case CPP_EOF:
11238 return;
11239 default:
11240 if (c_parser_next_token_is_keyword (parser, RID_AT_END))
11241 return;
11242 else if (c_parser_next_token_is_keyword (parser, RID_AT_PROPERTY))
11243 c_parser_objc_at_property_declaration (parser);
11244 else if (c_parser_next_token_is_keyword (parser, RID_AT_OPTIONAL))
11245 {
11246 objc_set_method_opt (true);
11247 c_parser_consume_token (parser);
11248 }
11249 else if (c_parser_next_token_is_keyword (parser, RID_AT_REQUIRED))
11250 {
11251 objc_set_method_opt (false);
11252 c_parser_consume_token (parser);
11253 }
11254 else
11255 c_parser_declaration_or_fndef (parser, false, false, true,
11256 false, true, NULL, vNULL);
11257 break;
11258 }
11259 }
11260 }
11261
11262 /* Parse an objc-methodproto.
11263
11264 objc-methodproto:
11265 objc-method-type objc-method-decl ;
11266 */
11267
11268 static void
11269 c_parser_objc_methodproto (c_parser *parser)
11270 {
11271 bool is_class_method = c_parser_objc_method_type (parser);
11272 tree decl, attributes = NULL_TREE;
11273
11274 /* Remember protocol qualifiers in prototypes. */
11275 parser->objc_pq_context = true;
11276 decl = c_parser_objc_method_decl (parser, is_class_method, &attributes,
11277 NULL);
11278 /* Forget protocol qualifiers now. */
11279 parser->objc_pq_context = false;
11280
11281 /* Do not allow the presence of attributes to hide an erroneous
11282 method implementation in the interface section. */
11283 if (!c_parser_next_token_is (parser, CPP_SEMICOLON))
11284 {
11285 c_parser_error (parser, "expected %<;%>");
11286 return;
11287 }
11288
11289 if (decl != error_mark_node)
11290 objc_add_method_declaration (is_class_method, decl, attributes);
11291
11292 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
11293 }
11294
11295 /* If we are at a position that method attributes may be present, check that
11296 there are not any parsed already (a syntax error) and then collect any
11297 specified at the current location. Finally, if new attributes were present,
11298 check that the next token is legal ( ';' for decls and '{' for defs). */
11299
11300 static bool
11301 c_parser_objc_maybe_method_attributes (c_parser* parser, tree* attributes)
11302 {
11303 bool bad = false;
11304 if (*attributes)
11305 {
11306 c_parser_error (parser,
11307 "method attributes must be specified at the end only");
11308 *attributes = NULL_TREE;
11309 bad = true;
11310 }
11311
11312 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
11313 *attributes = c_parser_gnu_attributes (parser);
11314
11315 /* If there were no attributes here, just report any earlier error. */
11316 if (*attributes == NULL_TREE || bad)
11317 return bad;
11318
11319 /* If the attributes are followed by a ; or {, then just report any earlier
11320 error. */
11321 if (c_parser_next_token_is (parser, CPP_SEMICOLON)
11322 || c_parser_next_token_is (parser, CPP_OPEN_BRACE))
11323 return bad;
11324
11325 /* We've got attributes, but not at the end. */
11326 c_parser_error (parser,
11327 "expected %<;%> or %<{%> after method attribute definition");
11328 return true;
11329 }
11330
11331 /* Parse an objc-method-decl.
11332
11333 objc-method-decl:
11334 ( objc-type-name ) objc-selector
11335 objc-selector
11336 ( objc-type-name ) objc-keyword-selector objc-optparmlist
11337 objc-keyword-selector objc-optparmlist
11338 gnu-attributes
11339
11340 objc-keyword-selector:
11341 objc-keyword-decl
11342 objc-keyword-selector objc-keyword-decl
11343
11344 objc-keyword-decl:
11345 objc-selector : ( objc-type-name ) identifier
11346 objc-selector : identifier
11347 : ( objc-type-name ) identifier
11348 : identifier
11349
11350 objc-optparmlist:
11351 objc-optparms objc-optellipsis
11352
11353 objc-optparms:
11354 empty
11355 objc-opt-parms , parameter-declaration
11356
11357 objc-optellipsis:
11358 empty
11359 , ...
11360 */
11361
11362 static tree
11363 c_parser_objc_method_decl (c_parser *parser, bool is_class_method,
11364 tree *attributes, tree *expr)
11365 {
11366 tree type = NULL_TREE;
11367 tree sel;
11368 tree parms = NULL_TREE;
11369 bool ellipsis = false;
11370 bool attr_err = false;
11371
11372 *attributes = NULL_TREE;
11373 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
11374 {
11375 matching_parens parens;
11376 parens.consume_open (parser);
11377 type = c_parser_objc_type_name (parser);
11378 parens.skip_until_found_close (parser);
11379 }
11380 sel = c_parser_objc_selector (parser);
11381 /* If there is no selector, or a colon follows, we have an
11382 objc-keyword-selector. If there is a selector, and a colon does
11383 not follow, that selector ends the objc-method-decl. */
11384 if (!sel || c_parser_next_token_is (parser, CPP_COLON))
11385 {
11386 tree tsel = sel;
11387 tree list = NULL_TREE;
11388 while (true)
11389 {
11390 tree atype = NULL_TREE, id, keyworddecl;
11391 tree param_attr = NULL_TREE;
11392 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
11393 break;
11394 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
11395 {
11396 c_parser_consume_token (parser);
11397 atype = c_parser_objc_type_name (parser);
11398 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
11399 "expected %<)%>");
11400 }
11401 /* New ObjC allows attributes on method parameters. */
11402 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
11403 param_attr = c_parser_gnu_attributes (parser);
11404 if (c_parser_next_token_is_not (parser, CPP_NAME))
11405 {
11406 c_parser_error (parser, "expected identifier");
11407 return error_mark_node;
11408 }
11409 id = c_parser_peek_token (parser)->value;
11410 c_parser_consume_token (parser);
11411 keyworddecl = objc_build_keyword_decl (tsel, atype, id, param_attr);
11412 list = chainon (list, keyworddecl);
11413 tsel = c_parser_objc_selector (parser);
11414 if (!tsel && c_parser_next_token_is_not (parser, CPP_COLON))
11415 break;
11416 }
11417
11418 attr_err |= c_parser_objc_maybe_method_attributes (parser, attributes) ;
11419
11420 /* Parse the optional parameter list. Optional Objective-C
11421 method parameters follow the C syntax, and may include '...'
11422 to denote a variable number of arguments. */
11423 parms = make_node (TREE_LIST);
11424 while (c_parser_next_token_is (parser, CPP_COMMA))
11425 {
11426 struct c_parm *parm;
11427 c_parser_consume_token (parser);
11428 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
11429 {
11430 ellipsis = true;
11431 c_parser_consume_token (parser);
11432 attr_err |= c_parser_objc_maybe_method_attributes
11433 (parser, attributes) ;
11434 break;
11435 }
11436 parm = c_parser_parameter_declaration (parser, NULL_TREE, false);
11437 if (parm == NULL)
11438 break;
11439 parms = chainon (parms,
11440 build_tree_list (NULL_TREE, grokparm (parm, expr)));
11441 }
11442 sel = list;
11443 }
11444 else
11445 attr_err |= c_parser_objc_maybe_method_attributes (parser, attributes) ;
11446
11447 if (sel == NULL)
11448 {
11449 c_parser_error (parser, "objective-c method declaration is expected");
11450 return error_mark_node;
11451 }
11452
11453 if (attr_err)
11454 return error_mark_node;
11455
11456 return objc_build_method_signature (is_class_method, type, sel, parms, ellipsis);
11457 }
11458
11459 /* Parse an objc-type-name.
11460
11461 objc-type-name:
11462 objc-type-qualifiers[opt] type-name
11463 objc-type-qualifiers[opt]
11464
11465 objc-type-qualifiers:
11466 objc-type-qualifier
11467 objc-type-qualifiers objc-type-qualifier
11468
11469 objc-type-qualifier: one of
11470 in out inout bycopy byref oneway
11471 */
11472
11473 static tree
11474 c_parser_objc_type_name (c_parser *parser)
11475 {
11476 tree quals = NULL_TREE;
11477 struct c_type_name *type_name = NULL;
11478 tree type = NULL_TREE;
11479 while (true)
11480 {
11481 c_token *token = c_parser_peek_token (parser);
11482 if (token->type == CPP_KEYWORD
11483 && (token->keyword == RID_IN
11484 || token->keyword == RID_OUT
11485 || token->keyword == RID_INOUT
11486 || token->keyword == RID_BYCOPY
11487 || token->keyword == RID_BYREF
11488 || token->keyword == RID_ONEWAY))
11489 {
11490 quals = chainon (build_tree_list (NULL_TREE, token->value), quals);
11491 c_parser_consume_token (parser);
11492 }
11493 else
11494 break;
11495 }
11496 if (c_parser_next_tokens_start_typename (parser, cla_prefer_type))
11497 type_name = c_parser_type_name (parser);
11498 if (type_name)
11499 type = groktypename (type_name, NULL, NULL);
11500
11501 /* If the type is unknown, and error has already been produced and
11502 we need to recover from the error. In that case, use NULL_TREE
11503 for the type, as if no type had been specified; this will use the
11504 default type ('id') which is good for error recovery. */
11505 if (type == error_mark_node)
11506 type = NULL_TREE;
11507
11508 return build_tree_list (quals, type);
11509 }
11510
11511 /* Parse objc-protocol-refs.
11512
11513 objc-protocol-refs:
11514 < identifier-list >
11515 */
11516
11517 static tree
11518 c_parser_objc_protocol_refs (c_parser *parser)
11519 {
11520 tree list = NULL_TREE;
11521 gcc_assert (c_parser_next_token_is (parser, CPP_LESS));
11522 c_parser_consume_token (parser);
11523 /* Any identifiers, including those declared as type names, are OK
11524 here. */
11525 while (true)
11526 {
11527 tree id;
11528 if (c_parser_next_token_is_not (parser, CPP_NAME))
11529 {
11530 c_parser_error (parser, "expected identifier");
11531 break;
11532 }
11533 id = c_parser_peek_token (parser)->value;
11534 list = chainon (list, build_tree_list (NULL_TREE, id));
11535 c_parser_consume_token (parser);
11536 if (c_parser_next_token_is (parser, CPP_COMMA))
11537 c_parser_consume_token (parser);
11538 else
11539 break;
11540 }
11541 c_parser_require (parser, CPP_GREATER, "expected %<>%>");
11542 return list;
11543 }
11544
11545 /* Parse an objc-try-catch-finally-statement.
11546
11547 objc-try-catch-finally-statement:
11548 @try compound-statement objc-catch-list[opt]
11549 @try compound-statement objc-catch-list[opt] @finally compound-statement
11550
11551 objc-catch-list:
11552 @catch ( objc-catch-parameter-declaration ) compound-statement
11553 objc-catch-list @catch ( objc-catch-parameter-declaration ) compound-statement
11554
11555 objc-catch-parameter-declaration:
11556 parameter-declaration
11557 '...'
11558
11559 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
11560
11561 PS: This function is identical to cp_parser_objc_try_catch_finally_statement
11562 for C++. Keep them in sync. */
11563
11564 static void
11565 c_parser_objc_try_catch_finally_statement (c_parser *parser)
11566 {
11567 location_t location;
11568 tree stmt;
11569
11570 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_TRY));
11571 c_parser_consume_token (parser);
11572 location = c_parser_peek_token (parser)->location;
11573 objc_maybe_warn_exceptions (location);
11574 stmt = c_parser_compound_statement (parser);
11575 objc_begin_try_stmt (location, stmt);
11576
11577 while (c_parser_next_token_is_keyword (parser, RID_AT_CATCH))
11578 {
11579 struct c_parm *parm;
11580 tree parameter_declaration = error_mark_node;
11581 bool seen_open_paren = false;
11582
11583 c_parser_consume_token (parser);
11584 matching_parens parens;
11585 if (!parens.require_open (parser))
11586 seen_open_paren = true;
11587 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
11588 {
11589 /* We have "@catch (...)" (where the '...' are literally
11590 what is in the code). Skip the '...'.
11591 parameter_declaration is set to NULL_TREE, and
11592 objc_being_catch_clauses() knows that that means
11593 '...'. */
11594 c_parser_consume_token (parser);
11595 parameter_declaration = NULL_TREE;
11596 }
11597 else
11598 {
11599 /* We have "@catch (NSException *exception)" or something
11600 like that. Parse the parameter declaration. */
11601 parm = c_parser_parameter_declaration (parser, NULL_TREE, false);
11602 if (parm == NULL)
11603 parameter_declaration = error_mark_node;
11604 else
11605 parameter_declaration = grokparm (parm, NULL);
11606 }
11607 if (seen_open_paren)
11608 parens.require_close (parser);
11609 else
11610 {
11611 /* If there was no open parenthesis, we are recovering from
11612 an error, and we are trying to figure out what mistake
11613 the user has made. */
11614
11615 /* If there is an immediate closing parenthesis, the user
11616 probably forgot the opening one (ie, they typed "@catch
11617 NSException *e)". Parse the closing parenthesis and keep
11618 going. */
11619 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
11620 c_parser_consume_token (parser);
11621
11622 /* If these is no immediate closing parenthesis, the user
11623 probably doesn't know that parenthesis are required at
11624 all (ie, they typed "@catch NSException *e"). So, just
11625 forget about the closing parenthesis and keep going. */
11626 }
11627 objc_begin_catch_clause (parameter_declaration);
11628 if (c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
11629 c_parser_compound_statement_nostart (parser);
11630 objc_finish_catch_clause ();
11631 }
11632 if (c_parser_next_token_is_keyword (parser, RID_AT_FINALLY))
11633 {
11634 c_parser_consume_token (parser);
11635 location = c_parser_peek_token (parser)->location;
11636 stmt = c_parser_compound_statement (parser);
11637 objc_build_finally_clause (location, stmt);
11638 }
11639 objc_finish_try_stmt ();
11640 }
11641
11642 /* Parse an objc-synchronized-statement.
11643
11644 objc-synchronized-statement:
11645 @synchronized ( expression ) compound-statement
11646 */
11647
11648 static void
11649 c_parser_objc_synchronized_statement (c_parser *parser)
11650 {
11651 location_t loc;
11652 tree expr, stmt;
11653 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_SYNCHRONIZED));
11654 c_parser_consume_token (parser);
11655 loc = c_parser_peek_token (parser)->location;
11656 objc_maybe_warn_exceptions (loc);
11657 matching_parens parens;
11658 if (parens.require_open (parser))
11659 {
11660 struct c_expr ce = c_parser_expression (parser);
11661 ce = convert_lvalue_to_rvalue (loc, ce, false, false);
11662 expr = ce.value;
11663 expr = c_fully_fold (expr, false, NULL);
11664 parens.skip_until_found_close (parser);
11665 }
11666 else
11667 expr = error_mark_node;
11668 stmt = c_parser_compound_statement (parser);
11669 objc_build_synchronized (loc, expr, stmt);
11670 }
11671
11672 /* Parse an objc-selector; return NULL_TREE without an error if the
11673 next token is not an objc-selector.
11674
11675 objc-selector:
11676 identifier
11677 one of
11678 enum struct union if else while do for switch case default
11679 break continue return goto asm sizeof typeof __alignof
11680 unsigned long const short volatile signed restrict _Complex
11681 in out inout bycopy byref oneway int char float double void _Bool
11682 _Atomic
11683
11684 ??? Why this selection of keywords but not, for example, storage
11685 class specifiers? */
11686
11687 static tree
11688 c_parser_objc_selector (c_parser *parser)
11689 {
11690 c_token *token = c_parser_peek_token (parser);
11691 tree value = token->value;
11692 if (token->type == CPP_NAME)
11693 {
11694 c_parser_consume_token (parser);
11695 return value;
11696 }
11697 if (token->type != CPP_KEYWORD)
11698 return NULL_TREE;
11699 switch (token->keyword)
11700 {
11701 case RID_ENUM:
11702 case RID_STRUCT:
11703 case RID_UNION:
11704 case RID_IF:
11705 case RID_ELSE:
11706 case RID_WHILE:
11707 case RID_DO:
11708 case RID_FOR:
11709 case RID_SWITCH:
11710 case RID_CASE:
11711 case RID_DEFAULT:
11712 case RID_BREAK:
11713 case RID_CONTINUE:
11714 case RID_RETURN:
11715 case RID_GOTO:
11716 case RID_ASM:
11717 case RID_SIZEOF:
11718 case RID_TYPEOF:
11719 case RID_ALIGNOF:
11720 case RID_UNSIGNED:
11721 case RID_LONG:
11722 case RID_CONST:
11723 case RID_SHORT:
11724 case RID_VOLATILE:
11725 case RID_SIGNED:
11726 case RID_RESTRICT:
11727 case RID_COMPLEX:
11728 case RID_IN:
11729 case RID_OUT:
11730 case RID_INOUT:
11731 case RID_BYCOPY:
11732 case RID_BYREF:
11733 case RID_ONEWAY:
11734 case RID_INT:
11735 case RID_CHAR:
11736 case RID_FLOAT:
11737 case RID_DOUBLE:
11738 CASE_RID_FLOATN_NX:
11739 case RID_VOID:
11740 case RID_BOOL:
11741 case RID_ATOMIC:
11742 case RID_AUTO_TYPE:
11743 case RID_INT_N_0:
11744 case RID_INT_N_1:
11745 case RID_INT_N_2:
11746 case RID_INT_N_3:
11747 c_parser_consume_token (parser);
11748 return value;
11749 default:
11750 return NULL_TREE;
11751 }
11752 }
11753
11754 /* Parse an objc-selector-arg.
11755
11756 objc-selector-arg:
11757 objc-selector
11758 objc-keywordname-list
11759
11760 objc-keywordname-list:
11761 objc-keywordname
11762 objc-keywordname-list objc-keywordname
11763
11764 objc-keywordname:
11765 objc-selector :
11766 :
11767 */
11768
11769 static tree
11770 c_parser_objc_selector_arg (c_parser *parser)
11771 {
11772 tree sel = c_parser_objc_selector (parser);
11773 tree list = NULL_TREE;
11774 if (sel && c_parser_next_token_is_not (parser, CPP_COLON))
11775 return sel;
11776 while (true)
11777 {
11778 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
11779 return list;
11780 list = chainon (list, build_tree_list (sel, NULL_TREE));
11781 sel = c_parser_objc_selector (parser);
11782 if (!sel && c_parser_next_token_is_not (parser, CPP_COLON))
11783 break;
11784 }
11785 return list;
11786 }
11787
11788 /* Parse an objc-receiver.
11789
11790 objc-receiver:
11791 expression
11792 class-name
11793 type-name
11794 */
11795
11796 static tree
11797 c_parser_objc_receiver (c_parser *parser)
11798 {
11799 location_t loc = c_parser_peek_token (parser)->location;
11800
11801 if (c_parser_peek_token (parser)->type == CPP_NAME
11802 && (c_parser_peek_token (parser)->id_kind == C_ID_TYPENAME
11803 || c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME))
11804 {
11805 tree id = c_parser_peek_token (parser)->value;
11806 c_parser_consume_token (parser);
11807 return objc_get_class_reference (id);
11808 }
11809 struct c_expr ce = c_parser_expression (parser);
11810 ce = convert_lvalue_to_rvalue (loc, ce, false, false);
11811 return c_fully_fold (ce.value, false, NULL);
11812 }
11813
11814 /* Parse objc-message-args.
11815
11816 objc-message-args:
11817 objc-selector
11818 objc-keywordarg-list
11819
11820 objc-keywordarg-list:
11821 objc-keywordarg
11822 objc-keywordarg-list objc-keywordarg
11823
11824 objc-keywordarg:
11825 objc-selector : objc-keywordexpr
11826 : objc-keywordexpr
11827 */
11828
11829 static tree
11830 c_parser_objc_message_args (c_parser *parser)
11831 {
11832 tree sel = c_parser_objc_selector (parser);
11833 tree list = NULL_TREE;
11834 if (sel && c_parser_next_token_is_not (parser, CPP_COLON))
11835 return sel;
11836 while (true)
11837 {
11838 tree keywordexpr;
11839 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
11840 return error_mark_node;
11841 keywordexpr = c_parser_objc_keywordexpr (parser);
11842 list = chainon (list, build_tree_list (sel, keywordexpr));
11843 sel = c_parser_objc_selector (parser);
11844 if (!sel && c_parser_next_token_is_not (parser, CPP_COLON))
11845 break;
11846 }
11847 return list;
11848 }
11849
11850 /* Parse an objc-keywordexpr.
11851
11852 objc-keywordexpr:
11853 nonempty-expr-list
11854 */
11855
11856 static tree
11857 c_parser_objc_keywordexpr (c_parser *parser)
11858 {
11859 tree ret;
11860 vec<tree, va_gc> *expr_list = c_parser_expr_list (parser, true, true,
11861 NULL, NULL, NULL, NULL);
11862 if (vec_safe_length (expr_list) == 1)
11863 {
11864 /* Just return the expression, remove a level of
11865 indirection. */
11866 ret = (*expr_list)[0];
11867 }
11868 else
11869 {
11870 /* We have a comma expression, we will collapse later. */
11871 ret = build_tree_list_vec (expr_list);
11872 }
11873 release_tree_vector (expr_list);
11874 return ret;
11875 }
11876
11877 /* A check, needed in several places, that ObjC interface, implementation or
11878 method definitions are not prefixed by incorrect items. */
11879 static bool
11880 c_parser_objc_diagnose_bad_element_prefix (c_parser *parser,
11881 struct c_declspecs *specs)
11882 {
11883 if (!specs->declspecs_seen_p || specs->non_sc_seen_p
11884 || specs->typespec_kind != ctsk_none)
11885 {
11886 c_parser_error (parser,
11887 "no type or storage class may be specified here,");
11888 c_parser_skip_to_end_of_block_or_statement (parser);
11889 return true;
11890 }
11891 return false;
11892 }
11893
11894 /* Parse an Objective-C @property declaration. The syntax is:
11895
11896 objc-property-declaration:
11897 '@property' objc-property-attributes[opt] struct-declaration ;
11898
11899 objc-property-attributes:
11900 '(' objc-property-attribute-list ')'
11901
11902 objc-property-attribute-list:
11903 objc-property-attribute
11904 objc-property-attribute-list, objc-property-attribute
11905
11906 objc-property-attribute
11907 'getter' = identifier
11908 'setter' = identifier
11909 'readonly'
11910 'readwrite'
11911 'assign'
11912 'retain'
11913 'copy'
11914 'nonatomic'
11915
11916 For example:
11917 @property NSString *name;
11918 @property (readonly) id object;
11919 @property (retain, nonatomic, getter=getTheName) id name;
11920 @property int a, b, c;
11921
11922 PS: This function is identical to cp_parser_objc_at_propery_declaration
11923 for C++. Keep them in sync. */
11924 static void
11925 c_parser_objc_at_property_declaration (c_parser *parser)
11926 {
11927 /* The following variables hold the attributes of the properties as
11928 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
11929 seen. When we see an attribute, we set them to 'true' (if they
11930 are boolean properties) or to the identifier (if they have an
11931 argument, ie, for getter and setter). Note that here we only
11932 parse the list of attributes, check the syntax and accumulate the
11933 attributes that we find. objc_add_property_declaration() will
11934 then process the information. */
11935 bool property_assign = false;
11936 bool property_copy = false;
11937 tree property_getter_ident = NULL_TREE;
11938 bool property_nonatomic = false;
11939 bool property_readonly = false;
11940 bool property_readwrite = false;
11941 bool property_retain = false;
11942 tree property_setter_ident = NULL_TREE;
11943
11944 /* 'properties' is the list of properties that we read. Usually a
11945 single one, but maybe more (eg, in "@property int a, b, c;" there
11946 are three). */
11947 tree properties;
11948 location_t loc;
11949
11950 loc = c_parser_peek_token (parser)->location;
11951 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_PROPERTY));
11952
11953 c_parser_consume_token (parser); /* Eat '@property'. */
11954
11955 /* Parse the optional attribute list... */
11956 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
11957 {
11958 matching_parens parens;
11959
11960 /* Eat the '(' */
11961 parens.consume_open (parser);
11962
11963 /* Property attribute keywords are valid now. */
11964 parser->objc_property_attr_context = true;
11965
11966 while (true)
11967 {
11968 bool syntax_error = false;
11969 c_token *token = c_parser_peek_token (parser);
11970 enum rid keyword;
11971
11972 if (token->type != CPP_KEYWORD)
11973 {
11974 if (token->type == CPP_CLOSE_PAREN)
11975 c_parser_error (parser, "expected identifier");
11976 else
11977 {
11978 c_parser_consume_token (parser);
11979 c_parser_error (parser, "unknown property attribute");
11980 }
11981 break;
11982 }
11983 keyword = token->keyword;
11984 c_parser_consume_token (parser);
11985 switch (keyword)
11986 {
11987 case RID_ASSIGN: property_assign = true; break;
11988 case RID_COPY: property_copy = true; break;
11989 case RID_NONATOMIC: property_nonatomic = true; break;
11990 case RID_READONLY: property_readonly = true; break;
11991 case RID_READWRITE: property_readwrite = true; break;
11992 case RID_RETAIN: property_retain = true; break;
11993
11994 case RID_GETTER:
11995 case RID_SETTER:
11996 if (c_parser_next_token_is_not (parser, CPP_EQ))
11997 {
11998 if (keyword == RID_GETTER)
11999 c_parser_error (parser,
12000 "missing %<=%> (after %<getter%> attribute)");
12001 else
12002 c_parser_error (parser,
12003 "missing %<=%> (after %<setter%> attribute)");
12004 syntax_error = true;
12005 break;
12006 }
12007 c_parser_consume_token (parser); /* eat the = */
12008 if (c_parser_next_token_is_not (parser, CPP_NAME))
12009 {
12010 c_parser_error (parser, "expected identifier");
12011 syntax_error = true;
12012 break;
12013 }
12014 if (keyword == RID_SETTER)
12015 {
12016 if (property_setter_ident != NULL_TREE)
12017 c_parser_error (parser, "the %<setter%> attribute may only be specified once");
12018 else
12019 property_setter_ident = c_parser_peek_token (parser)->value;
12020 c_parser_consume_token (parser);
12021 if (c_parser_next_token_is_not (parser, CPP_COLON))
12022 c_parser_error (parser, "setter name must terminate with %<:%>");
12023 else
12024 c_parser_consume_token (parser);
12025 }
12026 else
12027 {
12028 if (property_getter_ident != NULL_TREE)
12029 c_parser_error (parser, "the %<getter%> attribute may only be specified once");
12030 else
12031 property_getter_ident = c_parser_peek_token (parser)->value;
12032 c_parser_consume_token (parser);
12033 }
12034 break;
12035 default:
12036 c_parser_error (parser, "unknown property attribute");
12037 syntax_error = true;
12038 break;
12039 }
12040
12041 if (syntax_error)
12042 break;
12043
12044 if (c_parser_next_token_is (parser, CPP_COMMA))
12045 c_parser_consume_token (parser);
12046 else
12047 break;
12048 }
12049 parser->objc_property_attr_context = false;
12050 parens.skip_until_found_close (parser);
12051 }
12052 /* ... and the property declaration(s). */
12053 properties = c_parser_struct_declaration (parser);
12054
12055 if (properties == error_mark_node)
12056 {
12057 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
12058 parser->error = false;
12059 return;
12060 }
12061
12062 if (properties == NULL_TREE)
12063 c_parser_error (parser, "expected identifier");
12064 else
12065 {
12066 /* Comma-separated properties are chained together in
12067 reverse order; add them one by one. */
12068 properties = nreverse (properties);
12069
12070 for (; properties; properties = TREE_CHAIN (properties))
12071 objc_add_property_declaration (loc, copy_node (properties),
12072 property_readonly, property_readwrite,
12073 property_assign, property_retain,
12074 property_copy, property_nonatomic,
12075 property_getter_ident, property_setter_ident);
12076 }
12077
12078 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
12079 parser->error = false;
12080 }
12081
12082 /* Parse an Objective-C @synthesize declaration. The syntax is:
12083
12084 objc-synthesize-declaration:
12085 @synthesize objc-synthesize-identifier-list ;
12086
12087 objc-synthesize-identifier-list:
12088 objc-synthesize-identifier
12089 objc-synthesize-identifier-list, objc-synthesize-identifier
12090
12091 objc-synthesize-identifier
12092 identifier
12093 identifier = identifier
12094
12095 For example:
12096 @synthesize MyProperty;
12097 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
12098
12099 PS: This function is identical to cp_parser_objc_at_synthesize_declaration
12100 for C++. Keep them in sync.
12101 */
12102 static void
12103 c_parser_objc_at_synthesize_declaration (c_parser *parser)
12104 {
12105 tree list = NULL_TREE;
12106 location_t loc;
12107 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_SYNTHESIZE));
12108 loc = c_parser_peek_token (parser)->location;
12109
12110 c_parser_consume_token (parser);
12111 while (true)
12112 {
12113 tree property, ivar;
12114 if (c_parser_next_token_is_not (parser, CPP_NAME))
12115 {
12116 c_parser_error (parser, "expected identifier");
12117 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
12118 /* Once we find the semicolon, we can resume normal parsing.
12119 We have to reset parser->error manually because
12120 c_parser_skip_until_found() won't reset it for us if the
12121 next token is precisely a semicolon. */
12122 parser->error = false;
12123 return;
12124 }
12125 property = c_parser_peek_token (parser)->value;
12126 c_parser_consume_token (parser);
12127 if (c_parser_next_token_is (parser, CPP_EQ))
12128 {
12129 c_parser_consume_token (parser);
12130 if (c_parser_next_token_is_not (parser, CPP_NAME))
12131 {
12132 c_parser_error (parser, "expected identifier");
12133 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
12134 parser->error = false;
12135 return;
12136 }
12137 ivar = c_parser_peek_token (parser)->value;
12138 c_parser_consume_token (parser);
12139 }
12140 else
12141 ivar = NULL_TREE;
12142 list = chainon (list, build_tree_list (ivar, property));
12143 if (c_parser_next_token_is (parser, CPP_COMMA))
12144 c_parser_consume_token (parser);
12145 else
12146 break;
12147 }
12148 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
12149 objc_add_synthesize_declaration (loc, list);
12150 }
12151
12152 /* Parse an Objective-C @dynamic declaration. The syntax is:
12153
12154 objc-dynamic-declaration:
12155 @dynamic identifier-list ;
12156
12157 For example:
12158 @dynamic MyProperty;
12159 @dynamic MyProperty, AnotherProperty;
12160
12161 PS: This function is identical to cp_parser_objc_at_dynamic_declaration
12162 for C++. Keep them in sync.
12163 */
12164 static void
12165 c_parser_objc_at_dynamic_declaration (c_parser *parser)
12166 {
12167 tree list = NULL_TREE;
12168 location_t loc;
12169 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_DYNAMIC));
12170 loc = c_parser_peek_token (parser)->location;
12171
12172 c_parser_consume_token (parser);
12173 while (true)
12174 {
12175 tree property;
12176 if (c_parser_next_token_is_not (parser, CPP_NAME))
12177 {
12178 c_parser_error (parser, "expected identifier");
12179 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
12180 parser->error = false;
12181 return;
12182 }
12183 property = c_parser_peek_token (parser)->value;
12184 list = chainon (list, build_tree_list (NULL_TREE, property));
12185 c_parser_consume_token (parser);
12186 if (c_parser_next_token_is (parser, CPP_COMMA))
12187 c_parser_consume_token (parser);
12188 else
12189 break;
12190 }
12191 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
12192 objc_add_dynamic_declaration (loc, list);
12193 }
12194
12195 \f
12196 /* Parse a pragma GCC ivdep. */
12197
12198 static bool
12199 c_parse_pragma_ivdep (c_parser *parser)
12200 {
12201 c_parser_consume_pragma (parser);
12202 c_parser_skip_to_pragma_eol (parser);
12203 return true;
12204 }
12205
12206 /* Parse a pragma GCC unroll. */
12207
12208 static unsigned short
12209 c_parser_pragma_unroll (c_parser *parser)
12210 {
12211 unsigned short unroll;
12212 c_parser_consume_pragma (parser);
12213 location_t location = c_parser_peek_token (parser)->location;
12214 tree expr = c_parser_expr_no_commas (parser, NULL).value;
12215 mark_exp_read (expr);
12216 expr = c_fully_fold (expr, false, NULL);
12217 HOST_WIDE_INT lunroll = 0;
12218 if (!INTEGRAL_TYPE_P (TREE_TYPE (expr))
12219 || TREE_CODE (expr) != INTEGER_CST
12220 || (lunroll = tree_to_shwi (expr)) < 0
12221 || lunroll >= USHRT_MAX)
12222 {
12223 error_at (location, "%<#pragma GCC unroll%> requires an"
12224 " assignment-expression that evaluates to a non-negative"
12225 " integral constant less than %u", USHRT_MAX);
12226 unroll = 0;
12227 }
12228 else
12229 {
12230 unroll = (unsigned short)lunroll;
12231 if (unroll == 0)
12232 unroll = 1;
12233 }
12234
12235 c_parser_skip_to_pragma_eol (parser);
12236 return unroll;
12237 }
12238
12239 /* Handle pragmas. Some OpenMP pragmas are associated with, and therefore
12240 should be considered, statements. ALLOW_STMT is true if we're within
12241 the context of a function and such pragmas are to be allowed. Returns
12242 true if we actually parsed such a pragma. */
12243
12244 static bool
12245 c_parser_pragma (c_parser *parser, enum pragma_context context, bool *if_p)
12246 {
12247 unsigned int id;
12248 const char *construct = NULL;
12249
12250 id = c_parser_peek_token (parser)->pragma_kind;
12251 gcc_assert (id != PRAGMA_NONE);
12252
12253 switch (id)
12254 {
12255 case PRAGMA_OACC_DECLARE:
12256 c_parser_oacc_declare (parser);
12257 return false;
12258
12259 case PRAGMA_OACC_ENTER_DATA:
12260 if (context != pragma_compound)
12261 {
12262 construct = "acc enter data";
12263 in_compound:
12264 if (context == pragma_stmt)
12265 {
12266 error_at (c_parser_peek_token (parser)->location,
12267 "%<#pragma %s%> may only be used in compound "
12268 "statements", construct);
12269 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
12270 return false;
12271 }
12272 goto bad_stmt;
12273 }
12274 c_parser_oacc_enter_exit_data (parser, true);
12275 return false;
12276
12277 case PRAGMA_OACC_EXIT_DATA:
12278 if (context != pragma_compound)
12279 {
12280 construct = "acc exit data";
12281 goto in_compound;
12282 }
12283 c_parser_oacc_enter_exit_data (parser, false);
12284 return false;
12285
12286 case PRAGMA_OACC_ROUTINE:
12287 if (context != pragma_external)
12288 {
12289 error_at (c_parser_peek_token (parser)->location,
12290 "%<#pragma acc routine%> must be at file scope");
12291 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
12292 return false;
12293 }
12294 c_parser_oacc_routine (parser, context);
12295 return false;
12296
12297 case PRAGMA_OACC_UPDATE:
12298 if (context != pragma_compound)
12299 {
12300 construct = "acc update";
12301 goto in_compound;
12302 }
12303 c_parser_oacc_update (parser);
12304 return false;
12305
12306 case PRAGMA_OMP_BARRIER:
12307 if (context != pragma_compound)
12308 {
12309 construct = "omp barrier";
12310 goto in_compound;
12311 }
12312 c_parser_omp_barrier (parser);
12313 return false;
12314
12315 case PRAGMA_OMP_DEPOBJ:
12316 if (context != pragma_compound)
12317 {
12318 construct = "omp depobj";
12319 goto in_compound;
12320 }
12321 c_parser_omp_depobj (parser);
12322 return false;
12323
12324 case PRAGMA_OMP_FLUSH:
12325 if (context != pragma_compound)
12326 {
12327 construct = "omp flush";
12328 goto in_compound;
12329 }
12330 c_parser_omp_flush (parser);
12331 return false;
12332
12333 case PRAGMA_OMP_TASKWAIT:
12334 if (context != pragma_compound)
12335 {
12336 construct = "omp taskwait";
12337 goto in_compound;
12338 }
12339 c_parser_omp_taskwait (parser);
12340 return false;
12341
12342 case PRAGMA_OMP_TASKYIELD:
12343 if (context != pragma_compound)
12344 {
12345 construct = "omp taskyield";
12346 goto in_compound;
12347 }
12348 c_parser_omp_taskyield (parser);
12349 return false;
12350
12351 case PRAGMA_OMP_CANCEL:
12352 if (context != pragma_compound)
12353 {
12354 construct = "omp cancel";
12355 goto in_compound;
12356 }
12357 c_parser_omp_cancel (parser);
12358 return false;
12359
12360 case PRAGMA_OMP_CANCELLATION_POINT:
12361 c_parser_omp_cancellation_point (parser, context);
12362 return false;
12363
12364 case PRAGMA_OMP_THREADPRIVATE:
12365 c_parser_omp_threadprivate (parser);
12366 return false;
12367
12368 case PRAGMA_OMP_TARGET:
12369 return c_parser_omp_target (parser, context, if_p);
12370
12371 case PRAGMA_OMP_END_DECLARE_TARGET:
12372 c_parser_omp_end_declare_target (parser);
12373 return false;
12374
12375 case PRAGMA_OMP_SCAN:
12376 error_at (c_parser_peek_token (parser)->location,
12377 "%<#pragma omp scan%> may only be used in "
12378 "a loop construct with %<inscan%> %<reduction%> clause");
12379 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
12380 return false;
12381
12382 case PRAGMA_OMP_SECTION:
12383 error_at (c_parser_peek_token (parser)->location,
12384 "%<#pragma omp section%> may only be used in "
12385 "%<#pragma omp sections%> construct");
12386 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
12387 return false;
12388
12389 case PRAGMA_OMP_DECLARE:
12390 c_parser_omp_declare (parser, context);
12391 return false;
12392
12393 case PRAGMA_OMP_REQUIRES:
12394 c_parser_omp_requires (parser);
12395 return false;
12396
12397 case PRAGMA_OMP_ORDERED:
12398 return c_parser_omp_ordered (parser, context, if_p);
12399
12400 case PRAGMA_IVDEP:
12401 {
12402 const bool ivdep = c_parse_pragma_ivdep (parser);
12403 unsigned short unroll;
12404 if (c_parser_peek_token (parser)->pragma_kind == PRAGMA_UNROLL)
12405 unroll = c_parser_pragma_unroll (parser);
12406 else
12407 unroll = 0;
12408 if (!c_parser_next_token_is_keyword (parser, RID_FOR)
12409 && !c_parser_next_token_is_keyword (parser, RID_WHILE)
12410 && !c_parser_next_token_is_keyword (parser, RID_DO))
12411 {
12412 c_parser_error (parser, "for, while or do statement expected");
12413 return false;
12414 }
12415 if (c_parser_next_token_is_keyword (parser, RID_FOR))
12416 c_parser_for_statement (parser, ivdep, unroll, if_p);
12417 else if (c_parser_next_token_is_keyword (parser, RID_WHILE))
12418 c_parser_while_statement (parser, ivdep, unroll, if_p);
12419 else
12420 c_parser_do_statement (parser, ivdep, unroll);
12421 }
12422 return false;
12423
12424 case PRAGMA_UNROLL:
12425 {
12426 unsigned short unroll = c_parser_pragma_unroll (parser);
12427 bool ivdep;
12428 if (c_parser_peek_token (parser)->pragma_kind == PRAGMA_IVDEP)
12429 ivdep = c_parse_pragma_ivdep (parser);
12430 else
12431 ivdep = false;
12432 if (!c_parser_next_token_is_keyword (parser, RID_FOR)
12433 && !c_parser_next_token_is_keyword (parser, RID_WHILE)
12434 && !c_parser_next_token_is_keyword (parser, RID_DO))
12435 {
12436 c_parser_error (parser, "for, while or do statement expected");
12437 return false;
12438 }
12439 if (c_parser_next_token_is_keyword (parser, RID_FOR))
12440 c_parser_for_statement (parser, ivdep, unroll, if_p);
12441 else if (c_parser_next_token_is_keyword (parser, RID_WHILE))
12442 c_parser_while_statement (parser, ivdep, unroll, if_p);
12443 else
12444 c_parser_do_statement (parser, ivdep, unroll);
12445 }
12446 return false;
12447
12448 case PRAGMA_GCC_PCH_PREPROCESS:
12449 c_parser_error (parser, "%<#pragma GCC pch_preprocess%> must be first");
12450 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
12451 return false;
12452
12453 case PRAGMA_OACC_WAIT:
12454 if (context != pragma_compound)
12455 {
12456 construct = "acc wait";
12457 goto in_compound;
12458 }
12459 /* FALL THROUGH. */
12460
12461 default:
12462 if (id < PRAGMA_FIRST_EXTERNAL)
12463 {
12464 if (context != pragma_stmt && context != pragma_compound)
12465 {
12466 bad_stmt:
12467 c_parser_error (parser, "expected declaration specifiers");
12468 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
12469 return false;
12470 }
12471 c_parser_omp_construct (parser, if_p);
12472 return true;
12473 }
12474 break;
12475 }
12476
12477 c_parser_consume_pragma (parser);
12478 c_invoke_pragma_handler (id);
12479
12480 /* Skip to EOL, but suppress any error message. Those will have been
12481 generated by the handler routine through calling error, as opposed
12482 to calling c_parser_error. */
12483 parser->error = true;
12484 c_parser_skip_to_pragma_eol (parser);
12485
12486 return false;
12487 }
12488
12489 /* The interface the pragma parsers have to the lexer. */
12490
12491 enum cpp_ttype
12492 pragma_lex (tree *value, location_t *loc)
12493 {
12494 c_token *tok = c_parser_peek_token (the_parser);
12495 enum cpp_ttype ret = tok->type;
12496
12497 *value = tok->value;
12498 if (loc)
12499 *loc = tok->location;
12500
12501 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
12502 ret = CPP_EOF;
12503 else if (ret == CPP_STRING)
12504 *value = c_parser_string_literal (the_parser, false, false).value;
12505 else
12506 {
12507 if (ret == CPP_KEYWORD)
12508 ret = CPP_NAME;
12509 c_parser_consume_token (the_parser);
12510 }
12511
12512 return ret;
12513 }
12514
12515 static void
12516 c_parser_pragma_pch_preprocess (c_parser *parser)
12517 {
12518 tree name = NULL;
12519
12520 parser->lex_joined_string = true;
12521 c_parser_consume_pragma (parser);
12522 if (c_parser_next_token_is (parser, CPP_STRING))
12523 {
12524 name = c_parser_peek_token (parser)->value;
12525 c_parser_consume_token (parser);
12526 }
12527 else
12528 c_parser_error (parser, "expected string literal");
12529 c_parser_skip_to_pragma_eol (parser);
12530 parser->lex_joined_string = false;
12531
12532 if (name)
12533 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
12534 }
12535 \f
12536 /* OpenACC and OpenMP parsing routines. */
12537
12538 /* Returns name of the next clause.
12539 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
12540 the token is not consumed. Otherwise appropriate pragma_omp_clause is
12541 returned and the token is consumed. */
12542
12543 static pragma_omp_clause
12544 c_parser_omp_clause_name (c_parser *parser)
12545 {
12546 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
12547
12548 if (c_parser_next_token_is_keyword (parser, RID_AUTO))
12549 result = PRAGMA_OACC_CLAUSE_AUTO;
12550 else if (c_parser_next_token_is_keyword (parser, RID_IF))
12551 result = PRAGMA_OMP_CLAUSE_IF;
12552 else if (c_parser_next_token_is_keyword (parser, RID_DEFAULT))
12553 result = PRAGMA_OMP_CLAUSE_DEFAULT;
12554 else if (c_parser_next_token_is_keyword (parser, RID_FOR))
12555 result = PRAGMA_OMP_CLAUSE_FOR;
12556 else if (c_parser_next_token_is (parser, CPP_NAME))
12557 {
12558 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
12559
12560 switch (p[0])
12561 {
12562 case 'a':
12563 if (!strcmp ("aligned", p))
12564 result = PRAGMA_OMP_CLAUSE_ALIGNED;
12565 else if (!strcmp ("async", p))
12566 result = PRAGMA_OACC_CLAUSE_ASYNC;
12567 else if (!strcmp ("attach", p))
12568 result = PRAGMA_OACC_CLAUSE_ATTACH;
12569 break;
12570 case 'b':
12571 if (!strcmp ("bind", p))
12572 result = PRAGMA_OMP_CLAUSE_BIND;
12573 break;
12574 case 'c':
12575 if (!strcmp ("collapse", p))
12576 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
12577 else if (!strcmp ("copy", p))
12578 result = PRAGMA_OACC_CLAUSE_COPY;
12579 else if (!strcmp ("copyin", p))
12580 result = PRAGMA_OMP_CLAUSE_COPYIN;
12581 else if (!strcmp ("copyout", p))
12582 result = PRAGMA_OACC_CLAUSE_COPYOUT;
12583 else if (!strcmp ("copyprivate", p))
12584 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
12585 else if (!strcmp ("create", p))
12586 result = PRAGMA_OACC_CLAUSE_CREATE;
12587 break;
12588 case 'd':
12589 if (!strcmp ("defaultmap", p))
12590 result = PRAGMA_OMP_CLAUSE_DEFAULTMAP;
12591 else if (!strcmp ("delete", p))
12592 result = PRAGMA_OACC_CLAUSE_DELETE;
12593 else if (!strcmp ("depend", p))
12594 result = PRAGMA_OMP_CLAUSE_DEPEND;
12595 else if (!strcmp ("detach", p))
12596 result = PRAGMA_OACC_CLAUSE_DETACH;
12597 else if (!strcmp ("device", p))
12598 result = PRAGMA_OMP_CLAUSE_DEVICE;
12599 else if (!strcmp ("deviceptr", p))
12600 result = PRAGMA_OACC_CLAUSE_DEVICEPTR;
12601 else if (!strcmp ("device_resident", p))
12602 result = PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT;
12603 else if (!strcmp ("device_type", p))
12604 result = PRAGMA_OMP_CLAUSE_DEVICE_TYPE;
12605 else if (!strcmp ("dist_schedule", p))
12606 result = PRAGMA_OMP_CLAUSE_DIST_SCHEDULE;
12607 break;
12608 case 'f':
12609 if (!strcmp ("final", p))
12610 result = PRAGMA_OMP_CLAUSE_FINAL;
12611 else if (!strcmp ("finalize", p))
12612 result = PRAGMA_OACC_CLAUSE_FINALIZE;
12613 else if (!strcmp ("firstprivate", p))
12614 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
12615 else if (!strcmp ("from", p))
12616 result = PRAGMA_OMP_CLAUSE_FROM;
12617 break;
12618 case 'g':
12619 if (!strcmp ("gang", p))
12620 result = PRAGMA_OACC_CLAUSE_GANG;
12621 else if (!strcmp ("grainsize", p))
12622 result = PRAGMA_OMP_CLAUSE_GRAINSIZE;
12623 break;
12624 case 'h':
12625 if (!strcmp ("hint", p))
12626 result = PRAGMA_OMP_CLAUSE_HINT;
12627 else if (!strcmp ("host", p))
12628 result = PRAGMA_OACC_CLAUSE_HOST;
12629 break;
12630 case 'i':
12631 if (!strcmp ("if_present", p))
12632 result = PRAGMA_OACC_CLAUSE_IF_PRESENT;
12633 else if (!strcmp ("in_reduction", p))
12634 result = PRAGMA_OMP_CLAUSE_IN_REDUCTION;
12635 else if (!strcmp ("inbranch", p))
12636 result = PRAGMA_OMP_CLAUSE_INBRANCH;
12637 else if (!strcmp ("independent", p))
12638 result = PRAGMA_OACC_CLAUSE_INDEPENDENT;
12639 else if (!strcmp ("is_device_ptr", p))
12640 result = PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR;
12641 break;
12642 case 'l':
12643 if (!strcmp ("lastprivate", p))
12644 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
12645 else if (!strcmp ("linear", p))
12646 result = PRAGMA_OMP_CLAUSE_LINEAR;
12647 else if (!strcmp ("link", p))
12648 result = PRAGMA_OMP_CLAUSE_LINK;
12649 break;
12650 case 'm':
12651 if (!strcmp ("map", p))
12652 result = PRAGMA_OMP_CLAUSE_MAP;
12653 else if (!strcmp ("mergeable", p))
12654 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
12655 break;
12656 case 'n':
12657 if (!strcmp ("no_create", p))
12658 result = PRAGMA_OACC_CLAUSE_NO_CREATE;
12659 else if (!strcmp ("nogroup", p))
12660 result = PRAGMA_OMP_CLAUSE_NOGROUP;
12661 else if (!strcmp ("nontemporal", p))
12662 result = PRAGMA_OMP_CLAUSE_NONTEMPORAL;
12663 else if (!strcmp ("notinbranch", p))
12664 result = PRAGMA_OMP_CLAUSE_NOTINBRANCH;
12665 else if (!strcmp ("nowait", p))
12666 result = PRAGMA_OMP_CLAUSE_NOWAIT;
12667 else if (!strcmp ("num_gangs", p))
12668 result = PRAGMA_OACC_CLAUSE_NUM_GANGS;
12669 else if (!strcmp ("num_tasks", p))
12670 result = PRAGMA_OMP_CLAUSE_NUM_TASKS;
12671 else if (!strcmp ("num_teams", p))
12672 result = PRAGMA_OMP_CLAUSE_NUM_TEAMS;
12673 else if (!strcmp ("num_threads", p))
12674 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
12675 else if (!strcmp ("num_workers", p))
12676 result = PRAGMA_OACC_CLAUSE_NUM_WORKERS;
12677 break;
12678 case 'o':
12679 if (!strcmp ("ordered", p))
12680 result = PRAGMA_OMP_CLAUSE_ORDERED;
12681 else if (!strcmp ("order", p))
12682 result = PRAGMA_OMP_CLAUSE_ORDER;
12683 break;
12684 case 'p':
12685 if (!strcmp ("parallel", p))
12686 result = PRAGMA_OMP_CLAUSE_PARALLEL;
12687 else if (!strcmp ("present", p))
12688 result = PRAGMA_OACC_CLAUSE_PRESENT;
12689 /* As of OpenACC 2.5, these are now aliases of the non-present_or
12690 clauses. */
12691 else if (!strcmp ("present_or_copy", p)
12692 || !strcmp ("pcopy", p))
12693 result = PRAGMA_OACC_CLAUSE_COPY;
12694 else if (!strcmp ("present_or_copyin", p)
12695 || !strcmp ("pcopyin", p))
12696 result = PRAGMA_OACC_CLAUSE_COPYIN;
12697 else if (!strcmp ("present_or_copyout", p)
12698 || !strcmp ("pcopyout", p))
12699 result = PRAGMA_OACC_CLAUSE_COPYOUT;
12700 else if (!strcmp ("present_or_create", p)
12701 || !strcmp ("pcreate", p))
12702 result = PRAGMA_OACC_CLAUSE_CREATE;
12703 else if (!strcmp ("priority", p))
12704 result = PRAGMA_OMP_CLAUSE_PRIORITY;
12705 else if (!strcmp ("private", p))
12706 result = PRAGMA_OMP_CLAUSE_PRIVATE;
12707 else if (!strcmp ("proc_bind", p))
12708 result = PRAGMA_OMP_CLAUSE_PROC_BIND;
12709 break;
12710 case 'r':
12711 if (!strcmp ("reduction", p))
12712 result = PRAGMA_OMP_CLAUSE_REDUCTION;
12713 break;
12714 case 's':
12715 if (!strcmp ("safelen", p))
12716 result = PRAGMA_OMP_CLAUSE_SAFELEN;
12717 else if (!strcmp ("schedule", p))
12718 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
12719 else if (!strcmp ("sections", p))
12720 result = PRAGMA_OMP_CLAUSE_SECTIONS;
12721 else if (!strcmp ("self", p)) /* "self" is a synonym for "host". */
12722 result = PRAGMA_OACC_CLAUSE_HOST;
12723 else if (!strcmp ("seq", p))
12724 result = PRAGMA_OACC_CLAUSE_SEQ;
12725 else if (!strcmp ("shared", p))
12726 result = PRAGMA_OMP_CLAUSE_SHARED;
12727 else if (!strcmp ("simd", p))
12728 result = PRAGMA_OMP_CLAUSE_SIMD;
12729 else if (!strcmp ("simdlen", p))
12730 result = PRAGMA_OMP_CLAUSE_SIMDLEN;
12731 break;
12732 case 't':
12733 if (!strcmp ("task_reduction", p))
12734 result = PRAGMA_OMP_CLAUSE_TASK_REDUCTION;
12735 else if (!strcmp ("taskgroup", p))
12736 result = PRAGMA_OMP_CLAUSE_TASKGROUP;
12737 else if (!strcmp ("thread_limit", p))
12738 result = PRAGMA_OMP_CLAUSE_THREAD_LIMIT;
12739 else if (!strcmp ("threads", p))
12740 result = PRAGMA_OMP_CLAUSE_THREADS;
12741 else if (!strcmp ("tile", p))
12742 result = PRAGMA_OACC_CLAUSE_TILE;
12743 else if (!strcmp ("to", p))
12744 result = PRAGMA_OMP_CLAUSE_TO;
12745 break;
12746 case 'u':
12747 if (!strcmp ("uniform", p))
12748 result = PRAGMA_OMP_CLAUSE_UNIFORM;
12749 else if (!strcmp ("untied", p))
12750 result = PRAGMA_OMP_CLAUSE_UNTIED;
12751 else if (!strcmp ("use_device", p))
12752 result = PRAGMA_OACC_CLAUSE_USE_DEVICE;
12753 else if (!strcmp ("use_device_addr", p))
12754 result = PRAGMA_OMP_CLAUSE_USE_DEVICE_ADDR;
12755 else if (!strcmp ("use_device_ptr", p))
12756 result = PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR;
12757 break;
12758 case 'v':
12759 if (!strcmp ("vector", p))
12760 result = PRAGMA_OACC_CLAUSE_VECTOR;
12761 else if (!strcmp ("vector_length", p))
12762 result = PRAGMA_OACC_CLAUSE_VECTOR_LENGTH;
12763 break;
12764 case 'w':
12765 if (!strcmp ("wait", p))
12766 result = PRAGMA_OACC_CLAUSE_WAIT;
12767 else if (!strcmp ("worker", p))
12768 result = PRAGMA_OACC_CLAUSE_WORKER;
12769 break;
12770 }
12771 }
12772
12773 if (result != PRAGMA_OMP_CLAUSE_NONE)
12774 c_parser_consume_token (parser);
12775
12776 return result;
12777 }
12778
12779 /* Validate that a clause of the given type does not already exist. */
12780
12781 static void
12782 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
12783 const char *name)
12784 {
12785 if (tree c = omp_find_clause (clauses, code))
12786 error_at (OMP_CLAUSE_LOCATION (c), "too many %qs clauses", name);
12787 }
12788
12789 /* OpenACC 2.0
12790 Parse wait clause or wait directive parameters. */
12791
12792 static tree
12793 c_parser_oacc_wait_list (c_parser *parser, location_t clause_loc, tree list)
12794 {
12795 vec<tree, va_gc> *args;
12796 tree t, args_tree;
12797
12798 matching_parens parens;
12799 if (!parens.require_open (parser))
12800 return list;
12801
12802 args = c_parser_expr_list (parser, false, true, NULL, NULL, NULL, NULL);
12803 args_tree = build_tree_list_vec (args);
12804
12805 for (t = args_tree; t; t = TREE_CHAIN (t))
12806 {
12807 tree targ = TREE_VALUE (t);
12808
12809 if (targ != error_mark_node)
12810 {
12811 if (!INTEGRAL_TYPE_P (TREE_TYPE (targ)))
12812 {
12813 c_parser_error (parser, "expression must be integral");
12814 targ = error_mark_node;
12815 }
12816 else
12817 {
12818 tree c = build_omp_clause (clause_loc, OMP_CLAUSE_WAIT);
12819
12820 OMP_CLAUSE_DECL (c) = targ;
12821 OMP_CLAUSE_CHAIN (c) = list;
12822 list = c;
12823 }
12824 }
12825 }
12826
12827 release_tree_vector (args);
12828 parens.require_close (parser);
12829 return list;
12830 }
12831
12832 /* OpenACC 2.0, OpenMP 2.5:
12833 variable-list:
12834 identifier
12835 variable-list , identifier
12836
12837 If KIND is nonzero, create the appropriate node and install the
12838 decl in OMP_CLAUSE_DECL and add the node to the head of the list.
12839 If KIND is nonzero, CLAUSE_LOC is the location of the clause.
12840
12841 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
12842 return the list created.
12843
12844 The optional ALLOW_DEREF argument is true if list items can use the deref
12845 (->) operator. */
12846
12847 static tree
12848 c_parser_omp_variable_list (c_parser *parser,
12849 location_t clause_loc,
12850 enum omp_clause_code kind, tree list,
12851 bool allow_deref = false)
12852 {
12853 auto_vec<c_token> tokens;
12854 unsigned int tokens_avail = 0;
12855 bool first = true;
12856
12857 while (1)
12858 {
12859 bool array_section_p = false;
12860 if (kind == OMP_CLAUSE_DEPEND)
12861 {
12862 if (c_parser_next_token_is_not (parser, CPP_NAME)
12863 || c_parser_peek_token (parser)->id_kind != C_ID_ID)
12864 {
12865 struct c_expr expr = c_parser_expr_no_commas (parser, NULL);
12866 if (expr.value != error_mark_node)
12867 {
12868 tree u = build_omp_clause (clause_loc, kind);
12869 OMP_CLAUSE_DECL (u) = expr.value;
12870 OMP_CLAUSE_CHAIN (u) = list;
12871 list = u;
12872 }
12873
12874 if (c_parser_next_token_is_not (parser, CPP_COMMA))
12875 break;
12876
12877 c_parser_consume_token (parser);
12878 first = false;
12879 continue;
12880 }
12881
12882 tokens.truncate (0);
12883 unsigned int nesting_depth = 0;
12884 while (1)
12885 {
12886 c_token *token = c_parser_peek_token (parser);
12887 switch (token->type)
12888 {
12889 case CPP_EOF:
12890 case CPP_PRAGMA_EOL:
12891 break;
12892 case CPP_OPEN_BRACE:
12893 case CPP_OPEN_PAREN:
12894 case CPP_OPEN_SQUARE:
12895 ++nesting_depth;
12896 goto add;
12897 case CPP_CLOSE_BRACE:
12898 case CPP_CLOSE_PAREN:
12899 case CPP_CLOSE_SQUARE:
12900 if (nesting_depth-- == 0)
12901 break;
12902 goto add;
12903 case CPP_COMMA:
12904 if (nesting_depth == 0)
12905 break;
12906 goto add;
12907 default:
12908 add:
12909 tokens.safe_push (*token);
12910 c_parser_consume_token (parser);
12911 continue;
12912 }
12913 break;
12914 }
12915
12916 /* Make sure nothing tries to read past the end of the tokens. */
12917 c_token eof_token;
12918 memset (&eof_token, 0, sizeof (eof_token));
12919 eof_token.type = CPP_EOF;
12920 tokens.safe_push (eof_token);
12921 tokens.safe_push (eof_token);
12922
12923 tokens_avail = parser->tokens_avail;
12924 gcc_assert (parser->tokens == &parser->tokens_buf[0]);
12925 parser->tokens = tokens.address ();
12926 parser->tokens_avail = tokens.length ();
12927 }
12928
12929 tree t = NULL_TREE;
12930
12931 if (c_parser_next_token_is (parser, CPP_NAME)
12932 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
12933 {
12934 t = lookup_name (c_parser_peek_token (parser)->value);
12935
12936 if (t == NULL_TREE)
12937 {
12938 undeclared_variable (c_parser_peek_token (parser)->location,
12939 c_parser_peek_token (parser)->value);
12940 t = error_mark_node;
12941 }
12942
12943 c_parser_consume_token (parser);
12944 }
12945 else if (c_parser_next_token_is (parser, CPP_KEYWORD)
12946 && (c_parser_peek_token (parser)->keyword == RID_FUNCTION_NAME
12947 || (c_parser_peek_token (parser)->keyword
12948 == RID_PRETTY_FUNCTION_NAME)
12949 || (c_parser_peek_token (parser)->keyword
12950 == RID_C99_FUNCTION_NAME)))
12951 t = c_parser_predefined_identifier (parser).value;
12952 else
12953 {
12954 if (first)
12955 c_parser_error (parser, "expected identifier");
12956 break;
12957 }
12958
12959 if (t == error_mark_node)
12960 ;
12961 else if (kind != 0)
12962 {
12963 switch (kind)
12964 {
12965 case OMP_CLAUSE__CACHE_:
12966 /* The OpenACC cache directive explicitly only allows "array
12967 elements or subarrays". */
12968 if (c_parser_peek_token (parser)->type != CPP_OPEN_SQUARE)
12969 {
12970 c_parser_error (parser, "expected %<[%>");
12971 t = error_mark_node;
12972 break;
12973 }
12974 /* FALLTHROUGH */
12975 case OMP_CLAUSE_MAP:
12976 case OMP_CLAUSE_FROM:
12977 case OMP_CLAUSE_TO:
12978 while (c_parser_next_token_is (parser, CPP_DOT)
12979 || (allow_deref
12980 && c_parser_next_token_is (parser, CPP_DEREF)))
12981 {
12982 location_t op_loc = c_parser_peek_token (parser)->location;
12983 if (c_parser_next_token_is (parser, CPP_DEREF))
12984 t = build_simple_mem_ref (t);
12985 c_parser_consume_token (parser);
12986 if (!c_parser_next_token_is (parser, CPP_NAME))
12987 {
12988 c_parser_error (parser, "expected identifier");
12989 t = error_mark_node;
12990 break;
12991 }
12992
12993 c_token *comp_tok = c_parser_peek_token (parser);
12994 tree ident = comp_tok->value;
12995 location_t comp_loc = comp_tok->location;
12996 c_parser_consume_token (parser);
12997 t = build_component_ref (op_loc, t, ident, comp_loc);
12998 }
12999 /* FALLTHROUGH */
13000 case OMP_CLAUSE_DEPEND:
13001 case OMP_CLAUSE_REDUCTION:
13002 case OMP_CLAUSE_IN_REDUCTION:
13003 case OMP_CLAUSE_TASK_REDUCTION:
13004 while (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
13005 {
13006 tree low_bound = NULL_TREE, length = NULL_TREE;
13007
13008 c_parser_consume_token (parser);
13009 if (!c_parser_next_token_is (parser, CPP_COLON))
13010 {
13011 location_t expr_loc
13012 = c_parser_peek_token (parser)->location;
13013 c_expr expr = c_parser_expression (parser);
13014 expr = convert_lvalue_to_rvalue (expr_loc, expr,
13015 false, true);
13016 low_bound = expr.value;
13017 }
13018 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
13019 length = integer_one_node;
13020 else
13021 {
13022 /* Look for `:'. */
13023 if (!c_parser_require (parser, CPP_COLON,
13024 "expected %<:%>"))
13025 {
13026 t = error_mark_node;
13027 break;
13028 }
13029 array_section_p = true;
13030 if (!c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
13031 {
13032 location_t expr_loc
13033 = c_parser_peek_token (parser)->location;
13034 c_expr expr = c_parser_expression (parser);
13035 expr = convert_lvalue_to_rvalue (expr_loc, expr,
13036 false, true);
13037 length = expr.value;
13038 }
13039 }
13040 /* Look for the closing `]'. */
13041 if (!c_parser_require (parser, CPP_CLOSE_SQUARE,
13042 "expected %<]%>"))
13043 {
13044 t = error_mark_node;
13045 break;
13046 }
13047
13048 t = tree_cons (low_bound, length, t);
13049 }
13050 if (kind == OMP_CLAUSE_DEPEND
13051 && t != error_mark_node
13052 && parser->tokens_avail != 2)
13053 {
13054 if (array_section_p)
13055 {
13056 error_at (c_parser_peek_token (parser)->location,
13057 "expected %<)%> or %<,%>");
13058 t = error_mark_node;
13059 }
13060 else
13061 {
13062 parser->tokens = tokens.address ();
13063 parser->tokens_avail = tokens.length ();
13064
13065 t = c_parser_expr_no_commas (parser, NULL).value;
13066 if (t != error_mark_node && parser->tokens_avail != 2)
13067 {
13068 error_at (c_parser_peek_token (parser)->location,
13069 "expected %<)%> or %<,%>");
13070 t = error_mark_node;
13071 }
13072 }
13073 }
13074 break;
13075 default:
13076 break;
13077 }
13078
13079 if (t != error_mark_node)
13080 {
13081 tree u = build_omp_clause (clause_loc, kind);
13082 OMP_CLAUSE_DECL (u) = t;
13083 OMP_CLAUSE_CHAIN (u) = list;
13084 list = u;
13085 }
13086 }
13087 else
13088 list = tree_cons (t, NULL_TREE, list);
13089
13090 if (kind == OMP_CLAUSE_DEPEND)
13091 {
13092 parser->tokens = &parser->tokens_buf[0];
13093 parser->tokens_avail = tokens_avail;
13094 }
13095 if (c_parser_next_token_is_not (parser, CPP_COMMA))
13096 break;
13097
13098 c_parser_consume_token (parser);
13099 first = false;
13100 }
13101
13102 return list;
13103 }
13104
13105 /* Similarly, but expect leading and trailing parenthesis. This is a very
13106 common case for OpenACC and OpenMP clauses. The optional ALLOW_DEREF
13107 argument is true if list items can use the deref (->) operator. */
13108
13109 static tree
13110 c_parser_omp_var_list_parens (c_parser *parser, enum omp_clause_code kind,
13111 tree list, bool allow_deref = false)
13112 {
13113 /* The clauses location. */
13114 location_t loc = c_parser_peek_token (parser)->location;
13115
13116 matching_parens parens;
13117 if (parens.require_open (parser))
13118 {
13119 list = c_parser_omp_variable_list (parser, loc, kind, list, allow_deref);
13120 parens.skip_until_found_close (parser);
13121 }
13122 return list;
13123 }
13124
13125 /* OpenACC 2.0:
13126 copy ( variable-list )
13127 copyin ( variable-list )
13128 copyout ( variable-list )
13129 create ( variable-list )
13130 delete ( variable-list )
13131 present ( variable-list )
13132
13133 OpenACC 2.6:
13134 no_create ( variable-list )
13135 attach ( variable-list )
13136 detach ( variable-list ) */
13137
13138 static tree
13139 c_parser_oacc_data_clause (c_parser *parser, pragma_omp_clause c_kind,
13140 tree list)
13141 {
13142 enum gomp_map_kind kind;
13143 switch (c_kind)
13144 {
13145 case PRAGMA_OACC_CLAUSE_ATTACH:
13146 kind = GOMP_MAP_ATTACH;
13147 break;
13148 case PRAGMA_OACC_CLAUSE_COPY:
13149 kind = GOMP_MAP_TOFROM;
13150 break;
13151 case PRAGMA_OACC_CLAUSE_COPYIN:
13152 kind = GOMP_MAP_TO;
13153 break;
13154 case PRAGMA_OACC_CLAUSE_COPYOUT:
13155 kind = GOMP_MAP_FROM;
13156 break;
13157 case PRAGMA_OACC_CLAUSE_CREATE:
13158 kind = GOMP_MAP_ALLOC;
13159 break;
13160 case PRAGMA_OACC_CLAUSE_DELETE:
13161 kind = GOMP_MAP_RELEASE;
13162 break;
13163 case PRAGMA_OACC_CLAUSE_DETACH:
13164 kind = GOMP_MAP_DETACH;
13165 break;
13166 case PRAGMA_OACC_CLAUSE_DEVICE:
13167 kind = GOMP_MAP_FORCE_TO;
13168 break;
13169 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
13170 kind = GOMP_MAP_DEVICE_RESIDENT;
13171 break;
13172 case PRAGMA_OACC_CLAUSE_HOST:
13173 kind = GOMP_MAP_FORCE_FROM;
13174 break;
13175 case PRAGMA_OACC_CLAUSE_LINK:
13176 kind = GOMP_MAP_LINK;
13177 break;
13178 case PRAGMA_OACC_CLAUSE_NO_CREATE:
13179 kind = GOMP_MAP_IF_PRESENT;
13180 break;
13181 case PRAGMA_OACC_CLAUSE_PRESENT:
13182 kind = GOMP_MAP_FORCE_PRESENT;
13183 break;
13184 default:
13185 gcc_unreachable ();
13186 }
13187 tree nl, c;
13188 nl = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_MAP, list, true);
13189
13190 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
13191 OMP_CLAUSE_SET_MAP_KIND (c, kind);
13192
13193 return nl;
13194 }
13195
13196 /* OpenACC 2.0:
13197 deviceptr ( variable-list ) */
13198
13199 static tree
13200 c_parser_oacc_data_clause_deviceptr (c_parser *parser, tree list)
13201 {
13202 location_t loc = c_parser_peek_token (parser)->location;
13203 tree vars, t;
13204
13205 /* Can't use OMP_CLAUSE_MAP here (that is, can't use the generic
13206 c_parser_oacc_data_clause), as for PRAGMA_OACC_CLAUSE_DEVICEPTR,
13207 variable-list must only allow for pointer variables. */
13208 vars = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_ERROR, NULL);
13209 for (t = vars; t && t; t = TREE_CHAIN (t))
13210 {
13211 tree v = TREE_PURPOSE (t);
13212
13213 /* FIXME diagnostics: Ideally we should keep individual
13214 locations for all the variables in the var list to make the
13215 following errors more precise. Perhaps
13216 c_parser_omp_var_list_parens() should construct a list of
13217 locations to go along with the var list. */
13218
13219 if (!VAR_P (v) && TREE_CODE (v) != PARM_DECL)
13220 error_at (loc, "%qD is not a variable", v);
13221 else if (TREE_TYPE (v) == error_mark_node)
13222 ;
13223 else if (!POINTER_TYPE_P (TREE_TYPE (v)))
13224 error_at (loc, "%qD is not a pointer variable", v);
13225
13226 tree u = build_omp_clause (loc, OMP_CLAUSE_MAP);
13227 OMP_CLAUSE_SET_MAP_KIND (u, GOMP_MAP_FORCE_DEVICEPTR);
13228 OMP_CLAUSE_DECL (u) = v;
13229 OMP_CLAUSE_CHAIN (u) = list;
13230 list = u;
13231 }
13232
13233 return list;
13234 }
13235
13236 /* OpenACC 2.0, OpenMP 3.0:
13237 collapse ( constant-expression ) */
13238
13239 static tree
13240 c_parser_omp_clause_collapse (c_parser *parser, tree list)
13241 {
13242 tree c, num = error_mark_node;
13243 HOST_WIDE_INT n;
13244 location_t loc;
13245
13246 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse");
13247 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile");
13248
13249 loc = c_parser_peek_token (parser)->location;
13250 matching_parens parens;
13251 if (parens.require_open (parser))
13252 {
13253 num = c_parser_expr_no_commas (parser, NULL).value;
13254 parens.skip_until_found_close (parser);
13255 }
13256 if (num == error_mark_node)
13257 return list;
13258 mark_exp_read (num);
13259 num = c_fully_fold (num, false, NULL);
13260 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
13261 || !tree_fits_shwi_p (num)
13262 || (n = tree_to_shwi (num)) <= 0
13263 || (int) n != n)
13264 {
13265 error_at (loc,
13266 "collapse argument needs positive constant integer expression");
13267 return list;
13268 }
13269 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
13270 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
13271 OMP_CLAUSE_CHAIN (c) = list;
13272 return c;
13273 }
13274
13275 /* OpenMP 2.5:
13276 copyin ( variable-list ) */
13277
13278 static tree
13279 c_parser_omp_clause_copyin (c_parser *parser, tree list)
13280 {
13281 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_COPYIN, list);
13282 }
13283
13284 /* OpenMP 2.5:
13285 copyprivate ( variable-list ) */
13286
13287 static tree
13288 c_parser_omp_clause_copyprivate (c_parser *parser, tree list)
13289 {
13290 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_COPYPRIVATE, list);
13291 }
13292
13293 /* OpenMP 2.5:
13294 default ( none | shared )
13295
13296 OpenACC:
13297 default ( none | present ) */
13298
13299 static tree
13300 c_parser_omp_clause_default (c_parser *parser, tree list, bool is_oacc)
13301 {
13302 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
13303 location_t loc = c_parser_peek_token (parser)->location;
13304 tree c;
13305
13306 matching_parens parens;
13307 if (!parens.require_open (parser))
13308 return list;
13309 if (c_parser_next_token_is (parser, CPP_NAME))
13310 {
13311 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
13312
13313 switch (p[0])
13314 {
13315 case 'n':
13316 if (strcmp ("none", p) != 0)
13317 goto invalid_kind;
13318 kind = OMP_CLAUSE_DEFAULT_NONE;
13319 break;
13320
13321 case 'p':
13322 if (strcmp ("present", p) != 0 || !is_oacc)
13323 goto invalid_kind;
13324 kind = OMP_CLAUSE_DEFAULT_PRESENT;
13325 break;
13326
13327 case 's':
13328 if (strcmp ("shared", p) != 0 || is_oacc)
13329 goto invalid_kind;
13330 kind = OMP_CLAUSE_DEFAULT_SHARED;
13331 break;
13332
13333 default:
13334 goto invalid_kind;
13335 }
13336
13337 c_parser_consume_token (parser);
13338 }
13339 else
13340 {
13341 invalid_kind:
13342 if (is_oacc)
13343 c_parser_error (parser, "expected %<none%> or %<present%>");
13344 else
13345 c_parser_error (parser, "expected %<none%> or %<shared%>");
13346 }
13347 parens.skip_until_found_close (parser);
13348
13349 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
13350 return list;
13351
13352 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default");
13353 c = build_omp_clause (loc, OMP_CLAUSE_DEFAULT);
13354 OMP_CLAUSE_CHAIN (c) = list;
13355 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
13356
13357 return c;
13358 }
13359
13360 /* OpenMP 2.5:
13361 firstprivate ( variable-list ) */
13362
13363 static tree
13364 c_parser_omp_clause_firstprivate (c_parser *parser, tree list)
13365 {
13366 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_FIRSTPRIVATE, list);
13367 }
13368
13369 /* OpenMP 3.1:
13370 final ( expression ) */
13371
13372 static tree
13373 c_parser_omp_clause_final (c_parser *parser, tree list)
13374 {
13375 location_t loc = c_parser_peek_token (parser)->location;
13376 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
13377 {
13378 matching_parens parens;
13379 tree t, c;
13380 if (!parens.require_open (parser))
13381 t = error_mark_node;
13382 else
13383 {
13384 location_t eloc = c_parser_peek_token (parser)->location;
13385 c_expr expr = c_parser_expr_no_commas (parser, NULL);
13386 t = convert_lvalue_to_rvalue (eloc, expr, true, true).value;
13387 t = c_objc_common_truthvalue_conversion (eloc, t);
13388 t = c_fully_fold (t, false, NULL);
13389 parens.skip_until_found_close (parser);
13390 }
13391
13392 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final");
13393
13394 c = build_omp_clause (loc, OMP_CLAUSE_FINAL);
13395 OMP_CLAUSE_FINAL_EXPR (c) = t;
13396 OMP_CLAUSE_CHAIN (c) = list;
13397 list = c;
13398 }
13399 else
13400 c_parser_error (parser, "expected %<(%>");
13401
13402 return list;
13403 }
13404
13405 /* OpenACC, OpenMP 2.5:
13406 if ( expression )
13407
13408 OpenMP 4.5:
13409 if ( directive-name-modifier : expression )
13410
13411 directive-name-modifier:
13412 parallel | task | taskloop | target data | target | target update
13413 | target enter data | target exit data
13414
13415 OpenMP 5.0:
13416 directive-name-modifier:
13417 ... | simd | cancel */
13418
13419 static tree
13420 c_parser_omp_clause_if (c_parser *parser, tree list, bool is_omp)
13421 {
13422 location_t location = c_parser_peek_token (parser)->location;
13423 enum tree_code if_modifier = ERROR_MARK;
13424
13425 matching_parens parens;
13426 if (!parens.require_open (parser))
13427 return list;
13428
13429 if (is_omp && c_parser_next_token_is (parser, CPP_NAME))
13430 {
13431 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
13432 int n = 2;
13433 if (strcmp (p, "cancel") == 0)
13434 if_modifier = VOID_CST;
13435 else if (strcmp (p, "parallel") == 0)
13436 if_modifier = OMP_PARALLEL;
13437 else if (strcmp (p, "simd") == 0)
13438 if_modifier = OMP_SIMD;
13439 else if (strcmp (p, "task") == 0)
13440 if_modifier = OMP_TASK;
13441 else if (strcmp (p, "taskloop") == 0)
13442 if_modifier = OMP_TASKLOOP;
13443 else if (strcmp (p, "target") == 0)
13444 {
13445 if_modifier = OMP_TARGET;
13446 if (c_parser_peek_2nd_token (parser)->type == CPP_NAME)
13447 {
13448 p = IDENTIFIER_POINTER (c_parser_peek_2nd_token (parser)->value);
13449 if (strcmp ("data", p) == 0)
13450 if_modifier = OMP_TARGET_DATA;
13451 else if (strcmp ("update", p) == 0)
13452 if_modifier = OMP_TARGET_UPDATE;
13453 else if (strcmp ("enter", p) == 0)
13454 if_modifier = OMP_TARGET_ENTER_DATA;
13455 else if (strcmp ("exit", p) == 0)
13456 if_modifier = OMP_TARGET_EXIT_DATA;
13457 if (if_modifier != OMP_TARGET)
13458 {
13459 n = 3;
13460 c_parser_consume_token (parser);
13461 }
13462 else
13463 {
13464 location_t loc = c_parser_peek_2nd_token (parser)->location;
13465 error_at (loc, "expected %<data%>, %<update%>, %<enter%> "
13466 "or %<exit%>");
13467 if_modifier = ERROR_MARK;
13468 }
13469 if (if_modifier == OMP_TARGET_ENTER_DATA
13470 || if_modifier == OMP_TARGET_EXIT_DATA)
13471 {
13472 if (c_parser_peek_2nd_token (parser)->type == CPP_NAME)
13473 {
13474 p = IDENTIFIER_POINTER
13475 (c_parser_peek_2nd_token (parser)->value);
13476 if (strcmp ("data", p) == 0)
13477 n = 4;
13478 }
13479 if (n == 4)
13480 c_parser_consume_token (parser);
13481 else
13482 {
13483 location_t loc
13484 = c_parser_peek_2nd_token (parser)->location;
13485 error_at (loc, "expected %<data%>");
13486 if_modifier = ERROR_MARK;
13487 }
13488 }
13489 }
13490 }
13491 if (if_modifier != ERROR_MARK)
13492 {
13493 if (c_parser_peek_2nd_token (parser)->type == CPP_COLON)
13494 {
13495 c_parser_consume_token (parser);
13496 c_parser_consume_token (parser);
13497 }
13498 else
13499 {
13500 if (n > 2)
13501 {
13502 location_t loc = c_parser_peek_2nd_token (parser)->location;
13503 error_at (loc, "expected %<:%>");
13504 }
13505 if_modifier = ERROR_MARK;
13506 }
13507 }
13508 }
13509
13510 location_t loc = c_parser_peek_token (parser)->location;
13511 c_expr expr = c_parser_expr_no_commas (parser, NULL);
13512 expr = convert_lvalue_to_rvalue (loc, expr, true, true);
13513 tree t = c_objc_common_truthvalue_conversion (loc, expr.value), c;
13514 t = c_fully_fold (t, false, NULL);
13515 parens.skip_until_found_close (parser);
13516
13517 for (c = list; c ; c = OMP_CLAUSE_CHAIN (c))
13518 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IF)
13519 {
13520 if (if_modifier != ERROR_MARK
13521 && OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
13522 {
13523 const char *p = NULL;
13524 switch (if_modifier)
13525 {
13526 case VOID_CST: p = "cancel"; break;
13527 case OMP_PARALLEL: p = "parallel"; break;
13528 case OMP_SIMD: p = "simd"; break;
13529 case OMP_TASK: p = "task"; break;
13530 case OMP_TASKLOOP: p = "taskloop"; break;
13531 case OMP_TARGET_DATA: p = "target data"; break;
13532 case OMP_TARGET: p = "target"; break;
13533 case OMP_TARGET_UPDATE: p = "target update"; break;
13534 case OMP_TARGET_ENTER_DATA: p = "target enter data"; break;
13535 case OMP_TARGET_EXIT_DATA: p = "target exit data"; break;
13536 default: gcc_unreachable ();
13537 }
13538 error_at (location, "too many %<if%> clauses with %qs modifier",
13539 p);
13540 return list;
13541 }
13542 else if (OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
13543 {
13544 if (!is_omp)
13545 error_at (location, "too many %<if%> clauses");
13546 else
13547 error_at (location, "too many %<if%> clauses without modifier");
13548 return list;
13549 }
13550 else if (if_modifier == ERROR_MARK
13551 || OMP_CLAUSE_IF_MODIFIER (c) == ERROR_MARK)
13552 {
13553 error_at (location, "if any %<if%> clause has modifier, then all "
13554 "%<if%> clauses have to use modifier");
13555 return list;
13556 }
13557 }
13558
13559 c = build_omp_clause (location, OMP_CLAUSE_IF);
13560 OMP_CLAUSE_IF_MODIFIER (c) = if_modifier;
13561 OMP_CLAUSE_IF_EXPR (c) = t;
13562 OMP_CLAUSE_CHAIN (c) = list;
13563 return c;
13564 }
13565
13566 /* OpenMP 2.5:
13567 lastprivate ( variable-list )
13568
13569 OpenMP 5.0:
13570 lastprivate ( [ lastprivate-modifier : ] variable-list ) */
13571
13572 static tree
13573 c_parser_omp_clause_lastprivate (c_parser *parser, tree list)
13574 {
13575 /* The clauses location. */
13576 location_t loc = c_parser_peek_token (parser)->location;
13577
13578 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
13579 {
13580 bool conditional = false;
13581 if (c_parser_next_token_is (parser, CPP_NAME)
13582 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
13583 {
13584 const char *p
13585 = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
13586 if (strcmp (p, "conditional") == 0)
13587 {
13588 conditional = true;
13589 c_parser_consume_token (parser);
13590 c_parser_consume_token (parser);
13591 }
13592 }
13593 tree nlist = c_parser_omp_variable_list (parser, loc,
13594 OMP_CLAUSE_LASTPRIVATE, list);
13595 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
13596 if (conditional)
13597 for (tree c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
13598 OMP_CLAUSE_LASTPRIVATE_CONDITIONAL (c) = 1;
13599 return nlist;
13600 }
13601 return list;
13602 }
13603
13604 /* OpenMP 3.1:
13605 mergeable */
13606
13607 static tree
13608 c_parser_omp_clause_mergeable (c_parser *parser ATTRIBUTE_UNUSED, tree list)
13609 {
13610 tree c;
13611
13612 /* FIXME: Should we allow duplicates? */
13613 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable");
13614
13615 c = build_omp_clause (c_parser_peek_token (parser)->location,
13616 OMP_CLAUSE_MERGEABLE);
13617 OMP_CLAUSE_CHAIN (c) = list;
13618
13619 return c;
13620 }
13621
13622 /* OpenMP 2.5:
13623 nowait */
13624
13625 static tree
13626 c_parser_omp_clause_nowait (c_parser *parser ATTRIBUTE_UNUSED, tree list)
13627 {
13628 tree c;
13629 location_t loc = c_parser_peek_token (parser)->location;
13630
13631 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait");
13632
13633 c = build_omp_clause (loc, OMP_CLAUSE_NOWAIT);
13634 OMP_CLAUSE_CHAIN (c) = list;
13635 return c;
13636 }
13637
13638 /* OpenMP 2.5:
13639 num_threads ( expression ) */
13640
13641 static tree
13642 c_parser_omp_clause_num_threads (c_parser *parser, tree list)
13643 {
13644 location_t num_threads_loc = c_parser_peek_token (parser)->location;
13645 matching_parens parens;
13646 if (parens.require_open (parser))
13647 {
13648 location_t expr_loc = c_parser_peek_token (parser)->location;
13649 c_expr expr = c_parser_expr_no_commas (parser, NULL);
13650 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
13651 tree c, t = expr.value;
13652 t = c_fully_fold (t, false, NULL);
13653
13654 parens.skip_until_found_close (parser);
13655
13656 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
13657 {
13658 c_parser_error (parser, "expected integer expression");
13659 return list;
13660 }
13661
13662 /* Attempt to statically determine when the number isn't positive. */
13663 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
13664 build_int_cst (TREE_TYPE (t), 0));
13665 protected_set_expr_location (c, expr_loc);
13666 if (c == boolean_true_node)
13667 {
13668 warning_at (expr_loc, 0,
13669 "%<num_threads%> value must be positive");
13670 t = integer_one_node;
13671 }
13672
13673 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS, "num_threads");
13674
13675 c = build_omp_clause (num_threads_loc, OMP_CLAUSE_NUM_THREADS);
13676 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
13677 OMP_CLAUSE_CHAIN (c) = list;
13678 list = c;
13679 }
13680
13681 return list;
13682 }
13683
13684 /* OpenMP 4.5:
13685 num_tasks ( expression ) */
13686
13687 static tree
13688 c_parser_omp_clause_num_tasks (c_parser *parser, tree list)
13689 {
13690 location_t num_tasks_loc = c_parser_peek_token (parser)->location;
13691 matching_parens parens;
13692 if (parens.require_open (parser))
13693 {
13694 location_t expr_loc = c_parser_peek_token (parser)->location;
13695 c_expr expr = c_parser_expr_no_commas (parser, NULL);
13696 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
13697 tree c, t = expr.value;
13698 t = c_fully_fold (t, false, NULL);
13699
13700 parens.skip_until_found_close (parser);
13701
13702 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
13703 {
13704 c_parser_error (parser, "expected integer expression");
13705 return list;
13706 }
13707
13708 /* Attempt to statically determine when the number isn't positive. */
13709 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
13710 build_int_cst (TREE_TYPE (t), 0));
13711 if (CAN_HAVE_LOCATION_P (c))
13712 SET_EXPR_LOCATION (c, expr_loc);
13713 if (c == boolean_true_node)
13714 {
13715 warning_at (expr_loc, 0, "%<num_tasks%> value must be positive");
13716 t = integer_one_node;
13717 }
13718
13719 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TASKS, "num_tasks");
13720
13721 c = build_omp_clause (num_tasks_loc, OMP_CLAUSE_NUM_TASKS);
13722 OMP_CLAUSE_NUM_TASKS_EXPR (c) = t;
13723 OMP_CLAUSE_CHAIN (c) = list;
13724 list = c;
13725 }
13726
13727 return list;
13728 }
13729
13730 /* OpenMP 4.5:
13731 grainsize ( expression ) */
13732
13733 static tree
13734 c_parser_omp_clause_grainsize (c_parser *parser, tree list)
13735 {
13736 location_t grainsize_loc = c_parser_peek_token (parser)->location;
13737 matching_parens parens;
13738 if (parens.require_open (parser))
13739 {
13740 location_t expr_loc = c_parser_peek_token (parser)->location;
13741 c_expr expr = c_parser_expr_no_commas (parser, NULL);
13742 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
13743 tree c, t = expr.value;
13744 t = c_fully_fold (t, false, NULL);
13745
13746 parens.skip_until_found_close (parser);
13747
13748 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
13749 {
13750 c_parser_error (parser, "expected integer expression");
13751 return list;
13752 }
13753
13754 /* Attempt to statically determine when the number isn't positive. */
13755 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
13756 build_int_cst (TREE_TYPE (t), 0));
13757 if (CAN_HAVE_LOCATION_P (c))
13758 SET_EXPR_LOCATION (c, expr_loc);
13759 if (c == boolean_true_node)
13760 {
13761 warning_at (expr_loc, 0, "%<grainsize%> value must be positive");
13762 t = integer_one_node;
13763 }
13764
13765 check_no_duplicate_clause (list, OMP_CLAUSE_GRAINSIZE, "grainsize");
13766
13767 c = build_omp_clause (grainsize_loc, OMP_CLAUSE_GRAINSIZE);
13768 OMP_CLAUSE_GRAINSIZE_EXPR (c) = t;
13769 OMP_CLAUSE_CHAIN (c) = list;
13770 list = c;
13771 }
13772
13773 return list;
13774 }
13775
13776 /* OpenMP 4.5:
13777 priority ( expression ) */
13778
13779 static tree
13780 c_parser_omp_clause_priority (c_parser *parser, tree list)
13781 {
13782 location_t priority_loc = c_parser_peek_token (parser)->location;
13783 matching_parens parens;
13784 if (parens.require_open (parser))
13785 {
13786 location_t expr_loc = c_parser_peek_token (parser)->location;
13787 c_expr expr = c_parser_expr_no_commas (parser, NULL);
13788 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
13789 tree c, t = expr.value;
13790 t = c_fully_fold (t, false, NULL);
13791
13792 parens.skip_until_found_close (parser);
13793
13794 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
13795 {
13796 c_parser_error (parser, "expected integer expression");
13797 return list;
13798 }
13799
13800 /* Attempt to statically determine when the number isn't
13801 non-negative. */
13802 c = fold_build2_loc (expr_loc, LT_EXPR, boolean_type_node, t,
13803 build_int_cst (TREE_TYPE (t), 0));
13804 if (CAN_HAVE_LOCATION_P (c))
13805 SET_EXPR_LOCATION (c, expr_loc);
13806 if (c == boolean_true_node)
13807 {
13808 warning_at (expr_loc, 0, "%<priority%> value must be non-negative");
13809 t = integer_one_node;
13810 }
13811
13812 check_no_duplicate_clause (list, OMP_CLAUSE_PRIORITY, "priority");
13813
13814 c = build_omp_clause (priority_loc, OMP_CLAUSE_PRIORITY);
13815 OMP_CLAUSE_PRIORITY_EXPR (c) = t;
13816 OMP_CLAUSE_CHAIN (c) = list;
13817 list = c;
13818 }
13819
13820 return list;
13821 }
13822
13823 /* OpenMP 4.5:
13824 hint ( expression ) */
13825
13826 static tree
13827 c_parser_omp_clause_hint (c_parser *parser, tree list)
13828 {
13829 location_t hint_loc = c_parser_peek_token (parser)->location;
13830 matching_parens parens;
13831 if (parens.require_open (parser))
13832 {
13833 location_t expr_loc = c_parser_peek_token (parser)->location;
13834 c_expr expr = c_parser_expr_no_commas (parser, NULL);
13835 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
13836 tree c, t = expr.value;
13837 t = c_fully_fold (t, false, NULL);
13838
13839 parens.skip_until_found_close (parser);
13840
13841 if (!INTEGRAL_TYPE_P (TREE_TYPE (t))
13842 || TREE_CODE (t) != INTEGER_CST)
13843 {
13844 c_parser_error (parser, "expected constant integer expression");
13845 return list;
13846 }
13847
13848 check_no_duplicate_clause (list, OMP_CLAUSE_HINT, "hint");
13849
13850 c = build_omp_clause (hint_loc, OMP_CLAUSE_HINT);
13851 OMP_CLAUSE_HINT_EXPR (c) = t;
13852 OMP_CLAUSE_CHAIN (c) = list;
13853 list = c;
13854 }
13855
13856 return list;
13857 }
13858
13859 /* OpenMP 4.5:
13860 defaultmap ( tofrom : scalar )
13861
13862 OpenMP 5.0:
13863 defaultmap ( implicit-behavior [ : variable-category ] ) */
13864
13865 static tree
13866 c_parser_omp_clause_defaultmap (c_parser *parser, tree list)
13867 {
13868 location_t loc = c_parser_peek_token (parser)->location;
13869 tree c;
13870 const char *p;
13871 enum omp_clause_defaultmap_kind behavior = OMP_CLAUSE_DEFAULTMAP_DEFAULT;
13872 enum omp_clause_defaultmap_kind category
13873 = OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED;
13874
13875 matching_parens parens;
13876 if (!parens.require_open (parser))
13877 return list;
13878 if (c_parser_next_token_is_keyword (parser, RID_DEFAULT))
13879 p = "default";
13880 else if (!c_parser_next_token_is (parser, CPP_NAME))
13881 {
13882 invalid_behavior:
13883 c_parser_error (parser, "expected %<alloc%>, %<to%>, %<from%>, "
13884 "%<tofrom%>, %<firstprivate%>, %<none%> "
13885 "or %<default%>");
13886 goto out_err;
13887 }
13888 else
13889 p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
13890
13891 switch (p[0])
13892 {
13893 case 'a':
13894 if (strcmp ("alloc", p) == 0)
13895 behavior = OMP_CLAUSE_DEFAULTMAP_ALLOC;
13896 else
13897 goto invalid_behavior;
13898 break;
13899
13900 case 'd':
13901 if (strcmp ("default", p) == 0)
13902 behavior = OMP_CLAUSE_DEFAULTMAP_DEFAULT;
13903 else
13904 goto invalid_behavior;
13905 break;
13906
13907 case 'f':
13908 if (strcmp ("firstprivate", p) == 0)
13909 behavior = OMP_CLAUSE_DEFAULTMAP_FIRSTPRIVATE;
13910 else if (strcmp ("from", p) == 0)
13911 behavior = OMP_CLAUSE_DEFAULTMAP_FROM;
13912 else
13913 goto invalid_behavior;
13914 break;
13915
13916 case 'n':
13917 if (strcmp ("none", p) == 0)
13918 behavior = OMP_CLAUSE_DEFAULTMAP_NONE;
13919 else
13920 goto invalid_behavior;
13921 break;
13922
13923 case 't':
13924 if (strcmp ("tofrom", p) == 0)
13925 behavior = OMP_CLAUSE_DEFAULTMAP_TOFROM;
13926 else if (strcmp ("to", p) == 0)
13927 behavior = OMP_CLAUSE_DEFAULTMAP_TO;
13928 else
13929 goto invalid_behavior;
13930 break;
13931
13932 default:
13933 goto invalid_behavior;
13934 }
13935 c_parser_consume_token (parser);
13936
13937 if (!c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
13938 {
13939 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
13940 goto out_err;
13941 if (!c_parser_next_token_is (parser, CPP_NAME))
13942 {
13943 invalid_category:
13944 c_parser_error (parser, "expected %<scalar%>, %<aggregate%> or "
13945 "%<pointer%>");
13946 goto out_err;
13947 }
13948 p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
13949 switch (p[0])
13950 {
13951 case 'a':
13952 if (strcmp ("aggregate", p) == 0)
13953 category = OMP_CLAUSE_DEFAULTMAP_CATEGORY_AGGREGATE;
13954 else
13955 goto invalid_category;
13956 break;
13957
13958 case 'p':
13959 if (strcmp ("pointer", p) == 0)
13960 category = OMP_CLAUSE_DEFAULTMAP_CATEGORY_POINTER;
13961 else
13962 goto invalid_category;
13963 break;
13964
13965 case 's':
13966 if (strcmp ("scalar", p) == 0)
13967 category = OMP_CLAUSE_DEFAULTMAP_CATEGORY_SCALAR;
13968 else
13969 goto invalid_category;
13970 break;
13971
13972 default:
13973 goto invalid_category;
13974 }
13975
13976 c_parser_consume_token (parser);
13977 }
13978 parens.skip_until_found_close (parser);
13979
13980 for (c = list; c ; c = OMP_CLAUSE_CHAIN (c))
13981 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEFAULTMAP
13982 && (category == OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED
13983 || OMP_CLAUSE_DEFAULTMAP_CATEGORY (c) == category
13984 || (OMP_CLAUSE_DEFAULTMAP_CATEGORY (c)
13985 == OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED)))
13986 {
13987 enum omp_clause_defaultmap_kind cat = category;
13988 location_t loc = OMP_CLAUSE_LOCATION (c);
13989 if (cat == OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED)
13990 cat = OMP_CLAUSE_DEFAULTMAP_CATEGORY (c);
13991 p = NULL;
13992 switch (cat)
13993 {
13994 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED:
13995 p = NULL;
13996 break;
13997 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_AGGREGATE:
13998 p = "aggregate";
13999 break;
14000 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_POINTER:
14001 p = "pointer";
14002 break;
14003 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_SCALAR:
14004 p = "scalar";
14005 break;
14006 default:
14007 gcc_unreachable ();
14008 }
14009 if (p)
14010 error_at (loc, "too many %<defaultmap%> clauses with %qs category",
14011 p);
14012 else
14013 error_at (loc, "too many %<defaultmap%> clauses with unspecified "
14014 "category");
14015 break;
14016 }
14017
14018 c = build_omp_clause (loc, OMP_CLAUSE_DEFAULTMAP);
14019 OMP_CLAUSE_DEFAULTMAP_SET_KIND (c, behavior, category);
14020 OMP_CLAUSE_CHAIN (c) = list;
14021 return c;
14022
14023 out_err:
14024 parens.skip_until_found_close (parser);
14025 return list;
14026 }
14027
14028 /* OpenACC 2.0:
14029 use_device ( variable-list )
14030
14031 OpenMP 4.5:
14032 use_device_ptr ( variable-list ) */
14033
14034 static tree
14035 c_parser_omp_clause_use_device_ptr (c_parser *parser, tree list)
14036 {
14037 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_USE_DEVICE_PTR,
14038 list);
14039 }
14040
14041 /* OpenMP 5.0:
14042 use_device_addr ( variable-list ) */
14043
14044 static tree
14045 c_parser_omp_clause_use_device_addr (c_parser *parser, tree list)
14046 {
14047 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_USE_DEVICE_ADDR,
14048 list);
14049 }
14050
14051 /* OpenMP 4.5:
14052 is_device_ptr ( variable-list ) */
14053
14054 static tree
14055 c_parser_omp_clause_is_device_ptr (c_parser *parser, tree list)
14056 {
14057 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_IS_DEVICE_PTR, list);
14058 }
14059
14060 /* OpenACC:
14061 num_gangs ( expression )
14062 num_workers ( expression )
14063 vector_length ( expression ) */
14064
14065 static tree
14066 c_parser_oacc_single_int_clause (c_parser *parser, omp_clause_code code,
14067 tree list)
14068 {
14069 location_t loc = c_parser_peek_token (parser)->location;
14070
14071 matching_parens parens;
14072 if (!parens.require_open (parser))
14073 return list;
14074
14075 location_t expr_loc = c_parser_peek_token (parser)->location;
14076 c_expr expr = c_parser_expression (parser);
14077 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
14078 tree c, t = expr.value;
14079 t = c_fully_fold (t, false, NULL);
14080
14081 parens.skip_until_found_close (parser);
14082
14083 if (t == error_mark_node)
14084 return list;
14085 else if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
14086 {
14087 error_at (expr_loc, "%qs expression must be integral",
14088 omp_clause_code_name[code]);
14089 return list;
14090 }
14091
14092 /* Attempt to statically determine when the number isn't positive. */
14093 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
14094 build_int_cst (TREE_TYPE (t), 0));
14095 protected_set_expr_location (c, expr_loc);
14096 if (c == boolean_true_node)
14097 {
14098 warning_at (expr_loc, 0,
14099 "%qs value must be positive",
14100 omp_clause_code_name[code]);
14101 t = integer_one_node;
14102 }
14103
14104 check_no_duplicate_clause (list, code, omp_clause_code_name[code]);
14105
14106 c = build_omp_clause (loc, code);
14107 OMP_CLAUSE_OPERAND (c, 0) = t;
14108 OMP_CLAUSE_CHAIN (c) = list;
14109 return c;
14110 }
14111
14112 /* OpenACC:
14113
14114 gang [( gang-arg-list )]
14115 worker [( [num:] int-expr )]
14116 vector [( [length:] int-expr )]
14117
14118 where gang-arg is one of:
14119
14120 [num:] int-expr
14121 static: size-expr
14122
14123 and size-expr may be:
14124
14125 *
14126 int-expr
14127 */
14128
14129 static tree
14130 c_parser_oacc_shape_clause (c_parser *parser, location_t loc,
14131 omp_clause_code kind,
14132 const char *str, tree list)
14133 {
14134 const char *id = "num";
14135 tree ops[2] = { NULL_TREE, NULL_TREE }, c;
14136
14137 if (kind == OMP_CLAUSE_VECTOR)
14138 id = "length";
14139
14140 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
14141 {
14142 c_parser_consume_token (parser);
14143
14144 do
14145 {
14146 c_token *next = c_parser_peek_token (parser);
14147 int idx = 0;
14148
14149 /* Gang static argument. */
14150 if (kind == OMP_CLAUSE_GANG
14151 && c_parser_next_token_is_keyword (parser, RID_STATIC))
14152 {
14153 c_parser_consume_token (parser);
14154
14155 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
14156 goto cleanup_error;
14157
14158 idx = 1;
14159 if (ops[idx] != NULL_TREE)
14160 {
14161 c_parser_error (parser, "too many %<static%> arguments");
14162 goto cleanup_error;
14163 }
14164
14165 /* Check for the '*' argument. */
14166 if (c_parser_next_token_is (parser, CPP_MULT)
14167 && (c_parser_peek_2nd_token (parser)->type == CPP_COMMA
14168 || c_parser_peek_2nd_token (parser)->type
14169 == CPP_CLOSE_PAREN))
14170 {
14171 c_parser_consume_token (parser);
14172 ops[idx] = integer_minus_one_node;
14173
14174 if (c_parser_next_token_is (parser, CPP_COMMA))
14175 {
14176 c_parser_consume_token (parser);
14177 continue;
14178 }
14179 else
14180 break;
14181 }
14182 }
14183 /* Worker num: argument and vector length: arguments. */
14184 else if (c_parser_next_token_is (parser, CPP_NAME)
14185 && strcmp (id, IDENTIFIER_POINTER (next->value)) == 0
14186 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
14187 {
14188 c_parser_consume_token (parser); /* id */
14189 c_parser_consume_token (parser); /* ':' */
14190 }
14191
14192 /* Now collect the actual argument. */
14193 if (ops[idx] != NULL_TREE)
14194 {
14195 c_parser_error (parser, "unexpected argument");
14196 goto cleanup_error;
14197 }
14198
14199 location_t expr_loc = c_parser_peek_token (parser)->location;
14200 c_expr cexpr = c_parser_expr_no_commas (parser, NULL);
14201 cexpr = convert_lvalue_to_rvalue (expr_loc, cexpr, false, true);
14202 tree expr = cexpr.value;
14203 if (expr == error_mark_node)
14204 goto cleanup_error;
14205
14206 expr = c_fully_fold (expr, false, NULL);
14207
14208 /* Attempt to statically determine when the number isn't a
14209 positive integer. */
14210
14211 if (!INTEGRAL_TYPE_P (TREE_TYPE (expr)))
14212 {
14213 c_parser_error (parser, "expected integer expression");
14214 return list;
14215 }
14216
14217 tree c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, expr,
14218 build_int_cst (TREE_TYPE (expr), 0));
14219 if (c == boolean_true_node)
14220 {
14221 warning_at (loc, 0,
14222 "%qs value must be positive", str);
14223 expr = integer_one_node;
14224 }
14225
14226 ops[idx] = expr;
14227
14228 if (kind == OMP_CLAUSE_GANG
14229 && c_parser_next_token_is (parser, CPP_COMMA))
14230 {
14231 c_parser_consume_token (parser);
14232 continue;
14233 }
14234 break;
14235 }
14236 while (1);
14237
14238 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
14239 goto cleanup_error;
14240 }
14241
14242 check_no_duplicate_clause (list, kind, str);
14243
14244 c = build_omp_clause (loc, kind);
14245
14246 if (ops[1])
14247 OMP_CLAUSE_OPERAND (c, 1) = ops[1];
14248
14249 OMP_CLAUSE_OPERAND (c, 0) = ops[0];
14250 OMP_CLAUSE_CHAIN (c) = list;
14251
14252 return c;
14253
14254 cleanup_error:
14255 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 0);
14256 return list;
14257 }
14258
14259 /* OpenACC 2.5:
14260 auto
14261 finalize
14262 independent
14263 nohost
14264 seq */
14265
14266 static tree
14267 c_parser_oacc_simple_clause (location_t loc, enum omp_clause_code code,
14268 tree list)
14269 {
14270 check_no_duplicate_clause (list, code, omp_clause_code_name[code]);
14271
14272 tree c = build_omp_clause (loc, code);
14273 OMP_CLAUSE_CHAIN (c) = list;
14274
14275 return c;
14276 }
14277
14278 /* OpenACC:
14279 async [( int-expr )] */
14280
14281 static tree
14282 c_parser_oacc_clause_async (c_parser *parser, tree list)
14283 {
14284 tree c, t;
14285 location_t loc = c_parser_peek_token (parser)->location;
14286
14287 t = build_int_cst (integer_type_node, GOMP_ASYNC_NOVAL);
14288
14289 if (c_parser_peek_token (parser)->type == CPP_OPEN_PAREN)
14290 {
14291 c_parser_consume_token (parser);
14292
14293 t = c_parser_expression (parser).value;
14294 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
14295 c_parser_error (parser, "expected integer expression");
14296 else if (t == error_mark_node
14297 || !c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
14298 return list;
14299 }
14300 else
14301 t = c_fully_fold (t, false, NULL);
14302
14303 check_no_duplicate_clause (list, OMP_CLAUSE_ASYNC, "async");
14304
14305 c = build_omp_clause (loc, OMP_CLAUSE_ASYNC);
14306 OMP_CLAUSE_ASYNC_EXPR (c) = t;
14307 OMP_CLAUSE_CHAIN (c) = list;
14308 list = c;
14309
14310 return list;
14311 }
14312
14313 /* OpenACC 2.0:
14314 tile ( size-expr-list ) */
14315
14316 static tree
14317 c_parser_oacc_clause_tile (c_parser *parser, tree list)
14318 {
14319 tree c, expr = error_mark_node;
14320 location_t loc;
14321 tree tile = NULL_TREE;
14322
14323 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile");
14324 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse");
14325
14326 loc = c_parser_peek_token (parser)->location;
14327 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
14328 return list;
14329
14330 do
14331 {
14332 if (tile && !c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
14333 return list;
14334
14335 if (c_parser_next_token_is (parser, CPP_MULT)
14336 && (c_parser_peek_2nd_token (parser)->type == CPP_COMMA
14337 || c_parser_peek_2nd_token (parser)->type == CPP_CLOSE_PAREN))
14338 {
14339 c_parser_consume_token (parser);
14340 expr = integer_zero_node;
14341 }
14342 else
14343 {
14344 location_t expr_loc = c_parser_peek_token (parser)->location;
14345 c_expr cexpr = c_parser_expr_no_commas (parser, NULL);
14346 cexpr = convert_lvalue_to_rvalue (expr_loc, cexpr, false, true);
14347 expr = cexpr.value;
14348
14349 if (expr == error_mark_node)
14350 {
14351 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
14352 "expected %<)%>");
14353 return list;
14354 }
14355
14356 expr = c_fully_fold (expr, false, NULL);
14357
14358 if (!INTEGRAL_TYPE_P (TREE_TYPE (expr))
14359 || !tree_fits_shwi_p (expr)
14360 || tree_to_shwi (expr) <= 0)
14361 {
14362 error_at (expr_loc, "%<tile%> argument needs positive"
14363 " integral constant");
14364 expr = integer_zero_node;
14365 }
14366 }
14367
14368 tile = tree_cons (NULL_TREE, expr, tile);
14369 }
14370 while (c_parser_next_token_is_not (parser, CPP_CLOSE_PAREN));
14371
14372 /* Consume the trailing ')'. */
14373 c_parser_consume_token (parser);
14374
14375 c = build_omp_clause (loc, OMP_CLAUSE_TILE);
14376 tile = nreverse (tile);
14377 OMP_CLAUSE_TILE_LIST (c) = tile;
14378 OMP_CLAUSE_CHAIN (c) = list;
14379 return c;
14380 }
14381
14382 /* OpenACC:
14383 wait [( int-expr-list )] */
14384
14385 static tree
14386 c_parser_oacc_clause_wait (c_parser *parser, tree list)
14387 {
14388 location_t clause_loc = c_parser_peek_token (parser)->location;
14389
14390 if (c_parser_peek_token (parser)->type == CPP_OPEN_PAREN)
14391 list = c_parser_oacc_wait_list (parser, clause_loc, list);
14392 else
14393 {
14394 tree c = build_omp_clause (clause_loc, OMP_CLAUSE_WAIT);
14395
14396 OMP_CLAUSE_DECL (c) = build_int_cst (integer_type_node, GOMP_ASYNC_NOVAL);
14397 OMP_CLAUSE_CHAIN (c) = list;
14398 list = c;
14399 }
14400
14401 return list;
14402 }
14403
14404
14405 /* OpenMP 5.0:
14406 order ( concurrent ) */
14407
14408 static tree
14409 c_parser_omp_clause_order (c_parser *parser, tree list)
14410 {
14411 location_t loc = c_parser_peek_token (parser)->location;
14412 tree c;
14413 const char *p;
14414
14415 matching_parens parens;
14416 if (!parens.require_open (parser))
14417 return list;
14418 if (!c_parser_next_token_is (parser, CPP_NAME))
14419 {
14420 c_parser_error (parser, "expected %<concurrent%>");
14421 goto out_err;
14422 }
14423 p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
14424 if (strcmp (p, "concurrent") != 0)
14425 {
14426 c_parser_error (parser, "expected %<concurrent%>");
14427 goto out_err;
14428 }
14429 c_parser_consume_token (parser);
14430 parens.skip_until_found_close (parser);
14431 /* check_no_duplicate_clause (list, OMP_CLAUSE_ORDER, "order"); */
14432 c = build_omp_clause (loc, OMP_CLAUSE_ORDER);
14433 OMP_CLAUSE_CHAIN (c) = list;
14434 return c;
14435
14436 out_err:
14437 parens.skip_until_found_close (parser);
14438 return list;
14439 }
14440
14441
14442 /* OpenMP 5.0:
14443 bind ( teams | parallel | thread ) */
14444
14445 static tree
14446 c_parser_omp_clause_bind (c_parser *parser, tree list)
14447 {
14448 location_t loc = c_parser_peek_token (parser)->location;
14449 tree c;
14450 const char *p;
14451 enum omp_clause_bind_kind kind = OMP_CLAUSE_BIND_THREAD;
14452
14453 matching_parens parens;
14454 if (!parens.require_open (parser))
14455 return list;
14456 if (!c_parser_next_token_is (parser, CPP_NAME))
14457 {
14458 invalid:
14459 c_parser_error (parser,
14460 "expected %<teams%>, %<parallel%> or %<thread%>");
14461 parens.skip_until_found_close (parser);
14462 return list;
14463 }
14464 p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
14465 if (strcmp (p, "teams") == 0)
14466 kind = OMP_CLAUSE_BIND_TEAMS;
14467 else if (strcmp (p, "parallel") == 0)
14468 kind = OMP_CLAUSE_BIND_PARALLEL;
14469 else if (strcmp (p, "thread") != 0)
14470 goto invalid;
14471 c_parser_consume_token (parser);
14472 parens.skip_until_found_close (parser);
14473 /* check_no_duplicate_clause (list, OMP_CLAUSE_BIND, "bind"); */
14474 c = build_omp_clause (loc, OMP_CLAUSE_BIND);
14475 OMP_CLAUSE_BIND_KIND (c) = kind;
14476 OMP_CLAUSE_CHAIN (c) = list;
14477 return c;
14478 }
14479
14480
14481 /* OpenMP 2.5:
14482 ordered
14483
14484 OpenMP 4.5:
14485 ordered ( constant-expression ) */
14486
14487 static tree
14488 c_parser_omp_clause_ordered (c_parser *parser, tree list)
14489 {
14490 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED, "ordered");
14491
14492 tree c, num = NULL_TREE;
14493 HOST_WIDE_INT n;
14494 location_t loc = c_parser_peek_token (parser)->location;
14495 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
14496 {
14497 matching_parens parens;
14498 parens.consume_open (parser);
14499 num = c_parser_expr_no_commas (parser, NULL).value;
14500 parens.skip_until_found_close (parser);
14501 }
14502 if (num == error_mark_node)
14503 return list;
14504 if (num)
14505 {
14506 mark_exp_read (num);
14507 num = c_fully_fold (num, false, NULL);
14508 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
14509 || !tree_fits_shwi_p (num)
14510 || (n = tree_to_shwi (num)) <= 0
14511 || (int) n != n)
14512 {
14513 error_at (loc, "ordered argument needs positive "
14514 "constant integer expression");
14515 return list;
14516 }
14517 }
14518 c = build_omp_clause (loc, OMP_CLAUSE_ORDERED);
14519 OMP_CLAUSE_ORDERED_EXPR (c) = num;
14520 OMP_CLAUSE_CHAIN (c) = list;
14521 return c;
14522 }
14523
14524 /* OpenMP 2.5:
14525 private ( variable-list ) */
14526
14527 static tree
14528 c_parser_omp_clause_private (c_parser *parser, tree list)
14529 {
14530 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_PRIVATE, list);
14531 }
14532
14533 /* OpenMP 2.5:
14534 reduction ( reduction-operator : variable-list )
14535
14536 reduction-operator:
14537 One of: + * - & ^ | && ||
14538
14539 OpenMP 3.1:
14540
14541 reduction-operator:
14542 One of: + * - & ^ | && || max min
14543
14544 OpenMP 4.0:
14545
14546 reduction-operator:
14547 One of: + * - & ^ | && ||
14548 identifier
14549
14550 OpenMP 5.0:
14551 reduction ( reduction-modifier, reduction-operator : variable-list )
14552 in_reduction ( reduction-operator : variable-list )
14553 task_reduction ( reduction-operator : variable-list ) */
14554
14555 static tree
14556 c_parser_omp_clause_reduction (c_parser *parser, enum omp_clause_code kind,
14557 bool is_omp, tree list)
14558 {
14559 location_t clause_loc = c_parser_peek_token (parser)->location;
14560 matching_parens parens;
14561 if (parens.require_open (parser))
14562 {
14563 bool task = false;
14564 bool inscan = false;
14565 enum tree_code code = ERROR_MARK;
14566 tree reduc_id = NULL_TREE;
14567
14568 if (kind == OMP_CLAUSE_REDUCTION && is_omp)
14569 {
14570 if (c_parser_next_token_is_keyword (parser, RID_DEFAULT)
14571 && c_parser_peek_2nd_token (parser)->type == CPP_COMMA)
14572 {
14573 c_parser_consume_token (parser);
14574 c_parser_consume_token (parser);
14575 }
14576 else if (c_parser_next_token_is (parser, CPP_NAME)
14577 && c_parser_peek_2nd_token (parser)->type == CPP_COMMA)
14578 {
14579 const char *p
14580 = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
14581 if (strcmp (p, "task") == 0)
14582 task = true;
14583 else if (strcmp (p, "inscan") == 0)
14584 inscan = true;
14585 if (task || inscan)
14586 {
14587 c_parser_consume_token (parser);
14588 c_parser_consume_token (parser);
14589 }
14590 }
14591 }
14592
14593 switch (c_parser_peek_token (parser)->type)
14594 {
14595 case CPP_PLUS:
14596 code = PLUS_EXPR;
14597 break;
14598 case CPP_MULT:
14599 code = MULT_EXPR;
14600 break;
14601 case CPP_MINUS:
14602 code = MINUS_EXPR;
14603 break;
14604 case CPP_AND:
14605 code = BIT_AND_EXPR;
14606 break;
14607 case CPP_XOR:
14608 code = BIT_XOR_EXPR;
14609 break;
14610 case CPP_OR:
14611 code = BIT_IOR_EXPR;
14612 break;
14613 case CPP_AND_AND:
14614 code = TRUTH_ANDIF_EXPR;
14615 break;
14616 case CPP_OR_OR:
14617 code = TRUTH_ORIF_EXPR;
14618 break;
14619 case CPP_NAME:
14620 {
14621 const char *p
14622 = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
14623 if (strcmp (p, "min") == 0)
14624 {
14625 code = MIN_EXPR;
14626 break;
14627 }
14628 if (strcmp (p, "max") == 0)
14629 {
14630 code = MAX_EXPR;
14631 break;
14632 }
14633 reduc_id = c_parser_peek_token (parser)->value;
14634 break;
14635 }
14636 default:
14637 c_parser_error (parser,
14638 "expected %<+%>, %<*%>, %<-%>, %<&%>, "
14639 "%<^%>, %<|%>, %<&&%>, %<||%> or identifier");
14640 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 0);
14641 return list;
14642 }
14643 c_parser_consume_token (parser);
14644 reduc_id = c_omp_reduction_id (code, reduc_id);
14645 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
14646 {
14647 tree nl, c;
14648
14649 nl = c_parser_omp_variable_list (parser, clause_loc, kind, list);
14650 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
14651 {
14652 tree d = OMP_CLAUSE_DECL (c), type;
14653 if (TREE_CODE (d) != TREE_LIST)
14654 type = TREE_TYPE (d);
14655 else
14656 {
14657 int cnt = 0;
14658 tree t;
14659 for (t = d; TREE_CODE (t) == TREE_LIST; t = TREE_CHAIN (t))
14660 cnt++;
14661 type = TREE_TYPE (t);
14662 while (cnt > 0)
14663 {
14664 if (TREE_CODE (type) != POINTER_TYPE
14665 && TREE_CODE (type) != ARRAY_TYPE)
14666 break;
14667 type = TREE_TYPE (type);
14668 cnt--;
14669 }
14670 }
14671 while (TREE_CODE (type) == ARRAY_TYPE)
14672 type = TREE_TYPE (type);
14673 OMP_CLAUSE_REDUCTION_CODE (c) = code;
14674 if (task)
14675 OMP_CLAUSE_REDUCTION_TASK (c) = 1;
14676 else if (inscan)
14677 OMP_CLAUSE_REDUCTION_INSCAN (c) = 1;
14678 if (code == ERROR_MARK
14679 || !(INTEGRAL_TYPE_P (type)
14680 || TREE_CODE (type) == REAL_TYPE
14681 || TREE_CODE (type) == COMPLEX_TYPE))
14682 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c)
14683 = c_omp_reduction_lookup (reduc_id,
14684 TYPE_MAIN_VARIANT (type));
14685 }
14686
14687 list = nl;
14688 }
14689 parens.skip_until_found_close (parser);
14690 }
14691 return list;
14692 }
14693
14694 /* OpenMP 2.5:
14695 schedule ( schedule-kind )
14696 schedule ( schedule-kind , expression )
14697
14698 schedule-kind:
14699 static | dynamic | guided | runtime | auto
14700
14701 OpenMP 4.5:
14702 schedule ( schedule-modifier : schedule-kind )
14703 schedule ( schedule-modifier [ , schedule-modifier ] : schedule-kind , expression )
14704
14705 schedule-modifier:
14706 simd
14707 monotonic
14708 nonmonotonic */
14709
14710 static tree
14711 c_parser_omp_clause_schedule (c_parser *parser, tree list)
14712 {
14713 tree c, t;
14714 location_t loc = c_parser_peek_token (parser)->location;
14715 int modifiers = 0, nmodifiers = 0;
14716
14717 matching_parens parens;
14718 if (!parens.require_open (parser))
14719 return list;
14720
14721 c = build_omp_clause (loc, OMP_CLAUSE_SCHEDULE);
14722
14723 while (c_parser_next_token_is (parser, CPP_NAME))
14724 {
14725 tree kind = c_parser_peek_token (parser)->value;
14726 const char *p = IDENTIFIER_POINTER (kind);
14727 if (strcmp ("simd", p) == 0)
14728 OMP_CLAUSE_SCHEDULE_SIMD (c) = 1;
14729 else if (strcmp ("monotonic", p) == 0)
14730 modifiers |= OMP_CLAUSE_SCHEDULE_MONOTONIC;
14731 else if (strcmp ("nonmonotonic", p) == 0)
14732 modifiers |= OMP_CLAUSE_SCHEDULE_NONMONOTONIC;
14733 else
14734 break;
14735 c_parser_consume_token (parser);
14736 if (nmodifiers++ == 0
14737 && c_parser_next_token_is (parser, CPP_COMMA))
14738 c_parser_consume_token (parser);
14739 else
14740 {
14741 c_parser_require (parser, CPP_COLON, "expected %<:%>");
14742 break;
14743 }
14744 }
14745
14746 if ((modifiers & (OMP_CLAUSE_SCHEDULE_MONOTONIC
14747 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
14748 == (OMP_CLAUSE_SCHEDULE_MONOTONIC
14749 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
14750 {
14751 error_at (loc, "both %<monotonic%> and %<nonmonotonic%> modifiers "
14752 "specified");
14753 modifiers = 0;
14754 }
14755
14756 if (c_parser_next_token_is (parser, CPP_NAME))
14757 {
14758 tree kind = c_parser_peek_token (parser)->value;
14759 const char *p = IDENTIFIER_POINTER (kind);
14760
14761 switch (p[0])
14762 {
14763 case 'd':
14764 if (strcmp ("dynamic", p) != 0)
14765 goto invalid_kind;
14766 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
14767 break;
14768
14769 case 'g':
14770 if (strcmp ("guided", p) != 0)
14771 goto invalid_kind;
14772 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
14773 break;
14774
14775 case 'r':
14776 if (strcmp ("runtime", p) != 0)
14777 goto invalid_kind;
14778 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
14779 break;
14780
14781 default:
14782 goto invalid_kind;
14783 }
14784 }
14785 else if (c_parser_next_token_is_keyword (parser, RID_STATIC))
14786 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
14787 else if (c_parser_next_token_is_keyword (parser, RID_AUTO))
14788 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
14789 else
14790 goto invalid_kind;
14791
14792 c_parser_consume_token (parser);
14793 if (c_parser_next_token_is (parser, CPP_COMMA))
14794 {
14795 location_t here;
14796 c_parser_consume_token (parser);
14797
14798 here = c_parser_peek_token (parser)->location;
14799 c_expr expr = c_parser_expr_no_commas (parser, NULL);
14800 expr = convert_lvalue_to_rvalue (here, expr, false, true);
14801 t = expr.value;
14802 t = c_fully_fold (t, false, NULL);
14803
14804 if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
14805 error_at (here, "schedule %<runtime%> does not take "
14806 "a %<chunk_size%> parameter");
14807 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
14808 error_at (here,
14809 "schedule %<auto%> does not take "
14810 "a %<chunk_size%> parameter");
14811 else if (TREE_CODE (TREE_TYPE (t)) == INTEGER_TYPE)
14812 {
14813 /* Attempt to statically determine when the number isn't
14814 positive. */
14815 tree s = fold_build2_loc (loc, LE_EXPR, boolean_type_node, t,
14816 build_int_cst (TREE_TYPE (t), 0));
14817 protected_set_expr_location (s, loc);
14818 if (s == boolean_true_node)
14819 {
14820 warning_at (loc, 0,
14821 "chunk size value must be positive");
14822 t = integer_one_node;
14823 }
14824 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
14825 }
14826 else
14827 c_parser_error (parser, "expected integer expression");
14828
14829 parens.skip_until_found_close (parser);
14830 }
14831 else
14832 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
14833 "expected %<,%> or %<)%>");
14834
14835 OMP_CLAUSE_SCHEDULE_KIND (c)
14836 = (enum omp_clause_schedule_kind)
14837 (OMP_CLAUSE_SCHEDULE_KIND (c) | modifiers);
14838
14839 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule");
14840 OMP_CLAUSE_CHAIN (c) = list;
14841 return c;
14842
14843 invalid_kind:
14844 c_parser_error (parser, "invalid schedule kind");
14845 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 0);
14846 return list;
14847 }
14848
14849 /* OpenMP 2.5:
14850 shared ( variable-list ) */
14851
14852 static tree
14853 c_parser_omp_clause_shared (c_parser *parser, tree list)
14854 {
14855 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_SHARED, list);
14856 }
14857
14858 /* OpenMP 3.0:
14859 untied */
14860
14861 static tree
14862 c_parser_omp_clause_untied (c_parser *parser ATTRIBUTE_UNUSED, tree list)
14863 {
14864 tree c;
14865
14866 /* FIXME: Should we allow duplicates? */
14867 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied");
14868
14869 c = build_omp_clause (c_parser_peek_token (parser)->location,
14870 OMP_CLAUSE_UNTIED);
14871 OMP_CLAUSE_CHAIN (c) = list;
14872
14873 return c;
14874 }
14875
14876 /* OpenMP 4.0:
14877 inbranch
14878 notinbranch */
14879
14880 static tree
14881 c_parser_omp_clause_branch (c_parser *parser ATTRIBUTE_UNUSED,
14882 enum omp_clause_code code, tree list)
14883 {
14884 check_no_duplicate_clause (list, code, omp_clause_code_name[code]);
14885
14886 tree c = build_omp_clause (c_parser_peek_token (parser)->location, code);
14887 OMP_CLAUSE_CHAIN (c) = list;
14888
14889 return c;
14890 }
14891
14892 /* OpenMP 4.0:
14893 parallel
14894 for
14895 sections
14896 taskgroup */
14897
14898 static tree
14899 c_parser_omp_clause_cancelkind (c_parser *parser ATTRIBUTE_UNUSED,
14900 enum omp_clause_code code, tree list)
14901 {
14902 tree c = build_omp_clause (c_parser_peek_token (parser)->location, code);
14903 OMP_CLAUSE_CHAIN (c) = list;
14904
14905 return c;
14906 }
14907
14908 /* OpenMP 4.5:
14909 nogroup */
14910
14911 static tree
14912 c_parser_omp_clause_nogroup (c_parser *parser ATTRIBUTE_UNUSED, tree list)
14913 {
14914 check_no_duplicate_clause (list, OMP_CLAUSE_NOGROUP, "nogroup");
14915 tree c = build_omp_clause (c_parser_peek_token (parser)->location,
14916 OMP_CLAUSE_NOGROUP);
14917 OMP_CLAUSE_CHAIN (c) = list;
14918 return c;
14919 }
14920
14921 /* OpenMP 4.5:
14922 simd
14923 threads */
14924
14925 static tree
14926 c_parser_omp_clause_orderedkind (c_parser *parser ATTRIBUTE_UNUSED,
14927 enum omp_clause_code code, tree list)
14928 {
14929 check_no_duplicate_clause (list, code, omp_clause_code_name[code]);
14930 tree c = build_omp_clause (c_parser_peek_token (parser)->location, code);
14931 OMP_CLAUSE_CHAIN (c) = list;
14932 return c;
14933 }
14934
14935 /* OpenMP 4.0:
14936 num_teams ( expression ) */
14937
14938 static tree
14939 c_parser_omp_clause_num_teams (c_parser *parser, tree list)
14940 {
14941 location_t num_teams_loc = c_parser_peek_token (parser)->location;
14942 matching_parens parens;
14943 if (parens.require_open (parser))
14944 {
14945 location_t expr_loc = c_parser_peek_token (parser)->location;
14946 c_expr expr = c_parser_expr_no_commas (parser, NULL);
14947 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
14948 tree c, t = expr.value;
14949 t = c_fully_fold (t, false, NULL);
14950
14951 parens.skip_until_found_close (parser);
14952
14953 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
14954 {
14955 c_parser_error (parser, "expected integer expression");
14956 return list;
14957 }
14958
14959 /* Attempt to statically determine when the number isn't positive. */
14960 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
14961 build_int_cst (TREE_TYPE (t), 0));
14962 protected_set_expr_location (c, expr_loc);
14963 if (c == boolean_true_node)
14964 {
14965 warning_at (expr_loc, 0, "%<num_teams%> value must be positive");
14966 t = integer_one_node;
14967 }
14968
14969 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TEAMS, "num_teams");
14970
14971 c = build_omp_clause (num_teams_loc, OMP_CLAUSE_NUM_TEAMS);
14972 OMP_CLAUSE_NUM_TEAMS_EXPR (c) = t;
14973 OMP_CLAUSE_CHAIN (c) = list;
14974 list = c;
14975 }
14976
14977 return list;
14978 }
14979
14980 /* OpenMP 4.0:
14981 thread_limit ( expression ) */
14982
14983 static tree
14984 c_parser_omp_clause_thread_limit (c_parser *parser, tree list)
14985 {
14986 location_t num_thread_limit_loc = c_parser_peek_token (parser)->location;
14987 matching_parens parens;
14988 if (parens.require_open (parser))
14989 {
14990 location_t expr_loc = c_parser_peek_token (parser)->location;
14991 c_expr expr = c_parser_expr_no_commas (parser, NULL);
14992 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
14993 tree c, t = expr.value;
14994 t = c_fully_fold (t, false, NULL);
14995
14996 parens.skip_until_found_close (parser);
14997
14998 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
14999 {
15000 c_parser_error (parser, "expected integer expression");
15001 return list;
15002 }
15003
15004 /* Attempt to statically determine when the number isn't positive. */
15005 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
15006 build_int_cst (TREE_TYPE (t), 0));
15007 protected_set_expr_location (c, expr_loc);
15008 if (c == boolean_true_node)
15009 {
15010 warning_at (expr_loc, 0, "%<thread_limit%> value must be positive");
15011 t = integer_one_node;
15012 }
15013
15014 check_no_duplicate_clause (list, OMP_CLAUSE_THREAD_LIMIT,
15015 "thread_limit");
15016
15017 c = build_omp_clause (num_thread_limit_loc, OMP_CLAUSE_THREAD_LIMIT);
15018 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
15019 OMP_CLAUSE_CHAIN (c) = list;
15020 list = c;
15021 }
15022
15023 return list;
15024 }
15025
15026 /* OpenMP 4.0:
15027 aligned ( variable-list )
15028 aligned ( variable-list : constant-expression ) */
15029
15030 static tree
15031 c_parser_omp_clause_aligned (c_parser *parser, tree list)
15032 {
15033 location_t clause_loc = c_parser_peek_token (parser)->location;
15034 tree nl, c;
15035
15036 matching_parens parens;
15037 if (!parens.require_open (parser))
15038 return list;
15039
15040 nl = c_parser_omp_variable_list (parser, clause_loc,
15041 OMP_CLAUSE_ALIGNED, list);
15042
15043 if (c_parser_next_token_is (parser, CPP_COLON))
15044 {
15045 c_parser_consume_token (parser);
15046 location_t expr_loc = c_parser_peek_token (parser)->location;
15047 c_expr expr = c_parser_expr_no_commas (parser, NULL);
15048 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
15049 tree alignment = expr.value;
15050 alignment = c_fully_fold (alignment, false, NULL);
15051 if (TREE_CODE (alignment) != INTEGER_CST
15052 || !INTEGRAL_TYPE_P (TREE_TYPE (alignment))
15053 || tree_int_cst_sgn (alignment) != 1)
15054 {
15055 error_at (clause_loc, "%<aligned%> clause alignment expression must "
15056 "be positive constant integer expression");
15057 alignment = NULL_TREE;
15058 }
15059
15060 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
15061 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = alignment;
15062 }
15063
15064 parens.skip_until_found_close (parser);
15065 return nl;
15066 }
15067
15068 /* OpenMP 4.0:
15069 linear ( variable-list )
15070 linear ( variable-list : expression )
15071
15072 OpenMP 4.5:
15073 linear ( modifier ( variable-list ) )
15074 linear ( modifier ( variable-list ) : expression ) */
15075
15076 static tree
15077 c_parser_omp_clause_linear (c_parser *parser, tree list)
15078 {
15079 location_t clause_loc = c_parser_peek_token (parser)->location;
15080 tree nl, c, step;
15081 enum omp_clause_linear_kind kind = OMP_CLAUSE_LINEAR_DEFAULT;
15082
15083 matching_parens parens;
15084 if (!parens.require_open (parser))
15085 return list;
15086
15087 if (c_parser_next_token_is (parser, CPP_NAME))
15088 {
15089 c_token *tok = c_parser_peek_token (parser);
15090 const char *p = IDENTIFIER_POINTER (tok->value);
15091 if (strcmp ("val", p) == 0)
15092 kind = OMP_CLAUSE_LINEAR_VAL;
15093 if (c_parser_peek_2nd_token (parser)->type != CPP_OPEN_PAREN)
15094 kind = OMP_CLAUSE_LINEAR_DEFAULT;
15095 if (kind != OMP_CLAUSE_LINEAR_DEFAULT)
15096 {
15097 c_parser_consume_token (parser);
15098 c_parser_consume_token (parser);
15099 }
15100 }
15101
15102 nl = c_parser_omp_variable_list (parser, clause_loc,
15103 OMP_CLAUSE_LINEAR, list);
15104
15105 if (kind != OMP_CLAUSE_LINEAR_DEFAULT)
15106 parens.skip_until_found_close (parser);
15107
15108 if (c_parser_next_token_is (parser, CPP_COLON))
15109 {
15110 c_parser_consume_token (parser);
15111 location_t expr_loc = c_parser_peek_token (parser)->location;
15112 c_expr expr = c_parser_expr_no_commas (parser, NULL);
15113 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
15114 step = expr.value;
15115 step = c_fully_fold (step, false, NULL);
15116 if (!INTEGRAL_TYPE_P (TREE_TYPE (step)))
15117 {
15118 error_at (clause_loc, "%<linear%> clause step expression must "
15119 "be integral");
15120 step = integer_one_node;
15121 }
15122
15123 }
15124 else
15125 step = integer_one_node;
15126
15127 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
15128 {
15129 OMP_CLAUSE_LINEAR_STEP (c) = step;
15130 OMP_CLAUSE_LINEAR_KIND (c) = kind;
15131 }
15132
15133 parens.skip_until_found_close (parser);
15134 return nl;
15135 }
15136
15137 /* OpenMP 5.0:
15138 nontemporal ( variable-list ) */
15139
15140 static tree
15141 c_parser_omp_clause_nontemporal (c_parser *parser, tree list)
15142 {
15143 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_NONTEMPORAL, list);
15144 }
15145
15146 /* OpenMP 4.0:
15147 safelen ( constant-expression ) */
15148
15149 static tree
15150 c_parser_omp_clause_safelen (c_parser *parser, tree list)
15151 {
15152 location_t clause_loc = c_parser_peek_token (parser)->location;
15153 tree c, t;
15154
15155 matching_parens parens;
15156 if (!parens.require_open (parser))
15157 return list;
15158
15159 location_t expr_loc = c_parser_peek_token (parser)->location;
15160 c_expr expr = c_parser_expr_no_commas (parser, NULL);
15161 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
15162 t = expr.value;
15163 t = c_fully_fold (t, false, NULL);
15164 if (TREE_CODE (t) != INTEGER_CST
15165 || !INTEGRAL_TYPE_P (TREE_TYPE (t))
15166 || tree_int_cst_sgn (t) != 1)
15167 {
15168 error_at (clause_loc, "%<safelen%> clause expression must "
15169 "be positive constant integer expression");
15170 t = NULL_TREE;
15171 }
15172
15173 parens.skip_until_found_close (parser);
15174 if (t == NULL_TREE || t == error_mark_node)
15175 return list;
15176
15177 check_no_duplicate_clause (list, OMP_CLAUSE_SAFELEN, "safelen");
15178
15179 c = build_omp_clause (clause_loc, OMP_CLAUSE_SAFELEN);
15180 OMP_CLAUSE_SAFELEN_EXPR (c) = t;
15181 OMP_CLAUSE_CHAIN (c) = list;
15182 return c;
15183 }
15184
15185 /* OpenMP 4.0:
15186 simdlen ( constant-expression ) */
15187
15188 static tree
15189 c_parser_omp_clause_simdlen (c_parser *parser, tree list)
15190 {
15191 location_t clause_loc = c_parser_peek_token (parser)->location;
15192 tree c, t;
15193
15194 matching_parens parens;
15195 if (!parens.require_open (parser))
15196 return list;
15197
15198 location_t expr_loc = c_parser_peek_token (parser)->location;
15199 c_expr expr = c_parser_expr_no_commas (parser, NULL);
15200 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
15201 t = expr.value;
15202 t = c_fully_fold (t, false, NULL);
15203 if (TREE_CODE (t) != INTEGER_CST
15204 || !INTEGRAL_TYPE_P (TREE_TYPE (t))
15205 || tree_int_cst_sgn (t) != 1)
15206 {
15207 error_at (clause_loc, "%<simdlen%> clause expression must "
15208 "be positive constant integer expression");
15209 t = NULL_TREE;
15210 }
15211
15212 parens.skip_until_found_close (parser);
15213 if (t == NULL_TREE || t == error_mark_node)
15214 return list;
15215
15216 check_no_duplicate_clause (list, OMP_CLAUSE_SIMDLEN, "simdlen");
15217
15218 c = build_omp_clause (clause_loc, OMP_CLAUSE_SIMDLEN);
15219 OMP_CLAUSE_SIMDLEN_EXPR (c) = t;
15220 OMP_CLAUSE_CHAIN (c) = list;
15221 return c;
15222 }
15223
15224 /* OpenMP 4.5:
15225 vec:
15226 identifier [+/- integer]
15227 vec , identifier [+/- integer]
15228 */
15229
15230 static tree
15231 c_parser_omp_clause_depend_sink (c_parser *parser, location_t clause_loc,
15232 tree list)
15233 {
15234 tree vec = NULL;
15235 if (c_parser_next_token_is_not (parser, CPP_NAME)
15236 || c_parser_peek_token (parser)->id_kind != C_ID_ID)
15237 {
15238 c_parser_error (parser, "expected identifier");
15239 return list;
15240 }
15241
15242 while (c_parser_next_token_is (parser, CPP_NAME)
15243 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
15244 {
15245 tree t = lookup_name (c_parser_peek_token (parser)->value);
15246 tree addend = NULL;
15247
15248 if (t == NULL_TREE)
15249 {
15250 undeclared_variable (c_parser_peek_token (parser)->location,
15251 c_parser_peek_token (parser)->value);
15252 t = error_mark_node;
15253 }
15254
15255 c_parser_consume_token (parser);
15256
15257 bool neg = false;
15258 if (c_parser_next_token_is (parser, CPP_MINUS))
15259 neg = true;
15260 else if (!c_parser_next_token_is (parser, CPP_PLUS))
15261 {
15262 addend = integer_zero_node;
15263 neg = false;
15264 goto add_to_vector;
15265 }
15266 c_parser_consume_token (parser);
15267
15268 if (c_parser_next_token_is_not (parser, CPP_NUMBER))
15269 {
15270 c_parser_error (parser, "expected integer");
15271 return list;
15272 }
15273
15274 addend = c_parser_peek_token (parser)->value;
15275 if (TREE_CODE (addend) != INTEGER_CST)
15276 {
15277 c_parser_error (parser, "expected integer");
15278 return list;
15279 }
15280 c_parser_consume_token (parser);
15281
15282 add_to_vector:
15283 if (t != error_mark_node)
15284 {
15285 vec = tree_cons (addend, t, vec);
15286 if (neg)
15287 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (vec) = 1;
15288 }
15289
15290 if (c_parser_next_token_is_not (parser, CPP_COMMA))
15291 break;
15292
15293 c_parser_consume_token (parser);
15294 }
15295
15296 if (vec == NULL_TREE)
15297 return list;
15298
15299 tree u = build_omp_clause (clause_loc, OMP_CLAUSE_DEPEND);
15300 OMP_CLAUSE_DEPEND_KIND (u) = OMP_CLAUSE_DEPEND_SINK;
15301 OMP_CLAUSE_DECL (u) = nreverse (vec);
15302 OMP_CLAUSE_CHAIN (u) = list;
15303 return u;
15304 }
15305
15306 /* OpenMP 5.0:
15307 iterators ( iterators-definition )
15308
15309 iterators-definition:
15310 iterator-specifier
15311 iterator-specifier , iterators-definition
15312
15313 iterator-specifier:
15314 identifier = range-specification
15315 iterator-type identifier = range-specification
15316
15317 range-specification:
15318 begin : end
15319 begin : end : step */
15320
15321 static tree
15322 c_parser_omp_iterators (c_parser *parser)
15323 {
15324 tree ret = NULL_TREE, *last = &ret;
15325 c_parser_consume_token (parser);
15326
15327 push_scope ();
15328
15329 matching_parens parens;
15330 if (!parens.require_open (parser))
15331 return error_mark_node;
15332
15333 do
15334 {
15335 tree iter_type = NULL_TREE, type_expr = NULL_TREE;
15336 if (c_parser_next_tokens_start_typename (parser, cla_prefer_id))
15337 {
15338 struct c_type_name *type = c_parser_type_name (parser);
15339 if (type != NULL)
15340 iter_type = groktypename (type, &type_expr, NULL);
15341 }
15342 if (iter_type == NULL_TREE)
15343 iter_type = integer_type_node;
15344
15345 location_t loc = c_parser_peek_token (parser)->location;
15346 if (!c_parser_next_token_is (parser, CPP_NAME))
15347 {
15348 c_parser_error (parser, "expected identifier");
15349 break;
15350 }
15351
15352 tree id = c_parser_peek_token (parser)->value;
15353 c_parser_consume_token (parser);
15354
15355 if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
15356 break;
15357
15358 location_t eloc = c_parser_peek_token (parser)->location;
15359 c_expr expr = c_parser_expr_no_commas (parser, NULL);
15360 expr = convert_lvalue_to_rvalue (eloc, expr, true, false);
15361 tree begin = expr.value;
15362
15363 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
15364 break;
15365
15366 eloc = c_parser_peek_token (parser)->location;
15367 expr = c_parser_expr_no_commas (parser, NULL);
15368 expr = convert_lvalue_to_rvalue (eloc, expr, true, false);
15369 tree end = expr.value;
15370
15371 tree step = integer_one_node;
15372 if (c_parser_next_token_is (parser, CPP_COLON))
15373 {
15374 c_parser_consume_token (parser);
15375 eloc = c_parser_peek_token (parser)->location;
15376 expr = c_parser_expr_no_commas (parser, NULL);
15377 expr = convert_lvalue_to_rvalue (eloc, expr, true, false);
15378 step = expr.value;
15379 }
15380
15381 tree iter_var = build_decl (loc, VAR_DECL, id, iter_type);
15382 DECL_ARTIFICIAL (iter_var) = 1;
15383 DECL_CONTEXT (iter_var) = current_function_decl;
15384 pushdecl (iter_var);
15385
15386 *last = make_tree_vec (6);
15387 TREE_VEC_ELT (*last, 0) = iter_var;
15388 TREE_VEC_ELT (*last, 1) = begin;
15389 TREE_VEC_ELT (*last, 2) = end;
15390 TREE_VEC_ELT (*last, 3) = step;
15391 last = &TREE_CHAIN (*last);
15392
15393 if (c_parser_next_token_is (parser, CPP_COMMA))
15394 {
15395 c_parser_consume_token (parser);
15396 continue;
15397 }
15398 break;
15399 }
15400 while (1);
15401
15402 parens.skip_until_found_close (parser);
15403 return ret ? ret : error_mark_node;
15404 }
15405
15406 /* OpenMP 4.0:
15407 depend ( depend-kind: variable-list )
15408
15409 depend-kind:
15410 in | out | inout
15411
15412 OpenMP 4.5:
15413 depend ( source )
15414
15415 depend ( sink : vec )
15416
15417 OpenMP 5.0:
15418 depend ( depend-modifier , depend-kind: variable-list )
15419
15420 depend-kind:
15421 in | out | inout | mutexinoutset | depobj
15422
15423 depend-modifier:
15424 iterator ( iterators-definition ) */
15425
15426 static tree
15427 c_parser_omp_clause_depend (c_parser *parser, tree list)
15428 {
15429 location_t clause_loc = c_parser_peek_token (parser)->location;
15430 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_LAST;
15431 tree nl, c, iterators = NULL_TREE;
15432
15433 matching_parens parens;
15434 if (!parens.require_open (parser))
15435 return list;
15436
15437 do
15438 {
15439 if (c_parser_next_token_is_not (parser, CPP_NAME))
15440 goto invalid_kind;
15441
15442 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
15443 if (strcmp ("iterator", p) == 0 && iterators == NULL_TREE)
15444 {
15445 iterators = c_parser_omp_iterators (parser);
15446 c_parser_require (parser, CPP_COMMA, "expected %<,%>");
15447 continue;
15448 }
15449 if (strcmp ("in", p) == 0)
15450 kind = OMP_CLAUSE_DEPEND_IN;
15451 else if (strcmp ("inout", p) == 0)
15452 kind = OMP_CLAUSE_DEPEND_INOUT;
15453 else if (strcmp ("mutexinoutset", p) == 0)
15454 kind = OMP_CLAUSE_DEPEND_MUTEXINOUTSET;
15455 else if (strcmp ("out", p) == 0)
15456 kind = OMP_CLAUSE_DEPEND_OUT;
15457 else if (strcmp ("depobj", p) == 0)
15458 kind = OMP_CLAUSE_DEPEND_DEPOBJ;
15459 else if (strcmp ("sink", p) == 0)
15460 kind = OMP_CLAUSE_DEPEND_SINK;
15461 else if (strcmp ("source", p) == 0)
15462 kind = OMP_CLAUSE_DEPEND_SOURCE;
15463 else
15464 goto invalid_kind;
15465 break;
15466 }
15467 while (1);
15468
15469 c_parser_consume_token (parser);
15470
15471 if (iterators
15472 && (kind == OMP_CLAUSE_DEPEND_SOURCE || kind == OMP_CLAUSE_DEPEND_SINK))
15473 {
15474 pop_scope ();
15475 error_at (clause_loc, "%<iterator%> modifier incompatible with %qs",
15476 kind == OMP_CLAUSE_DEPEND_SOURCE ? "source" : "sink");
15477 iterators = NULL_TREE;
15478 }
15479
15480 if (kind == OMP_CLAUSE_DEPEND_SOURCE)
15481 {
15482 c = build_omp_clause (clause_loc, OMP_CLAUSE_DEPEND);
15483 OMP_CLAUSE_DEPEND_KIND (c) = kind;
15484 OMP_CLAUSE_DECL (c) = NULL_TREE;
15485 OMP_CLAUSE_CHAIN (c) = list;
15486 parens.skip_until_found_close (parser);
15487 return c;
15488 }
15489
15490 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
15491 goto resync_fail;
15492
15493 if (kind == OMP_CLAUSE_DEPEND_SINK)
15494 nl = c_parser_omp_clause_depend_sink (parser, clause_loc, list);
15495 else
15496 {
15497 nl = c_parser_omp_variable_list (parser, clause_loc,
15498 OMP_CLAUSE_DEPEND, list);
15499
15500 if (iterators)
15501 {
15502 tree block = pop_scope ();
15503 if (iterators == error_mark_node)
15504 iterators = NULL_TREE;
15505 else
15506 TREE_VEC_ELT (iterators, 5) = block;
15507 }
15508
15509 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
15510 {
15511 OMP_CLAUSE_DEPEND_KIND (c) = kind;
15512 if (iterators)
15513 OMP_CLAUSE_DECL (c)
15514 = build_tree_list (iterators, OMP_CLAUSE_DECL (c));
15515 }
15516 }
15517
15518 parens.skip_until_found_close (parser);
15519 return nl;
15520
15521 invalid_kind:
15522 c_parser_error (parser, "invalid depend kind");
15523 resync_fail:
15524 parens.skip_until_found_close (parser);
15525 if (iterators)
15526 pop_scope ();
15527 return list;
15528 }
15529
15530 /* OpenMP 4.0:
15531 map ( map-kind: variable-list )
15532 map ( variable-list )
15533
15534 map-kind:
15535 alloc | to | from | tofrom
15536
15537 OpenMP 4.5:
15538 map-kind:
15539 alloc | to | from | tofrom | release | delete
15540
15541 map ( always [,] map-kind: variable-list ) */
15542
15543 static tree
15544 c_parser_omp_clause_map (c_parser *parser, tree list)
15545 {
15546 location_t clause_loc = c_parser_peek_token (parser)->location;
15547 enum gomp_map_kind kind = GOMP_MAP_TOFROM;
15548 int always = 0;
15549 enum c_id_kind always_id_kind = C_ID_NONE;
15550 location_t always_loc = UNKNOWN_LOCATION;
15551 tree always_id = NULL_TREE;
15552 tree nl, c;
15553
15554 matching_parens parens;
15555 if (!parens.require_open (parser))
15556 return list;
15557
15558 if (c_parser_next_token_is (parser, CPP_NAME))
15559 {
15560 c_token *tok = c_parser_peek_token (parser);
15561 const char *p = IDENTIFIER_POINTER (tok->value);
15562 always_id_kind = tok->id_kind;
15563 always_loc = tok->location;
15564 always_id = tok->value;
15565 if (strcmp ("always", p) == 0)
15566 {
15567 c_token *sectok = c_parser_peek_2nd_token (parser);
15568 if (sectok->type == CPP_COMMA)
15569 {
15570 c_parser_consume_token (parser);
15571 c_parser_consume_token (parser);
15572 always = 2;
15573 }
15574 else if (sectok->type == CPP_NAME)
15575 {
15576 p = IDENTIFIER_POINTER (sectok->value);
15577 if (strcmp ("alloc", p) == 0
15578 || strcmp ("to", p) == 0
15579 || strcmp ("from", p) == 0
15580 || strcmp ("tofrom", p) == 0
15581 || strcmp ("release", p) == 0
15582 || strcmp ("delete", p) == 0)
15583 {
15584 c_parser_consume_token (parser);
15585 always = 1;
15586 }
15587 }
15588 }
15589 }
15590
15591 if (c_parser_next_token_is (parser, CPP_NAME)
15592 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
15593 {
15594 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
15595 if (strcmp ("alloc", p) == 0)
15596 kind = GOMP_MAP_ALLOC;
15597 else if (strcmp ("to", p) == 0)
15598 kind = always ? GOMP_MAP_ALWAYS_TO : GOMP_MAP_TO;
15599 else if (strcmp ("from", p) == 0)
15600 kind = always ? GOMP_MAP_ALWAYS_FROM : GOMP_MAP_FROM;
15601 else if (strcmp ("tofrom", p) == 0)
15602 kind = always ? GOMP_MAP_ALWAYS_TOFROM : GOMP_MAP_TOFROM;
15603 else if (strcmp ("release", p) == 0)
15604 kind = GOMP_MAP_RELEASE;
15605 else if (strcmp ("delete", p) == 0)
15606 kind = GOMP_MAP_DELETE;
15607 else
15608 {
15609 c_parser_error (parser, "invalid map kind");
15610 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
15611 "expected %<)%>");
15612 return list;
15613 }
15614 c_parser_consume_token (parser);
15615 c_parser_consume_token (parser);
15616 }
15617 else if (always)
15618 {
15619 if (always_id_kind != C_ID_ID)
15620 {
15621 c_parser_error (parser, "expected identifier");
15622 parens.skip_until_found_close (parser);
15623 return list;
15624 }
15625
15626 tree t = lookup_name (always_id);
15627 if (t == NULL_TREE)
15628 {
15629 undeclared_variable (always_loc, always_id);
15630 t = error_mark_node;
15631 }
15632 if (t != error_mark_node)
15633 {
15634 tree u = build_omp_clause (clause_loc, OMP_CLAUSE_MAP);
15635 OMP_CLAUSE_DECL (u) = t;
15636 OMP_CLAUSE_CHAIN (u) = list;
15637 OMP_CLAUSE_SET_MAP_KIND (u, kind);
15638 list = u;
15639 }
15640 if (always == 1)
15641 {
15642 parens.skip_until_found_close (parser);
15643 return list;
15644 }
15645 }
15646
15647 nl = c_parser_omp_variable_list (parser, clause_loc, OMP_CLAUSE_MAP, list);
15648
15649 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
15650 OMP_CLAUSE_SET_MAP_KIND (c, kind);
15651
15652 parens.skip_until_found_close (parser);
15653 return nl;
15654 }
15655
15656 /* OpenMP 4.0:
15657 device ( expression ) */
15658
15659 static tree
15660 c_parser_omp_clause_device (c_parser *parser, tree list)
15661 {
15662 location_t clause_loc = c_parser_peek_token (parser)->location;
15663 matching_parens parens;
15664 if (parens.require_open (parser))
15665 {
15666 location_t expr_loc = c_parser_peek_token (parser)->location;
15667 c_expr expr = c_parser_expr_no_commas (parser, NULL);
15668 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
15669 tree c, t = expr.value;
15670 t = c_fully_fold (t, false, NULL);
15671
15672 parens.skip_until_found_close (parser);
15673
15674 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
15675 {
15676 c_parser_error (parser, "expected integer expression");
15677 return list;
15678 }
15679
15680 check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE, "device");
15681
15682 c = build_omp_clause (clause_loc, OMP_CLAUSE_DEVICE);
15683 OMP_CLAUSE_DEVICE_ID (c) = t;
15684 OMP_CLAUSE_CHAIN (c) = list;
15685 list = c;
15686 }
15687
15688 return list;
15689 }
15690
15691 /* OpenMP 4.0:
15692 dist_schedule ( static )
15693 dist_schedule ( static , expression ) */
15694
15695 static tree
15696 c_parser_omp_clause_dist_schedule (c_parser *parser, tree list)
15697 {
15698 tree c, t = NULL_TREE;
15699 location_t loc = c_parser_peek_token (parser)->location;
15700
15701 matching_parens parens;
15702 if (!parens.require_open (parser))
15703 return list;
15704
15705 if (!c_parser_next_token_is_keyword (parser, RID_STATIC))
15706 {
15707 c_parser_error (parser, "invalid dist_schedule kind");
15708 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
15709 "expected %<)%>");
15710 return list;
15711 }
15712
15713 c_parser_consume_token (parser);
15714 if (c_parser_next_token_is (parser, CPP_COMMA))
15715 {
15716 c_parser_consume_token (parser);
15717
15718 location_t expr_loc = c_parser_peek_token (parser)->location;
15719 c_expr expr = c_parser_expr_no_commas (parser, NULL);
15720 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
15721 t = expr.value;
15722 t = c_fully_fold (t, false, NULL);
15723 parens.skip_until_found_close (parser);
15724 }
15725 else
15726 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
15727 "expected %<,%> or %<)%>");
15728
15729 /* check_no_duplicate_clause (list, OMP_CLAUSE_DIST_SCHEDULE,
15730 "dist_schedule"); */
15731 if (omp_find_clause (list, OMP_CLAUSE_DIST_SCHEDULE))
15732 warning_at (loc, 0, "too many %qs clauses", "dist_schedule");
15733 if (t == error_mark_node)
15734 return list;
15735
15736 c = build_omp_clause (loc, OMP_CLAUSE_DIST_SCHEDULE);
15737 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
15738 OMP_CLAUSE_CHAIN (c) = list;
15739 return c;
15740 }
15741
15742 /* OpenMP 4.0:
15743 proc_bind ( proc-bind-kind )
15744
15745 proc-bind-kind:
15746 master | close | spread */
15747
15748 static tree
15749 c_parser_omp_clause_proc_bind (c_parser *parser, tree list)
15750 {
15751 location_t clause_loc = c_parser_peek_token (parser)->location;
15752 enum omp_clause_proc_bind_kind kind;
15753 tree c;
15754
15755 matching_parens parens;
15756 if (!parens.require_open (parser))
15757 return list;
15758
15759 if (c_parser_next_token_is (parser, CPP_NAME))
15760 {
15761 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
15762 if (strcmp ("master", p) == 0)
15763 kind = OMP_CLAUSE_PROC_BIND_MASTER;
15764 else if (strcmp ("close", p) == 0)
15765 kind = OMP_CLAUSE_PROC_BIND_CLOSE;
15766 else if (strcmp ("spread", p) == 0)
15767 kind = OMP_CLAUSE_PROC_BIND_SPREAD;
15768 else
15769 goto invalid_kind;
15770 }
15771 else
15772 goto invalid_kind;
15773
15774 check_no_duplicate_clause (list, OMP_CLAUSE_PROC_BIND, "proc_bind");
15775 c_parser_consume_token (parser);
15776 parens.skip_until_found_close (parser);
15777 c = build_omp_clause (clause_loc, OMP_CLAUSE_PROC_BIND);
15778 OMP_CLAUSE_PROC_BIND_KIND (c) = kind;
15779 OMP_CLAUSE_CHAIN (c) = list;
15780 return c;
15781
15782 invalid_kind:
15783 c_parser_error (parser, "invalid proc_bind kind");
15784 parens.skip_until_found_close (parser);
15785 return list;
15786 }
15787
15788 /* OpenMP 5.0:
15789 device_type ( host | nohost | any ) */
15790
15791 static tree
15792 c_parser_omp_clause_device_type (c_parser *parser, tree list)
15793 {
15794 location_t clause_loc = c_parser_peek_token (parser)->location;
15795 enum omp_clause_device_type_kind kind;
15796 tree c;
15797
15798 matching_parens parens;
15799 if (!parens.require_open (parser))
15800 return list;
15801
15802 if (c_parser_next_token_is (parser, CPP_NAME))
15803 {
15804 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
15805 if (strcmp ("host", p) == 0)
15806 kind = OMP_CLAUSE_DEVICE_TYPE_HOST;
15807 else if (strcmp ("nohost", p) == 0)
15808 kind = OMP_CLAUSE_DEVICE_TYPE_NOHOST;
15809 else if (strcmp ("any", p) == 0)
15810 kind = OMP_CLAUSE_DEVICE_TYPE_ANY;
15811 else
15812 goto invalid_kind;
15813 }
15814 else
15815 goto invalid_kind;
15816
15817 /* check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE_TYPE,
15818 "device_type"); */
15819 c_parser_consume_token (parser);
15820 parens.skip_until_found_close (parser);
15821 c = build_omp_clause (clause_loc, OMP_CLAUSE_DEVICE_TYPE);
15822 OMP_CLAUSE_DEVICE_TYPE_KIND (c) = kind;
15823 OMP_CLAUSE_CHAIN (c) = list;
15824 return c;
15825
15826 invalid_kind:
15827 c_parser_error (parser, "expected %<host%>, %<nohost%> or %<any%>");
15828 parens.skip_until_found_close (parser);
15829 return list;
15830 }
15831
15832 /* OpenMP 4.0:
15833 to ( variable-list ) */
15834
15835 static tree
15836 c_parser_omp_clause_to (c_parser *parser, tree list)
15837 {
15838 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_TO, list);
15839 }
15840
15841 /* OpenMP 4.0:
15842 from ( variable-list ) */
15843
15844 static tree
15845 c_parser_omp_clause_from (c_parser *parser, tree list)
15846 {
15847 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_FROM, list);
15848 }
15849
15850 /* OpenMP 4.0:
15851 uniform ( variable-list ) */
15852
15853 static tree
15854 c_parser_omp_clause_uniform (c_parser *parser, tree list)
15855 {
15856 /* The clauses location. */
15857 location_t loc = c_parser_peek_token (parser)->location;
15858
15859 matching_parens parens;
15860 if (parens.require_open (parser))
15861 {
15862 list = c_parser_omp_variable_list (parser, loc, OMP_CLAUSE_UNIFORM,
15863 list);
15864 parens.skip_until_found_close (parser);
15865 }
15866 return list;
15867 }
15868
15869 /* Parse all OpenACC clauses. The set clauses allowed by the directive
15870 is a bitmask in MASK. Return the list of clauses found. */
15871
15872 static tree
15873 c_parser_oacc_all_clauses (c_parser *parser, omp_clause_mask mask,
15874 const char *where, bool finish_p = true)
15875 {
15876 tree clauses = NULL;
15877 bool first = true;
15878
15879 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
15880 {
15881 location_t here;
15882 pragma_omp_clause c_kind;
15883 const char *c_name;
15884 tree prev = clauses;
15885
15886 if (!first && c_parser_next_token_is (parser, CPP_COMMA))
15887 c_parser_consume_token (parser);
15888
15889 here = c_parser_peek_token (parser)->location;
15890 c_kind = c_parser_omp_clause_name (parser);
15891
15892 switch (c_kind)
15893 {
15894 case PRAGMA_OACC_CLAUSE_ASYNC:
15895 clauses = c_parser_oacc_clause_async (parser, clauses);
15896 c_name = "async";
15897 break;
15898 case PRAGMA_OACC_CLAUSE_AUTO:
15899 clauses = c_parser_oacc_simple_clause (here, OMP_CLAUSE_AUTO,
15900 clauses);
15901 c_name = "auto";
15902 break;
15903 case PRAGMA_OACC_CLAUSE_ATTACH:
15904 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
15905 c_name = "attach";
15906 break;
15907 case PRAGMA_OACC_CLAUSE_COLLAPSE:
15908 clauses = c_parser_omp_clause_collapse (parser, clauses);
15909 c_name = "collapse";
15910 break;
15911 case PRAGMA_OACC_CLAUSE_COPY:
15912 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
15913 c_name = "copy";
15914 break;
15915 case PRAGMA_OACC_CLAUSE_COPYIN:
15916 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
15917 c_name = "copyin";
15918 break;
15919 case PRAGMA_OACC_CLAUSE_COPYOUT:
15920 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
15921 c_name = "copyout";
15922 break;
15923 case PRAGMA_OACC_CLAUSE_CREATE:
15924 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
15925 c_name = "create";
15926 break;
15927 case PRAGMA_OACC_CLAUSE_DELETE:
15928 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
15929 c_name = "delete";
15930 break;
15931 case PRAGMA_OMP_CLAUSE_DEFAULT:
15932 clauses = c_parser_omp_clause_default (parser, clauses, true);
15933 c_name = "default";
15934 break;
15935 case PRAGMA_OACC_CLAUSE_DETACH:
15936 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
15937 c_name = "detach";
15938 break;
15939 case PRAGMA_OACC_CLAUSE_DEVICE:
15940 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
15941 c_name = "device";
15942 break;
15943 case PRAGMA_OACC_CLAUSE_DEVICEPTR:
15944 clauses = c_parser_oacc_data_clause_deviceptr (parser, clauses);
15945 c_name = "deviceptr";
15946 break;
15947 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
15948 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
15949 c_name = "device_resident";
15950 break;
15951 case PRAGMA_OACC_CLAUSE_FINALIZE:
15952 clauses = c_parser_oacc_simple_clause (here, OMP_CLAUSE_FINALIZE,
15953 clauses);
15954 c_name = "finalize";
15955 break;
15956 case PRAGMA_OACC_CLAUSE_FIRSTPRIVATE:
15957 clauses = c_parser_omp_clause_firstprivate (parser, clauses);
15958 c_name = "firstprivate";
15959 break;
15960 case PRAGMA_OACC_CLAUSE_GANG:
15961 c_name = "gang";
15962 clauses = c_parser_oacc_shape_clause (parser, here, OMP_CLAUSE_GANG,
15963 c_name, clauses);
15964 break;
15965 case PRAGMA_OACC_CLAUSE_HOST:
15966 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
15967 c_name = "host";
15968 break;
15969 case PRAGMA_OACC_CLAUSE_IF:
15970 clauses = c_parser_omp_clause_if (parser, clauses, false);
15971 c_name = "if";
15972 break;
15973 case PRAGMA_OACC_CLAUSE_IF_PRESENT:
15974 clauses = c_parser_oacc_simple_clause (here, OMP_CLAUSE_IF_PRESENT,
15975 clauses);
15976 c_name = "if_present";
15977 break;
15978 case PRAGMA_OACC_CLAUSE_INDEPENDENT:
15979 clauses = c_parser_oacc_simple_clause (here, OMP_CLAUSE_INDEPENDENT,
15980 clauses);
15981 c_name = "independent";
15982 break;
15983 case PRAGMA_OACC_CLAUSE_LINK:
15984 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
15985 c_name = "link";
15986 break;
15987 case PRAGMA_OACC_CLAUSE_NO_CREATE:
15988 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
15989 c_name = "no_create";
15990 break;
15991 case PRAGMA_OACC_CLAUSE_NUM_GANGS:
15992 clauses = c_parser_oacc_single_int_clause (parser,
15993 OMP_CLAUSE_NUM_GANGS,
15994 clauses);
15995 c_name = "num_gangs";
15996 break;
15997 case PRAGMA_OACC_CLAUSE_NUM_WORKERS:
15998 clauses = c_parser_oacc_single_int_clause (parser,
15999 OMP_CLAUSE_NUM_WORKERS,
16000 clauses);
16001 c_name = "num_workers";
16002 break;
16003 case PRAGMA_OACC_CLAUSE_PRESENT:
16004 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
16005 c_name = "present";
16006 break;
16007 case PRAGMA_OACC_CLAUSE_PRIVATE:
16008 clauses = c_parser_omp_clause_private (parser, clauses);
16009 c_name = "private";
16010 break;
16011 case PRAGMA_OACC_CLAUSE_REDUCTION:
16012 clauses
16013 = c_parser_omp_clause_reduction (parser, OMP_CLAUSE_REDUCTION,
16014 false, clauses);
16015 c_name = "reduction";
16016 break;
16017 case PRAGMA_OACC_CLAUSE_SEQ:
16018 clauses = c_parser_oacc_simple_clause (here, OMP_CLAUSE_SEQ,
16019 clauses);
16020 c_name = "seq";
16021 break;
16022 case PRAGMA_OACC_CLAUSE_TILE:
16023 clauses = c_parser_oacc_clause_tile (parser, clauses);
16024 c_name = "tile";
16025 break;
16026 case PRAGMA_OACC_CLAUSE_USE_DEVICE:
16027 clauses = c_parser_omp_clause_use_device_ptr (parser, clauses);
16028 c_name = "use_device";
16029 break;
16030 case PRAGMA_OACC_CLAUSE_VECTOR:
16031 c_name = "vector";
16032 clauses = c_parser_oacc_shape_clause (parser, here, OMP_CLAUSE_VECTOR,
16033 c_name, clauses);
16034 break;
16035 case PRAGMA_OACC_CLAUSE_VECTOR_LENGTH:
16036 clauses = c_parser_oacc_single_int_clause (parser,
16037 OMP_CLAUSE_VECTOR_LENGTH,
16038 clauses);
16039 c_name = "vector_length";
16040 break;
16041 case PRAGMA_OACC_CLAUSE_WAIT:
16042 clauses = c_parser_oacc_clause_wait (parser, clauses);
16043 c_name = "wait";
16044 break;
16045 case PRAGMA_OACC_CLAUSE_WORKER:
16046 c_name = "worker";
16047 clauses = c_parser_oacc_shape_clause (parser, here, OMP_CLAUSE_WORKER,
16048 c_name, clauses);
16049 break;
16050 default:
16051 c_parser_error (parser, "expected %<#pragma acc%> clause");
16052 goto saw_error;
16053 }
16054
16055 first = false;
16056
16057 if (((mask >> c_kind) & 1) == 0)
16058 {
16059 /* Remove the invalid clause(s) from the list to avoid
16060 confusing the rest of the compiler. */
16061 clauses = prev;
16062 error_at (here, "%qs is not valid for %qs", c_name, where);
16063 }
16064 }
16065
16066 saw_error:
16067 c_parser_skip_to_pragma_eol (parser);
16068
16069 if (finish_p)
16070 return c_finish_omp_clauses (clauses, C_ORT_ACC);
16071
16072 return clauses;
16073 }
16074
16075 /* Parse all OpenMP clauses. The set clauses allowed by the directive
16076 is a bitmask in MASK. Return the list of clauses found.
16077 FINISH_P set if c_finish_omp_clauses should be called.
16078 NESTED non-zero if clauses should be terminated by closing paren instead
16079 of end of pragma. If it is 2, additionally commas are required in between
16080 the clauses. */
16081
16082 static tree
16083 c_parser_omp_all_clauses (c_parser *parser, omp_clause_mask mask,
16084 const char *where, bool finish_p = true,
16085 int nested = 0)
16086 {
16087 tree clauses = NULL;
16088 bool first = true;
16089
16090 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
16091 {
16092 location_t here;
16093 pragma_omp_clause c_kind;
16094 const char *c_name;
16095 tree prev = clauses;
16096
16097 if (nested && c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
16098 break;
16099
16100 if (!first)
16101 {
16102 if (c_parser_next_token_is (parser, CPP_COMMA))
16103 c_parser_consume_token (parser);
16104 else if (nested == 2)
16105 error_at (c_parser_peek_token (parser)->location,
16106 "clauses in %<simd%> trait should be separated "
16107 "by %<,%>");
16108 }
16109
16110 here = c_parser_peek_token (parser)->location;
16111 c_kind = c_parser_omp_clause_name (parser);
16112
16113 switch (c_kind)
16114 {
16115 case PRAGMA_OMP_CLAUSE_BIND:
16116 clauses = c_parser_omp_clause_bind (parser, clauses);
16117 c_name = "bind";
16118 break;
16119 case PRAGMA_OMP_CLAUSE_COLLAPSE:
16120 clauses = c_parser_omp_clause_collapse (parser, clauses);
16121 c_name = "collapse";
16122 break;
16123 case PRAGMA_OMP_CLAUSE_COPYIN:
16124 clauses = c_parser_omp_clause_copyin (parser, clauses);
16125 c_name = "copyin";
16126 break;
16127 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
16128 clauses = c_parser_omp_clause_copyprivate (parser, clauses);
16129 c_name = "copyprivate";
16130 break;
16131 case PRAGMA_OMP_CLAUSE_DEFAULT:
16132 clauses = c_parser_omp_clause_default (parser, clauses, false);
16133 c_name = "default";
16134 break;
16135 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
16136 clauses = c_parser_omp_clause_firstprivate (parser, clauses);
16137 c_name = "firstprivate";
16138 break;
16139 case PRAGMA_OMP_CLAUSE_FINAL:
16140 clauses = c_parser_omp_clause_final (parser, clauses);
16141 c_name = "final";
16142 break;
16143 case PRAGMA_OMP_CLAUSE_GRAINSIZE:
16144 clauses = c_parser_omp_clause_grainsize (parser, clauses);
16145 c_name = "grainsize";
16146 break;
16147 case PRAGMA_OMP_CLAUSE_HINT:
16148 clauses = c_parser_omp_clause_hint (parser, clauses);
16149 c_name = "hint";
16150 break;
16151 case PRAGMA_OMP_CLAUSE_DEFAULTMAP:
16152 clauses = c_parser_omp_clause_defaultmap (parser, clauses);
16153 c_name = "defaultmap";
16154 break;
16155 case PRAGMA_OMP_CLAUSE_IF:
16156 clauses = c_parser_omp_clause_if (parser, clauses, true);
16157 c_name = "if";
16158 break;
16159 case PRAGMA_OMP_CLAUSE_IN_REDUCTION:
16160 clauses
16161 = c_parser_omp_clause_reduction (parser, OMP_CLAUSE_IN_REDUCTION,
16162 true, clauses);
16163 c_name = "in_reduction";
16164 break;
16165 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
16166 clauses = c_parser_omp_clause_lastprivate (parser, clauses);
16167 c_name = "lastprivate";
16168 break;
16169 case PRAGMA_OMP_CLAUSE_MERGEABLE:
16170 clauses = c_parser_omp_clause_mergeable (parser, clauses);
16171 c_name = "mergeable";
16172 break;
16173 case PRAGMA_OMP_CLAUSE_NOWAIT:
16174 clauses = c_parser_omp_clause_nowait (parser, clauses);
16175 c_name = "nowait";
16176 break;
16177 case PRAGMA_OMP_CLAUSE_NUM_TASKS:
16178 clauses = c_parser_omp_clause_num_tasks (parser, clauses);
16179 c_name = "num_tasks";
16180 break;
16181 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
16182 clauses = c_parser_omp_clause_num_threads (parser, clauses);
16183 c_name = "num_threads";
16184 break;
16185 case PRAGMA_OMP_CLAUSE_ORDER:
16186 clauses = c_parser_omp_clause_order (parser, clauses);
16187 c_name = "order";
16188 break;
16189 case PRAGMA_OMP_CLAUSE_ORDERED:
16190 clauses = c_parser_omp_clause_ordered (parser, clauses);
16191 c_name = "ordered";
16192 break;
16193 case PRAGMA_OMP_CLAUSE_PRIORITY:
16194 clauses = c_parser_omp_clause_priority (parser, clauses);
16195 c_name = "priority";
16196 break;
16197 case PRAGMA_OMP_CLAUSE_PRIVATE:
16198 clauses = c_parser_omp_clause_private (parser, clauses);
16199 c_name = "private";
16200 break;
16201 case PRAGMA_OMP_CLAUSE_REDUCTION:
16202 clauses
16203 = c_parser_omp_clause_reduction (parser, OMP_CLAUSE_REDUCTION,
16204 true, clauses);
16205 c_name = "reduction";
16206 break;
16207 case PRAGMA_OMP_CLAUSE_SCHEDULE:
16208 clauses = c_parser_omp_clause_schedule (parser, clauses);
16209 c_name = "schedule";
16210 break;
16211 case PRAGMA_OMP_CLAUSE_SHARED:
16212 clauses = c_parser_omp_clause_shared (parser, clauses);
16213 c_name = "shared";
16214 break;
16215 case PRAGMA_OMP_CLAUSE_TASK_REDUCTION:
16216 clauses
16217 = c_parser_omp_clause_reduction (parser, OMP_CLAUSE_TASK_REDUCTION,
16218 true, clauses);
16219 c_name = "task_reduction";
16220 break;
16221 case PRAGMA_OMP_CLAUSE_UNTIED:
16222 clauses = c_parser_omp_clause_untied (parser, clauses);
16223 c_name = "untied";
16224 break;
16225 case PRAGMA_OMP_CLAUSE_INBRANCH:
16226 clauses = c_parser_omp_clause_branch (parser, OMP_CLAUSE_INBRANCH,
16227 clauses);
16228 c_name = "inbranch";
16229 break;
16230 case PRAGMA_OMP_CLAUSE_NONTEMPORAL:
16231 clauses = c_parser_omp_clause_nontemporal (parser, clauses);
16232 c_name = "nontemporal";
16233 break;
16234 case PRAGMA_OMP_CLAUSE_NOTINBRANCH:
16235 clauses = c_parser_omp_clause_branch (parser, OMP_CLAUSE_NOTINBRANCH,
16236 clauses);
16237 c_name = "notinbranch";
16238 break;
16239 case PRAGMA_OMP_CLAUSE_PARALLEL:
16240 clauses
16241 = c_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_PARALLEL,
16242 clauses);
16243 c_name = "parallel";
16244 if (!first)
16245 {
16246 clause_not_first:
16247 error_at (here, "%qs must be the first clause of %qs",
16248 c_name, where);
16249 clauses = prev;
16250 }
16251 break;
16252 case PRAGMA_OMP_CLAUSE_FOR:
16253 clauses
16254 = c_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_FOR,
16255 clauses);
16256 c_name = "for";
16257 if (!first)
16258 goto clause_not_first;
16259 break;
16260 case PRAGMA_OMP_CLAUSE_SECTIONS:
16261 clauses
16262 = c_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_SECTIONS,
16263 clauses);
16264 c_name = "sections";
16265 if (!first)
16266 goto clause_not_first;
16267 break;
16268 case PRAGMA_OMP_CLAUSE_TASKGROUP:
16269 clauses
16270 = c_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_TASKGROUP,
16271 clauses);
16272 c_name = "taskgroup";
16273 if (!first)
16274 goto clause_not_first;
16275 break;
16276 case PRAGMA_OMP_CLAUSE_LINK:
16277 clauses
16278 = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_LINK, clauses);
16279 c_name = "link";
16280 break;
16281 case PRAGMA_OMP_CLAUSE_TO:
16282 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK)) != 0)
16283 clauses
16284 = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_TO_DECLARE,
16285 clauses);
16286 else
16287 clauses = c_parser_omp_clause_to (parser, clauses);
16288 c_name = "to";
16289 break;
16290 case PRAGMA_OMP_CLAUSE_FROM:
16291 clauses = c_parser_omp_clause_from (parser, clauses);
16292 c_name = "from";
16293 break;
16294 case PRAGMA_OMP_CLAUSE_UNIFORM:
16295 clauses = c_parser_omp_clause_uniform (parser, clauses);
16296 c_name = "uniform";
16297 break;
16298 case PRAGMA_OMP_CLAUSE_NUM_TEAMS:
16299 clauses = c_parser_omp_clause_num_teams (parser, clauses);
16300 c_name = "num_teams";
16301 break;
16302 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT:
16303 clauses = c_parser_omp_clause_thread_limit (parser, clauses);
16304 c_name = "thread_limit";
16305 break;
16306 case PRAGMA_OMP_CLAUSE_ALIGNED:
16307 clauses = c_parser_omp_clause_aligned (parser, clauses);
16308 c_name = "aligned";
16309 break;
16310 case PRAGMA_OMP_CLAUSE_LINEAR:
16311 clauses = c_parser_omp_clause_linear (parser, clauses);
16312 c_name = "linear";
16313 break;
16314 case PRAGMA_OMP_CLAUSE_DEPEND:
16315 clauses = c_parser_omp_clause_depend (parser, clauses);
16316 c_name = "depend";
16317 break;
16318 case PRAGMA_OMP_CLAUSE_MAP:
16319 clauses = c_parser_omp_clause_map (parser, clauses);
16320 c_name = "map";
16321 break;
16322 case PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR:
16323 clauses = c_parser_omp_clause_use_device_ptr (parser, clauses);
16324 c_name = "use_device_ptr";
16325 break;
16326 case PRAGMA_OMP_CLAUSE_USE_DEVICE_ADDR:
16327 clauses = c_parser_omp_clause_use_device_addr (parser, clauses);
16328 c_name = "use_device_addr";
16329 break;
16330 case PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR:
16331 clauses = c_parser_omp_clause_is_device_ptr (parser, clauses);
16332 c_name = "is_device_ptr";
16333 break;
16334 case PRAGMA_OMP_CLAUSE_DEVICE:
16335 clauses = c_parser_omp_clause_device (parser, clauses);
16336 c_name = "device";
16337 break;
16338 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE:
16339 clauses = c_parser_omp_clause_dist_schedule (parser, clauses);
16340 c_name = "dist_schedule";
16341 break;
16342 case PRAGMA_OMP_CLAUSE_PROC_BIND:
16343 clauses = c_parser_omp_clause_proc_bind (parser, clauses);
16344 c_name = "proc_bind";
16345 break;
16346 case PRAGMA_OMP_CLAUSE_DEVICE_TYPE:
16347 clauses = c_parser_omp_clause_device_type (parser, clauses);
16348 c_name = "device_type";
16349 break;
16350 case PRAGMA_OMP_CLAUSE_SAFELEN:
16351 clauses = c_parser_omp_clause_safelen (parser, clauses);
16352 c_name = "safelen";
16353 break;
16354 case PRAGMA_OMP_CLAUSE_SIMDLEN:
16355 clauses = c_parser_omp_clause_simdlen (parser, clauses);
16356 c_name = "simdlen";
16357 break;
16358 case PRAGMA_OMP_CLAUSE_NOGROUP:
16359 clauses = c_parser_omp_clause_nogroup (parser, clauses);
16360 c_name = "nogroup";
16361 break;
16362 case PRAGMA_OMP_CLAUSE_THREADS:
16363 clauses
16364 = c_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_THREADS,
16365 clauses);
16366 c_name = "threads";
16367 break;
16368 case PRAGMA_OMP_CLAUSE_SIMD:
16369 clauses
16370 = c_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_SIMD,
16371 clauses);
16372 c_name = "simd";
16373 break;
16374 default:
16375 c_parser_error (parser, "expected %<#pragma omp%> clause");
16376 goto saw_error;
16377 }
16378
16379 first = false;
16380
16381 if (((mask >> c_kind) & 1) == 0)
16382 {
16383 /* Remove the invalid clause(s) from the list to avoid
16384 confusing the rest of the compiler. */
16385 clauses = prev;
16386 error_at (here, "%qs is not valid for %qs", c_name, where);
16387 }
16388 }
16389
16390 saw_error:
16391 if (!nested)
16392 c_parser_skip_to_pragma_eol (parser);
16393
16394 if (finish_p)
16395 {
16396 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM)) != 0)
16397 return c_finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
16398 return c_finish_omp_clauses (clauses, C_ORT_OMP);
16399 }
16400
16401 return clauses;
16402 }
16403
16404 /* OpenACC 2.0, OpenMP 2.5:
16405 structured-block:
16406 statement
16407
16408 In practice, we're also interested in adding the statement to an
16409 outer node. So it is convenient if we work around the fact that
16410 c_parser_statement calls add_stmt. */
16411
16412 static tree
16413 c_parser_omp_structured_block (c_parser *parser, bool *if_p)
16414 {
16415 tree stmt = push_stmt_list ();
16416 c_parser_statement (parser, if_p);
16417 return pop_stmt_list (stmt);
16418 }
16419
16420 /* OpenACC 2.0:
16421 # pragma acc cache (variable-list) new-line
16422
16423 LOC is the location of the #pragma token.
16424 */
16425
16426 static tree
16427 c_parser_oacc_cache (location_t loc, c_parser *parser)
16428 {
16429 tree stmt, clauses;
16430
16431 clauses = c_parser_omp_var_list_parens (parser, OMP_CLAUSE__CACHE_, NULL);
16432 clauses = c_finish_omp_clauses (clauses, C_ORT_ACC);
16433
16434 c_parser_skip_to_pragma_eol (parser);
16435
16436 stmt = make_node (OACC_CACHE);
16437 TREE_TYPE (stmt) = void_type_node;
16438 OACC_CACHE_CLAUSES (stmt) = clauses;
16439 SET_EXPR_LOCATION (stmt, loc);
16440 add_stmt (stmt);
16441
16442 return stmt;
16443 }
16444
16445 /* OpenACC 2.0:
16446 # pragma acc data oacc-data-clause[optseq] new-line
16447 structured-block
16448
16449 LOC is the location of the #pragma token.
16450 */
16451
16452 #define OACC_DATA_CLAUSE_MASK \
16453 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ATTACH) \
16454 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
16455 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
16456 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
16457 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
16458 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
16459 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
16460 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NO_CREATE) \
16461 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT))
16462
16463 static tree
16464 c_parser_oacc_data (location_t loc, c_parser *parser, bool *if_p)
16465 {
16466 tree stmt, clauses, block;
16467
16468 clauses = c_parser_oacc_all_clauses (parser, OACC_DATA_CLAUSE_MASK,
16469 "#pragma acc data");
16470
16471 block = c_begin_omp_parallel ();
16472 add_stmt (c_parser_omp_structured_block (parser, if_p));
16473
16474 stmt = c_finish_oacc_data (loc, clauses, block);
16475
16476 return stmt;
16477 }
16478
16479 /* OpenACC 2.0:
16480 # pragma acc declare oacc-data-clause[optseq] new-line
16481 */
16482
16483 #define OACC_DECLARE_CLAUSE_MASK \
16484 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
16485 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
16486 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
16487 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
16488 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
16489 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT) \
16490 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_LINK) \
16491 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT))
16492
16493 static void
16494 c_parser_oacc_declare (c_parser *parser)
16495 {
16496 location_t pragma_loc = c_parser_peek_token (parser)->location;
16497 tree clauses, stmt, t, decl;
16498
16499 bool error = false;
16500
16501 c_parser_consume_pragma (parser);
16502
16503 clauses = c_parser_oacc_all_clauses (parser, OACC_DECLARE_CLAUSE_MASK,
16504 "#pragma acc declare");
16505 if (!clauses)
16506 {
16507 error_at (pragma_loc,
16508 "no valid clauses specified in %<#pragma acc declare%>");
16509 return;
16510 }
16511
16512 for (t = clauses; t; t = OMP_CLAUSE_CHAIN (t))
16513 {
16514 location_t loc = OMP_CLAUSE_LOCATION (t);
16515 decl = OMP_CLAUSE_DECL (t);
16516 if (!DECL_P (decl))
16517 {
16518 error_at (loc, "array section in %<#pragma acc declare%>");
16519 error = true;
16520 continue;
16521 }
16522
16523 switch (OMP_CLAUSE_MAP_KIND (t))
16524 {
16525 case GOMP_MAP_FIRSTPRIVATE_POINTER:
16526 case GOMP_MAP_ALLOC:
16527 case GOMP_MAP_TO:
16528 case GOMP_MAP_FORCE_DEVICEPTR:
16529 case GOMP_MAP_DEVICE_RESIDENT:
16530 break;
16531
16532 case GOMP_MAP_LINK:
16533 if (!global_bindings_p ()
16534 && (TREE_STATIC (decl)
16535 || !DECL_EXTERNAL (decl)))
16536 {
16537 error_at (loc,
16538 "%qD must be a global variable in "
16539 "%<#pragma acc declare link%>",
16540 decl);
16541 error = true;
16542 continue;
16543 }
16544 break;
16545
16546 default:
16547 if (global_bindings_p ())
16548 {
16549 error_at (loc, "invalid OpenACC clause at file scope");
16550 error = true;
16551 continue;
16552 }
16553 if (DECL_EXTERNAL (decl))
16554 {
16555 error_at (loc,
16556 "invalid use of %<extern%> variable %qD "
16557 "in %<#pragma acc declare%>", decl);
16558 error = true;
16559 continue;
16560 }
16561 else if (TREE_PUBLIC (decl))
16562 {
16563 error_at (loc,
16564 "invalid use of %<global%> variable %qD "
16565 "in %<#pragma acc declare%>", decl);
16566 error = true;
16567 continue;
16568 }
16569 break;
16570 }
16571
16572 if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (decl))
16573 || lookup_attribute ("omp declare target link",
16574 DECL_ATTRIBUTES (decl)))
16575 {
16576 error_at (loc, "variable %qD used more than once with "
16577 "%<#pragma acc declare%>", decl);
16578 error = true;
16579 continue;
16580 }
16581
16582 if (!error)
16583 {
16584 tree id;
16585
16586 if (OMP_CLAUSE_MAP_KIND (t) == GOMP_MAP_LINK)
16587 id = get_identifier ("omp declare target link");
16588 else
16589 id = get_identifier ("omp declare target");
16590
16591 DECL_ATTRIBUTES (decl)
16592 = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (decl));
16593
16594 if (global_bindings_p ())
16595 {
16596 symtab_node *node = symtab_node::get (decl);
16597 if (node != NULL)
16598 {
16599 node->offloadable = 1;
16600 if (ENABLE_OFFLOADING)
16601 {
16602 g->have_offload = true;
16603 if (is_a <varpool_node *> (node))
16604 vec_safe_push (offload_vars, decl);
16605 }
16606 }
16607 }
16608 }
16609 }
16610
16611 if (error || global_bindings_p ())
16612 return;
16613
16614 stmt = make_node (OACC_DECLARE);
16615 TREE_TYPE (stmt) = void_type_node;
16616 OACC_DECLARE_CLAUSES (stmt) = clauses;
16617 SET_EXPR_LOCATION (stmt, pragma_loc);
16618
16619 add_stmt (stmt);
16620
16621 return;
16622 }
16623
16624 /* OpenACC 2.0:
16625 # pragma acc enter data oacc-enter-data-clause[optseq] new-line
16626
16627 or
16628
16629 # pragma acc exit data oacc-exit-data-clause[optseq] new-line
16630
16631
16632 LOC is the location of the #pragma token.
16633 */
16634
16635 #define OACC_ENTER_DATA_CLAUSE_MASK \
16636 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
16637 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
16638 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ATTACH) \
16639 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
16640 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
16641 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
16642
16643 #define OACC_EXIT_DATA_CLAUSE_MASK \
16644 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
16645 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
16646 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
16647 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DELETE) \
16648 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DETACH) \
16649 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FINALIZE) \
16650 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
16651
16652 static void
16653 c_parser_oacc_enter_exit_data (c_parser *parser, bool enter)
16654 {
16655 location_t loc = c_parser_peek_token (parser)->location;
16656 tree clauses, stmt;
16657 const char *p = "";
16658
16659 c_parser_consume_pragma (parser);
16660
16661 if (c_parser_next_token_is (parser, CPP_NAME))
16662 {
16663 p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
16664 c_parser_consume_token (parser);
16665 }
16666
16667 if (strcmp (p, "data") != 0)
16668 {
16669 error_at (loc, "expected %<data%> after %<#pragma acc %s%>",
16670 enter ? "enter" : "exit");
16671 parser->error = true;
16672 c_parser_skip_to_pragma_eol (parser);
16673 return;
16674 }
16675
16676 if (enter)
16677 clauses = c_parser_oacc_all_clauses (parser, OACC_ENTER_DATA_CLAUSE_MASK,
16678 "#pragma acc enter data");
16679 else
16680 clauses = c_parser_oacc_all_clauses (parser, OACC_EXIT_DATA_CLAUSE_MASK,
16681 "#pragma acc exit data");
16682
16683 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
16684 {
16685 error_at (loc, "%<#pragma acc %s data%> has no data movement clause",
16686 enter ? "enter" : "exit");
16687 return;
16688 }
16689
16690 stmt = enter ? make_node (OACC_ENTER_DATA) : make_node (OACC_EXIT_DATA);
16691 TREE_TYPE (stmt) = void_type_node;
16692 OMP_STANDALONE_CLAUSES (stmt) = clauses;
16693 SET_EXPR_LOCATION (stmt, loc);
16694 add_stmt (stmt);
16695 }
16696
16697
16698 /* OpenACC 2.0:
16699 # pragma acc host_data oacc-data-clause[optseq] new-line
16700 structured-block
16701 */
16702
16703 #define OACC_HOST_DATA_CLAUSE_MASK \
16704 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_USE_DEVICE) )
16705
16706 static tree
16707 c_parser_oacc_host_data (location_t loc, c_parser *parser, bool *if_p)
16708 {
16709 tree stmt, clauses, block;
16710
16711 clauses = c_parser_oacc_all_clauses (parser, OACC_HOST_DATA_CLAUSE_MASK,
16712 "#pragma acc host_data");
16713
16714 block = c_begin_omp_parallel ();
16715 add_stmt (c_parser_omp_structured_block (parser, if_p));
16716 stmt = c_finish_oacc_host_data (loc, clauses, block);
16717 return stmt;
16718 }
16719
16720
16721 /* OpenACC 2.0:
16722
16723 # pragma acc loop oacc-loop-clause[optseq] new-line
16724 structured-block
16725
16726 LOC is the location of the #pragma token.
16727 */
16728
16729 #define OACC_LOOP_CLAUSE_MASK \
16730 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COLLAPSE) \
16731 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
16732 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
16733 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
16734 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
16735 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
16736 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_AUTO) \
16737 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_INDEPENDENT) \
16738 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ) \
16739 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_TILE) )
16740 static tree
16741 c_parser_oacc_loop (location_t loc, c_parser *parser, char *p_name,
16742 omp_clause_mask mask, tree *cclauses, bool *if_p)
16743 {
16744 bool is_parallel = ((mask >> PRAGMA_OACC_CLAUSE_REDUCTION) & 1) == 1;
16745
16746 strcat (p_name, " loop");
16747 mask |= OACC_LOOP_CLAUSE_MASK;
16748
16749 tree clauses = c_parser_oacc_all_clauses (parser, mask, p_name,
16750 cclauses == NULL);
16751 if (cclauses)
16752 {
16753 clauses = c_oacc_split_loop_clauses (clauses, cclauses, is_parallel);
16754 if (*cclauses)
16755 *cclauses = c_finish_omp_clauses (*cclauses, C_ORT_ACC);
16756 if (clauses)
16757 clauses = c_finish_omp_clauses (clauses, C_ORT_ACC);
16758 }
16759
16760 tree block = c_begin_compound_stmt (true);
16761 tree stmt = c_parser_omp_for_loop (loc, parser, OACC_LOOP, clauses, NULL,
16762 if_p);
16763 block = c_end_compound_stmt (loc, block, true);
16764 add_stmt (block);
16765
16766 return stmt;
16767 }
16768
16769 /* OpenACC 2.0:
16770 # pragma acc kernels oacc-kernels-clause[optseq] new-line
16771 structured-block
16772
16773 or
16774
16775 # pragma acc parallel oacc-parallel-clause[optseq] new-line
16776 structured-block
16777
16778 OpenACC 2.6:
16779
16780 # pragma acc serial oacc-serial-clause[optseq] new-line
16781 structured-block
16782
16783 LOC is the location of the #pragma token.
16784 */
16785
16786 #define OACC_KERNELS_CLAUSE_MASK \
16787 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
16788 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ATTACH) \
16789 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
16790 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
16791 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
16792 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
16793 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
16794 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
16795 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
16796 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NO_CREATE) \
16797 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
16798 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
16799 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
16800 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
16801 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
16802
16803 #define OACC_PARALLEL_CLAUSE_MASK \
16804 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
16805 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ATTACH) \
16806 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
16807 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
16808 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
16809 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
16810 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
16811 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
16812 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
16813 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NO_CREATE) \
16814 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
16815 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FIRSTPRIVATE) \
16816 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
16817 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
16818 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
16819 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
16820 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
16821 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
16822
16823 #define OACC_SERIAL_CLAUSE_MASK \
16824 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
16825 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ATTACH) \
16826 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
16827 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
16828 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
16829 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
16830 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
16831 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
16832 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
16833 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NO_CREATE) \
16834 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
16835 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FIRSTPRIVATE) \
16836 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
16837 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
16838 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
16839
16840 static tree
16841 c_parser_oacc_compute (location_t loc, c_parser *parser,
16842 enum pragma_kind p_kind, char *p_name, bool *if_p)
16843 {
16844 omp_clause_mask mask;
16845 enum tree_code code;
16846 switch (p_kind)
16847 {
16848 case PRAGMA_OACC_KERNELS:
16849 strcat (p_name, " kernels");
16850 mask = OACC_KERNELS_CLAUSE_MASK;
16851 code = OACC_KERNELS;
16852 break;
16853 case PRAGMA_OACC_PARALLEL:
16854 strcat (p_name, " parallel");
16855 mask = OACC_PARALLEL_CLAUSE_MASK;
16856 code = OACC_PARALLEL;
16857 break;
16858 case PRAGMA_OACC_SERIAL:
16859 strcat (p_name, " serial");
16860 mask = OACC_SERIAL_CLAUSE_MASK;
16861 code = OACC_SERIAL;
16862 break;
16863 default:
16864 gcc_unreachable ();
16865 }
16866
16867 if (c_parser_next_token_is (parser, CPP_NAME))
16868 {
16869 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
16870 if (strcmp (p, "loop") == 0)
16871 {
16872 c_parser_consume_token (parser);
16873 tree block = c_begin_omp_parallel ();
16874 tree clauses;
16875 c_parser_oacc_loop (loc, parser, p_name, mask, &clauses, if_p);
16876 return c_finish_omp_construct (loc, code, block, clauses);
16877 }
16878 }
16879
16880 tree clauses = c_parser_oacc_all_clauses (parser, mask, p_name);
16881
16882 tree block = c_begin_omp_parallel ();
16883 add_stmt (c_parser_omp_structured_block (parser, if_p));
16884
16885 return c_finish_omp_construct (loc, code, block, clauses);
16886 }
16887
16888 /* OpenACC 2.0:
16889 # pragma acc routine oacc-routine-clause[optseq] new-line
16890 function-definition
16891
16892 # pragma acc routine ( name ) oacc-routine-clause[optseq] new-line
16893 */
16894
16895 #define OACC_ROUTINE_CLAUSE_MASK \
16896 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
16897 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
16898 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
16899 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ) )
16900
16901 /* Parse an OpenACC routine directive. For named directives, we apply
16902 immediately to the named function. For unnamed ones we then parse
16903 a declaration or definition, which must be for a function. */
16904
16905 static void
16906 c_parser_oacc_routine (c_parser *parser, enum pragma_context context)
16907 {
16908 gcc_checking_assert (context == pragma_external);
16909
16910 oacc_routine_data data;
16911 data.error_seen = false;
16912 data.fndecl_seen = false;
16913 data.clauses = NULL_TREE;
16914 data.loc = c_parser_peek_token (parser)->location;
16915
16916 c_parser_consume_pragma (parser);
16917
16918 /* Look for optional '( name )'. */
16919 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
16920 {
16921 c_parser_consume_token (parser); /* '(' */
16922
16923 tree decl = NULL_TREE;
16924 c_token *name_token = c_parser_peek_token (parser);
16925 location_t name_loc = name_token->location;
16926 if (name_token->type == CPP_NAME
16927 && (name_token->id_kind == C_ID_ID
16928 || name_token->id_kind == C_ID_TYPENAME))
16929 {
16930 decl = lookup_name (name_token->value);
16931 if (!decl)
16932 error_at (name_loc,
16933 "%qE has not been declared", name_token->value);
16934 c_parser_consume_token (parser);
16935 }
16936 else
16937 c_parser_error (parser, "expected function name");
16938
16939 if (!decl
16940 || !c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
16941 {
16942 c_parser_skip_to_pragma_eol (parser, false);
16943 return;
16944 }
16945
16946 data.clauses
16947 = c_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
16948 "#pragma acc routine");
16949 /* The clauses are in reverse order; fix that to make later diagnostic
16950 emission easier. */
16951 data.clauses = nreverse (data.clauses);
16952
16953 if (TREE_CODE (decl) != FUNCTION_DECL)
16954 {
16955 error_at (name_loc, "%qD does not refer to a function", decl);
16956 return;
16957 }
16958
16959 c_finish_oacc_routine (&data, decl, false);
16960 }
16961 else /* No optional '( name )'. */
16962 {
16963 data.clauses
16964 = c_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
16965 "#pragma acc routine");
16966 /* The clauses are in reverse order; fix that to make later diagnostic
16967 emission easier. */
16968 data.clauses = nreverse (data.clauses);
16969
16970 /* Emit a helpful diagnostic if there's another pragma following this
16971 one. Also don't allow a static assertion declaration, as in the
16972 following we'll just parse a *single* "declaration or function
16973 definition", and the static assertion counts an one. */
16974 if (c_parser_next_token_is (parser, CPP_PRAGMA)
16975 || c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT))
16976 {
16977 error_at (data.loc,
16978 "%<#pragma acc routine%> not immediately followed by"
16979 " function declaration or definition");
16980 /* ..., and then just keep going. */
16981 return;
16982 }
16983
16984 /* We only have to consider the pragma_external case here. */
16985 if (c_parser_next_token_is (parser, CPP_KEYWORD)
16986 && c_parser_peek_token (parser)->keyword == RID_EXTENSION)
16987 {
16988 int ext = disable_extension_diagnostics ();
16989 do
16990 c_parser_consume_token (parser);
16991 while (c_parser_next_token_is (parser, CPP_KEYWORD)
16992 && c_parser_peek_token (parser)->keyword == RID_EXTENSION);
16993 c_parser_declaration_or_fndef (parser, true, true, true, false, true,
16994 NULL, vNULL, false, NULL, &data);
16995 restore_extension_diagnostics (ext);
16996 }
16997 else
16998 c_parser_declaration_or_fndef (parser, true, true, true, false, true,
16999 NULL, vNULL, false, NULL, &data);
17000 }
17001 }
17002
17003 /* Finalize an OpenACC routine pragma, applying it to FNDECL.
17004 IS_DEFN is true if we're applying it to the definition. */
17005
17006 static void
17007 c_finish_oacc_routine (struct oacc_routine_data *data, tree fndecl,
17008 bool is_defn)
17009 {
17010 /* Keep going if we're in error reporting mode. */
17011 if (data->error_seen
17012 || fndecl == error_mark_node)
17013 return;
17014
17015 if (data->fndecl_seen)
17016 {
17017 error_at (data->loc,
17018 "%<#pragma acc routine%> not immediately followed by"
17019 " a single function declaration or definition");
17020 data->error_seen = true;
17021 return;
17022 }
17023 if (fndecl == NULL_TREE || TREE_CODE (fndecl) != FUNCTION_DECL)
17024 {
17025 error_at (data->loc,
17026 "%<#pragma acc routine%> not immediately followed by"
17027 " function declaration or definition");
17028 data->error_seen = true;
17029 return;
17030 }
17031
17032 int compatible
17033 = oacc_verify_routine_clauses (fndecl, &data->clauses, data->loc,
17034 "#pragma acc routine");
17035 if (compatible < 0)
17036 {
17037 data->error_seen = true;
17038 return;
17039 }
17040 if (compatible > 0)
17041 {
17042 }
17043 else
17044 {
17045 if (TREE_USED (fndecl) || (!is_defn && DECL_SAVED_TREE (fndecl)))
17046 {
17047 error_at (data->loc,
17048 TREE_USED (fndecl)
17049 ? G_("%<#pragma acc routine%> must be applied before use")
17050 : G_("%<#pragma acc routine%> must be applied before"
17051 " definition"));
17052 data->error_seen = true;
17053 return;
17054 }
17055
17056 /* Set the routine's level of parallelism. */
17057 tree dims = oacc_build_routine_dims (data->clauses);
17058 oacc_replace_fn_attrib (fndecl, dims);
17059
17060 /* Add an "omp declare target" attribute. */
17061 DECL_ATTRIBUTES (fndecl)
17062 = tree_cons (get_identifier ("omp declare target"),
17063 data->clauses, DECL_ATTRIBUTES (fndecl));
17064 }
17065
17066 /* Remember that we've used this "#pragma acc routine". */
17067 data->fndecl_seen = true;
17068 }
17069
17070 /* OpenACC 2.0:
17071 # pragma acc update oacc-update-clause[optseq] new-line
17072 */
17073
17074 #define OACC_UPDATE_CLAUSE_MASK \
17075 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
17076 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE) \
17077 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_HOST) \
17078 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
17079 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF_PRESENT) \
17080 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
17081
17082 static void
17083 c_parser_oacc_update (c_parser *parser)
17084 {
17085 location_t loc = c_parser_peek_token (parser)->location;
17086
17087 c_parser_consume_pragma (parser);
17088
17089 tree clauses = c_parser_oacc_all_clauses (parser, OACC_UPDATE_CLAUSE_MASK,
17090 "#pragma acc update");
17091 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
17092 {
17093 error_at (loc,
17094 "%<#pragma acc update%> must contain at least one "
17095 "%<device%> or %<host%> or %<self%> clause");
17096 return;
17097 }
17098
17099 if (parser->error)
17100 return;
17101
17102 tree stmt = make_node (OACC_UPDATE);
17103 TREE_TYPE (stmt) = void_type_node;
17104 OACC_UPDATE_CLAUSES (stmt) = clauses;
17105 SET_EXPR_LOCATION (stmt, loc);
17106 add_stmt (stmt);
17107 }
17108
17109 /* OpenACC 2.0:
17110 # pragma acc wait [(intseq)] oacc-wait-clause[optseq] new-line
17111
17112 LOC is the location of the #pragma token.
17113 */
17114
17115 #define OACC_WAIT_CLAUSE_MASK \
17116 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) )
17117
17118 static tree
17119 c_parser_oacc_wait (location_t loc, c_parser *parser, char *p_name)
17120 {
17121 tree clauses, list = NULL_TREE, stmt = NULL_TREE;
17122
17123 if (c_parser_peek_token (parser)->type == CPP_OPEN_PAREN)
17124 list = c_parser_oacc_wait_list (parser, loc, list);
17125
17126 strcpy (p_name, " wait");
17127 clauses = c_parser_oacc_all_clauses (parser, OACC_WAIT_CLAUSE_MASK, p_name);
17128 stmt = c_finish_oacc_wait (loc, list, clauses);
17129 add_stmt (stmt);
17130
17131 return stmt;
17132 }
17133
17134 /* OpenMP 2.5:
17135 # pragma omp atomic new-line
17136 expression-stmt
17137
17138 expression-stmt:
17139 x binop= expr | x++ | ++x | x-- | --x
17140 binop:
17141 +, *, -, /, &, ^, |, <<, >>
17142
17143 where x is an lvalue expression with scalar type.
17144
17145 OpenMP 3.1:
17146 # pragma omp atomic new-line
17147 update-stmt
17148
17149 # pragma omp atomic read new-line
17150 read-stmt
17151
17152 # pragma omp atomic write new-line
17153 write-stmt
17154
17155 # pragma omp atomic update new-line
17156 update-stmt
17157
17158 # pragma omp atomic capture new-line
17159 capture-stmt
17160
17161 # pragma omp atomic capture new-line
17162 capture-block
17163
17164 read-stmt:
17165 v = x
17166 write-stmt:
17167 x = expr
17168 update-stmt:
17169 expression-stmt | x = x binop expr
17170 capture-stmt:
17171 v = expression-stmt
17172 capture-block:
17173 { v = x; update-stmt; } | { update-stmt; v = x; }
17174
17175 OpenMP 4.0:
17176 update-stmt:
17177 expression-stmt | x = x binop expr | x = expr binop x
17178 capture-stmt:
17179 v = update-stmt
17180 capture-block:
17181 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
17182
17183 where x and v are lvalue expressions with scalar type.
17184
17185 LOC is the location of the #pragma token. */
17186
17187 static void
17188 c_parser_omp_atomic (location_t loc, c_parser *parser)
17189 {
17190 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE;
17191 tree lhs1 = NULL_TREE, rhs1 = NULL_TREE;
17192 tree stmt, orig_lhs, unfolded_lhs = NULL_TREE, unfolded_lhs1 = NULL_TREE;
17193 enum tree_code code = ERROR_MARK, opcode = NOP_EXPR;
17194 enum omp_memory_order memory_order = OMP_MEMORY_ORDER_UNSPECIFIED;
17195 struct c_expr expr;
17196 location_t eloc;
17197 bool structured_block = false;
17198 bool swapped = false;
17199 bool non_lvalue_p;
17200 bool first = true;
17201 tree clauses = NULL_TREE;
17202
17203 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
17204 {
17205 if (!first && c_parser_next_token_is (parser, CPP_COMMA))
17206 c_parser_consume_token (parser);
17207
17208 first = false;
17209
17210 if (c_parser_next_token_is (parser, CPP_NAME))
17211 {
17212 const char *p
17213 = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
17214 location_t cloc = c_parser_peek_token (parser)->location;
17215 enum tree_code new_code = ERROR_MARK;
17216 enum omp_memory_order new_memory_order
17217 = OMP_MEMORY_ORDER_UNSPECIFIED;
17218
17219 if (!strcmp (p, "read"))
17220 new_code = OMP_ATOMIC_READ;
17221 else if (!strcmp (p, "write"))
17222 new_code = NOP_EXPR;
17223 else if (!strcmp (p, "update"))
17224 new_code = OMP_ATOMIC;
17225 else if (!strcmp (p, "capture"))
17226 new_code = OMP_ATOMIC_CAPTURE_NEW;
17227 else if (!strcmp (p, "seq_cst"))
17228 new_memory_order = OMP_MEMORY_ORDER_SEQ_CST;
17229 else if (!strcmp (p, "acq_rel"))
17230 new_memory_order = OMP_MEMORY_ORDER_ACQ_REL;
17231 else if (!strcmp (p, "release"))
17232 new_memory_order = OMP_MEMORY_ORDER_RELEASE;
17233 else if (!strcmp (p, "acquire"))
17234 new_memory_order = OMP_MEMORY_ORDER_ACQUIRE;
17235 else if (!strcmp (p, "relaxed"))
17236 new_memory_order = OMP_MEMORY_ORDER_RELAXED;
17237 else if (!strcmp (p, "hint"))
17238 {
17239 c_parser_consume_token (parser);
17240 clauses = c_parser_omp_clause_hint (parser, clauses);
17241 continue;
17242 }
17243 else
17244 {
17245 p = NULL;
17246 error_at (cloc, "expected %<read%>, %<write%>, %<update%>, "
17247 "%<capture%>, %<seq_cst%>, %<acq_rel%>, "
17248 "%<release%>, %<relaxed%> or %<hint%> clause");
17249 }
17250 if (p)
17251 {
17252 if (new_code != ERROR_MARK)
17253 {
17254 if (code != ERROR_MARK)
17255 error_at (cloc, "too many atomic clauses");
17256 else
17257 code = new_code;
17258 }
17259 else if (new_memory_order != OMP_MEMORY_ORDER_UNSPECIFIED)
17260 {
17261 if (memory_order != OMP_MEMORY_ORDER_UNSPECIFIED)
17262 error_at (cloc, "too many memory order clauses");
17263 else
17264 memory_order = new_memory_order;
17265 }
17266 c_parser_consume_token (parser);
17267 continue;
17268 }
17269 }
17270 break;
17271 }
17272 c_parser_skip_to_pragma_eol (parser);
17273
17274 if (code == ERROR_MARK)
17275 code = OMP_ATOMIC;
17276 if (memory_order == OMP_MEMORY_ORDER_UNSPECIFIED)
17277 {
17278 omp_requires_mask
17279 = (enum omp_requires) (omp_requires_mask
17280 | OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER_USED);
17281 switch ((enum omp_memory_order)
17282 (omp_requires_mask & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER))
17283 {
17284 case OMP_MEMORY_ORDER_UNSPECIFIED:
17285 case OMP_MEMORY_ORDER_RELAXED:
17286 memory_order = OMP_MEMORY_ORDER_RELAXED;
17287 break;
17288 case OMP_MEMORY_ORDER_SEQ_CST:
17289 memory_order = OMP_MEMORY_ORDER_SEQ_CST;
17290 break;
17291 case OMP_MEMORY_ORDER_ACQ_REL:
17292 switch (code)
17293 {
17294 case OMP_ATOMIC_READ:
17295 memory_order = OMP_MEMORY_ORDER_ACQUIRE;
17296 break;
17297 case NOP_EXPR: /* atomic write */
17298 case OMP_ATOMIC:
17299 memory_order = OMP_MEMORY_ORDER_RELEASE;
17300 break;
17301 default:
17302 memory_order = OMP_MEMORY_ORDER_ACQ_REL;
17303 break;
17304 }
17305 break;
17306 default:
17307 gcc_unreachable ();
17308 }
17309 }
17310 else
17311 switch (code)
17312 {
17313 case OMP_ATOMIC_READ:
17314 if (memory_order == OMP_MEMORY_ORDER_ACQ_REL
17315 || memory_order == OMP_MEMORY_ORDER_RELEASE)
17316 {
17317 error_at (loc, "%<#pragma omp atomic read%> incompatible with "
17318 "%<acq_rel%> or %<release%> clauses");
17319 memory_order = OMP_MEMORY_ORDER_SEQ_CST;
17320 }
17321 break;
17322 case NOP_EXPR: /* atomic write */
17323 if (memory_order == OMP_MEMORY_ORDER_ACQ_REL
17324 || memory_order == OMP_MEMORY_ORDER_ACQUIRE)
17325 {
17326 error_at (loc, "%<#pragma omp atomic write%> incompatible with "
17327 "%<acq_rel%> or %<acquire%> clauses");
17328 memory_order = OMP_MEMORY_ORDER_SEQ_CST;
17329 }
17330 break;
17331 case OMP_ATOMIC:
17332 if (memory_order == OMP_MEMORY_ORDER_ACQ_REL
17333 || memory_order == OMP_MEMORY_ORDER_ACQUIRE)
17334 {
17335 error_at (loc, "%<#pragma omp atomic update%> incompatible with "
17336 "%<acq_rel%> or %<acquire%> clauses");
17337 memory_order = OMP_MEMORY_ORDER_SEQ_CST;
17338 }
17339 break;
17340 default:
17341 break;
17342 }
17343
17344 switch (code)
17345 {
17346 case OMP_ATOMIC_READ:
17347 case NOP_EXPR: /* atomic write */
17348 v = c_parser_cast_expression (parser, NULL).value;
17349 non_lvalue_p = !lvalue_p (v);
17350 v = c_fully_fold (v, false, NULL, true);
17351 if (v == error_mark_node)
17352 goto saw_error;
17353 if (non_lvalue_p)
17354 v = non_lvalue (v);
17355 loc = c_parser_peek_token (parser)->location;
17356 if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
17357 goto saw_error;
17358 if (code == NOP_EXPR)
17359 {
17360 lhs = c_parser_expression (parser).value;
17361 lhs = c_fully_fold (lhs, false, NULL);
17362 if (lhs == error_mark_node)
17363 goto saw_error;
17364 }
17365 else
17366 {
17367 lhs = c_parser_cast_expression (parser, NULL).value;
17368 non_lvalue_p = !lvalue_p (lhs);
17369 lhs = c_fully_fold (lhs, false, NULL, true);
17370 if (lhs == error_mark_node)
17371 goto saw_error;
17372 if (non_lvalue_p)
17373 lhs = non_lvalue (lhs);
17374 }
17375 if (code == NOP_EXPR)
17376 {
17377 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
17378 opcode. */
17379 code = OMP_ATOMIC;
17380 rhs = lhs;
17381 lhs = v;
17382 v = NULL_TREE;
17383 }
17384 goto done;
17385 case OMP_ATOMIC_CAPTURE_NEW:
17386 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
17387 {
17388 c_parser_consume_token (parser);
17389 structured_block = true;
17390 }
17391 else
17392 {
17393 v = c_parser_cast_expression (parser, NULL).value;
17394 non_lvalue_p = !lvalue_p (v);
17395 v = c_fully_fold (v, false, NULL, true);
17396 if (v == error_mark_node)
17397 goto saw_error;
17398 if (non_lvalue_p)
17399 v = non_lvalue (v);
17400 if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
17401 goto saw_error;
17402 }
17403 break;
17404 default:
17405 break;
17406 }
17407
17408 /* For structured_block case we don't know yet whether
17409 old or new x should be captured. */
17410 restart:
17411 eloc = c_parser_peek_token (parser)->location;
17412 expr = c_parser_cast_expression (parser, NULL);
17413 lhs = expr.value;
17414 expr = default_function_array_conversion (eloc, expr);
17415 unfolded_lhs = expr.value;
17416 lhs = c_fully_fold (lhs, false, NULL, true);
17417 orig_lhs = lhs;
17418 switch (TREE_CODE (lhs))
17419 {
17420 case ERROR_MARK:
17421 saw_error:
17422 c_parser_skip_to_end_of_block_or_statement (parser);
17423 if (structured_block)
17424 {
17425 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
17426 c_parser_consume_token (parser);
17427 else if (code == OMP_ATOMIC_CAPTURE_NEW)
17428 {
17429 c_parser_skip_to_end_of_block_or_statement (parser);
17430 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
17431 c_parser_consume_token (parser);
17432 }
17433 }
17434 return;
17435
17436 case POSTINCREMENT_EXPR:
17437 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
17438 code = OMP_ATOMIC_CAPTURE_OLD;
17439 /* FALLTHROUGH */
17440 case PREINCREMENT_EXPR:
17441 lhs = TREE_OPERAND (lhs, 0);
17442 unfolded_lhs = NULL_TREE;
17443 opcode = PLUS_EXPR;
17444 rhs = integer_one_node;
17445 break;
17446
17447 case POSTDECREMENT_EXPR:
17448 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
17449 code = OMP_ATOMIC_CAPTURE_OLD;
17450 /* FALLTHROUGH */
17451 case PREDECREMENT_EXPR:
17452 lhs = TREE_OPERAND (lhs, 0);
17453 unfolded_lhs = NULL_TREE;
17454 opcode = MINUS_EXPR;
17455 rhs = integer_one_node;
17456 break;
17457
17458 case COMPOUND_EXPR:
17459 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
17460 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
17461 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
17462 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
17463 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
17464 (TREE_OPERAND (lhs, 1), 0), 0)))
17465 == BOOLEAN_TYPE)
17466 /* Undo effects of boolean_increment for post {in,de}crement. */
17467 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
17468 /* FALLTHRU */
17469 case MODIFY_EXPR:
17470 if (TREE_CODE (lhs) == MODIFY_EXPR
17471 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
17472 {
17473 /* Undo effects of boolean_increment. */
17474 if (integer_onep (TREE_OPERAND (lhs, 1)))
17475 {
17476 /* This is pre or post increment. */
17477 rhs = TREE_OPERAND (lhs, 1);
17478 lhs = TREE_OPERAND (lhs, 0);
17479 unfolded_lhs = NULL_TREE;
17480 opcode = NOP_EXPR;
17481 if (code == OMP_ATOMIC_CAPTURE_NEW
17482 && !structured_block
17483 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
17484 code = OMP_ATOMIC_CAPTURE_OLD;
17485 break;
17486 }
17487 if (TREE_CODE (TREE_OPERAND (lhs, 1)) == TRUTH_NOT_EXPR
17488 && TREE_OPERAND (lhs, 0)
17489 == TREE_OPERAND (TREE_OPERAND (lhs, 1), 0))
17490 {
17491 /* This is pre or post decrement. */
17492 rhs = TREE_OPERAND (lhs, 1);
17493 lhs = TREE_OPERAND (lhs, 0);
17494 unfolded_lhs = NULL_TREE;
17495 opcode = NOP_EXPR;
17496 if (code == OMP_ATOMIC_CAPTURE_NEW
17497 && !structured_block
17498 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
17499 code = OMP_ATOMIC_CAPTURE_OLD;
17500 break;
17501 }
17502 }
17503 /* FALLTHRU */
17504 default:
17505 if (!lvalue_p (unfolded_lhs))
17506 lhs = non_lvalue (lhs);
17507 switch (c_parser_peek_token (parser)->type)
17508 {
17509 case CPP_MULT_EQ:
17510 opcode = MULT_EXPR;
17511 break;
17512 case CPP_DIV_EQ:
17513 opcode = TRUNC_DIV_EXPR;
17514 break;
17515 case CPP_PLUS_EQ:
17516 opcode = PLUS_EXPR;
17517 break;
17518 case CPP_MINUS_EQ:
17519 opcode = MINUS_EXPR;
17520 break;
17521 case CPP_LSHIFT_EQ:
17522 opcode = LSHIFT_EXPR;
17523 break;
17524 case CPP_RSHIFT_EQ:
17525 opcode = RSHIFT_EXPR;
17526 break;
17527 case CPP_AND_EQ:
17528 opcode = BIT_AND_EXPR;
17529 break;
17530 case CPP_OR_EQ:
17531 opcode = BIT_IOR_EXPR;
17532 break;
17533 case CPP_XOR_EQ:
17534 opcode = BIT_XOR_EXPR;
17535 break;
17536 case CPP_EQ:
17537 c_parser_consume_token (parser);
17538 eloc = c_parser_peek_token (parser)->location;
17539 expr = c_parser_expr_no_commas (parser, NULL, unfolded_lhs);
17540 rhs1 = expr.value;
17541 switch (TREE_CODE (rhs1))
17542 {
17543 case MULT_EXPR:
17544 case TRUNC_DIV_EXPR:
17545 case RDIV_EXPR:
17546 case PLUS_EXPR:
17547 case MINUS_EXPR:
17548 case LSHIFT_EXPR:
17549 case RSHIFT_EXPR:
17550 case BIT_AND_EXPR:
17551 case BIT_IOR_EXPR:
17552 case BIT_XOR_EXPR:
17553 if (c_tree_equal (TREE_OPERAND (rhs1, 0), unfolded_lhs))
17554 {
17555 opcode = TREE_CODE (rhs1);
17556 rhs = c_fully_fold (TREE_OPERAND (rhs1, 1), false, NULL,
17557 true);
17558 rhs1 = c_fully_fold (TREE_OPERAND (rhs1, 0), false, NULL,
17559 true);
17560 goto stmt_done;
17561 }
17562 if (c_tree_equal (TREE_OPERAND (rhs1, 1), unfolded_lhs))
17563 {
17564 opcode = TREE_CODE (rhs1);
17565 rhs = c_fully_fold (TREE_OPERAND (rhs1, 0), false, NULL,
17566 true);
17567 rhs1 = c_fully_fold (TREE_OPERAND (rhs1, 1), false, NULL,
17568 true);
17569 swapped = !commutative_tree_code (opcode);
17570 goto stmt_done;
17571 }
17572 break;
17573 case ERROR_MARK:
17574 goto saw_error;
17575 default:
17576 break;
17577 }
17578 if (c_parser_peek_token (parser)->type == CPP_SEMICOLON)
17579 {
17580 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
17581 {
17582 code = OMP_ATOMIC_CAPTURE_OLD;
17583 v = lhs;
17584 lhs = NULL_TREE;
17585 expr = default_function_array_read_conversion (eloc, expr);
17586 unfolded_lhs1 = expr.value;
17587 lhs1 = c_fully_fold (unfolded_lhs1, false, NULL, true);
17588 rhs1 = NULL_TREE;
17589 c_parser_consume_token (parser);
17590 goto restart;
17591 }
17592 if (structured_block)
17593 {
17594 opcode = NOP_EXPR;
17595 expr = default_function_array_read_conversion (eloc, expr);
17596 rhs = c_fully_fold (expr.value, false, NULL, true);
17597 rhs1 = NULL_TREE;
17598 goto stmt_done;
17599 }
17600 }
17601 c_parser_error (parser, "invalid form of %<#pragma omp atomic%>");
17602 goto saw_error;
17603 default:
17604 c_parser_error (parser,
17605 "invalid operator for %<#pragma omp atomic%>");
17606 goto saw_error;
17607 }
17608
17609 /* Arrange to pass the location of the assignment operator to
17610 c_finish_omp_atomic. */
17611 loc = c_parser_peek_token (parser)->location;
17612 c_parser_consume_token (parser);
17613 eloc = c_parser_peek_token (parser)->location;
17614 expr = c_parser_expression (parser);
17615 expr = default_function_array_read_conversion (eloc, expr);
17616 rhs = expr.value;
17617 rhs = c_fully_fold (rhs, false, NULL, true);
17618 break;
17619 }
17620 stmt_done:
17621 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
17622 {
17623 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
17624 goto saw_error;
17625 v = c_parser_cast_expression (parser, NULL).value;
17626 non_lvalue_p = !lvalue_p (v);
17627 v = c_fully_fold (v, false, NULL, true);
17628 if (v == error_mark_node)
17629 goto saw_error;
17630 if (non_lvalue_p)
17631 v = non_lvalue (v);
17632 if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
17633 goto saw_error;
17634 eloc = c_parser_peek_token (parser)->location;
17635 expr = c_parser_cast_expression (parser, NULL);
17636 lhs1 = expr.value;
17637 expr = default_function_array_read_conversion (eloc, expr);
17638 unfolded_lhs1 = expr.value;
17639 lhs1 = c_fully_fold (lhs1, false, NULL, true);
17640 if (lhs1 == error_mark_node)
17641 goto saw_error;
17642 if (!lvalue_p (unfolded_lhs1))
17643 lhs1 = non_lvalue (lhs1);
17644 }
17645 if (structured_block)
17646 {
17647 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
17648 c_parser_require (parser, CPP_CLOSE_BRACE, "expected %<}%>");
17649 }
17650 done:
17651 if (unfolded_lhs && unfolded_lhs1
17652 && !c_tree_equal (unfolded_lhs, unfolded_lhs1))
17653 {
17654 error ("%<#pragma omp atomic capture%> uses two different "
17655 "expressions for memory");
17656 stmt = error_mark_node;
17657 }
17658 else
17659 stmt = c_finish_omp_atomic (loc, code, opcode, lhs, rhs, v, lhs1, rhs1,
17660 swapped, memory_order);
17661 if (stmt != error_mark_node)
17662 add_stmt (stmt);
17663
17664 if (!structured_block)
17665 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
17666 }
17667
17668
17669 /* OpenMP 2.5:
17670 # pragma omp barrier new-line
17671 */
17672
17673 static void
17674 c_parser_omp_barrier (c_parser *parser)
17675 {
17676 location_t loc = c_parser_peek_token (parser)->location;
17677 c_parser_consume_pragma (parser);
17678 c_parser_skip_to_pragma_eol (parser);
17679
17680 c_finish_omp_barrier (loc);
17681 }
17682
17683 /* OpenMP 2.5:
17684 # pragma omp critical [(name)] new-line
17685 structured-block
17686
17687 OpenMP 4.5:
17688 # pragma omp critical [(name) [hint(expression)]] new-line
17689
17690 LOC is the location of the #pragma itself. */
17691
17692 #define OMP_CRITICAL_CLAUSE_MASK \
17693 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_HINT) )
17694
17695 static tree
17696 c_parser_omp_critical (location_t loc, c_parser *parser, bool *if_p)
17697 {
17698 tree stmt, name = NULL_TREE, clauses = NULL_TREE;
17699
17700 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
17701 {
17702 c_parser_consume_token (parser);
17703 if (c_parser_next_token_is (parser, CPP_NAME))
17704 {
17705 name = c_parser_peek_token (parser)->value;
17706 c_parser_consume_token (parser);
17707 c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
17708 }
17709 else
17710 c_parser_error (parser, "expected identifier");
17711
17712 if (c_parser_next_token_is (parser, CPP_COMMA)
17713 && c_parser_peek_2nd_token (parser)->type == CPP_NAME)
17714 c_parser_consume_token (parser);
17715
17716 clauses = c_parser_omp_all_clauses (parser,
17717 OMP_CRITICAL_CLAUSE_MASK,
17718 "#pragma omp critical");
17719 }
17720 else
17721 {
17722 if (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
17723 c_parser_error (parser, "expected %<(%> or end of line");
17724 c_parser_skip_to_pragma_eol (parser);
17725 }
17726
17727 stmt = c_parser_omp_structured_block (parser, if_p);
17728 return c_finish_omp_critical (loc, stmt, name, clauses);
17729 }
17730
17731 /* OpenMP 5.0:
17732 # pragma omp depobj ( depobj ) depobj-clause new-line
17733
17734 depobj-clause:
17735 depend (dependence-type : locator)
17736 destroy
17737 update (dependence-type)
17738
17739 dependence-type:
17740 in
17741 out
17742 inout
17743 mutexinout */
17744
17745 static void
17746 c_parser_omp_depobj (c_parser *parser)
17747 {
17748 location_t loc = c_parser_peek_token (parser)->location;
17749 c_parser_consume_pragma (parser);
17750 matching_parens parens;
17751 if (!parens.require_open (parser))
17752 {
17753 c_parser_skip_to_pragma_eol (parser);
17754 return;
17755 }
17756
17757 tree depobj = c_parser_expr_no_commas (parser, NULL).value;
17758 if (depobj != error_mark_node)
17759 {
17760 if (!lvalue_p (depobj))
17761 {
17762 error_at (EXPR_LOC_OR_LOC (depobj, loc),
17763 "%<depobj%> expression is not lvalue expression");
17764 depobj = error_mark_node;
17765 }
17766 else
17767 {
17768 tree addr = build_unary_op (EXPR_LOC_OR_LOC (depobj, loc), ADDR_EXPR,
17769 depobj, false);
17770 if (addr == error_mark_node)
17771 depobj = error_mark_node;
17772 else
17773 depobj = build_indirect_ref (EXPR_LOC_OR_LOC (depobj, loc),
17774 addr, RO_UNARY_STAR);
17775 }
17776 }
17777
17778 parens.skip_until_found_close (parser);
17779 tree clause = NULL_TREE;
17780 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_SOURCE;
17781 location_t c_loc = c_parser_peek_token (parser)->location;
17782 if (c_parser_next_token_is (parser, CPP_NAME))
17783 {
17784 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
17785
17786 c_parser_consume_token (parser);
17787 if (!strcmp ("depend", p))
17788 {
17789 clause = c_parser_omp_clause_depend (parser, NULL_TREE);
17790 clause = c_finish_omp_clauses (clause, C_ORT_OMP);
17791 if (!clause)
17792 clause = error_mark_node;
17793 }
17794 else if (!strcmp ("destroy", p))
17795 kind = OMP_CLAUSE_DEPEND_LAST;
17796 else if (!strcmp ("update", p))
17797 {
17798 matching_parens c_parens;
17799 if (c_parens.require_open (parser))
17800 {
17801 location_t c2_loc = c_parser_peek_token (parser)->location;
17802 if (c_parser_next_token_is (parser, CPP_NAME))
17803 {
17804 const char *p2
17805 = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
17806
17807 c_parser_consume_token (parser);
17808 if (!strcmp ("in", p2))
17809 kind = OMP_CLAUSE_DEPEND_IN;
17810 else if (!strcmp ("out", p2))
17811 kind = OMP_CLAUSE_DEPEND_OUT;
17812 else if (!strcmp ("inout", p2))
17813 kind = OMP_CLAUSE_DEPEND_INOUT;
17814 else if (!strcmp ("mutexinoutset", p2))
17815 kind = OMP_CLAUSE_DEPEND_MUTEXINOUTSET;
17816 }
17817 if (kind == OMP_CLAUSE_DEPEND_SOURCE)
17818 {
17819 clause = error_mark_node;
17820 error_at (c2_loc, "expected %<in%>, %<out%>, %<inout%> or "
17821 "%<mutexinoutset%>");
17822 }
17823 c_parens.skip_until_found_close (parser);
17824 }
17825 else
17826 clause = error_mark_node;
17827 }
17828 }
17829 if (!clause && kind == OMP_CLAUSE_DEPEND_SOURCE)
17830 {
17831 clause = error_mark_node;
17832 error_at (c_loc, "expected %<depend%>, %<destroy%> or %<update%> clause");
17833 }
17834 c_parser_skip_to_pragma_eol (parser);
17835
17836 c_finish_omp_depobj (loc, depobj, kind, clause);
17837 }
17838
17839
17840 /* OpenMP 2.5:
17841 # pragma omp flush flush-vars[opt] new-line
17842
17843 flush-vars:
17844 ( variable-list )
17845
17846 OpenMP 5.0:
17847 # pragma omp flush memory-order-clause new-line */
17848
17849 static void
17850 c_parser_omp_flush (c_parser *parser)
17851 {
17852 location_t loc = c_parser_peek_token (parser)->location;
17853 c_parser_consume_pragma (parser);
17854 enum memmodel mo = MEMMODEL_LAST;
17855 if (c_parser_next_token_is (parser, CPP_NAME))
17856 {
17857 const char *p
17858 = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
17859
17860 if (!strcmp (p, "acq_rel"))
17861 mo = MEMMODEL_ACQ_REL;
17862 else if (!strcmp (p, "release"))
17863 mo = MEMMODEL_RELEASE;
17864 else if (!strcmp (p, "acquire"))
17865 mo = MEMMODEL_ACQUIRE;
17866 else
17867 error_at (c_parser_peek_token (parser)->location,
17868 "expected %<acq_rel%>, %<release%> or %<acquire%>");
17869 c_parser_consume_token (parser);
17870 }
17871 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
17872 {
17873 if (mo != MEMMODEL_LAST)
17874 error_at (c_parser_peek_token (parser)->location,
17875 "%<flush%> list specified together with memory order "
17876 "clause");
17877 c_parser_omp_var_list_parens (parser, OMP_CLAUSE_ERROR, NULL);
17878 }
17879 else if (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
17880 c_parser_error (parser, "expected %<(%> or end of line");
17881 c_parser_skip_to_pragma_eol (parser);
17882
17883 c_finish_omp_flush (loc, mo);
17884 }
17885
17886 /* OpenMP 5.0:
17887
17888 scan-loop-body:
17889 { structured-block scan-directive structured-block } */
17890
17891 static void
17892 c_parser_omp_scan_loop_body (c_parser *parser, bool open_brace_parsed)
17893 {
17894 tree substmt;
17895 location_t loc;
17896 tree clauses = NULL_TREE;
17897
17898 loc = c_parser_peek_token (parser)->location;
17899 if (!open_brace_parsed
17900 && !c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
17901 {
17902 /* Avoid skipping until the end of the block. */
17903 parser->error = false;
17904 return;
17905 }
17906
17907 substmt = c_parser_omp_structured_block (parser, NULL);
17908 substmt = build2 (OMP_SCAN, void_type_node, substmt, NULL_TREE);
17909 SET_EXPR_LOCATION (substmt, loc);
17910 add_stmt (substmt);
17911
17912 loc = c_parser_peek_token (parser)->location;
17913 if (c_parser_peek_token (parser)->pragma_kind == PRAGMA_OMP_SCAN)
17914 {
17915 enum omp_clause_code clause = OMP_CLAUSE_ERROR;
17916
17917 c_parser_consume_pragma (parser);
17918
17919 if (c_parser_next_token_is (parser, CPP_NAME))
17920 {
17921 const char *p
17922 = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
17923 if (strcmp (p, "inclusive") == 0)
17924 clause = OMP_CLAUSE_INCLUSIVE;
17925 else if (strcmp (p, "exclusive") == 0)
17926 clause = OMP_CLAUSE_EXCLUSIVE;
17927 }
17928 if (clause != OMP_CLAUSE_ERROR)
17929 {
17930 c_parser_consume_token (parser);
17931 clauses = c_parser_omp_var_list_parens (parser, clause, NULL_TREE);
17932 }
17933 else
17934 c_parser_error (parser, "expected %<inclusive%> or "
17935 "%<exclusive%> clause");
17936 c_parser_skip_to_pragma_eol (parser);
17937 }
17938 else
17939 error ("expected %<#pragma omp scan%>");
17940
17941 clauses = c_finish_omp_clauses (clauses, C_ORT_OMP);
17942 substmt = c_parser_omp_structured_block (parser, NULL);
17943 substmt = build2 (OMP_SCAN, void_type_node, substmt, clauses);
17944 SET_EXPR_LOCATION (substmt, loc);
17945 add_stmt (substmt);
17946
17947 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE,
17948 "expected %<}%>");
17949 }
17950
17951 /* Parse the restricted form of loop statements allowed by OpenACC and OpenMP.
17952 The real trick here is to determine the loop control variable early
17953 so that we can push a new decl if necessary to make it private.
17954 LOC is the location of the "acc" or "omp" in "#pragma acc" or "#pragma omp",
17955 respectively. */
17956
17957 static tree
17958 c_parser_omp_for_loop (location_t loc, c_parser *parser, enum tree_code code,
17959 tree clauses, tree *cclauses, bool *if_p)
17960 {
17961 tree decl, cond, incr, save_break, save_cont, body, init, stmt, cl;
17962 tree declv, condv, incrv, initv, ret = NULL_TREE;
17963 tree pre_body = NULL_TREE, this_pre_body;
17964 tree ordered_cl = NULL_TREE;
17965 bool fail = false, open_brace_parsed = false;
17966 int i, collapse = 1, ordered = 0, count, nbraces = 0;
17967 location_t for_loc;
17968 bool tiling = false;
17969 bool inscan = false;
17970 vec<tree, va_gc> *for_block = make_tree_vector ();
17971
17972 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
17973 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
17974 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl));
17975 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_TILE)
17976 {
17977 tiling = true;
17978 collapse = list_length (OMP_CLAUSE_TILE_LIST (cl));
17979 }
17980 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_ORDERED
17981 && OMP_CLAUSE_ORDERED_EXPR (cl))
17982 {
17983 ordered_cl = cl;
17984 ordered = tree_to_shwi (OMP_CLAUSE_ORDERED_EXPR (cl));
17985 }
17986 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_REDUCTION
17987 && OMP_CLAUSE_REDUCTION_INSCAN (cl)
17988 && (code == OMP_SIMD || code == OMP_FOR))
17989 inscan = true;
17990
17991 if (ordered && ordered < collapse)
17992 {
17993 error_at (OMP_CLAUSE_LOCATION (ordered_cl),
17994 "%<ordered%> clause parameter is less than %<collapse%>");
17995 OMP_CLAUSE_ORDERED_EXPR (ordered_cl)
17996 = build_int_cst (NULL_TREE, collapse);
17997 ordered = collapse;
17998 }
17999 if (ordered)
18000 {
18001 for (tree *pc = &clauses; *pc; )
18002 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LINEAR)
18003 {
18004 error_at (OMP_CLAUSE_LOCATION (*pc),
18005 "%<linear%> clause may not be specified together "
18006 "with %<ordered%> clause with a parameter");
18007 *pc = OMP_CLAUSE_CHAIN (*pc);
18008 }
18009 else
18010 pc = &OMP_CLAUSE_CHAIN (*pc);
18011 }
18012
18013 gcc_assert (tiling || (collapse >= 1 && ordered >= 0));
18014 count = ordered ? ordered : collapse;
18015
18016 declv = make_tree_vec (count);
18017 initv = make_tree_vec (count);
18018 condv = make_tree_vec (count);
18019 incrv = make_tree_vec (count);
18020
18021 if (!c_parser_next_token_is_keyword (parser, RID_FOR))
18022 {
18023 c_parser_error (parser, "for statement expected");
18024 return NULL;
18025 }
18026 for_loc = c_parser_peek_token (parser)->location;
18027 c_parser_consume_token (parser);
18028
18029 for (i = 0; i < count; i++)
18030 {
18031 int bracecount = 0;
18032
18033 matching_parens parens;
18034 if (!parens.require_open (parser))
18035 goto pop_scopes;
18036
18037 /* Parse the initialization declaration or expression. */
18038 if (c_parser_next_tokens_start_declaration (parser))
18039 {
18040 if (i > 0)
18041 vec_safe_push (for_block, c_begin_compound_stmt (true));
18042 this_pre_body = push_stmt_list ();
18043 c_parser_declaration_or_fndef (parser, true, true, true, true, true,
18044 NULL, vNULL);
18045 if (this_pre_body)
18046 {
18047 this_pre_body = pop_stmt_list (this_pre_body);
18048 if (pre_body)
18049 {
18050 tree t = pre_body;
18051 pre_body = push_stmt_list ();
18052 add_stmt (t);
18053 add_stmt (this_pre_body);
18054 pre_body = pop_stmt_list (pre_body);
18055 }
18056 else
18057 pre_body = this_pre_body;
18058 }
18059 decl = check_for_loop_decls (for_loc, flag_isoc99);
18060 if (decl == NULL)
18061 goto error_init;
18062 if (DECL_INITIAL (decl) == error_mark_node)
18063 decl = error_mark_node;
18064 init = decl;
18065 }
18066 else if (c_parser_next_token_is (parser, CPP_NAME)
18067 && c_parser_peek_2nd_token (parser)->type == CPP_EQ)
18068 {
18069 struct c_expr decl_exp;
18070 struct c_expr init_exp;
18071 location_t init_loc;
18072
18073 decl_exp = c_parser_postfix_expression (parser);
18074 decl = decl_exp.value;
18075
18076 c_parser_require (parser, CPP_EQ, "expected %<=%>");
18077
18078 init_loc = c_parser_peek_token (parser)->location;
18079 init_exp = c_parser_expr_no_commas (parser, NULL);
18080 init_exp = default_function_array_read_conversion (init_loc,
18081 init_exp);
18082 init = build_modify_expr (init_loc, decl, decl_exp.original_type,
18083 NOP_EXPR, init_loc, init_exp.value,
18084 init_exp.original_type);
18085 init = c_process_expr_stmt (init_loc, init);
18086
18087 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
18088 }
18089 else
18090 {
18091 error_init:
18092 c_parser_error (parser,
18093 "expected iteration declaration or initialization");
18094 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
18095 "expected %<)%>");
18096 fail = true;
18097 goto parse_next;
18098 }
18099
18100 /* Parse the loop condition. */
18101 cond = NULL_TREE;
18102 if (c_parser_next_token_is_not (parser, CPP_SEMICOLON))
18103 {
18104 location_t cond_loc = c_parser_peek_token (parser)->location;
18105 struct c_expr cond_expr
18106 = c_parser_binary_expression (parser, NULL, NULL_TREE);
18107
18108 cond = cond_expr.value;
18109 cond = c_objc_common_truthvalue_conversion (cond_loc, cond);
18110 if (COMPARISON_CLASS_P (cond))
18111 {
18112 tree op0 = TREE_OPERAND (cond, 0), op1 = TREE_OPERAND (cond, 1);
18113 op0 = c_fully_fold (op0, false, NULL);
18114 op1 = c_fully_fold (op1, false, NULL);
18115 TREE_OPERAND (cond, 0) = op0;
18116 TREE_OPERAND (cond, 1) = op1;
18117 }
18118 switch (cond_expr.original_code)
18119 {
18120 case GT_EXPR:
18121 case GE_EXPR:
18122 case LT_EXPR:
18123 case LE_EXPR:
18124 break;
18125 case NE_EXPR:
18126 if (code != OACC_LOOP)
18127 break;
18128 /* FALLTHRU. */
18129 default:
18130 /* Can't be cond = error_mark_node, because we want to preserve
18131 the location until c_finish_omp_for. */
18132 cond = build1 (NOP_EXPR, boolean_type_node, error_mark_node);
18133 break;
18134 }
18135 protected_set_expr_location (cond, cond_loc);
18136 }
18137 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
18138
18139 /* Parse the increment expression. */
18140 incr = NULL_TREE;
18141 if (c_parser_next_token_is_not (parser, CPP_CLOSE_PAREN))
18142 {
18143 location_t incr_loc = c_parser_peek_token (parser)->location;
18144
18145 incr = c_process_expr_stmt (incr_loc,
18146 c_parser_expression (parser).value);
18147 }
18148 parens.skip_until_found_close (parser);
18149
18150 if (decl == NULL || decl == error_mark_node || init == error_mark_node)
18151 fail = true;
18152 else
18153 {
18154 TREE_VEC_ELT (declv, i) = decl;
18155 TREE_VEC_ELT (initv, i) = init;
18156 TREE_VEC_ELT (condv, i) = cond;
18157 TREE_VEC_ELT (incrv, i) = incr;
18158 }
18159
18160 parse_next:
18161 if (i == count - 1)
18162 break;
18163
18164 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
18165 in between the collapsed for loops to be still considered perfectly
18166 nested. Hopefully the final version clarifies this.
18167 For now handle (multiple) {'s and empty statements. */
18168 do
18169 {
18170 if (c_parser_next_token_is_keyword (parser, RID_FOR))
18171 {
18172 c_parser_consume_token (parser);
18173 break;
18174 }
18175 else if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
18176 {
18177 c_parser_consume_token (parser);
18178 bracecount++;
18179 }
18180 else if (bracecount
18181 && c_parser_next_token_is (parser, CPP_SEMICOLON))
18182 c_parser_consume_token (parser);
18183 else
18184 {
18185 c_parser_error (parser, "not enough perfectly nested loops");
18186 if (bracecount)
18187 {
18188 open_brace_parsed = true;
18189 bracecount--;
18190 }
18191 fail = true;
18192 count = 0;
18193 break;
18194 }
18195 }
18196 while (1);
18197
18198 nbraces += bracecount;
18199 }
18200
18201 if (nbraces)
18202 if_p = NULL;
18203
18204 save_break = c_break_label;
18205 c_break_label = size_one_node;
18206 save_cont = c_cont_label;
18207 c_cont_label = NULL_TREE;
18208 body = push_stmt_list ();
18209
18210 if (inscan)
18211 c_parser_omp_scan_loop_body (parser, open_brace_parsed);
18212 else if (open_brace_parsed)
18213 {
18214 location_t here = c_parser_peek_token (parser)->location;
18215 stmt = c_begin_compound_stmt (true);
18216 c_parser_compound_statement_nostart (parser);
18217 add_stmt (c_end_compound_stmt (here, stmt, true));
18218 }
18219 else
18220 add_stmt (c_parser_c99_block_statement (parser, if_p));
18221 if (c_cont_label)
18222 {
18223 tree t = build1 (LABEL_EXPR, void_type_node, c_cont_label);
18224 SET_EXPR_LOCATION (t, loc);
18225 add_stmt (t);
18226 }
18227
18228 body = pop_stmt_list (body);
18229 c_break_label = save_break;
18230 c_cont_label = save_cont;
18231
18232 while (nbraces)
18233 {
18234 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
18235 {
18236 c_parser_consume_token (parser);
18237 nbraces--;
18238 }
18239 else if (c_parser_next_token_is (parser, CPP_SEMICOLON))
18240 c_parser_consume_token (parser);
18241 else
18242 {
18243 c_parser_error (parser, "collapsed loops not perfectly nested");
18244 while (nbraces)
18245 {
18246 location_t here = c_parser_peek_token (parser)->location;
18247 stmt = c_begin_compound_stmt (true);
18248 add_stmt (body);
18249 c_parser_compound_statement_nostart (parser);
18250 body = c_end_compound_stmt (here, stmt, true);
18251 nbraces--;
18252 }
18253 goto pop_scopes;
18254 }
18255 }
18256
18257 /* Only bother calling c_finish_omp_for if we haven't already generated
18258 an error from the initialization parsing. */
18259 if (!fail)
18260 {
18261 stmt = c_finish_omp_for (loc, code, declv, NULL, initv, condv,
18262 incrv, body, pre_body, true);
18263
18264 /* Check for iterators appearing in lb, b or incr expressions. */
18265 if (stmt && !c_omp_check_loop_iv (stmt, declv, NULL))
18266 stmt = NULL_TREE;
18267
18268 if (stmt)
18269 {
18270 add_stmt (stmt);
18271
18272 if (cclauses != NULL
18273 && cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL] != NULL)
18274 {
18275 tree *c;
18276 for (c = &cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL]; *c ; )
18277 if (OMP_CLAUSE_CODE (*c) != OMP_CLAUSE_FIRSTPRIVATE
18278 && OMP_CLAUSE_CODE (*c) != OMP_CLAUSE_LASTPRIVATE)
18279 c = &OMP_CLAUSE_CHAIN (*c);
18280 else
18281 {
18282 for (i = 0; i < count; i++)
18283 if (TREE_VEC_ELT (declv, i) == OMP_CLAUSE_DECL (*c))
18284 break;
18285 if (i == count)
18286 c = &OMP_CLAUSE_CHAIN (*c);
18287 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE)
18288 {
18289 error_at (loc,
18290 "iteration variable %qD should not be firstprivate",
18291 OMP_CLAUSE_DECL (*c));
18292 *c = OMP_CLAUSE_CHAIN (*c);
18293 }
18294 else
18295 {
18296 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
18297 tree l = *c;
18298 *c = OMP_CLAUSE_CHAIN (*c);
18299 if (code == OMP_SIMD)
18300 {
18301 OMP_CLAUSE_CHAIN (l)
18302 = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
18303 cclauses[C_OMP_CLAUSE_SPLIT_FOR] = l;
18304 }
18305 else
18306 {
18307 OMP_CLAUSE_CHAIN (l) = clauses;
18308 clauses = l;
18309 }
18310 }
18311 }
18312 }
18313 OMP_FOR_CLAUSES (stmt) = clauses;
18314 }
18315 ret = stmt;
18316 }
18317 pop_scopes:
18318 while (!for_block->is_empty ())
18319 {
18320 /* FIXME diagnostics: LOC below should be the actual location of
18321 this particular for block. We need to build a list of
18322 locations to go along with FOR_BLOCK. */
18323 stmt = c_end_compound_stmt (loc, for_block->pop (), true);
18324 add_stmt (stmt);
18325 }
18326 release_tree_vector (for_block);
18327 return ret;
18328 }
18329
18330 /* Helper function for OpenMP parsing, split clauses and call
18331 finish_omp_clauses on each of the set of clauses afterwards. */
18332
18333 static void
18334 omp_split_clauses (location_t loc, enum tree_code code,
18335 omp_clause_mask mask, tree clauses, tree *cclauses)
18336 {
18337 int i;
18338 c_omp_split_clauses (loc, code, mask, clauses, cclauses);
18339 for (i = 0; i < C_OMP_CLAUSE_SPLIT_COUNT; i++)
18340 if (cclauses[i])
18341 cclauses[i] = c_finish_omp_clauses (cclauses[i], C_ORT_OMP);
18342 }
18343
18344 /* OpenMP 5.0:
18345 #pragma omp loop loop-clause[optseq] new-line
18346 for-loop
18347
18348 LOC is the location of the #pragma token.
18349 */
18350
18351 #define OMP_LOOP_CLAUSE_MASK \
18352 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
18353 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
18354 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
18355 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
18356 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_BIND) \
18357 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDER))
18358
18359 static tree
18360 c_parser_omp_loop (location_t loc, c_parser *parser,
18361 char *p_name, omp_clause_mask mask, tree *cclauses,
18362 bool *if_p)
18363 {
18364 tree block, clauses, ret;
18365
18366 strcat (p_name, " loop");
18367 mask |= OMP_LOOP_CLAUSE_MASK;
18368
18369 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
18370 if (cclauses)
18371 {
18372 omp_split_clauses (loc, OMP_LOOP, mask, clauses, cclauses);
18373 clauses = cclauses[C_OMP_CLAUSE_SPLIT_LOOP];
18374 }
18375
18376 block = c_begin_compound_stmt (true);
18377 ret = c_parser_omp_for_loop (loc, parser, OMP_LOOP, clauses, cclauses, if_p);
18378 block = c_end_compound_stmt (loc, block, true);
18379 add_stmt (block);
18380
18381 return ret;
18382 }
18383
18384 /* OpenMP 4.0:
18385 #pragma omp simd simd-clause[optseq] new-line
18386 for-loop
18387
18388 LOC is the location of the #pragma token.
18389 */
18390
18391 #define OMP_SIMD_CLAUSE_MASK \
18392 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
18393 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
18394 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
18395 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
18396 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
18397 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
18398 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
18399 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
18400 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
18401 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NONTEMPORAL) \
18402 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDER))
18403
18404 static tree
18405 c_parser_omp_simd (location_t loc, c_parser *parser,
18406 char *p_name, omp_clause_mask mask, tree *cclauses,
18407 bool *if_p)
18408 {
18409 tree block, clauses, ret;
18410
18411 strcat (p_name, " simd");
18412 mask |= OMP_SIMD_CLAUSE_MASK;
18413
18414 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
18415 if (cclauses)
18416 {
18417 omp_split_clauses (loc, OMP_SIMD, mask, clauses, cclauses);
18418 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SIMD];
18419 tree c = omp_find_clause (cclauses[C_OMP_CLAUSE_SPLIT_FOR],
18420 OMP_CLAUSE_ORDERED);
18421 if (c && OMP_CLAUSE_ORDERED_EXPR (c))
18422 {
18423 error_at (OMP_CLAUSE_LOCATION (c),
18424 "%<ordered%> clause with parameter may not be specified "
18425 "on %qs construct", p_name);
18426 OMP_CLAUSE_ORDERED_EXPR (c) = NULL_TREE;
18427 }
18428 }
18429
18430 block = c_begin_compound_stmt (true);
18431 ret = c_parser_omp_for_loop (loc, parser, OMP_SIMD, clauses, cclauses, if_p);
18432 block = c_end_compound_stmt (loc, block, true);
18433 add_stmt (block);
18434
18435 return ret;
18436 }
18437
18438 /* OpenMP 2.5:
18439 #pragma omp for for-clause[optseq] new-line
18440 for-loop
18441
18442 OpenMP 4.0:
18443 #pragma omp for simd for-simd-clause[optseq] new-line
18444 for-loop
18445
18446 LOC is the location of the #pragma token.
18447 */
18448
18449 #define OMP_FOR_CLAUSE_MASK \
18450 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
18451 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
18452 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
18453 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
18454 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
18455 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
18456 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
18457 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
18458 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
18459 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDER))
18460
18461 static tree
18462 c_parser_omp_for (location_t loc, c_parser *parser,
18463 char *p_name, omp_clause_mask mask, tree *cclauses,
18464 bool *if_p)
18465 {
18466 tree block, clauses, ret;
18467
18468 strcat (p_name, " for");
18469 mask |= OMP_FOR_CLAUSE_MASK;
18470 /* parallel for{, simd} disallows nowait clause, but for
18471 target {teams distribute ,}parallel for{, simd} it should be accepted. */
18472 if (cclauses && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) == 0)
18473 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
18474 /* Composite distribute parallel for{, simd} disallows ordered clause. */
18475 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
18476 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED);
18477
18478 if (c_parser_next_token_is (parser, CPP_NAME))
18479 {
18480 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
18481
18482 if (strcmp (p, "simd") == 0)
18483 {
18484 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
18485 if (cclauses == NULL)
18486 cclauses = cclauses_buf;
18487
18488 c_parser_consume_token (parser);
18489 if (!flag_openmp) /* flag_openmp_simd */
18490 return c_parser_omp_simd (loc, parser, p_name, mask, cclauses,
18491 if_p);
18492 block = c_begin_compound_stmt (true);
18493 ret = c_parser_omp_simd (loc, parser, p_name, mask, cclauses, if_p);
18494 block = c_end_compound_stmt (loc, block, true);
18495 if (ret == NULL_TREE)
18496 return ret;
18497 ret = make_node (OMP_FOR);
18498 TREE_TYPE (ret) = void_type_node;
18499 OMP_FOR_BODY (ret) = block;
18500 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
18501 SET_EXPR_LOCATION (ret, loc);
18502 add_stmt (ret);
18503 return ret;
18504 }
18505 }
18506 if (!flag_openmp) /* flag_openmp_simd */
18507 {
18508 c_parser_skip_to_pragma_eol (parser, false);
18509 return NULL_TREE;
18510 }
18511
18512 /* Composite distribute parallel for disallows linear clause. */
18513 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
18514 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR);
18515
18516 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
18517 if (cclauses)
18518 {
18519 omp_split_clauses (loc, OMP_FOR, mask, clauses, cclauses);
18520 clauses = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
18521 }
18522
18523 block = c_begin_compound_stmt (true);
18524 ret = c_parser_omp_for_loop (loc, parser, OMP_FOR, clauses, cclauses, if_p);
18525 block = c_end_compound_stmt (loc, block, true);
18526 add_stmt (block);
18527
18528 return ret;
18529 }
18530
18531 static tree c_parser_omp_taskloop (location_t, c_parser *, char *,
18532 omp_clause_mask, tree *, bool *);
18533
18534 /* OpenMP 2.5:
18535 # pragma omp master new-line
18536 structured-block
18537
18538 LOC is the location of the #pragma token.
18539 */
18540
18541 static tree
18542 c_parser_omp_master (location_t loc, c_parser *parser,
18543 char *p_name, omp_clause_mask mask, tree *cclauses,
18544 bool *if_p)
18545 {
18546 tree block, clauses, ret;
18547
18548 strcat (p_name, " master");
18549
18550 if (c_parser_next_token_is (parser, CPP_NAME))
18551 {
18552 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
18553
18554 if (strcmp (p, "taskloop") == 0)
18555 {
18556 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
18557 if (cclauses == NULL)
18558 cclauses = cclauses_buf;
18559
18560 c_parser_consume_token (parser);
18561 if (!flag_openmp) /* flag_openmp_simd */
18562 return c_parser_omp_taskloop (loc, parser, p_name, mask, cclauses,
18563 if_p);
18564 block = c_begin_compound_stmt (true);
18565 ret = c_parser_omp_taskloop (loc, parser, p_name, mask, cclauses,
18566 if_p);
18567 block = c_end_compound_stmt (loc, block, true);
18568 if (ret == NULL_TREE)
18569 return ret;
18570 ret = c_finish_omp_master (loc, block);
18571 return ret;
18572 }
18573 }
18574 if (!flag_openmp) /* flag_openmp_simd */
18575 {
18576 c_parser_skip_to_pragma_eol (parser, false);
18577 return NULL_TREE;
18578 }
18579
18580 if (cclauses)
18581 {
18582 clauses = c_parser_omp_all_clauses (parser, mask, p_name, false);
18583 omp_split_clauses (loc, OMP_MASTER, mask, clauses, cclauses);
18584 }
18585 else
18586 c_parser_skip_to_pragma_eol (parser);
18587
18588 return c_finish_omp_master (loc, c_parser_omp_structured_block (parser,
18589 if_p));
18590 }
18591
18592 /* OpenMP 2.5:
18593 # pragma omp ordered new-line
18594 structured-block
18595
18596 OpenMP 4.5:
18597 # pragma omp ordered ordered-clauses new-line
18598 structured-block
18599
18600 # pragma omp ordered depend-clauses new-line */
18601
18602 #define OMP_ORDERED_CLAUSE_MASK \
18603 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREADS) \
18604 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMD))
18605
18606 #define OMP_ORDERED_DEPEND_CLAUSE_MASK \
18607 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
18608
18609 static bool
18610 c_parser_omp_ordered (c_parser *parser, enum pragma_context context,
18611 bool *if_p)
18612 {
18613 location_t loc = c_parser_peek_token (parser)->location;
18614 c_parser_consume_pragma (parser);
18615
18616 if (context != pragma_stmt && context != pragma_compound)
18617 {
18618 c_parser_error (parser, "expected declaration specifiers");
18619 c_parser_skip_to_pragma_eol (parser, false);
18620 return false;
18621 }
18622
18623 if (c_parser_next_token_is (parser, CPP_NAME))
18624 {
18625 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
18626
18627 if (!strcmp ("depend", p))
18628 {
18629 if (!flag_openmp) /* flag_openmp_simd */
18630 {
18631 c_parser_skip_to_pragma_eol (parser, false);
18632 return false;
18633 }
18634 if (context == pragma_stmt)
18635 {
18636 error_at (loc,
18637 "%<#pragma omp ordered%> with %<depend%> clause may "
18638 "only be used in compound statements");
18639 c_parser_skip_to_pragma_eol (parser, false);
18640 return false;
18641 }
18642
18643 tree clauses
18644 = c_parser_omp_all_clauses (parser,
18645 OMP_ORDERED_DEPEND_CLAUSE_MASK,
18646 "#pragma omp ordered");
18647 c_finish_omp_ordered (loc, clauses, NULL_TREE);
18648 return false;
18649 }
18650 }
18651
18652 tree clauses = c_parser_omp_all_clauses (parser, OMP_ORDERED_CLAUSE_MASK,
18653 "#pragma omp ordered");
18654
18655 if (!flag_openmp /* flag_openmp_simd */
18656 && omp_find_clause (clauses, OMP_CLAUSE_SIMD) == NULL_TREE)
18657 return false;
18658
18659 c_finish_omp_ordered (loc, clauses,
18660 c_parser_omp_structured_block (parser, if_p));
18661 return true;
18662 }
18663
18664 /* OpenMP 2.5:
18665
18666 section-scope:
18667 { section-sequence }
18668
18669 section-sequence:
18670 section-directive[opt] structured-block
18671 section-sequence section-directive structured-block
18672
18673 SECTIONS_LOC is the location of the #pragma omp sections. */
18674
18675 static tree
18676 c_parser_omp_sections_scope (location_t sections_loc, c_parser *parser)
18677 {
18678 tree stmt, substmt;
18679 bool error_suppress = false;
18680 location_t loc;
18681
18682 loc = c_parser_peek_token (parser)->location;
18683 if (!c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
18684 {
18685 /* Avoid skipping until the end of the block. */
18686 parser->error = false;
18687 return NULL_TREE;
18688 }
18689
18690 stmt = push_stmt_list ();
18691
18692 if (c_parser_peek_token (parser)->pragma_kind != PRAGMA_OMP_SECTION)
18693 {
18694 substmt = c_parser_omp_structured_block (parser, NULL);
18695 substmt = build1 (OMP_SECTION, void_type_node, substmt);
18696 SET_EXPR_LOCATION (substmt, loc);
18697 add_stmt (substmt);
18698 }
18699
18700 while (1)
18701 {
18702 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
18703 break;
18704 if (c_parser_next_token_is (parser, CPP_EOF))
18705 break;
18706
18707 loc = c_parser_peek_token (parser)->location;
18708 if (c_parser_peek_token (parser)->pragma_kind == PRAGMA_OMP_SECTION)
18709 {
18710 c_parser_consume_pragma (parser);
18711 c_parser_skip_to_pragma_eol (parser);
18712 error_suppress = false;
18713 }
18714 else if (!error_suppress)
18715 {
18716 error_at (loc, "expected %<#pragma omp section%> or %<}%>");
18717 error_suppress = true;
18718 }
18719
18720 substmt = c_parser_omp_structured_block (parser, NULL);
18721 substmt = build1 (OMP_SECTION, void_type_node, substmt);
18722 SET_EXPR_LOCATION (substmt, loc);
18723 add_stmt (substmt);
18724 }
18725 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE,
18726 "expected %<#pragma omp section%> or %<}%>");
18727
18728 substmt = pop_stmt_list (stmt);
18729
18730 stmt = make_node (OMP_SECTIONS);
18731 SET_EXPR_LOCATION (stmt, sections_loc);
18732 TREE_TYPE (stmt) = void_type_node;
18733 OMP_SECTIONS_BODY (stmt) = substmt;
18734
18735 return add_stmt (stmt);
18736 }
18737
18738 /* OpenMP 2.5:
18739 # pragma omp sections sections-clause[optseq] newline
18740 sections-scope
18741
18742 LOC is the location of the #pragma token.
18743 */
18744
18745 #define OMP_SECTIONS_CLAUSE_MASK \
18746 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
18747 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
18748 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
18749 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
18750 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
18751
18752 static tree
18753 c_parser_omp_sections (location_t loc, c_parser *parser,
18754 char *p_name, omp_clause_mask mask, tree *cclauses)
18755 {
18756 tree block, clauses, ret;
18757
18758 strcat (p_name, " sections");
18759 mask |= OMP_SECTIONS_CLAUSE_MASK;
18760 if (cclauses)
18761 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
18762
18763 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
18764 if (cclauses)
18765 {
18766 omp_split_clauses (loc, OMP_SECTIONS, mask, clauses, cclauses);
18767 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SECTIONS];
18768 }
18769
18770 block = c_begin_compound_stmt (true);
18771 ret = c_parser_omp_sections_scope (loc, parser);
18772 if (ret)
18773 OMP_SECTIONS_CLAUSES (ret) = clauses;
18774 block = c_end_compound_stmt (loc, block, true);
18775 add_stmt (block);
18776
18777 return ret;
18778 }
18779
18780 /* OpenMP 2.5:
18781 # pragma omp parallel parallel-clause[optseq] new-line
18782 structured-block
18783 # pragma omp parallel for parallel-for-clause[optseq] new-line
18784 structured-block
18785 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
18786 structured-block
18787
18788 OpenMP 4.0:
18789 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
18790 structured-block
18791
18792 LOC is the location of the #pragma token.
18793 */
18794
18795 #define OMP_PARALLEL_CLAUSE_MASK \
18796 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
18797 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
18798 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
18799 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
18800 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
18801 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
18802 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
18803 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
18804 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
18805
18806 static tree
18807 c_parser_omp_parallel (location_t loc, c_parser *parser,
18808 char *p_name, omp_clause_mask mask, tree *cclauses,
18809 bool *if_p)
18810 {
18811 tree stmt, clauses, block;
18812
18813 strcat (p_name, " parallel");
18814 mask |= OMP_PARALLEL_CLAUSE_MASK;
18815 /* #pragma omp target parallel{, for, for simd} disallow copyin clause. */
18816 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) != 0
18817 && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) == 0)
18818 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN);
18819
18820 if (c_parser_next_token_is_keyword (parser, RID_FOR))
18821 {
18822 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
18823 if (cclauses == NULL)
18824 cclauses = cclauses_buf;
18825
18826 c_parser_consume_token (parser);
18827 if (!flag_openmp) /* flag_openmp_simd */
18828 return c_parser_omp_for (loc, parser, p_name, mask, cclauses, if_p);
18829 block = c_begin_omp_parallel ();
18830 tree ret = c_parser_omp_for (loc, parser, p_name, mask, cclauses, if_p);
18831 stmt
18832 = c_finish_omp_parallel (loc, cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
18833 block);
18834 if (ret == NULL_TREE)
18835 return ret;
18836 OMP_PARALLEL_COMBINED (stmt) = 1;
18837 return stmt;
18838 }
18839 /* When combined with distribute, parallel has to be followed by for.
18840 #pragma omp target parallel is allowed though. */
18841 else if (cclauses
18842 && (mask & (OMP_CLAUSE_MASK_1
18843 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
18844 {
18845 error_at (loc, "expected %<for%> after %qs", p_name);
18846 c_parser_skip_to_pragma_eol (parser);
18847 return NULL_TREE;
18848 }
18849 else if (c_parser_next_token_is (parser, CPP_NAME))
18850 {
18851 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
18852 if (cclauses == NULL && strcmp (p, "master") == 0)
18853 {
18854 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
18855 cclauses = cclauses_buf;
18856
18857 c_parser_consume_token (parser);
18858 if (!flag_openmp) /* flag_openmp_simd */
18859 return c_parser_omp_master (loc, parser, p_name, mask, cclauses,
18860 if_p);
18861 block = c_begin_omp_parallel ();
18862 tree ret = c_parser_omp_master (loc, parser, p_name, mask, cclauses,
18863 if_p);
18864 stmt = c_finish_omp_parallel (loc,
18865 cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
18866 block);
18867 OMP_PARALLEL_COMBINED (stmt) = 1;
18868 if (ret == NULL)
18869 return ret;
18870 return stmt;
18871 }
18872 else if (strcmp (p, "loop") == 0)
18873 {
18874 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
18875 if (cclauses == NULL)
18876 cclauses = cclauses_buf;
18877
18878 c_parser_consume_token (parser);
18879 if (!flag_openmp) /* flag_openmp_simd */
18880 return c_parser_omp_loop (loc, parser, p_name, mask, cclauses,
18881 if_p);
18882 block = c_begin_omp_parallel ();
18883 tree ret = c_parser_omp_loop (loc, parser, p_name, mask, cclauses,
18884 if_p);
18885 stmt
18886 = c_finish_omp_parallel (loc,
18887 cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
18888 block);
18889 if (ret == NULL_TREE)
18890 return ret;
18891 OMP_PARALLEL_COMBINED (stmt) = 1;
18892 return stmt;
18893 }
18894 else if (!flag_openmp) /* flag_openmp_simd */
18895 {
18896 c_parser_skip_to_pragma_eol (parser, false);
18897 return NULL_TREE;
18898 }
18899 else if (cclauses == NULL && strcmp (p, "sections") == 0)
18900 {
18901 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
18902 cclauses = cclauses_buf;
18903
18904 c_parser_consume_token (parser);
18905 block = c_begin_omp_parallel ();
18906 c_parser_omp_sections (loc, parser, p_name, mask, cclauses);
18907 stmt = c_finish_omp_parallel (loc,
18908 cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
18909 block);
18910 OMP_PARALLEL_COMBINED (stmt) = 1;
18911 return stmt;
18912 }
18913 }
18914 else if (!flag_openmp) /* flag_openmp_simd */
18915 {
18916 c_parser_skip_to_pragma_eol (parser, false);
18917 return NULL_TREE;
18918 }
18919
18920 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
18921 if (cclauses)
18922 {
18923 omp_split_clauses (loc, OMP_PARALLEL, mask, clauses, cclauses);
18924 clauses = cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL];
18925 }
18926
18927 block = c_begin_omp_parallel ();
18928 c_parser_statement (parser, if_p);
18929 stmt = c_finish_omp_parallel (loc, clauses, block);
18930
18931 return stmt;
18932 }
18933
18934 /* OpenMP 2.5:
18935 # pragma omp single single-clause[optseq] new-line
18936 structured-block
18937
18938 LOC is the location of the #pragma.
18939 */
18940
18941 #define OMP_SINGLE_CLAUSE_MASK \
18942 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
18943 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
18944 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
18945 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
18946
18947 static tree
18948 c_parser_omp_single (location_t loc, c_parser *parser, bool *if_p)
18949 {
18950 tree stmt = make_node (OMP_SINGLE);
18951 SET_EXPR_LOCATION (stmt, loc);
18952 TREE_TYPE (stmt) = void_type_node;
18953
18954 OMP_SINGLE_CLAUSES (stmt)
18955 = c_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
18956 "#pragma omp single");
18957 OMP_SINGLE_BODY (stmt) = c_parser_omp_structured_block (parser, if_p);
18958
18959 return add_stmt (stmt);
18960 }
18961
18962 /* OpenMP 3.0:
18963 # pragma omp task task-clause[optseq] new-line
18964
18965 LOC is the location of the #pragma.
18966 */
18967
18968 #define OMP_TASK_CLAUSE_MASK \
18969 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
18970 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
18971 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
18972 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
18973 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
18974 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
18975 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
18976 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
18977 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
18978 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY) \
18979 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IN_REDUCTION))
18980
18981 static tree
18982 c_parser_omp_task (location_t loc, c_parser *parser, bool *if_p)
18983 {
18984 tree clauses, block;
18985
18986 clauses = c_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
18987 "#pragma omp task");
18988
18989 block = c_begin_omp_task ();
18990 c_parser_statement (parser, if_p);
18991 return c_finish_omp_task (loc, clauses, block);
18992 }
18993
18994 /* OpenMP 3.0:
18995 # pragma omp taskwait new-line
18996
18997 OpenMP 5.0:
18998 # pragma omp taskwait taskwait-clause[optseq] new-line
18999 */
19000
19001 #define OMP_TASKWAIT_CLAUSE_MASK \
19002 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
19003
19004 static void
19005 c_parser_omp_taskwait (c_parser *parser)
19006 {
19007 location_t loc = c_parser_peek_token (parser)->location;
19008 c_parser_consume_pragma (parser);
19009
19010 tree clauses
19011 = c_parser_omp_all_clauses (parser, OMP_TASKWAIT_CLAUSE_MASK,
19012 "#pragma omp taskwait");
19013
19014 if (clauses)
19015 {
19016 tree stmt = make_node (OMP_TASK);
19017 TREE_TYPE (stmt) = void_node;
19018 OMP_TASK_CLAUSES (stmt) = clauses;
19019 OMP_TASK_BODY (stmt) = NULL_TREE;
19020 SET_EXPR_LOCATION (stmt, loc);
19021 add_stmt (stmt);
19022 }
19023 else
19024 c_finish_omp_taskwait (loc);
19025 }
19026
19027 /* OpenMP 3.1:
19028 # pragma omp taskyield new-line
19029 */
19030
19031 static void
19032 c_parser_omp_taskyield (c_parser *parser)
19033 {
19034 location_t loc = c_parser_peek_token (parser)->location;
19035 c_parser_consume_pragma (parser);
19036 c_parser_skip_to_pragma_eol (parser);
19037
19038 c_finish_omp_taskyield (loc);
19039 }
19040
19041 /* OpenMP 4.0:
19042 # pragma omp taskgroup new-line
19043
19044 OpenMP 5.0:
19045 # pragma omp taskgroup taskgroup-clause[optseq] new-line
19046 */
19047
19048 #define OMP_TASKGROUP_CLAUSE_MASK \
19049 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASK_REDUCTION))
19050
19051 static tree
19052 c_parser_omp_taskgroup (location_t loc, c_parser *parser, bool *if_p)
19053 {
19054 tree clauses = c_parser_omp_all_clauses (parser, OMP_TASKGROUP_CLAUSE_MASK,
19055 "#pragma omp taskgroup");
19056
19057 tree body = c_parser_omp_structured_block (parser, if_p);
19058 return c_finish_omp_taskgroup (loc, body, clauses);
19059 }
19060
19061 /* OpenMP 4.0:
19062 # pragma omp cancel cancel-clause[optseq] new-line
19063
19064 LOC is the location of the #pragma.
19065 */
19066
19067 #define OMP_CANCEL_CLAUSE_MASK \
19068 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
19069 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
19070 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
19071 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
19072 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
19073
19074 static void
19075 c_parser_omp_cancel (c_parser *parser)
19076 {
19077 location_t loc = c_parser_peek_token (parser)->location;
19078
19079 c_parser_consume_pragma (parser);
19080 tree clauses = c_parser_omp_all_clauses (parser, OMP_CANCEL_CLAUSE_MASK,
19081 "#pragma omp cancel");
19082
19083 c_finish_omp_cancel (loc, clauses);
19084 }
19085
19086 /* OpenMP 4.0:
19087 # pragma omp cancellation point cancelpt-clause[optseq] new-line
19088
19089 LOC is the location of the #pragma.
19090 */
19091
19092 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
19093 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
19094 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
19095 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
19096 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
19097
19098 static void
19099 c_parser_omp_cancellation_point (c_parser *parser, enum pragma_context context)
19100 {
19101 location_t loc = c_parser_peek_token (parser)->location;
19102 tree clauses;
19103 bool point_seen = false;
19104
19105 c_parser_consume_pragma (parser);
19106 if (c_parser_next_token_is (parser, CPP_NAME))
19107 {
19108 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
19109 if (strcmp (p, "point") == 0)
19110 {
19111 c_parser_consume_token (parser);
19112 point_seen = true;
19113 }
19114 }
19115 if (!point_seen)
19116 {
19117 c_parser_error (parser, "expected %<point%>");
19118 c_parser_skip_to_pragma_eol (parser);
19119 return;
19120 }
19121
19122 if (context != pragma_compound)
19123 {
19124 if (context == pragma_stmt)
19125 error_at (loc,
19126 "%<#pragma %s%> may only be used in compound statements",
19127 "omp cancellation point");
19128 else
19129 c_parser_error (parser, "expected declaration specifiers");
19130 c_parser_skip_to_pragma_eol (parser, false);
19131 return;
19132 }
19133
19134 clauses
19135 = c_parser_omp_all_clauses (parser, OMP_CANCELLATION_POINT_CLAUSE_MASK,
19136 "#pragma omp cancellation point");
19137
19138 c_finish_omp_cancellation_point (loc, clauses);
19139 }
19140
19141 /* OpenMP 4.0:
19142 #pragma omp distribute distribute-clause[optseq] new-line
19143 for-loop */
19144
19145 #define OMP_DISTRIBUTE_CLAUSE_MASK \
19146 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
19147 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
19148 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
19149 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
19150 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
19151
19152 static tree
19153 c_parser_omp_distribute (location_t loc, c_parser *parser,
19154 char *p_name, omp_clause_mask mask, tree *cclauses,
19155 bool *if_p)
19156 {
19157 tree clauses, block, ret;
19158
19159 strcat (p_name, " distribute");
19160 mask |= OMP_DISTRIBUTE_CLAUSE_MASK;
19161
19162 if (c_parser_next_token_is (parser, CPP_NAME))
19163 {
19164 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
19165 bool simd = false;
19166 bool parallel = false;
19167
19168 if (strcmp (p, "simd") == 0)
19169 simd = true;
19170 else
19171 parallel = strcmp (p, "parallel") == 0;
19172 if (parallel || simd)
19173 {
19174 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
19175 if (cclauses == NULL)
19176 cclauses = cclauses_buf;
19177 c_parser_consume_token (parser);
19178 if (!flag_openmp) /* flag_openmp_simd */
19179 {
19180 if (simd)
19181 return c_parser_omp_simd (loc, parser, p_name, mask, cclauses,
19182 if_p);
19183 else
19184 return c_parser_omp_parallel (loc, parser, p_name, mask,
19185 cclauses, if_p);
19186 }
19187 block = c_begin_compound_stmt (true);
19188 if (simd)
19189 ret = c_parser_omp_simd (loc, parser, p_name, mask, cclauses,
19190 if_p);
19191 else
19192 ret = c_parser_omp_parallel (loc, parser, p_name, mask, cclauses,
19193 if_p);
19194 block = c_end_compound_stmt (loc, block, true);
19195 if (ret == NULL)
19196 return ret;
19197 ret = make_node (OMP_DISTRIBUTE);
19198 TREE_TYPE (ret) = void_type_node;
19199 OMP_FOR_BODY (ret) = block;
19200 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
19201 SET_EXPR_LOCATION (ret, loc);
19202 add_stmt (ret);
19203 return ret;
19204 }
19205 }
19206 if (!flag_openmp) /* flag_openmp_simd */
19207 {
19208 c_parser_skip_to_pragma_eol (parser, false);
19209 return NULL_TREE;
19210 }
19211
19212 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
19213 if (cclauses)
19214 {
19215 omp_split_clauses (loc, OMP_DISTRIBUTE, mask, clauses, cclauses);
19216 clauses = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
19217 }
19218
19219 block = c_begin_compound_stmt (true);
19220 ret = c_parser_omp_for_loop (loc, parser, OMP_DISTRIBUTE, clauses, NULL,
19221 if_p);
19222 block = c_end_compound_stmt (loc, block, true);
19223 add_stmt (block);
19224
19225 return ret;
19226 }
19227
19228 /* OpenMP 4.0:
19229 # pragma omp teams teams-clause[optseq] new-line
19230 structured-block */
19231
19232 #define OMP_TEAMS_CLAUSE_MASK \
19233 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
19234 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
19235 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
19236 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
19237 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
19238 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
19239 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
19240
19241 static tree
19242 c_parser_omp_teams (location_t loc, c_parser *parser,
19243 char *p_name, omp_clause_mask mask, tree *cclauses,
19244 bool *if_p)
19245 {
19246 tree clauses, block, ret;
19247
19248 strcat (p_name, " teams");
19249 mask |= OMP_TEAMS_CLAUSE_MASK;
19250
19251 if (c_parser_next_token_is (parser, CPP_NAME))
19252 {
19253 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
19254 if (strcmp (p, "distribute") == 0)
19255 {
19256 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
19257 if (cclauses == NULL)
19258 cclauses = cclauses_buf;
19259
19260 c_parser_consume_token (parser);
19261 if (!flag_openmp) /* flag_openmp_simd */
19262 return c_parser_omp_distribute (loc, parser, p_name, mask,
19263 cclauses, if_p);
19264 block = c_begin_omp_parallel ();
19265 ret = c_parser_omp_distribute (loc, parser, p_name, mask, cclauses,
19266 if_p);
19267 block = c_end_compound_stmt (loc, block, true);
19268 if (ret == NULL)
19269 return ret;
19270 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
19271 ret = make_node (OMP_TEAMS);
19272 TREE_TYPE (ret) = void_type_node;
19273 OMP_TEAMS_CLAUSES (ret) = clauses;
19274 OMP_TEAMS_BODY (ret) = block;
19275 OMP_TEAMS_COMBINED (ret) = 1;
19276 SET_EXPR_LOCATION (ret, loc);
19277 return add_stmt (ret);
19278 }
19279 else if (strcmp (p, "loop") == 0)
19280 {
19281 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
19282 if (cclauses == NULL)
19283 cclauses = cclauses_buf;
19284
19285 c_parser_consume_token (parser);
19286 if (!flag_openmp) /* flag_openmp_simd */
19287 return c_parser_omp_loop (loc, parser, p_name, mask, cclauses,
19288 if_p);
19289 block = c_begin_omp_parallel ();
19290 ret = c_parser_omp_loop (loc, parser, p_name, mask, cclauses, if_p);
19291 block = c_end_compound_stmt (loc, block, true);
19292 if (ret == NULL)
19293 return ret;
19294 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
19295 ret = make_node (OMP_TEAMS);
19296 TREE_TYPE (ret) = void_type_node;
19297 OMP_TEAMS_CLAUSES (ret) = clauses;
19298 OMP_TEAMS_BODY (ret) = block;
19299 OMP_TEAMS_COMBINED (ret) = 1;
19300 SET_EXPR_LOCATION (ret, loc);
19301 return add_stmt (ret);
19302 }
19303 }
19304 if (!flag_openmp) /* flag_openmp_simd */
19305 {
19306 c_parser_skip_to_pragma_eol (parser, false);
19307 return NULL_TREE;
19308 }
19309
19310 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
19311 if (cclauses)
19312 {
19313 omp_split_clauses (loc, OMP_TEAMS, mask, clauses, cclauses);
19314 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
19315 }
19316
19317 tree stmt = make_node (OMP_TEAMS);
19318 TREE_TYPE (stmt) = void_type_node;
19319 OMP_TEAMS_CLAUSES (stmt) = clauses;
19320 block = c_begin_omp_parallel ();
19321 add_stmt (c_parser_omp_structured_block (parser, if_p));
19322 OMP_TEAMS_BODY (stmt) = c_end_compound_stmt (loc, block, true);
19323 SET_EXPR_LOCATION (stmt, loc);
19324
19325 return add_stmt (stmt);
19326 }
19327
19328 /* OpenMP 4.0:
19329 # pragma omp target data target-data-clause[optseq] new-line
19330 structured-block */
19331
19332 #define OMP_TARGET_DATA_CLAUSE_MASK \
19333 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
19334 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
19335 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
19336 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR) \
19337 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_ADDR))
19338
19339 static tree
19340 c_parser_omp_target_data (location_t loc, c_parser *parser, bool *if_p)
19341 {
19342 tree clauses
19343 = c_parser_omp_all_clauses (parser, OMP_TARGET_DATA_CLAUSE_MASK,
19344 "#pragma omp target data");
19345 int map_seen = 0;
19346 for (tree *pc = &clauses; *pc;)
19347 {
19348 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
19349 switch (OMP_CLAUSE_MAP_KIND (*pc))
19350 {
19351 case GOMP_MAP_TO:
19352 case GOMP_MAP_ALWAYS_TO:
19353 case GOMP_MAP_FROM:
19354 case GOMP_MAP_ALWAYS_FROM:
19355 case GOMP_MAP_TOFROM:
19356 case GOMP_MAP_ALWAYS_TOFROM:
19357 case GOMP_MAP_ALLOC:
19358 map_seen = 3;
19359 break;
19360 case GOMP_MAP_FIRSTPRIVATE_POINTER:
19361 case GOMP_MAP_ALWAYS_POINTER:
19362 break;
19363 default:
19364 map_seen |= 1;
19365 error_at (OMP_CLAUSE_LOCATION (*pc),
19366 "%<#pragma omp target data%> with map-type other "
19367 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
19368 "on %<map%> clause");
19369 *pc = OMP_CLAUSE_CHAIN (*pc);
19370 continue;
19371 }
19372 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_USE_DEVICE_PTR
19373 || OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_USE_DEVICE_ADDR)
19374 map_seen = 3;
19375 pc = &OMP_CLAUSE_CHAIN (*pc);
19376 }
19377
19378 if (map_seen != 3)
19379 {
19380 if (map_seen == 0)
19381 error_at (loc,
19382 "%<#pragma omp target data%> must contain at least "
19383 "one %<map%>, %<use_device_ptr%> or %<use_device_addr%> "
19384 "clause");
19385 return NULL_TREE;
19386 }
19387
19388 tree stmt = make_node (OMP_TARGET_DATA);
19389 TREE_TYPE (stmt) = void_type_node;
19390 OMP_TARGET_DATA_CLAUSES (stmt) = clauses;
19391 keep_next_level ();
19392 tree block = c_begin_compound_stmt (true);
19393 add_stmt (c_parser_omp_structured_block (parser, if_p));
19394 OMP_TARGET_DATA_BODY (stmt) = c_end_compound_stmt (loc, block, true);
19395
19396 SET_EXPR_LOCATION (stmt, loc);
19397 return add_stmt (stmt);
19398 }
19399
19400 /* OpenMP 4.0:
19401 # pragma omp target update target-update-clause[optseq] new-line */
19402
19403 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
19404 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
19405 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
19406 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
19407 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
19408 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
19409 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
19410
19411 static bool
19412 c_parser_omp_target_update (location_t loc, c_parser *parser,
19413 enum pragma_context context)
19414 {
19415 if (context == pragma_stmt)
19416 {
19417 error_at (loc, "%<#pragma %s%> may only be used in compound statements",
19418 "omp target update");
19419 c_parser_skip_to_pragma_eol (parser, false);
19420 return false;
19421 }
19422
19423 tree clauses
19424 = c_parser_omp_all_clauses (parser, OMP_TARGET_UPDATE_CLAUSE_MASK,
19425 "#pragma omp target update");
19426 if (omp_find_clause (clauses, OMP_CLAUSE_TO) == NULL_TREE
19427 && omp_find_clause (clauses, OMP_CLAUSE_FROM) == NULL_TREE)
19428 {
19429 error_at (loc,
19430 "%<#pragma omp target update%> must contain at least one "
19431 "%<from%> or %<to%> clauses");
19432 return false;
19433 }
19434
19435 tree stmt = make_node (OMP_TARGET_UPDATE);
19436 TREE_TYPE (stmt) = void_type_node;
19437 OMP_TARGET_UPDATE_CLAUSES (stmt) = clauses;
19438 SET_EXPR_LOCATION (stmt, loc);
19439 add_stmt (stmt);
19440 return false;
19441 }
19442
19443 /* OpenMP 4.5:
19444 # pragma omp target enter data target-data-clause[optseq] new-line */
19445
19446 #define OMP_TARGET_ENTER_DATA_CLAUSE_MASK \
19447 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
19448 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
19449 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
19450 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
19451 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
19452
19453 static tree
19454 c_parser_omp_target_enter_data (location_t loc, c_parser *parser,
19455 enum pragma_context context)
19456 {
19457 bool data_seen = false;
19458 if (c_parser_next_token_is (parser, CPP_NAME))
19459 {
19460 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
19461 if (strcmp (p, "data") == 0)
19462 {
19463 c_parser_consume_token (parser);
19464 data_seen = true;
19465 }
19466 }
19467 if (!data_seen)
19468 {
19469 c_parser_error (parser, "expected %<data%>");
19470 c_parser_skip_to_pragma_eol (parser);
19471 return NULL_TREE;
19472 }
19473
19474 if (context == pragma_stmt)
19475 {
19476 error_at (loc, "%<#pragma %s%> may only be used in compound statements",
19477 "omp target enter data");
19478 c_parser_skip_to_pragma_eol (parser, false);
19479 return NULL_TREE;
19480 }
19481
19482 tree clauses
19483 = c_parser_omp_all_clauses (parser, OMP_TARGET_ENTER_DATA_CLAUSE_MASK,
19484 "#pragma omp target enter data");
19485 int map_seen = 0;
19486 for (tree *pc = &clauses; *pc;)
19487 {
19488 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
19489 switch (OMP_CLAUSE_MAP_KIND (*pc))
19490 {
19491 case GOMP_MAP_TO:
19492 case GOMP_MAP_ALWAYS_TO:
19493 case GOMP_MAP_ALLOC:
19494 map_seen = 3;
19495 break;
19496 case GOMP_MAP_FIRSTPRIVATE_POINTER:
19497 case GOMP_MAP_ALWAYS_POINTER:
19498 break;
19499 default:
19500 map_seen |= 1;
19501 error_at (OMP_CLAUSE_LOCATION (*pc),
19502 "%<#pragma omp target enter data%> with map-type other "
19503 "than %<to%> or %<alloc%> on %<map%> clause");
19504 *pc = OMP_CLAUSE_CHAIN (*pc);
19505 continue;
19506 }
19507 pc = &OMP_CLAUSE_CHAIN (*pc);
19508 }
19509
19510 if (map_seen != 3)
19511 {
19512 if (map_seen == 0)
19513 error_at (loc,
19514 "%<#pragma omp target enter data%> must contain at least "
19515 "one %<map%> clause");
19516 return NULL_TREE;
19517 }
19518
19519 tree stmt = make_node (OMP_TARGET_ENTER_DATA);
19520 TREE_TYPE (stmt) = void_type_node;
19521 OMP_TARGET_ENTER_DATA_CLAUSES (stmt) = clauses;
19522 SET_EXPR_LOCATION (stmt, loc);
19523 add_stmt (stmt);
19524 return stmt;
19525 }
19526
19527 /* OpenMP 4.5:
19528 # pragma omp target exit data target-data-clause[optseq] new-line */
19529
19530 #define OMP_TARGET_EXIT_DATA_CLAUSE_MASK \
19531 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
19532 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
19533 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
19534 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
19535 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
19536
19537 static tree
19538 c_parser_omp_target_exit_data (location_t loc, c_parser *parser,
19539 enum pragma_context context)
19540 {
19541 bool data_seen = false;
19542 if (c_parser_next_token_is (parser, CPP_NAME))
19543 {
19544 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
19545 if (strcmp (p, "data") == 0)
19546 {
19547 c_parser_consume_token (parser);
19548 data_seen = true;
19549 }
19550 }
19551 if (!data_seen)
19552 {
19553 c_parser_error (parser, "expected %<data%>");
19554 c_parser_skip_to_pragma_eol (parser);
19555 return NULL_TREE;
19556 }
19557
19558 if (context == pragma_stmt)
19559 {
19560 error_at (loc, "%<#pragma %s%> may only be used in compound statements",
19561 "omp target exit data");
19562 c_parser_skip_to_pragma_eol (parser, false);
19563 return NULL_TREE;
19564 }
19565
19566 tree clauses
19567 = c_parser_omp_all_clauses (parser, OMP_TARGET_EXIT_DATA_CLAUSE_MASK,
19568 "#pragma omp target exit data");
19569
19570 int map_seen = 0;
19571 for (tree *pc = &clauses; *pc;)
19572 {
19573 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
19574 switch (OMP_CLAUSE_MAP_KIND (*pc))
19575 {
19576 case GOMP_MAP_FROM:
19577 case GOMP_MAP_ALWAYS_FROM:
19578 case GOMP_MAP_RELEASE:
19579 case GOMP_MAP_DELETE:
19580 map_seen = 3;
19581 break;
19582 case GOMP_MAP_FIRSTPRIVATE_POINTER:
19583 case GOMP_MAP_ALWAYS_POINTER:
19584 break;
19585 default:
19586 map_seen |= 1;
19587 error_at (OMP_CLAUSE_LOCATION (*pc),
19588 "%<#pragma omp target exit data%> with map-type other "
19589 "than %<from%>, %<release%> or %<delete%> on %<map%>"
19590 " clause");
19591 *pc = OMP_CLAUSE_CHAIN (*pc);
19592 continue;
19593 }
19594 pc = &OMP_CLAUSE_CHAIN (*pc);
19595 }
19596
19597 if (map_seen != 3)
19598 {
19599 if (map_seen == 0)
19600 error_at (loc,
19601 "%<#pragma omp target exit data%> must contain at least one "
19602 "%<map%> clause");
19603 return NULL_TREE;
19604 }
19605
19606 tree stmt = make_node (OMP_TARGET_EXIT_DATA);
19607 TREE_TYPE (stmt) = void_type_node;
19608 OMP_TARGET_EXIT_DATA_CLAUSES (stmt) = clauses;
19609 SET_EXPR_LOCATION (stmt, loc);
19610 add_stmt (stmt);
19611 return stmt;
19612 }
19613
19614 /* OpenMP 4.0:
19615 # pragma omp target target-clause[optseq] new-line
19616 structured-block */
19617
19618 #define OMP_TARGET_CLAUSE_MASK \
19619 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
19620 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
19621 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
19622 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
19623 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
19624 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
19625 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
19626 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULTMAP) \
19627 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR))
19628
19629 static bool
19630 c_parser_omp_target (c_parser *parser, enum pragma_context context, bool *if_p)
19631 {
19632 location_t loc = c_parser_peek_token (parser)->location;
19633 c_parser_consume_pragma (parser);
19634 tree *pc = NULL, stmt, block;
19635
19636 if (context != pragma_stmt && context != pragma_compound)
19637 {
19638 c_parser_error (parser, "expected declaration specifiers");
19639 c_parser_skip_to_pragma_eol (parser);
19640 return false;
19641 }
19642
19643 if (flag_openmp)
19644 omp_requires_mask
19645 = (enum omp_requires) (omp_requires_mask | OMP_REQUIRES_TARGET_USED);
19646
19647 if (c_parser_next_token_is (parser, CPP_NAME))
19648 {
19649 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
19650 enum tree_code ccode = ERROR_MARK;
19651
19652 if (strcmp (p, "teams") == 0)
19653 ccode = OMP_TEAMS;
19654 else if (strcmp (p, "parallel") == 0)
19655 ccode = OMP_PARALLEL;
19656 else if (strcmp (p, "simd") == 0)
19657 ccode = OMP_SIMD;
19658 if (ccode != ERROR_MARK)
19659 {
19660 tree cclauses[C_OMP_CLAUSE_SPLIT_COUNT];
19661 char p_name[sizeof ("#pragma omp target teams distribute "
19662 "parallel for simd")];
19663
19664 c_parser_consume_token (parser);
19665 strcpy (p_name, "#pragma omp target");
19666 if (!flag_openmp) /* flag_openmp_simd */
19667 {
19668 tree stmt;
19669 switch (ccode)
19670 {
19671 case OMP_TEAMS:
19672 stmt = c_parser_omp_teams (loc, parser, p_name,
19673 OMP_TARGET_CLAUSE_MASK,
19674 cclauses, if_p);
19675 break;
19676 case OMP_PARALLEL:
19677 stmt = c_parser_omp_parallel (loc, parser, p_name,
19678 OMP_TARGET_CLAUSE_MASK,
19679 cclauses, if_p);
19680 break;
19681 case OMP_SIMD:
19682 stmt = c_parser_omp_simd (loc, parser, p_name,
19683 OMP_TARGET_CLAUSE_MASK,
19684 cclauses, if_p);
19685 break;
19686 default:
19687 gcc_unreachable ();
19688 }
19689 return stmt != NULL_TREE;
19690 }
19691 keep_next_level ();
19692 tree block = c_begin_compound_stmt (true), ret;
19693 switch (ccode)
19694 {
19695 case OMP_TEAMS:
19696 ret = c_parser_omp_teams (loc, parser, p_name,
19697 OMP_TARGET_CLAUSE_MASK, cclauses,
19698 if_p);
19699 break;
19700 case OMP_PARALLEL:
19701 ret = c_parser_omp_parallel (loc, parser, p_name,
19702 OMP_TARGET_CLAUSE_MASK, cclauses,
19703 if_p);
19704 break;
19705 case OMP_SIMD:
19706 ret = c_parser_omp_simd (loc, parser, p_name,
19707 OMP_TARGET_CLAUSE_MASK, cclauses,
19708 if_p);
19709 break;
19710 default:
19711 gcc_unreachable ();
19712 }
19713 block = c_end_compound_stmt (loc, block, true);
19714 if (ret == NULL_TREE)
19715 return false;
19716 if (ccode == OMP_TEAMS)
19717 {
19718 /* For combined target teams, ensure the num_teams and
19719 thread_limit clause expressions are evaluated on the host,
19720 before entering the target construct. */
19721 tree c;
19722 for (c = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
19723 c; c = OMP_CLAUSE_CHAIN (c))
19724 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
19725 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
19726 && TREE_CODE (OMP_CLAUSE_OPERAND (c, 0)) != INTEGER_CST)
19727 {
19728 tree expr = OMP_CLAUSE_OPERAND (c, 0);
19729 tree tmp = create_tmp_var_raw (TREE_TYPE (expr));
19730 expr = build4 (TARGET_EXPR, TREE_TYPE (expr), tmp,
19731 expr, NULL_TREE, NULL_TREE);
19732 add_stmt (expr);
19733 OMP_CLAUSE_OPERAND (c, 0) = expr;
19734 tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
19735 OMP_CLAUSE_FIRSTPRIVATE);
19736 OMP_CLAUSE_DECL (tc) = tmp;
19737 OMP_CLAUSE_CHAIN (tc)
19738 = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
19739 cclauses[C_OMP_CLAUSE_SPLIT_TARGET] = tc;
19740 }
19741 }
19742 tree stmt = make_node (OMP_TARGET);
19743 TREE_TYPE (stmt) = void_type_node;
19744 OMP_TARGET_CLAUSES (stmt) = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
19745 OMP_TARGET_BODY (stmt) = block;
19746 OMP_TARGET_COMBINED (stmt) = 1;
19747 SET_EXPR_LOCATION (stmt, loc);
19748 add_stmt (stmt);
19749 pc = &OMP_TARGET_CLAUSES (stmt);
19750 goto check_clauses;
19751 }
19752 else if (!flag_openmp) /* flag_openmp_simd */
19753 {
19754 c_parser_skip_to_pragma_eol (parser, false);
19755 return false;
19756 }
19757 else if (strcmp (p, "data") == 0)
19758 {
19759 c_parser_consume_token (parser);
19760 c_parser_omp_target_data (loc, parser, if_p);
19761 return true;
19762 }
19763 else if (strcmp (p, "enter") == 0)
19764 {
19765 c_parser_consume_token (parser);
19766 c_parser_omp_target_enter_data (loc, parser, context);
19767 return false;
19768 }
19769 else if (strcmp (p, "exit") == 0)
19770 {
19771 c_parser_consume_token (parser);
19772 c_parser_omp_target_exit_data (loc, parser, context);
19773 return false;
19774 }
19775 else if (strcmp (p, "update") == 0)
19776 {
19777 c_parser_consume_token (parser);
19778 return c_parser_omp_target_update (loc, parser, context);
19779 }
19780 }
19781 if (!flag_openmp) /* flag_openmp_simd */
19782 {
19783 c_parser_skip_to_pragma_eol (parser, false);
19784 return false;
19785 }
19786
19787 stmt = make_node (OMP_TARGET);
19788 TREE_TYPE (stmt) = void_type_node;
19789
19790 OMP_TARGET_CLAUSES (stmt)
19791 = c_parser_omp_all_clauses (parser, OMP_TARGET_CLAUSE_MASK,
19792 "#pragma omp target");
19793 pc = &OMP_TARGET_CLAUSES (stmt);
19794 keep_next_level ();
19795 block = c_begin_compound_stmt (true);
19796 add_stmt (c_parser_omp_structured_block (parser, if_p));
19797 OMP_TARGET_BODY (stmt) = c_end_compound_stmt (loc, block, true);
19798
19799 SET_EXPR_LOCATION (stmt, loc);
19800 add_stmt (stmt);
19801
19802 check_clauses:
19803 while (*pc)
19804 {
19805 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
19806 switch (OMP_CLAUSE_MAP_KIND (*pc))
19807 {
19808 case GOMP_MAP_TO:
19809 case GOMP_MAP_ALWAYS_TO:
19810 case GOMP_MAP_FROM:
19811 case GOMP_MAP_ALWAYS_FROM:
19812 case GOMP_MAP_TOFROM:
19813 case GOMP_MAP_ALWAYS_TOFROM:
19814 case GOMP_MAP_ALLOC:
19815 case GOMP_MAP_FIRSTPRIVATE_POINTER:
19816 case GOMP_MAP_ALWAYS_POINTER:
19817 break;
19818 default:
19819 error_at (OMP_CLAUSE_LOCATION (*pc),
19820 "%<#pragma omp target%> with map-type other "
19821 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
19822 "on %<map%> clause");
19823 *pc = OMP_CLAUSE_CHAIN (*pc);
19824 continue;
19825 }
19826 pc = &OMP_CLAUSE_CHAIN (*pc);
19827 }
19828 return true;
19829 }
19830
19831 /* OpenMP 4.0:
19832 # pragma omp declare simd declare-simd-clauses[optseq] new-line
19833
19834 OpenMP 5.0:
19835 # pragma omp declare variant (identifier) match(context-selector) new-line
19836 */
19837
19838 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
19839 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
19840 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
19841 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
19842 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
19843 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
19844 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
19845
19846 static void
19847 c_parser_omp_declare_simd (c_parser *parser, enum pragma_context context)
19848 {
19849 c_token *token = c_parser_peek_token (parser);
19850 gcc_assert (token->type == CPP_NAME);
19851 tree kind = token->value;
19852 gcc_assert (strcmp (IDENTIFIER_POINTER (kind), "simd") == 0
19853 || strcmp (IDENTIFIER_POINTER (kind), "variant") == 0);
19854
19855 auto_vec<c_token> clauses;
19856 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
19857 {
19858 c_token *token = c_parser_peek_token (parser);
19859 if (token->type == CPP_EOF)
19860 {
19861 c_parser_skip_to_pragma_eol (parser);
19862 return;
19863 }
19864 clauses.safe_push (*token);
19865 c_parser_consume_token (parser);
19866 }
19867 clauses.safe_push (*c_parser_peek_token (parser));
19868 c_parser_skip_to_pragma_eol (parser);
19869
19870 while (c_parser_next_token_is (parser, CPP_PRAGMA))
19871 {
19872 if (c_parser_peek_token (parser)->pragma_kind != PRAGMA_OMP_DECLARE
19873 || c_parser_peek_2nd_token (parser)->type != CPP_NAME
19874 || c_parser_peek_2nd_token (parser)->value != kind)
19875 {
19876 error ("%<#pragma omp declare %s%> must be followed by "
19877 "function declaration or definition or another "
19878 "%<#pragma omp declare %s%>",
19879 IDENTIFIER_POINTER (kind), IDENTIFIER_POINTER (kind));
19880 return;
19881 }
19882 c_parser_consume_pragma (parser);
19883 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
19884 {
19885 c_token *token = c_parser_peek_token (parser);
19886 if (token->type == CPP_EOF)
19887 {
19888 c_parser_skip_to_pragma_eol (parser);
19889 return;
19890 }
19891 clauses.safe_push (*token);
19892 c_parser_consume_token (parser);
19893 }
19894 clauses.safe_push (*c_parser_peek_token (parser));
19895 c_parser_skip_to_pragma_eol (parser);
19896 }
19897
19898 /* Make sure nothing tries to read past the end of the tokens. */
19899 c_token eof_token;
19900 memset (&eof_token, 0, sizeof (eof_token));
19901 eof_token.type = CPP_EOF;
19902 clauses.safe_push (eof_token);
19903 clauses.safe_push (eof_token);
19904
19905 switch (context)
19906 {
19907 case pragma_external:
19908 if (c_parser_next_token_is (parser, CPP_KEYWORD)
19909 && c_parser_peek_token (parser)->keyword == RID_EXTENSION)
19910 {
19911 int ext = disable_extension_diagnostics ();
19912 do
19913 c_parser_consume_token (parser);
19914 while (c_parser_next_token_is (parser, CPP_KEYWORD)
19915 && c_parser_peek_token (parser)->keyword == RID_EXTENSION);
19916 c_parser_declaration_or_fndef (parser, true, true, true, false, true,
19917 NULL, clauses);
19918 restore_extension_diagnostics (ext);
19919 }
19920 else
19921 c_parser_declaration_or_fndef (parser, true, true, true, false, true,
19922 NULL, clauses);
19923 break;
19924 case pragma_struct:
19925 case pragma_param:
19926 case pragma_stmt:
19927 error ("%<#pragma omp declare %s%> must be followed by "
19928 "function declaration or definition",
19929 IDENTIFIER_POINTER (kind));
19930 break;
19931 case pragma_compound:
19932 if (c_parser_next_token_is (parser, CPP_KEYWORD)
19933 && c_parser_peek_token (parser)->keyword == RID_EXTENSION)
19934 {
19935 int ext = disable_extension_diagnostics ();
19936 do
19937 c_parser_consume_token (parser);
19938 while (c_parser_next_token_is (parser, CPP_KEYWORD)
19939 && c_parser_peek_token (parser)->keyword == RID_EXTENSION);
19940 if (c_parser_next_tokens_start_declaration (parser))
19941 {
19942 c_parser_declaration_or_fndef (parser, true, true, true, true,
19943 true, NULL, clauses);
19944 restore_extension_diagnostics (ext);
19945 break;
19946 }
19947 restore_extension_diagnostics (ext);
19948 }
19949 else if (c_parser_next_tokens_start_declaration (parser))
19950 {
19951 c_parser_declaration_or_fndef (parser, true, true, true, true, true,
19952 NULL, clauses);
19953 break;
19954 }
19955 error ("%<#pragma omp declare %s%> must be followed by "
19956 "function declaration or definition",
19957 IDENTIFIER_POINTER (kind));
19958 break;
19959 default:
19960 gcc_unreachable ();
19961 }
19962 }
19963
19964 static const char *const omp_construct_selectors[] = {
19965 "simd", "target", "teams", "parallel", "for", NULL };
19966 static const char *const omp_device_selectors[] = {
19967 "kind", "isa", "arch", NULL };
19968 static const char *const omp_implementation_selectors[] = {
19969 "vendor", "extension", "atomic_default_mem_order", "unified_address",
19970 "unified_shared_memory", "dynamic_allocators", "reverse_offload", NULL };
19971 static const char *const omp_user_selectors[] = {
19972 "condition", NULL };
19973
19974 /* OpenMP 5.0:
19975
19976 trait-selector:
19977 trait-selector-name[([trait-score:]trait-property[,trait-property[,...]])]
19978
19979 trait-score:
19980 score(score-expression) */
19981
19982 static tree
19983 c_parser_omp_context_selector (c_parser *parser, tree set, tree parms)
19984 {
19985 tree ret = NULL_TREE;
19986 do
19987 {
19988 tree selector;
19989 if (c_parser_next_token_is (parser, CPP_KEYWORD)
19990 || c_parser_next_token_is (parser, CPP_NAME))
19991 selector = c_parser_peek_token (parser)->value;
19992 else
19993 {
19994 c_parser_error (parser, "expected trait selector name");
19995 return error_mark_node;
19996 }
19997
19998 tree properties = NULL_TREE;
19999 const char *const *selectors = NULL;
20000 bool allow_score = true;
20001 bool allow_user = false;
20002 int property_limit = 0;
20003 enum { CTX_PROPERTY_NONE, CTX_PROPERTY_USER, CTX_PROPERTY_NAME_LIST,
20004 CTX_PROPERTY_ID, CTX_PROPERTY_EXPR,
20005 CTX_PROPERTY_SIMD } property_kind = CTX_PROPERTY_NONE;
20006 switch (IDENTIFIER_POINTER (set)[0])
20007 {
20008 case 'c': /* construct */
20009 selectors = omp_construct_selectors;
20010 allow_score = false;
20011 property_limit = 1;
20012 property_kind = CTX_PROPERTY_SIMD;
20013 break;
20014 case 'd': /* device */
20015 selectors = omp_device_selectors;
20016 allow_score = false;
20017 allow_user = true;
20018 property_limit = 3;
20019 property_kind = CTX_PROPERTY_NAME_LIST;
20020 break;
20021 case 'i': /* implementation */
20022 selectors = omp_implementation_selectors;
20023 allow_user = true;
20024 property_limit = 3;
20025 property_kind = CTX_PROPERTY_NAME_LIST;
20026 break;
20027 case 'u': /* user */
20028 selectors = omp_user_selectors;
20029 property_limit = 1;
20030 property_kind = CTX_PROPERTY_EXPR;
20031 break;
20032 default:
20033 gcc_unreachable ();
20034 }
20035 for (int i = 0; ; i++)
20036 {
20037 if (selectors[i] == NULL)
20038 {
20039 if (allow_user)
20040 {
20041 property_kind = CTX_PROPERTY_USER;
20042 break;
20043 }
20044 else
20045 {
20046 error_at (c_parser_peek_token (parser)->location,
20047 "selector %qs not allowed for context selector "
20048 "set %qs", IDENTIFIER_POINTER (selector),
20049 IDENTIFIER_POINTER (set));
20050 c_parser_consume_token (parser);
20051 return error_mark_node;
20052 }
20053 }
20054 if (i == property_limit)
20055 property_kind = CTX_PROPERTY_NONE;
20056 if (strcmp (selectors[i], IDENTIFIER_POINTER (selector)) == 0)
20057 break;
20058 }
20059 if (property_kind == CTX_PROPERTY_NAME_LIST
20060 && IDENTIFIER_POINTER (set)[0] == 'i'
20061 && strcmp (IDENTIFIER_POINTER (selector),
20062 "atomic_default_mem_order") == 0)
20063 property_kind = CTX_PROPERTY_ID;
20064
20065 c_parser_consume_token (parser);
20066
20067 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
20068 {
20069 if (property_kind == CTX_PROPERTY_NONE)
20070 {
20071 error_at (c_parser_peek_token (parser)->location,
20072 "selector %qs does not accept any properties",
20073 IDENTIFIER_POINTER (selector));
20074 return error_mark_node;
20075 }
20076
20077 matching_parens parens;
20078 parens.require_open (parser);
20079
20080 c_token *token = c_parser_peek_token (parser);
20081 if (allow_score
20082 && c_parser_next_token_is (parser, CPP_NAME)
20083 && strcmp (IDENTIFIER_POINTER (token->value), "score") == 0
20084 && c_parser_peek_2nd_token (parser)->type == CPP_OPEN_PAREN)
20085 {
20086 c_parser_consume_token (parser);
20087
20088 matching_parens parens2;
20089 parens2.require_open (parser);
20090 tree score = c_parser_expr_no_commas (parser, NULL).value;
20091 parens2.skip_until_found_close (parser);
20092 c_parser_require (parser, CPP_COLON, "expected %<:%>");
20093 if (score != error_mark_node)
20094 {
20095 mark_exp_read (score);
20096 score = c_fully_fold (score, false, NULL);
20097 if (!INTEGRAL_TYPE_P (TREE_TYPE (score))
20098 || TREE_CODE (score) != INTEGER_CST)
20099 error_at (token->location, "score argument must be "
20100 "constant integer expression");
20101 else if (tree_int_cst_sgn (score) < 0)
20102 error_at (token->location, "score argument must be "
20103 "non-negative");
20104 else
20105 properties = tree_cons (get_identifier (" score"),
20106 score, properties);
20107 }
20108 token = c_parser_peek_token (parser);
20109 }
20110
20111 switch (property_kind)
20112 {
20113 tree t;
20114 case CTX_PROPERTY_USER:
20115 do
20116 {
20117 t = c_parser_expr_no_commas (parser, NULL).value;
20118 if (TREE_CODE (t) == STRING_CST)
20119 properties = tree_cons (NULL_TREE, t, properties);
20120 else if (t != error_mark_node)
20121 {
20122 mark_exp_read (t);
20123 t = c_fully_fold (t, false, NULL);
20124 if (!INTEGRAL_TYPE_P (TREE_TYPE (t))
20125 || !tree_fits_shwi_p (t))
20126 error_at (token->location, "property must be "
20127 "constant integer expression or string "
20128 "literal");
20129 else
20130 properties = tree_cons (NULL_TREE, t, properties);
20131 }
20132 else
20133 return error_mark_node;
20134
20135 if (c_parser_next_token_is (parser, CPP_COMMA))
20136 c_parser_consume_token (parser);
20137 else
20138 break;
20139 }
20140 while (1);
20141 break;
20142 case CTX_PROPERTY_ID:
20143 if (c_parser_next_token_is (parser, CPP_KEYWORD)
20144 || c_parser_next_token_is (parser, CPP_NAME))
20145 {
20146 tree prop = c_parser_peek_token (parser)->value;
20147 c_parser_consume_token (parser);
20148 properties = tree_cons (prop, NULL_TREE, properties);
20149 }
20150 else
20151 {
20152 c_parser_error (parser, "expected identifier");
20153 return error_mark_node;
20154 }
20155 break;
20156 case CTX_PROPERTY_NAME_LIST:
20157 do
20158 {
20159 tree prop = NULL_TREE, value = NULL_TREE;
20160 if (c_parser_next_token_is (parser, CPP_KEYWORD)
20161 || c_parser_next_token_is (parser, CPP_NAME))
20162 {
20163 prop = c_parser_peek_token (parser)->value;
20164 c_parser_consume_token (parser);
20165 }
20166 else if (c_parser_next_token_is (parser, CPP_STRING))
20167 value = c_parser_string_literal (parser, false,
20168 false).value;
20169 else
20170 {
20171 c_parser_error (parser, "expected identifier or "
20172 "string literal");
20173 return error_mark_node;
20174 }
20175
20176 properties = tree_cons (prop, value, properties);
20177
20178 if (c_parser_next_token_is (parser, CPP_COMMA))
20179 c_parser_consume_token (parser);
20180 else
20181 break;
20182 }
20183 while (1);
20184 break;
20185 case CTX_PROPERTY_EXPR:
20186 t = c_parser_expr_no_commas (parser, NULL).value;
20187 if (t != error_mark_node)
20188 {
20189 mark_exp_read (t);
20190 t = c_fully_fold (t, false, NULL);
20191 if (!INTEGRAL_TYPE_P (TREE_TYPE (t))
20192 || !tree_fits_shwi_p (t))
20193 error_at (token->location, "property must be "
20194 "constant integer expression");
20195 else
20196 properties = tree_cons (NULL_TREE, t, properties);
20197 }
20198 else
20199 return error_mark_node;
20200 break;
20201 case CTX_PROPERTY_SIMD:
20202 if (parms == NULL_TREE)
20203 {
20204 error_at (token->location, "properties for %<simd%> "
20205 "selector may not be specified in "
20206 "%<metadirective%>");
20207 return error_mark_node;
20208 }
20209 tree c;
20210 c = c_parser_omp_all_clauses (parser,
20211 OMP_DECLARE_SIMD_CLAUSE_MASK,
20212 "simd", true, 2);
20213 c = c_omp_declare_simd_clauses_to_numbers (parms
20214 == error_mark_node
20215 ? NULL_TREE : parms,
20216 c);
20217 properties = c;
20218 break;
20219 default:
20220 gcc_unreachable ();
20221 }
20222
20223 parens.skip_until_found_close (parser);
20224 properties = nreverse (properties);
20225 }
20226 else if (property_kind == CTX_PROPERTY_NAME_LIST
20227 || property_kind == CTX_PROPERTY_ID
20228 || property_kind == CTX_PROPERTY_EXPR)
20229 {
20230 c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>");
20231 return error_mark_node;
20232 }
20233
20234 ret = tree_cons (selector, properties, ret);
20235
20236 if (c_parser_next_token_is (parser, CPP_COMMA))
20237 c_parser_consume_token (parser);
20238 else
20239 break;
20240 }
20241 while (1);
20242
20243 return nreverse (ret);
20244 }
20245
20246 /* OpenMP 5.0:
20247
20248 trait-set-selector[,trait-set-selector[,...]]
20249
20250 trait-set-selector:
20251 trait-set-selector-name = { trait-selector[, trait-selector[, ...]] }
20252
20253 trait-set-selector-name:
20254 constructor
20255 device
20256 implementation
20257 user */
20258
20259 static tree
20260 c_parser_omp_context_selector_specification (c_parser *parser, tree parms)
20261 {
20262 tree ret = NULL_TREE;
20263 do
20264 {
20265 const char *setp = "";
20266 if (c_parser_next_token_is (parser, CPP_NAME))
20267 setp = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
20268 switch (setp[0])
20269 {
20270 case 'c':
20271 if (strcmp (setp, "construct") == 0)
20272 setp = NULL;
20273 break;
20274 case 'd':
20275 if (strcmp (setp, "device") == 0)
20276 setp = NULL;
20277 break;
20278 case 'i':
20279 if (strcmp (setp, "implementation") == 0)
20280 setp = NULL;
20281 break;
20282 case 'u':
20283 if (strcmp (setp, "user") == 0)
20284 setp = NULL;
20285 break;
20286 default:
20287 break;
20288 }
20289 if (setp)
20290 {
20291 c_parser_error (parser, "expected %<construct%>, %<device%>, "
20292 "%<implementation%> or %<user%>");
20293 return error_mark_node;
20294 }
20295
20296 tree set = c_parser_peek_token (parser)->value;
20297 c_parser_consume_token (parser);
20298
20299 if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
20300 return error_mark_node;
20301
20302 matching_braces braces;
20303 if (!braces.require_open (parser))
20304 return error_mark_node;
20305
20306 tree selectors = c_parser_omp_context_selector (parser, set, parms);
20307 if (selectors == error_mark_node)
20308 ret = error_mark_node;
20309 else if (ret != error_mark_node)
20310 ret = tree_cons (set, selectors, ret);
20311
20312 braces.skip_until_found_close (parser);
20313
20314 if (c_parser_next_token_is (parser, CPP_COMMA))
20315 c_parser_consume_token (parser);
20316 else
20317 break;
20318 }
20319 while (1);
20320
20321 if (ret == error_mark_node)
20322 return ret;
20323 return nreverse (ret);
20324 }
20325
20326 /* Finalize #pragma omp declare variant after FNDECL has been parsed, and put
20327 that into "omp declare variant base" attribute. */
20328
20329 static void
20330 c_finish_omp_declare_variant (c_parser *parser, tree fndecl, tree parms)
20331 {
20332 matching_parens parens;
20333 if (!parens.require_open (parser))
20334 {
20335 fail:
20336 c_parser_skip_to_pragma_eol (parser, false);
20337 return;
20338 }
20339
20340 if (c_parser_next_token_is_not (parser, CPP_NAME)
20341 || c_parser_peek_token (parser)->id_kind != C_ID_ID)
20342 {
20343 c_parser_error (parser, "expected identifier");
20344 goto fail;
20345 }
20346
20347 c_token *token = c_parser_peek_token (parser);
20348 tree variant = lookup_name (token->value);
20349
20350 if (variant == NULL_TREE)
20351 {
20352 undeclared_variable (token->location, token->value);
20353 variant = error_mark_node;
20354 }
20355
20356 c_parser_consume_token (parser);
20357
20358 parens.require_close (parser);
20359
20360 const char *clause = "";
20361 location_t match_loc = c_parser_peek_token (parser)->location;
20362 if (c_parser_next_token_is (parser, CPP_NAME))
20363 clause = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
20364 if (strcmp (clause, "match"))
20365 {
20366 c_parser_error (parser, "expected %<match%>");
20367 goto fail;
20368 }
20369
20370 c_parser_consume_token (parser);
20371
20372 if (!parens.require_open (parser))
20373 goto fail;
20374
20375 if (parms == NULL_TREE)
20376 parms = error_mark_node;
20377
20378 tree ctx = c_parser_omp_context_selector_specification (parser, parms);
20379 if (ctx == error_mark_node)
20380 goto fail;
20381 ctx = c_omp_check_context_selector (match_loc, ctx);
20382 if (ctx != error_mark_node && variant != error_mark_node)
20383 {
20384 if (TREE_CODE (variant) != FUNCTION_DECL)
20385 {
20386 error_at (token->location, "variant %qD is not a function", variant);
20387 variant = error_mark_node;
20388 }
20389 else if (omp_get_context_selector (ctx, "construct", "simd") == NULL_TREE
20390 && !comptypes (TREE_TYPE (fndecl), TREE_TYPE (variant)))
20391 {
20392 error_at (token->location, "variant %qD and base %qD have "
20393 "incompatible types", variant, fndecl);
20394 variant = error_mark_node;
20395 }
20396 else if (fndecl_built_in_p (variant)
20397 && (strncmp (IDENTIFIER_POINTER (DECL_NAME (variant)),
20398 "__builtin_", strlen ("__builtin_")) == 0
20399 || strncmp (IDENTIFIER_POINTER (DECL_NAME (variant)),
20400 "__sync_", strlen ("__sync_")) == 0
20401 || strncmp (IDENTIFIER_POINTER (DECL_NAME (variant)),
20402 "__atomic_", strlen ("__atomic_")) == 0))
20403 {
20404 error_at (token->location, "variant %qD is a built-in", variant);
20405 variant = error_mark_node;
20406 }
20407 if (variant != error_mark_node)
20408 {
20409 C_DECL_USED (variant) = 1;
20410 tree construct = omp_get_context_selector (ctx, "construct", NULL);
20411 c_omp_mark_declare_variant (match_loc, variant, construct);
20412 if (omp_context_selector_matches (ctx))
20413 {
20414 tree attr
20415 = tree_cons (get_identifier ("omp declare variant base"),
20416 build_tree_list (variant, ctx),
20417 DECL_ATTRIBUTES (fndecl));
20418 DECL_ATTRIBUTES (fndecl) = attr;
20419 }
20420 }
20421 }
20422
20423 parens.require_close (parser);
20424 c_parser_skip_to_pragma_eol (parser);
20425 }
20426
20427 /* Finalize #pragma omp declare simd or #pragma omp declare variant
20428 clauses after FNDECL has been parsed, and put that into "omp declare simd"
20429 or "omp declare variant base" attribute. */
20430
20431 static void
20432 c_finish_omp_declare_simd (c_parser *parser, tree fndecl, tree parms,
20433 vec<c_token> clauses)
20434 {
20435 /* Normally first token is CPP_NAME "simd" or "variant". CPP_EOF there
20436 indicates error has been reported and CPP_PRAGMA that
20437 c_finish_omp_declare_simd has already processed the tokens. */
20438 if (clauses.exists () && clauses[0].type == CPP_EOF)
20439 return;
20440 const char *kind = "simd";
20441 if (clauses.exists ()
20442 && (clauses[0].type == CPP_NAME || clauses[0].type == CPP_PRAGMA))
20443 kind = IDENTIFIER_POINTER (clauses[0].value);
20444 gcc_assert (strcmp (kind, "simd") == 0 || strcmp (kind, "variant") == 0);
20445 if (fndecl == NULL_TREE || TREE_CODE (fndecl) != FUNCTION_DECL)
20446 {
20447 error ("%<#pragma omp declare %s%> not immediately followed by "
20448 "a function declaration or definition", kind);
20449 clauses[0].type = CPP_EOF;
20450 return;
20451 }
20452 if (clauses.exists () && clauses[0].type != CPP_NAME)
20453 {
20454 error_at (DECL_SOURCE_LOCATION (fndecl),
20455 "%<#pragma omp declare %s%> not immediately followed by "
20456 "a single function declaration or definition", kind);
20457 clauses[0].type = CPP_EOF;
20458 return;
20459 }
20460
20461 if (parms == NULL_TREE)
20462 parms = DECL_ARGUMENTS (fndecl);
20463
20464 unsigned int tokens_avail = parser->tokens_avail;
20465 gcc_assert (parser->tokens == &parser->tokens_buf[0]);
20466
20467 parser->tokens = clauses.address ();
20468 parser->tokens_avail = clauses.length ();
20469
20470 /* c_parser_omp_declare_simd pushed 2 extra CPP_EOF tokens at the end. */
20471 while (parser->tokens_avail > 3)
20472 {
20473 c_token *token = c_parser_peek_token (parser);
20474 gcc_assert (token->type == CPP_NAME
20475 && strcmp (IDENTIFIER_POINTER (token->value), kind) == 0);
20476 c_parser_consume_token (parser);
20477 parser->in_pragma = true;
20478
20479 if (strcmp (kind, "simd") == 0)
20480 {
20481 tree c;
20482 c = c_parser_omp_all_clauses (parser, OMP_DECLARE_SIMD_CLAUSE_MASK,
20483 "#pragma omp declare simd");
20484 c = c_omp_declare_simd_clauses_to_numbers (parms, c);
20485 if (c != NULL_TREE)
20486 c = tree_cons (NULL_TREE, c, NULL_TREE);
20487 c = build_tree_list (get_identifier ("omp declare simd"), c);
20488 TREE_CHAIN (c) = DECL_ATTRIBUTES (fndecl);
20489 DECL_ATTRIBUTES (fndecl) = c;
20490 }
20491 else
20492 {
20493 gcc_assert (strcmp (kind, "variant") == 0);
20494 c_finish_omp_declare_variant (parser, fndecl, parms);
20495 }
20496 }
20497
20498 parser->tokens = &parser->tokens_buf[0];
20499 parser->tokens_avail = tokens_avail;
20500 if (clauses.exists ())
20501 clauses[0].type = CPP_PRAGMA;
20502 }
20503
20504
20505 /* OpenMP 4.0:
20506 # pragma omp declare target new-line
20507 declarations and definitions
20508 # pragma omp end declare target new-line
20509
20510 OpenMP 4.5:
20511 # pragma omp declare target ( extended-list ) new-line
20512
20513 # pragma omp declare target declare-target-clauses[seq] new-line */
20514
20515 #define OMP_DECLARE_TARGET_CLAUSE_MASK \
20516 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
20517 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK) \
20518 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE_TYPE))
20519
20520 static void
20521 c_parser_omp_declare_target (c_parser *parser)
20522 {
20523 tree clauses = NULL_TREE;
20524 int device_type = 0;
20525 bool only_device_type = true;
20526 if (c_parser_next_token_is (parser, CPP_NAME))
20527 clauses = c_parser_omp_all_clauses (parser, OMP_DECLARE_TARGET_CLAUSE_MASK,
20528 "#pragma omp declare target");
20529 else if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
20530 {
20531 clauses = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_TO_DECLARE,
20532 clauses);
20533 clauses = c_finish_omp_clauses (clauses, C_ORT_OMP);
20534 c_parser_skip_to_pragma_eol (parser);
20535 }
20536 else
20537 {
20538 c_parser_skip_to_pragma_eol (parser);
20539 current_omp_declare_target_attribute++;
20540 return;
20541 }
20542 for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
20543 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEVICE_TYPE)
20544 device_type |= OMP_CLAUSE_DEVICE_TYPE_KIND (c);
20545 for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
20546 {
20547 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEVICE_TYPE)
20548 continue;
20549 tree t = OMP_CLAUSE_DECL (c), id;
20550 tree at1 = lookup_attribute ("omp declare target", DECL_ATTRIBUTES (t));
20551 tree at2 = lookup_attribute ("omp declare target link",
20552 DECL_ATTRIBUTES (t));
20553 only_device_type = false;
20554 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINK)
20555 {
20556 id = get_identifier ("omp declare target link");
20557 std::swap (at1, at2);
20558 }
20559 else
20560 id = get_identifier ("omp declare target");
20561 if (at2)
20562 {
20563 error_at (OMP_CLAUSE_LOCATION (c),
20564 "%qD specified both in declare target %<link%> and %<to%>"
20565 " clauses", t);
20566 continue;
20567 }
20568 if (!at1)
20569 {
20570 DECL_ATTRIBUTES (t) = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (t));
20571 if (TREE_CODE (t) != FUNCTION_DECL && !is_global_var (t))
20572 continue;
20573
20574 symtab_node *node = symtab_node::get (t);
20575 if (node != NULL)
20576 {
20577 node->offloadable = 1;
20578 if (ENABLE_OFFLOADING)
20579 {
20580 g->have_offload = true;
20581 if (is_a <varpool_node *> (node))
20582 vec_safe_push (offload_vars, t);
20583 }
20584 }
20585 }
20586 if (TREE_CODE (t) != FUNCTION_DECL)
20587 continue;
20588 if ((device_type & OMP_CLAUSE_DEVICE_TYPE_HOST) != 0)
20589 {
20590 tree at3 = lookup_attribute ("omp declare target host",
20591 DECL_ATTRIBUTES (t));
20592 if (at3 == NULL_TREE)
20593 {
20594 id = get_identifier ("omp declare target host");
20595 DECL_ATTRIBUTES (t)
20596 = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (t));
20597 }
20598 }
20599 if ((device_type & OMP_CLAUSE_DEVICE_TYPE_NOHOST) != 0)
20600 {
20601 tree at3 = lookup_attribute ("omp declare target nohost",
20602 DECL_ATTRIBUTES (t));
20603 if (at3 == NULL_TREE)
20604 {
20605 id = get_identifier ("omp declare target nohost");
20606 DECL_ATTRIBUTES (t)
20607 = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (t));
20608 }
20609 }
20610 }
20611 if (device_type && only_device_type)
20612 warning_at (OMP_CLAUSE_LOCATION (clauses), 0,
20613 "directive with only %<device_type%> clauses ignored");
20614 }
20615
20616 static void
20617 c_parser_omp_end_declare_target (c_parser *parser)
20618 {
20619 location_t loc = c_parser_peek_token (parser)->location;
20620 c_parser_consume_pragma (parser);
20621 if (c_parser_next_token_is (parser, CPP_NAME)
20622 && strcmp (IDENTIFIER_POINTER (c_parser_peek_token (parser)->value),
20623 "declare") == 0)
20624 {
20625 c_parser_consume_token (parser);
20626 if (c_parser_next_token_is (parser, CPP_NAME)
20627 && strcmp (IDENTIFIER_POINTER (c_parser_peek_token (parser)->value),
20628 "target") == 0)
20629 c_parser_consume_token (parser);
20630 else
20631 {
20632 c_parser_error (parser, "expected %<target%>");
20633 c_parser_skip_to_pragma_eol (parser);
20634 return;
20635 }
20636 }
20637 else
20638 {
20639 c_parser_error (parser, "expected %<declare%>");
20640 c_parser_skip_to_pragma_eol (parser);
20641 return;
20642 }
20643 c_parser_skip_to_pragma_eol (parser);
20644 if (!current_omp_declare_target_attribute)
20645 error_at (loc, "%<#pragma omp end declare target%> without corresponding "
20646 "%<#pragma omp declare target%>");
20647 else
20648 current_omp_declare_target_attribute--;
20649 }
20650
20651
20652 /* OpenMP 4.0
20653 #pragma omp declare reduction (reduction-id : typename-list : expression) \
20654 initializer-clause[opt] new-line
20655
20656 initializer-clause:
20657 initializer (omp_priv = initializer)
20658 initializer (function-name (argument-list)) */
20659
20660 static void
20661 c_parser_omp_declare_reduction (c_parser *parser, enum pragma_context context)
20662 {
20663 unsigned int tokens_avail = 0, i;
20664 vec<tree> types = vNULL;
20665 vec<c_token> clauses = vNULL;
20666 enum tree_code reduc_code = ERROR_MARK;
20667 tree reduc_id = NULL_TREE;
20668 tree type;
20669 location_t rloc = c_parser_peek_token (parser)->location;
20670
20671 if (context == pragma_struct || context == pragma_param)
20672 {
20673 error ("%<#pragma omp declare reduction%> not at file or block scope");
20674 goto fail;
20675 }
20676
20677 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
20678 goto fail;
20679
20680 switch (c_parser_peek_token (parser)->type)
20681 {
20682 case CPP_PLUS:
20683 reduc_code = PLUS_EXPR;
20684 break;
20685 case CPP_MULT:
20686 reduc_code = MULT_EXPR;
20687 break;
20688 case CPP_MINUS:
20689 reduc_code = MINUS_EXPR;
20690 break;
20691 case CPP_AND:
20692 reduc_code = BIT_AND_EXPR;
20693 break;
20694 case CPP_XOR:
20695 reduc_code = BIT_XOR_EXPR;
20696 break;
20697 case CPP_OR:
20698 reduc_code = BIT_IOR_EXPR;
20699 break;
20700 case CPP_AND_AND:
20701 reduc_code = TRUTH_ANDIF_EXPR;
20702 break;
20703 case CPP_OR_OR:
20704 reduc_code = TRUTH_ORIF_EXPR;
20705 break;
20706 case CPP_NAME:
20707 const char *p;
20708 p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
20709 if (strcmp (p, "min") == 0)
20710 {
20711 reduc_code = MIN_EXPR;
20712 break;
20713 }
20714 if (strcmp (p, "max") == 0)
20715 {
20716 reduc_code = MAX_EXPR;
20717 break;
20718 }
20719 reduc_id = c_parser_peek_token (parser)->value;
20720 break;
20721 default:
20722 c_parser_error (parser,
20723 "expected %<+%>, %<*%>, %<-%>, %<&%>, "
20724 "%<^%>, %<|%>, %<&&%>, %<||%> or identifier");
20725 goto fail;
20726 }
20727
20728 tree orig_reduc_id, reduc_decl;
20729 orig_reduc_id = reduc_id;
20730 reduc_id = c_omp_reduction_id (reduc_code, reduc_id);
20731 reduc_decl = c_omp_reduction_decl (reduc_id);
20732 c_parser_consume_token (parser);
20733
20734 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
20735 goto fail;
20736
20737 while (true)
20738 {
20739 location_t loc = c_parser_peek_token (parser)->location;
20740 struct c_type_name *ctype = c_parser_type_name (parser);
20741 if (ctype != NULL)
20742 {
20743 type = groktypename (ctype, NULL, NULL);
20744 if (type == error_mark_node)
20745 ;
20746 else if ((INTEGRAL_TYPE_P (type)
20747 || TREE_CODE (type) == REAL_TYPE
20748 || TREE_CODE (type) == COMPLEX_TYPE)
20749 && orig_reduc_id == NULL_TREE)
20750 error_at (loc, "predeclared arithmetic type in "
20751 "%<#pragma omp declare reduction%>");
20752 else if (TREE_CODE (type) == FUNCTION_TYPE
20753 || TREE_CODE (type) == ARRAY_TYPE)
20754 error_at (loc, "function or array type in "
20755 "%<#pragma omp declare reduction%>");
20756 else if (TYPE_ATOMIC (type))
20757 error_at (loc, "%<_Atomic%> qualified type in "
20758 "%<#pragma omp declare reduction%>");
20759 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
20760 error_at (loc, "const, volatile or restrict qualified type in "
20761 "%<#pragma omp declare reduction%>");
20762 else
20763 {
20764 tree t;
20765 for (t = DECL_INITIAL (reduc_decl); t; t = TREE_CHAIN (t))
20766 if (comptypes (TREE_PURPOSE (t), type))
20767 {
20768 error_at (loc, "redeclaration of %qs "
20769 "%<#pragma omp declare reduction%> for "
20770 "type %qT",
20771 IDENTIFIER_POINTER (reduc_id)
20772 + sizeof ("omp declare reduction ") - 1,
20773 type);
20774 location_t ploc
20775 = DECL_SOURCE_LOCATION (TREE_VEC_ELT (TREE_VALUE (t),
20776 0));
20777 error_at (ploc, "previous %<#pragma omp declare "
20778 "reduction%>");
20779 break;
20780 }
20781 if (t == NULL_TREE)
20782 types.safe_push (type);
20783 }
20784 if (c_parser_next_token_is (parser, CPP_COMMA))
20785 c_parser_consume_token (parser);
20786 else
20787 break;
20788 }
20789 else
20790 break;
20791 }
20792
20793 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>")
20794 || types.is_empty ())
20795 {
20796 fail:
20797 clauses.release ();
20798 types.release ();
20799 while (true)
20800 {
20801 c_token *token = c_parser_peek_token (parser);
20802 if (token->type == CPP_EOF || token->type == CPP_PRAGMA_EOL)
20803 break;
20804 c_parser_consume_token (parser);
20805 }
20806 c_parser_skip_to_pragma_eol (parser);
20807 return;
20808 }
20809
20810 if (types.length () > 1)
20811 {
20812 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
20813 {
20814 c_token *token = c_parser_peek_token (parser);
20815 if (token->type == CPP_EOF)
20816 goto fail;
20817 clauses.safe_push (*token);
20818 c_parser_consume_token (parser);
20819 }
20820 clauses.safe_push (*c_parser_peek_token (parser));
20821 c_parser_skip_to_pragma_eol (parser);
20822
20823 /* Make sure nothing tries to read past the end of the tokens. */
20824 c_token eof_token;
20825 memset (&eof_token, 0, sizeof (eof_token));
20826 eof_token.type = CPP_EOF;
20827 clauses.safe_push (eof_token);
20828 clauses.safe_push (eof_token);
20829 }
20830
20831 int errs = errorcount;
20832 FOR_EACH_VEC_ELT (types, i, type)
20833 {
20834 tokens_avail = parser->tokens_avail;
20835 gcc_assert (parser->tokens == &parser->tokens_buf[0]);
20836 if (!clauses.is_empty ())
20837 {
20838 parser->tokens = clauses.address ();
20839 parser->tokens_avail = clauses.length ();
20840 parser->in_pragma = true;
20841 }
20842
20843 bool nested = current_function_decl != NULL_TREE;
20844 if (nested)
20845 c_push_function_context ();
20846 tree fndecl = build_decl (BUILTINS_LOCATION, FUNCTION_DECL,
20847 reduc_id, default_function_type);
20848 current_function_decl = fndecl;
20849 allocate_struct_function (fndecl, true);
20850 push_scope ();
20851 tree stmt = push_stmt_list ();
20852 /* Intentionally BUILTINS_LOCATION, so that -Wshadow doesn't
20853 warn about these. */
20854 tree omp_out = build_decl (BUILTINS_LOCATION, VAR_DECL,
20855 get_identifier ("omp_out"), type);
20856 DECL_ARTIFICIAL (omp_out) = 1;
20857 DECL_CONTEXT (omp_out) = fndecl;
20858 pushdecl (omp_out);
20859 tree omp_in = build_decl (BUILTINS_LOCATION, VAR_DECL,
20860 get_identifier ("omp_in"), type);
20861 DECL_ARTIFICIAL (omp_in) = 1;
20862 DECL_CONTEXT (omp_in) = fndecl;
20863 pushdecl (omp_in);
20864 struct c_expr combiner = c_parser_expression (parser);
20865 struct c_expr initializer;
20866 tree omp_priv = NULL_TREE, omp_orig = NULL_TREE;
20867 bool bad = false;
20868 initializer.set_error ();
20869 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
20870 bad = true;
20871 else if (c_parser_next_token_is (parser, CPP_NAME)
20872 && strcmp (IDENTIFIER_POINTER
20873 (c_parser_peek_token (parser)->value),
20874 "initializer") == 0)
20875 {
20876 c_parser_consume_token (parser);
20877 pop_scope ();
20878 push_scope ();
20879 omp_priv = build_decl (BUILTINS_LOCATION, VAR_DECL,
20880 get_identifier ("omp_priv"), type);
20881 DECL_ARTIFICIAL (omp_priv) = 1;
20882 DECL_INITIAL (omp_priv) = error_mark_node;
20883 DECL_CONTEXT (omp_priv) = fndecl;
20884 pushdecl (omp_priv);
20885 omp_orig = build_decl (BUILTINS_LOCATION, VAR_DECL,
20886 get_identifier ("omp_orig"), type);
20887 DECL_ARTIFICIAL (omp_orig) = 1;
20888 DECL_CONTEXT (omp_orig) = fndecl;
20889 pushdecl (omp_orig);
20890 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
20891 bad = true;
20892 else if (!c_parser_next_token_is (parser, CPP_NAME))
20893 {
20894 c_parser_error (parser, "expected %<omp_priv%> or "
20895 "function-name");
20896 bad = true;
20897 }
20898 else if (strcmp (IDENTIFIER_POINTER
20899 (c_parser_peek_token (parser)->value),
20900 "omp_priv") != 0)
20901 {
20902 if (c_parser_peek_2nd_token (parser)->type != CPP_OPEN_PAREN
20903 || c_parser_peek_token (parser)->id_kind != C_ID_ID)
20904 {
20905 c_parser_error (parser, "expected function-name %<(%>");
20906 bad = true;
20907 }
20908 else
20909 initializer = c_parser_postfix_expression (parser);
20910 if (initializer.value
20911 && TREE_CODE (initializer.value) == CALL_EXPR)
20912 {
20913 int j;
20914 tree c = initializer.value;
20915 for (j = 0; j < call_expr_nargs (c); j++)
20916 {
20917 tree a = CALL_EXPR_ARG (c, j);
20918 STRIP_NOPS (a);
20919 if (TREE_CODE (a) == ADDR_EXPR
20920 && TREE_OPERAND (a, 0) == omp_priv)
20921 break;
20922 }
20923 if (j == call_expr_nargs (c))
20924 error ("one of the initializer call arguments should be "
20925 "%<&omp_priv%>");
20926 }
20927 }
20928 else
20929 {
20930 c_parser_consume_token (parser);
20931 if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
20932 bad = true;
20933 else
20934 {
20935 tree st = push_stmt_list ();
20936 location_t loc = c_parser_peek_token (parser)->location;
20937 rich_location richloc (line_table, loc);
20938 start_init (omp_priv, NULL_TREE, 0, &richloc);
20939 struct c_expr init = c_parser_initializer (parser);
20940 finish_init ();
20941 finish_decl (omp_priv, loc, init.value,
20942 init.original_type, NULL_TREE);
20943 pop_stmt_list (st);
20944 }
20945 }
20946 if (!bad
20947 && !c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
20948 bad = true;
20949 }
20950
20951 if (!bad)
20952 {
20953 c_parser_skip_to_pragma_eol (parser);
20954
20955 tree t = tree_cons (type, make_tree_vec (omp_priv ? 6 : 3),
20956 DECL_INITIAL (reduc_decl));
20957 DECL_INITIAL (reduc_decl) = t;
20958 DECL_SOURCE_LOCATION (omp_out) = rloc;
20959 TREE_VEC_ELT (TREE_VALUE (t), 0) = omp_out;
20960 TREE_VEC_ELT (TREE_VALUE (t), 1) = omp_in;
20961 TREE_VEC_ELT (TREE_VALUE (t), 2) = combiner.value;
20962 walk_tree (&combiner.value, c_check_omp_declare_reduction_r,
20963 &TREE_VEC_ELT (TREE_VALUE (t), 0), NULL);
20964 if (omp_priv)
20965 {
20966 DECL_SOURCE_LOCATION (omp_priv) = rloc;
20967 TREE_VEC_ELT (TREE_VALUE (t), 3) = omp_priv;
20968 TREE_VEC_ELT (TREE_VALUE (t), 4) = omp_orig;
20969 TREE_VEC_ELT (TREE_VALUE (t), 5) = initializer.value;
20970 walk_tree (&initializer.value, c_check_omp_declare_reduction_r,
20971 &TREE_VEC_ELT (TREE_VALUE (t), 3), NULL);
20972 walk_tree (&DECL_INITIAL (omp_priv),
20973 c_check_omp_declare_reduction_r,
20974 &TREE_VEC_ELT (TREE_VALUE (t), 3), NULL);
20975 }
20976 }
20977
20978 pop_stmt_list (stmt);
20979 pop_scope ();
20980 if (cfun->language != NULL)
20981 {
20982 ggc_free (cfun->language);
20983 cfun->language = NULL;
20984 }
20985 set_cfun (NULL);
20986 current_function_decl = NULL_TREE;
20987 if (nested)
20988 c_pop_function_context ();
20989
20990 if (!clauses.is_empty ())
20991 {
20992 parser->tokens = &parser->tokens_buf[0];
20993 parser->tokens_avail = tokens_avail;
20994 }
20995 if (bad)
20996 goto fail;
20997 if (errs != errorcount)
20998 break;
20999 }
21000
21001 clauses.release ();
21002 types.release ();
21003 }
21004
21005
21006 /* OpenMP 4.0
21007 #pragma omp declare simd declare-simd-clauses[optseq] new-line
21008 #pragma omp declare reduction (reduction-id : typename-list : expression) \
21009 initializer-clause[opt] new-line
21010 #pragma omp declare target new-line
21011
21012 OpenMP 5.0
21013 #pragma omp declare variant (identifier) match (context-selector) */
21014
21015 static void
21016 c_parser_omp_declare (c_parser *parser, enum pragma_context context)
21017 {
21018 c_parser_consume_pragma (parser);
21019 if (c_parser_next_token_is (parser, CPP_NAME))
21020 {
21021 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
21022 if (strcmp (p, "simd") == 0)
21023 {
21024 /* c_parser_consume_token (parser); done in
21025 c_parser_omp_declare_simd. */
21026 c_parser_omp_declare_simd (parser, context);
21027 return;
21028 }
21029 if (strcmp (p, "reduction") == 0)
21030 {
21031 c_parser_consume_token (parser);
21032 c_parser_omp_declare_reduction (parser, context);
21033 return;
21034 }
21035 if (!flag_openmp) /* flag_openmp_simd */
21036 {
21037 c_parser_skip_to_pragma_eol (parser, false);
21038 return;
21039 }
21040 if (strcmp (p, "target") == 0)
21041 {
21042 c_parser_consume_token (parser);
21043 c_parser_omp_declare_target (parser);
21044 return;
21045 }
21046 if (strcmp (p, "variant") == 0)
21047 {
21048 /* c_parser_consume_token (parser); done in
21049 c_parser_omp_declare_simd. */
21050 c_parser_omp_declare_simd (parser, context);
21051 return;
21052 }
21053 }
21054
21055 c_parser_error (parser, "expected %<simd%>, %<reduction%>, "
21056 "%<target%> or %<variant%>");
21057 c_parser_skip_to_pragma_eol (parser);
21058 }
21059
21060 /* OpenMP 5.0
21061 #pragma omp requires clauses[optseq] new-line */
21062
21063 static void
21064 c_parser_omp_requires (c_parser *parser)
21065 {
21066 bool first = true;
21067 enum omp_requires new_req = (enum omp_requires) 0;
21068
21069 c_parser_consume_pragma (parser);
21070
21071 location_t loc = c_parser_peek_token (parser)->location;
21072 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
21073 {
21074 if (!first && c_parser_next_token_is (parser, CPP_COMMA))
21075 c_parser_consume_token (parser);
21076
21077 first = false;
21078
21079 if (c_parser_next_token_is (parser, CPP_NAME))
21080 {
21081 const char *p
21082 = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
21083 location_t cloc = c_parser_peek_token (parser)->location;
21084 enum omp_requires this_req = (enum omp_requires) 0;
21085
21086 if (!strcmp (p, "unified_address"))
21087 this_req = OMP_REQUIRES_UNIFIED_ADDRESS;
21088 else if (!strcmp (p, "unified_shared_memory"))
21089 this_req = OMP_REQUIRES_UNIFIED_SHARED_MEMORY;
21090 else if (!strcmp (p, "dynamic_allocators"))
21091 this_req = OMP_REQUIRES_DYNAMIC_ALLOCATORS;
21092 else if (!strcmp (p, "reverse_offload"))
21093 this_req = OMP_REQUIRES_REVERSE_OFFLOAD;
21094 else if (!strcmp (p, "atomic_default_mem_order"))
21095 {
21096 c_parser_consume_token (parser);
21097
21098 matching_parens parens;
21099 if (parens.require_open (parser))
21100 {
21101 if (c_parser_next_token_is (parser, CPP_NAME))
21102 {
21103 tree v = c_parser_peek_token (parser)->value;
21104 p = IDENTIFIER_POINTER (v);
21105
21106 if (!strcmp (p, "seq_cst"))
21107 this_req
21108 = (enum omp_requires) OMP_MEMORY_ORDER_SEQ_CST;
21109 else if (!strcmp (p, "relaxed"))
21110 this_req
21111 = (enum omp_requires) OMP_MEMORY_ORDER_RELAXED;
21112 else if (!strcmp (p, "acq_rel"))
21113 this_req
21114 = (enum omp_requires) OMP_MEMORY_ORDER_ACQ_REL;
21115 }
21116 if (this_req == 0)
21117 {
21118 error_at (c_parser_peek_token (parser)->location,
21119 "expected %<seq_cst%>, %<relaxed%> or "
21120 "%<acq_rel%>");
21121 if (c_parser_peek_2nd_token (parser)->type
21122 == CPP_CLOSE_PAREN)
21123 c_parser_consume_token (parser);
21124 }
21125 else
21126 c_parser_consume_token (parser);
21127
21128 parens.skip_until_found_close (parser);
21129 if (this_req == 0)
21130 {
21131 c_parser_skip_to_pragma_eol (parser, false);
21132 return;
21133 }
21134 }
21135 p = NULL;
21136 }
21137 else
21138 {
21139 error_at (cloc, "expected %<unified_address%>, "
21140 "%<unified_shared_memory%>, "
21141 "%<dynamic_allocators%>, "
21142 "%<reverse_offload%> "
21143 "or %<atomic_default_mem_order%> clause");
21144 c_parser_skip_to_pragma_eol (parser, false);
21145 return;
21146 }
21147 if (p)
21148 sorry_at (cloc, "%qs clause on %<requires%> directive not "
21149 "supported yet", p);
21150 if (p)
21151 c_parser_consume_token (parser);
21152 if (this_req)
21153 {
21154 if ((this_req & ~OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER) != 0)
21155 {
21156 if ((this_req & new_req) != 0)
21157 error_at (cloc, "too many %qs clauses", p);
21158 if (this_req != OMP_REQUIRES_DYNAMIC_ALLOCATORS
21159 && (omp_requires_mask & OMP_REQUIRES_TARGET_USED) != 0)
21160 error_at (cloc, "%qs clause used lexically after first "
21161 "target construct or offloading API", p);
21162 }
21163 else if ((new_req & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER) != 0)
21164 {
21165 error_at (cloc, "too many %qs clauses",
21166 "atomic_default_mem_order");
21167 this_req = (enum omp_requires) 0;
21168 }
21169 else if ((omp_requires_mask
21170 & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER) != 0)
21171 {
21172 error_at (cloc, "more than one %<atomic_default_mem_order%>"
21173 " clause in a single compilation unit");
21174 this_req
21175 = (enum omp_requires)
21176 (omp_requires_mask
21177 & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER);
21178 }
21179 else if ((omp_requires_mask
21180 & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER_USED) != 0)
21181 error_at (cloc, "%<atomic_default_mem_order%> clause used "
21182 "lexically after first %<atomic%> construct "
21183 "without memory order clause");
21184 new_req = (enum omp_requires) (new_req | this_req);
21185 omp_requires_mask
21186 = (enum omp_requires) (omp_requires_mask | this_req);
21187 continue;
21188 }
21189 }
21190 break;
21191 }
21192 c_parser_skip_to_pragma_eol (parser);
21193
21194 if (new_req == 0)
21195 error_at (loc, "%<pragma omp requires%> requires at least one clause");
21196 }
21197
21198 /* Helper function for c_parser_omp_taskloop.
21199 Disallow zero sized or potentially zero sized task reductions. */
21200
21201 static tree
21202 c_finish_taskloop_clauses (tree clauses)
21203 {
21204 tree *pc = &clauses;
21205 for (tree c = clauses; c; c = *pc)
21206 {
21207 bool remove = false;
21208 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
21209 {
21210 tree type = strip_array_types (TREE_TYPE (OMP_CLAUSE_DECL (c)));
21211 if (integer_zerop (TYPE_SIZE_UNIT (type)))
21212 {
21213 error_at (OMP_CLAUSE_LOCATION (c),
21214 "zero sized type %qT in %<reduction%> clause", type);
21215 remove = true;
21216 }
21217 else if (TREE_CODE (TYPE_SIZE_UNIT (type)) != INTEGER_CST)
21218 {
21219 error_at (OMP_CLAUSE_LOCATION (c),
21220 "variable sized type %qT in %<reduction%> clause",
21221 type);
21222 remove = true;
21223 }
21224 }
21225 if (remove)
21226 *pc = OMP_CLAUSE_CHAIN (c);
21227 else
21228 pc = &OMP_CLAUSE_CHAIN (c);
21229 }
21230 return clauses;
21231 }
21232
21233 /* OpenMP 4.5:
21234 #pragma omp taskloop taskloop-clause[optseq] new-line
21235 for-loop
21236
21237 #pragma omp taskloop simd taskloop-simd-clause[optseq] new-line
21238 for-loop */
21239
21240 #define OMP_TASKLOOP_CLAUSE_MASK \
21241 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
21242 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
21243 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
21244 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
21245 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
21246 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_GRAINSIZE) \
21247 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TASKS) \
21248 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
21249 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
21250 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
21251 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
21252 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
21253 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOGROUP) \
21254 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY) \
21255 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
21256 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IN_REDUCTION))
21257
21258 static tree
21259 c_parser_omp_taskloop (location_t loc, c_parser *parser,
21260 char *p_name, omp_clause_mask mask, tree *cclauses,
21261 bool *if_p)
21262 {
21263 tree clauses, block, ret;
21264
21265 strcat (p_name, " taskloop");
21266 mask |= OMP_TASKLOOP_CLAUSE_MASK;
21267 /* #pragma omp parallel master taskloop{, simd} disallow in_reduction
21268 clause. */
21269 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS)) != 0)
21270 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IN_REDUCTION);
21271
21272 if (c_parser_next_token_is (parser, CPP_NAME))
21273 {
21274 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
21275
21276 if (strcmp (p, "simd") == 0)
21277 {
21278 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
21279 if (cclauses == NULL)
21280 cclauses = cclauses_buf;
21281 c_parser_consume_token (parser);
21282 if (!flag_openmp) /* flag_openmp_simd */
21283 return c_parser_omp_simd (loc, parser, p_name, mask, cclauses,
21284 if_p);
21285 block = c_begin_compound_stmt (true);
21286 ret = c_parser_omp_simd (loc, parser, p_name, mask, cclauses, if_p);
21287 block = c_end_compound_stmt (loc, block, true);
21288 if (ret == NULL)
21289 return ret;
21290 ret = make_node (OMP_TASKLOOP);
21291 TREE_TYPE (ret) = void_type_node;
21292 OMP_FOR_BODY (ret) = block;
21293 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
21294 OMP_FOR_CLAUSES (ret)
21295 = c_finish_taskloop_clauses (OMP_FOR_CLAUSES (ret));
21296 SET_EXPR_LOCATION (ret, loc);
21297 add_stmt (ret);
21298 return ret;
21299 }
21300 }
21301 if (!flag_openmp) /* flag_openmp_simd */
21302 {
21303 c_parser_skip_to_pragma_eol (parser, false);
21304 return NULL_TREE;
21305 }
21306
21307 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
21308 if (cclauses)
21309 {
21310 omp_split_clauses (loc, OMP_TASKLOOP, mask, clauses, cclauses);
21311 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
21312 }
21313
21314 clauses = c_finish_taskloop_clauses (clauses);
21315 block = c_begin_compound_stmt (true);
21316 ret = c_parser_omp_for_loop (loc, parser, OMP_TASKLOOP, clauses, NULL, if_p);
21317 block = c_end_compound_stmt (loc, block, true);
21318 add_stmt (block);
21319
21320 return ret;
21321 }
21322
21323 /* Main entry point to parsing most OpenMP pragmas. */
21324
21325 static void
21326 c_parser_omp_construct (c_parser *parser, bool *if_p)
21327 {
21328 enum pragma_kind p_kind;
21329 location_t loc;
21330 tree stmt;
21331 char p_name[sizeof "#pragma omp teams distribute parallel for simd"];
21332 omp_clause_mask mask (0);
21333
21334 loc = c_parser_peek_token (parser)->location;
21335 p_kind = c_parser_peek_token (parser)->pragma_kind;
21336 c_parser_consume_pragma (parser);
21337
21338 switch (p_kind)
21339 {
21340 case PRAGMA_OACC_ATOMIC:
21341 c_parser_omp_atomic (loc, parser);
21342 return;
21343 case PRAGMA_OACC_CACHE:
21344 strcpy (p_name, "#pragma acc");
21345 stmt = c_parser_oacc_cache (loc, parser);
21346 break;
21347 case PRAGMA_OACC_DATA:
21348 stmt = c_parser_oacc_data (loc, parser, if_p);
21349 break;
21350 case PRAGMA_OACC_HOST_DATA:
21351 stmt = c_parser_oacc_host_data (loc, parser, if_p);
21352 break;
21353 case PRAGMA_OACC_KERNELS:
21354 case PRAGMA_OACC_PARALLEL:
21355 case PRAGMA_OACC_SERIAL:
21356 strcpy (p_name, "#pragma acc");
21357 stmt = c_parser_oacc_compute (loc, parser, p_kind, p_name, if_p);
21358 break;
21359 case PRAGMA_OACC_LOOP:
21360 strcpy (p_name, "#pragma acc");
21361 stmt = c_parser_oacc_loop (loc, parser, p_name, mask, NULL, if_p);
21362 break;
21363 case PRAGMA_OACC_WAIT:
21364 strcpy (p_name, "#pragma wait");
21365 stmt = c_parser_oacc_wait (loc, parser, p_name);
21366 break;
21367 case PRAGMA_OMP_ATOMIC:
21368 c_parser_omp_atomic (loc, parser);
21369 return;
21370 case PRAGMA_OMP_CRITICAL:
21371 stmt = c_parser_omp_critical (loc, parser, if_p);
21372 break;
21373 case PRAGMA_OMP_DISTRIBUTE:
21374 strcpy (p_name, "#pragma omp");
21375 stmt = c_parser_omp_distribute (loc, parser, p_name, mask, NULL, if_p);
21376 break;
21377 case PRAGMA_OMP_FOR:
21378 strcpy (p_name, "#pragma omp");
21379 stmt = c_parser_omp_for (loc, parser, p_name, mask, NULL, if_p);
21380 break;
21381 case PRAGMA_OMP_LOOP:
21382 strcpy (p_name, "#pragma omp");
21383 stmt = c_parser_omp_loop (loc, parser, p_name, mask, NULL, if_p);
21384 break;
21385 case PRAGMA_OMP_MASTER:
21386 strcpy (p_name, "#pragma omp");
21387 stmt = c_parser_omp_master (loc, parser, p_name, mask, NULL, if_p);
21388 break;
21389 case PRAGMA_OMP_PARALLEL:
21390 strcpy (p_name, "#pragma omp");
21391 stmt = c_parser_omp_parallel (loc, parser, p_name, mask, NULL, if_p);
21392 break;
21393 case PRAGMA_OMP_SECTIONS:
21394 strcpy (p_name, "#pragma omp");
21395 stmt = c_parser_omp_sections (loc, parser, p_name, mask, NULL);
21396 break;
21397 case PRAGMA_OMP_SIMD:
21398 strcpy (p_name, "#pragma omp");
21399 stmt = c_parser_omp_simd (loc, parser, p_name, mask, NULL, if_p);
21400 break;
21401 case PRAGMA_OMP_SINGLE:
21402 stmt = c_parser_omp_single (loc, parser, if_p);
21403 break;
21404 case PRAGMA_OMP_TASK:
21405 stmt = c_parser_omp_task (loc, parser, if_p);
21406 break;
21407 case PRAGMA_OMP_TASKGROUP:
21408 stmt = c_parser_omp_taskgroup (loc, parser, if_p);
21409 break;
21410 case PRAGMA_OMP_TASKLOOP:
21411 strcpy (p_name, "#pragma omp");
21412 stmt = c_parser_omp_taskloop (loc, parser, p_name, mask, NULL, if_p);
21413 break;
21414 case PRAGMA_OMP_TEAMS:
21415 strcpy (p_name, "#pragma omp");
21416 stmt = c_parser_omp_teams (loc, parser, p_name, mask, NULL, if_p);
21417 break;
21418 default:
21419 gcc_unreachable ();
21420 }
21421
21422 if (stmt)
21423 gcc_assert (EXPR_LOCATION (stmt) != UNKNOWN_LOCATION);
21424 }
21425
21426
21427 /* OpenMP 2.5:
21428 # pragma omp threadprivate (variable-list) */
21429
21430 static void
21431 c_parser_omp_threadprivate (c_parser *parser)
21432 {
21433 tree vars, t;
21434 location_t loc;
21435
21436 c_parser_consume_pragma (parser);
21437 loc = c_parser_peek_token (parser)->location;
21438 vars = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_ERROR, NULL);
21439
21440 /* Mark every variable in VARS to be assigned thread local storage. */
21441 for (t = vars; t; t = TREE_CHAIN (t))
21442 {
21443 tree v = TREE_PURPOSE (t);
21444
21445 /* FIXME diagnostics: Ideally we should keep individual
21446 locations for all the variables in the var list to make the
21447 following errors more precise. Perhaps
21448 c_parser_omp_var_list_parens() should construct a list of
21449 locations to go along with the var list. */
21450
21451 /* If V had already been marked threadprivate, it doesn't matter
21452 whether it had been used prior to this point. */
21453 if (!VAR_P (v))
21454 error_at (loc, "%qD is not a variable", v);
21455 else if (TREE_USED (v) && !C_DECL_THREADPRIVATE_P (v))
21456 error_at (loc, "%qE declared %<threadprivate%> after first use", v);
21457 else if (! is_global_var (v))
21458 error_at (loc, "automatic variable %qE cannot be %<threadprivate%>", v);
21459 else if (TREE_TYPE (v) == error_mark_node)
21460 ;
21461 else if (! COMPLETE_TYPE_P (TREE_TYPE (v)))
21462 error_at (loc, "%<threadprivate%> %qE has incomplete type", v);
21463 else
21464 {
21465 if (! DECL_THREAD_LOCAL_P (v))
21466 {
21467 set_decl_tls_model (v, decl_default_tls_model (v));
21468 /* If rtl has been already set for this var, call
21469 make_decl_rtl once again, so that encode_section_info
21470 has a chance to look at the new decl flags. */
21471 if (DECL_RTL_SET_P (v))
21472 make_decl_rtl (v);
21473 }
21474 C_DECL_THREADPRIVATE_P (v) = 1;
21475 }
21476 }
21477
21478 c_parser_skip_to_pragma_eol (parser);
21479 }
21480
21481 /* Parse a transaction attribute (GCC Extension).
21482
21483 transaction-attribute:
21484 gnu-attributes
21485 attribute-specifier
21486 */
21487
21488 static tree
21489 c_parser_transaction_attributes (c_parser *parser)
21490 {
21491 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
21492 return c_parser_gnu_attributes (parser);
21493
21494 if (!c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
21495 return NULL_TREE;
21496 return c_parser_std_attribute_specifier (parser, true);
21497 }
21498
21499 /* Parse a __transaction_atomic or __transaction_relaxed statement
21500 (GCC Extension).
21501
21502 transaction-statement:
21503 __transaction_atomic transaction-attribute[opt] compound-statement
21504 __transaction_relaxed compound-statement
21505
21506 Note that the only valid attribute is: "outer".
21507 */
21508
21509 static tree
21510 c_parser_transaction (c_parser *parser, enum rid keyword)
21511 {
21512 unsigned int old_in = parser->in_transaction;
21513 unsigned int this_in = 1, new_in;
21514 location_t loc = c_parser_peek_token (parser)->location;
21515 tree stmt, attrs;
21516
21517 gcc_assert ((keyword == RID_TRANSACTION_ATOMIC
21518 || keyword == RID_TRANSACTION_RELAXED)
21519 && c_parser_next_token_is_keyword (parser, keyword));
21520 c_parser_consume_token (parser);
21521
21522 if (keyword == RID_TRANSACTION_RELAXED)
21523 this_in |= TM_STMT_ATTR_RELAXED;
21524 else
21525 {
21526 attrs = c_parser_transaction_attributes (parser);
21527 if (attrs)
21528 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
21529 }
21530
21531 /* Keep track if we're in the lexical scope of an outer transaction. */
21532 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
21533
21534 parser->in_transaction = new_in;
21535 stmt = c_parser_compound_statement (parser);
21536 parser->in_transaction = old_in;
21537
21538 if (flag_tm)
21539 stmt = c_finish_transaction (loc, stmt, this_in);
21540 else
21541 error_at (loc, (keyword == RID_TRANSACTION_ATOMIC ?
21542 "%<__transaction_atomic%> without transactional memory support enabled"
21543 : "%<__transaction_relaxed %> "
21544 "without transactional memory support enabled"));
21545
21546 return stmt;
21547 }
21548
21549 /* Parse a __transaction_atomic or __transaction_relaxed expression
21550 (GCC Extension).
21551
21552 transaction-expression:
21553 __transaction_atomic ( expression )
21554 __transaction_relaxed ( expression )
21555 */
21556
21557 static struct c_expr
21558 c_parser_transaction_expression (c_parser *parser, enum rid keyword)
21559 {
21560 struct c_expr ret;
21561 unsigned int old_in = parser->in_transaction;
21562 unsigned int this_in = 1;
21563 location_t loc = c_parser_peek_token (parser)->location;
21564 tree attrs;
21565
21566 gcc_assert ((keyword == RID_TRANSACTION_ATOMIC
21567 || keyword == RID_TRANSACTION_RELAXED)
21568 && c_parser_next_token_is_keyword (parser, keyword));
21569 c_parser_consume_token (parser);
21570
21571 if (keyword == RID_TRANSACTION_RELAXED)
21572 this_in |= TM_STMT_ATTR_RELAXED;
21573 else
21574 {
21575 attrs = c_parser_transaction_attributes (parser);
21576 if (attrs)
21577 this_in |= parse_tm_stmt_attr (attrs, 0);
21578 }
21579
21580 parser->in_transaction = this_in;
21581 matching_parens parens;
21582 if (parens.require_open (parser))
21583 {
21584 tree expr = c_parser_expression (parser).value;
21585 ret.original_type = TREE_TYPE (expr);
21586 ret.value = build1 (TRANSACTION_EXPR, ret.original_type, expr);
21587 if (this_in & TM_STMT_ATTR_RELAXED)
21588 TRANSACTION_EXPR_RELAXED (ret.value) = 1;
21589 SET_EXPR_LOCATION (ret.value, loc);
21590 ret.original_code = TRANSACTION_EXPR;
21591 if (!parens.require_close (parser))
21592 {
21593 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
21594 goto error;
21595 }
21596 }
21597 else
21598 {
21599 error:
21600 ret.set_error ();
21601 ret.original_code = ERROR_MARK;
21602 ret.original_type = NULL;
21603 }
21604 parser->in_transaction = old_in;
21605
21606 if (!flag_tm)
21607 error_at (loc, (keyword == RID_TRANSACTION_ATOMIC ?
21608 "%<__transaction_atomic%> without transactional memory support enabled"
21609 : "%<__transaction_relaxed %> "
21610 "without transactional memory support enabled"));
21611
21612 set_c_expr_source_range (&ret, loc, loc);
21613
21614 return ret;
21615 }
21616
21617 /* Parse a __transaction_cancel statement (GCC Extension).
21618
21619 transaction-cancel-statement:
21620 __transaction_cancel transaction-attribute[opt] ;
21621
21622 Note that the only valid attribute is "outer".
21623 */
21624
21625 static tree
21626 c_parser_transaction_cancel (c_parser *parser)
21627 {
21628 location_t loc = c_parser_peek_token (parser)->location;
21629 tree attrs;
21630 bool is_outer = false;
21631
21632 gcc_assert (c_parser_next_token_is_keyword (parser, RID_TRANSACTION_CANCEL));
21633 c_parser_consume_token (parser);
21634
21635 attrs = c_parser_transaction_attributes (parser);
21636 if (attrs)
21637 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
21638
21639 if (!flag_tm)
21640 {
21641 error_at (loc, "%<__transaction_cancel%> without "
21642 "transactional memory support enabled");
21643 goto ret_error;
21644 }
21645 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
21646 {
21647 error_at (loc, "%<__transaction_cancel%> within a "
21648 "%<__transaction_relaxed%>");
21649 goto ret_error;
21650 }
21651 else if (is_outer)
21652 {
21653 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
21654 && !is_tm_may_cancel_outer (current_function_decl))
21655 {
21656 error_at (loc, "outer %<__transaction_cancel%> not "
21657 "within outer %<__transaction_atomic%> or "
21658 "a %<transaction_may_cancel_outer%> function");
21659 goto ret_error;
21660 }
21661 }
21662 else if (parser->in_transaction == 0)
21663 {
21664 error_at (loc, "%<__transaction_cancel%> not within "
21665 "%<__transaction_atomic%>");
21666 goto ret_error;
21667 }
21668
21669 return add_stmt (build_tm_abort_call (loc, is_outer));
21670
21671 ret_error:
21672 return build1 (NOP_EXPR, void_type_node, error_mark_node);
21673 }
21674 \f
21675 /* Parse a single source file. */
21676
21677 void
21678 c_parse_file (void)
21679 {
21680 /* Use local storage to begin. If the first token is a pragma, parse it.
21681 If it is #pragma GCC pch_preprocess, then this will load a PCH file
21682 which will cause garbage collection. */
21683 c_parser tparser;
21684
21685 memset (&tparser, 0, sizeof tparser);
21686 tparser.translate_strings_p = true;
21687 tparser.tokens = &tparser.tokens_buf[0];
21688 the_parser = &tparser;
21689
21690 if (c_parser_peek_token (&tparser)->pragma_kind == PRAGMA_GCC_PCH_PREPROCESS)
21691 c_parser_pragma_pch_preprocess (&tparser);
21692 else
21693 c_common_no_more_pch ();
21694
21695 the_parser = ggc_alloc<c_parser> ();
21696 *the_parser = tparser;
21697 if (tparser.tokens == &tparser.tokens_buf[0])
21698 the_parser->tokens = &the_parser->tokens_buf[0];
21699
21700 /* Initialize EH, if we've been told to do so. */
21701 if (flag_exceptions)
21702 using_eh_for_cleanups ();
21703
21704 c_parser_translation_unit (the_parser);
21705 the_parser = NULL;
21706 }
21707
21708 /* Parse the body of a function declaration marked with "__RTL".
21709
21710 The RTL parser works on the level of characters read from a
21711 FILE *, whereas c_parser works at the level of tokens.
21712 Square this circle by consuming all of the tokens up to and
21713 including the closing brace, recording the start/end of the RTL
21714 fragment, and reopening the file and re-reading the relevant
21715 lines within the RTL parser.
21716
21717 This requires the opening and closing braces of the C function
21718 to be on separate lines from the RTL they wrap.
21719
21720 Take ownership of START_WITH_PASS, if non-NULL. */
21721
21722 void
21723 c_parser_parse_rtl_body (c_parser *parser, char *start_with_pass)
21724 {
21725 if (!c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
21726 {
21727 free (start_with_pass);
21728 return;
21729 }
21730
21731 location_t start_loc = c_parser_peek_token (parser)->location;
21732
21733 /* Consume all tokens, up to the closing brace, handling
21734 matching pairs of braces in the rtl dump. */
21735 int num_open_braces = 1;
21736 while (1)
21737 {
21738 switch (c_parser_peek_token (parser)->type)
21739 {
21740 case CPP_OPEN_BRACE:
21741 num_open_braces++;
21742 break;
21743 case CPP_CLOSE_BRACE:
21744 if (--num_open_braces == 0)
21745 goto found_closing_brace;
21746 break;
21747 case CPP_EOF:
21748 error_at (start_loc, "no closing brace");
21749 free (start_with_pass);
21750 return;
21751 default:
21752 break;
21753 }
21754 c_parser_consume_token (parser);
21755 }
21756
21757 found_closing_brace:
21758 /* At the closing brace; record its location. */
21759 location_t end_loc = c_parser_peek_token (parser)->location;
21760
21761 /* Consume the closing brace. */
21762 c_parser_consume_token (parser);
21763
21764 /* Invoke the RTL parser. */
21765 if (!read_rtl_function_body_from_file_range (start_loc, end_loc))
21766 {
21767 free (start_with_pass);
21768 return;
21769 }
21770
21771 /* Run the backend on the cfun created above, transferring ownership of
21772 START_WITH_PASS. */
21773 run_rtl_passes (start_with_pass);
21774 }
21775
21776 #include "gt-c-c-parser.h"