Debian Codegraph @

gcc/cp/semantics.cc

raw
1 /* Perform the semantic phase of parsing, i.e., the process of
2 building tree structure, checking semantic consistency, and
3 building RTL. These routines are used both during actual parsing
4 and during the instantiation of template functions.
5
6 Copyright (C) 1998-2025 Free Software Foundation, Inc.
7 Written by Mark Mitchell ([email protected]) based on code found
8 formerly in parse.y and pt.cc.
9
10 This file is part of GCC.
11
12 GCC is free software; you can redistribute it and/or modify it
13 under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 3, or (at your option)
15 any later version.
16
17 GCC is distributed in the hope that it will be useful, but
18 WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 General Public License for more details.
21
22 You should have received a copy of the GNU General Public License
23 along with GCC; see the file COPYING3. If not see
24 <http://www.gnu.org/licenses/>. */
25
26 #include "config.h"
27 #include "system.h"
28 #include "coretypes.h"
29 #include "target.h"
30 #include "bitmap.h"
31 #include "cp-tree.h"
32 #include "stringpool.h"
33 #include "cgraph.h"
34 #include "stmt.h"
35 #include "varasm.h"
36 #include "stor-layout.h"
37 #include "c-family/c-objc.h"
38 #include "tree-inline.h"
39 #include "intl.h"
40 #include "tree-iterator.h"
41 #include "omp-general.h"
42 #include "convert.h"
43 #include "stringpool.h"
44 #include "attribs.h"
45 #include "gomp-constants.h"
46 #include "predict.h"
47 #include "memmodel.h"
48
49 /* There routines provide a modular interface to perform many parsing
50 operations. They may therefore be used during actual parsing, or
51 during template instantiation, which may be regarded as a
52 degenerate form of parsing. */
53
54 static tree maybe_convert_cond (tree);
55 static tree finalize_nrv_r (tree *, int *, void *);
56
57 /* Used for OpenMP non-static data member privatization. */
58
59 static hash_map<tree, tree> *omp_private_member_map;
60 static vec<tree> omp_private_member_vec;
61 static bool omp_private_member_ignore_next;
62
63
64 /* Deferred Access Checking Overview
65 ---------------------------------
66
67 Most C++ expressions and declarations require access checking
68 to be performed during parsing. However, in several cases,
69 this has to be treated differently.
70
71 For member declarations, access checking has to be deferred
72 until more information about the declaration is known. For
73 example:
74
75 class A {
76 typedef int X;
77 public:
78 X f();
79 };
80
81 A::X A::f();
82 A::X g();
83
84 When we are parsing the function return type `A::X', we don't
85 really know if this is allowed until we parse the function name.
86
87 Furthermore, some contexts require that access checking is
88 never performed at all. These include class heads, and template
89 instantiations.
90
91 Typical use of access checking functions is described here:
92
93 1. When we enter a context that requires certain access checking
94 mode, the function `push_deferring_access_checks' is called with
95 DEFERRING argument specifying the desired mode. Access checking
96 may be performed immediately (dk_no_deferred), deferred
97 (dk_deferred), or not performed (dk_no_check).
98
99 2. When a declaration such as a type, or a variable, is encountered,
100 the function `perform_or_defer_access_check' is called. It
101 maintains a vector of all deferred checks.
102
103 3. The global `current_class_type' or `current_function_decl' is then
104 setup by the parser. `enforce_access' relies on these information
105 to check access.
106
107 4. Upon exiting the context mentioned in step 1,
108 `perform_deferred_access_checks' is called to check all declaration
109 stored in the vector. `pop_deferring_access_checks' is then
110 called to restore the previous access checking mode.
111
112 In case of parsing error, we simply call `pop_deferring_access_checks'
113 without `perform_deferred_access_checks'. */
114
115 struct GTY(()) deferred_access {
116 /* A vector representing name-lookups for which we have deferred
117 checking access controls. We cannot check the accessibility of
118 names used in a decl-specifier-seq until we know what is being
119 declared because code like:
120
121 class A {
122 class B {};
123 B* f();
124 }
125
126 A::B* A::f() { return 0; }
127
128 is valid, even though `A::B' is not generally accessible. */
129 vec<deferred_access_check, va_gc> *deferred_access_checks;
130
131 /* The current mode of access checks. */
132 enum deferring_kind deferring_access_checks_kind;
133 };
134
135 /* Data for deferred access checking. */
136 static GTY(()) vec<deferred_access, va_gc> *deferred_access_stack;
137 static GTY(()) unsigned deferred_access_no_check;
138
139 /* Save the current deferred access states and start deferred
140 access checking iff DEFER_P is true. */
141
142 void
143 push_deferring_access_checks (deferring_kind deferring)
144 {
145 /* For context like template instantiation, access checking
146 disabling applies to all nested context. */
147 if (deferred_access_no_check || deferring == dk_no_check)
148 deferred_access_no_check++;
149 else
150 {
151 deferred_access e = {NULL, deferring};
152 vec_safe_push (deferred_access_stack, e);
153 }
154 }
155
156 /* Save the current deferred access states and start deferred access
157 checking, continuing the set of deferred checks in CHECKS. */
158
159 void
160 reopen_deferring_access_checks (vec<deferred_access_check, va_gc> * checks)
161 {
162 push_deferring_access_checks (dk_deferred);
163 if (!deferred_access_no_check)
164 deferred_access_stack->last().deferred_access_checks = checks;
165 }
166
167 /* Resume deferring access checks again after we stopped doing
168 this previously. */
169
170 void
171 resume_deferring_access_checks (void)
172 {
173 if (!deferred_access_no_check)
174 deferred_access_stack->last().deferring_access_checks_kind = dk_deferred;
175 }
176
177 /* Stop deferring access checks. */
178
179 void
180 stop_deferring_access_checks (void)
181 {
182 if (!deferred_access_no_check)
183 deferred_access_stack->last().deferring_access_checks_kind = dk_no_deferred;
184 }
185
186 /* Discard the current deferred access checks and restore the
187 previous states. */
188
189 void
190 pop_deferring_access_checks (void)
191 {
192 if (deferred_access_no_check)
193 deferred_access_no_check--;
194 else
195 deferred_access_stack->pop ();
196 }
197
198 /* Returns a TREE_LIST representing the deferred checks.
199 The TREE_PURPOSE of each node is the type through which the
200 access occurred; the TREE_VALUE is the declaration named.
201 */
202
203 vec<deferred_access_check, va_gc> *
204 get_deferred_access_checks (void)
205 {
206 if (deferred_access_no_check)
207 return NULL;
208 else
209 return (deferred_access_stack->last().deferred_access_checks);
210 }
211
212 /* Take current deferred checks and combine with the
213 previous states if we also defer checks previously.
214 Otherwise perform checks now. */
215
216 void
217 pop_to_parent_deferring_access_checks (void)
218 {
219 if (deferred_access_no_check)
220 deferred_access_no_check--;
221 else
222 {
223 vec<deferred_access_check, va_gc> *checks;
224 deferred_access *ptr;
225
226 checks = (deferred_access_stack->last ().deferred_access_checks);
227
228 deferred_access_stack->pop ();
229 ptr = &deferred_access_stack->last ();
230 if (ptr->deferring_access_checks_kind == dk_no_deferred)
231 {
232 /* Check access. */
233 perform_access_checks (checks, tf_warning_or_error);
234 }
235 else
236 {
237 /* Merge with parent. */
238 int i, j;
239 deferred_access_check *chk, *probe;
240
241 FOR_EACH_VEC_SAFE_ELT (checks, i, chk)
242 {
243 FOR_EACH_VEC_SAFE_ELT (ptr->deferred_access_checks, j, probe)
244 {
245 if (probe->binfo == chk->binfo &&
246 probe->decl == chk->decl &&
247 probe->diag_decl == chk->diag_decl)
248 goto found;
249 }
250 /* Insert into parent's checks. */
251 vec_safe_push (ptr->deferred_access_checks, *chk);
252 found:;
253 }
254 }
255 }
256 }
257
258 /* Called from enforce_access. A class has attempted (but failed) to access
259 DECL. It is already established that a baseclass of that class,
260 PARENT_BINFO, has private access to DECL. Examine certain special cases
261 to find a decl that accurately describes the source of the problem. If
262 none of the special cases apply, simply return DECL as the source of the
263 problem. */
264
265 static tree
266 get_class_access_diagnostic_decl (tree parent_binfo, tree decl)
267 {
268 /* When a class is denied access to a decl in a baseclass, most of the
269 time it is because the decl itself was declared as private at the point
270 of declaration.
271
272 However, in C++, there are (at least) two situations in which a decl
273 can be private even though it was not originally defined as such.
274 These two situations only apply if a baseclass had private access to
275 DECL (this function is only called if that is the case). */
276
277 /* We should first check whether the reason the parent had private access
278 to DECL was simply because DECL was created and declared as private in
279 the parent. If it was, then DECL is definitively the source of the
280 problem. */
281 if (SAME_BINFO_TYPE_P (context_for_name_lookup (decl),
282 BINFO_TYPE (parent_binfo)))
283 return decl;
284
285 /* 1. If the "using" keyword is used to inherit DECL within the parent,
286 this may cause DECL to be private, so we should return the using
287 statement as the source of the problem.
288
289 Scan the fields of PARENT_BINFO and see if there are any using decls. If
290 there are, see if they inherit DECL. If they do, that's where DECL must
291 have been declared private. */
292
293 for (tree parent_field = TYPE_FIELDS (BINFO_TYPE (parent_binfo));
294 parent_field;
295 parent_field = DECL_CHAIN (parent_field))
296 /* Not necessary, but also check TREE_PRIVATE for the sake of
297 eliminating obviously non-relevant using decls. */
298 if (TREE_CODE (parent_field) == USING_DECL
299 && TREE_PRIVATE (parent_field))
300 {
301 tree decl_stripped = strip_using_decl (parent_field);
302
303 /* The using statement might be overloaded. If so, we need to
304 check all of the overloads. */
305 for (ovl_iterator iter (decl_stripped); iter; ++iter)
306 /* If equal, the using statement inherits DECL, and so is the
307 source of the access failure, so return it. */
308 if (*iter == decl)
309 return parent_field;
310 }
311
312 /* 2. If DECL was privately inherited by the parent class, then DECL will
313 be inaccessible, even though it may originally have been accessible to
314 deriving classes. In that case, the fault lies with the parent, since it
315 used a private inheritance, so we return the parent as the source of the
316 problem.
317
318 Since this is the last check, we just assume it's true. At worst, it
319 will simply point to the class that failed to give access, which is
320 technically true. */
321 return TYPE_NAME (BINFO_TYPE (parent_binfo));
322 }
323
324 /* If the current scope isn't allowed to access DECL along
325 BASETYPE_PATH, give an error, or if we're parsing a function or class
326 template, defer the access check to be performed at instantiation time.
327 The most derived class in BASETYPE_PATH is the one used to qualify DECL.
328 DIAG_DECL is the declaration to use in the error diagnostic. */
329
330 static bool
331 enforce_access (tree basetype_path, tree decl, tree diag_decl,
332 tsubst_flags_t complain, access_failure_info *afi = NULL)
333 {
334 gcc_assert (TREE_CODE (basetype_path) == TREE_BINFO);
335
336 if (flag_new_inheriting_ctors
337 && DECL_INHERITED_CTOR (decl))
338 {
339 /* 7.3.3/18: The additional constructors are accessible if they would be
340 accessible when used to construct an object of the corresponding base
341 class. */
342 decl = strip_inheriting_ctors (decl);
343 basetype_path = lookup_base (basetype_path, DECL_CONTEXT (decl),
344 ba_any, NULL, complain);
345 }
346
347 tree cs = current_scope ();
348 if (in_template_context
349 && (CLASS_TYPE_P (cs) || TREE_CODE (cs) == FUNCTION_DECL))
350 if (tree template_info = get_template_info (cs))
351 {
352 /* When parsing a function or class template, we in general need to
353 defer access checks until template instantiation time, since a friend
354 declaration may grant access only to a particular specialization of
355 the template. */
356
357 if (accessible_p (basetype_path, decl, /*consider_local_p=*/true))
358 /* But if the member is deemed accessible at parse time, then we can
359 assume it'll be accessible at instantiation time. */
360 return true;
361
362 /* Access of a dependent decl should be rechecked after tsubst'ing
363 into the user of the decl, rather than explicitly deferring the
364 check here. */
365 gcc_assert (!uses_template_parms (decl));
366 if (TREE_CODE (decl) == FIELD_DECL)
367 gcc_assert (!uses_template_parms (DECL_CONTEXT (decl)));
368
369 /* Defer this access check until instantiation time. */
370 deferred_access_check access_check;
371 access_check.binfo = basetype_path;
372 access_check.decl = decl;
373 access_check.diag_decl = diag_decl;
374 access_check.loc = input_location;
375 vec_safe_push (TI_DEFERRED_ACCESS_CHECKS (template_info), access_check);
376 return true;
377 }
378
379 if (!accessible_p (basetype_path, decl, /*consider_local_p=*/true))
380 {
381 if (flag_new_inheriting_ctors)
382 diag_decl = strip_inheriting_ctors (diag_decl);
383 if (complain & tf_error)
384 {
385 access_kind access_failure_reason = ak_none;
386
387 /* By default, using the decl as the source of the problem will
388 usually give correct results. */
389 tree diag_location = diag_decl;
390
391 /* However, if a parent of BASETYPE_PATH had private access to decl,
392 then it actually might be the case that the source of the problem
393 is not DECL. */
394 tree parent_binfo = get_parent_with_private_access (decl,
395 basetype_path);
396
397 /* So if a parent did have private access, then we need to do
398 special checks to obtain the best diagnostic location decl. */
399 if (parent_binfo != NULL_TREE)
400 {
401 diag_location = get_class_access_diagnostic_decl (parent_binfo,
402 diag_decl);
403
404 /* We also at this point know that the reason access failed was
405 because decl was private. */
406 access_failure_reason = ak_private;
407 }
408
409 /* Finally, generate an error message. */
410 complain_about_access (decl, diag_decl, diag_location, true,
411 access_failure_reason);
412 }
413 if (afi)
414 afi->record_access_failure (basetype_path, decl, diag_decl);
415 return false;
416 }
417
418 return true;
419 }
420
421 /* Perform the access checks in CHECKS. The TREE_PURPOSE of each node
422 is the BINFO indicating the qualifying scope used to access the
423 DECL node stored in the TREE_VALUE of the node. If CHECKS is empty
424 or we aren't in SFINAE context or all the checks succeed return TRUE,
425 otherwise FALSE. */
426
427 bool
428 perform_access_checks (vec<deferred_access_check, va_gc> *checks,
429 tsubst_flags_t complain)
430 {
431 int i;
432 deferred_access_check *chk;
433 location_t loc = input_location;
434 bool ok = true;
435
436 if (!checks)
437 return true;
438
439 FOR_EACH_VEC_SAFE_ELT (checks, i, chk)
440 {
441 input_location = chk->loc;
442 ok &= enforce_access (chk->binfo, chk->decl, chk->diag_decl, complain);
443 }
444
445 input_location = loc;
446 return (complain & tf_error) ? true : ok;
447 }
448
449 /* Perform the deferred access checks.
450
451 After performing the checks, we still have to keep the list
452 `deferred_access_stack->deferred_access_checks' since we may want
453 to check access for them again later in a different context.
454 For example:
455
456 class A {
457 typedef int X;
458 static X a;
459 };
460 A::X A::a, x; // No error for `A::a', error for `x'
461
462 We have to perform deferred access of `A::X', first with `A::a',
463 next with `x'. Return value like perform_access_checks above. */
464
465 bool
466 perform_deferred_access_checks (tsubst_flags_t complain)
467 {
468 return perform_access_checks (get_deferred_access_checks (), complain);
469 }
470
471 /* Defer checking the accessibility of DECL, when looked up in
472 BINFO. DIAG_DECL is the declaration to use to print diagnostics.
473 Return value like perform_access_checks above.
474 If non-NULL, report failures to AFI. */
475
476 bool
477 perform_or_defer_access_check (tree binfo, tree decl, tree diag_decl,
478 tsubst_flags_t complain,
479 access_failure_info *afi)
480 {
481 int i;
482 deferred_access *ptr;
483 deferred_access_check *chk;
484
485 /* Exit if we are in a context that no access checking is performed. */
486 if (deferred_access_no_check)
487 return true;
488
489 gcc_assert (TREE_CODE (binfo) == TREE_BINFO);
490
491 ptr = &deferred_access_stack->last ();
492
493 /* If we are not supposed to defer access checks, just check now. */
494 if (ptr->deferring_access_checks_kind == dk_no_deferred)
495 {
496 bool ok = enforce_access (binfo, decl, diag_decl, complain, afi);
497 return (complain & tf_error) ? true : ok;
498 }
499
500 /* See if we are already going to perform this check. */
501 FOR_EACH_VEC_SAFE_ELT (ptr->deferred_access_checks, i, chk)
502 {
503 if (chk->decl == decl && chk->binfo == binfo &&
504 chk->diag_decl == diag_decl)
505 {
506 return true;
507 }
508 }
509 /* If not, record the check. */
510 deferred_access_check new_access = {binfo, decl, diag_decl, input_location};
511 vec_safe_push (ptr->deferred_access_checks, new_access);
512
513 return true;
514 }
515
516 /* Returns nonzero if the current statement is a full expression,
517 i.e. temporaries created during that statement should be destroyed
518 at the end of the statement. */
519
520 int
521 stmts_are_full_exprs_p (void)
522 {
523 return current_stmt_tree ()->stmts_are_full_exprs_p;
524 }
525
526 /* T is a statement. Add it to the statement-tree. This is the C++
527 version. The C/ObjC frontends have a slightly different version of
528 this function. */
529
530 tree
531 add_stmt (tree t)
532 {
533 enum tree_code code = TREE_CODE (t);
534
535 if (EXPR_P (t) && code != LABEL_EXPR)
536 {
537 if (!EXPR_HAS_LOCATION (t))
538 SET_EXPR_LOCATION (t, input_location);
539
540 /* When we expand a statement-tree, we must know whether or not the
541 statements are full-expressions. We record that fact here. */
542 if (STATEMENT_CODE_P (TREE_CODE (t)))
543 STMT_IS_FULL_EXPR_P (t) = stmts_are_full_exprs_p ();
544 }
545
546 if (code == LABEL_EXPR || code == CASE_LABEL_EXPR)
547 STATEMENT_LIST_HAS_LABEL (cur_stmt_list) = 1;
548
549 /* Add T to the statement-tree. Non-side-effect statements need to be
550 recorded during statement expressions. */
551 gcc_checking_assert (!stmt_list_stack->is_empty ());
552 append_to_statement_list_force (t, &cur_stmt_list);
553
554 return t;
555 }
556
557 /* Returns the stmt_tree to which statements are currently being added. */
558
559 stmt_tree
560 current_stmt_tree (void)
561 {
562 return (cfun
563 ? &cfun->language->base.x_stmt_tree
564 : &scope_chain->x_stmt_tree);
565 }
566
567 /* If statements are full expressions, wrap STMT in a CLEANUP_POINT_EXPR. */
568
569 static tree
570 maybe_cleanup_point_expr (tree expr)
571 {
572 if (!processing_template_decl && stmts_are_full_exprs_p ())
573 expr = fold_build_cleanup_point_expr (TREE_TYPE (expr), expr);
574 return expr;
575 }
576
577 /* Like maybe_cleanup_point_expr except have the type of the new expression be
578 void so we don't need to create a temporary variable to hold the inner
579 expression. The reason why we do this is because the original type might be
580 an aggregate and we cannot create a temporary variable for that type. */
581
582 tree
583 maybe_cleanup_point_expr_void (tree expr)
584 {
585 if (!processing_template_decl && stmts_are_full_exprs_p ())
586 expr = fold_build_cleanup_point_expr (void_type_node, expr);
587 return expr;
588 }
589
590
591
592 /* Create a declaration statement for the declaration given by the DECL. */
593
594 void
595 add_decl_expr (tree decl)
596 {
597 tree r = build_stmt (DECL_SOURCE_LOCATION (decl), DECL_EXPR, decl);
598 if (DECL_INITIAL (decl)
599 || (DECL_SIZE (decl) && TREE_SIDE_EFFECTS (DECL_SIZE (decl))))
600 r = maybe_cleanup_point_expr_void (r);
601 add_stmt (r);
602 }
603
604 /* Set EXPR_LOCATION on one cleanup T to LOC. */
605
606 static void
607 set_one_cleanup_loc (tree t, location_t loc)
608 {
609 if (!t)
610 return;
611 if (TREE_CODE (t) != POSTCONDITION_STMT)
612 protected_set_expr_location (t, loc);
613 /* Avoid locus differences for C++ cdtor calls depending on whether
614 cdtor_returns_this: a conversion to void is added to discard the return
615 value, and this conversion ends up carrying the location, and when it
616 gets discarded, the location is lost. So hold it in the call as well. */
617 if (TREE_CODE (t) == NOP_EXPR
618 && TREE_TYPE (t) == void_type_node
619 && TREE_CODE (TREE_OPERAND (t, 0)) == CALL_EXPR)
620 protected_set_expr_location (TREE_OPERAND (t, 0), loc);
621 }
622
623 /* Set EXPR_LOCATION of the cleanups of any CLEANUP_STMT in STMTS to LOC. */
624
625 static void
626 set_cleanup_locs (tree stmts, location_t loc)
627 {
628 if (TREE_CODE (stmts) == CLEANUP_STMT)
629 {
630 set_one_cleanup_loc (CLEANUP_EXPR (stmts), loc);
631 set_cleanup_locs (CLEANUP_BODY (stmts), loc);
632 }
633 else if (TREE_CODE (stmts) == STATEMENT_LIST)
634 for (tree stmt : tsi_range (stmts))
635 set_cleanup_locs (stmt, loc);
636 }
637
638 /* True iff the innermost block scope is a try block. */
639
640 static bool
641 at_try_scope ()
642 {
643 cp_binding_level *b = current_binding_level;
644 while (b && b->kind == sk_cleanup)
645 b = b->level_chain;
646 return b && b->kind == sk_try;
647 }
648
649 /* Finish a scope. */
650
651 tree
652 do_poplevel (tree stmt_list)
653 {
654 tree block = NULL;
655
656 bool was_try = at_try_scope ();
657
658 if (stmts_are_full_exprs_p ())
659 block = poplevel (kept_level_p (), 1, 0);
660
661 /* This needs to come after poplevel merges sk_cleanup statement_lists. */
662 maybe_splice_retval_cleanup (stmt_list, was_try);
663
664 stmt_list = pop_stmt_list (stmt_list);
665
666 /* input_location is the last token of the scope, usually a }. */
667 set_cleanup_locs (stmt_list, input_location);
668
669 if (!processing_template_decl)
670 {
671 stmt_list = c_build_bind_expr (input_location, block, stmt_list);
672 /* ??? See c_end_compound_stmt re statement expressions. */
673 }
674
675 return stmt_list;
676 }
677
678 /* Begin a new scope. */
679
680 static tree
681 do_pushlevel (scope_kind sk)
682 {
683 tree ret = push_stmt_list ();
684 if (stmts_are_full_exprs_p ())
685 begin_scope (sk, NULL);
686 return ret;
687 }
688
689 /* Queue a cleanup. CLEANUP is an expression/statement to be executed
690 when the current scope is exited. EH_ONLY is true when this is not
691 meant to apply to normal control flow transfer. DECL is the VAR_DECL
692 being cleaned up, if any, or null for temporaries or subobjects. */
693
694 void
695 push_cleanup (tree decl, tree cleanup, bool eh_only)
696 {
697 tree stmt = build_stmt (input_location, CLEANUP_STMT, NULL, cleanup, decl);
698 CLEANUP_EH_ONLY (stmt) = eh_only;
699 add_stmt (stmt);
700 CLEANUP_BODY (stmt) = push_stmt_list ();
701 }
702
703 /* Simple infinite loop tracking for -Wreturn-type. We keep a stack of all
704 the current loops, represented by 'NULL_TREE' if we've seen a possible
705 exit, and 'error_mark_node' if not. This is currently used only to
706 suppress the warning about a function with no return statements, and
707 therefore we don't bother noting returns as possible exits. We also
708 don't bother with gotos. */
709
710 static void
711 begin_maybe_infinite_loop (tree cond)
712 {
713 /* Only track this while parsing a function, not during instantiation. */
714 if (!cfun || (DECL_TEMPLATE_INSTANTIATION (current_function_decl)
715 && !processing_template_decl))
716 return;
717 bool maybe_infinite = true;
718 if (cond)
719 {
720 cond = fold_non_dependent_expr (cond);
721 maybe_infinite = integer_nonzerop (cond);
722 }
723 vec_safe_push (cp_function_chain->infinite_loops,
724 maybe_infinite ? error_mark_node : NULL_TREE);
725
726 }
727
728 /* A break is a possible exit for the current loop. */
729
730 void
731 break_maybe_infinite_loop (void)
732 {
733 if (!cfun)
734 return;
735 cp_function_chain->infinite_loops->last() = NULL_TREE;
736 }
737
738 /* If we reach the end of the loop without seeing a possible exit, we have
739 an infinite loop. */
740
741 static void
742 end_maybe_infinite_loop (tree cond)
743 {
744 if (!cfun || (DECL_TEMPLATE_INSTANTIATION (current_function_decl)
745 && !processing_template_decl))
746 return;
747 tree current = cp_function_chain->infinite_loops->pop();
748 if (current != NULL_TREE)
749 {
750 cond = fold_non_dependent_expr (cond);
751 if (integer_nonzerop (cond))
752 current_function_infinite_loop = 1;
753 }
754 }
755
756 /* Begin a conditional that might contain a declaration. When generating
757 normal code, we want the declaration to appear before the statement
758 containing the conditional. When generating template code, we want the
759 conditional to be rendered as the raw DECL_EXPR. */
760
761 static void
762 begin_cond (tree *cond_p)
763 {
764 if (processing_template_decl)
765 *cond_p = push_stmt_list ();
766 }
767
768 /* Finish such a conditional. */
769
770 static void
771 finish_cond (tree *cond_p, tree expr)
772 {
773 if (processing_template_decl)
774 {
775 tree cond = pop_stmt_list (*cond_p);
776
777 if (expr == NULL_TREE)
778 /* Empty condition in 'for'. */
779 gcc_assert (empty_expr_stmt_p (cond));
780 else if (check_for_bare_parameter_packs (expr))
781 expr = error_mark_node;
782 else if (!empty_expr_stmt_p (cond))
783 expr = build2 (COMPOUND_EXPR, TREE_TYPE (expr), cond, expr);
784 }
785 *cond_p = expr;
786 }
787
788 /* If loop condition specifies a conditional with a declaration,
789 such as
790 while (A x = 42) { }
791 for (; A x = 42;) { }
792 move the *BODY_P statements as a BIND_EXPR into {FOR,WHILE}_COND_PREP
793 and if there are any CLEANUP_STMT at the end, remember their count in
794 {FOR,WHILE}_COND_CLEANUP.
795 genericize_c_loop will then handle it appropriately. In particular,
796 the {FOR,WHILE}_COND, {FOR,WHILE}_BODY, if used continue label and
797 FOR_EXPR will be appended into the {FOR,WHILE}_COND_PREP BIND_EXPR,
798 but it can't be done too early because only the actual body should
799 bind BREAK_STMT and CONTINUE_STMT to the inner loop.
800 The statement list for *BODY will be empty if the conditional did
801 not declare anything. */
802
803 static void
804 adjust_loop_decl_cond (tree *body_p, tree *prep_p, tree *cleanup_p)
805 {
806 if (!TREE_SIDE_EFFECTS (*body_p))
807 return;
808
809 gcc_assert (!processing_template_decl);
810 *prep_p = *body_p;
811 if (*prep_p != cur_stmt_list)
812 {
813 /* There can be just one CLEANUP_STMT, or there could be multiple
814 nested CLEANUP_STMTs, e.g. for structured bindings used as
815 condition. */
816 gcc_assert (stmt_list_stack->length () > 1);
817 for (unsigned i = stmt_list_stack->length () - 2; ; --i)
818 {
819 tree t = (*stmt_list_stack)[i];
820 tree_stmt_iterator last = tsi_last (t);
821 gcc_assert (tsi_one_before_end_p (last)
822 && TREE_CODE (tsi_stmt (last)) == CLEANUP_STMT
823 && (CLEANUP_BODY (tsi_stmt (last))
824 == (*stmt_list_stack)[i + 1])
825 && !CLEANUP_EH_ONLY (tsi_stmt (last)));
826 if (t == *prep_p)
827 {
828 *cleanup_p = build_int_cst (long_unsigned_type_node,
829 stmt_list_stack->length () - 1 - i);
830 break;
831 }
832 gcc_assert (i >= 1);
833 }
834 }
835 current_binding_level->keep = true;
836 tree_stmt_iterator iter = tsi_last (cur_stmt_list);
837 /* Temporarily store in {FOR,WHILE}_BODY the last statement of
838 the innnermost statement list or NULL if it has no statement.
839 This is used in finish_loop_cond_prep to find out the splitting
840 point and then {FOR,WHILE}_BODY will be changed to the actual
841 body. */
842 if (tsi_end_p (iter))
843 *body_p = NULL_TREE;
844 else
845 *body_p = tsi_stmt (iter);
846 }
847
848 /* Finalize {FOR,WHILE}_{BODY,COND_PREP} after the loop body.
849 The above function initialized *BODY_P to the last statement
850 in *PREP_P at that point.
851 Call do_poplevel on *PREP_P and move everything after that
852 former last statement into *BODY_P. genericize_c_loop
853 will later put those parts back together.
854 CLEANUP is {FOR,WHILE}_COND_CLEANUP. */
855
856 static void
857 finish_loop_cond_prep (tree *body_p, tree *prep_p, tree cleanup)
858 {
859 *prep_p = do_poplevel (*prep_p);
860 gcc_assert (TREE_CODE (*prep_p) == BIND_EXPR);
861 if (BIND_EXPR_BODY (*prep_p) == *body_p)
862 {
863 gcc_assert (cleanup == NULL_TREE);
864 *body_p = build_empty_stmt (input_location);
865 return;
866 }
867 tree stmt_list = BIND_EXPR_BODY (*prep_p);
868 gcc_assert (TREE_CODE (stmt_list) == STATEMENT_LIST);
869 if (cleanup)
870 {
871 tree_stmt_iterator iter = tsi_last (stmt_list);
872 gcc_assert (TREE_CODE (tsi_stmt (iter)) == CLEANUP_STMT);
873 for (unsigned depth = tree_to_uhwi (cleanup); depth > 1; --depth)
874 {
875 gcc_assert (TREE_CODE (CLEANUP_BODY (tsi_stmt (iter)))
876 == STATEMENT_LIST);
877 iter = tsi_last (CLEANUP_BODY (tsi_stmt (iter)));
878 gcc_assert (TREE_CODE (tsi_stmt (iter)) == CLEANUP_STMT);
879 }
880 if (*body_p == NULL_TREE)
881 {
882 *body_p = CLEANUP_BODY (tsi_stmt (iter));
883 CLEANUP_BODY (tsi_stmt (iter)) = build_empty_stmt (input_location);
884 return;
885 }
886 stmt_list = CLEANUP_BODY (tsi_stmt (iter));
887 }
888 tree_stmt_iterator iter = tsi_start (stmt_list);
889 while (tsi_stmt (iter) != *body_p)
890 tsi_next (&iter);
891 *body_p = tsi_split_stmt_list (input_location, iter);
892 }
893
894 /* Finish a goto-statement. */
895
896 tree
897 finish_goto_stmt (tree destination)
898 {
899 if (identifier_p (destination))
900 destination = lookup_label (destination);
901
902 /* We warn about unused labels with -Wunused. That means we have to
903 mark the used labels as used. */
904 if (TREE_CODE (destination) == LABEL_DECL)
905 TREE_USED (destination) = 1;
906 else
907 {
908 destination = mark_rvalue_use (destination);
909 if (!processing_template_decl)
910 {
911 destination = cp_convert (ptr_type_node, destination,
912 tf_warning_or_error);
913 if (error_operand_p (destination))
914 return NULL_TREE;
915 destination
916 = fold_build_cleanup_point_expr (TREE_TYPE (destination),
917 destination);
918 }
919 }
920
921 check_goto (destination);
922
923 add_stmt (build_predict_expr (PRED_GOTO, NOT_TAKEN));
924 return add_stmt (build_stmt (input_location, GOTO_EXPR, destination));
925 }
926
927 /* Returns true if T corresponds to an assignment operator expression. */
928
929 static bool
930 is_assignment_op_expr_p (tree t)
931 {
932 if (t == NULL_TREE)
933 return false;
934
935 if (TREE_CODE (t) == MODIFY_EXPR
936 || (TREE_CODE (t) == MODOP_EXPR
937 && TREE_CODE (TREE_OPERAND (t, 1)) == NOP_EXPR))
938 return true;
939
940 tree call = extract_call_expr (t);
941 if (call == NULL_TREE
942 || call == error_mark_node
943 || !CALL_EXPR_OPERATOR_SYNTAX (call))
944 return false;
945
946 tree fndecl = cp_get_callee_fndecl_nofold (call);
947 return fndecl != NULL_TREE
948 && DECL_ASSIGNMENT_OPERATOR_P (fndecl)
949 && DECL_OVERLOADED_OPERATOR_IS (fndecl, NOP_EXPR);
950 }
951
952 /* Return true if TYPE is a class type that is convertible to
953 and assignable from bool. */
954
955 static GTY((deletable)) hash_map<tree, bool> *boolish_class_type_p_cache;
956
957 static bool
958 boolish_class_type_p (tree type)
959 {
960 type = TYPE_MAIN_VARIANT (type);
961 if (!CLASS_TYPE_P (type) || !COMPLETE_TYPE_P (type))
962 return false;
963
964 if (bool *r = hash_map_safe_get (boolish_class_type_p_cache, type))
965 return *r;
966
967 tree ops;
968 bool has_bool_assignment = false;
969 bool has_bool_conversion = false;
970
971 ops = lookup_fnfields (type, assign_op_identifier, /*protect=*/0, tf_none);
972 for (tree op : ovl_range (BASELINK_FUNCTIONS (ops)))
973 {
974 op = STRIP_TEMPLATE (op);
975 if (TREE_CODE (op) != FUNCTION_DECL)
976 continue;
977 tree parm = DECL_CHAIN (DECL_ARGUMENTS (op));
978 tree parm_type = non_reference (TREE_TYPE (parm));
979 if (TREE_CODE (parm_type) == BOOLEAN_TYPE)
980 {
981 has_bool_assignment = true;
982 break;
983 }
984 }
985
986 if (has_bool_assignment)
987 {
988 ops = lookup_conversions (type);
989 for (; ops; ops = TREE_CHAIN (ops))
990 {
991 tree op = TREE_VALUE (ops);
992 if (!DECL_NONCONVERTING_P (op)
993 && TREE_CODE (DECL_CONV_FN_TYPE (op)) == BOOLEAN_TYPE)
994 {
995 has_bool_conversion = true;
996 break;
997 }
998 }
999 }
1000
1001 bool boolish = has_bool_assignment && has_bool_conversion;
1002 hash_map_safe_put<true> (boolish_class_type_p_cache, type, boolish);
1003 return boolish;
1004 }
1005
1006
1007 /* Maybe warn about an unparenthesized 'a = b' (appearing in a
1008 boolean context where 'a == b' might have been intended).
1009 NESTED_P is true if T is the RHS of another assignment. */
1010
1011 void
1012 maybe_warn_unparenthesized_assignment (tree t, bool nested_p,
1013 tsubst_flags_t complain)
1014 {
1015 tree type = TREE_TYPE (t);
1016 t = STRIP_REFERENCE_REF (t);
1017
1018 if ((complain & tf_warning)
1019 && warn_parentheses
1020 && is_assignment_op_expr_p (t)
1021 /* A parenthesized expression would've had this warning
1022 suppressed by finish_parenthesized_expr. */
1023 && !warning_suppressed_p (t, OPT_Wparentheses)
1024 /* In c = a = b, don't warn if a has type bool or bool-like class. */
1025 && (!nested_p
1026 || (TREE_CODE (type) != BOOLEAN_TYPE
1027 && !boolish_class_type_p (type))))
1028 {
1029 warning_at (cp_expr_loc_or_input_loc (t), OPT_Wparentheses,
1030 "suggest parentheses around assignment used as truth value");
1031 suppress_warning (t, OPT_Wparentheses);
1032 }
1033 }
1034
1035 /* Helper class for saving/restoring ANNOTATE_EXPRs. For a tree node t, users
1036 can construct one of these like so:
1037
1038 annotate_saver s (&t);
1039
1040 and t will be updated to have any annotations removed. The user can then
1041 transform t, and later restore the ANNOTATE_EXPRs with:
1042
1043 t = s.restore (t).
1044
1045 The intent is to ensure that any ANNOTATE_EXPRs remain the outermost
1046 expressions following any operations on t. */
1047
1048 class annotate_saver {
1049 /* The chain of saved annotations, if there were any. Otherwise null. */
1050 tree m_annotations;
1051
1052 /* If M_ANNOTATIONS is non-null, then M_INNER points to TREE_OPERAND (A, 0)
1053 for the innermost annotation A. */
1054 tree *m_inner;
1055
1056 public:
1057 annotate_saver (tree *);
1058 tree restore (tree);
1059 };
1060
1061 /* If *COND is an ANNOTATE_EXPR, walk through the chain of annotations, and set
1062 *COND equal to the first non-ANNOTATE_EXPR (saving a pointer to the
1063 original chain of annotations for later use in restore). */
1064
1065 annotate_saver::annotate_saver (tree *cond) : m_annotations (nullptr)
1066 {
1067 tree *t = cond;
1068 while (TREE_CODE (*t) == ANNOTATE_EXPR)
1069 t = &TREE_OPERAND (*t, 0);
1070
1071 if (t != cond)
1072 {
1073 m_annotations = *cond;
1074 *cond = *t;
1075 m_inner = t;
1076 }
1077 }
1078
1079 /* If we didn't strip any annotations on construction, return NEW_INNER
1080 unmodified. Otherwise, wrap the saved annotations around NEW_INNER (updating
1081 the types and flags of the annotations if needed) and return the resulting
1082 expression. */
1083
1084 tree
1085 annotate_saver::restore (tree new_inner)
1086 {
1087 if (!m_annotations)
1088 return new_inner;
1089
1090 /* If the type of the inner expression changed, we need to update the types
1091 of all the ANNOTATE_EXPRs. We may need to update the flags too, but we
1092 assume they only change if the type of the inner expression changes.
1093 The flag update logic assumes that the other operands to the
1094 ANNOTATE_EXPRs are always INTEGER_CSTs. */
1095 if (TREE_TYPE (new_inner) != TREE_TYPE (*m_inner))
1096 {
1097 const bool new_readonly
1098 = TREE_READONLY (new_inner) || CONSTANT_CLASS_P (new_inner);
1099
1100 for (tree c = m_annotations; c != *m_inner; c = TREE_OPERAND (c, 0))
1101 {
1102 gcc_checking_assert (TREE_CODE (c) == ANNOTATE_EXPR
1103 && TREE_CODE (TREE_OPERAND (c, 1)) == INTEGER_CST
1104 && TREE_CODE (TREE_OPERAND (c, 2)) == INTEGER_CST);
1105 TREE_TYPE (c) = TREE_TYPE (new_inner);
1106 TREE_SIDE_EFFECTS (c) = TREE_SIDE_EFFECTS (new_inner);
1107 TREE_READONLY (c) = new_readonly;
1108 }
1109 }
1110
1111 *m_inner = new_inner;
1112 return m_annotations;
1113 }
1114
1115 /* COND is the condition-expression for an if, while, etc.,
1116 statement. Convert it to a boolean value, if appropriate.
1117 In addition, verify sequence points if -Wsequence-point is enabled. */
1118
1119 static tree
1120 maybe_convert_cond (tree cond)
1121 {
1122 /* Empty conditions remain empty. */
1123 if (!cond)
1124 return NULL_TREE;
1125
1126 /* Wait until we instantiate templates before doing conversion. */
1127 if (type_dependent_expression_p (cond))
1128 return cond;
1129
1130 /* Strip any ANNOTATE_EXPRs from COND. */
1131 annotate_saver annotations (&cond);
1132
1133 /* For structured binding used in condition, the conversion needs to be
1134 evaluated before the individual variables are initialized in the
1135 std::tuple_{size,elemenet} case. cp_finish_decomp saved the conversion
1136 result in a TARGET_EXPR, pick it up from there. */
1137 if (DECL_DECOMPOSITION_P (cond)
1138 && DECL_DECOMP_IS_BASE (cond)
1139 && DECL_DECOMP_BASE (cond)
1140 && TREE_CODE (DECL_DECOMP_BASE (cond)) == TARGET_EXPR)
1141 cond = TARGET_EXPR_SLOT (DECL_DECOMP_BASE (cond));
1142
1143 if (warn_sequence_point && !processing_template_decl)
1144 verify_sequence_points (cond);
1145
1146 maybe_warn_unparenthesized_assignment (cond, /*nested_p=*/false,
1147 tf_warning_or_error);
1148
1149 /* Do the conversion. */
1150 cond = convert_from_reference (cond);
1151 cond = condition_conversion (cond);
1152
1153 /* Restore any ANNOTATE_EXPRs around COND. */
1154 return annotations.restore (cond);
1155 }
1156
1157 /* Finish an expression-statement, whose EXPRESSION is as indicated. */
1158
1159 tree
1160 finish_expr_stmt (tree expr)
1161 {
1162 tree r = NULL_TREE;
1163 location_t loc = EXPR_LOCATION (expr);
1164
1165 if (expr != NULL_TREE)
1166 {
1167 /* If we ran into a problem, make sure we complained. */
1168 gcc_assert (expr != error_mark_node || seen_error ());
1169
1170 if (!processing_template_decl)
1171 {
1172 if (warn_sequence_point)
1173 verify_sequence_points (expr);
1174 expr = convert_to_void (expr, ICV_STATEMENT, tf_warning_or_error);
1175 }
1176 else if (!type_dependent_expression_p (expr))
1177 convert_to_void (expr, ICV_STATEMENT, tf_warning_or_error);
1178
1179 if (check_for_bare_parameter_packs (expr))
1180 expr = error_mark_node;
1181
1182 /* Simplification of inner statement expressions, compound exprs,
1183 etc can result in us already having an EXPR_STMT. */
1184 if (TREE_CODE (expr) != CLEANUP_POINT_EXPR)
1185 {
1186 if (TREE_CODE (expr) != EXPR_STMT)
1187 expr = build_stmt (loc, EXPR_STMT, expr);
1188 expr = maybe_cleanup_point_expr_void (expr);
1189 }
1190
1191 r = add_stmt (expr);
1192 }
1193
1194 return r;
1195 }
1196
1197
1198 /* Begin an if-statement. Returns a newly created IF_STMT if
1199 appropriate. */
1200
1201 tree
1202 begin_if_stmt (void)
1203 {
1204 tree r, scope;
1205 scope = do_pushlevel (sk_cond);
1206 r = build_stmt (input_location, IF_STMT, NULL_TREE,
1207 NULL_TREE, NULL_TREE, scope);
1208 current_binding_level->this_entity = r;
1209 begin_cond (&IF_COND (r));
1210 return r;
1211 }
1212
1213 /* Returns true if FN, a CALL_EXPR, is a call to
1214 std::is_constant_evaluated or __builtin_is_constant_evaluated. */
1215
1216 static bool
1217 is_std_constant_evaluated_p (tree fn)
1218 {
1219 /* std::is_constant_evaluated takes no arguments. */
1220 if (call_expr_nargs (fn) != 0)
1221 return false;
1222
1223 tree fndecl = cp_get_callee_fndecl_nofold (fn);
1224 if (fndecl == NULL_TREE)
1225 return false;
1226
1227 if (fndecl_built_in_p (fndecl, CP_BUILT_IN_IS_CONSTANT_EVALUATED,
1228 BUILT_IN_FRONTEND))
1229 return true;
1230
1231 if (!decl_in_std_namespace_p (fndecl))
1232 return false;
1233
1234 tree name = DECL_NAME (fndecl);
1235 return name && id_equal (name, "is_constant_evaluated");
1236 }
1237
1238 /* Callback function for maybe_warn_for_constant_evaluated that looks
1239 for calls to std::is_constant_evaluated in TP. */
1240
1241 static tree
1242 find_std_constant_evaluated_r (tree *tp, int *walk_subtrees, void *)
1243 {
1244 tree t = *tp;
1245
1246 if (TYPE_P (t) || TREE_CONSTANT (t))
1247 {
1248 *walk_subtrees = false;
1249 return NULL_TREE;
1250 }
1251
1252 switch (TREE_CODE (t))
1253 {
1254 case CALL_EXPR:
1255 if (is_std_constant_evaluated_p (t))
1256 return t;
1257 break;
1258 case EXPR_STMT:
1259 /* Don't warn in statement expressions. */
1260 *walk_subtrees = false;
1261 return NULL_TREE;
1262 default:
1263 break;
1264 }
1265
1266 return NULL_TREE;
1267 }
1268
1269 /* In certain contexts, std::is_constant_evaluated() is always true (for
1270 instance, in a consteval function or in a constexpr if), or always false
1271 (e.g., in a non-constexpr non-consteval function) so give the user a clue. */
1272
1273 static void
1274 maybe_warn_for_constant_evaluated (tree cond, bool constexpr_if,
1275 bool trivial_infinite)
1276 {
1277 if (!warn_tautological_compare)
1278 return;
1279
1280 /* Suppress warning for std::is_constant_evaluated if the conditional
1281 comes from a macro. */
1282 if (from_macro_expansion_at (EXPR_LOCATION (cond)))
1283 return;
1284
1285 cond = cp_walk_tree_without_duplicates (&cond, find_std_constant_evaluated_r,
1286 NULL);
1287 if (cond)
1288 {
1289 if (constexpr_if)
1290 warning_at (EXPR_LOCATION (cond), OPT_Wtautological_compare,
1291 "%<std::is_constant_evaluated%> always evaluates to "
1292 "true in %<if constexpr%>");
1293 else if (trivial_infinite)
1294 {
1295 auto_diagnostic_group d;
1296 if (warning_at (EXPR_LOCATION (cond), OPT_Wtautological_compare,
1297 "%<std::is_constant_evaluated%> evaluates to "
1298 "true when checking if trivially empty iteration "
1299 "statement is trivial infinite loop")
1300 && !maybe_constexpr_fn (current_function_decl))
1301 inform (EXPR_LOCATION (cond),
1302 "and evaluates to false when actually evaluating "
1303 "the condition in non-%<constexpr%> function");
1304 }
1305 else if (!maybe_constexpr_fn (current_function_decl))
1306 warning_at (EXPR_LOCATION (cond), OPT_Wtautological_compare,
1307 "%<std::is_constant_evaluated%> always evaluates to "
1308 "false in a non-%<constexpr%> function");
1309 else if (DECL_IMMEDIATE_FUNCTION_P (current_function_decl))
1310 warning_at (EXPR_LOCATION (cond), OPT_Wtautological_compare,
1311 "%<std::is_constant_evaluated%> always evaluates to "
1312 "true in a %<consteval%> function");
1313 }
1314 }
1315
1316 /* Process the COND of an if-statement, which may be given by
1317 IF_STMT. */
1318
1319 tree
1320 finish_if_stmt_cond (tree orig_cond, tree if_stmt)
1321 {
1322 tree cond = maybe_convert_cond (orig_cond);
1323 maybe_warn_for_constant_evaluated (cond, IF_STMT_CONSTEXPR_P (if_stmt),
1324 /*trivial_infinite=*/false);
1325 if (IF_STMT_CONSTEXPR_P (if_stmt)
1326 && !type_dependent_expression_p (cond)
1327 && require_constant_expression (cond)
1328 && !instantiation_dependent_expression_p (cond)
1329 /* Wait until instantiation time, since only then COND has been
1330 converted to bool. */
1331 && TYPE_MAIN_VARIANT (TREE_TYPE (cond)) == boolean_type_node)
1332 {
1333 cond = instantiate_non_dependent_expr (cond);
1334 cond = cxx_constant_value (cond);
1335 }
1336 else if (processing_template_decl)
1337 cond = orig_cond;
1338 finish_cond (&IF_COND (if_stmt), cond);
1339 add_stmt (if_stmt);
1340 THEN_CLAUSE (if_stmt) = push_stmt_list ();
1341 return cond;
1342 }
1343
1344 /* Finish the then-clause of an if-statement, which may be given by
1345 IF_STMT. */
1346
1347 tree
1348 finish_then_clause (tree if_stmt)
1349 {
1350 THEN_CLAUSE (if_stmt) = pop_stmt_list (THEN_CLAUSE (if_stmt));
1351 return if_stmt;
1352 }
1353
1354 /* Begin the else-clause of an if-statement. */
1355
1356 void
1357 begin_else_clause (tree if_stmt)
1358 {
1359 ELSE_CLAUSE (if_stmt) = push_stmt_list ();
1360 }
1361
1362 /* Finish the else-clause of an if-statement, which may be given by
1363 IF_STMT. */
1364
1365 void
1366 finish_else_clause (tree if_stmt)
1367 {
1368 ELSE_CLAUSE (if_stmt) = pop_stmt_list (ELSE_CLAUSE (if_stmt));
1369 }
1370
1371 /* Callback for cp_walk_tree to mark all {VAR,PARM}_DECLs in a tree as
1372 read. */
1373
1374 static tree
1375 maybe_mark_exp_read_r (tree *tp, int *, void *)
1376 {
1377 tree t = *tp;
1378 if (VAR_P (t) || TREE_CODE (t) == PARM_DECL)
1379 mark_exp_read (t);
1380 return NULL_TREE;
1381 }
1382
1383 /* Finish an if-statement. */
1384
1385 void
1386 finish_if_stmt (tree if_stmt)
1387 {
1388 tree scope = IF_SCOPE (if_stmt);
1389 IF_SCOPE (if_stmt) = NULL;
1390 if (IF_STMT_CONSTEXPR_P (if_stmt))
1391 {
1392 /* Prevent various -Wunused warnings. We might not instantiate
1393 either of these branches, so we would not mark the variables
1394 used in that branch as read. */
1395 cp_walk_tree_without_duplicates (&THEN_CLAUSE (if_stmt),
1396 maybe_mark_exp_read_r, NULL);
1397 cp_walk_tree_without_duplicates (&ELSE_CLAUSE (if_stmt),
1398 maybe_mark_exp_read_r, NULL);
1399 }
1400 add_stmt (do_poplevel (scope));
1401 }
1402
1403 /* Determine if iteration statement with *CONDP condition and
1404 loop BODY is trivially empty iteration statement or even
1405 trivial infinite loop. In the latter case for -ffinite-loops
1406 add ANNOTATE_EXPR to mark the loop as maybe validly infinite.
1407 Also, emit -Wtautological-compare warning for std::is_constant_evaluated ()
1408 calls in the condition when needed. */
1409
1410 static void
1411 finish_loop_cond (tree *condp, tree body)
1412 {
1413 if (TREE_CODE (*condp) == INTEGER_CST)
1414 return;
1415 bool trivially_empty = expr_first (body) == NULL_TREE;
1416 bool trivial_infinite = false;
1417 if (trivially_empty)
1418 {
1419 tree c = fold_non_dependent_expr (*condp, tf_none,
1420 /*manifestly_const_eval=*/true);
1421 trivial_infinite = c && integer_nonzerop (c);
1422 }
1423 if (warn_tautological_compare)
1424 {
1425 tree cond = *condp;
1426 while (TREE_CODE (cond) == ANNOTATE_EXPR)
1427 cond = TREE_OPERAND (cond, 0);
1428 if (trivial_infinite
1429 && !DECL_IMMEDIATE_FUNCTION_P (current_function_decl))
1430 maybe_warn_for_constant_evaluated (cond, /*constexpr_if=*/false,
1431 /*trivial_infinite=*/true);
1432 else if (!trivially_empty
1433 || !processing_template_decl
1434 || DECL_IMMEDIATE_FUNCTION_P (current_function_decl))
1435 maybe_warn_for_constant_evaluated (cond, /*constexpr_if=*/false,
1436 /*trivial_infinite=*/false);
1437 }
1438 if (trivial_infinite && flag_finite_loops && !processing_template_decl)
1439 *condp = build3 (ANNOTATE_EXPR, TREE_TYPE (*condp), *condp,
1440 build_int_cst (integer_type_node,
1441 annot_expr_maybe_infinite_kind),
1442 integer_zero_node);
1443 }
1444
1445 /* Begin a while-statement. Returns a newly created WHILE_STMT if
1446 appropriate. */
1447
1448 tree
1449 begin_while_stmt (void)
1450 {
1451 tree r;
1452 r = build_stmt (input_location, WHILE_STMT, NULL_TREE, NULL_TREE, NULL_TREE,
1453 NULL_TREE, NULL_TREE);
1454 add_stmt (r);
1455 WHILE_BODY (r) = do_pushlevel (sk_block);
1456 begin_cond (&WHILE_COND (r));
1457 return r;
1458 }
1459
1460 /* Process the COND of a while-statement, which may be given by
1461 WHILE_STMT. */
1462
1463 void
1464 finish_while_stmt_cond (tree cond, tree while_stmt, bool ivdep,
1465 tree unroll, bool novector)
1466 {
1467 cond = maybe_convert_cond (cond);
1468 finish_cond (&WHILE_COND (while_stmt), cond);
1469 begin_maybe_infinite_loop (cond);
1470 if (ivdep && cond != error_mark_node)
1471 WHILE_COND (while_stmt) = build3 (ANNOTATE_EXPR,
1472 TREE_TYPE (WHILE_COND (while_stmt)),
1473 WHILE_COND (while_stmt),
1474 build_int_cst (integer_type_node,
1475 annot_expr_ivdep_kind),
1476 integer_zero_node);
1477 if (unroll && cond != error_mark_node)
1478 WHILE_COND (while_stmt) = build3 (ANNOTATE_EXPR,
1479 TREE_TYPE (WHILE_COND (while_stmt)),
1480 WHILE_COND (while_stmt),
1481 build_int_cst (integer_type_node,
1482 annot_expr_unroll_kind),
1483 unroll);
1484 if (novector && cond != error_mark_node)
1485 WHILE_COND (while_stmt) = build3 (ANNOTATE_EXPR,
1486 TREE_TYPE (WHILE_COND (while_stmt)),
1487 WHILE_COND (while_stmt),
1488 build_int_cst (integer_type_node,
1489 annot_expr_no_vector_kind),
1490 integer_zero_node);
1491 adjust_loop_decl_cond (&WHILE_BODY (while_stmt),
1492 &WHILE_COND_PREP (while_stmt),
1493 &WHILE_COND_CLEANUP (while_stmt));
1494 }
1495
1496 /* Finish a while-statement, which may be given by WHILE_STMT. */
1497
1498 void
1499 finish_while_stmt (tree while_stmt)
1500 {
1501 end_maybe_infinite_loop (boolean_true_node);
1502 if (WHILE_COND_PREP (while_stmt))
1503 finish_loop_cond_prep (&WHILE_BODY (while_stmt),
1504 &WHILE_COND_PREP (while_stmt),
1505 WHILE_COND_CLEANUP (while_stmt));
1506 else
1507 WHILE_BODY (while_stmt) = do_poplevel (WHILE_BODY (while_stmt));
1508 finish_loop_cond (&WHILE_COND (while_stmt), WHILE_BODY (while_stmt));
1509 }
1510
1511 /* Begin a do-statement. Returns a newly created DO_STMT if
1512 appropriate. */
1513
1514 tree
1515 begin_do_stmt (void)
1516 {
1517 tree r = build_stmt (input_location, DO_STMT, NULL_TREE, NULL_TREE,
1518 NULL_TREE);
1519 begin_maybe_infinite_loop (boolean_true_node);
1520 add_stmt (r);
1521 DO_BODY (r) = push_stmt_list ();
1522 return r;
1523 }
1524
1525 /* Finish the body of a do-statement, which may be given by DO_STMT. */
1526
1527 void
1528 finish_do_body (tree do_stmt)
1529 {
1530 tree body = DO_BODY (do_stmt) = pop_stmt_list (DO_BODY (do_stmt));
1531
1532 if (TREE_CODE (body) == STATEMENT_LIST && STATEMENT_LIST_TAIL (body))
1533 body = STATEMENT_LIST_TAIL (body)->stmt;
1534
1535 if (IS_EMPTY_STMT (body))
1536 warning (OPT_Wempty_body,
1537 "suggest explicit braces around empty body in %<do%> statement");
1538 }
1539
1540 /* Finish a do-statement, which may be given by DO_STMT, and whose
1541 COND is as indicated. */
1542
1543 void
1544 finish_do_stmt (tree cond, tree do_stmt, bool ivdep, tree unroll,
1545 bool novector)
1546 {
1547 cond = maybe_convert_cond (cond);
1548 end_maybe_infinite_loop (cond);
1549 /* Unlike other iteration statements, the condition may not contain
1550 a declaration, so we don't call finish_cond which checks for
1551 unexpanded parameter packs. */
1552 if (check_for_bare_parameter_packs (cond))
1553 cond = error_mark_node;
1554 if (ivdep && cond != error_mark_node)
1555 cond = build3 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
1556 build_int_cst (integer_type_node, annot_expr_ivdep_kind),
1557 integer_zero_node);
1558 if (unroll && cond != error_mark_node)
1559 cond = build3 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
1560 build_int_cst (integer_type_node, annot_expr_unroll_kind),
1561 unroll);
1562 if (novector && cond != error_mark_node)
1563 cond = build3 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
1564 build_int_cst (integer_type_node, annot_expr_no_vector_kind),
1565 integer_zero_node);
1566 DO_COND (do_stmt) = cond;
1567 tree do_body = DO_BODY (do_stmt);
1568 if (CONVERT_EXPR_P (do_body)
1569 && integer_zerop (TREE_OPERAND (do_body, 0))
1570 && VOID_TYPE_P (TREE_TYPE (do_body)))
1571 do_body = NULL_TREE;
1572 finish_loop_cond (&DO_COND (do_stmt), do_body);
1573 }
1574
1575 /* Finish a return-statement. The EXPRESSION returned, if any, is as
1576 indicated. */
1577
1578 tree
1579 finish_return_stmt (tree expr)
1580 {
1581 tree r;
1582 bool no_warning;
1583 bool dangling;
1584
1585 expr = check_return_expr (expr, &no_warning, &dangling);
1586
1587 if (error_operand_p (expr)
1588 || (flag_openmp && !check_omp_return ()))
1589 {
1590 /* Suppress -Wreturn-type for this function. */
1591 if (warn_return_type)
1592 suppress_warning (current_function_decl, OPT_Wreturn_type);
1593 return error_mark_node;
1594 }
1595
1596 if (!processing_template_decl)
1597 {
1598 if (warn_sequence_point)
1599 verify_sequence_points (expr);
1600 }
1601
1602 r = build_stmt (input_location, RETURN_EXPR, expr);
1603 RETURN_EXPR_LOCAL_ADDR_P (r) = dangling;
1604 if (no_warning)
1605 suppress_warning (r, OPT_Wreturn_type);
1606 r = maybe_cleanup_point_expr_void (r);
1607 r = add_stmt (r);
1608
1609 return r;
1610 }
1611
1612 /* Begin the scope of a for-statement or a range-for-statement.
1613 Both the returned trees are to be used in a call to
1614 begin_for_stmt or begin_range_for_stmt. */
1615
1616 tree
1617 begin_for_scope (tree *init)
1618 {
1619 tree scope = do_pushlevel (sk_for);
1620
1621 if (processing_template_decl)
1622 *init = push_stmt_list ();
1623 else
1624 *init = NULL_TREE;
1625
1626 return scope;
1627 }
1628
1629 /* Begin a for-statement. Returns a new FOR_STMT.
1630 SCOPE and INIT should be the return of begin_for_scope,
1631 or both NULL_TREE */
1632
1633 tree
1634 begin_for_stmt (tree scope, tree init)
1635 {
1636 tree r;
1637
1638 r = build_stmt (input_location, FOR_STMT, NULL_TREE, NULL_TREE,
1639 NULL_TREE, NULL_TREE, NULL_TREE, NULL_TREE,
1640 NULL_TREE, NULL_TREE);
1641
1642 if (scope == NULL_TREE)
1643 {
1644 gcc_assert (!init);
1645 scope = begin_for_scope (&init);
1646 }
1647
1648 FOR_INIT_STMT (r) = init;
1649 FOR_SCOPE (r) = scope;
1650
1651 return r;
1652 }
1653
1654 /* Finish the init-statement of a for-statement, which may be
1655 given by FOR_STMT. */
1656
1657 void
1658 finish_init_stmt (tree for_stmt)
1659 {
1660 if (processing_template_decl)
1661 FOR_INIT_STMT (for_stmt) = pop_stmt_list (FOR_INIT_STMT (for_stmt));
1662 add_stmt (for_stmt);
1663 FOR_BODY (for_stmt) = do_pushlevel (sk_block);
1664 begin_cond (&FOR_COND (for_stmt));
1665 }
1666
1667 /* Finish the COND of a for-statement, which may be given by
1668 FOR_STMT. */
1669
1670 void
1671 finish_for_cond (tree cond, tree for_stmt, bool ivdep, tree unroll,
1672 bool novector)
1673 {
1674 cond = maybe_convert_cond (cond);
1675 finish_cond (&FOR_COND (for_stmt), cond);
1676 begin_maybe_infinite_loop (cond);
1677 if (ivdep && cond != error_mark_node)
1678 FOR_COND (for_stmt) = build3 (ANNOTATE_EXPR,
1679 TREE_TYPE (FOR_COND (for_stmt)),
1680 FOR_COND (for_stmt),
1681 build_int_cst (integer_type_node,
1682 annot_expr_ivdep_kind),
1683 integer_zero_node);
1684 if (unroll && cond != error_mark_node)
1685 FOR_COND (for_stmt) = build3 (ANNOTATE_EXPR,
1686 TREE_TYPE (FOR_COND (for_stmt)),
1687 FOR_COND (for_stmt),
1688 build_int_cst (integer_type_node,
1689 annot_expr_unroll_kind),
1690 unroll);
1691 if (novector && cond && cond != error_mark_node)
1692 FOR_COND (for_stmt) = build3 (ANNOTATE_EXPR,
1693 TREE_TYPE (FOR_COND (for_stmt)),
1694 FOR_COND (for_stmt),
1695 build_int_cst (integer_type_node,
1696 annot_expr_no_vector_kind),
1697 integer_zero_node);
1698 adjust_loop_decl_cond (&FOR_BODY (for_stmt), &FOR_COND_PREP (for_stmt),
1699 &FOR_COND_CLEANUP (for_stmt));
1700 }
1701
1702 /* Finish the increment-EXPRESSION in a for-statement, which may be
1703 given by FOR_STMT. */
1704
1705 void
1706 finish_for_expr (tree expr, tree for_stmt)
1707 {
1708 if (!expr)
1709 return;
1710 /* If EXPR is an overloaded function, issue an error; there is no
1711 context available to use to perform overload resolution. */
1712 if (type_unknown_p (expr))
1713 {
1714 cxx_incomplete_type_error (expr, TREE_TYPE (expr));
1715 expr = error_mark_node;
1716 }
1717 if (!processing_template_decl)
1718 {
1719 if (warn_sequence_point)
1720 verify_sequence_points (expr);
1721 expr = convert_to_void (expr, ICV_THIRD_IN_FOR,
1722 tf_warning_or_error);
1723 }
1724 else if (!type_dependent_expression_p (expr))
1725 convert_to_void (expr, ICV_THIRD_IN_FOR, tf_warning_or_error);
1726 expr = maybe_cleanup_point_expr_void (expr);
1727 if (check_for_bare_parameter_packs (expr))
1728 expr = error_mark_node;
1729 FOR_EXPR (for_stmt) = expr;
1730 }
1731
1732 /* During parsing of the body, range for uses "__for_{range,begin,end} "
1733 decl names to make those unaccessible by code in the body.
1734 Find those decls and store into RANGE_FOR_DECL array, so that they
1735 can be changed later to ones with underscore instead of space, so that
1736 it can be inspected in the debugger. */
1737
1738 void
1739 find_range_for_decls (tree range_for_decl[3])
1740 {
1741 gcc_assert (CPTI_FOR_BEGIN__IDENTIFIER == CPTI_FOR_RANGE__IDENTIFIER + 1
1742 && CPTI_FOR_END__IDENTIFIER == CPTI_FOR_RANGE__IDENTIFIER + 2
1743 && CPTI_FOR_RANGE_IDENTIFIER == CPTI_FOR_RANGE__IDENTIFIER + 3
1744 && CPTI_FOR_BEGIN_IDENTIFIER == CPTI_FOR_BEGIN__IDENTIFIER + 3
1745 && CPTI_FOR_END_IDENTIFIER == CPTI_FOR_END__IDENTIFIER + 3);
1746 for (int i = 0; i < 3; i++)
1747 {
1748 tree id = cp_global_trees[CPTI_FOR_RANGE__IDENTIFIER + i];
1749 if (IDENTIFIER_BINDING (id)
1750 && IDENTIFIER_BINDING (id)->scope == current_binding_level)
1751 {
1752 range_for_decl[i] = IDENTIFIER_BINDING (id)->value;
1753 gcc_assert (VAR_P (range_for_decl[i])
1754 && DECL_ARTIFICIAL (range_for_decl[i]));
1755 }
1756 }
1757 }
1758
1759 /* Finish the body of a for-statement, which may be given by
1760 FOR_STMT. The increment-EXPR for the loop must be
1761 provided.
1762 It can also finish RANGE_FOR_STMT. */
1763
1764 void
1765 finish_for_stmt (tree for_stmt)
1766 {
1767 end_maybe_infinite_loop (boolean_true_node);
1768
1769 if (TREE_CODE (for_stmt) == RANGE_FOR_STMT)
1770 RANGE_FOR_BODY (for_stmt) = do_poplevel (RANGE_FOR_BODY (for_stmt));
1771 else
1772 {
1773 if (FOR_COND_PREP (for_stmt))
1774 finish_loop_cond_prep (&FOR_BODY (for_stmt),
1775 &FOR_COND_PREP (for_stmt),
1776 FOR_COND_CLEANUP (for_stmt));
1777 else
1778 FOR_BODY (for_stmt) = do_poplevel (FOR_BODY (for_stmt));
1779 if (FOR_COND (for_stmt))
1780 finish_loop_cond (&FOR_COND (for_stmt),
1781 FOR_EXPR (for_stmt) ? integer_one_node
1782 : FOR_BODY (for_stmt));
1783 }
1784
1785 /* Pop the scope for the body of the loop. */
1786 tree *scope_ptr = (TREE_CODE (for_stmt) == RANGE_FOR_STMT
1787 ? &RANGE_FOR_SCOPE (for_stmt)
1788 : &FOR_SCOPE (for_stmt));
1789 tree scope = *scope_ptr;
1790 *scope_ptr = NULL;
1791
1792 /* During parsing of the body, range for uses "__for_{range,begin,end} "
1793 decl names to make those unaccessible by code in the body.
1794 Change it to ones with underscore instead of space, so that it can
1795 be inspected in the debugger. */
1796 tree range_for_decl[3] = { NULL_TREE, NULL_TREE, NULL_TREE };
1797 find_range_for_decls (range_for_decl);
1798
1799 add_stmt (do_poplevel (scope));
1800
1801 /* If we're being called from build_vec_init, don't mess with the names of
1802 the variables for an enclosing range-for. */
1803 if (!stmts_are_full_exprs_p ())
1804 return;
1805
1806 for (int i = 0; i < 3; i++)
1807 if (range_for_decl[i])
1808 DECL_NAME (range_for_decl[i])
1809 = cp_global_trees[CPTI_FOR_RANGE_IDENTIFIER + i];
1810 }
1811
1812 /* Begin a range-for-statement. Returns a new RANGE_FOR_STMT.
1813 SCOPE and INIT should be the return of begin_for_scope,
1814 or both NULL_TREE .
1815 To finish it call finish_for_stmt(). */
1816
1817 tree
1818 begin_range_for_stmt (tree scope, tree init)
1819 {
1820 begin_maybe_infinite_loop (boolean_false_node);
1821
1822 tree r = build_stmt (input_location, RANGE_FOR_STMT, NULL_TREE, NULL_TREE,
1823 NULL_TREE, NULL_TREE, NULL_TREE, NULL_TREE);
1824
1825 if (scope == NULL_TREE)
1826 {
1827 gcc_assert (!init);
1828 scope = begin_for_scope (&init);
1829 }
1830
1831 /* Since C++20, RANGE_FOR_STMTs can use the init tree, so save it. */
1832 RANGE_FOR_INIT_STMT (r) = init;
1833 RANGE_FOR_SCOPE (r) = scope;
1834
1835 return r;
1836 }
1837
1838 /* Finish the head of a range-based for statement, which may
1839 be given by RANGE_FOR_STMT. DECL must be the declaration
1840 and EXPR must be the loop expression. */
1841
1842 void
1843 finish_range_for_decl (tree range_for_stmt, tree decl, tree expr)
1844 {
1845 if (processing_template_decl)
1846 RANGE_FOR_INIT_STMT (range_for_stmt)
1847 = pop_stmt_list (RANGE_FOR_INIT_STMT (range_for_stmt));
1848 RANGE_FOR_DECL (range_for_stmt) = decl;
1849 RANGE_FOR_EXPR (range_for_stmt) = expr;
1850 add_stmt (range_for_stmt);
1851 RANGE_FOR_BODY (range_for_stmt) = do_pushlevel (sk_block);
1852 }
1853
1854 /* Finish a break-statement. */
1855
1856 tree
1857 finish_break_stmt (void)
1858 {
1859 /* In switch statements break is sometimes stylistically used after
1860 a return statement. This can lead to spurious warnings about
1861 control reaching the end of a non-void function when it is
1862 inlined. Note that we are calling block_may_fallthru with
1863 language specific tree nodes; this works because
1864 block_may_fallthru returns true when given something it does not
1865 understand. */
1866 if (!block_may_fallthru (cur_stmt_list))
1867 return void_node;
1868 note_break_stmt ();
1869 return add_stmt (build_stmt (input_location, BREAK_STMT, NULL_TREE));
1870 }
1871
1872 /* Finish a continue-statement. */
1873
1874 tree
1875 finish_continue_stmt (void)
1876 {
1877 return add_stmt (build_stmt (input_location, CONTINUE_STMT, NULL_TREE));
1878 }
1879
1880 /* Begin a switch-statement. Returns a new SWITCH_STMT if
1881 appropriate. */
1882
1883 tree
1884 begin_switch_stmt (void)
1885 {
1886 tree r, scope;
1887
1888 scope = do_pushlevel (sk_cond);
1889 r = build_stmt (input_location, SWITCH_STMT, NULL_TREE, NULL_TREE, NULL_TREE,
1890 scope, NULL_TREE);
1891
1892 begin_cond (&SWITCH_STMT_COND (r));
1893
1894 return r;
1895 }
1896
1897 /* Finish the cond of a switch-statement. */
1898
1899 void
1900 finish_switch_cond (tree cond, tree switch_stmt)
1901 {
1902 tree orig_type = NULL;
1903
1904 if (!processing_template_decl)
1905 {
1906 /* Convert the condition to an integer or enumeration type. */
1907 tree orig_cond = cond;
1908 /* For structured binding used in condition, the conversion needs to be
1909 evaluated before the individual variables are initialized in the
1910 std::tuple_{size,elemenet} case. cp_finish_decomp saved the
1911 conversion result in a TARGET_EXPR, pick it up from there. */
1912 if (DECL_DECOMPOSITION_P (cond)
1913 && DECL_DECOMP_IS_BASE (cond)
1914 && DECL_DECOMP_BASE (cond)
1915 && TREE_CODE (DECL_DECOMP_BASE (cond)) == TARGET_EXPR)
1916 cond = TARGET_EXPR_SLOT (DECL_DECOMP_BASE (cond));
1917 cond = build_expr_type_conversion (WANT_INT | WANT_ENUM, cond, true);
1918 if (cond == NULL_TREE)
1919 {
1920 error_at (cp_expr_loc_or_input_loc (orig_cond),
1921 "switch quantity not an integer");
1922 cond = error_mark_node;
1923 }
1924 /* We want unlowered type here to handle enum bit-fields. */
1925 orig_type = unlowered_expr_type (cond);
1926 if (TREE_CODE (orig_type) != ENUMERAL_TYPE)
1927 orig_type = TREE_TYPE (cond);
1928 if (cond != error_mark_node)
1929 {
1930 /* [stmt.switch]
1931
1932 Integral promotions are performed. */
1933 cond = perform_integral_promotions (cond);
1934 cond = maybe_cleanup_point_expr (cond);
1935 }
1936 }
1937 if (check_for_bare_parameter_packs (cond))
1938 cond = error_mark_node;
1939 else if (!processing_template_decl && warn_sequence_point)
1940 verify_sequence_points (cond);
1941
1942 finish_cond (&SWITCH_STMT_COND (switch_stmt), cond);
1943 SWITCH_STMT_TYPE (switch_stmt) = orig_type;
1944 add_stmt (switch_stmt);
1945 push_switch (switch_stmt);
1946 SWITCH_STMT_BODY (switch_stmt) = push_stmt_list ();
1947 }
1948
1949 /* Finish the body of a switch-statement, which may be given by
1950 SWITCH_STMT. The COND to switch on is indicated. */
1951
1952 void
1953 finish_switch_stmt (tree switch_stmt)
1954 {
1955 tree scope;
1956
1957 SWITCH_STMT_BODY (switch_stmt) =
1958 pop_stmt_list (SWITCH_STMT_BODY (switch_stmt));
1959 pop_switch ();
1960
1961 scope = SWITCH_STMT_SCOPE (switch_stmt);
1962 SWITCH_STMT_SCOPE (switch_stmt) = NULL;
1963 add_stmt (do_poplevel (scope));
1964 }
1965
1966 /* Begin a try-block. Returns a newly-created TRY_BLOCK if
1967 appropriate. */
1968
1969 tree
1970 begin_try_block (void)
1971 {
1972 tree r = build_stmt (input_location, TRY_BLOCK, NULL_TREE, NULL_TREE);
1973 add_stmt (r);
1974 TRY_STMTS (r) = push_stmt_list ();
1975 return r;
1976 }
1977
1978 /* Likewise, for a function-try-block. The block returned in
1979 *COMPOUND_STMT is an artificial outer scope, containing the
1980 function-try-block. */
1981
1982 tree
1983 begin_function_try_block (tree *compound_stmt)
1984 {
1985 tree r;
1986 /* This outer scope does not exist in the C++ standard, but we need
1987 a place to put __FUNCTION__ and similar variables. */
1988 *compound_stmt = begin_compound_stmt (0);
1989 current_binding_level->artificial = 1;
1990 r = begin_try_block ();
1991 FN_TRY_BLOCK_P (r) = 1;
1992 return r;
1993 }
1994
1995 /* Finish a try-block, which may be given by TRY_BLOCK. */
1996
1997 void
1998 finish_try_block (tree try_block)
1999 {
2000 TRY_STMTS (try_block) = pop_stmt_list (TRY_STMTS (try_block));
2001 TRY_HANDLERS (try_block) = push_stmt_list ();
2002 }
2003
2004 /* Finish the body of a cleanup try-block, which may be given by
2005 TRY_BLOCK. */
2006
2007 void
2008 finish_cleanup_try_block (tree try_block)
2009 {
2010 TRY_STMTS (try_block) = pop_stmt_list (TRY_STMTS (try_block));
2011 }
2012
2013 /* Finish an implicitly generated try-block, with a cleanup is given
2014 by CLEANUP. */
2015
2016 void
2017 finish_cleanup (tree cleanup, tree try_block)
2018 {
2019 TRY_HANDLERS (try_block) = cleanup;
2020 CLEANUP_P (try_block) = 1;
2021 }
2022
2023 /* Likewise, for a function-try-block. */
2024
2025 void
2026 finish_function_try_block (tree try_block)
2027 {
2028 finish_try_block (try_block);
2029 /* FIXME : something queer about CTOR_INITIALIZER somehow following
2030 the try block, but moving it inside. */
2031 in_function_try_handler = 1;
2032 }
2033
2034 /* Finish a handler-sequence for a try-block, which may be given by
2035 TRY_BLOCK. */
2036
2037 void
2038 finish_handler_sequence (tree try_block)
2039 {
2040 TRY_HANDLERS (try_block) = pop_stmt_list (TRY_HANDLERS (try_block));
2041 check_handlers (TRY_HANDLERS (try_block));
2042 }
2043
2044 /* Finish the handler-seq for a function-try-block, given by
2045 TRY_BLOCK. COMPOUND_STMT is the outer block created by
2046 begin_function_try_block. */
2047
2048 void
2049 finish_function_handler_sequence (tree try_block, tree compound_stmt)
2050 {
2051 in_function_try_handler = 0;
2052 finish_handler_sequence (try_block);
2053 finish_compound_stmt (compound_stmt);
2054 }
2055
2056 /* Begin a handler. Returns a HANDLER if appropriate. */
2057
2058 tree
2059 begin_handler (void)
2060 {
2061 tree r;
2062
2063 r = build_stmt (input_location, HANDLER, NULL_TREE, NULL_TREE);
2064 add_stmt (r);
2065
2066 /* Create a binding level for the eh_info and the exception object
2067 cleanup. */
2068 HANDLER_BODY (r) = do_pushlevel (sk_catch);
2069
2070 return r;
2071 }
2072
2073 /* Finish the handler-parameters for a handler, which may be given by
2074 HANDLER. DECL is the declaration for the catch parameter, or NULL
2075 if this is a `catch (...)' clause. */
2076
2077 void
2078 finish_handler_parms (tree decl, tree handler)
2079 {
2080 tree type = NULL_TREE;
2081 if (processing_template_decl)
2082 {
2083 if (decl)
2084 {
2085 decl = pushdecl (decl);
2086 decl = push_template_decl (decl);
2087 HANDLER_PARMS (handler) = decl;
2088 type = TREE_TYPE (decl);
2089 }
2090 }
2091 else
2092 {
2093 type = expand_start_catch_block (decl);
2094 if (warn_catch_value
2095 && type != NULL_TREE
2096 && type != error_mark_node
2097 && !TYPE_REF_P (TREE_TYPE (decl)))
2098 {
2099 tree orig_type = TREE_TYPE (decl);
2100 if (CLASS_TYPE_P (orig_type))
2101 {
2102 if (TYPE_POLYMORPHIC_P (orig_type))
2103 warning_at (DECL_SOURCE_LOCATION (decl),
2104 OPT_Wcatch_value_,
2105 "catching polymorphic type %q#T by value",
2106 orig_type);
2107 else if (warn_catch_value > 1)
2108 warning_at (DECL_SOURCE_LOCATION (decl),
2109 OPT_Wcatch_value_,
2110 "catching type %q#T by value", orig_type);
2111 }
2112 else if (warn_catch_value > 2)
2113 warning_at (DECL_SOURCE_LOCATION (decl),
2114 OPT_Wcatch_value_,
2115 "catching non-reference type %q#T", orig_type);
2116 }
2117 }
2118 HANDLER_TYPE (handler) = type;
2119 }
2120
2121 /* Finish a handler, which may be given by HANDLER. The BLOCKs are
2122 the return value from the matching call to finish_handler_parms. */
2123
2124 void
2125 finish_handler (tree handler)
2126 {
2127 if (!processing_template_decl)
2128 expand_end_catch_block ();
2129 HANDLER_BODY (handler) = do_poplevel (HANDLER_BODY (handler));
2130 }
2131
2132 /* Begin a compound statement. FLAGS contains some bits that control the
2133 behavior and context. If BCS_NO_SCOPE is set, the compound statement
2134 does not define a scope. If BCS_FN_BODY is set, this is the outermost
2135 block of a function. If BCS_TRY_BLOCK is set, this is the block
2136 created on behalf of a TRY statement. Returns a token to be passed to
2137 finish_compound_stmt. */
2138
2139 tree
2140 begin_compound_stmt (unsigned int flags)
2141 {
2142 tree r;
2143
2144 if (flags & BCS_NO_SCOPE)
2145 {
2146 r = push_stmt_list ();
2147 STATEMENT_LIST_NO_SCOPE (r) = 1;
2148
2149 /* Normally, we try hard to keep the BLOCK for a statement-expression.
2150 But, if it's a statement-expression with a scopeless block, there's
2151 nothing to keep, and we don't want to accidentally keep a block
2152 *inside* the scopeless block. */
2153 keep_next_level (false);
2154 }
2155 else
2156 {
2157 scope_kind sk = sk_block;
2158 if (flags & BCS_TRY_BLOCK)
2159 sk = sk_try;
2160 else if (flags & BCS_TRANSACTION)
2161 sk = sk_transaction;
2162 else if (flags & BCS_STMT_EXPR)
2163 sk = sk_stmt_expr;
2164 r = do_pushlevel (sk);
2165 }
2166
2167 /* When processing a template, we need to remember where the braces were,
2168 so that we can set up identical scopes when instantiating the template
2169 later. BIND_EXPR is a handy candidate for this.
2170 Note that do_poplevel won't create a BIND_EXPR itself here (and thus
2171 result in nested BIND_EXPRs), since we don't build BLOCK nodes when
2172 processing templates. */
2173 if (processing_template_decl)
2174 {
2175 r = build3 (BIND_EXPR, NULL, NULL, r, NULL);
2176 BIND_EXPR_TRY_BLOCK (r) = (flags & BCS_TRY_BLOCK) != 0;
2177 BIND_EXPR_BODY_BLOCK (r) = (flags & BCS_FN_BODY) != 0;
2178 TREE_SIDE_EFFECTS (r) = 1;
2179 }
2180
2181 return r;
2182 }
2183
2184 /* Finish a compound-statement, which is given by STMT. */
2185
2186 void
2187 finish_compound_stmt (tree stmt)
2188 {
2189 if (TREE_CODE (stmt) == BIND_EXPR)
2190 {
2191 tree body = do_poplevel (BIND_EXPR_BODY (stmt));
2192 /* If the STATEMENT_LIST is empty and this BIND_EXPR isn't special,
2193 discard the BIND_EXPR so it can be merged with the containing
2194 STATEMENT_LIST. */
2195 if (TREE_CODE (body) == STATEMENT_LIST
2196 && STATEMENT_LIST_HEAD (body) == NULL
2197 && !BIND_EXPR_BODY_BLOCK (stmt)
2198 && !BIND_EXPR_TRY_BLOCK (stmt))
2199 stmt = body;
2200 else
2201 BIND_EXPR_BODY (stmt) = body;
2202 }
2203 else if (STATEMENT_LIST_NO_SCOPE (stmt))
2204 stmt = pop_stmt_list (stmt);
2205 else
2206 {
2207 /* Destroy any ObjC "super" receivers that may have been
2208 created. */
2209 objc_clear_super_receiver ();
2210
2211 stmt = do_poplevel (stmt);
2212 }
2213
2214 /* ??? See c_end_compound_stmt wrt statement expressions. */
2215 add_stmt (stmt);
2216 }
2217
2218 /* Finish an asm string literal, which can be a string literal
2219 or parenthesized constant expression. Extract the string literal
2220 from the latter. */
2221
2222 tree
2223 finish_asm_string_expression (location_t loc, tree string)
2224 {
2225 if (string == error_mark_node
2226 || TREE_CODE (string) == STRING_CST
2227 || processing_template_decl)
2228 return string;
2229 string = cxx_constant_value (string, tf_error);
2230 if (TREE_CODE (string) == STRING_CST)
2231 string = build1_loc (loc, PAREN_EXPR, TREE_TYPE (string),
2232 string);
2233 cexpr_str cstr (string);
2234 if (!cstr.type_check (loc))
2235 return error_mark_node;
2236 if (!cstr.extract (loc, string))
2237 string = error_mark_node;
2238 return string;
2239 }
2240
2241 /* Finish an asm-statement, whose components are a STRING, some
2242 OUTPUT_OPERANDS, some INPUT_OPERANDS, some CLOBBERS and some
2243 LABELS. Also note whether the asm-statement should be
2244 considered volatile, and whether it is asm inline. TOPLEV_P
2245 is true if finishing namespace scope extended asm. */
2246
2247 tree
2248 finish_asm_stmt (location_t loc, int volatile_p, tree string,
2249 tree output_operands, tree input_operands, tree clobbers,
2250 tree labels, bool inline_p, bool toplev_p)
2251 {
2252 tree r;
2253 tree t;
2254 int ninputs = list_length (input_operands);
2255 int noutputs = list_length (output_operands);
2256
2257 if (!processing_template_decl)
2258 {
2259 const char *constraint;
2260 const char **oconstraints;
2261 bool allows_mem, allows_reg, is_inout;
2262 tree operand;
2263 int i;
2264
2265 oconstraints = XALLOCAVEC (const char *, noutputs);
2266
2267 string = finish_asm_string_expression (cp_expr_loc_or_loc (string, loc),
2268 string);
2269 if (string == error_mark_node)
2270 return error_mark_node;
2271 for (int i = 0; i < 2; ++i)
2272 for (t = i ? input_operands : output_operands; t; t = TREE_CHAIN (t))
2273 {
2274 tree s = TREE_VALUE (TREE_PURPOSE (t));
2275 s = finish_asm_string_expression (cp_expr_loc_or_loc (s, loc), s);
2276 if (s == error_mark_node)
2277 return error_mark_node;
2278 TREE_VALUE (TREE_PURPOSE (t)) = s;
2279 }
2280 for (t = clobbers; t; t = TREE_CHAIN (t))
2281 {
2282 tree s = TREE_VALUE (t);
2283 s = finish_asm_string_expression (cp_expr_loc_or_loc (s, loc), s);
2284 TREE_VALUE (t) = s;
2285 }
2286
2287 string = resolve_asm_operand_names (string, output_operands,
2288 input_operands, labels);
2289
2290 for (i = 0, t = output_operands; t; t = TREE_CHAIN (t), ++i)
2291 {
2292 operand = TREE_VALUE (t);
2293
2294 /* ??? Really, this should not be here. Users should be using a
2295 proper lvalue, dammit. But there's a long history of using
2296 casts in the output operands. In cases like longlong.h, this
2297 becomes a primitive form of typechecking -- if the cast can be
2298 removed, then the output operand had a type of the proper width;
2299 otherwise we'll get an error. Gross, but ... */
2300 STRIP_NOPS (operand);
2301
2302 operand = mark_lvalue_use (operand);
2303
2304 if (!lvalue_or_else (operand, lv_asm, tf_warning_or_error))
2305 operand = error_mark_node;
2306
2307 if (operand != error_mark_node
2308 && (TREE_READONLY (operand)
2309 || CP_TYPE_CONST_P (TREE_TYPE (operand))
2310 /* Functions are not modifiable, even though they are
2311 lvalues. */
2312 || FUNC_OR_METHOD_TYPE_P (TREE_TYPE (operand))
2313 /* If it's an aggregate and any field is const, then it is
2314 effectively const. */
2315 || (CLASS_TYPE_P (TREE_TYPE (operand))
2316 && C_TYPE_FIELDS_READONLY (TREE_TYPE (operand)))))
2317 cxx_readonly_error (loc, operand, lv_asm);
2318
2319 tree *op = &operand;
2320 while (TREE_CODE (*op) == COMPOUND_EXPR)
2321 op = &TREE_OPERAND (*op, 1);
2322 switch (TREE_CODE (*op))
2323 {
2324 case PREINCREMENT_EXPR:
2325 case PREDECREMENT_EXPR:
2326 case MODIFY_EXPR:
2327 *op = genericize_compound_lvalue (*op);
2328 op = &TREE_OPERAND (*op, 1);
2329 break;
2330 default:
2331 break;
2332 }
2333
2334 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (t)));
2335 oconstraints[i] = constraint;
2336
2337 if (parse_output_constraint (&constraint, i, ninputs, noutputs,
2338 &allows_mem, &allows_reg, &is_inout))
2339 {
2340 /* If the operand is going to end up in memory,
2341 mark it addressable. */
2342 if (!allows_reg && !cxx_mark_addressable (*op))
2343 operand = error_mark_node;
2344 if (allows_reg && toplev_p)
2345 {
2346 error_at (loc, "constraint allows registers outside of "
2347 "a function");
2348 operand = error_mark_node;
2349 }
2350 }
2351 else
2352 operand = error_mark_node;
2353
2354 if (toplev_p && operand != error_mark_node)
2355 {
2356 if (TREE_SIDE_EFFECTS (operand))
2357 {
2358 error_at (loc, "side-effects in output operand outside "
2359 "of a function");
2360 operand = error_mark_node;
2361 }
2362 else
2363 {
2364 tree addr
2365 = cp_build_addr_expr (operand, tf_warning_or_error);
2366 if (addr == error_mark_node)
2367 operand = error_mark_node;
2368 else
2369 {
2370 addr = maybe_constant_value (addr);
2371 if (!initializer_constant_valid_p (addr,
2372 TREE_TYPE (addr)))
2373 {
2374 error_at (loc, "output operand outside of a "
2375 "function is not constant");
2376 operand = error_mark_node;
2377 }
2378 else
2379 operand = build_fold_indirect_ref (addr);
2380 }
2381 }
2382 }
2383 else if (operand != error_mark_node && strstr (constraint, "-"))
2384 {
2385 error_at (loc, "%<-%> modifier used inside of a function");
2386 operand = error_mark_node;
2387 }
2388
2389 TREE_VALUE (t) = operand;
2390 }
2391
2392 for (i = 0, t = input_operands; t; ++i, t = TREE_CHAIN (t))
2393 {
2394 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (t)));
2395 bool constraint_parsed
2396 = parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
2397 oconstraints, &allows_mem, &allows_reg);
2398 /* If the operand is going to end up in memory, don't call
2399 decay_conversion. */
2400 if (constraint_parsed && !allows_reg && allows_mem)
2401 operand = mark_lvalue_use (TREE_VALUE (t));
2402 else
2403 operand = decay_conversion (TREE_VALUE (t), tf_warning_or_error);
2404
2405 /* If the type of the operand hasn't been determined (e.g.,
2406 because it involves an overloaded function), then issue
2407 an error message. There's no context available to
2408 resolve the overloading. */
2409 if (TREE_TYPE (operand) == unknown_type_node)
2410 {
2411 error_at (loc,
2412 "type of %<asm%> operand %qE could not be determined",
2413 TREE_VALUE (t));
2414 operand = error_mark_node;
2415 }
2416
2417 if (constraint_parsed)
2418 {
2419 /* If the operand is going to end up in memory,
2420 mark it addressable. */
2421 if (!allows_reg && allows_mem)
2422 {
2423 /* Strip the nops as we allow this case. FIXME, this really
2424 should be rejected or made deprecated. */
2425 STRIP_NOPS (operand);
2426
2427 tree *op = &operand;
2428 while (TREE_CODE (*op) == COMPOUND_EXPR)
2429 op = &TREE_OPERAND (*op, 1);
2430 switch (TREE_CODE (*op))
2431 {
2432 case PREINCREMENT_EXPR:
2433 case PREDECREMENT_EXPR:
2434 case MODIFY_EXPR:
2435 *op = genericize_compound_lvalue (*op);
2436 op = &TREE_OPERAND (*op, 1);
2437 break;
2438 default:
2439 break;
2440 }
2441
2442 if (!cxx_mark_addressable (*op))
2443 operand = error_mark_node;
2444 }
2445 else if (!allows_reg && !allows_mem)
2446 {
2447 /* If constraint allows neither register nor memory,
2448 try harder to get a constant. */
2449 tree constop = maybe_constant_value (operand);
2450 if (TREE_CONSTANT (constop))
2451 operand = constop;
2452 }
2453 if (allows_reg && toplev_p)
2454 {
2455 error_at (loc, "constraint allows registers outside of "
2456 "a function");
2457 operand = error_mark_node;
2458 }
2459 if (constraint[0] == ':' && operand != error_mark_node)
2460 {
2461 tree t = operand;
2462 STRIP_NOPS (t);
2463 if (TREE_CODE (t) != ADDR_EXPR
2464 || !(TREE_CODE (TREE_OPERAND (t, 0)) == FUNCTION_DECL
2465 || (VAR_P (TREE_OPERAND (t, 0))
2466 && is_global_var (TREE_OPERAND (t, 0)))))
2467 {
2468 error_at (loc, "%<:%> constraint operand is not address "
2469 "of a function or non-automatic variable");
2470 operand = error_mark_node;
2471 }
2472 }
2473 }
2474 else
2475 operand = error_mark_node;
2476
2477 if (toplev_p && operand != error_mark_node)
2478 {
2479 if (TREE_SIDE_EFFECTS (operand))
2480 {
2481 error_at (loc, "side-effects in input operand outside "
2482 "of a function");
2483 operand = error_mark_node;
2484 }
2485 else if (allows_mem && lvalue_or_else (operand, lv_asm, tf_none))
2486 {
2487 tree addr = cp_build_addr_expr (operand, tf_warning_or_error);
2488 if (addr == error_mark_node)
2489 operand = error_mark_node;
2490 else
2491 {
2492 addr = maybe_constant_value (addr);
2493 if (!initializer_constant_valid_p (addr,
2494 TREE_TYPE (addr)))
2495 {
2496 error_at (loc, "input operand outside of a "
2497 "function is not constant");
2498 operand = error_mark_node;
2499 }
2500 else
2501 operand = build_fold_indirect_ref (addr);
2502 }
2503 }
2504 else
2505 {
2506 operand = maybe_constant_value (operand);
2507 if (!initializer_constant_valid_p (operand,
2508 TREE_TYPE (operand)))
2509 {
2510 error_at (loc, "input operand outside of a "
2511 "function is not constant");
2512 operand = error_mark_node;
2513 }
2514 }
2515 }
2516 else if (operand != error_mark_node && strstr (constraint, "-"))
2517 {
2518 error_at (loc, "%<-%> modifier used inside of a function");
2519 operand = error_mark_node;
2520 }
2521
2522 TREE_VALUE (t) = operand;
2523 }
2524 }
2525
2526 r = build_stmt (loc, ASM_EXPR, string,
2527 output_operands, input_operands,
2528 clobbers, labels);
2529 ASM_VOLATILE_P (r) = volatile_p || noutputs == 0;
2530 ASM_INLINE_P (r) = inline_p;
2531 if (toplev_p)
2532 {
2533 symtab->finalize_toplevel_asm (r);
2534 return r;
2535 }
2536 r = maybe_cleanup_point_expr_void (r);
2537 return add_stmt (r);
2538 }
2539
2540 /* Finish a label with the indicated NAME. Returns the new label. */
2541
2542 tree
2543 finish_label_stmt (tree name)
2544 {
2545 tree decl = define_label (input_location, name);
2546
2547 if (decl == error_mark_node)
2548 return error_mark_node;
2549
2550 add_stmt (build_stmt (input_location, LABEL_EXPR, decl));
2551
2552 return decl;
2553 }
2554
2555 /* Finish a series of declarations for local labels. G++ allows users
2556 to declare "local" labels, i.e., labels with scope. This extension
2557 is useful when writing code involving statement-expressions. */
2558
2559 void
2560 finish_label_decl (tree name)
2561 {
2562 if (!at_function_scope_p ())
2563 {
2564 error ("%<__label__%> declarations are only allowed in function scopes");
2565 return;
2566 }
2567
2568 add_decl_expr (declare_local_label (name));
2569 }
2570
2571 /* When DECL goes out of scope, make sure that CLEANUP is executed. */
2572
2573 void
2574 finish_decl_cleanup (tree decl, tree cleanup)
2575 {
2576 push_cleanup (decl, cleanup, false);
2577 }
2578
2579 /* If the current scope exits with an exception, run CLEANUP. */
2580
2581 void
2582 finish_eh_cleanup (tree cleanup)
2583 {
2584 push_cleanup (NULL, cleanup, true);
2585 }
2586
2587 /* The MEM_INITS is a list of mem-initializers, in reverse of the
2588 order they were written by the user. Each node is as for
2589 emit_mem_initializers. */
2590
2591 void
2592 finish_mem_initializers (tree mem_inits)
2593 {
2594 /* Reorder the MEM_INITS so that they are in the order they appeared
2595 in the source program. */
2596 mem_inits = nreverse (mem_inits);
2597
2598 if (processing_template_decl)
2599 {
2600 tree mem;
2601
2602 for (mem = mem_inits; mem; mem = TREE_CHAIN (mem))
2603 {
2604 /* If the TREE_PURPOSE is a TYPE_PACK_EXPANSION, skip the
2605 check for bare parameter packs in the TREE_VALUE, because
2606 any parameter packs in the TREE_VALUE have already been
2607 bound as part of the TREE_PURPOSE. See
2608 make_pack_expansion for more information. */
2609 if (TREE_CODE (TREE_PURPOSE (mem)) != TYPE_PACK_EXPANSION
2610 && check_for_bare_parameter_packs (TREE_VALUE (mem)))
2611 TREE_VALUE (mem) = error_mark_node;
2612 }
2613
2614 add_stmt (build_min_nt_loc (UNKNOWN_LOCATION,
2615 CTOR_INITIALIZER, mem_inits));
2616 }
2617 else
2618 emit_mem_initializers (mem_inits);
2619 }
2620
2621 /* Obfuscate EXPR if it looks like an id-expression or member access so
2622 that the call to finish_decltype in do_auto_deduction will give the
2623 right result. If EVEN_UNEVAL, do this even in unevaluated context. */
2624
2625 tree
2626 force_paren_expr (tree expr, bool even_uneval /* = false */)
2627 {
2628 /* This is only needed for decltype(auto) in C++14. */
2629 if (cxx_dialect < cxx14)
2630 return expr;
2631
2632 /* If we're in unevaluated context, we can't be deducing a
2633 return/initializer type, so we don't need to mess with this. */
2634 if (cp_unevaluated_operand && !even_uneval)
2635 return expr;
2636
2637 if (TREE_CODE (expr) == COMPONENT_REF
2638 || TREE_CODE (expr) == SCOPE_REF
2639 || REFERENCE_REF_P (expr))
2640 REF_PARENTHESIZED_P (expr) = true;
2641 else if (DECL_P (tree_strip_any_location_wrapper (expr)))
2642 {
2643 location_t loc = cp_expr_location (expr);
2644 const tree_code code = processing_template_decl ? PAREN_EXPR
2645 : VIEW_CONVERT_EXPR;
2646 expr = build1_loc (loc, code, TREE_TYPE (expr), expr);
2647 REF_PARENTHESIZED_P (expr) = true;
2648 }
2649 return expr;
2650 }
2651
2652 /* If T is an id-expression obfuscated by force_paren_expr, undo the
2653 obfuscation and return the underlying id-expression. Otherwise
2654 return T. */
2655
2656 tree
2657 maybe_undo_parenthesized_ref (tree t)
2658 {
2659 if (cxx_dialect < cxx14)
2660 return t;
2661
2662 if ((TREE_CODE (t) == PAREN_EXPR || TREE_CODE (t) == VIEW_CONVERT_EXPR)
2663 && REF_PARENTHESIZED_P (t))
2664 t = TREE_OPERAND (t, 0);
2665
2666 return t;
2667 }
2668
2669 /* Finish a parenthesized expression EXPR. */
2670
2671 cp_expr
2672 finish_parenthesized_expr (cp_expr expr)
2673 {
2674 if (EXPR_P (expr))
2675 {
2676 /* This inhibits warnings in maybe_warn_unparenthesized_assignment
2677 and c_common_truthvalue_conversion. */
2678 suppress_warning (STRIP_REFERENCE_REF (*expr), OPT_Wparentheses);
2679 /* And maybe_warn_sizeof_array_div. */
2680 suppress_warning (STRIP_REFERENCE_REF (*expr), OPT_Wsizeof_array_div);
2681 }
2682
2683 if (TREE_CODE (expr) == OFFSET_REF
2684 || TREE_CODE (expr) == SCOPE_REF)
2685 /* [expr.unary.op]/3 The qualified id of a pointer-to-member must not be
2686 enclosed in parentheses. */
2687 PTRMEM_OK_P (expr) = 0;
2688
2689 tree stripped_expr = tree_strip_any_location_wrapper (expr);
2690 if (TREE_CODE (stripped_expr) == STRING_CST)
2691 PAREN_STRING_LITERAL_P (stripped_expr) = 1;
2692 else if (TREE_CODE (stripped_expr) == PACK_INDEX_EXPR)
2693 PACK_INDEX_PARENTHESIZED_P (stripped_expr) = true;
2694
2695 expr = cp_expr (force_paren_expr (expr), expr.get_location ());
2696
2697 return expr;
2698 }
2699
2700 /* Finish a reference to a non-static data member (DECL) that is not
2701 preceded by `.' or `->'. */
2702
2703 tree
2704 finish_non_static_data_member (tree decl, tree object, tree qualifying_scope,
2705 tsubst_flags_t complain /* = tf_warning_or_error */)
2706 {
2707 gcc_assert (TREE_CODE (decl) == FIELD_DECL);
2708 bool try_omp_private = !object && omp_private_member_map;
2709 tree ret;
2710
2711 if (!object)
2712 {
2713 tree scope = qualifying_scope;
2714 if (scope == NULL_TREE)
2715 {
2716 scope = context_for_name_lookup (decl);
2717 if (!TYPE_P (scope))
2718 {
2719 /* Can happen during error recovery (c++/85014). */
2720 gcc_assert (seen_error ());
2721 return error_mark_node;
2722 }
2723 }
2724 object = maybe_dummy_object (scope, NULL);
2725 }
2726
2727 object = maybe_resolve_dummy (object, true);
2728 if (object == error_mark_node)
2729 return error_mark_node;
2730
2731 /* DR 613/850: Can use non-static data members without an associated
2732 object in sizeof/decltype/alignof. */
2733 if (is_dummy_object (object)
2734 && !cp_unevaluated_operand
2735 && (!processing_template_decl || !current_class_ref))
2736 {
2737 if (complain & tf_error)
2738 {
2739 auto_diagnostic_group d;
2740 if (current_function_decl
2741 && DECL_STATIC_FUNCTION_P (current_function_decl))
2742 error ("invalid use of member %qD in static member function", decl);
2743 else if (current_function_decl
2744 && processing_contract_condition
2745 && DECL_CONSTRUCTOR_P (current_function_decl))
2746 error ("invalid use of member %qD in constructor %<pre%> contract", decl);
2747 else if (current_function_decl
2748 && processing_contract_condition
2749 && DECL_DESTRUCTOR_P (current_function_decl))
2750 error ("invalid use of member %qD in destructor %<post%> contract", decl);
2751 else
2752 error ("invalid use of non-static data member %qD", decl);
2753 inform (DECL_SOURCE_LOCATION (decl), "declared here");
2754 }
2755
2756 return error_mark_node;
2757 }
2758
2759 if (current_class_ptr)
2760 TREE_USED (current_class_ptr) = 1;
2761 if (processing_template_decl)
2762 {
2763 tree type = TREE_TYPE (decl);
2764
2765 if (TYPE_REF_P (type))
2766 /* Quals on the object don't matter. */;
2767 else if (PACK_EXPANSION_P (type))
2768 /* Don't bother trying to represent this. */
2769 type = NULL_TREE;
2770 else if (WILDCARD_TYPE_P (TREE_TYPE (object)))
2771 /* We don't know what the eventual quals will be, so punt until
2772 instantiation time.
2773
2774 This can happen when called from build_capture_proxy for an explicit
2775 object lambda. It's a bit marginal to call this function in that
2776 case, since this function is for references to members of 'this',
2777 but the deduced type is required to be derived from the closure
2778 type, so it works. */
2779 type = NULL_TREE;
2780 else
2781 {
2782 /* Set the cv qualifiers. */
2783 int quals = cp_type_quals (TREE_TYPE (object));
2784
2785 if (DECL_MUTABLE_P (decl))
2786 quals &= ~TYPE_QUAL_CONST;
2787
2788 quals |= cp_type_quals (TREE_TYPE (decl));
2789 type = cp_build_qualified_type (type, quals);
2790 }
2791
2792 if (qualifying_scope)
2793 /* Wrap this in a SCOPE_REF for now. */
2794 ret = build_qualified_name (type, qualifying_scope, decl,
2795 /*template_p=*/false);
2796 else
2797 ret = (convert_from_reference
2798 (build_min (COMPONENT_REF, type, object, decl, NULL_TREE)));
2799 }
2800 /* If PROCESSING_TEMPLATE_DECL is nonzero here, then
2801 QUALIFYING_SCOPE is also non-null. */
2802 else
2803 {
2804 tree access_type = TREE_TYPE (object);
2805
2806 if (!perform_or_defer_access_check (TYPE_BINFO (access_type), decl,
2807 decl, complain))
2808 return error_mark_node;
2809
2810 /* If the data member was named `C::M', convert `*this' to `C'
2811 first. */
2812 if (qualifying_scope)
2813 {
2814 tree binfo = NULL_TREE;
2815 object = build_scoped_ref (object, qualifying_scope,
2816 &binfo);
2817 }
2818
2819 ret = build_class_member_access_expr (object, decl,
2820 /*access_path=*/NULL_TREE,
2821 /*preserve_reference=*/false,
2822 complain);
2823 }
2824 if (try_omp_private)
2825 {
2826 tree *v = omp_private_member_map->get (decl);
2827 if (v)
2828 ret = convert_from_reference (*v);
2829 }
2830 return ret;
2831 }
2832
2833 /* DECL was the declaration to which a qualified-id resolved. Issue
2834 an error message if it is not accessible. If OBJECT_TYPE is
2835 non-NULL, we have just seen `x->' or `x.' and OBJECT_TYPE is the
2836 type of `*x', or `x', respectively. If the DECL was named as
2837 `A::B' then NESTED_NAME_SPECIFIER is `A'. Return value is like
2838 perform_access_checks above. */
2839
2840 bool
2841 check_accessibility_of_qualified_id (tree decl,
2842 tree object_type,
2843 tree nested_name_specifier,
2844 tsubst_flags_t complain)
2845 {
2846 /* If we're not checking, return immediately. */
2847 if (deferred_access_no_check)
2848 return true;
2849
2850 /* Determine the SCOPE of DECL. */
2851 tree scope = context_for_name_lookup (decl);
2852 /* If the SCOPE is not a type, then DECL is not a member. */
2853 if (!TYPE_P (scope)
2854 /* If SCOPE is dependent then we can't perform this access check now,
2855 and since we'll perform this access check again after substitution
2856 there's no need to explicitly defer it. */
2857 || dependent_type_p (scope))
2858 return true;
2859
2860 tree qualifying_type = NULL_TREE;
2861 /* Compute the scope through which DECL is being accessed. */
2862 if (object_type
2863 /* OBJECT_TYPE might not be a class type; consider:
2864
2865 class A { typedef int I; };
2866 I *p;
2867 p->A::I::~I();
2868
2869 In this case, we will have "A::I" as the DECL, but "I" as the
2870 OBJECT_TYPE. */
2871 && CLASS_TYPE_P (object_type)
2872 && DERIVED_FROM_P (scope, object_type))
2873 {
2874 /* If we are processing a `->' or `.' expression, use the type of the
2875 left-hand side. */
2876 if (tree open = currently_open_class (object_type))
2877 qualifying_type = open;
2878 else
2879 qualifying_type = object_type;
2880 }
2881 else if (nested_name_specifier)
2882 {
2883 /* If the reference is to a non-static member of the
2884 current class, treat it as if it were referenced through
2885 `this'. */
2886 if (DECL_NONSTATIC_MEMBER_P (decl)
2887 && current_class_ptr)
2888 if (tree current = current_nonlambda_class_type ())
2889 {
2890 if (dependent_type_p (current))
2891 /* In general we can't know whether this access goes through
2892 `this' until instantiation time. Punt now, or else we might
2893 create a deferred access check that's not relative to `this'
2894 when it ought to be. We'll check this access again after
2895 substitution, e.g. from tsubst_qualified_id. */
2896 return true;
2897
2898 if (DERIVED_FROM_P (scope, current))
2899 qualifying_type = current;
2900 }
2901 /* Otherwise, use the type indicated by the
2902 nested-name-specifier. */
2903 if (!qualifying_type)
2904 qualifying_type = nested_name_specifier;
2905 }
2906 else
2907 /* Otherwise, the name must be from the current class or one of
2908 its bases. */
2909 qualifying_type = currently_open_derived_class (scope);
2910
2911 if (qualifying_type
2912 /* It is possible for qualifying type to be a TEMPLATE_TYPE_PARM
2913 or similar in a default argument value. */
2914 && CLASS_TYPE_P (qualifying_type))
2915 return perform_or_defer_access_check (TYPE_BINFO (qualifying_type), decl,
2916 decl, complain);
2917
2918 return true;
2919 }
2920
2921 /* EXPR is the result of a qualified-id. The QUALIFYING_CLASS was the
2922 class named to the left of the "::" operator. DONE is true if this
2923 expression is a complete postfix-expression; it is false if this
2924 expression is followed by '->', '[', '(', etc. ADDRESS_P is true
2925 iff this expression is the operand of '&'. TEMPLATE_P is true iff
2926 the qualified-id was of the form "A::template B". TEMPLATE_ARG_P
2927 is true iff this qualified name appears as a template argument. */
2928
2929 tree
2930 finish_qualified_id_expr (tree qualifying_class,
2931 tree expr,
2932 bool done,
2933 bool address_p,
2934 bool template_p,
2935 bool template_arg_p,
2936 tsubst_flags_t complain)
2937 {
2938 gcc_assert (TYPE_P (qualifying_class));
2939
2940 if (error_operand_p (expr))
2941 return error_mark_node;
2942
2943 if (DECL_P (expr)
2944 /* Functions are marked after overload resolution; avoid redundant
2945 warnings. */
2946 && TREE_CODE (expr) != FUNCTION_DECL
2947 && !mark_used (expr, complain))
2948 return error_mark_node;
2949
2950 if (template_p)
2951 {
2952 if (TREE_CODE (expr) == UNBOUND_CLASS_TEMPLATE)
2953 {
2954 /* cp_parser_lookup_name thought we were looking for a type,
2955 but we're actually looking for a declaration. */
2956 qualifying_class = TYPE_CONTEXT (expr);
2957 expr = TYPE_IDENTIFIER (expr);
2958 }
2959 else
2960 check_template_keyword (expr);
2961 }
2962
2963 /* If EXPR occurs as the operand of '&', use special handling that
2964 permits a pointer-to-member. */
2965 if (address_p && done
2966 && TREE_CODE (qualifying_class) != ENUMERAL_TYPE)
2967 {
2968 if (TREE_CODE (expr) == SCOPE_REF)
2969 expr = TREE_OPERAND (expr, 1);
2970 expr = build_offset_ref (qualifying_class, expr,
2971 /*address_p=*/true, complain);
2972 return expr;
2973 }
2974
2975 /* No need to check access within an enum. */
2976 if (TREE_CODE (qualifying_class) == ENUMERAL_TYPE
2977 && TREE_CODE (expr) != IDENTIFIER_NODE)
2978 return expr;
2979
2980 /* Within the scope of a class, turn references to non-static
2981 members into expression of the form "this->...". */
2982 if (template_arg_p)
2983 /* But, within a template argument, we do not want make the
2984 transformation, as there is no "this" pointer. */
2985 ;
2986 else if (TREE_CODE (expr) == FIELD_DECL)
2987 {
2988 push_deferring_access_checks (dk_no_check);
2989 expr = finish_non_static_data_member (expr, NULL_TREE,
2990 qualifying_class, complain);
2991 pop_deferring_access_checks ();
2992 }
2993 else if (BASELINK_P (expr))
2994 {
2995 /* See if any of the functions are non-static members. */
2996 /* If so, the expression may be relative to 'this'. */
2997 if (!shared_member_p (expr)
2998 && current_class_ptr
2999 && DERIVED_FROM_P (qualifying_class,
3000 current_nonlambda_class_type ()))
3001 expr = (build_class_member_access_expr
3002 (maybe_dummy_object (qualifying_class, NULL),
3003 expr,
3004 BASELINK_ACCESS_BINFO (expr),
3005 /*preserve_reference=*/false,
3006 complain));
3007 else if (done)
3008 /* The expression is a qualified name whose address is not
3009 being taken. */
3010 expr = build_offset_ref (qualifying_class, expr, /*address_p=*/false,
3011 complain);
3012 }
3013 else if (!template_p
3014 && TREE_CODE (expr) == TEMPLATE_DECL
3015 && !DECL_FUNCTION_TEMPLATE_P (expr))
3016 {
3017 if (complain & tf_error)
3018 error ("%qE missing template arguments", expr);
3019 return error_mark_node;
3020 }
3021 else
3022 {
3023 /* In a template, return a SCOPE_REF for most qualified-ids
3024 so that we can check access at instantiation time. But if
3025 we're looking at a member of the current instantiation, we
3026 know we have access and building up the SCOPE_REF confuses
3027 non-type template argument handling. */
3028 if (processing_template_decl
3029 && (!currently_open_class (qualifying_class)
3030 || TREE_CODE (expr) == IDENTIFIER_NODE
3031 || TREE_CODE (expr) == TEMPLATE_ID_EXPR
3032 || TREE_CODE (expr) == BIT_NOT_EXPR))
3033 expr = build_qualified_name (TREE_TYPE (expr),
3034 qualifying_class, expr,
3035 template_p);
3036 else if (tree wrap = maybe_get_tls_wrapper_call (expr))
3037 expr = wrap;
3038
3039 expr = convert_from_reference (expr);
3040 }
3041
3042 return expr;
3043 }
3044
3045 /* Begin a statement-expression. The value returned must be passed to
3046 finish_stmt_expr. */
3047
3048 tree
3049 begin_stmt_expr (void)
3050 {
3051 return push_stmt_list ();
3052 }
3053
3054 /* Process the final expression of a statement expression. EXPR can be
3055 NULL, if the final expression is empty. Return a STATEMENT_LIST
3056 containing all the statements in the statement-expression, or
3057 ERROR_MARK_NODE if there was an error. */
3058
3059 tree
3060 finish_stmt_expr_expr (tree expr, tree stmt_expr)
3061 {
3062 if (error_operand_p (expr))
3063 {
3064 /* The type of the statement-expression is the type of the last
3065 expression. */
3066 TREE_TYPE (stmt_expr) = error_mark_node;
3067 return error_mark_node;
3068 }
3069
3070 /* If the last statement does not have "void" type, then the value
3071 of the last statement is the value of the entire expression. */
3072 if (expr)
3073 {
3074 tree type = TREE_TYPE (expr);
3075
3076 if (type && type_unknown_p (type))
3077 {
3078 error ("a statement expression is an insufficient context"
3079 " for overload resolution");
3080 TREE_TYPE (stmt_expr) = error_mark_node;
3081 return error_mark_node;
3082 }
3083 else if (processing_template_decl)
3084 {
3085 expr = build_stmt (input_location, EXPR_STMT, expr);
3086 expr = add_stmt (expr);
3087 /* Mark the last statement so that we can recognize it as such at
3088 template-instantiation time. */
3089 EXPR_STMT_STMT_EXPR_RESULT (expr) = 1;
3090 }
3091 else if (VOID_TYPE_P (type))
3092 {
3093 /* Just treat this like an ordinary statement. */
3094 expr = finish_expr_stmt (expr);
3095 }
3096 else
3097 {
3098 /* It actually has a value we need to deal with. First, force it
3099 to be an rvalue so that we won't need to build up a copy
3100 constructor call later when we try to assign it to something. */
3101 expr = force_rvalue (expr, tf_warning_or_error);
3102 if (error_operand_p (expr))
3103 return error_mark_node;
3104
3105 /* Update for array-to-pointer decay. */
3106 type = TREE_TYPE (expr);
3107
3108 /* This TARGET_EXPR will initialize the outer one added by
3109 finish_stmt_expr. */
3110 set_target_expr_eliding (expr);
3111
3112 /* Wrap it in a CLEANUP_POINT_EXPR and add it to the list like a
3113 normal statement, but don't convert to void or actually add
3114 the EXPR_STMT. */
3115 if (TREE_CODE (expr) != CLEANUP_POINT_EXPR)
3116 expr = maybe_cleanup_point_expr (expr);
3117 add_stmt (expr);
3118 }
3119
3120 /* The type of the statement-expression is the type of the last
3121 expression. */
3122 TREE_TYPE (stmt_expr) = type;
3123 }
3124
3125 return stmt_expr;
3126 }
3127
3128 /* Finish a statement-expression. EXPR should be the value returned
3129 by the previous begin_stmt_expr. Returns an expression
3130 representing the statement-expression. */
3131
3132 tree
3133 finish_stmt_expr (tree stmt_expr, bool has_no_scope)
3134 {
3135 tree type;
3136 tree result;
3137
3138 if (error_operand_p (stmt_expr))
3139 {
3140 pop_stmt_list (stmt_expr);
3141 return error_mark_node;
3142 }
3143
3144 gcc_assert (TREE_CODE (stmt_expr) == STATEMENT_LIST);
3145
3146 type = TREE_TYPE (stmt_expr);
3147 result = pop_stmt_list (stmt_expr);
3148 TREE_TYPE (result) = type;
3149
3150 if (processing_template_decl)
3151 {
3152 result = build_min (STMT_EXPR, type, result);
3153 TREE_SIDE_EFFECTS (result) = 1;
3154 STMT_EXPR_NO_SCOPE (result) = has_no_scope;
3155 }
3156 else if (CLASS_TYPE_P (type))
3157 {
3158 /* Wrap the statement-expression in a TARGET_EXPR so that the
3159 temporary object created by the final expression is destroyed at
3160 the end of the full-expression containing the
3161 statement-expression. */
3162 result = force_target_expr (type, result, tf_warning_or_error);
3163 }
3164
3165 return result;
3166 }
3167
3168 /* Returns the expression which provides the value of STMT_EXPR. */
3169
3170 tree
3171 stmt_expr_value_expr (tree stmt_expr)
3172 {
3173 tree t = STMT_EXPR_STMT (stmt_expr);
3174
3175 if (TREE_CODE (t) == BIND_EXPR)
3176 t = BIND_EXPR_BODY (t);
3177
3178 if (TREE_CODE (t) == STATEMENT_LIST && STATEMENT_LIST_TAIL (t))
3179 t = STATEMENT_LIST_TAIL (t)->stmt;
3180
3181 if (TREE_CODE (t) == EXPR_STMT)
3182 t = EXPR_STMT_EXPR (t);
3183
3184 return t;
3185 }
3186
3187 /* Return TRUE iff EXPR_STMT is an empty list of
3188 expression statements. */
3189
3190 bool
3191 empty_expr_stmt_p (tree expr_stmt)
3192 {
3193 tree body = NULL_TREE;
3194
3195 if (expr_stmt == void_node)
3196 return true;
3197
3198 if (expr_stmt)
3199 {
3200 if (TREE_CODE (expr_stmt) == EXPR_STMT)
3201 body = EXPR_STMT_EXPR (expr_stmt);
3202 else if (TREE_CODE (expr_stmt) == STATEMENT_LIST)
3203 body = expr_stmt;
3204 }
3205
3206 if (body)
3207 {
3208 if (TREE_CODE (body) == STATEMENT_LIST)
3209 return tsi_end_p (tsi_start (body));
3210 else
3211 return empty_expr_stmt_p (body);
3212 }
3213 return false;
3214 }
3215
3216 /* Perform Koenig lookup. FN_EXPR is the postfix-expression representing
3217 the function (or functions) to call; ARGS are the arguments to the
3218 call. Returns the functions to be considered by overload resolution. */
3219
3220 cp_expr
3221 perform_koenig_lookup (cp_expr fn_expr, vec<tree, va_gc> *args,
3222 tsubst_flags_t complain)
3223 {
3224 tree identifier = NULL_TREE;
3225 tree functions = NULL_TREE;
3226 tree tmpl_args = NULL_TREE;
3227 bool template_id = false;
3228 location_t loc = fn_expr.get_location ();
3229 tree fn = fn_expr.get_value ();
3230
3231 STRIP_ANY_LOCATION_WRAPPER (fn);
3232
3233 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
3234 {
3235 /* Use a separate flag to handle null args. */
3236 template_id = true;
3237 tmpl_args = TREE_OPERAND (fn, 1);
3238 fn = TREE_OPERAND (fn, 0);
3239 }
3240
3241 /* Find the name of the overloaded function. */
3242 if (identifier_p (fn))
3243 identifier = fn;
3244 else
3245 {
3246 functions = fn;
3247 identifier = OVL_NAME (functions);
3248 }
3249
3250 /* A call to a namespace-scope function using an unqualified name.
3251
3252 Do Koenig lookup -- unless any of the arguments are
3253 type-dependent. */
3254 if (!any_type_dependent_arguments_p (args)
3255 && !any_dependent_template_arguments_p (tmpl_args))
3256 {
3257 fn = lookup_arg_dependent (identifier, functions, args);
3258 if (!fn)
3259 {
3260 /* The unqualified name could not be resolved. */
3261 if (complain & tf_error)
3262 fn = unqualified_fn_lookup_error (cp_expr (identifier, loc));
3263 else
3264 fn = identifier;
3265 }
3266 }
3267
3268 if (fn && template_id && fn != error_mark_node)
3269 fn = build2 (TEMPLATE_ID_EXPR, unknown_type_node, fn, tmpl_args);
3270
3271 return cp_expr (fn, loc);
3272 }
3273
3274 /* Generate an expression for `FN (ARGS)'. This may change the
3275 contents of ARGS.
3276
3277 If DISALLOW_VIRTUAL is true, the call to FN will be not generated
3278 as a virtual call, even if FN is virtual. (This flag is set when
3279 encountering an expression where the function name is explicitly
3280 qualified. For example a call to `X::f' never generates a virtual
3281 call.)
3282
3283 Returns code for the call. */
3284
3285 tree
3286 finish_call_expr (tree fn, vec<tree, va_gc> **args, bool disallow_virtual,
3287 bool koenig_p, tsubst_flags_t complain)
3288 {
3289 tree result;
3290 tree orig_fn;
3291 vec<tree, va_gc> *orig_args = *args;
3292
3293 if (fn == error_mark_node)
3294 return error_mark_node;
3295
3296 gcc_assert (!TYPE_P (fn));
3297
3298 /* If FN may be a FUNCTION_DECL obfuscated by force_paren_expr, undo
3299 it so that we can tell this is a call to a known function. */
3300 fn = maybe_undo_parenthesized_ref (fn);
3301
3302 STRIP_ANY_LOCATION_WRAPPER (fn);
3303
3304 orig_fn = fn;
3305
3306 if (processing_template_decl)
3307 {
3308 /* If FN is a local extern declaration (or set thereof) in a template,
3309 look it up again at instantiation time. */
3310 if (is_overloaded_fn (fn))
3311 {
3312 tree ifn = get_first_fn (fn);
3313 if (TREE_CODE (ifn) == FUNCTION_DECL
3314 && dependent_local_decl_p (ifn))
3315 orig_fn = DECL_NAME (ifn);
3316 }
3317
3318 /* If the call expression is dependent, build a CALL_EXPR node
3319 with no type; type_dependent_expression_p recognizes
3320 expressions with no type as being dependent. */
3321 if (type_dependent_expression_p (fn)
3322 || any_type_dependent_arguments_p (*args))
3323 {
3324 result = build_min_nt_call_vec (orig_fn, *args);
3325 SET_EXPR_LOCATION (result, cp_expr_loc_or_input_loc (fn));
3326 KOENIG_LOOKUP_P (result) = koenig_p;
3327 /* Disable the std::move warnings since this call was dependent
3328 (c++/89780, c++/107363). This also suppresses the
3329 -Wredundant-move warning. */
3330 suppress_warning (result, OPT_Wpessimizing_move);
3331
3332 if (cfun && cp_function_chain && !cp_unevaluated_operand)
3333 {
3334 bool abnormal = true;
3335 for (lkp_iterator iter (maybe_get_fns (fn)); iter; ++iter)
3336 {
3337 tree fndecl = STRIP_TEMPLATE (*iter);
3338 if (TREE_CODE (fndecl) != FUNCTION_DECL
3339 || !TREE_THIS_VOLATILE (fndecl))
3340 {
3341 abnormal = false;
3342 break;
3343 }
3344 }
3345 /* FIXME: Stop warning about falling off end of non-void
3346 function. But this is wrong. Even if we only see
3347 no-return fns at this point, we could select a
3348 future-defined return fn during instantiation. Or
3349 vice-versa. */
3350 if (abnormal)
3351 current_function_returns_abnormally = 1;
3352 }
3353 if (TREE_CODE (fn) == COMPONENT_REF)
3354 maybe_generic_this_capture (TREE_OPERAND (fn, 0),
3355 TREE_OPERAND (fn, 1));
3356 return result;
3357 }
3358 orig_args = make_tree_vector_copy (*args);
3359 }
3360
3361 if (TREE_CODE (fn) == COMPONENT_REF)
3362 {
3363 tree member = TREE_OPERAND (fn, 1);
3364 if (BASELINK_P (member))
3365 {
3366 tree object = TREE_OPERAND (fn, 0);
3367 return build_new_method_call (object, member,
3368 args, NULL_TREE,
3369 (disallow_virtual
3370 ? LOOKUP_NORMAL | LOOKUP_NONVIRTUAL
3371 : LOOKUP_NORMAL),
3372 /*fn_p=*/NULL,
3373 complain);
3374 }
3375 }
3376
3377 /* Per 13.3.1.1, '(&f)(...)' is the same as '(f)(...)'. */
3378 if (TREE_CODE (fn) == ADDR_EXPR
3379 && TREE_CODE (TREE_OPERAND (fn, 0)) == OVERLOAD)
3380 fn = TREE_OPERAND (fn, 0);
3381
3382 if (is_overloaded_fn (fn))
3383 fn = baselink_for_fns (fn);
3384
3385 result = NULL_TREE;
3386 if (BASELINK_P (fn))
3387 {
3388 tree object;
3389
3390 /* A call to a member function. From [over.call.func]:
3391
3392 If the keyword this is in scope and refers to the class of
3393 that member function, or a derived class thereof, then the
3394 function call is transformed into a qualified function call
3395 using (*this) as the postfix-expression to the left of the
3396 . operator.... [Otherwise] a contrived object of type T
3397 becomes the implied object argument.
3398
3399 In this situation:
3400
3401 struct A { void f(); };
3402 struct B : public A {};
3403 struct C : public A { void g() { B::f(); }};
3404
3405 "the class of that member function" refers to `A'. But 11.2
3406 [class.access.base] says that we need to convert 'this' to B* as
3407 part of the access, so we pass 'B' to maybe_dummy_object. */
3408
3409 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (get_first_fn (fn)))
3410 {
3411 /* A constructor call always uses a dummy object. (This constructor
3412 call which has the form A::A () is actually invalid and we are
3413 going to reject it later in build_new_method_call.) */
3414 object = build_dummy_object (BINFO_TYPE (BASELINK_ACCESS_BINFO (fn)));
3415 }
3416 else
3417 object = maybe_dummy_object (BINFO_TYPE (BASELINK_ACCESS_BINFO (fn)),
3418 NULL);
3419
3420 result = build_new_method_call (object, fn, args, NULL_TREE,
3421 (disallow_virtual
3422 ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
3423 : LOOKUP_NORMAL),
3424 /*fn_p=*/NULL,
3425 complain);
3426 }
3427 else if (concept_check_p (fn))
3428 {
3429 error_at (EXPR_LOC_OR_LOC (fn, input_location),
3430 "cannot call a concept as a function");
3431 return error_mark_node;
3432 }
3433 else if (is_overloaded_fn (fn))
3434 {
3435 /* If the function is an overloaded builtin, resolve it. */
3436 if (TREE_CODE (fn) == FUNCTION_DECL
3437 && (DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL
3438 || DECL_BUILT_IN_CLASS (fn) == BUILT_IN_MD))
3439 result = resolve_overloaded_builtin (input_location, fn, *args,
3440 complain & tf_error);
3441
3442 if (!result)
3443 {
3444 tree alloc_size_attr = NULL_TREE;
3445 if (warn_calloc_transposed_args
3446 && TREE_CODE (fn) == FUNCTION_DECL
3447 && (alloc_size_attr
3448 = lookup_attribute ("alloc_size",
3449 TYPE_ATTRIBUTES (TREE_TYPE (fn)))))
3450 if (TREE_VALUE (alloc_size_attr) == NULL_TREE
3451 || TREE_CHAIN (TREE_VALUE (alloc_size_attr)) == NULL_TREE)
3452 alloc_size_attr = NULL_TREE;
3453 if ((warn_sizeof_pointer_memaccess || alloc_size_attr)
3454 && (complain & tf_warning)
3455 && !vec_safe_is_empty (*args)
3456 && !processing_template_decl)
3457 {
3458 location_t sizeof_arg_loc[6];
3459 tree sizeof_arg[6];
3460 unsigned int i;
3461 for (i = 0; i < (alloc_size_attr ? 6 : 3); i++)
3462 {
3463 tree t;
3464
3465 sizeof_arg_loc[i] = UNKNOWN_LOCATION;
3466 sizeof_arg[i] = NULL_TREE;
3467 if (i >= (*args)->length ())
3468 continue;
3469 t = (**args)[i];
3470 if (TREE_CODE (t) != SIZEOF_EXPR)
3471 continue;
3472 if (SIZEOF_EXPR_TYPE_P (t))
3473 sizeof_arg[i] = TREE_TYPE (TREE_OPERAND (t, 0));
3474 else
3475 sizeof_arg[i] = TREE_OPERAND (t, 0);
3476 sizeof_arg_loc[i] = EXPR_LOCATION (t);
3477 }
3478 if (warn_sizeof_pointer_memaccess)
3479 {
3480 auto same_p = same_type_ignoring_top_level_qualifiers_p;
3481 sizeof_pointer_memaccess_warning (sizeof_arg_loc, fn, *args,
3482 sizeof_arg, same_p);
3483 }
3484 if (alloc_size_attr)
3485 warn_for_calloc (sizeof_arg_loc, fn, *args, sizeof_arg,
3486 alloc_size_attr);
3487 }
3488
3489 if ((complain & tf_warning)
3490 && TREE_CODE (fn) == FUNCTION_DECL
3491 && fndecl_built_in_p (fn, BUILT_IN_MEMSET)
3492 && vec_safe_length (*args) == 3
3493 && !any_type_dependent_arguments_p (*args))
3494 {
3495 tree arg0 = (*orig_args)[0];
3496 tree arg1 = (*orig_args)[1];
3497 tree arg2 = (*orig_args)[2];
3498 int literal_mask = ((literal_integer_zerop (arg1) << 1)
3499 | (literal_integer_zerop (arg2) << 2));
3500 warn_for_memset (input_location, arg0, arg2, literal_mask);
3501 }
3502
3503 /* A call to a namespace-scope function. */
3504 result = build_new_function_call (fn, args, complain);
3505 }
3506 }
3507 else if (TREE_CODE (fn) == PSEUDO_DTOR_EXPR)
3508 {
3509 if (!vec_safe_is_empty (*args))
3510 error ("arguments to destructor are not allowed");
3511 /* C++20/DR: If the postfix-expression names a pseudo-destructor (in
3512 which case the postfix-expression is a possibly-parenthesized class
3513 member access), the function call destroys the object of scalar type
3514 denoted by the object expression of the class member access. */
3515 tree ob = TREE_OPERAND (fn, 0);
3516 if (obvalue_p (ob))
3517 result = build_trivial_dtor_call (ob, true);
3518 else
3519 /* No location to clobber. */
3520 result = convert_to_void (ob, ICV_STATEMENT, complain);
3521 }
3522 else if (CLASS_TYPE_P (TREE_TYPE (fn)))
3523 /* If the "function" is really an object of class type, it might
3524 have an overloaded `operator ()'. */
3525 result = build_op_call (fn, args, complain);
3526
3527 if (!result)
3528 /* A call where the function is unknown. */
3529 result = cp_build_function_call_vec (fn, args, complain);
3530
3531 if (processing_template_decl && result != error_mark_node)
3532 {
3533 if (INDIRECT_REF_P (result))
3534 result = TREE_OPERAND (result, 0);
3535
3536 /* Prune all but the selected function from the original overload
3537 set so that we can avoid some duplicate work at instantiation time. */
3538 if (TREE_CODE (result) == CALL_EXPR
3539 && really_overloaded_fn (orig_fn))
3540 {
3541 tree sel_fn = CALL_EXPR_FN (result);
3542 if (TREE_CODE (sel_fn) == COMPONENT_REF)
3543 {
3544 /* The non-dependent result of build_new_method_call. */
3545 sel_fn = TREE_OPERAND (sel_fn, 1);
3546 gcc_assert (BASELINK_P (sel_fn));
3547 }
3548 else if (TREE_CODE (sel_fn) == ADDR_EXPR)
3549 /* Our original callee wasn't wrapped in an ADDR_EXPR,
3550 so strip this ADDR_EXPR added by build_over_call. */
3551 sel_fn = TREE_OPERAND (sel_fn, 0);
3552 orig_fn = sel_fn;
3553 }
3554
3555 tree r = build_call_vec (TREE_TYPE (result), orig_fn, orig_args);
3556 SET_EXPR_LOCATION (r, input_location);
3557 KOENIG_LOOKUP_P (r) = koenig_p;
3558 TREE_NO_WARNING (r) = TREE_NO_WARNING (result);
3559 release_tree_vector (orig_args);
3560 result = convert_from_reference (r);
3561 }
3562
3563 return result;
3564 }
3565
3566 /* Finish a call to a postfix increment or decrement or EXPR. (Which
3567 is indicated by CODE, which should be POSTINCREMENT_EXPR or
3568 POSTDECREMENT_EXPR.) */
3569
3570 cp_expr
3571 finish_increment_expr (cp_expr expr, enum tree_code code)
3572 {
3573 /* input_location holds the location of the trailing operator token.
3574 Build a location of the form:
3575 expr++
3576 ~~~~^~
3577 with the caret at the operator token, ranging from the start
3578 of EXPR to the end of the operator token. */
3579 location_t combined_loc = make_location (input_location,
3580 expr.get_start (),
3581 get_finish (input_location));
3582 cp_expr result = build_x_unary_op (combined_loc, code, expr,
3583 NULL_TREE, tf_warning_or_error);
3584 /* TODO: build_x_unary_op doesn't honor the location, so set it here. */
3585 result.set_location (combined_loc);
3586 return result;
3587 }
3588
3589 /* Finish a use of `this'. Returns an expression for `this'. */
3590
3591 tree
3592 finish_this_expr (void)
3593 {
3594 tree result = NULL_TREE;
3595
3596 if (current_class_ptr)
3597 {
3598 tree type = TREE_TYPE (current_class_ref);
3599
3600 /* In a lambda expression, 'this' refers to the captured 'this'. */
3601 if (LAMBDA_TYPE_P (type))
3602 result = lambda_expr_this_capture (CLASSTYPE_LAMBDA_EXPR (type), true);
3603 else
3604 result = current_class_ptr;
3605 }
3606
3607 if (result)
3608 /* The keyword 'this' is a prvalue expression. */
3609 return rvalue (result);
3610
3611 tree fn = current_nonlambda_function ();
3612 if (fn && DECL_XOBJ_MEMBER_FUNCTION_P (fn))
3613 {
3614 auto_diagnostic_group d;
3615 error ("%<this%> is unavailable for explicit object member "
3616 "functions");
3617 tree xobj_parm = DECL_ARGUMENTS (fn);
3618 gcc_assert (xobj_parm);
3619 tree parm_name = DECL_NAME (xobj_parm);
3620
3621 static tree remembered_fn = NULL_TREE;
3622 /* Only output this diagnostic once per function. */
3623 if (remembered_fn == fn)
3624 /* Early escape. */;
3625 else if (parm_name)
3626 inform (DECL_SOURCE_LOCATION (xobj_parm),
3627 "use explicit object parameter %qs instead",
3628 IDENTIFIER_POINTER (parm_name));
3629 else
3630 inform (DECL_SOURCE_LOCATION (xobj_parm),
3631 "name the explicit object parameter");
3632
3633 remembered_fn = fn;
3634 }
3635 else if (fn && DECL_STATIC_FUNCTION_P (fn))
3636 error ("%<this%> is unavailable for static member functions");
3637 else if (fn && processing_contract_condition && DECL_CONSTRUCTOR_P (fn))
3638 error ("invalid use of %<this%> before it is valid");
3639 else if (fn && processing_contract_condition && DECL_DESTRUCTOR_P (fn))
3640 error ("invalid use of %<this%> after it is valid");
3641 else if (fn)
3642 error ("invalid use of %<this%> in non-member function");
3643 else
3644 error ("invalid use of %<this%> at top level");
3645 return error_mark_node;
3646 }
3647
3648 /* Finish a pseudo-destructor expression. If SCOPE is NULL, the
3649 expression was of the form `OBJECT.~DESTRUCTOR' where DESTRUCTOR is
3650 the TYPE for the type given. If SCOPE is non-NULL, the expression
3651 was of the form `OBJECT.SCOPE::~DESTRUCTOR'. */
3652
3653 tree
3654 finish_pseudo_destructor_expr (tree object, tree scope, tree destructor,
3655 location_t loc, tsubst_flags_t complain)
3656 {
3657 if (object == error_mark_node || destructor == error_mark_node)
3658 return error_mark_node;
3659
3660 gcc_assert (TYPE_P (destructor));
3661
3662 if (!processing_template_decl)
3663 {
3664 if (scope == error_mark_node)
3665 {
3666 if (complain & tf_error)
3667 error_at (loc, "invalid qualifying scope in pseudo-destructor name");
3668 return error_mark_node;
3669 }
3670 if (is_auto (destructor))
3671 destructor = TREE_TYPE (object);
3672 if (scope && TYPE_P (scope) && !check_dtor_name (scope, destructor))
3673 {
3674 if (complain & tf_error)
3675 error_at (loc,
3676 "qualified type %qT does not match destructor name ~%qT",
3677 scope, destructor);
3678 return error_mark_node;
3679 }
3680
3681
3682 /* [expr.pseudo] says both:
3683
3684 The type designated by the pseudo-destructor-name shall be
3685 the same as the object type.
3686
3687 and:
3688
3689 The cv-unqualified versions of the object type and of the
3690 type designated by the pseudo-destructor-name shall be the
3691 same type.
3692
3693 We implement the more generous second sentence, since that is
3694 what most other compilers do. */
3695 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (object),
3696 destructor))
3697 {
3698 if (complain & tf_error)
3699 error_at (loc, "%qE is not of type %qT", object, destructor);
3700 return error_mark_node;
3701 }
3702 }
3703
3704 tree type = (type_dependent_expression_p (object)
3705 ? NULL_TREE : void_type_node);
3706
3707 return build3_loc (loc, PSEUDO_DTOR_EXPR, type, object,
3708 scope, destructor);
3709 }
3710
3711 /* Finish an expression of the form CODE EXPR. */
3712
3713 cp_expr
3714 finish_unary_op_expr (location_t op_loc, enum tree_code code, cp_expr expr,
3715 tsubst_flags_t complain)
3716 {
3717 /* Build a location of the form:
3718 ++expr
3719 ^~~~~~
3720 with the caret at the operator token, ranging from the start
3721 of the operator token to the end of EXPR. */
3722 location_t combined_loc = make_location (op_loc,
3723 op_loc, expr.get_finish ());
3724 cp_expr result = build_x_unary_op (combined_loc, code, expr,
3725 NULL_TREE, complain);
3726 /* TODO: build_x_unary_op doesn't always honor the location. */
3727 result.set_location (combined_loc);
3728
3729 if (result == error_mark_node)
3730 return result;
3731
3732 if (!(complain & tf_warning))
3733 return result;
3734
3735 tree result_ovl = result;
3736 tree expr_ovl = expr;
3737
3738 if (!processing_template_decl)
3739 expr_ovl = cp_fully_fold (expr_ovl);
3740
3741 if (!CONSTANT_CLASS_P (expr_ovl)
3742 || TREE_OVERFLOW_P (expr_ovl))
3743 return result;
3744
3745 if (!processing_template_decl)
3746 result_ovl = cp_fully_fold (result_ovl);
3747
3748 if (CONSTANT_CLASS_P (result_ovl) && TREE_OVERFLOW_P (result_ovl))
3749 overflow_warning (combined_loc, result_ovl);
3750
3751 return result;
3752 }
3753
3754 /* Return true if CONSTRUCTOR EXPR after pack expansion could have no
3755 elements. */
3756
3757 static bool
3758 maybe_zero_constructor_nelts (tree expr)
3759 {
3760 if (CONSTRUCTOR_NELTS (expr) == 0)
3761 return true;
3762 if (!processing_template_decl)
3763 return false;
3764 for (constructor_elt &elt : CONSTRUCTOR_ELTS (expr))
3765 if (!PACK_EXPANSION_P (elt.value))
3766 return false;
3767 return true;
3768 }
3769
3770 /* Finish a compound-literal expression or C++11 functional cast with aggregate
3771 initializer. TYPE is the type to which the CONSTRUCTOR in COMPOUND_LITERAL
3772 is being cast. */
3773
3774 tree
3775 finish_compound_literal (tree type, tree compound_literal,
3776 tsubst_flags_t complain,
3777 fcl_t fcl_context)
3778 {
3779 if (type == error_mark_node)
3780 return error_mark_node;
3781
3782 if (TYPE_REF_P (type))
3783 {
3784 compound_literal
3785 = finish_compound_literal (TREE_TYPE (type), compound_literal,
3786 complain, fcl_context);
3787 /* The prvalue is then used to direct-initialize the reference. */
3788 tree r = (perform_implicit_conversion_flags
3789 (type, compound_literal, complain, LOOKUP_NORMAL));
3790 return convert_from_reference (r);
3791 }
3792
3793 if (!TYPE_OBJ_P (type))
3794 {
3795 /* DR2351 */
3796 if (VOID_TYPE_P (type) && CONSTRUCTOR_NELTS (compound_literal) == 0)
3797 {
3798 if (!processing_template_decl)
3799 return void_node;
3800 TREE_TYPE (compound_literal) = type;
3801 TREE_HAS_CONSTRUCTOR (compound_literal) = 1;
3802 CONSTRUCTOR_IS_DEPENDENT (compound_literal) = 0;
3803 return compound_literal;
3804 }
3805 else if (VOID_TYPE_P (type)
3806 && processing_template_decl
3807 && maybe_zero_constructor_nelts (compound_literal))
3808 /* If there are only packs in compound_literal, it could
3809 be void{} after pack expansion. */;
3810 else
3811 {
3812 if (complain & tf_error)
3813 error ("compound literal of non-object type %qT", type);
3814 return error_mark_node;
3815 }
3816 }
3817
3818 if (template_placeholder_p (type))
3819 {
3820 type = do_auto_deduction (type, compound_literal, type, complain,
3821 adc_variable_type);
3822 if (type == error_mark_node)
3823 return error_mark_node;
3824 }
3825 /* C++23 auto{x}. */
3826 else if (is_auto (type)
3827 && !AUTO_IS_DECLTYPE (type)
3828 && CONSTRUCTOR_NELTS (compound_literal) == 1)
3829 {
3830 if (is_constrained_auto (type))
3831 {
3832 if (complain & tf_error)
3833 error ("%<auto{x}%> cannot be constrained");
3834 return error_mark_node;
3835 }
3836 else if (cxx_dialect < cxx23)
3837 {
3838 if ((complain & tf_warning_or_error) != 0)
3839 pedwarn (input_location, OPT_Wc__23_extensions,
3840 "%<auto{x}%> only available with "
3841 "%<-std=c++23%> or %<-std=gnu++23%>");
3842 }
3843 type = do_auto_deduction (type, compound_literal, type, complain,
3844 adc_variable_type);
3845 if (type == error_mark_node)
3846 return error_mark_node;
3847 }
3848
3849 /* Used to hold a copy of the compound literal in a template. */
3850 tree orig_cl = NULL_TREE;
3851
3852 if (processing_template_decl)
3853 {
3854 const bool dependent_p
3855 = (instantiation_dependent_expression_p (compound_literal)
3856 || dependent_type_p (type));
3857 if (dependent_p)
3858 /* We're about to return, no need to copy. */
3859 orig_cl = compound_literal;
3860 else
3861 /* We're going to need a copy. */
3862 orig_cl = unshare_constructor (compound_literal);
3863 TREE_TYPE (orig_cl) = type;
3864 /* Mark the expression as a compound literal. */
3865 TREE_HAS_CONSTRUCTOR (orig_cl) = 1;
3866 /* And as instantiation-dependent. */
3867 CONSTRUCTOR_IS_DEPENDENT (orig_cl) = dependent_p;
3868 if (fcl_context == fcl_c99)
3869 CONSTRUCTOR_C99_COMPOUND_LITERAL (orig_cl) = 1;
3870 /* If the compound literal is dependent, we're done for now. */
3871 if (dependent_p)
3872 return orig_cl;
3873 /* Otherwise, do go on to e.g. check narrowing. */
3874 }
3875
3876 type = complete_type (type);
3877
3878 if (TYPE_NON_AGGREGATE_CLASS (type))
3879 {
3880 /* Trying to deal with a CONSTRUCTOR instead of a TREE_LIST
3881 everywhere that deals with function arguments would be a pain, so
3882 just wrap it in a TREE_LIST. The parser set a flag so we know
3883 that it came from T{} rather than T({}). */
3884 CONSTRUCTOR_IS_DIRECT_INIT (compound_literal) = 1;
3885 compound_literal = build_tree_list (NULL_TREE, compound_literal);
3886 return build_functional_cast (input_location, type,
3887 compound_literal, complain);
3888 }
3889
3890 if (TREE_CODE (type) == ARRAY_TYPE
3891 && check_array_initializer (NULL_TREE, type, compound_literal))
3892 return error_mark_node;
3893 compound_literal = reshape_init (type, compound_literal, complain);
3894 if (SCALAR_TYPE_P (type)
3895 && !BRACE_ENCLOSED_INITIALIZER_P (compound_literal)
3896 && !check_narrowing (type, compound_literal, complain))
3897 return error_mark_node;
3898 if (TREE_CODE (type) == ARRAY_TYPE
3899 && TYPE_DOMAIN (type) == NULL_TREE)
3900 {
3901 cp_complete_array_type_or_error (&type, compound_literal,
3902 false, complain);
3903 if (type == error_mark_node)
3904 return error_mark_node;
3905 }
3906 compound_literal = digest_init_flags (type, compound_literal,
3907 LOOKUP_NORMAL | LOOKUP_NO_NARROWING,
3908 complain);
3909 if (compound_literal == error_mark_node)
3910 return error_mark_node;
3911
3912 /* If we're in a template, return the original compound literal. */
3913 if (orig_cl)
3914 return orig_cl;
3915
3916 if (TREE_CODE (compound_literal) == CONSTRUCTOR)
3917 {
3918 TREE_HAS_CONSTRUCTOR (compound_literal) = true;
3919 if (fcl_context == fcl_c99)
3920 CONSTRUCTOR_C99_COMPOUND_LITERAL (compound_literal) = 1;
3921 }
3922
3923 /* Put static/constant array temporaries in static variables. */
3924 /* FIXME all C99 compound literals should be variables rather than C++
3925 temporaries, unless they are used as an aggregate initializer. */
3926 if ((!at_function_scope_p () || CP_TYPE_CONST_P (type))
3927 && fcl_context == fcl_c99
3928 && TREE_CODE (type) == ARRAY_TYPE
3929 && !TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
3930 && initializer_constant_valid_p (compound_literal, type))
3931 {
3932 tree decl = create_temporary_var (type);
3933 DECL_CONTEXT (decl) = NULL_TREE;
3934 DECL_INITIAL (decl) = compound_literal;
3935 TREE_STATIC (decl) = 1;
3936 if (literal_type_p (type) && CP_TYPE_CONST_NON_VOLATILE_P (type))
3937 {
3938 /* 5.19 says that a constant expression can include an
3939 lvalue-rvalue conversion applied to "a glvalue of literal type
3940 that refers to a non-volatile temporary object initialized
3941 with a constant expression". Rather than try to communicate
3942 that this VAR_DECL is a temporary, just mark it constexpr. */
3943 DECL_DECLARED_CONSTEXPR_P (decl) = true;
3944 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true;
3945 TREE_CONSTANT (decl) = true;
3946 }
3947 cp_apply_type_quals_to_decl (cp_type_quals (type), decl);
3948 decl = pushdecl_top_level (decl);
3949 DECL_NAME (decl) = make_anon_name ();
3950 SET_DECL_ASSEMBLER_NAME (decl, DECL_NAME (decl));
3951 /* Make sure the destructor is callable. */
3952 tree clean = cxx_maybe_build_cleanup (decl, complain);
3953 if (clean == error_mark_node)
3954 return error_mark_node;
3955 return decl;
3956 }
3957
3958 /* Represent other compound literals with TARGET_EXPR so we produce
3959 a prvalue, and can elide copies. */
3960 if (!VECTOR_TYPE_P (type)
3961 && (TREE_CODE (compound_literal) == CONSTRUCTOR
3962 || TREE_CODE (compound_literal) == VEC_INIT_EXPR))
3963 {
3964 /* The CONSTRUCTOR is now an initializer, not a compound literal. */
3965 if (TREE_CODE (compound_literal) == CONSTRUCTOR)
3966 TREE_HAS_CONSTRUCTOR (compound_literal) = false;
3967 compound_literal = get_target_expr (compound_literal, complain);
3968 }
3969 else
3970 /* For e.g. int{42} just make sure it's a prvalue. */
3971 compound_literal = rvalue (compound_literal);
3972
3973 return compound_literal;
3974 }
3975
3976 /* Return the declaration for the function-name variable indicated by
3977 ID. */
3978
3979 tree
3980 finish_fname (tree id)
3981 {
3982 tree decl;
3983
3984 decl = fname_decl (input_location, C_RID_CODE (id), id);
3985 if (processing_template_decl && current_function_decl
3986 && decl != error_mark_node)
3987 decl = DECL_NAME (decl);
3988 return decl;
3989 }
3990
3991 /* Finish a translation unit. */
3992
3993 void
3994 finish_translation_unit (void)
3995 {
3996 /* In case there were missing closebraces,
3997 get us back to the global binding level. */
3998 pop_everything ();
3999 while (current_namespace != global_namespace)
4000 pop_namespace ();
4001
4002 /* Do file scope __FUNCTION__ et al. */
4003 finish_fname_decls ();
4004
4005 if (vec_safe_length (scope_chain->omp_declare_target_attribute))
4006 {
4007 cp_omp_declare_target_attr
4008 a = scope_chain->omp_declare_target_attribute->pop ();
4009 if (!errorcount)
4010 error ("%qs without corresponding %qs",
4011 a.device_type >= 0 ? "#pragma omp begin declare target"
4012 : "#pragma omp declare target",
4013 "#pragma omp end declare target");
4014 vec_safe_truncate (scope_chain->omp_declare_target_attribute, 0);
4015 }
4016 if (vec_safe_length (scope_chain->omp_begin_assumes))
4017 {
4018 if (!errorcount)
4019 error ("%qs without corresponding %qs",
4020 "#pragma omp begin assumes", "#pragma omp end assumes");
4021 vec_safe_truncate (scope_chain->omp_begin_assumes, 0);
4022 }
4023 }
4024
4025 /* Finish a template type parameter, specified as AGGR IDENTIFIER.
4026 Returns the parameter. */
4027
4028 tree
4029 finish_template_type_parm (tree aggr, tree identifier)
4030 {
4031 if (aggr != class_type_node)
4032 {
4033 permerror (input_location, "template type parameters must use the keyword %<class%> or %<typename%>");
4034 aggr = class_type_node;
4035 }
4036
4037 return build_tree_list (aggr, identifier);
4038 }
4039
4040 /* Finish a template template parameter, specified as AGGR IDENTIFIER.
4041 Returns the parameter. */
4042
4043 tree
4044 finish_template_template_parm (tree aggr, tree identifier)
4045 {
4046 tree decl = build_decl (input_location,
4047 TYPE_DECL, identifier, NULL_TREE);
4048
4049 tree tmpl = build_lang_decl (TEMPLATE_DECL, identifier, NULL_TREE);
4050 DECL_TEMPLATE_PARMS (tmpl) = current_template_parms;
4051 DECL_TEMPLATE_RESULT (tmpl) = decl;
4052 DECL_ARTIFICIAL (decl) = 1;
4053
4054 /* Associate the constraints with the underlying declaration,
4055 not the template. */
4056 tree constr = current_template_constraints ();
4057 set_constraints (decl, constr);
4058
4059 end_template_decl ();
4060
4061 gcc_assert (DECL_TEMPLATE_PARMS (tmpl));
4062
4063 check_default_tmpl_args (decl, DECL_TEMPLATE_PARMS (tmpl),
4064 /*is_primary=*/true, /*is_partial=*/false,
4065 /*is_friend=*/0);
4066
4067 return finish_template_type_parm (aggr, tmpl);
4068 }
4069
4070 /* ARGUMENT is the default-argument value for a template template
4071 parameter. If ARGUMENT is invalid, issue error messages and return
4072 the ERROR_MARK_NODE. Otherwise, ARGUMENT itself is returned. */
4073
4074 tree
4075 check_template_template_default_arg (tree argument)
4076 {
4077 if (TREE_CODE (argument) != TEMPLATE_DECL
4078 && TREE_CODE (argument) != TEMPLATE_TEMPLATE_PARM
4079 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
4080 {
4081 if (TREE_CODE (argument) == TYPE_DECL)
4082 {
4083 if (tree t = maybe_get_template_decl_from_type_decl (argument))
4084 if (TREE_CODE (t) == TEMPLATE_DECL)
4085 return t;
4086 error ("invalid use of type %qT as a default value for a template "
4087 "template-parameter", TREE_TYPE (argument));
4088 }
4089 else
4090 error ("invalid default argument for a template template parameter");
4091 return error_mark_node;
4092 }
4093
4094 return argument;
4095 }
4096
4097 /* Begin a class definition, as indicated by T. */
4098
4099 tree
4100 begin_class_definition (tree t)
4101 {
4102 if (error_operand_p (t) || error_operand_p (TYPE_MAIN_DECL (t)))
4103 return error_mark_node;
4104
4105 if (processing_template_parmlist && !LAMBDA_TYPE_P (t))
4106 {
4107 error ("definition of %q#T inside template parameter list", t);
4108 return error_mark_node;
4109 }
4110
4111 /* According to the C++ ABI, decimal classes defined in ISO/IEC TR 24733
4112 are passed the same as decimal scalar types. */
4113 if (TREE_CODE (t) == RECORD_TYPE
4114 && !processing_template_decl)
4115 {
4116 tree ns = TYPE_CONTEXT (t);
4117 if (ns && TREE_CODE (ns) == NAMESPACE_DECL
4118 && DECL_CONTEXT (ns) == std_node
4119 && DECL_NAME (ns)
4120 && id_equal (DECL_NAME (ns), "decimal"))
4121 {
4122 const char *n = TYPE_NAME_STRING (t);
4123 if ((strcmp (n, "decimal32") == 0)
4124 || (strcmp (n, "decimal64") == 0)
4125 || (strcmp (n, "decimal128") == 0))
4126 TYPE_TRANSPARENT_AGGR (t) = 1;
4127 }
4128 }
4129
4130 /* A non-implicit typename comes from code like:
4131
4132 template <typename T> struct A {
4133 template <typename U> struct A<T>::B ...
4134
4135 This is erroneous. */
4136 else if (TREE_CODE (t) == TYPENAME_TYPE)
4137 {
4138 error ("invalid definition of qualified type %qT", t);
4139 t = error_mark_node;
4140 }
4141
4142 if (t == error_mark_node || ! MAYBE_CLASS_TYPE_P (t))
4143 {
4144 t = make_class_type (RECORD_TYPE);
4145 pushtag (make_anon_name (), t);
4146 }
4147
4148 if (TYPE_BEING_DEFINED (t))
4149 {
4150 t = make_class_type (TREE_CODE (t));
4151 pushtag (TYPE_IDENTIFIER (t), t);
4152 }
4153
4154 if (modules_p ())
4155 {
4156 if (!module_may_redeclare (TYPE_NAME (t)))
4157 return error_mark_node;
4158 set_instantiating_module (TYPE_NAME (t));
4159 set_defining_module (TYPE_NAME (t));
4160 }
4161
4162 maybe_process_partial_specialization (t);
4163 pushclass (t);
4164 TYPE_BEING_DEFINED (t) = 1;
4165 class_binding_level->defining_class_p = 1;
4166
4167 if (flag_pack_struct)
4168 {
4169 tree v;
4170 TYPE_PACKED (t) = 1;
4171 /* Even though the type is being defined for the first time
4172 here, there might have been a forward declaration, so there
4173 might be cv-qualified variants of T. */
4174 for (v = TYPE_NEXT_VARIANT (t); v; v = TYPE_NEXT_VARIANT (v))
4175 TYPE_PACKED (v) = 1;
4176 }
4177 /* Reset the interface data, at the earliest possible
4178 moment, as it might have been set via a class foo;
4179 before. */
4180 if (! TYPE_UNNAMED_P (t))
4181 {
4182 struct c_fileinfo *finfo = \
4183 get_fileinfo (LOCATION_FILE (input_location));
4184 CLASSTYPE_INTERFACE_ONLY (t) = finfo->interface_only;
4185 SET_CLASSTYPE_INTERFACE_UNKNOWN_X
4186 (t, finfo->interface_unknown);
4187 }
4188 reset_specialization ();
4189
4190 /* Make a declaration for this class in its own scope. */
4191 build_self_reference ();
4192
4193 return t;
4194 }
4195
4196 /* Finish the member declaration given by DECL. */
4197
4198 void
4199 finish_member_declaration (tree decl)
4200 {
4201 if (decl == error_mark_node || decl == NULL_TREE)
4202 return;
4203
4204 if (decl == void_type_node)
4205 /* The COMPONENT was a friend, not a member, and so there's
4206 nothing for us to do. */
4207 return;
4208
4209 /* We should see only one DECL at a time. */
4210 gcc_assert (DECL_CHAIN (decl) == NULL_TREE);
4211
4212 /* Don't add decls after definition. */
4213 gcc_assert (TYPE_BEING_DEFINED (current_class_type)
4214 /* We can add lambda types when late parsing default
4215 arguments. */
4216 || LAMBDA_TYPE_P (TREE_TYPE (decl)));
4217
4218 /* Set up access control for DECL. */
4219 TREE_PRIVATE (decl)
4220 = (current_access_specifier == access_private_node);
4221 TREE_PROTECTED (decl)
4222 = (current_access_specifier == access_protected_node);
4223 if (TREE_CODE (decl) == TEMPLATE_DECL)
4224 {
4225 TREE_PRIVATE (DECL_TEMPLATE_RESULT (decl)) = TREE_PRIVATE (decl);
4226 TREE_PROTECTED (DECL_TEMPLATE_RESULT (decl)) = TREE_PROTECTED (decl);
4227 }
4228
4229 /* Mark the DECL as a member of the current class, unless it's
4230 a member of an enumeration. */
4231 if (TREE_CODE (decl) != CONST_DECL)
4232 DECL_CONTEXT (decl) = current_class_type;
4233
4234 /* Remember the single FIELD_DECL an anonymous aggregate type is used for. */
4235 if (TREE_CODE (decl) == FIELD_DECL
4236 && ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
4237 {
4238 gcc_assert (!ANON_AGGR_TYPE_FIELD (TYPE_MAIN_VARIANT (TREE_TYPE (decl))));
4239 ANON_AGGR_TYPE_FIELD (TYPE_MAIN_VARIANT (TREE_TYPE (decl))) = decl;
4240 }
4241
4242 if (TREE_CODE (decl) == USING_DECL)
4243 /* Avoid debug info for class-scope USING_DECLS for now, we'll
4244 call cp_emit_debug_info_for_using later. */
4245 DECL_IGNORED_P (decl) = 1;
4246
4247 /* Check for bare parameter packs in the non-static data member
4248 declaration. */
4249 if (TREE_CODE (decl) == FIELD_DECL)
4250 {
4251 if (check_for_bare_parameter_packs (TREE_TYPE (decl)))
4252 TREE_TYPE (decl) = error_mark_node;
4253 if (check_for_bare_parameter_packs (DECL_ATTRIBUTES (decl)))
4254 DECL_ATTRIBUTES (decl) = NULL_TREE;
4255 }
4256
4257 /* [dcl.link]
4258
4259 A C language linkage is ignored for the names of class members
4260 and the member function type of class member functions. */
4261 if (DECL_LANG_SPECIFIC (decl))
4262 SET_DECL_LANGUAGE (decl, lang_cplusplus);
4263
4264 bool add = false;
4265
4266 /* Functions and non-functions are added differently. */
4267 if (DECL_DECLARES_FUNCTION_P (decl))
4268 add = add_method (current_class_type, decl, false);
4269 /* Enter the DECL into the scope of the class, if the class
4270 isn't a closure (whose fields are supposed to be unnamed). */
4271 else if (CLASSTYPE_LAMBDA_EXPR (current_class_type)
4272 || maybe_push_used_methods (decl)
4273 || pushdecl_class_level (decl))
4274 add = true;
4275
4276 if (add)
4277 {
4278 /* All TYPE_DECLs go at the end of TYPE_FIELDS. Ordinary fields
4279 go at the beginning. The reason is that
4280 legacy_nonfn_member_lookup searches the list in order, and we
4281 want a field name to override a type name so that the "struct
4282 stat hack" will work. In particular:
4283
4284 struct S { enum E { }; static const int E = 5; int ary[S::E]; } s;
4285
4286 is valid. */
4287
4288 if (TREE_CODE (decl) == TYPE_DECL)
4289 TYPE_FIELDS (current_class_type)
4290 = chainon (TYPE_FIELDS (current_class_type), decl);
4291 else
4292 {
4293 DECL_CHAIN (decl) = TYPE_FIELDS (current_class_type);
4294 TYPE_FIELDS (current_class_type) = decl;
4295 }
4296
4297 maybe_add_class_template_decl_list (current_class_type, decl,
4298 /*friend_p=*/0);
4299 }
4300 }
4301
4302 /* Finish processing a complete template declaration. The PARMS are
4303 the template parameters. */
4304
4305 void
4306 finish_template_decl (tree parms)
4307 {
4308 if (parms)
4309 end_template_decl ();
4310 else
4311 end_specialization ();
4312 }
4313
4314 // Returns the template type of the class scope being entered. If we're
4315 // entering a constrained class scope. TYPE is the class template
4316 // scope being entered and we may need to match the intended type with
4317 // a constrained specialization. For example:
4318 //
4319 // template<Object T>
4320 // struct S { void f(); }; #1
4321 //
4322 // template<Object T>
4323 // void S<T>::f() { } #2
4324 //
4325 // We check, in #2, that S<T> refers precisely to the type declared by
4326 // #1 (i.e., that the constraints match). Note that the following should
4327 // be an error since there is no specialization of S<T> that is
4328 // unconstrained, but this is not diagnosed here.
4329 //
4330 // template<typename T>
4331 // void S<T>::f() { }
4332 //
4333 // We cannot diagnose this problem here since this function also matches
4334 // qualified template names that are not part of a definition. For example:
4335 //
4336 // template<Integral T, Floating_point U>
4337 // typename pair<T, U>::first_type void f(T, U);
4338 //
4339 // Here, it is unlikely that there is a partial specialization of
4340 // pair constrained for Integral and Floating_point arguments.
4341 //
4342 // The general rule is: if a constrained specialization with matching
4343 // constraints is found return that type. Also note that if TYPE is not a
4344 // class-type (e.g. a typename type), then no fixup is needed.
4345
4346 static tree
4347 fixup_template_type (tree type)
4348 {
4349 // Find the template parameter list at the a depth appropriate to
4350 // the scope we're trying to enter.
4351 tree parms = current_template_parms;
4352 int depth = template_class_depth (type);
4353 for (int n = current_template_depth; n > depth && parms; --n)
4354 parms = TREE_CHAIN (parms);
4355 if (!parms)
4356 return type;
4357 tree cur_reqs = TEMPLATE_PARMS_CONSTRAINTS (parms);
4358 tree cur_constr = build_constraints (cur_reqs, NULL_TREE);
4359
4360 // Search for a specialization whose type and constraints match.
4361 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
4362 tree specs = DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
4363 while (specs)
4364 {
4365 tree spec_constr = get_constraints (TREE_VALUE (specs));
4366
4367 // If the type and constraints match a specialization, then we
4368 // are entering that type.
4369 if (same_type_p (type, TREE_TYPE (specs))
4370 && equivalent_constraints (cur_constr, spec_constr))
4371 return TREE_TYPE (specs);
4372 specs = TREE_CHAIN (specs);
4373 }
4374
4375 // If no specialization matches, then must return the type
4376 // previously found.
4377 return type;
4378 }
4379
4380 /* Finish processing a template-id (which names a type) of the form
4381 NAME < ARGS >. Return the TYPE_DECL for the type named by the
4382 template-id. If ENTERING_SCOPE is nonzero we are about to enter
4383 the scope of template-id indicated. */
4384
4385 tree
4386 finish_template_type (tree name, tree args, int entering_scope)
4387 {
4388 tree type;
4389
4390 type = lookup_template_class (name, args,
4391 NULL_TREE, NULL_TREE,
4392 tf_warning_or_error | tf_user);
4393 if (entering_scope)
4394 type = adjust_type_for_entering_scope (type);
4395
4396 /* If we might be entering the scope of a partial specialization,
4397 find the one with the right constraints. */
4398 if (flag_concepts
4399 && entering_scope
4400 && CLASS_TYPE_P (type)
4401 && CLASSTYPE_TEMPLATE_INFO (type)
4402 && dependent_type_p (type)
4403 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type)))
4404 type = fixup_template_type (type);
4405
4406 if (type == error_mark_node)
4407 return type;
4408 else if (CLASS_TYPE_P (type) && !alias_type_or_template_p (type))
4409 return TYPE_STUB_DECL (type);
4410 else
4411 return TYPE_NAME (type);
4412 }
4413
4414 /* Finish processing a BASE_CLASS with the indicated ACCESS_SPECIFIER.
4415 Return a TREE_LIST containing the ACCESS_SPECIFIER and the
4416 BASE_CLASS, or NULL_TREE if an error occurred. The
4417 ACCESS_SPECIFIER is one of
4418 access_{default,public,protected_private}_node. For a virtual base
4419 we set TREE_TYPE. */
4420
4421 tree
4422 finish_base_specifier (tree base, tree access, bool virtual_p)
4423 {
4424 tree result;
4425
4426 if (base == error_mark_node)
4427 {
4428 error ("invalid base-class specification");
4429 result = NULL_TREE;
4430 }
4431 else if (! MAYBE_CLASS_TYPE_P (base))
4432 {
4433 error ("%qT is not a class type", base);
4434 result = NULL_TREE;
4435 }
4436 else
4437 {
4438 if (cp_type_quals (base) != 0)
4439 {
4440 /* DR 484: Can a base-specifier name a cv-qualified
4441 class type? */
4442 base = TYPE_MAIN_VARIANT (base);
4443 }
4444 result = build_tree_list (access, base);
4445 if (virtual_p)
4446 TREE_TYPE (result) = integer_type_node;
4447 }
4448
4449 return result;
4450 }
4451
4452 /* If FNS is a member function, a set of member functions, or a
4453 template-id referring to one or more member functions, return a
4454 BASELINK for FNS, incorporating the current access context.
4455 Otherwise, return FNS unchanged. */
4456
4457 tree
4458 baselink_for_fns (tree fns)
4459 {
4460 tree scope;
4461 tree cl;
4462
4463 if (BASELINK_P (fns)
4464 || error_operand_p (fns))
4465 return fns;
4466
4467 scope = ovl_scope (fns);
4468 if (!CLASS_TYPE_P (scope))
4469 return fns;
4470
4471 cl = currently_open_derived_class (scope);
4472 if (!cl)
4473 cl = scope;
4474 tree access_path = TYPE_BINFO (cl);
4475 tree conv_path = (cl == scope ? access_path
4476 : lookup_base (cl, scope, ba_any, NULL, tf_none));
4477 return build_baselink (conv_path, access_path, fns, /*optype=*/NULL_TREE);
4478 }
4479
4480 /* Returns true iff DECL is a variable from a function outside
4481 the current one. */
4482
4483 static bool
4484 outer_var_p (tree decl)
4485 {
4486 /* These should have been stripped or otherwise handled by the caller. */
4487 gcc_checking_assert (!REFERENCE_REF_P (decl));
4488
4489 return ((VAR_P (decl) || TREE_CODE (decl) == PARM_DECL)
4490 && DECL_FUNCTION_SCOPE_P (decl)
4491 /* Don't get confused by temporaries. */
4492 && DECL_NAME (decl)
4493 && (DECL_CONTEXT (decl) != current_function_decl
4494 || parsing_nsdmi ()));
4495 }
4496
4497 /* As above, but also checks that DECL is automatic. */
4498
4499 bool
4500 outer_automatic_var_p (tree decl)
4501 {
4502 return (outer_var_p (decl)
4503 && !TREE_STATIC (decl));
4504 }
4505
4506 /* DECL satisfies outer_automatic_var_p. Possibly complain about it or
4507 rewrite it for lambda capture.
4508
4509 If ODR_USE is true, we're being called from mark_use, and we complain about
4510 use of constant variables. If ODR_USE is false, we're being called for the
4511 id-expression, and we do lambda capture. */
4512
4513 tree
4514 process_outer_var_ref (tree decl, tsubst_flags_t complain, bool odr_use)
4515 {
4516 if (cp_unevaluated_operand)
4517 {
4518 tree type = TREE_TYPE (decl);
4519 if (!dependent_type_p (type)
4520 && variably_modified_type_p (type, NULL_TREE))
4521 /* VLAs are used even in unevaluated context. */;
4522 else
4523 /* It's not a use (3.2) if we're in an unevaluated context. */
4524 return decl;
4525 }
4526 if (decl == error_mark_node)
4527 return decl;
4528
4529 tree context = DECL_CONTEXT (decl);
4530 tree containing_function = current_function_decl;
4531 tree lambda_stack = NULL_TREE;
4532 tree lambda_expr = NULL_TREE;
4533 tree initializer = convert_from_reference (decl);
4534 tree var = strip_normal_capture_proxy (decl);
4535
4536 /* Mark it as used now even if the use is ill-formed. */
4537 if (!mark_used (decl, complain))
4538 return error_mark_node;
4539
4540 if (parsing_nsdmi ())
4541 containing_function = NULL_TREE;
4542
4543 if (containing_function && LAMBDA_FUNCTION_P (containing_function))
4544 {
4545 /* Check whether we've already built a proxy. */
4546 tree d = retrieve_local_specialization (var);
4547
4548 if (d && d != decl && is_capture_proxy (d))
4549 {
4550 if (DECL_CONTEXT (d) == containing_function)
4551 /* We already have an inner proxy. */
4552 return d;
4553 else
4554 /* We need to capture an outer proxy. */
4555 return process_outer_var_ref (d, complain, odr_use);
4556 }
4557 }
4558
4559 /* If we are in a lambda function, we can move out until we hit
4560 1. the context,
4561 2. a non-lambda function, or
4562 3. a non-default capturing lambda function. */
4563 while (context != containing_function
4564 /* containing_function can be null with invalid generic lambdas. */
4565 && containing_function
4566 && LAMBDA_FUNCTION_P (containing_function))
4567 {
4568 tree closure = DECL_CONTEXT (containing_function);
4569 lambda_expr = CLASSTYPE_LAMBDA_EXPR (closure);
4570
4571 if (TYPE_CLASS_SCOPE_P (closure))
4572 /* A lambda in an NSDMI (c++/64496). */
4573 break;
4574
4575 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_NONE)
4576 break;
4577
4578 lambda_stack = tree_cons (NULL_TREE, lambda_expr, lambda_stack);
4579
4580 containing_function = decl_function_context (containing_function);
4581 }
4582
4583 /* In a lambda within a template, wait until instantiation time to implicitly
4584 capture a parameter pack. We want to wait because we don't know if we're
4585 capturing the whole pack or a single element, and it's OK to wait because
4586 find_parameter_packs_r walks into the lambda body. */
4587 if (context == containing_function
4588 && DECL_PACK_P (decl))
4589 return decl;
4590
4591 if (lambda_expr && VAR_P (decl) && DECL_ANON_UNION_VAR_P (decl))
4592 {
4593 if (complain & tf_error)
4594 error ("cannot capture member %qD of anonymous union", decl);
4595 return error_mark_node;
4596 }
4597 /* Do lambda capture when processing the id-expression, not when
4598 odr-using a variable. */
4599 if (!odr_use && context == containing_function)
4600 decl = add_default_capture (lambda_stack,
4601 /*id=*/DECL_NAME (decl), initializer);
4602 /* Only an odr-use of an outer automatic variable causes an
4603 error, and a constant variable can decay to a prvalue
4604 constant without odr-use. So don't complain yet. */
4605 else if (!odr_use && decl_constant_var_p (var))
4606 return var;
4607 else if (lambda_expr)
4608 {
4609 if (complain & tf_error)
4610 {
4611 auto_diagnostic_group d;
4612 error ("%qD is not captured", decl);
4613 tree closure = LAMBDA_EXPR_CLOSURE (lambda_expr);
4614 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_NONE)
4615 inform (location_of (closure),
4616 "the lambda has no capture-default");
4617 else if (TYPE_CLASS_SCOPE_P (closure))
4618 inform (UNKNOWN_LOCATION, "lambda in local class %q+T cannot "
4619 "capture variables from the enclosing context",
4620 TYPE_CONTEXT (closure));
4621 inform (DECL_SOURCE_LOCATION (decl), "%q#D declared here", decl);
4622 }
4623 return error_mark_node;
4624 }
4625 else if (processing_contract_condition && (TREE_CODE (decl) == PARM_DECL))
4626 /* Use of a parameter in a contract condition is fine. */
4627 return decl;
4628 else
4629 {
4630 if (complain & tf_error)
4631 {
4632 auto_diagnostic_group d;
4633 error (VAR_P (decl)
4634 ? G_("use of local variable with automatic storage from "
4635 "containing function")
4636 : G_("use of parameter from containing function"));
4637 inform (DECL_SOURCE_LOCATION (decl), "%q#D declared here", decl);
4638 }
4639 return error_mark_node;
4640 }
4641 return decl;
4642 }
4643
4644 /* ID_EXPRESSION is a representation of parsed, but unprocessed,
4645 id-expression. (See cp_parser_id_expression for details.) SCOPE,
4646 if non-NULL, is the type or namespace used to explicitly qualify
4647 ID_EXPRESSION. DECL is the entity to which that name has been
4648 resolved.
4649
4650 *CONSTANT_EXPRESSION_P is true if we are presently parsing a
4651 constant-expression. In that case, *NON_CONSTANT_EXPRESSION_P will
4652 be set to true if this expression isn't permitted in a
4653 constant-expression, but it is otherwise not set by this function.
4654 *ALLOW_NON_CONSTANT_EXPRESSION_P is true if we are parsing a
4655 constant-expression, but a non-constant expression is also
4656 permissible.
4657
4658 DONE is true if this expression is a complete postfix-expression;
4659 it is false if this expression is followed by '->', '[', '(', etc.
4660 ADDRESS_P is true iff this expression is the operand of '&'.
4661 TEMPLATE_P is true iff the qualified-id was of the form
4662 "A::template B". TEMPLATE_ARG_P is true iff this qualified name
4663 appears as a template argument.
4664
4665 If an error occurs, and it is the kind of error that might cause
4666 the parser to abort a tentative parse, *ERROR_MSG is filled in. It
4667 is the caller's responsibility to issue the message. *ERROR_MSG
4668 will be a string with static storage duration, so the caller need
4669 not "free" it.
4670
4671 Return an expression for the entity, after issuing appropriate
4672 diagnostics. This function is also responsible for transforming a
4673 reference to a non-static member into a COMPONENT_REF that makes
4674 the use of "this" explicit.
4675
4676 Upon return, *IDK will be filled in appropriately. */
4677 static cp_expr
4678 finish_id_expression_1 (tree id_expression,
4679 tree decl,
4680 tree scope,
4681 cp_id_kind *idk,
4682 bool integral_constant_expression_p,
4683 bool allow_non_integral_constant_expression_p,
4684 bool *non_integral_constant_expression_p,
4685 bool template_p,
4686 bool done,
4687 bool address_p,
4688 bool template_arg_p,
4689 const char **error_msg,
4690 location_t location)
4691 {
4692 decl = strip_using_decl (decl);
4693
4694 /* Initialize the output parameters. */
4695 *idk = CP_ID_KIND_NONE;
4696 *error_msg = NULL;
4697
4698 if (id_expression == error_mark_node)
4699 return error_mark_node;
4700 /* If we have a template-id, then no further lookup is
4701 required. If the template-id was for a template-class, we
4702 will sometimes have a TYPE_DECL at this point. */
4703 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
4704 || TREE_CODE (decl) == TYPE_DECL)
4705 ;
4706 /* Look up the name. */
4707 else
4708 {
4709 if (decl == error_mark_node)
4710 {
4711 /* Name lookup failed. */
4712 if (scope
4713 && (!TYPE_P (scope)
4714 || (!dependentish_scope_p (scope)
4715 && !(identifier_p (id_expression)
4716 && IDENTIFIER_CONV_OP_P (id_expression)
4717 && dependent_type_p (TREE_TYPE (id_expression))))))
4718 {
4719 /* If the qualifying type is non-dependent (and the name
4720 does not name a conversion operator to a dependent
4721 type), issue an error. */
4722 qualified_name_lookup_error (scope, id_expression, decl, location);
4723 return error_mark_node;
4724 }
4725 else if (!scope)
4726 {
4727 /* It may be resolved via Koenig lookup. */
4728 *idk = CP_ID_KIND_UNQUALIFIED;
4729 return id_expression;
4730 }
4731 else
4732 decl = id_expression;
4733 }
4734
4735 /* Remember that the name was used in the definition of
4736 the current class so that we can check later to see if
4737 the meaning would have been different after the class
4738 was entirely defined. */
4739 if (!scope && decl != error_mark_node && identifier_p (id_expression))
4740 maybe_note_name_used_in_class (id_expression, decl);
4741
4742 /* A use in unevaluated operand might not be instantiated appropriately
4743 if tsubst_copy builds a dummy parm, or if we never instantiate a
4744 generic lambda, so mark it now. */
4745 if (processing_template_decl && cp_unevaluated_operand)
4746 mark_type_use (decl);
4747
4748 /* Disallow uses of local variables from containing functions, except
4749 within lambda-expressions. */
4750 if (outer_automatic_var_p (decl))
4751 {
4752 decl = process_outer_var_ref (decl, tf_warning_or_error);
4753 if (decl == error_mark_node)
4754 return error_mark_node;
4755 }
4756
4757 /* Also disallow uses of function parameters outside the function
4758 body, except inside an unevaluated context (i.e. decltype). */
4759 if (TREE_CODE (decl) == PARM_DECL
4760 && DECL_CONTEXT (decl) == NULL_TREE
4761 && !CONSTRAINT_VAR_P (decl)
4762 && !cp_unevaluated_operand
4763 && !processing_contract_condition
4764 && !processing_omp_trait_property_expr)
4765 {
4766 *error_msg = G_("use of parameter outside function body");
4767 return error_mark_node;
4768 }
4769 }
4770
4771 /* If we didn't find anything, or what we found was a type,
4772 then this wasn't really an id-expression. */
4773 if (TREE_CODE (decl) == TEMPLATE_DECL
4774 && !DECL_FUNCTION_TEMPLATE_P (decl))
4775 {
4776 *error_msg = G_("missing template arguments");
4777 return error_mark_node;
4778 }
4779 else if (TREE_CODE (decl) == TYPE_DECL
4780 || TREE_CODE (decl) == NAMESPACE_DECL)
4781 {
4782 *error_msg = G_("expected primary-expression");
4783 return error_mark_node;
4784 }
4785
4786 /* If the name resolved to a template parameter, there is no
4787 need to look it up again later. */
4788 if ((TREE_CODE (decl) == CONST_DECL && DECL_TEMPLATE_PARM_P (decl))
4789 || TREE_CODE (decl) == TEMPLATE_PARM_INDEX)
4790 {
4791 tree r;
4792
4793 *idk = CP_ID_KIND_NONE;
4794 if (TREE_CODE (decl) == TEMPLATE_PARM_INDEX)
4795 decl = TEMPLATE_PARM_DECL (decl);
4796 r = DECL_INITIAL (decl);
4797 if (CLASS_TYPE_P (TREE_TYPE (r)) && !CP_TYPE_CONST_P (TREE_TYPE (r)))
4798 {
4799 /* If the entity is a template parameter object for a template
4800 parameter of type T, the type of the expression is const T. */
4801 tree ctype = TREE_TYPE (r);
4802 ctype = cp_build_qualified_type (ctype, (cp_type_quals (ctype)
4803 | TYPE_QUAL_CONST));
4804 r = build1 (VIEW_CONVERT_EXPR, ctype, r);
4805 }
4806 r = convert_from_reference (r);
4807 if (integral_constant_expression_p
4808 && !dependent_type_p (TREE_TYPE (decl))
4809 && !(INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (r))))
4810 {
4811 if (!allow_non_integral_constant_expression_p)
4812 error ("template parameter %qD of type %qT is not allowed in "
4813 "an integral constant expression because it is not of "
4814 "integral or enumeration type", decl, TREE_TYPE (decl));
4815 *non_integral_constant_expression_p = true;
4816 }
4817 return r;
4818 }
4819 else if (TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE)
4820 {
4821 gcc_checking_assert (scope);
4822 *idk = CP_ID_KIND_QUALIFIED;
4823 cp_warn_deprecated_use_scopes (scope);
4824 decl = finish_qualified_id_expr (scope, decl, done, address_p,
4825 template_p, template_arg_p,
4826 tf_warning_or_error);
4827 }
4828 else
4829 {
4830 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
4831 && variable_template_p (TREE_OPERAND (decl, 0))
4832 && !concept_check_p (decl))
4833 /* Try resolving this variable TEMPLATE_ID_EXPR (which is always
4834 considered type-dependent) now, so that the dependence test that
4835 follows gives us the right answer: if it represents a non-dependent
4836 variable template-id then finish_template_variable will yield the
4837 corresponding non-dependent VAR_DECL. */
4838 decl = finish_template_variable (decl);
4839
4840 bool dependent_p = type_dependent_expression_p (decl);
4841
4842 /* If the declaration was explicitly qualified indicate
4843 that. The semantics of `A::f(3)' are different than
4844 `f(3)' if `f' is virtual. */
4845 *idk = (scope
4846 ? CP_ID_KIND_QUALIFIED
4847 : (TREE_CODE (decl) == TEMPLATE_ID_EXPR
4848 ? CP_ID_KIND_TEMPLATE_ID
4849 : (dependent_p
4850 ? CP_ID_KIND_UNQUALIFIED_DEPENDENT
4851 : CP_ID_KIND_UNQUALIFIED)));
4852
4853 if (dependent_p
4854 && !scope
4855 && DECL_P (decl)
4856 && any_dependent_type_attributes_p (DECL_ATTRIBUTES (decl)))
4857 /* Dependent type attributes on the decl mean that the TREE_TYPE is
4858 wrong, so just return the identifier. */
4859 return id_expression;
4860
4861 if (DECL_CLASS_TEMPLATE_P (decl))
4862 {
4863 error ("use of class template %qT as expression", decl);
4864 return error_mark_node;
4865 }
4866
4867 if (TREE_CODE (decl) == TREE_LIST)
4868 {
4869 /* Ambiguous reference to base members. */
4870 auto_diagnostic_group d;
4871 error ("request for member %qD is ambiguous in "
4872 "multiple inheritance lattice", id_expression);
4873 print_candidates (decl);
4874 return error_mark_node;
4875 }
4876
4877 /* Mark variable-like entities as used. Functions are similarly
4878 marked either below or after overload resolution. */
4879 if ((VAR_P (decl)
4880 || TREE_CODE (decl) == PARM_DECL
4881 || TREE_CODE (decl) == CONST_DECL
4882 || TREE_CODE (decl) == RESULT_DECL)
4883 && !mark_used (decl))
4884 return error_mark_node;
4885
4886 /* Only certain kinds of names are allowed in constant
4887 expression. Template parameters have already
4888 been handled above. */
4889 if (! error_operand_p (decl)
4890 && !dependent_p
4891 && integral_constant_expression_p
4892 && !decl_constant_var_p (decl)
4893 && TREE_CODE (decl) != CONST_DECL
4894 && !builtin_valid_in_constant_expr_p (decl)
4895 && !concept_check_p (decl))
4896 {
4897 if (!allow_non_integral_constant_expression_p)
4898 {
4899 error ("%qD cannot appear in a constant-expression", decl);
4900 return error_mark_node;
4901 }
4902 *non_integral_constant_expression_p = true;
4903 }
4904
4905 if (tree wrap = maybe_get_tls_wrapper_call (decl))
4906 /* Replace an evaluated use of the thread_local variable with
4907 a call to its wrapper. */
4908 decl = wrap;
4909 else if (concept_check_p (decl))
4910 {
4911 /* Nothing more to do. All of the analysis for concept checks
4912 is done by build_conept_id, called from the parser. */
4913 }
4914 else if (scope)
4915 {
4916 if (TREE_CODE (decl) == SCOPE_REF)
4917 {
4918 gcc_assert (same_type_p (scope, TREE_OPERAND (decl, 0)));
4919 decl = TREE_OPERAND (decl, 1);
4920 }
4921
4922 decl = (adjust_result_of_qualified_name_lookup
4923 (decl, scope, current_nonlambda_class_type()));
4924
4925 cp_warn_deprecated_use_scopes (scope);
4926
4927 if (TYPE_P (scope))
4928 decl = finish_qualified_id_expr (scope,
4929 decl,
4930 done,
4931 address_p,
4932 template_p,
4933 template_arg_p,
4934 tf_warning_or_error);
4935 else
4936 decl = convert_from_reference (decl);
4937 }
4938 else if (TREE_CODE (decl) == FIELD_DECL)
4939 {
4940 /* Since SCOPE is NULL here, this is an unqualified name.
4941 Access checking has been performed during name lookup
4942 already. Turn off checking to avoid duplicate errors. */
4943 push_deferring_access_checks (dk_no_check);
4944 decl = finish_non_static_data_member (decl, NULL_TREE,
4945 /*qualifying_scope=*/NULL_TREE);
4946 pop_deferring_access_checks ();
4947 }
4948 else if (is_overloaded_fn (decl))
4949 {
4950 /* We only need to look at the first function,
4951 because all the fns share the attribute we're
4952 concerned with (all member fns or all non-members). */
4953 tree first_fn = get_first_fn (decl);
4954 first_fn = STRIP_TEMPLATE (first_fn);
4955
4956 if (!template_arg_p
4957 && (TREE_CODE (first_fn) == USING_DECL
4958 || (TREE_CODE (first_fn) == FUNCTION_DECL
4959 && DECL_FUNCTION_MEMBER_P (first_fn)
4960 && !shared_member_p (decl))))
4961 {
4962 /* A set of member functions. */
4963 decl = maybe_dummy_object (DECL_CONTEXT (first_fn), 0);
4964 return finish_class_member_access_expr (decl, id_expression,
4965 /*template_p=*/false,
4966 tf_warning_or_error);
4967 }
4968
4969 decl = baselink_for_fns (decl);
4970 }
4971 else
4972 {
4973 if (DECL_P (decl) && DECL_NONLOCAL (decl)
4974 && DECL_CLASS_SCOPE_P (decl))
4975 {
4976 tree context = context_for_name_lookup (decl);
4977 if (context != current_class_type)
4978 {
4979 tree path = currently_open_derived_class (context);
4980 if (!path)
4981 /* PATH can be null for using an enum of an unrelated
4982 class; we checked its access in lookup_using_decl.
4983
4984 ??? Should this case make a clone instead, like
4985 handle_using_decl? */
4986 gcc_assert (TREE_CODE (decl) == CONST_DECL);
4987 else
4988 perform_or_defer_access_check (TYPE_BINFO (path),
4989 decl, decl,
4990 tf_warning_or_error);
4991 }
4992 }
4993
4994 decl = convert_from_reference (decl);
4995 }
4996 }
4997
4998 return cp_expr (decl, location);
4999 }
5000
5001 /* As per finish_id_expression_1, but adding a wrapper node
5002 around the result if needed to express LOCATION. */
5003
5004 cp_expr
5005 finish_id_expression (tree id_expression,
5006 tree decl,
5007 tree scope,
5008 cp_id_kind *idk,
5009 bool integral_constant_expression_p,
5010 bool allow_non_integral_constant_expression_p,
5011 bool *non_integral_constant_expression_p,
5012 bool template_p,
5013 bool done,
5014 bool address_p,
5015 bool template_arg_p,
5016 const char **error_msg,
5017 location_t location)
5018 {
5019 cp_expr result
5020 = finish_id_expression_1 (id_expression, decl, scope, idk,
5021 integral_constant_expression_p,
5022 allow_non_integral_constant_expression_p,
5023 non_integral_constant_expression_p,
5024 template_p, done, address_p, template_arg_p,
5025 error_msg, location);
5026 return result.maybe_add_location_wrapper ();
5027 }
5028
5029 /* Implement the __typeof keyword: Return the type of EXPR, suitable for
5030 use as a type-specifier. */
5031
5032 tree
5033 finish_typeof (tree expr)
5034 {
5035 tree type;
5036
5037 if (type_dependent_expression_p (expr))
5038 {
5039 type = cxx_make_type (TYPEOF_TYPE);
5040 TYPEOF_TYPE_EXPR (type) = expr;
5041 SET_TYPE_STRUCTURAL_EQUALITY (type);
5042
5043 return type;
5044 }
5045
5046 expr = mark_type_use (expr);
5047
5048 type = unlowered_expr_type (expr);
5049
5050 if (!type || type == unknown_type_node)
5051 {
5052 error ("type of %qE is unknown", expr);
5053 return error_mark_node;
5054 }
5055
5056 return type;
5057 }
5058
5059 /* Implement the __underlying_type keyword: Return the underlying
5060 type of TYPE, suitable for use as a type-specifier. */
5061
5062 tree
5063 finish_underlying_type (tree type)
5064 {
5065 if (!complete_type_or_else (type, NULL_TREE))
5066 return error_mark_node;
5067
5068 if (TREE_CODE (type) != ENUMERAL_TYPE)
5069 {
5070 error ("%qT is not an enumeration type", type);
5071 return error_mark_node;
5072 }
5073
5074 tree underlying_type = ENUM_UNDERLYING_TYPE (type);
5075
5076 /* Fixup necessary in this case because ENUM_UNDERLYING_TYPE
5077 includes TYPE_MIN_VALUE and TYPE_MAX_VALUE information.
5078 See finish_enum_value_list for details. */
5079 if (!ENUM_FIXED_UNDERLYING_TYPE_P (type))
5080 underlying_type
5081 = c_common_type_for_mode (TYPE_MODE (underlying_type),
5082 TYPE_UNSIGNED (underlying_type));
5083
5084 return underlying_type;
5085 }
5086
5087 /* Implement the __type_pack_element keyword: Return the type
5088 at index IDX within TYPES. */
5089
5090 static tree
5091 finish_type_pack_element (tree idx, tree types, tsubst_flags_t complain)
5092 {
5093 idx = maybe_constant_value (idx, NULL_TREE, mce_true);
5094 if (TREE_CODE (idx) != INTEGER_CST || !INTEGRAL_TYPE_P (TREE_TYPE (idx)))
5095 {
5096 if (complain & tf_error)
5097 error ("pack index is not an integral constant");
5098 return error_mark_node;
5099 }
5100 if (tree_int_cst_sgn (idx) < 0)
5101 {
5102 if (complain & tf_error)
5103 error ("pack index is negative");
5104 return error_mark_node;
5105 }
5106 if (wi::to_widest (idx) >= TREE_VEC_LENGTH (types))
5107 {
5108 if (complain & tf_error)
5109 error ("pack index is out of range");
5110 return error_mark_node;
5111 }
5112 return TREE_VEC_ELT (types, tree_to_shwi (idx));
5113 }
5114
5115 /* In a pack-index T...[N], return the element at index IDX within TYPES.
5116 PARENTHESIZED_P is true iff the pack index was wrapped in (). */
5117
5118 tree
5119 pack_index_element (tree idx, tree types, bool parenthesized_p,
5120 tsubst_flags_t complain)
5121 {
5122 tree r = finish_type_pack_element (idx, types, complain);
5123 if (parenthesized_p)
5124 /* For the benefit of decltype(auto). */
5125 r = force_paren_expr (r);
5126 return r;
5127 }
5128
5129 /* Implement the __direct_bases keyword: Return the direct base classes
5130 of type. */
5131
5132 tree
5133 calculate_direct_bases (tree type, tsubst_flags_t complain)
5134 {
5135 if (!complete_type_or_maybe_complain (type, NULL_TREE, complain)
5136 || !NON_UNION_CLASS_TYPE_P (type))
5137 return make_tree_vec (0);
5138
5139 releasing_vec vector;
5140 vec<tree, va_gc> *base_binfos = BINFO_BASE_BINFOS (TYPE_BINFO (type));
5141 tree binfo;
5142 unsigned i;
5143
5144 /* Virtual bases are initialized first */
5145 for (i = 0; base_binfos->iterate (i, &binfo); i++)
5146 if (BINFO_VIRTUAL_P (binfo))
5147 vec_safe_push (vector, binfo);
5148
5149 /* Now non-virtuals */
5150 for (i = 0; base_binfos->iterate (i, &binfo); i++)
5151 if (!BINFO_VIRTUAL_P (binfo))
5152 vec_safe_push (vector, binfo);
5153
5154 tree bases_vec = make_tree_vec (vector->length ());
5155
5156 for (i = 0; i < vector->length (); ++i)
5157 TREE_VEC_ELT (bases_vec, i) = BINFO_TYPE ((*vector)[i]);
5158
5159 return bases_vec;
5160 }
5161
5162 /* Implement the __bases keyword: Return the base classes
5163 of type */
5164
5165 /* Find morally non-virtual base classes by walking binfo hierarchy */
5166 /* Virtual base classes are handled separately in finish_bases */
5167
5168 static tree
5169 dfs_calculate_bases_pre (tree binfo, void * /*data_*/)
5170 {
5171 /* Don't walk bases of virtual bases */
5172 return BINFO_VIRTUAL_P (binfo) ? dfs_skip_bases : NULL_TREE;
5173 }
5174
5175 static tree
5176 dfs_calculate_bases_post (tree binfo, void *data_)
5177 {
5178 vec<tree, va_gc> **data = ((vec<tree, va_gc> **) data_);
5179 if (!BINFO_VIRTUAL_P (binfo))
5180 vec_safe_push (*data, BINFO_TYPE (binfo));
5181 return NULL_TREE;
5182 }
5183
5184 /* Calculates the morally non-virtual base classes of a class */
5185 static vec<tree, va_gc> *
5186 calculate_bases_helper (tree type)
5187 {
5188 vec<tree, va_gc> *vector = make_tree_vector ();
5189
5190 /* Now add non-virtual base classes in order of construction */
5191 if (TYPE_BINFO (type))
5192 dfs_walk_all (TYPE_BINFO (type),
5193 dfs_calculate_bases_pre, dfs_calculate_bases_post, &vector);
5194 return vector;
5195 }
5196
5197 tree
5198 calculate_bases (tree type, tsubst_flags_t complain)
5199 {
5200 if (!complete_type_or_maybe_complain (type, NULL_TREE, complain)
5201 || !NON_UNION_CLASS_TYPE_P (type))
5202 return make_tree_vec (0);
5203
5204 releasing_vec vector;
5205 tree bases_vec = NULL_TREE;
5206 unsigned i;
5207 vec<tree, va_gc> *vbases;
5208 tree binfo;
5209
5210 /* First go through virtual base classes */
5211 for (vbases = CLASSTYPE_VBASECLASSES (type), i = 0;
5212 vec_safe_iterate (vbases, i, &binfo); i++)
5213 {
5214 releasing_vec vbase_bases
5215 = calculate_bases_helper (BINFO_TYPE (binfo));
5216 vec_safe_splice (vector, vbase_bases);
5217 }
5218
5219 /* Now for the non-virtual bases */
5220 releasing_vec nonvbases = calculate_bases_helper (type);
5221 vec_safe_splice (vector, nonvbases);
5222
5223 /* Note that during error recovery vector->length can even be zero. */
5224 if (vector->length () > 1)
5225 {
5226 /* Last element is entire class, so don't copy */
5227 bases_vec = make_tree_vec (vector->length () - 1);
5228
5229 for (i = 0; i < vector->length () - 1; ++i)
5230 TREE_VEC_ELT (bases_vec, i) = (*vector)[i];
5231 }
5232 else
5233 bases_vec = make_tree_vec (0);
5234
5235 return bases_vec;
5236 }
5237
5238 tree
5239 finish_bases (tree type, bool direct)
5240 {
5241 tree bases = NULL_TREE;
5242
5243 if (!processing_template_decl)
5244 {
5245 /* Parameter packs can only be used in templates */
5246 error ("parameter pack %<__bases%> only valid in template declaration");
5247 return error_mark_node;
5248 }
5249
5250 bases = cxx_make_type (BASES);
5251 BASES_TYPE (bases) = type;
5252 BASES_DIRECT (bases) = direct;
5253 SET_TYPE_STRUCTURAL_EQUALITY (bases);
5254
5255 return bases;
5256 }
5257
5258 /* Perform C++-specific checks for __builtin_offsetof before calling
5259 fold_offsetof. */
5260
5261 tree
5262 finish_offsetof (tree object_ptr, tree expr, location_t loc)
5263 {
5264 /* If we're processing a template, we can't finish the semantics yet.
5265 Otherwise we can fold the entire expression now. */
5266 if (processing_template_decl)
5267 {
5268 expr = build2 (OFFSETOF_EXPR, size_type_node, expr, object_ptr);
5269 SET_EXPR_LOCATION (expr, loc);
5270 return expr;
5271 }
5272
5273 if (expr == error_mark_node)
5274 return error_mark_node;
5275
5276 if (TREE_CODE (expr) == PSEUDO_DTOR_EXPR)
5277 {
5278 error ("cannot apply %<offsetof%> to destructor %<~%T%>",
5279 TREE_OPERAND (expr, 2));
5280 return error_mark_node;
5281 }
5282 if (FUNC_OR_METHOD_TYPE_P (TREE_TYPE (expr))
5283 || TREE_TYPE (expr) == unknown_type_node)
5284 {
5285 while (TREE_CODE (expr) == COMPONENT_REF
5286 || TREE_CODE (expr) == COMPOUND_EXPR)
5287 expr = TREE_OPERAND (expr, 1);
5288
5289 if (DECL_P (expr))
5290 {
5291 auto_diagnostic_group d;
5292 error ("cannot apply %<offsetof%> to member function %qD", expr);
5293 inform (DECL_SOURCE_LOCATION (expr), "declared here");
5294 }
5295 else
5296 error ("cannot apply %<offsetof%> to member function");
5297 return error_mark_node;
5298 }
5299 if (TREE_CODE (expr) == CONST_DECL)
5300 {
5301 error ("cannot apply %<offsetof%> to an enumerator %qD", expr);
5302 return error_mark_node;
5303 }
5304 if (REFERENCE_REF_P (expr))
5305 expr = TREE_OPERAND (expr, 0);
5306 if (!complete_type_or_else (TREE_TYPE (TREE_TYPE (object_ptr)), object_ptr))
5307 return error_mark_node;
5308 if (warn_invalid_offsetof
5309 && CLASS_TYPE_P (TREE_TYPE (TREE_TYPE (object_ptr)))
5310 && CLASSTYPE_NON_STD_LAYOUT (TREE_TYPE (TREE_TYPE (object_ptr)))
5311 && cp_unevaluated_operand == 0)
5312 warning_at (loc, OPT_Winvalid_offsetof, "%<offsetof%> within "
5313 "non-standard-layout type %qT is conditionally-supported",
5314 TREE_TYPE (TREE_TYPE (object_ptr)));
5315 return fold_offsetof (expr);
5316 }
5317
5318 /* Replace the AGGR_INIT_EXPR at *TP with an equivalent CALL_EXPR. This
5319 function is broken out from the above for the benefit of the tree-ssa
5320 project. */
5321
5322 void
5323 simplify_aggr_init_expr (tree *tp)
5324 {
5325 tree aggr_init_expr = *tp;
5326
5327 /* Form an appropriate CALL_EXPR. */
5328 tree fn = AGGR_INIT_EXPR_FN (aggr_init_expr);
5329 tree slot = AGGR_INIT_EXPR_SLOT (aggr_init_expr);
5330 tree type = TREE_TYPE (slot);
5331
5332 tree call_expr;
5333 enum style_t { ctor, arg, pcc } style;
5334
5335 if (AGGR_INIT_VIA_CTOR_P (aggr_init_expr))
5336 style = ctor;
5337 #ifdef PCC_STATIC_STRUCT_RETURN
5338 else if (1)
5339 style = pcc;
5340 #endif
5341 else
5342 {
5343 gcc_assert (TREE_ADDRESSABLE (type));
5344 style = arg;
5345 }
5346
5347 call_expr = build_call_array_loc (input_location,
5348 TREE_TYPE (TREE_TYPE (TREE_TYPE (fn))),
5349 fn,
5350 aggr_init_expr_nargs (aggr_init_expr),
5351 AGGR_INIT_EXPR_ARGP (aggr_init_expr));
5352 TREE_NOTHROW (call_expr) = TREE_NOTHROW (aggr_init_expr);
5353 CALL_FROM_THUNK_P (call_expr) = AGGR_INIT_FROM_THUNK_P (aggr_init_expr);
5354 CALL_EXPR_OPERATOR_SYNTAX (call_expr)
5355 = CALL_EXPR_OPERATOR_SYNTAX (aggr_init_expr);
5356 CALL_EXPR_ORDERED_ARGS (call_expr) = CALL_EXPR_ORDERED_ARGS (aggr_init_expr);
5357 CALL_EXPR_REVERSE_ARGS (call_expr) = CALL_EXPR_REVERSE_ARGS (aggr_init_expr);
5358 CALL_EXPR_MUST_TAIL_CALL (call_expr) = AGGR_INIT_EXPR_MUST_TAIL (aggr_init_expr);
5359
5360 if (style == ctor)
5361 {
5362 /* Replace the first argument to the ctor with the address of the
5363 slot. */
5364 cxx_mark_addressable (slot);
5365 CALL_EXPR_ARG (call_expr, 0) =
5366 build1 (ADDR_EXPR, build_pointer_type (type), slot);
5367 }
5368 else if (style == arg)
5369 {
5370 /* Just mark it addressable here, and leave the rest to
5371 expand_call{,_inline}. */
5372 cxx_mark_addressable (slot);
5373 CALL_EXPR_RETURN_SLOT_OPT (call_expr) = true;
5374 call_expr = cp_build_init_expr (slot, call_expr);
5375 }
5376 else if (style == pcc)
5377 {
5378 /* If we're using the non-reentrant PCC calling convention, then we
5379 need to copy the returned value out of the static buffer into the
5380 SLOT. */
5381 push_deferring_access_checks (dk_no_check);
5382 call_expr = build_aggr_init (slot, call_expr,
5383 DIRECT_BIND | LOOKUP_ONLYCONVERTING,
5384 tf_warning_or_error);
5385 pop_deferring_access_checks ();
5386 call_expr = build2 (COMPOUND_EXPR, TREE_TYPE (slot), call_expr, slot);
5387 }
5388
5389 if (AGGR_INIT_ZERO_FIRST (aggr_init_expr))
5390 {
5391 tree init = build_zero_init (type, NULL_TREE,
5392 /*static_storage_p=*/false);
5393 init = cp_build_init_expr (slot, init);
5394 call_expr = build2 (COMPOUND_EXPR, TREE_TYPE (call_expr),
5395 init, call_expr);
5396 }
5397
5398 *tp = call_expr;
5399 }
5400
5401 /* Emit all thunks to FN that should be emitted when FN is emitted. */
5402
5403 void
5404 emit_associated_thunks (tree fn)
5405 {
5406 /* When we use vcall offsets, we emit thunks with the virtual
5407 functions to which they thunk. The whole point of vcall offsets
5408 is so that you can know statically the entire set of thunks that
5409 will ever be needed for a given virtual function, thereby
5410 enabling you to output all the thunks with the function itself. */
5411 if (DECL_VIRTUAL_P (fn)
5412 /* Do not emit thunks for extern template instantiations. */
5413 && ! DECL_REALLY_EXTERN (fn)
5414 /* Do not emit thunks for tentative decls, those will be processed
5415 again at_eof if really needed. */
5416 && (DECL_INTERFACE_KNOWN (fn) || !DECL_DEFER_OUTPUT (fn)))
5417 {
5418 tree thunk;
5419
5420 for (thunk = DECL_THUNKS (fn); thunk; thunk = DECL_CHAIN (thunk))
5421 {
5422 if (!THUNK_ALIAS (thunk))
5423 {
5424 use_thunk (thunk, /*emit_p=*/1);
5425 if (DECL_RESULT_THUNK_P (thunk))
5426 {
5427 tree probe;
5428
5429 for (probe = DECL_THUNKS (thunk);
5430 probe; probe = DECL_CHAIN (probe))
5431 use_thunk (probe, /*emit_p=*/1);
5432 }
5433 }
5434 else
5435 gcc_assert (!DECL_THUNKS (thunk));
5436 }
5437 }
5438 }
5439
5440 /* Generate RTL for FN. */
5441
5442 bool
5443 expand_or_defer_fn_1 (tree fn)
5444 {
5445 /* When the parser calls us after finishing the body of a template
5446 function, we don't really want to expand the body. */
5447 if (processing_template_decl)
5448 {
5449 /* Normally, collection only occurs in rest_of_compilation. So,
5450 if we don't collect here, we never collect junk generated
5451 during the processing of templates until we hit a
5452 non-template function. It's not safe to do this inside a
5453 nested class, though, as the parser may have local state that
5454 is not a GC root. */
5455 if (!function_depth)
5456 ggc_collect ();
5457 return false;
5458 }
5459
5460 gcc_assert (DECL_SAVED_TREE (fn));
5461
5462 /* We make a decision about linkage for these functions at the end
5463 of the compilation. Until that point, we do not want the back
5464 end to output them -- but we do want it to see the bodies of
5465 these functions so that it can inline them as appropriate. */
5466 if (DECL_DECLARED_INLINE_P (fn) || DECL_IMPLICIT_INSTANTIATION (fn))
5467 {
5468 if (DECL_INTERFACE_KNOWN (fn))
5469 /* We've already made a decision as to how this function will
5470 be handled. */;
5471 else if (!at_eof
5472 || DECL_IMMEDIATE_FUNCTION_P (fn)
5473 || DECL_OMP_DECLARE_REDUCTION_P (fn))
5474 tentative_decl_linkage (fn);
5475 else
5476 import_export_decl (fn);
5477
5478 /* If the user wants us to keep all inline functions, then mark
5479 this function as needed so that finish_file will make sure to
5480 output it later. Similarly, all dllexport'd functions must
5481 be emitted; there may be callers in other DLLs. */
5482 if (DECL_DECLARED_INLINE_P (fn)
5483 && !DECL_REALLY_EXTERN (fn)
5484 && !DECL_IMMEDIATE_FUNCTION_P (fn)
5485 && !DECL_OMP_DECLARE_REDUCTION_P (fn)
5486 && (flag_keep_inline_functions
5487 || (flag_keep_inline_dllexport
5488 && lookup_attribute ("dllexport", DECL_ATTRIBUTES (fn)))))
5489 {
5490 mark_needed (fn);
5491 DECL_EXTERNAL (fn) = 0;
5492 }
5493 }
5494
5495 /* If this is a constructor or destructor body, we have to clone
5496 it. */
5497 if (maybe_clone_body (fn))
5498 {
5499 /* We don't want to process FN again, so pretend we've written
5500 it out, even though we haven't. */
5501 TREE_ASM_WRITTEN (fn) = 1;
5502 /* If this is a constexpr function we still need the body to be
5503 able to evaluate it. Similarly, with modules we only stream
5504 the maybe-in-charge cdtor and regenerate the clones from it on
5505 demand, so we also need to keep the body. Otherwise we don't
5506 need it anymore. */
5507 if (!DECL_DECLARED_CONSTEXPR_P (fn)
5508 && !(module_maybe_has_cmi_p () && vague_linkage_p (fn)))
5509 DECL_SAVED_TREE (fn) = void_node;
5510 return false;
5511 }
5512
5513 /* There's no reason to do any of the work here if we're only doing
5514 semantic analysis; this code just generates RTL. */
5515 if (flag_syntax_only)
5516 {
5517 /* Pretend that this function has been written out so that we don't try
5518 to expand it again. */
5519 TREE_ASM_WRITTEN (fn) = 1;
5520 return false;
5521 }
5522
5523 if (DECL_OMP_DECLARE_REDUCTION_P (fn))
5524 return false;
5525
5526 return true;
5527 }
5528
5529 void
5530 expand_or_defer_fn (tree fn)
5531 {
5532 if (expand_or_defer_fn_1 (fn))
5533 {
5534 function_depth++;
5535
5536 /* Expand or defer, at the whim of the compilation unit manager. */
5537 cgraph_node::finalize_function (fn, function_depth > 1);
5538 emit_associated_thunks (fn);
5539
5540 function_depth--;
5541
5542 if (DECL_IMMEDIATE_FUNCTION_P (fn))
5543 {
5544 if (cgraph_node *node = cgraph_node::get (fn))
5545 {
5546 node->body_removed = true;
5547 node->analyzed = false;
5548 node->definition = false;
5549 node->force_output = false;
5550 }
5551 }
5552 }
5553 }
5554
5555 class nrv_data
5556 {
5557 public:
5558 nrv_data () : visited (37) {}
5559
5560 tree var;
5561 tree result;
5562 hash_set<tree> visited;
5563 bool simple;
5564 bool in_nrv_cleanup;
5565 };
5566
5567 /* Helper function for walk_tree, used by finalize_nrv below. */
5568
5569 static tree
5570 finalize_nrv_r (tree* tp, int* walk_subtrees, void* data)
5571 {
5572 class nrv_data *dp = (class nrv_data *)data;
5573
5574 /* No need to walk into types. There wouldn't be any need to walk into
5575 non-statements, except that we have to consider STMT_EXPRs. */
5576 if (TYPE_P (*tp))
5577 *walk_subtrees = 0;
5578
5579 /* Replace all uses of the NRV with the RESULT_DECL. */
5580 else if (*tp == dp->var)
5581 *tp = dp->result;
5582
5583 /* Avoid walking into the same tree more than once. Unfortunately, we
5584 can't just use walk_tree_without duplicates because it would only call
5585 us for the first occurrence of dp->var in the function body. */
5586 else if (dp->visited.add (*tp))
5587 *walk_subtrees = 0;
5588
5589 /* If there's a label, we might need to destroy the NRV on goto (92407). */
5590 else if (TREE_CODE (*tp) == LABEL_EXPR && !dp->in_nrv_cleanup)
5591 dp->simple = false;
5592 /* Change NRV returns to just refer to the RESULT_DECL; this is a nop,
5593 but differs from using NULL_TREE in that it indicates that we care
5594 about the value of the RESULT_DECL. But preserve anything appended
5595 by check_return_expr. */
5596 else if (TREE_CODE (*tp) == RETURN_EXPR)
5597 {
5598 tree *p = &TREE_OPERAND (*tp, 0);
5599 while (TREE_CODE (*p) == COMPOUND_EXPR)
5600 p = &TREE_OPERAND (*p, 0);
5601 if (TREE_CODE (*p) == INIT_EXPR
5602 && INIT_EXPR_NRV_P (*p))
5603 *p = dp->result;
5604 }
5605 /* Change all cleanups for the NRV to only run when not returning. */
5606 else if (TREE_CODE (*tp) == CLEANUP_STMT
5607 && CLEANUP_DECL (*tp) == dp->var)
5608 {
5609 dp->in_nrv_cleanup = true;
5610 cp_walk_tree (&CLEANUP_BODY (*tp), finalize_nrv_r, data, 0);
5611 dp->in_nrv_cleanup = false;
5612 cp_walk_tree (&CLEANUP_EXPR (*tp), finalize_nrv_r, data, 0);
5613 *walk_subtrees = 0;
5614
5615 if (dp->simple)
5616 /* For a simple NRV, just run it on the EH path. */
5617 CLEANUP_EH_ONLY (*tp) = true;
5618 else
5619 {
5620 /* Not simple, we need to check current_retval_sentinel to decide
5621 whether to run it. If it's set, we're returning normally and
5622 don't want to destroy the NRV. If the sentinel is not set, we're
5623 leaving scope some other way, either by flowing off the end of its
5624 scope or throwing an exception. */
5625 tree cond = build3 (COND_EXPR, void_type_node,
5626 current_retval_sentinel,
5627 void_node, CLEANUP_EXPR (*tp));
5628 CLEANUP_EXPR (*tp) = cond;
5629 }
5630
5631 /* If a cleanup might throw, we need to clear current_retval_sentinel on
5632 the exception path, both so the check above succeeds and so an outer
5633 cleanup added by maybe_splice_retval_cleanup doesn't run. */
5634 if (cp_function_chain->throwing_cleanup)
5635 {
5636 tree clear = build2 (MODIFY_EXPR, boolean_type_node,
5637 current_retval_sentinel,
5638 boolean_false_node);
5639 if (dp->simple)
5640 {
5641 /* We're already only on the EH path, just prepend it. */
5642 tree &exp = CLEANUP_EXPR (*tp);
5643 exp = build2 (COMPOUND_EXPR, void_type_node, clear, exp);
5644 }
5645 else
5646 {
5647 /* The cleanup runs on both normal and EH paths, we need another
5648 CLEANUP_STMT to clear the flag only on the EH path. */
5649 tree &bod = CLEANUP_BODY (*tp);
5650 bod = build_stmt (EXPR_LOCATION (*tp), CLEANUP_STMT,
5651 bod, clear, current_retval_sentinel);
5652 CLEANUP_EH_ONLY (bod) = true;
5653 }
5654 }
5655 }
5656 /* Disable maybe_splice_retval_cleanup within the NRV cleanup scope, we don't
5657 want to destroy the retval before the variable goes out of scope. */
5658 else if (TREE_CODE (*tp) == CLEANUP_STMT
5659 && dp->in_nrv_cleanup
5660 && CLEANUP_DECL (*tp) == dp->result)
5661 CLEANUP_EXPR (*tp) = void_node;
5662 /* Replace the DECL_EXPR for the NRV with an initialization of the
5663 RESULT_DECL, if needed. */
5664 else if (TREE_CODE (*tp) == DECL_EXPR
5665 && DECL_EXPR_DECL (*tp) == dp->var)
5666 {
5667 tree init;
5668 if (DECL_INITIAL (dp->var)
5669 && DECL_INITIAL (dp->var) != error_mark_node)
5670 init = cp_build_init_expr (dp->result,
5671 DECL_INITIAL (dp->var));
5672 else
5673 init = build_empty_stmt (EXPR_LOCATION (*tp));
5674 DECL_INITIAL (dp->var) = NULL_TREE;
5675 SET_EXPR_LOCATION (init, EXPR_LOCATION (*tp));
5676 *tp = init;
5677 }
5678
5679 /* Keep iterating. */
5680 return NULL_TREE;
5681 }
5682
5683 /* Called from finish_function to implement the named return value
5684 optimization by overriding all the RETURN_EXPRs and pertinent
5685 CLEANUP_STMTs and replacing all occurrences of VAR with RESULT, the
5686 RESULT_DECL for the function. */
5687
5688 void
5689 finalize_nrv (tree fndecl, tree var)
5690 {
5691 class nrv_data data;
5692 tree result = DECL_RESULT (fndecl);
5693
5694 /* Copy name from VAR to RESULT. */
5695 DECL_NAME (result) = DECL_NAME (var);
5696 /* Don't forget that we take its address. */
5697 TREE_ADDRESSABLE (result) = TREE_ADDRESSABLE (var);
5698 /* Finally set DECL_VALUE_EXPR to avoid assigning
5699 a stack slot at -O0 for the original var and debug info
5700 uses RESULT location for VAR. */
5701 SET_DECL_VALUE_EXPR (var, result);
5702 DECL_HAS_VALUE_EXPR_P (var) = 1;
5703
5704 data.var = var;
5705 data.result = result;
5706 data.in_nrv_cleanup = false;
5707
5708 /* This is simpler for variables declared in the outer scope of
5709 the function so we know that their lifetime always ends with a
5710 return; see g++.dg/opt/nrv6.C. */
5711 tree outer = outer_curly_brace_block (fndecl);
5712 data.simple = chain_member (var, BLOCK_VARS (outer));
5713
5714 cp_walk_tree (&DECL_SAVED_TREE (fndecl), finalize_nrv_r, &data, 0);
5715 }
5716
5717 /* Create CP_OMP_CLAUSE_INFO for clause C. Returns true if it is invalid. */
5718
5719 bool
5720 cxx_omp_create_clause_info (tree c, tree type, bool need_default_ctor,
5721 bool need_copy_ctor, bool need_copy_assignment,
5722 bool need_dtor)
5723 {
5724 int save_errorcount = errorcount;
5725 tree info, t;
5726
5727 /* Always allocate 3 elements for simplicity. These are the
5728 function decls for the ctor, dtor, and assignment op.
5729 This layout is known to the three lang hooks,
5730 cxx_omp_clause_default_init, cxx_omp_clause_copy_init,
5731 and cxx_omp_clause_assign_op. */
5732 info = make_tree_vec (3);
5733 CP_OMP_CLAUSE_INFO (c) = info;
5734
5735 if (need_default_ctor || need_copy_ctor)
5736 {
5737 if (need_default_ctor)
5738 t = get_default_ctor (type);
5739 else
5740 t = get_copy_ctor (type, tf_warning_or_error);
5741
5742 if (t && !trivial_fn_p (t))
5743 TREE_VEC_ELT (info, 0) = t;
5744 }
5745
5746 if (need_dtor && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
5747 TREE_VEC_ELT (info, 1) = get_dtor (type, tf_warning_or_error);
5748
5749 if (need_copy_assignment)
5750 {
5751 t = get_copy_assign (type);
5752
5753 if (t && !trivial_fn_p (t))
5754 TREE_VEC_ELT (info, 2) = t;
5755 }
5756
5757 return errorcount != save_errorcount;
5758 }
5759
5760 /* If DECL is DECL_OMP_PRIVATIZED_MEMBER, return corresponding
5761 FIELD_DECL, otherwise return DECL itself. */
5762
5763 static tree
5764 omp_clause_decl_field (tree decl)
5765 {
5766 if (VAR_P (decl)
5767 && DECL_HAS_VALUE_EXPR_P (decl)
5768 && DECL_ARTIFICIAL (decl)
5769 && DECL_LANG_SPECIFIC (decl)
5770 && DECL_OMP_PRIVATIZED_MEMBER (decl))
5771 {
5772 tree f = DECL_VALUE_EXPR (decl);
5773 if (INDIRECT_REF_P (f))
5774 f = TREE_OPERAND (f, 0);
5775 if (TREE_CODE (f) == COMPONENT_REF)
5776 {
5777 f = TREE_OPERAND (f, 1);
5778 gcc_assert (TREE_CODE (f) == FIELD_DECL);
5779 return f;
5780 }
5781 }
5782 return NULL_TREE;
5783 }
5784
5785 /* Adjust DECL if needed for printing using %qE. */
5786
5787 static tree
5788 omp_clause_printable_decl (tree decl)
5789 {
5790 tree t = omp_clause_decl_field (decl);
5791 if (t)
5792 return t;
5793 return decl;
5794 }
5795
5796 /* For a FIELD_DECL F and corresponding DECL_OMP_PRIVATIZED_MEMBER
5797 VAR_DECL T that doesn't need a DECL_EXPR added, record it for
5798 privatization. */
5799
5800 static void
5801 omp_note_field_privatization (tree f, tree t)
5802 {
5803 if (!omp_private_member_map)
5804 omp_private_member_map = new hash_map<tree, tree>;
5805 tree &v = omp_private_member_map->get_or_insert (f);
5806 if (v == NULL_TREE)
5807 {
5808 v = t;
5809 omp_private_member_vec.safe_push (f);
5810 /* Signal that we don't want to create DECL_EXPR for this dummy var. */
5811 omp_private_member_vec.safe_push (integer_zero_node);
5812 }
5813 }
5814
5815 /* Privatize FIELD_DECL T, return corresponding DECL_OMP_PRIVATIZED_MEMBER
5816 dummy VAR_DECL. */
5817
5818 tree
5819 omp_privatize_field (tree t, bool shared)
5820 {
5821 tree m = finish_non_static_data_member (t, NULL_TREE, NULL_TREE);
5822 if (m == error_mark_node)
5823 return error_mark_node;
5824 if (!omp_private_member_map && !shared)
5825 omp_private_member_map = new hash_map<tree, tree>;
5826 if (TYPE_REF_P (TREE_TYPE (t)))
5827 {
5828 gcc_assert (INDIRECT_REF_P (m));
5829 m = TREE_OPERAND (m, 0);
5830 }
5831 tree vb = NULL_TREE;
5832 tree &v = shared ? vb : omp_private_member_map->get_or_insert (t);
5833 if (v == NULL_TREE)
5834 {
5835 v = create_temporary_var (TREE_TYPE (m));
5836 retrofit_lang_decl (v);
5837 DECL_OMP_PRIVATIZED_MEMBER (v) = 1;
5838 SET_DECL_VALUE_EXPR (v, m);
5839 DECL_HAS_VALUE_EXPR_P (v) = 1;
5840 if (!shared)
5841 omp_private_member_vec.safe_push (t);
5842 }
5843 return v;
5844 }
5845
5846 /* C++ specialisation of the c_omp_address_inspector class. */
5847
5848 class cp_omp_address_inspector : public c_omp_address_inspector
5849 {
5850 public:
5851 cp_omp_address_inspector (location_t loc, tree t)
5852 : c_omp_address_inspector (loc, t)
5853 {
5854 }
5855
5856 ~cp_omp_address_inspector ()
5857 {
5858 }
5859
5860 bool processing_template_decl_p ()
5861 {
5862 return processing_template_decl;
5863 }
5864
5865 void emit_unmappable_type_notes (tree t)
5866 {
5867 if (TREE_TYPE (t) != error_mark_node
5868 && !COMPLETE_TYPE_P (TREE_TYPE (t)))
5869 cxx_incomplete_type_inform (TREE_TYPE (t));
5870 }
5871
5872 tree convert_from_reference (tree x)
5873 {
5874 return ::convert_from_reference (x);
5875 }
5876
5877 tree build_array_ref (location_t loc, tree arr, tree idx)
5878 {
5879 return ::build_array_ref (loc, arr, idx);
5880 }
5881
5882 bool check_clause (tree clause)
5883 {
5884 if (TREE_CODE (orig) == COMPONENT_REF
5885 && invalid_nonstatic_memfn_p (EXPR_LOCATION (orig), orig,
5886 tf_warning_or_error))
5887 return false;
5888 if (!c_omp_address_inspector::check_clause (clause))
5889 return false;
5890 return true;
5891 }
5892 };
5893
5894 /* Helper function for handle_omp_array_sections. Called recursively
5895 to handle multiple array-section-subscripts. C is the clause,
5896 T current expression (initially OMP_CLAUSE_DECL), which is either
5897 a TREE_LIST for array-section-subscript (TREE_PURPOSE is low-bound
5898 expression if specified, TREE_VALUE length expression if specified,
5899 TREE_CHAIN is what it has been specified after, or some decl.
5900 TYPES vector is populated with array section types, MAYBE_ZERO_LEN
5901 set to true if any of the array-section-subscript could have length
5902 of zero (explicit or implicit), FIRST_NON_ONE is the index of the
5903 first array-section-subscript which is known not to have length
5904 of one. Given say:
5905 map(a[:b][2:1][:c][:2][:d][e:f][2:5])
5906 FIRST_NON_ONE will be 3, array-section-subscript [:b], [2:1] and [:c]
5907 all are or may have length of 1, array-section-subscript [:2] is the
5908 first one known not to have length 1. For array-section-subscript
5909 <= FIRST_NON_ONE we diagnose non-contiguous arrays if low bound isn't
5910 0 or length isn't the array domain max + 1, for > FIRST_NON_ONE we
5911 can if MAYBE_ZERO_LEN is false. MAYBE_ZERO_LEN will be true in the above
5912 case though, as some lengths could be zero. */
5913
5914 static tree
5915 handle_omp_array_sections_1 (tree c, tree t, vec<tree> &types,
5916 bool &maybe_zero_len, unsigned int &first_non_one,
5917 enum c_omp_region_type ort)
5918 {
5919 tree ret, low_bound, length, type;
5920 bool openacc = (ort & C_ORT_ACC) != 0;
5921 if (TREE_CODE (t) != OMP_ARRAY_SECTION)
5922 {
5923 if (error_operand_p (t))
5924 return error_mark_node;
5925
5926 cp_omp_address_inspector ai (OMP_CLAUSE_LOCATION (c), t);
5927 tree t_refto = ai.maybe_unconvert_ref (t);
5928
5929 if (!ai.check_clause (c))
5930 return error_mark_node;
5931 else if (ai.component_access_p ()
5932 && (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
5933 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO
5934 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FROM))
5935 t = ai.get_root_term (true);
5936 else
5937 t = ai.unconverted_ref_origin ();
5938 if (t == error_mark_node)
5939 return error_mark_node;
5940 ret = t_refto;
5941 if (TREE_CODE (t) == FIELD_DECL)
5942 ret = finish_non_static_data_member (t, NULL_TREE, NULL_TREE);
5943 else if (!VAR_P (t)
5944 && (openacc || !EXPR_P (t))
5945 && TREE_CODE (t) != PARM_DECL)
5946 {
5947 if (processing_template_decl && TREE_CODE (t) != OVERLOAD)
5948 return NULL_TREE;
5949 if (DECL_P (t))
5950 error_at (OMP_CLAUSE_LOCATION (c),
5951 "%qD is not a variable in %qs clause", t,
5952 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5953 else
5954 error_at (OMP_CLAUSE_LOCATION (c),
5955 "%qE is not a variable in %qs clause", t,
5956 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5957 return error_mark_node;
5958 }
5959 else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_AFFINITY
5960 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
5961 && VAR_P (t) && CP_DECL_THREAD_LOCAL_P (t))
5962 {
5963 error_at (OMP_CLAUSE_LOCATION (c),
5964 "%qD is threadprivate variable in %qs clause", t,
5965 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5966 return error_mark_node;
5967 }
5968 if (type_dependent_expression_p (ret))
5969 return NULL_TREE;
5970 ret = convert_from_reference (ret);
5971 return ret;
5972 }
5973
5974 if ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP
5975 && (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
5976 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
5977 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
5978 && TREE_CODE (TREE_OPERAND (t, 0)) == FIELD_DECL)
5979 TREE_OPERAND (t, 0) = omp_privatize_field (TREE_OPERAND (t, 0), false);
5980 ret = handle_omp_array_sections_1 (c, TREE_OPERAND (t, 0), types,
5981 maybe_zero_len, first_non_one, ort);
5982 if (ret == error_mark_node || ret == NULL_TREE)
5983 return ret;
5984
5985 type = TREE_TYPE (ret);
5986 low_bound = TREE_OPERAND (t, 1);
5987 length = TREE_OPERAND (t, 2);
5988 if ((low_bound && type_dependent_expression_p (low_bound))
5989 || (length && type_dependent_expression_p (length)))
5990 return NULL_TREE;
5991
5992 if (low_bound == error_mark_node || length == error_mark_node)
5993 return error_mark_node;
5994
5995 if (low_bound && !INTEGRAL_TYPE_P (TREE_TYPE (low_bound)))
5996 {
5997 error_at (OMP_CLAUSE_LOCATION (c),
5998 "low bound %qE of array section does not have integral type",
5999 low_bound);
6000 return error_mark_node;
6001 }
6002 if (length && !INTEGRAL_TYPE_P (TREE_TYPE (length)))
6003 {
6004 error_at (OMP_CLAUSE_LOCATION (c),
6005 "length %qE of array section does not have integral type",
6006 length);
6007 return error_mark_node;
6008 }
6009 if (low_bound)
6010 low_bound = mark_rvalue_use (low_bound);
6011 if (length)
6012 length = mark_rvalue_use (length);
6013 /* We need to reduce to real constant-values for checks below. */
6014 if (length)
6015 length = fold_simple (length);
6016 if (low_bound)
6017 low_bound = fold_simple (low_bound);
6018 if (low_bound
6019 && TREE_CODE (low_bound) == INTEGER_CST
6020 && TYPE_PRECISION (TREE_TYPE (low_bound))
6021 > TYPE_PRECISION (sizetype))
6022 low_bound = fold_convert (sizetype, low_bound);
6023 if (length
6024 && TREE_CODE (length) == INTEGER_CST
6025 && TYPE_PRECISION (TREE_TYPE (length))
6026 > TYPE_PRECISION (sizetype))
6027 length = fold_convert (sizetype, length);
6028 if (low_bound == NULL_TREE)
6029 low_bound = integer_zero_node;
6030
6031 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
6032 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH
6033 || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_DETACH))
6034 {
6035 if (length != integer_one_node)
6036 {
6037 error_at (OMP_CLAUSE_LOCATION (c),
6038 "expected single pointer in %qs clause",
6039 user_omp_clause_code_name (c, openacc));
6040 return error_mark_node;
6041 }
6042 }
6043 if (length != NULL_TREE)
6044 {
6045 if (!integer_nonzerop (length))
6046 {
6047 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_AFFINITY
6048 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
6049 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
6050 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
6051 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
6052 {
6053 if (integer_zerop (length))
6054 {
6055 error_at (OMP_CLAUSE_LOCATION (c),
6056 "zero length array section in %qs clause",
6057 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6058 return error_mark_node;
6059 }
6060 }
6061 else
6062 maybe_zero_len = true;
6063 }
6064 if (first_non_one == types.length ()
6065 && (TREE_CODE (length) != INTEGER_CST || integer_onep (length)))
6066 first_non_one++;
6067 }
6068 if (TREE_CODE (type) == ARRAY_TYPE)
6069 {
6070 if (length == NULL_TREE
6071 && (TYPE_DOMAIN (type) == NULL_TREE
6072 || TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE))
6073 {
6074 error_at (OMP_CLAUSE_LOCATION (c),
6075 "for unknown bound array type length expression must "
6076 "be specified");
6077 return error_mark_node;
6078 }
6079 if (TREE_CODE (low_bound) == INTEGER_CST
6080 && tree_int_cst_sgn (low_bound) == -1)
6081 {
6082 error_at (OMP_CLAUSE_LOCATION (c),
6083 "negative low bound in array section in %qs clause",
6084 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6085 return error_mark_node;
6086 }
6087 if (length != NULL_TREE
6088 && TREE_CODE (length) == INTEGER_CST
6089 && tree_int_cst_sgn (length) == -1)
6090 {
6091 error_at (OMP_CLAUSE_LOCATION (c),
6092 "negative length in array section in %qs clause",
6093 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6094 return error_mark_node;
6095 }
6096 if (TYPE_DOMAIN (type)
6097 && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
6098 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
6099 == INTEGER_CST)
6100 {
6101 tree size
6102 = fold_convert (sizetype, TYPE_MAX_VALUE (TYPE_DOMAIN (type)));
6103 size = size_binop (PLUS_EXPR, size, size_one_node);
6104 if (TREE_CODE (low_bound) == INTEGER_CST)
6105 {
6106 if (tree_int_cst_lt (size, low_bound))
6107 {
6108 error_at (OMP_CLAUSE_LOCATION (c),
6109 "low bound %qE above array section size "
6110 "in %qs clause", low_bound,
6111 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6112 return error_mark_node;
6113 }
6114 if (tree_int_cst_equal (size, low_bound))
6115 {
6116 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_AFFINITY
6117 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
6118 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
6119 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
6120 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
6121 {
6122 error_at (OMP_CLAUSE_LOCATION (c),
6123 "zero length array section in %qs clause",
6124 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6125 return error_mark_node;
6126 }
6127 maybe_zero_len = true;
6128 }
6129 else if (length == NULL_TREE
6130 && first_non_one == types.length ()
6131 && tree_int_cst_equal
6132 (TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
6133 low_bound))
6134 first_non_one++;
6135 }
6136 else if (length == NULL_TREE)
6137 {
6138 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_AFFINITY
6139 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
6140 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION
6141 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_IN_REDUCTION
6142 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_TASK_REDUCTION)
6143 maybe_zero_len = true;
6144 if (first_non_one == types.length ())
6145 first_non_one++;
6146 }
6147 if (length && TREE_CODE (length) == INTEGER_CST)
6148 {
6149 if (tree_int_cst_lt (size, length))
6150 {
6151 error_at (OMP_CLAUSE_LOCATION (c),
6152 "length %qE above array section size "
6153 "in %qs clause", length,
6154 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6155 return error_mark_node;
6156 }
6157 if (TREE_CODE (low_bound) == INTEGER_CST)
6158 {
6159 tree lbpluslen
6160 = size_binop (PLUS_EXPR,
6161 fold_convert (sizetype, low_bound),
6162 fold_convert (sizetype, length));
6163 if (TREE_CODE (lbpluslen) == INTEGER_CST
6164 && tree_int_cst_lt (size, lbpluslen))
6165 {
6166 error_at (OMP_CLAUSE_LOCATION (c),
6167 "high bound %qE above array section size "
6168 "in %qs clause", lbpluslen,
6169 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6170 return error_mark_node;
6171 }
6172 }
6173 }
6174 }
6175 else if (length == NULL_TREE)
6176 {
6177 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_AFFINITY
6178 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
6179 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION
6180 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_IN_REDUCTION
6181 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_TASK_REDUCTION)
6182 maybe_zero_len = true;
6183 if (first_non_one == types.length ())
6184 first_non_one++;
6185 }
6186
6187 /* For [lb:] we will need to evaluate lb more than once. */
6188 if (length == NULL_TREE && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
6189 {
6190 tree lb = cp_save_expr (low_bound);
6191 if (lb != low_bound)
6192 {
6193 TREE_OPERAND (t, 1) = lb;
6194 low_bound = lb;
6195 }
6196 }
6197 }
6198 else if (TYPE_PTR_P (type))
6199 {
6200 if (length == NULL_TREE)
6201 {
6202 if (TREE_CODE (ret) == PARM_DECL && DECL_ARRAY_PARAMETER_P (ret))
6203 error_at (OMP_CLAUSE_LOCATION (c),
6204 "for array function parameter length expression "
6205 "must be specified");
6206 else
6207 error_at (OMP_CLAUSE_LOCATION (c),
6208 "for pointer type length expression must be specified");
6209 return error_mark_node;
6210 }
6211 if (length != NULL_TREE
6212 && TREE_CODE (length) == INTEGER_CST
6213 && tree_int_cst_sgn (length) == -1)
6214 {
6215 error_at (OMP_CLAUSE_LOCATION (c),
6216 "negative length in array section in %qs clause",
6217 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6218 return error_mark_node;
6219 }
6220 /* If there is a pointer type anywhere but in the very first
6221 array-section-subscript, the array section could be non-contiguous. */
6222 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_AFFINITY
6223 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
6224 && TREE_CODE (TREE_OPERAND (t, 0)) == OMP_ARRAY_SECTION)
6225 {
6226 /* If any prior dimension has a non-one length, then deem this
6227 array section as non-contiguous. */
6228 for (tree d = TREE_OPERAND (t, 0); TREE_CODE (d) == OMP_ARRAY_SECTION;
6229 d = TREE_OPERAND (d, 0))
6230 {
6231 tree d_length = TREE_OPERAND (d, 2);
6232 if (d_length == NULL_TREE || !integer_onep (d_length))
6233 {
6234 error_at (OMP_CLAUSE_LOCATION (c),
6235 "array section is not contiguous in %qs clause",
6236 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6237 return error_mark_node;
6238 }
6239 }
6240 }
6241 }
6242 else
6243 {
6244 error_at (OMP_CLAUSE_LOCATION (c),
6245 "%qE does not have pointer or array type", ret);
6246 return error_mark_node;
6247 }
6248 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
6249 types.safe_push (TREE_TYPE (ret));
6250 /* We will need to evaluate lb more than once. */
6251 tree lb = cp_save_expr (low_bound);
6252 if (lb != low_bound)
6253 {
6254 TREE_OPERAND (t, 1) = lb;
6255 low_bound = lb;
6256 }
6257 /* Temporarily disable -fstrong-eval-order for array reductions.
6258 The SAVE_EXPR and COMPOUND_EXPR added if low_bound has side-effects
6259 is something the middle-end can't cope with and more importantly,
6260 it needs to be the actual base variable that is privatized, not some
6261 temporary assigned previous value of it. That, together with OpenMP
6262 saying how many times the side-effects are evaluated is unspecified,
6263 makes int *a, *b; ... reduction(+:a[a = b, 3:10]) really unspecified. */
6264 warning_sentinel s (flag_strong_eval_order,
6265 OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
6266 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
6267 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION);
6268 ret = grok_array_decl (OMP_CLAUSE_LOCATION (c), ret, low_bound, NULL,
6269 tf_warning_or_error);
6270 return ret;
6271 }
6272
6273 /* Handle array sections for clause C. */
6274
6275 static bool
6276 handle_omp_array_sections (tree &c, enum c_omp_region_type ort)
6277 {
6278 bool maybe_zero_len = false;
6279 unsigned int first_non_one = 0;
6280 auto_vec<tree, 10> types;
6281 tree *tp = &OMP_CLAUSE_DECL (c);
6282 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
6283 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_AFFINITY)
6284 && TREE_CODE (*tp) == TREE_LIST
6285 && TREE_PURPOSE (*tp)
6286 && TREE_CODE (TREE_PURPOSE (*tp)) == TREE_VEC)
6287 tp = &TREE_VALUE (*tp);
6288 tree first = handle_omp_array_sections_1 (c, *tp, types,
6289 maybe_zero_len, first_non_one,
6290 ort);
6291 if (first == error_mark_node)
6292 return true;
6293 if (first == NULL_TREE)
6294 return false;
6295 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
6296 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_AFFINITY)
6297 {
6298 tree t = *tp;
6299 tree tem = NULL_TREE;
6300 if (processing_template_decl)
6301 return false;
6302 /* Need to evaluate side effects in the length expressions
6303 if any. */
6304 while (TREE_CODE (t) == TREE_LIST)
6305 {
6306 if (TREE_VALUE (t) && TREE_SIDE_EFFECTS (TREE_VALUE (t)))
6307 {
6308 if (tem == NULL_TREE)
6309 tem = TREE_VALUE (t);
6310 else
6311 tem = build2 (COMPOUND_EXPR, TREE_TYPE (tem),
6312 TREE_VALUE (t), tem);
6313 }
6314 t = TREE_CHAIN (t);
6315 }
6316 if (tem)
6317 first = build2 (COMPOUND_EXPR, TREE_TYPE (first), tem, first);
6318 *tp = first;
6319 }
6320 else
6321 {
6322 unsigned int num = types.length (), i;
6323 tree t, side_effects = NULL_TREE, size = NULL_TREE;
6324 tree condition = NULL_TREE;
6325
6326 if (int_size_in_bytes (TREE_TYPE (first)) <= 0)
6327 maybe_zero_len = true;
6328 if (processing_template_decl && maybe_zero_len)
6329 return false;
6330
6331 for (i = num, t = OMP_CLAUSE_DECL (c); i > 0;
6332 t = TREE_OPERAND (t, 0))
6333 {
6334 gcc_assert (TREE_CODE (t) == OMP_ARRAY_SECTION);
6335
6336 tree low_bound = TREE_OPERAND (t, 1);
6337 tree length = TREE_OPERAND (t, 2);
6338
6339 i--;
6340 if (low_bound
6341 && TREE_CODE (low_bound) == INTEGER_CST
6342 && TYPE_PRECISION (TREE_TYPE (low_bound))
6343 > TYPE_PRECISION (sizetype))
6344 low_bound = fold_convert (sizetype, low_bound);
6345 if (length
6346 && TREE_CODE (length) == INTEGER_CST
6347 && TYPE_PRECISION (TREE_TYPE (length))
6348 > TYPE_PRECISION (sizetype))
6349 length = fold_convert (sizetype, length);
6350 if (low_bound == NULL_TREE)
6351 low_bound = integer_zero_node;
6352 if (!maybe_zero_len && i > first_non_one)
6353 {
6354 if (integer_nonzerop (low_bound))
6355 goto do_warn_noncontiguous;
6356 if (length != NULL_TREE
6357 && TREE_CODE (length) == INTEGER_CST
6358 && TYPE_DOMAIN (types[i])
6359 && TYPE_MAX_VALUE (TYPE_DOMAIN (types[i]))
6360 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])))
6361 == INTEGER_CST)
6362 {
6363 tree size;
6364 size = size_binop (PLUS_EXPR,
6365 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
6366 size_one_node);
6367 if (!tree_int_cst_equal (length, size))
6368 {
6369 do_warn_noncontiguous:
6370 error_at (OMP_CLAUSE_LOCATION (c),
6371 "array section is not contiguous in %qs "
6372 "clause",
6373 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
6374 return true;
6375 }
6376 }
6377 if (!processing_template_decl
6378 && length != NULL_TREE
6379 && TREE_SIDE_EFFECTS (length))
6380 {
6381 if (side_effects == NULL_TREE)
6382 side_effects = length;
6383 else
6384 side_effects = build2 (COMPOUND_EXPR,
6385 TREE_TYPE (side_effects),
6386 length, side_effects);
6387 }
6388 }
6389 else if (processing_template_decl)
6390 continue;
6391 else
6392 {
6393 tree l;
6394
6395 if (i > first_non_one
6396 && ((length && integer_nonzerop (length))
6397 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
6398 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
6399 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION))
6400 continue;
6401 if (length)
6402 l = fold_convert (sizetype, length);
6403 else
6404 {
6405 l = size_binop (PLUS_EXPR,
6406 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
6407 size_one_node);
6408 l = size_binop (MINUS_EXPR, l,
6409 fold_convert (sizetype, low_bound));
6410 }
6411 if (i > first_non_one)
6412 {
6413 l = fold_build2 (NE_EXPR, boolean_type_node, l,
6414 size_zero_node);
6415 if (condition == NULL_TREE)
6416 condition = l;
6417 else
6418 condition = fold_build2 (BIT_AND_EXPR, boolean_type_node,
6419 l, condition);
6420 }
6421 else if (size == NULL_TREE)
6422 {
6423 size = size_in_bytes (TREE_TYPE (types[i]));
6424 tree eltype = TREE_TYPE (types[num - 1]);
6425 while (TREE_CODE (eltype) == ARRAY_TYPE)
6426 eltype = TREE_TYPE (eltype);
6427 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
6428 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
6429 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
6430 size = size_binop (EXACT_DIV_EXPR, size,
6431 size_in_bytes (eltype));
6432 size = size_binop (MULT_EXPR, size, l);
6433 if (condition)
6434 size = fold_build3 (COND_EXPR, sizetype, condition,
6435 size, size_zero_node);
6436 }
6437 else
6438 size = size_binop (MULT_EXPR, size, l);
6439 }
6440 }
6441 if (!processing_template_decl)
6442 {
6443 if (side_effects)
6444 size = build2 (COMPOUND_EXPR, sizetype, side_effects, size);
6445 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
6446 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
6447 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
6448 {
6449 size = size_binop (MINUS_EXPR, size, size_one_node);
6450 size = save_expr (size);
6451 tree index_type = build_index_type (size);
6452 tree eltype = TREE_TYPE (first);
6453 while (TREE_CODE (eltype) == ARRAY_TYPE)
6454 eltype = TREE_TYPE (eltype);
6455 tree type = build_array_type (eltype, index_type);
6456 tree ptype = build_pointer_type (eltype);
6457 if (TYPE_REF_P (TREE_TYPE (t))
6458 && INDIRECT_TYPE_P (TREE_TYPE (TREE_TYPE (t))))
6459 t = convert_from_reference (t);
6460 else if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
6461 t = build_fold_addr_expr (t);
6462 tree t2 = build_fold_addr_expr (first);
6463 t2 = fold_convert_loc (OMP_CLAUSE_LOCATION (c),
6464 ptrdiff_type_node, t2);
6465 t2 = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
6466 ptrdiff_type_node, t2,
6467 fold_convert_loc (OMP_CLAUSE_LOCATION (c),
6468 ptrdiff_type_node, t));
6469 if (tree_fits_shwi_p (t2))
6470 t = build2 (MEM_REF, type, t,
6471 build_int_cst (ptype, tree_to_shwi (t2)));
6472 else
6473 {
6474 t2 = fold_convert_loc (OMP_CLAUSE_LOCATION (c),
6475 sizetype, t2);
6476 t = build2_loc (OMP_CLAUSE_LOCATION (c), POINTER_PLUS_EXPR,
6477 TREE_TYPE (t), t, t2);
6478 t = build2 (MEM_REF, type, t, build_int_cst (ptype, 0));
6479 }
6480 OMP_CLAUSE_DECL (c) = t;
6481 return false;
6482 }
6483 OMP_CLAUSE_DECL (c) = first;
6484 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_HAS_DEVICE_ADDR)
6485 return false;
6486 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP
6487 || (OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_ATTACH
6488 && OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_DETACH
6489 && OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_FORCE_DETACH))
6490 OMP_CLAUSE_SIZE (c) = size;
6491 if (TREE_CODE (t) == FIELD_DECL)
6492 t = finish_non_static_data_member (t, NULL_TREE, NULL_TREE);
6493
6494 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
6495 return false;
6496
6497 if (TREE_CODE (first) == INDIRECT_REF)
6498 {
6499 /* Detect and skip adding extra nodes for pointer-to-member
6500 mappings. These are unsupported for now. */
6501 tree tmp = TREE_OPERAND (first, 0);
6502
6503 if (TREE_CODE (tmp) == NON_LVALUE_EXPR)
6504 tmp = TREE_OPERAND (tmp, 0);
6505
6506 if (TREE_CODE (tmp) == INDIRECT_REF)
6507 tmp = TREE_OPERAND (tmp, 0);
6508
6509 if (TREE_CODE (tmp) == POINTER_PLUS_EXPR)
6510 {
6511 tree offset = TREE_OPERAND (tmp, 1);
6512 STRIP_NOPS (offset);
6513 if (TYPE_PTRMEM_P (TREE_TYPE (offset)))
6514 {
6515 sorry_at (OMP_CLAUSE_LOCATION (c),
6516 "pointer-to-member mapping %qE not supported",
6517 OMP_CLAUSE_DECL (c));
6518 return true;
6519 }
6520 }
6521 }
6522
6523 /* FIRST represents the first item of data that we are mapping.
6524 E.g. if we're mapping an array, FIRST might resemble
6525 "foo.bar.myarray[0]". */
6526
6527 auto_vec<omp_addr_token *, 10> addr_tokens;
6528
6529 if (!omp_parse_expr (addr_tokens, first))
6530 return true;
6531
6532 cp_omp_address_inspector ai (OMP_CLAUSE_LOCATION (c), t);
6533
6534 tree nc = ai.expand_map_clause (c, first, addr_tokens, ort);
6535 if (nc != error_mark_node)
6536 {
6537 using namespace omp_addr_tokenizer;
6538
6539 if (ai.maybe_zero_length_array_section (c))
6540 OMP_CLAUSE_MAP_MAYBE_ZERO_LENGTH_ARRAY_SECTION (c) = 1;
6541
6542 /* !!! If we're accessing a base decl via chained access
6543 methods (e.g. multiple indirections), duplicate clause
6544 detection won't work properly. Skip it in that case. */
6545 if ((addr_tokens[0]->type == STRUCTURE_BASE
6546 || addr_tokens[0]->type == ARRAY_BASE)
6547 && addr_tokens[0]->u.structure_base_kind == BASE_DECL
6548 && addr_tokens[1]->type == ACCESS_METHOD
6549 && omp_access_chain_p (addr_tokens, 1))
6550 c = nc;
6551
6552 return false;
6553 }
6554 }
6555 }
6556 return false;
6557 }
6558
6559 /* Return identifier to look up for omp declare reduction. */
6560
6561 tree
6562 omp_reduction_id (enum tree_code reduction_code, tree reduction_id, tree type)
6563 {
6564 const char *p = NULL;
6565 const char *m = NULL;
6566 switch (reduction_code)
6567 {
6568 case PLUS_EXPR:
6569 case MULT_EXPR:
6570 case MINUS_EXPR:
6571 case BIT_AND_EXPR:
6572 case BIT_XOR_EXPR:
6573 case BIT_IOR_EXPR:
6574 case TRUTH_ANDIF_EXPR:
6575 case TRUTH_ORIF_EXPR:
6576 reduction_id = ovl_op_identifier (false, reduction_code);
6577 break;
6578 case MIN_EXPR:
6579 p = "min";
6580 break;
6581 case MAX_EXPR:
6582 p = "max";
6583 break;
6584 default:
6585 break;
6586 }
6587
6588 if (p == NULL)
6589 {
6590 if (TREE_CODE (reduction_id) != IDENTIFIER_NODE)
6591 return error_mark_node;
6592 p = IDENTIFIER_POINTER (reduction_id);
6593 }
6594
6595 if (type != NULL_TREE)
6596 m = mangle_type_string (TYPE_MAIN_VARIANT (type));
6597
6598 const char prefix[] = "omp declare reduction ";
6599 size_t lenp = sizeof (prefix);
6600 if (strncmp (p, prefix, lenp - 1) == 0)
6601 lenp = 1;
6602 size_t len = strlen (p);
6603 size_t lenm = m ? strlen (m) + 1 : 0;
6604 char *name = XALLOCAVEC (char, lenp + len + lenm);
6605 if (lenp > 1)
6606 memcpy (name, prefix, lenp - 1);
6607 memcpy (name + lenp - 1, p, len + 1);
6608 if (m)
6609 {
6610 name[lenp + len - 1] = '~';
6611 memcpy (name + lenp + len, m, lenm);
6612 }
6613 return get_identifier (name);
6614 }
6615
6616 /* Lookup OpenMP UDR ID for TYPE, return the corresponding artificial
6617 FUNCTION_DECL or NULL_TREE if not found. */
6618
6619 static tree
6620 omp_reduction_lookup (location_t loc, tree id, tree type, tree *baselinkp,
6621 vec<tree> *ambiguousp)
6622 {
6623 tree orig_id = id;
6624 tree baselink = NULL_TREE;
6625 if (identifier_p (id))
6626 {
6627 cp_id_kind idk;
6628 bool nonint_cst_expression_p;
6629 const char *error_msg;
6630 id = omp_reduction_id (ERROR_MARK, id, type);
6631 tree decl = lookup_name (id);
6632 if (decl == NULL_TREE)
6633 decl = error_mark_node;
6634 id = finish_id_expression (id, decl, NULL_TREE, &idk, false, true,
6635 &nonint_cst_expression_p, false, true, false,
6636 false, &error_msg, loc);
6637 if (idk == CP_ID_KIND_UNQUALIFIED
6638 && identifier_p (id))
6639 {
6640 vec<tree, va_gc> *args = NULL;
6641 vec_safe_push (args, build_reference_type (type));
6642 id = perform_koenig_lookup (id, args, tf_none);
6643 }
6644 }
6645 else if (TREE_CODE (id) == SCOPE_REF)
6646 id = lookup_qualified_name (TREE_OPERAND (id, 0),
6647 omp_reduction_id (ERROR_MARK,
6648 TREE_OPERAND (id, 1),
6649 type),
6650 LOOK_want::NORMAL, false);
6651 tree fns = id;
6652 id = NULL_TREE;
6653 if (fns && is_overloaded_fn (fns))
6654 {
6655 for (lkp_iterator iter (get_fns (fns)); iter; ++iter)
6656 {
6657 tree fndecl = *iter;
6658 if (TREE_CODE (fndecl) == FUNCTION_DECL)
6659 {
6660 tree argtype = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
6661 if (same_type_p (TREE_TYPE (argtype), type))
6662 {
6663 id = fndecl;
6664 break;
6665 }
6666 }
6667 }
6668
6669 if (id && BASELINK_P (fns))
6670 {
6671 if (baselinkp)
6672 *baselinkp = fns;
6673 else
6674 baselink = fns;
6675 }
6676 }
6677
6678 if (!id && CLASS_TYPE_P (type) && TYPE_BINFO (type))
6679 {
6680 auto_vec<tree> ambiguous;
6681 tree binfo = TYPE_BINFO (type), base_binfo, ret = NULL_TREE;
6682 unsigned int ix;
6683 if (ambiguousp == NULL)
6684 ambiguousp = &ambiguous;
6685 for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ix++)
6686 {
6687 id = omp_reduction_lookup (loc, orig_id, BINFO_TYPE (base_binfo),
6688 baselinkp ? baselinkp : &baselink,
6689 ambiguousp);
6690 if (id == NULL_TREE)
6691 continue;
6692 if (!ambiguousp->is_empty ())
6693 ambiguousp->safe_push (id);
6694 else if (ret != NULL_TREE)
6695 {
6696 ambiguousp->safe_push (ret);
6697 ambiguousp->safe_push (id);
6698 ret = NULL_TREE;
6699 }
6700 else
6701 ret = id;
6702 }
6703 if (ambiguousp != &ambiguous)
6704 return ret;
6705 if (!ambiguous.is_empty ())
6706 {
6707 auto_diagnostic_group d;
6708 const char *str = _("candidates are:");
6709 unsigned int idx;
6710 tree udr;
6711 error_at (loc, "user defined reduction lookup is ambiguous");
6712 FOR_EACH_VEC_ELT (ambiguous, idx, udr)
6713 {
6714 inform (DECL_SOURCE_LOCATION (udr), "%s %#qD", str, udr);
6715 if (idx == 0)
6716 str = get_spaces (str);
6717 }
6718 ret = error_mark_node;
6719 baselink = NULL_TREE;
6720 }
6721 id = ret;
6722 }
6723 if (id && baselink)
6724 perform_or_defer_access_check (BASELINK_BINFO (baselink),
6725 id, id, tf_warning_or_error);
6726 return id;
6727 }
6728
6729 /* Helper function for cp_parser_omp_declare_reduction_exprs
6730 and tsubst_omp_udr.
6731 Remove CLEANUP_STMT for data (omp_priv variable).
6732 Also append INIT_EXPR for DECL_INITIAL of omp_priv after its
6733 DECL_EXPR. */
6734
6735 tree
6736 cp_remove_omp_priv_cleanup_stmt (tree *tp, int *walk_subtrees, void *data)
6737 {
6738 if (TYPE_P (*tp))
6739 *walk_subtrees = 0;
6740 else if (TREE_CODE (*tp) == CLEANUP_STMT && CLEANUP_DECL (*tp) == (tree) data)
6741 *tp = CLEANUP_BODY (*tp);
6742 else if (TREE_CODE (*tp) == DECL_EXPR)
6743 {
6744 tree decl = DECL_EXPR_DECL (*tp);
6745 if (!processing_template_decl
6746 && decl == (tree) data
6747 && DECL_INITIAL (decl)
6748 && DECL_INITIAL (decl) != error_mark_node)
6749 {
6750 tree list = NULL_TREE;
6751 append_to_statement_list_force (*tp, &list);
6752 tree init_expr = build2 (INIT_EXPR, void_type_node,
6753 decl, DECL_INITIAL (decl));
6754 DECL_INITIAL (decl) = NULL_TREE;
6755 append_to_statement_list_force (init_expr, &list);
6756 *tp = list;
6757 }
6758 }
6759 return NULL_TREE;
6760 }
6761
6762 /* Data passed from cp_check_omp_declare_reduction to
6763 cp_check_omp_declare_reduction_r. */
6764
6765 struct cp_check_omp_declare_reduction_data
6766 {
6767 location_t loc;
6768 tree stmts[7];
6769 bool combiner_p;
6770 };
6771
6772 /* Helper function for cp_check_omp_declare_reduction, called via
6773 cp_walk_tree. */
6774
6775 static tree
6776 cp_check_omp_declare_reduction_r (tree *tp, int *, void *data)
6777 {
6778 struct cp_check_omp_declare_reduction_data *udr_data
6779 = (struct cp_check_omp_declare_reduction_data *) data;
6780 if (SSA_VAR_P (*tp)
6781 && !DECL_ARTIFICIAL (*tp)
6782 && *tp != DECL_EXPR_DECL (udr_data->stmts[udr_data->combiner_p ? 0 : 3])
6783 && *tp != DECL_EXPR_DECL (udr_data->stmts[udr_data->combiner_p ? 1 : 4]))
6784 {
6785 location_t loc = udr_data->loc;
6786 if (udr_data->combiner_p)
6787 error_at (loc, "%<#pragma omp declare reduction%> combiner refers to "
6788 "variable %qD which is not %<omp_out%> nor %<omp_in%>",
6789 *tp);
6790 else
6791 error_at (loc, "%<#pragma omp declare reduction%> initializer refers "
6792 "to variable %qD which is not %<omp_priv%> nor "
6793 "%<omp_orig%>",
6794 *tp);
6795 return *tp;
6796 }
6797 return NULL_TREE;
6798 }
6799
6800 /* Diagnose violation of OpenMP #pragma omp declare reduction restrictions. */
6801
6802 bool
6803 cp_check_omp_declare_reduction (tree udr)
6804 {
6805 tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (udr)));
6806 gcc_assert (TYPE_REF_P (type));
6807 type = TREE_TYPE (type);
6808 int i;
6809 location_t loc = DECL_SOURCE_LOCATION (udr);
6810
6811 if (type == error_mark_node)
6812 return false;
6813 if (ARITHMETIC_TYPE_P (type))
6814 {
6815 static enum tree_code predef_codes[]
6816 = { PLUS_EXPR, MULT_EXPR, MINUS_EXPR, BIT_AND_EXPR, BIT_XOR_EXPR,
6817 BIT_IOR_EXPR, TRUTH_ANDIF_EXPR, TRUTH_ORIF_EXPR };
6818 for (i = 0; i < 8; i++)
6819 {
6820 tree id = omp_reduction_id (predef_codes[i], NULL_TREE, NULL_TREE);
6821 const char *n1 = IDENTIFIER_POINTER (DECL_NAME (udr));
6822 const char *n2 = IDENTIFIER_POINTER (id);
6823 if (strncmp (n1, n2, IDENTIFIER_LENGTH (id)) == 0
6824 && (n1[IDENTIFIER_LENGTH (id)] == '~'
6825 || n1[IDENTIFIER_LENGTH (id)] == '\0'))
6826 break;
6827 }
6828
6829 if (i == 8
6830 && TREE_CODE (type) != COMPLEX_EXPR)
6831 {
6832 const char prefix_minmax[] = "omp declare reduction m";
6833 size_t prefix_size = sizeof (prefix_minmax) - 1;
6834 const char *n = IDENTIFIER_POINTER (DECL_NAME (udr));
6835 if (strncmp (IDENTIFIER_POINTER (DECL_NAME (udr)),
6836 prefix_minmax, prefix_size) == 0
6837 && ((n[prefix_size] == 'i' && n[prefix_size + 1] == 'n')
6838 || (n[prefix_size] == 'a' && n[prefix_size + 1] == 'x'))
6839 && (n[prefix_size + 2] == '~' || n[prefix_size + 2] == '\0'))
6840 i = 0;
6841 }
6842 if (i < 8)
6843 {
6844 error_at (loc, "predeclared arithmetic type %qT in "
6845 "%<#pragma omp declare reduction%>", type);
6846 return false;
6847 }
6848 }
6849 else if (FUNC_OR_METHOD_TYPE_P (type)
6850 || TREE_CODE (type) == ARRAY_TYPE)
6851 {
6852 error_at (loc, "function or array type %qT in "
6853 "%<#pragma omp declare reduction%>", type);
6854 return false;
6855 }
6856 else if (TYPE_REF_P (type))
6857 {
6858 error_at (loc, "reference type %qT in %<#pragma omp declare reduction%>",
6859 type);
6860 return false;
6861 }
6862 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
6863 {
6864 error_at (loc, "%<const%>, %<volatile%> or %<__restrict%>-qualified "
6865 "type %qT in %<#pragma omp declare reduction%>", type);
6866 return false;
6867 }
6868
6869 tree body = DECL_SAVED_TREE (udr);
6870 if (body == NULL_TREE || TREE_CODE (body) != STATEMENT_LIST)
6871 return true;
6872
6873 tree_stmt_iterator tsi;
6874 struct cp_check_omp_declare_reduction_data data;
6875 memset (data.stmts, 0, sizeof data.stmts);
6876 for (i = 0, tsi = tsi_start (body);
6877 i < 7 && !tsi_end_p (tsi);
6878 i++, tsi_next (&tsi))
6879 data.stmts[i] = tsi_stmt (tsi);
6880 data.loc = loc;
6881 gcc_assert (tsi_end_p (tsi));
6882 if (i >= 3)
6883 {
6884 gcc_assert (TREE_CODE (data.stmts[0]) == DECL_EXPR
6885 && TREE_CODE (data.stmts[1]) == DECL_EXPR);
6886 if (warning_suppressed_p (DECL_EXPR_DECL (data.stmts[0]) /* What warning? */))
6887 return true;
6888 data.combiner_p = true;
6889 if (cp_walk_tree (&data.stmts[2], cp_check_omp_declare_reduction_r,
6890 &data, NULL))
6891 suppress_warning (DECL_EXPR_DECL (data.stmts[0]) /* What warning? */);
6892 }
6893 if (i >= 6)
6894 {
6895 gcc_assert (TREE_CODE (data.stmts[3]) == DECL_EXPR
6896 && TREE_CODE (data.stmts[4]) == DECL_EXPR);
6897 data.combiner_p = false;
6898 if (cp_walk_tree (&data.stmts[5], cp_check_omp_declare_reduction_r,
6899 &data, NULL)
6900 || cp_walk_tree (&DECL_INITIAL (DECL_EXPR_DECL (data.stmts[3])),
6901 cp_check_omp_declare_reduction_r, &data, NULL))
6902 suppress_warning (DECL_EXPR_DECL (data.stmts[0]) /* Wat warning? */);
6903 if (i == 7)
6904 gcc_assert (TREE_CODE (data.stmts[6]) == DECL_EXPR);
6905 }
6906 return true;
6907 }
6908
6909 /* Helper function of finish_omp_clauses. Clone STMT as if we were making
6910 an inline call. But, remap
6911 the OMP_DECL1 VAR_DECL (omp_out resp. omp_orig) to PLACEHOLDER
6912 and OMP_DECL2 VAR_DECL (omp_in resp. omp_priv) to DECL. */
6913
6914 static tree
6915 clone_omp_udr (tree stmt, tree omp_decl1, tree omp_decl2,
6916 tree decl, tree placeholder)
6917 {
6918 copy_body_data id;
6919 hash_map<tree, tree> decl_map;
6920
6921 decl_map.put (omp_decl1, placeholder);
6922 decl_map.put (omp_decl2, decl);
6923 memset (&id, 0, sizeof (id));
6924 id.src_fn = DECL_CONTEXT (omp_decl1);
6925 id.dst_fn = current_function_decl;
6926 id.src_cfun = DECL_STRUCT_FUNCTION (id.src_fn);
6927 id.decl_map = &decl_map;
6928
6929 id.copy_decl = copy_decl_no_change;
6930 id.transform_call_graph_edges = CB_CGE_DUPLICATE;
6931 id.transform_new_cfg = true;
6932 id.transform_return_to_modify = false;
6933 id.eh_lp_nr = 0;
6934 walk_tree (&stmt, copy_tree_body_r, &id, NULL);
6935 return stmt;
6936 }
6937
6938 /* Helper function of finish_omp_clauses, called via cp_walk_tree.
6939 Find OMP_CLAUSE_PLACEHOLDER (passed in DATA) in *TP. */
6940
6941 static tree
6942 find_omp_placeholder_r (tree *tp, int *, void *data)
6943 {
6944 if (*tp == (tree) data)
6945 return *tp;
6946 return NULL_TREE;
6947 }
6948
6949 /* Helper function of finish_omp_clauses. Handle OMP_CLAUSE_REDUCTION C.
6950 Return true if there is some error and the clause should be removed. */
6951
6952 static bool
6953 finish_omp_reduction_clause (tree c, bool *need_default_ctor, bool *need_dtor)
6954 {
6955 tree t = OMP_CLAUSE_DECL (c);
6956 bool predefined = false;
6957 if (TREE_CODE (t) == TREE_LIST)
6958 {
6959 gcc_assert (processing_template_decl);
6960 return false;
6961 }
6962 tree type = TREE_TYPE (t);
6963 if (TREE_CODE (t) == MEM_REF)
6964 type = TREE_TYPE (type);
6965 if (TYPE_REF_P (type))
6966 type = TREE_TYPE (type);
6967 if (TREE_CODE (type) == ARRAY_TYPE)
6968 {
6969 tree oatype = type;
6970 gcc_assert (TREE_CODE (t) != MEM_REF);
6971 while (TREE_CODE (type) == ARRAY_TYPE)
6972 type = TREE_TYPE (type);
6973 if (!processing_template_decl)
6974 {
6975 t = require_complete_type (t);
6976 if (t == error_mark_node
6977 || !complete_type_or_else (oatype, NULL_TREE))
6978 return true;
6979 tree size = size_binop (EXACT_DIV_EXPR, TYPE_SIZE_UNIT (oatype),
6980 TYPE_SIZE_UNIT (type));
6981 if (integer_zerop (size))
6982 {
6983 error_at (OMP_CLAUSE_LOCATION (c),
6984 "%qE in %<reduction%> clause is a zero size array",
6985 omp_clause_printable_decl (t));
6986 return true;
6987 }
6988 size = size_binop (MINUS_EXPR, size, size_one_node);
6989 size = save_expr (size);
6990 tree index_type = build_index_type (size);
6991 tree atype = build_array_type (type, index_type);
6992 tree ptype = build_pointer_type (type);
6993 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
6994 t = build_fold_addr_expr (t);
6995 t = build2 (MEM_REF, atype, t, build_int_cst (ptype, 0));
6996 OMP_CLAUSE_DECL (c) = t;
6997 }
6998 }
6999 if (type == error_mark_node)
7000 return true;
7001 else if (ARITHMETIC_TYPE_P (type))
7002 switch (OMP_CLAUSE_REDUCTION_CODE (c))
7003 {
7004 case PLUS_EXPR:
7005 case MULT_EXPR:
7006 case MINUS_EXPR:
7007 case TRUTH_ANDIF_EXPR:
7008 case TRUTH_ORIF_EXPR:
7009 predefined = true;
7010 break;
7011 case MIN_EXPR:
7012 case MAX_EXPR:
7013 if (TREE_CODE (type) == COMPLEX_TYPE)
7014 break;
7015 predefined = true;
7016 break;
7017 case BIT_AND_EXPR:
7018 case BIT_IOR_EXPR:
7019 case BIT_XOR_EXPR:
7020 if (FLOAT_TYPE_P (type) || TREE_CODE (type) == COMPLEX_TYPE)
7021 break;
7022 predefined = true;
7023 break;
7024 default:
7025 break;
7026 }
7027 else if (TYPE_READONLY (type))
7028 {
7029 error_at (OMP_CLAUSE_LOCATION (c),
7030 "%qE has const type for %<reduction%>",
7031 omp_clause_printable_decl (t));
7032 return true;
7033 }
7034 else if (!processing_template_decl)
7035 {
7036 t = require_complete_type (t);
7037 if (t == error_mark_node)
7038 return true;
7039 OMP_CLAUSE_DECL (c) = t;
7040 }
7041
7042 if (predefined)
7043 {
7044 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = NULL_TREE;
7045 return false;
7046 }
7047 else if (processing_template_decl)
7048 {
7049 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == error_mark_node)
7050 return true;
7051 return false;
7052 }
7053
7054 tree id = OMP_CLAUSE_REDUCTION_PLACEHOLDER (c);
7055
7056 type = TYPE_MAIN_VARIANT (type);
7057 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = NULL_TREE;
7058 if (id == NULL_TREE)
7059 id = omp_reduction_id (OMP_CLAUSE_REDUCTION_CODE (c),
7060 NULL_TREE, NULL_TREE);
7061 id = omp_reduction_lookup (OMP_CLAUSE_LOCATION (c), id, type, NULL, NULL);
7062 if (id)
7063 {
7064 if (id == error_mark_node)
7065 return true;
7066 mark_used (id);
7067 tree body = DECL_SAVED_TREE (id);
7068 if (!body)
7069 return true;
7070 if (TREE_CODE (body) == STATEMENT_LIST)
7071 {
7072 tree_stmt_iterator tsi;
7073 tree placeholder = NULL_TREE, decl_placeholder = NULL_TREE;
7074 int i;
7075 tree stmts[7];
7076 tree atype = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (id)));
7077 atype = TREE_TYPE (atype);
7078 bool need_static_cast = !same_type_p (type, atype);
7079 memset (stmts, 0, sizeof stmts);
7080 for (i = 0, tsi = tsi_start (body);
7081 i < 7 && !tsi_end_p (tsi);
7082 i++, tsi_next (&tsi))
7083 stmts[i] = tsi_stmt (tsi);
7084 gcc_assert (tsi_end_p (tsi));
7085
7086 if (i >= 3)
7087 {
7088 gcc_assert (TREE_CODE (stmts[0]) == DECL_EXPR
7089 && TREE_CODE (stmts[1]) == DECL_EXPR);
7090 placeholder = build_lang_decl (VAR_DECL, NULL_TREE, type);
7091 DECL_ARTIFICIAL (placeholder) = 1;
7092 DECL_IGNORED_P (placeholder) = 1;
7093 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = placeholder;
7094 if (TREE_CODE (t) == MEM_REF)
7095 {
7096 decl_placeholder = build_lang_decl (VAR_DECL, NULL_TREE,
7097 type);
7098 DECL_ARTIFICIAL (decl_placeholder) = 1;
7099 DECL_IGNORED_P (decl_placeholder) = 1;
7100 OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c) = decl_placeholder;
7101 }
7102 if (TREE_ADDRESSABLE (DECL_EXPR_DECL (stmts[0])))
7103 cxx_mark_addressable (placeholder);
7104 if (TREE_ADDRESSABLE (DECL_EXPR_DECL (stmts[1]))
7105 && (decl_placeholder
7106 || !TYPE_REF_P (TREE_TYPE (OMP_CLAUSE_DECL (c)))))
7107 cxx_mark_addressable (decl_placeholder ? decl_placeholder
7108 : OMP_CLAUSE_DECL (c));
7109 tree omp_out = placeholder;
7110 tree omp_in = decl_placeholder ? decl_placeholder
7111 : convert_from_reference (OMP_CLAUSE_DECL (c));
7112 if (need_static_cast)
7113 {
7114 tree rtype = build_reference_type (atype);
7115 omp_out = build_static_cast (input_location,
7116 rtype, omp_out,
7117 tf_warning_or_error);
7118 omp_in = build_static_cast (input_location,
7119 rtype, omp_in,
7120 tf_warning_or_error);
7121 if (omp_out == error_mark_node || omp_in == error_mark_node)
7122 return true;
7123 omp_out = convert_from_reference (omp_out);
7124 omp_in = convert_from_reference (omp_in);
7125 }
7126 OMP_CLAUSE_REDUCTION_MERGE (c)
7127 = clone_omp_udr (stmts[2], DECL_EXPR_DECL (stmts[0]),
7128 DECL_EXPR_DECL (stmts[1]), omp_in, omp_out);
7129 }
7130 if (i >= 6)
7131 {
7132 gcc_assert (TREE_CODE (stmts[3]) == DECL_EXPR
7133 && TREE_CODE (stmts[4]) == DECL_EXPR);
7134 if (TREE_ADDRESSABLE (DECL_EXPR_DECL (stmts[3]))
7135 && (decl_placeholder
7136 || !TYPE_REF_P (TREE_TYPE (OMP_CLAUSE_DECL (c)))))
7137 cxx_mark_addressable (decl_placeholder ? decl_placeholder
7138 : OMP_CLAUSE_DECL (c));
7139 if (TREE_ADDRESSABLE (DECL_EXPR_DECL (stmts[4])))
7140 cxx_mark_addressable (placeholder);
7141 tree omp_priv = decl_placeholder ? decl_placeholder
7142 : convert_from_reference (OMP_CLAUSE_DECL (c));
7143 tree omp_orig = placeholder;
7144 if (need_static_cast)
7145 {
7146 if (i == 7)
7147 {
7148 error_at (OMP_CLAUSE_LOCATION (c),
7149 "user defined reduction with constructor "
7150 "initializer for base class %qT", atype);
7151 return true;
7152 }
7153 tree rtype = build_reference_type (atype);
7154 omp_priv = build_static_cast (input_location,
7155 rtype, omp_priv,
7156 tf_warning_or_error);
7157 omp_orig = build_static_cast (input_location,
7158 rtype, omp_orig,
7159 tf_warning_or_error);
7160 if (omp_priv == error_mark_node
7161 || omp_orig == error_mark_node)
7162 return true;
7163 omp_priv = convert_from_reference (omp_priv);
7164 omp_orig = convert_from_reference (omp_orig);
7165 }
7166 if (i == 6)
7167 *need_default_ctor = true;
7168 OMP_CLAUSE_REDUCTION_INIT (c)
7169 = clone_omp_udr (stmts[5], DECL_EXPR_DECL (stmts[4]),
7170 DECL_EXPR_DECL (stmts[3]),
7171 omp_priv, omp_orig);
7172 if (cp_walk_tree (&OMP_CLAUSE_REDUCTION_INIT (c),
7173 find_omp_placeholder_r, placeholder, NULL))
7174 OMP_CLAUSE_REDUCTION_OMP_ORIG_REF (c) = 1;
7175 }
7176 else if (i >= 3)
7177 {
7178 if (CLASS_TYPE_P (type) && !pod_type_p (type))
7179 *need_default_ctor = true;
7180 else
7181 {
7182 tree init;
7183 tree v = decl_placeholder ? decl_placeholder
7184 : convert_from_reference (t);
7185 if (AGGREGATE_TYPE_P (TREE_TYPE (v)))
7186 init = build_constructor (TREE_TYPE (v), NULL);
7187 else
7188 init = fold_convert (TREE_TYPE (v), integer_zero_node);
7189 OMP_CLAUSE_REDUCTION_INIT (c)
7190 = cp_build_init_expr (v, init);
7191 }
7192 }
7193 }
7194 }
7195 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
7196 *need_dtor = true;
7197 else
7198 {
7199 error_at (OMP_CLAUSE_LOCATION (c),
7200 "user defined reduction not found for %qE",
7201 omp_clause_printable_decl (t));
7202 return true;
7203 }
7204 if (TREE_CODE (OMP_CLAUSE_DECL (c)) == MEM_REF)
7205 gcc_assert (TYPE_SIZE_UNIT (type)
7206 && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST);
7207 return false;
7208 }
7209
7210 /* Called from finish_struct_1. linear(this) or linear(this:step)
7211 clauses might not be finalized yet because the class has been incomplete
7212 when parsing #pragma omp declare simd methods. Fix those up now. */
7213
7214 void
7215 finish_omp_declare_simd_methods (tree t)
7216 {
7217 if (processing_template_decl)
7218 return;
7219
7220 for (tree x = TYPE_FIELDS (t); x; x = DECL_CHAIN (x))
7221 {
7222 if (TREE_CODE (x) == USING_DECL
7223 || !DECL_IOBJ_MEMBER_FUNCTION_P (x))
7224 continue;
7225 tree ods = lookup_attribute ("omp declare simd", DECL_ATTRIBUTES (x));
7226 if (!ods || !TREE_VALUE (ods))
7227 continue;
7228 for (tree c = TREE_VALUE (TREE_VALUE (ods)); c; c = OMP_CLAUSE_CHAIN (c))
7229 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
7230 && integer_zerop (OMP_CLAUSE_DECL (c))
7231 && OMP_CLAUSE_LINEAR_STEP (c)
7232 && TYPE_PTR_P (TREE_TYPE (OMP_CLAUSE_LINEAR_STEP (c))))
7233 {
7234 tree s = OMP_CLAUSE_LINEAR_STEP (c);
7235 s = fold_convert_loc (OMP_CLAUSE_LOCATION (c), sizetype, s);
7236 s = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MULT_EXPR,
7237 sizetype, s, TYPE_SIZE_UNIT (t));
7238 OMP_CLAUSE_LINEAR_STEP (c) = s;
7239 }
7240 }
7241 }
7242
7243 /* Adjust sink depend/doacross clause to take into account pointer offsets.
7244
7245 Return TRUE if there was a problem processing the offset, and the
7246 whole clause should be removed. */
7247
7248 static bool
7249 cp_finish_omp_clause_doacross_sink (tree sink_clause)
7250 {
7251 tree t = OMP_CLAUSE_DECL (sink_clause);
7252 gcc_assert (TREE_CODE (t) == TREE_LIST);
7253
7254 /* Make sure we don't adjust things twice for templates. */
7255 if (processing_template_decl)
7256 return false;
7257
7258 for (; t; t = TREE_CHAIN (t))
7259 {
7260 tree decl = TREE_VALUE (t);
7261 if (TYPE_PTR_P (TREE_TYPE (decl)))
7262 {
7263 tree offset = TREE_PURPOSE (t);
7264 bool neg = wi::neg_p (wi::to_wide (offset));
7265 offset = fold_unary (ABS_EXPR, TREE_TYPE (offset), offset);
7266 decl = mark_rvalue_use (decl);
7267 decl = convert_from_reference (decl);
7268 tree t2 = pointer_int_sum (OMP_CLAUSE_LOCATION (sink_clause),
7269 neg ? MINUS_EXPR : PLUS_EXPR,
7270 decl, offset);
7271 t2 = fold_build2_loc (OMP_CLAUSE_LOCATION (sink_clause),
7272 MINUS_EXPR, sizetype,
7273 fold_convert (sizetype, t2),
7274 fold_convert (sizetype, decl));
7275 if (t2 == error_mark_node)
7276 return true;
7277 TREE_PURPOSE (t) = t2;
7278 }
7279 }
7280 return false;
7281 }
7282
7283 /* Finish OpenMP iterators ITER. Return true if they are errorneous
7284 and clauses containing them should be removed. */
7285
7286 static bool
7287 cp_omp_finish_iterators (tree iter)
7288 {
7289 bool ret = false;
7290 for (tree it = iter; it; it = TREE_CHAIN (it))
7291 {
7292 tree var = TREE_VEC_ELT (it, 0);
7293 tree begin = TREE_VEC_ELT (it, 1);
7294 tree end = TREE_VEC_ELT (it, 2);
7295 tree step = TREE_VEC_ELT (it, 3);
7296 tree orig_step;
7297 tree type = TREE_TYPE (var);
7298 location_t loc = DECL_SOURCE_LOCATION (var);
7299 if (type == error_mark_node)
7300 {
7301 ret = true;
7302 continue;
7303 }
7304 if (type_dependent_expression_p (var))
7305 continue;
7306 if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
7307 {
7308 error_at (loc, "iterator %qD has neither integral nor pointer type",
7309 var);
7310 ret = true;
7311 continue;
7312 }
7313 else if (TYPE_READONLY (type))
7314 {
7315 error_at (loc, "iterator %qD has const qualified type", var);
7316 ret = true;
7317 continue;
7318 }
7319 if (type_dependent_expression_p (begin)
7320 || type_dependent_expression_p (end)
7321 || type_dependent_expression_p (step))
7322 continue;
7323 else if (error_operand_p (step))
7324 {
7325 ret = true;
7326 continue;
7327 }
7328 else if (!INTEGRAL_TYPE_P (TREE_TYPE (step)))
7329 {
7330 error_at (EXPR_LOC_OR_LOC (step, loc),
7331 "iterator step with non-integral type");
7332 ret = true;
7333 continue;
7334 }
7335
7336 begin = mark_rvalue_use (begin);
7337 end = mark_rvalue_use (end);
7338 step = mark_rvalue_use (step);
7339 begin = cp_build_c_cast (input_location, type, begin,
7340 tf_warning_or_error);
7341 end = cp_build_c_cast (input_location, type, end,
7342 tf_warning_or_error);
7343 orig_step = step;
7344 if (!processing_template_decl)
7345 step = orig_step = save_expr (step);
7346 tree stype = POINTER_TYPE_P (type) ? sizetype : type;
7347 step = cp_build_c_cast (input_location, stype, step,
7348 tf_warning_or_error);
7349 if (POINTER_TYPE_P (type) && !processing_template_decl)
7350 {
7351 begin = save_expr (begin);
7352 step = pointer_int_sum (loc, PLUS_EXPR, begin, step);
7353 step = fold_build2_loc (loc, MINUS_EXPR, sizetype,
7354 fold_convert (sizetype, step),
7355 fold_convert (sizetype, begin));
7356 step = fold_convert (ssizetype, step);
7357 }
7358 if (!processing_template_decl)
7359 {
7360 begin = maybe_constant_value (begin);
7361 end = maybe_constant_value (end);
7362 step = maybe_constant_value (step);
7363 orig_step = maybe_constant_value (orig_step);
7364 }
7365 if (integer_zerop (step))
7366 {
7367 error_at (loc, "iterator %qD has zero step", var);
7368 ret = true;
7369 continue;
7370 }
7371
7372 if (begin == error_mark_node
7373 || end == error_mark_node
7374 || step == error_mark_node
7375 || orig_step == error_mark_node)
7376 {
7377 ret = true;
7378 continue;
7379 }
7380
7381 if (!processing_template_decl)
7382 {
7383 begin = fold_build_cleanup_point_expr (TREE_TYPE (begin), begin);
7384 end = fold_build_cleanup_point_expr (TREE_TYPE (end), end);
7385 step = fold_build_cleanup_point_expr (TREE_TYPE (step), step);
7386 orig_step = fold_build_cleanup_point_expr (TREE_TYPE (orig_step),
7387 orig_step);
7388 }
7389 hash_set<tree> pset;
7390 tree it2;
7391 for (it2 = TREE_CHAIN (it); it2; it2 = TREE_CHAIN (it2))
7392 {
7393 tree var2 = TREE_VEC_ELT (it2, 0);
7394 tree begin2 = TREE_VEC_ELT (it2, 1);
7395 tree end2 = TREE_VEC_ELT (it2, 2);
7396 tree step2 = TREE_VEC_ELT (it2, 3);
7397 location_t loc2 = DECL_SOURCE_LOCATION (var2);
7398 if (cp_walk_tree (&begin2, find_omp_placeholder_r, var, &pset))
7399 {
7400 error_at (EXPR_LOC_OR_LOC (begin2, loc2),
7401 "begin expression refers to outer iterator %qD", var);
7402 break;
7403 }
7404 else if (cp_walk_tree (&end2, find_omp_placeholder_r, var, &pset))
7405 {
7406 error_at (EXPR_LOC_OR_LOC (end2, loc2),
7407 "end expression refers to outer iterator %qD", var);
7408 break;
7409 }
7410 else if (cp_walk_tree (&step2, find_omp_placeholder_r, var, &pset))
7411 {
7412 error_at (EXPR_LOC_OR_LOC (step2, loc2),
7413 "step expression refers to outer iterator %qD", var);
7414 break;
7415 }
7416 }
7417 if (it2)
7418 {
7419 ret = true;
7420 continue;
7421 }
7422 TREE_VEC_ELT (it, 1) = begin;
7423 TREE_VEC_ELT (it, 2) = end;
7424 if (processing_template_decl)
7425 TREE_VEC_ELT (it, 3) = orig_step;
7426 else
7427 {
7428 TREE_VEC_ELT (it, 3) = step;
7429 TREE_VEC_ELT (it, 4) = orig_step;
7430 }
7431 }
7432 return ret;
7433 }
7434
7435 /* Ensure that pointers are used in OpenACC attach and detach clauses.
7436 Return true if an error has been detected. */
7437
7438 static bool
7439 cp_oacc_check_attachments (tree c)
7440 {
7441 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
7442 return false;
7443
7444 /* OpenACC attach / detach clauses must be pointers. */
7445 if (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH
7446 || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_DETACH)
7447 {
7448 tree t = OMP_CLAUSE_DECL (c);
7449 tree type;
7450
7451 while (TREE_CODE (t) == OMP_ARRAY_SECTION)
7452 t = TREE_OPERAND (t, 0);
7453
7454 type = TREE_TYPE (t);
7455
7456 if (TREE_CODE (type) == REFERENCE_TYPE)
7457 type = TREE_TYPE (type);
7458
7459 if (TREE_CODE (type) != POINTER_TYPE)
7460 {
7461 error_at (OMP_CLAUSE_LOCATION (c), "expected pointer in %qs clause",
7462 user_omp_clause_code_name (c, true));
7463 return true;
7464 }
7465 }
7466
7467 return false;
7468 }
7469
7470 /* Update OMP_CLAUSE_INIT_PREFER_TYPE in case template substitution
7471 happened. */
7472
7473 tree
7474 cp_finish_omp_init_prefer_type (tree pref_type)
7475 {
7476 if (processing_template_decl
7477 || pref_type == NULL_TREE
7478 || TREE_CODE (pref_type) != TREE_LIST)
7479 return pref_type;
7480
7481 tree t = TREE_PURPOSE (pref_type);
7482 char *str = const_cast<char *> (TREE_STRING_POINTER (t));
7483 tree fr_list = TREE_VALUE (pref_type);
7484 int len = TREE_VEC_LENGTH (fr_list);
7485 int cnt = 0;
7486
7487 while (str[0] == (char) GOMP_INTEROP_IFR_SEPARATOR)
7488 {
7489 str++;
7490 if (str[0] == (char) GOMP_INTEROP_IFR_UNKNOWN)
7491 {
7492 /* Assume a no or a single 'fr'. */
7493 gcc_checking_assert (str[1] == (char) GOMP_INTEROP_IFR_SEPARATOR);
7494 location_t loc = UNKNOWN_LOCATION;
7495 tree value = TREE_VEC_ELT (fr_list, cnt);
7496 if (value != NULL_TREE && value != error_mark_node)
7497 {
7498 loc = EXPR_LOCATION (value);
7499 if (value && TREE_CODE (value) == NOP_EXPR)
7500 value = TREE_OPERAND (value, 0);
7501 value = cp_fully_fold (value);
7502 }
7503 if (value != NULL_TREE && value != error_mark_node)
7504 {
7505 if (TREE_CODE (value) != INTEGER_CST
7506 || !tree_fits_shwi_p (value))
7507 error_at (loc,
7508 "expected string literal or "
7509 "constant integer expression instead of %qE", value);
7510 else
7511 {
7512 HOST_WIDE_INT n = tree_to_shwi (value);
7513 if (n < 1 || n > GOMP_INTEROP_IFR_LAST)
7514 {
7515 warning_at (loc, OPT_Wopenmp,
7516 "unknown foreign runtime identifier %qwd", n);
7517 n = GOMP_INTEROP_IFR_UNKNOWN;
7518 }
7519 str[0] = (char) n;
7520 }
7521 }
7522 str++;
7523 }
7524 else if (str[0] != (char) GOMP_INTEROP_IFR_SEPARATOR)
7525 {
7526 /* Assume a no or a single 'fr'. */
7527 gcc_checking_assert (str[1] == (char) GOMP_INTEROP_IFR_SEPARATOR);
7528 str++;
7529 }
7530 str++;
7531 while (str[0] != '\0')
7532 str += strlen (str) + 1;
7533 str++;
7534 cnt++;
7535 if (cnt >= len)
7536 break;
7537 }
7538 return t;
7539 }
7540
7541 /* For all elements of CLAUSES, validate them vs OpenMP constraints.
7542 Remove any elements from the list that are invalid. */
7543
7544 tree
7545 finish_omp_clauses (tree clauses, enum c_omp_region_type ort)
7546 {
7547 bitmap_head generic_head, firstprivate_head, lastprivate_head;
7548 bitmap_head aligned_head, map_head, map_field_head, map_firstprivate_head;
7549 bitmap_head oacc_reduction_head, is_on_device_head;
7550 tree c, t, *pc;
7551 tree safelen = NULL_TREE;
7552 bool openacc = (ort & C_ORT_ACC) != 0;
7553 bool branch_seen = false;
7554 bool copyprivate_seen = false;
7555 bool ordered_seen = false;
7556 bool order_seen = false;
7557 bool schedule_seen = false;
7558 bool oacc_async = false;
7559 bool indir_component_ref_p = false;
7560 tree last_iterators = NULL_TREE;
7561 bool last_iterators_remove = false;
7562 /* 1 if normal/task reduction has been seen, -1 if inscan reduction
7563 has been seen, -2 if mixed inscan/normal reduction diagnosed. */
7564 int reduction_seen = 0;
7565 bool allocate_seen = false;
7566 tree detach_seen = NULL_TREE;
7567 bool mergeable_seen = false;
7568 bool implicit_moved = false;
7569 bool target_in_reduction_seen = false;
7570 bool num_tasks_seen = false;
7571 bool partial_seen = false;
7572 bool init_seen = false;
7573 bool init_use_destroy_seen = false;
7574 tree init_no_targetsync_clause = NULL_TREE;
7575 tree depend_clause = NULL_TREE;
7576
7577 bitmap_obstack_initialize (NULL);
7578 bitmap_initialize (&generic_head, &bitmap_default_obstack);
7579 bitmap_initialize (&firstprivate_head, &bitmap_default_obstack);
7580 bitmap_initialize (&lastprivate_head, &bitmap_default_obstack);
7581 bitmap_initialize (&aligned_head, &bitmap_default_obstack);
7582 /* If ort == C_ORT_OMP_DECLARE_SIMD used as uniform_head instead. */
7583 bitmap_initialize (&map_head, &bitmap_default_obstack);
7584 bitmap_initialize (&map_field_head, &bitmap_default_obstack);
7585 bitmap_initialize (&map_firstprivate_head, &bitmap_default_obstack);
7586 /* If ort == C_ORT_OMP used as nontemporal_head or use_device_xxx_head
7587 instead and for ort == C_ORT_OMP_TARGET used as in_reduction_head. */
7588 bitmap_initialize (&oacc_reduction_head, &bitmap_default_obstack);
7589 bitmap_initialize (&is_on_device_head, &bitmap_default_obstack);
7590
7591 if (openacc)
7592 for (c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
7593 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ASYNC)
7594 {
7595 oacc_async = true;
7596 break;
7597 }
7598
7599 tree *grp_start_p = NULL, grp_sentinel = NULL_TREE;
7600
7601 for (pc = &clauses, c = clauses; c ; c = *pc)
7602 {
7603 bool remove = false;
7604 bool field_ok = false;
7605
7606 /* We've reached the end of a list of expanded nodes. Reset the group
7607 start pointer. */
7608 if (c == grp_sentinel)
7609 grp_start_p = NULL;
7610
7611 switch (OMP_CLAUSE_CODE (c))
7612 {
7613 case OMP_CLAUSE_SHARED:
7614 field_ok = ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP);
7615 goto check_dup_generic;
7616 case OMP_CLAUSE_PRIVATE:
7617 field_ok = ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP);
7618 goto check_dup_generic;
7619 case OMP_CLAUSE_REDUCTION:
7620 if (reduction_seen == 0)
7621 reduction_seen = OMP_CLAUSE_REDUCTION_INSCAN (c) ? -1 : 1;
7622 else if (reduction_seen != -2
7623 && reduction_seen != (OMP_CLAUSE_REDUCTION_INSCAN (c)
7624 ? -1 : 1))
7625 {
7626 error_at (OMP_CLAUSE_LOCATION (c),
7627 "%<inscan%> and non-%<inscan%> %<reduction%> clauses "
7628 "on the same construct");
7629 reduction_seen = -2;
7630 }
7631 /* FALLTHRU */
7632 case OMP_CLAUSE_IN_REDUCTION:
7633 case OMP_CLAUSE_TASK_REDUCTION:
7634 field_ok = ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP);
7635 t = OMP_CLAUSE_DECL (c);
7636 if (TREE_CODE (t) == OMP_ARRAY_SECTION)
7637 {
7638 if (handle_omp_array_sections (c, ort))
7639 {
7640 remove = true;
7641 break;
7642 }
7643 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
7644 && OMP_CLAUSE_REDUCTION_INSCAN (c))
7645 {
7646 error_at (OMP_CLAUSE_LOCATION (c),
7647 "%<inscan%> %<reduction%> clause with array "
7648 "section");
7649 remove = true;
7650 break;
7651 }
7652 if (TREE_CODE (t) == OMP_ARRAY_SECTION)
7653 {
7654 while (TREE_CODE (t) == OMP_ARRAY_SECTION)
7655 t = TREE_OPERAND (t, 0);
7656 }
7657 else
7658 {
7659 gcc_assert (TREE_CODE (t) == MEM_REF);
7660 t = TREE_OPERAND (t, 0);
7661 if (TREE_CODE (t) == POINTER_PLUS_EXPR)
7662 t = TREE_OPERAND (t, 0);
7663 if (TREE_CODE (t) == ADDR_EXPR
7664 || INDIRECT_REF_P (t))
7665 t = TREE_OPERAND (t, 0);
7666 }
7667 tree n = omp_clause_decl_field (t);
7668 if (n)
7669 t = n;
7670 goto check_dup_generic_t;
7671 }
7672 if (oacc_async)
7673 cxx_mark_addressable (t);
7674 goto check_dup_generic;
7675 case OMP_CLAUSE_COPYPRIVATE:
7676 copyprivate_seen = true;
7677 field_ok = ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP);
7678 goto check_dup_generic;
7679 case OMP_CLAUSE_COPYIN:
7680 goto check_dup_generic;
7681 case OMP_CLAUSE_LINEAR:
7682 field_ok = ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP);
7683 t = OMP_CLAUSE_DECL (c);
7684 if (ort != C_ORT_OMP_DECLARE_SIMD
7685 && OMP_CLAUSE_LINEAR_KIND (c) != OMP_CLAUSE_LINEAR_DEFAULT)
7686 {
7687 if (OMP_CLAUSE_LINEAR_OLD_LINEAR_MODIFIER (c))
7688 {
7689 error_at (OMP_CLAUSE_LOCATION (c),
7690 "modifier should not be specified in %<linear%> "
7691 "clause on %<simd%> or %<for%> constructs when "
7692 "not using OpenMP 5.2 modifiers");
7693 OMP_CLAUSE_LINEAR_KIND (c) = OMP_CLAUSE_LINEAR_DEFAULT;
7694 }
7695 else if (OMP_CLAUSE_LINEAR_KIND (c) != OMP_CLAUSE_LINEAR_VAL)
7696 {
7697 error_at (OMP_CLAUSE_LOCATION (c),
7698 "modifier other than %<val%> specified in "
7699 "%<linear%> clause on %<simd%> or %<for%> "
7700 "constructs when using OpenMP 5.2 modifiers");
7701 OMP_CLAUSE_LINEAR_KIND (c) = OMP_CLAUSE_LINEAR_DEFAULT;
7702 }
7703 }
7704 if ((VAR_P (t) || TREE_CODE (t) == PARM_DECL)
7705 && !type_dependent_expression_p (t))
7706 {
7707 tree type = TREE_TYPE (t);
7708 if ((OMP_CLAUSE_LINEAR_KIND (c) == OMP_CLAUSE_LINEAR_REF
7709 || OMP_CLAUSE_LINEAR_KIND (c) == OMP_CLAUSE_LINEAR_UVAL)
7710 && !TYPE_REF_P (type))
7711 {
7712 error_at (OMP_CLAUSE_LOCATION (c),
7713 "linear clause with %qs modifier applied to "
7714 "non-reference variable with %qT type",
7715 OMP_CLAUSE_LINEAR_KIND (c) == OMP_CLAUSE_LINEAR_REF
7716 ? "ref" : "uval", TREE_TYPE (t));
7717 remove = true;
7718 break;
7719 }
7720 if (TYPE_REF_P (type))
7721 type = TREE_TYPE (type);
7722 if (OMP_CLAUSE_LINEAR_KIND (c) != OMP_CLAUSE_LINEAR_REF)
7723 {
7724 if (!INTEGRAL_TYPE_P (type)
7725 && !TYPE_PTR_P (type))
7726 {
7727 error_at (OMP_CLAUSE_LOCATION (c),
7728 "linear clause applied to non-integral "
7729 "non-pointer variable with %qT type",
7730 TREE_TYPE (t));
7731 remove = true;
7732 break;
7733 }
7734 }
7735 }
7736 t = OMP_CLAUSE_LINEAR_STEP (c);
7737 if (t == NULL_TREE)
7738 t = integer_one_node;
7739 if (t == error_mark_node)
7740 {
7741 remove = true;
7742 break;
7743 }
7744 else if (!type_dependent_expression_p (t)
7745 && !INTEGRAL_TYPE_P (TREE_TYPE (t))
7746 && (ort != C_ORT_OMP_DECLARE_SIMD
7747 || TREE_CODE (t) != PARM_DECL
7748 || !TYPE_REF_P (TREE_TYPE (t))
7749 || !INTEGRAL_TYPE_P (TREE_TYPE (TREE_TYPE (t)))))
7750 {
7751 error_at (OMP_CLAUSE_LOCATION (c),
7752 "linear step expression must be integral");
7753 remove = true;
7754 break;
7755 }
7756 else
7757 {
7758 t = mark_rvalue_use (t);
7759 if (ort == C_ORT_OMP_DECLARE_SIMD && TREE_CODE (t) == PARM_DECL)
7760 {
7761 OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (c) = 1;
7762 goto check_dup_generic;
7763 }
7764 if (!processing_template_decl
7765 && (VAR_P (OMP_CLAUSE_DECL (c))
7766 || TREE_CODE (OMP_CLAUSE_DECL (c)) == PARM_DECL))
7767 {
7768 if (ort == C_ORT_OMP_DECLARE_SIMD)
7769 {
7770 t = maybe_constant_value (t);
7771 if (TREE_CODE (t) != INTEGER_CST)
7772 {
7773 error_at (OMP_CLAUSE_LOCATION (c),
7774 "%<linear%> clause step %qE is neither "
7775 "constant nor a parameter", t);
7776 remove = true;
7777 break;
7778 }
7779 }
7780 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
7781 tree type = TREE_TYPE (OMP_CLAUSE_DECL (c));
7782 if (TYPE_REF_P (type))
7783 type = TREE_TYPE (type);
7784 if (OMP_CLAUSE_LINEAR_KIND (c) == OMP_CLAUSE_LINEAR_REF)
7785 {
7786 type = build_pointer_type (type);
7787 tree d = fold_convert (type, OMP_CLAUSE_DECL (c));
7788 t = pointer_int_sum (OMP_CLAUSE_LOCATION (c), PLUS_EXPR,
7789 d, t);
7790 t = fold_build2_loc (OMP_CLAUSE_LOCATION (c),
7791 MINUS_EXPR, sizetype,
7792 fold_convert (sizetype, t),
7793 fold_convert (sizetype, d));
7794 if (t == error_mark_node)
7795 {
7796 remove = true;
7797 break;
7798 }
7799 }
7800 else if (TYPE_PTR_P (type)
7801 /* Can't multiply the step yet if *this
7802 is still incomplete type. */
7803 && (ort != C_ORT_OMP_DECLARE_SIMD
7804 || TREE_CODE (OMP_CLAUSE_DECL (c)) != PARM_DECL
7805 || !DECL_ARTIFICIAL (OMP_CLAUSE_DECL (c))
7806 || DECL_NAME (OMP_CLAUSE_DECL (c))
7807 != this_identifier
7808 || !TYPE_BEING_DEFINED (TREE_TYPE (type))))
7809 {
7810 tree d = convert_from_reference (OMP_CLAUSE_DECL (c));
7811 t = pointer_int_sum (OMP_CLAUSE_LOCATION (c), PLUS_EXPR,
7812 d, t);
7813 t = fold_build2_loc (OMP_CLAUSE_LOCATION (c),
7814 MINUS_EXPR, sizetype,
7815 fold_convert (sizetype, t),
7816 fold_convert (sizetype, d));
7817 if (t == error_mark_node)
7818 {
7819 remove = true;
7820 break;
7821 }
7822 }
7823 else
7824 t = fold_convert (type, t);
7825 }
7826 OMP_CLAUSE_LINEAR_STEP (c) = t;
7827 }
7828 goto check_dup_generic;
7829 check_dup_generic:
7830 t = omp_clause_decl_field (OMP_CLAUSE_DECL (c));
7831 if (t)
7832 {
7833 if (!remove && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_SHARED)
7834 omp_note_field_privatization (t, OMP_CLAUSE_DECL (c));
7835 }
7836 else
7837 t = OMP_CLAUSE_DECL (c);
7838 check_dup_generic_t:
7839 if (t == current_class_ptr
7840 && ((ort != C_ORT_OMP_DECLARE_SIMD && !openacc)
7841 || (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_LINEAR
7842 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_UNIFORM)))
7843 {
7844 error_at (OMP_CLAUSE_LOCATION (c),
7845 "%<this%> allowed in OpenMP only in %<declare simd%>"
7846 " clauses");
7847 remove = true;
7848 break;
7849 }
7850 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL
7851 && (!field_ok || TREE_CODE (t) != FIELD_DECL))
7852 {
7853 if (processing_template_decl && TREE_CODE (t) != OVERLOAD)
7854 break;
7855 if (DECL_P (t))
7856 error_at (OMP_CLAUSE_LOCATION (c),
7857 "%qD is not a variable in clause %qs", t,
7858 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
7859 else
7860 error_at (OMP_CLAUSE_LOCATION (c),
7861 "%qE is not a variable in clause %qs", t,
7862 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
7863 remove = true;
7864 }
7865 else if ((openacc
7866 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
7867 || (ort == C_ORT_OMP
7868 && (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_USE_DEVICE_PTR
7869 || (OMP_CLAUSE_CODE (c)
7870 == OMP_CLAUSE_USE_DEVICE_ADDR)))
7871 || (ort == C_ORT_OMP_TARGET
7872 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION))
7873 {
7874 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
7875 && (bitmap_bit_p (&generic_head, DECL_UID (t))
7876 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))))
7877 {
7878 error_at (OMP_CLAUSE_LOCATION (c),
7879 "%qD appears more than once in data-sharing "
7880 "clauses", t);
7881 remove = true;
7882 break;
7883 }
7884 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION)
7885 target_in_reduction_seen = true;
7886 if (bitmap_bit_p (&oacc_reduction_head, DECL_UID (t)))
7887 {
7888 error_at (OMP_CLAUSE_LOCATION (c),
7889 openacc
7890 ? "%qD appears more than once in reduction clauses"
7891 : "%qD appears more than once in data clauses",
7892 t);
7893 remove = true;
7894 }
7895 else
7896 bitmap_set_bit (&oacc_reduction_head, DECL_UID (t));
7897 }
7898 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
7899 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
7900 || bitmap_bit_p (&lastprivate_head, DECL_UID (t))
7901 || bitmap_bit_p (&map_firstprivate_head, DECL_UID (t)))
7902 {
7903 error_at (OMP_CLAUSE_LOCATION (c),
7904 "%qD appears more than once in data clauses", t);
7905 remove = true;
7906 }
7907 else if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
7908 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_HAS_DEVICE_ADDR
7909 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IS_DEVICE_PTR)
7910 && bitmap_bit_p (&map_head, DECL_UID (t)))
7911 {
7912 if (openacc)
7913 error_at (OMP_CLAUSE_LOCATION (c),
7914 "%qD appears more than once in data clauses", t);
7915 else
7916 error_at (OMP_CLAUSE_LOCATION (c),
7917 "%qD appears both in data and map clauses", t);
7918 remove = true;
7919 }
7920 else
7921 bitmap_set_bit (&generic_head, DECL_UID (t));
7922 if (!field_ok)
7923 break;
7924 handle_field_decl:
7925 if (!remove
7926 && TREE_CODE (t) == FIELD_DECL
7927 && t == OMP_CLAUSE_DECL (c))
7928 {
7929 OMP_CLAUSE_DECL (c)
7930 = omp_privatize_field (t, (OMP_CLAUSE_CODE (c)
7931 == OMP_CLAUSE_SHARED));
7932 if (OMP_CLAUSE_DECL (c) == error_mark_node)
7933 remove = true;
7934 }
7935 break;
7936
7937 case OMP_CLAUSE_FIRSTPRIVATE:
7938 if (OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT (c) && !implicit_moved)
7939 {
7940 move_implicit:
7941 implicit_moved = true;
7942 /* Move firstprivate and map clauses with
7943 OMP_CLAUSE_{FIRSTPRIVATE,MAP}_IMPLICIT set to the end of
7944 clauses chain. */
7945 tree cl1 = NULL_TREE, cl2 = NULL_TREE;
7946 tree *pc1 = pc, *pc2 = &cl1, *pc3 = &cl2;
7947 while (*pc1)
7948 if (OMP_CLAUSE_CODE (*pc1) == OMP_CLAUSE_FIRSTPRIVATE
7949 && OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT (*pc1))
7950 {
7951 *pc3 = *pc1;
7952 pc3 = &OMP_CLAUSE_CHAIN (*pc3);
7953 *pc1 = OMP_CLAUSE_CHAIN (*pc1);
7954 }
7955 else if (OMP_CLAUSE_CODE (*pc1) == OMP_CLAUSE_MAP
7956 && OMP_CLAUSE_MAP_IMPLICIT (*pc1))
7957 {
7958 *pc2 = *pc1;
7959 pc2 = &OMP_CLAUSE_CHAIN (*pc2);
7960 *pc1 = OMP_CLAUSE_CHAIN (*pc1);
7961 }
7962 else
7963 pc1 = &OMP_CLAUSE_CHAIN (*pc1);
7964 *pc3 = NULL;
7965 *pc2 = cl2;
7966 *pc1 = cl1;
7967 continue;
7968 }
7969 t = omp_clause_decl_field (OMP_CLAUSE_DECL (c));
7970 if (t)
7971 omp_note_field_privatization (t, OMP_CLAUSE_DECL (c));
7972 else
7973 t = OMP_CLAUSE_DECL (c);
7974 if (!openacc && t == current_class_ptr)
7975 {
7976 error_at (OMP_CLAUSE_LOCATION (c),
7977 "%<this%> allowed in OpenMP only in %<declare simd%>"
7978 " clauses");
7979 remove = true;
7980 break;
7981 }
7982 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL
7983 && ((ort & C_ORT_OMP_DECLARE_SIMD) != C_ORT_OMP
7984 || TREE_CODE (t) != FIELD_DECL))
7985 {
7986 if (processing_template_decl && TREE_CODE (t) != OVERLOAD)
7987 break;
7988 if (DECL_P (t))
7989 error_at (OMP_CLAUSE_LOCATION (c),
7990 "%qD is not a variable in clause %<firstprivate%>",
7991 t);
7992 else
7993 error_at (OMP_CLAUSE_LOCATION (c),
7994 "%qE is not a variable in clause %<firstprivate%>",
7995 t);
7996 remove = true;
7997 }
7998 else if (OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT (c)
7999 && !OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT_TARGET (c)
8000 && bitmap_bit_p (&map_firstprivate_head, DECL_UID (t)))
8001 remove = true;
8002 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
8003 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
8004 || bitmap_bit_p (&map_firstprivate_head, DECL_UID (t)))
8005 {
8006 error_at (OMP_CLAUSE_LOCATION (c),
8007 "%qD appears more than once in data clauses", t);
8008 remove = true;
8009 }
8010 else if (bitmap_bit_p (&map_head, DECL_UID (t))
8011 || bitmap_bit_p (&map_field_head, DECL_UID (t)))
8012 {
8013 if (openacc)
8014 error_at (OMP_CLAUSE_LOCATION (c),
8015 "%qD appears more than once in data clauses", t);
8016 else if (OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT (c)
8017 && !OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT_TARGET (c))
8018 /* Silently drop the clause. */;
8019 else
8020 error_at (OMP_CLAUSE_LOCATION (c),
8021 "%qD appears both in data and map clauses", t);
8022 remove = true;
8023 }
8024 else
8025 bitmap_set_bit (&firstprivate_head, DECL_UID (t));
8026 goto handle_field_decl;
8027
8028 case OMP_CLAUSE_LASTPRIVATE:
8029 t = omp_clause_decl_field (OMP_CLAUSE_DECL (c));
8030 if (t)
8031 omp_note_field_privatization (t, OMP_CLAUSE_DECL (c));
8032 else
8033 t = OMP_CLAUSE_DECL (c);
8034 if (!openacc && t == current_class_ptr)
8035 {
8036 error_at (OMP_CLAUSE_LOCATION (c),
8037 "%<this%> allowed in OpenMP only in %<declare simd%>"
8038 " clauses");
8039 remove = true;
8040 break;
8041 }
8042 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL
8043 && ((ort & C_ORT_OMP_DECLARE_SIMD) != C_ORT_OMP
8044 || TREE_CODE (t) != FIELD_DECL))
8045 {
8046 if (processing_template_decl && TREE_CODE (t) != OVERLOAD)
8047 break;
8048 if (DECL_P (t))
8049 error_at (OMP_CLAUSE_LOCATION (c),
8050 "%qD is not a variable in clause %<lastprivate%>",
8051 t);
8052 else
8053 error_at (OMP_CLAUSE_LOCATION (c),
8054 "%qE is not a variable in clause %<lastprivate%>",
8055 t);
8056 remove = true;
8057 }
8058 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
8059 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
8060 {
8061 error_at (OMP_CLAUSE_LOCATION (c),
8062 "%qD appears more than once in data clauses", t);
8063 remove = true;
8064 }
8065 else
8066 bitmap_set_bit (&lastprivate_head, DECL_UID (t));
8067 goto handle_field_decl;
8068
8069 case OMP_CLAUSE_IF:
8070 case OMP_CLAUSE_SELF:
8071 t = OMP_CLAUSE_OPERAND (c, 0);
8072 t = maybe_convert_cond (t);
8073 if (t == error_mark_node)
8074 remove = true;
8075 else if (!processing_template_decl)
8076 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
8077 OMP_CLAUSE_OPERAND (c, 0) = t;
8078 break;
8079
8080 case OMP_CLAUSE_FINAL:
8081 t = OMP_CLAUSE_FINAL_EXPR (c);
8082 t = maybe_convert_cond (t);
8083 if (t == error_mark_node)
8084 remove = true;
8085 else if (!processing_template_decl)
8086 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
8087 OMP_CLAUSE_FINAL_EXPR (c) = t;
8088 break;
8089
8090 case OMP_CLAUSE_NOCONTEXT:
8091 t = OMP_CLAUSE_NOCONTEXT_EXPR (c);
8092 t = maybe_convert_cond (t);
8093 if (t == error_mark_node)
8094 remove = true;
8095 else if (!processing_template_decl)
8096 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
8097 OMP_CLAUSE_NOCONTEXT_EXPR (c) = t;
8098 break;
8099
8100 case OMP_CLAUSE_NOVARIANTS:
8101 t = OMP_CLAUSE_NOVARIANTS_EXPR (c);
8102 t = maybe_convert_cond (t);
8103 if (t == error_mark_node)
8104 remove = true;
8105 else if (!processing_template_decl)
8106 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
8107 OMP_CLAUSE_NOVARIANTS_EXPR (c) = t;
8108 break;
8109
8110 case OMP_CLAUSE_GANG:
8111 /* Operand 1 is the gang static: argument. */
8112 t = OMP_CLAUSE_OPERAND (c, 1);
8113 if (t != NULL_TREE)
8114 {
8115 if (t == error_mark_node)
8116 remove = true;
8117 else if (!type_dependent_expression_p (t)
8118 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
8119 {
8120 error_at (OMP_CLAUSE_LOCATION (c),
8121 "%<gang%> static expression must be integral");
8122 remove = true;
8123 }
8124 else
8125 {
8126 t = mark_rvalue_use (t);
8127 if (!processing_template_decl)
8128 {
8129 t = maybe_constant_value (t);
8130 if (TREE_CODE (t) == INTEGER_CST
8131 && tree_int_cst_sgn (t) != 1
8132 && t != integer_minus_one_node)
8133 {
8134 warning_at (OMP_CLAUSE_LOCATION (c), 0,
8135 "%<gang%> static value must be "
8136 "positive");
8137 t = integer_one_node;
8138 }
8139 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
8140 }
8141 }
8142 OMP_CLAUSE_OPERAND (c, 1) = t;
8143 }
8144 /* Check operand 0, the num argument. */
8145 /* FALLTHRU */
8146
8147 case OMP_CLAUSE_WORKER:
8148 case OMP_CLAUSE_VECTOR:
8149 if (OMP_CLAUSE_OPERAND (c, 0) == NULL_TREE)
8150 break;
8151 /* FALLTHRU */
8152
8153 case OMP_CLAUSE_NUM_TASKS:
8154 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TASKS)
8155 num_tasks_seen = true;
8156 /* FALLTHRU */
8157
8158 case OMP_CLAUSE_NUM_TEAMS:
8159 case OMP_CLAUSE_NUM_THREADS:
8160 case OMP_CLAUSE_NUM_GANGS:
8161 case OMP_CLAUSE_NUM_WORKERS:
8162 case OMP_CLAUSE_VECTOR_LENGTH:
8163 t = OMP_CLAUSE_OPERAND (c, 0);
8164 if (t == error_mark_node)
8165 remove = true;
8166 else if (!type_dependent_expression_p (t)
8167 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
8168 {
8169 switch (OMP_CLAUSE_CODE (c))
8170 {
8171 case OMP_CLAUSE_GANG:
8172 error_at (OMP_CLAUSE_LOCATION (c),
8173 "%<gang%> num expression must be integral"); break;
8174 case OMP_CLAUSE_VECTOR:
8175 error_at (OMP_CLAUSE_LOCATION (c),
8176 "%<vector%> length expression must be integral");
8177 break;
8178 case OMP_CLAUSE_WORKER:
8179 error_at (OMP_CLAUSE_LOCATION (c),
8180 "%<worker%> num expression must be integral");
8181 break;
8182 default:
8183 error_at (OMP_CLAUSE_LOCATION (c),
8184 "%qs expression must be integral",
8185 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
8186 }
8187 remove = true;
8188 }
8189 else
8190 {
8191 t = mark_rvalue_use (t);
8192 if (!processing_template_decl)
8193 {
8194 t = maybe_constant_value (t);
8195 if (TREE_CODE (t) == INTEGER_CST
8196 && tree_int_cst_sgn (t) != 1)
8197 {
8198 switch (OMP_CLAUSE_CODE (c))
8199 {
8200 case OMP_CLAUSE_GANG:
8201 warning_at (OMP_CLAUSE_LOCATION (c), 0,
8202 "%<gang%> num value must be positive");
8203 break;
8204 case OMP_CLAUSE_VECTOR:
8205 warning_at (OMP_CLAUSE_LOCATION (c), 0,
8206 "%<vector%> length value must be "
8207 "positive");
8208 break;
8209 case OMP_CLAUSE_WORKER:
8210 warning_at (OMP_CLAUSE_LOCATION (c), 0,
8211 "%<worker%> num value must be "
8212 "positive");
8213 break;
8214 default:
8215 warning_at (OMP_CLAUSE_LOCATION (c),
8216 (flag_openmp || flag_openmp_simd)
8217 ? OPT_Wopenmp : 0,
8218 "%qs value must be positive",
8219 omp_clause_code_name
8220 [OMP_CLAUSE_CODE (c)]);
8221 }
8222 t = integer_one_node;
8223 }
8224 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
8225 }
8226 OMP_CLAUSE_OPERAND (c, 0) = t;
8227 }
8228 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
8229 && OMP_CLAUSE_NUM_TEAMS_LOWER_EXPR (c)
8230 && !remove)
8231 {
8232 t = OMP_CLAUSE_NUM_TEAMS_LOWER_EXPR (c);
8233 if (t == error_mark_node)
8234 remove = true;
8235 else if (!type_dependent_expression_p (t)
8236 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
8237 {
8238 error_at (OMP_CLAUSE_LOCATION (c),
8239 "%qs expression must be integral",
8240 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
8241 remove = true;
8242 }
8243 else
8244 {
8245 t = mark_rvalue_use (t);
8246 if (!processing_template_decl)
8247 {
8248 t = maybe_constant_value (t);
8249 if (TREE_CODE (t) == INTEGER_CST
8250 && tree_int_cst_sgn (t) != 1)
8251 {
8252 warning_at (OMP_CLAUSE_LOCATION (c), OPT_Wopenmp,
8253 "%qs value must be positive",
8254 omp_clause_code_name
8255 [OMP_CLAUSE_CODE (c)]);
8256 t = NULL_TREE;
8257 }
8258 else
8259 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
8260 tree upper = OMP_CLAUSE_NUM_TEAMS_UPPER_EXPR (c);
8261 if (t
8262 && TREE_CODE (t) == INTEGER_CST
8263 && TREE_CODE (upper) == INTEGER_CST
8264 && tree_int_cst_lt (upper, t))
8265 {
8266 warning_at (OMP_CLAUSE_LOCATION (c), OPT_Wopenmp,
8267 "%<num_teams%> lower bound %qE bigger "
8268 "than upper bound %qE", t, upper);
8269 t = NULL_TREE;
8270 }
8271 }
8272 OMP_CLAUSE_NUM_TEAMS_LOWER_EXPR (c) = t;
8273 }
8274 }
8275 break;
8276
8277 case OMP_CLAUSE_SCHEDULE:
8278 t = OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c);
8279 if (t == NULL)
8280 ;
8281 else if (t == error_mark_node)
8282 remove = true;
8283 else if (!type_dependent_expression_p (t)
8284 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
8285 {
8286 error_at (OMP_CLAUSE_LOCATION (c),
8287 "schedule chunk size expression must be integral");
8288 remove = true;
8289 }
8290 else
8291 {
8292 t = mark_rvalue_use (t);
8293 if (!processing_template_decl)
8294 {
8295 t = maybe_constant_value (t);
8296 if (TREE_CODE (t) == INTEGER_CST
8297 && tree_int_cst_sgn (t) != 1)
8298 {
8299 warning_at (OMP_CLAUSE_LOCATION (c), OPT_Wopenmp,
8300 "chunk size value must be positive");
8301 t = integer_one_node;
8302 }
8303 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
8304 }
8305 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
8306 }
8307 if (!remove)
8308 schedule_seen = true;
8309 break;
8310
8311 case OMP_CLAUSE_SIMDLEN:
8312 case OMP_CLAUSE_SAFELEN:
8313 t = OMP_CLAUSE_OPERAND (c, 0);
8314 if (t == error_mark_node)
8315 remove = true;
8316 else if (!type_dependent_expression_p (t)
8317 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
8318 {
8319 error_at (OMP_CLAUSE_LOCATION (c),
8320 "%qs length expression must be integral",
8321 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
8322 remove = true;
8323 }
8324 else
8325 {
8326 t = mark_rvalue_use (t);
8327 if (!processing_template_decl)
8328 {
8329 t = maybe_constant_value (t);
8330 if (TREE_CODE (t) != INTEGER_CST
8331 || tree_int_cst_sgn (t) != 1)
8332 {
8333 error_at (OMP_CLAUSE_LOCATION (c),
8334 "%qs length expression must be positive "
8335 "constant integer expression",
8336 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
8337 remove = true;
8338 }
8339 }
8340 OMP_CLAUSE_OPERAND (c, 0) = t;
8341 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SAFELEN)
8342 safelen = c;
8343 }
8344 break;
8345
8346 case OMP_CLAUSE_ASYNC:
8347 t = OMP_CLAUSE_ASYNC_EXPR (c);
8348 if (t == error_mark_node)
8349 remove = true;
8350 else if (!type_dependent_expression_p (t)
8351 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
8352 {
8353 error_at (OMP_CLAUSE_LOCATION (c),
8354 "%<async%> expression must be integral");
8355 remove = true;
8356 }
8357 else
8358 {
8359 t = mark_rvalue_use (t);
8360 if (!processing_template_decl)
8361 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
8362 OMP_CLAUSE_ASYNC_EXPR (c) = t;
8363 }
8364 break;
8365
8366 case OMP_CLAUSE_WAIT:
8367 t = OMP_CLAUSE_WAIT_EXPR (c);
8368 if (t == error_mark_node)
8369 remove = true;
8370 else if (!processing_template_decl)
8371 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
8372 OMP_CLAUSE_WAIT_EXPR (c) = t;
8373 break;
8374
8375 case OMP_CLAUSE_THREAD_LIMIT:
8376 t = OMP_CLAUSE_THREAD_LIMIT_EXPR (c);
8377 if (t == error_mark_node)
8378 remove = true;
8379 else if (!type_dependent_expression_p (t)
8380 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
8381 {
8382 error_at (OMP_CLAUSE_LOCATION (c),
8383 "%<thread_limit%> expression must be integral");
8384 remove = true;
8385 }
8386 else
8387 {
8388 t = mark_rvalue_use (t);
8389 if (!processing_template_decl)
8390 {
8391 t = maybe_constant_value (t);
8392 if (TREE_CODE (t) == INTEGER_CST
8393 && tree_int_cst_sgn (t) != 1)
8394 {
8395 warning_at (OMP_CLAUSE_LOCATION (c), OPT_Wopenmp,
8396 "%<thread_limit%> value must be positive");
8397 t = integer_one_node;
8398 }
8399 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
8400 }
8401 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
8402 }
8403 break;
8404
8405 case OMP_CLAUSE_DEVICE:
8406 t = OMP_CLAUSE_DEVICE_ID (c);
8407 if (t == error_mark_node)
8408 remove = true;
8409 else if (!type_dependent_expression_p (t)
8410 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
8411 {
8412 error_at (OMP_CLAUSE_LOCATION (c),
8413 "%<device%> id must be integral");
8414 remove = true;
8415 }
8416 else if (OMP_CLAUSE_DEVICE_ANCESTOR (c)
8417 && TREE_CODE (t) == INTEGER_CST
8418 && !integer_onep (t))
8419 {
8420 error_at (OMP_CLAUSE_LOCATION (c),
8421 "the %<device%> clause expression must evaluate to "
8422 "%<1%>");
8423 remove = true;
8424 }
8425 else
8426 {
8427 t = mark_rvalue_use (t);
8428 if (!processing_template_decl)
8429 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
8430 OMP_CLAUSE_DEVICE_ID (c) = t;
8431 }
8432 break;
8433
8434 case OMP_CLAUSE_DIST_SCHEDULE:
8435 t = OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c);
8436 if (t == NULL)
8437 ;
8438 else if (t == error_mark_node)
8439 remove = true;
8440 else if (!type_dependent_expression_p (t)
8441 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
8442 {
8443 error_at (OMP_CLAUSE_LOCATION (c),
8444 "%<dist_schedule%> chunk size expression must be "
8445 "integral");
8446 remove = true;
8447 }
8448 else
8449 {
8450 t = mark_rvalue_use (t);
8451 if (!processing_template_decl)
8452 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
8453 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
8454 }
8455 break;
8456
8457 case OMP_CLAUSE_ALIGNED:
8458 t = OMP_CLAUSE_DECL (c);
8459 if (t == current_class_ptr && ort != C_ORT_OMP_DECLARE_SIMD)
8460 {
8461 error_at (OMP_CLAUSE_LOCATION (c),
8462 "%<this%> allowed in OpenMP only in %<declare simd%>"
8463 " clauses");
8464 remove = true;
8465 break;
8466 }
8467 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
8468 {
8469 if (processing_template_decl && TREE_CODE (t) != OVERLOAD)
8470 break;
8471 if (DECL_P (t))
8472 error_at (OMP_CLAUSE_LOCATION (c),
8473 "%qD is not a variable in %<aligned%> clause", t);
8474 else
8475 error_at (OMP_CLAUSE_LOCATION (c),
8476 "%qE is not a variable in %<aligned%> clause", t);
8477 remove = true;
8478 }
8479 else if (!type_dependent_expression_p (t)
8480 && !TYPE_PTR_P (TREE_TYPE (t))
8481 && TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE
8482 && (!TYPE_REF_P (TREE_TYPE (t))
8483 || (!INDIRECT_TYPE_P (TREE_TYPE (TREE_TYPE (t)))
8484 && (TREE_CODE (TREE_TYPE (TREE_TYPE (t)))
8485 != ARRAY_TYPE))))
8486 {
8487 error_at (OMP_CLAUSE_LOCATION (c),
8488 "%qE in %<aligned%> clause is neither a pointer nor "
8489 "an array nor a reference to pointer or array", t);
8490 remove = true;
8491 }
8492 else if (bitmap_bit_p (&aligned_head, DECL_UID (t)))
8493 {
8494 error_at (OMP_CLAUSE_LOCATION (c),
8495 "%qD appears more than once in %<aligned%> clauses",
8496 t);
8497 remove = true;
8498 }
8499 else
8500 bitmap_set_bit (&aligned_head, DECL_UID (t));
8501 t = OMP_CLAUSE_ALIGNED_ALIGNMENT (c);
8502 if (t == error_mark_node)
8503 remove = true;
8504 else if (t == NULL_TREE)
8505 break;
8506 else if (!type_dependent_expression_p (t)
8507 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
8508 {
8509 error_at (OMP_CLAUSE_LOCATION (c),
8510 "%<aligned%> clause alignment expression must "
8511 "be integral");
8512 remove = true;
8513 }
8514 else
8515 {
8516 t = mark_rvalue_use (t);
8517 if (!processing_template_decl)
8518 {
8519 t = maybe_constant_value (t);
8520 if (TREE_CODE (t) != INTEGER_CST
8521 || tree_int_cst_sgn (t) != 1)
8522 {
8523 error_at (OMP_CLAUSE_LOCATION (c),
8524 "%<aligned%> clause alignment expression must "
8525 "be positive constant integer expression");
8526 remove = true;
8527 }
8528 }
8529 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = t;
8530 }
8531 break;
8532
8533 case OMP_CLAUSE_NONTEMPORAL:
8534 t = OMP_CLAUSE_DECL (c);
8535 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
8536 {
8537 if (processing_template_decl && TREE_CODE (t) != OVERLOAD)
8538 break;
8539 if (DECL_P (t))
8540 error_at (OMP_CLAUSE_LOCATION (c),
8541 "%qD is not a variable in %<nontemporal%> clause",
8542 t);
8543 else
8544 error_at (OMP_CLAUSE_LOCATION (c),
8545 "%qE is not a variable in %<nontemporal%> clause",
8546 t);
8547 remove = true;
8548 }
8549 else if (bitmap_bit_p (&oacc_reduction_head, DECL_UID (t)))
8550 {
8551 error_at (OMP_CLAUSE_LOCATION (c),
8552 "%qD appears more than once in %<nontemporal%> "
8553 "clauses", t);
8554 remove = true;
8555 }
8556 else
8557 bitmap_set_bit (&oacc_reduction_head, DECL_UID (t));
8558 break;
8559
8560 case OMP_CLAUSE_ALLOCATE:
8561 t = omp_clause_decl_field (OMP_CLAUSE_DECL (c));
8562 if (t)
8563 omp_note_field_privatization (t, OMP_CLAUSE_DECL (c));
8564 else
8565 t = OMP_CLAUSE_DECL (c);
8566 if (t == current_class_ptr)
8567 {
8568 error_at (OMP_CLAUSE_LOCATION (c),
8569 "%<this%> not allowed in %<allocate%> clause");
8570 remove = true;
8571 break;
8572 }
8573 if (!VAR_P (t)
8574 && TREE_CODE (t) != PARM_DECL
8575 && TREE_CODE (t) != FIELD_DECL)
8576 {
8577 if (processing_template_decl && TREE_CODE (t) != OVERLOAD)
8578 break;
8579 if (DECL_P (t))
8580 error_at (OMP_CLAUSE_LOCATION (c),
8581 "%qD is not a variable in %<allocate%> clause", t);
8582 else
8583 error_at (OMP_CLAUSE_LOCATION (c),
8584 "%qE is not a variable in %<allocate%> clause", t);
8585 remove = true;
8586 }
8587 else if (bitmap_bit_p (&aligned_head, DECL_UID (t)))
8588 {
8589 warning_at (OMP_CLAUSE_LOCATION (c), OPT_Wopenmp,
8590 "%qD appears more than once in %<allocate%> clauses",
8591 t);
8592 remove = true;
8593 }
8594 else
8595 {
8596 bitmap_set_bit (&aligned_head, DECL_UID (t));
8597 allocate_seen = true;
8598 }
8599 tree allocator, align;
8600 align = OMP_CLAUSE_ALLOCATE_ALIGN (c);
8601 if (error_operand_p (align))
8602 {
8603 remove = true;
8604 break;
8605 }
8606 if (align)
8607 {
8608 if (!type_dependent_expression_p (align)
8609 && !INTEGRAL_TYPE_P (TREE_TYPE (align)))
8610 {
8611 error_at (OMP_CLAUSE_LOCATION (c),
8612 "%<allocate%> clause %<align%> modifier "
8613 "argument needs to be positive constant "
8614 "power of two integer expression");
8615 remove = true;
8616 }
8617 else
8618 {
8619 align = mark_rvalue_use (align);
8620 if (!processing_template_decl)
8621 {
8622 align = maybe_constant_value (align);
8623 if (TREE_CODE (align) != INTEGER_CST
8624 || !tree_fits_uhwi_p (align)
8625 || !integer_pow2p (align))
8626 {
8627 error_at (OMP_CLAUSE_LOCATION (c),
8628 "%<allocate%> clause %<align%> modifier "
8629 "argument needs to be positive constant "
8630 "power of two integer expression");
8631 remove = true;
8632 }
8633 }
8634 }
8635 OMP_CLAUSE_ALLOCATE_ALIGN (c) = align;
8636 }
8637 allocator = OMP_CLAUSE_ALLOCATE_ALLOCATOR (c);
8638 if (error_operand_p (allocator))
8639 {
8640 remove = true;
8641 break;
8642 }
8643 if (allocator == NULL_TREE)
8644 goto handle_field_decl;
8645 tree allocatort;
8646 allocatort = TYPE_MAIN_VARIANT (TREE_TYPE (allocator));
8647 if (!type_dependent_expression_p (allocator)
8648 && (TREE_CODE (allocatort) != ENUMERAL_TYPE
8649 || TYPE_NAME (allocatort) == NULL_TREE
8650 || TREE_CODE (TYPE_NAME (allocatort)) != TYPE_DECL
8651 || (DECL_NAME (TYPE_NAME (allocatort))
8652 != get_identifier ("omp_allocator_handle_t"))
8653 || (TYPE_CONTEXT (allocatort)
8654 != DECL_CONTEXT (global_namespace))))
8655 {
8656 error_at (OMP_CLAUSE_LOCATION (c),
8657 "%<allocate%> clause allocator expression has "
8658 "type %qT rather than %<omp_allocator_handle_t%>",
8659 TREE_TYPE (allocator));
8660 remove = true;
8661 break;
8662 }
8663 else
8664 {
8665 allocator = mark_rvalue_use (allocator);
8666 if (!processing_template_decl)
8667 allocator = maybe_constant_value (allocator);
8668 OMP_CLAUSE_ALLOCATE_ALLOCATOR (c) = allocator;
8669 }
8670 goto handle_field_decl;
8671
8672 case OMP_CLAUSE_DOACROSS:
8673 t = OMP_CLAUSE_DECL (c);
8674 if (t == NULL_TREE)
8675 break;
8676 if (OMP_CLAUSE_DOACROSS_KIND (c) == OMP_CLAUSE_DOACROSS_SINK)
8677 {
8678 if (cp_finish_omp_clause_doacross_sink (c))
8679 remove = true;
8680 break;
8681 }
8682 gcc_unreachable ();
8683 case OMP_CLAUSE_DEPEND:
8684 depend_clause = c;
8685 /* FALLTHRU */
8686 case OMP_CLAUSE_AFFINITY:
8687 t = OMP_CLAUSE_DECL (c);
8688 if (TREE_CODE (t) == TREE_LIST
8689 && TREE_PURPOSE (t)
8690 && TREE_CODE (TREE_PURPOSE (t)) == TREE_VEC)
8691 {
8692 if (TREE_PURPOSE (t) != last_iterators)
8693 last_iterators_remove
8694 = cp_omp_finish_iterators (TREE_PURPOSE (t));
8695 last_iterators = TREE_PURPOSE (t);
8696 t = TREE_VALUE (t);
8697 if (last_iterators_remove)
8698 t = error_mark_node;
8699 }
8700 else
8701 last_iterators = NULL_TREE;
8702
8703 if (TREE_CODE (t) == OMP_ARRAY_SECTION)
8704 {
8705 if (handle_omp_array_sections (c, ort))
8706 remove = true;
8707 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
8708 && (OMP_CLAUSE_DEPEND_KIND (c)
8709 == OMP_CLAUSE_DEPEND_DEPOBJ))
8710 {
8711 error_at (OMP_CLAUSE_LOCATION (c),
8712 "%<depend%> clause with %<depobj%> dependence "
8713 "type on array section");
8714 remove = true;
8715 }
8716 break;
8717 }
8718 if (t == error_mark_node)
8719 remove = true;
8720 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
8721 && t == ridpointers[RID_OMP_ALL_MEMORY])
8722 {
8723 if (OMP_CLAUSE_DEPEND_KIND (c) != OMP_CLAUSE_DEPEND_OUT
8724 && OMP_CLAUSE_DEPEND_KIND (c) != OMP_CLAUSE_DEPEND_INOUT)
8725 {
8726 error_at (OMP_CLAUSE_LOCATION (c),
8727 "%<omp_all_memory%> used with %<depend%> kind "
8728 "other than %<out%> or %<inout%>");
8729 remove = true;
8730 }
8731 if (processing_template_decl)
8732 break;
8733 }
8734 else if (processing_template_decl && TREE_CODE (t) != OVERLOAD)
8735 break;
8736 else if (!lvalue_p (t))
8737 {
8738 if (DECL_P (t))
8739 error_at (OMP_CLAUSE_LOCATION (c),
8740 "%qD is not lvalue expression nor array section "
8741 "in %qs clause", t,
8742 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
8743 else
8744 error_at (OMP_CLAUSE_LOCATION (c),
8745 "%qE is not lvalue expression nor array section "
8746 "in %qs clause", t,
8747 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
8748 remove = true;
8749 }
8750 else if (TREE_CODE (t) == COMPONENT_REF
8751 && TREE_CODE (TREE_OPERAND (t, 1)) == FIELD_DECL
8752 && DECL_BIT_FIELD (TREE_OPERAND (t, 1)))
8753 {
8754 error_at (OMP_CLAUSE_LOCATION (c),
8755 "bit-field %qE in %qs clause", t,
8756 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
8757 remove = true;
8758 }
8759 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
8760 && OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_DEPOBJ)
8761 {
8762 if (!c_omp_depend_t_p (TYPE_REF_P (TREE_TYPE (t))
8763 ? TREE_TYPE (TREE_TYPE (t))
8764 : TREE_TYPE (t)))
8765 {
8766 error_at (OMP_CLAUSE_LOCATION (c),
8767 "%qE does not have %<omp_depend_t%> type in "
8768 "%<depend%> clause with %<depobj%> dependence "
8769 "type", t);
8770 remove = true;
8771 }
8772 }
8773 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
8774 && c_omp_depend_t_p (TYPE_REF_P (TREE_TYPE (t))
8775 ? TREE_TYPE (TREE_TYPE (t))
8776 : TREE_TYPE (t)))
8777 {
8778 error_at (OMP_CLAUSE_LOCATION (c),
8779 "%qE should not have %<omp_depend_t%> type in "
8780 "%<depend%> clause with dependence type other than "
8781 "%<depobj%>", t);
8782 remove = true;
8783 }
8784 if (!remove)
8785 {
8786 if (t == ridpointers[RID_OMP_ALL_MEMORY])
8787 t = null_pointer_node;
8788 else
8789 {
8790 tree addr = cp_build_addr_expr (t, tf_warning_or_error);
8791 if (addr == error_mark_node)
8792 {
8793 remove = true;
8794 break;
8795 }
8796 t = cp_build_indirect_ref (OMP_CLAUSE_LOCATION (c),
8797 addr, RO_UNARY_STAR,
8798 tf_warning_or_error);
8799 if (t == error_mark_node)
8800 {
8801 remove = true;
8802 break;
8803 }
8804 }
8805 if (TREE_CODE (OMP_CLAUSE_DECL (c)) == TREE_LIST
8806 && TREE_PURPOSE (OMP_CLAUSE_DECL (c))
8807 && (TREE_CODE (TREE_PURPOSE (OMP_CLAUSE_DECL (c)))
8808 == TREE_VEC))
8809 TREE_VALUE (OMP_CLAUSE_DECL (c)) = t;
8810 else
8811 OMP_CLAUSE_DECL (c) = t;
8812 }
8813 break;
8814 case OMP_CLAUSE_DETACH:
8815 t = OMP_CLAUSE_DECL (c);
8816 if (detach_seen)
8817 {
8818 error_at (OMP_CLAUSE_LOCATION (c),
8819 "too many %qs clauses on a task construct",
8820 "detach");
8821 remove = true;
8822 break;
8823 }
8824 else if (error_operand_p (t))
8825 {
8826 remove = true;
8827 break;
8828 }
8829 else
8830 {
8831 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (t));
8832 if (!type_dependent_expression_p (t)
8833 && (!INTEGRAL_TYPE_P (type)
8834 || TREE_CODE (type) != ENUMERAL_TYPE
8835 || TYPE_NAME (type) == NULL_TREE
8836 || (DECL_NAME (TYPE_NAME (type))
8837 != get_identifier ("omp_event_handle_t"))))
8838 {
8839 error_at (OMP_CLAUSE_LOCATION (c),
8840 "%<detach%> clause event handle "
8841 "has type %qT rather than "
8842 "%<omp_event_handle_t%>",
8843 type);
8844 remove = true;
8845 }
8846 detach_seen = c;
8847 cxx_mark_addressable (t);
8848 }
8849 break;
8850
8851 case OMP_CLAUSE_MAP:
8852 if (OMP_CLAUSE_MAP_IMPLICIT (c) && !implicit_moved)
8853 goto move_implicit;
8854 /* FALLTHRU */
8855 case OMP_CLAUSE_TO:
8856 case OMP_CLAUSE_FROM:
8857 case OMP_CLAUSE__CACHE_:
8858 {
8859 using namespace omp_addr_tokenizer;
8860 auto_vec<omp_addr_token *, 10> addr_tokens;
8861
8862 t = OMP_CLAUSE_DECL (c);
8863 if (TREE_CODE (t) == OMP_ARRAY_SECTION)
8864 {
8865 grp_start_p = pc;
8866 grp_sentinel = OMP_CLAUSE_CHAIN (c);
8867
8868 if (handle_omp_array_sections (c, ort))
8869 remove = true;
8870 else
8871 {
8872 t = OMP_CLAUSE_DECL (c);
8873 if (TREE_CODE (t) != OMP_ARRAY_SECTION
8874 && !type_dependent_expression_p (t)
8875 && !omp_mappable_type (TREE_TYPE (t)))
8876 {
8877 auto_diagnostic_group d;
8878 error_at (OMP_CLAUSE_LOCATION (c),
8879 "array section does not have mappable type "
8880 "in %qs clause",
8881 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
8882 if (TREE_TYPE (t) != error_mark_node
8883 && !COMPLETE_TYPE_P (TREE_TYPE (t)))
8884 cxx_incomplete_type_inform (TREE_TYPE (t));
8885 remove = true;
8886 }
8887 while (TREE_CODE (t) == ARRAY_REF)
8888 t = TREE_OPERAND (t, 0);
8889
8890 if (type_dependent_expression_p (t))
8891 break;
8892
8893 cp_omp_address_inspector ai (OMP_CLAUSE_LOCATION (c), t);
8894
8895 if (!ai.map_supported_p ()
8896 || !omp_parse_expr (addr_tokens, t))
8897 {
8898 sorry_at (OMP_CLAUSE_LOCATION (c),
8899 "unsupported map expression %qE",
8900 OMP_CLAUSE_DECL (c));
8901 remove = true;
8902 break;
8903 }
8904
8905 /* This check is to determine if this will be the only map
8906 node created for this clause. Otherwise, we'll check
8907 the following FIRSTPRIVATE_POINTER,
8908 FIRSTPRIVATE_REFERENCE or ATTACH_DETACH node on the next
8909 iteration(s) of the loop. */
8910 if (addr_tokens.length () >= 4
8911 && addr_tokens[0]->type == STRUCTURE_BASE
8912 && addr_tokens[0]->u.structure_base_kind == BASE_DECL
8913 && addr_tokens[1]->type == ACCESS_METHOD
8914 && addr_tokens[2]->type == COMPONENT_SELECTOR
8915 && addr_tokens[3]->type == ACCESS_METHOD
8916 && (addr_tokens[3]->u.access_kind == ACCESS_DIRECT
8917 || (addr_tokens[3]->u.access_kind
8918 == ACCESS_INDEXED_ARRAY)))
8919 {
8920 tree rt = addr_tokens[1]->expr;
8921
8922 gcc_assert (DECL_P (rt));
8923
8924 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
8925 && OMP_CLAUSE_MAP_IMPLICIT (c)
8926 && (bitmap_bit_p (&map_head, DECL_UID (rt))
8927 || bitmap_bit_p (&map_field_head, DECL_UID (rt))
8928 || bitmap_bit_p (&map_firstprivate_head,
8929 DECL_UID (rt))))
8930 {
8931 remove = true;
8932 break;
8933 }
8934 if (bitmap_bit_p (&map_field_head, DECL_UID (rt)))
8935 break;
8936 if (bitmap_bit_p (&map_head, DECL_UID (rt)))
8937 {
8938 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
8939 error_at (OMP_CLAUSE_LOCATION (c),
8940 "%qD appears more than once in motion"
8941 " clauses", rt);
8942 else if (openacc)
8943 error_at (OMP_CLAUSE_LOCATION (c),
8944 "%qD appears more than once in data"
8945 " clauses", rt);
8946 else
8947 error_at (OMP_CLAUSE_LOCATION (c),
8948 "%qD appears more than once in map"
8949 " clauses", rt);
8950 remove = true;
8951 }
8952 else
8953 {
8954 bitmap_set_bit (&map_head, DECL_UID (rt));
8955 bitmap_set_bit (&map_field_head, DECL_UID (rt));
8956 }
8957 }
8958 }
8959 if (cp_oacc_check_attachments (c))
8960 remove = true;
8961 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
8962 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH
8963 || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_DETACH)
8964 && !OMP_CLAUSE_SIZE (c))
8965 /* In this case, we have a single array element which is a
8966 pointer, and we already set OMP_CLAUSE_SIZE in
8967 handle_omp_array_sections above. For attach/detach
8968 clauses, reset the OMP_CLAUSE_SIZE (representing a bias)
8969 to zero here. */
8970 OMP_CLAUSE_SIZE (c) = size_zero_node;
8971 break;
8972 }
8973 else if (type_dependent_expression_p (t))
8974 break;
8975 else if (!omp_parse_expr (addr_tokens, t))
8976 {
8977 sorry_at (OMP_CLAUSE_LOCATION (c),
8978 "unsupported map expression %qE",
8979 OMP_CLAUSE_DECL (c));
8980 remove = true;
8981 break;
8982 }
8983 if (t == error_mark_node)
8984 {
8985 remove = true;
8986 break;
8987 }
8988 /* OpenACC attach / detach clauses must be pointers. */
8989 if (cp_oacc_check_attachments (c))
8990 {
8991 remove = true;
8992 break;
8993 }
8994 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
8995 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH
8996 || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_DETACH)
8997 && !OMP_CLAUSE_SIZE (c))
8998 /* For attach/detach clauses, set OMP_CLAUSE_SIZE (representing a
8999 bias) to zero here, so it is not set erroneously to the
9000 pointer size later on in gimplify.cc. */
9001 OMP_CLAUSE_SIZE (c) = size_zero_node;
9002
9003 cp_omp_address_inspector ai (OMP_CLAUSE_LOCATION (c), t);
9004
9005 if (!ai.check_clause (c))
9006 {
9007 remove = true;
9008 break;
9009 }
9010
9011 if (!ai.map_supported_p ())
9012 {
9013 sorry_at (OMP_CLAUSE_LOCATION (c),
9014 "unsupported map expression %qE",
9015 OMP_CLAUSE_DECL (c));
9016 remove = true;
9017 break;
9018 }
9019
9020 gcc_assert ((addr_tokens[0]->type == ARRAY_BASE
9021 || addr_tokens[0]->type == STRUCTURE_BASE)
9022 && addr_tokens[1]->type == ACCESS_METHOD);
9023
9024 t = addr_tokens[1]->expr;
9025
9026 /* This is used to prevent cxx_mark_addressable from being called
9027 on 'this' for expressions like 'this->a', i.e. typical member
9028 accesses. */
9029 indir_component_ref_p
9030 = (addr_tokens[0]->type == STRUCTURE_BASE
9031 && addr_tokens[1]->u.access_kind != ACCESS_DIRECT);
9032
9033 if (addr_tokens[0]->u.structure_base_kind != BASE_DECL)
9034 goto skip_decl_checks;
9035
9036 /* For OpenMP, we can access a struct "t" and "t.d" on the same
9037 mapping. OpenACC allows multiple fields of the same structure
9038 to be written. */
9039 if (addr_tokens[0]->type == STRUCTURE_BASE
9040 && (bitmap_bit_p (&map_field_head, DECL_UID (t))
9041 || (!openacc && bitmap_bit_p (&map_head, DECL_UID (t)))))
9042 goto skip_decl_checks;
9043
9044 if (!processing_template_decl && TREE_CODE (t) == FIELD_DECL)
9045 {
9046 OMP_CLAUSE_DECL (c)
9047 = finish_non_static_data_member (t, NULL_TREE, NULL_TREE);
9048 break;
9049 }
9050 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
9051 {
9052 if (processing_template_decl && TREE_CODE (t) != OVERLOAD)
9053 break;
9054 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
9055 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_POINTER
9056 || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ALWAYS_POINTER
9057 || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH_DETACH
9058 || (!openacc && EXPR_P (t))))
9059 break;
9060 if (DECL_P (t))
9061 error_at (OMP_CLAUSE_LOCATION (c),
9062 "%qD is not a variable in %qs clause", t,
9063 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
9064 else
9065 error_at (OMP_CLAUSE_LOCATION (c),
9066 "%qE is not a variable in %qs clause", t,
9067 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
9068 remove = true;
9069 }
9070 else if (VAR_P (t) && CP_DECL_THREAD_LOCAL_P (t))
9071 {
9072 error_at (OMP_CLAUSE_LOCATION (c),
9073 "%qD is threadprivate variable in %qs clause", t,
9074 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
9075 remove = true;
9076 }
9077 else if (!processing_template_decl
9078 && !TYPE_REF_P (TREE_TYPE (t))
9079 && (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP
9080 || (OMP_CLAUSE_MAP_KIND (c)
9081 != GOMP_MAP_FIRSTPRIVATE_POINTER))
9082 && !indir_component_ref_p
9083 && (t != current_class_ptr
9084 || OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP
9085 || OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_ATTACH_DETACH)
9086 && !cxx_mark_addressable (t))
9087 remove = true;
9088 else if (!(OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
9089 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_POINTER
9090 || (OMP_CLAUSE_MAP_KIND (c)
9091 == GOMP_MAP_FIRSTPRIVATE_POINTER)
9092 || (OMP_CLAUSE_MAP_KIND (c)
9093 == GOMP_MAP_ATTACH_DETACH)))
9094 && t == OMP_CLAUSE_DECL (c)
9095 && !type_dependent_expression_p (t)
9096 && !omp_mappable_type (TYPE_REF_P (TREE_TYPE (t))
9097 ? TREE_TYPE (TREE_TYPE (t))
9098 : TREE_TYPE (t)))
9099 {
9100 auto_diagnostic_group d;
9101 error_at (OMP_CLAUSE_LOCATION (c),
9102 "%qD does not have a mappable type in %qs clause", t,
9103 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
9104 if (TREE_TYPE (t) != error_mark_node
9105 && !COMPLETE_TYPE_P (TREE_TYPE (t)))
9106 cxx_incomplete_type_inform (TREE_TYPE (t));
9107 remove = true;
9108 }
9109 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
9110 && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FORCE_DEVICEPTR
9111 && !type_dependent_expression_p (t)
9112 && !INDIRECT_TYPE_P (TREE_TYPE (t)))
9113 {
9114 error_at (OMP_CLAUSE_LOCATION (c),
9115 "%qD is not a pointer variable", t);
9116 remove = true;
9117 }
9118 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
9119 && OMP_CLAUSE_MAP_IMPLICIT (c)
9120 && (bitmap_bit_p (&map_head, DECL_UID (t))
9121 || bitmap_bit_p (&map_field_head, DECL_UID (t))
9122 || bitmap_bit_p (&map_firstprivate_head,
9123 DECL_UID (t))))
9124 remove = true;
9125 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
9126 && (OMP_CLAUSE_MAP_KIND (c)
9127 == GOMP_MAP_FIRSTPRIVATE_POINTER))
9128 {
9129 if (bitmap_bit_p (&generic_head, DECL_UID (t))
9130 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
9131 || bitmap_bit_p (&map_firstprivate_head, DECL_UID (t)))
9132 {
9133 error_at (OMP_CLAUSE_LOCATION (c),
9134 "%qD appears more than once in data clauses", t);
9135 remove = true;
9136 }
9137 else if (bitmap_bit_p (&map_head, DECL_UID (t))
9138 && !bitmap_bit_p (&map_field_head, DECL_UID (t))
9139 && openacc)
9140 {
9141 error_at (OMP_CLAUSE_LOCATION (c),
9142 "%qD appears more than once in data clauses", t);
9143 remove = true;
9144 }
9145 else
9146 bitmap_set_bit (&map_firstprivate_head, DECL_UID (t));
9147 }
9148 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
9149 && (OMP_CLAUSE_MAP_KIND (c)
9150 == GOMP_MAP_FIRSTPRIVATE_REFERENCE))
9151 bitmap_set_bit (&map_firstprivate_head, DECL_UID (t));
9152 else if (bitmap_bit_p (&map_head, DECL_UID (t))
9153 && !bitmap_bit_p (&map_field_head, DECL_UID (t))
9154 && ort != C_ORT_OMP
9155 && ort != C_ORT_OMP_EXIT_DATA)
9156 {
9157 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
9158 error_at (OMP_CLAUSE_LOCATION (c),
9159 "%qD appears more than once in motion clauses", t);
9160 else if (openacc)
9161 error_at (OMP_CLAUSE_LOCATION (c),
9162 "%qD appears more than once in data clauses", t);
9163 else
9164 error_at (OMP_CLAUSE_LOCATION (c),
9165 "%qD appears more than once in map clauses", t);
9166 remove = true;
9167 }
9168 else if (openacc && bitmap_bit_p (&generic_head, DECL_UID (t)))
9169 {
9170 error_at (OMP_CLAUSE_LOCATION (c),
9171 "%qD appears more than once in data clauses", t);
9172 remove = true;
9173 }
9174 else if (bitmap_bit_p (&firstprivate_head, DECL_UID (t))
9175 || bitmap_bit_p (&is_on_device_head, DECL_UID (t)))
9176 {
9177 if (openacc)
9178 error_at (OMP_CLAUSE_LOCATION (c),
9179 "%qD appears more than once in data clauses", t);
9180 else
9181 error_at (OMP_CLAUSE_LOCATION (c),
9182 "%qD appears both in data and map clauses", t);
9183 remove = true;
9184 }
9185 else if (!omp_access_chain_p (addr_tokens, 1))
9186 {
9187 bitmap_set_bit (&map_head, DECL_UID (t));
9188
9189 tree decl = OMP_CLAUSE_DECL (c);
9190 if (t != decl
9191 && (TREE_CODE (decl) == COMPONENT_REF
9192 || (INDIRECT_REF_P (decl)
9193 && (TREE_CODE (TREE_OPERAND (decl, 0))
9194 == COMPONENT_REF)
9195 && TYPE_REF_P (TREE_TYPE (TREE_OPERAND (decl,
9196 0))))))
9197 bitmap_set_bit (&map_field_head, DECL_UID (t));
9198 }
9199
9200 skip_decl_checks:
9201 /* If we call ai.expand_map_clause in handle_omp_array_sections,
9202 the containing loop (here) iterates through the new nodes
9203 created by that expansion. Avoid expanding those again (just
9204 by checking the node type). */
9205 if (!remove
9206 && !processing_template_decl
9207 && ort != C_ORT_DECLARE_SIMD
9208 && (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP
9209 || (OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_FIRSTPRIVATE_POINTER
9210 && (OMP_CLAUSE_MAP_KIND (c)
9211 != GOMP_MAP_FIRSTPRIVATE_REFERENCE)
9212 && OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_ALWAYS_POINTER
9213 && OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_ATTACH_DETACH
9214 && OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_ATTACH
9215 && OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_DETACH)))
9216 {
9217 grp_start_p = pc;
9218 grp_sentinel = OMP_CLAUSE_CHAIN (c);
9219 tree nc = ai.expand_map_clause (c, OMP_CLAUSE_DECL (c),
9220 addr_tokens, ort);
9221 if (nc != error_mark_node)
9222 c = nc;
9223 }
9224 }
9225 break;
9226
9227 case OMP_CLAUSE_ENTER:
9228 case OMP_CLAUSE_LINK:
9229 t = OMP_CLAUSE_DECL (c);
9230 const char *cname;
9231 cname = omp_clause_code_name[OMP_CLAUSE_CODE (c)];
9232 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ENTER
9233 && OMP_CLAUSE_ENTER_TO (c))
9234 cname = "to";
9235 if (TREE_CODE (t) == FUNCTION_DECL
9236 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ENTER)
9237 ;
9238 else if (!VAR_P (t))
9239 {
9240 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ENTER)
9241 {
9242 if (TREE_CODE (t) == TEMPLATE_ID_EXPR)
9243 error_at (OMP_CLAUSE_LOCATION (c),
9244 "template %qE in clause %qs", t, cname);
9245 else if (really_overloaded_fn (t))
9246 error_at (OMP_CLAUSE_LOCATION (c),
9247 "overloaded function name %qE in clause %qs", t,
9248 cname);
9249 else
9250 error_at (OMP_CLAUSE_LOCATION (c),
9251 "%qE is neither a variable nor a function name "
9252 "in clause %qs", t, cname);
9253 }
9254 else
9255 error_at (OMP_CLAUSE_LOCATION (c),
9256 "%qE is not a variable in clause %qs", t, cname);
9257 remove = true;
9258 }
9259 else if (DECL_THREAD_LOCAL_P (t))
9260 {
9261 error_at (OMP_CLAUSE_LOCATION (c),
9262 "%qD is threadprivate variable in %qs clause", t,
9263 cname);
9264 remove = true;
9265 }
9266 else if (!omp_mappable_type (TREE_TYPE (t)))
9267 {
9268 auto_diagnostic_group d;
9269 error_at (OMP_CLAUSE_LOCATION (c),
9270 "%qD does not have a mappable type in %qs clause", t,
9271 cname);
9272 if (TREE_TYPE (t) != error_mark_node
9273 && !COMPLETE_TYPE_P (TREE_TYPE (t)))
9274 cxx_incomplete_type_inform (TREE_TYPE (t));
9275 remove = true;
9276 }
9277 if (remove)
9278 break;
9279 if (bitmap_bit_p (&generic_head, DECL_UID (t)))
9280 {
9281 error_at (OMP_CLAUSE_LOCATION (c),
9282 "%qE appears more than once on the same "
9283 "%<declare target%> directive", t);
9284 remove = true;
9285 }
9286 else
9287 bitmap_set_bit (&generic_head, DECL_UID (t));
9288 break;
9289
9290 case OMP_CLAUSE_UNIFORM:
9291 t = OMP_CLAUSE_DECL (c);
9292 if (TREE_CODE (t) != PARM_DECL)
9293 {
9294 if (processing_template_decl)
9295 break;
9296 if (DECL_P (t))
9297 error_at (OMP_CLAUSE_LOCATION (c),
9298 "%qD is not an argument in %<uniform%> clause", t);
9299 else
9300 error_at (OMP_CLAUSE_LOCATION (c),
9301 "%qE is not an argument in %<uniform%> clause", t);
9302 remove = true;
9303 break;
9304 }
9305 /* map_head bitmap is used as uniform_head if declare_simd. */
9306 bitmap_set_bit (&map_head, DECL_UID (t));
9307 goto check_dup_generic;
9308
9309 case OMP_CLAUSE_GRAINSIZE:
9310 t = OMP_CLAUSE_GRAINSIZE_EXPR (c);
9311 if (t == error_mark_node)
9312 remove = true;
9313 else if (!type_dependent_expression_p (t)
9314 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
9315 {
9316 error_at (OMP_CLAUSE_LOCATION (c),
9317 "%<grainsize%> expression must be integral");
9318 remove = true;
9319 }
9320 else
9321 {
9322 t = mark_rvalue_use (t);
9323 if (!processing_template_decl)
9324 {
9325 t = maybe_constant_value (t);
9326 if (TREE_CODE (t) == INTEGER_CST
9327 && tree_int_cst_sgn (t) != 1)
9328 {
9329 warning_at (OMP_CLAUSE_LOCATION (c), OPT_Wopenmp,
9330 "%<grainsize%> value must be positive");
9331 t = integer_one_node;
9332 }
9333 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
9334 }
9335 OMP_CLAUSE_GRAINSIZE_EXPR (c) = t;
9336 }
9337 break;
9338
9339 case OMP_CLAUSE_PRIORITY:
9340 t = OMP_CLAUSE_PRIORITY_EXPR (c);
9341 if (t == error_mark_node)
9342 remove = true;
9343 else if (!type_dependent_expression_p (t)
9344 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
9345 {
9346 error_at (OMP_CLAUSE_LOCATION (c),
9347 "%<priority%> expression must be integral");
9348 remove = true;
9349 }
9350 else
9351 {
9352 t = mark_rvalue_use (t);
9353 if (!processing_template_decl)
9354 {
9355 t = maybe_constant_value (t);
9356 if (TREE_CODE (t) == INTEGER_CST
9357 && tree_int_cst_sgn (t) == -1)
9358 {
9359 warning_at (OMP_CLAUSE_LOCATION (c), OPT_Wopenmp,
9360 "%<priority%> value must be non-negative");
9361 t = integer_one_node;
9362 }
9363 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
9364 }
9365 OMP_CLAUSE_PRIORITY_EXPR (c) = t;
9366 }
9367 break;
9368
9369 case OMP_CLAUSE_HINT:
9370 t = OMP_CLAUSE_HINT_EXPR (c);
9371 if (t == error_mark_node)
9372 remove = true;
9373 else if (!type_dependent_expression_p (t)
9374 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
9375 {
9376 error_at (OMP_CLAUSE_LOCATION (c),
9377 "%<hint%> expression must be integral");
9378 remove = true;
9379 }
9380 else
9381 {
9382 t = mark_rvalue_use (t);
9383 if (!processing_template_decl)
9384 {
9385 t = maybe_constant_value (t);
9386 if (TREE_CODE (t) != INTEGER_CST)
9387 {
9388 error_at (OMP_CLAUSE_LOCATION (c),
9389 "%<hint%> expression must be constant integer "
9390 "expression");
9391 remove = true;
9392 }
9393 }
9394 OMP_CLAUSE_HINT_EXPR (c) = t;
9395 }
9396 break;
9397
9398 case OMP_CLAUSE_FILTER:
9399 t = OMP_CLAUSE_FILTER_EXPR (c);
9400 if (t == error_mark_node)
9401 remove = true;
9402 else if (!type_dependent_expression_p (t)
9403 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
9404 {
9405 error_at (OMP_CLAUSE_LOCATION (c),
9406 "%<filter%> expression must be integral");
9407 remove = true;
9408 }
9409 else
9410 {
9411 t = mark_rvalue_use (t);
9412 if (!processing_template_decl)
9413 {
9414 t = maybe_constant_value (t);
9415 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
9416 }
9417 OMP_CLAUSE_FILTER_EXPR (c) = t;
9418 }
9419 break;
9420
9421 case OMP_CLAUSE_IS_DEVICE_PTR:
9422 case OMP_CLAUSE_USE_DEVICE_PTR:
9423 field_ok = (ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP;
9424 t = OMP_CLAUSE_DECL (c);
9425 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IS_DEVICE_PTR)
9426 bitmap_set_bit (&is_on_device_head, DECL_UID (t));
9427 if (!type_dependent_expression_p (t))
9428 {
9429 tree type = TREE_TYPE (t);
9430 if (!TYPE_PTR_P (type)
9431 && (!TYPE_REF_P (type) || !TYPE_PTR_P (TREE_TYPE (type))))
9432 {
9433 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_USE_DEVICE_PTR
9434 && ort == C_ORT_OMP)
9435 {
9436 error_at (OMP_CLAUSE_LOCATION (c),
9437 "%qs variable is neither a pointer "
9438 "nor reference to pointer",
9439 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
9440 remove = true;
9441 }
9442 else if (TREE_CODE (type) != ARRAY_TYPE
9443 && (!TYPE_REF_P (type)
9444 || TREE_CODE (TREE_TYPE (type)) != ARRAY_TYPE))
9445 {
9446 error_at (OMP_CLAUSE_LOCATION (c),
9447 "%qs variable is neither a pointer, nor an "
9448 "array nor reference to pointer or array",
9449 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
9450 remove = true;
9451 }
9452 }
9453 }
9454 goto check_dup_generic;
9455
9456 case OMP_CLAUSE_HAS_DEVICE_ADDR:
9457 t = OMP_CLAUSE_DECL (c);
9458 if (TREE_CODE (t) == OMP_ARRAY_SECTION)
9459 {
9460 if (handle_omp_array_sections (c, ort))
9461 remove = true;
9462 else
9463 {
9464 t = OMP_CLAUSE_DECL (c);
9465 while (TREE_CODE (t) == OMP_ARRAY_SECTION)
9466 t = TREE_OPERAND (t, 0);
9467 while (INDIRECT_REF_P (t)
9468 || TREE_CODE (t) == ARRAY_REF)
9469 t = TREE_OPERAND (t, 0);
9470 }
9471 }
9472 if (VAR_P (t) || TREE_CODE (t) == PARM_DECL)
9473 {
9474 bitmap_set_bit (&is_on_device_head, DECL_UID (t));
9475 if (!processing_template_decl
9476 && !cxx_mark_addressable (t))
9477 remove = true;
9478 }
9479 goto check_dup_generic_t;
9480
9481 case OMP_CLAUSE_USE_DEVICE_ADDR:
9482 field_ok = true;
9483 t = OMP_CLAUSE_DECL (c);
9484 if (!processing_template_decl
9485 && (VAR_P (t) || TREE_CODE (t) == PARM_DECL)
9486 && !TYPE_REF_P (TREE_TYPE (t))
9487 && !cxx_mark_addressable (t))
9488 remove = true;
9489 goto check_dup_generic;
9490
9491 case OMP_CLAUSE_NOWAIT:
9492 case OMP_CLAUSE_DEFAULT:
9493 case OMP_CLAUSE_UNTIED:
9494 case OMP_CLAUSE_COLLAPSE:
9495 case OMP_CLAUSE_PARALLEL:
9496 case OMP_CLAUSE_FOR:
9497 case OMP_CLAUSE_SECTIONS:
9498 case OMP_CLAUSE_TASKGROUP:
9499 case OMP_CLAUSE_PROC_BIND:
9500 case OMP_CLAUSE_DEVICE_TYPE:
9501 case OMP_CLAUSE_NOGROUP:
9502 case OMP_CLAUSE_THREADS:
9503 case OMP_CLAUSE_SIMD:
9504 case OMP_CLAUSE_DEFAULTMAP:
9505 case OMP_CLAUSE_BIND:
9506 case OMP_CLAUSE_AUTO:
9507 case OMP_CLAUSE_INDEPENDENT:
9508 case OMP_CLAUSE_SEQ:
9509 case OMP_CLAUSE_IF_PRESENT:
9510 case OMP_CLAUSE_FINALIZE:
9511 case OMP_CLAUSE_NOHOST:
9512 case OMP_CLAUSE_INDIRECT:
9513 break;
9514
9515 case OMP_CLAUSE_MERGEABLE:
9516 mergeable_seen = true;
9517 break;
9518
9519 case OMP_CLAUSE_TILE:
9520 for (tree list = OMP_CLAUSE_TILE_LIST (c); !remove && list;
9521 list = TREE_CHAIN (list))
9522 {
9523 t = TREE_VALUE (list);
9524
9525 if (t == error_mark_node)
9526 remove = true;
9527 else if (!type_dependent_expression_p (t)
9528 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
9529 {
9530 error_at (OMP_CLAUSE_LOCATION (c),
9531 "%<tile%> argument needs integral type");
9532 remove = true;
9533 }
9534 else
9535 {
9536 t = mark_rvalue_use (t);
9537 if (!processing_template_decl)
9538 {
9539 /* Zero is used to indicate '*', we permit you
9540 to get there via an ICE of value zero. */
9541 t = maybe_constant_value (t);
9542 if (!tree_fits_shwi_p (t)
9543 || tree_to_shwi (t) < 0)
9544 {
9545 error_at (OMP_CLAUSE_LOCATION (c),
9546 "%<tile%> argument needs positive "
9547 "integral constant");
9548 remove = true;
9549 }
9550 }
9551 }
9552
9553 /* Update list item. */
9554 TREE_VALUE (list) = t;
9555 }
9556 break;
9557
9558 case OMP_CLAUSE_SIZES:
9559 for (tree list = OMP_CLAUSE_SIZES_LIST (c);
9560 !remove && list; list = TREE_CHAIN (list))
9561 {
9562 t = TREE_VALUE (list);
9563
9564 if (t == error_mark_node)
9565 t = integer_one_node;
9566 else if (!type_dependent_expression_p (t)
9567 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
9568 {
9569 error_at (OMP_CLAUSE_LOCATION (c),
9570 "%<sizes%> argument needs positive integral "
9571 "constant");
9572 t = integer_one_node;
9573 }
9574 else
9575 {
9576 t = mark_rvalue_use (t);
9577 if (!processing_template_decl)
9578 {
9579 t = maybe_constant_value (t);
9580 HOST_WIDE_INT n;
9581 if (!tree_fits_shwi_p (t)
9582 || !INTEGRAL_TYPE_P (TREE_TYPE (t))
9583 || (n = tree_to_shwi (t)) <= 0
9584 || (int)n != n)
9585 {
9586 error_at (OMP_CLAUSE_LOCATION (c),
9587 "%<sizes%> argument needs positive "
9588 "integral constant");
9589 t = integer_one_node;
9590 }
9591 }
9592 }
9593
9594 /* Update list item. */
9595 TREE_VALUE (list) = t;
9596 }
9597 break;
9598
9599 case OMP_CLAUSE_ORDERED:
9600 ordered_seen = true;
9601 break;
9602
9603 case OMP_CLAUSE_ORDER:
9604 if (order_seen)
9605 remove = true;
9606 else
9607 order_seen = true;
9608 break;
9609
9610 case OMP_CLAUSE_INBRANCH:
9611 case OMP_CLAUSE_NOTINBRANCH:
9612 if (branch_seen)
9613 {
9614 error_at (OMP_CLAUSE_LOCATION (c),
9615 "%<inbranch%> clause is incompatible with "
9616 "%<notinbranch%>");
9617 remove = true;
9618 }
9619 branch_seen = true;
9620 break;
9621
9622 case OMP_CLAUSE_INCLUSIVE:
9623 case OMP_CLAUSE_EXCLUSIVE:
9624 t = omp_clause_decl_field (OMP_CLAUSE_DECL (c));
9625 if (!t)
9626 t = OMP_CLAUSE_DECL (c);
9627 if (t == current_class_ptr)
9628 {
9629 error_at (OMP_CLAUSE_LOCATION (c),
9630 "%<this%> allowed in OpenMP only in %<declare simd%>"
9631 " clauses");
9632 remove = true;
9633 break;
9634 }
9635 if (!VAR_P (t)
9636 && TREE_CODE (t) != PARM_DECL
9637 && TREE_CODE (t) != FIELD_DECL)
9638 {
9639 if (processing_template_decl && TREE_CODE (t) != OVERLOAD)
9640 break;
9641 if (DECL_P (t))
9642 error_at (OMP_CLAUSE_LOCATION (c),
9643 "%qD is not a variable in clause %qs", t,
9644 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
9645 else
9646 error_at (OMP_CLAUSE_LOCATION (c),
9647 "%qE is not a variable in clause %qs", t,
9648 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
9649 remove = true;
9650 }
9651 break;
9652
9653 case OMP_CLAUSE_FULL:
9654 break;
9655
9656 case OMP_CLAUSE_PARTIAL:
9657 partial_seen = true;
9658 t = OMP_CLAUSE_PARTIAL_EXPR (c);
9659 if (!t)
9660 break;
9661
9662 if (t == error_mark_node)
9663 t = NULL_TREE;
9664 else if (!type_dependent_expression_p (t)
9665 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
9666 {
9667 error_at (OMP_CLAUSE_LOCATION (c),
9668 "%<partial%> argument needs positive constant "
9669 "integer expression");
9670 t = NULL_TREE;
9671 }
9672 else
9673 {
9674 t = mark_rvalue_use (t);
9675 if (!processing_template_decl)
9676 {
9677 t = maybe_constant_value (t);
9678
9679 HOST_WIDE_INT n;
9680 if (!INTEGRAL_TYPE_P (TREE_TYPE (t))
9681 || !tree_fits_shwi_p (t)
9682 || (n = tree_to_shwi (t)) <= 0
9683 || (int)n != n)
9684 {
9685 error_at (OMP_CLAUSE_LOCATION (c),
9686 "%<partial%> argument needs positive "
9687 "constant integer expression");
9688 t = NULL_TREE;
9689 }
9690 }
9691 }
9692
9693 OMP_CLAUSE_PARTIAL_EXPR (c) = t;
9694 break;
9695 case OMP_CLAUSE_INIT:
9696 init_seen = true;
9697 OMP_CLAUSE_INIT_PREFER_TYPE (c)
9698 = cp_finish_omp_init_prefer_type (OMP_CLAUSE_INIT_PREFER_TYPE (c));
9699 if (!OMP_CLAUSE_INIT_TARGETSYNC (c))
9700 init_no_targetsync_clause = c;
9701 /* FALLTHRU */
9702 case OMP_CLAUSE_DESTROY:
9703 case OMP_CLAUSE_USE:
9704 init_use_destroy_seen = true;
9705 t = OMP_CLAUSE_DECL (c);
9706 if (bitmap_bit_p (&generic_head, DECL_UID (t)))
9707 {
9708 error_at (OMP_CLAUSE_LOCATION (c),
9709 "%qD appears more than once in action clauses", t);
9710 remove = true;
9711 break;
9712 }
9713 bitmap_set_bit (&generic_head, DECL_UID (t));
9714 /* FALLTHRU */
9715 case OMP_CLAUSE_INTEROP:
9716 if (!processing_template_decl)
9717 {
9718 if (/* (ort == C_ORT_OMP_INTEROP [uncomment for depobj init]
9719 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_INTEROP) && */
9720 !c_omp_interop_t_p (TREE_TYPE (OMP_CLAUSE_DECL (c))))
9721 {
9722 error_at (OMP_CLAUSE_LOCATION (c),
9723 "%qD must be of %<omp_interop_t%>",
9724 OMP_CLAUSE_DECL (c));
9725 remove = true;
9726 }
9727 else if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_INIT
9728 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DESTROY)
9729 && TREE_READONLY (OMP_CLAUSE_DECL (c)))
9730 {
9731 error_at (OMP_CLAUSE_LOCATION (c),
9732 "%qD shall not be const", OMP_CLAUSE_DECL (c));
9733 remove = true;
9734 }
9735 }
9736 pc = &OMP_CLAUSE_CHAIN (c);
9737 break;
9738 default:
9739 gcc_unreachable ();
9740 }
9741
9742 if (remove)
9743 {
9744 if (grp_start_p)
9745 {
9746 /* If we found a clause to remove, we want to remove the whole
9747 expanded group, otherwise gimplify
9748 (omp_resolve_clause_dependencies) can get confused. */
9749 *grp_start_p = grp_sentinel;
9750 pc = grp_start_p;
9751 grp_start_p = NULL;
9752 }
9753 else
9754 *pc = OMP_CLAUSE_CHAIN (c);
9755 }
9756 else
9757 pc = &OMP_CLAUSE_CHAIN (c);
9758 }
9759
9760 if (reduction_seen < 0 && (ordered_seen || schedule_seen))
9761 reduction_seen = -2;
9762
9763 for (pc = &clauses, c = clauses; c ; c = *pc)
9764 {
9765 enum omp_clause_code c_kind = OMP_CLAUSE_CODE (c);
9766 bool remove = false;
9767 bool need_complete_type = false;
9768 bool need_default_ctor = false;
9769 bool need_copy_ctor = false;
9770 bool need_copy_assignment = false;
9771 bool need_implicitly_determined = false;
9772 bool need_dtor = false;
9773 tree type, inner_type;
9774
9775 switch (c_kind)
9776 {
9777 case OMP_CLAUSE_SHARED:
9778 need_implicitly_determined = true;
9779 break;
9780 case OMP_CLAUSE_PRIVATE:
9781 need_complete_type = true;
9782 need_default_ctor = true;
9783 need_dtor = true;
9784 need_implicitly_determined = true;
9785 break;
9786 case OMP_CLAUSE_FIRSTPRIVATE:
9787 need_complete_type = true;
9788 need_copy_ctor = true;
9789 need_dtor = true;
9790 need_implicitly_determined = true;
9791 break;
9792 case OMP_CLAUSE_LASTPRIVATE:
9793 need_complete_type = true;
9794 need_copy_assignment = true;
9795 need_implicitly_determined = true;
9796 break;
9797 case OMP_CLAUSE_REDUCTION:
9798 if (reduction_seen == -2)
9799 OMP_CLAUSE_REDUCTION_INSCAN (c) = 0;
9800 if (OMP_CLAUSE_REDUCTION_INSCAN (c))
9801 need_copy_assignment = true;
9802 need_implicitly_determined = true;
9803 break;
9804 case OMP_CLAUSE_IN_REDUCTION:
9805 case OMP_CLAUSE_TASK_REDUCTION:
9806 case OMP_CLAUSE_INCLUSIVE:
9807 case OMP_CLAUSE_EXCLUSIVE:
9808 need_implicitly_determined = true;
9809 break;
9810 case OMP_CLAUSE_LINEAR:
9811 if (ort != C_ORT_OMP_DECLARE_SIMD)
9812 need_implicitly_determined = true;
9813 else if (OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (c)
9814 && !bitmap_bit_p (&map_head,
9815 DECL_UID (OMP_CLAUSE_LINEAR_STEP (c))))
9816 {
9817 error_at (OMP_CLAUSE_LOCATION (c),
9818 "%<linear%> clause step is a parameter %qD not "
9819 "specified in %<uniform%> clause",
9820 OMP_CLAUSE_LINEAR_STEP (c));
9821 *pc = OMP_CLAUSE_CHAIN (c);
9822 continue;
9823 }
9824 break;
9825 case OMP_CLAUSE_COPYPRIVATE:
9826 need_copy_assignment = true;
9827 break;
9828 case OMP_CLAUSE_COPYIN:
9829 need_copy_assignment = true;
9830 break;
9831 case OMP_CLAUSE_SIMDLEN:
9832 if (safelen
9833 && !processing_template_decl
9834 && tree_int_cst_lt (OMP_CLAUSE_SAFELEN_EXPR (safelen),
9835 OMP_CLAUSE_SIMDLEN_EXPR (c)))
9836 {
9837 error_at (OMP_CLAUSE_LOCATION (c),
9838 "%<simdlen%> clause value is bigger than "
9839 "%<safelen%> clause value");
9840 OMP_CLAUSE_SIMDLEN_EXPR (c)
9841 = OMP_CLAUSE_SAFELEN_EXPR (safelen);
9842 }
9843 pc = &OMP_CLAUSE_CHAIN (c);
9844 continue;
9845 case OMP_CLAUSE_SCHEDULE:
9846 if (ordered_seen
9847 && (OMP_CLAUSE_SCHEDULE_KIND (c)
9848 & OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
9849 {
9850 error_at (OMP_CLAUSE_LOCATION (c),
9851 "%<nonmonotonic%> schedule modifier specified "
9852 "together with %<ordered%> clause");
9853 OMP_CLAUSE_SCHEDULE_KIND (c)
9854 = (enum omp_clause_schedule_kind)
9855 (OMP_CLAUSE_SCHEDULE_KIND (c)
9856 & ~OMP_CLAUSE_SCHEDULE_NONMONOTONIC);
9857 }
9858 if (reduction_seen == -2)
9859 error_at (OMP_CLAUSE_LOCATION (c),
9860 "%qs clause specified together with %<inscan%> "
9861 "%<reduction%> clause", "schedule");
9862 pc = &OMP_CLAUSE_CHAIN (c);
9863 continue;
9864 case OMP_CLAUSE_NOGROUP:
9865 if (reduction_seen)
9866 {
9867 error_at (OMP_CLAUSE_LOCATION (c),
9868 "%<nogroup%> clause must not be used together with "
9869 "%<reduction%> clause");
9870 *pc = OMP_CLAUSE_CHAIN (c);
9871 continue;
9872 }
9873 pc = &OMP_CLAUSE_CHAIN (c);
9874 continue;
9875 case OMP_CLAUSE_GRAINSIZE:
9876 if (num_tasks_seen)
9877 {
9878 error_at (OMP_CLAUSE_LOCATION (c),
9879 "%<grainsize%> clause must not be used together with "
9880 "%<num_tasks%> clause");
9881 *pc = OMP_CLAUSE_CHAIN (c);
9882 continue;
9883 }
9884 pc = &OMP_CLAUSE_CHAIN (c);
9885 continue;
9886 case OMP_CLAUSE_ORDERED:
9887 if (reduction_seen == -2)
9888 error_at (OMP_CLAUSE_LOCATION (c),
9889 "%qs clause specified together with %<inscan%> "
9890 "%<reduction%> clause", "ordered");
9891 pc = &OMP_CLAUSE_CHAIN (c);
9892 continue;
9893 case OMP_CLAUSE_ORDER:
9894 if (ordered_seen)
9895 {
9896 error_at (OMP_CLAUSE_LOCATION (c),
9897 "%<order%> clause must not be used together "
9898 "with %<ordered%> clause");
9899 *pc = OMP_CLAUSE_CHAIN (c);
9900 continue;
9901 }
9902 pc = &OMP_CLAUSE_CHAIN (c);
9903 continue;
9904 case OMP_CLAUSE_DETACH:
9905 if (mergeable_seen)
9906 {
9907 error_at (OMP_CLAUSE_LOCATION (c),
9908 "%<detach%> clause must not be used together with "
9909 "%<mergeable%> clause");
9910 *pc = OMP_CLAUSE_CHAIN (c);
9911 continue;
9912 }
9913 pc = &OMP_CLAUSE_CHAIN (c);
9914 continue;
9915 case OMP_CLAUSE_MAP:
9916 if (target_in_reduction_seen && !processing_template_decl)
9917 {
9918 t = OMP_CLAUSE_DECL (c);
9919 while (handled_component_p (t)
9920 || INDIRECT_REF_P (t)
9921 || TREE_CODE (t) == ADDR_EXPR
9922 || TREE_CODE (t) == MEM_REF
9923 || TREE_CODE (t) == NON_LVALUE_EXPR)
9924 t = TREE_OPERAND (t, 0);
9925 if (DECL_P (t)
9926 && bitmap_bit_p (&oacc_reduction_head, DECL_UID (t)))
9927 OMP_CLAUSE_MAP_IN_REDUCTION (c) = 1;
9928 }
9929 pc = &OMP_CLAUSE_CHAIN (c);
9930 continue;
9931 case OMP_CLAUSE_FULL:
9932 if (partial_seen)
9933 {
9934 error_at (OMP_CLAUSE_LOCATION (c),
9935 "%<full%> clause must not be used together "
9936 "with %<partial%> clause");
9937 *pc = OMP_CLAUSE_CHAIN (c);
9938 continue;
9939 }
9940 pc = &OMP_CLAUSE_CHAIN (c);
9941 continue;
9942 case OMP_CLAUSE_NOWAIT:
9943 if (copyprivate_seen)
9944 {
9945 error_at (OMP_CLAUSE_LOCATION (c),
9946 "%<nowait%> clause must not be used together "
9947 "with %<copyprivate%> clause");
9948 *pc = OMP_CLAUSE_CHAIN (c);
9949 continue;
9950 }
9951 /* FALLTHRU */
9952 default:
9953 pc = &OMP_CLAUSE_CHAIN (c);
9954 continue;
9955 }
9956
9957 t = OMP_CLAUSE_DECL (c);
9958 switch (c_kind)
9959 {
9960 case OMP_CLAUSE_LASTPRIVATE:
9961 if (DECL_P (t)
9962 && !bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
9963 {
9964 need_default_ctor = true;
9965 need_dtor = true;
9966 }
9967 break;
9968
9969 case OMP_CLAUSE_REDUCTION:
9970 case OMP_CLAUSE_IN_REDUCTION:
9971 case OMP_CLAUSE_TASK_REDUCTION:
9972 if (allocate_seen)
9973 {
9974 if (TREE_CODE (t) == MEM_REF)
9975 {
9976 t = TREE_OPERAND (t, 0);
9977 if (TREE_CODE (t) == POINTER_PLUS_EXPR)
9978 t = TREE_OPERAND (t, 0);
9979 if (TREE_CODE (t) == ADDR_EXPR
9980 || INDIRECT_REF_P (t))
9981 t = TREE_OPERAND (t, 0);
9982 if (DECL_P (t))
9983 bitmap_clear_bit (&aligned_head, DECL_UID (t));
9984 }
9985 else if (TREE_CODE (t) == OMP_ARRAY_SECTION)
9986 {
9987 while (TREE_CODE (t) == OMP_ARRAY_SECTION)
9988 t = TREE_OPERAND (t, 0);
9989 if (DECL_P (t))
9990 bitmap_clear_bit (&aligned_head, DECL_UID (t));
9991 t = OMP_CLAUSE_DECL (c);
9992 }
9993 else if (DECL_P (t))
9994 bitmap_clear_bit (&aligned_head, DECL_UID (t));
9995 t = OMP_CLAUSE_DECL (c);
9996 }
9997 if (processing_template_decl
9998 && !VAR_P (t) && TREE_CODE (t) != PARM_DECL)
9999 break;
10000 if (finish_omp_reduction_clause (c, &need_default_ctor,
10001 &need_dtor))
10002 remove = true;
10003 else
10004 t = OMP_CLAUSE_DECL (c);
10005 break;
10006
10007 case OMP_CLAUSE_COPYIN:
10008 if (processing_template_decl
10009 && !VAR_P (t) && TREE_CODE (t) != PARM_DECL)
10010 break;
10011 if (!VAR_P (t) || !CP_DECL_THREAD_LOCAL_P (t))
10012 {
10013 error_at (OMP_CLAUSE_LOCATION (c),
10014 "%qE must be %<threadprivate%> for %<copyin%>", t);
10015 remove = true;
10016 }
10017 break;
10018
10019 default:
10020 break;
10021 }
10022
10023 if (processing_template_decl
10024 && !VAR_P (t) && TREE_CODE (t) != PARM_DECL)
10025 {
10026 pc = &OMP_CLAUSE_CHAIN (c);
10027 continue;
10028 }
10029
10030 if (need_complete_type || need_copy_assignment)
10031 {
10032 t = require_complete_type (t);
10033 if (t == error_mark_node)
10034 remove = true;
10035 else if (!processing_template_decl
10036 && TYPE_REF_P (TREE_TYPE (t))
10037 && !complete_type_or_else (TREE_TYPE (TREE_TYPE (t)), t))
10038 remove = true;
10039 }
10040 if (need_implicitly_determined)
10041 {
10042 const char *share_name = NULL;
10043
10044 if (allocate_seen
10045 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_SHARED
10046 && DECL_P (t))
10047 bitmap_clear_bit (&aligned_head, DECL_UID (t));
10048
10049 if (VAR_P (t) && CP_DECL_THREAD_LOCAL_P (t))
10050 share_name = "threadprivate";
10051 else switch (cxx_omp_predetermined_sharing_1 (t))
10052 {
10053 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
10054 break;
10055 case OMP_CLAUSE_DEFAULT_SHARED:
10056 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED
10057 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE)
10058 && c_omp_predefined_variable (t))
10059 /* The __func__ variable and similar function-local predefined
10060 variables may be listed in a shared or firstprivate
10061 clause. */
10062 break;
10063 if (VAR_P (t)
10064 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
10065 && TREE_STATIC (t)
10066 && cxx_omp_const_qual_no_mutable (t))
10067 {
10068 tree ctx = CP_DECL_CONTEXT (t);
10069 /* const qualified static data members without mutable
10070 member may be specified in firstprivate clause. */
10071 if (TYPE_P (ctx) && MAYBE_CLASS_TYPE_P (ctx))
10072 break;
10073 }
10074 share_name = "shared";
10075 break;
10076 case OMP_CLAUSE_DEFAULT_PRIVATE:
10077 share_name = "private";
10078 break;
10079 default:
10080 gcc_unreachable ();
10081 }
10082 if (share_name)
10083 {
10084 error_at (OMP_CLAUSE_LOCATION (c),
10085 "%qE is predetermined %qs for %qs",
10086 omp_clause_printable_decl (t), share_name,
10087 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
10088 remove = true;
10089 }
10090 else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_SHARED
10091 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_FIRSTPRIVATE
10092 && cxx_omp_const_qual_no_mutable (t))
10093 {
10094 error_at (OMP_CLAUSE_LOCATION (c),
10095 "%<const%> qualified %qE without %<mutable%> member "
10096 "may appear only in %<shared%> or %<firstprivate%> "
10097 "clauses", omp_clause_printable_decl (t));
10098 remove = true;
10099 }
10100 }
10101
10102 if (detach_seen
10103 && (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED
10104 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
10105 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
10106 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
10107 && OMP_CLAUSE_DECL (c) == OMP_CLAUSE_DECL (detach_seen))
10108 {
10109 error_at (OMP_CLAUSE_LOCATION (c),
10110 "the event handle of a %<detach%> clause "
10111 "should not be in a data-sharing clause");
10112 remove = true;
10113 }
10114
10115 /* We're interested in the base element, not arrays. */
10116 inner_type = type = TREE_TYPE (t);
10117 if ((need_complete_type
10118 || need_copy_assignment
10119 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
10120 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
10121 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
10122 && TYPE_REF_P (inner_type))
10123 inner_type = TREE_TYPE (inner_type);
10124 while (TREE_CODE (inner_type) == ARRAY_TYPE)
10125 inner_type = TREE_TYPE (inner_type);
10126
10127 /* Check for special function availability by building a call to one.
10128 Save the results, because later we won't be in the right context
10129 for making these queries. */
10130 if (CLASS_TYPE_P (inner_type)
10131 && COMPLETE_TYPE_P (inner_type)
10132 && (need_default_ctor || need_copy_ctor
10133 || need_copy_assignment || need_dtor)
10134 && !type_dependent_expression_p (t)
10135 && cxx_omp_create_clause_info (c, inner_type, need_default_ctor,
10136 need_copy_ctor, need_copy_assignment,
10137 need_dtor))
10138 remove = true;
10139
10140 if (!remove
10141 && c_kind == OMP_CLAUSE_SHARED
10142 && processing_template_decl)
10143 {
10144 t = omp_clause_decl_field (OMP_CLAUSE_DECL (c));
10145 if (t)
10146 OMP_CLAUSE_DECL (c) = t;
10147 }
10148
10149 if (remove)
10150 *pc = OMP_CLAUSE_CHAIN (c);
10151 else
10152 pc = &OMP_CLAUSE_CHAIN (c);
10153 }
10154
10155 if (allocate_seen)
10156 for (pc = &clauses, c = clauses; c ; c = *pc)
10157 {
10158 bool remove = false;
10159 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ALLOCATE
10160 && !OMP_CLAUSE_ALLOCATE_COMBINED (c)
10161 && DECL_P (OMP_CLAUSE_DECL (c))
10162 && bitmap_bit_p (&aligned_head, DECL_UID (OMP_CLAUSE_DECL (c))))
10163 {
10164 error_at (OMP_CLAUSE_LOCATION (c),
10165 "%qD specified in %<allocate%> clause but not in "
10166 "an explicit privatization clause", OMP_CLAUSE_DECL (c));
10167 remove = true;
10168 }
10169 if (remove)
10170 *pc = OMP_CLAUSE_CHAIN (c);
10171 else
10172 pc = &OMP_CLAUSE_CHAIN (c);
10173 }
10174
10175 if (ort == C_ORT_OMP_INTEROP
10176 && depend_clause
10177 && (!init_use_destroy_seen
10178 || (init_seen && init_no_targetsync_clause)))
10179 {
10180 error_at (OMP_CLAUSE_LOCATION (depend_clause),
10181 "%<depend%> clause requires action clauses with "
10182 "%<targetsync%> interop-type");
10183 if (init_no_targetsync_clause)
10184 inform (OMP_CLAUSE_LOCATION (init_no_targetsync_clause),
10185 "%<init%> clause lacks the %<targetsync%> modifier");
10186 }
10187
10188 bitmap_obstack_release (NULL);
10189 return clauses;
10190 }
10191
10192 /* Start processing OpenMP clauses that can include any
10193 privatization clauses for non-static data members. */
10194
10195 tree
10196 push_omp_privatization_clauses (bool ignore_next)
10197 {
10198 if (omp_private_member_ignore_next)
10199 {
10200 omp_private_member_ignore_next = ignore_next;
10201 return NULL_TREE;
10202 }
10203 omp_private_member_ignore_next = ignore_next;
10204 if (omp_private_member_map)
10205 omp_private_member_vec.safe_push (error_mark_node);
10206 return push_stmt_list ();
10207 }
10208
10209 /* Revert remapping of any non-static data members since
10210 the last push_omp_privatization_clauses () call. */
10211
10212 void
10213 pop_omp_privatization_clauses (tree stmt)
10214 {
10215 if (stmt == NULL_TREE)
10216 return;
10217 stmt = pop_stmt_list (stmt);
10218 if (omp_private_member_map)
10219 {
10220 while (!omp_private_member_vec.is_empty ())
10221 {
10222 tree t = omp_private_member_vec.pop ();
10223 if (t == error_mark_node)
10224 {
10225 add_stmt (stmt);
10226 return;
10227 }
10228 bool no_decl_expr = t == integer_zero_node;
10229 if (no_decl_expr)
10230 t = omp_private_member_vec.pop ();
10231 tree *v = omp_private_member_map->get (t);
10232 gcc_assert (v);
10233 if (!no_decl_expr)
10234 add_decl_expr (*v);
10235 omp_private_member_map->remove (t);
10236 }
10237 delete omp_private_member_map;
10238 omp_private_member_map = NULL;
10239 }
10240 add_stmt (stmt);
10241 }
10242
10243 /* Remember OpenMP privatization clauses mapping and clear it.
10244 Used for lambdas. */
10245
10246 void
10247 save_omp_privatization_clauses (vec<tree> &save)
10248 {
10249 save = vNULL;
10250 if (omp_private_member_ignore_next)
10251 save.safe_push (integer_one_node);
10252 omp_private_member_ignore_next = false;
10253 if (!omp_private_member_map)
10254 return;
10255
10256 while (!omp_private_member_vec.is_empty ())
10257 {
10258 tree t = omp_private_member_vec.pop ();
10259 if (t == error_mark_node)
10260 {
10261 save.safe_push (t);
10262 continue;
10263 }
10264 tree n = t;
10265 if (t == integer_zero_node)
10266 t = omp_private_member_vec.pop ();
10267 tree *v = omp_private_member_map->get (t);
10268 gcc_assert (v);
10269 save.safe_push (*v);
10270 save.safe_push (t);
10271 if (n != t)
10272 save.safe_push (n);
10273 }
10274 delete omp_private_member_map;
10275 omp_private_member_map = NULL;
10276 }
10277
10278 /* Restore OpenMP privatization clauses mapping saved by the
10279 above function. */
10280
10281 void
10282 restore_omp_privatization_clauses (vec<tree> &save)
10283 {
10284 gcc_assert (omp_private_member_vec.is_empty ());
10285 omp_private_member_ignore_next = false;
10286 if (save.is_empty ())
10287 return;
10288 if (save.length () == 1 && save[0] == integer_one_node)
10289 {
10290 omp_private_member_ignore_next = true;
10291 save.release ();
10292 return;
10293 }
10294
10295 omp_private_member_map = new hash_map <tree, tree>;
10296 while (!save.is_empty ())
10297 {
10298 tree t = save.pop ();
10299 tree n = t;
10300 if (t != error_mark_node)
10301 {
10302 if (t == integer_one_node)
10303 {
10304 omp_private_member_ignore_next = true;
10305 gcc_assert (save.is_empty ());
10306 break;
10307 }
10308 if (t == integer_zero_node)
10309 t = save.pop ();
10310 tree &v = omp_private_member_map->get_or_insert (t);
10311 v = save.pop ();
10312 }
10313 omp_private_member_vec.safe_push (t);
10314 if (n != t)
10315 omp_private_member_vec.safe_push (n);
10316 }
10317 save.release ();
10318 }
10319
10320 /* For all variables in the tree_list VARS, mark them as thread local. */
10321
10322 void
10323 finish_omp_threadprivate (tree vars)
10324 {
10325 tree t;
10326
10327 /* Mark every variable in VARS to be assigned thread local storage. */
10328 for (t = vars; t; t = TREE_CHAIN (t))
10329 {
10330 tree v = TREE_PURPOSE (t);
10331 location_t loc = EXPR_LOCATION (TREE_VALUE (t));
10332
10333 if (error_operand_p (v))
10334 ;
10335 else if (!VAR_P (v))
10336 error_at (loc, "%<threadprivate%> %qD is not file, namespace "
10337 "or block scope variable", v);
10338 /* If V had already been marked threadprivate, it doesn't matter
10339 whether it had been used prior to this point. */
10340 else if (TREE_USED (v)
10341 && (DECL_LANG_SPECIFIC (v) == NULL
10342 || !CP_DECL_THREADPRIVATE_P (v)))
10343 error_at (loc, "%qE declared %<threadprivate%> after first use", v);
10344 else if (! TREE_STATIC (v) && ! DECL_EXTERNAL (v))
10345 error_at (loc, "automatic variable %qE cannot be %<threadprivate%>", v);
10346 else if (! COMPLETE_TYPE_P (complete_type (TREE_TYPE (v))))
10347 error_at (loc, "%<threadprivate%> %qE has incomplete type", v);
10348 else if (TREE_STATIC (v) && TYPE_P (CP_DECL_CONTEXT (v))
10349 && CP_DECL_CONTEXT (v) != current_class_type)
10350 error_at (loc, "%<threadprivate%> %qE directive not "
10351 "in %qT definition", v, CP_DECL_CONTEXT (v));
10352 else
10353 {
10354 /* Allocate a LANG_SPECIFIC structure for V, if needed. */
10355 if (DECL_LANG_SPECIFIC (v) == NULL)
10356 retrofit_lang_decl (v);
10357
10358 if (! CP_DECL_THREAD_LOCAL_P (v))
10359 {
10360 CP_DECL_THREAD_LOCAL_P (v) = true;
10361 set_decl_tls_model (v, decl_default_tls_model (v));
10362 /* If rtl has been already set for this var, call
10363 make_decl_rtl once again, so that encode_section_info
10364 has a chance to look at the new decl flags. */
10365 if (DECL_RTL_SET_P (v))
10366 make_decl_rtl (v);
10367 }
10368 CP_DECL_THREADPRIVATE_P (v) = 1;
10369 }
10370 }
10371 }
10372
10373 /* Build an OpenMP structured block. */
10374
10375 tree
10376 begin_omp_structured_block (void)
10377 {
10378 return do_pushlevel (sk_omp);
10379 }
10380
10381 tree
10382 finish_omp_structured_block (tree block)
10383 {
10384 return do_poplevel (block);
10385 }
10386
10387 /* Similarly, except force the retention of the BLOCK. */
10388
10389 tree
10390 begin_omp_parallel (void)
10391 {
10392 keep_next_level (true);
10393 return begin_omp_structured_block ();
10394 }
10395
10396 /* Generate OACC_DATA, with CLAUSES and BLOCK as its compound
10397 statement. */
10398
10399 tree
10400 finish_oacc_data (tree clauses, tree block)
10401 {
10402 tree stmt;
10403
10404 block = finish_omp_structured_block (block);
10405
10406 stmt = make_node (OACC_DATA);
10407 TREE_TYPE (stmt) = void_type_node;
10408 OACC_DATA_CLAUSES (stmt) = clauses;
10409 OACC_DATA_BODY (stmt) = block;
10410
10411 return add_stmt (stmt);
10412 }
10413
10414 /* Generate OACC_HOST_DATA, with CLAUSES and BLOCK as its compound
10415 statement. */
10416
10417 tree
10418 finish_oacc_host_data (tree clauses, tree block)
10419 {
10420 tree stmt;
10421
10422 block = finish_omp_structured_block (block);
10423
10424 stmt = make_node (OACC_HOST_DATA);
10425 TREE_TYPE (stmt) = void_type_node;
10426 OACC_HOST_DATA_CLAUSES (stmt) = clauses;
10427 OACC_HOST_DATA_BODY (stmt) = block;
10428
10429 return add_stmt (stmt);
10430 }
10431
10432 /* Generate OMP construct CODE, with BODY and CLAUSES as its compound
10433 statement. */
10434
10435 tree
10436 finish_omp_construct (enum tree_code code, tree body, tree clauses)
10437 {
10438 body = finish_omp_structured_block (body);
10439
10440 tree stmt = make_node (code);
10441 TREE_TYPE (stmt) = void_type_node;
10442 OMP_BODY (stmt) = body;
10443 OMP_CLAUSES (stmt) = clauses;
10444
10445 return add_stmt (stmt);
10446 }
10447
10448 /* Used to walk OpenMP target directive body. */
10449
10450 struct omp_target_walk_data
10451 {
10452 /* Holds the 'this' expression found in current function. */
10453 tree current_object;
10454
10455 /* True if the 'this' expression was accessed in the target body. */
10456 bool this_expr_accessed;
10457
10458 /* For non-static functions, record which pointer-typed members were
10459 accessed, and the whole expression. */
10460 hash_map<tree, tree> ptr_members_accessed;
10461
10462 /* Record which lambda objects were accessed in target body. */
10463 hash_set<tree> lambda_objects_accessed;
10464
10465 /* For lambda functions, the __closure object expression of the current
10466 function, and the set of captured variables accessed in target body. */
10467 tree current_closure;
10468 hash_set<tree> closure_vars_accessed;
10469
10470 /* Local variables declared inside a BIND_EXPR, used to filter out such
10471 variables when recording lambda_objects_accessed. */
10472 hash_set<tree> local_decls;
10473 };
10474
10475 /* Helper function of finish_omp_target_clauses, called via
10476 cp_walk_tree_without_duplicates. Traverse body of OpenMP target
10477 directive *TP, and fill out omp_target_walk_data passed in *PTR. */
10478
10479 static tree
10480 finish_omp_target_clauses_r (tree *tp, int *walk_subtrees, void *ptr)
10481 {
10482 tree t = *tp;
10483 struct omp_target_walk_data *data = (struct omp_target_walk_data *) ptr;
10484 tree current_object = data->current_object;
10485 tree current_closure = data->current_closure;
10486
10487 /* References inside of these expression codes shouldn't incur any
10488 form of mapping, so return early. */
10489 if (TREE_CODE (t) == SIZEOF_EXPR
10490 || TREE_CODE (t) == ALIGNOF_EXPR)
10491 {
10492 *walk_subtrees = 0;
10493 return NULL_TREE;
10494 }
10495
10496 if (TREE_CODE (t) == OMP_CLAUSE)
10497 return NULL_TREE;
10498
10499 if (current_object)
10500 {
10501 tree this_expr = TREE_OPERAND (current_object, 0);
10502
10503 if (operand_equal_p (t, this_expr))
10504 {
10505 data->this_expr_accessed = true;
10506 *walk_subtrees = 0;
10507 return NULL_TREE;
10508 }
10509
10510 if (TREE_CODE (t) == COMPONENT_REF
10511 && POINTER_TYPE_P (TREE_TYPE (t))
10512 && operand_equal_p (TREE_OPERAND (t, 0), current_object)
10513 && TREE_CODE (TREE_OPERAND (t, 1)) == FIELD_DECL)
10514 {
10515 data->this_expr_accessed = true;
10516 tree fld = TREE_OPERAND (t, 1);
10517 if (data->ptr_members_accessed.get (fld) == NULL)
10518 {
10519 if (TREE_CODE (TREE_TYPE (t)) == REFERENCE_TYPE)
10520 t = convert_from_reference (t);
10521 data->ptr_members_accessed.put (fld, t);
10522 }
10523 *walk_subtrees = 0;
10524 return NULL_TREE;
10525 }
10526 }
10527
10528 /* When the current_function_decl is a lambda function, the closure object
10529 argument's type seems to not yet have fields layed out, so a recording
10530 of DECL_VALUE_EXPRs during the target body walk seems the only way to
10531 find them. */
10532 if (current_closure
10533 && (VAR_P (t)
10534 || TREE_CODE (t) == PARM_DECL
10535 || TREE_CODE (t) == RESULT_DECL)
10536 && DECL_HAS_VALUE_EXPR_P (t)
10537 && TREE_CODE (DECL_VALUE_EXPR (t)) == COMPONENT_REF
10538 && operand_equal_p (current_closure,
10539 TREE_OPERAND (DECL_VALUE_EXPR (t), 0)))
10540 {
10541 if (!data->closure_vars_accessed.contains (t))
10542 data->closure_vars_accessed.add (t);
10543 *walk_subtrees = 0;
10544 return NULL_TREE;
10545 }
10546
10547 if (TREE_CODE (t) == BIND_EXPR)
10548 {
10549 if (tree block = BIND_EXPR_BLOCK (t))
10550 for (tree var = BLOCK_VARS (block); var; var = DECL_CHAIN (var))
10551 if (!data->local_decls.contains (var))
10552 data->local_decls.add (var);
10553 return NULL_TREE;
10554 }
10555
10556 if (TREE_TYPE (t) && LAMBDA_TYPE_P (TREE_TYPE (t)))
10557 {
10558 tree lt = TREE_TYPE (t);
10559 gcc_assert (CLASS_TYPE_P (lt));
10560
10561 if (!data->lambda_objects_accessed.contains (t)
10562 /* Do not prepare to create target maps for locally declared
10563 lambdas or anonymous ones. */
10564 && !data->local_decls.contains (t)
10565 && TREE_CODE (t) != TARGET_EXPR)
10566 data->lambda_objects_accessed.add (t);
10567 *walk_subtrees = 0;
10568 return NULL_TREE;
10569 }
10570
10571 return NULL_TREE;
10572 }
10573
10574 /* Helper function for finish_omp_target, and also from tsubst_expr.
10575 Create additional clauses for mapping of non-static members, lambda objects,
10576 etc. */
10577
10578 void
10579 finish_omp_target_clauses (location_t loc, tree body, tree *clauses_ptr)
10580 {
10581 omp_target_walk_data data;
10582 data.this_expr_accessed = false;
10583 data.current_object = NULL_TREE;
10584
10585 if (DECL_NONSTATIC_MEMBER_P (current_function_decl) && current_class_ptr)
10586 if (tree ct = current_nonlambda_class_type ())
10587 {
10588 tree object = maybe_dummy_object (ct, NULL);
10589 object = maybe_resolve_dummy (object, true);
10590 data.current_object = object;
10591 }
10592
10593 if (DECL_LAMBDA_FUNCTION_P (current_function_decl))
10594 {
10595 tree closure = DECL_ARGUMENTS (current_function_decl);
10596 data.current_closure = build_indirect_ref (loc, closure, RO_UNARY_STAR);
10597 }
10598 else
10599 data.current_closure = NULL_TREE;
10600
10601 cp_walk_tree_without_duplicates (&body, finish_omp_target_clauses_r, &data);
10602
10603 auto_vec<tree, 16> new_clauses;
10604
10605 tree omp_target_this_expr = NULL_TREE;
10606 tree *explicit_this_deref_map = NULL;
10607 if (data.this_expr_accessed)
10608 {
10609 omp_target_this_expr = TREE_OPERAND (data.current_object, 0);
10610
10611 /* See if explicit user-specified map(this[:]) clause already exists.
10612 If not, we create an implicit map(tofrom:this[:1]) clause. */
10613 for (tree *cp = clauses_ptr; *cp; cp = &OMP_CLAUSE_CHAIN (*cp))
10614 if (OMP_CLAUSE_CODE (*cp) == OMP_CLAUSE_MAP
10615 && (TREE_CODE (OMP_CLAUSE_DECL (*cp)) == INDIRECT_REF
10616 || TREE_CODE (OMP_CLAUSE_DECL (*cp)) == MEM_REF)
10617 && operand_equal_p (TREE_OPERAND (OMP_CLAUSE_DECL (*cp), 0),
10618 omp_target_this_expr))
10619 {
10620 explicit_this_deref_map = cp;
10621 break;
10622 }
10623 }
10624
10625 if (DECL_LAMBDA_FUNCTION_P (current_function_decl)
10626 && (data.this_expr_accessed
10627 || !data.closure_vars_accessed.is_empty ()))
10628 {
10629 /* For lambda functions, we need to first create a copy of the
10630 __closure object. */
10631 tree closure = DECL_ARGUMENTS (current_function_decl);
10632 tree c = build_omp_clause (loc, OMP_CLAUSE_MAP);
10633 OMP_CLAUSE_SET_MAP_KIND (c, GOMP_MAP_TO);
10634 OMP_CLAUSE_DECL (c)
10635 = build_indirect_ref (loc, closure, RO_UNARY_STAR);
10636 OMP_CLAUSE_SIZE (c)
10637 = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (closure)));
10638 new_clauses.safe_push (c);
10639
10640 tree closure_obj = OMP_CLAUSE_DECL (c);
10641 tree closure_type = TREE_TYPE (closure_obj);
10642
10643 gcc_assert (LAMBDA_TYPE_P (closure_type)
10644 && CLASS_TYPE_P (closure_type));
10645
10646 tree c2 = build_omp_clause (loc, OMP_CLAUSE_MAP);
10647 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_FIRSTPRIVATE_POINTER);
10648 OMP_CLAUSE_DECL (c2) = closure;
10649 OMP_CLAUSE_SIZE (c2) = size_zero_node;
10650 new_clauses.safe_push (c2);
10651 }
10652
10653 if (data.this_expr_accessed)
10654 {
10655 /* If the this-expr was accessed, create a map(*this) clause. */
10656 enum gomp_map_kind kind = GOMP_MAP_TOFROM;
10657 if (explicit_this_deref_map)
10658 {
10659 tree this_map = *explicit_this_deref_map;
10660 tree nc = OMP_CLAUSE_CHAIN (this_map);
10661 gcc_assert (nc != NULL_TREE
10662 && OMP_CLAUSE_CODE (nc) == OMP_CLAUSE_MAP
10663 && (OMP_CLAUSE_MAP_KIND (nc)
10664 == GOMP_MAP_FIRSTPRIVATE_POINTER));
10665 kind = OMP_CLAUSE_MAP_KIND (this_map);
10666 /* Remove the original 'map(*this) map(firstprivate_ptr:this)'
10667 two-map sequence away from the chain. */
10668 *explicit_this_deref_map = OMP_CLAUSE_CHAIN (nc);
10669 }
10670 tree c = build_omp_clause (loc, OMP_CLAUSE_MAP);
10671 OMP_CLAUSE_SET_MAP_KIND (c, kind);
10672 OMP_CLAUSE_DECL (c)
10673 = build_indirect_ref (loc, omp_target_this_expr, RO_UNARY_STAR);
10674 OMP_CLAUSE_SIZE (c)
10675 = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (omp_target_this_expr)));
10676 new_clauses.safe_push (c);
10677
10678 /* If we're in a lambda function, the this-pointer will actually be
10679 '__closure->this', a mapped member of __closure, hence always_pointer.
10680 Otherwise it's a firstprivate pointer. */
10681 enum gomp_map_kind ptr_kind
10682 = (DECL_LAMBDA_FUNCTION_P (current_function_decl)
10683 ? GOMP_MAP_ALWAYS_POINTER
10684 : GOMP_MAP_FIRSTPRIVATE_POINTER);
10685 c = build_omp_clause (loc, OMP_CLAUSE_MAP);
10686 OMP_CLAUSE_SET_MAP_KIND (c, ptr_kind);
10687 OMP_CLAUSE_DECL (c) = omp_target_this_expr;
10688 OMP_CLAUSE_SIZE (c) = size_zero_node;
10689 new_clauses.safe_push (c);
10690 }
10691
10692 if (DECL_LAMBDA_FUNCTION_P (current_function_decl))
10693 {
10694 if (omp_target_this_expr)
10695 {
10696 STRIP_NOPS (omp_target_this_expr);
10697 gcc_assert (DECL_HAS_VALUE_EXPR_P (omp_target_this_expr));
10698 omp_target_this_expr = DECL_VALUE_EXPR (omp_target_this_expr);
10699 }
10700
10701 for (hash_set<tree>::iterator i = data.closure_vars_accessed.begin ();
10702 i != data.closure_vars_accessed.end (); ++i)
10703 {
10704 tree orig_decl = *i;
10705 tree closure_expr = DECL_VALUE_EXPR (orig_decl);
10706
10707 if (TREE_CODE (TREE_TYPE (orig_decl)) == POINTER_TYPE
10708 || TREE_CODE (TREE_TYPE (orig_decl)) == REFERENCE_TYPE)
10709 {
10710 /* this-pointer is processed above, outside this loop. */
10711 if (omp_target_this_expr
10712 && operand_equal_p (closure_expr, omp_target_this_expr))
10713 continue;
10714
10715 bool ptr_p = TREE_CODE (TREE_TYPE (orig_decl)) == POINTER_TYPE;
10716 enum gomp_map_kind kind, ptr_kind, nc_kind;
10717 tree size;
10718
10719 if (ptr_p)
10720 {
10721 /* For pointers, default mapped as zero-length array
10722 section. */
10723 kind = GOMP_MAP_ALLOC;
10724 nc_kind = GOMP_MAP_FIRSTPRIVATE_POINTER;
10725 ptr_kind = GOMP_MAP_ATTACH_ZERO_LENGTH_ARRAY_SECTION;
10726 size = size_zero_node;
10727 }
10728 else
10729 {
10730 /* For references, default mapped as appearing on map
10731 clause. */
10732 kind = GOMP_MAP_TOFROM;
10733 nc_kind = GOMP_MAP_FIRSTPRIVATE_REFERENCE;
10734 ptr_kind = GOMP_MAP_ALWAYS_POINTER;
10735 size = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (closure_expr)));
10736 }
10737
10738 for (tree *p = clauses_ptr; *p; p = &OMP_CLAUSE_CHAIN (*p))
10739 if (OMP_CLAUSE_CODE (*p) == OMP_CLAUSE_MAP
10740 && (TREE_CODE (OMP_CLAUSE_DECL (*p)) == INDIRECT_REF
10741 || TREE_CODE (OMP_CLAUSE_DECL (*p)) == MEM_REF)
10742 && operand_equal_p (TREE_OPERAND (OMP_CLAUSE_DECL (*p), 0),
10743 orig_decl))
10744 {
10745 /* If this was already specified by user as a map,
10746 save the user specified map kind, delete the
10747 "map(*ptr/ref), map(firstprivate ptr/ref)" sequence,
10748 and insert our own sequence:
10749 "map(*__closure->ptr/ref), map(<ptr_kind>:__closure->ref"
10750 */
10751 tree nc = OMP_CLAUSE_CHAIN (*p);
10752 gcc_assert (nc != NULL_TREE
10753 && OMP_CLAUSE_CODE (nc) == OMP_CLAUSE_MAP
10754 && OMP_CLAUSE_MAP_KIND (nc) == nc_kind);
10755 /* Update with user specified kind and size. */
10756 kind = OMP_CLAUSE_MAP_KIND (*p);
10757 size = OMP_CLAUSE_SIZE (*p);
10758 *p = OMP_CLAUSE_CHAIN (nc);
10759 break;
10760 }
10761
10762 tree c = build_omp_clause (loc, OMP_CLAUSE_MAP);
10763 OMP_CLAUSE_SET_MAP_KIND (c, kind);
10764 OMP_CLAUSE_DECL (c)
10765 = build_indirect_ref (loc, closure_expr, RO_UNARY_STAR);
10766 OMP_CLAUSE_SIZE (c) = size;
10767 if (ptr_p)
10768 OMP_CLAUSE_MAP_MAYBE_ZERO_LENGTH_ARRAY_SECTION (c) = 1;
10769 new_clauses.safe_push (c);
10770
10771 c = build_omp_clause (loc, OMP_CLAUSE_MAP);
10772 OMP_CLAUSE_SET_MAP_KIND (c, ptr_kind);
10773 OMP_CLAUSE_DECL (c) = closure_expr;
10774 OMP_CLAUSE_SIZE (c) = size_zero_node;
10775 new_clauses.safe_push (c);
10776 }
10777 }
10778 }
10779
10780 if (!data.ptr_members_accessed.is_empty ())
10781 for (hash_map<tree, tree>::iterator i = data.ptr_members_accessed.begin ();
10782 i != data.ptr_members_accessed.end (); ++i)
10783 {
10784 /* For each referenced member that is of pointer or reference-to-pointer
10785 type, create the equivalent of map(alloc:this->ptr[:0]). */
10786 tree field_decl = (*i).first;
10787 tree ptr_member = (*i).second;
10788
10789 for (tree c = *clauses_ptr; c; c = OMP_CLAUSE_CHAIN (c))
10790 {
10791 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
10792 continue;
10793 /* If map(this->ptr[:N]) already exists, avoid creating another
10794 such map. */
10795 tree decl = OMP_CLAUSE_DECL (c);
10796 if ((TREE_CODE (decl) == INDIRECT_REF
10797 || TREE_CODE (decl) == MEM_REF)
10798 && operand_equal_p (TREE_OPERAND (decl, 0), ptr_member))
10799 goto next_ptr_member;
10800 }
10801
10802 if (!cxx_mark_addressable (ptr_member))
10803 gcc_unreachable ();
10804
10805 if (TREE_CODE (TREE_TYPE (field_decl)) == REFERENCE_TYPE)
10806 {
10807 /* For reference to pointers, we need to map the referenced
10808 pointer first for things to be correct. */
10809 tree ptr_member_type = TREE_TYPE (ptr_member);
10810
10811 /* Map pointer target as zero-length array section. */
10812 tree c = build_omp_clause (loc, OMP_CLAUSE_MAP);
10813 OMP_CLAUSE_SET_MAP_KIND (c, GOMP_MAP_ALLOC);
10814 OMP_CLAUSE_DECL (c)
10815 = build1 (INDIRECT_REF, TREE_TYPE (ptr_member_type), ptr_member);
10816 OMP_CLAUSE_SIZE (c) = size_zero_node;
10817 OMP_CLAUSE_MAP_MAYBE_ZERO_LENGTH_ARRAY_SECTION (c) = 1;
10818
10819 /* Map pointer to zero-length array section. */
10820 tree c2 = build_omp_clause (loc, OMP_CLAUSE_MAP);
10821 OMP_CLAUSE_SET_MAP_KIND
10822 (c2, GOMP_MAP_POINTER_TO_ZERO_LENGTH_ARRAY_SECTION);
10823 OMP_CLAUSE_DECL (c2) = ptr_member;
10824 OMP_CLAUSE_SIZE (c2) = size_zero_node;
10825
10826 /* Attach reference-to-pointer field to pointer. */
10827 tree c3 = build_omp_clause (loc, OMP_CLAUSE_MAP);
10828 OMP_CLAUSE_SET_MAP_KIND (c3, GOMP_MAP_ATTACH);
10829 OMP_CLAUSE_DECL (c3) = TREE_OPERAND (ptr_member, 0);
10830 OMP_CLAUSE_SIZE (c3) = size_zero_node;
10831
10832 new_clauses.safe_push (c);
10833 new_clauses.safe_push (c2);
10834 new_clauses.safe_push (c3);
10835 }
10836 else if (TREE_CODE (TREE_TYPE (field_decl)) == POINTER_TYPE)
10837 {
10838 /* Map pointer target as zero-length array section. */
10839 tree c = build_omp_clause (loc, OMP_CLAUSE_MAP);
10840 OMP_CLAUSE_SET_MAP_KIND (c, GOMP_MAP_ALLOC);
10841 OMP_CLAUSE_DECL (c) = build_indirect_ref (loc, ptr_member,
10842 RO_UNARY_STAR);
10843 OMP_CLAUSE_SIZE (c) = size_zero_node;
10844 OMP_CLAUSE_MAP_MAYBE_ZERO_LENGTH_ARRAY_SECTION (c) = 1;
10845
10846 /* Attach zero-length array section to pointer. */
10847 tree c2 = build_omp_clause (loc, OMP_CLAUSE_MAP);
10848 OMP_CLAUSE_SET_MAP_KIND
10849 (c2, GOMP_MAP_ATTACH_ZERO_LENGTH_ARRAY_SECTION);
10850 OMP_CLAUSE_DECL (c2) = ptr_member;
10851 OMP_CLAUSE_SIZE (c2) = size_zero_node;
10852
10853 new_clauses.safe_push (c);
10854 new_clauses.safe_push (c2);
10855 }
10856 else
10857 gcc_unreachable ();
10858
10859 next_ptr_member:
10860 ;
10861 }
10862
10863 for (hash_set<tree>::iterator i = data.lambda_objects_accessed.begin ();
10864 i != data.lambda_objects_accessed.end (); ++i)
10865 {
10866 tree lobj = *i;
10867 if (TREE_CODE (lobj) == TARGET_EXPR)
10868 lobj = TARGET_EXPR_SLOT (lobj);
10869
10870 tree lt = TREE_TYPE (lobj);
10871 gcc_assert (LAMBDA_TYPE_P (lt) && CLASS_TYPE_P (lt));
10872
10873 tree lc = build_omp_clause (loc, OMP_CLAUSE_MAP);
10874 OMP_CLAUSE_SET_MAP_KIND (lc, GOMP_MAP_TO);
10875 OMP_CLAUSE_DECL (lc) = lobj;
10876 OMP_CLAUSE_SIZE (lc) = TYPE_SIZE_UNIT (lt);
10877 new_clauses.safe_push (lc);
10878
10879 for (tree fld = TYPE_FIELDS (lt); fld; fld = DECL_CHAIN (fld))
10880 {
10881 if (TREE_CODE (TREE_TYPE (fld)) == POINTER_TYPE)
10882 {
10883 tree exp = build3 (COMPONENT_REF, TREE_TYPE (fld),
10884 lobj, fld, NULL_TREE);
10885 tree c = build_omp_clause (loc, OMP_CLAUSE_MAP);
10886 OMP_CLAUSE_SET_MAP_KIND (c, GOMP_MAP_ALLOC);
10887 OMP_CLAUSE_DECL (c)
10888 = build_indirect_ref (loc, exp, RO_UNARY_STAR);
10889 OMP_CLAUSE_SIZE (c) = size_zero_node;
10890 OMP_CLAUSE_MAP_MAYBE_ZERO_LENGTH_ARRAY_SECTION (c) = 1;
10891 new_clauses.safe_push (c);
10892
10893 c = build_omp_clause (loc, OMP_CLAUSE_MAP);
10894 OMP_CLAUSE_SET_MAP_KIND
10895 (c, GOMP_MAP_ATTACH_ZERO_LENGTH_ARRAY_SECTION);
10896 OMP_CLAUSE_DECL (c) = exp;
10897 OMP_CLAUSE_SIZE (c) = size_zero_node;
10898 new_clauses.safe_push (c);
10899 }
10900 else if (TREE_CODE (TREE_TYPE (fld)) == REFERENCE_TYPE)
10901 {
10902 tree exp = build3 (COMPONENT_REF, TREE_TYPE (fld),
10903 lobj, fld, NULL_TREE);
10904 tree c = build_omp_clause (loc, OMP_CLAUSE_MAP);
10905 OMP_CLAUSE_SET_MAP_KIND (c, GOMP_MAP_TOFROM);
10906 OMP_CLAUSE_DECL (c)
10907 = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (exp)), exp);
10908 OMP_CLAUSE_SIZE (c)
10909 = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (exp)));
10910 new_clauses.safe_push (c);
10911
10912 c = build_omp_clause (loc, OMP_CLAUSE_MAP);
10913 OMP_CLAUSE_SET_MAP_KIND (c, GOMP_MAP_ALWAYS_POINTER);
10914 OMP_CLAUSE_DECL (c) = exp;
10915 OMP_CLAUSE_SIZE (c) = size_zero_node;
10916 new_clauses.safe_push (c);
10917 }
10918 }
10919 }
10920
10921 tree c = *clauses_ptr;
10922 for (int i = new_clauses.length () - 1; i >= 0; i--)
10923 {
10924 OMP_CLAUSE_CHAIN (new_clauses[i]) = c;
10925 c = new_clauses[i];
10926 }
10927 *clauses_ptr = c;
10928 }
10929
10930 /* Called from cp_parser_omp_target. Create additional implicit clauses for
10931 OpenMP target directives, and do sanity checks. */
10932
10933 tree
10934 finish_omp_target (location_t loc, tree clauses, tree body, bool combined_p)
10935 {
10936 if (!processing_template_decl)
10937 finish_omp_target_clauses (loc, body, &clauses);
10938
10939 tree stmt = make_node (OMP_TARGET);
10940 TREE_TYPE (stmt) = void_type_node;
10941 OMP_TARGET_CLAUSES (stmt) = clauses;
10942 OMP_TARGET_BODY (stmt) = body;
10943 OMP_TARGET_COMBINED (stmt) = combined_p;
10944 SET_EXPR_LOCATION (stmt, loc);
10945
10946 tree c = clauses;
10947 while (c)
10948 {
10949 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP)
10950 switch (OMP_CLAUSE_MAP_KIND (c))
10951 {
10952 case GOMP_MAP_TO:
10953 case GOMP_MAP_ALWAYS_TO:
10954 case GOMP_MAP_PRESENT_TO:
10955 case GOMP_MAP_ALWAYS_PRESENT_TO:
10956 case GOMP_MAP_FROM:
10957 case GOMP_MAP_ALWAYS_FROM:
10958 case GOMP_MAP_PRESENT_FROM:
10959 case GOMP_MAP_ALWAYS_PRESENT_FROM:
10960 case GOMP_MAP_TOFROM:
10961 case GOMP_MAP_ALWAYS_TOFROM:
10962 case GOMP_MAP_PRESENT_TOFROM:
10963 case GOMP_MAP_ALWAYS_PRESENT_TOFROM:
10964 case GOMP_MAP_ALLOC:
10965 case GOMP_MAP_PRESENT_ALLOC:
10966 case GOMP_MAP_FIRSTPRIVATE_POINTER:
10967 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
10968 case GOMP_MAP_ALWAYS_POINTER:
10969 case GOMP_MAP_ATTACH_DETACH:
10970 case GOMP_MAP_ATTACH:
10971 case GOMP_MAP_ATTACH_ZERO_LENGTH_ARRAY_SECTION:
10972 case GOMP_MAP_POINTER:
10973 case GOMP_MAP_POINTER_TO_ZERO_LENGTH_ARRAY_SECTION:
10974 break;
10975 default:
10976 error_at (OMP_CLAUSE_LOCATION (c),
10977 "%<#pragma omp target%> with map-type other "
10978 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
10979 "on %<map%> clause");
10980 break;
10981 }
10982 c = OMP_CLAUSE_CHAIN (c);
10983 }
10984 return add_stmt (stmt);
10985 }
10986
10987 tree
10988 finish_omp_parallel (tree clauses, tree body)
10989 {
10990 tree stmt;
10991
10992 body = finish_omp_structured_block (body);
10993
10994 stmt = make_node (OMP_PARALLEL);
10995 TREE_TYPE (stmt) = void_type_node;
10996 OMP_PARALLEL_CLAUSES (stmt) = clauses;
10997 OMP_PARALLEL_BODY (stmt) = body;
10998
10999 return add_stmt (stmt);
11000 }
11001
11002 tree
11003 begin_omp_task (void)
11004 {
11005 keep_next_level (true);
11006 return begin_omp_structured_block ();
11007 }
11008
11009 tree
11010 finish_omp_task (tree clauses, tree body)
11011 {
11012 tree stmt;
11013
11014 body = finish_omp_structured_block (body);
11015
11016 stmt = make_node (OMP_TASK);
11017 TREE_TYPE (stmt) = void_type_node;
11018 OMP_TASK_CLAUSES (stmt) = clauses;
11019 OMP_TASK_BODY (stmt) = body;
11020
11021 return add_stmt (stmt);
11022 }
11023
11024 /* Helper function for finish_omp_for. Convert Ith random access iterator
11025 into integral iterator. Return FALSE if successful. */
11026
11027 static bool
11028 handle_omp_for_class_iterator (int i, location_t locus, enum tree_code code,
11029 tree declv, tree orig_declv, tree initv,
11030 tree condv, tree incrv, tree *body,
11031 tree *pre_body, tree &clauses,
11032 int collapse, int ordered)
11033 {
11034 tree diff, iter_init, iter_incr = NULL, last;
11035 tree incr_var = NULL, orig_pre_body, orig_body, c;
11036 tree decl = TREE_VEC_ELT (declv, i);
11037 tree init = TREE_VEC_ELT (initv, i);
11038 tree cond = TREE_VEC_ELT (condv, i);
11039 tree incr = TREE_VEC_ELT (incrv, i);
11040 tree iter = decl;
11041 location_t elocus = locus;
11042
11043 if (init && EXPR_HAS_LOCATION (init))
11044 elocus = EXPR_LOCATION (init);
11045
11046 switch (TREE_CODE (cond))
11047 {
11048 case GT_EXPR:
11049 case GE_EXPR:
11050 case LT_EXPR:
11051 case LE_EXPR:
11052 case NE_EXPR:
11053 if (TREE_OPERAND (cond, 1) == iter)
11054 cond = build2 (swap_tree_comparison (TREE_CODE (cond)),
11055 TREE_TYPE (cond), iter, TREE_OPERAND (cond, 0));
11056 if (TREE_OPERAND (cond, 0) != iter)
11057 cond = error_mark_node;
11058 else
11059 {
11060 tree tem = build_x_binary_op (EXPR_LOCATION (cond),
11061 TREE_CODE (cond),
11062 iter, ERROR_MARK,
11063 TREE_OPERAND (cond, 1), ERROR_MARK,
11064 NULL_TREE, NULL, tf_warning_or_error);
11065 if (error_operand_p (tem))
11066 return true;
11067 }
11068 break;
11069 default:
11070 cond = error_mark_node;
11071 break;
11072 }
11073 if (cond == error_mark_node)
11074 {
11075 error_at (elocus, "invalid controlling predicate");
11076 return true;
11077 }
11078 diff = build_x_binary_op (elocus, MINUS_EXPR,
11079 TREE_OPERAND (cond, 1), ERROR_MARK,
11080 iter, ERROR_MARK,
11081 NULL_TREE, NULL, tf_warning_or_error);
11082 diff = cp_fully_fold (diff);
11083 if (error_operand_p (diff))
11084 return true;
11085 if (TREE_CODE (TREE_TYPE (diff)) != INTEGER_TYPE)
11086 {
11087 error_at (elocus, "difference between %qE and %qD does not have integer type",
11088 TREE_OPERAND (cond, 1), iter);
11089 return true;
11090 }
11091 if (!c_omp_check_loop_iv_exprs (locus, code, orig_declv, i,
11092 TREE_VEC_ELT (declv, i), NULL_TREE,
11093 cond, cp_walk_subtrees))
11094 return true;
11095
11096 switch (TREE_CODE (incr))
11097 {
11098 case PREINCREMENT_EXPR:
11099 case PREDECREMENT_EXPR:
11100 case POSTINCREMENT_EXPR:
11101 case POSTDECREMENT_EXPR:
11102 if (TREE_OPERAND (incr, 0) != iter)
11103 {
11104 incr = error_mark_node;
11105 break;
11106 }
11107 iter_incr = build_x_unary_op (EXPR_LOCATION (incr),
11108 TREE_CODE (incr), iter,
11109 NULL_TREE, tf_warning_or_error);
11110 if (error_operand_p (iter_incr))
11111 return true;
11112 else if (TREE_CODE (incr) == PREINCREMENT_EXPR
11113 || TREE_CODE (incr) == POSTINCREMENT_EXPR)
11114 incr = integer_one_node;
11115 else
11116 incr = integer_minus_one_node;
11117 break;
11118 case MODIFY_EXPR:
11119 if (TREE_OPERAND (incr, 0) != iter)
11120 incr = error_mark_node;
11121 else if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
11122 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
11123 {
11124 tree rhs = TREE_OPERAND (incr, 1);
11125 if (TREE_OPERAND (rhs, 0) == iter)
11126 {
11127 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (rhs, 1)))
11128 != INTEGER_TYPE)
11129 incr = error_mark_node;
11130 else
11131 {
11132 iter_incr = build_x_modify_expr (EXPR_LOCATION (rhs),
11133 iter, TREE_CODE (rhs),
11134 TREE_OPERAND (rhs, 1),
11135 NULL_TREE,
11136 tf_warning_or_error);
11137 if (error_operand_p (iter_incr))
11138 return true;
11139 incr = TREE_OPERAND (rhs, 1);
11140 incr = cp_convert (TREE_TYPE (diff), incr,
11141 tf_warning_or_error);
11142 if (TREE_CODE (rhs) == MINUS_EXPR)
11143 {
11144 incr = build1 (NEGATE_EXPR, TREE_TYPE (diff), incr);
11145 incr = fold_simple (incr);
11146 }
11147 if (TREE_CODE (incr) != INTEGER_CST
11148 && (TREE_CODE (incr) != NOP_EXPR
11149 || (TREE_CODE (TREE_OPERAND (incr, 0))
11150 != INTEGER_CST)))
11151 iter_incr = NULL;
11152 }
11153 }
11154 else if (TREE_OPERAND (rhs, 1) == iter)
11155 {
11156 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (rhs, 0))) != INTEGER_TYPE
11157 || TREE_CODE (rhs) != PLUS_EXPR)
11158 incr = error_mark_node;
11159 else
11160 {
11161 iter_incr = build_x_binary_op (EXPR_LOCATION (rhs),
11162 PLUS_EXPR,
11163 TREE_OPERAND (rhs, 0),
11164 ERROR_MARK, iter,
11165 ERROR_MARK, NULL_TREE, NULL,
11166 tf_warning_or_error);
11167 if (error_operand_p (iter_incr))
11168 return true;
11169 iter_incr = build_x_modify_expr (EXPR_LOCATION (rhs),
11170 iter, NOP_EXPR,
11171 iter_incr, NULL_TREE,
11172 tf_warning_or_error);
11173 if (error_operand_p (iter_incr))
11174 return true;
11175 incr = TREE_OPERAND (rhs, 0);
11176 iter_incr = NULL;
11177 }
11178 }
11179 else
11180 incr = error_mark_node;
11181 }
11182 else
11183 incr = error_mark_node;
11184 break;
11185 default:
11186 incr = error_mark_node;
11187 break;
11188 }
11189
11190 if (incr == error_mark_node)
11191 {
11192 error_at (elocus, "invalid increment expression");
11193 return true;
11194 }
11195
11196 incr = cp_convert (TREE_TYPE (diff), incr, tf_warning_or_error);
11197 incr = cp_fully_fold (incr);
11198 tree loop_iv_seen = NULL_TREE;
11199 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
11200 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
11201 && OMP_CLAUSE_DECL (c) == iter)
11202 {
11203 if (code == OMP_TASKLOOP || code == OMP_LOOP)
11204 {
11205 loop_iv_seen = c;
11206 OMP_CLAUSE_LASTPRIVATE_LOOP_IV (c) = 1;
11207 }
11208 break;
11209 }
11210 else if ((code == OMP_TASKLOOP || code == OMP_LOOP)
11211 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
11212 && OMP_CLAUSE_DECL (c) == iter)
11213 {
11214 loop_iv_seen = c;
11215 if (code == OMP_TASKLOOP)
11216 OMP_CLAUSE_PRIVATE_TASKLOOP_IV (c) = 1;
11217 }
11218
11219 decl = create_temporary_var (TREE_TYPE (diff));
11220 pushdecl (decl);
11221 add_decl_expr (decl);
11222 last = create_temporary_var (TREE_TYPE (diff));
11223 pushdecl (last);
11224 add_decl_expr (last);
11225 if (c && iter_incr == NULL && TREE_CODE (incr) != INTEGER_CST
11226 && (!ordered || (i < collapse && collapse > 1)))
11227 {
11228 incr_var = create_temporary_var (TREE_TYPE (diff));
11229 pushdecl (incr_var);
11230 add_decl_expr (incr_var);
11231 }
11232 gcc_assert (stmts_are_full_exprs_p ());
11233 tree diffvar = NULL_TREE;
11234 if (code == OMP_TASKLOOP)
11235 {
11236 if (!loop_iv_seen)
11237 {
11238 tree ivc = build_omp_clause (locus, OMP_CLAUSE_FIRSTPRIVATE);
11239 OMP_CLAUSE_DECL (ivc) = iter;
11240 cxx_omp_finish_clause (ivc, NULL, false);
11241 OMP_CLAUSE_CHAIN (ivc) = clauses;
11242 clauses = ivc;
11243 }
11244 tree lvc = build_omp_clause (locus, OMP_CLAUSE_FIRSTPRIVATE);
11245 OMP_CLAUSE_DECL (lvc) = last;
11246 OMP_CLAUSE_CHAIN (lvc) = clauses;
11247 clauses = lvc;
11248 diffvar = create_temporary_var (TREE_TYPE (diff));
11249 pushdecl (diffvar);
11250 add_decl_expr (diffvar);
11251 }
11252 else if (code == OMP_LOOP)
11253 {
11254 if (!loop_iv_seen)
11255 {
11256 /* While iterators on the loop construct are predetermined
11257 lastprivate, if the decl is not declared inside of the
11258 loop, OMP_CLAUSE_LASTPRIVATE should have been added
11259 already. */
11260 loop_iv_seen = build_omp_clause (locus, OMP_CLAUSE_FIRSTPRIVATE);
11261 OMP_CLAUSE_DECL (loop_iv_seen) = iter;
11262 OMP_CLAUSE_CHAIN (loop_iv_seen) = clauses;
11263 clauses = loop_iv_seen;
11264 }
11265 else if (OMP_CLAUSE_CODE (loop_iv_seen) == OMP_CLAUSE_PRIVATE)
11266 {
11267 OMP_CLAUSE_PRIVATE_DEBUG (loop_iv_seen) = 0;
11268 OMP_CLAUSE_PRIVATE_OUTER_REF (loop_iv_seen) = 0;
11269 OMP_CLAUSE_CODE (loop_iv_seen) = OMP_CLAUSE_FIRSTPRIVATE;
11270 }
11271 if (OMP_CLAUSE_CODE (loop_iv_seen) == OMP_CLAUSE_FIRSTPRIVATE)
11272 cxx_omp_finish_clause (loop_iv_seen, NULL, false);
11273 }
11274
11275 orig_pre_body = *pre_body;
11276 *pre_body = push_stmt_list ();
11277 if (orig_pre_body)
11278 add_stmt (orig_pre_body);
11279 if (init != NULL)
11280 finish_expr_stmt (build_x_modify_expr (elocus,
11281 iter, NOP_EXPR, init,
11282 NULL_TREE, tf_warning_or_error));
11283 init = build_int_cst (TREE_TYPE (diff), 0);
11284 if (c && iter_incr == NULL
11285 && (!ordered || (i < collapse && collapse > 1)))
11286 {
11287 if (incr_var)
11288 {
11289 finish_expr_stmt (build_x_modify_expr (elocus,
11290 incr_var, NOP_EXPR,
11291 incr, NULL_TREE,
11292 tf_warning_or_error));
11293 incr = incr_var;
11294 }
11295 iter_incr = build_x_modify_expr (elocus,
11296 iter, PLUS_EXPR, incr,
11297 NULL_TREE, tf_warning_or_error);
11298 }
11299 if (c && ordered && i < collapse && collapse > 1)
11300 iter_incr = incr;
11301 finish_expr_stmt (build_x_modify_expr (elocus,
11302 last, NOP_EXPR, init,
11303 NULL_TREE, tf_warning_or_error));
11304 if (diffvar)
11305 {
11306 finish_expr_stmt (build_x_modify_expr (elocus,
11307 diffvar, NOP_EXPR,
11308 diff, NULL_TREE, tf_warning_or_error));
11309 diff = diffvar;
11310 }
11311 *pre_body = pop_stmt_list (*pre_body);
11312
11313 cond = cp_build_binary_op (elocus,
11314 TREE_CODE (cond), decl, diff,
11315 tf_warning_or_error);
11316 incr = build_modify_expr (elocus, decl, NULL_TREE, PLUS_EXPR,
11317 elocus, incr, NULL_TREE);
11318
11319 orig_body = *body;
11320 *body = push_stmt_list ();
11321 iter_init = build2 (MINUS_EXPR, TREE_TYPE (diff), decl, last);
11322 iter_init = build_x_modify_expr (elocus,
11323 iter, PLUS_EXPR, iter_init,
11324 NULL_TREE, tf_warning_or_error);
11325 if (iter_init != error_mark_node)
11326 iter_init = build1 (NOP_EXPR, void_type_node, iter_init);
11327 finish_expr_stmt (iter_init);
11328 finish_expr_stmt (build_x_modify_expr (elocus,
11329 last, NOP_EXPR, decl,
11330 NULL_TREE, tf_warning_or_error));
11331 add_stmt (orig_body);
11332 *body = pop_stmt_list (*body);
11333
11334 if (c)
11335 {
11336 OMP_CLAUSE_LASTPRIVATE_STMT (c) = push_stmt_list ();
11337 if (!ordered)
11338 finish_expr_stmt (iter_incr);
11339 else
11340 {
11341 iter_init = decl;
11342 if (i < collapse && collapse > 1 && !error_operand_p (iter_incr))
11343 iter_init = build2 (PLUS_EXPR, TREE_TYPE (diff),
11344 iter_init, iter_incr);
11345 iter_init = build2 (MINUS_EXPR, TREE_TYPE (diff), iter_init, last);
11346 iter_init = build_x_modify_expr (elocus,
11347 iter, PLUS_EXPR, iter_init,
11348 NULL_TREE, tf_warning_or_error);
11349 if (iter_init != error_mark_node)
11350 iter_init = build1 (NOP_EXPR, void_type_node, iter_init);
11351 finish_expr_stmt (iter_init);
11352 }
11353 OMP_CLAUSE_LASTPRIVATE_STMT (c)
11354 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (c));
11355 }
11356
11357 if (TREE_CODE (TREE_VEC_ELT (orig_declv, i)) == TREE_LIST)
11358 {
11359 tree t = TREE_VEC_ELT (orig_declv, i);
11360 gcc_assert (TREE_PURPOSE (t) == NULL_TREE
11361 && TREE_VALUE (t) == NULL_TREE
11362 && TREE_CODE (TREE_CHAIN (t)) == TREE_VEC);
11363 TREE_PURPOSE (t) = TREE_VEC_ELT (declv, i);
11364 TREE_VALUE (t) = last;
11365 }
11366 else
11367 TREE_VEC_ELT (orig_declv, i)
11368 = tree_cons (TREE_VEC_ELT (declv, i), last, NULL_TREE);
11369 TREE_VEC_ELT (declv, i) = decl;
11370 TREE_VEC_ELT (initv, i) = init;
11371 TREE_VEC_ELT (condv, i) = cond;
11372 TREE_VEC_ELT (incrv, i) = incr;
11373
11374 return false;
11375 }
11376
11377 /* Build and validate an OMP_FOR statement. CLAUSES, BODY, COND, INCR
11378 are directly for their associated operands in the statement. DECL
11379 and INIT are a combo; if DECL is NULL then INIT ought to be a
11380 MODIFY_EXPR, and the DECL should be extracted. PRE_BODY are
11381 optional statements that need to go before the loop into its
11382 sk_omp scope. */
11383
11384 tree
11385 finish_omp_for (location_t locus, enum tree_code code, tree declv,
11386 tree orig_declv, tree initv, tree condv, tree incrv,
11387 tree body, tree pre_body, vec<tree> *orig_inits, tree clauses)
11388 {
11389 tree omp_for = NULL, orig_incr = NULL;
11390 tree decl = NULL, init, cond, incr;
11391 location_t elocus;
11392 int i;
11393 int collapse = 1;
11394 int ordered = 0;
11395 auto_vec<location_t> init_locv;
11396
11397 gcc_assert (TREE_VEC_LENGTH (declv) == TREE_VEC_LENGTH (initv));
11398 gcc_assert (TREE_VEC_LENGTH (declv) == TREE_VEC_LENGTH (condv));
11399 gcc_assert (TREE_VEC_LENGTH (declv) == TREE_VEC_LENGTH (incrv));
11400 if (TREE_VEC_LENGTH (declv) > 1)
11401 {
11402 if (tree ti = omp_find_clause (clauses, OMP_CLAUSE_TILE))
11403 collapse = list_length (OMP_CLAUSE_TILE_LIST (ti));
11404 else
11405 {
11406 if (tree co = omp_find_clause (clauses, OMP_CLAUSE_COLLAPSE))
11407 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (co));
11408 else if (tree si = omp_find_clause (clauses, OMP_CLAUSE_SIZES))
11409 collapse = list_length (OMP_CLAUSE_SIZES_LIST (si));
11410 if (collapse != TREE_VEC_LENGTH (declv))
11411 ordered = TREE_VEC_LENGTH (declv);
11412 }
11413 }
11414 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
11415 {
11416 decl = TREE_VEC_ELT (declv, i);
11417 init = TREE_VEC_ELT (initv, i);
11418 cond = TREE_VEC_ELT (condv, i);
11419 incr = TREE_VEC_ELT (incrv, i);
11420 elocus = locus;
11421
11422 if (decl == global_namespace)
11423 {
11424 gcc_assert (init == NULL_TREE && cond == NULL_TREE && incr == NULL_TREE);
11425 TREE_VEC_ELT (declv, i) = NULL_TREE;
11426 init_locv.safe_push (UNKNOWN_LOCATION);
11427 continue;
11428 }
11429 /* We are going to throw out the init's original MODIFY_EXPR or
11430 MODOP_EXPR below. Save its location so we can use it when
11431 reconstructing the expression farther down. Alternatively, if the
11432 initializer is a binding of the iteration variable, save
11433 that location. Any of these locations in the initialization clause
11434 for the current nested loop are better than using the argument locus,
11435 that points to the "for" of the outermost loop in the nest. */
11436 if (init && EXPR_HAS_LOCATION (init))
11437 elocus = EXPR_LOCATION (init);
11438 else if (decl && INDIRECT_REF_P (decl) && EXPR_HAS_LOCATION (decl))
11439 /* This can happen for class iterators. */
11440 elocus = EXPR_LOCATION (decl);
11441 else if (decl && DECL_P (decl))
11442 {
11443 if (DECL_SOURCE_LOCATION (decl) != UNKNOWN_LOCATION)
11444 elocus = DECL_SOURCE_LOCATION (decl);
11445 else if (DECL_INITIAL (decl)
11446 && EXPR_HAS_LOCATION (DECL_INITIAL (decl)))
11447 elocus = EXPR_LOCATION (DECL_INITIAL (decl));
11448 }
11449 init_locv.safe_push (elocus);
11450
11451 if (decl == NULL)
11452 {
11453 if (init != NULL)
11454 switch (TREE_CODE (init))
11455 {
11456 case MODIFY_EXPR:
11457 decl = TREE_OPERAND (init, 0);
11458 init = TREE_OPERAND (init, 1);
11459 break;
11460 case MODOP_EXPR:
11461 if (TREE_CODE (TREE_OPERAND (init, 1)) == NOP_EXPR)
11462 {
11463 decl = TREE_OPERAND (init, 0);
11464 init = TREE_OPERAND (init, 2);
11465 }
11466 break;
11467 default:
11468 break;
11469 }
11470
11471 if (decl == NULL)
11472 {
11473 error_at (locus,
11474 "expected iteration declaration or initialization");
11475 return NULL;
11476 }
11477 }
11478
11479 if (cond == global_namespace)
11480 continue;
11481
11482 if (cond == NULL)
11483 {
11484 error_at (elocus, "missing controlling predicate");
11485 return NULL;
11486 }
11487
11488 if (incr == NULL)
11489 {
11490 error_at (elocus, "missing increment expression");
11491 return NULL;
11492 }
11493
11494 TREE_VEC_ELT (declv, i) = decl;
11495 TREE_VEC_ELT (initv, i) = init;
11496 }
11497
11498 if (orig_inits)
11499 {
11500 bool fail = false;
11501 tree orig_init;
11502 FOR_EACH_VEC_ELT (*orig_inits, i, orig_init)
11503 if (orig_init
11504 && !c_omp_check_loop_iv_exprs (locus, code,
11505 orig_declv ? orig_declv : declv, i,
11506 TREE_VEC_ELT (declv, i), orig_init,
11507 NULL_TREE, cp_walk_subtrees))
11508 fail = true;
11509 if (fail)
11510 return NULL;
11511 }
11512
11513 if (dependent_omp_for_p (declv, initv, condv, incrv, body))
11514 {
11515 tree stmt;
11516
11517 stmt = make_node (code);
11518
11519 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
11520 {
11521 if (TREE_VEC_ELT (declv, i) == NULL_TREE)
11522 continue;
11523 /* This is really just a place-holder. We'll be decomposing this
11524 again and going through the cp_build_modify_expr path below when
11525 we instantiate the thing. */
11526 TREE_VEC_ELT (initv, i)
11527 = build2_loc (init_locv[i], MODIFY_EXPR, void_type_node,
11528 TREE_VEC_ELT (declv, i), TREE_VEC_ELT (initv, i));
11529 }
11530
11531 TREE_TYPE (stmt) = void_type_node;
11532 OMP_FOR_INIT (stmt) = initv;
11533 OMP_FOR_COND (stmt) = condv;
11534 OMP_FOR_INCR (stmt) = incrv;
11535 OMP_FOR_BODY (stmt) = body;
11536 OMP_FOR_PRE_BODY (stmt) = pre_body;
11537 OMP_FOR_CLAUSES (stmt) = clauses;
11538
11539 SET_EXPR_LOCATION (stmt, locus);
11540 return add_stmt (stmt);
11541 }
11542
11543 if (!orig_declv)
11544 orig_declv = copy_node (declv);
11545
11546 if (processing_template_decl)
11547 orig_incr = make_tree_vec (TREE_VEC_LENGTH (incrv));
11548
11549 for (i = 0; i < TREE_VEC_LENGTH (declv); )
11550 {
11551 decl = TREE_VEC_ELT (declv, i);
11552 init = TREE_VEC_ELT (initv, i);
11553 cond = TREE_VEC_ELT (condv, i);
11554 incr = TREE_VEC_ELT (incrv, i);
11555 if (orig_incr)
11556 TREE_VEC_ELT (orig_incr, i) = incr;
11557 elocus = init_locv[i];
11558
11559 if (decl == NULL_TREE)
11560 {
11561 i++;
11562 continue;
11563 }
11564
11565 if (!DECL_P (decl))
11566 {
11567 error_at (elocus, "expected iteration declaration or initialization");
11568 return NULL;
11569 }
11570
11571 if (incr && TREE_CODE (incr) == MODOP_EXPR)
11572 {
11573 if (orig_incr)
11574 TREE_VEC_ELT (orig_incr, i) = incr;
11575 incr = cp_build_modify_expr (elocus, TREE_OPERAND (incr, 0),
11576 TREE_CODE (TREE_OPERAND (incr, 1)),
11577 TREE_OPERAND (incr, 2),
11578 tf_warning_or_error);
11579 }
11580
11581 if (CLASS_TYPE_P (TREE_TYPE (decl)))
11582 {
11583 if (code == OMP_SIMD)
11584 {
11585 error_at (elocus, "%<#pragma omp simd%> used with class "
11586 "iteration variable %qE", decl);
11587 return NULL;
11588 }
11589 if (handle_omp_for_class_iterator (i, locus, code, declv, orig_declv,
11590 initv, condv, incrv, &body,
11591 &pre_body, clauses,
11592 collapse, ordered))
11593 return NULL;
11594 continue;
11595 }
11596
11597 if (!INTEGRAL_TYPE_P (TREE_TYPE (decl))
11598 && !TYPE_PTR_P (TREE_TYPE (decl)))
11599 {
11600 error_at (elocus, "invalid type for iteration variable %qE", decl);
11601 return NULL;
11602 }
11603
11604 if (!processing_template_decl && TREE_CODE (init) != TREE_VEC)
11605 init = cp_build_modify_expr (elocus, decl, NOP_EXPR, init,
11606 tf_warning_or_error);
11607 else
11608 init = build2_loc (elocus, MODIFY_EXPR, void_type_node, decl, init);
11609 if (decl == error_mark_node || init == error_mark_node)
11610 return NULL;
11611
11612 TREE_VEC_ELT (declv, i) = decl;
11613 TREE_VEC_ELT (initv, i) = init;
11614 TREE_VEC_ELT (condv, i) = cond;
11615 TREE_VEC_ELT (incrv, i) = incr;
11616 i++;
11617 }
11618
11619 if (pre_body && IS_EMPTY_STMT (pre_body))
11620 pre_body = NULL;
11621
11622 omp_for = c_finish_omp_for (locus, code, declv, orig_declv, initv, condv,
11623 incrv, body, pre_body,
11624 !processing_template_decl);
11625
11626 /* Check for iterators appearing in lb, b or incr expressions. */
11627 if (omp_for && !c_omp_check_loop_iv (omp_for, orig_declv, cp_walk_subtrees))
11628 omp_for = NULL_TREE;
11629
11630 if (omp_for == NULL)
11631 return NULL;
11632
11633 add_stmt (omp_for);
11634
11635 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INCR (omp_for)); i++)
11636 {
11637 init = TREE_VEC_ELT (OMP_FOR_INIT (omp_for), i);
11638 if (init == NULL_TREE)
11639 continue;
11640 decl = TREE_OPERAND (init, 0);
11641 cond = TREE_VEC_ELT (OMP_FOR_COND (omp_for), i);
11642 incr = TREE_VEC_ELT (OMP_FOR_INCR (omp_for), i);
11643
11644 if (!processing_template_decl)
11645 {
11646 if (TREE_CODE (TREE_OPERAND (init, 1)) == TREE_VEC)
11647 {
11648 tree t = TREE_VEC_ELT (TREE_OPERAND (init, 1), 1);
11649 TREE_VEC_ELT (TREE_OPERAND (init, 1), 1)
11650 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
11651 t = TREE_VEC_ELT (TREE_OPERAND (init, 1), 2);
11652 TREE_VEC_ELT (TREE_OPERAND (init, 1), 2)
11653 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
11654 }
11655 else
11656 {
11657 tree t = TREE_OPERAND (init, 1);
11658 TREE_OPERAND (init, 1)
11659 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
11660 }
11661 if (TREE_CODE (TREE_OPERAND (cond, 1)) == TREE_VEC)
11662 {
11663 tree t = TREE_VEC_ELT (TREE_OPERAND (cond, 1), 1);
11664 TREE_VEC_ELT (TREE_OPERAND (cond, 1), 1)
11665 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
11666 t = TREE_VEC_ELT (TREE_OPERAND (cond, 1), 2);
11667 TREE_VEC_ELT (TREE_OPERAND (cond, 1), 2)
11668 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
11669 }
11670 else
11671 {
11672 tree t = TREE_OPERAND (cond, 1);
11673 TREE_OPERAND (cond, 1)
11674 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
11675 }
11676 }
11677
11678 if (TREE_CODE (incr) != MODIFY_EXPR)
11679 continue;
11680
11681 if (TREE_SIDE_EFFECTS (TREE_OPERAND (incr, 1))
11682 && BINARY_CLASS_P (TREE_OPERAND (incr, 1))
11683 && !processing_template_decl)
11684 {
11685 tree t = TREE_OPERAND (TREE_OPERAND (incr, 1), 0);
11686 if (TREE_SIDE_EFFECTS (t)
11687 && t != decl
11688 && (TREE_CODE (t) != NOP_EXPR
11689 || TREE_OPERAND (t, 0) != decl))
11690 TREE_OPERAND (TREE_OPERAND (incr, 1), 0)
11691 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
11692
11693 t = TREE_OPERAND (TREE_OPERAND (incr, 1), 1);
11694 if (TREE_SIDE_EFFECTS (t)
11695 && t != decl
11696 && (TREE_CODE (t) != NOP_EXPR
11697 || TREE_OPERAND (t, 0) != decl))
11698 TREE_OPERAND (TREE_OPERAND (incr, 1), 1)
11699 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
11700 }
11701
11702 if (orig_incr)
11703 TREE_VEC_ELT (OMP_FOR_INCR (omp_for), i) = TREE_VEC_ELT (orig_incr, i);
11704 }
11705 OMP_FOR_CLAUSES (omp_for) = clauses;
11706
11707 /* For simd loops with non-static data member iterators, we could have added
11708 OMP_CLAUSE_LINEAR clauses without OMP_CLAUSE_LINEAR_STEP. As we know the
11709 step at this point, fill it in. */
11710 if (code == OMP_SIMD && !processing_template_decl
11711 && TREE_VEC_LENGTH (OMP_FOR_INCR (omp_for)) == 1)
11712 for (tree c = omp_find_clause (clauses, OMP_CLAUSE_LINEAR); c;
11713 c = omp_find_clause (OMP_CLAUSE_CHAIN (c), OMP_CLAUSE_LINEAR))
11714 if (OMP_CLAUSE_LINEAR_STEP (c) == NULL_TREE)
11715 {
11716 decl = TREE_OPERAND (TREE_VEC_ELT (OMP_FOR_INIT (omp_for), 0), 0);
11717 gcc_assert (decl == OMP_CLAUSE_DECL (c));
11718 incr = TREE_VEC_ELT (OMP_FOR_INCR (omp_for), 0);
11719 tree step, stept;
11720 switch (TREE_CODE (incr))
11721 {
11722 case PREINCREMENT_EXPR:
11723 case POSTINCREMENT_EXPR:
11724 /* c_omp_for_incr_canonicalize_ptr() should have been
11725 called to massage things appropriately. */
11726 gcc_assert (!INDIRECT_TYPE_P (TREE_TYPE (decl)));
11727 OMP_CLAUSE_LINEAR_STEP (c) = build_int_cst (TREE_TYPE (decl), 1);
11728 break;
11729 case PREDECREMENT_EXPR:
11730 case POSTDECREMENT_EXPR:
11731 /* c_omp_for_incr_canonicalize_ptr() should have been
11732 called to massage things appropriately. */
11733 gcc_assert (!INDIRECT_TYPE_P (TREE_TYPE (decl)));
11734 OMP_CLAUSE_LINEAR_STEP (c)
11735 = build_int_cst (TREE_TYPE (decl), -1);
11736 break;
11737 case MODIFY_EXPR:
11738 gcc_assert (TREE_OPERAND (incr, 0) == decl);
11739 incr = TREE_OPERAND (incr, 1);
11740 switch (TREE_CODE (incr))
11741 {
11742 case PLUS_EXPR:
11743 if (TREE_OPERAND (incr, 1) == decl)
11744 step = TREE_OPERAND (incr, 0);
11745 else
11746 step = TREE_OPERAND (incr, 1);
11747 break;
11748 case MINUS_EXPR:
11749 case POINTER_PLUS_EXPR:
11750 gcc_assert (TREE_OPERAND (incr, 0) == decl);
11751 step = TREE_OPERAND (incr, 1);
11752 break;
11753 default:
11754 gcc_unreachable ();
11755 }
11756 stept = TREE_TYPE (decl);
11757 if (INDIRECT_TYPE_P (stept))
11758 stept = sizetype;
11759 step = fold_convert (stept, step);
11760 if (TREE_CODE (incr) == MINUS_EXPR)
11761 step = fold_build1 (NEGATE_EXPR, stept, step);
11762 OMP_CLAUSE_LINEAR_STEP (c) = step;
11763 break;
11764 default:
11765 gcc_unreachable ();
11766 }
11767 }
11768 /* Override saved methods on OMP_LOOP's OMP_CLAUSE_LASTPRIVATE_LOOP_IV
11769 clauses, we need copy ctor for those rather than default ctor,
11770 plus as for other lastprivates assignment op and dtor. */
11771 if (code == OMP_LOOP && !processing_template_decl)
11772 for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
11773 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
11774 && OMP_CLAUSE_LASTPRIVATE_LOOP_IV (c)
11775 && cxx_omp_create_clause_info (c, TREE_TYPE (OMP_CLAUSE_DECL (c)),
11776 false, true, true, true))
11777 CP_OMP_CLAUSE_INFO (c) = NULL_TREE;
11778
11779 return omp_for;
11780 }
11781
11782 /* Code walker for finish_omp_for_block: extract binding of DP->var
11783 from its current block and move it to a new BIND_EXPR DP->b
11784 surrounding the body of DP->omp_for. */
11785
11786 struct fofb_data {
11787 tree var;
11788 tree b;
11789 tree omp_for;
11790 };
11791
11792 static tree
11793 finish_omp_for_block_walker (tree *tp, int *walk_subtrees, void *dp)
11794 {
11795 struct fofb_data *fofb = (struct fofb_data *)dp;
11796 if (TREE_CODE (*tp) == BIND_EXPR)
11797 for (tree *p = &BIND_EXPR_VARS (*tp); *p; p = &DECL_CHAIN (*p))
11798 {
11799 if (*p == fofb->var)
11800 {
11801 *p = DECL_CHAIN (*p);
11802 if (fofb->b == NULL_TREE)
11803 {
11804 fofb->b = make_node (BLOCK);
11805 fofb->b = build3 (BIND_EXPR, void_type_node, NULL_TREE,
11806 OMP_FOR_BODY (fofb->omp_for), fofb->b);
11807 TREE_SIDE_EFFECTS (fofb->b) = 1;
11808 OMP_FOR_BODY (fofb->omp_for) = fofb->b;
11809 }
11810 DECL_CHAIN (fofb->var) = BIND_EXPR_VARS (fofb->b);
11811 BIND_EXPR_VARS (fofb->b) = fofb->var;
11812 BLOCK_VARS (BIND_EXPR_BLOCK (fofb->b)) = fofb->var;
11813 BLOCK_VARS (BIND_EXPR_BLOCK (*tp)) = BIND_EXPR_VARS (*tp);
11814 return *tp;
11815 }
11816 }
11817 if (TREE_CODE (*tp) != BIND_EXPR && TREE_CODE (*tp) != STATEMENT_LIST)
11818 *walk_subtrees = false;
11819 return NULL_TREE;
11820 }
11821
11822 /* Fix up range for decls. Those decls were pushed into BIND's
11823 BIND_EXPR_VARS, or that of a nested BIND_EXPR inside its body,
11824 and need to be moved into a new BIND_EXPR surrounding OMP_FOR's body
11825 so that processing of combined loop directives can find them. */
11826 tree
11827 finish_omp_for_block (tree bind, tree omp_for)
11828 {
11829 if (omp_for == NULL_TREE
11830 || !OMP_FOR_ORIG_DECLS (omp_for)
11831 || bind == NULL_TREE)
11832 return bind;
11833 struct fofb_data fofb;
11834 fofb.b = NULL_TREE;
11835 fofb.omp_for = omp_for;
11836 for (int i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (omp_for)); i++)
11837 if (TREE_VEC_ELT (OMP_FOR_INIT (omp_for), i)
11838 && (TREE_CODE (TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (omp_for), i))
11839 == TREE_LIST)
11840 && TREE_CHAIN (TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (omp_for), i)))
11841 {
11842 tree v = TREE_CHAIN (TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (omp_for), i));
11843 for (int j = 2; j < TREE_VEC_LENGTH (v); j++)
11844 {
11845 fofb.var = TREE_VEC_ELT (v, j);
11846 cp_walk_tree (&bind, finish_omp_for_block_walker,
11847 (void *)&fofb, NULL);
11848 }
11849 }
11850 return bind;
11851 }
11852
11853 void
11854 finish_omp_atomic (location_t loc, enum tree_code code, enum tree_code opcode,
11855 tree lhs, tree rhs, tree v, tree lhs1, tree rhs1, tree r,
11856 tree clauses, enum omp_memory_order mo, bool weak)
11857 {
11858 tree orig_lhs;
11859 tree orig_rhs;
11860 tree orig_v;
11861 tree orig_lhs1;
11862 tree orig_rhs1;
11863 tree orig_r;
11864 bool dependent_p;
11865 tree stmt;
11866
11867 orig_lhs = lhs;
11868 orig_rhs = rhs;
11869 orig_v = v;
11870 orig_lhs1 = lhs1;
11871 orig_rhs1 = rhs1;
11872 orig_r = r;
11873 dependent_p = false;
11874 stmt = NULL_TREE;
11875
11876 /* Even in a template, we can detect invalid uses of the atomic
11877 pragma if neither LHS nor RHS is type-dependent. */
11878 if (processing_template_decl)
11879 {
11880 dependent_p = (type_dependent_expression_p (lhs)
11881 || (rhs && type_dependent_expression_p (rhs))
11882 || (v && type_dependent_expression_p (v))
11883 || (lhs1 && type_dependent_expression_p (lhs1))
11884 || (rhs1 && type_dependent_expression_p (rhs1))
11885 || (r
11886 && r != void_list_node
11887 && type_dependent_expression_p (r)));
11888 if (clauses)
11889 {
11890 gcc_assert (TREE_CODE (clauses) == OMP_CLAUSE
11891 && OMP_CLAUSE_CODE (clauses) == OMP_CLAUSE_HINT
11892 && OMP_CLAUSE_CHAIN (clauses) == NULL_TREE);
11893 if (type_dependent_expression_p (OMP_CLAUSE_HINT_EXPR (clauses))
11894 || TREE_CODE (OMP_CLAUSE_HINT_EXPR (clauses)) != INTEGER_CST)
11895 dependent_p = true;
11896 }
11897 }
11898 if (!dependent_p)
11899 {
11900 bool swapped = false;
11901 if (rhs1 && opcode != COND_EXPR && cp_tree_equal (lhs, rhs))
11902 {
11903 std::swap (rhs, rhs1);
11904 swapped = !commutative_tree_code (opcode);
11905 }
11906 if (rhs1 && opcode != COND_EXPR && !cp_tree_equal (lhs, rhs1))
11907 {
11908 if (code == OMP_ATOMIC)
11909 error ("%<#pragma omp atomic update%> uses two different "
11910 "expressions for memory");
11911 else
11912 error ("%<#pragma omp atomic capture%> uses two different "
11913 "expressions for memory");
11914 return;
11915 }
11916 if (lhs1 && !cp_tree_equal (lhs, lhs1))
11917 {
11918 if (code == OMP_ATOMIC)
11919 error ("%<#pragma omp atomic update%> uses two different "
11920 "expressions for memory");
11921 else
11922 error ("%<#pragma omp atomic capture%> uses two different "
11923 "expressions for memory");
11924 return;
11925 }
11926 stmt = c_finish_omp_atomic (loc, code, opcode, lhs, rhs,
11927 v, lhs1, rhs1, r, swapped, mo, weak,
11928 processing_template_decl != 0);
11929 if (stmt == error_mark_node)
11930 return;
11931 }
11932 if (processing_template_decl)
11933 {
11934 if (code == OMP_ATOMIC_READ)
11935 {
11936 stmt = build_min_nt_loc (loc, OMP_ATOMIC_READ, orig_lhs);
11937 OMP_ATOMIC_MEMORY_ORDER (stmt) = mo;
11938 stmt = build2 (MODIFY_EXPR, void_type_node, orig_v, stmt);
11939 }
11940 else
11941 {
11942 if (opcode == NOP_EXPR)
11943 stmt = build2 (MODIFY_EXPR, void_type_node, orig_lhs, orig_rhs);
11944 else if (opcode == COND_EXPR)
11945 {
11946 stmt = build2 (EQ_EXPR, boolean_type_node, orig_lhs, orig_rhs);
11947 if (orig_r)
11948 stmt = build2 (MODIFY_EXPR, boolean_type_node, orig_r,
11949 stmt);
11950 stmt = build3 (COND_EXPR, void_type_node, stmt, orig_rhs1,
11951 orig_lhs);
11952 orig_rhs1 = NULL_TREE;
11953 }
11954 else
11955 stmt = build2 (opcode, void_type_node, orig_lhs, orig_rhs);
11956 if (orig_rhs1)
11957 stmt = build_min_nt_loc (EXPR_LOCATION (orig_rhs1),
11958 COMPOUND_EXPR, orig_rhs1, stmt);
11959 if (code != OMP_ATOMIC)
11960 {
11961 stmt = build_min_nt_loc (loc, code, orig_lhs1, stmt);
11962 OMP_ATOMIC_MEMORY_ORDER (stmt) = mo;
11963 OMP_ATOMIC_WEAK (stmt) = weak;
11964 stmt = build2 (MODIFY_EXPR, void_type_node, orig_v, stmt);
11965 }
11966 }
11967 stmt = build2 (OMP_ATOMIC, void_type_node,
11968 clauses ? clauses : integer_zero_node, stmt);
11969 OMP_ATOMIC_MEMORY_ORDER (stmt) = mo;
11970 OMP_ATOMIC_WEAK (stmt) = weak;
11971 SET_EXPR_LOCATION (stmt, loc);
11972 }
11973
11974 /* Avoid -Wunused-value warnings here, the whole construct has side-effects
11975 and even if it might be wrapped from fold-const.cc or c-omp.cc wrapped
11976 in some tree that appears to be unused, the value is not unused. */
11977 warning_sentinel w (warn_unused_value);
11978 finish_expr_stmt (stmt);
11979 }
11980
11981 void
11982 finish_omp_barrier (void)
11983 {
11984 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_BARRIER);
11985 releasing_vec vec;
11986 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
11987 finish_expr_stmt (stmt);
11988 }
11989
11990 void
11991 finish_omp_depobj (location_t loc, tree depobj,
11992 enum omp_clause_depend_kind kind, tree clause)
11993 {
11994 if (!error_operand_p (depobj) && !type_dependent_expression_p (depobj))
11995 {
11996 if (!lvalue_p (depobj))
11997 {
11998 error_at (EXPR_LOC_OR_LOC (depobj, loc),
11999 "%<depobj%> expression is not lvalue expression");
12000 depobj = error_mark_node;
12001 }
12002 }
12003
12004 if (processing_template_decl)
12005 {
12006 if (clause == NULL_TREE)
12007 clause = build_int_cst (integer_type_node, kind);
12008 add_stmt (build_min_nt_loc (loc, OMP_DEPOBJ, depobj, clause));
12009 return;
12010 }
12011
12012 if (!error_operand_p (depobj))
12013 {
12014 tree addr = cp_build_addr_expr (depobj, tf_warning_or_error);
12015 if (addr == error_mark_node)
12016 depobj = error_mark_node;
12017 else
12018 depobj = cp_build_indirect_ref (loc, addr, RO_UNARY_STAR,
12019 tf_warning_or_error);
12020 }
12021
12022 c_finish_omp_depobj (loc, depobj, kind, clause);
12023 }
12024
12025 void
12026 finish_omp_flush (int mo)
12027 {
12028 tree fn = builtin_decl_explicit (BUILT_IN_SYNC_SYNCHRONIZE);
12029 releasing_vec vec;
12030 if (mo != MEMMODEL_LAST && mo != MEMMODEL_SEQ_CST)
12031 {
12032 fn = builtin_decl_explicit (BUILT_IN_ATOMIC_THREAD_FENCE);
12033 vec->quick_push (build_int_cst (integer_type_node, mo));
12034 }
12035 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
12036 finish_expr_stmt (stmt);
12037 }
12038
12039 void
12040 finish_omp_taskwait (void)
12041 {
12042 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_TASKWAIT);
12043 releasing_vec vec;
12044 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
12045 finish_expr_stmt (stmt);
12046 }
12047
12048 void
12049 finish_omp_taskyield (void)
12050 {
12051 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_TASKYIELD);
12052 releasing_vec vec;
12053 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
12054 finish_expr_stmt (stmt);
12055 }
12056
12057 void
12058 finish_omp_cancel (tree clauses)
12059 {
12060 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCEL);
12061 int mask = 0;
12062 if (omp_find_clause (clauses, OMP_CLAUSE_PARALLEL))
12063 mask = 1;
12064 else if (omp_find_clause (clauses, OMP_CLAUSE_FOR))
12065 mask = 2;
12066 else if (omp_find_clause (clauses, OMP_CLAUSE_SECTIONS))
12067 mask = 4;
12068 else if (omp_find_clause (clauses, OMP_CLAUSE_TASKGROUP))
12069 mask = 8;
12070 else
12071 {
12072 error ("%<#pragma omp cancel%> must specify one of "
12073 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> clauses");
12074 return;
12075 }
12076 releasing_vec vec;
12077 tree ifc = omp_find_clause (clauses, OMP_CLAUSE_IF);
12078 if (ifc != NULL_TREE)
12079 {
12080 if (OMP_CLAUSE_IF_MODIFIER (ifc) != ERROR_MARK
12081 && OMP_CLAUSE_IF_MODIFIER (ifc) != VOID_CST)
12082 error_at (OMP_CLAUSE_LOCATION (ifc),
12083 "expected %<cancel%> %<if%> clause modifier");
12084 else
12085 {
12086 tree ifc2 = omp_find_clause (OMP_CLAUSE_CHAIN (ifc), OMP_CLAUSE_IF);
12087 if (ifc2 != NULL_TREE)
12088 {
12089 gcc_assert (OMP_CLAUSE_IF_MODIFIER (ifc) == VOID_CST
12090 && OMP_CLAUSE_IF_MODIFIER (ifc2) != ERROR_MARK
12091 && OMP_CLAUSE_IF_MODIFIER (ifc2) != VOID_CST);
12092 error_at (OMP_CLAUSE_LOCATION (ifc2),
12093 "expected %<cancel%> %<if%> clause modifier");
12094 }
12095 }
12096
12097 if (!processing_template_decl)
12098 ifc = maybe_convert_cond (OMP_CLAUSE_IF_EXPR (ifc));
12099 else
12100 ifc = build_x_binary_op (OMP_CLAUSE_LOCATION (ifc), NE_EXPR,
12101 OMP_CLAUSE_IF_EXPR (ifc), ERROR_MARK,
12102 integer_zero_node, ERROR_MARK,
12103 NULL_TREE, NULL, tf_warning_or_error);
12104 }
12105 else
12106 ifc = boolean_true_node;
12107 vec->quick_push (build_int_cst (integer_type_node, mask));
12108 vec->quick_push (ifc);
12109 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
12110 finish_expr_stmt (stmt);
12111 }
12112
12113 void
12114 finish_omp_cancellation_point (tree clauses)
12115 {
12116 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCELLATION_POINT);
12117 int mask = 0;
12118 if (omp_find_clause (clauses, OMP_CLAUSE_PARALLEL))
12119 mask = 1;
12120 else if (omp_find_clause (clauses, OMP_CLAUSE_FOR))
12121 mask = 2;
12122 else if (omp_find_clause (clauses, OMP_CLAUSE_SECTIONS))
12123 mask = 4;
12124 else if (omp_find_clause (clauses, OMP_CLAUSE_TASKGROUP))
12125 mask = 8;
12126 else
12127 {
12128 error ("%<#pragma omp cancellation point%> must specify one of "
12129 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> clauses");
12130 return;
12131 }
12132 releasing_vec vec
12133 = make_tree_vector_single (build_int_cst (integer_type_node, mask));
12134 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
12135 finish_expr_stmt (stmt);
12136 }
12137
12138 /* Begin a __transaction_atomic or __transaction_relaxed statement.
12139 If PCOMPOUND is non-null, this is for a function-transaction-block, and we
12140 should create an extra compound stmt. */
12141
12142 tree
12143 begin_transaction_stmt (location_t loc, tree *pcompound, int flags)
12144 {
12145 tree r;
12146
12147 if (pcompound)
12148 *pcompound = begin_compound_stmt (0);
12149
12150 r = build_stmt (loc, TRANSACTION_EXPR, NULL_TREE);
12151
12152 /* Only add the statement to the function if support enabled. */
12153 if (flag_tm)
12154 add_stmt (r);
12155 else
12156 error_at (loc, ((flags & TM_STMT_ATTR_RELAXED) != 0
12157 ? G_("%<__transaction_relaxed%> without "
12158 "transactional memory support enabled")
12159 : G_("%<__transaction_atomic%> without "
12160 "transactional memory support enabled")));
12161
12162 TRANSACTION_EXPR_BODY (r) = push_stmt_list ();
12163 TREE_SIDE_EFFECTS (r) = 1;
12164 return r;
12165 }
12166
12167 /* End a __transaction_atomic or __transaction_relaxed statement.
12168 If COMPOUND_STMT is non-null, this is for a function-transaction-block,
12169 and we should end the compound. If NOEX is non-NULL, we wrap the body in
12170 a MUST_NOT_THROW_EXPR with NOEX as condition. */
12171
12172 void
12173 finish_transaction_stmt (tree stmt, tree compound_stmt, int flags, tree noex)
12174 {
12175 TRANSACTION_EXPR_BODY (stmt) = pop_stmt_list (TRANSACTION_EXPR_BODY (stmt));
12176 TRANSACTION_EXPR_OUTER (stmt) = (flags & TM_STMT_ATTR_OUTER) != 0;
12177 TRANSACTION_EXPR_RELAXED (stmt) = (flags & TM_STMT_ATTR_RELAXED) != 0;
12178 TRANSACTION_EXPR_IS_STMT (stmt) = 1;
12179
12180 /* noexcept specifications are not allowed for function transactions. */
12181 gcc_assert (!(noex && compound_stmt));
12182 if (noex)
12183 {
12184 tree body = build_must_not_throw_expr (TRANSACTION_EXPR_BODY (stmt),
12185 noex);
12186 protected_set_expr_location
12187 (body, EXPR_LOCATION (TRANSACTION_EXPR_BODY (stmt)));
12188 TREE_SIDE_EFFECTS (body) = 1;
12189 TRANSACTION_EXPR_BODY (stmt) = body;
12190 }
12191
12192 if (compound_stmt)
12193 finish_compound_stmt (compound_stmt);
12194 }
12195
12196 /* Build a __transaction_atomic or __transaction_relaxed expression. If
12197 NOEX is non-NULL, we wrap the body in a MUST_NOT_THROW_EXPR with NOEX as
12198 condition. */
12199
12200 tree
12201 build_transaction_expr (location_t loc, tree expr, int flags, tree noex)
12202 {
12203 tree ret;
12204 if (noex)
12205 {
12206 expr = build_must_not_throw_expr (expr, noex);
12207 protected_set_expr_location (expr, loc);
12208 TREE_SIDE_EFFECTS (expr) = 1;
12209 }
12210 ret = build1 (TRANSACTION_EXPR, TREE_TYPE (expr), expr);
12211 if (flags & TM_STMT_ATTR_RELAXED)
12212 TRANSACTION_EXPR_RELAXED (ret) = 1;
12213 TREE_SIDE_EFFECTS (ret) = 1;
12214 SET_EXPR_LOCATION (ret, loc);
12215 return ret;
12216 }
12217
12218 void
12219 init_cp_semantics (void)
12220 {
12221 }
12222
12223
12224 /* Get constant string at LOCATION. Returns true if successful,
12225 otherwise false. */
12226
12227 bool
12228 cexpr_str::type_check (location_t location)
12229 {
12230 tsubst_flags_t complain = tf_warning_or_error;
12231
12232 if (message == NULL_TREE
12233 || message == error_mark_node
12234 || check_for_bare_parameter_packs (message))
12235 return false;
12236
12237 if (TREE_CODE (message) != STRING_CST
12238 && !type_dependent_expression_p (message))
12239 {
12240 message_sz
12241 = finish_class_member_access_expr (message,
12242 get_identifier ("size"),
12243 false, complain);
12244 if (message_sz != error_mark_node)
12245 message_data
12246 = finish_class_member_access_expr (message,
12247 get_identifier ("data"),
12248 false, complain);
12249 if (message_sz == error_mark_node || message_data == error_mark_node)
12250 {
12251 error_at (location, "constexpr string must be a string "
12252 "literal or object with %<size%> and "
12253 "%<data%> members");
12254 return false;
12255 }
12256 releasing_vec size_args, data_args;
12257 message_sz = finish_call_expr (message_sz, &size_args, false, false,
12258 complain);
12259 message_data = finish_call_expr (message_data, &data_args, false, false,
12260 complain);
12261 if (message_sz == error_mark_node || message_data == error_mark_node)
12262 return false;
12263 message_sz = build_converted_constant_expr (size_type_node, message_sz,
12264 complain);
12265 if (message_sz == error_mark_node)
12266 {
12267 error_at (location, "constexpr string %<size()%> "
12268 "must be implicitly convertible to "
12269 "%<std::size_t%>");
12270 return false;
12271 }
12272 message_data = build_converted_constant_expr (const_string_type_node,
12273 message_data, complain);
12274 if (message_data == error_mark_node)
12275 {
12276 error_at (location, "constexpr string %<data()%> "
12277 "must be implicitly convertible to "
12278 "%<const char*%>");
12279 return false;
12280 }
12281 }
12282 return true;
12283 }
12284
12285 /* Extract constant string at LOCATON into output string STR.
12286 Returns true if successful, otherwise false. */
12287
12288 bool
12289 cexpr_str::extract (location_t location, tree &str)
12290 {
12291 const char *msg;
12292 int len;
12293 if (!extract (location, msg, len))
12294 return false;
12295 str = build_string (len, msg);
12296 return true;
12297 }
12298
12299 /* Extract constant string at LOCATION into output string MSG with LEN.
12300 Returns true if successful, otherwise false. */
12301
12302 bool
12303 cexpr_str::extract (location_t location, const char * & msg, int &len)
12304 {
12305 tsubst_flags_t complain = tf_warning_or_error;
12306
12307 msg = NULL;
12308 if (message_sz && message_data)
12309 {
12310 tree msz = cxx_constant_value (message_sz, NULL_TREE, complain);
12311 if (!tree_fits_uhwi_p (msz))
12312 {
12313 error_at (location,
12314 "constexpr string %<size()%> "
12315 "must be a constant expression");
12316 return false;
12317 }
12318 else if ((unsigned HOST_WIDE_INT) (int) tree_to_uhwi (msz)
12319 != tree_to_uhwi (msz))
12320 {
12321 error_at (location,
12322 "constexpr string message %<size()%> "
12323 "%qE too large", msz);
12324 return false;
12325 }
12326 len = tree_to_uhwi (msz);
12327 tree data = maybe_constant_value (message_data, NULL_TREE,
12328 mce_true);
12329 if (!reduced_constant_expression_p (data))
12330 data = NULL_TREE;
12331 if (len)
12332 {
12333 if (data)
12334 msg = c_getstr (data);
12335 if (msg == NULL)
12336 buf = XNEWVEC (char, len);
12337 for (int i = 0; i < len; ++i)
12338 {
12339 tree t = message_data;
12340 if (i)
12341 t = build2 (POINTER_PLUS_EXPR,
12342 TREE_TYPE (message_data), message_data,
12343 size_int (i));
12344 t = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (t)), t);
12345 tree t2 = cxx_constant_value (t, NULL_TREE, complain);
12346 if (!tree_fits_shwi_p (t2))
12347 {
12348 error_at (location,
12349 "constexpr string %<data()[%d]%> "
12350 "must be a constant expression", i);
12351 return false;
12352 }
12353 if (msg == NULL)
12354 buf[i] = tree_to_shwi (t2);
12355 /* If c_getstr worked, just verify the first and
12356 last characters using constant evaluation. */
12357 else if (len > 2 && i == 0)
12358 i = len - 2;
12359 }
12360 if (msg == NULL)
12361 msg = buf;
12362 }
12363 else if (!data)
12364 {
12365 /* We don't have any function to test whether some
12366 expression is a core constant expression. So, instead
12367 test whether (message.data (), 0) is a constant
12368 expression. */
12369 data = build2 (COMPOUND_EXPR, integer_type_node,
12370 message_data, integer_zero_node);
12371 tree t = cxx_constant_value (data, NULL_TREE, complain);
12372 if (!integer_zerop (t))
12373 {
12374 error_at (location,
12375 "constexpr string %<data()%> "
12376 "must be a core constant expression");
12377 return false;
12378 }
12379 }
12380 }
12381 else
12382 {
12383 tree eltype = TREE_TYPE (TREE_TYPE (message));
12384 int sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (eltype));
12385 msg = TREE_STRING_POINTER (message);
12386 len = TREE_STRING_LENGTH (message) / sz - 1;
12387 }
12388
12389 return true;
12390 }
12391
12392 /* Build a STATIC_ASSERT for a static assertion with the condition
12393 CONDITION and the message text MESSAGE. LOCATION is the location
12394 of the static assertion in the source code. When MEMBER_P, this
12395 static assertion is a member of a class. If SHOW_EXPR_P is true,
12396 print the condition (because it was instantiation-dependent). */
12397
12398 void
12399 finish_static_assert (tree condition, tree message, location_t location,
12400 bool member_p, bool show_expr_p)
12401 {
12402 tsubst_flags_t complain = tf_warning_or_error;
12403
12404 if (condition == NULL_TREE
12405 || condition == error_mark_node)
12406 return;
12407
12408 if (check_for_bare_parameter_packs (condition))
12409 return;
12410
12411 cexpr_str cstr(message);
12412 if (!cstr.type_check (location))
12413 return;
12414
12415 /* Save the condition in case it was a concept check. */
12416 tree orig_condition = condition;
12417
12418 if (instantiation_dependent_expression_p (condition)
12419 || instantiation_dependent_expression_p (message))
12420 {
12421 /* We're in a template; build a STATIC_ASSERT and put it in
12422 the right place. */
12423 defer:
12424 tree assertion = make_node (STATIC_ASSERT);
12425 STATIC_ASSERT_CONDITION (assertion) = orig_condition;
12426 STATIC_ASSERT_MESSAGE (assertion) = cstr.message;
12427 STATIC_ASSERT_SOURCE_LOCATION (assertion) = location;
12428
12429 if (member_p)
12430 maybe_add_class_template_decl_list (current_class_type,
12431 assertion,
12432 /*friend_p=*/0);
12433 else
12434 add_stmt (assertion);
12435
12436 return;
12437 }
12438
12439 /* Fold the expression and convert it to a boolean value. */
12440 condition = contextual_conv_bool (condition, complain);
12441 condition = fold_non_dependent_expr (condition, complain,
12442 /*manifestly_const_eval=*/true);
12443
12444 if (TREE_CODE (condition) == INTEGER_CST && !integer_zerop (condition))
12445 /* Do nothing; the condition is satisfied. */
12446 ;
12447 else
12448 {
12449 iloc_sentinel ils (location);
12450
12451 if (integer_zerop (condition))
12452 {
12453 /* CWG2518: static_assert failure in a template is not IFNDR. */
12454 if (processing_template_decl)
12455 goto defer;
12456
12457 int len;
12458 const char *msg = NULL;
12459 if (!cstr.extract (location, msg, len))
12460 return;
12461
12462 /* See if we can find which clause was failing (for logical AND). */
12463 tree bad = find_failing_clause (NULL, orig_condition);
12464 /* If not, or its location is unusable, fall back to the previous
12465 location. */
12466 location_t cloc = cp_expr_loc_or_loc (bad, location);
12467
12468 auto_diagnostic_group d;
12469
12470 /* Report the error. */
12471 if (len == 0)
12472 error_at (cloc, "static assertion failed");
12473 else
12474 error_at (cloc, "static assertion failed: %.*s", len, msg);
12475
12476 diagnose_failing_condition (bad, cloc, show_expr_p);
12477
12478 /* Suppress -Wreturn-type for functions with failed static_asserts.
12479 Otherwise templates like:
12480 if constexpr (whatever)
12481 return something (args);
12482 else
12483 static_assert (false, "explanation");
12484 get a useless extra -Wreturn-type warning. */
12485 if (current_function_decl)
12486 suppress_warning (current_function_decl, OPT_Wreturn_type);
12487 }
12488 else if (condition && condition != error_mark_node)
12489 {
12490 error ("non-constant condition for static assertion");
12491 if (require_rvalue_constant_expression (condition))
12492 cxx_constant_value (condition);
12493 }
12494 }
12495 }
12496
12497 /* Implements the C++0x decltype keyword. Returns the type of EXPR,
12498 suitable for use as a type-specifier.
12499
12500 ID_EXPRESSION_OR_MEMBER_ACCESS_P is true when EXPR was parsed as an
12501 id-expression or a class member access, FALSE when it was parsed as
12502 a full expression. */
12503
12504 tree
12505 finish_decltype_type (tree expr, bool id_expression_or_member_access_p,
12506 tsubst_flags_t complain)
12507 {
12508 tree type = NULL_TREE;
12509
12510 if (!expr || error_operand_p (expr))
12511 return error_mark_node;
12512
12513 if (TYPE_P (expr)
12514 || TREE_CODE (expr) == TYPE_DECL
12515 || (TREE_CODE (expr) == BIT_NOT_EXPR
12516 && TYPE_P (TREE_OPERAND (expr, 0))))
12517 {
12518 if (complain & tf_error)
12519 error ("argument to %<decltype%> must be an expression");
12520 return error_mark_node;
12521 }
12522
12523 /* decltype is an unevaluated context. */
12524 cp_unevaluated u;
12525
12526 processing_template_decl_sentinel ptds (/*reset=*/false);
12527
12528 /* Depending on the resolution of DR 1172, we may later need to distinguish
12529 instantiation-dependent but not type-dependent expressions so that, say,
12530 A<decltype(sizeof(T))>::U doesn't require 'typename'. */
12531 if (instantiation_dependent_uneval_expression_p (expr))
12532 {
12533 dependent:
12534 type = cxx_make_type (DECLTYPE_TYPE);
12535 DECLTYPE_TYPE_EXPR (type) = expr;
12536 DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (type)
12537 = id_expression_or_member_access_p;
12538 SET_TYPE_STRUCTURAL_EQUALITY (type);
12539
12540 return type;
12541 }
12542 else if (processing_template_decl)
12543 {
12544 expr = instantiate_non_dependent_expr (expr, complain|tf_decltype);
12545 if (expr == error_mark_node)
12546 return error_mark_node;
12547 /* Keep processing_template_decl cleared for the rest of the function
12548 (for sake of the call to lvalue_kind below, which handles templated
12549 and non-templated COND_EXPR differently). */
12550 processing_template_decl = 0;
12551 }
12552
12553 /* The type denoted by decltype(e) is defined as follows: */
12554
12555 expr = resolve_nondeduced_context (expr, complain);
12556 if (!mark_single_function (expr, complain))
12557 return error_mark_node;
12558
12559 if (invalid_nonstatic_memfn_p (input_location, expr, complain))
12560 return error_mark_node;
12561
12562 if (type_unknown_p (expr))
12563 {
12564 if (complain & tf_error)
12565 error ("%<decltype%> cannot resolve address of overloaded function");
12566 return error_mark_node;
12567 }
12568
12569 if (id_expression_or_member_access_p)
12570 {
12571 /* If e is an id-expression or a class member access (5.2.5
12572 [expr.ref]), decltype(e) is defined as the type of the entity
12573 named by e. If there is no such entity, or e names a set of
12574 overloaded functions, the program is ill-formed. */
12575 if (identifier_p (expr))
12576 expr = lookup_name (expr);
12577
12578 if (INDIRECT_REF_P (expr)
12579 || TREE_CODE (expr) == VIEW_CONVERT_EXPR)
12580 /* This can happen when the expression is, e.g., "a.b". Just
12581 look at the underlying operand. */
12582 expr = TREE_OPERAND (expr, 0);
12583
12584 if (TREE_CODE (expr) == OFFSET_REF
12585 || TREE_CODE (expr) == MEMBER_REF
12586 || TREE_CODE (expr) == SCOPE_REF)
12587 /* We're only interested in the field itself. If it is a
12588 BASELINK, we will need to see through it in the next
12589 step. */
12590 expr = TREE_OPERAND (expr, 1);
12591
12592 if (BASELINK_P (expr))
12593 /* See through BASELINK nodes to the underlying function. */
12594 expr = BASELINK_FUNCTIONS (expr);
12595
12596 /* decltype of a decomposition name drops references in the tuple case
12597 (unlike decltype of a normal variable) and keeps cv-qualifiers from
12598 the containing object in the other cases (unlike decltype of a member
12599 access expression). */
12600 if (DECL_DECOMPOSITION_P (expr))
12601 {
12602 if (ptds.saved)
12603 {
12604 gcc_checking_assert (DECL_HAS_VALUE_EXPR_P (expr)
12605 || (DECL_CONTEXT (expr)
12606 != current_function_decl));
12607 /* DECL_HAS_VALUE_EXPR_P is always set if
12608 processing_template_decl at least for structured bindings
12609 within the template. If lookup_decomp_type
12610 returns non-NULL, it is the tuple case. */
12611 if (tree ret = lookup_decomp_type (expr))
12612 return ret;
12613 gcc_checking_assert (DECL_HAS_VALUE_EXPR_P (expr));
12614 }
12615 if (DECL_HAS_VALUE_EXPR_P (expr))
12616 /* Expr is an array or struct subobject proxy, handle
12617 bit-fields properly. */
12618 return unlowered_expr_type (expr);
12619 else
12620 /* Expr is a reference variable for the tuple case. */
12621 return lookup_decomp_type (expr);
12622 }
12623
12624 switch (TREE_CODE (expr))
12625 {
12626 case FIELD_DECL:
12627 if (DECL_BIT_FIELD_TYPE (expr))
12628 {
12629 type = DECL_BIT_FIELD_TYPE (expr);
12630 break;
12631 }
12632 /* Fall through for fields that aren't bitfields. */
12633 gcc_fallthrough ();
12634
12635 case VAR_DECL:
12636 if (is_capture_proxy (expr))
12637 {
12638 if (is_normal_capture_proxy (expr))
12639 {
12640 expr = DECL_CAPTURED_VARIABLE (expr);
12641 type = TREE_TYPE (expr);
12642 }
12643 else
12644 {
12645 expr = DECL_VALUE_EXPR (expr);
12646 gcc_assert (TREE_CODE (expr) == COMPONENT_REF);
12647 expr = TREE_OPERAND (expr, 1);
12648 type = TREE_TYPE (expr);
12649 }
12650 break;
12651 }
12652 /* Fall through for variables that aren't capture proxies. */
12653 gcc_fallthrough ();
12654
12655 case FUNCTION_DECL:
12656 case CONST_DECL:
12657 case PARM_DECL:
12658 case RESULT_DECL:
12659 case TEMPLATE_PARM_INDEX:
12660 expr = mark_type_use (expr);
12661 type = TREE_TYPE (expr);
12662 if (VAR_P (expr) && DECL_NTTP_OBJECT_P (expr))
12663 {
12664 /* decltype of an NTTP object is the type of the template
12665 parameter, which is the object type modulo cv-quals. */
12666 int quals = cp_type_quals (type);
12667 gcc_checking_assert (quals & TYPE_QUAL_CONST);
12668 type = cv_unqualified (type);
12669 }
12670 break;
12671
12672 case ERROR_MARK:
12673 type = error_mark_node;
12674 break;
12675
12676 case COMPONENT_REF:
12677 case COMPOUND_EXPR:
12678 mark_type_use (expr);
12679 type = is_bitfield_expr_with_lowered_type (expr);
12680 if (!type)
12681 type = TREE_TYPE (TREE_OPERAND (expr, 1));
12682 break;
12683
12684 case BIT_FIELD_REF:
12685 gcc_unreachable ();
12686
12687 case INTEGER_CST:
12688 case PTRMEM_CST:
12689 /* We can get here when the id-expression refers to an
12690 enumerator or non-type template parameter. */
12691 type = TREE_TYPE (expr);
12692 break;
12693
12694 default:
12695 /* Handle instantiated template non-type arguments. */
12696 type = TREE_TYPE (expr);
12697 break;
12698 }
12699 }
12700 else
12701 {
12702 if (outer_automatic_var_p (STRIP_REFERENCE_REF (expr))
12703 && current_function_decl
12704 && LAMBDA_FUNCTION_P (current_function_decl))
12705 {
12706 /* [expr.prim.id.unqual]/3: If naming the entity from outside of an
12707 unevaluated operand within S would refer to an entity captured by
12708 copy in some intervening lambda-expression, then let E be the
12709 innermost such lambda-expression.
12710
12711 If there is such a lambda-expression and if P is in E's function
12712 parameter scope but not its parameter-declaration-clause, then the
12713 type of the expression is the type of a class member access
12714 expression naming the non-static data member that would be declared
12715 for such a capture in the object parameter of the function call
12716 operator of E." */
12717 /* FIXME: This transformation needs to happen for all uses of an outer
12718 local variable inside decltype, not just decltype((x)) (PR83167).
12719 And we don't handle nested lambdas properly, where we need to
12720 consider the outer lambdas as well (PR112926). */
12721 tree decl = STRIP_REFERENCE_REF (expr);
12722 tree lam = CLASSTYPE_LAMBDA_EXPR (DECL_CONTEXT (current_function_decl));
12723 tree cap = lookup_name (DECL_NAME (decl), LOOK_where::BLOCK,
12724 LOOK_want::HIDDEN_LAMBDA);
12725
12726 if (cap && is_capture_proxy (cap))
12727 type = TREE_TYPE (cap);
12728 else if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lam) == CPLD_COPY)
12729 {
12730 type = TREE_TYPE (decl);
12731 if (TYPE_REF_P (type)
12732 && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
12733 type = TREE_TYPE (type);
12734 }
12735
12736 if (type && !TYPE_REF_P (type))
12737 {
12738 tree obtype = TREE_TYPE (DECL_ARGUMENTS (current_function_decl));
12739 if (WILDCARD_TYPE_P (non_reference (obtype)))
12740 /* We don't know what the eventual obtype quals will be. */
12741 goto dependent;
12742 auto direct_type = [](tree t){
12743 if (INDIRECT_TYPE_P (t))
12744 return TREE_TYPE (t);
12745 return t;
12746 };
12747 int const quals = cp_type_quals (type)
12748 | cp_type_quals (direct_type (obtype));
12749 type = cp_build_qualified_type (type, quals);
12750 type = build_reference_type (type);
12751 }
12752 }
12753 else if (error_operand_p (expr))
12754 type = error_mark_node;
12755 else if (expr == current_class_ptr)
12756 /* If the expression is just "this", we want the
12757 cv-unqualified pointer for the "this" type. */
12758 type = TYPE_MAIN_VARIANT (TREE_TYPE (expr));
12759
12760 if (!type)
12761 {
12762 /* Otherwise, where T is the type of e, if e is an lvalue,
12763 decltype(e) is defined as T&; if an xvalue, T&&; otherwise, T. */
12764 cp_lvalue_kind clk = lvalue_kind (expr);
12765 type = unlowered_expr_type (expr);
12766 gcc_assert (!TYPE_REF_P (type));
12767
12768 /* For vector types, pick a non-opaque variant. */
12769 if (VECTOR_TYPE_P (type))
12770 type = strip_typedefs (type);
12771
12772 if (clk != clk_none && !(clk & clk_class))
12773 type = cp_build_reference_type (type, (clk & clk_rvalueref));
12774 }
12775 }
12776
12777 return type;
12778 }
12779
12780 /* Called from trait_expr_value to evaluate either __has_nothrow_assign or
12781 __has_nothrow_copy, depending on assign_p. Returns true iff all
12782 the copy {ctor,assign} fns are nothrow. */
12783
12784 static bool
12785 classtype_has_nothrow_assign_or_copy_p (tree type, bool assign_p)
12786 {
12787 tree fns = NULL_TREE;
12788
12789 if (assign_p || TYPE_HAS_COPY_CTOR (type))
12790 fns = get_class_binding (type, assign_p ? assign_op_identifier
12791 : ctor_identifier);
12792
12793 bool saw_copy = false;
12794 for (ovl_iterator iter (fns); iter; ++iter)
12795 {
12796 tree fn = *iter;
12797
12798 if (copy_fn_p (fn) > 0)
12799 {
12800 saw_copy = true;
12801 if (!maybe_instantiate_noexcept (fn)
12802 || !TYPE_NOTHROW_P (TREE_TYPE (fn)))
12803 return false;
12804 }
12805 }
12806
12807 return saw_copy;
12808 }
12809
12810 /* Return true if DERIVED is pointer interconvertible base of BASE. */
12811
12812 static bool
12813 pointer_interconvertible_base_of_p (tree base, tree derived)
12814 {
12815 if (base == error_mark_node || derived == error_mark_node)
12816 return false;
12817 base = TYPE_MAIN_VARIANT (base);
12818 derived = TYPE_MAIN_VARIANT (derived);
12819 if (!NON_UNION_CLASS_TYPE_P (base)
12820 || !NON_UNION_CLASS_TYPE_P (derived))
12821 return false;
12822
12823 if (same_type_p (base, derived))
12824 return true;
12825
12826 if (!std_layout_type_p (derived))
12827 return false;
12828
12829 return uniquely_derived_from_p (base, derived);
12830 }
12831
12832 /* Helper function for fold_builtin_is_pointer_inverconvertible_with_class,
12833 return true if MEMBERTYPE is the type of the first non-static data member
12834 of TYPE or for unions of any members. */
12835 static bool
12836 first_nonstatic_data_member_p (tree type, tree membertype)
12837 {
12838 for (tree field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
12839 {
12840 if (TREE_CODE (field) != FIELD_DECL)
12841 continue;
12842 if (DECL_FIELD_IS_BASE (field) && is_empty_field (field))
12843 continue;
12844 if (DECL_FIELD_IS_BASE (field))
12845 return first_nonstatic_data_member_p (TREE_TYPE (field), membertype);
12846 if (ANON_AGGR_TYPE_P (TREE_TYPE (field)))
12847 {
12848 if ((TREE_CODE (TREE_TYPE (field)) == UNION_TYPE
12849 || std_layout_type_p (TREE_TYPE (field)))
12850 && first_nonstatic_data_member_p (TREE_TYPE (field), membertype))
12851 return true;
12852 }
12853 else if (same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (field),
12854 membertype))
12855 return true;
12856 if (TREE_CODE (type) != UNION_TYPE)
12857 return false;
12858 }
12859 return false;
12860 }
12861
12862 /* Fold __builtin_is_pointer_interconvertible_with_class call. */
12863
12864 tree
12865 fold_builtin_is_pointer_inverconvertible_with_class (location_t loc, int nargs,
12866 tree *args)
12867 {
12868 /* Unless users call the builtin directly, the following 3 checks should be
12869 ensured from std::is_pointer_interconvertible_with_class function
12870 template. */
12871 if (nargs != 1)
12872 {
12873 error_at (loc, "%<__builtin_is_pointer_interconvertible_with_class%> "
12874 "needs a single argument");
12875 return boolean_false_node;
12876 }
12877 tree arg = args[0];
12878 if (error_operand_p (arg))
12879 return boolean_false_node;
12880 if (!TYPE_PTRMEM_P (TREE_TYPE (arg)))
12881 {
12882 error_at (loc, "%<__builtin_is_pointer_interconvertible_with_class%> "
12883 "argument is not pointer to member");
12884 return boolean_false_node;
12885 }
12886
12887 if (!TYPE_PTRDATAMEM_P (TREE_TYPE (arg)))
12888 return boolean_false_node;
12889
12890 tree membertype = TREE_TYPE (TREE_TYPE (arg));
12891 tree basetype = TYPE_OFFSET_BASETYPE (TREE_TYPE (arg));
12892 if (!complete_type_or_else (basetype, NULL_TREE))
12893 return boolean_false_node;
12894
12895 if (TREE_CODE (basetype) != UNION_TYPE
12896 && !std_layout_type_p (basetype))
12897 return boolean_false_node;
12898
12899 if (!first_nonstatic_data_member_p (basetype, membertype))
12900 return boolean_false_node;
12901
12902 if (TREE_CODE (arg) == PTRMEM_CST)
12903 arg = cplus_expand_constant (arg);
12904
12905 if (integer_nonzerop (arg))
12906 return boolean_false_node;
12907 if (integer_zerop (arg))
12908 return boolean_true_node;
12909
12910 return fold_build2 (EQ_EXPR, boolean_type_node, arg,
12911 build_zero_cst (TREE_TYPE (arg)));
12912 }
12913
12914 /* Helper function for is_corresponding_member_aggr. Return true if
12915 MEMBERTYPE pointer-to-data-member ARG can be found in anonymous
12916 union or structure BASETYPE. */
12917
12918 static bool
12919 is_corresponding_member_union (tree basetype, tree membertype, tree arg)
12920 {
12921 for (tree field = TYPE_FIELDS (basetype); field; field = DECL_CHAIN (field))
12922 if (TREE_CODE (field) != FIELD_DECL || DECL_BIT_FIELD_TYPE (field))
12923 continue;
12924 else if (same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (field),
12925 membertype))
12926 {
12927 if (TREE_CODE (arg) != INTEGER_CST
12928 || tree_int_cst_equal (arg, byte_position (field)))
12929 return true;
12930 }
12931 else if (ANON_AGGR_TYPE_P (TREE_TYPE (field)))
12932 {
12933 tree narg = arg;
12934 if (TREE_CODE (basetype) != UNION_TYPE
12935 && TREE_CODE (narg) == INTEGER_CST)
12936 narg = size_binop (MINUS_EXPR, arg, byte_position (field));
12937 if (is_corresponding_member_union (TREE_TYPE (field),
12938 membertype, narg))
12939 return true;
12940 }
12941 return false;
12942 }
12943
12944 /* Helper function for fold_builtin_is_corresponding_member call.
12945 Return boolean_false_node if MEMBERTYPE1 BASETYPE1::*ARG1 and
12946 MEMBERTYPE2 BASETYPE2::*ARG2 aren't corresponding members,
12947 boolean_true_node if they are corresponding members, or for
12948 non-constant ARG2 the highest member offset for corresponding
12949 members. */
12950
12951 static tree
12952 is_corresponding_member_aggr (location_t loc, tree basetype1, tree membertype1,
12953 tree arg1, tree basetype2, tree membertype2,
12954 tree arg2)
12955 {
12956 tree field1 = TYPE_FIELDS (basetype1);
12957 tree field2 = TYPE_FIELDS (basetype2);
12958 tree ret = boolean_false_node;
12959 while (1)
12960 {
12961 bool r = next_common_initial_sequence (field1, field2);
12962 if (field1 == NULL_TREE || field2 == NULL_TREE)
12963 break;
12964 if (r
12965 && same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (field1),
12966 membertype1)
12967 && same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (field2),
12968 membertype2))
12969 {
12970 tree pos = byte_position (field1);
12971 if (TREE_CODE (arg1) == INTEGER_CST
12972 && tree_int_cst_equal (arg1, pos))
12973 {
12974 if (TREE_CODE (arg2) == INTEGER_CST)
12975 return boolean_true_node;
12976 return pos;
12977 }
12978 else if (TREE_CODE (arg1) != INTEGER_CST)
12979 ret = pos;
12980 }
12981 else if (ANON_AGGR_TYPE_P (TREE_TYPE (field1))
12982 && ANON_AGGR_TYPE_P (TREE_TYPE (field2)))
12983 {
12984 if ((!lookup_attribute ("no_unique_address",
12985 DECL_ATTRIBUTES (field1)))
12986 != !lookup_attribute ("no_unique_address",
12987 DECL_ATTRIBUTES (field2)))
12988 break;
12989 if (!tree_int_cst_equal (bit_position (field1),
12990 bit_position (field2)))
12991 break;
12992 bool overlap = true;
12993 tree pos = byte_position (field1);
12994 if (TREE_CODE (arg1) == INTEGER_CST)
12995 {
12996 tree off1 = fold_convert (sizetype, arg1);
12997 tree sz1 = TYPE_SIZE_UNIT (TREE_TYPE (field1));
12998 if (tree_int_cst_lt (off1, pos)
12999 || tree_int_cst_le (size_binop (PLUS_EXPR, pos, sz1), off1))
13000 overlap = false;
13001 }
13002 if (TREE_CODE (arg2) == INTEGER_CST)
13003 {
13004 tree off2 = fold_convert (sizetype, arg2);
13005 tree sz2 = TYPE_SIZE_UNIT (TREE_TYPE (field2));
13006 if (tree_int_cst_lt (off2, pos)
13007 || tree_int_cst_le (size_binop (PLUS_EXPR, pos, sz2), off2))
13008 overlap = false;
13009 }
13010 if (overlap
13011 && NON_UNION_CLASS_TYPE_P (TREE_TYPE (field1))
13012 && NON_UNION_CLASS_TYPE_P (TREE_TYPE (field2)))
13013 {
13014 tree narg1 = arg1;
13015 if (TREE_CODE (arg1) == INTEGER_CST)
13016 narg1 = size_binop (MINUS_EXPR,
13017 fold_convert (sizetype, arg1), pos);
13018 tree narg2 = arg2;
13019 if (TREE_CODE (arg2) == INTEGER_CST)
13020 narg2 = size_binop (MINUS_EXPR,
13021 fold_convert (sizetype, arg2), pos);
13022 tree t1 = TREE_TYPE (field1);
13023 tree t2 = TREE_TYPE (field2);
13024 tree nret = is_corresponding_member_aggr (loc, t1, membertype1,
13025 narg1, t2, membertype2,
13026 narg2);
13027 if (nret != boolean_false_node)
13028 {
13029 if (nret == boolean_true_node)
13030 return nret;
13031 if (TREE_CODE (arg1) == INTEGER_CST)
13032 return size_binop (PLUS_EXPR, nret, pos);
13033 ret = size_binop (PLUS_EXPR, nret, pos);
13034 }
13035 }
13036 else if (overlap
13037 && TREE_CODE (TREE_TYPE (field1)) == UNION_TYPE
13038 && TREE_CODE (TREE_TYPE (field2)) == UNION_TYPE)
13039 {
13040 tree narg1 = arg1;
13041 if (TREE_CODE (arg1) == INTEGER_CST)
13042 narg1 = size_binop (MINUS_EXPR,
13043 fold_convert (sizetype, arg1), pos);
13044 tree narg2 = arg2;
13045 if (TREE_CODE (arg2) == INTEGER_CST)
13046 narg2 = size_binop (MINUS_EXPR,
13047 fold_convert (sizetype, arg2), pos);
13048 if (is_corresponding_member_union (TREE_TYPE (field1),
13049 membertype1, narg1)
13050 && is_corresponding_member_union (TREE_TYPE (field2),
13051 membertype2, narg2))
13052 {
13053 sorry_at (loc, "%<__builtin_is_corresponding_member%> "
13054 "not well defined for anonymous unions");
13055 return boolean_false_node;
13056 }
13057 }
13058 }
13059 if (!r)
13060 break;
13061 field1 = DECL_CHAIN (field1);
13062 field2 = DECL_CHAIN (field2);
13063 }
13064 return ret;
13065 }
13066
13067 /* Fold __builtin_is_corresponding_member call. */
13068
13069 tree
13070 fold_builtin_is_corresponding_member (location_t loc, int nargs,
13071 tree *args)
13072 {
13073 /* Unless users call the builtin directly, the following 3 checks should be
13074 ensured from std::is_corresponding_member function template. */
13075 if (nargs != 2)
13076 {
13077 error_at (loc, "%<__builtin_is_corresponding_member%> "
13078 "needs two arguments");
13079 return boolean_false_node;
13080 }
13081 tree arg1 = args[0];
13082 tree arg2 = args[1];
13083 if (error_operand_p (arg1) || error_operand_p (arg2))
13084 return boolean_false_node;
13085 if (!TYPE_PTRMEM_P (TREE_TYPE (arg1))
13086 || !TYPE_PTRMEM_P (TREE_TYPE (arg2)))
13087 {
13088 error_at (loc, "%<__builtin_is_corresponding_member%> "
13089 "argument is not pointer to member");
13090 return boolean_false_node;
13091 }
13092
13093 if (!TYPE_PTRDATAMEM_P (TREE_TYPE (arg1))
13094 || !TYPE_PTRDATAMEM_P (TREE_TYPE (arg2)))
13095 return boolean_false_node;
13096
13097 tree membertype1 = TREE_TYPE (TREE_TYPE (arg1));
13098 tree basetype1 = TYPE_OFFSET_BASETYPE (TREE_TYPE (arg1));
13099 if (!complete_type_or_else (basetype1, NULL_TREE))
13100 return boolean_false_node;
13101
13102 tree membertype2 = TREE_TYPE (TREE_TYPE (arg2));
13103 tree basetype2 = TYPE_OFFSET_BASETYPE (TREE_TYPE (arg2));
13104 if (!complete_type_or_else (basetype2, NULL_TREE))
13105 return boolean_false_node;
13106
13107 if (!NON_UNION_CLASS_TYPE_P (basetype1)
13108 || !NON_UNION_CLASS_TYPE_P (basetype2)
13109 || !std_layout_type_p (basetype1)
13110 || !std_layout_type_p (basetype2))
13111 return boolean_false_node;
13112
13113 /* If the member types aren't layout compatible, then they
13114 can't be corresponding members. */
13115 if (!layout_compatible_type_p (membertype1, membertype2))
13116 return boolean_false_node;
13117
13118 if (TREE_CODE (arg1) == PTRMEM_CST)
13119 arg1 = cplus_expand_constant (arg1);
13120 if (TREE_CODE (arg2) == PTRMEM_CST)
13121 arg2 = cplus_expand_constant (arg2);
13122
13123 if (null_member_pointer_value_p (arg1)
13124 || null_member_pointer_value_p (arg2))
13125 return boolean_false_node;
13126
13127 if (TREE_CODE (arg1) == INTEGER_CST
13128 && TREE_CODE (arg2) == INTEGER_CST
13129 && !tree_int_cst_equal (arg1, arg2))
13130 return boolean_false_node;
13131
13132 if (TREE_CODE (arg2) == INTEGER_CST
13133 && TREE_CODE (arg1) != INTEGER_CST)
13134 {
13135 std::swap (arg1, arg2);
13136 std::swap (membertype1, membertype2);
13137 std::swap (basetype1, basetype2);
13138 }
13139
13140 tree ret = is_corresponding_member_aggr (loc, basetype1, membertype1, arg1,
13141 basetype2, membertype2, arg2);
13142 if (TREE_TYPE (ret) == boolean_type_node)
13143 return ret;
13144 /* If both arg1 and arg2 are INTEGER_CSTs, is_corresponding_member_aggr
13145 already returns boolean_{true,false}_node whether those particular
13146 members are corresponding members or not. Otherwise, if only
13147 one of them is INTEGER_CST (canonicalized to first being INTEGER_CST
13148 above), it returns boolean_false_node if it is certainly not a
13149 corresponding member and otherwise we need to do a runtime check that
13150 those two OFFSET_TYPE offsets are equal.
13151 If neither of the operands is INTEGER_CST, is_corresponding_member_aggr
13152 returns the largest offset at which the members would be corresponding
13153 members, so perform arg1 <= ret && arg1 == arg2 runtime check. */
13154 gcc_assert (TREE_CODE (arg2) != INTEGER_CST);
13155 if (TREE_CODE (arg1) == INTEGER_CST)
13156 return fold_build2 (EQ_EXPR, boolean_type_node, arg1,
13157 fold_convert (TREE_TYPE (arg1), arg2));
13158 ret = fold_build2 (LE_EXPR, boolean_type_node,
13159 fold_convert (pointer_sized_int_node, arg1),
13160 fold_convert (pointer_sized_int_node, ret));
13161 return fold_build2 (TRUTH_AND_EXPR, boolean_type_node, ret,
13162 fold_build2 (EQ_EXPR, boolean_type_node, arg1,
13163 fold_convert (TREE_TYPE (arg1), arg2)));
13164 }
13165
13166 /* [basic.types] 8. True iff TYPE is an object type. */
13167
13168 static bool
13169 object_type_p (const_tree type)
13170 {
13171 return (TREE_CODE (type) != FUNCTION_TYPE
13172 && !TYPE_REF_P (type)
13173 && !VOID_TYPE_P (type));
13174 }
13175
13176 /* Actually evaluates the trait. */
13177
13178 static bool
13179 trait_expr_value (cp_trait_kind kind, tree type1, tree type2)
13180 {
13181 enum tree_code type_code1;
13182 tree t;
13183
13184 type_code1 = TREE_CODE (type1);
13185
13186 switch (kind)
13187 {
13188 case CPTK_HAS_NOTHROW_ASSIGN:
13189 type1 = strip_array_types (type1);
13190 return (!CP_TYPE_CONST_P (type1) && type_code1 != REFERENCE_TYPE
13191 && (trait_expr_value (CPTK_HAS_TRIVIAL_ASSIGN, type1, type2)
13192 || (CLASS_TYPE_P (type1)
13193 && classtype_has_nothrow_assign_or_copy_p (type1,
13194 true))));
13195
13196 case CPTK_HAS_NOTHROW_CONSTRUCTOR:
13197 type1 = strip_array_types (type1);
13198 return (trait_expr_value (CPTK_HAS_TRIVIAL_CONSTRUCTOR, type1, type2)
13199 || (CLASS_TYPE_P (type1)
13200 && (t = locate_ctor (type1))
13201 && maybe_instantiate_noexcept (t)
13202 && TYPE_NOTHROW_P (TREE_TYPE (t))));
13203
13204 case CPTK_HAS_NOTHROW_COPY:
13205 type1 = strip_array_types (type1);
13206 return (trait_expr_value (CPTK_HAS_TRIVIAL_COPY, type1, type2)
13207 || (CLASS_TYPE_P (type1)
13208 && classtype_has_nothrow_assign_or_copy_p (type1, false)));
13209
13210 case CPTK_HAS_TRIVIAL_ASSIGN:
13211 /* ??? The standard seems to be missing the "or array of such a class
13212 type" wording for this trait. */
13213 type1 = strip_array_types (type1);
13214 return (!CP_TYPE_CONST_P (type1) && type_code1 != REFERENCE_TYPE
13215 && (trivial_type_p (type1)
13216 || (CLASS_TYPE_P (type1)
13217 && TYPE_HAS_TRIVIAL_COPY_ASSIGN (type1))));
13218
13219 case CPTK_HAS_TRIVIAL_CONSTRUCTOR:
13220 type1 = strip_array_types (type1);
13221 return (trivial_type_p (type1)
13222 || (CLASS_TYPE_P (type1) && TYPE_HAS_TRIVIAL_DFLT (type1)));
13223
13224 case CPTK_HAS_TRIVIAL_COPY:
13225 /* ??? The standard seems to be missing the "or array of such a class
13226 type" wording for this trait. */
13227 type1 = strip_array_types (type1);
13228 return (trivial_type_p (type1) || type_code1 == REFERENCE_TYPE
13229 || (CLASS_TYPE_P (type1) && TYPE_HAS_TRIVIAL_COPY_CTOR (type1)));
13230
13231 case CPTK_HAS_TRIVIAL_DESTRUCTOR:
13232 type1 = strip_array_types (type1);
13233 return (trivial_type_p (type1) || type_code1 == REFERENCE_TYPE
13234 || (CLASS_TYPE_P (type1)
13235 && TYPE_HAS_TRIVIAL_DESTRUCTOR (type1)));
13236
13237 case CPTK_HAS_UNIQUE_OBJ_REPRESENTATIONS:
13238 return type_has_unique_obj_representations (type1);
13239
13240 case CPTK_HAS_VIRTUAL_DESTRUCTOR:
13241 return type_has_virtual_destructor (type1);
13242
13243 case CPTK_IS_ABSTRACT:
13244 return ABSTRACT_CLASS_TYPE_P (type1);
13245
13246 case CPTK_IS_AGGREGATE:
13247 return CP_AGGREGATE_TYPE_P (type1);
13248
13249 case CPTK_IS_ARRAY:
13250 return (type_code1 == ARRAY_TYPE
13251 /* We don't want to report T[0] as being an array type.
13252 This is for compatibility with an implementation of
13253 std::is_array by template argument deduction, because
13254 compute_array_index_type_loc rejects a zero-size array
13255 in SFINAE context. */
13256 && !(TYPE_SIZE (type1) && integer_zerop (TYPE_SIZE (type1))));
13257
13258 case CPTK_IS_ASSIGNABLE:
13259 return is_xible (MODIFY_EXPR, type1, type2);
13260
13261 case CPTK_IS_BASE_OF:
13262 return (NON_UNION_CLASS_TYPE_P (type1) && NON_UNION_CLASS_TYPE_P (type2)
13263 && (same_type_ignoring_top_level_qualifiers_p (type1, type2)
13264 || DERIVED_FROM_P (type1, type2)));
13265
13266 case CPTK_IS_BOUNDED_ARRAY:
13267 return (type_code1 == ARRAY_TYPE
13268 && TYPE_DOMAIN (type1)
13269 /* We don't want to report T[0] as being a bounded array type.
13270 This is for compatibility with an implementation of
13271 std::is_bounded_array by template argument deduction, because
13272 compute_array_index_type_loc rejects a zero-size array
13273 in SFINAE context. */
13274 && !(TYPE_SIZE (type1) && integer_zerop (TYPE_SIZE (type1))));
13275
13276 case CPTK_IS_CLASS:
13277 return NON_UNION_CLASS_TYPE_P (type1);
13278
13279 case CPTK_IS_CONST:
13280 return CP_TYPE_CONST_P (type1);
13281
13282 case CPTK_IS_CONSTRUCTIBLE:
13283 return is_xible (INIT_EXPR, type1, type2);
13284
13285 case CPTK_IS_CONVERTIBLE:
13286 return is_convertible (type1, type2);
13287
13288 case CPTK_IS_EMPTY:
13289 return NON_UNION_CLASS_TYPE_P (type1) && CLASSTYPE_EMPTY_P (type1);
13290
13291 case CPTK_IS_ENUM:
13292 return type_code1 == ENUMERAL_TYPE;
13293
13294 case CPTK_IS_FINAL:
13295 return CLASS_TYPE_P (type1) && CLASSTYPE_FINAL (type1);
13296
13297 case CPTK_IS_FUNCTION:
13298 return type_code1 == FUNCTION_TYPE;
13299
13300 case CPTK_IS_INVOCABLE:
13301 return !error_operand_p (build_invoke (type1, type2, tf_none));
13302
13303 case CPTK_IS_LAYOUT_COMPATIBLE:
13304 return layout_compatible_type_p (type1, type2);
13305
13306 case CPTK_IS_LITERAL_TYPE:
13307 return literal_type_p (type1);
13308
13309 case CPTK_IS_MEMBER_FUNCTION_POINTER:
13310 return TYPE_PTRMEMFUNC_P (type1);
13311
13312 case CPTK_IS_MEMBER_OBJECT_POINTER:
13313 return TYPE_PTRDATAMEM_P (type1);
13314
13315 case CPTK_IS_MEMBER_POINTER:
13316 return TYPE_PTRMEM_P (type1);
13317
13318 case CPTK_IS_NOTHROW_ASSIGNABLE:
13319 return is_nothrow_xible (MODIFY_EXPR, type1, type2);
13320
13321 case CPTK_IS_NOTHROW_CONSTRUCTIBLE:
13322 return is_nothrow_xible (INIT_EXPR, type1, type2);
13323
13324 case CPTK_IS_NOTHROW_CONVERTIBLE:
13325 return is_nothrow_convertible (type1, type2);
13326
13327 case CPTK_IS_NOTHROW_INVOCABLE:
13328 return expr_noexcept_p (build_invoke (type1, type2, tf_none), tf_none);
13329
13330 case CPTK_IS_OBJECT:
13331 return object_type_p (type1);
13332
13333 case CPTK_IS_POINTER_INTERCONVERTIBLE_BASE_OF:
13334 return pointer_interconvertible_base_of_p (type1, type2);
13335
13336 case CPTK_IS_POD:
13337 return pod_type_p (type1);
13338
13339 case CPTK_IS_POINTER:
13340 return TYPE_PTR_P (type1);
13341
13342 case CPTK_IS_POLYMORPHIC:
13343 return CLASS_TYPE_P (type1) && TYPE_POLYMORPHIC_P (type1);
13344
13345 case CPTK_IS_REFERENCE:
13346 return type_code1 == REFERENCE_TYPE;
13347
13348 case CPTK_IS_SAME:
13349 return same_type_p (type1, type2);
13350
13351 case CPTK_IS_SCOPED_ENUM:
13352 return SCOPED_ENUM_P (type1);
13353
13354 case CPTK_IS_STD_LAYOUT:
13355 return std_layout_type_p (type1);
13356
13357 case CPTK_IS_TRIVIAL:
13358 return trivial_type_p (type1);
13359
13360 case CPTK_IS_TRIVIALLY_ASSIGNABLE:
13361 return is_trivially_xible (MODIFY_EXPR, type1, type2);
13362
13363 case CPTK_IS_TRIVIALLY_CONSTRUCTIBLE:
13364 return is_trivially_xible (INIT_EXPR, type1, type2);
13365
13366 case CPTK_IS_TRIVIALLY_COPYABLE:
13367 return trivially_copyable_p (type1);
13368
13369 case CPTK_IS_UNBOUNDED_ARRAY:
13370 return array_of_unknown_bound_p (type1);
13371
13372 case CPTK_IS_UNION:
13373 return type_code1 == UNION_TYPE;
13374
13375 case CPTK_IS_VIRTUAL_BASE_OF:
13376 return (NON_UNION_CLASS_TYPE_P (type1) && NON_UNION_CLASS_TYPE_P (type2)
13377 && lookup_base (type2, type1, ba_require_virtual,
13378 NULL, tf_none) != NULL_TREE);
13379
13380 case CPTK_IS_VOLATILE:
13381 return CP_TYPE_VOLATILE_P (type1);
13382
13383 case CPTK_REF_CONSTRUCTS_FROM_TEMPORARY:
13384 return ref_xes_from_temporary (type1, type2, /*direct_init=*/true);
13385
13386 case CPTK_REF_CONVERTS_FROM_TEMPORARY:
13387 return ref_xes_from_temporary (type1, type2, /*direct_init=*/false);
13388
13389 case CPTK_IS_DEDUCIBLE:
13390 return type_targs_deducible_from (type1, type2);
13391
13392 /* __array_rank is handled in finish_trait_expr. */
13393 case CPTK_RANK:
13394 gcc_unreachable ();
13395
13396 #define DEFTRAIT_TYPE(CODE, NAME, ARITY) \
13397 case CPTK_##CODE:
13398 #include "cp-trait.def"
13399 #undef DEFTRAIT_TYPE
13400 /* Type-yielding traits are handled in finish_trait_type. */
13401 break;
13402 }
13403
13404 gcc_unreachable ();
13405 }
13406
13407 /* Returns true if TYPE meets the requirements for the specified KIND,
13408 false otherwise.
13409
13410 When KIND == 1, TYPE must be an array of unknown bound,
13411 or (possibly cv-qualified) void, or a complete type.
13412
13413 When KIND == 2, TYPE must be a complete type, or array of complete type,
13414 or (possibly cv-qualified) void.
13415
13416 When KIND == 3:
13417 If TYPE is a non-union class type, it must be complete.
13418
13419 When KIND == 4:
13420 If TYPE is a class type, it must be complete. */
13421
13422 static bool
13423 check_trait_type (tree type, int kind = 1)
13424 {
13425 if (type == NULL_TREE)
13426 return true;
13427
13428 if (TREE_CODE (type) == TREE_VEC)
13429 {
13430 for (tree arg : tree_vec_range (type))
13431 if (!check_trait_type (arg, kind))
13432 return false;
13433 return true;
13434 }
13435
13436 if (kind == 1 && TREE_CODE (type) == ARRAY_TYPE && !TYPE_DOMAIN (type))
13437 return true; // Array of unknown bound. Don't care about completeness.
13438
13439 if (kind == 3 && !NON_UNION_CLASS_TYPE_P (type))
13440 return true; // Not a non-union class type. Don't care about completeness.
13441
13442 if (kind == 4 && TREE_CODE (type) == ARRAY_TYPE)
13443 return true; // Not a class type. Don't care about completeness.
13444
13445 if (VOID_TYPE_P (type))
13446 return true;
13447
13448 type = complete_type (strip_array_types (type));
13449 if (!COMPLETE_TYPE_P (type)
13450 && cxx_incomplete_type_diagnostic (NULL_TREE, type, DK_PERMERROR)
13451 && !flag_permissive)
13452 return false;
13453 return true;
13454 }
13455
13456 /* True iff the conversion (if any) would be a direct reference
13457 binding, not requiring complete types. This is LWG2939. */
13458
13459 static bool
13460 same_type_ref_bind_p (cp_trait_kind kind, tree type1, tree type2)
13461 {
13462 tree from, to;
13463 switch (kind)
13464 {
13465 /* These put the target type first. */
13466 case CPTK_IS_CONSTRUCTIBLE:
13467 case CPTK_IS_NOTHROW_CONSTRUCTIBLE:
13468 case CPTK_IS_TRIVIALLY_CONSTRUCTIBLE:
13469 case CPTK_IS_INVOCABLE:
13470 case CPTK_IS_NOTHROW_INVOCABLE:
13471 case CPTK_REF_CONSTRUCTS_FROM_TEMPORARY:
13472 case CPTK_REF_CONVERTS_FROM_TEMPORARY:
13473 to = type1;
13474 from = type2;
13475 break;
13476
13477 /* These put it second. */
13478 case CPTK_IS_CONVERTIBLE:
13479 case CPTK_IS_NOTHROW_CONVERTIBLE:
13480 to = type2;
13481 from = type1;
13482 break;
13483
13484 default:
13485 gcc_unreachable ();
13486 }
13487
13488 if (TREE_CODE (to) != REFERENCE_TYPE || !from)
13489 return false;
13490 if (TREE_CODE (from) == TREE_VEC && TREE_VEC_LENGTH (from) == 1)
13491 from = TREE_VEC_ELT (from, 0);
13492 return (TYPE_P (from)
13493 && (same_type_ignoring_top_level_qualifiers_p
13494 (non_reference (to), non_reference (from))));
13495 }
13496
13497 /* [defns.referenceable] True iff TYPE is a referenceable type. */
13498
13499 static bool
13500 referenceable_type_p (const_tree type)
13501 {
13502 return (TYPE_REF_P (type)
13503 || object_type_p (type)
13504 || (FUNC_OR_METHOD_TYPE_P (type)
13505 && (type_memfn_quals (type) == TYPE_UNQUALIFIED
13506 && type_memfn_rqual (type) == REF_QUAL_NONE)));
13507 }
13508
13509 /* Process a trait expression. */
13510
13511 tree
13512 finish_trait_expr (location_t loc, cp_trait_kind kind, tree type1, tree type2)
13513 {
13514 if (type1 == error_mark_node
13515 || type2 == error_mark_node)
13516 return error_mark_node;
13517
13518 if (processing_template_decl)
13519 {
13520 tree trait_expr = make_node (TRAIT_EXPR);
13521 if (kind == CPTK_RANK)
13522 TREE_TYPE (trait_expr) = size_type_node;
13523 else
13524 TREE_TYPE (trait_expr) = boolean_type_node;
13525 TRAIT_EXPR_TYPE1 (trait_expr) = type1;
13526 TRAIT_EXPR_TYPE2 (trait_expr) = type2;
13527 TRAIT_EXPR_KIND (trait_expr) = kind;
13528 TRAIT_EXPR_LOCATION (trait_expr) = loc;
13529 return trait_expr;
13530 }
13531
13532 switch (kind)
13533 {
13534 case CPTK_HAS_NOTHROW_ASSIGN:
13535 case CPTK_HAS_TRIVIAL_ASSIGN:
13536 case CPTK_HAS_NOTHROW_CONSTRUCTOR:
13537 case CPTK_HAS_TRIVIAL_CONSTRUCTOR:
13538 case CPTK_HAS_NOTHROW_COPY:
13539 case CPTK_HAS_TRIVIAL_COPY:
13540 case CPTK_HAS_TRIVIAL_DESTRUCTOR:
13541 if (!check_trait_type (type1))
13542 return error_mark_node;
13543 break;
13544
13545 case CPTK_IS_LITERAL_TYPE:
13546 case CPTK_IS_POD:
13547 case CPTK_IS_STD_LAYOUT:
13548 case CPTK_IS_TRIVIAL:
13549 case CPTK_IS_TRIVIALLY_COPYABLE:
13550 case CPTK_HAS_UNIQUE_OBJ_REPRESENTATIONS:
13551 if (!check_trait_type (type1, /* kind = */ 2))
13552 return error_mark_node;
13553 break;
13554
13555 case CPTK_IS_ABSTRACT:
13556 case CPTK_IS_EMPTY:
13557 case CPTK_IS_POLYMORPHIC:
13558 case CPTK_HAS_VIRTUAL_DESTRUCTOR:
13559 if (!check_trait_type (type1, /* kind = */ 3))
13560 return error_mark_node;
13561 break;
13562
13563 /* N.B. std::is_aggregate is kind=2 but we don't need a complete element
13564 type to know whether an array is an aggregate, so use kind=4 here. */
13565 case CPTK_IS_AGGREGATE:
13566 case CPTK_IS_FINAL:
13567 if (!check_trait_type (type1, /* kind = */ 4))
13568 return error_mark_node;
13569 break;
13570
13571 case CPTK_IS_CONSTRUCTIBLE:
13572 case CPTK_IS_CONVERTIBLE:
13573 case CPTK_IS_INVOCABLE:
13574 case CPTK_IS_NOTHROW_CONSTRUCTIBLE:
13575 case CPTK_IS_NOTHROW_CONVERTIBLE:
13576 case CPTK_IS_NOTHROW_INVOCABLE:
13577 case CPTK_IS_TRIVIALLY_CONSTRUCTIBLE:
13578 case CPTK_REF_CONSTRUCTS_FROM_TEMPORARY:
13579 case CPTK_REF_CONVERTS_FROM_TEMPORARY:
13580 /* Don't check completeness for direct reference binding. */;
13581 if (same_type_ref_bind_p (kind, type1, type2))
13582 break;
13583 gcc_fallthrough ();
13584
13585 case CPTK_IS_ASSIGNABLE:
13586 case CPTK_IS_NOTHROW_ASSIGNABLE:
13587 case CPTK_IS_TRIVIALLY_ASSIGNABLE:
13588 if (!check_trait_type (type1)
13589 || !check_trait_type (type2))
13590 return error_mark_node;
13591 break;
13592
13593 case CPTK_IS_BASE_OF:
13594 case CPTK_IS_POINTER_INTERCONVERTIBLE_BASE_OF:
13595 if (NON_UNION_CLASS_TYPE_P (type1) && NON_UNION_CLASS_TYPE_P (type2)
13596 && !same_type_ignoring_top_level_qualifiers_p (type1, type2)
13597 && !complete_type_or_else (type2, NULL_TREE))
13598 /* We already issued an error. */
13599 return error_mark_node;
13600 break;
13601
13602 case CPTK_IS_VIRTUAL_BASE_OF:
13603 if (NON_UNION_CLASS_TYPE_P (type1) && NON_UNION_CLASS_TYPE_P (type2)
13604 && !complete_type_or_else (type2, NULL_TREE))
13605 /* We already issued an error. */
13606 return error_mark_node;
13607 break;
13608
13609 case CPTK_IS_ARRAY:
13610 case CPTK_IS_BOUNDED_ARRAY:
13611 case CPTK_IS_CLASS:
13612 case CPTK_IS_CONST:
13613 case CPTK_IS_ENUM:
13614 case CPTK_IS_FUNCTION:
13615 case CPTK_IS_MEMBER_FUNCTION_POINTER:
13616 case CPTK_IS_MEMBER_OBJECT_POINTER:
13617 case CPTK_IS_MEMBER_POINTER:
13618 case CPTK_IS_OBJECT:
13619 case CPTK_IS_POINTER:
13620 case CPTK_IS_REFERENCE:
13621 case CPTK_IS_SAME:
13622 case CPTK_IS_SCOPED_ENUM:
13623 case CPTK_IS_UNBOUNDED_ARRAY:
13624 case CPTK_IS_UNION:
13625 case CPTK_IS_VOLATILE:
13626 case CPTK_RANK:
13627 break;
13628
13629 case CPTK_IS_LAYOUT_COMPATIBLE:
13630 if (!array_of_unknown_bound_p (type1)
13631 && TREE_CODE (type1) != VOID_TYPE
13632 && !complete_type_or_else (type1, NULL_TREE))
13633 /* We already issued an error. */
13634 return error_mark_node;
13635 if (!array_of_unknown_bound_p (type2)
13636 && TREE_CODE (type2) != VOID_TYPE
13637 && !complete_type_or_else (type2, NULL_TREE))
13638 /* We already issued an error. */
13639 return error_mark_node;
13640 break;
13641
13642 case CPTK_IS_DEDUCIBLE:
13643 if (!DECL_TYPE_TEMPLATE_P (type1))
13644 {
13645 error ("%qD is not a class or alias template", type1);
13646 return error_mark_node;
13647 }
13648 break;
13649
13650 #define DEFTRAIT_TYPE(CODE, NAME, ARITY) \
13651 case CPTK_##CODE:
13652 #include "cp-trait.def"
13653 #undef DEFTRAIT_TYPE
13654 /* Type-yielding traits are handled in finish_trait_type. */
13655 gcc_unreachable ();
13656 }
13657
13658 tree val;
13659 if (kind == CPTK_RANK)
13660 {
13661 size_t rank = 0;
13662 for (; TREE_CODE (type1) == ARRAY_TYPE; type1 = TREE_TYPE (type1))
13663 ++rank;
13664 val = build_int_cst (size_type_node, rank);
13665 }
13666 else
13667 val = (trait_expr_value (kind, type1, type2)
13668 ? boolean_true_node : boolean_false_node);
13669
13670 return maybe_wrap_with_location (val, loc);
13671 }
13672
13673 /* Process a trait type. */
13674
13675 tree
13676 finish_trait_type (cp_trait_kind kind, tree type1, tree type2,
13677 tsubst_flags_t complain)
13678 {
13679 if (type1 == error_mark_node
13680 || type2 == error_mark_node)
13681 return error_mark_node;
13682
13683 if (processing_template_decl)
13684 {
13685 tree type = cxx_make_type (TRAIT_TYPE);
13686 TRAIT_TYPE_TYPE1 (type) = type1;
13687 TRAIT_TYPE_TYPE2 (type) = type2;
13688 TRAIT_TYPE_KIND_RAW (type) = build_int_cstu (integer_type_node, kind);
13689 /* These traits are intended to be used in the definition of the ::type
13690 member of the corresponding standard library type trait and aren't
13691 mangleable (and thus won't appear directly in template signatures),
13692 so structural equality should suffice. */
13693 SET_TYPE_STRUCTURAL_EQUALITY (type);
13694 return type;
13695 }
13696
13697 switch (kind)
13698 {
13699 case CPTK_ADD_LVALUE_REFERENCE:
13700 /* [meta.trans.ref]. */
13701 if (referenceable_type_p (type1))
13702 return cp_build_reference_type (type1, /*rval=*/false);
13703 return type1;
13704
13705 case CPTK_ADD_POINTER:
13706 /* [meta.trans.ptr]. */
13707 if (VOID_TYPE_P (type1) || referenceable_type_p (type1))
13708 {
13709 if (TYPE_REF_P (type1))
13710 type1 = TREE_TYPE (type1);
13711 return build_pointer_type (type1);
13712 }
13713 return type1;
13714
13715 case CPTK_ADD_RVALUE_REFERENCE:
13716 /* [meta.trans.ref]. */
13717 if (referenceable_type_p (type1))
13718 return cp_build_reference_type (type1, /*rval=*/true);
13719 return type1;
13720
13721 case CPTK_DECAY:
13722 if (TYPE_REF_P (type1))
13723 type1 = TREE_TYPE (type1);
13724
13725 if (TREE_CODE (type1) == ARRAY_TYPE)
13726 return finish_trait_type (CPTK_ADD_POINTER, TREE_TYPE (type1), type2,
13727 complain);
13728 else if (TREE_CODE (type1) == FUNCTION_TYPE)
13729 return finish_trait_type (CPTK_ADD_POINTER, type1, type2, complain);
13730 else
13731 return cv_unqualified (type1);
13732
13733 case CPTK_REMOVE_ALL_EXTENTS:
13734 return strip_array_types (type1);
13735
13736 case CPTK_REMOVE_CV:
13737 return cv_unqualified (type1);
13738
13739 case CPTK_REMOVE_CVREF:
13740 if (TYPE_REF_P (type1))
13741 type1 = TREE_TYPE (type1);
13742 return cv_unqualified (type1);
13743
13744 case CPTK_REMOVE_EXTENT:
13745 if (TREE_CODE (type1) == ARRAY_TYPE)
13746 type1 = TREE_TYPE (type1);
13747 return type1;
13748
13749 case CPTK_REMOVE_POINTER:
13750 if (TYPE_PTR_P (type1))
13751 type1 = TREE_TYPE (type1);
13752 return type1;
13753
13754 case CPTK_REMOVE_REFERENCE:
13755 if (TYPE_REF_P (type1))
13756 type1 = TREE_TYPE (type1);
13757 return type1;
13758
13759 case CPTK_TYPE_PACK_ELEMENT:
13760 return finish_type_pack_element (type1, type2, complain);
13761
13762 case CPTK_UNDERLYING_TYPE:
13763 return finish_underlying_type (type1);
13764
13765 #define DEFTRAIT_EXPR(CODE, NAME, ARITY) \
13766 case CPTK_##CODE:
13767 #include "cp-trait.def"
13768 #undef DEFTRAIT_EXPR
13769 /* Expression-yielding traits are handled in finish_trait_expr. */
13770 case CPTK_BASES:
13771 case CPTK_DIRECT_BASES:
13772 /* BASES and DIRECT_BASES are handled in finish_bases. */
13773 break;
13774 }
13775
13776 gcc_unreachable ();
13777 }
13778
13779 /* Do-nothing variants of functions to handle pragma FLOAT_CONST_DECIMAL64,
13780 which is ignored for C++. */
13781
13782 void
13783 set_float_const_decimal64 (void)
13784 {
13785 }
13786
13787 void
13788 clear_float_const_decimal64 (void)
13789 {
13790 }
13791
13792 bool
13793 float_const_decimal64_p (void)
13794 {
13795 return 0;
13796 }
13797
13798
13799 /* Return true if T designates the implied `this' parameter. */
13800
13801 bool
13802 is_this_parameter (tree t)
13803 {
13804 if (!DECL_P (t) || DECL_NAME (t) != this_identifier)
13805 return false;
13806 gcc_assert (TREE_CODE (t) == PARM_DECL
13807 || (VAR_P (t) && DECL_HAS_VALUE_EXPR_P (t))
13808 || (cp_binding_oracle && VAR_P (t)));
13809 return true;
13810 }
13811
13812 /* As above, or a C++23 explicit object parameter. */
13813
13814 bool
13815 is_object_parameter (tree t)
13816 {
13817 if (is_this_parameter (t))
13818 return true;
13819 if (TREE_CODE (t) != PARM_DECL)
13820 return false;
13821 tree ctx = DECL_CONTEXT (t);
13822 return (ctx && DECL_XOBJ_MEMBER_FUNCTION_P (ctx)
13823 && t == DECL_ARGUMENTS (ctx));
13824 }
13825
13826 /* Insert the deduced return type for an auto function. */
13827
13828 void
13829 apply_deduced_return_type (tree fco, tree return_type)
13830 {
13831 tree result;
13832
13833 if (return_type == error_mark_node)
13834 return;
13835
13836 if (DECL_CONV_FN_P (fco))
13837 DECL_NAME (fco) = make_conv_op_name (return_type);
13838
13839 TREE_TYPE (fco) = change_return_type (return_type, TREE_TYPE (fco));
13840
13841 maybe_update_postconditions (fco);
13842
13843 /* Apply the type to the result object. */
13844
13845 result = DECL_RESULT (fco);
13846 if (result == NULL_TREE)
13847 return;
13848 if (TREE_TYPE (result) == return_type)
13849 return;
13850
13851 if (!processing_template_decl && !VOID_TYPE_P (return_type)
13852 && !complete_type_or_else (return_type, NULL_TREE))
13853 return;
13854
13855 /* We already have a DECL_RESULT from start_preparsed_function.
13856 Now we need to redo the work it and allocate_struct_function
13857 did to reflect the new type. */
13858 result = build_decl (DECL_SOURCE_LOCATION (result), RESULT_DECL, NULL_TREE,
13859 TYPE_MAIN_VARIANT (return_type));
13860 DECL_ARTIFICIAL (result) = 1;
13861 DECL_IGNORED_P (result) = 1;
13862 cp_apply_type_quals_to_decl (cp_type_quals (return_type),
13863 result);
13864 DECL_RESULT (fco) = result;
13865
13866 if (!processing_template_decl)
13867 if (function *fun = DECL_STRUCT_FUNCTION (fco))
13868 {
13869 bool aggr = aggregate_value_p (result, fco);
13870 #ifdef PCC_STATIC_STRUCT_RETURN
13871 fun->returns_pcc_struct = aggr;
13872 #endif
13873 fun->returns_struct = aggr;
13874 }
13875 }
13876
13877 /* Build a unary fold expression of EXPR over OP. If IS_RIGHT is true,
13878 this is a right unary fold. Otherwise it is a left unary fold. */
13879
13880 static tree
13881 finish_unary_fold_expr (location_t loc, tree expr, int op, tree_code dir)
13882 {
13883 /* Build a pack expansion (assuming expr has pack type). */
13884 if (!uses_parameter_packs (expr))
13885 {
13886 error_at (location_of (expr), "operand of fold expression has no "
13887 "unexpanded parameter packs");
13888 return error_mark_node;
13889 }
13890 tree pack = make_pack_expansion (expr);
13891
13892 /* Build the fold expression. */
13893 tree code = build_int_cstu (integer_type_node, abs (op));
13894 tree fold = build_min_nt_loc (loc, dir, code, pack);
13895 FOLD_EXPR_MODIFY_P (fold) = (op < 0);
13896 TREE_TYPE (fold) = build_dependent_operator_type (NULL_TREE,
13897 FOLD_EXPR_OP (fold),
13898 FOLD_EXPR_MODIFY_P (fold));
13899 return fold;
13900 }
13901
13902 tree
13903 finish_left_unary_fold_expr (location_t loc, tree expr, int op)
13904 {
13905 return finish_unary_fold_expr (loc, expr, op, UNARY_LEFT_FOLD_EXPR);
13906 }
13907
13908 tree
13909 finish_right_unary_fold_expr (location_t loc, tree expr, int op)
13910 {
13911 return finish_unary_fold_expr (loc, expr, op, UNARY_RIGHT_FOLD_EXPR);
13912 }
13913
13914 /* Build a binary fold expression over EXPR1 and EXPR2. The
13915 associativity of the fold is determined by EXPR1 and EXPR2 (whichever
13916 has an unexpanded parameter pack). */
13917
13918 static tree
13919 finish_binary_fold_expr (location_t loc, tree pack, tree init,
13920 int op, tree_code dir)
13921 {
13922 pack = make_pack_expansion (pack);
13923 tree code = build_int_cstu (integer_type_node, abs (op));
13924 tree fold = build_min_nt_loc (loc, dir, code, pack, init);
13925 FOLD_EXPR_MODIFY_P (fold) = (op < 0);
13926 TREE_TYPE (fold) = build_dependent_operator_type (NULL_TREE,
13927 FOLD_EXPR_OP (fold),
13928 FOLD_EXPR_MODIFY_P (fold));
13929 return fold;
13930 }
13931
13932 tree
13933 finish_binary_fold_expr (location_t loc, tree expr1, tree expr2, int op)
13934 {
13935 // Determine which expr has an unexpanded parameter pack and
13936 // set the pack and initial term.
13937 bool pack1 = uses_parameter_packs (expr1);
13938 bool pack2 = uses_parameter_packs (expr2);
13939 if (pack1 && !pack2)
13940 return finish_binary_fold_expr (loc, expr1, expr2, op, BINARY_RIGHT_FOLD_EXPR);
13941 else if (pack2 && !pack1)
13942 return finish_binary_fold_expr (loc, expr2, expr1, op, BINARY_LEFT_FOLD_EXPR);
13943 else
13944 {
13945 if (pack1)
13946 error ("both arguments in binary fold have unexpanded parameter packs");
13947 else
13948 error ("no unexpanded parameter packs in binary fold");
13949 }
13950 return error_mark_node;
13951 }
13952
13953 /* Finish __builtin_launder (arg). */
13954
13955 tree
13956 finish_builtin_launder (location_t loc, tree arg, tsubst_flags_t complain)
13957 {
13958 tree orig_arg = arg;
13959 if (!type_dependent_expression_p (arg))
13960 arg = decay_conversion (arg, complain);
13961 if (error_operand_p (arg))
13962 return error_mark_node;
13963 if (!type_dependent_expression_p (arg) && !TYPE_PTROB_P (TREE_TYPE (arg)))
13964 {
13965 error_at (loc, "type %qT of argument to %<__builtin_launder%> "
13966 "is not a pointer to object type", TREE_TYPE (arg));
13967 return error_mark_node;
13968 }
13969 if (processing_template_decl)
13970 arg = orig_arg;
13971 return build_call_expr_internal_loc (loc, IFN_LAUNDER,
13972 TREE_TYPE (arg), 1, arg);
13973 }
13974
13975 /* Finish __builtin_convertvector (arg, type). */
13976
13977 tree
13978 cp_build_vec_convert (tree arg, location_t loc, tree type,
13979 tsubst_flags_t complain)
13980 {
13981 if (error_operand_p (type))
13982 return error_mark_node;
13983 if (error_operand_p (arg))
13984 return error_mark_node;
13985
13986 tree ret = NULL_TREE;
13987 if (!type_dependent_expression_p (arg) && !dependent_type_p (type))
13988 ret = c_build_vec_convert (cp_expr_loc_or_input_loc (arg),
13989 decay_conversion (arg, complain),
13990 loc, type, (complain & tf_error) != 0);
13991
13992 if (!processing_template_decl)
13993 return ret;
13994
13995 return build_call_expr_internal_loc (loc, IFN_VEC_CONVERT, type, 1, arg);
13996 }
13997
13998 /* Finish __builtin_bit_cast (type, arg). */
13999
14000 tree
14001 cp_build_bit_cast (location_t loc, tree type, tree arg,
14002 tsubst_flags_t complain)
14003 {
14004 if (error_operand_p (type))
14005 return error_mark_node;
14006 if (!dependent_type_p (type))
14007 {
14008 if (!complete_type_or_maybe_complain (type, NULL_TREE, complain))
14009 return error_mark_node;
14010 if (TREE_CODE (type) == ARRAY_TYPE)
14011 {
14012 /* std::bit_cast for destination ARRAY_TYPE is not possible,
14013 as functions may not return an array, so don't bother trying
14014 to support this (and then deal with VLAs etc.). */
14015 error_at (loc, "%<__builtin_bit_cast%> destination type %qT "
14016 "is an array type", type);
14017 return error_mark_node;
14018 }
14019 if (!trivially_copyable_p (type))
14020 {
14021 error_at (loc, "%<__builtin_bit_cast%> destination type %qT "
14022 "is not trivially copyable", type);
14023 return error_mark_node;
14024 }
14025 }
14026
14027 if (error_operand_p (arg))
14028 return error_mark_node;
14029
14030 if (!type_dependent_expression_p (arg))
14031 {
14032 if (TREE_CODE (TREE_TYPE (arg)) == ARRAY_TYPE)
14033 {
14034 /* Don't perform array-to-pointer conversion. */
14035 arg = mark_rvalue_use (arg, loc, true);
14036 if (!complete_type_or_maybe_complain (TREE_TYPE (arg), arg, complain))
14037 return error_mark_node;
14038 }
14039 else
14040 arg = decay_conversion (arg, complain);
14041
14042 if (error_operand_p (arg))
14043 return error_mark_node;
14044
14045 if (!trivially_copyable_p (TREE_TYPE (arg)))
14046 {
14047 error_at (cp_expr_loc_or_loc (arg, loc),
14048 "%<__builtin_bit_cast%> source type %qT "
14049 "is not trivially copyable", TREE_TYPE (arg));
14050 return error_mark_node;
14051 }
14052 if (!dependent_type_p (type)
14053 && !cp_tree_equal (TYPE_SIZE_UNIT (type),
14054 TYPE_SIZE_UNIT (TREE_TYPE (arg))))
14055 {
14056 error_at (loc, "%<__builtin_bit_cast%> source size %qE "
14057 "not equal to destination type size %qE",
14058 TYPE_SIZE_UNIT (TREE_TYPE (arg)),
14059 TYPE_SIZE_UNIT (type));
14060 return error_mark_node;
14061 }
14062 }
14063
14064 tree ret = build_min (BIT_CAST_EXPR, type, arg);
14065 SET_EXPR_LOCATION (ret, loc);
14066
14067 if (!processing_template_decl && CLASS_TYPE_P (type))
14068 ret = get_target_expr (ret, complain);
14069
14070 return ret;
14071 }
14072
14073 /* Diagnose invalid #pragma GCC unroll argument and adjust
14074 it if needed. */
14075
14076 tree
14077 cp_check_pragma_unroll (location_t loc, tree unroll)
14078 {
14079 HOST_WIDE_INT lunroll = 0;
14080 if (type_dependent_expression_p (unroll))
14081 ;
14082 else if (!INTEGRAL_TYPE_P (TREE_TYPE (unroll))
14083 || (!value_dependent_expression_p (unroll)
14084 && (!tree_fits_shwi_p (unroll)
14085 || (lunroll = tree_to_shwi (unroll)) < 0
14086 || lunroll >= USHRT_MAX)))
14087 {
14088 error_at (loc, "%<#pragma GCC unroll%> requires an"
14089 " assignment-expression that evaluates to a non-negative"
14090 " integral constant less than %u", USHRT_MAX);
14091 unroll = integer_one_node;
14092 }
14093 else if (TREE_CODE (unroll) == INTEGER_CST)
14094 {
14095 unroll = fold_convert (integer_type_node, unroll);
14096 if (integer_zerop (unroll))
14097 unroll = integer_one_node;
14098 }
14099 return unroll;
14100 }
14101
14102 #include "gt-cp-semantics.h"